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/clk.h>
15 #include <linux/delay.h>
16 #include <linux/dmaengine.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/dmapool.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/slab.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 currently been tested only with the Atmel AT32AP7000,
37 * which does not support descriptor writeback.
40 static inline bool is_request_line_unset(struct dw_dma_chan
*dwc
)
42 return dwc
->request_line
== (typeof(dwc
->request_line
))~0;
45 static inline void dwc_set_masters(struct dw_dma_chan
*dwc
)
47 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
48 struct dw_dma_slave
*dws
= dwc
->chan
.private;
49 unsigned char mmax
= dw
->nr_masters
- 1;
51 if (!is_request_line_unset(dwc
))
54 dwc
->src_master
= min_t(unsigned char, mmax
, dwc_get_sms(dws
));
55 dwc
->dst_master
= min_t(unsigned char, mmax
, dwc_get_dms(dws
));
58 #define DWC_DEFAULT_CTLLO(_chan) ({ \
59 struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \
60 struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
61 bool _is_slave = is_slave_direction(_dwc->direction); \
62 u8 _smsize = _is_slave ? _sconfig->src_maxburst : \
64 u8 _dmsize = _is_slave ? _sconfig->dst_maxburst : \
67 (DWC_CTLL_DST_MSIZE(_dmsize) \
68 | DWC_CTLL_SRC_MSIZE(_smsize) \
71 | DWC_CTLL_DMS(_dwc->dst_master) \
72 | DWC_CTLL_SMS(_dwc->src_master)); \
76 * Number of descriptors to allocate for each channel. This should be
77 * made configurable somehow; preferably, the clients (at least the
78 * ones using slave transfers) should be able to give us a hint.
80 #define NR_DESCS_PER_CHANNEL 64
82 /*----------------------------------------------------------------------*/
84 static struct device
*chan2dev(struct dma_chan
*chan
)
86 return &chan
->dev
->device
;
89 static struct dw_desc
*dwc_first_active(struct dw_dma_chan
*dwc
)
91 return to_dw_desc(dwc
->active_list
.next
);
94 static struct dw_desc
*dwc_desc_get(struct dw_dma_chan
*dwc
)
96 struct dw_desc
*desc
, *_desc
;
97 struct dw_desc
*ret
= NULL
;
101 spin_lock_irqsave(&dwc
->lock
, flags
);
102 list_for_each_entry_safe(desc
, _desc
, &dwc
->free_list
, desc_node
) {
104 if (async_tx_test_ack(&desc
->txd
)) {
105 list_del(&desc
->desc_node
);
109 dev_dbg(chan2dev(&dwc
->chan
), "desc %p not ACKed\n", desc
);
111 spin_unlock_irqrestore(&dwc
->lock
, flags
);
113 dev_vdbg(chan2dev(&dwc
->chan
), "scanned %u descriptors on freelist\n", i
);
119 * Move a descriptor, including any children, to the free list.
120 * `desc' must not be on any lists.
122 static void dwc_desc_put(struct dw_dma_chan
*dwc
, struct dw_desc
*desc
)
127 struct dw_desc
*child
;
129 spin_lock_irqsave(&dwc
->lock
, flags
);
130 list_for_each_entry(child
, &desc
->tx_list
, desc_node
)
131 dev_vdbg(chan2dev(&dwc
->chan
),
132 "moving child desc %p to freelist\n",
134 list_splice_init(&desc
->tx_list
, &dwc
->free_list
);
135 dev_vdbg(chan2dev(&dwc
->chan
), "moving desc %p to freelist\n", desc
);
136 list_add(&desc
->desc_node
, &dwc
->free_list
);
137 spin_unlock_irqrestore(&dwc
->lock
, flags
);
141 static void dwc_initialize(struct dw_dma_chan
*dwc
)
143 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
144 struct dw_dma_slave
*dws
= dwc
->chan
.private;
145 u32 cfghi
= DWC_CFGH_FIFO_MODE
;
146 u32 cfglo
= DWC_CFGL_CH_PRIOR(dwc
->priority
);
148 if (dwc
->initialized
== true)
153 * We need controller-specific data to set up slave
156 BUG_ON(!dws
->dma_dev
|| dws
->dma_dev
!= dw
->dma
.dev
);
159 cfglo
|= dws
->cfg_lo
& ~DWC_CFGL_CH_PRIOR_MASK
;
161 if (dwc
->direction
== DMA_MEM_TO_DEV
)
162 cfghi
= DWC_CFGH_DST_PER(dwc
->request_line
);
163 else if (dwc
->direction
== DMA_DEV_TO_MEM
)
164 cfghi
= DWC_CFGH_SRC_PER(dwc
->request_line
);
167 channel_writel(dwc
, CFG_LO
, cfglo
);
168 channel_writel(dwc
, CFG_HI
, cfghi
);
170 /* Enable interrupts */
171 channel_set_bit(dw
, MASK
.XFER
, dwc
->mask
);
172 channel_set_bit(dw
, MASK
.ERROR
, dwc
->mask
);
174 dwc
->initialized
= true;
177 /*----------------------------------------------------------------------*/
179 static inline unsigned int dwc_fast_fls(unsigned long long v
)
182 * We can be a lot more clever here, but this should take care
183 * of the most common optimization.
194 static inline void dwc_dump_chan_regs(struct dw_dma_chan
*dwc
)
196 dev_err(chan2dev(&dwc
->chan
),
197 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
198 channel_readl(dwc
, SAR
),
199 channel_readl(dwc
, DAR
),
200 channel_readl(dwc
, LLP
),
201 channel_readl(dwc
, CTL_HI
),
202 channel_readl(dwc
, CTL_LO
));
205 static inline void dwc_chan_disable(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
)
207 channel_clear_bit(dw
, CH_EN
, dwc
->mask
);
208 while (dma_readl(dw
, CH_EN
) & dwc
->mask
)
212 /*----------------------------------------------------------------------*/
214 /* Perform single block transfer */
215 static inline void dwc_do_single_block(struct dw_dma_chan
*dwc
,
216 struct dw_desc
*desc
)
218 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
222 * Software emulation of LLP mode relies on interrupts to continue
223 * multi block transfer.
225 ctllo
= desc
->lli
.ctllo
| DWC_CTLL_INT_EN
;
227 channel_writel(dwc
, SAR
, desc
->lli
.sar
);
228 channel_writel(dwc
, DAR
, desc
->lli
.dar
);
229 channel_writel(dwc
, CTL_LO
, ctllo
);
230 channel_writel(dwc
, CTL_HI
, desc
->lli
.ctlhi
);
231 channel_set_bit(dw
, CH_EN
, dwc
->mask
);
233 /* Move pointer to next descriptor */
234 dwc
->tx_node_active
= dwc
->tx_node_active
->next
;
237 /* Called with dwc->lock held and bh disabled */
238 static void dwc_dostart(struct dw_dma_chan
*dwc
, struct dw_desc
*first
)
240 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
241 unsigned long was_soft_llp
;
243 /* ASSERT: channel is idle */
244 if (dma_readl(dw
, CH_EN
) & dwc
->mask
) {
245 dev_err(chan2dev(&dwc
->chan
),
246 "BUG: Attempted to start non-idle channel\n");
247 dwc_dump_chan_regs(dwc
);
249 /* The tasklet will hopefully advance the queue... */
254 was_soft_llp
= test_and_set_bit(DW_DMA_IS_SOFT_LLP
,
257 dev_err(chan2dev(&dwc
->chan
),
258 "BUG: Attempted to start new LLP transfer inside ongoing one\n");
264 dwc
->residue
= first
->total_len
;
265 dwc
->tx_node_active
= &first
->tx_list
;
267 /* Submit first block */
268 dwc_do_single_block(dwc
, first
);
275 channel_writel(dwc
, LLP
, first
->txd
.phys
);
276 channel_writel(dwc
, CTL_LO
,
277 DWC_CTLL_LLP_D_EN
| DWC_CTLL_LLP_S_EN
);
278 channel_writel(dwc
, CTL_HI
, 0);
279 channel_set_bit(dw
, CH_EN
, dwc
->mask
);
282 /*----------------------------------------------------------------------*/
285 dwc_descriptor_complete(struct dw_dma_chan
*dwc
, struct dw_desc
*desc
,
286 bool callback_required
)
288 dma_async_tx_callback callback
= NULL
;
290 struct dma_async_tx_descriptor
*txd
= &desc
->txd
;
291 struct dw_desc
*child
;
294 dev_vdbg(chan2dev(&dwc
->chan
), "descriptor %u complete\n", txd
->cookie
);
296 spin_lock_irqsave(&dwc
->lock
, flags
);
297 dma_cookie_complete(txd
);
298 if (callback_required
) {
299 callback
= txd
->callback
;
300 param
= txd
->callback_param
;
304 list_for_each_entry(child
, &desc
->tx_list
, desc_node
)
305 async_tx_ack(&child
->txd
);
306 async_tx_ack(&desc
->txd
);
308 list_splice_init(&desc
->tx_list
, &dwc
->free_list
);
309 list_move(&desc
->desc_node
, &dwc
->free_list
);
311 dma_descriptor_unmap(txd
);
312 spin_unlock_irqrestore(&dwc
->lock
, flags
);
318 static void dwc_complete_all(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
)
320 struct dw_desc
*desc
, *_desc
;
324 spin_lock_irqsave(&dwc
->lock
, flags
);
325 if (dma_readl(dw
, CH_EN
) & dwc
->mask
) {
326 dev_err(chan2dev(&dwc
->chan
),
327 "BUG: XFER bit set, but channel not idle!\n");
329 /* Try to continue after resetting the channel... */
330 dwc_chan_disable(dw
, dwc
);
334 * Submit queued descriptors ASAP, i.e. before we go through
335 * the completed ones.
337 list_splice_init(&dwc
->active_list
, &list
);
338 if (!list_empty(&dwc
->queue
)) {
339 list_move(dwc
->queue
.next
, &dwc
->active_list
);
340 dwc_dostart(dwc
, dwc_first_active(dwc
));
343 spin_unlock_irqrestore(&dwc
->lock
, flags
);
345 list_for_each_entry_safe(desc
, _desc
, &list
, desc_node
)
346 dwc_descriptor_complete(dwc
, desc
, true);
349 /* Returns how many bytes were already received from source */
350 static inline u32
dwc_get_sent(struct dw_dma_chan
*dwc
)
352 u32 ctlhi
= channel_readl(dwc
, CTL_HI
);
353 u32 ctllo
= channel_readl(dwc
, CTL_LO
);
355 return (ctlhi
& DWC_CTLH_BLOCK_TS_MASK
) * (1 << (ctllo
>> 4 & 7));
358 static void dwc_scan_descriptors(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
)
361 struct dw_desc
*desc
, *_desc
;
362 struct dw_desc
*child
;
366 spin_lock_irqsave(&dwc
->lock
, flags
);
367 llp
= channel_readl(dwc
, LLP
);
368 status_xfer
= dma_readl(dw
, RAW
.XFER
);
370 if (status_xfer
& dwc
->mask
) {
371 /* Everything we've submitted is done */
372 dma_writel(dw
, CLEAR
.XFER
, dwc
->mask
);
374 if (test_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
)) {
375 struct list_head
*head
, *active
= dwc
->tx_node_active
;
378 * We are inside first active descriptor.
379 * Otherwise something is really wrong.
381 desc
= dwc_first_active(dwc
);
383 head
= &desc
->tx_list
;
384 if (active
!= head
) {
385 /* Update desc to reflect last sent one */
386 if (active
!= head
->next
)
387 desc
= to_dw_desc(active
->prev
);
389 dwc
->residue
-= desc
->len
;
391 child
= to_dw_desc(active
);
393 /* Submit next block */
394 dwc_do_single_block(dwc
, child
);
396 spin_unlock_irqrestore(&dwc
->lock
, flags
);
400 /* We are done here */
401 clear_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
);
406 spin_unlock_irqrestore(&dwc
->lock
, flags
);
408 dwc_complete_all(dw
, dwc
);
412 if (list_empty(&dwc
->active_list
)) {
414 spin_unlock_irqrestore(&dwc
->lock
, flags
);
418 if (test_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
)) {
419 dev_vdbg(chan2dev(&dwc
->chan
), "%s: soft LLP mode\n", __func__
);
420 spin_unlock_irqrestore(&dwc
->lock
, flags
);
424 dev_vdbg(chan2dev(&dwc
->chan
), "%s: llp=%pad\n", __func__
, &llp
);
426 list_for_each_entry_safe(desc
, _desc
, &dwc
->active_list
, desc_node
) {
427 /* Initial residue value */
428 dwc
->residue
= desc
->total_len
;
430 /* Check first descriptors addr */
431 if (desc
->txd
.phys
== llp
) {
432 spin_unlock_irqrestore(&dwc
->lock
, flags
);
436 /* Check first descriptors llp */
437 if (desc
->lli
.llp
== llp
) {
438 /* This one is currently in progress */
439 dwc
->residue
-= dwc_get_sent(dwc
);
440 spin_unlock_irqrestore(&dwc
->lock
, flags
);
444 dwc
->residue
-= desc
->len
;
445 list_for_each_entry(child
, &desc
->tx_list
, desc_node
) {
446 if (child
->lli
.llp
== llp
) {
447 /* Currently in progress */
448 dwc
->residue
-= dwc_get_sent(dwc
);
449 spin_unlock_irqrestore(&dwc
->lock
, flags
);
452 dwc
->residue
-= child
->len
;
456 * No descriptors so far seem to be in progress, i.e.
457 * this one must be done.
459 spin_unlock_irqrestore(&dwc
->lock
, flags
);
460 dwc_descriptor_complete(dwc
, desc
, true);
461 spin_lock_irqsave(&dwc
->lock
, flags
);
464 dev_err(chan2dev(&dwc
->chan
),
465 "BUG: All descriptors done, but channel not idle!\n");
467 /* Try to continue after resetting the channel... */
468 dwc_chan_disable(dw
, dwc
);
470 if (!list_empty(&dwc
->queue
)) {
471 list_move(dwc
->queue
.next
, &dwc
->active_list
);
472 dwc_dostart(dwc
, dwc_first_active(dwc
));
474 spin_unlock_irqrestore(&dwc
->lock
, flags
);
477 static inline void dwc_dump_lli(struct dw_dma_chan
*dwc
, struct dw_lli
*lli
)
479 dev_crit(chan2dev(&dwc
->chan
), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
480 lli
->sar
, lli
->dar
, lli
->llp
, lli
->ctlhi
, lli
->ctllo
);
483 static void dwc_handle_error(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
)
485 struct dw_desc
*bad_desc
;
486 struct dw_desc
*child
;
489 dwc_scan_descriptors(dw
, dwc
);
491 spin_lock_irqsave(&dwc
->lock
, flags
);
494 * The descriptor currently at the head of the active list is
495 * borked. Since we don't have any way to report errors, we'll
496 * just have to scream loudly and try to carry on.
498 bad_desc
= dwc_first_active(dwc
);
499 list_del_init(&bad_desc
->desc_node
);
500 list_move(dwc
->queue
.next
, dwc
->active_list
.prev
);
502 /* Clear the error flag and try to restart the controller */
503 dma_writel(dw
, CLEAR
.ERROR
, dwc
->mask
);
504 if (!list_empty(&dwc
->active_list
))
505 dwc_dostart(dwc
, dwc_first_active(dwc
));
508 * WARN may seem harsh, but since this only happens
509 * when someone submits a bad physical address in a
510 * descriptor, we should consider ourselves lucky that the
511 * controller flagged an error instead of scribbling over
512 * random memory locations.
514 dev_WARN(chan2dev(&dwc
->chan
), "Bad descriptor submitted for DMA!\n"
515 " cookie: %d\n", bad_desc
->txd
.cookie
);
516 dwc_dump_lli(dwc
, &bad_desc
->lli
);
517 list_for_each_entry(child
, &bad_desc
->tx_list
, desc_node
)
518 dwc_dump_lli(dwc
, &child
->lli
);
520 spin_unlock_irqrestore(&dwc
->lock
, flags
);
522 /* Pretend the descriptor completed successfully */
523 dwc_descriptor_complete(dwc
, bad_desc
, true);
526 /* --------------------- Cyclic DMA API extensions -------------------- */
528 dma_addr_t
dw_dma_get_src_addr(struct dma_chan
*chan
)
530 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
531 return channel_readl(dwc
, SAR
);
533 EXPORT_SYMBOL(dw_dma_get_src_addr
);
535 dma_addr_t
dw_dma_get_dst_addr(struct dma_chan
*chan
)
537 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
538 return channel_readl(dwc
, DAR
);
540 EXPORT_SYMBOL(dw_dma_get_dst_addr
);
542 /* Called with dwc->lock held and all DMAC interrupts disabled */
543 static void dwc_handle_cyclic(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
,
544 u32 status_err
, u32 status_xfer
)
549 void (*callback
)(void *param
);
550 void *callback_param
;
552 dev_vdbg(chan2dev(&dwc
->chan
), "new cyclic period llp 0x%08x\n",
553 channel_readl(dwc
, LLP
));
555 callback
= dwc
->cdesc
->period_callback
;
556 callback_param
= dwc
->cdesc
->period_callback_param
;
559 callback(callback_param
);
563 * Error and transfer complete are highly unlikely, and will most
564 * likely be due to a configuration error by the user.
566 if (unlikely(status_err
& dwc
->mask
) ||
567 unlikely(status_xfer
& dwc
->mask
)) {
570 dev_err(chan2dev(&dwc
->chan
),
571 "cyclic DMA unexpected %s interrupt, stopping DMA transfer\n",
572 status_xfer
? "xfer" : "error");
574 spin_lock_irqsave(&dwc
->lock
, flags
);
576 dwc_dump_chan_regs(dwc
);
578 dwc_chan_disable(dw
, dwc
);
580 /* Make sure DMA does not restart by loading a new list */
581 channel_writel(dwc
, LLP
, 0);
582 channel_writel(dwc
, CTL_LO
, 0);
583 channel_writel(dwc
, CTL_HI
, 0);
585 dma_writel(dw
, CLEAR
.ERROR
, dwc
->mask
);
586 dma_writel(dw
, CLEAR
.XFER
, dwc
->mask
);
588 for (i
= 0; i
< dwc
->cdesc
->periods
; i
++)
589 dwc_dump_lli(dwc
, &dwc
->cdesc
->desc
[i
]->lli
);
591 spin_unlock_irqrestore(&dwc
->lock
, flags
);
595 /* ------------------------------------------------------------------------- */
597 static void dw_dma_tasklet(unsigned long data
)
599 struct dw_dma
*dw
= (struct dw_dma
*)data
;
600 struct dw_dma_chan
*dwc
;
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_err
, status_xfer
);
614 else if (status_err
& (1 << i
))
615 dwc_handle_error(dw
, dwc
);
616 else if (status_xfer
& (1 << i
))
617 dwc_scan_descriptors(dw
, dwc
);
621 * Re-enable interrupts.
623 channel_set_bit(dw
, MASK
.XFER
, dw
->all_chan_mask
);
624 channel_set_bit(dw
, MASK
.ERROR
, dw
->all_chan_mask
);
627 static irqreturn_t
dw_dma_interrupt(int irq
, void *dev_id
)
629 struct dw_dma
*dw
= dev_id
;
630 u32 status
= dma_readl(dw
, STATUS_INT
);
632 dev_vdbg(dw
->dma
.dev
, "%s: status=0x%x\n", __func__
, status
);
634 /* Check if we have any interrupt from the DMAC */
639 * Just disable the interrupts. We'll turn them back on in the
642 channel_clear_bit(dw
, MASK
.XFER
, dw
->all_chan_mask
);
643 channel_clear_bit(dw
, MASK
.ERROR
, dw
->all_chan_mask
);
645 status
= dma_readl(dw
, STATUS_INT
);
648 "BUG: Unexpected interrupts pending: 0x%x\n",
652 channel_clear_bit(dw
, MASK
.XFER
, (1 << 8) - 1);
653 channel_clear_bit(dw
, MASK
.SRC_TRAN
, (1 << 8) - 1);
654 channel_clear_bit(dw
, MASK
.DST_TRAN
, (1 << 8) - 1);
655 channel_clear_bit(dw
, MASK
.ERROR
, (1 << 8) - 1);
658 tasklet_schedule(&dw
->tasklet
);
663 /*----------------------------------------------------------------------*/
665 static dma_cookie_t
dwc_tx_submit(struct dma_async_tx_descriptor
*tx
)
667 struct dw_desc
*desc
= txd_to_dw_desc(tx
);
668 struct dw_dma_chan
*dwc
= to_dw_dma_chan(tx
->chan
);
672 spin_lock_irqsave(&dwc
->lock
, flags
);
673 cookie
= dma_cookie_assign(tx
);
676 * REVISIT: We should attempt to chain as many descriptors as
677 * possible, perhaps even appending to those already submitted
678 * for DMA. But this is hard to do in a race-free manner.
680 if (list_empty(&dwc
->active_list
)) {
681 dev_vdbg(chan2dev(tx
->chan
), "%s: started %u\n", __func__
,
683 list_add_tail(&desc
->desc_node
, &dwc
->active_list
);
684 dwc_dostart(dwc
, dwc_first_active(dwc
));
686 dev_vdbg(chan2dev(tx
->chan
), "%s: queued %u\n", __func__
,
689 list_add_tail(&desc
->desc_node
, &dwc
->queue
);
692 spin_unlock_irqrestore(&dwc
->lock
, flags
);
697 static struct dma_async_tx_descriptor
*
698 dwc_prep_dma_memcpy(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t src
,
699 size_t len
, unsigned long flags
)
701 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
702 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
703 struct dw_desc
*desc
;
704 struct dw_desc
*first
;
705 struct dw_desc
*prev
;
708 unsigned int src_width
;
709 unsigned int dst_width
;
710 unsigned int data_width
;
713 dev_vdbg(chan2dev(chan
),
714 "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__
,
715 &dest
, &src
, len
, flags
);
717 if (unlikely(!len
)) {
718 dev_dbg(chan2dev(chan
), "%s: length is zero!\n", __func__
);
722 dwc
->direction
= DMA_MEM_TO_MEM
;
724 data_width
= min_t(unsigned int, dw
->data_width
[dwc
->src_master
],
725 dw
->data_width
[dwc
->dst_master
]);
727 src_width
= dst_width
= min_t(unsigned int, data_width
,
728 dwc_fast_fls(src
| dest
| len
));
730 ctllo
= DWC_DEFAULT_CTLLO(chan
)
731 | DWC_CTLL_DST_WIDTH(dst_width
)
732 | DWC_CTLL_SRC_WIDTH(src_width
)
738 for (offset
= 0; offset
< len
; offset
+= xfer_count
<< src_width
) {
739 xfer_count
= min_t(size_t, (len
- offset
) >> src_width
,
742 desc
= dwc_desc_get(dwc
);
746 desc
->lli
.sar
= src
+ offset
;
747 desc
->lli
.dar
= dest
+ offset
;
748 desc
->lli
.ctllo
= ctllo
;
749 desc
->lli
.ctlhi
= xfer_count
;
750 desc
->len
= xfer_count
<< src_width
;
755 prev
->lli
.llp
= desc
->txd
.phys
;
756 list_add_tail(&desc
->desc_node
,
762 if (flags
& DMA_PREP_INTERRUPT
)
763 /* Trigger interrupt after last block */
764 prev
->lli
.ctllo
|= DWC_CTLL_INT_EN
;
767 first
->txd
.flags
= flags
;
768 first
->total_len
= len
;
773 dwc_desc_put(dwc
, first
);
777 static struct dma_async_tx_descriptor
*
778 dwc_prep_slave_sg(struct dma_chan
*chan
, struct scatterlist
*sgl
,
779 unsigned int sg_len
, enum dma_transfer_direction direction
,
780 unsigned long flags
, void *context
)
782 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
783 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
784 struct dma_slave_config
*sconfig
= &dwc
->dma_sconfig
;
785 struct dw_desc
*prev
;
786 struct dw_desc
*first
;
789 unsigned int reg_width
;
790 unsigned int mem_width
;
791 unsigned int data_width
;
793 struct scatterlist
*sg
;
794 size_t total_len
= 0;
796 dev_vdbg(chan2dev(chan
), "%s\n", __func__
);
798 if (unlikely(!is_slave_direction(direction
) || !sg_len
))
801 dwc
->direction
= direction
;
807 reg_width
= __fls(sconfig
->dst_addr_width
);
808 reg
= sconfig
->dst_addr
;
809 ctllo
= (DWC_DEFAULT_CTLLO(chan
)
810 | DWC_CTLL_DST_WIDTH(reg_width
)
814 ctllo
|= sconfig
->device_fc
? DWC_CTLL_FC(DW_DMA_FC_P_M2P
) :
815 DWC_CTLL_FC(DW_DMA_FC_D_M2P
);
817 data_width
= dw
->data_width
[dwc
->src_master
];
819 for_each_sg(sgl
, sg
, sg_len
, i
) {
820 struct dw_desc
*desc
;
823 mem
= sg_dma_address(sg
);
824 len
= sg_dma_len(sg
);
826 mem_width
= min_t(unsigned int,
827 data_width
, dwc_fast_fls(mem
| len
));
829 slave_sg_todev_fill_desc
:
830 desc
= dwc_desc_get(dwc
);
832 dev_err(chan2dev(chan
),
833 "not enough descriptors available\n");
839 desc
->lli
.ctllo
= ctllo
| DWC_CTLL_SRC_WIDTH(mem_width
);
840 if ((len
>> mem_width
) > dwc
->block_size
) {
841 dlen
= dwc
->block_size
<< mem_width
;
849 desc
->lli
.ctlhi
= dlen
>> mem_width
;
855 prev
->lli
.llp
= desc
->txd
.phys
;
856 list_add_tail(&desc
->desc_node
,
863 goto slave_sg_todev_fill_desc
;
867 reg_width
= __fls(sconfig
->src_addr_width
);
868 reg
= sconfig
->src_addr
;
869 ctllo
= (DWC_DEFAULT_CTLLO(chan
)
870 | DWC_CTLL_SRC_WIDTH(reg_width
)
874 ctllo
|= sconfig
->device_fc
? DWC_CTLL_FC(DW_DMA_FC_P_P2M
) :
875 DWC_CTLL_FC(DW_DMA_FC_D_P2M
);
877 data_width
= dw
->data_width
[dwc
->dst_master
];
879 for_each_sg(sgl
, sg
, sg_len
, i
) {
880 struct dw_desc
*desc
;
883 mem
= sg_dma_address(sg
);
884 len
= sg_dma_len(sg
);
886 mem_width
= min_t(unsigned int,
887 data_width
, dwc_fast_fls(mem
| len
));
889 slave_sg_fromdev_fill_desc
:
890 desc
= dwc_desc_get(dwc
);
892 dev_err(chan2dev(chan
),
893 "not enough descriptors available\n");
899 desc
->lli
.ctllo
= ctllo
| DWC_CTLL_DST_WIDTH(mem_width
);
900 if ((len
>> reg_width
) > dwc
->block_size
) {
901 dlen
= dwc
->block_size
<< reg_width
;
908 desc
->lli
.ctlhi
= dlen
>> reg_width
;
914 prev
->lli
.llp
= desc
->txd
.phys
;
915 list_add_tail(&desc
->desc_node
,
922 goto slave_sg_fromdev_fill_desc
;
929 if (flags
& DMA_PREP_INTERRUPT
)
930 /* Trigger interrupt after last block */
931 prev
->lli
.ctllo
|= DWC_CTLL_INT_EN
;
934 first
->total_len
= total_len
;
939 dwc_desc_put(dwc
, first
);
944 * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
945 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
947 * NOTE: burst size 2 is not supported by controller.
949 * This can be done by finding least significant bit set: n & (n - 1)
951 static inline void convert_burst(u32
*maxburst
)
954 *maxburst
= fls(*maxburst
) - 2;
960 set_runtime_config(struct dma_chan
*chan
, struct dma_slave_config
*sconfig
)
962 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
964 /* Check if chan will be configured for slave transfers */
965 if (!is_slave_direction(sconfig
->direction
))
968 memcpy(&dwc
->dma_sconfig
, sconfig
, sizeof(*sconfig
));
969 dwc
->direction
= sconfig
->direction
;
971 /* Take the request line from slave_id member */
972 if (is_request_line_unset(dwc
))
973 dwc
->request_line
= sconfig
->slave_id
;
975 convert_burst(&dwc
->dma_sconfig
.src_maxburst
);
976 convert_burst(&dwc
->dma_sconfig
.dst_maxburst
);
981 static inline void dwc_chan_pause(struct dw_dma_chan
*dwc
)
983 u32 cfglo
= channel_readl(dwc
, CFG_LO
);
984 unsigned int count
= 20; /* timeout iterations */
986 channel_writel(dwc
, CFG_LO
, cfglo
| DWC_CFGL_CH_SUSP
);
987 while (!(channel_readl(dwc
, CFG_LO
) & DWC_CFGL_FIFO_EMPTY
) && count
--)
993 static inline void dwc_chan_resume(struct dw_dma_chan
*dwc
)
995 u32 cfglo
= channel_readl(dwc
, CFG_LO
);
997 channel_writel(dwc
, CFG_LO
, cfglo
& ~DWC_CFGL_CH_SUSP
);
1002 static int dwc_control(struct dma_chan
*chan
, enum dma_ctrl_cmd cmd
,
1005 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1006 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
1007 struct dw_desc
*desc
, *_desc
;
1008 unsigned long flags
;
1011 if (cmd
== DMA_PAUSE
) {
1012 spin_lock_irqsave(&dwc
->lock
, flags
);
1014 dwc_chan_pause(dwc
);
1016 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1017 } else if (cmd
== DMA_RESUME
) {
1021 spin_lock_irqsave(&dwc
->lock
, flags
);
1023 dwc_chan_resume(dwc
);
1025 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1026 } else if (cmd
== DMA_TERMINATE_ALL
) {
1027 spin_lock_irqsave(&dwc
->lock
, flags
);
1029 clear_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
);
1031 dwc_chan_disable(dw
, dwc
);
1033 dwc_chan_resume(dwc
);
1035 /* active_list entries will end up before queued entries */
1036 list_splice_init(&dwc
->queue
, &list
);
1037 list_splice_init(&dwc
->active_list
, &list
);
1039 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1041 /* Flush all pending and queued descriptors */
1042 list_for_each_entry_safe(desc
, _desc
, &list
, desc_node
)
1043 dwc_descriptor_complete(dwc
, desc
, false);
1044 } else if (cmd
== DMA_SLAVE_CONFIG
) {
1045 return set_runtime_config(chan
, (struct dma_slave_config
*)arg
);
1053 static inline u32
dwc_get_residue(struct dw_dma_chan
*dwc
)
1055 unsigned long flags
;
1058 spin_lock_irqsave(&dwc
->lock
, flags
);
1060 residue
= dwc
->residue
;
1061 if (test_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
) && residue
)
1062 residue
-= dwc_get_sent(dwc
);
1064 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1068 static enum dma_status
1069 dwc_tx_status(struct dma_chan
*chan
,
1070 dma_cookie_t cookie
,
1071 struct dma_tx_state
*txstate
)
1073 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1074 enum dma_status ret
;
1076 ret
= dma_cookie_status(chan
, cookie
, txstate
);
1077 if (ret
== DMA_COMPLETE
)
1080 dwc_scan_descriptors(to_dw_dma(chan
->device
), dwc
);
1082 ret
= dma_cookie_status(chan
, cookie
, txstate
);
1083 if (ret
!= DMA_COMPLETE
)
1084 dma_set_residue(txstate
, dwc_get_residue(dwc
));
1086 if (dwc
->paused
&& ret
== DMA_IN_PROGRESS
)
1092 static void dwc_issue_pending(struct dma_chan
*chan
)
1094 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1096 if (!list_empty(&dwc
->queue
))
1097 dwc_scan_descriptors(to_dw_dma(chan
->device
), dwc
);
1100 static int dwc_alloc_chan_resources(struct dma_chan
*chan
)
1102 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1103 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
1104 struct dw_desc
*desc
;
1106 unsigned long flags
;
1108 dev_vdbg(chan2dev(chan
), "%s\n", __func__
);
1110 /* ASSERT: channel is idle */
1111 if (dma_readl(dw
, CH_EN
) & dwc
->mask
) {
1112 dev_dbg(chan2dev(chan
), "DMA channel not idle?\n");
1116 dma_cookie_init(chan
);
1119 * NOTE: some controllers may have additional features that we
1120 * need to initialize here, like "scatter-gather" (which
1121 * doesn't mean what you think it means), and status writeback.
1124 dwc_set_masters(dwc
);
1126 spin_lock_irqsave(&dwc
->lock
, flags
);
1127 i
= dwc
->descs_allocated
;
1128 while (dwc
->descs_allocated
< NR_DESCS_PER_CHANNEL
) {
1131 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1133 desc
= dma_pool_alloc(dw
->desc_pool
, GFP_ATOMIC
, &phys
);
1135 goto err_desc_alloc
;
1137 memset(desc
, 0, sizeof(struct dw_desc
));
1139 INIT_LIST_HEAD(&desc
->tx_list
);
1140 dma_async_tx_descriptor_init(&desc
->txd
, chan
);
1141 desc
->txd
.tx_submit
= dwc_tx_submit
;
1142 desc
->txd
.flags
= DMA_CTRL_ACK
;
1143 desc
->txd
.phys
= phys
;
1145 dwc_desc_put(dwc
, desc
);
1147 spin_lock_irqsave(&dwc
->lock
, flags
);
1148 i
= ++dwc
->descs_allocated
;
1151 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1153 dev_dbg(chan2dev(chan
), "%s: allocated %d descriptors\n", __func__
, i
);
1158 dev_info(chan2dev(chan
), "only allocated %d descriptors\n", i
);
1163 static void dwc_free_chan_resources(struct dma_chan
*chan
)
1165 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1166 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
1167 struct dw_desc
*desc
, *_desc
;
1168 unsigned long flags
;
1171 dev_dbg(chan2dev(chan
), "%s: descs allocated=%u\n", __func__
,
1172 dwc
->descs_allocated
);
1174 /* ASSERT: channel is idle */
1175 BUG_ON(!list_empty(&dwc
->active_list
));
1176 BUG_ON(!list_empty(&dwc
->queue
));
1177 BUG_ON(dma_readl(to_dw_dma(chan
->device
), CH_EN
) & dwc
->mask
);
1179 spin_lock_irqsave(&dwc
->lock
, flags
);
1180 list_splice_init(&dwc
->free_list
, &list
);
1181 dwc
->descs_allocated
= 0;
1182 dwc
->initialized
= false;
1183 dwc
->request_line
= ~0;
1185 /* Disable interrupts */
1186 channel_clear_bit(dw
, MASK
.XFER
, dwc
->mask
);
1187 channel_clear_bit(dw
, MASK
.ERROR
, dwc
->mask
);
1189 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1191 list_for_each_entry_safe(desc
, _desc
, &list
, desc_node
) {
1192 dev_vdbg(chan2dev(chan
), " freeing descriptor %p\n", desc
);
1193 dma_pool_free(dw
->desc_pool
, desc
, desc
->txd
.phys
);
1196 dev_vdbg(chan2dev(chan
), "%s: done\n", __func__
);
1199 /* --------------------- Cyclic DMA API extensions -------------------- */
1202 * dw_dma_cyclic_start - start the cyclic DMA transfer
1203 * @chan: the DMA channel to start
1205 * Must be called with soft interrupts disabled. Returns zero on success or
1206 * -errno on failure.
1208 int dw_dma_cyclic_start(struct dma_chan
*chan
)
1210 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1211 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
1212 unsigned long flags
;
1214 if (!test_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
)) {
1215 dev_err(chan2dev(&dwc
->chan
), "missing prep for cyclic DMA\n");
1219 spin_lock_irqsave(&dwc
->lock
, flags
);
1221 /* Assert channel is idle */
1222 if (dma_readl(dw
, CH_EN
) & dwc
->mask
) {
1223 dev_err(chan2dev(&dwc
->chan
),
1224 "BUG: Attempted to start non-idle channel\n");
1225 dwc_dump_chan_regs(dwc
);
1226 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1230 dma_writel(dw
, CLEAR
.ERROR
, dwc
->mask
);
1231 dma_writel(dw
, CLEAR
.XFER
, dwc
->mask
);
1233 /* Setup DMAC channel registers */
1234 channel_writel(dwc
, LLP
, dwc
->cdesc
->desc
[0]->txd
.phys
);
1235 channel_writel(dwc
, CTL_LO
, DWC_CTLL_LLP_D_EN
| DWC_CTLL_LLP_S_EN
);
1236 channel_writel(dwc
, CTL_HI
, 0);
1238 channel_set_bit(dw
, CH_EN
, dwc
->mask
);
1240 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1244 EXPORT_SYMBOL(dw_dma_cyclic_start
);
1247 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1248 * @chan: the DMA channel to stop
1250 * Must be called with soft interrupts disabled.
1252 void dw_dma_cyclic_stop(struct dma_chan
*chan
)
1254 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1255 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
1256 unsigned long flags
;
1258 spin_lock_irqsave(&dwc
->lock
, flags
);
1260 dwc_chan_disable(dw
, dwc
);
1262 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1264 EXPORT_SYMBOL(dw_dma_cyclic_stop
);
1267 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1268 * @chan: the DMA channel to prepare
1269 * @buf_addr: physical DMA address where the buffer starts
1270 * @buf_len: total number of bytes for the entire buffer
1271 * @period_len: number of bytes for each period
1272 * @direction: transfer direction, to or from device
1274 * Must be called before trying to start the transfer. Returns a valid struct
1275 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1277 struct dw_cyclic_desc
*dw_dma_cyclic_prep(struct dma_chan
*chan
,
1278 dma_addr_t buf_addr
, size_t buf_len
, size_t period_len
,
1279 enum dma_transfer_direction direction
)
1281 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1282 struct dma_slave_config
*sconfig
= &dwc
->dma_sconfig
;
1283 struct dw_cyclic_desc
*cdesc
;
1284 struct dw_cyclic_desc
*retval
= NULL
;
1285 struct dw_desc
*desc
;
1286 struct dw_desc
*last
= NULL
;
1287 unsigned long was_cyclic
;
1288 unsigned int reg_width
;
1289 unsigned int periods
;
1291 unsigned long flags
;
1293 spin_lock_irqsave(&dwc
->lock
, flags
);
1295 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1296 dev_dbg(chan2dev(&dwc
->chan
),
1297 "channel doesn't support LLP transfers\n");
1298 return ERR_PTR(-EINVAL
);
1301 if (!list_empty(&dwc
->queue
) || !list_empty(&dwc
->active_list
)) {
1302 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1303 dev_dbg(chan2dev(&dwc
->chan
),
1304 "queue and/or active list are not empty\n");
1305 return ERR_PTR(-EBUSY
);
1308 was_cyclic
= test_and_set_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
);
1309 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1311 dev_dbg(chan2dev(&dwc
->chan
),
1312 "channel already prepared for cyclic DMA\n");
1313 return ERR_PTR(-EBUSY
);
1316 retval
= ERR_PTR(-EINVAL
);
1318 if (unlikely(!is_slave_direction(direction
)))
1321 dwc
->direction
= direction
;
1323 if (direction
== DMA_MEM_TO_DEV
)
1324 reg_width
= __ffs(sconfig
->dst_addr_width
);
1326 reg_width
= __ffs(sconfig
->src_addr_width
);
1328 periods
= buf_len
/ period_len
;
1330 /* Check for too big/unaligned periods and unaligned DMA buffer. */
1331 if (period_len
> (dwc
->block_size
<< reg_width
))
1333 if (unlikely(period_len
& ((1 << reg_width
) - 1)))
1335 if (unlikely(buf_addr
& ((1 << reg_width
) - 1)))
1338 retval
= ERR_PTR(-ENOMEM
);
1340 if (periods
> NR_DESCS_PER_CHANNEL
)
1343 cdesc
= kzalloc(sizeof(struct dw_cyclic_desc
), GFP_KERNEL
);
1347 cdesc
->desc
= kzalloc(sizeof(struct dw_desc
*) * periods
, GFP_KERNEL
);
1351 for (i
= 0; i
< periods
; i
++) {
1352 desc
= dwc_desc_get(dwc
);
1354 goto out_err_desc_get
;
1356 switch (direction
) {
1357 case DMA_MEM_TO_DEV
:
1358 desc
->lli
.dar
= sconfig
->dst_addr
;
1359 desc
->lli
.sar
= buf_addr
+ (period_len
* i
);
1360 desc
->lli
.ctllo
= (DWC_DEFAULT_CTLLO(chan
)
1361 | DWC_CTLL_DST_WIDTH(reg_width
)
1362 | DWC_CTLL_SRC_WIDTH(reg_width
)
1367 desc
->lli
.ctllo
|= sconfig
->device_fc
?
1368 DWC_CTLL_FC(DW_DMA_FC_P_M2P
) :
1369 DWC_CTLL_FC(DW_DMA_FC_D_M2P
);
1372 case DMA_DEV_TO_MEM
:
1373 desc
->lli
.dar
= buf_addr
+ (period_len
* i
);
1374 desc
->lli
.sar
= sconfig
->src_addr
;
1375 desc
->lli
.ctllo
= (DWC_DEFAULT_CTLLO(chan
)
1376 | DWC_CTLL_SRC_WIDTH(reg_width
)
1377 | DWC_CTLL_DST_WIDTH(reg_width
)
1382 desc
->lli
.ctllo
|= sconfig
->device_fc
?
1383 DWC_CTLL_FC(DW_DMA_FC_P_P2M
) :
1384 DWC_CTLL_FC(DW_DMA_FC_D_P2M
);
1391 desc
->lli
.ctlhi
= (period_len
>> reg_width
);
1392 cdesc
->desc
[i
] = desc
;
1395 last
->lli
.llp
= desc
->txd
.phys
;
1400 /* Let's make a cyclic list */
1401 last
->lli
.llp
= cdesc
->desc
[0]->txd
.phys
;
1403 dev_dbg(chan2dev(&dwc
->chan
),
1404 "cyclic prepared buf %pad len %zu period %zu periods %d\n",
1405 &buf_addr
, buf_len
, period_len
, periods
);
1407 cdesc
->periods
= periods
;
1414 dwc_desc_put(dwc
, cdesc
->desc
[i
]);
1418 clear_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
);
1419 return (struct dw_cyclic_desc
*)retval
;
1421 EXPORT_SYMBOL(dw_dma_cyclic_prep
);
1424 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1425 * @chan: the DMA channel to free
1427 void dw_dma_cyclic_free(struct dma_chan
*chan
)
1429 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1430 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
1431 struct dw_cyclic_desc
*cdesc
= dwc
->cdesc
;
1433 unsigned long flags
;
1435 dev_dbg(chan2dev(&dwc
->chan
), "%s\n", __func__
);
1440 spin_lock_irqsave(&dwc
->lock
, flags
);
1442 dwc_chan_disable(dw
, dwc
);
1444 dma_writel(dw
, CLEAR
.ERROR
, dwc
->mask
);
1445 dma_writel(dw
, CLEAR
.XFER
, dwc
->mask
);
1447 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1449 for (i
= 0; i
< cdesc
->periods
; i
++)
1450 dwc_desc_put(dwc
, cdesc
->desc
[i
]);
1455 clear_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
);
1457 EXPORT_SYMBOL(dw_dma_cyclic_free
);
1459 /*----------------------------------------------------------------------*/
1461 static void dw_dma_off(struct dw_dma
*dw
)
1465 dma_writel(dw
, CFG
, 0);
1467 channel_clear_bit(dw
, MASK
.XFER
, dw
->all_chan_mask
);
1468 channel_clear_bit(dw
, MASK
.SRC_TRAN
, dw
->all_chan_mask
);
1469 channel_clear_bit(dw
, MASK
.DST_TRAN
, dw
->all_chan_mask
);
1470 channel_clear_bit(dw
, MASK
.ERROR
, dw
->all_chan_mask
);
1472 while (dma_readl(dw
, CFG
) & DW_CFG_DMA_EN
)
1475 for (i
= 0; i
< dw
->dma
.chancnt
; i
++)
1476 dw
->chan
[i
].initialized
= false;
1479 int dw_dma_probe(struct dw_dma_chip
*chip
, struct dw_dma_platform_data
*pdata
)
1484 unsigned int dw_params
;
1485 unsigned int nr_channels
;
1486 unsigned int max_blk_size
= 0;
1490 dw_params
= dma_read_byaddr(chip
->regs
, DW_PARAMS
);
1491 autocfg
= dw_params
>> DW_PARAMS_EN
& 0x1;
1493 dev_dbg(chip
->dev
, "DW_PARAMS: 0x%08x\n", dw_params
);
1495 if (!pdata
&& autocfg
) {
1496 pdata
= devm_kzalloc(chip
->dev
, sizeof(*pdata
), GFP_KERNEL
);
1500 /* Fill platform data with the default values */
1501 pdata
->is_private
= true;
1502 pdata
->chan_allocation_order
= CHAN_ALLOCATION_ASCENDING
;
1503 pdata
->chan_priority
= CHAN_PRIORITY_ASCENDING
;
1504 } else if (!pdata
|| pdata
->nr_channels
> DW_DMA_MAX_NR_CHANNELS
)
1508 nr_channels
= (dw_params
>> DW_PARAMS_NR_CHAN
& 0x7) + 1;
1510 nr_channels
= pdata
->nr_channels
;
1512 size
= sizeof(struct dw_dma
) + nr_channels
* sizeof(struct dw_dma_chan
);
1513 dw
= devm_kzalloc(chip
->dev
, size
, GFP_KERNEL
);
1517 dw
->clk
= devm_clk_get(chip
->dev
, "hclk");
1518 if (IS_ERR(dw
->clk
))
1519 return PTR_ERR(dw
->clk
);
1520 clk_prepare_enable(dw
->clk
);
1522 dw
->regs
= chip
->regs
;
1525 /* Get hardware configuration parameters */
1527 max_blk_size
= dma_readl(dw
, MAX_BLK_SIZE
);
1529 dw
->nr_masters
= (dw_params
>> DW_PARAMS_NR_MASTER
& 3) + 1;
1530 for (i
= 0; i
< dw
->nr_masters
; i
++) {
1532 (dw_params
>> DW_PARAMS_DATA_WIDTH(i
) & 3) + 2;
1535 dw
->nr_masters
= pdata
->nr_masters
;
1536 memcpy(dw
->data_width
, pdata
->data_width
, 4);
1539 /* Calculate all channel mask before DMA setup */
1540 dw
->all_chan_mask
= (1 << nr_channels
) - 1;
1542 /* Force dma off, just in case */
1545 /* Disable BLOCK interrupts as well */
1546 channel_clear_bit(dw
, MASK
.BLOCK
, dw
->all_chan_mask
);
1548 err
= devm_request_irq(chip
->dev
, chip
->irq
, dw_dma_interrupt
,
1549 IRQF_SHARED
, "dw_dmac", dw
);
1553 /* Create a pool of consistent memory blocks for hardware descriptors */
1554 dw
->desc_pool
= dmam_pool_create("dw_dmac_desc_pool", chip
->dev
,
1555 sizeof(struct dw_desc
), 4, 0);
1556 if (!dw
->desc_pool
) {
1557 dev_err(chip
->dev
, "No memory for descriptors dma pool\n");
1561 tasklet_init(&dw
->tasklet
, dw_dma_tasklet
, (unsigned long)dw
);
1563 INIT_LIST_HEAD(&dw
->dma
.channels
);
1564 for (i
= 0; i
< nr_channels
; i
++) {
1565 struct dw_dma_chan
*dwc
= &dw
->chan
[i
];
1566 int r
= nr_channels
- i
- 1;
1568 dwc
->chan
.device
= &dw
->dma
;
1569 dma_cookie_init(&dwc
->chan
);
1570 if (pdata
->chan_allocation_order
== CHAN_ALLOCATION_ASCENDING
)
1571 list_add_tail(&dwc
->chan
.device_node
,
1574 list_add(&dwc
->chan
.device_node
, &dw
->dma
.channels
);
1576 /* 7 is highest priority & 0 is lowest. */
1577 if (pdata
->chan_priority
== CHAN_PRIORITY_ASCENDING
)
1582 dwc
->ch_regs
= &__dw_regs(dw
)->CHAN
[i
];
1583 spin_lock_init(&dwc
->lock
);
1586 INIT_LIST_HEAD(&dwc
->active_list
);
1587 INIT_LIST_HEAD(&dwc
->queue
);
1588 INIT_LIST_HEAD(&dwc
->free_list
);
1590 channel_clear_bit(dw
, CH_EN
, dwc
->mask
);
1592 dwc
->direction
= DMA_TRANS_NONE
;
1593 dwc
->request_line
= ~0;
1595 /* Hardware configuration */
1597 unsigned int dwc_params
;
1598 void __iomem
*addr
= chip
->regs
+ r
* sizeof(u32
);
1600 dwc_params
= dma_read_byaddr(addr
, DWC_PARAMS
);
1602 dev_dbg(chip
->dev
, "DWC_PARAMS[%d]: 0x%08x\n", i
,
1606 * Decode maximum block size for given channel. The
1607 * stored 4 bit value represents blocks from 0x00 for 3
1608 * up to 0x0a for 4095.
1611 (4 << ((max_blk_size
>> 4 * i
) & 0xf)) - 1;
1613 (dwc_params
>> DWC_PARAMS_MBLK_EN
& 0x1) == 0;
1615 dwc
->block_size
= pdata
->block_size
;
1617 /* Check if channel supports multi block transfer */
1618 channel_writel(dwc
, LLP
, 0xfffffffc);
1620 (channel_readl(dwc
, LLP
) & 0xfffffffc) == 0;
1621 channel_writel(dwc
, LLP
, 0);
1625 /* Clear all interrupts on all channels. */
1626 dma_writel(dw
, CLEAR
.XFER
, dw
->all_chan_mask
);
1627 dma_writel(dw
, CLEAR
.BLOCK
, dw
->all_chan_mask
);
1628 dma_writel(dw
, CLEAR
.SRC_TRAN
, dw
->all_chan_mask
);
1629 dma_writel(dw
, CLEAR
.DST_TRAN
, dw
->all_chan_mask
);
1630 dma_writel(dw
, CLEAR
.ERROR
, dw
->all_chan_mask
);
1632 dma_cap_set(DMA_MEMCPY
, dw
->dma
.cap_mask
);
1633 dma_cap_set(DMA_SLAVE
, dw
->dma
.cap_mask
);
1634 if (pdata
->is_private
)
1635 dma_cap_set(DMA_PRIVATE
, dw
->dma
.cap_mask
);
1636 dw
->dma
.dev
= chip
->dev
;
1637 dw
->dma
.device_alloc_chan_resources
= dwc_alloc_chan_resources
;
1638 dw
->dma
.device_free_chan_resources
= dwc_free_chan_resources
;
1640 dw
->dma
.device_prep_dma_memcpy
= dwc_prep_dma_memcpy
;
1642 dw
->dma
.device_prep_slave_sg
= dwc_prep_slave_sg
;
1643 dw
->dma
.device_control
= dwc_control
;
1645 dw
->dma
.device_tx_status
= dwc_tx_status
;
1646 dw
->dma
.device_issue_pending
= dwc_issue_pending
;
1648 dma_writel(dw
, CFG
, DW_CFG_DMA_EN
);
1650 dev_info(chip
->dev
, "DesignWare DMA Controller, %d channels\n",
1653 dma_async_device_register(&dw
->dma
);
1657 EXPORT_SYMBOL_GPL(dw_dma_probe
);
1659 int dw_dma_remove(struct dw_dma_chip
*chip
)
1661 struct dw_dma
*dw
= chip
->dw
;
1662 struct dw_dma_chan
*dwc
, *_dwc
;
1665 dma_async_device_unregister(&dw
->dma
);
1667 tasklet_kill(&dw
->tasklet
);
1669 list_for_each_entry_safe(dwc
, _dwc
, &dw
->dma
.channels
,
1671 list_del(&dwc
->chan
.device_node
);
1672 channel_clear_bit(dw
, CH_EN
, dwc
->mask
);
1677 EXPORT_SYMBOL_GPL(dw_dma_remove
);
1679 void dw_dma_shutdown(struct dw_dma_chip
*chip
)
1681 struct dw_dma
*dw
= chip
->dw
;
1684 clk_disable_unprepare(dw
->clk
);
1686 EXPORT_SYMBOL_GPL(dw_dma_shutdown
);
1688 #ifdef CONFIG_PM_SLEEP
1690 int dw_dma_suspend(struct dw_dma_chip
*chip
)
1692 struct dw_dma
*dw
= chip
->dw
;
1695 clk_disable_unprepare(dw
->clk
);
1699 EXPORT_SYMBOL_GPL(dw_dma_suspend
);
1701 int dw_dma_resume(struct dw_dma_chip
*chip
)
1703 struct dw_dma
*dw
= chip
->dw
;
1705 clk_prepare_enable(dw
->clk
);
1706 dma_writel(dw
, CFG
, DW_CFG_DMA_EN
);
1710 EXPORT_SYMBOL_GPL(dw_dma_resume
);
1712 #endif /* CONFIG_PM_SLEEP */
1714 MODULE_LICENSE("GPL v2");
1715 MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
1716 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1717 MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");