]> Git Repo - linux.git/blob - drivers/dma/ti/edma.c
Linux 6.14-rc3
[linux.git] / drivers / dma / ti / edma.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TI EDMA DMA engine driver
4  *
5  * Copyright 2012 Texas Instruments
6  */
7
8 #include <linux/dmaengine.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/bitmap.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/of.h>
20 #include <linux/of_dma.h>
21 #include <linux/of_irq.h>
22 #include <linux/of_address.h>
23 #include <linux/pm_runtime.h>
24
25 #include <linux/platform_data/edma.h>
26
27 #include "../dmaengine.h"
28 #include "../virt-dma.h"
29
30 /* Offsets matching "struct edmacc_param" */
31 #define PARM_OPT                0x00
32 #define PARM_SRC                0x04
33 #define PARM_A_B_CNT            0x08
34 #define PARM_DST                0x0c
35 #define PARM_SRC_DST_BIDX       0x10
36 #define PARM_LINK_BCNTRLD       0x14
37 #define PARM_SRC_DST_CIDX       0x18
38 #define PARM_CCNT               0x1c
39
40 #define PARM_SIZE               0x20
41
42 /* Offsets for EDMA CC global channel registers and their shadows */
43 #define SH_ER                   0x00    /* 64 bits */
44 #define SH_ECR                  0x08    /* 64 bits */
45 #define SH_ESR                  0x10    /* 64 bits */
46 #define SH_CER                  0x18    /* 64 bits */
47 #define SH_EER                  0x20    /* 64 bits */
48 #define SH_EECR                 0x28    /* 64 bits */
49 #define SH_EESR                 0x30    /* 64 bits */
50 #define SH_SER                  0x38    /* 64 bits */
51 #define SH_SECR                 0x40    /* 64 bits */
52 #define SH_IER                  0x50    /* 64 bits */
53 #define SH_IECR                 0x58    /* 64 bits */
54 #define SH_IESR                 0x60    /* 64 bits */
55 #define SH_IPR                  0x68    /* 64 bits */
56 #define SH_ICR                  0x70    /* 64 bits */
57 #define SH_IEVAL                0x78
58 #define SH_QER                  0x80
59 #define SH_QEER                 0x84
60 #define SH_QEECR                0x88
61 #define SH_QEESR                0x8c
62 #define SH_QSER                 0x90
63 #define SH_QSECR                0x94
64 #define SH_SIZE                 0x200
65
66 /* Offsets for EDMA CC global registers */
67 #define EDMA_REV                0x0000
68 #define EDMA_CCCFG              0x0004
69 #define EDMA_QCHMAP             0x0200  /* 8 registers */
70 #define EDMA_DMAQNUM            0x0240  /* 8 registers (4 on OMAP-L1xx) */
71 #define EDMA_QDMAQNUM           0x0260
72 #define EDMA_QUETCMAP           0x0280
73 #define EDMA_QUEPRI             0x0284
74 #define EDMA_EMR                0x0300  /* 64 bits */
75 #define EDMA_EMCR               0x0308  /* 64 bits */
76 #define EDMA_QEMR               0x0310
77 #define EDMA_QEMCR              0x0314
78 #define EDMA_CCERR              0x0318
79 #define EDMA_CCERRCLR           0x031c
80 #define EDMA_EEVAL              0x0320
81 #define EDMA_DRAE               0x0340  /* 4 x 64 bits*/
82 #define EDMA_QRAE               0x0380  /* 4 registers */
83 #define EDMA_QUEEVTENTRY        0x0400  /* 2 x 16 registers */
84 #define EDMA_QSTAT              0x0600  /* 2 registers */
85 #define EDMA_QWMTHRA            0x0620
86 #define EDMA_QWMTHRB            0x0624
87 #define EDMA_CCSTAT             0x0640
88
89 #define EDMA_M                  0x1000  /* global channel registers */
90 #define EDMA_ECR                0x1008
91 #define EDMA_ECRH               0x100C
92 #define EDMA_SHADOW0            0x2000  /* 4 shadow regions */
93 #define EDMA_PARM               0x4000  /* PaRAM entries */
94
95 #define PARM_OFFSET(param_no)   (EDMA_PARM + ((param_no) << 5))
96
97 #define EDMA_DCHMAP             0x0100  /* 64 registers */
98
99 /* CCCFG register */
100 #define GET_NUM_DMACH(x)        (x & 0x7) /* bits 0-2 */
101 #define GET_NUM_QDMACH(x)       ((x & 0x70) >> 4) /* bits 4-6 */
102 #define GET_NUM_PAENTRY(x)      ((x & 0x7000) >> 12) /* bits 12-14 */
103 #define GET_NUM_EVQUE(x)        ((x & 0x70000) >> 16) /* bits 16-18 */
104 #define GET_NUM_REGN(x)         ((x & 0x300000) >> 20) /* bits 20-21 */
105 #define CHMAP_EXIST             BIT(24)
106
107 /* CCSTAT register */
108 #define EDMA_CCSTAT_ACTV        BIT(4)
109
110 /*
111  * Max of 20 segments per channel to conserve PaRAM slots
112  * Also note that MAX_NR_SG should be at least the no.of periods
113  * that are required for ASoC, otherwise DMA prep calls will
114  * fail. Today davinci-pcm is the only user of this driver and
115  * requires at least 17 slots, so we setup the default to 20.
116  */
117 #define MAX_NR_SG               20
118 #define EDMA_MAX_SLOTS          MAX_NR_SG
119 #define EDMA_DESCRIPTORS        16
120
121 #define EDMA_CHANNEL_ANY                -1      /* for edma_alloc_channel() */
122 #define EDMA_SLOT_ANY                   -1      /* for edma_alloc_slot() */
123 #define EDMA_CONT_PARAMS_ANY             1001
124 #define EDMA_CONT_PARAMS_FIXED_EXACT     1002
125 #define EDMA_CONT_PARAMS_FIXED_NOT_EXACT 1003
126
127 /*
128  * 64bit array registers are split into two 32bit registers:
129  * reg0: channel/event 0-31
130  * reg1: channel/event 32-63
131  *
132  * bit 5 in the channel number tells the array index (0/1)
133  * bit 0-4 (0x1f) is the bit offset within the register
134  */
135 #define EDMA_REG_ARRAY_INDEX(channel)   ((channel) >> 5)
136 #define EDMA_CHANNEL_BIT(channel)       (BIT((channel) & 0x1f))
137
138 /* PaRAM slots are laid out like this */
139 struct edmacc_param {
140         u32 opt;
141         u32 src;
142         u32 a_b_cnt;
143         u32 dst;
144         u32 src_dst_bidx;
145         u32 link_bcntrld;
146         u32 src_dst_cidx;
147         u32 ccnt;
148 } __packed;
149
150 /* fields in edmacc_param.opt */
151 #define SAM             BIT(0)
152 #define DAM             BIT(1)
153 #define SYNCDIM         BIT(2)
154 #define STATIC          BIT(3)
155 #define EDMA_FWID       (0x07 << 8)
156 #define TCCMODE         BIT(11)
157 #define EDMA_TCC(t)     ((t) << 12)
158 #define TCINTEN         BIT(20)
159 #define ITCINTEN        BIT(21)
160 #define TCCHEN          BIT(22)
161 #define ITCCHEN         BIT(23)
162
163 struct edma_pset {
164         u32                             len;
165         dma_addr_t                      addr;
166         struct edmacc_param             param;
167 };
168
169 struct edma_desc {
170         struct virt_dma_desc            vdesc;
171         struct list_head                node;
172         enum dma_transfer_direction     direction;
173         int                             cyclic;
174         bool                            polled;
175         int                             absync;
176         int                             pset_nr;
177         struct edma_chan                *echan;
178         int                             processed;
179
180         /*
181          * The following 4 elements are used for residue accounting.
182          *
183          * - processed_stat: the number of SG elements we have traversed
184          * so far to cover accounting. This is updated directly to processed
185          * during edma_callback and is always <= processed, because processed
186          * refers to the number of pending transfer (programmed to EDMA
187          * controller), where as processed_stat tracks number of transfers
188          * accounted for so far.
189          *
190          * - residue: The amount of bytes we have left to transfer for this desc
191          *
192          * - residue_stat: The residue in bytes of data we have covered
193          * so far for accounting. This is updated directly to residue
194          * during callbacks to keep it current.
195          *
196          * - sg_len: Tracks the length of the current intermediate transfer,
197          * this is required to update the residue during intermediate transfer
198          * completion callback.
199          */
200         int                             processed_stat;
201         u32                             sg_len;
202         u32                             residue;
203         u32                             residue_stat;
204
205         struct edma_pset                pset[] __counted_by(pset_nr);
206 };
207
208 struct edma_cc;
209
210 struct edma_tc {
211         u16                             id;
212 };
213
214 struct edma_chan {
215         struct virt_dma_chan            vchan;
216         struct list_head                node;
217         struct edma_desc                *edesc;
218         struct edma_cc                  *ecc;
219         struct edma_tc                  *tc;
220         int                             ch_num;
221         bool                            alloced;
222         bool                            hw_triggered;
223         int                             slot[EDMA_MAX_SLOTS];
224         int                             missed;
225         struct dma_slave_config         cfg;
226 };
227
228 struct edma_cc {
229         struct device                   *dev;
230         struct edma_soc_info            *info;
231         void __iomem                    *base;
232         int                             id;
233         bool                            legacy_mode;
234
235         /* eDMA3 resource information */
236         unsigned                        num_channels;
237         unsigned                        num_qchannels;
238         unsigned                        num_region;
239         unsigned                        num_slots;
240         unsigned                        num_tc;
241         bool                            chmap_exist;
242         enum dma_event_q                default_queue;
243
244         unsigned int                    ccint;
245         unsigned int                    ccerrint;
246
247         /*
248          * The slot_inuse bit for each PaRAM slot is clear unless the slot is
249          * in use by Linux or if it is allocated to be used by DSP.
250          */
251         unsigned long *slot_inuse;
252
253         /*
254          * For tracking reserved channels used by DSP.
255          * If the bit is cleared, the channel is allocated to be used by DSP
256          * and Linux must not touch it.
257          */
258         unsigned long *channels_mask;
259
260         struct dma_device               dma_slave;
261         struct dma_device               *dma_memcpy;
262         struct edma_chan                *slave_chans;
263         struct edma_tc                  *tc_list;
264         int                             dummy_slot;
265 };
266
267 /* dummy param set used to (re)initialize parameter RAM slots */
268 static const struct edmacc_param dummy_paramset = {
269         .link_bcntrld = 0xffff,
270         .ccnt = 1,
271 };
272
273 #define EDMA_BINDING_LEGACY     0
274 #define EDMA_BINDING_TPCC       1
275 static const u32 edma_binding_type[] = {
276         [EDMA_BINDING_LEGACY] = EDMA_BINDING_LEGACY,
277         [EDMA_BINDING_TPCC] = EDMA_BINDING_TPCC,
278 };
279
280 static const struct of_device_id edma_of_ids[] = {
281         {
282                 .compatible = "ti,edma3",
283                 .data = &edma_binding_type[EDMA_BINDING_LEGACY],
284         },
285         {
286                 .compatible = "ti,edma3-tpcc",
287                 .data = &edma_binding_type[EDMA_BINDING_TPCC],
288         },
289         {}
290 };
291 MODULE_DEVICE_TABLE(of, edma_of_ids);
292
293 static const struct of_device_id edma_tptc_of_ids[] = {
294         { .compatible = "ti,edma3-tptc", },
295         {}
296 };
297 MODULE_DEVICE_TABLE(of, edma_tptc_of_ids);
298
299 static inline unsigned int edma_read(struct edma_cc *ecc, int offset)
300 {
301         return (unsigned int)__raw_readl(ecc->base + offset);
302 }
303
304 static inline void edma_write(struct edma_cc *ecc, int offset, int val)
305 {
306         __raw_writel(val, ecc->base + offset);
307 }
308
309 static inline void edma_modify(struct edma_cc *ecc, int offset, unsigned and,
310                                unsigned or)
311 {
312         unsigned val = edma_read(ecc, offset);
313
314         val &= and;
315         val |= or;
316         edma_write(ecc, offset, val);
317 }
318
319 static inline void edma_or(struct edma_cc *ecc, int offset, unsigned or)
320 {
321         unsigned val = edma_read(ecc, offset);
322
323         val |= or;
324         edma_write(ecc, offset, val);
325 }
326
327 static inline unsigned int edma_read_array(struct edma_cc *ecc, int offset,
328                                            int i)
329 {
330         return edma_read(ecc, offset + (i << 2));
331 }
332
333 static inline void edma_write_array(struct edma_cc *ecc, int offset, int i,
334                                     unsigned val)
335 {
336         edma_write(ecc, offset + (i << 2), val);
337 }
338
339 static inline void edma_modify_array(struct edma_cc *ecc, int offset, int i,
340                                      unsigned and, unsigned or)
341 {
342         edma_modify(ecc, offset + (i << 2), and, or);
343 }
344
345 static inline void edma_or_array2(struct edma_cc *ecc, int offset, int i, int j,
346                                   unsigned or)
347 {
348         edma_or(ecc, offset + ((i * 2 + j) << 2), or);
349 }
350
351 static inline void edma_write_array2(struct edma_cc *ecc, int offset, int i,
352                                      int j, unsigned val)
353 {
354         edma_write(ecc, offset + ((i * 2 + j) << 2), val);
355 }
356
357 static inline unsigned int edma_shadow0_read_array(struct edma_cc *ecc,
358                                                    int offset, int i)
359 {
360         return edma_read(ecc, EDMA_SHADOW0 + offset + (i << 2));
361 }
362
363 static inline void edma_shadow0_write(struct edma_cc *ecc, int offset,
364                                       unsigned val)
365 {
366         edma_write(ecc, EDMA_SHADOW0 + offset, val);
367 }
368
369 static inline void edma_shadow0_write_array(struct edma_cc *ecc, int offset,
370                                             int i, unsigned val)
371 {
372         edma_write(ecc, EDMA_SHADOW0 + offset + (i << 2), val);
373 }
374
375 static inline void edma_param_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
381 static void edma_assign_priority_to_queue(struct edma_cc *ecc, int queue_no,
382                                           int priority)
383 {
384         int bit = queue_no * 4;
385
386         edma_modify(ecc, EDMA_QUEPRI, ~(0x7 << bit), ((priority & 0x7) << bit));
387 }
388
389 static void edma_set_chmap(struct edma_chan *echan, int slot)
390 {
391         struct edma_cc *ecc = echan->ecc;
392         int channel = EDMA_CHAN_SLOT(echan->ch_num);
393
394         if (ecc->chmap_exist) {
395                 slot = EDMA_CHAN_SLOT(slot);
396                 edma_write_array(ecc, EDMA_DCHMAP, channel, (slot << 5));
397         }
398 }
399
400 static void edma_setup_interrupt(struct edma_chan *echan, bool enable)
401 {
402         struct edma_cc *ecc = echan->ecc;
403         int channel = EDMA_CHAN_SLOT(echan->ch_num);
404         int idx = EDMA_REG_ARRAY_INDEX(channel);
405         int ch_bit = EDMA_CHANNEL_BIT(channel);
406
407         if (enable) {
408                 edma_shadow0_write_array(ecc, SH_ICR, idx, ch_bit);
409                 edma_shadow0_write_array(ecc, SH_IESR, idx, ch_bit);
410         } else {
411                 edma_shadow0_write_array(ecc, SH_IECR, idx, ch_bit);
412         }
413 }
414
415 /*
416  * paRAM slot management functions
417  */
418 static void edma_write_slot(struct edma_cc *ecc, unsigned slot,
419                             const struct edmacc_param *param)
420 {
421         slot = EDMA_CHAN_SLOT(slot);
422         if (slot >= ecc->num_slots)
423                 return;
424         memcpy_toio(ecc->base + PARM_OFFSET(slot), param, PARM_SIZE);
425 }
426
427 static int edma_read_slot(struct edma_cc *ecc, unsigned slot,
428                            struct edmacc_param *param)
429 {
430         slot = EDMA_CHAN_SLOT(slot);
431         if (slot >= ecc->num_slots)
432                 return -EINVAL;
433         memcpy_fromio(param, ecc->base + PARM_OFFSET(slot), PARM_SIZE);
434
435         return 0;
436 }
437
438 /**
439  * edma_alloc_slot - allocate DMA parameter RAM
440  * @ecc: pointer to edma_cc struct
441  * @slot: specific slot to allocate; negative for "any unused slot"
442  *
443  * This allocates a parameter RAM slot, initializing it to hold a
444  * dummy transfer.  Slots allocated using this routine have not been
445  * mapped to a hardware DMA channel, and will normally be used by
446  * linking to them from a slot associated with a DMA channel.
447  *
448  * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
449  * slots may be allocated on behalf of DSP firmware.
450  *
451  * Returns the number of the slot, else negative errno.
452  */
453 static int edma_alloc_slot(struct edma_cc *ecc, int slot)
454 {
455         if (slot >= 0) {
456                 slot = EDMA_CHAN_SLOT(slot);
457                 /* Requesting entry paRAM slot for a HW triggered channel. */
458                 if (ecc->chmap_exist && slot < ecc->num_channels)
459                         slot = EDMA_SLOT_ANY;
460         }
461
462         if (slot < 0) {
463                 if (ecc->chmap_exist)
464                         slot = 0;
465                 else
466                         slot = ecc->num_channels;
467                 for (;;) {
468                         slot = find_next_zero_bit(ecc->slot_inuse,
469                                                   ecc->num_slots,
470                                                   slot);
471                         if (slot == ecc->num_slots)
472                                 return -ENOMEM;
473                         if (!test_and_set_bit(slot, ecc->slot_inuse))
474                                 break;
475                 }
476         } else if (slot >= ecc->num_slots) {
477                 return -EINVAL;
478         } else if (test_and_set_bit(slot, ecc->slot_inuse)) {
479                 return -EBUSY;
480         }
481
482         edma_write_slot(ecc, slot, &dummy_paramset);
483
484         return EDMA_CTLR_CHAN(ecc->id, slot);
485 }
486
487 static void edma_free_slot(struct edma_cc *ecc, unsigned slot)
488 {
489         slot = EDMA_CHAN_SLOT(slot);
490         if (slot >= ecc->num_slots)
491                 return;
492
493         edma_write_slot(ecc, slot, &dummy_paramset);
494         clear_bit(slot, ecc->slot_inuse);
495 }
496
497 /**
498  * edma_link - link one parameter RAM slot to another
499  * @ecc: pointer to edma_cc struct
500  * @from: parameter RAM slot originating the link
501  * @to: parameter RAM slot which is the link target
502  *
503  * The originating slot should not be part of any active DMA transfer.
504  */
505 static void edma_link(struct edma_cc *ecc, unsigned from, unsigned to)
506 {
507         if (unlikely(EDMA_CTLR(from) != EDMA_CTLR(to)))
508                 dev_warn(ecc->dev, "Ignoring eDMA instance for linking\n");
509
510         from = EDMA_CHAN_SLOT(from);
511         to = EDMA_CHAN_SLOT(to);
512         if (from >= ecc->num_slots || to >= ecc->num_slots)
513                 return;
514
515         edma_param_modify(ecc, PARM_LINK_BCNTRLD, from, 0xffff0000,
516                           PARM_OFFSET(to));
517 }
518
519 /**
520  * edma_get_position - returns the current transfer point
521  * @ecc: pointer to edma_cc struct
522  * @slot: parameter RAM slot being examined
523  * @dst:  true selects the dest position, false the source
524  *
525  * Returns the position of the current active slot
526  */
527 static dma_addr_t edma_get_position(struct edma_cc *ecc, unsigned slot,
528                                     bool dst)
529 {
530         u32 offs;
531
532         slot = EDMA_CHAN_SLOT(slot);
533         offs = PARM_OFFSET(slot);
534         offs += dst ? PARM_DST : PARM_SRC;
535
536         return edma_read(ecc, offs);
537 }
538
539 /*
540  * Channels with event associations will be triggered by their hardware
541  * events, and channels without such associations will be triggered by
542  * software.  (At this writing there is no interface for using software
543  * triggers except with channels that don't support hardware triggers.)
544  */
545 static void edma_start(struct edma_chan *echan)
546 {
547         struct edma_cc *ecc = echan->ecc;
548         int channel = EDMA_CHAN_SLOT(echan->ch_num);
549         int idx = EDMA_REG_ARRAY_INDEX(channel);
550         int ch_bit = EDMA_CHANNEL_BIT(channel);
551
552         if (!echan->hw_triggered) {
553                 /* EDMA channels without event association */
554                 dev_dbg(ecc->dev, "ESR%d %08x\n", idx,
555                         edma_shadow0_read_array(ecc, SH_ESR, idx));
556                 edma_shadow0_write_array(ecc, SH_ESR, idx, ch_bit);
557         } else {
558                 /* EDMA channel with event association */
559                 dev_dbg(ecc->dev, "ER%d %08x\n", idx,
560                         edma_shadow0_read_array(ecc, SH_ER, idx));
561                 /* Clear any pending event or error */
562                 edma_write_array(ecc, EDMA_ECR, idx, ch_bit);
563                 edma_write_array(ecc, EDMA_EMCR, idx, ch_bit);
564                 /* Clear any SER */
565                 edma_shadow0_write_array(ecc, SH_SECR, idx, ch_bit);
566                 edma_shadow0_write_array(ecc, SH_EESR, idx, ch_bit);
567                 dev_dbg(ecc->dev, "EER%d %08x\n", idx,
568                         edma_shadow0_read_array(ecc, SH_EER, idx));
569         }
570 }
571
572 static void edma_stop(struct edma_chan *echan)
573 {
574         struct edma_cc *ecc = echan->ecc;
575         int channel = EDMA_CHAN_SLOT(echan->ch_num);
576         int idx = EDMA_REG_ARRAY_INDEX(channel);
577         int ch_bit = EDMA_CHANNEL_BIT(channel);
578
579         edma_shadow0_write_array(ecc, SH_EECR, idx, ch_bit);
580         edma_shadow0_write_array(ecc, SH_ECR, idx, ch_bit);
581         edma_shadow0_write_array(ecc, SH_SECR, idx, ch_bit);
582         edma_write_array(ecc, EDMA_EMCR, idx, ch_bit);
583
584         /* clear possibly pending completion interrupt */
585         edma_shadow0_write_array(ecc, SH_ICR, idx, ch_bit);
586
587         dev_dbg(ecc->dev, "EER%d %08x\n", idx,
588                 edma_shadow0_read_array(ecc, SH_EER, idx));
589
590         /* REVISIT:  consider guarding against inappropriate event
591          * chaining by overwriting with dummy_paramset.
592          */
593 }
594
595 /*
596  * Temporarily disable EDMA hardware events on the specified channel,
597  * preventing them from triggering new transfers
598  */
599 static void edma_pause(struct edma_chan *echan)
600 {
601         int channel = EDMA_CHAN_SLOT(echan->ch_num);
602
603         edma_shadow0_write_array(echan->ecc, SH_EECR,
604                                  EDMA_REG_ARRAY_INDEX(channel),
605                                  EDMA_CHANNEL_BIT(channel));
606 }
607
608 /* Re-enable EDMA hardware events on the specified channel.  */
609 static void edma_resume(struct edma_chan *echan)
610 {
611         int channel = EDMA_CHAN_SLOT(echan->ch_num);
612
613         edma_shadow0_write_array(echan->ecc, SH_EESR,
614                                  EDMA_REG_ARRAY_INDEX(channel),
615                                  EDMA_CHANNEL_BIT(channel));
616 }
617
618 static void edma_trigger_channel(struct edma_chan *echan)
619 {
620         struct edma_cc *ecc = echan->ecc;
621         int channel = EDMA_CHAN_SLOT(echan->ch_num);
622         int idx = EDMA_REG_ARRAY_INDEX(channel);
623         int ch_bit = EDMA_CHANNEL_BIT(channel);
624
625         edma_shadow0_write_array(ecc, SH_ESR, idx, ch_bit);
626
627         dev_dbg(ecc->dev, "ESR%d %08x\n", idx,
628                 edma_shadow0_read_array(ecc, SH_ESR, idx));
629 }
630
631 static void edma_clean_channel(struct edma_chan *echan)
632 {
633         struct edma_cc *ecc = echan->ecc;
634         int channel = EDMA_CHAN_SLOT(echan->ch_num);
635         int idx = EDMA_REG_ARRAY_INDEX(channel);
636         int ch_bit = EDMA_CHANNEL_BIT(channel);
637
638         dev_dbg(ecc->dev, "EMR%d %08x\n", idx,
639                 edma_read_array(ecc, EDMA_EMR, idx));
640         edma_shadow0_write_array(ecc, SH_ECR, idx, ch_bit);
641         /* Clear the corresponding EMR bits */
642         edma_write_array(ecc, EDMA_EMCR, idx, ch_bit);
643         /* Clear any SER */
644         edma_shadow0_write_array(ecc, SH_SECR, idx, ch_bit);
645         edma_write(ecc, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
646 }
647
648 /* Move channel to a specific event queue */
649 static void edma_assign_channel_eventq(struct edma_chan *echan,
650                                        enum dma_event_q eventq_no)
651 {
652         struct edma_cc *ecc = echan->ecc;
653         int channel = EDMA_CHAN_SLOT(echan->ch_num);
654         int bit = (channel & 0x7) * 4;
655
656         /* default to low priority queue */
657         if (eventq_no == EVENTQ_DEFAULT)
658                 eventq_no = ecc->default_queue;
659         if (eventq_no >= ecc->num_tc)
660                 return;
661
662         eventq_no &= 7;
663         edma_modify_array(ecc, EDMA_DMAQNUM, (channel >> 3), ~(0x7 << bit),
664                           eventq_no << bit);
665 }
666
667 static int edma_alloc_channel(struct edma_chan *echan,
668                               enum dma_event_q eventq_no)
669 {
670         struct edma_cc *ecc = echan->ecc;
671         int channel = EDMA_CHAN_SLOT(echan->ch_num);
672
673         if (!test_bit(echan->ch_num, ecc->channels_mask)) {
674                 dev_err(ecc->dev, "Channel%d is reserved, can not be used!\n",
675                         echan->ch_num);
676                 return -EINVAL;
677         }
678
679         /* ensure access through shadow region 0 */
680         edma_or_array2(ecc, EDMA_DRAE, 0, EDMA_REG_ARRAY_INDEX(channel),
681                        EDMA_CHANNEL_BIT(channel));
682
683         /* ensure no events are pending */
684         edma_stop(echan);
685
686         edma_setup_interrupt(echan, true);
687
688         edma_assign_channel_eventq(echan, eventq_no);
689
690         return 0;
691 }
692
693 static void edma_free_channel(struct edma_chan *echan)
694 {
695         /* ensure no events are pending */
696         edma_stop(echan);
697         /* REVISIT should probably take out of shadow region 0 */
698         edma_setup_interrupt(echan, false);
699 }
700
701 static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
702 {
703         return container_of(c, struct edma_chan, vchan.chan);
704 }
705
706 static inline struct edma_desc *to_edma_desc(struct dma_async_tx_descriptor *tx)
707 {
708         return container_of(tx, struct edma_desc, vdesc.tx);
709 }
710
711 static void edma_desc_free(struct virt_dma_desc *vdesc)
712 {
713         kfree(container_of(vdesc, struct edma_desc, vdesc));
714 }
715
716 /* Dispatch a queued descriptor to the controller (caller holds lock) */
717 static void edma_execute(struct edma_chan *echan)
718 {
719         struct edma_cc *ecc = echan->ecc;
720         struct virt_dma_desc *vdesc;
721         struct edma_desc *edesc;
722         struct device *dev = echan->vchan.chan.device->dev;
723         int i, j, left, nslots;
724
725         if (!echan->edesc) {
726                 /* Setup is needed for the first transfer */
727                 vdesc = vchan_next_desc(&echan->vchan);
728                 if (!vdesc)
729                         return;
730                 list_del(&vdesc->node);
731                 echan->edesc = to_edma_desc(&vdesc->tx);
732         }
733
734         edesc = echan->edesc;
735
736         /* Find out how many left */
737         left = edesc->pset_nr - edesc->processed;
738         nslots = min(MAX_NR_SG, left);
739         edesc->sg_len = 0;
740
741         /* Write descriptor PaRAM set(s) */
742         for (i = 0; i < nslots; i++) {
743                 j = i + edesc->processed;
744                 edma_write_slot(ecc, echan->slot[i], &edesc->pset[j].param);
745                 edesc->sg_len += edesc->pset[j].len;
746                 dev_vdbg(dev,
747                          "\n pset[%d]:\n"
748                          "  chnum\t%d\n"
749                          "  slot\t%d\n"
750                          "  opt\t%08x\n"
751                          "  src\t%08x\n"
752                          "  dst\t%08x\n"
753                          "  abcnt\t%08x\n"
754                          "  ccnt\t%08x\n"
755                          "  bidx\t%08x\n"
756                          "  cidx\t%08x\n"
757                          "  lkrld\t%08x\n",
758                          j, echan->ch_num, echan->slot[i],
759                          edesc->pset[j].param.opt,
760                          edesc->pset[j].param.src,
761                          edesc->pset[j].param.dst,
762                          edesc->pset[j].param.a_b_cnt,
763                          edesc->pset[j].param.ccnt,
764                          edesc->pset[j].param.src_dst_bidx,
765                          edesc->pset[j].param.src_dst_cidx,
766                          edesc->pset[j].param.link_bcntrld);
767                 /* Link to the previous slot if not the last set */
768                 if (i != (nslots - 1))
769                         edma_link(ecc, echan->slot[i], echan->slot[i + 1]);
770         }
771
772         edesc->processed += nslots;
773
774         /*
775          * If this is either the last set in a set of SG-list transactions
776          * then setup a link to the dummy slot, this results in all future
777          * events being absorbed and that's OK because we're done
778          */
779         if (edesc->processed == edesc->pset_nr) {
780                 if (edesc->cyclic)
781                         edma_link(ecc, echan->slot[nslots - 1], echan->slot[1]);
782                 else
783                         edma_link(ecc, echan->slot[nslots - 1],
784                                   echan->ecc->dummy_slot);
785         }
786
787         if (echan->missed) {
788                 /*
789                  * This happens due to setup times between intermediate
790                  * transfers in long SG lists which have to be broken up into
791                  * transfers of MAX_NR_SG
792                  */
793                 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
794                 edma_clean_channel(echan);
795                 edma_stop(echan);
796                 edma_start(echan);
797                 edma_trigger_channel(echan);
798                 echan->missed = 0;
799         } else if (edesc->processed <= MAX_NR_SG) {
800                 dev_dbg(dev, "first transfer starting on channel %d\n",
801                         echan->ch_num);
802                 edma_start(echan);
803         } else {
804                 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
805                         echan->ch_num, edesc->processed);
806                 edma_resume(echan);
807         }
808 }
809
810 static int edma_terminate_all(struct dma_chan *chan)
811 {
812         struct edma_chan *echan = to_edma_chan(chan);
813         unsigned long flags;
814         LIST_HEAD(head);
815
816         spin_lock_irqsave(&echan->vchan.lock, flags);
817
818         /*
819          * Stop DMA activity: we assume the callback will not be called
820          * after edma_dma() returns (even if it does, it will see
821          * echan->edesc is NULL and exit.)
822          */
823         if (echan->edesc) {
824                 edma_stop(echan);
825                 /* Move the cyclic channel back to default queue */
826                 if (!echan->tc && echan->edesc->cyclic)
827                         edma_assign_channel_eventq(echan, EVENTQ_DEFAULT);
828
829                 vchan_terminate_vdesc(&echan->edesc->vdesc);
830                 echan->edesc = NULL;
831         }
832
833         vchan_get_all_descriptors(&echan->vchan, &head);
834         spin_unlock_irqrestore(&echan->vchan.lock, flags);
835         vchan_dma_desc_free_list(&echan->vchan, &head);
836
837         return 0;
838 }
839
840 static void edma_synchronize(struct dma_chan *chan)
841 {
842         struct edma_chan *echan = to_edma_chan(chan);
843
844         vchan_synchronize(&echan->vchan);
845 }
846
847 static int edma_slave_config(struct dma_chan *chan,
848         struct dma_slave_config *cfg)
849 {
850         struct edma_chan *echan = to_edma_chan(chan);
851
852         if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
853             cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
854                 return -EINVAL;
855
856         if (cfg->src_maxburst > chan->device->max_burst ||
857             cfg->dst_maxburst > chan->device->max_burst)
858                 return -EINVAL;
859
860         memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
861
862         return 0;
863 }
864
865 static int edma_dma_pause(struct dma_chan *chan)
866 {
867         struct edma_chan *echan = to_edma_chan(chan);
868
869         if (!echan->edesc)
870                 return -EINVAL;
871
872         edma_pause(echan);
873         return 0;
874 }
875
876 static int edma_dma_resume(struct dma_chan *chan)
877 {
878         struct edma_chan *echan = to_edma_chan(chan);
879
880         edma_resume(echan);
881         return 0;
882 }
883
884 /*
885  * A PaRAM set configuration abstraction used by other modes
886  * @chan: Channel who's PaRAM set we're configuring
887  * @pset: PaRAM set to initialize and setup.
888  * @src_addr: Source address of the DMA
889  * @dst_addr: Destination address of the DMA
890  * @burst: In units of dev_width, how much to send
891  * @dev_width: How much is the dev_width
892  * @dma_length: Total length of the DMA transfer
893  * @direction: Direction of the transfer
894  */
895 static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
896                             dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
897                             unsigned int acnt, unsigned int dma_length,
898                             enum dma_transfer_direction direction)
899 {
900         struct edma_chan *echan = to_edma_chan(chan);
901         struct device *dev = chan->device->dev;
902         struct edmacc_param *param = &epset->param;
903         int bcnt, ccnt, cidx;
904         int src_bidx, dst_bidx, src_cidx, dst_cidx;
905         int absync;
906
907         /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
908         if (!burst)
909                 burst = 1;
910         /*
911          * If the maxburst is equal to the fifo width, use
912          * A-synced transfers. This allows for large contiguous
913          * buffer transfers using only one PaRAM set.
914          */
915         if (burst == 1) {
916                 /*
917                  * For the A-sync case, bcnt and ccnt are the remainder
918                  * and quotient respectively of the division of:
919                  * (dma_length / acnt) by (SZ_64K -1). This is so
920                  * that in case bcnt over flows, we have ccnt to use.
921                  * Note: In A-sync transfer only, bcntrld is used, but it
922                  * only applies for sg_dma_len(sg) >= SZ_64K.
923                  * In this case, the best way adopted is- bccnt for the
924                  * first frame will be the remainder below. Then for
925                  * every successive frame, bcnt will be SZ_64K-1. This
926                  * is assured as bcntrld = 0xffff in end of function.
927                  */
928                 absync = false;
929                 ccnt = dma_length / acnt / (SZ_64K - 1);
930                 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
931                 /*
932                  * If bcnt is non-zero, we have a remainder and hence an
933                  * extra frame to transfer, so increment ccnt.
934                  */
935                 if (bcnt)
936                         ccnt++;
937                 else
938                         bcnt = SZ_64K - 1;
939                 cidx = acnt;
940         } else {
941                 /*
942                  * If maxburst is greater than the fifo address_width,
943                  * use AB-synced transfers where A count is the fifo
944                  * address_width and B count is the maxburst. In this
945                  * case, we are limited to transfers of C count frames
946                  * of (address_width * maxburst) where C count is limited
947                  * to SZ_64K-1. This places an upper bound on the length
948                  * of an SG segment that can be handled.
949                  */
950                 absync = true;
951                 bcnt = burst;
952                 ccnt = dma_length / (acnt * bcnt);
953                 if (ccnt > (SZ_64K - 1)) {
954                         dev_err(dev, "Exceeded max SG segment size\n");
955                         return -EINVAL;
956                 }
957                 cidx = acnt * bcnt;
958         }
959
960         epset->len = dma_length;
961
962         if (direction == DMA_MEM_TO_DEV) {
963                 src_bidx = acnt;
964                 src_cidx = cidx;
965                 dst_bidx = 0;
966                 dst_cidx = 0;
967                 epset->addr = src_addr;
968         } else if (direction == DMA_DEV_TO_MEM)  {
969                 src_bidx = 0;
970                 src_cidx = 0;
971                 dst_bidx = acnt;
972                 dst_cidx = cidx;
973                 epset->addr = dst_addr;
974         } else if (direction == DMA_MEM_TO_MEM)  {
975                 src_bidx = acnt;
976                 src_cidx = cidx;
977                 dst_bidx = acnt;
978                 dst_cidx = cidx;
979                 epset->addr = src_addr;
980         } else {
981                 dev_err(dev, "%s: direction not implemented yet\n", __func__);
982                 return -EINVAL;
983         }
984
985         param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
986         /* Configure A or AB synchronized transfers */
987         if (absync)
988                 param->opt |= SYNCDIM;
989
990         param->src = src_addr;
991         param->dst = dst_addr;
992
993         param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
994         param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
995
996         param->a_b_cnt = bcnt << 16 | acnt;
997         param->ccnt = ccnt;
998         /*
999          * Only time when (bcntrld) auto reload is required is for
1000          * A-sync case, and in this case, a requirement of reload value
1001          * of SZ_64K-1 only is assured. 'link' is initially set to NULL
1002          * and then later will be populated by edma_execute.
1003          */
1004         param->link_bcntrld = 0xffffffff;
1005         return absync;
1006 }
1007
1008 static struct dma_async_tx_descriptor *edma_prep_slave_sg(
1009         struct dma_chan *chan, struct scatterlist *sgl,
1010         unsigned int sg_len, enum dma_transfer_direction direction,
1011         unsigned long tx_flags, void *context)
1012 {
1013         struct edma_chan *echan = to_edma_chan(chan);
1014         struct device *dev = chan->device->dev;
1015         struct edma_desc *edesc;
1016         dma_addr_t src_addr = 0, dst_addr = 0;
1017         enum dma_slave_buswidth dev_width;
1018         u32 burst;
1019         struct scatterlist *sg;
1020         int i, nslots, ret;
1021
1022         if (unlikely(!echan || !sgl || !sg_len))
1023                 return NULL;
1024
1025         if (direction == DMA_DEV_TO_MEM) {
1026                 src_addr = echan->cfg.src_addr;
1027                 dev_width = echan->cfg.src_addr_width;
1028                 burst = echan->cfg.src_maxburst;
1029         } else if (direction == DMA_MEM_TO_DEV) {
1030                 dst_addr = echan->cfg.dst_addr;
1031                 dev_width = echan->cfg.dst_addr_width;
1032                 burst = echan->cfg.dst_maxburst;
1033         } else {
1034                 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
1035                 return NULL;
1036         }
1037
1038         if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
1039                 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
1040                 return NULL;
1041         }
1042
1043         edesc = kzalloc(struct_size(edesc, pset, sg_len), GFP_ATOMIC);
1044         if (!edesc)
1045                 return NULL;
1046
1047         edesc->pset_nr = sg_len;
1048         edesc->residue = 0;
1049         edesc->direction = direction;
1050         edesc->echan = echan;
1051
1052         /* Allocate a PaRAM slot, if needed */
1053         nslots = min_t(unsigned, MAX_NR_SG, sg_len);
1054
1055         for (i = 0; i < nslots; i++) {
1056                 if (echan->slot[i] < 0) {
1057                         echan->slot[i] =
1058                                 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
1059                         if (echan->slot[i] < 0) {
1060                                 kfree(edesc);
1061                                 dev_err(dev, "%s: Failed to allocate slot\n",
1062                                         __func__);
1063                                 return NULL;
1064                         }
1065                 }
1066         }
1067
1068         /* Configure PaRAM sets for each SG */
1069         for_each_sg(sgl, sg, sg_len, i) {
1070                 /* Get address for each SG */
1071                 if (direction == DMA_DEV_TO_MEM)
1072                         dst_addr = sg_dma_address(sg);
1073                 else
1074                         src_addr = sg_dma_address(sg);
1075
1076                 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1077                                        dst_addr, burst, dev_width,
1078                                        sg_dma_len(sg), direction);
1079                 if (ret < 0) {
1080                         kfree(edesc);
1081                         return NULL;
1082                 }
1083
1084                 edesc->absync = ret;
1085                 edesc->residue += sg_dma_len(sg);
1086
1087                 if (i == sg_len - 1)
1088                         /* Enable completion interrupt */
1089                         edesc->pset[i].param.opt |= TCINTEN;
1090                 else if (!((i+1) % MAX_NR_SG))
1091                         /*
1092                          * Enable early completion interrupt for the
1093                          * intermediateset. In this case the driver will be
1094                          * notified when the paRAM set is submitted to TC. This
1095                          * will allow more time to set up the next set of slots.
1096                          */
1097                         edesc->pset[i].param.opt |= (TCINTEN | TCCMODE);
1098         }
1099         edesc->residue_stat = edesc->residue;
1100
1101         return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1102 }
1103
1104 static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
1105         struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1106         size_t len, unsigned long tx_flags)
1107 {
1108         int ret, nslots;
1109         struct edma_desc *edesc;
1110         struct device *dev = chan->device->dev;
1111         struct edma_chan *echan = to_edma_chan(chan);
1112         unsigned int width, pset_len, array_size;
1113
1114         if (unlikely(!echan || !len))
1115                 return NULL;
1116
1117         /* Align the array size (acnt block) with the transfer properties */
1118         switch (__ffs((src | dest | len))) {
1119         case 0:
1120                 array_size = SZ_32K - 1;
1121                 break;
1122         case 1:
1123                 array_size = SZ_32K - 2;
1124                 break;
1125         default:
1126                 array_size = SZ_32K - 4;
1127                 break;
1128         }
1129
1130         if (len < SZ_64K) {
1131                 /*
1132                  * Transfer size less than 64K can be handled with one paRAM
1133                  * slot and with one burst.
1134                  * ACNT = length
1135                  */
1136                 width = len;
1137                 pset_len = len;
1138                 nslots = 1;
1139         } else {
1140                 /*
1141                  * Transfer size bigger than 64K will be handled with maximum of
1142                  * two paRAM slots.
1143                  * slot1: (full_length / 32767) times 32767 bytes bursts.
1144                  *        ACNT = 32767, length1: (full_length / 32767) * 32767
1145                  * slot2: the remaining amount of data after slot1.
1146                  *        ACNT = full_length - length1, length2 = ACNT
1147                  *
1148                  * When the full_length is a multiple of 32767 one slot can be
1149                  * used to complete the transfer.
1150                  */
1151                 width = array_size;
1152                 pset_len = rounddown(len, width);
1153                 /* One slot is enough for lengths multiple of (SZ_32K -1) */
1154                 if (unlikely(pset_len == len))
1155                         nslots = 1;
1156                 else
1157                         nslots = 2;
1158         }
1159
1160         edesc = kzalloc(struct_size(edesc, pset, nslots), GFP_ATOMIC);
1161         if (!edesc)
1162                 return NULL;
1163
1164         edesc->pset_nr = nslots;
1165         edesc->residue = edesc->residue_stat = len;
1166         edesc->direction = DMA_MEM_TO_MEM;
1167         edesc->echan = echan;
1168
1169         ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
1170                                width, pset_len, DMA_MEM_TO_MEM);
1171         if (ret < 0) {
1172                 kfree(edesc);
1173                 return NULL;
1174         }
1175
1176         edesc->absync = ret;
1177
1178         edesc->pset[0].param.opt |= ITCCHEN;
1179         if (nslots == 1) {
1180                 /* Enable transfer complete interrupt if requested */
1181                 if (tx_flags & DMA_PREP_INTERRUPT)
1182                         edesc->pset[0].param.opt |= TCINTEN;
1183         } else {
1184                 /* Enable transfer complete chaining for the first slot */
1185                 edesc->pset[0].param.opt |= TCCHEN;
1186
1187                 if (echan->slot[1] < 0) {
1188                         echan->slot[1] = edma_alloc_slot(echan->ecc,
1189                                                          EDMA_SLOT_ANY);
1190                         if (echan->slot[1] < 0) {
1191                                 kfree(edesc);
1192                                 dev_err(dev, "%s: Failed to allocate slot\n",
1193                                         __func__);
1194                                 return NULL;
1195                         }
1196                 }
1197                 dest += pset_len;
1198                 src += pset_len;
1199                 pset_len = width = len % array_size;
1200
1201                 ret = edma_config_pset(chan, &edesc->pset[1], src, dest, 1,
1202                                        width, pset_len, DMA_MEM_TO_MEM);
1203                 if (ret < 0) {
1204                         kfree(edesc);
1205                         return NULL;
1206                 }
1207
1208                 edesc->pset[1].param.opt |= ITCCHEN;
1209                 /* Enable transfer complete interrupt if requested */
1210                 if (tx_flags & DMA_PREP_INTERRUPT)
1211                         edesc->pset[1].param.opt |= TCINTEN;
1212         }
1213
1214         if (!(tx_flags & DMA_PREP_INTERRUPT))
1215                 edesc->polled = true;
1216
1217         return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1218 }
1219
1220 static struct dma_async_tx_descriptor *
1221 edma_prep_dma_interleaved(struct dma_chan *chan,
1222                           struct dma_interleaved_template *xt,
1223                           unsigned long tx_flags)
1224 {
1225         struct device *dev = chan->device->dev;
1226         struct edma_chan *echan = to_edma_chan(chan);
1227         struct edmacc_param *param;
1228         struct edma_desc *edesc;
1229         size_t src_icg, dst_icg;
1230         int src_bidx, dst_bidx;
1231
1232         /* Slave mode is not supported */
1233         if (is_slave_direction(xt->dir))
1234                 return NULL;
1235
1236         if (xt->frame_size != 1 || xt->numf == 0)
1237                 return NULL;
1238
1239         if (xt->sgl[0].size > SZ_64K || xt->numf > SZ_64K)
1240                 return NULL;
1241
1242         src_icg = dmaengine_get_src_icg(xt, &xt->sgl[0]);
1243         if (src_icg) {
1244                 src_bidx = src_icg + xt->sgl[0].size;
1245         } else if (xt->src_inc) {
1246                 src_bidx = xt->sgl[0].size;
1247         } else {
1248                 dev_err(dev, "%s: SRC constant addressing is not supported\n",
1249                         __func__);
1250                 return NULL;
1251         }
1252
1253         dst_icg = dmaengine_get_dst_icg(xt, &xt->sgl[0]);
1254         if (dst_icg) {
1255                 dst_bidx = dst_icg + xt->sgl[0].size;
1256         } else if (xt->dst_inc) {
1257                 dst_bidx = xt->sgl[0].size;
1258         } else {
1259                 dev_err(dev, "%s: DST constant addressing is not supported\n",
1260                         __func__);
1261                 return NULL;
1262         }
1263
1264         if (src_bidx > SZ_64K || dst_bidx > SZ_64K)
1265                 return NULL;
1266
1267         edesc = kzalloc(struct_size(edesc, pset, 1), GFP_ATOMIC);
1268         if (!edesc)
1269                 return NULL;
1270
1271         edesc->direction = DMA_MEM_TO_MEM;
1272         edesc->echan = echan;
1273         edesc->pset_nr = 1;
1274
1275         param = &edesc->pset[0].param;
1276
1277         param->src = xt->src_start;
1278         param->dst = xt->dst_start;
1279         param->a_b_cnt = xt->numf << 16 | xt->sgl[0].size;
1280         param->ccnt = 1;
1281         param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
1282         param->src_dst_cidx = 0;
1283
1284         param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
1285         param->opt |= ITCCHEN;
1286         /* Enable transfer complete interrupt if requested */
1287         if (tx_flags & DMA_PREP_INTERRUPT)
1288                 param->opt |= TCINTEN;
1289         else
1290                 edesc->polled = true;
1291
1292         return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1293 }
1294
1295 static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
1296         struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1297         size_t period_len, enum dma_transfer_direction direction,
1298         unsigned long tx_flags)
1299 {
1300         struct edma_chan *echan = to_edma_chan(chan);
1301         struct device *dev = chan->device->dev;
1302         struct edma_desc *edesc;
1303         dma_addr_t src_addr, dst_addr;
1304         enum dma_slave_buswidth dev_width;
1305         bool use_intermediate = false;
1306         u32 burst;
1307         int i, ret, nslots;
1308
1309         if (unlikely(!echan || !buf_len || !period_len))
1310                 return NULL;
1311
1312         if (direction == DMA_DEV_TO_MEM) {
1313                 src_addr = echan->cfg.src_addr;
1314                 dst_addr = buf_addr;
1315                 dev_width = echan->cfg.src_addr_width;
1316                 burst = echan->cfg.src_maxburst;
1317         } else if (direction == DMA_MEM_TO_DEV) {
1318                 src_addr = buf_addr;
1319                 dst_addr = echan->cfg.dst_addr;
1320                 dev_width = echan->cfg.dst_addr_width;
1321                 burst = echan->cfg.dst_maxburst;
1322         } else {
1323                 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
1324                 return NULL;
1325         }
1326
1327         if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
1328                 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
1329                 return NULL;
1330         }
1331
1332         if (unlikely(buf_len % period_len)) {
1333                 dev_err(dev, "Period should be multiple of Buffer length\n");
1334                 return NULL;
1335         }
1336
1337         nslots = (buf_len / period_len) + 1;
1338
1339         /*
1340          * Cyclic DMA users such as audio cannot tolerate delays introduced
1341          * by cases where the number of periods is more than the maximum
1342          * number of SGs the EDMA driver can handle at a time. For DMA types
1343          * such as Slave SGs, such delays are tolerable and synchronized,
1344          * but the synchronization is difficult to achieve with Cyclic and
1345          * cannot be guaranteed, so we error out early.
1346          */
1347         if (nslots > MAX_NR_SG) {
1348                 /*
1349                  * If the burst and period sizes are the same, we can put
1350                  * the full buffer into a single period and activate
1351                  * intermediate interrupts. This will produce interrupts
1352                  * after each burst, which is also after each desired period.
1353                  */
1354                 if (burst == period_len) {
1355                         period_len = buf_len;
1356                         nslots = 2;
1357                         use_intermediate = true;
1358                 } else {
1359                         return NULL;
1360                 }
1361         }
1362
1363         edesc = kzalloc(struct_size(edesc, pset, nslots), GFP_ATOMIC);
1364         if (!edesc)
1365                 return NULL;
1366
1367         edesc->cyclic = 1;
1368         edesc->pset_nr = nslots;
1369         edesc->residue = edesc->residue_stat = buf_len;
1370         edesc->direction = direction;
1371         edesc->echan = echan;
1372
1373         dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
1374                 __func__, echan->ch_num, nslots, period_len, buf_len);
1375
1376         for (i = 0; i < nslots; i++) {
1377                 /* Allocate a PaRAM slot, if needed */
1378                 if (echan->slot[i] < 0) {
1379                         echan->slot[i] =
1380                                 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
1381                         if (echan->slot[i] < 0) {
1382                                 kfree(edesc);
1383                                 dev_err(dev, "%s: Failed to allocate slot\n",
1384                                         __func__);
1385                                 return NULL;
1386                         }
1387                 }
1388
1389                 if (i == nslots - 1) {
1390                         memcpy(&edesc->pset[i], &edesc->pset[0],
1391                                sizeof(edesc->pset[0]));
1392                         break;
1393                 }
1394
1395                 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1396                                        dst_addr, burst, dev_width, period_len,
1397                                        direction);
1398                 if (ret < 0) {
1399                         kfree(edesc);
1400                         return NULL;
1401                 }
1402
1403                 if (direction == DMA_DEV_TO_MEM)
1404                         dst_addr += period_len;
1405                 else
1406                         src_addr += period_len;
1407
1408                 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
1409                 dev_vdbg(dev,
1410                         "\n pset[%d]:\n"
1411                         "  chnum\t%d\n"
1412                         "  slot\t%d\n"
1413                         "  opt\t%08x\n"
1414                         "  src\t%08x\n"
1415                         "  dst\t%08x\n"
1416                         "  abcnt\t%08x\n"
1417                         "  ccnt\t%08x\n"
1418                         "  bidx\t%08x\n"
1419                         "  cidx\t%08x\n"
1420                         "  lkrld\t%08x\n",
1421                         i, echan->ch_num, echan->slot[i],
1422                         edesc->pset[i].param.opt,
1423                         edesc->pset[i].param.src,
1424                         edesc->pset[i].param.dst,
1425                         edesc->pset[i].param.a_b_cnt,
1426                         edesc->pset[i].param.ccnt,
1427                         edesc->pset[i].param.src_dst_bidx,
1428                         edesc->pset[i].param.src_dst_cidx,
1429                         edesc->pset[i].param.link_bcntrld);
1430
1431                 edesc->absync = ret;
1432
1433                 /*
1434                  * Enable period interrupt only if it is requested
1435                  */
1436                 if (tx_flags & DMA_PREP_INTERRUPT) {
1437                         edesc->pset[i].param.opt |= TCINTEN;
1438
1439                         /* Also enable intermediate interrupts if necessary */
1440                         if (use_intermediate)
1441                                 edesc->pset[i].param.opt |= ITCINTEN;
1442                 }
1443         }
1444
1445         /* Place the cyclic channel to highest priority queue */
1446         if (!echan->tc)
1447                 edma_assign_channel_eventq(echan, EVENTQ_0);
1448
1449         return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1450 }
1451
1452 static void edma_completion_handler(struct edma_chan *echan)
1453 {
1454         struct device *dev = echan->vchan.chan.device->dev;
1455         struct edma_desc *edesc;
1456
1457         spin_lock(&echan->vchan.lock);
1458         edesc = echan->edesc;
1459         if (edesc) {
1460                 if (edesc->cyclic) {
1461                         vchan_cyclic_callback(&edesc->vdesc);
1462                         spin_unlock(&echan->vchan.lock);
1463                         return;
1464                 } else if (edesc->processed == edesc->pset_nr) {
1465                         edesc->residue = 0;
1466                         edma_stop(echan);
1467                         vchan_cookie_complete(&edesc->vdesc);
1468                         echan->edesc = NULL;
1469
1470                         dev_dbg(dev, "Transfer completed on channel %d\n",
1471                                 echan->ch_num);
1472                 } else {
1473                         dev_dbg(dev, "Sub transfer completed on channel %d\n",
1474                                 echan->ch_num);
1475
1476                         edma_pause(echan);
1477
1478                         /* Update statistics for tx_status */
1479                         edesc->residue -= edesc->sg_len;
1480                         edesc->residue_stat = edesc->residue;
1481                         edesc->processed_stat = edesc->processed;
1482                 }
1483                 edma_execute(echan);
1484         }
1485
1486         spin_unlock(&echan->vchan.lock);
1487 }
1488
1489 /* eDMA interrupt handler */
1490 static irqreturn_t dma_irq_handler(int irq, void *data)
1491 {
1492         struct edma_cc *ecc = data;
1493         int ctlr;
1494         u32 sh_ier;
1495         u32 sh_ipr;
1496         u32 bank;
1497
1498         ctlr = ecc->id;
1499         if (ctlr < 0)
1500                 return IRQ_NONE;
1501
1502         dev_vdbg(ecc->dev, "dma_irq_handler\n");
1503
1504         sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 0);
1505         if (!sh_ipr) {
1506                 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 1);
1507                 if (!sh_ipr)
1508                         return IRQ_NONE;
1509                 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 1);
1510                 bank = 1;
1511         } else {
1512                 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 0);
1513                 bank = 0;
1514         }
1515
1516         do {
1517                 u32 slot;
1518                 u32 channel;
1519
1520                 slot = __ffs(sh_ipr);
1521                 sh_ipr &= ~(BIT(slot));
1522
1523                 if (sh_ier & BIT(slot)) {
1524                         channel = (bank << 5) | slot;
1525                         /* Clear the corresponding IPR bits */
1526                         edma_shadow0_write_array(ecc, SH_ICR, bank, BIT(slot));
1527                         edma_completion_handler(&ecc->slave_chans[channel]);
1528                 }
1529         } while (sh_ipr);
1530
1531         edma_shadow0_write(ecc, SH_IEVAL, 1);
1532         return IRQ_HANDLED;
1533 }
1534
1535 static void edma_error_handler(struct edma_chan *echan)
1536 {
1537         struct edma_cc *ecc = echan->ecc;
1538         struct device *dev = echan->vchan.chan.device->dev;
1539         struct edmacc_param p;
1540         int err;
1541
1542         if (!echan->edesc)
1543                 return;
1544
1545         spin_lock(&echan->vchan.lock);
1546
1547         err = edma_read_slot(ecc, echan->slot[0], &p);
1548
1549         /*
1550          * Issue later based on missed flag which will be sure
1551          * to happen as:
1552          * (1) we finished transmitting an intermediate slot and
1553          *     edma_execute is coming up.
1554          * (2) or we finished current transfer and issue will
1555          *     call edma_execute.
1556          *
1557          * Important note: issuing can be dangerous here and
1558          * lead to some nasty recursion when we are in a NULL
1559          * slot. So we avoid doing so and set the missed flag.
1560          */
1561         if (err || (p.a_b_cnt == 0 && p.ccnt == 0)) {
1562                 dev_dbg(dev, "Error on null slot, setting miss\n");
1563                 echan->missed = 1;
1564         } else {
1565                 /*
1566                  * The slot is already programmed but the event got
1567                  * missed, so its safe to issue it here.
1568                  */
1569                 dev_dbg(dev, "Missed event, TRIGGERING\n");
1570                 edma_clean_channel(echan);
1571                 edma_stop(echan);
1572                 edma_start(echan);
1573                 edma_trigger_channel(echan);
1574         }
1575         spin_unlock(&echan->vchan.lock);
1576 }
1577
1578 static inline bool edma_error_pending(struct edma_cc *ecc)
1579 {
1580         if (edma_read_array(ecc, EDMA_EMR, 0) ||
1581             edma_read_array(ecc, EDMA_EMR, 1) ||
1582             edma_read(ecc, EDMA_QEMR) || edma_read(ecc, EDMA_CCERR))
1583                 return true;
1584
1585         return false;
1586 }
1587
1588 /* eDMA error interrupt handler */
1589 static irqreturn_t dma_ccerr_handler(int irq, void *data)
1590 {
1591         struct edma_cc *ecc = data;
1592         int i, j;
1593         int ctlr;
1594         unsigned int cnt = 0;
1595         unsigned int val;
1596
1597         ctlr = ecc->id;
1598         if (ctlr < 0)
1599                 return IRQ_NONE;
1600
1601         dev_vdbg(ecc->dev, "dma_ccerr_handler\n");
1602
1603         if (!edma_error_pending(ecc)) {
1604                 /*
1605                  * The registers indicate no pending error event but the irq
1606                  * handler has been called.
1607                  * Ask eDMA to re-evaluate the error registers.
1608                  */
1609                 dev_err(ecc->dev, "%s: Error interrupt without error event!\n",
1610                         __func__);
1611                 edma_write(ecc, EDMA_EEVAL, 1);
1612                 return IRQ_NONE;
1613         }
1614
1615         while (1) {
1616                 /* Event missed register(s) */
1617                 for (j = 0; j < 2; j++) {
1618                         unsigned long emr;
1619
1620                         val = edma_read_array(ecc, EDMA_EMR, j);
1621                         if (!val)
1622                                 continue;
1623
1624                         dev_dbg(ecc->dev, "EMR%d 0x%08x\n", j, val);
1625                         emr = val;
1626                         for_each_set_bit(i, &emr, 32) {
1627                                 int k = (j << 5) + i;
1628
1629                                 /* Clear the corresponding EMR bits */
1630                                 edma_write_array(ecc, EDMA_EMCR, j, BIT(i));
1631                                 /* Clear any SER */
1632                                 edma_shadow0_write_array(ecc, SH_SECR, j,
1633                                                          BIT(i));
1634                                 edma_error_handler(&ecc->slave_chans[k]);
1635                         }
1636                 }
1637
1638                 val = edma_read(ecc, EDMA_QEMR);
1639                 if (val) {
1640                         dev_dbg(ecc->dev, "QEMR 0x%02x\n", val);
1641                         /* Not reported, just clear the interrupt reason. */
1642                         edma_write(ecc, EDMA_QEMCR, val);
1643                         edma_shadow0_write(ecc, SH_QSECR, val);
1644                 }
1645
1646                 val = edma_read(ecc, EDMA_CCERR);
1647                 if (val) {
1648                         dev_warn(ecc->dev, "CCERR 0x%08x\n", val);
1649                         /* Not reported, just clear the interrupt reason. */
1650                         edma_write(ecc, EDMA_CCERRCLR, val);
1651                 }
1652
1653                 if (!edma_error_pending(ecc))
1654                         break;
1655                 cnt++;
1656                 if (cnt > 10)
1657                         break;
1658         }
1659         edma_write(ecc, EDMA_EEVAL, 1);
1660         return IRQ_HANDLED;
1661 }
1662
1663 /* Alloc channel resources */
1664 static int edma_alloc_chan_resources(struct dma_chan *chan)
1665 {
1666         struct edma_chan *echan = to_edma_chan(chan);
1667         struct edma_cc *ecc = echan->ecc;
1668         struct device *dev = ecc->dev;
1669         enum dma_event_q eventq_no = EVENTQ_DEFAULT;
1670         int ret;
1671
1672         if (echan->tc) {
1673                 eventq_no = echan->tc->id;
1674         } else if (ecc->tc_list) {
1675                 /* memcpy channel */
1676                 echan->tc = &ecc->tc_list[ecc->info->default_queue];
1677                 eventq_no = echan->tc->id;
1678         }
1679
1680         ret = edma_alloc_channel(echan, eventq_no);
1681         if (ret)
1682                 return ret;
1683
1684         echan->slot[0] = edma_alloc_slot(ecc, echan->ch_num);
1685         if (echan->slot[0] < 0) {
1686                 dev_err(dev, "Entry slot allocation failed for channel %u\n",
1687                         EDMA_CHAN_SLOT(echan->ch_num));
1688                 ret = echan->slot[0];
1689                 goto err_slot;
1690         }
1691
1692         /* Set up channel -> slot mapping for the entry slot */
1693         edma_set_chmap(echan, echan->slot[0]);
1694         echan->alloced = true;
1695
1696         dev_dbg(dev, "Got eDMA channel %d for virt channel %d (%s trigger)\n",
1697                 EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id,
1698                 echan->hw_triggered ? "HW" : "SW");
1699
1700         return 0;
1701
1702 err_slot:
1703         edma_free_channel(echan);
1704         return ret;
1705 }
1706
1707 /* Free channel resources */
1708 static void edma_free_chan_resources(struct dma_chan *chan)
1709 {
1710         struct edma_chan *echan = to_edma_chan(chan);
1711         struct device *dev = echan->ecc->dev;
1712         int i;
1713
1714         /* Terminate transfers */
1715         edma_stop(echan);
1716
1717         vchan_free_chan_resources(&echan->vchan);
1718
1719         /* Free EDMA PaRAM slots */
1720         for (i = 0; i < EDMA_MAX_SLOTS; i++) {
1721                 if (echan->slot[i] >= 0) {
1722                         edma_free_slot(echan->ecc, echan->slot[i]);
1723                         echan->slot[i] = -1;
1724                 }
1725         }
1726
1727         /* Set entry slot to the dummy slot */
1728         edma_set_chmap(echan, echan->ecc->dummy_slot);
1729
1730         /* Free EDMA channel */
1731         if (echan->alloced) {
1732                 edma_free_channel(echan);
1733                 echan->alloced = false;
1734         }
1735
1736         echan->tc = NULL;
1737         echan->hw_triggered = false;
1738
1739         dev_dbg(dev, "Free eDMA channel %d for virt channel %d\n",
1740                 EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id);
1741 }
1742
1743 /* Send pending descriptor to hardware */
1744 static void edma_issue_pending(struct dma_chan *chan)
1745 {
1746         struct edma_chan *echan = to_edma_chan(chan);
1747         unsigned long flags;
1748
1749         spin_lock_irqsave(&echan->vchan.lock, flags);
1750         if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
1751                 edma_execute(echan);
1752         spin_unlock_irqrestore(&echan->vchan.lock, flags);
1753 }
1754
1755 /*
1756  * This limit exists to avoid a possible infinite loop when waiting for proof
1757  * that a particular transfer is completed. This limit can be hit if there
1758  * are large bursts to/from slow devices or the CPU is never able to catch
1759  * the DMA hardware idle. On an AM335x transferring 48 bytes from the UART
1760  * RX-FIFO, as many as 55 loops have been seen.
1761  */
1762 #define EDMA_MAX_TR_WAIT_LOOPS 1000
1763
1764 static u32 edma_residue(struct edma_desc *edesc)
1765 {
1766         bool dst = edesc->direction == DMA_DEV_TO_MEM;
1767         int loop_count = EDMA_MAX_TR_WAIT_LOOPS;
1768         struct edma_chan *echan = edesc->echan;
1769         struct edma_pset *pset = edesc->pset;
1770         dma_addr_t done, pos, pos_old;
1771         int channel = EDMA_CHAN_SLOT(echan->ch_num);
1772         int idx = EDMA_REG_ARRAY_INDEX(channel);
1773         int ch_bit = EDMA_CHANNEL_BIT(channel);
1774         int event_reg;
1775         int i;
1776
1777         /*
1778          * We always read the dst/src position from the first RamPar
1779          * pset. That's the one which is active now.
1780          */
1781         pos = edma_get_position(echan->ecc, echan->slot[0], dst);
1782
1783         /*
1784          * "pos" may represent a transfer request that is still being
1785          * processed by the EDMACC or EDMATC. We will busy wait until
1786          * any one of the situations occurs:
1787          *   1. while and event is pending for the channel
1788          *   2. a position updated
1789          *   3. we hit the loop limit
1790          */
1791         if (is_slave_direction(edesc->direction))
1792                 event_reg = SH_ER;
1793         else
1794                 event_reg = SH_ESR;
1795
1796         pos_old = pos;
1797         while (edma_shadow0_read_array(echan->ecc, event_reg, idx) & ch_bit) {
1798                 pos = edma_get_position(echan->ecc, echan->slot[0], dst);
1799                 if (pos != pos_old)
1800                         break;
1801
1802                 if (!--loop_count) {
1803                         dev_dbg_ratelimited(echan->vchan.chan.device->dev,
1804                                 "%s: timeout waiting for PaRAM update\n",
1805                                 __func__);
1806                         break;
1807                 }
1808
1809                 cpu_relax();
1810         }
1811
1812         /*
1813          * Cyclic is simple. Just subtract pset[0].addr from pos.
1814          *
1815          * We never update edesc->residue in the cyclic case, so we
1816          * can tell the remaining room to the end of the circular
1817          * buffer.
1818          */
1819         if (edesc->cyclic) {
1820                 done = pos - pset->addr;
1821                 edesc->residue_stat = edesc->residue - done;
1822                 return edesc->residue_stat;
1823         }
1824
1825         /*
1826          * If the position is 0, then EDMA loaded the closing dummy slot, the
1827          * transfer is completed
1828          */
1829         if (!pos)
1830                 return 0;
1831         /*
1832          * For SG operation we catch up with the last processed
1833          * status.
1834          */
1835         pset += edesc->processed_stat;
1836
1837         for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
1838                 /*
1839                  * If we are inside this pset address range, we know
1840                  * this is the active one. Get the current delta and
1841                  * stop walking the psets.
1842                  */
1843                 if (pos >= pset->addr && pos < pset->addr + pset->len)
1844                         return edesc->residue_stat - (pos - pset->addr);
1845
1846                 /* Otherwise mark it done and update residue_stat. */
1847                 edesc->processed_stat++;
1848                 edesc->residue_stat -= pset->len;
1849         }
1850         return edesc->residue_stat;
1851 }
1852
1853 /* Check request completion status */
1854 static enum dma_status edma_tx_status(struct dma_chan *chan,
1855                                       dma_cookie_t cookie,
1856                                       struct dma_tx_state *txstate)
1857 {
1858         struct edma_chan *echan = to_edma_chan(chan);
1859         struct dma_tx_state txstate_tmp;
1860         enum dma_status ret;
1861         unsigned long flags;
1862
1863         ret = dma_cookie_status(chan, cookie, txstate);
1864
1865         if (ret == DMA_COMPLETE)
1866                 return ret;
1867
1868         /* Provide a dummy dma_tx_state for completion checking */
1869         if (!txstate)
1870                 txstate = &txstate_tmp;
1871
1872         spin_lock_irqsave(&echan->vchan.lock, flags);
1873         if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie) {
1874                 txstate->residue = edma_residue(echan->edesc);
1875         } else {
1876                 struct virt_dma_desc *vdesc = vchan_find_desc(&echan->vchan,
1877                                                               cookie);
1878
1879                 if (vdesc)
1880                         txstate->residue = to_edma_desc(&vdesc->tx)->residue;
1881                 else
1882                         txstate->residue = 0;
1883         }
1884
1885         /*
1886          * Mark the cookie completed if the residue is 0 for non cyclic
1887          * transfers
1888          */
1889         if (ret != DMA_COMPLETE && !txstate->residue &&
1890             echan->edesc && echan->edesc->polled &&
1891             echan->edesc->vdesc.tx.cookie == cookie) {
1892                 edma_stop(echan);
1893                 vchan_cookie_complete(&echan->edesc->vdesc);
1894                 echan->edesc = NULL;
1895                 edma_execute(echan);
1896                 ret = DMA_COMPLETE;
1897         }
1898
1899         spin_unlock_irqrestore(&echan->vchan.lock, flags);
1900
1901         return ret;
1902 }
1903
1904 static bool edma_is_memcpy_channel(int ch_num, s32 *memcpy_channels)
1905 {
1906         if (!memcpy_channels)
1907                 return false;
1908         while (*memcpy_channels != -1) {
1909                 if (*memcpy_channels == ch_num)
1910                         return true;
1911                 memcpy_channels++;
1912         }
1913         return false;
1914 }
1915
1916 #define EDMA_DMA_BUSWIDTHS      (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1917                                  BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
1918                                  BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
1919                                  BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1920
1921 static void edma_dma_init(struct edma_cc *ecc, bool legacy_mode)
1922 {
1923         struct dma_device *s_ddev = &ecc->dma_slave;
1924         struct dma_device *m_ddev = NULL;
1925         s32 *memcpy_channels = ecc->info->memcpy_channels;
1926         int i, j;
1927
1928         dma_cap_zero(s_ddev->cap_mask);
1929         dma_cap_set(DMA_SLAVE, s_ddev->cap_mask);
1930         dma_cap_set(DMA_CYCLIC, s_ddev->cap_mask);
1931         if (ecc->legacy_mode && !memcpy_channels) {
1932                 dev_warn(ecc->dev,
1933                          "Legacy memcpy is enabled, things might not work\n");
1934
1935                 dma_cap_set(DMA_MEMCPY, s_ddev->cap_mask);
1936                 dma_cap_set(DMA_INTERLEAVE, s_ddev->cap_mask);
1937                 s_ddev->device_prep_dma_memcpy = edma_prep_dma_memcpy;
1938                 s_ddev->device_prep_interleaved_dma = edma_prep_dma_interleaved;
1939                 s_ddev->directions = BIT(DMA_MEM_TO_MEM);
1940         }
1941
1942         s_ddev->device_prep_slave_sg = edma_prep_slave_sg;
1943         s_ddev->device_prep_dma_cyclic = edma_prep_dma_cyclic;
1944         s_ddev->device_alloc_chan_resources = edma_alloc_chan_resources;
1945         s_ddev->device_free_chan_resources = edma_free_chan_resources;
1946         s_ddev->device_issue_pending = edma_issue_pending;
1947         s_ddev->device_tx_status = edma_tx_status;
1948         s_ddev->device_config = edma_slave_config;
1949         s_ddev->device_pause = edma_dma_pause;
1950         s_ddev->device_resume = edma_dma_resume;
1951         s_ddev->device_terminate_all = edma_terminate_all;
1952         s_ddev->device_synchronize = edma_synchronize;
1953
1954         s_ddev->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1955         s_ddev->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
1956         s_ddev->directions |= (BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV));
1957         s_ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1958         s_ddev->max_burst = SZ_32K - 1; /* CIDX: 16bit signed */
1959
1960         s_ddev->dev = ecc->dev;
1961         INIT_LIST_HEAD(&s_ddev->channels);
1962
1963         if (memcpy_channels) {
1964                 m_ddev = devm_kzalloc(ecc->dev, sizeof(*m_ddev), GFP_KERNEL);
1965                 if (!m_ddev) {
1966                         dev_warn(ecc->dev, "memcpy is disabled due to OoM\n");
1967                         memcpy_channels = NULL;
1968                         goto ch_setup;
1969                 }
1970                 ecc->dma_memcpy = m_ddev;
1971
1972                 dma_cap_zero(m_ddev->cap_mask);
1973                 dma_cap_set(DMA_MEMCPY, m_ddev->cap_mask);
1974                 dma_cap_set(DMA_INTERLEAVE, m_ddev->cap_mask);
1975
1976                 m_ddev->device_prep_dma_memcpy = edma_prep_dma_memcpy;
1977                 m_ddev->device_prep_interleaved_dma = edma_prep_dma_interleaved;
1978                 m_ddev->device_alloc_chan_resources = edma_alloc_chan_resources;
1979                 m_ddev->device_free_chan_resources = edma_free_chan_resources;
1980                 m_ddev->device_issue_pending = edma_issue_pending;
1981                 m_ddev->device_tx_status = edma_tx_status;
1982                 m_ddev->device_config = edma_slave_config;
1983                 m_ddev->device_pause = edma_dma_pause;
1984                 m_ddev->device_resume = edma_dma_resume;
1985                 m_ddev->device_terminate_all = edma_terminate_all;
1986                 m_ddev->device_synchronize = edma_synchronize;
1987
1988                 m_ddev->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1989                 m_ddev->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
1990                 m_ddev->directions = BIT(DMA_MEM_TO_MEM);
1991                 m_ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1992
1993                 m_ddev->dev = ecc->dev;
1994                 INIT_LIST_HEAD(&m_ddev->channels);
1995         } else if (!ecc->legacy_mode) {
1996                 dev_info(ecc->dev, "memcpy is disabled\n");
1997         }
1998
1999 ch_setup:
2000         for (i = 0; i < ecc->num_channels; i++) {
2001                 struct edma_chan *echan = &ecc->slave_chans[i];
2002                 echan->ch_num = EDMA_CTLR_CHAN(ecc->id, i);
2003                 echan->ecc = ecc;
2004                 echan->vchan.desc_free = edma_desc_free;
2005
2006                 if (m_ddev && edma_is_memcpy_channel(i, memcpy_channels))
2007                         vchan_init(&echan->vchan, m_ddev);
2008                 else
2009                         vchan_init(&echan->vchan, s_ddev);
2010
2011                 INIT_LIST_HEAD(&echan->node);
2012                 for (j = 0; j < EDMA_MAX_SLOTS; j++)
2013                         echan->slot[j] = -1;
2014         }
2015 }
2016
2017 static int edma_setup_from_hw(struct device *dev, struct edma_soc_info *pdata,
2018                               struct edma_cc *ecc)
2019 {
2020         int i;
2021         u32 value, cccfg;
2022         s8 (*queue_priority_map)[2];
2023
2024         /* Decode the eDMA3 configuration from CCCFG register */
2025         cccfg = edma_read(ecc, EDMA_CCCFG);
2026
2027         value = GET_NUM_REGN(cccfg);
2028         ecc->num_region = BIT(value);
2029
2030         value = GET_NUM_DMACH(cccfg);
2031         ecc->num_channels = BIT(value + 1);
2032
2033         value = GET_NUM_QDMACH(cccfg);
2034         ecc->num_qchannels = value * 2;
2035
2036         value = GET_NUM_PAENTRY(cccfg);
2037         ecc->num_slots = BIT(value + 4);
2038
2039         value = GET_NUM_EVQUE(cccfg);
2040         ecc->num_tc = value + 1;
2041
2042         ecc->chmap_exist = (cccfg & CHMAP_EXIST) ? true : false;
2043
2044         dev_dbg(dev, "eDMA3 CC HW configuration (cccfg: 0x%08x):\n", cccfg);
2045         dev_dbg(dev, "num_region: %u\n", ecc->num_region);
2046         dev_dbg(dev, "num_channels: %u\n", ecc->num_channels);
2047         dev_dbg(dev, "num_qchannels: %u\n", ecc->num_qchannels);
2048         dev_dbg(dev, "num_slots: %u\n", ecc->num_slots);
2049         dev_dbg(dev, "num_tc: %u\n", ecc->num_tc);
2050         dev_dbg(dev, "chmap_exist: %s\n", ecc->chmap_exist ? "yes" : "no");
2051
2052         /* Nothing need to be done if queue priority is provided */
2053         if (pdata->queue_priority_mapping)
2054                 return 0;
2055
2056         /*
2057          * Configure TC/queue priority as follows:
2058          * Q0 - priority 0
2059          * Q1 - priority 1
2060          * Q2 - priority 2
2061          * ...
2062          * The meaning of priority numbers: 0 highest priority, 7 lowest
2063          * priority. So Q0 is the highest priority queue and the last queue has
2064          * the lowest priority.
2065          */
2066         queue_priority_map = devm_kcalloc(dev, ecc->num_tc + 1, sizeof(s8),
2067                                           GFP_KERNEL);
2068         if (!queue_priority_map)
2069                 return -ENOMEM;
2070
2071         for (i = 0; i < ecc->num_tc; i++) {
2072                 queue_priority_map[i][0] = i;
2073                 queue_priority_map[i][1] = i;
2074         }
2075         queue_priority_map[i][0] = -1;
2076         queue_priority_map[i][1] = -1;
2077
2078         pdata->queue_priority_mapping = queue_priority_map;
2079         /* Default queue has the lowest priority */
2080         pdata->default_queue = i - 1;
2081
2082         return 0;
2083 }
2084
2085 #if IS_ENABLED(CONFIG_OF)
2086 static int edma_xbar_event_map(struct device *dev, struct edma_soc_info *pdata,
2087                                size_t sz)
2088 {
2089         const char pname[] = "ti,edma-xbar-event-map";
2090         struct resource res;
2091         void __iomem *xbar;
2092         s16 (*xbar_chans)[2];
2093         size_t nelm = sz / sizeof(s16);
2094         u32 shift, offset, mux;
2095         int ret, i;
2096
2097         xbar_chans = devm_kcalloc(dev, nelm + 2, sizeof(s16), GFP_KERNEL);
2098         if (!xbar_chans)
2099                 return -ENOMEM;
2100
2101         ret = of_address_to_resource(dev->of_node, 1, &res);
2102         if (ret)
2103                 return -ENOMEM;
2104
2105         xbar = devm_ioremap(dev, res.start, resource_size(&res));
2106         if (!xbar)
2107                 return -ENOMEM;
2108
2109         ret = of_property_read_u16_array(dev->of_node, pname, (u16 *)xbar_chans,
2110                                          nelm);
2111         if (ret)
2112                 return -EIO;
2113
2114         /* Invalidate last entry for the other user of this mess */
2115         nelm >>= 1;
2116         xbar_chans[nelm][0] = -1;
2117         xbar_chans[nelm][1] = -1;
2118
2119         for (i = 0; i < nelm; i++) {
2120                 shift = (xbar_chans[i][1] & 0x03) << 3;
2121                 offset = xbar_chans[i][1] & 0xfffffffc;
2122                 mux = readl(xbar + offset);
2123                 mux &= ~(0xff << shift);
2124                 mux |= xbar_chans[i][0] << shift;
2125                 writel(mux, (xbar + offset));
2126         }
2127
2128         pdata->xbar_chans = (const s16 (*)[2]) xbar_chans;
2129         return 0;
2130 }
2131
2132 static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
2133                                                      bool legacy_mode)
2134 {
2135         struct edma_soc_info *info;
2136         struct property *prop;
2137         int sz, ret;
2138
2139         info = devm_kzalloc(dev, sizeof(struct edma_soc_info), GFP_KERNEL);
2140         if (!info)
2141                 return ERR_PTR(-ENOMEM);
2142
2143         if (legacy_mode) {
2144                 prop = of_find_property(dev->of_node, "ti,edma-xbar-event-map",
2145                                         &sz);
2146                 if (prop) {
2147                         ret = edma_xbar_event_map(dev, info, sz);
2148                         if (ret)
2149                                 return ERR_PTR(ret);
2150                 }
2151                 return info;
2152         }
2153
2154         /* Get the list of channels allocated to be used for memcpy */
2155         prop = of_find_property(dev->of_node, "ti,edma-memcpy-channels", &sz);
2156         if (prop) {
2157                 const char pname[] = "ti,edma-memcpy-channels";
2158                 size_t nelm = sz / sizeof(s32);
2159                 s32 *memcpy_ch;
2160
2161                 memcpy_ch = devm_kcalloc(dev, nelm + 1, sizeof(s32),
2162                                          GFP_KERNEL);
2163                 if (!memcpy_ch)
2164                         return ERR_PTR(-ENOMEM);
2165
2166                 ret = of_property_read_u32_array(dev->of_node, pname,
2167                                                  (u32 *)memcpy_ch, nelm);
2168                 if (ret)
2169                         return ERR_PTR(ret);
2170
2171                 memcpy_ch[nelm] = -1;
2172                 info->memcpy_channels = memcpy_ch;
2173         }
2174
2175         prop = of_find_property(dev->of_node, "ti,edma-reserved-slot-ranges",
2176                                 &sz);
2177         if (prop) {
2178                 const char pname[] = "ti,edma-reserved-slot-ranges";
2179                 u32 (*tmp)[2];
2180                 s16 (*rsv_slots)[2];
2181                 size_t nelm = sz / sizeof(*tmp);
2182                 struct edma_rsv_info *rsv_info;
2183                 int i;
2184
2185                 if (!nelm)
2186                         return info;
2187
2188                 tmp = kcalloc(nelm, sizeof(*tmp), GFP_KERNEL);
2189                 if (!tmp)
2190                         return ERR_PTR(-ENOMEM);
2191
2192                 rsv_info = devm_kzalloc(dev, sizeof(*rsv_info), GFP_KERNEL);
2193                 if (!rsv_info) {
2194                         kfree(tmp);
2195                         return ERR_PTR(-ENOMEM);
2196                 }
2197
2198                 rsv_slots = devm_kcalloc(dev, nelm + 1, sizeof(*rsv_slots),
2199                                          GFP_KERNEL);
2200                 if (!rsv_slots) {
2201                         kfree(tmp);
2202                         return ERR_PTR(-ENOMEM);
2203                 }
2204
2205                 ret = of_property_read_u32_array(dev->of_node, pname,
2206                                                  (u32 *)tmp, nelm * 2);
2207                 if (ret) {
2208                         kfree(tmp);
2209                         return ERR_PTR(ret);
2210                 }
2211
2212                 for (i = 0; i < nelm; i++) {
2213                         rsv_slots[i][0] = tmp[i][0];
2214                         rsv_slots[i][1] = tmp[i][1];
2215                 }
2216                 rsv_slots[nelm][0] = -1;
2217                 rsv_slots[nelm][1] = -1;
2218
2219                 info->rsv = rsv_info;
2220                 info->rsv->rsv_slots = (const s16 (*)[2])rsv_slots;
2221
2222                 kfree(tmp);
2223         }
2224
2225         return info;
2226 }
2227
2228 static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
2229                                       struct of_dma *ofdma)
2230 {
2231         struct edma_cc *ecc = ofdma->of_dma_data;
2232         struct dma_chan *chan = NULL;
2233         struct edma_chan *echan;
2234         int i;
2235
2236         if (!ecc || dma_spec->args_count < 1)
2237                 return NULL;
2238
2239         for (i = 0; i < ecc->num_channels; i++) {
2240                 echan = &ecc->slave_chans[i];
2241                 if (echan->ch_num == dma_spec->args[0]) {
2242                         chan = &echan->vchan.chan;
2243                         break;
2244                 }
2245         }
2246
2247         if (!chan)
2248                 return NULL;
2249
2250         if (echan->ecc->legacy_mode && dma_spec->args_count == 1)
2251                 goto out;
2252
2253         if (!echan->ecc->legacy_mode && dma_spec->args_count == 2 &&
2254             dma_spec->args[1] < echan->ecc->num_tc) {
2255                 echan->tc = &echan->ecc->tc_list[dma_spec->args[1]];
2256                 goto out;
2257         }
2258
2259         return NULL;
2260 out:
2261         /* The channel is going to be used as HW synchronized */
2262         echan->hw_triggered = true;
2263         return dma_get_slave_channel(chan);
2264 }
2265 #else
2266 static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
2267                                                      bool legacy_mode)
2268 {
2269         return ERR_PTR(-EINVAL);
2270 }
2271
2272 static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
2273                                       struct of_dma *ofdma)
2274 {
2275         return NULL;
2276 }
2277 #endif
2278
2279 static bool edma_filter_fn(struct dma_chan *chan, void *param);
2280
2281 static int edma_probe(struct platform_device *pdev)
2282 {
2283         struct edma_soc_info    *info = pdev->dev.platform_data;
2284         s8                      (*queue_priority_mapping)[2];
2285         const s16               (*reserved)[2];
2286         int                     i, irq;
2287         char                    *irq_name;
2288         struct resource         *mem;
2289         struct device_node      *node = pdev->dev.of_node;
2290         struct device           *dev = &pdev->dev;
2291         struct edma_cc          *ecc;
2292         bool                    legacy_mode = true;
2293         int ret;
2294
2295         if (node) {
2296                 const struct of_device_id *match;
2297
2298                 match = of_match_node(edma_of_ids, node);
2299                 if (match && (*(u32 *)match->data) == EDMA_BINDING_TPCC)
2300                         legacy_mode = false;
2301
2302                 info = edma_setup_info_from_dt(dev, legacy_mode);
2303                 if (IS_ERR(info)) {
2304                         dev_err(dev, "failed to get DT data\n");
2305                         return PTR_ERR(info);
2306                 }
2307         }
2308
2309         if (!info)
2310                 return -ENODEV;
2311
2312         ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2313         if (ret)
2314                 return ret;
2315
2316         ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
2317         if (!ecc)
2318                 return -ENOMEM;
2319
2320         ecc->dev = dev;
2321         ecc->id = pdev->id;
2322         ecc->legacy_mode = legacy_mode;
2323         /* When booting with DT the pdev->id is -1 */
2324         if (ecc->id < 0)
2325                 ecc->id = 0;
2326
2327         mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma3_cc");
2328         if (!mem) {
2329                 dev_dbg(dev, "mem resource not found, using index 0\n");
2330                 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2331                 if (!mem) {
2332                         dev_err(dev, "no mem resource?\n");
2333                         return -ENODEV;
2334                 }
2335         }
2336         ecc->base = devm_ioremap_resource(dev, mem);
2337         if (IS_ERR(ecc->base))
2338                 return PTR_ERR(ecc->base);
2339
2340         platform_set_drvdata(pdev, ecc);
2341
2342         pm_runtime_enable(dev);
2343         ret = pm_runtime_get_sync(dev);
2344         if (ret < 0) {
2345                 dev_err(dev, "pm_runtime_get_sync() failed\n");
2346                 pm_runtime_disable(dev);
2347                 return ret;
2348         }
2349
2350         /* Get eDMA3 configuration from IP */
2351         ret = edma_setup_from_hw(dev, info, ecc);
2352         if (ret)
2353                 goto err_disable_pm;
2354
2355         /* Allocate memory based on the information we got from the IP */
2356         ecc->slave_chans = devm_kcalloc(dev, ecc->num_channels,
2357                                         sizeof(*ecc->slave_chans), GFP_KERNEL);
2358
2359         ecc->slot_inuse = devm_kcalloc(dev, BITS_TO_LONGS(ecc->num_slots),
2360                                        sizeof(unsigned long), GFP_KERNEL);
2361
2362         ecc->channels_mask = devm_kcalloc(dev,
2363                                            BITS_TO_LONGS(ecc->num_channels),
2364                                            sizeof(unsigned long), GFP_KERNEL);
2365         if (!ecc->slave_chans || !ecc->slot_inuse || !ecc->channels_mask) {
2366                 ret = -ENOMEM;
2367                 goto err_disable_pm;
2368         }
2369
2370         /* Mark all channels available initially */
2371         bitmap_fill(ecc->channels_mask, ecc->num_channels);
2372
2373         ecc->default_queue = info->default_queue;
2374
2375         if (info->rsv) {
2376                 /* Set the reserved slots in inuse list */
2377                 reserved = info->rsv->rsv_slots;
2378                 if (reserved) {
2379                         for (i = 0; reserved[i][0] != -1; i++)
2380                                 bitmap_set(ecc->slot_inuse, reserved[i][0],
2381                                            reserved[i][1]);
2382                 }
2383
2384                 /* Clear channels not usable for Linux */
2385                 reserved = info->rsv->rsv_chans;
2386                 if (reserved) {
2387                         for (i = 0; reserved[i][0] != -1; i++)
2388                                 bitmap_clear(ecc->channels_mask, reserved[i][0],
2389                                              reserved[i][1]);
2390                 }
2391         }
2392
2393         for (i = 0; i < ecc->num_slots; i++) {
2394                 /* Reset only unused - not reserved - paRAM slots */
2395                 if (!test_bit(i, ecc->slot_inuse))
2396                         edma_write_slot(ecc, i, &dummy_paramset);
2397         }
2398
2399         irq = platform_get_irq_byname(pdev, "edma3_ccint");
2400         if (irq < 0 && node)
2401                 irq = irq_of_parse_and_map(node, 0);
2402
2403         if (irq > 0) {
2404                 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccint",
2405                                           dev_name(dev));
2406                 if (!irq_name) {
2407                         ret = -ENOMEM;
2408                         goto err_disable_pm;
2409                 }
2410
2411                 ret = devm_request_irq(dev, irq, dma_irq_handler, 0, irq_name,
2412                                        ecc);
2413                 if (ret) {
2414                         dev_err(dev, "CCINT (%d) failed --> %d\n", irq, ret);
2415                         goto err_disable_pm;
2416                 }
2417                 ecc->ccint = irq;
2418         }
2419
2420         irq = platform_get_irq_byname(pdev, "edma3_ccerrint");
2421         if (irq < 0 && node)
2422                 irq = irq_of_parse_and_map(node, 2);
2423
2424         if (irq > 0) {
2425                 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccerrint",
2426                                           dev_name(dev));
2427                 if (!irq_name) {
2428                         ret = -ENOMEM;
2429                         goto err_disable_pm;
2430                 }
2431
2432                 ret = devm_request_irq(dev, irq, dma_ccerr_handler, 0, irq_name,
2433                                        ecc);
2434                 if (ret) {
2435                         dev_err(dev, "CCERRINT (%d) failed --> %d\n", irq, ret);
2436                         goto err_disable_pm;
2437                 }
2438                 ecc->ccerrint = irq;
2439         }
2440
2441         ecc->dummy_slot = edma_alloc_slot(ecc, EDMA_SLOT_ANY);
2442         if (ecc->dummy_slot < 0) {
2443                 dev_err(dev, "Can't allocate PaRAM dummy slot\n");
2444                 ret = ecc->dummy_slot;
2445                 goto err_disable_pm;
2446         }
2447
2448         queue_priority_mapping = info->queue_priority_mapping;
2449
2450         if (!ecc->legacy_mode) {
2451                 int lowest_priority = 0;
2452                 unsigned int array_max;
2453                 struct of_phandle_args tc_args;
2454
2455                 ecc->tc_list = devm_kcalloc(dev, ecc->num_tc,
2456                                             sizeof(*ecc->tc_list), GFP_KERNEL);
2457                 if (!ecc->tc_list) {
2458                         ret = -ENOMEM;
2459                         goto err_reg1;
2460                 }
2461
2462                 for (i = 0; i < ecc->num_tc; i++) {
2463                         ret = of_parse_phandle_with_fixed_args(node, "ti,tptcs",
2464                                                                1, i, &tc_args);
2465                         if (ret)
2466                                 break;
2467
2468                         ecc->tc_list[i].id = i;
2469                         queue_priority_mapping[i][1] = tc_args.args[0];
2470                         if (queue_priority_mapping[i][1] > lowest_priority) {
2471                                 lowest_priority = queue_priority_mapping[i][1];
2472                                 info->default_queue = i;
2473                         }
2474                         of_node_put(tc_args.np);
2475                 }
2476
2477                 /* See if we have optional dma-channel-mask array */
2478                 array_max = DIV_ROUND_UP(ecc->num_channels, BITS_PER_TYPE(u32));
2479                 ret = of_property_read_variable_u32_array(node,
2480                                                 "dma-channel-mask",
2481                                                 (u32 *)ecc->channels_mask,
2482                                                 1, array_max);
2483                 if (ret > 0 && ret != array_max)
2484                         dev_warn(dev, "dma-channel-mask is not complete.\n");
2485                 else if (ret == -EOVERFLOW || ret == -ENODATA)
2486                         dev_warn(dev,
2487                                  "dma-channel-mask is out of range or empty\n");
2488         }
2489
2490         /* Event queue priority mapping */
2491         for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2492                 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2493                                               queue_priority_mapping[i][1]);
2494
2495         edma_write_array2(ecc, EDMA_DRAE, 0, 0, 0x0);
2496         edma_write_array2(ecc, EDMA_DRAE, 0, 1, 0x0);
2497         edma_write_array(ecc, EDMA_QRAE, 0, 0x0);
2498
2499         ecc->info = info;
2500
2501         /* Init the dma device and channels */
2502         edma_dma_init(ecc, legacy_mode);
2503
2504         for (i = 0; i < ecc->num_channels; i++) {
2505                 /* Do not touch reserved channels */
2506                 if (!test_bit(i, ecc->channels_mask))
2507                         continue;
2508
2509                 /* Assign all channels to the default queue */
2510                 edma_assign_channel_eventq(&ecc->slave_chans[i],
2511                                            info->default_queue);
2512                 /* Set entry slot to the dummy slot */
2513                 edma_set_chmap(&ecc->slave_chans[i], ecc->dummy_slot);
2514         }
2515
2516         ecc->dma_slave.filter.map = info->slave_map;
2517         ecc->dma_slave.filter.mapcnt = info->slavecnt;
2518         ecc->dma_slave.filter.fn = edma_filter_fn;
2519
2520         ret = dma_async_device_register(&ecc->dma_slave);
2521         if (ret) {
2522                 dev_err(dev, "slave ddev registration failed (%d)\n", ret);
2523                 goto err_reg1;
2524         }
2525
2526         if (ecc->dma_memcpy) {
2527                 ret = dma_async_device_register(ecc->dma_memcpy);
2528                 if (ret) {
2529                         dev_err(dev, "memcpy ddev registration failed (%d)\n",
2530                                 ret);
2531                         dma_async_device_unregister(&ecc->dma_slave);
2532                         goto err_reg1;
2533                 }
2534         }
2535
2536         if (node)
2537                 of_dma_controller_register(node, of_edma_xlate, ecc);
2538
2539         dev_info(dev, "TI EDMA DMA engine driver\n");
2540
2541         return 0;
2542
2543 err_reg1:
2544         edma_free_slot(ecc, ecc->dummy_slot);
2545 err_disable_pm:
2546         pm_runtime_put_sync(dev);
2547         pm_runtime_disable(dev);
2548         return ret;
2549 }
2550
2551 static void edma_cleanupp_vchan(struct dma_device *dmadev)
2552 {
2553         struct edma_chan *echan, *_echan;
2554
2555         list_for_each_entry_safe(echan, _echan,
2556                         &dmadev->channels, vchan.chan.device_node) {
2557                 list_del(&echan->vchan.chan.device_node);
2558                 tasklet_kill(&echan->vchan.task);
2559         }
2560 }
2561
2562 static void edma_remove(struct platform_device *pdev)
2563 {
2564         struct device *dev = &pdev->dev;
2565         struct edma_cc *ecc = dev_get_drvdata(dev);
2566
2567         devm_free_irq(dev, ecc->ccint, ecc);
2568         devm_free_irq(dev, ecc->ccerrint, ecc);
2569
2570         edma_cleanupp_vchan(&ecc->dma_slave);
2571
2572         if (dev->of_node)
2573                 of_dma_controller_free(dev->of_node);
2574         dma_async_device_unregister(&ecc->dma_slave);
2575         if (ecc->dma_memcpy)
2576                 dma_async_device_unregister(ecc->dma_memcpy);
2577         edma_free_slot(ecc, ecc->dummy_slot);
2578         pm_runtime_put_sync(dev);
2579         pm_runtime_disable(dev);
2580 }
2581
2582 #ifdef CONFIG_PM_SLEEP
2583 static int edma_pm_suspend(struct device *dev)
2584 {
2585         struct edma_cc *ecc = dev_get_drvdata(dev);
2586         struct edma_chan *echan = ecc->slave_chans;
2587         int i;
2588
2589         for (i = 0; i < ecc->num_channels; i++) {
2590                 if (echan[i].alloced)
2591                         edma_setup_interrupt(&echan[i], false);
2592         }
2593
2594         return 0;
2595 }
2596
2597 static int edma_pm_resume(struct device *dev)
2598 {
2599         struct edma_cc *ecc = dev_get_drvdata(dev);
2600         struct edma_chan *echan = ecc->slave_chans;
2601         int i;
2602         s8 (*queue_priority_mapping)[2];
2603
2604         /* re initialize dummy slot to dummy param set */
2605         edma_write_slot(ecc, ecc->dummy_slot, &dummy_paramset);
2606
2607         queue_priority_mapping = ecc->info->queue_priority_mapping;
2608
2609         /* Event queue priority mapping */
2610         for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2611                 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2612                                               queue_priority_mapping[i][1]);
2613
2614         for (i = 0; i < ecc->num_channels; i++) {
2615                 if (echan[i].alloced) {
2616                         /* ensure access through shadow region 0 */
2617                         edma_or_array2(ecc, EDMA_DRAE, 0,
2618                                        EDMA_REG_ARRAY_INDEX(i),
2619                                        EDMA_CHANNEL_BIT(i));
2620
2621                         edma_setup_interrupt(&echan[i], true);
2622
2623                         /* Set up channel -> slot mapping for the entry slot */
2624                         edma_set_chmap(&echan[i], echan[i].slot[0]);
2625                 }
2626         }
2627
2628         return 0;
2629 }
2630 #endif
2631
2632 static const struct dev_pm_ops edma_pm_ops = {
2633         SET_LATE_SYSTEM_SLEEP_PM_OPS(edma_pm_suspend, edma_pm_resume)
2634 };
2635
2636 static struct platform_driver edma_driver = {
2637         .probe          = edma_probe,
2638         .remove         = edma_remove,
2639         .driver = {
2640                 .name   = "edma",
2641                 .pm     = &edma_pm_ops,
2642                 .of_match_table = edma_of_ids,
2643         },
2644 };
2645
2646 static int edma_tptc_probe(struct platform_device *pdev)
2647 {
2648         pm_runtime_enable(&pdev->dev);
2649         return pm_runtime_get_sync(&pdev->dev);
2650 }
2651
2652 static struct platform_driver edma_tptc_driver = {
2653         .probe          = edma_tptc_probe,
2654         .driver = {
2655                 .name   = "edma3-tptc",
2656                 .of_match_table = edma_tptc_of_ids,
2657         },
2658 };
2659
2660 static bool edma_filter_fn(struct dma_chan *chan, void *param)
2661 {
2662         bool match = false;
2663
2664         if (chan->device->dev->driver == &edma_driver.driver) {
2665                 struct edma_chan *echan = to_edma_chan(chan);
2666                 unsigned ch_req = *(unsigned *)param;
2667                 if (ch_req == echan->ch_num) {
2668                         /* The channel is going to be used as HW synchronized */
2669                         echan->hw_triggered = true;
2670                         match = true;
2671                 }
2672         }
2673         return match;
2674 }
2675
2676 static int edma_init(void)
2677 {
2678         int ret;
2679
2680         ret = platform_driver_register(&edma_tptc_driver);
2681         if (ret)
2682                 return ret;
2683
2684         return platform_driver_register(&edma_driver);
2685 }
2686 subsys_initcall(edma_init);
2687
2688 static void __exit edma_exit(void)
2689 {
2690         platform_driver_unregister(&edma_driver);
2691         platform_driver_unregister(&edma_tptc_driver);
2692 }
2693 module_exit(edma_exit);
2694
2695 MODULE_AUTHOR("Matt Porter <[email protected]>");
2696 MODULE_DESCRIPTION("TI EDMA DMA engine driver");
2697 MODULE_LICENSE("GPL v2");
This page took 0.188865 seconds and 4 git commands to generate.