blob: eaf1f9e4bde0c090961b75462292708e9d7e3ac2 [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 Ujfalusi2b6b3b72015-10-14 14:42:53 +0300394static void edma_assign_priority_to_queue(struct edma_cc *ecc, int queue_no,
395 int priority)
396{
397 int bit = queue_no * 4;
398
399 edma_modify(ecc, EDMA_QUEPRI, ~(0x7 << bit), ((priority & 0x7) << bit));
400}
401
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300402static void edma_set_chmap(struct edma_chan *echan, int slot)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300403{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300404 struct edma_cc *ecc = echan->ecc;
405 int channel = EDMA_CHAN_SLOT(echan->ch_num);
406
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300407 if (ecc->chmap_exist) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300408 slot = EDMA_CHAN_SLOT(slot);
409 edma_write_array(ecc, EDMA_DCHMAP, channel, (slot << 5));
410 }
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300411}
412
413static int prepare_unused_channel_list(struct device *dev, void *data)
414{
415 struct platform_device *pdev = to_platform_device(dev);
416 struct edma_cc *ecc = data;
Peter Ujfalusicb782052015-10-14 14:42:54 +0300417 int dma_req_min = EDMA_CTLR_CHAN(ecc->id, 0);
418 int dma_req_max = dma_req_min + ecc->num_channels;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300419 int i, count;
420 struct of_phandle_args dma_spec;
421
422 if (dev->of_node) {
423 struct platform_device *dma_pdev;
424
425 count = of_property_count_strings(dev->of_node, "dma-names");
426 if (count < 0)
427 return 0;
428 for (i = 0; i < count; i++) {
429 if (of_parse_phandle_with_args(dev->of_node, "dmas",
430 "#dma-cells", i,
431 &dma_spec))
432 continue;
433
434 if (!of_match_node(edma_of_ids, dma_spec.np)) {
435 of_node_put(dma_spec.np);
436 continue;
437 }
438
439 dma_pdev = of_find_device_by_node(dma_spec.np);
440 if (&dma_pdev->dev != ecc->dev)
441 continue;
442
443 clear_bit(EDMA_CHAN_SLOT(dma_spec.args[0]),
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300444 ecc->channel_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300445 of_node_put(dma_spec.np);
446 }
447 return 0;
448 }
449
450 /* For non-OF case */
451 for (i = 0; i < pdev->num_resources; i++) {
452 struct resource *res = &pdev->resource[i];
Peter Ujfalusicb782052015-10-14 14:42:54 +0300453 int dma_req;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300454
Peter Ujfalusicb782052015-10-14 14:42:54 +0300455 if (!(res->flags & IORESOURCE_DMA))
456 continue;
457
458 dma_req = (int)res->start;
459 if (dma_req >= dma_req_min && dma_req < dma_req_max)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300460 clear_bit(EDMA_CHAN_SLOT(pdev->resource[i].start),
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300461 ecc->channel_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300462 }
463
464 return 0;
465}
466
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300467static void edma_setup_interrupt(struct edma_chan *echan, bool enable)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300468{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300469 struct edma_cc *ecc = echan->ecc;
470 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300471
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300472 if (enable) {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300473 edma_shadow0_write_array(ecc, SH_ICR, channel >> 5,
474 BIT(channel & 0x1f));
475 edma_shadow0_write_array(ecc, SH_IESR, channel >> 5,
476 BIT(channel & 0x1f));
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300477 } else {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300478 edma_shadow0_write_array(ecc, SH_IECR, channel >> 5,
479 BIT(channel & 0x1f));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300480 }
481}
482
483/*
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300484 * paRAM slot management functions
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300485 */
486static void edma_write_slot(struct edma_cc *ecc, unsigned slot,
487 const struct edmacc_param *param)
488{
489 slot = EDMA_CHAN_SLOT(slot);
490 if (slot >= ecc->num_slots)
491 return;
492 memcpy_toio(ecc->base + PARM_OFFSET(slot), param, PARM_SIZE);
493}
494
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300495static void edma_read_slot(struct edma_cc *ecc, unsigned slot,
496 struct edmacc_param *param)
497{
498 slot = EDMA_CHAN_SLOT(slot);
499 if (slot >= ecc->num_slots)
500 return;
501 memcpy_fromio(param, ecc->base + PARM_OFFSET(slot), PARM_SIZE);
502}
503
504/**
505 * edma_alloc_slot - allocate DMA parameter RAM
506 * @ecc: pointer to edma_cc struct
507 * @slot: specific slot to allocate; negative for "any unused slot"
508 *
509 * This allocates a parameter RAM slot, initializing it to hold a
510 * dummy transfer. Slots allocated using this routine have not been
511 * mapped to a hardware DMA channel, and will normally be used by
512 * linking to them from a slot associated with a DMA channel.
513 *
514 * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
515 * slots may be allocated on behalf of DSP firmware.
516 *
517 * Returns the number of the slot, else negative errno.
518 */
519static int edma_alloc_slot(struct edma_cc *ecc, int slot)
520{
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300521 if (slot > 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300522 slot = EDMA_CHAN_SLOT(slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300523 /* Requesting entry paRAM slot for a HW triggered channel. */
524 if (ecc->chmap_exist && slot < ecc->num_channels)
525 slot = EDMA_SLOT_ANY;
526 }
527
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300528 if (slot < 0) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300529 if (ecc->chmap_exist)
530 slot = 0;
531 else
532 slot = ecc->num_channels;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300533 for (;;) {
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300534 slot = find_next_zero_bit(ecc->slot_inuse,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300535 ecc->num_slots,
536 slot);
537 if (slot == ecc->num_slots)
538 return -ENOMEM;
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300539 if (!test_and_set_bit(slot, ecc->slot_inuse))
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300540 break;
541 }
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300542 } else if (slot >= ecc->num_slots) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300543 return -EINVAL;
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300544 } else if (test_and_set_bit(slot, ecc->slot_inuse)) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300545 return -EBUSY;
546 }
547
548 edma_write_slot(ecc, slot, &dummy_paramset);
549
550 return EDMA_CTLR_CHAN(ecc->id, slot);
551}
552
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300553static void edma_free_slot(struct edma_cc *ecc, unsigned slot)
554{
555 slot = EDMA_CHAN_SLOT(slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300556 if (slot >= ecc->num_slots)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300557 return;
558
559 edma_write_slot(ecc, slot, &dummy_paramset);
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300560 clear_bit(slot, ecc->slot_inuse);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300561}
562
563/**
564 * edma_link - link one parameter RAM slot to another
565 * @ecc: pointer to edma_cc struct
566 * @from: parameter RAM slot originating the link
567 * @to: parameter RAM slot which is the link target
568 *
569 * The originating slot should not be part of any active DMA transfer.
570 */
571static void edma_link(struct edma_cc *ecc, unsigned from, unsigned to)
572{
Peter Ujfalusifc014092015-10-14 14:42:59 +0300573 if (unlikely(EDMA_CTLR(from) != EDMA_CTLR(to)))
574 dev_warn(ecc->dev, "Ignoring eDMA instance for linking\n");
575
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300576 from = EDMA_CHAN_SLOT(from);
577 to = EDMA_CHAN_SLOT(to);
578 if (from >= ecc->num_slots || to >= ecc->num_slots)
579 return;
580
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300581 edma_param_modify(ecc, PARM_LINK_BCNTRLD, from, 0xffff0000,
582 PARM_OFFSET(to));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300583}
584
585/**
586 * edma_get_position - returns the current transfer point
587 * @ecc: pointer to edma_cc struct
588 * @slot: parameter RAM slot being examined
589 * @dst: true selects the dest position, false the source
590 *
591 * Returns the position of the current active slot
592 */
593static dma_addr_t edma_get_position(struct edma_cc *ecc, unsigned slot,
594 bool dst)
595{
596 u32 offs;
597
598 slot = EDMA_CHAN_SLOT(slot);
599 offs = PARM_OFFSET(slot);
600 offs += dst ? PARM_DST : PARM_SRC;
601
602 return edma_read(ecc, offs);
603}
604
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300605/*
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300606 * Channels with event associations will be triggered by their hardware
607 * events, and channels without such associations will be triggered by
608 * software. (At this writing there is no interface for using software
609 * triggers except with channels that don't support hardware triggers.)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300610 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300611static void edma_start(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300612{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300613 struct edma_cc *ecc = echan->ecc;
614 int channel = EDMA_CHAN_SLOT(echan->ch_num);
615 int j = (channel >> 5);
616 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300617
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300618 if (test_bit(channel, ecc->channel_unused)) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300619 /* EDMA channels without event association */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300620 dev_dbg(ecc->dev, "ESR%d %08x\n", j,
621 edma_shadow0_read_array(ecc, SH_ESR, j));
622 edma_shadow0_write_array(ecc, SH_ESR, j, mask);
623 } else {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300624 /* EDMA channel with event association */
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300625 dev_dbg(ecc->dev, "ER%d %08x\n", j,
626 edma_shadow0_read_array(ecc, SH_ER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300627 /* Clear any pending event or error */
628 edma_write_array(ecc, EDMA_ECR, j, mask);
629 edma_write_array(ecc, EDMA_EMCR, j, mask);
630 /* Clear any SER */
631 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
632 edma_shadow0_write_array(ecc, SH_EESR, j, mask);
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300633 dev_dbg(ecc->dev, "EER%d %08x\n", j,
634 edma_shadow0_read_array(ecc, SH_EER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300635 }
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300636}
637
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300638static void edma_stop(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300639{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300640 struct edma_cc *ecc = echan->ecc;
641 int channel = EDMA_CHAN_SLOT(echan->ch_num);
642 int j = (channel >> 5);
643 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300644
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300645 edma_shadow0_write_array(ecc, SH_EECR, j, mask);
646 edma_shadow0_write_array(ecc, SH_ECR, j, mask);
647 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
648 edma_write_array(ecc, EDMA_EMCR, j, mask);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300649
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300650 /* clear possibly pending completion interrupt */
651 edma_shadow0_write_array(ecc, SH_ICR, j, mask);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300652
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300653 dev_dbg(ecc->dev, "EER%d %08x\n", j,
654 edma_shadow0_read_array(ecc, SH_EER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300655
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300656 /* REVISIT: consider guarding against inappropriate event
657 * chaining by overwriting with dummy_paramset.
658 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300659}
660
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300661/*
662 * Temporarily disable EDMA hardware events on the specified channel,
663 * preventing them from triggering new transfers
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300664 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300665static void edma_pause(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300666{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300667 int channel = EDMA_CHAN_SLOT(echan->ch_num);
668 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300669
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300670 edma_shadow0_write_array(echan->ecc, SH_EECR, channel >> 5, mask);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300671}
672
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300673/* Re-enable EDMA hardware events on the specified channel. */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300674static void edma_resume(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300675{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300676 int channel = EDMA_CHAN_SLOT(echan->ch_num);
677 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300678
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300679 edma_shadow0_write_array(echan->ecc, SH_EESR, channel >> 5, mask);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300680}
681
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300682static void edma_trigger_channel(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300683{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300684 struct edma_cc *ecc = echan->ecc;
685 int channel = EDMA_CHAN_SLOT(echan->ch_num);
686 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300687
688 edma_shadow0_write_array(ecc, SH_ESR, (channel >> 5), mask);
689
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300690 dev_dbg(ecc->dev, "ESR%d %08x\n", (channel >> 5),
691 edma_shadow0_read_array(ecc, SH_ESR, (channel >> 5)));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300692}
693
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300694static void edma_clean_channel(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300695{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300696 struct edma_cc *ecc = echan->ecc;
697 int channel = EDMA_CHAN_SLOT(echan->ch_num);
698 int j = (channel >> 5);
699 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300700
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300701 dev_dbg(ecc->dev, "EMR%d %08x\n", j, edma_read_array(ecc, EDMA_EMR, j));
702 edma_shadow0_write_array(ecc, SH_ECR, j, mask);
703 /* Clear the corresponding EMR bits */
704 edma_write_array(ecc, EDMA_EMCR, j, mask);
705 /* Clear any SER */
706 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
707 edma_write(ecc, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300708}
709
Peter Ujfalusif9425de2015-10-16 10:18:03 +0300710/* Move channel to a specific event queue */
711static void edma_assign_channel_eventq(struct edma_chan *echan,
712 enum dma_event_q eventq_no)
713{
714 struct edma_cc *ecc = echan->ecc;
715 int channel = EDMA_CHAN_SLOT(echan->ch_num);
716 int bit = (channel & 0x7) * 4;
717
718 /* default to low priority queue */
719 if (eventq_no == EVENTQ_DEFAULT)
720 eventq_no = ecc->default_queue;
721 if (eventq_no >= ecc->num_tc)
722 return;
723
724 eventq_no &= 7;
725 edma_modify_array(ecc, EDMA_DMAQNUM, (channel >> 3), ~(0x7 << bit),
726 eventq_no << bit);
727}
728
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300729static int edma_alloc_channel(struct edma_chan *echan,
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300730 enum dma_event_q eventq_no)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300731{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300732 struct edma_cc *ecc = echan->ecc;
733 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300734
735 if (!ecc->unused_chan_list_done) {
736 /*
737 * Scan all the platform devices to find out the EDMA channels
738 * used and clear them in the unused list, making the rest
739 * available for ARM usage.
740 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300741 int ret = bus_for_each_dev(&platform_bus_type, NULL, ecc,
742 prepare_unused_channel_list);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300743 if (ret < 0)
744 return ret;
745
746 ecc->unused_chan_list_done = true;
747 }
748
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300749 /* ensure access through shadow region 0 */
750 edma_or_array2(ecc, EDMA_DRAE, 0, channel >> 5, BIT(channel & 0x1f));
751
752 /* ensure no events are pending */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300753 edma_stop(echan);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300754
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300755 edma_setup_interrupt(echan, true);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300756
Peter Ujfalusif9425de2015-10-16 10:18:03 +0300757 edma_assign_channel_eventq(echan, eventq_no);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300758
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300759 return 0;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300760}
761
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300762static void edma_free_channel(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300763{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300764 /* ensure no events are pending */
765 edma_stop(echan);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300766 /* REVISIT should probably take out of shadow region 0 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300767 edma_setup_interrupt(echan, false);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300768}
769
Matt Porterc2dde5f2012-08-22 21:09:34 -0400770static inline struct edma_cc *to_edma_cc(struct dma_device *d)
771{
772 return container_of(d, struct edma_cc, dma_slave);
773}
774
775static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
776{
777 return container_of(c, struct edma_chan, vchan.chan);
778}
779
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300780static inline struct edma_desc *to_edma_desc(struct dma_async_tx_descriptor *tx)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400781{
782 return container_of(tx, struct edma_desc, vdesc.tx);
783}
784
785static void edma_desc_free(struct virt_dma_desc *vdesc)
786{
787 kfree(container_of(vdesc, struct edma_desc, vdesc));
788}
789
790/* Dispatch a queued descriptor to the controller (caller holds lock) */
791static void edma_execute(struct edma_chan *echan)
792{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300793 struct edma_cc *ecc = echan->ecc;
Joel Fernandes53407062013-09-03 10:02:46 -0500794 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400795 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500796 struct device *dev = echan->vchan.chan.device->dev;
797 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400798
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300799 if (!echan->edesc) {
800 /* Setup is needed for the first transfer */
Joel Fernandes53407062013-09-03 10:02:46 -0500801 vdesc = vchan_next_desc(&echan->vchan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300802 if (!vdesc)
Joel Fernandes53407062013-09-03 10:02:46 -0500803 return;
Joel Fernandes53407062013-09-03 10:02:46 -0500804 list_del(&vdesc->node);
805 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400806 }
807
Joel Fernandes53407062013-09-03 10:02:46 -0500808 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400809
Joel Fernandes53407062013-09-03 10:02:46 -0500810 /* Find out how many left */
811 left = edesc->pset_nr - edesc->processed;
812 nslots = min(MAX_NR_SG, left);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500813 edesc->sg_len = 0;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400814
815 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500816 for (i = 0; i < nslots; i++) {
817 j = i + edesc->processed;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300818 edma_write_slot(ecc, echan->slot[i], &edesc->pset[j].param);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500819 edesc->sg_len += edesc->pset[j].len;
Peter Ujfalusi907f74a2015-10-14 14:42:56 +0300820 dev_vdbg(dev,
821 "\n pset[%d]:\n"
822 " chnum\t%d\n"
823 " slot\t%d\n"
824 " opt\t%08x\n"
825 " src\t%08x\n"
826 " dst\t%08x\n"
827 " abcnt\t%08x\n"
828 " ccnt\t%08x\n"
829 " bidx\t%08x\n"
830 " cidx\t%08x\n"
831 " lkrld\t%08x\n",
832 j, echan->ch_num, echan->slot[i],
833 edesc->pset[j].param.opt,
834 edesc->pset[j].param.src,
835 edesc->pset[j].param.dst,
836 edesc->pset[j].param.a_b_cnt,
837 edesc->pset[j].param.ccnt,
838 edesc->pset[j].param.src_dst_bidx,
839 edesc->pset[j].param.src_dst_cidx,
840 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400841 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500842 if (i != (nslots - 1))
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300843 edma_link(ecc, echan->slot[i], echan->slot[i + 1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400844 }
845
Joel Fernandes53407062013-09-03 10:02:46 -0500846 edesc->processed += nslots;
847
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500848 /*
849 * If this is either the last set in a set of SG-list transactions
850 * then setup a link to the dummy slot, this results in all future
851 * events being absorbed and that's OK because we're done
852 */
Joel Fernandes50a9c702013-10-31 16:31:23 -0500853 if (edesc->processed == edesc->pset_nr) {
854 if (edesc->cyclic)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300855 edma_link(ecc, echan->slot[nslots - 1], echan->slot[1]);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500856 else
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300857 edma_link(ecc, echan->slot[nslots - 1],
Joel Fernandes50a9c702013-10-31 16:31:23 -0500858 echan->ecc->dummy_slot);
859 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500860
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300861 if (echan->missed) {
862 /*
863 * This happens due to setup times between intermediate
864 * transfers in long SG lists which have to be broken up into
865 * transfers of MAX_NR_SG
866 */
867 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300868 edma_clean_channel(echan);
869 edma_stop(echan);
870 edma_start(echan);
871 edma_trigger_channel(echan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300872 echan->missed = 0;
873 } else if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300874 dev_dbg(dev, "first transfer starting on channel %d\n",
875 echan->ch_num);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300876 edma_start(echan);
Sekhar Nori5fc68a62014-03-19 11:25:50 +0530877 } else {
878 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
879 echan->ch_num, edesc->processed);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300880 edma_resume(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500881 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400882}
883
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100884static int edma_terminate_all(struct dma_chan *chan)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400885{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100886 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400887 unsigned long flags;
888 LIST_HEAD(head);
889
890 spin_lock_irqsave(&echan->vchan.lock, flags);
891
892 /*
893 * Stop DMA activity: we assume the callback will not be called
894 * after edma_dma() returns (even if it does, it will see
895 * echan->edesc is NULL and exit.)
896 */
897 if (echan->edesc) {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300898 edma_stop(echan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300899 /* Move the cyclic channel back to default queue */
900 if (echan->edesc->cyclic)
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300901 edma_assign_channel_eventq(echan, EVENTQ_DEFAULT);
Petr Kulhavy5ca9e7c2015-03-27 13:35:51 +0200902 /*
903 * free the running request descriptor
904 * since it is not in any of the vdesc lists
905 */
906 edma_desc_free(&echan->edesc->vdesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400907 echan->edesc = NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400908 }
909
910 vchan_get_all_descriptors(&echan->vchan, &head);
911 spin_unlock_irqrestore(&echan->vchan.lock, flags);
912 vchan_dma_desc_free_list(&echan->vchan, &head);
913
914 return 0;
915}
916
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100917static int edma_slave_config(struct dma_chan *chan,
Matt Porter661f7cb2013-01-10 13:41:04 -0500918 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400919{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100920 struct edma_chan *echan = to_edma_chan(chan);
921
Matt Porter661f7cb2013-01-10 13:41:04 -0500922 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
923 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400924 return -EINVAL;
925
Matt Porter661f7cb2013-01-10 13:41:04 -0500926 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400927
928 return 0;
929}
930
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100931static int edma_dma_pause(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300932{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100933 struct edma_chan *echan = to_edma_chan(chan);
934
John Ogness02ec6042015-04-27 13:52:25 +0200935 if (!echan->edesc)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300936 return -EINVAL;
937
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300938 edma_pause(echan);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300939 return 0;
940}
941
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100942static int edma_dma_resume(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300943{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100944 struct edma_chan *echan = to_edma_chan(chan);
945
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300946 edma_resume(echan);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300947 return 0;
948}
949
Joel Fernandesfd009032013-09-23 18:05:13 -0500950/*
951 * A PaRAM set configuration abstraction used by other modes
952 * @chan: Channel who's PaRAM set we're configuring
953 * @pset: PaRAM set to initialize and setup.
954 * @src_addr: Source address of the DMA
955 * @dst_addr: Destination address of the DMA
956 * @burst: In units of dev_width, how much to send
957 * @dev_width: How much is the dev_width
958 * @dma_length: Total length of the DMA transfer
959 * @direction: Direction of the transfer
960 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500961static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300962 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
Peter Ujfalusidf6694f2015-10-16 10:18:00 +0300963 unsigned int acnt, unsigned int dma_length,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300964 enum dma_transfer_direction direction)
Joel Fernandesfd009032013-09-23 18:05:13 -0500965{
966 struct edma_chan *echan = to_edma_chan(chan);
967 struct device *dev = chan->device->dev;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500968 struct edmacc_param *param = &epset->param;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +0300969 int bcnt, ccnt, cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500970 int src_bidx, dst_bidx, src_cidx, dst_cidx;
971 int absync;
972
Peter Ujfalusib2b617d2014-04-14 14:41:58 +0300973 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
974 if (!burst)
975 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -0500976 /*
977 * If the maxburst is equal to the fifo width, use
978 * A-synced transfers. This allows for large contiguous
979 * buffer transfers using only one PaRAM set.
980 */
981 if (burst == 1) {
982 /*
983 * For the A-sync case, bcnt and ccnt are the remainder
984 * and quotient respectively of the division of:
985 * (dma_length / acnt) by (SZ_64K -1). This is so
986 * that in case bcnt over flows, we have ccnt to use.
987 * Note: In A-sync tranfer only, bcntrld is used, but it
988 * only applies for sg_dma_len(sg) >= SZ_64K.
989 * In this case, the best way adopted is- bccnt for the
990 * first frame will be the remainder below. Then for
991 * every successive frame, bcnt will be SZ_64K-1. This
992 * is assured as bcntrld = 0xffff in end of function.
993 */
994 absync = false;
995 ccnt = dma_length / acnt / (SZ_64K - 1);
996 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
997 /*
998 * If bcnt is non-zero, we have a remainder and hence an
999 * extra frame to transfer, so increment ccnt.
1000 */
1001 if (bcnt)
1002 ccnt++;
1003 else
1004 bcnt = SZ_64K - 1;
1005 cidx = acnt;
1006 } else {
1007 /*
1008 * If maxburst is greater than the fifo address_width,
1009 * use AB-synced transfers where A count is the fifo
1010 * address_width and B count is the maxburst. In this
1011 * case, we are limited to transfers of C count frames
1012 * of (address_width * maxburst) where C count is limited
1013 * to SZ_64K-1. This places an upper bound on the length
1014 * of an SG segment that can be handled.
1015 */
1016 absync = true;
1017 bcnt = burst;
1018 ccnt = dma_length / (acnt * bcnt);
1019 if (ccnt > (SZ_64K - 1)) {
1020 dev_err(dev, "Exceeded max SG segment size\n");
1021 return -EINVAL;
1022 }
1023 cidx = acnt * bcnt;
1024 }
1025
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001026 epset->len = dma_length;
1027
Joel Fernandesfd009032013-09-23 18:05:13 -05001028 if (direction == DMA_MEM_TO_DEV) {
1029 src_bidx = acnt;
1030 src_cidx = cidx;
1031 dst_bidx = 0;
1032 dst_cidx = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001033 epset->addr = src_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001034 } else if (direction == DMA_DEV_TO_MEM) {
1035 src_bidx = 0;
1036 src_cidx = 0;
1037 dst_bidx = acnt;
1038 dst_cidx = cidx;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001039 epset->addr = dst_addr;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001040 } else if (direction == DMA_MEM_TO_MEM) {
1041 src_bidx = acnt;
1042 src_cidx = cidx;
1043 dst_bidx = acnt;
1044 dst_cidx = cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001045 } else {
1046 dev_err(dev, "%s: direction not implemented yet\n", __func__);
1047 return -EINVAL;
1048 }
1049
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001050 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
Joel Fernandesfd009032013-09-23 18:05:13 -05001051 /* Configure A or AB synchronized transfers */
1052 if (absync)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001053 param->opt |= SYNCDIM;
Joel Fernandesfd009032013-09-23 18:05:13 -05001054
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001055 param->src = src_addr;
1056 param->dst = dst_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001057
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001058 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
1059 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001060
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001061 param->a_b_cnt = bcnt << 16 | acnt;
1062 param->ccnt = ccnt;
Joel Fernandesfd009032013-09-23 18:05:13 -05001063 /*
1064 * Only time when (bcntrld) auto reload is required is for
1065 * A-sync case, and in this case, a requirement of reload value
1066 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
1067 * and then later will be populated by edma_execute.
1068 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001069 param->link_bcntrld = 0xffffffff;
Joel Fernandesfd009032013-09-23 18:05:13 -05001070 return absync;
1071}
1072
Matt Porterc2dde5f2012-08-22 21:09:34 -04001073static struct dma_async_tx_descriptor *edma_prep_slave_sg(
1074 struct dma_chan *chan, struct scatterlist *sgl,
1075 unsigned int sg_len, enum dma_transfer_direction direction,
1076 unsigned long tx_flags, void *context)
1077{
1078 struct edma_chan *echan = to_edma_chan(chan);
1079 struct device *dev = chan->device->dev;
1080 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -05001081 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -05001082 enum dma_slave_buswidth dev_width;
1083 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001084 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -05001085 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001086
1087 if (unlikely(!echan || !sgl || !sg_len))
1088 return NULL;
1089
Matt Porter661f7cb2013-01-10 13:41:04 -05001090 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001091 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001092 dev_width = echan->cfg.src_addr_width;
1093 burst = echan->cfg.src_maxburst;
1094 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001095 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001096 dev_width = echan->cfg.dst_addr_width;
1097 burst = echan->cfg.dst_maxburst;
1098 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001099 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Matt Porter661f7cb2013-01-10 13:41:04 -05001100 return NULL;
1101 }
1102
1103 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001104 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001105 return NULL;
1106 }
1107
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001108 edesc = kzalloc(sizeof(*edesc) + sg_len * sizeof(edesc->pset[0]),
1109 GFP_ATOMIC);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001110 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001111 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001112 return NULL;
1113 }
1114
1115 edesc->pset_nr = sg_len;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001116 edesc->residue = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001117 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001118 edesc->echan = echan;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001119
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001120 /* Allocate a PaRAM slot, if needed */
1121 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
1122
1123 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001124 if (echan->slot[i] < 0) {
1125 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001126 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001127 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +03001128 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001129 dev_err(dev, "%s: Failed to allocate slot\n",
1130 __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001131 return NULL;
1132 }
1133 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001134 }
1135
1136 /* Configure PaRAM sets for each SG */
1137 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001138 /* Get address for each SG */
1139 if (direction == DMA_DEV_TO_MEM)
1140 dst_addr = sg_dma_address(sg);
1141 else
1142 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001143
Joel Fernandesfd009032013-09-23 18:05:13 -05001144 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1145 dst_addr, burst, dev_width,
1146 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +05301147 if (ret < 0) {
1148 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -05001149 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001150 }
1151
Joel Fernandesfd009032013-09-23 18:05:13 -05001152 edesc->absync = ret;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001153 edesc->residue += sg_dma_len(sg);
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001154
1155 /* If this is the last in a current SG set of transactions,
1156 enable interrupts so that next set is processed */
1157 if (!((i+1) % MAX_NR_SG))
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001158 edesc->pset[i].param.opt |= TCINTEN;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001159
Matt Porterc2dde5f2012-08-22 21:09:34 -04001160 /* If this is the last set, enable completion interrupt flag */
1161 if (i == sg_len - 1)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001162 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001163 }
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001164 edesc->residue_stat = edesc->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001165
Matt Porterc2dde5f2012-08-22 21:09:34 -04001166 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1167}
Matt Porterc2dde5f2012-08-22 21:09:34 -04001168
Lad, Prabhakarb7a4fd52015-02-04 13:03:27 +00001169static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001170 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1171 size_t len, unsigned long tx_flags)
1172{
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001173 int ret, nslots;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001174 struct edma_desc *edesc;
1175 struct device *dev = chan->device->dev;
1176 struct edma_chan *echan = to_edma_chan(chan);
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001177 unsigned int width, pset_len;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001178
1179 if (unlikely(!echan || !len))
1180 return NULL;
1181
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001182 if (len < SZ_64K) {
1183 /*
1184 * Transfer size less than 64K can be handled with one paRAM
1185 * slot and with one burst.
1186 * ACNT = length
1187 */
1188 width = len;
1189 pset_len = len;
1190 nslots = 1;
1191 } else {
1192 /*
1193 * Transfer size bigger than 64K will be handled with maximum of
1194 * two paRAM slots.
1195 * slot1: (full_length / 32767) times 32767 bytes bursts.
1196 * ACNT = 32767, length1: (full_length / 32767) * 32767
1197 * slot2: the remaining amount of data after slot1.
1198 * ACNT = full_length - length1, length2 = ACNT
1199 *
1200 * When the full_length is multibple of 32767 one slot can be
1201 * used to complete the transfer.
1202 */
1203 width = SZ_32K - 1;
1204 pset_len = rounddown(len, width);
1205 /* One slot is enough for lengths multiple of (SZ_32K -1) */
1206 if (unlikely(pset_len == len))
1207 nslots = 1;
1208 else
1209 nslots = 2;
1210 }
1211
1212 edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]),
1213 GFP_ATOMIC);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001214 if (!edesc) {
1215 dev_dbg(dev, "Failed to allocate a descriptor\n");
1216 return NULL;
1217 }
1218
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001219 edesc->pset_nr = nslots;
1220 edesc->residue = edesc->residue_stat = len;
1221 edesc->direction = DMA_MEM_TO_MEM;
1222 edesc->echan = echan;
Peter Ujfalusi21a31842015-10-16 10:17:59 +03001223
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001224 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001225 width, pset_len, DMA_MEM_TO_MEM);
1226 if (ret < 0) {
1227 kfree(edesc);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001228 return NULL;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001229 }
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001230
1231 edesc->absync = ret;
1232
Joel Fernandesb0cce4c2014-04-28 15:30:32 -05001233 edesc->pset[0].param.opt |= ITCCHEN;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001234 if (nslots == 1) {
1235 /* Enable transfer complete interrupt */
1236 edesc->pset[0].param.opt |= TCINTEN;
1237 } else {
1238 /* Enable transfer complete chaining for the first slot */
1239 edesc->pset[0].param.opt |= TCCHEN;
1240
1241 if (echan->slot[1] < 0) {
1242 echan->slot[1] = edma_alloc_slot(echan->ecc,
1243 EDMA_SLOT_ANY);
1244 if (echan->slot[1] < 0) {
1245 kfree(edesc);
1246 dev_err(dev, "%s: Failed to allocate slot\n",
1247 __func__);
1248 return NULL;
1249 }
1250 }
1251 dest += pset_len;
1252 src += pset_len;
1253 pset_len = width = len % (SZ_32K - 1);
1254
1255 ret = edma_config_pset(chan, &edesc->pset[1], src, dest, 1,
1256 width, pset_len, DMA_MEM_TO_MEM);
1257 if (ret < 0) {
1258 kfree(edesc);
1259 return NULL;
1260 }
1261
1262 edesc->pset[1].param.opt |= ITCCHEN;
1263 edesc->pset[1].param.opt |= TCINTEN;
1264 }
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001265
1266 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1267}
1268
Joel Fernandes50a9c702013-10-31 16:31:23 -05001269static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
1270 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1271 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +02001272 unsigned long tx_flags)
Joel Fernandes50a9c702013-10-31 16:31:23 -05001273{
1274 struct edma_chan *echan = to_edma_chan(chan);
1275 struct device *dev = chan->device->dev;
1276 struct edma_desc *edesc;
1277 dma_addr_t src_addr, dst_addr;
1278 enum dma_slave_buswidth dev_width;
1279 u32 burst;
1280 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001281
Joel Fernandes50a9c702013-10-31 16:31:23 -05001282 if (unlikely(!echan || !buf_len || !period_len))
1283 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001284
Joel Fernandes50a9c702013-10-31 16:31:23 -05001285 if (direction == DMA_DEV_TO_MEM) {
1286 src_addr = echan->cfg.src_addr;
1287 dst_addr = buf_addr;
1288 dev_width = echan->cfg.src_addr_width;
1289 burst = echan->cfg.src_maxburst;
1290 } else if (direction == DMA_MEM_TO_DEV) {
1291 src_addr = buf_addr;
1292 dst_addr = echan->cfg.dst_addr;
1293 dev_width = echan->cfg.dst_addr_width;
1294 burst = echan->cfg.dst_maxburst;
1295 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001296 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001297 return NULL;
1298 }
1299
1300 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001301 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001302 return NULL;
1303 }
1304
1305 if (unlikely(buf_len % period_len)) {
1306 dev_err(dev, "Period should be multiple of Buffer length\n");
1307 return NULL;
1308 }
1309
1310 nslots = (buf_len / period_len) + 1;
1311
1312 /*
1313 * Cyclic DMA users such as audio cannot tolerate delays introduced
1314 * by cases where the number of periods is more than the maximum
1315 * number of SGs the EDMA driver can handle at a time. For DMA types
1316 * such as Slave SGs, such delays are tolerable and synchronized,
1317 * but the synchronization is difficult to achieve with Cyclic and
1318 * cannot be guaranteed, so we error out early.
1319 */
1320 if (nslots > MAX_NR_SG)
1321 return NULL;
1322
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001323 edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]),
1324 GFP_ATOMIC);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001325 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001326 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001327 return NULL;
1328 }
1329
1330 edesc->cyclic = 1;
1331 edesc->pset_nr = nslots;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001332 edesc->residue = edesc->residue_stat = buf_len;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001333 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001334 edesc->echan = echan;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001335
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001336 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
1337 __func__, echan->ch_num, nslots, period_len, buf_len);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001338
1339 for (i = 0; i < nslots; i++) {
1340 /* Allocate a PaRAM slot, if needed */
1341 if (echan->slot[i] < 0) {
1342 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001343 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001344 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001345 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001346 dev_err(dev, "%s: Failed to allocate slot\n",
1347 __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001348 return NULL;
1349 }
1350 }
1351
1352 if (i == nslots - 1) {
1353 memcpy(&edesc->pset[i], &edesc->pset[0],
1354 sizeof(edesc->pset[0]));
1355 break;
1356 }
1357
1358 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1359 dst_addr, burst, dev_width, period_len,
1360 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001361 if (ret < 0) {
1362 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001363 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001364 }
Joel Fernandes50a9c702013-10-31 16:31:23 -05001365
1366 if (direction == DMA_DEV_TO_MEM)
1367 dst_addr += period_len;
1368 else
1369 src_addr += period_len;
1370
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001371 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
1372 dev_vdbg(dev,
Joel Fernandes50a9c702013-10-31 16:31:23 -05001373 "\n pset[%d]:\n"
1374 " chnum\t%d\n"
1375 " slot\t%d\n"
1376 " opt\t%08x\n"
1377 " src\t%08x\n"
1378 " dst\t%08x\n"
1379 " abcnt\t%08x\n"
1380 " ccnt\t%08x\n"
1381 " bidx\t%08x\n"
1382 " cidx\t%08x\n"
1383 " lkrld\t%08x\n",
1384 i, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001385 edesc->pset[i].param.opt,
1386 edesc->pset[i].param.src,
1387 edesc->pset[i].param.dst,
1388 edesc->pset[i].param.a_b_cnt,
1389 edesc->pset[i].param.ccnt,
1390 edesc->pset[i].param.src_dst_bidx,
1391 edesc->pset[i].param.src_dst_cidx,
1392 edesc->pset[i].param.link_bcntrld);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001393
1394 edesc->absync = ret;
1395
1396 /*
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001397 * Enable period interrupt only if it is requested
Joel Fernandes50a9c702013-10-31 16:31:23 -05001398 */
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001399 if (tx_flags & DMA_PREP_INTERRUPT)
1400 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001401 }
1402
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001403 /* Place the cyclic channel to highest priority queue */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001404 edma_assign_channel_eventq(echan, EVENTQ_0);
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001405
Matt Porterc2dde5f2012-08-22 21:09:34 -04001406 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1407}
1408
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001409static void edma_completion_handler(struct edma_chan *echan)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001410{
Matt Porterc2dde5f2012-08-22 21:09:34 -04001411 struct device *dev = echan->vchan.chan.device->dev;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001412 struct edma_desc *edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001413
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001414 if (!edesc)
1415 return;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001416
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001417 spin_lock(&echan->vchan.lock);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001418 if (edesc->cyclic) {
1419 vchan_cyclic_callback(&edesc->vdesc);
1420 spin_unlock(&echan->vchan.lock);
1421 return;
1422 } else if (edesc->processed == edesc->pset_nr) {
1423 edesc->residue = 0;
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001424 edma_stop(echan);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001425 vchan_cookie_complete(&edesc->vdesc);
1426 echan->edesc = NULL;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001427
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001428 dev_dbg(dev, "Transfer completed on channel %d\n",
1429 echan->ch_num);
1430 } else {
1431 dev_dbg(dev, "Sub transfer completed on channel %d\n",
1432 echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001433
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001434 edma_pause(echan);
Joel Fernandesc5f47992013-08-29 18:05:43 -05001435
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001436 /* Update statistics for tx_status */
1437 edesc->residue -= edesc->sg_len;
1438 edesc->residue_stat = edesc->residue;
1439 edesc->processed_stat = edesc->processed;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001440 }
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001441 edma_execute(echan);
1442
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001443 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001444}
1445
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001446/* eDMA interrupt handler */
1447static irqreturn_t dma_irq_handler(int irq, void *data)
1448{
1449 struct edma_cc *ecc = data;
1450 int ctlr;
1451 u32 sh_ier;
1452 u32 sh_ipr;
1453 u32 bank;
1454
1455 ctlr = ecc->id;
1456 if (ctlr < 0)
1457 return IRQ_NONE;
1458
1459 dev_vdbg(ecc->dev, "dma_irq_handler\n");
1460
1461 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 0);
1462 if (!sh_ipr) {
1463 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 1);
1464 if (!sh_ipr)
1465 return IRQ_NONE;
1466 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 1);
1467 bank = 1;
1468 } else {
1469 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 0);
1470 bank = 0;
1471 }
1472
1473 do {
1474 u32 slot;
1475 u32 channel;
1476
1477 slot = __ffs(sh_ipr);
1478 sh_ipr &= ~(BIT(slot));
1479
1480 if (sh_ier & BIT(slot)) {
1481 channel = (bank << 5) | slot;
1482 /* Clear the corresponding IPR bits */
1483 edma_shadow0_write_array(ecc, SH_ICR, bank, BIT(slot));
1484 edma_completion_handler(&ecc->slave_chans[channel]);
1485 }
1486 } while (sh_ipr);
1487
1488 edma_shadow0_write(ecc, SH_IEVAL, 1);
1489 return IRQ_HANDLED;
1490}
1491
1492static void edma_error_handler(struct edma_chan *echan)
1493{
1494 struct edma_cc *ecc = echan->ecc;
1495 struct device *dev = echan->vchan.chan.device->dev;
1496 struct edmacc_param p;
1497
1498 if (!echan->edesc)
1499 return;
1500
1501 spin_lock(&echan->vchan.lock);
1502
1503 edma_read_slot(ecc, echan->slot[0], &p);
1504 /*
1505 * Issue later based on missed flag which will be sure
1506 * to happen as:
1507 * (1) we finished transmitting an intermediate slot and
1508 * edma_execute is coming up.
1509 * (2) or we finished current transfer and issue will
1510 * call edma_execute.
1511 *
1512 * Important note: issuing can be dangerous here and
1513 * lead to some nasty recursion when we are in a NULL
1514 * slot. So we avoid doing so and set the missed flag.
1515 */
1516 if (p.a_b_cnt == 0 && p.ccnt == 0) {
1517 dev_dbg(dev, "Error on null slot, setting miss\n");
1518 echan->missed = 1;
1519 } else {
1520 /*
1521 * The slot is already programmed but the event got
1522 * missed, so its safe to issue it here.
1523 */
1524 dev_dbg(dev, "Missed event, TRIGGERING\n");
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001525 edma_clean_channel(echan);
1526 edma_stop(echan);
1527 edma_start(echan);
1528 edma_trigger_channel(echan);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001529 }
1530 spin_unlock(&echan->vchan.lock);
1531}
1532
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001533static inline bool edma_error_pending(struct edma_cc *ecc)
1534{
1535 if (edma_read_array(ecc, EDMA_EMR, 0) ||
1536 edma_read_array(ecc, EDMA_EMR, 1) ||
1537 edma_read(ecc, EDMA_QEMR) || edma_read(ecc, EDMA_CCERR))
1538 return true;
1539
1540 return false;
1541}
1542
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001543/* eDMA error interrupt handler */
1544static irqreturn_t dma_ccerr_handler(int irq, void *data)
1545{
1546 struct edma_cc *ecc = data;
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001547 int i, j;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001548 int ctlr;
1549 unsigned int cnt = 0;
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001550 unsigned int val;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001551
1552 ctlr = ecc->id;
1553 if (ctlr < 0)
1554 return IRQ_NONE;
1555
1556 dev_vdbg(ecc->dev, "dma_ccerr_handler\n");
1557
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001558 if (!edma_error_pending(ecc))
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001559 return IRQ_NONE;
1560
1561 while (1) {
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001562 /* Event missed register(s) */
1563 for (j = 0; j < 2; j++) {
1564 unsigned long emr;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001565
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001566 val = edma_read_array(ecc, EDMA_EMR, j);
1567 if (!val)
1568 continue;
1569
1570 dev_dbg(ecc->dev, "EMR%d 0x%08x\n", j, val);
1571 emr = val;
1572 for (i = find_next_bit(&emr, 32, 0); i < 32;
1573 i = find_next_bit(&emr, 32, i + 1)) {
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001574 int k = (j << 5) + i;
1575
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001576 /* Clear the corresponding EMR bits */
1577 edma_write_array(ecc, EDMA_EMCR, j, BIT(i));
1578 /* Clear any SER */
1579 edma_shadow0_write_array(ecc, SH_SECR, j,
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001580 BIT(i));
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001581 edma_error_handler(&ecc->slave_chans[k]);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001582 }
1583 }
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001584
1585 val = edma_read(ecc, EDMA_QEMR);
1586 if (val) {
1587 dev_dbg(ecc->dev, "QEMR 0x%02x\n", val);
1588 /* Not reported, just clear the interrupt reason. */
1589 edma_write(ecc, EDMA_QEMCR, val);
1590 edma_shadow0_write(ecc, SH_QSECR, val);
1591 }
1592
1593 val = edma_read(ecc, EDMA_CCERR);
1594 if (val) {
1595 dev_warn(ecc->dev, "CCERR 0x%08x\n", val);
1596 /* Not reported, just clear the interrupt reason. */
1597 edma_write(ecc, EDMA_CCERRCLR, val);
1598 }
1599
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001600 if (!edma_error_pending(ecc))
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001601 break;
1602 cnt++;
1603 if (cnt > 10)
1604 break;
1605 }
1606 edma_write(ecc, EDMA_EEVAL, 1);
1607 return IRQ_HANDLED;
1608}
1609
Matt Porterc2dde5f2012-08-22 21:09:34 -04001610/* Alloc channel resources */
1611static int edma_alloc_chan_resources(struct dma_chan *chan)
1612{
1613 struct edma_chan *echan = to_edma_chan(chan);
1614 struct device *dev = chan->device->dev;
1615 int ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001616
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001617 ret = edma_alloc_channel(echan, EVENTQ_DEFAULT);
1618 if (ret)
1619 return ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001620
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001621 echan->slot[0] = edma_alloc_slot(echan->ecc, echan->ch_num);
1622 if (echan->slot[0] < 0) {
1623 dev_err(dev, "Entry slot allocation failed for channel %u\n",
1624 EDMA_CHAN_SLOT(echan->ch_num));
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001625 goto err_slot;
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001626 }
1627
1628 /* Set up channel -> slot mapping for the entry slot */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001629 edma_set_chmap(echan, echan->slot[0]);
1630 echan->alloced = true;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001631
Peter Ujfalusi9aac9092014-04-24 10:29:50 +03001632 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
Ezequiel Garcia0e772c62013-12-13 11:06:18 -03001633 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
Matt Porterc2dde5f2012-08-22 21:09:34 -04001634
1635 return 0;
1636
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001637err_slot:
1638 edma_free_channel(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001639 return ret;
1640}
1641
1642/* Free channel resources */
1643static void edma_free_chan_resources(struct dma_chan *chan)
1644{
1645 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001646 int i;
1647
1648 /* Terminate transfers */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001649 edma_stop(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001650
1651 vchan_free_chan_resources(&echan->vchan);
1652
1653 /* Free EDMA PaRAM slots */
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001654 for (i = 0; i < EDMA_MAX_SLOTS; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001655 if (echan->slot[i] >= 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001656 edma_free_slot(echan->ecc, echan->slot[i]);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001657 echan->slot[i] = -1;
1658 }
1659 }
1660
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001661 /* Set entry slot to the dummy slot */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001662 edma_set_chmap(echan, echan->ecc->dummy_slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001663
Matt Porterc2dde5f2012-08-22 21:09:34 -04001664 /* Free EDMA channel */
1665 if (echan->alloced) {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001666 edma_free_channel(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001667 echan->alloced = false;
1668 }
1669
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03001670 dev_dbg(chan->device->dev, "freeing channel for %u\n", echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001671}
1672
1673/* Send pending descriptor to hardware */
1674static void edma_issue_pending(struct dma_chan *chan)
1675{
1676 struct edma_chan *echan = to_edma_chan(chan);
1677 unsigned long flags;
1678
1679 spin_lock_irqsave(&echan->vchan.lock, flags);
1680 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
1681 edma_execute(echan);
1682 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1683}
1684
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001685static u32 edma_residue(struct edma_desc *edesc)
1686{
1687 bool dst = edesc->direction == DMA_DEV_TO_MEM;
1688 struct edma_pset *pset = edesc->pset;
1689 dma_addr_t done, pos;
1690 int i;
1691
1692 /*
1693 * We always read the dst/src position from the first RamPar
1694 * pset. That's the one which is active now.
1695 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001696 pos = edma_get_position(edesc->echan->ecc, edesc->echan->slot[0], dst);
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001697
1698 /*
1699 * Cyclic is simple. Just subtract pset[0].addr from pos.
1700 *
1701 * We never update edesc->residue in the cyclic case, so we
1702 * can tell the remaining room to the end of the circular
1703 * buffer.
1704 */
1705 if (edesc->cyclic) {
1706 done = pos - pset->addr;
1707 edesc->residue_stat = edesc->residue - done;
1708 return edesc->residue_stat;
1709 }
1710
1711 /*
1712 * For SG operation we catch up with the last processed
1713 * status.
1714 */
1715 pset += edesc->processed_stat;
1716
1717 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
1718 /*
1719 * If we are inside this pset address range, we know
1720 * this is the active one. Get the current delta and
1721 * stop walking the psets.
1722 */
1723 if (pos >= pset->addr && pos < pset->addr + pset->len)
1724 return edesc->residue_stat - (pos - pset->addr);
1725
1726 /* Otherwise mark it done and update residue_stat. */
1727 edesc->processed_stat++;
1728 edesc->residue_stat -= pset->len;
1729 }
1730 return edesc->residue_stat;
1731}
1732
Matt Porterc2dde5f2012-08-22 21:09:34 -04001733/* Check request completion status */
1734static enum dma_status edma_tx_status(struct dma_chan *chan,
1735 dma_cookie_t cookie,
1736 struct dma_tx_state *txstate)
1737{
1738 struct edma_chan *echan = to_edma_chan(chan);
1739 struct virt_dma_desc *vdesc;
1740 enum dma_status ret;
1741 unsigned long flags;
1742
1743 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +05301744 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001745 return ret;
1746
1747 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001748 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001749 txstate->residue = edma_residue(echan->edesc);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001750 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
1751 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001752 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1753
1754 return ret;
1755}
1756
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001757static void __init edma_chan_init(struct edma_cc *ecc, struct dma_device *dma,
Matt Porterc2dde5f2012-08-22 21:09:34 -04001758 struct edma_chan *echans)
1759{
1760 int i, j;
1761
Peter Ujfalusicb782052015-10-14 14:42:54 +03001762 for (i = 0; i < ecc->num_channels; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001763 struct edma_chan *echan = &echans[i];
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001764 echan->ch_num = EDMA_CTLR_CHAN(ecc->id, i);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001765 echan->ecc = ecc;
1766 echan->vchan.desc_free = edma_desc_free;
1767
1768 vchan_init(&echan->vchan, dma);
1769
1770 INIT_LIST_HEAD(&echan->node);
1771 for (j = 0; j < EDMA_MAX_SLOTS; j++)
1772 echan->slot[j] = -1;
1773 }
1774}
1775
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001776#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1777 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
Peter Ujfalusie4a899d2014-07-03 07:51:56 +03001778 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001779 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1780
Matt Porterc2dde5f2012-08-22 21:09:34 -04001781static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
1782 struct device *dev)
1783{
1784 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001785 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001786 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001787 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
1788 dma->device_free_chan_resources = edma_free_chan_resources;
1789 dma->device_issue_pending = edma_issue_pending;
1790 dma->device_tx_status = edma_tx_status;
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001791 dma->device_config = edma_slave_config;
1792 dma->device_pause = edma_dma_pause;
1793 dma->device_resume = edma_dma_resume;
1794 dma->device_terminate_all = edma_terminate_all;
Maxime Ripard9f59cd02014-11-17 14:42:47 +01001795
1796 dma->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1797 dma->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
1798 dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1799 dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1800
Matt Porterc2dde5f2012-08-22 21:09:34 -04001801 dma->dev = dev;
1802
1803 INIT_LIST_HEAD(&dma->channels);
1804}
1805
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001806static int edma_setup_from_hw(struct device *dev, struct edma_soc_info *pdata,
1807 struct edma_cc *ecc)
1808{
1809 int i;
1810 u32 value, cccfg;
1811 s8 (*queue_priority_map)[2];
1812
1813 /* Decode the eDMA3 configuration from CCCFG register */
1814 cccfg = edma_read(ecc, EDMA_CCCFG);
1815
1816 value = GET_NUM_REGN(cccfg);
1817 ecc->num_region = BIT(value);
1818
1819 value = GET_NUM_DMACH(cccfg);
1820 ecc->num_channels = BIT(value + 1);
1821
1822 value = GET_NUM_PAENTRY(cccfg);
1823 ecc->num_slots = BIT(value + 4);
1824
1825 value = GET_NUM_EVQUE(cccfg);
1826 ecc->num_tc = value + 1;
1827
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +03001828 ecc->chmap_exist = (cccfg & CHMAP_EXIST) ? true : false;
1829
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001830 dev_dbg(dev, "eDMA3 CC HW configuration (cccfg: 0x%08x):\n", cccfg);
1831 dev_dbg(dev, "num_region: %u\n", ecc->num_region);
1832 dev_dbg(dev, "num_channels: %u\n", ecc->num_channels);
1833 dev_dbg(dev, "num_slots: %u\n", ecc->num_slots);
1834 dev_dbg(dev, "num_tc: %u\n", ecc->num_tc);
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +03001835 dev_dbg(dev, "chmap_exist: %s\n", ecc->chmap_exist ? "yes" : "no");
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001836
1837 /* Nothing need to be done if queue priority is provided */
1838 if (pdata->queue_priority_mapping)
1839 return 0;
1840
1841 /*
1842 * Configure TC/queue priority as follows:
1843 * Q0 - priority 0
1844 * Q1 - priority 1
1845 * Q2 - priority 2
1846 * ...
1847 * The meaning of priority numbers: 0 highest priority, 7 lowest
1848 * priority. So Q0 is the highest priority queue and the last queue has
1849 * the lowest priority.
1850 */
Peter Ujfalusi547c6e22015-10-14 14:42:55 +03001851 queue_priority_map = devm_kcalloc(dev, ecc->num_tc + 1, sizeof(s8),
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001852 GFP_KERNEL);
1853 if (!queue_priority_map)
1854 return -ENOMEM;
1855
1856 for (i = 0; i < ecc->num_tc; i++) {
1857 queue_priority_map[i][0] = i;
1858 queue_priority_map[i][1] = i;
1859 }
1860 queue_priority_map[i][0] = -1;
1861 queue_priority_map[i][1] = -1;
1862
1863 pdata->queue_priority_mapping = queue_priority_map;
1864 /* Default queue has the lowest priority */
1865 pdata->default_queue = i - 1;
1866
1867 return 0;
1868}
1869
1870#if IS_ENABLED(CONFIG_OF)
1871static int edma_xbar_event_map(struct device *dev, struct edma_soc_info *pdata,
1872 size_t sz)
1873{
1874 const char pname[] = "ti,edma-xbar-event-map";
1875 struct resource res;
1876 void __iomem *xbar;
1877 s16 (*xbar_chans)[2];
1878 size_t nelm = sz / sizeof(s16);
1879 u32 shift, offset, mux;
1880 int ret, i;
1881
Peter Ujfalusi547c6e22015-10-14 14:42:55 +03001882 xbar_chans = devm_kcalloc(dev, nelm + 2, sizeof(s16), GFP_KERNEL);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001883 if (!xbar_chans)
1884 return -ENOMEM;
1885
1886 ret = of_address_to_resource(dev->of_node, 1, &res);
1887 if (ret)
1888 return -ENOMEM;
1889
1890 xbar = devm_ioremap(dev, res.start, resource_size(&res));
1891 if (!xbar)
1892 return -ENOMEM;
1893
1894 ret = of_property_read_u16_array(dev->of_node, pname, (u16 *)xbar_chans,
1895 nelm);
1896 if (ret)
1897 return -EIO;
1898
1899 /* Invalidate last entry for the other user of this mess */
1900 nelm >>= 1;
1901 xbar_chans[nelm][0] = -1;
1902 xbar_chans[nelm][1] = -1;
1903
1904 for (i = 0; i < nelm; i++) {
1905 shift = (xbar_chans[i][1] & 0x03) << 3;
1906 offset = xbar_chans[i][1] & 0xfffffffc;
1907 mux = readl(xbar + offset);
1908 mux &= ~(0xff << shift);
1909 mux |= xbar_chans[i][0] << shift;
1910 writel(mux, (xbar + offset));
1911 }
1912
1913 pdata->xbar_chans = (const s16 (*)[2]) xbar_chans;
1914 return 0;
1915}
1916
1917static int edma_of_parse_dt(struct device *dev, struct edma_soc_info *pdata)
1918{
1919 int ret = 0;
1920 struct property *prop;
1921 size_t sz;
1922 struct edma_rsv_info *rsv_info;
1923
1924 rsv_info = devm_kzalloc(dev, sizeof(struct edma_rsv_info), GFP_KERNEL);
1925 if (!rsv_info)
1926 return -ENOMEM;
1927 pdata->rsv = rsv_info;
1928
1929 prop = of_find_property(dev->of_node, "ti,edma-xbar-event-map", &sz);
1930 if (prop)
1931 ret = edma_xbar_event_map(dev, pdata, sz);
1932
1933 return ret;
1934}
1935
1936static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev)
1937{
1938 struct edma_soc_info *info;
1939 int ret;
1940
1941 info = devm_kzalloc(dev, sizeof(struct edma_soc_info), GFP_KERNEL);
1942 if (!info)
1943 return ERR_PTR(-ENOMEM);
1944
1945 ret = edma_of_parse_dt(dev, info);
1946 if (ret)
1947 return ERR_PTR(ret);
1948
1949 return info;
1950}
1951#else
1952static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev)
1953{
1954 return ERR_PTR(-EINVAL);
1955}
1956#endif
1957
Bill Pemberton463a1f82012-11-19 13:22:55 -05001958static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001959{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001960 struct edma_soc_info *info = pdev->dev.platform_data;
1961 s8 (*queue_priority_mapping)[2];
1962 int i, off, ln;
1963 const s16 (*rsv_chans)[2];
1964 const s16 (*rsv_slots)[2];
1965 const s16 (*xbar_chans)[2];
1966 int irq;
1967 char *irq_name;
1968 struct resource *mem;
1969 struct device_node *node = pdev->dev.of_node;
1970 struct device *dev = &pdev->dev;
1971 struct edma_cc *ecc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001972 int ret;
1973
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001974 if (node) {
1975 info = edma_setup_info_from_dt(dev);
1976 if (IS_ERR(info)) {
1977 dev_err(dev, "failed to get DT data\n");
1978 return PTR_ERR(info);
1979 }
1980 }
1981
1982 if (!info)
1983 return -ENODEV;
1984
1985 pm_runtime_enable(dev);
1986 ret = pm_runtime_get_sync(dev);
1987 if (ret < 0) {
1988 dev_err(dev, "pm_runtime_get_sync() failed\n");
1989 return ret;
1990 }
1991
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03001992 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
Russell King94cb0e72013-06-27 13:45:16 +01001993 if (ret)
1994 return ret;
1995
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03001996 ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001997 if (!ecc) {
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03001998 dev_err(dev, "Can't allocate controller\n");
Matt Porterc2dde5f2012-08-22 21:09:34 -04001999 return -ENOMEM;
2000 }
2001
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002002 ecc->dev = dev;
2003 ecc->id = pdev->id;
2004 /* When booting with DT the pdev->id is -1 */
2005 if (ecc->id < 0)
2006 ecc->id = 0;
Peter Ujfalusica304fa2015-10-14 14:42:49 +03002007
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002008 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma3_cc");
2009 if (!mem) {
2010 dev_dbg(dev, "mem resource not found, using index 0\n");
2011 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2012 if (!mem) {
2013 dev_err(dev, "no mem resource?\n");
2014 return -ENODEV;
2015 }
2016 }
2017 ecc->base = devm_ioremap_resource(dev, mem);
2018 if (IS_ERR(ecc->base))
2019 return PTR_ERR(ecc->base);
Peter Ujfalusib2c843a2015-10-14 14:42:50 +03002020
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002021 platform_set_drvdata(pdev, ecc);
2022
2023 /* Get eDMA3 configuration from IP */
2024 ret = edma_setup_from_hw(dev, info, ecc);
2025 if (ret)
2026 return ret;
2027
Peter Ujfalusicb782052015-10-14 14:42:54 +03002028 /* Allocate memory based on the information we got from the IP */
2029 ecc->slave_chans = devm_kcalloc(dev, ecc->num_channels,
2030 sizeof(*ecc->slave_chans), GFP_KERNEL);
2031 if (!ecc->slave_chans)
2032 return -ENOMEM;
2033
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002034 ecc->channel_unused = devm_kcalloc(dev,
2035 BITS_TO_LONGS(ecc->num_channels),
2036 sizeof(unsigned long), GFP_KERNEL);
2037 if (!ecc->channel_unused)
Peter Ujfalusicb782052015-10-14 14:42:54 +03002038 return -ENOMEM;
2039
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002040 ecc->slot_inuse = devm_kcalloc(dev, BITS_TO_LONGS(ecc->num_slots),
Peter Ujfalusicb782052015-10-14 14:42:54 +03002041 sizeof(unsigned long), GFP_KERNEL);
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002042 if (!ecc->slot_inuse)
Peter Ujfalusicb782052015-10-14 14:42:54 +03002043 return -ENOMEM;
2044
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002045 ecc->default_queue = info->default_queue;
2046
2047 for (i = 0; i < ecc->num_slots; i++)
2048 edma_write_slot(ecc, i, &dummy_paramset);
2049
2050 /* Mark all channels as unused */
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002051 memset(ecc->channel_unused, 0xff, sizeof(ecc->channel_unused));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002052
2053 if (info->rsv) {
2054 /* Clear the reserved channels in unused list */
2055 rsv_chans = info->rsv->rsv_chans;
2056 if (rsv_chans) {
2057 for (i = 0; rsv_chans[i][0] != -1; i++) {
2058 off = rsv_chans[i][0];
2059 ln = rsv_chans[i][1];
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002060 clear_bits(off, ln, ecc->channel_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002061 }
2062 }
2063
2064 /* Set the reserved slots in inuse list */
2065 rsv_slots = info->rsv->rsv_slots;
2066 if (rsv_slots) {
2067 for (i = 0; rsv_slots[i][0] != -1; i++) {
2068 off = rsv_slots[i][0];
2069 ln = rsv_slots[i][1];
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002070 set_bits(off, ln, ecc->slot_inuse);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002071 }
2072 }
2073 }
2074
2075 /* Clear the xbar mapped channels in unused list */
2076 xbar_chans = info->xbar_chans;
2077 if (xbar_chans) {
2078 for (i = 0; xbar_chans[i][1] != -1; i++) {
2079 off = xbar_chans[i][1];
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002080 clear_bits(off, 1, ecc->channel_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002081 }
2082 }
2083
2084 irq = platform_get_irq_byname(pdev, "edma3_ccint");
2085 if (irq < 0 && node)
2086 irq = irq_of_parse_and_map(node, 0);
2087
2088 if (irq >= 0) {
2089 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccint",
2090 dev_name(dev));
2091 ret = devm_request_irq(dev, irq, dma_irq_handler, 0, irq_name,
2092 ecc);
2093 if (ret) {
2094 dev_err(dev, "CCINT (%d) failed --> %d\n", irq, ret);
2095 return ret;
2096 }
2097 }
2098
2099 irq = platform_get_irq_byname(pdev, "edma3_ccerrint");
2100 if (irq < 0 && node)
2101 irq = irq_of_parse_and_map(node, 2);
2102
2103 if (irq >= 0) {
2104 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccerrint",
2105 dev_name(dev));
2106 ret = devm_request_irq(dev, irq, dma_ccerr_handler, 0, irq_name,
2107 ecc);
2108 if (ret) {
2109 dev_err(dev, "CCERRINT (%d) failed --> %d\n", irq, ret);
2110 return ret;
2111 }
2112 }
2113
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002114 ecc->dummy_slot = edma_alloc_slot(ecc, EDMA_SLOT_ANY);
2115 if (ecc->dummy_slot < 0) {
2116 dev_err(dev, "Can't allocate PaRAM dummy slot\n");
2117 return ecc->dummy_slot;
2118 }
2119
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002120 queue_priority_mapping = info->queue_priority_mapping;
2121
2122 /* Event queue priority mapping */
2123 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2124 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2125 queue_priority_mapping[i][1]);
2126
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002127 for (i = 0; i < ecc->num_region; i++) {
2128 edma_write_array2(ecc, EDMA_DRAE, i, 0, 0x0);
2129 edma_write_array2(ecc, EDMA_DRAE, i, 1, 0x0);
2130 edma_write_array(ecc, EDMA_QRAE, i, 0x0);
2131 }
2132 ecc->info = info;
2133
Matt Porterc2dde5f2012-08-22 21:09:34 -04002134 dma_cap_zero(ecc->dma_slave.cap_mask);
2135 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
Peter Ujfalusi232b223d2014-04-14 14:42:00 +03002136 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05002137 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002138
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002139 edma_dma_init(ecc, &ecc->dma_slave, dev);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002140
2141 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
2142
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002143 for (i = 0; i < ecc->num_channels; i++) {
2144 /* Assign all channels to the default queue */
Peter Ujfalusif9425de2015-10-16 10:18:03 +03002145 edma_assign_channel_eventq(&ecc->slave_chans[i],
2146 info->default_queue);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002147 /* Set entry slot to the dummy slot */
2148 edma_set_chmap(&ecc->slave_chans[i], ecc->dummy_slot);
2149 }
2150
Matt Porterc2dde5f2012-08-22 21:09:34 -04002151 ret = dma_async_device_register(&ecc->dma_slave);
2152 if (ret)
2153 goto err_reg1;
2154
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002155 if (node)
2156 of_dma_controller_register(node, of_dma_xlate_by_chan_id,
Peter Ujfalusib2c843a2015-10-14 14:42:50 +03002157 &ecc->dma_slave);
Peter Ujfalusidc9b60552015-10-14 14:42:47 +03002158
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002159 dev_info(dev, "TI EDMA DMA engine driver\n");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002160
2161 return 0;
2162
2163err_reg1:
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002164 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002165 return ret;
2166}
2167
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08002168static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002169{
2170 struct device *dev = &pdev->dev;
2171 struct edma_cc *ecc = dev_get_drvdata(dev);
2172
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002173 if (dev->of_node)
2174 of_dma_controller_free(dev->of_node);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002175 dma_async_device_unregister(&ecc->dma_slave);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002176 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002177
2178 return 0;
2179}
2180
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002181#ifdef CONFIG_PM_SLEEP
2182static int edma_pm_resume(struct device *dev)
2183{
2184 struct edma_cc *ecc = dev_get_drvdata(dev);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002185 struct edma_chan *echan = ecc->slave_chans;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002186 int i;
2187 s8 (*queue_priority_mapping)[2];
2188
2189 queue_priority_mapping = ecc->info->queue_priority_mapping;
2190
2191 /* Event queue priority mapping */
2192 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2193 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2194 queue_priority_mapping[i][1]);
2195
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002196 for (i = 0; i < ecc->num_channels; i++) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002197 if (echan[i].alloced) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002198 /* ensure access through shadow region 0 */
2199 edma_or_array2(ecc, EDMA_DRAE, 0, i >> 5,
2200 BIT(i & 0x1f));
2201
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002202 edma_setup_interrupt(&echan[i], true);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002203
2204 /* Set up channel -> slot mapping for the entry slot */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002205 edma_set_chmap(&echan[i], echan[i].slot[0]);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002206 }
2207 }
2208
2209 return 0;
2210}
2211#endif
2212
2213static const struct dev_pm_ops edma_pm_ops = {
2214 SET_LATE_SYSTEM_SLEEP_PM_OPS(NULL, edma_pm_resume)
2215};
2216
Matt Porterc2dde5f2012-08-22 21:09:34 -04002217static struct platform_driver edma_driver = {
2218 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05002219 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002220 .driver = {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002221 .name = "edma",
2222 .pm = &edma_pm_ops,
2223 .of_match_table = edma_of_ids,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002224 },
2225};
2226
2227bool edma_filter_fn(struct dma_chan *chan, void *param)
2228{
2229 if (chan->device->dev->driver == &edma_driver.driver) {
2230 struct edma_chan *echan = to_edma_chan(chan);
2231 unsigned ch_req = *(unsigned *)param;
2232 return ch_req == echan->ch_num;
2233 }
2234 return false;
2235}
2236EXPORT_SYMBOL(edma_filter_fn);
2237
Matt Porterc2dde5f2012-08-22 21:09:34 -04002238static int edma_init(void)
2239{
Arnd Bergmann5305e4d2014-10-24 18:14:01 +02002240 return platform_driver_register(&edma_driver);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002241}
2242subsys_initcall(edma_init);
2243
2244static void __exit edma_exit(void)
2245{
Matt Porterc2dde5f2012-08-22 21:09:34 -04002246 platform_driver_unregister(&edma_driver);
2247}
2248module_exit(edma_exit);
2249
Josh Boyerd71505b2013-09-04 10:32:50 -04002250MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002251MODULE_DESCRIPTION("TI EDMA DMA engine driver");
2252MODULE_LICENSE("GPL v2");