spi-bcm2835.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Driver for Broadcom BCM2835 SPI Controllers
  3. *
  4. * Copyright (C) 2012 Chris Boot
  5. * Copyright (C) 2013 Stephen Warren
  6. * Copyright (C) 2015 Martin Sperl
  7. *
  8. * This driver is inspired by:
  9. * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
  10. * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/completion.h>
  24. #include <linux/delay.h>
  25. #include <linux/err.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/of.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/of_device.h>
  34. #include <linux/spi/spi.h>
  35. /* SPI register offsets */
  36. #define BCM2835_SPI_CS 0x00
  37. #define BCM2835_SPI_FIFO 0x04
  38. #define BCM2835_SPI_CLK 0x08
  39. #define BCM2835_SPI_DLEN 0x0c
  40. #define BCM2835_SPI_LTOH 0x10
  41. #define BCM2835_SPI_DC 0x14
  42. /* Bitfields in CS */
  43. #define BCM2835_SPI_CS_LEN_LONG 0x02000000
  44. #define BCM2835_SPI_CS_DMA_LEN 0x01000000
  45. #define BCM2835_SPI_CS_CSPOL2 0x00800000
  46. #define BCM2835_SPI_CS_CSPOL1 0x00400000
  47. #define BCM2835_SPI_CS_CSPOL0 0x00200000
  48. #define BCM2835_SPI_CS_RXF 0x00100000
  49. #define BCM2835_SPI_CS_RXR 0x00080000
  50. #define BCM2835_SPI_CS_TXD 0x00040000
  51. #define BCM2835_SPI_CS_RXD 0x00020000
  52. #define BCM2835_SPI_CS_DONE 0x00010000
  53. #define BCM2835_SPI_CS_LEN 0x00002000
  54. #define BCM2835_SPI_CS_REN 0x00001000
  55. #define BCM2835_SPI_CS_ADCS 0x00000800
  56. #define BCM2835_SPI_CS_INTR 0x00000400
  57. #define BCM2835_SPI_CS_INTD 0x00000200
  58. #define BCM2835_SPI_CS_DMAEN 0x00000100
  59. #define BCM2835_SPI_CS_TA 0x00000080
  60. #define BCM2835_SPI_CS_CSPOL 0x00000040
  61. #define BCM2835_SPI_CS_CLEAR_RX 0x00000020
  62. #define BCM2835_SPI_CS_CLEAR_TX 0x00000010
  63. #define BCM2835_SPI_CS_CPOL 0x00000008
  64. #define BCM2835_SPI_CS_CPHA 0x00000004
  65. #define BCM2835_SPI_CS_CS_10 0x00000002
  66. #define BCM2835_SPI_CS_CS_01 0x00000001
  67. #define BCM2835_SPI_POLLING_LIMIT_US 30
  68. #define BCM2835_SPI_TIMEOUT_MS 30000
  69. #define BCM2835_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
  70. | SPI_NO_CS | SPI_3WIRE)
  71. #define DRV_NAME "spi-bcm2835"
  72. struct bcm2835_spi {
  73. void __iomem *regs;
  74. struct clk *clk;
  75. int irq;
  76. const u8 *tx_buf;
  77. u8 *rx_buf;
  78. int tx_len;
  79. int rx_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)
  90. {
  91. u8 byte;
  92. while ((bs->rx_len) &&
  93. (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD)) {
  94. byte = bcm2835_rd(bs, BCM2835_SPI_FIFO);
  95. if (bs->rx_buf)
  96. *bs->rx_buf++ = byte;
  97. bs->rx_len--;
  98. }
  99. }
  100. static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs)
  101. {
  102. u8 byte;
  103. while ((bs->tx_len) &&
  104. (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) {
  105. byte = bs->tx_buf ? *bs->tx_buf++ : 0;
  106. bcm2835_wr(bs, BCM2835_SPI_FIFO, byte);
  107. bs->tx_len--;
  108. }
  109. }
  110. static void bcm2835_spi_reset_hw(struct spi_master *master)
  111. {
  112. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  113. u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
  114. /* Disable SPI interrupts and transfer */
  115. cs &= ~(BCM2835_SPI_CS_INTR |
  116. BCM2835_SPI_CS_INTD |
  117. BCM2835_SPI_CS_TA);
  118. /* and reset RX/TX FIFOS */
  119. cs |= BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX;
  120. /* and reset the SPI_HW */
  121. bcm2835_wr(bs, BCM2835_SPI_CS, cs);
  122. }
  123. static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id)
  124. {
  125. struct spi_master *master = dev_id;
  126. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  127. /* Read as many bytes as possible from FIFO */
  128. bcm2835_rd_fifo(bs);
  129. /* Write as many bytes as possible to FIFO */
  130. bcm2835_wr_fifo(bs);
  131. /* based on flags decide if we can finish the transfer */
  132. if (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_DONE) {
  133. /* Transfer complete - reset SPI HW */
  134. bcm2835_spi_reset_hw(master);
  135. /* wake up the framework */
  136. complete(&master->xfer_completion);
  137. }
  138. return IRQ_HANDLED;
  139. }
  140. static int bcm2835_spi_transfer_one_poll(struct spi_master *master,
  141. struct spi_device *spi,
  142. struct spi_transfer *tfr,
  143. u32 cs,
  144. unsigned long xfer_time_us)
  145. {
  146. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  147. /* set timeout to 1 second of maximum polling */
  148. unsigned long timeout = jiffies + HZ;
  149. /* enable HW block without interrupts */
  150. bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
  151. /* loop until finished the transfer */
  152. while (bs->rx_len) {
  153. /* read from fifo as much as possible */
  154. bcm2835_rd_fifo(bs);
  155. /* fill in tx fifo as much as possible */
  156. bcm2835_wr_fifo(bs);
  157. /* if we still expect some data after the read,
  158. * check for a possible timeout
  159. */
  160. if (bs->rx_len && time_after(jiffies, timeout)) {
  161. /* Transfer complete - reset SPI HW */
  162. bcm2835_spi_reset_hw(master);
  163. /* and return timeout */
  164. return -ETIMEDOUT;
  165. }
  166. }
  167. /* Transfer complete - reset SPI HW */
  168. bcm2835_spi_reset_hw(master);
  169. /* and return without waiting for completion */
  170. return 0;
  171. }
  172. static int bcm2835_spi_transfer_one_irq(struct spi_master *master,
  173. struct spi_device *spi,
  174. struct spi_transfer *tfr,
  175. u32 cs)
  176. {
  177. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  178. /* fill in fifo if we have gpio-cs
  179. * note that there have been rare events where the native-CS
  180. * flapped for <1us which may change the behaviour
  181. * with gpio-cs this does not happen, so it is implemented
  182. * only for this case
  183. */
  184. if (gpio_is_valid(spi->cs_gpio)) {
  185. /* enable HW block, but without interrupts enabled
  186. * this would triggern an immediate interrupt
  187. */
  188. bcm2835_wr(bs, BCM2835_SPI_CS,
  189. cs | BCM2835_SPI_CS_TA);
  190. /* fill in tx fifo as much as possible */
  191. bcm2835_wr_fifo(bs);
  192. }
  193. /*
  194. * Enable the HW block. This will immediately trigger a DONE (TX
  195. * empty) interrupt, upon which we will fill the TX FIFO with the
  196. * first TX bytes. Pre-filling the TX FIFO here to avoid the
  197. * interrupt doesn't work:-(
  198. */
  199. cs |= BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA;
  200. bcm2835_wr(bs, BCM2835_SPI_CS, cs);
  201. /* signal that we need to wait for completion */
  202. return 1;
  203. }
  204. static int bcm2835_spi_transfer_one(struct spi_master *master,
  205. struct spi_device *spi,
  206. struct spi_transfer *tfr)
  207. {
  208. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  209. unsigned long spi_hz, clk_hz, cdiv;
  210. unsigned long spi_used_hz, xfer_time_us;
  211. u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
  212. /* set clock */
  213. spi_hz = tfr->speed_hz;
  214. clk_hz = clk_get_rate(bs->clk);
  215. if (spi_hz >= clk_hz / 2) {
  216. cdiv = 2; /* clk_hz/2 is the fastest we can go */
  217. } else if (spi_hz) {
  218. /* CDIV must be a multiple of two */
  219. cdiv = DIV_ROUND_UP(clk_hz, spi_hz);
  220. cdiv += (cdiv % 2);
  221. if (cdiv >= 65536)
  222. cdiv = 0; /* 0 is the slowest we can go */
  223. } else {
  224. cdiv = 0; /* 0 is the slowest we can go */
  225. }
  226. spi_used_hz = cdiv ? (clk_hz / cdiv) : (clk_hz / 65536);
  227. bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv);
  228. /* handle all the modes */
  229. if ((spi->mode & SPI_3WIRE) && (tfr->rx_buf))
  230. cs |= BCM2835_SPI_CS_REN;
  231. if (spi->mode & SPI_CPOL)
  232. cs |= BCM2835_SPI_CS_CPOL;
  233. if (spi->mode & SPI_CPHA)
  234. cs |= BCM2835_SPI_CS_CPHA;
  235. /* for gpio_cs set dummy CS so that no HW-CS get changed
  236. * we can not run this in bcm2835_spi_set_cs, as it does
  237. * not get called for cs_gpio cases, so we need to do it here
  238. */
  239. if (gpio_is_valid(spi->cs_gpio) || (spi->mode & SPI_NO_CS))
  240. cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
  241. /* set transmit buffers and length */
  242. bs->tx_buf = tfr->tx_buf;
  243. bs->rx_buf = tfr->rx_buf;
  244. bs->tx_len = tfr->len;
  245. bs->rx_len = tfr->len;
  246. /* calculate the estimated time in us the transfer runs */
  247. xfer_time_us = tfr->len
  248. * 9 /* clocks/byte - SPI-HW waits 1 clock after each byte */
  249. * 1000000 / spi_used_hz;
  250. /* for short requests run polling*/
  251. if (xfer_time_us <= BCM2835_SPI_POLLING_LIMIT_US)
  252. return bcm2835_spi_transfer_one_poll(master, spi, tfr,
  253. cs, xfer_time_us);
  254. return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs);
  255. }
  256. static void bcm2835_spi_handle_err(struct spi_master *master,
  257. struct spi_message *msg)
  258. {
  259. bcm2835_spi_reset_hw(master);
  260. }
  261. static void bcm2835_spi_set_cs(struct spi_device *spi, bool gpio_level)
  262. {
  263. /*
  264. * we can assume that we are "native" as per spi_set_cs
  265. * calling us ONLY when cs_gpio is not set
  266. * we can also assume that we are CS < 3 as per bcm2835_spi_setup
  267. * we would not get called because of error handling there.
  268. * the level passed is the electrical level not enabled/disabled
  269. * so it has to get translated back to enable/disable
  270. * see spi_set_cs in spi.c for the implementation
  271. */
  272. struct spi_master *master = spi->master;
  273. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  274. u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
  275. bool enable;
  276. /* calculate the enable flag from the passed gpio_level */
  277. enable = (spi->mode & SPI_CS_HIGH) ? gpio_level : !gpio_level;
  278. /* set flags for "reverse" polarity in the registers */
  279. if (spi->mode & SPI_CS_HIGH) {
  280. /* set the correct CS-bits */
  281. cs |= BCM2835_SPI_CS_CSPOL;
  282. cs |= BCM2835_SPI_CS_CSPOL0 << spi->chip_select;
  283. } else {
  284. /* clean the CS-bits */
  285. cs &= ~BCM2835_SPI_CS_CSPOL;
  286. cs &= ~(BCM2835_SPI_CS_CSPOL0 << spi->chip_select);
  287. }
  288. /* select the correct chip_select depending on disabled/enabled */
  289. if (enable) {
  290. /* set cs correctly */
  291. if (spi->mode & SPI_NO_CS) {
  292. /* use the "undefined" chip-select */
  293. cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
  294. } else {
  295. /* set the chip select */
  296. cs &= ~(BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01);
  297. cs |= spi->chip_select;
  298. }
  299. } else {
  300. /* disable CSPOL which puts HW-CS into deselected state */
  301. cs &= ~BCM2835_SPI_CS_CSPOL;
  302. /* use the "undefined" chip-select as precaution */
  303. cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
  304. }
  305. /* finally set the calculated flags in SPI_CS */
  306. bcm2835_wr(bs, BCM2835_SPI_CS, cs);
  307. }
  308. static int chip_match_name(struct gpio_chip *chip, void *data)
  309. {
  310. return !strcmp(chip->label, data);
  311. }
  312. static int bcm2835_spi_setup(struct spi_device *spi)
  313. {
  314. int err;
  315. struct gpio_chip *chip;
  316. /*
  317. * sanity checking the native-chipselects
  318. */
  319. if (spi->mode & SPI_NO_CS)
  320. return 0;
  321. if (gpio_is_valid(spi->cs_gpio))
  322. return 0;
  323. if (spi->chip_select > 1) {
  324. /* error in the case of native CS requested with CS > 1
  325. * officially there is a CS2, but it is not documented
  326. * which GPIO is connected with that...
  327. */
  328. dev_err(&spi->dev,
  329. "setup: only two native chip-selects are supported\n");
  330. return -EINVAL;
  331. }
  332. /* now translate native cs to GPIO */
  333. /* get the gpio chip for the base */
  334. chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
  335. if (!chip)
  336. return 0;
  337. /* and calculate the real CS */
  338. spi->cs_gpio = chip->base + 8 - spi->chip_select;
  339. /* and set up the "mode" and level */
  340. dev_info(&spi->dev, "setting up native-CS%i as GPIO %i\n",
  341. spi->chip_select, spi->cs_gpio);
  342. /* set up GPIO as output and pull to the correct level */
  343. err = gpio_direction_output(spi->cs_gpio,
  344. (spi->mode & SPI_CS_HIGH) ? 0 : 1);
  345. if (err) {
  346. dev_err(&spi->dev,
  347. "could not set CS%i gpio %i as output: %i",
  348. spi->chip_select, spi->cs_gpio, err);
  349. return err;
  350. }
  351. /* the implementation of pinctrl-bcm2835 currently does not
  352. * set the GPIO value when using gpio_direction_output
  353. * so we are setting it here explicitly
  354. */
  355. gpio_set_value(spi->cs_gpio, (spi->mode & SPI_CS_HIGH) ? 0 : 1);
  356. return 0;
  357. }
  358. static int bcm2835_spi_probe(struct platform_device *pdev)
  359. {
  360. struct spi_master *master;
  361. struct bcm2835_spi *bs;
  362. struct resource *res;
  363. int err;
  364. master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  365. if (!master) {
  366. dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  367. return -ENOMEM;
  368. }
  369. platform_set_drvdata(pdev, master);
  370. master->mode_bits = BCM2835_SPI_MODE_BITS;
  371. master->bits_per_word_mask = SPI_BPW_MASK(8);
  372. master->num_chipselect = 3;
  373. master->setup = bcm2835_spi_setup;
  374. master->set_cs = bcm2835_spi_set_cs;
  375. master->transfer_one = bcm2835_spi_transfer_one;
  376. master->handle_err = bcm2835_spi_handle_err;
  377. master->dev.of_node = pdev->dev.of_node;
  378. bs = spi_master_get_devdata(master);
  379. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  380. bs->regs = devm_ioremap_resource(&pdev->dev, res);
  381. if (IS_ERR(bs->regs)) {
  382. err = PTR_ERR(bs->regs);
  383. goto out_master_put;
  384. }
  385. bs->clk = devm_clk_get(&pdev->dev, NULL);
  386. if (IS_ERR(bs->clk)) {
  387. err = PTR_ERR(bs->clk);
  388. dev_err(&pdev->dev, "could not get clk: %d\n", err);
  389. goto out_master_put;
  390. }
  391. bs->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  392. if (bs->irq <= 0) {
  393. dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq);
  394. err = bs->irq ? bs->irq : -ENODEV;
  395. goto out_master_put;
  396. }
  397. clk_prepare_enable(bs->clk);
  398. err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0,
  399. dev_name(&pdev->dev), master);
  400. if (err) {
  401. dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  402. goto out_clk_disable;
  403. }
  404. /* initialise the hardware with the default polarities */
  405. bcm2835_wr(bs, BCM2835_SPI_CS,
  406. BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
  407. err = devm_spi_register_master(&pdev->dev, master);
  408. if (err) {
  409. dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  410. goto out_clk_disable;
  411. }
  412. return 0;
  413. out_clk_disable:
  414. clk_disable_unprepare(bs->clk);
  415. out_master_put:
  416. spi_master_put(master);
  417. return err;
  418. }
  419. static int bcm2835_spi_remove(struct platform_device *pdev)
  420. {
  421. struct spi_master *master = platform_get_drvdata(pdev);
  422. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  423. /* Clear FIFOs, and disable the HW block */
  424. bcm2835_wr(bs, BCM2835_SPI_CS,
  425. BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
  426. clk_disable_unprepare(bs->clk);
  427. return 0;
  428. }
  429. static const struct of_device_id bcm2835_spi_match[] = {
  430. { .compatible = "brcm,bcm2835-spi", },
  431. {}
  432. };
  433. MODULE_DEVICE_TABLE(of, bcm2835_spi_match);
  434. static struct platform_driver bcm2835_spi_driver = {
  435. .driver = {
  436. .name = DRV_NAME,
  437. .of_match_table = bcm2835_spi_match,
  438. },
  439. .probe = bcm2835_spi_probe,
  440. .remove = bcm2835_spi_remove,
  441. };
  442. module_platform_driver(bcm2835_spi_driver);
  443. MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835");
  444. MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  445. MODULE_LICENSE("GPL v2");