spi-bcm2835aux.c 15 KB

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