spi-bcm2835aux.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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_IN_RISING 0x00000400
  64. #define BCM2835_AUX_SPI_CNTL0_CLEARFIFO 0x00000200
  65. #define BCM2835_AUX_SPI_CNTL0_OUT_RISING 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_TXEMPTY 0x00000080
  72. #define BCM2835_AUX_SPI_CNTL1_IDLE 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. struct bcm2835aux_spi {
  88. void __iomem *regs;
  89. struct clk *clk;
  90. int irq;
  91. u32 cntl[2];
  92. const u8 *tx_buf;
  93. u8 *rx_buf;
  94. int tx_len;
  95. int rx_len;
  96. int pending;
  97. };
  98. static inline u32 bcm2835aux_rd(struct bcm2835aux_spi *bs, unsigned reg)
  99. {
  100. return readl(bs->regs + reg);
  101. }
  102. static inline void bcm2835aux_wr(struct bcm2835aux_spi *bs, unsigned reg,
  103. u32 val)
  104. {
  105. writel(val, bs->regs + reg);
  106. }
  107. static inline void bcm2835aux_rd_fifo(struct bcm2835aux_spi *bs)
  108. {
  109. u32 data;
  110. int count = min(bs->rx_len, 3);
  111. data = bcm2835aux_rd(bs, BCM2835_AUX_SPI_IO);
  112. if (bs->rx_buf) {
  113. switch (count) {
  114. case 4:
  115. *bs->rx_buf++ = (data >> 24) & 0xff;
  116. /* fallthrough */
  117. case 3:
  118. *bs->rx_buf++ = (data >> 16) & 0xff;
  119. /* fallthrough */
  120. case 2:
  121. *bs->rx_buf++ = (data >> 8) & 0xff;
  122. /* fallthrough */
  123. case 1:
  124. *bs->rx_buf++ = (data >> 0) & 0xff;
  125. /* fallthrough - no default */
  126. }
  127. }
  128. bs->rx_len -= count;
  129. bs->pending -= count;
  130. }
  131. static inline void bcm2835aux_wr_fifo(struct bcm2835aux_spi *bs)
  132. {
  133. u32 data;
  134. u8 byte;
  135. int count;
  136. int i;
  137. /* gather up to 3 bytes to write to the FIFO */
  138. count = min(bs->tx_len, 3);
  139. data = 0;
  140. for (i = 0; i < count; i++) {
  141. byte = bs->tx_buf ? *bs->tx_buf++ : 0;
  142. data |= byte << (8 * (2 - i));
  143. }
  144. /* and set the variable bit-length */
  145. data |= (count * 8) << 24;
  146. /* and decrement length */
  147. bs->tx_len -= count;
  148. bs->pending += count;
  149. /* write to the correct TX-register */
  150. if (bs->tx_len)
  151. bcm2835aux_wr(bs, BCM2835_AUX_SPI_TXHOLD, data);
  152. else
  153. bcm2835aux_wr(bs, BCM2835_AUX_SPI_IO, data);
  154. }
  155. static void bcm2835aux_spi_reset_hw(struct bcm2835aux_spi *bs)
  156. {
  157. /* disable spi clearing fifo and interrupts */
  158. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, 0);
  159. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0,
  160. BCM2835_AUX_SPI_CNTL0_CLEARFIFO);
  161. }
  162. static irqreturn_t bcm2835aux_spi_interrupt(int irq, void *dev_id)
  163. {
  164. struct spi_master *master = dev_id;
  165. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  166. irqreturn_t ret = IRQ_NONE;
  167. /* check if we have data to read */
  168. while (bs->rx_len &&
  169. (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
  170. BCM2835_AUX_SPI_STAT_RX_EMPTY))) {
  171. bcm2835aux_rd_fifo(bs);
  172. ret = IRQ_HANDLED;
  173. }
  174. /* check if we have data to write */
  175. while (bs->tx_len &&
  176. (bs->pending < 12) &&
  177. (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
  178. BCM2835_AUX_SPI_STAT_TX_FULL))) {
  179. bcm2835aux_wr_fifo(bs);
  180. ret = IRQ_HANDLED;
  181. }
  182. /* and check if we have reached "done" */
  183. while (bs->rx_len &&
  184. (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
  185. BCM2835_AUX_SPI_STAT_BUSY))) {
  186. bcm2835aux_rd_fifo(bs);
  187. ret = IRQ_HANDLED;
  188. }
  189. if (!bs->tx_len) {
  190. /* disable tx fifo empty interrupt */
  191. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1] |
  192. BCM2835_AUX_SPI_CNTL1_IDLE);
  193. }
  194. /* and if rx_len is 0 then disable interrupts and wake up completion */
  195. if (!bs->rx_len) {
  196. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
  197. complete(&master->xfer_completion);
  198. }
  199. /* and return */
  200. return ret;
  201. }
  202. static int __bcm2835aux_spi_transfer_one_irq(struct spi_master *master,
  203. struct spi_device *spi,
  204. struct spi_transfer *tfr)
  205. {
  206. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  207. /* enable interrupts */
  208. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1] |
  209. BCM2835_AUX_SPI_CNTL1_TXEMPTY |
  210. BCM2835_AUX_SPI_CNTL1_IDLE);
  211. /* and wait for finish... */
  212. return 1;
  213. }
  214. static int bcm2835aux_spi_transfer_one_irq(struct spi_master *master,
  215. struct spi_device *spi,
  216. struct spi_transfer *tfr)
  217. {
  218. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  219. /* fill in registers and fifos before enabling interrupts */
  220. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
  221. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
  222. /* fill in tx fifo with data before enabling interrupts */
  223. while ((bs->tx_len) &&
  224. (bs->pending < 12) &&
  225. (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
  226. BCM2835_AUX_SPI_STAT_TX_FULL))) {
  227. bcm2835aux_wr_fifo(bs);
  228. }
  229. /* now run the interrupt mode */
  230. return __bcm2835aux_spi_transfer_one_irq(master, spi, tfr);
  231. }
  232. static int bcm2835aux_spi_transfer_one_poll(struct spi_master *master,
  233. struct spi_device *spi,
  234. struct spi_transfer *tfr)
  235. {
  236. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  237. unsigned long timeout;
  238. u32 stat;
  239. /* configure spi */
  240. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
  241. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
  242. /* set the timeout */
  243. timeout = jiffies + BCM2835_AUX_SPI_POLLING_JIFFIES;
  244. /* loop until finished the transfer */
  245. while (bs->rx_len) {
  246. /* read status */
  247. stat = bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT);
  248. /* fill in tx fifo with remaining data */
  249. if ((bs->tx_len) && (!(stat & BCM2835_AUX_SPI_STAT_TX_FULL))) {
  250. bcm2835aux_wr_fifo(bs);
  251. continue;
  252. }
  253. /* read data from fifo for both cases */
  254. if (!(stat & BCM2835_AUX_SPI_STAT_RX_EMPTY)) {
  255. bcm2835aux_rd_fifo(bs);
  256. continue;
  257. }
  258. if (!(stat & BCM2835_AUX_SPI_STAT_BUSY)) {
  259. bcm2835aux_rd_fifo(bs);
  260. continue;
  261. }
  262. /* there is still data pending to read check the timeout */
  263. if (bs->rx_len && time_after(jiffies, timeout)) {
  264. dev_dbg_ratelimited(&spi->dev,
  265. "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n",
  266. jiffies - timeout,
  267. bs->tx_len, bs->rx_len);
  268. /* forward to interrupt handler */
  269. return __bcm2835aux_spi_transfer_one_irq(master,
  270. spi, tfr);
  271. }
  272. }
  273. /* and return without waiting for completion */
  274. return 0;
  275. }
  276. static int bcm2835aux_spi_transfer_one(struct spi_master *master,
  277. struct spi_device *spi,
  278. struct spi_transfer *tfr)
  279. {
  280. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  281. unsigned long spi_hz, clk_hz, speed;
  282. unsigned long spi_used_hz;
  283. unsigned long long xfer_time_us;
  284. /* calculate the registers to handle
  285. *
  286. * note that we use the variable data mode, which
  287. * is not optimal for longer transfers as we waste registers
  288. * resulting (potentially) in more interrupts when transferring
  289. * more than 12 bytes
  290. */
  291. /* set clock */
  292. spi_hz = tfr->speed_hz;
  293. clk_hz = clk_get_rate(bs->clk);
  294. if (spi_hz >= clk_hz / 2) {
  295. speed = 0;
  296. } else if (spi_hz) {
  297. speed = DIV_ROUND_UP(clk_hz, 2 * spi_hz) - 1;
  298. if (speed > BCM2835_AUX_SPI_CNTL0_SPEED_MAX)
  299. speed = BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
  300. } else { /* the slowest we can go */
  301. speed = BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
  302. }
  303. /* mask out old speed from previous spi_transfer */
  304. bs->cntl[0] &= ~(BCM2835_AUX_SPI_CNTL0_SPEED);
  305. /* set the new speed */
  306. bs->cntl[0] |= speed << BCM2835_AUX_SPI_CNTL0_SPEED_SHIFT;
  307. spi_used_hz = clk_hz / (2 * (speed + 1));
  308. /* set transmit buffers and length */
  309. bs->tx_buf = tfr->tx_buf;
  310. bs->rx_buf = tfr->rx_buf;
  311. bs->tx_len = tfr->len;
  312. bs->rx_len = tfr->len;
  313. bs->pending = 0;
  314. /* calculate the estimated time in us the transfer runs
  315. * note that there are are 2 idle clocks after each
  316. * chunk getting transferred - in our case the chunk size
  317. * is 3 bytes, so we approximate this by 9 bits/byte
  318. */
  319. xfer_time_us = tfr->len * 9 * 1000000;
  320. do_div(xfer_time_us, spi_used_hz);
  321. /* run in polling mode for short transfers */
  322. if (xfer_time_us < BCM2835_AUX_SPI_POLLING_LIMIT_US)
  323. return bcm2835aux_spi_transfer_one_poll(master, spi, tfr);
  324. /* run in interrupt mode for all others */
  325. return bcm2835aux_spi_transfer_one_irq(master, spi, tfr);
  326. }
  327. static int bcm2835aux_spi_prepare_message(struct spi_master *master,
  328. struct spi_message *msg)
  329. {
  330. struct spi_device *spi = msg->spi;
  331. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  332. bs->cntl[0] = BCM2835_AUX_SPI_CNTL0_ENABLE |
  333. BCM2835_AUX_SPI_CNTL0_VAR_WIDTH |
  334. BCM2835_AUX_SPI_CNTL0_MSBF_OUT;
  335. bs->cntl[1] = BCM2835_AUX_SPI_CNTL1_MSBF_IN;
  336. /* handle all the modes */
  337. if (spi->mode & SPI_CPOL) {
  338. bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_CPOL;
  339. bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_OUT_RISING;
  340. } else {
  341. bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_IN_RISING;
  342. }
  343. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
  344. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
  345. return 0;
  346. }
  347. static int bcm2835aux_spi_unprepare_message(struct spi_master *master,
  348. struct spi_message *msg)
  349. {
  350. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  351. bcm2835aux_spi_reset_hw(bs);
  352. return 0;
  353. }
  354. static void bcm2835aux_spi_handle_err(struct spi_master *master,
  355. struct spi_message *msg)
  356. {
  357. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  358. bcm2835aux_spi_reset_hw(bs);
  359. }
  360. static int bcm2835aux_spi_probe(struct platform_device *pdev)
  361. {
  362. struct spi_master *master;
  363. struct bcm2835aux_spi *bs;
  364. struct resource *res;
  365. unsigned long clk_hz;
  366. int err;
  367. master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  368. if (!master) {
  369. dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  370. return -ENOMEM;
  371. }
  372. platform_set_drvdata(pdev, master);
  373. master->mode_bits = (SPI_CPOL | SPI_CS_HIGH | SPI_NO_CS);
  374. master->bits_per_word_mask = SPI_BPW_MASK(8);
  375. master->num_chipselect = -1;
  376. master->transfer_one = bcm2835aux_spi_transfer_one;
  377. master->handle_err = bcm2835aux_spi_handle_err;
  378. master->prepare_message = bcm2835aux_spi_prepare_message;
  379. master->unprepare_message = bcm2835aux_spi_unprepare_message;
  380. master->dev.of_node = pdev->dev.of_node;
  381. bs = spi_master_get_devdata(master);
  382. /* the main area */
  383. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  384. bs->regs = devm_ioremap_resource(&pdev->dev, res);
  385. if (IS_ERR(bs->regs)) {
  386. err = PTR_ERR(bs->regs);
  387. goto out_master_put;
  388. }
  389. bs->clk = devm_clk_get(&pdev->dev, NULL);
  390. if ((!bs->clk) || (IS_ERR(bs->clk))) {
  391. err = PTR_ERR(bs->clk);
  392. dev_err(&pdev->dev, "could not get clk: %d\n", err);
  393. goto out_master_put;
  394. }
  395. bs->irq = platform_get_irq(pdev, 0);
  396. if (bs->irq <= 0) {
  397. dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq);
  398. err = bs->irq ? bs->irq : -ENODEV;
  399. goto out_master_put;
  400. }
  401. /* this also enables the HW block */
  402. err = clk_prepare_enable(bs->clk);
  403. if (err) {
  404. dev_err(&pdev->dev, "could not prepare clock: %d\n", err);
  405. goto out_master_put;
  406. }
  407. /* just checking if the clock returns a sane value */
  408. clk_hz = clk_get_rate(bs->clk);
  409. if (!clk_hz) {
  410. dev_err(&pdev->dev, "clock returns 0 Hz\n");
  411. err = -ENODEV;
  412. goto out_clk_disable;
  413. }
  414. /* reset SPI-HW block */
  415. bcm2835aux_spi_reset_hw(bs);
  416. err = devm_request_irq(&pdev->dev, bs->irq,
  417. bcm2835aux_spi_interrupt,
  418. IRQF_SHARED,
  419. dev_name(&pdev->dev), master);
  420. if (err) {
  421. dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  422. goto out_clk_disable;
  423. }
  424. err = devm_spi_register_master(&pdev->dev, master);
  425. if (err) {
  426. dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  427. goto out_clk_disable;
  428. }
  429. return 0;
  430. out_clk_disable:
  431. clk_disable_unprepare(bs->clk);
  432. out_master_put:
  433. spi_master_put(master);
  434. return err;
  435. }
  436. static int bcm2835aux_spi_remove(struct platform_device *pdev)
  437. {
  438. struct spi_master *master = platform_get_drvdata(pdev);
  439. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  440. bcm2835aux_spi_reset_hw(bs);
  441. /* disable the HW block by releasing the clock */
  442. clk_disable_unprepare(bs->clk);
  443. return 0;
  444. }
  445. static const struct of_device_id bcm2835aux_spi_match[] = {
  446. { .compatible = "brcm,bcm2835-aux-spi", },
  447. {}
  448. };
  449. MODULE_DEVICE_TABLE(of, bcm2835aux_spi_match);
  450. static struct platform_driver bcm2835aux_spi_driver = {
  451. .driver = {
  452. .name = "spi-bcm2835aux",
  453. .of_match_table = bcm2835aux_spi_match,
  454. },
  455. .probe = bcm2835aux_spi_probe,
  456. .remove = bcm2835aux_spi_remove,
  457. };
  458. module_platform_driver(bcm2835aux_spi_driver);
  459. MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835 aux");
  460. MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
  461. MODULE_LICENSE("GPL v2");