blob: 6bcbdceb3dc274316706eb465d0d09ef0da5ef36 [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
157/*ch_status parameter of callback function possible values*/
158#define EDMA_DMA_COMPLETE 1
159#define EDMA_DMA_CC_ERROR 2
160#define EDMA_DMA_TC1_ERROR 3
161#define EDMA_DMA_TC2_ERROR 4
162
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500163struct edma_pset {
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500164 u32 len;
165 dma_addr_t addr;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500166 struct edmacc_param param;
167};
168
Matt Porterc2dde5f2012-08-22 21:09:34 -0400169struct edma_desc {
170 struct virt_dma_desc vdesc;
171 struct list_head node;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500172 enum dma_transfer_direction direction;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500173 int cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400174 int absync;
175 int pset_nr;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500176 struct edma_chan *echan;
Joel Fernandes04361d82014-04-28 15:19:31 -0500177 int processed;
178
179 /*
180 * The following 4 elements are used for residue accounting.
181 *
182 * - processed_stat: the number of SG elements we have traversed
183 * so far to cover accounting. This is updated directly to processed
184 * during edma_callback and is always <= processed, because processed
185 * refers to the number of pending transfer (programmed to EDMA
186 * controller), where as processed_stat tracks number of transfers
187 * accounted for so far.
188 *
189 * - residue: The amount of bytes we have left to transfer for this desc
190 *
191 * - residue_stat: The residue in bytes of data we have covered
192 * so far for accounting. This is updated directly to residue
193 * during callbacks to keep it current.
194 *
195 * - sg_len: Tracks the length of the current intermediate transfer,
196 * this is required to update the residue during intermediate transfer
197 * completion callback.
198 */
199 int processed_stat;
200 u32 sg_len;
201 u32 residue;
202 u32 residue_stat;
203
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500204 struct edma_pset pset[0];
Matt Porterc2dde5f2012-08-22 21:09:34 -0400205};
206
207struct edma_cc;
208
209struct edma_chan {
210 struct virt_dma_chan vchan;
211 struct list_head node;
212 struct edma_desc *edesc;
213 struct edma_cc *ecc;
214 int ch_num;
215 bool alloced;
216 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -0500217 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -0500218 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400219};
220
221struct edma_cc {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300222 struct device *dev;
223 struct edma_soc_info *info;
224 void __iomem *base;
225 int id;
226
227 /* eDMA3 resource information */
228 unsigned num_channels;
229 unsigned num_region;
230 unsigned num_slots;
231 unsigned num_tc;
232 enum dma_event_q default_queue;
233
234 bool unused_chan_list_done;
235 /* The edma_inuse bit for each PaRAM slot is clear unless the
236 * channel is in use ... by ARM or DSP, for QDMA, or whatever.
237 */
Peter Ujfalusicb782052015-10-14 14:42:54 +0300238 unsigned long *edma_inuse;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300239
240 /* The edma_unused bit for each channel is clear unless
241 * it is not being used on this platform. It uses a bit
242 * of SOC-specific initialization code.
243 */
Peter Ujfalusicb782052015-10-14 14:42:54 +0300244 unsigned long *edma_unused;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300245
246 struct dma_interrupt_data {
247 void (*callback)(unsigned channel, unsigned short ch_status,
248 void *data);
249 void *data;
Peter Ujfalusicb782052015-10-14 14:42:54 +0300250 } *intr_data;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300251
Matt Porterc2dde5f2012-08-22 21:09:34 -0400252 struct dma_device dma_slave;
Peter Ujfalusicb782052015-10-14 14:42:54 +0300253 struct edma_chan *slave_chans;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400254 int dummy_slot;
255};
256
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300257/* dummy param set used to (re)initialize parameter RAM slots */
258static const struct edmacc_param dummy_paramset = {
259 .link_bcntrld = 0xffff,
260 .ccnt = 1,
261};
262
263static const struct of_device_id edma_of_ids[] = {
264 { .compatible = "ti,edma3", },
265 {}
266};
267
268static inline unsigned int edma_read(struct edma_cc *ecc, int offset)
269{
270 return (unsigned int)__raw_readl(ecc->base + offset);
271}
272
273static inline void edma_write(struct edma_cc *ecc, int offset, int val)
274{
275 __raw_writel(val, ecc->base + offset);
276}
277
278static inline void edma_modify(struct edma_cc *ecc, int offset, unsigned and,
279 unsigned or)
280{
281 unsigned val = edma_read(ecc, offset);
282
283 val &= and;
284 val |= or;
285 edma_write(ecc, offset, val);
286}
287
288static inline void edma_and(struct edma_cc *ecc, int offset, unsigned and)
289{
290 unsigned val = edma_read(ecc, offset);
291
292 val &= and;
293 edma_write(ecc, offset, val);
294}
295
296static inline void edma_or(struct edma_cc *ecc, int offset, unsigned or)
297{
298 unsigned val = edma_read(ecc, offset);
299
300 val |= or;
301 edma_write(ecc, offset, val);
302}
303
304static inline unsigned int edma_read_array(struct edma_cc *ecc, int offset,
305 int i)
306{
307 return edma_read(ecc, offset + (i << 2));
308}
309
310static inline void edma_write_array(struct edma_cc *ecc, int offset, int i,
311 unsigned val)
312{
313 edma_write(ecc, offset + (i << 2), val);
314}
315
316static inline void edma_modify_array(struct edma_cc *ecc, int offset, int i,
317 unsigned and, unsigned or)
318{
319 edma_modify(ecc, offset + (i << 2), and, or);
320}
321
322static inline void edma_or_array(struct edma_cc *ecc, int offset, int i,
323 unsigned or)
324{
325 edma_or(ecc, offset + (i << 2), or);
326}
327
328static inline void edma_or_array2(struct edma_cc *ecc, int offset, int i, int j,
329 unsigned or)
330{
331 edma_or(ecc, offset + ((i * 2 + j) << 2), or);
332}
333
334static inline void edma_write_array2(struct edma_cc *ecc, int offset, int i,
335 int j, unsigned val)
336{
337 edma_write(ecc, offset + ((i * 2 + j) << 2), val);
338}
339
340static inline unsigned int edma_shadow0_read(struct edma_cc *ecc, int offset)
341{
342 return edma_read(ecc, EDMA_SHADOW0 + offset);
343}
344
345static inline unsigned int edma_shadow0_read_array(struct edma_cc *ecc,
346 int offset, int i)
347{
348 return edma_read(ecc, EDMA_SHADOW0 + offset + (i << 2));
349}
350
351static inline void edma_shadow0_write(struct edma_cc *ecc, int offset,
352 unsigned val)
353{
354 edma_write(ecc, EDMA_SHADOW0 + offset, val);
355}
356
357static inline void edma_shadow0_write_array(struct edma_cc *ecc, int offset,
358 int i, unsigned val)
359{
360 edma_write(ecc, EDMA_SHADOW0 + offset + (i << 2), val);
361}
362
363static inline unsigned int edma_parm_read(struct edma_cc *ecc, int offset,
364 int param_no)
365{
366 return edma_read(ecc, EDMA_PARM + offset + (param_no << 5));
367}
368
369static inline void edma_parm_write(struct edma_cc *ecc, int offset,
370 int param_no, unsigned val)
371{
372 edma_write(ecc, EDMA_PARM + offset + (param_no << 5), val);
373}
374
375static inline void edma_parm_modify(struct edma_cc *ecc, int offset,
376 int param_no, unsigned and, unsigned or)
377{
378 edma_modify(ecc, EDMA_PARM + offset + (param_no << 5), and, or);
379}
380
381static inline void edma_parm_and(struct edma_cc *ecc, int offset, int param_no,
382 unsigned and)
383{
384 edma_and(ecc, EDMA_PARM + offset + (param_no << 5), and);
385}
386
387static inline void edma_parm_or(struct edma_cc *ecc, int offset, int param_no,
388 unsigned or)
389{
390 edma_or(ecc, EDMA_PARM + offset + (param_no << 5), or);
391}
392
393static inline void set_bits(int offset, int len, unsigned long *p)
394{
395 for (; len > 0; len--)
396 set_bit(offset + (len - 1), p);
397}
398
399static inline void clear_bits(int offset, int len, unsigned long *p)
400{
401 for (; len > 0; len--)
402 clear_bit(offset + (len - 1), p);
403}
404
405static void edma_map_dmach_to_queue(struct edma_cc *ecc, unsigned ch_no,
406 enum dma_event_q queue_no)
407{
408 int bit = (ch_no & 0x7) * 4;
409
410 /* default to low priority queue */
411 if (queue_no == EVENTQ_DEFAULT)
412 queue_no = ecc->default_queue;
413
414 queue_no &= 7;
415 edma_modify_array(ecc, EDMA_DMAQNUM, (ch_no >> 3), ~(0x7 << bit),
416 queue_no << bit);
417}
418
419static void edma_assign_priority_to_queue(struct edma_cc *ecc, int queue_no,
420 int priority)
421{
422 int bit = queue_no * 4;
423
424 edma_modify(ecc, EDMA_QUEPRI, ~(0x7 << bit), ((priority & 0x7) << bit));
425}
426
427static void edma_direct_dmach_to_param_mapping(struct edma_cc *ecc)
428{
429 int i;
430
431 for (i = 0; i < ecc->num_channels; i++)
432 edma_write_array(ecc, EDMA_DCHMAP, i, (i << 5));
433}
434
435static int prepare_unused_channel_list(struct device *dev, void *data)
436{
437 struct platform_device *pdev = to_platform_device(dev);
438 struct edma_cc *ecc = data;
Peter Ujfalusicb782052015-10-14 14:42:54 +0300439 int dma_req_min = EDMA_CTLR_CHAN(ecc->id, 0);
440 int dma_req_max = dma_req_min + ecc->num_channels;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300441 int i, count;
442 struct of_phandle_args dma_spec;
443
444 if (dev->of_node) {
445 struct platform_device *dma_pdev;
446
447 count = of_property_count_strings(dev->of_node, "dma-names");
448 if (count < 0)
449 return 0;
450 for (i = 0; i < count; i++) {
451 if (of_parse_phandle_with_args(dev->of_node, "dmas",
452 "#dma-cells", i,
453 &dma_spec))
454 continue;
455
456 if (!of_match_node(edma_of_ids, dma_spec.np)) {
457 of_node_put(dma_spec.np);
458 continue;
459 }
460
461 dma_pdev = of_find_device_by_node(dma_spec.np);
462 if (&dma_pdev->dev != ecc->dev)
463 continue;
464
465 clear_bit(EDMA_CHAN_SLOT(dma_spec.args[0]),
466 ecc->edma_unused);
467 of_node_put(dma_spec.np);
468 }
469 return 0;
470 }
471
472 /* For non-OF case */
473 for (i = 0; i < pdev->num_resources; i++) {
474 struct resource *res = &pdev->resource[i];
Peter Ujfalusicb782052015-10-14 14:42:54 +0300475 int dma_req;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300476
Peter Ujfalusicb782052015-10-14 14:42:54 +0300477 if (!(res->flags & IORESOURCE_DMA))
478 continue;
479
480 dma_req = (int)res->start;
481 if (dma_req >= dma_req_min && dma_req < dma_req_max)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300482 clear_bit(EDMA_CHAN_SLOT(pdev->resource[i].start),
483 ecc->edma_unused);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300484 }
485
486 return 0;
487}
488
489static void edma_setup_interrupt(struct edma_cc *ecc, unsigned lch,
490 void (*callback)(unsigned channel, u16 ch_status, void *data),
491 void *data)
492{
493 lch = EDMA_CHAN_SLOT(lch);
494
495 if (!callback)
496 edma_shadow0_write_array(ecc, SH_IECR, lch >> 5,
497 BIT(lch & 0x1f));
498
499 ecc->intr_data[lch].callback = callback;
500 ecc->intr_data[lch].data = data;
501
502 if (callback) {
503 edma_shadow0_write_array(ecc, SH_ICR, lch >> 5,
504 BIT(lch & 0x1f));
505 edma_shadow0_write_array(ecc, SH_IESR, lch >> 5,
506 BIT(lch & 0x1f));
507 }
508}
509
510/*
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300511 * paRAM slot management functions
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300512 */
513static void edma_write_slot(struct edma_cc *ecc, unsigned slot,
514 const struct edmacc_param *param)
515{
516 slot = EDMA_CHAN_SLOT(slot);
517 if (slot >= ecc->num_slots)
518 return;
519 memcpy_toio(ecc->base + PARM_OFFSET(slot), param, PARM_SIZE);
520}
521
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300522static void edma_read_slot(struct edma_cc *ecc, unsigned slot,
523 struct edmacc_param *param)
524{
525 slot = EDMA_CHAN_SLOT(slot);
526 if (slot >= ecc->num_slots)
527 return;
528 memcpy_fromio(param, ecc->base + PARM_OFFSET(slot), PARM_SIZE);
529}
530
531/**
532 * edma_alloc_slot - allocate DMA parameter RAM
533 * @ecc: pointer to edma_cc struct
534 * @slot: specific slot to allocate; negative for "any unused slot"
535 *
536 * This allocates a parameter RAM slot, initializing it to hold a
537 * dummy transfer. Slots allocated using this routine have not been
538 * mapped to a hardware DMA channel, and will normally be used by
539 * linking to them from a slot associated with a DMA channel.
540 *
541 * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
542 * slots may be allocated on behalf of DSP firmware.
543 *
544 * Returns the number of the slot, else negative errno.
545 */
546static int edma_alloc_slot(struct edma_cc *ecc, int slot)
547{
548 if (slot > 0)
549 slot = EDMA_CHAN_SLOT(slot);
550 if (slot < 0) {
551 slot = ecc->num_channels;
552 for (;;) {
553 slot = find_next_zero_bit(ecc->edma_inuse,
554 ecc->num_slots,
555 slot);
556 if (slot == ecc->num_slots)
557 return -ENOMEM;
558 if (!test_and_set_bit(slot, ecc->edma_inuse))
559 break;
560 }
561 } else if (slot < ecc->num_channels || slot >= ecc->num_slots) {
562 return -EINVAL;
563 } else if (test_and_set_bit(slot, ecc->edma_inuse)) {
564 return -EBUSY;
565 }
566
567 edma_write_slot(ecc, slot, &dummy_paramset);
568
569 return EDMA_CTLR_CHAN(ecc->id, slot);
570}
571
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300572static void edma_free_slot(struct edma_cc *ecc, unsigned slot)
573{
574 slot = EDMA_CHAN_SLOT(slot);
575 if (slot < ecc->num_channels || slot >= ecc->num_slots)
576 return;
577
578 edma_write_slot(ecc, slot, &dummy_paramset);
579 clear_bit(slot, ecc->edma_inuse);
580}
581
582/**
583 * edma_link - link one parameter RAM slot to another
584 * @ecc: pointer to edma_cc struct
585 * @from: parameter RAM slot originating the link
586 * @to: parameter RAM slot which is the link target
587 *
588 * The originating slot should not be part of any active DMA transfer.
589 */
590static void edma_link(struct edma_cc *ecc, unsigned from, unsigned to)
591{
Peter Ujfalusifc014092015-10-14 14:42:59 +0300592 if (unlikely(EDMA_CTLR(from) != EDMA_CTLR(to)))
593 dev_warn(ecc->dev, "Ignoring eDMA instance for linking\n");
594
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300595 from = EDMA_CHAN_SLOT(from);
596 to = EDMA_CHAN_SLOT(to);
597 if (from >= ecc->num_slots || to >= ecc->num_slots)
598 return;
599
600 edma_parm_modify(ecc, PARM_LINK_BCNTRLD, from, 0xffff0000,
601 PARM_OFFSET(to));
602}
603
604/**
605 * edma_get_position - returns the current transfer point
606 * @ecc: pointer to edma_cc struct
607 * @slot: parameter RAM slot being examined
608 * @dst: true selects the dest position, false the source
609 *
610 * Returns the position of the current active slot
611 */
612static dma_addr_t edma_get_position(struct edma_cc *ecc, unsigned slot,
613 bool dst)
614{
615 u32 offs;
616
617 slot = EDMA_CHAN_SLOT(slot);
618 offs = PARM_OFFSET(slot);
619 offs += dst ? PARM_DST : PARM_SRC;
620
621 return edma_read(ecc, offs);
622}
623
624/*-----------------------------------------------------------------------*/
625/**
626 * edma_start - start dma on a channel
627 * @ecc: pointer to edma_cc struct
628 * @channel: channel being activated
629 *
630 * Channels with event associations will be triggered by their hardware
631 * events, and channels without such associations will be triggered by
632 * software. (At this writing there is no interface for using software
633 * triggers except with channels that don't support hardware triggers.)
634 *
635 * Returns zero on success, else negative errno.
636 */
637static int edma_start(struct edma_cc *ecc, unsigned channel)
638{
639 if (ecc->id != EDMA_CTLR(channel)) {
640 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
641 ecc->id, EDMA_CTLR(channel));
642 return -EINVAL;
643 }
644 channel = EDMA_CHAN_SLOT(channel);
645
646 if (channel < ecc->num_channels) {
647 int j = channel >> 5;
648 unsigned int mask = BIT(channel & 0x1f);
649
650 /* EDMA channels without event association */
651 if (test_bit(channel, ecc->edma_unused)) {
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300652 dev_dbg(ecc->dev, "ESR%d %08x\n", j,
653 edma_shadow0_read_array(ecc, SH_ESR, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300654 edma_shadow0_write_array(ecc, SH_ESR, j, mask);
655 return 0;
656 }
657
658 /* EDMA channel with event association */
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300659 dev_dbg(ecc->dev, "ER%d %08x\n", j,
660 edma_shadow0_read_array(ecc, SH_ER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300661 /* Clear any pending event or error */
662 edma_write_array(ecc, EDMA_ECR, j, mask);
663 edma_write_array(ecc, EDMA_EMCR, j, mask);
664 /* Clear any SER */
665 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
666 edma_shadow0_write_array(ecc, SH_EESR, j, mask);
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300667 dev_dbg(ecc->dev, "EER%d %08x\n", j,
668 edma_shadow0_read_array(ecc, SH_EER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300669 return 0;
670 }
671
672 return -EINVAL;
673}
674
675/**
676 * edma_stop - stops dma on the channel passed
677 * @ecc: pointer to edma_cc struct
678 * @channel: channel being deactivated
679 *
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300680 * Any active transfer is paused and all pending hardware events are cleared.
681 * The current transfer may not be resumed, and the channel's Parameter RAM
682 * should be reinitialized before being reused.
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300683 */
684static void edma_stop(struct edma_cc *ecc, unsigned channel)
685{
686 if (ecc->id != EDMA_CTLR(channel)) {
687 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
688 ecc->id, EDMA_CTLR(channel));
689 return;
690 }
691 channel = EDMA_CHAN_SLOT(channel);
692
693 if (channel < ecc->num_channels) {
694 int j = channel >> 5;
695 unsigned int mask = BIT(channel & 0x1f);
696
697 edma_shadow0_write_array(ecc, SH_EECR, j, mask);
698 edma_shadow0_write_array(ecc, SH_ECR, j, mask);
699 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
700 edma_write_array(ecc, EDMA_EMCR, j, mask);
701
702 /* clear possibly pending completion interrupt */
703 edma_shadow0_write_array(ecc, SH_ICR, j, mask);
704
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300705 dev_dbg(ecc->dev, "EER%d %08x\n", j,
706 edma_shadow0_read_array(ecc, SH_EER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300707
708 /* REVISIT: consider guarding against inappropriate event
709 * chaining by overwriting with dummy_paramset.
710 */
711 }
712}
713
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300714/*
715 * Temporarily disable EDMA hardware events on the specified channel,
716 * preventing them from triggering new transfers
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300717 */
718static void edma_pause(struct edma_cc *ecc, unsigned channel)
719{
720 if (ecc->id != EDMA_CTLR(channel)) {
721 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
722 ecc->id, EDMA_CTLR(channel));
723 return;
724 }
725 channel = EDMA_CHAN_SLOT(channel);
726
727 if (channel < ecc->num_channels) {
728 unsigned int mask = BIT(channel & 0x1f);
729
730 edma_shadow0_write_array(ecc, SH_EECR, channel >> 5, mask);
731 }
732}
733
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300734/* Re-enable EDMA hardware events on the specified channel. */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300735static void edma_resume(struct edma_cc *ecc, unsigned channel)
736{
737 if (ecc->id != EDMA_CTLR(channel)) {
738 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
739 ecc->id, EDMA_CTLR(channel));
740 return;
741 }
742 channel = EDMA_CHAN_SLOT(channel);
743
744 if (channel < ecc->num_channels) {
745 unsigned int mask = BIT(channel & 0x1f);
746
747 edma_shadow0_write_array(ecc, SH_EESR, channel >> 5, mask);
748 }
749}
750
751static int edma_trigger_channel(struct edma_cc *ecc, unsigned channel)
752{
753 unsigned int mask;
754
755 if (ecc->id != EDMA_CTLR(channel)) {
756 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
757 ecc->id, EDMA_CTLR(channel));
758 return -EINVAL;
759 }
760 channel = EDMA_CHAN_SLOT(channel);
761 mask = BIT(channel & 0x1f);
762
763 edma_shadow0_write_array(ecc, SH_ESR, (channel >> 5), mask);
764
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300765 dev_dbg(ecc->dev, "ESR%d %08x\n", (channel >> 5),
766 edma_shadow0_read_array(ecc, SH_ESR, (channel >> 5)));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300767 return 0;
768}
769
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300770static void edma_clean_channel(struct edma_cc *ecc, unsigned channel)
771{
772 if (ecc->id != EDMA_CTLR(channel)) {
773 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
774 ecc->id, EDMA_CTLR(channel));
775 return;
776 }
777 channel = EDMA_CHAN_SLOT(channel);
778
779 if (channel < ecc->num_channels) {
780 int j = (channel >> 5);
781 unsigned int mask = BIT(channel & 0x1f);
782
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300783 dev_dbg(ecc->dev, "EMR%d %08x\n", j,
784 edma_read_array(ecc, EDMA_EMR, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300785 edma_shadow0_write_array(ecc, SH_ECR, j, mask);
786 /* Clear the corresponding EMR bits */
787 edma_write_array(ecc, EDMA_EMCR, j, mask);
788 /* Clear any SER */
789 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
790 edma_write(ecc, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
791 }
792}
793
794/**
795 * edma_alloc_channel - allocate DMA channel and paired parameter RAM
796 * @ecc: pointer to edma_cc struct
797 * @channel: specific channel to allocate; negative for "any unmapped channel"
798 * @callback: optional; to be issued on DMA completion or errors
799 * @data: passed to callback
800 * @eventq_no: an EVENTQ_* constant, used to choose which Transfer
801 * Controller (TC) executes requests using this channel. Use
802 * EVENTQ_DEFAULT unless you really need a high priority queue.
803 *
804 * This allocates a DMA channel and its associated parameter RAM slot.
805 * The parameter RAM is initialized to hold a dummy transfer.
806 *
807 * Normal use is to pass a specific channel number as @channel, to make
808 * use of hardware events mapped to that channel. When the channel will
809 * be used only for software triggering or event chaining, channels not
810 * mapped to hardware events (or mapped to unused events) are preferable.
811 *
812 * DMA transfers start from a channel using edma_start(), or by
813 * chaining. When the transfer described in that channel's parameter RAM
814 * slot completes, that slot's data may be reloaded through a link.
815 *
816 * DMA errors are only reported to the @callback associated with the
817 * channel driving that transfer, but transfer completion callbacks can
818 * be sent to another channel under control of the TCC field in
819 * the option word of the transfer's parameter RAM set. Drivers must not
820 * use DMA transfer completion callbacks for channels they did not allocate.
821 * (The same applies to TCC codes used in transfer chaining.)
822 *
823 * Returns the number of the channel, else negative errno.
824 */
825static int edma_alloc_channel(struct edma_cc *ecc, int channel,
826 void (*callback)(unsigned channel, u16 ch_status, void *data),
827 void *data,
828 enum dma_event_q eventq_no)
829{
830 unsigned done = 0;
831 int ret = 0;
832
833 if (!ecc->unused_chan_list_done) {
834 /*
835 * Scan all the platform devices to find out the EDMA channels
836 * used and clear them in the unused list, making the rest
837 * available for ARM usage.
838 */
839 ret = bus_for_each_dev(&platform_bus_type, NULL, ecc,
840 prepare_unused_channel_list);
841 if (ret < 0)
842 return ret;
843
844 ecc->unused_chan_list_done = true;
845 }
846
847 if (channel >= 0) {
848 if (ecc->id != EDMA_CTLR(channel)) {
849 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n",
850 __func__, ecc->id, EDMA_CTLR(channel));
851 return -EINVAL;
852 }
853 channel = EDMA_CHAN_SLOT(channel);
854 }
855
856 if (channel < 0) {
857 channel = 0;
858 for (;;) {
859 channel = find_next_bit(ecc->edma_unused,
860 ecc->num_channels, channel);
861 if (channel == ecc->num_channels)
862 break;
863 if (!test_and_set_bit(channel, ecc->edma_inuse)) {
864 done = 1;
865 break;
866 }
867 channel++;
868 }
869 if (!done)
870 return -ENOMEM;
871 } else if (channel >= ecc->num_channels) {
872 return -EINVAL;
873 } else if (test_and_set_bit(channel, ecc->edma_inuse)) {
874 return -EBUSY;
875 }
876
877 /* ensure access through shadow region 0 */
878 edma_or_array2(ecc, EDMA_DRAE, 0, channel >> 5, BIT(channel & 0x1f));
879
880 /* ensure no events are pending */
881 edma_stop(ecc, EDMA_CTLR_CHAN(ecc->id, channel));
882 edma_write_slot(ecc, channel, &dummy_paramset);
883
884 if (callback)
885 edma_setup_interrupt(ecc, EDMA_CTLR_CHAN(ecc->id, channel),
886 callback, data);
887
888 edma_map_dmach_to_queue(ecc, channel, eventq_no);
889
890 return EDMA_CTLR_CHAN(ecc->id, channel);
891}
892
893/**
894 * edma_free_channel - deallocate DMA channel
895 * @ecc: pointer to edma_cc struct
896 * @channel: dma channel returned from edma_alloc_channel()
897 *
898 * This deallocates the DMA channel and associated parameter RAM slot
899 * allocated by edma_alloc_channel().
900 *
901 * Callers are responsible for ensuring the channel is inactive, and
902 * will not be reactivated by linking, chaining, or software calls to
903 * edma_start().
904 */
905static void edma_free_channel(struct edma_cc *ecc, unsigned channel)
906{
907 if (ecc->id != EDMA_CTLR(channel)) {
908 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
909 ecc->id, EDMA_CTLR(channel));
910 return;
911 }
912 channel = EDMA_CHAN_SLOT(channel);
913
914 if (channel >= ecc->num_channels)
915 return;
916
917 edma_setup_interrupt(ecc, channel, NULL, NULL);
918 /* REVISIT should probably take out of shadow region 0 */
919
Peter Ujfalusi96f5ff02015-10-14 14:42:58 +0300920 edma_write_slot(ecc, channel, &dummy_paramset);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300921 clear_bit(channel, ecc->edma_inuse);
922}
923
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300924/* Move channel to a specific event queue */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300925static void edma_assign_channel_eventq(struct edma_cc *ecc, unsigned channel,
926 enum dma_event_q eventq_no)
927{
928 if (ecc->id != EDMA_CTLR(channel)) {
929 dev_err(ecc->dev, "%s: ID mismatch for eDMA%d: %d\n", __func__,
930 ecc->id, EDMA_CTLR(channel));
931 return;
932 }
933 channel = EDMA_CHAN_SLOT(channel);
934
935 if (channel >= ecc->num_channels)
936 return;
937
938 /* default to low priority queue */
939 if (eventq_no == EVENTQ_DEFAULT)
940 eventq_no = ecc->default_queue;
941 if (eventq_no >= ecc->num_tc)
942 return;
943
944 edma_map_dmach_to_queue(ecc, channel, eventq_no);
945}
946
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300947/* eDMA interrupt handler */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300948static irqreturn_t dma_irq_handler(int irq, void *data)
949{
950 struct edma_cc *ecc = data;
951 int ctlr;
952 u32 sh_ier;
953 u32 sh_ipr;
954 u32 bank;
955
956 ctlr = ecc->id;
957 if (ctlr < 0)
958 return IRQ_NONE;
959
960 dev_dbg(ecc->dev, "dma_irq_handler\n");
961
962 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 0);
963 if (!sh_ipr) {
964 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 1);
965 if (!sh_ipr)
966 return IRQ_NONE;
967 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 1);
968 bank = 1;
969 } else {
970 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 0);
971 bank = 0;
972 }
973
974 do {
975 u32 slot;
976 u32 channel;
977
978 dev_dbg(ecc->dev, "IPR%d %08x\n", bank, sh_ipr);
979
980 slot = __ffs(sh_ipr);
981 sh_ipr &= ~(BIT(slot));
982
983 if (sh_ier & BIT(slot)) {
984 channel = (bank << 5) | slot;
985 /* Clear the corresponding IPR bits */
986 edma_shadow0_write_array(ecc, SH_ICR, bank, BIT(slot));
987 if (ecc->intr_data[channel].callback)
988 ecc->intr_data[channel].callback(
989 EDMA_CTLR_CHAN(ctlr, channel),
990 EDMA_DMA_COMPLETE,
991 ecc->intr_data[channel].data);
992 }
993 } while (sh_ipr);
994
995 edma_shadow0_write(ecc, SH_IEVAL, 1);
996 return IRQ_HANDLED;
997}
998
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300999/* eDMA error interrupt handler */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001000static irqreturn_t dma_ccerr_handler(int irq, void *data)
1001{
1002 struct edma_cc *ecc = data;
1003 int i;
1004 int ctlr;
1005 unsigned int cnt = 0;
1006
1007 ctlr = ecc->id;
1008 if (ctlr < 0)
1009 return IRQ_NONE;
1010
1011 dev_dbg(ecc->dev, "dma_ccerr_handler\n");
1012
1013 if ((edma_read_array(ecc, EDMA_EMR, 0) == 0) &&
1014 (edma_read_array(ecc, EDMA_EMR, 1) == 0) &&
1015 (edma_read(ecc, EDMA_QEMR) == 0) &&
1016 (edma_read(ecc, EDMA_CCERR) == 0))
1017 return IRQ_NONE;
1018
1019 while (1) {
1020 int j = -1;
1021
1022 if (edma_read_array(ecc, EDMA_EMR, 0))
1023 j = 0;
1024 else if (edma_read_array(ecc, EDMA_EMR, 1))
1025 j = 1;
1026 if (j >= 0) {
1027 dev_dbg(ecc->dev, "EMR%d %08x\n", j,
1028 edma_read_array(ecc, EDMA_EMR, j));
1029 for (i = 0; i < 32; i++) {
1030 int k = (j << 5) + i;
1031
1032 if (edma_read_array(ecc, EDMA_EMR, j) &
1033 BIT(i)) {
1034 /* Clear the corresponding EMR bits */
1035 edma_write_array(ecc, EDMA_EMCR, j,
1036 BIT(i));
1037 /* Clear any SER */
1038 edma_shadow0_write_array(ecc, SH_SECR,
1039 j, BIT(i));
1040 if (ecc->intr_data[k].callback) {
1041 ecc->intr_data[k].callback(
1042 EDMA_CTLR_CHAN(ctlr, k),
1043 EDMA_DMA_CC_ERROR,
1044 ecc->intr_data[k].data);
1045 }
1046 }
1047 }
1048 } else if (edma_read(ecc, EDMA_QEMR)) {
1049 dev_dbg(ecc->dev, "QEMR %02x\n",
1050 edma_read(ecc, EDMA_QEMR));
1051 for (i = 0; i < 8; i++) {
1052 if (edma_read(ecc, EDMA_QEMR) & BIT(i)) {
1053 /* Clear the corresponding IPR bits */
1054 edma_write(ecc, EDMA_QEMCR, BIT(i));
1055 edma_shadow0_write(ecc, SH_QSECR,
1056 BIT(i));
1057
1058 /* NOTE: not reported!! */
1059 }
1060 }
1061 } else if (edma_read(ecc, EDMA_CCERR)) {
1062 dev_dbg(ecc->dev, "CCERR %08x\n",
1063 edma_read(ecc, EDMA_CCERR));
1064 /* FIXME: CCERR.BIT(16) ignored! much better
1065 * to just write CCERRCLR with CCERR value...
1066 */
1067 for (i = 0; i < 8; i++) {
1068 if (edma_read(ecc, EDMA_CCERR) & BIT(i)) {
1069 /* Clear the corresponding IPR bits */
1070 edma_write(ecc, EDMA_CCERRCLR, BIT(i));
1071
1072 /* NOTE: not reported!! */
1073 }
1074 }
1075 }
1076 if ((edma_read_array(ecc, EDMA_EMR, 0) == 0) &&
1077 (edma_read_array(ecc, EDMA_EMR, 1) == 0) &&
1078 (edma_read(ecc, EDMA_QEMR) == 0) &&
1079 (edma_read(ecc, EDMA_CCERR) == 0))
1080 break;
1081 cnt++;
1082 if (cnt > 10)
1083 break;
1084 }
1085 edma_write(ecc, EDMA_EEVAL, 1);
1086 return IRQ_HANDLED;
1087}
1088
Matt Porterc2dde5f2012-08-22 21:09:34 -04001089static inline struct edma_cc *to_edma_cc(struct dma_device *d)
1090{
1091 return container_of(d, struct edma_cc, dma_slave);
1092}
1093
1094static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
1095{
1096 return container_of(c, struct edma_chan, vchan.chan);
1097}
1098
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001099static inline struct edma_desc *to_edma_desc(struct dma_async_tx_descriptor *tx)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001100{
1101 return container_of(tx, struct edma_desc, vdesc.tx);
1102}
1103
1104static void edma_desc_free(struct virt_dma_desc *vdesc)
1105{
1106 kfree(container_of(vdesc, struct edma_desc, vdesc));
1107}
1108
1109/* Dispatch a queued descriptor to the controller (caller holds lock) */
1110static void edma_execute(struct edma_chan *echan)
1111{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001112 struct edma_cc *ecc = echan->ecc;
Joel Fernandes53407062013-09-03 10:02:46 -05001113 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001114 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -05001115 struct device *dev = echan->vchan.chan.device->dev;
1116 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001117
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001118 if (!echan->edesc) {
1119 /* Setup is needed for the first transfer */
Joel Fernandes53407062013-09-03 10:02:46 -05001120 vdesc = vchan_next_desc(&echan->vchan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001121 if (!vdesc)
Joel Fernandes53407062013-09-03 10:02:46 -05001122 return;
Joel Fernandes53407062013-09-03 10:02:46 -05001123 list_del(&vdesc->node);
1124 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001125 }
1126
Joel Fernandes53407062013-09-03 10:02:46 -05001127 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001128
Joel Fernandes53407062013-09-03 10:02:46 -05001129 /* Find out how many left */
1130 left = edesc->pset_nr - edesc->processed;
1131 nslots = min(MAX_NR_SG, left);
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001132 edesc->sg_len = 0;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001133
1134 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -05001135 for (i = 0; i < nslots; i++) {
1136 j = i + edesc->processed;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001137 edma_write_slot(ecc, echan->slot[i], &edesc->pset[j].param);
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001138 edesc->sg_len += edesc->pset[j].len;
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03001139 dev_vdbg(dev,
1140 "\n pset[%d]:\n"
1141 " chnum\t%d\n"
1142 " slot\t%d\n"
1143 " opt\t%08x\n"
1144 " src\t%08x\n"
1145 " dst\t%08x\n"
1146 " abcnt\t%08x\n"
1147 " ccnt\t%08x\n"
1148 " bidx\t%08x\n"
1149 " cidx\t%08x\n"
1150 " lkrld\t%08x\n",
1151 j, echan->ch_num, echan->slot[i],
1152 edesc->pset[j].param.opt,
1153 edesc->pset[j].param.src,
1154 edesc->pset[j].param.dst,
1155 edesc->pset[j].param.a_b_cnt,
1156 edesc->pset[j].param.ccnt,
1157 edesc->pset[j].param.src_dst_bidx,
1158 edesc->pset[j].param.src_dst_cidx,
1159 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001160 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -05001161 if (i != (nslots - 1))
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001162 edma_link(ecc, echan->slot[i], echan->slot[i + 1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001163 }
1164
Joel Fernandes53407062013-09-03 10:02:46 -05001165 edesc->processed += nslots;
1166
Joel Fernandesb267b3b2013-08-29 18:05:44 -05001167 /*
1168 * If this is either the last set in a set of SG-list transactions
1169 * then setup a link to the dummy slot, this results in all future
1170 * events being absorbed and that's OK because we're done
1171 */
Joel Fernandes50a9c702013-10-31 16:31:23 -05001172 if (edesc->processed == edesc->pset_nr) {
1173 if (edesc->cyclic)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001174 edma_link(ecc, echan->slot[nslots - 1], echan->slot[1]);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001175 else
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001176 edma_link(ecc, echan->slot[nslots - 1],
Joel Fernandes50a9c702013-10-31 16:31:23 -05001177 echan->ecc->dummy_slot);
1178 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -05001179
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001180 if (echan->missed) {
1181 /*
1182 * This happens due to setup times between intermediate
1183 * transfers in long SG lists which have to be broken up into
1184 * transfers of MAX_NR_SG
1185 */
1186 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001187 edma_clean_channel(ecc, echan->ch_num);
1188 edma_stop(ecc, echan->ch_num);
1189 edma_start(ecc, echan->ch_num);
1190 edma_trigger_channel(ecc, echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001191 echan->missed = 0;
1192 } else if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +03001193 dev_dbg(dev, "first transfer starting on channel %d\n",
1194 echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001195 edma_start(ecc, echan->ch_num);
Sekhar Nori5fc68a62014-03-19 11:25:50 +05301196 } else {
1197 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
1198 echan->ch_num, edesc->processed);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001199 edma_resume(ecc, echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -05001200 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001201}
1202
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001203static int edma_terminate_all(struct dma_chan *chan)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001204{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001205 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001206 unsigned long flags;
1207 LIST_HEAD(head);
1208
1209 spin_lock_irqsave(&echan->vchan.lock, flags);
1210
1211 /*
1212 * Stop DMA activity: we assume the callback will not be called
1213 * after edma_dma() returns (even if it does, it will see
1214 * echan->edesc is NULL and exit.)
1215 */
1216 if (echan->edesc) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001217 edma_stop(echan->ecc, echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001218 /* Move the cyclic channel back to default queue */
1219 if (echan->edesc->cyclic)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001220 edma_assign_channel_eventq(echan->ecc, echan->ch_num,
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001221 EVENTQ_DEFAULT);
Petr Kulhavy5ca9e7c2015-03-27 13:35:51 +02001222 /*
1223 * free the running request descriptor
1224 * since it is not in any of the vdesc lists
1225 */
1226 edma_desc_free(&echan->edesc->vdesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001227 echan->edesc = NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001228 }
1229
1230 vchan_get_all_descriptors(&echan->vchan, &head);
1231 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1232 vchan_dma_desc_free_list(&echan->vchan, &head);
1233
1234 return 0;
1235}
1236
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001237static int edma_slave_config(struct dma_chan *chan,
Matt Porter661f7cb2013-01-10 13:41:04 -05001238 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001239{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001240 struct edma_chan *echan = to_edma_chan(chan);
1241
Matt Porter661f7cb2013-01-10 13:41:04 -05001242 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
1243 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001244 return -EINVAL;
1245
Matt Porter661f7cb2013-01-10 13:41:04 -05001246 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -04001247
1248 return 0;
1249}
1250
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001251static int edma_dma_pause(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001252{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001253 struct edma_chan *echan = to_edma_chan(chan);
1254
John Ogness02ec6042015-04-27 13:52:25 +02001255 if (!echan->edesc)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001256 return -EINVAL;
1257
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001258 edma_pause(echan->ecc, echan->ch_num);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001259 return 0;
1260}
1261
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001262static int edma_dma_resume(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001263{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001264 struct edma_chan *echan = to_edma_chan(chan);
1265
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001266 edma_resume(echan->ecc, echan->ch_num);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +03001267 return 0;
1268}
1269
Joel Fernandesfd009032013-09-23 18:05:13 -05001270/*
1271 * A PaRAM set configuration abstraction used by other modes
1272 * @chan: Channel who's PaRAM set we're configuring
1273 * @pset: PaRAM set to initialize and setup.
1274 * @src_addr: Source address of the DMA
1275 * @dst_addr: Destination address of the DMA
1276 * @burst: In units of dev_width, how much to send
1277 * @dev_width: How much is the dev_width
1278 * @dma_length: Total length of the DMA transfer
1279 * @direction: Direction of the transfer
1280 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001281static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001282 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
1283 enum dma_slave_buswidth dev_width,
1284 unsigned int dma_length,
1285 enum dma_transfer_direction direction)
Joel Fernandesfd009032013-09-23 18:05:13 -05001286{
1287 struct edma_chan *echan = to_edma_chan(chan);
1288 struct device *dev = chan->device->dev;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001289 struct edmacc_param *param = &epset->param;
Joel Fernandesfd009032013-09-23 18:05:13 -05001290 int acnt, bcnt, ccnt, cidx;
1291 int src_bidx, dst_bidx, src_cidx, dst_cidx;
1292 int absync;
1293
1294 acnt = dev_width;
Peter Ujfalusib2b617d2014-04-14 14:41:58 +03001295
1296 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
1297 if (!burst)
1298 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -05001299 /*
1300 * If the maxburst is equal to the fifo width, use
1301 * A-synced transfers. This allows for large contiguous
1302 * buffer transfers using only one PaRAM set.
1303 */
1304 if (burst == 1) {
1305 /*
1306 * For the A-sync case, bcnt and ccnt are the remainder
1307 * and quotient respectively of the division of:
1308 * (dma_length / acnt) by (SZ_64K -1). This is so
1309 * that in case bcnt over flows, we have ccnt to use.
1310 * Note: In A-sync tranfer only, bcntrld is used, but it
1311 * only applies for sg_dma_len(sg) >= SZ_64K.
1312 * In this case, the best way adopted is- bccnt for the
1313 * first frame will be the remainder below. Then for
1314 * every successive frame, bcnt will be SZ_64K-1. This
1315 * is assured as bcntrld = 0xffff in end of function.
1316 */
1317 absync = false;
1318 ccnt = dma_length / acnt / (SZ_64K - 1);
1319 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
1320 /*
1321 * If bcnt is non-zero, we have a remainder and hence an
1322 * extra frame to transfer, so increment ccnt.
1323 */
1324 if (bcnt)
1325 ccnt++;
1326 else
1327 bcnt = SZ_64K - 1;
1328 cidx = acnt;
1329 } else {
1330 /*
1331 * If maxburst is greater than the fifo address_width,
1332 * use AB-synced transfers where A count is the fifo
1333 * address_width and B count is the maxburst. In this
1334 * case, we are limited to transfers of C count frames
1335 * of (address_width * maxburst) where C count is limited
1336 * to SZ_64K-1. This places an upper bound on the length
1337 * of an SG segment that can be handled.
1338 */
1339 absync = true;
1340 bcnt = burst;
1341 ccnt = dma_length / (acnt * bcnt);
1342 if (ccnt > (SZ_64K - 1)) {
1343 dev_err(dev, "Exceeded max SG segment size\n");
1344 return -EINVAL;
1345 }
1346 cidx = acnt * bcnt;
1347 }
1348
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001349 epset->len = dma_length;
1350
Joel Fernandesfd009032013-09-23 18:05:13 -05001351 if (direction == DMA_MEM_TO_DEV) {
1352 src_bidx = acnt;
1353 src_cidx = cidx;
1354 dst_bidx = 0;
1355 dst_cidx = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001356 epset->addr = src_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001357 } else if (direction == DMA_DEV_TO_MEM) {
1358 src_bidx = 0;
1359 src_cidx = 0;
1360 dst_bidx = acnt;
1361 dst_cidx = cidx;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001362 epset->addr = dst_addr;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001363 } else if (direction == DMA_MEM_TO_MEM) {
1364 src_bidx = acnt;
1365 src_cidx = cidx;
1366 dst_bidx = acnt;
1367 dst_cidx = cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001368 } else {
1369 dev_err(dev, "%s: direction not implemented yet\n", __func__);
1370 return -EINVAL;
1371 }
1372
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001373 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
Joel Fernandesfd009032013-09-23 18:05:13 -05001374 /* Configure A or AB synchronized transfers */
1375 if (absync)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001376 param->opt |= SYNCDIM;
Joel Fernandesfd009032013-09-23 18:05:13 -05001377
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001378 param->src = src_addr;
1379 param->dst = dst_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001380
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001381 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
1382 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001383
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001384 param->a_b_cnt = bcnt << 16 | acnt;
1385 param->ccnt = ccnt;
Joel Fernandesfd009032013-09-23 18:05:13 -05001386 /*
1387 * Only time when (bcntrld) auto reload is required is for
1388 * A-sync case, and in this case, a requirement of reload value
1389 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
1390 * and then later will be populated by edma_execute.
1391 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001392 param->link_bcntrld = 0xffffffff;
Joel Fernandesfd009032013-09-23 18:05:13 -05001393 return absync;
1394}
1395
Matt Porterc2dde5f2012-08-22 21:09:34 -04001396static struct dma_async_tx_descriptor *edma_prep_slave_sg(
1397 struct dma_chan *chan, struct scatterlist *sgl,
1398 unsigned int sg_len, enum dma_transfer_direction direction,
1399 unsigned long tx_flags, void *context)
1400{
1401 struct edma_chan *echan = to_edma_chan(chan);
1402 struct device *dev = chan->device->dev;
1403 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -05001404 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -05001405 enum dma_slave_buswidth dev_width;
1406 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001407 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -05001408 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001409
1410 if (unlikely(!echan || !sgl || !sg_len))
1411 return NULL;
1412
Matt Porter661f7cb2013-01-10 13:41:04 -05001413 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001414 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001415 dev_width = echan->cfg.src_addr_width;
1416 burst = echan->cfg.src_maxburst;
1417 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001418 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001419 dev_width = echan->cfg.dst_addr_width;
1420 burst = echan->cfg.dst_maxburst;
1421 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001422 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Matt Porter661f7cb2013-01-10 13:41:04 -05001423 return NULL;
1424 }
1425
1426 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001427 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001428 return NULL;
1429 }
1430
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001431 edesc = kzalloc(sizeof(*edesc) + sg_len * sizeof(edesc->pset[0]),
1432 GFP_ATOMIC);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001433 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001434 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001435 return NULL;
1436 }
1437
1438 edesc->pset_nr = sg_len;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001439 edesc->residue = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001440 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001441 edesc->echan = echan;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001442
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001443 /* Allocate a PaRAM slot, if needed */
1444 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
1445
1446 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001447 if (echan->slot[i] < 0) {
1448 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001449 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001450 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +03001451 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001452 dev_err(dev, "%s: Failed to allocate slot\n",
1453 __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001454 return NULL;
1455 }
1456 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001457 }
1458
1459 /* Configure PaRAM sets for each SG */
1460 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001461 /* Get address for each SG */
1462 if (direction == DMA_DEV_TO_MEM)
1463 dst_addr = sg_dma_address(sg);
1464 else
1465 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001466
Joel Fernandesfd009032013-09-23 18:05:13 -05001467 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1468 dst_addr, burst, dev_width,
1469 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +05301470 if (ret < 0) {
1471 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -05001472 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001473 }
1474
Joel Fernandesfd009032013-09-23 18:05:13 -05001475 edesc->absync = ret;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001476 edesc->residue += sg_dma_len(sg);
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001477
1478 /* If this is the last in a current SG set of transactions,
1479 enable interrupts so that next set is processed */
1480 if (!((i+1) % MAX_NR_SG))
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001481 edesc->pset[i].param.opt |= TCINTEN;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001482
Matt Porterc2dde5f2012-08-22 21:09:34 -04001483 /* If this is the last set, enable completion interrupt flag */
1484 if (i == sg_len - 1)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001485 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001486 }
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001487 edesc->residue_stat = edesc->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001488
Matt Porterc2dde5f2012-08-22 21:09:34 -04001489 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1490}
Matt Porterc2dde5f2012-08-22 21:09:34 -04001491
Lad, Prabhakarb7a4fd52015-02-04 13:03:27 +00001492static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001493 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1494 size_t len, unsigned long tx_flags)
1495{
1496 int ret;
1497 struct edma_desc *edesc;
1498 struct device *dev = chan->device->dev;
1499 struct edma_chan *echan = to_edma_chan(chan);
1500
1501 if (unlikely(!echan || !len))
1502 return NULL;
1503
1504 edesc = kzalloc(sizeof(*edesc) + sizeof(edesc->pset[0]), GFP_ATOMIC);
1505 if (!edesc) {
1506 dev_dbg(dev, "Failed to allocate a descriptor\n");
1507 return NULL;
1508 }
1509
1510 edesc->pset_nr = 1;
1511
1512 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
1513 DMA_SLAVE_BUSWIDTH_4_BYTES, len, DMA_MEM_TO_MEM);
1514 if (ret < 0)
1515 return NULL;
1516
1517 edesc->absync = ret;
1518
1519 /*
1520 * Enable intermediate transfer chaining to re-trigger channel
1521 * on completion of every TR, and enable transfer-completion
1522 * interrupt on completion of the whole transfer.
1523 */
Joel Fernandesb0cce4c2014-04-28 15:30:32 -05001524 edesc->pset[0].param.opt |= ITCCHEN;
1525 edesc->pset[0].param.opt |= TCINTEN;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001526
1527 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1528}
1529
Joel Fernandes50a9c702013-10-31 16:31:23 -05001530static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
1531 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1532 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +02001533 unsigned long tx_flags)
Joel Fernandes50a9c702013-10-31 16:31:23 -05001534{
1535 struct edma_chan *echan = to_edma_chan(chan);
1536 struct device *dev = chan->device->dev;
1537 struct edma_desc *edesc;
1538 dma_addr_t src_addr, dst_addr;
1539 enum dma_slave_buswidth dev_width;
1540 u32 burst;
1541 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001542
Joel Fernandes50a9c702013-10-31 16:31:23 -05001543 if (unlikely(!echan || !buf_len || !period_len))
1544 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001545
Joel Fernandes50a9c702013-10-31 16:31:23 -05001546 if (direction == DMA_DEV_TO_MEM) {
1547 src_addr = echan->cfg.src_addr;
1548 dst_addr = buf_addr;
1549 dev_width = echan->cfg.src_addr_width;
1550 burst = echan->cfg.src_maxburst;
1551 } else if (direction == DMA_MEM_TO_DEV) {
1552 src_addr = buf_addr;
1553 dst_addr = echan->cfg.dst_addr;
1554 dev_width = echan->cfg.dst_addr_width;
1555 burst = echan->cfg.dst_maxburst;
1556 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001557 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001558 return NULL;
1559 }
1560
1561 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001562 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001563 return NULL;
1564 }
1565
1566 if (unlikely(buf_len % period_len)) {
1567 dev_err(dev, "Period should be multiple of Buffer length\n");
1568 return NULL;
1569 }
1570
1571 nslots = (buf_len / period_len) + 1;
1572
1573 /*
1574 * Cyclic DMA users such as audio cannot tolerate delays introduced
1575 * by cases where the number of periods is more than the maximum
1576 * number of SGs the EDMA driver can handle at a time. For DMA types
1577 * such as Slave SGs, such delays are tolerable and synchronized,
1578 * but the synchronization is difficult to achieve with Cyclic and
1579 * cannot be guaranteed, so we error out early.
1580 */
1581 if (nslots > MAX_NR_SG)
1582 return NULL;
1583
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001584 edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]),
1585 GFP_ATOMIC);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001586 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001587 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001588 return NULL;
1589 }
1590
1591 edesc->cyclic = 1;
1592 edesc->pset_nr = nslots;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001593 edesc->residue = edesc->residue_stat = buf_len;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001594 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001595 edesc->echan = echan;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001596
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001597 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
1598 __func__, echan->ch_num, nslots, period_len, buf_len);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001599
1600 for (i = 0; i < nslots; i++) {
1601 /* Allocate a PaRAM slot, if needed */
1602 if (echan->slot[i] < 0) {
1603 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001604 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001605 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001606 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001607 dev_err(dev, "%s: Failed to allocate slot\n",
1608 __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001609 return NULL;
1610 }
1611 }
1612
1613 if (i == nslots - 1) {
1614 memcpy(&edesc->pset[i], &edesc->pset[0],
1615 sizeof(edesc->pset[0]));
1616 break;
1617 }
1618
1619 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1620 dst_addr, burst, dev_width, period_len,
1621 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001622 if (ret < 0) {
1623 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001624 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001625 }
Joel Fernandes50a9c702013-10-31 16:31:23 -05001626
1627 if (direction == DMA_DEV_TO_MEM)
1628 dst_addr += period_len;
1629 else
1630 src_addr += period_len;
1631
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001632 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
1633 dev_vdbg(dev,
Joel Fernandes50a9c702013-10-31 16:31:23 -05001634 "\n pset[%d]:\n"
1635 " chnum\t%d\n"
1636 " slot\t%d\n"
1637 " opt\t%08x\n"
1638 " src\t%08x\n"
1639 " dst\t%08x\n"
1640 " abcnt\t%08x\n"
1641 " ccnt\t%08x\n"
1642 " bidx\t%08x\n"
1643 " cidx\t%08x\n"
1644 " lkrld\t%08x\n",
1645 i, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001646 edesc->pset[i].param.opt,
1647 edesc->pset[i].param.src,
1648 edesc->pset[i].param.dst,
1649 edesc->pset[i].param.a_b_cnt,
1650 edesc->pset[i].param.ccnt,
1651 edesc->pset[i].param.src_dst_bidx,
1652 edesc->pset[i].param.src_dst_cidx,
1653 edesc->pset[i].param.link_bcntrld);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001654
1655 edesc->absync = ret;
1656
1657 /*
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001658 * Enable period interrupt only if it is requested
Joel Fernandes50a9c702013-10-31 16:31:23 -05001659 */
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001660 if (tx_flags & DMA_PREP_INTERRUPT)
1661 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001662 }
1663
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001664 /* Place the cyclic channel to highest priority queue */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001665 edma_assign_channel_eventq(echan->ecc, echan->ch_num, EVENTQ_0);
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001666
Matt Porterc2dde5f2012-08-22 21:09:34 -04001667 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1668}
1669
1670static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
1671{
1672 struct edma_chan *echan = data;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001673 struct edma_cc *ecc = echan->ecc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001674 struct device *dev = echan->vchan.chan.device->dev;
1675 struct edma_desc *edesc;
Joel Fernandesc5f47992013-08-29 18:05:43 -05001676 struct edmacc_param p;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001677
Joel Fernandes50a9c702013-10-31 16:31:23 -05001678 edesc = echan->edesc;
1679
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001680 spin_lock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001681 switch (ch_status) {
Vinod Kouldb60d8d2013-10-30 18:22:30 +05301682 case EDMA_DMA_COMPLETE:
Matt Porterc2dde5f2012-08-22 21:09:34 -04001683 if (edesc) {
Joel Fernandes50a9c702013-10-31 16:31:23 -05001684 if (edesc->cyclic) {
1685 vchan_cyclic_callback(&edesc->vdesc);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001686 goto out;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001687 } else if (edesc->processed == edesc->pset_nr) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001688 dev_dbg(dev,
1689 "Transfer completed on channel %d\n",
1690 ch_num);
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001691 edesc->residue = 0;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001692 edma_stop(ecc, echan->ch_num);
Joel Fernandes53407062013-09-03 10:02:46 -05001693 vchan_cookie_complete(&edesc->vdesc);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001694 echan->edesc = NULL;
Joel Fernandes53407062013-09-03 10:02:46 -05001695 } else {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001696 dev_dbg(dev,
1697 "Sub transfer completed on channel %d\n",
1698 ch_num);
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001699
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001700 edma_pause(ecc, echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001701
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001702 /* Update statistics for tx_status */
1703 edesc->residue -= edesc->sg_len;
1704 edesc->residue_stat = edesc->residue;
1705 edesc->processed_stat = edesc->processed;
Joel Fernandes53407062013-09-03 10:02:46 -05001706 }
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001707 edma_execute(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001708 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001709 break;
Vinod Kouldb60d8d2013-10-30 18:22:30 +05301710 case EDMA_DMA_CC_ERROR:
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001711 edma_read_slot(ecc, echan->slot[0], &p);
Joel Fernandesc5f47992013-08-29 18:05:43 -05001712
1713 /*
1714 * Issue later based on missed flag which will be sure
1715 * to happen as:
1716 * (1) we finished transmitting an intermediate slot and
1717 * edma_execute is coming up.
1718 * (2) or we finished current transfer and issue will
1719 * call edma_execute.
1720 *
1721 * Important note: issuing can be dangerous here and
1722 * lead to some nasty recursion when we are in a NULL
1723 * slot. So we avoid doing so and set the missed flag.
1724 */
1725 if (p.a_b_cnt == 0 && p.ccnt == 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001726 dev_dbg(dev, "Error on null slot, setting miss\n");
Joel Fernandesc5f47992013-08-29 18:05:43 -05001727 echan->missed = 1;
1728 } else {
1729 /*
1730 * The slot is already programmed but the event got
1731 * missed, so its safe to issue it here.
1732 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001733 dev_dbg(dev, "Missed event, TRIGGERING\n");
1734 edma_clean_channel(ecc, echan->ch_num);
1735 edma_stop(ecc, echan->ch_num);
1736 edma_start(ecc, echan->ch_num);
1737 edma_trigger_channel(ecc, echan->ch_num);
Joel Fernandesc5f47992013-08-29 18:05:43 -05001738 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001739 break;
1740 default:
1741 break;
1742 }
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001743out:
1744 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001745}
1746
1747/* Alloc channel resources */
1748static int edma_alloc_chan_resources(struct dma_chan *chan)
1749{
1750 struct edma_chan *echan = to_edma_chan(chan);
1751 struct device *dev = chan->device->dev;
1752 int ret;
1753 int a_ch_num;
1754 LIST_HEAD(descs);
1755
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001756 a_ch_num = edma_alloc_channel(echan->ecc, echan->ch_num,
Peter Ujfalusica304fa2015-10-14 14:42:49 +03001757 edma_callback, echan, EVENTQ_DEFAULT);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001758
1759 if (a_ch_num < 0) {
1760 ret = -ENODEV;
1761 goto err_no_chan;
1762 }
1763
1764 if (a_ch_num != echan->ch_num) {
1765 dev_err(dev, "failed to allocate requested channel %u:%u\n",
1766 EDMA_CTLR(echan->ch_num),
1767 EDMA_CHAN_SLOT(echan->ch_num));
1768 ret = -ENODEV;
1769 goto err_wrong_chan;
1770 }
1771
1772 echan->alloced = true;
1773 echan->slot[0] = echan->ch_num;
1774
Peter Ujfalusi9aac9092014-04-24 10:29:50 +03001775 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
Ezequiel Garcia0e772c62013-12-13 11:06:18 -03001776 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
Matt Porterc2dde5f2012-08-22 21:09:34 -04001777
1778 return 0;
1779
1780err_wrong_chan:
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001781 edma_free_channel(echan->ecc, a_ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001782err_no_chan:
1783 return ret;
1784}
1785
1786/* Free channel resources */
1787static void edma_free_chan_resources(struct dma_chan *chan)
1788{
1789 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001790 int i;
1791
1792 /* Terminate transfers */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001793 edma_stop(echan->ecc, echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001794
1795 vchan_free_chan_resources(&echan->vchan);
1796
1797 /* Free EDMA PaRAM slots */
1798 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
1799 if (echan->slot[i] >= 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001800 edma_free_slot(echan->ecc, echan->slot[i]);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001801 echan->slot[i] = -1;
1802 }
1803 }
1804
1805 /* Free EDMA channel */
1806 if (echan->alloced) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001807 edma_free_channel(echan->ecc, echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001808 echan->alloced = false;
1809 }
1810
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03001811 dev_dbg(chan->device->dev, "freeing channel for %u\n", echan->ch_num);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001812}
1813
1814/* Send pending descriptor to hardware */
1815static void edma_issue_pending(struct dma_chan *chan)
1816{
1817 struct edma_chan *echan = to_edma_chan(chan);
1818 unsigned long flags;
1819
1820 spin_lock_irqsave(&echan->vchan.lock, flags);
1821 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
1822 edma_execute(echan);
1823 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1824}
1825
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001826static u32 edma_residue(struct edma_desc *edesc)
1827{
1828 bool dst = edesc->direction == DMA_DEV_TO_MEM;
1829 struct edma_pset *pset = edesc->pset;
1830 dma_addr_t done, pos;
1831 int i;
1832
1833 /*
1834 * We always read the dst/src position from the first RamPar
1835 * pset. That's the one which is active now.
1836 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001837 pos = edma_get_position(edesc->echan->ecc, edesc->echan->slot[0], dst);
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001838
1839 /*
1840 * Cyclic is simple. Just subtract pset[0].addr from pos.
1841 *
1842 * We never update edesc->residue in the cyclic case, so we
1843 * can tell the remaining room to the end of the circular
1844 * buffer.
1845 */
1846 if (edesc->cyclic) {
1847 done = pos - pset->addr;
1848 edesc->residue_stat = edesc->residue - done;
1849 return edesc->residue_stat;
1850 }
1851
1852 /*
1853 * For SG operation we catch up with the last processed
1854 * status.
1855 */
1856 pset += edesc->processed_stat;
1857
1858 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
1859 /*
1860 * If we are inside this pset address range, we know
1861 * this is the active one. Get the current delta and
1862 * stop walking the psets.
1863 */
1864 if (pos >= pset->addr && pos < pset->addr + pset->len)
1865 return edesc->residue_stat - (pos - pset->addr);
1866
1867 /* Otherwise mark it done and update residue_stat. */
1868 edesc->processed_stat++;
1869 edesc->residue_stat -= pset->len;
1870 }
1871 return edesc->residue_stat;
1872}
1873
Matt Porterc2dde5f2012-08-22 21:09:34 -04001874/* Check request completion status */
1875static enum dma_status edma_tx_status(struct dma_chan *chan,
1876 dma_cookie_t cookie,
1877 struct dma_tx_state *txstate)
1878{
1879 struct edma_chan *echan = to_edma_chan(chan);
1880 struct virt_dma_desc *vdesc;
1881 enum dma_status ret;
1882 unsigned long flags;
1883
1884 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +05301885 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001886 return ret;
1887
1888 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001889 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001890 txstate->residue = edma_residue(echan->edesc);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001891 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
1892 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001893 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1894
1895 return ret;
1896}
1897
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001898static void __init edma_chan_init(struct edma_cc *ecc, struct dma_device *dma,
Matt Porterc2dde5f2012-08-22 21:09:34 -04001899 struct edma_chan *echans)
1900{
1901 int i, j;
1902
Peter Ujfalusicb782052015-10-14 14:42:54 +03001903 for (i = 0; i < ecc->num_channels; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001904 struct edma_chan *echan = &echans[i];
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001905 echan->ch_num = EDMA_CTLR_CHAN(ecc->id, i);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001906 echan->ecc = ecc;
1907 echan->vchan.desc_free = edma_desc_free;
1908
1909 vchan_init(&echan->vchan, dma);
1910
1911 INIT_LIST_HEAD(&echan->node);
1912 for (j = 0; j < EDMA_MAX_SLOTS; j++)
1913 echan->slot[j] = -1;
1914 }
1915}
1916
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001917#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1918 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
Peter Ujfalusie4a899d2014-07-03 07:51:56 +03001919 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001920 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1921
Matt Porterc2dde5f2012-08-22 21:09:34 -04001922static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
1923 struct device *dev)
1924{
1925 dma->device_prep_slave_sg = edma_prep_slave_sg;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001926 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001927 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001928 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
1929 dma->device_free_chan_resources = edma_free_chan_resources;
1930 dma->device_issue_pending = edma_issue_pending;
1931 dma->device_tx_status = edma_tx_status;
Maxime Ripardaa7c09b2014-11-17 14:42:13 +01001932 dma->device_config = edma_slave_config;
1933 dma->device_pause = edma_dma_pause;
1934 dma->device_resume = edma_dma_resume;
1935 dma->device_terminate_all = edma_terminate_all;
Maxime Ripard9f59cd02014-11-17 14:42:47 +01001936
1937 dma->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1938 dma->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
1939 dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1940 dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1941
Matt Porterc2dde5f2012-08-22 21:09:34 -04001942 dma->dev = dev;
1943
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001944 /*
1945 * code using dma memcpy must make sure alignment of
1946 * length is at dma->copy_align boundary.
1947 */
Maxime Ripard77a68e52015-07-20 10:41:32 +02001948 dma->copy_align = DMAENGINE_ALIGN_4_BYTES;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001949
Matt Porterc2dde5f2012-08-22 21:09:34 -04001950 INIT_LIST_HEAD(&dma->channels);
1951}
1952
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001953static int edma_setup_from_hw(struct device *dev, struct edma_soc_info *pdata,
1954 struct edma_cc *ecc)
1955{
1956 int i;
1957 u32 value, cccfg;
1958 s8 (*queue_priority_map)[2];
1959
1960 /* Decode the eDMA3 configuration from CCCFG register */
1961 cccfg = edma_read(ecc, EDMA_CCCFG);
1962
1963 value = GET_NUM_REGN(cccfg);
1964 ecc->num_region = BIT(value);
1965
1966 value = GET_NUM_DMACH(cccfg);
1967 ecc->num_channels = BIT(value + 1);
1968
1969 value = GET_NUM_PAENTRY(cccfg);
1970 ecc->num_slots = BIT(value + 4);
1971
1972 value = GET_NUM_EVQUE(cccfg);
1973 ecc->num_tc = value + 1;
1974
1975 dev_dbg(dev, "eDMA3 CC HW configuration (cccfg: 0x%08x):\n", cccfg);
1976 dev_dbg(dev, "num_region: %u\n", ecc->num_region);
1977 dev_dbg(dev, "num_channels: %u\n", ecc->num_channels);
1978 dev_dbg(dev, "num_slots: %u\n", ecc->num_slots);
1979 dev_dbg(dev, "num_tc: %u\n", ecc->num_tc);
1980
1981 /* Nothing need to be done if queue priority is provided */
1982 if (pdata->queue_priority_mapping)
1983 return 0;
1984
1985 /*
1986 * Configure TC/queue priority as follows:
1987 * Q0 - priority 0
1988 * Q1 - priority 1
1989 * Q2 - priority 2
1990 * ...
1991 * The meaning of priority numbers: 0 highest priority, 7 lowest
1992 * priority. So Q0 is the highest priority queue and the last queue has
1993 * the lowest priority.
1994 */
Peter Ujfalusi547c6e22015-10-14 14:42:55 +03001995 queue_priority_map = devm_kcalloc(dev, ecc->num_tc + 1, sizeof(s8),
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001996 GFP_KERNEL);
1997 if (!queue_priority_map)
1998 return -ENOMEM;
1999
2000 for (i = 0; i < ecc->num_tc; i++) {
2001 queue_priority_map[i][0] = i;
2002 queue_priority_map[i][1] = i;
2003 }
2004 queue_priority_map[i][0] = -1;
2005 queue_priority_map[i][1] = -1;
2006
2007 pdata->queue_priority_mapping = queue_priority_map;
2008 /* Default queue has the lowest priority */
2009 pdata->default_queue = i - 1;
2010
2011 return 0;
2012}
2013
2014#if IS_ENABLED(CONFIG_OF)
2015static int edma_xbar_event_map(struct device *dev, struct edma_soc_info *pdata,
2016 size_t sz)
2017{
2018 const char pname[] = "ti,edma-xbar-event-map";
2019 struct resource res;
2020 void __iomem *xbar;
2021 s16 (*xbar_chans)[2];
2022 size_t nelm = sz / sizeof(s16);
2023 u32 shift, offset, mux;
2024 int ret, i;
2025
Peter Ujfalusi547c6e22015-10-14 14:42:55 +03002026 xbar_chans = devm_kcalloc(dev, nelm + 2, sizeof(s16), GFP_KERNEL);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002027 if (!xbar_chans)
2028 return -ENOMEM;
2029
2030 ret = of_address_to_resource(dev->of_node, 1, &res);
2031 if (ret)
2032 return -ENOMEM;
2033
2034 xbar = devm_ioremap(dev, res.start, resource_size(&res));
2035 if (!xbar)
2036 return -ENOMEM;
2037
2038 ret = of_property_read_u16_array(dev->of_node, pname, (u16 *)xbar_chans,
2039 nelm);
2040 if (ret)
2041 return -EIO;
2042
2043 /* Invalidate last entry for the other user of this mess */
2044 nelm >>= 1;
2045 xbar_chans[nelm][0] = -1;
2046 xbar_chans[nelm][1] = -1;
2047
2048 for (i = 0; i < nelm; i++) {
2049 shift = (xbar_chans[i][1] & 0x03) << 3;
2050 offset = xbar_chans[i][1] & 0xfffffffc;
2051 mux = readl(xbar + offset);
2052 mux &= ~(0xff << shift);
2053 mux |= xbar_chans[i][0] << shift;
2054 writel(mux, (xbar + offset));
2055 }
2056
2057 pdata->xbar_chans = (const s16 (*)[2]) xbar_chans;
2058 return 0;
2059}
2060
2061static int edma_of_parse_dt(struct device *dev, struct edma_soc_info *pdata)
2062{
2063 int ret = 0;
2064 struct property *prop;
2065 size_t sz;
2066 struct edma_rsv_info *rsv_info;
2067
2068 rsv_info = devm_kzalloc(dev, sizeof(struct edma_rsv_info), GFP_KERNEL);
2069 if (!rsv_info)
2070 return -ENOMEM;
2071 pdata->rsv = rsv_info;
2072
2073 prop = of_find_property(dev->of_node, "ti,edma-xbar-event-map", &sz);
2074 if (prop)
2075 ret = edma_xbar_event_map(dev, pdata, sz);
2076
2077 return ret;
2078}
2079
2080static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev)
2081{
2082 struct edma_soc_info *info;
2083 int ret;
2084
2085 info = devm_kzalloc(dev, sizeof(struct edma_soc_info), GFP_KERNEL);
2086 if (!info)
2087 return ERR_PTR(-ENOMEM);
2088
2089 ret = edma_of_parse_dt(dev, info);
2090 if (ret)
2091 return ERR_PTR(ret);
2092
2093 return info;
2094}
2095#else
2096static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev)
2097{
2098 return ERR_PTR(-EINVAL);
2099}
2100#endif
2101
Bill Pemberton463a1f82012-11-19 13:22:55 -05002102static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002103{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002104 struct edma_soc_info *info = pdev->dev.platform_data;
2105 s8 (*queue_priority_mapping)[2];
2106 int i, off, ln;
2107 const s16 (*rsv_chans)[2];
2108 const s16 (*rsv_slots)[2];
2109 const s16 (*xbar_chans)[2];
2110 int irq;
2111 char *irq_name;
2112 struct resource *mem;
2113 struct device_node *node = pdev->dev.of_node;
2114 struct device *dev = &pdev->dev;
2115 struct edma_cc *ecc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04002116 int ret;
2117
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002118 if (node) {
2119 info = edma_setup_info_from_dt(dev);
2120 if (IS_ERR(info)) {
2121 dev_err(dev, "failed to get DT data\n");
2122 return PTR_ERR(info);
2123 }
2124 }
2125
2126 if (!info)
2127 return -ENODEV;
2128
2129 pm_runtime_enable(dev);
2130 ret = pm_runtime_get_sync(dev);
2131 if (ret < 0) {
2132 dev_err(dev, "pm_runtime_get_sync() failed\n");
2133 return ret;
2134 }
2135
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002136 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
Russell King94cb0e72013-06-27 13:45:16 +01002137 if (ret)
2138 return ret;
2139
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002140 ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002141 if (!ecc) {
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002142 dev_err(dev, "Can't allocate controller\n");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002143 return -ENOMEM;
2144 }
2145
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002146 ecc->dev = dev;
2147 ecc->id = pdev->id;
2148 /* When booting with DT the pdev->id is -1 */
2149 if (ecc->id < 0)
2150 ecc->id = 0;
Peter Ujfalusica304fa2015-10-14 14:42:49 +03002151
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002152 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma3_cc");
2153 if (!mem) {
2154 dev_dbg(dev, "mem resource not found, using index 0\n");
2155 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2156 if (!mem) {
2157 dev_err(dev, "no mem resource?\n");
2158 return -ENODEV;
2159 }
2160 }
2161 ecc->base = devm_ioremap_resource(dev, mem);
2162 if (IS_ERR(ecc->base))
2163 return PTR_ERR(ecc->base);
Peter Ujfalusib2c843a2015-10-14 14:42:50 +03002164
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002165 platform_set_drvdata(pdev, ecc);
2166
2167 /* Get eDMA3 configuration from IP */
2168 ret = edma_setup_from_hw(dev, info, ecc);
2169 if (ret)
2170 return ret;
2171
Peter Ujfalusicb782052015-10-14 14:42:54 +03002172 /* Allocate memory based on the information we got from the IP */
2173 ecc->slave_chans = devm_kcalloc(dev, ecc->num_channels,
2174 sizeof(*ecc->slave_chans), GFP_KERNEL);
2175 if (!ecc->slave_chans)
2176 return -ENOMEM;
2177
2178 ecc->intr_data = devm_kcalloc(dev, ecc->num_channels,
2179 sizeof(*ecc->intr_data), GFP_KERNEL);
2180 if (!ecc->intr_data)
2181 return -ENOMEM;
2182
2183 ecc->edma_unused = devm_kcalloc(dev, BITS_TO_LONGS(ecc->num_channels),
2184 sizeof(unsigned long), GFP_KERNEL);
2185 if (!ecc->edma_unused)
2186 return -ENOMEM;
2187
2188 ecc->edma_inuse = devm_kcalloc(dev, BITS_TO_LONGS(ecc->num_slots),
2189 sizeof(unsigned long), GFP_KERNEL);
2190 if (!ecc->edma_inuse)
2191 return -ENOMEM;
2192
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002193 ecc->default_queue = info->default_queue;
2194
2195 for (i = 0; i < ecc->num_slots; i++)
2196 edma_write_slot(ecc, i, &dummy_paramset);
2197
2198 /* Mark all channels as unused */
2199 memset(ecc->edma_unused, 0xff, sizeof(ecc->edma_unused));
2200
2201 if (info->rsv) {
2202 /* Clear the reserved channels in unused list */
2203 rsv_chans = info->rsv->rsv_chans;
2204 if (rsv_chans) {
2205 for (i = 0; rsv_chans[i][0] != -1; i++) {
2206 off = rsv_chans[i][0];
2207 ln = rsv_chans[i][1];
2208 clear_bits(off, ln, ecc->edma_unused);
2209 }
2210 }
2211
2212 /* Set the reserved slots in inuse list */
2213 rsv_slots = info->rsv->rsv_slots;
2214 if (rsv_slots) {
2215 for (i = 0; rsv_slots[i][0] != -1; i++) {
2216 off = rsv_slots[i][0];
2217 ln = rsv_slots[i][1];
2218 set_bits(off, ln, ecc->edma_inuse);
2219 }
2220 }
2221 }
2222
2223 /* Clear the xbar mapped channels in unused list */
2224 xbar_chans = info->xbar_chans;
2225 if (xbar_chans) {
2226 for (i = 0; xbar_chans[i][1] != -1; i++) {
2227 off = xbar_chans[i][1];
2228 clear_bits(off, 1, ecc->edma_unused);
2229 }
2230 }
2231
2232 irq = platform_get_irq_byname(pdev, "edma3_ccint");
2233 if (irq < 0 && node)
2234 irq = irq_of_parse_and_map(node, 0);
2235
2236 if (irq >= 0) {
2237 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccint",
2238 dev_name(dev));
2239 ret = devm_request_irq(dev, irq, dma_irq_handler, 0, irq_name,
2240 ecc);
2241 if (ret) {
2242 dev_err(dev, "CCINT (%d) failed --> %d\n", irq, ret);
2243 return ret;
2244 }
2245 }
2246
2247 irq = platform_get_irq_byname(pdev, "edma3_ccerrint");
2248 if (irq < 0 && node)
2249 irq = irq_of_parse_and_map(node, 2);
2250
2251 if (irq >= 0) {
2252 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccerrint",
2253 dev_name(dev));
2254 ret = devm_request_irq(dev, irq, dma_ccerr_handler, 0, irq_name,
2255 ecc);
2256 if (ret) {
2257 dev_err(dev, "CCERRINT (%d) failed --> %d\n", irq, ret);
2258 return ret;
2259 }
2260 }
2261
2262 for (i = 0; i < ecc->num_channels; i++)
2263 edma_map_dmach_to_queue(ecc, i, info->default_queue);
2264
2265 queue_priority_mapping = info->queue_priority_mapping;
2266
2267 /* Event queue priority mapping */
2268 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2269 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2270 queue_priority_mapping[i][1]);
2271
2272 /* Map the channel to param entry if channel mapping logic exist */
2273 if (edma_read(ecc, EDMA_CCCFG) & CHMAP_EXIST)
2274 edma_direct_dmach_to_param_mapping(ecc);
2275
2276 for (i = 0; i < ecc->num_region; i++) {
2277 edma_write_array2(ecc, EDMA_DRAE, i, 0, 0x0);
2278 edma_write_array2(ecc, EDMA_DRAE, i, 1, 0x0);
2279 edma_write_array(ecc, EDMA_QRAE, i, 0x0);
2280 }
2281 ecc->info = info;
2282
2283 ecc->dummy_slot = edma_alloc_slot(ecc, EDMA_SLOT_ANY);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002284 if (ecc->dummy_slot < 0) {
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002285 dev_err(dev, "Can't allocate PaRAM dummy slot\n");
Peter Ujfalusi04d537d2014-07-31 13:12:37 +03002286 return ecc->dummy_slot;
Matt Porterc2dde5f2012-08-22 21:09:34 -04002287 }
2288
2289 dma_cap_zero(ecc->dma_slave.cap_mask);
2290 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
Peter Ujfalusi232b223d2014-04-14 14:42:00 +03002291 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05002292 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002293
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002294 edma_dma_init(ecc, &ecc->dma_slave, dev);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002295
2296 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
2297
2298 ret = dma_async_device_register(&ecc->dma_slave);
2299 if (ret)
2300 goto err_reg1;
2301
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002302 if (node)
2303 of_dma_controller_register(node, of_dma_xlate_by_chan_id,
Peter Ujfalusib2c843a2015-10-14 14:42:50 +03002304 &ecc->dma_slave);
Peter Ujfalusidc9b60552015-10-14 14:42:47 +03002305
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002306 dev_info(dev, "TI EDMA DMA engine driver\n");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002307
2308 return 0;
2309
2310err_reg1:
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002311 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002312 return ret;
2313}
2314
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08002315static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002316{
2317 struct device *dev = &pdev->dev;
2318 struct edma_cc *ecc = dev_get_drvdata(dev);
2319
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002320 if (dev->of_node)
2321 of_dma_controller_free(dev->of_node);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002322 dma_async_device_unregister(&ecc->dma_slave);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002323 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002324
2325 return 0;
2326}
2327
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002328#ifdef CONFIG_PM_SLEEP
2329static int edma_pm_resume(struct device *dev)
2330{
2331 struct edma_cc *ecc = dev_get_drvdata(dev);
2332 int i;
2333 s8 (*queue_priority_mapping)[2];
2334
2335 queue_priority_mapping = ecc->info->queue_priority_mapping;
2336
2337 /* Event queue priority mapping */
2338 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2339 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2340 queue_priority_mapping[i][1]);
2341
2342 /* Map the channel to param entry if channel mapping logic */
2343 if (edma_read(ecc, EDMA_CCCFG) & CHMAP_EXIST)
2344 edma_direct_dmach_to_param_mapping(ecc);
2345
2346 for (i = 0; i < ecc->num_channels; i++) {
2347 if (test_bit(i, ecc->edma_inuse)) {
2348 /* ensure access through shadow region 0 */
2349 edma_or_array2(ecc, EDMA_DRAE, 0, i >> 5,
2350 BIT(i & 0x1f));
2351
2352 edma_setup_interrupt(ecc, EDMA_CTLR_CHAN(ecc->id, i),
2353 ecc->intr_data[i].callback,
2354 ecc->intr_data[i].data);
2355 }
2356 }
2357
2358 return 0;
2359}
2360#endif
2361
2362static const struct dev_pm_ops edma_pm_ops = {
2363 SET_LATE_SYSTEM_SLEEP_PM_OPS(NULL, edma_pm_resume)
2364};
2365
Matt Porterc2dde5f2012-08-22 21:09:34 -04002366static struct platform_driver edma_driver = {
2367 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05002368 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002369 .driver = {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002370 .name = "edma",
2371 .pm = &edma_pm_ops,
2372 .of_match_table = edma_of_ids,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002373 },
2374};
2375
2376bool edma_filter_fn(struct dma_chan *chan, void *param)
2377{
2378 if (chan->device->dev->driver == &edma_driver.driver) {
2379 struct edma_chan *echan = to_edma_chan(chan);
2380 unsigned ch_req = *(unsigned *)param;
2381 return ch_req == echan->ch_num;
2382 }
2383 return false;
2384}
2385EXPORT_SYMBOL(edma_filter_fn);
2386
Matt Porterc2dde5f2012-08-22 21:09:34 -04002387static int edma_init(void)
2388{
Arnd Bergmann5305e4d2014-10-24 18:14:01 +02002389 return platform_driver_register(&edma_driver);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002390}
2391subsys_initcall(edma_init);
2392
2393static void __exit edma_exit(void)
2394{
Matt Porterc2dde5f2012-08-22 21:09:34 -04002395 platform_driver_unregister(&edma_driver);
2396}
2397module_exit(edma_exit);
2398
Josh Boyerd71505b2013-09-04 10:32:50 -04002399MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002400MODULE_DESCRIPTION("TI EDMA DMA engine driver");
2401MODULE_LICENSE("GPL v2");