blob: 4b52126c13cfca956f3ebd8c34b491e6c68d7135 [file] [log] [blame]
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +01001#include <linux/delay.h>
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02002#include <linux/dmaengine.h>
3#include <linux/dma-mapping.h>
4#include <linux/platform_device.h>
5#include <linux/module.h>
6#include <linux/of.h>
7#include <linux/slab.h>
8#include <linux/of_dma.h>
9#include <linux/of_irq.h>
10#include <linux/dmapool.h>
11#include <linux/interrupt.h>
12#include <linux/of_address.h>
Sebastian Andrzej Siewiord6aafa22013-08-20 18:35:53 +020013#include <linux/pm_runtime.h>
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020014#include "dmaengine.h"
15
16#define DESC_TYPE 27
17#define DESC_TYPE_HOST 0x10
18#define DESC_TYPE_TEARD 0x13
19
20#define TD_DESC_IS_RX (1 << 16)
21#define TD_DESC_DMA_NUM 10
22
23#define DESC_LENGTH_BITS_NUM 21
24
25#define DESC_TYPE_USB (5 << 26)
26#define DESC_PD_COMPLETE (1 << 31)
27
28/* DMA engine */
29#define DMA_TDFDQ 4
30#define DMA_TXGCR(x) (0x800 + (x) * 0x20)
31#define DMA_RXGCR(x) (0x808 + (x) * 0x20)
32#define RXHPCRA0 4
33
34#define GCR_CHAN_ENABLE (1 << 31)
35#define GCR_TEARDOWN (1 << 30)
36#define GCR_STARV_RETRY (1 << 24)
37#define GCR_DESC_TYPE_HOST (1 << 14)
38
39/* DMA scheduler */
40#define DMA_SCHED_CTRL 0
41#define DMA_SCHED_CTRL_EN (1 << 31)
42#define DMA_SCHED_WORD(x) ((x) * 4 + 0x800)
43
44#define SCHED_ENTRY0_CHAN(x) ((x) << 0)
45#define SCHED_ENTRY0_IS_RX (1 << 7)
46
47#define SCHED_ENTRY1_CHAN(x) ((x) << 8)
48#define SCHED_ENTRY1_IS_RX (1 << 15)
49
50#define SCHED_ENTRY2_CHAN(x) ((x) << 16)
51#define SCHED_ENTRY2_IS_RX (1 << 23)
52
53#define SCHED_ENTRY3_CHAN(x) ((x) << 24)
54#define SCHED_ENTRY3_IS_RX (1 << 31)
55
56/* Queue manager */
57/* 4 KiB of memory for descriptors, 2 for each endpoint */
58#define ALLOC_DECS_NUM 128
59#define DESCS_AREAS 1
60#define TOTAL_DESCS_NUM (ALLOC_DECS_NUM * DESCS_AREAS)
61#define QMGR_SCRATCH_SIZE (TOTAL_DESCS_NUM * 4)
62
63#define QMGR_LRAM0_BASE 0x80
64#define QMGR_LRAM_SIZE 0x84
65#define QMGR_LRAM1_BASE 0x88
66#define QMGR_MEMBASE(x) (0x1000 + (x) * 0x10)
67#define QMGR_MEMCTRL(x) (0x1004 + (x) * 0x10)
68#define QMGR_MEMCTRL_IDX_SH 16
69#define QMGR_MEMCTRL_DESC_SH 8
70
71#define QMGR_NUM_PEND 5
72#define QMGR_PEND(x) (0x90 + (x) * 4)
73
74#define QMGR_PENDING_SLOT_Q(x) (x / 32)
75#define QMGR_PENDING_BIT_Q(x) (x % 32)
76
77#define QMGR_QUEUE_A(n) (0x2000 + (n) * 0x10)
78#define QMGR_QUEUE_B(n) (0x2004 + (n) * 0x10)
79#define QMGR_QUEUE_C(n) (0x2008 + (n) * 0x10)
80#define QMGR_QUEUE_D(n) (0x200c + (n) * 0x10)
81
82/* Glue layer specific */
83/* USBSS / USB AM335x */
84#define USBSS_IRQ_STATUS 0x28
85#define USBSS_IRQ_ENABLER 0x2c
86#define USBSS_IRQ_CLEARR 0x30
87
88#define USBSS_IRQ_PD_COMP (1 << 2)
89
Daniel Mack13bbfb52014-05-26 14:52:34 +020090/* Packet Descriptor */
91#define PD2_ZERO_LENGTH (1 << 19)
92
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020093struct cppi41_channel {
94 struct dma_chan chan;
95 struct dma_async_tx_descriptor txd;
96 struct cppi41_dd *cdd;
97 struct cppi41_desc *desc;
98 dma_addr_t desc_phys;
99 void __iomem *gcr_reg;
100 int is_tx;
101 u32 residue;
102
103 unsigned int q_num;
104 unsigned int q_comp_num;
105 unsigned int port_num;
106
107 unsigned td_retry;
108 unsigned td_queued:1;
109 unsigned td_seen:1;
110 unsigned td_desc_seen:1;
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700111
112 struct list_head node; /* Node for pending list */
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200113};
114
115struct cppi41_desc {
116 u32 pd0;
117 u32 pd1;
118 u32 pd2;
119 u32 pd3;
120 u32 pd4;
121 u32 pd5;
122 u32 pd6;
123 u32 pd7;
124} __aligned(32);
125
126struct chan_queues {
127 u16 submit;
128 u16 complete;
129};
130
131struct cppi41_dd {
132 struct dma_device ddev;
133
134 void *qmgr_scratch;
135 dma_addr_t scratch_phys;
136
137 struct cppi41_desc *cd;
138 dma_addr_t descs_phys;
139 u32 first_td_desc;
140 struct cppi41_channel *chan_busy[ALLOC_DECS_NUM];
141
142 void __iomem *usbss_mem;
143 void __iomem *ctrl_mem;
144 void __iomem *sched_mem;
145 void __iomem *qmgr_mem;
146 unsigned int irq;
147 const struct chan_queues *queues_rx;
148 const struct chan_queues *queues_tx;
149 struct chan_queues td_queue;
Daniel Mackf8964962013-10-22 12:14:03 +0200150
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700151 struct list_head pending; /* Pending queued transfers */
152 spinlock_t lock; /* Lock for pending list */
153
Daniel Mackf8964962013-10-22 12:14:03 +0200154 /* context for suspend/resume */
155 unsigned int dma_tdfdq;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200156};
157
158#define FIST_COMPLETION_QUEUE 93
159static struct chan_queues usb_queues_tx[] = {
160 /* USB0 ENDP 1 */
161 [ 0] = { .submit = 32, .complete = 93},
162 [ 1] = { .submit = 34, .complete = 94},
163 [ 2] = { .submit = 36, .complete = 95},
164 [ 3] = { .submit = 38, .complete = 96},
165 [ 4] = { .submit = 40, .complete = 97},
166 [ 5] = { .submit = 42, .complete = 98},
167 [ 6] = { .submit = 44, .complete = 99},
168 [ 7] = { .submit = 46, .complete = 100},
169 [ 8] = { .submit = 48, .complete = 101},
170 [ 9] = { .submit = 50, .complete = 102},
171 [10] = { .submit = 52, .complete = 103},
172 [11] = { .submit = 54, .complete = 104},
173 [12] = { .submit = 56, .complete = 105},
174 [13] = { .submit = 58, .complete = 106},
175 [14] = { .submit = 60, .complete = 107},
176
177 /* USB1 ENDP1 */
178 [15] = { .submit = 62, .complete = 125},
179 [16] = { .submit = 64, .complete = 126},
180 [17] = { .submit = 66, .complete = 127},
181 [18] = { .submit = 68, .complete = 128},
182 [19] = { .submit = 70, .complete = 129},
183 [20] = { .submit = 72, .complete = 130},
184 [21] = { .submit = 74, .complete = 131},
185 [22] = { .submit = 76, .complete = 132},
186 [23] = { .submit = 78, .complete = 133},
187 [24] = { .submit = 80, .complete = 134},
188 [25] = { .submit = 82, .complete = 135},
189 [26] = { .submit = 84, .complete = 136},
190 [27] = { .submit = 86, .complete = 137},
191 [28] = { .submit = 88, .complete = 138},
192 [29] = { .submit = 90, .complete = 139},
193};
194
195static const struct chan_queues usb_queues_rx[] = {
196 /* USB0 ENDP 1 */
197 [ 0] = { .submit = 1, .complete = 109},
198 [ 1] = { .submit = 2, .complete = 110},
199 [ 2] = { .submit = 3, .complete = 111},
200 [ 3] = { .submit = 4, .complete = 112},
201 [ 4] = { .submit = 5, .complete = 113},
202 [ 5] = { .submit = 6, .complete = 114},
203 [ 6] = { .submit = 7, .complete = 115},
204 [ 7] = { .submit = 8, .complete = 116},
205 [ 8] = { .submit = 9, .complete = 117},
206 [ 9] = { .submit = 10, .complete = 118},
207 [10] = { .submit = 11, .complete = 119},
208 [11] = { .submit = 12, .complete = 120},
209 [12] = { .submit = 13, .complete = 121},
210 [13] = { .submit = 14, .complete = 122},
211 [14] = { .submit = 15, .complete = 123},
212
213 /* USB1 ENDP 1 */
214 [15] = { .submit = 16, .complete = 141},
215 [16] = { .submit = 17, .complete = 142},
216 [17] = { .submit = 18, .complete = 143},
217 [18] = { .submit = 19, .complete = 144},
218 [19] = { .submit = 20, .complete = 145},
219 [20] = { .submit = 21, .complete = 146},
220 [21] = { .submit = 22, .complete = 147},
221 [22] = { .submit = 23, .complete = 148},
222 [23] = { .submit = 24, .complete = 149},
223 [24] = { .submit = 25, .complete = 150},
224 [25] = { .submit = 26, .complete = 151},
225 [26] = { .submit = 27, .complete = 152},
226 [27] = { .submit = 28, .complete = 153},
227 [28] = { .submit = 29, .complete = 154},
228 [29] = { .submit = 30, .complete = 155},
229};
230
231struct cppi_glue_infos {
232 irqreturn_t (*isr)(int irq, void *data);
233 const struct chan_queues *queues_rx;
234 const struct chan_queues *queues_tx;
235 struct chan_queues td_queue;
236};
237
238static struct cppi41_channel *to_cpp41_chan(struct dma_chan *c)
239{
240 return container_of(c, struct cppi41_channel, chan);
241}
242
243static struct cppi41_channel *desc_to_chan(struct cppi41_dd *cdd, u32 desc)
244{
245 struct cppi41_channel *c;
246 u32 descs_size;
247 u32 desc_num;
248
249 descs_size = sizeof(struct cppi41_desc) * ALLOC_DECS_NUM;
250
251 if (!((desc >= cdd->descs_phys) &&
252 (desc < (cdd->descs_phys + descs_size)))) {
253 return NULL;
254 }
255
256 desc_num = (desc - cdd->descs_phys) / sizeof(struct cppi41_desc);
Dan Carpenter2d17f7f2013-08-28 13:48:44 +0300257 BUG_ON(desc_num >= ALLOC_DECS_NUM);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200258 c = cdd->chan_busy[desc_num];
259 cdd->chan_busy[desc_num] = NULL;
260 return c;
261}
262
263static void cppi_writel(u32 val, void *__iomem *mem)
264{
265 __raw_writel(val, mem);
266}
267
268static u32 cppi_readl(void *__iomem *mem)
269{
270 return __raw_readl(mem);
271}
272
273static u32 pd_trans_len(u32 val)
274{
275 return val & ((1 << (DESC_LENGTH_BITS_NUM + 1)) - 1);
276}
277
Daniel Mack706ff622013-10-22 12:14:04 +0200278static u32 cppi41_pop_desc(struct cppi41_dd *cdd, unsigned queue_num)
279{
280 u32 desc;
281
282 desc = cppi_readl(cdd->qmgr_mem + QMGR_QUEUE_D(queue_num));
283 desc &= ~0x1f;
284 return desc;
285}
286
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200287static irqreturn_t cppi41_irq(int irq, void *data)
288{
289 struct cppi41_dd *cdd = data;
290 struct cppi41_channel *c;
291 u32 status;
292 int i;
293
294 status = cppi_readl(cdd->usbss_mem + USBSS_IRQ_STATUS);
295 if (!(status & USBSS_IRQ_PD_COMP))
296 return IRQ_NONE;
297 cppi_writel(status, cdd->usbss_mem + USBSS_IRQ_STATUS);
298
299 for (i = QMGR_PENDING_SLOT_Q(FIST_COMPLETION_QUEUE); i < QMGR_NUM_PEND;
300 i++) {
301 u32 val;
302 u32 q_num;
303
304 val = cppi_readl(cdd->qmgr_mem + QMGR_PEND(i));
305 if (i == QMGR_PENDING_SLOT_Q(FIST_COMPLETION_QUEUE) && val) {
306 u32 mask;
307 /* set corresponding bit for completetion Q 93 */
308 mask = 1 << QMGR_PENDING_BIT_Q(FIST_COMPLETION_QUEUE);
309 /* not set all bits for queues less than Q 93 */
310 mask--;
311 /* now invert and keep only Q 93+ set */
312 val &= ~mask;
313 }
314
315 if (val)
316 __iormb();
317
318 while (val) {
Daniel Mack13bbfb52014-05-26 14:52:34 +0200319 u32 desc, len;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200320
Tony Lindgren098de422016-11-09 09:47:59 -0700321 status = pm_runtime_get(cdd->ddev.dev);
322 if (status < 0)
323 dev_err(cdd->ddev.dev, "%s pm runtime get: %i\n",
324 __func__, status);
325
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200326 q_num = __fls(val);
327 val &= ~(1 << q_num);
328 q_num += 32 * i;
Daniel Mack706ff622013-10-22 12:14:04 +0200329 desc = cppi41_pop_desc(cdd, q_num);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200330 c = desc_to_chan(cdd, desc);
331 if (WARN_ON(!c)) {
332 pr_err("%s() q %d desc %08x\n", __func__,
333 q_num, desc);
334 continue;
335 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200336
Daniel Mack13bbfb52014-05-26 14:52:34 +0200337 if (c->desc->pd2 & PD2_ZERO_LENGTH)
338 len = 0;
339 else
340 len = pd_trans_len(c->desc->pd0);
341
342 c->residue = pd_trans_len(c->desc->pd6) - len;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200343 dma_cookie_complete(&c->txd);
Dave Jiangb310a612016-07-20 13:10:54 -0700344 dmaengine_desc_get_callback_invoke(&c->txd, NULL);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700345
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700346 pm_runtime_mark_last_busy(cdd->ddev.dev);
347 pm_runtime_put_autosuspend(cdd->ddev.dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200348 }
349 }
350 return IRQ_HANDLED;
351}
352
353static dma_cookie_t cppi41_tx_submit(struct dma_async_tx_descriptor *tx)
354{
355 dma_cookie_t cookie;
356
357 cookie = dma_cookie_assign(tx);
358
359 return cookie;
360}
361
362static int cppi41_dma_alloc_chan_resources(struct dma_chan *chan)
363{
364 struct cppi41_channel *c = to_cpp41_chan(chan);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700365 struct cppi41_dd *cdd = c->cdd;
366 int error;
367
368 error = pm_runtime_get_sync(cdd->ddev.dev);
Tony Lindgren740b4be2016-11-11 11:28:52 -0800369 if (error < 0) {
370 pm_runtime_put_noidle(cdd->ddev.dev);
371
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700372 return error;
Tony Lindgren740b4be2016-11-11 11:28:52 -0800373 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200374
375 dma_cookie_init(chan);
376 dma_async_tx_descriptor_init(&c->txd, chan);
377 c->txd.tx_submit = cppi41_tx_submit;
378
379 if (!c->is_tx)
380 cppi_writel(c->q_num, c->gcr_reg + RXHPCRA0);
381
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700382 pm_runtime_mark_last_busy(cdd->ddev.dev);
383 pm_runtime_put_autosuspend(cdd->ddev.dev);
384
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200385 return 0;
386}
387
388static void cppi41_dma_free_chan_resources(struct dma_chan *chan)
389{
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700390 struct cppi41_channel *c = to_cpp41_chan(chan);
391 struct cppi41_dd *cdd = c->cdd;
392 int error;
393
394 error = pm_runtime_get_sync(cdd->ddev.dev);
Tony Lindgren740b4be2016-11-11 11:28:52 -0800395 if (error < 0) {
396 pm_runtime_put_noidle(cdd->ddev.dev);
397
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700398 return;
Tony Lindgren740b4be2016-11-11 11:28:52 -0800399 }
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700400
401 WARN_ON(!list_empty(&cdd->pending));
402
403 pm_runtime_mark_last_busy(cdd->ddev.dev);
404 pm_runtime_put_autosuspend(cdd->ddev.dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200405}
406
407static enum dma_status cppi41_dma_tx_status(struct dma_chan *chan,
408 dma_cookie_t cookie, struct dma_tx_state *txstate)
409{
410 struct cppi41_channel *c = to_cpp41_chan(chan);
411 enum dma_status ret;
412
413 /* lock */
414 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Kouled83c0c2013-10-16 13:36:28 +0530415 if (txstate && ret == DMA_COMPLETE)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200416 txstate->residue = c->residue;
417 /* unlock */
418
419 return ret;
420}
421
422static void push_desc_queue(struct cppi41_channel *c)
423{
424 struct cppi41_dd *cdd = c->cdd;
425 u32 desc_num;
426 u32 desc_phys;
427 u32 reg;
428
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200429 c->residue = 0;
430
431 reg = GCR_CHAN_ENABLE;
432 if (!c->is_tx) {
433 reg |= GCR_STARV_RETRY;
434 reg |= GCR_DESC_TYPE_HOST;
435 reg |= c->q_comp_num;
436 }
437
438 cppi_writel(reg, c->gcr_reg);
439
440 /*
441 * We don't use writel() but __raw_writel() so we have to make sure
442 * that the DMA descriptor in coherent memory made to the main memory
443 * before starting the dma engine.
444 */
445 __iowmb();
Tony Lindgren670fc2a2016-08-19 15:59:39 -0700446
447 desc_phys = lower_32_bits(c->desc_phys);
448 desc_num = (desc_phys - cdd->descs_phys) / sizeof(struct cppi41_desc);
449 WARN_ON(cdd->chan_busy[desc_num]);
450 cdd->chan_busy[desc_num] = c;
451
452 reg = (sizeof(struct cppi41_desc) - 24) / 4;
453 reg |= desc_phys;
454 cppi_writel(reg, cdd->qmgr_mem + QMGR_QUEUE_D(c->q_num));
455}
456
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700457static void pending_desc(struct cppi41_channel *c)
458{
459 struct cppi41_dd *cdd = c->cdd;
460 unsigned long flags;
461
462 spin_lock_irqsave(&cdd->lock, flags);
463 list_add_tail(&c->node, &cdd->pending);
464 spin_unlock_irqrestore(&cdd->lock, flags);
465}
466
Tony Lindgren670fc2a2016-08-19 15:59:39 -0700467static void cppi41_dma_issue_pending(struct dma_chan *chan)
468{
469 struct cppi41_channel *c = to_cpp41_chan(chan);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700470 struct cppi41_dd *cdd = c->cdd;
471 int error;
Tony Lindgren670fc2a2016-08-19 15:59:39 -0700472
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700473 error = pm_runtime_get(cdd->ddev.dev);
Tony Lindgrenf2f6f822016-09-13 10:22:43 -0700474 if ((error != -EINPROGRESS) && error < 0) {
Tony Lindgren740b4be2016-11-11 11:28:52 -0800475 pm_runtime_put_noidle(cdd->ddev.dev);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700476 dev_err(cdd->ddev.dev, "Failed to pm_runtime_get: %i\n",
477 error);
478
479 return;
480 }
481
482 if (likely(pm_runtime_active(cdd->ddev.dev)))
483 push_desc_queue(c);
484 else
485 pending_desc(c);
Tony Lindgren098de422016-11-09 09:47:59 -0700486
487 pm_runtime_mark_last_busy(cdd->ddev.dev);
488 pm_runtime_put_autosuspend(cdd->ddev.dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200489}
490
491static u32 get_host_pd0(u32 length)
492{
493 u32 reg;
494
495 reg = DESC_TYPE_HOST << DESC_TYPE;
496 reg |= length;
497
498 return reg;
499}
500
501static u32 get_host_pd1(struct cppi41_channel *c)
502{
503 u32 reg;
504
505 reg = 0;
506
507 return reg;
508}
509
510static u32 get_host_pd2(struct cppi41_channel *c)
511{
512 u32 reg;
513
514 reg = DESC_TYPE_USB;
515 reg |= c->q_comp_num;
516
517 return reg;
518}
519
520static u32 get_host_pd3(u32 length)
521{
522 u32 reg;
523
524 /* PD3 = packet size */
525 reg = length;
526
527 return reg;
528}
529
530static u32 get_host_pd6(u32 length)
531{
532 u32 reg;
533
534 /* PD6 buffer size */
535 reg = DESC_PD_COMPLETE;
536 reg |= length;
537
538 return reg;
539}
540
541static u32 get_host_pd4_or_7(u32 addr)
542{
543 u32 reg;
544
545 reg = addr;
546
547 return reg;
548}
549
550static u32 get_host_pd5(void)
551{
552 u32 reg;
553
554 reg = 0;
555
556 return reg;
557}
558
559static struct dma_async_tx_descriptor *cppi41_dma_prep_slave_sg(
560 struct dma_chan *chan, struct scatterlist *sgl, unsigned sg_len,
561 enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
562{
563 struct cppi41_channel *c = to_cpp41_chan(chan);
564 struct cppi41_desc *d;
565 struct scatterlist *sg;
566 unsigned int i;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200567
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200568 d = c->desc;
569 for_each_sg(sgl, sg, sg_len, i) {
570 u32 addr;
571 u32 len;
572
573 /* We need to use more than one desc once musb supports sg */
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200574 addr = lower_32_bits(sg_dma_address(sg));
575 len = sg_dma_len(sg);
576
577 d->pd0 = get_host_pd0(len);
578 d->pd1 = get_host_pd1(c);
579 d->pd2 = get_host_pd2(c);
580 d->pd3 = get_host_pd3(len);
581 d->pd4 = get_host_pd4_or_7(addr);
582 d->pd5 = get_host_pd5();
583 d->pd6 = get_host_pd6(len);
584 d->pd7 = get_host_pd4_or_7(addr);
585
586 d++;
587 }
588
589 return &c->txd;
590}
591
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200592static void cppi41_compute_td_desc(struct cppi41_desc *d)
593{
594 d->pd0 = DESC_TYPE_TEARD << DESC_TYPE;
595}
596
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200597static int cppi41_tear_down_chan(struct cppi41_channel *c)
598{
599 struct cppi41_dd *cdd = c->cdd;
600 struct cppi41_desc *td;
601 u32 reg;
602 u32 desc_phys;
603 u32 td_desc_phys;
604
605 td = cdd->cd;
606 td += cdd->first_td_desc;
607
608 td_desc_phys = cdd->descs_phys;
609 td_desc_phys += cdd->first_td_desc * sizeof(struct cppi41_desc);
610
611 if (!c->td_queued) {
612 cppi41_compute_td_desc(td);
613 __iowmb();
614
615 reg = (sizeof(struct cppi41_desc) - 24) / 4;
616 reg |= td_desc_phys;
617 cppi_writel(reg, cdd->qmgr_mem +
618 QMGR_QUEUE_D(cdd->td_queue.submit));
619
620 reg = GCR_CHAN_ENABLE;
621 if (!c->is_tx) {
622 reg |= GCR_STARV_RETRY;
623 reg |= GCR_DESC_TYPE_HOST;
624 reg |= c->q_comp_num;
625 }
626 reg |= GCR_TEARDOWN;
627 cppi_writel(reg, c->gcr_reg);
628 c->td_queued = 1;
Sebastian Andrzej Siewior6f9d7052014-12-03 15:09:49 +0100629 c->td_retry = 500;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200630 }
631
Sebastian Andrzej Siewior1e378a62013-10-22 12:14:05 +0200632 if (!c->td_seen || !c->td_desc_seen) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200633
Sebastian Andrzej Siewior1e378a62013-10-22 12:14:05 +0200634 desc_phys = cppi41_pop_desc(cdd, cdd->td_queue.complete);
635 if (!desc_phys)
636 desc_phys = cppi41_pop_desc(cdd, c->q_comp_num);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200637
Sebastian Andrzej Siewior1e378a62013-10-22 12:14:05 +0200638 if (desc_phys == c->desc_phys) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200639 c->td_desc_seen = 1;
Sebastian Andrzej Siewior1e378a62013-10-22 12:14:05 +0200640
641 } else if (desc_phys == td_desc_phys) {
642 u32 pd0;
643
644 __iormb();
645 pd0 = td->pd0;
646 WARN_ON((pd0 >> DESC_TYPE) != DESC_TYPE_TEARD);
647 WARN_ON(!c->is_tx && !(pd0 & TD_DESC_IS_RX));
648 WARN_ON((pd0 & 0x1f) != c->port_num);
649 c->td_seen = 1;
650 } else if (desc_phys) {
651 WARN_ON_ONCE(1);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200652 }
653 }
654 c->td_retry--;
655 /*
656 * If the TX descriptor / channel is in use, the caller needs to poke
657 * his TD bit multiple times. After that he hardware releases the
658 * transfer descriptor followed by TD descriptor. Waiting seems not to
659 * cause any difference.
660 * RX seems to be thrown out right away. However once the TearDown
661 * descriptor gets through we are done. If we have seens the transfer
662 * descriptor before the TD we fetch it from enqueue, it has to be
663 * there waiting for us.
664 */
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +0100665 if (!c->td_seen && c->td_retry) {
666 udelay(1);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200667 return -EAGAIN;
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +0100668 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200669 WARN_ON(!c->td_retry);
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +0100670
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200671 if (!c->td_desc_seen) {
Daniel Mack706ff622013-10-22 12:14:04 +0200672 desc_phys = cppi41_pop_desc(cdd, c->q_num);
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +0100673 if (!desc_phys)
674 desc_phys = cppi41_pop_desc(cdd, c->q_comp_num);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200675 WARN_ON(!desc_phys);
676 }
677
678 c->td_queued = 0;
679 c->td_seen = 0;
680 c->td_desc_seen = 0;
681 cppi_writel(0, c->gcr_reg);
682 return 0;
683}
684
685static int cppi41_stop_chan(struct dma_chan *chan)
686{
687 struct cppi41_channel *c = to_cpp41_chan(chan);
688 struct cppi41_dd *cdd = c->cdd;
689 u32 desc_num;
690 u32 desc_phys;
691 int ret;
692
George Cherian975faae2014-02-27 10:44:40 +0530693 desc_phys = lower_32_bits(c->desc_phys);
694 desc_num = (desc_phys - cdd->descs_phys) / sizeof(struct cppi41_desc);
695 if (!cdd->chan_busy[desc_num])
696 return 0;
697
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200698 ret = cppi41_tear_down_chan(c);
699 if (ret)
700 return ret;
701
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200702 WARN_ON(!cdd->chan_busy[desc_num]);
703 cdd->chan_busy[desc_num] = NULL;
704
705 return 0;
706}
707
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200708static void cleanup_chans(struct cppi41_dd *cdd)
709{
710 while (!list_empty(&cdd->ddev.channels)) {
711 struct cppi41_channel *cchan;
712
713 cchan = list_first_entry(&cdd->ddev.channels,
714 struct cppi41_channel, chan.device_node);
715 list_del(&cchan->chan.device_node);
716 kfree(cchan);
717 }
718}
719
Daniel Macke327e212013-09-22 16:50:00 +0200720static int cppi41_add_chans(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200721{
722 struct cppi41_channel *cchan;
723 int i;
724 int ret;
725 u32 n_chans;
726
Daniel Macke327e212013-09-22 16:50:00 +0200727 ret = of_property_read_u32(dev->of_node, "#dma-channels",
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200728 &n_chans);
729 if (ret)
730 return ret;
731 /*
732 * The channels can only be used as TX or as RX. So we add twice
733 * that much dma channels because USB can only do RX or TX.
734 */
735 n_chans *= 2;
736
737 for (i = 0; i < n_chans; i++) {
738 cchan = kzalloc(sizeof(*cchan), GFP_KERNEL);
739 if (!cchan)
740 goto err;
741
742 cchan->cdd = cdd;
743 if (i & 1) {
744 cchan->gcr_reg = cdd->ctrl_mem + DMA_TXGCR(i >> 1);
745 cchan->is_tx = 1;
746 } else {
747 cchan->gcr_reg = cdd->ctrl_mem + DMA_RXGCR(i >> 1);
748 cchan->is_tx = 0;
749 }
750 cchan->port_num = i >> 1;
751 cchan->desc = &cdd->cd[i];
752 cchan->desc_phys = cdd->descs_phys;
753 cchan->desc_phys += i * sizeof(struct cppi41_desc);
754 cchan->chan.device = &cdd->ddev;
755 list_add_tail(&cchan->chan.device_node, &cdd->ddev.channels);
756 }
757 cdd->first_td_desc = n_chans;
758
759 return 0;
760err:
761 cleanup_chans(cdd);
762 return -ENOMEM;
763}
764
Daniel Macke327e212013-09-22 16:50:00 +0200765static void purge_descs(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200766{
767 unsigned int mem_decs;
768 int i;
769
770 mem_decs = ALLOC_DECS_NUM * sizeof(struct cppi41_desc);
771
772 for (i = 0; i < DESCS_AREAS; i++) {
773
774 cppi_writel(0, cdd->qmgr_mem + QMGR_MEMBASE(i));
775 cppi_writel(0, cdd->qmgr_mem + QMGR_MEMCTRL(i));
776
Daniel Macke327e212013-09-22 16:50:00 +0200777 dma_free_coherent(dev, mem_decs, cdd->cd,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200778 cdd->descs_phys);
779 }
780}
781
782static void disable_sched(struct cppi41_dd *cdd)
783{
784 cppi_writel(0, cdd->sched_mem + DMA_SCHED_CTRL);
785}
786
Daniel Mackb46ce4d2013-09-22 16:50:01 +0200787static void deinit_cppi41(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200788{
789 disable_sched(cdd);
790
Daniel Macke327e212013-09-22 16:50:00 +0200791 purge_descs(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200792
793 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM0_BASE);
794 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM0_BASE);
Daniel Macke327e212013-09-22 16:50:00 +0200795 dma_free_coherent(dev, QMGR_SCRATCH_SIZE, cdd->qmgr_scratch,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200796 cdd->scratch_phys);
797}
798
Daniel Macke327e212013-09-22 16:50:00 +0200799static int init_descs(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200800{
801 unsigned int desc_size;
802 unsigned int mem_decs;
803 int i;
804 u32 reg;
805 u32 idx;
806
807 BUILD_BUG_ON(sizeof(struct cppi41_desc) &
808 (sizeof(struct cppi41_desc) - 1));
809 BUILD_BUG_ON(sizeof(struct cppi41_desc) < 32);
810 BUILD_BUG_ON(ALLOC_DECS_NUM < 32);
811
812 desc_size = sizeof(struct cppi41_desc);
813 mem_decs = ALLOC_DECS_NUM * desc_size;
814
815 idx = 0;
816 for (i = 0; i < DESCS_AREAS; i++) {
817
818 reg = idx << QMGR_MEMCTRL_IDX_SH;
819 reg |= (ilog2(desc_size) - 5) << QMGR_MEMCTRL_DESC_SH;
820 reg |= ilog2(ALLOC_DECS_NUM) - 5;
821
822 BUILD_BUG_ON(DESCS_AREAS != 1);
Daniel Macke327e212013-09-22 16:50:00 +0200823 cdd->cd = dma_alloc_coherent(dev, mem_decs,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200824 &cdd->descs_phys, GFP_KERNEL);
825 if (!cdd->cd)
826 return -ENOMEM;
827
828 cppi_writel(cdd->descs_phys, cdd->qmgr_mem + QMGR_MEMBASE(i));
829 cppi_writel(reg, cdd->qmgr_mem + QMGR_MEMCTRL(i));
830
831 idx += ALLOC_DECS_NUM;
832 }
833 return 0;
834}
835
836static void init_sched(struct cppi41_dd *cdd)
837{
838 unsigned ch;
839 unsigned word;
840 u32 reg;
841
842 word = 0;
843 cppi_writel(0, cdd->sched_mem + DMA_SCHED_CTRL);
844 for (ch = 0; ch < 15 * 2; ch += 2) {
845
846 reg = SCHED_ENTRY0_CHAN(ch);
847 reg |= SCHED_ENTRY1_CHAN(ch) | SCHED_ENTRY1_IS_RX;
848
849 reg |= SCHED_ENTRY2_CHAN(ch + 1);
850 reg |= SCHED_ENTRY3_CHAN(ch + 1) | SCHED_ENTRY3_IS_RX;
851 cppi_writel(reg, cdd->sched_mem + DMA_SCHED_WORD(word));
852 word++;
853 }
854 reg = 15 * 2 * 2 - 1;
855 reg |= DMA_SCHED_CTRL_EN;
856 cppi_writel(reg, cdd->sched_mem + DMA_SCHED_CTRL);
857}
858
Daniel Macke327e212013-09-22 16:50:00 +0200859static int init_cppi41(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200860{
861 int ret;
862
863 BUILD_BUG_ON(QMGR_SCRATCH_SIZE > ((1 << 14) - 1));
Daniel Macke327e212013-09-22 16:50:00 +0200864 cdd->qmgr_scratch = dma_alloc_coherent(dev, QMGR_SCRATCH_SIZE,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200865 &cdd->scratch_phys, GFP_KERNEL);
866 if (!cdd->qmgr_scratch)
867 return -ENOMEM;
868
869 cppi_writel(cdd->scratch_phys, cdd->qmgr_mem + QMGR_LRAM0_BASE);
870 cppi_writel(QMGR_SCRATCH_SIZE, cdd->qmgr_mem + QMGR_LRAM_SIZE);
871 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM1_BASE);
872
Daniel Macke327e212013-09-22 16:50:00 +0200873 ret = init_descs(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200874 if (ret)
875 goto err_td;
876
877 cppi_writel(cdd->td_queue.submit, cdd->ctrl_mem + DMA_TDFDQ);
878 init_sched(cdd);
879 return 0;
880err_td:
Daniel Mackb46ce4d2013-09-22 16:50:01 +0200881 deinit_cppi41(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200882 return ret;
883}
884
885static struct platform_driver cpp41_dma_driver;
886/*
887 * The param format is:
888 * X Y
889 * X: Port
890 * Y: 0 = RX else TX
891 */
892#define INFO_PORT 0
893#define INFO_IS_TX 1
894
895static bool cpp41_dma_filter_fn(struct dma_chan *chan, void *param)
896{
897 struct cppi41_channel *cchan;
898 struct cppi41_dd *cdd;
899 const struct chan_queues *queues;
900 u32 *num = param;
901
902 if (chan->device->dev->driver != &cpp41_dma_driver.driver)
903 return false;
904
905 cchan = to_cpp41_chan(chan);
906
907 if (cchan->port_num != num[INFO_PORT])
908 return false;
909
910 if (cchan->is_tx && !num[INFO_IS_TX])
911 return false;
912 cdd = cchan->cdd;
913 if (cchan->is_tx)
914 queues = cdd->queues_tx;
915 else
916 queues = cdd->queues_rx;
917
918 BUILD_BUG_ON(ARRAY_SIZE(usb_queues_rx) != ARRAY_SIZE(usb_queues_tx));
919 if (WARN_ON(cchan->port_num > ARRAY_SIZE(usb_queues_rx)))
920 return false;
921
922 cchan->q_num = queues[cchan->port_num].submit;
923 cchan->q_comp_num = queues[cchan->port_num].complete;
924 return true;
925}
926
927static struct of_dma_filter_info cpp41_dma_info = {
928 .filter_fn = cpp41_dma_filter_fn,
929};
930
931static struct dma_chan *cppi41_dma_xlate(struct of_phandle_args *dma_spec,
932 struct of_dma *ofdma)
933{
934 int count = dma_spec->args_count;
935 struct of_dma_filter_info *info = ofdma->of_dma_data;
936
937 if (!info || !info->filter_fn)
938 return NULL;
939
940 if (count != 2)
941 return NULL;
942
943 return dma_request_channel(info->dma_cap, info->filter_fn,
944 &dma_spec->args[0]);
945}
946
947static const struct cppi_glue_infos usb_infos = {
948 .isr = cppi41_irq,
949 .queues_rx = usb_queues_rx,
950 .queues_tx = usb_queues_tx,
951 .td_queue = { .submit = 31, .complete = 0 },
952};
953
954static const struct of_device_id cppi41_dma_ids[] = {
955 { .compatible = "ti,am3359-cppi41", .data = &usb_infos},
956 {},
957};
958MODULE_DEVICE_TABLE(of, cppi41_dma_ids);
959
Daniel Macke327e212013-09-22 16:50:00 +0200960static const struct cppi_glue_infos *get_glue_info(struct device *dev)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200961{
962 const struct of_device_id *of_id;
963
Daniel Macke327e212013-09-22 16:50:00 +0200964 of_id = of_match_node(cppi41_dma_ids, dev->of_node);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200965 if (!of_id)
966 return NULL;
967 return of_id->data;
968}
969
Felipe Balbiffeb13a2015-04-08 11:45:42 -0500970#define CPPI41_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
971 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
972 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
973 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
974
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200975static int cppi41_dma_probe(struct platform_device *pdev)
976{
977 struct cppi41_dd *cdd;
Daniel Mack717d8182013-09-22 16:50:02 +0200978 struct device *dev = &pdev->dev;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200979 const struct cppi_glue_infos *glue_info;
980 int irq;
981 int ret;
982
Daniel Mack717d8182013-09-22 16:50:02 +0200983 glue_info = get_glue_info(dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200984 if (!glue_info)
985 return -EINVAL;
986
Kiran Padwalf0f3b5f2014-09-24 15:53:46 +0530987 cdd = devm_kzalloc(&pdev->dev, sizeof(*cdd), GFP_KERNEL);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200988 if (!cdd)
989 return -ENOMEM;
990
991 dma_cap_set(DMA_SLAVE, cdd->ddev.cap_mask);
992 cdd->ddev.device_alloc_chan_resources = cppi41_dma_alloc_chan_resources;
993 cdd->ddev.device_free_chan_resources = cppi41_dma_free_chan_resources;
994 cdd->ddev.device_tx_status = cppi41_dma_tx_status;
995 cdd->ddev.device_issue_pending = cppi41_dma_issue_pending;
996 cdd->ddev.device_prep_slave_sg = cppi41_dma_prep_slave_sg;
Maxime Ripard3b5a03a2014-11-17 14:42:10 +0100997 cdd->ddev.device_terminate_all = cppi41_stop_chan;
Felipe Balbiffeb13a2015-04-08 11:45:42 -0500998 cdd->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
999 cdd->ddev.src_addr_widths = CPPI41_DMA_BUSWIDTHS;
1000 cdd->ddev.dst_addr_widths = CPPI41_DMA_BUSWIDTHS;
1001 cdd->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Daniel Mack717d8182013-09-22 16:50:02 +02001002 cdd->ddev.dev = dev;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001003 INIT_LIST_HEAD(&cdd->ddev.channels);
1004 cpp41_dma_info.dma_cap = cdd->ddev.cap_mask;
1005
Daniel Mack717d8182013-09-22 16:50:02 +02001006 cdd->usbss_mem = of_iomap(dev->of_node, 0);
1007 cdd->ctrl_mem = of_iomap(dev->of_node, 1);
1008 cdd->sched_mem = of_iomap(dev->of_node, 2);
1009 cdd->qmgr_mem = of_iomap(dev->of_node, 3);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001010 spin_lock_init(&cdd->lock);
1011 INIT_LIST_HEAD(&cdd->pending);
1012
1013 platform_set_drvdata(pdev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001014
1015 if (!cdd->usbss_mem || !cdd->ctrl_mem || !cdd->sched_mem ||
Kiran Padwalf0f3b5f2014-09-24 15:53:46 +05301016 !cdd->qmgr_mem)
1017 return -ENXIO;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001018
Daniel Mack717d8182013-09-22 16:50:02 +02001019 pm_runtime_enable(dev);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001020 pm_runtime_set_autosuspend_delay(dev, 100);
1021 pm_runtime_use_autosuspend(dev);
Daniel Mack717d8182013-09-22 16:50:02 +02001022 ret = pm_runtime_get_sync(dev);
Sebastian Andrzej Siewiorcbf1e562013-10-22 12:14:06 +02001023 if (ret < 0)
Sebastian Andrzej Siewiord6aafa22013-08-20 18:35:53 +02001024 goto err_get_sync;
1025
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001026 cdd->queues_rx = glue_info->queues_rx;
1027 cdd->queues_tx = glue_info->queues_tx;
1028 cdd->td_queue = glue_info->td_queue;
1029
Daniel Mack717d8182013-09-22 16:50:02 +02001030 ret = init_cppi41(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001031 if (ret)
1032 goto err_init_cppi;
1033
Daniel Mack717d8182013-09-22 16:50:02 +02001034 ret = cppi41_add_chans(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001035 if (ret)
1036 goto err_chans;
1037
Daniel Mack717d8182013-09-22 16:50:02 +02001038 irq = irq_of_parse_and_map(dev->of_node, 0);
Julia Lawallf3b77722013-12-29 23:47:23 +01001039 if (!irq) {
1040 ret = -EINVAL;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001041 goto err_irq;
Julia Lawallf3b77722013-12-29 23:47:23 +01001042 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001043
1044 cppi_writel(USBSS_IRQ_PD_COMP, cdd->usbss_mem + USBSS_IRQ_ENABLER);
1045
Kiran Padwalf0f3b5f2014-09-24 15:53:46 +05301046 ret = devm_request_irq(&pdev->dev, irq, glue_info->isr, IRQF_SHARED,
Daniel Mack717d8182013-09-22 16:50:02 +02001047 dev_name(dev), cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001048 if (ret)
1049 goto err_irq;
1050 cdd->irq = irq;
1051
1052 ret = dma_async_device_register(&cdd->ddev);
1053 if (ret)
1054 goto err_dma_reg;
1055
Daniel Mack717d8182013-09-22 16:50:02 +02001056 ret = of_dma_controller_register(dev->of_node,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001057 cppi41_dma_xlate, &cpp41_dma_info);
1058 if (ret)
1059 goto err_of;
1060
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001061 pm_runtime_mark_last_busy(dev);
1062 pm_runtime_put_autosuspend(dev);
1063
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001064 return 0;
1065err_of:
1066 dma_async_device_unregister(&cdd->ddev);
1067err_dma_reg:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001068err_irq:
1069 cppi_writel(0, cdd->usbss_mem + USBSS_IRQ_CLEARR);
1070 cleanup_chans(cdd);
1071err_chans:
Daniel Mack717d8182013-09-22 16:50:02 +02001072 deinit_cppi41(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001073err_init_cppi:
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001074 pm_runtime_dont_use_autosuspend(dev);
1075 pm_runtime_put_sync(dev);
Sebastian Andrzej Siewiord6aafa22013-08-20 18:35:53 +02001076err_get_sync:
Daniel Mack717d8182013-09-22 16:50:02 +02001077 pm_runtime_disable(dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001078 iounmap(cdd->usbss_mem);
1079 iounmap(cdd->ctrl_mem);
1080 iounmap(cdd->sched_mem);
1081 iounmap(cdd->qmgr_mem);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001082 return ret;
1083}
1084
1085static int cppi41_dma_remove(struct platform_device *pdev)
1086{
1087 struct cppi41_dd *cdd = platform_get_drvdata(pdev);
Tony Lindgren12f59082016-11-09 09:47:58 -07001088 int error;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001089
Tony Lindgren12f59082016-11-09 09:47:58 -07001090 error = pm_runtime_get_sync(&pdev->dev);
1091 if (error < 0)
1092 dev_err(&pdev->dev, "%s could not pm_runtime_get: %i\n",
1093 __func__, error);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001094 of_dma_controller_free(pdev->dev.of_node);
1095 dma_async_device_unregister(&cdd->ddev);
1096
1097 cppi_writel(0, cdd->usbss_mem + USBSS_IRQ_CLEARR);
Kiran Padwalf0f3b5f2014-09-24 15:53:46 +05301098 devm_free_irq(&pdev->dev, cdd->irq, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001099 cleanup_chans(cdd);
Daniel Mackb46ce4d2013-09-22 16:50:01 +02001100 deinit_cppi41(&pdev->dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001101 iounmap(cdd->usbss_mem);
1102 iounmap(cdd->ctrl_mem);
1103 iounmap(cdd->sched_mem);
1104 iounmap(cdd->qmgr_mem);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001105 pm_runtime_dont_use_autosuspend(&pdev->dev);
1106 pm_runtime_put_sync(&pdev->dev);
Sebastian Andrzej Siewiord6aafa22013-08-20 18:35:53 +02001107 pm_runtime_disable(&pdev->dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001108 return 0;
1109}
1110
Arnd Bergmann522ef612016-09-06 15:20:05 +02001111static int __maybe_unused cppi41_suspend(struct device *dev)
Daniel Mackf97b98d2013-09-22 16:50:04 +02001112{
1113 struct cppi41_dd *cdd = dev_get_drvdata(dev);
1114
Daniel Mackf8964962013-10-22 12:14:03 +02001115 cdd->dma_tdfdq = cppi_readl(cdd->ctrl_mem + DMA_TDFDQ);
Daniel Mackf97b98d2013-09-22 16:50:04 +02001116 cppi_writel(0, cdd->usbss_mem + USBSS_IRQ_CLEARR);
1117 disable_sched(cdd);
1118
1119 return 0;
1120}
1121
Arnd Bergmann522ef612016-09-06 15:20:05 +02001122static int __maybe_unused cppi41_resume(struct device *dev)
Daniel Mackf97b98d2013-09-22 16:50:04 +02001123{
1124 struct cppi41_dd *cdd = dev_get_drvdata(dev);
Daniel Mackf8964962013-10-22 12:14:03 +02001125 struct cppi41_channel *c;
Daniel Mackf97b98d2013-09-22 16:50:04 +02001126 int i;
1127
1128 for (i = 0; i < DESCS_AREAS; i++)
1129 cppi_writel(cdd->descs_phys, cdd->qmgr_mem + QMGR_MEMBASE(i));
1130
Daniel Mackf8964962013-10-22 12:14:03 +02001131 list_for_each_entry(c, &cdd->ddev.channels, chan.device_node)
1132 if (!c->is_tx)
1133 cppi_writel(c->q_num, c->gcr_reg + RXHPCRA0);
1134
Daniel Mackf97b98d2013-09-22 16:50:04 +02001135 init_sched(cdd);
Daniel Mackf8964962013-10-22 12:14:03 +02001136
1137 cppi_writel(cdd->dma_tdfdq, cdd->ctrl_mem + DMA_TDFDQ);
1138 cppi_writel(cdd->scratch_phys, cdd->qmgr_mem + QMGR_LRAM0_BASE);
1139 cppi_writel(QMGR_SCRATCH_SIZE, cdd->qmgr_mem + QMGR_LRAM_SIZE);
1140 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM1_BASE);
1141
Daniel Mackf97b98d2013-09-22 16:50:04 +02001142 cppi_writel(USBSS_IRQ_PD_COMP, cdd->usbss_mem + USBSS_IRQ_ENABLER);
1143
1144 return 0;
1145}
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001146
Arnd Bergmann522ef612016-09-06 15:20:05 +02001147static int __maybe_unused cppi41_runtime_suspend(struct device *dev)
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001148{
1149 struct cppi41_dd *cdd = dev_get_drvdata(dev);
1150
1151 WARN_ON(!list_empty(&cdd->pending));
1152
1153 return 0;
1154}
1155
Arnd Bergmann522ef612016-09-06 15:20:05 +02001156static int __maybe_unused cppi41_runtime_resume(struct device *dev)
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001157{
1158 struct cppi41_dd *cdd = dev_get_drvdata(dev);
1159 struct cppi41_channel *c, *_c;
1160 unsigned long flags;
1161
1162 spin_lock_irqsave(&cdd->lock, flags);
1163 list_for_each_entry_safe(c, _c, &cdd->pending, node) {
1164 push_desc_queue(c);
1165 list_del(&c->node);
1166 }
1167 spin_unlock_irqrestore(&cdd->lock, flags);
1168
1169 return 0;
1170}
Daniel Mackf97b98d2013-09-22 16:50:04 +02001171
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001172static const struct dev_pm_ops cppi41_pm_ops = {
1173 SET_LATE_SYSTEM_SLEEP_PM_OPS(cppi41_suspend, cppi41_resume)
1174 SET_RUNTIME_PM_OPS(cppi41_runtime_suspend,
1175 cppi41_runtime_resume,
1176 NULL)
1177};
Daniel Mackf97b98d2013-09-22 16:50:04 +02001178
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001179static struct platform_driver cpp41_dma_driver = {
1180 .probe = cppi41_dma_probe,
1181 .remove = cppi41_dma_remove,
1182 .driver = {
1183 .name = "cppi41-dma-engine",
Daniel Mackf97b98d2013-09-22 16:50:04 +02001184 .pm = &cppi41_pm_ops,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001185 .of_match_table = of_match_ptr(cppi41_dma_ids),
1186 },
1187};
1188
1189module_platform_driver(cpp41_dma_driver);
1190MODULE_LICENSE("GPL");
1191MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");