blob: 318a342fc7ececb73f32342cd70afb0f6ea7637d [file] [log] [blame]
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001/*
2 * Driver for the Synopsys DesignWare DMA Controller (aka DMACA on
3 * AVR32 systems.)
4 *
5 * Copyright (C) 2007-2008 Atmel Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/clk.h>
12#include <linux/delay.h>
13#include <linux/dmaengine.h>
14#include <linux/dma-mapping.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/mm.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22
23#include "dw_dmac_regs.h"
24
25/*
26 * This supports the Synopsys "DesignWare AHB Central DMA Controller",
27 * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
28 * of which use ARM any more). See the "Databook" from Synopsys for
29 * information beyond what licensees probably provide.
30 *
31 * The driver has currently been tested only with the Atmel AT32AP7000,
32 * which does not support descriptor writeback.
33 */
34
Jamie Ilesf301c062011-01-21 14:11:53 +000035#define DWC_DEFAULT_CTLLO(private) ({ \
36 struct dw_dma_slave *__slave = (private); \
37 int dms = __slave ? __slave->dst_master : 0; \
38 int sms = __slave ? __slave->src_master : 1; \
39 \
40 (DWC_CTLL_DST_MSIZE(0) \
41 | DWC_CTLL_SRC_MSIZE(0) \
42 | DWC_CTLL_LLP_D_EN \
43 | DWC_CTLL_LLP_S_EN \
44 | DWC_CTLL_DMS(dms) \
45 | DWC_CTLL_SMS(sms)); \
46 })
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070047
48/*
49 * This is configuration-dependent and usually a funny size like 4095.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070050 *
51 * Note that this is a transfer count, i.e. if we transfer 32-bit
Viresh Kumar418e7402011-03-04 15:42:50 +053052 * words, we can do 16380 bytes per descriptor.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070053 *
54 * This parameter is also system-specific.
55 */
Viresh Kumar418e7402011-03-04 15:42:50 +053056#define DWC_MAX_COUNT 4095U
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070057
58/*
59 * Number of descriptors to allocate for each channel. This should be
60 * made configurable somehow; preferably, the clients (at least the
61 * ones using slave transfers) should be able to give us a hint.
62 */
63#define NR_DESCS_PER_CHANNEL 64
64
65/*----------------------------------------------------------------------*/
66
67/*
68 * Because we're not relying on writeback from the controller (it may not
69 * even be configured into the core!) we don't need to use dma_pool. These
70 * descriptors -- and associated data -- are cacheable. We do need to make
71 * sure their dcache entries are written back before handing them off to
72 * the controller, though.
73 */
74
Dan Williams41d5e592009-01-06 11:38:21 -070075static struct device *chan2dev(struct dma_chan *chan)
76{
77 return &chan->dev->device;
78}
79static struct device *chan2parent(struct dma_chan *chan)
80{
81 return chan->dev->device.parent;
82}
83
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070084static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
85{
86 return list_entry(dwc->active_list.next, struct dw_desc, desc_node);
87}
88
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070089static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
90{
91 struct dw_desc *desc, *_desc;
92 struct dw_desc *ret = NULL;
93 unsigned int i = 0;
94
95 spin_lock_bh(&dwc->lock);
96 list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
97 if (async_tx_test_ack(&desc->txd)) {
98 list_del(&desc->desc_node);
99 ret = desc;
100 break;
101 }
Dan Williams41d5e592009-01-06 11:38:21 -0700102 dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700103 i++;
104 }
105 spin_unlock_bh(&dwc->lock);
106
Dan Williams41d5e592009-01-06 11:38:21 -0700107 dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700108
109 return ret;
110}
111
112static void dwc_sync_desc_for_cpu(struct dw_dma_chan *dwc, struct dw_desc *desc)
113{
114 struct dw_desc *child;
115
Dan Williamse0bd0f82009-09-08 17:53:02 -0700116 list_for_each_entry(child, &desc->tx_list, desc_node)
Dan Williams41d5e592009-01-06 11:38:21 -0700117 dma_sync_single_for_cpu(chan2parent(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700118 child->txd.phys, sizeof(child->lli),
119 DMA_TO_DEVICE);
Dan Williams41d5e592009-01-06 11:38:21 -0700120 dma_sync_single_for_cpu(chan2parent(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700121 desc->txd.phys, sizeof(desc->lli),
122 DMA_TO_DEVICE);
123}
124
125/*
126 * Move a descriptor, including any children, to the free list.
127 * `desc' must not be on any lists.
128 */
129static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
130{
131 if (desc) {
132 struct dw_desc *child;
133
134 dwc_sync_desc_for_cpu(dwc, desc);
135
136 spin_lock_bh(&dwc->lock);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700137 list_for_each_entry(child, &desc->tx_list, desc_node)
Dan Williams41d5e592009-01-06 11:38:21 -0700138 dev_vdbg(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700139 "moving child desc %p to freelist\n",
140 child);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700141 list_splice_init(&desc->tx_list, &dwc->free_list);
Dan Williams41d5e592009-01-06 11:38:21 -0700142 dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700143 list_add(&desc->desc_node, &dwc->free_list);
144 spin_unlock_bh(&dwc->lock);
145 }
146}
147
148/* Called with dwc->lock held and bh disabled */
149static dma_cookie_t
150dwc_assign_cookie(struct dw_dma_chan *dwc, struct dw_desc *desc)
151{
152 dma_cookie_t cookie = dwc->chan.cookie;
153
154 if (++cookie < 0)
155 cookie = 1;
156
157 dwc->chan.cookie = cookie;
158 desc->txd.cookie = cookie;
159
160 return cookie;
161}
162
163/*----------------------------------------------------------------------*/
164
165/* Called with dwc->lock held and bh disabled */
166static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
167{
168 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
169
170 /* ASSERT: channel is idle */
171 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700172 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700173 "BUG: Attempted to start non-idle channel\n");
Dan Williams41d5e592009-01-06 11:38:21 -0700174 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700175 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
176 channel_readl(dwc, SAR),
177 channel_readl(dwc, DAR),
178 channel_readl(dwc, LLP),
179 channel_readl(dwc, CTL_HI),
180 channel_readl(dwc, CTL_LO));
181
182 /* The tasklet will hopefully advance the queue... */
183 return;
184 }
185
186 channel_writel(dwc, LLP, first->txd.phys);
187 channel_writel(dwc, CTL_LO,
188 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
189 channel_writel(dwc, CTL_HI, 0);
190 channel_set_bit(dw, CH_EN, dwc->mask);
191}
192
193/*----------------------------------------------------------------------*/
194
195static void
196dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc)
197{
198 dma_async_tx_callback callback;
199 void *param;
200 struct dma_async_tx_descriptor *txd = &desc->txd;
Viresh Kumare5180762011-03-03 15:47:20 +0530201 struct dw_desc *child;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700202
Dan Williams41d5e592009-01-06 11:38:21 -0700203 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700204
205 dwc->completed = txd->cookie;
206 callback = txd->callback;
207 param = txd->callback_param;
208
209 dwc_sync_desc_for_cpu(dwc, desc);
Viresh Kumare5180762011-03-03 15:47:20 +0530210
211 /* async_tx_ack */
212 list_for_each_entry(child, &desc->tx_list, desc_node)
213 async_tx_ack(&child->txd);
214 async_tx_ack(&desc->txd);
215
Dan Williamse0bd0f82009-09-08 17:53:02 -0700216 list_splice_init(&desc->tx_list, &dwc->free_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700217 list_move(&desc->desc_node, &dwc->free_list);
218
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700219 if (!dwc->chan.private) {
220 struct device *parent = chan2parent(&dwc->chan);
221 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
222 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
223 dma_unmap_single(parent, desc->lli.dar,
224 desc->len, DMA_FROM_DEVICE);
225 else
226 dma_unmap_page(parent, desc->lli.dar,
227 desc->len, DMA_FROM_DEVICE);
228 }
229 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
230 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
231 dma_unmap_single(parent, desc->lli.sar,
232 desc->len, DMA_TO_DEVICE);
233 else
234 dma_unmap_page(parent, desc->lli.sar,
235 desc->len, DMA_TO_DEVICE);
236 }
237 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700238
239 /*
240 * The API requires that no submissions are done from a
241 * callback, so we don't need to drop the lock here
242 */
243 if (callback)
244 callback(param);
245}
246
247static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
248{
249 struct dw_desc *desc, *_desc;
250 LIST_HEAD(list);
251
252 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700253 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700254 "BUG: XFER bit set, but channel not idle!\n");
255
256 /* Try to continue after resetting the channel... */
257 channel_clear_bit(dw, CH_EN, dwc->mask);
258 while (dma_readl(dw, CH_EN) & dwc->mask)
259 cpu_relax();
260 }
261
262 /*
263 * Submit queued descriptors ASAP, i.e. before we go through
264 * the completed ones.
265 */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700266 list_splice_init(&dwc->active_list, &list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530267 if (!list_empty(&dwc->queue)) {
268 list_move(dwc->queue.next, &dwc->active_list);
269 dwc_dostart(dwc, dwc_first_active(dwc));
270 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700271
272 list_for_each_entry_safe(desc, _desc, &list, desc_node)
273 dwc_descriptor_complete(dwc, desc);
274}
275
276static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
277{
278 dma_addr_t llp;
279 struct dw_desc *desc, *_desc;
280 struct dw_desc *child;
281 u32 status_xfer;
282
283 /*
284 * Clear block interrupt flag before scanning so that we don't
285 * miss any, and read LLP before RAW_XFER to ensure it is
286 * valid if we decide to scan the list.
287 */
288 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
289 llp = channel_readl(dwc, LLP);
290 status_xfer = dma_readl(dw, RAW.XFER);
291
292 if (status_xfer & dwc->mask) {
293 /* Everything we've submitted is done */
294 dma_writel(dw, CLEAR.XFER, dwc->mask);
295 dwc_complete_all(dw, dwc);
296 return;
297 }
298
Jamie Iles087809f2011-01-21 14:11:52 +0000299 if (list_empty(&dwc->active_list))
300 return;
301
Dan Williams41d5e592009-01-06 11:38:21 -0700302 dev_vdbg(chan2dev(&dwc->chan), "scan_descriptors: llp=0x%x\n", llp);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700303
304 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
305 if (desc->lli.llp == llp)
306 /* This one is currently in progress */
307 return;
308
Dan Williamse0bd0f82009-09-08 17:53:02 -0700309 list_for_each_entry(child, &desc->tx_list, desc_node)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700310 if (child->lli.llp == llp)
311 /* Currently in progress */
312 return;
313
314 /*
315 * No descriptors so far seem to be in progress, i.e.
316 * this one must be done.
317 */
318 dwc_descriptor_complete(dwc, desc);
319 }
320
Dan Williams41d5e592009-01-06 11:38:21 -0700321 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700322 "BUG: All descriptors done, but channel not idle!\n");
323
324 /* Try to continue after resetting the channel... */
325 channel_clear_bit(dw, CH_EN, dwc->mask);
326 while (dma_readl(dw, CH_EN) & dwc->mask)
327 cpu_relax();
328
329 if (!list_empty(&dwc->queue)) {
Viresh Kumarf336e422011-03-03 15:47:16 +0530330 list_move(dwc->queue.next, &dwc->active_list);
331 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700332 }
333}
334
335static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
336{
Dan Williams41d5e592009-01-06 11:38:21 -0700337 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700338 " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
339 lli->sar, lli->dar, lli->llp,
340 lli->ctlhi, lli->ctllo);
341}
342
343static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
344{
345 struct dw_desc *bad_desc;
346 struct dw_desc *child;
347
348 dwc_scan_descriptors(dw, dwc);
349
350 /*
351 * The descriptor currently at the head of the active list is
352 * borked. Since we don't have any way to report errors, we'll
353 * just have to scream loudly and try to carry on.
354 */
355 bad_desc = dwc_first_active(dwc);
356 list_del_init(&bad_desc->desc_node);
Viresh Kumarf336e422011-03-03 15:47:16 +0530357 list_move(dwc->queue.next, dwc->active_list.prev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700358
359 /* Clear the error flag and try to restart the controller */
360 dma_writel(dw, CLEAR.ERROR, dwc->mask);
361 if (!list_empty(&dwc->active_list))
362 dwc_dostart(dwc, dwc_first_active(dwc));
363
364 /*
365 * KERN_CRITICAL may seem harsh, but since this only happens
366 * when someone submits a bad physical address in a
367 * descriptor, we should consider ourselves lucky that the
368 * controller flagged an error instead of scribbling over
369 * random memory locations.
370 */
Dan Williams41d5e592009-01-06 11:38:21 -0700371 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700372 "Bad descriptor submitted for DMA!\n");
Dan Williams41d5e592009-01-06 11:38:21 -0700373 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700374 " cookie: %d\n", bad_desc->txd.cookie);
375 dwc_dump_lli(dwc, &bad_desc->lli);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700376 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700377 dwc_dump_lli(dwc, &child->lli);
378
379 /* Pretend the descriptor completed successfully */
380 dwc_descriptor_complete(dwc, bad_desc);
381}
382
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200383/* --------------------- Cyclic DMA API extensions -------------------- */
384
385inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
386{
387 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
388 return channel_readl(dwc, SAR);
389}
390EXPORT_SYMBOL(dw_dma_get_src_addr);
391
392inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
393{
394 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
395 return channel_readl(dwc, DAR);
396}
397EXPORT_SYMBOL(dw_dma_get_dst_addr);
398
399/* called with dwc->lock held and all DMAC interrupts disabled */
400static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
401 u32 status_block, u32 status_err, u32 status_xfer)
402{
403 if (status_block & dwc->mask) {
404 void (*callback)(void *param);
405 void *callback_param;
406
407 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
408 channel_readl(dwc, LLP));
409 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
410
411 callback = dwc->cdesc->period_callback;
412 callback_param = dwc->cdesc->period_callback_param;
413 if (callback) {
414 spin_unlock(&dwc->lock);
415 callback(callback_param);
416 spin_lock(&dwc->lock);
417 }
418 }
419
420 /*
421 * Error and transfer complete are highly unlikely, and will most
422 * likely be due to a configuration error by the user.
423 */
424 if (unlikely(status_err & dwc->mask) ||
425 unlikely(status_xfer & dwc->mask)) {
426 int i;
427
428 dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
429 "interrupt, stopping DMA transfer\n",
430 status_xfer ? "xfer" : "error");
431 dev_err(chan2dev(&dwc->chan),
432 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
433 channel_readl(dwc, SAR),
434 channel_readl(dwc, DAR),
435 channel_readl(dwc, LLP),
436 channel_readl(dwc, CTL_HI),
437 channel_readl(dwc, CTL_LO));
438
439 channel_clear_bit(dw, CH_EN, dwc->mask);
440 while (dma_readl(dw, CH_EN) & dwc->mask)
441 cpu_relax();
442
443 /* make sure DMA does not restart by loading a new list */
444 channel_writel(dwc, LLP, 0);
445 channel_writel(dwc, CTL_LO, 0);
446 channel_writel(dwc, CTL_HI, 0);
447
448 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
449 dma_writel(dw, CLEAR.ERROR, dwc->mask);
450 dma_writel(dw, CLEAR.XFER, dwc->mask);
451
452 for (i = 0; i < dwc->cdesc->periods; i++)
453 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
454 }
455}
456
457/* ------------------------------------------------------------------------- */
458
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700459static void dw_dma_tasklet(unsigned long data)
460{
461 struct dw_dma *dw = (struct dw_dma *)data;
462 struct dw_dma_chan *dwc;
463 u32 status_block;
464 u32 status_xfer;
465 u32 status_err;
466 int i;
467
468 status_block = dma_readl(dw, RAW.BLOCK);
Haavard Skinnemoen7fe7b2f2008-10-03 15:23:46 -0700469 status_xfer = dma_readl(dw, RAW.XFER);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700470 status_err = dma_readl(dw, RAW.ERROR);
471
472 dev_vdbg(dw->dma.dev, "tasklet: status_block=%x status_err=%x\n",
473 status_block, status_err);
474
475 for (i = 0; i < dw->dma.chancnt; i++) {
476 dwc = &dw->chan[i];
477 spin_lock(&dwc->lock);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200478 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
479 dwc_handle_cyclic(dw, dwc, status_block, status_err,
480 status_xfer);
481 else if (status_err & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700482 dwc_handle_error(dw, dwc);
483 else if ((status_block | status_xfer) & (1 << i))
484 dwc_scan_descriptors(dw, dwc);
485 spin_unlock(&dwc->lock);
486 }
487
488 /*
489 * Re-enable interrupts. Block Complete interrupts are only
490 * enabled if the INT_EN bit in the descriptor is set. This
491 * will trigger a scan before the whole list is done.
492 */
493 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
494 channel_set_bit(dw, MASK.BLOCK, dw->all_chan_mask);
495 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
496}
497
498static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
499{
500 struct dw_dma *dw = dev_id;
501 u32 status;
502
503 dev_vdbg(dw->dma.dev, "interrupt: status=0x%x\n",
504 dma_readl(dw, STATUS_INT));
505
506 /*
507 * Just disable the interrupts. We'll turn them back on in the
508 * softirq handler.
509 */
510 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
511 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
512 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
513
514 status = dma_readl(dw, STATUS_INT);
515 if (status) {
516 dev_err(dw->dma.dev,
517 "BUG: Unexpected interrupts pending: 0x%x\n",
518 status);
519
520 /* Try to recover */
521 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
522 channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
523 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
524 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
525 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
526 }
527
528 tasklet_schedule(&dw->tasklet);
529
530 return IRQ_HANDLED;
531}
532
533/*----------------------------------------------------------------------*/
534
535static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
536{
537 struct dw_desc *desc = txd_to_dw_desc(tx);
538 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
539 dma_cookie_t cookie;
540
541 spin_lock_bh(&dwc->lock);
542 cookie = dwc_assign_cookie(dwc, desc);
543
544 /*
545 * REVISIT: We should attempt to chain as many descriptors as
546 * possible, perhaps even appending to those already submitted
547 * for DMA. But this is hard to do in a race-free manner.
548 */
549 if (list_empty(&dwc->active_list)) {
Dan Williams41d5e592009-01-06 11:38:21 -0700550 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700551 desc->txd.cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700552 list_add_tail(&desc->desc_node, &dwc->active_list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530553 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700554 } else {
Dan Williams41d5e592009-01-06 11:38:21 -0700555 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700556 desc->txd.cookie);
557
558 list_add_tail(&desc->desc_node, &dwc->queue);
559 }
560
561 spin_unlock_bh(&dwc->lock);
562
563 return cookie;
564}
565
566static struct dma_async_tx_descriptor *
567dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
568 size_t len, unsigned long flags)
569{
570 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
571 struct dw_desc *desc;
572 struct dw_desc *first;
573 struct dw_desc *prev;
574 size_t xfer_count;
575 size_t offset;
576 unsigned int src_width;
577 unsigned int dst_width;
578 u32 ctllo;
579
Dan Williams41d5e592009-01-06 11:38:21 -0700580 dev_vdbg(chan2dev(chan), "prep_dma_memcpy d0x%x s0x%x l0x%zx f0x%lx\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700581 dest, src, len, flags);
582
583 if (unlikely(!len)) {
Dan Williams41d5e592009-01-06 11:38:21 -0700584 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700585 return NULL;
586 }
587
588 /*
589 * We can be a lot more clever here, but this should take care
590 * of the most common optimization.
591 */
Viresh Kumara0227452011-03-03 15:47:18 +0530592 if (!((src | dest | len) & 7))
593 src_width = dst_width = 3;
594 else if (!((src | dest | len) & 3))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700595 src_width = dst_width = 2;
596 else if (!((src | dest | len) & 1))
597 src_width = dst_width = 1;
598 else
599 src_width = dst_width = 0;
600
Jamie Ilesf301c062011-01-21 14:11:53 +0000601 ctllo = DWC_DEFAULT_CTLLO(chan->private)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700602 | DWC_CTLL_DST_WIDTH(dst_width)
603 | DWC_CTLL_SRC_WIDTH(src_width)
604 | DWC_CTLL_DST_INC
605 | DWC_CTLL_SRC_INC
606 | DWC_CTLL_FC_M2M;
607 prev = first = NULL;
608
609 for (offset = 0; offset < len; offset += xfer_count << src_width) {
610 xfer_count = min_t(size_t, (len - offset) >> src_width,
611 DWC_MAX_COUNT);
612
613 desc = dwc_desc_get(dwc);
614 if (!desc)
615 goto err_desc_get;
616
617 desc->lli.sar = src + offset;
618 desc->lli.dar = dest + offset;
619 desc->lli.ctllo = ctllo;
620 desc->lli.ctlhi = xfer_count;
621
622 if (!first) {
623 first = desc;
624 } else {
625 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700626 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700627 prev->txd.phys, sizeof(prev->lli),
628 DMA_TO_DEVICE);
629 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700630 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700631 }
632 prev = desc;
633 }
634
635
636 if (flags & DMA_PREP_INTERRUPT)
637 /* Trigger interrupt after last block */
638 prev->lli.ctllo |= DWC_CTLL_INT_EN;
639
640 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700641 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700642 prev->txd.phys, sizeof(prev->lli),
643 DMA_TO_DEVICE);
644
645 first->txd.flags = flags;
646 first->len = len;
647
648 return &first->txd;
649
650err_desc_get:
651 dwc_desc_put(dwc, first);
652 return NULL;
653}
654
655static struct dma_async_tx_descriptor *
656dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
657 unsigned int sg_len, enum dma_data_direction direction,
658 unsigned long flags)
659{
660 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Dan Williams287d8592009-02-18 14:48:26 -0800661 struct dw_dma_slave *dws = chan->private;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700662 struct dw_desc *prev;
663 struct dw_desc *first;
664 u32 ctllo;
665 dma_addr_t reg;
666 unsigned int reg_width;
667 unsigned int mem_width;
668 unsigned int i;
669 struct scatterlist *sg;
670 size_t total_len = 0;
671
Dan Williams41d5e592009-01-06 11:38:21 -0700672 dev_vdbg(chan2dev(chan), "prep_dma_slave\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700673
674 if (unlikely(!dws || !sg_len))
675 return NULL;
676
Dan Williams74465b42009-01-06 11:38:16 -0700677 reg_width = dws->reg_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700678 prev = first = NULL;
679
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700680 switch (direction) {
681 case DMA_TO_DEVICE:
Jamie Ilesf301c062011-01-21 14:11:53 +0000682 ctllo = (DWC_DEFAULT_CTLLO(chan->private)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700683 | DWC_CTLL_DST_WIDTH(reg_width)
684 | DWC_CTLL_DST_FIX
685 | DWC_CTLL_SRC_INC
686 | DWC_CTLL_FC_M2P);
Dan Williams74465b42009-01-06 11:38:16 -0700687 reg = dws->tx_reg;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700688 for_each_sg(sgl, sg, sg_len, i) {
689 struct dw_desc *desc;
690 u32 len;
691 u32 mem;
692
693 desc = dwc_desc_get(dwc);
694 if (!desc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700695 dev_err(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700696 "not enough descriptors available\n");
697 goto err_desc_get;
698 }
699
700 mem = sg_phys(sg);
701 len = sg_dma_len(sg);
702 mem_width = 2;
703 if (unlikely(mem & 3 || len & 3))
704 mem_width = 0;
705
706 desc->lli.sar = mem;
707 desc->lli.dar = reg;
708 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
709 desc->lli.ctlhi = len >> mem_width;
710
711 if (!first) {
712 first = desc;
713 } else {
714 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700715 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700716 prev->txd.phys,
717 sizeof(prev->lli),
718 DMA_TO_DEVICE);
719 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700720 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700721 }
722 prev = desc;
723 total_len += len;
724 }
725 break;
726 case DMA_FROM_DEVICE:
Jamie Ilesf301c062011-01-21 14:11:53 +0000727 ctllo = (DWC_DEFAULT_CTLLO(chan->private)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700728 | DWC_CTLL_SRC_WIDTH(reg_width)
729 | DWC_CTLL_DST_INC
730 | DWC_CTLL_SRC_FIX
731 | DWC_CTLL_FC_P2M);
732
Dan Williams74465b42009-01-06 11:38:16 -0700733 reg = dws->rx_reg;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700734 for_each_sg(sgl, sg, sg_len, i) {
735 struct dw_desc *desc;
736 u32 len;
737 u32 mem;
738
739 desc = dwc_desc_get(dwc);
740 if (!desc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700741 dev_err(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700742 "not enough descriptors available\n");
743 goto err_desc_get;
744 }
745
746 mem = sg_phys(sg);
747 len = sg_dma_len(sg);
748 mem_width = 2;
749 if (unlikely(mem & 3 || len & 3))
750 mem_width = 0;
751
752 desc->lli.sar = reg;
753 desc->lli.dar = mem;
754 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
755 desc->lli.ctlhi = len >> reg_width;
756
757 if (!first) {
758 first = desc;
759 } else {
760 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700761 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700762 prev->txd.phys,
763 sizeof(prev->lli),
764 DMA_TO_DEVICE);
765 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700766 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700767 }
768 prev = desc;
769 total_len += len;
770 }
771 break;
772 default:
773 return NULL;
774 }
775
776 if (flags & DMA_PREP_INTERRUPT)
777 /* Trigger interrupt after last block */
778 prev->lli.ctllo |= DWC_CTLL_INT_EN;
779
780 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700781 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700782 prev->txd.phys, sizeof(prev->lli),
783 DMA_TO_DEVICE);
784
785 first->len = total_len;
786
787 return &first->txd;
788
789err_desc_get:
790 dwc_desc_put(dwc, first);
791 return NULL;
792}
793
Linus Walleij05827632010-05-17 16:30:42 -0700794static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
795 unsigned long arg)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700796{
797 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
798 struct dw_dma *dw = to_dw_dma(chan->device);
799 struct dw_desc *desc, *_desc;
800 LIST_HEAD(list);
801
Linus Walleijc3635c72010-03-26 16:44:01 -0700802 /* Only supports DMA_TERMINATE_ALL */
803 if (cmd != DMA_TERMINATE_ALL)
804 return -ENXIO;
805
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700806 /*
807 * This is only called when something went wrong elsewhere, so
808 * we don't really care about the data. Just disable the
809 * channel. We still have to poll the channel enable bit due
810 * to AHB/HSB limitations.
811 */
812 spin_lock_bh(&dwc->lock);
813
814 channel_clear_bit(dw, CH_EN, dwc->mask);
815
816 while (dma_readl(dw, CH_EN) & dwc->mask)
817 cpu_relax();
818
819 /* active_list entries will end up before queued entries */
820 list_splice_init(&dwc->queue, &list);
821 list_splice_init(&dwc->active_list, &list);
822
823 spin_unlock_bh(&dwc->lock);
824
825 /* Flush all pending and queued descriptors */
826 list_for_each_entry_safe(desc, _desc, &list, desc_node)
827 dwc_descriptor_complete(dwc, desc);
Linus Walleijc3635c72010-03-26 16:44:01 -0700828
829 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700830}
831
832static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -0700833dwc_tx_status(struct dma_chan *chan,
834 dma_cookie_t cookie,
835 struct dma_tx_state *txstate)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700836{
837 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
838 dma_cookie_t last_used;
839 dma_cookie_t last_complete;
840 int ret;
841
842 last_complete = dwc->completed;
843 last_used = chan->cookie;
844
845 ret = dma_async_is_complete(cookie, last_complete, last_used);
846 if (ret != DMA_SUCCESS) {
Viresh Kumar569432e2011-03-03 15:47:17 +0530847 spin_lock_bh(&dwc->lock);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700848 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
Viresh Kumar569432e2011-03-03 15:47:17 +0530849 spin_unlock_bh(&dwc->lock);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700850
851 last_complete = dwc->completed;
852 last_used = chan->cookie;
853
854 ret = dma_async_is_complete(cookie, last_complete, last_used);
855 }
856
Dan Williamsbca34692010-03-26 16:52:10 -0700857 dma_set_tx_state(txstate, last_complete, last_used, 0);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700858
859 return ret;
860}
861
862static void dwc_issue_pending(struct dma_chan *chan)
863{
864 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
865
866 spin_lock_bh(&dwc->lock);
867 if (!list_empty(&dwc->queue))
868 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
869 spin_unlock_bh(&dwc->lock);
870}
871
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700872static int dwc_alloc_chan_resources(struct dma_chan *chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700873{
874 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
875 struct dw_dma *dw = to_dw_dma(chan->device);
876 struct dw_desc *desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700877 struct dw_dma_slave *dws;
878 int i;
879 u32 cfghi;
880 u32 cfglo;
881
Dan Williams41d5e592009-01-06 11:38:21 -0700882 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700883
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700884 /* ASSERT: channel is idle */
885 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700886 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700887 return -EIO;
888 }
889
890 dwc->completed = chan->cookie = 1;
891
892 cfghi = DWC_CFGH_FIFO_MODE;
893 cfglo = 0;
894
Dan Williams287d8592009-02-18 14:48:26 -0800895 dws = chan->private;
Dan Williams74465b42009-01-06 11:38:16 -0700896 if (dws) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700897 /*
898 * We need controller-specific data to set up slave
899 * transfers.
900 */
Dan Williams74465b42009-01-06 11:38:16 -0700901 BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700902
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700903 cfghi = dws->cfg_hi;
Viresh Kumar93317e82011-03-03 15:47:22 +0530904 cfglo = dws->cfg_lo & ~DWC_CFGL_CH_PRIOR_MASK;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700905 }
Viresh Kumar93317e82011-03-03 15:47:22 +0530906
907 cfglo |= DWC_CFGL_CH_PRIOR(dwc->priority);
908
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700909 channel_writel(dwc, CFG_LO, cfglo);
910 channel_writel(dwc, CFG_HI, cfghi);
911
912 /*
913 * NOTE: some controllers may have additional features that we
914 * need to initialize here, like "scatter-gather" (which
915 * doesn't mean what you think it means), and status writeback.
916 */
917
918 spin_lock_bh(&dwc->lock);
919 i = dwc->descs_allocated;
920 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
921 spin_unlock_bh(&dwc->lock);
922
923 desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
924 if (!desc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700925 dev_info(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700926 "only allocated %d descriptors\n", i);
927 spin_lock_bh(&dwc->lock);
928 break;
929 }
930
Dan Williamse0bd0f82009-09-08 17:53:02 -0700931 INIT_LIST_HEAD(&desc->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700932 dma_async_tx_descriptor_init(&desc->txd, chan);
933 desc->txd.tx_submit = dwc_tx_submit;
934 desc->txd.flags = DMA_CTRL_ACK;
Dan Williams41d5e592009-01-06 11:38:21 -0700935 desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700936 sizeof(desc->lli), DMA_TO_DEVICE);
937 dwc_desc_put(dwc, desc);
938
939 spin_lock_bh(&dwc->lock);
940 i = ++dwc->descs_allocated;
941 }
942
943 /* Enable interrupts */
944 channel_set_bit(dw, MASK.XFER, dwc->mask);
945 channel_set_bit(dw, MASK.BLOCK, dwc->mask);
946 channel_set_bit(dw, MASK.ERROR, dwc->mask);
947
948 spin_unlock_bh(&dwc->lock);
949
Dan Williams41d5e592009-01-06 11:38:21 -0700950 dev_dbg(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700951 "alloc_chan_resources allocated %d descriptors\n", i);
952
953 return i;
954}
955
956static void dwc_free_chan_resources(struct dma_chan *chan)
957{
958 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
959 struct dw_dma *dw = to_dw_dma(chan->device);
960 struct dw_desc *desc, *_desc;
961 LIST_HEAD(list);
962
Dan Williams41d5e592009-01-06 11:38:21 -0700963 dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700964 dwc->descs_allocated);
965
966 /* ASSERT: channel is idle */
967 BUG_ON(!list_empty(&dwc->active_list));
968 BUG_ON(!list_empty(&dwc->queue));
969 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
970
971 spin_lock_bh(&dwc->lock);
972 list_splice_init(&dwc->free_list, &list);
973 dwc->descs_allocated = 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700974
975 /* Disable interrupts */
976 channel_clear_bit(dw, MASK.XFER, dwc->mask);
977 channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
978 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
979
980 spin_unlock_bh(&dwc->lock);
981
982 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
Dan Williams41d5e592009-01-06 11:38:21 -0700983 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
984 dma_unmap_single(chan2parent(chan), desc->txd.phys,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700985 sizeof(desc->lli), DMA_TO_DEVICE);
986 kfree(desc);
987 }
988
Dan Williams41d5e592009-01-06 11:38:21 -0700989 dev_vdbg(chan2dev(chan), "free_chan_resources done\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700990}
991
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200992/* --------------------- Cyclic DMA API extensions -------------------- */
993
994/**
995 * dw_dma_cyclic_start - start the cyclic DMA transfer
996 * @chan: the DMA channel to start
997 *
998 * Must be called with soft interrupts disabled. Returns zero on success or
999 * -errno on failure.
1000 */
1001int dw_dma_cyclic_start(struct dma_chan *chan)
1002{
1003 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1004 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1005
1006 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
1007 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
1008 return -ENODEV;
1009 }
1010
1011 spin_lock(&dwc->lock);
1012
1013 /* assert channel is idle */
1014 if (dma_readl(dw, CH_EN) & dwc->mask) {
1015 dev_err(chan2dev(&dwc->chan),
1016 "BUG: Attempted to start non-idle channel\n");
1017 dev_err(chan2dev(&dwc->chan),
1018 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
1019 channel_readl(dwc, SAR),
1020 channel_readl(dwc, DAR),
1021 channel_readl(dwc, LLP),
1022 channel_readl(dwc, CTL_HI),
1023 channel_readl(dwc, CTL_LO));
1024 spin_unlock(&dwc->lock);
1025 return -EBUSY;
1026 }
1027
1028 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
1029 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1030 dma_writel(dw, CLEAR.XFER, dwc->mask);
1031
1032 /* setup DMAC channel registers */
1033 channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
1034 channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
1035 channel_writel(dwc, CTL_HI, 0);
1036
1037 channel_set_bit(dw, CH_EN, dwc->mask);
1038
1039 spin_unlock(&dwc->lock);
1040
1041 return 0;
1042}
1043EXPORT_SYMBOL(dw_dma_cyclic_start);
1044
1045/**
1046 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1047 * @chan: the DMA channel to stop
1048 *
1049 * Must be called with soft interrupts disabled.
1050 */
1051void dw_dma_cyclic_stop(struct dma_chan *chan)
1052{
1053 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1054 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1055
1056 spin_lock(&dwc->lock);
1057
1058 channel_clear_bit(dw, CH_EN, dwc->mask);
1059 while (dma_readl(dw, CH_EN) & dwc->mask)
1060 cpu_relax();
1061
1062 spin_unlock(&dwc->lock);
1063}
1064EXPORT_SYMBOL(dw_dma_cyclic_stop);
1065
1066/**
1067 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1068 * @chan: the DMA channel to prepare
1069 * @buf_addr: physical DMA address where the buffer starts
1070 * @buf_len: total number of bytes for the entire buffer
1071 * @period_len: number of bytes for each period
1072 * @direction: transfer direction, to or from device
1073 *
1074 * Must be called before trying to start the transfer. Returns a valid struct
1075 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1076 */
1077struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1078 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
1079 enum dma_data_direction direction)
1080{
1081 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1082 struct dw_cyclic_desc *cdesc;
1083 struct dw_cyclic_desc *retval = NULL;
1084 struct dw_desc *desc;
1085 struct dw_desc *last = NULL;
1086 struct dw_dma_slave *dws = chan->private;
1087 unsigned long was_cyclic;
1088 unsigned int reg_width;
1089 unsigned int periods;
1090 unsigned int i;
1091
1092 spin_lock_bh(&dwc->lock);
1093 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
1094 spin_unlock_bh(&dwc->lock);
1095 dev_dbg(chan2dev(&dwc->chan),
1096 "queue and/or active list are not empty\n");
1097 return ERR_PTR(-EBUSY);
1098 }
1099
1100 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1101 spin_unlock_bh(&dwc->lock);
1102 if (was_cyclic) {
1103 dev_dbg(chan2dev(&dwc->chan),
1104 "channel already prepared for cyclic DMA\n");
1105 return ERR_PTR(-EBUSY);
1106 }
1107
1108 retval = ERR_PTR(-EINVAL);
1109 reg_width = dws->reg_width;
1110 periods = buf_len / period_len;
1111
1112 /* Check for too big/unaligned periods and unaligned DMA buffer. */
1113 if (period_len > (DWC_MAX_COUNT << reg_width))
1114 goto out_err;
1115 if (unlikely(period_len & ((1 << reg_width) - 1)))
1116 goto out_err;
1117 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1118 goto out_err;
1119 if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE))))
1120 goto out_err;
1121
1122 retval = ERR_PTR(-ENOMEM);
1123
1124 if (periods > NR_DESCS_PER_CHANNEL)
1125 goto out_err;
1126
1127 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1128 if (!cdesc)
1129 goto out_err;
1130
1131 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1132 if (!cdesc->desc)
1133 goto out_err_alloc;
1134
1135 for (i = 0; i < periods; i++) {
1136 desc = dwc_desc_get(dwc);
1137 if (!desc)
1138 goto out_err_desc_get;
1139
1140 switch (direction) {
1141 case DMA_TO_DEVICE:
1142 desc->lli.dar = dws->tx_reg;
1143 desc->lli.sar = buf_addr + (period_len * i);
Jamie Ilesf301c062011-01-21 14:11:53 +00001144 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001145 | DWC_CTLL_DST_WIDTH(reg_width)
1146 | DWC_CTLL_SRC_WIDTH(reg_width)
1147 | DWC_CTLL_DST_FIX
1148 | DWC_CTLL_SRC_INC
1149 | DWC_CTLL_FC_M2P
1150 | DWC_CTLL_INT_EN);
1151 break;
1152 case DMA_FROM_DEVICE:
1153 desc->lli.dar = buf_addr + (period_len * i);
1154 desc->lli.sar = dws->rx_reg;
Jamie Ilesf301c062011-01-21 14:11:53 +00001155 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001156 | DWC_CTLL_SRC_WIDTH(reg_width)
1157 | DWC_CTLL_DST_WIDTH(reg_width)
1158 | DWC_CTLL_DST_INC
1159 | DWC_CTLL_SRC_FIX
1160 | DWC_CTLL_FC_P2M
1161 | DWC_CTLL_INT_EN);
1162 break;
1163 default:
1164 break;
1165 }
1166
1167 desc->lli.ctlhi = (period_len >> reg_width);
1168 cdesc->desc[i] = desc;
1169
1170 if (last) {
1171 last->lli.llp = desc->txd.phys;
1172 dma_sync_single_for_device(chan2parent(chan),
1173 last->txd.phys, sizeof(last->lli),
1174 DMA_TO_DEVICE);
1175 }
1176
1177 last = desc;
1178 }
1179
1180 /* lets make a cyclic list */
1181 last->lli.llp = cdesc->desc[0]->txd.phys;
1182 dma_sync_single_for_device(chan2parent(chan), last->txd.phys,
1183 sizeof(last->lli), DMA_TO_DEVICE);
1184
1185 dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%08x len %zu "
1186 "period %zu periods %d\n", buf_addr, buf_len,
1187 period_len, periods);
1188
1189 cdesc->periods = periods;
1190 dwc->cdesc = cdesc;
1191
1192 return cdesc;
1193
1194out_err_desc_get:
1195 while (i--)
1196 dwc_desc_put(dwc, cdesc->desc[i]);
1197out_err_alloc:
1198 kfree(cdesc);
1199out_err:
1200 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1201 return (struct dw_cyclic_desc *)retval;
1202}
1203EXPORT_SYMBOL(dw_dma_cyclic_prep);
1204
1205/**
1206 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1207 * @chan: the DMA channel to free
1208 */
1209void dw_dma_cyclic_free(struct dma_chan *chan)
1210{
1211 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1212 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1213 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1214 int i;
1215
1216 dev_dbg(chan2dev(&dwc->chan), "cyclic free\n");
1217
1218 if (!cdesc)
1219 return;
1220
1221 spin_lock_bh(&dwc->lock);
1222
1223 channel_clear_bit(dw, CH_EN, dwc->mask);
1224 while (dma_readl(dw, CH_EN) & dwc->mask)
1225 cpu_relax();
1226
1227 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
1228 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1229 dma_writel(dw, CLEAR.XFER, dwc->mask);
1230
1231 spin_unlock_bh(&dwc->lock);
1232
1233 for (i = 0; i < cdesc->periods; i++)
1234 dwc_desc_put(dwc, cdesc->desc[i]);
1235
1236 kfree(cdesc->desc);
1237 kfree(cdesc);
1238
1239 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1240}
1241EXPORT_SYMBOL(dw_dma_cyclic_free);
1242
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001243/*----------------------------------------------------------------------*/
1244
1245static void dw_dma_off(struct dw_dma *dw)
1246{
1247 dma_writel(dw, CFG, 0);
1248
1249 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
1250 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1251 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1252 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1253 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1254
1255 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1256 cpu_relax();
1257}
1258
1259static int __init dw_probe(struct platform_device *pdev)
1260{
1261 struct dw_dma_platform_data *pdata;
1262 struct resource *io;
1263 struct dw_dma *dw;
1264 size_t size;
1265 int irq;
1266 int err;
1267 int i;
1268
1269 pdata = pdev->dev.platform_data;
1270 if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
1271 return -EINVAL;
1272
1273 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1274 if (!io)
1275 return -EINVAL;
1276
1277 irq = platform_get_irq(pdev, 0);
1278 if (irq < 0)
1279 return irq;
1280
1281 size = sizeof(struct dw_dma);
1282 size += pdata->nr_channels * sizeof(struct dw_dma_chan);
1283 dw = kzalloc(size, GFP_KERNEL);
1284 if (!dw)
1285 return -ENOMEM;
1286
1287 if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
1288 err = -EBUSY;
1289 goto err_kfree;
1290 }
1291
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001292 dw->regs = ioremap(io->start, DW_REGLEN);
1293 if (!dw->regs) {
1294 err = -ENOMEM;
1295 goto err_release_r;
1296 }
1297
1298 dw->clk = clk_get(&pdev->dev, "hclk");
1299 if (IS_ERR(dw->clk)) {
1300 err = PTR_ERR(dw->clk);
1301 goto err_clk;
1302 }
1303 clk_enable(dw->clk);
1304
1305 /* force dma off, just in case */
1306 dw_dma_off(dw);
1307
1308 err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
1309 if (err)
1310 goto err_irq;
1311
1312 platform_set_drvdata(pdev, dw);
1313
1314 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1315
1316 dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
1317
1318 INIT_LIST_HEAD(&dw->dma.channels);
1319 for (i = 0; i < pdata->nr_channels; i++, dw->dma.chancnt++) {
1320 struct dw_dma_chan *dwc = &dw->chan[i];
1321
1322 dwc->chan.device = &dw->dma;
1323 dwc->chan.cookie = dwc->completed = 1;
1324 dwc->chan.chan_id = i;
Viresh Kumarb0c31302011-03-03 15:47:21 +05301325 if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
1326 list_add_tail(&dwc->chan.device_node,
1327 &dw->dma.channels);
1328 else
1329 list_add(&dwc->chan.device_node, &dw->dma.channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001330
Viresh Kumar93317e82011-03-03 15:47:22 +05301331 /* 7 is highest priority & 0 is lowest. */
1332 if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
1333 dwc->priority = 7 - i;
1334 else
1335 dwc->priority = i;
1336
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001337 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1338 spin_lock_init(&dwc->lock);
1339 dwc->mask = 1 << i;
1340
1341 INIT_LIST_HEAD(&dwc->active_list);
1342 INIT_LIST_HEAD(&dwc->queue);
1343 INIT_LIST_HEAD(&dwc->free_list);
1344
1345 channel_clear_bit(dw, CH_EN, dwc->mask);
1346 }
1347
1348 /* Clear/disable all interrupts on all channels. */
1349 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
1350 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
1351 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1352 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1353 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1354
1355 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
1356 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1357 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1358 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1359 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1360
1361 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1362 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
Jamie Iles95ea7592011-01-21 14:11:54 +00001363 if (pdata->is_private)
1364 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001365 dw->dma.dev = &pdev->dev;
1366 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1367 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1368
1369 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
1370
1371 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
Linus Walleijc3635c72010-03-26 16:44:01 -07001372 dw->dma.device_control = dwc_control;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001373
Linus Walleij07934482010-03-26 16:50:49 -07001374 dw->dma.device_tx_status = dwc_tx_status;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001375 dw->dma.device_issue_pending = dwc_issue_pending;
1376
1377 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1378
1379 printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
Kay Sieversdfbc9012009-03-24 16:38:22 -07001380 dev_name(&pdev->dev), dw->dma.chancnt);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001381
1382 dma_async_device_register(&dw->dma);
1383
1384 return 0;
1385
1386err_irq:
1387 clk_disable(dw->clk);
1388 clk_put(dw->clk);
1389err_clk:
1390 iounmap(dw->regs);
1391 dw->regs = NULL;
1392err_release_r:
1393 release_resource(io);
1394err_kfree:
1395 kfree(dw);
1396 return err;
1397}
1398
1399static int __exit dw_remove(struct platform_device *pdev)
1400{
1401 struct dw_dma *dw = platform_get_drvdata(pdev);
1402 struct dw_dma_chan *dwc, *_dwc;
1403 struct resource *io;
1404
1405 dw_dma_off(dw);
1406 dma_async_device_unregister(&dw->dma);
1407
1408 free_irq(platform_get_irq(pdev, 0), dw);
1409 tasklet_kill(&dw->tasklet);
1410
1411 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1412 chan.device_node) {
1413 list_del(&dwc->chan.device_node);
1414 channel_clear_bit(dw, CH_EN, dwc->mask);
1415 }
1416
1417 clk_disable(dw->clk);
1418 clk_put(dw->clk);
1419
1420 iounmap(dw->regs);
1421 dw->regs = NULL;
1422
1423 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1424 release_mem_region(io->start, DW_REGLEN);
1425
1426 kfree(dw);
1427
1428 return 0;
1429}
1430
1431static void dw_shutdown(struct platform_device *pdev)
1432{
1433 struct dw_dma *dw = platform_get_drvdata(pdev);
1434
1435 dw_dma_off(platform_get_drvdata(pdev));
1436 clk_disable(dw->clk);
1437}
1438
Magnus Damm4a256b52009-07-08 13:22:18 +02001439static int dw_suspend_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001440{
Magnus Damm4a256b52009-07-08 13:22:18 +02001441 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001442 struct dw_dma *dw = platform_get_drvdata(pdev);
1443
1444 dw_dma_off(platform_get_drvdata(pdev));
1445 clk_disable(dw->clk);
1446 return 0;
1447}
1448
Magnus Damm4a256b52009-07-08 13:22:18 +02001449static int dw_resume_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001450{
Magnus Damm4a256b52009-07-08 13:22:18 +02001451 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001452 struct dw_dma *dw = platform_get_drvdata(pdev);
1453
1454 clk_enable(dw->clk);
1455 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1456 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001457}
1458
Alexey Dobriyan47145212009-12-14 18:00:08 -08001459static const struct dev_pm_ops dw_dev_pm_ops = {
Magnus Damm4a256b52009-07-08 13:22:18 +02001460 .suspend_noirq = dw_suspend_noirq,
1461 .resume_noirq = dw_resume_noirq,
1462};
1463
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001464static struct platform_driver dw_driver = {
1465 .remove = __exit_p(dw_remove),
1466 .shutdown = dw_shutdown,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001467 .driver = {
1468 .name = "dw_dmac",
Magnus Damm4a256b52009-07-08 13:22:18 +02001469 .pm = &dw_dev_pm_ops,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001470 },
1471};
1472
1473static int __init dw_init(void)
1474{
1475 return platform_driver_probe(&dw_driver, dw_probe);
1476}
Viresh Kumarcb689a72011-03-03 15:47:15 +05301477subsys_initcall(dw_init);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001478
1479static void __exit dw_exit(void)
1480{
1481 platform_driver_unregister(&dw_driver);
1482}
1483module_exit(dw_exit);
1484
1485MODULE_LICENSE("GPL v2");
1486MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
1487MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");