tmio_mmc_dma.c 7.8 KB

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