tmio_mmc_dma.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 (host->dma->enable)
  27. host->dma->enable(host, enable);
  28. }
  29. void tmio_mmc_abort_dma(struct tmio_mmc_host *host)
  30. {
  31. tmio_mmc_enable_dma(host, false);
  32. if (host->chan_rx)
  33. dmaengine_terminate_all(host->chan_rx);
  34. if (host->chan_tx)
  35. dmaengine_terminate_all(host->chan_tx);
  36. tmio_mmc_enable_dma(host, true);
  37. }
  38. static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
  39. {
  40. struct scatterlist *sg = host->sg_ptr, *sg_tmp;
  41. struct dma_async_tx_descriptor *desc = NULL;
  42. struct dma_chan *chan = host->chan_rx;
  43. dma_cookie_t cookie;
  44. int ret, i;
  45. bool aligned = true, multiple = true;
  46. unsigned int align = (1 << host->pdata->alignment_shift) - 1;
  47. for_each_sg(sg, sg_tmp, host->sg_len, i) {
  48. if (sg_tmp->offset & align)
  49. aligned = false;
  50. if (sg_tmp->length & align) {
  51. multiple = false;
  52. break;
  53. }
  54. }
  55. if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
  56. (align & PAGE_MASK))) || !multiple) {
  57. ret = -EINVAL;
  58. goto pio;
  59. }
  60. if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
  61. host->force_pio = true;
  62. return;
  63. }
  64. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_RXRDY);
  65. /* The only sg element can be unaligned, use our bounce buffer then */
  66. if (!aligned) {
  67. sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
  68. host->sg_ptr = &host->bounce_sg;
  69. sg = host->sg_ptr;
  70. }
  71. ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
  72. if (ret > 0)
  73. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  74. DMA_DEV_TO_MEM, DMA_CTRL_ACK);
  75. if (desc) {
  76. cookie = dmaengine_submit(desc);
  77. if (cookie < 0) {
  78. desc = NULL;
  79. ret = cookie;
  80. }
  81. }
  82. pio:
  83. if (!desc) {
  84. /* DMA failed, fall back to PIO */
  85. tmio_mmc_enable_dma(host, false);
  86. if (ret >= 0)
  87. ret = -EIO;
  88. host->chan_rx = NULL;
  89. dma_release_channel(chan);
  90. /* Free the Tx channel too */
  91. chan = host->chan_tx;
  92. if (chan) {
  93. host->chan_tx = NULL;
  94. dma_release_channel(chan);
  95. }
  96. dev_warn(&host->pdev->dev,
  97. "DMA failed: %d, falling back to PIO\n", ret);
  98. }
  99. }
  100. static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
  101. {
  102. struct scatterlist *sg = host->sg_ptr, *sg_tmp;
  103. struct dma_async_tx_descriptor *desc = NULL;
  104. struct dma_chan *chan = host->chan_tx;
  105. dma_cookie_t cookie;
  106. int ret, i;
  107. bool aligned = true, multiple = true;
  108. unsigned int align = (1 << host->pdata->alignment_shift) - 1;
  109. for_each_sg(sg, sg_tmp, host->sg_len, i) {
  110. if (sg_tmp->offset & align)
  111. aligned = false;
  112. if (sg_tmp->length & align) {
  113. multiple = false;
  114. break;
  115. }
  116. }
  117. if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
  118. (align & PAGE_MASK))) || !multiple) {
  119. ret = -EINVAL;
  120. goto pio;
  121. }
  122. if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
  123. host->force_pio = true;
  124. return;
  125. }
  126. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_TXRQ);
  127. /* The only sg element can be unaligned, use our bounce buffer then */
  128. if (!aligned) {
  129. unsigned long flags;
  130. void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
  131. sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
  132. memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
  133. tmio_mmc_kunmap_atomic(sg, &flags, sg_vaddr);
  134. host->sg_ptr = &host->bounce_sg;
  135. sg = host->sg_ptr;
  136. }
  137. ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
  138. if (ret > 0)
  139. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  140. DMA_MEM_TO_DEV, DMA_CTRL_ACK);
  141. if (desc) {
  142. cookie = dmaengine_submit(desc);
  143. if (cookie < 0) {
  144. desc = NULL;
  145. ret = cookie;
  146. }
  147. }
  148. pio:
  149. if (!desc) {
  150. /* DMA failed, fall back to PIO */
  151. tmio_mmc_enable_dma(host, false);
  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. }
  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. void tmio_mmc_request_dma(struct tmio_mmc_host *host, struct tmio_mmc_data *pdata)
  212. {
  213. /* We can only either use DMA for both Tx and Rx or not use it at all */
  214. if (!host->dma || (!host->pdev->dev.of_node &&
  215. (!pdata->chan_priv_tx || !pdata->chan_priv_rx)))
  216. return;
  217. if (!host->chan_tx && !host->chan_rx) {
  218. struct resource *res = platform_get_resource(host->pdev,
  219. IORESOURCE_MEM, 0);
  220. struct dma_slave_config cfg = {};
  221. dma_cap_mask_t mask;
  222. int ret;
  223. if (!res)
  224. return;
  225. dma_cap_zero(mask);
  226. dma_cap_set(DMA_SLAVE, mask);
  227. host->chan_tx = dma_request_slave_channel_compat(mask,
  228. host->dma->filter, pdata->chan_priv_tx,
  229. &host->pdev->dev, "tx");
  230. dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
  231. host->chan_tx);
  232. if (!host->chan_tx)
  233. return;
  234. cfg.direction = DMA_MEM_TO_DEV;
  235. cfg.dst_addr = res->start + (CTL_SD_DATA_PORT << host->bus_shift);
  236. cfg.dst_addr_width = host->dma->dma_buswidth;
  237. if (!cfg.dst_addr_width)
  238. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  239. cfg.src_addr = 0;
  240. ret = dmaengine_slave_config(host->chan_tx, &cfg);
  241. if (ret < 0)
  242. goto ecfgtx;
  243. host->chan_rx = dma_request_slave_channel_compat(mask,
  244. host->dma->filter, pdata->chan_priv_rx,
  245. &host->pdev->dev, "rx");
  246. dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
  247. host->chan_rx);
  248. if (!host->chan_rx)
  249. goto ereqrx;
  250. cfg.direction = DMA_DEV_TO_MEM;
  251. cfg.src_addr = cfg.dst_addr + host->pdata->dma_rx_offset;
  252. cfg.src_addr_width = host->dma->dma_buswidth;
  253. if (!cfg.src_addr_width)
  254. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  255. cfg.dst_addr = 0;
  256. ret = dmaengine_slave_config(host->chan_rx, &cfg);
  257. if (ret < 0)
  258. goto ecfgrx;
  259. host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
  260. if (!host->bounce_buf)
  261. goto ebouncebuf;
  262. tasklet_init(&host->dma_complete, tmio_mmc_tasklet_fn, (unsigned long)host);
  263. tasklet_init(&host->dma_issue, tmio_mmc_issue_tasklet_fn, (unsigned long)host);
  264. }
  265. tmio_mmc_enable_dma(host, true);
  266. return;
  267. ebouncebuf:
  268. ecfgrx:
  269. dma_release_channel(host->chan_rx);
  270. host->chan_rx = NULL;
  271. ereqrx:
  272. ecfgtx:
  273. dma_release_channel(host->chan_tx);
  274. host->chan_tx = NULL;
  275. }
  276. void tmio_mmc_release_dma(struct tmio_mmc_host *host)
  277. {
  278. if (host->chan_tx) {
  279. struct dma_chan *chan = host->chan_tx;
  280. host->chan_tx = NULL;
  281. dma_release_channel(chan);
  282. }
  283. if (host->chan_rx) {
  284. struct dma_chan *chan = host->chan_rx;
  285. host->chan_rx = NULL;
  286. dma_release_channel(chan);
  287. }
  288. if (host->bounce_buf) {
  289. free_pages((unsigned long)host->bounce_buf, 0);
  290. host->bounce_buf = NULL;
  291. }
  292. }