tmio_mmc_dma.c 7.7 KB

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