spi-pxa2xx-dma.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * PXA2xx SPI DMA engine support.
  3. *
  4. * Copyright (C) 2013, Intel Corporation
  5. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/pxa2xx_ssp.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/sizes.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/spi/pxa2xx_spi.h>
  19. #include "spi-pxa2xx.h"
  20. static int pxa2xx_spi_map_dma_buffer(struct driver_data *drv_data,
  21. enum dma_data_direction dir)
  22. {
  23. int i, nents, len = drv_data->len;
  24. struct scatterlist *sg;
  25. struct device *dmadev;
  26. struct sg_table *sgt;
  27. void *buf, *pbuf;
  28. if (dir == DMA_TO_DEVICE) {
  29. dmadev = drv_data->tx_chan->device->dev;
  30. sgt = &drv_data->tx_sgt;
  31. buf = drv_data->tx;
  32. } else {
  33. dmadev = drv_data->rx_chan->device->dev;
  34. sgt = &drv_data->rx_sgt;
  35. buf = drv_data->rx;
  36. }
  37. nents = DIV_ROUND_UP(len, SZ_2K);
  38. if (nents != sgt->nents) {
  39. int ret;
  40. sg_free_table(sgt);
  41. ret = sg_alloc_table(sgt, nents, GFP_ATOMIC);
  42. if (ret)
  43. return ret;
  44. }
  45. pbuf = buf;
  46. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  47. size_t bytes = min_t(size_t, len, SZ_2K);
  48. sg_set_buf(sg, pbuf, bytes);
  49. pbuf += bytes;
  50. len -= bytes;
  51. }
  52. nents = dma_map_sg(dmadev, sgt->sgl, sgt->nents, dir);
  53. if (!nents)
  54. return -ENOMEM;
  55. return nents;
  56. }
  57. static void pxa2xx_spi_unmap_dma_buffer(struct driver_data *drv_data,
  58. enum dma_data_direction dir)
  59. {
  60. struct device *dmadev;
  61. struct sg_table *sgt;
  62. if (dir == DMA_TO_DEVICE) {
  63. dmadev = drv_data->tx_chan->device->dev;
  64. sgt = &drv_data->tx_sgt;
  65. } else {
  66. dmadev = drv_data->rx_chan->device->dev;
  67. sgt = &drv_data->rx_sgt;
  68. }
  69. dma_unmap_sg(dmadev, sgt->sgl, sgt->nents, dir);
  70. }
  71. static void pxa2xx_spi_unmap_dma_buffers(struct driver_data *drv_data)
  72. {
  73. if (!drv_data->dma_mapped)
  74. return;
  75. pxa2xx_spi_unmap_dma_buffer(drv_data, DMA_FROM_DEVICE);
  76. pxa2xx_spi_unmap_dma_buffer(drv_data, DMA_TO_DEVICE);
  77. drv_data->dma_mapped = 0;
  78. }
  79. static void pxa2xx_spi_dma_transfer_complete(struct driver_data *drv_data,
  80. bool error)
  81. {
  82. struct spi_message *msg = drv_data->cur_msg;
  83. /*
  84. * It is possible that one CPU is handling ROR interrupt and other
  85. * just gets DMA completion. Calling pump_transfers() twice for the
  86. * same transfer leads to problems thus we prevent concurrent calls
  87. * by using ->dma_running.
  88. */
  89. if (atomic_dec_and_test(&drv_data->dma_running)) {
  90. /*
  91. * If the other CPU is still handling the ROR interrupt we
  92. * might not know about the error yet. So we re-check the
  93. * ROR bit here before we clear the status register.
  94. */
  95. if (!error) {
  96. u32 status = pxa2xx_spi_read(drv_data, SSSR)
  97. & drv_data->mask_sr;
  98. error = status & SSSR_ROR;
  99. }
  100. /* Clear status & disable interrupts */
  101. pxa2xx_spi_write(drv_data, SSCR1,
  102. pxa2xx_spi_read(drv_data, SSCR1)
  103. & ~drv_data->dma_cr1);
  104. write_SSSR_CS(drv_data, drv_data->clear_sr);
  105. if (!pxa25x_ssp_comp(drv_data))
  106. pxa2xx_spi_write(drv_data, SSTO, 0);
  107. if (!error) {
  108. pxa2xx_spi_unmap_dma_buffers(drv_data);
  109. msg->actual_length += drv_data->len;
  110. msg->state = pxa2xx_spi_next_transfer(drv_data);
  111. } else {
  112. /* In case we got an error we disable the SSP now */
  113. pxa2xx_spi_write(drv_data, SSCR0,
  114. pxa2xx_spi_read(drv_data, SSCR0)
  115. & ~SSCR0_SSE);
  116. msg->state = ERROR_STATE;
  117. }
  118. tasklet_schedule(&drv_data->pump_transfers);
  119. }
  120. }
  121. static void pxa2xx_spi_dma_callback(void *data)
  122. {
  123. pxa2xx_spi_dma_transfer_complete(data, false);
  124. }
  125. static struct dma_async_tx_descriptor *
  126. pxa2xx_spi_dma_prepare_one(struct driver_data *drv_data,
  127. enum dma_transfer_direction dir)
  128. {
  129. struct chip_data *chip = drv_data->cur_chip;
  130. enum dma_slave_buswidth width;
  131. struct dma_slave_config cfg;
  132. struct dma_chan *chan;
  133. struct sg_table *sgt;
  134. int nents, ret;
  135. switch (drv_data->n_bytes) {
  136. case 1:
  137. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  138. break;
  139. case 2:
  140. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  141. break;
  142. default:
  143. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  144. break;
  145. }
  146. memset(&cfg, 0, sizeof(cfg));
  147. cfg.direction = dir;
  148. if (dir == DMA_MEM_TO_DEV) {
  149. cfg.dst_addr = drv_data->ssdr_physical;
  150. cfg.dst_addr_width = width;
  151. cfg.dst_maxburst = chip->dma_burst_size;
  152. sgt = &drv_data->tx_sgt;
  153. nents = drv_data->tx_nents;
  154. chan = drv_data->tx_chan;
  155. } else {
  156. cfg.src_addr = drv_data->ssdr_physical;
  157. cfg.src_addr_width = width;
  158. cfg.src_maxburst = chip->dma_burst_size;
  159. sgt = &drv_data->rx_sgt;
  160. nents = drv_data->rx_nents;
  161. chan = drv_data->rx_chan;
  162. }
  163. ret = dmaengine_slave_config(chan, &cfg);
  164. if (ret) {
  165. dev_warn(&drv_data->pdev->dev, "DMA slave config failed\n");
  166. return NULL;
  167. }
  168. return dmaengine_prep_slave_sg(chan, sgt->sgl, nents, dir,
  169. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  170. }
  171. bool pxa2xx_spi_dma_is_possible(size_t len)
  172. {
  173. return len <= MAX_DMA_LEN;
  174. }
  175. int pxa2xx_spi_map_dma_buffers(struct driver_data *drv_data)
  176. {
  177. const struct chip_data *chip = drv_data->cur_chip;
  178. int ret;
  179. if (!chip->enable_dma)
  180. return 0;
  181. /* Don't bother with DMA if we can't do even a single burst */
  182. if (drv_data->len < chip->dma_burst_size)
  183. return 0;
  184. ret = pxa2xx_spi_map_dma_buffer(drv_data, DMA_TO_DEVICE);
  185. if (ret <= 0) {
  186. dev_warn(&drv_data->pdev->dev, "failed to DMA map TX\n");
  187. return 0;
  188. }
  189. drv_data->tx_nents = ret;
  190. ret = pxa2xx_spi_map_dma_buffer(drv_data, DMA_FROM_DEVICE);
  191. if (ret <= 0) {
  192. pxa2xx_spi_unmap_dma_buffer(drv_data, DMA_TO_DEVICE);
  193. dev_warn(&drv_data->pdev->dev, "failed to DMA map RX\n");
  194. return 0;
  195. }
  196. drv_data->rx_nents = ret;
  197. return 1;
  198. }
  199. irqreturn_t pxa2xx_spi_dma_transfer(struct driver_data *drv_data)
  200. {
  201. u32 status;
  202. status = pxa2xx_spi_read(drv_data, SSSR) & drv_data->mask_sr;
  203. if (status & SSSR_ROR) {
  204. dev_err(&drv_data->pdev->dev, "FIFO overrun\n");
  205. dmaengine_terminate_async(drv_data->rx_chan);
  206. dmaengine_terminate_async(drv_data->tx_chan);
  207. pxa2xx_spi_dma_transfer_complete(drv_data, true);
  208. return IRQ_HANDLED;
  209. }
  210. return IRQ_NONE;
  211. }
  212. int pxa2xx_spi_dma_prepare(struct driver_data *drv_data, u32 dma_burst)
  213. {
  214. struct dma_async_tx_descriptor *tx_desc, *rx_desc;
  215. int err = 0;
  216. tx_desc = pxa2xx_spi_dma_prepare_one(drv_data, DMA_MEM_TO_DEV);
  217. if (!tx_desc) {
  218. dev_err(&drv_data->pdev->dev,
  219. "failed to get DMA TX descriptor\n");
  220. err = -EBUSY;
  221. goto err_tx;
  222. }
  223. rx_desc = pxa2xx_spi_dma_prepare_one(drv_data, DMA_DEV_TO_MEM);
  224. if (!rx_desc) {
  225. dev_err(&drv_data->pdev->dev,
  226. "failed to get DMA RX descriptor\n");
  227. err = -EBUSY;
  228. goto err_rx;
  229. }
  230. /* We are ready when RX completes */
  231. rx_desc->callback = pxa2xx_spi_dma_callback;
  232. rx_desc->callback_param = drv_data;
  233. dmaengine_submit(rx_desc);
  234. dmaengine_submit(tx_desc);
  235. return 0;
  236. err_rx:
  237. dmaengine_terminate_async(drv_data->tx_chan);
  238. err_tx:
  239. pxa2xx_spi_unmap_dma_buffers(drv_data);
  240. return err;
  241. }
  242. void pxa2xx_spi_dma_start(struct driver_data *drv_data)
  243. {
  244. dma_async_issue_pending(drv_data->rx_chan);
  245. dma_async_issue_pending(drv_data->tx_chan);
  246. atomic_set(&drv_data->dma_running, 1);
  247. }
  248. int pxa2xx_spi_dma_setup(struct driver_data *drv_data)
  249. {
  250. struct pxa2xx_spi_master *pdata = drv_data->master_info;
  251. struct device *dev = &drv_data->pdev->dev;
  252. dma_cap_mask_t mask;
  253. dma_cap_zero(mask);
  254. dma_cap_set(DMA_SLAVE, mask);
  255. drv_data->tx_chan = dma_request_slave_channel_compat(mask,
  256. pdata->dma_filter, pdata->tx_param, dev, "tx");
  257. if (!drv_data->tx_chan)
  258. return -ENODEV;
  259. drv_data->rx_chan = dma_request_slave_channel_compat(mask,
  260. pdata->dma_filter, pdata->rx_param, dev, "rx");
  261. if (!drv_data->rx_chan) {
  262. dma_release_channel(drv_data->tx_chan);
  263. drv_data->tx_chan = NULL;
  264. return -ENODEV;
  265. }
  266. return 0;
  267. }
  268. void pxa2xx_spi_dma_release(struct driver_data *drv_data)
  269. {
  270. if (drv_data->rx_chan) {
  271. dmaengine_terminate_sync(drv_data->rx_chan);
  272. dma_release_channel(drv_data->rx_chan);
  273. sg_free_table(&drv_data->rx_sgt);
  274. drv_data->rx_chan = NULL;
  275. }
  276. if (drv_data->tx_chan) {
  277. dmaengine_terminate_sync(drv_data->tx_chan);
  278. dma_release_channel(drv_data->tx_chan);
  279. sg_free_table(&drv_data->tx_sgt);
  280. drv_data->tx_chan = NULL;
  281. }
  282. }
  283. int pxa2xx_spi_set_dma_burst_and_threshold(struct chip_data *chip,
  284. struct spi_device *spi,
  285. u8 bits_per_word, u32 *burst_code,
  286. u32 *threshold)
  287. {
  288. struct pxa2xx_spi_chip *chip_info = spi->controller_data;
  289. /*
  290. * If the DMA burst size is given in chip_info we use that,
  291. * otherwise we use the default. Also we use the default FIFO
  292. * thresholds for now.
  293. */
  294. *burst_code = chip_info ? chip_info->dma_burst_size : 1;
  295. *threshold = SSCR1_RxTresh(RX_THRESH_DFLT)
  296. | SSCR1_TxTresh(TX_THRESH_DFLT);
  297. return 0;
  298. }