tmio_mmc_dma.c 8.4 KB

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