blob: aeb67e0cc523906d26b0cb9066d65657cfabe400 [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/*
116 * This will go away when the private EDMA API is folded
117 * into this driver and the platform device(s) are
118 * instantiated in the arch code. We can only get away
119 * with this simplification because DA8XX may not be built
120 * in the same kernel image with other DaVinci parts. This
121 * avoids having to sprinkle dmaengine driver platform devices
122 * and data throughout all the existing board files.
123 */
124#ifdef CONFIG_ARCH_DAVINCI_DA8XX
125#define EDMA_CTLRS 2
126#define EDMA_CHANS 32
127#else
128#define EDMA_CTLRS 1
129#define EDMA_CHANS 64
130#endif /* CONFIG_ARCH_DAVINCI_DA8XX */
131
Joel Fernandes2abd5f12013-09-23 18:05:15 -0500132/*
133 * Max of 20 segments per channel to conserve PaRAM slots
134 * Also note that MAX_NR_SG should be atleast the no.of periods
135 * that are required for ASoC, otherwise DMA prep calls will
136 * fail. Today davinci-pcm is the only user of this driver and
137 * requires atleast 17 slots, so we setup the default to 20.
138 */
139#define MAX_NR_SG 20
Matt Porterc2dde5f2012-08-22 21:09:34 -0400140#define EDMA_MAX_SLOTS MAX_NR_SG
141#define EDMA_DESCRIPTORS 16
142
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300143#define EDMA_MAX_PARAMENTRY 512
144
145#define EDMA_CHANNEL_ANY -1 /* for edma_alloc_channel() */
146#define EDMA_SLOT_ANY -1 /* for edma_alloc_slot() */
147#define EDMA_CONT_PARAMS_ANY 1001
148#define EDMA_CONT_PARAMS_FIXED_EXACT 1002
149#define EDMA_CONT_PARAMS_FIXED_NOT_EXACT 1003
150
151#define EDMA_MAX_CC 2
152
153/* PaRAM slots are laid out like this */
154struct edmacc_param {
155 u32 opt;
156 u32 src;
157 u32 a_b_cnt;
158 u32 dst;
159 u32 src_dst_bidx;
160 u32 link_bcntrld;
161 u32 src_dst_cidx;
162 u32 ccnt;
163} __packed;
164
165/* fields in edmacc_param.opt */
166#define SAM BIT(0)
167#define DAM BIT(1)
168#define SYNCDIM BIT(2)
169#define STATIC BIT(3)
170#define EDMA_FWID (0x07 << 8)
171#define TCCMODE BIT(11)
172#define EDMA_TCC(t) ((t) << 12)
173#define TCINTEN BIT(20)
174#define ITCINTEN BIT(21)
175#define TCCHEN BIT(22)
176#define ITCCHEN BIT(23)
177
178/*ch_status parameter of callback function possible values*/
179#define EDMA_DMA_COMPLETE 1
180#define EDMA_DMA_CC_ERROR 2
181#define EDMA_DMA_TC1_ERROR 3
182#define EDMA_DMA_TC2_ERROR 4
183
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500184struct edma_pset {
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500185 u32 len;
186 dma_addr_t addr;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500187 struct edmacc_param param;
188};
189
Matt Porterc2dde5f2012-08-22 21:09:34 -0400190struct edma_desc {
191 struct virt_dma_desc vdesc;
192 struct list_head node;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500193 enum dma_transfer_direction direction;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500194 int cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400195 int absync;
196 int pset_nr;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500197 struct edma_chan *echan;
Joel Fernandes04361d82014-04-28 15:19:31 -0500198 int processed;
199
200 /*
201 * The following 4 elements are used for residue accounting.
202 *
203 * - processed_stat: the number of SG elements we have traversed
204 * so far to cover accounting. This is updated directly to processed
205 * during edma_callback and is always <= processed, because processed
206 * refers to the number of pending transfer (programmed to EDMA
207 * controller), where as processed_stat tracks number of transfers
208 * accounted for so far.
209 *
210 * - residue: The amount of bytes we have left to transfer for this desc
211 *
212 * - residue_stat: The residue in bytes of data we have covered
213 * so far for accounting. This is updated directly to residue
214 * during callbacks to keep it current.
215 *
216 * - sg_len: Tracks the length of the current intermediate transfer,
217 * this is required to update the residue during intermediate transfer
218 * completion callback.
219 */
220 int processed_stat;
221 u32 sg_len;
222 u32 residue;
223 u32 residue_stat;
224
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500225 struct edma_pset pset[0];
Matt Porterc2dde5f2012-08-22 21:09:34 -0400226};
227
228struct edma_cc;
229
230struct edma_chan {
231 struct virt_dma_chan vchan;
232 struct list_head node;
233 struct edma_desc *edesc;
234 struct edma_cc *ecc;
235 int ch_num;
236 bool alloced;
237 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -0500238 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -0500239 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400240};
241
242struct edma_cc {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300243 struct device *dev;
244 struct edma_soc_info *info;
245 void __iomem *base;
246 int id;
247
248 /* eDMA3 resource information */
249 unsigned num_channels;
250 unsigned num_region;
251 unsigned num_slots;
252 unsigned num_tc;
253 enum dma_event_q default_queue;
254
255 bool unused_chan_list_done;
256 /* The edma_inuse bit for each PaRAM slot is clear unless the
257 * channel is in use ... by ARM or DSP, for QDMA, or whatever.
258 */
259 DECLARE_BITMAP(edma_inuse, EDMA_MAX_PARAMENTRY);
260
261 /* The edma_unused bit for each channel is clear unless
262 * it is not being used on this platform. It uses a bit
263 * of SOC-specific initialization code.
264 */
265 DECLARE_BITMAP(edma_unused, EDMA_CHANS);
266
267 struct dma_interrupt_data {
268 void (*callback)(unsigned channel, unsigned short ch_status,
269 void *data);
270 void *data;
271 } intr_data[EDMA_CHANS];
272
Matt Porterc2dde5f2012-08-22 21:09:34 -0400273 struct dma_device dma_slave;
274 struct edma_chan slave_chans[EDMA_CHANS];
Matt Porterc2dde5f2012-08-22 21:09:34 -0400275 int dummy_slot;
276};
277
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300278/* dummy param set used to (re)initialize parameter RAM slots */
279static const struct edmacc_param dummy_paramset = {
280 .link_bcntrld = 0xffff,
281 .ccnt = 1,
282};
283
284static const struct of_device_id edma_of_ids[] = {
285 { .compatible = "ti,edma3", },
286 {}
287};
288
289static inline unsigned int edma_read(struct edma_cc *ecc, int offset)
290{
291 return (unsigned int)__raw_readl(ecc->base + offset);
292}
293
294static inline void edma_write(struct edma_cc *ecc, int offset, int val)
295{
296 __raw_writel(val, ecc->base + offset);
297}
298
299static inline void edma_modify(struct edma_cc *ecc, int offset, unsigned and,
300 unsigned or)
301{
302 unsigned val = edma_read(ecc, offset);
303
304 val &= and;
305 val |= or;
306 edma_write(ecc, offset, val);
307}
308
309static inline void edma_and(struct edma_cc *ecc, int offset, unsigned and)
310{
311 unsigned val = edma_read(ecc, offset);
312
313 val &= and;
314 edma_write(ecc, offset, val);
315}
316
317static inline void edma_or(struct edma_cc *ecc, int offset, unsigned or)
318{
319 unsigned val = edma_read(ecc, offset);
320
321 val |= or;
322 edma_write(ecc, offset, val);
323}
324
325static inline unsigned int edma_read_array(struct edma_cc *ecc, int offset,
326 int i)
327{
328 return edma_read(ecc, offset + (i << 2));
329}
330
331static inline void edma_write_array(struct edma_cc *ecc, int offset, int i,
332 unsigned val)
333{
334 edma_write(ecc, offset + (i << 2), val);
335}
336
337static inline void edma_modify_array(struct edma_cc *ecc, int offset, int i,
338 unsigned and, unsigned or)
339{
340 edma_modify(ecc, offset + (i << 2), and, or);
341}
342
343static inline void edma_or_array(struct edma_cc *ecc, int offset, int i,
344 unsigned or)
345{
346 edma_or(ecc, offset + (i << 2), or);
347}
348
349static inline void edma_or_array2(struct edma_cc *ecc, int offset, int i, int j,
350 unsigned or)
351{
352 edma_or(ecc, offset + ((i * 2 + j) << 2), or);
353}
354
355static inline void edma_write_array2(struct edma_cc *ecc, int offset, int i,
356 int j, unsigned val)
357{
358 edma_write(ecc, offset + ((i * 2 + j) << 2), val);
359}
360
361static inline unsigned int edma_shadow0_read(struct edma_cc *ecc, int offset)
362{
363 return edma_read(ecc, EDMA_SHADOW0 + offset);
364}
365
366static inline unsigned int edma_shadow0_read_array(struct edma_cc *ecc,
367 int offset, int i)
368{
369 return edma_read(ecc, EDMA_SHADOW0 + offset + (i << 2));
370}
371
372static inline void edma_shadow0_write(struct edma_cc *ecc, int offset,
373 unsigned val)
374{
375 edma_write(ecc, EDMA_SHADOW0 + offset, val);
376}
377
378static inline void edma_shadow0_write_array(struct edma_cc *ecc, int offset,
379 int i, unsigned val)
380{
381 edma_write(ecc, EDMA_SHADOW0 + offset + (i << 2), val);
382}
383
384static inline unsigned int edma_parm_read(struct edma_cc *ecc, int offset,
385 int param_no)
386{
387 return edma_read(ecc, EDMA_PARM + offset + (param_no << 5));
388}
389
390static inline void edma_parm_write(struct edma_cc *ecc, int offset,
391 int param_no, unsigned val)
392{
393 edma_write(ecc, EDMA_PARM + offset + (param_no << 5), val);
394}
395
396static inline void edma_parm_modify(struct edma_cc *ecc, int offset,
397 int param_no, unsigned and, unsigned or)
398{
399 edma_modify(ecc, EDMA_PARM + offset + (param_no << 5), and, or);
400}
401
402static inline void edma_parm_and(struct edma_cc *ecc, int offset, int param_no,
403 unsigned and)
404{
405 edma_and(ecc, EDMA_PARM + offset + (param_no << 5), and);
406}
407
408static inline void edma_parm_or(struct edma_cc *ecc, int offset, int param_no,
409 unsigned or)
410{
411 edma_or(ecc, EDMA_PARM + offset + (param_no << 5), or);
412}
413
414static inline void set_bits(int offset, int len, unsigned long *p)
415{
416 for (; len > 0; len--)
417 set_bit(offset + (len - 1), p);
418}
419
420static inline void clear_bits(int offset, int len, unsigned long *p)
421{
422 for (; len > 0; len--)
423 clear_bit(offset + (len - 1), p);
424}
425
426static void edma_map_dmach_to_queue(struct edma_cc *ecc, unsigned ch_no,
427 enum dma_event_q queue_no)
428{
429 int bit = (ch_no & 0x7) * 4;
430
431 /* default to low priority queue */
432 if (queue_no == EVENTQ_DEFAULT)
433 queue_no = ecc->default_queue;
434
435 queue_no &= 7;
436 edma_modify_array(ecc, EDMA_DMAQNUM, (ch_no >> 3), ~(0x7 << bit),
437 queue_no << bit);
438}
439
440static void edma_assign_priority_to_queue(struct edma_cc *ecc, int queue_no,
441 int priority)
442{
443 int bit = queue_no * 4;
444
445 edma_modify(ecc, EDMA_QUEPRI, ~(0x7 << bit), ((priority & 0x7) << bit));
446}
447
448static void edma_direct_dmach_to_param_mapping(struct edma_cc *ecc)
449{
450 int i;
451
452 for (i = 0; i < ecc->num_channels; i++)
453 edma_write_array(ecc, EDMA_DCHMAP, i, (i << 5));
454}
455
456static int prepare_unused_channel_list(struct device *dev, void *data)
457{
458 struct platform_device *pdev = to_platform_device(dev);
459 struct edma_cc *ecc = data;
460 int i, count;
461 struct of_phandle_args dma_spec;
462
463 if (dev->of_node) {
464 struct platform_device *dma_pdev;
465
466 count = of_property_count_strings(dev->of_node, "dma-names");
467 if (count < 0)
468 return 0;
469 for (i = 0; i < count; i++) {
470 if (of_parse_phandle_with_args(dev->of_node, "dmas",
471 "#dma-cells", i,
472 &dma_spec))
473 continue;
474
475 if (!of_match_node(edma_of_ids, dma_spec.np)) {
476 of_node_put(dma_spec.np);
477 continue;
478 }
479
480 dma_pdev = of_find_device_by_node(dma_spec.np);
481 if (&dma_pdev->dev != ecc->dev)
482 continue;
483
484 clear_bit(EDMA_CHAN_SLOT(dma_spec.args[0]),
485 ecc->edma_unused);
486 of_node_put(dma_spec.np);
487 }
488 return 0;
489 }
490
491 /* For non-OF case */
492 for (i = 0; i < pdev->num_resources; i++) {
493 struct resource *res = &pdev->resource[i];
494
495 if ((res->flags & IORESOURCE_DMA) && (int)res->start >= 0) {
496 clear_bit(EDMA_CHAN_SLOT(pdev->resource[i].start),
497 ecc->edma_unused);
498 }
499 }
500
501 return 0;
502}
503
504static void edma_setup_interrupt(struct edma_cc *ecc, unsigned lch,
505 void (*callback)(unsigned channel, u16 ch_status, void *data),
506 void *data)
507{
508 lch = EDMA_CHAN_SLOT(lch);
509
510 if (!callback)
511 edma_shadow0_write_array(ecc, SH_IECR, lch >> 5,
512 BIT(lch & 0x1f));
513
514 ecc->intr_data[lch].callback = callback;
515 ecc->intr_data[lch].data = data;
516
517 if (callback) {
518 edma_shadow0_write_array(ecc, SH_ICR, lch >> 5,
519 BIT(lch & 0x1f));
520 edma_shadow0_write_array(ecc, SH_IESR, lch >> 5,
521 BIT(lch & 0x1f));
522 }
523}
524
525/*
526 * paRAM management functions
527 */
528
529/**
530 * edma_write_slot - write parameter RAM data for slot
531 * @ecc: pointer to edma_cc struct
532 * @slot: number of parameter RAM slot being modified
533 * @param: data to be written into parameter RAM slot
534 *
535 * Use this to assign all parameters of a transfer at once. This
536 * allows more efficient setup of transfers than issuing multiple
537 * calls to set up those parameters in small pieces, and provides
538 * complete control over all transfer options.
539 */
540static void edma_write_slot(struct edma_cc *ecc, unsigned slot,
541 const struct edmacc_param *param)
542{
543 slot = EDMA_CHAN_SLOT(slot);
544 if (slot >= ecc->num_slots)
545 return;
546 memcpy_toio(ecc->base + PARM_OFFSET(slot), param, PARM_SIZE);
547}
548
549/**
550 * edma_read_slot - read parameter RAM data from slot
551 * @ecc: pointer to edma_cc struct
552 * @slot: number of parameter RAM slot being copied
553 * @param: where to store copy of parameter RAM data
554 *
555 * Use this to read data from a parameter RAM slot, perhaps to
556 * save them as a template for later reuse.
557 */
558static void edma_read_slot(struct edma_cc *ecc, unsigned slot,
559 struct edmacc_param *param)
560{
561 slot = EDMA_CHAN_SLOT(slot);
562 if (slot >= ecc->num_slots)
563 return;
564 memcpy_fromio(param, ecc->base + PARM_OFFSET(slot), PARM_SIZE);
565}
566
567/**
568 * edma_alloc_slot - allocate DMA parameter RAM
569 * @ecc: pointer to edma_cc struct
570 * @slot: specific slot to allocate; negative for "any unused slot"
571 *
572 * This allocates a parameter RAM slot, initializing it to hold a
573 * dummy transfer. Slots allocated using this routine have not been
574 * mapped to a hardware DMA channel, and will normally be used by
575 * linking to them from a slot associated with a DMA channel.
576 *
577 * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
578 * slots may be allocated on behalf of DSP firmware.
579 *
580 * Returns the number of the slot, else negative errno.
581 */
582static int edma_alloc_slot(struct edma_cc *ecc, int slot)
583{
584 if (slot > 0)
585 slot = EDMA_CHAN_SLOT(slot);
586 if (slot < 0) {
587 slot = ecc->num_channels;
588 for (;;) {
589 slot = find_next_zero_bit(ecc->edma_inuse,
590 ecc->num_slots,
591 slot);
592 if (slot == ecc->num_slots)
593 return -ENOMEM;
594 if (!test_and_set_bit(slot, ecc->edma_inuse))
595 break;
596 }
597 } else if (slot < ecc->num_channels || slot >= ecc->num_slots) {
598 return -EINVAL;
599 } else if (test_and_set_bit(slot, ecc->edma_inuse)) {
600 return -EBUSY;
601 }
602
603 edma_write_slot(ecc, slot, &dummy_paramset);
604
605 return EDMA_CTLR_CHAN(ecc->id, slot);
606}
607
608/**
609 * edma_free_slot - deallocate DMA parameter RAM
610 * @ecc: pointer to edma_cc struct
611 * @slot: parameter RAM slot returned from edma_alloc_slot()
612 *
613 * This deallocates the parameter RAM slot allocated by edma_alloc_slot().
614 * Callers are responsible for ensuring the slot is inactive, and will
615 * not be activated.
616 */
617static void edma_free_slot(struct edma_cc *ecc, unsigned slot)
618{
619 slot = EDMA_CHAN_SLOT(slot);
620 if (slot < ecc->num_channels || slot >= ecc->num_slots)
621 return;
622
623 edma_write_slot(ecc, slot, &dummy_paramset);
624 clear_bit(slot, ecc->edma_inuse);
625}
626
627/**
628 * edma_link - link one parameter RAM slot to another
629 * @ecc: pointer to edma_cc struct
630 * @from: parameter RAM slot originating the link
631 * @to: parameter RAM slot which is the link target
632 *
633 * The originating slot should not be part of any active DMA transfer.
634 */
635static void edma_link(struct edma_cc *ecc, unsigned from, unsigned to)
636{
637 from = EDMA_CHAN_SLOT(from);
638 to = EDMA_CHAN_SLOT(to);
639 if (from >= ecc->num_slots || to >= ecc->num_slots)
640 return;
641
642 edma_parm_modify(ecc, PARM_LINK_BCNTRLD, from, 0xffff0000,
643 PARM_OFFSET(to));
644}
645
646/**
647 * edma_get_position - returns the current transfer point
648 * @ecc: pointer to edma_cc struct
649 * @slot: parameter RAM slot being examined
650 * @dst: true selects the dest position, false the source
651 *
652 * Returns the position of the current active slot
653 */
654static dma_addr_t edma_get_position(struct edma_cc *ecc, unsigned slot,
655 bool dst)
656{
657 u32 offs;
658
659 slot = EDMA_CHAN_SLOT(slot);
660 offs = PARM_OFFSET(slot);
661 offs += dst ? PARM_DST : PARM_SRC;
662
663 return edma_read(ecc, offs);
664}
665
666/*-----------------------------------------------------------------------*/
667/**
668 * edma_start - start dma on a channel
669 * @ecc: pointer to edma_cc struct
670 * @channel: channel being activated
671 *
672 * Channels with event associations will be triggered by their hardware
673 * events, and channels without such associations will be triggered by
674 * software. (At this writing there is no interface for using software
675 * triggers except with channels that don't support hardware triggers.)
676 *
677 * Returns zero on success, else negative errno.
678 */
679static int edma_start(struct edma_cc *ecc, unsigned channel)
680{
681 if (ecc->id != EDMA_CTLR(channel)) {
682 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
683 ecc->id, EDMA_CTLR(channel));
684 return -EINVAL;
685 }
686 channel = EDMA_CHAN_SLOT(channel);
687
688 if (channel < ecc->num_channels) {
689 int j = channel >> 5;
690 unsigned int mask = BIT(channel & 0x1f);
691
692 /* EDMA channels without event association */
693 if (test_bit(channel, ecc->edma_unused)) {
694 pr_debug("EDMA: ESR%d %08x\n", j,
695 edma_shadow0_read_array(ecc, SH_ESR, j));
696 edma_shadow0_write_array(ecc, SH_ESR, j, mask);
697 return 0;
698 }
699
700 /* EDMA channel with event association */
701 pr_debug("EDMA: ER%d %08x\n", j,
702 edma_shadow0_read_array(ecc, SH_ER, j));
703 /* Clear any pending event or error */
704 edma_write_array(ecc, EDMA_ECR, j, mask);
705 edma_write_array(ecc, EDMA_EMCR, j, mask);
706 /* Clear any SER */
707 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
708 edma_shadow0_write_array(ecc, SH_EESR, j, mask);
709 pr_debug("EDMA: EER%d %08x\n", j,
710 edma_shadow0_read_array(ecc, SH_EER, j));
711 return 0;
712 }
713
714 return -EINVAL;
715}
716
717/**
718 * edma_stop - stops dma on the channel passed
719 * @ecc: pointer to edma_cc struct
720 * @channel: channel being deactivated
721 *
722 * When @lch is a channel, any active transfer is paused and
723 * all pending hardware events are cleared. The current transfer
724 * may not be resumed, and the channel's Parameter RAM should be
725 * reinitialized before being reused.
726 */
727static void edma_stop(struct edma_cc *ecc, unsigned channel)
728{
729 if (ecc->id != EDMA_CTLR(channel)) {
730 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
731 ecc->id, EDMA_CTLR(channel));
732 return;
733 }
734 channel = EDMA_CHAN_SLOT(channel);
735
736 if (channel < ecc->num_channels) {
737 int j = channel >> 5;
738 unsigned int mask = BIT(channel & 0x1f);
739
740 edma_shadow0_write_array(ecc, SH_EECR, j, mask);
741 edma_shadow0_write_array(ecc, SH_ECR, j, mask);
742 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
743 edma_write_array(ecc, EDMA_EMCR, j, mask);
744
745 /* clear possibly pending completion interrupt */
746 edma_shadow0_write_array(ecc, SH_ICR, j, mask);
747
748 pr_debug("EDMA: EER%d %08x\n", j,
749 edma_shadow0_read_array(ecc, SH_EER, j));
750
751 /* REVISIT: consider guarding against inappropriate event
752 * chaining by overwriting with dummy_paramset.
753 */
754 }
755}
756
757/**
758 * edma_pause - pause dma on a channel
759 * @ecc: pointer to edma_cc struct
760 * @channel: on which edma_start() has been called
761 *
762 * This temporarily disables EDMA hardware events on the specified channel,
763 * preventing them from triggering new transfers on its behalf
764 */
765static void edma_pause(struct edma_cc *ecc, unsigned channel)
766{
767 if (ecc->id != EDMA_CTLR(channel)) {
768 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
769 ecc->id, EDMA_CTLR(channel));
770 return;
771 }
772 channel = EDMA_CHAN_SLOT(channel);
773
774 if (channel < ecc->num_channels) {
775 unsigned int mask = BIT(channel & 0x1f);
776
777 edma_shadow0_write_array(ecc, SH_EECR, channel >> 5, mask);
778 }
779}
780
781/**
782 * edma_resume - resumes dma on a paused channel
783 * @ecc: pointer to edma_cc struct
784 * @channel: on which edma_pause() has been called
785 *
786 * This re-enables EDMA hardware events on the specified channel.
787 */
788static void edma_resume(struct edma_cc *ecc, unsigned channel)
789{
790 if (ecc->id != EDMA_CTLR(channel)) {
791 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
792 ecc->id, EDMA_CTLR(channel));
793 return;
794 }
795 channel = EDMA_CHAN_SLOT(channel);
796
797 if (channel < ecc->num_channels) {
798 unsigned int mask = BIT(channel & 0x1f);
799
800 edma_shadow0_write_array(ecc, SH_EESR, channel >> 5, mask);
801 }
802}
803
804static int edma_trigger_channel(struct edma_cc *ecc, unsigned channel)
805{
806 unsigned int mask;
807
808 if (ecc->id != EDMA_CTLR(channel)) {
809 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
810 ecc->id, EDMA_CTLR(channel));
811 return -EINVAL;
812 }
813 channel = EDMA_CHAN_SLOT(channel);
814 mask = BIT(channel & 0x1f);
815
816 edma_shadow0_write_array(ecc, SH_ESR, (channel >> 5), mask);
817
818 pr_debug("EDMA: ESR%d %08x\n", (channel >> 5),
819 edma_shadow0_read_array(ecc, SH_ESR, (channel >> 5)));
820 return 0;
821}
822
823/******************************************************************************
824 *
825 * It cleans ParamEntry qand bring back EDMA to initial state if media has
826 * been removed before EDMA has finished.It is usedful for removable media.
827 * Arguments:
828 * ch_no - channel no
829 *
830 * Return: zero on success, or corresponding error no on failure
831 *
832 * FIXME this should not be needed ... edma_stop() should suffice.
833 *
834 *****************************************************************************/
835
836static void edma_clean_channel(struct edma_cc *ecc, unsigned channel)
837{
838 if (ecc->id != EDMA_CTLR(channel)) {
839 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
840 ecc->id, EDMA_CTLR(channel));
841 return;
842 }
843 channel = EDMA_CHAN_SLOT(channel);
844
845 if (channel < ecc->num_channels) {
846 int j = (channel >> 5);
847 unsigned int mask = BIT(channel & 0x1f);
848
849 pr_debug("EDMA: EMR%d %08x\n", j,
850 edma_read_array(ecc, EDMA_EMR, j));
851 edma_shadow0_write_array(ecc, SH_ECR, j, mask);
852 /* Clear the corresponding EMR bits */
853 edma_write_array(ecc, EDMA_EMCR, j, mask);
854 /* Clear any SER */
855 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
856 edma_write(ecc, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
857 }
858}
859
860/**
861 * edma_alloc_channel - allocate DMA channel and paired parameter RAM
862 * @ecc: pointer to edma_cc struct
863 * @channel: specific channel to allocate; negative for "any unmapped channel"
864 * @callback: optional; to be issued on DMA completion or errors
865 * @data: passed to callback
866 * @eventq_no: an EVENTQ_* constant, used to choose which Transfer
867 * Controller (TC) executes requests using this channel. Use
868 * EVENTQ_DEFAULT unless you really need a high priority queue.
869 *
870 * This allocates a DMA channel and its associated parameter RAM slot.
871 * The parameter RAM is initialized to hold a dummy transfer.
872 *
873 * Normal use is to pass a specific channel number as @channel, to make
874 * use of hardware events mapped to that channel. When the channel will
875 * be used only for software triggering or event chaining, channels not
876 * mapped to hardware events (or mapped to unused events) are preferable.
877 *
878 * DMA transfers start from a channel using edma_start(), or by
879 * chaining. When the transfer described in that channel's parameter RAM
880 * slot completes, that slot's data may be reloaded through a link.
881 *
882 * DMA errors are only reported to the @callback associated with the
883 * channel driving that transfer, but transfer completion callbacks can
884 * be sent to another channel under control of the TCC field in
885 * the option word of the transfer's parameter RAM set. Drivers must not
886 * use DMA transfer completion callbacks for channels they did not allocate.
887 * (The same applies to TCC codes used in transfer chaining.)
888 *
889 * Returns the number of the channel, else negative errno.
890 */
891static int edma_alloc_channel(struct edma_cc *ecc, int channel,
892 void (*callback)(unsigned channel, u16 ch_status, void *data),
893 void *data,
894 enum dma_event_q eventq_no)
895{
896 unsigned done = 0;
897 int ret = 0;
898
899 if (!ecc->unused_chan_list_done) {
900 /*
901 * Scan all the platform devices to find out the EDMA channels
902 * used and clear them in the unused list, making the rest
903 * available for ARM usage.
904 */
905 ret = bus_for_each_dev(&platform_bus_type, NULL, ecc,
906 prepare_unused_channel_list);
907 if (ret < 0)
908 return ret;
909
910 ecc->unused_chan_list_done = true;
911 }
912
913 if (channel >= 0) {
914 if (ecc->id != EDMA_CTLR(channel)) {
915 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n",
916 __func__, ecc->id, EDMA_CTLR(channel));
917 return -EINVAL;
918 }
919 channel = EDMA_CHAN_SLOT(channel);
920 }
921
922 if (channel < 0) {
923 channel = 0;
924 for (;;) {
925 channel = find_next_bit(ecc->edma_unused,
926 ecc->num_channels, channel);
927 if (channel == ecc->num_channels)
928 break;
929 if (!test_and_set_bit(channel, ecc->edma_inuse)) {
930 done = 1;
931 break;
932 }
933 channel++;
934 }
935 if (!done)
936 return -ENOMEM;
937 } else if (channel >= ecc->num_channels) {
938 return -EINVAL;
939 } else if (test_and_set_bit(channel, ecc->edma_inuse)) {
940 return -EBUSY;
941 }
942
943 /* ensure access through shadow region 0 */
944 edma_or_array2(ecc, EDMA_DRAE, 0, channel >> 5, BIT(channel & 0x1f));
945
946 /* ensure no events are pending */
947 edma_stop(ecc, EDMA_CTLR_CHAN(ecc->id, channel));
948 edma_write_slot(ecc, channel, &dummy_paramset);
949
950 if (callback)
951 edma_setup_interrupt(ecc, EDMA_CTLR_CHAN(ecc->id, channel),
952 callback, data);
953
954 edma_map_dmach_to_queue(ecc, channel, eventq_no);
955
956 return EDMA_CTLR_CHAN(ecc->id, channel);
957}
958
959/**
960 * edma_free_channel - deallocate DMA channel
961 * @ecc: pointer to edma_cc struct
962 * @channel: dma channel returned from edma_alloc_channel()
963 *
964 * This deallocates the DMA channel and associated parameter RAM slot
965 * allocated by edma_alloc_channel().
966 *
967 * Callers are responsible for ensuring the channel is inactive, and
968 * will not be reactivated by linking, chaining, or software calls to
969 * edma_start().
970 */
971static void edma_free_channel(struct edma_cc *ecc, unsigned channel)
972{
973 if (ecc->id != EDMA_CTLR(channel)) {
974 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
975 ecc->id, EDMA_CTLR(channel));
976 return;
977 }
978 channel = EDMA_CHAN_SLOT(channel);
979
980 if (channel >= ecc->num_channels)
981 return;
982
983 edma_setup_interrupt(ecc, channel, NULL, NULL);
984 /* REVISIT should probably take out of shadow region 0 */
985
986 memcpy_toio(ecc->base + PARM_OFFSET(channel), &dummy_paramset,
987 PARM_SIZE);
988 clear_bit(channel, ecc->edma_inuse);
989}
990
991/*
992 * edma_assign_channel_eventq - move given channel to desired eventq
993 * Arguments:
994 * channel - channel number
995 * eventq_no - queue to move the channel
996 *
997 * Can be used to move a channel to a selected event queue.
998 */
999static void edma_assign_channel_eventq(struct edma_cc *ecc, unsigned channel,
1000 enum dma_event_q eventq_no)
1001{
1002 if (ecc->id != EDMA_CTLR(channel)) {
1003 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
1004 ecc->id, EDMA_CTLR(channel));
1005 return;
1006 }
1007 channel = EDMA_CHAN_SLOT(channel);
1008
1009 if (channel >= ecc->num_channels)
1010 return;
1011
1012 /* default to low priority queue */
1013 if (eventq_no == EVENTQ_DEFAULT)
1014 eventq_no = ecc->default_queue;
1015 if (eventq_no >= ecc->num_tc)
1016 return;
1017
1018 edma_map_dmach_to_queue(ecc, channel, eventq_no);
1019}
1020
1021static irqreturn_t dma_irq_handler(int irq, void *data)
1022{
1023 struct edma_cc *ecc = data;
1024 int ctlr;
1025 u32 sh_ier;
1026 u32 sh_ipr;
1027 u32 bank;
1028
1029 ctlr = ecc->id;
1030 if (ctlr < 0)
1031 return IRQ_NONE;
1032
1033 dev_dbg(ecc->dev, "dma_irq_handler\n");
1034
1035 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 0);
1036 if (!sh_ipr) {
1037 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 1);
1038 if (!sh_ipr)
1039 return IRQ_NONE;
1040 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 1);
1041 bank = 1;
1042 } else {
1043 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 0);
1044 bank = 0;
1045 }
1046
1047 do {
1048 u32 slot;
1049 u32 channel;
1050
1051 dev_dbg(ecc->dev, "IPR%d %08x\n", bank, sh_ipr);
1052
1053 slot = __ffs(sh_ipr);
1054 sh_ipr &= ~(BIT(slot));
1055
1056 if (sh_ier & BIT(slot)) {
1057 channel = (bank << 5) | slot;
1058 /* Clear the corresponding IPR bits */
1059 edma_shadow0_write_array(ecc, SH_ICR, bank, BIT(slot));
1060 if (ecc->intr_data[channel].callback)
1061 ecc->intr_data[channel].callback(
1062 EDMA_CTLR_CHAN(ctlr, channel),
1063 EDMA_DMA_COMPLETE,
1064 ecc->intr_data[channel].data);
1065 }
1066 } while (sh_ipr);
1067
1068 edma_shadow0_write(ecc, SH_IEVAL, 1);
1069 return IRQ_HANDLED;
1070}
1071
1072/******************************************************************************
1073 *
1074 * DMA error interrupt handler
1075 *
1076 *****************************************************************************/
1077static irqreturn_t dma_ccerr_handler(int irq, void *data)
1078{
1079 struct edma_cc *ecc = data;
1080 int i;
1081 int ctlr;
1082 unsigned int cnt = 0;
1083
1084 ctlr = ecc->id;
1085 if (ctlr < 0)
1086 return IRQ_NONE;
1087
1088 dev_dbg(ecc->dev, "dma_ccerr_handler\n");
1089
1090 if ((edma_read_array(ecc, EDMA_EMR, 0) == 0) &&
1091 (edma_read_array(ecc, EDMA_EMR, 1) == 0) &&
1092 (edma_read(ecc, EDMA_QEMR) == 0) &&
1093 (edma_read(ecc, EDMA_CCERR) == 0))
1094 return IRQ_NONE;
1095
1096 while (1) {
1097 int j = -1;
1098
1099 if (edma_read_array(ecc, EDMA_EMR, 0))
1100 j = 0;
1101 else if (edma_read_array(ecc, EDMA_EMR, 1))
1102 j = 1;
1103 if (j >= 0) {
1104 dev_dbg(ecc->dev, "EMR%d %08x\n", j,
1105 edma_read_array(ecc, EDMA_EMR, j));
1106 for (i = 0; i < 32; i++) {
1107 int k = (j << 5) + i;
1108
1109 if (edma_read_array(ecc, EDMA_EMR, j) &
1110 BIT(i)) {
1111 /* Clear the corresponding EMR bits */
1112 edma_write_array(ecc, EDMA_EMCR, j,
1113 BIT(i));
1114 /* Clear any SER */
1115 edma_shadow0_write_array(ecc, SH_SECR,
1116 j, BIT(i));
1117 if (ecc->intr_data[k].callback) {
1118 ecc->intr_data[k].callback(
1119 EDMA_CTLR_CHAN(ctlr, k),
1120 EDMA_DMA_CC_ERROR,
1121 ecc->intr_data[k].data);
1122 }
1123 }
1124 }
1125 } else if (edma_read(ecc, EDMA_QEMR)) {
1126 dev_dbg(ecc->dev, "QEMR %02x\n",
1127 edma_read(ecc, EDMA_QEMR));
1128 for (i = 0; i < 8; i++) {
1129 if (edma_read(ecc, EDMA_QEMR) & BIT(i)) {
1130 /* Clear the corresponding IPR bits */
1131 edma_write(ecc, EDMA_QEMCR, BIT(i));
1132 edma_shadow0_write(ecc, SH_QSECR,
1133 BIT(i));
1134
1135 /* NOTE: not reported!! */
1136 }
1137 }
1138 } else if (edma_read(ecc, EDMA_CCERR)) {
1139 dev_dbg(ecc->dev, "CCERR %08x\n",
1140 edma_read(ecc, EDMA_CCERR));
1141 /* FIXME: CCERR.BIT(16) ignored! much better
1142 * to just write CCERRCLR with CCERR value...
1143 */
1144 for (i = 0; i < 8; i++) {
1145 if (edma_read(ecc, EDMA_CCERR) & BIT(i)) {
1146 /* Clear the corresponding IPR bits */
1147 edma_write(ecc, EDMA_CCERRCLR, BIT(i));
1148
1149 /* NOTE: not reported!! */
1150 }
1151 }
1152 }
1153 if ((edma_read_array(ecc, EDMA_EMR, 0) == 0) &&
1154 (edma_read_array(ecc, EDMA_EMR, 1) == 0) &&
1155 (edma_read(ecc, EDMA_QEMR) == 0) &&
1156 (edma_read(ecc, EDMA_CCERR) == 0))
1157 break;
1158 cnt++;
1159 if (cnt > 10)
1160 break;
1161 }
1162 edma_write(ecc, EDMA_EEVAL, 1);
1163 return IRQ_HANDLED;
1164}
1165
Matt Porterc2dde5f2012-08-22 21:09:34 -04001166static inline struct edma_cc *to_edma_cc(struct dma_device *d)
1167{
1168 return container_of(d, struct edma_cc, dma_slave);
1169}
1170
1171static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
1172{
1173 return container_of(c, struct edma_chan, vchan.chan);
1174}
1175
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001176static inline struct edma_desc *to_edma_desc(struct dma_async_tx_descriptor *tx)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001177{
1178 return container_of(tx, struct edma_desc, vdesc.tx);
1179}
1180
1181static void edma_desc_free(struct virt_dma_desc *vdesc)
1182{
1183 kfree(container_of(vdesc, struct edma_desc, vdesc));
1184}
1185
1186/* Dispatch a queued descriptor to the controller (caller holds lock) */
1187static void edma_execute(struct edma_chan *echan)
1188{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001189 struct edma_cc *ecc = echan->ecc;
Joel Fernandes53407062013-09-03 10:02:46 -05001190 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001191 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -05001192 struct device *dev = echan->vchan.chan.device->dev;
1193 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001194
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001195 if (!echan->edesc) {
1196 /* Setup is needed for the first transfer */
Joel Fernandes53407062013-09-03 10:02:46 -05001197 vdesc = vchan_next_desc(&echan->vchan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001198 if (!vdesc)
Joel Fernandes53407062013-09-03 10:02:46 -05001199 return;
Joel Fernandes53407062013-09-03 10:02:46 -05001200 list_del(&vdesc->node);
1201 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001202 }
1203
Joel Fernandes53407062013-09-03 10:02:46 -05001204 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001205
Joel Fernandes53407062013-09-03 10:02:46 -05001206 /* Find out how many left */
1207 left = edesc->pset_nr - edesc->processed;
1208 nslots = min(MAX_NR_SG, left);
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001209 edesc->sg_len = 0;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001210
1211 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -05001212 for (i = 0; i < nslots; i++) {
1213 j = i + edesc->processed;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001214 edma_write_slot(ecc, echan->slot[i], &edesc->pset[j].param);
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001215 edesc->sg_len += edesc->pset[j].len;
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001216 dev_vdbg(echan->vchan.chan.device->dev,
Matt Porterc2dde5f2012-08-22 21:09:34 -04001217 "\n pset[%d]:\n"
1218 " chnum\t%d\n"
1219 " slot\t%d\n"
1220 " opt\t%08x\n"
1221 " src\t%08x\n"
1222 " dst\t%08x\n"
1223 " abcnt\t%08x\n"
1224 " ccnt\t%08x\n"
1225 " bidx\t%08x\n"
1226 " cidx\t%08x\n"
1227 " lkrld\t%08x\n",
Joel Fernandes53407062013-09-03 10:02:46 -05001228 j, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001229 edesc->pset[j].param.opt,
1230 edesc->pset[j].param.src,
1231 edesc->pset[j].param.dst,
1232 edesc->pset[j].param.a_b_cnt,
1233 edesc->pset[j].param.ccnt,
1234 edesc->pset[j].param.src_dst_bidx,
1235 edesc->pset[j].param.src_dst_cidx,
1236 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001237 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -05001238 if (i != (nslots - 1))
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001239 edma_link(ecc, echan->slot[i], echan->slot[i + 1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001240 }
1241
Joel Fernandes53407062013-09-03 10:02:46 -05001242 edesc->processed += nslots;
1243
Joel Fernandesb267b3b2013-08-29 18:05:44 -05001244 /*
1245 * If this is either the last set in a set of SG-list transactions
1246 * then setup a link to the dummy slot, this results in all future
1247 * events being absorbed and that's OK because we're done
1248 */
Joel Fernandes50a9c702013-10-31 16:31:23 -05001249 if (edesc->processed == edesc->pset_nr) {
1250 if (edesc->cyclic)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001251 edma_link(ecc, echan->slot[nslots - 1], echan->slot[1]);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001252 else
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001253 edma_link(ecc, echan->slot[nslots - 1],
Joel Fernandes50a9c702013-10-31 16:31:23 -05001254 echan->ecc->dummy_slot);
1255 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -05001256
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001257 if (echan->missed) {
1258 /*
1259 * This happens due to setup times between intermediate
1260 * transfers in long SG lists which have to be broken up into
1261 * transfers of MAX_NR_SG
1262 */
1263 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001264 edma_clean_channel(ecc, echan->ch_num);
1265 edma_stop(ecc, echan->ch_num);
1266 edma_start(ecc, echan->ch_num);
1267 edma_trigger_channel(ecc, echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001268 echan->missed = 0;
1269 } else if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +03001270 dev_dbg(dev, "first transfer starting on channel %d\n",
1271 echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001272 edma_start(ecc, echan->ch_num);
Sekhar Nori5fc68a62014-03-19 11:25:50 +05301273 } else {
1274 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
1275 echan->ch_num, edesc->processed);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001276 edma_resume(ecc, echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -05001277 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001278}
1279
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001280static int edma_terminate_all(struct dma_chan *chan)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001281{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001282 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001283 unsigned long flags;
1284 LIST_HEAD(head);
1285
1286 spin_lock_irqsave(&echan->vchan.lock, flags);
1287
1288 /*
1289 * Stop DMA activity: we assume the callback will not be called
1290 * after edma_dma() returns (even if it does, it will see
1291 * echan->edesc is NULL and exit.)
1292 */
1293 if (echan->edesc) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001294 edma_stop(echan->ecc, echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001295 /* Move the cyclic channel back to default queue */
1296 if (echan->edesc->cyclic)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001297 edma_assign_channel_eventq(echan->ecc, echan->ch_num,
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001298 EVENTQ_DEFAULT);
Petr Kulhavy5ca9e7c2015-03-27 13:35:51 +02001299 /*
1300 * free the running request descriptor
1301 * since it is not in any of the vdesc lists
1302 */
1303 edma_desc_free(&echan->edesc->vdesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001304 echan->edesc = NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001305 }
1306
1307 vchan_get_all_descriptors(&echan->vchan, &head);
1308 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1309 vchan_dma_desc_free_list(&echan->vchan, &head);
1310
1311 return 0;
1312}
1313
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001314static int edma_slave_config(struct dma_chan *chan,
Matt Porter661f7cb2013-01-10 13:41:04 -05001315 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001316{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001317 struct edma_chan *echan = to_edma_chan(chan);
1318
Matt Porter661f7cb2013-01-10 13:41:04 -05001319 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
1320 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001321 return -EINVAL;
1322
Matt Porter661f7cb2013-01-10 13:41:04 -05001323 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -04001324
1325 return 0;
1326}
1327
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001328static int edma_dma_pause(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001329{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001330 struct edma_chan *echan = to_edma_chan(chan);
1331
John Ogness02ec6042015-04-27 13:52:25 +02001332 if (!echan->edesc)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001333 return -EINVAL;
1334
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001335 edma_pause(echan->ecc, echan->ch_num);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001336 return 0;
1337}
1338
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001339static int edma_dma_resume(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001340{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001341 struct edma_chan *echan = to_edma_chan(chan);
1342
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001343 edma_resume(echan->ecc, echan->ch_num);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001344 return 0;
1345}
1346
Joel Fernandesfd009032013-09-23 18:05:13 -05001347/*
1348 * A PaRAM set configuration abstraction used by other modes
1349 * @chan: Channel who's PaRAM set we're configuring
1350 * @pset: PaRAM set to initialize and setup.
1351 * @src_addr: Source address of the DMA
1352 * @dst_addr: Destination address of the DMA
1353 * @burst: In units of dev_width, how much to send
1354 * @dev_width: How much is the dev_width
1355 * @dma_length: Total length of the DMA transfer
1356 * @direction: Direction of the transfer
1357 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001358static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001359 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
1360 enum dma_slave_buswidth dev_width,
1361 unsigned int dma_length,
1362 enum dma_transfer_direction direction)
Joel Fernandesfd009032013-09-23 18:05:13 -05001363{
1364 struct edma_chan *echan = to_edma_chan(chan);
1365 struct device *dev = chan->device->dev;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001366 struct edmacc_param *param = &epset->param;
Joel Fernandesfd009032013-09-23 18:05:13 -05001367 int acnt, bcnt, ccnt, cidx;
1368 int src_bidx, dst_bidx, src_cidx, dst_cidx;
1369 int absync;
1370
1371 acnt = dev_width;
Peter Ujfalusib2b617d2014-04-14 14:41:58 +03001372
1373 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
1374 if (!burst)
1375 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -05001376 /*
1377 * If the maxburst is equal to the fifo width, use
1378 * A-synced transfers. This allows for large contiguous
1379 * buffer transfers using only one PaRAM set.
1380 */
1381 if (burst == 1) {
1382 /*
1383 * For the A-sync case, bcnt and ccnt are the remainder
1384 * and quotient respectively of the division of:
1385 * (dma_length / acnt) by (SZ_64K -1). This is so
1386 * that in case bcnt over flows, we have ccnt to use.
1387 * Note: In A-sync tranfer only, bcntrld is used, but it
1388 * only applies for sg_dma_len(sg) >= SZ_64K.
1389 * In this case, the best way adopted is- bccnt for the
1390 * first frame will be the remainder below. Then for
1391 * every successive frame, bcnt will be SZ_64K-1. This
1392 * is assured as bcntrld = 0xffff in end of function.
1393 */
1394 absync = false;
1395 ccnt = dma_length / acnt / (SZ_64K - 1);
1396 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
1397 /*
1398 * If bcnt is non-zero, we have a remainder and hence an
1399 * extra frame to transfer, so increment ccnt.
1400 */
1401 if (bcnt)
1402 ccnt++;
1403 else
1404 bcnt = SZ_64K - 1;
1405 cidx = acnt;
1406 } else {
1407 /*
1408 * If maxburst is greater than the fifo address_width,
1409 * use AB-synced transfers where A count is the fifo
1410 * address_width and B count is the maxburst. In this
1411 * case, we are limited to transfers of C count frames
1412 * of (address_width * maxburst) where C count is limited
1413 * to SZ_64K-1. This places an upper bound on the length
1414 * of an SG segment that can be handled.
1415 */
1416 absync = true;
1417 bcnt = burst;
1418 ccnt = dma_length / (acnt * bcnt);
1419 if (ccnt > (SZ_64K - 1)) {
1420 dev_err(dev, "Exceeded max SG segment size\n");
1421 return -EINVAL;
1422 }
1423 cidx = acnt * bcnt;
1424 }
1425
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001426 epset->len = dma_length;
1427
Joel Fernandesfd009032013-09-23 18:05:13 -05001428 if (direction == DMA_MEM_TO_DEV) {
1429 src_bidx = acnt;
1430 src_cidx = cidx;
1431 dst_bidx = 0;
1432 dst_cidx = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001433 epset->addr = src_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001434 } else if (direction == DMA_DEV_TO_MEM) {
1435 src_bidx = 0;
1436 src_cidx = 0;
1437 dst_bidx = acnt;
1438 dst_cidx = cidx;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001439 epset->addr = dst_addr;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001440 } else if (direction == DMA_MEM_TO_MEM) {
1441 src_bidx = acnt;
1442 src_cidx = cidx;
1443 dst_bidx = acnt;
1444 dst_cidx = cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001445 } else {
1446 dev_err(dev, "%s: direction not implemented yet\n", __func__);
1447 return -EINVAL;
1448 }
1449
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001450 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
Joel Fernandesfd009032013-09-23 18:05:13 -05001451 /* Configure A or AB synchronized transfers */
1452 if (absync)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001453 param->opt |= SYNCDIM;
Joel Fernandesfd009032013-09-23 18:05:13 -05001454
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001455 param->src = src_addr;
1456 param->dst = dst_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001457
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001458 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
1459 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001460
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001461 param->a_b_cnt = bcnt << 16 | acnt;
1462 param->ccnt = ccnt;
Joel Fernandesfd009032013-09-23 18:05:13 -05001463 /*
1464 * Only time when (bcntrld) auto reload is required is for
1465 * A-sync case, and in this case, a requirement of reload value
1466 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
1467 * and then later will be populated by edma_execute.
1468 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001469 param->link_bcntrld = 0xffffffff;
Joel Fernandesfd009032013-09-23 18:05:13 -05001470 return absync;
1471}
1472
Matt Porterc2dde5f2012-08-22 21:09:34 -04001473static struct dma_async_tx_descriptor *edma_prep_slave_sg(
1474 struct dma_chan *chan, struct scatterlist *sgl,
1475 unsigned int sg_len, enum dma_transfer_direction direction,
1476 unsigned long tx_flags, void *context)
1477{
1478 struct edma_chan *echan = to_edma_chan(chan);
1479 struct device *dev = chan->device->dev;
1480 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -05001481 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -05001482 enum dma_slave_buswidth dev_width;
1483 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001484 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -05001485 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001486
1487 if (unlikely(!echan || !sgl || !sg_len))
1488 return NULL;
1489
Matt Porter661f7cb2013-01-10 13:41:04 -05001490 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001491 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001492 dev_width = echan->cfg.src_addr_width;
1493 burst = echan->cfg.src_maxburst;
1494 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001495 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001496 dev_width = echan->cfg.dst_addr_width;
1497 burst = echan->cfg.dst_maxburst;
1498 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001499 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Matt Porter661f7cb2013-01-10 13:41:04 -05001500 return NULL;
1501 }
1502
1503 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001504 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001505 return NULL;
1506 }
1507
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001508 edesc = kzalloc(sizeof(*edesc) + sg_len * sizeof(edesc->pset[0]),
1509 GFP_ATOMIC);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001510 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001511 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001512 return NULL;
1513 }
1514
1515 edesc->pset_nr = sg_len;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001516 edesc->residue = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001517 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001518 edesc->echan = echan;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001519
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001520 /* Allocate a PaRAM slot, if needed */
1521 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
1522
1523 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001524 if (echan->slot[i] < 0) {
1525 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001526 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001527 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +03001528 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001529 dev_err(dev, "%s: Failed to allocate slot\n",
1530 __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001531 return NULL;
1532 }
1533 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001534 }
1535
1536 /* Configure PaRAM sets for each SG */
1537 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001538 /* Get address for each SG */
1539 if (direction == DMA_DEV_TO_MEM)
1540 dst_addr = sg_dma_address(sg);
1541 else
1542 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001543
Joel Fernandesfd009032013-09-23 18:05:13 -05001544 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1545 dst_addr, burst, dev_width,
1546 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +05301547 if (ret < 0) {
1548 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -05001549 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001550 }
1551
Joel Fernandesfd009032013-09-23 18:05:13 -05001552 edesc->absync = ret;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001553 edesc->residue += sg_dma_len(sg);
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001554
1555 /* If this is the last in a current SG set of transactions,
1556 enable interrupts so that next set is processed */
1557 if (!((i+1) % MAX_NR_SG))
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001558 edesc->pset[i].param.opt |= TCINTEN;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001559
Matt Porterc2dde5f2012-08-22 21:09:34 -04001560 /* If this is the last set, enable completion interrupt flag */
1561 if (i == sg_len - 1)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001562 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001563 }
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001564 edesc->residue_stat = edesc->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001565
Matt Porterc2dde5f2012-08-22 21:09:34 -04001566 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1567}
Matt Porterc2dde5f2012-08-22 21:09:34 -04001568
Lad, Prabhakarb7a4fd52015-02-04 13:03:27 +00001569static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001570 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1571 size_t len, unsigned long tx_flags)
1572{
1573 int ret;
1574 struct edma_desc *edesc;
1575 struct device *dev = chan->device->dev;
1576 struct edma_chan *echan = to_edma_chan(chan);
1577
1578 if (unlikely(!echan || !len))
1579 return NULL;
1580
1581 edesc = kzalloc(sizeof(*edesc) + sizeof(edesc->pset[0]), GFP_ATOMIC);
1582 if (!edesc) {
1583 dev_dbg(dev, "Failed to allocate a descriptor\n");
1584 return NULL;
1585 }
1586
1587 edesc->pset_nr = 1;
1588
1589 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
1590 DMA_SLAVE_BUSWIDTH_4_BYTES, len, DMA_MEM_TO_MEM);
1591 if (ret < 0)
1592 return NULL;
1593
1594 edesc->absync = ret;
1595
1596 /*
1597 * Enable intermediate transfer chaining to re-trigger channel
1598 * on completion of every TR, and enable transfer-completion
1599 * interrupt on completion of the whole transfer.
1600 */
Joel Fernandesb0cce4c2014-04-28 15:30:32 -05001601 edesc->pset[0].param.opt |= ITCCHEN;
1602 edesc->pset[0].param.opt |= TCINTEN;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001603
1604 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1605}
1606
Joel Fernandes50a9c702013-10-31 16:31:23 -05001607static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
1608 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1609 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +02001610 unsigned long tx_flags)
Joel Fernandes50a9c702013-10-31 16:31:23 -05001611{
1612 struct edma_chan *echan = to_edma_chan(chan);
1613 struct device *dev = chan->device->dev;
1614 struct edma_desc *edesc;
1615 dma_addr_t src_addr, dst_addr;
1616 enum dma_slave_buswidth dev_width;
1617 u32 burst;
1618 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001619
Joel Fernandes50a9c702013-10-31 16:31:23 -05001620 if (unlikely(!echan || !buf_len || !period_len))
1621 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001622
Joel Fernandes50a9c702013-10-31 16:31:23 -05001623 if (direction == DMA_DEV_TO_MEM) {
1624 src_addr = echan->cfg.src_addr;
1625 dst_addr = buf_addr;
1626 dev_width = echan->cfg.src_addr_width;
1627 burst = echan->cfg.src_maxburst;
1628 } else if (direction == DMA_MEM_TO_DEV) {
1629 src_addr = buf_addr;
1630 dst_addr = echan->cfg.dst_addr;
1631 dev_width = echan->cfg.dst_addr_width;
1632 burst = echan->cfg.dst_maxburst;
1633 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001634 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001635 return NULL;
1636 }
1637
1638 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001639 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001640 return NULL;
1641 }
1642
1643 if (unlikely(buf_len % period_len)) {
1644 dev_err(dev, "Period should be multiple of Buffer length\n");
1645 return NULL;
1646 }
1647
1648 nslots = (buf_len / period_len) + 1;
1649
1650 /*
1651 * Cyclic DMA users such as audio cannot tolerate delays introduced
1652 * by cases where the number of periods is more than the maximum
1653 * number of SGs the EDMA driver can handle at a time. For DMA types
1654 * such as Slave SGs, such delays are tolerable and synchronized,
1655 * but the synchronization is difficult to achieve with Cyclic and
1656 * cannot be guaranteed, so we error out early.
1657 */
1658 if (nslots > MAX_NR_SG)
1659 return NULL;
1660
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001661 edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]),
1662 GFP_ATOMIC);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001663 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001664 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001665 return NULL;
1666 }
1667
1668 edesc->cyclic = 1;
1669 edesc->pset_nr = nslots;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001670 edesc->residue = edesc->residue_stat = buf_len;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001671 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001672 edesc->echan = echan;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001673
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001674 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
1675 __func__, echan->ch_num, nslots, period_len, buf_len);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001676
1677 for (i = 0; i < nslots; i++) {
1678 /* Allocate a PaRAM slot, if needed */
1679 if (echan->slot[i] < 0) {
1680 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001681 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001682 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001683 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001684 dev_err(dev, "%s: Failed to allocate slot\n",
1685 __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001686 return NULL;
1687 }
1688 }
1689
1690 if (i == nslots - 1) {
1691 memcpy(&edesc->pset[i], &edesc->pset[0],
1692 sizeof(edesc->pset[0]));
1693 break;
1694 }
1695
1696 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1697 dst_addr, burst, dev_width, period_len,
1698 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001699 if (ret < 0) {
1700 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001701 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001702 }
Joel Fernandes50a9c702013-10-31 16:31:23 -05001703
1704 if (direction == DMA_DEV_TO_MEM)
1705 dst_addr += period_len;
1706 else
1707 src_addr += period_len;
1708
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001709 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
1710 dev_vdbg(dev,
Joel Fernandes50a9c702013-10-31 16:31:23 -05001711 "\n pset[%d]:\n"
1712 " chnum\t%d\n"
1713 " slot\t%d\n"
1714 " opt\t%08x\n"
1715 " src\t%08x\n"
1716 " dst\t%08x\n"
1717 " abcnt\t%08x\n"
1718 " ccnt\t%08x\n"
1719 " bidx\t%08x\n"
1720 " cidx\t%08x\n"
1721 " lkrld\t%08x\n",
1722 i, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001723 edesc->pset[i].param.opt,
1724 edesc->pset[i].param.src,
1725 edesc->pset[i].param.dst,
1726 edesc->pset[i].param.a_b_cnt,
1727 edesc->pset[i].param.ccnt,
1728 edesc->pset[i].param.src_dst_bidx,
1729 edesc->pset[i].param.src_dst_cidx,
1730 edesc->pset[i].param.link_bcntrld);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001731
1732 edesc->absync = ret;
1733
1734 /*
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001735 * Enable period interrupt only if it is requested
Joel Fernandes50a9c702013-10-31 16:31:23 -05001736 */
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001737 if (tx_flags & DMA_PREP_INTERRUPT)
1738 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001739 }
1740
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001741 /* Place the cyclic channel to highest priority queue */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001742 edma_assign_channel_eventq(echan->ecc, echan->ch_num, EVENTQ_0);
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001743
Matt Porterc2dde5f2012-08-22 21:09:34 -04001744 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1745}
1746
1747static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
1748{
1749 struct edma_chan *echan = data;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001750 struct edma_cc *ecc = echan->ecc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001751 struct device *dev = echan->vchan.chan.device->dev;
1752 struct edma_desc *edesc;
Joel Fernandesc5f47992013-08-29 18:05:43 -05001753 struct edmacc_param p;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001754
Joel Fernandes50a9c702013-10-31 16:31:23 -05001755 edesc = echan->edesc;
1756
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001757 spin_lock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001758 switch (ch_status) {
Vinod Kouldb60d8d2013-10-30 18:22:30 +05301759 case EDMA_DMA_COMPLETE:
Matt Porterc2dde5f2012-08-22 21:09:34 -04001760 if (edesc) {
Joel Fernandes50a9c702013-10-31 16:31:23 -05001761 if (edesc->cyclic) {
1762 vchan_cyclic_callback(&edesc->vdesc);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001763 goto out;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001764 } else if (edesc->processed == edesc->pset_nr) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001765 dev_dbg(dev,
1766 "Transfer completed on channel %d\n",
1767 ch_num);
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001768 edesc->residue = 0;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001769 edma_stop(ecc, echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -05001770 vchan_cookie_complete(&edesc->vdesc);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001771 echan->edesc = NULL;
Joel Fernandes53407062013-09-03 10:02:46 -05001772 } else {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001773 dev_dbg(dev,
1774 "Sub transfer completed on channel %d\n",
1775 ch_num);
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001776
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001777 edma_pause(ecc, echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001778
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001779 /* Update statistics for tx_status */
1780 edesc->residue -= edesc->sg_len;
1781 edesc->residue_stat = edesc->residue;
1782 edesc->processed_stat = edesc->processed;
Joel Fernandes53407062013-09-03 10:02:46 -05001783 }
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001784 edma_execute(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001785 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001786 break;
Vinod Kouldb60d8d2013-10-30 18:22:30 +05301787 case EDMA_DMA_CC_ERROR:
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001788 edma_read_slot(ecc, echan->slot[0], &p);
Joel Fernandesc5f47992013-08-29 18:05:43 -05001789
1790 /*
1791 * Issue later based on missed flag which will be sure
1792 * to happen as:
1793 * (1) we finished transmitting an intermediate slot and
1794 * edma_execute is coming up.
1795 * (2) or we finished current transfer and issue will
1796 * call edma_execute.
1797 *
1798 * Important note: issuing can be dangerous here and
1799 * lead to some nasty recursion when we are in a NULL
1800 * slot. So we avoid doing so and set the missed flag.
1801 */
1802 if (p.a_b_cnt == 0 && p.ccnt == 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001803 dev_dbg(dev, "Error on null slot, setting miss\n");
Joel Fernandesc5f47992013-08-29 18:05:43 -05001804 echan->missed = 1;
1805 } else {
1806 /*
1807 * The slot is already programmed but the event got
1808 * missed, so its safe to issue it here.
1809 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001810 dev_dbg(dev, "Missed event, TRIGGERING\n");
1811 edma_clean_channel(ecc, echan->ch_num);
1812 edma_stop(ecc, echan->ch_num);
1813 edma_start(ecc, echan->ch_num);
1814 edma_trigger_channel(ecc, echan->ch_num);
Joel Fernandesc5f47992013-08-29 18:05:43 -05001815 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001816 break;
1817 default:
1818 break;
1819 }
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001820out:
1821 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001822}
1823
1824/* Alloc channel resources */
1825static int edma_alloc_chan_resources(struct dma_chan *chan)
1826{
1827 struct edma_chan *echan = to_edma_chan(chan);
1828 struct device *dev = chan->device->dev;
1829 int ret;
1830 int a_ch_num;
1831 LIST_HEAD(descs);
1832
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001833 a_ch_num = edma_alloc_channel(echan->ecc, echan->ch_num,
Peter Ujfalusica304fa2015-10-14 14:42:49 +03001834 edma_callback, echan, EVENTQ_DEFAULT);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001835
1836 if (a_ch_num < 0) {
1837 ret = -ENODEV;
1838 goto err_no_chan;
1839 }
1840
1841 if (a_ch_num != echan->ch_num) {
1842 dev_err(dev, "failed to allocate requested channel %u:%u\n",
1843 EDMA_CTLR(echan->ch_num),
1844 EDMA_CHAN_SLOT(echan->ch_num));
1845 ret = -ENODEV;
1846 goto err_wrong_chan;
1847 }
1848
1849 echan->alloced = true;
1850 echan->slot[0] = echan->ch_num;
1851
Peter Ujfalusi9aac9092014-04-24 10:29:50 +03001852 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
Ezequiel Garcia0e772c62013-12-13 11:06:18 -03001853 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
Matt Porterc2dde5f2012-08-22 21:09:34 -04001854
1855 return 0;
1856
1857err_wrong_chan:
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001858 edma_free_channel(echan->ecc, a_ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001859err_no_chan:
1860 return ret;
1861}
1862
1863/* Free channel resources */
1864static void edma_free_chan_resources(struct dma_chan *chan)
1865{
1866 struct edma_chan *echan = to_edma_chan(chan);
1867 struct device *dev = chan->device->dev;
1868 int i;
1869
1870 /* Terminate transfers */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001871 edma_stop(echan->ecc, echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001872
1873 vchan_free_chan_resources(&echan->vchan);
1874
1875 /* Free EDMA PaRAM slots */
1876 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
1877 if (echan->slot[i] >= 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001878 edma_free_slot(echan->ecc, echan->slot[i]);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001879 echan->slot[i] = -1;
1880 }
1881 }
1882
1883 /* Free EDMA channel */
1884 if (echan->alloced) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001885 edma_free_channel(echan->ecc, echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001886 echan->alloced = false;
1887 }
1888
Ezequiel Garcia0e772c62013-12-13 11:06:18 -03001889 dev_dbg(dev, "freeing channel for %u\n", echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001890}
1891
1892/* Send pending descriptor to hardware */
1893static void edma_issue_pending(struct dma_chan *chan)
1894{
1895 struct edma_chan *echan = to_edma_chan(chan);
1896 unsigned long flags;
1897
1898 spin_lock_irqsave(&echan->vchan.lock, flags);
1899 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
1900 edma_execute(echan);
1901 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1902}
1903
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001904static u32 edma_residue(struct edma_desc *edesc)
1905{
1906 bool dst = edesc->direction == DMA_DEV_TO_MEM;
1907 struct edma_pset *pset = edesc->pset;
1908 dma_addr_t done, pos;
1909 int i;
1910
1911 /*
1912 * We always read the dst/src position from the first RamPar
1913 * pset. That's the one which is active now.
1914 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001915 pos = edma_get_position(edesc->echan->ecc, edesc->echan->slot[0], dst);
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001916
1917 /*
1918 * Cyclic is simple. Just subtract pset[0].addr from pos.
1919 *
1920 * We never update edesc->residue in the cyclic case, so we
1921 * can tell the remaining room to the end of the circular
1922 * buffer.
1923 */
1924 if (edesc->cyclic) {
1925 done = pos - pset->addr;
1926 edesc->residue_stat = edesc->residue - done;
1927 return edesc->residue_stat;
1928 }
1929
1930 /*
1931 * For SG operation we catch up with the last processed
1932 * status.
1933 */
1934 pset += edesc->processed_stat;
1935
1936 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
1937 /*
1938 * If we are inside this pset address range, we know
1939 * this is the active one. Get the current delta and
1940 * stop walking the psets.
1941 */
1942 if (pos >= pset->addr && pos < pset->addr + pset->len)
1943 return edesc->residue_stat - (pos - pset->addr);
1944
1945 /* Otherwise mark it done and update residue_stat. */
1946 edesc->processed_stat++;
1947 edesc->residue_stat -= pset->len;
1948 }
1949 return edesc->residue_stat;
1950}
1951
Matt Porterc2dde5f2012-08-22 21:09:34 -04001952/* Check request completion status */
1953static enum dma_status edma_tx_status(struct dma_chan *chan,
1954 dma_cookie_t cookie,
1955 struct dma_tx_state *txstate)
1956{
1957 struct edma_chan *echan = to_edma_chan(chan);
1958 struct virt_dma_desc *vdesc;
1959 enum dma_status ret;
1960 unsigned long flags;
1961
1962 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +05301963 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001964 return ret;
1965
1966 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001967 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001968 txstate->residue = edma_residue(echan->edesc);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001969 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
1970 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001971 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1972
1973 return ret;
1974}
1975
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001976static void __init edma_chan_init(struct edma_cc *ecc, struct dma_device *dma,
Matt Porterc2dde5f2012-08-22 21:09:34 -04001977 struct edma_chan *echans)
1978{
1979 int i, j;
1980
1981 for (i = 0; i < EDMA_CHANS; i++) {
1982 struct edma_chan *echan = &echans[i];
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001983 echan->ch_num = EDMA_CTLR_CHAN(ecc->id, i);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001984 echan->ecc = ecc;
1985 echan->vchan.desc_free = edma_desc_free;
1986
1987 vchan_init(&echan->vchan, dma);
1988
1989 INIT_LIST_HEAD(&echan->node);
1990 for (j = 0; j < EDMA_MAX_SLOTS; j++)
1991 echan->slot[j] = -1;
1992 }
1993}
1994
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001995#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1996 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
Peter Ujfalusie4a899d2014-07-03 07:51:56 +03001997 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001998 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1999
Matt Porterc2dde5f2012-08-22 21:09:34 -04002000static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
2001 struct device *dev)
2002{
2003 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -05002004 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05002005 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
Matt Porterc2dde5f2012-08-22 21:09:34 -04002006 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
2007 dma->device_free_chan_resources = edma_free_chan_resources;
2008 dma->device_issue_pending = edma_issue_pending;
2009 dma->device_tx_status = edma_tx_status;
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01002010 dma->device_config = edma_slave_config;
2011 dma->device_pause = edma_dma_pause;
2012 dma->device_resume = edma_dma_resume;
2013 dma->device_terminate_all = edma_terminate_all;
Maxime Ripard9f59cd02014-11-17 14:42:47 +01002014
2015 dma->src_addr_widths = EDMA_DMA_BUSWIDTHS;
2016 dma->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
2017 dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2018 dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
2019
Matt Porterc2dde5f2012-08-22 21:09:34 -04002020 dma->dev = dev;
2021
Joel Fernandes8cc3e302014-04-18 21:50:33 -05002022 /*
2023 * code using dma memcpy must make sure alignment of
2024 * length is at dma->copy_align boundary.
2025 */
Maxime Ripard77a68e52015-07-20 10:41:32 +02002026 dma->copy_align = DMAENGINE_ALIGN_4_BYTES;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05002027
Matt Porterc2dde5f2012-08-22 21:09:34 -04002028 INIT_LIST_HEAD(&dma->channels);
2029}
2030
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002031static int edma_setup_from_hw(struct device *dev, struct edma_soc_info *pdata,
2032 struct edma_cc *ecc)
2033{
2034 int i;
2035 u32 value, cccfg;
2036 s8 (*queue_priority_map)[2];
2037
2038 /* Decode the eDMA3 configuration from CCCFG register */
2039 cccfg = edma_read(ecc, EDMA_CCCFG);
2040
2041 value = GET_NUM_REGN(cccfg);
2042 ecc->num_region = BIT(value);
2043
2044 value = GET_NUM_DMACH(cccfg);
2045 ecc->num_channels = BIT(value + 1);
2046
2047 value = GET_NUM_PAENTRY(cccfg);
2048 ecc->num_slots = BIT(value + 4);
2049
2050 value = GET_NUM_EVQUE(cccfg);
2051 ecc->num_tc = value + 1;
2052
2053 dev_dbg(dev, "eDMA3 CC HW configuration (cccfg: 0x%08x):\n", cccfg);
2054 dev_dbg(dev, "num_region: %u\n", ecc->num_region);
2055 dev_dbg(dev, "num_channels: %u\n", ecc->num_channels);
2056 dev_dbg(dev, "num_slots: %u\n", ecc->num_slots);
2057 dev_dbg(dev, "num_tc: %u\n", ecc->num_tc);
2058
2059 /* Nothing need to be done if queue priority is provided */
2060 if (pdata->queue_priority_mapping)
2061 return 0;
2062
2063 /*
2064 * Configure TC/queue priority as follows:
2065 * Q0 - priority 0
2066 * Q1 - priority 1
2067 * Q2 - priority 2
2068 * ...
2069 * The meaning of priority numbers: 0 highest priority, 7 lowest
2070 * priority. So Q0 is the highest priority queue and the last queue has
2071 * the lowest priority.
2072 */
2073 queue_priority_map = devm_kzalloc(dev, (ecc->num_tc + 1) * sizeof(s8),
2074 GFP_KERNEL);
2075 if (!queue_priority_map)
2076 return -ENOMEM;
2077
2078 for (i = 0; i < ecc->num_tc; i++) {
2079 queue_priority_map[i][0] = i;
2080 queue_priority_map[i][1] = i;
2081 }
2082 queue_priority_map[i][0] = -1;
2083 queue_priority_map[i][1] = -1;
2084
2085 pdata->queue_priority_mapping = queue_priority_map;
2086 /* Default queue has the lowest priority */
2087 pdata->default_queue = i - 1;
2088
2089 return 0;
2090}
2091
2092#if IS_ENABLED(CONFIG_OF)
2093static int edma_xbar_event_map(struct device *dev, struct edma_soc_info *pdata,
2094 size_t sz)
2095{
2096 const char pname[] = "ti,edma-xbar-event-map";
2097 struct resource res;
2098 void __iomem *xbar;
2099 s16 (*xbar_chans)[2];
2100 size_t nelm = sz / sizeof(s16);
2101 u32 shift, offset, mux;
2102 int ret, i;
2103
2104 xbar_chans = devm_kzalloc(dev, (nelm + 2) * sizeof(s16), GFP_KERNEL);
2105 if (!xbar_chans)
2106 return -ENOMEM;
2107
2108 ret = of_address_to_resource(dev->of_node, 1, &res);
2109 if (ret)
2110 return -ENOMEM;
2111
2112 xbar = devm_ioremap(dev, res.start, resource_size(&res));
2113 if (!xbar)
2114 return -ENOMEM;
2115
2116 ret = of_property_read_u16_array(dev->of_node, pname, (u16 *)xbar_chans,
2117 nelm);
2118 if (ret)
2119 return -EIO;
2120
2121 /* Invalidate last entry for the other user of this mess */
2122 nelm >>= 1;
2123 xbar_chans[nelm][0] = -1;
2124 xbar_chans[nelm][1] = -1;
2125
2126 for (i = 0; i < nelm; i++) {
2127 shift = (xbar_chans[i][1] & 0x03) << 3;
2128 offset = xbar_chans[i][1] & 0xfffffffc;
2129 mux = readl(xbar + offset);
2130 mux &= ~(0xff << shift);
2131 mux |= xbar_chans[i][0] << shift;
2132 writel(mux, (xbar + offset));
2133 }
2134
2135 pdata->xbar_chans = (const s16 (*)[2]) xbar_chans;
2136 return 0;
2137}
2138
2139static int edma_of_parse_dt(struct device *dev, struct edma_soc_info *pdata)
2140{
2141 int ret = 0;
2142 struct property *prop;
2143 size_t sz;
2144 struct edma_rsv_info *rsv_info;
2145
2146 rsv_info = devm_kzalloc(dev, sizeof(struct edma_rsv_info), GFP_KERNEL);
2147 if (!rsv_info)
2148 return -ENOMEM;
2149 pdata->rsv = rsv_info;
2150
2151 prop = of_find_property(dev->of_node, "ti,edma-xbar-event-map", &sz);
2152 if (prop)
2153 ret = edma_xbar_event_map(dev, pdata, sz);
2154
2155 return ret;
2156}
2157
2158static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev)
2159{
2160 struct edma_soc_info *info;
2161 int ret;
2162
2163 info = devm_kzalloc(dev, sizeof(struct edma_soc_info), GFP_KERNEL);
2164 if (!info)
2165 return ERR_PTR(-ENOMEM);
2166
2167 ret = edma_of_parse_dt(dev, info);
2168 if (ret)
2169 return ERR_PTR(ret);
2170
2171 return info;
2172}
2173#else
2174static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev)
2175{
2176 return ERR_PTR(-EINVAL);
2177}
2178#endif
2179
Bill Pemberton463a1f82012-11-19 13:22:55 -05002180static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002181{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002182 struct edma_soc_info *info = pdev->dev.platform_data;
2183 s8 (*queue_priority_mapping)[2];
2184 int i, off, ln;
2185 const s16 (*rsv_chans)[2];
2186 const s16 (*rsv_slots)[2];
2187 const s16 (*xbar_chans)[2];
2188 int irq;
2189 char *irq_name;
2190 struct resource *mem;
2191 struct device_node *node = pdev->dev.of_node;
2192 struct device *dev = &pdev->dev;
2193 struct edma_cc *ecc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04002194 int ret;
2195
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002196 if (node) {
2197 info = edma_setup_info_from_dt(dev);
2198 if (IS_ERR(info)) {
2199 dev_err(dev, "failed to get DT data\n");
2200 return PTR_ERR(info);
2201 }
2202 }
2203
2204 if (!info)
2205 return -ENODEV;
2206
2207 pm_runtime_enable(dev);
2208 ret = pm_runtime_get_sync(dev);
2209 if (ret < 0) {
2210 dev_err(dev, "pm_runtime_get_sync() failed\n");
2211 return ret;
2212 }
2213
Russell King94cb0e72013-06-27 13:45:16 +01002214 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2215 if (ret)
2216 return ret;
2217
Matt Porterc2dde5f2012-08-22 21:09:34 -04002218 ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
2219 if (!ecc) {
2220 dev_err(&pdev->dev, "Can't allocate controller\n");
2221 return -ENOMEM;
2222 }
2223
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002224 ecc->dev = dev;
2225 ecc->id = pdev->id;
2226 /* When booting with DT the pdev->id is -1 */
2227 if (ecc->id < 0)
2228 ecc->id = 0;
Peter Ujfalusica304fa2015-10-14 14:42:49 +03002229
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002230 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma3_cc");
2231 if (!mem) {
2232 dev_dbg(dev, "mem resource not found, using index 0\n");
2233 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2234 if (!mem) {
2235 dev_err(dev, "no mem resource?\n");
2236 return -ENODEV;
2237 }
2238 }
2239 ecc->base = devm_ioremap_resource(dev, mem);
2240 if (IS_ERR(ecc->base))
2241 return PTR_ERR(ecc->base);
Peter Ujfalusib2c843a2015-10-14 14:42:50 +03002242
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002243 platform_set_drvdata(pdev, ecc);
2244
2245 /* Get eDMA3 configuration from IP */
2246 ret = edma_setup_from_hw(dev, info, ecc);
2247 if (ret)
2248 return ret;
2249
2250 ecc->default_queue = info->default_queue;
2251
2252 for (i = 0; i < ecc->num_slots; i++)
2253 edma_write_slot(ecc, i, &dummy_paramset);
2254
2255 /* Mark all channels as unused */
2256 memset(ecc->edma_unused, 0xff, sizeof(ecc->edma_unused));
2257
2258 if (info->rsv) {
2259 /* Clear the reserved channels in unused list */
2260 rsv_chans = info->rsv->rsv_chans;
2261 if (rsv_chans) {
2262 for (i = 0; rsv_chans[i][0] != -1; i++) {
2263 off = rsv_chans[i][0];
2264 ln = rsv_chans[i][1];
2265 clear_bits(off, ln, ecc->edma_unused);
2266 }
2267 }
2268
2269 /* Set the reserved slots in inuse list */
2270 rsv_slots = info->rsv->rsv_slots;
2271 if (rsv_slots) {
2272 for (i = 0; rsv_slots[i][0] != -1; i++) {
2273 off = rsv_slots[i][0];
2274 ln = rsv_slots[i][1];
2275 set_bits(off, ln, ecc->edma_inuse);
2276 }
2277 }
2278 }
2279
2280 /* Clear the xbar mapped channels in unused list */
2281 xbar_chans = info->xbar_chans;
2282 if (xbar_chans) {
2283 for (i = 0; xbar_chans[i][1] != -1; i++) {
2284 off = xbar_chans[i][1];
2285 clear_bits(off, 1, ecc->edma_unused);
2286 }
2287 }
2288
2289 irq = platform_get_irq_byname(pdev, "edma3_ccint");
2290 if (irq < 0 && node)
2291 irq = irq_of_parse_and_map(node, 0);
2292
2293 if (irq >= 0) {
2294 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccint",
2295 dev_name(dev));
2296 ret = devm_request_irq(dev, irq, dma_irq_handler, 0, irq_name,
2297 ecc);
2298 if (ret) {
2299 dev_err(dev, "CCINT (%d) failed --> %d\n", irq, ret);
2300 return ret;
2301 }
2302 }
2303
2304 irq = platform_get_irq_byname(pdev, "edma3_ccerrint");
2305 if (irq < 0 && node)
2306 irq = irq_of_parse_and_map(node, 2);
2307
2308 if (irq >= 0) {
2309 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccerrint",
2310 dev_name(dev));
2311 ret = devm_request_irq(dev, irq, dma_ccerr_handler, 0, irq_name,
2312 ecc);
2313 if (ret) {
2314 dev_err(dev, "CCERRINT (%d) failed --> %d\n", irq, ret);
2315 return ret;
2316 }
2317 }
2318
2319 for (i = 0; i < ecc->num_channels; i++)
2320 edma_map_dmach_to_queue(ecc, i, info->default_queue);
2321
2322 queue_priority_mapping = info->queue_priority_mapping;
2323
2324 /* Event queue priority mapping */
2325 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2326 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2327 queue_priority_mapping[i][1]);
2328
2329 /* Map the channel to param entry if channel mapping logic exist */
2330 if (edma_read(ecc, EDMA_CCCFG) & CHMAP_EXIST)
2331 edma_direct_dmach_to_param_mapping(ecc);
2332
2333 for (i = 0; i < ecc->num_region; i++) {
2334 edma_write_array2(ecc, EDMA_DRAE, i, 0, 0x0);
2335 edma_write_array2(ecc, EDMA_DRAE, i, 1, 0x0);
2336 edma_write_array(ecc, EDMA_QRAE, i, 0x0);
2337 }
2338 ecc->info = info;
2339
2340 ecc->dummy_slot = edma_alloc_slot(ecc, EDMA_SLOT_ANY);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002341 if (ecc->dummy_slot < 0) {
2342 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
Peter Ujfalusi04d537d2014-07-31 13:12:37 +03002343 return ecc->dummy_slot;
Matt Porterc2dde5f2012-08-22 21:09:34 -04002344 }
2345
2346 dma_cap_zero(ecc->dma_slave.cap_mask);
2347 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
Peter Ujfalusi232b223d2014-04-14 14:42:00 +03002348 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05002349 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002350
2351 edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
2352
2353 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
2354
2355 ret = dma_async_device_register(&ecc->dma_slave);
2356 if (ret)
2357 goto err_reg1;
2358
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002359 if (node)
2360 of_dma_controller_register(node, of_dma_xlate_by_chan_id,
Peter Ujfalusib2c843a2015-10-14 14:42:50 +03002361 &ecc->dma_slave);
Peter Ujfalusidc9b60552015-10-14 14:42:47 +03002362
Matt Porterc2dde5f2012-08-22 21:09:34 -04002363 dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
2364
2365 return 0;
2366
2367err_reg1:
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002368 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002369 return ret;
2370}
2371
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08002372static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002373{
2374 struct device *dev = &pdev->dev;
2375 struct edma_cc *ecc = dev_get_drvdata(dev);
2376
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002377 if (pdev->dev.of_node)
2378 of_dma_controller_free(pdev->dev.of_node);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002379 dma_async_device_unregister(&ecc->dma_slave);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002380 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002381
2382 return 0;
2383}
2384
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002385#ifdef CONFIG_PM_SLEEP
2386static int edma_pm_resume(struct device *dev)
2387{
2388 struct edma_cc *ecc = dev_get_drvdata(dev);
2389 int i;
2390 s8 (*queue_priority_mapping)[2];
2391
2392 queue_priority_mapping = ecc->info->queue_priority_mapping;
2393
2394 /* Event queue priority mapping */
2395 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2396 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2397 queue_priority_mapping[i][1]);
2398
2399 /* Map the channel to param entry if channel mapping logic */
2400 if (edma_read(ecc, EDMA_CCCFG) & CHMAP_EXIST)
2401 edma_direct_dmach_to_param_mapping(ecc);
2402
2403 for (i = 0; i < ecc->num_channels; i++) {
2404 if (test_bit(i, ecc->edma_inuse)) {
2405 /* ensure access through shadow region 0 */
2406 edma_or_array2(ecc, EDMA_DRAE, 0, i >> 5,
2407 BIT(i & 0x1f));
2408
2409 edma_setup_interrupt(ecc, EDMA_CTLR_CHAN(ecc->id, i),
2410 ecc->intr_data[i].callback,
2411 ecc->intr_data[i].data);
2412 }
2413 }
2414
2415 return 0;
2416}
2417#endif
2418
2419static const struct dev_pm_ops edma_pm_ops = {
2420 SET_LATE_SYSTEM_SLEEP_PM_OPS(NULL, edma_pm_resume)
2421};
2422
Matt Porterc2dde5f2012-08-22 21:09:34 -04002423static struct platform_driver edma_driver = {
2424 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05002425 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002426 .driver = {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002427 .name = "edma",
2428 .pm = &edma_pm_ops,
2429 .of_match_table = edma_of_ids,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002430 },
2431};
2432
2433bool edma_filter_fn(struct dma_chan *chan, void *param)
2434{
2435 if (chan->device->dev->driver == &edma_driver.driver) {
2436 struct edma_chan *echan = to_edma_chan(chan);
2437 unsigned ch_req = *(unsigned *)param;
2438 return ch_req == echan->ch_num;
2439 }
2440 return false;
2441}
2442EXPORT_SYMBOL(edma_filter_fn);
2443
Matt Porterc2dde5f2012-08-22 21:09:34 -04002444static int edma_init(void)
2445{
Arnd Bergmann5305e4d2014-10-24 18:14:01 +02002446 return platform_driver_register(&edma_driver);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002447}
2448subsys_initcall(edma_init);
2449
2450static void __exit edma_exit(void)
2451{
Matt Porterc2dde5f2012-08-22 21:09:34 -04002452 platform_driver_unregister(&edma_driver);
2453}
2454module_exit(edma_exit);
2455
Josh Boyerd71505b2013-09-04 10:32:50 -04002456MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002457MODULE_DESCRIPTION("TI EDMA DMA engine driver");
2458MODULE_LICENSE("GPL v2");