blob: 35db3f282bdb7d1c013fabb703a22f76595fb840 [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 if (edesc->processed <= MAX_NR_SG) {
186 dev_dbg(dev, "first transfer starting %d\n", echan->ch_num);
187 edma_start(echan->ch_num);
Sekhar Nori5fc68a62014-03-19 11:25:50 +0530188 } else {
189 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
190 echan->ch_num, edesc->processed);
191 edma_resume(echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500192 }
Joel Fernandesc5f47992013-08-29 18:05:43 -0500193
194 /*
195 * This happens due to setup times between intermediate transfers
196 * in long SG lists which have to be broken up into transfers of
197 * MAX_NR_SG
198 */
199 if (echan->missed) {
200 dev_dbg(dev, "missed event in execute detected\n");
201 edma_clean_channel(echan->ch_num);
202 edma_stop(echan->ch_num);
203 edma_start(echan->ch_num);
204 edma_trigger_channel(echan->ch_num);
205 echan->missed = 0;
206 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400207}
208
209static int edma_terminate_all(struct edma_chan *echan)
210{
211 unsigned long flags;
212 LIST_HEAD(head);
213
214 spin_lock_irqsave(&echan->vchan.lock, flags);
215
216 /*
217 * Stop DMA activity: we assume the callback will not be called
218 * after edma_dma() returns (even if it does, it will see
219 * echan->edesc is NULL and exit.)
220 */
221 if (echan->edesc) {
222 echan->edesc = NULL;
223 edma_stop(echan->ch_num);
224 }
225
226 vchan_get_all_descriptors(&echan->vchan, &head);
227 spin_unlock_irqrestore(&echan->vchan.lock, flags);
228 vchan_dma_desc_free_list(&echan->vchan, &head);
229
230 return 0;
231}
232
Matt Porterc2dde5f2012-08-22 21:09:34 -0400233static int edma_slave_config(struct edma_chan *echan,
Matt Porter661f7cb2013-01-10 13:41:04 -0500234 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400235{
Matt Porter661f7cb2013-01-10 13:41:04 -0500236 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
237 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400238 return -EINVAL;
239
Matt Porter661f7cb2013-01-10 13:41:04 -0500240 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400241
242 return 0;
243}
244
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300245static int edma_dma_pause(struct edma_chan *echan)
246{
247 /* Pause/Resume only allowed with cyclic mode */
248 if (!echan->edesc->cyclic)
249 return -EINVAL;
250
251 edma_pause(echan->ch_num);
252 return 0;
253}
254
255static int edma_dma_resume(struct edma_chan *echan)
256{
257 /* Pause/Resume only allowed with cyclic mode */
258 if (!echan->edesc->cyclic)
259 return -EINVAL;
260
261 edma_resume(echan->ch_num);
262 return 0;
263}
264
Matt Porterc2dde5f2012-08-22 21:09:34 -0400265static int edma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
266 unsigned long arg)
267{
268 int ret = 0;
269 struct dma_slave_config *config;
270 struct edma_chan *echan = to_edma_chan(chan);
271
272 switch (cmd) {
273 case DMA_TERMINATE_ALL:
274 edma_terminate_all(echan);
275 break;
276 case DMA_SLAVE_CONFIG:
277 config = (struct dma_slave_config *)arg;
278 ret = edma_slave_config(echan, config);
279 break;
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300280 case DMA_PAUSE:
281 ret = edma_dma_pause(echan);
282 break;
283
284 case DMA_RESUME:
285 ret = edma_dma_resume(echan);
286 break;
287
Matt Porterc2dde5f2012-08-22 21:09:34 -0400288 default:
289 ret = -ENOSYS;
290 }
291
292 return ret;
293}
294
Joel Fernandesfd009032013-09-23 18:05:13 -0500295/*
296 * A PaRAM set configuration abstraction used by other modes
297 * @chan: Channel who's PaRAM set we're configuring
298 * @pset: PaRAM set to initialize and setup.
299 * @src_addr: Source address of the DMA
300 * @dst_addr: Destination address of the DMA
301 * @burst: In units of dev_width, how much to send
302 * @dev_width: How much is the dev_width
303 * @dma_length: Total length of the DMA transfer
304 * @direction: Direction of the transfer
305 */
306static int edma_config_pset(struct dma_chan *chan, struct edmacc_param *pset,
307 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
308 enum dma_slave_buswidth dev_width, unsigned int dma_length,
309 enum dma_transfer_direction direction)
310{
311 struct edma_chan *echan = to_edma_chan(chan);
312 struct device *dev = chan->device->dev;
313 int acnt, bcnt, ccnt, cidx;
314 int src_bidx, dst_bidx, src_cidx, dst_cidx;
315 int absync;
316
317 acnt = dev_width;
Peter Ujfalusib2b617d2014-04-14 14:41:58 +0300318
319 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
320 if (!burst)
321 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -0500322 /*
323 * If the maxburst is equal to the fifo width, use
324 * A-synced transfers. This allows for large contiguous
325 * buffer transfers using only one PaRAM set.
326 */
327 if (burst == 1) {
328 /*
329 * For the A-sync case, bcnt and ccnt are the remainder
330 * and quotient respectively of the division of:
331 * (dma_length / acnt) by (SZ_64K -1). This is so
332 * that in case bcnt over flows, we have ccnt to use.
333 * Note: In A-sync tranfer only, bcntrld is used, but it
334 * only applies for sg_dma_len(sg) >= SZ_64K.
335 * In this case, the best way adopted is- bccnt for the
336 * first frame will be the remainder below. Then for
337 * every successive frame, bcnt will be SZ_64K-1. This
338 * is assured as bcntrld = 0xffff in end of function.
339 */
340 absync = false;
341 ccnt = dma_length / acnt / (SZ_64K - 1);
342 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
343 /*
344 * If bcnt is non-zero, we have a remainder and hence an
345 * extra frame to transfer, so increment ccnt.
346 */
347 if (bcnt)
348 ccnt++;
349 else
350 bcnt = SZ_64K - 1;
351 cidx = acnt;
352 } else {
353 /*
354 * If maxburst is greater than the fifo address_width,
355 * use AB-synced transfers where A count is the fifo
356 * address_width and B count is the maxburst. In this
357 * case, we are limited to transfers of C count frames
358 * of (address_width * maxburst) where C count is limited
359 * to SZ_64K-1. This places an upper bound on the length
360 * of an SG segment that can be handled.
361 */
362 absync = true;
363 bcnt = burst;
364 ccnt = dma_length / (acnt * bcnt);
365 if (ccnt > (SZ_64K - 1)) {
366 dev_err(dev, "Exceeded max SG segment size\n");
367 return -EINVAL;
368 }
369 cidx = acnt * bcnt;
370 }
371
372 if (direction == DMA_MEM_TO_DEV) {
373 src_bidx = acnt;
374 src_cidx = cidx;
375 dst_bidx = 0;
376 dst_cidx = 0;
377 } else if (direction == DMA_DEV_TO_MEM) {
378 src_bidx = 0;
379 src_cidx = 0;
380 dst_bidx = acnt;
381 dst_cidx = cidx;
382 } else {
383 dev_err(dev, "%s: direction not implemented yet\n", __func__);
384 return -EINVAL;
385 }
386
387 pset->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
388 /* Configure A or AB synchronized transfers */
389 if (absync)
390 pset->opt |= SYNCDIM;
391
392 pset->src = src_addr;
393 pset->dst = dst_addr;
394
395 pset->src_dst_bidx = (dst_bidx << 16) | src_bidx;
396 pset->src_dst_cidx = (dst_cidx << 16) | src_cidx;
397
398 pset->a_b_cnt = bcnt << 16 | acnt;
399 pset->ccnt = ccnt;
400 /*
401 * Only time when (bcntrld) auto reload is required is for
402 * A-sync case, and in this case, a requirement of reload value
403 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
404 * and then later will be populated by edma_execute.
405 */
406 pset->link_bcntrld = 0xffffffff;
407 return absync;
408}
409
Matt Porterc2dde5f2012-08-22 21:09:34 -0400410static struct dma_async_tx_descriptor *edma_prep_slave_sg(
411 struct dma_chan *chan, struct scatterlist *sgl,
412 unsigned int sg_len, enum dma_transfer_direction direction,
413 unsigned long tx_flags, void *context)
414{
415 struct edma_chan *echan = to_edma_chan(chan);
416 struct device *dev = chan->device->dev;
417 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -0500418 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -0500419 enum dma_slave_buswidth dev_width;
420 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400421 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -0500422 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400423
424 if (unlikely(!echan || !sgl || !sg_len))
425 return NULL;
426
Matt Porter661f7cb2013-01-10 13:41:04 -0500427 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500428 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500429 dev_width = echan->cfg.src_addr_width;
430 burst = echan->cfg.src_maxburst;
431 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500432 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500433 dev_width = echan->cfg.dst_addr_width;
434 burst = echan->cfg.dst_maxburst;
435 } else {
436 dev_err(dev, "%s: bad direction?\n", __func__);
437 return NULL;
438 }
439
440 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Matt Porterc2dde5f2012-08-22 21:09:34 -0400441 dev_err(dev, "Undefined slave buswidth\n");
442 return NULL;
443 }
444
Matt Porterc2dde5f2012-08-22 21:09:34 -0400445 edesc = kzalloc(sizeof(*edesc) + sg_len *
446 sizeof(edesc->pset[0]), GFP_ATOMIC);
447 if (!edesc) {
448 dev_dbg(dev, "Failed to allocate a descriptor\n");
449 return NULL;
450 }
451
452 edesc->pset_nr = sg_len;
453
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500454 /* Allocate a PaRAM slot, if needed */
455 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
456
457 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -0400458 if (echan->slot[i] < 0) {
459 echan->slot[i] =
460 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
461 EDMA_SLOT_ANY);
462 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +0300463 kfree(edesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400464 dev_err(dev, "Failed to allocate slot\n");
465 return NULL;
466 }
467 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500468 }
469
470 /* Configure PaRAM sets for each SG */
471 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500472 /* Get address for each SG */
473 if (direction == DMA_DEV_TO_MEM)
474 dst_addr = sg_dma_address(sg);
475 else
476 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400477
Joel Fernandesfd009032013-09-23 18:05:13 -0500478 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
479 dst_addr, burst, dev_width,
480 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +0530481 if (ret < 0) {
482 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -0500483 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400484 }
485
Joel Fernandesfd009032013-09-23 18:05:13 -0500486 edesc->absync = ret;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500487
488 /* If this is the last in a current SG set of transactions,
489 enable interrupts so that next set is processed */
490 if (!((i+1) % MAX_NR_SG))
491 edesc->pset[i].opt |= TCINTEN;
492
Matt Porterc2dde5f2012-08-22 21:09:34 -0400493 /* If this is the last set, enable completion interrupt flag */
494 if (i == sg_len - 1)
495 edesc->pset[i].opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400496 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400497
Matt Porterc2dde5f2012-08-22 21:09:34 -0400498 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
499}
Matt Porterc2dde5f2012-08-22 21:09:34 -0400500
Joel Fernandes50a9c702013-10-31 16:31:23 -0500501static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
502 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
503 size_t period_len, enum dma_transfer_direction direction,
504 unsigned long tx_flags, void *context)
505{
506 struct edma_chan *echan = to_edma_chan(chan);
507 struct device *dev = chan->device->dev;
508 struct edma_desc *edesc;
509 dma_addr_t src_addr, dst_addr;
510 enum dma_slave_buswidth dev_width;
511 u32 burst;
512 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400513
Joel Fernandes50a9c702013-10-31 16:31:23 -0500514 if (unlikely(!echan || !buf_len || !period_len))
515 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400516
Joel Fernandes50a9c702013-10-31 16:31:23 -0500517 if (direction == DMA_DEV_TO_MEM) {
518 src_addr = echan->cfg.src_addr;
519 dst_addr = buf_addr;
520 dev_width = echan->cfg.src_addr_width;
521 burst = echan->cfg.src_maxburst;
522 } else if (direction == DMA_MEM_TO_DEV) {
523 src_addr = buf_addr;
524 dst_addr = echan->cfg.dst_addr;
525 dev_width = echan->cfg.dst_addr_width;
526 burst = echan->cfg.dst_maxburst;
527 } else {
528 dev_err(dev, "%s: bad direction?\n", __func__);
529 return NULL;
530 }
531
532 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
533 dev_err(dev, "Undefined slave buswidth\n");
534 return NULL;
535 }
536
537 if (unlikely(buf_len % period_len)) {
538 dev_err(dev, "Period should be multiple of Buffer length\n");
539 return NULL;
540 }
541
542 nslots = (buf_len / period_len) + 1;
543
544 /*
545 * Cyclic DMA users such as audio cannot tolerate delays introduced
546 * by cases where the number of periods is more than the maximum
547 * number of SGs the EDMA driver can handle at a time. For DMA types
548 * such as Slave SGs, such delays are tolerable and synchronized,
549 * but the synchronization is difficult to achieve with Cyclic and
550 * cannot be guaranteed, so we error out early.
551 */
552 if (nslots > MAX_NR_SG)
553 return NULL;
554
555 edesc = kzalloc(sizeof(*edesc) + nslots *
556 sizeof(edesc->pset[0]), GFP_ATOMIC);
557 if (!edesc) {
558 dev_dbg(dev, "Failed to allocate a descriptor\n");
559 return NULL;
560 }
561
562 edesc->cyclic = 1;
563 edesc->pset_nr = nslots;
564
565 dev_dbg(dev, "%s: nslots=%d\n", __func__, nslots);
566 dev_dbg(dev, "%s: period_len=%d\n", __func__, period_len);
567 dev_dbg(dev, "%s: buf_len=%d\n", __func__, buf_len);
568
569 for (i = 0; i < nslots; i++) {
570 /* Allocate a PaRAM slot, if needed */
571 if (echan->slot[i] < 0) {
572 echan->slot[i] =
573 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
574 EDMA_SLOT_ANY);
575 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100576 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500577 dev_err(dev, "Failed to allocate slot\n");
578 return NULL;
579 }
580 }
581
582 if (i == nslots - 1) {
583 memcpy(&edesc->pset[i], &edesc->pset[0],
584 sizeof(edesc->pset[0]));
585 break;
586 }
587
588 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
589 dst_addr, burst, dev_width, period_len,
590 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100591 if (ret < 0) {
592 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500593 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100594 }
Joel Fernandes50a9c702013-10-31 16:31:23 -0500595
596 if (direction == DMA_DEV_TO_MEM)
597 dst_addr += period_len;
598 else
599 src_addr += period_len;
600
601 dev_dbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
602 dev_dbg(dev,
603 "\n pset[%d]:\n"
604 " chnum\t%d\n"
605 " slot\t%d\n"
606 " opt\t%08x\n"
607 " src\t%08x\n"
608 " dst\t%08x\n"
609 " abcnt\t%08x\n"
610 " ccnt\t%08x\n"
611 " bidx\t%08x\n"
612 " cidx\t%08x\n"
613 " lkrld\t%08x\n",
614 i, echan->ch_num, echan->slot[i],
615 edesc->pset[i].opt,
616 edesc->pset[i].src,
617 edesc->pset[i].dst,
618 edesc->pset[i].a_b_cnt,
619 edesc->pset[i].ccnt,
620 edesc->pset[i].src_dst_bidx,
621 edesc->pset[i].src_dst_cidx,
622 edesc->pset[i].link_bcntrld);
623
624 edesc->absync = ret;
625
626 /*
627 * Enable interrupts for every period because callback
628 * has to be called for every period.
629 */
630 edesc->pset[i].opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400631 }
632
633 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
634}
635
636static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
637{
638 struct edma_chan *echan = data;
639 struct device *dev = echan->vchan.chan.device->dev;
640 struct edma_desc *edesc;
641 unsigned long flags;
Joel Fernandesc5f47992013-08-29 18:05:43 -0500642 struct edmacc_param p;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400643
Joel Fernandes50a9c702013-10-31 16:31:23 -0500644 edesc = echan->edesc;
645
646 /* Pause the channel for non-cyclic */
647 if (!edesc || (edesc && !edesc->cyclic))
648 edma_pause(echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400649
650 switch (ch_status) {
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530651 case EDMA_DMA_COMPLETE:
Matt Porterc2dde5f2012-08-22 21:09:34 -0400652 spin_lock_irqsave(&echan->vchan.lock, flags);
653
Matt Porterc2dde5f2012-08-22 21:09:34 -0400654 if (edesc) {
Joel Fernandes50a9c702013-10-31 16:31:23 -0500655 if (edesc->cyclic) {
656 vchan_cyclic_callback(&edesc->vdesc);
657 } else if (edesc->processed == edesc->pset_nr) {
Joel Fernandes53407062013-09-03 10:02:46 -0500658 dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
659 edma_stop(echan->ch_num);
660 vchan_cookie_complete(&edesc->vdesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500661 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500662 } else {
663 dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500664 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500665 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400666 }
667
668 spin_unlock_irqrestore(&echan->vchan.lock, flags);
669
670 break;
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530671 case EDMA_DMA_CC_ERROR:
Joel Fernandesc5f47992013-08-29 18:05:43 -0500672 spin_lock_irqsave(&echan->vchan.lock, flags);
673
674 edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
675
676 /*
677 * Issue later based on missed flag which will be sure
678 * to happen as:
679 * (1) we finished transmitting an intermediate slot and
680 * edma_execute is coming up.
681 * (2) or we finished current transfer and issue will
682 * call edma_execute.
683 *
684 * Important note: issuing can be dangerous here and
685 * lead to some nasty recursion when we are in a NULL
686 * slot. So we avoid doing so and set the missed flag.
687 */
688 if (p.a_b_cnt == 0 && p.ccnt == 0) {
689 dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
690 echan->missed = 1;
691 } else {
692 /*
693 * The slot is already programmed but the event got
694 * missed, so its safe to issue it here.
695 */
696 dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
697 edma_clean_channel(echan->ch_num);
698 edma_stop(echan->ch_num);
699 edma_start(echan->ch_num);
700 edma_trigger_channel(echan->ch_num);
701 }
702
703 spin_unlock_irqrestore(&echan->vchan.lock, flags);
704
Matt Porterc2dde5f2012-08-22 21:09:34 -0400705 break;
706 default:
707 break;
708 }
709}
710
711/* Alloc channel resources */
712static int edma_alloc_chan_resources(struct dma_chan *chan)
713{
714 struct edma_chan *echan = to_edma_chan(chan);
715 struct device *dev = chan->device->dev;
716 int ret;
717 int a_ch_num;
718 LIST_HEAD(descs);
719
720 a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
721 chan, EVENTQ_DEFAULT);
722
723 if (a_ch_num < 0) {
724 ret = -ENODEV;
725 goto err_no_chan;
726 }
727
728 if (a_ch_num != echan->ch_num) {
729 dev_err(dev, "failed to allocate requested channel %u:%u\n",
730 EDMA_CTLR(echan->ch_num),
731 EDMA_CHAN_SLOT(echan->ch_num));
732 ret = -ENODEV;
733 goto err_wrong_chan;
734 }
735
736 echan->alloced = true;
737 echan->slot[0] = echan->ch_num;
738
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300739 dev_dbg(dev, "allocated channel for %u:%u\n",
740 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400741
742 return 0;
743
744err_wrong_chan:
745 edma_free_channel(a_ch_num);
746err_no_chan:
747 return ret;
748}
749
750/* Free channel resources */
751static void edma_free_chan_resources(struct dma_chan *chan)
752{
753 struct edma_chan *echan = to_edma_chan(chan);
754 struct device *dev = chan->device->dev;
755 int i;
756
757 /* Terminate transfers */
758 edma_stop(echan->ch_num);
759
760 vchan_free_chan_resources(&echan->vchan);
761
762 /* Free EDMA PaRAM slots */
763 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
764 if (echan->slot[i] >= 0) {
765 edma_free_slot(echan->slot[i]);
766 echan->slot[i] = -1;
767 }
768 }
769
770 /* Free EDMA channel */
771 if (echan->alloced) {
772 edma_free_channel(echan->ch_num);
773 echan->alloced = false;
774 }
775
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300776 dev_dbg(dev, "freeing channel for %u\n", echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400777}
778
779/* Send pending descriptor to hardware */
780static void edma_issue_pending(struct dma_chan *chan)
781{
782 struct edma_chan *echan = to_edma_chan(chan);
783 unsigned long flags;
784
785 spin_lock_irqsave(&echan->vchan.lock, flags);
786 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
787 edma_execute(echan);
788 spin_unlock_irqrestore(&echan->vchan.lock, flags);
789}
790
791static size_t edma_desc_size(struct edma_desc *edesc)
792{
793 int i;
794 size_t size;
795
796 if (edesc->absync)
797 for (size = i = 0; i < edesc->pset_nr; i++)
798 size += (edesc->pset[i].a_b_cnt & 0xffff) *
799 (edesc->pset[i].a_b_cnt >> 16) *
800 edesc->pset[i].ccnt;
801 else
802 size = (edesc->pset[0].a_b_cnt & 0xffff) *
803 (edesc->pset[0].a_b_cnt >> 16) +
804 (edesc->pset[0].a_b_cnt & 0xffff) *
805 (SZ_64K - 1) * edesc->pset[0].ccnt;
806
807 return size;
808}
809
810/* Check request completion status */
811static enum dma_status edma_tx_status(struct dma_chan *chan,
812 dma_cookie_t cookie,
813 struct dma_tx_state *txstate)
814{
815 struct edma_chan *echan = to_edma_chan(chan);
816 struct virt_dma_desc *vdesc;
817 enum dma_status ret;
818 unsigned long flags;
819
820 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +0530821 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400822 return ret;
823
824 spin_lock_irqsave(&echan->vchan.lock, flags);
825 vdesc = vchan_find_desc(&echan->vchan, cookie);
826 if (vdesc) {
827 txstate->residue = edma_desc_size(to_edma_desc(&vdesc->tx));
828 } else if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie) {
829 struct edma_desc *edesc = echan->edesc;
830 txstate->residue = edma_desc_size(edesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400831 }
832 spin_unlock_irqrestore(&echan->vchan.lock, flags);
833
834 return ret;
835}
836
837static void __init edma_chan_init(struct edma_cc *ecc,
838 struct dma_device *dma,
839 struct edma_chan *echans)
840{
841 int i, j;
842
843 for (i = 0; i < EDMA_CHANS; i++) {
844 struct edma_chan *echan = &echans[i];
845 echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
846 echan->ecc = ecc;
847 echan->vchan.desc_free = edma_desc_free;
848
849 vchan_init(&echan->vchan, dma);
850
851 INIT_LIST_HEAD(&echan->node);
852 for (j = 0; j < EDMA_MAX_SLOTS; j++)
853 echan->slot[j] = -1;
854 }
855}
856
857static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
858 struct device *dev)
859{
860 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500861 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400862 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
863 dma->device_free_chan_resources = edma_free_chan_resources;
864 dma->device_issue_pending = edma_issue_pending;
865 dma->device_tx_status = edma_tx_status;
866 dma->device_control = edma_control;
867 dma->dev = dev;
868
869 INIT_LIST_HEAD(&dma->channels);
870}
871
Bill Pemberton463a1f82012-11-19 13:22:55 -0500872static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400873{
874 struct edma_cc *ecc;
875 int ret;
876
Russell King94cb0e72013-06-27 13:45:16 +0100877 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
878 if (ret)
879 return ret;
880
Matt Porterc2dde5f2012-08-22 21:09:34 -0400881 ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
882 if (!ecc) {
883 dev_err(&pdev->dev, "Can't allocate controller\n");
884 return -ENOMEM;
885 }
886
887 ecc->ctlr = pdev->id;
888 ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
889 if (ecc->dummy_slot < 0) {
890 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
891 return -EIO;
892 }
893
894 dma_cap_zero(ecc->dma_slave.cap_mask);
895 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
896
897 edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
898
899 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
900
901 ret = dma_async_device_register(&ecc->dma_slave);
902 if (ret)
903 goto err_reg1;
904
905 platform_set_drvdata(pdev, ecc);
906
907 dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
908
909 return 0;
910
911err_reg1:
912 edma_free_slot(ecc->dummy_slot);
913 return ret;
914}
915
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -0800916static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400917{
918 struct device *dev = &pdev->dev;
919 struct edma_cc *ecc = dev_get_drvdata(dev);
920
921 dma_async_device_unregister(&ecc->dma_slave);
922 edma_free_slot(ecc->dummy_slot);
923
924 return 0;
925}
926
927static struct platform_driver edma_driver = {
928 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -0500929 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400930 .driver = {
931 .name = "edma-dma-engine",
932 .owner = THIS_MODULE,
933 },
934};
935
936bool edma_filter_fn(struct dma_chan *chan, void *param)
937{
938 if (chan->device->dev->driver == &edma_driver.driver) {
939 struct edma_chan *echan = to_edma_chan(chan);
940 unsigned ch_req = *(unsigned *)param;
941 return ch_req == echan->ch_num;
942 }
943 return false;
944}
945EXPORT_SYMBOL(edma_filter_fn);
946
947static struct platform_device *pdev0, *pdev1;
948
949static const struct platform_device_info edma_dev_info0 = {
950 .name = "edma-dma-engine",
951 .id = 0,
Russell King94cb0e72013-06-27 13:45:16 +0100952 .dma_mask = DMA_BIT_MASK(32),
Matt Porterc2dde5f2012-08-22 21:09:34 -0400953};
954
955static const struct platform_device_info edma_dev_info1 = {
956 .name = "edma-dma-engine",
957 .id = 1,
Russell King94cb0e72013-06-27 13:45:16 +0100958 .dma_mask = DMA_BIT_MASK(32),
Matt Porterc2dde5f2012-08-22 21:09:34 -0400959};
960
961static int edma_init(void)
962{
963 int ret = platform_driver_register(&edma_driver);
964
965 if (ret == 0) {
966 pdev0 = platform_device_register_full(&edma_dev_info0);
967 if (IS_ERR(pdev0)) {
968 platform_driver_unregister(&edma_driver);
969 ret = PTR_ERR(pdev0);
970 goto out;
971 }
972 }
973
974 if (EDMA_CTLRS == 2) {
975 pdev1 = platform_device_register_full(&edma_dev_info1);
976 if (IS_ERR(pdev1)) {
977 platform_driver_unregister(&edma_driver);
978 platform_device_unregister(pdev0);
979 ret = PTR_ERR(pdev1);
980 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400981 }
982
983out:
984 return ret;
985}
986subsys_initcall(edma_init);
987
988static void __exit edma_exit(void)
989{
990 platform_device_unregister(pdev0);
991 if (pdev1)
992 platform_device_unregister(pdev1);
993 platform_driver_unregister(&edma_driver);
994}
995module_exit(edma_exit);
996
Josh Boyerd71505b2013-09-04 10:32:50 -0400997MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -0400998MODULE_DESCRIPTION("TI EDMA DMA engine driver");
999MODULE_LICENSE("GPL v2");