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