spi-bcm2835.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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 <asm/page.h>
  23. #include <linux/clk.h>
  24. #include <linux/completion.h>
  25. #include <linux/delay.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/dmaengine.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_address.h>
  35. #include <linux/of_device.h>
  36. #include <linux/of_gpio.h>
  37. #include <linux/of_irq.h>
  38. #include <linux/spi/spi.h>
  39. /* SPI register offsets */
  40. #define BCM2835_SPI_CS 0x00
  41. #define BCM2835_SPI_FIFO 0x04
  42. #define BCM2835_SPI_CLK 0x08
  43. #define BCM2835_SPI_DLEN 0x0c
  44. #define BCM2835_SPI_LTOH 0x10
  45. #define BCM2835_SPI_DC 0x14
  46. /* Bitfields in CS */
  47. #define BCM2835_SPI_CS_LEN_LONG 0x02000000
  48. #define BCM2835_SPI_CS_DMA_LEN 0x01000000
  49. #define BCM2835_SPI_CS_CSPOL2 0x00800000
  50. #define BCM2835_SPI_CS_CSPOL1 0x00400000
  51. #define BCM2835_SPI_CS_CSPOL0 0x00200000
  52. #define BCM2835_SPI_CS_RXF 0x00100000
  53. #define BCM2835_SPI_CS_RXR 0x00080000
  54. #define BCM2835_SPI_CS_TXD 0x00040000
  55. #define BCM2835_SPI_CS_RXD 0x00020000
  56. #define BCM2835_SPI_CS_DONE 0x00010000
  57. #define BCM2835_SPI_CS_LEN 0x00002000
  58. #define BCM2835_SPI_CS_REN 0x00001000
  59. #define BCM2835_SPI_CS_ADCS 0x00000800
  60. #define BCM2835_SPI_CS_INTR 0x00000400
  61. #define BCM2835_SPI_CS_INTD 0x00000200
  62. #define BCM2835_SPI_CS_DMAEN 0x00000100
  63. #define BCM2835_SPI_CS_TA 0x00000080
  64. #define BCM2835_SPI_CS_CSPOL 0x00000040
  65. #define BCM2835_SPI_CS_CLEAR_RX 0x00000020
  66. #define BCM2835_SPI_CS_CLEAR_TX 0x00000010
  67. #define BCM2835_SPI_CS_CPOL 0x00000008
  68. #define BCM2835_SPI_CS_CPHA 0x00000004
  69. #define BCM2835_SPI_CS_CS_10 0x00000002
  70. #define BCM2835_SPI_CS_CS_01 0x00000001
  71. #define BCM2835_SPI_POLLING_LIMIT_US 30
  72. #define BCM2835_SPI_POLLING_JIFFIES 2
  73. #define BCM2835_SPI_DMA_MIN_LENGTH 96
  74. #define BCM2835_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
  75. | SPI_NO_CS | SPI_3WIRE)
  76. #define DRV_NAME "spi-bcm2835"
  77. struct bcm2835_spi {
  78. void __iomem *regs;
  79. struct clk *clk;
  80. int irq;
  81. const u8 *tx_buf;
  82. u8 *rx_buf;
  83. int tx_len;
  84. int rx_len;
  85. unsigned int dma_pending;
  86. };
  87. static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned reg)
  88. {
  89. return readl(bs->regs + reg);
  90. }
  91. static inline void bcm2835_wr(struct bcm2835_spi *bs, unsigned reg, u32 val)
  92. {
  93. writel(val, bs->regs + reg);
  94. }
  95. static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs)
  96. {
  97. u8 byte;
  98. while ((bs->rx_len) &&
  99. (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD)) {
  100. byte = bcm2835_rd(bs, BCM2835_SPI_FIFO);
  101. if (bs->rx_buf)
  102. *bs->rx_buf++ = byte;
  103. bs->rx_len--;
  104. }
  105. }
  106. static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs)
  107. {
  108. u8 byte;
  109. while ((bs->tx_len) &&
  110. (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) {
  111. byte = bs->tx_buf ? *bs->tx_buf++ : 0;
  112. bcm2835_wr(bs, BCM2835_SPI_FIFO, byte);
  113. bs->tx_len--;
  114. }
  115. }
  116. static void bcm2835_spi_reset_hw(struct spi_master *master)
  117. {
  118. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  119. u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
  120. /* Disable SPI interrupts and transfer */
  121. cs &= ~(BCM2835_SPI_CS_INTR |
  122. BCM2835_SPI_CS_INTD |
  123. BCM2835_SPI_CS_DMAEN |
  124. BCM2835_SPI_CS_TA);
  125. /* and reset RX/TX FIFOS */
  126. cs |= BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX;
  127. /* and reset the SPI_HW */
  128. bcm2835_wr(bs, BCM2835_SPI_CS, cs);
  129. /* as well as DLEN */
  130. bcm2835_wr(bs, BCM2835_SPI_DLEN, 0);
  131. }
  132. static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id)
  133. {
  134. struct spi_master *master = dev_id;
  135. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  136. /* Read as many bytes as possible from FIFO */
  137. bcm2835_rd_fifo(bs);
  138. /* Write as many bytes as possible to FIFO */
  139. bcm2835_wr_fifo(bs);
  140. if (!bs->rx_len) {
  141. /* Transfer complete - reset SPI HW */
  142. bcm2835_spi_reset_hw(master);
  143. /* wake up the framework */
  144. complete(&master->xfer_completion);
  145. }
  146. return IRQ_HANDLED;
  147. }
  148. static int bcm2835_spi_transfer_one_irq(struct spi_master *master,
  149. struct spi_device *spi,
  150. struct spi_transfer *tfr,
  151. u32 cs)
  152. {
  153. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  154. /* fill in fifo if we have gpio-cs
  155. * note that there have been rare events where the native-CS
  156. * flapped for <1us which may change the behaviour
  157. * with gpio-cs this does not happen, so it is implemented
  158. * only for this case
  159. */
  160. if (gpio_is_valid(spi->cs_gpio)) {
  161. /* enable HW block, but without interrupts enabled
  162. * this would triggern an immediate interrupt
  163. */
  164. bcm2835_wr(bs, BCM2835_SPI_CS,
  165. cs | BCM2835_SPI_CS_TA);
  166. /* fill in tx fifo as much as possible */
  167. bcm2835_wr_fifo(bs);
  168. }
  169. /*
  170. * Enable the HW block. This will immediately trigger a DONE (TX
  171. * empty) interrupt, upon which we will fill the TX FIFO with the
  172. * first TX bytes. Pre-filling the TX FIFO here to avoid the
  173. * interrupt doesn't work:-(
  174. */
  175. cs |= BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA;
  176. bcm2835_wr(bs, BCM2835_SPI_CS, cs);
  177. /* signal that we need to wait for completion */
  178. return 1;
  179. }
  180. /*
  181. * DMA support
  182. *
  183. * this implementation has currently a few issues in so far as it does
  184. * not work arrount limitations of the HW.
  185. *
  186. * the main one being that DMA transfers are limited to 16 bit
  187. * (so 0 to 65535 bytes) by the SPI HW due to BCM2835_SPI_DLEN
  188. *
  189. * also we currently assume that the scatter-gather fragments are
  190. * all multiple of 4 (except the last) - otherwise we would need
  191. * to reset the FIFO before subsequent transfers...
  192. * this also means that tx/rx transfers sg's need to be of equal size!
  193. *
  194. * there may be a few more border-cases we may need to address as well
  195. * but unfortunately this would mean splitting up the scatter-gather
  196. * list making it slightly unpractical...
  197. */
  198. static void bcm2835_spi_dma_done(void *data)
  199. {
  200. struct spi_master *master = data;
  201. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  202. /* reset fifo and HW */
  203. bcm2835_spi_reset_hw(master);
  204. /* and terminate tx-dma as we do not have an irq for it
  205. * because when the rx dma will terminate and this callback
  206. * is called the tx-dma must have finished - can't get to this
  207. * situation otherwise...
  208. */
  209. if (cmpxchg(&bs->dma_pending, true, false)) {
  210. dmaengine_terminate_all(master->dma_tx);
  211. }
  212. /* and mark as completed */;
  213. complete(&master->xfer_completion);
  214. }
  215. static int bcm2835_spi_prepare_sg(struct spi_master *master,
  216. struct spi_transfer *tfr,
  217. bool is_tx)
  218. {
  219. struct dma_chan *chan;
  220. struct scatterlist *sgl;
  221. unsigned int nents;
  222. enum dma_transfer_direction dir;
  223. unsigned long flags;
  224. struct dma_async_tx_descriptor *desc;
  225. dma_cookie_t cookie;
  226. if (is_tx) {
  227. dir = DMA_MEM_TO_DEV;
  228. chan = master->dma_tx;
  229. nents = tfr->tx_sg.nents;
  230. sgl = tfr->tx_sg.sgl;
  231. flags = 0 /* no tx interrupt */;
  232. } else {
  233. dir = DMA_DEV_TO_MEM;
  234. chan = master->dma_rx;
  235. nents = tfr->rx_sg.nents;
  236. sgl = tfr->rx_sg.sgl;
  237. flags = DMA_PREP_INTERRUPT;
  238. }
  239. /* prepare the channel */
  240. desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags);
  241. if (!desc)
  242. return -EINVAL;
  243. /* set callback for rx */
  244. if (!is_tx) {
  245. desc->callback = bcm2835_spi_dma_done;
  246. desc->callback_param = master;
  247. }
  248. /* submit it to DMA-engine */
  249. cookie = dmaengine_submit(desc);
  250. return dma_submit_error(cookie);
  251. }
  252. static inline int bcm2835_check_sg_length(struct sg_table *sgt)
  253. {
  254. int i;
  255. struct scatterlist *sgl;
  256. /* check that the sg entries are word-sized (except for last) */
  257. for_each_sg(sgt->sgl, sgl, (int)sgt->nents - 1, i) {
  258. if (sg_dma_len(sgl) % 4)
  259. return -EFAULT;
  260. }
  261. return 0;
  262. }
  263. static int bcm2835_spi_transfer_one_dma(struct spi_master *master,
  264. struct spi_device *spi,
  265. struct spi_transfer *tfr,
  266. u32 cs)
  267. {
  268. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  269. int ret;
  270. /* check that the scatter gather segments are all a multiple of 4 */
  271. if (bcm2835_check_sg_length(&tfr->tx_sg) ||
  272. bcm2835_check_sg_length(&tfr->rx_sg)) {
  273. dev_warn_once(&spi->dev,
  274. "scatter gather segment length is not a multiple of 4 - falling back to interrupt mode\n");
  275. return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs);
  276. }
  277. /* setup tx-DMA */
  278. ret = bcm2835_spi_prepare_sg(master, tfr, true);
  279. if (ret)
  280. return ret;
  281. /* start TX early */
  282. dma_async_issue_pending(master->dma_tx);
  283. /* mark as dma pending */
  284. bs->dma_pending = 1;
  285. /* set the DMA length */
  286. bcm2835_wr(bs, BCM2835_SPI_DLEN, tfr->len);
  287. /* start the HW */
  288. bcm2835_wr(bs, BCM2835_SPI_CS,
  289. cs | BCM2835_SPI_CS_TA | BCM2835_SPI_CS_DMAEN);
  290. /* setup rx-DMA late - to run transfers while
  291. * mapping of the rx buffers still takes place
  292. * this saves 10us or more.
  293. */
  294. ret = bcm2835_spi_prepare_sg(master, tfr, false);
  295. if (ret) {
  296. /* need to reset on errors */
  297. dmaengine_terminate_all(master->dma_tx);
  298. bs->dma_pending = false;
  299. bcm2835_spi_reset_hw(master);
  300. return ret;
  301. }
  302. /* start rx dma late */
  303. dma_async_issue_pending(master->dma_rx);
  304. /* wait for wakeup in framework */
  305. return 1;
  306. }
  307. static bool bcm2835_spi_can_dma(struct spi_master *master,
  308. struct spi_device *spi,
  309. struct spi_transfer *tfr)
  310. {
  311. /* only run for gpio_cs */
  312. if (!gpio_is_valid(spi->cs_gpio))
  313. return false;
  314. /* we start DMA efforts only on bigger transfers */
  315. if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH)
  316. return false;
  317. /* BCM2835_SPI_DLEN has defined a max transfer size as
  318. * 16 bit, so max is 65535
  319. * we can revisit this by using an alternative transfer
  320. * method - ideally this would get done without any more
  321. * interaction...
  322. */
  323. if (tfr->len > 65535) {
  324. dev_warn_once(&spi->dev,
  325. "transfer size of %d too big for dma-transfer\n",
  326. tfr->len);
  327. return false;
  328. }
  329. /* if we run rx/tx_buf with word aligned addresses then we are OK */
  330. if ((((size_t)tfr->rx_buf & 3) == 0) &&
  331. (((size_t)tfr->tx_buf & 3) == 0))
  332. return true;
  333. /* otherwise we only allow transfers within the same page
  334. * to avoid wasting time on dma_mapping when it is not practical
  335. */
  336. if (((size_t)tfr->tx_buf & (PAGE_SIZE - 1)) + tfr->len > PAGE_SIZE) {
  337. dev_warn_once(&spi->dev,
  338. "Unaligned spi tx-transfer bridging page\n");
  339. return false;
  340. }
  341. if (((size_t)tfr->rx_buf & (PAGE_SIZE - 1)) + tfr->len > PAGE_SIZE) {
  342. dev_warn_once(&spi->dev,
  343. "Unaligned spi rx-transfer bridging page\n");
  344. return false;
  345. }
  346. /* return OK */
  347. return true;
  348. }
  349. static void bcm2835_dma_release(struct spi_master *master)
  350. {
  351. if (master->dma_tx) {
  352. dmaengine_terminate_all(master->dma_tx);
  353. dma_release_channel(master->dma_tx);
  354. master->dma_tx = NULL;
  355. }
  356. if (master->dma_rx) {
  357. dmaengine_terminate_all(master->dma_rx);
  358. dma_release_channel(master->dma_rx);
  359. master->dma_rx = NULL;
  360. }
  361. }
  362. static void bcm2835_dma_init(struct spi_master *master, struct device *dev)
  363. {
  364. struct dma_slave_config slave_config;
  365. const __be32 *addr;
  366. dma_addr_t dma_reg_base;
  367. int ret;
  368. /* base address in dma-space */
  369. addr = of_get_address(master->dev.of_node, 0, NULL, NULL);
  370. if (!addr) {
  371. dev_err(dev, "could not get DMA-register address - not using dma mode\n");
  372. goto err;
  373. }
  374. dma_reg_base = be32_to_cpup(addr);
  375. /* get tx/rx dma */
  376. master->dma_tx = dma_request_slave_channel(dev, "tx");
  377. if (!master->dma_tx) {
  378. dev_err(dev, "no tx-dma configuration found - not using dma mode\n");
  379. goto err;
  380. }
  381. master->dma_rx = dma_request_slave_channel(dev, "rx");
  382. if (!master->dma_rx) {
  383. dev_err(dev, "no rx-dma configuration found - not using dma mode\n");
  384. goto err_release;
  385. }
  386. /* configure DMAs */
  387. slave_config.direction = DMA_MEM_TO_DEV;
  388. slave_config.dst_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
  389. slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  390. ret = dmaengine_slave_config(master->dma_tx, &slave_config);
  391. if (ret)
  392. goto err_config;
  393. slave_config.direction = DMA_DEV_TO_MEM;
  394. slave_config.src_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
  395. slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  396. ret = dmaengine_slave_config(master->dma_rx, &slave_config);
  397. if (ret)
  398. goto err_config;
  399. /* all went well, so set can_dma */
  400. master->can_dma = bcm2835_spi_can_dma;
  401. master->max_dma_len = 65535; /* limitation by BCM2835_SPI_DLEN */
  402. /* need to do TX AND RX DMA, so we need dummy buffers */
  403. master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
  404. return;
  405. err_config:
  406. dev_err(dev, "issue configuring dma: %d - not using DMA mode\n",
  407. ret);
  408. err_release:
  409. bcm2835_dma_release(master);
  410. err:
  411. return;
  412. }
  413. static int bcm2835_spi_transfer_one_poll(struct spi_master *master,
  414. struct spi_device *spi,
  415. struct spi_transfer *tfr,
  416. u32 cs,
  417. unsigned long long xfer_time_us)
  418. {
  419. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  420. unsigned long timeout;
  421. /* enable HW block without interrupts */
  422. bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
  423. /* fill in the fifo before timeout calculations
  424. * if we are interrupted here, then the data is
  425. * getting transferred by the HW while we are interrupted
  426. */
  427. bcm2835_wr_fifo(bs);
  428. /* set the timeout */
  429. timeout = jiffies + BCM2835_SPI_POLLING_JIFFIES;
  430. /* loop until finished the transfer */
  431. while (bs->rx_len) {
  432. /* fill in tx fifo with remaining data */
  433. bcm2835_wr_fifo(bs);
  434. /* read from fifo as much as possible */
  435. bcm2835_rd_fifo(bs);
  436. /* if there is still data pending to read
  437. * then check the timeout
  438. */
  439. if (bs->rx_len && time_after(jiffies, timeout)) {
  440. dev_dbg_ratelimited(&spi->dev,
  441. "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n",
  442. jiffies - timeout,
  443. bs->tx_len, bs->rx_len);
  444. /* fall back to interrupt mode */
  445. return bcm2835_spi_transfer_one_irq(master, spi,
  446. tfr, cs);
  447. }
  448. }
  449. /* Transfer complete - reset SPI HW */
  450. bcm2835_spi_reset_hw(master);
  451. /* and return without waiting for completion */
  452. return 0;
  453. }
  454. static int bcm2835_spi_transfer_one(struct spi_master *master,
  455. struct spi_device *spi,
  456. struct spi_transfer *tfr)
  457. {
  458. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  459. unsigned long spi_hz, clk_hz, cdiv;
  460. unsigned long spi_used_hz;
  461. unsigned long long xfer_time_us;
  462. u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
  463. /* set clock */
  464. spi_hz = tfr->speed_hz;
  465. clk_hz = clk_get_rate(bs->clk);
  466. if (spi_hz >= clk_hz / 2) {
  467. cdiv = 2; /* clk_hz/2 is the fastest we can go */
  468. } else if (spi_hz) {
  469. /* CDIV must be a multiple of two */
  470. cdiv = DIV_ROUND_UP(clk_hz, spi_hz);
  471. cdiv += (cdiv % 2);
  472. if (cdiv >= 65536)
  473. cdiv = 0; /* 0 is the slowest we can go */
  474. } else {
  475. cdiv = 0; /* 0 is the slowest we can go */
  476. }
  477. spi_used_hz = cdiv ? (clk_hz / cdiv) : (clk_hz / 65536);
  478. bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv);
  479. /* handle all the 3-wire mode */
  480. if (spi->mode & SPI_3WIRE && tfr->rx_buf &&
  481. tfr->rx_buf != master->dummy_rx)
  482. cs |= BCM2835_SPI_CS_REN;
  483. else
  484. cs &= ~BCM2835_SPI_CS_REN;
  485. /* for gpio_cs set dummy CS so that no HW-CS get changed
  486. * we can not run this in bcm2835_spi_set_cs, as it does
  487. * not get called for cs_gpio cases, so we need to do it here
  488. */
  489. if (gpio_is_valid(spi->cs_gpio) || (spi->mode & SPI_NO_CS))
  490. cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
  491. /* set transmit buffers and length */
  492. bs->tx_buf = tfr->tx_buf;
  493. bs->rx_buf = tfr->rx_buf;
  494. bs->tx_len = tfr->len;
  495. bs->rx_len = tfr->len;
  496. /* calculate the estimated time in us the transfer runs */
  497. xfer_time_us = (unsigned long long)tfr->len
  498. * 9 /* clocks/byte - SPI-HW waits 1 clock after each byte */
  499. * 1000000;
  500. do_div(xfer_time_us, spi_used_hz);
  501. /* for short requests run polling*/
  502. if (xfer_time_us <= BCM2835_SPI_POLLING_LIMIT_US)
  503. return bcm2835_spi_transfer_one_poll(master, spi, tfr,
  504. cs, xfer_time_us);
  505. /* run in dma mode if conditions are right */
  506. if (master->can_dma && bcm2835_spi_can_dma(master, spi, tfr))
  507. return bcm2835_spi_transfer_one_dma(master, spi, tfr, cs);
  508. /* run in interrupt-mode */
  509. return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs);
  510. }
  511. static int bcm2835_spi_prepare_message(struct spi_master *master,
  512. struct spi_message *msg)
  513. {
  514. struct spi_device *spi = msg->spi;
  515. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  516. u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
  517. cs &= ~(BCM2835_SPI_CS_CPOL | BCM2835_SPI_CS_CPHA);
  518. if (spi->mode & SPI_CPOL)
  519. cs |= BCM2835_SPI_CS_CPOL;
  520. if (spi->mode & SPI_CPHA)
  521. cs |= BCM2835_SPI_CS_CPHA;
  522. bcm2835_wr(bs, BCM2835_SPI_CS, cs);
  523. return 0;
  524. }
  525. static void bcm2835_spi_handle_err(struct spi_master *master,
  526. struct spi_message *msg)
  527. {
  528. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  529. /* if an error occurred and we have an active dma, then terminate */
  530. if (cmpxchg(&bs->dma_pending, true, false)) {
  531. dmaengine_terminate_all(master->dma_tx);
  532. dmaengine_terminate_all(master->dma_rx);
  533. }
  534. /* and reset */
  535. bcm2835_spi_reset_hw(master);
  536. }
  537. static void bcm2835_spi_set_cs(struct spi_device *spi, bool gpio_level)
  538. {
  539. /*
  540. * we can assume that we are "native" as per spi_set_cs
  541. * calling us ONLY when cs_gpio is not set
  542. * we can also assume that we are CS < 3 as per bcm2835_spi_setup
  543. * we would not get called because of error handling there.
  544. * the level passed is the electrical level not enabled/disabled
  545. * so it has to get translated back to enable/disable
  546. * see spi_set_cs in spi.c for the implementation
  547. */
  548. struct spi_master *master = spi->master;
  549. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  550. u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
  551. bool enable;
  552. /* calculate the enable flag from the passed gpio_level */
  553. enable = (spi->mode & SPI_CS_HIGH) ? gpio_level : !gpio_level;
  554. /* set flags for "reverse" polarity in the registers */
  555. if (spi->mode & SPI_CS_HIGH) {
  556. /* set the correct CS-bits */
  557. cs |= BCM2835_SPI_CS_CSPOL;
  558. cs |= BCM2835_SPI_CS_CSPOL0 << spi->chip_select;
  559. } else {
  560. /* clean the CS-bits */
  561. cs &= ~BCM2835_SPI_CS_CSPOL;
  562. cs &= ~(BCM2835_SPI_CS_CSPOL0 << spi->chip_select);
  563. }
  564. /* select the correct chip_select depending on disabled/enabled */
  565. if (enable) {
  566. /* set cs correctly */
  567. if (spi->mode & SPI_NO_CS) {
  568. /* use the "undefined" chip-select */
  569. cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
  570. } else {
  571. /* set the chip select */
  572. cs &= ~(BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01);
  573. cs |= spi->chip_select;
  574. }
  575. } else {
  576. /* disable CSPOL which puts HW-CS into deselected state */
  577. cs &= ~BCM2835_SPI_CS_CSPOL;
  578. /* use the "undefined" chip-select as precaution */
  579. cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
  580. }
  581. /* finally set the calculated flags in SPI_CS */
  582. bcm2835_wr(bs, BCM2835_SPI_CS, cs);
  583. }
  584. static int chip_match_name(struct gpio_chip *chip, void *data)
  585. {
  586. return !strcmp(chip->label, data);
  587. }
  588. static int bcm2835_spi_setup(struct spi_device *spi)
  589. {
  590. int err;
  591. struct gpio_chip *chip;
  592. /*
  593. * sanity checking the native-chipselects
  594. */
  595. if (spi->mode & SPI_NO_CS)
  596. return 0;
  597. if (gpio_is_valid(spi->cs_gpio))
  598. return 0;
  599. if (spi->chip_select > 1) {
  600. /* error in the case of native CS requested with CS > 1
  601. * officially there is a CS2, but it is not documented
  602. * which GPIO is connected with that...
  603. */
  604. dev_err(&spi->dev,
  605. "setup: only two native chip-selects are supported\n");
  606. return -EINVAL;
  607. }
  608. /* now translate native cs to GPIO */
  609. /* get the gpio chip for the base */
  610. chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
  611. if (!chip)
  612. return 0;
  613. /* and calculate the real CS */
  614. spi->cs_gpio = chip->base + 8 - spi->chip_select;
  615. /* and set up the "mode" and level */
  616. dev_info(&spi->dev, "setting up native-CS%i as GPIO %i\n",
  617. spi->chip_select, spi->cs_gpio);
  618. /* set up GPIO as output and pull to the correct level */
  619. err = gpio_direction_output(spi->cs_gpio,
  620. (spi->mode & SPI_CS_HIGH) ? 0 : 1);
  621. if (err) {
  622. dev_err(&spi->dev,
  623. "could not set CS%i gpio %i as output: %i",
  624. spi->chip_select, spi->cs_gpio, err);
  625. return err;
  626. }
  627. return 0;
  628. }
  629. static int bcm2835_spi_probe(struct platform_device *pdev)
  630. {
  631. struct spi_master *master;
  632. struct bcm2835_spi *bs;
  633. struct resource *res;
  634. int err;
  635. master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  636. if (!master) {
  637. dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  638. return -ENOMEM;
  639. }
  640. platform_set_drvdata(pdev, master);
  641. master->mode_bits = BCM2835_SPI_MODE_BITS;
  642. master->bits_per_word_mask = SPI_BPW_MASK(8);
  643. master->num_chipselect = 3;
  644. master->setup = bcm2835_spi_setup;
  645. master->set_cs = bcm2835_spi_set_cs;
  646. master->transfer_one = bcm2835_spi_transfer_one;
  647. master->handle_err = bcm2835_spi_handle_err;
  648. master->prepare_message = bcm2835_spi_prepare_message;
  649. master->dev.of_node = pdev->dev.of_node;
  650. bs = spi_master_get_devdata(master);
  651. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  652. bs->regs = devm_ioremap_resource(&pdev->dev, res);
  653. if (IS_ERR(bs->regs)) {
  654. err = PTR_ERR(bs->regs);
  655. goto out_master_put;
  656. }
  657. bs->clk = devm_clk_get(&pdev->dev, NULL);
  658. if (IS_ERR(bs->clk)) {
  659. err = PTR_ERR(bs->clk);
  660. dev_err(&pdev->dev, "could not get clk: %d\n", err);
  661. goto out_master_put;
  662. }
  663. bs->irq = platform_get_irq(pdev, 0);
  664. if (bs->irq <= 0) {
  665. dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq);
  666. err = bs->irq ? bs->irq : -ENODEV;
  667. goto out_master_put;
  668. }
  669. clk_prepare_enable(bs->clk);
  670. bcm2835_dma_init(master, &pdev->dev);
  671. /* initialise the hardware with the default polarities */
  672. bcm2835_wr(bs, BCM2835_SPI_CS,
  673. BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
  674. err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0,
  675. dev_name(&pdev->dev), master);
  676. if (err) {
  677. dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  678. goto out_clk_disable;
  679. }
  680. err = devm_spi_register_master(&pdev->dev, master);
  681. if (err) {
  682. dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  683. goto out_clk_disable;
  684. }
  685. return 0;
  686. out_clk_disable:
  687. clk_disable_unprepare(bs->clk);
  688. out_master_put:
  689. spi_master_put(master);
  690. return err;
  691. }
  692. static int bcm2835_spi_remove(struct platform_device *pdev)
  693. {
  694. struct spi_master *master = platform_get_drvdata(pdev);
  695. struct bcm2835_spi *bs = spi_master_get_devdata(master);
  696. /* Clear FIFOs, and disable the HW block */
  697. bcm2835_wr(bs, BCM2835_SPI_CS,
  698. BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
  699. clk_disable_unprepare(bs->clk);
  700. bcm2835_dma_release(master);
  701. return 0;
  702. }
  703. static const struct of_device_id bcm2835_spi_match[] = {
  704. { .compatible = "brcm,bcm2835-spi", },
  705. {}
  706. };
  707. MODULE_DEVICE_TABLE(of, bcm2835_spi_match);
  708. static struct platform_driver bcm2835_spi_driver = {
  709. .driver = {
  710. .name = DRV_NAME,
  711. .of_match_table = bcm2835_spi_match,
  712. },
  713. .probe = bcm2835_spi_probe,
  714. .remove = bcm2835_spi_remove,
  715. };
  716. module_platform_driver(bcm2835_spi_driver);
  717. MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835");
  718. MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  719. MODULE_LICENSE("GPL v2");