spi-bcm63xx-hsspi.c 12 KB

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