spi-pxa2xx-dma.c 8.7 KB

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