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