]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/mmc/host/tmio_mmc_dma.c
mmc: tmio: ensure end of DMA and SD access are in sync
[karo-tx-linux.git] / drivers / mmc / host / tmio_mmc_dma.c
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
13 #include <linux/device.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/dmaengine.h>
16 #include <linux/mfd/tmio.h>
17 #include <linux/mmc/host.h>
18 #include <linux/pagemap.h>
19 #include <linux/scatterlist.h>
20
21 #include "tmio_mmc.h"
22
23 #define TMIO_MMC_MIN_DMA_LEN 8
24
25 void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
26 {
27         if (!host->chan_tx || !host->chan_rx)
28                 return;
29
30         if (host->dma->enable)
31                 host->dma->enable(host, enable);
32 }
33
34 void tmio_mmc_abort_dma(struct tmio_mmc_host *host)
35 {
36         tmio_mmc_enable_dma(host, false);
37
38         if (host->chan_rx)
39                 dmaengine_terminate_all(host->chan_rx);
40         if (host->chan_tx)
41                 dmaengine_terminate_all(host->chan_tx);
42
43         tmio_mmc_enable_dma(host, true);
44 }
45
46 static void tmio_mmc_dma_callback(void *arg)
47 {
48         struct tmio_mmc_host *host = arg;
49
50         wait_for_completion(&host->dma_dataend);
51
52         spin_lock_irq(&host->lock);
53
54         if (!host->data)
55                 goto out;
56
57         if (host->data->flags & MMC_DATA_READ)
58                 dma_unmap_sg(host->chan_rx->device->dev,
59                              host->sg_ptr, host->sg_len,
60                              DMA_FROM_DEVICE);
61         else
62                 dma_unmap_sg(host->chan_tx->device->dev,
63                              host->sg_ptr, host->sg_len,
64                              DMA_TO_DEVICE);
65
66         tmio_mmc_do_data_irq(host);
67 out:
68         spin_unlock_irq(&host->lock);
69 }
70
71 static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
72 {
73         struct scatterlist *sg = host->sg_ptr, *sg_tmp;
74         struct dma_async_tx_descriptor *desc = NULL;
75         struct dma_chan *chan = host->chan_rx;
76         dma_cookie_t cookie;
77         int ret, i;
78         bool aligned = true, multiple = true;
79         unsigned int align = (1 << host->pdata->alignment_shift) - 1;
80
81         for_each_sg(sg, sg_tmp, host->sg_len, i) {
82                 if (sg_tmp->offset & align)
83                         aligned = false;
84                 if (sg_tmp->length & align) {
85                         multiple = false;
86                         break;
87                 }
88         }
89
90         if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
91                           (align & PAGE_MASK))) || !multiple) {
92                 ret = -EINVAL;
93                 goto pio;
94         }
95
96         if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
97                 host->force_pio = true;
98                 return;
99         }
100
101         tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_RXRDY);
102
103         /* The only sg element can be unaligned, use our bounce buffer then */
104         if (!aligned) {
105                 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
106                 host->sg_ptr = &host->bounce_sg;
107                 sg = host->sg_ptr;
108         }
109
110         ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
111         if (ret > 0)
112                 desc = dmaengine_prep_slave_sg(chan, sg, ret,
113                         DMA_DEV_TO_MEM, DMA_CTRL_ACK);
114
115         if (desc) {
116                 reinit_completion(&host->dma_dataend);
117                 desc->callback = tmio_mmc_dma_callback;
118                 desc->callback_param = host;
119
120                 cookie = dmaengine_submit(desc);
121                 if (cookie < 0) {
122                         desc = NULL;
123                         ret = cookie;
124                 }
125         }
126 pio:
127         if (!desc) {
128                 /* DMA failed, fall back to PIO */
129                 tmio_mmc_enable_dma(host, false);
130                 if (ret >= 0)
131                         ret = -EIO;
132                 host->chan_rx = NULL;
133                 dma_release_channel(chan);
134                 /* Free the Tx channel too */
135                 chan = host->chan_tx;
136                 if (chan) {
137                         host->chan_tx = NULL;
138                         dma_release_channel(chan);
139                 }
140                 dev_warn(&host->pdev->dev,
141                          "DMA failed: %d, falling back to PIO\n", ret);
142         }
143 }
144
145 static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
146 {
147         struct scatterlist *sg = host->sg_ptr, *sg_tmp;
148         struct dma_async_tx_descriptor *desc = NULL;
149         struct dma_chan *chan = host->chan_tx;
150         dma_cookie_t cookie;
151         int ret, i;
152         bool aligned = true, multiple = true;
153         unsigned int align = (1 << host->pdata->alignment_shift) - 1;
154
155         for_each_sg(sg, sg_tmp, host->sg_len, i) {
156                 if (sg_tmp->offset & align)
157                         aligned = false;
158                 if (sg_tmp->length & align) {
159                         multiple = false;
160                         break;
161                 }
162         }
163
164         if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
165                           (align & PAGE_MASK))) || !multiple) {
166                 ret = -EINVAL;
167                 goto pio;
168         }
169
170         if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
171                 host->force_pio = true;
172                 return;
173         }
174
175         tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_TXRQ);
176
177         /* The only sg element can be unaligned, use our bounce buffer then */
178         if (!aligned) {
179                 unsigned long flags;
180                 void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
181                 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
182                 memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
183                 tmio_mmc_kunmap_atomic(sg, &flags, sg_vaddr);
184                 host->sg_ptr = &host->bounce_sg;
185                 sg = host->sg_ptr;
186         }
187
188         ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
189         if (ret > 0)
190                 desc = dmaengine_prep_slave_sg(chan, sg, ret,
191                         DMA_MEM_TO_DEV, DMA_CTRL_ACK);
192
193         if (desc) {
194                 reinit_completion(&host->dma_dataend);
195                 desc->callback = tmio_mmc_dma_callback;
196                 desc->callback_param = host;
197
198                 cookie = dmaengine_submit(desc);
199                 if (cookie < 0) {
200                         desc = NULL;
201                         ret = cookie;
202                 }
203         }
204 pio:
205         if (!desc) {
206                 /* DMA failed, fall back to PIO */
207                 tmio_mmc_enable_dma(host, false);
208                 if (ret >= 0)
209                         ret = -EIO;
210                 host->chan_tx = NULL;
211                 dma_release_channel(chan);
212                 /* Free the Rx channel too */
213                 chan = host->chan_rx;
214                 if (chan) {
215                         host->chan_rx = NULL;
216                         dma_release_channel(chan);
217                 }
218                 dev_warn(&host->pdev->dev,
219                          "DMA failed: %d, falling back to PIO\n", ret);
220         }
221 }
222
223 void tmio_mmc_start_dma(struct tmio_mmc_host *host,
224                                struct mmc_data *data)
225 {
226         if (data->flags & MMC_DATA_READ) {
227                 if (host->chan_rx)
228                         tmio_mmc_start_dma_rx(host);
229         } else {
230                 if (host->chan_tx)
231                         tmio_mmc_start_dma_tx(host);
232         }
233 }
234
235 static void tmio_mmc_issue_tasklet_fn(unsigned long priv)
236 {
237         struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
238         struct dma_chan *chan = NULL;
239
240         spin_lock_irq(&host->lock);
241
242         if (host && host->data) {
243                 if (host->data->flags & MMC_DATA_READ)
244                         chan = host->chan_rx;
245                 else
246                         chan = host->chan_tx;
247         }
248
249         spin_unlock_irq(&host->lock);
250
251         tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
252
253         if (chan)
254                 dma_async_issue_pending(chan);
255 }
256
257 void tmio_mmc_request_dma(struct tmio_mmc_host *host, struct tmio_mmc_data *pdata)
258 {
259         /* We can only either use DMA for both Tx and Rx or not use it at all */
260         if (!host->dma || (!host->pdev->dev.of_node &&
261                 (!pdata->chan_priv_tx || !pdata->chan_priv_rx)))
262                 return;
263
264         if (!host->chan_tx && !host->chan_rx) {
265                 struct resource *res = platform_get_resource(host->pdev,
266                                                              IORESOURCE_MEM, 0);
267                 struct dma_slave_config cfg = {};
268                 dma_cap_mask_t mask;
269                 int ret;
270
271                 if (!res)
272                         return;
273
274                 dma_cap_zero(mask);
275                 dma_cap_set(DMA_SLAVE, mask);
276
277                 host->chan_tx = dma_request_slave_channel_compat(mask,
278                                         host->dma->filter, pdata->chan_priv_tx,
279                                         &host->pdev->dev, "tx");
280                 dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
281                         host->chan_tx);
282
283                 if (!host->chan_tx)
284                         return;
285
286                 cfg.direction = DMA_MEM_TO_DEV;
287                 cfg.dst_addr = res->start + (CTL_SD_DATA_PORT << host->bus_shift);
288                 cfg.dst_addr_width = host->dma->dma_buswidth;
289                 if (!cfg.dst_addr_width)
290                         cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
291                 cfg.src_addr = 0;
292                 ret = dmaengine_slave_config(host->chan_tx, &cfg);
293                 if (ret < 0)
294                         goto ecfgtx;
295
296                 host->chan_rx = dma_request_slave_channel_compat(mask,
297                                         host->dma->filter, pdata->chan_priv_rx,
298                                         &host->pdev->dev, "rx");
299                 dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
300                         host->chan_rx);
301
302                 if (!host->chan_rx)
303                         goto ereqrx;
304
305                 cfg.direction = DMA_DEV_TO_MEM;
306                 cfg.src_addr = cfg.dst_addr + host->pdata->dma_rx_offset;
307                 cfg.src_addr_width = host->dma->dma_buswidth;
308                 if (!cfg.src_addr_width)
309                         cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
310                 cfg.dst_addr = 0;
311                 ret = dmaengine_slave_config(host->chan_rx, &cfg);
312                 if (ret < 0)
313                         goto ecfgrx;
314
315                 host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
316                 if (!host->bounce_buf)
317                         goto ebouncebuf;
318
319                 init_completion(&host->dma_dataend);
320                 tasklet_init(&host->dma_issue, tmio_mmc_issue_tasklet_fn, (unsigned long)host);
321         }
322
323         tmio_mmc_enable_dma(host, true);
324
325         return;
326
327 ebouncebuf:
328 ecfgrx:
329         dma_release_channel(host->chan_rx);
330         host->chan_rx = NULL;
331 ereqrx:
332 ecfgtx:
333         dma_release_channel(host->chan_tx);
334         host->chan_tx = NULL;
335 }
336
337 void tmio_mmc_release_dma(struct tmio_mmc_host *host)
338 {
339         if (host->chan_tx) {
340                 struct dma_chan *chan = host->chan_tx;
341                 host->chan_tx = NULL;
342                 dma_release_channel(chan);
343         }
344         if (host->chan_rx) {
345                 struct dma_chan *chan = host->chan_rx;
346                 host->chan_rx = NULL;
347                 dma_release_channel(chan);
348         }
349         if (host->bounce_buf) {
350                 free_pages((unsigned long)host->bounce_buf, 0);
351                 host->bounce_buf = NULL;
352         }
353 }