blob: 31722d436a42e6f717a7ebc0075d5110076e9d37 [file] [log] [blame]
Matt Porterc2dde5f2012-08-22 21:09:34 -04001/*
2 * TI EDMA DMA engine driver
3 *
4 * Copyright 2012 Texas Instruments
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
9 *
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/dmaengine.h>
17#include <linux/dma-mapping.h>
Lad, Prabhakarb7a4fd52015-02-04 13:03:27 +000018#include <linux/edma.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040019#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/list.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26#include <linux/spinlock.h>
Peter Ujfalusied646102014-07-31 13:12:38 +030027#include <linux/of.h>
Peter Ujfalusidc9b60552015-10-14 14:42:47 +030028#include <linux/of_dma.h>
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +030029#include <linux/of_irq.h>
30#include <linux/of_address.h>
31#include <linux/of_device.h>
32#include <linux/pm_runtime.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040033
Matt Porter3ad7a422013-03-06 11:15:31 -050034#include <linux/platform_data/edma.h>
Matt Porterc2dde5f2012-08-22 21:09:34 -040035
36#include "dmaengine.h"
37#include "virt-dma.h"
38
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +030039/* Offsets matching "struct edmacc_param" */
40#define PARM_OPT 0x00
41#define PARM_SRC 0x04
42#define PARM_A_B_CNT 0x08
43#define PARM_DST 0x0c
44#define PARM_SRC_DST_BIDX 0x10
45#define PARM_LINK_BCNTRLD 0x14
46#define PARM_SRC_DST_CIDX 0x18
47#define PARM_CCNT 0x1c
48
49#define PARM_SIZE 0x20
50
51/* Offsets for EDMA CC global channel registers and their shadows */
52#define SH_ER 0x00 /* 64 bits */
53#define SH_ECR 0x08 /* 64 bits */
54#define SH_ESR 0x10 /* 64 bits */
55#define SH_CER 0x18 /* 64 bits */
56#define SH_EER 0x20 /* 64 bits */
57#define SH_EECR 0x28 /* 64 bits */
58#define SH_EESR 0x30 /* 64 bits */
59#define SH_SER 0x38 /* 64 bits */
60#define SH_SECR 0x40 /* 64 bits */
61#define SH_IER 0x50 /* 64 bits */
62#define SH_IECR 0x58 /* 64 bits */
63#define SH_IESR 0x60 /* 64 bits */
64#define SH_IPR 0x68 /* 64 bits */
65#define SH_ICR 0x70 /* 64 bits */
66#define SH_IEVAL 0x78
67#define SH_QER 0x80
68#define SH_QEER 0x84
69#define SH_QEECR 0x88
70#define SH_QEESR 0x8c
71#define SH_QSER 0x90
72#define SH_QSECR 0x94
73#define SH_SIZE 0x200
74
75/* Offsets for EDMA CC global registers */
76#define EDMA_REV 0x0000
77#define EDMA_CCCFG 0x0004
78#define EDMA_QCHMAP 0x0200 /* 8 registers */
79#define EDMA_DMAQNUM 0x0240 /* 8 registers (4 on OMAP-L1xx) */
80#define EDMA_QDMAQNUM 0x0260
81#define EDMA_QUETCMAP 0x0280
82#define EDMA_QUEPRI 0x0284
83#define EDMA_EMR 0x0300 /* 64 bits */
84#define EDMA_EMCR 0x0308 /* 64 bits */
85#define EDMA_QEMR 0x0310
86#define EDMA_QEMCR 0x0314
87#define EDMA_CCERR 0x0318
88#define EDMA_CCERRCLR 0x031c
89#define EDMA_EEVAL 0x0320
90#define EDMA_DRAE 0x0340 /* 4 x 64 bits*/
91#define EDMA_QRAE 0x0380 /* 4 registers */
92#define EDMA_QUEEVTENTRY 0x0400 /* 2 x 16 registers */
93#define EDMA_QSTAT 0x0600 /* 2 registers */
94#define EDMA_QWMTHRA 0x0620
95#define EDMA_QWMTHRB 0x0624
96#define EDMA_CCSTAT 0x0640
97
98#define EDMA_M 0x1000 /* global channel registers */
99#define EDMA_ECR 0x1008
100#define EDMA_ECRH 0x100C
101#define EDMA_SHADOW0 0x2000 /* 4 shadow regions */
102#define EDMA_PARM 0x4000 /* PaRAM entries */
103
104#define PARM_OFFSET(param_no) (EDMA_PARM + ((param_no) << 5))
105
106#define EDMA_DCHMAP 0x0100 /* 64 registers */
107
108/* CCCFG register */
109#define GET_NUM_DMACH(x) (x & 0x7) /* bits 0-2 */
Peter Ujfalusi633e42b2015-10-16 10:18:04 +0300110#define GET_NUM_QDMACH(x) (x & 0x70 >> 4) /* bits 4-6 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300111#define GET_NUM_PAENTRY(x) ((x & 0x7000) >> 12) /* bits 12-14 */
112#define GET_NUM_EVQUE(x) ((x & 0x70000) >> 16) /* bits 16-18 */
113#define GET_NUM_REGN(x) ((x & 0x300000) >> 20) /* bits 20-21 */
114#define CHMAP_EXIST BIT(24)
115
Matt Porterc2dde5f2012-08-22 21:09:34 -0400116/*
Joel Fernandes2abd5f12013-09-23 18:05:15 -0500117 * Max of 20 segments per channel to conserve PaRAM slots
118 * Also note that MAX_NR_SG should be atleast the no.of periods
119 * that are required for ASoC, otherwise DMA prep calls will
120 * fail. Today davinci-pcm is the only user of this driver and
121 * requires atleast 17 slots, so we setup the default to 20.
122 */
123#define MAX_NR_SG 20
Matt Porterc2dde5f2012-08-22 21:09:34 -0400124#define EDMA_MAX_SLOTS MAX_NR_SG
125#define EDMA_DESCRIPTORS 16
126
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300127#define EDMA_CHANNEL_ANY -1 /* for edma_alloc_channel() */
128#define EDMA_SLOT_ANY -1 /* for edma_alloc_slot() */
129#define EDMA_CONT_PARAMS_ANY 1001
130#define EDMA_CONT_PARAMS_FIXED_EXACT 1002
131#define EDMA_CONT_PARAMS_FIXED_NOT_EXACT 1003
132
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300133/* PaRAM slots are laid out like this */
134struct edmacc_param {
135 u32 opt;
136 u32 src;
137 u32 a_b_cnt;
138 u32 dst;
139 u32 src_dst_bidx;
140 u32 link_bcntrld;
141 u32 src_dst_cidx;
142 u32 ccnt;
143} __packed;
144
145/* fields in edmacc_param.opt */
146#define SAM BIT(0)
147#define DAM BIT(1)
148#define SYNCDIM BIT(2)
149#define STATIC BIT(3)
150#define EDMA_FWID (0x07 << 8)
151#define TCCMODE BIT(11)
152#define EDMA_TCC(t) ((t) << 12)
153#define TCINTEN BIT(20)
154#define ITCINTEN BIT(21)
155#define TCCHEN BIT(22)
156#define ITCCHEN BIT(23)
157
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500158struct edma_pset {
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500159 u32 len;
160 dma_addr_t addr;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500161 struct edmacc_param param;
162};
163
Matt Porterc2dde5f2012-08-22 21:09:34 -0400164struct edma_desc {
165 struct virt_dma_desc vdesc;
166 struct list_head node;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500167 enum dma_transfer_direction direction;
Joel Fernandes50a9c702013-10-31 16:31:23 -0500168 int cyclic;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400169 int absync;
170 int pset_nr;
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500171 struct edma_chan *echan;
Joel Fernandes04361d82014-04-28 15:19:31 -0500172 int processed;
173
174 /*
175 * The following 4 elements are used for residue accounting.
176 *
177 * - processed_stat: the number of SG elements we have traversed
178 * so far to cover accounting. This is updated directly to processed
179 * during edma_callback and is always <= processed, because processed
180 * refers to the number of pending transfer (programmed to EDMA
181 * controller), where as processed_stat tracks number of transfers
182 * accounted for so far.
183 *
184 * - residue: The amount of bytes we have left to transfer for this desc
185 *
186 * - residue_stat: The residue in bytes of data we have covered
187 * so far for accounting. This is updated directly to residue
188 * during callbacks to keep it current.
189 *
190 * - sg_len: Tracks the length of the current intermediate transfer,
191 * this is required to update the residue during intermediate transfer
192 * completion callback.
193 */
194 int processed_stat;
195 u32 sg_len;
196 u32 residue;
197 u32 residue_stat;
198
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500199 struct edma_pset pset[0];
Matt Porterc2dde5f2012-08-22 21:09:34 -0400200};
201
202struct edma_cc;
203
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300204struct edma_tc {
205 struct device_node *node;
206 u16 id;
207};
208
Matt Porterc2dde5f2012-08-22 21:09:34 -0400209struct edma_chan {
210 struct virt_dma_chan vchan;
211 struct list_head node;
212 struct edma_desc *edesc;
213 struct edma_cc *ecc;
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300214 struct edma_tc *tc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400215 int ch_num;
216 bool alloced;
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300217 bool hw_triggered;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400218 int slot[EDMA_MAX_SLOTS];
Joel Fernandesc5f47992013-08-29 18:05:43 -0500219 int missed;
Matt Porter661f7cb2013-01-10 13:41:04 -0500220 struct dma_slave_config cfg;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400221};
222
223struct edma_cc {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300224 struct device *dev;
225 struct edma_soc_info *info;
226 void __iomem *base;
227 int id;
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300228 bool legacy_mode;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300229
230 /* eDMA3 resource information */
231 unsigned num_channels;
Peter Ujfalusi633e42b2015-10-16 10:18:04 +0300232 unsigned num_qchannels;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300233 unsigned num_region;
234 unsigned num_slots;
235 unsigned num_tc;
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +0300236 bool chmap_exist;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300237 enum dma_event_q default_queue;
238
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300239 /*
240 * The slot_inuse bit for each PaRAM slot is clear unless the slot is
241 * in use by Linux or if it is allocated to be used by DSP.
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300242 */
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300243 unsigned long *slot_inuse;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300244
Matt Porterc2dde5f2012-08-22 21:09:34 -0400245 struct dma_device dma_slave;
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300246 struct dma_device *dma_memcpy;
Peter Ujfalusicb782052015-10-14 14:42:54 +0300247 struct edma_chan *slave_chans;
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300248 struct edma_tc *tc_list;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400249 int dummy_slot;
250};
251
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300252/* dummy param set used to (re)initialize parameter RAM slots */
253static const struct edmacc_param dummy_paramset = {
254 .link_bcntrld = 0xffff,
255 .ccnt = 1,
256};
257
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300258#define EDMA_BINDING_LEGACY 0
259#define EDMA_BINDING_TPCC 1
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300260static const struct of_device_id edma_of_ids[] = {
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300261 {
262 .compatible = "ti,edma3",
263 .data = (void *)EDMA_BINDING_LEGACY,
264 },
265 {
266 .compatible = "ti,edma3-tpcc",
267 .data = (void *)EDMA_BINDING_TPCC,
268 },
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300269 {}
270};
271
272static inline unsigned int edma_read(struct edma_cc *ecc, int offset)
273{
274 return (unsigned int)__raw_readl(ecc->base + offset);
275}
276
277static inline void edma_write(struct edma_cc *ecc, int offset, int val)
278{
279 __raw_writel(val, ecc->base + offset);
280}
281
282static inline void edma_modify(struct edma_cc *ecc, int offset, unsigned and,
283 unsigned or)
284{
285 unsigned val = edma_read(ecc, offset);
286
287 val &= and;
288 val |= or;
289 edma_write(ecc, offset, val);
290}
291
292static inline void edma_and(struct edma_cc *ecc, int offset, unsigned and)
293{
294 unsigned val = edma_read(ecc, offset);
295
296 val &= and;
297 edma_write(ecc, offset, val);
298}
299
300static inline void edma_or(struct edma_cc *ecc, int offset, unsigned or)
301{
302 unsigned val = edma_read(ecc, offset);
303
304 val |= or;
305 edma_write(ecc, offset, val);
306}
307
308static inline unsigned int edma_read_array(struct edma_cc *ecc, int offset,
309 int i)
310{
311 return edma_read(ecc, offset + (i << 2));
312}
313
314static inline void edma_write_array(struct edma_cc *ecc, int offset, int i,
315 unsigned val)
316{
317 edma_write(ecc, offset + (i << 2), val);
318}
319
320static inline void edma_modify_array(struct edma_cc *ecc, int offset, int i,
321 unsigned and, unsigned or)
322{
323 edma_modify(ecc, offset + (i << 2), and, or);
324}
325
326static inline void edma_or_array(struct edma_cc *ecc, int offset, int i,
327 unsigned or)
328{
329 edma_or(ecc, offset + (i << 2), or);
330}
331
332static inline void edma_or_array2(struct edma_cc *ecc, int offset, int i, int j,
333 unsigned or)
334{
335 edma_or(ecc, offset + ((i * 2 + j) << 2), or);
336}
337
338static inline void edma_write_array2(struct edma_cc *ecc, int offset, int i,
339 int j, unsigned val)
340{
341 edma_write(ecc, offset + ((i * 2 + j) << 2), val);
342}
343
344static inline unsigned int edma_shadow0_read(struct edma_cc *ecc, int offset)
345{
346 return edma_read(ecc, EDMA_SHADOW0 + offset);
347}
348
349static inline unsigned int edma_shadow0_read_array(struct edma_cc *ecc,
350 int offset, int i)
351{
352 return edma_read(ecc, EDMA_SHADOW0 + offset + (i << 2));
353}
354
355static inline void edma_shadow0_write(struct edma_cc *ecc, int offset,
356 unsigned val)
357{
358 edma_write(ecc, EDMA_SHADOW0 + offset, val);
359}
360
361static inline void edma_shadow0_write_array(struct edma_cc *ecc, int offset,
362 int i, unsigned val)
363{
364 edma_write(ecc, EDMA_SHADOW0 + offset + (i << 2), val);
365}
366
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300367static inline unsigned int edma_param_read(struct edma_cc *ecc, int offset,
368 int param_no)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300369{
370 return edma_read(ecc, EDMA_PARM + offset + (param_no << 5));
371}
372
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300373static inline void edma_param_write(struct edma_cc *ecc, int offset,
374 int param_no, unsigned val)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300375{
376 edma_write(ecc, EDMA_PARM + offset + (param_no << 5), val);
377}
378
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300379static inline void edma_param_modify(struct edma_cc *ecc, int offset,
380 int param_no, unsigned and, unsigned or)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300381{
382 edma_modify(ecc, EDMA_PARM + offset + (param_no << 5), and, or);
383}
384
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300385static inline void edma_param_and(struct edma_cc *ecc, int offset, int param_no,
386 unsigned and)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300387{
388 edma_and(ecc, EDMA_PARM + offset + (param_no << 5), and);
389}
390
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300391static inline void edma_param_or(struct edma_cc *ecc, int offset, int param_no,
392 unsigned or)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300393{
394 edma_or(ecc, EDMA_PARM + offset + (param_no << 5), or);
395}
396
397static inline void set_bits(int offset, int len, unsigned long *p)
398{
399 for (; len > 0; len--)
400 set_bit(offset + (len - 1), p);
401}
402
403static inline void clear_bits(int offset, int len, unsigned long *p)
404{
405 for (; len > 0; len--)
406 clear_bit(offset + (len - 1), p);
407}
408
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300409static void edma_assign_priority_to_queue(struct edma_cc *ecc, int queue_no,
410 int priority)
411{
412 int bit = queue_no * 4;
413
414 edma_modify(ecc, EDMA_QUEPRI, ~(0x7 << bit), ((priority & 0x7) << bit));
415}
416
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300417static void edma_set_chmap(struct edma_chan *echan, int slot)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300418{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300419 struct edma_cc *ecc = echan->ecc;
420 int channel = EDMA_CHAN_SLOT(echan->ch_num);
421
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300422 if (ecc->chmap_exist) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300423 slot = EDMA_CHAN_SLOT(slot);
424 edma_write_array(ecc, EDMA_DCHMAP, channel, (slot << 5));
425 }
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300426}
427
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300428static void edma_setup_interrupt(struct edma_chan *echan, bool enable)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300429{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300430 struct edma_cc *ecc = echan->ecc;
431 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300432
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300433 if (enable) {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300434 edma_shadow0_write_array(ecc, SH_ICR, channel >> 5,
435 BIT(channel & 0x1f));
436 edma_shadow0_write_array(ecc, SH_IESR, channel >> 5,
437 BIT(channel & 0x1f));
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300438 } else {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300439 edma_shadow0_write_array(ecc, SH_IECR, channel >> 5,
440 BIT(channel & 0x1f));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300441 }
442}
443
444/*
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300445 * paRAM slot management functions
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300446 */
447static void edma_write_slot(struct edma_cc *ecc, unsigned slot,
448 const struct edmacc_param *param)
449{
450 slot = EDMA_CHAN_SLOT(slot);
451 if (slot >= ecc->num_slots)
452 return;
453 memcpy_toio(ecc->base + PARM_OFFSET(slot), param, PARM_SIZE);
454}
455
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300456static void edma_read_slot(struct edma_cc *ecc, unsigned slot,
457 struct edmacc_param *param)
458{
459 slot = EDMA_CHAN_SLOT(slot);
460 if (slot >= ecc->num_slots)
461 return;
462 memcpy_fromio(param, ecc->base + PARM_OFFSET(slot), PARM_SIZE);
463}
464
465/**
466 * edma_alloc_slot - allocate DMA parameter RAM
467 * @ecc: pointer to edma_cc struct
468 * @slot: specific slot to allocate; negative for "any unused slot"
469 *
470 * This allocates a parameter RAM slot, initializing it to hold a
471 * dummy transfer. Slots allocated using this routine have not been
472 * mapped to a hardware DMA channel, and will normally be used by
473 * linking to them from a slot associated with a DMA channel.
474 *
475 * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
476 * slots may be allocated on behalf of DSP firmware.
477 *
478 * Returns the number of the slot, else negative errno.
479 */
480static int edma_alloc_slot(struct edma_cc *ecc, int slot)
481{
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300482 if (slot > 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300483 slot = EDMA_CHAN_SLOT(slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300484 /* Requesting entry paRAM slot for a HW triggered channel. */
485 if (ecc->chmap_exist && slot < ecc->num_channels)
486 slot = EDMA_SLOT_ANY;
487 }
488
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300489 if (slot < 0) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300490 if (ecc->chmap_exist)
491 slot = 0;
492 else
493 slot = ecc->num_channels;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300494 for (;;) {
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300495 slot = find_next_zero_bit(ecc->slot_inuse,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300496 ecc->num_slots,
497 slot);
498 if (slot == ecc->num_slots)
499 return -ENOMEM;
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300500 if (!test_and_set_bit(slot, ecc->slot_inuse))
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300501 break;
502 }
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300503 } else if (slot >= ecc->num_slots) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300504 return -EINVAL;
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300505 } else if (test_and_set_bit(slot, ecc->slot_inuse)) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300506 return -EBUSY;
507 }
508
509 edma_write_slot(ecc, slot, &dummy_paramset);
510
511 return EDMA_CTLR_CHAN(ecc->id, slot);
512}
513
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300514static void edma_free_slot(struct edma_cc *ecc, unsigned slot)
515{
516 slot = EDMA_CHAN_SLOT(slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +0300517 if (slot >= ecc->num_slots)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300518 return;
519
520 edma_write_slot(ecc, slot, &dummy_paramset);
Peter Ujfalusi7a73b132015-10-14 14:43:05 +0300521 clear_bit(slot, ecc->slot_inuse);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300522}
523
524/**
525 * edma_link - link one parameter RAM slot to another
526 * @ecc: pointer to edma_cc struct
527 * @from: parameter RAM slot originating the link
528 * @to: parameter RAM slot which is the link target
529 *
530 * The originating slot should not be part of any active DMA transfer.
531 */
532static void edma_link(struct edma_cc *ecc, unsigned from, unsigned to)
533{
Peter Ujfalusifc014092015-10-14 14:42:59 +0300534 if (unlikely(EDMA_CTLR(from) != EDMA_CTLR(to)))
535 dev_warn(ecc->dev, "Ignoring eDMA instance for linking\n");
536
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300537 from = EDMA_CHAN_SLOT(from);
538 to = EDMA_CHAN_SLOT(to);
539 if (from >= ecc->num_slots || to >= ecc->num_slots)
540 return;
541
Peter Ujfalusid9c345d2015-10-16 10:18:02 +0300542 edma_param_modify(ecc, PARM_LINK_BCNTRLD, from, 0xffff0000,
543 PARM_OFFSET(to));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300544}
545
546/**
547 * edma_get_position - returns the current transfer point
548 * @ecc: pointer to edma_cc struct
549 * @slot: parameter RAM slot being examined
550 * @dst: true selects the dest position, false the source
551 *
552 * Returns the position of the current active slot
553 */
554static dma_addr_t edma_get_position(struct edma_cc *ecc, unsigned slot,
555 bool dst)
556{
557 u32 offs;
558
559 slot = EDMA_CHAN_SLOT(slot);
560 offs = PARM_OFFSET(slot);
561 offs += dst ? PARM_DST : PARM_SRC;
562
563 return edma_read(ecc, offs);
564}
565
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300566/*
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300567 * Channels with event associations will be triggered by their hardware
568 * events, and channels without such associations will be triggered by
569 * software. (At this writing there is no interface for using software
570 * triggers except with channels that don't support hardware triggers.)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300571 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300572static void edma_start(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300573{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300574 struct edma_cc *ecc = echan->ecc;
575 int channel = EDMA_CHAN_SLOT(echan->ch_num);
576 int j = (channel >> 5);
577 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300578
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300579 if (!echan->hw_triggered) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300580 /* EDMA channels without event association */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300581 dev_dbg(ecc->dev, "ESR%d %08x\n", j,
582 edma_shadow0_read_array(ecc, SH_ESR, j));
583 edma_shadow0_write_array(ecc, SH_ESR, j, mask);
584 } else {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300585 /* EDMA channel with event association */
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300586 dev_dbg(ecc->dev, "ER%d %08x\n", j,
587 edma_shadow0_read_array(ecc, SH_ER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300588 /* Clear any pending event or error */
589 edma_write_array(ecc, EDMA_ECR, j, mask);
590 edma_write_array(ecc, EDMA_EMCR, j, mask);
591 /* Clear any SER */
592 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
593 edma_shadow0_write_array(ecc, SH_EESR, j, mask);
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300594 dev_dbg(ecc->dev, "EER%d %08x\n", j,
595 edma_shadow0_read_array(ecc, SH_EER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300596 }
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300597}
598
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300599static void edma_stop(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300600{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300601 struct edma_cc *ecc = echan->ecc;
602 int channel = EDMA_CHAN_SLOT(echan->ch_num);
603 int j = (channel >> 5);
604 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300605
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300606 edma_shadow0_write_array(ecc, SH_EECR, j, mask);
607 edma_shadow0_write_array(ecc, SH_ECR, j, mask);
608 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
609 edma_write_array(ecc, EDMA_EMCR, j, mask);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300610
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300611 /* clear possibly pending completion interrupt */
612 edma_shadow0_write_array(ecc, SH_ICR, j, mask);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300613
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300614 dev_dbg(ecc->dev, "EER%d %08x\n", j,
615 edma_shadow0_read_array(ecc, SH_EER, j));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300616
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300617 /* REVISIT: consider guarding against inappropriate event
618 * chaining by overwriting with dummy_paramset.
619 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300620}
621
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300622/*
623 * Temporarily disable EDMA hardware events on the specified channel,
624 * preventing them from triggering new transfers
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300625 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300626static void edma_pause(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300627{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300628 int channel = EDMA_CHAN_SLOT(echan->ch_num);
629 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300630
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300631 edma_shadow0_write_array(echan->ecc, SH_EECR, channel >> 5, mask);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300632}
633
Peter Ujfalusi11c15732015-10-14 14:43:00 +0300634/* Re-enable EDMA hardware events on the specified channel. */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300635static void edma_resume(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300636{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300637 int channel = EDMA_CHAN_SLOT(echan->ch_num);
638 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300639
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300640 edma_shadow0_write_array(echan->ecc, SH_EESR, channel >> 5, mask);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300641}
642
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300643static void edma_trigger_channel(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300644{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300645 struct edma_cc *ecc = echan->ecc;
646 int channel = EDMA_CHAN_SLOT(echan->ch_num);
647 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300648
649 edma_shadow0_write_array(ecc, SH_ESR, (channel >> 5), mask);
650
Peter Ujfalusi3287fb42015-10-14 14:42:57 +0300651 dev_dbg(ecc->dev, "ESR%d %08x\n", (channel >> 5),
652 edma_shadow0_read_array(ecc, SH_ESR, (channel >> 5)));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300653}
654
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300655static void edma_clean_channel(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300656{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300657 struct edma_cc *ecc = echan->ecc;
658 int channel = EDMA_CHAN_SLOT(echan->ch_num);
659 int j = (channel >> 5);
660 unsigned int mask = BIT(channel & 0x1f);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300661
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300662 dev_dbg(ecc->dev, "EMR%d %08x\n", j, edma_read_array(ecc, EDMA_EMR, j));
663 edma_shadow0_write_array(ecc, SH_ECR, j, mask);
664 /* Clear the corresponding EMR bits */
665 edma_write_array(ecc, EDMA_EMCR, j, mask);
666 /* Clear any SER */
667 edma_shadow0_write_array(ecc, SH_SECR, j, mask);
668 edma_write(ecc, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300669}
670
Peter Ujfalusif9425de2015-10-16 10:18:03 +0300671/* Move channel to a specific event queue */
672static void edma_assign_channel_eventq(struct edma_chan *echan,
673 enum dma_event_q eventq_no)
674{
675 struct edma_cc *ecc = echan->ecc;
676 int channel = EDMA_CHAN_SLOT(echan->ch_num);
677 int bit = (channel & 0x7) * 4;
678
679 /* default to low priority queue */
680 if (eventq_no == EVENTQ_DEFAULT)
681 eventq_no = ecc->default_queue;
682 if (eventq_no >= ecc->num_tc)
683 return;
684
685 eventq_no &= 7;
686 edma_modify_array(ecc, EDMA_DMAQNUM, (channel >> 3), ~(0x7 << bit),
687 eventq_no << bit);
688}
689
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300690static int edma_alloc_channel(struct edma_chan *echan,
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +0300691 enum dma_event_q eventq_no)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300692{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300693 struct edma_cc *ecc = echan->ecc;
694 int channel = EDMA_CHAN_SLOT(echan->ch_num);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300695
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300696 /* ensure access through shadow region 0 */
697 edma_or_array2(ecc, EDMA_DRAE, 0, channel >> 5, BIT(channel & 0x1f));
698
699 /* ensure no events are pending */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300700 edma_stop(echan);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300701
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300702 edma_setup_interrupt(echan, true);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300703
Peter Ujfalusif9425de2015-10-16 10:18:03 +0300704 edma_assign_channel_eventq(echan, eventq_no);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300705
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300706 return 0;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300707}
708
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300709static void edma_free_channel(struct edma_chan *echan)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300710{
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300711 /* ensure no events are pending */
712 edma_stop(echan);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300713 /* REVISIT should probably take out of shadow region 0 */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300714 edma_setup_interrupt(echan, false);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300715}
716
Matt Porterc2dde5f2012-08-22 21:09:34 -0400717static inline struct edma_cc *to_edma_cc(struct dma_device *d)
718{
719 return container_of(d, struct edma_cc, dma_slave);
720}
721
722static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
723{
724 return container_of(c, struct edma_chan, vchan.chan);
725}
726
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300727static inline struct edma_desc *to_edma_desc(struct dma_async_tx_descriptor *tx)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400728{
729 return container_of(tx, struct edma_desc, vdesc.tx);
730}
731
732static void edma_desc_free(struct virt_dma_desc *vdesc)
733{
734 kfree(container_of(vdesc, struct edma_desc, vdesc));
735}
736
737/* Dispatch a queued descriptor to the controller (caller holds lock) */
738static void edma_execute(struct edma_chan *echan)
739{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300740 struct edma_cc *ecc = echan->ecc;
Joel Fernandes53407062013-09-03 10:02:46 -0500741 struct virt_dma_desc *vdesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400742 struct edma_desc *edesc;
Joel Fernandes53407062013-09-03 10:02:46 -0500743 struct device *dev = echan->vchan.chan.device->dev;
744 int i, j, left, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400745
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300746 if (!echan->edesc) {
747 /* Setup is needed for the first transfer */
Joel Fernandes53407062013-09-03 10:02:46 -0500748 vdesc = vchan_next_desc(&echan->vchan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300749 if (!vdesc)
Joel Fernandes53407062013-09-03 10:02:46 -0500750 return;
Joel Fernandes53407062013-09-03 10:02:46 -0500751 list_del(&vdesc->node);
752 echan->edesc = to_edma_desc(&vdesc->tx);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400753 }
754
Joel Fernandes53407062013-09-03 10:02:46 -0500755 edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400756
Joel Fernandes53407062013-09-03 10:02:46 -0500757 /* Find out how many left */
758 left = edesc->pset_nr - edesc->processed;
759 nslots = min(MAX_NR_SG, left);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500760 edesc->sg_len = 0;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400761
762 /* Write descriptor PaRAM set(s) */
Joel Fernandes53407062013-09-03 10:02:46 -0500763 for (i = 0; i < nslots; i++) {
764 j = i + edesc->processed;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300765 edma_write_slot(ecc, echan->slot[i], &edesc->pset[j].param);
Thomas Gleixner740b41f2014-04-28 14:34:11 -0500766 edesc->sg_len += edesc->pset[j].len;
Peter Ujfalusi907f74a2015-10-14 14:42:56 +0300767 dev_vdbg(dev,
768 "\n pset[%d]:\n"
769 " chnum\t%d\n"
770 " slot\t%d\n"
771 " opt\t%08x\n"
772 " src\t%08x\n"
773 " dst\t%08x\n"
774 " abcnt\t%08x\n"
775 " ccnt\t%08x\n"
776 " bidx\t%08x\n"
777 " cidx\t%08x\n"
778 " lkrld\t%08x\n",
779 j, echan->ch_num, echan->slot[i],
780 edesc->pset[j].param.opt,
781 edesc->pset[j].param.src,
782 edesc->pset[j].param.dst,
783 edesc->pset[j].param.a_b_cnt,
784 edesc->pset[j].param.ccnt,
785 edesc->pset[j].param.src_dst_bidx,
786 edesc->pset[j].param.src_dst_cidx,
787 edesc->pset[j].param.link_bcntrld);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400788 /* Link to the previous slot if not the last set */
Joel Fernandes53407062013-09-03 10:02:46 -0500789 if (i != (nslots - 1))
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300790 edma_link(ecc, echan->slot[i], echan->slot[i + 1]);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400791 }
792
Joel Fernandes53407062013-09-03 10:02:46 -0500793 edesc->processed += nslots;
794
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500795 /*
796 * If this is either the last set in a set of SG-list transactions
797 * then setup a link to the dummy slot, this results in all future
798 * events being absorbed and that's OK because we're done
799 */
Joel Fernandes50a9c702013-10-31 16:31:23 -0500800 if (edesc->processed == edesc->pset_nr) {
801 if (edesc->cyclic)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300802 edma_link(ecc, echan->slot[nslots - 1], echan->slot[1]);
Joel Fernandes50a9c702013-10-31 16:31:23 -0500803 else
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300804 edma_link(ecc, echan->slot[nslots - 1],
Joel Fernandes50a9c702013-10-31 16:31:23 -0500805 echan->ecc->dummy_slot);
806 }
Joel Fernandesb267b3b2013-08-29 18:05:44 -0500807
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300808 if (echan->missed) {
809 /*
810 * This happens due to setup times between intermediate
811 * transfers in long SG lists which have to be broken up into
812 * transfers of MAX_NR_SG
813 */
814 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300815 edma_clean_channel(echan);
816 edma_stop(echan);
817 edma_start(echan);
818 edma_trigger_channel(echan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300819 echan->missed = 0;
820 } else if (edesc->processed <= MAX_NR_SG) {
Peter Ujfalusi9aac9092014-04-24 10:29:50 +0300821 dev_dbg(dev, "first transfer starting on channel %d\n",
822 echan->ch_num);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300823 edma_start(echan);
Sekhar Nori5fc68a62014-03-19 11:25:50 +0530824 } else {
825 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
826 echan->ch_num, edesc->processed);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300827 edma_resume(echan);
Joel Fernandes53407062013-09-03 10:02:46 -0500828 }
Matt Porterc2dde5f2012-08-22 21:09:34 -0400829}
830
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100831static int edma_terminate_all(struct dma_chan *chan)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400832{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100833 struct edma_chan *echan = to_edma_chan(chan);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400834 unsigned long flags;
835 LIST_HEAD(head);
836
837 spin_lock_irqsave(&echan->vchan.lock, flags);
838
839 /*
840 * Stop DMA activity: we assume the callback will not be called
841 * after edma_dma() returns (even if it does, it will see
842 * echan->edesc is NULL and exit.)
843 */
844 if (echan->edesc) {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300845 edma_stop(echan);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +0300846 /* Move the cyclic channel back to default queue */
Peter Ujfalusi1be53362015-10-16 10:18:10 +0300847 if (!echan->tc && echan->edesc->cyclic)
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300848 edma_assign_channel_eventq(echan, EVENTQ_DEFAULT);
Petr Kulhavy5ca9e7c2015-03-27 13:35:51 +0200849 /*
850 * free the running request descriptor
851 * since it is not in any of the vdesc lists
852 */
853 edma_desc_free(&echan->edesc->vdesc);
Matt Porterc2dde5f2012-08-22 21:09:34 -0400854 echan->edesc = NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -0400855 }
856
857 vchan_get_all_descriptors(&echan->vchan, &head);
858 spin_unlock_irqrestore(&echan->vchan.lock, flags);
859 vchan_dma_desc_free_list(&echan->vchan, &head);
860
861 return 0;
862}
863
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100864static int edma_slave_config(struct dma_chan *chan,
Matt Porter661f7cb2013-01-10 13:41:04 -0500865 struct dma_slave_config *cfg)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400866{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100867 struct edma_chan *echan = to_edma_chan(chan);
868
Matt Porter661f7cb2013-01-10 13:41:04 -0500869 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
870 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
Matt Porterc2dde5f2012-08-22 21:09:34 -0400871 return -EINVAL;
872
Matt Porter661f7cb2013-01-10 13:41:04 -0500873 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
Matt Porterc2dde5f2012-08-22 21:09:34 -0400874
875 return 0;
876}
877
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100878static int edma_dma_pause(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300879{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100880 struct edma_chan *echan = to_edma_chan(chan);
881
John Ogness02ec6042015-04-27 13:52:25 +0200882 if (!echan->edesc)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300883 return -EINVAL;
884
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300885 edma_pause(echan);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300886 return 0;
887}
888
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100889static int edma_dma_resume(struct dma_chan *chan)
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300890{
Maxime Ripardaa7c09b2014-11-17 14:42:13 +0100891 struct edma_chan *echan = to_edma_chan(chan);
892
Peter Ujfalusi34cf3012015-10-16 10:18:01 +0300893 edma_resume(echan);
Peter Ujfalusi72c7b672014-04-14 14:41:59 +0300894 return 0;
895}
896
Joel Fernandesfd009032013-09-23 18:05:13 -0500897/*
898 * A PaRAM set configuration abstraction used by other modes
899 * @chan: Channel who's PaRAM set we're configuring
900 * @pset: PaRAM set to initialize and setup.
901 * @src_addr: Source address of the DMA
902 * @dst_addr: Destination address of the DMA
903 * @burst: In units of dev_width, how much to send
904 * @dev_width: How much is the dev_width
905 * @dma_length: Total length of the DMA transfer
906 * @direction: Direction of the transfer
907 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500908static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300909 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
Peter Ujfalusidf6694f2015-10-16 10:18:00 +0300910 unsigned int acnt, unsigned int dma_length,
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +0300911 enum dma_transfer_direction direction)
Joel Fernandesfd009032013-09-23 18:05:13 -0500912{
913 struct edma_chan *echan = to_edma_chan(chan);
914 struct device *dev = chan->device->dev;
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500915 struct edmacc_param *param = &epset->param;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +0300916 int bcnt, ccnt, cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500917 int src_bidx, dst_bidx, src_cidx, dst_cidx;
918 int absync;
919
Peter Ujfalusib2b617d2014-04-14 14:41:58 +0300920 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
921 if (!burst)
922 burst = 1;
Joel Fernandesfd009032013-09-23 18:05:13 -0500923 /*
924 * If the maxburst is equal to the fifo width, use
925 * A-synced transfers. This allows for large contiguous
926 * buffer transfers using only one PaRAM set.
927 */
928 if (burst == 1) {
929 /*
930 * For the A-sync case, bcnt and ccnt are the remainder
931 * and quotient respectively of the division of:
932 * (dma_length / acnt) by (SZ_64K -1). This is so
933 * that in case bcnt over flows, we have ccnt to use.
934 * Note: In A-sync tranfer only, bcntrld is used, but it
935 * only applies for sg_dma_len(sg) >= SZ_64K.
936 * In this case, the best way adopted is- bccnt for the
937 * first frame will be the remainder below. Then for
938 * every successive frame, bcnt will be SZ_64K-1. This
939 * is assured as bcntrld = 0xffff in end of function.
940 */
941 absync = false;
942 ccnt = dma_length / acnt / (SZ_64K - 1);
943 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
944 /*
945 * If bcnt is non-zero, we have a remainder and hence an
946 * extra frame to transfer, so increment ccnt.
947 */
948 if (bcnt)
949 ccnt++;
950 else
951 bcnt = SZ_64K - 1;
952 cidx = acnt;
953 } else {
954 /*
955 * If maxburst is greater than the fifo address_width,
956 * use AB-synced transfers where A count is the fifo
957 * address_width and B count is the maxburst. In this
958 * case, we are limited to transfers of C count frames
959 * of (address_width * maxburst) where C count is limited
960 * to SZ_64K-1. This places an upper bound on the length
961 * of an SG segment that can be handled.
962 */
963 absync = true;
964 bcnt = burst;
965 ccnt = dma_length / (acnt * bcnt);
966 if (ccnt > (SZ_64K - 1)) {
967 dev_err(dev, "Exceeded max SG segment size\n");
968 return -EINVAL;
969 }
970 cidx = acnt * bcnt;
971 }
972
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500973 epset->len = dma_length;
974
Joel Fernandesfd009032013-09-23 18:05:13 -0500975 if (direction == DMA_MEM_TO_DEV) {
976 src_bidx = acnt;
977 src_cidx = cidx;
978 dst_bidx = 0;
979 dst_cidx = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500980 epset->addr = src_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -0500981 } else if (direction == DMA_DEV_TO_MEM) {
982 src_bidx = 0;
983 src_cidx = 0;
984 dst_bidx = acnt;
985 dst_cidx = cidx;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -0500986 epset->addr = dst_addr;
Joel Fernandes8cc3e302014-04-18 21:50:33 -0500987 } else if (direction == DMA_MEM_TO_MEM) {
988 src_bidx = acnt;
989 src_cidx = cidx;
990 dst_bidx = acnt;
991 dst_cidx = cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -0500992 } else {
993 dev_err(dev, "%s: direction not implemented yet\n", __func__);
994 return -EINVAL;
995 }
996
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -0500997 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
Joel Fernandesfd009032013-09-23 18:05:13 -0500998 /* Configure A or AB synchronized transfers */
999 if (absync)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001000 param->opt |= SYNCDIM;
Joel Fernandesfd009032013-09-23 18:05:13 -05001001
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001002 param->src = src_addr;
1003 param->dst = dst_addr;
Joel Fernandesfd009032013-09-23 18:05:13 -05001004
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001005 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
1006 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
Joel Fernandesfd009032013-09-23 18:05:13 -05001007
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001008 param->a_b_cnt = bcnt << 16 | acnt;
1009 param->ccnt = ccnt;
Joel Fernandesfd009032013-09-23 18:05:13 -05001010 /*
1011 * Only time when (bcntrld) auto reload is required is for
1012 * A-sync case, and in this case, a requirement of reload value
1013 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
1014 * and then later will be populated by edma_execute.
1015 */
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001016 param->link_bcntrld = 0xffffffff;
Joel Fernandesfd009032013-09-23 18:05:13 -05001017 return absync;
1018}
1019
Matt Porterc2dde5f2012-08-22 21:09:34 -04001020static struct dma_async_tx_descriptor *edma_prep_slave_sg(
1021 struct dma_chan *chan, struct scatterlist *sgl,
1022 unsigned int sg_len, enum dma_transfer_direction direction,
1023 unsigned long tx_flags, void *context)
1024{
1025 struct edma_chan *echan = to_edma_chan(chan);
1026 struct device *dev = chan->device->dev;
1027 struct edma_desc *edesc;
Joel Fernandesfd009032013-09-23 18:05:13 -05001028 dma_addr_t src_addr = 0, dst_addr = 0;
Matt Porter661f7cb2013-01-10 13:41:04 -05001029 enum dma_slave_buswidth dev_width;
1030 u32 burst;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001031 struct scatterlist *sg;
Joel Fernandesfd009032013-09-23 18:05:13 -05001032 int i, nslots, ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001033
1034 if (unlikely(!echan || !sgl || !sg_len))
1035 return NULL;
1036
Matt Porter661f7cb2013-01-10 13:41:04 -05001037 if (direction == DMA_DEV_TO_MEM) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001038 src_addr = echan->cfg.src_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001039 dev_width = echan->cfg.src_addr_width;
1040 burst = echan->cfg.src_maxburst;
1041 } else if (direction == DMA_MEM_TO_DEV) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001042 dst_addr = echan->cfg.dst_addr;
Matt Porter661f7cb2013-01-10 13:41:04 -05001043 dev_width = echan->cfg.dst_addr_width;
1044 burst = echan->cfg.dst_maxburst;
1045 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001046 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Matt Porter661f7cb2013-01-10 13:41:04 -05001047 return NULL;
1048 }
1049
1050 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001051 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001052 return NULL;
1053 }
1054
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001055 edesc = kzalloc(sizeof(*edesc) + sg_len * sizeof(edesc->pset[0]),
1056 GFP_ATOMIC);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001057 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001058 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001059 return NULL;
1060 }
1061
1062 edesc->pset_nr = sg_len;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001063 edesc->residue = 0;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001064 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001065 edesc->echan = echan;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001066
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001067 /* Allocate a PaRAM slot, if needed */
1068 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
1069
1070 for (i = 0; i < nslots; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001071 if (echan->slot[i] < 0) {
1072 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001073 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001074 if (echan->slot[i] < 0) {
Valentin Ilie4b6271a2013-10-24 16:14:22 +03001075 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001076 dev_err(dev, "%s: Failed to allocate slot\n",
1077 __func__);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001078 return NULL;
1079 }
1080 }
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001081 }
1082
1083 /* Configure PaRAM sets for each SG */
1084 for_each_sg(sgl, sg, sg_len, i) {
Joel Fernandesfd009032013-09-23 18:05:13 -05001085 /* Get address for each SG */
1086 if (direction == DMA_DEV_TO_MEM)
1087 dst_addr = sg_dma_address(sg);
1088 else
1089 src_addr = sg_dma_address(sg);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001090
Joel Fernandesfd009032013-09-23 18:05:13 -05001091 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1092 dst_addr, burst, dev_width,
1093 sg_dma_len(sg), direction);
Vinod Koulb967aec2013-10-30 13:07:18 +05301094 if (ret < 0) {
1095 kfree(edesc);
Joel Fernandesfd009032013-09-23 18:05:13 -05001096 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001097 }
1098
Joel Fernandesfd009032013-09-23 18:05:13 -05001099 edesc->absync = ret;
Thomas Gleixnerb6205c32014-04-28 14:18:45 -05001100 edesc->residue += sg_dma_len(sg);
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001101
1102 /* If this is the last in a current SG set of transactions,
1103 enable interrupts so that next set is processed */
1104 if (!((i+1) % MAX_NR_SG))
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001105 edesc->pset[i].param.opt |= TCINTEN;
Joel Fernandes6fbe24d2013-08-29 18:05:40 -05001106
Matt Porterc2dde5f2012-08-22 21:09:34 -04001107 /* If this is the last set, enable completion interrupt flag */
1108 if (i == sg_len - 1)
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001109 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001110 }
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001111 edesc->residue_stat = edesc->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001112
Matt Porterc2dde5f2012-08-22 21:09:34 -04001113 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1114}
Matt Porterc2dde5f2012-08-22 21:09:34 -04001115
Lad, Prabhakarb7a4fd52015-02-04 13:03:27 +00001116static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001117 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1118 size_t len, unsigned long tx_flags)
1119{
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001120 int ret, nslots;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001121 struct edma_desc *edesc;
1122 struct device *dev = chan->device->dev;
1123 struct edma_chan *echan = to_edma_chan(chan);
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001124 unsigned int width, pset_len;
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001125
1126 if (unlikely(!echan || !len))
1127 return NULL;
1128
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001129 if (len < SZ_64K) {
1130 /*
1131 * Transfer size less than 64K can be handled with one paRAM
1132 * slot and with one burst.
1133 * ACNT = length
1134 */
1135 width = len;
1136 pset_len = len;
1137 nslots = 1;
1138 } else {
1139 /*
1140 * Transfer size bigger than 64K will be handled with maximum of
1141 * two paRAM slots.
1142 * slot1: (full_length / 32767) times 32767 bytes bursts.
1143 * ACNT = 32767, length1: (full_length / 32767) * 32767
1144 * slot2: the remaining amount of data after slot1.
1145 * ACNT = full_length - length1, length2 = ACNT
1146 *
1147 * When the full_length is multibple of 32767 one slot can be
1148 * used to complete the transfer.
1149 */
1150 width = SZ_32K - 1;
1151 pset_len = rounddown(len, width);
1152 /* One slot is enough for lengths multiple of (SZ_32K -1) */
1153 if (unlikely(pset_len == len))
1154 nslots = 1;
1155 else
1156 nslots = 2;
1157 }
1158
1159 edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]),
1160 GFP_ATOMIC);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001161 if (!edesc) {
1162 dev_dbg(dev, "Failed to allocate a descriptor\n");
1163 return NULL;
1164 }
1165
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001166 edesc->pset_nr = nslots;
1167 edesc->residue = edesc->residue_stat = len;
1168 edesc->direction = DMA_MEM_TO_MEM;
1169 edesc->echan = echan;
Peter Ujfalusi21a31842015-10-16 10:17:59 +03001170
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001171 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001172 width, pset_len, DMA_MEM_TO_MEM);
1173 if (ret < 0) {
1174 kfree(edesc);
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001175 return NULL;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001176 }
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001177
1178 edesc->absync = ret;
1179
Joel Fernandesb0cce4c2014-04-28 15:30:32 -05001180 edesc->pset[0].param.opt |= ITCCHEN;
Peter Ujfalusidf6694f2015-10-16 10:18:00 +03001181 if (nslots == 1) {
1182 /* Enable transfer complete interrupt */
1183 edesc->pset[0].param.opt |= TCINTEN;
1184 } else {
1185 /* Enable transfer complete chaining for the first slot */
1186 edesc->pset[0].param.opt |= TCCHEN;
1187
1188 if (echan->slot[1] < 0) {
1189 echan->slot[1] = edma_alloc_slot(echan->ecc,
1190 EDMA_SLOT_ANY);
1191 if (echan->slot[1] < 0) {
1192 kfree(edesc);
1193 dev_err(dev, "%s: Failed to allocate slot\n",
1194 __func__);
1195 return NULL;
1196 }
1197 }
1198 dest += pset_len;
1199 src += pset_len;
1200 pset_len = width = len % (SZ_32K - 1);
1201
1202 ret = edma_config_pset(chan, &edesc->pset[1], src, dest, 1,
1203 width, pset_len, DMA_MEM_TO_MEM);
1204 if (ret < 0) {
1205 kfree(edesc);
1206 return NULL;
1207 }
1208
1209 edesc->pset[1].param.opt |= ITCCHEN;
1210 edesc->pset[1].param.opt |= TCINTEN;
1211 }
Joel Fernandes8cc3e302014-04-18 21:50:33 -05001212
1213 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1214}
1215
Joel Fernandes50a9c702013-10-31 16:31:23 -05001216static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
1217 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1218 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +02001219 unsigned long tx_flags)
Joel Fernandes50a9c702013-10-31 16:31:23 -05001220{
1221 struct edma_chan *echan = to_edma_chan(chan);
1222 struct device *dev = chan->device->dev;
1223 struct edma_desc *edesc;
1224 dma_addr_t src_addr, dst_addr;
1225 enum dma_slave_buswidth dev_width;
1226 u32 burst;
1227 int i, ret, nslots;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001228
Joel Fernandes50a9c702013-10-31 16:31:23 -05001229 if (unlikely(!echan || !buf_len || !period_len))
1230 return NULL;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001231
Joel Fernandes50a9c702013-10-31 16:31:23 -05001232 if (direction == DMA_DEV_TO_MEM) {
1233 src_addr = echan->cfg.src_addr;
1234 dst_addr = buf_addr;
1235 dev_width = echan->cfg.src_addr_width;
1236 burst = echan->cfg.src_maxburst;
1237 } else if (direction == DMA_MEM_TO_DEV) {
1238 src_addr = buf_addr;
1239 dst_addr = echan->cfg.dst_addr;
1240 dev_width = echan->cfg.dst_addr_width;
1241 burst = echan->cfg.dst_maxburst;
1242 } else {
Peter Ujfalusie6fad592014-04-14 14:42:05 +03001243 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001244 return NULL;
1245 }
1246
1247 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001248 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001249 return NULL;
1250 }
1251
1252 if (unlikely(buf_len % period_len)) {
1253 dev_err(dev, "Period should be multiple of Buffer length\n");
1254 return NULL;
1255 }
1256
1257 nslots = (buf_len / period_len) + 1;
1258
1259 /*
1260 * Cyclic DMA users such as audio cannot tolerate delays introduced
1261 * by cases where the number of periods is more than the maximum
1262 * number of SGs the EDMA driver can handle at a time. For DMA types
1263 * such as Slave SGs, such delays are tolerable and synchronized,
1264 * but the synchronization is difficult to achieve with Cyclic and
1265 * cannot be guaranteed, so we error out early.
1266 */
1267 if (nslots > MAX_NR_SG)
1268 return NULL;
1269
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001270 edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]),
1271 GFP_ATOMIC);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001272 if (!edesc) {
Peter Ujfalusic594c892014-04-14 14:42:03 +03001273 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001274 return NULL;
1275 }
1276
1277 edesc->cyclic = 1;
1278 edesc->pset_nr = nslots;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001279 edesc->residue = edesc->residue_stat = buf_len;
Thomas Gleixnerc2da2342014-04-28 14:29:57 -05001280 edesc->direction = direction;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001281 edesc->echan = echan;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001282
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001283 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
1284 __func__, echan->ch_num, nslots, period_len, buf_len);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001285
1286 for (i = 0; i < nslots; i++) {
1287 /* Allocate a PaRAM slot, if needed */
1288 if (echan->slot[i] < 0) {
1289 echan->slot[i] =
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001290 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001291 if (echan->slot[i] < 0) {
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001292 kfree(edesc);
Peter Ujfalusic594c892014-04-14 14:42:03 +03001293 dev_err(dev, "%s: Failed to allocate slot\n",
1294 __func__);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001295 return NULL;
1296 }
1297 }
1298
1299 if (i == nslots - 1) {
1300 memcpy(&edesc->pset[i], &edesc->pset[0],
1301 sizeof(edesc->pset[0]));
1302 break;
1303 }
1304
1305 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1306 dst_addr, burst, dev_width, period_len,
1307 direction);
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001308 if (ret < 0) {
1309 kfree(edesc);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001310 return NULL;
Christian Engelmayere3ddc972013-12-30 20:48:39 +01001311 }
Joel Fernandes50a9c702013-10-31 16:31:23 -05001312
1313 if (direction == DMA_DEV_TO_MEM)
1314 dst_addr += period_len;
1315 else
1316 src_addr += period_len;
1317
Peter Ujfalusi83bb3122014-04-14 14:42:02 +03001318 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
1319 dev_vdbg(dev,
Joel Fernandes50a9c702013-10-31 16:31:23 -05001320 "\n pset[%d]:\n"
1321 " chnum\t%d\n"
1322 " slot\t%d\n"
1323 " opt\t%08x\n"
1324 " src\t%08x\n"
1325 " dst\t%08x\n"
1326 " abcnt\t%08x\n"
1327 " ccnt\t%08x\n"
1328 " bidx\t%08x\n"
1329 " cidx\t%08x\n"
1330 " lkrld\t%08x\n",
1331 i, echan->ch_num, echan->slot[i],
Thomas Gleixnerb5088ad2014-04-28 14:23:55 -05001332 edesc->pset[i].param.opt,
1333 edesc->pset[i].param.src,
1334 edesc->pset[i].param.dst,
1335 edesc->pset[i].param.a_b_cnt,
1336 edesc->pset[i].param.ccnt,
1337 edesc->pset[i].param.src_dst_bidx,
1338 edesc->pset[i].param.src_dst_cidx,
1339 edesc->pset[i].param.link_bcntrld);
Joel Fernandes50a9c702013-10-31 16:31:23 -05001340
1341 edesc->absync = ret;
1342
1343 /*
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001344 * Enable period interrupt only if it is requested
Joel Fernandes50a9c702013-10-31 16:31:23 -05001345 */
Peter Ujfalusia1f146f2014-07-16 15:29:21 +03001346 if (tx_flags & DMA_PREP_INTERRUPT)
1347 edesc->pset[i].param.opt |= TCINTEN;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001348 }
1349
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001350 /* Place the cyclic channel to highest priority queue */
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001351 if (!echan->tc)
1352 edma_assign_channel_eventq(echan, EVENTQ_0);
Peter Ujfalusi8e8805d2014-07-08 13:46:38 +03001353
Matt Porterc2dde5f2012-08-22 21:09:34 -04001354 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1355}
1356
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001357static void edma_completion_handler(struct edma_chan *echan)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001358{
Matt Porterc2dde5f2012-08-22 21:09:34 -04001359 struct device *dev = echan->vchan.chan.device->dev;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001360 struct edma_desc *edesc = echan->edesc;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001361
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001362 if (!edesc)
1363 return;
Joel Fernandes50a9c702013-10-31 16:31:23 -05001364
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001365 spin_lock(&echan->vchan.lock);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001366 if (edesc->cyclic) {
1367 vchan_cyclic_callback(&edesc->vdesc);
1368 spin_unlock(&echan->vchan.lock);
1369 return;
1370 } else if (edesc->processed == edesc->pset_nr) {
1371 edesc->residue = 0;
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001372 edma_stop(echan);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001373 vchan_cookie_complete(&edesc->vdesc);
1374 echan->edesc = NULL;
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001375
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001376 dev_dbg(dev, "Transfer completed on channel %d\n",
1377 echan->ch_num);
1378 } else {
1379 dev_dbg(dev, "Sub transfer completed on channel %d\n",
1380 echan->ch_num);
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001381
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001382 edma_pause(echan);
Joel Fernandesc5f47992013-08-29 18:05:43 -05001383
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001384 /* Update statistics for tx_status */
1385 edesc->residue -= edesc->sg_len;
1386 edesc->residue_stat = edesc->residue;
1387 edesc->processed_stat = edesc->processed;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001388 }
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001389 edma_execute(echan);
1390
Peter Ujfalusi8fa7ff42015-10-14 14:42:45 +03001391 spin_unlock(&echan->vchan.lock);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001392}
1393
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001394/* eDMA interrupt handler */
1395static irqreturn_t dma_irq_handler(int irq, void *data)
1396{
1397 struct edma_cc *ecc = data;
1398 int ctlr;
1399 u32 sh_ier;
1400 u32 sh_ipr;
1401 u32 bank;
1402
1403 ctlr = ecc->id;
1404 if (ctlr < 0)
1405 return IRQ_NONE;
1406
1407 dev_vdbg(ecc->dev, "dma_irq_handler\n");
1408
1409 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 0);
1410 if (!sh_ipr) {
1411 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 1);
1412 if (!sh_ipr)
1413 return IRQ_NONE;
1414 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 1);
1415 bank = 1;
1416 } else {
1417 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 0);
1418 bank = 0;
1419 }
1420
1421 do {
1422 u32 slot;
1423 u32 channel;
1424
1425 slot = __ffs(sh_ipr);
1426 sh_ipr &= ~(BIT(slot));
1427
1428 if (sh_ier & BIT(slot)) {
1429 channel = (bank << 5) | slot;
1430 /* Clear the corresponding IPR bits */
1431 edma_shadow0_write_array(ecc, SH_ICR, bank, BIT(slot));
1432 edma_completion_handler(&ecc->slave_chans[channel]);
1433 }
1434 } while (sh_ipr);
1435
1436 edma_shadow0_write(ecc, SH_IEVAL, 1);
1437 return IRQ_HANDLED;
1438}
1439
1440static void edma_error_handler(struct edma_chan *echan)
1441{
1442 struct edma_cc *ecc = echan->ecc;
1443 struct device *dev = echan->vchan.chan.device->dev;
1444 struct edmacc_param p;
1445
1446 if (!echan->edesc)
1447 return;
1448
1449 spin_lock(&echan->vchan.lock);
1450
1451 edma_read_slot(ecc, echan->slot[0], &p);
1452 /*
1453 * Issue later based on missed flag which will be sure
1454 * to happen as:
1455 * (1) we finished transmitting an intermediate slot and
1456 * edma_execute is coming up.
1457 * (2) or we finished current transfer and issue will
1458 * call edma_execute.
1459 *
1460 * Important note: issuing can be dangerous here and
1461 * lead to some nasty recursion when we are in a NULL
1462 * slot. So we avoid doing so and set the missed flag.
1463 */
1464 if (p.a_b_cnt == 0 && p.ccnt == 0) {
1465 dev_dbg(dev, "Error on null slot, setting miss\n");
1466 echan->missed = 1;
1467 } else {
1468 /*
1469 * The slot is already programmed but the event got
1470 * missed, so its safe to issue it here.
1471 */
1472 dev_dbg(dev, "Missed event, TRIGGERING\n");
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001473 edma_clean_channel(echan);
1474 edma_stop(echan);
1475 edma_start(echan);
1476 edma_trigger_channel(echan);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001477 }
1478 spin_unlock(&echan->vchan.lock);
1479}
1480
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001481static inline bool edma_error_pending(struct edma_cc *ecc)
1482{
1483 if (edma_read_array(ecc, EDMA_EMR, 0) ||
1484 edma_read_array(ecc, EDMA_EMR, 1) ||
1485 edma_read(ecc, EDMA_QEMR) || edma_read(ecc, EDMA_CCERR))
1486 return true;
1487
1488 return false;
1489}
1490
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001491/* eDMA error interrupt handler */
1492static irqreturn_t dma_ccerr_handler(int irq, void *data)
1493{
1494 struct edma_cc *ecc = data;
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001495 int i, j;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001496 int ctlr;
1497 unsigned int cnt = 0;
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001498 unsigned int val;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001499
1500 ctlr = ecc->id;
1501 if (ctlr < 0)
1502 return IRQ_NONE;
1503
1504 dev_vdbg(ecc->dev, "dma_ccerr_handler\n");
1505
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001506 if (!edma_error_pending(ecc))
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001507 return IRQ_NONE;
1508
1509 while (1) {
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001510 /* Event missed register(s) */
1511 for (j = 0; j < 2; j++) {
1512 unsigned long emr;
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001513
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001514 val = edma_read_array(ecc, EDMA_EMR, j);
1515 if (!val)
1516 continue;
1517
1518 dev_dbg(ecc->dev, "EMR%d 0x%08x\n", j, val);
1519 emr = val;
1520 for (i = find_next_bit(&emr, 32, 0); i < 32;
1521 i = find_next_bit(&emr, 32, i + 1)) {
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001522 int k = (j << 5) + i;
1523
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001524 /* Clear the corresponding EMR bits */
1525 edma_write_array(ecc, EDMA_EMCR, j, BIT(i));
1526 /* Clear any SER */
1527 edma_shadow0_write_array(ecc, SH_SECR, j,
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001528 BIT(i));
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001529 edma_error_handler(&ecc->slave_chans[k]);
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001530 }
1531 }
Peter Ujfalusie4402a12015-10-14 14:43:03 +03001532
1533 val = edma_read(ecc, EDMA_QEMR);
1534 if (val) {
1535 dev_dbg(ecc->dev, "QEMR 0x%02x\n", val);
1536 /* Not reported, just clear the interrupt reason. */
1537 edma_write(ecc, EDMA_QEMCR, val);
1538 edma_shadow0_write(ecc, SH_QSECR, val);
1539 }
1540
1541 val = edma_read(ecc, EDMA_CCERR);
1542 if (val) {
1543 dev_warn(ecc->dev, "CCERR 0x%08x\n", val);
1544 /* Not reported, just clear the interrupt reason. */
1545 edma_write(ecc, EDMA_CCERRCLR, val);
1546 }
1547
Peter Ujfalusi7c3b8b32015-10-14 14:43:02 +03001548 if (!edma_error_pending(ecc))
Peter Ujfalusi79ad2e32015-10-14 14:43:01 +03001549 break;
1550 cnt++;
1551 if (cnt > 10)
1552 break;
1553 }
1554 edma_write(ecc, EDMA_EEVAL, 1);
1555 return IRQ_HANDLED;
1556}
1557
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001558static void edma_tc_set_pm_state(struct edma_tc *tc, bool enable)
1559{
1560 struct platform_device *tc_pdev;
1561 int ret;
1562
1563 if (!tc)
1564 return;
1565
1566 tc_pdev = of_find_device_by_node(tc->node);
1567 if (!tc_pdev) {
1568 pr_err("%s: TPTC device is not found\n", __func__);
1569 return;
1570 }
1571 if (!pm_runtime_enabled(&tc_pdev->dev))
1572 pm_runtime_enable(&tc_pdev->dev);
1573
1574 if (enable)
1575 ret = pm_runtime_get_sync(&tc_pdev->dev);
1576 else
1577 ret = pm_runtime_put_sync(&tc_pdev->dev);
1578
1579 if (ret < 0)
1580 pr_err("%s: pm_runtime_%s_sync() failed for %s\n", __func__,
1581 enable ? "get" : "put", dev_name(&tc_pdev->dev));
1582}
1583
Matt Porterc2dde5f2012-08-22 21:09:34 -04001584/* Alloc channel resources */
1585static int edma_alloc_chan_resources(struct dma_chan *chan)
1586{
1587 struct edma_chan *echan = to_edma_chan(chan);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001588 struct edma_cc *ecc = echan->ecc;
1589 struct device *dev = ecc->dev;
1590 enum dma_event_q eventq_no = EVENTQ_DEFAULT;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001591 int ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001592
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001593 if (echan->tc) {
1594 eventq_no = echan->tc->id;
1595 } else if (ecc->tc_list) {
1596 /* memcpy channel */
1597 echan->tc = &ecc->tc_list[ecc->info->default_queue];
1598 eventq_no = echan->tc->id;
1599 }
1600
1601 ret = edma_alloc_channel(echan, eventq_no);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001602 if (ret)
1603 return ret;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001604
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001605 echan->slot[0] = edma_alloc_slot(ecc, echan->ch_num);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001606 if (echan->slot[0] < 0) {
1607 dev_err(dev, "Entry slot allocation failed for channel %u\n",
1608 EDMA_CHAN_SLOT(echan->ch_num));
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001609 goto err_slot;
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001610 }
1611
1612 /* Set up channel -> slot mapping for the entry slot */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001613 edma_set_chmap(echan, echan->slot[0]);
1614 echan->alloced = true;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001615
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001616 dev_dbg(dev, "Got eDMA channel %d for virt channel %d (%s trigger)\n",
1617 EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id,
1618 echan->hw_triggered ? "HW" : "SW");
1619
1620 edma_tc_set_pm_state(echan->tc, true);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001621
1622 return 0;
1623
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001624err_slot:
1625 edma_free_channel(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001626 return ret;
1627}
1628
1629/* Free channel resources */
1630static void edma_free_chan_resources(struct dma_chan *chan)
1631{
1632 struct edma_chan *echan = to_edma_chan(chan);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001633 struct device *dev = echan->ecc->dev;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001634 int i;
1635
1636 /* Terminate transfers */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001637 edma_stop(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001638
1639 vchan_free_chan_resources(&echan->vchan);
1640
1641 /* Free EDMA PaRAM slots */
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001642 for (i = 0; i < EDMA_MAX_SLOTS; i++) {
Matt Porterc2dde5f2012-08-22 21:09:34 -04001643 if (echan->slot[i] >= 0) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001644 edma_free_slot(echan->ecc, echan->slot[i]);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001645 echan->slot[i] = -1;
1646 }
1647 }
1648
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001649 /* Set entry slot to the dummy slot */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001650 edma_set_chmap(echan, echan->ecc->dummy_slot);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03001651
Matt Porterc2dde5f2012-08-22 21:09:34 -04001652 /* Free EDMA channel */
1653 if (echan->alloced) {
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03001654 edma_free_channel(echan);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001655 echan->alloced = false;
1656 }
1657
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001658 edma_tc_set_pm_state(echan->tc, false);
1659 echan->tc = NULL;
1660 echan->hw_triggered = false;
1661
1662 dev_dbg(dev, "Free eDMA channel %d for virt channel %d\n",
1663 EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id);
Matt Porterc2dde5f2012-08-22 21:09:34 -04001664}
1665
1666/* Send pending descriptor to hardware */
1667static void edma_issue_pending(struct dma_chan *chan)
1668{
1669 struct edma_chan *echan = to_edma_chan(chan);
1670 unsigned long flags;
1671
1672 spin_lock_irqsave(&echan->vchan.lock, flags);
1673 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
1674 edma_execute(echan);
1675 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1676}
1677
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001678static u32 edma_residue(struct edma_desc *edesc)
1679{
1680 bool dst = edesc->direction == DMA_DEV_TO_MEM;
1681 struct edma_pset *pset = edesc->pset;
1682 dma_addr_t done, pos;
1683 int i;
1684
1685 /*
1686 * We always read the dst/src position from the first RamPar
1687 * pset. That's the one which is active now.
1688 */
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001689 pos = edma_get_position(edesc->echan->ecc, edesc->echan->slot[0], dst);
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001690
1691 /*
1692 * Cyclic is simple. Just subtract pset[0].addr from pos.
1693 *
1694 * We never update edesc->residue in the cyclic case, so we
1695 * can tell the remaining room to the end of the circular
1696 * buffer.
1697 */
1698 if (edesc->cyclic) {
1699 done = pos - pset->addr;
1700 edesc->residue_stat = edesc->residue - done;
1701 return edesc->residue_stat;
1702 }
1703
1704 /*
1705 * For SG operation we catch up with the last processed
1706 * status.
1707 */
1708 pset += edesc->processed_stat;
1709
1710 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
1711 /*
1712 * If we are inside this pset address range, we know
1713 * this is the active one. Get the current delta and
1714 * stop walking the psets.
1715 */
1716 if (pos >= pset->addr && pos < pset->addr + pset->len)
1717 return edesc->residue_stat - (pos - pset->addr);
1718
1719 /* Otherwise mark it done and update residue_stat. */
1720 edesc->processed_stat++;
1721 edesc->residue_stat -= pset->len;
1722 }
1723 return edesc->residue_stat;
1724}
1725
Matt Porterc2dde5f2012-08-22 21:09:34 -04001726/* Check request completion status */
1727static enum dma_status edma_tx_status(struct dma_chan *chan,
1728 dma_cookie_t cookie,
1729 struct dma_tx_state *txstate)
1730{
1731 struct edma_chan *echan = to_edma_chan(chan);
1732 struct virt_dma_desc *vdesc;
1733 enum dma_status ret;
1734 unsigned long flags;
1735
1736 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul9d386ec2013-10-16 13:42:15 +05301737 if (ret == DMA_COMPLETE || !txstate)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001738 return ret;
1739
1740 spin_lock_irqsave(&echan->vchan.lock, flags);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001741 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
Thomas Gleixner740b41f2014-04-28 14:34:11 -05001742 txstate->residue = edma_residue(echan->edesc);
Thomas Gleixnerde135932014-04-28 14:19:51 -05001743 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
1744 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
Matt Porterc2dde5f2012-08-22 21:09:34 -04001745 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1746
1747 return ret;
1748}
1749
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001750static bool edma_is_memcpy_channel(int ch_num, u16 *memcpy_channels)
1751{
1752 s16 *memcpy_ch = memcpy_channels;
1753
1754 if (!memcpy_channels)
1755 return false;
1756 while (*memcpy_ch != -1) {
1757 if (*memcpy_ch == ch_num)
1758 return true;
1759 memcpy_ch++;
1760 }
1761 return false;
1762}
1763
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001764#define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1765 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
Peter Ujfalusie4a899d2014-07-03 07:51:56 +03001766 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
Peter Ujfalusi2c88ee62014-04-14 14:42:01 +03001767 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1768
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001769static void edma_dma_init(struct edma_cc *ecc, bool legacy_mode)
Matt Porterc2dde5f2012-08-22 21:09:34 -04001770{
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001771 struct dma_device *s_ddev = &ecc->dma_slave;
1772 struct dma_device *m_ddev = NULL;
1773 s16 *memcpy_channels = ecc->info->memcpy_channels;
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03001774 int i, j;
Maxime Ripard9f59cd02014-11-17 14:42:47 +01001775
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001776 dma_cap_zero(s_ddev->cap_mask);
1777 dma_cap_set(DMA_SLAVE, s_ddev->cap_mask);
1778 dma_cap_set(DMA_CYCLIC, s_ddev->cap_mask);
1779 if (ecc->legacy_mode && !memcpy_channels) {
1780 dev_warn(ecc->dev,
1781 "Legacy memcpy is enabled, things might not work\n");
Maxime Ripard9f59cd02014-11-17 14:42:47 +01001782
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001783 dma_cap_set(DMA_MEMCPY, s_ddev->cap_mask);
1784 s_ddev->device_prep_dma_memcpy = edma_prep_dma_memcpy;
1785 s_ddev->directions = BIT(DMA_MEM_TO_MEM);
1786 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001787
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001788 s_ddev->device_prep_slave_sg = edma_prep_slave_sg;
1789 s_ddev->device_prep_dma_cyclic = edma_prep_dma_cyclic;
1790 s_ddev->device_alloc_chan_resources = edma_alloc_chan_resources;
1791 s_ddev->device_free_chan_resources = edma_free_chan_resources;
1792 s_ddev->device_issue_pending = edma_issue_pending;
1793 s_ddev->device_tx_status = edma_tx_status;
1794 s_ddev->device_config = edma_slave_config;
1795 s_ddev->device_pause = edma_dma_pause;
1796 s_ddev->device_resume = edma_dma_resume;
1797 s_ddev->device_terminate_all = edma_terminate_all;
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03001798
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001799 s_ddev->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1800 s_ddev->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
1801 s_ddev->directions |= (BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV));
1802 s_ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03001803
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001804 s_ddev->dev = ecc->dev;
1805 INIT_LIST_HEAD(&s_ddev->channels);
1806
1807 if (memcpy_channels) {
1808 m_ddev = devm_kzalloc(ecc->dev, sizeof(*m_ddev), GFP_KERNEL);
1809 ecc->dma_memcpy = m_ddev;
1810
1811 dma_cap_zero(m_ddev->cap_mask);
1812 dma_cap_set(DMA_MEMCPY, m_ddev->cap_mask);
1813
1814 m_ddev->device_prep_dma_memcpy = edma_prep_dma_memcpy;
1815 m_ddev->device_alloc_chan_resources = edma_alloc_chan_resources;
1816 m_ddev->device_free_chan_resources = edma_free_chan_resources;
1817 m_ddev->device_issue_pending = edma_issue_pending;
1818 m_ddev->device_tx_status = edma_tx_status;
1819 m_ddev->device_config = edma_slave_config;
1820 m_ddev->device_pause = edma_dma_pause;
1821 m_ddev->device_resume = edma_dma_resume;
1822 m_ddev->device_terminate_all = edma_terminate_all;
1823
1824 m_ddev->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1825 m_ddev->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
1826 m_ddev->directions = BIT(DMA_MEM_TO_MEM);
1827 m_ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1828
1829 m_ddev->dev = ecc->dev;
1830 INIT_LIST_HEAD(&m_ddev->channels);
1831 } else if (!ecc->legacy_mode) {
1832 dev_info(ecc->dev, "memcpy is disabled\n");
1833 }
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03001834
1835 for (i = 0; i < ecc->num_channels; i++) {
1836 struct edma_chan *echan = &ecc->slave_chans[i];
1837 echan->ch_num = EDMA_CTLR_CHAN(ecc->id, i);
1838 echan->ecc = ecc;
1839 echan->vchan.desc_free = edma_desc_free;
1840
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001841 if (m_ddev && edma_is_memcpy_channel(i, memcpy_channels))
1842 vchan_init(&echan->vchan, m_ddev);
1843 else
1844 vchan_init(&echan->vchan, s_ddev);
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03001845
1846 INIT_LIST_HEAD(&echan->node);
1847 for (j = 0; j < EDMA_MAX_SLOTS; j++)
1848 echan->slot[j] = -1;
1849 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04001850}
1851
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001852static int edma_setup_from_hw(struct device *dev, struct edma_soc_info *pdata,
1853 struct edma_cc *ecc)
1854{
1855 int i;
1856 u32 value, cccfg;
1857 s8 (*queue_priority_map)[2];
1858
1859 /* Decode the eDMA3 configuration from CCCFG register */
1860 cccfg = edma_read(ecc, EDMA_CCCFG);
1861
1862 value = GET_NUM_REGN(cccfg);
1863 ecc->num_region = BIT(value);
1864
1865 value = GET_NUM_DMACH(cccfg);
1866 ecc->num_channels = BIT(value + 1);
1867
Peter Ujfalusi633e42b2015-10-16 10:18:04 +03001868 value = GET_NUM_QDMACH(cccfg);
1869 ecc->num_qchannels = value * 2;
1870
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001871 value = GET_NUM_PAENTRY(cccfg);
1872 ecc->num_slots = BIT(value + 4);
1873
1874 value = GET_NUM_EVQUE(cccfg);
1875 ecc->num_tc = value + 1;
1876
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +03001877 ecc->chmap_exist = (cccfg & CHMAP_EXIST) ? true : false;
1878
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001879 dev_dbg(dev, "eDMA3 CC HW configuration (cccfg: 0x%08x):\n", cccfg);
1880 dev_dbg(dev, "num_region: %u\n", ecc->num_region);
1881 dev_dbg(dev, "num_channels: %u\n", ecc->num_channels);
Peter Ujfalusi633e42b2015-10-16 10:18:04 +03001882 dev_dbg(dev, "num_qchannels: %u\n", ecc->num_qchannels);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001883 dev_dbg(dev, "num_slots: %u\n", ecc->num_slots);
1884 dev_dbg(dev, "num_tc: %u\n", ecc->num_tc);
Peter Ujfalusi4ab54f62015-10-14 14:43:04 +03001885 dev_dbg(dev, "chmap_exist: %s\n", ecc->chmap_exist ? "yes" : "no");
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001886
1887 /* Nothing need to be done if queue priority is provided */
1888 if (pdata->queue_priority_mapping)
1889 return 0;
1890
1891 /*
1892 * Configure TC/queue priority as follows:
1893 * Q0 - priority 0
1894 * Q1 - priority 1
1895 * Q2 - priority 2
1896 * ...
1897 * The meaning of priority numbers: 0 highest priority, 7 lowest
1898 * priority. So Q0 is the highest priority queue and the last queue has
1899 * the lowest priority.
1900 */
Peter Ujfalusi547c6e22015-10-14 14:42:55 +03001901 queue_priority_map = devm_kcalloc(dev, ecc->num_tc + 1, sizeof(s8),
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001902 GFP_KERNEL);
1903 if (!queue_priority_map)
1904 return -ENOMEM;
1905
1906 for (i = 0; i < ecc->num_tc; i++) {
1907 queue_priority_map[i][0] = i;
1908 queue_priority_map[i][1] = i;
1909 }
1910 queue_priority_map[i][0] = -1;
1911 queue_priority_map[i][1] = -1;
1912
1913 pdata->queue_priority_mapping = queue_priority_map;
1914 /* Default queue has the lowest priority */
1915 pdata->default_queue = i - 1;
1916
1917 return 0;
1918}
1919
1920#if IS_ENABLED(CONFIG_OF)
1921static int edma_xbar_event_map(struct device *dev, struct edma_soc_info *pdata,
1922 size_t sz)
1923{
1924 const char pname[] = "ti,edma-xbar-event-map";
1925 struct resource res;
1926 void __iomem *xbar;
1927 s16 (*xbar_chans)[2];
1928 size_t nelm = sz / sizeof(s16);
1929 u32 shift, offset, mux;
1930 int ret, i;
1931
Peter Ujfalusi547c6e22015-10-14 14:42:55 +03001932 xbar_chans = devm_kcalloc(dev, nelm + 2, sizeof(s16), GFP_KERNEL);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001933 if (!xbar_chans)
1934 return -ENOMEM;
1935
1936 ret = of_address_to_resource(dev->of_node, 1, &res);
1937 if (ret)
1938 return -ENOMEM;
1939
1940 xbar = devm_ioremap(dev, res.start, resource_size(&res));
1941 if (!xbar)
1942 return -ENOMEM;
1943
1944 ret = of_property_read_u16_array(dev->of_node, pname, (u16 *)xbar_chans,
1945 nelm);
1946 if (ret)
1947 return -EIO;
1948
1949 /* Invalidate last entry for the other user of this mess */
1950 nelm >>= 1;
1951 xbar_chans[nelm][0] = -1;
1952 xbar_chans[nelm][1] = -1;
1953
1954 for (i = 0; i < nelm; i++) {
1955 shift = (xbar_chans[i][1] & 0x03) << 3;
1956 offset = xbar_chans[i][1] & 0xfffffffc;
1957 mux = readl(xbar + offset);
1958 mux &= ~(0xff << shift);
1959 mux |= xbar_chans[i][0] << shift;
1960 writel(mux, (xbar + offset));
1961 }
1962
1963 pdata->xbar_chans = (const s16 (*)[2]) xbar_chans;
1964 return 0;
1965}
1966
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001967static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
1968 bool legacy_mode)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001969{
1970 struct edma_soc_info *info;
Peter Ujfalusi966a87b2015-10-16 10:18:07 +03001971 struct property *prop;
1972 size_t sz;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03001973 int ret;
1974
1975 info = devm_kzalloc(dev, sizeof(struct edma_soc_info), GFP_KERNEL);
1976 if (!info)
1977 return ERR_PTR(-ENOMEM);
1978
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001979 if (legacy_mode) {
1980 prop = of_find_property(dev->of_node, "ti,edma-xbar-event-map",
1981 &sz);
1982 if (prop) {
1983 ret = edma_xbar_event_map(dev, info, sz);
1984 if (ret)
1985 return ERR_PTR(ret);
1986 }
1987 return info;
1988 }
1989
1990 /* Get the list of channels allocated to be used for memcpy */
1991 prop = of_find_property(dev->of_node, "ti,edma-memcpy-channels", &sz);
Peter Ujfalusi966a87b2015-10-16 10:18:07 +03001992 if (prop) {
Peter Ujfalusi1be53362015-10-16 10:18:10 +03001993 const char pname[] = "ti,edma-memcpy-channels";
1994 size_t nelm = sz / sizeof(s16);
1995 s16 *memcpy_ch;
1996
1997 memcpy_ch = devm_kcalloc(dev, nelm + 1, sizeof(s16),
1998 GFP_KERNEL);
1999 if (!memcpy_ch)
2000 return ERR_PTR(-ENOMEM);
2001
2002 ret = of_property_read_u16_array(dev->of_node, pname,
2003 (u16 *)memcpy_ch, nelm);
Peter Ujfalusi966a87b2015-10-16 10:18:07 +03002004 if (ret)
2005 return ERR_PTR(ret);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002006
2007 memcpy_ch[nelm] = -1;
2008 info->memcpy_channels = memcpy_ch;
2009 }
2010
2011 prop = of_find_property(dev->of_node, "ti,edma-reserved-slot-ranges",
2012 &sz);
2013 if (prop) {
2014 const char pname[] = "ti,edma-reserved-slot-ranges";
2015 s16 (*rsv_slots)[2];
2016 size_t nelm = sz / sizeof(*rsv_slots);
2017 struct edma_rsv_info *rsv_info;
2018
2019 if (!nelm)
2020 return info;
2021
2022 rsv_info = devm_kzalloc(dev, sizeof(*rsv_info), GFP_KERNEL);
2023 if (!rsv_info)
2024 return ERR_PTR(-ENOMEM);
2025
2026 rsv_slots = devm_kcalloc(dev, nelm + 1, sizeof(*rsv_slots),
2027 GFP_KERNEL);
2028 if (!rsv_slots)
2029 return ERR_PTR(-ENOMEM);
2030
2031 ret = of_property_read_u16_array(dev->of_node, pname,
2032 (u16 *)rsv_slots, nelm * 2);
2033 if (ret)
2034 return ERR_PTR(ret);
2035
2036 rsv_slots[nelm][0] = -1;
2037 rsv_slots[nelm][1] = -1;
2038 info->rsv = rsv_info;
2039 info->rsv->rsv_slots = (const s16 (*)[2])rsv_slots;
Peter Ujfalusi966a87b2015-10-16 10:18:07 +03002040 }
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002041
2042 return info;
2043}
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002044
2045static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
2046 struct of_dma *ofdma)
2047{
2048 struct edma_cc *ecc = ofdma->of_dma_data;
2049 struct dma_chan *chan = NULL;
2050 struct edma_chan *echan;
2051 int i;
2052
2053 if (!ecc || dma_spec->args_count < 1)
2054 return NULL;
2055
2056 for (i = 0; i < ecc->num_channels; i++) {
2057 echan = &ecc->slave_chans[i];
2058 if (echan->ch_num == dma_spec->args[0]) {
2059 chan = &echan->vchan.chan;
2060 break;
2061 }
2062 }
2063
2064 if (!chan)
2065 return NULL;
2066
2067 if (echan->ecc->legacy_mode && dma_spec->args_count == 1)
2068 goto out;
2069
2070 if (!echan->ecc->legacy_mode && dma_spec->args_count == 2 &&
2071 dma_spec->args[1] < echan->ecc->num_tc) {
2072 echan->tc = &echan->ecc->tc_list[dma_spec->args[1]];
2073 goto out;
2074 }
2075
2076 return NULL;
2077out:
2078 /* The channel is going to be used as HW synchronized */
2079 echan->hw_triggered = true;
2080 return dma_get_slave_channel(chan);
2081}
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002082#else
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002083static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
2084 bool legacy_mode)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002085{
2086 return ERR_PTR(-EINVAL);
2087}
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002088
2089static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
2090 struct of_dma *ofdma)
2091{
2092 return NULL;
2093}
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002094#endif
2095
Bill Pemberton463a1f82012-11-19 13:22:55 -05002096static int edma_probe(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002097{
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002098 struct edma_soc_info *info = pdev->dev.platform_data;
2099 s8 (*queue_priority_mapping)[2];
2100 int i, off, ln;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002101 const s16 (*rsv_slots)[2];
2102 const s16 (*xbar_chans)[2];
2103 int irq;
2104 char *irq_name;
2105 struct resource *mem;
2106 struct device_node *node = pdev->dev.of_node;
2107 struct device *dev = &pdev->dev;
2108 struct edma_cc *ecc;
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002109 bool legacy_mode = true;
Matt Porterc2dde5f2012-08-22 21:09:34 -04002110 int ret;
2111
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002112 if (node) {
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002113 const struct of_device_id *match;
2114
2115 match = of_match_node(edma_of_ids, node);
2116 if (match && (u32)match->data == EDMA_BINDING_TPCC)
2117 legacy_mode = false;
2118
2119 info = edma_setup_info_from_dt(dev, legacy_mode);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002120 if (IS_ERR(info)) {
2121 dev_err(dev, "failed to get DT data\n");
2122 return PTR_ERR(info);
2123 }
2124 }
2125
2126 if (!info)
2127 return -ENODEV;
2128
2129 pm_runtime_enable(dev);
2130 ret = pm_runtime_get_sync(dev);
2131 if (ret < 0) {
2132 dev_err(dev, "pm_runtime_get_sync() failed\n");
2133 return ret;
2134 }
2135
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002136 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
Russell King94cb0e72013-06-27 13:45:16 +01002137 if (ret)
2138 return ret;
2139
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002140 ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002141 if (!ecc) {
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002142 dev_err(dev, "Can't allocate controller\n");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002143 return -ENOMEM;
2144 }
2145
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002146 ecc->dev = dev;
2147 ecc->id = pdev->id;
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002148 ecc->legacy_mode = legacy_mode;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002149 /* When booting with DT the pdev->id is -1 */
2150 if (ecc->id < 0)
2151 ecc->id = 0;
Peter Ujfalusica304fa2015-10-14 14:42:49 +03002152
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002153 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma3_cc");
2154 if (!mem) {
2155 dev_dbg(dev, "mem resource not found, using index 0\n");
2156 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2157 if (!mem) {
2158 dev_err(dev, "no mem resource?\n");
2159 return -ENODEV;
2160 }
2161 }
2162 ecc->base = devm_ioremap_resource(dev, mem);
2163 if (IS_ERR(ecc->base))
2164 return PTR_ERR(ecc->base);
Peter Ujfalusib2c843a2015-10-14 14:42:50 +03002165
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002166 platform_set_drvdata(pdev, ecc);
2167
2168 /* Get eDMA3 configuration from IP */
2169 ret = edma_setup_from_hw(dev, info, ecc);
2170 if (ret)
2171 return ret;
2172
Peter Ujfalusicb782052015-10-14 14:42:54 +03002173 /* Allocate memory based on the information we got from the IP */
2174 ecc->slave_chans = devm_kcalloc(dev, ecc->num_channels,
2175 sizeof(*ecc->slave_chans), GFP_KERNEL);
2176 if (!ecc->slave_chans)
2177 return -ENOMEM;
2178
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002179 ecc->slot_inuse = devm_kcalloc(dev, BITS_TO_LONGS(ecc->num_slots),
Peter Ujfalusicb782052015-10-14 14:42:54 +03002180 sizeof(unsigned long), GFP_KERNEL);
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002181 if (!ecc->slot_inuse)
Peter Ujfalusicb782052015-10-14 14:42:54 +03002182 return -ENOMEM;
2183
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002184 ecc->default_queue = info->default_queue;
2185
2186 for (i = 0; i < ecc->num_slots; i++)
2187 edma_write_slot(ecc, i, &dummy_paramset);
2188
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002189 if (info->rsv) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002190 /* Set the reserved slots in inuse list */
2191 rsv_slots = info->rsv->rsv_slots;
2192 if (rsv_slots) {
2193 for (i = 0; rsv_slots[i][0] != -1; i++) {
2194 off = rsv_slots[i][0];
2195 ln = rsv_slots[i][1];
Peter Ujfalusi7a73b132015-10-14 14:43:05 +03002196 set_bits(off, ln, ecc->slot_inuse);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002197 }
2198 }
2199 }
2200
2201 /* Clear the xbar mapped channels in unused list */
2202 xbar_chans = info->xbar_chans;
2203 if (xbar_chans) {
2204 for (i = 0; xbar_chans[i][1] != -1; i++) {
2205 off = xbar_chans[i][1];
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002206 }
2207 }
2208
2209 irq = platform_get_irq_byname(pdev, "edma3_ccint");
2210 if (irq < 0 && node)
2211 irq = irq_of_parse_and_map(node, 0);
2212
2213 if (irq >= 0) {
2214 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccint",
2215 dev_name(dev));
2216 ret = devm_request_irq(dev, irq, dma_irq_handler, 0, irq_name,
2217 ecc);
2218 if (ret) {
2219 dev_err(dev, "CCINT (%d) failed --> %d\n", irq, ret);
2220 return ret;
2221 }
2222 }
2223
2224 irq = platform_get_irq_byname(pdev, "edma3_ccerrint");
2225 if (irq < 0 && node)
2226 irq = irq_of_parse_and_map(node, 2);
2227
2228 if (irq >= 0) {
2229 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccerrint",
2230 dev_name(dev));
2231 ret = devm_request_irq(dev, irq, dma_ccerr_handler, 0, irq_name,
2232 ecc);
2233 if (ret) {
2234 dev_err(dev, "CCERRINT (%d) failed --> %d\n", irq, ret);
2235 return ret;
2236 }
2237 }
2238
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002239 ecc->dummy_slot = edma_alloc_slot(ecc, EDMA_SLOT_ANY);
2240 if (ecc->dummy_slot < 0) {
2241 dev_err(dev, "Can't allocate PaRAM dummy slot\n");
2242 return ecc->dummy_slot;
2243 }
2244
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002245 queue_priority_mapping = info->queue_priority_mapping;
2246
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002247 if (!ecc->legacy_mode) {
2248 int lowest_priority = 0;
2249 struct of_phandle_args tc_args;
2250
2251 ecc->tc_list = devm_kcalloc(dev, ecc->num_tc,
2252 sizeof(*ecc->tc_list), GFP_KERNEL);
2253 if (!ecc->tc_list)
2254 return -ENOMEM;
2255
2256 for (i = 0;; i++) {
2257 ret = of_parse_phandle_with_fixed_args(node, "ti,tptcs",
2258 1, i, &tc_args);
2259 if (ret || i == ecc->num_tc)
2260 break;
2261
2262 ecc->tc_list[i].node = tc_args.np;
2263 ecc->tc_list[i].id = i;
2264 queue_priority_mapping[i][1] = tc_args.args[0];
2265 if (queue_priority_mapping[i][1] > lowest_priority) {
2266 lowest_priority = queue_priority_mapping[i][1];
2267 info->default_queue = i;
2268 }
2269 }
2270 }
2271
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002272 /* Event queue priority mapping */
2273 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2274 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2275 queue_priority_mapping[i][1]);
2276
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002277 for (i = 0; i < ecc->num_region; i++) {
2278 edma_write_array2(ecc, EDMA_DRAE, i, 0, 0x0);
2279 edma_write_array2(ecc, EDMA_DRAE, i, 1, 0x0);
2280 edma_write_array(ecc, EDMA_QRAE, i, 0x0);
2281 }
2282 ecc->info = info;
2283
Peter Ujfalusi02f77ef2015-10-16 10:18:05 +03002284 /* Init the dma device and channels */
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002285 edma_dma_init(ecc, legacy_mode);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002286
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002287 for (i = 0; i < ecc->num_channels; i++) {
2288 /* Assign all channels to the default queue */
Peter Ujfalusif9425de2015-10-16 10:18:03 +03002289 edma_assign_channel_eventq(&ecc->slave_chans[i],
2290 info->default_queue);
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002291 /* Set entry slot to the dummy slot */
2292 edma_set_chmap(&ecc->slave_chans[i], ecc->dummy_slot);
2293 }
2294
Matt Porterc2dde5f2012-08-22 21:09:34 -04002295 ret = dma_async_device_register(&ecc->dma_slave);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002296 if (ret) {
2297 dev_err(dev, "slave ddev registration failed (%d)\n", ret);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002298 goto err_reg1;
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002299 }
2300
2301 if (ecc->dma_memcpy) {
2302 ret = dma_async_device_register(ecc->dma_memcpy);
2303 if (ret) {
2304 dev_err(dev, "memcpy ddev registration failed (%d)\n",
2305 ret);
2306 dma_async_device_unregister(&ecc->dma_slave);
2307 goto err_reg1;
2308 }
2309 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04002310
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002311 if (node)
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002312 of_dma_controller_register(node, of_edma_xlate, ecc);
Peter Ujfalusidc9b60552015-10-14 14:42:47 +03002313
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002314 dev_info(dev, "TI EDMA DMA engine driver\n");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002315
2316 return 0;
2317
2318err_reg1:
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002319 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002320 return ret;
2321}
2322
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08002323static int edma_remove(struct platform_device *pdev)
Matt Porterc2dde5f2012-08-22 21:09:34 -04002324{
2325 struct device *dev = &pdev->dev;
2326 struct edma_cc *ecc = dev_get_drvdata(dev);
2327
Peter Ujfalusi907f74a2015-10-14 14:42:56 +03002328 if (dev->of_node)
2329 of_dma_controller_free(dev->of_node);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002330 dma_async_device_unregister(&ecc->dma_slave);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002331 if (ecc->dma_memcpy)
2332 dma_async_device_unregister(ecc->dma_memcpy);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002333 edma_free_slot(ecc, ecc->dummy_slot);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002334
2335 return 0;
2336}
2337
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002338#ifdef CONFIG_PM_SLEEP
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002339static int edma_pm_suspend(struct device *dev)
2340{
2341 struct edma_cc *ecc = dev_get_drvdata(dev);
2342 struct edma_chan *echan = ecc->slave_chans;
2343 int i;
2344
2345 for (i = 0; i < ecc->num_channels; i++) {
2346 if (echan[i].alloced) {
2347 edma_setup_interrupt(&echan[i], false);
2348 edma_tc_set_pm_state(echan[i].tc, false);
2349 }
2350 }
2351
2352 return 0;
2353}
2354
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002355static int edma_pm_resume(struct device *dev)
2356{
2357 struct edma_cc *ecc = dev_get_drvdata(dev);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002358 struct edma_chan *echan = ecc->slave_chans;
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002359 int i;
2360 s8 (*queue_priority_mapping)[2];
2361
2362 queue_priority_mapping = ecc->info->queue_priority_mapping;
2363
2364 /* Event queue priority mapping */
2365 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2366 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2367 queue_priority_mapping[i][1]);
2368
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002369 for (i = 0; i < ecc->num_channels; i++) {
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002370 if (echan[i].alloced) {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002371 /* ensure access through shadow region 0 */
2372 edma_or_array2(ecc, EDMA_DRAE, 0, i >> 5,
2373 BIT(i & 0x1f));
2374
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002375 edma_setup_interrupt(&echan[i], true);
Peter Ujfalusie4e886c2015-10-14 14:43:06 +03002376
2377 /* Set up channel -> slot mapping for the entry slot */
Peter Ujfalusi34cf3012015-10-16 10:18:01 +03002378 edma_set_chmap(&echan[i], echan[i].slot[0]);
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002379
2380 edma_tc_set_pm_state(echan[i].tc, true);
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002381 }
2382 }
2383
2384 return 0;
2385}
2386#endif
2387
2388static const struct dev_pm_ops edma_pm_ops = {
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002389 SET_LATE_SYSTEM_SLEEP_PM_OPS(edma_pm_suspend, edma_pm_resume)
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002390};
2391
Matt Porterc2dde5f2012-08-22 21:09:34 -04002392static struct platform_driver edma_driver = {
2393 .probe = edma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05002394 .remove = edma_remove,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002395 .driver = {
Peter Ujfalusi2b6b3b72015-10-14 14:42:53 +03002396 .name = "edma",
2397 .pm = &edma_pm_ops,
2398 .of_match_table = edma_of_ids,
Matt Porterc2dde5f2012-08-22 21:09:34 -04002399 },
2400};
2401
2402bool edma_filter_fn(struct dma_chan *chan, void *param)
2403{
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002404 bool match = false;
2405
Matt Porterc2dde5f2012-08-22 21:09:34 -04002406 if (chan->device->dev->driver == &edma_driver.driver) {
2407 struct edma_chan *echan = to_edma_chan(chan);
2408 unsigned ch_req = *(unsigned *)param;
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002409 if (ch_req == echan->ch_num) {
2410 /* The channel is going to be used as HW synchronized */
2411 echan->hw_triggered = true;
2412 match = true;
2413 }
Matt Porterc2dde5f2012-08-22 21:09:34 -04002414 }
Peter Ujfalusi1be53362015-10-16 10:18:10 +03002415 return match;
Matt Porterc2dde5f2012-08-22 21:09:34 -04002416}
2417EXPORT_SYMBOL(edma_filter_fn);
2418
Matt Porterc2dde5f2012-08-22 21:09:34 -04002419static int edma_init(void)
2420{
Arnd Bergmann5305e4d2014-10-24 18:14:01 +02002421 return platform_driver_register(&edma_driver);
Matt Porterc2dde5f2012-08-22 21:09:34 -04002422}
2423subsys_initcall(edma_init);
2424
2425static void __exit edma_exit(void)
2426{
Matt Porterc2dde5f2012-08-22 21:09:34 -04002427 platform_driver_unregister(&edma_driver);
2428}
2429module_exit(edma_exit);
2430
Josh Boyerd71505b2013-09-04 10:32:50 -04002431MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
Matt Porterc2dde5f2012-08-22 21:09:34 -04002432MODULE_DESCRIPTION("TI EDMA DMA engine driver");
2433MODULE_LICENSE("GPL v2");