blob: 5d9f57f27ffb2ca2c45426077271af797444ee56 [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
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050060struct edma_pset {
Thomas Gleixnerc2da2342014-04-28 14:29:57 -050061 u32 len;
62 dma_addr_t addr;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050063 struct edmacc_param param;
64};
65
Matt Porterc2dde5f2012-08-22 21:09:34 -040066struct edma_desc {
67 struct virt_dma_desc vdesc;
68 struct list_head node;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -050069 enum dma_transfer_direction direction;
Joel Fernandes50a9c702013-10-31 16:31:23 -050070 int cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -040071 int absync;
72 int pset_nr;
Joel Fernandes53407062013-09-03 10:02:46 -050073 int processed;
Thomas Gleixner740b41f2014-04-28 14:34:11 -050074 int processed_stat;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -050075 u32 residue;
Thomas Gleixner740b41f2014-04-28 14:34:11 -050076 u32 sg_len;
77 u32 residue_stat;
78 struct edma_chan *echan;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050079 struct edma_pset pset[0];
Matt Porterc2dde5f2012-08-22 21:09:34 -040080};
81
82struct edma_cc;
83
84struct edma_chan {
85 struct virt_dma_chan vchan;
86 struct list_head node;
87 struct edma_desc *edesc;
88 struct edma_cc *ecc;
89 int ch_num;
90 bool alloced;
91 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -050092 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -050093 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -040094};
95
96struct edma_cc {
97 int ctlr;
98 struct dma_device dma_slave;
99 struct edma_chan slave_chans[EDMA_CHANS];
100 int num_slave_chans;
101 int dummy_slot;
102};
103
104static inline struct edma_cc *to_edma_cc(struct dma_device *d)
105{
106 return container_of(d, struct edma_cc, dma_slave);
107}
108
109static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
110{
111 return container_of(c, struct edma_chan, vchan.chan);
112}
113
114static inline struct edma_desc
115*to_edma_desc(struct dma_async_tx_descriptor *tx)
116{
117 return container_of(tx, struct edma_desc, vdesc.tx);
118}
119
120static void edma_desc_free(struct virt_dma_desc *vdesc)
121{
122 kfree(container_of(vdesc, struct edma_desc, vdesc));
123}
124
125/* Dispatch a queued descriptor to the controller (caller holds lock) */
126static void edma_execute(struct edma_chan *echan)
127{
Joel Fernandes53407062013-09-03 10:02:46 -0500128 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400129 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500130 struct device *dev = echan->vchan.chan.device->dev;
131 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400132
Joel Fernandes53407062013-09-03 10:02:46 -0500133 /* If either we processed all psets or we're still not started */
134 if (!echan->edesc ||
135 echan->edesc->pset_nr == echan->edesc->processed) {
136 /* Get next vdesc */
137 vdesc = vchan_next_desc(&echan->vchan);
138 if (!vdesc) {
139 echan->edesc = NULL;
140 return;
141 }
142 list_del(&vdesc->node);
143 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400144 }
145
Joel Fernandes53407062013-09-03 10:02:46 -0500146 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400147
Joel Fernandes53407062013-09-03 10:02:46 -0500148 /* Find out how many left */
149 left = edesc->pset_nr - edesc->processed;
150 nslots = min(MAX_NR_SG, left);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500151 edesc->sg_len = 0;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400152
153 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500154 for (i = 0; i < nslots; i++) {
155 j = i + edesc->processed;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500156 edma_write_slot(echan->slot[i], &edesc->pset[j].param);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500157 edesc->sg_len += edesc->pset[j].len;
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300158 dev_vdbg(echan->vchan.chan.device->dev,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400159 "\n pset[%d]:\n"
160 " chnum\t%d\n"
161 " slot\t%d\n"
162 " opt\t%08x\n"
163 " src\t%08x\n"
164 " dst\t%08x\n"
165 " abcnt\t%08x\n"
166 " ccnt\t%08x\n"
167 " bidx\t%08x\n"
168 " cidx\t%08x\n"
169 " lkrld\t%08x\n",
Joel Fernandes53407062013-09-03 10:02:46 -0500170 j, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500171 edesc->pset[j].param.opt,
172 edesc->pset[j].param.src,
173 edesc->pset[j].param.dst,
174 edesc->pset[j].param.a_b_cnt,
175 edesc->pset[j].param.ccnt,
176 edesc->pset[j].param.src_dst_bidx,
177 edesc->pset[j].param.src_dst_cidx,
178 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400179 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500180 if (i != (nslots - 1))
Matt Porterc2dde5f2012-08-22 21:09:34 -0400181 edma_link(echan->slot[i], echan->slot[i+1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400182 }
183
Joel Fernandes53407062013-09-03 10:02:46 -0500184 edesc->processed += nslots;
185
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500186 /*
187 * If this is either the last set in a set of SG-list transactions
188 * then setup a link to the dummy slot, this results in all future
189 * events being absorbed and that's OK because we're done
190 */
Joel Fernandes50a9c702013-10-31 16:31:23 -0500191 if (edesc->processed == edesc->pset_nr) {
192 if (edesc->cyclic)
193 edma_link(echan->slot[nslots-1], echan->slot[1]);
194 else
195 edma_link(echan->slot[nslots-1],
196 echan->ecc->dummy_slot);
197 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500198
Joel Fernandes53407062013-09-03 10:02:46 -0500199 if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300200 dev_dbg(dev, "first transfer starting on channel %d\n",
201 echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500202 edma_start(echan->ch_num);
Sekhar Nori5fc68a62014-03-19 11:25:50 +0530203 } else {
204 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
205 echan->ch_num, edesc->processed);
206 edma_resume(echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500207 }
Joel Fernandesc5f47992013-08-29 18:05:43 -0500208
209 /*
210 * This happens due to setup times between intermediate transfers
211 * in long SG lists which have to be broken up into transfers of
212 * MAX_NR_SG
213 */
214 if (echan->missed) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300215 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500216 edma_clean_channel(echan->ch_num);
217 edma_stop(echan->ch_num);
218 edma_start(echan->ch_num);
219 edma_trigger_channel(echan->ch_num);
220 echan->missed = 0;
221 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400222}
223
224static int edma_terminate_all(struct edma_chan *echan)
225{
226 unsigned long flags;
227 LIST_HEAD(head);
228
229 spin_lock_irqsave(&echan->vchan.lock, flags);
230
231 /*
232 * Stop DMA activity: we assume the callback will not be called
233 * after edma_dma() returns (even if it does, it will see
234 * echan->edesc is NULL and exit.)
235 */
236 if (echan->edesc) {
237 echan->edesc = NULL;
238 edma_stop(echan->ch_num);
239 }
240
241 vchan_get_all_descriptors(&echan->vchan, &head);
242 spin_unlock_irqrestore(&echan->vchan.lock, flags);
243 vchan_dma_desc_free_list(&echan->vchan, &head);
244
245 return 0;
246}
247
Matt Porterc2dde5f2012-08-22 21:09:34 -0400248static int edma_slave_config(struct edma_chan *echan,
Matt Porter661f7cb2013-01-10 13:41:04 -0500249 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400250{
Matt Porter661f7cb2013-01-10 13:41:04 -0500251 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
252 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400253 return -EINVAL;
254
Matt Porter661f7cb2013-01-10 13:41:04 -0500255 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400256
257 return 0;
258}
259
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300260static int edma_dma_pause(struct edma_chan *echan)
261{
262 /* Pause/Resume only allowed with cyclic mode */
263 if (!echan->edesc->cyclic)
264 return -EINVAL;
265
266 edma_pause(echan->ch_num);
267 return 0;
268}
269
270static int edma_dma_resume(struct edma_chan *echan)
271{
272 /* Pause/Resume only allowed with cyclic mode */
273 if (!echan->edesc->cyclic)
274 return -EINVAL;
275
276 edma_resume(echan->ch_num);
277 return 0;
278}
279
Matt Porterc2dde5f2012-08-22 21:09:34 -0400280static int edma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
281 unsigned long arg)
282{
283 int ret = 0;
284 struct dma_slave_config *config;
285 struct edma_chan *echan = to_edma_chan(chan);
286
287 switch (cmd) {
288 case DMA_TERMINATE_ALL:
289 edma_terminate_all(echan);
290 break;
291 case DMA_SLAVE_CONFIG:
292 config = (struct dma_slave_config *)arg;
293 ret = edma_slave_config(echan, config);
294 break;
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300295 case DMA_PAUSE:
296 ret = edma_dma_pause(echan);
297 break;
298
299 case DMA_RESUME:
300 ret = edma_dma_resume(echan);
301 break;
302
Matt Porterc2dde5f2012-08-22 21:09:34 -0400303 default:
304 ret = -ENOSYS;
305 }
306
307 return ret;
308}
309
Joel Fernandesfd009032013-09-23 18:05:13 -0500310/*
311 * A PaRAM set configuration abstraction used by other modes
312 * @chan: Channel who's PaRAM set we're configuring
313 * @pset: PaRAM set to initialize and setup.
314 * @src_addr: Source address of the DMA
315 * @dst_addr: Destination address of the DMA
316 * @burst: In units of dev_width, how much to send
317 * @dev_width: How much is the dev_width
318 * @dma_length: Total length of the DMA transfer
319 * @direction: Direction of the transfer
320 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500321static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
Joel Fernandesfd009032013-09-23 18:05:13 -0500322 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
323 enum dma_slave_buswidth dev_width, unsigned int dma_length,
324 enum dma_transfer_direction direction)
325{
326 struct edma_chan *echan = to_edma_chan(chan);
327 struct device *dev = chan->device->dev;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500328 struct edmacc_param *param = &epset->param;
Joel Fernandesfd009032013-09-23 18:05:13 -0500329 int acnt, bcnt, ccnt, cidx;
330 int src_bidx, dst_bidx, src_cidx, dst_cidx;
331 int absync;
332
333 acnt = dev_width;
Peter Ujfalusib2b617d2014-04-14 14:41:58 +0300334
335 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
336 if (!burst)
337 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -0500338 /*
339 * If the maxburst is equal to the fifo width, use
340 * A-synced transfers. This allows for large contiguous
341 * buffer transfers using only one PaRAM set.
342 */
343 if (burst == 1) {
344 /*
345 * For the A-sync case, bcnt and ccnt are the remainder
346 * and quotient respectively of the division of:
347 * (dma_length / acnt) by (SZ_64K -1). This is so
348 * that in case bcnt over flows, we have ccnt to use.
349 * Note: In A-sync tranfer only, bcntrld is used, but it
350 * only applies for sg_dma_len(sg) >= SZ_64K.
351 * In this case, the best way adopted is- bccnt for the
352 * first frame will be the remainder below. Then for
353 * every successive frame, bcnt will be SZ_64K-1. This
354 * is assured as bcntrld = 0xffff in end of function.
355 */
356 absync = false;
357 ccnt = dma_length / acnt / (SZ_64K - 1);
358 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
359 /*
360 * If bcnt is non-zero, we have a remainder and hence an
361 * extra frame to transfer, so increment ccnt.
362 */
363 if (bcnt)
364 ccnt++;
365 else
366 bcnt = SZ_64K - 1;
367 cidx = acnt;
368 } else {
369 /*
370 * If maxburst is greater than the fifo address_width,
371 * use AB-synced transfers where A count is the fifo
372 * address_width and B count is the maxburst. In this
373 * case, we are limited to transfers of C count frames
374 * of (address_width * maxburst) where C count is limited
375 * to SZ_64K-1. This places an upper bound on the length
376 * of an SG segment that can be handled.
377 */
378 absync = true;
379 bcnt = burst;
380 ccnt = dma_length / (acnt * bcnt);
381 if (ccnt > (SZ_64K - 1)) {
382 dev_err(dev, "Exceeded max SG segment size\n");
383 return -EINVAL;
384 }
385 cidx = acnt * bcnt;
386 }
387
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500388 epset->len = dma_length;
389
Joel Fernandesfd009032013-09-23 18:05:13 -0500390 if (direction == DMA_MEM_TO_DEV) {
391 src_bidx = acnt;
392 src_cidx = cidx;
393 dst_bidx = 0;
394 dst_cidx = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500395 epset->addr = src_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -0500396 } else if (direction == DMA_DEV_TO_MEM) {
397 src_bidx = 0;
398 src_cidx = 0;
399 dst_bidx = acnt;
400 dst_cidx = cidx;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500401 epset->addr = dst_addr;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500402 } else if (direction == DMA_MEM_TO_MEM) {
403 src_bidx = acnt;
404 src_cidx = cidx;
405 dst_bidx = acnt;
406 dst_cidx = cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500407 } else {
408 dev_err(dev, "%s: direction not implemented yet\n", __func__);
409 return -EINVAL;
410 }
411
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500412 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
Joel Fernandesfd009032013-09-23 18:05:13 -0500413 /* Configure A or AB synchronized transfers */
414 if (absync)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500415 param->opt |= SYNCDIM;
Joel Fernandesfd009032013-09-23 18:05:13 -0500416
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500417 param->src = src_addr;
418 param->dst = dst_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -0500419
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500420 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
421 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500422
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500423 param->a_b_cnt = bcnt << 16 | acnt;
424 param->ccnt = ccnt;
Joel Fernandesfd009032013-09-23 18:05:13 -0500425 /*
426 * Only time when (bcntrld) auto reload is required is for
427 * A-sync case, and in this case, a requirement of reload value
428 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
429 * and then later will be populated by edma_execute.
430 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500431 param->link_bcntrld = 0xffffffff;
Joel Fernandesfd009032013-09-23 18:05:13 -0500432 return absync;
433}
434
Matt Porterc2dde5f2012-08-22 21:09:34 -0400435static struct dma_async_tx_descriptor *edma_prep_slave_sg(
436 struct dma_chan *chan, struct scatterlist *sgl,
437 unsigned int sg_len, enum dma_transfer_direction direction,
438 unsigned long tx_flags, void *context)
439{
440 struct edma_chan *echan = to_edma_chan(chan);
441 struct device *dev = chan->device->dev;
442 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -0500443 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -0500444 enum dma_slave_buswidth dev_width;
445 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400446 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -0500447 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400448
449 if (unlikely(!echan || !sgl || !sg_len))
450 return NULL;
451
Matt Porter661f7cb2013-01-10 13:41:04 -0500452 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500453 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500454 dev_width = echan->cfg.src_addr_width;
455 burst = echan->cfg.src_maxburst;
456 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500457 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500458 dev_width = echan->cfg.dst_addr_width;
459 burst = echan->cfg.dst_maxburst;
460 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +0300461 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Matt Porter661f7cb2013-01-10 13:41:04 -0500462 return NULL;
463 }
464
465 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300466 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400467 return NULL;
468 }
469
Matt Porterc2dde5f2012-08-22 21:09:34 -0400470 edesc = kzalloc(sizeof(*edesc) + sg_len *
471 sizeof(edesc->pset[0]), GFP_ATOMIC);
472 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300473 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400474 return NULL;
475 }
476
477 edesc->pset_nr = sg_len;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500478 edesc->residue = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500479 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500480 edesc->echan = echan;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400481
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500482 /* Allocate a PaRAM slot, if needed */
483 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
484
485 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -0400486 if (echan->slot[i] < 0) {
487 echan->slot[i] =
488 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
489 EDMA_SLOT_ANY);
490 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +0300491 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +0300492 dev_err(dev, "%s: Failed to allocate slot\n",
493 __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400494 return NULL;
495 }
496 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500497 }
498
499 /* Configure PaRAM sets for each SG */
500 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500501 /* Get address for each SG */
502 if (direction == DMA_DEV_TO_MEM)
503 dst_addr = sg_dma_address(sg);
504 else
505 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400506
Joel Fernandesfd009032013-09-23 18:05:13 -0500507 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
508 dst_addr, burst, dev_width,
509 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +0530510 if (ret < 0) {
511 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -0500512 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400513 }
514
Joel Fernandesfd009032013-09-23 18:05:13 -0500515 edesc->absync = ret;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500516 edesc->residue += sg_dma_len(sg);
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500517
518 /* If this is the last in a current SG set of transactions,
519 enable interrupts so that next set is processed */
520 if (!((i+1) % MAX_NR_SG))
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500521 edesc->pset[i].param.opt |= TCINTEN;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500522
Matt Porterc2dde5f2012-08-22 21:09:34 -0400523 /* If this is the last set, enable completion interrupt flag */
524 if (i == sg_len - 1)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500525 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400526 }
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500527 edesc->residue_stat = edesc->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400528
Matt Porterc2dde5f2012-08-22 21:09:34 -0400529 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
530}
Matt Porterc2dde5f2012-08-22 21:09:34 -0400531
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500532struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
533 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
534 size_t len, unsigned long tx_flags)
535{
536 int ret;
537 struct edma_desc *edesc;
538 struct device *dev = chan->device->dev;
539 struct edma_chan *echan = to_edma_chan(chan);
540
541 if (unlikely(!echan || !len))
542 return NULL;
543
544 edesc = kzalloc(sizeof(*edesc) + sizeof(edesc->pset[0]), GFP_ATOMIC);
545 if (!edesc) {
546 dev_dbg(dev, "Failed to allocate a descriptor\n");
547 return NULL;
548 }
549
550 edesc->pset_nr = 1;
551
552 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
553 DMA_SLAVE_BUSWIDTH_4_BYTES, len, DMA_MEM_TO_MEM);
554 if (ret < 0)
555 return NULL;
556
557 edesc->absync = ret;
558
559 /*
560 * Enable intermediate transfer chaining to re-trigger channel
561 * on completion of every TR, and enable transfer-completion
562 * interrupt on completion of the whole transfer.
563 */
564 edesc->pset[0].opt |= ITCCHEN;
565 edesc->pset[0].opt |= TCINTEN;
566
567 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
568}
569
Joel Fernandes50a9c702013-10-31 16:31:23 -0500570static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
571 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
572 size_t period_len, enum dma_transfer_direction direction,
573 unsigned long tx_flags, void *context)
574{
575 struct edma_chan *echan = to_edma_chan(chan);
576 struct device *dev = chan->device->dev;
577 struct edma_desc *edesc;
578 dma_addr_t src_addr, dst_addr;
579 enum dma_slave_buswidth dev_width;
580 u32 burst;
581 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400582
Joel Fernandes50a9c702013-10-31 16:31:23 -0500583 if (unlikely(!echan || !buf_len || !period_len))
584 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400585
Joel Fernandes50a9c702013-10-31 16:31:23 -0500586 if (direction == DMA_DEV_TO_MEM) {
587 src_addr = echan->cfg.src_addr;
588 dst_addr = buf_addr;
589 dev_width = echan->cfg.src_addr_width;
590 burst = echan->cfg.src_maxburst;
591 } else if (direction == DMA_MEM_TO_DEV) {
592 src_addr = buf_addr;
593 dst_addr = echan->cfg.dst_addr;
594 dev_width = echan->cfg.dst_addr_width;
595 burst = echan->cfg.dst_maxburst;
596 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +0300597 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500598 return NULL;
599 }
600
601 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300602 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500603 return NULL;
604 }
605
606 if (unlikely(buf_len % period_len)) {
607 dev_err(dev, "Period should be multiple of Buffer length\n");
608 return NULL;
609 }
610
611 nslots = (buf_len / period_len) + 1;
612
613 /*
614 * Cyclic DMA users such as audio cannot tolerate delays introduced
615 * by cases where the number of periods is more than the maximum
616 * number of SGs the EDMA driver can handle at a time. For DMA types
617 * such as Slave SGs, such delays are tolerable and synchronized,
618 * but the synchronization is difficult to achieve with Cyclic and
619 * cannot be guaranteed, so we error out early.
620 */
621 if (nslots > MAX_NR_SG)
622 return NULL;
623
624 edesc = kzalloc(sizeof(*edesc) + nslots *
625 sizeof(edesc->pset[0]), GFP_ATOMIC);
626 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300627 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500628 return NULL;
629 }
630
631 edesc->cyclic = 1;
632 edesc->pset_nr = nslots;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500633 edesc->residue = edesc->residue_stat = buf_len;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500634 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500635 edesc->echan = echan;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500636
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300637 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
638 __func__, echan->ch_num, nslots, period_len, buf_len);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500639
640 for (i = 0; i < nslots; i++) {
641 /* Allocate a PaRAM slot, if needed */
642 if (echan->slot[i] < 0) {
643 echan->slot[i] =
644 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
645 EDMA_SLOT_ANY);
646 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100647 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +0300648 dev_err(dev, "%s: Failed to allocate slot\n",
649 __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500650 return NULL;
651 }
652 }
653
654 if (i == nslots - 1) {
655 memcpy(&edesc->pset[i], &edesc->pset[0],
656 sizeof(edesc->pset[0]));
657 break;
658 }
659
660 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
661 dst_addr, burst, dev_width, period_len,
662 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100663 if (ret < 0) {
664 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500665 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100666 }
Joel Fernandes50a9c702013-10-31 16:31:23 -0500667
668 if (direction == DMA_DEV_TO_MEM)
669 dst_addr += period_len;
670 else
671 src_addr += period_len;
672
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300673 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
674 dev_vdbg(dev,
Joel Fernandes50a9c702013-10-31 16:31:23 -0500675 "\n pset[%d]:\n"
676 " chnum\t%d\n"
677 " slot\t%d\n"
678 " opt\t%08x\n"
679 " src\t%08x\n"
680 " dst\t%08x\n"
681 " abcnt\t%08x\n"
682 " ccnt\t%08x\n"
683 " bidx\t%08x\n"
684 " cidx\t%08x\n"
685 " lkrld\t%08x\n",
686 i, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500687 edesc->pset[i].param.opt,
688 edesc->pset[i].param.src,
689 edesc->pset[i].param.dst,
690 edesc->pset[i].param.a_b_cnt,
691 edesc->pset[i].param.ccnt,
692 edesc->pset[i].param.src_dst_bidx,
693 edesc->pset[i].param.src_dst_cidx,
694 edesc->pset[i].param.link_bcntrld);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500695
696 edesc->absync = ret;
697
698 /*
699 * Enable interrupts for every period because callback
700 * has to be called for every period.
701 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500702 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400703 }
704
705 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
706}
707
708static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
709{
710 struct edma_chan *echan = data;
711 struct device *dev = echan->vchan.chan.device->dev;
712 struct edma_desc *edesc;
Joel Fernandesc5f47992013-08-29 18:05:43 -0500713 struct edmacc_param p;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400714
Joel Fernandes50a9c702013-10-31 16:31:23 -0500715 edesc = echan->edesc;
716
717 /* Pause the channel for non-cyclic */
718 if (!edesc || (edesc && !edesc->cyclic))
719 edma_pause(echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400720
721 switch (ch_status) {
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530722 case EDMA_DMA_COMPLETE:
Joel Fernandes406efb12014-04-17 00:58:33 -0500723 spin_lock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400724
Matt Porterc2dde5f2012-08-22 21:09:34 -0400725 if (edesc) {
Joel Fernandes50a9c702013-10-31 16:31:23 -0500726 if (edesc->cyclic) {
727 vchan_cyclic_callback(&edesc->vdesc);
728 } else if (edesc->processed == edesc->pset_nr) {
Joel Fernandes53407062013-09-03 10:02:46 -0500729 dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500730 edesc->residue = 0;
Joel Fernandes53407062013-09-03 10:02:46 -0500731 edma_stop(echan->ch_num);
732 vchan_cookie_complete(&edesc->vdesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500733 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500734 } else {
735 dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500736
737 /* Update statistics for tx_status */
738 edesc->residue -= edesc->sg_len;
739 edesc->residue_stat = edesc->residue;
740 edesc->processed_stat = edesc->processed;
741
Joel Fernandes50a9c702013-10-31 16:31:23 -0500742 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500743 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400744 }
745
Joel Fernandes406efb12014-04-17 00:58:33 -0500746 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400747
748 break;
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530749 case EDMA_DMA_CC_ERROR:
Joel Fernandes406efb12014-04-17 00:58:33 -0500750 spin_lock(&echan->vchan.lock);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500751
752 edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
753
754 /*
755 * Issue later based on missed flag which will be sure
756 * to happen as:
757 * (1) we finished transmitting an intermediate slot and
758 * edma_execute is coming up.
759 * (2) or we finished current transfer and issue will
760 * call edma_execute.
761 *
762 * Important note: issuing can be dangerous here and
763 * lead to some nasty recursion when we are in a NULL
764 * slot. So we avoid doing so and set the missed flag.
765 */
766 if (p.a_b_cnt == 0 && p.ccnt == 0) {
767 dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
768 echan->missed = 1;
769 } else {
770 /*
771 * The slot is already programmed but the event got
772 * missed, so its safe to issue it here.
773 */
774 dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
775 edma_clean_channel(echan->ch_num);
776 edma_stop(echan->ch_num);
777 edma_start(echan->ch_num);
778 edma_trigger_channel(echan->ch_num);
779 }
780
Joel Fernandes406efb12014-04-17 00:58:33 -0500781 spin_unlock(&echan->vchan.lock);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500782
Matt Porterc2dde5f2012-08-22 21:09:34 -0400783 break;
784 default:
785 break;
786 }
787}
788
789/* Alloc channel resources */
790static int edma_alloc_chan_resources(struct dma_chan *chan)
791{
792 struct edma_chan *echan = to_edma_chan(chan);
793 struct device *dev = chan->device->dev;
794 int ret;
795 int a_ch_num;
796 LIST_HEAD(descs);
797
798 a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
799 chan, EVENTQ_DEFAULT);
800
801 if (a_ch_num < 0) {
802 ret = -ENODEV;
803 goto err_no_chan;
804 }
805
806 if (a_ch_num != echan->ch_num) {
807 dev_err(dev, "failed to allocate requested channel %u:%u\n",
808 EDMA_CTLR(echan->ch_num),
809 EDMA_CHAN_SLOT(echan->ch_num));
810 ret = -ENODEV;
811 goto err_wrong_chan;
812 }
813
814 echan->alloced = true;
815 echan->slot[0] = echan->ch_num;
816
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300817 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300818 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400819
820 return 0;
821
822err_wrong_chan:
823 edma_free_channel(a_ch_num);
824err_no_chan:
825 return ret;
826}
827
828/* Free channel resources */
829static void edma_free_chan_resources(struct dma_chan *chan)
830{
831 struct edma_chan *echan = to_edma_chan(chan);
832 struct device *dev = chan->device->dev;
833 int i;
834
835 /* Terminate transfers */
836 edma_stop(echan->ch_num);
837
838 vchan_free_chan_resources(&echan->vchan);
839
840 /* Free EDMA PaRAM slots */
841 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
842 if (echan->slot[i] >= 0) {
843 edma_free_slot(echan->slot[i]);
844 echan->slot[i] = -1;
845 }
846 }
847
848 /* Free EDMA channel */
849 if (echan->alloced) {
850 edma_free_channel(echan->ch_num);
851 echan->alloced = false;
852 }
853
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300854 dev_dbg(dev, "freeing channel for %u\n", echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400855}
856
857/* Send pending descriptor to hardware */
858static void edma_issue_pending(struct dma_chan *chan)
859{
860 struct edma_chan *echan = to_edma_chan(chan);
861 unsigned long flags;
862
863 spin_lock_irqsave(&echan->vchan.lock, flags);
864 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
865 edma_execute(echan);
866 spin_unlock_irqrestore(&echan->vchan.lock, flags);
867}
868
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500869static u32 edma_residue(struct edma_desc *edesc)
870{
871 bool dst = edesc->direction == DMA_DEV_TO_MEM;
872 struct edma_pset *pset = edesc->pset;
873 dma_addr_t done, pos;
874 int i;
875
876 /*
877 * We always read the dst/src position from the first RamPar
878 * pset. That's the one which is active now.
879 */
880 pos = edma_get_position(edesc->echan->slot[0], dst);
881
882 /*
883 * Cyclic is simple. Just subtract pset[0].addr from pos.
884 *
885 * We never update edesc->residue in the cyclic case, so we
886 * can tell the remaining room to the end of the circular
887 * buffer.
888 */
889 if (edesc->cyclic) {
890 done = pos - pset->addr;
891 edesc->residue_stat = edesc->residue - done;
892 return edesc->residue_stat;
893 }
894
895 /*
896 * For SG operation we catch up with the last processed
897 * status.
898 */
899 pset += edesc->processed_stat;
900
901 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
902 /*
903 * If we are inside this pset address range, we know
904 * this is the active one. Get the current delta and
905 * stop walking the psets.
906 */
907 if (pos >= pset->addr && pos < pset->addr + pset->len)
908 return edesc->residue_stat - (pos - pset->addr);
909
910 /* Otherwise mark it done and update residue_stat. */
911 edesc->processed_stat++;
912 edesc->residue_stat -= pset->len;
913 }
914 return edesc->residue_stat;
915}
916
Matt Porterc2dde5f2012-08-22 21:09:34 -0400917/* Check request completion status */
918static enum dma_status edma_tx_status(struct dma_chan *chan,
919 dma_cookie_t cookie,
920 struct dma_tx_state *txstate)
921{
922 struct edma_chan *echan = to_edma_chan(chan);
923 struct virt_dma_desc *vdesc;
924 enum dma_status ret;
925 unsigned long flags;
926
927 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +0530928 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400929 return ret;
930
931 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -0500932 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500933 txstate->residue = edma_residue(echan->edesc);
Thomas Gleixnerde135932014-04-28 14:19:51 -0500934 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
935 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400936 spin_unlock_irqrestore(&echan->vchan.lock, flags);
937
938 return ret;
939}
940
941static void __init edma_chan_init(struct edma_cc *ecc,
942 struct dma_device *dma,
943 struct edma_chan *echans)
944{
945 int i, j;
946
947 for (i = 0; i < EDMA_CHANS; i++) {
948 struct edma_chan *echan = &echans[i];
949 echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
950 echan->ecc = ecc;
951 echan->vchan.desc_free = edma_desc_free;
952
953 vchan_init(&echan->vchan, dma);
954
955 INIT_LIST_HEAD(&echan->node);
956 for (j = 0; j < EDMA_MAX_SLOTS; j++)
957 echan->slot[j] = -1;
958 }
959}
960
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +0300961#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
962 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
963 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
964
965static int edma_dma_device_slave_caps(struct dma_chan *dchan,
966 struct dma_slave_caps *caps)
967{
968 caps->src_addr_widths = EDMA_DMA_BUSWIDTHS;
969 caps->dstn_addr_widths = EDMA_DMA_BUSWIDTHS;
970 caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
971 caps->cmd_pause = true;
972 caps->cmd_terminate = true;
973 caps->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
974
975 return 0;
976}
977
Matt Porterc2dde5f2012-08-22 21:09:34 -0400978static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
979 struct device *dev)
980{
981 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500982 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500983 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400984 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
985 dma->device_free_chan_resources = edma_free_chan_resources;
986 dma->device_issue_pending = edma_issue_pending;
987 dma->device_tx_status = edma_tx_status;
988 dma->device_control = edma_control;
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +0300989 dma->device_slave_caps = edma_dma_device_slave_caps;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400990 dma->dev = dev;
991
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500992 /*
993 * code using dma memcpy must make sure alignment of
994 * length is at dma->copy_align boundary.
995 */
996 dma->copy_align = DMA_SLAVE_BUSWIDTH_4_BYTES;
997
Matt Porterc2dde5f2012-08-22 21:09:34 -0400998 INIT_LIST_HEAD(&dma->channels);
999}
1000
Bill Pemberton463a1f82012-11-19 13:22:55 -05001001static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001002{
1003 struct edma_cc *ecc;
1004 int ret;
1005
Russell King94cb0e72013-06-27 13:45:16 +01001006 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1007 if (ret)
1008 return ret;
1009
Matt Porterc2dde5f2012-08-22 21:09:34 -04001010 ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
1011 if (!ecc) {
1012 dev_err(&pdev->dev, "Can't allocate controller\n");
1013 return -ENOMEM;
1014 }
1015
1016 ecc->ctlr = pdev->id;
1017 ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
1018 if (ecc->dummy_slot < 0) {
1019 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
1020 return -EIO;
1021 }
1022
1023 dma_cap_zero(ecc->dma_slave.cap_mask);
1024 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
Peter Ujfalusi232b223d2014-04-14 14:42:00 +03001025 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001026 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001027
1028 edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
1029
1030 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
1031
1032 ret = dma_async_device_register(&ecc->dma_slave);
1033 if (ret)
1034 goto err_reg1;
1035
1036 platform_set_drvdata(pdev, ecc);
1037
1038 dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
1039
1040 return 0;
1041
1042err_reg1:
1043 edma_free_slot(ecc->dummy_slot);
1044 return ret;
1045}
1046
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001047static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001048{
1049 struct device *dev = &pdev->dev;
1050 struct edma_cc *ecc = dev_get_drvdata(dev);
1051
1052 dma_async_device_unregister(&ecc->dma_slave);
1053 edma_free_slot(ecc->dummy_slot);
1054
1055 return 0;
1056}
1057
1058static struct platform_driver edma_driver = {
1059 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05001060 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -04001061 .driver = {
1062 .name = "edma-dma-engine",
1063 .owner = THIS_MODULE,
1064 },
1065};
1066
1067bool edma_filter_fn(struct dma_chan *chan, void *param)
1068{
1069 if (chan->device->dev->driver == &edma_driver.driver) {
1070 struct edma_chan *echan = to_edma_chan(chan);
1071 unsigned ch_req = *(unsigned *)param;
1072 return ch_req == echan->ch_num;
1073 }
1074 return false;
1075}
1076EXPORT_SYMBOL(edma_filter_fn);
1077
1078static struct platform_device *pdev0, *pdev1;
1079
1080static const struct platform_device_info edma_dev_info0 = {
1081 .name = "edma-dma-engine",
1082 .id = 0,
Russell King94cb0e72013-06-27 13:45:16 +01001083 .dma_mask = DMA_BIT_MASK(32),
Matt Porterc2dde5f2012-08-22 21:09:34 -04001084};
1085
1086static const struct platform_device_info edma_dev_info1 = {
1087 .name = "edma-dma-engine",
1088 .id = 1,
Russell King94cb0e72013-06-27 13:45:16 +01001089 .dma_mask = DMA_BIT_MASK(32),
Matt Porterc2dde5f2012-08-22 21:09:34 -04001090};
1091
1092static int edma_init(void)
1093{
1094 int ret = platform_driver_register(&edma_driver);
1095
1096 if (ret == 0) {
1097 pdev0 = platform_device_register_full(&edma_dev_info0);
1098 if (IS_ERR(pdev0)) {
1099 platform_driver_unregister(&edma_driver);
1100 ret = PTR_ERR(pdev0);
1101 goto out;
1102 }
1103 }
1104
1105 if (EDMA_CTLRS == 2) {
1106 pdev1 = platform_device_register_full(&edma_dev_info1);
1107 if (IS_ERR(pdev1)) {
1108 platform_driver_unregister(&edma_driver);
1109 platform_device_unregister(pdev0);
1110 ret = PTR_ERR(pdev1);
1111 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001112 }
1113
1114out:
1115 return ret;
1116}
1117subsys_initcall(edma_init);
1118
1119static void __exit edma_exit(void)
1120{
1121 platform_device_unregister(pdev0);
1122 if (pdev1)
1123 platform_device_unregister(pdev1);
1124 platform_driver_unregister(&edma_driver);
1125}
1126module_exit(edma_exit);
1127
Josh Boyerd71505b2013-09-04 10:32:50 -04001128MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -04001129MODULE_DESCRIPTION("TI EDMA DMA engine driver");
1130MODULE_LICENSE("GPL v2");