blob: e95fa7dabc0c87a109673b2b3d9fda3ad3b31e40 [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>
Peter Ujfalusied646102014-07-31 13:12:38 +030026#include <linux/of.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040027
Matt Porter3ad7a422013-03-06 11:15:31 -050028#include <linux/platform_data/edma.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040029
30#include "dmaengine.h"
31#include "virt-dma.h"
32
33/*
34 * This will go away when the private EDMA API is folded
35 * into this driver and the platform device(s) are
36 * instantiated in the arch code. We can only get away
37 * with this simplification because DA8XX may not be built
38 * in the same kernel image with other DaVinci parts. This
39 * avoids having to sprinkle dmaengine driver platform devices
40 * and data throughout all the existing board files.
41 */
42#ifdef CONFIG_ARCH_DAVINCI_DA8XX
43#define EDMA_CTLRS 2
44#define EDMA_CHANS 32
45#else
46#define EDMA_CTLRS 1
47#define EDMA_CHANS 64
48#endif /* CONFIG_ARCH_DAVINCI_DA8XX */
49
Joel Fernandes2abd5f12013-09-23 18:05:15 -050050/*
51 * Max of 20 segments per channel to conserve PaRAM slots
52 * Also note that MAX_NR_SG should be atleast the no.of periods
53 * that are required for ASoC, otherwise DMA prep calls will
54 * fail. Today davinci-pcm is the only user of this driver and
55 * requires atleast 17 slots, so we setup the default to 20.
56 */
57#define MAX_NR_SG 20
Matt Porterc2dde5f2012-08-22 21:09:34 -040058#define EDMA_MAX_SLOTS MAX_NR_SG
59#define EDMA_DESCRIPTORS 16
60
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050061struct edma_pset {
Thomas Gleixnerc2da2342014-04-28 14:29:57 -050062 u32 len;
63 dma_addr_t addr;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050064 struct edmacc_param param;
65};
66
Matt Porterc2dde5f2012-08-22 21:09:34 -040067struct edma_desc {
68 struct virt_dma_desc vdesc;
69 struct list_head node;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -050070 enum dma_transfer_direction direction;
Joel Fernandes50a9c702013-10-31 16:31:23 -050071 int cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -040072 int absync;
73 int pset_nr;
Thomas Gleixner740b41f2014-04-28 14:34:11 -050074 struct edma_chan *echan;
Joel Fernandes04361d82014-04-28 15:19:31 -050075 int processed;
76
77 /*
78 * The following 4 elements are used for residue accounting.
79 *
80 * - processed_stat: the number of SG elements we have traversed
81 * so far to cover accounting. This is updated directly to processed
82 * during edma_callback and is always <= processed, because processed
83 * refers to the number of pending transfer (programmed to EDMA
84 * controller), where as processed_stat tracks number of transfers
85 * accounted for so far.
86 *
87 * - residue: The amount of bytes we have left to transfer for this desc
88 *
89 * - residue_stat: The residue in bytes of data we have covered
90 * so far for accounting. This is updated directly to residue
91 * during callbacks to keep it current.
92 *
93 * - sg_len: Tracks the length of the current intermediate transfer,
94 * this is required to update the residue during intermediate transfer
95 * completion callback.
96 */
97 int processed_stat;
98 u32 sg_len;
99 u32 residue;
100 u32 residue_stat;
101
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500102 struct edma_pset pset[0];
Matt Porterc2dde5f2012-08-22 21:09:34 -0400103};
104
105struct edma_cc;
106
107struct edma_chan {
108 struct virt_dma_chan vchan;
109 struct list_head node;
110 struct edma_desc *edesc;
111 struct edma_cc *ecc;
112 int ch_num;
113 bool alloced;
114 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -0500115 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -0500116 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400117};
118
119struct edma_cc {
120 int ctlr;
121 struct dma_device dma_slave;
122 struct edma_chan slave_chans[EDMA_CHANS];
123 int num_slave_chans;
124 int dummy_slot;
125};
126
127static inline struct edma_cc *to_edma_cc(struct dma_device *d)
128{
129 return container_of(d, struct edma_cc, dma_slave);
130}
131
132static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
133{
134 return container_of(c, struct edma_chan, vchan.chan);
135}
136
137static inline struct edma_desc
138*to_edma_desc(struct dma_async_tx_descriptor *tx)
139{
140 return container_of(tx, struct edma_desc, vdesc.tx);
141}
142
143static void edma_desc_free(struct virt_dma_desc *vdesc)
144{
145 kfree(container_of(vdesc, struct edma_desc, vdesc));
146}
147
148/* Dispatch a queued descriptor to the controller (caller holds lock) */
149static void edma_execute(struct edma_chan *echan)
150{
Joel Fernandes53407062013-09-03 10:02:46 -0500151 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400152 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500153 struct device *dev = echan->vchan.chan.device->dev;
154 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400155
Joel Fernandes53407062013-09-03 10:02:46 -0500156 /* If either we processed all psets or we're still not started */
157 if (!echan->edesc ||
158 echan->edesc->pset_nr == echan->edesc->processed) {
159 /* Get next vdesc */
160 vdesc = vchan_next_desc(&echan->vchan);
161 if (!vdesc) {
162 echan->edesc = NULL;
163 return;
164 }
165 list_del(&vdesc->node);
166 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400167 }
168
Joel Fernandes53407062013-09-03 10:02:46 -0500169 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400170
Joel Fernandes53407062013-09-03 10:02:46 -0500171 /* Find out how many left */
172 left = edesc->pset_nr - edesc->processed;
173 nslots = min(MAX_NR_SG, left);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500174 edesc->sg_len = 0;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400175
176 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500177 for (i = 0; i < nslots; i++) {
178 j = i + edesc->processed;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500179 edma_write_slot(echan->slot[i], &edesc->pset[j].param);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500180 edesc->sg_len += edesc->pset[j].len;
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300181 dev_vdbg(echan->vchan.chan.device->dev,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400182 "\n pset[%d]:\n"
183 " chnum\t%d\n"
184 " slot\t%d\n"
185 " opt\t%08x\n"
186 " src\t%08x\n"
187 " dst\t%08x\n"
188 " abcnt\t%08x\n"
189 " ccnt\t%08x\n"
190 " bidx\t%08x\n"
191 " cidx\t%08x\n"
192 " lkrld\t%08x\n",
Joel Fernandes53407062013-09-03 10:02:46 -0500193 j, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500194 edesc->pset[j].param.opt,
195 edesc->pset[j].param.src,
196 edesc->pset[j].param.dst,
197 edesc->pset[j].param.a_b_cnt,
198 edesc->pset[j].param.ccnt,
199 edesc->pset[j].param.src_dst_bidx,
200 edesc->pset[j].param.src_dst_cidx,
201 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400202 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500203 if (i != (nslots - 1))
Matt Porterc2dde5f2012-08-22 21:09:34 -0400204 edma_link(echan->slot[i], echan->slot[i+1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400205 }
206
Joel Fernandes53407062013-09-03 10:02:46 -0500207 edesc->processed += nslots;
208
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500209 /*
210 * If this is either the last set in a set of SG-list transactions
211 * then setup a link to the dummy slot, this results in all future
212 * events being absorbed and that's OK because we're done
213 */
Joel Fernandes50a9c702013-10-31 16:31:23 -0500214 if (edesc->processed == edesc->pset_nr) {
215 if (edesc->cyclic)
216 edma_link(echan->slot[nslots-1], echan->slot[1]);
217 else
218 edma_link(echan->slot[nslots-1],
219 echan->ecc->dummy_slot);
220 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500221
Joel Fernandes53407062013-09-03 10:02:46 -0500222 if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300223 dev_dbg(dev, "first transfer starting on channel %d\n",
224 echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500225 edma_start(echan->ch_num);
Sekhar Nori5fc68a62014-03-19 11:25:50 +0530226 } else {
227 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
228 echan->ch_num, edesc->processed);
229 edma_resume(echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500230 }
Joel Fernandesc5f47992013-08-29 18:05:43 -0500231
232 /*
233 * This happens due to setup times between intermediate transfers
234 * in long SG lists which have to be broken up into transfers of
235 * MAX_NR_SG
236 */
237 if (echan->missed) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300238 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500239 edma_clean_channel(echan->ch_num);
240 edma_stop(echan->ch_num);
241 edma_start(echan->ch_num);
242 edma_trigger_channel(echan->ch_num);
243 echan->missed = 0;
244 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400245}
246
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100247static int edma_terminate_all(struct dma_chan *chan)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400248{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100249 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400250 unsigned long flags;
251 LIST_HEAD(head);
252
253 spin_lock_irqsave(&echan->vchan.lock, flags);
254
255 /*
256 * Stop DMA activity: we assume the callback will not be called
257 * after edma_dma() returns (even if it does, it will see
258 * echan->edesc is NULL and exit.)
259 */
260 if (echan->edesc) {
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +0300261 int cyclic = echan->edesc->cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400262 echan->edesc = NULL;
263 edma_stop(echan->ch_num);
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +0300264 /* Move the cyclic channel back to default queue */
265 if (cyclic)
266 edma_assign_channel_eventq(echan->ch_num,
267 EVENTQ_DEFAULT);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400268 }
269
270 vchan_get_all_descriptors(&echan->vchan, &head);
271 spin_unlock_irqrestore(&echan->vchan.lock, flags);
272 vchan_dma_desc_free_list(&echan->vchan, &head);
273
274 return 0;
275}
276
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100277static int edma_slave_config(struct dma_chan *chan,
Matt Porter661f7cb2013-01-10 13:41:04 -0500278 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400279{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100280 struct edma_chan *echan = to_edma_chan(chan);
281
Matt Porter661f7cb2013-01-10 13:41:04 -0500282 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
283 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400284 return -EINVAL;
285
Matt Porter661f7cb2013-01-10 13:41:04 -0500286 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400287
288 return 0;
289}
290
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100291static int edma_dma_pause(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300292{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100293 struct edma_chan *echan = to_edma_chan(chan);
294
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300295 /* Pause/Resume only allowed with cyclic mode */
Sebastian Andrzej Siewior639559a2014-09-29 20:06:45 +0200296 if (!echan->edesc || !echan->edesc->cyclic)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300297 return -EINVAL;
298
299 edma_pause(echan->ch_num);
300 return 0;
301}
302
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100303static int edma_dma_resume(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300304{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100305 struct edma_chan *echan = to_edma_chan(chan);
306
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300307 /* Pause/Resume only allowed with cyclic mode */
308 if (!echan->edesc->cyclic)
309 return -EINVAL;
310
311 edma_resume(echan->ch_num);
312 return 0;
313}
314
Joel Fernandesfd009032013-09-23 18:05:13 -0500315/*
316 * A PaRAM set configuration abstraction used by other modes
317 * @chan: Channel who's PaRAM set we're configuring
318 * @pset: PaRAM set to initialize and setup.
319 * @src_addr: Source address of the DMA
320 * @dst_addr: Destination address of the DMA
321 * @burst: In units of dev_width, how much to send
322 * @dev_width: How much is the dev_width
323 * @dma_length: Total length of the DMA transfer
324 * @direction: Direction of the transfer
325 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500326static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
Joel Fernandesfd009032013-09-23 18:05:13 -0500327 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
328 enum dma_slave_buswidth dev_width, unsigned int dma_length,
329 enum dma_transfer_direction direction)
330{
331 struct edma_chan *echan = to_edma_chan(chan);
332 struct device *dev = chan->device->dev;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500333 struct edmacc_param *param = &epset->param;
Joel Fernandesfd009032013-09-23 18:05:13 -0500334 int acnt, bcnt, ccnt, cidx;
335 int src_bidx, dst_bidx, src_cidx, dst_cidx;
336 int absync;
337
338 acnt = dev_width;
Peter Ujfalusib2b617d2014-04-14 14:41:58 +0300339
340 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
341 if (!burst)
342 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -0500343 /*
344 * If the maxburst is equal to the fifo width, use
345 * A-synced transfers. This allows for large contiguous
346 * buffer transfers using only one PaRAM set.
347 */
348 if (burst == 1) {
349 /*
350 * For the A-sync case, bcnt and ccnt are the remainder
351 * and quotient respectively of the division of:
352 * (dma_length / acnt) by (SZ_64K -1). This is so
353 * that in case bcnt over flows, we have ccnt to use.
354 * Note: In A-sync tranfer only, bcntrld is used, but it
355 * only applies for sg_dma_len(sg) >= SZ_64K.
356 * In this case, the best way adopted is- bccnt for the
357 * first frame will be the remainder below. Then for
358 * every successive frame, bcnt will be SZ_64K-1. This
359 * is assured as bcntrld = 0xffff in end of function.
360 */
361 absync = false;
362 ccnt = dma_length / acnt / (SZ_64K - 1);
363 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
364 /*
365 * If bcnt is non-zero, we have a remainder and hence an
366 * extra frame to transfer, so increment ccnt.
367 */
368 if (bcnt)
369 ccnt++;
370 else
371 bcnt = SZ_64K - 1;
372 cidx = acnt;
373 } else {
374 /*
375 * If maxburst is greater than the fifo address_width,
376 * use AB-synced transfers where A count is the fifo
377 * address_width and B count is the maxburst. In this
378 * case, we are limited to transfers of C count frames
379 * of (address_width * maxburst) where C count is limited
380 * to SZ_64K-1. This places an upper bound on the length
381 * of an SG segment that can be handled.
382 */
383 absync = true;
384 bcnt = burst;
385 ccnt = dma_length / (acnt * bcnt);
386 if (ccnt > (SZ_64K - 1)) {
387 dev_err(dev, "Exceeded max SG segment size\n");
388 return -EINVAL;
389 }
390 cidx = acnt * bcnt;
391 }
392
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500393 epset->len = dma_length;
394
Joel Fernandesfd009032013-09-23 18:05:13 -0500395 if (direction == DMA_MEM_TO_DEV) {
396 src_bidx = acnt;
397 src_cidx = cidx;
398 dst_bidx = 0;
399 dst_cidx = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500400 epset->addr = src_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -0500401 } else if (direction == DMA_DEV_TO_MEM) {
402 src_bidx = 0;
403 src_cidx = 0;
404 dst_bidx = acnt;
405 dst_cidx = cidx;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500406 epset->addr = dst_addr;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500407 } else if (direction == DMA_MEM_TO_MEM) {
408 src_bidx = acnt;
409 src_cidx = cidx;
410 dst_bidx = acnt;
411 dst_cidx = cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500412 } else {
413 dev_err(dev, "%s: direction not implemented yet\n", __func__);
414 return -EINVAL;
415 }
416
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500417 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
Joel Fernandesfd009032013-09-23 18:05:13 -0500418 /* Configure A or AB synchronized transfers */
419 if (absync)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500420 param->opt |= SYNCDIM;
Joel Fernandesfd009032013-09-23 18:05:13 -0500421
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500422 param->src = src_addr;
423 param->dst = dst_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -0500424
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500425 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
426 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500427
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500428 param->a_b_cnt = bcnt << 16 | acnt;
429 param->ccnt = ccnt;
Joel Fernandesfd009032013-09-23 18:05:13 -0500430 /*
431 * Only time when (bcntrld) auto reload is required is for
432 * A-sync case, and in this case, a requirement of reload value
433 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
434 * and then later will be populated by edma_execute.
435 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500436 param->link_bcntrld = 0xffffffff;
Joel Fernandesfd009032013-09-23 18:05:13 -0500437 return absync;
438}
439
Matt Porterc2dde5f2012-08-22 21:09:34 -0400440static struct dma_async_tx_descriptor *edma_prep_slave_sg(
441 struct dma_chan *chan, struct scatterlist *sgl,
442 unsigned int sg_len, enum dma_transfer_direction direction,
443 unsigned long tx_flags, void *context)
444{
445 struct edma_chan *echan = to_edma_chan(chan);
446 struct device *dev = chan->device->dev;
447 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -0500448 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -0500449 enum dma_slave_buswidth dev_width;
450 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400451 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -0500452 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400453
454 if (unlikely(!echan || !sgl || !sg_len))
455 return NULL;
456
Matt Porter661f7cb2013-01-10 13:41:04 -0500457 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500458 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500459 dev_width = echan->cfg.src_addr_width;
460 burst = echan->cfg.src_maxburst;
461 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500462 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500463 dev_width = echan->cfg.dst_addr_width;
464 burst = echan->cfg.dst_maxburst;
465 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +0300466 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Matt Porter661f7cb2013-01-10 13:41:04 -0500467 return NULL;
468 }
469
470 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300471 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400472 return NULL;
473 }
474
Matt Porterc2dde5f2012-08-22 21:09:34 -0400475 edesc = kzalloc(sizeof(*edesc) + sg_len *
476 sizeof(edesc->pset[0]), GFP_ATOMIC);
477 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300478 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400479 return NULL;
480 }
481
482 edesc->pset_nr = sg_len;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500483 edesc->residue = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500484 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500485 edesc->echan = echan;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400486
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500487 /* Allocate a PaRAM slot, if needed */
488 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
489
490 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -0400491 if (echan->slot[i] < 0) {
492 echan->slot[i] =
493 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
494 EDMA_SLOT_ANY);
495 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +0300496 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +0300497 dev_err(dev, "%s: Failed to allocate slot\n",
498 __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400499 return NULL;
500 }
501 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500502 }
503
504 /* Configure PaRAM sets for each SG */
505 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500506 /* Get address for each SG */
507 if (direction == DMA_DEV_TO_MEM)
508 dst_addr = sg_dma_address(sg);
509 else
510 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400511
Joel Fernandesfd009032013-09-23 18:05:13 -0500512 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
513 dst_addr, burst, dev_width,
514 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +0530515 if (ret < 0) {
516 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -0500517 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400518 }
519
Joel Fernandesfd009032013-09-23 18:05:13 -0500520 edesc->absync = ret;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500521 edesc->residue += sg_dma_len(sg);
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500522
523 /* If this is the last in a current SG set of transactions,
524 enable interrupts so that next set is processed */
525 if (!((i+1) % MAX_NR_SG))
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500526 edesc->pset[i].param.opt |= TCINTEN;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500527
Matt Porterc2dde5f2012-08-22 21:09:34 -0400528 /* If this is the last set, enable completion interrupt flag */
529 if (i == sg_len - 1)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500530 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400531 }
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500532 edesc->residue_stat = edesc->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400533
Matt Porterc2dde5f2012-08-22 21:09:34 -0400534 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
535}
Matt Porterc2dde5f2012-08-22 21:09:34 -0400536
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500537struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
538 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
539 size_t len, unsigned long tx_flags)
540{
541 int ret;
542 struct edma_desc *edesc;
543 struct device *dev = chan->device->dev;
544 struct edma_chan *echan = to_edma_chan(chan);
545
546 if (unlikely(!echan || !len))
547 return NULL;
548
549 edesc = kzalloc(sizeof(*edesc) + sizeof(edesc->pset[0]), GFP_ATOMIC);
550 if (!edesc) {
551 dev_dbg(dev, "Failed to allocate a descriptor\n");
552 return NULL;
553 }
554
555 edesc->pset_nr = 1;
556
557 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
558 DMA_SLAVE_BUSWIDTH_4_BYTES, len, DMA_MEM_TO_MEM);
559 if (ret < 0)
560 return NULL;
561
562 edesc->absync = ret;
563
564 /*
565 * Enable intermediate transfer chaining to re-trigger channel
566 * on completion of every TR, and enable transfer-completion
567 * interrupt on completion of the whole transfer.
568 */
Joel Fernandesb0cce4c2014-04-28 15:30:32 -0500569 edesc->pset[0].param.opt |= ITCCHEN;
570 edesc->pset[0].param.opt |= TCINTEN;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500571
572 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
573}
574
Joel Fernandes50a9c702013-10-31 16:31:23 -0500575static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
576 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
577 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +0200578 unsigned long tx_flags)
Joel Fernandes50a9c702013-10-31 16:31:23 -0500579{
580 struct edma_chan *echan = to_edma_chan(chan);
581 struct device *dev = chan->device->dev;
582 struct edma_desc *edesc;
583 dma_addr_t src_addr, dst_addr;
584 enum dma_slave_buswidth dev_width;
585 u32 burst;
586 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400587
Joel Fernandes50a9c702013-10-31 16:31:23 -0500588 if (unlikely(!echan || !buf_len || !period_len))
589 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400590
Joel Fernandes50a9c702013-10-31 16:31:23 -0500591 if (direction == DMA_DEV_TO_MEM) {
592 src_addr = echan->cfg.src_addr;
593 dst_addr = buf_addr;
594 dev_width = echan->cfg.src_addr_width;
595 burst = echan->cfg.src_maxburst;
596 } else if (direction == DMA_MEM_TO_DEV) {
597 src_addr = buf_addr;
598 dst_addr = echan->cfg.dst_addr;
599 dev_width = echan->cfg.dst_addr_width;
600 burst = echan->cfg.dst_maxburst;
601 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +0300602 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500603 return NULL;
604 }
605
606 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300607 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500608 return NULL;
609 }
610
611 if (unlikely(buf_len % period_len)) {
612 dev_err(dev, "Period should be multiple of Buffer length\n");
613 return NULL;
614 }
615
616 nslots = (buf_len / period_len) + 1;
617
618 /*
619 * Cyclic DMA users such as audio cannot tolerate delays introduced
620 * by cases where the number of periods is more than the maximum
621 * number of SGs the EDMA driver can handle at a time. For DMA types
622 * such as Slave SGs, such delays are tolerable and synchronized,
623 * but the synchronization is difficult to achieve with Cyclic and
624 * cannot be guaranteed, so we error out early.
625 */
626 if (nslots > MAX_NR_SG)
627 return NULL;
628
629 edesc = kzalloc(sizeof(*edesc) + nslots *
630 sizeof(edesc->pset[0]), GFP_ATOMIC);
631 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300632 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500633 return NULL;
634 }
635
636 edesc->cyclic = 1;
637 edesc->pset_nr = nslots;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500638 edesc->residue = edesc->residue_stat = buf_len;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500639 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500640 edesc->echan = echan;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500641
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300642 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
643 __func__, echan->ch_num, nslots, period_len, buf_len);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500644
645 for (i = 0; i < nslots; i++) {
646 /* Allocate a PaRAM slot, if needed */
647 if (echan->slot[i] < 0) {
648 echan->slot[i] =
649 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
650 EDMA_SLOT_ANY);
651 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100652 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +0300653 dev_err(dev, "%s: Failed to allocate slot\n",
654 __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500655 return NULL;
656 }
657 }
658
659 if (i == nslots - 1) {
660 memcpy(&edesc->pset[i], &edesc->pset[0],
661 sizeof(edesc->pset[0]));
662 break;
663 }
664
665 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
666 dst_addr, burst, dev_width, period_len,
667 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100668 if (ret < 0) {
669 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500670 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100671 }
Joel Fernandes50a9c702013-10-31 16:31:23 -0500672
673 if (direction == DMA_DEV_TO_MEM)
674 dst_addr += period_len;
675 else
676 src_addr += period_len;
677
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300678 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
679 dev_vdbg(dev,
Joel Fernandes50a9c702013-10-31 16:31:23 -0500680 "\n pset[%d]:\n"
681 " chnum\t%d\n"
682 " slot\t%d\n"
683 " opt\t%08x\n"
684 " src\t%08x\n"
685 " dst\t%08x\n"
686 " abcnt\t%08x\n"
687 " ccnt\t%08x\n"
688 " bidx\t%08x\n"
689 " cidx\t%08x\n"
690 " lkrld\t%08x\n",
691 i, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500692 edesc->pset[i].param.opt,
693 edesc->pset[i].param.src,
694 edesc->pset[i].param.dst,
695 edesc->pset[i].param.a_b_cnt,
696 edesc->pset[i].param.ccnt,
697 edesc->pset[i].param.src_dst_bidx,
698 edesc->pset[i].param.src_dst_cidx,
699 edesc->pset[i].param.link_bcntrld);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500700
701 edesc->absync = ret;
702
703 /*
Peter Ujfalusia1f146f2014-07-16 15:29:21 +0300704 * Enable period interrupt only if it is requested
Joel Fernandes50a9c702013-10-31 16:31:23 -0500705 */
Peter Ujfalusia1f146f2014-07-16 15:29:21 +0300706 if (tx_flags & DMA_PREP_INTERRUPT)
707 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400708 }
709
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +0300710 /* Place the cyclic channel to highest priority queue */
711 edma_assign_channel_eventq(echan->ch_num, EVENTQ_0);
712
Matt Porterc2dde5f2012-08-22 21:09:34 -0400713 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
714}
715
716static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
717{
718 struct edma_chan *echan = data;
719 struct device *dev = echan->vchan.chan.device->dev;
720 struct edma_desc *edesc;
Joel Fernandesc5f47992013-08-29 18:05:43 -0500721 struct edmacc_param p;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400722
Joel Fernandes50a9c702013-10-31 16:31:23 -0500723 edesc = echan->edesc;
724
725 /* Pause the channel for non-cyclic */
726 if (!edesc || (edesc && !edesc->cyclic))
727 edma_pause(echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400728
729 switch (ch_status) {
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530730 case EDMA_DMA_COMPLETE:
Joel Fernandes406efb12014-04-17 00:58:33 -0500731 spin_lock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400732
Matt Porterc2dde5f2012-08-22 21:09:34 -0400733 if (edesc) {
Joel Fernandes50a9c702013-10-31 16:31:23 -0500734 if (edesc->cyclic) {
735 vchan_cyclic_callback(&edesc->vdesc);
736 } else if (edesc->processed == edesc->pset_nr) {
Joel Fernandes53407062013-09-03 10:02:46 -0500737 dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500738 edesc->residue = 0;
Joel Fernandes53407062013-09-03 10:02:46 -0500739 edma_stop(echan->ch_num);
740 vchan_cookie_complete(&edesc->vdesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500741 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500742 } else {
743 dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500744
745 /* Update statistics for tx_status */
746 edesc->residue -= edesc->sg_len;
747 edesc->residue_stat = edesc->residue;
748 edesc->processed_stat = edesc->processed;
749
Joel Fernandes50a9c702013-10-31 16:31:23 -0500750 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500751 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400752 }
753
Joel Fernandes406efb12014-04-17 00:58:33 -0500754 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400755
756 break;
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530757 case EDMA_DMA_CC_ERROR:
Joel Fernandes406efb12014-04-17 00:58:33 -0500758 spin_lock(&echan->vchan.lock);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500759
760 edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
761
762 /*
763 * Issue later based on missed flag which will be sure
764 * to happen as:
765 * (1) we finished transmitting an intermediate slot and
766 * edma_execute is coming up.
767 * (2) or we finished current transfer and issue will
768 * call edma_execute.
769 *
770 * Important note: issuing can be dangerous here and
771 * lead to some nasty recursion when we are in a NULL
772 * slot. So we avoid doing so and set the missed flag.
773 */
774 if (p.a_b_cnt == 0 && p.ccnt == 0) {
775 dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
776 echan->missed = 1;
777 } else {
778 /*
779 * The slot is already programmed but the event got
780 * missed, so its safe to issue it here.
781 */
782 dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
783 edma_clean_channel(echan->ch_num);
784 edma_stop(echan->ch_num);
785 edma_start(echan->ch_num);
786 edma_trigger_channel(echan->ch_num);
787 }
788
Joel Fernandes406efb12014-04-17 00:58:33 -0500789 spin_unlock(&echan->vchan.lock);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500790
Matt Porterc2dde5f2012-08-22 21:09:34 -0400791 break;
792 default:
793 break;
794 }
795}
796
797/* Alloc channel resources */
798static int edma_alloc_chan_resources(struct dma_chan *chan)
799{
800 struct edma_chan *echan = to_edma_chan(chan);
801 struct device *dev = chan->device->dev;
802 int ret;
803 int a_ch_num;
804 LIST_HEAD(descs);
805
806 a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
807 chan, EVENTQ_DEFAULT);
808
809 if (a_ch_num < 0) {
810 ret = -ENODEV;
811 goto err_no_chan;
812 }
813
814 if (a_ch_num != echan->ch_num) {
815 dev_err(dev, "failed to allocate requested channel %u:%u\n",
816 EDMA_CTLR(echan->ch_num),
817 EDMA_CHAN_SLOT(echan->ch_num));
818 ret = -ENODEV;
819 goto err_wrong_chan;
820 }
821
822 echan->alloced = true;
823 echan->slot[0] = echan->ch_num;
824
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300825 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300826 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400827
828 return 0;
829
830err_wrong_chan:
831 edma_free_channel(a_ch_num);
832err_no_chan:
833 return ret;
834}
835
836/* Free channel resources */
837static void edma_free_chan_resources(struct dma_chan *chan)
838{
839 struct edma_chan *echan = to_edma_chan(chan);
840 struct device *dev = chan->device->dev;
841 int i;
842
843 /* Terminate transfers */
844 edma_stop(echan->ch_num);
845
846 vchan_free_chan_resources(&echan->vchan);
847
848 /* Free EDMA PaRAM slots */
849 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
850 if (echan->slot[i] >= 0) {
851 edma_free_slot(echan->slot[i]);
852 echan->slot[i] = -1;
853 }
854 }
855
856 /* Free EDMA channel */
857 if (echan->alloced) {
858 edma_free_channel(echan->ch_num);
859 echan->alloced = false;
860 }
861
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300862 dev_dbg(dev, "freeing channel for %u\n", echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400863}
864
865/* Send pending descriptor to hardware */
866static void edma_issue_pending(struct dma_chan *chan)
867{
868 struct edma_chan *echan = to_edma_chan(chan);
869 unsigned long flags;
870
871 spin_lock_irqsave(&echan->vchan.lock, flags);
872 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
873 edma_execute(echan);
874 spin_unlock_irqrestore(&echan->vchan.lock, flags);
875}
876
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500877static u32 edma_residue(struct edma_desc *edesc)
878{
879 bool dst = edesc->direction == DMA_DEV_TO_MEM;
880 struct edma_pset *pset = edesc->pset;
881 dma_addr_t done, pos;
882 int i;
883
884 /*
885 * We always read the dst/src position from the first RamPar
886 * pset. That's the one which is active now.
887 */
888 pos = edma_get_position(edesc->echan->slot[0], dst);
889
890 /*
891 * Cyclic is simple. Just subtract pset[0].addr from pos.
892 *
893 * We never update edesc->residue in the cyclic case, so we
894 * can tell the remaining room to the end of the circular
895 * buffer.
896 */
897 if (edesc->cyclic) {
898 done = pos - pset->addr;
899 edesc->residue_stat = edesc->residue - done;
900 return edesc->residue_stat;
901 }
902
903 /*
904 * For SG operation we catch up with the last processed
905 * status.
906 */
907 pset += edesc->processed_stat;
908
909 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
910 /*
911 * If we are inside this pset address range, we know
912 * this is the active one. Get the current delta and
913 * stop walking the psets.
914 */
915 if (pos >= pset->addr && pos < pset->addr + pset->len)
916 return edesc->residue_stat - (pos - pset->addr);
917
918 /* Otherwise mark it done and update residue_stat. */
919 edesc->processed_stat++;
920 edesc->residue_stat -= pset->len;
921 }
922 return edesc->residue_stat;
923}
924
Matt Porterc2dde5f2012-08-22 21:09:34 -0400925/* Check request completion status */
926static enum dma_status edma_tx_status(struct dma_chan *chan,
927 dma_cookie_t cookie,
928 struct dma_tx_state *txstate)
929{
930 struct edma_chan *echan = to_edma_chan(chan);
931 struct virt_dma_desc *vdesc;
932 enum dma_status ret;
933 unsigned long flags;
934
935 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +0530936 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400937 return ret;
938
939 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -0500940 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500941 txstate->residue = edma_residue(echan->edesc);
Thomas Gleixnerde135932014-04-28 14:19:51 -0500942 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
943 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400944 spin_unlock_irqrestore(&echan->vchan.lock, flags);
945
946 return ret;
947}
948
949static void __init edma_chan_init(struct edma_cc *ecc,
950 struct dma_device *dma,
951 struct edma_chan *echans)
952{
953 int i, j;
954
955 for (i = 0; i < EDMA_CHANS; i++) {
956 struct edma_chan *echan = &echans[i];
957 echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
958 echan->ecc = ecc;
959 echan->vchan.desc_free = edma_desc_free;
960
961 vchan_init(&echan->vchan, dma);
962
963 INIT_LIST_HEAD(&echan->node);
964 for (j = 0; j < EDMA_MAX_SLOTS; j++)
965 echan->slot[j] = -1;
966 }
967}
968
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +0300969#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
970 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
Peter Ujfalusie4a899d2014-07-03 07:51:56 +0300971 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +0300972 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
973
Matt Porterc2dde5f2012-08-22 21:09:34 -0400974static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
975 struct device *dev)
976{
977 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500978 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500979 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400980 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
981 dma->device_free_chan_resources = edma_free_chan_resources;
982 dma->device_issue_pending = edma_issue_pending;
983 dma->device_tx_status = edma_tx_status;
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100984 dma->device_config = edma_slave_config;
985 dma->device_pause = edma_dma_pause;
986 dma->device_resume = edma_dma_resume;
987 dma->device_terminate_all = edma_terminate_all;
Maxime Ripard9f59cd02014-11-17 14:42:47 +0100988
989 dma->src_addr_widths = EDMA_DMA_BUSWIDTHS;
990 dma->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
991 dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
992 dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
993
Matt Porterc2dde5f2012-08-22 21:09:34 -0400994 dma->dev = dev;
995
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500996 /*
997 * code using dma memcpy must make sure alignment of
998 * length is at dma->copy_align boundary.
999 */
1000 dma->copy_align = DMA_SLAVE_BUSWIDTH_4_BYTES;
1001
Matt Porterc2dde5f2012-08-22 21:09:34 -04001002 INIT_LIST_HEAD(&dma->channels);
1003}
1004
Bill Pemberton463a1f82012-11-19 13:22:55 -05001005static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001006{
1007 struct edma_cc *ecc;
1008 int ret;
1009
Russell King94cb0e72013-06-27 13:45:16 +01001010 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1011 if (ret)
1012 return ret;
1013
Matt Porterc2dde5f2012-08-22 21:09:34 -04001014 ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
1015 if (!ecc) {
1016 dev_err(&pdev->dev, "Can't allocate controller\n");
1017 return -ENOMEM;
1018 }
1019
1020 ecc->ctlr = pdev->id;
1021 ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
1022 if (ecc->dummy_slot < 0) {
1023 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
Peter Ujfalusi04d537d2014-07-31 13:12:37 +03001024 return ecc->dummy_slot;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001025 }
1026
1027 dma_cap_zero(ecc->dma_slave.cap_mask);
1028 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
Peter Ujfalusi232b223d2014-04-14 14:42:00 +03001029 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001030 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001031
1032 edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
1033
1034 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
1035
1036 ret = dma_async_device_register(&ecc->dma_slave);
1037 if (ret)
1038 goto err_reg1;
1039
1040 platform_set_drvdata(pdev, ecc);
1041
1042 dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
1043
1044 return 0;
1045
1046err_reg1:
1047 edma_free_slot(ecc->dummy_slot);
1048 return ret;
1049}
1050
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001051static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001052{
1053 struct device *dev = &pdev->dev;
1054 struct edma_cc *ecc = dev_get_drvdata(dev);
1055
1056 dma_async_device_unregister(&ecc->dma_slave);
1057 edma_free_slot(ecc->dummy_slot);
1058
1059 return 0;
1060}
1061
1062static struct platform_driver edma_driver = {
1063 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05001064 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -04001065 .driver = {
1066 .name = "edma-dma-engine",
Matt Porterc2dde5f2012-08-22 21:09:34 -04001067 },
1068};
1069
1070bool edma_filter_fn(struct dma_chan *chan, void *param)
1071{
1072 if (chan->device->dev->driver == &edma_driver.driver) {
1073 struct edma_chan *echan = to_edma_chan(chan);
1074 unsigned ch_req = *(unsigned *)param;
1075 return ch_req == echan->ch_num;
1076 }
1077 return false;
1078}
1079EXPORT_SYMBOL(edma_filter_fn);
1080
Matt Porterc2dde5f2012-08-22 21:09:34 -04001081static int edma_init(void)
1082{
Arnd Bergmann5305e4d2014-10-24 18:14:01 +02001083 return platform_driver_register(&edma_driver);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001084}
1085subsys_initcall(edma_init);
1086
1087static void __exit edma_exit(void)
1088{
Matt Porterc2dde5f2012-08-22 21:09:34 -04001089 platform_driver_unregister(&edma_driver);
1090}
1091module_exit(edma_exit);
1092
Josh Boyerd71505b2013-09-04 10:32:50 -04001093MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -04001094MODULE_DESCRIPTION("TI EDMA DMA engine driver");
1095MODULE_LICENSE("GPL v2");