2 * Copyright 2015 Robert Jarzmik <robert.jarzmik@free.fr>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/types.h>
13 #include <linux/interrupt.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/slab.h>
16 #include <linux/dmaengine.h>
17 #include <linux/platform_device.h>
18 #include <linux/device.h>
19 #include <linux/platform_data/mmp_dma.h>
20 #include <linux/dmapool.h>
21 #include <linux/of_device.h>
22 #include <linux/of_dma.h>
24 #include <linux/dma/pxa-dma.h>
26 #include "dmaengine.h"
29 #define DCSR(n) (0x0000 + ((n) << 2))
30 #define DALGN(n) 0x00a0
32 #define DDADR(n) (0x0200 + ((n) << 4))
33 #define DSADR(n) (0x0204 + ((n) << 4))
34 #define DTADR(n) (0x0208 + ((n) << 4))
35 #define DCMD(n) (0x020c + ((n) << 4))
37 #define PXA_DCSR_RUN BIT(31) /* Run Bit (read / write) */
38 #define PXA_DCSR_NODESC BIT(30) /* No-Descriptor Fetch (read / write) */
39 #define PXA_DCSR_STOPIRQEN BIT(29) /* Stop Interrupt Enable (R/W) */
40 #define PXA_DCSR_REQPEND BIT(8) /* Request Pending (read-only) */
41 #define PXA_DCSR_STOPSTATE BIT(3) /* Stop State (read-only) */
42 #define PXA_DCSR_ENDINTR BIT(2) /* End Interrupt (read / write) */
43 #define PXA_DCSR_STARTINTR BIT(1) /* Start Interrupt (read / write) */
44 #define PXA_DCSR_BUSERR BIT(0) /* Bus Error Interrupt (read / write) */
46 #define PXA_DCSR_EORIRQEN BIT(28) /* End of Receive IRQ Enable (R/W) */
47 #define PXA_DCSR_EORJMPEN BIT(27) /* Jump to next descriptor on EOR */
48 #define PXA_DCSR_EORSTOPEN BIT(26) /* STOP on an EOR */
49 #define PXA_DCSR_SETCMPST BIT(25) /* Set Descriptor Compare Status */
50 #define PXA_DCSR_CLRCMPST BIT(24) /* Clear Descriptor Compare Status */
51 #define PXA_DCSR_CMPST BIT(10) /* The Descriptor Compare Status */
52 #define PXA_DCSR_EORINTR BIT(9) /* The end of Receive */
54 #define DRCMR_MAPVLD BIT(7) /* Map Valid (read / write) */
55 #define DRCMR_CHLNUM 0x1f /* mask for Channel Number (read / write) */
57 #define DDADR_DESCADDR 0xfffffff0 /* Address of next descriptor (mask) */
58 #define DDADR_STOP BIT(0) /* Stop (read / write) */
60 #define PXA_DCMD_INCSRCADDR BIT(31) /* Source Address Increment Setting. */
61 #define PXA_DCMD_INCTRGADDR BIT(30) /* Target Address Increment Setting. */
62 #define PXA_DCMD_FLOWSRC BIT(29) /* Flow Control by the source. */
63 #define PXA_DCMD_FLOWTRG BIT(28) /* Flow Control by the target. */
64 #define PXA_DCMD_STARTIRQEN BIT(22) /* Start Interrupt Enable */
65 #define PXA_DCMD_ENDIRQEN BIT(21) /* End Interrupt Enable */
66 #define PXA_DCMD_ENDIAN BIT(18) /* Device Endian-ness. */
67 #define PXA_DCMD_BURST8 (1 << 16) /* 8 byte burst */
68 #define PXA_DCMD_BURST16 (2 << 16) /* 16 byte burst */
69 #define PXA_DCMD_BURST32 (3 << 16) /* 32 byte burst */
70 #define PXA_DCMD_WIDTH1 (1 << 14) /* 1 byte width */
71 #define PXA_DCMD_WIDTH2 (2 << 14) /* 2 byte width (HalfWord) */
72 #define PXA_DCMD_WIDTH4 (3 << 14) /* 4 byte width (Word) */
73 #define PXA_DCMD_LENGTH 0x01fff /* length mask (max = 8K - 1) */
75 #define PDMA_ALIGNMENT 3
76 #define PDMA_MAX_DESC_BYTES (PXA_DCMD_LENGTH & ~((1 << PDMA_ALIGNMENT) - 1))
79 u32 ddadr
; /* Points to the next descriptor + flags */
80 u32 dsadr
; /* DSADR value for the current transfer */
81 u32 dtadr
; /* DTADR value for the current transfer */
82 u32 dcmd
; /* DCMD value for the current transfer */
86 struct virt_dma_desc vd
; /* Virtual descriptor */
87 int nb_desc
; /* Number of hw. descriptors */
88 size_t len
; /* Number of bytes xfered */
89 dma_addr_t first
; /* First descriptor's addr */
91 /* At least one descriptor has an src/dst address not multiple of 8 */
94 struct dma_pool
*desc_pool
; /* Channel's used allocator */
96 struct pxad_desc_hw
*hw_desc
[]; /* DMA coherent descriptors */
102 struct pxad_chan
*vchan
;
106 struct virt_dma_chan vc
; /* Virtual channel */
107 u32 drcmr
; /* Requestor of the channel */
108 enum pxad_chan_prio prio
; /* Required priority of phy */
110 * At least one desc_sw in submitted or issued transfers on this channel
111 * has one address such as: addr % 8 != 0. This implies the DALGN
112 * setting on the phy.
115 struct dma_slave_config cfg
; /* Runtime config */
117 /* protected by vc->lock */
118 struct pxad_phy
*phy
;
119 struct dma_pool
*desc_pool
; /* Descriptors pool */
123 struct dma_device slave
;
126 struct pxad_phy
*phys
;
127 spinlock_t phy_lock
; /* Phy association */
128 #ifdef CONFIG_DEBUG_FS
129 struct dentry
*dbgfs_root
;
130 struct dentry
*dbgfs_state
;
131 struct dentry
**dbgfs_chan
;
135 #define tx_to_pxad_desc(tx) \
136 container_of(tx, struct pxad_desc_sw, async_tx)
137 #define to_pxad_chan(dchan) \
138 container_of(dchan, struct pxad_chan, vc.chan)
139 #define to_pxad_dev(dmadev) \
140 container_of(dmadev, struct pxad_device, slave)
141 #define to_pxad_sw_desc(_vd) \
142 container_of((_vd), struct pxad_desc_sw, vd)
144 #define _phy_readl_relaxed(phy, _reg) \
145 readl_relaxed((phy)->base + _reg((phy)->idx))
146 #define phy_readl_relaxed(phy, _reg) \
149 _v = readl_relaxed((phy)->base + _reg((phy)->idx)); \
150 dev_vdbg(&phy->vchan->vc.chan.dev->device, \
151 "%s(): readl(%s): 0x%08x\n", __func__, #_reg, \
155 #define phy_writel(phy, val, _reg) \
157 writel((val), (phy)->base + _reg((phy)->idx)); \
158 dev_vdbg(&phy->vchan->vc.chan.dev->device, \
159 "%s(): writel(0x%08x, %s)\n", \
160 __func__, (u32)(val), #_reg); \
162 #define phy_writel_relaxed(phy, val, _reg) \
164 writel_relaxed((val), (phy)->base + _reg((phy)->idx)); \
165 dev_vdbg(&phy->vchan->vc.chan.dev->device, \
166 "%s(): writel_relaxed(0x%08x, %s)\n", \
167 __func__, (u32)(val), #_reg); \
170 static unsigned int pxad_drcmr(unsigned int line
)
173 return 0x100 + line
* 4;
174 return 0x1000 + line
* 4;
180 #ifdef CONFIG_DEBUG_FS
181 #include <linux/debugfs.h>
182 #include <linux/uaccess.h>
183 #include <linux/seq_file.h>
185 static int dbg_show_requester_chan(struct seq_file
*s
, void *p
)
187 struct pxad_phy
*phy
= s
->private;
191 seq_printf(s
, "DMA channel %d requester :\n", phy
->idx
);
192 for (i
= 0; i
< 70; i
++) {
193 drcmr
= readl_relaxed(phy
->base
+ pxad_drcmr(i
));
194 if ((drcmr
& DRCMR_CHLNUM
) == phy
->idx
)
195 seq_printf(s
, "\tRequester %d (MAPVLD=%d)\n", i
,
196 !!(drcmr
& DRCMR_MAPVLD
));
201 static inline int dbg_burst_from_dcmd(u32 dcmd
)
203 int burst
= (dcmd
>> 16) & 0x3;
205 return burst
? 4 << burst
: 0;
208 static int is_phys_valid(unsigned long addr
)
210 return pfn_valid(__phys_to_pfn(addr
));
213 #define PXA_DCSR_STR(flag) (dcsr & PXA_DCSR_##flag ? #flag" " : "")
214 #define PXA_DCMD_STR(flag) (dcmd & PXA_DCMD_##flag ? #flag" " : "")
216 static int dbg_show_descriptors(struct seq_file
*s
, void *p
)
218 struct pxad_phy
*phy
= s
->private;
219 int i
, max_show
= 20, burst
, width
;
221 unsigned long phys_desc
, ddadr
;
222 struct pxad_desc_hw
*desc
;
224 phys_desc
= ddadr
= _phy_readl_relaxed(phy
, DDADR
);
226 seq_printf(s
, "DMA channel %d descriptors :\n", phy
->idx
);
227 seq_printf(s
, "[%03d] First descriptor unknown\n", 0);
228 for (i
= 1; i
< max_show
&& is_phys_valid(phys_desc
); i
++) {
229 desc
= phys_to_virt(phys_desc
);
231 burst
= dbg_burst_from_dcmd(dcmd
);
232 width
= (1 << ((dcmd
>> 14) & 0x3)) >> 1;
234 seq_printf(s
, "[%03d] Desc at %08lx(virt %p)\n",
236 seq_printf(s
, "\tDDADR = %08x\n", desc
->ddadr
);
237 seq_printf(s
, "\tDSADR = %08x\n", desc
->dsadr
);
238 seq_printf(s
, "\tDTADR = %08x\n", desc
->dtadr
);
239 seq_printf(s
, "\tDCMD = %08x (%s%s%s%s%s%s%sburst=%d width=%d len=%d)\n",
241 PXA_DCMD_STR(INCSRCADDR
), PXA_DCMD_STR(INCTRGADDR
),
242 PXA_DCMD_STR(FLOWSRC
), PXA_DCMD_STR(FLOWTRG
),
243 PXA_DCMD_STR(STARTIRQEN
), PXA_DCMD_STR(ENDIRQEN
),
244 PXA_DCMD_STR(ENDIAN
), burst
, width
,
245 dcmd
& PXA_DCMD_LENGTH
);
246 phys_desc
= desc
->ddadr
;
249 seq_printf(s
, "[%03d] Desc at %08lx ... max display reached\n",
252 seq_printf(s
, "[%03d] Desc at %08lx is %s\n",
253 i
, phys_desc
, phys_desc
== DDADR_STOP
?
254 "DDADR_STOP" : "invalid");
259 static int dbg_show_chan_state(struct seq_file
*s
, void *p
)
261 struct pxad_phy
*phy
= s
->private;
264 static const char * const str_prio
[] = {
265 "high", "normal", "low", "invalid"
268 dcsr
= _phy_readl_relaxed(phy
, DCSR
);
269 dcmd
= _phy_readl_relaxed(phy
, DCMD
);
270 burst
= dbg_burst_from_dcmd(dcmd
);
271 width
= (1 << ((dcmd
>> 14) & 0x3)) >> 1;
273 seq_printf(s
, "DMA channel %d\n", phy
->idx
);
274 seq_printf(s
, "\tPriority : %s\n",
275 str_prio
[(phy
->idx
& 0xf) / 4]);
276 seq_printf(s
, "\tUnaligned transfer bit: %s\n",
277 _phy_readl_relaxed(phy
, DALGN
) & BIT(phy
->idx
) ?
279 seq_printf(s
, "\tDCSR = %08x (%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
280 dcsr
, PXA_DCSR_STR(RUN
), PXA_DCSR_STR(NODESC
),
281 PXA_DCSR_STR(STOPIRQEN
), PXA_DCSR_STR(EORIRQEN
),
282 PXA_DCSR_STR(EORJMPEN
), PXA_DCSR_STR(EORSTOPEN
),
283 PXA_DCSR_STR(SETCMPST
), PXA_DCSR_STR(CLRCMPST
),
284 PXA_DCSR_STR(CMPST
), PXA_DCSR_STR(EORINTR
),
285 PXA_DCSR_STR(REQPEND
), PXA_DCSR_STR(STOPSTATE
),
286 PXA_DCSR_STR(ENDINTR
), PXA_DCSR_STR(STARTINTR
),
287 PXA_DCSR_STR(BUSERR
));
289 seq_printf(s
, "\tDCMD = %08x (%s%s%s%s%s%s%sburst=%d width=%d len=%d)\n",
291 PXA_DCMD_STR(INCSRCADDR
), PXA_DCMD_STR(INCTRGADDR
),
292 PXA_DCMD_STR(FLOWSRC
), PXA_DCMD_STR(FLOWTRG
),
293 PXA_DCMD_STR(STARTIRQEN
), PXA_DCMD_STR(ENDIRQEN
),
294 PXA_DCMD_STR(ENDIAN
), burst
, width
, dcmd
& PXA_DCMD_LENGTH
);
295 seq_printf(s
, "\tDSADR = %08x\n", _phy_readl_relaxed(phy
, DSADR
));
296 seq_printf(s
, "\tDTADR = %08x\n", _phy_readl_relaxed(phy
, DTADR
));
297 seq_printf(s
, "\tDDADR = %08x\n", _phy_readl_relaxed(phy
, DDADR
));
302 static int dbg_show_state(struct seq_file
*s
, void *p
)
304 struct pxad_device
*pdev
= s
->private;
306 /* basic device status */
307 seq_puts(s
, "DMA engine status\n");
308 seq_printf(s
, "\tChannel number: %d\n", pdev
->nr_chans
);
313 #define DBGFS_FUNC_DECL(name) \
314 static int dbg_open_##name(struct inode *inode, struct file *file) \
316 return single_open(file, dbg_show_##name, inode->i_private); \
318 static const struct file_operations dbg_fops_##name = { \
319 .owner = THIS_MODULE, \
320 .open = dbg_open_##name, \
321 .llseek = seq_lseek, \
323 .release = single_release, \
326 DBGFS_FUNC_DECL(state
);
327 DBGFS_FUNC_DECL(chan_state
);
328 DBGFS_FUNC_DECL(descriptors
);
329 DBGFS_FUNC_DECL(requester_chan
);
331 static struct dentry
*pxad_dbg_alloc_chan(struct pxad_device
*pdev
,
332 int ch
, struct dentry
*chandir
)
335 struct dentry
*chan
, *chan_state
= NULL
, *chan_descr
= NULL
;
336 struct dentry
*chan_reqs
= NULL
;
339 scnprintf(chan_name
, sizeof(chan_name
), "%d", ch
);
340 chan
= debugfs_create_dir(chan_name
, chandir
);
341 dt
= (void *)&pdev
->phys
[ch
];
344 chan_state
= debugfs_create_file("state", 0400, chan
, dt
,
345 &dbg_fops_chan_state
);
347 chan_descr
= debugfs_create_file("descriptors", 0400, chan
, dt
,
348 &dbg_fops_descriptors
);
350 chan_reqs
= debugfs_create_file("requesters", 0400, chan
, dt
,
351 &dbg_fops_requester_chan
);
358 debugfs_remove_recursive(chan
);
362 static void pxad_init_debugfs(struct pxad_device
*pdev
)
365 struct dentry
*chandir
;
367 pdev
->dbgfs_root
= debugfs_create_dir(dev_name(pdev
->slave
.dev
), NULL
);
368 if (IS_ERR(pdev
->dbgfs_root
) || !pdev
->dbgfs_root
)
371 pdev
->dbgfs_state
= debugfs_create_file("state", 0400, pdev
->dbgfs_root
,
372 pdev
, &dbg_fops_state
);
373 if (!pdev
->dbgfs_state
)
377 kmalloc_array(pdev
->nr_chans
, sizeof(*pdev
->dbgfs_state
),
379 if (!pdev
->dbgfs_chan
)
382 chandir
= debugfs_create_dir("channels", pdev
->dbgfs_root
);
386 for (i
= 0; i
< pdev
->nr_chans
; i
++) {
387 pdev
->dbgfs_chan
[i
] = pxad_dbg_alloc_chan(pdev
, i
, chandir
);
388 if (!pdev
->dbgfs_chan
[i
])
395 kfree(pdev
->dbgfs_chan
);
398 debugfs_remove_recursive(pdev
->dbgfs_root
);
400 pr_err("pxad: debugfs is not available\n");
403 static void pxad_cleanup_debugfs(struct pxad_device
*pdev
)
405 debugfs_remove_recursive(pdev
->dbgfs_root
);
408 static inline void pxad_init_debugfs(struct pxad_device
*pdev
) {}
409 static inline void pxad_cleanup_debugfs(struct pxad_device
*pdev
) {}
413 * In the transition phase where legacy pxa handling is done at the same time as
414 * mmp_dma, the DMA physical channel split between the 2 DMA providers is done
415 * through legacy_reserved. Legacy code reserves DMA channels by settings
416 * corresponding bits in legacy_reserved.
418 static u32 legacy_reserved
;
419 static u32 legacy_unavailable
;
421 static struct pxad_phy
*lookup_phy(struct pxad_chan
*pchan
)
424 struct pxad_device
*pdev
= to_pxad_dev(pchan
->vc
.chan
.device
);
425 struct pxad_phy
*phy
, *found
= NULL
;
429 * dma channel priorities
430 * ch 0 - 3, 16 - 19 <--> (0)
431 * ch 4 - 7, 20 - 23 <--> (1)
432 * ch 8 - 11, 24 - 27 <--> (2)
433 * ch 12 - 15, 28 - 31 <--> (3)
436 spin_lock_irqsave(&pdev
->phy_lock
, flags
);
437 for (prio
= pchan
->prio
; prio
>= PXAD_PRIO_HIGHEST
; prio
--) {
438 for (i
= 0; i
< pdev
->nr_chans
; i
++) {
439 if (prio
!= (i
& 0xf) >> 2)
441 if ((i
< 32) && (legacy_reserved
& BIT(i
)))
443 phy
= &pdev
->phys
[i
];
448 legacy_unavailable
|= BIT(i
);
455 spin_unlock_irqrestore(&pdev
->phy_lock
, flags
);
456 dev_dbg(&pchan
->vc
.chan
.dev
->device
,
457 "%s(): phy=%p(%d)\n", __func__
, found
,
458 found
? found
->idx
: -1);
463 static void pxad_free_phy(struct pxad_chan
*chan
)
465 struct pxad_device
*pdev
= to_pxad_dev(chan
->vc
.chan
.device
);
470 dev_dbg(&chan
->vc
.chan
.dev
->device
,
471 "%s(): freeing\n", __func__
);
475 /* clear the channel mapping in DRCMR */
476 if (chan
->drcmr
<= DRCMR_CHLNUM
) {
477 reg
= pxad_drcmr(chan
->drcmr
);
478 writel_relaxed(0, chan
->phy
->base
+ reg
);
481 spin_lock_irqsave(&pdev
->phy_lock
, flags
);
482 for (i
= 0; i
< 32; i
++)
483 if (chan
->phy
== &pdev
->phys
[i
])
484 legacy_unavailable
&= ~BIT(i
);
485 chan
->phy
->vchan
= NULL
;
487 spin_unlock_irqrestore(&pdev
->phy_lock
, flags
);
490 static bool is_chan_running(struct pxad_chan
*chan
)
493 struct pxad_phy
*phy
= chan
->phy
;
497 dcsr
= phy_readl_relaxed(phy
, DCSR
);
498 return dcsr
& PXA_DCSR_RUN
;
501 static bool is_running_chan_misaligned(struct pxad_chan
*chan
)
506 dalgn
= phy_readl_relaxed(chan
->phy
, DALGN
);
507 return dalgn
& (BIT(chan
->phy
->idx
));
510 static void phy_enable(struct pxad_phy
*phy
, bool misaligned
)
517 dev_dbg(&phy
->vchan
->vc
.chan
.dev
->device
,
518 "%s(); phy=%p(%d) misaligned=%d\n", __func__
,
519 phy
, phy
->idx
, misaligned
);
521 if (phy
->vchan
->drcmr
<= DRCMR_CHLNUM
) {
522 reg
= pxad_drcmr(phy
->vchan
->drcmr
);
523 writel_relaxed(DRCMR_MAPVLD
| phy
->idx
, phy
->base
+ reg
);
526 dalgn
= phy_readl_relaxed(phy
, DALGN
);
528 dalgn
|= BIT(phy
->idx
);
530 dalgn
&= ~BIT(phy
->idx
);
531 phy_writel_relaxed(phy
, dalgn
, DALGN
);
533 phy_writel(phy
, PXA_DCSR_STOPIRQEN
| PXA_DCSR_ENDINTR
|
534 PXA_DCSR_BUSERR
| PXA_DCSR_RUN
, DCSR
);
537 static void phy_disable(struct pxad_phy
*phy
)
544 dcsr
= phy_readl_relaxed(phy
, DCSR
);
545 dev_dbg(&phy
->vchan
->vc
.chan
.dev
->device
,
546 "%s(): phy=%p(%d)\n", __func__
, phy
, phy
->idx
);
547 phy_writel(phy
, dcsr
& ~PXA_DCSR_RUN
& ~PXA_DCSR_STOPIRQEN
, DCSR
);
550 static void pxad_launch_chan(struct pxad_chan
*chan
,
551 struct pxad_desc_sw
*desc
)
553 dev_dbg(&chan
->vc
.chan
.dev
->device
,
554 "%s(): desc=%p\n", __func__
, desc
);
556 chan
->phy
= lookup_phy(chan
);
558 dev_dbg(&chan
->vc
.chan
.dev
->device
,
559 "%s(): no free dma channel\n", __func__
);
565 * Program the descriptor's address into the DMA controller,
566 * then start the DMA transaction
568 phy_writel(chan
->phy
, desc
->first
, DDADR
);
569 phy_enable(chan
->phy
, chan
->misaligned
);
572 static void set_updater_desc(struct pxad_desc_sw
*sw_desc
,
575 struct pxad_desc_hw
*updater
=
576 sw_desc
->hw_desc
[sw_desc
->nb_desc
- 1];
577 dma_addr_t dma
= sw_desc
->hw_desc
[sw_desc
->nb_desc
- 2]->ddadr
;
579 updater
->ddadr
= DDADR_STOP
;
580 updater
->dsadr
= dma
;
581 updater
->dtadr
= dma
+ 8;
582 updater
->dcmd
= PXA_DCMD_WIDTH4
| PXA_DCMD_BURST32
|
583 (PXA_DCMD_LENGTH
& sizeof(u32
));
584 if (flags
& DMA_PREP_INTERRUPT
)
585 updater
->dcmd
|= PXA_DCMD_ENDIRQEN
;
588 static bool is_desc_completed(struct virt_dma_desc
*vd
)
590 struct pxad_desc_sw
*sw_desc
= to_pxad_sw_desc(vd
);
591 struct pxad_desc_hw
*updater
=
592 sw_desc
->hw_desc
[sw_desc
->nb_desc
- 1];
594 return updater
->dtadr
!= (updater
->dsadr
+ 8);
597 static void pxad_desc_chain(struct virt_dma_desc
*vd1
,
598 struct virt_dma_desc
*vd2
)
600 struct pxad_desc_sw
*desc1
= to_pxad_sw_desc(vd1
);
601 struct pxad_desc_sw
*desc2
= to_pxad_sw_desc(vd2
);
602 dma_addr_t dma_to_chain
;
604 dma_to_chain
= desc2
->first
;
605 desc1
->hw_desc
[desc1
->nb_desc
- 1]->ddadr
= dma_to_chain
;
608 static bool pxad_try_hotchain(struct virt_dma_chan
*vc
,
609 struct virt_dma_desc
*vd
)
611 struct virt_dma_desc
*vd_last_issued
= NULL
;
612 struct pxad_chan
*chan
= to_pxad_chan(&vc
->chan
);
615 * Attempt to hot chain the tx if the phy is still running. This is
616 * considered successful only if either the channel is still running
617 * after the chaining, or if the chained transfer is completed after
618 * having been hot chained.
619 * A change of alignment is not allowed, and forbids hotchaining.
621 if (is_chan_running(chan
)) {
622 BUG_ON(list_empty(&vc
->desc_issued
));
624 if (!is_running_chan_misaligned(chan
) &&
625 to_pxad_sw_desc(vd
)->misaligned
)
628 vd_last_issued
= list_entry(vc
->desc_issued
.prev
,
629 struct virt_dma_desc
, node
);
630 pxad_desc_chain(vd_last_issued
, vd
);
631 if (is_chan_running(chan
) || is_desc_completed(vd_last_issued
))
638 static unsigned int clear_chan_irq(struct pxad_phy
*phy
)
641 u32 dint
= readl(phy
->base
+ DINT
);
643 if (!(dint
& BIT(phy
->idx
)))
647 dcsr
= phy_readl_relaxed(phy
, DCSR
);
648 phy_writel(phy
, dcsr
, DCSR
);
649 if ((dcsr
& PXA_DCSR_BUSERR
) && (phy
->vchan
))
650 dev_warn(&phy
->vchan
->vc
.chan
.dev
->device
,
651 "%s(chan=%p): PXA_DCSR_BUSERR\n",
652 __func__
, &phy
->vchan
);
654 return dcsr
& ~PXA_DCSR_RUN
;
657 static irqreturn_t
pxad_chan_handler(int irq
, void *dev_id
)
659 struct pxad_phy
*phy
= dev_id
;
660 struct pxad_chan
*chan
= phy
->vchan
;
661 struct virt_dma_desc
*vd
, *tmp
;
667 dcsr
= clear_chan_irq(phy
);
668 if (dcsr
& PXA_DCSR_RUN
)
671 spin_lock_irqsave(&chan
->vc
.lock
, flags
);
672 list_for_each_entry_safe(vd
, tmp
, &chan
->vc
.desc_issued
, node
) {
673 dev_dbg(&chan
->vc
.chan
.dev
->device
,
674 "%s(): checking txd %p[%x]: completed=%d\n",
675 __func__
, vd
, vd
->tx
.cookie
, is_desc_completed(vd
));
676 if (is_desc_completed(vd
)) {
678 vchan_cookie_complete(vd
);
684 if (dcsr
& PXA_DCSR_STOPSTATE
) {
685 dev_dbg(&chan
->vc
.chan
.dev
->device
,
686 "%s(): channel stopped, submitted_empty=%d issued_empty=%d",
688 list_empty(&chan
->vc
.desc_submitted
),
689 list_empty(&chan
->vc
.desc_issued
));
690 phy_writel_relaxed(phy
, dcsr
& ~PXA_DCSR_STOPIRQEN
, DCSR
);
692 if (list_empty(&chan
->vc
.desc_issued
)) {
694 !list_empty(&chan
->vc
.desc_submitted
);
696 vd
= list_first_entry(&chan
->vc
.desc_issued
,
697 struct virt_dma_desc
, node
);
698 pxad_launch_chan(chan
, to_pxad_sw_desc(vd
));
701 spin_unlock_irqrestore(&chan
->vc
.lock
, flags
);
706 static irqreturn_t
pxad_int_handler(int irq
, void *dev_id
)
708 struct pxad_device
*pdev
= dev_id
;
709 struct pxad_phy
*phy
;
710 u32 dint
= readl(pdev
->base
+ DINT
);
711 int i
, ret
= IRQ_NONE
;
716 phy
= &pdev
->phys
[i
];
717 if ((i
< 32) && (legacy_reserved
& BIT(i
)))
719 if (pxad_chan_handler(irq
, phy
) == IRQ_HANDLED
)
726 static int pxad_alloc_chan_resources(struct dma_chan
*dchan
)
728 struct pxad_chan
*chan
= to_pxad_chan(dchan
);
729 struct pxad_device
*pdev
= to_pxad_dev(chan
->vc
.chan
.device
);
734 chan
->desc_pool
= dma_pool_create(dma_chan_name(dchan
),
736 sizeof(struct pxad_desc_hw
),
737 __alignof__(struct pxad_desc_hw
),
739 if (!chan
->desc_pool
) {
740 dev_err(&chan
->vc
.chan
.dev
->device
,
741 "%s(): unable to allocate descriptor pool\n",
749 static void pxad_free_chan_resources(struct dma_chan
*dchan
)
751 struct pxad_chan
*chan
= to_pxad_chan(dchan
);
753 vchan_free_chan_resources(&chan
->vc
);
754 dma_pool_destroy(chan
->desc_pool
);
755 chan
->desc_pool
= NULL
;
759 static void pxad_free_desc(struct virt_dma_desc
*vd
)
763 struct pxad_desc_sw
*sw_desc
= to_pxad_sw_desc(vd
);
765 BUG_ON(sw_desc
->nb_desc
== 0);
766 for (i
= sw_desc
->nb_desc
- 1; i
>= 0; i
--) {
768 dma
= sw_desc
->hw_desc
[i
- 1]->ddadr
;
770 dma
= sw_desc
->first
;
771 dma_pool_free(sw_desc
->desc_pool
,
772 sw_desc
->hw_desc
[i
], dma
);
774 sw_desc
->nb_desc
= 0;
778 static struct pxad_desc_sw
*
779 pxad_alloc_desc(struct pxad_chan
*chan
, unsigned int nb_hw_desc
)
781 struct pxad_desc_sw
*sw_desc
;
785 sw_desc
= kzalloc(sizeof(*sw_desc
) +
786 nb_hw_desc
* sizeof(struct pxad_desc_hw
*),
790 sw_desc
->desc_pool
= chan
->desc_pool
;
792 for (i
= 0; i
< nb_hw_desc
; i
++) {
793 sw_desc
->hw_desc
[i
] = dma_pool_alloc(sw_desc
->desc_pool
,
795 if (!sw_desc
->hw_desc
[i
]) {
796 dev_err(&chan
->vc
.chan
.dev
->device
,
797 "%s(): Couldn't allocate the %dth hw_desc from dma_pool %p\n",
798 __func__
, i
, sw_desc
->desc_pool
);
803 sw_desc
->first
= dma
;
805 sw_desc
->hw_desc
[i
- 1]->ddadr
= dma
;
811 pxad_free_desc(&sw_desc
->vd
);
815 static dma_cookie_t
pxad_tx_submit(struct dma_async_tx_descriptor
*tx
)
817 struct virt_dma_chan
*vc
= to_virt_chan(tx
->chan
);
818 struct pxad_chan
*chan
= to_pxad_chan(&vc
->chan
);
819 struct virt_dma_desc
*vd_chained
= NULL
,
820 *vd
= container_of(tx
, struct virt_dma_desc
, tx
);
824 set_updater_desc(to_pxad_sw_desc(vd
), tx
->flags
);
826 spin_lock_irqsave(&vc
->lock
, flags
);
827 cookie
= dma_cookie_assign(tx
);
829 if (list_empty(&vc
->desc_submitted
) && pxad_try_hotchain(vc
, vd
)) {
830 list_move_tail(&vd
->node
, &vc
->desc_issued
);
831 dev_dbg(&chan
->vc
.chan
.dev
->device
,
832 "%s(): txd %p[%x]: submitted (hot linked)\n",
833 __func__
, vd
, cookie
);
838 * Fallback to placing the tx in the submitted queue
840 if (!list_empty(&vc
->desc_submitted
)) {
841 vd_chained
= list_entry(vc
->desc_submitted
.prev
,
842 struct virt_dma_desc
, node
);
844 * Only chain the descriptors if no new misalignment is
845 * introduced. If a new misalignment is chained, let the channel
846 * stop, and be relaunched in misalign mode from the irq
849 if (chan
->misaligned
|| !to_pxad_sw_desc(vd
)->misaligned
)
850 pxad_desc_chain(vd_chained
, vd
);
854 dev_dbg(&chan
->vc
.chan
.dev
->device
,
855 "%s(): txd %p[%x]: submitted (%s linked)\n",
856 __func__
, vd
, cookie
, vd_chained
? "cold" : "not");
857 list_move_tail(&vd
->node
, &vc
->desc_submitted
);
858 chan
->misaligned
|= to_pxad_sw_desc(vd
)->misaligned
;
861 spin_unlock_irqrestore(&vc
->lock
, flags
);
865 static void pxad_issue_pending(struct dma_chan
*dchan
)
867 struct pxad_chan
*chan
= to_pxad_chan(dchan
);
868 struct virt_dma_desc
*vd_first
;
871 spin_lock_irqsave(&chan
->vc
.lock
, flags
);
872 if (list_empty(&chan
->vc
.desc_submitted
))
875 vd_first
= list_first_entry(&chan
->vc
.desc_submitted
,
876 struct virt_dma_desc
, node
);
877 dev_dbg(&chan
->vc
.chan
.dev
->device
,
878 "%s(): txd %p[%x]", __func__
, vd_first
, vd_first
->tx
.cookie
);
880 vchan_issue_pending(&chan
->vc
);
881 if (!pxad_try_hotchain(&chan
->vc
, vd_first
))
882 pxad_launch_chan(chan
, to_pxad_sw_desc(vd_first
));
884 spin_unlock_irqrestore(&chan
->vc
.lock
, flags
);
887 static inline struct dma_async_tx_descriptor
*
888 pxad_tx_prep(struct virt_dma_chan
*vc
, struct virt_dma_desc
*vd
,
889 unsigned long tx_flags
)
891 struct dma_async_tx_descriptor
*tx
;
892 struct pxad_chan
*chan
= container_of(vc
, struct pxad_chan
, vc
);
894 INIT_LIST_HEAD(&vd
->node
);
895 tx
= vchan_tx_prep(vc
, vd
, tx_flags
);
896 tx
->tx_submit
= pxad_tx_submit
;
897 dev_dbg(&chan
->vc
.chan
.dev
->device
,
898 "%s(): vc=%p txd=%p[%x] flags=0x%lx\n", __func__
,
899 vc
, vd
, vd
->tx
.cookie
,
905 static void pxad_get_config(struct pxad_chan
*chan
,
906 enum dma_transfer_direction dir
,
907 u32
*dcmd
, u32
*dev_src
, u32
*dev_dst
)
909 u32 maxburst
= 0, dev_addr
= 0;
910 enum dma_slave_buswidth width
= DMA_SLAVE_BUSWIDTH_UNDEFINED
;
913 if (dir
== DMA_DEV_TO_MEM
) {
914 maxburst
= chan
->cfg
.src_maxburst
;
915 width
= chan
->cfg
.src_addr_width
;
916 dev_addr
= chan
->cfg
.src_addr
;
918 *dcmd
|= PXA_DCMD_INCTRGADDR
;
919 if (chan
->drcmr
<= DRCMR_CHLNUM
)
920 *dcmd
|= PXA_DCMD_FLOWSRC
;
922 if (dir
== DMA_MEM_TO_DEV
) {
923 maxburst
= chan
->cfg
.dst_maxburst
;
924 width
= chan
->cfg
.dst_addr_width
;
925 dev_addr
= chan
->cfg
.dst_addr
;
927 *dcmd
|= PXA_DCMD_INCSRCADDR
;
928 if (chan
->drcmr
<= DRCMR_CHLNUM
)
929 *dcmd
|= PXA_DCMD_FLOWTRG
;
931 if (dir
== DMA_MEM_TO_MEM
)
932 *dcmd
|= PXA_DCMD_BURST32
| PXA_DCMD_INCTRGADDR
|
935 dev_dbg(&chan
->vc
.chan
.dev
->device
,
936 "%s(): dev_addr=0x%x maxburst=%d width=%d dir=%d\n",
937 __func__
, dev_addr
, maxburst
, width
, dir
);
939 if (width
== DMA_SLAVE_BUSWIDTH_1_BYTE
)
940 *dcmd
|= PXA_DCMD_WIDTH1
;
941 else if (width
== DMA_SLAVE_BUSWIDTH_2_BYTES
)
942 *dcmd
|= PXA_DCMD_WIDTH2
;
943 else if (width
== DMA_SLAVE_BUSWIDTH_4_BYTES
)
944 *dcmd
|= PXA_DCMD_WIDTH4
;
947 *dcmd
|= PXA_DCMD_BURST8
;
948 else if (maxburst
== 16)
949 *dcmd
|= PXA_DCMD_BURST16
;
950 else if (maxburst
== 32)
951 *dcmd
|= PXA_DCMD_BURST32
;
953 /* FIXME: drivers should be ported over to use the filter
954 * function. Once that's done, the following two lines can
957 if (chan
->cfg
.slave_id
)
958 chan
->drcmr
= chan
->cfg
.slave_id
;
961 static struct dma_async_tx_descriptor
*
962 pxad_prep_memcpy(struct dma_chan
*dchan
,
963 dma_addr_t dma_dst
, dma_addr_t dma_src
,
964 size_t len
, unsigned long flags
)
966 struct pxad_chan
*chan
= to_pxad_chan(dchan
);
967 struct pxad_desc_sw
*sw_desc
;
968 struct pxad_desc_hw
*hw_desc
;
970 unsigned int i
, nb_desc
= 0;
976 dev_dbg(&chan
->vc
.chan
.dev
->device
,
977 "%s(): dma_dst=0x%lx dma_src=0x%lx len=%zu flags=%lx\n",
978 __func__
, (unsigned long)dma_dst
, (unsigned long)dma_src
,
980 pxad_get_config(chan
, DMA_MEM_TO_MEM
, &dcmd
, NULL
, NULL
);
982 nb_desc
= DIV_ROUND_UP(len
, PDMA_MAX_DESC_BYTES
);
983 sw_desc
= pxad_alloc_desc(chan
, nb_desc
+ 1);
988 if (!IS_ALIGNED(dma_src
, 1 << PDMA_ALIGNMENT
) ||
989 !IS_ALIGNED(dma_dst
, 1 << PDMA_ALIGNMENT
))
990 sw_desc
->misaligned
= true;
994 hw_desc
= sw_desc
->hw_desc
[i
++];
995 copy
= min_t(size_t, len
, PDMA_MAX_DESC_BYTES
);
996 hw_desc
->dcmd
= dcmd
| (PXA_DCMD_LENGTH
& copy
);
997 hw_desc
->dsadr
= dma_src
;
998 hw_desc
->dtadr
= dma_dst
;
1003 set_updater_desc(sw_desc
, flags
);
1005 return pxad_tx_prep(&chan
->vc
, &sw_desc
->vd
, flags
);
1008 static struct dma_async_tx_descriptor
*
1009 pxad_prep_slave_sg(struct dma_chan
*dchan
, struct scatterlist
*sgl
,
1010 unsigned int sg_len
, enum dma_transfer_direction dir
,
1011 unsigned long flags
, void *context
)
1013 struct pxad_chan
*chan
= to_pxad_chan(dchan
);
1014 struct pxad_desc_sw
*sw_desc
;
1016 struct scatterlist
*sg
;
1018 u32 dcmd
, dsadr
= 0, dtadr
= 0;
1019 unsigned int nb_desc
= 0, i
, j
= 0;
1021 if ((sgl
== NULL
) || (sg_len
== 0))
1024 pxad_get_config(chan
, dir
, &dcmd
, &dsadr
, &dtadr
);
1025 dev_dbg(&chan
->vc
.chan
.dev
->device
,
1026 "%s(): dir=%d flags=%lx\n", __func__
, dir
, flags
);
1028 for_each_sg(sgl
, sg
, sg_len
, i
)
1029 nb_desc
+= DIV_ROUND_UP(sg_dma_len(sg
), PDMA_MAX_DESC_BYTES
);
1030 sw_desc
= pxad_alloc_desc(chan
, nb_desc
+ 1);
1034 for_each_sg(sgl
, sg
, sg_len
, i
) {
1035 dma
= sg_dma_address(sg
);
1036 avail
= sg_dma_len(sg
);
1037 sw_desc
->len
+= avail
;
1040 len
= min_t(size_t, avail
, PDMA_MAX_DESC_BYTES
);
1042 sw_desc
->misaligned
= true;
1044 sw_desc
->hw_desc
[j
]->dcmd
=
1045 dcmd
| (PXA_DCMD_LENGTH
& len
);
1046 sw_desc
->hw_desc
[j
]->dsadr
= dsadr
? dsadr
: dma
;
1047 sw_desc
->hw_desc
[j
++]->dtadr
= dtadr
? dtadr
: dma
;
1053 set_updater_desc(sw_desc
, flags
);
1055 return pxad_tx_prep(&chan
->vc
, &sw_desc
->vd
, flags
);
1058 static struct dma_async_tx_descriptor
*
1059 pxad_prep_dma_cyclic(struct dma_chan
*dchan
,
1060 dma_addr_t buf_addr
, size_t len
, size_t period_len
,
1061 enum dma_transfer_direction dir
, unsigned long flags
)
1063 struct pxad_chan
*chan
= to_pxad_chan(dchan
);
1064 struct pxad_desc_sw
*sw_desc
;
1065 struct pxad_desc_hw
**phw_desc
;
1067 u32 dcmd
, dsadr
= 0, dtadr
= 0;
1068 unsigned int nb_desc
= 0;
1070 if (!dchan
|| !len
|| !period_len
)
1072 if ((dir
!= DMA_DEV_TO_MEM
) && (dir
!= DMA_MEM_TO_DEV
)) {
1073 dev_err(&chan
->vc
.chan
.dev
->device
,
1074 "Unsupported direction for cyclic DMA\n");
1077 /* the buffer length must be a multiple of period_len */
1078 if (len
% period_len
!= 0 || period_len
> PDMA_MAX_DESC_BYTES
||
1079 !IS_ALIGNED(period_len
, 1 << PDMA_ALIGNMENT
))
1082 pxad_get_config(chan
, dir
, &dcmd
, &dsadr
, &dtadr
);
1083 dcmd
|= PXA_DCMD_ENDIRQEN
| (PXA_DCMD_LENGTH
| period_len
);
1084 dev_dbg(&chan
->vc
.chan
.dev
->device
,
1085 "%s(): buf_addr=0x%lx len=%zu period=%zu dir=%d flags=%lx\n",
1086 __func__
, (unsigned long)buf_addr
, len
, period_len
, dir
, flags
);
1088 nb_desc
= DIV_ROUND_UP(period_len
, PDMA_MAX_DESC_BYTES
);
1089 nb_desc
*= DIV_ROUND_UP(len
, period_len
);
1090 sw_desc
= pxad_alloc_desc(chan
, nb_desc
+ 1);
1093 sw_desc
->cyclic
= true;
1096 phw_desc
= sw_desc
->hw_desc
;
1099 phw_desc
[0]->dsadr
= dsadr
? dsadr
: dma
;
1100 phw_desc
[0]->dtadr
= dtadr
? dtadr
: dma
;
1101 phw_desc
[0]->dcmd
= dcmd
;
1106 set_updater_desc(sw_desc
, flags
);
1108 return pxad_tx_prep(&chan
->vc
, &sw_desc
->vd
, flags
);
1111 static int pxad_config(struct dma_chan
*dchan
,
1112 struct dma_slave_config
*cfg
)
1114 struct pxad_chan
*chan
= to_pxad_chan(dchan
);
1123 static int pxad_terminate_all(struct dma_chan
*dchan
)
1125 struct pxad_chan
*chan
= to_pxad_chan(dchan
);
1126 struct pxad_device
*pdev
= to_pxad_dev(chan
->vc
.chan
.device
);
1127 struct virt_dma_desc
*vd
= NULL
;
1128 unsigned long flags
;
1129 struct pxad_phy
*phy
;
1132 dev_dbg(&chan
->vc
.chan
.dev
->device
,
1133 "%s(): vchan %p: terminate all\n", __func__
, &chan
->vc
);
1135 spin_lock_irqsave(&chan
->vc
.lock
, flags
);
1136 vchan_get_all_descriptors(&chan
->vc
, &head
);
1138 list_for_each_entry(vd
, &head
, node
) {
1139 dev_dbg(&chan
->vc
.chan
.dev
->device
,
1140 "%s(): cancelling txd %p[%x] (completed=%d)", __func__
,
1141 vd
, vd
->tx
.cookie
, is_desc_completed(vd
));
1146 phy_disable(chan
->phy
);
1147 pxad_free_phy(chan
);
1149 spin_lock(&pdev
->phy_lock
);
1151 spin_unlock(&pdev
->phy_lock
);
1153 spin_unlock_irqrestore(&chan
->vc
.lock
, flags
);
1154 vchan_dma_desc_free_list(&chan
->vc
, &head
);
1159 static unsigned int pxad_residue(struct pxad_chan
*chan
,
1160 dma_cookie_t cookie
)
1162 struct virt_dma_desc
*vd
= NULL
;
1163 struct pxad_desc_sw
*sw_desc
= NULL
;
1164 struct pxad_desc_hw
*hw_desc
= NULL
;
1165 u32 curr
, start
, len
, end
, residue
= 0;
1166 unsigned long flags
;
1167 bool passed
= false;
1171 * If the channel does not have a phy pointer anymore, it has already
1172 * been completed. Therefore, its residue is 0.
1177 spin_lock_irqsave(&chan
->vc
.lock
, flags
);
1179 vd
= vchan_find_desc(&chan
->vc
, cookie
);
1183 sw_desc
= to_pxad_sw_desc(vd
);
1184 if (sw_desc
->hw_desc
[0]->dcmd
& PXA_DCMD_INCSRCADDR
)
1185 curr
= phy_readl_relaxed(chan
->phy
, DSADR
);
1187 curr
= phy_readl_relaxed(chan
->phy
, DTADR
);
1190 * curr has to be actually read before checking descriptor
1191 * completion, so that a curr inside a status updater
1192 * descriptor implies the following test returns true, and
1193 * preventing reordering of curr load and the test.
1196 if (is_desc_completed(vd
))
1199 for (i
= 0; i
< sw_desc
->nb_desc
- 1; i
++) {
1200 hw_desc
= sw_desc
->hw_desc
[i
];
1201 if (sw_desc
->hw_desc
[0]->dcmd
& PXA_DCMD_INCSRCADDR
)
1202 start
= hw_desc
->dsadr
;
1204 start
= hw_desc
->dtadr
;
1205 len
= hw_desc
->dcmd
& PXA_DCMD_LENGTH
;
1209 * 'passed' will be latched once we found the descriptor
1210 * which lies inside the boundaries of the curr
1211 * pointer. All descriptors that occur in the list
1212 * _after_ we found that partially handled descriptor
1213 * are still to be processed and are hence added to the
1214 * residual bytes counter.
1219 } else if (curr
>= start
&& curr
<= end
) {
1220 residue
+= end
- curr
;
1225 residue
= sw_desc
->len
;
1228 spin_unlock_irqrestore(&chan
->vc
.lock
, flags
);
1229 dev_dbg(&chan
->vc
.chan
.dev
->device
,
1230 "%s(): txd %p[%x] sw_desc=%p: %d\n",
1231 __func__
, vd
, cookie
, sw_desc
, residue
);
1235 static enum dma_status
pxad_tx_status(struct dma_chan
*dchan
,
1236 dma_cookie_t cookie
,
1237 struct dma_tx_state
*txstate
)
1239 struct pxad_chan
*chan
= to_pxad_chan(dchan
);
1240 enum dma_status ret
;
1242 ret
= dma_cookie_status(dchan
, cookie
, txstate
);
1243 if (likely(txstate
&& (ret
!= DMA_ERROR
)))
1244 dma_set_residue(txstate
, pxad_residue(chan
, cookie
));
1249 static void pxad_free_channels(struct dma_device
*dmadev
)
1251 struct pxad_chan
*c
, *cn
;
1253 list_for_each_entry_safe(c
, cn
, &dmadev
->channels
,
1254 vc
.chan
.device_node
) {
1255 list_del(&c
->vc
.chan
.device_node
);
1256 tasklet_kill(&c
->vc
.task
);
1260 static int pxad_remove(struct platform_device
*op
)
1262 struct pxad_device
*pdev
= platform_get_drvdata(op
);
1264 pxad_cleanup_debugfs(pdev
);
1265 pxad_free_channels(&pdev
->slave
);
1266 dma_async_device_unregister(&pdev
->slave
);
1270 static int pxad_init_phys(struct platform_device
*op
,
1271 struct pxad_device
*pdev
,
1272 unsigned int nb_phy_chans
)
1274 int irq0
, irq
, nr_irq
= 0, i
, ret
;
1275 struct pxad_phy
*phy
;
1277 irq0
= platform_get_irq(op
, 0);
1281 pdev
->phys
= devm_kcalloc(&op
->dev
, nb_phy_chans
,
1282 sizeof(pdev
->phys
[0]), GFP_KERNEL
);
1286 for (i
= 0; i
< nb_phy_chans
; i
++)
1287 if (platform_get_irq(op
, i
) > 0)
1290 for (i
= 0; i
< nb_phy_chans
; i
++) {
1291 phy
= &pdev
->phys
[i
];
1292 phy
->base
= pdev
->base
;
1294 irq
= platform_get_irq(op
, i
);
1295 if ((nr_irq
> 1) && (irq
> 0))
1296 ret
= devm_request_irq(&op
->dev
, irq
,
1298 IRQF_SHARED
, "pxa-dma", phy
);
1299 if ((nr_irq
== 1) && (i
== 0))
1300 ret
= devm_request_irq(&op
->dev
, irq0
,
1302 IRQF_SHARED
, "pxa-dma", pdev
);
1304 dev_err(pdev
->slave
.dev
,
1305 "%s(): can't request irq %d:%d\n", __func__
,
1314 static const struct of_device_id
const pxad_dt_ids
[] = {
1315 { .compatible
= "marvell,pdma-1.0", },
1318 MODULE_DEVICE_TABLE(of
, pxad_dt_ids
);
1320 static struct dma_chan
*pxad_dma_xlate(struct of_phandle_args
*dma_spec
,
1321 struct of_dma
*ofdma
)
1323 struct pxad_device
*d
= ofdma
->of_dma_data
;
1324 struct dma_chan
*chan
;
1326 chan
= dma_get_any_slave_channel(&d
->slave
);
1330 to_pxad_chan(chan
)->drcmr
= dma_spec
->args
[0];
1331 to_pxad_chan(chan
)->prio
= dma_spec
->args
[1];
1336 static int pxad_init_dmadev(struct platform_device
*op
,
1337 struct pxad_device
*pdev
,
1338 unsigned int nr_phy_chans
)
1342 struct pxad_chan
*c
;
1344 pdev
->nr_chans
= nr_phy_chans
;
1345 INIT_LIST_HEAD(&pdev
->slave
.channels
);
1346 pdev
->slave
.device_alloc_chan_resources
= pxad_alloc_chan_resources
;
1347 pdev
->slave
.device_free_chan_resources
= pxad_free_chan_resources
;
1348 pdev
->slave
.device_tx_status
= pxad_tx_status
;
1349 pdev
->slave
.device_issue_pending
= pxad_issue_pending
;
1350 pdev
->slave
.device_config
= pxad_config
;
1351 pdev
->slave
.device_terminate_all
= pxad_terminate_all
;
1353 if (op
->dev
.coherent_dma_mask
)
1354 dma_set_mask(&op
->dev
, op
->dev
.coherent_dma_mask
);
1356 dma_set_mask(&op
->dev
, DMA_BIT_MASK(32));
1358 ret
= pxad_init_phys(op
, pdev
, nr_phy_chans
);
1362 for (i
= 0; i
< nr_phy_chans
; i
++) {
1363 c
= devm_kzalloc(&op
->dev
, sizeof(*c
), GFP_KERNEL
);
1366 c
->vc
.desc_free
= pxad_free_desc
;
1367 vchan_init(&c
->vc
, &pdev
->slave
);
1370 return dma_async_device_register(&pdev
->slave
);
1373 static int pxad_probe(struct platform_device
*op
)
1375 struct pxad_device
*pdev
;
1376 const struct of_device_id
*of_id
;
1377 struct mmp_dma_platdata
*pdata
= dev_get_platdata(&op
->dev
);
1378 struct resource
*iores
;
1379 int ret
, dma_channels
= 0;
1380 const enum dma_slave_buswidth widths
=
1381 DMA_SLAVE_BUSWIDTH_1_BYTE
| DMA_SLAVE_BUSWIDTH_2_BYTES
|
1382 DMA_SLAVE_BUSWIDTH_4_BYTES
;
1384 pdev
= devm_kzalloc(&op
->dev
, sizeof(*pdev
), GFP_KERNEL
);
1388 spin_lock_init(&pdev
->phy_lock
);
1390 iores
= platform_get_resource(op
, IORESOURCE_MEM
, 0);
1391 pdev
->base
= devm_ioremap_resource(&op
->dev
, iores
);
1392 if (IS_ERR(pdev
->base
))
1393 return PTR_ERR(pdev
->base
);
1395 of_id
= of_match_device(pxad_dt_ids
, &op
->dev
);
1397 of_property_read_u32(op
->dev
.of_node
, "#dma-channels",
1399 else if (pdata
&& pdata
->dma_channels
)
1400 dma_channels
= pdata
->dma_channels
;
1402 dma_channels
= 32; /* default 32 channel */
1404 dma_cap_set(DMA_SLAVE
, pdev
->slave
.cap_mask
);
1405 dma_cap_set(DMA_MEMCPY
, pdev
->slave
.cap_mask
);
1406 dma_cap_set(DMA_CYCLIC
, pdev
->slave
.cap_mask
);
1407 dma_cap_set(DMA_PRIVATE
, pdev
->slave
.cap_mask
);
1408 pdev
->slave
.device_prep_dma_memcpy
= pxad_prep_memcpy
;
1409 pdev
->slave
.device_prep_slave_sg
= pxad_prep_slave_sg
;
1410 pdev
->slave
.device_prep_dma_cyclic
= pxad_prep_dma_cyclic
;
1412 pdev
->slave
.copy_align
= PDMA_ALIGNMENT
;
1413 pdev
->slave
.src_addr_widths
= widths
;
1414 pdev
->slave
.dst_addr_widths
= widths
;
1415 pdev
->slave
.directions
= BIT(DMA_MEM_TO_DEV
) | BIT(DMA_DEV_TO_MEM
);
1416 pdev
->slave
.residue_granularity
= DMA_RESIDUE_GRANULARITY_DESCRIPTOR
;
1418 pdev
->slave
.dev
= &op
->dev
;
1419 ret
= pxad_init_dmadev(op
, pdev
, dma_channels
);
1421 dev_err(pdev
->slave
.dev
, "unable to register\n");
1425 if (op
->dev
.of_node
) {
1426 /* Device-tree DMA controller registration */
1427 ret
= of_dma_controller_register(op
->dev
.of_node
,
1428 pxad_dma_xlate
, pdev
);
1430 dev_err(pdev
->slave
.dev
,
1431 "of_dma_controller_register failed\n");
1436 platform_set_drvdata(op
, pdev
);
1437 pxad_init_debugfs(pdev
);
1438 dev_info(pdev
->slave
.dev
, "initialized %d channels\n", dma_channels
);
1442 static const struct platform_device_id pxad_id_table
[] = {
1447 static struct platform_driver pxad_driver
= {
1450 .of_match_table
= pxad_dt_ids
,
1452 .id_table
= pxad_id_table
,
1453 .probe
= pxad_probe
,
1454 .remove
= pxad_remove
,
1457 bool pxad_filter_fn(struct dma_chan
*chan
, void *param
)
1459 struct pxad_chan
*c
= to_pxad_chan(chan
);
1460 struct pxad_param
*p
= param
;
1462 if (chan
->device
->dev
->driver
!= &pxad_driver
.driver
)
1465 c
->drcmr
= p
->drcmr
;
1470 EXPORT_SYMBOL_GPL(pxad_filter_fn
);
1472 int pxad_toggle_reserved_channel(int legacy_channel
)
1474 if (legacy_unavailable
& (BIT(legacy_channel
)))
1476 legacy_reserved
^= BIT(legacy_channel
);
1479 EXPORT_SYMBOL_GPL(pxad_toggle_reserved_channel
);
1481 module_platform_driver(pxad_driver
);
1483 MODULE_DESCRIPTION("Marvell PXA Peripheral DMA Driver");
1484 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
1485 MODULE_LICENSE("GPL v2");