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