blob: c0165e3d3396412e4bb71779394e7688248af51c [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>
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +030029#include <linux/of_irq.h>
30#include <linux/of_address.h>
31#include <linux/of_device.h>
32#include <linux/pm_runtime.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040033
Matt Porter3ad7a422013-03-06 11:15:31 -050034#include <linux/platform_data/edma.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040035
36#include "dmaengine.h"
37#include "virt-dma.h"
38
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +030039/* Offsets matching "struct edmacc_param" */
40#define PARM_OPT 0x00
41#define PARM_SRC 0x04
42#define PARM_A_B_CNT 0x08
43#define PARM_DST 0x0c
44#define PARM_SRC_DST_BIDX 0x10
45#define PARM_LINK_BCNTRLD 0x14
46#define PARM_SRC_DST_CIDX 0x18
47#define PARM_CCNT 0x1c
48
49#define PARM_SIZE 0x20
50
51/* Offsets for EDMA CC global channel registers and their shadows */
52#define SH_ER 0x00 /* 64 bits */
53#define SH_ECR 0x08 /* 64 bits */
54#define SH_ESR 0x10 /* 64 bits */
55#define SH_CER 0x18 /* 64 bits */
56#define SH_EER 0x20 /* 64 bits */
57#define SH_EECR 0x28 /* 64 bits */
58#define SH_EESR 0x30 /* 64 bits */
59#define SH_SER 0x38 /* 64 bits */
60#define SH_SECR 0x40 /* 64 bits */
61#define SH_IER 0x50 /* 64 bits */
62#define SH_IECR 0x58 /* 64 bits */
63#define SH_IESR 0x60 /* 64 bits */
64#define SH_IPR 0x68 /* 64 bits */
65#define SH_ICR 0x70 /* 64 bits */
66#define SH_IEVAL 0x78
67#define SH_QER 0x80
68#define SH_QEER 0x84
69#define SH_QEECR 0x88
70#define SH_QEESR 0x8c
71#define SH_QSER 0x90
72#define SH_QSECR 0x94
73#define SH_SIZE 0x200
74
75/* Offsets for EDMA CC global registers */
76#define EDMA_REV 0x0000
77#define EDMA_CCCFG 0x0004
78#define EDMA_QCHMAP 0x0200 /* 8 registers */
79#define EDMA_DMAQNUM 0x0240 /* 8 registers (4 on OMAP-L1xx) */
80#define EDMA_QDMAQNUM 0x0260
81#define EDMA_QUETCMAP 0x0280
82#define EDMA_QUEPRI 0x0284
83#define EDMA_EMR 0x0300 /* 64 bits */
84#define EDMA_EMCR 0x0308 /* 64 bits */
85#define EDMA_QEMR 0x0310
86#define EDMA_QEMCR 0x0314
87#define EDMA_CCERR 0x0318
88#define EDMA_CCERRCLR 0x031c
89#define EDMA_EEVAL 0x0320
90#define EDMA_DRAE 0x0340 /* 4 x 64 bits*/
91#define EDMA_QRAE 0x0380 /* 4 registers */
92#define EDMA_QUEEVTENTRY 0x0400 /* 2 x 16 registers */
93#define EDMA_QSTAT 0x0600 /* 2 registers */
94#define EDMA_QWMTHRA 0x0620
95#define EDMA_QWMTHRB 0x0624
96#define EDMA_CCSTAT 0x0640
97
98#define EDMA_M 0x1000 /* global channel registers */
99#define EDMA_ECR 0x1008
100#define EDMA_ECRH 0x100C
101#define EDMA_SHADOW0 0x2000 /* 4 shadow regions */
102#define EDMA_PARM 0x4000 /* PaRAM entries */
103
104#define PARM_OFFSET(param_no) (EDMA_PARM + ((param_no) << 5))
105
106#define EDMA_DCHMAP 0x0100 /* 64 registers */
107
108/* CCCFG register */
109#define GET_NUM_DMACH(x) (x & 0x7) /* bits 0-2 */
110#define GET_NUM_PAENTRY(x) ((x & 0x7000) >> 12) /* bits 12-14 */
111#define GET_NUM_EVQUE(x) ((x & 0x70000) >> 16) /* bits 16-18 */
112#define GET_NUM_REGN(x) ((x & 0x300000) >> 20) /* bits 20-21 */
113#define CHMAP_EXIST BIT(24)
114
Matt Porterc2dde5f2012-08-22 21:09:34 -0400115/*
Joel Fernandes2abd5f12013-09-23 18:05:15 -0500116 * Max of 20 segments per channel to conserve PaRAM slots
117 * Also note that MAX_NR_SG should be atleast the no.of periods
118 * that are required for ASoC, otherwise DMA prep calls will
119 * fail. Today davinci-pcm is the only user of this driver and
120 * requires atleast 17 slots, so we setup the default to 20.
121 */
122#define MAX_NR_SG 20
Matt Porterc2dde5f2012-08-22 21:09:34 -0400123#define EDMA_MAX_SLOTS MAX_NR_SG
124#define EDMA_DESCRIPTORS 16
125
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300126#define EDMA_CHANNEL_ANY -1 /* for edma_alloc_channel() */
127#define EDMA_SLOT_ANY -1 /* for edma_alloc_slot() */
128#define EDMA_CONT_PARAMS_ANY 1001
129#define EDMA_CONT_PARAMS_FIXED_EXACT 1002
130#define EDMA_CONT_PARAMS_FIXED_NOT_EXACT 1003
131
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300132/* PaRAM slots are laid out like this */
133struct edmacc_param {
134 u32 opt;
135 u32 src;
136 u32 a_b_cnt;
137 u32 dst;
138 u32 src_dst_bidx;
139 u32 link_bcntrld;
140 u32 src_dst_cidx;
141 u32 ccnt;
142} __packed;
143
144/* fields in edmacc_param.opt */
145#define SAM BIT(0)
146#define DAM BIT(1)
147#define SYNCDIM BIT(2)
148#define STATIC BIT(3)
149#define EDMA_FWID (0x07 << 8)
150#define TCCMODE BIT(11)
151#define EDMA_TCC(t) ((t) << 12)
152#define TCINTEN BIT(20)
153#define ITCINTEN BIT(21)
154#define TCCHEN BIT(22)
155#define ITCCHEN BIT(23)
156
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500157struct edma_pset {
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500158 u32 len;
159 dma_addr_t addr;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500160 struct edmacc_param param;
161};
162
Matt Porterc2dde5f2012-08-22 21:09:34 -0400163struct edma_desc {
164 struct virt_dma_desc vdesc;
165 struct list_head node;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500166 enum dma_transfer_direction direction;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500167 int cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400168 int absync;
169 int pset_nr;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500170 struct edma_chan *echan;
Joel Fernandes04361d82014-04-28 15:19:31 -0500171 int processed;
172
173 /*
174 * The following 4 elements are used for residue accounting.
175 *
176 * - processed_stat: the number of SG elements we have traversed
177 * so far to cover accounting. This is updated directly to processed
178 * during edma_callback and is always <= processed, because processed
179 * refers to the number of pending transfer (programmed to EDMA
180 * controller), where as processed_stat tracks number of transfers
181 * accounted for so far.
182 *
183 * - residue: The amount of bytes we have left to transfer for this desc
184 *
185 * - residue_stat: The residue in bytes of data we have covered
186 * so far for accounting. This is updated directly to residue
187 * during callbacks to keep it current.
188 *
189 * - sg_len: Tracks the length of the current intermediate transfer,
190 * this is required to update the residue during intermediate transfer
191 * completion callback.
192 */
193 int processed_stat;
194 u32 sg_len;
195 u32 residue;
196 u32 residue_stat;
197
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500198 struct edma_pset pset[0];
Matt Porterc2dde5f2012-08-22 21:09:34 -0400199};
200
201struct edma_cc;
202
203struct edma_chan {
204 struct virt_dma_chan vchan;
205 struct list_head node;
206 struct edma_desc *edesc;
207 struct edma_cc *ecc;
208 int ch_num;
209 bool alloced;
210 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -0500211 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -0500212 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400213};
214
215struct edma_cc {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300216 struct device *dev;
217 struct edma_soc_info *info;
218 void __iomem *base;
219 int id;
220
221 /* eDMA3 resource information */
222 unsigned num_channels;
223 unsigned num_region;
224 unsigned num_slots;
225 unsigned num_tc;
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +0300226 bool chmap_exist;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300227 enum dma_event_q default_queue;
228
229 bool unused_chan_list_done;
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300230 /* The slot_inuse bit for each PaRAM slot is clear unless the
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300231 * channel is in use ... by ARM or DSP, for QDMA, or whatever.
232 */
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300233 unsigned long *slot_inuse;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300234
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300235 /* The channel_unused bit for each channel is clear unless
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300236 * it is not being used on this platform. It uses a bit
237 * of SOC-specific initialization code.
238 */
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300239 unsigned long *channel_unused;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300240
Matt Porterc2dde5f2012-08-22 21:09:34 -0400241 struct dma_device dma_slave;
Peter Ujfalusicb782052015-10-14 14:42:54 +0300242 struct edma_chan *slave_chans;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400243 int dummy_slot;
244};
245
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300246/* dummy param set used to (re)initialize parameter RAM slots */
247static const struct edmacc_param dummy_paramset = {
248 .link_bcntrld = 0xffff,
249 .ccnt = 1,
250};
251
252static const struct of_device_id edma_of_ids[] = {
253 { .compatible = "ti,edma3", },
254 {}
255};
256
257static inline unsigned int edma_read(struct edma_cc *ecc, int offset)
258{
259 return (unsigned int)__raw_readl(ecc->base + offset);
260}
261
262static inline void edma_write(struct edma_cc *ecc, int offset, int val)
263{
264 __raw_writel(val, ecc->base + offset);
265}
266
267static inline void edma_modify(struct edma_cc *ecc, int offset, unsigned and,
268 unsigned or)
269{
270 unsigned val = edma_read(ecc, offset);
271
272 val &= and;
273 val |= or;
274 edma_write(ecc, offset, val);
275}
276
277static inline void edma_and(struct edma_cc *ecc, int offset, unsigned and)
278{
279 unsigned val = edma_read(ecc, offset);
280
281 val &= and;
282 edma_write(ecc, offset, val);
283}
284
285static inline void edma_or(struct edma_cc *ecc, int offset, unsigned or)
286{
287 unsigned val = edma_read(ecc, offset);
288
289 val |= or;
290 edma_write(ecc, offset, val);
291}
292
293static inline unsigned int edma_read_array(struct edma_cc *ecc, int offset,
294 int i)
295{
296 return edma_read(ecc, offset + (i << 2));
297}
298
299static inline void edma_write_array(struct edma_cc *ecc, int offset, int i,
300 unsigned val)
301{
302 edma_write(ecc, offset + (i << 2), val);
303}
304
305static inline void edma_modify_array(struct edma_cc *ecc, int offset, int i,
306 unsigned and, unsigned or)
307{
308 edma_modify(ecc, offset + (i << 2), and, or);
309}
310
311static inline void edma_or_array(struct edma_cc *ecc, int offset, int i,
312 unsigned or)
313{
314 edma_or(ecc, offset + (i << 2), or);
315}
316
317static inline void edma_or_array2(struct edma_cc *ecc, int offset, int i, int j,
318 unsigned or)
319{
320 edma_or(ecc, offset + ((i * 2 + j) << 2), or);
321}
322
323static inline void edma_write_array2(struct edma_cc *ecc, int offset, int i,
324 int j, unsigned val)
325{
326 edma_write(ecc, offset + ((i * 2 + j) << 2), val);
327}
328
329static inline unsigned int edma_shadow0_read(struct edma_cc *ecc, int offset)
330{
331 return edma_read(ecc, EDMA_SHADOW0 + offset);
332}
333
334static inline unsigned int edma_shadow0_read_array(struct edma_cc *ecc,
335 int offset, int i)
336{
337 return edma_read(ecc, EDMA_SHADOW0 + offset + (i << 2));
338}
339
340static inline void edma_shadow0_write(struct edma_cc *ecc, int offset,
341 unsigned val)
342{
343 edma_write(ecc, EDMA_SHADOW0 + offset, val);
344}
345
346static inline void edma_shadow0_write_array(struct edma_cc *ecc, int offset,
347 int i, unsigned val)
348{
349 edma_write(ecc, EDMA_SHADOW0 + offset + (i << 2), val);
350}
351
352static inline unsigned int edma_parm_read(struct edma_cc *ecc, int offset,
353 int param_no)
354{
355 return edma_read(ecc, EDMA_PARM + offset + (param_no << 5));
356}
357
358static inline void edma_parm_write(struct edma_cc *ecc, int offset,
359 int param_no, unsigned val)
360{
361 edma_write(ecc, EDMA_PARM + offset + (param_no << 5), val);
362}
363
364static inline void edma_parm_modify(struct edma_cc *ecc, int offset,
365 int param_no, unsigned and, unsigned or)
366{
367 edma_modify(ecc, EDMA_PARM + offset + (param_no << 5), and, or);
368}
369
370static inline void edma_parm_and(struct edma_cc *ecc, int offset, int param_no,
371 unsigned and)
372{
373 edma_and(ecc, EDMA_PARM + offset + (param_no << 5), and);
374}
375
376static inline void edma_parm_or(struct edma_cc *ecc, int offset, int param_no,
377 unsigned or)
378{
379 edma_or(ecc, EDMA_PARM + offset + (param_no << 5), or);
380}
381
382static inline void set_bits(int offset, int len, unsigned long *p)
383{
384 for (; len > 0; len--)
385 set_bit(offset + (len - 1), p);
386}
387
388static inline void clear_bits(int offset, int len, unsigned long *p)
389{
390 for (; len > 0; len--)
391 clear_bit(offset + (len - 1), p);
392}
393
394static void edma_map_dmach_to_queue(struct edma_cc *ecc, unsigned ch_no,
395 enum dma_event_q queue_no)
396{
397 int bit = (ch_no & 0x7) * 4;
398
399 /* default to low priority queue */
400 if (queue_no == EVENTQ_DEFAULT)
401 queue_no = ecc->default_queue;
402
403 queue_no &= 7;
404 edma_modify_array(ecc, EDMA_DMAQNUM, (ch_no >> 3), ~(0x7 << bit),
405 queue_no << bit);
406}
407
408static void edma_assign_priority_to_queue(struct edma_cc *ecc, int queue_no,
409 int priority)
410{
411 int bit = queue_no * 4;
412
413 edma_modify(ecc, EDMA_QUEPRI, ~(0x7 << bit), ((priority & 0x7) << bit));
414}
415
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300416static void edma_set_chmap(struct edma_cc *ecc, int channel, int slot)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300417{
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300418 if (ecc->chmap_exist) {
419 channel = EDMA_CHAN_SLOT(channel);
420 slot = EDMA_CHAN_SLOT(slot);
421 edma_write_array(ecc, EDMA_DCHMAP, channel, (slot << 5));
422 }
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300423}
424
425static int prepare_unused_channel_list(struct device *dev, void *data)
426{
427 struct platform_device *pdev = to_platform_device(dev);
428 struct edma_cc *ecc = data;
Peter Ujfalusicb782052015-10-14 14:42:54 +0300429 int dma_req_min = EDMA_CTLR_CHAN(ecc->id, 0);
430 int dma_req_max = dma_req_min + ecc->num_channels;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300431 int i, count;
432 struct of_phandle_args dma_spec;
433
434 if (dev->of_node) {
435 struct platform_device *dma_pdev;
436
437 count = of_property_count_strings(dev->of_node, "dma-names");
438 if (count < 0)
439 return 0;
440 for (i = 0; i < count; i++) {
441 if (of_parse_phandle_with_args(dev->of_node, "dmas",
442 "#dma-cells", i,
443 &dma_spec))
444 continue;
445
446 if (!of_match_node(edma_of_ids, dma_spec.np)) {
447 of_node_put(dma_spec.np);
448 continue;
449 }
450
451 dma_pdev = of_find_device_by_node(dma_spec.np);
452 if (&dma_pdev->dev != ecc->dev)
453 continue;
454
455 clear_bit(EDMA_CHAN_SLOT(dma_spec.args[0]),
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300456 ecc->channel_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300457 of_node_put(dma_spec.np);
458 }
459 return 0;
460 }
461
462 /* For non-OF case */
463 for (i = 0; i < pdev->num_resources; i++) {
464 struct resource *res = &pdev->resource[i];
Peter Ujfalusicb782052015-10-14 14:42:54 +0300465 int dma_req;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300466
Peter Ujfalusicb782052015-10-14 14:42:54 +0300467 if (!(res->flags & IORESOURCE_DMA))
468 continue;
469
470 dma_req = (int)res->start;
471 if (dma_req >= dma_req_min && dma_req < dma_req_max)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300472 clear_bit(EDMA_CHAN_SLOT(pdev->resource[i].start),
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300473 ecc->channel_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300474 }
475
476 return 0;
477}
478
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300479static void edma_setup_interrupt(struct edma_cc *ecc, unsigned lch, bool enable)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300480{
481 lch = EDMA_CHAN_SLOT(lch);
482
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300483 if (enable) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300484 edma_shadow0_write_array(ecc, SH_ICR, lch >> 5,
485 BIT(lch & 0x1f));
486 edma_shadow0_write_array(ecc, SH_IESR, lch >> 5,
487 BIT(lch & 0x1f));
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300488 } else {
489 edma_shadow0_write_array(ecc, SH_IECR, lch >> 5,
490 BIT(lch & 0x1f));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300491 }
492}
493
494/*
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300495 * paRAM slot management functions
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300496 */
497static void edma_write_slot(struct edma_cc *ecc, unsigned slot,
498 const struct edmacc_param *param)
499{
500 slot = EDMA_CHAN_SLOT(slot);
501 if (slot >= ecc->num_slots)
502 return;
503 memcpy_toio(ecc->base + PARM_OFFSET(slot), param, PARM_SIZE);
504}
505
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300506static void edma_read_slot(struct edma_cc *ecc, unsigned slot,
507 struct edmacc_param *param)
508{
509 slot = EDMA_CHAN_SLOT(slot);
510 if (slot >= ecc->num_slots)
511 return;
512 memcpy_fromio(param, ecc->base + PARM_OFFSET(slot), PARM_SIZE);
513}
514
515/**
516 * edma_alloc_slot - allocate DMA parameter RAM
517 * @ecc: pointer to edma_cc struct
518 * @slot: specific slot to allocate; negative for "any unused slot"
519 *
520 * This allocates a parameter RAM slot, initializing it to hold a
521 * dummy transfer. Slots allocated using this routine have not been
522 * mapped to a hardware DMA channel, and will normally be used by
523 * linking to them from a slot associated with a DMA channel.
524 *
525 * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
526 * slots may be allocated on behalf of DSP firmware.
527 *
528 * Returns the number of the slot, else negative errno.
529 */
530static int edma_alloc_slot(struct edma_cc *ecc, int slot)
531{
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300532 if (slot > 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300533 slot = EDMA_CHAN_SLOT(slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300534 /* Requesting entry paRAM slot for a HW triggered channel. */
535 if (ecc->chmap_exist && slot < ecc->num_channels)
536 slot = EDMA_SLOT_ANY;
537 }
538
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300539 if (slot < 0) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300540 if (ecc->chmap_exist)
541 slot = 0;
542 else
543 slot = ecc->num_channels;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300544 for (;;) {
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300545 slot = find_next_zero_bit(ecc->slot_inuse,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300546 ecc->num_slots,
547 slot);
548 if (slot == ecc->num_slots)
549 return -ENOMEM;
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300550 if (!test_and_set_bit(slot, ecc->slot_inuse))
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300551 break;
552 }
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300553 } else if (slot >= ecc->num_slots) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300554 return -EINVAL;
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300555 } else if (test_and_set_bit(slot, ecc->slot_inuse)) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300556 return -EBUSY;
557 }
558
559 edma_write_slot(ecc, slot, &dummy_paramset);
560
561 return EDMA_CTLR_CHAN(ecc->id, slot);
562}
563
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300564static void edma_free_slot(struct edma_cc *ecc, unsigned slot)
565{
566 slot = EDMA_CHAN_SLOT(slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300567 if (slot >= ecc->num_slots)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300568 return;
569
570 edma_write_slot(ecc, slot, &dummy_paramset);
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300571 clear_bit(slot, ecc->slot_inuse);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300572}
573
574/**
575 * edma_link - link one parameter RAM slot to another
576 * @ecc: pointer to edma_cc struct
577 * @from: parameter RAM slot originating the link
578 * @to: parameter RAM slot which is the link target
579 *
580 * The originating slot should not be part of any active DMA transfer.
581 */
582static void edma_link(struct edma_cc *ecc, unsigned from, unsigned to)
583{
Peter Ujfalusifc014092015-10-14 14:42:59 +0300584 if (unlikely(EDMA_CTLR(from) != EDMA_CTLR(to)))
585 dev_warn(ecc->dev, "Ignoring eDMA instance for linking\n");
586
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300587 from = EDMA_CHAN_SLOT(from);
588 to = EDMA_CHAN_SLOT(to);
589 if (from >= ecc->num_slots || to >= ecc->num_slots)
590 return;
591
592 edma_parm_modify(ecc, PARM_LINK_BCNTRLD, from, 0xffff0000,
593 PARM_OFFSET(to));
594}
595
596/**
597 * edma_get_position - returns the current transfer point
598 * @ecc: pointer to edma_cc struct
599 * @slot: parameter RAM slot being examined
600 * @dst: true selects the dest position, false the source
601 *
602 * Returns the position of the current active slot
603 */
604static dma_addr_t edma_get_position(struct edma_cc *ecc, unsigned slot,
605 bool dst)
606{
607 u32 offs;
608
609 slot = EDMA_CHAN_SLOT(slot);
610 offs = PARM_OFFSET(slot);
611 offs += dst ? PARM_DST : PARM_SRC;
612
613 return edma_read(ecc, offs);
614}
615
616/*-----------------------------------------------------------------------*/
617/**
618 * edma_start - start dma on a channel
619 * @ecc: pointer to edma_cc struct
620 * @channel: channel being activated
621 *
622 * Channels with event associations will be triggered by their hardware
623 * events, and channels without such associations will be triggered by
624 * software. (At this writing there is no interface for using software
625 * triggers except with channels that don't support hardware triggers.)
626 *
627 * Returns zero on success, else negative errno.
628 */
629static int edma_start(struct edma_cc *ecc, unsigned channel)
630{
631 if (ecc->id != EDMA_CTLR(channel)) {
632 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
633 ecc->id, EDMA_CTLR(channel));
634 return -EINVAL;
635 }
636 channel = EDMA_CHAN_SLOT(channel);
637
638 if (channel < ecc->num_channels) {
639 int j = channel >> 5;
640 unsigned int mask = BIT(channel & 0x1f);
641
642 /* EDMA channels without event association */
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300643 if (test_bit(channel, ecc->channel_unused)) {
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300644 dev_dbg(ecc->dev, "ESR%d %08x\n", j,
645 edma_shadow0_read_array(ecc, SH_ESR, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300646 edma_shadow0_write_array(ecc, SH_ESR, j, mask);
647 return 0;
648 }
649
650 /* EDMA channel with event association */
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300651 dev_dbg(ecc->dev, "ER%d %08x\n", j,
652 edma_shadow0_read_array(ecc, SH_ER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300653 /* Clear any pending event or error */
654 edma_write_array(ecc, EDMA_ECR, j, mask);
655 edma_write_array(ecc, EDMA_EMCR, j, mask);
656 /* Clear any SER */
657 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
658 edma_shadow0_write_array(ecc, SH_EESR, j, mask);
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300659 dev_dbg(ecc->dev, "EER%d %08x\n", j,
660 edma_shadow0_read_array(ecc, SH_EER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300661 return 0;
662 }
663
664 return -EINVAL;
665}
666
667/**
668 * edma_stop - stops dma on the channel passed
669 * @ecc: pointer to edma_cc struct
670 * @channel: channel being deactivated
671 *
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300672 * Any active transfer is paused and all pending hardware events are cleared.
673 * The current transfer may not be resumed, and the channel's Parameter RAM
674 * should be reinitialized before being reused.
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300675 */
676static void edma_stop(struct edma_cc *ecc, unsigned channel)
677{
678 if (ecc->id != EDMA_CTLR(channel)) {
679 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
680 ecc->id, EDMA_CTLR(channel));
681 return;
682 }
683 channel = EDMA_CHAN_SLOT(channel);
684
685 if (channel < ecc->num_channels) {
686 int j = channel >> 5;
687 unsigned int mask = BIT(channel & 0x1f);
688
689 edma_shadow0_write_array(ecc, SH_EECR, j, mask);
690 edma_shadow0_write_array(ecc, SH_ECR, j, mask);
691 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
692 edma_write_array(ecc, EDMA_EMCR, j, mask);
693
694 /* clear possibly pending completion interrupt */
695 edma_shadow0_write_array(ecc, SH_ICR, j, mask);
696
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300697 dev_dbg(ecc->dev, "EER%d %08x\n", j,
698 edma_shadow0_read_array(ecc, SH_EER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300699
700 /* REVISIT: consider guarding against inappropriate event
701 * chaining by overwriting with dummy_paramset.
702 */
703 }
704}
705
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300706/*
707 * Temporarily disable EDMA hardware events on the specified channel,
708 * preventing them from triggering new transfers
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300709 */
710static void edma_pause(struct edma_cc *ecc, unsigned channel)
711{
712 if (ecc->id != EDMA_CTLR(channel)) {
713 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
714 ecc->id, EDMA_CTLR(channel));
715 return;
716 }
717 channel = EDMA_CHAN_SLOT(channel);
718
719 if (channel < ecc->num_channels) {
720 unsigned int mask = BIT(channel & 0x1f);
721
722 edma_shadow0_write_array(ecc, SH_EECR, channel >> 5, mask);
723 }
724}
725
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300726/* Re-enable EDMA hardware events on the specified channel. */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300727static void edma_resume(struct edma_cc *ecc, unsigned channel)
728{
729 if (ecc->id != EDMA_CTLR(channel)) {
730 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
731 ecc->id, EDMA_CTLR(channel));
732 return;
733 }
734 channel = EDMA_CHAN_SLOT(channel);
735
736 if (channel < ecc->num_channels) {
737 unsigned int mask = BIT(channel & 0x1f);
738
739 edma_shadow0_write_array(ecc, SH_EESR, channel >> 5, mask);
740 }
741}
742
743static int edma_trigger_channel(struct edma_cc *ecc, unsigned channel)
744{
745 unsigned int mask;
746
747 if (ecc->id != EDMA_CTLR(channel)) {
748 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
749 ecc->id, EDMA_CTLR(channel));
750 return -EINVAL;
751 }
752 channel = EDMA_CHAN_SLOT(channel);
753 mask = BIT(channel & 0x1f);
754
755 edma_shadow0_write_array(ecc, SH_ESR, (channel >> 5), mask);
756
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300757 dev_dbg(ecc->dev, "ESR%d %08x\n", (channel >> 5),
758 edma_shadow0_read_array(ecc, SH_ESR, (channel >> 5)));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300759 return 0;
760}
761
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300762static void edma_clean_channel(struct edma_cc *ecc, unsigned channel)
763{
764 if (ecc->id != EDMA_CTLR(channel)) {
765 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
766 ecc->id, EDMA_CTLR(channel));
767 return;
768 }
769 channel = EDMA_CHAN_SLOT(channel);
770
771 if (channel < ecc->num_channels) {
772 int j = (channel >> 5);
773 unsigned int mask = BIT(channel & 0x1f);
774
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300775 dev_dbg(ecc->dev, "EMR%d %08x\n", j,
776 edma_read_array(ecc, EDMA_EMR, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300777 edma_shadow0_write_array(ecc, SH_ECR, j, mask);
778 /* Clear the corresponding EMR bits */
779 edma_write_array(ecc, EDMA_EMCR, j, mask);
780 /* Clear any SER */
781 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
782 edma_write(ecc, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
783 }
784}
785
786/**
787 * edma_alloc_channel - allocate DMA channel and paired parameter RAM
788 * @ecc: pointer to edma_cc struct
789 * @channel: specific channel to allocate; negative for "any unmapped channel"
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300790 * @eventq_no: an EVENTQ_* constant, used to choose which Transfer
791 * Controller (TC) executes requests using this channel. Use
792 * EVENTQ_DEFAULT unless you really need a high priority queue.
793 *
794 * This allocates a DMA channel and its associated parameter RAM slot.
795 * The parameter RAM is initialized to hold a dummy transfer.
796 *
797 * Normal use is to pass a specific channel number as @channel, to make
798 * use of hardware events mapped to that channel. When the channel will
799 * be used only for software triggering or event chaining, channels not
800 * mapped to hardware events (or mapped to unused events) are preferable.
801 *
802 * DMA transfers start from a channel using edma_start(), or by
803 * chaining. When the transfer described in that channel's parameter RAM
804 * slot completes, that slot's data may be reloaded through a link.
805 *
806 * DMA errors are only reported to the @callback associated with the
807 * channel driving that transfer, but transfer completion callbacks can
808 * be sent to another channel under control of the TCC field in
809 * the option word of the transfer's parameter RAM set. Drivers must not
810 * use DMA transfer completion callbacks for channels they did not allocate.
811 * (The same applies to TCC codes used in transfer chaining.)
812 *
813 * Returns the number of the channel, else negative errno.
814 */
815static int edma_alloc_channel(struct edma_cc *ecc, int channel,
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300816 enum dma_event_q eventq_no)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300817{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300818 int ret = 0;
819
820 if (!ecc->unused_chan_list_done) {
821 /*
822 * Scan all the platform devices to find out the EDMA channels
823 * used and clear them in the unused list, making the rest
824 * available for ARM usage.
825 */
826 ret = bus_for_each_dev(&platform_bus_type, NULL, ecc,
827 prepare_unused_channel_list);
828 if (ret < 0)
829 return ret;
830
831 ecc->unused_chan_list_done = true;
832 }
833
834 if (channel >= 0) {
835 if (ecc->id != EDMA_CTLR(channel)) {
836 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n",
837 __func__, ecc->id, EDMA_CTLR(channel));
838 return -EINVAL;
839 }
840 channel = EDMA_CHAN_SLOT(channel);
841 }
842
843 if (channel < 0) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300844 channel = find_next_bit(ecc->channel_unused, ecc->num_channels,
845 0);
846 if (channel == ecc->num_channels)
847 return -EBUSY;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300848 } else if (channel >= ecc->num_channels) {
849 return -EINVAL;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300850 }
851
852 /* ensure access through shadow region 0 */
853 edma_or_array2(ecc, EDMA_DRAE, 0, channel >> 5, BIT(channel & 0x1f));
854
855 /* ensure no events are pending */
856 edma_stop(ecc, EDMA_CTLR_CHAN(ecc->id, channel));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300857
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300858 edma_setup_interrupt(ecc, EDMA_CTLR_CHAN(ecc->id, channel), true);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300859
860 edma_map_dmach_to_queue(ecc, channel, eventq_no);
861
862 return EDMA_CTLR_CHAN(ecc->id, channel);
863}
864
865/**
866 * edma_free_channel - deallocate DMA channel
867 * @ecc: pointer to edma_cc struct
868 * @channel: dma channel returned from edma_alloc_channel()
869 *
870 * This deallocates the DMA channel and associated parameter RAM slot
871 * allocated by edma_alloc_channel().
872 *
873 * Callers are responsible for ensuring the channel is inactive, and
874 * will not be reactivated by linking, chaining, or software calls to
875 * edma_start().
876 */
877static void edma_free_channel(struct edma_cc *ecc, unsigned channel)
878{
879 if (ecc->id != EDMA_CTLR(channel)) {
880 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
881 ecc->id, EDMA_CTLR(channel));
882 return;
883 }
884 channel = EDMA_CHAN_SLOT(channel);
885
886 if (channel >= ecc->num_channels)
887 return;
888
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300889 /* REVISIT should probably take out of shadow region 0 */
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300890 edma_setup_interrupt(ecc, channel, false);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300891}
892
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300893/* Move channel to a specific event queue */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300894static void edma_assign_channel_eventq(struct edma_cc *ecc, unsigned channel,
895 enum dma_event_q eventq_no)
896{
897 if (ecc->id != EDMA_CTLR(channel)) {
898 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
899 ecc->id, EDMA_CTLR(channel));
900 return;
901 }
902 channel = EDMA_CHAN_SLOT(channel);
903
904 if (channel >= ecc->num_channels)
905 return;
906
907 /* default to low priority queue */
908 if (eventq_no == EVENTQ_DEFAULT)
909 eventq_no = ecc->default_queue;
910 if (eventq_no >= ecc->num_tc)
911 return;
912
913 edma_map_dmach_to_queue(ecc, channel, eventq_no);
914}
915
Matt Porterc2dde5f2012-08-22 21:09:34 -0400916static inline struct edma_cc *to_edma_cc(struct dma_device *d)
917{
918 return container_of(d, struct edma_cc, dma_slave);
919}
920
921static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
922{
923 return container_of(c, struct edma_chan, vchan.chan);
924}
925
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300926static inline struct edma_desc *to_edma_desc(struct dma_async_tx_descriptor *tx)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400927{
928 return container_of(tx, struct edma_desc, vdesc.tx);
929}
930
931static void edma_desc_free(struct virt_dma_desc *vdesc)
932{
933 kfree(container_of(vdesc, struct edma_desc, vdesc));
934}
935
936/* Dispatch a queued descriptor to the controller (caller holds lock) */
937static void edma_execute(struct edma_chan *echan)
938{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300939 struct edma_cc *ecc = echan->ecc;
Joel Fernandes53407062013-09-03 10:02:46 -0500940 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400941 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500942 struct device *dev = echan->vchan.chan.device->dev;
943 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400944
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300945 if (!echan->edesc) {
946 /* Setup is needed for the first transfer */
Joel Fernandes53407062013-09-03 10:02:46 -0500947 vdesc = vchan_next_desc(&echan->vchan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300948 if (!vdesc)
Joel Fernandes53407062013-09-03 10:02:46 -0500949 return;
Joel Fernandes53407062013-09-03 10:02:46 -0500950 list_del(&vdesc->node);
951 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400952 }
953
Joel Fernandes53407062013-09-03 10:02:46 -0500954 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400955
Joel Fernandes53407062013-09-03 10:02:46 -0500956 /* Find out how many left */
957 left = edesc->pset_nr - edesc->processed;
958 nslots = min(MAX_NR_SG, left);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500959 edesc->sg_len = 0;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400960
961 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500962 for (i = 0; i < nslots; i++) {
963 j = i + edesc->processed;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300964 edma_write_slot(ecc, echan->slot[i], &edesc->pset[j].param);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500965 edesc->sg_len += edesc->pset[j].len;
Peter Ujfalusi907f74a2015-10-14 14:42:56 +0300966 dev_vdbg(dev,
967 "\n pset[%d]:\n"
968 " chnum\t%d\n"
969 " slot\t%d\n"
970 " opt\t%08x\n"
971 " src\t%08x\n"
972 " dst\t%08x\n"
973 " abcnt\t%08x\n"
974 " ccnt\t%08x\n"
975 " bidx\t%08x\n"
976 " cidx\t%08x\n"
977 " lkrld\t%08x\n",
978 j, echan->ch_num, echan->slot[i],
979 edesc->pset[j].param.opt,
980 edesc->pset[j].param.src,
981 edesc->pset[j].param.dst,
982 edesc->pset[j].param.a_b_cnt,
983 edesc->pset[j].param.ccnt,
984 edesc->pset[j].param.src_dst_bidx,
985 edesc->pset[j].param.src_dst_cidx,
986 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400987 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500988 if (i != (nslots - 1))
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300989 edma_link(ecc, echan->slot[i], echan->slot[i + 1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400990 }
991
Joel Fernandes53407062013-09-03 10:02:46 -0500992 edesc->processed += nslots;
993
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500994 /*
995 * If this is either the last set in a set of SG-list transactions
996 * then setup a link to the dummy slot, this results in all future
997 * events being absorbed and that's OK because we're done
998 */
Joel Fernandes50a9c702013-10-31 16:31:23 -0500999 if (edesc->processed == edesc->pset_nr) {
1000 if (edesc->cyclic)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001001 edma_link(ecc, echan->slot[nslots - 1], echan->slot[1]);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001002 else
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001003 edma_link(ecc, echan->slot[nslots - 1],
Joel Fernandes50a9c702013-10-31 16:31:23 -05001004 echan->ecc->dummy_slot);
1005 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -05001006
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001007 if (echan->missed) {
1008 /*
1009 * This happens due to setup times between intermediate
1010 * transfers in long SG lists which have to be broken up into
1011 * transfers of MAX_NR_SG
1012 */
1013 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001014 edma_clean_channel(ecc, echan->ch_num);
1015 edma_stop(ecc, echan->ch_num);
1016 edma_start(ecc, echan->ch_num);
1017 edma_trigger_channel(ecc, echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001018 echan->missed = 0;
1019 } else if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +03001020 dev_dbg(dev, "first transfer starting on channel %d\n",
1021 echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001022 edma_start(ecc, echan->ch_num);
Sekhar Nori5fc68a62014-03-19 11:25:50 +05301023 } else {
1024 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
1025 echan->ch_num, edesc->processed);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001026 edma_resume(ecc, echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -05001027 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001028}
1029
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001030static int edma_terminate_all(struct dma_chan *chan)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001031{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001032 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001033 unsigned long flags;
1034 LIST_HEAD(head);
1035
1036 spin_lock_irqsave(&echan->vchan.lock, flags);
1037
1038 /*
1039 * Stop DMA activity: we assume the callback will not be called
1040 * after edma_dma() returns (even if it does, it will see
1041 * echan->edesc is NULL and exit.)
1042 */
1043 if (echan->edesc) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001044 edma_stop(echan->ecc, echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001045 /* Move the cyclic channel back to default queue */
1046 if (echan->edesc->cyclic)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001047 edma_assign_channel_eventq(echan->ecc, echan->ch_num,
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001048 EVENTQ_DEFAULT);
Petr Kulhavy5ca9e7c2015-03-27 13:35:51 +02001049 /*
1050 * free the running request descriptor
1051 * since it is not in any of the vdesc lists
1052 */
1053 edma_desc_free(&echan->edesc->vdesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001054 echan->edesc = NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001055 }
1056
1057 vchan_get_all_descriptors(&echan->vchan, &head);
1058 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1059 vchan_dma_desc_free_list(&echan->vchan, &head);
1060
1061 return 0;
1062}
1063
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001064static int edma_slave_config(struct dma_chan *chan,
Matt Porter661f7cb2013-01-10 13:41:04 -05001065 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001066{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001067 struct edma_chan *echan = to_edma_chan(chan);
1068
Matt Porter661f7cb2013-01-10 13:41:04 -05001069 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
1070 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001071 return -EINVAL;
1072
Matt Porter661f7cb2013-01-10 13:41:04 -05001073 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -04001074
1075 return 0;
1076}
1077
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001078static int edma_dma_pause(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001079{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001080 struct edma_chan *echan = to_edma_chan(chan);
1081
John Ogness02ec6042015-04-27 13:52:25 +02001082 if (!echan->edesc)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001083 return -EINVAL;
1084
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001085 edma_pause(echan->ecc, echan->ch_num);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001086 return 0;
1087}
1088
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001089static int edma_dma_resume(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001090{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001091 struct edma_chan *echan = to_edma_chan(chan);
1092
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001093 edma_resume(echan->ecc, echan->ch_num);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001094 return 0;
1095}
1096
Joel Fernandesfd009032013-09-23 18:05:13 -05001097/*
1098 * A PaRAM set configuration abstraction used by other modes
1099 * @chan: Channel who's PaRAM set we're configuring
1100 * @pset: PaRAM set to initialize and setup.
1101 * @src_addr: Source address of the DMA
1102 * @dst_addr: Destination address of the DMA
1103 * @burst: In units of dev_width, how much to send
1104 * @dev_width: How much is the dev_width
1105 * @dma_length: Total length of the DMA transfer
1106 * @direction: Direction of the transfer
1107 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001108static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001109 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001110 unsigned int acnt, unsigned int dma_length,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001111 enum dma_transfer_direction direction)
Joel Fernandesfd009032013-09-23 18:05:13 -05001112{
1113 struct edma_chan *echan = to_edma_chan(chan);
1114 struct device *dev = chan->device->dev;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001115 struct edmacc_param *param = &epset->param;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001116 int bcnt, ccnt, cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001117 int src_bidx, dst_bidx, src_cidx, dst_cidx;
1118 int absync;
1119
Peter Ujfalusib2b617d2014-04-14 14:41:58 +03001120 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
1121 if (!burst)
1122 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -05001123 /*
1124 * If the maxburst is equal to the fifo width, use
1125 * A-synced transfers. This allows for large contiguous
1126 * buffer transfers using only one PaRAM set.
1127 */
1128 if (burst == 1) {
1129 /*
1130 * For the A-sync case, bcnt and ccnt are the remainder
1131 * and quotient respectively of the division of:
1132 * (dma_length / acnt) by (SZ_64K -1). This is so
1133 * that in case bcnt over flows, we have ccnt to use.
1134 * Note: In A-sync tranfer only, bcntrld is used, but it
1135 * only applies for sg_dma_len(sg) >= SZ_64K.
1136 * In this case, the best way adopted is- bccnt for the
1137 * first frame will be the remainder below. Then for
1138 * every successive frame, bcnt will be SZ_64K-1. This
1139 * is assured as bcntrld = 0xffff in end of function.
1140 */
1141 absync = false;
1142 ccnt = dma_length / acnt / (SZ_64K - 1);
1143 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
1144 /*
1145 * If bcnt is non-zero, we have a remainder and hence an
1146 * extra frame to transfer, so increment ccnt.
1147 */
1148 if (bcnt)
1149 ccnt++;
1150 else
1151 bcnt = SZ_64K - 1;
1152 cidx = acnt;
1153 } else {
1154 /*
1155 * If maxburst is greater than the fifo address_width,
1156 * use AB-synced transfers where A count is the fifo
1157 * address_width and B count is the maxburst. In this
1158 * case, we are limited to transfers of C count frames
1159 * of (address_width * maxburst) where C count is limited
1160 * to SZ_64K-1. This places an upper bound on the length
1161 * of an SG segment that can be handled.
1162 */
1163 absync = true;
1164 bcnt = burst;
1165 ccnt = dma_length / (acnt * bcnt);
1166 if (ccnt > (SZ_64K - 1)) {
1167 dev_err(dev, "Exceeded max SG segment size\n");
1168 return -EINVAL;
1169 }
1170 cidx = acnt * bcnt;
1171 }
1172
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001173 epset->len = dma_length;
1174
Joel Fernandesfd009032013-09-23 18:05:13 -05001175 if (direction == DMA_MEM_TO_DEV) {
1176 src_bidx = acnt;
1177 src_cidx = cidx;
1178 dst_bidx = 0;
1179 dst_cidx = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001180 epset->addr = src_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001181 } else if (direction == DMA_DEV_TO_MEM) {
1182 src_bidx = 0;
1183 src_cidx = 0;
1184 dst_bidx = acnt;
1185 dst_cidx = cidx;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001186 epset->addr = dst_addr;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001187 } else if (direction == DMA_MEM_TO_MEM) {
1188 src_bidx = acnt;
1189 src_cidx = cidx;
1190 dst_bidx = acnt;
1191 dst_cidx = cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001192 } else {
1193 dev_err(dev, "%s: direction not implemented yet\n", __func__);
1194 return -EINVAL;
1195 }
1196
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001197 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
Joel Fernandesfd009032013-09-23 18:05:13 -05001198 /* Configure A or AB synchronized transfers */
1199 if (absync)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001200 param->opt |= SYNCDIM;
Joel Fernandesfd009032013-09-23 18:05:13 -05001201
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001202 param->src = src_addr;
1203 param->dst = dst_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001204
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001205 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
1206 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001207
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001208 param->a_b_cnt = bcnt << 16 | acnt;
1209 param->ccnt = ccnt;
Joel Fernandesfd009032013-09-23 18:05:13 -05001210 /*
1211 * Only time when (bcntrld) auto reload is required is for
1212 * A-sync case, and in this case, a requirement of reload value
1213 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
1214 * and then later will be populated by edma_execute.
1215 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001216 param->link_bcntrld = 0xffffffff;
Joel Fernandesfd009032013-09-23 18:05:13 -05001217 return absync;
1218}
1219
Matt Porterc2dde5f2012-08-22 21:09:34 -04001220static struct dma_async_tx_descriptor *edma_prep_slave_sg(
1221 struct dma_chan *chan, struct scatterlist *sgl,
1222 unsigned int sg_len, enum dma_transfer_direction direction,
1223 unsigned long tx_flags, void *context)
1224{
1225 struct edma_chan *echan = to_edma_chan(chan);
1226 struct device *dev = chan->device->dev;
1227 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -05001228 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -05001229 enum dma_slave_buswidth dev_width;
1230 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001231 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -05001232 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001233
1234 if (unlikely(!echan || !sgl || !sg_len))
1235 return NULL;
1236
Matt Porter661f7cb2013-01-10 13:41:04 -05001237 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001238 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001239 dev_width = echan->cfg.src_addr_width;
1240 burst = echan->cfg.src_maxburst;
1241 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001242 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001243 dev_width = echan->cfg.dst_addr_width;
1244 burst = echan->cfg.dst_maxburst;
1245 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001246 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Matt Porter661f7cb2013-01-10 13:41:04 -05001247 return NULL;
1248 }
1249
1250 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001251 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001252 return NULL;
1253 }
1254
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001255 edesc = kzalloc(sizeof(*edesc) + sg_len * sizeof(edesc->pset[0]),
1256 GFP_ATOMIC);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001257 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001258 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001259 return NULL;
1260 }
1261
1262 edesc->pset_nr = sg_len;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001263 edesc->residue = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001264 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001265 edesc->echan = echan;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001266
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001267 /* Allocate a PaRAM slot, if needed */
1268 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
1269
1270 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001271 if (echan->slot[i] < 0) {
1272 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001273 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001274 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +03001275 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001276 dev_err(dev, "%s: Failed to allocate slot\n",
1277 __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001278 return NULL;
1279 }
1280 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001281 }
1282
1283 /* Configure PaRAM sets for each SG */
1284 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001285 /* Get address for each SG */
1286 if (direction == DMA_DEV_TO_MEM)
1287 dst_addr = sg_dma_address(sg);
1288 else
1289 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001290
Joel Fernandesfd009032013-09-23 18:05:13 -05001291 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1292 dst_addr, burst, dev_width,
1293 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +05301294 if (ret < 0) {
1295 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -05001296 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001297 }
1298
Joel Fernandesfd009032013-09-23 18:05:13 -05001299 edesc->absync = ret;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001300 edesc->residue += sg_dma_len(sg);
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001301
1302 /* If this is the last in a current SG set of transactions,
1303 enable interrupts so that next set is processed */
1304 if (!((i+1) % MAX_NR_SG))
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001305 edesc->pset[i].param.opt |= TCINTEN;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001306
Matt Porterc2dde5f2012-08-22 21:09:34 -04001307 /* If this is the last set, enable completion interrupt flag */
1308 if (i == sg_len - 1)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001309 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001310 }
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001311 edesc->residue_stat = edesc->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001312
Matt Porterc2dde5f2012-08-22 21:09:34 -04001313 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1314}
Matt Porterc2dde5f2012-08-22 21:09:34 -04001315
Lad, Prabhakarb7a4fd52015-02-04 13:03:27 +00001316static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001317 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1318 size_t len, unsigned long tx_flags)
1319{
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001320 int ret, nslots;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001321 struct edma_desc *edesc;
1322 struct device *dev = chan->device->dev;
1323 struct edma_chan *echan = to_edma_chan(chan);
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001324 unsigned int width, pset_len;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001325
1326 if (unlikely(!echan || !len))
1327 return NULL;
1328
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001329 if (len < SZ_64K) {
1330 /*
1331 * Transfer size less than 64K can be handled with one paRAM
1332 * slot and with one burst.
1333 * ACNT = length
1334 */
1335 width = len;
1336 pset_len = len;
1337 nslots = 1;
1338 } else {
1339 /*
1340 * Transfer size bigger than 64K will be handled with maximum of
1341 * two paRAM slots.
1342 * slot1: (full_length / 32767) times 32767 bytes bursts.
1343 * ACNT = 32767, length1: (full_length / 32767) * 32767
1344 * slot2: the remaining amount of data after slot1.
1345 * ACNT = full_length - length1, length2 = ACNT
1346 *
1347 * When the full_length is multibple of 32767 one slot can be
1348 * used to complete the transfer.
1349 */
1350 width = SZ_32K - 1;
1351 pset_len = rounddown(len, width);
1352 /* One slot is enough for lengths multiple of (SZ_32K -1) */
1353 if (unlikely(pset_len == len))
1354 nslots = 1;
1355 else
1356 nslots = 2;
1357 }
1358
1359 edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]),
1360 GFP_ATOMIC);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001361 if (!edesc) {
1362 dev_dbg(dev, "Failed to allocate a descriptor\n");
1363 return NULL;
1364 }
1365
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001366 edesc->pset_nr = nslots;
1367 edesc->residue = edesc->residue_stat = len;
1368 edesc->direction = DMA_MEM_TO_MEM;
1369 edesc->echan = echan;
Peter Ujfalusi21a31842015-10-16 10:17:59 +03001370
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001371 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001372 width, pset_len, DMA_MEM_TO_MEM);
1373 if (ret < 0) {
1374 kfree(edesc);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001375 return NULL;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001376 }
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001377
1378 edesc->absync = ret;
1379
Joel Fernandesb0cce4c2014-04-28 15:30:32 -05001380 edesc->pset[0].param.opt |= ITCCHEN;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001381 if (nslots == 1) {
1382 /* Enable transfer complete interrupt */
1383 edesc->pset[0].param.opt |= TCINTEN;
1384 } else {
1385 /* Enable transfer complete chaining for the first slot */
1386 edesc->pset[0].param.opt |= TCCHEN;
1387
1388 if (echan->slot[1] < 0) {
1389 echan->slot[1] = edma_alloc_slot(echan->ecc,
1390 EDMA_SLOT_ANY);
1391 if (echan->slot[1] < 0) {
1392 kfree(edesc);
1393 dev_err(dev, "%s: Failed to allocate slot\n",
1394 __func__);
1395 return NULL;
1396 }
1397 }
1398 dest += pset_len;
1399 src += pset_len;
1400 pset_len = width = len % (SZ_32K - 1);
1401
1402 ret = edma_config_pset(chan, &edesc->pset[1], src, dest, 1,
1403 width, pset_len, DMA_MEM_TO_MEM);
1404 if (ret < 0) {
1405 kfree(edesc);
1406 return NULL;
1407 }
1408
1409 edesc->pset[1].param.opt |= ITCCHEN;
1410 edesc->pset[1].param.opt |= TCINTEN;
1411 }
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001412
1413 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1414}
1415
Joel Fernandes50a9c702013-10-31 16:31:23 -05001416static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
1417 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1418 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +02001419 unsigned long tx_flags)
Joel Fernandes50a9c702013-10-31 16:31:23 -05001420{
1421 struct edma_chan *echan = to_edma_chan(chan);
1422 struct device *dev = chan->device->dev;
1423 struct edma_desc *edesc;
1424 dma_addr_t src_addr, dst_addr;
1425 enum dma_slave_buswidth dev_width;
1426 u32 burst;
1427 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001428
Joel Fernandes50a9c702013-10-31 16:31:23 -05001429 if (unlikely(!echan || !buf_len || !period_len))
1430 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001431
Joel Fernandes50a9c702013-10-31 16:31:23 -05001432 if (direction == DMA_DEV_TO_MEM) {
1433 src_addr = echan->cfg.src_addr;
1434 dst_addr = buf_addr;
1435 dev_width = echan->cfg.src_addr_width;
1436 burst = echan->cfg.src_maxburst;
1437 } else if (direction == DMA_MEM_TO_DEV) {
1438 src_addr = buf_addr;
1439 dst_addr = echan->cfg.dst_addr;
1440 dev_width = echan->cfg.dst_addr_width;
1441 burst = echan->cfg.dst_maxburst;
1442 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001443 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001444 return NULL;
1445 }
1446
1447 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001448 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001449 return NULL;
1450 }
1451
1452 if (unlikely(buf_len % period_len)) {
1453 dev_err(dev, "Period should be multiple of Buffer length\n");
1454 return NULL;
1455 }
1456
1457 nslots = (buf_len / period_len) + 1;
1458
1459 /*
1460 * Cyclic DMA users such as audio cannot tolerate delays introduced
1461 * by cases where the number of periods is more than the maximum
1462 * number of SGs the EDMA driver can handle at a time. For DMA types
1463 * such as Slave SGs, such delays are tolerable and synchronized,
1464 * but the synchronization is difficult to achieve with Cyclic and
1465 * cannot be guaranteed, so we error out early.
1466 */
1467 if (nslots > MAX_NR_SG)
1468 return NULL;
1469
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001470 edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]),
1471 GFP_ATOMIC);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001472 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001473 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001474 return NULL;
1475 }
1476
1477 edesc->cyclic = 1;
1478 edesc->pset_nr = nslots;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001479 edesc->residue = edesc->residue_stat = buf_len;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001480 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001481 edesc->echan = echan;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001482
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001483 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
1484 __func__, echan->ch_num, nslots, period_len, buf_len);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001485
1486 for (i = 0; i < nslots; i++) {
1487 /* Allocate a PaRAM slot, if needed */
1488 if (echan->slot[i] < 0) {
1489 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001490 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001491 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001492 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001493 dev_err(dev, "%s: Failed to allocate slot\n",
1494 __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001495 return NULL;
1496 }
1497 }
1498
1499 if (i == nslots - 1) {
1500 memcpy(&edesc->pset[i], &edesc->pset[0],
1501 sizeof(edesc->pset[0]));
1502 break;
1503 }
1504
1505 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1506 dst_addr, burst, dev_width, period_len,
1507 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001508 if (ret < 0) {
1509 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001510 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001511 }
Joel Fernandes50a9c702013-10-31 16:31:23 -05001512
1513 if (direction == DMA_DEV_TO_MEM)
1514 dst_addr += period_len;
1515 else
1516 src_addr += period_len;
1517
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001518 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
1519 dev_vdbg(dev,
Joel Fernandes50a9c702013-10-31 16:31:23 -05001520 "\n pset[%d]:\n"
1521 " chnum\t%d\n"
1522 " slot\t%d\n"
1523 " opt\t%08x\n"
1524 " src\t%08x\n"
1525 " dst\t%08x\n"
1526 " abcnt\t%08x\n"
1527 " ccnt\t%08x\n"
1528 " bidx\t%08x\n"
1529 " cidx\t%08x\n"
1530 " lkrld\t%08x\n",
1531 i, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001532 edesc->pset[i].param.opt,
1533 edesc->pset[i].param.src,
1534 edesc->pset[i].param.dst,
1535 edesc->pset[i].param.a_b_cnt,
1536 edesc->pset[i].param.ccnt,
1537 edesc->pset[i].param.src_dst_bidx,
1538 edesc->pset[i].param.src_dst_cidx,
1539 edesc->pset[i].param.link_bcntrld);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001540
1541 edesc->absync = ret;
1542
1543 /*
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001544 * Enable period interrupt only if it is requested
Joel Fernandes50a9c702013-10-31 16:31:23 -05001545 */
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001546 if (tx_flags & DMA_PREP_INTERRUPT)
1547 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001548 }
1549
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001550 /* Place the cyclic channel to highest priority queue */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001551 edma_assign_channel_eventq(echan->ecc, echan->ch_num, EVENTQ_0);
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001552
Matt Porterc2dde5f2012-08-22 21:09:34 -04001553 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1554}
1555
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001556static void edma_completion_handler(struct edma_chan *echan)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001557{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001558 struct edma_cc *ecc = echan->ecc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001559 struct device *dev = echan->vchan.chan.device->dev;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001560 struct edma_desc *edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001561
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001562 if (!edesc)
1563 return;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001564
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001565 spin_lock(&echan->vchan.lock);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001566 if (edesc->cyclic) {
1567 vchan_cyclic_callback(&edesc->vdesc);
1568 spin_unlock(&echan->vchan.lock);
1569 return;
1570 } else if (edesc->processed == edesc->pset_nr) {
1571 edesc->residue = 0;
1572 edma_stop(ecc, echan->ch_num);
1573 vchan_cookie_complete(&edesc->vdesc);
1574 echan->edesc = NULL;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001575
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001576 dev_dbg(dev, "Transfer completed on channel %d\n",
1577 echan->ch_num);
1578 } else {
1579 dev_dbg(dev, "Sub transfer completed on channel %d\n",
1580 echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001581
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001582 edma_pause(ecc, echan->ch_num);
Joel Fernandesc5f47992013-08-29 18:05:43 -05001583
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001584 /* Update statistics for tx_status */
1585 edesc->residue -= edesc->sg_len;
1586 edesc->residue_stat = edesc->residue;
1587 edesc->processed_stat = edesc->processed;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001588 }
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001589 edma_execute(echan);
1590
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001591 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001592}
1593
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001594/* eDMA interrupt handler */
1595static irqreturn_t dma_irq_handler(int irq, void *data)
1596{
1597 struct edma_cc *ecc = data;
1598 int ctlr;
1599 u32 sh_ier;
1600 u32 sh_ipr;
1601 u32 bank;
1602
1603 ctlr = ecc->id;
1604 if (ctlr < 0)
1605 return IRQ_NONE;
1606
1607 dev_vdbg(ecc->dev, "dma_irq_handler\n");
1608
1609 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 0);
1610 if (!sh_ipr) {
1611 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 1);
1612 if (!sh_ipr)
1613 return IRQ_NONE;
1614 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 1);
1615 bank = 1;
1616 } else {
1617 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 0);
1618 bank = 0;
1619 }
1620
1621 do {
1622 u32 slot;
1623 u32 channel;
1624
1625 slot = __ffs(sh_ipr);
1626 sh_ipr &= ~(BIT(slot));
1627
1628 if (sh_ier & BIT(slot)) {
1629 channel = (bank << 5) | slot;
1630 /* Clear the corresponding IPR bits */
1631 edma_shadow0_write_array(ecc, SH_ICR, bank, BIT(slot));
1632 edma_completion_handler(&ecc->slave_chans[channel]);
1633 }
1634 } while (sh_ipr);
1635
1636 edma_shadow0_write(ecc, SH_IEVAL, 1);
1637 return IRQ_HANDLED;
1638}
1639
1640static void edma_error_handler(struct edma_chan *echan)
1641{
1642 struct edma_cc *ecc = echan->ecc;
1643 struct device *dev = echan->vchan.chan.device->dev;
1644 struct edmacc_param p;
1645
1646 if (!echan->edesc)
1647 return;
1648
1649 spin_lock(&echan->vchan.lock);
1650
1651 edma_read_slot(ecc, echan->slot[0], &p);
1652 /*
1653 * Issue later based on missed flag which will be sure
1654 * to happen as:
1655 * (1) we finished transmitting an intermediate slot and
1656 * edma_execute is coming up.
1657 * (2) or we finished current transfer and issue will
1658 * call edma_execute.
1659 *
1660 * Important note: issuing can be dangerous here and
1661 * lead to some nasty recursion when we are in a NULL
1662 * slot. So we avoid doing so and set the missed flag.
1663 */
1664 if (p.a_b_cnt == 0 && p.ccnt == 0) {
1665 dev_dbg(dev, "Error on null slot, setting miss\n");
1666 echan->missed = 1;
1667 } else {
1668 /*
1669 * The slot is already programmed but the event got
1670 * missed, so its safe to issue it here.
1671 */
1672 dev_dbg(dev, "Missed event, TRIGGERING\n");
1673 edma_clean_channel(ecc, echan->ch_num);
1674 edma_stop(ecc, echan->ch_num);
1675 edma_start(ecc, echan->ch_num);
1676 edma_trigger_channel(ecc, echan->ch_num);
1677 }
1678 spin_unlock(&echan->vchan.lock);
1679}
1680
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001681static inline bool edma_error_pending(struct edma_cc *ecc)
1682{
1683 if (edma_read_array(ecc, EDMA_EMR, 0) ||
1684 edma_read_array(ecc, EDMA_EMR, 1) ||
1685 edma_read(ecc, EDMA_QEMR) || edma_read(ecc, EDMA_CCERR))
1686 return true;
1687
1688 return false;
1689}
1690
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001691/* eDMA error interrupt handler */
1692static irqreturn_t dma_ccerr_handler(int irq, void *data)
1693{
1694 struct edma_cc *ecc = data;
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001695 int i, j;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001696 int ctlr;
1697 unsigned int cnt = 0;
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001698 unsigned int val;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001699
1700 ctlr = ecc->id;
1701 if (ctlr < 0)
1702 return IRQ_NONE;
1703
1704 dev_vdbg(ecc->dev, "dma_ccerr_handler\n");
1705
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001706 if (!edma_error_pending(ecc))
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001707 return IRQ_NONE;
1708
1709 while (1) {
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001710 /* Event missed register(s) */
1711 for (j = 0; j < 2; j++) {
1712 unsigned long emr;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001713
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001714 val = edma_read_array(ecc, EDMA_EMR, j);
1715 if (!val)
1716 continue;
1717
1718 dev_dbg(ecc->dev, "EMR%d 0x%08x\n", j, val);
1719 emr = val;
1720 for (i = find_next_bit(&emr, 32, 0); i < 32;
1721 i = find_next_bit(&emr, 32, i + 1)) {
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001722 int k = (j << 5) + i;
1723
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001724 /* Clear the corresponding EMR bits */
1725 edma_write_array(ecc, EDMA_EMCR, j, BIT(i));
1726 /* Clear any SER */
1727 edma_shadow0_write_array(ecc, SH_SECR, j,
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001728 BIT(i));
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001729 edma_error_handler(&ecc->slave_chans[k]);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001730 }
1731 }
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001732
1733 val = edma_read(ecc, EDMA_QEMR);
1734 if (val) {
1735 dev_dbg(ecc->dev, "QEMR 0x%02x\n", val);
1736 /* Not reported, just clear the interrupt reason. */
1737 edma_write(ecc, EDMA_QEMCR, val);
1738 edma_shadow0_write(ecc, SH_QSECR, val);
1739 }
1740
1741 val = edma_read(ecc, EDMA_CCERR);
1742 if (val) {
1743 dev_warn(ecc->dev, "CCERR 0x%08x\n", val);
1744 /* Not reported, just clear the interrupt reason. */
1745 edma_write(ecc, EDMA_CCERRCLR, val);
1746 }
1747
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001748 if (!edma_error_pending(ecc))
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001749 break;
1750 cnt++;
1751 if (cnt > 10)
1752 break;
1753 }
1754 edma_write(ecc, EDMA_EEVAL, 1);
1755 return IRQ_HANDLED;
1756}
1757
Matt Porterc2dde5f2012-08-22 21:09:34 -04001758/* Alloc channel resources */
1759static int edma_alloc_chan_resources(struct dma_chan *chan)
1760{
1761 struct edma_chan *echan = to_edma_chan(chan);
1762 struct device *dev = chan->device->dev;
1763 int ret;
1764 int a_ch_num;
1765 LIST_HEAD(descs);
1766
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001767 a_ch_num = edma_alloc_channel(echan->ecc, echan->ch_num, EVENTQ_DEFAULT);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001768
1769 if (a_ch_num < 0) {
1770 ret = -ENODEV;
1771 goto err_no_chan;
1772 }
1773
1774 if (a_ch_num != echan->ch_num) {
1775 dev_err(dev, "failed to allocate requested channel %u:%u\n",
1776 EDMA_CTLR(echan->ch_num),
1777 EDMA_CHAN_SLOT(echan->ch_num));
1778 ret = -ENODEV;
1779 goto err_wrong_chan;
1780 }
1781
1782 echan->alloced = true;
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001783 echan->slot[0] = edma_alloc_slot(echan->ecc, echan->ch_num);
1784 if (echan->slot[0] < 0) {
1785 dev_err(dev, "Entry slot allocation failed for channel %u\n",
1786 EDMA_CHAN_SLOT(echan->ch_num));
1787 goto err_wrong_chan;
1788 }
1789
1790 /* Set up channel -> slot mapping for the entry slot */
1791 edma_set_chmap(echan->ecc, echan->ch_num, echan->slot[0]);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001792
Peter Ujfalusi9aac9092014-04-24 10:29:50 +03001793 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
Ezequiel Garcia0e772c62013-12-13 11:06:18 -03001794 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
Matt Porterc2dde5f2012-08-22 21:09:34 -04001795
1796 return 0;
1797
1798err_wrong_chan:
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001799 edma_free_channel(echan->ecc, a_ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001800err_no_chan:
1801 return ret;
1802}
1803
1804/* Free channel resources */
1805static void edma_free_chan_resources(struct dma_chan *chan)
1806{
1807 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001808 int i;
1809
1810 /* Terminate transfers */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001811 edma_stop(echan->ecc, echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001812
1813 vchan_free_chan_resources(&echan->vchan);
1814
1815 /* Free EDMA PaRAM slots */
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001816 for (i = 0; i < EDMA_MAX_SLOTS; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001817 if (echan->slot[i] >= 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001818 edma_free_slot(echan->ecc, echan->slot[i]);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001819 echan->slot[i] = -1;
1820 }
1821 }
1822
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001823 /* Set entry slot to the dummy slot */
1824 edma_set_chmap(echan->ecc, echan->ch_num, echan->ecc->dummy_slot);
1825
Matt Porterc2dde5f2012-08-22 21:09:34 -04001826 /* Free EDMA channel */
1827 if (echan->alloced) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001828 edma_free_channel(echan->ecc, echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001829 echan->alloced = false;
1830 }
1831
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03001832 dev_dbg(chan->device->dev, "freeing channel for %u\n", echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001833}
1834
1835/* Send pending descriptor to hardware */
1836static void edma_issue_pending(struct dma_chan *chan)
1837{
1838 struct edma_chan *echan = to_edma_chan(chan);
1839 unsigned long flags;
1840
1841 spin_lock_irqsave(&echan->vchan.lock, flags);
1842 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
1843 edma_execute(echan);
1844 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1845}
1846
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001847static u32 edma_residue(struct edma_desc *edesc)
1848{
1849 bool dst = edesc->direction == DMA_DEV_TO_MEM;
1850 struct edma_pset *pset = edesc->pset;
1851 dma_addr_t done, pos;
1852 int i;
1853
1854 /*
1855 * We always read the dst/src position from the first RamPar
1856 * pset. That's the one which is active now.
1857 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001858 pos = edma_get_position(edesc->echan->ecc, edesc->echan->slot[0], dst);
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001859
1860 /*
1861 * Cyclic is simple. Just subtract pset[0].addr from pos.
1862 *
1863 * We never update edesc->residue in the cyclic case, so we
1864 * can tell the remaining room to the end of the circular
1865 * buffer.
1866 */
1867 if (edesc->cyclic) {
1868 done = pos - pset->addr;
1869 edesc->residue_stat = edesc->residue - done;
1870 return edesc->residue_stat;
1871 }
1872
1873 /*
1874 * For SG operation we catch up with the last processed
1875 * status.
1876 */
1877 pset += edesc->processed_stat;
1878
1879 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
1880 /*
1881 * If we are inside this pset address range, we know
1882 * this is the active one. Get the current delta and
1883 * stop walking the psets.
1884 */
1885 if (pos >= pset->addr && pos < pset->addr + pset->len)
1886 return edesc->residue_stat - (pos - pset->addr);
1887
1888 /* Otherwise mark it done and update residue_stat. */
1889 edesc->processed_stat++;
1890 edesc->residue_stat -= pset->len;
1891 }
1892 return edesc->residue_stat;
1893}
1894
Matt Porterc2dde5f2012-08-22 21:09:34 -04001895/* Check request completion status */
1896static enum dma_status edma_tx_status(struct dma_chan *chan,
1897 dma_cookie_t cookie,
1898 struct dma_tx_state *txstate)
1899{
1900 struct edma_chan *echan = to_edma_chan(chan);
1901 struct virt_dma_desc *vdesc;
1902 enum dma_status ret;
1903 unsigned long flags;
1904
1905 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +05301906 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001907 return ret;
1908
1909 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001910 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001911 txstate->residue = edma_residue(echan->edesc);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001912 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
1913 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001914 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1915
1916 return ret;
1917}
1918
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001919static void __init edma_chan_init(struct edma_cc *ecc, struct dma_device *dma,
Matt Porterc2dde5f2012-08-22 21:09:34 -04001920 struct edma_chan *echans)
1921{
1922 int i, j;
1923
Peter Ujfalusicb782052015-10-14 14:42:54 +03001924 for (i = 0; i < ecc->num_channels; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001925 struct edma_chan *echan = &echans[i];
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001926 echan->ch_num = EDMA_CTLR_CHAN(ecc->id, i);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001927 echan->ecc = ecc;
1928 echan->vchan.desc_free = edma_desc_free;
1929
1930 vchan_init(&echan->vchan, dma);
1931
1932 INIT_LIST_HEAD(&echan->node);
1933 for (j = 0; j < EDMA_MAX_SLOTS; j++)
1934 echan->slot[j] = -1;
1935 }
1936}
1937
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001938#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1939 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
Peter Ujfalusie4a899d2014-07-03 07:51:56 +03001940 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001941 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1942
Matt Porterc2dde5f2012-08-22 21:09:34 -04001943static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
1944 struct device *dev)
1945{
1946 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001947 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001948 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001949 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
1950 dma->device_free_chan_resources = edma_free_chan_resources;
1951 dma->device_issue_pending = edma_issue_pending;
1952 dma->device_tx_status = edma_tx_status;
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001953 dma->device_config = edma_slave_config;
1954 dma->device_pause = edma_dma_pause;
1955 dma->device_resume = edma_dma_resume;
1956 dma->device_terminate_all = edma_terminate_all;
Maxime Ripard9f59cd02014-11-17 14:42:47 +01001957
1958 dma->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1959 dma->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
1960 dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1961 dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1962
Matt Porterc2dde5f2012-08-22 21:09:34 -04001963 dma->dev = dev;
1964
1965 INIT_LIST_HEAD(&dma->channels);
1966}
1967
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001968static int edma_setup_from_hw(struct device *dev, struct edma_soc_info *pdata,
1969 struct edma_cc *ecc)
1970{
1971 int i;
1972 u32 value, cccfg;
1973 s8 (*queue_priority_map)[2];
1974
1975 /* Decode the eDMA3 configuration from CCCFG register */
1976 cccfg = edma_read(ecc, EDMA_CCCFG);
1977
1978 value = GET_NUM_REGN(cccfg);
1979 ecc->num_region = BIT(value);
1980
1981 value = GET_NUM_DMACH(cccfg);
1982 ecc->num_channels = BIT(value + 1);
1983
1984 value = GET_NUM_PAENTRY(cccfg);
1985 ecc->num_slots = BIT(value + 4);
1986
1987 value = GET_NUM_EVQUE(cccfg);
1988 ecc->num_tc = value + 1;
1989
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +03001990 ecc->chmap_exist = (cccfg & CHMAP_EXIST) ? true : false;
1991
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001992 dev_dbg(dev, "eDMA3 CC HW configuration (cccfg: 0x%08x):\n", cccfg);
1993 dev_dbg(dev, "num_region: %u\n", ecc->num_region);
1994 dev_dbg(dev, "num_channels: %u\n", ecc->num_channels);
1995 dev_dbg(dev, "num_slots: %u\n", ecc->num_slots);
1996 dev_dbg(dev, "num_tc: %u\n", ecc->num_tc);
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +03001997 dev_dbg(dev, "chmap_exist: %s\n", ecc->chmap_exist ? "yes" : "no");
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001998
1999 /* Nothing need to be done if queue priority is provided */
2000 if (pdata->queue_priority_mapping)
2001 return 0;
2002
2003 /*
2004 * Configure TC/queue priority as follows:
2005 * Q0 - priority 0
2006 * Q1 - priority 1
2007 * Q2 - priority 2
2008 * ...
2009 * The meaning of priority numbers: 0 highest priority, 7 lowest
2010 * priority. So Q0 is the highest priority queue and the last queue has
2011 * the lowest priority.
2012 */
Peter Ujfalusi547c6e22015-10-14 14:42:55 +03002013 queue_priority_map = devm_kcalloc(dev, ecc->num_tc + 1, sizeof(s8),
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002014 GFP_KERNEL);
2015 if (!queue_priority_map)
2016 return -ENOMEM;
2017
2018 for (i = 0; i < ecc->num_tc; i++) {
2019 queue_priority_map[i][0] = i;
2020 queue_priority_map[i][1] = i;
2021 }
2022 queue_priority_map[i][0] = -1;
2023 queue_priority_map[i][1] = -1;
2024
2025 pdata->queue_priority_mapping = queue_priority_map;
2026 /* Default queue has the lowest priority */
2027 pdata->default_queue = i - 1;
2028
2029 return 0;
2030}
2031
2032#if IS_ENABLED(CONFIG_OF)
2033static int edma_xbar_event_map(struct device *dev, struct edma_soc_info *pdata,
2034 size_t sz)
2035{
2036 const char pname[] = "ti,edma-xbar-event-map";
2037 struct resource res;
2038 void __iomem *xbar;
2039 s16 (*xbar_chans)[2];
2040 size_t nelm = sz / sizeof(s16);
2041 u32 shift, offset, mux;
2042 int ret, i;
2043
Peter Ujfalusi547c6e22015-10-14 14:42:55 +03002044 xbar_chans = devm_kcalloc(dev, nelm + 2, sizeof(s16), GFP_KERNEL);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002045 if (!xbar_chans)
2046 return -ENOMEM;
2047
2048 ret = of_address_to_resource(dev->of_node, 1, &res);
2049 if (ret)
2050 return -ENOMEM;
2051
2052 xbar = devm_ioremap(dev, res.start, resource_size(&res));
2053 if (!xbar)
2054 return -ENOMEM;
2055
2056 ret = of_property_read_u16_array(dev->of_node, pname, (u16 *)xbar_chans,
2057 nelm);
2058 if (ret)
2059 return -EIO;
2060
2061 /* Invalidate last entry for the other user of this mess */
2062 nelm >>= 1;
2063 xbar_chans[nelm][0] = -1;
2064 xbar_chans[nelm][1] = -1;
2065
2066 for (i = 0; i < nelm; i++) {
2067 shift = (xbar_chans[i][1] & 0x03) << 3;
2068 offset = xbar_chans[i][1] & 0xfffffffc;
2069 mux = readl(xbar + offset);
2070 mux &= ~(0xff << shift);
2071 mux |= xbar_chans[i][0] << shift;
2072 writel(mux, (xbar + offset));
2073 }
2074
2075 pdata->xbar_chans = (const s16 (*)[2]) xbar_chans;
2076 return 0;
2077}
2078
2079static int edma_of_parse_dt(struct device *dev, struct edma_soc_info *pdata)
2080{
2081 int ret = 0;
2082 struct property *prop;
2083 size_t sz;
2084 struct edma_rsv_info *rsv_info;
2085
2086 rsv_info = devm_kzalloc(dev, sizeof(struct edma_rsv_info), GFP_KERNEL);
2087 if (!rsv_info)
2088 return -ENOMEM;
2089 pdata->rsv = rsv_info;
2090
2091 prop = of_find_property(dev->of_node, "ti,edma-xbar-event-map", &sz);
2092 if (prop)
2093 ret = edma_xbar_event_map(dev, pdata, sz);
2094
2095 return ret;
2096}
2097
2098static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev)
2099{
2100 struct edma_soc_info *info;
2101 int ret;
2102
2103 info = devm_kzalloc(dev, sizeof(struct edma_soc_info), GFP_KERNEL);
2104 if (!info)
2105 return ERR_PTR(-ENOMEM);
2106
2107 ret = edma_of_parse_dt(dev, info);
2108 if (ret)
2109 return ERR_PTR(ret);
2110
2111 return info;
2112}
2113#else
2114static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev)
2115{
2116 return ERR_PTR(-EINVAL);
2117}
2118#endif
2119
Bill Pemberton463a1f82012-11-19 13:22:55 -05002120static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002121{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002122 struct edma_soc_info *info = pdev->dev.platform_data;
2123 s8 (*queue_priority_mapping)[2];
2124 int i, off, ln;
2125 const s16 (*rsv_chans)[2];
2126 const s16 (*rsv_slots)[2];
2127 const s16 (*xbar_chans)[2];
2128 int irq;
2129 char *irq_name;
2130 struct resource *mem;
2131 struct device_node *node = pdev->dev.of_node;
2132 struct device *dev = &pdev->dev;
2133 struct edma_cc *ecc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04002134 int ret;
2135
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002136 if (node) {
2137 info = edma_setup_info_from_dt(dev);
2138 if (IS_ERR(info)) {
2139 dev_err(dev, "failed to get DT data\n");
2140 return PTR_ERR(info);
2141 }
2142 }
2143
2144 if (!info)
2145 return -ENODEV;
2146
2147 pm_runtime_enable(dev);
2148 ret = pm_runtime_get_sync(dev);
2149 if (ret < 0) {
2150 dev_err(dev, "pm_runtime_get_sync() failed\n");
2151 return ret;
2152 }
2153
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002154 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
Russell King94cb0e72013-06-27 13:45:16 +01002155 if (ret)
2156 return ret;
2157
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002158 ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002159 if (!ecc) {
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002160 dev_err(dev, "Can't allocate controller\n");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002161 return -ENOMEM;
2162 }
2163
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002164 ecc->dev = dev;
2165 ecc->id = pdev->id;
2166 /* When booting with DT the pdev->id is -1 */
2167 if (ecc->id < 0)
2168 ecc->id = 0;
Peter Ujfalusica304fa2015-10-14 14:42:49 +03002169
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002170 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma3_cc");
2171 if (!mem) {
2172 dev_dbg(dev, "mem resource not found, using index 0\n");
2173 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2174 if (!mem) {
2175 dev_err(dev, "no mem resource?\n");
2176 return -ENODEV;
2177 }
2178 }
2179 ecc->base = devm_ioremap_resource(dev, mem);
2180 if (IS_ERR(ecc->base))
2181 return PTR_ERR(ecc->base);
Peter Ujfalusib2c843a2015-10-14 14:42:50 +03002182
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002183 platform_set_drvdata(pdev, ecc);
2184
2185 /* Get eDMA3 configuration from IP */
2186 ret = edma_setup_from_hw(dev, info, ecc);
2187 if (ret)
2188 return ret;
2189
Peter Ujfalusicb782052015-10-14 14:42:54 +03002190 /* Allocate memory based on the information we got from the IP */
2191 ecc->slave_chans = devm_kcalloc(dev, ecc->num_channels,
2192 sizeof(*ecc->slave_chans), GFP_KERNEL);
2193 if (!ecc->slave_chans)
2194 return -ENOMEM;
2195
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002196 ecc->channel_unused = devm_kcalloc(dev,
2197 BITS_TO_LONGS(ecc->num_channels),
2198 sizeof(unsigned long), GFP_KERNEL);
2199 if (!ecc->channel_unused)
Peter Ujfalusicb782052015-10-14 14:42:54 +03002200 return -ENOMEM;
2201
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002202 ecc->slot_inuse = devm_kcalloc(dev, BITS_TO_LONGS(ecc->num_slots),
Peter Ujfalusicb782052015-10-14 14:42:54 +03002203 sizeof(unsigned long), GFP_KERNEL);
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002204 if (!ecc->slot_inuse)
Peter Ujfalusicb782052015-10-14 14:42:54 +03002205 return -ENOMEM;
2206
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002207 ecc->default_queue = info->default_queue;
2208
2209 for (i = 0; i < ecc->num_slots; i++)
2210 edma_write_slot(ecc, i, &dummy_paramset);
2211
2212 /* Mark all channels as unused */
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002213 memset(ecc->channel_unused, 0xff, sizeof(ecc->channel_unused));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002214
2215 if (info->rsv) {
2216 /* Clear the reserved channels in unused list */
2217 rsv_chans = info->rsv->rsv_chans;
2218 if (rsv_chans) {
2219 for (i = 0; rsv_chans[i][0] != -1; i++) {
2220 off = rsv_chans[i][0];
2221 ln = rsv_chans[i][1];
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002222 clear_bits(off, ln, ecc->channel_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002223 }
2224 }
2225
2226 /* Set the reserved slots in inuse list */
2227 rsv_slots = info->rsv->rsv_slots;
2228 if (rsv_slots) {
2229 for (i = 0; rsv_slots[i][0] != -1; i++) {
2230 off = rsv_slots[i][0];
2231 ln = rsv_slots[i][1];
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002232 set_bits(off, ln, ecc->slot_inuse);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002233 }
2234 }
2235 }
2236
2237 /* Clear the xbar mapped channels in unused list */
2238 xbar_chans = info->xbar_chans;
2239 if (xbar_chans) {
2240 for (i = 0; xbar_chans[i][1] != -1; i++) {
2241 off = xbar_chans[i][1];
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002242 clear_bits(off, 1, ecc->channel_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002243 }
2244 }
2245
2246 irq = platform_get_irq_byname(pdev, "edma3_ccint");
2247 if (irq < 0 && node)
2248 irq = irq_of_parse_and_map(node, 0);
2249
2250 if (irq >= 0) {
2251 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccint",
2252 dev_name(dev));
2253 ret = devm_request_irq(dev, irq, dma_irq_handler, 0, irq_name,
2254 ecc);
2255 if (ret) {
2256 dev_err(dev, "CCINT (%d) failed --> %d\n", irq, ret);
2257 return ret;
2258 }
2259 }
2260
2261 irq = platform_get_irq_byname(pdev, "edma3_ccerrint");
2262 if (irq < 0 && node)
2263 irq = irq_of_parse_and_map(node, 2);
2264
2265 if (irq >= 0) {
2266 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccerrint",
2267 dev_name(dev));
2268 ret = devm_request_irq(dev, irq, dma_ccerr_handler, 0, irq_name,
2269 ecc);
2270 if (ret) {
2271 dev_err(dev, "CCERRINT (%d) failed --> %d\n", irq, ret);
2272 return ret;
2273 }
2274 }
2275
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002276 ecc->dummy_slot = edma_alloc_slot(ecc, EDMA_SLOT_ANY);
2277 if (ecc->dummy_slot < 0) {
2278 dev_err(dev, "Can't allocate PaRAM dummy slot\n");
2279 return ecc->dummy_slot;
2280 }
2281
2282 for (i = 0; i < ecc->num_channels; i++) {
2283 /* Assign all channels to the default queue */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002284 edma_map_dmach_to_queue(ecc, i, info->default_queue);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002285 /* Set entry slot to the dummy slot */
2286 edma_set_chmap(ecc, i, ecc->dummy_slot);
2287 }
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002288
2289 queue_priority_mapping = info->queue_priority_mapping;
2290
2291 /* Event queue priority mapping */
2292 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2293 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2294 queue_priority_mapping[i][1]);
2295
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002296 for (i = 0; i < ecc->num_region; i++) {
2297 edma_write_array2(ecc, EDMA_DRAE, i, 0, 0x0);
2298 edma_write_array2(ecc, EDMA_DRAE, i, 1, 0x0);
2299 edma_write_array(ecc, EDMA_QRAE, i, 0x0);
2300 }
2301 ecc->info = info;
2302
Matt Porterc2dde5f2012-08-22 21:09:34 -04002303 dma_cap_zero(ecc->dma_slave.cap_mask);
2304 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
Peter Ujfalusi232b223d2014-04-14 14:42:00 +03002305 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05002306 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002307
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002308 edma_dma_init(ecc, &ecc->dma_slave, dev);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002309
2310 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
2311
2312 ret = dma_async_device_register(&ecc->dma_slave);
2313 if (ret)
2314 goto err_reg1;
2315
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002316 if (node)
2317 of_dma_controller_register(node, of_dma_xlate_by_chan_id,
Peter Ujfalusib2c843a2015-10-14 14:42:50 +03002318 &ecc->dma_slave);
Peter Ujfalusidc9b60552015-10-14 14:42:47 +03002319
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002320 dev_info(dev, "TI EDMA DMA engine driver\n");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002321
2322 return 0;
2323
2324err_reg1:
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002325 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002326 return ret;
2327}
2328
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08002329static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002330{
2331 struct device *dev = &pdev->dev;
2332 struct edma_cc *ecc = dev_get_drvdata(dev);
2333
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002334 if (dev->of_node)
2335 of_dma_controller_free(dev->of_node);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002336 dma_async_device_unregister(&ecc->dma_slave);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002337 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002338
2339 return 0;
2340}
2341
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002342#ifdef CONFIG_PM_SLEEP
2343static int edma_pm_resume(struct device *dev)
2344{
2345 struct edma_cc *ecc = dev_get_drvdata(dev);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002346 struct edma_chan *echan = ecc->slave_chans;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002347 int i;
2348 s8 (*queue_priority_mapping)[2];
2349
2350 queue_priority_mapping = ecc->info->queue_priority_mapping;
2351
2352 /* Event queue priority mapping */
2353 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2354 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2355 queue_priority_mapping[i][1]);
2356
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002357 for (i = 0; i < ecc->num_channels; i++) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002358 if (echan[i].alloced) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002359 /* ensure access through shadow region 0 */
2360 edma_or_array2(ecc, EDMA_DRAE, 0, i >> 5,
2361 BIT(i & 0x1f));
2362
2363 edma_setup_interrupt(ecc, EDMA_CTLR_CHAN(ecc->id, i),
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03002364 true);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002365
2366 /* Set up channel -> slot mapping for the entry slot */
2367 edma_set_chmap(ecc, echan[i].ch_num, echan[i].slot[0]);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002368 }
2369 }
2370
2371 return 0;
2372}
2373#endif
2374
2375static const struct dev_pm_ops edma_pm_ops = {
2376 SET_LATE_SYSTEM_SLEEP_PM_OPS(NULL, edma_pm_resume)
2377};
2378
Matt Porterc2dde5f2012-08-22 21:09:34 -04002379static struct platform_driver edma_driver = {
2380 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05002381 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002382 .driver = {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002383 .name = "edma",
2384 .pm = &edma_pm_ops,
2385 .of_match_table = edma_of_ids,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002386 },
2387};
2388
2389bool edma_filter_fn(struct dma_chan *chan, void *param)
2390{
2391 if (chan->device->dev->driver == &edma_driver.driver) {
2392 struct edma_chan *echan = to_edma_chan(chan);
2393 unsigned ch_req = *(unsigned *)param;
2394 return ch_req == echan->ch_num;
2395 }
2396 return false;
2397}
2398EXPORT_SYMBOL(edma_filter_fn);
2399
Matt Porterc2dde5f2012-08-22 21:09:34 -04002400static int edma_init(void)
2401{
Arnd Bergmann5305e4d2014-10-24 18:14:01 +02002402 return platform_driver_register(&edma_driver);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002403}
2404subsys_initcall(edma_init);
2405
2406static void __exit edma_exit(void)
2407{
Matt Porterc2dde5f2012-08-22 21:09:34 -04002408 platform_driver_unregister(&edma_driver);
2409}
2410module_exit(edma_exit);
2411
Josh Boyerd71505b2013-09-04 10:32:50 -04002412MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002413MODULE_DESCRIPTION("TI EDMA DMA engine driver");
2414MODULE_LICENSE("GPL v2");