blob: c6f60e9af8affcdd04e32ae19c241429cc3d2d3e [file] [log] [blame]
Matt Porterc2dde5f2012-08-22 21:09:34 -04001/*
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
Matt Porter3ad7a422013-03-06 11:15:31 -050027#include <linux/platform_data/edma.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040028
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
Joel Fernandes2abd5f12013-09-23 18:05:15 -050049/*
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
Matt Porterc2dde5f2012-08-22 21:09:34 -040057#define EDMA_MAX_SLOTS MAX_NR_SG
58#define EDMA_DESCRIPTORS 16
59
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050060struct edma_pset {
61 struct edmacc_param param;
62};
63
Matt Porterc2dde5f2012-08-22 21:09:34 -040064struct edma_desc {
65 struct virt_dma_desc vdesc;
66 struct list_head node;
Joel Fernandes50a9c702013-10-31 16:31:23 -050067 int cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -040068 int absync;
69 int pset_nr;
Joel Fernandes53407062013-09-03 10:02:46 -050070 int processed;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -050071 u32 residue;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050072 struct edma_pset pset[0];
Matt Porterc2dde5f2012-08-22 21:09:34 -040073};
74
75struct edma_cc;
76
77struct edma_chan {
78 struct virt_dma_chan vchan;
79 struct list_head node;
80 struct edma_desc *edesc;
81 struct edma_cc *ecc;
82 int ch_num;
83 bool alloced;
84 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -050085 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -050086 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -040087};
88
89struct edma_cc {
90 int ctlr;
91 struct dma_device dma_slave;
92 struct edma_chan slave_chans[EDMA_CHANS];
93 int num_slave_chans;
94 int dummy_slot;
95};
96
97static inline struct edma_cc *to_edma_cc(struct dma_device *d)
98{
99 return container_of(d, struct edma_cc, dma_slave);
100}
101
102static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
103{
104 return container_of(c, struct edma_chan, vchan.chan);
105}
106
107static inline struct edma_desc
108*to_edma_desc(struct dma_async_tx_descriptor *tx)
109{
110 return container_of(tx, struct edma_desc, vdesc.tx);
111}
112
113static void edma_desc_free(struct virt_dma_desc *vdesc)
114{
115 kfree(container_of(vdesc, struct edma_desc, vdesc));
116}
117
118/* Dispatch a queued descriptor to the controller (caller holds lock) */
119static void edma_execute(struct edma_chan *echan)
120{
Joel Fernandes53407062013-09-03 10:02:46 -0500121 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400122 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500123 struct device *dev = echan->vchan.chan.device->dev;
124 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400125
Joel Fernandes53407062013-09-03 10:02:46 -0500126 /* If either we processed all psets or we're still not started */
127 if (!echan->edesc ||
128 echan->edesc->pset_nr == echan->edesc->processed) {
129 /* Get next vdesc */
130 vdesc = vchan_next_desc(&echan->vchan);
131 if (!vdesc) {
132 echan->edesc = NULL;
133 return;
134 }
135 list_del(&vdesc->node);
136 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400137 }
138
Joel Fernandes53407062013-09-03 10:02:46 -0500139 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400140
Joel Fernandes53407062013-09-03 10:02:46 -0500141 /* Find out how many left */
142 left = edesc->pset_nr - edesc->processed;
143 nslots = min(MAX_NR_SG, left);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400144
145 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500146 for (i = 0; i < nslots; i++) {
147 j = i + edesc->processed;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500148 edma_write_slot(echan->slot[i], &edesc->pset[j].param);
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300149 dev_vdbg(echan->vchan.chan.device->dev,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400150 "\n pset[%d]:\n"
151 " chnum\t%d\n"
152 " slot\t%d\n"
153 " opt\t%08x\n"
154 " src\t%08x\n"
155 " dst\t%08x\n"
156 " abcnt\t%08x\n"
157 " ccnt\t%08x\n"
158 " bidx\t%08x\n"
159 " cidx\t%08x\n"
160 " lkrld\t%08x\n",
Joel Fernandes53407062013-09-03 10:02:46 -0500161 j, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500162 edesc->pset[j].param.opt,
163 edesc->pset[j].param.src,
164 edesc->pset[j].param.dst,
165 edesc->pset[j].param.a_b_cnt,
166 edesc->pset[j].param.ccnt,
167 edesc->pset[j].param.src_dst_bidx,
168 edesc->pset[j].param.src_dst_cidx,
169 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400170 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500171 if (i != (nslots - 1))
Matt Porterc2dde5f2012-08-22 21:09:34 -0400172 edma_link(echan->slot[i], echan->slot[i+1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400173 }
174
Joel Fernandes53407062013-09-03 10:02:46 -0500175 edesc->processed += nslots;
176
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500177 /*
178 * If this is either the last set in a set of SG-list transactions
179 * then setup a link to the dummy slot, this results in all future
180 * events being absorbed and that's OK because we're done
181 */
Joel Fernandes50a9c702013-10-31 16:31:23 -0500182 if (edesc->processed == edesc->pset_nr) {
183 if (edesc->cyclic)
184 edma_link(echan->slot[nslots-1], echan->slot[1]);
185 else
186 edma_link(echan->slot[nslots-1],
187 echan->ecc->dummy_slot);
188 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500189
Joel Fernandes53407062013-09-03 10:02:46 -0500190 if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300191 dev_dbg(dev, "first transfer starting on channel %d\n",
192 echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500193 edma_start(echan->ch_num);
Sekhar Nori5fc68a62014-03-19 11:25:50 +0530194 } else {
195 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
196 echan->ch_num, edesc->processed);
197 edma_resume(echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500198 }
Joel Fernandesc5f47992013-08-29 18:05:43 -0500199
200 /*
201 * This happens due to setup times between intermediate transfers
202 * in long SG lists which have to be broken up into transfers of
203 * MAX_NR_SG
204 */
205 if (echan->missed) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300206 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500207 edma_clean_channel(echan->ch_num);
208 edma_stop(echan->ch_num);
209 edma_start(echan->ch_num);
210 edma_trigger_channel(echan->ch_num);
211 echan->missed = 0;
212 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400213}
214
215static int edma_terminate_all(struct edma_chan *echan)
216{
217 unsigned long flags;
218 LIST_HEAD(head);
219
220 spin_lock_irqsave(&echan->vchan.lock, flags);
221
222 /*
223 * Stop DMA activity: we assume the callback will not be called
224 * after edma_dma() returns (even if it does, it will see
225 * echan->edesc is NULL and exit.)
226 */
227 if (echan->edesc) {
228 echan->edesc = NULL;
229 edma_stop(echan->ch_num);
230 }
231
232 vchan_get_all_descriptors(&echan->vchan, &head);
233 spin_unlock_irqrestore(&echan->vchan.lock, flags);
234 vchan_dma_desc_free_list(&echan->vchan, &head);
235
236 return 0;
237}
238
Matt Porterc2dde5f2012-08-22 21:09:34 -0400239static int edma_slave_config(struct edma_chan *echan,
Matt Porter661f7cb2013-01-10 13:41:04 -0500240 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400241{
Matt Porter661f7cb2013-01-10 13:41:04 -0500242 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
243 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400244 return -EINVAL;
245
Matt Porter661f7cb2013-01-10 13:41:04 -0500246 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400247
248 return 0;
249}
250
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300251static int edma_dma_pause(struct edma_chan *echan)
252{
253 /* Pause/Resume only allowed with cyclic mode */
254 if (!echan->edesc->cyclic)
255 return -EINVAL;
256
257 edma_pause(echan->ch_num);
258 return 0;
259}
260
261static int edma_dma_resume(struct edma_chan *echan)
262{
263 /* Pause/Resume only allowed with cyclic mode */
264 if (!echan->edesc->cyclic)
265 return -EINVAL;
266
267 edma_resume(echan->ch_num);
268 return 0;
269}
270
Matt Porterc2dde5f2012-08-22 21:09:34 -0400271static int edma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
272 unsigned long arg)
273{
274 int ret = 0;
275 struct dma_slave_config *config;
276 struct edma_chan *echan = to_edma_chan(chan);
277
278 switch (cmd) {
279 case DMA_TERMINATE_ALL:
280 edma_terminate_all(echan);
281 break;
282 case DMA_SLAVE_CONFIG:
283 config = (struct dma_slave_config *)arg;
284 ret = edma_slave_config(echan, config);
285 break;
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300286 case DMA_PAUSE:
287 ret = edma_dma_pause(echan);
288 break;
289
290 case DMA_RESUME:
291 ret = edma_dma_resume(echan);
292 break;
293
Matt Porterc2dde5f2012-08-22 21:09:34 -0400294 default:
295 ret = -ENOSYS;
296 }
297
298 return ret;
299}
300
Joel Fernandesfd009032013-09-23 18:05:13 -0500301/*
302 * A PaRAM set configuration abstraction used by other modes
303 * @chan: Channel who's PaRAM set we're configuring
304 * @pset: PaRAM set to initialize and setup.
305 * @src_addr: Source address of the DMA
306 * @dst_addr: Destination address of the DMA
307 * @burst: In units of dev_width, how much to send
308 * @dev_width: How much is the dev_width
309 * @dma_length: Total length of the DMA transfer
310 * @direction: Direction of the transfer
311 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500312static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
Joel Fernandesfd009032013-09-23 18:05:13 -0500313 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
314 enum dma_slave_buswidth dev_width, unsigned int dma_length,
315 enum dma_transfer_direction direction)
316{
317 struct edma_chan *echan = to_edma_chan(chan);
318 struct device *dev = chan->device->dev;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500319 struct edmacc_param *param = &epset->param;
Joel Fernandesfd009032013-09-23 18:05:13 -0500320 int acnt, bcnt, ccnt, cidx;
321 int src_bidx, dst_bidx, src_cidx, dst_cidx;
322 int absync;
323
324 acnt = dev_width;
Peter Ujfalusib2b617d2014-04-14 14:41:58 +0300325
326 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
327 if (!burst)
328 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -0500329 /*
330 * If the maxburst is equal to the fifo width, use
331 * A-synced transfers. This allows for large contiguous
332 * buffer transfers using only one PaRAM set.
333 */
334 if (burst == 1) {
335 /*
336 * For the A-sync case, bcnt and ccnt are the remainder
337 * and quotient respectively of the division of:
338 * (dma_length / acnt) by (SZ_64K -1). This is so
339 * that in case bcnt over flows, we have ccnt to use.
340 * Note: In A-sync tranfer only, bcntrld is used, but it
341 * only applies for sg_dma_len(sg) >= SZ_64K.
342 * In this case, the best way adopted is- bccnt for the
343 * first frame will be the remainder below. Then for
344 * every successive frame, bcnt will be SZ_64K-1. This
345 * is assured as bcntrld = 0xffff in end of function.
346 */
347 absync = false;
348 ccnt = dma_length / acnt / (SZ_64K - 1);
349 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
350 /*
351 * If bcnt is non-zero, we have a remainder and hence an
352 * extra frame to transfer, so increment ccnt.
353 */
354 if (bcnt)
355 ccnt++;
356 else
357 bcnt = SZ_64K - 1;
358 cidx = acnt;
359 } else {
360 /*
361 * If maxburst is greater than the fifo address_width,
362 * use AB-synced transfers where A count is the fifo
363 * address_width and B count is the maxburst. In this
364 * case, we are limited to transfers of C count frames
365 * of (address_width * maxburst) where C count is limited
366 * to SZ_64K-1. This places an upper bound on the length
367 * of an SG segment that can be handled.
368 */
369 absync = true;
370 bcnt = burst;
371 ccnt = dma_length / (acnt * bcnt);
372 if (ccnt > (SZ_64K - 1)) {
373 dev_err(dev, "Exceeded max SG segment size\n");
374 return -EINVAL;
375 }
376 cidx = acnt * bcnt;
377 }
378
379 if (direction == DMA_MEM_TO_DEV) {
380 src_bidx = acnt;
381 src_cidx = cidx;
382 dst_bidx = 0;
383 dst_cidx = 0;
384 } else if (direction == DMA_DEV_TO_MEM) {
385 src_bidx = 0;
386 src_cidx = 0;
387 dst_bidx = acnt;
388 dst_cidx = cidx;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500389 } else if (direction == DMA_MEM_TO_MEM) {
390 src_bidx = acnt;
391 src_cidx = cidx;
392 dst_bidx = acnt;
393 dst_cidx = cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500394 } else {
395 dev_err(dev, "%s: direction not implemented yet\n", __func__);
396 return -EINVAL;
397 }
398
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500399 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
Joel Fernandesfd009032013-09-23 18:05:13 -0500400 /* Configure A or AB synchronized transfers */
401 if (absync)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500402 param->opt |= SYNCDIM;
Joel Fernandesfd009032013-09-23 18:05:13 -0500403
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500404 param->src = src_addr;
405 param->dst = dst_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -0500406
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500407 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
408 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500409
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500410 param->a_b_cnt = bcnt << 16 | acnt;
411 param->ccnt = ccnt;
Joel Fernandesfd009032013-09-23 18:05:13 -0500412 /*
413 * Only time when (bcntrld) auto reload is required is for
414 * A-sync case, and in this case, a requirement of reload value
415 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
416 * and then later will be populated by edma_execute.
417 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500418 param->link_bcntrld = 0xffffffff;
Joel Fernandesfd009032013-09-23 18:05:13 -0500419 return absync;
420}
421
Matt Porterc2dde5f2012-08-22 21:09:34 -0400422static struct dma_async_tx_descriptor *edma_prep_slave_sg(
423 struct dma_chan *chan, struct scatterlist *sgl,
424 unsigned int sg_len, enum dma_transfer_direction direction,
425 unsigned long tx_flags, void *context)
426{
427 struct edma_chan *echan = to_edma_chan(chan);
428 struct device *dev = chan->device->dev;
429 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -0500430 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -0500431 enum dma_slave_buswidth dev_width;
432 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400433 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -0500434 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400435
436 if (unlikely(!echan || !sgl || !sg_len))
437 return NULL;
438
Matt Porter661f7cb2013-01-10 13:41:04 -0500439 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500440 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500441 dev_width = echan->cfg.src_addr_width;
442 burst = echan->cfg.src_maxburst;
443 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500444 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500445 dev_width = echan->cfg.dst_addr_width;
446 burst = echan->cfg.dst_maxburst;
447 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +0300448 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Matt Porter661f7cb2013-01-10 13:41:04 -0500449 return NULL;
450 }
451
452 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300453 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400454 return NULL;
455 }
456
Matt Porterc2dde5f2012-08-22 21:09:34 -0400457 edesc = kzalloc(sizeof(*edesc) + sg_len *
458 sizeof(edesc->pset[0]), GFP_ATOMIC);
459 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300460 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400461 return NULL;
462 }
463
464 edesc->pset_nr = sg_len;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500465 edesc->residue = 0;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400466
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500467 /* Allocate a PaRAM slot, if needed */
468 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
469
470 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -0400471 if (echan->slot[i] < 0) {
472 echan->slot[i] =
473 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
474 EDMA_SLOT_ANY);
475 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +0300476 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +0300477 dev_err(dev, "%s: Failed to allocate slot\n",
478 __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400479 return NULL;
480 }
481 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500482 }
483
484 /* Configure PaRAM sets for each SG */
485 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500486 /* Get address for each SG */
487 if (direction == DMA_DEV_TO_MEM)
488 dst_addr = sg_dma_address(sg);
489 else
490 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400491
Joel Fernandesfd009032013-09-23 18:05:13 -0500492 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
493 dst_addr, burst, dev_width,
494 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +0530495 if (ret < 0) {
496 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -0500497 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400498 }
499
Joel Fernandesfd009032013-09-23 18:05:13 -0500500 edesc->absync = ret;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500501 edesc->residue += sg_dma_len(sg);
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500502
503 /* If this is the last in a current SG set of transactions,
504 enable interrupts so that next set is processed */
505 if (!((i+1) % MAX_NR_SG))
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500506 edesc->pset[i].param.opt |= TCINTEN;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500507
Matt Porterc2dde5f2012-08-22 21:09:34 -0400508 /* If this is the last set, enable completion interrupt flag */
509 if (i == sg_len - 1)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500510 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400511 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400512
Matt Porterc2dde5f2012-08-22 21:09:34 -0400513 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
514}
Matt Porterc2dde5f2012-08-22 21:09:34 -0400515
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500516struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
517 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
518 size_t len, unsigned long tx_flags)
519{
520 int ret;
521 struct edma_desc *edesc;
522 struct device *dev = chan->device->dev;
523 struct edma_chan *echan = to_edma_chan(chan);
524
525 if (unlikely(!echan || !len))
526 return NULL;
527
528 edesc = kzalloc(sizeof(*edesc) + sizeof(edesc->pset[0]), GFP_ATOMIC);
529 if (!edesc) {
530 dev_dbg(dev, "Failed to allocate a descriptor\n");
531 return NULL;
532 }
533
534 edesc->pset_nr = 1;
535
536 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
537 DMA_SLAVE_BUSWIDTH_4_BYTES, len, DMA_MEM_TO_MEM);
538 if (ret < 0)
539 return NULL;
540
541 edesc->absync = ret;
542
543 /*
544 * Enable intermediate transfer chaining to re-trigger channel
545 * on completion of every TR, and enable transfer-completion
546 * interrupt on completion of the whole transfer.
547 */
548 edesc->pset[0].opt |= ITCCHEN;
549 edesc->pset[0].opt |= TCINTEN;
550
551 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
552}
553
Joel Fernandes50a9c702013-10-31 16:31:23 -0500554static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
555 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
556 size_t period_len, enum dma_transfer_direction direction,
557 unsigned long tx_flags, void *context)
558{
559 struct edma_chan *echan = to_edma_chan(chan);
560 struct device *dev = chan->device->dev;
561 struct edma_desc *edesc;
562 dma_addr_t src_addr, dst_addr;
563 enum dma_slave_buswidth dev_width;
564 u32 burst;
565 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400566
Joel Fernandes50a9c702013-10-31 16:31:23 -0500567 if (unlikely(!echan || !buf_len || !period_len))
568 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400569
Joel Fernandes50a9c702013-10-31 16:31:23 -0500570 if (direction == DMA_DEV_TO_MEM) {
571 src_addr = echan->cfg.src_addr;
572 dst_addr = buf_addr;
573 dev_width = echan->cfg.src_addr_width;
574 burst = echan->cfg.src_maxburst;
575 } else if (direction == DMA_MEM_TO_DEV) {
576 src_addr = buf_addr;
577 dst_addr = echan->cfg.dst_addr;
578 dev_width = echan->cfg.dst_addr_width;
579 burst = echan->cfg.dst_maxburst;
580 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +0300581 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500582 return NULL;
583 }
584
585 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300586 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500587 return NULL;
588 }
589
590 if (unlikely(buf_len % period_len)) {
591 dev_err(dev, "Period should be multiple of Buffer length\n");
592 return NULL;
593 }
594
595 nslots = (buf_len / period_len) + 1;
596
597 /*
598 * Cyclic DMA users such as audio cannot tolerate delays introduced
599 * by cases where the number of periods is more than the maximum
600 * number of SGs the EDMA driver can handle at a time. For DMA types
601 * such as Slave SGs, such delays are tolerable and synchronized,
602 * but the synchronization is difficult to achieve with Cyclic and
603 * cannot be guaranteed, so we error out early.
604 */
605 if (nslots > MAX_NR_SG)
606 return NULL;
607
608 edesc = kzalloc(sizeof(*edesc) + nslots *
609 sizeof(edesc->pset[0]), GFP_ATOMIC);
610 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300611 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500612 return NULL;
613 }
614
615 edesc->cyclic = 1;
616 edesc->pset_nr = nslots;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500617 edesc->residue = buf_len;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500618
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300619 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
620 __func__, echan->ch_num, nslots, period_len, buf_len);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500621
622 for (i = 0; i < nslots; i++) {
623 /* Allocate a PaRAM slot, if needed */
624 if (echan->slot[i] < 0) {
625 echan->slot[i] =
626 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
627 EDMA_SLOT_ANY);
628 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100629 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +0300630 dev_err(dev, "%s: Failed to allocate slot\n",
631 __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500632 return NULL;
633 }
634 }
635
636 if (i == nslots - 1) {
637 memcpy(&edesc->pset[i], &edesc->pset[0],
638 sizeof(edesc->pset[0]));
639 break;
640 }
641
642 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
643 dst_addr, burst, dev_width, period_len,
644 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100645 if (ret < 0) {
646 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500647 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100648 }
Joel Fernandes50a9c702013-10-31 16:31:23 -0500649
650 if (direction == DMA_DEV_TO_MEM)
651 dst_addr += period_len;
652 else
653 src_addr += period_len;
654
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300655 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
656 dev_vdbg(dev,
Joel Fernandes50a9c702013-10-31 16:31:23 -0500657 "\n pset[%d]:\n"
658 " chnum\t%d\n"
659 " slot\t%d\n"
660 " opt\t%08x\n"
661 " src\t%08x\n"
662 " dst\t%08x\n"
663 " abcnt\t%08x\n"
664 " ccnt\t%08x\n"
665 " bidx\t%08x\n"
666 " cidx\t%08x\n"
667 " lkrld\t%08x\n",
668 i, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500669 edesc->pset[i].param.opt,
670 edesc->pset[i].param.src,
671 edesc->pset[i].param.dst,
672 edesc->pset[i].param.a_b_cnt,
673 edesc->pset[i].param.ccnt,
674 edesc->pset[i].param.src_dst_bidx,
675 edesc->pset[i].param.src_dst_cidx,
676 edesc->pset[i].param.link_bcntrld);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500677
678 edesc->absync = ret;
679
680 /*
681 * Enable interrupts for every period because callback
682 * has to be called for every period.
683 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500684 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400685 }
686
687 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
688}
689
690static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
691{
692 struct edma_chan *echan = data;
693 struct device *dev = echan->vchan.chan.device->dev;
694 struct edma_desc *edesc;
Joel Fernandesc5f47992013-08-29 18:05:43 -0500695 struct edmacc_param p;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400696
Joel Fernandes50a9c702013-10-31 16:31:23 -0500697 edesc = echan->edesc;
698
699 /* Pause the channel for non-cyclic */
700 if (!edesc || (edesc && !edesc->cyclic))
701 edma_pause(echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400702
703 switch (ch_status) {
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530704 case EDMA_DMA_COMPLETE:
Joel Fernandes406efb12014-04-17 00:58:33 -0500705 spin_lock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400706
Matt Porterc2dde5f2012-08-22 21:09:34 -0400707 if (edesc) {
Joel Fernandes50a9c702013-10-31 16:31:23 -0500708 if (edesc->cyclic) {
709 vchan_cyclic_callback(&edesc->vdesc);
710 } else if (edesc->processed == edesc->pset_nr) {
Joel Fernandes53407062013-09-03 10:02:46 -0500711 dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500712 edesc->residue = 0;
Joel Fernandes53407062013-09-03 10:02:46 -0500713 edma_stop(echan->ch_num);
714 vchan_cookie_complete(&edesc->vdesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500715 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500716 } else {
717 dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500718 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500719 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400720 }
721
Joel Fernandes406efb12014-04-17 00:58:33 -0500722 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400723
724 break;
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530725 case EDMA_DMA_CC_ERROR:
Joel Fernandes406efb12014-04-17 00:58:33 -0500726 spin_lock(&echan->vchan.lock);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500727
728 edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
729
730 /*
731 * Issue later based on missed flag which will be sure
732 * to happen as:
733 * (1) we finished transmitting an intermediate slot and
734 * edma_execute is coming up.
735 * (2) or we finished current transfer and issue will
736 * call edma_execute.
737 *
738 * Important note: issuing can be dangerous here and
739 * lead to some nasty recursion when we are in a NULL
740 * slot. So we avoid doing so and set the missed flag.
741 */
742 if (p.a_b_cnt == 0 && p.ccnt == 0) {
743 dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
744 echan->missed = 1;
745 } else {
746 /*
747 * The slot is already programmed but the event got
748 * missed, so its safe to issue it here.
749 */
750 dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
751 edma_clean_channel(echan->ch_num);
752 edma_stop(echan->ch_num);
753 edma_start(echan->ch_num);
754 edma_trigger_channel(echan->ch_num);
755 }
756
Joel Fernandes406efb12014-04-17 00:58:33 -0500757 spin_unlock(&echan->vchan.lock);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500758
Matt Porterc2dde5f2012-08-22 21:09:34 -0400759 break;
760 default:
761 break;
762 }
763}
764
765/* Alloc channel resources */
766static int edma_alloc_chan_resources(struct dma_chan *chan)
767{
768 struct edma_chan *echan = to_edma_chan(chan);
769 struct device *dev = chan->device->dev;
770 int ret;
771 int a_ch_num;
772 LIST_HEAD(descs);
773
774 a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
775 chan, EVENTQ_DEFAULT);
776
777 if (a_ch_num < 0) {
778 ret = -ENODEV;
779 goto err_no_chan;
780 }
781
782 if (a_ch_num != echan->ch_num) {
783 dev_err(dev, "failed to allocate requested channel %u:%u\n",
784 EDMA_CTLR(echan->ch_num),
785 EDMA_CHAN_SLOT(echan->ch_num));
786 ret = -ENODEV;
787 goto err_wrong_chan;
788 }
789
790 echan->alloced = true;
791 echan->slot[0] = echan->ch_num;
792
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300793 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300794 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400795
796 return 0;
797
798err_wrong_chan:
799 edma_free_channel(a_ch_num);
800err_no_chan:
801 return ret;
802}
803
804/* Free channel resources */
805static void edma_free_chan_resources(struct dma_chan *chan)
806{
807 struct edma_chan *echan = to_edma_chan(chan);
808 struct device *dev = chan->device->dev;
809 int i;
810
811 /* Terminate transfers */
812 edma_stop(echan->ch_num);
813
814 vchan_free_chan_resources(&echan->vchan);
815
816 /* Free EDMA PaRAM slots */
817 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
818 if (echan->slot[i] >= 0) {
819 edma_free_slot(echan->slot[i]);
820 echan->slot[i] = -1;
821 }
822 }
823
824 /* Free EDMA channel */
825 if (echan->alloced) {
826 edma_free_channel(echan->ch_num);
827 echan->alloced = false;
828 }
829
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300830 dev_dbg(dev, "freeing channel for %u\n", echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400831}
832
833/* Send pending descriptor to hardware */
834static void edma_issue_pending(struct dma_chan *chan)
835{
836 struct edma_chan *echan = to_edma_chan(chan);
837 unsigned long flags;
838
839 spin_lock_irqsave(&echan->vchan.lock, flags);
840 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
841 edma_execute(echan);
842 spin_unlock_irqrestore(&echan->vchan.lock, flags);
843}
844
Matt Porterc2dde5f2012-08-22 21:09:34 -0400845/* Check request completion status */
846static enum dma_status edma_tx_status(struct dma_chan *chan,
847 dma_cookie_t cookie,
848 struct dma_tx_state *txstate)
849{
850 struct edma_chan *echan = to_edma_chan(chan);
851 struct virt_dma_desc *vdesc;
852 enum dma_status ret;
853 unsigned long flags;
854
855 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +0530856 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400857 return ret;
858
859 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -0500860 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500861 txstate->residue = echan->edesc->residue;
Thomas Gleixnerde135932014-04-28 14:19:51 -0500862 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
863 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400864 spin_unlock_irqrestore(&echan->vchan.lock, flags);
865
866 return ret;
867}
868
869static void __init edma_chan_init(struct edma_cc *ecc,
870 struct dma_device *dma,
871 struct edma_chan *echans)
872{
873 int i, j;
874
875 for (i = 0; i < EDMA_CHANS; i++) {
876 struct edma_chan *echan = &echans[i];
877 echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
878 echan->ecc = ecc;
879 echan->vchan.desc_free = edma_desc_free;
880
881 vchan_init(&echan->vchan, dma);
882
883 INIT_LIST_HEAD(&echan->node);
884 for (j = 0; j < EDMA_MAX_SLOTS; j++)
885 echan->slot[j] = -1;
886 }
887}
888
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +0300889#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
890 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
891 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
892
893static int edma_dma_device_slave_caps(struct dma_chan *dchan,
894 struct dma_slave_caps *caps)
895{
896 caps->src_addr_widths = EDMA_DMA_BUSWIDTHS;
897 caps->dstn_addr_widths = EDMA_DMA_BUSWIDTHS;
898 caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
899 caps->cmd_pause = true;
900 caps->cmd_terminate = true;
901 caps->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
902
903 return 0;
904}
905
Matt Porterc2dde5f2012-08-22 21:09:34 -0400906static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
907 struct device *dev)
908{
909 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500910 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500911 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400912 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
913 dma->device_free_chan_resources = edma_free_chan_resources;
914 dma->device_issue_pending = edma_issue_pending;
915 dma->device_tx_status = edma_tx_status;
916 dma->device_control = edma_control;
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +0300917 dma->device_slave_caps = edma_dma_device_slave_caps;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400918 dma->dev = dev;
919
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500920 /*
921 * code using dma memcpy must make sure alignment of
922 * length is at dma->copy_align boundary.
923 */
924 dma->copy_align = DMA_SLAVE_BUSWIDTH_4_BYTES;
925
Matt Porterc2dde5f2012-08-22 21:09:34 -0400926 INIT_LIST_HEAD(&dma->channels);
927}
928
Bill Pemberton463a1f82012-11-19 13:22:55 -0500929static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400930{
931 struct edma_cc *ecc;
932 int ret;
933
Russell King94cb0e72013-06-27 13:45:16 +0100934 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
935 if (ret)
936 return ret;
937
Matt Porterc2dde5f2012-08-22 21:09:34 -0400938 ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
939 if (!ecc) {
940 dev_err(&pdev->dev, "Can't allocate controller\n");
941 return -ENOMEM;
942 }
943
944 ecc->ctlr = pdev->id;
945 ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
946 if (ecc->dummy_slot < 0) {
947 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
948 return -EIO;
949 }
950
951 dma_cap_zero(ecc->dma_slave.cap_mask);
952 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
Peter Ujfalusi232b223d2014-04-14 14:42:00 +0300953 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500954 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400955
956 edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
957
958 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
959
960 ret = dma_async_device_register(&ecc->dma_slave);
961 if (ret)
962 goto err_reg1;
963
964 platform_set_drvdata(pdev, ecc);
965
966 dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
967
968 return 0;
969
970err_reg1:
971 edma_free_slot(ecc->dummy_slot);
972 return ret;
973}
974
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -0800975static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400976{
977 struct device *dev = &pdev->dev;
978 struct edma_cc *ecc = dev_get_drvdata(dev);
979
980 dma_async_device_unregister(&ecc->dma_slave);
981 edma_free_slot(ecc->dummy_slot);
982
983 return 0;
984}
985
986static struct platform_driver edma_driver = {
987 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -0500988 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400989 .driver = {
990 .name = "edma-dma-engine",
991 .owner = THIS_MODULE,
992 },
993};
994
995bool edma_filter_fn(struct dma_chan *chan, void *param)
996{
997 if (chan->device->dev->driver == &edma_driver.driver) {
998 struct edma_chan *echan = to_edma_chan(chan);
999 unsigned ch_req = *(unsigned *)param;
1000 return ch_req == echan->ch_num;
1001 }
1002 return false;
1003}
1004EXPORT_SYMBOL(edma_filter_fn);
1005
1006static struct platform_device *pdev0, *pdev1;
1007
1008static const struct platform_device_info edma_dev_info0 = {
1009 .name = "edma-dma-engine",
1010 .id = 0,
Russell King94cb0e72013-06-27 13:45:16 +01001011 .dma_mask = DMA_BIT_MASK(32),
Matt Porterc2dde5f2012-08-22 21:09:34 -04001012};
1013
1014static const struct platform_device_info edma_dev_info1 = {
1015 .name = "edma-dma-engine",
1016 .id = 1,
Russell King94cb0e72013-06-27 13:45:16 +01001017 .dma_mask = DMA_BIT_MASK(32),
Matt Porterc2dde5f2012-08-22 21:09:34 -04001018};
1019
1020static int edma_init(void)
1021{
1022 int ret = platform_driver_register(&edma_driver);
1023
1024 if (ret == 0) {
1025 pdev0 = platform_device_register_full(&edma_dev_info0);
1026 if (IS_ERR(pdev0)) {
1027 platform_driver_unregister(&edma_driver);
1028 ret = PTR_ERR(pdev0);
1029 goto out;
1030 }
1031 }
1032
1033 if (EDMA_CTLRS == 2) {
1034 pdev1 = platform_device_register_full(&edma_dev_info1);
1035 if (IS_ERR(pdev1)) {
1036 platform_driver_unregister(&edma_driver);
1037 platform_device_unregister(pdev0);
1038 ret = PTR_ERR(pdev1);
1039 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001040 }
1041
1042out:
1043 return ret;
1044}
1045subsys_initcall(edma_init);
1046
1047static void __exit edma_exit(void)
1048{
1049 platform_device_unregister(pdev0);
1050 if (pdev1)
1051 platform_device_unregister(pdev1);
1052 platform_driver_unregister(&edma_driver);
1053}
1054module_exit(edma_exit);
1055
Josh Boyerd71505b2013-09-04 10:32:50 -04001056MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -04001057MODULE_DESCRIPTION("TI EDMA DMA engine driver");
1058MODULE_LICENSE("GPL v2");