blob: 6e230006eb08018c073cc6033d083c51bcaf3bac [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 {
Thomas Gleixnerc2da2342014-04-28 14:29:57 -050061 u32 len;
62 dma_addr_t addr;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050063 struct edmacc_param param;
64};
65
Matt Porterc2dde5f2012-08-22 21:09:34 -040066struct edma_desc {
67 struct virt_dma_desc vdesc;
68 struct list_head node;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -050069 enum dma_transfer_direction direction;
Joel Fernandes50a9c702013-10-31 16:31:23 -050070 int cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -040071 int absync;
72 int pset_nr;
Joel Fernandes53407062013-09-03 10:02:46 -050073 int processed;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -050074 u32 residue;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050075 struct edma_pset pset[0];
Matt Porterc2dde5f2012-08-22 21:09:34 -040076};
77
78struct edma_cc;
79
80struct edma_chan {
81 struct virt_dma_chan vchan;
82 struct list_head node;
83 struct edma_desc *edesc;
84 struct edma_cc *ecc;
85 int ch_num;
86 bool alloced;
87 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -050088 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -050089 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -040090};
91
92struct edma_cc {
93 int ctlr;
94 struct dma_device dma_slave;
95 struct edma_chan slave_chans[EDMA_CHANS];
96 int num_slave_chans;
97 int dummy_slot;
98};
99
100static inline struct edma_cc *to_edma_cc(struct dma_device *d)
101{
102 return container_of(d, struct edma_cc, dma_slave);
103}
104
105static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
106{
107 return container_of(c, struct edma_chan, vchan.chan);
108}
109
110static inline struct edma_desc
111*to_edma_desc(struct dma_async_tx_descriptor *tx)
112{
113 return container_of(tx, struct edma_desc, vdesc.tx);
114}
115
116static void edma_desc_free(struct virt_dma_desc *vdesc)
117{
118 kfree(container_of(vdesc, struct edma_desc, vdesc));
119}
120
121/* Dispatch a queued descriptor to the controller (caller holds lock) */
122static void edma_execute(struct edma_chan *echan)
123{
Joel Fernandes53407062013-09-03 10:02:46 -0500124 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400125 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500126 struct device *dev = echan->vchan.chan.device->dev;
127 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400128
Joel Fernandes53407062013-09-03 10:02:46 -0500129 /* If either we processed all psets or we're still not started */
130 if (!echan->edesc ||
131 echan->edesc->pset_nr == echan->edesc->processed) {
132 /* Get next vdesc */
133 vdesc = vchan_next_desc(&echan->vchan);
134 if (!vdesc) {
135 echan->edesc = NULL;
136 return;
137 }
138 list_del(&vdesc->node);
139 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400140 }
141
Joel Fernandes53407062013-09-03 10:02:46 -0500142 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400143
Joel Fernandes53407062013-09-03 10:02:46 -0500144 /* Find out how many left */
145 left = edesc->pset_nr - edesc->processed;
146 nslots = min(MAX_NR_SG, left);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400147
148 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500149 for (i = 0; i < nslots; i++) {
150 j = i + edesc->processed;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500151 edma_write_slot(echan->slot[i], &edesc->pset[j].param);
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300152 dev_vdbg(echan->vchan.chan.device->dev,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400153 "\n pset[%d]:\n"
154 " chnum\t%d\n"
155 " slot\t%d\n"
156 " opt\t%08x\n"
157 " src\t%08x\n"
158 " dst\t%08x\n"
159 " abcnt\t%08x\n"
160 " ccnt\t%08x\n"
161 " bidx\t%08x\n"
162 " cidx\t%08x\n"
163 " lkrld\t%08x\n",
Joel Fernandes53407062013-09-03 10:02:46 -0500164 j, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500165 edesc->pset[j].param.opt,
166 edesc->pset[j].param.src,
167 edesc->pset[j].param.dst,
168 edesc->pset[j].param.a_b_cnt,
169 edesc->pset[j].param.ccnt,
170 edesc->pset[j].param.src_dst_bidx,
171 edesc->pset[j].param.src_dst_cidx,
172 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400173 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500174 if (i != (nslots - 1))
Matt Porterc2dde5f2012-08-22 21:09:34 -0400175 edma_link(echan->slot[i], echan->slot[i+1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400176 }
177
Joel Fernandes53407062013-09-03 10:02:46 -0500178 edesc->processed += nslots;
179
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500180 /*
181 * If this is either the last set in a set of SG-list transactions
182 * then setup a link to the dummy slot, this results in all future
183 * events being absorbed and that's OK because we're done
184 */
Joel Fernandes50a9c702013-10-31 16:31:23 -0500185 if (edesc->processed == edesc->pset_nr) {
186 if (edesc->cyclic)
187 edma_link(echan->slot[nslots-1], echan->slot[1]);
188 else
189 edma_link(echan->slot[nslots-1],
190 echan->ecc->dummy_slot);
191 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500192
Joel Fernandes53407062013-09-03 10:02:46 -0500193 if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300194 dev_dbg(dev, "first transfer starting on channel %d\n",
195 echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500196 edma_start(echan->ch_num);
Sekhar Nori5fc68a62014-03-19 11:25:50 +0530197 } else {
198 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
199 echan->ch_num, edesc->processed);
200 edma_resume(echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500201 }
Joel Fernandesc5f47992013-08-29 18:05:43 -0500202
203 /*
204 * This happens due to setup times between intermediate transfers
205 * in long SG lists which have to be broken up into transfers of
206 * MAX_NR_SG
207 */
208 if (echan->missed) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300209 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500210 edma_clean_channel(echan->ch_num);
211 edma_stop(echan->ch_num);
212 edma_start(echan->ch_num);
213 edma_trigger_channel(echan->ch_num);
214 echan->missed = 0;
215 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400216}
217
218static int edma_terminate_all(struct edma_chan *echan)
219{
220 unsigned long flags;
221 LIST_HEAD(head);
222
223 spin_lock_irqsave(&echan->vchan.lock, flags);
224
225 /*
226 * Stop DMA activity: we assume the callback will not be called
227 * after edma_dma() returns (even if it does, it will see
228 * echan->edesc is NULL and exit.)
229 */
230 if (echan->edesc) {
231 echan->edesc = NULL;
232 edma_stop(echan->ch_num);
233 }
234
235 vchan_get_all_descriptors(&echan->vchan, &head);
236 spin_unlock_irqrestore(&echan->vchan.lock, flags);
237 vchan_dma_desc_free_list(&echan->vchan, &head);
238
239 return 0;
240}
241
Matt Porterc2dde5f2012-08-22 21:09:34 -0400242static int edma_slave_config(struct edma_chan *echan,
Matt Porter661f7cb2013-01-10 13:41:04 -0500243 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400244{
Matt Porter661f7cb2013-01-10 13:41:04 -0500245 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
246 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400247 return -EINVAL;
248
Matt Porter661f7cb2013-01-10 13:41:04 -0500249 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400250
251 return 0;
252}
253
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300254static int edma_dma_pause(struct edma_chan *echan)
255{
256 /* Pause/Resume only allowed with cyclic mode */
257 if (!echan->edesc->cyclic)
258 return -EINVAL;
259
260 edma_pause(echan->ch_num);
261 return 0;
262}
263
264static int edma_dma_resume(struct edma_chan *echan)
265{
266 /* Pause/Resume only allowed with cyclic mode */
267 if (!echan->edesc->cyclic)
268 return -EINVAL;
269
270 edma_resume(echan->ch_num);
271 return 0;
272}
273
Matt Porterc2dde5f2012-08-22 21:09:34 -0400274static int edma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
275 unsigned long arg)
276{
277 int ret = 0;
278 struct dma_slave_config *config;
279 struct edma_chan *echan = to_edma_chan(chan);
280
281 switch (cmd) {
282 case DMA_TERMINATE_ALL:
283 edma_terminate_all(echan);
284 break;
285 case DMA_SLAVE_CONFIG:
286 config = (struct dma_slave_config *)arg;
287 ret = edma_slave_config(echan, config);
288 break;
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300289 case DMA_PAUSE:
290 ret = edma_dma_pause(echan);
291 break;
292
293 case DMA_RESUME:
294 ret = edma_dma_resume(echan);
295 break;
296
Matt Porterc2dde5f2012-08-22 21:09:34 -0400297 default:
298 ret = -ENOSYS;
299 }
300
301 return ret;
302}
303
Joel Fernandesfd009032013-09-23 18:05:13 -0500304/*
305 * A PaRAM set configuration abstraction used by other modes
306 * @chan: Channel who's PaRAM set we're configuring
307 * @pset: PaRAM set to initialize and setup.
308 * @src_addr: Source address of the DMA
309 * @dst_addr: Destination address of the DMA
310 * @burst: In units of dev_width, how much to send
311 * @dev_width: How much is the dev_width
312 * @dma_length: Total length of the DMA transfer
313 * @direction: Direction of the transfer
314 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500315static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
Joel Fernandesfd009032013-09-23 18:05:13 -0500316 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
317 enum dma_slave_buswidth dev_width, unsigned int dma_length,
318 enum dma_transfer_direction direction)
319{
320 struct edma_chan *echan = to_edma_chan(chan);
321 struct device *dev = chan->device->dev;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500322 struct edmacc_param *param = &epset->param;
Joel Fernandesfd009032013-09-23 18:05:13 -0500323 int acnt, bcnt, ccnt, cidx;
324 int src_bidx, dst_bidx, src_cidx, dst_cidx;
325 int absync;
326
327 acnt = dev_width;
Peter Ujfalusib2b617d2014-04-14 14:41:58 +0300328
329 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
330 if (!burst)
331 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -0500332 /*
333 * If the maxburst is equal to the fifo width, use
334 * A-synced transfers. This allows for large contiguous
335 * buffer transfers using only one PaRAM set.
336 */
337 if (burst == 1) {
338 /*
339 * For the A-sync case, bcnt and ccnt are the remainder
340 * and quotient respectively of the division of:
341 * (dma_length / acnt) by (SZ_64K -1). This is so
342 * that in case bcnt over flows, we have ccnt to use.
343 * Note: In A-sync tranfer only, bcntrld is used, but it
344 * only applies for sg_dma_len(sg) >= SZ_64K.
345 * In this case, the best way adopted is- bccnt for the
346 * first frame will be the remainder below. Then for
347 * every successive frame, bcnt will be SZ_64K-1. This
348 * is assured as bcntrld = 0xffff in end of function.
349 */
350 absync = false;
351 ccnt = dma_length / acnt / (SZ_64K - 1);
352 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
353 /*
354 * If bcnt is non-zero, we have a remainder and hence an
355 * extra frame to transfer, so increment ccnt.
356 */
357 if (bcnt)
358 ccnt++;
359 else
360 bcnt = SZ_64K - 1;
361 cidx = acnt;
362 } else {
363 /*
364 * If maxburst is greater than the fifo address_width,
365 * use AB-synced transfers where A count is the fifo
366 * address_width and B count is the maxburst. In this
367 * case, we are limited to transfers of C count frames
368 * of (address_width * maxburst) where C count is limited
369 * to SZ_64K-1. This places an upper bound on the length
370 * of an SG segment that can be handled.
371 */
372 absync = true;
373 bcnt = burst;
374 ccnt = dma_length / (acnt * bcnt);
375 if (ccnt > (SZ_64K - 1)) {
376 dev_err(dev, "Exceeded max SG segment size\n");
377 return -EINVAL;
378 }
379 cidx = acnt * bcnt;
380 }
381
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500382 epset->len = dma_length;
383
Joel Fernandesfd009032013-09-23 18:05:13 -0500384 if (direction == DMA_MEM_TO_DEV) {
385 src_bidx = acnt;
386 src_cidx = cidx;
387 dst_bidx = 0;
388 dst_cidx = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500389 epset->addr = src_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -0500390 } else if (direction == DMA_DEV_TO_MEM) {
391 src_bidx = 0;
392 src_cidx = 0;
393 dst_bidx = acnt;
394 dst_cidx = cidx;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500395 epset->addr = dst_addr;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500396 } else if (direction == DMA_MEM_TO_MEM) {
397 src_bidx = acnt;
398 src_cidx = cidx;
399 dst_bidx = acnt;
400 dst_cidx = cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500401 } else {
402 dev_err(dev, "%s: direction not implemented yet\n", __func__);
403 return -EINVAL;
404 }
405
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500406 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
Joel Fernandesfd009032013-09-23 18:05:13 -0500407 /* Configure A or AB synchronized transfers */
408 if (absync)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500409 param->opt |= SYNCDIM;
Joel Fernandesfd009032013-09-23 18:05:13 -0500410
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500411 param->src = src_addr;
412 param->dst = dst_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -0500413
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500414 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
415 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500416
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500417 param->a_b_cnt = bcnt << 16 | acnt;
418 param->ccnt = ccnt;
Joel Fernandesfd009032013-09-23 18:05:13 -0500419 /*
420 * Only time when (bcntrld) auto reload is required is for
421 * A-sync case, and in this case, a requirement of reload value
422 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
423 * and then later will be populated by edma_execute.
424 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500425 param->link_bcntrld = 0xffffffff;
Joel Fernandesfd009032013-09-23 18:05:13 -0500426 return absync;
427}
428
Matt Porterc2dde5f2012-08-22 21:09:34 -0400429static struct dma_async_tx_descriptor *edma_prep_slave_sg(
430 struct dma_chan *chan, struct scatterlist *sgl,
431 unsigned int sg_len, enum dma_transfer_direction direction,
432 unsigned long tx_flags, void *context)
433{
434 struct edma_chan *echan = to_edma_chan(chan);
435 struct device *dev = chan->device->dev;
436 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -0500437 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -0500438 enum dma_slave_buswidth dev_width;
439 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400440 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -0500441 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400442
443 if (unlikely(!echan || !sgl || !sg_len))
444 return NULL;
445
Matt Porter661f7cb2013-01-10 13:41:04 -0500446 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500447 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500448 dev_width = echan->cfg.src_addr_width;
449 burst = echan->cfg.src_maxburst;
450 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500451 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500452 dev_width = echan->cfg.dst_addr_width;
453 burst = echan->cfg.dst_maxburst;
454 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +0300455 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Matt Porter661f7cb2013-01-10 13:41:04 -0500456 return NULL;
457 }
458
459 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300460 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400461 return NULL;
462 }
463
Matt Porterc2dde5f2012-08-22 21:09:34 -0400464 edesc = kzalloc(sizeof(*edesc) + sg_len *
465 sizeof(edesc->pset[0]), GFP_ATOMIC);
466 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300467 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400468 return NULL;
469 }
470
471 edesc->pset_nr = sg_len;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500472 edesc->residue = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500473 edesc->direction = direction;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400474
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500475 /* Allocate a PaRAM slot, if needed */
476 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
477
478 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -0400479 if (echan->slot[i] < 0) {
480 echan->slot[i] =
481 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
482 EDMA_SLOT_ANY);
483 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +0300484 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +0300485 dev_err(dev, "%s: Failed to allocate slot\n",
486 __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400487 return NULL;
488 }
489 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500490 }
491
492 /* Configure PaRAM sets for each SG */
493 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500494 /* Get address for each SG */
495 if (direction == DMA_DEV_TO_MEM)
496 dst_addr = sg_dma_address(sg);
497 else
498 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400499
Joel Fernandesfd009032013-09-23 18:05:13 -0500500 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
501 dst_addr, burst, dev_width,
502 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +0530503 if (ret < 0) {
504 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -0500505 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400506 }
507
Joel Fernandesfd009032013-09-23 18:05:13 -0500508 edesc->absync = ret;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500509 edesc->residue += sg_dma_len(sg);
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500510
511 /* If this is the last in a current SG set of transactions,
512 enable interrupts so that next set is processed */
513 if (!((i+1) % MAX_NR_SG))
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500514 edesc->pset[i].param.opt |= TCINTEN;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500515
Matt Porterc2dde5f2012-08-22 21:09:34 -0400516 /* If this is the last set, enable completion interrupt flag */
517 if (i == sg_len - 1)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500518 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400519 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400520
Matt Porterc2dde5f2012-08-22 21:09:34 -0400521 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
522}
Matt Porterc2dde5f2012-08-22 21:09:34 -0400523
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500524struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
525 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
526 size_t len, unsigned long tx_flags)
527{
528 int ret;
529 struct edma_desc *edesc;
530 struct device *dev = chan->device->dev;
531 struct edma_chan *echan = to_edma_chan(chan);
532
533 if (unlikely(!echan || !len))
534 return NULL;
535
536 edesc = kzalloc(sizeof(*edesc) + sizeof(edesc->pset[0]), GFP_ATOMIC);
537 if (!edesc) {
538 dev_dbg(dev, "Failed to allocate a descriptor\n");
539 return NULL;
540 }
541
542 edesc->pset_nr = 1;
543
544 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
545 DMA_SLAVE_BUSWIDTH_4_BYTES, len, DMA_MEM_TO_MEM);
546 if (ret < 0)
547 return NULL;
548
549 edesc->absync = ret;
550
551 /*
552 * Enable intermediate transfer chaining to re-trigger channel
553 * on completion of every TR, and enable transfer-completion
554 * interrupt on completion of the whole transfer.
555 */
556 edesc->pset[0].opt |= ITCCHEN;
557 edesc->pset[0].opt |= TCINTEN;
558
559 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
560}
561
Joel Fernandes50a9c702013-10-31 16:31:23 -0500562static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
563 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
564 size_t period_len, enum dma_transfer_direction direction,
565 unsigned long tx_flags, void *context)
566{
567 struct edma_chan *echan = to_edma_chan(chan);
568 struct device *dev = chan->device->dev;
569 struct edma_desc *edesc;
570 dma_addr_t src_addr, dst_addr;
571 enum dma_slave_buswidth dev_width;
572 u32 burst;
573 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400574
Joel Fernandes50a9c702013-10-31 16:31:23 -0500575 if (unlikely(!echan || !buf_len || !period_len))
576 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400577
Joel Fernandes50a9c702013-10-31 16:31:23 -0500578 if (direction == DMA_DEV_TO_MEM) {
579 src_addr = echan->cfg.src_addr;
580 dst_addr = buf_addr;
581 dev_width = echan->cfg.src_addr_width;
582 burst = echan->cfg.src_maxburst;
583 } else if (direction == DMA_MEM_TO_DEV) {
584 src_addr = buf_addr;
585 dst_addr = echan->cfg.dst_addr;
586 dev_width = echan->cfg.dst_addr_width;
587 burst = echan->cfg.dst_maxburst;
588 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +0300589 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500590 return NULL;
591 }
592
593 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300594 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500595 return NULL;
596 }
597
598 if (unlikely(buf_len % period_len)) {
599 dev_err(dev, "Period should be multiple of Buffer length\n");
600 return NULL;
601 }
602
603 nslots = (buf_len / period_len) + 1;
604
605 /*
606 * Cyclic DMA users such as audio cannot tolerate delays introduced
607 * by cases where the number of periods is more than the maximum
608 * number of SGs the EDMA driver can handle at a time. For DMA types
609 * such as Slave SGs, such delays are tolerable and synchronized,
610 * but the synchronization is difficult to achieve with Cyclic and
611 * cannot be guaranteed, so we error out early.
612 */
613 if (nslots > MAX_NR_SG)
614 return NULL;
615
616 edesc = kzalloc(sizeof(*edesc) + nslots *
617 sizeof(edesc->pset[0]), GFP_ATOMIC);
618 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300619 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500620 return NULL;
621 }
622
623 edesc->cyclic = 1;
624 edesc->pset_nr = nslots;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500625 edesc->residue = buf_len;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500626 edesc->direction = direction;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500627
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300628 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
629 __func__, echan->ch_num, nslots, period_len, buf_len);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500630
631 for (i = 0; i < nslots; i++) {
632 /* Allocate a PaRAM slot, if needed */
633 if (echan->slot[i] < 0) {
634 echan->slot[i] =
635 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
636 EDMA_SLOT_ANY);
637 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100638 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +0300639 dev_err(dev, "%s: Failed to allocate slot\n",
640 __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500641 return NULL;
642 }
643 }
644
645 if (i == nslots - 1) {
646 memcpy(&edesc->pset[i], &edesc->pset[0],
647 sizeof(edesc->pset[0]));
648 break;
649 }
650
651 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
652 dst_addr, burst, dev_width, period_len,
653 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100654 if (ret < 0) {
655 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500656 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100657 }
Joel Fernandes50a9c702013-10-31 16:31:23 -0500658
659 if (direction == DMA_DEV_TO_MEM)
660 dst_addr += period_len;
661 else
662 src_addr += period_len;
663
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300664 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
665 dev_vdbg(dev,
Joel Fernandes50a9c702013-10-31 16:31:23 -0500666 "\n pset[%d]:\n"
667 " chnum\t%d\n"
668 " slot\t%d\n"
669 " opt\t%08x\n"
670 " src\t%08x\n"
671 " dst\t%08x\n"
672 " abcnt\t%08x\n"
673 " ccnt\t%08x\n"
674 " bidx\t%08x\n"
675 " cidx\t%08x\n"
676 " lkrld\t%08x\n",
677 i, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500678 edesc->pset[i].param.opt,
679 edesc->pset[i].param.src,
680 edesc->pset[i].param.dst,
681 edesc->pset[i].param.a_b_cnt,
682 edesc->pset[i].param.ccnt,
683 edesc->pset[i].param.src_dst_bidx,
684 edesc->pset[i].param.src_dst_cidx,
685 edesc->pset[i].param.link_bcntrld);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500686
687 edesc->absync = ret;
688
689 /*
690 * Enable interrupts for every period because callback
691 * has to be called for every period.
692 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500693 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400694 }
695
696 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
697}
698
699static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
700{
701 struct edma_chan *echan = data;
702 struct device *dev = echan->vchan.chan.device->dev;
703 struct edma_desc *edesc;
Joel Fernandesc5f47992013-08-29 18:05:43 -0500704 struct edmacc_param p;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400705
Joel Fernandes50a9c702013-10-31 16:31:23 -0500706 edesc = echan->edesc;
707
708 /* Pause the channel for non-cyclic */
709 if (!edesc || (edesc && !edesc->cyclic))
710 edma_pause(echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400711
712 switch (ch_status) {
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530713 case EDMA_DMA_COMPLETE:
Joel Fernandes406efb12014-04-17 00:58:33 -0500714 spin_lock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400715
Matt Porterc2dde5f2012-08-22 21:09:34 -0400716 if (edesc) {
Joel Fernandes50a9c702013-10-31 16:31:23 -0500717 if (edesc->cyclic) {
718 vchan_cyclic_callback(&edesc->vdesc);
719 } else if (edesc->processed == edesc->pset_nr) {
Joel Fernandes53407062013-09-03 10:02:46 -0500720 dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500721 edesc->residue = 0;
Joel Fernandes53407062013-09-03 10:02:46 -0500722 edma_stop(echan->ch_num);
723 vchan_cookie_complete(&edesc->vdesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500724 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500725 } else {
726 dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500727 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500728 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400729 }
730
Joel Fernandes406efb12014-04-17 00:58:33 -0500731 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400732
733 break;
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530734 case EDMA_DMA_CC_ERROR:
Joel Fernandes406efb12014-04-17 00:58:33 -0500735 spin_lock(&echan->vchan.lock);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500736
737 edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
738
739 /*
740 * Issue later based on missed flag which will be sure
741 * to happen as:
742 * (1) we finished transmitting an intermediate slot and
743 * edma_execute is coming up.
744 * (2) or we finished current transfer and issue will
745 * call edma_execute.
746 *
747 * Important note: issuing can be dangerous here and
748 * lead to some nasty recursion when we are in a NULL
749 * slot. So we avoid doing so and set the missed flag.
750 */
751 if (p.a_b_cnt == 0 && p.ccnt == 0) {
752 dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
753 echan->missed = 1;
754 } else {
755 /*
756 * The slot is already programmed but the event got
757 * missed, so its safe to issue it here.
758 */
759 dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
760 edma_clean_channel(echan->ch_num);
761 edma_stop(echan->ch_num);
762 edma_start(echan->ch_num);
763 edma_trigger_channel(echan->ch_num);
764 }
765
Joel Fernandes406efb12014-04-17 00:58:33 -0500766 spin_unlock(&echan->vchan.lock);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500767
Matt Porterc2dde5f2012-08-22 21:09:34 -0400768 break;
769 default:
770 break;
771 }
772}
773
774/* Alloc channel resources */
775static int edma_alloc_chan_resources(struct dma_chan *chan)
776{
777 struct edma_chan *echan = to_edma_chan(chan);
778 struct device *dev = chan->device->dev;
779 int ret;
780 int a_ch_num;
781 LIST_HEAD(descs);
782
783 a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
784 chan, EVENTQ_DEFAULT);
785
786 if (a_ch_num < 0) {
787 ret = -ENODEV;
788 goto err_no_chan;
789 }
790
791 if (a_ch_num != echan->ch_num) {
792 dev_err(dev, "failed to allocate requested channel %u:%u\n",
793 EDMA_CTLR(echan->ch_num),
794 EDMA_CHAN_SLOT(echan->ch_num));
795 ret = -ENODEV;
796 goto err_wrong_chan;
797 }
798
799 echan->alloced = true;
800 echan->slot[0] = echan->ch_num;
801
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300802 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300803 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400804
805 return 0;
806
807err_wrong_chan:
808 edma_free_channel(a_ch_num);
809err_no_chan:
810 return ret;
811}
812
813/* Free channel resources */
814static void edma_free_chan_resources(struct dma_chan *chan)
815{
816 struct edma_chan *echan = to_edma_chan(chan);
817 struct device *dev = chan->device->dev;
818 int i;
819
820 /* Terminate transfers */
821 edma_stop(echan->ch_num);
822
823 vchan_free_chan_resources(&echan->vchan);
824
825 /* Free EDMA PaRAM slots */
826 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
827 if (echan->slot[i] >= 0) {
828 edma_free_slot(echan->slot[i]);
829 echan->slot[i] = -1;
830 }
831 }
832
833 /* Free EDMA channel */
834 if (echan->alloced) {
835 edma_free_channel(echan->ch_num);
836 echan->alloced = false;
837 }
838
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300839 dev_dbg(dev, "freeing channel for %u\n", echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400840}
841
842/* Send pending descriptor to hardware */
843static void edma_issue_pending(struct dma_chan *chan)
844{
845 struct edma_chan *echan = to_edma_chan(chan);
846 unsigned long flags;
847
848 spin_lock_irqsave(&echan->vchan.lock, flags);
849 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
850 edma_execute(echan);
851 spin_unlock_irqrestore(&echan->vchan.lock, flags);
852}
853
Matt Porterc2dde5f2012-08-22 21:09:34 -0400854/* Check request completion status */
855static enum dma_status edma_tx_status(struct dma_chan *chan,
856 dma_cookie_t cookie,
857 struct dma_tx_state *txstate)
858{
859 struct edma_chan *echan = to_edma_chan(chan);
860 struct virt_dma_desc *vdesc;
861 enum dma_status ret;
862 unsigned long flags;
863
864 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +0530865 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400866 return ret;
867
868 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -0500869 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500870 txstate->residue = echan->edesc->residue;
Thomas Gleixnerde135932014-04-28 14:19:51 -0500871 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
872 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400873 spin_unlock_irqrestore(&echan->vchan.lock, flags);
874
875 return ret;
876}
877
878static void __init edma_chan_init(struct edma_cc *ecc,
879 struct dma_device *dma,
880 struct edma_chan *echans)
881{
882 int i, j;
883
884 for (i = 0; i < EDMA_CHANS; i++) {
885 struct edma_chan *echan = &echans[i];
886 echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
887 echan->ecc = ecc;
888 echan->vchan.desc_free = edma_desc_free;
889
890 vchan_init(&echan->vchan, dma);
891
892 INIT_LIST_HEAD(&echan->node);
893 for (j = 0; j < EDMA_MAX_SLOTS; j++)
894 echan->slot[j] = -1;
895 }
896}
897
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +0300898#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
899 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
900 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
901
902static int edma_dma_device_slave_caps(struct dma_chan *dchan,
903 struct dma_slave_caps *caps)
904{
905 caps->src_addr_widths = EDMA_DMA_BUSWIDTHS;
906 caps->dstn_addr_widths = EDMA_DMA_BUSWIDTHS;
907 caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
908 caps->cmd_pause = true;
909 caps->cmd_terminate = true;
910 caps->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
911
912 return 0;
913}
914
Matt Porterc2dde5f2012-08-22 21:09:34 -0400915static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
916 struct device *dev)
917{
918 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500919 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500920 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400921 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
922 dma->device_free_chan_resources = edma_free_chan_resources;
923 dma->device_issue_pending = edma_issue_pending;
924 dma->device_tx_status = edma_tx_status;
925 dma->device_control = edma_control;
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +0300926 dma->device_slave_caps = edma_dma_device_slave_caps;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400927 dma->dev = dev;
928
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500929 /*
930 * code using dma memcpy must make sure alignment of
931 * length is at dma->copy_align boundary.
932 */
933 dma->copy_align = DMA_SLAVE_BUSWIDTH_4_BYTES;
934
Matt Porterc2dde5f2012-08-22 21:09:34 -0400935 INIT_LIST_HEAD(&dma->channels);
936}
937
Bill Pemberton463a1f82012-11-19 13:22:55 -0500938static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400939{
940 struct edma_cc *ecc;
941 int ret;
942
Russell King94cb0e72013-06-27 13:45:16 +0100943 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
944 if (ret)
945 return ret;
946
Matt Porterc2dde5f2012-08-22 21:09:34 -0400947 ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
948 if (!ecc) {
949 dev_err(&pdev->dev, "Can't allocate controller\n");
950 return -ENOMEM;
951 }
952
953 ecc->ctlr = pdev->id;
954 ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
955 if (ecc->dummy_slot < 0) {
956 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
957 return -EIO;
958 }
959
960 dma_cap_zero(ecc->dma_slave.cap_mask);
961 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
Peter Ujfalusi232b223d2014-04-14 14:42:00 +0300962 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500963 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400964
965 edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
966
967 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
968
969 ret = dma_async_device_register(&ecc->dma_slave);
970 if (ret)
971 goto err_reg1;
972
973 platform_set_drvdata(pdev, ecc);
974
975 dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
976
977 return 0;
978
979err_reg1:
980 edma_free_slot(ecc->dummy_slot);
981 return ret;
982}
983
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -0800984static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400985{
986 struct device *dev = &pdev->dev;
987 struct edma_cc *ecc = dev_get_drvdata(dev);
988
989 dma_async_device_unregister(&ecc->dma_slave);
990 edma_free_slot(ecc->dummy_slot);
991
992 return 0;
993}
994
995static struct platform_driver edma_driver = {
996 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -0500997 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400998 .driver = {
999 .name = "edma-dma-engine",
1000 .owner = THIS_MODULE,
1001 },
1002};
1003
1004bool edma_filter_fn(struct dma_chan *chan, void *param)
1005{
1006 if (chan->device->dev->driver == &edma_driver.driver) {
1007 struct edma_chan *echan = to_edma_chan(chan);
1008 unsigned ch_req = *(unsigned *)param;
1009 return ch_req == echan->ch_num;
1010 }
1011 return false;
1012}
1013EXPORT_SYMBOL(edma_filter_fn);
1014
1015static struct platform_device *pdev0, *pdev1;
1016
1017static const struct platform_device_info edma_dev_info0 = {
1018 .name = "edma-dma-engine",
1019 .id = 0,
Russell King94cb0e72013-06-27 13:45:16 +01001020 .dma_mask = DMA_BIT_MASK(32),
Matt Porterc2dde5f2012-08-22 21:09:34 -04001021};
1022
1023static const struct platform_device_info edma_dev_info1 = {
1024 .name = "edma-dma-engine",
1025 .id = 1,
Russell King94cb0e72013-06-27 13:45:16 +01001026 .dma_mask = DMA_BIT_MASK(32),
Matt Porterc2dde5f2012-08-22 21:09:34 -04001027};
1028
1029static int edma_init(void)
1030{
1031 int ret = platform_driver_register(&edma_driver);
1032
1033 if (ret == 0) {
1034 pdev0 = platform_device_register_full(&edma_dev_info0);
1035 if (IS_ERR(pdev0)) {
1036 platform_driver_unregister(&edma_driver);
1037 ret = PTR_ERR(pdev0);
1038 goto out;
1039 }
1040 }
1041
1042 if (EDMA_CTLRS == 2) {
1043 pdev1 = platform_device_register_full(&edma_dev_info1);
1044 if (IS_ERR(pdev1)) {
1045 platform_driver_unregister(&edma_driver);
1046 platform_device_unregister(pdev0);
1047 ret = PTR_ERR(pdev1);
1048 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001049 }
1050
1051out:
1052 return ret;
1053}
1054subsys_initcall(edma_init);
1055
1056static void __exit edma_exit(void)
1057{
1058 platform_device_unregister(pdev0);
1059 if (pdev1)
1060 platform_device_unregister(pdev1);
1061 platform_driver_unregister(&edma_driver);
1062}
1063module_exit(edma_exit);
1064
Josh Boyerd71505b2013-09-04 10:32:50 -04001065MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -04001066MODULE_DESCRIPTION("TI EDMA DMA engine driver");
1067MODULE_LICENSE("GPL v2");