blob: 276157f22612dc18140b84294ab1ef49ecf46ebc [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>
Lad, Prabhakarb7a4fd52015-02-04 13:03:27 +000018#include <linux/edma.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040019#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/list.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26#include <linux/spinlock.h>
Peter Ujfalusied646102014-07-31 13:12:38 +030027#include <linux/of.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040028
Matt Porter3ad7a422013-03-06 11:15:31 -050029#include <linux/platform_data/edma.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040030
31#include "dmaengine.h"
32#include "virt-dma.h"
33
34/*
35 * This will go away when the private EDMA API is folded
36 * into this driver and the platform device(s) are
37 * instantiated in the arch code. We can only get away
38 * with this simplification because DA8XX may not be built
39 * in the same kernel image with other DaVinci parts. This
40 * avoids having to sprinkle dmaengine driver platform devices
41 * and data throughout all the existing board files.
42 */
43#ifdef CONFIG_ARCH_DAVINCI_DA8XX
44#define EDMA_CTLRS 2
45#define EDMA_CHANS 32
46#else
47#define EDMA_CTLRS 1
48#define EDMA_CHANS 64
49#endif /* CONFIG_ARCH_DAVINCI_DA8XX */
50
Joel Fernandes2abd5f12013-09-23 18:05:15 -050051/*
52 * Max of 20 segments per channel to conserve PaRAM slots
53 * Also note that MAX_NR_SG should be atleast the no.of periods
54 * that are required for ASoC, otherwise DMA prep calls will
55 * fail. Today davinci-pcm is the only user of this driver and
56 * requires atleast 17 slots, so we setup the default to 20.
57 */
58#define MAX_NR_SG 20
Matt Porterc2dde5f2012-08-22 21:09:34 -040059#define EDMA_MAX_SLOTS MAX_NR_SG
60#define EDMA_DESCRIPTORS 16
61
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050062struct edma_pset {
Thomas Gleixnerc2da2342014-04-28 14:29:57 -050063 u32 len;
64 dma_addr_t addr;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050065 struct edmacc_param param;
66};
67
Matt Porterc2dde5f2012-08-22 21:09:34 -040068struct edma_desc {
69 struct virt_dma_desc vdesc;
70 struct list_head node;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -050071 enum dma_transfer_direction direction;
Joel Fernandes50a9c702013-10-31 16:31:23 -050072 int cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -040073 int absync;
74 int pset_nr;
Thomas Gleixner740b41f2014-04-28 14:34:11 -050075 struct edma_chan *echan;
Joel Fernandes04361d82014-04-28 15:19:31 -050076 int processed;
77
78 /*
79 * The following 4 elements are used for residue accounting.
80 *
81 * - processed_stat: the number of SG elements we have traversed
82 * so far to cover accounting. This is updated directly to processed
83 * during edma_callback and is always <= processed, because processed
84 * refers to the number of pending transfer (programmed to EDMA
85 * controller), where as processed_stat tracks number of transfers
86 * accounted for so far.
87 *
88 * - residue: The amount of bytes we have left to transfer for this desc
89 *
90 * - residue_stat: The residue in bytes of data we have covered
91 * so far for accounting. This is updated directly to residue
92 * during callbacks to keep it current.
93 *
94 * - sg_len: Tracks the length of the current intermediate transfer,
95 * this is required to update the residue during intermediate transfer
96 * completion callback.
97 */
98 int processed_stat;
99 u32 sg_len;
100 u32 residue;
101 u32 residue_stat;
102
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500103 struct edma_pset pset[0];
Matt Porterc2dde5f2012-08-22 21:09:34 -0400104};
105
106struct edma_cc;
107
108struct edma_chan {
109 struct virt_dma_chan vchan;
110 struct list_head node;
111 struct edma_desc *edesc;
112 struct edma_cc *ecc;
113 int ch_num;
114 bool alloced;
115 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -0500116 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -0500117 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400118};
119
120struct edma_cc {
121 int ctlr;
122 struct dma_device dma_slave;
123 struct edma_chan slave_chans[EDMA_CHANS];
124 int num_slave_chans;
125 int dummy_slot;
126};
127
128static inline struct edma_cc *to_edma_cc(struct dma_device *d)
129{
130 return container_of(d, struct edma_cc, dma_slave);
131}
132
133static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
134{
135 return container_of(c, struct edma_chan, vchan.chan);
136}
137
138static inline struct edma_desc
139*to_edma_desc(struct dma_async_tx_descriptor *tx)
140{
141 return container_of(tx, struct edma_desc, vdesc.tx);
142}
143
144static void edma_desc_free(struct virt_dma_desc *vdesc)
145{
146 kfree(container_of(vdesc, struct edma_desc, vdesc));
147}
148
149/* Dispatch a queued descriptor to the controller (caller holds lock) */
150static void edma_execute(struct edma_chan *echan)
151{
Joel Fernandes53407062013-09-03 10:02:46 -0500152 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400153 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500154 struct device *dev = echan->vchan.chan.device->dev;
155 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400156
Joel Fernandes53407062013-09-03 10:02:46 -0500157 /* If either we processed all psets or we're still not started */
158 if (!echan->edesc ||
159 echan->edesc->pset_nr == echan->edesc->processed) {
160 /* Get next vdesc */
161 vdesc = vchan_next_desc(&echan->vchan);
162 if (!vdesc) {
163 echan->edesc = NULL;
164 return;
165 }
166 list_del(&vdesc->node);
167 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400168 }
169
Joel Fernandes53407062013-09-03 10:02:46 -0500170 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400171
Joel Fernandes53407062013-09-03 10:02:46 -0500172 /* Find out how many left */
173 left = edesc->pset_nr - edesc->processed;
174 nslots = min(MAX_NR_SG, left);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500175 edesc->sg_len = 0;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400176
177 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500178 for (i = 0; i < nslots; i++) {
179 j = i + edesc->processed;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500180 edma_write_slot(echan->slot[i], &edesc->pset[j].param);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500181 edesc->sg_len += edesc->pset[j].len;
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300182 dev_vdbg(echan->vchan.chan.device->dev,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400183 "\n pset[%d]:\n"
184 " chnum\t%d\n"
185 " slot\t%d\n"
186 " opt\t%08x\n"
187 " src\t%08x\n"
188 " dst\t%08x\n"
189 " abcnt\t%08x\n"
190 " ccnt\t%08x\n"
191 " bidx\t%08x\n"
192 " cidx\t%08x\n"
193 " lkrld\t%08x\n",
Joel Fernandes53407062013-09-03 10:02:46 -0500194 j, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500195 edesc->pset[j].param.opt,
196 edesc->pset[j].param.src,
197 edesc->pset[j].param.dst,
198 edesc->pset[j].param.a_b_cnt,
199 edesc->pset[j].param.ccnt,
200 edesc->pset[j].param.src_dst_bidx,
201 edesc->pset[j].param.src_dst_cidx,
202 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400203 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500204 if (i != (nslots - 1))
Matt Porterc2dde5f2012-08-22 21:09:34 -0400205 edma_link(echan->slot[i], echan->slot[i+1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400206 }
207
Joel Fernandes53407062013-09-03 10:02:46 -0500208 edesc->processed += nslots;
209
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500210 /*
211 * If this is either the last set in a set of SG-list transactions
212 * then setup a link to the dummy slot, this results in all future
213 * events being absorbed and that's OK because we're done
214 */
Joel Fernandes50a9c702013-10-31 16:31:23 -0500215 if (edesc->processed == edesc->pset_nr) {
216 if (edesc->cyclic)
217 edma_link(echan->slot[nslots-1], echan->slot[1]);
218 else
219 edma_link(echan->slot[nslots-1],
220 echan->ecc->dummy_slot);
221 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500222
Joel Fernandes53407062013-09-03 10:02:46 -0500223 if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300224 dev_dbg(dev, "first transfer starting on channel %d\n",
225 echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500226 edma_start(echan->ch_num);
Sekhar Nori5fc68a62014-03-19 11:25:50 +0530227 } else {
228 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
229 echan->ch_num, edesc->processed);
230 edma_resume(echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500231 }
Joel Fernandesc5f47992013-08-29 18:05:43 -0500232
233 /*
234 * This happens due to setup times between intermediate transfers
235 * in long SG lists which have to be broken up into transfers of
236 * MAX_NR_SG
237 */
238 if (echan->missed) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300239 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500240 edma_clean_channel(echan->ch_num);
241 edma_stop(echan->ch_num);
242 edma_start(echan->ch_num);
243 edma_trigger_channel(echan->ch_num);
244 echan->missed = 0;
245 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400246}
247
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100248static int edma_terminate_all(struct dma_chan *chan)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400249{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100250 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400251 unsigned long flags;
252 LIST_HEAD(head);
253
254 spin_lock_irqsave(&echan->vchan.lock, flags);
255
256 /*
257 * Stop DMA activity: we assume the callback will not be called
258 * after edma_dma() returns (even if it does, it will see
259 * echan->edesc is NULL and exit.)
260 */
261 if (echan->edesc) {
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +0300262 int cyclic = echan->edesc->cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400263 echan->edesc = NULL;
264 edma_stop(echan->ch_num);
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +0300265 /* Move the cyclic channel back to default queue */
266 if (cyclic)
267 edma_assign_channel_eventq(echan->ch_num,
268 EVENTQ_DEFAULT);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400269 }
270
271 vchan_get_all_descriptors(&echan->vchan, &head);
272 spin_unlock_irqrestore(&echan->vchan.lock, flags);
273 vchan_dma_desc_free_list(&echan->vchan, &head);
274
275 return 0;
276}
277
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100278static int edma_slave_config(struct dma_chan *chan,
Matt Porter661f7cb2013-01-10 13:41:04 -0500279 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400280{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100281 struct edma_chan *echan = to_edma_chan(chan);
282
Matt Porter661f7cb2013-01-10 13:41:04 -0500283 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
284 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400285 return -EINVAL;
286
Matt Porter661f7cb2013-01-10 13:41:04 -0500287 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400288
289 return 0;
290}
291
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100292static int edma_dma_pause(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300293{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100294 struct edma_chan *echan = to_edma_chan(chan);
295
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300296 /* Pause/Resume only allowed with cyclic mode */
Sebastian Andrzej Siewior639559a2014-09-29 20:06:45 +0200297 if (!echan->edesc || !echan->edesc->cyclic)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300298 return -EINVAL;
299
300 edma_pause(echan->ch_num);
301 return 0;
302}
303
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100304static int edma_dma_resume(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300305{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100306 struct edma_chan *echan = to_edma_chan(chan);
307
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300308 /* Pause/Resume only allowed with cyclic mode */
309 if (!echan->edesc->cyclic)
310 return -EINVAL;
311
312 edma_resume(echan->ch_num);
313 return 0;
314}
315
Joel Fernandesfd009032013-09-23 18:05:13 -0500316/*
317 * A PaRAM set configuration abstraction used by other modes
318 * @chan: Channel who's PaRAM set we're configuring
319 * @pset: PaRAM set to initialize and setup.
320 * @src_addr: Source address of the DMA
321 * @dst_addr: Destination address of the DMA
322 * @burst: In units of dev_width, how much to send
323 * @dev_width: How much is the dev_width
324 * @dma_length: Total length of the DMA transfer
325 * @direction: Direction of the transfer
326 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500327static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
Joel Fernandesfd009032013-09-23 18:05:13 -0500328 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
329 enum dma_slave_buswidth dev_width, unsigned int dma_length,
330 enum dma_transfer_direction direction)
331{
332 struct edma_chan *echan = to_edma_chan(chan);
333 struct device *dev = chan->device->dev;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500334 struct edmacc_param *param = &epset->param;
Joel Fernandesfd009032013-09-23 18:05:13 -0500335 int acnt, bcnt, ccnt, cidx;
336 int src_bidx, dst_bidx, src_cidx, dst_cidx;
337 int absync;
338
339 acnt = dev_width;
Peter Ujfalusib2b617d2014-04-14 14:41:58 +0300340
341 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
342 if (!burst)
343 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -0500344 /*
345 * If the maxburst is equal to the fifo width, use
346 * A-synced transfers. This allows for large contiguous
347 * buffer transfers using only one PaRAM set.
348 */
349 if (burst == 1) {
350 /*
351 * For the A-sync case, bcnt and ccnt are the remainder
352 * and quotient respectively of the division of:
353 * (dma_length / acnt) by (SZ_64K -1). This is so
354 * that in case bcnt over flows, we have ccnt to use.
355 * Note: In A-sync tranfer only, bcntrld is used, but it
356 * only applies for sg_dma_len(sg) >= SZ_64K.
357 * In this case, the best way adopted is- bccnt for the
358 * first frame will be the remainder below. Then for
359 * every successive frame, bcnt will be SZ_64K-1. This
360 * is assured as bcntrld = 0xffff in end of function.
361 */
362 absync = false;
363 ccnt = dma_length / acnt / (SZ_64K - 1);
364 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
365 /*
366 * If bcnt is non-zero, we have a remainder and hence an
367 * extra frame to transfer, so increment ccnt.
368 */
369 if (bcnt)
370 ccnt++;
371 else
372 bcnt = SZ_64K - 1;
373 cidx = acnt;
374 } else {
375 /*
376 * If maxburst is greater than the fifo address_width,
377 * use AB-synced transfers where A count is the fifo
378 * address_width and B count is the maxburst. In this
379 * case, we are limited to transfers of C count frames
380 * of (address_width * maxburst) where C count is limited
381 * to SZ_64K-1. This places an upper bound on the length
382 * of an SG segment that can be handled.
383 */
384 absync = true;
385 bcnt = burst;
386 ccnt = dma_length / (acnt * bcnt);
387 if (ccnt > (SZ_64K - 1)) {
388 dev_err(dev, "Exceeded max SG segment size\n");
389 return -EINVAL;
390 }
391 cidx = acnt * bcnt;
392 }
393
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500394 epset->len = dma_length;
395
Joel Fernandesfd009032013-09-23 18:05:13 -0500396 if (direction == DMA_MEM_TO_DEV) {
397 src_bidx = acnt;
398 src_cidx = cidx;
399 dst_bidx = 0;
400 dst_cidx = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500401 epset->addr = src_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -0500402 } else if (direction == DMA_DEV_TO_MEM) {
403 src_bidx = 0;
404 src_cidx = 0;
405 dst_bidx = acnt;
406 dst_cidx = cidx;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500407 epset->addr = dst_addr;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500408 } else if (direction == DMA_MEM_TO_MEM) {
409 src_bidx = acnt;
410 src_cidx = cidx;
411 dst_bidx = acnt;
412 dst_cidx = cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500413 } else {
414 dev_err(dev, "%s: direction not implemented yet\n", __func__);
415 return -EINVAL;
416 }
417
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500418 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
Joel Fernandesfd009032013-09-23 18:05:13 -0500419 /* Configure A or AB synchronized transfers */
420 if (absync)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500421 param->opt |= SYNCDIM;
Joel Fernandesfd009032013-09-23 18:05:13 -0500422
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500423 param->src = src_addr;
424 param->dst = dst_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -0500425
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500426 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
427 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500428
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500429 param->a_b_cnt = bcnt << 16 | acnt;
430 param->ccnt = ccnt;
Joel Fernandesfd009032013-09-23 18:05:13 -0500431 /*
432 * Only time when (bcntrld) auto reload is required is for
433 * A-sync case, and in this case, a requirement of reload value
434 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
435 * and then later will be populated by edma_execute.
436 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500437 param->link_bcntrld = 0xffffffff;
Joel Fernandesfd009032013-09-23 18:05:13 -0500438 return absync;
439}
440
Matt Porterc2dde5f2012-08-22 21:09:34 -0400441static struct dma_async_tx_descriptor *edma_prep_slave_sg(
442 struct dma_chan *chan, struct scatterlist *sgl,
443 unsigned int sg_len, enum dma_transfer_direction direction,
444 unsigned long tx_flags, void *context)
445{
446 struct edma_chan *echan = to_edma_chan(chan);
447 struct device *dev = chan->device->dev;
448 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -0500449 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -0500450 enum dma_slave_buswidth dev_width;
451 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400452 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -0500453 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400454
455 if (unlikely(!echan || !sgl || !sg_len))
456 return NULL;
457
Matt Porter661f7cb2013-01-10 13:41:04 -0500458 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500459 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500460 dev_width = echan->cfg.src_addr_width;
461 burst = echan->cfg.src_maxburst;
462 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500463 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -0500464 dev_width = echan->cfg.dst_addr_width;
465 burst = echan->cfg.dst_maxburst;
466 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +0300467 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Matt Porter661f7cb2013-01-10 13:41:04 -0500468 return NULL;
469 }
470
471 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300472 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400473 return NULL;
474 }
475
Matt Porterc2dde5f2012-08-22 21:09:34 -0400476 edesc = kzalloc(sizeof(*edesc) + sg_len *
477 sizeof(edesc->pset[0]), GFP_ATOMIC);
478 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300479 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400480 return NULL;
481 }
482
483 edesc->pset_nr = sg_len;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500484 edesc->residue = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500485 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500486 edesc->echan = echan;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400487
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500488 /* Allocate a PaRAM slot, if needed */
489 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
490
491 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -0400492 if (echan->slot[i] < 0) {
493 echan->slot[i] =
494 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
495 EDMA_SLOT_ANY);
496 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +0300497 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +0300498 dev_err(dev, "%s: Failed to allocate slot\n",
499 __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400500 return NULL;
501 }
502 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500503 }
504
505 /* Configure PaRAM sets for each SG */
506 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -0500507 /* Get address for each SG */
508 if (direction == DMA_DEV_TO_MEM)
509 dst_addr = sg_dma_address(sg);
510 else
511 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400512
Joel Fernandesfd009032013-09-23 18:05:13 -0500513 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
514 dst_addr, burst, dev_width,
515 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +0530516 if (ret < 0) {
517 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -0500518 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400519 }
520
Joel Fernandesfd009032013-09-23 18:05:13 -0500521 edesc->absync = ret;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500522 edesc->residue += sg_dma_len(sg);
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500523
524 /* If this is the last in a current SG set of transactions,
525 enable interrupts so that next set is processed */
526 if (!((i+1) % MAX_NR_SG))
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500527 edesc->pset[i].param.opt |= TCINTEN;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -0500528
Matt Porterc2dde5f2012-08-22 21:09:34 -0400529 /* If this is the last set, enable completion interrupt flag */
530 if (i == sg_len - 1)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500531 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400532 }
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500533 edesc->residue_stat = edesc->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400534
Matt Porterc2dde5f2012-08-22 21:09:34 -0400535 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
536}
Matt Porterc2dde5f2012-08-22 21:09:34 -0400537
Lad, Prabhakarb7a4fd52015-02-04 13:03:27 +0000538static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500539 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
540 size_t len, unsigned long tx_flags)
541{
542 int ret;
543 struct edma_desc *edesc;
544 struct device *dev = chan->device->dev;
545 struct edma_chan *echan = to_edma_chan(chan);
546
547 if (unlikely(!echan || !len))
548 return NULL;
549
550 edesc = kzalloc(sizeof(*edesc) + sizeof(edesc->pset[0]), GFP_ATOMIC);
551 if (!edesc) {
552 dev_dbg(dev, "Failed to allocate a descriptor\n");
553 return NULL;
554 }
555
556 edesc->pset_nr = 1;
557
558 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
559 DMA_SLAVE_BUSWIDTH_4_BYTES, len, DMA_MEM_TO_MEM);
560 if (ret < 0)
561 return NULL;
562
563 edesc->absync = ret;
564
565 /*
566 * Enable intermediate transfer chaining to re-trigger channel
567 * on completion of every TR, and enable transfer-completion
568 * interrupt on completion of the whole transfer.
569 */
Joel Fernandesb0cce4c2014-04-28 15:30:32 -0500570 edesc->pset[0].param.opt |= ITCCHEN;
571 edesc->pset[0].param.opt |= TCINTEN;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500572
573 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
574}
575
Joel Fernandes50a9c702013-10-31 16:31:23 -0500576static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
577 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
578 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +0200579 unsigned long tx_flags)
Joel Fernandes50a9c702013-10-31 16:31:23 -0500580{
581 struct edma_chan *echan = to_edma_chan(chan);
582 struct device *dev = chan->device->dev;
583 struct edma_desc *edesc;
584 dma_addr_t src_addr, dst_addr;
585 enum dma_slave_buswidth dev_width;
586 u32 burst;
587 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400588
Joel Fernandes50a9c702013-10-31 16:31:23 -0500589 if (unlikely(!echan || !buf_len || !period_len))
590 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400591
Joel Fernandes50a9c702013-10-31 16:31:23 -0500592 if (direction == DMA_DEV_TO_MEM) {
593 src_addr = echan->cfg.src_addr;
594 dst_addr = buf_addr;
595 dev_width = echan->cfg.src_addr_width;
596 burst = echan->cfg.src_maxburst;
597 } else if (direction == DMA_MEM_TO_DEV) {
598 src_addr = buf_addr;
599 dst_addr = echan->cfg.dst_addr;
600 dev_width = echan->cfg.dst_addr_width;
601 burst = echan->cfg.dst_maxburst;
602 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +0300603 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500604 return NULL;
605 }
606
607 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300608 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500609 return NULL;
610 }
611
612 if (unlikely(buf_len % period_len)) {
613 dev_err(dev, "Period should be multiple of Buffer length\n");
614 return NULL;
615 }
616
617 nslots = (buf_len / period_len) + 1;
618
619 /*
620 * Cyclic DMA users such as audio cannot tolerate delays introduced
621 * by cases where the number of periods is more than the maximum
622 * number of SGs the EDMA driver can handle at a time. For DMA types
623 * such as Slave SGs, such delays are tolerable and synchronized,
624 * but the synchronization is difficult to achieve with Cyclic and
625 * cannot be guaranteed, so we error out early.
626 */
627 if (nslots > MAX_NR_SG)
628 return NULL;
629
630 edesc = kzalloc(sizeof(*edesc) + nslots *
631 sizeof(edesc->pset[0]), GFP_ATOMIC);
632 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +0300633 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500634 return NULL;
635 }
636
637 edesc->cyclic = 1;
638 edesc->pset_nr = nslots;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500639 edesc->residue = edesc->residue_stat = buf_len;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500640 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500641 edesc->echan = echan;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500642
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300643 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
644 __func__, echan->ch_num, nslots, period_len, buf_len);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500645
646 for (i = 0; i < nslots; i++) {
647 /* Allocate a PaRAM slot, if needed */
648 if (echan->slot[i] < 0) {
649 echan->slot[i] =
650 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
651 EDMA_SLOT_ANY);
652 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100653 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +0300654 dev_err(dev, "%s: Failed to allocate slot\n",
655 __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500656 return NULL;
657 }
658 }
659
660 if (i == nslots - 1) {
661 memcpy(&edesc->pset[i], &edesc->pset[0],
662 sizeof(edesc->pset[0]));
663 break;
664 }
665
666 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
667 dst_addr, burst, dev_width, period_len,
668 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100669 if (ret < 0) {
670 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500671 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +0100672 }
Joel Fernandes50a9c702013-10-31 16:31:23 -0500673
674 if (direction == DMA_DEV_TO_MEM)
675 dst_addr += period_len;
676 else
677 src_addr += period_len;
678
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300679 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
680 dev_vdbg(dev,
Joel Fernandes50a9c702013-10-31 16:31:23 -0500681 "\n pset[%d]:\n"
682 " chnum\t%d\n"
683 " slot\t%d\n"
684 " opt\t%08x\n"
685 " src\t%08x\n"
686 " dst\t%08x\n"
687 " abcnt\t%08x\n"
688 " ccnt\t%08x\n"
689 " bidx\t%08x\n"
690 " cidx\t%08x\n"
691 " lkrld\t%08x\n",
692 i, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500693 edesc->pset[i].param.opt,
694 edesc->pset[i].param.src,
695 edesc->pset[i].param.dst,
696 edesc->pset[i].param.a_b_cnt,
697 edesc->pset[i].param.ccnt,
698 edesc->pset[i].param.src_dst_bidx,
699 edesc->pset[i].param.src_dst_cidx,
700 edesc->pset[i].param.link_bcntrld);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500701
702 edesc->absync = ret;
703
704 /*
Peter Ujfalusia1f146f2014-07-16 15:29:21 +0300705 * Enable period interrupt only if it is requested
Joel Fernandes50a9c702013-10-31 16:31:23 -0500706 */
Peter Ujfalusia1f146f2014-07-16 15:29:21 +0300707 if (tx_flags & DMA_PREP_INTERRUPT)
708 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400709 }
710
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +0300711 /* Place the cyclic channel to highest priority queue */
712 edma_assign_channel_eventq(echan->ch_num, EVENTQ_0);
713
Matt Porterc2dde5f2012-08-22 21:09:34 -0400714 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
715}
716
717static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
718{
719 struct edma_chan *echan = data;
720 struct device *dev = echan->vchan.chan.device->dev;
721 struct edma_desc *edesc;
Joel Fernandesc5f47992013-08-29 18:05:43 -0500722 struct edmacc_param p;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400723
Joel Fernandes50a9c702013-10-31 16:31:23 -0500724 edesc = echan->edesc;
725
726 /* Pause the channel for non-cyclic */
727 if (!edesc || (edesc && !edesc->cyclic))
728 edma_pause(echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400729
730 switch (ch_status) {
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530731 case EDMA_DMA_COMPLETE:
Joel Fernandes406efb12014-04-17 00:58:33 -0500732 spin_lock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400733
Matt Porterc2dde5f2012-08-22 21:09:34 -0400734 if (edesc) {
Joel Fernandes50a9c702013-10-31 16:31:23 -0500735 if (edesc->cyclic) {
736 vchan_cyclic_callback(&edesc->vdesc);
737 } else if (edesc->processed == edesc->pset_nr) {
Joel Fernandes53407062013-09-03 10:02:46 -0500738 dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500739 edesc->residue = 0;
Joel Fernandes53407062013-09-03 10:02:46 -0500740 edma_stop(echan->ch_num);
741 vchan_cookie_complete(&edesc->vdesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500742 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500743 } else {
744 dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500745
746 /* Update statistics for tx_status */
747 edesc->residue -= edesc->sg_len;
748 edesc->residue_stat = edesc->residue;
749 edesc->processed_stat = edesc->processed;
750
Joel Fernandes50a9c702013-10-31 16:31:23 -0500751 edma_execute(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500752 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400753 }
754
Joel Fernandes406efb12014-04-17 00:58:33 -0500755 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400756
757 break;
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530758 case EDMA_DMA_CC_ERROR:
Joel Fernandes406efb12014-04-17 00:58:33 -0500759 spin_lock(&echan->vchan.lock);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500760
761 edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
762
763 /*
764 * Issue later based on missed flag which will be sure
765 * to happen as:
766 * (1) we finished transmitting an intermediate slot and
767 * edma_execute is coming up.
768 * (2) or we finished current transfer and issue will
769 * call edma_execute.
770 *
771 * Important note: issuing can be dangerous here and
772 * lead to some nasty recursion when we are in a NULL
773 * slot. So we avoid doing so and set the missed flag.
774 */
775 if (p.a_b_cnt == 0 && p.ccnt == 0) {
776 dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
777 echan->missed = 1;
778 } else {
779 /*
780 * The slot is already programmed but the event got
781 * missed, so its safe to issue it here.
782 */
783 dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
784 edma_clean_channel(echan->ch_num);
785 edma_stop(echan->ch_num);
786 edma_start(echan->ch_num);
787 edma_trigger_channel(echan->ch_num);
788 }
789
Joel Fernandes406efb12014-04-17 00:58:33 -0500790 spin_unlock(&echan->vchan.lock);
Joel Fernandesc5f47992013-08-29 18:05:43 -0500791
Matt Porterc2dde5f2012-08-22 21:09:34 -0400792 break;
793 default:
794 break;
795 }
796}
797
798/* Alloc channel resources */
799static int edma_alloc_chan_resources(struct dma_chan *chan)
800{
801 struct edma_chan *echan = to_edma_chan(chan);
802 struct device *dev = chan->device->dev;
803 int ret;
804 int a_ch_num;
805 LIST_HEAD(descs);
806
807 a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
808 chan, EVENTQ_DEFAULT);
809
810 if (a_ch_num < 0) {
811 ret = -ENODEV;
812 goto err_no_chan;
813 }
814
815 if (a_ch_num != echan->ch_num) {
816 dev_err(dev, "failed to allocate requested channel %u:%u\n",
817 EDMA_CTLR(echan->ch_num),
818 EDMA_CHAN_SLOT(echan->ch_num));
819 ret = -ENODEV;
820 goto err_wrong_chan;
821 }
822
823 echan->alloced = true;
824 echan->slot[0] = echan->ch_num;
825
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300826 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300827 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400828
829 return 0;
830
831err_wrong_chan:
832 edma_free_channel(a_ch_num);
833err_no_chan:
834 return ret;
835}
836
837/* Free channel resources */
838static void edma_free_chan_resources(struct dma_chan *chan)
839{
840 struct edma_chan *echan = to_edma_chan(chan);
841 struct device *dev = chan->device->dev;
842 int i;
843
844 /* Terminate transfers */
845 edma_stop(echan->ch_num);
846
847 vchan_free_chan_resources(&echan->vchan);
848
849 /* Free EDMA PaRAM slots */
850 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
851 if (echan->slot[i] >= 0) {
852 edma_free_slot(echan->slot[i]);
853 echan->slot[i] = -1;
854 }
855 }
856
857 /* Free EDMA channel */
858 if (echan->alloced) {
859 edma_free_channel(echan->ch_num);
860 echan->alloced = false;
861 }
862
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300863 dev_dbg(dev, "freeing channel for %u\n", echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400864}
865
866/* Send pending descriptor to hardware */
867static void edma_issue_pending(struct dma_chan *chan)
868{
869 struct edma_chan *echan = to_edma_chan(chan);
870 unsigned long flags;
871
872 spin_lock_irqsave(&echan->vchan.lock, flags);
873 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
874 edma_execute(echan);
875 spin_unlock_irqrestore(&echan->vchan.lock, flags);
876}
877
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500878static u32 edma_residue(struct edma_desc *edesc)
879{
880 bool dst = edesc->direction == DMA_DEV_TO_MEM;
881 struct edma_pset *pset = edesc->pset;
882 dma_addr_t done, pos;
883 int i;
884
885 /*
886 * We always read the dst/src position from the first RamPar
887 * pset. That's the one which is active now.
888 */
889 pos = edma_get_position(edesc->echan->slot[0], dst);
890
891 /*
892 * Cyclic is simple. Just subtract pset[0].addr from pos.
893 *
894 * We never update edesc->residue in the cyclic case, so we
895 * can tell the remaining room to the end of the circular
896 * buffer.
897 */
898 if (edesc->cyclic) {
899 done = pos - pset->addr;
900 edesc->residue_stat = edesc->residue - done;
901 return edesc->residue_stat;
902 }
903
904 /*
905 * For SG operation we catch up with the last processed
906 * status.
907 */
908 pset += edesc->processed_stat;
909
910 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
911 /*
912 * If we are inside this pset address range, we know
913 * this is the active one. Get the current delta and
914 * stop walking the psets.
915 */
916 if (pos >= pset->addr && pos < pset->addr + pset->len)
917 return edesc->residue_stat - (pos - pset->addr);
918
919 /* Otherwise mark it done and update residue_stat. */
920 edesc->processed_stat++;
921 edesc->residue_stat -= pset->len;
922 }
923 return edesc->residue_stat;
924}
925
Matt Porterc2dde5f2012-08-22 21:09:34 -0400926/* Check request completion status */
927static enum dma_status edma_tx_status(struct dma_chan *chan,
928 dma_cookie_t cookie,
929 struct dma_tx_state *txstate)
930{
931 struct edma_chan *echan = to_edma_chan(chan);
932 struct virt_dma_desc *vdesc;
933 enum dma_status ret;
934 unsigned long flags;
935
936 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +0530937 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400938 return ret;
939
940 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -0500941 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500942 txstate->residue = edma_residue(echan->edesc);
Thomas Gleixnerde135932014-04-28 14:19:51 -0500943 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
944 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400945 spin_unlock_irqrestore(&echan->vchan.lock, flags);
946
947 return ret;
948}
949
950static void __init edma_chan_init(struct edma_cc *ecc,
951 struct dma_device *dma,
952 struct edma_chan *echans)
953{
954 int i, j;
955
956 for (i = 0; i < EDMA_CHANS; i++) {
957 struct edma_chan *echan = &echans[i];
958 echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
959 echan->ecc = ecc;
960 echan->vchan.desc_free = edma_desc_free;
961
962 vchan_init(&echan->vchan, dma);
963
964 INIT_LIST_HEAD(&echan->node);
965 for (j = 0; j < EDMA_MAX_SLOTS; j++)
966 echan->slot[j] = -1;
967 }
968}
969
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +0300970#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
971 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
Peter Ujfalusie4a899d2014-07-03 07:51:56 +0300972 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +0300973 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
974
Matt Porterc2dde5f2012-08-22 21:09:34 -0400975static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
976 struct device *dev)
977{
978 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500979 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500980 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400981 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
982 dma->device_free_chan_resources = edma_free_chan_resources;
983 dma->device_issue_pending = edma_issue_pending;
984 dma->device_tx_status = edma_tx_status;
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100985 dma->device_config = edma_slave_config;
986 dma->device_pause = edma_dma_pause;
987 dma->device_resume = edma_dma_resume;
988 dma->device_terminate_all = edma_terminate_all;
Maxime Ripard9f59cd02014-11-17 14:42:47 +0100989
990 dma->src_addr_widths = EDMA_DMA_BUSWIDTHS;
991 dma->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
992 dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
993 dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
994
Matt Porterc2dde5f2012-08-22 21:09:34 -0400995 dma->dev = dev;
996
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500997 /*
998 * code using dma memcpy must make sure alignment of
999 * length is at dma->copy_align boundary.
1000 */
1001 dma->copy_align = DMA_SLAVE_BUSWIDTH_4_BYTES;
1002
Matt Porterc2dde5f2012-08-22 21:09:34 -04001003 INIT_LIST_HEAD(&dma->channels);
1004}
1005
Bill Pemberton463a1f82012-11-19 13:22:55 -05001006static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001007{
1008 struct edma_cc *ecc;
1009 int ret;
1010
Russell King94cb0e72013-06-27 13:45:16 +01001011 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1012 if (ret)
1013 return ret;
1014
Matt Porterc2dde5f2012-08-22 21:09:34 -04001015 ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
1016 if (!ecc) {
1017 dev_err(&pdev->dev, "Can't allocate controller\n");
1018 return -ENOMEM;
1019 }
1020
1021 ecc->ctlr = pdev->id;
1022 ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
1023 if (ecc->dummy_slot < 0) {
1024 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
Peter Ujfalusi04d537d2014-07-31 13:12:37 +03001025 return ecc->dummy_slot;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001026 }
1027
1028 dma_cap_zero(ecc->dma_slave.cap_mask);
1029 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
Peter Ujfalusi232b223d2014-04-14 14:42:00 +03001030 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001031 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001032
1033 edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
1034
1035 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
1036
1037 ret = dma_async_device_register(&ecc->dma_slave);
1038 if (ret)
1039 goto err_reg1;
1040
1041 platform_set_drvdata(pdev, ecc);
1042
1043 dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
1044
1045 return 0;
1046
1047err_reg1:
1048 edma_free_slot(ecc->dummy_slot);
1049 return ret;
1050}
1051
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001052static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001053{
1054 struct device *dev = &pdev->dev;
1055 struct edma_cc *ecc = dev_get_drvdata(dev);
1056
1057 dma_async_device_unregister(&ecc->dma_slave);
1058 edma_free_slot(ecc->dummy_slot);
1059
1060 return 0;
1061}
1062
1063static struct platform_driver edma_driver = {
1064 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05001065 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -04001066 .driver = {
1067 .name = "edma-dma-engine",
Matt Porterc2dde5f2012-08-22 21:09:34 -04001068 },
1069};
1070
1071bool edma_filter_fn(struct dma_chan *chan, void *param)
1072{
1073 if (chan->device->dev->driver == &edma_driver.driver) {
1074 struct edma_chan *echan = to_edma_chan(chan);
1075 unsigned ch_req = *(unsigned *)param;
1076 return ch_req == echan->ch_num;
1077 }
1078 return false;
1079}
1080EXPORT_SYMBOL(edma_filter_fn);
1081
Matt Porterc2dde5f2012-08-22 21:09:34 -04001082static int edma_init(void)
1083{
Arnd Bergmann5305e4d2014-10-24 18:14:01 +02001084 return platform_driver_register(&edma_driver);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001085}
1086subsys_initcall(edma_init);
1087
1088static void __exit edma_exit(void)
1089{
Matt Porterc2dde5f2012-08-22 21:09:34 -04001090 platform_driver_unregister(&edma_driver);
1091}
1092module_exit(edma_exit);
1093
Josh Boyerd71505b2013-09-04 10:32:50 -04001094MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -04001095MODULE_DESCRIPTION("TI EDMA DMA engine driver");
1096MODULE_LICENSE("GPL v2");