spi-bcm2835.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Driver for Broadcom BCM2835 SPI Controllers
  3. *
  4. * Copyright (C) 2012 Chris Boot
  5. * Copyright (C) 2013 Stephen Warren
  6. *
  7. * This driver is inspired by:
  8. * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
  9. * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
  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. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. #include <linux/clk.h>
  26. #include <linux/completion.h>
  27. #include <linux/delay.h>
  28. #include <linux/err.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/io.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/of.h>
  34. #include <linux/of_irq.h>
  35. #include <linux/of_device.h>
  36. #include <linux/spi/spi.h>
  37. /* SPI register offsets */
  38. #define BCM2835_SPI_CS 0x00
  39. #define BCM2835_SPI_FIFO 0x04
  40. #define BCM2835_SPI_CLK 0x08
  41. #define BCM2835_SPI_DLEN 0x0c
  42. #define BCM2835_SPI_LTOH 0x10
  43. #define BCM2835_SPI_DC 0x14
  44. /* Bitfields in CS */
  45. #define BCM2835_SPI_CS_LEN_LONG 0x02000000
  46. #define BCM2835_SPI_CS_DMA_LEN 0x01000000
  47. #define BCM2835_SPI_CS_CSPOL2 0x00800000
  48. #define BCM2835_SPI_CS_CSPOL1 0x00400000
  49. #define BCM2835_SPI_CS_CSPOL0 0x00200000
  50. #define BCM2835_SPI_CS_RXF 0x00100000
  51. #define BCM2835_SPI_CS_RXR 0x00080000
  52. #define BCM2835_SPI_CS_TXD 0x00040000
  53. #define BCM2835_SPI_CS_RXD 0x00020000
  54. #define BCM2835_SPI_CS_DONE 0x00010000
  55. #define BCM2835_SPI_CS_LEN 0x00002000
  56. #define BCM2835_SPI_CS_REN 0x00001000
  57. #define BCM2835_SPI_CS_ADCS 0x00000800
  58. #define BCM2835_SPI_CS_INTR 0x00000400
  59. #define BCM2835_SPI_CS_INTD 0x00000200
  60. #define BCM2835_SPI_CS_DMAEN 0x00000100
  61. #define BCM2835_SPI_CS_TA 0x00000080
  62. #define BCM2835_SPI_CS_CSPOL 0x00000040
  63. #define BCM2835_SPI_CS_CLEAR_RX 0x00000020
  64. #define BCM2835_SPI_CS_CLEAR_TX 0x00000010
  65. #define BCM2835_SPI_CS_CPOL 0x00000008
  66. #define BCM2835_SPI_CS_CPHA 0x00000004
  67. #define BCM2835_SPI_CS_CS_10 0x00000002
  68. #define BCM2835_SPI_CS_CS_01 0x00000001
  69. #define BCM2835_SPI_TIMEOUT_MS 30000
  70. #define BCM2835_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS)
  71. #define DRV_NAME "spi-bcm2835"
  72. struct bcm2835_spi {
  73. void __iomem *regs;
  74. struct clk *clk;
  75. int irq;
  76. struct completion done;
  77. const u8 *tx_buf;
  78. u8 *rx_buf;
  79. int len;
  80. };
  81. static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned reg)
  82. {
  83. return readl(bs->regs + reg);
  84. }
  85. static inline void bcm2835_wr(struct bcm2835_spi *bs, unsigned reg, u32 val)
  86. {
  87. writel(val, bs->regs + reg);
  88. }
  89. static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs, int len)
  90. {
  91. u8 byte;
  92. while (len--) {
  93. byte = bcm2835_rd(bs, BCM2835_SPI_FIFO);
  94. if (bs->rx_buf)
  95. *bs->rx_buf++ = byte;
  96. }
  97. }
  98. static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs, int len)
  99. {
  100. u8 byte;
  101. if (len > bs->len)
  102. len = bs->len;
  103. while (len--) {
  104. byte = bs->tx_buf ? *bs->tx_buf++ : 0;
  105. bcm2835_wr(bs, BCM2835_SPI_FIFO, byte);
  106. bs->len--;
  107. }
  108. }
  109. static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id)
  110. {
  111. struct spi_master *master = dev_id;
  112. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  113. u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
  114. /*
  115. * RXR - RX needs Reading. This means 12 (or more) bytes have been
  116. * transmitted and hence 12 (or more) bytes have been received.
  117. *
  118. * The FIFO is 16-bytes deep. We check for this interrupt to keep the
  119. * FIFO full; we have a 4-byte-time buffer for IRQ latency. We check
  120. * this before DONE (TX empty) just in case we delayed processing this
  121. * interrupt for some reason.
  122. *
  123. * We only check for this case if we have more bytes to TX; at the end
  124. * of the transfer, we ignore this pipelining optimization, and let
  125. * bcm2835_spi_finish_transfer() drain the RX FIFO.
  126. */
  127. if (bs->len && (cs & BCM2835_SPI_CS_RXR)) {
  128. /* Read 12 bytes of data */
  129. bcm2835_rd_fifo(bs, 12);
  130. /* Write up to 12 bytes */
  131. bcm2835_wr_fifo(bs, 12);
  132. /*
  133. * We must have written something to the TX FIFO due to the
  134. * bs->len check above, so cannot be DONE. Hence, return
  135. * early. Note that DONE could also be set if we serviced an
  136. * RXR interrupt really late.
  137. */
  138. return IRQ_HANDLED;
  139. }
  140. /*
  141. * DONE - TX empty. This occurs when we first enable the transfer
  142. * since we do not pre-fill the TX FIFO. At any other time, given that
  143. * we refill the TX FIFO above based on RXR, and hence ignore DONE if
  144. * RXR is set, DONE really does mean end-of-transfer.
  145. */
  146. if (cs & BCM2835_SPI_CS_DONE) {
  147. if (bs->len) { /* First interrupt in a transfer */
  148. bcm2835_wr_fifo(bs, 16);
  149. } else { /* Transfer complete */
  150. /* Disable SPI interrupts */
  151. cs &= ~(BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD);
  152. bcm2835_wr(bs, BCM2835_SPI_CS, cs);
  153. /*
  154. * Wake up bcm2835_spi_transfer_one(), which will call
  155. * bcm2835_spi_finish_transfer(), to drain the RX FIFO.
  156. */
  157. complete(&bs->done);
  158. }
  159. return IRQ_HANDLED;
  160. }
  161. return IRQ_NONE;
  162. }
  163. static int bcm2835_spi_start_transfer(struct spi_device *spi,
  164. struct spi_transfer *tfr)
  165. {
  166. struct bcm2835_spi *bs = spi_master_get_devdata(spi->master);
  167. unsigned long spi_hz, clk_hz, cdiv;
  168. u32 cs = BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA;
  169. spi_hz = tfr->speed_hz;
  170. clk_hz = clk_get_rate(bs->clk);
  171. if (spi_hz >= clk_hz / 2) {
  172. cdiv = 2; /* clk_hz/2 is the fastest we can go */
  173. } else if (spi_hz) {
  174. /* CDIV must be a power of two */
  175. cdiv = roundup_pow_of_two(DIV_ROUND_UP(clk_hz, spi_hz));
  176. if (cdiv >= 65536)
  177. cdiv = 0; /* 0 is the slowest we can go */
  178. } else
  179. cdiv = 0; /* 0 is the slowest we can go */
  180. if (spi->mode & SPI_CPOL)
  181. cs |= BCM2835_SPI_CS_CPOL;
  182. if (spi->mode & SPI_CPHA)
  183. cs |= BCM2835_SPI_CS_CPHA;
  184. if (!(spi->mode & SPI_NO_CS)) {
  185. if (spi->mode & SPI_CS_HIGH) {
  186. cs |= BCM2835_SPI_CS_CSPOL;
  187. cs |= BCM2835_SPI_CS_CSPOL0 << spi->chip_select;
  188. }
  189. cs |= spi->chip_select;
  190. }
  191. INIT_COMPLETION(bs->done);
  192. bs->tx_buf = tfr->tx_buf;
  193. bs->rx_buf = tfr->rx_buf;
  194. bs->len = tfr->len;
  195. bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv);
  196. /*
  197. * Enable the HW block. This will immediately trigger a DONE (TX
  198. * empty) interrupt, upon which we will fill the TX FIFO with the
  199. * first TX bytes. Pre-filling the TX FIFO here to avoid the
  200. * interrupt doesn't work:-(
  201. */
  202. bcm2835_wr(bs, BCM2835_SPI_CS, cs);
  203. return 0;
  204. }
  205. static int bcm2835_spi_finish_transfer(struct spi_device *spi,
  206. struct spi_transfer *tfr, bool cs_change)
  207. {
  208. struct bcm2835_spi *bs = spi_master_get_devdata(spi->master);
  209. u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
  210. /* Drain RX FIFO */
  211. while (cs & BCM2835_SPI_CS_RXD) {
  212. bcm2835_rd_fifo(bs, 1);
  213. cs = bcm2835_rd(bs, BCM2835_SPI_CS);
  214. }
  215. if (tfr->delay_usecs)
  216. udelay(tfr->delay_usecs);
  217. if (cs_change)
  218. /* Clear TA flag */
  219. bcm2835_wr(bs, BCM2835_SPI_CS, cs & ~BCM2835_SPI_CS_TA);
  220. return 0;
  221. }
  222. static int bcm2835_spi_transfer_one(struct spi_master *master,
  223. struct spi_message *mesg)
  224. {
  225. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  226. struct spi_transfer *tfr;
  227. struct spi_device *spi = mesg->spi;
  228. int err = 0;
  229. unsigned int timeout;
  230. bool cs_change;
  231. list_for_each_entry(tfr, &mesg->transfers, transfer_list) {
  232. err = bcm2835_spi_start_transfer(spi, tfr);
  233. if (err)
  234. goto out;
  235. timeout = wait_for_completion_timeout(&bs->done,
  236. msecs_to_jiffies(BCM2835_SPI_TIMEOUT_MS));
  237. if (!timeout) {
  238. err = -ETIMEDOUT;
  239. goto out;
  240. }
  241. cs_change = tfr->cs_change ||
  242. list_is_last(&tfr->transfer_list, &mesg->transfers);
  243. err = bcm2835_spi_finish_transfer(spi, tfr, cs_change);
  244. if (err)
  245. goto out;
  246. mesg->actual_length += (tfr->len - bs->len);
  247. }
  248. out:
  249. /* Clear FIFOs, and disable the HW block */
  250. bcm2835_wr(bs, BCM2835_SPI_CS,
  251. BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
  252. mesg->status = err;
  253. spi_finalize_current_message(master);
  254. return 0;
  255. }
  256. static int bcm2835_spi_probe(struct platform_device *pdev)
  257. {
  258. struct spi_master *master;
  259. struct bcm2835_spi *bs;
  260. struct resource *res;
  261. int err;
  262. master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  263. if (!master) {
  264. dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  265. return -ENOMEM;
  266. }
  267. platform_set_drvdata(pdev, master);
  268. master->mode_bits = BCM2835_SPI_MODE_BITS;
  269. master->bits_per_word_mask = BIT(8 - 1);
  270. master->bus_num = -1;
  271. master->num_chipselect = 3;
  272. master->transfer_one_message = bcm2835_spi_transfer_one;
  273. master->dev.of_node = pdev->dev.of_node;
  274. bs = spi_master_get_devdata(master);
  275. init_completion(&bs->done);
  276. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  277. if (!res) {
  278. dev_err(&pdev->dev, "could not get memory resource\n");
  279. err = -ENODEV;
  280. goto out_master_put;
  281. }
  282. bs->regs = devm_request_and_ioremap(&pdev->dev, res);
  283. if (!bs->regs) {
  284. dev_err(&pdev->dev, "could not request/map memory region\n");
  285. err = -ENODEV;
  286. goto out_master_put;
  287. }
  288. bs->clk = devm_clk_get(&pdev->dev, NULL);
  289. if (IS_ERR(bs->clk)) {
  290. err = PTR_ERR(bs->clk);
  291. dev_err(&pdev->dev, "could not get clk: %d\n", err);
  292. goto out_master_put;
  293. }
  294. bs->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  295. if (bs->irq <= 0) {
  296. dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq);
  297. err = bs->irq ? bs->irq : -ENODEV;
  298. goto out_master_put;
  299. }
  300. clk_prepare_enable(bs->clk);
  301. err = request_irq(bs->irq, bcm2835_spi_interrupt, 0,
  302. dev_name(&pdev->dev), master);
  303. if (err) {
  304. dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  305. goto out_clk_disable;
  306. }
  307. /* initialise the hardware */
  308. bcm2835_wr(bs, BCM2835_SPI_CS,
  309. BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
  310. err = spi_register_master(master);
  311. if (err) {
  312. dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  313. goto out_free_irq;
  314. }
  315. return 0;
  316. out_free_irq:
  317. free_irq(bs->irq, master);
  318. out_clk_disable:
  319. clk_disable_unprepare(bs->clk);
  320. out_master_put:
  321. spi_master_put(master);
  322. return err;
  323. }
  324. static int bcm2835_spi_remove(struct platform_device *pdev)
  325. {
  326. struct spi_master *master = platform_get_drvdata(pdev);
  327. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  328. free_irq(bs->irq, master);
  329. spi_unregister_master(master);
  330. /* Clear FIFOs, and disable the HW block */
  331. bcm2835_wr(bs, BCM2835_SPI_CS,
  332. BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
  333. clk_disable_unprepare(bs->clk);
  334. spi_master_put(master);
  335. return 0;
  336. }
  337. static const struct of_device_id bcm2835_spi_match[] = {
  338. { .compatible = "brcm,bcm2835-spi", },
  339. {}
  340. };
  341. MODULE_DEVICE_TABLE(of, bcm2835_spi_match);
  342. static struct platform_driver bcm2835_spi_driver = {
  343. .driver = {
  344. .name = DRV_NAME,
  345. .owner = THIS_MODULE,
  346. .of_match_table = bcm2835_spi_match,
  347. },
  348. .probe = bcm2835_spi_probe,
  349. .remove = bcm2835_spi_remove,
  350. };
  351. module_platform_driver(bcm2835_spi_driver);
  352. MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835");
  353. MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  354. MODULE_LICENSE("GPL v2");