spi-bcm2835.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. unsigned long timeout = jiffies +
  148. max(4 * xfer_time_us * HZ / 1000000, 2uL);
  149. /* enable HW block without interrupts */
  150. bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
  151. /* set timeout to 4x the expected time, or 2 jiffies */
  152. /* loop until finished the transfer */
  153. while (bs->rx_len) {
  154. /* read from fifo as much as possible */
  155. bcm2835_rd_fifo(bs);
  156. /* fill in tx fifo as much as possible */
  157. bcm2835_wr_fifo(bs);
  158. /* if we still expect some data after the read,
  159. * check for a possible timeout
  160. */
  161. if (bs->rx_len && time_after(jiffies, timeout)) {
  162. /* Transfer complete - reset SPI HW */
  163. bcm2835_spi_reset_hw(master);
  164. /* and return timeout */
  165. return -ETIMEDOUT;
  166. }
  167. }
  168. /* Transfer complete - reset SPI HW */
  169. bcm2835_spi_reset_hw(master);
  170. /* and return without waiting for completion */
  171. return 0;
  172. }
  173. static int bcm2835_spi_transfer_one_irq(struct spi_master *master,
  174. struct spi_device *spi,
  175. struct spi_transfer *tfr,
  176. u32 cs)
  177. {
  178. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  179. /* fill in fifo if we have gpio-cs
  180. * note that there have been rare events where the native-CS
  181. * flapped for <1us which may change the behaviour
  182. * with gpio-cs this does not happen, so it is implemented
  183. * only for this case
  184. */
  185. if (gpio_is_valid(spi->cs_gpio)) {
  186. /* enable HW block, but without interrupts enabled
  187. * this would triggern an immediate interrupt
  188. */
  189. bcm2835_wr(bs, BCM2835_SPI_CS,
  190. cs | BCM2835_SPI_CS_TA);
  191. /* fill in tx fifo as much as possible */
  192. bcm2835_wr_fifo(bs);
  193. }
  194. /*
  195. * Enable the HW block. This will immediately trigger a DONE (TX
  196. * empty) interrupt, upon which we will fill the TX FIFO with the
  197. * first TX bytes. Pre-filling the TX FIFO here to avoid the
  198. * interrupt doesn't work:-(
  199. */
  200. cs |= BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA;
  201. bcm2835_wr(bs, BCM2835_SPI_CS, cs);
  202. /* signal that we need to wait for completion */
  203. return 1;
  204. }
  205. static int bcm2835_spi_transfer_one(struct spi_master *master,
  206. struct spi_device *spi,
  207. struct spi_transfer *tfr)
  208. {
  209. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  210. unsigned long spi_hz, clk_hz, cdiv;
  211. unsigned long spi_used_hz, xfer_time_us;
  212. u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
  213. /* set clock */
  214. spi_hz = tfr->speed_hz;
  215. clk_hz = clk_get_rate(bs->clk);
  216. if (spi_hz >= clk_hz / 2) {
  217. cdiv = 2; /* clk_hz/2 is the fastest we can go */
  218. } else if (spi_hz) {
  219. /* CDIV must be a multiple of two */
  220. cdiv = DIV_ROUND_UP(clk_hz, spi_hz);
  221. cdiv += (cdiv % 2);
  222. if (cdiv >= 65536)
  223. cdiv = 0; /* 0 is the slowest we can go */
  224. } else {
  225. cdiv = 0; /* 0 is the slowest we can go */
  226. }
  227. spi_used_hz = cdiv ? (clk_hz / cdiv) : (clk_hz / 65536);
  228. bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv);
  229. /* handle all the modes */
  230. if ((spi->mode & SPI_3WIRE) && (tfr->rx_buf))
  231. cs |= BCM2835_SPI_CS_REN;
  232. if (spi->mode & SPI_CPOL)
  233. cs |= BCM2835_SPI_CS_CPOL;
  234. if (spi->mode & SPI_CPHA)
  235. cs |= BCM2835_SPI_CS_CPHA;
  236. /* for gpio_cs set dummy CS so that no HW-CS get changed
  237. * we can not run this in bcm2835_spi_set_cs, as it does
  238. * not get called for cs_gpio cases, so we need to do it here
  239. */
  240. if (gpio_is_valid(spi->cs_gpio) || (spi->mode & SPI_NO_CS))
  241. cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
  242. /* set transmit buffers and length */
  243. bs->tx_buf = tfr->tx_buf;
  244. bs->rx_buf = tfr->rx_buf;
  245. bs->tx_len = tfr->len;
  246. bs->rx_len = tfr->len;
  247. /* calculate the estimated time in us the transfer runs */
  248. xfer_time_us = tfr->len
  249. * 9 /* clocks/byte - SPI-HW waits 1 clock after each byte */
  250. * 1000000 / spi_used_hz;
  251. /* for short requests run polling*/
  252. if (xfer_time_us <= BCM2835_SPI_POLLING_LIMIT_US)
  253. return bcm2835_spi_transfer_one_poll(master, spi, tfr,
  254. cs, xfer_time_us);
  255. return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs);
  256. }
  257. static void bcm2835_spi_handle_err(struct spi_master *master,
  258. struct spi_message *msg)
  259. {
  260. bcm2835_spi_reset_hw(master);
  261. }
  262. static void bcm2835_spi_set_cs(struct spi_device *spi, bool gpio_level)
  263. {
  264. /*
  265. * we can assume that we are "native" as per spi_set_cs
  266. * calling us ONLY when cs_gpio is not set
  267. * we can also assume that we are CS < 3 as per bcm2835_spi_setup
  268. * we would not get called because of error handling there.
  269. * the level passed is the electrical level not enabled/disabled
  270. * so it has to get translated back to enable/disable
  271. * see spi_set_cs in spi.c for the implementation
  272. */
  273. struct spi_master *master = spi->master;
  274. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  275. u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
  276. bool enable;
  277. /* calculate the enable flag from the passed gpio_level */
  278. enable = (spi->mode & SPI_CS_HIGH) ? gpio_level : !gpio_level;
  279. /* set flags for "reverse" polarity in the registers */
  280. if (spi->mode & SPI_CS_HIGH) {
  281. /* set the correct CS-bits */
  282. cs |= BCM2835_SPI_CS_CSPOL;
  283. cs |= BCM2835_SPI_CS_CSPOL0 << spi->chip_select;
  284. } else {
  285. /* clean the CS-bits */
  286. cs &= ~BCM2835_SPI_CS_CSPOL;
  287. cs &= ~(BCM2835_SPI_CS_CSPOL0 << spi->chip_select);
  288. }
  289. /* select the correct chip_select depending on disabled/enabled */
  290. if (enable) {
  291. /* set cs correctly */
  292. if (spi->mode & SPI_NO_CS) {
  293. /* use the "undefined" chip-select */
  294. cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
  295. } else {
  296. /* set the chip select */
  297. cs &= ~(BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01);
  298. cs |= spi->chip_select;
  299. }
  300. } else {
  301. /* disable CSPOL which puts HW-CS into deselected state */
  302. cs &= ~BCM2835_SPI_CS_CSPOL;
  303. /* use the "undefined" chip-select as precaution */
  304. cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
  305. }
  306. /* finally set the calculated flags in SPI_CS */
  307. bcm2835_wr(bs, BCM2835_SPI_CS, cs);
  308. }
  309. static int chip_match_name(struct gpio_chip *chip, void *data)
  310. {
  311. return !strcmp(chip->label, data);
  312. }
  313. static int bcm2835_spi_setup(struct spi_device *spi)
  314. {
  315. int err;
  316. struct gpio_chip *chip;
  317. /*
  318. * sanity checking the native-chipselects
  319. */
  320. if (spi->mode & SPI_NO_CS)
  321. return 0;
  322. if (gpio_is_valid(spi->cs_gpio))
  323. return 0;
  324. if (spi->chip_select > 1) {
  325. /* error in the case of native CS requested with CS > 1
  326. * officially there is a CS2, but it is not documented
  327. * which GPIO is connected with that...
  328. */
  329. dev_err(&spi->dev,
  330. "setup: only two native chip-selects are supported\n");
  331. return -EINVAL;
  332. }
  333. /* now translate native cs to GPIO */
  334. /* get the gpio chip for the base */
  335. chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
  336. if (!chip)
  337. return 0;
  338. /* and calculate the real CS */
  339. spi->cs_gpio = chip->base + 8 - spi->chip_select;
  340. /* and set up the "mode" and level */
  341. dev_info(&spi->dev, "setting up native-CS%i as GPIO %i\n",
  342. spi->chip_select, spi->cs_gpio);
  343. /* set up GPIO as output and pull to the correct level */
  344. err = gpio_direction_output(spi->cs_gpio,
  345. (spi->mode & SPI_CS_HIGH) ? 0 : 1);
  346. if (err) {
  347. dev_err(&spi->dev,
  348. "could not set CS%i gpio %i as output: %i",
  349. spi->chip_select, spi->cs_gpio, err);
  350. return err;
  351. }
  352. /* the implementation of pinctrl-bcm2835 currently does not
  353. * set the GPIO value when using gpio_direction_output
  354. * so we are setting it here explicitly
  355. */
  356. gpio_set_value(spi->cs_gpio, (spi->mode & SPI_CS_HIGH) ? 0 : 1);
  357. return 0;
  358. }
  359. static int bcm2835_spi_probe(struct platform_device *pdev)
  360. {
  361. struct spi_master *master;
  362. struct bcm2835_spi *bs;
  363. struct resource *res;
  364. int err;
  365. master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  366. if (!master) {
  367. dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  368. return -ENOMEM;
  369. }
  370. platform_set_drvdata(pdev, master);
  371. master->mode_bits = BCM2835_SPI_MODE_BITS;
  372. master->bits_per_word_mask = SPI_BPW_MASK(8);
  373. master->num_chipselect = 3;
  374. master->setup = bcm2835_spi_setup;
  375. master->set_cs = bcm2835_spi_set_cs;
  376. master->transfer_one = bcm2835_spi_transfer_one;
  377. master->handle_err = bcm2835_spi_handle_err;
  378. master->dev.of_node = pdev->dev.of_node;
  379. bs = spi_master_get_devdata(master);
  380. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  381. bs->regs = devm_ioremap_resource(&pdev->dev, res);
  382. if (IS_ERR(bs->regs)) {
  383. err = PTR_ERR(bs->regs);
  384. goto out_master_put;
  385. }
  386. bs->clk = devm_clk_get(&pdev->dev, NULL);
  387. if (IS_ERR(bs->clk)) {
  388. err = PTR_ERR(bs->clk);
  389. dev_err(&pdev->dev, "could not get clk: %d\n", err);
  390. goto out_master_put;
  391. }
  392. bs->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  393. if (bs->irq <= 0) {
  394. dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq);
  395. err = bs->irq ? bs->irq : -ENODEV;
  396. goto out_master_put;
  397. }
  398. clk_prepare_enable(bs->clk);
  399. err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0,
  400. dev_name(&pdev->dev), master);
  401. if (err) {
  402. dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  403. goto out_clk_disable;
  404. }
  405. /* initialise the hardware with the default polarities */
  406. bcm2835_wr(bs, BCM2835_SPI_CS,
  407. BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
  408. err = devm_spi_register_master(&pdev->dev, master);
  409. if (err) {
  410. dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  411. goto out_clk_disable;
  412. }
  413. return 0;
  414. out_clk_disable:
  415. clk_disable_unprepare(bs->clk);
  416. out_master_put:
  417. spi_master_put(master);
  418. return err;
  419. }
  420. static int bcm2835_spi_remove(struct platform_device *pdev)
  421. {
  422. struct spi_master *master = platform_get_drvdata(pdev);
  423. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  424. /* Clear FIFOs, and disable the HW block */
  425. bcm2835_wr(bs, BCM2835_SPI_CS,
  426. BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
  427. clk_disable_unprepare(bs->clk);
  428. return 0;
  429. }
  430. static const struct of_device_id bcm2835_spi_match[] = {
  431. { .compatible = "brcm,bcm2835-spi", },
  432. {}
  433. };
  434. MODULE_DEVICE_TABLE(of, bcm2835_spi_match);
  435. static struct platform_driver bcm2835_spi_driver = {
  436. .driver = {
  437. .name = DRV_NAME,
  438. .of_match_table = bcm2835_spi_match,
  439. },
  440. .probe = bcm2835_spi_probe,
  441. .remove = bcm2835_spi_remove,
  442. };
  443. module_platform_driver(bcm2835_spi_driver);
  444. MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835");
  445. MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  446. MODULE_LICENSE("GPL v2");