tmio_mmc_dma.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE)
  27. /* Switch DMA mode on or off - SuperH specific? */
  28. sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? 2 : 0);
  29. #endif
  30. }
  31. void tmio_mmc_abort_dma(struct tmio_mmc_host *host)
  32. {
  33. tmio_mmc_enable_dma(host, false);
  34. if (host->chan_rx)
  35. dmaengine_terminate_all(host->chan_rx);
  36. if (host->chan_tx)
  37. dmaengine_terminate_all(host->chan_tx);
  38. tmio_mmc_enable_dma(host, true);
  39. }
  40. static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
  41. {
  42. struct scatterlist *sg = host->sg_ptr, *sg_tmp;
  43. struct dma_async_tx_descriptor *desc = NULL;
  44. struct dma_chan *chan = host->chan_rx;
  45. struct tmio_mmc_data *pdata = host->pdata;
  46. dma_cookie_t cookie;
  47. int ret, i;
  48. bool aligned = true, multiple = true;
  49. unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
  50. for_each_sg(sg, sg_tmp, host->sg_len, i) {
  51. if (sg_tmp->offset & align)
  52. aligned = false;
  53. if (sg_tmp->length & align) {
  54. multiple = false;
  55. break;
  56. }
  57. }
  58. if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
  59. (align & PAGE_MASK))) || !multiple) {
  60. ret = -EINVAL;
  61. goto pio;
  62. }
  63. if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
  64. host->force_pio = true;
  65. return;
  66. }
  67. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_RXRDY);
  68. /* The only sg element can be unaligned, use our bounce buffer then */
  69. if (!aligned) {
  70. sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
  71. host->sg_ptr = &host->bounce_sg;
  72. sg = host->sg_ptr;
  73. }
  74. ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
  75. if (ret > 0)
  76. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  77. DMA_DEV_TO_MEM, DMA_CTRL_ACK);
  78. if (desc) {
  79. cookie = dmaengine_submit(desc);
  80. if (cookie < 0) {
  81. desc = NULL;
  82. ret = cookie;
  83. }
  84. }
  85. dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
  86. __func__, host->sg_len, ret, cookie, host->mrq);
  87. pio:
  88. if (!desc) {
  89. /* DMA failed, fall back to PIO */
  90. tmio_mmc_enable_dma(host, false);
  91. if (ret >= 0)
  92. ret = -EIO;
  93. host->chan_rx = NULL;
  94. dma_release_channel(chan);
  95. /* Free the Tx channel too */
  96. chan = host->chan_tx;
  97. if (chan) {
  98. host->chan_tx = NULL;
  99. dma_release_channel(chan);
  100. }
  101. dev_warn(&host->pdev->dev,
  102. "DMA failed: %d, falling back to PIO\n", ret);
  103. }
  104. dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
  105. desc, cookie, host->sg_len);
  106. }
  107. static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
  108. {
  109. struct scatterlist *sg = host->sg_ptr, *sg_tmp;
  110. struct dma_async_tx_descriptor *desc = NULL;
  111. struct dma_chan *chan = host->chan_tx;
  112. struct tmio_mmc_data *pdata = host->pdata;
  113. dma_cookie_t cookie;
  114. int ret, i;
  115. bool aligned = true, multiple = true;
  116. unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
  117. for_each_sg(sg, sg_tmp, host->sg_len, i) {
  118. if (sg_tmp->offset & align)
  119. aligned = false;
  120. if (sg_tmp->length & align) {
  121. multiple = false;
  122. break;
  123. }
  124. }
  125. if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
  126. (align & PAGE_MASK))) || !multiple) {
  127. ret = -EINVAL;
  128. goto pio;
  129. }
  130. if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
  131. host->force_pio = true;
  132. return;
  133. }
  134. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_TXRQ);
  135. /* The only sg element can be unaligned, use our bounce buffer then */
  136. if (!aligned) {
  137. unsigned long flags;
  138. void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
  139. sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
  140. memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
  141. tmio_mmc_kunmap_atomic(sg, &flags, sg_vaddr);
  142. host->sg_ptr = &host->bounce_sg;
  143. sg = host->sg_ptr;
  144. }
  145. ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
  146. if (ret > 0)
  147. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  148. DMA_MEM_TO_DEV, DMA_CTRL_ACK);
  149. if (desc) {
  150. cookie = dmaengine_submit(desc);
  151. if (cookie < 0) {
  152. desc = NULL;
  153. ret = cookie;
  154. }
  155. }
  156. dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
  157. __func__, host->sg_len, ret, cookie, host->mrq);
  158. pio:
  159. if (!desc) {
  160. /* DMA failed, fall back to PIO */
  161. tmio_mmc_enable_dma(host, false);
  162. if (ret >= 0)
  163. ret = -EIO;
  164. host->chan_tx = NULL;
  165. dma_release_channel(chan);
  166. /* Free the Rx channel too */
  167. chan = host->chan_rx;
  168. if (chan) {
  169. host->chan_rx = NULL;
  170. dma_release_channel(chan);
  171. }
  172. dev_warn(&host->pdev->dev,
  173. "DMA failed: %d, falling back to PIO\n", ret);
  174. }
  175. dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
  176. desc, cookie);
  177. }
  178. void tmio_mmc_start_dma(struct tmio_mmc_host *host,
  179. struct mmc_data *data)
  180. {
  181. if (data->flags & MMC_DATA_READ) {
  182. if (host->chan_rx)
  183. tmio_mmc_start_dma_rx(host);
  184. } else {
  185. if (host->chan_tx)
  186. tmio_mmc_start_dma_tx(host);
  187. }
  188. }
  189. static void tmio_mmc_issue_tasklet_fn(unsigned long priv)
  190. {
  191. struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
  192. struct dma_chan *chan = NULL;
  193. spin_lock_irq(&host->lock);
  194. if (host && host->data) {
  195. if (host->data->flags & MMC_DATA_READ)
  196. chan = host->chan_rx;
  197. else
  198. chan = host->chan_tx;
  199. }
  200. spin_unlock_irq(&host->lock);
  201. tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
  202. if (chan)
  203. dma_async_issue_pending(chan);
  204. }
  205. static void tmio_mmc_tasklet_fn(unsigned long arg)
  206. {
  207. struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
  208. spin_lock_irq(&host->lock);
  209. if (!host->data)
  210. goto out;
  211. if (host->data->flags & MMC_DATA_READ)
  212. dma_unmap_sg(host->chan_rx->device->dev,
  213. host->sg_ptr, host->sg_len,
  214. DMA_FROM_DEVICE);
  215. else
  216. dma_unmap_sg(host->chan_tx->device->dev,
  217. host->sg_ptr, host->sg_len,
  218. DMA_TO_DEVICE);
  219. tmio_mmc_do_data_irq(host);
  220. out:
  221. spin_unlock_irq(&host->lock);
  222. }
  223. /* It might be necessary to make filter MFD specific */
  224. static bool tmio_mmc_filter(struct dma_chan *chan, void *arg)
  225. {
  226. dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg);
  227. chan->private = arg;
  228. return true;
  229. }
  230. void tmio_mmc_request_dma(struct tmio_mmc_host *host, struct tmio_mmc_data *pdata)
  231. {
  232. /* We can only either use DMA for both Tx and Rx or not use it at all */
  233. if (!pdata->dma)
  234. return;
  235. if (!host->chan_tx && !host->chan_rx) {
  236. dma_cap_mask_t mask;
  237. dma_cap_zero(mask);
  238. dma_cap_set(DMA_SLAVE, mask);
  239. host->chan_tx = dma_request_channel(mask, tmio_mmc_filter,
  240. pdata->dma->chan_priv_tx);
  241. dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
  242. host->chan_tx);
  243. if (!host->chan_tx)
  244. return;
  245. host->chan_rx = dma_request_channel(mask, tmio_mmc_filter,
  246. pdata->dma->chan_priv_rx);
  247. dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
  248. host->chan_rx);
  249. if (!host->chan_rx)
  250. goto ereqrx;
  251. host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
  252. if (!host->bounce_buf)
  253. goto ebouncebuf;
  254. tasklet_init(&host->dma_complete, tmio_mmc_tasklet_fn, (unsigned long)host);
  255. tasklet_init(&host->dma_issue, tmio_mmc_issue_tasklet_fn, (unsigned long)host);
  256. }
  257. tmio_mmc_enable_dma(host, true);
  258. return;
  259. ebouncebuf:
  260. dma_release_channel(host->chan_rx);
  261. host->chan_rx = NULL;
  262. ereqrx:
  263. dma_release_channel(host->chan_tx);
  264. host->chan_tx = NULL;
  265. }
  266. void tmio_mmc_release_dma(struct tmio_mmc_host *host)
  267. {
  268. if (host->chan_tx) {
  269. struct dma_chan *chan = host->chan_tx;
  270. host->chan_tx = NULL;
  271. dma_release_channel(chan);
  272. }
  273. if (host->chan_rx) {
  274. struct dma_chan *chan = host->chan_rx;
  275. host->chan_rx = NULL;
  276. dma_release_channel(chan);
  277. }
  278. if (host->bounce_buf) {
  279. free_pages((unsigned long)host->bounce_buf, 0);
  280. host->bounce_buf = NULL;
  281. }
  282. }