spi-bcm2835aux.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. /* 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. /* set clock */
  291. spi_hz = tfr->speed_hz;
  292. clk_hz = clk_get_rate(bs->clk);
  293. if (spi_hz >= clk_hz / 2) {
  294. speed = 0;
  295. } else if (spi_hz) {
  296. speed = DIV_ROUND_UP(clk_hz, 2 * spi_hz) - 1;
  297. if (speed > BCM2835_AUX_SPI_CNTL0_SPEED_MAX)
  298. speed = BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
  299. } else { /* the slowest we can go */
  300. speed = BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
  301. }
  302. /* mask out old speed from previous spi_transfer */
  303. bs->cntl[0] &= ~(BCM2835_AUX_SPI_CNTL0_SPEED);
  304. /* set the new speed */
  305. bs->cntl[0] |= speed << BCM2835_AUX_SPI_CNTL0_SPEED_SHIFT;
  306. spi_used_hz = clk_hz / (2 * (speed + 1));
  307. /* set transmit buffers and length */
  308. bs->tx_buf = tfr->tx_buf;
  309. bs->rx_buf = tfr->rx_buf;
  310. bs->tx_len = tfr->len;
  311. bs->rx_len = tfr->len;
  312. bs->pending = 0;
  313. /* Calculate the estimated time in us the transfer runs. Note that
  314. * there are are 2 idle clocks cycles after each chunk getting
  315. * transferred - in our case the chunk size is 3 bytes, so we
  316. * approximate this by 9 cycles/byte. This is used to find the number
  317. * of Hz per byte per polling limit. E.g., we can transfer 1 byte in
  318. * 30 µs per 300,000 Hz of bus clock.
  319. */
  320. #define HZ_PER_BYTE ((9 * 1000000) / BCM2835_AUX_SPI_POLLING_LIMIT_US)
  321. /* run in polling mode for short transfers */
  322. if (tfr->len < spi_used_hz / HZ_PER_BYTE)
  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. #undef HZ_PER_BYTE
  327. }
  328. static int bcm2835aux_spi_prepare_message(struct spi_master *master,
  329. struct spi_message *msg)
  330. {
  331. struct spi_device *spi = msg->spi;
  332. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  333. bs->cntl[0] = BCM2835_AUX_SPI_CNTL0_ENABLE |
  334. BCM2835_AUX_SPI_CNTL0_VAR_WIDTH |
  335. BCM2835_AUX_SPI_CNTL0_MSBF_OUT;
  336. bs->cntl[1] = BCM2835_AUX_SPI_CNTL1_MSBF_IN;
  337. /* handle all the modes */
  338. if (spi->mode & SPI_CPOL) {
  339. bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_CPOL;
  340. bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_OUT_RISING;
  341. } else {
  342. bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_IN_RISING;
  343. }
  344. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
  345. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
  346. return 0;
  347. }
  348. static int bcm2835aux_spi_unprepare_message(struct spi_master *master,
  349. struct spi_message *msg)
  350. {
  351. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  352. bcm2835aux_spi_reset_hw(bs);
  353. return 0;
  354. }
  355. static void bcm2835aux_spi_handle_err(struct spi_master *master,
  356. struct spi_message *msg)
  357. {
  358. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  359. bcm2835aux_spi_reset_hw(bs);
  360. }
  361. static int bcm2835aux_spi_probe(struct platform_device *pdev)
  362. {
  363. struct spi_master *master;
  364. struct bcm2835aux_spi *bs;
  365. struct resource *res;
  366. unsigned long clk_hz;
  367. int err;
  368. master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  369. if (!master) {
  370. dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  371. return -ENOMEM;
  372. }
  373. platform_set_drvdata(pdev, master);
  374. master->mode_bits = (SPI_CPOL | SPI_CS_HIGH | SPI_NO_CS);
  375. master->bits_per_word_mask = SPI_BPW_MASK(8);
  376. master->num_chipselect = -1;
  377. master->transfer_one = bcm2835aux_spi_transfer_one;
  378. master->handle_err = bcm2835aux_spi_handle_err;
  379. master->prepare_message = bcm2835aux_spi_prepare_message;
  380. master->unprepare_message = bcm2835aux_spi_unprepare_message;
  381. master->dev.of_node = pdev->dev.of_node;
  382. bs = spi_master_get_devdata(master);
  383. /* the main area */
  384. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  385. bs->regs = devm_ioremap_resource(&pdev->dev, res);
  386. if (IS_ERR(bs->regs)) {
  387. err = PTR_ERR(bs->regs);
  388. goto out_master_put;
  389. }
  390. bs->clk = devm_clk_get(&pdev->dev, NULL);
  391. if ((!bs->clk) || (IS_ERR(bs->clk))) {
  392. err = PTR_ERR(bs->clk);
  393. dev_err(&pdev->dev, "could not get clk: %d\n", err);
  394. goto out_master_put;
  395. }
  396. bs->irq = platform_get_irq(pdev, 0);
  397. if (bs->irq <= 0) {
  398. dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq);
  399. err = bs->irq ? bs->irq : -ENODEV;
  400. goto out_master_put;
  401. }
  402. /* this also enables the HW block */
  403. err = clk_prepare_enable(bs->clk);
  404. if (err) {
  405. dev_err(&pdev->dev, "could not prepare clock: %d\n", err);
  406. goto out_master_put;
  407. }
  408. /* just checking if the clock returns a sane value */
  409. clk_hz = clk_get_rate(bs->clk);
  410. if (!clk_hz) {
  411. dev_err(&pdev->dev, "clock returns 0 Hz\n");
  412. err = -ENODEV;
  413. goto out_clk_disable;
  414. }
  415. /* reset SPI-HW block */
  416. bcm2835aux_spi_reset_hw(bs);
  417. err = devm_request_irq(&pdev->dev, bs->irq,
  418. bcm2835aux_spi_interrupt,
  419. IRQF_SHARED,
  420. dev_name(&pdev->dev), master);
  421. if (err) {
  422. dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  423. goto out_clk_disable;
  424. }
  425. err = devm_spi_register_master(&pdev->dev, master);
  426. if (err) {
  427. dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  428. goto out_clk_disable;
  429. }
  430. return 0;
  431. out_clk_disable:
  432. clk_disable_unprepare(bs->clk);
  433. out_master_put:
  434. spi_master_put(master);
  435. return err;
  436. }
  437. static int bcm2835aux_spi_remove(struct platform_device *pdev)
  438. {
  439. struct spi_master *master = platform_get_drvdata(pdev);
  440. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  441. bcm2835aux_spi_reset_hw(bs);
  442. /* disable the HW block by releasing the clock */
  443. clk_disable_unprepare(bs->clk);
  444. return 0;
  445. }
  446. static const struct of_device_id bcm2835aux_spi_match[] = {
  447. { .compatible = "brcm,bcm2835-aux-spi", },
  448. {}
  449. };
  450. MODULE_DEVICE_TABLE(of, bcm2835aux_spi_match);
  451. static struct platform_driver bcm2835aux_spi_driver = {
  452. .driver = {
  453. .name = "spi-bcm2835aux",
  454. .of_match_table = bcm2835aux_spi_match,
  455. },
  456. .probe = bcm2835aux_spi_probe,
  457. .remove = bcm2835aux_spi_remove,
  458. };
  459. module_platform_driver(bcm2835aux_spi_driver);
  460. MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835 aux");
  461. MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
  462. MODULE_LICENSE("GPL v2");