dmaengine/dw_dmac: don't scan descriptors if no xfers in progress
[deliverable/linux.git] / drivers / dma / dw_dmac.c
CommitLineData
3bfb1d20
HS
1/*
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
35/* NOTE: DMS+SMS is system-specific. We should get this information
36 * from the platform code somehow.
37 */
38#define DWC_DEFAULT_CTLLO (DWC_CTLL_DST_MSIZE(0) \
39 | DWC_CTLL_SRC_MSIZE(0) \
40 | DWC_CTLL_DMS(0) \
41 | DWC_CTLL_SMS(1) \
42 | DWC_CTLL_LLP_D_EN \
43 | DWC_CTLL_LLP_S_EN)
44
45/*
46 * This is configuration-dependent and usually a funny size like 4095.
47 * Let's round it down to the nearest power of two.
48 *
49 * Note that this is a transfer count, i.e. if we transfer 32-bit
50 * words, we can do 8192 bytes per descriptor.
51 *
52 * This parameter is also system-specific.
53 */
54#define DWC_MAX_COUNT 2048U
55
56/*
57 * Number of descriptors to allocate for each channel. This should be
58 * made configurable somehow; preferably, the clients (at least the
59 * ones using slave transfers) should be able to give us a hint.
60 */
61#define NR_DESCS_PER_CHANNEL 64
62
63/*----------------------------------------------------------------------*/
64
65/*
66 * Because we're not relying on writeback from the controller (it may not
67 * even be configured into the core!) we don't need to use dma_pool. These
68 * descriptors -- and associated data -- are cacheable. We do need to make
69 * sure their dcache entries are written back before handing them off to
70 * the controller, though.
71 */
72
41d5e59c
DW
73static struct device *chan2dev(struct dma_chan *chan)
74{
75 return &chan->dev->device;
76}
77static struct device *chan2parent(struct dma_chan *chan)
78{
79 return chan->dev->device.parent;
80}
81
3bfb1d20
HS
82static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
83{
84 return list_entry(dwc->active_list.next, struct dw_desc, desc_node);
85}
86
87static struct dw_desc *dwc_first_queued(struct dw_dma_chan *dwc)
88{
89 return list_entry(dwc->queue.next, struct dw_desc, desc_node);
90}
91
92static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
93{
94 struct dw_desc *desc, *_desc;
95 struct dw_desc *ret = NULL;
96 unsigned int i = 0;
97
98 spin_lock_bh(&dwc->lock);
99 list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
100 if (async_tx_test_ack(&desc->txd)) {
101 list_del(&desc->desc_node);
102 ret = desc;
103 break;
104 }
41d5e59c 105 dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
3bfb1d20
HS
106 i++;
107 }
108 spin_unlock_bh(&dwc->lock);
109
41d5e59c 110 dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
3bfb1d20
HS
111
112 return ret;
113}
114
115static void dwc_sync_desc_for_cpu(struct dw_dma_chan *dwc, struct dw_desc *desc)
116{
117 struct dw_desc *child;
118
e0bd0f8c 119 list_for_each_entry(child, &desc->tx_list, desc_node)
41d5e59c 120 dma_sync_single_for_cpu(chan2parent(&dwc->chan),
3bfb1d20
HS
121 child->txd.phys, sizeof(child->lli),
122 DMA_TO_DEVICE);
41d5e59c 123 dma_sync_single_for_cpu(chan2parent(&dwc->chan),
3bfb1d20
HS
124 desc->txd.phys, sizeof(desc->lli),
125 DMA_TO_DEVICE);
126}
127
128/*
129 * Move a descriptor, including any children, to the free list.
130 * `desc' must not be on any lists.
131 */
132static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
133{
134 if (desc) {
135 struct dw_desc *child;
136
137 dwc_sync_desc_for_cpu(dwc, desc);
138
139 spin_lock_bh(&dwc->lock);
e0bd0f8c 140 list_for_each_entry(child, &desc->tx_list, desc_node)
41d5e59c 141 dev_vdbg(chan2dev(&dwc->chan),
3bfb1d20
HS
142 "moving child desc %p to freelist\n",
143 child);
e0bd0f8c 144 list_splice_init(&desc->tx_list, &dwc->free_list);
41d5e59c 145 dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
3bfb1d20
HS
146 list_add(&desc->desc_node, &dwc->free_list);
147 spin_unlock_bh(&dwc->lock);
148 }
149}
150
151/* Called with dwc->lock held and bh disabled */
152static dma_cookie_t
153dwc_assign_cookie(struct dw_dma_chan *dwc, struct dw_desc *desc)
154{
155 dma_cookie_t cookie = dwc->chan.cookie;
156
157 if (++cookie < 0)
158 cookie = 1;
159
160 dwc->chan.cookie = cookie;
161 desc->txd.cookie = cookie;
162
163 return cookie;
164}
165
166/*----------------------------------------------------------------------*/
167
168/* Called with dwc->lock held and bh disabled */
169static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
170{
171 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
172
173 /* ASSERT: channel is idle */
174 if (dma_readl(dw, CH_EN) & dwc->mask) {
41d5e59c 175 dev_err(chan2dev(&dwc->chan),
3bfb1d20 176 "BUG: Attempted to start non-idle channel\n");
41d5e59c 177 dev_err(chan2dev(&dwc->chan),
3bfb1d20
HS
178 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
179 channel_readl(dwc, SAR),
180 channel_readl(dwc, DAR),
181 channel_readl(dwc, LLP),
182 channel_readl(dwc, CTL_HI),
183 channel_readl(dwc, CTL_LO));
184
185 /* The tasklet will hopefully advance the queue... */
186 return;
187 }
188
189 channel_writel(dwc, LLP, first->txd.phys);
190 channel_writel(dwc, CTL_LO,
191 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
192 channel_writel(dwc, CTL_HI, 0);
193 channel_set_bit(dw, CH_EN, dwc->mask);
194}
195
196/*----------------------------------------------------------------------*/
197
198static void
199dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc)
200{
201 dma_async_tx_callback callback;
202 void *param;
203 struct dma_async_tx_descriptor *txd = &desc->txd;
204
41d5e59c 205 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
3bfb1d20
HS
206
207 dwc->completed = txd->cookie;
208 callback = txd->callback;
209 param = txd->callback_param;
210
211 dwc_sync_desc_for_cpu(dwc, desc);
e0bd0f8c 212 list_splice_init(&desc->tx_list, &dwc->free_list);
3bfb1d20
HS
213 list_move(&desc->desc_node, &dwc->free_list);
214
657a77fa
AN
215 if (!dwc->chan.private) {
216 struct device *parent = chan2parent(&dwc->chan);
217 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
218 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
219 dma_unmap_single(parent, desc->lli.dar,
220 desc->len, DMA_FROM_DEVICE);
221 else
222 dma_unmap_page(parent, desc->lli.dar,
223 desc->len, DMA_FROM_DEVICE);
224 }
225 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
226 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
227 dma_unmap_single(parent, desc->lli.sar,
228 desc->len, DMA_TO_DEVICE);
229 else
230 dma_unmap_page(parent, desc->lli.sar,
231 desc->len, DMA_TO_DEVICE);
232 }
233 }
3bfb1d20
HS
234
235 /*
236 * The API requires that no submissions are done from a
237 * callback, so we don't need to drop the lock here
238 */
239 if (callback)
240 callback(param);
241}
242
243static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
244{
245 struct dw_desc *desc, *_desc;
246 LIST_HEAD(list);
247
248 if (dma_readl(dw, CH_EN) & dwc->mask) {
41d5e59c 249 dev_err(chan2dev(&dwc->chan),
3bfb1d20
HS
250 "BUG: XFER bit set, but channel not idle!\n");
251
252 /* Try to continue after resetting the channel... */
253 channel_clear_bit(dw, CH_EN, dwc->mask);
254 while (dma_readl(dw, CH_EN) & dwc->mask)
255 cpu_relax();
256 }
257
258 /*
259 * Submit queued descriptors ASAP, i.e. before we go through
260 * the completed ones.
261 */
262 if (!list_empty(&dwc->queue))
263 dwc_dostart(dwc, dwc_first_queued(dwc));
264 list_splice_init(&dwc->active_list, &list);
265 list_splice_init(&dwc->queue, &dwc->active_list);
266
267 list_for_each_entry_safe(desc, _desc, &list, desc_node)
268 dwc_descriptor_complete(dwc, desc);
269}
270
271static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
272{
273 dma_addr_t llp;
274 struct dw_desc *desc, *_desc;
275 struct dw_desc *child;
276 u32 status_xfer;
277
278 /*
279 * Clear block interrupt flag before scanning so that we don't
280 * miss any, and read LLP before RAW_XFER to ensure it is
281 * valid if we decide to scan the list.
282 */
283 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
284 llp = channel_readl(dwc, LLP);
285 status_xfer = dma_readl(dw, RAW.XFER);
286
287 if (status_xfer & dwc->mask) {
288 /* Everything we've submitted is done */
289 dma_writel(dw, CLEAR.XFER, dwc->mask);
290 dwc_complete_all(dw, dwc);
291 return;
292 }
293
087809fc
JI
294 if (list_empty(&dwc->active_list))
295 return;
296
41d5e59c 297 dev_vdbg(chan2dev(&dwc->chan), "scan_descriptors: llp=0x%x\n", llp);
3bfb1d20
HS
298
299 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
300 if (desc->lli.llp == llp)
301 /* This one is currently in progress */
302 return;
303
e0bd0f8c 304 list_for_each_entry(child, &desc->tx_list, desc_node)
3bfb1d20
HS
305 if (child->lli.llp == llp)
306 /* Currently in progress */
307 return;
308
309 /*
310 * No descriptors so far seem to be in progress, i.e.
311 * this one must be done.
312 */
313 dwc_descriptor_complete(dwc, desc);
314 }
315
41d5e59c 316 dev_err(chan2dev(&dwc->chan),
3bfb1d20
HS
317 "BUG: All descriptors done, but channel not idle!\n");
318
319 /* Try to continue after resetting the channel... */
320 channel_clear_bit(dw, CH_EN, dwc->mask);
321 while (dma_readl(dw, CH_EN) & dwc->mask)
322 cpu_relax();
323
324 if (!list_empty(&dwc->queue)) {
325 dwc_dostart(dwc, dwc_first_queued(dwc));
326 list_splice_init(&dwc->queue, &dwc->active_list);
327 }
328}
329
330static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
331{
41d5e59c 332 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
3bfb1d20
HS
333 " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
334 lli->sar, lli->dar, lli->llp,
335 lli->ctlhi, lli->ctllo);
336}
337
338static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
339{
340 struct dw_desc *bad_desc;
341 struct dw_desc *child;
342
343 dwc_scan_descriptors(dw, dwc);
344
345 /*
346 * The descriptor currently at the head of the active list is
347 * borked. Since we don't have any way to report errors, we'll
348 * just have to scream loudly and try to carry on.
349 */
350 bad_desc = dwc_first_active(dwc);
351 list_del_init(&bad_desc->desc_node);
352 list_splice_init(&dwc->queue, dwc->active_list.prev);
353
354 /* Clear the error flag and try to restart the controller */
355 dma_writel(dw, CLEAR.ERROR, dwc->mask);
356 if (!list_empty(&dwc->active_list))
357 dwc_dostart(dwc, dwc_first_active(dwc));
358
359 /*
360 * KERN_CRITICAL may seem harsh, but since this only happens
361 * when someone submits a bad physical address in a
362 * descriptor, we should consider ourselves lucky that the
363 * controller flagged an error instead of scribbling over
364 * random memory locations.
365 */
41d5e59c 366 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
3bfb1d20 367 "Bad descriptor submitted for DMA!\n");
41d5e59c 368 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
3bfb1d20
HS
369 " cookie: %d\n", bad_desc->txd.cookie);
370 dwc_dump_lli(dwc, &bad_desc->lli);
e0bd0f8c 371 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
3bfb1d20
HS
372 dwc_dump_lli(dwc, &child->lli);
373
374 /* Pretend the descriptor completed successfully */
375 dwc_descriptor_complete(dwc, bad_desc);
376}
377
d9de4519
HCE
378/* --------------------- Cyclic DMA API extensions -------------------- */
379
380inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
381{
382 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
383 return channel_readl(dwc, SAR);
384}
385EXPORT_SYMBOL(dw_dma_get_src_addr);
386
387inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
388{
389 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
390 return channel_readl(dwc, DAR);
391}
392EXPORT_SYMBOL(dw_dma_get_dst_addr);
393
394/* called with dwc->lock held and all DMAC interrupts disabled */
395static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
396 u32 status_block, u32 status_err, u32 status_xfer)
397{
398 if (status_block & dwc->mask) {
399 void (*callback)(void *param);
400 void *callback_param;
401
402 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
403 channel_readl(dwc, LLP));
404 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
405
406 callback = dwc->cdesc->period_callback;
407 callback_param = dwc->cdesc->period_callback_param;
408 if (callback) {
409 spin_unlock(&dwc->lock);
410 callback(callback_param);
411 spin_lock(&dwc->lock);
412 }
413 }
414
415 /*
416 * Error and transfer complete are highly unlikely, and will most
417 * likely be due to a configuration error by the user.
418 */
419 if (unlikely(status_err & dwc->mask) ||
420 unlikely(status_xfer & dwc->mask)) {
421 int i;
422
423 dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
424 "interrupt, stopping DMA transfer\n",
425 status_xfer ? "xfer" : "error");
426 dev_err(chan2dev(&dwc->chan),
427 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
428 channel_readl(dwc, SAR),
429 channel_readl(dwc, DAR),
430 channel_readl(dwc, LLP),
431 channel_readl(dwc, CTL_HI),
432 channel_readl(dwc, CTL_LO));
433
434 channel_clear_bit(dw, CH_EN, dwc->mask);
435 while (dma_readl(dw, CH_EN) & dwc->mask)
436 cpu_relax();
437
438 /* make sure DMA does not restart by loading a new list */
439 channel_writel(dwc, LLP, 0);
440 channel_writel(dwc, CTL_LO, 0);
441 channel_writel(dwc, CTL_HI, 0);
442
443 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
444 dma_writel(dw, CLEAR.ERROR, dwc->mask);
445 dma_writel(dw, CLEAR.XFER, dwc->mask);
446
447 for (i = 0; i < dwc->cdesc->periods; i++)
448 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
449 }
450}
451
452/* ------------------------------------------------------------------------- */
453
3bfb1d20
HS
454static void dw_dma_tasklet(unsigned long data)
455{
456 struct dw_dma *dw = (struct dw_dma *)data;
457 struct dw_dma_chan *dwc;
458 u32 status_block;
459 u32 status_xfer;
460 u32 status_err;
461 int i;
462
463 status_block = dma_readl(dw, RAW.BLOCK);
7fe7b2f4 464 status_xfer = dma_readl(dw, RAW.XFER);
3bfb1d20
HS
465 status_err = dma_readl(dw, RAW.ERROR);
466
467 dev_vdbg(dw->dma.dev, "tasklet: status_block=%x status_err=%x\n",
468 status_block, status_err);
469
470 for (i = 0; i < dw->dma.chancnt; i++) {
471 dwc = &dw->chan[i];
472 spin_lock(&dwc->lock);
d9de4519
HCE
473 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
474 dwc_handle_cyclic(dw, dwc, status_block, status_err,
475 status_xfer);
476 else if (status_err & (1 << i))
3bfb1d20
HS
477 dwc_handle_error(dw, dwc);
478 else if ((status_block | status_xfer) & (1 << i))
479 dwc_scan_descriptors(dw, dwc);
480 spin_unlock(&dwc->lock);
481 }
482
483 /*
484 * Re-enable interrupts. Block Complete interrupts are only
485 * enabled if the INT_EN bit in the descriptor is set. This
486 * will trigger a scan before the whole list is done.
487 */
488 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
489 channel_set_bit(dw, MASK.BLOCK, dw->all_chan_mask);
490 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
491}
492
493static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
494{
495 struct dw_dma *dw = dev_id;
496 u32 status;
497
498 dev_vdbg(dw->dma.dev, "interrupt: status=0x%x\n",
499 dma_readl(dw, STATUS_INT));
500
501 /*
502 * Just disable the interrupts. We'll turn them back on in the
503 * softirq handler.
504 */
505 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
506 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
507 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
508
509 status = dma_readl(dw, STATUS_INT);
510 if (status) {
511 dev_err(dw->dma.dev,
512 "BUG: Unexpected interrupts pending: 0x%x\n",
513 status);
514
515 /* Try to recover */
516 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
517 channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
518 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
519 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
520 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
521 }
522
523 tasklet_schedule(&dw->tasklet);
524
525 return IRQ_HANDLED;
526}
527
528/*----------------------------------------------------------------------*/
529
530static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
531{
532 struct dw_desc *desc = txd_to_dw_desc(tx);
533 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
534 dma_cookie_t cookie;
535
536 spin_lock_bh(&dwc->lock);
537 cookie = dwc_assign_cookie(dwc, desc);
538
539 /*
540 * REVISIT: We should attempt to chain as many descriptors as
541 * possible, perhaps even appending to those already submitted
542 * for DMA. But this is hard to do in a race-free manner.
543 */
544 if (list_empty(&dwc->active_list)) {
41d5e59c 545 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
3bfb1d20
HS
546 desc->txd.cookie);
547 dwc_dostart(dwc, desc);
548 list_add_tail(&desc->desc_node, &dwc->active_list);
549 } else {
41d5e59c 550 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
3bfb1d20
HS
551 desc->txd.cookie);
552
553 list_add_tail(&desc->desc_node, &dwc->queue);
554 }
555
556 spin_unlock_bh(&dwc->lock);
557
558 return cookie;
559}
560
561static struct dma_async_tx_descriptor *
562dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
563 size_t len, unsigned long flags)
564{
565 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
566 struct dw_desc *desc;
567 struct dw_desc *first;
568 struct dw_desc *prev;
569 size_t xfer_count;
570 size_t offset;
571 unsigned int src_width;
572 unsigned int dst_width;
573 u32 ctllo;
574
41d5e59c 575 dev_vdbg(chan2dev(chan), "prep_dma_memcpy d0x%x s0x%x l0x%zx f0x%lx\n",
3bfb1d20
HS
576 dest, src, len, flags);
577
578 if (unlikely(!len)) {
41d5e59c 579 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
3bfb1d20
HS
580 return NULL;
581 }
582
583 /*
584 * We can be a lot more clever here, but this should take care
585 * of the most common optimization.
586 */
587 if (!((src | dest | len) & 3))
588 src_width = dst_width = 2;
589 else if (!((src | dest | len) & 1))
590 src_width = dst_width = 1;
591 else
592 src_width = dst_width = 0;
593
594 ctllo = DWC_DEFAULT_CTLLO
595 | DWC_CTLL_DST_WIDTH(dst_width)
596 | DWC_CTLL_SRC_WIDTH(src_width)
597 | DWC_CTLL_DST_INC
598 | DWC_CTLL_SRC_INC
599 | DWC_CTLL_FC_M2M;
600 prev = first = NULL;
601
602 for (offset = 0; offset < len; offset += xfer_count << src_width) {
603 xfer_count = min_t(size_t, (len - offset) >> src_width,
604 DWC_MAX_COUNT);
605
606 desc = dwc_desc_get(dwc);
607 if (!desc)
608 goto err_desc_get;
609
610 desc->lli.sar = src + offset;
611 desc->lli.dar = dest + offset;
612 desc->lli.ctllo = ctllo;
613 desc->lli.ctlhi = xfer_count;
614
615 if (!first) {
616 first = desc;
617 } else {
618 prev->lli.llp = desc->txd.phys;
41d5e59c 619 dma_sync_single_for_device(chan2parent(chan),
3bfb1d20
HS
620 prev->txd.phys, sizeof(prev->lli),
621 DMA_TO_DEVICE);
622 list_add_tail(&desc->desc_node,
e0bd0f8c 623 &first->tx_list);
3bfb1d20
HS
624 }
625 prev = desc;
626 }
627
628
629 if (flags & DMA_PREP_INTERRUPT)
630 /* Trigger interrupt after last block */
631 prev->lli.ctllo |= DWC_CTLL_INT_EN;
632
633 prev->lli.llp = 0;
41d5e59c 634 dma_sync_single_for_device(chan2parent(chan),
3bfb1d20
HS
635 prev->txd.phys, sizeof(prev->lli),
636 DMA_TO_DEVICE);
637
638 first->txd.flags = flags;
639 first->len = len;
640
641 return &first->txd;
642
643err_desc_get:
644 dwc_desc_put(dwc, first);
645 return NULL;
646}
647
648static struct dma_async_tx_descriptor *
649dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
650 unsigned int sg_len, enum dma_data_direction direction,
651 unsigned long flags)
652{
653 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
287d8592 654 struct dw_dma_slave *dws = chan->private;
3bfb1d20
HS
655 struct dw_desc *prev;
656 struct dw_desc *first;
657 u32 ctllo;
658 dma_addr_t reg;
659 unsigned int reg_width;
660 unsigned int mem_width;
661 unsigned int i;
662 struct scatterlist *sg;
663 size_t total_len = 0;
664
41d5e59c 665 dev_vdbg(chan2dev(chan), "prep_dma_slave\n");
3bfb1d20
HS
666
667 if (unlikely(!dws || !sg_len))
668 return NULL;
669
74465b4f 670 reg_width = dws->reg_width;
3bfb1d20
HS
671 prev = first = NULL;
672
3bfb1d20
HS
673 switch (direction) {
674 case DMA_TO_DEVICE:
675 ctllo = (DWC_DEFAULT_CTLLO
676 | DWC_CTLL_DST_WIDTH(reg_width)
677 | DWC_CTLL_DST_FIX
678 | DWC_CTLL_SRC_INC
679 | DWC_CTLL_FC_M2P);
74465b4f 680 reg = dws->tx_reg;
3bfb1d20
HS
681 for_each_sg(sgl, sg, sg_len, i) {
682 struct dw_desc *desc;
683 u32 len;
684 u32 mem;
685
686 desc = dwc_desc_get(dwc);
687 if (!desc) {
41d5e59c 688 dev_err(chan2dev(chan),
3bfb1d20
HS
689 "not enough descriptors available\n");
690 goto err_desc_get;
691 }
692
693 mem = sg_phys(sg);
694 len = sg_dma_len(sg);
695 mem_width = 2;
696 if (unlikely(mem & 3 || len & 3))
697 mem_width = 0;
698
699 desc->lli.sar = mem;
700 desc->lli.dar = reg;
701 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
702 desc->lli.ctlhi = len >> mem_width;
703
704 if (!first) {
705 first = desc;
706 } else {
707 prev->lli.llp = desc->txd.phys;
41d5e59c 708 dma_sync_single_for_device(chan2parent(chan),
3bfb1d20
HS
709 prev->txd.phys,
710 sizeof(prev->lli),
711 DMA_TO_DEVICE);
712 list_add_tail(&desc->desc_node,
e0bd0f8c 713 &first->tx_list);
3bfb1d20
HS
714 }
715 prev = desc;
716 total_len += len;
717 }
718 break;
719 case DMA_FROM_DEVICE:
720 ctllo = (DWC_DEFAULT_CTLLO
721 | DWC_CTLL_SRC_WIDTH(reg_width)
722 | DWC_CTLL_DST_INC
723 | DWC_CTLL_SRC_FIX
724 | DWC_CTLL_FC_P2M);
725
74465b4f 726 reg = dws->rx_reg;
3bfb1d20
HS
727 for_each_sg(sgl, sg, sg_len, i) {
728 struct dw_desc *desc;
729 u32 len;
730 u32 mem;
731
732 desc = dwc_desc_get(dwc);
733 if (!desc) {
41d5e59c 734 dev_err(chan2dev(chan),
3bfb1d20
HS
735 "not enough descriptors available\n");
736 goto err_desc_get;
737 }
738
739 mem = sg_phys(sg);
740 len = sg_dma_len(sg);
741 mem_width = 2;
742 if (unlikely(mem & 3 || len & 3))
743 mem_width = 0;
744
745 desc->lli.sar = reg;
746 desc->lli.dar = mem;
747 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
748 desc->lli.ctlhi = len >> reg_width;
749
750 if (!first) {
751 first = desc;
752 } else {
753 prev->lli.llp = desc->txd.phys;
41d5e59c 754 dma_sync_single_for_device(chan2parent(chan),
3bfb1d20
HS
755 prev->txd.phys,
756 sizeof(prev->lli),
757 DMA_TO_DEVICE);
758 list_add_tail(&desc->desc_node,
e0bd0f8c 759 &first->tx_list);
3bfb1d20
HS
760 }
761 prev = desc;
762 total_len += len;
763 }
764 break;
765 default:
766 return NULL;
767 }
768
769 if (flags & DMA_PREP_INTERRUPT)
770 /* Trigger interrupt after last block */
771 prev->lli.ctllo |= DWC_CTLL_INT_EN;
772
773 prev->lli.llp = 0;
41d5e59c 774 dma_sync_single_for_device(chan2parent(chan),
3bfb1d20
HS
775 prev->txd.phys, sizeof(prev->lli),
776 DMA_TO_DEVICE);
777
778 first->len = total_len;
779
780 return &first->txd;
781
782err_desc_get:
783 dwc_desc_put(dwc, first);
784 return NULL;
785}
786
05827630
LW
787static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
788 unsigned long arg)
3bfb1d20
HS
789{
790 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
791 struct dw_dma *dw = to_dw_dma(chan->device);
792 struct dw_desc *desc, *_desc;
793 LIST_HEAD(list);
794
c3635c78
LW
795 /* Only supports DMA_TERMINATE_ALL */
796 if (cmd != DMA_TERMINATE_ALL)
797 return -ENXIO;
798
3bfb1d20
HS
799 /*
800 * This is only called when something went wrong elsewhere, so
801 * we don't really care about the data. Just disable the
802 * channel. We still have to poll the channel enable bit due
803 * to AHB/HSB limitations.
804 */
805 spin_lock_bh(&dwc->lock);
806
807 channel_clear_bit(dw, CH_EN, dwc->mask);
808
809 while (dma_readl(dw, CH_EN) & dwc->mask)
810 cpu_relax();
811
812 /* active_list entries will end up before queued entries */
813 list_splice_init(&dwc->queue, &list);
814 list_splice_init(&dwc->active_list, &list);
815
816 spin_unlock_bh(&dwc->lock);
817
818 /* Flush all pending and queued descriptors */
819 list_for_each_entry_safe(desc, _desc, &list, desc_node)
820 dwc_descriptor_complete(dwc, desc);
c3635c78
LW
821
822 return 0;
3bfb1d20
HS
823}
824
825static enum dma_status
07934481
LW
826dwc_tx_status(struct dma_chan *chan,
827 dma_cookie_t cookie,
828 struct dma_tx_state *txstate)
3bfb1d20
HS
829{
830 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
831 dma_cookie_t last_used;
832 dma_cookie_t last_complete;
833 int ret;
834
835 last_complete = dwc->completed;
836 last_used = chan->cookie;
837
838 ret = dma_async_is_complete(cookie, last_complete, last_used);
839 if (ret != DMA_SUCCESS) {
840 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
841
842 last_complete = dwc->completed;
843 last_used = chan->cookie;
844
845 ret = dma_async_is_complete(cookie, last_complete, last_used);
846 }
847
bca34692 848 dma_set_tx_state(txstate, last_complete, last_used, 0);
3bfb1d20
HS
849
850 return ret;
851}
852
853static void dwc_issue_pending(struct dma_chan *chan)
854{
855 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
856
857 spin_lock_bh(&dwc->lock);
858 if (!list_empty(&dwc->queue))
859 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
860 spin_unlock_bh(&dwc->lock);
861}
862
aa1e6f1a 863static int dwc_alloc_chan_resources(struct dma_chan *chan)
3bfb1d20
HS
864{
865 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
866 struct dw_dma *dw = to_dw_dma(chan->device);
867 struct dw_desc *desc;
3bfb1d20
HS
868 struct dw_dma_slave *dws;
869 int i;
870 u32 cfghi;
871 u32 cfglo;
872
41d5e59c 873 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
3bfb1d20 874
3bfb1d20
HS
875 /* ASSERT: channel is idle */
876 if (dma_readl(dw, CH_EN) & dwc->mask) {
41d5e59c 877 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
3bfb1d20
HS
878 return -EIO;
879 }
880
881 dwc->completed = chan->cookie = 1;
882
883 cfghi = DWC_CFGH_FIFO_MODE;
884 cfglo = 0;
885
287d8592 886 dws = chan->private;
74465b4f 887 if (dws) {
3bfb1d20
HS
888 /*
889 * We need controller-specific data to set up slave
890 * transfers.
891 */
74465b4f 892 BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
3bfb1d20 893
3bfb1d20
HS
894 cfghi = dws->cfg_hi;
895 cfglo = dws->cfg_lo;
3bfb1d20 896 }
3bfb1d20
HS
897 channel_writel(dwc, CFG_LO, cfglo);
898 channel_writel(dwc, CFG_HI, cfghi);
899
900 /*
901 * NOTE: some controllers may have additional features that we
902 * need to initialize here, like "scatter-gather" (which
903 * doesn't mean what you think it means), and status writeback.
904 */
905
906 spin_lock_bh(&dwc->lock);
907 i = dwc->descs_allocated;
908 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
909 spin_unlock_bh(&dwc->lock);
910
911 desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
912 if (!desc) {
41d5e59c 913 dev_info(chan2dev(chan),
3bfb1d20
HS
914 "only allocated %d descriptors\n", i);
915 spin_lock_bh(&dwc->lock);
916 break;
917 }
918
e0bd0f8c 919 INIT_LIST_HEAD(&desc->tx_list);
3bfb1d20
HS
920 dma_async_tx_descriptor_init(&desc->txd, chan);
921 desc->txd.tx_submit = dwc_tx_submit;
922 desc->txd.flags = DMA_CTRL_ACK;
41d5e59c 923 desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
3bfb1d20
HS
924 sizeof(desc->lli), DMA_TO_DEVICE);
925 dwc_desc_put(dwc, desc);
926
927 spin_lock_bh(&dwc->lock);
928 i = ++dwc->descs_allocated;
929 }
930
931 /* Enable interrupts */
932 channel_set_bit(dw, MASK.XFER, dwc->mask);
933 channel_set_bit(dw, MASK.BLOCK, dwc->mask);
934 channel_set_bit(dw, MASK.ERROR, dwc->mask);
935
936 spin_unlock_bh(&dwc->lock);
937
41d5e59c 938 dev_dbg(chan2dev(chan),
3bfb1d20
HS
939 "alloc_chan_resources allocated %d descriptors\n", i);
940
941 return i;
942}
943
944static void dwc_free_chan_resources(struct dma_chan *chan)
945{
946 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
947 struct dw_dma *dw = to_dw_dma(chan->device);
948 struct dw_desc *desc, *_desc;
949 LIST_HEAD(list);
950
41d5e59c 951 dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n",
3bfb1d20
HS
952 dwc->descs_allocated);
953
954 /* ASSERT: channel is idle */
955 BUG_ON(!list_empty(&dwc->active_list));
956 BUG_ON(!list_empty(&dwc->queue));
957 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
958
959 spin_lock_bh(&dwc->lock);
960 list_splice_init(&dwc->free_list, &list);
961 dwc->descs_allocated = 0;
3bfb1d20
HS
962
963 /* Disable interrupts */
964 channel_clear_bit(dw, MASK.XFER, dwc->mask);
965 channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
966 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
967
968 spin_unlock_bh(&dwc->lock);
969
970 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
41d5e59c
DW
971 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
972 dma_unmap_single(chan2parent(chan), desc->txd.phys,
3bfb1d20
HS
973 sizeof(desc->lli), DMA_TO_DEVICE);
974 kfree(desc);
975 }
976
41d5e59c 977 dev_vdbg(chan2dev(chan), "free_chan_resources done\n");
3bfb1d20
HS
978}
979
d9de4519
HCE
980/* --------------------- Cyclic DMA API extensions -------------------- */
981
982/**
983 * dw_dma_cyclic_start - start the cyclic DMA transfer
984 * @chan: the DMA channel to start
985 *
986 * Must be called with soft interrupts disabled. Returns zero on success or
987 * -errno on failure.
988 */
989int dw_dma_cyclic_start(struct dma_chan *chan)
990{
991 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
992 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
993
994 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
995 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
996 return -ENODEV;
997 }
998
999 spin_lock(&dwc->lock);
1000
1001 /* assert channel is idle */
1002 if (dma_readl(dw, CH_EN) & dwc->mask) {
1003 dev_err(chan2dev(&dwc->chan),
1004 "BUG: Attempted to start non-idle channel\n");
1005 dev_err(chan2dev(&dwc->chan),
1006 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
1007 channel_readl(dwc, SAR),
1008 channel_readl(dwc, DAR),
1009 channel_readl(dwc, LLP),
1010 channel_readl(dwc, CTL_HI),
1011 channel_readl(dwc, CTL_LO));
1012 spin_unlock(&dwc->lock);
1013 return -EBUSY;
1014 }
1015
1016 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
1017 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1018 dma_writel(dw, CLEAR.XFER, dwc->mask);
1019
1020 /* setup DMAC channel registers */
1021 channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
1022 channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
1023 channel_writel(dwc, CTL_HI, 0);
1024
1025 channel_set_bit(dw, CH_EN, dwc->mask);
1026
1027 spin_unlock(&dwc->lock);
1028
1029 return 0;
1030}
1031EXPORT_SYMBOL(dw_dma_cyclic_start);
1032
1033/**
1034 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1035 * @chan: the DMA channel to stop
1036 *
1037 * Must be called with soft interrupts disabled.
1038 */
1039void dw_dma_cyclic_stop(struct dma_chan *chan)
1040{
1041 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1042 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1043
1044 spin_lock(&dwc->lock);
1045
1046 channel_clear_bit(dw, CH_EN, dwc->mask);
1047 while (dma_readl(dw, CH_EN) & dwc->mask)
1048 cpu_relax();
1049
1050 spin_unlock(&dwc->lock);
1051}
1052EXPORT_SYMBOL(dw_dma_cyclic_stop);
1053
1054/**
1055 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1056 * @chan: the DMA channel to prepare
1057 * @buf_addr: physical DMA address where the buffer starts
1058 * @buf_len: total number of bytes for the entire buffer
1059 * @period_len: number of bytes for each period
1060 * @direction: transfer direction, to or from device
1061 *
1062 * Must be called before trying to start the transfer. Returns a valid struct
1063 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1064 */
1065struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1066 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
1067 enum dma_data_direction direction)
1068{
1069 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1070 struct dw_cyclic_desc *cdesc;
1071 struct dw_cyclic_desc *retval = NULL;
1072 struct dw_desc *desc;
1073 struct dw_desc *last = NULL;
1074 struct dw_dma_slave *dws = chan->private;
1075 unsigned long was_cyclic;
1076 unsigned int reg_width;
1077 unsigned int periods;
1078 unsigned int i;
1079
1080 spin_lock_bh(&dwc->lock);
1081 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
1082 spin_unlock_bh(&dwc->lock);
1083 dev_dbg(chan2dev(&dwc->chan),
1084 "queue and/or active list are not empty\n");
1085 return ERR_PTR(-EBUSY);
1086 }
1087
1088 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1089 spin_unlock_bh(&dwc->lock);
1090 if (was_cyclic) {
1091 dev_dbg(chan2dev(&dwc->chan),
1092 "channel already prepared for cyclic DMA\n");
1093 return ERR_PTR(-EBUSY);
1094 }
1095
1096 retval = ERR_PTR(-EINVAL);
1097 reg_width = dws->reg_width;
1098 periods = buf_len / period_len;
1099
1100 /* Check for too big/unaligned periods and unaligned DMA buffer. */
1101 if (period_len > (DWC_MAX_COUNT << reg_width))
1102 goto out_err;
1103 if (unlikely(period_len & ((1 << reg_width) - 1)))
1104 goto out_err;
1105 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1106 goto out_err;
1107 if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE))))
1108 goto out_err;
1109
1110 retval = ERR_PTR(-ENOMEM);
1111
1112 if (periods > NR_DESCS_PER_CHANNEL)
1113 goto out_err;
1114
1115 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1116 if (!cdesc)
1117 goto out_err;
1118
1119 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1120 if (!cdesc->desc)
1121 goto out_err_alloc;
1122
1123 for (i = 0; i < periods; i++) {
1124 desc = dwc_desc_get(dwc);
1125 if (!desc)
1126 goto out_err_desc_get;
1127
1128 switch (direction) {
1129 case DMA_TO_DEVICE:
1130 desc->lli.dar = dws->tx_reg;
1131 desc->lli.sar = buf_addr + (period_len * i);
1132 desc->lli.ctllo = (DWC_DEFAULT_CTLLO
1133 | DWC_CTLL_DST_WIDTH(reg_width)
1134 | DWC_CTLL_SRC_WIDTH(reg_width)
1135 | DWC_CTLL_DST_FIX
1136 | DWC_CTLL_SRC_INC
1137 | DWC_CTLL_FC_M2P
1138 | DWC_CTLL_INT_EN);
1139 break;
1140 case DMA_FROM_DEVICE:
1141 desc->lli.dar = buf_addr + (period_len * i);
1142 desc->lli.sar = dws->rx_reg;
1143 desc->lli.ctllo = (DWC_DEFAULT_CTLLO
1144 | DWC_CTLL_SRC_WIDTH(reg_width)
1145 | DWC_CTLL_DST_WIDTH(reg_width)
1146 | DWC_CTLL_DST_INC
1147 | DWC_CTLL_SRC_FIX
1148 | DWC_CTLL_FC_P2M
1149 | DWC_CTLL_INT_EN);
1150 break;
1151 default:
1152 break;
1153 }
1154
1155 desc->lli.ctlhi = (period_len >> reg_width);
1156 cdesc->desc[i] = desc;
1157
1158 if (last) {
1159 last->lli.llp = desc->txd.phys;
1160 dma_sync_single_for_device(chan2parent(chan),
1161 last->txd.phys, sizeof(last->lli),
1162 DMA_TO_DEVICE);
1163 }
1164
1165 last = desc;
1166 }
1167
1168 /* lets make a cyclic list */
1169 last->lli.llp = cdesc->desc[0]->txd.phys;
1170 dma_sync_single_for_device(chan2parent(chan), last->txd.phys,
1171 sizeof(last->lli), DMA_TO_DEVICE);
1172
1173 dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%08x len %zu "
1174 "period %zu periods %d\n", buf_addr, buf_len,
1175 period_len, periods);
1176
1177 cdesc->periods = periods;
1178 dwc->cdesc = cdesc;
1179
1180 return cdesc;
1181
1182out_err_desc_get:
1183 while (i--)
1184 dwc_desc_put(dwc, cdesc->desc[i]);
1185out_err_alloc:
1186 kfree(cdesc);
1187out_err:
1188 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1189 return (struct dw_cyclic_desc *)retval;
1190}
1191EXPORT_SYMBOL(dw_dma_cyclic_prep);
1192
1193/**
1194 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1195 * @chan: the DMA channel to free
1196 */
1197void dw_dma_cyclic_free(struct dma_chan *chan)
1198{
1199 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1200 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1201 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1202 int i;
1203
1204 dev_dbg(chan2dev(&dwc->chan), "cyclic free\n");
1205
1206 if (!cdesc)
1207 return;
1208
1209 spin_lock_bh(&dwc->lock);
1210
1211 channel_clear_bit(dw, CH_EN, dwc->mask);
1212 while (dma_readl(dw, CH_EN) & dwc->mask)
1213 cpu_relax();
1214
1215 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
1216 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1217 dma_writel(dw, CLEAR.XFER, dwc->mask);
1218
1219 spin_unlock_bh(&dwc->lock);
1220
1221 for (i = 0; i < cdesc->periods; i++)
1222 dwc_desc_put(dwc, cdesc->desc[i]);
1223
1224 kfree(cdesc->desc);
1225 kfree(cdesc);
1226
1227 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1228}
1229EXPORT_SYMBOL(dw_dma_cyclic_free);
1230
3bfb1d20
HS
1231/*----------------------------------------------------------------------*/
1232
1233static void dw_dma_off(struct dw_dma *dw)
1234{
1235 dma_writel(dw, CFG, 0);
1236
1237 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
1238 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1239 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1240 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1241 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1242
1243 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1244 cpu_relax();
1245}
1246
1247static int __init dw_probe(struct platform_device *pdev)
1248{
1249 struct dw_dma_platform_data *pdata;
1250 struct resource *io;
1251 struct dw_dma *dw;
1252 size_t size;
1253 int irq;
1254 int err;
1255 int i;
1256
1257 pdata = pdev->dev.platform_data;
1258 if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
1259 return -EINVAL;
1260
1261 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1262 if (!io)
1263 return -EINVAL;
1264
1265 irq = platform_get_irq(pdev, 0);
1266 if (irq < 0)
1267 return irq;
1268
1269 size = sizeof(struct dw_dma);
1270 size += pdata->nr_channels * sizeof(struct dw_dma_chan);
1271 dw = kzalloc(size, GFP_KERNEL);
1272 if (!dw)
1273 return -ENOMEM;
1274
1275 if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
1276 err = -EBUSY;
1277 goto err_kfree;
1278 }
1279
3bfb1d20
HS
1280 dw->regs = ioremap(io->start, DW_REGLEN);
1281 if (!dw->regs) {
1282 err = -ENOMEM;
1283 goto err_release_r;
1284 }
1285
1286 dw->clk = clk_get(&pdev->dev, "hclk");
1287 if (IS_ERR(dw->clk)) {
1288 err = PTR_ERR(dw->clk);
1289 goto err_clk;
1290 }
1291 clk_enable(dw->clk);
1292
1293 /* force dma off, just in case */
1294 dw_dma_off(dw);
1295
1296 err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
1297 if (err)
1298 goto err_irq;
1299
1300 platform_set_drvdata(pdev, dw);
1301
1302 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1303
1304 dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
1305
1306 INIT_LIST_HEAD(&dw->dma.channels);
1307 for (i = 0; i < pdata->nr_channels; i++, dw->dma.chancnt++) {
1308 struct dw_dma_chan *dwc = &dw->chan[i];
1309
1310 dwc->chan.device = &dw->dma;
1311 dwc->chan.cookie = dwc->completed = 1;
1312 dwc->chan.chan_id = i;
1313 list_add_tail(&dwc->chan.device_node, &dw->dma.channels);
1314
1315 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1316 spin_lock_init(&dwc->lock);
1317 dwc->mask = 1 << i;
1318
1319 INIT_LIST_HEAD(&dwc->active_list);
1320 INIT_LIST_HEAD(&dwc->queue);
1321 INIT_LIST_HEAD(&dwc->free_list);
1322
1323 channel_clear_bit(dw, CH_EN, dwc->mask);
1324 }
1325
1326 /* Clear/disable all interrupts on all channels. */
1327 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
1328 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
1329 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1330 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1331 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1332
1333 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
1334 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1335 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1336 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1337 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1338
1339 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1340 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
1341 dw->dma.dev = &pdev->dev;
1342 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1343 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1344
1345 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
1346
1347 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
c3635c78 1348 dw->dma.device_control = dwc_control;
3bfb1d20 1349
07934481 1350 dw->dma.device_tx_status = dwc_tx_status;
3bfb1d20
HS
1351 dw->dma.device_issue_pending = dwc_issue_pending;
1352
1353 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1354
1355 printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
dfbc9019 1356 dev_name(&pdev->dev), dw->dma.chancnt);
3bfb1d20
HS
1357
1358 dma_async_device_register(&dw->dma);
1359
1360 return 0;
1361
1362err_irq:
1363 clk_disable(dw->clk);
1364 clk_put(dw->clk);
1365err_clk:
1366 iounmap(dw->regs);
1367 dw->regs = NULL;
1368err_release_r:
1369 release_resource(io);
1370err_kfree:
1371 kfree(dw);
1372 return err;
1373}
1374
1375static int __exit dw_remove(struct platform_device *pdev)
1376{
1377 struct dw_dma *dw = platform_get_drvdata(pdev);
1378 struct dw_dma_chan *dwc, *_dwc;
1379 struct resource *io;
1380
1381 dw_dma_off(dw);
1382 dma_async_device_unregister(&dw->dma);
1383
1384 free_irq(platform_get_irq(pdev, 0), dw);
1385 tasklet_kill(&dw->tasklet);
1386
1387 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1388 chan.device_node) {
1389 list_del(&dwc->chan.device_node);
1390 channel_clear_bit(dw, CH_EN, dwc->mask);
1391 }
1392
1393 clk_disable(dw->clk);
1394 clk_put(dw->clk);
1395
1396 iounmap(dw->regs);
1397 dw->regs = NULL;
1398
1399 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1400 release_mem_region(io->start, DW_REGLEN);
1401
1402 kfree(dw);
1403
1404 return 0;
1405}
1406
1407static void dw_shutdown(struct platform_device *pdev)
1408{
1409 struct dw_dma *dw = platform_get_drvdata(pdev);
1410
1411 dw_dma_off(platform_get_drvdata(pdev));
1412 clk_disable(dw->clk);
1413}
1414
4a256b5f 1415static int dw_suspend_noirq(struct device *dev)
3bfb1d20 1416{
4a256b5f 1417 struct platform_device *pdev = to_platform_device(dev);
3bfb1d20
HS
1418 struct dw_dma *dw = platform_get_drvdata(pdev);
1419
1420 dw_dma_off(platform_get_drvdata(pdev));
1421 clk_disable(dw->clk);
1422 return 0;
1423}
1424
4a256b5f 1425static int dw_resume_noirq(struct device *dev)
3bfb1d20 1426{
4a256b5f 1427 struct platform_device *pdev = to_platform_device(dev);
3bfb1d20
HS
1428 struct dw_dma *dw = platform_get_drvdata(pdev);
1429
1430 clk_enable(dw->clk);
1431 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1432 return 0;
3bfb1d20
HS
1433}
1434
47145210 1435static const struct dev_pm_ops dw_dev_pm_ops = {
4a256b5f
MD
1436 .suspend_noirq = dw_suspend_noirq,
1437 .resume_noirq = dw_resume_noirq,
1438};
1439
3bfb1d20
HS
1440static struct platform_driver dw_driver = {
1441 .remove = __exit_p(dw_remove),
1442 .shutdown = dw_shutdown,
3bfb1d20
HS
1443 .driver = {
1444 .name = "dw_dmac",
4a256b5f 1445 .pm = &dw_dev_pm_ops,
3bfb1d20
HS
1446 },
1447};
1448
1449static int __init dw_init(void)
1450{
1451 return platform_driver_probe(&dw_driver, dw_probe);
1452}
1453module_init(dw_init);
1454
1455static void __exit dw_exit(void)
1456{
1457 platform_driver_unregister(&dw_driver);
1458}
1459module_exit(dw_exit);
1460
1461MODULE_LICENSE("GPL v2");
1462MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
1463MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
This page took 0.254903 seconds and 5 git commands to generate.