blob: 051a7c4593d4abd496db7bcb05ec96c9bafa6162 [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
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300352static inline unsigned int edma_param_read(struct edma_cc *ecc, int offset,
353 int param_no)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300354{
355 return edma_read(ecc, EDMA_PARM + offset + (param_no << 5));
356}
357
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300358static inline void edma_param_write(struct edma_cc *ecc, int offset,
359 int param_no, unsigned val)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300360{
361 edma_write(ecc, EDMA_PARM + offset + (param_no << 5), val);
362}
363
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300364static inline void edma_param_modify(struct edma_cc *ecc, int offset,
365 int param_no, unsigned and, unsigned or)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300366{
367 edma_modify(ecc, EDMA_PARM + offset + (param_no << 5), and, or);
368}
369
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300370static inline void edma_param_and(struct edma_cc *ecc, int offset, int param_no,
371 unsigned and)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300372{
373 edma_and(ecc, EDMA_PARM + offset + (param_no << 5), and);
374}
375
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300376static inline void edma_param_or(struct edma_cc *ecc, int offset, int param_no,
377 unsigned or)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300378{
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
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300394static void edma_map_dmach_to_queue(struct edma_chan *echan,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300395 enum dma_event_q queue_no)
396{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300397 struct edma_cc *ecc = echan->ecc;
398 int channel = EDMA_CHAN_SLOT(echan->ch_num);
399 int bit = (channel & 0x7) * 4;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300400
401 /* default to low priority queue */
402 if (queue_no == EVENTQ_DEFAULT)
403 queue_no = ecc->default_queue;
404
405 queue_no &= 7;
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300406 edma_modify_array(ecc, EDMA_DMAQNUM, (channel >> 3), ~(0x7 << bit),
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300407 queue_no << bit);
408}
409
410static void edma_assign_priority_to_queue(struct edma_cc *ecc, int queue_no,
411 int priority)
412{
413 int bit = queue_no * 4;
414
415 edma_modify(ecc, EDMA_QUEPRI, ~(0x7 << bit), ((priority & 0x7) << bit));
416}
417
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300418static void edma_set_chmap(struct edma_chan *echan, int slot)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300419{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300420 struct edma_cc *ecc = echan->ecc;
421 int channel = EDMA_CHAN_SLOT(echan->ch_num);
422
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300423 if (ecc->chmap_exist) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300424 slot = EDMA_CHAN_SLOT(slot);
425 edma_write_array(ecc, EDMA_DCHMAP, channel, (slot << 5));
426 }
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300427}
428
429static int prepare_unused_channel_list(struct device *dev, void *data)
430{
431 struct platform_device *pdev = to_platform_device(dev);
432 struct edma_cc *ecc = data;
Peter Ujfalusicb782052015-10-14 14:42:54 +0300433 int dma_req_min = EDMA_CTLR_CHAN(ecc->id, 0);
434 int dma_req_max = dma_req_min + ecc->num_channels;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300435 int i, count;
436 struct of_phandle_args dma_spec;
437
438 if (dev->of_node) {
439 struct platform_device *dma_pdev;
440
441 count = of_property_count_strings(dev->of_node, "dma-names");
442 if (count < 0)
443 return 0;
444 for (i = 0; i < count; i++) {
445 if (of_parse_phandle_with_args(dev->of_node, "dmas",
446 "#dma-cells", i,
447 &dma_spec))
448 continue;
449
450 if (!of_match_node(edma_of_ids, dma_spec.np)) {
451 of_node_put(dma_spec.np);
452 continue;
453 }
454
455 dma_pdev = of_find_device_by_node(dma_spec.np);
456 if (&dma_pdev->dev != ecc->dev)
457 continue;
458
459 clear_bit(EDMA_CHAN_SLOT(dma_spec.args[0]),
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300460 ecc->channel_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300461 of_node_put(dma_spec.np);
462 }
463 return 0;
464 }
465
466 /* For non-OF case */
467 for (i = 0; i < pdev->num_resources; i++) {
468 struct resource *res = &pdev->resource[i];
Peter Ujfalusicb782052015-10-14 14:42:54 +0300469 int dma_req;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300470
Peter Ujfalusicb782052015-10-14 14:42:54 +0300471 if (!(res->flags & IORESOURCE_DMA))
472 continue;
473
474 dma_req = (int)res->start;
475 if (dma_req >= dma_req_min && dma_req < dma_req_max)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300476 clear_bit(EDMA_CHAN_SLOT(pdev->resource[i].start),
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300477 ecc->channel_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300478 }
479
480 return 0;
481}
482
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300483static void edma_setup_interrupt(struct edma_chan *echan, bool enable)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300484{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300485 struct edma_cc *ecc = echan->ecc;
486 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300487
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300488 if (enable) {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300489 edma_shadow0_write_array(ecc, SH_ICR, channel >> 5,
490 BIT(channel & 0x1f));
491 edma_shadow0_write_array(ecc, SH_IESR, channel >> 5,
492 BIT(channel & 0x1f));
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300493 } else {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300494 edma_shadow0_write_array(ecc, SH_IECR, channel >> 5,
495 BIT(channel & 0x1f));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300496 }
497}
498
499/*
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300500 * paRAM slot management functions
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300501 */
502static void edma_write_slot(struct edma_cc *ecc, unsigned slot,
503 const struct edmacc_param *param)
504{
505 slot = EDMA_CHAN_SLOT(slot);
506 if (slot >= ecc->num_slots)
507 return;
508 memcpy_toio(ecc->base + PARM_OFFSET(slot), param, PARM_SIZE);
509}
510
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300511static void edma_read_slot(struct edma_cc *ecc, unsigned slot,
512 struct edmacc_param *param)
513{
514 slot = EDMA_CHAN_SLOT(slot);
515 if (slot >= ecc->num_slots)
516 return;
517 memcpy_fromio(param, ecc->base + PARM_OFFSET(slot), PARM_SIZE);
518}
519
520/**
521 * edma_alloc_slot - allocate DMA parameter RAM
522 * @ecc: pointer to edma_cc struct
523 * @slot: specific slot to allocate; negative for "any unused slot"
524 *
525 * This allocates a parameter RAM slot, initializing it to hold a
526 * dummy transfer. Slots allocated using this routine have not been
527 * mapped to a hardware DMA channel, and will normally be used by
528 * linking to them from a slot associated with a DMA channel.
529 *
530 * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
531 * slots may be allocated on behalf of DSP firmware.
532 *
533 * Returns the number of the slot, else negative errno.
534 */
535static int edma_alloc_slot(struct edma_cc *ecc, int slot)
536{
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300537 if (slot > 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300538 slot = EDMA_CHAN_SLOT(slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300539 /* Requesting entry paRAM slot for a HW triggered channel. */
540 if (ecc->chmap_exist && slot < ecc->num_channels)
541 slot = EDMA_SLOT_ANY;
542 }
543
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300544 if (slot < 0) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300545 if (ecc->chmap_exist)
546 slot = 0;
547 else
548 slot = ecc->num_channels;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300549 for (;;) {
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300550 slot = find_next_zero_bit(ecc->slot_inuse,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300551 ecc->num_slots,
552 slot);
553 if (slot == ecc->num_slots)
554 return -ENOMEM;
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300555 if (!test_and_set_bit(slot, ecc->slot_inuse))
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300556 break;
557 }
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300558 } else if (slot >= ecc->num_slots) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300559 return -EINVAL;
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300560 } else if (test_and_set_bit(slot, ecc->slot_inuse)) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300561 return -EBUSY;
562 }
563
564 edma_write_slot(ecc, slot, &dummy_paramset);
565
566 return EDMA_CTLR_CHAN(ecc->id, slot);
567}
568
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300569static void edma_free_slot(struct edma_cc *ecc, unsigned slot)
570{
571 slot = EDMA_CHAN_SLOT(slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300572 if (slot >= ecc->num_slots)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300573 return;
574
575 edma_write_slot(ecc, slot, &dummy_paramset);
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300576 clear_bit(slot, ecc->slot_inuse);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300577}
578
579/**
580 * edma_link - link one parameter RAM slot to another
581 * @ecc: pointer to edma_cc struct
582 * @from: parameter RAM slot originating the link
583 * @to: parameter RAM slot which is the link target
584 *
585 * The originating slot should not be part of any active DMA transfer.
586 */
587static void edma_link(struct edma_cc *ecc, unsigned from, unsigned to)
588{
Peter Ujfalusifc014092015-10-14 14:42:59 +0300589 if (unlikely(EDMA_CTLR(from) != EDMA_CTLR(to)))
590 dev_warn(ecc->dev, "Ignoring eDMA instance for linking\n");
591
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300592 from = EDMA_CHAN_SLOT(from);
593 to = EDMA_CHAN_SLOT(to);
594 if (from >= ecc->num_slots || to >= ecc->num_slots)
595 return;
596
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300597 edma_param_modify(ecc, PARM_LINK_BCNTRLD, from, 0xffff0000,
598 PARM_OFFSET(to));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300599}
600
601/**
602 * edma_get_position - returns the current transfer point
603 * @ecc: pointer to edma_cc struct
604 * @slot: parameter RAM slot being examined
605 * @dst: true selects the dest position, false the source
606 *
607 * Returns the position of the current active slot
608 */
609static dma_addr_t edma_get_position(struct edma_cc *ecc, unsigned slot,
610 bool dst)
611{
612 u32 offs;
613
614 slot = EDMA_CHAN_SLOT(slot);
615 offs = PARM_OFFSET(slot);
616 offs += dst ? PARM_DST : PARM_SRC;
617
618 return edma_read(ecc, offs);
619}
620
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300621/*
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300622 * 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.)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300626 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300627static void edma_start(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300628{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300629 struct edma_cc *ecc = echan->ecc;
630 int channel = EDMA_CHAN_SLOT(echan->ch_num);
631 int j = (channel >> 5);
632 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300633
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300634 if (test_bit(channel, ecc->channel_unused)) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300635 /* EDMA channels without event association */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300636 dev_dbg(ecc->dev, "ESR%d %08x\n", j,
637 edma_shadow0_read_array(ecc, SH_ESR, j));
638 edma_shadow0_write_array(ecc, SH_ESR, j, mask);
639 } else {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300640 /* EDMA channel with event association */
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300641 dev_dbg(ecc->dev, "ER%d %08x\n", j,
642 edma_shadow0_read_array(ecc, SH_ER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300643 /* Clear any pending event or error */
644 edma_write_array(ecc, EDMA_ECR, j, mask);
645 edma_write_array(ecc, EDMA_EMCR, j, mask);
646 /* Clear any SER */
647 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
648 edma_shadow0_write_array(ecc, SH_EESR, j, mask);
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300649 dev_dbg(ecc->dev, "EER%d %08x\n", j,
650 edma_shadow0_read_array(ecc, SH_EER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300651 }
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300652}
653
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300654static void edma_stop(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300655{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300656 struct edma_cc *ecc = echan->ecc;
657 int channel = EDMA_CHAN_SLOT(echan->ch_num);
658 int j = (channel >> 5);
659 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300660
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300661 edma_shadow0_write_array(ecc, SH_EECR, j, mask);
662 edma_shadow0_write_array(ecc, SH_ECR, j, mask);
663 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
664 edma_write_array(ecc, EDMA_EMCR, j, mask);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300665
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300666 /* clear possibly pending completion interrupt */
667 edma_shadow0_write_array(ecc, SH_ICR, j, mask);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300668
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300669 dev_dbg(ecc->dev, "EER%d %08x\n", j,
670 edma_shadow0_read_array(ecc, SH_EER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300671
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300672 /* REVISIT: consider guarding against inappropriate event
673 * chaining by overwriting with dummy_paramset.
674 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300675}
676
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300677/*
678 * Temporarily disable EDMA hardware events on the specified channel,
679 * preventing them from triggering new transfers
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300680 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300681static void edma_pause(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300682{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300683 int channel = EDMA_CHAN_SLOT(echan->ch_num);
684 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300685
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300686 edma_shadow0_write_array(echan->ecc, SH_EECR, channel >> 5, mask);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300687}
688
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300689/* Re-enable EDMA hardware events on the specified channel. */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300690static void edma_resume(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300691{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300692 int channel = EDMA_CHAN_SLOT(echan->ch_num);
693 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300694
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300695 edma_shadow0_write_array(echan->ecc, SH_EESR, channel >> 5, mask);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300696}
697
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300698static void edma_trigger_channel(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300699{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300700 struct edma_cc *ecc = echan->ecc;
701 int channel = EDMA_CHAN_SLOT(echan->ch_num);
702 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300703
704 edma_shadow0_write_array(ecc, SH_ESR, (channel >> 5), mask);
705
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300706 dev_dbg(ecc->dev, "ESR%d %08x\n", (channel >> 5),
707 edma_shadow0_read_array(ecc, SH_ESR, (channel >> 5)));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300708}
709
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300710static void edma_clean_channel(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300711{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300712 struct edma_cc *ecc = echan->ecc;
713 int channel = EDMA_CHAN_SLOT(echan->ch_num);
714 int j = (channel >> 5);
715 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300716
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300717 dev_dbg(ecc->dev, "EMR%d %08x\n", j, edma_read_array(ecc, EDMA_EMR, j));
718 edma_shadow0_write_array(ecc, SH_ECR, j, mask);
719 /* Clear the corresponding EMR bits */
720 edma_write_array(ecc, EDMA_EMCR, j, mask);
721 /* Clear any SER */
722 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
723 edma_write(ecc, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300724}
725
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300726static int edma_alloc_channel(struct edma_chan *echan,
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300727 enum dma_event_q eventq_no)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300728{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300729 struct edma_cc *ecc = echan->ecc;
730 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300731
732 if (!ecc->unused_chan_list_done) {
733 /*
734 * Scan all the platform devices to find out the EDMA channels
735 * used and clear them in the unused list, making the rest
736 * available for ARM usage.
737 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300738 int ret = bus_for_each_dev(&platform_bus_type, NULL, ecc,
739 prepare_unused_channel_list);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300740 if (ret < 0)
741 return ret;
742
743 ecc->unused_chan_list_done = true;
744 }
745
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300746 /* ensure access through shadow region 0 */
747 edma_or_array2(ecc, EDMA_DRAE, 0, channel >> 5, BIT(channel & 0x1f));
748
749 /* ensure no events are pending */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300750 edma_stop(echan);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300751
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300752 edma_setup_interrupt(echan, true);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300753
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300754 edma_map_dmach_to_queue(echan, eventq_no);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300755
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300756 return 0;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300757}
758
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300759static void edma_free_channel(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300760{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300761 /* ensure no events are pending */
762 edma_stop(echan);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300763 /* REVISIT should probably take out of shadow region 0 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300764 edma_setup_interrupt(echan, false);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300765}
766
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300767/* Move channel to a specific event queue */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300768static void edma_assign_channel_eventq(struct edma_chan *echan,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300769 enum dma_event_q eventq_no)
770{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300771 struct edma_cc *ecc = echan->ecc;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300772
773 /* default to low priority queue */
774 if (eventq_no == EVENTQ_DEFAULT)
775 eventq_no = ecc->default_queue;
776 if (eventq_no >= ecc->num_tc)
777 return;
778
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300779 edma_map_dmach_to_queue(echan, eventq_no);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300780}
781
Matt Porterc2dde5f2012-08-22 21:09:34 -0400782static inline struct edma_cc *to_edma_cc(struct dma_device *d)
783{
784 return container_of(d, struct edma_cc, dma_slave);
785}
786
787static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
788{
789 return container_of(c, struct edma_chan, vchan.chan);
790}
791
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300792static inline struct edma_desc *to_edma_desc(struct dma_async_tx_descriptor *tx)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400793{
794 return container_of(tx, struct edma_desc, vdesc.tx);
795}
796
797static void edma_desc_free(struct virt_dma_desc *vdesc)
798{
799 kfree(container_of(vdesc, struct edma_desc, vdesc));
800}
801
802/* Dispatch a queued descriptor to the controller (caller holds lock) */
803static void edma_execute(struct edma_chan *echan)
804{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300805 struct edma_cc *ecc = echan->ecc;
Joel Fernandes53407062013-09-03 10:02:46 -0500806 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400807 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500808 struct device *dev = echan->vchan.chan.device->dev;
809 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400810
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300811 if (!echan->edesc) {
812 /* Setup is needed for the first transfer */
Joel Fernandes53407062013-09-03 10:02:46 -0500813 vdesc = vchan_next_desc(&echan->vchan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300814 if (!vdesc)
Joel Fernandes53407062013-09-03 10:02:46 -0500815 return;
Joel Fernandes53407062013-09-03 10:02:46 -0500816 list_del(&vdesc->node);
817 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400818 }
819
Joel Fernandes53407062013-09-03 10:02:46 -0500820 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400821
Joel Fernandes53407062013-09-03 10:02:46 -0500822 /* Find out how many left */
823 left = edesc->pset_nr - edesc->processed;
824 nslots = min(MAX_NR_SG, left);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500825 edesc->sg_len = 0;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400826
827 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500828 for (i = 0; i < nslots; i++) {
829 j = i + edesc->processed;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300830 edma_write_slot(ecc, echan->slot[i], &edesc->pset[j].param);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500831 edesc->sg_len += edesc->pset[j].len;
Peter Ujfalusi907f74a2015-10-14 14:42:56 +0300832 dev_vdbg(dev,
833 "\n pset[%d]:\n"
834 " chnum\t%d\n"
835 " slot\t%d\n"
836 " opt\t%08x\n"
837 " src\t%08x\n"
838 " dst\t%08x\n"
839 " abcnt\t%08x\n"
840 " ccnt\t%08x\n"
841 " bidx\t%08x\n"
842 " cidx\t%08x\n"
843 " lkrld\t%08x\n",
844 j, echan->ch_num, echan->slot[i],
845 edesc->pset[j].param.opt,
846 edesc->pset[j].param.src,
847 edesc->pset[j].param.dst,
848 edesc->pset[j].param.a_b_cnt,
849 edesc->pset[j].param.ccnt,
850 edesc->pset[j].param.src_dst_bidx,
851 edesc->pset[j].param.src_dst_cidx,
852 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400853 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500854 if (i != (nslots - 1))
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300855 edma_link(ecc, echan->slot[i], echan->slot[i + 1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400856 }
857
Joel Fernandes53407062013-09-03 10:02:46 -0500858 edesc->processed += nslots;
859
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500860 /*
861 * If this is either the last set in a set of SG-list transactions
862 * then setup a link to the dummy slot, this results in all future
863 * events being absorbed and that's OK because we're done
864 */
Joel Fernandes50a9c702013-10-31 16:31:23 -0500865 if (edesc->processed == edesc->pset_nr) {
866 if (edesc->cyclic)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300867 edma_link(ecc, echan->slot[nslots - 1], echan->slot[1]);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500868 else
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300869 edma_link(ecc, echan->slot[nslots - 1],
Joel Fernandes50a9c702013-10-31 16:31:23 -0500870 echan->ecc->dummy_slot);
871 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500872
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300873 if (echan->missed) {
874 /*
875 * This happens due to setup times between intermediate
876 * transfers in long SG lists which have to be broken up into
877 * transfers of MAX_NR_SG
878 */
879 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300880 edma_clean_channel(echan);
881 edma_stop(echan);
882 edma_start(echan);
883 edma_trigger_channel(echan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300884 echan->missed = 0;
885 } else if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300886 dev_dbg(dev, "first transfer starting on channel %d\n",
887 echan->ch_num);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300888 edma_start(echan);
Sekhar Nori5fc68a62014-03-19 11:25:50 +0530889 } else {
890 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
891 echan->ch_num, edesc->processed);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300892 edma_resume(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500893 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400894}
895
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100896static int edma_terminate_all(struct dma_chan *chan)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400897{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100898 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400899 unsigned long flags;
900 LIST_HEAD(head);
901
902 spin_lock_irqsave(&echan->vchan.lock, flags);
903
904 /*
905 * Stop DMA activity: we assume the callback will not be called
906 * after edma_dma() returns (even if it does, it will see
907 * echan->edesc is NULL and exit.)
908 */
909 if (echan->edesc) {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300910 edma_stop(echan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300911 /* Move the cyclic channel back to default queue */
912 if (echan->edesc->cyclic)
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300913 edma_assign_channel_eventq(echan, EVENTQ_DEFAULT);
Petr Kulhavy5ca9e7c2015-03-27 13:35:51 +0200914 /*
915 * free the running request descriptor
916 * since it is not in any of the vdesc lists
917 */
918 edma_desc_free(&echan->edesc->vdesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400919 echan->edesc = NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400920 }
921
922 vchan_get_all_descriptors(&echan->vchan, &head);
923 spin_unlock_irqrestore(&echan->vchan.lock, flags);
924 vchan_dma_desc_free_list(&echan->vchan, &head);
925
926 return 0;
927}
928
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100929static int edma_slave_config(struct dma_chan *chan,
Matt Porter661f7cb2013-01-10 13:41:04 -0500930 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400931{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100932 struct edma_chan *echan = to_edma_chan(chan);
933
Matt Porter661f7cb2013-01-10 13:41:04 -0500934 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
935 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400936 return -EINVAL;
937
Matt Porter661f7cb2013-01-10 13:41:04 -0500938 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400939
940 return 0;
941}
942
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100943static int edma_dma_pause(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300944{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100945 struct edma_chan *echan = to_edma_chan(chan);
946
John Ogness02ec6042015-04-27 13:52:25 +0200947 if (!echan->edesc)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300948 return -EINVAL;
949
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300950 edma_pause(echan);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300951 return 0;
952}
953
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100954static int edma_dma_resume(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300955{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100956 struct edma_chan *echan = to_edma_chan(chan);
957
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300958 edma_resume(echan);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300959 return 0;
960}
961
Joel Fernandesfd009032013-09-23 18:05:13 -0500962/*
963 * A PaRAM set configuration abstraction used by other modes
964 * @chan: Channel who's PaRAM set we're configuring
965 * @pset: PaRAM set to initialize and setup.
966 * @src_addr: Source address of the DMA
967 * @dst_addr: Destination address of the DMA
968 * @burst: In units of dev_width, how much to send
969 * @dev_width: How much is the dev_width
970 * @dma_length: Total length of the DMA transfer
971 * @direction: Direction of the transfer
972 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500973static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300974 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
Peter Ujfalusidf6694f2015-10-16 10:18:00 +0300975 unsigned int acnt, unsigned int dma_length,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300976 enum dma_transfer_direction direction)
Joel Fernandesfd009032013-09-23 18:05:13 -0500977{
978 struct edma_chan *echan = to_edma_chan(chan);
979 struct device *dev = chan->device->dev;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500980 struct edmacc_param *param = &epset->param;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +0300981 int bcnt, ccnt, cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500982 int src_bidx, dst_bidx, src_cidx, dst_cidx;
983 int absync;
984
Peter Ujfalusib2b617d2014-04-14 14:41:58 +0300985 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
986 if (!burst)
987 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -0500988 /*
989 * If the maxburst is equal to the fifo width, use
990 * A-synced transfers. This allows for large contiguous
991 * buffer transfers using only one PaRAM set.
992 */
993 if (burst == 1) {
994 /*
995 * For the A-sync case, bcnt and ccnt are the remainder
996 * and quotient respectively of the division of:
997 * (dma_length / acnt) by (SZ_64K -1). This is so
998 * that in case bcnt over flows, we have ccnt to use.
999 * Note: In A-sync tranfer only, bcntrld is used, but it
1000 * only applies for sg_dma_len(sg) >= SZ_64K.
1001 * In this case, the best way adopted is- bccnt for the
1002 * first frame will be the remainder below. Then for
1003 * every successive frame, bcnt will be SZ_64K-1. This
1004 * is assured as bcntrld = 0xffff in end of function.
1005 */
1006 absync = false;
1007 ccnt = dma_length / acnt / (SZ_64K - 1);
1008 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
1009 /*
1010 * If bcnt is non-zero, we have a remainder and hence an
1011 * extra frame to transfer, so increment ccnt.
1012 */
1013 if (bcnt)
1014 ccnt++;
1015 else
1016 bcnt = SZ_64K - 1;
1017 cidx = acnt;
1018 } else {
1019 /*
1020 * If maxburst is greater than the fifo address_width,
1021 * use AB-synced transfers where A count is the fifo
1022 * address_width and B count is the maxburst. In this
1023 * case, we are limited to transfers of C count frames
1024 * of (address_width * maxburst) where C count is limited
1025 * to SZ_64K-1. This places an upper bound on the length
1026 * of an SG segment that can be handled.
1027 */
1028 absync = true;
1029 bcnt = burst;
1030 ccnt = dma_length / (acnt * bcnt);
1031 if (ccnt > (SZ_64K - 1)) {
1032 dev_err(dev, "Exceeded max SG segment size\n");
1033 return -EINVAL;
1034 }
1035 cidx = acnt * bcnt;
1036 }
1037
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001038 epset->len = dma_length;
1039
Joel Fernandesfd009032013-09-23 18:05:13 -05001040 if (direction == DMA_MEM_TO_DEV) {
1041 src_bidx = acnt;
1042 src_cidx = cidx;
1043 dst_bidx = 0;
1044 dst_cidx = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001045 epset->addr = src_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001046 } else if (direction == DMA_DEV_TO_MEM) {
1047 src_bidx = 0;
1048 src_cidx = 0;
1049 dst_bidx = acnt;
1050 dst_cidx = cidx;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001051 epset->addr = dst_addr;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001052 } else if (direction == DMA_MEM_TO_MEM) {
1053 src_bidx = acnt;
1054 src_cidx = cidx;
1055 dst_bidx = acnt;
1056 dst_cidx = cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001057 } else {
1058 dev_err(dev, "%s: direction not implemented yet\n", __func__);
1059 return -EINVAL;
1060 }
1061
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001062 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
Joel Fernandesfd009032013-09-23 18:05:13 -05001063 /* Configure A or AB synchronized transfers */
1064 if (absync)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001065 param->opt |= SYNCDIM;
Joel Fernandesfd009032013-09-23 18:05:13 -05001066
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001067 param->src = src_addr;
1068 param->dst = dst_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001069
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001070 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
1071 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001072
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001073 param->a_b_cnt = bcnt << 16 | acnt;
1074 param->ccnt = ccnt;
Joel Fernandesfd009032013-09-23 18:05:13 -05001075 /*
1076 * Only time when (bcntrld) auto reload is required is for
1077 * A-sync case, and in this case, a requirement of reload value
1078 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
1079 * and then later will be populated by edma_execute.
1080 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001081 param->link_bcntrld = 0xffffffff;
Joel Fernandesfd009032013-09-23 18:05:13 -05001082 return absync;
1083}
1084
Matt Porterc2dde5f2012-08-22 21:09:34 -04001085static struct dma_async_tx_descriptor *edma_prep_slave_sg(
1086 struct dma_chan *chan, struct scatterlist *sgl,
1087 unsigned int sg_len, enum dma_transfer_direction direction,
1088 unsigned long tx_flags, void *context)
1089{
1090 struct edma_chan *echan = to_edma_chan(chan);
1091 struct device *dev = chan->device->dev;
1092 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -05001093 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -05001094 enum dma_slave_buswidth dev_width;
1095 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001096 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -05001097 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001098
1099 if (unlikely(!echan || !sgl || !sg_len))
1100 return NULL;
1101
Matt Porter661f7cb2013-01-10 13:41:04 -05001102 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001103 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001104 dev_width = echan->cfg.src_addr_width;
1105 burst = echan->cfg.src_maxburst;
1106 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001107 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001108 dev_width = echan->cfg.dst_addr_width;
1109 burst = echan->cfg.dst_maxburst;
1110 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001111 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Matt Porter661f7cb2013-01-10 13:41:04 -05001112 return NULL;
1113 }
1114
1115 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001116 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001117 return NULL;
1118 }
1119
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001120 edesc = kzalloc(sizeof(*edesc) + sg_len * sizeof(edesc->pset[0]),
1121 GFP_ATOMIC);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001122 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001123 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001124 return NULL;
1125 }
1126
1127 edesc->pset_nr = sg_len;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001128 edesc->residue = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001129 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001130 edesc->echan = echan;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001131
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001132 /* Allocate a PaRAM slot, if needed */
1133 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
1134
1135 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001136 if (echan->slot[i] < 0) {
1137 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001138 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001139 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +03001140 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001141 dev_err(dev, "%s: Failed to allocate slot\n",
1142 __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001143 return NULL;
1144 }
1145 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001146 }
1147
1148 /* Configure PaRAM sets for each SG */
1149 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001150 /* Get address for each SG */
1151 if (direction == DMA_DEV_TO_MEM)
1152 dst_addr = sg_dma_address(sg);
1153 else
1154 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001155
Joel Fernandesfd009032013-09-23 18:05:13 -05001156 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1157 dst_addr, burst, dev_width,
1158 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +05301159 if (ret < 0) {
1160 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -05001161 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001162 }
1163
Joel Fernandesfd009032013-09-23 18:05:13 -05001164 edesc->absync = ret;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001165 edesc->residue += sg_dma_len(sg);
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001166
1167 /* If this is the last in a current SG set of transactions,
1168 enable interrupts so that next set is processed */
1169 if (!((i+1) % MAX_NR_SG))
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001170 edesc->pset[i].param.opt |= TCINTEN;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001171
Matt Porterc2dde5f2012-08-22 21:09:34 -04001172 /* If this is the last set, enable completion interrupt flag */
1173 if (i == sg_len - 1)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001174 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001175 }
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001176 edesc->residue_stat = edesc->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001177
Matt Porterc2dde5f2012-08-22 21:09:34 -04001178 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1179}
Matt Porterc2dde5f2012-08-22 21:09:34 -04001180
Lad, Prabhakarb7a4fd52015-02-04 13:03:27 +00001181static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001182 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1183 size_t len, unsigned long tx_flags)
1184{
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001185 int ret, nslots;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001186 struct edma_desc *edesc;
1187 struct device *dev = chan->device->dev;
1188 struct edma_chan *echan = to_edma_chan(chan);
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001189 unsigned int width, pset_len;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001190
1191 if (unlikely(!echan || !len))
1192 return NULL;
1193
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001194 if (len < SZ_64K) {
1195 /*
1196 * Transfer size less than 64K can be handled with one paRAM
1197 * slot and with one burst.
1198 * ACNT = length
1199 */
1200 width = len;
1201 pset_len = len;
1202 nslots = 1;
1203 } else {
1204 /*
1205 * Transfer size bigger than 64K will be handled with maximum of
1206 * two paRAM slots.
1207 * slot1: (full_length / 32767) times 32767 bytes bursts.
1208 * ACNT = 32767, length1: (full_length / 32767) * 32767
1209 * slot2: the remaining amount of data after slot1.
1210 * ACNT = full_length - length1, length2 = ACNT
1211 *
1212 * When the full_length is multibple of 32767 one slot can be
1213 * used to complete the transfer.
1214 */
1215 width = SZ_32K - 1;
1216 pset_len = rounddown(len, width);
1217 /* One slot is enough for lengths multiple of (SZ_32K -1) */
1218 if (unlikely(pset_len == len))
1219 nslots = 1;
1220 else
1221 nslots = 2;
1222 }
1223
1224 edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]),
1225 GFP_ATOMIC);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001226 if (!edesc) {
1227 dev_dbg(dev, "Failed to allocate a descriptor\n");
1228 return NULL;
1229 }
1230
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001231 edesc->pset_nr = nslots;
1232 edesc->residue = edesc->residue_stat = len;
1233 edesc->direction = DMA_MEM_TO_MEM;
1234 edesc->echan = echan;
Peter Ujfalusi21a31842015-10-16 10:17:59 +03001235
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001236 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001237 width, pset_len, DMA_MEM_TO_MEM);
1238 if (ret < 0) {
1239 kfree(edesc);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001240 return NULL;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001241 }
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001242
1243 edesc->absync = ret;
1244
Joel Fernandesb0cce4c2014-04-28 15:30:32 -05001245 edesc->pset[0].param.opt |= ITCCHEN;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001246 if (nslots == 1) {
1247 /* Enable transfer complete interrupt */
1248 edesc->pset[0].param.opt |= TCINTEN;
1249 } else {
1250 /* Enable transfer complete chaining for the first slot */
1251 edesc->pset[0].param.opt |= TCCHEN;
1252
1253 if (echan->slot[1] < 0) {
1254 echan->slot[1] = edma_alloc_slot(echan->ecc,
1255 EDMA_SLOT_ANY);
1256 if (echan->slot[1] < 0) {
1257 kfree(edesc);
1258 dev_err(dev, "%s: Failed to allocate slot\n",
1259 __func__);
1260 return NULL;
1261 }
1262 }
1263 dest += pset_len;
1264 src += pset_len;
1265 pset_len = width = len % (SZ_32K - 1);
1266
1267 ret = edma_config_pset(chan, &edesc->pset[1], src, dest, 1,
1268 width, pset_len, DMA_MEM_TO_MEM);
1269 if (ret < 0) {
1270 kfree(edesc);
1271 return NULL;
1272 }
1273
1274 edesc->pset[1].param.opt |= ITCCHEN;
1275 edesc->pset[1].param.opt |= TCINTEN;
1276 }
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001277
1278 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1279}
1280
Joel Fernandes50a9c702013-10-31 16:31:23 -05001281static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
1282 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1283 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +02001284 unsigned long tx_flags)
Joel Fernandes50a9c702013-10-31 16:31:23 -05001285{
1286 struct edma_chan *echan = to_edma_chan(chan);
1287 struct device *dev = chan->device->dev;
1288 struct edma_desc *edesc;
1289 dma_addr_t src_addr, dst_addr;
1290 enum dma_slave_buswidth dev_width;
1291 u32 burst;
1292 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001293
Joel Fernandes50a9c702013-10-31 16:31:23 -05001294 if (unlikely(!echan || !buf_len || !period_len))
1295 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001296
Joel Fernandes50a9c702013-10-31 16:31:23 -05001297 if (direction == DMA_DEV_TO_MEM) {
1298 src_addr = echan->cfg.src_addr;
1299 dst_addr = buf_addr;
1300 dev_width = echan->cfg.src_addr_width;
1301 burst = echan->cfg.src_maxburst;
1302 } else if (direction == DMA_MEM_TO_DEV) {
1303 src_addr = buf_addr;
1304 dst_addr = echan->cfg.dst_addr;
1305 dev_width = echan->cfg.dst_addr_width;
1306 burst = echan->cfg.dst_maxburst;
1307 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001308 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001309 return NULL;
1310 }
1311
1312 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001313 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001314 return NULL;
1315 }
1316
1317 if (unlikely(buf_len % period_len)) {
1318 dev_err(dev, "Period should be multiple of Buffer length\n");
1319 return NULL;
1320 }
1321
1322 nslots = (buf_len / period_len) + 1;
1323
1324 /*
1325 * Cyclic DMA users such as audio cannot tolerate delays introduced
1326 * by cases where the number of periods is more than the maximum
1327 * number of SGs the EDMA driver can handle at a time. For DMA types
1328 * such as Slave SGs, such delays are tolerable and synchronized,
1329 * but the synchronization is difficult to achieve with Cyclic and
1330 * cannot be guaranteed, so we error out early.
1331 */
1332 if (nslots > MAX_NR_SG)
1333 return NULL;
1334
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001335 edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]),
1336 GFP_ATOMIC);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001337 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001338 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001339 return NULL;
1340 }
1341
1342 edesc->cyclic = 1;
1343 edesc->pset_nr = nslots;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001344 edesc->residue = edesc->residue_stat = buf_len;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001345 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001346 edesc->echan = echan;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001347
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001348 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
1349 __func__, echan->ch_num, nslots, period_len, buf_len);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001350
1351 for (i = 0; i < nslots; i++) {
1352 /* Allocate a PaRAM slot, if needed */
1353 if (echan->slot[i] < 0) {
1354 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001355 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001356 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001357 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001358 dev_err(dev, "%s: Failed to allocate slot\n",
1359 __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001360 return NULL;
1361 }
1362 }
1363
1364 if (i == nslots - 1) {
1365 memcpy(&edesc->pset[i], &edesc->pset[0],
1366 sizeof(edesc->pset[0]));
1367 break;
1368 }
1369
1370 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1371 dst_addr, burst, dev_width, period_len,
1372 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001373 if (ret < 0) {
1374 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001375 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001376 }
Joel Fernandes50a9c702013-10-31 16:31:23 -05001377
1378 if (direction == DMA_DEV_TO_MEM)
1379 dst_addr += period_len;
1380 else
1381 src_addr += period_len;
1382
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001383 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
1384 dev_vdbg(dev,
Joel Fernandes50a9c702013-10-31 16:31:23 -05001385 "\n pset[%d]:\n"
1386 " chnum\t%d\n"
1387 " slot\t%d\n"
1388 " opt\t%08x\n"
1389 " src\t%08x\n"
1390 " dst\t%08x\n"
1391 " abcnt\t%08x\n"
1392 " ccnt\t%08x\n"
1393 " bidx\t%08x\n"
1394 " cidx\t%08x\n"
1395 " lkrld\t%08x\n",
1396 i, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001397 edesc->pset[i].param.opt,
1398 edesc->pset[i].param.src,
1399 edesc->pset[i].param.dst,
1400 edesc->pset[i].param.a_b_cnt,
1401 edesc->pset[i].param.ccnt,
1402 edesc->pset[i].param.src_dst_bidx,
1403 edesc->pset[i].param.src_dst_cidx,
1404 edesc->pset[i].param.link_bcntrld);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001405
1406 edesc->absync = ret;
1407
1408 /*
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001409 * Enable period interrupt only if it is requested
Joel Fernandes50a9c702013-10-31 16:31:23 -05001410 */
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001411 if (tx_flags & DMA_PREP_INTERRUPT)
1412 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001413 }
1414
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001415 /* Place the cyclic channel to highest priority queue */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001416 edma_assign_channel_eventq(echan, EVENTQ_0);
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001417
Matt Porterc2dde5f2012-08-22 21:09:34 -04001418 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1419}
1420
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001421static void edma_completion_handler(struct edma_chan *echan)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001422{
Matt Porterc2dde5f2012-08-22 21:09:34 -04001423 struct device *dev = echan->vchan.chan.device->dev;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001424 struct edma_desc *edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001425
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001426 if (!edesc)
1427 return;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001428
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001429 spin_lock(&echan->vchan.lock);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001430 if (edesc->cyclic) {
1431 vchan_cyclic_callback(&edesc->vdesc);
1432 spin_unlock(&echan->vchan.lock);
1433 return;
1434 } else if (edesc->processed == edesc->pset_nr) {
1435 edesc->residue = 0;
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001436 edma_stop(echan);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001437 vchan_cookie_complete(&edesc->vdesc);
1438 echan->edesc = NULL;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001439
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001440 dev_dbg(dev, "Transfer completed on channel %d\n",
1441 echan->ch_num);
1442 } else {
1443 dev_dbg(dev, "Sub transfer completed on channel %d\n",
1444 echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001445
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001446 edma_pause(echan);
Joel Fernandesc5f47992013-08-29 18:05:43 -05001447
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001448 /* Update statistics for tx_status */
1449 edesc->residue -= edesc->sg_len;
1450 edesc->residue_stat = edesc->residue;
1451 edesc->processed_stat = edesc->processed;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001452 }
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001453 edma_execute(echan);
1454
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001455 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001456}
1457
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001458/* eDMA interrupt handler */
1459static irqreturn_t dma_irq_handler(int irq, void *data)
1460{
1461 struct edma_cc *ecc = data;
1462 int ctlr;
1463 u32 sh_ier;
1464 u32 sh_ipr;
1465 u32 bank;
1466
1467 ctlr = ecc->id;
1468 if (ctlr < 0)
1469 return IRQ_NONE;
1470
1471 dev_vdbg(ecc->dev, "dma_irq_handler\n");
1472
1473 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 0);
1474 if (!sh_ipr) {
1475 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 1);
1476 if (!sh_ipr)
1477 return IRQ_NONE;
1478 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 1);
1479 bank = 1;
1480 } else {
1481 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 0);
1482 bank = 0;
1483 }
1484
1485 do {
1486 u32 slot;
1487 u32 channel;
1488
1489 slot = __ffs(sh_ipr);
1490 sh_ipr &= ~(BIT(slot));
1491
1492 if (sh_ier & BIT(slot)) {
1493 channel = (bank << 5) | slot;
1494 /* Clear the corresponding IPR bits */
1495 edma_shadow0_write_array(ecc, SH_ICR, bank, BIT(slot));
1496 edma_completion_handler(&ecc->slave_chans[channel]);
1497 }
1498 } while (sh_ipr);
1499
1500 edma_shadow0_write(ecc, SH_IEVAL, 1);
1501 return IRQ_HANDLED;
1502}
1503
1504static void edma_error_handler(struct edma_chan *echan)
1505{
1506 struct edma_cc *ecc = echan->ecc;
1507 struct device *dev = echan->vchan.chan.device->dev;
1508 struct edmacc_param p;
1509
1510 if (!echan->edesc)
1511 return;
1512
1513 spin_lock(&echan->vchan.lock);
1514
1515 edma_read_slot(ecc, echan->slot[0], &p);
1516 /*
1517 * Issue later based on missed flag which will be sure
1518 * to happen as:
1519 * (1) we finished transmitting an intermediate slot and
1520 * edma_execute is coming up.
1521 * (2) or we finished current transfer and issue will
1522 * call edma_execute.
1523 *
1524 * Important note: issuing can be dangerous here and
1525 * lead to some nasty recursion when we are in a NULL
1526 * slot. So we avoid doing so and set the missed flag.
1527 */
1528 if (p.a_b_cnt == 0 && p.ccnt == 0) {
1529 dev_dbg(dev, "Error on null slot, setting miss\n");
1530 echan->missed = 1;
1531 } else {
1532 /*
1533 * The slot is already programmed but the event got
1534 * missed, so its safe to issue it here.
1535 */
1536 dev_dbg(dev, "Missed event, TRIGGERING\n");
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001537 edma_clean_channel(echan);
1538 edma_stop(echan);
1539 edma_start(echan);
1540 edma_trigger_channel(echan);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001541 }
1542 spin_unlock(&echan->vchan.lock);
1543}
1544
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001545static inline bool edma_error_pending(struct edma_cc *ecc)
1546{
1547 if (edma_read_array(ecc, EDMA_EMR, 0) ||
1548 edma_read_array(ecc, EDMA_EMR, 1) ||
1549 edma_read(ecc, EDMA_QEMR) || edma_read(ecc, EDMA_CCERR))
1550 return true;
1551
1552 return false;
1553}
1554
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001555/* eDMA error interrupt handler */
1556static irqreturn_t dma_ccerr_handler(int irq, void *data)
1557{
1558 struct edma_cc *ecc = data;
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001559 int i, j;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001560 int ctlr;
1561 unsigned int cnt = 0;
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001562 unsigned int val;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001563
1564 ctlr = ecc->id;
1565 if (ctlr < 0)
1566 return IRQ_NONE;
1567
1568 dev_vdbg(ecc->dev, "dma_ccerr_handler\n");
1569
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001570 if (!edma_error_pending(ecc))
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001571 return IRQ_NONE;
1572
1573 while (1) {
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001574 /* Event missed register(s) */
1575 for (j = 0; j < 2; j++) {
1576 unsigned long emr;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001577
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001578 val = edma_read_array(ecc, EDMA_EMR, j);
1579 if (!val)
1580 continue;
1581
1582 dev_dbg(ecc->dev, "EMR%d 0x%08x\n", j, val);
1583 emr = val;
1584 for (i = find_next_bit(&emr, 32, 0); i < 32;
1585 i = find_next_bit(&emr, 32, i + 1)) {
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001586 int k = (j << 5) + i;
1587
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001588 /* Clear the corresponding EMR bits */
1589 edma_write_array(ecc, EDMA_EMCR, j, BIT(i));
1590 /* Clear any SER */
1591 edma_shadow0_write_array(ecc, SH_SECR, j,
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001592 BIT(i));
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001593 edma_error_handler(&ecc->slave_chans[k]);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001594 }
1595 }
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001596
1597 val = edma_read(ecc, EDMA_QEMR);
1598 if (val) {
1599 dev_dbg(ecc->dev, "QEMR 0x%02x\n", val);
1600 /* Not reported, just clear the interrupt reason. */
1601 edma_write(ecc, EDMA_QEMCR, val);
1602 edma_shadow0_write(ecc, SH_QSECR, val);
1603 }
1604
1605 val = edma_read(ecc, EDMA_CCERR);
1606 if (val) {
1607 dev_warn(ecc->dev, "CCERR 0x%08x\n", val);
1608 /* Not reported, just clear the interrupt reason. */
1609 edma_write(ecc, EDMA_CCERRCLR, val);
1610 }
1611
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001612 if (!edma_error_pending(ecc))
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001613 break;
1614 cnt++;
1615 if (cnt > 10)
1616 break;
1617 }
1618 edma_write(ecc, EDMA_EEVAL, 1);
1619 return IRQ_HANDLED;
1620}
1621
Matt Porterc2dde5f2012-08-22 21:09:34 -04001622/* Alloc channel resources */
1623static int edma_alloc_chan_resources(struct dma_chan *chan)
1624{
1625 struct edma_chan *echan = to_edma_chan(chan);
1626 struct device *dev = chan->device->dev;
1627 int ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001628
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001629 ret = edma_alloc_channel(echan, EVENTQ_DEFAULT);
1630 if (ret)
1631 return ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001632
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001633 echan->slot[0] = edma_alloc_slot(echan->ecc, echan->ch_num);
1634 if (echan->slot[0] < 0) {
1635 dev_err(dev, "Entry slot allocation failed for channel %u\n",
1636 EDMA_CHAN_SLOT(echan->ch_num));
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001637 goto err_slot;
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001638 }
1639
1640 /* Set up channel -> slot mapping for the entry slot */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001641 edma_set_chmap(echan, echan->slot[0]);
1642 echan->alloced = true;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001643
Peter Ujfalusi9aac9092014-04-24 10:29:50 +03001644 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
Ezequiel Garcia0e772c62013-12-13 11:06:18 -03001645 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
Matt Porterc2dde5f2012-08-22 21:09:34 -04001646
1647 return 0;
1648
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001649err_slot:
1650 edma_free_channel(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001651 return ret;
1652}
1653
1654/* Free channel resources */
1655static void edma_free_chan_resources(struct dma_chan *chan)
1656{
1657 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001658 int i;
1659
1660 /* Terminate transfers */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001661 edma_stop(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001662
1663 vchan_free_chan_resources(&echan->vchan);
1664
1665 /* Free EDMA PaRAM slots */
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001666 for (i = 0; i < EDMA_MAX_SLOTS; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001667 if (echan->slot[i] >= 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001668 edma_free_slot(echan->ecc, echan->slot[i]);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001669 echan->slot[i] = -1;
1670 }
1671 }
1672
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001673 /* Set entry slot to the dummy slot */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001674 edma_set_chmap(echan, echan->ecc->dummy_slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001675
Matt Porterc2dde5f2012-08-22 21:09:34 -04001676 /* Free EDMA channel */
1677 if (echan->alloced) {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001678 edma_free_channel(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001679 echan->alloced = false;
1680 }
1681
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03001682 dev_dbg(chan->device->dev, "freeing channel for %u\n", echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001683}
1684
1685/* Send pending descriptor to hardware */
1686static void edma_issue_pending(struct dma_chan *chan)
1687{
1688 struct edma_chan *echan = to_edma_chan(chan);
1689 unsigned long flags;
1690
1691 spin_lock_irqsave(&echan->vchan.lock, flags);
1692 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
1693 edma_execute(echan);
1694 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1695}
1696
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001697static u32 edma_residue(struct edma_desc *edesc)
1698{
1699 bool dst = edesc->direction == DMA_DEV_TO_MEM;
1700 struct edma_pset *pset = edesc->pset;
1701 dma_addr_t done, pos;
1702 int i;
1703
1704 /*
1705 * We always read the dst/src position from the first RamPar
1706 * pset. That's the one which is active now.
1707 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001708 pos = edma_get_position(edesc->echan->ecc, edesc->echan->slot[0], dst);
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001709
1710 /*
1711 * Cyclic is simple. Just subtract pset[0].addr from pos.
1712 *
1713 * We never update edesc->residue in the cyclic case, so we
1714 * can tell the remaining room to the end of the circular
1715 * buffer.
1716 */
1717 if (edesc->cyclic) {
1718 done = pos - pset->addr;
1719 edesc->residue_stat = edesc->residue - done;
1720 return edesc->residue_stat;
1721 }
1722
1723 /*
1724 * For SG operation we catch up with the last processed
1725 * status.
1726 */
1727 pset += edesc->processed_stat;
1728
1729 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
1730 /*
1731 * If we are inside this pset address range, we know
1732 * this is the active one. Get the current delta and
1733 * stop walking the psets.
1734 */
1735 if (pos >= pset->addr && pos < pset->addr + pset->len)
1736 return edesc->residue_stat - (pos - pset->addr);
1737
1738 /* Otherwise mark it done and update residue_stat. */
1739 edesc->processed_stat++;
1740 edesc->residue_stat -= pset->len;
1741 }
1742 return edesc->residue_stat;
1743}
1744
Matt Porterc2dde5f2012-08-22 21:09:34 -04001745/* Check request completion status */
1746static enum dma_status edma_tx_status(struct dma_chan *chan,
1747 dma_cookie_t cookie,
1748 struct dma_tx_state *txstate)
1749{
1750 struct edma_chan *echan = to_edma_chan(chan);
1751 struct virt_dma_desc *vdesc;
1752 enum dma_status ret;
1753 unsigned long flags;
1754
1755 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +05301756 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001757 return ret;
1758
1759 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001760 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001761 txstate->residue = edma_residue(echan->edesc);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001762 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
1763 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001764 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1765
1766 return ret;
1767}
1768
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001769static void __init edma_chan_init(struct edma_cc *ecc, struct dma_device *dma,
Matt Porterc2dde5f2012-08-22 21:09:34 -04001770 struct edma_chan *echans)
1771{
1772 int i, j;
1773
Peter Ujfalusicb782052015-10-14 14:42:54 +03001774 for (i = 0; i < ecc->num_channels; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001775 struct edma_chan *echan = &echans[i];
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001776 echan->ch_num = EDMA_CTLR_CHAN(ecc->id, i);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001777 echan->ecc = ecc;
1778 echan->vchan.desc_free = edma_desc_free;
1779
1780 vchan_init(&echan->vchan, dma);
1781
1782 INIT_LIST_HEAD(&echan->node);
1783 for (j = 0; j < EDMA_MAX_SLOTS; j++)
1784 echan->slot[j] = -1;
1785 }
1786}
1787
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001788#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1789 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
Peter Ujfalusie4a899d2014-07-03 07:51:56 +03001790 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001791 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1792
Matt Porterc2dde5f2012-08-22 21:09:34 -04001793static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
1794 struct device *dev)
1795{
1796 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001797 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001798 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001799 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
1800 dma->device_free_chan_resources = edma_free_chan_resources;
1801 dma->device_issue_pending = edma_issue_pending;
1802 dma->device_tx_status = edma_tx_status;
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001803 dma->device_config = edma_slave_config;
1804 dma->device_pause = edma_dma_pause;
1805 dma->device_resume = edma_dma_resume;
1806 dma->device_terminate_all = edma_terminate_all;
Maxime Ripard9f59cd02014-11-17 14:42:47 +01001807
1808 dma->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1809 dma->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
1810 dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1811 dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1812
Matt Porterc2dde5f2012-08-22 21:09:34 -04001813 dma->dev = dev;
1814
1815 INIT_LIST_HEAD(&dma->channels);
1816}
1817
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001818static int edma_setup_from_hw(struct device *dev, struct edma_soc_info *pdata,
1819 struct edma_cc *ecc)
1820{
1821 int i;
1822 u32 value, cccfg;
1823 s8 (*queue_priority_map)[2];
1824
1825 /* Decode the eDMA3 configuration from CCCFG register */
1826 cccfg = edma_read(ecc, EDMA_CCCFG);
1827
1828 value = GET_NUM_REGN(cccfg);
1829 ecc->num_region = BIT(value);
1830
1831 value = GET_NUM_DMACH(cccfg);
1832 ecc->num_channels = BIT(value + 1);
1833
1834 value = GET_NUM_PAENTRY(cccfg);
1835 ecc->num_slots = BIT(value + 4);
1836
1837 value = GET_NUM_EVQUE(cccfg);
1838 ecc->num_tc = value + 1;
1839
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +03001840 ecc->chmap_exist = (cccfg & CHMAP_EXIST) ? true : false;
1841
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001842 dev_dbg(dev, "eDMA3 CC HW configuration (cccfg: 0x%08x):\n", cccfg);
1843 dev_dbg(dev, "num_region: %u\n", ecc->num_region);
1844 dev_dbg(dev, "num_channels: %u\n", ecc->num_channels);
1845 dev_dbg(dev, "num_slots: %u\n", ecc->num_slots);
1846 dev_dbg(dev, "num_tc: %u\n", ecc->num_tc);
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +03001847 dev_dbg(dev, "chmap_exist: %s\n", ecc->chmap_exist ? "yes" : "no");
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001848
1849 /* Nothing need to be done if queue priority is provided */
1850 if (pdata->queue_priority_mapping)
1851 return 0;
1852
1853 /*
1854 * Configure TC/queue priority as follows:
1855 * Q0 - priority 0
1856 * Q1 - priority 1
1857 * Q2 - priority 2
1858 * ...
1859 * The meaning of priority numbers: 0 highest priority, 7 lowest
1860 * priority. So Q0 is the highest priority queue and the last queue has
1861 * the lowest priority.
1862 */
Peter Ujfalusi547c6e22015-10-14 14:42:55 +03001863 queue_priority_map = devm_kcalloc(dev, ecc->num_tc + 1, sizeof(s8),
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001864 GFP_KERNEL);
1865 if (!queue_priority_map)
1866 return -ENOMEM;
1867
1868 for (i = 0; i < ecc->num_tc; i++) {
1869 queue_priority_map[i][0] = i;
1870 queue_priority_map[i][1] = i;
1871 }
1872 queue_priority_map[i][0] = -1;
1873 queue_priority_map[i][1] = -1;
1874
1875 pdata->queue_priority_mapping = queue_priority_map;
1876 /* Default queue has the lowest priority */
1877 pdata->default_queue = i - 1;
1878
1879 return 0;
1880}
1881
1882#if IS_ENABLED(CONFIG_OF)
1883static int edma_xbar_event_map(struct device *dev, struct edma_soc_info *pdata,
1884 size_t sz)
1885{
1886 const char pname[] = "ti,edma-xbar-event-map";
1887 struct resource res;
1888 void __iomem *xbar;
1889 s16 (*xbar_chans)[2];
1890 size_t nelm = sz / sizeof(s16);
1891 u32 shift, offset, mux;
1892 int ret, i;
1893
Peter Ujfalusi547c6e22015-10-14 14:42:55 +03001894 xbar_chans = devm_kcalloc(dev, nelm + 2, sizeof(s16), GFP_KERNEL);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001895 if (!xbar_chans)
1896 return -ENOMEM;
1897
1898 ret = of_address_to_resource(dev->of_node, 1, &res);
1899 if (ret)
1900 return -ENOMEM;
1901
1902 xbar = devm_ioremap(dev, res.start, resource_size(&res));
1903 if (!xbar)
1904 return -ENOMEM;
1905
1906 ret = of_property_read_u16_array(dev->of_node, pname, (u16 *)xbar_chans,
1907 nelm);
1908 if (ret)
1909 return -EIO;
1910
1911 /* Invalidate last entry for the other user of this mess */
1912 nelm >>= 1;
1913 xbar_chans[nelm][0] = -1;
1914 xbar_chans[nelm][1] = -1;
1915
1916 for (i = 0; i < nelm; i++) {
1917 shift = (xbar_chans[i][1] & 0x03) << 3;
1918 offset = xbar_chans[i][1] & 0xfffffffc;
1919 mux = readl(xbar + offset);
1920 mux &= ~(0xff << shift);
1921 mux |= xbar_chans[i][0] << shift;
1922 writel(mux, (xbar + offset));
1923 }
1924
1925 pdata->xbar_chans = (const s16 (*)[2]) xbar_chans;
1926 return 0;
1927}
1928
1929static int edma_of_parse_dt(struct device *dev, struct edma_soc_info *pdata)
1930{
1931 int ret = 0;
1932 struct property *prop;
1933 size_t sz;
1934 struct edma_rsv_info *rsv_info;
1935
1936 rsv_info = devm_kzalloc(dev, sizeof(struct edma_rsv_info), GFP_KERNEL);
1937 if (!rsv_info)
1938 return -ENOMEM;
1939 pdata->rsv = rsv_info;
1940
1941 prop = of_find_property(dev->of_node, "ti,edma-xbar-event-map", &sz);
1942 if (prop)
1943 ret = edma_xbar_event_map(dev, pdata, sz);
1944
1945 return ret;
1946}
1947
1948static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev)
1949{
1950 struct edma_soc_info *info;
1951 int ret;
1952
1953 info = devm_kzalloc(dev, sizeof(struct edma_soc_info), GFP_KERNEL);
1954 if (!info)
1955 return ERR_PTR(-ENOMEM);
1956
1957 ret = edma_of_parse_dt(dev, info);
1958 if (ret)
1959 return ERR_PTR(ret);
1960
1961 return info;
1962}
1963#else
1964static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev)
1965{
1966 return ERR_PTR(-EINVAL);
1967}
1968#endif
1969
Bill Pemberton463a1f82012-11-19 13:22:55 -05001970static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001971{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001972 struct edma_soc_info *info = pdev->dev.platform_data;
1973 s8 (*queue_priority_mapping)[2];
1974 int i, off, ln;
1975 const s16 (*rsv_chans)[2];
1976 const s16 (*rsv_slots)[2];
1977 const s16 (*xbar_chans)[2];
1978 int irq;
1979 char *irq_name;
1980 struct resource *mem;
1981 struct device_node *node = pdev->dev.of_node;
1982 struct device *dev = &pdev->dev;
1983 struct edma_cc *ecc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001984 int ret;
1985
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001986 if (node) {
1987 info = edma_setup_info_from_dt(dev);
1988 if (IS_ERR(info)) {
1989 dev_err(dev, "failed to get DT data\n");
1990 return PTR_ERR(info);
1991 }
1992 }
1993
1994 if (!info)
1995 return -ENODEV;
1996
1997 pm_runtime_enable(dev);
1998 ret = pm_runtime_get_sync(dev);
1999 if (ret < 0) {
2000 dev_err(dev, "pm_runtime_get_sync() failed\n");
2001 return ret;
2002 }
2003
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002004 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
Russell King94cb0e72013-06-27 13:45:16 +01002005 if (ret)
2006 return ret;
2007
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002008 ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002009 if (!ecc) {
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002010 dev_err(dev, "Can't allocate controller\n");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002011 return -ENOMEM;
2012 }
2013
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002014 ecc->dev = dev;
2015 ecc->id = pdev->id;
2016 /* When booting with DT the pdev->id is -1 */
2017 if (ecc->id < 0)
2018 ecc->id = 0;
Peter Ujfalusica304fa2015-10-14 14:42:49 +03002019
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002020 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma3_cc");
2021 if (!mem) {
2022 dev_dbg(dev, "mem resource not found, using index 0\n");
2023 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2024 if (!mem) {
2025 dev_err(dev, "no mem resource?\n");
2026 return -ENODEV;
2027 }
2028 }
2029 ecc->base = devm_ioremap_resource(dev, mem);
2030 if (IS_ERR(ecc->base))
2031 return PTR_ERR(ecc->base);
Peter Ujfalusib2c843a2015-10-14 14:42:50 +03002032
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002033 platform_set_drvdata(pdev, ecc);
2034
2035 /* Get eDMA3 configuration from IP */
2036 ret = edma_setup_from_hw(dev, info, ecc);
2037 if (ret)
2038 return ret;
2039
Peter Ujfalusicb782052015-10-14 14:42:54 +03002040 /* Allocate memory based on the information we got from the IP */
2041 ecc->slave_chans = devm_kcalloc(dev, ecc->num_channels,
2042 sizeof(*ecc->slave_chans), GFP_KERNEL);
2043 if (!ecc->slave_chans)
2044 return -ENOMEM;
2045
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002046 ecc->channel_unused = devm_kcalloc(dev,
2047 BITS_TO_LONGS(ecc->num_channels),
2048 sizeof(unsigned long), GFP_KERNEL);
2049 if (!ecc->channel_unused)
Peter Ujfalusicb782052015-10-14 14:42:54 +03002050 return -ENOMEM;
2051
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002052 ecc->slot_inuse = devm_kcalloc(dev, BITS_TO_LONGS(ecc->num_slots),
Peter Ujfalusicb782052015-10-14 14:42:54 +03002053 sizeof(unsigned long), GFP_KERNEL);
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002054 if (!ecc->slot_inuse)
Peter Ujfalusicb782052015-10-14 14:42:54 +03002055 return -ENOMEM;
2056
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002057 ecc->default_queue = info->default_queue;
2058
2059 for (i = 0; i < ecc->num_slots; i++)
2060 edma_write_slot(ecc, i, &dummy_paramset);
2061
2062 /* Mark all channels as unused */
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002063 memset(ecc->channel_unused, 0xff, sizeof(ecc->channel_unused));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002064
2065 if (info->rsv) {
2066 /* Clear the reserved channels in unused list */
2067 rsv_chans = info->rsv->rsv_chans;
2068 if (rsv_chans) {
2069 for (i = 0; rsv_chans[i][0] != -1; i++) {
2070 off = rsv_chans[i][0];
2071 ln = rsv_chans[i][1];
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002072 clear_bits(off, ln, ecc->channel_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002073 }
2074 }
2075
2076 /* Set the reserved slots in inuse list */
2077 rsv_slots = info->rsv->rsv_slots;
2078 if (rsv_slots) {
2079 for (i = 0; rsv_slots[i][0] != -1; i++) {
2080 off = rsv_slots[i][0];
2081 ln = rsv_slots[i][1];
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002082 set_bits(off, ln, ecc->slot_inuse);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002083 }
2084 }
2085 }
2086
2087 /* Clear the xbar mapped channels in unused list */
2088 xbar_chans = info->xbar_chans;
2089 if (xbar_chans) {
2090 for (i = 0; xbar_chans[i][1] != -1; i++) {
2091 off = xbar_chans[i][1];
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002092 clear_bits(off, 1, ecc->channel_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002093 }
2094 }
2095
2096 irq = platform_get_irq_byname(pdev, "edma3_ccint");
2097 if (irq < 0 && node)
2098 irq = irq_of_parse_and_map(node, 0);
2099
2100 if (irq >= 0) {
2101 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccint",
2102 dev_name(dev));
2103 ret = devm_request_irq(dev, irq, dma_irq_handler, 0, irq_name,
2104 ecc);
2105 if (ret) {
2106 dev_err(dev, "CCINT (%d) failed --> %d\n", irq, ret);
2107 return ret;
2108 }
2109 }
2110
2111 irq = platform_get_irq_byname(pdev, "edma3_ccerrint");
2112 if (irq < 0 && node)
2113 irq = irq_of_parse_and_map(node, 2);
2114
2115 if (irq >= 0) {
2116 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccerrint",
2117 dev_name(dev));
2118 ret = devm_request_irq(dev, irq, dma_ccerr_handler, 0, irq_name,
2119 ecc);
2120 if (ret) {
2121 dev_err(dev, "CCERRINT (%d) failed --> %d\n", irq, ret);
2122 return ret;
2123 }
2124 }
2125
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002126 ecc->dummy_slot = edma_alloc_slot(ecc, EDMA_SLOT_ANY);
2127 if (ecc->dummy_slot < 0) {
2128 dev_err(dev, "Can't allocate PaRAM dummy slot\n");
2129 return ecc->dummy_slot;
2130 }
2131
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002132 queue_priority_mapping = info->queue_priority_mapping;
2133
2134 /* Event queue priority mapping */
2135 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2136 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2137 queue_priority_mapping[i][1]);
2138
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002139 for (i = 0; i < ecc->num_region; i++) {
2140 edma_write_array2(ecc, EDMA_DRAE, i, 0, 0x0);
2141 edma_write_array2(ecc, EDMA_DRAE, i, 1, 0x0);
2142 edma_write_array(ecc, EDMA_QRAE, i, 0x0);
2143 }
2144 ecc->info = info;
2145
Matt Porterc2dde5f2012-08-22 21:09:34 -04002146 dma_cap_zero(ecc->dma_slave.cap_mask);
2147 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
Peter Ujfalusi232b223d2014-04-14 14:42:00 +03002148 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05002149 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002150
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002151 edma_dma_init(ecc, &ecc->dma_slave, dev);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002152
2153 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
2154
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002155 for (i = 0; i < ecc->num_channels; i++) {
2156 /* Assign all channels to the default queue */
2157 edma_map_dmach_to_queue(&ecc->slave_chans[i],
2158 info->default_queue);
2159 /* Set entry slot to the dummy slot */
2160 edma_set_chmap(&ecc->slave_chans[i], ecc->dummy_slot);
2161 }
2162
Matt Porterc2dde5f2012-08-22 21:09:34 -04002163 ret = dma_async_device_register(&ecc->dma_slave);
2164 if (ret)
2165 goto err_reg1;
2166
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002167 if (node)
2168 of_dma_controller_register(node, of_dma_xlate_by_chan_id,
Peter Ujfalusib2c843a2015-10-14 14:42:50 +03002169 &ecc->dma_slave);
Peter Ujfalusidc9b60552015-10-14 14:42:47 +03002170
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002171 dev_info(dev, "TI EDMA DMA engine driver\n");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002172
2173 return 0;
2174
2175err_reg1:
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002176 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002177 return ret;
2178}
2179
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08002180static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002181{
2182 struct device *dev = &pdev->dev;
2183 struct edma_cc *ecc = dev_get_drvdata(dev);
2184
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002185 if (dev->of_node)
2186 of_dma_controller_free(dev->of_node);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002187 dma_async_device_unregister(&ecc->dma_slave);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002188 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002189
2190 return 0;
2191}
2192
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002193#ifdef CONFIG_PM_SLEEP
2194static int edma_pm_resume(struct device *dev)
2195{
2196 struct edma_cc *ecc = dev_get_drvdata(dev);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002197 struct edma_chan *echan = ecc->slave_chans;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002198 int i;
2199 s8 (*queue_priority_mapping)[2];
2200
2201 queue_priority_mapping = ecc->info->queue_priority_mapping;
2202
2203 /* Event queue priority mapping */
2204 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2205 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2206 queue_priority_mapping[i][1]);
2207
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002208 for (i = 0; i < ecc->num_channels; i++) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002209 if (echan[i].alloced) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002210 /* ensure access through shadow region 0 */
2211 edma_or_array2(ecc, EDMA_DRAE, 0, i >> 5,
2212 BIT(i & 0x1f));
2213
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002214 edma_setup_interrupt(&echan[i], true);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002215
2216 /* Set up channel -> slot mapping for the entry slot */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002217 edma_set_chmap(&echan[i], echan[i].slot[0]);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002218 }
2219 }
2220
2221 return 0;
2222}
2223#endif
2224
2225static const struct dev_pm_ops edma_pm_ops = {
2226 SET_LATE_SYSTEM_SLEEP_PM_OPS(NULL, edma_pm_resume)
2227};
2228
Matt Porterc2dde5f2012-08-22 21:09:34 -04002229static struct platform_driver edma_driver = {
2230 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05002231 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002232 .driver = {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002233 .name = "edma",
2234 .pm = &edma_pm_ops,
2235 .of_match_table = edma_of_ids,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002236 },
2237};
2238
2239bool edma_filter_fn(struct dma_chan *chan, void *param)
2240{
2241 if (chan->device->dev->driver == &edma_driver.driver) {
2242 struct edma_chan *echan = to_edma_chan(chan);
2243 unsigned ch_req = *(unsigned *)param;
2244 return ch_req == echan->ch_num;
2245 }
2246 return false;
2247}
2248EXPORT_SYMBOL(edma_filter_fn);
2249
Matt Porterc2dde5f2012-08-22 21:09:34 -04002250static int edma_init(void)
2251{
Arnd Bergmann5305e4d2014-10-24 18:14:01 +02002252 return platform_driver_register(&edma_driver);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002253}
2254subsys_initcall(edma_init);
2255
2256static void __exit edma_exit(void)
2257{
Matt Porterc2dde5f2012-08-22 21:09:34 -04002258 platform_driver_unregister(&edma_driver);
2259}
2260module_exit(edma_exit);
2261
Josh Boyerd71505b2013-09-04 10:32:50 -04002262MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002263MODULE_DESCRIPTION("TI EDMA DMA engine driver");
2264MODULE_LICENSE("GPL v2");