tmio_mmc_dma.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * linux/drivers/mmc/tmio_mmc_dma.c
  3. *
  4. * Copyright (C) 2010-2011 Guennadi Liakhovetski
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * DMA function for TMIO MMC implementations
  11. */
  12. #include <linux/device.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/mfd/tmio.h>
  16. #include <linux/mmc/host.h>
  17. #include <linux/mmc/tmio.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/scatterlist.h>
  20. #include "tmio_mmc.h"
  21. #define TMIO_MMC_MIN_DMA_LEN 8
  22. void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
  23. {
  24. if (!host->chan_tx || !host->chan_rx)
  25. return;
  26. if (host->pdata->flags & TMIO_MMC_HAVE_CTL_DMA_REG)
  27. sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? 2 : 0);
  28. }
  29. void tmio_mmc_abort_dma(struct tmio_mmc_host *host)
  30. {
  31. tmio_mmc_enable_dma(host, false);
  32. if (host->chan_rx)
  33. dmaengine_terminate_all(host->chan_rx);
  34. if (host->chan_tx)
  35. dmaengine_terminate_all(host->chan_tx);
  36. tmio_mmc_enable_dma(host, true);
  37. }
  38. static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
  39. {
  40. struct scatterlist *sg = host->sg_ptr, *sg_tmp;
  41. struct dma_async_tx_descriptor *desc = NULL;
  42. struct dma_chan *chan = host->chan_rx;
  43. struct tmio_mmc_data *pdata = host->pdata;
  44. dma_cookie_t cookie;
  45. int ret, i;
  46. bool aligned = true, multiple = true;
  47. unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
  48. for_each_sg(sg, sg_tmp, host->sg_len, i) {
  49. if (sg_tmp->offset & align)
  50. aligned = false;
  51. if (sg_tmp->length & align) {
  52. multiple = false;
  53. break;
  54. }
  55. }
  56. if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
  57. (align & PAGE_MASK))) || !multiple) {
  58. ret = -EINVAL;
  59. goto pio;
  60. }
  61. if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
  62. host->force_pio = true;
  63. return;
  64. }
  65. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_RXRDY);
  66. /* The only sg element can be unaligned, use our bounce buffer then */
  67. if (!aligned) {
  68. sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
  69. host->sg_ptr = &host->bounce_sg;
  70. sg = host->sg_ptr;
  71. }
  72. ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
  73. if (ret > 0)
  74. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  75. DMA_DEV_TO_MEM, DMA_CTRL_ACK);
  76. if (desc) {
  77. cookie = dmaengine_submit(desc);
  78. if (cookie < 0) {
  79. desc = NULL;
  80. ret = cookie;
  81. }
  82. }
  83. dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
  84. __func__, host->sg_len, ret, cookie, host->mrq);
  85. pio:
  86. if (!desc) {
  87. /* DMA failed, fall back to PIO */
  88. tmio_mmc_enable_dma(host, false);
  89. if (ret >= 0)
  90. ret = -EIO;
  91. host->chan_rx = NULL;
  92. dma_release_channel(chan);
  93. /* Free the Tx channel too */
  94. chan = host->chan_tx;
  95. if (chan) {
  96. host->chan_tx = NULL;
  97. dma_release_channel(chan);
  98. }
  99. dev_warn(&host->pdev->dev,
  100. "DMA failed: %d, falling back to PIO\n", ret);
  101. }
  102. dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
  103. desc, cookie, host->sg_len);
  104. }
  105. static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
  106. {
  107. struct scatterlist *sg = host->sg_ptr, *sg_tmp;
  108. struct dma_async_tx_descriptor *desc = NULL;
  109. struct dma_chan *chan = host->chan_tx;
  110. struct tmio_mmc_data *pdata = host->pdata;
  111. dma_cookie_t cookie;
  112. int ret, i;
  113. bool aligned = true, multiple = true;
  114. unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
  115. for_each_sg(sg, sg_tmp, host->sg_len, i) {
  116. if (sg_tmp->offset & align)
  117. aligned = false;
  118. if (sg_tmp->length & align) {
  119. multiple = false;
  120. break;
  121. }
  122. }
  123. if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
  124. (align & PAGE_MASK))) || !multiple) {
  125. ret = -EINVAL;
  126. goto pio;
  127. }
  128. if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
  129. host->force_pio = true;
  130. return;
  131. }
  132. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_TXRQ);
  133. /* The only sg element can be unaligned, use our bounce buffer then */
  134. if (!aligned) {
  135. unsigned long flags;
  136. void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
  137. sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
  138. memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
  139. tmio_mmc_kunmap_atomic(sg, &flags, sg_vaddr);
  140. host->sg_ptr = &host->bounce_sg;
  141. sg = host->sg_ptr;
  142. }
  143. ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
  144. if (ret > 0)
  145. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  146. DMA_MEM_TO_DEV, DMA_CTRL_ACK);
  147. if (desc) {
  148. cookie = dmaengine_submit(desc);
  149. if (cookie < 0) {
  150. desc = NULL;
  151. ret = cookie;
  152. }
  153. }
  154. dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
  155. __func__, host->sg_len, ret, cookie, host->mrq);
  156. pio:
  157. if (!desc) {
  158. /* DMA failed, fall back to PIO */
  159. tmio_mmc_enable_dma(host, false);
  160. if (ret >= 0)
  161. ret = -EIO;
  162. host->chan_tx = NULL;
  163. dma_release_channel(chan);
  164. /* Free the Rx channel too */
  165. chan = host->chan_rx;
  166. if (chan) {
  167. host->chan_rx = NULL;
  168. dma_release_channel(chan);
  169. }
  170. dev_warn(&host->pdev->dev,
  171. "DMA failed: %d, falling back to PIO\n", ret);
  172. }
  173. dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
  174. desc, cookie);
  175. }
  176. void tmio_mmc_start_dma(struct tmio_mmc_host *host,
  177. struct mmc_data *data)
  178. {
  179. if (data->flags & MMC_DATA_READ) {
  180. if (host->chan_rx)
  181. tmio_mmc_start_dma_rx(host);
  182. } else {
  183. if (host->chan_tx)
  184. tmio_mmc_start_dma_tx(host);
  185. }
  186. }
  187. static void tmio_mmc_issue_tasklet_fn(unsigned long priv)
  188. {
  189. struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
  190. struct dma_chan *chan = NULL;
  191. spin_lock_irq(&host->lock);
  192. if (host && host->data) {
  193. if (host->data->flags & MMC_DATA_READ)
  194. chan = host->chan_rx;
  195. else
  196. chan = host->chan_tx;
  197. }
  198. spin_unlock_irq(&host->lock);
  199. tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
  200. if (chan)
  201. dma_async_issue_pending(chan);
  202. }
  203. static void tmio_mmc_tasklet_fn(unsigned long arg)
  204. {
  205. struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
  206. spin_lock_irq(&host->lock);
  207. if (!host->data)
  208. goto out;
  209. if (host->data->flags & MMC_DATA_READ)
  210. dma_unmap_sg(host->chan_rx->device->dev,
  211. host->sg_ptr, host->sg_len,
  212. DMA_FROM_DEVICE);
  213. else
  214. dma_unmap_sg(host->chan_tx->device->dev,
  215. host->sg_ptr, host->sg_len,
  216. DMA_TO_DEVICE);
  217. tmio_mmc_do_data_irq(host);
  218. out:
  219. spin_unlock_irq(&host->lock);
  220. }
  221. void tmio_mmc_request_dma(struct tmio_mmc_host *host, struct tmio_mmc_data *pdata)
  222. {
  223. /* We can only either use DMA for both Tx and Rx or not use it at all */
  224. if (!pdata->dma || (!host->pdev->dev.of_node &&
  225. (!pdata->dma->chan_priv_tx || !pdata->dma->chan_priv_rx)))
  226. return;
  227. if (!host->chan_tx && !host->chan_rx) {
  228. struct resource *res = platform_get_resource(host->pdev,
  229. IORESOURCE_MEM, 0);
  230. struct dma_slave_config cfg = {};
  231. dma_cap_mask_t mask;
  232. int ret;
  233. if (!res)
  234. return;
  235. dma_cap_zero(mask);
  236. dma_cap_set(DMA_SLAVE, mask);
  237. host->chan_tx = dma_request_slave_channel_compat(mask,
  238. pdata->dma->filter, pdata->dma->chan_priv_tx,
  239. &host->pdev->dev, "tx");
  240. dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
  241. host->chan_tx);
  242. if (!host->chan_tx)
  243. return;
  244. if (pdata->dma->chan_priv_tx)
  245. cfg.slave_id = pdata->dma->slave_id_tx;
  246. cfg.direction = DMA_MEM_TO_DEV;
  247. cfg.dst_addr = res->start + (CTL_SD_DATA_PORT << host->pdata->bus_shift);
  248. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  249. cfg.src_addr = 0;
  250. ret = dmaengine_slave_config(host->chan_tx, &cfg);
  251. if (ret < 0)
  252. goto ecfgtx;
  253. host->chan_rx = dma_request_slave_channel_compat(mask,
  254. pdata->dma->filter, pdata->dma->chan_priv_rx,
  255. &host->pdev->dev, "rx");
  256. dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
  257. host->chan_rx);
  258. if (!host->chan_rx)
  259. goto ereqrx;
  260. if (pdata->dma->chan_priv_rx)
  261. cfg.slave_id = pdata->dma->slave_id_rx;
  262. cfg.direction = DMA_DEV_TO_MEM;
  263. cfg.src_addr = cfg.dst_addr + pdata->dma->dma_rx_offset;
  264. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  265. cfg.dst_addr = 0;
  266. ret = dmaengine_slave_config(host->chan_rx, &cfg);
  267. if (ret < 0)
  268. goto ecfgrx;
  269. host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
  270. if (!host->bounce_buf)
  271. goto ebouncebuf;
  272. tasklet_init(&host->dma_complete, tmio_mmc_tasklet_fn, (unsigned long)host);
  273. tasklet_init(&host->dma_issue, tmio_mmc_issue_tasklet_fn, (unsigned long)host);
  274. }
  275. tmio_mmc_enable_dma(host, true);
  276. return;
  277. ebouncebuf:
  278. ecfgrx:
  279. dma_release_channel(host->chan_rx);
  280. host->chan_rx = NULL;
  281. ereqrx:
  282. ecfgtx:
  283. dma_release_channel(host->chan_tx);
  284. host->chan_tx = NULL;
  285. }
  286. void tmio_mmc_release_dma(struct tmio_mmc_host *host)
  287. {
  288. if (host->chan_tx) {
  289. struct dma_chan *chan = host->chan_tx;
  290. host->chan_tx = NULL;
  291. dma_release_channel(chan);
  292. }
  293. if (host->chan_rx) {
  294. struct dma_chan *chan = host->chan_rx;
  295. host->chan_rx = NULL;
  296. dma_release_channel(chan);
  297. }
  298. if (host->bounce_buf) {
  299. free_pages((unsigned long)host->bounce_buf, 0);
  300. host->bounce_buf = NULL;
  301. }
  302. }