blob: 592f3be8b435ef5b345de2e96f6736d4b1fa2965 [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
60struct edma_desc {
61 struct virt_dma_desc vdesc;
62 struct list_head node;
Joel Fernandes50a9c702013-10-31 16:31:23 -050063 int cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -040064 int absync;
65 int pset_nr;
Joel Fernandes53407062013-09-03 10:02:46 -050066 int processed;
Matt Porterc2dde5f2012-08-22 21:09:34 -040067 struct edmacc_param pset[0];
68};
69
70struct edma_cc;
71
72struct edma_chan {
73 struct virt_dma_chan vchan;
74 struct list_head node;
75 struct edma_desc *edesc;
76 struct edma_cc *ecc;
77 int ch_num;
78 bool alloced;
79 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -050080 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -050081 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -040082};
83
84struct edma_cc {
85 int ctlr;
86 struct dma_device dma_slave;
87 struct edma_chan slave_chans[EDMA_CHANS];
88 int num_slave_chans;
89 int dummy_slot;
90};
91
92static inline struct edma_cc *to_edma_cc(struct dma_device *d)
93{
94 return container_of(d, struct edma_cc, dma_slave);
95}
96
97static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
98{
99 return container_of(c, struct edma_chan, vchan.chan);
100}
101
102static inline struct edma_desc
103*to_edma_desc(struct dma_async_tx_descriptor *tx)
104{
105 return container_of(tx, struct edma_desc, vdesc.tx);
106}
107
108static void edma_desc_free(struct virt_dma_desc *vdesc)
109{
110 kfree(container_of(vdesc, struct edma_desc, vdesc));
111}
112
113/* Dispatch a queued descriptor to the controller (caller holds lock) */
114static void edma_execute(struct edma_chan *echan)
115{
Joel Fernandes53407062013-09-03 10:02:46 -0500116 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400117 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500118 struct device *dev = echan->vchan.chan.device->dev;
119 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400120
Joel Fernandes53407062013-09-03 10:02:46 -0500121 /* If either we processed all psets or we're still not started */
122 if (!echan->edesc ||
123 echan->edesc->pset_nr == echan->edesc->processed) {
124 /* Get next vdesc */
125 vdesc = vchan_next_desc(&echan->vchan);
126 if (!vdesc) {
127 echan->edesc = NULL;
128 return;
129 }
130 list_del(&vdesc->node);
131 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400132 }
133
Joel Fernandes53407062013-09-03 10:02:46 -0500134 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400135
Joel Fernandes53407062013-09-03 10:02:46 -0500136 /* Find out how many left */
137 left = edesc->pset_nr - edesc->processed;
138 nslots = min(MAX_NR_SG, left);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400139
140 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500141 for (i = 0; i < nslots; i++) {
142 j = i + edesc->processed;
143 edma_write_slot(echan->slot[i], &edesc->pset[j]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400144 dev_dbg(echan->vchan.chan.device->dev,
145 "\n pset[%d]:\n"
146 " chnum\t%d\n"
147 " slot\t%d\n"
148 " opt\t%08x\n"
149 " src\t%08x\n"
150 " dst\t%08x\n"
151 " abcnt\t%08x\n"
152 " ccnt\t%08x\n"
153 " bidx\t%08x\n"
154 " cidx\t%08x\n"
155 " lkrld\t%08x\n",
Joel Fernandes53407062013-09-03 10:02:46 -0500156 j, echan->ch_num, echan->slot[i],
157 edesc->pset[j].opt,
158 edesc->pset[j].src,
159 edesc->pset[j].dst,
160 edesc->pset[j].a_b_cnt,
161 edesc->pset[j].ccnt,
162 edesc->pset[j].src_dst_bidx,
163 edesc->pset[j].src_dst_cidx,
164 edesc->pset[j].link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400165 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500166 if (i != (nslots - 1))
Matt Porterc2dde5f2012-08-22 21:09:34 -0400167 edma_link(echan->slot[i], echan->slot[i+1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400168 }
169
Joel Fernandes53407062013-09-03 10:02:46 -0500170 edesc->processed += nslots;
171
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500172 /*
173 * If this is either the last set in a set of SG-list transactions
174 * then setup a link to the dummy slot, this results in all future
175 * events being absorbed and that's OK because we're done
176 */
Joel Fernandes50a9c702013-10-31 16:31:23 -0500177 if (edesc->processed == edesc->pset_nr) {
178 if (edesc->cyclic)
179 edma_link(echan->slot[nslots-1], echan->slot[1]);
180 else
181 edma_link(echan->slot[nslots-1],
182 echan->ecc->dummy_slot);
183 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500184
Joel Fernandes53407062013-09-03 10:02:46 -0500185 edma_resume(echan->ch_num);
186
187 if (edesc->processed <= MAX_NR_SG) {
188 dev_dbg(dev, "first transfer starting %d\n", echan->ch_num);
189 edma_start(echan->ch_num);
190 }
Joel Fernandesc5f47992013-08-29 18:05:43 -0500191
192 /*
193 * This happens due to setup times between intermediate transfers
194 * in long SG lists which have to be broken up into transfers of
195 * MAX_NR_SG
196 */
197 if (echan->missed) {
198 dev_dbg(dev, "missed event in execute detected\n");
199 edma_clean_channel(echan->ch_num);
200 edma_stop(echan->ch_num);
201 edma_start(echan->ch_num);
202 edma_trigger_channel(echan->ch_num);
203 echan->missed = 0;
204 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400205}
206
207static int edma_terminate_all(struct edma_chan *echan)
208{
209 unsigned long flags;
210 LIST_HEAD(head);
211
212 spin_lock_irqsave(&echan->vchan.lock, flags);
213
214 /*
215 * Stop DMA activity: we assume the callback will not be called
216 * after edma_dma() returns (even if it does, it will see
217 * echan->edesc is NULL and exit.)
218 */
219 if (echan->edesc) {
220 echan->edesc = NULL;
221 edma_stop(echan->ch_num);
222 }
223
224 vchan_get_all_descriptors(&echan->vchan, &head);
225 spin_unlock_irqrestore(&echan->vchan.lock, flags);
226 vchan_dma_desc_free_list(&echan->vchan, &head);
227
228 return 0;
229}
230
Matt Porterc2dde5f2012-08-22 21:09:34 -0400231static int edma_slave_config(struct edma_chan *echan,
Matt Porter661f7cb2013-01-10 13:41:04 -0500232 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400233{
Matt Porter661f7cb2013-01-10 13:41:04 -0500234 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
235 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400236 return -EINVAL;
237
Matt Porter661f7cb2013-01-10 13:41:04 -0500238 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400239
240 return 0;
241}
242
243static int edma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
244 unsigned long arg)
245{
246 int ret = 0;
247 struct dma_slave_config *config;
248 struct edma_chan *echan = to_edma_chan(chan);
249
250 switch (cmd) {
251 case DMA_TERMINATE_ALL:
252 edma_terminate_all(echan);
253 break;
254 case DMA_SLAVE_CONFIG:
255 config = (struct dma_slave_config *)arg;
256 ret = edma_slave_config(echan, config);
257 break;
258 default:
259 ret = -ENOSYS;
260 }
261
262 return ret;
263}
264
Joel Fernandesfd009032013-09-23 18:05:13 -0500265/*
266 * A PaRAM set configuration abstraction used by other modes
267 * @chan: Channel who's PaRAM set we're configuring
268 * @pset: PaRAM set to initialize and setup.
269 * @src_addr: Source address of the DMA
270 * @dst_addr: Destination address of the DMA
271 * @burst: In units of dev_width, how much to send
272 * @dev_width: How much is the dev_width
273 * @dma_length: Total length of the DMA transfer
274 * @direction: Direction of the transfer
275 */
276static int edma_config_pset(struct dma_chan *chan, struct edmacc_param *pset,
277 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
278 enum dma_slave_buswidth dev_width, unsigned int dma_length,
279 enum dma_transfer_direction direction)
280{
281 struct edma_chan *echan = to_edma_chan(chan);
282 struct device *dev = chan->device->dev;
283 int acnt, bcnt, ccnt, cidx;
284 int src_bidx, dst_bidx, src_cidx, dst_cidx;
285 int absync;
286
287 acnt = dev_width;
288 /*
289 * If the maxburst is equal to the fifo width, use
290 * A-synced transfers. This allows for large contiguous
291 * buffer transfers using only one PaRAM set.
292 */
293 if (burst == 1) {
294 /*
295 * For the A-sync case, bcnt and ccnt are the remainder
296 * and quotient respectively of the division of:
297 * (dma_length / acnt) by (SZ_64K -1). This is so
298 * that in case bcnt over flows, we have ccnt to use.
299 * Note: In A-sync tranfer only, bcntrld is used, but it
300 * only applies for sg_dma_len(sg) >= SZ_64K.
301 * In this case, the best way adopted is- bccnt for the
302 * first frame will be the remainder below. Then for
303 * every successive frame, bcnt will be SZ_64K-1. This
304 * is assured as bcntrld = 0xffff in end of function.
305 */
306 absync = false;
307 ccnt = dma_length / acnt / (SZ_64K - 1);
308 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
309 /*
310 * If bcnt is non-zero, we have a remainder and hence an
311 * extra frame to transfer, so increment ccnt.
312 */
313 if (bcnt)
314 ccnt++;
315 else
316 bcnt = SZ_64K - 1;
317 cidx = acnt;
318 } else {
319 /*
320 * If maxburst is greater than the fifo address_width,
321 * use AB-synced transfers where A count is the fifo
322 * address_width and B count is the maxburst. In this
323 * case, we are limited to transfers of C count frames
324 * of (address_width * maxburst) where C count is limited
325 * to SZ_64K-1. This places an upper bound on the length
326 * of an SG segment that can be handled.
327 */
328 absync = true;
329 bcnt = burst;
330 ccnt = dma_length / (acnt * bcnt);
331 if (ccnt > (SZ_64K - 1)) {
332 dev_err(dev, "Exceeded max SG segment size\n");
333 return -EINVAL;
334 }
335 cidx = acnt * bcnt;
336 }
337
338 if (direction == DMA_MEM_TO_DEV) {
339 src_bidx = acnt;
340 src_cidx = cidx;
341 dst_bidx = 0;
342 dst_cidx = 0;
343 } else if (direction == DMA_DEV_TO_MEM) {
344 src_bidx = 0;
345 src_cidx = 0;
346 dst_bidx = acnt;
347 dst_cidx = cidx;
348 } else {
349 dev_err(dev, "%s: direction not implemented yet\n", __func__);
350 return -EINVAL;
351 }
352
353 pset->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
354 /* Configure A or AB synchronized transfers */
355 if (absync)
356 pset->opt |= SYNCDIM;
357
358 pset->src = src_addr;
359 pset->dst = dst_addr;
360
361 pset->src_dst_bidx = (dst_bidx << 16) | src_bidx;
362 pset->src_dst_cidx = (dst_cidx << 16) | src_cidx;
363
364 pset->a_b_cnt = bcnt << 16 | acnt;
365 pset->ccnt = ccnt;
366 /*
367 * Only time when (bcntrld) auto reload is required is for
368 * A-sync case, and in this case, a requirement of reload value
369 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
370 * and then later will be populated by edma_execute.
371 */
372 pset->link_bcntrld = 0xffffffff;
373 return absync;
374}
375
Matt Porterc2dde5f2012-08-22 21:09:34 -0400376static struct dma_async_tx_descriptor *edma_prep_slave_sg(
377 struct dma_chan *chan, struct scatterlist *sgl,
378 unsigned int sg_len, enum dma_transfer_direction direction,
379 unsigned long tx_flags, void *context)
380{
381 struct edma_chan *echan = to_edma_chan(chan);
382 struct device *dev = chan->device->dev;
383 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -0500384 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -0500385 enum dma_slave_buswidth dev_width;
386 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400387 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -0500388 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400389
390 if (unlikely(!echan || !sgl || !sg_len))
391 return NULL;
392
Matt Porter661f7cb2013-01-10 13:41:04 -0500393 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500394 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500395 dev_width = echan->cfg.src_addr_width;
396 burst = echan->cfg.src_maxburst;
397 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500398 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500399 dev_width = echan->cfg.dst_addr_width;
400 burst = echan->cfg.dst_maxburst;
401 } else {
402 dev_err(dev, "%s: bad direction?\n", __func__);
403 return NULL;
404 }
405
406 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Matt Porterc2dde5f2012-08-22 21:09:34 -0400407 dev_err(dev, "Undefined slave buswidth\n");
408 return NULL;
409 }
410
Matt Porterc2dde5f2012-08-22 21:09:34 -0400411 edesc = kzalloc(sizeof(*edesc) + sg_len *
412 sizeof(edesc->pset[0]), GFP_ATOMIC);
413 if (!edesc) {
414 dev_dbg(dev, "Failed to allocate a descriptor\n");
415 return NULL;
416 }
417
418 edesc->pset_nr = sg_len;
419
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500420 /* Allocate a PaRAM slot, if needed */
421 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
422
423 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -0400424 if (echan->slot[i] < 0) {
425 echan->slot[i] =
426 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
427 EDMA_SLOT_ANY);
428 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +0300429 kfree(edesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400430 dev_err(dev, "Failed to allocate slot\n");
Geyslan G. Bem2f6d8fa2013-10-07 19:19:58 -0300431 kfree(edesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400432 return NULL;
433 }
434 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500435 }
436
437 /* Configure PaRAM sets for each SG */
438 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500439 /* Get address for each SG */
440 if (direction == DMA_DEV_TO_MEM)
441 dst_addr = sg_dma_address(sg);
442 else
443 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400444
Joel Fernandesfd009032013-09-23 18:05:13 -0500445 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
446 dst_addr, burst, dev_width,
447 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +0530448 if (ret < 0) {
449 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -0500450 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400451 }
452
Joel Fernandesfd009032013-09-23 18:05:13 -0500453 edesc->absync = ret;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500454
455 /* If this is the last in a current SG set of transactions,
456 enable interrupts so that next set is processed */
457 if (!((i+1) % MAX_NR_SG))
458 edesc->pset[i].opt |= TCINTEN;
459
Matt Porterc2dde5f2012-08-22 21:09:34 -0400460 /* If this is the last set, enable completion interrupt flag */
461 if (i == sg_len - 1)
462 edesc->pset[i].opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400463 }
464
465 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
466}
467
Joel Fernandes50a9c702013-10-31 16:31:23 -0500468static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
469 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
470 size_t period_len, enum dma_transfer_direction direction,
471 unsigned long tx_flags, void *context)
472{
473 struct edma_chan *echan = to_edma_chan(chan);
474 struct device *dev = chan->device->dev;
475 struct edma_desc *edesc;
476 dma_addr_t src_addr, dst_addr;
477 enum dma_slave_buswidth dev_width;
478 u32 burst;
479 int i, ret, nslots;
480
481 if (unlikely(!echan || !buf_len || !period_len))
482 return NULL;
483
484 if (direction == DMA_DEV_TO_MEM) {
485 src_addr = echan->cfg.src_addr;
486 dst_addr = buf_addr;
487 dev_width = echan->cfg.src_addr_width;
488 burst = echan->cfg.src_maxburst;
489 } else if (direction == DMA_MEM_TO_DEV) {
490 src_addr = buf_addr;
491 dst_addr = echan->cfg.dst_addr;
492 dev_width = echan->cfg.dst_addr_width;
493 burst = echan->cfg.dst_maxburst;
494 } else {
495 dev_err(dev, "%s: bad direction?\n", __func__);
496 return NULL;
497 }
498
499 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
500 dev_err(dev, "Undefined slave buswidth\n");
501 return NULL;
502 }
503
504 if (unlikely(buf_len % period_len)) {
505 dev_err(dev, "Period should be multiple of Buffer length\n");
506 return NULL;
507 }
508
509 nslots = (buf_len / period_len) + 1;
510
511 /*
512 * Cyclic DMA users such as audio cannot tolerate delays introduced
513 * by cases where the number of periods is more than the maximum
514 * number of SGs the EDMA driver can handle at a time. For DMA types
515 * such as Slave SGs, such delays are tolerable and synchronized,
516 * but the synchronization is difficult to achieve with Cyclic and
517 * cannot be guaranteed, so we error out early.
518 */
519 if (nslots > MAX_NR_SG)
520 return NULL;
521
522 edesc = kzalloc(sizeof(*edesc) + nslots *
523 sizeof(edesc->pset[0]), GFP_ATOMIC);
524 if (!edesc) {
525 dev_dbg(dev, "Failed to allocate a descriptor\n");
526 return NULL;
527 }
528
529 edesc->cyclic = 1;
530 edesc->pset_nr = nslots;
531
532 dev_dbg(dev, "%s: nslots=%d\n", __func__, nslots);
533 dev_dbg(dev, "%s: period_len=%d\n", __func__, period_len);
534 dev_dbg(dev, "%s: buf_len=%d\n", __func__, buf_len);
535
536 for (i = 0; i < nslots; i++) {
537 /* Allocate a PaRAM slot, if needed */
538 if (echan->slot[i] < 0) {
539 echan->slot[i] =
540 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
541 EDMA_SLOT_ANY);
542 if (echan->slot[i] < 0) {
543 dev_err(dev, "Failed to allocate slot\n");
544 return NULL;
545 }
546 }
547
548 if (i == nslots - 1) {
549 memcpy(&edesc->pset[i], &edesc->pset[0],
550 sizeof(edesc->pset[0]));
551 break;
552 }
553
554 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
555 dst_addr, burst, dev_width, period_len,
556 direction);
557 if (ret < 0)
558 return NULL;
559
560 if (direction == DMA_DEV_TO_MEM)
561 dst_addr += period_len;
562 else
563 src_addr += period_len;
564
565 dev_dbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
566 dev_dbg(dev,
567 "\n pset[%d]:\n"
568 " chnum\t%d\n"
569 " slot\t%d\n"
570 " opt\t%08x\n"
571 " src\t%08x\n"
572 " dst\t%08x\n"
573 " abcnt\t%08x\n"
574 " ccnt\t%08x\n"
575 " bidx\t%08x\n"
576 " cidx\t%08x\n"
577 " lkrld\t%08x\n",
578 i, echan->ch_num, echan->slot[i],
579 edesc->pset[i].opt,
580 edesc->pset[i].src,
581 edesc->pset[i].dst,
582 edesc->pset[i].a_b_cnt,
583 edesc->pset[i].ccnt,
584 edesc->pset[i].src_dst_bidx,
585 edesc->pset[i].src_dst_cidx,
586 edesc->pset[i].link_bcntrld);
587
588 edesc->absync = ret;
589
590 /*
591 * Enable interrupts for every period because callback
592 * has to be called for every period.
593 */
594 edesc->pset[i].opt |= TCINTEN;
595 }
596
597 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
598}
599
Matt Porterc2dde5f2012-08-22 21:09:34 -0400600static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
601{
602 struct edma_chan *echan = data;
603 struct device *dev = echan->vchan.chan.device->dev;
604 struct edma_desc *edesc;
605 unsigned long flags;
Joel Fernandesc5f47992013-08-29 18:05:43 -0500606 struct edmacc_param p;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400607
Joel Fernandes50a9c702013-10-31 16:31:23 -0500608 edesc = echan->edesc;
609
610 /* Pause the channel for non-cyclic */
611 if (!edesc || (edesc && !edesc->cyclic))
612 edma_pause(echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400613
614 switch (ch_status) {
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530615 case EDMA_DMA_COMPLETE:
Matt Porterc2dde5f2012-08-22 21:09:34 -0400616 spin_lock_irqsave(&echan->vchan.lock, flags);
617
Matt Porterc2dde5f2012-08-22 21:09:34 -0400618 if (edesc) {
Joel Fernandes50a9c702013-10-31 16:31:23 -0500619 if (edesc->cyclic) {
620 vchan_cyclic_callback(&edesc->vdesc);
621 } else if (edesc->processed == edesc->pset_nr) {
Joel Fernandes53407062013-09-03 10:02:46 -0500622 dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
623 edma_stop(echan->ch_num);
624 vchan_cookie_complete(&edesc->vdesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500625 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500626 } else {
627 dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500628 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500629 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400630 }
631
632 spin_unlock_irqrestore(&echan->vchan.lock, flags);
633
634 break;
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530635 case EDMA_DMA_CC_ERROR:
Joel Fernandesc5f47992013-08-29 18:05:43 -0500636 spin_lock_irqsave(&echan->vchan.lock, flags);
637
638 edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
639
640 /*
641 * Issue later based on missed flag which will be sure
642 * to happen as:
643 * (1) we finished transmitting an intermediate slot and
644 * edma_execute is coming up.
645 * (2) or we finished current transfer and issue will
646 * call edma_execute.
647 *
648 * Important note: issuing can be dangerous here and
649 * lead to some nasty recursion when we are in a NULL
650 * slot. So we avoid doing so and set the missed flag.
651 */
652 if (p.a_b_cnt == 0 && p.ccnt == 0) {
653 dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
654 echan->missed = 1;
655 } else {
656 /*
657 * The slot is already programmed but the event got
658 * missed, so its safe to issue it here.
659 */
660 dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
661 edma_clean_channel(echan->ch_num);
662 edma_stop(echan->ch_num);
663 edma_start(echan->ch_num);
664 edma_trigger_channel(echan->ch_num);
665 }
666
667 spin_unlock_irqrestore(&echan->vchan.lock, flags);
668
Matt Porterc2dde5f2012-08-22 21:09:34 -0400669 break;
670 default:
671 break;
672 }
673}
674
675/* Alloc channel resources */
676static int edma_alloc_chan_resources(struct dma_chan *chan)
677{
678 struct edma_chan *echan = to_edma_chan(chan);
679 struct device *dev = chan->device->dev;
680 int ret;
681 int a_ch_num;
682 LIST_HEAD(descs);
683
684 a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
685 chan, EVENTQ_DEFAULT);
686
687 if (a_ch_num < 0) {
688 ret = -ENODEV;
689 goto err_no_chan;
690 }
691
692 if (a_ch_num != echan->ch_num) {
693 dev_err(dev, "failed to allocate requested channel %u:%u\n",
694 EDMA_CTLR(echan->ch_num),
695 EDMA_CHAN_SLOT(echan->ch_num));
696 ret = -ENODEV;
697 goto err_wrong_chan;
698 }
699
700 echan->alloced = true;
701 echan->slot[0] = echan->ch_num;
702
703 dev_info(dev, "allocated channel for %u:%u\n",
704 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
705
706 return 0;
707
708err_wrong_chan:
709 edma_free_channel(a_ch_num);
710err_no_chan:
711 return ret;
712}
713
714/* Free channel resources */
715static void edma_free_chan_resources(struct dma_chan *chan)
716{
717 struct edma_chan *echan = to_edma_chan(chan);
718 struct device *dev = chan->device->dev;
719 int i;
720
721 /* Terminate transfers */
722 edma_stop(echan->ch_num);
723
724 vchan_free_chan_resources(&echan->vchan);
725
726 /* Free EDMA PaRAM slots */
727 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
728 if (echan->slot[i] >= 0) {
729 edma_free_slot(echan->slot[i]);
730 echan->slot[i] = -1;
731 }
732 }
733
734 /* Free EDMA channel */
735 if (echan->alloced) {
736 edma_free_channel(echan->ch_num);
737 echan->alloced = false;
738 }
739
740 dev_info(dev, "freeing channel for %u\n", echan->ch_num);
741}
742
743/* Send pending descriptor to hardware */
744static void edma_issue_pending(struct dma_chan *chan)
745{
746 struct edma_chan *echan = to_edma_chan(chan);
747 unsigned long flags;
748
749 spin_lock_irqsave(&echan->vchan.lock, flags);
750 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
751 edma_execute(echan);
752 spin_unlock_irqrestore(&echan->vchan.lock, flags);
753}
754
755static size_t edma_desc_size(struct edma_desc *edesc)
756{
757 int i;
758 size_t size;
759
760 if (edesc->absync)
761 for (size = i = 0; i < edesc->pset_nr; i++)
762 size += (edesc->pset[i].a_b_cnt & 0xffff) *
763 (edesc->pset[i].a_b_cnt >> 16) *
764 edesc->pset[i].ccnt;
765 else
766 size = (edesc->pset[0].a_b_cnt & 0xffff) *
767 (edesc->pset[0].a_b_cnt >> 16) +
768 (edesc->pset[0].a_b_cnt & 0xffff) *
769 (SZ_64K - 1) * edesc->pset[0].ccnt;
770
771 return size;
772}
773
774/* Check request completion status */
775static enum dma_status edma_tx_status(struct dma_chan *chan,
776 dma_cookie_t cookie,
777 struct dma_tx_state *txstate)
778{
779 struct edma_chan *echan = to_edma_chan(chan);
780 struct virt_dma_desc *vdesc;
781 enum dma_status ret;
782 unsigned long flags;
783
784 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +0530785 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400786 return ret;
787
788 spin_lock_irqsave(&echan->vchan.lock, flags);
789 vdesc = vchan_find_desc(&echan->vchan, cookie);
790 if (vdesc) {
791 txstate->residue = edma_desc_size(to_edma_desc(&vdesc->tx));
792 } else if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie) {
793 struct edma_desc *edesc = echan->edesc;
794 txstate->residue = edma_desc_size(edesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400795 }
796 spin_unlock_irqrestore(&echan->vchan.lock, flags);
797
798 return ret;
799}
800
801static void __init edma_chan_init(struct edma_cc *ecc,
802 struct dma_device *dma,
803 struct edma_chan *echans)
804{
805 int i, j;
806
807 for (i = 0; i < EDMA_CHANS; i++) {
808 struct edma_chan *echan = &echans[i];
809 echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
810 echan->ecc = ecc;
811 echan->vchan.desc_free = edma_desc_free;
812
813 vchan_init(&echan->vchan, dma);
814
815 INIT_LIST_HEAD(&echan->node);
816 for (j = 0; j < EDMA_MAX_SLOTS; j++)
817 echan->slot[j] = -1;
818 }
819}
820
821static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
822 struct device *dev)
823{
824 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500825 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400826 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
827 dma->device_free_chan_resources = edma_free_chan_resources;
828 dma->device_issue_pending = edma_issue_pending;
829 dma->device_tx_status = edma_tx_status;
830 dma->device_control = edma_control;
831 dma->dev = dev;
832
833 INIT_LIST_HEAD(&dma->channels);
834}
835
Bill Pemberton463a1f82012-11-19 13:22:55 -0500836static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400837{
838 struct edma_cc *ecc;
839 int ret;
840
841 ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
842 if (!ecc) {
843 dev_err(&pdev->dev, "Can't allocate controller\n");
844 return -ENOMEM;
845 }
846
847 ecc->ctlr = pdev->id;
848 ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
849 if (ecc->dummy_slot < 0) {
850 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
851 return -EIO;
852 }
853
854 dma_cap_zero(ecc->dma_slave.cap_mask);
855 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
856
857 edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
858
859 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
860
861 ret = dma_async_device_register(&ecc->dma_slave);
862 if (ret)
863 goto err_reg1;
864
865 platform_set_drvdata(pdev, ecc);
866
867 dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
868
869 return 0;
870
871err_reg1:
872 edma_free_slot(ecc->dummy_slot);
873 return ret;
874}
875
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -0800876static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400877{
878 struct device *dev = &pdev->dev;
879 struct edma_cc *ecc = dev_get_drvdata(dev);
880
881 dma_async_device_unregister(&ecc->dma_slave);
882 edma_free_slot(ecc->dummy_slot);
883
884 return 0;
885}
886
887static struct platform_driver edma_driver = {
888 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -0500889 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400890 .driver = {
891 .name = "edma-dma-engine",
892 .owner = THIS_MODULE,
893 },
894};
895
896bool edma_filter_fn(struct dma_chan *chan, void *param)
897{
898 if (chan->device->dev->driver == &edma_driver.driver) {
899 struct edma_chan *echan = to_edma_chan(chan);
900 unsigned ch_req = *(unsigned *)param;
901 return ch_req == echan->ch_num;
902 }
903 return false;
904}
905EXPORT_SYMBOL(edma_filter_fn);
906
907static struct platform_device *pdev0, *pdev1;
908
909static const struct platform_device_info edma_dev_info0 = {
910 .name = "edma-dma-engine",
911 .id = 0,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400912};
913
914static const struct platform_device_info edma_dev_info1 = {
915 .name = "edma-dma-engine",
916 .id = 1,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400917};
918
919static int edma_init(void)
920{
921 int ret = platform_driver_register(&edma_driver);
922
923 if (ret == 0) {
924 pdev0 = platform_device_register_full(&edma_dev_info0);
925 if (IS_ERR(pdev0)) {
926 platform_driver_unregister(&edma_driver);
927 ret = PTR_ERR(pdev0);
928 goto out;
929 }
Andy Shevchenko373459e2013-02-14 11:00:19 +0200930 pdev0->dev.dma_mask = &pdev0->dev.coherent_dma_mask;
931 pdev0->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400932 }
933
934 if (EDMA_CTLRS == 2) {
935 pdev1 = platform_device_register_full(&edma_dev_info1);
936 if (IS_ERR(pdev1)) {
937 platform_driver_unregister(&edma_driver);
938 platform_device_unregister(pdev0);
939 ret = PTR_ERR(pdev1);
940 }
Andy Shevchenko373459e2013-02-14 11:00:19 +0200941 pdev1->dev.dma_mask = &pdev1->dev.coherent_dma_mask;
942 pdev1->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400943 }
944
945out:
946 return ret;
947}
948subsys_initcall(edma_init);
949
950static void __exit edma_exit(void)
951{
952 platform_device_unregister(pdev0);
953 if (pdev1)
954 platform_device_unregister(pdev1);
955 platform_driver_unregister(&edma_driver);
956}
957module_exit(edma_exit);
958
Josh Boyerd71505b2013-09-04 10:32:50 -0400959MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -0400960MODULE_DESCRIPTION("TI EDMA DMA engine driver");
961MODULE_LICENSE("GPL v2");