]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/dma/edma.c
Merge remote-tracking branch 'slave-dma/next'
[karo-tx-linux.git] / drivers / dma / edma.c
1 /*
2  * TI EDMA DMA engine driver
3  *
4  * Copyright 2012 Texas Instruments
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation version 2.
9  *
10  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11  * kind, whether express or implied; without even the implied warranty
12  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <linux/dmaengine.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26
27 #include <linux/platform_data/edma.h>
28
29 #include "dmaengine.h"
30 #include "virt-dma.h"
31
32 /*
33  * This will go away when the private EDMA API is folded
34  * into this driver and the platform device(s) are
35  * instantiated in the arch code. We can only get away
36  * with this simplification because DA8XX may not be built
37  * in the same kernel image with other DaVinci parts. This
38  * avoids having to sprinkle dmaengine driver platform devices
39  * and data throughout all the existing board files.
40  */
41 #ifdef CONFIG_ARCH_DAVINCI_DA8XX
42 #define EDMA_CTLRS      2
43 #define EDMA_CHANS      32
44 #else
45 #define EDMA_CTLRS      1
46 #define EDMA_CHANS      64
47 #endif /* CONFIG_ARCH_DAVINCI_DA8XX */
48
49 /*
50  * Max of 20 segments per channel to conserve PaRAM slots
51  * Also note that MAX_NR_SG should be atleast the no.of periods
52  * that are required for ASoC, otherwise DMA prep calls will
53  * fail. Today davinci-pcm is the only user of this driver and
54  * requires atleast 17 slots, so we setup the default to 20.
55  */
56 #define MAX_NR_SG               20
57 #define EDMA_MAX_SLOTS          MAX_NR_SG
58 #define EDMA_DESCRIPTORS        16
59
60 struct edma_desc {
61         struct virt_dma_desc            vdesc;
62         struct list_head                node;
63         int                             absync;
64         int                             pset_nr;
65         int                             processed;
66         struct edmacc_param             pset[0];
67 };
68
69 struct edma_cc;
70
71 struct edma_chan {
72         struct virt_dma_chan            vchan;
73         struct list_head                node;
74         struct edma_desc                *edesc;
75         struct edma_cc                  *ecc;
76         int                             ch_num;
77         bool                            alloced;
78         int                             slot[EDMA_MAX_SLOTS];
79         int                             missed;
80         struct dma_slave_config         cfg;
81 };
82
83 struct edma_cc {
84         int                             ctlr;
85         struct dma_device               dma_slave;
86         struct edma_chan                slave_chans[EDMA_CHANS];
87         int                             num_slave_chans;
88         int                             dummy_slot;
89 };
90
91 static inline struct edma_cc *to_edma_cc(struct dma_device *d)
92 {
93         return container_of(d, struct edma_cc, dma_slave);
94 }
95
96 static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
97 {
98         return container_of(c, struct edma_chan, vchan.chan);
99 }
100
101 static inline struct edma_desc
102 *to_edma_desc(struct dma_async_tx_descriptor *tx)
103 {
104         return container_of(tx, struct edma_desc, vdesc.tx);
105 }
106
107 static void edma_desc_free(struct virt_dma_desc *vdesc)
108 {
109         kfree(container_of(vdesc, struct edma_desc, vdesc));
110 }
111
112 /* Dispatch a queued descriptor to the controller (caller holds lock) */
113 static void edma_execute(struct edma_chan *echan)
114 {
115         struct virt_dma_desc *vdesc;
116         struct edma_desc *edesc;
117         struct device *dev = echan->vchan.chan.device->dev;
118         int i, j, left, nslots;
119
120         /* If either we processed all psets or we're still not started */
121         if (!echan->edesc ||
122             echan->edesc->pset_nr == echan->edesc->processed) {
123                 /* Get next vdesc */
124                 vdesc = vchan_next_desc(&echan->vchan);
125                 if (!vdesc) {
126                         echan->edesc = NULL;
127                         return;
128                 }
129                 list_del(&vdesc->node);
130                 echan->edesc = to_edma_desc(&vdesc->tx);
131         }
132
133         edesc = echan->edesc;
134
135         /* Find out how many left */
136         left = edesc->pset_nr - edesc->processed;
137         nslots = min(MAX_NR_SG, left);
138
139         /* Write descriptor PaRAM set(s) */
140         for (i = 0; i < nslots; i++) {
141                 j = i + edesc->processed;
142                 edma_write_slot(echan->slot[i], &edesc->pset[j]);
143                 dev_dbg(echan->vchan.chan.device->dev,
144                         "\n pset[%d]:\n"
145                         "  chnum\t%d\n"
146                         "  slot\t%d\n"
147                         "  opt\t%08x\n"
148                         "  src\t%08x\n"
149                         "  dst\t%08x\n"
150                         "  abcnt\t%08x\n"
151                         "  ccnt\t%08x\n"
152                         "  bidx\t%08x\n"
153                         "  cidx\t%08x\n"
154                         "  lkrld\t%08x\n",
155                         j, echan->ch_num, echan->slot[i],
156                         edesc->pset[j].opt,
157                         edesc->pset[j].src,
158                         edesc->pset[j].dst,
159                         edesc->pset[j].a_b_cnt,
160                         edesc->pset[j].ccnt,
161                         edesc->pset[j].src_dst_bidx,
162                         edesc->pset[j].src_dst_cidx,
163                         edesc->pset[j].link_bcntrld);
164                 /* Link to the previous slot if not the last set */
165                 if (i != (nslots - 1))
166                         edma_link(echan->slot[i], echan->slot[i+1]);
167         }
168
169         edesc->processed += nslots;
170
171         /*
172          * If this is either the last set in a set of SG-list transactions
173          * then setup a link to the dummy slot, this results in all future
174          * events being absorbed and that's OK because we're done
175          */
176         if (edesc->processed == edesc->pset_nr)
177                 edma_link(echan->slot[nslots-1], echan->ecc->dummy_slot);
178
179         edma_resume(echan->ch_num);
180
181         if (edesc->processed <= MAX_NR_SG) {
182                 dev_dbg(dev, "first transfer starting %d\n", echan->ch_num);
183                 edma_start(echan->ch_num);
184         }
185
186         /*
187          * This happens due to setup times between intermediate transfers
188          * in long SG lists which have to be broken up into transfers of
189          * MAX_NR_SG
190          */
191         if (echan->missed) {
192                 dev_dbg(dev, "missed event in execute detected\n");
193                 edma_clean_channel(echan->ch_num);
194                 edma_stop(echan->ch_num);
195                 edma_start(echan->ch_num);
196                 edma_trigger_channel(echan->ch_num);
197                 echan->missed = 0;
198         }
199 }
200
201 static int edma_terminate_all(struct edma_chan *echan)
202 {
203         unsigned long flags;
204         LIST_HEAD(head);
205
206         spin_lock_irqsave(&echan->vchan.lock, flags);
207
208         /*
209          * Stop DMA activity: we assume the callback will not be called
210          * after edma_dma() returns (even if it does, it will see
211          * echan->edesc is NULL and exit.)
212          */
213         if (echan->edesc) {
214                 echan->edesc = NULL;
215                 edma_stop(echan->ch_num);
216         }
217
218         vchan_get_all_descriptors(&echan->vchan, &head);
219         spin_unlock_irqrestore(&echan->vchan.lock, flags);
220         vchan_dma_desc_free_list(&echan->vchan, &head);
221
222         return 0;
223 }
224
225 static int edma_slave_config(struct edma_chan *echan,
226         struct dma_slave_config *cfg)
227 {
228         if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
229             cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
230                 return -EINVAL;
231
232         memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
233
234         return 0;
235 }
236
237 static int edma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
238                         unsigned long arg)
239 {
240         int ret = 0;
241         struct dma_slave_config *config;
242         struct edma_chan *echan = to_edma_chan(chan);
243
244         switch (cmd) {
245         case DMA_TERMINATE_ALL:
246                 edma_terminate_all(echan);
247                 break;
248         case DMA_SLAVE_CONFIG:
249                 config = (struct dma_slave_config *)arg;
250                 ret = edma_slave_config(echan, config);
251                 break;
252         default:
253                 ret = -ENOSYS;
254         }
255
256         return ret;
257 }
258
259 /*
260  * A PaRAM set configuration abstraction used by other modes
261  * @chan: Channel who's PaRAM set we're configuring
262  * @pset: PaRAM set to initialize and setup.
263  * @src_addr: Source address of the DMA
264  * @dst_addr: Destination address of the DMA
265  * @burst: In units of dev_width, how much to send
266  * @dev_width: How much is the dev_width
267  * @dma_length: Total length of the DMA transfer
268  * @direction: Direction of the transfer
269  */
270 static int edma_config_pset(struct dma_chan *chan, struct edmacc_param *pset,
271         dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
272         enum dma_slave_buswidth dev_width, unsigned int dma_length,
273         enum dma_transfer_direction direction)
274 {
275         struct edma_chan *echan = to_edma_chan(chan);
276         struct device *dev = chan->device->dev;
277         int acnt, bcnt, ccnt, cidx;
278         int src_bidx, dst_bidx, src_cidx, dst_cidx;
279         int absync;
280
281         acnt = dev_width;
282         /*
283          * If the maxburst is equal to the fifo width, use
284          * A-synced transfers. This allows for large contiguous
285          * buffer transfers using only one PaRAM set.
286          */
287         if (burst == 1) {
288                 /*
289                  * For the A-sync case, bcnt and ccnt are the remainder
290                  * and quotient respectively of the division of:
291                  * (dma_length / acnt) by (SZ_64K -1). This is so
292                  * that in case bcnt over flows, we have ccnt to use.
293                  * Note: In A-sync tranfer only, bcntrld is used, but it
294                  * only applies for sg_dma_len(sg) >= SZ_64K.
295                  * In this case, the best way adopted is- bccnt for the
296                  * first frame will be the remainder below. Then for
297                  * every successive frame, bcnt will be SZ_64K-1. This
298                  * is assured as bcntrld = 0xffff in end of function.
299                  */
300                 absync = false;
301                 ccnt = dma_length / acnt / (SZ_64K - 1);
302                 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
303                 /*
304                  * If bcnt is non-zero, we have a remainder and hence an
305                  * extra frame to transfer, so increment ccnt.
306                  */
307                 if (bcnt)
308                         ccnt++;
309                 else
310                         bcnt = SZ_64K - 1;
311                 cidx = acnt;
312         } else {
313                 /*
314                  * If maxburst is greater than the fifo address_width,
315                  * use AB-synced transfers where A count is the fifo
316                  * address_width and B count is the maxburst. In this
317                  * case, we are limited to transfers of C count frames
318                  * of (address_width * maxburst) where C count is limited
319                  * to SZ_64K-1. This places an upper bound on the length
320                  * of an SG segment that can be handled.
321                  */
322                 absync = true;
323                 bcnt = burst;
324                 ccnt = dma_length / (acnt * bcnt);
325                 if (ccnt > (SZ_64K - 1)) {
326                         dev_err(dev, "Exceeded max SG segment size\n");
327                         return -EINVAL;
328                 }
329                 cidx = acnt * bcnt;
330         }
331
332         if (direction == DMA_MEM_TO_DEV) {
333                 src_bidx = acnt;
334                 src_cidx = cidx;
335                 dst_bidx = 0;
336                 dst_cidx = 0;
337         } else if (direction == DMA_DEV_TO_MEM)  {
338                 src_bidx = 0;
339                 src_cidx = 0;
340                 dst_bidx = acnt;
341                 dst_cidx = cidx;
342         } else {
343                 dev_err(dev, "%s: direction not implemented yet\n", __func__);
344                 return -EINVAL;
345         }
346
347         pset->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
348         /* Configure A or AB synchronized transfers */
349         if (absync)
350                 pset->opt |= SYNCDIM;
351
352         pset->src = src_addr;
353         pset->dst = dst_addr;
354
355         pset->src_dst_bidx = (dst_bidx << 16) | src_bidx;
356         pset->src_dst_cidx = (dst_cidx << 16) | src_cidx;
357
358         pset->a_b_cnt = bcnt << 16 | acnt;
359         pset->ccnt = ccnt;
360         /*
361          * Only time when (bcntrld) auto reload is required is for
362          * A-sync case, and in this case, a requirement of reload value
363          * of SZ_64K-1 only is assured. 'link' is initially set to NULL
364          * and then later will be populated by edma_execute.
365          */
366         pset->link_bcntrld = 0xffffffff;
367         return absync;
368 }
369
370 static struct dma_async_tx_descriptor *edma_prep_slave_sg(
371         struct dma_chan *chan, struct scatterlist *sgl,
372         unsigned int sg_len, enum dma_transfer_direction direction,
373         unsigned long tx_flags, void *context)
374 {
375         struct edma_chan *echan = to_edma_chan(chan);
376         struct device *dev = chan->device->dev;
377         struct edma_desc *edesc;
378         dma_addr_t src_addr = 0, dst_addr = 0;
379         enum dma_slave_buswidth dev_width;
380         u32 burst;
381         struct scatterlist *sg;
382         int i, nslots, ret;
383
384         if (unlikely(!echan || !sgl || !sg_len))
385                 return NULL;
386
387         if (direction == DMA_DEV_TO_MEM) {
388                 src_addr = echan->cfg.src_addr;
389                 dev_width = echan->cfg.src_addr_width;
390                 burst = echan->cfg.src_maxburst;
391         } else if (direction == DMA_MEM_TO_DEV) {
392                 dst_addr = echan->cfg.dst_addr;
393                 dev_width = echan->cfg.dst_addr_width;
394                 burst = echan->cfg.dst_maxburst;
395         } else {
396                 dev_err(dev, "%s: bad direction?\n", __func__);
397                 return NULL;
398         }
399
400         if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
401                 dev_err(dev, "Undefined slave buswidth\n");
402                 return NULL;
403         }
404
405         edesc = kzalloc(sizeof(*edesc) + sg_len *
406                 sizeof(edesc->pset[0]), GFP_ATOMIC);
407         if (!edesc) {
408                 dev_dbg(dev, "Failed to allocate a descriptor\n");
409                 return NULL;
410         }
411
412         edesc->pset_nr = sg_len;
413
414         /* Allocate a PaRAM slot, if needed */
415         nslots = min_t(unsigned, MAX_NR_SG, sg_len);
416
417         for (i = 0; i < nslots; i++) {
418                 if (echan->slot[i] < 0) {
419                         echan->slot[i] =
420                                 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
421                                                 EDMA_SLOT_ANY);
422                         if (echan->slot[i] < 0) {
423                                 dev_err(dev, "Failed to allocate slot\n");
424                                 kfree(edesc);
425                                 return NULL;
426                         }
427                 }
428         }
429
430         /* Configure PaRAM sets for each SG */
431         for_each_sg(sgl, sg, sg_len, i) {
432                 /* Get address for each SG */
433                 if (direction == DMA_DEV_TO_MEM)
434                         dst_addr = sg_dma_address(sg);
435                 else
436                         src_addr = sg_dma_address(sg);
437
438                 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
439                                        dst_addr, burst, dev_width,
440                                        sg_dma_len(sg), direction);
441                 if (ret < 0)
442                         return NULL;
443
444                 edesc->absync = ret;
445
446                 /* If this is the last in a current SG set of transactions,
447                    enable interrupts so that next set is processed */
448                 if (!((i+1) % MAX_NR_SG))
449                         edesc->pset[i].opt |= TCINTEN;
450
451                 /* If this is the last set, enable completion interrupt flag */
452                 if (i == sg_len - 1)
453                         edesc->pset[i].opt |= TCINTEN;
454         }
455
456         return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
457 }
458
459 static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
460 {
461         struct edma_chan *echan = data;
462         struct device *dev = echan->vchan.chan.device->dev;
463         struct edma_desc *edesc;
464         unsigned long flags;
465         struct edmacc_param p;
466
467         /* Pause the channel */
468         edma_pause(echan->ch_num);
469
470         switch (ch_status) {
471         case DMA_COMPLETE:
472                 spin_lock_irqsave(&echan->vchan.lock, flags);
473
474                 edesc = echan->edesc;
475                 if (edesc) {
476                         if (edesc->processed == edesc->pset_nr) {
477                                 dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
478                                 edma_stop(echan->ch_num);
479                                 vchan_cookie_complete(&edesc->vdesc);
480                         } else {
481                                 dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
482                         }
483
484                         edma_execute(echan);
485                 }
486
487                 spin_unlock_irqrestore(&echan->vchan.lock, flags);
488
489                 break;
490         case DMA_CC_ERROR:
491                 spin_lock_irqsave(&echan->vchan.lock, flags);
492
493                 edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
494
495                 /*
496                  * Issue later based on missed flag which will be sure
497                  * to happen as:
498                  * (1) we finished transmitting an intermediate slot and
499                  *     edma_execute is coming up.
500                  * (2) or we finished current transfer and issue will
501                  *     call edma_execute.
502                  *
503                  * Important note: issuing can be dangerous here and
504                  * lead to some nasty recursion when we are in a NULL
505                  * slot. So we avoid doing so and set the missed flag.
506                  */
507                 if (p.a_b_cnt == 0 && p.ccnt == 0) {
508                         dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
509                         echan->missed = 1;
510                 } else {
511                         /*
512                          * The slot is already programmed but the event got
513                          * missed, so its safe to issue it here.
514                          */
515                         dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
516                         edma_clean_channel(echan->ch_num);
517                         edma_stop(echan->ch_num);
518                         edma_start(echan->ch_num);
519                         edma_trigger_channel(echan->ch_num);
520                 }
521
522                 spin_unlock_irqrestore(&echan->vchan.lock, flags);
523
524                 break;
525         default:
526                 break;
527         }
528 }
529
530 /* Alloc channel resources */
531 static int edma_alloc_chan_resources(struct dma_chan *chan)
532 {
533         struct edma_chan *echan = to_edma_chan(chan);
534         struct device *dev = chan->device->dev;
535         int ret;
536         int a_ch_num;
537         LIST_HEAD(descs);
538
539         a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
540                                         chan, EVENTQ_DEFAULT);
541
542         if (a_ch_num < 0) {
543                 ret = -ENODEV;
544                 goto err_no_chan;
545         }
546
547         if (a_ch_num != echan->ch_num) {
548                 dev_err(dev, "failed to allocate requested channel %u:%u\n",
549                         EDMA_CTLR(echan->ch_num),
550                         EDMA_CHAN_SLOT(echan->ch_num));
551                 ret = -ENODEV;
552                 goto err_wrong_chan;
553         }
554
555         echan->alloced = true;
556         echan->slot[0] = echan->ch_num;
557
558         dev_info(dev, "allocated channel for %u:%u\n",
559                  EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
560
561         return 0;
562
563 err_wrong_chan:
564         edma_free_channel(a_ch_num);
565 err_no_chan:
566         return ret;
567 }
568
569 /* Free channel resources */
570 static void edma_free_chan_resources(struct dma_chan *chan)
571 {
572         struct edma_chan *echan = to_edma_chan(chan);
573         struct device *dev = chan->device->dev;
574         int i;
575
576         /* Terminate transfers */
577         edma_stop(echan->ch_num);
578
579         vchan_free_chan_resources(&echan->vchan);
580
581         /* Free EDMA PaRAM slots */
582         for (i = 1; i < EDMA_MAX_SLOTS; i++) {
583                 if (echan->slot[i] >= 0) {
584                         edma_free_slot(echan->slot[i]);
585                         echan->slot[i] = -1;
586                 }
587         }
588
589         /* Free EDMA channel */
590         if (echan->alloced) {
591                 edma_free_channel(echan->ch_num);
592                 echan->alloced = false;
593         }
594
595         dev_info(dev, "freeing channel for %u\n", echan->ch_num);
596 }
597
598 /* Send pending descriptor to hardware */
599 static void edma_issue_pending(struct dma_chan *chan)
600 {
601         struct edma_chan *echan = to_edma_chan(chan);
602         unsigned long flags;
603
604         spin_lock_irqsave(&echan->vchan.lock, flags);
605         if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
606                 edma_execute(echan);
607         spin_unlock_irqrestore(&echan->vchan.lock, flags);
608 }
609
610 static size_t edma_desc_size(struct edma_desc *edesc)
611 {
612         int i;
613         size_t size;
614
615         if (edesc->absync)
616                 for (size = i = 0; i < edesc->pset_nr; i++)
617                         size += (edesc->pset[i].a_b_cnt & 0xffff) *
618                                 (edesc->pset[i].a_b_cnt >> 16) *
619                                  edesc->pset[i].ccnt;
620         else
621                 size = (edesc->pset[0].a_b_cnt & 0xffff) *
622                         (edesc->pset[0].a_b_cnt >> 16) +
623                         (edesc->pset[0].a_b_cnt & 0xffff) *
624                         (SZ_64K - 1) * edesc->pset[0].ccnt;
625
626         return size;
627 }
628
629 /* Check request completion status */
630 static enum dma_status edma_tx_status(struct dma_chan *chan,
631                                       dma_cookie_t cookie,
632                                       struct dma_tx_state *txstate)
633 {
634         struct edma_chan *echan = to_edma_chan(chan);
635         struct virt_dma_desc *vdesc;
636         enum dma_status ret;
637         unsigned long flags;
638
639         ret = dma_cookie_status(chan, cookie, txstate);
640         if (ret == DMA_COMPLETE || !txstate)
641                 return ret;
642
643         spin_lock_irqsave(&echan->vchan.lock, flags);
644         vdesc = vchan_find_desc(&echan->vchan, cookie);
645         if (vdesc) {
646                 txstate->residue = edma_desc_size(to_edma_desc(&vdesc->tx));
647         } else if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie) {
648                 struct edma_desc *edesc = echan->edesc;
649                 txstate->residue = edma_desc_size(edesc);
650         }
651         spin_unlock_irqrestore(&echan->vchan.lock, flags);
652
653         return ret;
654 }
655
656 static void __init edma_chan_init(struct edma_cc *ecc,
657                                   struct dma_device *dma,
658                                   struct edma_chan *echans)
659 {
660         int i, j;
661
662         for (i = 0; i < EDMA_CHANS; i++) {
663                 struct edma_chan *echan = &echans[i];
664                 echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
665                 echan->ecc = ecc;
666                 echan->vchan.desc_free = edma_desc_free;
667
668                 vchan_init(&echan->vchan, dma);
669
670                 INIT_LIST_HEAD(&echan->node);
671                 for (j = 0; j < EDMA_MAX_SLOTS; j++)
672                         echan->slot[j] = -1;
673         }
674 }
675
676 static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
677                           struct device *dev)
678 {
679         dma->device_prep_slave_sg = edma_prep_slave_sg;
680         dma->device_alloc_chan_resources = edma_alloc_chan_resources;
681         dma->device_free_chan_resources = edma_free_chan_resources;
682         dma->device_issue_pending = edma_issue_pending;
683         dma->device_tx_status = edma_tx_status;
684         dma->device_control = edma_control;
685         dma->dev = dev;
686
687         INIT_LIST_HEAD(&dma->channels);
688 }
689
690 static int edma_probe(struct platform_device *pdev)
691 {
692         struct edma_cc *ecc;
693         int ret;
694
695         ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
696         if (ret)
697                 return ret;
698
699         ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
700         if (!ecc) {
701                 dev_err(&pdev->dev, "Can't allocate controller\n");
702                 return -ENOMEM;
703         }
704
705         ecc->ctlr = pdev->id;
706         ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
707         if (ecc->dummy_slot < 0) {
708                 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
709                 return -EIO;
710         }
711
712         dma_cap_zero(ecc->dma_slave.cap_mask);
713         dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
714
715         edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
716
717         edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
718
719         ret = dma_async_device_register(&ecc->dma_slave);
720         if (ret)
721                 goto err_reg1;
722
723         platform_set_drvdata(pdev, ecc);
724
725         dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
726
727         return 0;
728
729 err_reg1:
730         edma_free_slot(ecc->dummy_slot);
731         return ret;
732 }
733
734 static int edma_remove(struct platform_device *pdev)
735 {
736         struct device *dev = &pdev->dev;
737         struct edma_cc *ecc = dev_get_drvdata(dev);
738
739         dma_async_device_unregister(&ecc->dma_slave);
740         edma_free_slot(ecc->dummy_slot);
741
742         return 0;
743 }
744
745 static struct platform_driver edma_driver = {
746         .probe          = edma_probe,
747         .remove         = edma_remove,
748         .driver = {
749                 .name = "edma-dma-engine",
750                 .owner = THIS_MODULE,
751         },
752 };
753
754 bool edma_filter_fn(struct dma_chan *chan, void *param)
755 {
756         if (chan->device->dev->driver == &edma_driver.driver) {
757                 struct edma_chan *echan = to_edma_chan(chan);
758                 unsigned ch_req = *(unsigned *)param;
759                 return ch_req == echan->ch_num;
760         }
761         return false;
762 }
763 EXPORT_SYMBOL(edma_filter_fn);
764
765 static struct platform_device *pdev0, *pdev1;
766
767 static const struct platform_device_info edma_dev_info0 = {
768         .name = "edma-dma-engine",
769         .id = 0,
770         .dma_mask = DMA_BIT_MASK(32),
771 };
772
773 static const struct platform_device_info edma_dev_info1 = {
774         .name = "edma-dma-engine",
775         .id = 1,
776         .dma_mask = DMA_BIT_MASK(32),
777 };
778
779 static int edma_init(void)
780 {
781         int ret = platform_driver_register(&edma_driver);
782
783         if (ret == 0) {
784                 pdev0 = platform_device_register_full(&edma_dev_info0);
785                 if (IS_ERR(pdev0)) {
786                         platform_driver_unregister(&edma_driver);
787                         ret = PTR_ERR(pdev0);
788                         goto out;
789                 }
790         }
791
792         if (EDMA_CTLRS == 2) {
793                 pdev1 = platform_device_register_full(&edma_dev_info1);
794                 if (IS_ERR(pdev1)) {
795                         platform_driver_unregister(&edma_driver);
796                         platform_device_unregister(pdev0);
797                         ret = PTR_ERR(pdev1);
798                 }
799         }
800
801 out:
802         return ret;
803 }
804 subsys_initcall(edma_init);
805
806 static void __exit edma_exit(void)
807 {
808         platform_device_unregister(pdev0);
809         if (pdev1)
810                 platform_device_unregister(pdev1);
811         platform_driver_unregister(&edma_driver);
812 }
813 module_exit(edma_exit);
814
815 MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
816 MODULE_DESCRIPTION("TI EDMA DMA engine driver");
817 MODULE_LICENSE("GPL v2");