fsldma: implement support for scatterlist to scatterlist copy
[pandora-kernel.git] / drivers / dma / fsldma.c
1 /*
2  * Freescale MPC85xx, MPC83xx DMA Engine support
3  *
4  * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
5  *
6  * Author:
7  *   Zhang Wei <wei.zhang@freescale.com>, Jul 2007
8  *   Ebony Zhu <ebony.zhu@freescale.com>, May 2007
9  *
10  * Description:
11  *   DMA engine driver for Freescale MPC8540 DMA controller, which is
12  *   also fit for MPC8560, MPC8555, MPC8548, MPC8641, and etc.
13  *   The support for MPC8349 DMA controller is also added.
14  *
15  * This driver instructs the DMA controller to issue the PCI Read Multiple
16  * command for PCI read operations, instead of using the default PCI Read Line
17  * command. Please be aware that this setting may result in read pre-fetching
18  * on some platforms.
19  *
20  * This is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation; either version 2 of the License, or
23  * (at your option) any later version.
24  *
25  */
26
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/dmaengine.h>
33 #include <linux/delay.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/dmapool.h>
36 #include <linux/of_platform.h>
37
38 #include <asm/fsldma.h>
39 #include "fsldma.h"
40
41 static const char msg_ld_oom[] = "No free memory for link descriptor\n";
42
43 static void dma_init(struct fsldma_chan *chan)
44 {
45         /* Reset the channel */
46         DMA_OUT(chan, &chan->regs->mr, 0, 32);
47
48         switch (chan->feature & FSL_DMA_IP_MASK) {
49         case FSL_DMA_IP_85XX:
50                 /* Set the channel to below modes:
51                  * EIE - Error interrupt enable
52                  * EOSIE - End of segments interrupt enable (basic mode)
53                  * EOLNIE - End of links interrupt enable
54                  */
55                 DMA_OUT(chan, &chan->regs->mr, FSL_DMA_MR_EIE
56                                 | FSL_DMA_MR_EOLNIE | FSL_DMA_MR_EOSIE, 32);
57                 break;
58         case FSL_DMA_IP_83XX:
59                 /* Set the channel to below modes:
60                  * EOTIE - End-of-transfer interrupt enable
61                  * PRC_RM - PCI read multiple
62                  */
63                 DMA_OUT(chan, &chan->regs->mr, FSL_DMA_MR_EOTIE
64                                 | FSL_DMA_MR_PRC_RM, 32);
65                 break;
66         }
67 }
68
69 static void set_sr(struct fsldma_chan *chan, u32 val)
70 {
71         DMA_OUT(chan, &chan->regs->sr, val, 32);
72 }
73
74 static u32 get_sr(struct fsldma_chan *chan)
75 {
76         return DMA_IN(chan, &chan->regs->sr, 32);
77 }
78
79 static void set_desc_cnt(struct fsldma_chan *chan,
80                                 struct fsl_dma_ld_hw *hw, u32 count)
81 {
82         hw->count = CPU_TO_DMA(chan, count, 32);
83 }
84
85 static void set_desc_src(struct fsldma_chan *chan,
86                                 struct fsl_dma_ld_hw *hw, dma_addr_t src)
87 {
88         u64 snoop_bits;
89
90         snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
91                 ? ((u64)FSL_DMA_SATR_SREADTYPE_SNOOP_READ << 32) : 0;
92         hw->src_addr = CPU_TO_DMA(chan, snoop_bits | src, 64);
93 }
94
95 static void set_desc_dst(struct fsldma_chan *chan,
96                                 struct fsl_dma_ld_hw *hw, dma_addr_t dst)
97 {
98         u64 snoop_bits;
99
100         snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
101                 ? ((u64)FSL_DMA_DATR_DWRITETYPE_SNOOP_WRITE << 32) : 0;
102         hw->dst_addr = CPU_TO_DMA(chan, snoop_bits | dst, 64);
103 }
104
105 static void set_desc_next(struct fsldma_chan *chan,
106                                 struct fsl_dma_ld_hw *hw, dma_addr_t next)
107 {
108         u64 snoop_bits;
109
110         snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
111                 ? FSL_DMA_SNEN : 0;
112         hw->next_ln_addr = CPU_TO_DMA(chan, snoop_bits | next, 64);
113 }
114
115 static void set_cdar(struct fsldma_chan *chan, dma_addr_t addr)
116 {
117         DMA_OUT(chan, &chan->regs->cdar, addr | FSL_DMA_SNEN, 64);
118 }
119
120 static dma_addr_t get_cdar(struct fsldma_chan *chan)
121 {
122         return DMA_IN(chan, &chan->regs->cdar, 64) & ~FSL_DMA_SNEN;
123 }
124
125 static dma_addr_t get_ndar(struct fsldma_chan *chan)
126 {
127         return DMA_IN(chan, &chan->regs->ndar, 64);
128 }
129
130 static u32 get_bcr(struct fsldma_chan *chan)
131 {
132         return DMA_IN(chan, &chan->regs->bcr, 32);
133 }
134
135 static int dma_is_idle(struct fsldma_chan *chan)
136 {
137         u32 sr = get_sr(chan);
138         return (!(sr & FSL_DMA_SR_CB)) || (sr & FSL_DMA_SR_CH);
139 }
140
141 static void dma_start(struct fsldma_chan *chan)
142 {
143         u32 mode;
144
145         mode = DMA_IN(chan, &chan->regs->mr, 32);
146
147         if ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX) {
148                 if (chan->feature & FSL_DMA_CHAN_PAUSE_EXT) {
149                         DMA_OUT(chan, &chan->regs->bcr, 0, 32);
150                         mode |= FSL_DMA_MR_EMP_EN;
151                 } else {
152                         mode &= ~FSL_DMA_MR_EMP_EN;
153                 }
154         }
155
156         if (chan->feature & FSL_DMA_CHAN_START_EXT)
157                 mode |= FSL_DMA_MR_EMS_EN;
158         else
159                 mode |= FSL_DMA_MR_CS;
160
161         DMA_OUT(chan, &chan->regs->mr, mode, 32);
162 }
163
164 static void dma_halt(struct fsldma_chan *chan)
165 {
166         u32 mode;
167         int i;
168
169         mode = DMA_IN(chan, &chan->regs->mr, 32);
170         mode |= FSL_DMA_MR_CA;
171         DMA_OUT(chan, &chan->regs->mr, mode, 32);
172
173         mode &= ~(FSL_DMA_MR_CS | FSL_DMA_MR_EMS_EN | FSL_DMA_MR_CA);
174         DMA_OUT(chan, &chan->regs->mr, mode, 32);
175
176         for (i = 0; i < 100; i++) {
177                 if (dma_is_idle(chan))
178                         return;
179
180                 udelay(10);
181         }
182
183         if (!dma_is_idle(chan))
184                 dev_err(chan->dev, "DMA halt timeout!\n");
185 }
186
187 static void set_ld_eol(struct fsldma_chan *chan,
188                         struct fsl_desc_sw *desc)
189 {
190         u64 snoop_bits;
191
192         snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
193                 ? FSL_DMA_SNEN : 0;
194
195         desc->hw.next_ln_addr = CPU_TO_DMA(chan,
196                 DMA_TO_CPU(chan, desc->hw.next_ln_addr, 64) | FSL_DMA_EOL
197                         | snoop_bits, 64);
198 }
199
200 /**
201  * fsl_chan_set_src_loop_size - Set source address hold transfer size
202  * @chan : Freescale DMA channel
203  * @size     : Address loop size, 0 for disable loop
204  *
205  * The set source address hold transfer size. The source
206  * address hold or loop transfer size is when the DMA transfer
207  * data from source address (SA), if the loop size is 4, the DMA will
208  * read data from SA, SA + 1, SA + 2, SA + 3, then loop back to SA,
209  * SA + 1 ... and so on.
210  */
211 static void fsl_chan_set_src_loop_size(struct fsldma_chan *chan, int size)
212 {
213         u32 mode;
214
215         mode = DMA_IN(chan, &chan->regs->mr, 32);
216
217         switch (size) {
218         case 0:
219                 mode &= ~FSL_DMA_MR_SAHE;
220                 break;
221         case 1:
222         case 2:
223         case 4:
224         case 8:
225                 mode |= FSL_DMA_MR_SAHE | (__ilog2(size) << 14);
226                 break;
227         }
228
229         DMA_OUT(chan, &chan->regs->mr, mode, 32);
230 }
231
232 /**
233  * fsl_chan_set_dst_loop_size - Set destination address hold transfer size
234  * @chan : Freescale DMA channel
235  * @size     : Address loop size, 0 for disable loop
236  *
237  * The set destination address hold transfer size. The destination
238  * address hold or loop transfer size is when the DMA transfer
239  * data to destination address (TA), if the loop size is 4, the DMA will
240  * write data to TA, TA + 1, TA + 2, TA + 3, then loop back to TA,
241  * TA + 1 ... and so on.
242  */
243 static void fsl_chan_set_dst_loop_size(struct fsldma_chan *chan, int size)
244 {
245         u32 mode;
246
247         mode = DMA_IN(chan, &chan->regs->mr, 32);
248
249         switch (size) {
250         case 0:
251                 mode &= ~FSL_DMA_MR_DAHE;
252                 break;
253         case 1:
254         case 2:
255         case 4:
256         case 8:
257                 mode |= FSL_DMA_MR_DAHE | (__ilog2(size) << 16);
258                 break;
259         }
260
261         DMA_OUT(chan, &chan->regs->mr, mode, 32);
262 }
263
264 /**
265  * fsl_chan_set_request_count - Set DMA Request Count for external control
266  * @chan : Freescale DMA channel
267  * @size     : Number of bytes to transfer in a single request
268  *
269  * The Freescale DMA channel can be controlled by the external signal DREQ#.
270  * The DMA request count is how many bytes are allowed to transfer before
271  * pausing the channel, after which a new assertion of DREQ# resumes channel
272  * operation.
273  *
274  * A size of 0 disables external pause control. The maximum size is 1024.
275  */
276 static void fsl_chan_set_request_count(struct fsldma_chan *chan, int size)
277 {
278         u32 mode;
279
280         BUG_ON(size > 1024);
281
282         mode = DMA_IN(chan, &chan->regs->mr, 32);
283         mode |= (__ilog2(size) << 24) & 0x0f000000;
284
285         DMA_OUT(chan, &chan->regs->mr, mode, 32);
286 }
287
288 /**
289  * fsl_chan_toggle_ext_pause - Toggle channel external pause status
290  * @chan : Freescale DMA channel
291  * @enable   : 0 is disabled, 1 is enabled.
292  *
293  * The Freescale DMA channel can be controlled by the external signal DREQ#.
294  * The DMA Request Count feature should be used in addition to this feature
295  * to set the number of bytes to transfer before pausing the channel.
296  */
297 static void fsl_chan_toggle_ext_pause(struct fsldma_chan *chan, int enable)
298 {
299         if (enable)
300                 chan->feature |= FSL_DMA_CHAN_PAUSE_EXT;
301         else
302                 chan->feature &= ~FSL_DMA_CHAN_PAUSE_EXT;
303 }
304
305 /**
306  * fsl_chan_toggle_ext_start - Toggle channel external start status
307  * @chan : Freescale DMA channel
308  * @enable   : 0 is disabled, 1 is enabled.
309  *
310  * If enable the external start, the channel can be started by an
311  * external DMA start pin. So the dma_start() does not start the
312  * transfer immediately. The DMA channel will wait for the
313  * control pin asserted.
314  */
315 static void fsl_chan_toggle_ext_start(struct fsldma_chan *chan, int enable)
316 {
317         if (enable)
318                 chan->feature |= FSL_DMA_CHAN_START_EXT;
319         else
320                 chan->feature &= ~FSL_DMA_CHAN_START_EXT;
321 }
322
323 static void append_ld_queue(struct fsldma_chan *chan,
324                             struct fsl_desc_sw *desc)
325 {
326         struct fsl_desc_sw *tail = to_fsl_desc(chan->ld_pending.prev);
327
328         if (list_empty(&chan->ld_pending))
329                 goto out_splice;
330
331         /*
332          * Add the hardware descriptor to the chain of hardware descriptors
333          * that already exists in memory.
334          *
335          * This will un-set the EOL bit of the existing transaction, and the
336          * last link in this transaction will become the EOL descriptor.
337          */
338         set_desc_next(chan, &tail->hw, desc->async_tx.phys);
339
340         /*
341          * Add the software descriptor and all children to the list
342          * of pending transactions
343          */
344 out_splice:
345         list_splice_tail_init(&desc->tx_list, &chan->ld_pending);
346 }
347
348 static dma_cookie_t fsl_dma_tx_submit(struct dma_async_tx_descriptor *tx)
349 {
350         struct fsldma_chan *chan = to_fsl_chan(tx->chan);
351         struct fsl_desc_sw *desc = tx_to_fsl_desc(tx);
352         struct fsl_desc_sw *child;
353         unsigned long flags;
354         dma_cookie_t cookie;
355
356         spin_lock_irqsave(&chan->desc_lock, flags);
357
358         /*
359          * assign cookies to all of the software descriptors
360          * that make up this transaction
361          */
362         cookie = chan->common.cookie;
363         list_for_each_entry(child, &desc->tx_list, node) {
364                 cookie++;
365                 if (cookie < 0)
366                         cookie = 1;
367
368                 child->async_tx.cookie = cookie;
369         }
370
371         chan->common.cookie = cookie;
372
373         /* put this transaction onto the tail of the pending queue */
374         append_ld_queue(chan, desc);
375
376         spin_unlock_irqrestore(&chan->desc_lock, flags);
377
378         return cookie;
379 }
380
381 /**
382  * fsl_dma_alloc_descriptor - Allocate descriptor from channel's DMA pool.
383  * @chan : Freescale DMA channel
384  *
385  * Return - The descriptor allocated. NULL for failed.
386  */
387 static struct fsl_desc_sw *fsl_dma_alloc_descriptor(
388                                         struct fsldma_chan *chan)
389 {
390         struct fsl_desc_sw *desc;
391         dma_addr_t pdesc;
392
393         desc = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
394         if (!desc) {
395                 dev_dbg(chan->dev, "out of memory for link desc\n");
396                 return NULL;
397         }
398
399         memset(desc, 0, sizeof(*desc));
400         INIT_LIST_HEAD(&desc->tx_list);
401         dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
402         desc->async_tx.tx_submit = fsl_dma_tx_submit;
403         desc->async_tx.phys = pdesc;
404
405         return desc;
406 }
407
408
409 /**
410  * fsl_dma_alloc_chan_resources - Allocate resources for DMA channel.
411  * @chan : Freescale DMA channel
412  *
413  * This function will create a dma pool for descriptor allocation.
414  *
415  * Return - The number of descriptors allocated.
416  */
417 static int fsl_dma_alloc_chan_resources(struct dma_chan *dchan)
418 {
419         struct fsldma_chan *chan = to_fsl_chan(dchan);
420
421         /* Has this channel already been allocated? */
422         if (chan->desc_pool)
423                 return 1;
424
425         /*
426          * We need the descriptor to be aligned to 32bytes
427          * for meeting FSL DMA specification requirement.
428          */
429         chan->desc_pool = dma_pool_create("fsl_dma_engine_desc_pool",
430                                           chan->dev,
431                                           sizeof(struct fsl_desc_sw),
432                                           __alignof__(struct fsl_desc_sw), 0);
433         if (!chan->desc_pool) {
434                 dev_err(chan->dev, "unable to allocate channel %d "
435                                    "descriptor pool\n", chan->id);
436                 return -ENOMEM;
437         }
438
439         /* there is at least one descriptor free to be allocated */
440         return 1;
441 }
442
443 /**
444  * fsldma_free_desc_list - Free all descriptors in a queue
445  * @chan: Freescae DMA channel
446  * @list: the list to free
447  *
448  * LOCKING: must hold chan->desc_lock
449  */
450 static void fsldma_free_desc_list(struct fsldma_chan *chan,
451                                   struct list_head *list)
452 {
453         struct fsl_desc_sw *desc, *_desc;
454
455         list_for_each_entry_safe(desc, _desc, list, node) {
456                 list_del(&desc->node);
457                 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
458         }
459 }
460
461 static void fsldma_free_desc_list_reverse(struct fsldma_chan *chan,
462                                           struct list_head *list)
463 {
464         struct fsl_desc_sw *desc, *_desc;
465
466         list_for_each_entry_safe_reverse(desc, _desc, list, node) {
467                 list_del(&desc->node);
468                 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
469         }
470 }
471
472 /**
473  * fsl_dma_free_chan_resources - Free all resources of the channel.
474  * @chan : Freescale DMA channel
475  */
476 static void fsl_dma_free_chan_resources(struct dma_chan *dchan)
477 {
478         struct fsldma_chan *chan = to_fsl_chan(dchan);
479         unsigned long flags;
480
481         dev_dbg(chan->dev, "Free all channel resources.\n");
482         spin_lock_irqsave(&chan->desc_lock, flags);
483         fsldma_free_desc_list(chan, &chan->ld_pending);
484         fsldma_free_desc_list(chan, &chan->ld_running);
485         spin_unlock_irqrestore(&chan->desc_lock, flags);
486
487         dma_pool_destroy(chan->desc_pool);
488         chan->desc_pool = NULL;
489 }
490
491 static struct dma_async_tx_descriptor *
492 fsl_dma_prep_interrupt(struct dma_chan *dchan, unsigned long flags)
493 {
494         struct fsldma_chan *chan;
495         struct fsl_desc_sw *new;
496
497         if (!dchan)
498                 return NULL;
499
500         chan = to_fsl_chan(dchan);
501
502         new = fsl_dma_alloc_descriptor(chan);
503         if (!new) {
504                 dev_err(chan->dev, msg_ld_oom);
505                 return NULL;
506         }
507
508         new->async_tx.cookie = -EBUSY;
509         new->async_tx.flags = flags;
510
511         /* Insert the link descriptor to the LD ring */
512         list_add_tail(&new->node, &new->tx_list);
513
514         /* Set End-of-link to the last link descriptor of new list*/
515         set_ld_eol(chan, new);
516
517         return &new->async_tx;
518 }
519
520 static struct dma_async_tx_descriptor *fsl_dma_prep_memcpy(
521         struct dma_chan *dchan, dma_addr_t dma_dst, dma_addr_t dma_src,
522         size_t len, unsigned long flags)
523 {
524         struct fsldma_chan *chan;
525         struct fsl_desc_sw *first = NULL, *prev = NULL, *new;
526         size_t copy;
527
528         if (!dchan)
529                 return NULL;
530
531         if (!len)
532                 return NULL;
533
534         chan = to_fsl_chan(dchan);
535
536         do {
537
538                 /* Allocate the link descriptor from DMA pool */
539                 new = fsl_dma_alloc_descriptor(chan);
540                 if (!new) {
541                         dev_err(chan->dev, msg_ld_oom);
542                         goto fail;
543                 }
544 #ifdef FSL_DMA_LD_DEBUG
545                 dev_dbg(chan->dev, "new link desc alloc %p\n", new);
546 #endif
547
548                 copy = min(len, (size_t)FSL_DMA_BCR_MAX_CNT);
549
550                 set_desc_cnt(chan, &new->hw, copy);
551                 set_desc_src(chan, &new->hw, dma_src);
552                 set_desc_dst(chan, &new->hw, dma_dst);
553
554                 if (!first)
555                         first = new;
556                 else
557                         set_desc_next(chan, &prev->hw, new->async_tx.phys);
558
559                 new->async_tx.cookie = 0;
560                 async_tx_ack(&new->async_tx);
561
562                 prev = new;
563                 len -= copy;
564                 dma_src += copy;
565                 dma_dst += copy;
566
567                 /* Insert the link descriptor to the LD ring */
568                 list_add_tail(&new->node, &first->tx_list);
569         } while (len);
570
571         new->async_tx.flags = flags; /* client is in control of this ack */
572         new->async_tx.cookie = -EBUSY;
573
574         /* Set End-of-link to the last link descriptor of new list*/
575         set_ld_eol(chan, new);
576
577         return &first->async_tx;
578
579 fail:
580         if (!first)
581                 return NULL;
582
583         fsldma_free_desc_list_reverse(chan, &first->tx_list);
584         return NULL;
585 }
586
587 static struct dma_async_tx_descriptor *fsl_dma_prep_sg(struct dma_chan *dchan,
588         struct scatterlist *dst_sg, unsigned int dst_nents,
589         struct scatterlist *src_sg, unsigned int src_nents,
590         unsigned long flags)
591 {
592         struct fsl_desc_sw *first = NULL, *prev = NULL, *new = NULL;
593         struct fsldma_chan *chan = to_fsl_chan(dchan);
594         size_t dst_avail, src_avail;
595         dma_addr_t dst, src;
596         size_t len;
597
598         /* basic sanity checks */
599         if (dst_nents == 0 || src_nents == 0)
600                 return NULL;
601
602         if (dst_sg == NULL || src_sg == NULL)
603                 return NULL;
604
605         /*
606          * TODO: should we check that both scatterlists have the same
607          * TODO: number of bytes in total? Is that really an error?
608          */
609
610         /* get prepared for the loop */
611         dst_avail = sg_dma_len(dst_sg);
612         src_avail = sg_dma_len(src_sg);
613
614         /* run until we are out of scatterlist entries */
615         while (true) {
616
617                 /* create the largest transaction possible */
618                 len = min_t(size_t, src_avail, dst_avail);
619                 len = min_t(size_t, len, FSL_DMA_BCR_MAX_CNT);
620                 if (len == 0)
621                         goto fetch;
622
623                 dst = sg_dma_address(dst_sg) + sg_dma_len(dst_sg) - dst_avail;
624                 src = sg_dma_address(src_sg) + sg_dma_len(src_sg) - src_avail;
625
626                 /* allocate and populate the descriptor */
627                 new = fsl_dma_alloc_descriptor(chan);
628                 if (!new) {
629                         dev_err(chan->dev, msg_ld_oom);
630                         goto fail;
631                 }
632 #ifdef FSL_DMA_LD_DEBUG
633                 dev_dbg(chan->dev, "new link desc alloc %p\n", new);
634 #endif
635
636                 set_desc_cnt(chan, &new->hw, len);
637                 set_desc_src(chan, &new->hw, src);
638                 set_desc_dst(chan, &new->hw, dst);
639
640                 if (!first)
641                         first = new;
642                 else
643                         set_desc_next(chan, &prev->hw, new->async_tx.phys);
644
645                 new->async_tx.cookie = 0;
646                 async_tx_ack(&new->async_tx);
647                 prev = new;
648
649                 /* Insert the link descriptor to the LD ring */
650                 list_add_tail(&new->node, &first->tx_list);
651
652                 /* update metadata */
653                 dst_avail -= len;
654                 src_avail -= len;
655
656 fetch:
657                 /* fetch the next dst scatterlist entry */
658                 if (dst_avail == 0) {
659
660                         /* no more entries: we're done */
661                         if (dst_nents == 0)
662                                 break;
663
664                         /* fetch the next entry: if there are no more: done */
665                         dst_sg = sg_next(dst_sg);
666                         if (dst_sg == NULL)
667                                 break;
668
669                         dst_nents--;
670                         dst_avail = sg_dma_len(dst_sg);
671                 }
672
673                 /* fetch the next src scatterlist entry */
674                 if (src_avail == 0) {
675
676                         /* no more entries: we're done */
677                         if (src_nents == 0)
678                                 break;
679
680                         /* fetch the next entry: if there are no more: done */
681                         src_sg = sg_next(src_sg);
682                         if (src_sg == NULL)
683                                 break;
684
685                         src_nents--;
686                         src_avail = sg_dma_len(src_sg);
687                 }
688         }
689
690         new->async_tx.flags = flags; /* client is in control of this ack */
691         new->async_tx.cookie = -EBUSY;
692
693         /* Set End-of-link to the last link descriptor of new list */
694         set_ld_eol(chan, new);
695
696         return &first->async_tx;
697
698 fail:
699         if (!first)
700                 return NULL;
701
702         fsldma_free_desc_list_reverse(chan, &first->tx_list);
703         return NULL;
704 }
705
706 /**
707  * fsl_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
708  * @chan: DMA channel
709  * @sgl: scatterlist to transfer to/from
710  * @sg_len: number of entries in @scatterlist
711  * @direction: DMA direction
712  * @flags: DMAEngine flags
713  *
714  * Prepare a set of descriptors for a DMA_SLAVE transaction. Following the
715  * DMA_SLAVE API, this gets the device-specific information from the
716  * chan->private variable.
717  */
718 static struct dma_async_tx_descriptor *fsl_dma_prep_slave_sg(
719         struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
720         enum dma_data_direction direction, unsigned long flags)
721 {
722         struct fsldma_chan *chan;
723         struct fsl_desc_sw *first = NULL, *prev = NULL, *new = NULL;
724         struct fsl_dma_slave *slave;
725         size_t copy;
726
727         int i;
728         struct scatterlist *sg;
729         size_t sg_used;
730         size_t hw_used;
731         struct fsl_dma_hw_addr *hw;
732         dma_addr_t dma_dst, dma_src;
733
734         if (!dchan)
735                 return NULL;
736
737         if (!dchan->private)
738                 return NULL;
739
740         chan = to_fsl_chan(dchan);
741         slave = dchan->private;
742
743         if (list_empty(&slave->addresses))
744                 return NULL;
745
746         hw = list_first_entry(&slave->addresses, struct fsl_dma_hw_addr, entry);
747         hw_used = 0;
748
749         /*
750          * Build the hardware transaction to copy from the scatterlist to
751          * the hardware, or from the hardware to the scatterlist
752          *
753          * If you are copying from the hardware to the scatterlist and it
754          * takes two hardware entries to fill an entire page, then both
755          * hardware entries will be coalesced into the same page
756          *
757          * If you are copying from the scatterlist to the hardware and a
758          * single page can fill two hardware entries, then the data will
759          * be read out of the page into the first hardware entry, and so on
760          */
761         for_each_sg(sgl, sg, sg_len, i) {
762                 sg_used = 0;
763
764                 /* Loop until the entire scatterlist entry is used */
765                 while (sg_used < sg_dma_len(sg)) {
766
767                         /*
768                          * If we've used up the current hardware address/length
769                          * pair, we need to load a new one
770                          *
771                          * This is done in a while loop so that descriptors with
772                          * length == 0 will be skipped
773                          */
774                         while (hw_used >= hw->length) {
775
776                                 /*
777                                  * If the current hardware entry is the last
778                                  * entry in the list, we're finished
779                                  */
780                                 if (list_is_last(&hw->entry, &slave->addresses))
781                                         goto finished;
782
783                                 /* Get the next hardware address/length pair */
784                                 hw = list_entry(hw->entry.next,
785                                                 struct fsl_dma_hw_addr, entry);
786                                 hw_used = 0;
787                         }
788
789                         /* Allocate the link descriptor from DMA pool */
790                         new = fsl_dma_alloc_descriptor(chan);
791                         if (!new) {
792                                 dev_err(chan->dev, "No free memory for "
793                                                        "link descriptor\n");
794                                 goto fail;
795                         }
796 #ifdef FSL_DMA_LD_DEBUG
797                         dev_dbg(chan->dev, "new link desc alloc %p\n", new);
798 #endif
799
800                         /*
801                          * Calculate the maximum number of bytes to transfer,
802                          * making sure it is less than the DMA controller limit
803                          */
804                         copy = min_t(size_t, sg_dma_len(sg) - sg_used,
805                                              hw->length - hw_used);
806                         copy = min_t(size_t, copy, FSL_DMA_BCR_MAX_CNT);
807
808                         /*
809                          * DMA_FROM_DEVICE
810                          * from the hardware to the scatterlist
811                          *
812                          * DMA_TO_DEVICE
813                          * from the scatterlist to the hardware
814                          */
815                         if (direction == DMA_FROM_DEVICE) {
816                                 dma_src = hw->address + hw_used;
817                                 dma_dst = sg_dma_address(sg) + sg_used;
818                         } else {
819                                 dma_src = sg_dma_address(sg) + sg_used;
820                                 dma_dst = hw->address + hw_used;
821                         }
822
823                         /* Fill in the descriptor */
824                         set_desc_cnt(chan, &new->hw, copy);
825                         set_desc_src(chan, &new->hw, dma_src);
826                         set_desc_dst(chan, &new->hw, dma_dst);
827
828                         /*
829                          * If this is not the first descriptor, chain the
830                          * current descriptor after the previous descriptor
831                          */
832                         if (!first) {
833                                 first = new;
834                         } else {
835                                 set_desc_next(chan, &prev->hw,
836                                               new->async_tx.phys);
837                         }
838
839                         new->async_tx.cookie = 0;
840                         async_tx_ack(&new->async_tx);
841
842                         prev = new;
843                         sg_used += copy;
844                         hw_used += copy;
845
846                         /* Insert the link descriptor into the LD ring */
847                         list_add_tail(&new->node, &first->tx_list);
848                 }
849         }
850
851 finished:
852
853         /* All of the hardware address/length pairs had length == 0 */
854         if (!first || !new)
855                 return NULL;
856
857         new->async_tx.flags = flags;
858         new->async_tx.cookie = -EBUSY;
859
860         /* Set End-of-link to the last link descriptor of new list */
861         set_ld_eol(chan, new);
862
863         /* Enable extra controller features */
864         if (chan->set_src_loop_size)
865                 chan->set_src_loop_size(chan, slave->src_loop_size);
866
867         if (chan->set_dst_loop_size)
868                 chan->set_dst_loop_size(chan, slave->dst_loop_size);
869
870         if (chan->toggle_ext_start)
871                 chan->toggle_ext_start(chan, slave->external_start);
872
873         if (chan->toggle_ext_pause)
874                 chan->toggle_ext_pause(chan, slave->external_pause);
875
876         if (chan->set_request_count)
877                 chan->set_request_count(chan, slave->request_count);
878
879         return &first->async_tx;
880
881 fail:
882         /* If first was not set, then we failed to allocate the very first
883          * descriptor, and we're done */
884         if (!first)
885                 return NULL;
886
887         /*
888          * First is set, so all of the descriptors we allocated have been added
889          * to first->tx_list, INCLUDING "first" itself. Therefore we
890          * must traverse the list backwards freeing each descriptor in turn
891          *
892          * We're re-using variables for the loop, oh well
893          */
894         fsldma_free_desc_list_reverse(chan, &first->tx_list);
895         return NULL;
896 }
897
898 static int fsl_dma_device_control(struct dma_chan *dchan,
899                                   enum dma_ctrl_cmd cmd, unsigned long arg)
900 {
901         struct fsldma_chan *chan;
902         unsigned long flags;
903
904         /* Only supports DMA_TERMINATE_ALL */
905         if (cmd != DMA_TERMINATE_ALL)
906                 return -ENXIO;
907
908         if (!dchan)
909                 return -EINVAL;
910
911         chan = to_fsl_chan(dchan);
912
913         /* Halt the DMA engine */
914         dma_halt(chan);
915
916         spin_lock_irqsave(&chan->desc_lock, flags);
917
918         /* Remove and free all of the descriptors in the LD queue */
919         fsldma_free_desc_list(chan, &chan->ld_pending);
920         fsldma_free_desc_list(chan, &chan->ld_running);
921
922         spin_unlock_irqrestore(&chan->desc_lock, flags);
923
924         return 0;
925 }
926
927 /**
928  * fsl_dma_update_completed_cookie - Update the completed cookie.
929  * @chan : Freescale DMA channel
930  *
931  * CONTEXT: hardirq
932  */
933 static void fsl_dma_update_completed_cookie(struct fsldma_chan *chan)
934 {
935         struct fsl_desc_sw *desc;
936         unsigned long flags;
937         dma_cookie_t cookie;
938
939         spin_lock_irqsave(&chan->desc_lock, flags);
940
941         if (list_empty(&chan->ld_running)) {
942                 dev_dbg(chan->dev, "no running descriptors\n");
943                 goto out_unlock;
944         }
945
946         /* Get the last descriptor, update the cookie to that */
947         desc = to_fsl_desc(chan->ld_running.prev);
948         if (dma_is_idle(chan))
949                 cookie = desc->async_tx.cookie;
950         else {
951                 cookie = desc->async_tx.cookie - 1;
952                 if (unlikely(cookie < DMA_MIN_COOKIE))
953                         cookie = DMA_MAX_COOKIE;
954         }
955
956         chan->completed_cookie = cookie;
957
958 out_unlock:
959         spin_unlock_irqrestore(&chan->desc_lock, flags);
960 }
961
962 /**
963  * fsldma_desc_status - Check the status of a descriptor
964  * @chan: Freescale DMA channel
965  * @desc: DMA SW descriptor
966  *
967  * This function will return the status of the given descriptor
968  */
969 static enum dma_status fsldma_desc_status(struct fsldma_chan *chan,
970                                           struct fsl_desc_sw *desc)
971 {
972         return dma_async_is_complete(desc->async_tx.cookie,
973                                      chan->completed_cookie,
974                                      chan->common.cookie);
975 }
976
977 /**
978  * fsl_chan_ld_cleanup - Clean up link descriptors
979  * @chan : Freescale DMA channel
980  *
981  * This function clean up the ld_queue of DMA channel.
982  */
983 static void fsl_chan_ld_cleanup(struct fsldma_chan *chan)
984 {
985         struct fsl_desc_sw *desc, *_desc;
986         unsigned long flags;
987
988         spin_lock_irqsave(&chan->desc_lock, flags);
989
990         dev_dbg(chan->dev, "chan completed_cookie = %d\n", chan->completed_cookie);
991         list_for_each_entry_safe(desc, _desc, &chan->ld_running, node) {
992                 dma_async_tx_callback callback;
993                 void *callback_param;
994
995                 if (fsldma_desc_status(chan, desc) == DMA_IN_PROGRESS)
996                         break;
997
998                 /* Remove from the list of running transactions */
999                 list_del(&desc->node);
1000
1001                 /* Run the link descriptor callback function */
1002                 callback = desc->async_tx.callback;
1003                 callback_param = desc->async_tx.callback_param;
1004                 if (callback) {
1005                         spin_unlock_irqrestore(&chan->desc_lock, flags);
1006                         dev_dbg(chan->dev, "LD %p callback\n", desc);
1007                         callback(callback_param);
1008                         spin_lock_irqsave(&chan->desc_lock, flags);
1009                 }
1010
1011                 /* Run any dependencies, then free the descriptor */
1012                 dma_run_dependencies(&desc->async_tx);
1013                 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
1014         }
1015
1016         spin_unlock_irqrestore(&chan->desc_lock, flags);
1017 }
1018
1019 /**
1020  * fsl_chan_xfer_ld_queue - transfer any pending transactions
1021  * @chan : Freescale DMA channel
1022  *
1023  * This will make sure that any pending transactions will be run.
1024  * If the DMA controller is idle, it will be started. Otherwise,
1025  * the DMA controller's interrupt handler will start any pending
1026  * transactions when it becomes idle.
1027  */
1028 static void fsl_chan_xfer_ld_queue(struct fsldma_chan *chan)
1029 {
1030         struct fsl_desc_sw *desc;
1031         unsigned long flags;
1032
1033         spin_lock_irqsave(&chan->desc_lock, flags);
1034
1035         /*
1036          * If the list of pending descriptors is empty, then we
1037          * don't need to do any work at all
1038          */
1039         if (list_empty(&chan->ld_pending)) {
1040                 dev_dbg(chan->dev, "no pending LDs\n");
1041                 goto out_unlock;
1042         }
1043
1044         /*
1045          * The DMA controller is not idle, which means the interrupt
1046          * handler will start any queued transactions when it runs
1047          * at the end of the current transaction
1048          */
1049         if (!dma_is_idle(chan)) {
1050                 dev_dbg(chan->dev, "DMA controller still busy\n");
1051                 goto out_unlock;
1052         }
1053
1054         /*
1055          * TODO:
1056          * make sure the dma_halt() function really un-wedges the
1057          * controller as much as possible
1058          */
1059         dma_halt(chan);
1060
1061         /*
1062          * If there are some link descriptors which have not been
1063          * transferred, we need to start the controller
1064          */
1065
1066         /*
1067          * Move all elements from the queue of pending transactions
1068          * onto the list of running transactions
1069          */
1070         desc = list_first_entry(&chan->ld_pending, struct fsl_desc_sw, node);
1071         list_splice_tail_init(&chan->ld_pending, &chan->ld_running);
1072
1073         /*
1074          * Program the descriptor's address into the DMA controller,
1075          * then start the DMA transaction
1076          */
1077         set_cdar(chan, desc->async_tx.phys);
1078         dma_start(chan);
1079
1080 out_unlock:
1081         spin_unlock_irqrestore(&chan->desc_lock, flags);
1082 }
1083
1084 /**
1085  * fsl_dma_memcpy_issue_pending - Issue the DMA start command
1086  * @chan : Freescale DMA channel
1087  */
1088 static void fsl_dma_memcpy_issue_pending(struct dma_chan *dchan)
1089 {
1090         struct fsldma_chan *chan = to_fsl_chan(dchan);
1091         fsl_chan_xfer_ld_queue(chan);
1092 }
1093
1094 /**
1095  * fsl_tx_status - Determine the DMA status
1096  * @chan : Freescale DMA channel
1097  */
1098 static enum dma_status fsl_tx_status(struct dma_chan *dchan,
1099                                         dma_cookie_t cookie,
1100                                         struct dma_tx_state *txstate)
1101 {
1102         struct fsldma_chan *chan = to_fsl_chan(dchan);
1103         dma_cookie_t last_used;
1104         dma_cookie_t last_complete;
1105
1106         fsl_chan_ld_cleanup(chan);
1107
1108         last_used = dchan->cookie;
1109         last_complete = chan->completed_cookie;
1110
1111         dma_set_tx_state(txstate, last_complete, last_used, 0);
1112
1113         return dma_async_is_complete(cookie, last_complete, last_used);
1114 }
1115
1116 /*----------------------------------------------------------------------------*/
1117 /* Interrupt Handling                                                         */
1118 /*----------------------------------------------------------------------------*/
1119
1120 static irqreturn_t fsldma_chan_irq(int irq, void *data)
1121 {
1122         struct fsldma_chan *chan = data;
1123         int update_cookie = 0;
1124         int xfer_ld_q = 0;
1125         u32 stat;
1126
1127         /* save and clear the status register */
1128         stat = get_sr(chan);
1129         set_sr(chan, stat);
1130         dev_dbg(chan->dev, "irq: channel %d, stat = 0x%x\n", chan->id, stat);
1131
1132         stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);
1133         if (!stat)
1134                 return IRQ_NONE;
1135
1136         if (stat & FSL_DMA_SR_TE)
1137                 dev_err(chan->dev, "Transfer Error!\n");
1138
1139         /*
1140          * Programming Error
1141          * The DMA_INTERRUPT async_tx is a NULL transfer, which will
1142          * triger a PE interrupt.
1143          */
1144         if (stat & FSL_DMA_SR_PE) {
1145                 dev_dbg(chan->dev, "irq: Programming Error INT\n");
1146                 if (get_bcr(chan) == 0) {
1147                         /* BCR register is 0, this is a DMA_INTERRUPT async_tx.
1148                          * Now, update the completed cookie, and continue the
1149                          * next uncompleted transfer.
1150                          */
1151                         update_cookie = 1;
1152                         xfer_ld_q = 1;
1153                 }
1154                 stat &= ~FSL_DMA_SR_PE;
1155         }
1156
1157         /*
1158          * If the link descriptor segment transfer finishes,
1159          * we will recycle the used descriptor.
1160          */
1161         if (stat & FSL_DMA_SR_EOSI) {
1162                 dev_dbg(chan->dev, "irq: End-of-segments INT\n");
1163                 dev_dbg(chan->dev, "irq: clndar 0x%llx, nlndar 0x%llx\n",
1164                         (unsigned long long)get_cdar(chan),
1165                         (unsigned long long)get_ndar(chan));
1166                 stat &= ~FSL_DMA_SR_EOSI;
1167                 update_cookie = 1;
1168         }
1169
1170         /*
1171          * For MPC8349, EOCDI event need to update cookie
1172          * and start the next transfer if it exist.
1173          */
1174         if (stat & FSL_DMA_SR_EOCDI) {
1175                 dev_dbg(chan->dev, "irq: End-of-Chain link INT\n");
1176                 stat &= ~FSL_DMA_SR_EOCDI;
1177                 update_cookie = 1;
1178                 xfer_ld_q = 1;
1179         }
1180
1181         /*
1182          * If it current transfer is the end-of-transfer,
1183          * we should clear the Channel Start bit for
1184          * prepare next transfer.
1185          */
1186         if (stat & FSL_DMA_SR_EOLNI) {
1187                 dev_dbg(chan->dev, "irq: End-of-link INT\n");
1188                 stat &= ~FSL_DMA_SR_EOLNI;
1189                 xfer_ld_q = 1;
1190         }
1191
1192         if (update_cookie)
1193                 fsl_dma_update_completed_cookie(chan);
1194         if (xfer_ld_q)
1195                 fsl_chan_xfer_ld_queue(chan);
1196         if (stat)
1197                 dev_dbg(chan->dev, "irq: unhandled sr 0x%02x\n", stat);
1198
1199         dev_dbg(chan->dev, "irq: Exit\n");
1200         tasklet_schedule(&chan->tasklet);
1201         return IRQ_HANDLED;
1202 }
1203
1204 static void dma_do_tasklet(unsigned long data)
1205 {
1206         struct fsldma_chan *chan = (struct fsldma_chan *)data;
1207         fsl_chan_ld_cleanup(chan);
1208 }
1209
1210 static irqreturn_t fsldma_ctrl_irq(int irq, void *data)
1211 {
1212         struct fsldma_device *fdev = data;
1213         struct fsldma_chan *chan;
1214         unsigned int handled = 0;
1215         u32 gsr, mask;
1216         int i;
1217
1218         gsr = (fdev->feature & FSL_DMA_BIG_ENDIAN) ? in_be32(fdev->regs)
1219                                                    : in_le32(fdev->regs);
1220         mask = 0xff000000;
1221         dev_dbg(fdev->dev, "IRQ: gsr 0x%.8x\n", gsr);
1222
1223         for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
1224                 chan = fdev->chan[i];
1225                 if (!chan)
1226                         continue;
1227
1228                 if (gsr & mask) {
1229                         dev_dbg(fdev->dev, "IRQ: chan %d\n", chan->id);
1230                         fsldma_chan_irq(irq, chan);
1231                         handled++;
1232                 }
1233
1234                 gsr &= ~mask;
1235                 mask >>= 8;
1236         }
1237
1238         return IRQ_RETVAL(handled);
1239 }
1240
1241 static void fsldma_free_irqs(struct fsldma_device *fdev)
1242 {
1243         struct fsldma_chan *chan;
1244         int i;
1245
1246         if (fdev->irq != NO_IRQ) {
1247                 dev_dbg(fdev->dev, "free per-controller IRQ\n");
1248                 free_irq(fdev->irq, fdev);
1249                 return;
1250         }
1251
1252         for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
1253                 chan = fdev->chan[i];
1254                 if (chan && chan->irq != NO_IRQ) {
1255                         dev_dbg(fdev->dev, "free channel %d IRQ\n", chan->id);
1256                         free_irq(chan->irq, chan);
1257                 }
1258         }
1259 }
1260
1261 static int fsldma_request_irqs(struct fsldma_device *fdev)
1262 {
1263         struct fsldma_chan *chan;
1264         int ret;
1265         int i;
1266
1267         /* if we have a per-controller IRQ, use that */
1268         if (fdev->irq != NO_IRQ) {
1269                 dev_dbg(fdev->dev, "request per-controller IRQ\n");
1270                 ret = request_irq(fdev->irq, fsldma_ctrl_irq, IRQF_SHARED,
1271                                   "fsldma-controller", fdev);
1272                 return ret;
1273         }
1274
1275         /* no per-controller IRQ, use the per-channel IRQs */
1276         for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
1277                 chan = fdev->chan[i];
1278                 if (!chan)
1279                         continue;
1280
1281                 if (chan->irq == NO_IRQ) {
1282                         dev_err(fdev->dev, "no interrupts property defined for "
1283                                            "DMA channel %d. Please fix your "
1284                                            "device tree\n", chan->id);
1285                         ret = -ENODEV;
1286                         goto out_unwind;
1287                 }
1288
1289                 dev_dbg(fdev->dev, "request channel %d IRQ\n", chan->id);
1290                 ret = request_irq(chan->irq, fsldma_chan_irq, IRQF_SHARED,
1291                                   "fsldma-chan", chan);
1292                 if (ret) {
1293                         dev_err(fdev->dev, "unable to request IRQ for DMA "
1294                                            "channel %d\n", chan->id);
1295                         goto out_unwind;
1296                 }
1297         }
1298
1299         return 0;
1300
1301 out_unwind:
1302         for (/* none */; i >= 0; i--) {
1303                 chan = fdev->chan[i];
1304                 if (!chan)
1305                         continue;
1306
1307                 if (chan->irq == NO_IRQ)
1308                         continue;
1309
1310                 free_irq(chan->irq, chan);
1311         }
1312
1313         return ret;
1314 }
1315
1316 /*----------------------------------------------------------------------------*/
1317 /* OpenFirmware Subsystem                                                     */
1318 /*----------------------------------------------------------------------------*/
1319
1320 static int __devinit fsl_dma_chan_probe(struct fsldma_device *fdev,
1321         struct device_node *node, u32 feature, const char *compatible)
1322 {
1323         struct fsldma_chan *chan;
1324         struct resource res;
1325         int err;
1326
1327         /* alloc channel */
1328         chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1329         if (!chan) {
1330                 dev_err(fdev->dev, "no free memory for DMA channels!\n");
1331                 err = -ENOMEM;
1332                 goto out_return;
1333         }
1334
1335         /* ioremap registers for use */
1336         chan->regs = of_iomap(node, 0);
1337         if (!chan->regs) {
1338                 dev_err(fdev->dev, "unable to ioremap registers\n");
1339                 err = -ENOMEM;
1340                 goto out_free_chan;
1341         }
1342
1343         err = of_address_to_resource(node, 0, &res);
1344         if (err) {
1345                 dev_err(fdev->dev, "unable to find 'reg' property\n");
1346                 goto out_iounmap_regs;
1347         }
1348
1349         chan->feature = feature;
1350         if (!fdev->feature)
1351                 fdev->feature = chan->feature;
1352
1353         /*
1354          * If the DMA device's feature is different than the feature
1355          * of its channels, report the bug
1356          */
1357         WARN_ON(fdev->feature != chan->feature);
1358
1359         chan->dev = fdev->dev;
1360         chan->id = ((res.start - 0x100) & 0xfff) >> 7;
1361         if (chan->id >= FSL_DMA_MAX_CHANS_PER_DEVICE) {
1362                 dev_err(fdev->dev, "too many channels for device\n");
1363                 err = -EINVAL;
1364                 goto out_iounmap_regs;
1365         }
1366
1367         fdev->chan[chan->id] = chan;
1368         tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
1369
1370         /* Initialize the channel */
1371         dma_init(chan);
1372
1373         /* Clear cdar registers */
1374         set_cdar(chan, 0);
1375
1376         switch (chan->feature & FSL_DMA_IP_MASK) {
1377         case FSL_DMA_IP_85XX:
1378                 chan->toggle_ext_pause = fsl_chan_toggle_ext_pause;
1379         case FSL_DMA_IP_83XX:
1380                 chan->toggle_ext_start = fsl_chan_toggle_ext_start;
1381                 chan->set_src_loop_size = fsl_chan_set_src_loop_size;
1382                 chan->set_dst_loop_size = fsl_chan_set_dst_loop_size;
1383                 chan->set_request_count = fsl_chan_set_request_count;
1384         }
1385
1386         spin_lock_init(&chan->desc_lock);
1387         INIT_LIST_HEAD(&chan->ld_pending);
1388         INIT_LIST_HEAD(&chan->ld_running);
1389
1390         chan->common.device = &fdev->common;
1391
1392         /* find the IRQ line, if it exists in the device tree */
1393         chan->irq = irq_of_parse_and_map(node, 0);
1394
1395         /* Add the channel to DMA device channel list */
1396         list_add_tail(&chan->common.device_node, &fdev->common.channels);
1397         fdev->common.chancnt++;
1398
1399         dev_info(fdev->dev, "#%d (%s), irq %d\n", chan->id, compatible,
1400                  chan->irq != NO_IRQ ? chan->irq : fdev->irq);
1401
1402         return 0;
1403
1404 out_iounmap_regs:
1405         iounmap(chan->regs);
1406 out_free_chan:
1407         kfree(chan);
1408 out_return:
1409         return err;
1410 }
1411
1412 static void fsl_dma_chan_remove(struct fsldma_chan *chan)
1413 {
1414         irq_dispose_mapping(chan->irq);
1415         list_del(&chan->common.device_node);
1416         iounmap(chan->regs);
1417         kfree(chan);
1418 }
1419
1420 static int __devinit fsldma_of_probe(struct platform_device *op,
1421                         const struct of_device_id *match)
1422 {
1423         struct fsldma_device *fdev;
1424         struct device_node *child;
1425         int err;
1426
1427         fdev = kzalloc(sizeof(*fdev), GFP_KERNEL);
1428         if (!fdev) {
1429                 dev_err(&op->dev, "No enough memory for 'priv'\n");
1430                 err = -ENOMEM;
1431                 goto out_return;
1432         }
1433
1434         fdev->dev = &op->dev;
1435         INIT_LIST_HEAD(&fdev->common.channels);
1436
1437         /* ioremap the registers for use */
1438         fdev->regs = of_iomap(op->dev.of_node, 0);
1439         if (!fdev->regs) {
1440                 dev_err(&op->dev, "unable to ioremap registers\n");
1441                 err = -ENOMEM;
1442                 goto out_free_fdev;
1443         }
1444
1445         /* map the channel IRQ if it exists, but don't hookup the handler yet */
1446         fdev->irq = irq_of_parse_and_map(op->dev.of_node, 0);
1447
1448         dma_cap_set(DMA_MEMCPY, fdev->common.cap_mask);
1449         dma_cap_set(DMA_INTERRUPT, fdev->common.cap_mask);
1450         dma_cap_set(DMA_SG, fdev->common.cap_mask);
1451         dma_cap_set(DMA_SLAVE, fdev->common.cap_mask);
1452         fdev->common.device_alloc_chan_resources = fsl_dma_alloc_chan_resources;
1453         fdev->common.device_free_chan_resources = fsl_dma_free_chan_resources;
1454         fdev->common.device_prep_dma_interrupt = fsl_dma_prep_interrupt;
1455         fdev->common.device_prep_dma_memcpy = fsl_dma_prep_memcpy;
1456         fdev->common.device_prep_dma_sg = fsl_dma_prep_sg;
1457         fdev->common.device_tx_status = fsl_tx_status;
1458         fdev->common.device_issue_pending = fsl_dma_memcpy_issue_pending;
1459         fdev->common.device_prep_slave_sg = fsl_dma_prep_slave_sg;
1460         fdev->common.device_control = fsl_dma_device_control;
1461         fdev->common.dev = &op->dev;
1462
1463         dev_set_drvdata(&op->dev, fdev);
1464
1465         /*
1466          * We cannot use of_platform_bus_probe() because there is no
1467          * of_platform_bus_remove(). Instead, we manually instantiate every DMA
1468          * channel object.
1469          */
1470         for_each_child_of_node(op->dev.of_node, child) {
1471                 if (of_device_is_compatible(child, "fsl,eloplus-dma-channel")) {
1472                         fsl_dma_chan_probe(fdev, child,
1473                                 FSL_DMA_IP_85XX | FSL_DMA_BIG_ENDIAN,
1474                                 "fsl,eloplus-dma-channel");
1475                 }
1476
1477                 if (of_device_is_compatible(child, "fsl,elo-dma-channel")) {
1478                         fsl_dma_chan_probe(fdev, child,
1479                                 FSL_DMA_IP_83XX | FSL_DMA_LITTLE_ENDIAN,
1480                                 "fsl,elo-dma-channel");
1481                 }
1482         }
1483
1484         /*
1485          * Hookup the IRQ handler(s)
1486          *
1487          * If we have a per-controller interrupt, we prefer that to the
1488          * per-channel interrupts to reduce the number of shared interrupt
1489          * handlers on the same IRQ line
1490          */
1491         err = fsldma_request_irqs(fdev);
1492         if (err) {
1493                 dev_err(fdev->dev, "unable to request IRQs\n");
1494                 goto out_free_fdev;
1495         }
1496
1497         dma_async_device_register(&fdev->common);
1498         return 0;
1499
1500 out_free_fdev:
1501         irq_dispose_mapping(fdev->irq);
1502         kfree(fdev);
1503 out_return:
1504         return err;
1505 }
1506
1507 static int fsldma_of_remove(struct platform_device *op)
1508 {
1509         struct fsldma_device *fdev;
1510         unsigned int i;
1511
1512         fdev = dev_get_drvdata(&op->dev);
1513         dma_async_device_unregister(&fdev->common);
1514
1515         fsldma_free_irqs(fdev);
1516
1517         for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
1518                 if (fdev->chan[i])
1519                         fsl_dma_chan_remove(fdev->chan[i]);
1520         }
1521
1522         iounmap(fdev->regs);
1523         dev_set_drvdata(&op->dev, NULL);
1524         kfree(fdev);
1525
1526         return 0;
1527 }
1528
1529 static const struct of_device_id fsldma_of_ids[] = {
1530         { .compatible = "fsl,eloplus-dma", },
1531         { .compatible = "fsl,elo-dma", },
1532         {}
1533 };
1534
1535 static struct of_platform_driver fsldma_of_driver = {
1536         .driver = {
1537                 .name = "fsl-elo-dma",
1538                 .owner = THIS_MODULE,
1539                 .of_match_table = fsldma_of_ids,
1540         },
1541         .probe = fsldma_of_probe,
1542         .remove = fsldma_of_remove,
1543 };
1544
1545 /*----------------------------------------------------------------------------*/
1546 /* Module Init / Exit                                                         */
1547 /*----------------------------------------------------------------------------*/
1548
1549 static __init int fsldma_init(void)
1550 {
1551         int ret;
1552
1553         pr_info("Freescale Elo / Elo Plus DMA driver\n");
1554
1555         ret = of_register_platform_driver(&fsldma_of_driver);
1556         if (ret)
1557                 pr_err("fsldma: failed to register platform driver\n");
1558
1559         return ret;
1560 }
1561
1562 static void __exit fsldma_exit(void)
1563 {
1564         of_unregister_platform_driver(&fsldma_of_driver);
1565 }
1566
1567 subsys_initcall(fsldma_init);
1568 module_exit(fsldma_exit);
1569
1570 MODULE_DESCRIPTION("Freescale Elo / Elo Plus DMA driver");
1571 MODULE_LICENSE("GPL");