spi-dw-dma.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Special handling for DW DMA core
  4. *
  5. * Copyright (c) 2009, 2014 Intel Corporation.
  6. */
  7. #include <linux/completion.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/dmaengine.h>
  10. #include <linux/irqreturn.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/platform_data/dma-dw.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/types.h>
  17. #include "spi-dw.h"
  18. #define DW_SPI_RX_BUSY 0
  19. #define DW_SPI_RX_BURST_LEVEL 16
  20. #define DW_SPI_TX_BUSY 1
  21. #define DW_SPI_TX_BURST_LEVEL 16
  22. static bool dw_spi_dma_chan_filter(struct dma_chan *chan, void *param)
  23. {
  24. struct dw_dma_slave *s = param;
  25. if (s->dma_dev != chan->device->dev)
  26. return false;
  27. chan->private = s;
  28. return true;
  29. }
  30. static void dw_spi_dma_maxburst_init(struct dw_spi *dws)
  31. {
  32. struct dma_slave_caps caps;
  33. u32 max_burst, def_burst;
  34. int ret;
  35. def_burst = dws->fifo_len / 2;
  36. ret = dma_get_slave_caps(dws->rxchan, &caps);
  37. if (!ret && caps.max_burst)
  38. max_burst = caps.max_burst;
  39. else
  40. max_burst = DW_SPI_RX_BURST_LEVEL;
  41. dws->rxburst = min(max_burst, def_burst);
  42. dw_writel(dws, DW_SPI_DMARDLR, dws->rxburst - 1);
  43. ret = dma_get_slave_caps(dws->txchan, &caps);
  44. if (!ret && caps.max_burst)
  45. max_burst = caps.max_burst;
  46. else
  47. max_burst = DW_SPI_TX_BURST_LEVEL;
  48. /*
  49. * Having a Rx DMA channel serviced with higher priority than a Tx DMA
  50. * channel might not be enough to provide a well balanced DMA-based
  51. * SPI transfer interface. There might still be moments when the Tx DMA
  52. * channel is occasionally handled faster than the Rx DMA channel.
  53. * That in its turn will eventually cause the SPI Rx FIFO overflow if
  54. * SPI bus speed is high enough to fill the SPI Rx FIFO in before it's
  55. * cleared by the Rx DMA channel. In order to fix the problem the Tx
  56. * DMA activity is intentionally slowed down by limiting the SPI Tx
  57. * FIFO depth with a value twice bigger than the Tx burst length.
  58. */
  59. dws->txburst = min(max_burst, def_burst);
  60. dw_writel(dws, DW_SPI_DMATDLR, dws->txburst);
  61. }
  62. static void dw_spi_dma_sg_burst_init(struct dw_spi *dws)
  63. {
  64. struct dma_slave_caps tx = {0}, rx = {0};
  65. dma_get_slave_caps(dws->txchan, &tx);
  66. dma_get_slave_caps(dws->rxchan, &rx);
  67. if (tx.max_sg_burst > 0 && rx.max_sg_burst > 0)
  68. dws->dma_sg_burst = min(tx.max_sg_burst, rx.max_sg_burst);
  69. else if (tx.max_sg_burst > 0)
  70. dws->dma_sg_burst = tx.max_sg_burst;
  71. else if (rx.max_sg_burst > 0)
  72. dws->dma_sg_burst = rx.max_sg_burst;
  73. else
  74. dws->dma_sg_burst = 0;
  75. }
  76. static int dw_spi_dma_init_mfld(struct device *dev, struct dw_spi *dws)
  77. {
  78. struct dw_dma_slave dma_tx = { .dst_id = 1 }, *tx = &dma_tx;
  79. struct dw_dma_slave dma_rx = { .src_id = 0 }, *rx = &dma_rx;
  80. struct pci_dev *dma_dev;
  81. dma_cap_mask_t mask;
  82. /*
  83. * Get pci device for DMA controller, currently it could only
  84. * be the DMA controller of Medfield
  85. */
  86. dma_dev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0827, NULL);
  87. if (!dma_dev)
  88. return -ENODEV;
  89. dma_cap_zero(mask);
  90. dma_cap_set(DMA_SLAVE, mask);
  91. /* 1. Init rx channel */
  92. rx->dma_dev = &dma_dev->dev;
  93. dws->rxchan = dma_request_channel(mask, dw_spi_dma_chan_filter, rx);
  94. if (!dws->rxchan)
  95. goto err_exit;
  96. /* 2. Init tx channel */
  97. tx->dma_dev = &dma_dev->dev;
  98. dws->txchan = dma_request_channel(mask, dw_spi_dma_chan_filter, tx);
  99. if (!dws->txchan)
  100. goto free_rxchan;
  101. dws->master->dma_rx = dws->rxchan;
  102. dws->master->dma_tx = dws->txchan;
  103. init_completion(&dws->dma_completion);
  104. dw_spi_dma_maxburst_init(dws);
  105. dw_spi_dma_sg_burst_init(dws);
  106. pci_dev_put(dma_dev);
  107. return 0;
  108. free_rxchan:
  109. dma_release_channel(dws->rxchan);
  110. dws->rxchan = NULL;
  111. err_exit:
  112. pci_dev_put(dma_dev);
  113. return -EBUSY;
  114. }
  115. static int dw_spi_dma_init_generic(struct device *dev, struct dw_spi *dws)
  116. {
  117. int ret;
  118. dws->rxchan = dma_request_chan(dev, "rx");
  119. if (IS_ERR(dws->rxchan)) {
  120. ret = PTR_ERR(dws->rxchan);
  121. dws->rxchan = NULL;
  122. goto err_exit;
  123. }
  124. dws->txchan = dma_request_chan(dev, "tx");
  125. if (IS_ERR(dws->txchan)) {
  126. ret = PTR_ERR(dws->txchan);
  127. dws->txchan = NULL;
  128. goto free_rxchan;
  129. }
  130. dws->master->dma_rx = dws->rxchan;
  131. dws->master->dma_tx = dws->txchan;
  132. init_completion(&dws->dma_completion);
  133. dw_spi_dma_maxburst_init(dws);
  134. dw_spi_dma_sg_burst_init(dws);
  135. return 0;
  136. free_rxchan:
  137. dma_release_channel(dws->rxchan);
  138. dws->rxchan = NULL;
  139. err_exit:
  140. return ret;
  141. }
  142. static void dw_spi_dma_exit(struct dw_spi *dws)
  143. {
  144. if (dws->txchan) {
  145. dmaengine_terminate_sync(dws->txchan);
  146. dma_release_channel(dws->txchan);
  147. }
  148. if (dws->rxchan) {
  149. dmaengine_terminate_sync(dws->rxchan);
  150. dma_release_channel(dws->rxchan);
  151. }
  152. }
  153. static irqreturn_t dw_spi_dma_transfer_handler(struct dw_spi *dws)
  154. {
  155. dw_spi_check_status(dws, false);
  156. complete(&dws->dma_completion);
  157. return IRQ_HANDLED;
  158. }
  159. static bool dw_spi_can_dma(struct spi_controller *master,
  160. struct spi_device *spi, struct spi_transfer *xfer)
  161. {
  162. struct dw_spi *dws = spi_controller_get_devdata(master);
  163. return xfer->len > dws->fifo_len;
  164. }
  165. static enum dma_slave_buswidth dw_spi_dma_convert_width(u8 n_bytes)
  166. {
  167. if (n_bytes == 1)
  168. return DMA_SLAVE_BUSWIDTH_1_BYTE;
  169. else if (n_bytes == 2)
  170. return DMA_SLAVE_BUSWIDTH_2_BYTES;
  171. return DMA_SLAVE_BUSWIDTH_UNDEFINED;
  172. }
  173. static int dw_spi_dma_wait(struct dw_spi *dws, unsigned int len, u32 speed)
  174. {
  175. unsigned long long ms;
  176. ms = len * MSEC_PER_SEC * BITS_PER_BYTE;
  177. do_div(ms, speed);
  178. ms += ms + 200;
  179. if (ms > UINT_MAX)
  180. ms = UINT_MAX;
  181. ms = wait_for_completion_timeout(&dws->dma_completion,
  182. msecs_to_jiffies(ms));
  183. if (ms == 0) {
  184. dev_err(&dws->master->cur_msg->spi->dev,
  185. "DMA transaction timed out\n");
  186. return -ETIMEDOUT;
  187. }
  188. return 0;
  189. }
  190. static inline bool dw_spi_dma_tx_busy(struct dw_spi *dws)
  191. {
  192. return !(dw_readl(dws, DW_SPI_SR) & DW_SPI_SR_TF_EMPT);
  193. }
  194. static int dw_spi_dma_wait_tx_done(struct dw_spi *dws,
  195. struct spi_transfer *xfer)
  196. {
  197. int retry = DW_SPI_WAIT_RETRIES;
  198. struct spi_delay delay;
  199. u32 nents;
  200. nents = dw_readl(dws, DW_SPI_TXFLR);
  201. delay.unit = SPI_DELAY_UNIT_SCK;
  202. delay.value = nents * dws->n_bytes * BITS_PER_BYTE;
  203. while (dw_spi_dma_tx_busy(dws) && retry--)
  204. spi_delay_exec(&delay, xfer);
  205. if (retry < 0) {
  206. dev_err(&dws->master->dev, "Tx hanged up\n");
  207. return -EIO;
  208. }
  209. return 0;
  210. }
  211. /*
  212. * dws->dma_chan_busy is set before the dma transfer starts, callback for tx
  213. * channel will clear a corresponding bit.
  214. */
  215. static void dw_spi_dma_tx_done(void *arg)
  216. {
  217. struct dw_spi *dws = arg;
  218. clear_bit(DW_SPI_TX_BUSY, &dws->dma_chan_busy);
  219. if (test_bit(DW_SPI_RX_BUSY, &dws->dma_chan_busy))
  220. return;
  221. complete(&dws->dma_completion);
  222. }
  223. static int dw_spi_dma_config_tx(struct dw_spi *dws)
  224. {
  225. struct dma_slave_config txconf;
  226. memset(&txconf, 0, sizeof(txconf));
  227. txconf.direction = DMA_MEM_TO_DEV;
  228. txconf.dst_addr = dws->dma_addr;
  229. txconf.dst_maxburst = dws->txburst;
  230. txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  231. txconf.dst_addr_width = dw_spi_dma_convert_width(dws->n_bytes);
  232. txconf.device_fc = false;
  233. return dmaengine_slave_config(dws->txchan, &txconf);
  234. }
  235. static int dw_spi_dma_submit_tx(struct dw_spi *dws, struct scatterlist *sgl,
  236. unsigned int nents)
  237. {
  238. struct dma_async_tx_descriptor *txdesc;
  239. dma_cookie_t cookie;
  240. int ret;
  241. txdesc = dmaengine_prep_slave_sg(dws->txchan, sgl, nents,
  242. DMA_MEM_TO_DEV,
  243. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  244. if (!txdesc)
  245. return -ENOMEM;
  246. txdesc->callback = dw_spi_dma_tx_done;
  247. txdesc->callback_param = dws;
  248. cookie = dmaengine_submit(txdesc);
  249. ret = dma_submit_error(cookie);
  250. if (ret) {
  251. dmaengine_terminate_sync(dws->txchan);
  252. return ret;
  253. }
  254. set_bit(DW_SPI_TX_BUSY, &dws->dma_chan_busy);
  255. return 0;
  256. }
  257. static inline bool dw_spi_dma_rx_busy(struct dw_spi *dws)
  258. {
  259. return !!(dw_readl(dws, DW_SPI_SR) & DW_SPI_SR_RF_NOT_EMPT);
  260. }
  261. static int dw_spi_dma_wait_rx_done(struct dw_spi *dws)
  262. {
  263. int retry = DW_SPI_WAIT_RETRIES;
  264. struct spi_delay delay;
  265. unsigned long ns, us;
  266. u32 nents;
  267. /*
  268. * It's unlikely that DMA engine is still doing the data fetching, but
  269. * if it's let's give it some reasonable time. The timeout calculation
  270. * is based on the synchronous APB/SSI reference clock rate, on a
  271. * number of data entries left in the Rx FIFO, times a number of clock
  272. * periods normally needed for a single APB read/write transaction
  273. * without PREADY signal utilized (which is true for the DW APB SSI
  274. * controller).
  275. */
  276. nents = dw_readl(dws, DW_SPI_RXFLR);
  277. ns = 4U * NSEC_PER_SEC / dws->max_freq * nents;
  278. if (ns <= NSEC_PER_USEC) {
  279. delay.unit = SPI_DELAY_UNIT_NSECS;
  280. delay.value = ns;
  281. } else {
  282. us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
  283. delay.unit = SPI_DELAY_UNIT_USECS;
  284. delay.value = clamp_val(us, 0, USHRT_MAX);
  285. }
  286. while (dw_spi_dma_rx_busy(dws) && retry--)
  287. spi_delay_exec(&delay, NULL);
  288. if (retry < 0) {
  289. dev_err(&dws->master->dev, "Rx hanged up\n");
  290. return -EIO;
  291. }
  292. return 0;
  293. }
  294. /*
  295. * dws->dma_chan_busy is set before the dma transfer starts, callback for rx
  296. * channel will clear a corresponding bit.
  297. */
  298. static void dw_spi_dma_rx_done(void *arg)
  299. {
  300. struct dw_spi *dws = arg;
  301. clear_bit(DW_SPI_RX_BUSY, &dws->dma_chan_busy);
  302. if (test_bit(DW_SPI_TX_BUSY, &dws->dma_chan_busy))
  303. return;
  304. complete(&dws->dma_completion);
  305. }
  306. static int dw_spi_dma_config_rx(struct dw_spi *dws)
  307. {
  308. struct dma_slave_config rxconf;
  309. memset(&rxconf, 0, sizeof(rxconf));
  310. rxconf.direction = DMA_DEV_TO_MEM;
  311. rxconf.src_addr = dws->dma_addr;
  312. rxconf.src_maxburst = dws->rxburst;
  313. rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  314. rxconf.src_addr_width = dw_spi_dma_convert_width(dws->n_bytes);
  315. rxconf.device_fc = false;
  316. return dmaengine_slave_config(dws->rxchan, &rxconf);
  317. }
  318. static int dw_spi_dma_submit_rx(struct dw_spi *dws, struct scatterlist *sgl,
  319. unsigned int nents)
  320. {
  321. struct dma_async_tx_descriptor *rxdesc;
  322. dma_cookie_t cookie;
  323. int ret;
  324. rxdesc = dmaengine_prep_slave_sg(dws->rxchan, sgl, nents,
  325. DMA_DEV_TO_MEM,
  326. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  327. if (!rxdesc)
  328. return -ENOMEM;
  329. rxdesc->callback = dw_spi_dma_rx_done;
  330. rxdesc->callback_param = dws;
  331. cookie = dmaengine_submit(rxdesc);
  332. ret = dma_submit_error(cookie);
  333. if (ret) {
  334. dmaengine_terminate_sync(dws->rxchan);
  335. return ret;
  336. }
  337. set_bit(DW_SPI_RX_BUSY, &dws->dma_chan_busy);
  338. return 0;
  339. }
  340. static int dw_spi_dma_setup(struct dw_spi *dws, struct spi_transfer *xfer)
  341. {
  342. u16 imr, dma_ctrl;
  343. int ret;
  344. if (!xfer->tx_buf)
  345. return -EINVAL;
  346. /* Setup DMA channels */
  347. ret = dw_spi_dma_config_tx(dws);
  348. if (ret)
  349. return ret;
  350. if (xfer->rx_buf) {
  351. ret = dw_spi_dma_config_rx(dws);
  352. if (ret)
  353. return ret;
  354. }
  355. /* Set the DMA handshaking interface */
  356. dma_ctrl = DW_SPI_DMACR_TDMAE;
  357. if (xfer->rx_buf)
  358. dma_ctrl |= DW_SPI_DMACR_RDMAE;
  359. dw_writel(dws, DW_SPI_DMACR, dma_ctrl);
  360. /* Set the interrupt mask */
  361. imr = DW_SPI_INT_TXOI;
  362. if (xfer->rx_buf)
  363. imr |= DW_SPI_INT_RXUI | DW_SPI_INT_RXOI;
  364. dw_spi_umask_intr(dws, imr);
  365. reinit_completion(&dws->dma_completion);
  366. dws->transfer_handler = dw_spi_dma_transfer_handler;
  367. return 0;
  368. }
  369. static int dw_spi_dma_transfer_all(struct dw_spi *dws,
  370. struct spi_transfer *xfer)
  371. {
  372. int ret;
  373. /* Submit the DMA Tx transfer */
  374. ret = dw_spi_dma_submit_tx(dws, xfer->tx_sg.sgl, xfer->tx_sg.nents);
  375. if (ret)
  376. goto err_clear_dmac;
  377. /* Submit the DMA Rx transfer if required */
  378. if (xfer->rx_buf) {
  379. ret = dw_spi_dma_submit_rx(dws, xfer->rx_sg.sgl,
  380. xfer->rx_sg.nents);
  381. if (ret)
  382. goto err_clear_dmac;
  383. /* rx must be started before tx due to spi instinct */
  384. dma_async_issue_pending(dws->rxchan);
  385. }
  386. dma_async_issue_pending(dws->txchan);
  387. ret = dw_spi_dma_wait(dws, xfer->len, xfer->effective_speed_hz);
  388. err_clear_dmac:
  389. dw_writel(dws, DW_SPI_DMACR, 0);
  390. return ret;
  391. }
  392. /*
  393. * In case if at least one of the requested DMA channels doesn't support the
  394. * hardware accelerated SG list entries traverse, the DMA driver will most
  395. * likely work that around by performing the IRQ-based SG list entries
  396. * resubmission. That might and will cause a problem if the DMA Tx channel is
  397. * recharged and re-executed before the Rx DMA channel. Due to
  398. * non-deterministic IRQ-handler execution latency the DMA Tx channel will
  399. * start pushing data to the SPI bus before the Rx DMA channel is even
  400. * reinitialized with the next inbound SG list entry. By doing so the DMA Tx
  401. * channel will implicitly start filling the DW APB SSI Rx FIFO up, which while
  402. * the DMA Rx channel being recharged and re-executed will eventually be
  403. * overflown.
  404. *
  405. * In order to solve the problem we have to feed the DMA engine with SG list
  406. * entries one-by-one. It shall keep the DW APB SSI Tx and Rx FIFOs
  407. * synchronized and prevent the Rx FIFO overflow. Since in general the tx_sg
  408. * and rx_sg lists may have different number of entries of different lengths
  409. * (though total length should match) let's virtually split the SG-lists to the
  410. * set of DMA transfers, which length is a minimum of the ordered SG-entries
  411. * lengths. An ASCII-sketch of the implemented algo is following:
  412. * xfer->len
  413. * |___________|
  414. * tx_sg list: |___|____|__|
  415. * rx_sg list: |_|____|____|
  416. * DMA transfers: |_|_|__|_|__|
  417. *
  418. * Note in order to have this workaround solving the denoted problem the DMA
  419. * engine driver should properly initialize the max_sg_burst capability and set
  420. * the DMA device max segment size parameter with maximum data block size the
  421. * DMA engine supports.
  422. */
  423. static int dw_spi_dma_transfer_one(struct dw_spi *dws,
  424. struct spi_transfer *xfer)
  425. {
  426. struct scatterlist *tx_sg = NULL, *rx_sg = NULL, tx_tmp, rx_tmp;
  427. unsigned int tx_len = 0, rx_len = 0;
  428. unsigned int base, len;
  429. int ret;
  430. sg_init_table(&tx_tmp, 1);
  431. sg_init_table(&rx_tmp, 1);
  432. for (base = 0, len = 0; base < xfer->len; base += len) {
  433. /* Fetch next Tx DMA data chunk */
  434. if (!tx_len) {
  435. tx_sg = !tx_sg ? &xfer->tx_sg.sgl[0] : sg_next(tx_sg);
  436. sg_dma_address(&tx_tmp) = sg_dma_address(tx_sg);
  437. tx_len = sg_dma_len(tx_sg);
  438. }
  439. /* Fetch next Rx DMA data chunk */
  440. if (!rx_len) {
  441. rx_sg = !rx_sg ? &xfer->rx_sg.sgl[0] : sg_next(rx_sg);
  442. sg_dma_address(&rx_tmp) = sg_dma_address(rx_sg);
  443. rx_len = sg_dma_len(rx_sg);
  444. }
  445. len = min(tx_len, rx_len);
  446. sg_dma_len(&tx_tmp) = len;
  447. sg_dma_len(&rx_tmp) = len;
  448. /* Submit DMA Tx transfer */
  449. ret = dw_spi_dma_submit_tx(dws, &tx_tmp, 1);
  450. if (ret)
  451. break;
  452. /* Submit DMA Rx transfer */
  453. ret = dw_spi_dma_submit_rx(dws, &rx_tmp, 1);
  454. if (ret)
  455. break;
  456. /* Rx must be started before Tx due to SPI instinct */
  457. dma_async_issue_pending(dws->rxchan);
  458. dma_async_issue_pending(dws->txchan);
  459. /*
  460. * Here we only need to wait for the DMA transfer to be
  461. * finished since SPI controller is kept enabled during the
  462. * procedure this loop implements and there is no risk to lose
  463. * data left in the Tx/Rx FIFOs.
  464. */
  465. ret = dw_spi_dma_wait(dws, len, xfer->effective_speed_hz);
  466. if (ret)
  467. break;
  468. reinit_completion(&dws->dma_completion);
  469. sg_dma_address(&tx_tmp) += len;
  470. sg_dma_address(&rx_tmp) += len;
  471. tx_len -= len;
  472. rx_len -= len;
  473. }
  474. dw_writel(dws, DW_SPI_DMACR, 0);
  475. return ret;
  476. }
  477. static int dw_spi_dma_transfer(struct dw_spi *dws, struct spi_transfer *xfer)
  478. {
  479. unsigned int nents;
  480. int ret;
  481. nents = max(xfer->tx_sg.nents, xfer->rx_sg.nents);
  482. /*
  483. * Execute normal DMA-based transfer (which submits the Rx and Tx SG
  484. * lists directly to the DMA engine at once) if either full hardware
  485. * accelerated SG list traverse is supported by both channels, or the
  486. * Tx-only SPI transfer is requested, or the DMA engine is capable to
  487. * handle both SG lists on hardware accelerated basis.
  488. */
  489. if (!dws->dma_sg_burst || !xfer->rx_buf || nents <= dws->dma_sg_burst)
  490. ret = dw_spi_dma_transfer_all(dws, xfer);
  491. else
  492. ret = dw_spi_dma_transfer_one(dws, xfer);
  493. if (ret)
  494. return ret;
  495. if (dws->master->cur_msg->status == -EINPROGRESS) {
  496. ret = dw_spi_dma_wait_tx_done(dws, xfer);
  497. if (ret)
  498. return ret;
  499. }
  500. if (xfer->rx_buf && dws->master->cur_msg->status == -EINPROGRESS)
  501. ret = dw_spi_dma_wait_rx_done(dws);
  502. return ret;
  503. }
  504. static void dw_spi_dma_stop(struct dw_spi *dws)
  505. {
  506. if (test_bit(DW_SPI_TX_BUSY, &dws->dma_chan_busy)) {
  507. dmaengine_terminate_sync(dws->txchan);
  508. clear_bit(DW_SPI_TX_BUSY, &dws->dma_chan_busy);
  509. }
  510. if (test_bit(DW_SPI_RX_BUSY, &dws->dma_chan_busy)) {
  511. dmaengine_terminate_sync(dws->rxchan);
  512. clear_bit(DW_SPI_RX_BUSY, &dws->dma_chan_busy);
  513. }
  514. }
  515. static const struct dw_spi_dma_ops dw_spi_dma_mfld_ops = {
  516. .dma_init = dw_spi_dma_init_mfld,
  517. .dma_exit = dw_spi_dma_exit,
  518. .dma_setup = dw_spi_dma_setup,
  519. .can_dma = dw_spi_can_dma,
  520. .dma_transfer = dw_spi_dma_transfer,
  521. .dma_stop = dw_spi_dma_stop,
  522. };
  523. void dw_spi_dma_setup_mfld(struct dw_spi *dws)
  524. {
  525. dws->dma_ops = &dw_spi_dma_mfld_ops;
  526. }
  527. EXPORT_SYMBOL_NS_GPL(dw_spi_dma_setup_mfld, SPI_DW_CORE);
  528. static const struct dw_spi_dma_ops dw_spi_dma_generic_ops = {
  529. .dma_init = dw_spi_dma_init_generic,
  530. .dma_exit = dw_spi_dma_exit,
  531. .dma_setup = dw_spi_dma_setup,
  532. .can_dma = dw_spi_can_dma,
  533. .dma_transfer = dw_spi_dma_transfer,
  534. .dma_stop = dw_spi_dma_stop,
  535. };
  536. void dw_spi_dma_setup_generic(struct dw_spi *dws)
  537. {
  538. dws->dma_ops = &dw_spi_dma_generic_ops;
  539. }
  540. EXPORT_SYMBOL_NS_GPL(dw_spi_dma_setup_generic, SPI_DW_CORE);