2 * Core driver for the Synopsys DesignWare DMA Controller
4 * Copyright (C) 2007-2008 Atmel Corporation
5 * Copyright (C) 2010-2011 ST Microelectronics
6 * Copyright (C) 2013 Intel Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/bitops.h>
14 #include <linux/delay.h>
15 #include <linux/dmaengine.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/dmapool.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/pm_runtime.h>
27 #include "../dmaengine.h"
31 * This supports the Synopsys "DesignWare AHB Central DMA Controller",
32 * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
33 * of which use ARM any more). See the "Databook" from Synopsys for
34 * information beyond what licensees probably provide.
36 * The driver has been tested with the Atmel AT32AP7000, which does not
37 * support descriptor writeback.
40 #define DWC_DEFAULT_CTLLO(_chan) ({ \
41 struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \
42 struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
43 bool _is_slave = is_slave_direction(_dwc->direction); \
44 u8 _smsize = _is_slave ? _sconfig->src_maxburst : \
46 u8 _dmsize = _is_slave ? _sconfig->dst_maxburst : \
49 (DWC_CTLL_DST_MSIZE(_dmsize) \
50 | DWC_CTLL_SRC_MSIZE(_smsize) \
53 | DWC_CTLL_DMS(_dwc->dst_master) \
54 | DWC_CTLL_SMS(_dwc->src_master)); \
58 * Number of descriptors to allocate for each channel. This should be
59 * made configurable somehow; preferably, the clients (at least the
60 * ones using slave transfers) should be able to give us a hint.
62 #define NR_DESCS_PER_CHANNEL 64
64 /* The set of bus widths supported by the DMA controller */
65 #define DW_DMA_BUSWIDTHS \
66 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
67 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
68 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
69 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
71 /*----------------------------------------------------------------------*/
73 static struct device *chan2dev(struct dma_chan *chan)
75 return &chan->dev->device;
78 static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
80 return to_dw_desc(dwc->active_list.next);
83 static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
85 struct dw_desc *desc, *_desc;
86 struct dw_desc *ret = NULL;
90 spin_lock_irqsave(&dwc->lock, flags);
91 list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
93 if (async_tx_test_ack(&desc->txd)) {
94 list_del(&desc->desc_node);
98 dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
100 spin_unlock_irqrestore(&dwc->lock, flags);
102 dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
108 * Move a descriptor, including any children, to the free list.
109 * `desc' must not be on any lists.
111 static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
116 struct dw_desc *child;
118 spin_lock_irqsave(&dwc->lock, flags);
119 list_for_each_entry(child, &desc->tx_list, desc_node)
120 dev_vdbg(chan2dev(&dwc->chan),
121 "moving child desc %p to freelist\n",
123 list_splice_init(&desc->tx_list, &dwc->free_list);
124 dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
125 list_add(&desc->desc_node, &dwc->free_list);
126 spin_unlock_irqrestore(&dwc->lock, flags);
130 static void dwc_initialize(struct dw_dma_chan *dwc)
132 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
133 struct dw_dma_slave *dws = dwc->chan.private;
134 u32 cfghi = DWC_CFGH_FIFO_MODE;
135 u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
137 if (dwc->initialized == true)
142 * We need controller-specific data to set up slave
145 BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
147 cfghi |= DWC_CFGH_DST_PER(dws->dst_id);
148 cfghi |= DWC_CFGH_SRC_PER(dws->src_id);
150 cfghi |= DWC_CFGH_DST_PER(dwc->dst_id);
151 cfghi |= DWC_CFGH_SRC_PER(dwc->src_id);
154 channel_writel(dwc, CFG_LO, cfglo);
155 channel_writel(dwc, CFG_HI, cfghi);
157 /* Enable interrupts */
158 channel_set_bit(dw, MASK.XFER, dwc->mask);
159 channel_set_bit(dw, MASK.BLOCK, dwc->mask);
160 channel_set_bit(dw, MASK.ERROR, dwc->mask);
162 dwc->initialized = true;
165 /*----------------------------------------------------------------------*/
167 static inline unsigned int dwc_fast_ffs(unsigned long long v)
170 * We can be a lot more clever here, but this should take care
171 * of the most common optimization.
182 static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
184 dev_err(chan2dev(&dwc->chan),
185 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
186 channel_readl(dwc, SAR),
187 channel_readl(dwc, DAR),
188 channel_readl(dwc, LLP),
189 channel_readl(dwc, CTL_HI),
190 channel_readl(dwc, CTL_LO));
193 static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
195 channel_clear_bit(dw, CH_EN, dwc->mask);
196 while (dma_readl(dw, CH_EN) & dwc->mask)
200 /*----------------------------------------------------------------------*/
202 /* Perform single block transfer */
203 static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
204 struct dw_desc *desc)
206 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
210 * Software emulation of LLP mode relies on interrupts to continue
211 * multi block transfer.
213 ctllo = desc->lli.ctllo | DWC_CTLL_INT_EN;
215 channel_writel(dwc, SAR, desc->lli.sar);
216 channel_writel(dwc, DAR, desc->lli.dar);
217 channel_writel(dwc, CTL_LO, ctllo);
218 channel_writel(dwc, CTL_HI, desc->lli.ctlhi);
219 channel_set_bit(dw, CH_EN, dwc->mask);
221 /* Move pointer to next descriptor */
222 dwc->tx_node_active = dwc->tx_node_active->next;
225 /* Called with dwc->lock held and bh disabled */
226 static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
228 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
229 unsigned long was_soft_llp;
231 /* ASSERT: channel is idle */
232 if (dma_readl(dw, CH_EN) & dwc->mask) {
233 dev_err(chan2dev(&dwc->chan),
234 "%s: BUG: Attempted to start non-idle channel\n",
236 dwc_dump_chan_regs(dwc);
238 /* The tasklet will hopefully advance the queue... */
243 was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
246 dev_err(chan2dev(&dwc->chan),
247 "BUG: Attempted to start new LLP transfer inside ongoing one\n");
253 dwc->residue = first->total_len;
254 dwc->tx_node_active = &first->tx_list;
256 /* Submit first block */
257 dwc_do_single_block(dwc, first);
264 channel_writel(dwc, LLP, first->txd.phys);
265 channel_writel(dwc, CTL_LO,
266 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
267 channel_writel(dwc, CTL_HI, 0);
268 channel_set_bit(dw, CH_EN, dwc->mask);
271 static void dwc_dostart_first_queued(struct dw_dma_chan *dwc)
273 struct dw_desc *desc;
275 if (list_empty(&dwc->queue))
278 list_move(dwc->queue.next, &dwc->active_list);
279 desc = dwc_first_active(dwc);
280 dev_vdbg(chan2dev(&dwc->chan), "%s: started %u\n", __func__, desc->txd.cookie);
281 dwc_dostart(dwc, desc);
284 /*----------------------------------------------------------------------*/
287 dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
288 bool callback_required)
290 dma_async_tx_callback callback = NULL;
292 struct dma_async_tx_descriptor *txd = &desc->txd;
293 struct dw_desc *child;
296 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
298 spin_lock_irqsave(&dwc->lock, flags);
299 dma_cookie_complete(txd);
300 if (callback_required) {
301 callback = txd->callback;
302 param = txd->callback_param;
306 list_for_each_entry(child, &desc->tx_list, desc_node)
307 async_tx_ack(&child->txd);
308 async_tx_ack(&desc->txd);
310 list_splice_init(&desc->tx_list, &dwc->free_list);
311 list_move(&desc->desc_node, &dwc->free_list);
313 dma_descriptor_unmap(txd);
314 spin_unlock_irqrestore(&dwc->lock, flags);
320 static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
322 struct dw_desc *desc, *_desc;
326 spin_lock_irqsave(&dwc->lock, flags);
327 if (dma_readl(dw, CH_EN) & dwc->mask) {
328 dev_err(chan2dev(&dwc->chan),
329 "BUG: XFER bit set, but channel not idle!\n");
331 /* Try to continue after resetting the channel... */
332 dwc_chan_disable(dw, dwc);
336 * Submit queued descriptors ASAP, i.e. before we go through
337 * the completed ones.
339 list_splice_init(&dwc->active_list, &list);
340 dwc_dostart_first_queued(dwc);
342 spin_unlock_irqrestore(&dwc->lock, flags);
344 list_for_each_entry_safe(desc, _desc, &list, desc_node)
345 dwc_descriptor_complete(dwc, desc, true);
348 /* Returns how many bytes were already received from source */
349 static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
351 u32 ctlhi = channel_readl(dwc, CTL_HI);
352 u32 ctllo = channel_readl(dwc, CTL_LO);
354 return (ctlhi & DWC_CTLH_BLOCK_TS_MASK) * (1 << (ctllo >> 4 & 7));
357 static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
360 struct dw_desc *desc, *_desc;
361 struct dw_desc *child;
365 spin_lock_irqsave(&dwc->lock, flags);
366 llp = channel_readl(dwc, LLP);
367 status_xfer = dma_readl(dw, RAW.XFER);
369 if (status_xfer & dwc->mask) {
370 /* Everything we've submitted is done */
371 dma_writel(dw, CLEAR.XFER, dwc->mask);
373 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
374 struct list_head *head, *active = dwc->tx_node_active;
377 * We are inside first active descriptor.
378 * Otherwise something is really wrong.
380 desc = dwc_first_active(dwc);
382 head = &desc->tx_list;
383 if (active != head) {
384 /* Update desc to reflect last sent one */
385 if (active != head->next)
386 desc = to_dw_desc(active->prev);
388 dwc->residue -= desc->len;
390 child = to_dw_desc(active);
392 /* Submit next block */
393 dwc_do_single_block(dwc, child);
395 spin_unlock_irqrestore(&dwc->lock, flags);
399 /* We are done here */
400 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
405 spin_unlock_irqrestore(&dwc->lock, flags);
407 dwc_complete_all(dw, dwc);
411 if (list_empty(&dwc->active_list)) {
413 spin_unlock_irqrestore(&dwc->lock, flags);
417 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
418 dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
419 spin_unlock_irqrestore(&dwc->lock, flags);
423 dev_vdbg(chan2dev(&dwc->chan), "%s: llp=%pad\n", __func__, &llp);
425 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
426 /* Initial residue value */
427 dwc->residue = desc->total_len;
429 /* Check first descriptors addr */
430 if (desc->txd.phys == llp) {
431 spin_unlock_irqrestore(&dwc->lock, flags);
435 /* Check first descriptors llp */
436 if (desc->lli.llp == llp) {
437 /* This one is currently in progress */
438 dwc->residue -= dwc_get_sent(dwc);
439 spin_unlock_irqrestore(&dwc->lock, flags);
443 dwc->residue -= desc->len;
444 list_for_each_entry(child, &desc->tx_list, desc_node) {
445 if (child->lli.llp == llp) {
446 /* Currently in progress */
447 dwc->residue -= dwc_get_sent(dwc);
448 spin_unlock_irqrestore(&dwc->lock, flags);
451 dwc->residue -= child->len;
455 * No descriptors so far seem to be in progress, i.e.
456 * this one must be done.
458 spin_unlock_irqrestore(&dwc->lock, flags);
459 dwc_descriptor_complete(dwc, desc, true);
460 spin_lock_irqsave(&dwc->lock, flags);
463 dev_err(chan2dev(&dwc->chan),
464 "BUG: All descriptors done, but channel not idle!\n");
466 /* Try to continue after resetting the channel... */
467 dwc_chan_disable(dw, dwc);
469 dwc_dostart_first_queued(dwc);
470 spin_unlock_irqrestore(&dwc->lock, flags);
473 static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
475 dev_crit(chan2dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
476 lli->sar, lli->dar, lli->llp, lli->ctlhi, lli->ctllo);
479 static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
481 struct dw_desc *bad_desc;
482 struct dw_desc *child;
485 dwc_scan_descriptors(dw, dwc);
487 spin_lock_irqsave(&dwc->lock, flags);
490 * The descriptor currently at the head of the active list is
491 * borked. Since we don't have any way to report errors, we'll
492 * just have to scream loudly and try to carry on.
494 bad_desc = dwc_first_active(dwc);
495 list_del_init(&bad_desc->desc_node);
496 list_move(dwc->queue.next, dwc->active_list.prev);
498 /* Clear the error flag and try to restart the controller */
499 dma_writel(dw, CLEAR.ERROR, dwc->mask);
500 if (!list_empty(&dwc->active_list))
501 dwc_dostart(dwc, dwc_first_active(dwc));
504 * WARN may seem harsh, but since this only happens
505 * when someone submits a bad physical address in a
506 * descriptor, we should consider ourselves lucky that the
507 * controller flagged an error instead of scribbling over
508 * random memory locations.
510 dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n"
511 " cookie: %d\n", bad_desc->txd.cookie);
512 dwc_dump_lli(dwc, &bad_desc->lli);
513 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
514 dwc_dump_lli(dwc, &child->lli);
516 spin_unlock_irqrestore(&dwc->lock, flags);
518 /* Pretend the descriptor completed successfully */
519 dwc_descriptor_complete(dwc, bad_desc, true);
522 /* --------------------- Cyclic DMA API extensions -------------------- */
524 dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
526 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
527 return channel_readl(dwc, SAR);
529 EXPORT_SYMBOL(dw_dma_get_src_addr);
531 dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
533 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
534 return channel_readl(dwc, DAR);
536 EXPORT_SYMBOL(dw_dma_get_dst_addr);
538 /* Called with dwc->lock held and all DMAC interrupts disabled */
539 static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
540 u32 status_block, u32 status_err, u32 status_xfer)
544 if (status_block & dwc->mask) {
545 void (*callback)(void *param);
546 void *callback_param;
548 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
549 channel_readl(dwc, LLP));
550 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
552 callback = dwc->cdesc->period_callback;
553 callback_param = dwc->cdesc->period_callback_param;
556 callback(callback_param);
560 * Error and transfer complete are highly unlikely, and will most
561 * likely be due to a configuration error by the user.
563 if (unlikely(status_err & dwc->mask) ||
564 unlikely(status_xfer & dwc->mask)) {
567 dev_err(chan2dev(&dwc->chan),
568 "cyclic DMA unexpected %s interrupt, stopping DMA transfer\n",
569 status_xfer ? "xfer" : "error");
571 spin_lock_irqsave(&dwc->lock, flags);
573 dwc_dump_chan_regs(dwc);
575 dwc_chan_disable(dw, dwc);
577 /* Make sure DMA does not restart by loading a new list */
578 channel_writel(dwc, LLP, 0);
579 channel_writel(dwc, CTL_LO, 0);
580 channel_writel(dwc, CTL_HI, 0);
582 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
583 dma_writel(dw, CLEAR.ERROR, dwc->mask);
584 dma_writel(dw, CLEAR.XFER, dwc->mask);
586 for (i = 0; i < dwc->cdesc->periods; i++)
587 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
589 spin_unlock_irqrestore(&dwc->lock, flags);
593 /* ------------------------------------------------------------------------- */
595 static void dw_dma_tasklet(unsigned long data)
597 struct dw_dma *dw = (struct dw_dma *)data;
598 struct dw_dma_chan *dwc;
604 status_block = dma_readl(dw, RAW.BLOCK);
605 status_xfer = dma_readl(dw, RAW.XFER);
606 status_err = dma_readl(dw, RAW.ERROR);
608 dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
610 for (i = 0; i < dw->dma.chancnt; i++) {
612 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
613 dwc_handle_cyclic(dw, dwc, status_block, status_err,
615 else if (status_err & (1 << i))
616 dwc_handle_error(dw, dwc);
617 else if (status_xfer & (1 << i))
618 dwc_scan_descriptors(dw, dwc);
622 * Re-enable interrupts.
624 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
625 channel_set_bit(dw, MASK.BLOCK, dw->all_chan_mask);
626 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
629 static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
631 struct dw_dma *dw = dev_id;
632 u32 status = dma_readl(dw, STATUS_INT);
634 dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, status);
636 /* Check if we have any interrupt from the DMAC */
637 if (!status || !dw->in_use)
641 * Just disable the interrupts. We'll turn them back on in the
644 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
645 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
646 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
648 status = dma_readl(dw, STATUS_INT);
651 "BUG: Unexpected interrupts pending: 0x%x\n",
655 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
656 channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
657 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
658 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
659 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
662 tasklet_schedule(&dw->tasklet);
667 /*----------------------------------------------------------------------*/
669 static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
671 struct dw_desc *desc = txd_to_dw_desc(tx);
672 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
676 spin_lock_irqsave(&dwc->lock, flags);
677 cookie = dma_cookie_assign(tx);
680 * REVISIT: We should attempt to chain as many descriptors as
681 * possible, perhaps even appending to those already submitted
682 * for DMA. But this is hard to do in a race-free manner.
685 dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", __func__, desc->txd.cookie);
686 list_add_tail(&desc->desc_node, &dwc->queue);
688 spin_unlock_irqrestore(&dwc->lock, flags);
693 static struct dma_async_tx_descriptor *
694 dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
695 size_t len, unsigned long flags)
697 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
698 struct dw_dma *dw = to_dw_dma(chan->device);
699 struct dw_desc *desc;
700 struct dw_desc *first;
701 struct dw_desc *prev;
704 unsigned int src_width;
705 unsigned int dst_width;
706 unsigned int data_width;
709 dev_vdbg(chan2dev(chan),
710 "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__,
711 &dest, &src, len, flags);
713 if (unlikely(!len)) {
714 dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
718 dwc->direction = DMA_MEM_TO_MEM;
720 data_width = min_t(unsigned int, dw->data_width[dwc->src_master],
721 dw->data_width[dwc->dst_master]);
723 src_width = dst_width = min_t(unsigned int, data_width,
724 dwc_fast_ffs(src | dest | len));
726 ctllo = DWC_DEFAULT_CTLLO(chan)
727 | DWC_CTLL_DST_WIDTH(dst_width)
728 | DWC_CTLL_SRC_WIDTH(src_width)
734 for (offset = 0; offset < len; offset += xfer_count << src_width) {
735 xfer_count = min_t(size_t, (len - offset) >> src_width,
738 desc = dwc_desc_get(dwc);
742 desc->lli.sar = src + offset;
743 desc->lli.dar = dest + offset;
744 desc->lli.ctllo = ctllo;
745 desc->lli.ctlhi = xfer_count;
746 desc->len = xfer_count << src_width;
751 prev->lli.llp = desc->txd.phys;
752 list_add_tail(&desc->desc_node,
758 if (flags & DMA_PREP_INTERRUPT)
759 /* Trigger interrupt after last block */
760 prev->lli.ctllo |= DWC_CTLL_INT_EN;
763 first->txd.flags = flags;
764 first->total_len = len;
769 dwc_desc_put(dwc, first);
773 static struct dma_async_tx_descriptor *
774 dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
775 unsigned int sg_len, enum dma_transfer_direction direction,
776 unsigned long flags, void *context)
778 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
779 struct dw_dma *dw = to_dw_dma(chan->device);
780 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
781 struct dw_desc *prev;
782 struct dw_desc *first;
785 unsigned int reg_width;
786 unsigned int mem_width;
787 unsigned int data_width;
789 struct scatterlist *sg;
790 size_t total_len = 0;
792 dev_vdbg(chan2dev(chan), "%s\n", __func__);
794 if (unlikely(!is_slave_direction(direction) || !sg_len))
797 dwc->direction = direction;
803 reg_width = __ffs(sconfig->dst_addr_width);
804 reg = sconfig->dst_addr;
805 ctllo = (DWC_DEFAULT_CTLLO(chan)
806 | DWC_CTLL_DST_WIDTH(reg_width)
810 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
811 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
813 data_width = dw->data_width[dwc->src_master];
815 for_each_sg(sgl, sg, sg_len, i) {
816 struct dw_desc *desc;
819 mem = sg_dma_address(sg);
820 len = sg_dma_len(sg);
822 mem_width = min_t(unsigned int,
823 data_width, dwc_fast_ffs(mem | len));
825 slave_sg_todev_fill_desc:
826 desc = dwc_desc_get(dwc);
832 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
833 if ((len >> mem_width) > dwc->block_size) {
834 dlen = dwc->block_size << mem_width;
842 desc->lli.ctlhi = dlen >> mem_width;
848 prev->lli.llp = desc->txd.phys;
849 list_add_tail(&desc->desc_node,
856 goto slave_sg_todev_fill_desc;
860 reg_width = __ffs(sconfig->src_addr_width);
861 reg = sconfig->src_addr;
862 ctllo = (DWC_DEFAULT_CTLLO(chan)
863 | DWC_CTLL_SRC_WIDTH(reg_width)
867 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
868 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
870 data_width = dw->data_width[dwc->dst_master];
872 for_each_sg(sgl, sg, sg_len, i) {
873 struct dw_desc *desc;
876 mem = sg_dma_address(sg);
877 len = sg_dma_len(sg);
879 mem_width = min_t(unsigned int,
880 data_width, dwc_fast_ffs(mem | len));
882 slave_sg_fromdev_fill_desc:
883 desc = dwc_desc_get(dwc);
889 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
890 if ((len >> reg_width) > dwc->block_size) {
891 dlen = dwc->block_size << reg_width;
898 desc->lli.ctlhi = dlen >> reg_width;
904 prev->lli.llp = desc->txd.phys;
905 list_add_tail(&desc->desc_node,
912 goto slave_sg_fromdev_fill_desc;
919 if (flags & DMA_PREP_INTERRUPT)
920 /* Trigger interrupt after last block */
921 prev->lli.ctllo |= DWC_CTLL_INT_EN;
924 first->total_len = total_len;
929 dev_err(chan2dev(chan),
930 "not enough descriptors available. Direction %d\n", direction);
931 dwc_desc_put(dwc, first);
935 bool dw_dma_filter(struct dma_chan *chan, void *param)
937 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
938 struct dw_dma_slave *dws = param;
940 if (!dws || dws->dma_dev != chan->device->dev)
943 /* We have to copy data since dws can be temporary storage */
945 dwc->src_id = dws->src_id;
946 dwc->dst_id = dws->dst_id;
948 dwc->src_master = dws->src_master;
949 dwc->dst_master = dws->dst_master;
953 EXPORT_SYMBOL_GPL(dw_dma_filter);
956 * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
957 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
959 * NOTE: burst size 2 is not supported by controller.
961 * This can be done by finding least significant bit set: n & (n - 1)
963 static inline void convert_burst(u32 *maxburst)
966 *maxburst = fls(*maxburst) - 2;
971 static int dwc_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
973 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
975 /* Check if chan will be configured for slave transfers */
976 if (!is_slave_direction(sconfig->direction))
979 memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
980 dwc->direction = sconfig->direction;
982 convert_burst(&dwc->dma_sconfig.src_maxburst);
983 convert_burst(&dwc->dma_sconfig.dst_maxburst);
988 static int dwc_pause(struct dma_chan *chan)
990 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
992 unsigned int count = 20; /* timeout iterations */
995 spin_lock_irqsave(&dwc->lock, flags);
997 cfglo = channel_readl(dwc, CFG_LO);
998 channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
999 while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--)
1004 spin_unlock_irqrestore(&dwc->lock, flags);
1009 static inline void dwc_chan_resume(struct dw_dma_chan *dwc)
1011 u32 cfglo = channel_readl(dwc, CFG_LO);
1013 channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
1015 dwc->paused = false;
1018 static int dwc_resume(struct dma_chan *chan)
1020 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1021 unsigned long flags;
1026 spin_lock_irqsave(&dwc->lock, flags);
1028 dwc_chan_resume(dwc);
1030 spin_unlock_irqrestore(&dwc->lock, flags);
1035 static int dwc_terminate_all(struct dma_chan *chan)
1037 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1038 struct dw_dma *dw = to_dw_dma(chan->device);
1039 struct dw_desc *desc, *_desc;
1040 unsigned long flags;
1043 spin_lock_irqsave(&dwc->lock, flags);
1045 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
1047 dwc_chan_disable(dw, dwc);
1049 dwc_chan_resume(dwc);
1051 /* active_list entries will end up before queued entries */
1052 list_splice_init(&dwc->queue, &list);
1053 list_splice_init(&dwc->active_list, &list);
1055 spin_unlock_irqrestore(&dwc->lock, flags);
1057 /* Flush all pending and queued descriptors */
1058 list_for_each_entry_safe(desc, _desc, &list, desc_node)
1059 dwc_descriptor_complete(dwc, desc, false);
1064 static inline u32 dwc_get_residue(struct dw_dma_chan *dwc)
1066 unsigned long flags;
1069 spin_lock_irqsave(&dwc->lock, flags);
1071 residue = dwc->residue;
1072 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
1073 residue -= dwc_get_sent(dwc);
1075 spin_unlock_irqrestore(&dwc->lock, flags);
1079 static enum dma_status
1080 dwc_tx_status(struct dma_chan *chan,
1081 dma_cookie_t cookie,
1082 struct dma_tx_state *txstate)
1084 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1085 enum dma_status ret;
1087 ret = dma_cookie_status(chan, cookie, txstate);
1088 if (ret == DMA_COMPLETE)
1091 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
1093 ret = dma_cookie_status(chan, cookie, txstate);
1094 if (ret != DMA_COMPLETE)
1095 dma_set_residue(txstate, dwc_get_residue(dwc));
1097 if (dwc->paused && ret == DMA_IN_PROGRESS)
1103 static void dwc_issue_pending(struct dma_chan *chan)
1105 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1106 unsigned long flags;
1108 spin_lock_irqsave(&dwc->lock, flags);
1109 if (list_empty(&dwc->active_list))
1110 dwc_dostart_first_queued(dwc);
1111 spin_unlock_irqrestore(&dwc->lock, flags);
1114 /*----------------------------------------------------------------------*/
1116 static void dw_dma_off(struct dw_dma *dw)
1120 dma_writel(dw, CFG, 0);
1122 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
1123 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1124 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1125 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1126 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1128 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1131 for (i = 0; i < dw->dma.chancnt; i++)
1132 dw->chan[i].initialized = false;
1135 static void dw_dma_on(struct dw_dma *dw)
1137 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1140 static int dwc_alloc_chan_resources(struct dma_chan *chan)
1142 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1143 struct dw_dma *dw = to_dw_dma(chan->device);
1144 struct dw_desc *desc;
1146 unsigned long flags;
1148 dev_vdbg(chan2dev(chan), "%s\n", __func__);
1150 /* ASSERT: channel is idle */
1151 if (dma_readl(dw, CH_EN) & dwc->mask) {
1152 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
1156 dma_cookie_init(chan);
1159 * NOTE: some controllers may have additional features that we
1160 * need to initialize here, like "scatter-gather" (which
1161 * doesn't mean what you think it means), and status writeback.
1164 /* Enable controller here if needed */
1167 dw->in_use |= dwc->mask;
1169 spin_lock_irqsave(&dwc->lock, flags);
1170 i = dwc->descs_allocated;
1171 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
1174 spin_unlock_irqrestore(&dwc->lock, flags);
1176 desc = dma_pool_alloc(dw->desc_pool, GFP_ATOMIC, &phys);
1178 goto err_desc_alloc;
1180 memset(desc, 0, sizeof(struct dw_desc));
1182 INIT_LIST_HEAD(&desc->tx_list);
1183 dma_async_tx_descriptor_init(&desc->txd, chan);
1184 desc->txd.tx_submit = dwc_tx_submit;
1185 desc->txd.flags = DMA_CTRL_ACK;
1186 desc->txd.phys = phys;
1188 dwc_desc_put(dwc, desc);
1190 spin_lock_irqsave(&dwc->lock, flags);
1191 i = ++dwc->descs_allocated;
1194 spin_unlock_irqrestore(&dwc->lock, flags);
1196 dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
1201 dev_info(chan2dev(chan), "only allocated %d descriptors\n", i);
1206 static void dwc_free_chan_resources(struct dma_chan *chan)
1208 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1209 struct dw_dma *dw = to_dw_dma(chan->device);
1210 struct dw_desc *desc, *_desc;
1211 unsigned long flags;
1214 dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
1215 dwc->descs_allocated);
1217 /* ASSERT: channel is idle */
1218 BUG_ON(!list_empty(&dwc->active_list));
1219 BUG_ON(!list_empty(&dwc->queue));
1220 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
1222 spin_lock_irqsave(&dwc->lock, flags);
1223 list_splice_init(&dwc->free_list, &list);
1224 dwc->descs_allocated = 0;
1225 dwc->initialized = false;
1227 /* Disable interrupts */
1228 channel_clear_bit(dw, MASK.XFER, dwc->mask);
1229 channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
1230 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
1232 spin_unlock_irqrestore(&dwc->lock, flags);
1234 /* Disable controller in case it was a last user */
1235 dw->in_use &= ~dwc->mask;
1239 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
1240 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1241 dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
1244 dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
1247 /* --------------------- Cyclic DMA API extensions -------------------- */
1250 * dw_dma_cyclic_start - start the cyclic DMA transfer
1251 * @chan: the DMA channel to start
1253 * Must be called with soft interrupts disabled. Returns zero on success or
1254 * -errno on failure.
1256 int dw_dma_cyclic_start(struct dma_chan *chan)
1258 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1259 unsigned long flags;
1261 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
1262 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
1266 spin_lock_irqsave(&dwc->lock, flags);
1267 dwc_dostart(dwc, dwc->cdesc->desc[0]);
1268 spin_unlock_irqrestore(&dwc->lock, flags);
1272 EXPORT_SYMBOL(dw_dma_cyclic_start);
1275 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1276 * @chan: the DMA channel to stop
1278 * Must be called with soft interrupts disabled.
1280 void dw_dma_cyclic_stop(struct dma_chan *chan)
1282 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1283 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1284 unsigned long flags;
1286 spin_lock_irqsave(&dwc->lock, flags);
1288 dwc_chan_disable(dw, dwc);
1290 spin_unlock_irqrestore(&dwc->lock, flags);
1292 EXPORT_SYMBOL(dw_dma_cyclic_stop);
1295 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1296 * @chan: the DMA channel to prepare
1297 * @buf_addr: physical DMA address where the buffer starts
1298 * @buf_len: total number of bytes for the entire buffer
1299 * @period_len: number of bytes for each period
1300 * @direction: transfer direction, to or from device
1302 * Must be called before trying to start the transfer. Returns a valid struct
1303 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1305 struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1306 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
1307 enum dma_transfer_direction direction)
1309 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1310 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
1311 struct dw_cyclic_desc *cdesc;
1312 struct dw_cyclic_desc *retval = NULL;
1313 struct dw_desc *desc;
1314 struct dw_desc *last = NULL;
1315 unsigned long was_cyclic;
1316 unsigned int reg_width;
1317 unsigned int periods;
1319 unsigned long flags;
1321 spin_lock_irqsave(&dwc->lock, flags);
1323 spin_unlock_irqrestore(&dwc->lock, flags);
1324 dev_dbg(chan2dev(&dwc->chan),
1325 "channel doesn't support LLP transfers\n");
1326 return ERR_PTR(-EINVAL);
1329 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
1330 spin_unlock_irqrestore(&dwc->lock, flags);
1331 dev_dbg(chan2dev(&dwc->chan),
1332 "queue and/or active list are not empty\n");
1333 return ERR_PTR(-EBUSY);
1336 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1337 spin_unlock_irqrestore(&dwc->lock, flags);
1339 dev_dbg(chan2dev(&dwc->chan),
1340 "channel already prepared for cyclic DMA\n");
1341 return ERR_PTR(-EBUSY);
1344 retval = ERR_PTR(-EINVAL);
1346 if (unlikely(!is_slave_direction(direction)))
1349 dwc->direction = direction;
1351 if (direction == DMA_MEM_TO_DEV)
1352 reg_width = __ffs(sconfig->dst_addr_width);
1354 reg_width = __ffs(sconfig->src_addr_width);
1356 periods = buf_len / period_len;
1358 /* Check for too big/unaligned periods and unaligned DMA buffer. */
1359 if (period_len > (dwc->block_size << reg_width))
1361 if (unlikely(period_len & ((1 << reg_width) - 1)))
1363 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1366 retval = ERR_PTR(-ENOMEM);
1368 if (periods > NR_DESCS_PER_CHANNEL)
1371 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1375 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1379 for (i = 0; i < periods; i++) {
1380 desc = dwc_desc_get(dwc);
1382 goto out_err_desc_get;
1384 switch (direction) {
1385 case DMA_MEM_TO_DEV:
1386 desc->lli.dar = sconfig->dst_addr;
1387 desc->lli.sar = buf_addr + (period_len * i);
1388 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
1389 | DWC_CTLL_DST_WIDTH(reg_width)
1390 | DWC_CTLL_SRC_WIDTH(reg_width)
1395 desc->lli.ctllo |= sconfig->device_fc ?
1396 DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
1397 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
1400 case DMA_DEV_TO_MEM:
1401 desc->lli.dar = buf_addr + (period_len * i);
1402 desc->lli.sar = sconfig->src_addr;
1403 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
1404 | DWC_CTLL_SRC_WIDTH(reg_width)
1405 | DWC_CTLL_DST_WIDTH(reg_width)
1410 desc->lli.ctllo |= sconfig->device_fc ?
1411 DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
1412 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
1419 desc->lli.ctlhi = (period_len >> reg_width);
1420 cdesc->desc[i] = desc;
1423 last->lli.llp = desc->txd.phys;
1428 /* Let's make a cyclic list */
1429 last->lli.llp = cdesc->desc[0]->txd.phys;
1431 dev_dbg(chan2dev(&dwc->chan),
1432 "cyclic prepared buf %pad len %zu period %zu periods %d\n",
1433 &buf_addr, buf_len, period_len, periods);
1435 cdesc->periods = periods;
1442 dwc_desc_put(dwc, cdesc->desc[i]);
1446 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1447 return (struct dw_cyclic_desc *)retval;
1449 EXPORT_SYMBOL(dw_dma_cyclic_prep);
1452 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1453 * @chan: the DMA channel to free
1455 void dw_dma_cyclic_free(struct dma_chan *chan)
1457 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1458 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1459 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1461 unsigned long flags;
1463 dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
1468 spin_lock_irqsave(&dwc->lock, flags);
1470 dwc_chan_disable(dw, dwc);
1472 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
1473 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1474 dma_writel(dw, CLEAR.XFER, dwc->mask);
1476 spin_unlock_irqrestore(&dwc->lock, flags);
1478 for (i = 0; i < cdesc->periods; i++)
1479 dwc_desc_put(dwc, cdesc->desc[i]);
1484 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1486 EXPORT_SYMBOL(dw_dma_cyclic_free);
1488 /*----------------------------------------------------------------------*/
1490 int dw_dma_probe(struct dw_dma_chip *chip, struct dw_dma_platform_data *pdata)
1493 bool autocfg = false;
1494 unsigned int dw_params;
1495 unsigned int max_blk_size = 0;
1499 dw = devm_kzalloc(chip->dev, sizeof(*dw), GFP_KERNEL);
1503 dw->regs = chip->regs;
1506 pm_runtime_get_sync(chip->dev);
1509 dw_params = dma_read_byaddr(chip->regs, DW_PARAMS);
1510 dev_dbg(chip->dev, "DW_PARAMS: 0x%08x\n", dw_params);
1512 autocfg = dw_params >> DW_PARAMS_EN & 1;
1518 pdata = devm_kzalloc(chip->dev, sizeof(*pdata), GFP_KERNEL);
1524 /* Get hardware configuration parameters */
1525 pdata->nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 7) + 1;
1526 pdata->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
1527 for (i = 0; i < pdata->nr_masters; i++) {
1528 pdata->data_width[i] =
1529 (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3) + 2;
1531 max_blk_size = dma_readl(dw, MAX_BLK_SIZE);
1533 /* Fill platform data with the default values */
1534 pdata->is_private = true;
1535 pdata->is_memcpy = true;
1536 pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
1537 pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
1538 } else if (pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) {
1543 dw->chan = devm_kcalloc(chip->dev, pdata->nr_channels, sizeof(*dw->chan),
1550 /* Get hardware configuration parameters */
1551 dw->nr_masters = pdata->nr_masters;
1552 for (i = 0; i < dw->nr_masters; i++)
1553 dw->data_width[i] = pdata->data_width[i];
1555 /* Calculate all channel mask before DMA setup */
1556 dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
1558 /* Force dma off, just in case */
1561 /* Create a pool of consistent memory blocks for hardware descriptors */
1562 dw->desc_pool = dmam_pool_create("dw_dmac_desc_pool", chip->dev,
1563 sizeof(struct dw_desc), 4, 0);
1564 if (!dw->desc_pool) {
1565 dev_err(chip->dev, "No memory for descriptors dma pool\n");
1570 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1572 err = request_irq(chip->irq, dw_dma_interrupt, IRQF_SHARED,
1577 INIT_LIST_HEAD(&dw->dma.channels);
1578 for (i = 0; i < pdata->nr_channels; i++) {
1579 struct dw_dma_chan *dwc = &dw->chan[i];
1581 dwc->chan.device = &dw->dma;
1582 dma_cookie_init(&dwc->chan);
1583 if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
1584 list_add_tail(&dwc->chan.device_node,
1587 list_add(&dwc->chan.device_node, &dw->dma.channels);
1589 /* 7 is highest priority & 0 is lowest. */
1590 if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
1591 dwc->priority = pdata->nr_channels - i - 1;
1595 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1596 spin_lock_init(&dwc->lock);
1599 INIT_LIST_HEAD(&dwc->active_list);
1600 INIT_LIST_HEAD(&dwc->queue);
1601 INIT_LIST_HEAD(&dwc->free_list);
1603 channel_clear_bit(dw, CH_EN, dwc->mask);
1605 dwc->direction = DMA_TRANS_NONE;
1607 /* Hardware configuration */
1609 unsigned int dwc_params;
1610 unsigned int r = DW_DMA_MAX_NR_CHANNELS - i - 1;
1611 void __iomem *addr = chip->regs + r * sizeof(u32);
1613 dwc_params = dma_read_byaddr(addr, DWC_PARAMS);
1615 dev_dbg(chip->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
1619 * Decode maximum block size for given channel. The
1620 * stored 4 bit value represents blocks from 0x00 for 3
1621 * up to 0x0a for 4095.
1624 (4 << ((max_blk_size >> 4 * i) & 0xf)) - 1;
1626 (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0;
1628 dwc->block_size = pdata->block_size;
1630 /* Check if channel supports multi block transfer */
1631 channel_writel(dwc, LLP, 0xfffffffc);
1633 (channel_readl(dwc, LLP) & 0xfffffffc) == 0;
1634 channel_writel(dwc, LLP, 0);
1638 /* Clear all interrupts on all channels. */
1639 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
1640 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
1641 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1642 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1643 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1645 /* Set capabilities */
1646 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
1647 if (pdata->is_private)
1648 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
1649 if (pdata->is_memcpy)
1650 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1652 dw->dma.dev = chip->dev;
1653 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1654 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1656 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
1657 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
1659 dw->dma.device_config = dwc_config;
1660 dw->dma.device_pause = dwc_pause;
1661 dw->dma.device_resume = dwc_resume;
1662 dw->dma.device_terminate_all = dwc_terminate_all;
1664 dw->dma.device_tx_status = dwc_tx_status;
1665 dw->dma.device_issue_pending = dwc_issue_pending;
1667 /* DMA capabilities */
1668 dw->dma.src_addr_widths = DW_DMA_BUSWIDTHS;
1669 dw->dma.dst_addr_widths = DW_DMA_BUSWIDTHS;
1670 dw->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
1671 BIT(DMA_MEM_TO_MEM);
1672 dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1674 err = dma_async_device_register(&dw->dma);
1676 goto err_dma_register;
1678 dev_info(chip->dev, "DesignWare DMA Controller, %d channels\n",
1679 pdata->nr_channels);
1681 pm_runtime_put_sync_suspend(chip->dev);
1686 free_irq(chip->irq, dw);
1688 pm_runtime_put_sync_suspend(chip->dev);
1691 EXPORT_SYMBOL_GPL(dw_dma_probe);
1693 int dw_dma_remove(struct dw_dma_chip *chip)
1695 struct dw_dma *dw = chip->dw;
1696 struct dw_dma_chan *dwc, *_dwc;
1698 pm_runtime_get_sync(chip->dev);
1701 dma_async_device_unregister(&dw->dma);
1703 free_irq(chip->irq, dw);
1704 tasklet_kill(&dw->tasklet);
1706 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1708 list_del(&dwc->chan.device_node);
1709 channel_clear_bit(dw, CH_EN, dwc->mask);
1712 pm_runtime_put_sync_suspend(chip->dev);
1715 EXPORT_SYMBOL_GPL(dw_dma_remove);
1717 int dw_dma_disable(struct dw_dma_chip *chip)
1719 struct dw_dma *dw = chip->dw;
1724 EXPORT_SYMBOL_GPL(dw_dma_disable);
1726 int dw_dma_enable(struct dw_dma_chip *chip)
1728 struct dw_dma *dw = chip->dw;
1733 EXPORT_SYMBOL_GPL(dw_dma_enable);
1735 MODULE_LICENSE("GPL v2");
1736 MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
1737 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1738 MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");