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->p_master : _dwc->m_master; \
50 u8 _sms = (_dwc->direction == DMA_DEV_TO_MEM) ? \
51 _dwc->p_master : _dwc->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
);
147 if (test_bit(DW_DMA_IS_INITIALIZED
, &dwc
->flags
))
150 cfghi
|= DWC_CFGH_DST_PER(dwc
->dst_id
);
151 cfghi
|= DWC_CFGH_SRC_PER(dwc
->src_id
);
153 channel_writel(dwc
, CFG_LO
, cfglo
);
154 channel_writel(dwc
, CFG_HI
, cfghi
);
156 /* Enable interrupts */
157 channel_set_bit(dw
, MASK
.XFER
, dwc
->mask
);
158 channel_set_bit(dw
, MASK
.ERROR
, dwc
->mask
);
160 set_bit(DW_DMA_IS_INITIALIZED
, &dwc
->flags
);
163 /*----------------------------------------------------------------------*/
165 static inline void dwc_dump_chan_regs(struct dw_dma_chan
*dwc
)
167 dev_err(chan2dev(&dwc
->chan
),
168 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
169 channel_readl(dwc
, SAR
),
170 channel_readl(dwc
, DAR
),
171 channel_readl(dwc
, LLP
),
172 channel_readl(dwc
, CTL_HI
),
173 channel_readl(dwc
, CTL_LO
));
176 static inline void dwc_chan_disable(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
)
178 channel_clear_bit(dw
, CH_EN
, dwc
->mask
);
179 while (dma_readl(dw
, CH_EN
) & dwc
->mask
)
183 /*----------------------------------------------------------------------*/
185 /* Perform single block transfer */
186 static inline void dwc_do_single_block(struct dw_dma_chan
*dwc
,
187 struct dw_desc
*desc
)
189 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
193 * Software emulation of LLP mode relies on interrupts to continue
194 * multi block transfer.
196 ctllo
= lli_read(desc
, ctllo
) | DWC_CTLL_INT_EN
;
198 channel_writel(dwc
, SAR
, lli_read(desc
, sar
));
199 channel_writel(dwc
, DAR
, lli_read(desc
, dar
));
200 channel_writel(dwc
, CTL_LO
, ctllo
);
201 channel_writel(dwc
, CTL_HI
, lli_read(desc
, ctlhi
));
202 channel_set_bit(dw
, CH_EN
, dwc
->mask
);
204 /* Move pointer to next descriptor */
205 dwc
->tx_node_active
= dwc
->tx_node_active
->next
;
208 /* Called with dwc->lock held and bh disabled */
209 static void dwc_dostart(struct dw_dma_chan
*dwc
, struct dw_desc
*first
)
211 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
212 u8 lms
= DWC_LLP_LMS(dwc
->m_master
);
213 unsigned long was_soft_llp
;
215 /* ASSERT: channel is idle */
216 if (dma_readl(dw
, CH_EN
) & dwc
->mask
) {
217 dev_err(chan2dev(&dwc
->chan
),
218 "%s: BUG: Attempted to start non-idle channel\n",
220 dwc_dump_chan_regs(dwc
);
222 /* The tasklet will hopefully advance the queue... */
227 was_soft_llp
= test_and_set_bit(DW_DMA_IS_SOFT_LLP
,
230 dev_err(chan2dev(&dwc
->chan
),
231 "BUG: Attempted to start new LLP transfer inside ongoing one\n");
237 first
->residue
= first
->total_len
;
238 dwc
->tx_node_active
= &first
->tx_list
;
240 /* Submit first block */
241 dwc_do_single_block(dwc
, first
);
248 channel_writel(dwc
, LLP
, first
->txd
.phys
| lms
);
249 channel_writel(dwc
, CTL_LO
, DWC_CTLL_LLP_D_EN
| DWC_CTLL_LLP_S_EN
);
250 channel_writel(dwc
, CTL_HI
, 0);
251 channel_set_bit(dw
, CH_EN
, dwc
->mask
);
254 static void dwc_dostart_first_queued(struct dw_dma_chan
*dwc
)
256 struct dw_desc
*desc
;
258 if (list_empty(&dwc
->queue
))
261 list_move(dwc
->queue
.next
, &dwc
->active_list
);
262 desc
= dwc_first_active(dwc
);
263 dev_vdbg(chan2dev(&dwc
->chan
), "%s: started %u\n", __func__
, desc
->txd
.cookie
);
264 dwc_dostart(dwc
, desc
);
267 /*----------------------------------------------------------------------*/
270 dwc_descriptor_complete(struct dw_dma_chan
*dwc
, struct dw_desc
*desc
,
271 bool callback_required
)
273 struct dma_async_tx_descriptor
*txd
= &desc
->txd
;
274 struct dw_desc
*child
;
276 struct dmaengine_desc_callback cb
;
278 dev_vdbg(chan2dev(&dwc
->chan
), "descriptor %u complete\n", txd
->cookie
);
280 spin_lock_irqsave(&dwc
->lock
, flags
);
281 dma_cookie_complete(txd
);
282 if (callback_required
)
283 dmaengine_desc_get_callback(txd
, &cb
);
285 memset(&cb
, 0, sizeof(cb
));
288 list_for_each_entry(child
, &desc
->tx_list
, desc_node
)
289 async_tx_ack(&child
->txd
);
290 async_tx_ack(&desc
->txd
);
291 dwc_desc_put(dwc
, desc
);
292 spin_unlock_irqrestore(&dwc
->lock
, flags
);
294 dmaengine_desc_callback_invoke(&cb
, NULL
);
297 static void dwc_complete_all(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
)
299 struct dw_desc
*desc
, *_desc
;
303 spin_lock_irqsave(&dwc
->lock
, flags
);
304 if (dma_readl(dw
, CH_EN
) & dwc
->mask
) {
305 dev_err(chan2dev(&dwc
->chan
),
306 "BUG: XFER bit set, but channel not idle!\n");
308 /* Try to continue after resetting the channel... */
309 dwc_chan_disable(dw
, dwc
);
313 * Submit queued descriptors ASAP, i.e. before we go through
314 * the completed ones.
316 list_splice_init(&dwc
->active_list
, &list
);
317 dwc_dostart_first_queued(dwc
);
319 spin_unlock_irqrestore(&dwc
->lock
, flags
);
321 list_for_each_entry_safe(desc
, _desc
, &list
, desc_node
)
322 dwc_descriptor_complete(dwc
, desc
, true);
325 /* Returns how many bytes were already received from source */
326 static inline u32
dwc_get_sent(struct dw_dma_chan
*dwc
)
328 u32 ctlhi
= channel_readl(dwc
, CTL_HI
);
329 u32 ctllo
= channel_readl(dwc
, CTL_LO
);
331 return (ctlhi
& DWC_CTLH_BLOCK_TS_MASK
) * (1 << (ctllo
>> 4 & 7));
334 static void dwc_scan_descriptors(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
)
337 struct dw_desc
*desc
, *_desc
;
338 struct dw_desc
*child
;
342 spin_lock_irqsave(&dwc
->lock
, flags
);
343 llp
= channel_readl(dwc
, LLP
);
344 status_xfer
= dma_readl(dw
, RAW
.XFER
);
346 if (status_xfer
& dwc
->mask
) {
347 /* Everything we've submitted is done */
348 dma_writel(dw
, CLEAR
.XFER
, dwc
->mask
);
350 if (test_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
)) {
351 struct list_head
*head
, *active
= dwc
->tx_node_active
;
354 * We are inside first active descriptor.
355 * Otherwise something is really wrong.
357 desc
= dwc_first_active(dwc
);
359 head
= &desc
->tx_list
;
360 if (active
!= head
) {
361 /* Update residue to reflect last sent descriptor */
362 if (active
== head
->next
)
363 desc
->residue
-= desc
->len
;
365 desc
->residue
-= to_dw_desc(active
->prev
)->len
;
367 child
= to_dw_desc(active
);
369 /* Submit next block */
370 dwc_do_single_block(dwc
, child
);
372 spin_unlock_irqrestore(&dwc
->lock
, flags
);
376 /* We are done here */
377 clear_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
);
380 spin_unlock_irqrestore(&dwc
->lock
, flags
);
382 dwc_complete_all(dw
, dwc
);
386 if (list_empty(&dwc
->active_list
)) {
387 spin_unlock_irqrestore(&dwc
->lock
, flags
);
391 if (test_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
)) {
392 dev_vdbg(chan2dev(&dwc
->chan
), "%s: soft LLP mode\n", __func__
);
393 spin_unlock_irqrestore(&dwc
->lock
, flags
);
397 dev_vdbg(chan2dev(&dwc
->chan
), "%s: llp=%pad\n", __func__
, &llp
);
399 list_for_each_entry_safe(desc
, _desc
, &dwc
->active_list
, desc_node
) {
400 /* Initial residue value */
401 desc
->residue
= desc
->total_len
;
403 /* Check first descriptors addr */
404 if (desc
->txd
.phys
== DWC_LLP_LOC(llp
)) {
405 spin_unlock_irqrestore(&dwc
->lock
, flags
);
409 /* Check first descriptors llp */
410 if (lli_read(desc
, llp
) == llp
) {
411 /* This one is currently in progress */
412 desc
->residue
-= dwc_get_sent(dwc
);
413 spin_unlock_irqrestore(&dwc
->lock
, flags
);
417 desc
->residue
-= desc
->len
;
418 list_for_each_entry(child
, &desc
->tx_list
, desc_node
) {
419 if (lli_read(child
, llp
) == llp
) {
420 /* Currently in progress */
421 desc
->residue
-= dwc_get_sent(dwc
);
422 spin_unlock_irqrestore(&dwc
->lock
, flags
);
425 desc
->residue
-= child
->len
;
429 * No descriptors so far seem to be in progress, i.e.
430 * this one must be done.
432 spin_unlock_irqrestore(&dwc
->lock
, flags
);
433 dwc_descriptor_complete(dwc
, desc
, true);
434 spin_lock_irqsave(&dwc
->lock
, flags
);
437 dev_err(chan2dev(&dwc
->chan
),
438 "BUG: All descriptors done, but channel not idle!\n");
440 /* Try to continue after resetting the channel... */
441 dwc_chan_disable(dw
, dwc
);
443 dwc_dostart_first_queued(dwc
);
444 spin_unlock_irqrestore(&dwc
->lock
, flags
);
447 static inline void dwc_dump_lli(struct dw_dma_chan
*dwc
, struct dw_desc
*desc
)
449 dev_crit(chan2dev(&dwc
->chan
), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
453 lli_read(desc
, ctlhi
),
454 lli_read(desc
, ctllo
));
457 static void dwc_handle_error(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
)
459 struct dw_desc
*bad_desc
;
460 struct dw_desc
*child
;
463 dwc_scan_descriptors(dw
, dwc
);
465 spin_lock_irqsave(&dwc
->lock
, flags
);
468 * The descriptor currently at the head of the active list is
469 * borked. Since we don't have any way to report errors, we'll
470 * just have to scream loudly and try to carry on.
472 bad_desc
= dwc_first_active(dwc
);
473 list_del_init(&bad_desc
->desc_node
);
474 list_move(dwc
->queue
.next
, dwc
->active_list
.prev
);
476 /* Clear the error flag and try to restart the controller */
477 dma_writel(dw
, CLEAR
.ERROR
, dwc
->mask
);
478 if (!list_empty(&dwc
->active_list
))
479 dwc_dostart(dwc
, dwc_first_active(dwc
));
482 * WARN may seem harsh, but since this only happens
483 * when someone submits a bad physical address in a
484 * descriptor, we should consider ourselves lucky that the
485 * controller flagged an error instead of scribbling over
486 * random memory locations.
488 dev_WARN(chan2dev(&dwc
->chan
), "Bad descriptor submitted for DMA!\n"
489 " cookie: %d\n", bad_desc
->txd
.cookie
);
490 dwc_dump_lli(dwc
, bad_desc
);
491 list_for_each_entry(child
, &bad_desc
->tx_list
, desc_node
)
492 dwc_dump_lli(dwc
, child
);
494 spin_unlock_irqrestore(&dwc
->lock
, flags
);
496 /* Pretend the descriptor completed successfully */
497 dwc_descriptor_complete(dwc
, bad_desc
, true);
500 /* --------------------- Cyclic DMA API extensions -------------------- */
502 dma_addr_t
dw_dma_get_src_addr(struct dma_chan
*chan
)
504 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
505 return channel_readl(dwc
, SAR
);
507 EXPORT_SYMBOL(dw_dma_get_src_addr
);
509 dma_addr_t
dw_dma_get_dst_addr(struct dma_chan
*chan
)
511 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
512 return channel_readl(dwc
, DAR
);
514 EXPORT_SYMBOL(dw_dma_get_dst_addr
);
516 /* Called with dwc->lock held and all DMAC interrupts disabled */
517 static void dwc_handle_cyclic(struct dw_dma
*dw
, struct dw_dma_chan
*dwc
,
518 u32 status_block
, u32 status_err
, u32 status_xfer
)
522 if (status_block
& dwc
->mask
) {
523 void (*callback
)(void *param
);
524 void *callback_param
;
526 dev_vdbg(chan2dev(&dwc
->chan
), "new cyclic period llp 0x%08x\n",
527 channel_readl(dwc
, LLP
));
528 dma_writel(dw
, CLEAR
.BLOCK
, dwc
->mask
);
530 callback
= dwc
->cdesc
->period_callback
;
531 callback_param
= dwc
->cdesc
->period_callback_param
;
534 callback(callback_param
);
538 * Error and transfer complete are highly unlikely, and will most
539 * likely be due to a configuration error by the user.
541 if (unlikely(status_err
& dwc
->mask
) ||
542 unlikely(status_xfer
& dwc
->mask
)) {
545 dev_err(chan2dev(&dwc
->chan
),
546 "cyclic DMA unexpected %s interrupt, stopping DMA transfer\n",
547 status_xfer
? "xfer" : "error");
549 spin_lock_irqsave(&dwc
->lock
, flags
);
551 dwc_dump_chan_regs(dwc
);
553 dwc_chan_disable(dw
, dwc
);
555 /* Make sure DMA does not restart by loading a new list */
556 channel_writel(dwc
, LLP
, 0);
557 channel_writel(dwc
, CTL_LO
, 0);
558 channel_writel(dwc
, CTL_HI
, 0);
560 dma_writel(dw
, CLEAR
.BLOCK
, dwc
->mask
);
561 dma_writel(dw
, CLEAR
.ERROR
, dwc
->mask
);
562 dma_writel(dw
, CLEAR
.XFER
, dwc
->mask
);
564 for (i
= 0; i
< dwc
->cdesc
->periods
; i
++)
565 dwc_dump_lli(dwc
, dwc
->cdesc
->desc
[i
]);
567 spin_unlock_irqrestore(&dwc
->lock
, flags
);
570 /* Re-enable interrupts */
571 channel_set_bit(dw
, MASK
.BLOCK
, dwc
->mask
);
574 /* ------------------------------------------------------------------------- */
576 static void dw_dma_tasklet(unsigned long data
)
578 struct dw_dma
*dw
= (struct dw_dma
*)data
;
579 struct dw_dma_chan
*dwc
;
585 status_block
= dma_readl(dw
, RAW
.BLOCK
);
586 status_xfer
= dma_readl(dw
, RAW
.XFER
);
587 status_err
= dma_readl(dw
, RAW
.ERROR
);
589 dev_vdbg(dw
->dma
.dev
, "%s: status_err=%x\n", __func__
, status_err
);
591 for (i
= 0; i
< dw
->dma
.chancnt
; i
++) {
593 if (test_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
))
594 dwc_handle_cyclic(dw
, dwc
, status_block
, status_err
,
596 else if (status_err
& (1 << i
))
597 dwc_handle_error(dw
, dwc
);
598 else if (status_xfer
& (1 << i
))
599 dwc_scan_descriptors(dw
, dwc
);
602 /* Re-enable interrupts */
603 channel_set_bit(dw
, MASK
.XFER
, dw
->all_chan_mask
);
604 channel_set_bit(dw
, MASK
.ERROR
, dw
->all_chan_mask
);
607 static irqreturn_t
dw_dma_interrupt(int irq
, void *dev_id
)
609 struct dw_dma
*dw
= dev_id
;
612 /* Check if we have any interrupt from the DMAC which is not in use */
616 status
= dma_readl(dw
, STATUS_INT
);
617 dev_vdbg(dw
->dma
.dev
, "%s: status=0x%x\n", __func__
, status
);
619 /* Check if we have any interrupt from the DMAC */
624 * Just disable the interrupts. We'll turn them back on in the
627 channel_clear_bit(dw
, MASK
.XFER
, dw
->all_chan_mask
);
628 channel_clear_bit(dw
, MASK
.BLOCK
, dw
->all_chan_mask
);
629 channel_clear_bit(dw
, MASK
.ERROR
, dw
->all_chan_mask
);
631 status
= dma_readl(dw
, STATUS_INT
);
634 "BUG: Unexpected interrupts pending: 0x%x\n",
638 channel_clear_bit(dw
, MASK
.XFER
, (1 << 8) - 1);
639 channel_clear_bit(dw
, MASK
.BLOCK
, (1 << 8) - 1);
640 channel_clear_bit(dw
, MASK
.SRC_TRAN
, (1 << 8) - 1);
641 channel_clear_bit(dw
, MASK
.DST_TRAN
, (1 << 8) - 1);
642 channel_clear_bit(dw
, MASK
.ERROR
, (1 << 8) - 1);
645 tasklet_schedule(&dw
->tasklet
);
650 /*----------------------------------------------------------------------*/
652 static struct dma_async_tx_descriptor
*
653 dwc_prep_dma_memcpy(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t src
,
654 size_t len
, unsigned long flags
)
656 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
657 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
658 struct dw_desc
*desc
;
659 struct dw_desc
*first
;
660 struct dw_desc
*prev
;
663 u8 m_master
= dwc
->m_master
;
664 unsigned int src_width
;
665 unsigned int dst_width
;
666 unsigned int data_width
= dw
->pdata
->data_width
[m_master
];
668 u8 lms
= DWC_LLP_LMS(m_master
);
670 dev_vdbg(chan2dev(chan
),
671 "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__
,
672 &dest
, &src
, len
, flags
);
674 if (unlikely(!len
)) {
675 dev_dbg(chan2dev(chan
), "%s: length is zero!\n", __func__
);
679 dwc
->direction
= DMA_MEM_TO_MEM
;
681 src_width
= dst_width
= __ffs(data_width
| src
| dest
| len
);
683 ctllo
= DWC_DEFAULT_CTLLO(chan
)
684 | DWC_CTLL_DST_WIDTH(dst_width
)
685 | DWC_CTLL_SRC_WIDTH(src_width
)
691 for (offset
= 0; offset
< len
; offset
+= xfer_count
<< src_width
) {
692 xfer_count
= min_t(size_t, (len
- offset
) >> src_width
,
695 desc
= dwc_desc_get(dwc
);
699 lli_write(desc
, sar
, src
+ offset
);
700 lli_write(desc
, dar
, dest
+ offset
);
701 lli_write(desc
, ctllo
, ctllo
);
702 lli_write(desc
, ctlhi
, xfer_count
);
703 desc
->len
= xfer_count
<< src_width
;
708 lli_write(prev
, llp
, desc
->txd
.phys
| lms
);
709 list_add_tail(&desc
->desc_node
, &first
->tx_list
);
714 if (flags
& DMA_PREP_INTERRUPT
)
715 /* Trigger interrupt after last block */
716 lli_set(prev
, ctllo
, DWC_CTLL_INT_EN
);
719 lli_clear(prev
, ctllo
, DWC_CTLL_LLP_D_EN
| DWC_CTLL_LLP_S_EN
);
720 first
->txd
.flags
= flags
;
721 first
->total_len
= len
;
726 dwc_desc_put(dwc
, first
);
730 static struct dma_async_tx_descriptor
*
731 dwc_prep_slave_sg(struct dma_chan
*chan
, struct scatterlist
*sgl
,
732 unsigned int sg_len
, enum dma_transfer_direction direction
,
733 unsigned long flags
, void *context
)
735 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
736 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
737 struct dma_slave_config
*sconfig
= &dwc
->dma_sconfig
;
738 struct dw_desc
*prev
;
739 struct dw_desc
*first
;
741 u8 m_master
= dwc
->m_master
;
742 u8 lms
= DWC_LLP_LMS(m_master
);
744 unsigned int reg_width
;
745 unsigned int mem_width
;
746 unsigned int data_width
= dw
->pdata
->data_width
[m_master
];
748 struct scatterlist
*sg
;
749 size_t total_len
= 0;
751 dev_vdbg(chan2dev(chan
), "%s\n", __func__
);
753 if (unlikely(!is_slave_direction(direction
) || !sg_len
))
756 dwc
->direction
= direction
;
762 reg_width
= __ffs(sconfig
->dst_addr_width
);
763 reg
= sconfig
->dst_addr
;
764 ctllo
= (DWC_DEFAULT_CTLLO(chan
)
765 | DWC_CTLL_DST_WIDTH(reg_width
)
769 ctllo
|= sconfig
->device_fc
? DWC_CTLL_FC(DW_DMA_FC_P_M2P
) :
770 DWC_CTLL_FC(DW_DMA_FC_D_M2P
);
772 for_each_sg(sgl
, sg
, sg_len
, i
) {
773 struct dw_desc
*desc
;
776 mem
= sg_dma_address(sg
);
777 len
= sg_dma_len(sg
);
779 mem_width
= __ffs(data_width
| mem
| len
);
781 slave_sg_todev_fill_desc
:
782 desc
= dwc_desc_get(dwc
);
786 lli_write(desc
, sar
, mem
);
787 lli_write(desc
, dar
, reg
);
788 lli_write(desc
, ctllo
, ctllo
| DWC_CTLL_SRC_WIDTH(mem_width
));
789 if ((len
>> mem_width
) > dwc
->block_size
) {
790 dlen
= dwc
->block_size
<< mem_width
;
798 lli_write(desc
, ctlhi
, dlen
>> mem_width
);
804 lli_write(prev
, llp
, desc
->txd
.phys
| lms
);
805 list_add_tail(&desc
->desc_node
, &first
->tx_list
);
811 goto slave_sg_todev_fill_desc
;
815 reg_width
= __ffs(sconfig
->src_addr_width
);
816 reg
= sconfig
->src_addr
;
817 ctllo
= (DWC_DEFAULT_CTLLO(chan
)
818 | DWC_CTLL_SRC_WIDTH(reg_width
)
822 ctllo
|= sconfig
->device_fc
? DWC_CTLL_FC(DW_DMA_FC_P_P2M
) :
823 DWC_CTLL_FC(DW_DMA_FC_D_P2M
);
825 for_each_sg(sgl
, sg
, sg_len
, i
) {
826 struct dw_desc
*desc
;
829 mem
= sg_dma_address(sg
);
830 len
= sg_dma_len(sg
);
832 mem_width
= __ffs(data_width
| mem
| len
);
834 slave_sg_fromdev_fill_desc
:
835 desc
= dwc_desc_get(dwc
);
839 lli_write(desc
, sar
, reg
);
840 lli_write(desc
, dar
, mem
);
841 lli_write(desc
, ctllo
, ctllo
| DWC_CTLL_DST_WIDTH(mem_width
));
842 if ((len
>> reg_width
) > dwc
->block_size
) {
843 dlen
= dwc
->block_size
<< reg_width
;
850 lli_write(desc
, ctlhi
, dlen
>> reg_width
);
856 lli_write(prev
, llp
, desc
->txd
.phys
| lms
);
857 list_add_tail(&desc
->desc_node
, &first
->tx_list
);
863 goto slave_sg_fromdev_fill_desc
;
870 if (flags
& DMA_PREP_INTERRUPT
)
871 /* Trigger interrupt after last block */
872 lli_set(prev
, ctllo
, DWC_CTLL_INT_EN
);
875 lli_clear(prev
, ctllo
, DWC_CTLL_LLP_D_EN
| DWC_CTLL_LLP_S_EN
);
876 first
->total_len
= total_len
;
881 dev_err(chan2dev(chan
),
882 "not enough descriptors available. Direction %d\n", direction
);
883 dwc_desc_put(dwc
, first
);
887 bool dw_dma_filter(struct dma_chan
*chan
, void *param
)
889 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
890 struct dw_dma_slave
*dws
= param
;
892 if (dws
->dma_dev
!= chan
->device
->dev
)
895 /* We have to copy data since dws can be temporary storage */
897 dwc
->src_id
= dws
->src_id
;
898 dwc
->dst_id
= dws
->dst_id
;
900 dwc
->m_master
= dws
->m_master
;
901 dwc
->p_master
= dws
->p_master
;
905 EXPORT_SYMBOL_GPL(dw_dma_filter
);
908 * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
909 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
911 * NOTE: burst size 2 is not supported by controller.
913 * This can be done by finding least significant bit set: n & (n - 1)
915 static inline void convert_burst(u32
*maxburst
)
918 *maxburst
= fls(*maxburst
) - 2;
923 static int dwc_config(struct dma_chan
*chan
, struct dma_slave_config
*sconfig
)
925 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
927 /* Check if chan will be configured for slave transfers */
928 if (!is_slave_direction(sconfig
->direction
))
931 memcpy(&dwc
->dma_sconfig
, sconfig
, sizeof(*sconfig
));
932 dwc
->direction
= sconfig
->direction
;
934 convert_burst(&dwc
->dma_sconfig
.src_maxburst
);
935 convert_burst(&dwc
->dma_sconfig
.dst_maxburst
);
940 static int dwc_pause(struct dma_chan
*chan
)
942 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
944 unsigned int count
= 20; /* timeout iterations */
947 spin_lock_irqsave(&dwc
->lock
, flags
);
949 cfglo
= channel_readl(dwc
, CFG_LO
);
950 channel_writel(dwc
, CFG_LO
, cfglo
| DWC_CFGL_CH_SUSP
);
951 while (!(channel_readl(dwc
, CFG_LO
) & DWC_CFGL_FIFO_EMPTY
) && count
--)
954 set_bit(DW_DMA_IS_PAUSED
, &dwc
->flags
);
956 spin_unlock_irqrestore(&dwc
->lock
, flags
);
961 static inline void dwc_chan_resume(struct dw_dma_chan
*dwc
)
963 u32 cfglo
= channel_readl(dwc
, CFG_LO
);
965 channel_writel(dwc
, CFG_LO
, cfglo
& ~DWC_CFGL_CH_SUSP
);
967 clear_bit(DW_DMA_IS_PAUSED
, &dwc
->flags
);
970 static int dwc_resume(struct dma_chan
*chan
)
972 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
975 spin_lock_irqsave(&dwc
->lock
, flags
);
977 if (test_bit(DW_DMA_IS_PAUSED
, &dwc
->flags
))
978 dwc_chan_resume(dwc
);
980 spin_unlock_irqrestore(&dwc
->lock
, flags
);
985 static int dwc_terminate_all(struct dma_chan
*chan
)
987 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
988 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
989 struct dw_desc
*desc
, *_desc
;
993 spin_lock_irqsave(&dwc
->lock
, flags
);
995 clear_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
);
997 dwc_chan_disable(dw
, dwc
);
999 dwc_chan_resume(dwc
);
1001 /* active_list entries will end up before queued entries */
1002 list_splice_init(&dwc
->queue
, &list
);
1003 list_splice_init(&dwc
->active_list
, &list
);
1005 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1007 /* Flush all pending and queued descriptors */
1008 list_for_each_entry_safe(desc
, _desc
, &list
, desc_node
)
1009 dwc_descriptor_complete(dwc
, desc
, false);
1014 static struct dw_desc
*dwc_find_desc(struct dw_dma_chan
*dwc
, dma_cookie_t c
)
1016 struct dw_desc
*desc
;
1018 list_for_each_entry(desc
, &dwc
->active_list
, desc_node
)
1019 if (desc
->txd
.cookie
== c
)
1025 static u32
dwc_get_residue(struct dw_dma_chan
*dwc
, dma_cookie_t cookie
)
1027 struct dw_desc
*desc
;
1028 unsigned long flags
;
1031 spin_lock_irqsave(&dwc
->lock
, flags
);
1033 desc
= dwc_find_desc(dwc
, cookie
);
1035 if (desc
== dwc_first_active(dwc
)) {
1036 residue
= desc
->residue
;
1037 if (test_bit(DW_DMA_IS_SOFT_LLP
, &dwc
->flags
) && residue
)
1038 residue
-= dwc_get_sent(dwc
);
1040 residue
= desc
->total_len
;
1046 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1050 static enum dma_status
1051 dwc_tx_status(struct dma_chan
*chan
,
1052 dma_cookie_t cookie
,
1053 struct dma_tx_state
*txstate
)
1055 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1056 enum dma_status ret
;
1058 ret
= dma_cookie_status(chan
, cookie
, txstate
);
1059 if (ret
== DMA_COMPLETE
)
1062 dwc_scan_descriptors(to_dw_dma(chan
->device
), dwc
);
1064 ret
= dma_cookie_status(chan
, cookie
, txstate
);
1065 if (ret
== DMA_COMPLETE
)
1068 dma_set_residue(txstate
, dwc_get_residue(dwc
, cookie
));
1070 if (test_bit(DW_DMA_IS_PAUSED
, &dwc
->flags
) && ret
== DMA_IN_PROGRESS
)
1076 static void dwc_issue_pending(struct dma_chan
*chan
)
1078 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1079 unsigned long flags
;
1081 spin_lock_irqsave(&dwc
->lock
, flags
);
1082 if (list_empty(&dwc
->active_list
))
1083 dwc_dostart_first_queued(dwc
);
1084 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1087 /*----------------------------------------------------------------------*/
1089 static void dw_dma_off(struct dw_dma
*dw
)
1093 dma_writel(dw
, CFG
, 0);
1095 channel_clear_bit(dw
, MASK
.XFER
, dw
->all_chan_mask
);
1096 channel_clear_bit(dw
, MASK
.BLOCK
, dw
->all_chan_mask
);
1097 channel_clear_bit(dw
, MASK
.SRC_TRAN
, dw
->all_chan_mask
);
1098 channel_clear_bit(dw
, MASK
.DST_TRAN
, dw
->all_chan_mask
);
1099 channel_clear_bit(dw
, MASK
.ERROR
, dw
->all_chan_mask
);
1101 while (dma_readl(dw
, CFG
) & DW_CFG_DMA_EN
)
1104 for (i
= 0; i
< dw
->dma
.chancnt
; i
++)
1105 clear_bit(DW_DMA_IS_INITIALIZED
, &dw
->chan
[i
].flags
);
1108 static void dw_dma_on(struct dw_dma
*dw
)
1110 dma_writel(dw
, CFG
, DW_CFG_DMA_EN
);
1113 static int dwc_alloc_chan_resources(struct dma_chan
*chan
)
1115 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1116 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
1118 dev_vdbg(chan2dev(chan
), "%s\n", __func__
);
1120 /* ASSERT: channel is idle */
1121 if (dma_readl(dw
, CH_EN
) & dwc
->mask
) {
1122 dev_dbg(chan2dev(chan
), "DMA channel not idle?\n");
1126 dma_cookie_init(chan
);
1129 * NOTE: some controllers may have additional features that we
1130 * need to initialize here, like "scatter-gather" (which
1131 * doesn't mean what you think it means), and status writeback.
1135 * We need controller-specific data to set up slave transfers.
1137 if (chan
->private && !dw_dma_filter(chan
, chan
->private)) {
1138 dev_warn(chan2dev(chan
), "Wrong controller-specific data\n");
1142 /* Enable controller here if needed */
1145 dw
->in_use
|= dwc
->mask
;
1150 static void dwc_free_chan_resources(struct dma_chan
*chan
)
1152 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1153 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
1154 unsigned long flags
;
1157 dev_dbg(chan2dev(chan
), "%s: descs allocated=%u\n", __func__
,
1158 dwc
->descs_allocated
);
1160 /* ASSERT: channel is idle */
1161 BUG_ON(!list_empty(&dwc
->active_list
));
1162 BUG_ON(!list_empty(&dwc
->queue
));
1163 BUG_ON(dma_readl(to_dw_dma(chan
->device
), CH_EN
) & dwc
->mask
);
1165 spin_lock_irqsave(&dwc
->lock
, flags
);
1167 /* Clear custom channel configuration */
1174 clear_bit(DW_DMA_IS_INITIALIZED
, &dwc
->flags
);
1176 /* Disable interrupts */
1177 channel_clear_bit(dw
, MASK
.XFER
, dwc
->mask
);
1178 channel_clear_bit(dw
, MASK
.BLOCK
, dwc
->mask
);
1179 channel_clear_bit(dw
, MASK
.ERROR
, dwc
->mask
);
1181 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1183 /* Disable controller in case it was a last user */
1184 dw
->in_use
&= ~dwc
->mask
;
1188 dev_vdbg(chan2dev(chan
), "%s: done\n", __func__
);
1191 /* --------------------- Cyclic DMA API extensions -------------------- */
1194 * dw_dma_cyclic_start - start the cyclic DMA transfer
1195 * @chan: the DMA channel to start
1197 * Must be called with soft interrupts disabled. Returns zero on success or
1198 * -errno on failure.
1200 int dw_dma_cyclic_start(struct dma_chan
*chan
)
1202 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1203 struct dw_dma
*dw
= to_dw_dma(chan
->device
);
1204 unsigned long flags
;
1206 if (!test_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
)) {
1207 dev_err(chan2dev(&dwc
->chan
), "missing prep for cyclic DMA\n");
1211 spin_lock_irqsave(&dwc
->lock
, flags
);
1213 /* Enable interrupts to perform cyclic transfer */
1214 channel_set_bit(dw
, MASK
.BLOCK
, dwc
->mask
);
1216 dwc_dostart(dwc
, dwc
->cdesc
->desc
[0]);
1218 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1222 EXPORT_SYMBOL(dw_dma_cyclic_start
);
1225 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1226 * @chan: the DMA channel to stop
1228 * Must be called with soft interrupts disabled.
1230 void dw_dma_cyclic_stop(struct dma_chan
*chan
)
1232 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1233 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
1234 unsigned long flags
;
1236 spin_lock_irqsave(&dwc
->lock
, flags
);
1238 dwc_chan_disable(dw
, dwc
);
1240 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1242 EXPORT_SYMBOL(dw_dma_cyclic_stop
);
1245 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1246 * @chan: the DMA channel to prepare
1247 * @buf_addr: physical DMA address where the buffer starts
1248 * @buf_len: total number of bytes for the entire buffer
1249 * @period_len: number of bytes for each period
1250 * @direction: transfer direction, to or from device
1252 * Must be called before trying to start the transfer. Returns a valid struct
1253 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1255 struct dw_cyclic_desc
*dw_dma_cyclic_prep(struct dma_chan
*chan
,
1256 dma_addr_t buf_addr
, size_t buf_len
, size_t period_len
,
1257 enum dma_transfer_direction direction
)
1259 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1260 struct dma_slave_config
*sconfig
= &dwc
->dma_sconfig
;
1261 struct dw_cyclic_desc
*cdesc
;
1262 struct dw_cyclic_desc
*retval
= NULL
;
1263 struct dw_desc
*desc
;
1264 struct dw_desc
*last
= NULL
;
1265 u8 lms
= DWC_LLP_LMS(dwc
->m_master
);
1266 unsigned long was_cyclic
;
1267 unsigned int reg_width
;
1268 unsigned int periods
;
1270 unsigned long flags
;
1272 spin_lock_irqsave(&dwc
->lock
, flags
);
1274 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1275 dev_dbg(chan2dev(&dwc
->chan
),
1276 "channel doesn't support LLP transfers\n");
1277 return ERR_PTR(-EINVAL
);
1280 if (!list_empty(&dwc
->queue
) || !list_empty(&dwc
->active_list
)) {
1281 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1282 dev_dbg(chan2dev(&dwc
->chan
),
1283 "queue and/or active list are not empty\n");
1284 return ERR_PTR(-EBUSY
);
1287 was_cyclic
= test_and_set_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
);
1288 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1290 dev_dbg(chan2dev(&dwc
->chan
),
1291 "channel already prepared for cyclic DMA\n");
1292 return ERR_PTR(-EBUSY
);
1295 retval
= ERR_PTR(-EINVAL
);
1297 if (unlikely(!is_slave_direction(direction
)))
1300 dwc
->direction
= direction
;
1302 if (direction
== DMA_MEM_TO_DEV
)
1303 reg_width
= __ffs(sconfig
->dst_addr_width
);
1305 reg_width
= __ffs(sconfig
->src_addr_width
);
1307 periods
= buf_len
/ period_len
;
1309 /* Check for too big/unaligned periods and unaligned DMA buffer. */
1310 if (period_len
> (dwc
->block_size
<< reg_width
))
1312 if (unlikely(period_len
& ((1 << reg_width
) - 1)))
1314 if (unlikely(buf_addr
& ((1 << reg_width
) - 1)))
1317 retval
= ERR_PTR(-ENOMEM
);
1319 cdesc
= kzalloc(sizeof(struct dw_cyclic_desc
), GFP_KERNEL
);
1323 cdesc
->desc
= kzalloc(sizeof(struct dw_desc
*) * periods
, GFP_KERNEL
);
1327 for (i
= 0; i
< periods
; i
++) {
1328 desc
= dwc_desc_get(dwc
);
1330 goto out_err_desc_get
;
1332 switch (direction
) {
1333 case DMA_MEM_TO_DEV
:
1334 lli_write(desc
, dar
, sconfig
->dst_addr
);
1335 lli_write(desc
, sar
, buf_addr
+ period_len
* i
);
1336 lli_write(desc
, ctllo
, (DWC_DEFAULT_CTLLO(chan
)
1337 | DWC_CTLL_DST_WIDTH(reg_width
)
1338 | DWC_CTLL_SRC_WIDTH(reg_width
)
1341 | DWC_CTLL_INT_EN
));
1343 lli_set(desc
, ctllo
, sconfig
->device_fc
?
1344 DWC_CTLL_FC(DW_DMA_FC_P_M2P
) :
1345 DWC_CTLL_FC(DW_DMA_FC_D_M2P
));
1348 case DMA_DEV_TO_MEM
:
1349 lli_write(desc
, dar
, buf_addr
+ period_len
* i
);
1350 lli_write(desc
, sar
, sconfig
->src_addr
);
1351 lli_write(desc
, ctllo
, (DWC_DEFAULT_CTLLO(chan
)
1352 | DWC_CTLL_SRC_WIDTH(reg_width
)
1353 | DWC_CTLL_DST_WIDTH(reg_width
)
1356 | DWC_CTLL_INT_EN
));
1358 lli_set(desc
, ctllo
, sconfig
->device_fc
?
1359 DWC_CTLL_FC(DW_DMA_FC_P_P2M
) :
1360 DWC_CTLL_FC(DW_DMA_FC_D_P2M
));
1367 lli_write(desc
, ctlhi
, period_len
>> reg_width
);
1368 cdesc
->desc
[i
] = desc
;
1371 lli_write(last
, llp
, desc
->txd
.phys
| lms
);
1376 /* Let's make a cyclic list */
1377 lli_write(last
, llp
, cdesc
->desc
[0]->txd
.phys
| lms
);
1379 dev_dbg(chan2dev(&dwc
->chan
),
1380 "cyclic prepared buf %pad len %zu period %zu periods %d\n",
1381 &buf_addr
, buf_len
, period_len
, periods
);
1383 cdesc
->periods
= periods
;
1390 dwc_desc_put(dwc
, cdesc
->desc
[i
]);
1394 clear_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
);
1395 return (struct dw_cyclic_desc
*)retval
;
1397 EXPORT_SYMBOL(dw_dma_cyclic_prep
);
1400 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1401 * @chan: the DMA channel to free
1403 void dw_dma_cyclic_free(struct dma_chan
*chan
)
1405 struct dw_dma_chan
*dwc
= to_dw_dma_chan(chan
);
1406 struct dw_dma
*dw
= to_dw_dma(dwc
->chan
.device
);
1407 struct dw_cyclic_desc
*cdesc
= dwc
->cdesc
;
1409 unsigned long flags
;
1411 dev_dbg(chan2dev(&dwc
->chan
), "%s\n", __func__
);
1416 spin_lock_irqsave(&dwc
->lock
, flags
);
1418 dwc_chan_disable(dw
, dwc
);
1420 dma_writel(dw
, CLEAR
.BLOCK
, dwc
->mask
);
1421 dma_writel(dw
, CLEAR
.ERROR
, dwc
->mask
);
1422 dma_writel(dw
, CLEAR
.XFER
, dwc
->mask
);
1424 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1426 for (i
= 0; i
< cdesc
->periods
; i
++)
1427 dwc_desc_put(dwc
, cdesc
->desc
[i
]);
1434 clear_bit(DW_DMA_IS_CYCLIC
, &dwc
->flags
);
1436 EXPORT_SYMBOL(dw_dma_cyclic_free
);
1438 /*----------------------------------------------------------------------*/
1440 int dw_dma_probe(struct dw_dma_chip
*chip
)
1442 struct dw_dma_platform_data
*pdata
;
1444 bool autocfg
= false;
1445 unsigned int dw_params
;
1449 dw
= devm_kzalloc(chip
->dev
, sizeof(*dw
), GFP_KERNEL
);
1453 dw
->pdata
= devm_kzalloc(chip
->dev
, sizeof(*dw
->pdata
), GFP_KERNEL
);
1457 dw
->regs
= chip
->regs
;
1460 pm_runtime_get_sync(chip
->dev
);
1463 dw_params
= dma_readl(dw
, DW_PARAMS
);
1464 dev_dbg(chip
->dev
, "DW_PARAMS: 0x%08x\n", dw_params
);
1466 autocfg
= dw_params
>> DW_PARAMS_EN
& 1;
1472 /* Reassign the platform data pointer */
1475 /* Get hardware configuration parameters */
1476 pdata
->nr_channels
= (dw_params
>> DW_PARAMS_NR_CHAN
& 7) + 1;
1477 pdata
->nr_masters
= (dw_params
>> DW_PARAMS_NR_MASTER
& 3) + 1;
1478 for (i
= 0; i
< pdata
->nr_masters
; i
++) {
1479 pdata
->data_width
[i
] =
1480 4 << (dw_params
>> DW_PARAMS_DATA_WIDTH(i
) & 3);
1482 pdata
->block_size
= dma_readl(dw
, MAX_BLK_SIZE
);
1484 /* Fill platform data with the default values */
1485 pdata
->is_private
= true;
1486 pdata
->is_memcpy
= true;
1487 pdata
->chan_allocation_order
= CHAN_ALLOCATION_ASCENDING
;
1488 pdata
->chan_priority
= CHAN_PRIORITY_ASCENDING
;
1489 } else if (chip
->pdata
->nr_channels
> DW_DMA_MAX_NR_CHANNELS
) {
1493 memcpy(dw
->pdata
, chip
->pdata
, sizeof(*dw
->pdata
));
1495 /* Reassign the platform data pointer */
1499 dw
->chan
= devm_kcalloc(chip
->dev
, pdata
->nr_channels
, sizeof(*dw
->chan
),
1506 /* Calculate all channel mask before DMA setup */
1507 dw
->all_chan_mask
= (1 << pdata
->nr_channels
) - 1;
1509 /* Force dma off, just in case */
1512 /* Create a pool of consistent memory blocks for hardware descriptors */
1513 dw
->desc_pool
= dmam_pool_create("dw_dmac_desc_pool", chip
->dev
,
1514 sizeof(struct dw_desc
), 4, 0);
1515 if (!dw
->desc_pool
) {
1516 dev_err(chip
->dev
, "No memory for descriptors dma pool\n");
1521 tasklet_init(&dw
->tasklet
, dw_dma_tasklet
, (unsigned long)dw
);
1523 err
= request_irq(chip
->irq
, dw_dma_interrupt
, IRQF_SHARED
,
1528 INIT_LIST_HEAD(&dw
->dma
.channels
);
1529 for (i
= 0; i
< pdata
->nr_channels
; i
++) {
1530 struct dw_dma_chan
*dwc
= &dw
->chan
[i
];
1532 dwc
->chan
.device
= &dw
->dma
;
1533 dma_cookie_init(&dwc
->chan
);
1534 if (pdata
->chan_allocation_order
== CHAN_ALLOCATION_ASCENDING
)
1535 list_add_tail(&dwc
->chan
.device_node
,
1538 list_add(&dwc
->chan
.device_node
, &dw
->dma
.channels
);
1540 /* 7 is highest priority & 0 is lowest. */
1541 if (pdata
->chan_priority
== CHAN_PRIORITY_ASCENDING
)
1542 dwc
->priority
= pdata
->nr_channels
- i
- 1;
1546 dwc
->ch_regs
= &__dw_regs(dw
)->CHAN
[i
];
1547 spin_lock_init(&dwc
->lock
);
1550 INIT_LIST_HEAD(&dwc
->active_list
);
1551 INIT_LIST_HEAD(&dwc
->queue
);
1553 channel_clear_bit(dw
, CH_EN
, dwc
->mask
);
1555 dwc
->direction
= DMA_TRANS_NONE
;
1557 /* Hardware configuration */
1559 unsigned int r
= DW_DMA_MAX_NR_CHANNELS
- i
- 1;
1560 void __iomem
*addr
= &__dw_regs(dw
)->DWC_PARAMS
[r
];
1561 unsigned int dwc_params
= dma_readl_native(addr
);
1563 dev_dbg(chip
->dev
, "DWC_PARAMS[%d]: 0x%08x\n", i
,
1567 * Decode maximum block size for given channel. The
1568 * stored 4 bit value represents blocks from 0x00 for 3
1569 * up to 0x0a for 4095.
1572 (4 << ((pdata
->block_size
>> 4 * i
) & 0xf)) - 1;
1574 (dwc_params
>> DWC_PARAMS_MBLK_EN
& 0x1) == 0;
1576 dwc
->block_size
= pdata
->block_size
;
1578 /* Check if channel supports multi block transfer */
1579 channel_writel(dwc
, LLP
, DWC_LLP_LOC(0xffffffff));
1580 dwc
->nollp
= DWC_LLP_LOC(channel_readl(dwc
, LLP
)) == 0;
1581 channel_writel(dwc
, LLP
, 0);
1585 /* Clear all interrupts on all channels. */
1586 dma_writel(dw
, CLEAR
.XFER
, dw
->all_chan_mask
);
1587 dma_writel(dw
, CLEAR
.BLOCK
, dw
->all_chan_mask
);
1588 dma_writel(dw
, CLEAR
.SRC_TRAN
, dw
->all_chan_mask
);
1589 dma_writel(dw
, CLEAR
.DST_TRAN
, dw
->all_chan_mask
);
1590 dma_writel(dw
, CLEAR
.ERROR
, dw
->all_chan_mask
);
1592 /* Set capabilities */
1593 dma_cap_set(DMA_SLAVE
, dw
->dma
.cap_mask
);
1594 if (pdata
->is_private
)
1595 dma_cap_set(DMA_PRIVATE
, dw
->dma
.cap_mask
);
1596 if (pdata
->is_memcpy
)
1597 dma_cap_set(DMA_MEMCPY
, dw
->dma
.cap_mask
);
1599 dw
->dma
.dev
= chip
->dev
;
1600 dw
->dma
.device_alloc_chan_resources
= dwc_alloc_chan_resources
;
1601 dw
->dma
.device_free_chan_resources
= dwc_free_chan_resources
;
1603 dw
->dma
.device_prep_dma_memcpy
= dwc_prep_dma_memcpy
;
1604 dw
->dma
.device_prep_slave_sg
= dwc_prep_slave_sg
;
1606 dw
->dma
.device_config
= dwc_config
;
1607 dw
->dma
.device_pause
= dwc_pause
;
1608 dw
->dma
.device_resume
= dwc_resume
;
1609 dw
->dma
.device_terminate_all
= dwc_terminate_all
;
1611 dw
->dma
.device_tx_status
= dwc_tx_status
;
1612 dw
->dma
.device_issue_pending
= dwc_issue_pending
;
1614 /* DMA capabilities */
1615 dw
->dma
.src_addr_widths
= DW_DMA_BUSWIDTHS
;
1616 dw
->dma
.dst_addr_widths
= DW_DMA_BUSWIDTHS
;
1617 dw
->dma
.directions
= BIT(DMA_DEV_TO_MEM
) | BIT(DMA_MEM_TO_DEV
) |
1618 BIT(DMA_MEM_TO_MEM
);
1619 dw
->dma
.residue_granularity
= DMA_RESIDUE_GRANULARITY_BURST
;
1621 err
= dma_async_device_register(&dw
->dma
);
1623 goto err_dma_register
;
1625 dev_info(chip
->dev
, "DesignWare DMA Controller, %d channels\n",
1626 pdata
->nr_channels
);
1628 pm_runtime_put_sync_suspend(chip
->dev
);
1633 free_irq(chip
->irq
, dw
);
1635 pm_runtime_put_sync_suspend(chip
->dev
);
1638 EXPORT_SYMBOL_GPL(dw_dma_probe
);
1640 int dw_dma_remove(struct dw_dma_chip
*chip
)
1642 struct dw_dma
*dw
= chip
->dw
;
1643 struct dw_dma_chan
*dwc
, *_dwc
;
1645 pm_runtime_get_sync(chip
->dev
);
1648 dma_async_device_unregister(&dw
->dma
);
1650 free_irq(chip
->irq
, dw
);
1651 tasklet_kill(&dw
->tasklet
);
1653 list_for_each_entry_safe(dwc
, _dwc
, &dw
->dma
.channels
,
1655 list_del(&dwc
->chan
.device_node
);
1656 channel_clear_bit(dw
, CH_EN
, dwc
->mask
);
1659 pm_runtime_put_sync_suspend(chip
->dev
);
1662 EXPORT_SYMBOL_GPL(dw_dma_remove
);
1664 int dw_dma_disable(struct dw_dma_chip
*chip
)
1666 struct dw_dma
*dw
= chip
->dw
;
1671 EXPORT_SYMBOL_GPL(dw_dma_disable
);
1673 int dw_dma_enable(struct dw_dma_chip
*chip
)
1675 struct dw_dma
*dw
= chip
->dw
;
1680 EXPORT_SYMBOL_GPL(dw_dma_enable
);
1682 MODULE_LICENSE("GPL v2");
1683 MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
1684 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1685 MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");