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