2 * Copyright (C) Freescale Semicondutor, Inc. 2007, 2008.
3 * Copyright (C) Semihalf 2009
4 * Copyright (C) Ilya Yanok, Emcraft Systems 2010
5 * Copyright (C) Alexander Popov, Promcontroller 2014
6 * Copyright (C) Mario Six, Guntermann & Drunck GmbH, 2016
8 * Written by Piotr Ziecik <kosmo@semihalf.com>. Hardware description
9 * (defines, structures and comments) was taken from MPC5121 DMA driver
10 * written by Hongjun Chen <hong-jun.chen@freescale.com>.
12 * Approved as OSADL project by a majority of OSADL members and funded
13 * by OSADL membership fees in 2009; for details see www.osadl.org.
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 2 of the License, or (at your option)
20 * This program is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25 * The full GNU General Public License is included in this distribution in the
26 * file called COPYING.
30 * MPC512x and MPC8308 DMA driver. It supports memory to memory data transfers
31 * (tested using dmatest module) and data transfers between memory and
32 * peripheral I/O memory by means of slave scatter/gather with these
34 * - chunked transfers (described by s/g lists with more than one item) are
35 * refused as long as proper support for scatter/gather is missing
36 * - transfers on MPC8308 always start from software as this SoC does not have
37 * external request lines for peripheral flow control
38 * - memory <-> I/O memory transfer chunks of sizes of 1, 2, 4, 16 (for
39 * MPC512x), and 32 bytes are supported, and, consequently, source
40 * addresses and destination addresses must be aligned accordingly;
41 * furthermore, for MPC512x SoCs, the transfer size must be aligned on
42 * (chunk size * maxburst)
45 #include <linux/module.h>
46 #include <linux/dmaengine.h>
47 #include <linux/dma-mapping.h>
48 #include <linux/interrupt.h>
50 #include <linux/slab.h>
51 #include <linux/of_address.h>
52 #include <linux/of_device.h>
53 #include <linux/of_irq.h>
54 #include <linux/of_dma.h>
55 #include <linux/of_platform.h>
57 #include <linux/random.h>
59 #include "dmaengine.h"
61 /* Number of DMA Transfer descriptors allocated per channel */
62 #define MPC_DMA_DESCRIPTORS 64
64 /* Macro definitions */
65 #define MPC_DMA_TCD_OFFSET 0x1000
68 * Maximum channel counts for individual hardware variants
69 * and the maximum channel count over all supported controllers,
70 * used for data structure size
72 #define MPC8308_DMACHAN_MAX 16
73 #define MPC512x_DMACHAN_MAX 64
74 #define MPC_DMA_CHANNELS 64
76 /* Arbitration mode of group and channel */
77 #define MPC_DMA_DMACR_EDCG (1 << 31)
78 #define MPC_DMA_DMACR_ERGA (1 << 3)
79 #define MPC_DMA_DMACR_ERCA (1 << 2)
82 #define MPC_DMA_DMAES_VLD (1 << 31)
83 #define MPC_DMA_DMAES_GPE (1 << 15)
84 #define MPC_DMA_DMAES_CPE (1 << 14)
85 #define MPC_DMA_DMAES_ERRCHN(err) \
87 #define MPC_DMA_DMAES_SAE (1 << 7)
88 #define MPC_DMA_DMAES_SOE (1 << 6)
89 #define MPC_DMA_DMAES_DAE (1 << 5)
90 #define MPC_DMA_DMAES_DOE (1 << 4)
91 #define MPC_DMA_DMAES_NCE (1 << 3)
92 #define MPC_DMA_DMAES_SGE (1 << 2)
93 #define MPC_DMA_DMAES_SBE (1 << 1)
94 #define MPC_DMA_DMAES_DBE (1 << 0)
96 #define MPC_DMA_DMAGPOR_SNOOP_ENABLE (1 << 6)
98 #define MPC_DMA_TSIZE_1 0x00
99 #define MPC_DMA_TSIZE_2 0x01
100 #define MPC_DMA_TSIZE_4 0x02
101 #define MPC_DMA_TSIZE_16 0x04
102 #define MPC_DMA_TSIZE_32 0x05
104 /* MPC5121 DMA engine registers */
105 struct __attribute__ ((__packed__
)) mpc_dma_regs
{
107 u32 dmacr
; /* DMA control register */
108 u32 dmaes
; /* DMA error status */
110 u32 dmaerqh
; /* DMA enable request high(channels 63~32) */
111 u32 dmaerql
; /* DMA enable request low(channels 31~0) */
112 u32 dmaeeih
; /* DMA enable error interrupt high(ch63~32) */
113 u32 dmaeeil
; /* DMA enable error interrupt low(ch31~0) */
115 u8 dmaserq
; /* DMA set enable request */
116 u8 dmacerq
; /* DMA clear enable request */
117 u8 dmaseei
; /* DMA set enable error interrupt */
118 u8 dmaceei
; /* DMA clear enable error interrupt */
120 u8 dmacint
; /* DMA clear interrupt request */
121 u8 dmacerr
; /* DMA clear error */
122 u8 dmassrt
; /* DMA set start bit */
123 u8 dmacdne
; /* DMA clear DONE status bit */
125 u32 dmainth
; /* DMA interrupt request high(ch63~32) */
126 u32 dmaintl
; /* DMA interrupt request low(ch31~0) */
127 u32 dmaerrh
; /* DMA error high(ch63~32) */
128 u32 dmaerrl
; /* DMA error low(ch31~0) */
130 u32 dmahrsh
; /* DMA hw request status high(ch63~32) */
131 u32 dmahrsl
; /* DMA hardware request status low(ch31~0) */
133 u32 dmaihsa
; /* DMA interrupt high select AXE(ch63~32) */
134 u32 dmagpor
; /* (General purpose register on MPC8308) */
136 u32 dmailsa
; /* DMA interrupt low select AXE(ch31~0) */
138 u32 reserve0
[48]; /* Reserved */
140 u8 dchpri
[MPC_DMA_CHANNELS
];
141 /* DMA channels(0~63) priority */
144 struct __attribute__ ((__packed__
)) mpc_dma_tcd
{
146 u32 saddr
; /* Source address */
148 u32 smod
:5; /* Source address modulo */
149 u32 ssize
:3; /* Source data transfer size */
150 u32 dmod
:5; /* Destination address modulo */
151 u32 dsize
:3; /* Destination data transfer size */
152 u32 soff
:16; /* Signed source address offset */
155 u32 nbytes
; /* Inner "minor" byte count */
156 u32 slast
; /* Last source address adjustment */
157 u32 daddr
; /* Destination address */
160 u32 citer_elink
:1; /* Enable channel-to-channel linking on
161 * minor loop complete
163 u32 citer_linkch
:6; /* Link channel for minor loop complete */
164 u32 citer
:9; /* Current "major" iteration count */
165 u32 doff
:16; /* Signed destination address offset */
168 u32 dlast_sga
; /* Last Destination address adjustment/scatter
173 u32 biter_elink
:1; /* Enable channel-to-channel linking on major
177 u32 biter
:9; /* Beginning "major" iteration count */
178 u32 bwc
:2; /* Bandwidth control */
179 u32 major_linkch
:6; /* Link channel number */
180 u32 done
:1; /* Channel done */
181 u32 active
:1; /* Channel active */
182 u32 major_elink
:1; /* Enable channel-to-channel linking on major
185 u32 e_sg
:1; /* Enable scatter/gather processing */
186 u32 d_req
:1; /* Disable request */
187 u32 int_half
:1; /* Enable an interrupt when major counter is
190 u32 int_maj
:1; /* Enable an interrupt when major iteration
193 u32 start
:1; /* Channel start */
196 struct mpc_dma_desc
{
197 struct dma_async_tx_descriptor desc
;
198 struct mpc_dma_tcd
*tcd
;
199 dma_addr_t tcd_paddr
;
201 struct list_head node
;
202 int will_access_peripheral
;
205 struct mpc_dma_chan
{
206 struct dma_chan chan
;
207 struct list_head free
;
208 struct list_head prepared
;
209 struct list_head queued
;
210 struct list_head active
;
211 struct list_head completed
;
212 struct mpc_dma_tcd
*tcd
;
213 dma_addr_t tcd_paddr
;
215 /* Settings for access to peripheral FIFO */
216 dma_addr_t src_per_paddr
;
219 dma_addr_t dst_per_paddr
;
223 /* Lock for this structure */
228 struct dma_device dma
;
229 struct tasklet_struct tasklet
;
230 struct mpc_dma_chan channels
[MPC_DMA_CHANNELS
];
231 struct mpc_dma_regs __iomem
*regs
;
232 struct mpc_dma_tcd __iomem
*tcd
;
238 /* Lock for error_status field in this structure */
239 spinlock_t error_status_lock
;
242 #define DRV_NAME "mpc512x_dma"
244 /* Convert struct dma_chan to struct mpc_dma_chan */
245 static inline struct mpc_dma_chan
*dma_chan_to_mpc_dma_chan(struct dma_chan
*c
)
247 return container_of(c
, struct mpc_dma_chan
, chan
);
250 /* Convert struct dma_chan to struct mpc_dma */
251 static inline struct mpc_dma
*dma_chan_to_mpc_dma(struct dma_chan
*c
)
253 struct mpc_dma_chan
*mchan
= dma_chan_to_mpc_dma_chan(c
);
255 return container_of(mchan
, struct mpc_dma
, channels
[c
->chan_id
]);
259 * Execute all queued DMA descriptors.
261 * Following requirements must be met while calling mpc_dma_execute():
262 * a) mchan->lock is acquired,
263 * b) mchan->active list is empty,
264 * c) mchan->queued list contains at least one entry.
266 static void mpc_dma_execute(struct mpc_dma_chan
*mchan
)
268 struct mpc_dma
*mdma
= dma_chan_to_mpc_dma(&mchan
->chan
);
269 struct mpc_dma_desc
*first
= NULL
;
270 struct mpc_dma_desc
*prev
= NULL
;
271 struct mpc_dma_desc
*mdesc
;
272 int cid
= mchan
->chan
.chan_id
;
274 while (!list_empty(&mchan
->queued
)) {
275 mdesc
= list_first_entry(&mchan
->queued
,
276 struct mpc_dma_desc
, node
);
278 * Grab either several mem-to-mem transfer descriptors
279 * or one peripheral transfer descriptor,
280 * don't mix mem-to-mem and peripheral transfer descriptors
281 * within the same 'active' list.
283 if (mdesc
->will_access_peripheral
) {
284 if (list_empty(&mchan
->active
))
285 list_move_tail(&mdesc
->node
, &mchan
->active
);
288 list_move_tail(&mdesc
->node
, &mchan
->active
);
292 /* Chain descriptors into one transaction */
293 list_for_each_entry(mdesc
, &mchan
->active
, node
) {
302 prev
->tcd
->dlast_sga
= mdesc
->tcd_paddr
;
304 mdesc
->tcd
->start
= 1;
309 prev
->tcd
->int_maj
= 1;
311 /* Send first descriptor in chain into hardware */
312 memcpy_toio(&mdma
->tcd
[cid
], first
->tcd
, sizeof(struct mpc_dma_tcd
));
315 mdma
->tcd
[cid
].e_sg
= 1;
317 if (mdma
->is_mpc8308
) {
318 /* MPC8308, no request lines, software initiated start */
319 out_8(&mdma
->regs
->dmassrt
, cid
);
320 } else if (first
->will_access_peripheral
) {
321 /* Peripherals involved, start by external request signal */
322 out_8(&mdma
->regs
->dmaserq
, cid
);
324 /* Memory to memory transfer, software initiated start */
325 out_8(&mdma
->regs
->dmassrt
, cid
);
329 /* Handle interrupt on one half of DMA controller (32 channels) */
330 static void mpc_dma_irq_process(struct mpc_dma
*mdma
, u32 is
, u32 es
, int off
)
332 struct mpc_dma_chan
*mchan
;
333 struct mpc_dma_desc
*mdesc
;
334 u32 status
= is
| es
;
337 while ((ch
= fls(status
) - 1) >= 0) {
338 status
&= ~(1 << ch
);
339 mchan
= &mdma
->channels
[ch
+ off
];
341 spin_lock(&mchan
->lock
);
343 out_8(&mdma
->regs
->dmacint
, ch
+ off
);
344 out_8(&mdma
->regs
->dmacerr
, ch
+ off
);
346 /* Check error status */
348 list_for_each_entry(mdesc
, &mchan
->active
, node
)
351 /* Execute queued descriptors */
352 list_splice_tail_init(&mchan
->active
, &mchan
->completed
);
353 if (!list_empty(&mchan
->queued
))
354 mpc_dma_execute(mchan
);
356 spin_unlock(&mchan
->lock
);
360 /* Interrupt handler */
361 static irqreturn_t
mpc_dma_irq(int irq
, void *data
)
363 struct mpc_dma
*mdma
= data
;
366 /* Save error status register */
367 es
= in_be32(&mdma
->regs
->dmaes
);
368 spin_lock(&mdma
->error_status_lock
);
369 if ((es
& MPC_DMA_DMAES_VLD
) && mdma
->error_status
== 0)
370 mdma
->error_status
= es
;
371 spin_unlock(&mdma
->error_status_lock
);
373 /* Handle interrupt on each channel */
374 if (mdma
->dma
.chancnt
> 32) {
375 mpc_dma_irq_process(mdma
, in_be32(&mdma
->regs
->dmainth
),
376 in_be32(&mdma
->regs
->dmaerrh
), 32);
378 mpc_dma_irq_process(mdma
, in_be32(&mdma
->regs
->dmaintl
),
379 in_be32(&mdma
->regs
->dmaerrl
), 0);
381 /* Schedule tasklet */
382 tasklet_schedule(&mdma
->tasklet
);
387 /* process completed descriptors */
388 static void mpc_dma_process_completed(struct mpc_dma
*mdma
)
390 dma_cookie_t last_cookie
= 0;
391 struct mpc_dma_chan
*mchan
;
392 struct mpc_dma_desc
*mdesc
;
393 struct dma_async_tx_descriptor
*desc
;
398 for (i
= 0; i
< mdma
->dma
.chancnt
; i
++) {
399 mchan
= &mdma
->channels
[i
];
401 /* Get all completed descriptors */
402 spin_lock_irqsave(&mchan
->lock
, flags
);
403 if (!list_empty(&mchan
->completed
))
404 list_splice_tail_init(&mchan
->completed
, &list
);
405 spin_unlock_irqrestore(&mchan
->lock
, flags
);
407 if (list_empty(&list
))
410 /* Execute callbacks and run dependencies */
411 list_for_each_entry(mdesc
, &list
, node
) {
414 dmaengine_desc_get_callback_invoke(desc
, NULL
);
416 last_cookie
= desc
->cookie
;
417 dma_run_dependencies(desc
);
420 /* Free descriptors */
421 spin_lock_irqsave(&mchan
->lock
, flags
);
422 list_splice_tail_init(&list
, &mchan
->free
);
423 mchan
->chan
.completed_cookie
= last_cookie
;
424 spin_unlock_irqrestore(&mchan
->lock
, flags
);
429 static void mpc_dma_tasklet(unsigned long data
)
431 struct mpc_dma
*mdma
= (void *)data
;
435 spin_lock_irqsave(&mdma
->error_status_lock
, flags
);
436 es
= mdma
->error_status
;
437 mdma
->error_status
= 0;
438 spin_unlock_irqrestore(&mdma
->error_status_lock
, flags
);
440 /* Print nice error report */
442 dev_err(mdma
->dma
.dev
,
443 "Hardware reported following error(s) on channel %u:\n",
444 MPC_DMA_DMAES_ERRCHN(es
));
446 if (es
& MPC_DMA_DMAES_GPE
)
447 dev_err(mdma
->dma
.dev
, "- Group Priority Error\n");
448 if (es
& MPC_DMA_DMAES_CPE
)
449 dev_err(mdma
->dma
.dev
, "- Channel Priority Error\n");
450 if (es
& MPC_DMA_DMAES_SAE
)
451 dev_err(mdma
->dma
.dev
, "- Source Address Error\n");
452 if (es
& MPC_DMA_DMAES_SOE
)
453 dev_err(mdma
->dma
.dev
, "- Source Offset Configuration Error\n");
454 if (es
& MPC_DMA_DMAES_DAE
)
455 dev_err(mdma
->dma
.dev
, "- Destination Address Error\n");
456 if (es
& MPC_DMA_DMAES_DOE
)
457 dev_err(mdma
->dma
.dev
, "- Destination Offset Configuration Error\n");
458 if (es
& MPC_DMA_DMAES_NCE
)
459 dev_err(mdma
->dma
.dev
, "- NBytes/Citter Configuration Error\n");
460 if (es
& MPC_DMA_DMAES_SGE
)
461 dev_err(mdma
->dma
.dev
, "- Scatter/Gather Configuration Error\n");
462 if (es
& MPC_DMA_DMAES_SBE
)
463 dev_err(mdma
->dma
.dev
, "- Source Bus Error\n");
464 if (es
& MPC_DMA_DMAES_DBE
)
465 dev_err(mdma
->dma
.dev
, "- Destination Bus Error\n");
468 mpc_dma_process_completed(mdma
);
471 /* Submit descriptor to hardware */
472 static dma_cookie_t
mpc_dma_tx_submit(struct dma_async_tx_descriptor
*txd
)
474 struct mpc_dma_chan
*mchan
= dma_chan_to_mpc_dma_chan(txd
->chan
);
475 struct mpc_dma_desc
*mdesc
;
479 mdesc
= container_of(txd
, struct mpc_dma_desc
, desc
);
481 spin_lock_irqsave(&mchan
->lock
, flags
);
483 /* Move descriptor to queue */
484 list_move_tail(&mdesc
->node
, &mchan
->queued
);
486 /* If channel is idle, execute all queued descriptors */
487 if (list_empty(&mchan
->active
))
488 mpc_dma_execute(mchan
);
491 cookie
= dma_cookie_assign(txd
);
492 spin_unlock_irqrestore(&mchan
->lock
, flags
);
497 /* Alloc channel resources */
498 static int mpc_dma_alloc_chan_resources(struct dma_chan
*chan
)
500 struct mpc_dma
*mdma
= dma_chan_to_mpc_dma(chan
);
501 struct mpc_dma_chan
*mchan
= dma_chan_to_mpc_dma_chan(chan
);
502 struct mpc_dma_desc
*mdesc
;
503 struct mpc_dma_tcd
*tcd
;
504 dma_addr_t tcd_paddr
;
509 /* Alloc DMA memory for Transfer Control Descriptors */
510 tcd
= dma_alloc_coherent(mdma
->dma
.dev
,
511 MPC_DMA_DESCRIPTORS
* sizeof(struct mpc_dma_tcd
),
512 &tcd_paddr
, GFP_KERNEL
);
516 /* Alloc descriptors for this channel */
517 for (i
= 0; i
< MPC_DMA_DESCRIPTORS
; i
++) {
518 mdesc
= kzalloc(sizeof(struct mpc_dma_desc
), GFP_KERNEL
);
520 dev_notice(mdma
->dma
.dev
,
521 "Memory allocation error. Allocated only %u descriptors\n", i
);
525 dma_async_tx_descriptor_init(&mdesc
->desc
, chan
);
526 mdesc
->desc
.flags
= DMA_CTRL_ACK
;
527 mdesc
->desc
.tx_submit
= mpc_dma_tx_submit
;
529 mdesc
->tcd
= &tcd
[i
];
530 mdesc
->tcd_paddr
= tcd_paddr
+ (i
* sizeof(struct mpc_dma_tcd
));
532 list_add_tail(&mdesc
->node
, &descs
);
535 /* Return error only if no descriptors were allocated */
537 dma_free_coherent(mdma
->dma
.dev
,
538 MPC_DMA_DESCRIPTORS
* sizeof(struct mpc_dma_tcd
),
543 spin_lock_irqsave(&mchan
->lock
, flags
);
545 mchan
->tcd_paddr
= tcd_paddr
;
546 list_splice_tail_init(&descs
, &mchan
->free
);
547 spin_unlock_irqrestore(&mchan
->lock
, flags
);
549 /* Enable Error Interrupt */
550 out_8(&mdma
->regs
->dmaseei
, chan
->chan_id
);
555 /* Free channel resources */
556 static void mpc_dma_free_chan_resources(struct dma_chan
*chan
)
558 struct mpc_dma
*mdma
= dma_chan_to_mpc_dma(chan
);
559 struct mpc_dma_chan
*mchan
= dma_chan_to_mpc_dma_chan(chan
);
560 struct mpc_dma_desc
*mdesc
, *tmp
;
561 struct mpc_dma_tcd
*tcd
;
562 dma_addr_t tcd_paddr
;
566 spin_lock_irqsave(&mchan
->lock
, flags
);
568 /* Channel must be idle */
569 BUG_ON(!list_empty(&mchan
->prepared
));
570 BUG_ON(!list_empty(&mchan
->queued
));
571 BUG_ON(!list_empty(&mchan
->active
));
572 BUG_ON(!list_empty(&mchan
->completed
));
575 list_splice_tail_init(&mchan
->free
, &descs
);
577 tcd_paddr
= mchan
->tcd_paddr
;
579 spin_unlock_irqrestore(&mchan
->lock
, flags
);
581 /* Free DMA memory used by descriptors */
582 dma_free_coherent(mdma
->dma
.dev
,
583 MPC_DMA_DESCRIPTORS
* sizeof(struct mpc_dma_tcd
),
586 /* Free descriptors */
587 list_for_each_entry_safe(mdesc
, tmp
, &descs
, node
)
590 /* Disable Error Interrupt */
591 out_8(&mdma
->regs
->dmaceei
, chan
->chan_id
);
594 /* Send all pending descriptor to hardware */
595 static void mpc_dma_issue_pending(struct dma_chan
*chan
)
598 * We are posting descriptors to the hardware as soon as
599 * they are ready, so this function does nothing.
603 /* Check request completion status */
604 static enum dma_status
605 mpc_dma_tx_status(struct dma_chan
*chan
, dma_cookie_t cookie
,
606 struct dma_tx_state
*txstate
)
608 return dma_cookie_status(chan
, cookie
, txstate
);
611 /* Prepare descriptor for memory to memory copy */
612 static struct dma_async_tx_descriptor
*
613 mpc_dma_prep_memcpy(struct dma_chan
*chan
, dma_addr_t dst
, dma_addr_t src
,
614 size_t len
, unsigned long flags
)
616 struct mpc_dma
*mdma
= dma_chan_to_mpc_dma(chan
);
617 struct mpc_dma_chan
*mchan
= dma_chan_to_mpc_dma_chan(chan
);
618 struct mpc_dma_desc
*mdesc
= NULL
;
619 struct mpc_dma_tcd
*tcd
;
620 unsigned long iflags
;
622 /* Get free descriptor */
623 spin_lock_irqsave(&mchan
->lock
, iflags
);
624 if (!list_empty(&mchan
->free
)) {
625 mdesc
= list_first_entry(&mchan
->free
, struct mpc_dma_desc
,
627 list_del(&mdesc
->node
);
629 spin_unlock_irqrestore(&mchan
->lock
, iflags
);
632 /* try to free completed descriptors */
633 mpc_dma_process_completed(mdma
);
638 mdesc
->will_access_peripheral
= 0;
641 /* Prepare Transfer Control Descriptor for this transaction */
642 memset(tcd
, 0, sizeof(struct mpc_dma_tcd
));
644 if (IS_ALIGNED(src
| dst
| len
, 32)) {
645 tcd
->ssize
= MPC_DMA_TSIZE_32
;
646 tcd
->dsize
= MPC_DMA_TSIZE_32
;
649 } else if (!mdma
->is_mpc8308
&& IS_ALIGNED(src
| dst
| len
, 16)) {
650 /* MPC8308 doesn't support 16 byte transfers */
651 tcd
->ssize
= MPC_DMA_TSIZE_16
;
652 tcd
->dsize
= MPC_DMA_TSIZE_16
;
655 } else if (IS_ALIGNED(src
| dst
| len
, 4)) {
656 tcd
->ssize
= MPC_DMA_TSIZE_4
;
657 tcd
->dsize
= MPC_DMA_TSIZE_4
;
660 } else if (IS_ALIGNED(src
| dst
| len
, 2)) {
661 tcd
->ssize
= MPC_DMA_TSIZE_2
;
662 tcd
->dsize
= MPC_DMA_TSIZE_2
;
666 tcd
->ssize
= MPC_DMA_TSIZE_1
;
667 tcd
->dsize
= MPC_DMA_TSIZE_1
;
678 /* Place descriptor in prepared list */
679 spin_lock_irqsave(&mchan
->lock
, iflags
);
680 list_add_tail(&mdesc
->node
, &mchan
->prepared
);
681 spin_unlock_irqrestore(&mchan
->lock
, iflags
);
686 inline u8
buswidth_to_dmatsize(u8 buswidth
)
690 for (res
= 0; buswidth
> 1; buswidth
/= 2)
695 static struct dma_async_tx_descriptor
*
696 mpc_dma_prep_slave_sg(struct dma_chan
*chan
, struct scatterlist
*sgl
,
697 unsigned int sg_len
, enum dma_transfer_direction direction
,
698 unsigned long flags
, void *context
)
700 struct mpc_dma
*mdma
= dma_chan_to_mpc_dma(chan
);
701 struct mpc_dma_chan
*mchan
= dma_chan_to_mpc_dma_chan(chan
);
702 struct mpc_dma_desc
*mdesc
= NULL
;
703 dma_addr_t per_paddr
;
705 struct mpc_dma_tcd
*tcd
;
706 unsigned long iflags
;
707 struct scatterlist
*sg
;
711 /* Currently there is no proper support for scatter/gather */
715 if (!is_slave_direction(direction
))
718 for_each_sg(sgl
, sg
, sg_len
, i
) {
719 spin_lock_irqsave(&mchan
->lock
, iflags
);
721 mdesc
= list_first_entry(&mchan
->free
,
722 struct mpc_dma_desc
, node
);
724 spin_unlock_irqrestore(&mchan
->lock
, iflags
);
725 /* Try to free completed descriptors */
726 mpc_dma_process_completed(mdma
);
730 list_del(&mdesc
->node
);
732 if (direction
== DMA_DEV_TO_MEM
) {
733 per_paddr
= mchan
->src_per_paddr
;
734 tcd_nunits
= mchan
->src_tcd_nunits
;
736 per_paddr
= mchan
->dst_per_paddr
;
737 tcd_nunits
= mchan
->dst_tcd_nunits
;
740 spin_unlock_irqrestore(&mchan
->lock
, iflags
);
742 if (per_paddr
== 0 || tcd_nunits
== 0)
746 mdesc
->will_access_peripheral
= 1;
748 /* Prepare Transfer Control Descriptor for this transaction */
751 memset(tcd
, 0, sizeof(struct mpc_dma_tcd
));
753 if (direction
== DMA_DEV_TO_MEM
) {
754 tcd
->saddr
= per_paddr
;
755 tcd
->daddr
= sg_dma_address(sg
);
757 if (!IS_ALIGNED(sg_dma_address(sg
), mchan
->dwidth
))
761 tcd
->doff
= mchan
->dwidth
;
763 tcd
->saddr
= sg_dma_address(sg
);
764 tcd
->daddr
= per_paddr
;
766 if (!IS_ALIGNED(sg_dma_address(sg
), mchan
->swidth
))
769 tcd
->soff
= mchan
->swidth
;
773 tcd
->ssize
= buswidth_to_dmatsize(mchan
->swidth
);
774 tcd
->dsize
= buswidth_to_dmatsize(mchan
->dwidth
);
776 if (mdma
->is_mpc8308
) {
777 tcd
->nbytes
= sg_dma_len(sg
);
778 if (!IS_ALIGNED(tcd
->nbytes
, mchan
->swidth
))
781 /* No major loops for MPC8303 */
785 len
= sg_dma_len(sg
);
786 tcd
->nbytes
= tcd_nunits
* tcd
->ssize
;
787 if (!IS_ALIGNED(len
, tcd
->nbytes
))
790 iter
= len
/ tcd
->nbytes
;
791 if (iter
>= 1 << 15) {
795 /* citer_linkch contains the high bits of iter */
796 tcd
->biter
= iter
& 0x1ff;
797 tcd
->biter_linkch
= iter
>> 9;
798 tcd
->citer
= tcd
->biter
;
799 tcd
->citer_linkch
= tcd
->biter_linkch
;
805 /* Place descriptor in prepared list */
806 spin_lock_irqsave(&mchan
->lock
, iflags
);
807 list_add_tail(&mdesc
->node
, &mchan
->prepared
);
808 spin_unlock_irqrestore(&mchan
->lock
, iflags
);
814 /* Put the descriptor back */
815 spin_lock_irqsave(&mchan
->lock
, iflags
);
816 list_add_tail(&mdesc
->node
, &mchan
->free
);
817 spin_unlock_irqrestore(&mchan
->lock
, iflags
);
822 inline bool is_buswidth_valid(u8 buswidth
, bool is_mpc8308
)
840 static int mpc_dma_device_config(struct dma_chan
*chan
,
841 struct dma_slave_config
*cfg
)
843 struct mpc_dma_chan
*mchan
= dma_chan_to_mpc_dma_chan(chan
);
844 struct mpc_dma
*mdma
= dma_chan_to_mpc_dma(&mchan
->chan
);
848 * Software constraints:
849 * - only transfers between a peripheral device and memory are
851 * - transfer chunk sizes of 1, 2, 4, 16 (for MPC512x), and 32 bytes
852 * are supported, and, consequently, source addresses and
853 * destination addresses; must be aligned accordingly; furthermore,
854 * for MPC512x SoCs, the transfer size must be aligned on (chunk
856 * - during the transfer, the RAM address is incremented by the size
858 * - the peripheral port's address is constant during the transfer.
861 if (!IS_ALIGNED(cfg
->src_addr
, cfg
->src_addr_width
) ||
862 !IS_ALIGNED(cfg
->dst_addr
, cfg
->dst_addr_width
)) {
866 if (!is_buswidth_valid(cfg
->src_addr_width
, mdma
->is_mpc8308
) ||
867 !is_buswidth_valid(cfg
->dst_addr_width
, mdma
->is_mpc8308
))
870 spin_lock_irqsave(&mchan
->lock
, flags
);
872 mchan
->src_per_paddr
= cfg
->src_addr
;
873 mchan
->src_tcd_nunits
= cfg
->src_maxburst
;
874 mchan
->swidth
= cfg
->src_addr_width
;
875 mchan
->dst_per_paddr
= cfg
->dst_addr
;
876 mchan
->dst_tcd_nunits
= cfg
->dst_maxburst
;
877 mchan
->dwidth
= cfg
->dst_addr_width
;
880 if (mchan
->src_tcd_nunits
== 0)
881 mchan
->src_tcd_nunits
= 1;
882 if (mchan
->dst_tcd_nunits
== 0)
883 mchan
->dst_tcd_nunits
= 1;
885 spin_unlock_irqrestore(&mchan
->lock
, flags
);
890 static int mpc_dma_device_terminate_all(struct dma_chan
*chan
)
892 struct mpc_dma_chan
*mchan
= dma_chan_to_mpc_dma_chan(chan
);
893 struct mpc_dma
*mdma
= dma_chan_to_mpc_dma(chan
);
896 /* Disable channel requests */
897 spin_lock_irqsave(&mchan
->lock
, flags
);
899 out_8(&mdma
->regs
->dmacerq
, chan
->chan_id
);
900 list_splice_tail_init(&mchan
->prepared
, &mchan
->free
);
901 list_splice_tail_init(&mchan
->queued
, &mchan
->free
);
902 list_splice_tail_init(&mchan
->active
, &mchan
->free
);
904 spin_unlock_irqrestore(&mchan
->lock
, flags
);
909 static int mpc_dma_probe(struct platform_device
*op
)
911 struct device_node
*dn
= op
->dev
.of_node
;
912 struct device
*dev
= &op
->dev
;
913 struct dma_device
*dma
;
914 struct mpc_dma
*mdma
;
915 struct mpc_dma_chan
*mchan
;
917 ulong regs_start
, regs_size
;
921 mdma
= devm_kzalloc(dev
, sizeof(struct mpc_dma
), GFP_KERNEL
);
927 mdma
->irq
= irq_of_parse_and_map(dn
, 0);
929 dev_err(dev
, "Error mapping IRQ!\n");
934 if (of_device_is_compatible(dn
, "fsl,mpc8308-dma")) {
935 mdma
->is_mpc8308
= 1;
936 mdma
->irq2
= irq_of_parse_and_map(dn
, 1);
938 dev_err(dev
, "Error mapping IRQ!\n");
944 retval
= of_address_to_resource(dn
, 0, &res
);
946 dev_err(dev
, "Error parsing memory region!\n");
950 regs_start
= res
.start
;
951 regs_size
= resource_size(&res
);
953 if (!devm_request_mem_region(dev
, regs_start
, regs_size
, DRV_NAME
)) {
954 dev_err(dev
, "Error requesting memory region!\n");
959 mdma
->regs
= devm_ioremap(dev
, regs_start
, regs_size
);
961 dev_err(dev
, "Error mapping memory region!\n");
966 mdma
->tcd
= (struct mpc_dma_tcd
*)((u8
*)(mdma
->regs
)
967 + MPC_DMA_TCD_OFFSET
);
969 retval
= request_irq(mdma
->irq
, &mpc_dma_irq
, 0, DRV_NAME
, mdma
);
971 dev_err(dev
, "Error requesting IRQ!\n");
976 if (mdma
->is_mpc8308
) {
977 retval
= request_irq(mdma
->irq2
, &mpc_dma_irq
, 0,
980 dev_err(dev
, "Error requesting IRQ2!\n");
986 spin_lock_init(&mdma
->error_status_lock
);
990 dma
->device_alloc_chan_resources
= mpc_dma_alloc_chan_resources
;
991 dma
->device_free_chan_resources
= mpc_dma_free_chan_resources
;
992 dma
->device_issue_pending
= mpc_dma_issue_pending
;
993 dma
->device_tx_status
= mpc_dma_tx_status
;
994 dma
->device_prep_dma_memcpy
= mpc_dma_prep_memcpy
;
995 dma
->device_prep_slave_sg
= mpc_dma_prep_slave_sg
;
996 dma
->device_config
= mpc_dma_device_config
;
997 dma
->device_terminate_all
= mpc_dma_device_terminate_all
;
999 INIT_LIST_HEAD(&dma
->channels
);
1000 dma_cap_set(DMA_MEMCPY
, dma
->cap_mask
);
1001 dma_cap_set(DMA_SLAVE
, dma
->cap_mask
);
1003 if (mdma
->is_mpc8308
)
1004 chancnt
= MPC8308_DMACHAN_MAX
;
1006 chancnt
= MPC512x_DMACHAN_MAX
;
1008 for (i
= 0; i
< chancnt
; i
++) {
1009 mchan
= &mdma
->channels
[i
];
1011 mchan
->chan
.device
= dma
;
1012 dma_cookie_init(&mchan
->chan
);
1014 INIT_LIST_HEAD(&mchan
->free
);
1015 INIT_LIST_HEAD(&mchan
->prepared
);
1016 INIT_LIST_HEAD(&mchan
->queued
);
1017 INIT_LIST_HEAD(&mchan
->active
);
1018 INIT_LIST_HEAD(&mchan
->completed
);
1020 spin_lock_init(&mchan
->lock
);
1021 list_add_tail(&mchan
->chan
.device_node
, &dma
->channels
);
1024 tasklet_init(&mdma
->tasklet
, mpc_dma_tasklet
, (unsigned long)mdma
);
1027 * Configure DMA Engine:
1029 * - Round-robin group arbitration,
1030 * - Round-robin channel arbitration.
1032 if (mdma
->is_mpc8308
) {
1033 /* MPC8308 has 16 channels and lacks some registers */
1034 out_be32(&mdma
->regs
->dmacr
, MPC_DMA_DMACR_ERCA
);
1036 /* enable snooping */
1037 out_be32(&mdma
->regs
->dmagpor
, MPC_DMA_DMAGPOR_SNOOP_ENABLE
);
1038 /* Disable error interrupts */
1039 out_be32(&mdma
->regs
->dmaeeil
, 0);
1041 /* Clear interrupts status */
1042 out_be32(&mdma
->regs
->dmaintl
, 0xFFFF);
1043 out_be32(&mdma
->regs
->dmaerrl
, 0xFFFF);
1045 out_be32(&mdma
->regs
->dmacr
, MPC_DMA_DMACR_EDCG
|
1046 MPC_DMA_DMACR_ERGA
|
1047 MPC_DMA_DMACR_ERCA
);
1049 /* Disable hardware DMA requests */
1050 out_be32(&mdma
->regs
->dmaerqh
, 0);
1051 out_be32(&mdma
->regs
->dmaerql
, 0);
1053 /* Disable error interrupts */
1054 out_be32(&mdma
->regs
->dmaeeih
, 0);
1055 out_be32(&mdma
->regs
->dmaeeil
, 0);
1057 /* Clear interrupts status */
1058 out_be32(&mdma
->regs
->dmainth
, 0xFFFFFFFF);
1059 out_be32(&mdma
->regs
->dmaintl
, 0xFFFFFFFF);
1060 out_be32(&mdma
->regs
->dmaerrh
, 0xFFFFFFFF);
1061 out_be32(&mdma
->regs
->dmaerrl
, 0xFFFFFFFF);
1063 /* Route interrupts to IPIC */
1064 out_be32(&mdma
->regs
->dmaihsa
, 0);
1065 out_be32(&mdma
->regs
->dmailsa
, 0);
1068 /* Register DMA engine */
1069 dev_set_drvdata(dev
, mdma
);
1070 retval
= dma_async_device_register(dma
);
1074 /* Register with OF helpers for DMA lookups (nonfatal) */
1076 retval
= of_dma_controller_register(dev
->of_node
,
1077 of_dma_xlate_by_chan_id
, mdma
);
1079 dev_warn(dev
, "Could not register for OF lookup\n");
1085 if (mdma
->is_mpc8308
)
1086 free_irq(mdma
->irq2
, mdma
);
1088 free_irq(mdma
->irq
, mdma
);
1090 if (mdma
->is_mpc8308
)
1091 irq_dispose_mapping(mdma
->irq2
);
1093 irq_dispose_mapping(mdma
->irq
);
1098 static int mpc_dma_remove(struct platform_device
*op
)
1100 struct device
*dev
= &op
->dev
;
1101 struct mpc_dma
*mdma
= dev_get_drvdata(dev
);
1104 of_dma_controller_free(dev
->of_node
);
1105 dma_async_device_unregister(&mdma
->dma
);
1106 if (mdma
->is_mpc8308
) {
1107 free_irq(mdma
->irq2
, mdma
);
1108 irq_dispose_mapping(mdma
->irq2
);
1110 free_irq(mdma
->irq
, mdma
);
1111 irq_dispose_mapping(mdma
->irq
);
1112 tasklet_kill(&mdma
->tasklet
);
1117 static const struct of_device_id mpc_dma_match
[] = {
1118 { .compatible
= "fsl,mpc5121-dma", },
1119 { .compatible
= "fsl,mpc8308-dma", },
1122 MODULE_DEVICE_TABLE(of
, mpc_dma_match
);
1124 static struct platform_driver mpc_dma_driver
= {
1125 .probe
= mpc_dma_probe
,
1126 .remove
= mpc_dma_remove
,
1129 .of_match_table
= mpc_dma_match
,
1133 module_platform_driver(mpc_dma_driver
);
1135 MODULE_LICENSE("GPL");
1136 MODULE_AUTHOR("Piotr Ziecik <kosmo@semihalf.com>");