spi-bcm2835aux.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * Driver for Broadcom BCM2835 auxiliary SPI Controllers
  3. *
  4. * the driver does not rely on the native chipselects at all
  5. * but only uses the gpio type chipselects
  6. *
  7. * Based on: spi-bcm2835.c
  8. *
  9. * Copyright (C) 2015 Martin Sperl
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/completion.h>
  23. #include <linux/delay.h>
  24. #include <linux/err.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/of.h>
  30. #include <linux/of_address.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/of_irq.h>
  34. #include <linux/regmap.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/spinlock.h>
  37. /*
  38. * spi register defines
  39. *
  40. * note there is garbage in the "official" documentation,
  41. * so some data is taken from the file:
  42. * brcm_usrlib/dag/vmcsx/vcinclude/bcm2708_chip/aux_io.h
  43. * inside of:
  44. * http://www.broadcom.com/docs/support/videocore/Brcm_Android_ICS_Graphics_Stack.tar.gz
  45. */
  46. /* SPI register offsets */
  47. #define BCM2835_AUX_SPI_CNTL0 0x00
  48. #define BCM2835_AUX_SPI_CNTL1 0x04
  49. #define BCM2835_AUX_SPI_STAT 0x08
  50. #define BCM2835_AUX_SPI_PEEK 0x0C
  51. #define BCM2835_AUX_SPI_IO 0x20
  52. #define BCM2835_AUX_SPI_TXHOLD 0x30
  53. /* Bitfields in CNTL0 */
  54. #define BCM2835_AUX_SPI_CNTL0_SPEED 0xFFF00000
  55. #define BCM2835_AUX_SPI_CNTL0_SPEED_MAX 0xFFF
  56. #define BCM2835_AUX_SPI_CNTL0_SPEED_SHIFT 20
  57. #define BCM2835_AUX_SPI_CNTL0_CS 0x000E0000
  58. #define BCM2835_AUX_SPI_CNTL0_POSTINPUT 0x00010000
  59. #define BCM2835_AUX_SPI_CNTL0_VAR_CS 0x00008000
  60. #define BCM2835_AUX_SPI_CNTL0_VAR_WIDTH 0x00004000
  61. #define BCM2835_AUX_SPI_CNTL0_DOUTHOLD 0x00003000
  62. #define BCM2835_AUX_SPI_CNTL0_ENABLE 0x00000800
  63. #define BCM2835_AUX_SPI_CNTL0_CPHA_IN 0x00000400
  64. #define BCM2835_AUX_SPI_CNTL0_CLEARFIFO 0x00000200
  65. #define BCM2835_AUX_SPI_CNTL0_CPHA_OUT 0x00000100
  66. #define BCM2835_AUX_SPI_CNTL0_CPOL 0x00000080
  67. #define BCM2835_AUX_SPI_CNTL0_MSBF_OUT 0x00000040
  68. #define BCM2835_AUX_SPI_CNTL0_SHIFTLEN 0x0000003F
  69. /* Bitfields in CNTL1 */
  70. #define BCM2835_AUX_SPI_CNTL1_CSHIGH 0x00000700
  71. #define BCM2835_AUX_SPI_CNTL1_IDLE 0x00000080
  72. #define BCM2835_AUX_SPI_CNTL1_TXEMPTY 0x00000040
  73. #define BCM2835_AUX_SPI_CNTL1_MSBF_IN 0x00000002
  74. #define BCM2835_AUX_SPI_CNTL1_KEEP_IN 0x00000001
  75. /* Bitfields in STAT */
  76. #define BCM2835_AUX_SPI_STAT_TX_LVL 0xFF000000
  77. #define BCM2835_AUX_SPI_STAT_RX_LVL 0x00FF0000
  78. #define BCM2835_AUX_SPI_STAT_TX_FULL 0x00000400
  79. #define BCM2835_AUX_SPI_STAT_TX_EMPTY 0x00000200
  80. #define BCM2835_AUX_SPI_STAT_RX_FULL 0x00000100
  81. #define BCM2835_AUX_SPI_STAT_RX_EMPTY 0x00000080
  82. #define BCM2835_AUX_SPI_STAT_BUSY 0x00000040
  83. #define BCM2835_AUX_SPI_STAT_BITCOUNT 0x0000003F
  84. /* timeout values */
  85. #define BCM2835_AUX_SPI_POLLING_LIMIT_US 30
  86. #define BCM2835_AUX_SPI_POLLING_JIFFIES 2
  87. #define BCM2835_AUX_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
  88. | SPI_NO_CS)
  89. struct bcm2835aux_spi {
  90. void __iomem *regs;
  91. struct clk *clk;
  92. int irq;
  93. u32 cntl[2];
  94. const u8 *tx_buf;
  95. u8 *rx_buf;
  96. int tx_len;
  97. int rx_len;
  98. int pending;
  99. };
  100. static inline u32 bcm2835aux_rd(struct bcm2835aux_spi *bs, unsigned reg)
  101. {
  102. return readl(bs->regs + reg);
  103. }
  104. static inline void bcm2835aux_wr(struct bcm2835aux_spi *bs, unsigned reg,
  105. u32 val)
  106. {
  107. writel(val, bs->regs + reg);
  108. }
  109. static inline void bcm2835aux_rd_fifo(struct bcm2835aux_spi *bs)
  110. {
  111. u32 data;
  112. int count = min(bs->rx_len, 3);
  113. data = bcm2835aux_rd(bs, BCM2835_AUX_SPI_IO);
  114. if (bs->rx_buf) {
  115. switch (count) {
  116. case 4:
  117. *bs->rx_buf++ = (data >> 24) & 0xff;
  118. /* fallthrough */
  119. case 3:
  120. *bs->rx_buf++ = (data >> 16) & 0xff;
  121. /* fallthrough */
  122. case 2:
  123. *bs->rx_buf++ = (data >> 8) & 0xff;
  124. /* fallthrough */
  125. case 1:
  126. *bs->rx_buf++ = (data >> 0) & 0xff;
  127. /* fallthrough - no default */
  128. }
  129. }
  130. bs->rx_len -= count;
  131. bs->pending -= count;
  132. }
  133. static inline void bcm2835aux_wr_fifo(struct bcm2835aux_spi *bs)
  134. {
  135. u32 data;
  136. u8 byte;
  137. int count;
  138. int i;
  139. /* gather up to 3 bytes to write to the FIFO */
  140. count = min(bs->tx_len, 3);
  141. data = 0;
  142. for (i = 0; i < count; i++) {
  143. byte = bs->tx_buf ? *bs->tx_buf++ : 0;
  144. data |= byte << (8 * (2 - i));
  145. }
  146. /* and set the variable bit-length */
  147. data |= (count * 8) << 24;
  148. /* and decrement length */
  149. bs->tx_len -= count;
  150. bs->pending += count;
  151. /* write to the correct TX-register */
  152. if (bs->tx_len)
  153. bcm2835aux_wr(bs, BCM2835_AUX_SPI_TXHOLD, data);
  154. else
  155. bcm2835aux_wr(bs, BCM2835_AUX_SPI_IO, data);
  156. }
  157. static void bcm2835aux_spi_reset_hw(struct bcm2835aux_spi *bs)
  158. {
  159. /* disable spi clearing fifo and interrupts */
  160. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, 0);
  161. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0,
  162. BCM2835_AUX_SPI_CNTL0_CLEARFIFO);
  163. }
  164. static irqreturn_t bcm2835aux_spi_interrupt(int irq, void *dev_id)
  165. {
  166. struct spi_master *master = dev_id;
  167. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  168. irqreturn_t ret = IRQ_NONE;
  169. /* check if we have data to read */
  170. while (bs->rx_len &&
  171. (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
  172. BCM2835_AUX_SPI_STAT_RX_EMPTY))) {
  173. bcm2835aux_rd_fifo(bs);
  174. ret = IRQ_HANDLED;
  175. }
  176. /* check if we have data to write */
  177. while (bs->tx_len &&
  178. (bs->pending < 12) &&
  179. (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
  180. BCM2835_AUX_SPI_STAT_TX_FULL))) {
  181. bcm2835aux_wr_fifo(bs);
  182. ret = IRQ_HANDLED;
  183. }
  184. /* and check if we have reached "done" */
  185. while (bs->rx_len &&
  186. (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
  187. BCM2835_AUX_SPI_STAT_BUSY))) {
  188. bcm2835aux_rd_fifo(bs);
  189. ret = IRQ_HANDLED;
  190. }
  191. /* and if rx_len is 0 then wake up completion and disable spi */
  192. if (!bs->rx_len) {
  193. bcm2835aux_spi_reset_hw(bs);
  194. complete(&master->xfer_completion);
  195. }
  196. /* and return */
  197. return ret;
  198. }
  199. static int __bcm2835aux_spi_transfer_one_irq(struct spi_master *master,
  200. struct spi_device *spi,
  201. struct spi_transfer *tfr)
  202. {
  203. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  204. /* enable interrupts */
  205. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1] |
  206. BCM2835_AUX_SPI_CNTL1_TXEMPTY |
  207. BCM2835_AUX_SPI_CNTL1_IDLE);
  208. /* and wait for finish... */
  209. return 1;
  210. }
  211. static int bcm2835aux_spi_transfer_one_irq(struct spi_master *master,
  212. struct spi_device *spi,
  213. struct spi_transfer *tfr)
  214. {
  215. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  216. /* fill in registers and fifos before enabling interrupts */
  217. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
  218. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
  219. /* fill in tx fifo with data before enabling interrupts */
  220. while ((bs->tx_len) &&
  221. (bs->pending < 12) &&
  222. (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
  223. BCM2835_AUX_SPI_STAT_TX_FULL))) {
  224. bcm2835aux_wr_fifo(bs);
  225. }
  226. /* now run the interrupt mode */
  227. return __bcm2835aux_spi_transfer_one_irq(master, spi, tfr);
  228. }
  229. static int bcm2835aux_spi_transfer_one_poll(struct spi_master *master,
  230. struct spi_device *spi,
  231. struct spi_transfer *tfr)
  232. {
  233. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  234. unsigned long timeout;
  235. u32 stat;
  236. /* configure spi */
  237. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
  238. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
  239. /* set the timeout */
  240. timeout = jiffies + BCM2835_AUX_SPI_POLLING_JIFFIES;
  241. /* loop until finished the transfer */
  242. while (bs->rx_len) {
  243. /* read status */
  244. stat = bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT);
  245. /* fill in tx fifo with remaining data */
  246. if ((bs->tx_len) && (!(stat & BCM2835_AUX_SPI_STAT_TX_FULL))) {
  247. bcm2835aux_wr_fifo(bs);
  248. continue;
  249. }
  250. /* read data from fifo for both cases */
  251. if (!(stat & BCM2835_AUX_SPI_STAT_RX_EMPTY)) {
  252. bcm2835aux_rd_fifo(bs);
  253. continue;
  254. }
  255. if (!(stat & BCM2835_AUX_SPI_STAT_BUSY)) {
  256. bcm2835aux_rd_fifo(bs);
  257. continue;
  258. }
  259. /* there is still data pending to read check the timeout */
  260. if (bs->rx_len && time_after(jiffies, timeout)) {
  261. dev_dbg_ratelimited(&spi->dev,
  262. "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n",
  263. jiffies - timeout,
  264. bs->tx_len, bs->rx_len);
  265. /* forward to interrupt handler */
  266. return __bcm2835aux_spi_transfer_one_irq(master,
  267. spi, tfr);
  268. }
  269. }
  270. /* Transfer complete - reset SPI HW */
  271. bcm2835aux_spi_reset_hw(bs);
  272. /* and return without waiting for completion */
  273. return 0;
  274. }
  275. static int bcm2835aux_spi_transfer_one(struct spi_master *master,
  276. struct spi_device *spi,
  277. struct spi_transfer *tfr)
  278. {
  279. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  280. unsigned long spi_hz, clk_hz, speed;
  281. unsigned long spi_used_hz;
  282. unsigned long long xfer_time_us;
  283. /* calculate the registers to handle
  284. *
  285. * note that we use the variable data mode, which
  286. * is not optimal for longer transfers as we waste registers
  287. * resulting (potentially) in more interrupts when transferring
  288. * more than 12 bytes
  289. */
  290. bs->cntl[0] = BCM2835_AUX_SPI_CNTL0_ENABLE |
  291. BCM2835_AUX_SPI_CNTL0_VAR_WIDTH |
  292. BCM2835_AUX_SPI_CNTL0_MSBF_OUT;
  293. bs->cntl[1] = BCM2835_AUX_SPI_CNTL1_MSBF_IN;
  294. /* set clock */
  295. spi_hz = tfr->speed_hz;
  296. clk_hz = clk_get_rate(bs->clk);
  297. if (spi_hz >= clk_hz / 2) {
  298. speed = 0;
  299. } else if (spi_hz) {
  300. speed = DIV_ROUND_UP(clk_hz, 2 * spi_hz) - 1;
  301. if (speed > BCM2835_AUX_SPI_CNTL0_SPEED_MAX)
  302. speed = BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
  303. } else { /* the slowest we can go */
  304. speed = BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
  305. }
  306. bs->cntl[0] |= speed << BCM2835_AUX_SPI_CNTL0_SPEED_SHIFT;
  307. spi_used_hz = clk_hz / (2 * (speed + 1));
  308. /* handle all the modes */
  309. if (spi->mode & SPI_CPOL)
  310. bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_CPOL;
  311. if (spi->mode & SPI_CPHA)
  312. bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_CPHA_OUT |
  313. BCM2835_AUX_SPI_CNTL0_CPHA_IN;
  314. /* set transmit buffers and length */
  315. bs->tx_buf = tfr->tx_buf;
  316. bs->rx_buf = tfr->rx_buf;
  317. bs->tx_len = tfr->len;
  318. bs->rx_len = tfr->len;
  319. bs->pending = 0;
  320. /* calculate the estimated time in us the transfer runs
  321. * note that there are are 2 idle clocks after each
  322. * chunk getting transferred - in our case the chunk size
  323. * is 3 bytes, so we approximate this by 9 bits/byte
  324. */
  325. xfer_time_us = tfr->len * 9 * 1000000;
  326. do_div(xfer_time_us, spi_used_hz);
  327. /* run in polling mode for short transfers */
  328. if (xfer_time_us < BCM2835_AUX_SPI_POLLING_LIMIT_US)
  329. return bcm2835aux_spi_transfer_one_poll(master, spi, tfr);
  330. /* run in interrupt mode for all others */
  331. return bcm2835aux_spi_transfer_one_irq(master, spi, tfr);
  332. }
  333. static void bcm2835aux_spi_handle_err(struct spi_master *master,
  334. struct spi_message *msg)
  335. {
  336. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  337. bcm2835aux_spi_reset_hw(bs);
  338. }
  339. static int bcm2835aux_spi_probe(struct platform_device *pdev)
  340. {
  341. struct spi_master *master;
  342. struct bcm2835aux_spi *bs;
  343. struct resource *res;
  344. unsigned long clk_hz;
  345. int err;
  346. master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  347. if (!master) {
  348. dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  349. return -ENOMEM;
  350. }
  351. platform_set_drvdata(pdev, master);
  352. master->mode_bits = BCM2835_AUX_SPI_MODE_BITS;
  353. master->bits_per_word_mask = SPI_BPW_MASK(8);
  354. master->num_chipselect = -1;
  355. master->transfer_one = bcm2835aux_spi_transfer_one;
  356. master->handle_err = bcm2835aux_spi_handle_err;
  357. master->dev.of_node = pdev->dev.of_node;
  358. bs = spi_master_get_devdata(master);
  359. /* the main area */
  360. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  361. bs->regs = devm_ioremap_resource(&pdev->dev, res);
  362. if (IS_ERR(bs->regs)) {
  363. err = PTR_ERR(bs->regs);
  364. goto out_master_put;
  365. }
  366. bs->clk = devm_clk_get(&pdev->dev, NULL);
  367. if ((!bs->clk) || (IS_ERR(bs->clk))) {
  368. err = PTR_ERR(bs->clk);
  369. dev_err(&pdev->dev, "could not get clk: %d\n", err);
  370. goto out_master_put;
  371. }
  372. bs->irq = platform_get_irq(pdev, 0);
  373. if (bs->irq <= 0) {
  374. dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq);
  375. err = bs->irq ? bs->irq : -ENODEV;
  376. goto out_master_put;
  377. }
  378. /* this also enables the HW block */
  379. err = clk_prepare_enable(bs->clk);
  380. if (err) {
  381. dev_err(&pdev->dev, "could not prepare clock: %d\n", err);
  382. goto out_master_put;
  383. }
  384. /* just checking if the clock returns a sane value */
  385. clk_hz = clk_get_rate(bs->clk);
  386. if (!clk_hz) {
  387. dev_err(&pdev->dev, "clock returns 0 Hz\n");
  388. err = -ENODEV;
  389. goto out_clk_disable;
  390. }
  391. /* reset SPI-HW block */
  392. bcm2835aux_spi_reset_hw(bs);
  393. err = devm_request_irq(&pdev->dev, bs->irq,
  394. bcm2835aux_spi_interrupt,
  395. IRQF_SHARED,
  396. dev_name(&pdev->dev), master);
  397. if (err) {
  398. dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  399. goto out_clk_disable;
  400. }
  401. err = devm_spi_register_master(&pdev->dev, master);
  402. if (err) {
  403. dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  404. goto out_clk_disable;
  405. }
  406. return 0;
  407. out_clk_disable:
  408. clk_disable_unprepare(bs->clk);
  409. out_master_put:
  410. spi_master_put(master);
  411. return err;
  412. }
  413. static int bcm2835aux_spi_remove(struct platform_device *pdev)
  414. {
  415. struct spi_master *master = platform_get_drvdata(pdev);
  416. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  417. bcm2835aux_spi_reset_hw(bs);
  418. /* disable the HW block by releasing the clock */
  419. clk_disable_unprepare(bs->clk);
  420. return 0;
  421. }
  422. static const struct of_device_id bcm2835aux_spi_match[] = {
  423. { .compatible = "brcm,bcm2835-aux-spi", },
  424. {}
  425. };
  426. MODULE_DEVICE_TABLE(of, bcm2835aux_spi_match);
  427. static struct platform_driver bcm2835aux_spi_driver = {
  428. .driver = {
  429. .name = "spi-bcm2835aux",
  430. .of_match_table = bcm2835aux_spi_match,
  431. },
  432. .probe = bcm2835aux_spi_probe,
  433. .remove = bcm2835aux_spi_remove,
  434. };
  435. module_platform_driver(bcm2835aux_spi_driver);
  436. MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835 aux");
  437. MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
  438. MODULE_LICENSE("GPL v2");