blob: eb6b45cdb7efba77409fbcbd9c2a746d1dae170a [file] [log] [blame]
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +01001/*
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
13#include <linux/device.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000014#include <linux/dma-mapping.h>
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010015#include <linux/dmaengine.h>
16#include <linux/mfd/tmio.h>
17#include <linux/mmc/host.h>
Simon Hormancba179a2011-03-24 09:48:36 +010018#include <linux/mmc/tmio.h>
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010019#include <linux/pagemap.h>
20#include <linux/scatterlist.h>
21
22#include "tmio_mmc.h"
23
24#define TMIO_MMC_MIN_DMA_LEN 8
25
Guennadi Liakhovetski162f43e2011-07-14 18:39:10 +020026void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010027{
Guennadi Liakhovetski162f43e2011-07-14 18:39:10 +020028 if (!host->chan_tx || !host->chan_rx)
29 return;
30
Kuninori Morimotoe85dd042014-08-24 20:01:54 -070031 if (host->pdata->flags & TMIO_MMC_HAVE_CTL_DMA_REG)
32 sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? 2 : 0);
Kuninori Morimoto5add2ac2015-01-13 04:59:05 +000033
34 if (host->dma->enable)
35 host->dma->enable(host, enable);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010036}
37
Guennadi Liakhovetskie3de2be2012-01-06 13:06:51 +010038void tmio_mmc_abort_dma(struct tmio_mmc_host *host)
39{
40 tmio_mmc_enable_dma(host, false);
41
42 if (host->chan_rx)
43 dmaengine_terminate_all(host->chan_rx);
44 if (host->chan_tx)
45 dmaengine_terminate_all(host->chan_tx);
46
47 tmio_mmc_enable_dma(host, true);
48}
49
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010050static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
51{
52 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
53 struct dma_async_tx_descriptor *desc = NULL;
54 struct dma_chan *chan = host->chan_rx;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010055 dma_cookie_t cookie;
56 int ret, i;
57 bool aligned = true, multiple = true;
Kuninori Morimotoe471df02015-01-13 04:58:46 +000058 unsigned int align = (1 << host->pdata->alignment_shift) - 1;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010059
60 for_each_sg(sg, sg_tmp, host->sg_len, i) {
61 if (sg_tmp->offset & align)
62 aligned = false;
63 if (sg_tmp->length & align) {
64 multiple = false;
65 break;
66 }
67 }
68
69 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
70 (align & PAGE_MASK))) || !multiple) {
71 ret = -EINVAL;
72 goto pio;
73 }
74
75 if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
76 host->force_pio = true;
77 return;
78 }
79
80 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_RXRDY);
81
82 /* The only sg element can be unaligned, use our bounce buffer then */
83 if (!aligned) {
84 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
85 host->sg_ptr = &host->bounce_sg;
86 sg = host->sg_ptr;
87 }
88
89 ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
90 if (ret > 0)
Alexandre Bounine16052822012-03-08 16:11:18 -050091 desc = dmaengine_prep_slave_sg(chan, sg, ret,
Vinod Koul05f57992011-10-14 10:45:11 +053092 DMA_DEV_TO_MEM, DMA_CTRL_ACK);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010093
94 if (desc) {
95 cookie = dmaengine_submit(desc);
96 if (cookie < 0) {
97 desc = NULL;
98 ret = cookie;
99 }
100 }
101 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
102 __func__, host->sg_len, ret, cookie, host->mrq);
103
104pio:
105 if (!desc) {
106 /* DMA failed, fall back to PIO */
Sergei Shtylyovf936f9b2013-08-24 23:38:15 -0400107 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100108 if (ret >= 0)
109 ret = -EIO;
110 host->chan_rx = NULL;
111 dma_release_channel(chan);
112 /* Free the Tx channel too */
113 chan = host->chan_tx;
114 if (chan) {
115 host->chan_tx = NULL;
116 dma_release_channel(chan);
117 }
118 dev_warn(&host->pdev->dev,
119 "DMA failed: %d, falling back to PIO\n", ret);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100120 }
121
122 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
123 desc, cookie, host->sg_len);
124}
125
126static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
127{
128 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
129 struct dma_async_tx_descriptor *desc = NULL;
130 struct dma_chan *chan = host->chan_tx;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100131 dma_cookie_t cookie;
132 int ret, i;
133 bool aligned = true, multiple = true;
Kuninori Morimotoe471df02015-01-13 04:58:46 +0000134 unsigned int align = (1 << host->pdata->alignment_shift) - 1;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100135
136 for_each_sg(sg, sg_tmp, host->sg_len, i) {
137 if (sg_tmp->offset & align)
138 aligned = false;
139 if (sg_tmp->length & align) {
140 multiple = false;
141 break;
142 }
143 }
144
145 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
146 (align & PAGE_MASK))) || !multiple) {
147 ret = -EINVAL;
148 goto pio;
149 }
150
151 if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
152 host->force_pio = true;
153 return;
154 }
155
156 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_TXRQ);
157
158 /* The only sg element can be unaligned, use our bounce buffer then */
159 if (!aligned) {
160 unsigned long flags;
161 void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
162 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
163 memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
164 tmio_mmc_kunmap_atomic(sg, &flags, sg_vaddr);
165 host->sg_ptr = &host->bounce_sg;
166 sg = host->sg_ptr;
167 }
168
169 ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
170 if (ret > 0)
Alexandre Bounine16052822012-03-08 16:11:18 -0500171 desc = dmaengine_prep_slave_sg(chan, sg, ret,
Vinod Koul05f57992011-10-14 10:45:11 +0530172 DMA_MEM_TO_DEV, DMA_CTRL_ACK);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100173
174 if (desc) {
175 cookie = dmaengine_submit(desc);
176 if (cookie < 0) {
177 desc = NULL;
178 ret = cookie;
179 }
180 }
181 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
182 __func__, host->sg_len, ret, cookie, host->mrq);
183
184pio:
185 if (!desc) {
186 /* DMA failed, fall back to PIO */
Sergei Shtylyovf936f9b2013-08-24 23:38:15 -0400187 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100188 if (ret >= 0)
189 ret = -EIO;
190 host->chan_tx = NULL;
191 dma_release_channel(chan);
192 /* Free the Rx channel too */
193 chan = host->chan_rx;
194 if (chan) {
195 host->chan_rx = NULL;
196 dma_release_channel(chan);
197 }
198 dev_warn(&host->pdev->dev,
199 "DMA failed: %d, falling back to PIO\n", ret);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100200 }
201
202 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
203 desc, cookie);
204}
205
206void tmio_mmc_start_dma(struct tmio_mmc_host *host,
207 struct mmc_data *data)
208{
209 if (data->flags & MMC_DATA_READ) {
210 if (host->chan_rx)
211 tmio_mmc_start_dma_rx(host);
212 } else {
213 if (host->chan_tx)
214 tmio_mmc_start_dma_tx(host);
215 }
216}
217
218static void tmio_mmc_issue_tasklet_fn(unsigned long priv)
219{
220 struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
221 struct dma_chan *chan = NULL;
222
223 spin_lock_irq(&host->lock);
224
225 if (host && host->data) {
226 if (host->data->flags & MMC_DATA_READ)
227 chan = host->chan_rx;
228 else
229 chan = host->chan_tx;
230 }
231
232 spin_unlock_irq(&host->lock);
233
234 tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
235
236 if (chan)
237 dma_async_issue_pending(chan);
238}
239
240static void tmio_mmc_tasklet_fn(unsigned long arg)
241{
242 struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
243
244 spin_lock_irq(&host->lock);
245
246 if (!host->data)
247 goto out;
248
249 if (host->data->flags & MMC_DATA_READ)
250 dma_unmap_sg(host->chan_rx->device->dev,
251 host->sg_ptr, host->sg_len,
252 DMA_FROM_DEVICE);
253 else
254 dma_unmap_sg(host->chan_tx->device->dev,
255 host->sg_ptr, host->sg_len,
256 DMA_TO_DEVICE);
257
258 tmio_mmc_do_data_irq(host);
259out:
260 spin_unlock_irq(&host->lock);
261}
262
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100263void tmio_mmc_request_dma(struct tmio_mmc_host *host, struct tmio_mmc_data *pdata)
264{
265 /* We can only either use DMA for both Tx and Rx or not use it at all */
Kuninori Morimoto7ecc09b2015-01-13 04:57:33 +0000266 if (!host->dma || (!host->pdev->dev.of_node &&
267 (!host->dma->chan_priv_tx || !host->dma->chan_priv_rx)))
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000268 return;
269
270 if (!host->chan_tx && !host->chan_rx) {
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200271 struct resource *res = platform_get_resource(host->pdev,
272 IORESOURCE_MEM, 0);
273 struct dma_slave_config cfg = {};
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100274 dma_cap_mask_t mask;
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200275 int ret;
276
277 if (!res)
278 return;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100279
280 dma_cap_zero(mask);
281 dma_cap_set(DMA_SLAVE, mask);
282
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200283 host->chan_tx = dma_request_slave_channel_compat(mask,
Kuninori Morimoto7ecc09b2015-01-13 04:57:33 +0000284 host->dma->filter, host->dma->chan_priv_tx,
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200285 &host->pdev->dev, "tx");
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100286 dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
287 host->chan_tx);
288
289 if (!host->chan_tx)
290 return;
291
Kuninori Morimoto7ecc09b2015-01-13 04:57:33 +0000292 if (host->dma->chan_priv_tx)
293 cfg.slave_id = host->dma->slave_id_tx;
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200294 cfg.direction = DMA_MEM_TO_DEV;
Kuninori Morimoto7445bf92015-01-13 04:58:20 +0000295 cfg.dst_addr = res->start + (CTL_SD_DATA_PORT << host->bus_shift);
Laurent Pinchart39ab1962014-07-16 00:45:14 +0200296 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200297 cfg.src_addr = 0;
298 ret = dmaengine_slave_config(host->chan_tx, &cfg);
299 if (ret < 0)
300 goto ecfgtx;
301
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200302 host->chan_rx = dma_request_slave_channel_compat(mask,
Kuninori Morimoto7ecc09b2015-01-13 04:57:33 +0000303 host->dma->filter, host->dma->chan_priv_rx,
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200304 &host->pdev->dev, "rx");
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100305 dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
306 host->chan_rx);
307
308 if (!host->chan_rx)
309 goto ereqrx;
310
Kuninori Morimoto7ecc09b2015-01-13 04:57:33 +0000311 if (host->dma->chan_priv_rx)
312 cfg.slave_id = host->dma->slave_id_rx;
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200313 cfg.direction = DMA_DEV_TO_MEM;
Kuninori Morimoto8b4c8f32015-01-13 04:58:56 +0000314 cfg.src_addr = cfg.dst_addr + host->pdata->dma_rx_offset;
Laurent Pinchart39ab1962014-07-16 00:45:14 +0200315 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200316 cfg.dst_addr = 0;
317 ret = dmaengine_slave_config(host->chan_rx, &cfg);
318 if (ret < 0)
319 goto ecfgrx;
320
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100321 host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
322 if (!host->bounce_buf)
323 goto ebouncebuf;
324
325 tasklet_init(&host->dma_complete, tmio_mmc_tasklet_fn, (unsigned long)host);
326 tasklet_init(&host->dma_issue, tmio_mmc_issue_tasklet_fn, (unsigned long)host);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100327 }
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000328
329 tmio_mmc_enable_dma(host, true);
330
331 return;
332
333ebouncebuf:
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200334ecfgrx:
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000335 dma_release_channel(host->chan_rx);
336 host->chan_rx = NULL;
337ereqrx:
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200338ecfgtx:
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000339 dma_release_channel(host->chan_tx);
340 host->chan_tx = NULL;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100341}
342
343void tmio_mmc_release_dma(struct tmio_mmc_host *host)
344{
345 if (host->chan_tx) {
346 struct dma_chan *chan = host->chan_tx;
347 host->chan_tx = NULL;
348 dma_release_channel(chan);
349 }
350 if (host->chan_rx) {
351 struct dma_chan *chan = host->chan_rx;
352 host->chan_rx = NULL;
353 dma_release_channel(chan);
354 }
355 if (host->bounce_buf) {
356 free_pages((unsigned long)host->bounce_buf, 0);
357 host->bounce_buf = NULL;
358 }
359}