spi-bcm63xx-hsspi.c 12 KB

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