spi-bcm2835aux.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 void bcm2835aux_spi_transfer_helper(struct bcm2835aux_spi *bs)
  163. {
  164. u32 stat = bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT);
  165. /* check if we have data to read */
  166. for (; bs->rx_len && (stat & BCM2835_AUX_SPI_STAT_RX_LVL);
  167. stat = bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT))
  168. bcm2835aux_rd_fifo(bs);
  169. /* check if we have data to write */
  170. while (bs->tx_len &&
  171. (bs->pending < 12) &&
  172. (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
  173. BCM2835_AUX_SPI_STAT_TX_FULL))) {
  174. bcm2835aux_wr_fifo(bs);
  175. }
  176. }
  177. static irqreturn_t bcm2835aux_spi_interrupt(int irq, void *dev_id)
  178. {
  179. struct spi_master *master = dev_id;
  180. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  181. /* IRQ may be shared, so return if our interrupts are disabled */
  182. if (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_CNTL1) &
  183. (BCM2835_AUX_SPI_CNTL1_TXEMPTY | BCM2835_AUX_SPI_CNTL1_IDLE)))
  184. return IRQ_NONE;
  185. /* do common fifo handling */
  186. bcm2835aux_spi_transfer_helper(bs);
  187. if (!bs->tx_len) {
  188. /* disable tx fifo empty interrupt */
  189. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1] |
  190. BCM2835_AUX_SPI_CNTL1_IDLE);
  191. }
  192. /* and if rx_len is 0 then disable interrupts and wake up completion */
  193. if (!bs->rx_len) {
  194. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
  195. complete(&master->xfer_completion);
  196. }
  197. return IRQ_HANDLED;
  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. /* configure spi */
  236. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
  237. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
  238. /* set the timeout */
  239. timeout = jiffies + BCM2835_AUX_SPI_POLLING_JIFFIES;
  240. /* loop until finished the transfer */
  241. while (bs->rx_len) {
  242. /* do common fifo handling */
  243. bcm2835aux_spi_transfer_helper(bs);
  244. /* there is still data pending to read check the timeout */
  245. if (bs->rx_len && time_after(jiffies, timeout)) {
  246. dev_dbg_ratelimited(&spi->dev,
  247. "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n",
  248. jiffies - timeout,
  249. bs->tx_len, bs->rx_len);
  250. /* forward to interrupt handler */
  251. return __bcm2835aux_spi_transfer_one_irq(master,
  252. spi, tfr);
  253. }
  254. }
  255. /* and return without waiting for completion */
  256. return 0;
  257. }
  258. static int bcm2835aux_spi_transfer_one(struct spi_master *master,
  259. struct spi_device *spi,
  260. struct spi_transfer *tfr)
  261. {
  262. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  263. unsigned long spi_hz, clk_hz, speed;
  264. unsigned long spi_used_hz;
  265. unsigned long long xfer_time_us;
  266. /* calculate the registers to handle
  267. *
  268. * note that we use the variable data mode, which
  269. * is not optimal for longer transfers as we waste registers
  270. * resulting (potentially) in more interrupts when transferring
  271. * more than 12 bytes
  272. */
  273. /* set clock */
  274. spi_hz = tfr->speed_hz;
  275. clk_hz = clk_get_rate(bs->clk);
  276. if (spi_hz >= clk_hz / 2) {
  277. speed = 0;
  278. } else if (spi_hz) {
  279. speed = DIV_ROUND_UP(clk_hz, 2 * spi_hz) - 1;
  280. if (speed > BCM2835_AUX_SPI_CNTL0_SPEED_MAX)
  281. speed = BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
  282. } else { /* the slowest we can go */
  283. speed = BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
  284. }
  285. /* mask out old speed from previous spi_transfer */
  286. bs->cntl[0] &= ~(BCM2835_AUX_SPI_CNTL0_SPEED);
  287. /* set the new speed */
  288. bs->cntl[0] |= speed << BCM2835_AUX_SPI_CNTL0_SPEED_SHIFT;
  289. spi_used_hz = clk_hz / (2 * (speed + 1));
  290. /* set transmit buffers and length */
  291. bs->tx_buf = tfr->tx_buf;
  292. bs->rx_buf = tfr->rx_buf;
  293. bs->tx_len = tfr->len;
  294. bs->rx_len = tfr->len;
  295. bs->pending = 0;
  296. /* calculate the estimated time in us the transfer runs
  297. * note that there are are 2 idle clocks after each
  298. * chunk getting transferred - in our case the chunk size
  299. * is 3 bytes, so we approximate this by 9 bits/byte
  300. */
  301. xfer_time_us = tfr->len * 9 * 1000000;
  302. do_div(xfer_time_us, spi_used_hz);
  303. /* run in polling mode for short transfers */
  304. if (xfer_time_us < BCM2835_AUX_SPI_POLLING_LIMIT_US)
  305. return bcm2835aux_spi_transfer_one_poll(master, spi, tfr);
  306. /* run in interrupt mode for all others */
  307. return bcm2835aux_spi_transfer_one_irq(master, spi, tfr);
  308. }
  309. static int bcm2835aux_spi_prepare_message(struct spi_master *master,
  310. struct spi_message *msg)
  311. {
  312. struct spi_device *spi = msg->spi;
  313. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  314. bs->cntl[0] = BCM2835_AUX_SPI_CNTL0_ENABLE |
  315. BCM2835_AUX_SPI_CNTL0_VAR_WIDTH |
  316. BCM2835_AUX_SPI_CNTL0_MSBF_OUT;
  317. bs->cntl[1] = BCM2835_AUX_SPI_CNTL1_MSBF_IN;
  318. /* handle all the modes */
  319. if (spi->mode & SPI_CPOL) {
  320. bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_CPOL;
  321. bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_OUT_RISING;
  322. } else {
  323. bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_IN_RISING;
  324. }
  325. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
  326. bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
  327. return 0;
  328. }
  329. static int bcm2835aux_spi_unprepare_message(struct spi_master *master,
  330. struct spi_message *msg)
  331. {
  332. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  333. bcm2835aux_spi_reset_hw(bs);
  334. return 0;
  335. }
  336. static void bcm2835aux_spi_handle_err(struct spi_master *master,
  337. struct spi_message *msg)
  338. {
  339. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  340. bcm2835aux_spi_reset_hw(bs);
  341. }
  342. static int bcm2835aux_spi_probe(struct platform_device *pdev)
  343. {
  344. struct spi_master *master;
  345. struct bcm2835aux_spi *bs;
  346. struct resource *res;
  347. unsigned long clk_hz;
  348. int err;
  349. master = devm_spi_alloc_master(&pdev->dev, sizeof(*bs));
  350. if (!master) {
  351. dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  352. return -ENOMEM;
  353. }
  354. platform_set_drvdata(pdev, master);
  355. master->mode_bits = (SPI_CPOL | SPI_CS_HIGH | SPI_NO_CS);
  356. master->bits_per_word_mask = SPI_BPW_MASK(8);
  357. /* even though the driver never officially supported native CS
  358. * allow a single native CS for legacy DT support purposes when
  359. * no cs-gpio is configured.
  360. * Known limitations for native cs are:
  361. * * multiple chip-selects: cs0-cs2 are all simultaniously asserted
  362. * whenever there is a transfer - this even includes SPI_NO_CS
  363. * * SPI_CS_HIGH: is ignores - cs are always asserted low
  364. * * cs_change: cs is deasserted after each spi_transfer
  365. * * cs_delay_usec: cs is always deasserted one SCK cycle after
  366. * a spi_transfer
  367. */
  368. master->num_chipselect = 1;
  369. master->transfer_one = bcm2835aux_spi_transfer_one;
  370. master->handle_err = bcm2835aux_spi_handle_err;
  371. master->prepare_message = bcm2835aux_spi_prepare_message;
  372. master->unprepare_message = bcm2835aux_spi_unprepare_message;
  373. master->dev.of_node = pdev->dev.of_node;
  374. bs = spi_master_get_devdata(master);
  375. /* the main area */
  376. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  377. bs->regs = devm_ioremap_resource(&pdev->dev, res);
  378. if (IS_ERR(bs->regs))
  379. return PTR_ERR(bs->regs);
  380. bs->clk = devm_clk_get(&pdev->dev, NULL);
  381. if ((!bs->clk) || (IS_ERR(bs->clk))) {
  382. err = PTR_ERR(bs->clk);
  383. dev_err(&pdev->dev, "could not get clk: %d\n", err);
  384. return err;
  385. }
  386. bs->irq = platform_get_irq(pdev, 0);
  387. if (bs->irq <= 0) {
  388. dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq);
  389. return bs->irq ? bs->irq : -ENODEV;
  390. }
  391. /* this also enables the HW block */
  392. err = clk_prepare_enable(bs->clk);
  393. if (err) {
  394. dev_err(&pdev->dev, "could not prepare clock: %d\n", err);
  395. return err;
  396. }
  397. /* just checking if the clock returns a sane value */
  398. clk_hz = clk_get_rate(bs->clk);
  399. if (!clk_hz) {
  400. dev_err(&pdev->dev, "clock returns 0 Hz\n");
  401. err = -ENODEV;
  402. goto out_clk_disable;
  403. }
  404. /* reset SPI-HW block */
  405. bcm2835aux_spi_reset_hw(bs);
  406. err = devm_request_irq(&pdev->dev, bs->irq,
  407. bcm2835aux_spi_interrupt,
  408. IRQF_SHARED,
  409. dev_name(&pdev->dev), master);
  410. if (err) {
  411. dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  412. goto out_clk_disable;
  413. }
  414. err = spi_register_master(master);
  415. if (err) {
  416. dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  417. goto out_clk_disable;
  418. }
  419. return 0;
  420. out_clk_disable:
  421. clk_disable_unprepare(bs->clk);
  422. return err;
  423. }
  424. static int bcm2835aux_spi_remove(struct platform_device *pdev)
  425. {
  426. struct spi_master *master = platform_get_drvdata(pdev);
  427. struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
  428. spi_unregister_master(master);
  429. bcm2835aux_spi_reset_hw(bs);
  430. /* disable the HW block by releasing the clock */
  431. clk_disable_unprepare(bs->clk);
  432. return 0;
  433. }
  434. static const struct of_device_id bcm2835aux_spi_match[] = {
  435. { .compatible = "brcm,bcm2835-aux-spi", },
  436. {}
  437. };
  438. MODULE_DEVICE_TABLE(of, bcm2835aux_spi_match);
  439. static struct platform_driver bcm2835aux_spi_driver = {
  440. .driver = {
  441. .name = "spi-bcm2835aux",
  442. .of_match_table = bcm2835aux_spi_match,
  443. },
  444. .probe = bcm2835aux_spi_probe,
  445. .remove = bcm2835aux_spi_remove,
  446. };
  447. module_platform_driver(bcm2835aux_spi_driver);
  448. MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835 aux");
  449. MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
  450. MODULE_LICENSE("GPL v2");