1
2
3
4
5
6
7
8
9
10
11#include <asm/io.h>
12#include <common.h>
13#include <dm.h>
14#include <dma-uclass.h>
15#include <asm/omap_common.h>
16#include <asm/ti-common/ti-edma3.h>
17
18#define EDMA3_SL_BASE(slot) (0x4000 + ((slot) << 5))
19#define EDMA3_SL_MAX_NUM 512
20#define EDMA3_SLOPT_FIFO_WIDTH_MASK (0x7 << 8)
21
22#define EDMA3_QCHMAP(ch) 0x0200 + ((ch) << 2)
23#define EDMA3_CHMAP_PARSET_MASK 0x1ff
24#define EDMA3_CHMAP_PARSET_SHIFT 0x5
25#define EDMA3_CHMAP_TRIGWORD_SHIFT 0x2
26
27#define EDMA3_QEMCR 0x314
28#define EDMA3_IPR 0x1068
29#define EDMA3_IPRH 0x106c
30#define EDMA3_ICR 0x1070
31#define EDMA3_ICRH 0x1074
32#define EDMA3_QEECR 0x1088
33#define EDMA3_QEESR 0x108c
34#define EDMA3_QSECR 0x1094
35
36#define EDMA_FILL_BUFFER_SIZE 512
37
38struct ti_edma3_priv {
39 u32 base;
40};
41
42static u8 edma_fill_buffer[EDMA_FILL_BUFFER_SIZE] __aligned(ARCH_DMA_MINALIGN);
43
44
45
46
47
48
49
50
51
52
53
54void qedma3_start(u32 base, struct edma3_channel_config *cfg)
55{
56 u32 qchmap;
57
58
59 if (cfg->complete_code < 32)
60 __raw_writel(1 << cfg->complete_code, base + EDMA3_ICR);
61 else
62 __raw_writel(1 << cfg->complete_code, base + EDMA3_ICRH);
63
64
65 qchmap = ((EDMA3_CHMAP_PARSET_MASK & cfg->slot)
66 << EDMA3_CHMAP_PARSET_SHIFT) |
67 (cfg->trigger_slot_word << EDMA3_CHMAP_TRIGWORD_SHIFT);
68
69 __raw_writel(qchmap, base + EDMA3_QCHMAP(cfg->chnum));
70
71
72 __raw_writel(1 << cfg->chnum, base + EDMA3_QSECR);
73 __raw_writel(1 << cfg->chnum, base + EDMA3_QEMCR);
74
75
76 __raw_writel(1 << cfg->chnum, base + EDMA3_QEESR);
77}
78
79
80
81
82
83
84
85
86
87
88
89
90
91void edma3_set_dest(u32 base, int slot, u32 dst, enum edma3_address_mode mode,
92 enum edma3_fifo_width width)
93{
94 u32 opt;
95 struct edma3_slot_layout *rg;
96
97 rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
98
99 opt = __raw_readl(&rg->opt);
100 if (mode == FIFO)
101 opt = (opt & EDMA3_SLOPT_FIFO_WIDTH_MASK) |
102 (EDMA3_SLOPT_DST_ADDR_CONST_MODE |
103 EDMA3_SLOPT_FIFO_WIDTH_SET(width));
104 else
105 opt &= ~EDMA3_SLOPT_DST_ADDR_CONST_MODE;
106
107 __raw_writel(opt, &rg->opt);
108 __raw_writel(dst, &rg->dst);
109}
110
111
112
113
114
115
116
117
118
119
120
121
122void edma3_set_dest_index(u32 base, unsigned slot, int bidx, int cidx)
123{
124 u32 src_dst_bidx;
125 u32 src_dst_cidx;
126 struct edma3_slot_layout *rg;
127
128 rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
129
130 src_dst_bidx = __raw_readl(&rg->src_dst_bidx);
131 src_dst_cidx = __raw_readl(&rg->src_dst_cidx);
132
133 __raw_writel((src_dst_bidx & 0x0000ffff) | (bidx << 16),
134 &rg->src_dst_bidx);
135 __raw_writel((src_dst_cidx & 0x0000ffff) | (cidx << 16),
136 &rg->src_dst_cidx);
137}
138
139
140
141
142void edma3_set_dest_addr(u32 base, int slot, u32 dst)
143{
144 struct edma3_slot_layout *rg;
145
146 rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
147 __raw_writel(dst, &rg->dst);
148}
149
150
151
152
153
154
155
156
157
158
159
160
161
162void edma3_set_src(u32 base, int slot, u32 src, enum edma3_address_mode mode,
163 enum edma3_fifo_width width)
164{
165 u32 opt;
166 struct edma3_slot_layout *rg;
167
168 rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
169
170 opt = __raw_readl(&rg->opt);
171 if (mode == FIFO)
172 opt = (opt & EDMA3_SLOPT_FIFO_WIDTH_MASK) |
173 (EDMA3_SLOPT_DST_ADDR_CONST_MODE |
174 EDMA3_SLOPT_FIFO_WIDTH_SET(width));
175 else
176 opt &= ~EDMA3_SLOPT_DST_ADDR_CONST_MODE;
177
178 __raw_writel(opt, &rg->opt);
179 __raw_writel(src, &rg->src);
180}
181
182
183
184
185
186
187
188
189
190
191
192
193void edma3_set_src_index(u32 base, unsigned slot, int bidx, int cidx)
194{
195 u32 src_dst_bidx;
196 u32 src_dst_cidx;
197 struct edma3_slot_layout *rg;
198
199 rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
200
201 src_dst_bidx = __raw_readl(&rg->src_dst_bidx);
202 src_dst_cidx = __raw_readl(&rg->src_dst_cidx);
203
204 __raw_writel((src_dst_bidx & 0xffff0000) | bidx,
205 &rg->src_dst_bidx);
206 __raw_writel((src_dst_cidx & 0xffff0000) | cidx,
207 &rg->src_dst_cidx);
208}
209
210
211
212
213void edma3_set_src_addr(u32 base, int slot, u32 src)
214{
215 struct edma3_slot_layout *rg;
216
217 rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
218 __raw_writel(src, &rg->src);
219}
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251void edma3_set_transfer_params(u32 base, int slot, int acnt,
252 int bcnt, int ccnt, u16 bcnt_rld,
253 enum edma3_sync_dimension sync_mode)
254{
255 u32 opt;
256 u32 link_bcntrld;
257 struct edma3_slot_layout *rg;
258
259 rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
260
261 link_bcntrld = __raw_readl(&rg->link_bcntrld);
262
263 __raw_writel((bcnt_rld << 16) | (0x0000ffff & link_bcntrld),
264 &rg->link_bcntrld);
265
266 opt = __raw_readl(&rg->opt);
267 if (sync_mode == ASYNC)
268 __raw_writel(opt & ~EDMA3_SLOPT_AB_SYNC, &rg->opt);
269 else
270 __raw_writel(opt | EDMA3_SLOPT_AB_SYNC, &rg->opt);
271
272
273 __raw_writel((bcnt << 16) | (acnt & 0xffff), &rg->a_b_cnt);
274 __raw_writel(0xffff & ccnt, &rg->ccnt);
275}
276
277
278
279
280
281
282
283
284
285
286
287
288void edma3_write_slot(u32 base, int slot, struct edma3_slot_layout *param)
289{
290 int i;
291 u32 *p = (u32 *)param;
292 u32 *addr = (u32 *)(base + EDMA3_SL_BASE(slot));
293
294 for (i = 0; i < sizeof(struct edma3_slot_layout)/4; i += 4)
295 __raw_writel(*p++, addr++);
296}
297
298
299
300
301
302
303
304
305
306
307void edma3_read_slot(u32 base, int slot, struct edma3_slot_layout *param)
308{
309 int i;
310 u32 *p = (u32 *)param;
311 u32 *addr = (u32 *)(base + EDMA3_SL_BASE(slot));
312
313 for (i = 0; i < sizeof(struct edma3_slot_layout)/4; i += 4)
314 *p++ = __raw_readl(addr++);
315}
316
317void edma3_slot_configure(u32 base, int slot, struct edma3_slot_config *cfg)
318{
319 struct edma3_slot_layout *rg;
320
321 rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
322
323 __raw_writel(cfg->opt, &rg->opt);
324 __raw_writel(cfg->src, &rg->src);
325 __raw_writel((cfg->bcnt << 16) | (cfg->acnt & 0xffff), &rg->a_b_cnt);
326 __raw_writel(cfg->dst, &rg->dst);
327 __raw_writel((cfg->dst_bidx << 16) |
328 (cfg->src_bidx & 0xffff), &rg->src_dst_bidx);
329 __raw_writel((cfg->bcntrld << 16) |
330 (cfg->link & 0xffff), &rg->link_bcntrld);
331 __raw_writel((cfg->dst_cidx << 16) |
332 (cfg->src_cidx & 0xffff), &rg->src_dst_cidx);
333 __raw_writel(0xffff & cfg->ccnt, &rg->ccnt);
334}
335
336
337
338
339
340
341
342
343
344
345int edma3_check_for_transfer(u32 base, struct edma3_channel_config *cfg)
346{
347 u32 inum;
348 u32 ipr_base;
349 u32 icr_base;
350
351 if (cfg->complete_code < 32) {
352 ipr_base = base + EDMA3_IPR;
353 icr_base = base + EDMA3_ICR;
354 inum = 1 << cfg->complete_code;
355 } else {
356 ipr_base = base + EDMA3_IPRH;
357 icr_base = base + EDMA3_ICRH;
358 inum = 1 << (cfg->complete_code - 32);
359 }
360
361
362 if (!(__raw_readl(ipr_base) & inum))
363 return 1;
364
365
366 __raw_writel(inum, icr_base);
367
368 return 0;
369}
370
371
372
373
374
375
376
377void qedma3_stop(u32 base, struct edma3_channel_config *cfg)
378{
379
380 __raw_writel(1 << cfg->chnum, base + EDMA3_QEECR);
381
382
383 if (cfg->complete_code < 32)
384 __raw_writel(1 << cfg->complete_code, base + EDMA3_ICR);
385 else
386 __raw_writel(1 << cfg->complete_code, base + EDMA3_ICRH);
387
388
389 __raw_writel(1 << cfg->chnum, base + EDMA3_QSECR);
390 __raw_writel(1 << cfg->chnum, base + EDMA3_QEMCR);
391
392
393 __raw_writel(0, base + EDMA3_QCHMAP(cfg->chnum));
394}
395
396void __edma3_transfer(unsigned long edma3_base_addr, unsigned int edma_slot_num,
397 void *dst, void *src, size_t len, size_t s_len)
398{
399 struct edma3_slot_config slot;
400 struct edma3_channel_config edma_channel;
401 int b_cnt_value = 1;
402 int rem_bytes = 0;
403 int a_cnt_value = len;
404 unsigned int addr = (unsigned int) (dst);
405 unsigned int max_acnt = 0x7FFFU;
406
407 if (len > s_len) {
408 b_cnt_value = (len / s_len);
409 rem_bytes = (len % s_len);
410 a_cnt_value = s_len;
411 } else if (len > max_acnt) {
412 b_cnt_value = (len / max_acnt);
413 rem_bytes = (len % max_acnt);
414 a_cnt_value = max_acnt;
415 }
416
417 slot.opt = 0;
418 slot.src = ((unsigned int) src);
419 slot.acnt = a_cnt_value;
420 slot.bcnt = b_cnt_value;
421 slot.ccnt = 1;
422 if (len == s_len)
423 slot.src_bidx = a_cnt_value;
424 else
425 slot.src_bidx = 0;
426 slot.dst_bidx = a_cnt_value;
427 slot.src_cidx = 0;
428 slot.dst_cidx = 0;
429 slot.link = EDMA3_PARSET_NULL_LINK;
430 slot.bcntrld = 0;
431 slot.opt = EDMA3_SLOPT_TRANS_COMP_INT_ENB |
432 EDMA3_SLOPT_COMP_CODE(0) |
433 EDMA3_SLOPT_STATIC | EDMA3_SLOPT_AB_SYNC;
434
435 edma3_slot_configure(edma3_base_addr, edma_slot_num, &slot);
436 edma_channel.slot = edma_slot_num;
437 edma_channel.chnum = 0;
438 edma_channel.complete_code = 0;
439
440 edma_channel.trigger_slot_word = EDMA3_TWORD(dst);
441
442 qedma3_start(edma3_base_addr, &edma_channel);
443 edma3_set_dest_addr(edma3_base_addr, edma_channel.slot, addr);
444
445 while (edma3_check_for_transfer(edma3_base_addr, &edma_channel))
446 ;
447 qedma3_stop(edma3_base_addr, &edma_channel);
448
449 if (rem_bytes != 0) {
450 slot.opt = 0;
451 if (len == s_len)
452 slot.src =
453 (b_cnt_value * max_acnt) + ((unsigned int) src);
454 else
455 slot.src = (unsigned int) src;
456 slot.acnt = rem_bytes;
457 slot.bcnt = 1;
458 slot.ccnt = 1;
459 slot.src_bidx = rem_bytes;
460 slot.dst_bidx = rem_bytes;
461 slot.src_cidx = 0;
462 slot.dst_cidx = 0;
463 slot.link = EDMA3_PARSET_NULL_LINK;
464 slot.bcntrld = 0;
465 slot.opt = EDMA3_SLOPT_TRANS_COMP_INT_ENB |
466 EDMA3_SLOPT_COMP_CODE(0) |
467 EDMA3_SLOPT_STATIC | EDMA3_SLOPT_AB_SYNC;
468 edma3_slot_configure(edma3_base_addr, edma_slot_num, &slot);
469 edma_channel.slot = edma_slot_num;
470 edma_channel.chnum = 0;
471 edma_channel.complete_code = 0;
472
473 edma_channel.trigger_slot_word = EDMA3_TWORD(dst);
474
475 qedma3_start(edma3_base_addr, &edma_channel);
476 edma3_set_dest_addr(edma3_base_addr, edma_channel.slot, addr +
477 (max_acnt * b_cnt_value));
478 while (edma3_check_for_transfer(edma3_base_addr, &edma_channel))
479 ;
480 qedma3_stop(edma3_base_addr, &edma_channel);
481 }
482}
483
484void __edma3_fill(unsigned long edma3_base_addr, unsigned int edma_slot_num,
485 void *dst, u8 val, size_t len)
486{
487 int xfer_len;
488 int max_xfer = EDMA_FILL_BUFFER_SIZE * 65535;
489
490 memset((void *)edma_fill_buffer, val, sizeof(edma_fill_buffer));
491
492 while (len) {
493 xfer_len = len;
494 if (xfer_len > max_xfer)
495 xfer_len = max_xfer;
496
497 __edma3_transfer(edma3_base_addr, edma_slot_num, dst,
498 edma_fill_buffer, xfer_len,
499 EDMA_FILL_BUFFER_SIZE);
500 len -= xfer_len;
501 dst += xfer_len;
502 }
503}
504
505#ifndef CONFIG_DMA
506
507void edma3_transfer(unsigned long edma3_base_addr, unsigned int edma_slot_num,
508 void *dst, void *src, size_t len)
509{
510 __edma3_transfer(edma3_base_addr, edma_slot_num, dst, src, len, len);
511}
512
513void edma3_fill(unsigned long edma3_base_addr, unsigned int edma_slot_num,
514 void *dst, u8 val, size_t len)
515{
516 __edma3_fill(edma3_base_addr, edma_slot_num, dst, val, len);
517}
518
519#else
520
521static int ti_edma3_transfer(struct udevice *dev, int direction, void *dst,
522 void *src, size_t len)
523{
524 struct ti_edma3_priv *priv = dev_get_priv(dev);
525
526
527 enable_edma3_clocks();
528
529 switch (direction) {
530 case DMA_MEM_TO_MEM:
531 __edma3_transfer(priv->base, 1, dst, src, len, len);
532 break;
533 default:
534 pr_err("Transfer type not implemented in DMA driver\n");
535 break;
536 }
537
538
539 disable_edma3_clocks();
540
541 return 0;
542}
543
544static int ti_edma3_ofdata_to_platdata(struct udevice *dev)
545{
546 struct ti_edma3_priv *priv = dev_get_priv(dev);
547
548 priv->base = devfdt_get_addr(dev);
549
550 return 0;
551}
552
553static int ti_edma3_probe(struct udevice *dev)
554{
555 struct dma_dev_priv *uc_priv = dev_get_uclass_priv(dev);
556
557 uc_priv->supported = DMA_SUPPORTS_MEM_TO_MEM;
558
559 return 0;
560}
561
562static const struct dma_ops ti_edma3_ops = {
563 .transfer = ti_edma3_transfer,
564};
565
566static const struct udevice_id ti_edma3_ids[] = {
567 { .compatible = "ti,edma3" },
568 { }
569};
570
571U_BOOT_DRIVER(ti_edma3) = {
572 .name = "ti_edma3",
573 .id = UCLASS_DMA,
574 .of_match = ti_edma3_ids,
575 .ops = &ti_edma3_ops,
576 .ofdata_to_platdata = ti_edma3_ofdata_to_platdata,
577 .probe = ti_edma3_probe,
578 .priv_auto_alloc_size = sizeof(struct ti_edma3_priv),
579};
580#endif
581