spi-bcm63xx-hsspi.c 14 KB

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