spi-pxa2xx-dma.c 9.5 KB

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