tmio_mmc_dma.c 7.7 KB

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