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