blob: 7d077388b9eb48010bafd28a171addc461f163b6 [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);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010033}
34
Guennadi Liakhovetskie3de2be2012-01-06 13:06:51 +010035void tmio_mmc_abort_dma(struct tmio_mmc_host *host)
36{
37 tmio_mmc_enable_dma(host, false);
38
39 if (host->chan_rx)
40 dmaengine_terminate_all(host->chan_rx);
41 if (host->chan_tx)
42 dmaengine_terminate_all(host->chan_tx);
43
44 tmio_mmc_enable_dma(host, true);
45}
46
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010047static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
48{
49 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
50 struct dma_async_tx_descriptor *desc = NULL;
51 struct dma_chan *chan = host->chan_rx;
52 struct tmio_mmc_data *pdata = host->pdata;
53 dma_cookie_t cookie;
54 int ret, i;
55 bool aligned = true, multiple = true;
56 unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
57
58 for_each_sg(sg, sg_tmp, host->sg_len, i) {
59 if (sg_tmp->offset & align)
60 aligned = false;
61 if (sg_tmp->length & align) {
62 multiple = false;
63 break;
64 }
65 }
66
67 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
68 (align & PAGE_MASK))) || !multiple) {
69 ret = -EINVAL;
70 goto pio;
71 }
72
73 if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
74 host->force_pio = true;
75 return;
76 }
77
78 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_RXRDY);
79
80 /* The only sg element can be unaligned, use our bounce buffer then */
81 if (!aligned) {
82 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
83 host->sg_ptr = &host->bounce_sg;
84 sg = host->sg_ptr;
85 }
86
87 ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
88 if (ret > 0)
Alexandre Bounine16052822012-03-08 16:11:18 -050089 desc = dmaengine_prep_slave_sg(chan, sg, ret,
Vinod Koul05f57992011-10-14 10:45:11 +053090 DMA_DEV_TO_MEM, DMA_CTRL_ACK);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010091
92 if (desc) {
93 cookie = dmaengine_submit(desc);
94 if (cookie < 0) {
95 desc = NULL;
96 ret = cookie;
97 }
98 }
99 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
100 __func__, host->sg_len, ret, cookie, host->mrq);
101
102pio:
103 if (!desc) {
104 /* DMA failed, fall back to PIO */
Sergei Shtylyovf936f9b2013-08-24 23:38:15 -0400105 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100106 if (ret >= 0)
107 ret = -EIO;
108 host->chan_rx = NULL;
109 dma_release_channel(chan);
110 /* Free the Tx channel too */
111 chan = host->chan_tx;
112 if (chan) {
113 host->chan_tx = NULL;
114 dma_release_channel(chan);
115 }
116 dev_warn(&host->pdev->dev,
117 "DMA failed: %d, falling back to PIO\n", ret);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100118 }
119
120 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
121 desc, cookie, host->sg_len);
122}
123
124static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
125{
126 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
127 struct dma_async_tx_descriptor *desc = NULL;
128 struct dma_chan *chan = host->chan_tx;
129 struct tmio_mmc_data *pdata = host->pdata;
130 dma_cookie_t cookie;
131 int ret, i;
132 bool aligned = true, multiple = true;
133 unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
134
135 for_each_sg(sg, sg_tmp, host->sg_len, i) {
136 if (sg_tmp->offset & align)
137 aligned = false;
138 if (sg_tmp->length & align) {
139 multiple = false;
140 break;
141 }
142 }
143
144 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
145 (align & PAGE_MASK))) || !multiple) {
146 ret = -EINVAL;
147 goto pio;
148 }
149
150 if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
151 host->force_pio = true;
152 return;
153 }
154
155 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_TXRQ);
156
157 /* The only sg element can be unaligned, use our bounce buffer then */
158 if (!aligned) {
159 unsigned long flags;
160 void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
161 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
162 memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
163 tmio_mmc_kunmap_atomic(sg, &flags, sg_vaddr);
164 host->sg_ptr = &host->bounce_sg;
165 sg = host->sg_ptr;
166 }
167
168 ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
169 if (ret > 0)
Alexandre Bounine16052822012-03-08 16:11:18 -0500170 desc = dmaengine_prep_slave_sg(chan, sg, ret,
Vinod Koul05f57992011-10-14 10:45:11 +0530171 DMA_MEM_TO_DEV, DMA_CTRL_ACK);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100172
173 if (desc) {
174 cookie = dmaengine_submit(desc);
175 if (cookie < 0) {
176 desc = NULL;
177 ret = cookie;
178 }
179 }
180 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
181 __func__, host->sg_len, ret, cookie, host->mrq);
182
183pio:
184 if (!desc) {
185 /* DMA failed, fall back to PIO */
Sergei Shtylyovf936f9b2013-08-24 23:38:15 -0400186 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100187 if (ret >= 0)
188 ret = -EIO;
189 host->chan_tx = NULL;
190 dma_release_channel(chan);
191 /* Free the Rx channel too */
192 chan = host->chan_rx;
193 if (chan) {
194 host->chan_rx = NULL;
195 dma_release_channel(chan);
196 }
197 dev_warn(&host->pdev->dev,
198 "DMA failed: %d, falling back to PIO\n", ret);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100199 }
200
201 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
202 desc, cookie);
203}
204
205void tmio_mmc_start_dma(struct tmio_mmc_host *host,
206 struct mmc_data *data)
207{
208 if (data->flags & MMC_DATA_READ) {
209 if (host->chan_rx)
210 tmio_mmc_start_dma_rx(host);
211 } else {
212 if (host->chan_tx)
213 tmio_mmc_start_dma_tx(host);
214 }
215}
216
217static void tmio_mmc_issue_tasklet_fn(unsigned long priv)
218{
219 struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
220 struct dma_chan *chan = NULL;
221
222 spin_lock_irq(&host->lock);
223
224 if (host && host->data) {
225 if (host->data->flags & MMC_DATA_READ)
226 chan = host->chan_rx;
227 else
228 chan = host->chan_tx;
229 }
230
231 spin_unlock_irq(&host->lock);
232
233 tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
234
235 if (chan)
236 dma_async_issue_pending(chan);
237}
238
239static void tmio_mmc_tasklet_fn(unsigned long arg)
240{
241 struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
242
243 spin_lock_irq(&host->lock);
244
245 if (!host->data)
246 goto out;
247
248 if (host->data->flags & MMC_DATA_READ)
249 dma_unmap_sg(host->chan_rx->device->dev,
250 host->sg_ptr, host->sg_len,
251 DMA_FROM_DEVICE);
252 else
253 dma_unmap_sg(host->chan_tx->device->dev,
254 host->sg_ptr, host->sg_len,
255 DMA_TO_DEVICE);
256
257 tmio_mmc_do_data_irq(host);
258out:
259 spin_unlock_irq(&host->lock);
260}
261
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100262void tmio_mmc_request_dma(struct tmio_mmc_host *host, struct tmio_mmc_data *pdata)
263{
264 /* We can only either use DMA for both Tx and Rx or not use it at all */
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200265 if (!pdata->dma || (!host->pdev->dev.of_node &&
266 (!pdata->dma->chan_priv_tx || !pdata->dma->chan_priv_rx)))
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000267 return;
268
269 if (!host->chan_tx && !host->chan_rx) {
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200270 struct resource *res = platform_get_resource(host->pdev,
271 IORESOURCE_MEM, 0);
272 struct dma_slave_config cfg = {};
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100273 dma_cap_mask_t mask;
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200274 int ret;
275
276 if (!res)
277 return;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100278
279 dma_cap_zero(mask);
280 dma_cap_set(DMA_SLAVE, mask);
281
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200282 host->chan_tx = dma_request_slave_channel_compat(mask,
283 pdata->dma->filter, pdata->dma->chan_priv_tx,
284 &host->pdev->dev, "tx");
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100285 dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
286 host->chan_tx);
287
288 if (!host->chan_tx)
289 return;
290
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200291 if (pdata->dma->chan_priv_tx)
292 cfg.slave_id = pdata->dma->slave_id_tx;
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200293 cfg.direction = DMA_MEM_TO_DEV;
Kuninori Morimoto3b159a62013-11-20 00:30:55 -0800294 cfg.dst_addr = res->start + (CTL_SD_DATA_PORT << host->pdata->bus_shift);
Laurent Pinchart39ab1962014-07-16 00:45:14 +0200295 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200296 cfg.src_addr = 0;
297 ret = dmaengine_slave_config(host->chan_tx, &cfg);
298 if (ret < 0)
299 goto ecfgtx;
300
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200301 host->chan_rx = dma_request_slave_channel_compat(mask,
302 pdata->dma->filter, pdata->dma->chan_priv_rx,
303 &host->pdev->dev, "rx");
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100304 dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
305 host->chan_rx);
306
307 if (!host->chan_rx)
308 goto ereqrx;
309
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200310 if (pdata->dma->chan_priv_rx)
311 cfg.slave_id = pdata->dma->slave_id_rx;
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200312 cfg.direction = DMA_DEV_TO_MEM;
Kuninori Morimoto384b2cb2014-08-24 19:58:48 -0700313 cfg.src_addr = cfg.dst_addr + pdata->dma->dma_rx_offset;
Laurent Pinchart39ab1962014-07-16 00:45:14 +0200314 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200315 cfg.dst_addr = 0;
316 ret = dmaengine_slave_config(host->chan_rx, &cfg);
317 if (ret < 0)
318 goto ecfgrx;
319
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100320 host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
321 if (!host->bounce_buf)
322 goto ebouncebuf;
323
324 tasklet_init(&host->dma_complete, tmio_mmc_tasklet_fn, (unsigned long)host);
325 tasklet_init(&host->dma_issue, tmio_mmc_issue_tasklet_fn, (unsigned long)host);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100326 }
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000327
328 tmio_mmc_enable_dma(host, true);
329
330 return;
331
332ebouncebuf:
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200333ecfgrx:
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000334 dma_release_channel(host->chan_rx);
335 host->chan_rx = NULL;
336ereqrx:
Guennadi Liakhovetskieec95ee2013-04-26 17:47:18 +0200337ecfgtx:
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000338 dma_release_channel(host->chan_tx);
339 host->chan_tx = NULL;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100340}
341
342void tmio_mmc_release_dma(struct tmio_mmc_host *host)
343{
344 if (host->chan_tx) {
345 struct dma_chan *chan = host->chan_tx;
346 host->chan_tx = NULL;
347 dma_release_channel(chan);
348 }
349 if (host->chan_rx) {
350 struct dma_chan *chan = host->chan_rx;
351 host->chan_rx = NULL;
352 dma_release_channel(chan);
353 }
354 if (host->bounce_buf) {
355 free_pages((unsigned long)host->bounce_buf, 0);
356 host->bounce_buf = NULL;
357 }
358}