spi-bcm63xx-hsspi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Broadcom BCM63XX High Speed SPI Controller driver
  3. *
  4. * Copyright 2000-2010 Broadcom Corporation
  5. * Copyright 2012-2013 Jonas Gorski <jogo@openwrt.org>
  6. *
  7. * Licensed under the GNU/GPL. See COPYING for details.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/io.h>
  12. #include <linux/clk.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/delay.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/err.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/mutex.h>
  21. #define HSSPI_GLOBAL_CTRL_REG 0x0
  22. #define GLOBAL_CTRL_CS_POLARITY_SHIFT 0
  23. #define GLOBAL_CTRL_CS_POLARITY_MASK 0x000000ff
  24. #define GLOBAL_CTRL_PLL_CLK_CTRL_SHIFT 8
  25. #define GLOBAL_CTRL_PLL_CLK_CTRL_MASK 0x0000ff00
  26. #define GLOBAL_CTRL_CLK_GATE_SSOFF BIT(16)
  27. #define GLOBAL_CTRL_CLK_POLARITY BIT(17)
  28. #define GLOBAL_CTRL_MOSI_IDLE BIT(18)
  29. #define HSSPI_GLOBAL_EXT_TRIGGER_REG 0x4
  30. #define HSSPI_INT_STATUS_REG 0x8
  31. #define HSSPI_INT_STATUS_MASKED_REG 0xc
  32. #define HSSPI_INT_MASK_REG 0x10
  33. #define HSSPI_PINGx_CMD_DONE(i) BIT((i * 8) + 0)
  34. #define HSSPI_PINGx_RX_OVER(i) BIT((i * 8) + 1)
  35. #define HSSPI_PINGx_TX_UNDER(i) BIT((i * 8) + 2)
  36. #define HSSPI_PINGx_POLL_TIMEOUT(i) BIT((i * 8) + 3)
  37. #define HSSPI_PINGx_CTRL_INVAL(i) BIT((i * 8) + 4)
  38. #define HSSPI_INT_CLEAR_ALL 0xff001f1f
  39. #define HSSPI_PINGPONG_COMMAND_REG(x) (0x80 + (x) * 0x40)
  40. #define PINGPONG_CMD_COMMAND_MASK 0xf
  41. #define PINGPONG_COMMAND_NOOP 0
  42. #define PINGPONG_COMMAND_START_NOW 1
  43. #define PINGPONG_COMMAND_START_TRIGGER 2
  44. #define PINGPONG_COMMAND_HALT 3
  45. #define PINGPONG_COMMAND_FLUSH 4
  46. #define PINGPONG_CMD_PROFILE_SHIFT 8
  47. #define PINGPONG_CMD_SS_SHIFT 12
  48. #define HSSPI_PINGPONG_STATUS_REG(x) (0x84 + (x) * 0x40)
  49. #define HSSPI_PROFILE_CLK_CTRL_REG(x) (0x100 + (x) * 0x20)
  50. #define CLK_CTRL_FREQ_CTRL_MASK 0x0000ffff
  51. #define CLK_CTRL_SPI_CLK_2X_SEL BIT(14)
  52. #define CLK_CTRL_ACCUM_RST_ON_LOOP BIT(15)
  53. #define HSSPI_PROFILE_SIGNAL_CTRL_REG(x) (0x104 + (x) * 0x20)
  54. #define SIGNAL_CTRL_LATCH_RISING BIT(12)
  55. #define SIGNAL_CTRL_LAUNCH_RISING BIT(13)
  56. #define SIGNAL_CTRL_ASYNC_INPUT_PATH BIT(16)
  57. #define HSSPI_PROFILE_MODE_CTRL_REG(x) (0x108 + (x) * 0x20)
  58. #define MODE_CTRL_MULTIDATA_RD_STRT_SHIFT 8
  59. #define MODE_CTRL_MULTIDATA_WR_STRT_SHIFT 12
  60. #define MODE_CTRL_MULTIDATA_RD_SIZE_SHIFT 16
  61. #define MODE_CTRL_MULTIDATA_WR_SIZE_SHIFT 18
  62. #define MODE_CTRL_MODE_3WIRE BIT(20)
  63. #define MODE_CTRL_PREPENDBYTE_CNT_SHIFT 24
  64. #define HSSPI_FIFO_REG(x) (0x200 + (x) * 0x200)
  65. #define HSSPI_OP_MULTIBIT BIT(11)
  66. #define HSSPI_OP_CODE_SHIFT 13
  67. #define HSSPI_OP_SLEEP (0 << HSSPI_OP_CODE_SHIFT)
  68. #define HSSPI_OP_READ_WRITE (1 << HSSPI_OP_CODE_SHIFT)
  69. #define HSSPI_OP_WRITE (2 << HSSPI_OP_CODE_SHIFT)
  70. #define HSSPI_OP_READ (3 << HSSPI_OP_CODE_SHIFT)
  71. #define HSSPI_OP_SETIRQ (4 << HSSPI_OP_CODE_SHIFT)
  72. #define HSSPI_BUFFER_LEN 512
  73. #define HSSPI_OPCODE_LEN 2
  74. #define HSSPI_MAX_PREPEND_LEN 15
  75. #define HSSPI_MAX_SYNC_CLOCK 30000000
  76. #define HSSPI_BUS_NUM 1 /* 0 is legacy SPI */
  77. struct bcm63xx_hsspi {
  78. struct completion done;
  79. struct mutex bus_mutex;
  80. struct platform_device *pdev;
  81. struct clk *clk;
  82. void __iomem *regs;
  83. u8 __iomem *fifo;
  84. u32 speed_hz;
  85. u8 cs_polarity;
  86. };
  87. static void bcm63xx_hsspi_set_cs(struct bcm63xx_hsspi *bs, unsigned cs,
  88. bool active)
  89. {
  90. u32 reg;
  91. mutex_lock(&bs->bus_mutex);
  92. reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
  93. reg &= ~BIT(cs);
  94. if (active == !(bs->cs_polarity & BIT(cs)))
  95. reg |= BIT(cs);
  96. __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
  97. mutex_unlock(&bs->bus_mutex);
  98. }
  99. static void bcm63xx_hsspi_set_clk(struct bcm63xx_hsspi *bs,
  100. struct spi_device *spi, int hz)
  101. {
  102. unsigned profile = spi->chip_select;
  103. u32 reg;
  104. reg = DIV_ROUND_UP(2048, DIV_ROUND_UP(bs->speed_hz, hz));
  105. __raw_writel(CLK_CTRL_ACCUM_RST_ON_LOOP | reg,
  106. bs->regs + HSSPI_PROFILE_CLK_CTRL_REG(profile));
  107. reg = __raw_readl(bs->regs + HSSPI_PROFILE_SIGNAL_CTRL_REG(profile));
  108. if (hz > HSSPI_MAX_SYNC_CLOCK)
  109. reg |= SIGNAL_CTRL_ASYNC_INPUT_PATH;
  110. else
  111. reg &= ~SIGNAL_CTRL_ASYNC_INPUT_PATH;
  112. __raw_writel(reg, bs->regs + HSSPI_PROFILE_SIGNAL_CTRL_REG(profile));
  113. mutex_lock(&bs->bus_mutex);
  114. /* setup clock polarity */
  115. reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
  116. reg &= ~GLOBAL_CTRL_CLK_POLARITY;
  117. if (spi->mode & SPI_CPOL)
  118. reg |= GLOBAL_CTRL_CLK_POLARITY;
  119. __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
  120. mutex_unlock(&bs->bus_mutex);
  121. }
  122. static int bcm63xx_hsspi_do_txrx(struct spi_device *spi, struct spi_transfer *t)
  123. {
  124. struct bcm63xx_hsspi *bs = spi_master_get_devdata(spi->master);
  125. unsigned chip_select = spi->chip_select;
  126. u16 opcode = 0;
  127. int pending = t->len;
  128. int step_size = HSSPI_BUFFER_LEN;
  129. const u8 *tx = t->tx_buf;
  130. u8 *rx = t->rx_buf;
  131. bcm63xx_hsspi_set_clk(bs, spi, t->speed_hz);
  132. bcm63xx_hsspi_set_cs(bs, spi->chip_select, true);
  133. if (tx && rx)
  134. opcode = HSSPI_OP_READ_WRITE;
  135. else if (tx)
  136. opcode = HSSPI_OP_WRITE;
  137. else if (rx)
  138. opcode = HSSPI_OP_READ;
  139. if (opcode != HSSPI_OP_READ)
  140. step_size -= HSSPI_OPCODE_LEN;
  141. if ((opcode == HSSPI_OP_READ && t->rx_nbits == SPI_NBITS_DUAL) ||
  142. (opcode == HSSPI_OP_WRITE && t->tx_nbits == SPI_NBITS_DUAL))
  143. opcode |= HSSPI_OP_MULTIBIT;
  144. __raw_writel(1 << MODE_CTRL_MULTIDATA_WR_SIZE_SHIFT |
  145. 1 << MODE_CTRL_MULTIDATA_RD_SIZE_SHIFT | 0xff,
  146. bs->regs + HSSPI_PROFILE_MODE_CTRL_REG(chip_select));
  147. while (pending > 0) {
  148. int curr_step = min_t(int, step_size, pending);
  149. reinit_completion(&bs->done);
  150. if (tx) {
  151. memcpy_toio(bs->fifo + HSSPI_OPCODE_LEN, tx, curr_step);
  152. tx += curr_step;
  153. }
  154. __raw_writew(opcode | curr_step, bs->fifo);
  155. /* enable interrupt */
  156. __raw_writel(HSSPI_PINGx_CMD_DONE(0),
  157. bs->regs + HSSPI_INT_MASK_REG);
  158. /* start the transfer */
  159. __raw_writel(!chip_select << PINGPONG_CMD_SS_SHIFT |
  160. chip_select << PINGPONG_CMD_PROFILE_SHIFT |
  161. PINGPONG_COMMAND_START_NOW,
  162. bs->regs + HSSPI_PINGPONG_COMMAND_REG(0));
  163. if (wait_for_completion_timeout(&bs->done, HZ) == 0) {
  164. dev_err(&bs->pdev->dev, "transfer timed out!\n");
  165. return -ETIMEDOUT;
  166. }
  167. if (rx) {
  168. memcpy_fromio(rx, bs->fifo, curr_step);
  169. rx += curr_step;
  170. }
  171. pending -= curr_step;
  172. }
  173. return 0;
  174. }
  175. static int bcm63xx_hsspi_setup(struct spi_device *spi)
  176. {
  177. struct bcm63xx_hsspi *bs = spi_master_get_devdata(spi->master);
  178. u32 reg;
  179. reg = __raw_readl(bs->regs +
  180. HSSPI_PROFILE_SIGNAL_CTRL_REG(spi->chip_select));
  181. reg &= ~(SIGNAL_CTRL_LAUNCH_RISING | SIGNAL_CTRL_LATCH_RISING);
  182. if (spi->mode & SPI_CPHA)
  183. reg |= SIGNAL_CTRL_LAUNCH_RISING;
  184. else
  185. reg |= SIGNAL_CTRL_LATCH_RISING;
  186. __raw_writel(reg, bs->regs +
  187. HSSPI_PROFILE_SIGNAL_CTRL_REG(spi->chip_select));
  188. mutex_lock(&bs->bus_mutex);
  189. reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
  190. /* only change actual polarities if there is no transfer */
  191. if ((reg & GLOBAL_CTRL_CS_POLARITY_MASK) == bs->cs_polarity) {
  192. if (spi->mode & SPI_CS_HIGH)
  193. reg |= BIT(spi->chip_select);
  194. else
  195. reg &= ~BIT(spi->chip_select);
  196. __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
  197. }
  198. if (spi->mode & SPI_CS_HIGH)
  199. bs->cs_polarity |= BIT(spi->chip_select);
  200. else
  201. bs->cs_polarity &= ~BIT(spi->chip_select);
  202. mutex_unlock(&bs->bus_mutex);
  203. return 0;
  204. }
  205. static int bcm63xx_hsspi_transfer_one(struct spi_master *master,
  206. struct spi_message *msg)
  207. {
  208. struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
  209. struct spi_transfer *t;
  210. struct spi_device *spi = msg->spi;
  211. int status = -EINVAL;
  212. int dummy_cs;
  213. u32 reg;
  214. /* This controller does not support keeping CS active during idle.
  215. * To work around this, we use the following ugly hack:
  216. *
  217. * a. Invert the target chip select's polarity so it will be active.
  218. * b. Select a "dummy" chip select to use as the hardware target.
  219. * c. Invert the dummy chip select's polarity so it will be inactive
  220. * during the actual transfers.
  221. * d. Tell the hardware to send to the dummy chip select. Thanks to
  222. * the multiplexed nature of SPI the actual target will receive
  223. * the transfer and we see its response.
  224. *
  225. * e. At the end restore the polarities again to their default values.
  226. */
  227. dummy_cs = !spi->chip_select;
  228. bcm63xx_hsspi_set_cs(bs, dummy_cs, true);
  229. list_for_each_entry(t, &msg->transfers, transfer_list) {
  230. status = bcm63xx_hsspi_do_txrx(spi, t);
  231. if (status)
  232. break;
  233. msg->actual_length += t->len;
  234. if (t->delay_usecs)
  235. udelay(t->delay_usecs);
  236. if (t->cs_change)
  237. bcm63xx_hsspi_set_cs(bs, spi->chip_select, false);
  238. }
  239. mutex_lock(&bs->bus_mutex);
  240. reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
  241. reg &= ~GLOBAL_CTRL_CS_POLARITY_MASK;
  242. reg |= bs->cs_polarity;
  243. __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
  244. mutex_unlock(&bs->bus_mutex);
  245. msg->status = status;
  246. spi_finalize_current_message(master);
  247. return 0;
  248. }
  249. static irqreturn_t bcm63xx_hsspi_interrupt(int irq, void *dev_id)
  250. {
  251. struct bcm63xx_hsspi *bs = (struct bcm63xx_hsspi *)dev_id;
  252. if (__raw_readl(bs->regs + HSSPI_INT_STATUS_MASKED_REG) == 0)
  253. return IRQ_NONE;
  254. __raw_writel(HSSPI_INT_CLEAR_ALL, bs->regs + HSSPI_INT_STATUS_REG);
  255. __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG);
  256. complete(&bs->done);
  257. return IRQ_HANDLED;
  258. }
  259. static int bcm63xx_hsspi_probe(struct platform_device *pdev)
  260. {
  261. struct spi_master *master;
  262. struct bcm63xx_hsspi *bs;
  263. struct resource *res_mem;
  264. void __iomem *regs;
  265. struct device *dev = &pdev->dev;
  266. struct clk *clk;
  267. int irq, ret;
  268. u32 reg, rate;
  269. irq = platform_get_irq(pdev, 0);
  270. if (irq < 0) {
  271. dev_err(dev, "no irq: %d\n", irq);
  272. return irq;
  273. }
  274. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  275. regs = devm_ioremap_resource(dev, res_mem);
  276. if (IS_ERR(regs))
  277. return PTR_ERR(regs);
  278. clk = devm_clk_get(dev, "hsspi");
  279. if (IS_ERR(clk))
  280. return PTR_ERR(clk);
  281. rate = clk_get_rate(clk);
  282. if (!rate)
  283. return -EINVAL;
  284. ret = clk_prepare_enable(clk);
  285. if (ret)
  286. return ret;
  287. master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  288. if (!master) {
  289. ret = -ENOMEM;
  290. goto out_disable_clk;
  291. }
  292. bs = spi_master_get_devdata(master);
  293. bs->pdev = pdev;
  294. bs->clk = clk;
  295. bs->regs = regs;
  296. bs->speed_hz = rate;
  297. bs->fifo = (u8 __iomem *)(bs->regs + HSSPI_FIFO_REG(0));
  298. mutex_init(&bs->bus_mutex);
  299. init_completion(&bs->done);
  300. master->bus_num = HSSPI_BUS_NUM;
  301. master->num_chipselect = 8;
  302. master->setup = bcm63xx_hsspi_setup;
  303. master->transfer_one_message = bcm63xx_hsspi_transfer_one;
  304. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH |
  305. SPI_RX_DUAL | SPI_TX_DUAL;
  306. master->bits_per_word_mask = SPI_BPW_MASK(8);
  307. master->auto_runtime_pm = true;
  308. platform_set_drvdata(pdev, master);
  309. /* Initialize the hardware */
  310. __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG);
  311. /* clean up any pending interrupts */
  312. __raw_writel(HSSPI_INT_CLEAR_ALL, bs->regs + HSSPI_INT_STATUS_REG);
  313. /* read out default CS polarities */
  314. reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
  315. bs->cs_polarity = reg & GLOBAL_CTRL_CS_POLARITY_MASK;
  316. __raw_writel(reg | GLOBAL_CTRL_CLK_GATE_SSOFF,
  317. bs->regs + HSSPI_GLOBAL_CTRL_REG);
  318. ret = devm_request_irq(dev, irq, bcm63xx_hsspi_interrupt, IRQF_SHARED,
  319. pdev->name, bs);
  320. if (ret)
  321. goto out_put_master;
  322. /* register and we are done */
  323. ret = devm_spi_register_master(dev, master);
  324. if (ret)
  325. goto out_put_master;
  326. return 0;
  327. out_put_master:
  328. spi_master_put(master);
  329. out_disable_clk:
  330. clk_disable_unprepare(clk);
  331. return ret;
  332. }
  333. static int bcm63xx_hsspi_remove(struct platform_device *pdev)
  334. {
  335. struct spi_master *master = platform_get_drvdata(pdev);
  336. struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
  337. /* reset the hardware and block queue progress */
  338. __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG);
  339. clk_disable_unprepare(bs->clk);
  340. return 0;
  341. }
  342. #ifdef CONFIG_PM_SLEEP
  343. static int bcm63xx_hsspi_suspend(struct device *dev)
  344. {
  345. struct spi_master *master = dev_get_drvdata(dev);
  346. struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
  347. spi_master_suspend(master);
  348. clk_disable_unprepare(bs->clk);
  349. return 0;
  350. }
  351. static int bcm63xx_hsspi_resume(struct device *dev)
  352. {
  353. struct spi_master *master = dev_get_drvdata(dev);
  354. struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
  355. int ret;
  356. ret = clk_prepare_enable(bs->clk);
  357. if (ret)
  358. return ret;
  359. spi_master_resume(master);
  360. return 0;
  361. }
  362. #endif
  363. static SIMPLE_DEV_PM_OPS(bcm63xx_hsspi_pm_ops, bcm63xx_hsspi_suspend,
  364. bcm63xx_hsspi_resume);
  365. static struct platform_driver bcm63xx_hsspi_driver = {
  366. .driver = {
  367. .name = "bcm63xx-hsspi",
  368. .pm = &bcm63xx_hsspi_pm_ops,
  369. },
  370. .probe = bcm63xx_hsspi_probe,
  371. .remove = bcm63xx_hsspi_remove,
  372. };
  373. module_platform_driver(bcm63xx_hsspi_driver);
  374. MODULE_ALIAS("platform:bcm63xx_hsspi");
  375. MODULE_DESCRIPTION("Broadcom BCM63xx High Speed SPI Controller driver");
  376. MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
  377. MODULE_LICENSE("GPL");