blob: 6d0e0bcc037928948dd7c81c2d8e7e1321470556 [file] [log] [blame]
Matt Porterc2dde5f2012-08-22 21:09:34 -04001/*
2 * TI EDMA DMA engine driver
3 *
4 * Copyright 2012 Texas Instruments
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
9 *
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/dmaengine.h>
17#include <linux/dma-mapping.h>
18#include <linux/err.h>
19#include <linux/init.h>
20#include <linux/interrupt.h>
21#include <linux/list.h>
22#include <linux/module.h>
23#include <linux/platform_device.h>
24#include <linux/slab.h>
25#include <linux/spinlock.h>
Peter Ujfalusied646102014-07-31 13:12:38 +030026#include <linux/of.h>
Peter Ujfalusidc9b60552015-10-14 14:42:47 +030027#include <linux/of_dma.h>
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +030028#include <linux/of_irq.h>
29#include <linux/of_address.h>
30#include <linux/of_device.h>
31#include <linux/pm_runtime.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040032
Matt Porter3ad7a422013-03-06 11:15:31 -050033#include <linux/platform_data/edma.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040034
Peter Ujfalusid88b1392018-04-25 11:45:03 +030035#include "../dmaengine.h"
36#include "../virt-dma.h"
Matt Porterc2dde5f2012-08-22 21:09:34 -040037
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +030038/* Offsets matching "struct edmacc_param" */
39#define PARM_OPT 0x00
40#define PARM_SRC 0x04
41#define PARM_A_B_CNT 0x08
42#define PARM_DST 0x0c
43#define PARM_SRC_DST_BIDX 0x10
44#define PARM_LINK_BCNTRLD 0x14
45#define PARM_SRC_DST_CIDX 0x18
46#define PARM_CCNT 0x1c
47
48#define PARM_SIZE 0x20
49
50/* Offsets for EDMA CC global channel registers and their shadows */
51#define SH_ER 0x00 /* 64 bits */
52#define SH_ECR 0x08 /* 64 bits */
53#define SH_ESR 0x10 /* 64 bits */
54#define SH_CER 0x18 /* 64 bits */
55#define SH_EER 0x20 /* 64 bits */
56#define SH_EECR 0x28 /* 64 bits */
57#define SH_EESR 0x30 /* 64 bits */
58#define SH_SER 0x38 /* 64 bits */
59#define SH_SECR 0x40 /* 64 bits */
60#define SH_IER 0x50 /* 64 bits */
61#define SH_IECR 0x58 /* 64 bits */
62#define SH_IESR 0x60 /* 64 bits */
63#define SH_IPR 0x68 /* 64 bits */
64#define SH_ICR 0x70 /* 64 bits */
65#define SH_IEVAL 0x78
66#define SH_QER 0x80
67#define SH_QEER 0x84
68#define SH_QEECR 0x88
69#define SH_QEESR 0x8c
70#define SH_QSER 0x90
71#define SH_QSECR 0x94
72#define SH_SIZE 0x200
73
74/* Offsets for EDMA CC global registers */
75#define EDMA_REV 0x0000
76#define EDMA_CCCFG 0x0004
77#define EDMA_QCHMAP 0x0200 /* 8 registers */
78#define EDMA_DMAQNUM 0x0240 /* 8 registers (4 on OMAP-L1xx) */
79#define EDMA_QDMAQNUM 0x0260
80#define EDMA_QUETCMAP 0x0280
81#define EDMA_QUEPRI 0x0284
82#define EDMA_EMR 0x0300 /* 64 bits */
83#define EDMA_EMCR 0x0308 /* 64 bits */
84#define EDMA_QEMR 0x0310
85#define EDMA_QEMCR 0x0314
86#define EDMA_CCERR 0x0318
87#define EDMA_CCERRCLR 0x031c
88#define EDMA_EEVAL 0x0320
89#define EDMA_DRAE 0x0340 /* 4 x 64 bits*/
90#define EDMA_QRAE 0x0380 /* 4 registers */
91#define EDMA_QUEEVTENTRY 0x0400 /* 2 x 16 registers */
92#define EDMA_QSTAT 0x0600 /* 2 registers */
93#define EDMA_QWMTHRA 0x0620
94#define EDMA_QWMTHRB 0x0624
95#define EDMA_CCSTAT 0x0640
96
97#define EDMA_M 0x1000 /* global channel registers */
98#define EDMA_ECR 0x1008
99#define EDMA_ECRH 0x100C
100#define EDMA_SHADOW0 0x2000 /* 4 shadow regions */
101#define EDMA_PARM 0x4000 /* PaRAM entries */
102
103#define PARM_OFFSET(param_no) (EDMA_PARM + ((param_no) << 5))
104
105#define EDMA_DCHMAP 0x0100 /* 64 registers */
106
107/* CCCFG register */
108#define GET_NUM_DMACH(x) (x & 0x7) /* bits 0-2 */
Dan Carpenterf5ea7ad2015-11-04 16:38:31 +0300109#define GET_NUM_QDMACH(x) ((x & 0x70) >> 4) /* bits 4-6 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300110#define GET_NUM_PAENTRY(x) ((x & 0x7000) >> 12) /* bits 12-14 */
111#define GET_NUM_EVQUE(x) ((x & 0x70000) >> 16) /* bits 16-18 */
112#define GET_NUM_REGN(x) ((x & 0x300000) >> 20) /* bits 20-21 */
113#define CHMAP_EXIST BIT(24)
114
John Ogness4ac31d12016-01-28 11:29:08 +0100115/* CCSTAT register */
116#define EDMA_CCSTAT_ACTV BIT(4)
117
Matt Porterc2dde5f2012-08-22 21:09:34 -0400118/*
Joel Fernandes2abd5f12013-09-23 18:05:15 -0500119 * Max of 20 segments per channel to conserve PaRAM slots
120 * Also note that MAX_NR_SG should be atleast the no.of periods
121 * that are required for ASoC, otherwise DMA prep calls will
122 * fail. Today davinci-pcm is the only user of this driver and
123 * requires atleast 17 slots, so we setup the default to 20.
124 */
125#define MAX_NR_SG 20
Matt Porterc2dde5f2012-08-22 21:09:34 -0400126#define EDMA_MAX_SLOTS MAX_NR_SG
127#define EDMA_DESCRIPTORS 16
128
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300129#define EDMA_CHANNEL_ANY -1 /* for edma_alloc_channel() */
130#define EDMA_SLOT_ANY -1 /* for edma_alloc_slot() */
131#define EDMA_CONT_PARAMS_ANY 1001
132#define EDMA_CONT_PARAMS_FIXED_EXACT 1002
133#define EDMA_CONT_PARAMS_FIXED_NOT_EXACT 1003
134
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300135/*
136 * 64bit array registers are split into two 32bit registers:
137 * reg0: channel/event 0-31
138 * reg1: channel/event 32-63
139 *
140 * bit 5 in the channel number tells the array index (0/1)
141 * bit 0-4 (0x1f) is the bit offset within the register
142 */
143#define EDMA_REG_ARRAY_INDEX(channel) ((channel) >> 5)
144#define EDMA_CHANNEL_BIT(channel) (BIT((channel) & 0x1f))
145
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300146/* PaRAM slots are laid out like this */
147struct edmacc_param {
148 u32 opt;
149 u32 src;
150 u32 a_b_cnt;
151 u32 dst;
152 u32 src_dst_bidx;
153 u32 link_bcntrld;
154 u32 src_dst_cidx;
155 u32 ccnt;
156} __packed;
157
158/* fields in edmacc_param.opt */
159#define SAM BIT(0)
160#define DAM BIT(1)
161#define SYNCDIM BIT(2)
162#define STATIC BIT(3)
163#define EDMA_FWID (0x07 << 8)
164#define TCCMODE BIT(11)
165#define EDMA_TCC(t) ((t) << 12)
166#define TCINTEN BIT(20)
167#define ITCINTEN BIT(21)
168#define TCCHEN BIT(22)
169#define ITCCHEN BIT(23)
170
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500171struct edma_pset {
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500172 u32 len;
173 dma_addr_t addr;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500174 struct edmacc_param param;
175};
176
Matt Porterc2dde5f2012-08-22 21:09:34 -0400177struct edma_desc {
178 struct virt_dma_desc vdesc;
179 struct list_head node;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500180 enum dma_transfer_direction direction;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500181 int cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400182 int absync;
183 int pset_nr;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500184 struct edma_chan *echan;
Joel Fernandes04361d82014-04-28 15:19:31 -0500185 int processed;
186
187 /*
188 * The following 4 elements are used for residue accounting.
189 *
190 * - processed_stat: the number of SG elements we have traversed
191 * so far to cover accounting. This is updated directly to processed
192 * during edma_callback and is always <= processed, because processed
193 * refers to the number of pending transfer (programmed to EDMA
194 * controller), where as processed_stat tracks number of transfers
195 * accounted for so far.
196 *
197 * - residue: The amount of bytes we have left to transfer for this desc
198 *
199 * - residue_stat: The residue in bytes of data we have covered
200 * so far for accounting. This is updated directly to residue
201 * during callbacks to keep it current.
202 *
203 * - sg_len: Tracks the length of the current intermediate transfer,
204 * this is required to update the residue during intermediate transfer
205 * completion callback.
206 */
207 int processed_stat;
208 u32 sg_len;
209 u32 residue;
210 u32 residue_stat;
211
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500212 struct edma_pset pset[0];
Matt Porterc2dde5f2012-08-22 21:09:34 -0400213};
214
215struct edma_cc;
216
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300217struct edma_tc {
218 struct device_node *node;
219 u16 id;
220};
221
Matt Porterc2dde5f2012-08-22 21:09:34 -0400222struct edma_chan {
223 struct virt_dma_chan vchan;
224 struct list_head node;
225 struct edma_desc *edesc;
226 struct edma_cc *ecc;
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300227 struct edma_tc *tc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400228 int ch_num;
229 bool alloced;
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300230 bool hw_triggered;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400231 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -0500232 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -0500233 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400234};
235
236struct edma_cc {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300237 struct device *dev;
238 struct edma_soc_info *info;
239 void __iomem *base;
240 int id;
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300241 bool legacy_mode;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300242
243 /* eDMA3 resource information */
244 unsigned num_channels;
Peter Ujfalusi633e42b2015-10-16 10:18:04 +0300245 unsigned num_qchannels;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300246 unsigned num_region;
247 unsigned num_slots;
248 unsigned num_tc;
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +0300249 bool chmap_exist;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300250 enum dma_event_q default_queue;
251
Vinod Koul638001e2016-07-01 11:34:35 +0530252 unsigned int ccint;
253 unsigned int ccerrint;
254
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300255 /*
256 * The slot_inuse bit for each PaRAM slot is clear unless the slot is
257 * in use by Linux or if it is allocated to be used by DSP.
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300258 */
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300259 unsigned long *slot_inuse;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300260
Matt Porterc2dde5f2012-08-22 21:09:34 -0400261 struct dma_device dma_slave;
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300262 struct dma_device *dma_memcpy;
Peter Ujfalusicb782052015-10-14 14:42:54 +0300263 struct edma_chan *slave_chans;
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300264 struct edma_tc *tc_list;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400265 int dummy_slot;
266};
267
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300268/* dummy param set used to (re)initialize parameter RAM slots */
269static const struct edmacc_param dummy_paramset = {
270 .link_bcntrld = 0xffff,
271 .ccnt = 1,
272};
273
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300274#define EDMA_BINDING_LEGACY 0
275#define EDMA_BINDING_TPCC 1
Peter Ujfalusib7862742016-09-21 15:41:28 +0300276static const u32 edma_binding_type[] = {
277 [EDMA_BINDING_LEGACY] = EDMA_BINDING_LEGACY,
278 [EDMA_BINDING_TPCC] = EDMA_BINDING_TPCC,
279};
280
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300281static const struct of_device_id edma_of_ids[] = {
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300282 {
283 .compatible = "ti,edma3",
Peter Ujfalusib7862742016-09-21 15:41:28 +0300284 .data = &edma_binding_type[EDMA_BINDING_LEGACY],
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300285 },
286 {
287 .compatible = "ti,edma3-tpcc",
Peter Ujfalusib7862742016-09-21 15:41:28 +0300288 .data = &edma_binding_type[EDMA_BINDING_TPCC],
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300289 },
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300290 {}
291};
Peter Ujfalusi86737512016-09-21 15:41:27 +0300292MODULE_DEVICE_TABLE(of, edma_of_ids);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300293
Peter Ujfalusi34635b1a2015-11-02 15:21:40 +0200294static const struct of_device_id edma_tptc_of_ids[] = {
295 { .compatible = "ti,edma3-tptc", },
296 {}
297};
Peter Ujfalusi86737512016-09-21 15:41:27 +0300298MODULE_DEVICE_TABLE(of, edma_tptc_of_ids);
Peter Ujfalusi34635b1a2015-11-02 15:21:40 +0200299
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300300static inline unsigned int edma_read(struct edma_cc *ecc, int offset)
301{
302 return (unsigned int)__raw_readl(ecc->base + offset);
303}
304
305static inline void edma_write(struct edma_cc *ecc, int offset, int val)
306{
307 __raw_writel(val, ecc->base + offset);
308}
309
310static inline void edma_modify(struct edma_cc *ecc, int offset, unsigned and,
311 unsigned or)
312{
313 unsigned val = edma_read(ecc, offset);
314
315 val &= and;
316 val |= or;
317 edma_write(ecc, offset, val);
318}
319
320static inline void edma_and(struct edma_cc *ecc, int offset, unsigned and)
321{
322 unsigned val = edma_read(ecc, offset);
323
324 val &= and;
325 edma_write(ecc, offset, val);
326}
327
328static inline void edma_or(struct edma_cc *ecc, int offset, unsigned or)
329{
330 unsigned val = edma_read(ecc, offset);
331
332 val |= or;
333 edma_write(ecc, offset, val);
334}
335
336static inline unsigned int edma_read_array(struct edma_cc *ecc, int offset,
337 int i)
338{
339 return edma_read(ecc, offset + (i << 2));
340}
341
342static inline void edma_write_array(struct edma_cc *ecc, int offset, int i,
343 unsigned val)
344{
345 edma_write(ecc, offset + (i << 2), val);
346}
347
348static inline void edma_modify_array(struct edma_cc *ecc, int offset, int i,
349 unsigned and, unsigned or)
350{
351 edma_modify(ecc, offset + (i << 2), and, or);
352}
353
354static inline void edma_or_array(struct edma_cc *ecc, int offset, int i,
355 unsigned or)
356{
357 edma_or(ecc, offset + (i << 2), or);
358}
359
360static inline void edma_or_array2(struct edma_cc *ecc, int offset, int i, int j,
361 unsigned or)
362{
363 edma_or(ecc, offset + ((i * 2 + j) << 2), or);
364}
365
366static inline void edma_write_array2(struct edma_cc *ecc, int offset, int i,
367 int j, unsigned val)
368{
369 edma_write(ecc, offset + ((i * 2 + j) << 2), val);
370}
371
372static inline unsigned int edma_shadow0_read(struct edma_cc *ecc, int offset)
373{
374 return edma_read(ecc, EDMA_SHADOW0 + offset);
375}
376
377static inline unsigned int edma_shadow0_read_array(struct edma_cc *ecc,
378 int offset, int i)
379{
380 return edma_read(ecc, EDMA_SHADOW0 + offset + (i << 2));
381}
382
383static inline void edma_shadow0_write(struct edma_cc *ecc, int offset,
384 unsigned val)
385{
386 edma_write(ecc, EDMA_SHADOW0 + offset, val);
387}
388
389static inline void edma_shadow0_write_array(struct edma_cc *ecc, int offset,
390 int i, unsigned val)
391{
392 edma_write(ecc, EDMA_SHADOW0 + offset + (i << 2), val);
393}
394
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300395static inline unsigned int edma_param_read(struct edma_cc *ecc, int offset,
396 int param_no)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300397{
398 return edma_read(ecc, EDMA_PARM + offset + (param_no << 5));
399}
400
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300401static inline void edma_param_write(struct edma_cc *ecc, int offset,
402 int param_no, unsigned val)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300403{
404 edma_write(ecc, EDMA_PARM + offset + (param_no << 5), val);
405}
406
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300407static inline void edma_param_modify(struct edma_cc *ecc, int offset,
408 int param_no, unsigned and, unsigned or)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300409{
410 edma_modify(ecc, EDMA_PARM + offset + (param_no << 5), and, or);
411}
412
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300413static inline void edma_param_and(struct edma_cc *ecc, int offset, int param_no,
414 unsigned and)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300415{
416 edma_and(ecc, EDMA_PARM + offset + (param_no << 5), and);
417}
418
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300419static inline void edma_param_or(struct edma_cc *ecc, int offset, int param_no,
420 unsigned or)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300421{
422 edma_or(ecc, EDMA_PARM + offset + (param_no << 5), or);
423}
424
Peter Ujfalusi1634d302016-09-22 09:31:04 +0300425static inline void edma_set_bits(int offset, int len, unsigned long *p)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300426{
427 for (; len > 0; len--)
428 set_bit(offset + (len - 1), p);
429}
430
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300431static void edma_assign_priority_to_queue(struct edma_cc *ecc, int queue_no,
432 int priority)
433{
434 int bit = queue_no * 4;
435
436 edma_modify(ecc, EDMA_QUEPRI, ~(0x7 << bit), ((priority & 0x7) << bit));
437}
438
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300439static void edma_set_chmap(struct edma_chan *echan, int slot)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300440{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300441 struct edma_cc *ecc = echan->ecc;
442 int channel = EDMA_CHAN_SLOT(echan->ch_num);
443
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300444 if (ecc->chmap_exist) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300445 slot = EDMA_CHAN_SLOT(slot);
446 edma_write_array(ecc, EDMA_DCHMAP, channel, (slot << 5));
447 }
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300448}
449
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300450static void edma_setup_interrupt(struct edma_chan *echan, bool enable)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300451{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300452 struct edma_cc *ecc = echan->ecc;
453 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300454 int idx = EDMA_REG_ARRAY_INDEX(channel);
455 int ch_bit = EDMA_CHANNEL_BIT(channel);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300456
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300457 if (enable) {
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300458 edma_shadow0_write_array(ecc, SH_ICR, idx, ch_bit);
459 edma_shadow0_write_array(ecc, SH_IESR, idx, ch_bit);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300460 } else {
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300461 edma_shadow0_write_array(ecc, SH_IECR, idx, ch_bit);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300462 }
463}
464
465/*
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300466 * paRAM slot management functions
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300467 */
468static void edma_write_slot(struct edma_cc *ecc, unsigned slot,
469 const struct edmacc_param *param)
470{
471 slot = EDMA_CHAN_SLOT(slot);
472 if (slot >= ecc->num_slots)
473 return;
474 memcpy_toio(ecc->base + PARM_OFFSET(slot), param, PARM_SIZE);
475}
476
Arnd Bergmann2cc40ee2016-09-30 18:19:01 +0200477static int edma_read_slot(struct edma_cc *ecc, unsigned slot,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300478 struct edmacc_param *param)
479{
480 slot = EDMA_CHAN_SLOT(slot);
481 if (slot >= ecc->num_slots)
Arnd Bergmann2cc40ee2016-09-30 18:19:01 +0200482 return -EINVAL;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300483 memcpy_fromio(param, ecc->base + PARM_OFFSET(slot), PARM_SIZE);
Arnd Bergmann2cc40ee2016-09-30 18:19:01 +0200484
485 return 0;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300486}
487
488/**
489 * edma_alloc_slot - allocate DMA parameter RAM
490 * @ecc: pointer to edma_cc struct
491 * @slot: specific slot to allocate; negative for "any unused slot"
492 *
493 * This allocates a parameter RAM slot, initializing it to hold a
494 * dummy transfer. Slots allocated using this routine have not been
495 * mapped to a hardware DMA channel, and will normally be used by
496 * linking to them from a slot associated with a DMA channel.
497 *
498 * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
499 * slots may be allocated on behalf of DSP firmware.
500 *
501 * Returns the number of the slot, else negative errno.
502 */
503static int edma_alloc_slot(struct edma_cc *ecc, int slot)
504{
Peter Ujfalusid20313b2016-01-11 10:38:01 +0200505 if (slot >= 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300506 slot = EDMA_CHAN_SLOT(slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300507 /* Requesting entry paRAM slot for a HW triggered channel. */
508 if (ecc->chmap_exist && slot < ecc->num_channels)
509 slot = EDMA_SLOT_ANY;
510 }
511
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300512 if (slot < 0) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300513 if (ecc->chmap_exist)
514 slot = 0;
515 else
516 slot = ecc->num_channels;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300517 for (;;) {
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300518 slot = find_next_zero_bit(ecc->slot_inuse,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300519 ecc->num_slots,
520 slot);
521 if (slot == ecc->num_slots)
522 return -ENOMEM;
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300523 if (!test_and_set_bit(slot, ecc->slot_inuse))
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300524 break;
525 }
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300526 } else if (slot >= ecc->num_slots) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300527 return -EINVAL;
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300528 } else if (test_and_set_bit(slot, ecc->slot_inuse)) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300529 return -EBUSY;
530 }
531
532 edma_write_slot(ecc, slot, &dummy_paramset);
533
534 return EDMA_CTLR_CHAN(ecc->id, slot);
535}
536
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300537static void edma_free_slot(struct edma_cc *ecc, unsigned slot)
538{
539 slot = EDMA_CHAN_SLOT(slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300540 if (slot >= ecc->num_slots)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300541 return;
542
543 edma_write_slot(ecc, slot, &dummy_paramset);
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300544 clear_bit(slot, ecc->slot_inuse);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300545}
546
547/**
548 * edma_link - link one parameter RAM slot to another
549 * @ecc: pointer to edma_cc struct
550 * @from: parameter RAM slot originating the link
551 * @to: parameter RAM slot which is the link target
552 *
553 * The originating slot should not be part of any active DMA transfer.
554 */
555static void edma_link(struct edma_cc *ecc, unsigned from, unsigned to)
556{
Peter Ujfalusifc014092015-10-14 14:42:59 +0300557 if (unlikely(EDMA_CTLR(from) != EDMA_CTLR(to)))
558 dev_warn(ecc->dev, "Ignoring eDMA instance for linking\n");
559
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300560 from = EDMA_CHAN_SLOT(from);
561 to = EDMA_CHAN_SLOT(to);
562 if (from >= ecc->num_slots || to >= ecc->num_slots)
563 return;
564
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300565 edma_param_modify(ecc, PARM_LINK_BCNTRLD, from, 0xffff0000,
566 PARM_OFFSET(to));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300567}
568
569/**
570 * edma_get_position - returns the current transfer point
571 * @ecc: pointer to edma_cc struct
572 * @slot: parameter RAM slot being examined
573 * @dst: true selects the dest position, false the source
574 *
575 * Returns the position of the current active slot
576 */
577static dma_addr_t edma_get_position(struct edma_cc *ecc, unsigned slot,
578 bool dst)
579{
580 u32 offs;
581
582 slot = EDMA_CHAN_SLOT(slot);
583 offs = PARM_OFFSET(slot);
584 offs += dst ? PARM_DST : PARM_SRC;
585
586 return edma_read(ecc, offs);
587}
588
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300589/*
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300590 * Channels with event associations will be triggered by their hardware
591 * events, and channels without such associations will be triggered by
592 * software. (At this writing there is no interface for using software
593 * triggers except with channels that don't support hardware triggers.)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300594 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300595static void edma_start(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300596{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300597 struct edma_cc *ecc = echan->ecc;
598 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300599 int idx = EDMA_REG_ARRAY_INDEX(channel);
600 int ch_bit = EDMA_CHANNEL_BIT(channel);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300601
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300602 if (!echan->hw_triggered) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300603 /* EDMA channels without event association */
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300604 dev_dbg(ecc->dev, "ESR%d %08x\n", idx,
605 edma_shadow0_read_array(ecc, SH_ESR, idx));
606 edma_shadow0_write_array(ecc, SH_ESR, idx, ch_bit);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300607 } else {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300608 /* EDMA channel with event association */
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300609 dev_dbg(ecc->dev, "ER%d %08x\n", idx,
610 edma_shadow0_read_array(ecc, SH_ER, idx));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300611 /* Clear any pending event or error */
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300612 edma_write_array(ecc, EDMA_ECR, idx, ch_bit);
613 edma_write_array(ecc, EDMA_EMCR, idx, ch_bit);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300614 /* Clear any SER */
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300615 edma_shadow0_write_array(ecc, SH_SECR, idx, ch_bit);
616 edma_shadow0_write_array(ecc, SH_EESR, idx, ch_bit);
617 dev_dbg(ecc->dev, "EER%d %08x\n", idx,
618 edma_shadow0_read_array(ecc, SH_EER, idx));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300619 }
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300620}
621
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300622static void edma_stop(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300623{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300624 struct edma_cc *ecc = echan->ecc;
625 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300626 int idx = EDMA_REG_ARRAY_INDEX(channel);
627 int ch_bit = EDMA_CHANNEL_BIT(channel);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300628
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300629 edma_shadow0_write_array(ecc, SH_EECR, idx, ch_bit);
630 edma_shadow0_write_array(ecc, SH_ECR, idx, ch_bit);
631 edma_shadow0_write_array(ecc, SH_SECR, idx, ch_bit);
632 edma_write_array(ecc, EDMA_EMCR, idx, ch_bit);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300633
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300634 /* clear possibly pending completion interrupt */
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300635 edma_shadow0_write_array(ecc, SH_ICR, idx, ch_bit);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300636
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300637 dev_dbg(ecc->dev, "EER%d %08x\n", idx,
638 edma_shadow0_read_array(ecc, SH_EER, idx));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300639
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300640 /* REVISIT: consider guarding against inappropriate event
641 * chaining by overwriting with dummy_paramset.
642 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300643}
644
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300645/*
646 * Temporarily disable EDMA hardware events on the specified channel,
647 * preventing them from triggering new transfers
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300648 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300649static void edma_pause(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300650{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300651 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300652
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300653 edma_shadow0_write_array(echan->ecc, SH_EECR,
654 EDMA_REG_ARRAY_INDEX(channel),
655 EDMA_CHANNEL_BIT(channel));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300656}
657
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300658/* Re-enable EDMA hardware events on the specified channel. */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300659static void edma_resume(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300660{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300661 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300662
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300663 edma_shadow0_write_array(echan->ecc, SH_EESR,
664 EDMA_REG_ARRAY_INDEX(channel),
665 EDMA_CHANNEL_BIT(channel));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300666}
667
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300668static void edma_trigger_channel(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300669{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300670 struct edma_cc *ecc = echan->ecc;
671 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300672 int idx = EDMA_REG_ARRAY_INDEX(channel);
673 int ch_bit = EDMA_CHANNEL_BIT(channel);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300674
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300675 edma_shadow0_write_array(ecc, SH_ESR, idx, ch_bit);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300676
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300677 dev_dbg(ecc->dev, "ESR%d %08x\n", idx,
678 edma_shadow0_read_array(ecc, SH_ESR, idx));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300679}
680
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300681static void edma_clean_channel(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300682{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300683 struct edma_cc *ecc = echan->ecc;
684 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300685 int idx = EDMA_REG_ARRAY_INDEX(channel);
686 int ch_bit = EDMA_CHANNEL_BIT(channel);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300687
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300688 dev_dbg(ecc->dev, "EMR%d %08x\n", idx,
689 edma_read_array(ecc, EDMA_EMR, idx));
690 edma_shadow0_write_array(ecc, SH_ECR, idx, ch_bit);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300691 /* Clear the corresponding EMR bits */
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300692 edma_write_array(ecc, EDMA_EMCR, idx, ch_bit);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300693 /* Clear any SER */
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300694 edma_shadow0_write_array(ecc, SH_SECR, idx, ch_bit);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300695 edma_write(ecc, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300696}
697
Peter Ujfalusif9425de2015-10-16 10:18:03 +0300698/* Move channel to a specific event queue */
699static void edma_assign_channel_eventq(struct edma_chan *echan,
700 enum dma_event_q eventq_no)
701{
702 struct edma_cc *ecc = echan->ecc;
703 int channel = EDMA_CHAN_SLOT(echan->ch_num);
704 int bit = (channel & 0x7) * 4;
705
706 /* default to low priority queue */
707 if (eventq_no == EVENTQ_DEFAULT)
708 eventq_no = ecc->default_queue;
709 if (eventq_no >= ecc->num_tc)
710 return;
711
712 eventq_no &= 7;
713 edma_modify_array(ecc, EDMA_DMAQNUM, (channel >> 3), ~(0x7 << bit),
714 eventq_no << bit);
715}
716
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300717static int edma_alloc_channel(struct edma_chan *echan,
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300718 enum dma_event_q eventq_no)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300719{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300720 struct edma_cc *ecc = echan->ecc;
721 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300722
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300723 /* ensure access through shadow region 0 */
Peter Ujfalusie96b1f62019-07-16 11:26:53 +0300724 edma_or_array2(ecc, EDMA_DRAE, 0, EDMA_REG_ARRAY_INDEX(channel),
725 EDMA_CHANNEL_BIT(channel));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300726
727 /* ensure no events are pending */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300728 edma_stop(echan);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300729
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300730 edma_setup_interrupt(echan, true);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300731
Peter Ujfalusif9425de2015-10-16 10:18:03 +0300732 edma_assign_channel_eventq(echan, eventq_no);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300733
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300734 return 0;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300735}
736
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300737static void edma_free_channel(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300738{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300739 /* ensure no events are pending */
740 edma_stop(echan);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300741 /* REVISIT should probably take out of shadow region 0 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300742 edma_setup_interrupt(echan, false);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300743}
744
Matt Porterc2dde5f2012-08-22 21:09:34 -0400745static inline struct edma_cc *to_edma_cc(struct dma_device *d)
746{
747 return container_of(d, struct edma_cc, dma_slave);
748}
749
750static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
751{
752 return container_of(c, struct edma_chan, vchan.chan);
753}
754
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300755static inline struct edma_desc *to_edma_desc(struct dma_async_tx_descriptor *tx)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400756{
757 return container_of(tx, struct edma_desc, vdesc.tx);
758}
759
760static void edma_desc_free(struct virt_dma_desc *vdesc)
761{
762 kfree(container_of(vdesc, struct edma_desc, vdesc));
763}
764
765/* Dispatch a queued descriptor to the controller (caller holds lock) */
766static void edma_execute(struct edma_chan *echan)
767{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300768 struct edma_cc *ecc = echan->ecc;
Joel Fernandes53407062013-09-03 10:02:46 -0500769 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400770 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500771 struct device *dev = echan->vchan.chan.device->dev;
772 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400773
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300774 if (!echan->edesc) {
775 /* Setup is needed for the first transfer */
Joel Fernandes53407062013-09-03 10:02:46 -0500776 vdesc = vchan_next_desc(&echan->vchan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300777 if (!vdesc)
Joel Fernandes53407062013-09-03 10:02:46 -0500778 return;
Joel Fernandes53407062013-09-03 10:02:46 -0500779 list_del(&vdesc->node);
780 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400781 }
782
Joel Fernandes53407062013-09-03 10:02:46 -0500783 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400784
Joel Fernandes53407062013-09-03 10:02:46 -0500785 /* Find out how many left */
786 left = edesc->pset_nr - edesc->processed;
787 nslots = min(MAX_NR_SG, left);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500788 edesc->sg_len = 0;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400789
790 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500791 for (i = 0; i < nslots; i++) {
792 j = i + edesc->processed;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300793 edma_write_slot(ecc, echan->slot[i], &edesc->pset[j].param);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500794 edesc->sg_len += edesc->pset[j].len;
Peter Ujfalusi907f74a2015-10-14 14:42:56 +0300795 dev_vdbg(dev,
796 "\n pset[%d]:\n"
797 " chnum\t%d\n"
798 " slot\t%d\n"
799 " opt\t%08x\n"
800 " src\t%08x\n"
801 " dst\t%08x\n"
802 " abcnt\t%08x\n"
803 " ccnt\t%08x\n"
804 " bidx\t%08x\n"
805 " cidx\t%08x\n"
806 " lkrld\t%08x\n",
807 j, echan->ch_num, echan->slot[i],
808 edesc->pset[j].param.opt,
809 edesc->pset[j].param.src,
810 edesc->pset[j].param.dst,
811 edesc->pset[j].param.a_b_cnt,
812 edesc->pset[j].param.ccnt,
813 edesc->pset[j].param.src_dst_bidx,
814 edesc->pset[j].param.src_dst_cidx,
815 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400816 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500817 if (i != (nslots - 1))
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300818 edma_link(ecc, echan->slot[i], echan->slot[i + 1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400819 }
820
Joel Fernandes53407062013-09-03 10:02:46 -0500821 edesc->processed += nslots;
822
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500823 /*
824 * If this is either the last set in a set of SG-list transactions
825 * then setup a link to the dummy slot, this results in all future
826 * events being absorbed and that's OK because we're done
827 */
Joel Fernandes50a9c702013-10-31 16:31:23 -0500828 if (edesc->processed == edesc->pset_nr) {
829 if (edesc->cyclic)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300830 edma_link(ecc, echan->slot[nslots - 1], echan->slot[1]);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500831 else
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300832 edma_link(ecc, echan->slot[nslots - 1],
Joel Fernandes50a9c702013-10-31 16:31:23 -0500833 echan->ecc->dummy_slot);
834 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500835
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300836 if (echan->missed) {
837 /*
838 * This happens due to setup times between intermediate
839 * transfers in long SG lists which have to be broken up into
840 * transfers of MAX_NR_SG
841 */
842 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300843 edma_clean_channel(echan);
844 edma_stop(echan);
845 edma_start(echan);
846 edma_trigger_channel(echan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300847 echan->missed = 0;
848 } else if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300849 dev_dbg(dev, "first transfer starting on channel %d\n",
850 echan->ch_num);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300851 edma_start(echan);
Sekhar Nori5fc68a62014-03-19 11:25:50 +0530852 } else {
853 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
854 echan->ch_num, edesc->processed);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300855 edma_resume(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500856 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400857}
858
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100859static int edma_terminate_all(struct dma_chan *chan)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400860{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100861 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400862 unsigned long flags;
863 LIST_HEAD(head);
864
865 spin_lock_irqsave(&echan->vchan.lock, flags);
866
867 /*
868 * Stop DMA activity: we assume the callback will not be called
869 * after edma_dma() returns (even if it does, it will see
870 * echan->edesc is NULL and exit.)
871 */
872 if (echan->edesc) {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300873 edma_stop(echan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300874 /* Move the cyclic channel back to default queue */
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300875 if (!echan->tc && echan->edesc->cyclic)
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300876 edma_assign_channel_eventq(echan, EVENTQ_DEFAULT);
Peter Ujfalusi174334bc2017-11-14 16:32:06 +0200877
878 vchan_terminate_vdesc(&echan->edesc->vdesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400879 echan->edesc = NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400880 }
881
882 vchan_get_all_descriptors(&echan->vchan, &head);
883 spin_unlock_irqrestore(&echan->vchan.lock, flags);
884 vchan_dma_desc_free_list(&echan->vchan, &head);
885
886 return 0;
887}
888
Peter Ujfalusib84730f2016-02-11 11:08:42 +0200889static void edma_synchronize(struct dma_chan *chan)
890{
891 struct edma_chan *echan = to_edma_chan(chan);
892
893 vchan_synchronize(&echan->vchan);
894}
895
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100896static int edma_slave_config(struct dma_chan *chan,
Matt Porter661f7cb2013-01-10 13:41:04 -0500897 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400898{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100899 struct edma_chan *echan = to_edma_chan(chan);
900
Matt Porter661f7cb2013-01-10 13:41:04 -0500901 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
902 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400903 return -EINVAL;
904
Peter Ujfalusiea09ea52017-10-03 11:35:37 +0300905 if (cfg->src_maxburst > chan->device->max_burst ||
906 cfg->dst_maxburst > chan->device->max_burst)
907 return -EINVAL;
908
Matt Porter661f7cb2013-01-10 13:41:04 -0500909 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400910
911 return 0;
912}
913
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100914static int edma_dma_pause(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300915{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100916 struct edma_chan *echan = to_edma_chan(chan);
917
John Ogness02ec6042015-04-27 13:52:25 +0200918 if (!echan->edesc)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300919 return -EINVAL;
920
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300921 edma_pause(echan);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300922 return 0;
923}
924
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100925static int edma_dma_resume(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300926{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100927 struct edma_chan *echan = to_edma_chan(chan);
928
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300929 edma_resume(echan);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300930 return 0;
931}
932
Joel Fernandesfd009032013-09-23 18:05:13 -0500933/*
934 * A PaRAM set configuration abstraction used by other modes
935 * @chan: Channel who's PaRAM set we're configuring
936 * @pset: PaRAM set to initialize and setup.
937 * @src_addr: Source address of the DMA
938 * @dst_addr: Destination address of the DMA
939 * @burst: In units of dev_width, how much to send
940 * @dev_width: How much is the dev_width
941 * @dma_length: Total length of the DMA transfer
942 * @direction: Direction of the transfer
943 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500944static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300945 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
Peter Ujfalusidf6694f2015-10-16 10:18:00 +0300946 unsigned int acnt, unsigned int dma_length,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300947 enum dma_transfer_direction direction)
Joel Fernandesfd009032013-09-23 18:05:13 -0500948{
949 struct edma_chan *echan = to_edma_chan(chan);
950 struct device *dev = chan->device->dev;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500951 struct edmacc_param *param = &epset->param;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +0300952 int bcnt, ccnt, cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500953 int src_bidx, dst_bidx, src_cidx, dst_cidx;
954 int absync;
955
Peter Ujfalusib2b617d2014-04-14 14:41:58 +0300956 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
957 if (!burst)
958 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -0500959 /*
960 * If the maxburst is equal to the fifo width, use
961 * A-synced transfers. This allows for large contiguous
962 * buffer transfers using only one PaRAM set.
963 */
964 if (burst == 1) {
965 /*
966 * For the A-sync case, bcnt and ccnt are the remainder
967 * and quotient respectively of the division of:
968 * (dma_length / acnt) by (SZ_64K -1). This is so
969 * that in case bcnt over flows, we have ccnt to use.
970 * Note: In A-sync tranfer only, bcntrld is used, but it
971 * only applies for sg_dma_len(sg) >= SZ_64K.
972 * In this case, the best way adopted is- bccnt for the
973 * first frame will be the remainder below. Then for
974 * every successive frame, bcnt will be SZ_64K-1. This
975 * is assured as bcntrld = 0xffff in end of function.
976 */
977 absync = false;
978 ccnt = dma_length / acnt / (SZ_64K - 1);
979 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
980 /*
981 * If bcnt is non-zero, we have a remainder and hence an
982 * extra frame to transfer, so increment ccnt.
983 */
984 if (bcnt)
985 ccnt++;
986 else
987 bcnt = SZ_64K - 1;
988 cidx = acnt;
989 } else {
990 /*
991 * If maxburst is greater than the fifo address_width,
992 * use AB-synced transfers where A count is the fifo
993 * address_width and B count is the maxburst. In this
994 * case, we are limited to transfers of C count frames
995 * of (address_width * maxburst) where C count is limited
996 * to SZ_64K-1. This places an upper bound on the length
997 * of an SG segment that can be handled.
998 */
999 absync = true;
1000 bcnt = burst;
1001 ccnt = dma_length / (acnt * bcnt);
1002 if (ccnt > (SZ_64K - 1)) {
1003 dev_err(dev, "Exceeded max SG segment size\n");
1004 return -EINVAL;
1005 }
1006 cidx = acnt * bcnt;
1007 }
1008
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001009 epset->len = dma_length;
1010
Joel Fernandesfd009032013-09-23 18:05:13 -05001011 if (direction == DMA_MEM_TO_DEV) {
1012 src_bidx = acnt;
1013 src_cidx = cidx;
1014 dst_bidx = 0;
1015 dst_cidx = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001016 epset->addr = src_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001017 } else if (direction == DMA_DEV_TO_MEM) {
1018 src_bidx = 0;
1019 src_cidx = 0;
1020 dst_bidx = acnt;
1021 dst_cidx = cidx;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001022 epset->addr = dst_addr;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001023 } else if (direction == DMA_MEM_TO_MEM) {
1024 src_bidx = acnt;
1025 src_cidx = cidx;
1026 dst_bidx = acnt;
1027 dst_cidx = cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001028 } else {
1029 dev_err(dev, "%s: direction not implemented yet\n", __func__);
1030 return -EINVAL;
1031 }
1032
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001033 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
Joel Fernandesfd009032013-09-23 18:05:13 -05001034 /* Configure A or AB synchronized transfers */
1035 if (absync)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001036 param->opt |= SYNCDIM;
Joel Fernandesfd009032013-09-23 18:05:13 -05001037
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001038 param->src = src_addr;
1039 param->dst = dst_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001040
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001041 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
1042 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001043
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001044 param->a_b_cnt = bcnt << 16 | acnt;
1045 param->ccnt = ccnt;
Joel Fernandesfd009032013-09-23 18:05:13 -05001046 /*
1047 * Only time when (bcntrld) auto reload is required is for
1048 * A-sync case, and in this case, a requirement of reload value
1049 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
1050 * and then later will be populated by edma_execute.
1051 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001052 param->link_bcntrld = 0xffffffff;
Joel Fernandesfd009032013-09-23 18:05:13 -05001053 return absync;
1054}
1055
Matt Porterc2dde5f2012-08-22 21:09:34 -04001056static struct dma_async_tx_descriptor *edma_prep_slave_sg(
1057 struct dma_chan *chan, struct scatterlist *sgl,
1058 unsigned int sg_len, enum dma_transfer_direction direction,
1059 unsigned long tx_flags, void *context)
1060{
1061 struct edma_chan *echan = to_edma_chan(chan);
1062 struct device *dev = chan->device->dev;
1063 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -05001064 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -05001065 enum dma_slave_buswidth dev_width;
1066 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001067 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -05001068 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001069
1070 if (unlikely(!echan || !sgl || !sg_len))
1071 return NULL;
1072
Matt Porter661f7cb2013-01-10 13:41:04 -05001073 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001074 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001075 dev_width = echan->cfg.src_addr_width;
1076 burst = echan->cfg.src_maxburst;
1077 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001078 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001079 dev_width = echan->cfg.dst_addr_width;
1080 burst = echan->cfg.dst_maxburst;
1081 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001082 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Matt Porter661f7cb2013-01-10 13:41:04 -05001083 return NULL;
1084 }
1085
1086 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001087 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001088 return NULL;
1089 }
1090
Kees Cookacafe7e2018-05-08 13:45:50 -07001091 edesc = kzalloc(struct_size(edesc, pset, sg_len), GFP_ATOMIC);
Peter Griffinaef94fe2016-06-07 18:38:41 +01001092 if (!edesc)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001093 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001094
1095 edesc->pset_nr = sg_len;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001096 edesc->residue = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001097 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001098 edesc->echan = echan;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001099
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001100 /* Allocate a PaRAM slot, if needed */
1101 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
1102
1103 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001104 if (echan->slot[i] < 0) {
1105 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001106 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001107 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +03001108 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001109 dev_err(dev, "%s: Failed to allocate slot\n",
1110 __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001111 return NULL;
1112 }
1113 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001114 }
1115
1116 /* Configure PaRAM sets for each SG */
1117 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001118 /* Get address for each SG */
1119 if (direction == DMA_DEV_TO_MEM)
1120 dst_addr = sg_dma_address(sg);
1121 else
1122 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001123
Joel Fernandesfd009032013-09-23 18:05:13 -05001124 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1125 dst_addr, burst, dev_width,
1126 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +05301127 if (ret < 0) {
1128 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -05001129 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001130 }
1131
Joel Fernandesfd009032013-09-23 18:05:13 -05001132 edesc->absync = ret;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001133 edesc->residue += sg_dma_len(sg);
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001134
Matt Porterc2dde5f2012-08-22 21:09:34 -04001135 if (i == sg_len - 1)
Peter Ujfalusi2e4ed082016-06-07 11:19:44 +03001136 /* Enable completion interrupt */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001137 edesc->pset[i].param.opt |= TCINTEN;
Peter Ujfalusi2e4ed082016-06-07 11:19:44 +03001138 else if (!((i+1) % MAX_NR_SG))
1139 /*
1140 * Enable early completion interrupt for the
1141 * intermediateset. In this case the driver will be
1142 * notified when the paRAM set is submitted to TC. This
1143 * will allow more time to set up the next set of slots.
1144 */
1145 edesc->pset[i].param.opt |= (TCINTEN | TCCMODE);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001146 }
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001147 edesc->residue_stat = edesc->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001148
Matt Porterc2dde5f2012-08-22 21:09:34 -04001149 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1150}
Matt Porterc2dde5f2012-08-22 21:09:34 -04001151
Lad, Prabhakarb7a4fd52015-02-04 13:03:27 +00001152static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001153 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1154 size_t len, unsigned long tx_flags)
1155{
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001156 int ret, nslots;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001157 struct edma_desc *edesc;
1158 struct device *dev = chan->device->dev;
1159 struct edma_chan *echan = to_edma_chan(chan);
Peter Ujfalusi87a2f6222017-09-18 11:16:26 +03001160 unsigned int width, pset_len, array_size;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001161
1162 if (unlikely(!echan || !len))
1163 return NULL;
1164
Peter Ujfalusi87a2f6222017-09-18 11:16:26 +03001165 /* Align the array size (acnt block) with the transfer properties */
1166 switch (__ffs((src | dest | len))) {
1167 case 0:
1168 array_size = SZ_32K - 1;
1169 break;
1170 case 1:
1171 array_size = SZ_32K - 2;
1172 break;
1173 default:
1174 array_size = SZ_32K - 4;
1175 break;
1176 }
1177
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001178 if (len < SZ_64K) {
1179 /*
1180 * Transfer size less than 64K can be handled with one paRAM
1181 * slot and with one burst.
1182 * ACNT = length
1183 */
1184 width = len;
1185 pset_len = len;
1186 nslots = 1;
1187 } else {
1188 /*
1189 * Transfer size bigger than 64K will be handled with maximum of
1190 * two paRAM slots.
1191 * slot1: (full_length / 32767) times 32767 bytes bursts.
1192 * ACNT = 32767, length1: (full_length / 32767) * 32767
1193 * slot2: the remaining amount of data after slot1.
1194 * ACNT = full_length - length1, length2 = ACNT
1195 *
1196 * When the full_length is multibple of 32767 one slot can be
1197 * used to complete the transfer.
1198 */
Peter Ujfalusi87a2f6222017-09-18 11:16:26 +03001199 width = array_size;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001200 pset_len = rounddown(len, width);
1201 /* One slot is enough for lengths multiple of (SZ_32K -1) */
1202 if (unlikely(pset_len == len))
1203 nslots = 1;
1204 else
1205 nslots = 2;
1206 }
1207
Kees Cookacafe7e2018-05-08 13:45:50 -07001208 edesc = kzalloc(struct_size(edesc, pset, nslots), GFP_ATOMIC);
Peter Griffinaef94fe2016-06-07 18:38:41 +01001209 if (!edesc)
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001210 return NULL;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001211
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001212 edesc->pset_nr = nslots;
1213 edesc->residue = edesc->residue_stat = len;
1214 edesc->direction = DMA_MEM_TO_MEM;
1215 edesc->echan = echan;
Peter Ujfalusi21a31842015-10-16 10:17:59 +03001216
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001217 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001218 width, pset_len, DMA_MEM_TO_MEM);
1219 if (ret < 0) {
1220 kfree(edesc);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001221 return NULL;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001222 }
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001223
1224 edesc->absync = ret;
1225
Joel Fernandesb0cce4c2014-04-28 15:30:32 -05001226 edesc->pset[0].param.opt |= ITCCHEN;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001227 if (nslots == 1) {
1228 /* Enable transfer complete interrupt */
1229 edesc->pset[0].param.opt |= TCINTEN;
1230 } else {
1231 /* Enable transfer complete chaining for the first slot */
1232 edesc->pset[0].param.opt |= TCCHEN;
1233
1234 if (echan->slot[1] < 0) {
1235 echan->slot[1] = edma_alloc_slot(echan->ecc,
1236 EDMA_SLOT_ANY);
1237 if (echan->slot[1] < 0) {
1238 kfree(edesc);
1239 dev_err(dev, "%s: Failed to allocate slot\n",
1240 __func__);
1241 return NULL;
1242 }
1243 }
1244 dest += pset_len;
1245 src += pset_len;
Peter Ujfalusi87a2f6222017-09-18 11:16:26 +03001246 pset_len = width = len % array_size;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001247
1248 ret = edma_config_pset(chan, &edesc->pset[1], src, dest, 1,
1249 width, pset_len, DMA_MEM_TO_MEM);
1250 if (ret < 0) {
1251 kfree(edesc);
1252 return NULL;
1253 }
1254
1255 edesc->pset[1].param.opt |= ITCCHEN;
1256 edesc->pset[1].param.opt |= TCINTEN;
1257 }
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001258
1259 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1260}
1261
Joel Fernandes50a9c702013-10-31 16:31:23 -05001262static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
1263 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1264 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +02001265 unsigned long tx_flags)
Joel Fernandes50a9c702013-10-31 16:31:23 -05001266{
1267 struct edma_chan *echan = to_edma_chan(chan);
1268 struct device *dev = chan->device->dev;
1269 struct edma_desc *edesc;
1270 dma_addr_t src_addr, dst_addr;
1271 enum dma_slave_buswidth dev_width;
John Ognessa482f4e02016-04-06 13:01:47 +03001272 bool use_intermediate = false;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001273 u32 burst;
1274 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001275
Joel Fernandes50a9c702013-10-31 16:31:23 -05001276 if (unlikely(!echan || !buf_len || !period_len))
1277 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001278
Joel Fernandes50a9c702013-10-31 16:31:23 -05001279 if (direction == DMA_DEV_TO_MEM) {
1280 src_addr = echan->cfg.src_addr;
1281 dst_addr = buf_addr;
1282 dev_width = echan->cfg.src_addr_width;
1283 burst = echan->cfg.src_maxburst;
1284 } else if (direction == DMA_MEM_TO_DEV) {
1285 src_addr = buf_addr;
1286 dst_addr = echan->cfg.dst_addr;
1287 dev_width = echan->cfg.dst_addr_width;
1288 burst = echan->cfg.dst_maxburst;
1289 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001290 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001291 return NULL;
1292 }
1293
1294 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001295 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001296 return NULL;
1297 }
1298
1299 if (unlikely(buf_len % period_len)) {
1300 dev_err(dev, "Period should be multiple of Buffer length\n");
1301 return NULL;
1302 }
1303
1304 nslots = (buf_len / period_len) + 1;
1305
1306 /*
1307 * Cyclic DMA users such as audio cannot tolerate delays introduced
1308 * by cases where the number of periods is more than the maximum
1309 * number of SGs the EDMA driver can handle at a time. For DMA types
1310 * such as Slave SGs, such delays are tolerable and synchronized,
1311 * but the synchronization is difficult to achieve with Cyclic and
1312 * cannot be guaranteed, so we error out early.
1313 */
John Ognessa482f4e02016-04-06 13:01:47 +03001314 if (nslots > MAX_NR_SG) {
1315 /*
1316 * If the burst and period sizes are the same, we can put
1317 * the full buffer into a single period and activate
1318 * intermediate interrupts. This will produce interrupts
1319 * after each burst, which is also after each desired period.
1320 */
1321 if (burst == period_len) {
1322 period_len = buf_len;
1323 nslots = 2;
1324 use_intermediate = true;
1325 } else {
1326 return NULL;
1327 }
1328 }
Joel Fernandes50a9c702013-10-31 16:31:23 -05001329
Kees Cookacafe7e2018-05-08 13:45:50 -07001330 edesc = kzalloc(struct_size(edesc, pset, nslots), GFP_ATOMIC);
Peter Griffinaef94fe2016-06-07 18:38:41 +01001331 if (!edesc)
Joel Fernandes50a9c702013-10-31 16:31:23 -05001332 return NULL;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001333
1334 edesc->cyclic = 1;
1335 edesc->pset_nr = nslots;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001336 edesc->residue = edesc->residue_stat = buf_len;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001337 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001338 edesc->echan = echan;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001339
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001340 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
1341 __func__, echan->ch_num, nslots, period_len, buf_len);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001342
1343 for (i = 0; i < nslots; i++) {
1344 /* Allocate a PaRAM slot, if needed */
1345 if (echan->slot[i] < 0) {
1346 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001347 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001348 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001349 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001350 dev_err(dev, "%s: Failed to allocate slot\n",
1351 __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001352 return NULL;
1353 }
1354 }
1355
1356 if (i == nslots - 1) {
1357 memcpy(&edesc->pset[i], &edesc->pset[0],
1358 sizeof(edesc->pset[0]));
1359 break;
1360 }
1361
1362 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1363 dst_addr, burst, dev_width, period_len,
1364 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001365 if (ret < 0) {
1366 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001367 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001368 }
Joel Fernandes50a9c702013-10-31 16:31:23 -05001369
1370 if (direction == DMA_DEV_TO_MEM)
1371 dst_addr += period_len;
1372 else
1373 src_addr += period_len;
1374
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001375 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
1376 dev_vdbg(dev,
Joel Fernandes50a9c702013-10-31 16:31:23 -05001377 "\n pset[%d]:\n"
1378 " chnum\t%d\n"
1379 " slot\t%d\n"
1380 " opt\t%08x\n"
1381 " src\t%08x\n"
1382 " dst\t%08x\n"
1383 " abcnt\t%08x\n"
1384 " ccnt\t%08x\n"
1385 " bidx\t%08x\n"
1386 " cidx\t%08x\n"
1387 " lkrld\t%08x\n",
1388 i, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001389 edesc->pset[i].param.opt,
1390 edesc->pset[i].param.src,
1391 edesc->pset[i].param.dst,
1392 edesc->pset[i].param.a_b_cnt,
1393 edesc->pset[i].param.ccnt,
1394 edesc->pset[i].param.src_dst_bidx,
1395 edesc->pset[i].param.src_dst_cidx,
1396 edesc->pset[i].param.link_bcntrld);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001397
1398 edesc->absync = ret;
1399
1400 /*
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001401 * Enable period interrupt only if it is requested
Joel Fernandes50a9c702013-10-31 16:31:23 -05001402 */
John Ognessa482f4e02016-04-06 13:01:47 +03001403 if (tx_flags & DMA_PREP_INTERRUPT) {
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001404 edesc->pset[i].param.opt |= TCINTEN;
John Ognessa482f4e02016-04-06 13:01:47 +03001405
1406 /* Also enable intermediate interrupts if necessary */
1407 if (use_intermediate)
1408 edesc->pset[i].param.opt |= ITCINTEN;
1409 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001410 }
1411
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001412 /* Place the cyclic channel to highest priority queue */
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001413 if (!echan->tc)
1414 edma_assign_channel_eventq(echan, EVENTQ_0);
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001415
Matt Porterc2dde5f2012-08-22 21:09:34 -04001416 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1417}
1418
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001419static void edma_completion_handler(struct edma_chan *echan)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001420{
Matt Porterc2dde5f2012-08-22 21:09:34 -04001421 struct device *dev = echan->vchan.chan.device->dev;
Peter Ujfalusie4d88172016-02-11 15:17:48 +02001422 struct edma_desc *edesc;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001423
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001424 spin_lock(&echan->vchan.lock);
Peter Ujfalusie4d88172016-02-11 15:17:48 +02001425 edesc = echan->edesc;
1426 if (edesc) {
1427 if (edesc->cyclic) {
1428 vchan_cyclic_callback(&edesc->vdesc);
1429 spin_unlock(&echan->vchan.lock);
1430 return;
1431 } else if (edesc->processed == edesc->pset_nr) {
1432 edesc->residue = 0;
1433 edma_stop(echan);
1434 vchan_cookie_complete(&edesc->vdesc);
1435 echan->edesc = NULL;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001436
Peter Ujfalusie4d88172016-02-11 15:17:48 +02001437 dev_dbg(dev, "Transfer completed on channel %d\n",
1438 echan->ch_num);
1439 } else {
1440 dev_dbg(dev, "Sub transfer completed on channel %d\n",
1441 echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001442
Peter Ujfalusie4d88172016-02-11 15:17:48 +02001443 edma_pause(echan);
Joel Fernandesc5f47992013-08-29 18:05:43 -05001444
Peter Ujfalusie4d88172016-02-11 15:17:48 +02001445 /* Update statistics for tx_status */
1446 edesc->residue -= edesc->sg_len;
1447 edesc->residue_stat = edesc->residue;
1448 edesc->processed_stat = edesc->processed;
1449 }
1450 edma_execute(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001451 }
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001452
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001453 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001454}
1455
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001456/* eDMA interrupt handler */
1457static irqreturn_t dma_irq_handler(int irq, void *data)
1458{
1459 struct edma_cc *ecc = data;
1460 int ctlr;
1461 u32 sh_ier;
1462 u32 sh_ipr;
1463 u32 bank;
1464
1465 ctlr = ecc->id;
1466 if (ctlr < 0)
1467 return IRQ_NONE;
1468
1469 dev_vdbg(ecc->dev, "dma_irq_handler\n");
1470
1471 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 0);
1472 if (!sh_ipr) {
1473 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 1);
1474 if (!sh_ipr)
1475 return IRQ_NONE;
1476 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 1);
1477 bank = 1;
1478 } else {
1479 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 0);
1480 bank = 0;
1481 }
1482
1483 do {
1484 u32 slot;
1485 u32 channel;
1486
1487 slot = __ffs(sh_ipr);
1488 sh_ipr &= ~(BIT(slot));
1489
1490 if (sh_ier & BIT(slot)) {
1491 channel = (bank << 5) | slot;
1492 /* Clear the corresponding IPR bits */
1493 edma_shadow0_write_array(ecc, SH_ICR, bank, BIT(slot));
1494 edma_completion_handler(&ecc->slave_chans[channel]);
1495 }
1496 } while (sh_ipr);
1497
1498 edma_shadow0_write(ecc, SH_IEVAL, 1);
1499 return IRQ_HANDLED;
1500}
1501
1502static void edma_error_handler(struct edma_chan *echan)
1503{
1504 struct edma_cc *ecc = echan->ecc;
1505 struct device *dev = echan->vchan.chan.device->dev;
1506 struct edmacc_param p;
Arnd Bergmann2cc40ee2016-09-30 18:19:01 +02001507 int err;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001508
1509 if (!echan->edesc)
1510 return;
1511
1512 spin_lock(&echan->vchan.lock);
1513
Arnd Bergmann2cc40ee2016-09-30 18:19:01 +02001514 err = edma_read_slot(ecc, echan->slot[0], &p);
1515
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001516 /*
1517 * Issue later based on missed flag which will be sure
1518 * to happen as:
1519 * (1) we finished transmitting an intermediate slot and
1520 * edma_execute is coming up.
1521 * (2) or we finished current transfer and issue will
1522 * call edma_execute.
1523 *
1524 * Important note: issuing can be dangerous here and
1525 * lead to some nasty recursion when we are in a NULL
1526 * slot. So we avoid doing so and set the missed flag.
1527 */
Arnd Bergmann2cc40ee2016-09-30 18:19:01 +02001528 if (err || (p.a_b_cnt == 0 && p.ccnt == 0)) {
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001529 dev_dbg(dev, "Error on null slot, setting miss\n");
1530 echan->missed = 1;
1531 } else {
1532 /*
1533 * The slot is already programmed but the event got
1534 * missed, so its safe to issue it here.
1535 */
1536 dev_dbg(dev, "Missed event, TRIGGERING\n");
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001537 edma_clean_channel(echan);
1538 edma_stop(echan);
1539 edma_start(echan);
1540 edma_trigger_channel(echan);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001541 }
1542 spin_unlock(&echan->vchan.lock);
1543}
1544
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001545static inline bool edma_error_pending(struct edma_cc *ecc)
1546{
1547 if (edma_read_array(ecc, EDMA_EMR, 0) ||
1548 edma_read_array(ecc, EDMA_EMR, 1) ||
1549 edma_read(ecc, EDMA_QEMR) || edma_read(ecc, EDMA_CCERR))
1550 return true;
1551
1552 return false;
1553}
1554
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001555/* eDMA error interrupt handler */
1556static irqreturn_t dma_ccerr_handler(int irq, void *data)
1557{
1558 struct edma_cc *ecc = data;
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001559 int i, j;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001560 int ctlr;
1561 unsigned int cnt = 0;
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001562 unsigned int val;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001563
1564 ctlr = ecc->id;
1565 if (ctlr < 0)
1566 return IRQ_NONE;
1567
1568 dev_vdbg(ecc->dev, "dma_ccerr_handler\n");
1569
Peter Ujfalusi3b2bc8a2016-05-10 13:40:54 +03001570 if (!edma_error_pending(ecc)) {
1571 /*
1572 * The registers indicate no pending error event but the irq
1573 * handler has been called.
1574 * Ask eDMA to re-evaluate the error registers.
1575 */
1576 dev_err(ecc->dev, "%s: Error interrupt without error event!\n",
1577 __func__);
1578 edma_write(ecc, EDMA_EEVAL, 1);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001579 return IRQ_NONE;
Peter Ujfalusi3b2bc8a2016-05-10 13:40:54 +03001580 }
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001581
1582 while (1) {
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001583 /* Event missed register(s) */
1584 for (j = 0; j < 2; j++) {
1585 unsigned long emr;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001586
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001587 val = edma_read_array(ecc, EDMA_EMR, j);
1588 if (!val)
1589 continue;
1590
1591 dev_dbg(ecc->dev, "EMR%d 0x%08x\n", j, val);
1592 emr = val;
1593 for (i = find_next_bit(&emr, 32, 0); i < 32;
1594 i = find_next_bit(&emr, 32, i + 1)) {
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001595 int k = (j << 5) + i;
1596
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001597 /* Clear the corresponding EMR bits */
1598 edma_write_array(ecc, EDMA_EMCR, j, BIT(i));
1599 /* Clear any SER */
1600 edma_shadow0_write_array(ecc, SH_SECR, j,
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001601 BIT(i));
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001602 edma_error_handler(&ecc->slave_chans[k]);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001603 }
1604 }
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001605
1606 val = edma_read(ecc, EDMA_QEMR);
1607 if (val) {
1608 dev_dbg(ecc->dev, "QEMR 0x%02x\n", val);
1609 /* Not reported, just clear the interrupt reason. */
1610 edma_write(ecc, EDMA_QEMCR, val);
1611 edma_shadow0_write(ecc, SH_QSECR, val);
1612 }
1613
1614 val = edma_read(ecc, EDMA_CCERR);
1615 if (val) {
1616 dev_warn(ecc->dev, "CCERR 0x%08x\n", val);
1617 /* Not reported, just clear the interrupt reason. */
1618 edma_write(ecc, EDMA_CCERRCLR, val);
1619 }
1620
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001621 if (!edma_error_pending(ecc))
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001622 break;
1623 cnt++;
1624 if (cnt > 10)
1625 break;
1626 }
1627 edma_write(ecc, EDMA_EEVAL, 1);
1628 return IRQ_HANDLED;
1629}
1630
Matt Porterc2dde5f2012-08-22 21:09:34 -04001631/* Alloc channel resources */
1632static int edma_alloc_chan_resources(struct dma_chan *chan)
1633{
1634 struct edma_chan *echan = to_edma_chan(chan);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001635 struct edma_cc *ecc = echan->ecc;
1636 struct device *dev = ecc->dev;
1637 enum dma_event_q eventq_no = EVENTQ_DEFAULT;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001638 int ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001639
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001640 if (echan->tc) {
1641 eventq_no = echan->tc->id;
1642 } else if (ecc->tc_list) {
1643 /* memcpy channel */
1644 echan->tc = &ecc->tc_list[ecc->info->default_queue];
1645 eventq_no = echan->tc->id;
1646 }
1647
1648 ret = edma_alloc_channel(echan, eventq_no);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001649 if (ret)
1650 return ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001651
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001652 echan->slot[0] = edma_alloc_slot(ecc, echan->ch_num);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001653 if (echan->slot[0] < 0) {
1654 dev_err(dev, "Entry slot allocation failed for channel %u\n",
1655 EDMA_CHAN_SLOT(echan->ch_num));
Wei Yongjunf95df7d2016-10-17 15:16:35 +00001656 ret = echan->slot[0];
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001657 goto err_slot;
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001658 }
1659
1660 /* Set up channel -> slot mapping for the entry slot */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001661 edma_set_chmap(echan, echan->slot[0]);
1662 echan->alloced = true;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001663
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001664 dev_dbg(dev, "Got eDMA channel %d for virt channel %d (%s trigger)\n",
1665 EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id,
1666 echan->hw_triggered ? "HW" : "SW");
1667
Matt Porterc2dde5f2012-08-22 21:09:34 -04001668 return 0;
1669
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001670err_slot:
1671 edma_free_channel(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001672 return ret;
1673}
1674
1675/* Free channel resources */
1676static void edma_free_chan_resources(struct dma_chan *chan)
1677{
1678 struct edma_chan *echan = to_edma_chan(chan);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001679 struct device *dev = echan->ecc->dev;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001680 int i;
1681
1682 /* Terminate transfers */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001683 edma_stop(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001684
1685 vchan_free_chan_resources(&echan->vchan);
1686
1687 /* Free EDMA PaRAM slots */
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001688 for (i = 0; i < EDMA_MAX_SLOTS; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001689 if (echan->slot[i] >= 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001690 edma_free_slot(echan->ecc, echan->slot[i]);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001691 echan->slot[i] = -1;
1692 }
1693 }
1694
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001695 /* Set entry slot to the dummy slot */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001696 edma_set_chmap(echan, echan->ecc->dummy_slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001697
Matt Porterc2dde5f2012-08-22 21:09:34 -04001698 /* Free EDMA channel */
1699 if (echan->alloced) {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001700 edma_free_channel(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001701 echan->alloced = false;
1702 }
1703
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001704 echan->tc = NULL;
1705 echan->hw_triggered = false;
1706
1707 dev_dbg(dev, "Free eDMA channel %d for virt channel %d\n",
1708 EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001709}
1710
1711/* Send pending descriptor to hardware */
1712static void edma_issue_pending(struct dma_chan *chan)
1713{
1714 struct edma_chan *echan = to_edma_chan(chan);
1715 unsigned long flags;
1716
1717 spin_lock_irqsave(&echan->vchan.lock, flags);
1718 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
1719 edma_execute(echan);
1720 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1721}
1722
John Ogness4ac31d12016-01-28 11:29:08 +01001723/*
1724 * This limit exists to avoid a possible infinite loop when waiting for proof
1725 * that a particular transfer is completed. This limit can be hit if there
1726 * are large bursts to/from slow devices or the CPU is never able to catch
1727 * the DMA hardware idle. On an AM335x transfering 48 bytes from the UART
1728 * RX-FIFO, as many as 55 loops have been seen.
1729 */
1730#define EDMA_MAX_TR_WAIT_LOOPS 1000
1731
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001732static u32 edma_residue(struct edma_desc *edesc)
1733{
1734 bool dst = edesc->direction == DMA_DEV_TO_MEM;
John Ogness4ac31d12016-01-28 11:29:08 +01001735 int loop_count = EDMA_MAX_TR_WAIT_LOOPS;
1736 struct edma_chan *echan = edesc->echan;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001737 struct edma_pset *pset = edesc->pset;
1738 dma_addr_t done, pos;
1739 int i;
1740
1741 /*
1742 * We always read the dst/src position from the first RamPar
1743 * pset. That's the one which is active now.
1744 */
John Ogness4ac31d12016-01-28 11:29:08 +01001745 pos = edma_get_position(echan->ecc, echan->slot[0], dst);
1746
1747 /*
1748 * "pos" may represent a transfer request that is still being
1749 * processed by the EDMACC or EDMATC. We will busy wait until
1750 * any one of the situations occurs:
1751 * 1. the DMA hardware is idle
1752 * 2. a new transfer request is setup
1753 * 3. we hit the loop limit
1754 */
1755 while (edma_read(echan->ecc, EDMA_CCSTAT) & EDMA_CCSTAT_ACTV) {
1756 /* check if a new transfer request is setup */
1757 if (edma_get_position(echan->ecc,
1758 echan->slot[0], dst) != pos) {
1759 break;
1760 }
1761
1762 if (!--loop_count) {
1763 dev_dbg_ratelimited(echan->vchan.chan.device->dev,
1764 "%s: timeout waiting for PaRAM update\n",
1765 __func__);
1766 break;
1767 }
1768
1769 cpu_relax();
1770 }
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001771
1772 /*
1773 * Cyclic is simple. Just subtract pset[0].addr from pos.
1774 *
1775 * We never update edesc->residue in the cyclic case, so we
1776 * can tell the remaining room to the end of the circular
1777 * buffer.
1778 */
1779 if (edesc->cyclic) {
1780 done = pos - pset->addr;
1781 edesc->residue_stat = edesc->residue - done;
1782 return edesc->residue_stat;
1783 }
1784
1785 /*
1786 * For SG operation we catch up with the last processed
1787 * status.
1788 */
1789 pset += edesc->processed_stat;
1790
1791 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
1792 /*
1793 * If we are inside this pset address range, we know
1794 * this is the active one. Get the current delta and
1795 * stop walking the psets.
1796 */
1797 if (pos >= pset->addr && pos < pset->addr + pset->len)
1798 return edesc->residue_stat - (pos - pset->addr);
1799
1800 /* Otherwise mark it done and update residue_stat. */
1801 edesc->processed_stat++;
1802 edesc->residue_stat -= pset->len;
1803 }
1804 return edesc->residue_stat;
1805}
1806
Matt Porterc2dde5f2012-08-22 21:09:34 -04001807/* Check request completion status */
1808static enum dma_status edma_tx_status(struct dma_chan *chan,
1809 dma_cookie_t cookie,
1810 struct dma_tx_state *txstate)
1811{
1812 struct edma_chan *echan = to_edma_chan(chan);
1813 struct virt_dma_desc *vdesc;
1814 enum dma_status ret;
1815 unsigned long flags;
1816
1817 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +05301818 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001819 return ret;
1820
1821 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001822 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001823 txstate->residue = edma_residue(echan->edesc);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001824 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
1825 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001826 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1827
1828 return ret;
1829}
1830
Peter Ujfalusiecb7dec2015-12-09 10:18:10 +02001831static bool edma_is_memcpy_channel(int ch_num, s32 *memcpy_channels)
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001832{
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001833 if (!memcpy_channels)
1834 return false;
Peter Ujfalusiecb7dec2015-12-09 10:18:10 +02001835 while (*memcpy_channels != -1) {
1836 if (*memcpy_channels == ch_num)
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001837 return true;
Peter Ujfalusiecb7dec2015-12-09 10:18:10 +02001838 memcpy_channels++;
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001839 }
1840 return false;
1841}
1842
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001843#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1844 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
Peter Ujfalusie4a899d2014-07-03 07:51:56 +03001845 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001846 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1847
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001848static void edma_dma_init(struct edma_cc *ecc, bool legacy_mode)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001849{
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001850 struct dma_device *s_ddev = &ecc->dma_slave;
1851 struct dma_device *m_ddev = NULL;
Peter Ujfalusiecb7dec2015-12-09 10:18:10 +02001852 s32 *memcpy_channels = ecc->info->memcpy_channels;
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03001853 int i, j;
Maxime Ripard9f59cd02014-11-17 14:42:47 +01001854
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001855 dma_cap_zero(s_ddev->cap_mask);
1856 dma_cap_set(DMA_SLAVE, s_ddev->cap_mask);
1857 dma_cap_set(DMA_CYCLIC, s_ddev->cap_mask);
1858 if (ecc->legacy_mode && !memcpy_channels) {
1859 dev_warn(ecc->dev,
1860 "Legacy memcpy is enabled, things might not work\n");
Maxime Ripard9f59cd02014-11-17 14:42:47 +01001861
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001862 dma_cap_set(DMA_MEMCPY, s_ddev->cap_mask);
1863 s_ddev->device_prep_dma_memcpy = edma_prep_dma_memcpy;
1864 s_ddev->directions = BIT(DMA_MEM_TO_MEM);
1865 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001866
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001867 s_ddev->device_prep_slave_sg = edma_prep_slave_sg;
1868 s_ddev->device_prep_dma_cyclic = edma_prep_dma_cyclic;
1869 s_ddev->device_alloc_chan_resources = edma_alloc_chan_resources;
1870 s_ddev->device_free_chan_resources = edma_free_chan_resources;
1871 s_ddev->device_issue_pending = edma_issue_pending;
1872 s_ddev->device_tx_status = edma_tx_status;
1873 s_ddev->device_config = edma_slave_config;
1874 s_ddev->device_pause = edma_dma_pause;
1875 s_ddev->device_resume = edma_dma_resume;
1876 s_ddev->device_terminate_all = edma_terminate_all;
Peter Ujfalusib84730f2016-02-11 11:08:42 +02001877 s_ddev->device_synchronize = edma_synchronize;
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03001878
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001879 s_ddev->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1880 s_ddev->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
1881 s_ddev->directions |= (BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV));
1882 s_ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Peter Ujfalusiea09ea52017-10-03 11:35:37 +03001883 s_ddev->max_burst = SZ_32K - 1; /* CIDX: 16bit signed */
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03001884
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001885 s_ddev->dev = ecc->dev;
1886 INIT_LIST_HEAD(&s_ddev->channels);
1887
1888 if (memcpy_channels) {
1889 m_ddev = devm_kzalloc(ecc->dev, sizeof(*m_ddev), GFP_KERNEL);
Peter Ujfalusif31b3232018-03-21 10:30:22 +02001890 if (!m_ddev) {
1891 dev_warn(ecc->dev, "memcpy is disabled due to OoM\n");
1892 memcpy_channels = NULL;
1893 goto ch_setup;
1894 }
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001895 ecc->dma_memcpy = m_ddev;
1896
1897 dma_cap_zero(m_ddev->cap_mask);
1898 dma_cap_set(DMA_MEMCPY, m_ddev->cap_mask);
1899
1900 m_ddev->device_prep_dma_memcpy = edma_prep_dma_memcpy;
1901 m_ddev->device_alloc_chan_resources = edma_alloc_chan_resources;
1902 m_ddev->device_free_chan_resources = edma_free_chan_resources;
1903 m_ddev->device_issue_pending = edma_issue_pending;
1904 m_ddev->device_tx_status = edma_tx_status;
1905 m_ddev->device_config = edma_slave_config;
1906 m_ddev->device_pause = edma_dma_pause;
1907 m_ddev->device_resume = edma_dma_resume;
1908 m_ddev->device_terminate_all = edma_terminate_all;
Peter Ujfalusib84730f2016-02-11 11:08:42 +02001909 m_ddev->device_synchronize = edma_synchronize;
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001910
1911 m_ddev->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1912 m_ddev->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
1913 m_ddev->directions = BIT(DMA_MEM_TO_MEM);
1914 m_ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1915
1916 m_ddev->dev = ecc->dev;
1917 INIT_LIST_HEAD(&m_ddev->channels);
1918 } else if (!ecc->legacy_mode) {
1919 dev_info(ecc->dev, "memcpy is disabled\n");
1920 }
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03001921
Peter Ujfalusif31b3232018-03-21 10:30:22 +02001922ch_setup:
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03001923 for (i = 0; i < ecc->num_channels; i++) {
1924 struct edma_chan *echan = &ecc->slave_chans[i];
1925 echan->ch_num = EDMA_CTLR_CHAN(ecc->id, i);
1926 echan->ecc = ecc;
1927 echan->vchan.desc_free = edma_desc_free;
1928
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001929 if (m_ddev && edma_is_memcpy_channel(i, memcpy_channels))
1930 vchan_init(&echan->vchan, m_ddev);
1931 else
1932 vchan_init(&echan->vchan, s_ddev);
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03001933
1934 INIT_LIST_HEAD(&echan->node);
1935 for (j = 0; j < EDMA_MAX_SLOTS; j++)
1936 echan->slot[j] = -1;
1937 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001938}
1939
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001940static int edma_setup_from_hw(struct device *dev, struct edma_soc_info *pdata,
1941 struct edma_cc *ecc)
1942{
1943 int i;
1944 u32 value, cccfg;
1945 s8 (*queue_priority_map)[2];
1946
1947 /* Decode the eDMA3 configuration from CCCFG register */
1948 cccfg = edma_read(ecc, EDMA_CCCFG);
1949
1950 value = GET_NUM_REGN(cccfg);
1951 ecc->num_region = BIT(value);
1952
1953 value = GET_NUM_DMACH(cccfg);
1954 ecc->num_channels = BIT(value + 1);
1955
Peter Ujfalusi633e42b2015-10-16 10:18:04 +03001956 value = GET_NUM_QDMACH(cccfg);
1957 ecc->num_qchannels = value * 2;
1958
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001959 value = GET_NUM_PAENTRY(cccfg);
1960 ecc->num_slots = BIT(value + 4);
1961
1962 value = GET_NUM_EVQUE(cccfg);
1963 ecc->num_tc = value + 1;
1964
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +03001965 ecc->chmap_exist = (cccfg & CHMAP_EXIST) ? true : false;
1966
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001967 dev_dbg(dev, "eDMA3 CC HW configuration (cccfg: 0x%08x):\n", cccfg);
1968 dev_dbg(dev, "num_region: %u\n", ecc->num_region);
1969 dev_dbg(dev, "num_channels: %u\n", ecc->num_channels);
Peter Ujfalusi633e42b2015-10-16 10:18:04 +03001970 dev_dbg(dev, "num_qchannels: %u\n", ecc->num_qchannels);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001971 dev_dbg(dev, "num_slots: %u\n", ecc->num_slots);
1972 dev_dbg(dev, "num_tc: %u\n", ecc->num_tc);
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +03001973 dev_dbg(dev, "chmap_exist: %s\n", ecc->chmap_exist ? "yes" : "no");
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001974
1975 /* Nothing need to be done if queue priority is provided */
1976 if (pdata->queue_priority_mapping)
1977 return 0;
1978
1979 /*
1980 * Configure TC/queue priority as follows:
1981 * Q0 - priority 0
1982 * Q1 - priority 1
1983 * Q2 - priority 2
1984 * ...
1985 * The meaning of priority numbers: 0 highest priority, 7 lowest
1986 * priority. So Q0 is the highest priority queue and the last queue has
1987 * the lowest priority.
1988 */
Peter Ujfalusi547c6e22015-10-14 14:42:55 +03001989 queue_priority_map = devm_kcalloc(dev, ecc->num_tc + 1, sizeof(s8),
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001990 GFP_KERNEL);
1991 if (!queue_priority_map)
1992 return -ENOMEM;
1993
1994 for (i = 0; i < ecc->num_tc; i++) {
1995 queue_priority_map[i][0] = i;
1996 queue_priority_map[i][1] = i;
1997 }
1998 queue_priority_map[i][0] = -1;
1999 queue_priority_map[i][1] = -1;
2000
2001 pdata->queue_priority_mapping = queue_priority_map;
2002 /* Default queue has the lowest priority */
2003 pdata->default_queue = i - 1;
2004
2005 return 0;
2006}
2007
2008#if IS_ENABLED(CONFIG_OF)
2009static int edma_xbar_event_map(struct device *dev, struct edma_soc_info *pdata,
2010 size_t sz)
2011{
2012 const char pname[] = "ti,edma-xbar-event-map";
2013 struct resource res;
2014 void __iomem *xbar;
2015 s16 (*xbar_chans)[2];
2016 size_t nelm = sz / sizeof(s16);
2017 u32 shift, offset, mux;
2018 int ret, i;
2019
Peter Ujfalusi547c6e22015-10-14 14:42:55 +03002020 xbar_chans = devm_kcalloc(dev, nelm + 2, sizeof(s16), GFP_KERNEL);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002021 if (!xbar_chans)
2022 return -ENOMEM;
2023
2024 ret = of_address_to_resource(dev->of_node, 1, &res);
2025 if (ret)
2026 return -ENOMEM;
2027
2028 xbar = devm_ioremap(dev, res.start, resource_size(&res));
2029 if (!xbar)
2030 return -ENOMEM;
2031
2032 ret = of_property_read_u16_array(dev->of_node, pname, (u16 *)xbar_chans,
2033 nelm);
2034 if (ret)
2035 return -EIO;
2036
2037 /* Invalidate last entry for the other user of this mess */
2038 nelm >>= 1;
2039 xbar_chans[nelm][0] = -1;
2040 xbar_chans[nelm][1] = -1;
2041
2042 for (i = 0; i < nelm; i++) {
2043 shift = (xbar_chans[i][1] & 0x03) << 3;
2044 offset = xbar_chans[i][1] & 0xfffffffc;
2045 mux = readl(xbar + offset);
2046 mux &= ~(0xff << shift);
2047 mux |= xbar_chans[i][0] << shift;
2048 writel(mux, (xbar + offset));
2049 }
2050
2051 pdata->xbar_chans = (const s16 (*)[2]) xbar_chans;
2052 return 0;
2053}
2054
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002055static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
2056 bool legacy_mode)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002057{
2058 struct edma_soc_info *info;
Peter Ujfalusi966a87b2015-10-16 10:18:07 +03002059 struct property *prop;
Peter Ujfalusif1d1e342016-09-21 15:41:29 +03002060 int sz, ret;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002061
2062 info = devm_kzalloc(dev, sizeof(struct edma_soc_info), GFP_KERNEL);
2063 if (!info)
2064 return ERR_PTR(-ENOMEM);
2065
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002066 if (legacy_mode) {
2067 prop = of_find_property(dev->of_node, "ti,edma-xbar-event-map",
2068 &sz);
2069 if (prop) {
2070 ret = edma_xbar_event_map(dev, info, sz);
2071 if (ret)
2072 return ERR_PTR(ret);
2073 }
2074 return info;
2075 }
2076
2077 /* Get the list of channels allocated to be used for memcpy */
2078 prop = of_find_property(dev->of_node, "ti,edma-memcpy-channels", &sz);
Peter Ujfalusi966a87b2015-10-16 10:18:07 +03002079 if (prop) {
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002080 const char pname[] = "ti,edma-memcpy-channels";
Peter Ujfalusiecb7dec2015-12-09 10:18:10 +02002081 size_t nelm = sz / sizeof(s32);
2082 s32 *memcpy_ch;
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002083
Peter Ujfalusiecb7dec2015-12-09 10:18:10 +02002084 memcpy_ch = devm_kcalloc(dev, nelm + 1, sizeof(s32),
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002085 GFP_KERNEL);
2086 if (!memcpy_ch)
2087 return ERR_PTR(-ENOMEM);
2088
Peter Ujfalusiecb7dec2015-12-09 10:18:10 +02002089 ret = of_property_read_u32_array(dev->of_node, pname,
2090 (u32 *)memcpy_ch, nelm);
Peter Ujfalusi966a87b2015-10-16 10:18:07 +03002091 if (ret)
2092 return ERR_PTR(ret);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002093
2094 memcpy_ch[nelm] = -1;
2095 info->memcpy_channels = memcpy_ch;
2096 }
2097
2098 prop = of_find_property(dev->of_node, "ti,edma-reserved-slot-ranges",
2099 &sz);
2100 if (prop) {
2101 const char pname[] = "ti,edma-reserved-slot-ranges";
Peter Ujfalusiae0add72015-12-09 10:18:11 +02002102 u32 (*tmp)[2];
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002103 s16 (*rsv_slots)[2];
Peter Ujfalusiae0add72015-12-09 10:18:11 +02002104 size_t nelm = sz / sizeof(*tmp);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002105 struct edma_rsv_info *rsv_info;
Peter Ujfalusiae0add72015-12-09 10:18:11 +02002106 int i;
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002107
2108 if (!nelm)
2109 return info;
2110
Peter Ujfalusiae0add72015-12-09 10:18:11 +02002111 tmp = kcalloc(nelm, sizeof(*tmp), GFP_KERNEL);
2112 if (!tmp)
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002113 return ERR_PTR(-ENOMEM);
2114
Peter Ujfalusiae0add72015-12-09 10:18:11 +02002115 rsv_info = devm_kzalloc(dev, sizeof(*rsv_info), GFP_KERNEL);
2116 if (!rsv_info) {
2117 kfree(tmp);
2118 return ERR_PTR(-ENOMEM);
2119 }
2120
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002121 rsv_slots = devm_kcalloc(dev, nelm + 1, sizeof(*rsv_slots),
2122 GFP_KERNEL);
Peter Ujfalusiae0add72015-12-09 10:18:11 +02002123 if (!rsv_slots) {
2124 kfree(tmp);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002125 return ERR_PTR(-ENOMEM);
Peter Ujfalusiae0add72015-12-09 10:18:11 +02002126 }
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002127
Peter Ujfalusiae0add72015-12-09 10:18:11 +02002128 ret = of_property_read_u32_array(dev->of_node, pname,
2129 (u32 *)tmp, nelm * 2);
2130 if (ret) {
2131 kfree(tmp);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002132 return ERR_PTR(ret);
Peter Ujfalusiae0add72015-12-09 10:18:11 +02002133 }
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002134
Peter Ujfalusiae0add72015-12-09 10:18:11 +02002135 for (i = 0; i < nelm; i++) {
2136 rsv_slots[i][0] = tmp[i][0];
2137 rsv_slots[i][1] = tmp[i][1];
2138 }
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002139 rsv_slots[nelm][0] = -1;
2140 rsv_slots[nelm][1] = -1;
Peter Ujfalusiae0add72015-12-09 10:18:11 +02002141
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002142 info->rsv = rsv_info;
2143 info->rsv->rsv_slots = (const s16 (*)[2])rsv_slots;
Peter Ujfalusiae0add72015-12-09 10:18:11 +02002144
2145 kfree(tmp);
Peter Ujfalusi966a87b2015-10-16 10:18:07 +03002146 }
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002147
2148 return info;
2149}
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002150
2151static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
2152 struct of_dma *ofdma)
2153{
2154 struct edma_cc *ecc = ofdma->of_dma_data;
2155 struct dma_chan *chan = NULL;
2156 struct edma_chan *echan;
2157 int i;
2158
2159 if (!ecc || dma_spec->args_count < 1)
2160 return NULL;
2161
2162 for (i = 0; i < ecc->num_channels; i++) {
2163 echan = &ecc->slave_chans[i];
2164 if (echan->ch_num == dma_spec->args[0]) {
2165 chan = &echan->vchan.chan;
2166 break;
2167 }
2168 }
2169
2170 if (!chan)
2171 return NULL;
2172
2173 if (echan->ecc->legacy_mode && dma_spec->args_count == 1)
2174 goto out;
2175
2176 if (!echan->ecc->legacy_mode && dma_spec->args_count == 2 &&
2177 dma_spec->args[1] < echan->ecc->num_tc) {
2178 echan->tc = &echan->ecc->tc_list[dma_spec->args[1]];
2179 goto out;
2180 }
2181
2182 return NULL;
2183out:
2184 /* The channel is going to be used as HW synchronized */
2185 echan->hw_triggered = true;
2186 return dma_get_slave_channel(chan);
2187}
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002188#else
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002189static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
2190 bool legacy_mode)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002191{
2192 return ERR_PTR(-EINVAL);
2193}
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002194
2195static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
2196 struct of_dma *ofdma)
2197{
2198 return NULL;
2199}
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002200#endif
2201
Arnd Bergmannd2bfe7b2019-07-22 10:16:45 +02002202static bool edma_filter_fn(struct dma_chan *chan, void *param);
2203
Bill Pemberton463a1f82012-11-19 13:22:55 -05002204static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002205{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002206 struct edma_soc_info *info = pdev->dev.platform_data;
2207 s8 (*queue_priority_mapping)[2];
2208 int i, off, ln;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002209 const s16 (*rsv_slots)[2];
2210 const s16 (*xbar_chans)[2];
2211 int irq;
2212 char *irq_name;
2213 struct resource *mem;
2214 struct device_node *node = pdev->dev.of_node;
2215 struct device *dev = &pdev->dev;
2216 struct edma_cc *ecc;
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002217 bool legacy_mode = true;
Matt Porterc2dde5f2012-08-22 21:09:34 -04002218 int ret;
2219
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002220 if (node) {
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002221 const struct of_device_id *match;
2222
2223 match = of_match_node(edma_of_ids, node);
Peter Ujfalusib7862742016-09-21 15:41:28 +03002224 if (match && (*(u32 *)match->data) == EDMA_BINDING_TPCC)
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002225 legacy_mode = false;
2226
2227 info = edma_setup_info_from_dt(dev, legacy_mode);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002228 if (IS_ERR(info)) {
2229 dev_err(dev, "failed to get DT data\n");
2230 return PTR_ERR(info);
2231 }
2232 }
2233
2234 if (!info)
2235 return -ENODEV;
2236
2237 pm_runtime_enable(dev);
2238 ret = pm_runtime_get_sync(dev);
2239 if (ret < 0) {
2240 dev_err(dev, "pm_runtime_get_sync() failed\n");
2241 return ret;
2242 }
2243
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002244 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
Russell King94cb0e72013-06-27 13:45:16 +01002245 if (ret)
2246 return ret;
2247
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002248 ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
Peter Griffinaef94fe2016-06-07 18:38:41 +01002249 if (!ecc)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002250 return -ENOMEM;
Matt Porterc2dde5f2012-08-22 21:09:34 -04002251
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002252 ecc->dev = dev;
2253 ecc->id = pdev->id;
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002254 ecc->legacy_mode = legacy_mode;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002255 /* When booting with DT the pdev->id is -1 */
2256 if (ecc->id < 0)
2257 ecc->id = 0;
Peter Ujfalusica304fa2015-10-14 14:42:49 +03002258
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002259 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma3_cc");
2260 if (!mem) {
2261 dev_dbg(dev, "mem resource not found, using index 0\n");
2262 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2263 if (!mem) {
2264 dev_err(dev, "no mem resource?\n");
2265 return -ENODEV;
2266 }
2267 }
2268 ecc->base = devm_ioremap_resource(dev, mem);
2269 if (IS_ERR(ecc->base))
2270 return PTR_ERR(ecc->base);
Peter Ujfalusib2c843a2015-10-14 14:42:50 +03002271
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002272 platform_set_drvdata(pdev, ecc);
2273
2274 /* Get eDMA3 configuration from IP */
2275 ret = edma_setup_from_hw(dev, info, ecc);
2276 if (ret)
2277 return ret;
2278
Peter Ujfalusicb782052015-10-14 14:42:54 +03002279 /* Allocate memory based on the information we got from the IP */
2280 ecc->slave_chans = devm_kcalloc(dev, ecc->num_channels,
2281 sizeof(*ecc->slave_chans), GFP_KERNEL);
2282 if (!ecc->slave_chans)
2283 return -ENOMEM;
2284
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002285 ecc->slot_inuse = devm_kcalloc(dev, BITS_TO_LONGS(ecc->num_slots),
Peter Ujfalusicb782052015-10-14 14:42:54 +03002286 sizeof(unsigned long), GFP_KERNEL);
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002287 if (!ecc->slot_inuse)
Peter Ujfalusicb782052015-10-14 14:42:54 +03002288 return -ENOMEM;
2289
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002290 ecc->default_queue = info->default_queue;
2291
2292 for (i = 0; i < ecc->num_slots; i++)
2293 edma_write_slot(ecc, i, &dummy_paramset);
2294
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002295 if (info->rsv) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002296 /* Set the reserved slots in inuse list */
2297 rsv_slots = info->rsv->rsv_slots;
2298 if (rsv_slots) {
2299 for (i = 0; rsv_slots[i][0] != -1; i++) {
2300 off = rsv_slots[i][0];
2301 ln = rsv_slots[i][1];
Peter Ujfalusi1634d302016-09-22 09:31:04 +03002302 edma_set_bits(off, ln, ecc->slot_inuse);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002303 }
2304 }
2305 }
2306
2307 /* Clear the xbar mapped channels in unused list */
2308 xbar_chans = info->xbar_chans;
2309 if (xbar_chans) {
2310 for (i = 0; xbar_chans[i][1] != -1; i++) {
2311 off = xbar_chans[i][1];
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002312 }
2313 }
2314
2315 irq = platform_get_irq_byname(pdev, "edma3_ccint");
2316 if (irq < 0 && node)
2317 irq = irq_of_parse_and_map(node, 0);
2318
2319 if (irq >= 0) {
2320 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccint",
2321 dev_name(dev));
2322 ret = devm_request_irq(dev, irq, dma_irq_handler, 0, irq_name,
2323 ecc);
2324 if (ret) {
2325 dev_err(dev, "CCINT (%d) failed --> %d\n", irq, ret);
2326 return ret;
2327 }
Vinod Koul638001e2016-07-01 11:34:35 +05302328 ecc->ccint = irq;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002329 }
2330
2331 irq = platform_get_irq_byname(pdev, "edma3_ccerrint");
2332 if (irq < 0 && node)
2333 irq = irq_of_parse_and_map(node, 2);
2334
2335 if (irq >= 0) {
2336 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccerrint",
2337 dev_name(dev));
2338 ret = devm_request_irq(dev, irq, dma_ccerr_handler, 0, irq_name,
2339 ecc);
2340 if (ret) {
2341 dev_err(dev, "CCERRINT (%d) failed --> %d\n", irq, ret);
2342 return ret;
2343 }
Vinod Koul638001e2016-07-01 11:34:35 +05302344 ecc->ccerrint = irq;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002345 }
2346
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002347 ecc->dummy_slot = edma_alloc_slot(ecc, EDMA_SLOT_ANY);
2348 if (ecc->dummy_slot < 0) {
2349 dev_err(dev, "Can't allocate PaRAM dummy slot\n");
2350 return ecc->dummy_slot;
2351 }
2352
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002353 queue_priority_mapping = info->queue_priority_mapping;
2354
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002355 if (!ecc->legacy_mode) {
2356 int lowest_priority = 0;
2357 struct of_phandle_args tc_args;
2358
2359 ecc->tc_list = devm_kcalloc(dev, ecc->num_tc,
2360 sizeof(*ecc->tc_list), GFP_KERNEL);
2361 if (!ecc->tc_list)
2362 return -ENOMEM;
2363
2364 for (i = 0;; i++) {
2365 ret = of_parse_phandle_with_fixed_args(node, "ti,tptcs",
2366 1, i, &tc_args);
2367 if (ret || i == ecc->num_tc)
2368 break;
2369
2370 ecc->tc_list[i].node = tc_args.np;
2371 ecc->tc_list[i].id = i;
2372 queue_priority_mapping[i][1] = tc_args.args[0];
2373 if (queue_priority_mapping[i][1] > lowest_priority) {
2374 lowest_priority = queue_priority_mapping[i][1];
2375 info->default_queue = i;
2376 }
2377 }
2378 }
2379
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002380 /* Event queue priority mapping */
2381 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2382 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2383 queue_priority_mapping[i][1]);
2384
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002385 for (i = 0; i < ecc->num_region; i++) {
2386 edma_write_array2(ecc, EDMA_DRAE, i, 0, 0x0);
2387 edma_write_array2(ecc, EDMA_DRAE, i, 1, 0x0);
2388 edma_write_array(ecc, EDMA_QRAE, i, 0x0);
2389 }
2390 ecc->info = info;
2391
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03002392 /* Init the dma device and channels */
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002393 edma_dma_init(ecc, legacy_mode);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002394
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002395 for (i = 0; i < ecc->num_channels; i++) {
2396 /* Assign all channels to the default queue */
Peter Ujfalusif9425de2015-10-16 10:18:03 +03002397 edma_assign_channel_eventq(&ecc->slave_chans[i],
2398 info->default_queue);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002399 /* Set entry slot to the dummy slot */
2400 edma_set_chmap(&ecc->slave_chans[i], ecc->dummy_slot);
2401 }
2402
Peter Ujfalusi23e67232015-12-14 22:47:41 +02002403 ecc->dma_slave.filter.map = info->slave_map;
2404 ecc->dma_slave.filter.mapcnt = info->slavecnt;
2405 ecc->dma_slave.filter.fn = edma_filter_fn;
2406
Matt Porterc2dde5f2012-08-22 21:09:34 -04002407 ret = dma_async_device_register(&ecc->dma_slave);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002408 if (ret) {
2409 dev_err(dev, "slave ddev registration failed (%d)\n", ret);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002410 goto err_reg1;
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002411 }
2412
2413 if (ecc->dma_memcpy) {
2414 ret = dma_async_device_register(ecc->dma_memcpy);
2415 if (ret) {
2416 dev_err(dev, "memcpy ddev registration failed (%d)\n",
2417 ret);
2418 dma_async_device_unregister(&ecc->dma_slave);
2419 goto err_reg1;
2420 }
2421 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04002422
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002423 if (node)
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002424 of_dma_controller_register(node, of_edma_xlate, ecc);
Peter Ujfalusidc9b60552015-10-14 14:42:47 +03002425
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002426 dev_info(dev, "TI EDMA DMA engine driver\n");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002427
2428 return 0;
2429
2430err_reg1:
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002431 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002432 return ret;
2433}
2434
Vinod Koulf4e06282016-07-01 13:51:41 +05302435static void edma_cleanupp_vchan(struct dma_device *dmadev)
2436{
2437 struct edma_chan *echan, *_echan;
2438
2439 list_for_each_entry_safe(echan, _echan,
2440 &dmadev->channels, vchan.chan.device_node) {
2441 list_del(&echan->vchan.chan.device_node);
2442 tasklet_kill(&echan->vchan.task);
2443 }
2444}
2445
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08002446static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002447{
2448 struct device *dev = &pdev->dev;
2449 struct edma_cc *ecc = dev_get_drvdata(dev);
2450
Vinod Koul638001e2016-07-01 11:34:35 +05302451 devm_free_irq(dev, ecc->ccint, ecc);
2452 devm_free_irq(dev, ecc->ccerrint, ecc);
2453
Vinod Koulf4e06282016-07-01 13:51:41 +05302454 edma_cleanupp_vchan(&ecc->dma_slave);
2455
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002456 if (dev->of_node)
2457 of_dma_controller_free(dev->of_node);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002458 dma_async_device_unregister(&ecc->dma_slave);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002459 if (ecc->dma_memcpy)
2460 dma_async_device_unregister(ecc->dma_memcpy);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002461 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002462
2463 return 0;
2464}
2465
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002466#ifdef CONFIG_PM_SLEEP
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002467static int edma_pm_suspend(struct device *dev)
2468{
2469 struct edma_cc *ecc = dev_get_drvdata(dev);
2470 struct edma_chan *echan = ecc->slave_chans;
2471 int i;
2472
2473 for (i = 0; i < ecc->num_channels; i++) {
Peter Ujfalusi23f49fd2016-04-06 13:01:46 +03002474 if (echan[i].alloced)
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002475 edma_setup_interrupt(&echan[i], false);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002476 }
2477
2478 return 0;
2479}
2480
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002481static int edma_pm_resume(struct device *dev)
2482{
2483 struct edma_cc *ecc = dev_get_drvdata(dev);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002484 struct edma_chan *echan = ecc->slave_chans;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002485 int i;
2486 s8 (*queue_priority_mapping)[2];
2487
Vignesh R08c824e2016-11-23 14:57:55 +05302488 /* re initialize dummy slot to dummy param set */
2489 edma_write_slot(ecc, ecc->dummy_slot, &dummy_paramset);
2490
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002491 queue_priority_mapping = ecc->info->queue_priority_mapping;
2492
2493 /* Event queue priority mapping */
2494 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2495 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2496 queue_priority_mapping[i][1]);
2497
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002498 for (i = 0; i < ecc->num_channels; i++) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002499 if (echan[i].alloced) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002500 /* ensure access through shadow region 0 */
Peter Ujfalusie96b1f62019-07-16 11:26:53 +03002501 edma_or_array2(ecc, EDMA_DRAE, 0,
2502 EDMA_REG_ARRAY_INDEX(i),
2503 EDMA_CHANNEL_BIT(i));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002504
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002505 edma_setup_interrupt(&echan[i], true);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002506
2507 /* Set up channel -> slot mapping for the entry slot */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002508 edma_set_chmap(&echan[i], echan[i].slot[0]);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002509 }
2510 }
2511
2512 return 0;
2513}
2514#endif
2515
2516static const struct dev_pm_ops edma_pm_ops = {
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002517 SET_LATE_SYSTEM_SLEEP_PM_OPS(edma_pm_suspend, edma_pm_resume)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002518};
2519
Matt Porterc2dde5f2012-08-22 21:09:34 -04002520static struct platform_driver edma_driver = {
2521 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05002522 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002523 .driver = {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002524 .name = "edma",
2525 .pm = &edma_pm_ops,
2526 .of_match_table = edma_of_ids,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002527 },
2528};
2529
Peter Ujfalusi4fa2d092015-12-16 15:19:05 +02002530static int edma_tptc_probe(struct platform_device *pdev)
2531{
Peter Ujfalusi23f49fd2016-04-06 13:01:46 +03002532 pm_runtime_enable(&pdev->dev);
2533 return pm_runtime_get_sync(&pdev->dev);
Peter Ujfalusi4fa2d092015-12-16 15:19:05 +02002534}
2535
Peter Ujfalusi34635b1a2015-11-02 15:21:40 +02002536static struct platform_driver edma_tptc_driver = {
Peter Ujfalusi4fa2d092015-12-16 15:19:05 +02002537 .probe = edma_tptc_probe,
Peter Ujfalusi34635b1a2015-11-02 15:21:40 +02002538 .driver = {
2539 .name = "edma3-tptc",
2540 .of_match_table = edma_tptc_of_ids,
2541 },
2542};
2543
Arnd Bergmannd2bfe7b2019-07-22 10:16:45 +02002544static bool edma_filter_fn(struct dma_chan *chan, void *param)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002545{
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002546 bool match = false;
2547
Matt Porterc2dde5f2012-08-22 21:09:34 -04002548 if (chan->device->dev->driver == &edma_driver.driver) {
2549 struct edma_chan *echan = to_edma_chan(chan);
2550 unsigned ch_req = *(unsigned *)param;
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002551 if (ch_req == echan->ch_num) {
2552 /* The channel is going to be used as HW synchronized */
2553 echan->hw_triggered = true;
2554 match = true;
2555 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04002556 }
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002557 return match;
Matt Porterc2dde5f2012-08-22 21:09:34 -04002558}
2559EXPORT_SYMBOL(edma_filter_fn);
2560
Matt Porterc2dde5f2012-08-22 21:09:34 -04002561static int edma_init(void)
2562{
Peter Ujfalusi34635b1a2015-11-02 15:21:40 +02002563 int ret;
2564
2565 ret = platform_driver_register(&edma_tptc_driver);
2566 if (ret)
2567 return ret;
2568
Arnd Bergmann5305e4d2014-10-24 18:14:01 +02002569 return platform_driver_register(&edma_driver);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002570}
2571subsys_initcall(edma_init);
2572
2573static void __exit edma_exit(void)
2574{
Matt Porterc2dde5f2012-08-22 21:09:34 -04002575 platform_driver_unregister(&edma_driver);
Peter Ujfalusi34635b1a2015-11-02 15:21:40 +02002576 platform_driver_unregister(&edma_tptc_driver);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002577}
2578module_exit(edma_exit);
2579
Josh Boyerd71505b2013-09-04 10:32:50 -04002580MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002581MODULE_DESCRIPTION("TI EDMA DMA engine driver");
2582MODULE_LICENSE("GPL v2");