blob: fcb4680efed7b78887767f74aed2c9f66152ddaa [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>
Peter Ujfalusidc9b60552015-10-14 14:42:47 +030028#include <linux/of_dma.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040029
Matt Porter3ad7a422013-03-06 11:15:31 -050030#include <linux/platform_data/edma.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040031
32#include "dmaengine.h"
33#include "virt-dma.h"
34
35/*
36 * This will go away when the private EDMA API is folded
37 * into this driver and the platform device(s) are
38 * instantiated in the arch code. We can only get away
39 * with this simplification because DA8XX may not be built
40 * in the same kernel image with other DaVinci parts. This
41 * avoids having to sprinkle dmaengine driver platform devices
42 * and data throughout all the existing board files.
43 */
44#ifdef CONFIG_ARCH_DAVINCI_DA8XX
45#define EDMA_CTLRS 2
46#define EDMA_CHANS 32
47#else
48#define EDMA_CTLRS 1
49#define EDMA_CHANS 64
50#endif /* CONFIG_ARCH_DAVINCI_DA8XX */
51
Joel Fernandes2abd5f12013-09-23 18:05:15 -050052/*
53 * Max of 20 segments per channel to conserve PaRAM slots
54 * Also note that MAX_NR_SG should be atleast the no.of periods
55 * that are required for ASoC, otherwise DMA prep calls will
56 * fail. Today davinci-pcm is the only user of this driver and
57 * requires atleast 17 slots, so we setup the default to 20.
58 */
59#define MAX_NR_SG 20
Matt Porterc2dde5f2012-08-22 21:09:34 -040060#define EDMA_MAX_SLOTS MAX_NR_SG
61#define EDMA_DESCRIPTORS 16
62
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050063struct edma_pset {
Thomas Gleixnerc2da2342014-04-28 14:29:57 -050064 u32 len;
65 dma_addr_t addr;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -050066 struct edmacc_param param;
67};
68
Matt Porterc2dde5f2012-08-22 21:09:34 -040069struct edma_desc {
70 struct virt_dma_desc vdesc;
71 struct list_head node;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -050072 enum dma_transfer_direction direction;
Joel Fernandes50a9c702013-10-31 16:31:23 -050073 int cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -040074 int absync;
75 int pset_nr;
Thomas Gleixner740b41f2014-04-28 14:34:11 -050076 struct edma_chan *echan;
Joel Fernandes04361d82014-04-28 15:19:31 -050077 int processed;
78
79 /*
80 * The following 4 elements are used for residue accounting.
81 *
82 * - processed_stat: the number of SG elements we have traversed
83 * so far to cover accounting. This is updated directly to processed
84 * during edma_callback and is always <= processed, because processed
85 * refers to the number of pending transfer (programmed to EDMA
86 * controller), where as processed_stat tracks number of transfers
87 * accounted for so far.
88 *
89 * - residue: The amount of bytes we have left to transfer for this desc
90 *
91 * - residue_stat: The residue in bytes of data we have covered
92 * so far for accounting. This is updated directly to residue
93 * during callbacks to keep it current.
94 *
95 * - sg_len: Tracks the length of the current intermediate transfer,
96 * this is required to update the residue during intermediate transfer
97 * completion callback.
98 */
99 int processed_stat;
100 u32 sg_len;
101 u32 residue;
102 u32 residue_stat;
103
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500104 struct edma_pset pset[0];
Matt Porterc2dde5f2012-08-22 21:09:34 -0400105};
106
107struct edma_cc;
108
109struct edma_chan {
110 struct virt_dma_chan vchan;
111 struct list_head node;
112 struct edma_desc *edesc;
113 struct edma_cc *ecc;
114 int ch_num;
115 bool alloced;
116 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -0500117 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -0500118 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400119};
120
121struct edma_cc {
122 int ctlr;
123 struct dma_device dma_slave;
124 struct edma_chan slave_chans[EDMA_CHANS];
125 int num_slave_chans;
126 int dummy_slot;
127};
128
129static inline struct edma_cc *to_edma_cc(struct dma_device *d)
130{
131 return container_of(d, struct edma_cc, dma_slave);
132}
133
134static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
135{
136 return container_of(c, struct edma_chan, vchan.chan);
137}
138
139static inline struct edma_desc
140*to_edma_desc(struct dma_async_tx_descriptor *tx)
141{
142 return container_of(tx, struct edma_desc, vdesc.tx);
143}
144
145static void edma_desc_free(struct virt_dma_desc *vdesc)
146{
147 kfree(container_of(vdesc, struct edma_desc, vdesc));
148}
149
150/* Dispatch a queued descriptor to the controller (caller holds lock) */
151static void edma_execute(struct edma_chan *echan)
152{
Joel Fernandes53407062013-09-03 10:02:46 -0500153 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400154 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500155 struct device *dev = echan->vchan.chan.device->dev;
156 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400157
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300158 if (!echan->edesc) {
159 /* Setup is needed for the first transfer */
Joel Fernandes53407062013-09-03 10:02:46 -0500160 vdesc = vchan_next_desc(&echan->vchan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300161 if (!vdesc)
Joel Fernandes53407062013-09-03 10:02:46 -0500162 return;
Joel Fernandes53407062013-09-03 10:02:46 -0500163 list_del(&vdesc->node);
164 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400165 }
166
Joel Fernandes53407062013-09-03 10:02:46 -0500167 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400168
Joel Fernandes53407062013-09-03 10:02:46 -0500169 /* Find out how many left */
170 left = edesc->pset_nr - edesc->processed;
171 nslots = min(MAX_NR_SG, left);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500172 edesc->sg_len = 0;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400173
174 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500175 for (i = 0; i < nslots; i++) {
176 j = i + edesc->processed;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500177 edma_write_slot(echan->slot[i], &edesc->pset[j].param);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500178 edesc->sg_len += edesc->pset[j].len;
Peter Ujfalusi83bb3122014-04-14 14:42:02 +0300179 dev_vdbg(echan->vchan.chan.device->dev,
Matt Porterc2dde5f2012-08-22 21:09:34 -0400180 "\n pset[%d]:\n"
181 " chnum\t%d\n"
182 " slot\t%d\n"
183 " opt\t%08x\n"
184 " src\t%08x\n"
185 " dst\t%08x\n"
186 " abcnt\t%08x\n"
187 " ccnt\t%08x\n"
188 " bidx\t%08x\n"
189 " cidx\t%08x\n"
190 " lkrld\t%08x\n",
Joel Fernandes53407062013-09-03 10:02:46 -0500191 j, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500192 edesc->pset[j].param.opt,
193 edesc->pset[j].param.src,
194 edesc->pset[j].param.dst,
195 edesc->pset[j].param.a_b_cnt,
196 edesc->pset[j].param.ccnt,
197 edesc->pset[j].param.src_dst_bidx,
198 edesc->pset[j].param.src_dst_cidx,
199 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400200 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500201 if (i != (nslots - 1))
Matt Porterc2dde5f2012-08-22 21:09:34 -0400202 edma_link(echan->slot[i], echan->slot[i+1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400203 }
204
Joel Fernandes53407062013-09-03 10:02:46 -0500205 edesc->processed += nslots;
206
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500207 /*
208 * If this is either the last set in a set of SG-list transactions
209 * then setup a link to the dummy slot, this results in all future
210 * events being absorbed and that's OK because we're done
211 */
Joel Fernandes50a9c702013-10-31 16:31:23 -0500212 if (edesc->processed == edesc->pset_nr) {
213 if (edesc->cyclic)
214 edma_link(echan->slot[nslots-1], echan->slot[1]);
215 else
216 edma_link(echan->slot[nslots-1],
217 echan->ecc->dummy_slot);
218 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500219
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300220 if (echan->missed) {
221 /*
222 * This happens due to setup times between intermediate
223 * transfers in long SG lists which have to be broken up into
224 * transfers of MAX_NR_SG
225 */
226 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
227 edma_clean_channel(echan->ch_num);
228 edma_stop(echan->ch_num);
229 edma_start(echan->ch_num);
230 edma_trigger_channel(echan->ch_num);
231 echan->missed = 0;
232 } else if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300233 dev_dbg(dev, "first transfer starting on channel %d\n",
234 echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500235 edma_start(echan->ch_num);
Sekhar Nori5fc68a62014-03-19 11:25:50 +0530236 } else {
237 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
238 echan->ch_num, edesc->processed);
239 edma_resume(echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -0500240 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400241}
242
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100243static int edma_terminate_all(struct dma_chan *chan)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400244{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100245 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400246 unsigned long flags;
247 LIST_HEAD(head);
248
249 spin_lock_irqsave(&echan->vchan.lock, flags);
250
251 /*
252 * Stop DMA activity: we assume the callback will not be called
253 * after edma_dma() returns (even if it does, it will see
254 * echan->edesc is NULL and exit.)
255 */
256 if (echan->edesc) {
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300257 edma_stop(echan->ch_num);
258 /* Move the cyclic channel back to default queue */
259 if (echan->edesc->cyclic)
260 edma_assign_channel_eventq(echan->ch_num,
261 EVENTQ_DEFAULT);
Petr Kulhavy5ca9e7c2015-03-27 13:35:51 +0200262 /*
263 * free the running request descriptor
264 * since it is not in any of the vdesc lists
265 */
266 edma_desc_free(&echan->edesc->vdesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400267 echan->edesc = NULL;
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
John Ogness02ec6042015-04-27 13:52:25 +0200295 if (!echan->edesc)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300296 return -EINVAL;
297
298 edma_pause(echan->ch_num);
299 return 0;
300}
301
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100302static int edma_dma_resume(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300303{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100304 struct edma_chan *echan = to_edma_chan(chan);
305
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300306 edma_resume(echan->ch_num);
307 return 0;
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
Lad, Prabhakarb7a4fd52015-02-04 13:03:27 +0000532static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500533 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 */
Joel Fernandesb0cce4c2014-04-28 15:30:32 -0500564 edesc->pset[0].param.opt |= ITCCHEN;
565 edesc->pset[0].param.opt |= TCINTEN;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500566
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,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +0200573 unsigned long tx_flags)
Joel Fernandes50a9c702013-10-31 16:31:23 -0500574{
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 /*
Peter Ujfalusia1f146f2014-07-16 15:29:21 +0300699 * Enable period interrupt only if it is requested
Joel Fernandes50a9c702013-10-31 16:31:23 -0500700 */
Peter Ujfalusia1f146f2014-07-16 15:29:21 +0300701 if (tx_flags & DMA_PREP_INTERRUPT)
702 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400703 }
704
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +0300705 /* Place the cyclic channel to highest priority queue */
706 edma_assign_channel_eventq(echan->ch_num, EVENTQ_0);
707
Matt Porterc2dde5f2012-08-22 21:09:34 -0400708 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
709}
710
711static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
712{
713 struct edma_chan *echan = data;
714 struct device *dev = echan->vchan.chan.device->dev;
715 struct edma_desc *edesc;
Joel Fernandesc5f47992013-08-29 18:05:43 -0500716 struct edmacc_param p;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400717
Joel Fernandes50a9c702013-10-31 16:31:23 -0500718 edesc = echan->edesc;
719
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300720 spin_lock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400721 switch (ch_status) {
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530722 case EDMA_DMA_COMPLETE:
Matt Porterc2dde5f2012-08-22 21:09:34 -0400723 if (edesc) {
Joel Fernandes50a9c702013-10-31 16:31:23 -0500724 if (edesc->cyclic) {
725 vchan_cyclic_callback(&edesc->vdesc);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300726 goto out;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500727 } else if (edesc->processed == edesc->pset_nr) {
Joel Fernandes53407062013-09-03 10:02:46 -0500728 dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
Thomas Gleixnerb6205c32014-04-28 14:18:45 -0500729 edesc->residue = 0;
Joel Fernandes53407062013-09-03 10:02:46 -0500730 edma_stop(echan->ch_num);
731 vchan_cookie_complete(&edesc->vdesc);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300732 echan->edesc = NULL;
Joel Fernandes53407062013-09-03 10:02:46 -0500733 } else {
734 dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500735
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300736 edma_pause(echan->ch_num);
737
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500738 /* Update statistics for tx_status */
739 edesc->residue -= edesc->sg_len;
740 edesc->residue_stat = edesc->residue;
741 edesc->processed_stat = edesc->processed;
Joel Fernandes53407062013-09-03 10:02:46 -0500742 }
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300743 edma_execute(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400744 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400745 break;
Vinod Kouldb60d8d2013-10-30 18:22:30 +0530746 case EDMA_DMA_CC_ERROR:
Joel Fernandesc5f47992013-08-29 18:05:43 -0500747 edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
748
749 /*
750 * Issue later based on missed flag which will be sure
751 * to happen as:
752 * (1) we finished transmitting an intermediate slot and
753 * edma_execute is coming up.
754 * (2) or we finished current transfer and issue will
755 * call edma_execute.
756 *
757 * Important note: issuing can be dangerous here and
758 * lead to some nasty recursion when we are in a NULL
759 * slot. So we avoid doing so and set the missed flag.
760 */
761 if (p.a_b_cnt == 0 && p.ccnt == 0) {
762 dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
763 echan->missed = 1;
764 } else {
765 /*
766 * The slot is already programmed but the event got
767 * missed, so its safe to issue it here.
768 */
769 dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
770 edma_clean_channel(echan->ch_num);
771 edma_stop(echan->ch_num);
772 edma_start(echan->ch_num);
773 edma_trigger_channel(echan->ch_num);
774 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400775 break;
776 default:
777 break;
778 }
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300779out:
780 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400781}
782
783/* Alloc channel resources */
784static int edma_alloc_chan_resources(struct dma_chan *chan)
785{
786 struct edma_chan *echan = to_edma_chan(chan);
787 struct device *dev = chan->device->dev;
788 int ret;
789 int a_ch_num;
790 LIST_HEAD(descs);
791
792 a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
Petr Kulhavyab7add32015-03-23 21:35:01 +0100793 echan, EVENTQ_DEFAULT);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400794
795 if (a_ch_num < 0) {
796 ret = -ENODEV;
797 goto err_no_chan;
798 }
799
800 if (a_ch_num != echan->ch_num) {
801 dev_err(dev, "failed to allocate requested channel %u:%u\n",
802 EDMA_CTLR(echan->ch_num),
803 EDMA_CHAN_SLOT(echan->ch_num));
804 ret = -ENODEV;
805 goto err_wrong_chan;
806 }
807
808 echan->alloced = true;
809 echan->slot[0] = echan->ch_num;
810
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300811 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300812 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400813
814 return 0;
815
816err_wrong_chan:
817 edma_free_channel(a_ch_num);
818err_no_chan:
819 return ret;
820}
821
822/* Free channel resources */
823static void edma_free_chan_resources(struct dma_chan *chan)
824{
825 struct edma_chan *echan = to_edma_chan(chan);
826 struct device *dev = chan->device->dev;
827 int i;
828
829 /* Terminate transfers */
830 edma_stop(echan->ch_num);
831
832 vchan_free_chan_resources(&echan->vchan);
833
834 /* Free EDMA PaRAM slots */
835 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
836 if (echan->slot[i] >= 0) {
837 edma_free_slot(echan->slot[i]);
838 echan->slot[i] = -1;
839 }
840 }
841
842 /* Free EDMA channel */
843 if (echan->alloced) {
844 edma_free_channel(echan->ch_num);
845 echan->alloced = false;
846 }
847
Ezequiel Garcia0e772c62013-12-13 11:06:18 -0300848 dev_dbg(dev, "freeing channel for %u\n", echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400849}
850
851/* Send pending descriptor to hardware */
852static void edma_issue_pending(struct dma_chan *chan)
853{
854 struct edma_chan *echan = to_edma_chan(chan);
855 unsigned long flags;
856
857 spin_lock_irqsave(&echan->vchan.lock, flags);
858 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
859 edma_execute(echan);
860 spin_unlock_irqrestore(&echan->vchan.lock, flags);
861}
862
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500863static u32 edma_residue(struct edma_desc *edesc)
864{
865 bool dst = edesc->direction == DMA_DEV_TO_MEM;
866 struct edma_pset *pset = edesc->pset;
867 dma_addr_t done, pos;
868 int i;
869
870 /*
871 * We always read the dst/src position from the first RamPar
872 * pset. That's the one which is active now.
873 */
874 pos = edma_get_position(edesc->echan->slot[0], dst);
875
876 /*
877 * Cyclic is simple. Just subtract pset[0].addr from pos.
878 *
879 * We never update edesc->residue in the cyclic case, so we
880 * can tell the remaining room to the end of the circular
881 * buffer.
882 */
883 if (edesc->cyclic) {
884 done = pos - pset->addr;
885 edesc->residue_stat = edesc->residue - done;
886 return edesc->residue_stat;
887 }
888
889 /*
890 * For SG operation we catch up with the last processed
891 * status.
892 */
893 pset += edesc->processed_stat;
894
895 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
896 /*
897 * If we are inside this pset address range, we know
898 * this is the active one. Get the current delta and
899 * stop walking the psets.
900 */
901 if (pos >= pset->addr && pos < pset->addr + pset->len)
902 return edesc->residue_stat - (pos - pset->addr);
903
904 /* Otherwise mark it done and update residue_stat. */
905 edesc->processed_stat++;
906 edesc->residue_stat -= pset->len;
907 }
908 return edesc->residue_stat;
909}
910
Matt Porterc2dde5f2012-08-22 21:09:34 -0400911/* Check request completion status */
912static enum dma_status edma_tx_status(struct dma_chan *chan,
913 dma_cookie_t cookie,
914 struct dma_tx_state *txstate)
915{
916 struct edma_chan *echan = to_edma_chan(chan);
917 struct virt_dma_desc *vdesc;
918 enum dma_status ret;
919 unsigned long flags;
920
921 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +0530922 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400923 return ret;
924
925 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -0500926 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500927 txstate->residue = edma_residue(echan->edesc);
Thomas Gleixnerde135932014-04-28 14:19:51 -0500928 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
929 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400930 spin_unlock_irqrestore(&echan->vchan.lock, flags);
931
932 return ret;
933}
934
935static void __init edma_chan_init(struct edma_cc *ecc,
936 struct dma_device *dma,
937 struct edma_chan *echans)
938{
939 int i, j;
940
941 for (i = 0; i < EDMA_CHANS; i++) {
942 struct edma_chan *echan = &echans[i];
943 echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
944 echan->ecc = ecc;
945 echan->vchan.desc_free = edma_desc_free;
946
947 vchan_init(&echan->vchan, dma);
948
949 INIT_LIST_HEAD(&echan->node);
950 for (j = 0; j < EDMA_MAX_SLOTS; j++)
951 echan->slot[j] = -1;
952 }
953}
954
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +0300955#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
956 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
Peter Ujfalusie4a899d2014-07-03 07:51:56 +0300957 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +0300958 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
959
Matt Porterc2dde5f2012-08-22 21:09:34 -0400960static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
961 struct device *dev)
962{
963 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500964 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500965 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400966 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
967 dma->device_free_chan_resources = edma_free_chan_resources;
968 dma->device_issue_pending = edma_issue_pending;
969 dma->device_tx_status = edma_tx_status;
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100970 dma->device_config = edma_slave_config;
971 dma->device_pause = edma_dma_pause;
972 dma->device_resume = edma_dma_resume;
973 dma->device_terminate_all = edma_terminate_all;
Maxime Ripard9f59cd02014-11-17 14:42:47 +0100974
975 dma->src_addr_widths = EDMA_DMA_BUSWIDTHS;
976 dma->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
977 dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
978 dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
979
Matt Porterc2dde5f2012-08-22 21:09:34 -0400980 dma->dev = dev;
981
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500982 /*
983 * code using dma memcpy must make sure alignment of
984 * length is at dma->copy_align boundary.
985 */
Maxime Ripard77a68e52015-07-20 10:41:32 +0200986 dma->copy_align = DMAENGINE_ALIGN_4_BYTES;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500987
Matt Porterc2dde5f2012-08-22 21:09:34 -0400988 INIT_LIST_HEAD(&dma->channels);
989}
990
Peter Ujfalusidc9b60552015-10-14 14:42:47 +0300991static struct of_dma_filter_info edma_filter_info = {
992 .filter_fn = edma_filter_fn,
993};
994
Bill Pemberton463a1f82012-11-19 13:22:55 -0500995static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400996{
997 struct edma_cc *ecc;
Peter Ujfalusidc9b60552015-10-14 14:42:47 +0300998 struct device_node *parent_node = pdev->dev.parent->of_node;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400999 int ret;
1000
Russell King94cb0e72013-06-27 13:45:16 +01001001 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1002 if (ret)
1003 return ret;
1004
Matt Porterc2dde5f2012-08-22 21:09:34 -04001005 ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
1006 if (!ecc) {
1007 dev_err(&pdev->dev, "Can't allocate controller\n");
1008 return -ENOMEM;
1009 }
1010
1011 ecc->ctlr = pdev->id;
1012 ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
1013 if (ecc->dummy_slot < 0) {
1014 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
Peter Ujfalusi04d537d2014-07-31 13:12:37 +03001015 return ecc->dummy_slot;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001016 }
1017
1018 dma_cap_zero(ecc->dma_slave.cap_mask);
1019 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
Peter Ujfalusi232b223d2014-04-14 14:42:00 +03001020 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001021 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001022
1023 edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
1024
1025 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
1026
1027 ret = dma_async_device_register(&ecc->dma_slave);
1028 if (ret)
1029 goto err_reg1;
1030
1031 platform_set_drvdata(pdev, ecc);
1032
Peter Ujfalusidc9b60552015-10-14 14:42:47 +03001033 if (parent_node) {
1034 dma_cap_set(DMA_SLAVE, edma_filter_info.dma_cap);
1035 dma_cap_set(DMA_CYCLIC, edma_filter_info.dma_cap);
1036 of_dma_controller_register(parent_node, of_dma_simple_xlate,
1037 &edma_filter_info);
1038 }
1039
Matt Porterc2dde5f2012-08-22 21:09:34 -04001040 dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
1041
1042 return 0;
1043
1044err_reg1:
1045 edma_free_slot(ecc->dummy_slot);
1046 return ret;
1047}
1048
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001049static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001050{
1051 struct device *dev = &pdev->dev;
1052 struct edma_cc *ecc = dev_get_drvdata(dev);
Peter Ujfalusidc9b60552015-10-14 14:42:47 +03001053 struct device_node *parent_node = pdev->dev.parent->of_node;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001054
Peter Ujfalusidc9b60552015-10-14 14:42:47 +03001055 if (parent_node)
1056 of_dma_controller_free(parent_node);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001057 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");