]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/dma/dw_dmac.c
dmaengine: kill struct dma_client and supporting infrastructure
[mirror_ubuntu-zesty-kernel.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
73static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
74{
75 return list_entry(dwc->active_list.next, struct dw_desc, desc_node);
76}
77
78static struct dw_desc *dwc_first_queued(struct dw_dma_chan *dwc)
79{
80 return list_entry(dwc->queue.next, struct dw_desc, desc_node);
81}
82
83static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
84{
85 struct dw_desc *desc, *_desc;
86 struct dw_desc *ret = NULL;
87 unsigned int i = 0;
88
89 spin_lock_bh(&dwc->lock);
90 list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
91 if (async_tx_test_ack(&desc->txd)) {
92 list_del(&desc->desc_node);
93 ret = desc;
94 break;
95 }
96 dev_dbg(&dwc->chan.dev, "desc %p not ACKed\n", desc);
97 i++;
98 }
99 spin_unlock_bh(&dwc->lock);
100
101 dev_vdbg(&dwc->chan.dev, "scanned %u descriptors on freelist\n", i);
102
103 return ret;
104}
105
106static void dwc_sync_desc_for_cpu(struct dw_dma_chan *dwc, struct dw_desc *desc)
107{
108 struct dw_desc *child;
109
110 list_for_each_entry(child, &desc->txd.tx_list, desc_node)
111 dma_sync_single_for_cpu(dwc->chan.dev.parent,
112 child->txd.phys, sizeof(child->lli),
113 DMA_TO_DEVICE);
114 dma_sync_single_for_cpu(dwc->chan.dev.parent,
115 desc->txd.phys, sizeof(desc->lli),
116 DMA_TO_DEVICE);
117}
118
119/*
120 * Move a descriptor, including any children, to the free list.
121 * `desc' must not be on any lists.
122 */
123static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
124{
125 if (desc) {
126 struct dw_desc *child;
127
128 dwc_sync_desc_for_cpu(dwc, desc);
129
130 spin_lock_bh(&dwc->lock);
131 list_for_each_entry(child, &desc->txd.tx_list, desc_node)
132 dev_vdbg(&dwc->chan.dev,
133 "moving child desc %p to freelist\n",
134 child);
135 list_splice_init(&desc->txd.tx_list, &dwc->free_list);
136 dev_vdbg(&dwc->chan.dev, "moving desc %p to freelist\n", desc);
137 list_add(&desc->desc_node, &dwc->free_list);
138 spin_unlock_bh(&dwc->lock);
139 }
140}
141
142/* Called with dwc->lock held and bh disabled */
143static dma_cookie_t
144dwc_assign_cookie(struct dw_dma_chan *dwc, struct dw_desc *desc)
145{
146 dma_cookie_t cookie = dwc->chan.cookie;
147
148 if (++cookie < 0)
149 cookie = 1;
150
151 dwc->chan.cookie = cookie;
152 desc->txd.cookie = cookie;
153
154 return cookie;
155}
156
157/*----------------------------------------------------------------------*/
158
159/* Called with dwc->lock held and bh disabled */
160static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
161{
162 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
163
164 /* ASSERT: channel is idle */
165 if (dma_readl(dw, CH_EN) & dwc->mask) {
166 dev_err(&dwc->chan.dev,
167 "BUG: Attempted to start non-idle channel\n");
168 dev_err(&dwc->chan.dev,
169 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
170 channel_readl(dwc, SAR),
171 channel_readl(dwc, DAR),
172 channel_readl(dwc, LLP),
173 channel_readl(dwc, CTL_HI),
174 channel_readl(dwc, CTL_LO));
175
176 /* The tasklet will hopefully advance the queue... */
177 return;
178 }
179
180 channel_writel(dwc, LLP, first->txd.phys);
181 channel_writel(dwc, CTL_LO,
182 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
183 channel_writel(dwc, CTL_HI, 0);
184 channel_set_bit(dw, CH_EN, dwc->mask);
185}
186
187/*----------------------------------------------------------------------*/
188
189static void
190dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc)
191{
192 dma_async_tx_callback callback;
193 void *param;
194 struct dma_async_tx_descriptor *txd = &desc->txd;
195
196 dev_vdbg(&dwc->chan.dev, "descriptor %u complete\n", txd->cookie);
197
198 dwc->completed = txd->cookie;
199 callback = txd->callback;
200 param = txd->callback_param;
201
202 dwc_sync_desc_for_cpu(dwc, desc);
203 list_splice_init(&txd->tx_list, &dwc->free_list);
204 list_move(&desc->desc_node, &dwc->free_list);
205
206 /*
207 * We use dma_unmap_page() regardless of how the buffers were
208 * mapped before they were submitted...
209 */
210 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP))
211 dma_unmap_page(dwc->chan.dev.parent, desc->lli.dar, desc->len,
212 DMA_FROM_DEVICE);
213 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP))
214 dma_unmap_page(dwc->chan.dev.parent, desc->lli.sar, desc->len,
215 DMA_TO_DEVICE);
216
217 /*
218 * The API requires that no submissions are done from a
219 * callback, so we don't need to drop the lock here
220 */
221 if (callback)
222 callback(param);
223}
224
225static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
226{
227 struct dw_desc *desc, *_desc;
228 LIST_HEAD(list);
229
230 if (dma_readl(dw, CH_EN) & dwc->mask) {
231 dev_err(&dwc->chan.dev,
232 "BUG: XFER bit set, but channel not idle!\n");
233
234 /* Try to continue after resetting the channel... */
235 channel_clear_bit(dw, CH_EN, dwc->mask);
236 while (dma_readl(dw, CH_EN) & dwc->mask)
237 cpu_relax();
238 }
239
240 /*
241 * Submit queued descriptors ASAP, i.e. before we go through
242 * the completed ones.
243 */
244 if (!list_empty(&dwc->queue))
245 dwc_dostart(dwc, dwc_first_queued(dwc));
246 list_splice_init(&dwc->active_list, &list);
247 list_splice_init(&dwc->queue, &dwc->active_list);
248
249 list_for_each_entry_safe(desc, _desc, &list, desc_node)
250 dwc_descriptor_complete(dwc, desc);
251}
252
253static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
254{
255 dma_addr_t llp;
256 struct dw_desc *desc, *_desc;
257 struct dw_desc *child;
258 u32 status_xfer;
259
260 /*
261 * Clear block interrupt flag before scanning so that we don't
262 * miss any, and read LLP before RAW_XFER to ensure it is
263 * valid if we decide to scan the list.
264 */
265 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
266 llp = channel_readl(dwc, LLP);
267 status_xfer = dma_readl(dw, RAW.XFER);
268
269 if (status_xfer & dwc->mask) {
270 /* Everything we've submitted is done */
271 dma_writel(dw, CLEAR.XFER, dwc->mask);
272 dwc_complete_all(dw, dwc);
273 return;
274 }
275
276 dev_vdbg(&dwc->chan.dev, "scan_descriptors: llp=0x%x\n", llp);
277
278 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
279 if (desc->lli.llp == llp)
280 /* This one is currently in progress */
281 return;
282
283 list_for_each_entry(child, &desc->txd.tx_list, desc_node)
284 if (child->lli.llp == llp)
285 /* Currently in progress */
286 return;
287
288 /*
289 * No descriptors so far seem to be in progress, i.e.
290 * this one must be done.
291 */
292 dwc_descriptor_complete(dwc, desc);
293 }
294
295 dev_err(&dwc->chan.dev,
296 "BUG: All descriptors done, but channel not idle!\n");
297
298 /* Try to continue after resetting the channel... */
299 channel_clear_bit(dw, CH_EN, dwc->mask);
300 while (dma_readl(dw, CH_EN) & dwc->mask)
301 cpu_relax();
302
303 if (!list_empty(&dwc->queue)) {
304 dwc_dostart(dwc, dwc_first_queued(dwc));
305 list_splice_init(&dwc->queue, &dwc->active_list);
306 }
307}
308
309static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
310{
311 dev_printk(KERN_CRIT, &dwc->chan.dev,
312 " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
313 lli->sar, lli->dar, lli->llp,
314 lli->ctlhi, lli->ctllo);
315}
316
317static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
318{
319 struct dw_desc *bad_desc;
320 struct dw_desc *child;
321
322 dwc_scan_descriptors(dw, dwc);
323
324 /*
325 * The descriptor currently at the head of the active list is
326 * borked. Since we don't have any way to report errors, we'll
327 * just have to scream loudly and try to carry on.
328 */
329 bad_desc = dwc_first_active(dwc);
330 list_del_init(&bad_desc->desc_node);
331 list_splice_init(&dwc->queue, dwc->active_list.prev);
332
333 /* Clear the error flag and try to restart the controller */
334 dma_writel(dw, CLEAR.ERROR, dwc->mask);
335 if (!list_empty(&dwc->active_list))
336 dwc_dostart(dwc, dwc_first_active(dwc));
337
338 /*
339 * KERN_CRITICAL may seem harsh, but since this only happens
340 * when someone submits a bad physical address in a
341 * descriptor, we should consider ourselves lucky that the
342 * controller flagged an error instead of scribbling over
343 * random memory locations.
344 */
345 dev_printk(KERN_CRIT, &dwc->chan.dev,
346 "Bad descriptor submitted for DMA!\n");
347 dev_printk(KERN_CRIT, &dwc->chan.dev,
348 " cookie: %d\n", bad_desc->txd.cookie);
349 dwc_dump_lli(dwc, &bad_desc->lli);
350 list_for_each_entry(child, &bad_desc->txd.tx_list, desc_node)
351 dwc_dump_lli(dwc, &child->lli);
352
353 /* Pretend the descriptor completed successfully */
354 dwc_descriptor_complete(dwc, bad_desc);
355}
356
357static void dw_dma_tasklet(unsigned long data)
358{
359 struct dw_dma *dw = (struct dw_dma *)data;
360 struct dw_dma_chan *dwc;
361 u32 status_block;
362 u32 status_xfer;
363 u32 status_err;
364 int i;
365
366 status_block = dma_readl(dw, RAW.BLOCK);
7fe7b2f4 367 status_xfer = dma_readl(dw, RAW.XFER);
3bfb1d20
HS
368 status_err = dma_readl(dw, RAW.ERROR);
369
370 dev_vdbg(dw->dma.dev, "tasklet: status_block=%x status_err=%x\n",
371 status_block, status_err);
372
373 for (i = 0; i < dw->dma.chancnt; i++) {
374 dwc = &dw->chan[i];
375 spin_lock(&dwc->lock);
376 if (status_err & (1 << i))
377 dwc_handle_error(dw, dwc);
378 else if ((status_block | status_xfer) & (1 << i))
379 dwc_scan_descriptors(dw, dwc);
380 spin_unlock(&dwc->lock);
381 }
382
383 /*
384 * Re-enable interrupts. Block Complete interrupts are only
385 * enabled if the INT_EN bit in the descriptor is set. This
386 * will trigger a scan before the whole list is done.
387 */
388 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
389 channel_set_bit(dw, MASK.BLOCK, dw->all_chan_mask);
390 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
391}
392
393static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
394{
395 struct dw_dma *dw = dev_id;
396 u32 status;
397
398 dev_vdbg(dw->dma.dev, "interrupt: status=0x%x\n",
399 dma_readl(dw, STATUS_INT));
400
401 /*
402 * Just disable the interrupts. We'll turn them back on in the
403 * softirq handler.
404 */
405 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
406 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
407 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
408
409 status = dma_readl(dw, STATUS_INT);
410 if (status) {
411 dev_err(dw->dma.dev,
412 "BUG: Unexpected interrupts pending: 0x%x\n",
413 status);
414
415 /* Try to recover */
416 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
417 channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
418 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
419 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
420 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
421 }
422
423 tasklet_schedule(&dw->tasklet);
424
425 return IRQ_HANDLED;
426}
427
428/*----------------------------------------------------------------------*/
429
430static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
431{
432 struct dw_desc *desc = txd_to_dw_desc(tx);
433 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
434 dma_cookie_t cookie;
435
436 spin_lock_bh(&dwc->lock);
437 cookie = dwc_assign_cookie(dwc, desc);
438
439 /*
440 * REVISIT: We should attempt to chain as many descriptors as
441 * possible, perhaps even appending to those already submitted
442 * for DMA. But this is hard to do in a race-free manner.
443 */
444 if (list_empty(&dwc->active_list)) {
445 dev_vdbg(&tx->chan->dev, "tx_submit: started %u\n",
446 desc->txd.cookie);
447 dwc_dostart(dwc, desc);
448 list_add_tail(&desc->desc_node, &dwc->active_list);
449 } else {
450 dev_vdbg(&tx->chan->dev, "tx_submit: queued %u\n",
451 desc->txd.cookie);
452
453 list_add_tail(&desc->desc_node, &dwc->queue);
454 }
455
456 spin_unlock_bh(&dwc->lock);
457
458 return cookie;
459}
460
461static struct dma_async_tx_descriptor *
462dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
463 size_t len, unsigned long flags)
464{
465 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
466 struct dw_desc *desc;
467 struct dw_desc *first;
468 struct dw_desc *prev;
469 size_t xfer_count;
470 size_t offset;
471 unsigned int src_width;
472 unsigned int dst_width;
473 u32 ctllo;
474
475 dev_vdbg(&chan->dev, "prep_dma_memcpy d0x%x s0x%x l0x%zx f0x%lx\n",
476 dest, src, len, flags);
477
478 if (unlikely(!len)) {
479 dev_dbg(&chan->dev, "prep_dma_memcpy: length is zero!\n");
480 return NULL;
481 }
482
483 /*
484 * We can be a lot more clever here, but this should take care
485 * of the most common optimization.
486 */
487 if (!((src | dest | len) & 3))
488 src_width = dst_width = 2;
489 else if (!((src | dest | len) & 1))
490 src_width = dst_width = 1;
491 else
492 src_width = dst_width = 0;
493
494 ctllo = DWC_DEFAULT_CTLLO
495 | DWC_CTLL_DST_WIDTH(dst_width)
496 | DWC_CTLL_SRC_WIDTH(src_width)
497 | DWC_CTLL_DST_INC
498 | DWC_CTLL_SRC_INC
499 | DWC_CTLL_FC_M2M;
500 prev = first = NULL;
501
502 for (offset = 0; offset < len; offset += xfer_count << src_width) {
503 xfer_count = min_t(size_t, (len - offset) >> src_width,
504 DWC_MAX_COUNT);
505
506 desc = dwc_desc_get(dwc);
507 if (!desc)
508 goto err_desc_get;
509
510 desc->lli.sar = src + offset;
511 desc->lli.dar = dest + offset;
512 desc->lli.ctllo = ctllo;
513 desc->lli.ctlhi = xfer_count;
514
515 if (!first) {
516 first = desc;
517 } else {
518 prev->lli.llp = desc->txd.phys;
519 dma_sync_single_for_device(chan->dev.parent,
520 prev->txd.phys, sizeof(prev->lli),
521 DMA_TO_DEVICE);
522 list_add_tail(&desc->desc_node,
523 &first->txd.tx_list);
524 }
525 prev = desc;
526 }
527
528
529 if (flags & DMA_PREP_INTERRUPT)
530 /* Trigger interrupt after last block */
531 prev->lli.ctllo |= DWC_CTLL_INT_EN;
532
533 prev->lli.llp = 0;
534 dma_sync_single_for_device(chan->dev.parent,
535 prev->txd.phys, sizeof(prev->lli),
536 DMA_TO_DEVICE);
537
538 first->txd.flags = flags;
539 first->len = len;
540
541 return &first->txd;
542
543err_desc_get:
544 dwc_desc_put(dwc, first);
545 return NULL;
546}
547
548static struct dma_async_tx_descriptor *
549dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
550 unsigned int sg_len, enum dma_data_direction direction,
551 unsigned long flags)
552{
553 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
554 struct dw_dma_slave *dws = dwc->dws;
555 struct dw_desc *prev;
556 struct dw_desc *first;
557 u32 ctllo;
558 dma_addr_t reg;
559 unsigned int reg_width;
560 unsigned int mem_width;
561 unsigned int i;
562 struct scatterlist *sg;
563 size_t total_len = 0;
564
565 dev_vdbg(&chan->dev, "prep_dma_slave\n");
566
567 if (unlikely(!dws || !sg_len))
568 return NULL;
569
74465b4f 570 reg_width = dws->reg_width;
3bfb1d20
HS
571 prev = first = NULL;
572
573 sg_len = dma_map_sg(chan->dev.parent, sgl, sg_len, direction);
574
575 switch (direction) {
576 case DMA_TO_DEVICE:
577 ctllo = (DWC_DEFAULT_CTLLO
578 | DWC_CTLL_DST_WIDTH(reg_width)
579 | DWC_CTLL_DST_FIX
580 | DWC_CTLL_SRC_INC
581 | DWC_CTLL_FC_M2P);
74465b4f 582 reg = dws->tx_reg;
3bfb1d20
HS
583 for_each_sg(sgl, sg, sg_len, i) {
584 struct dw_desc *desc;
585 u32 len;
586 u32 mem;
587
588 desc = dwc_desc_get(dwc);
589 if (!desc) {
590 dev_err(&chan->dev,
591 "not enough descriptors available\n");
592 goto err_desc_get;
593 }
594
595 mem = sg_phys(sg);
596 len = sg_dma_len(sg);
597 mem_width = 2;
598 if (unlikely(mem & 3 || len & 3))
599 mem_width = 0;
600
601 desc->lli.sar = mem;
602 desc->lli.dar = reg;
603 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
604 desc->lli.ctlhi = len >> mem_width;
605
606 if (!first) {
607 first = desc;
608 } else {
609 prev->lli.llp = desc->txd.phys;
610 dma_sync_single_for_device(chan->dev.parent,
611 prev->txd.phys,
612 sizeof(prev->lli),
613 DMA_TO_DEVICE);
614 list_add_tail(&desc->desc_node,
615 &first->txd.tx_list);
616 }
617 prev = desc;
618 total_len += len;
619 }
620 break;
621 case DMA_FROM_DEVICE:
622 ctllo = (DWC_DEFAULT_CTLLO
623 | DWC_CTLL_SRC_WIDTH(reg_width)
624 | DWC_CTLL_DST_INC
625 | DWC_CTLL_SRC_FIX
626 | DWC_CTLL_FC_P2M);
627
74465b4f 628 reg = dws->rx_reg;
3bfb1d20
HS
629 for_each_sg(sgl, sg, sg_len, i) {
630 struct dw_desc *desc;
631 u32 len;
632 u32 mem;
633
634 desc = dwc_desc_get(dwc);
635 if (!desc) {
636 dev_err(&chan->dev,
637 "not enough descriptors available\n");
638 goto err_desc_get;
639 }
640
641 mem = sg_phys(sg);
642 len = sg_dma_len(sg);
643 mem_width = 2;
644 if (unlikely(mem & 3 || len & 3))
645 mem_width = 0;
646
647 desc->lli.sar = reg;
648 desc->lli.dar = mem;
649 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
650 desc->lli.ctlhi = len >> reg_width;
651
652 if (!first) {
653 first = desc;
654 } else {
655 prev->lli.llp = desc->txd.phys;
656 dma_sync_single_for_device(chan->dev.parent,
657 prev->txd.phys,
658 sizeof(prev->lli),
659 DMA_TO_DEVICE);
660 list_add_tail(&desc->desc_node,
661 &first->txd.tx_list);
662 }
663 prev = desc;
664 total_len += len;
665 }
666 break;
667 default:
668 return NULL;
669 }
670
671 if (flags & DMA_PREP_INTERRUPT)
672 /* Trigger interrupt after last block */
673 prev->lli.ctllo |= DWC_CTLL_INT_EN;
674
675 prev->lli.llp = 0;
676 dma_sync_single_for_device(chan->dev.parent,
677 prev->txd.phys, sizeof(prev->lli),
678 DMA_TO_DEVICE);
679
680 first->len = total_len;
681
682 return &first->txd;
683
684err_desc_get:
685 dwc_desc_put(dwc, first);
686 return NULL;
687}
688
689static void dwc_terminate_all(struct dma_chan *chan)
690{
691 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
692 struct dw_dma *dw = to_dw_dma(chan->device);
693 struct dw_desc *desc, *_desc;
694 LIST_HEAD(list);
695
696 /*
697 * This is only called when something went wrong elsewhere, so
698 * we don't really care about the data. Just disable the
699 * channel. We still have to poll the channel enable bit due
700 * to AHB/HSB limitations.
701 */
702 spin_lock_bh(&dwc->lock);
703
704 channel_clear_bit(dw, CH_EN, dwc->mask);
705
706 while (dma_readl(dw, CH_EN) & dwc->mask)
707 cpu_relax();
708
709 /* active_list entries will end up before queued entries */
710 list_splice_init(&dwc->queue, &list);
711 list_splice_init(&dwc->active_list, &list);
712
713 spin_unlock_bh(&dwc->lock);
714
715 /* Flush all pending and queued descriptors */
716 list_for_each_entry_safe(desc, _desc, &list, desc_node)
717 dwc_descriptor_complete(dwc, desc);
718}
719
720static enum dma_status
721dwc_is_tx_complete(struct dma_chan *chan,
722 dma_cookie_t cookie,
723 dma_cookie_t *done, dma_cookie_t *used)
724{
725 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
726 dma_cookie_t last_used;
727 dma_cookie_t last_complete;
728 int ret;
729
730 last_complete = dwc->completed;
731 last_used = chan->cookie;
732
733 ret = dma_async_is_complete(cookie, last_complete, last_used);
734 if (ret != DMA_SUCCESS) {
735 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
736
737 last_complete = dwc->completed;
738 last_used = chan->cookie;
739
740 ret = dma_async_is_complete(cookie, last_complete, last_used);
741 }
742
743 if (done)
744 *done = last_complete;
745 if (used)
746 *used = last_used;
747
748 return ret;
749}
750
751static void dwc_issue_pending(struct dma_chan *chan)
752{
753 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
754
755 spin_lock_bh(&dwc->lock);
756 if (!list_empty(&dwc->queue))
757 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
758 spin_unlock_bh(&dwc->lock);
759}
760
aa1e6f1a 761static int dwc_alloc_chan_resources(struct dma_chan *chan)
3bfb1d20
HS
762{
763 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
764 struct dw_dma *dw = to_dw_dma(chan->device);
765 struct dw_desc *desc;
3bfb1d20
HS
766 struct dw_dma_slave *dws;
767 int i;
768 u32 cfghi;
769 u32 cfglo;
770
771 dev_vdbg(&chan->dev, "alloc_chan_resources\n");
772
3bfb1d20
HS
773 /* ASSERT: channel is idle */
774 if (dma_readl(dw, CH_EN) & dwc->mask) {
775 dev_dbg(&chan->dev, "DMA channel not idle?\n");
776 return -EIO;
777 }
778
779 dwc->completed = chan->cookie = 1;
780
781 cfghi = DWC_CFGH_FIFO_MODE;
782 cfglo = 0;
783
74465b4f
DW
784 dws = dwc->dws;
785 if (dws) {
3bfb1d20
HS
786 /*
787 * We need controller-specific data to set up slave
788 * transfers.
789 */
74465b4f 790 BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
3bfb1d20 791
3bfb1d20
HS
792 cfghi = dws->cfg_hi;
793 cfglo = dws->cfg_lo;
3bfb1d20 794 }
3bfb1d20
HS
795 channel_writel(dwc, CFG_LO, cfglo);
796 channel_writel(dwc, CFG_HI, cfghi);
797
798 /*
799 * NOTE: some controllers may have additional features that we
800 * need to initialize here, like "scatter-gather" (which
801 * doesn't mean what you think it means), and status writeback.
802 */
803
804 spin_lock_bh(&dwc->lock);
805 i = dwc->descs_allocated;
806 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
807 spin_unlock_bh(&dwc->lock);
808
809 desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
810 if (!desc) {
811 dev_info(&chan->dev,
812 "only allocated %d descriptors\n", i);
813 spin_lock_bh(&dwc->lock);
814 break;
815 }
816
817 dma_async_tx_descriptor_init(&desc->txd, chan);
818 desc->txd.tx_submit = dwc_tx_submit;
819 desc->txd.flags = DMA_CTRL_ACK;
820 INIT_LIST_HEAD(&desc->txd.tx_list);
821 desc->txd.phys = dma_map_single(chan->dev.parent, &desc->lli,
822 sizeof(desc->lli), DMA_TO_DEVICE);
823 dwc_desc_put(dwc, desc);
824
825 spin_lock_bh(&dwc->lock);
826 i = ++dwc->descs_allocated;
827 }
828
829 /* Enable interrupts */
830 channel_set_bit(dw, MASK.XFER, dwc->mask);
831 channel_set_bit(dw, MASK.BLOCK, dwc->mask);
832 channel_set_bit(dw, MASK.ERROR, dwc->mask);
833
834 spin_unlock_bh(&dwc->lock);
835
836 dev_dbg(&chan->dev,
837 "alloc_chan_resources allocated %d descriptors\n", i);
838
839 return i;
840}
841
842static void dwc_free_chan_resources(struct dma_chan *chan)
843{
844 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
845 struct dw_dma *dw = to_dw_dma(chan->device);
846 struct dw_desc *desc, *_desc;
847 LIST_HEAD(list);
848
849 dev_dbg(&chan->dev, "free_chan_resources (descs allocated=%u)\n",
850 dwc->descs_allocated);
851
852 /* ASSERT: channel is idle */
853 BUG_ON(!list_empty(&dwc->active_list));
854 BUG_ON(!list_empty(&dwc->queue));
855 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
856
857 spin_lock_bh(&dwc->lock);
858 list_splice_init(&dwc->free_list, &list);
859 dwc->descs_allocated = 0;
860 dwc->dws = NULL;
861
862 /* Disable interrupts */
863 channel_clear_bit(dw, MASK.XFER, dwc->mask);
864 channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
865 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
866
867 spin_unlock_bh(&dwc->lock);
868
869 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
870 dev_vdbg(&chan->dev, " freeing descriptor %p\n", desc);
871 dma_unmap_single(chan->dev.parent, desc->txd.phys,
872 sizeof(desc->lli), DMA_TO_DEVICE);
873 kfree(desc);
874 }
875
876 dev_vdbg(&chan->dev, "free_chan_resources done\n");
877}
878
879/*----------------------------------------------------------------------*/
880
881static void dw_dma_off(struct dw_dma *dw)
882{
883 dma_writel(dw, CFG, 0);
884
885 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
886 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
887 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
888 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
889 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
890
891 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
892 cpu_relax();
893}
894
895static int __init dw_probe(struct platform_device *pdev)
896{
897 struct dw_dma_platform_data *pdata;
898 struct resource *io;
899 struct dw_dma *dw;
900 size_t size;
901 int irq;
902 int err;
903 int i;
904
905 pdata = pdev->dev.platform_data;
906 if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
907 return -EINVAL;
908
909 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
910 if (!io)
911 return -EINVAL;
912
913 irq = platform_get_irq(pdev, 0);
914 if (irq < 0)
915 return irq;
916
917 size = sizeof(struct dw_dma);
918 size += pdata->nr_channels * sizeof(struct dw_dma_chan);
919 dw = kzalloc(size, GFP_KERNEL);
920 if (!dw)
921 return -ENOMEM;
922
923 if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
924 err = -EBUSY;
925 goto err_kfree;
926 }
927
928 memset(dw, 0, sizeof *dw);
929
930 dw->regs = ioremap(io->start, DW_REGLEN);
931 if (!dw->regs) {
932 err = -ENOMEM;
933 goto err_release_r;
934 }
935
936 dw->clk = clk_get(&pdev->dev, "hclk");
937 if (IS_ERR(dw->clk)) {
938 err = PTR_ERR(dw->clk);
939 goto err_clk;
940 }
941 clk_enable(dw->clk);
942
943 /* force dma off, just in case */
944 dw_dma_off(dw);
945
946 err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
947 if (err)
948 goto err_irq;
949
950 platform_set_drvdata(pdev, dw);
951
952 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
953
954 dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
955
956 INIT_LIST_HEAD(&dw->dma.channels);
957 for (i = 0; i < pdata->nr_channels; i++, dw->dma.chancnt++) {
958 struct dw_dma_chan *dwc = &dw->chan[i];
959
960 dwc->chan.device = &dw->dma;
961 dwc->chan.cookie = dwc->completed = 1;
962 dwc->chan.chan_id = i;
963 list_add_tail(&dwc->chan.device_node, &dw->dma.channels);
964
965 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
966 spin_lock_init(&dwc->lock);
967 dwc->mask = 1 << i;
968
969 INIT_LIST_HEAD(&dwc->active_list);
970 INIT_LIST_HEAD(&dwc->queue);
971 INIT_LIST_HEAD(&dwc->free_list);
972
973 channel_clear_bit(dw, CH_EN, dwc->mask);
974 }
975
976 /* Clear/disable all interrupts on all channels. */
977 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
978 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
979 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
980 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
981 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
982
983 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
984 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
985 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
986 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
987 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
988
989 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
990 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
991 dw->dma.dev = &pdev->dev;
992 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
993 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
994
995 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
996
997 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
998 dw->dma.device_terminate_all = dwc_terminate_all;
999
1000 dw->dma.device_is_tx_complete = dwc_is_tx_complete;
1001 dw->dma.device_issue_pending = dwc_issue_pending;
1002
1003 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1004
1005 printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
1006 pdev->dev.bus_id, dw->dma.chancnt);
1007
1008 dma_async_device_register(&dw->dma);
1009
1010 return 0;
1011
1012err_irq:
1013 clk_disable(dw->clk);
1014 clk_put(dw->clk);
1015err_clk:
1016 iounmap(dw->regs);
1017 dw->regs = NULL;
1018err_release_r:
1019 release_resource(io);
1020err_kfree:
1021 kfree(dw);
1022 return err;
1023}
1024
1025static int __exit dw_remove(struct platform_device *pdev)
1026{
1027 struct dw_dma *dw = platform_get_drvdata(pdev);
1028 struct dw_dma_chan *dwc, *_dwc;
1029 struct resource *io;
1030
1031 dw_dma_off(dw);
1032 dma_async_device_unregister(&dw->dma);
1033
1034 free_irq(platform_get_irq(pdev, 0), dw);
1035 tasklet_kill(&dw->tasklet);
1036
1037 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1038 chan.device_node) {
1039 list_del(&dwc->chan.device_node);
1040 channel_clear_bit(dw, CH_EN, dwc->mask);
1041 }
1042
1043 clk_disable(dw->clk);
1044 clk_put(dw->clk);
1045
1046 iounmap(dw->regs);
1047 dw->regs = NULL;
1048
1049 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1050 release_mem_region(io->start, DW_REGLEN);
1051
1052 kfree(dw);
1053
1054 return 0;
1055}
1056
1057static void dw_shutdown(struct platform_device *pdev)
1058{
1059 struct dw_dma *dw = platform_get_drvdata(pdev);
1060
1061 dw_dma_off(platform_get_drvdata(pdev));
1062 clk_disable(dw->clk);
1063}
1064
1065static int dw_suspend_late(struct platform_device *pdev, pm_message_t mesg)
1066{
1067 struct dw_dma *dw = platform_get_drvdata(pdev);
1068
1069 dw_dma_off(platform_get_drvdata(pdev));
1070 clk_disable(dw->clk);
1071 return 0;
1072}
1073
1074static int dw_resume_early(struct platform_device *pdev)
1075{
1076 struct dw_dma *dw = platform_get_drvdata(pdev);
1077
1078 clk_enable(dw->clk);
1079 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1080 return 0;
1081
1082}
1083
1084static struct platform_driver dw_driver = {
1085 .remove = __exit_p(dw_remove),
1086 .shutdown = dw_shutdown,
1087 .suspend_late = dw_suspend_late,
1088 .resume_early = dw_resume_early,
1089 .driver = {
1090 .name = "dw_dmac",
1091 },
1092};
1093
1094static int __init dw_init(void)
1095{
1096 return platform_driver_probe(&dw_driver, dw_probe);
1097}
1098module_init(dw_init);
1099
1100static void __exit dw_exit(void)
1101{
1102 platform_driver_unregister(&dw_driver);
1103}
1104module_exit(dw_exit);
1105
1106MODULE_LICENSE("GPL v2");
1107MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
1108MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");