blob: 79b046b3e2f0141ea80be23eb4e343d8a629e25e [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;
63 int absync;
64 int pset_nr;
Joel Fernandes53407062013-09-03 10:02:46 -050065 int processed;
Matt Porterc2dde5f2012-08-22 21:09:34 -040066 struct edmacc_param pset[0];
67};
68
69struct edma_cc;
70
71struct edma_chan {
72 struct virt_dma_chan vchan;
73 struct list_head node;
74 struct edma_desc *edesc;
75 struct edma_cc *ecc;
76 int ch_num;
77 bool alloced;
78 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -050079 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -050080 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -040081};
82
83struct edma_cc {
84 int ctlr;
85 struct dma_device dma_slave;
86 struct edma_chan slave_chans[EDMA_CHANS];
87 int num_slave_chans;
88 int dummy_slot;
89};
90
91static inline struct edma_cc *to_edma_cc(struct dma_device *d)
92{
93 return container_of(d, struct edma_cc, dma_slave);
94}
95
96static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
97{
98 return container_of(c, struct edma_chan, vchan.chan);
99}
100
101static inline struct edma_desc
102*to_edma_desc(struct dma_async_tx_descriptor *tx)
103{
104 return container_of(tx, struct edma_desc, vdesc.tx);
105}
106
107static void edma_desc_free(struct virt_dma_desc *vdesc)
108{
109 kfree(container_of(vdesc, struct edma_desc, vdesc));
110}
111
112/* Dispatch a queued descriptor to the controller (caller holds lock) */
113static void edma_execute(struct edma_chan *echan)
114{
Joel Fernandes53407062013-09-03 10:02:46 -0500115 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400116 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500117 struct device *dev = echan->vchan.chan.device->dev;
118 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400119
Joel Fernandes53407062013-09-03 10:02:46 -0500120 /* If either we processed all psets or we're still not started */
121 if (!echan->edesc ||
122 echan->edesc->pset_nr == echan->edesc->processed) {
123 /* Get next vdesc */
124 vdesc = vchan_next_desc(&echan->vchan);
125 if (!vdesc) {
126 echan->edesc = NULL;
127 return;
128 }
129 list_del(&vdesc->node);
130 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400131 }
132
Joel Fernandes53407062013-09-03 10:02:46 -0500133 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400134
Joel Fernandes53407062013-09-03 10:02:46 -0500135 /* Find out how many left */
136 left = edesc->pset_nr - edesc->processed;
137 nslots = min(MAX_NR_SG, left);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400138
139 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500140 for (i = 0; i < nslots; i++) {
141 j = i + edesc->processed;
142 edma_write_slot(echan->slot[i], &edesc->pset[j]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400143 dev_dbg(echan->vchan.chan.device->dev,
144 "\n pset[%d]:\n"
145 " chnum\t%d\n"
146 " slot\t%d\n"
147 " opt\t%08x\n"
148 " src\t%08x\n"
149 " dst\t%08x\n"
150 " abcnt\t%08x\n"
151 " ccnt\t%08x\n"
152 " bidx\t%08x\n"
153 " cidx\t%08x\n"
154 " lkrld\t%08x\n",
Joel Fernandes53407062013-09-03 10:02:46 -0500155 j, echan->ch_num, echan->slot[i],
156 edesc->pset[j].opt,
157 edesc->pset[j].src,
158 edesc->pset[j].dst,
159 edesc->pset[j].a_b_cnt,
160 edesc->pset[j].ccnt,
161 edesc->pset[j].src_dst_bidx,
162 edesc->pset[j].src_dst_cidx,
163 edesc->pset[j].link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400164 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500165 if (i != (nslots - 1))
Matt Porterc2dde5f2012-08-22 21:09:34 -0400166 edma_link(echan->slot[i], echan->slot[i+1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400167 }
168
Joel Fernandes53407062013-09-03 10:02:46 -0500169 edesc->processed += nslots;
170
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500171 /*
172 * If this is either the last set in a set of SG-list transactions
173 * then setup a link to the dummy slot, this results in all future
174 * events being absorbed and that's OK because we're done
175 */
176 if (edesc->processed == edesc->pset_nr)
177 edma_link(echan->slot[nslots-1], echan->ecc->dummy_slot);
178
Joel Fernandes53407062013-09-03 10:02:46 -0500179 edma_resume(echan->ch_num);
180
181 if (edesc->processed <= MAX_NR_SG) {
182 dev_dbg(dev, "first transfer starting %d\n", echan->ch_num);
183 edma_start(echan->ch_num);
184 }
Joel Fernandesc5f47992013-08-29 18:05:43 -0500185
186 /*
187 * This happens due to setup times between intermediate transfers
188 * in long SG lists which have to be broken up into transfers of
189 * MAX_NR_SG
190 */
191 if (echan->missed) {
192 dev_dbg(dev, "missed event in execute detected\n");
193 edma_clean_channel(echan->ch_num);
194 edma_stop(echan->ch_num);
195 edma_start(echan->ch_num);
196 edma_trigger_channel(echan->ch_num);
197 echan->missed = 0;
198 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400199}
200
201static int edma_terminate_all(struct edma_chan *echan)
202{
203 unsigned long flags;
204 LIST_HEAD(head);
205
206 spin_lock_irqsave(&echan->vchan.lock, flags);
207
208 /*
209 * Stop DMA activity: we assume the callback will not be called
210 * after edma_dma() returns (even if it does, it will see
211 * echan->edesc is NULL and exit.)
212 */
213 if (echan->edesc) {
214 echan->edesc = NULL;
215 edma_stop(echan->ch_num);
216 }
217
218 vchan_get_all_descriptors(&echan->vchan, &head);
219 spin_unlock_irqrestore(&echan->vchan.lock, flags);
220 vchan_dma_desc_free_list(&echan->vchan, &head);
221
222 return 0;
223}
224
Matt Porterc2dde5f2012-08-22 21:09:34 -0400225static int edma_slave_config(struct edma_chan *echan,
Matt Porter661f7cb2013-01-10 13:41:04 -0500226 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400227{
Matt Porter661f7cb2013-01-10 13:41:04 -0500228 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
229 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400230 return -EINVAL;
231
Matt Porter661f7cb2013-01-10 13:41:04 -0500232 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400233
234 return 0;
235}
236
237static int edma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
238 unsigned long arg)
239{
240 int ret = 0;
241 struct dma_slave_config *config;
242 struct edma_chan *echan = to_edma_chan(chan);
243
244 switch (cmd) {
245 case DMA_TERMINATE_ALL:
246 edma_terminate_all(echan);
247 break;
248 case DMA_SLAVE_CONFIG:
249 config = (struct dma_slave_config *)arg;
250 ret = edma_slave_config(echan, config);
251 break;
252 default:
253 ret = -ENOSYS;
254 }
255
256 return ret;
257}
258
Joel Fernandesfd009032013-09-23 18:05:13 -0500259/*
260 * A PaRAM set configuration abstraction used by other modes
261 * @chan: Channel who's PaRAM set we're configuring
262 * @pset: PaRAM set to initialize and setup.
263 * @src_addr: Source address of the DMA
264 * @dst_addr: Destination address of the DMA
265 * @burst: In units of dev_width, how much to send
266 * @dev_width: How much is the dev_width
267 * @dma_length: Total length of the DMA transfer
268 * @direction: Direction of the transfer
269 */
270static int edma_config_pset(struct dma_chan *chan, struct edmacc_param *pset,
271 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
272 enum dma_slave_buswidth dev_width, unsigned int dma_length,
273 enum dma_transfer_direction direction)
274{
275 struct edma_chan *echan = to_edma_chan(chan);
276 struct device *dev = chan->device->dev;
277 int acnt, bcnt, ccnt, cidx;
278 int src_bidx, dst_bidx, src_cidx, dst_cidx;
279 int absync;
280
281 acnt = dev_width;
282 /*
283 * If the maxburst is equal to the fifo width, use
284 * A-synced transfers. This allows for large contiguous
285 * buffer transfers using only one PaRAM set.
286 */
287 if (burst == 1) {
288 /*
289 * For the A-sync case, bcnt and ccnt are the remainder
290 * and quotient respectively of the division of:
291 * (dma_length / acnt) by (SZ_64K -1). This is so
292 * that in case bcnt over flows, we have ccnt to use.
293 * Note: In A-sync tranfer only, bcntrld is used, but it
294 * only applies for sg_dma_len(sg) >= SZ_64K.
295 * In this case, the best way adopted is- bccnt for the
296 * first frame will be the remainder below. Then for
297 * every successive frame, bcnt will be SZ_64K-1. This
298 * is assured as bcntrld = 0xffff in end of function.
299 */
300 absync = false;
301 ccnt = dma_length / acnt / (SZ_64K - 1);
302 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
303 /*
304 * If bcnt is non-zero, we have a remainder and hence an
305 * extra frame to transfer, so increment ccnt.
306 */
307 if (bcnt)
308 ccnt++;
309 else
310 bcnt = SZ_64K - 1;
311 cidx = acnt;
312 } else {
313 /*
314 * If maxburst is greater than the fifo address_width,
315 * use AB-synced transfers where A count is the fifo
316 * address_width and B count is the maxburst. In this
317 * case, we are limited to transfers of C count frames
318 * of (address_width * maxburst) where C count is limited
319 * to SZ_64K-1. This places an upper bound on the length
320 * of an SG segment that can be handled.
321 */
322 absync = true;
323 bcnt = burst;
324 ccnt = dma_length / (acnt * bcnt);
325 if (ccnt > (SZ_64K - 1)) {
326 dev_err(dev, "Exceeded max SG segment size\n");
327 return -EINVAL;
328 }
329 cidx = acnt * bcnt;
330 }
331
332 if (direction == DMA_MEM_TO_DEV) {
333 src_bidx = acnt;
334 src_cidx = cidx;
335 dst_bidx = 0;
336 dst_cidx = 0;
337 } else if (direction == DMA_DEV_TO_MEM) {
338 src_bidx = 0;
339 src_cidx = 0;
340 dst_bidx = acnt;
341 dst_cidx = cidx;
342 } else {
343 dev_err(dev, "%s: direction not implemented yet\n", __func__);
344 return -EINVAL;
345 }
346
347 pset->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
348 /* Configure A or AB synchronized transfers */
349 if (absync)
350 pset->opt |= SYNCDIM;
351
352 pset->src = src_addr;
353 pset->dst = dst_addr;
354
355 pset->src_dst_bidx = (dst_bidx << 16) | src_bidx;
356 pset->src_dst_cidx = (dst_cidx << 16) | src_cidx;
357
358 pset->a_b_cnt = bcnt << 16 | acnt;
359 pset->ccnt = ccnt;
360 /*
361 * Only time when (bcntrld) auto reload is required is for
362 * A-sync case, and in this case, a requirement of reload value
363 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
364 * and then later will be populated by edma_execute.
365 */
366 pset->link_bcntrld = 0xffffffff;
367 return absync;
368}
369
Matt Porterc2dde5f2012-08-22 21:09:34 -0400370static struct dma_async_tx_descriptor *edma_prep_slave_sg(
371 struct dma_chan *chan, struct scatterlist *sgl,
372 unsigned int sg_len, enum dma_transfer_direction direction,
373 unsigned long tx_flags, void *context)
374{
375 struct edma_chan *echan = to_edma_chan(chan);
376 struct device *dev = chan->device->dev;
377 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -0500378 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -0500379 enum dma_slave_buswidth dev_width;
380 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400381 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -0500382 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400383
384 if (unlikely(!echan || !sgl || !sg_len))
385 return NULL;
386
Matt Porter661f7cb2013-01-10 13:41:04 -0500387 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500388 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500389 dev_width = echan->cfg.src_addr_width;
390 burst = echan->cfg.src_maxburst;
391 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500392 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500393 dev_width = echan->cfg.dst_addr_width;
394 burst = echan->cfg.dst_maxburst;
395 } else {
396 dev_err(dev, "%s: bad direction?\n", __func__);
397 return NULL;
398 }
399
400 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Matt Porterc2dde5f2012-08-22 21:09:34 -0400401 dev_err(dev, "Undefined slave buswidth\n");
402 return NULL;
403 }
404
Matt Porterc2dde5f2012-08-22 21:09:34 -0400405 edesc = kzalloc(sizeof(*edesc) + sg_len *
406 sizeof(edesc->pset[0]), GFP_ATOMIC);
407 if (!edesc) {
408 dev_dbg(dev, "Failed to allocate a descriptor\n");
409 return NULL;
410 }
411
412 edesc->pset_nr = sg_len;
413
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500414 /* Allocate a PaRAM slot, if needed */
415 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
416
417 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -0400418 if (echan->slot[i] < 0) {
419 echan->slot[i] =
420 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
421 EDMA_SLOT_ANY);
422 if (echan->slot[i] < 0) {
423 dev_err(dev, "Failed to allocate slot\n");
424 return NULL;
425 }
426 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500427 }
428
429 /* Configure PaRAM sets for each SG */
430 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500431 /* Get address for each SG */
432 if (direction == DMA_DEV_TO_MEM)
433 dst_addr = sg_dma_address(sg);
434 else
435 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400436
Joel Fernandesfd009032013-09-23 18:05:13 -0500437 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
438 dst_addr, burst, dev_width,
439 sg_dma_len(sg), direction);
440 if (ret < 0)
441 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400442
Joel Fernandesfd009032013-09-23 18:05:13 -0500443 edesc->absync = ret;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500444
445 /* If this is the last in a current SG set of transactions,
446 enable interrupts so that next set is processed */
447 if (!((i+1) % MAX_NR_SG))
448 edesc->pset[i].opt |= TCINTEN;
449
Matt Porterc2dde5f2012-08-22 21:09:34 -0400450 /* If this is the last set, enable completion interrupt flag */
451 if (i == sg_len - 1)
452 edesc->pset[i].opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400453 }
454
455 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
456}
457
458static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
459{
460 struct edma_chan *echan = data;
461 struct device *dev = echan->vchan.chan.device->dev;
462 struct edma_desc *edesc;
463 unsigned long flags;
Joel Fernandesc5f47992013-08-29 18:05:43 -0500464 struct edmacc_param p;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400465
Joel Fernandes53407062013-09-03 10:02:46 -0500466 /* Pause the channel */
467 edma_pause(echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400468
469 switch (ch_status) {
470 case DMA_COMPLETE:
Matt Porterc2dde5f2012-08-22 21:09:34 -0400471 spin_lock_irqsave(&echan->vchan.lock, flags);
472
473 edesc = echan->edesc;
474 if (edesc) {
Joel Fernandes53407062013-09-03 10:02:46 -0500475 if (edesc->processed == edesc->pset_nr) {
476 dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
477 edma_stop(echan->ch_num);
478 vchan_cookie_complete(&edesc->vdesc);
479 } else {
480 dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
481 }
482
Matt Porterc2dde5f2012-08-22 21:09:34 -0400483 edma_execute(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400484 }
485
486 spin_unlock_irqrestore(&echan->vchan.lock, flags);
487
488 break;
489 case DMA_CC_ERROR:
Joel Fernandesc5f47992013-08-29 18:05:43 -0500490 spin_lock_irqsave(&echan->vchan.lock, flags);
491
492 edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
493
494 /*
495 * Issue later based on missed flag which will be sure
496 * to happen as:
497 * (1) we finished transmitting an intermediate slot and
498 * edma_execute is coming up.
499 * (2) or we finished current transfer and issue will
500 * call edma_execute.
501 *
502 * Important note: issuing can be dangerous here and
503 * lead to some nasty recursion when we are in a NULL
504 * slot. So we avoid doing so and set the missed flag.
505 */
506 if (p.a_b_cnt == 0 && p.ccnt == 0) {
507 dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
508 echan->missed = 1;
509 } else {
510 /*
511 * The slot is already programmed but the event got
512 * missed, so its safe to issue it here.
513 */
514 dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
515 edma_clean_channel(echan->ch_num);
516 edma_stop(echan->ch_num);
517 edma_start(echan->ch_num);
518 edma_trigger_channel(echan->ch_num);
519 }
520
521 spin_unlock_irqrestore(&echan->vchan.lock, flags);
522
Matt Porterc2dde5f2012-08-22 21:09:34 -0400523 break;
524 default:
525 break;
526 }
527}
528
529/* Alloc channel resources */
530static int edma_alloc_chan_resources(struct dma_chan *chan)
531{
532 struct edma_chan *echan = to_edma_chan(chan);
533 struct device *dev = chan->device->dev;
534 int ret;
535 int a_ch_num;
536 LIST_HEAD(descs);
537
538 a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
539 chan, EVENTQ_DEFAULT);
540
541 if (a_ch_num < 0) {
542 ret = -ENODEV;
543 goto err_no_chan;
544 }
545
546 if (a_ch_num != echan->ch_num) {
547 dev_err(dev, "failed to allocate requested channel %u:%u\n",
548 EDMA_CTLR(echan->ch_num),
549 EDMA_CHAN_SLOT(echan->ch_num));
550 ret = -ENODEV;
551 goto err_wrong_chan;
552 }
553
554 echan->alloced = true;
555 echan->slot[0] = echan->ch_num;
556
557 dev_info(dev, "allocated channel for %u:%u\n",
558 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
559
560 return 0;
561
562err_wrong_chan:
563 edma_free_channel(a_ch_num);
564err_no_chan:
565 return ret;
566}
567
568/* Free channel resources */
569static void edma_free_chan_resources(struct dma_chan *chan)
570{
571 struct edma_chan *echan = to_edma_chan(chan);
572 struct device *dev = chan->device->dev;
573 int i;
574
575 /* Terminate transfers */
576 edma_stop(echan->ch_num);
577
578 vchan_free_chan_resources(&echan->vchan);
579
580 /* Free EDMA PaRAM slots */
581 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
582 if (echan->slot[i] >= 0) {
583 edma_free_slot(echan->slot[i]);
584 echan->slot[i] = -1;
585 }
586 }
587
588 /* Free EDMA channel */
589 if (echan->alloced) {
590 edma_free_channel(echan->ch_num);
591 echan->alloced = false;
592 }
593
594 dev_info(dev, "freeing channel for %u\n", echan->ch_num);
595}
596
597/* Send pending descriptor to hardware */
598static void edma_issue_pending(struct dma_chan *chan)
599{
600 struct edma_chan *echan = to_edma_chan(chan);
601 unsigned long flags;
602
603 spin_lock_irqsave(&echan->vchan.lock, flags);
604 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
605 edma_execute(echan);
606 spin_unlock_irqrestore(&echan->vchan.lock, flags);
607}
608
609static size_t edma_desc_size(struct edma_desc *edesc)
610{
611 int i;
612 size_t size;
613
614 if (edesc->absync)
615 for (size = i = 0; i < edesc->pset_nr; i++)
616 size += (edesc->pset[i].a_b_cnt & 0xffff) *
617 (edesc->pset[i].a_b_cnt >> 16) *
618 edesc->pset[i].ccnt;
619 else
620 size = (edesc->pset[0].a_b_cnt & 0xffff) *
621 (edesc->pset[0].a_b_cnt >> 16) +
622 (edesc->pset[0].a_b_cnt & 0xffff) *
623 (SZ_64K - 1) * edesc->pset[0].ccnt;
624
625 return size;
626}
627
628/* Check request completion status */
629static enum dma_status edma_tx_status(struct dma_chan *chan,
630 dma_cookie_t cookie,
631 struct dma_tx_state *txstate)
632{
633 struct edma_chan *echan = to_edma_chan(chan);
634 struct virt_dma_desc *vdesc;
635 enum dma_status ret;
636 unsigned long flags;
637
638 ret = dma_cookie_status(chan, cookie, txstate);
639 if (ret == DMA_SUCCESS || !txstate)
640 return ret;
641
642 spin_lock_irqsave(&echan->vchan.lock, flags);
643 vdesc = vchan_find_desc(&echan->vchan, cookie);
644 if (vdesc) {
645 txstate->residue = edma_desc_size(to_edma_desc(&vdesc->tx));
646 } else if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie) {
647 struct edma_desc *edesc = echan->edesc;
648 txstate->residue = edma_desc_size(edesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400649 }
650 spin_unlock_irqrestore(&echan->vchan.lock, flags);
651
652 return ret;
653}
654
655static void __init edma_chan_init(struct edma_cc *ecc,
656 struct dma_device *dma,
657 struct edma_chan *echans)
658{
659 int i, j;
660
661 for (i = 0; i < EDMA_CHANS; i++) {
662 struct edma_chan *echan = &echans[i];
663 echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
664 echan->ecc = ecc;
665 echan->vchan.desc_free = edma_desc_free;
666
667 vchan_init(&echan->vchan, dma);
668
669 INIT_LIST_HEAD(&echan->node);
670 for (j = 0; j < EDMA_MAX_SLOTS; j++)
671 echan->slot[j] = -1;
672 }
673}
674
675static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
676 struct device *dev)
677{
678 dma->device_prep_slave_sg = edma_prep_slave_sg;
679 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
680 dma->device_free_chan_resources = edma_free_chan_resources;
681 dma->device_issue_pending = edma_issue_pending;
682 dma->device_tx_status = edma_tx_status;
683 dma->device_control = edma_control;
684 dma->dev = dev;
685
686 INIT_LIST_HEAD(&dma->channels);
687}
688
Bill Pemberton463a1f82012-11-19 13:22:55 -0500689static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400690{
691 struct edma_cc *ecc;
692 int ret;
693
694 ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
695 if (!ecc) {
696 dev_err(&pdev->dev, "Can't allocate controller\n");
697 return -ENOMEM;
698 }
699
700 ecc->ctlr = pdev->id;
701 ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
702 if (ecc->dummy_slot < 0) {
703 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
704 return -EIO;
705 }
706
707 dma_cap_zero(ecc->dma_slave.cap_mask);
708 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
709
710 edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
711
712 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
713
714 ret = dma_async_device_register(&ecc->dma_slave);
715 if (ret)
716 goto err_reg1;
717
718 platform_set_drvdata(pdev, ecc);
719
720 dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
721
722 return 0;
723
724err_reg1:
725 edma_free_slot(ecc->dummy_slot);
726 return ret;
727}
728
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -0800729static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400730{
731 struct device *dev = &pdev->dev;
732 struct edma_cc *ecc = dev_get_drvdata(dev);
733
734 dma_async_device_unregister(&ecc->dma_slave);
735 edma_free_slot(ecc->dummy_slot);
736
737 return 0;
738}
739
740static struct platform_driver edma_driver = {
741 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -0500742 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400743 .driver = {
744 .name = "edma-dma-engine",
745 .owner = THIS_MODULE,
746 },
747};
748
749bool edma_filter_fn(struct dma_chan *chan, void *param)
750{
751 if (chan->device->dev->driver == &edma_driver.driver) {
752 struct edma_chan *echan = to_edma_chan(chan);
753 unsigned ch_req = *(unsigned *)param;
754 return ch_req == echan->ch_num;
755 }
756 return false;
757}
758EXPORT_SYMBOL(edma_filter_fn);
759
760static struct platform_device *pdev0, *pdev1;
761
762static const struct platform_device_info edma_dev_info0 = {
763 .name = "edma-dma-engine",
764 .id = 0,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400765};
766
767static const struct platform_device_info edma_dev_info1 = {
768 .name = "edma-dma-engine",
769 .id = 1,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400770};
771
772static int edma_init(void)
773{
774 int ret = platform_driver_register(&edma_driver);
775
776 if (ret == 0) {
777 pdev0 = platform_device_register_full(&edma_dev_info0);
778 if (IS_ERR(pdev0)) {
779 platform_driver_unregister(&edma_driver);
780 ret = PTR_ERR(pdev0);
781 goto out;
782 }
Andy Shevchenko373459e2013-02-14 11:00:19 +0200783 pdev0->dev.dma_mask = &pdev0->dev.coherent_dma_mask;
784 pdev0->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400785 }
786
787 if (EDMA_CTLRS == 2) {
788 pdev1 = platform_device_register_full(&edma_dev_info1);
789 if (IS_ERR(pdev1)) {
790 platform_driver_unregister(&edma_driver);
791 platform_device_unregister(pdev0);
792 ret = PTR_ERR(pdev1);
793 }
Andy Shevchenko373459e2013-02-14 11:00:19 +0200794 pdev1->dev.dma_mask = &pdev1->dev.coherent_dma_mask;
795 pdev1->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400796 }
797
798out:
799 return ret;
800}
801subsys_initcall(edma_init);
802
803static void __exit edma_exit(void)
804{
805 platform_device_unregister(pdev0);
806 if (pdev1)
807 platform_device_unregister(pdev1);
808 platform_driver_unregister(&edma_driver);
809}
810module_exit(edma_exit);
811
Josh Boyerd71505b2013-09-04 10:32:50 -0400812MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -0400813MODULE_DESCRIPTION("TI EDMA DMA engine driver");
814MODULE_LICENSE("GPL v2");