dmaengine: rcar-dmac: Fix uninitialized variable usage
[linux-2.6-block.git] / drivers / dma / edma.c
CommitLineData
c2dde5f8
MP
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>
ed64610f 26#include <linux/of.h>
c2dde5f8 27
3ad7a42d 28#include <linux/platform_data/edma.h>
c2dde5f8
MP
29
30#include "dmaengine.h"
31#include "virt-dma.h"
32
33/*
34 * This will go away when the private EDMA API is folded
35 * into this driver and the platform device(s) are
36 * instantiated in the arch code. We can only get away
37 * with this simplification because DA8XX may not be built
38 * in the same kernel image with other DaVinci parts. This
39 * avoids having to sprinkle dmaengine driver platform devices
40 * and data throughout all the existing board files.
41 */
42#ifdef CONFIG_ARCH_DAVINCI_DA8XX
43#define EDMA_CTLRS 2
44#define EDMA_CHANS 32
45#else
46#define EDMA_CTLRS 1
47#define EDMA_CHANS 64
48#endif /* CONFIG_ARCH_DAVINCI_DA8XX */
49
2abd5f1b
JF
50/*
51 * Max of 20 segments per channel to conserve PaRAM slots
52 * Also note that MAX_NR_SG should be atleast the no.of periods
53 * that are required for ASoC, otherwise DMA prep calls will
54 * fail. Today davinci-pcm is the only user of this driver and
55 * requires atleast 17 slots, so we setup the default to 20.
56 */
57#define MAX_NR_SG 20
c2dde5f8
MP
58#define EDMA_MAX_SLOTS MAX_NR_SG
59#define EDMA_DESCRIPTORS 16
60
b5088ad9 61struct edma_pset {
c2da2340
TG
62 u32 len;
63 dma_addr_t addr;
b5088ad9
TG
64 struct edmacc_param param;
65};
66
c2dde5f8
MP
67struct edma_desc {
68 struct virt_dma_desc vdesc;
69 struct list_head node;
c2da2340 70 enum dma_transfer_direction direction;
50a9c707 71 int cyclic;
c2dde5f8
MP
72 int absync;
73 int pset_nr;
04361d88 74 struct edma_chan *echan;
53407062 75 int processed;
04361d88
JF
76
77 /*
78 * The following 4 elements are used for residue accounting.
79 *
80 * - processed_stat: the number of SG elements we have traversed
81 * so far to cover accounting. This is updated directly to processed
82 * during edma_callback and is always <= processed, because processed
83 * refers to the number of pending transfer (programmed to EDMA
84 * controller), where as processed_stat tracks number of transfers
85 * accounted for so far.
86 *
87 * - residue: The amount of bytes we have left to transfer for this desc
88 *
89 * - residue_stat: The residue in bytes of data we have covered
90 * so far for accounting. This is updated directly to residue
91 * during callbacks to keep it current.
92 *
93 * - sg_len: Tracks the length of the current intermediate transfer,
94 * this is required to update the residue during intermediate transfer
95 * completion callback.
96 */
740b41f7 97 int processed_stat;
740b41f7 98 u32 sg_len;
04361d88 99 u32 residue;
740b41f7 100 u32 residue_stat;
04361d88 101
b5088ad9 102 struct edma_pset pset[0];
c2dde5f8
MP
103};
104
105struct edma_cc;
106
107struct edma_chan {
108 struct virt_dma_chan vchan;
109 struct list_head node;
110 struct edma_desc *edesc;
111 struct edma_cc *ecc;
112 int ch_num;
113 bool alloced;
114 int slot[EDMA_MAX_SLOTS];
c5f47990 115 int missed;
661f7cb5 116 struct dma_slave_config cfg;
c2dde5f8
MP
117};
118
119struct edma_cc {
120 int ctlr;
121 struct dma_device dma_slave;
122 struct edma_chan slave_chans[EDMA_CHANS];
123 int num_slave_chans;
124 int dummy_slot;
125};
126
127static inline struct edma_cc *to_edma_cc(struct dma_device *d)
128{
129 return container_of(d, struct edma_cc, dma_slave);
130}
131
132static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
133{
134 return container_of(c, struct edma_chan, vchan.chan);
135}
136
137static inline struct edma_desc
138*to_edma_desc(struct dma_async_tx_descriptor *tx)
139{
140 return container_of(tx, struct edma_desc, vdesc.tx);
141}
142
143static void edma_desc_free(struct virt_dma_desc *vdesc)
144{
145 kfree(container_of(vdesc, struct edma_desc, vdesc));
146}
147
148/* Dispatch a queued descriptor to the controller (caller holds lock) */
149static void edma_execute(struct edma_chan *echan)
150{
53407062 151 struct virt_dma_desc *vdesc;
c2dde5f8 152 struct edma_desc *edesc;
53407062
JF
153 struct device *dev = echan->vchan.chan.device->dev;
154 int i, j, left, nslots;
155
156 /* If either we processed all psets or we're still not started */
157 if (!echan->edesc ||
158 echan->edesc->pset_nr == echan->edesc->processed) {
159 /* Get next vdesc */
160 vdesc = vchan_next_desc(&echan->vchan);
161 if (!vdesc) {
162 echan->edesc = NULL;
163 return;
164 }
165 list_del(&vdesc->node);
166 echan->edesc = to_edma_desc(&vdesc->tx);
c2dde5f8
MP
167 }
168
53407062 169 edesc = echan->edesc;
c2dde5f8 170
53407062
JF
171 /* Find out how many left */
172 left = edesc->pset_nr - edesc->processed;
173 nslots = min(MAX_NR_SG, left);
740b41f7 174 edesc->sg_len = 0;
c2dde5f8
MP
175
176 /* Write descriptor PaRAM set(s) */
53407062
JF
177 for (i = 0; i < nslots; i++) {
178 j = i + edesc->processed;
b5088ad9 179 edma_write_slot(echan->slot[i], &edesc->pset[j].param);
740b41f7 180 edesc->sg_len += edesc->pset[j].len;
83bb3126 181 dev_vdbg(echan->vchan.chan.device->dev,
c2dde5f8
MP
182 "\n pset[%d]:\n"
183 " chnum\t%d\n"
184 " slot\t%d\n"
185 " opt\t%08x\n"
186 " src\t%08x\n"
187 " dst\t%08x\n"
188 " abcnt\t%08x\n"
189 " ccnt\t%08x\n"
190 " bidx\t%08x\n"
191 " cidx\t%08x\n"
192 " lkrld\t%08x\n",
53407062 193 j, echan->ch_num, echan->slot[i],
b5088ad9
TG
194 edesc->pset[j].param.opt,
195 edesc->pset[j].param.src,
196 edesc->pset[j].param.dst,
197 edesc->pset[j].param.a_b_cnt,
198 edesc->pset[j].param.ccnt,
199 edesc->pset[j].param.src_dst_bidx,
200 edesc->pset[j].param.src_dst_cidx,
201 edesc->pset[j].param.link_bcntrld);
c2dde5f8 202 /* Link to the previous slot if not the last set */
53407062 203 if (i != (nslots - 1))
c2dde5f8 204 edma_link(echan->slot[i], echan->slot[i+1]);
c2dde5f8
MP
205 }
206
53407062
JF
207 edesc->processed += nslots;
208
b267b3bc
JF
209 /*
210 * If this is either the last set in a set of SG-list transactions
211 * then setup a link to the dummy slot, this results in all future
212 * events being absorbed and that's OK because we're done
213 */
50a9c707
JF
214 if (edesc->processed == edesc->pset_nr) {
215 if (edesc->cyclic)
216 edma_link(echan->slot[nslots-1], echan->slot[1]);
217 else
218 edma_link(echan->slot[nslots-1],
219 echan->ecc->dummy_slot);
220 }
b267b3bc 221
53407062 222 if (edesc->processed <= MAX_NR_SG) {
9aac9096
PU
223 dev_dbg(dev, "first transfer starting on channel %d\n",
224 echan->ch_num);
53407062 225 edma_start(echan->ch_num);
5fc68a6c
SN
226 } else {
227 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
228 echan->ch_num, edesc->processed);
229 edma_resume(echan->ch_num);
53407062 230 }
c5f47990
JF
231
232 /*
233 * This happens due to setup times between intermediate transfers
234 * in long SG lists which have to be broken up into transfers of
235 * MAX_NR_SG
236 */
237 if (echan->missed) {
9aac9096 238 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
c5f47990
JF
239 edma_clean_channel(echan->ch_num);
240 edma_stop(echan->ch_num);
241 edma_start(echan->ch_num);
242 edma_trigger_channel(echan->ch_num);
243 echan->missed = 0;
244 }
c2dde5f8
MP
245}
246
aa7c09b6 247static int edma_terminate_all(struct dma_chan *chan)
c2dde5f8 248{
aa7c09b6 249 struct edma_chan *echan = to_edma_chan(chan);
c2dde5f8
MP
250 unsigned long flags;
251 LIST_HEAD(head);
252
253 spin_lock_irqsave(&echan->vchan.lock, flags);
254
255 /*
256 * Stop DMA activity: we assume the callback will not be called
257 * after edma_dma() returns (even if it does, it will see
258 * echan->edesc is NULL and exit.)
259 */
260 if (echan->edesc) {
8e8805d5 261 int cyclic = echan->edesc->cyclic;
c2dde5f8
MP
262 echan->edesc = NULL;
263 edma_stop(echan->ch_num);
8e8805d5
PU
264 /* Move the cyclic channel back to default queue */
265 if (cyclic)
266 edma_assign_channel_eventq(echan->ch_num,
267 EVENTQ_DEFAULT);
c2dde5f8
MP
268 }
269
270 vchan_get_all_descriptors(&echan->vchan, &head);
271 spin_unlock_irqrestore(&echan->vchan.lock, flags);
272 vchan_dma_desc_free_list(&echan->vchan, &head);
273
274 return 0;
275}
276
aa7c09b6 277static int edma_slave_config(struct dma_chan *chan,
661f7cb5 278 struct dma_slave_config *cfg)
c2dde5f8 279{
aa7c09b6
MR
280 struct edma_chan *echan = to_edma_chan(chan);
281
661f7cb5
MP
282 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
283 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
c2dde5f8
MP
284 return -EINVAL;
285
661f7cb5 286 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
c2dde5f8
MP
287
288 return 0;
289}
290
aa7c09b6 291static int edma_dma_pause(struct dma_chan *chan)
72c7b67a 292{
aa7c09b6
MR
293 struct edma_chan *echan = to_edma_chan(chan);
294
72c7b67a 295 /* Pause/Resume only allowed with cyclic mode */
639559ad 296 if (!echan->edesc || !echan->edesc->cyclic)
72c7b67a
PU
297 return -EINVAL;
298
299 edma_pause(echan->ch_num);
300 return 0;
301}
302
aa7c09b6 303static int edma_dma_resume(struct dma_chan *chan)
72c7b67a 304{
aa7c09b6
MR
305 struct edma_chan *echan = to_edma_chan(chan);
306
72c7b67a
PU
307 /* Pause/Resume only allowed with cyclic mode */
308 if (!echan->edesc->cyclic)
309 return -EINVAL;
310
311 edma_resume(echan->ch_num);
312 return 0;
313}
314
fd009035
JF
315/*
316 * A PaRAM set configuration abstraction used by other modes
317 * @chan: Channel who's PaRAM set we're configuring
318 * @pset: PaRAM set to initialize and setup.
319 * @src_addr: Source address of the DMA
320 * @dst_addr: Destination address of the DMA
321 * @burst: In units of dev_width, how much to send
322 * @dev_width: How much is the dev_width
323 * @dma_length: Total length of the DMA transfer
324 * @direction: Direction of the transfer
325 */
b5088ad9 326static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
fd009035
JF
327 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
328 enum dma_slave_buswidth dev_width, unsigned int dma_length,
329 enum dma_transfer_direction direction)
330{
331 struct edma_chan *echan = to_edma_chan(chan);
332 struct device *dev = chan->device->dev;
b5088ad9 333 struct edmacc_param *param = &epset->param;
fd009035
JF
334 int acnt, bcnt, ccnt, cidx;
335 int src_bidx, dst_bidx, src_cidx, dst_cidx;
336 int absync;
337
338 acnt = dev_width;
b2b617de
PU
339
340 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
341 if (!burst)
342 burst = 1;
fd009035
JF
343 /*
344 * If the maxburst is equal to the fifo width, use
345 * A-synced transfers. This allows for large contiguous
346 * buffer transfers using only one PaRAM set.
347 */
348 if (burst == 1) {
349 /*
350 * For the A-sync case, bcnt and ccnt are the remainder
351 * and quotient respectively of the division of:
352 * (dma_length / acnt) by (SZ_64K -1). This is so
353 * that in case bcnt over flows, we have ccnt to use.
354 * Note: In A-sync tranfer only, bcntrld is used, but it
355 * only applies for sg_dma_len(sg) >= SZ_64K.
356 * In this case, the best way adopted is- bccnt for the
357 * first frame will be the remainder below. Then for
358 * every successive frame, bcnt will be SZ_64K-1. This
359 * is assured as bcntrld = 0xffff in end of function.
360 */
361 absync = false;
362 ccnt = dma_length / acnt / (SZ_64K - 1);
363 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
364 /*
365 * If bcnt is non-zero, we have a remainder and hence an
366 * extra frame to transfer, so increment ccnt.
367 */
368 if (bcnt)
369 ccnt++;
370 else
371 bcnt = SZ_64K - 1;
372 cidx = acnt;
373 } else {
374 /*
375 * If maxburst is greater than the fifo address_width,
376 * use AB-synced transfers where A count is the fifo
377 * address_width and B count is the maxburst. In this
378 * case, we are limited to transfers of C count frames
379 * of (address_width * maxburst) where C count is limited
380 * to SZ_64K-1. This places an upper bound on the length
381 * of an SG segment that can be handled.
382 */
383 absync = true;
384 bcnt = burst;
385 ccnt = dma_length / (acnt * bcnt);
386 if (ccnt > (SZ_64K - 1)) {
387 dev_err(dev, "Exceeded max SG segment size\n");
388 return -EINVAL;
389 }
390 cidx = acnt * bcnt;
391 }
392
c2da2340
TG
393 epset->len = dma_length;
394
fd009035
JF
395 if (direction == DMA_MEM_TO_DEV) {
396 src_bidx = acnt;
397 src_cidx = cidx;
398 dst_bidx = 0;
399 dst_cidx = 0;
c2da2340 400 epset->addr = src_addr;
fd009035
JF
401 } else if (direction == DMA_DEV_TO_MEM) {
402 src_bidx = 0;
403 src_cidx = 0;
404 dst_bidx = acnt;
405 dst_cidx = cidx;
c2da2340 406 epset->addr = dst_addr;
8cc3e30b
JF
407 } else if (direction == DMA_MEM_TO_MEM) {
408 src_bidx = acnt;
409 src_cidx = cidx;
410 dst_bidx = acnt;
411 dst_cidx = cidx;
fd009035
JF
412 } else {
413 dev_err(dev, "%s: direction not implemented yet\n", __func__);
414 return -EINVAL;
415 }
416
b5088ad9 417 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
fd009035
JF
418 /* Configure A or AB synchronized transfers */
419 if (absync)
b5088ad9 420 param->opt |= SYNCDIM;
fd009035 421
b5088ad9
TG
422 param->src = src_addr;
423 param->dst = dst_addr;
fd009035 424
b5088ad9
TG
425 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
426 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
fd009035 427
b5088ad9
TG
428 param->a_b_cnt = bcnt << 16 | acnt;
429 param->ccnt = ccnt;
fd009035
JF
430 /*
431 * Only time when (bcntrld) auto reload is required is for
432 * A-sync case, and in this case, a requirement of reload value
433 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
434 * and then later will be populated by edma_execute.
435 */
b5088ad9 436 param->link_bcntrld = 0xffffffff;
fd009035
JF
437 return absync;
438}
439
c2dde5f8
MP
440static struct dma_async_tx_descriptor *edma_prep_slave_sg(
441 struct dma_chan *chan, struct scatterlist *sgl,
442 unsigned int sg_len, enum dma_transfer_direction direction,
443 unsigned long tx_flags, void *context)
444{
445 struct edma_chan *echan = to_edma_chan(chan);
446 struct device *dev = chan->device->dev;
447 struct edma_desc *edesc;
fd009035 448 dma_addr_t src_addr = 0, dst_addr = 0;
661f7cb5
MP
449 enum dma_slave_buswidth dev_width;
450 u32 burst;
c2dde5f8 451 struct scatterlist *sg;
fd009035 452 int i, nslots, ret;
c2dde5f8
MP
453
454 if (unlikely(!echan || !sgl || !sg_len))
455 return NULL;
456
661f7cb5 457 if (direction == DMA_DEV_TO_MEM) {
fd009035 458 src_addr = echan->cfg.src_addr;
661f7cb5
MP
459 dev_width = echan->cfg.src_addr_width;
460 burst = echan->cfg.src_maxburst;
461 } else if (direction == DMA_MEM_TO_DEV) {
fd009035 462 dst_addr = echan->cfg.dst_addr;
661f7cb5
MP
463 dev_width = echan->cfg.dst_addr_width;
464 burst = echan->cfg.dst_maxburst;
465 } else {
e6fad592 466 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
661f7cb5
MP
467 return NULL;
468 }
469
470 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
c594c891 471 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
c2dde5f8
MP
472 return NULL;
473 }
474
c2dde5f8
MP
475 edesc = kzalloc(sizeof(*edesc) + sg_len *
476 sizeof(edesc->pset[0]), GFP_ATOMIC);
477 if (!edesc) {
c594c891 478 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
c2dde5f8
MP
479 return NULL;
480 }
481
482 edesc->pset_nr = sg_len;
b6205c39 483 edesc->residue = 0;
c2da2340 484 edesc->direction = direction;
740b41f7 485 edesc->echan = echan;
c2dde5f8 486
6fbe24da
JF
487 /* Allocate a PaRAM slot, if needed */
488 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
489
490 for (i = 0; i < nslots; i++) {
c2dde5f8
MP
491 if (echan->slot[i] < 0) {
492 echan->slot[i] =
493 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
494 EDMA_SLOT_ANY);
495 if (echan->slot[i] < 0) {
4b6271a6 496 kfree(edesc);
c594c891
PU
497 dev_err(dev, "%s: Failed to allocate slot\n",
498 __func__);
c2dde5f8
MP
499 return NULL;
500 }
501 }
6fbe24da
JF
502 }
503
504 /* Configure PaRAM sets for each SG */
505 for_each_sg(sgl, sg, sg_len, i) {
fd009035
JF
506 /* Get address for each SG */
507 if (direction == DMA_DEV_TO_MEM)
508 dst_addr = sg_dma_address(sg);
509 else
510 src_addr = sg_dma_address(sg);
c2dde5f8 511
fd009035
JF
512 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
513 dst_addr, burst, dev_width,
514 sg_dma_len(sg), direction);
b967aecf
VK
515 if (ret < 0) {
516 kfree(edesc);
fd009035 517 return NULL;
c2dde5f8
MP
518 }
519
fd009035 520 edesc->absync = ret;
b6205c39 521 edesc->residue += sg_dma_len(sg);
6fbe24da
JF
522
523 /* If this is the last in a current SG set of transactions,
524 enable interrupts so that next set is processed */
525 if (!((i+1) % MAX_NR_SG))
b5088ad9 526 edesc->pset[i].param.opt |= TCINTEN;
6fbe24da 527
c2dde5f8
MP
528 /* If this is the last set, enable completion interrupt flag */
529 if (i == sg_len - 1)
b5088ad9 530 edesc->pset[i].param.opt |= TCINTEN;
c2dde5f8 531 }
740b41f7 532 edesc->residue_stat = edesc->residue;
c2dde5f8 533
c2dde5f8
MP
534 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
535}
c2dde5f8 536
8cc3e30b
JF
537struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
538 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
539 size_t len, unsigned long tx_flags)
540{
541 int ret;
542 struct edma_desc *edesc;
543 struct device *dev = chan->device->dev;
544 struct edma_chan *echan = to_edma_chan(chan);
545
546 if (unlikely(!echan || !len))
547 return NULL;
548
549 edesc = kzalloc(sizeof(*edesc) + sizeof(edesc->pset[0]), GFP_ATOMIC);
550 if (!edesc) {
551 dev_dbg(dev, "Failed to allocate a descriptor\n");
552 return NULL;
553 }
554
555 edesc->pset_nr = 1;
556
557 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
558 DMA_SLAVE_BUSWIDTH_4_BYTES, len, DMA_MEM_TO_MEM);
559 if (ret < 0)
560 return NULL;
561
562 edesc->absync = ret;
563
564 /*
565 * Enable intermediate transfer chaining to re-trigger channel
566 * on completion of every TR, and enable transfer-completion
567 * interrupt on completion of the whole transfer.
568 */
b0cce4ca
JF
569 edesc->pset[0].param.opt |= ITCCHEN;
570 edesc->pset[0].param.opt |= TCINTEN;
8cc3e30b
JF
571
572 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
573}
574
50a9c707
JF
575static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
576 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
577 size_t period_len, enum dma_transfer_direction direction,
31c1e5a1 578 unsigned long tx_flags)
50a9c707
JF
579{
580 struct edma_chan *echan = to_edma_chan(chan);
581 struct device *dev = chan->device->dev;
582 struct edma_desc *edesc;
583 dma_addr_t src_addr, dst_addr;
584 enum dma_slave_buswidth dev_width;
585 u32 burst;
586 int i, ret, nslots;
587
588 if (unlikely(!echan || !buf_len || !period_len))
589 return NULL;
590
591 if (direction == DMA_DEV_TO_MEM) {
592 src_addr = echan->cfg.src_addr;
593 dst_addr = buf_addr;
594 dev_width = echan->cfg.src_addr_width;
595 burst = echan->cfg.src_maxburst;
596 } else if (direction == DMA_MEM_TO_DEV) {
597 src_addr = buf_addr;
598 dst_addr = echan->cfg.dst_addr;
599 dev_width = echan->cfg.dst_addr_width;
600 burst = echan->cfg.dst_maxburst;
601 } else {
e6fad592 602 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
50a9c707
JF
603 return NULL;
604 }
605
606 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
c594c891 607 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
50a9c707
JF
608 return NULL;
609 }
610
611 if (unlikely(buf_len % period_len)) {
612 dev_err(dev, "Period should be multiple of Buffer length\n");
613 return NULL;
614 }
615
616 nslots = (buf_len / period_len) + 1;
617
618 /*
619 * Cyclic DMA users such as audio cannot tolerate delays introduced
620 * by cases where the number of periods is more than the maximum
621 * number of SGs the EDMA driver can handle at a time. For DMA types
622 * such as Slave SGs, such delays are tolerable and synchronized,
623 * but the synchronization is difficult to achieve with Cyclic and
624 * cannot be guaranteed, so we error out early.
625 */
626 if (nslots > MAX_NR_SG)
627 return NULL;
628
629 edesc = kzalloc(sizeof(*edesc) + nslots *
630 sizeof(edesc->pset[0]), GFP_ATOMIC);
631 if (!edesc) {
c594c891 632 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
50a9c707
JF
633 return NULL;
634 }
635
636 edesc->cyclic = 1;
637 edesc->pset_nr = nslots;
740b41f7 638 edesc->residue = edesc->residue_stat = buf_len;
c2da2340 639 edesc->direction = direction;
740b41f7 640 edesc->echan = echan;
50a9c707 641
83bb3126
PU
642 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
643 __func__, echan->ch_num, nslots, period_len, buf_len);
50a9c707
JF
644
645 for (i = 0; i < nslots; i++) {
646 /* Allocate a PaRAM slot, if needed */
647 if (echan->slot[i] < 0) {
648 echan->slot[i] =
649 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
650 EDMA_SLOT_ANY);
651 if (echan->slot[i] < 0) {
e3ddc979 652 kfree(edesc);
c594c891
PU
653 dev_err(dev, "%s: Failed to allocate slot\n",
654 __func__);
50a9c707
JF
655 return NULL;
656 }
657 }
658
659 if (i == nslots - 1) {
660 memcpy(&edesc->pset[i], &edesc->pset[0],
661 sizeof(edesc->pset[0]));
662 break;
663 }
664
665 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
666 dst_addr, burst, dev_width, period_len,
667 direction);
e3ddc979
CE
668 if (ret < 0) {
669 kfree(edesc);
50a9c707 670 return NULL;
e3ddc979 671 }
c2dde5f8 672
50a9c707
JF
673 if (direction == DMA_DEV_TO_MEM)
674 dst_addr += period_len;
675 else
676 src_addr += period_len;
c2dde5f8 677
83bb3126
PU
678 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
679 dev_vdbg(dev,
50a9c707
JF
680 "\n pset[%d]:\n"
681 " chnum\t%d\n"
682 " slot\t%d\n"
683 " opt\t%08x\n"
684 " src\t%08x\n"
685 " dst\t%08x\n"
686 " abcnt\t%08x\n"
687 " ccnt\t%08x\n"
688 " bidx\t%08x\n"
689 " cidx\t%08x\n"
690 " lkrld\t%08x\n",
691 i, echan->ch_num, echan->slot[i],
b5088ad9
TG
692 edesc->pset[i].param.opt,
693 edesc->pset[i].param.src,
694 edesc->pset[i].param.dst,
695 edesc->pset[i].param.a_b_cnt,
696 edesc->pset[i].param.ccnt,
697 edesc->pset[i].param.src_dst_bidx,
698 edesc->pset[i].param.src_dst_cidx,
699 edesc->pset[i].param.link_bcntrld);
50a9c707
JF
700
701 edesc->absync = ret;
702
703 /*
a1f146f3 704 * Enable period interrupt only if it is requested
50a9c707 705 */
a1f146f3
PU
706 if (tx_flags & DMA_PREP_INTERRUPT)
707 edesc->pset[i].param.opt |= TCINTEN;
c2dde5f8
MP
708 }
709
8e8805d5
PU
710 /* Place the cyclic channel to highest priority queue */
711 edma_assign_channel_eventq(echan->ch_num, EVENTQ_0);
712
c2dde5f8
MP
713 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
714}
715
716static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
717{
718 struct edma_chan *echan = data;
719 struct device *dev = echan->vchan.chan.device->dev;
720 struct edma_desc *edesc;
c5f47990 721 struct edmacc_param p;
c2dde5f8 722
50a9c707
JF
723 edesc = echan->edesc;
724
725 /* Pause the channel for non-cyclic */
726 if (!edesc || (edesc && !edesc->cyclic))
727 edma_pause(echan->ch_num);
c2dde5f8
MP
728
729 switch (ch_status) {
db60d8da 730 case EDMA_DMA_COMPLETE:
406efb1a 731 spin_lock(&echan->vchan.lock);
c2dde5f8 732
c2dde5f8 733 if (edesc) {
50a9c707
JF
734 if (edesc->cyclic) {
735 vchan_cyclic_callback(&edesc->vdesc);
736 } else if (edesc->processed == edesc->pset_nr) {
53407062 737 dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
b6205c39 738 edesc->residue = 0;
53407062
JF
739 edma_stop(echan->ch_num);
740 vchan_cookie_complete(&edesc->vdesc);
50a9c707 741 edma_execute(echan);
53407062
JF
742 } else {
743 dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
740b41f7
TG
744
745 /* Update statistics for tx_status */
746 edesc->residue -= edesc->sg_len;
747 edesc->residue_stat = edesc->residue;
748 edesc->processed_stat = edesc->processed;
749
50a9c707 750 edma_execute(echan);
53407062 751 }
c2dde5f8
MP
752 }
753
406efb1a 754 spin_unlock(&echan->vchan.lock);
c2dde5f8
MP
755
756 break;
db60d8da 757 case EDMA_DMA_CC_ERROR:
406efb1a 758 spin_lock(&echan->vchan.lock);
c5f47990
JF
759
760 edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
761
762 /*
763 * Issue later based on missed flag which will be sure
764 * to happen as:
765 * (1) we finished transmitting an intermediate slot and
766 * edma_execute is coming up.
767 * (2) or we finished current transfer and issue will
768 * call edma_execute.
769 *
770 * Important note: issuing can be dangerous here and
771 * lead to some nasty recursion when we are in a NULL
772 * slot. So we avoid doing so and set the missed flag.
773 */
774 if (p.a_b_cnt == 0 && p.ccnt == 0) {
775 dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
776 echan->missed = 1;
777 } else {
778 /*
779 * The slot is already programmed but the event got
780 * missed, so its safe to issue it here.
781 */
782 dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
783 edma_clean_channel(echan->ch_num);
784 edma_stop(echan->ch_num);
785 edma_start(echan->ch_num);
786 edma_trigger_channel(echan->ch_num);
787 }
788
406efb1a 789 spin_unlock(&echan->vchan.lock);
c5f47990 790
c2dde5f8
MP
791 break;
792 default:
793 break;
794 }
795}
796
797/* Alloc channel resources */
798static int edma_alloc_chan_resources(struct dma_chan *chan)
799{
800 struct edma_chan *echan = to_edma_chan(chan);
801 struct device *dev = chan->device->dev;
802 int ret;
803 int a_ch_num;
804 LIST_HEAD(descs);
805
806 a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
807 chan, EVENTQ_DEFAULT);
808
809 if (a_ch_num < 0) {
810 ret = -ENODEV;
811 goto err_no_chan;
812 }
813
814 if (a_ch_num != echan->ch_num) {
815 dev_err(dev, "failed to allocate requested channel %u:%u\n",
816 EDMA_CTLR(echan->ch_num),
817 EDMA_CHAN_SLOT(echan->ch_num));
818 ret = -ENODEV;
819 goto err_wrong_chan;
820 }
821
822 echan->alloced = true;
823 echan->slot[0] = echan->ch_num;
824
9aac9096 825 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
0e772c67 826 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
c2dde5f8
MP
827
828 return 0;
829
830err_wrong_chan:
831 edma_free_channel(a_ch_num);
832err_no_chan:
833 return ret;
834}
835
836/* Free channel resources */
837static void edma_free_chan_resources(struct dma_chan *chan)
838{
839 struct edma_chan *echan = to_edma_chan(chan);
840 struct device *dev = chan->device->dev;
841 int i;
842
843 /* Terminate transfers */
844 edma_stop(echan->ch_num);
845
846 vchan_free_chan_resources(&echan->vchan);
847
848 /* Free EDMA PaRAM slots */
849 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
850 if (echan->slot[i] >= 0) {
851 edma_free_slot(echan->slot[i]);
852 echan->slot[i] = -1;
853 }
854 }
855
856 /* Free EDMA channel */
857 if (echan->alloced) {
858 edma_free_channel(echan->ch_num);
859 echan->alloced = false;
860 }
861
0e772c67 862 dev_dbg(dev, "freeing channel for %u\n", echan->ch_num);
c2dde5f8
MP
863}
864
865/* Send pending descriptor to hardware */
866static void edma_issue_pending(struct dma_chan *chan)
867{
868 struct edma_chan *echan = to_edma_chan(chan);
869 unsigned long flags;
870
871 spin_lock_irqsave(&echan->vchan.lock, flags);
872 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
873 edma_execute(echan);
874 spin_unlock_irqrestore(&echan->vchan.lock, flags);
875}
876
740b41f7
TG
877static u32 edma_residue(struct edma_desc *edesc)
878{
879 bool dst = edesc->direction == DMA_DEV_TO_MEM;
880 struct edma_pset *pset = edesc->pset;
881 dma_addr_t done, pos;
882 int i;
883
884 /*
885 * We always read the dst/src position from the first RamPar
886 * pset. That's the one which is active now.
887 */
888 pos = edma_get_position(edesc->echan->slot[0], dst);
889
890 /*
891 * Cyclic is simple. Just subtract pset[0].addr from pos.
892 *
893 * We never update edesc->residue in the cyclic case, so we
894 * can tell the remaining room to the end of the circular
895 * buffer.
896 */
897 if (edesc->cyclic) {
898 done = pos - pset->addr;
899 edesc->residue_stat = edesc->residue - done;
900 return edesc->residue_stat;
901 }
902
903 /*
904 * For SG operation we catch up with the last processed
905 * status.
906 */
907 pset += edesc->processed_stat;
908
909 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
910 /*
911 * If we are inside this pset address range, we know
912 * this is the active one. Get the current delta and
913 * stop walking the psets.
914 */
915 if (pos >= pset->addr && pos < pset->addr + pset->len)
916 return edesc->residue_stat - (pos - pset->addr);
917
918 /* Otherwise mark it done and update residue_stat. */
919 edesc->processed_stat++;
920 edesc->residue_stat -= pset->len;
921 }
922 return edesc->residue_stat;
923}
924
c2dde5f8
MP
925/* Check request completion status */
926static enum dma_status edma_tx_status(struct dma_chan *chan,
927 dma_cookie_t cookie,
928 struct dma_tx_state *txstate)
929{
930 struct edma_chan *echan = to_edma_chan(chan);
931 struct virt_dma_desc *vdesc;
932 enum dma_status ret;
933 unsigned long flags;
934
935 ret = dma_cookie_status(chan, cookie, txstate);
9d386ec5 936 if (ret == DMA_COMPLETE || !txstate)
c2dde5f8
MP
937 return ret;
938
939 spin_lock_irqsave(&echan->vchan.lock, flags);
de135939 940 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
740b41f7 941 txstate->residue = edma_residue(echan->edesc);
de135939
TG
942 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
943 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
c2dde5f8
MP
944 spin_unlock_irqrestore(&echan->vchan.lock, flags);
945
946 return ret;
947}
948
949static void __init edma_chan_init(struct edma_cc *ecc,
950 struct dma_device *dma,
951 struct edma_chan *echans)
952{
953 int i, j;
954
955 for (i = 0; i < EDMA_CHANS; i++) {
956 struct edma_chan *echan = &echans[i];
957 echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
958 echan->ecc = ecc;
959 echan->vchan.desc_free = edma_desc_free;
960
961 vchan_init(&echan->vchan, dma);
962
963 INIT_LIST_HEAD(&echan->node);
964 for (j = 0; j < EDMA_MAX_SLOTS; j++)
965 echan->slot[j] = -1;
966 }
967}
968
2c88ee6b
PU
969#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
970 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
e4a899d9 971 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
2c88ee6b
PU
972 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
973
c2dde5f8
MP
974static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
975 struct device *dev)
976{
977 dma->device_prep_slave_sg = edma_prep_slave_sg;
50a9c707 978 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
8cc3e30b 979 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
c2dde5f8
MP
980 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
981 dma->device_free_chan_resources = edma_free_chan_resources;
982 dma->device_issue_pending = edma_issue_pending;
983 dma->device_tx_status = edma_tx_status;
aa7c09b6
MR
984 dma->device_config = edma_slave_config;
985 dma->device_pause = edma_dma_pause;
986 dma->device_resume = edma_dma_resume;
987 dma->device_terminate_all = edma_terminate_all;
9f59cd05
MR
988
989 dma->src_addr_widths = EDMA_DMA_BUSWIDTHS;
990 dma->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
991 dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
992 dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
993
c2dde5f8
MP
994 dma->dev = dev;
995
8cc3e30b
JF
996 /*
997 * code using dma memcpy must make sure alignment of
998 * length is at dma->copy_align boundary.
999 */
1000 dma->copy_align = DMA_SLAVE_BUSWIDTH_4_BYTES;
1001
c2dde5f8
MP
1002 INIT_LIST_HEAD(&dma->channels);
1003}
1004
463a1f8b 1005static int edma_probe(struct platform_device *pdev)
c2dde5f8
MP
1006{
1007 struct edma_cc *ecc;
1008 int ret;
1009
94cb0e79
RK
1010 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1011 if (ret)
1012 return ret;
1013
c2dde5f8
MP
1014 ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
1015 if (!ecc) {
1016 dev_err(&pdev->dev, "Can't allocate controller\n");
1017 return -ENOMEM;
1018 }
1019
1020 ecc->ctlr = pdev->id;
1021 ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
1022 if (ecc->dummy_slot < 0) {
1023 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
04d537d9 1024 return ecc->dummy_slot;
c2dde5f8
MP
1025 }
1026
1027 dma_cap_zero(ecc->dma_slave.cap_mask);
1028 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
232b223d 1029 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
8cc3e30b 1030 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
c2dde5f8
MP
1031
1032 edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
1033
1034 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
1035
1036 ret = dma_async_device_register(&ecc->dma_slave);
1037 if (ret)
1038 goto err_reg1;
1039
1040 platform_set_drvdata(pdev, ecc);
1041
1042 dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
1043
1044 return 0;
1045
1046err_reg1:
1047 edma_free_slot(ecc->dummy_slot);
1048 return ret;
1049}
1050
4bf27b8b 1051static int edma_remove(struct platform_device *pdev)
c2dde5f8
MP
1052{
1053 struct device *dev = &pdev->dev;
1054 struct edma_cc *ecc = dev_get_drvdata(dev);
1055
1056 dma_async_device_unregister(&ecc->dma_slave);
1057 edma_free_slot(ecc->dummy_slot);
1058
1059 return 0;
1060}
1061
1062static struct platform_driver edma_driver = {
1063 .probe = edma_probe,
a7d6e3ec 1064 .remove = edma_remove,
c2dde5f8
MP
1065 .driver = {
1066 .name = "edma-dma-engine",
c2dde5f8
MP
1067 },
1068};
1069
1070bool edma_filter_fn(struct dma_chan *chan, void *param)
1071{
1072 if (chan->device->dev->driver == &edma_driver.driver) {
1073 struct edma_chan *echan = to_edma_chan(chan);
1074 unsigned ch_req = *(unsigned *)param;
1075 return ch_req == echan->ch_num;
1076 }
1077 return false;
1078}
1079EXPORT_SYMBOL(edma_filter_fn);
1080
c2dde5f8
MP
1081static int edma_init(void)
1082{
5305e4d6 1083 return platform_driver_register(&edma_driver);
c2dde5f8
MP
1084}
1085subsys_initcall(edma_init);
1086
1087static void __exit edma_exit(void)
1088{
c2dde5f8
MP
1089 platform_driver_unregister(&edma_driver);
1090}
1091module_exit(edma_exit);
1092
d71505b6 1093MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
c2dde5f8
MP
1094MODULE_DESCRIPTION("TI EDMA DMA engine driver");
1095MODULE_LICENSE("GPL v2");