]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/dma/mpc512x_dma.c
powerpc/512x: add MPC8308 dma support
[karo-tx-linux.git] / drivers / dma / mpc512x_dma.c
1 /*
2  * Copyright (C) Freescale Semicondutor, Inc. 2007, 2008.
3  * Copyright (C) Semihalf 2009
4  * Copyright (C) Ilya Yanok, Emcraft Systems 2010
5  *
6  * Written by Piotr Ziecik <kosmo@semihalf.com>. Hardware description
7  * (defines, structures and comments) was taken from MPC5121 DMA driver
8  * written by Hongjun Chen <hong-jun.chen@freescale.com>.
9  *
10  * Approved as OSADL project by a majority of OSADL members and funded
11  * by OSADL membership fees in 2009;  for details see www.osadl.org.
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 2 of the License, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
21  * more details.
22  *
23  * You should have received a copy of the GNU General Public License along with
24  * this program; if not, write to the Free Software Foundation, Inc., 59
25  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  *
27  * The full GNU General Public License is included in this distribution in the
28  * file called COPYING.
29  */
30
31 /*
32  * This is initial version of MPC5121 DMA driver. Only memory to memory
33  * transfers are supported (tested using dmatest module).
34  */
35
36 #include <linux/module.h>
37 #include <linux/dmaengine.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/interrupt.h>
40 #include <linux/io.h>
41 #include <linux/slab.h>
42 #include <linux/of_device.h>
43 #include <linux/of_platform.h>
44
45 #include <linux/random.h>
46
47 /* Number of DMA Transfer descriptors allocated per channel */
48 #define MPC_DMA_DESCRIPTORS     64
49
50 /* Macro definitions */
51 #define MPC_DMA_CHANNELS        64
52 #define MPC_DMA_TCD_OFFSET      0x1000
53
54 /* Arbitration mode of group and channel */
55 #define MPC_DMA_DMACR_EDCG      (1 << 31)
56 #define MPC_DMA_DMACR_ERGA      (1 << 3)
57 #define MPC_DMA_DMACR_ERCA      (1 << 2)
58
59 /* Error codes */
60 #define MPC_DMA_DMAES_VLD       (1 << 31)
61 #define MPC_DMA_DMAES_GPE       (1 << 15)
62 #define MPC_DMA_DMAES_CPE       (1 << 14)
63 #define MPC_DMA_DMAES_ERRCHN(err) \
64                                 (((err) >> 8) & 0x3f)
65 #define MPC_DMA_DMAES_SAE       (1 << 7)
66 #define MPC_DMA_DMAES_SOE       (1 << 6)
67 #define MPC_DMA_DMAES_DAE       (1 << 5)
68 #define MPC_DMA_DMAES_DOE       (1 << 4)
69 #define MPC_DMA_DMAES_NCE       (1 << 3)
70 #define MPC_DMA_DMAES_SGE       (1 << 2)
71 #define MPC_DMA_DMAES_SBE       (1 << 1)
72 #define MPC_DMA_DMAES_DBE       (1 << 0)
73
74 #define MPC_DMA_DMAGPOR_SNOOP_ENABLE    (1 << 6)
75
76 #define MPC_DMA_TSIZE_1         0x00
77 #define MPC_DMA_TSIZE_2         0x01
78 #define MPC_DMA_TSIZE_4         0x02
79 #define MPC_DMA_TSIZE_16        0x04
80 #define MPC_DMA_TSIZE_32        0x05
81
82 /* MPC5121 DMA engine registers */
83 struct __attribute__ ((__packed__)) mpc_dma_regs {
84         /* 0x00 */
85         u32 dmacr;              /* DMA control register */
86         u32 dmaes;              /* DMA error status */
87         /* 0x08 */
88         u32 dmaerqh;            /* DMA enable request high(channels 63~32) */
89         u32 dmaerql;            /* DMA enable request low(channels 31~0) */
90         u32 dmaeeih;            /* DMA enable error interrupt high(ch63~32) */
91         u32 dmaeeil;            /* DMA enable error interrupt low(ch31~0) */
92         /* 0x18 */
93         u8 dmaserq;             /* DMA set enable request */
94         u8 dmacerq;             /* DMA clear enable request */
95         u8 dmaseei;             /* DMA set enable error interrupt */
96         u8 dmaceei;             /* DMA clear enable error interrupt */
97         /* 0x1c */
98         u8 dmacint;             /* DMA clear interrupt request */
99         u8 dmacerr;             /* DMA clear error */
100         u8 dmassrt;             /* DMA set start bit */
101         u8 dmacdne;             /* DMA clear DONE status bit */
102         /* 0x20 */
103         u32 dmainth;            /* DMA interrupt request high(ch63~32) */
104         u32 dmaintl;            /* DMA interrupt request low(ch31~0) */
105         u32 dmaerrh;            /* DMA error high(ch63~32) */
106         u32 dmaerrl;            /* DMA error low(ch31~0) */
107         /* 0x30 */
108         u32 dmahrsh;            /* DMA hw request status high(ch63~32) */
109         u32 dmahrsl;            /* DMA hardware request status low(ch31~0) */
110         union {
111                 u32 dmaihsa;    /* DMA interrupt high select AXE(ch63~32) */
112                 u32 dmagpor;    /* (General purpose register on MPC8308) */
113         };
114         u32 dmailsa;            /* DMA interrupt low select AXE(ch31~0) */
115         /* 0x40 ~ 0xff */
116         u32 reserve0[48];       /* Reserved */
117         /* 0x100 */
118         u8 dchpri[MPC_DMA_CHANNELS];
119         /* DMA channels(0~63) priority */
120 };
121
122 struct __attribute__ ((__packed__)) mpc_dma_tcd {
123         /* 0x00 */
124         u32 saddr;              /* Source address */
125
126         u32 smod:5;             /* Source address modulo */
127         u32 ssize:3;            /* Source data transfer size */
128         u32 dmod:5;             /* Destination address modulo */
129         u32 dsize:3;            /* Destination data transfer size */
130         u32 soff:16;            /* Signed source address offset */
131
132         /* 0x08 */
133         u32 nbytes;             /* Inner "minor" byte count */
134         u32 slast;              /* Last source address adjustment */
135         u32 daddr;              /* Destination address */
136
137         /* 0x14 */
138         u32 citer_elink:1;      /* Enable channel-to-channel linking on
139                                  * minor loop complete
140                                  */
141         u32 citer_linkch:6;     /* Link channel for minor loop complete */
142         u32 citer:9;            /* Current "major" iteration count */
143         u32 doff:16;            /* Signed destination address offset */
144
145         /* 0x18 */
146         u32 dlast_sga;          /* Last Destination address adjustment/scatter
147                                  * gather address
148                                  */
149
150         /* 0x1c */
151         u32 biter_elink:1;      /* Enable channel-to-channel linking on major
152                                  * loop complete
153                                  */
154         u32 biter_linkch:6;
155         u32 biter:9;            /* Beginning "major" iteration count */
156         u32 bwc:2;              /* Bandwidth control */
157         u32 major_linkch:6;     /* Link channel number */
158         u32 done:1;             /* Channel done */
159         u32 active:1;           /* Channel active */
160         u32 major_elink:1;      /* Enable channel-to-channel linking on major
161                                  * loop complete
162                                  */
163         u32 e_sg:1;             /* Enable scatter/gather processing */
164         u32 d_req:1;            /* Disable request */
165         u32 int_half:1;         /* Enable an interrupt when major counter is
166                                  * half complete
167                                  */
168         u32 int_maj:1;          /* Enable an interrupt when major iteration
169                                  * count completes
170                                  */
171         u32 start:1;            /* Channel start */
172 };
173
174 struct mpc_dma_desc {
175         struct dma_async_tx_descriptor  desc;
176         struct mpc_dma_tcd              *tcd;
177         dma_addr_t                      tcd_paddr;
178         int                             error;
179         struct list_head                node;
180 };
181
182 struct mpc_dma_chan {
183         struct dma_chan                 chan;
184         struct list_head                free;
185         struct list_head                prepared;
186         struct list_head                queued;
187         struct list_head                active;
188         struct list_head                completed;
189         struct mpc_dma_tcd              *tcd;
190         dma_addr_t                      tcd_paddr;
191         dma_cookie_t                    completed_cookie;
192
193         /* Lock for this structure */
194         spinlock_t                      lock;
195 };
196
197 struct mpc_dma {
198         struct dma_device               dma;
199         struct tasklet_struct           tasklet;
200         struct mpc_dma_chan             channels[MPC_DMA_CHANNELS];
201         struct mpc_dma_regs __iomem     *regs;
202         struct mpc_dma_tcd __iomem      *tcd;
203         int                             irq;
204         int                             irq2;
205         uint                            error_status;
206         int                             is_mpc8308;
207
208         /* Lock for error_status field in this structure */
209         spinlock_t                      error_status_lock;
210 };
211
212 #define DRV_NAME        "mpc512x_dma"
213
214 /* Convert struct dma_chan to struct mpc_dma_chan */
215 static inline struct mpc_dma_chan *dma_chan_to_mpc_dma_chan(struct dma_chan *c)
216 {
217         return container_of(c, struct mpc_dma_chan, chan);
218 }
219
220 /* Convert struct dma_chan to struct mpc_dma */
221 static inline struct mpc_dma *dma_chan_to_mpc_dma(struct dma_chan *c)
222 {
223         struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(c);
224         return container_of(mchan, struct mpc_dma, channels[c->chan_id]);
225 }
226
227 /*
228  * Execute all queued DMA descriptors.
229  *
230  * Following requirements must be met while calling mpc_dma_execute():
231  *      a) mchan->lock is acquired,
232  *      b) mchan->active list is empty,
233  *      c) mchan->queued list contains at least one entry.
234  */
235 static void mpc_dma_execute(struct mpc_dma_chan *mchan)
236 {
237         struct mpc_dma *mdma = dma_chan_to_mpc_dma(&mchan->chan);
238         struct mpc_dma_desc *first = NULL;
239         struct mpc_dma_desc *prev = NULL;
240         struct mpc_dma_desc *mdesc;
241         int cid = mchan->chan.chan_id;
242
243         /* Move all queued descriptors to active list */
244         list_splice_tail_init(&mchan->queued, &mchan->active);
245
246         /* Chain descriptors into one transaction */
247         list_for_each_entry(mdesc, &mchan->active, node) {
248                 if (!first)
249                         first = mdesc;
250
251                 if (!prev) {
252                         prev = mdesc;
253                         continue;
254                 }
255
256                 prev->tcd->dlast_sga = mdesc->tcd_paddr;
257                 prev->tcd->e_sg = 1;
258                 mdesc->tcd->start = 1;
259
260                 prev = mdesc;
261         }
262
263         prev->tcd->int_maj = 1;
264
265         /* Send first descriptor in chain into hardware */
266         memcpy_toio(&mdma->tcd[cid], first->tcd, sizeof(struct mpc_dma_tcd));
267
268         if (first != prev)
269                 mdma->tcd[cid].e_sg = 1;
270         out_8(&mdma->regs->dmassrt, cid);
271 }
272
273 /* Handle interrupt on one half of DMA controller (32 channels) */
274 static void mpc_dma_irq_process(struct mpc_dma *mdma, u32 is, u32 es, int off)
275 {
276         struct mpc_dma_chan *mchan;
277         struct mpc_dma_desc *mdesc;
278         u32 status = is | es;
279         int ch;
280
281         while ((ch = fls(status) - 1) >= 0) {
282                 status &= ~(1 << ch);
283                 mchan = &mdma->channels[ch + off];
284
285                 spin_lock(&mchan->lock);
286
287                 out_8(&mdma->regs->dmacint, ch + off);
288                 out_8(&mdma->regs->dmacerr, ch + off);
289
290                 /* Check error status */
291                 if (es & (1 << ch))
292                         list_for_each_entry(mdesc, &mchan->active, node)
293                                 mdesc->error = -EIO;
294
295                 /* Execute queued descriptors */
296                 list_splice_tail_init(&mchan->active, &mchan->completed);
297                 if (!list_empty(&mchan->queued))
298                         mpc_dma_execute(mchan);
299
300                 spin_unlock(&mchan->lock);
301         }
302 }
303
304 /* Interrupt handler */
305 static irqreturn_t mpc_dma_irq(int irq, void *data)
306 {
307         struct mpc_dma *mdma = data;
308         uint es;
309
310         /* Save error status register */
311         es = in_be32(&mdma->regs->dmaes);
312         spin_lock(&mdma->error_status_lock);
313         if ((es & MPC_DMA_DMAES_VLD) && mdma->error_status == 0)
314                 mdma->error_status = es;
315         spin_unlock(&mdma->error_status_lock);
316
317         /* Handle interrupt on each channel */
318         if (mdma->dma.chancnt > 32) {
319                 mpc_dma_irq_process(mdma, in_be32(&mdma->regs->dmainth),
320                                         in_be32(&mdma->regs->dmaerrh), 32);
321         }
322         mpc_dma_irq_process(mdma, in_be32(&mdma->regs->dmaintl),
323                                         in_be32(&mdma->regs->dmaerrl), 0);
324
325         /* Schedule tasklet */
326         tasklet_schedule(&mdma->tasklet);
327
328         return IRQ_HANDLED;
329 }
330
331 /* DMA Tasklet */
332 static void mpc_dma_tasklet(unsigned long data)
333 {
334         struct mpc_dma *mdma = (void *)data;
335         dma_cookie_t last_cookie = 0;
336         struct mpc_dma_chan *mchan;
337         struct mpc_dma_desc *mdesc;
338         struct dma_async_tx_descriptor *desc;
339         unsigned long flags;
340         LIST_HEAD(list);
341         uint es;
342         int i;
343
344         spin_lock_irqsave(&mdma->error_status_lock, flags);
345         es = mdma->error_status;
346         mdma->error_status = 0;
347         spin_unlock_irqrestore(&mdma->error_status_lock, flags);
348
349         /* Print nice error report */
350         if (es) {
351                 dev_err(mdma->dma.dev,
352                         "Hardware reported following error(s) on channel %u:\n",
353                                                       MPC_DMA_DMAES_ERRCHN(es));
354
355                 if (es & MPC_DMA_DMAES_GPE)
356                         dev_err(mdma->dma.dev, "- Group Priority Error\n");
357                 if (es & MPC_DMA_DMAES_CPE)
358                         dev_err(mdma->dma.dev, "- Channel Priority Error\n");
359                 if (es & MPC_DMA_DMAES_SAE)
360                         dev_err(mdma->dma.dev, "- Source Address Error\n");
361                 if (es & MPC_DMA_DMAES_SOE)
362                         dev_err(mdma->dma.dev, "- Source Offset"
363                                                 " Configuration Error\n");
364                 if (es & MPC_DMA_DMAES_DAE)
365                         dev_err(mdma->dma.dev, "- Destination Address"
366                                                                 " Error\n");
367                 if (es & MPC_DMA_DMAES_DOE)
368                         dev_err(mdma->dma.dev, "- Destination Offset"
369                                                 " Configuration Error\n");
370                 if (es & MPC_DMA_DMAES_NCE)
371                         dev_err(mdma->dma.dev, "- NBytes/Citter"
372                                                 " Configuration Error\n");
373                 if (es & MPC_DMA_DMAES_SGE)
374                         dev_err(mdma->dma.dev, "- Scatter/Gather"
375                                                 " Configuration Error\n");
376                 if (es & MPC_DMA_DMAES_SBE)
377                         dev_err(mdma->dma.dev, "- Source Bus Error\n");
378                 if (es & MPC_DMA_DMAES_DBE)
379                         dev_err(mdma->dma.dev, "- Destination Bus Error\n");
380         }
381
382         for (i = 0; i < mdma->dma.chancnt; i++) {
383                 mchan = &mdma->channels[i];
384
385                 /* Get all completed descriptors */
386                 spin_lock_irqsave(&mchan->lock, flags);
387                 if (!list_empty(&mchan->completed))
388                         list_splice_tail_init(&mchan->completed, &list);
389                 spin_unlock_irqrestore(&mchan->lock, flags);
390
391                 if (list_empty(&list))
392                         continue;
393
394                 /* Execute callbacks and run dependencies */
395                 list_for_each_entry(mdesc, &list, node) {
396                         desc = &mdesc->desc;
397
398                         if (desc->callback)
399                                 desc->callback(desc->callback_param);
400
401                         last_cookie = desc->cookie;
402                         dma_run_dependencies(desc);
403                 }
404
405                 /* Free descriptors */
406                 spin_lock_irqsave(&mchan->lock, flags);
407                 list_splice_tail_init(&list, &mchan->free);
408                 mchan->completed_cookie = last_cookie;
409                 spin_unlock_irqrestore(&mchan->lock, flags);
410         }
411 }
412
413 /* Submit descriptor to hardware */
414 static dma_cookie_t mpc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
415 {
416         struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(txd->chan);
417         struct mpc_dma_desc *mdesc;
418         unsigned long flags;
419         dma_cookie_t cookie;
420
421         mdesc = container_of(txd, struct mpc_dma_desc, desc);
422
423         spin_lock_irqsave(&mchan->lock, flags);
424
425         /* Move descriptor to queue */
426         list_move_tail(&mdesc->node, &mchan->queued);
427
428         /* If channel is idle, execute all queued descriptors */
429         if (list_empty(&mchan->active))
430                 mpc_dma_execute(mchan);
431
432         /* Update cookie */
433         cookie = mchan->chan.cookie + 1;
434         if (cookie <= 0)
435                 cookie = 1;
436
437         mchan->chan.cookie = cookie;
438         mdesc->desc.cookie = cookie;
439
440         spin_unlock_irqrestore(&mchan->lock, flags);
441
442         return cookie;
443 }
444
445 /* Alloc channel resources */
446 static int mpc_dma_alloc_chan_resources(struct dma_chan *chan)
447 {
448         struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
449         struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
450         struct mpc_dma_desc *mdesc;
451         struct mpc_dma_tcd *tcd;
452         dma_addr_t tcd_paddr;
453         unsigned long flags;
454         LIST_HEAD(descs);
455         int i;
456
457         /* Alloc DMA memory for Transfer Control Descriptors */
458         tcd = dma_alloc_coherent(mdma->dma.dev,
459                         MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
460                                                         &tcd_paddr, GFP_KERNEL);
461         if (!tcd)
462                 return -ENOMEM;
463
464         /* Alloc descriptors for this channel */
465         for (i = 0; i < MPC_DMA_DESCRIPTORS; i++) {
466                 mdesc = kzalloc(sizeof(struct mpc_dma_desc), GFP_KERNEL);
467                 if (!mdesc) {
468                         dev_notice(mdma->dma.dev, "Memory allocation error. "
469                                         "Allocated only %u descriptors\n", i);
470                         break;
471                 }
472
473                 dma_async_tx_descriptor_init(&mdesc->desc, chan);
474                 mdesc->desc.flags = DMA_CTRL_ACK;
475                 mdesc->desc.tx_submit = mpc_dma_tx_submit;
476
477                 mdesc->tcd = &tcd[i];
478                 mdesc->tcd_paddr = tcd_paddr + (i * sizeof(struct mpc_dma_tcd));
479
480                 list_add_tail(&mdesc->node, &descs);
481         }
482
483         /* Return error only if no descriptors were allocated */
484         if (i == 0) {
485                 dma_free_coherent(mdma->dma.dev,
486                         MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
487                                                                 tcd, tcd_paddr);
488                 return -ENOMEM;
489         }
490
491         spin_lock_irqsave(&mchan->lock, flags);
492         mchan->tcd = tcd;
493         mchan->tcd_paddr = tcd_paddr;
494         list_splice_tail_init(&descs, &mchan->free);
495         spin_unlock_irqrestore(&mchan->lock, flags);
496
497         /* Enable Error Interrupt */
498         out_8(&mdma->regs->dmaseei, chan->chan_id);
499
500         return 0;
501 }
502
503 /* Free channel resources */
504 static void mpc_dma_free_chan_resources(struct dma_chan *chan)
505 {
506         struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
507         struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
508         struct mpc_dma_desc *mdesc, *tmp;
509         struct mpc_dma_tcd *tcd;
510         dma_addr_t tcd_paddr;
511         unsigned long flags;
512         LIST_HEAD(descs);
513
514         spin_lock_irqsave(&mchan->lock, flags);
515
516         /* Channel must be idle */
517         BUG_ON(!list_empty(&mchan->prepared));
518         BUG_ON(!list_empty(&mchan->queued));
519         BUG_ON(!list_empty(&mchan->active));
520         BUG_ON(!list_empty(&mchan->completed));
521
522         /* Move data */
523         list_splice_tail_init(&mchan->free, &descs);
524         tcd = mchan->tcd;
525         tcd_paddr = mchan->tcd_paddr;
526
527         spin_unlock_irqrestore(&mchan->lock, flags);
528
529         /* Free DMA memory used by descriptors */
530         dma_free_coherent(mdma->dma.dev,
531                         MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
532                                                                 tcd, tcd_paddr);
533
534         /* Free descriptors */
535         list_for_each_entry_safe(mdesc, tmp, &descs, node)
536                 kfree(mdesc);
537
538         /* Disable Error Interrupt */
539         out_8(&mdma->regs->dmaceei, chan->chan_id);
540 }
541
542 /* Send all pending descriptor to hardware */
543 static void mpc_dma_issue_pending(struct dma_chan *chan)
544 {
545         /*
546          * We are posting descriptors to the hardware as soon as
547          * they are ready, so this function does nothing.
548          */
549 }
550
551 /* Check request completion status */
552 static enum dma_status
553 mpc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
554                struct dma_tx_state *txstate)
555 {
556         struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
557         unsigned long flags;
558         dma_cookie_t last_used;
559         dma_cookie_t last_complete;
560
561         spin_lock_irqsave(&mchan->lock, flags);
562         last_used = mchan->chan.cookie;
563         last_complete = mchan->completed_cookie;
564         spin_unlock_irqrestore(&mchan->lock, flags);
565
566         dma_set_tx_state(txstate, last_complete, last_used, 0);
567         return dma_async_is_complete(cookie, last_complete, last_used);
568 }
569
570 /* Prepare descriptor for memory to memory copy */
571 static struct dma_async_tx_descriptor *
572 mpc_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
573                                         size_t len, unsigned long flags)
574 {
575         struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
576         struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
577         struct mpc_dma_desc *mdesc = NULL;
578         struct mpc_dma_tcd *tcd;
579         unsigned long iflags;
580
581         /* Get free descriptor */
582         spin_lock_irqsave(&mchan->lock, iflags);
583         if (!list_empty(&mchan->free)) {
584                 mdesc = list_first_entry(&mchan->free, struct mpc_dma_desc,
585                                                                         node);
586                 list_del(&mdesc->node);
587         }
588         spin_unlock_irqrestore(&mchan->lock, iflags);
589
590         if (!mdesc)
591                 return NULL;
592
593         mdesc->error = 0;
594         tcd = mdesc->tcd;
595
596         /* Prepare Transfer Control Descriptor for this transaction */
597         memset(tcd, 0, sizeof(struct mpc_dma_tcd));
598
599         if (IS_ALIGNED(src | dst | len, 32)) {
600                 tcd->ssize = MPC_DMA_TSIZE_32;
601                 tcd->dsize = MPC_DMA_TSIZE_32;
602                 tcd->soff = 32;
603                 tcd->doff = 32;
604         } else if (!mdma->is_mpc8308 && IS_ALIGNED(src | dst | len, 16)) {
605                 /* MPC8308 doesn't support 16 byte transfers */
606                 tcd->ssize = MPC_DMA_TSIZE_16;
607                 tcd->dsize = MPC_DMA_TSIZE_16;
608                 tcd->soff = 16;
609                 tcd->doff = 16;
610         } else if (IS_ALIGNED(src | dst | len, 4)) {
611                 tcd->ssize = MPC_DMA_TSIZE_4;
612                 tcd->dsize = MPC_DMA_TSIZE_4;
613                 tcd->soff = 4;
614                 tcd->doff = 4;
615         } else if (IS_ALIGNED(src | dst | len, 2)) {
616                 tcd->ssize = MPC_DMA_TSIZE_2;
617                 tcd->dsize = MPC_DMA_TSIZE_2;
618                 tcd->soff = 2;
619                 tcd->doff = 2;
620         } else {
621                 tcd->ssize = MPC_DMA_TSIZE_1;
622                 tcd->dsize = MPC_DMA_TSIZE_1;
623                 tcd->soff = 1;
624                 tcd->doff = 1;
625         }
626
627         tcd->saddr = src;
628         tcd->daddr = dst;
629         tcd->nbytes = len;
630         tcd->biter = 1;
631         tcd->citer = 1;
632
633         /* Place descriptor in prepared list */
634         spin_lock_irqsave(&mchan->lock, iflags);
635         list_add_tail(&mdesc->node, &mchan->prepared);
636         spin_unlock_irqrestore(&mchan->lock, iflags);
637
638         return &mdesc->desc;
639 }
640
641 static int __devinit mpc_dma_probe(struct platform_device *op,
642                                         const struct of_device_id *match)
643 {
644         struct device_node *dn = op->dev.of_node;
645         struct device *dev = &op->dev;
646         struct dma_device *dma;
647         struct mpc_dma *mdma;
648         struct mpc_dma_chan *mchan;
649         struct resource res;
650         ulong regs_start, regs_size;
651         int retval, i;
652
653         mdma = devm_kzalloc(dev, sizeof(struct mpc_dma), GFP_KERNEL);
654         if (!mdma) {
655                 dev_err(dev, "Memory exhausted!\n");
656                 return -ENOMEM;
657         }
658
659         mdma->irq = irq_of_parse_and_map(dn, 0);
660         if (mdma->irq == NO_IRQ) {
661                 dev_err(dev, "Error mapping IRQ!\n");
662                 return -EINVAL;
663         }
664
665         if (of_device_is_compatible(dn, "fsl,mpc8308-dma")) {
666                 mdma->is_mpc8308 = 1;
667                 mdma->irq2 = irq_of_parse_and_map(dn, 1);
668                 if (mdma->irq2 == NO_IRQ) {
669                         dev_err(dev, "Error mapping IRQ!\n");
670                         return -EINVAL;
671                 }
672         }
673
674         retval = of_address_to_resource(dn, 0, &res);
675         if (retval) {
676                 dev_err(dev, "Error parsing memory region!\n");
677                 return retval;
678         }
679
680         regs_start = res.start;
681         regs_size = resource_size(&res);
682
683         if (!devm_request_mem_region(dev, regs_start, regs_size, DRV_NAME)) {
684                 dev_err(dev, "Error requesting memory region!\n");
685                 return -EBUSY;
686         }
687
688         mdma->regs = devm_ioremap(dev, regs_start, regs_size);
689         if (!mdma->regs) {
690                 dev_err(dev, "Error mapping memory region!\n");
691                 return -ENOMEM;
692         }
693
694         mdma->tcd = (struct mpc_dma_tcd *)((u8 *)(mdma->regs)
695                                                         + MPC_DMA_TCD_OFFSET);
696
697         retval = devm_request_irq(dev, mdma->irq, &mpc_dma_irq, 0, DRV_NAME,
698                                                                         mdma);
699         if (retval) {
700                 dev_err(dev, "Error requesting IRQ!\n");
701                 return -EINVAL;
702         }
703
704         if (mdma->is_mpc8308) {
705                 retval = devm_request_irq(dev, mdma->irq2, &mpc_dma_irq, 0,
706                                 DRV_NAME, mdma);
707                 if (retval) {
708                         dev_err(dev, "Error requesting IRQ2!\n");
709                         return -EINVAL;
710                 }
711         }
712
713         spin_lock_init(&mdma->error_status_lock);
714
715         dma = &mdma->dma;
716         dma->dev = dev;
717         if (!mdma->is_mpc8308)
718                 dma->chancnt = MPC_DMA_CHANNELS;
719         else
720                 dma->chancnt = 16; /* MPC8308 DMA has only 16 channels */
721         dma->device_alloc_chan_resources = mpc_dma_alloc_chan_resources;
722         dma->device_free_chan_resources = mpc_dma_free_chan_resources;
723         dma->device_issue_pending = mpc_dma_issue_pending;
724         dma->device_tx_status = mpc_dma_tx_status;
725         dma->device_prep_dma_memcpy = mpc_dma_prep_memcpy;
726
727         INIT_LIST_HEAD(&dma->channels);
728         dma_cap_set(DMA_MEMCPY, dma->cap_mask);
729
730         for (i = 0; i < dma->chancnt; i++) {
731                 mchan = &mdma->channels[i];
732
733                 mchan->chan.device = dma;
734                 mchan->chan.chan_id = i;
735                 mchan->chan.cookie = 1;
736                 mchan->completed_cookie = mchan->chan.cookie;
737
738                 INIT_LIST_HEAD(&mchan->free);
739                 INIT_LIST_HEAD(&mchan->prepared);
740                 INIT_LIST_HEAD(&mchan->queued);
741                 INIT_LIST_HEAD(&mchan->active);
742                 INIT_LIST_HEAD(&mchan->completed);
743
744                 spin_lock_init(&mchan->lock);
745                 list_add_tail(&mchan->chan.device_node, &dma->channels);
746         }
747
748         tasklet_init(&mdma->tasklet, mpc_dma_tasklet, (unsigned long)mdma);
749
750         /*
751          * Configure DMA Engine:
752          * - Dynamic clock,
753          * - Round-robin group arbitration,
754          * - Round-robin channel arbitration.
755          */
756         if (!mdma->is_mpc8308) {
757                 out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_EDCG |
758                                         MPC_DMA_DMACR_ERGA | MPC_DMA_DMACR_ERCA);
759
760                 /* Disable hardware DMA requests */
761                 out_be32(&mdma->regs->dmaerqh, 0);
762                 out_be32(&mdma->regs->dmaerql, 0);
763
764                 /* Disable error interrupts */
765                 out_be32(&mdma->regs->dmaeeih, 0);
766                 out_be32(&mdma->regs->dmaeeil, 0);
767
768                 /* Clear interrupts status */
769                 out_be32(&mdma->regs->dmainth, 0xFFFFFFFF);
770                 out_be32(&mdma->regs->dmaintl, 0xFFFFFFFF);
771                 out_be32(&mdma->regs->dmaerrh, 0xFFFFFFFF);
772                 out_be32(&mdma->regs->dmaerrl, 0xFFFFFFFF);
773
774                 /* Route interrupts to IPIC */
775                 out_be32(&mdma->regs->dmaihsa, 0);
776                 out_be32(&mdma->regs->dmailsa, 0);
777         } else {
778                 /* MPC8308 has 16 channels and lacks some registers */
779                 out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_ERCA);
780
781                 /* enable snooping */
782                 out_be32(&mdma->regs->dmagpor, MPC_DMA_DMAGPOR_SNOOP_ENABLE);
783                 /* Disable error interrupts */
784                 out_be32(&mdma->regs->dmaeeil, 0);
785
786                 /* Clear interrupts status */
787                 out_be32(&mdma->regs->dmaintl, 0xFFFF);
788                 out_be32(&mdma->regs->dmaerrl, 0xFFFF);
789         }
790
791         /* Register DMA engine */
792         dev_set_drvdata(dev, mdma);
793         retval = dma_async_device_register(dma);
794         if (retval) {
795                 devm_free_irq(dev, mdma->irq, mdma);
796                 irq_dispose_mapping(mdma->irq);
797         }
798
799         return retval;
800 }
801
802 static int __devexit mpc_dma_remove(struct platform_device *op)
803 {
804         struct device *dev = &op->dev;
805         struct mpc_dma *mdma = dev_get_drvdata(dev);
806
807         dma_async_device_unregister(&mdma->dma);
808         devm_free_irq(dev, mdma->irq, mdma);
809         irq_dispose_mapping(mdma->irq);
810
811         return 0;
812 }
813
814 static struct of_device_id mpc_dma_match[] = {
815         { .compatible = "fsl,mpc5121-dma", },
816         {},
817 };
818
819 static struct of_platform_driver mpc_dma_driver = {
820         .probe          = mpc_dma_probe,
821         .remove         = __devexit_p(mpc_dma_remove),
822         .driver = {
823                 .name = DRV_NAME,
824                 .owner = THIS_MODULE,
825                 .of_match_table = mpc_dma_match,
826         },
827 };
828
829 static int __init mpc_dma_init(void)
830 {
831         return of_register_platform_driver(&mpc_dma_driver);
832 }
833 module_init(mpc_dma_init);
834
835 static void __exit mpc_dma_exit(void)
836 {
837         of_unregister_platform_driver(&mpc_dma_driver);
838 }
839 module_exit(mpc_dma_exit);
840
841 MODULE_LICENSE("GPL");
842 MODULE_AUTHOR("Piotr Ziecik <kosmo@semihalf.com>");