blob: e69617889953ec2c05b844e3d5019f3fe8adf584 [file] [log] [blame]
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001#include <linux/dmaengine.h>
2#include <linux/dma-mapping.h>
3#include <linux/platform_device.h>
4#include <linux/module.h>
5#include <linux/of.h>
6#include <linux/slab.h>
7#include <linux/of_dma.h>
8#include <linux/of_irq.h>
9#include <linux/dmapool.h>
10#include <linux/interrupt.h>
11#include <linux/of_address.h>
12#include "dmaengine.h"
13
14#define DESC_TYPE 27
15#define DESC_TYPE_HOST 0x10
16#define DESC_TYPE_TEARD 0x13
17
18#define TD_DESC_IS_RX (1 << 16)
19#define TD_DESC_DMA_NUM 10
20
21#define DESC_LENGTH_BITS_NUM 21
22
23#define DESC_TYPE_USB (5 << 26)
24#define DESC_PD_COMPLETE (1 << 31)
25
26/* DMA engine */
27#define DMA_TDFDQ 4
28#define DMA_TXGCR(x) (0x800 + (x) * 0x20)
29#define DMA_RXGCR(x) (0x808 + (x) * 0x20)
30#define RXHPCRA0 4
31
32#define GCR_CHAN_ENABLE (1 << 31)
33#define GCR_TEARDOWN (1 << 30)
34#define GCR_STARV_RETRY (1 << 24)
35#define GCR_DESC_TYPE_HOST (1 << 14)
36
37/* DMA scheduler */
38#define DMA_SCHED_CTRL 0
39#define DMA_SCHED_CTRL_EN (1 << 31)
40#define DMA_SCHED_WORD(x) ((x) * 4 + 0x800)
41
42#define SCHED_ENTRY0_CHAN(x) ((x) << 0)
43#define SCHED_ENTRY0_IS_RX (1 << 7)
44
45#define SCHED_ENTRY1_CHAN(x) ((x) << 8)
46#define SCHED_ENTRY1_IS_RX (1 << 15)
47
48#define SCHED_ENTRY2_CHAN(x) ((x) << 16)
49#define SCHED_ENTRY2_IS_RX (1 << 23)
50
51#define SCHED_ENTRY3_CHAN(x) ((x) << 24)
52#define SCHED_ENTRY3_IS_RX (1 << 31)
53
54/* Queue manager */
55/* 4 KiB of memory for descriptors, 2 for each endpoint */
56#define ALLOC_DECS_NUM 128
57#define DESCS_AREAS 1
58#define TOTAL_DESCS_NUM (ALLOC_DECS_NUM * DESCS_AREAS)
59#define QMGR_SCRATCH_SIZE (TOTAL_DESCS_NUM * 4)
60
61#define QMGR_LRAM0_BASE 0x80
62#define QMGR_LRAM_SIZE 0x84
63#define QMGR_LRAM1_BASE 0x88
64#define QMGR_MEMBASE(x) (0x1000 + (x) * 0x10)
65#define QMGR_MEMCTRL(x) (0x1004 + (x) * 0x10)
66#define QMGR_MEMCTRL_IDX_SH 16
67#define QMGR_MEMCTRL_DESC_SH 8
68
69#define QMGR_NUM_PEND 5
70#define QMGR_PEND(x) (0x90 + (x) * 4)
71
72#define QMGR_PENDING_SLOT_Q(x) (x / 32)
73#define QMGR_PENDING_BIT_Q(x) (x % 32)
74
75#define QMGR_QUEUE_A(n) (0x2000 + (n) * 0x10)
76#define QMGR_QUEUE_B(n) (0x2004 + (n) * 0x10)
77#define QMGR_QUEUE_C(n) (0x2008 + (n) * 0x10)
78#define QMGR_QUEUE_D(n) (0x200c + (n) * 0x10)
79
80/* Glue layer specific */
81/* USBSS / USB AM335x */
82#define USBSS_IRQ_STATUS 0x28
83#define USBSS_IRQ_ENABLER 0x2c
84#define USBSS_IRQ_CLEARR 0x30
85
86#define USBSS_IRQ_PD_COMP (1 << 2)
87
88struct cppi41_channel {
89 struct dma_chan chan;
90 struct dma_async_tx_descriptor txd;
91 struct cppi41_dd *cdd;
92 struct cppi41_desc *desc;
93 dma_addr_t desc_phys;
94 void __iomem *gcr_reg;
95 int is_tx;
96 u32 residue;
97
98 unsigned int q_num;
99 unsigned int q_comp_num;
100 unsigned int port_num;
101
102 unsigned td_retry;
103 unsigned td_queued:1;
104 unsigned td_seen:1;
105 unsigned td_desc_seen:1;
106};
107
108struct cppi41_desc {
109 u32 pd0;
110 u32 pd1;
111 u32 pd2;
112 u32 pd3;
113 u32 pd4;
114 u32 pd5;
115 u32 pd6;
116 u32 pd7;
117} __aligned(32);
118
119struct chan_queues {
120 u16 submit;
121 u16 complete;
122};
123
124struct cppi41_dd {
125 struct dma_device ddev;
126
127 void *qmgr_scratch;
128 dma_addr_t scratch_phys;
129
130 struct cppi41_desc *cd;
131 dma_addr_t descs_phys;
132 u32 first_td_desc;
133 struct cppi41_channel *chan_busy[ALLOC_DECS_NUM];
134
135 void __iomem *usbss_mem;
136 void __iomem *ctrl_mem;
137 void __iomem *sched_mem;
138 void __iomem *qmgr_mem;
139 unsigned int irq;
140 const struct chan_queues *queues_rx;
141 const struct chan_queues *queues_tx;
142 struct chan_queues td_queue;
143};
144
145#define FIST_COMPLETION_QUEUE 93
146static struct chan_queues usb_queues_tx[] = {
147 /* USB0 ENDP 1 */
148 [ 0] = { .submit = 32, .complete = 93},
149 [ 1] = { .submit = 34, .complete = 94},
150 [ 2] = { .submit = 36, .complete = 95},
151 [ 3] = { .submit = 38, .complete = 96},
152 [ 4] = { .submit = 40, .complete = 97},
153 [ 5] = { .submit = 42, .complete = 98},
154 [ 6] = { .submit = 44, .complete = 99},
155 [ 7] = { .submit = 46, .complete = 100},
156 [ 8] = { .submit = 48, .complete = 101},
157 [ 9] = { .submit = 50, .complete = 102},
158 [10] = { .submit = 52, .complete = 103},
159 [11] = { .submit = 54, .complete = 104},
160 [12] = { .submit = 56, .complete = 105},
161 [13] = { .submit = 58, .complete = 106},
162 [14] = { .submit = 60, .complete = 107},
163
164 /* USB1 ENDP1 */
165 [15] = { .submit = 62, .complete = 125},
166 [16] = { .submit = 64, .complete = 126},
167 [17] = { .submit = 66, .complete = 127},
168 [18] = { .submit = 68, .complete = 128},
169 [19] = { .submit = 70, .complete = 129},
170 [20] = { .submit = 72, .complete = 130},
171 [21] = { .submit = 74, .complete = 131},
172 [22] = { .submit = 76, .complete = 132},
173 [23] = { .submit = 78, .complete = 133},
174 [24] = { .submit = 80, .complete = 134},
175 [25] = { .submit = 82, .complete = 135},
176 [26] = { .submit = 84, .complete = 136},
177 [27] = { .submit = 86, .complete = 137},
178 [28] = { .submit = 88, .complete = 138},
179 [29] = { .submit = 90, .complete = 139},
180};
181
182static const struct chan_queues usb_queues_rx[] = {
183 /* USB0 ENDP 1 */
184 [ 0] = { .submit = 1, .complete = 109},
185 [ 1] = { .submit = 2, .complete = 110},
186 [ 2] = { .submit = 3, .complete = 111},
187 [ 3] = { .submit = 4, .complete = 112},
188 [ 4] = { .submit = 5, .complete = 113},
189 [ 5] = { .submit = 6, .complete = 114},
190 [ 6] = { .submit = 7, .complete = 115},
191 [ 7] = { .submit = 8, .complete = 116},
192 [ 8] = { .submit = 9, .complete = 117},
193 [ 9] = { .submit = 10, .complete = 118},
194 [10] = { .submit = 11, .complete = 119},
195 [11] = { .submit = 12, .complete = 120},
196 [12] = { .submit = 13, .complete = 121},
197 [13] = { .submit = 14, .complete = 122},
198 [14] = { .submit = 15, .complete = 123},
199
200 /* USB1 ENDP 1 */
201 [15] = { .submit = 16, .complete = 141},
202 [16] = { .submit = 17, .complete = 142},
203 [17] = { .submit = 18, .complete = 143},
204 [18] = { .submit = 19, .complete = 144},
205 [19] = { .submit = 20, .complete = 145},
206 [20] = { .submit = 21, .complete = 146},
207 [21] = { .submit = 22, .complete = 147},
208 [22] = { .submit = 23, .complete = 148},
209 [23] = { .submit = 24, .complete = 149},
210 [24] = { .submit = 25, .complete = 150},
211 [25] = { .submit = 26, .complete = 151},
212 [26] = { .submit = 27, .complete = 152},
213 [27] = { .submit = 28, .complete = 153},
214 [28] = { .submit = 29, .complete = 154},
215 [29] = { .submit = 30, .complete = 155},
216};
217
218struct cppi_glue_infos {
219 irqreturn_t (*isr)(int irq, void *data);
220 const struct chan_queues *queues_rx;
221 const struct chan_queues *queues_tx;
222 struct chan_queues td_queue;
223};
224
225static struct cppi41_channel *to_cpp41_chan(struct dma_chan *c)
226{
227 return container_of(c, struct cppi41_channel, chan);
228}
229
230static struct cppi41_channel *desc_to_chan(struct cppi41_dd *cdd, u32 desc)
231{
232 struct cppi41_channel *c;
233 u32 descs_size;
234 u32 desc_num;
235
236 descs_size = sizeof(struct cppi41_desc) * ALLOC_DECS_NUM;
237
238 if (!((desc >= cdd->descs_phys) &&
239 (desc < (cdd->descs_phys + descs_size)))) {
240 return NULL;
241 }
242
243 desc_num = (desc - cdd->descs_phys) / sizeof(struct cppi41_desc);
244 BUG_ON(desc_num > ALLOC_DECS_NUM);
245 c = cdd->chan_busy[desc_num];
246 cdd->chan_busy[desc_num] = NULL;
247 return c;
248}
249
250static void cppi_writel(u32 val, void *__iomem *mem)
251{
252 __raw_writel(val, mem);
253}
254
255static u32 cppi_readl(void *__iomem *mem)
256{
257 return __raw_readl(mem);
258}
259
260static u32 pd_trans_len(u32 val)
261{
262 return val & ((1 << (DESC_LENGTH_BITS_NUM + 1)) - 1);
263}
264
265static irqreturn_t cppi41_irq(int irq, void *data)
266{
267 struct cppi41_dd *cdd = data;
268 struct cppi41_channel *c;
269 u32 status;
270 int i;
271
272 status = cppi_readl(cdd->usbss_mem + USBSS_IRQ_STATUS);
273 if (!(status & USBSS_IRQ_PD_COMP))
274 return IRQ_NONE;
275 cppi_writel(status, cdd->usbss_mem + USBSS_IRQ_STATUS);
276
277 for (i = QMGR_PENDING_SLOT_Q(FIST_COMPLETION_QUEUE); i < QMGR_NUM_PEND;
278 i++) {
279 u32 val;
280 u32 q_num;
281
282 val = cppi_readl(cdd->qmgr_mem + QMGR_PEND(i));
283 if (i == QMGR_PENDING_SLOT_Q(FIST_COMPLETION_QUEUE) && val) {
284 u32 mask;
285 /* set corresponding bit for completetion Q 93 */
286 mask = 1 << QMGR_PENDING_BIT_Q(FIST_COMPLETION_QUEUE);
287 /* not set all bits for queues less than Q 93 */
288 mask--;
289 /* now invert and keep only Q 93+ set */
290 val &= ~mask;
291 }
292
293 if (val)
294 __iormb();
295
296 while (val) {
297 u32 desc;
298
299 q_num = __fls(val);
300 val &= ~(1 << q_num);
301 q_num += 32 * i;
302 desc = cppi_readl(cdd->qmgr_mem + QMGR_QUEUE_D(q_num));
303 desc &= ~0x1f;
304 c = desc_to_chan(cdd, desc);
305 if (WARN_ON(!c)) {
306 pr_err("%s() q %d desc %08x\n", __func__,
307 q_num, desc);
308 continue;
309 }
310 c->residue = pd_trans_len(c->desc->pd6) -
311 pd_trans_len(c->desc->pd0);
312
313 dma_cookie_complete(&c->txd);
314 c->txd.callback(c->txd.callback_param);
315 }
316 }
317 return IRQ_HANDLED;
318}
319
320static dma_cookie_t cppi41_tx_submit(struct dma_async_tx_descriptor *tx)
321{
322 dma_cookie_t cookie;
323
324 cookie = dma_cookie_assign(tx);
325
326 return cookie;
327}
328
329static int cppi41_dma_alloc_chan_resources(struct dma_chan *chan)
330{
331 struct cppi41_channel *c = to_cpp41_chan(chan);
332
333 dma_cookie_init(chan);
334 dma_async_tx_descriptor_init(&c->txd, chan);
335 c->txd.tx_submit = cppi41_tx_submit;
336
337 if (!c->is_tx)
338 cppi_writel(c->q_num, c->gcr_reg + RXHPCRA0);
339
340 return 0;
341}
342
343static void cppi41_dma_free_chan_resources(struct dma_chan *chan)
344{
345}
346
347static enum dma_status cppi41_dma_tx_status(struct dma_chan *chan,
348 dma_cookie_t cookie, struct dma_tx_state *txstate)
349{
350 struct cppi41_channel *c = to_cpp41_chan(chan);
351 enum dma_status ret;
352
353 /* lock */
354 ret = dma_cookie_status(chan, cookie, txstate);
355 if (txstate && ret == DMA_SUCCESS)
356 txstate->residue = c->residue;
357 /* unlock */
358
359 return ret;
360}
361
362static void push_desc_queue(struct cppi41_channel *c)
363{
364 struct cppi41_dd *cdd = c->cdd;
365 u32 desc_num;
366 u32 desc_phys;
367 u32 reg;
368
369 desc_phys = lower_32_bits(c->desc_phys);
370 desc_num = (desc_phys - cdd->descs_phys) / sizeof(struct cppi41_desc);
371 WARN_ON(cdd->chan_busy[desc_num]);
372 cdd->chan_busy[desc_num] = c;
373
374 reg = (sizeof(struct cppi41_desc) - 24) / 4;
375 reg |= desc_phys;
376 cppi_writel(reg, cdd->qmgr_mem + QMGR_QUEUE_D(c->q_num));
377}
378
379static void cppi41_dma_issue_pending(struct dma_chan *chan)
380{
381 struct cppi41_channel *c = to_cpp41_chan(chan);
382 u32 reg;
383
384 c->residue = 0;
385
386 reg = GCR_CHAN_ENABLE;
387 if (!c->is_tx) {
388 reg |= GCR_STARV_RETRY;
389 reg |= GCR_DESC_TYPE_HOST;
390 reg |= c->q_comp_num;
391 }
392
393 cppi_writel(reg, c->gcr_reg);
394
395 /*
396 * We don't use writel() but __raw_writel() so we have to make sure
397 * that the DMA descriptor in coherent memory made to the main memory
398 * before starting the dma engine.
399 */
400 __iowmb();
401 push_desc_queue(c);
402}
403
404static u32 get_host_pd0(u32 length)
405{
406 u32 reg;
407
408 reg = DESC_TYPE_HOST << DESC_TYPE;
409 reg |= length;
410
411 return reg;
412}
413
414static u32 get_host_pd1(struct cppi41_channel *c)
415{
416 u32 reg;
417
418 reg = 0;
419
420 return reg;
421}
422
423static u32 get_host_pd2(struct cppi41_channel *c)
424{
425 u32 reg;
426
427 reg = DESC_TYPE_USB;
428 reg |= c->q_comp_num;
429
430 return reg;
431}
432
433static u32 get_host_pd3(u32 length)
434{
435 u32 reg;
436
437 /* PD3 = packet size */
438 reg = length;
439
440 return reg;
441}
442
443static u32 get_host_pd6(u32 length)
444{
445 u32 reg;
446
447 /* PD6 buffer size */
448 reg = DESC_PD_COMPLETE;
449 reg |= length;
450
451 return reg;
452}
453
454static u32 get_host_pd4_or_7(u32 addr)
455{
456 u32 reg;
457
458 reg = addr;
459
460 return reg;
461}
462
463static u32 get_host_pd5(void)
464{
465 u32 reg;
466
467 reg = 0;
468
469 return reg;
470}
471
472static struct dma_async_tx_descriptor *cppi41_dma_prep_slave_sg(
473 struct dma_chan *chan, struct scatterlist *sgl, unsigned sg_len,
474 enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
475{
476 struct cppi41_channel *c = to_cpp41_chan(chan);
477 struct cppi41_desc *d;
478 struct scatterlist *sg;
479 unsigned int i;
480 unsigned int num;
481
482 num = 0;
483 d = c->desc;
484 for_each_sg(sgl, sg, sg_len, i) {
485 u32 addr;
486 u32 len;
487
488 /* We need to use more than one desc once musb supports sg */
489 BUG_ON(num > 0);
490 addr = lower_32_bits(sg_dma_address(sg));
491 len = sg_dma_len(sg);
492
493 d->pd0 = get_host_pd0(len);
494 d->pd1 = get_host_pd1(c);
495 d->pd2 = get_host_pd2(c);
496 d->pd3 = get_host_pd3(len);
497 d->pd4 = get_host_pd4_or_7(addr);
498 d->pd5 = get_host_pd5();
499 d->pd6 = get_host_pd6(len);
500 d->pd7 = get_host_pd4_or_7(addr);
501
502 d++;
503 }
504
505 return &c->txd;
506}
507
508static int cpp41_cfg_chan(struct cppi41_channel *c,
509 struct dma_slave_config *cfg)
510{
511 return 0;
512}
513
514static void cppi41_compute_td_desc(struct cppi41_desc *d)
515{
516 d->pd0 = DESC_TYPE_TEARD << DESC_TYPE;
517}
518
519static u32 cppi41_pop_desc(struct cppi41_dd *cdd, unsigned queue_num)
520{
521 u32 desc;
522
523 desc = cppi_readl(cdd->qmgr_mem + QMGR_QUEUE_D(queue_num));
524 desc &= ~0x1f;
525 return desc;
526}
527
528static int cppi41_tear_down_chan(struct cppi41_channel *c)
529{
530 struct cppi41_dd *cdd = c->cdd;
531 struct cppi41_desc *td;
532 u32 reg;
533 u32 desc_phys;
534 u32 td_desc_phys;
535
536 td = cdd->cd;
537 td += cdd->first_td_desc;
538
539 td_desc_phys = cdd->descs_phys;
540 td_desc_phys += cdd->first_td_desc * sizeof(struct cppi41_desc);
541
542 if (!c->td_queued) {
543 cppi41_compute_td_desc(td);
544 __iowmb();
545
546 reg = (sizeof(struct cppi41_desc) - 24) / 4;
547 reg |= td_desc_phys;
548 cppi_writel(reg, cdd->qmgr_mem +
549 QMGR_QUEUE_D(cdd->td_queue.submit));
550
551 reg = GCR_CHAN_ENABLE;
552 if (!c->is_tx) {
553 reg |= GCR_STARV_RETRY;
554 reg |= GCR_DESC_TYPE_HOST;
555 reg |= c->q_comp_num;
556 }
557 reg |= GCR_TEARDOWN;
558 cppi_writel(reg, c->gcr_reg);
559 c->td_queued = 1;
560 c->td_retry = 100;
561 }
562
563 if (!c->td_seen) {
564 unsigned td_comp_queue;
565
566 if (c->is_tx)
567 td_comp_queue = cdd->td_queue.complete;
568 else
569 td_comp_queue = c->q_comp_num;
570
571 desc_phys = cppi41_pop_desc(cdd, td_comp_queue);
572 if (desc_phys) {
573 __iormb();
574
575 if (desc_phys == td_desc_phys) {
576 u32 pd0;
577 pd0 = td->pd0;
578 WARN_ON((pd0 >> DESC_TYPE) != DESC_TYPE_TEARD);
579 WARN_ON(!c->is_tx && !(pd0 & TD_DESC_IS_RX));
580 WARN_ON((pd0 & 0x1f) != c->port_num);
581 } else {
Sebastian Andrzej Siewiorbd2fbf32013-08-16 17:40:55 +0200582 WARN_ON_ONCE(1);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200583 }
584 c->td_seen = 1;
585 }
586 }
587 if (!c->td_desc_seen) {
588 desc_phys = cppi41_pop_desc(cdd, c->q_comp_num);
589 if (desc_phys) {
590 __iormb();
591 WARN_ON(c->desc_phys != desc_phys);
592 c->td_desc_seen = 1;
593 }
594 }
595 c->td_retry--;
596 /*
597 * If the TX descriptor / channel is in use, the caller needs to poke
598 * his TD bit multiple times. After that he hardware releases the
599 * transfer descriptor followed by TD descriptor. Waiting seems not to
600 * cause any difference.
601 * RX seems to be thrown out right away. However once the TearDown
602 * descriptor gets through we are done. If we have seens the transfer
603 * descriptor before the TD we fetch it from enqueue, it has to be
604 * there waiting for us.
605 */
606 if (!c->td_seen && c->td_retry)
607 return -EAGAIN;
608
609 WARN_ON(!c->td_retry);
610 if (!c->td_desc_seen) {
611 desc_phys = cppi_readl(cdd->qmgr_mem + QMGR_QUEUE_D(c->q_num));
612 WARN_ON(!desc_phys);
613 }
614
615 c->td_queued = 0;
616 c->td_seen = 0;
617 c->td_desc_seen = 0;
618 cppi_writel(0, c->gcr_reg);
619 return 0;
620}
621
622static int cppi41_stop_chan(struct dma_chan *chan)
623{
624 struct cppi41_channel *c = to_cpp41_chan(chan);
625 struct cppi41_dd *cdd = c->cdd;
626 u32 desc_num;
627 u32 desc_phys;
628 int ret;
629
630 ret = cppi41_tear_down_chan(c);
631 if (ret)
632 return ret;
633
634 desc_phys = lower_32_bits(c->desc_phys);
635 desc_num = (desc_phys - cdd->descs_phys) / sizeof(struct cppi41_desc);
636 WARN_ON(!cdd->chan_busy[desc_num]);
637 cdd->chan_busy[desc_num] = NULL;
638
639 return 0;
640}
641
642static int cppi41_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
643 unsigned long arg)
644{
645 struct cppi41_channel *c = to_cpp41_chan(chan);
646 int ret;
647
648 switch (cmd) {
649 case DMA_SLAVE_CONFIG:
650 ret = cpp41_cfg_chan(c, (struct dma_slave_config *) arg);
651 break;
652
653 case DMA_TERMINATE_ALL:
654 ret = cppi41_stop_chan(chan);
655 break;
656
657 default:
658 ret = -ENXIO;
659 break;
660 }
661 return ret;
662}
663
664static void cleanup_chans(struct cppi41_dd *cdd)
665{
666 while (!list_empty(&cdd->ddev.channels)) {
667 struct cppi41_channel *cchan;
668
669 cchan = list_first_entry(&cdd->ddev.channels,
670 struct cppi41_channel, chan.device_node);
671 list_del(&cchan->chan.device_node);
672 kfree(cchan);
673 }
674}
675
676static int cppi41_add_chans(struct platform_device *pdev, struct cppi41_dd *cdd)
677{
678 struct cppi41_channel *cchan;
679 int i;
680 int ret;
681 u32 n_chans;
682
683 ret = of_property_read_u32(pdev->dev.of_node, "#dma-channels",
684 &n_chans);
685 if (ret)
686 return ret;
687 /*
688 * The channels can only be used as TX or as RX. So we add twice
689 * that much dma channels because USB can only do RX or TX.
690 */
691 n_chans *= 2;
692
693 for (i = 0; i < n_chans; i++) {
694 cchan = kzalloc(sizeof(*cchan), GFP_KERNEL);
695 if (!cchan)
696 goto err;
697
698 cchan->cdd = cdd;
699 if (i & 1) {
700 cchan->gcr_reg = cdd->ctrl_mem + DMA_TXGCR(i >> 1);
701 cchan->is_tx = 1;
702 } else {
703 cchan->gcr_reg = cdd->ctrl_mem + DMA_RXGCR(i >> 1);
704 cchan->is_tx = 0;
705 }
706 cchan->port_num = i >> 1;
707 cchan->desc = &cdd->cd[i];
708 cchan->desc_phys = cdd->descs_phys;
709 cchan->desc_phys += i * sizeof(struct cppi41_desc);
710 cchan->chan.device = &cdd->ddev;
711 list_add_tail(&cchan->chan.device_node, &cdd->ddev.channels);
712 }
713 cdd->first_td_desc = n_chans;
714
715 return 0;
716err:
717 cleanup_chans(cdd);
718 return -ENOMEM;
719}
720
721static void purge_descs(struct platform_device *pdev, struct cppi41_dd *cdd)
722{
723 unsigned int mem_decs;
724 int i;
725
726 mem_decs = ALLOC_DECS_NUM * sizeof(struct cppi41_desc);
727
728 for (i = 0; i < DESCS_AREAS; i++) {
729
730 cppi_writel(0, cdd->qmgr_mem + QMGR_MEMBASE(i));
731 cppi_writel(0, cdd->qmgr_mem + QMGR_MEMCTRL(i));
732
733 dma_free_coherent(&pdev->dev, mem_decs, cdd->cd,
734 cdd->descs_phys);
735 }
736}
737
738static void disable_sched(struct cppi41_dd *cdd)
739{
740 cppi_writel(0, cdd->sched_mem + DMA_SCHED_CTRL);
741}
742
743static void deinit_cpii41(struct platform_device *pdev, struct cppi41_dd *cdd)
744{
745 disable_sched(cdd);
746
747 purge_descs(pdev, cdd);
748
749 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM0_BASE);
750 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM0_BASE);
751 dma_free_coherent(&pdev->dev, QMGR_SCRATCH_SIZE, cdd->qmgr_scratch,
752 cdd->scratch_phys);
753}
754
755static int init_descs(struct platform_device *pdev, struct cppi41_dd *cdd)
756{
757 unsigned int desc_size;
758 unsigned int mem_decs;
759 int i;
760 u32 reg;
761 u32 idx;
762
763 BUILD_BUG_ON(sizeof(struct cppi41_desc) &
764 (sizeof(struct cppi41_desc) - 1));
765 BUILD_BUG_ON(sizeof(struct cppi41_desc) < 32);
766 BUILD_BUG_ON(ALLOC_DECS_NUM < 32);
767
768 desc_size = sizeof(struct cppi41_desc);
769 mem_decs = ALLOC_DECS_NUM * desc_size;
770
771 idx = 0;
772 for (i = 0; i < DESCS_AREAS; i++) {
773
774 reg = idx << QMGR_MEMCTRL_IDX_SH;
775 reg |= (ilog2(desc_size) - 5) << QMGR_MEMCTRL_DESC_SH;
776 reg |= ilog2(ALLOC_DECS_NUM) - 5;
777
778 BUILD_BUG_ON(DESCS_AREAS != 1);
779 cdd->cd = dma_alloc_coherent(&pdev->dev, mem_decs,
780 &cdd->descs_phys, GFP_KERNEL);
781 if (!cdd->cd)
782 return -ENOMEM;
783
784 cppi_writel(cdd->descs_phys, cdd->qmgr_mem + QMGR_MEMBASE(i));
785 cppi_writel(reg, cdd->qmgr_mem + QMGR_MEMCTRL(i));
786
787 idx += ALLOC_DECS_NUM;
788 }
789 return 0;
790}
791
792static void init_sched(struct cppi41_dd *cdd)
793{
794 unsigned ch;
795 unsigned word;
796 u32 reg;
797
798 word = 0;
799 cppi_writel(0, cdd->sched_mem + DMA_SCHED_CTRL);
800 for (ch = 0; ch < 15 * 2; ch += 2) {
801
802 reg = SCHED_ENTRY0_CHAN(ch);
803 reg |= SCHED_ENTRY1_CHAN(ch) | SCHED_ENTRY1_IS_RX;
804
805 reg |= SCHED_ENTRY2_CHAN(ch + 1);
806 reg |= SCHED_ENTRY3_CHAN(ch + 1) | SCHED_ENTRY3_IS_RX;
807 cppi_writel(reg, cdd->sched_mem + DMA_SCHED_WORD(word));
808 word++;
809 }
810 reg = 15 * 2 * 2 - 1;
811 reg |= DMA_SCHED_CTRL_EN;
812 cppi_writel(reg, cdd->sched_mem + DMA_SCHED_CTRL);
813}
814
815static int init_cppi41(struct platform_device *pdev, struct cppi41_dd *cdd)
816{
817 int ret;
818
819 BUILD_BUG_ON(QMGR_SCRATCH_SIZE > ((1 << 14) - 1));
820 cdd->qmgr_scratch = dma_alloc_coherent(&pdev->dev, QMGR_SCRATCH_SIZE,
821 &cdd->scratch_phys, GFP_KERNEL);
822 if (!cdd->qmgr_scratch)
823 return -ENOMEM;
824
825 cppi_writel(cdd->scratch_phys, cdd->qmgr_mem + QMGR_LRAM0_BASE);
826 cppi_writel(QMGR_SCRATCH_SIZE, cdd->qmgr_mem + QMGR_LRAM_SIZE);
827 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM1_BASE);
828
829 ret = init_descs(pdev, cdd);
830 if (ret)
831 goto err_td;
832
833 cppi_writel(cdd->td_queue.submit, cdd->ctrl_mem + DMA_TDFDQ);
834 init_sched(cdd);
835 return 0;
836err_td:
837 deinit_cpii41(pdev, cdd);
838 return ret;
839}
840
841static struct platform_driver cpp41_dma_driver;
842/*
843 * The param format is:
844 * X Y
845 * X: Port
846 * Y: 0 = RX else TX
847 */
848#define INFO_PORT 0
849#define INFO_IS_TX 1
850
851static bool cpp41_dma_filter_fn(struct dma_chan *chan, void *param)
852{
853 struct cppi41_channel *cchan;
854 struct cppi41_dd *cdd;
855 const struct chan_queues *queues;
856 u32 *num = param;
857
858 if (chan->device->dev->driver != &cpp41_dma_driver.driver)
859 return false;
860
861 cchan = to_cpp41_chan(chan);
862
863 if (cchan->port_num != num[INFO_PORT])
864 return false;
865
866 if (cchan->is_tx && !num[INFO_IS_TX])
867 return false;
868 cdd = cchan->cdd;
869 if (cchan->is_tx)
870 queues = cdd->queues_tx;
871 else
872 queues = cdd->queues_rx;
873
874 BUILD_BUG_ON(ARRAY_SIZE(usb_queues_rx) != ARRAY_SIZE(usb_queues_tx));
875 if (WARN_ON(cchan->port_num > ARRAY_SIZE(usb_queues_rx)))
876 return false;
877
878 cchan->q_num = queues[cchan->port_num].submit;
879 cchan->q_comp_num = queues[cchan->port_num].complete;
880 return true;
881}
882
883static struct of_dma_filter_info cpp41_dma_info = {
884 .filter_fn = cpp41_dma_filter_fn,
885};
886
887static struct dma_chan *cppi41_dma_xlate(struct of_phandle_args *dma_spec,
888 struct of_dma *ofdma)
889{
890 int count = dma_spec->args_count;
891 struct of_dma_filter_info *info = ofdma->of_dma_data;
892
893 if (!info || !info->filter_fn)
894 return NULL;
895
896 if (count != 2)
897 return NULL;
898
899 return dma_request_channel(info->dma_cap, info->filter_fn,
900 &dma_spec->args[0]);
901}
902
903static const struct cppi_glue_infos usb_infos = {
904 .isr = cppi41_irq,
905 .queues_rx = usb_queues_rx,
906 .queues_tx = usb_queues_tx,
907 .td_queue = { .submit = 31, .complete = 0 },
908};
909
910static const struct of_device_id cppi41_dma_ids[] = {
911 { .compatible = "ti,am3359-cppi41", .data = &usb_infos},
912 {},
913};
914MODULE_DEVICE_TABLE(of, cppi41_dma_ids);
915
916static const struct cppi_glue_infos *get_glue_info(struct platform_device *pdev)
917{
918 const struct of_device_id *of_id;
919
920 of_id = of_match_node(cppi41_dma_ids, pdev->dev.of_node);
921 if (!of_id)
922 return NULL;
923 return of_id->data;
924}
925
926static int cppi41_dma_probe(struct platform_device *pdev)
927{
928 struct cppi41_dd *cdd;
929 const struct cppi_glue_infos *glue_info;
930 int irq;
931 int ret;
932
933 glue_info = get_glue_info(pdev);
934 if (!glue_info)
935 return -EINVAL;
936
937 cdd = kzalloc(sizeof(*cdd), GFP_KERNEL);
938 if (!cdd)
939 return -ENOMEM;
940
941 dma_cap_set(DMA_SLAVE, cdd->ddev.cap_mask);
942 cdd->ddev.device_alloc_chan_resources = cppi41_dma_alloc_chan_resources;
943 cdd->ddev.device_free_chan_resources = cppi41_dma_free_chan_resources;
944 cdd->ddev.device_tx_status = cppi41_dma_tx_status;
945 cdd->ddev.device_issue_pending = cppi41_dma_issue_pending;
946 cdd->ddev.device_prep_slave_sg = cppi41_dma_prep_slave_sg;
947 cdd->ddev.device_control = cppi41_dma_control;
948 cdd->ddev.dev = &pdev->dev;
949 INIT_LIST_HEAD(&cdd->ddev.channels);
950 cpp41_dma_info.dma_cap = cdd->ddev.cap_mask;
951
952 cdd->usbss_mem = of_iomap(pdev->dev.of_node, 0);
953 cdd->ctrl_mem = of_iomap(pdev->dev.of_node, 1);
954 cdd->sched_mem = of_iomap(pdev->dev.of_node, 2);
955 cdd->qmgr_mem = of_iomap(pdev->dev.of_node, 3);
956
957 if (!cdd->usbss_mem || !cdd->ctrl_mem || !cdd->sched_mem ||
958 !cdd->qmgr_mem) {
959 ret = -ENXIO;
960 goto err_remap;
961 }
962
963 cdd->queues_rx = glue_info->queues_rx;
964 cdd->queues_tx = glue_info->queues_tx;
965 cdd->td_queue = glue_info->td_queue;
966
967 ret = init_cppi41(pdev, cdd);
968 if (ret)
969 goto err_init_cppi;
970
971 ret = cppi41_add_chans(pdev, cdd);
972 if (ret)
973 goto err_chans;
974
975 irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
976 if (!irq)
977 goto err_irq;
978
979 cppi_writel(USBSS_IRQ_PD_COMP, cdd->usbss_mem + USBSS_IRQ_ENABLER);
980
981 ret = request_irq(irq, glue_info->isr, IRQF_SHARED,
982 dev_name(&pdev->dev), cdd);
983 if (ret)
984 goto err_irq;
985 cdd->irq = irq;
986
987 ret = dma_async_device_register(&cdd->ddev);
988 if (ret)
989 goto err_dma_reg;
990
991 ret = of_dma_controller_register(pdev->dev.of_node,
992 cppi41_dma_xlate, &cpp41_dma_info);
993 if (ret)
994 goto err_of;
995
996 platform_set_drvdata(pdev, cdd);
997 return 0;
998err_of:
999 dma_async_device_unregister(&cdd->ddev);
1000err_dma_reg:
1001 free_irq(irq, cdd);
1002err_irq:
1003 cppi_writel(0, cdd->usbss_mem + USBSS_IRQ_CLEARR);
1004 cleanup_chans(cdd);
1005err_chans:
1006 deinit_cpii41(pdev, cdd);
1007err_init_cppi:
1008 iounmap(cdd->usbss_mem);
1009 iounmap(cdd->ctrl_mem);
1010 iounmap(cdd->sched_mem);
1011 iounmap(cdd->qmgr_mem);
1012err_remap:
1013 kfree(cdd);
1014 return ret;
1015}
1016
1017static int cppi41_dma_remove(struct platform_device *pdev)
1018{
1019 struct cppi41_dd *cdd = platform_get_drvdata(pdev);
1020
1021 of_dma_controller_free(pdev->dev.of_node);
1022 dma_async_device_unregister(&cdd->ddev);
1023
1024 cppi_writel(0, cdd->usbss_mem + USBSS_IRQ_CLEARR);
1025 free_irq(cdd->irq, cdd);
1026 cleanup_chans(cdd);
1027 deinit_cpii41(pdev, cdd);
1028 iounmap(cdd->usbss_mem);
1029 iounmap(cdd->ctrl_mem);
1030 iounmap(cdd->sched_mem);
1031 iounmap(cdd->qmgr_mem);
1032 kfree(cdd);
1033 return 0;
1034}
1035
1036static struct platform_driver cpp41_dma_driver = {
1037 .probe = cppi41_dma_probe,
1038 .remove = cppi41_dma_remove,
1039 .driver = {
1040 .name = "cppi41-dma-engine",
1041 .owner = THIS_MODULE,
1042 .of_match_table = of_match_ptr(cppi41_dma_ids),
1043 },
1044};
1045
1046module_platform_driver(cpp41_dma_driver);
1047MODULE_LICENSE("GPL");
1048MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");