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 : \
48 u8 _dms = (_dwc->direction == DMA_MEM_TO_DEV) ? \
49 _dwc->dws.p_master : _dwc->dws.m_master; \
50 u8 _sms = (_dwc->direction == DMA_DEV_TO_MEM) ? \
51 _dwc->dws.p_master : _dwc->dws.m_master; \
53 (DWC_CTLL_DST_MSIZE(_dmsize) \
54 | DWC_CTLL_SRC_MSIZE(_smsize) \
57 | DWC_CTLL_DMS(_dms) \
58 | DWC_CTLL_SMS(_sms)); \
61 /* The set of bus widths supported by the DMA controller */
62 #define DW_DMA_BUSWIDTHS \
63 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
64 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
65 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
66 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
68 /*----------------------------------------------------------------------*/
70 static struct device
*chan2dev(struct dma_chan
*chan
)
72 return &chan
->dev
->device
;
75 static struct dw_desc
*dwc_first_active(struct dw_dma_chan
*dwc
)
77 return to_dw_desc(dwc
->active_list
.next
);
80 static dma_cookie_t
dwc_tx_submit(struct dma_async_tx_descriptor
*tx
)
82 struct dw_desc
*desc
= txd_to_dw_desc(tx
);
83 struct dw_dma_chan
*dwc
= to_dw_dma_chan(tx
->chan
);
87 spin_lock_irqsave(&dwc
->lock
, flags
);
88 cookie
= dma_cookie_assign(tx
);
91 * REVISIT: We should attempt to chain as many descriptors as
92 * possible, perhaps even appending to those already submitted
93 * for DMA. But this is hard to do in a race-free manner.
96 list_add_tail(&desc
->desc_node
, &dwc
->queue
);
97 spin_unlock_irqrestore(&dwc
->lock
, flags
);
98 dev_vdbg(chan2dev(tx
->chan
), "%s: queued %u\n",
99 __func__
, desc
->txd
.cookie
);
104 static struct dw_desc
*dwc_desc_get(struct dw_dma_chan
*dwc
)
106 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
107 struct dw_desc
*desc
;
110 desc
= dma_pool_zalloc(dw
->desc_pool
, GFP_ATOMIC
, &phys
);
114 dwc
->descs_allocated
++;
115 INIT_LIST_HEAD(&desc
->tx_list
);
116 dma_async_tx_descriptor_init(&desc
->txd
, &dwc
->chan
);
117 desc
->txd
.tx_submit
= dwc_tx_submit
;
118 desc
->txd
.flags
= DMA_CTRL_ACK
;
119 desc
->txd
.phys
= phys
;
123 static void dwc_desc_put(struct dw_dma_chan
*dwc
, struct dw_desc
*desc
)
125 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
126 struct dw_desc
*child
, *_next
;
131 list_for_each_entry_safe(child
, _next
, &desc
->tx_list
, desc_node
) {
132 list_del(&child
->desc_node
);
133 dma_pool_free(dw
->desc_pool
, child
, child
->txd
.phys
);
134 dwc
->descs_allocated
--;
137 dma_pool_free(dw
->desc_pool
, desc
, desc
->txd
.phys
);
138 dwc
->descs_allocated
--;
141 static void dwc_initialize(struct dw_dma_chan
*dwc
)
143 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
144 u32 cfghi
= DWC_CFGH_FIFO_MODE
;
145 u32 cfglo
= DWC_CFGL_CH_PRIOR(dwc
->priority
);
146 bool hs_polarity
= dwc
->dws
.hs_polarity
;
148 if (test_bit(DW_DMA_IS_INITIALIZED
, &dwc
->flags
))
151 cfghi
|= DWC_CFGH_DST_PER(dwc
->dws
.dst_id
);
152 cfghi
|= DWC_CFGH_SRC_PER(dwc
->dws
.src_id
);
154 /* Set polarity of handshake interface */
155 cfglo
|= hs_polarity
? DWC_CFGL_HS_DST_POL
| DWC_CFGL_HS_SRC_POL
: 0;
157 channel_writel(dwc
, CFG_LO
, cfglo
);
158 channel_writel(dwc
, CFG_HI
, cfghi
);
160 /* Enable interrupts */
161 channel_set_bit(dw
, MASK
.XFER
, dwc
->mask
);
162 channel_set_bit(dw
, MASK
.ERROR
, dwc
->mask
);
164 set_bit(DW_DMA_IS_INITIALIZED
, &dwc
->flags
);
167 /*----------------------------------------------------------------------*/
169 static inline void dwc_dump_chan_regs(struct dw_dma_chan
*dwc
)
171 dev_err(chan2dev(&dwc
->chan
),
172 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
173 channel_readl(dwc
, SAR
),
174 channel_readl(dwc
, DAR
),
175 channel_readl(dwc
, LLP
),
176 channel_readl(dwc
, CTL_HI
),
177 channel_readl(dwc
, CTL_LO
));
180 static inline void dwc_chan_disable(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
)
182 channel_clear_bit(dw
, CH_EN
, dwc
->mask
);
183 while (dma_readl(dw
, CH_EN
) & dwc
->mask
)
187 /*----------------------------------------------------------------------*/
189 /* Perform single block transfer */
190 static inline void dwc_do_single_block(struct dw_dma_chan
*dwc
,
191 struct dw_desc
*desc
)
193 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
197 * Software emulation of LLP mode relies on interrupts to continue
198 * multi block transfer.
200 ctllo
= lli_read(desc
, ctllo
) | DWC_CTLL_INT_EN
;
202 channel_writel(dwc
, SAR
, lli_read(desc
, sar
));
203 channel_writel(dwc
, DAR
, lli_read(desc
, dar
));
204 channel_writel(dwc
, CTL_LO
, ctllo
);
205 channel_writel(dwc
, CTL_HI
, lli_read(desc
, ctlhi
));
206 channel_set_bit(dw
, CH_EN
, dwc
->mask
);
208 /* Move pointer to next descriptor */
209 dwc
->tx_node_active
= dwc
->tx_node_active
->next
;
212 /* Called with dwc->lock held and bh disabled */
213 static void dwc_dostart(struct dw_dma_chan
*dwc
, struct dw_desc
*first
)
215 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
216 u8 lms
= DWC_LLP_LMS(dwc
->dws
.m_master
);
217 unsigned long was_soft_llp
;
219 /* ASSERT: channel is idle */
220 if (dma_readl(dw
, CH_EN
) & dwc
->mask
) {
221 dev_err(chan2dev(&dwc
->chan
),
222 "%s: BUG: Attempted to start non-idle channel\n",
224 dwc_dump_chan_regs(dwc
);
226 /* The tasklet will hopefully advance the queue... */
231 was_soft_llp
= test_and_set_bit(DW_DMA_IS_SOFT_LLP
,
234 dev_err(chan2dev(&dwc
->chan
),
235 "BUG: Attempted to start new LLP transfer inside ongoing one\n");
241 first
->residue
= first
->total_len
;
242 dwc
->tx_node_active
= &first
->tx_list
;
244 /* Submit first block */
245 dwc_do_single_block(dwc
, first
);
252 channel_writel(dwc
, LLP
, first
->txd
.phys
| lms
);
253 channel_writel(dwc
, CTL_LO
, DWC_CTLL_LLP_D_EN
| DWC_CTLL_LLP_S_EN
);
254 channel_writel(dwc
, CTL_HI
, 0);
255 channel_set_bit(dw
, CH_EN
, dwc
->mask
);
258 static void dwc_dostart_first_queued(struct dw_dma_chan
*dwc
)
260 struct dw_desc
*desc
;
262 if (list_empty(&dwc
->queue
))
265 list_move(dwc
->queue
.next
, &dwc
->active_list
);
266 desc
= dwc_first_active(dwc
);
267 dev_vdbg(chan2dev(&dwc
->chan
), "%s: started %u\n", __func__
, desc
->txd
.cookie
);
268 dwc_dostart(dwc
, desc
);
271 /*----------------------------------------------------------------------*/
274 dwc_descriptor_complete(struct dw_dma_chan
*dwc
, struct dw_desc
*desc
,
275 bool callback_required
)
277 struct dma_async_tx_descriptor
*txd
= &desc
->txd
;
278 struct dw_desc
*child
;
280 struct dmaengine_desc_callback cb
;
282 dev_vdbg(chan2dev(&dwc
->chan
), "descriptor %u complete\n", txd
->cookie
);
284 spin_lock_irqsave(&dwc
->lock
, flags
);
285 dma_cookie_complete(txd
);
286 if (callback_required
)
287 dmaengine_desc_get_callback(txd
, &cb
);
289 memset(&cb
, 0, sizeof(cb
));
292 list_for_each_entry(child
, &desc
->tx_list
, desc_node
)
293 async_tx_ack(&child
->txd
);
294 async_tx_ack(&desc
->txd
);
295 dwc_desc_put(dwc
, desc
);
296 spin_unlock_irqrestore(&dwc
->lock
, flags
);
298 dmaengine_desc_callback_invoke(&cb
, NULL
);
301 static void dwc_complete_all(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
)
303 struct dw_desc
*desc
, *_desc
;
307 spin_lock_irqsave(&dwc
->lock
, flags
);
308 if (dma_readl(dw
, CH_EN
) & dwc
->mask
) {
309 dev_err(chan2dev(&dwc
->chan
),
310 "BUG: XFER bit set, but channel not idle!\n");
312 /* Try to continue after resetting the channel... */
313 dwc_chan_disable(dw
, dwc
);
317 * Submit queued descriptors ASAP, i.e. before we go through
318 * the completed ones.
320 list_splice_init(&dwc
->active_list
, &list
);
321 dwc_dostart_first_queued(dwc
);
323 spin_unlock_irqrestore(&dwc
->lock
, flags
);
325 list_for_each_entry_safe(desc
, _desc
, &list
, desc_node
)
326 dwc_descriptor_complete(dwc
, desc
, true);
329 /* Returns how many bytes were already received from source */
330 static inline u32
dwc_get_sent(struct dw_dma_chan
*dwc
)
332 u32 ctlhi
= channel_readl(dwc
, CTL_HI
);
333 u32 ctllo
= channel_readl(dwc
, CTL_LO
);
335 return (ctlhi
& DWC_CTLH_BLOCK_TS_MASK
) * (1 << (ctllo
>> 4 & 7));
338 static void dwc_scan_descriptors(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
)
341 struct dw_desc
*desc
, *_desc
;
342 struct dw_desc
*child
;
346 spin_lock_irqsave(&dwc
->lock
, flags
);
347 llp
= channel_readl(dwc
, LLP
);
348 status_xfer
= dma_readl(dw
, RAW
.XFER
);
350 if (status_xfer
& dwc
->mask
) {
351 /* Everything we've submitted is done */
352 dma_writel(dw
, CLEAR
.XFER
, dwc
->mask
);
354 if (test_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
)) {
355 struct list_head
*head
, *active
= dwc
->tx_node_active
;
358 * We are inside first active descriptor.
359 * Otherwise something is really wrong.
361 desc
= dwc_first_active(dwc
);
363 head
= &desc
->tx_list
;
364 if (active
!= head
) {
365 /* Update residue to reflect last sent descriptor */
366 if (active
== head
->next
)
367 desc
->residue
-= desc
->len
;
369 desc
->residue
-= to_dw_desc(active
->prev
)->len
;
371 child
= to_dw_desc(active
);
373 /* Submit next block */
374 dwc_do_single_block(dwc
, child
);
376 spin_unlock_irqrestore(&dwc
->lock
, flags
);
380 /* We are done here */
381 clear_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
);
384 spin_unlock_irqrestore(&dwc
->lock
, flags
);
386 dwc_complete_all(dw
, dwc
);
390 if (list_empty(&dwc
->active_list
)) {
391 spin_unlock_irqrestore(&dwc
->lock
, flags
);
395 if (test_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
)) {
396 dev_vdbg(chan2dev(&dwc
->chan
), "%s: soft LLP mode\n", __func__
);
397 spin_unlock_irqrestore(&dwc
->lock
, flags
);
401 dev_vdbg(chan2dev(&dwc
->chan
), "%s: llp=%pad\n", __func__
, &llp
);
403 list_for_each_entry_safe(desc
, _desc
, &dwc
->active_list
, desc_node
) {
404 /* Initial residue value */
405 desc
->residue
= desc
->total_len
;
407 /* Check first descriptors addr */
408 if (desc
->txd
.phys
== DWC_LLP_LOC(llp
)) {
409 spin_unlock_irqrestore(&dwc
->lock
, flags
);
413 /* Check first descriptors llp */
414 if (lli_read(desc
, llp
) == llp
) {
415 /* This one is currently in progress */
416 desc
->residue
-= dwc_get_sent(dwc
);
417 spin_unlock_irqrestore(&dwc
->lock
, flags
);
421 desc
->residue
-= desc
->len
;
422 list_for_each_entry(child
, &desc
->tx_list
, desc_node
) {
423 if (lli_read(child
, llp
) == llp
) {
424 /* Currently in progress */
425 desc
->residue
-= dwc_get_sent(dwc
);
426 spin_unlock_irqrestore(&dwc
->lock
, flags
);
429 desc
->residue
-= child
->len
;
433 * No descriptors so far seem to be in progress, i.e.
434 * this one must be done.
436 spin_unlock_irqrestore(&dwc
->lock
, flags
);
437 dwc_descriptor_complete(dwc
, desc
, true);
438 spin_lock_irqsave(&dwc
->lock
, flags
);
441 dev_err(chan2dev(&dwc
->chan
),
442 "BUG: All descriptors done, but channel not idle!\n");
444 /* Try to continue after resetting the channel... */
445 dwc_chan_disable(dw
, dwc
);
447 dwc_dostart_first_queued(dwc
);
448 spin_unlock_irqrestore(&dwc
->lock
, flags
);
451 static inline void dwc_dump_lli(struct dw_dma_chan
*dwc
, struct dw_desc
*desc
)
453 dev_crit(chan2dev(&dwc
->chan
), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
457 lli_read(desc
, ctlhi
),
458 lli_read(desc
, ctllo
));
461 static void dwc_handle_error(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
)
463 struct dw_desc
*bad_desc
;
464 struct dw_desc
*child
;
467 dwc_scan_descriptors(dw
, dwc
);
469 spin_lock_irqsave(&dwc
->lock
, flags
);
472 * The descriptor currently at the head of the active list is
473 * borked. Since we don't have any way to report errors, we'll
474 * just have to scream loudly and try to carry on.
476 bad_desc
= dwc_first_active(dwc
);
477 list_del_init(&bad_desc
->desc_node
);
478 list_move(dwc
->queue
.next
, dwc
->active_list
.prev
);
480 /* Clear the error flag and try to restart the controller */
481 dma_writel(dw
, CLEAR
.ERROR
, dwc
->mask
);
482 if (!list_empty(&dwc
->active_list
))
483 dwc_dostart(dwc
, dwc_first_active(dwc
));
486 * WARN may seem harsh, but since this only happens
487 * when someone submits a bad physical address in a
488 * descriptor, we should consider ourselves lucky that the
489 * controller flagged an error instead of scribbling over
490 * random memory locations.
492 dev_WARN(chan2dev(&dwc
->chan
), "Bad descriptor submitted for DMA!\n"
493 " cookie: %d\n", bad_desc
->txd
.cookie
);
494 dwc_dump_lli(dwc
, bad_desc
);
495 list_for_each_entry(child
, &bad_desc
->tx_list
, desc_node
)
496 dwc_dump_lli(dwc
, child
);
498 spin_unlock_irqrestore(&dwc
->lock
, flags
);
500 /* Pretend the descriptor completed successfully */
501 dwc_descriptor_complete(dwc
, bad_desc
, true);
504 /* --------------------- Cyclic DMA API extensions -------------------- */
506 dma_addr_t
dw_dma_get_src_addr(struct dma_chan
*chan
)
508 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
509 return channel_readl(dwc
, SAR
);
511 EXPORT_SYMBOL(dw_dma_get_src_addr
);
513 dma_addr_t
dw_dma_get_dst_addr(struct dma_chan
*chan
)
515 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
516 return channel_readl(dwc
, DAR
);
518 EXPORT_SYMBOL(dw_dma_get_dst_addr
);
520 /* Called with dwc->lock held and all DMAC interrupts disabled */
521 static void dwc_handle_cyclic(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
,
522 u32 status_block
, u32 status_err
, u32 status_xfer
)
526 if (status_block
& dwc
->mask
) {
527 void (*callback
)(void *param
);
528 void *callback_param
;
530 dev_vdbg(chan2dev(&dwc
->chan
), "new cyclic period llp 0x%08x\n",
531 channel_readl(dwc
, LLP
));
532 dma_writel(dw
, CLEAR
.BLOCK
, dwc
->mask
);
534 callback
= dwc
->cdesc
->period_callback
;
535 callback_param
= dwc
->cdesc
->period_callback_param
;
538 callback(callback_param
);
542 * Error and transfer complete are highly unlikely, and will most
543 * likely be due to a configuration error by the user.
545 if (unlikely(status_err
& dwc
->mask
) ||
546 unlikely(status_xfer
& dwc
->mask
)) {
549 dev_err(chan2dev(&dwc
->chan
),
550 "cyclic DMA unexpected %s interrupt, stopping DMA transfer\n",
551 status_xfer
? "xfer" : "error");
553 spin_lock_irqsave(&dwc
->lock
, flags
);
555 dwc_dump_chan_regs(dwc
);
557 dwc_chan_disable(dw
, dwc
);
559 /* Make sure DMA does not restart by loading a new list */
560 channel_writel(dwc
, LLP
, 0);
561 channel_writel(dwc
, CTL_LO
, 0);
562 channel_writel(dwc
, CTL_HI
, 0);
564 dma_writel(dw
, CLEAR
.BLOCK
, dwc
->mask
);
565 dma_writel(dw
, CLEAR
.ERROR
, dwc
->mask
);
566 dma_writel(dw
, CLEAR
.XFER
, dwc
->mask
);
568 for (i
= 0; i
< dwc
->cdesc
->periods
; i
++)
569 dwc_dump_lli(dwc
, dwc
->cdesc
->desc
[i
]);
571 spin_unlock_irqrestore(&dwc
->lock
, flags
);
574 /* Re-enable interrupts */
575 channel_set_bit(dw
, MASK
.BLOCK
, dwc
->mask
);
578 /* ------------------------------------------------------------------------- */
580 static void dw_dma_tasklet(unsigned long data
)
582 struct dw_dma
*dw
= (struct dw_dma
*)data
;
583 struct dw_dma_chan
*dwc
;
589 status_block
= dma_readl(dw
, RAW
.BLOCK
);
590 status_xfer
= dma_readl(dw
, RAW
.XFER
);
591 status_err
= dma_readl(dw
, RAW
.ERROR
);
593 dev_vdbg(dw
->dma
.dev
, "%s: status_err=%x\n", __func__
, status_err
);
595 for (i
= 0; i
< dw
->dma
.chancnt
; i
++) {
597 if (test_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
))
598 dwc_handle_cyclic(dw
, dwc
, status_block
, status_err
,
600 else if (status_err
& (1 << i
))
601 dwc_handle_error(dw
, dwc
);
602 else if (status_xfer
& (1 << i
))
603 dwc_scan_descriptors(dw
, dwc
);
606 /* Re-enable interrupts */
607 channel_set_bit(dw
, MASK
.XFER
, dw
->all_chan_mask
);
608 channel_set_bit(dw
, MASK
.ERROR
, dw
->all_chan_mask
);
611 static irqreturn_t
dw_dma_interrupt(int irq
, void *dev_id
)
613 struct dw_dma
*dw
= dev_id
;
616 /* Check if we have any interrupt from the DMAC which is not in use */
620 status
= dma_readl(dw
, STATUS_INT
);
621 dev_vdbg(dw
->dma
.dev
, "%s: status=0x%x\n", __func__
, status
);
623 /* Check if we have any interrupt from the DMAC */
628 * Just disable the interrupts. We'll turn them back on in the
631 channel_clear_bit(dw
, MASK
.XFER
, dw
->all_chan_mask
);
632 channel_clear_bit(dw
, MASK
.BLOCK
, dw
->all_chan_mask
);
633 channel_clear_bit(dw
, MASK
.ERROR
, dw
->all_chan_mask
);
635 status
= dma_readl(dw
, STATUS_INT
);
638 "BUG: Unexpected interrupts pending: 0x%x\n",
642 channel_clear_bit(dw
, MASK
.XFER
, (1 << 8) - 1);
643 channel_clear_bit(dw
, MASK
.BLOCK
, (1 << 8) - 1);
644 channel_clear_bit(dw
, MASK
.SRC_TRAN
, (1 << 8) - 1);
645 channel_clear_bit(dw
, MASK
.DST_TRAN
, (1 << 8) - 1);
646 channel_clear_bit(dw
, MASK
.ERROR
, (1 << 8) - 1);
649 tasklet_schedule(&dw
->tasklet
);
654 /*----------------------------------------------------------------------*/
656 static struct dma_async_tx_descriptor
*
657 dwc_prep_dma_memcpy(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t src
,
658 size_t len
, unsigned long flags
)
660 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
661 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
662 struct dw_desc
*desc
;
663 struct dw_desc
*first
;
664 struct dw_desc
*prev
;
667 u8 m_master
= dwc
->dws
.m_master
;
668 unsigned int src_width
;
669 unsigned int dst_width
;
670 unsigned int data_width
= dw
->pdata
->data_width
[m_master
];
672 u8 lms
= DWC_LLP_LMS(m_master
);
674 dev_vdbg(chan2dev(chan
),
675 "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__
,
676 &dest
, &src
, len
, flags
);
678 if (unlikely(!len
)) {
679 dev_dbg(chan2dev(chan
), "%s: length is zero!\n", __func__
);
683 dwc
->direction
= DMA_MEM_TO_MEM
;
685 src_width
= dst_width
= __ffs(data_width
| src
| dest
| len
);
687 ctllo
= DWC_DEFAULT_CTLLO(chan
)
688 | DWC_CTLL_DST_WIDTH(dst_width
)
689 | DWC_CTLL_SRC_WIDTH(src_width
)
695 for (offset
= 0; offset
< len
; offset
+= xfer_count
<< src_width
) {
696 xfer_count
= min_t(size_t, (len
- offset
) >> src_width
,
699 desc
= dwc_desc_get(dwc
);
703 lli_write(desc
, sar
, src
+ offset
);
704 lli_write(desc
, dar
, dest
+ offset
);
705 lli_write(desc
, ctllo
, ctllo
);
706 lli_write(desc
, ctlhi
, xfer_count
);
707 desc
->len
= xfer_count
<< src_width
;
712 lli_write(prev
, llp
, desc
->txd
.phys
| lms
);
713 list_add_tail(&desc
->desc_node
, &first
->tx_list
);
718 if (flags
& DMA_PREP_INTERRUPT
)
719 /* Trigger interrupt after last block */
720 lli_set(prev
, ctllo
, DWC_CTLL_INT_EN
);
723 lli_clear(prev
, ctllo
, DWC_CTLL_LLP_D_EN
| DWC_CTLL_LLP_S_EN
);
724 first
->txd
.flags
= flags
;
725 first
->total_len
= len
;
730 dwc_desc_put(dwc
, first
);
734 static struct dma_async_tx_descriptor
*
735 dwc_prep_slave_sg(struct dma_chan
*chan
, struct scatterlist
*sgl
,
736 unsigned int sg_len
, enum dma_transfer_direction direction
,
737 unsigned long flags
, void *context
)
739 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
740 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
741 struct dma_slave_config
*sconfig
= &dwc
->dma_sconfig
;
742 struct dw_desc
*prev
;
743 struct dw_desc
*first
;
745 u8 m_master
= dwc
->dws
.m_master
;
746 u8 lms
= DWC_LLP_LMS(m_master
);
748 unsigned int reg_width
;
749 unsigned int mem_width
;
750 unsigned int data_width
= dw
->pdata
->data_width
[m_master
];
752 struct scatterlist
*sg
;
753 size_t total_len
= 0;
755 dev_vdbg(chan2dev(chan
), "%s\n", __func__
);
757 if (unlikely(!is_slave_direction(direction
) || !sg_len
))
760 dwc
->direction
= direction
;
766 reg_width
= __ffs(sconfig
->dst_addr_width
);
767 reg
= sconfig
->dst_addr
;
768 ctllo
= (DWC_DEFAULT_CTLLO(chan
)
769 | DWC_CTLL_DST_WIDTH(reg_width
)
773 ctllo
|= sconfig
->device_fc
? DWC_CTLL_FC(DW_DMA_FC_P_M2P
) :
774 DWC_CTLL_FC(DW_DMA_FC_D_M2P
);
776 for_each_sg(sgl
, sg
, sg_len
, i
) {
777 struct dw_desc
*desc
;
780 mem
= sg_dma_address(sg
);
781 len
= sg_dma_len(sg
);
783 mem_width
= __ffs(data_width
| mem
| len
);
785 slave_sg_todev_fill_desc
:
786 desc
= dwc_desc_get(dwc
);
790 lli_write(desc
, sar
, mem
);
791 lli_write(desc
, dar
, reg
);
792 lli_write(desc
, ctllo
, ctllo
| DWC_CTLL_SRC_WIDTH(mem_width
));
793 if ((len
>> mem_width
) > dwc
->block_size
) {
794 dlen
= dwc
->block_size
<< mem_width
;
802 lli_write(desc
, ctlhi
, dlen
>> mem_width
);
808 lli_write(prev
, llp
, desc
->txd
.phys
| lms
);
809 list_add_tail(&desc
->desc_node
, &first
->tx_list
);
815 goto slave_sg_todev_fill_desc
;
819 reg_width
= __ffs(sconfig
->src_addr_width
);
820 reg
= sconfig
->src_addr
;
821 ctllo
= (DWC_DEFAULT_CTLLO(chan
)
822 | DWC_CTLL_SRC_WIDTH(reg_width
)
826 ctllo
|= sconfig
->device_fc
? DWC_CTLL_FC(DW_DMA_FC_P_P2M
) :
827 DWC_CTLL_FC(DW_DMA_FC_D_P2M
);
829 for_each_sg(sgl
, sg
, sg_len
, i
) {
830 struct dw_desc
*desc
;
833 mem
= sg_dma_address(sg
);
834 len
= sg_dma_len(sg
);
836 mem_width
= __ffs(data_width
| mem
| len
);
838 slave_sg_fromdev_fill_desc
:
839 desc
= dwc_desc_get(dwc
);
843 lli_write(desc
, sar
, reg
);
844 lli_write(desc
, dar
, mem
);
845 lli_write(desc
, ctllo
, ctllo
| DWC_CTLL_DST_WIDTH(mem_width
));
846 if ((len
>> reg_width
) > dwc
->block_size
) {
847 dlen
= dwc
->block_size
<< reg_width
;
854 lli_write(desc
, ctlhi
, dlen
>> reg_width
);
860 lli_write(prev
, llp
, desc
->txd
.phys
| lms
);
861 list_add_tail(&desc
->desc_node
, &first
->tx_list
);
867 goto slave_sg_fromdev_fill_desc
;
874 if (flags
& DMA_PREP_INTERRUPT
)
875 /* Trigger interrupt after last block */
876 lli_set(prev
, ctllo
, DWC_CTLL_INT_EN
);
879 lli_clear(prev
, ctllo
, DWC_CTLL_LLP_D_EN
| DWC_CTLL_LLP_S_EN
);
880 first
->total_len
= total_len
;
885 dev_err(chan2dev(chan
),
886 "not enough descriptors available. Direction %d\n", direction
);
887 dwc_desc_put(dwc
, first
);
891 bool dw_dma_filter(struct dma_chan
*chan
, void *param
)
893 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
894 struct dw_dma_slave
*dws
= param
;
896 if (dws
->dma_dev
!= chan
->device
->dev
)
899 /* We have to copy data since dws can be temporary storage */
900 memcpy(&dwc
->dws
, dws
, sizeof(struct dw_dma_slave
));
904 EXPORT_SYMBOL_GPL(dw_dma_filter
);
907 * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
908 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
910 * NOTE: burst size 2 is not supported by controller.
912 * This can be done by finding least significant bit set: n & (n - 1)
914 static inline void convert_burst(u32
*maxburst
)
917 *maxburst
= fls(*maxburst
) - 2;
922 static int dwc_config(struct dma_chan
*chan
, struct dma_slave_config
*sconfig
)
924 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
926 /* Check if chan will be configured for slave transfers */
927 if (!is_slave_direction(sconfig
->direction
))
930 memcpy(&dwc
->dma_sconfig
, sconfig
, sizeof(*sconfig
));
931 dwc
->direction
= sconfig
->direction
;
933 convert_burst(&dwc
->dma_sconfig
.src_maxburst
);
934 convert_burst(&dwc
->dma_sconfig
.dst_maxburst
);
939 static int dwc_pause(struct dma_chan
*chan
)
941 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
943 unsigned int count
= 20; /* timeout iterations */
946 spin_lock_irqsave(&dwc
->lock
, flags
);
948 cfglo
= channel_readl(dwc
, CFG_LO
);
949 channel_writel(dwc
, CFG_LO
, cfglo
| DWC_CFGL_CH_SUSP
);
950 while (!(channel_readl(dwc
, CFG_LO
) & DWC_CFGL_FIFO_EMPTY
) && count
--)
953 set_bit(DW_DMA_IS_PAUSED
, &dwc
->flags
);
955 spin_unlock_irqrestore(&dwc
->lock
, flags
);
960 static inline void dwc_chan_resume(struct dw_dma_chan
*dwc
)
962 u32 cfglo
= channel_readl(dwc
, CFG_LO
);
964 channel_writel(dwc
, CFG_LO
, cfglo
& ~DWC_CFGL_CH_SUSP
);
966 clear_bit(DW_DMA_IS_PAUSED
, &dwc
->flags
);
969 static int dwc_resume(struct dma_chan
*chan
)
971 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
974 spin_lock_irqsave(&dwc
->lock
, flags
);
976 if (test_bit(DW_DMA_IS_PAUSED
, &dwc
->flags
))
977 dwc_chan_resume(dwc
);
979 spin_unlock_irqrestore(&dwc
->lock
, flags
);
984 static int dwc_terminate_all(struct dma_chan
*chan
)
986 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
987 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
988 struct dw_desc
*desc
, *_desc
;
992 spin_lock_irqsave(&dwc
->lock
, flags
);
994 clear_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
);
996 dwc_chan_disable(dw
, dwc
);
998 dwc_chan_resume(dwc
);
1000 /* active_list entries will end up before queued entries */
1001 list_splice_init(&dwc
->queue
, &list
);
1002 list_splice_init(&dwc
->active_list
, &list
);
1004 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1006 /* Flush all pending and queued descriptors */
1007 list_for_each_entry_safe(desc
, _desc
, &list
, desc_node
)
1008 dwc_descriptor_complete(dwc
, desc
, false);
1013 static struct dw_desc
*dwc_find_desc(struct dw_dma_chan
*dwc
, dma_cookie_t c
)
1015 struct dw_desc
*desc
;
1017 list_for_each_entry(desc
, &dwc
->active_list
, desc_node
)
1018 if (desc
->txd
.cookie
== c
)
1024 static u32
dwc_get_residue(struct dw_dma_chan
*dwc
, dma_cookie_t cookie
)
1026 struct dw_desc
*desc
;
1027 unsigned long flags
;
1030 spin_lock_irqsave(&dwc
->lock
, flags
);
1032 desc
= dwc_find_desc(dwc
, cookie
);
1034 if (desc
== dwc_first_active(dwc
)) {
1035 residue
= desc
->residue
;
1036 if (test_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
) && residue
)
1037 residue
-= dwc_get_sent(dwc
);
1039 residue
= desc
->total_len
;
1045 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1049 static enum dma_status
1050 dwc_tx_status(struct dma_chan
*chan
,
1051 dma_cookie_t cookie
,
1052 struct dma_tx_state
*txstate
)
1054 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1055 enum dma_status ret
;
1057 ret
= dma_cookie_status(chan
, cookie
, txstate
);
1058 if (ret
== DMA_COMPLETE
)
1061 dwc_scan_descriptors(to_dw_dma(chan
->device
), dwc
);
1063 ret
= dma_cookie_status(chan
, cookie
, txstate
);
1064 if (ret
== DMA_COMPLETE
)
1067 dma_set_residue(txstate
, dwc_get_residue(dwc
, cookie
));
1069 if (test_bit(DW_DMA_IS_PAUSED
, &dwc
->flags
) && ret
== DMA_IN_PROGRESS
)
1075 static void dwc_issue_pending(struct dma_chan
*chan
)
1077 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1078 unsigned long flags
;
1080 spin_lock_irqsave(&dwc
->lock
, flags
);
1081 if (list_empty(&dwc
->active_list
))
1082 dwc_dostart_first_queued(dwc
);
1083 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1086 /*----------------------------------------------------------------------*/
1088 static void dw_dma_off(struct dw_dma
*dw
)
1092 dma_writel(dw
, CFG
, 0);
1094 channel_clear_bit(dw
, MASK
.XFER
, dw
->all_chan_mask
);
1095 channel_clear_bit(dw
, MASK
.BLOCK
, dw
->all_chan_mask
);
1096 channel_clear_bit(dw
, MASK
.SRC_TRAN
, dw
->all_chan_mask
);
1097 channel_clear_bit(dw
, MASK
.DST_TRAN
, dw
->all_chan_mask
);
1098 channel_clear_bit(dw
, MASK
.ERROR
, dw
->all_chan_mask
);
1100 while (dma_readl(dw
, CFG
) & DW_CFG_DMA_EN
)
1103 for (i
= 0; i
< dw
->dma
.chancnt
; i
++)
1104 clear_bit(DW_DMA_IS_INITIALIZED
, &dw
->chan
[i
].flags
);
1107 static void dw_dma_on(struct dw_dma
*dw
)
1109 dma_writel(dw
, CFG
, DW_CFG_DMA_EN
);
1112 static int dwc_alloc_chan_resources(struct dma_chan
*chan
)
1114 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1115 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
1117 dev_vdbg(chan2dev(chan
), "%s\n", __func__
);
1119 /* ASSERT: channel is idle */
1120 if (dma_readl(dw
, CH_EN
) & dwc
->mask
) {
1121 dev_dbg(chan2dev(chan
), "DMA channel not idle?\n");
1125 dma_cookie_init(chan
);
1128 * NOTE: some controllers may have additional features that we
1129 * need to initialize here, like "scatter-gather" (which
1130 * doesn't mean what you think it means), and status writeback.
1134 * We need controller-specific data to set up slave transfers.
1136 if (chan
->private && !dw_dma_filter(chan
, chan
->private)) {
1137 dev_warn(chan2dev(chan
), "Wrong controller-specific data\n");
1141 /* Enable controller here if needed */
1144 dw
->in_use
|= dwc
->mask
;
1149 static void dwc_free_chan_resources(struct dma_chan
*chan
)
1151 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1152 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
1153 unsigned long flags
;
1156 dev_dbg(chan2dev(chan
), "%s: descs allocated=%u\n", __func__
,
1157 dwc
->descs_allocated
);
1159 /* ASSERT: channel is idle */
1160 BUG_ON(!list_empty(&dwc
->active_list
));
1161 BUG_ON(!list_empty(&dwc
->queue
));
1162 BUG_ON(dma_readl(to_dw_dma(chan
->device
), CH_EN
) & dwc
->mask
);
1164 spin_lock_irqsave(&dwc
->lock
, flags
);
1166 /* Clear custom channel configuration */
1167 memset(&dwc
->dws
, 0, sizeof(struct dw_dma_slave
));
1169 clear_bit(DW_DMA_IS_INITIALIZED
, &dwc
->flags
);
1171 /* Disable interrupts */
1172 channel_clear_bit(dw
, MASK
.XFER
, dwc
->mask
);
1173 channel_clear_bit(dw
, MASK
.BLOCK
, dwc
->mask
);
1174 channel_clear_bit(dw
, MASK
.ERROR
, dwc
->mask
);
1176 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1178 /* Disable controller in case it was a last user */
1179 dw
->in_use
&= ~dwc
->mask
;
1183 dev_vdbg(chan2dev(chan
), "%s: done\n", __func__
);
1186 /* --------------------- Cyclic DMA API extensions -------------------- */
1189 * dw_dma_cyclic_start - start the cyclic DMA transfer
1190 * @chan: the DMA channel to start
1192 * Must be called with soft interrupts disabled. Returns zero on success or
1193 * -errno on failure.
1195 int dw_dma_cyclic_start(struct dma_chan
*chan
)
1197 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1198 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
1199 unsigned long flags
;
1201 if (!test_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
)) {
1202 dev_err(chan2dev(&dwc
->chan
), "missing prep for cyclic DMA\n");
1206 spin_lock_irqsave(&dwc
->lock
, flags
);
1208 /* Enable interrupts to perform cyclic transfer */
1209 channel_set_bit(dw
, MASK
.BLOCK
, dwc
->mask
);
1211 dwc_dostart(dwc
, dwc
->cdesc
->desc
[0]);
1213 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1217 EXPORT_SYMBOL(dw_dma_cyclic_start
);
1220 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1221 * @chan: the DMA channel to stop
1223 * Must be called with soft interrupts disabled.
1225 void dw_dma_cyclic_stop(struct dma_chan
*chan
)
1227 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1228 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
1229 unsigned long flags
;
1231 spin_lock_irqsave(&dwc
->lock
, flags
);
1233 dwc_chan_disable(dw
, dwc
);
1235 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1237 EXPORT_SYMBOL(dw_dma_cyclic_stop
);
1240 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1241 * @chan: the DMA channel to prepare
1242 * @buf_addr: physical DMA address where the buffer starts
1243 * @buf_len: total number of bytes for the entire buffer
1244 * @period_len: number of bytes for each period
1245 * @direction: transfer direction, to or from device
1247 * Must be called before trying to start the transfer. Returns a valid struct
1248 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1250 struct dw_cyclic_desc
*dw_dma_cyclic_prep(struct dma_chan
*chan
,
1251 dma_addr_t buf_addr
, size_t buf_len
, size_t period_len
,
1252 enum dma_transfer_direction direction
)
1254 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1255 struct dma_slave_config
*sconfig
= &dwc
->dma_sconfig
;
1256 struct dw_cyclic_desc
*cdesc
;
1257 struct dw_cyclic_desc
*retval
= NULL
;
1258 struct dw_desc
*desc
;
1259 struct dw_desc
*last
= NULL
;
1260 u8 lms
= DWC_LLP_LMS(dwc
->dws
.m_master
);
1261 unsigned long was_cyclic
;
1262 unsigned int reg_width
;
1263 unsigned int periods
;
1265 unsigned long flags
;
1267 spin_lock_irqsave(&dwc
->lock
, flags
);
1269 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1270 dev_dbg(chan2dev(&dwc
->chan
),
1271 "channel doesn't support LLP transfers\n");
1272 return ERR_PTR(-EINVAL
);
1275 if (!list_empty(&dwc
->queue
) || !list_empty(&dwc
->active_list
)) {
1276 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1277 dev_dbg(chan2dev(&dwc
->chan
),
1278 "queue and/or active list are not empty\n");
1279 return ERR_PTR(-EBUSY
);
1282 was_cyclic
= test_and_set_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
);
1283 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1285 dev_dbg(chan2dev(&dwc
->chan
),
1286 "channel already prepared for cyclic DMA\n");
1287 return ERR_PTR(-EBUSY
);
1290 retval
= ERR_PTR(-EINVAL
);
1292 if (unlikely(!is_slave_direction(direction
)))
1295 dwc
->direction
= direction
;
1297 if (direction
== DMA_MEM_TO_DEV
)
1298 reg_width
= __ffs(sconfig
->dst_addr_width
);
1300 reg_width
= __ffs(sconfig
->src_addr_width
);
1302 periods
= buf_len
/ period_len
;
1304 /* Check for too big/unaligned periods and unaligned DMA buffer. */
1305 if (period_len
> (dwc
->block_size
<< reg_width
))
1307 if (unlikely(period_len
& ((1 << reg_width
) - 1)))
1309 if (unlikely(buf_addr
& ((1 << reg_width
) - 1)))
1312 retval
= ERR_PTR(-ENOMEM
);
1314 cdesc
= kzalloc(sizeof(struct dw_cyclic_desc
), GFP_KERNEL
);
1318 cdesc
->desc
= kzalloc(sizeof(struct dw_desc
*) * periods
, GFP_KERNEL
);
1322 for (i
= 0; i
< periods
; i
++) {
1323 desc
= dwc_desc_get(dwc
);
1325 goto out_err_desc_get
;
1327 switch (direction
) {
1328 case DMA_MEM_TO_DEV
:
1329 lli_write(desc
, dar
, sconfig
->dst_addr
);
1330 lli_write(desc
, sar
, buf_addr
+ period_len
* i
);
1331 lli_write(desc
, ctllo
, (DWC_DEFAULT_CTLLO(chan
)
1332 | DWC_CTLL_DST_WIDTH(reg_width
)
1333 | DWC_CTLL_SRC_WIDTH(reg_width
)
1336 | DWC_CTLL_INT_EN
));
1338 lli_set(desc
, ctllo
, sconfig
->device_fc
?
1339 DWC_CTLL_FC(DW_DMA_FC_P_M2P
) :
1340 DWC_CTLL_FC(DW_DMA_FC_D_M2P
));
1343 case DMA_DEV_TO_MEM
:
1344 lli_write(desc
, dar
, buf_addr
+ period_len
* i
);
1345 lli_write(desc
, sar
, sconfig
->src_addr
);
1346 lli_write(desc
, ctllo
, (DWC_DEFAULT_CTLLO(chan
)
1347 | DWC_CTLL_SRC_WIDTH(reg_width
)
1348 | DWC_CTLL_DST_WIDTH(reg_width
)
1351 | DWC_CTLL_INT_EN
));
1353 lli_set(desc
, ctllo
, sconfig
->device_fc
?
1354 DWC_CTLL_FC(DW_DMA_FC_P_P2M
) :
1355 DWC_CTLL_FC(DW_DMA_FC_D_P2M
));
1362 lli_write(desc
, ctlhi
, period_len
>> reg_width
);
1363 cdesc
->desc
[i
] = desc
;
1366 lli_write(last
, llp
, desc
->txd
.phys
| lms
);
1371 /* Let's make a cyclic list */
1372 lli_write(last
, llp
, cdesc
->desc
[0]->txd
.phys
| lms
);
1374 dev_dbg(chan2dev(&dwc
->chan
),
1375 "cyclic prepared buf %pad len %zu period %zu periods %d\n",
1376 &buf_addr
, buf_len
, period_len
, periods
);
1378 cdesc
->periods
= periods
;
1385 dwc_desc_put(dwc
, cdesc
->desc
[i
]);
1389 clear_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
);
1390 return (struct dw_cyclic_desc
*)retval
;
1392 EXPORT_SYMBOL(dw_dma_cyclic_prep
);
1395 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1396 * @chan: the DMA channel to free
1398 void dw_dma_cyclic_free(struct dma_chan
*chan
)
1400 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1401 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
1402 struct dw_cyclic_desc
*cdesc
= dwc
->cdesc
;
1404 unsigned long flags
;
1406 dev_dbg(chan2dev(&dwc
->chan
), "%s\n", __func__
);
1411 spin_lock_irqsave(&dwc
->lock
, flags
);
1413 dwc_chan_disable(dw
, dwc
);
1415 dma_writel(dw
, CLEAR
.BLOCK
, dwc
->mask
);
1416 dma_writel(dw
, CLEAR
.ERROR
, dwc
->mask
);
1417 dma_writel(dw
, CLEAR
.XFER
, dwc
->mask
);
1419 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1421 for (i
= 0; i
< cdesc
->periods
; i
++)
1422 dwc_desc_put(dwc
, cdesc
->desc
[i
]);
1429 clear_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
);
1431 EXPORT_SYMBOL(dw_dma_cyclic_free
);
1433 /*----------------------------------------------------------------------*/
1435 int dw_dma_probe(struct dw_dma_chip
*chip
)
1437 struct dw_dma_platform_data
*pdata
;
1439 bool autocfg
= false;
1440 unsigned int dw_params
;
1444 dw
= devm_kzalloc(chip
->dev
, sizeof(*dw
), GFP_KERNEL
);
1448 dw
->pdata
= devm_kzalloc(chip
->dev
, sizeof(*dw
->pdata
), GFP_KERNEL
);
1452 dw
->regs
= chip
->regs
;
1455 pm_runtime_get_sync(chip
->dev
);
1458 dw_params
= dma_readl(dw
, DW_PARAMS
);
1459 dev_dbg(chip
->dev
, "DW_PARAMS: 0x%08x\n", dw_params
);
1461 autocfg
= dw_params
>> DW_PARAMS_EN
& 1;
1467 /* Reassign the platform data pointer */
1470 /* Get hardware configuration parameters */
1471 pdata
->nr_channels
= (dw_params
>> DW_PARAMS_NR_CHAN
& 7) + 1;
1472 pdata
->nr_masters
= (dw_params
>> DW_PARAMS_NR_MASTER
& 3) + 1;
1473 for (i
= 0; i
< pdata
->nr_masters
; i
++) {
1474 pdata
->data_width
[i
] =
1475 4 << (dw_params
>> DW_PARAMS_DATA_WIDTH(i
) & 3);
1477 pdata
->block_size
= dma_readl(dw
, MAX_BLK_SIZE
);
1479 /* Fill platform data with the default values */
1480 pdata
->is_private
= true;
1481 pdata
->is_memcpy
= true;
1482 pdata
->chan_allocation_order
= CHAN_ALLOCATION_ASCENDING
;
1483 pdata
->chan_priority
= CHAN_PRIORITY_ASCENDING
;
1484 } else if (chip
->pdata
->nr_channels
> DW_DMA_MAX_NR_CHANNELS
) {
1488 memcpy(dw
->pdata
, chip
->pdata
, sizeof(*dw
->pdata
));
1490 /* Reassign the platform data pointer */
1494 dw
->chan
= devm_kcalloc(chip
->dev
, pdata
->nr_channels
, sizeof(*dw
->chan
),
1501 /* Calculate all channel mask before DMA setup */
1502 dw
->all_chan_mask
= (1 << pdata
->nr_channels
) - 1;
1504 /* Force dma off, just in case */
1507 /* Create a pool of consistent memory blocks for hardware descriptors */
1508 dw
->desc_pool
= dmam_pool_create("dw_dmac_desc_pool", chip
->dev
,
1509 sizeof(struct dw_desc
), 4, 0);
1510 if (!dw
->desc_pool
) {
1511 dev_err(chip
->dev
, "No memory for descriptors dma pool\n");
1516 tasklet_init(&dw
->tasklet
, dw_dma_tasklet
, (unsigned long)dw
);
1518 err
= request_irq(chip
->irq
, dw_dma_interrupt
, IRQF_SHARED
,
1523 INIT_LIST_HEAD(&dw
->dma
.channels
);
1524 for (i
= 0; i
< pdata
->nr_channels
; i
++) {
1525 struct dw_dma_chan
*dwc
= &dw
->chan
[i
];
1527 dwc
->chan
.device
= &dw
->dma
;
1528 dma_cookie_init(&dwc
->chan
);
1529 if (pdata
->chan_allocation_order
== CHAN_ALLOCATION_ASCENDING
)
1530 list_add_tail(&dwc
->chan
.device_node
,
1533 list_add(&dwc
->chan
.device_node
, &dw
->dma
.channels
);
1535 /* 7 is highest priority & 0 is lowest. */
1536 if (pdata
->chan_priority
== CHAN_PRIORITY_ASCENDING
)
1537 dwc
->priority
= pdata
->nr_channels
- i
- 1;
1541 dwc
->ch_regs
= &__dw_regs(dw
)->CHAN
[i
];
1542 spin_lock_init(&dwc
->lock
);
1545 INIT_LIST_HEAD(&dwc
->active_list
);
1546 INIT_LIST_HEAD(&dwc
->queue
);
1548 channel_clear_bit(dw
, CH_EN
, dwc
->mask
);
1550 dwc
->direction
= DMA_TRANS_NONE
;
1552 /* Hardware configuration */
1554 unsigned int r
= DW_DMA_MAX_NR_CHANNELS
- i
- 1;
1555 void __iomem
*addr
= &__dw_regs(dw
)->DWC_PARAMS
[r
];
1556 unsigned int dwc_params
= dma_readl_native(addr
);
1558 dev_dbg(chip
->dev
, "DWC_PARAMS[%d]: 0x%08x\n", i
,
1562 * Decode maximum block size for given channel. The
1563 * stored 4 bit value represents blocks from 0x00 for 3
1564 * up to 0x0a for 4095.
1567 (4 << ((pdata
->block_size
>> 4 * i
) & 0xf)) - 1;
1569 (dwc_params
>> DWC_PARAMS_MBLK_EN
& 0x1) == 0;
1571 dwc
->block_size
= pdata
->block_size
;
1572 dwc
->nollp
= pdata
->is_nollp
;
1576 /* Clear all interrupts on all channels. */
1577 dma_writel(dw
, CLEAR
.XFER
, dw
->all_chan_mask
);
1578 dma_writel(dw
, CLEAR
.BLOCK
, dw
->all_chan_mask
);
1579 dma_writel(dw
, CLEAR
.SRC_TRAN
, dw
->all_chan_mask
);
1580 dma_writel(dw
, CLEAR
.DST_TRAN
, dw
->all_chan_mask
);
1581 dma_writel(dw
, CLEAR
.ERROR
, dw
->all_chan_mask
);
1583 /* Set capabilities */
1584 dma_cap_set(DMA_SLAVE
, dw
->dma
.cap_mask
);
1585 if (pdata
->is_private
)
1586 dma_cap_set(DMA_PRIVATE
, dw
->dma
.cap_mask
);
1587 if (pdata
->is_memcpy
)
1588 dma_cap_set(DMA_MEMCPY
, dw
->dma
.cap_mask
);
1590 dw
->dma
.dev
= chip
->dev
;
1591 dw
->dma
.device_alloc_chan_resources
= dwc_alloc_chan_resources
;
1592 dw
->dma
.device_free_chan_resources
= dwc_free_chan_resources
;
1594 dw
->dma
.device_prep_dma_memcpy
= dwc_prep_dma_memcpy
;
1595 dw
->dma
.device_prep_slave_sg
= dwc_prep_slave_sg
;
1597 dw
->dma
.device_config
= dwc_config
;
1598 dw
->dma
.device_pause
= dwc_pause
;
1599 dw
->dma
.device_resume
= dwc_resume
;
1600 dw
->dma
.device_terminate_all
= dwc_terminate_all
;
1602 dw
->dma
.device_tx_status
= dwc_tx_status
;
1603 dw
->dma
.device_issue_pending
= dwc_issue_pending
;
1605 /* DMA capabilities */
1606 dw
->dma
.src_addr_widths
= DW_DMA_BUSWIDTHS
;
1607 dw
->dma
.dst_addr_widths
= DW_DMA_BUSWIDTHS
;
1608 dw
->dma
.directions
= BIT(DMA_DEV_TO_MEM
) | BIT(DMA_MEM_TO_DEV
) |
1609 BIT(DMA_MEM_TO_MEM
);
1610 dw
->dma
.residue_granularity
= DMA_RESIDUE_GRANULARITY_BURST
;
1612 err
= dma_async_device_register(&dw
->dma
);
1614 goto err_dma_register
;
1616 dev_info(chip
->dev
, "DesignWare DMA Controller, %d channels\n",
1617 pdata
->nr_channels
);
1619 pm_runtime_put_sync_suspend(chip
->dev
);
1624 free_irq(chip
->irq
, dw
);
1626 pm_runtime_put_sync_suspend(chip
->dev
);
1629 EXPORT_SYMBOL_GPL(dw_dma_probe
);
1631 int dw_dma_remove(struct dw_dma_chip
*chip
)
1633 struct dw_dma
*dw
= chip
->dw
;
1634 struct dw_dma_chan
*dwc
, *_dwc
;
1636 pm_runtime_get_sync(chip
->dev
);
1639 dma_async_device_unregister(&dw
->dma
);
1641 free_irq(chip
->irq
, dw
);
1642 tasklet_kill(&dw
->tasklet
);
1644 list_for_each_entry_safe(dwc
, _dwc
, &dw
->dma
.channels
,
1646 list_del(&dwc
->chan
.device_node
);
1647 channel_clear_bit(dw
, CH_EN
, dwc
->mask
);
1650 pm_runtime_put_sync_suspend(chip
->dev
);
1653 EXPORT_SYMBOL_GPL(dw_dma_remove
);
1655 int dw_dma_disable(struct dw_dma_chip
*chip
)
1657 struct dw_dma
*dw
= chip
->dw
;
1662 EXPORT_SYMBOL_GPL(dw_dma_disable
);
1664 int dw_dma_enable(struct dw_dma_chip
*chip
)
1666 struct dw_dma
*dw
= chip
->dw
;
1671 EXPORT_SYMBOL_GPL(dw_dma_enable
);
1673 MODULE_LICENSE("GPL v2");
1674 MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
1675 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1676 MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");