spi-pxa2xx-dma.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. void __iomem *reg = drv_data->ioaddr;
  96. /*
  97. * If the other CPU is still handling the ROR interrupt we
  98. * might not know about the error yet. So we re-check the
  99. * ROR bit here before we clear the status register.
  100. */
  101. if (!error) {
  102. u32 status = read_SSSR(reg) & drv_data->mask_sr;
  103. error = status & SSSR_ROR;
  104. }
  105. /* Clear status & disable interrupts */
  106. write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
  107. write_SSSR_CS(drv_data, drv_data->clear_sr);
  108. if (!pxa25x_ssp_comp(drv_data))
  109. write_SSTO(0, reg);
  110. if (!error) {
  111. pxa2xx_spi_unmap_dma_buffers(drv_data);
  112. drv_data->tx += drv_data->tx_map_len;
  113. drv_data->rx += drv_data->rx_map_len;
  114. msg->actual_length += drv_data->len;
  115. msg->state = pxa2xx_spi_next_transfer(drv_data);
  116. } else {
  117. /* In case we got an error we disable the SSP now */
  118. write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
  119. msg->state = ERROR_STATE;
  120. }
  121. tasklet_schedule(&drv_data->pump_transfers);
  122. }
  123. }
  124. static void pxa2xx_spi_dma_callback(void *data)
  125. {
  126. pxa2xx_spi_dma_transfer_complete(data, false);
  127. }
  128. static struct dma_async_tx_descriptor *
  129. pxa2xx_spi_dma_prepare_one(struct driver_data *drv_data,
  130. enum dma_transfer_direction dir)
  131. {
  132. struct pxa2xx_spi_master *pdata = drv_data->master_info;
  133. struct chip_data *chip = drv_data->cur_chip;
  134. enum dma_slave_buswidth width;
  135. struct dma_slave_config cfg;
  136. struct dma_chan *chan;
  137. struct sg_table *sgt;
  138. int nents, ret;
  139. switch (drv_data->n_bytes) {
  140. case 1:
  141. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  142. break;
  143. case 2:
  144. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  145. break;
  146. default:
  147. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  148. break;
  149. }
  150. memset(&cfg, 0, sizeof(cfg));
  151. cfg.direction = dir;
  152. if (dir == DMA_MEM_TO_DEV) {
  153. cfg.dst_addr = drv_data->ssdr_physical;
  154. cfg.dst_addr_width = width;
  155. cfg.dst_maxburst = chip->dma_burst_size;
  156. cfg.slave_id = pdata->tx_slave_id;
  157. sgt = &drv_data->tx_sgt;
  158. nents = drv_data->tx_nents;
  159. chan = drv_data->tx_chan;
  160. } else {
  161. cfg.src_addr = drv_data->ssdr_physical;
  162. cfg.src_addr_width = width;
  163. cfg.src_maxburst = chip->dma_burst_size;
  164. cfg.slave_id = pdata->rx_slave_id;
  165. sgt = &drv_data->rx_sgt;
  166. nents = drv_data->rx_nents;
  167. chan = drv_data->rx_chan;
  168. }
  169. ret = dmaengine_slave_config(chan, &cfg);
  170. if (ret) {
  171. dev_warn(&drv_data->pdev->dev, "DMA slave config failed\n");
  172. return NULL;
  173. }
  174. return dmaengine_prep_slave_sg(chan, sgt->sgl, nents, dir,
  175. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  176. }
  177. static bool pxa2xx_spi_dma_filter(struct dma_chan *chan, void *param)
  178. {
  179. const struct pxa2xx_spi_master *pdata = param;
  180. return chan->chan_id == pdata->tx_chan_id ||
  181. chan->chan_id == pdata->rx_chan_id;
  182. }
  183. bool pxa2xx_spi_dma_is_possible(size_t len)
  184. {
  185. return len <= MAX_DMA_LEN;
  186. }
  187. int pxa2xx_spi_map_dma_buffers(struct driver_data *drv_data)
  188. {
  189. const struct chip_data *chip = drv_data->cur_chip;
  190. int ret;
  191. if (!chip->enable_dma)
  192. return 0;
  193. /* Don't bother with DMA if we can't do even a single burst */
  194. if (drv_data->len < chip->dma_burst_size)
  195. return 0;
  196. ret = pxa2xx_spi_map_dma_buffer(drv_data, DMA_TO_DEVICE);
  197. if (ret <= 0) {
  198. dev_warn(&drv_data->pdev->dev, "failed to DMA map TX\n");
  199. return 0;
  200. }
  201. drv_data->tx_nents = ret;
  202. ret = pxa2xx_spi_map_dma_buffer(drv_data, DMA_FROM_DEVICE);
  203. if (ret <= 0) {
  204. pxa2xx_spi_unmap_dma_buffer(drv_data, DMA_TO_DEVICE);
  205. dev_warn(&drv_data->pdev->dev, "failed to DMA map RX\n");
  206. return 0;
  207. }
  208. drv_data->rx_nents = ret;
  209. return 1;
  210. }
  211. irqreturn_t pxa2xx_spi_dma_transfer(struct driver_data *drv_data)
  212. {
  213. u32 status;
  214. status = read_SSSR(drv_data->ioaddr) & drv_data->mask_sr;
  215. if (status & SSSR_ROR) {
  216. dev_err(&drv_data->pdev->dev, "FIFO overrun\n");
  217. dmaengine_terminate_all(drv_data->rx_chan);
  218. dmaengine_terminate_all(drv_data->tx_chan);
  219. pxa2xx_spi_dma_transfer_complete(drv_data, true);
  220. return IRQ_HANDLED;
  221. }
  222. return IRQ_NONE;
  223. }
  224. int pxa2xx_spi_dma_prepare(struct driver_data *drv_data, u32 dma_burst)
  225. {
  226. struct dma_async_tx_descriptor *tx_desc, *rx_desc;
  227. tx_desc = pxa2xx_spi_dma_prepare_one(drv_data, DMA_MEM_TO_DEV);
  228. if (!tx_desc) {
  229. dev_err(&drv_data->pdev->dev,
  230. "failed to get DMA TX descriptor\n");
  231. return -EBUSY;
  232. }
  233. rx_desc = pxa2xx_spi_dma_prepare_one(drv_data, DMA_DEV_TO_MEM);
  234. if (!rx_desc) {
  235. dev_err(&drv_data->pdev->dev,
  236. "failed to get DMA RX descriptor\n");
  237. return -EBUSY;
  238. }
  239. /* We are ready when RX completes */
  240. rx_desc->callback = pxa2xx_spi_dma_callback;
  241. rx_desc->callback_param = drv_data;
  242. dmaengine_submit(rx_desc);
  243. dmaengine_submit(tx_desc);
  244. return 0;
  245. }
  246. void pxa2xx_spi_dma_start(struct driver_data *drv_data)
  247. {
  248. dma_async_issue_pending(drv_data->rx_chan);
  249. dma_async_issue_pending(drv_data->tx_chan);
  250. atomic_set(&drv_data->dma_running, 1);
  251. }
  252. int pxa2xx_spi_dma_setup(struct driver_data *drv_data)
  253. {
  254. struct pxa2xx_spi_master *pdata = drv_data->master_info;
  255. struct device *dev = &drv_data->pdev->dev;
  256. dma_cap_mask_t mask;
  257. dma_cap_zero(mask);
  258. dma_cap_set(DMA_SLAVE, mask);
  259. drv_data->dummy = devm_kzalloc(dev, SZ_2K, GFP_KERNEL);
  260. if (!drv_data->dummy)
  261. return -ENOMEM;
  262. drv_data->tx_chan = dma_request_slave_channel_compat(mask,
  263. pxa2xx_spi_dma_filter, pdata, dev, "tx");
  264. if (!drv_data->tx_chan)
  265. return -ENODEV;
  266. drv_data->rx_chan = dma_request_slave_channel_compat(mask,
  267. pxa2xx_spi_dma_filter, pdata, dev, "rx");
  268. if (!drv_data->rx_chan) {
  269. dma_release_channel(drv_data->tx_chan);
  270. drv_data->tx_chan = NULL;
  271. return -ENODEV;
  272. }
  273. return 0;
  274. }
  275. void pxa2xx_spi_dma_release(struct driver_data *drv_data)
  276. {
  277. if (drv_data->rx_chan) {
  278. dmaengine_terminate_all(drv_data->rx_chan);
  279. dma_release_channel(drv_data->rx_chan);
  280. sg_free_table(&drv_data->rx_sgt);
  281. drv_data->rx_chan = NULL;
  282. }
  283. if (drv_data->tx_chan) {
  284. dmaengine_terminate_all(drv_data->tx_chan);
  285. dma_release_channel(drv_data->tx_chan);
  286. sg_free_table(&drv_data->tx_sgt);
  287. drv_data->tx_chan = NULL;
  288. }
  289. }
  290. void pxa2xx_spi_dma_resume(struct driver_data *drv_data)
  291. {
  292. }
  293. int pxa2xx_spi_set_dma_burst_and_threshold(struct chip_data *chip,
  294. struct spi_device *spi,
  295. u8 bits_per_word, u32 *burst_code,
  296. u32 *threshold)
  297. {
  298. struct pxa2xx_spi_chip *chip_info = spi->controller_data;
  299. /*
  300. * If the DMA burst size is given in chip_info we use that,
  301. * otherwise we use the default. Also we use the default FIFO
  302. * thresholds for now.
  303. */
  304. *burst_code = chip_info ? chip_info->dma_burst_size : 1;
  305. *threshold = SSCR1_RxTresh(RX_THRESH_DFLT)
  306. | SSCR1_TxTresh(TX_THRESH_DFLT);
  307. return 0;
  308. }