2 * linux/drivers/mmc/tmio_mmc_dma.c
4 * Copyright (C) 2010-2011 Guennadi Liakhovetski
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.
10 * DMA function for TMIO MMC implementations
13 #include <linux/device.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>
22 #define TMIO_MMC_MIN_DMA_LEN 8
24 static void tmio_mmc_enable_dma(struct tmio_mmc_host
*host
, bool enable
)
26 #if defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE)
27 /* Switch DMA mode on or off - SuperH specific? */
28 writew(enable
? 2 : 0, host
->ctl
+ (0xd8 << host
->bus_shift
));
32 static void tmio_mmc_start_dma_rx(struct tmio_mmc_host
*host
)
34 struct scatterlist
*sg
= host
->sg_ptr
, *sg_tmp
;
35 struct dma_async_tx_descriptor
*desc
= NULL
;
36 struct dma_chan
*chan
= host
->chan_rx
;
37 struct tmio_mmc_data
*pdata
= host
->pdata
;
40 bool aligned
= true, multiple
= true;
41 unsigned int align
= (1 << pdata
->dma
->alignment_shift
) - 1;
43 for_each_sg(sg
, sg_tmp
, host
->sg_len
, i
) {
44 if (sg_tmp
->offset
& align
)
46 if (sg_tmp
->length
& align
) {
52 if ((!aligned
&& (host
->sg_len
> 1 || sg
->length
> PAGE_CACHE_SIZE
||
53 (align
& PAGE_MASK
))) || !multiple
) {
58 if (sg
->length
< TMIO_MMC_MIN_DMA_LEN
) {
59 host
->force_pio
= true;
63 tmio_mmc_disable_mmc_irqs(host
, TMIO_STAT_RXRDY
);
65 /* The only sg element can be unaligned, use our bounce buffer then */
67 sg_init_one(&host
->bounce_sg
, host
->bounce_buf
, sg
->length
);
68 host
->sg_ptr
= &host
->bounce_sg
;
72 ret
= dma_map_sg(chan
->device
->dev
, sg
, host
->sg_len
, DMA_FROM_DEVICE
);
74 desc
= chan
->device
->device_prep_slave_sg(chan
, sg
, ret
,
75 DMA_FROM_DEVICE
, DMA_CTRL_ACK
);
78 cookie
= dmaengine_submit(desc
);
84 dev_dbg(&host
->pdev
->dev
, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
85 __func__
, host
->sg_len
, ret
, cookie
, host
->mrq
);
89 /* DMA failed, fall back to PIO */
93 dma_release_channel(chan
);
94 /* Free the Tx channel too */
98 dma_release_channel(chan
);
100 dev_warn(&host
->pdev
->dev
,
101 "DMA failed: %d, falling back to PIO\n", ret
);
102 tmio_mmc_enable_dma(host
, false);
105 dev_dbg(&host
->pdev
->dev
, "%s(): desc %p, cookie %d, sg[%d]\n", __func__
,
106 desc
, cookie
, host
->sg_len
);
109 static void tmio_mmc_start_dma_tx(struct tmio_mmc_host
*host
)
111 struct scatterlist
*sg
= host
->sg_ptr
, *sg_tmp
;
112 struct dma_async_tx_descriptor
*desc
= NULL
;
113 struct dma_chan
*chan
= host
->chan_tx
;
114 struct tmio_mmc_data
*pdata
= host
->pdata
;
117 bool aligned
= true, multiple
= true;
118 unsigned int align
= (1 << pdata
->dma
->alignment_shift
) - 1;
120 for_each_sg(sg
, sg_tmp
, host
->sg_len
, i
) {
121 if (sg_tmp
->offset
& align
)
123 if (sg_tmp
->length
& align
) {
129 if ((!aligned
&& (host
->sg_len
> 1 || sg
->length
> PAGE_CACHE_SIZE
||
130 (align
& PAGE_MASK
))) || !multiple
) {
135 if (sg
->length
< TMIO_MMC_MIN_DMA_LEN
) {
136 host
->force_pio
= true;
140 tmio_mmc_disable_mmc_irqs(host
, TMIO_STAT_TXRQ
);
142 /* The only sg element can be unaligned, use our bounce buffer then */
145 void *sg_vaddr
= tmio_mmc_kmap_atomic(sg
, &flags
);
146 sg_init_one(&host
->bounce_sg
, host
->bounce_buf
, sg
->length
);
147 memcpy(host
->bounce_buf
, sg_vaddr
, host
->bounce_sg
.length
);
148 tmio_mmc_kunmap_atomic(sg
, &flags
, sg_vaddr
);
149 host
->sg_ptr
= &host
->bounce_sg
;
153 ret
= dma_map_sg(chan
->device
->dev
, sg
, host
->sg_len
, DMA_TO_DEVICE
);
155 desc
= chan
->device
->device_prep_slave_sg(chan
, sg
, ret
,
156 DMA_TO_DEVICE
, DMA_CTRL_ACK
);
159 cookie
= dmaengine_submit(desc
);
165 dev_dbg(&host
->pdev
->dev
, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
166 __func__
, host
->sg_len
, ret
, cookie
, host
->mrq
);
170 /* DMA failed, fall back to PIO */
173 host
->chan_tx
= NULL
;
174 dma_release_channel(chan
);
175 /* Free the Rx channel too */
176 chan
= host
->chan_rx
;
178 host
->chan_rx
= NULL
;
179 dma_release_channel(chan
);
181 dev_warn(&host
->pdev
->dev
,
182 "DMA failed: %d, falling back to PIO\n", ret
);
183 tmio_mmc_enable_dma(host
, false);
186 dev_dbg(&host
->pdev
->dev
, "%s(): desc %p, cookie %d\n", __func__
,
190 void tmio_mmc_start_dma(struct tmio_mmc_host
*host
,
191 struct mmc_data
*data
)
193 if (data
->flags
& MMC_DATA_READ
) {
195 tmio_mmc_start_dma_rx(host
);
198 tmio_mmc_start_dma_tx(host
);
202 static void tmio_mmc_issue_tasklet_fn(unsigned long priv
)
204 struct tmio_mmc_host
*host
= (struct tmio_mmc_host
*)priv
;
205 struct dma_chan
*chan
= NULL
;
207 spin_lock_irq(&host
->lock
);
209 if (host
&& host
->data
) {
210 if (host
->data
->flags
& MMC_DATA_READ
)
211 chan
= host
->chan_rx
;
213 chan
= host
->chan_tx
;
216 spin_unlock_irq(&host
->lock
);
218 tmio_mmc_enable_mmc_irqs(host
, TMIO_STAT_DATAEND
);
221 dma_async_issue_pending(chan
);
224 static void tmio_mmc_tasklet_fn(unsigned long arg
)
226 struct tmio_mmc_host
*host
= (struct tmio_mmc_host
*)arg
;
228 spin_lock_irq(&host
->lock
);
233 if (host
->data
->flags
& MMC_DATA_READ
)
234 dma_unmap_sg(host
->chan_rx
->device
->dev
,
235 host
->sg_ptr
, host
->sg_len
,
238 dma_unmap_sg(host
->chan_tx
->device
->dev
,
239 host
->sg_ptr
, host
->sg_len
,
242 tmio_mmc_do_data_irq(host
);
244 spin_unlock_irq(&host
->lock
);
247 /* It might be necessary to make filter MFD specific */
248 static bool tmio_mmc_filter(struct dma_chan
*chan
, void *arg
)
250 dev_dbg(chan
->device
->dev
, "%s: slave data %p\n", __func__
, arg
);
255 void tmio_mmc_request_dma(struct tmio_mmc_host
*host
, struct tmio_mmc_data
*pdata
)
257 /* We can only either use DMA for both Tx and Rx or not use it at all */
262 dma_cap_set(DMA_SLAVE
, mask
);
264 host
->chan_tx
= dma_request_channel(mask
, tmio_mmc_filter
,
265 pdata
->dma
->chan_priv_tx
);
266 dev_dbg(&host
->pdev
->dev
, "%s: TX: got channel %p\n", __func__
,
272 host
->chan_rx
= dma_request_channel(mask
, tmio_mmc_filter
,
273 pdata
->dma
->chan_priv_rx
);
274 dev_dbg(&host
->pdev
->dev
, "%s: RX: got channel %p\n", __func__
,
280 host
->bounce_buf
= (u8
*)__get_free_page(GFP_KERNEL
| GFP_DMA
);
281 if (!host
->bounce_buf
)
284 tasklet_init(&host
->dma_complete
, tmio_mmc_tasklet_fn
, (unsigned long)host
);
285 tasklet_init(&host
->dma_issue
, tmio_mmc_issue_tasklet_fn
, (unsigned long)host
);
287 tmio_mmc_enable_dma(host
, true);
291 dma_release_channel(host
->chan_rx
);
292 host
->chan_rx
= NULL
;
294 dma_release_channel(host
->chan_tx
);
295 host
->chan_tx
= NULL
;
300 void tmio_mmc_release_dma(struct tmio_mmc_host
*host
)
303 struct dma_chan
*chan
= host
->chan_tx
;
304 host
->chan_tx
= NULL
;
305 dma_release_channel(chan
);
308 struct dma_chan
*chan
= host
->chan_rx
;
309 host
->chan_rx
= NULL
;
310 dma_release_channel(chan
);
312 if (host
->bounce_buf
) {
313 free_pages((unsigned long)host
->bounce_buf
, 0);
314 host
->bounce_buf
= NULL
;