blob: 676228ff71dea9c0818bca9125a652fc5ef56e06 [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
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020071#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
Daniel Mack13bbfb52014-05-26 14:52:34 +020081/* Packet Descriptor */
82#define PD2_ZERO_LENGTH (1 << 19)
83
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020084struct cppi41_channel {
85 struct dma_chan chan;
86 struct dma_async_tx_descriptor txd;
87 struct cppi41_dd *cdd;
88 struct cppi41_desc *desc;
89 dma_addr_t desc_phys;
90 void __iomem *gcr_reg;
91 int is_tx;
92 u32 residue;
93
94 unsigned int q_num;
95 unsigned int q_comp_num;
96 unsigned int port_num;
97
98 unsigned td_retry;
99 unsigned td_queued:1;
100 unsigned td_seen:1;
101 unsigned td_desc_seen:1;
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700102
103 struct list_head node; /* Node for pending list */
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200104};
105
106struct cppi41_desc {
107 u32 pd0;
108 u32 pd1;
109 u32 pd2;
110 u32 pd3;
111 u32 pd4;
112 u32 pd5;
113 u32 pd6;
114 u32 pd7;
115} __aligned(32);
116
117struct chan_queues {
118 u16 submit;
119 u16 complete;
120};
121
122struct cppi41_dd {
123 struct dma_device ddev;
124
125 void *qmgr_scratch;
126 dma_addr_t scratch_phys;
127
128 struct cppi41_desc *cd;
129 dma_addr_t descs_phys;
130 u32 first_td_desc;
131 struct cppi41_channel *chan_busy[ALLOC_DECS_NUM];
132
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200133 void __iomem *ctrl_mem;
134 void __iomem *sched_mem;
135 void __iomem *qmgr_mem;
136 unsigned int irq;
137 const struct chan_queues *queues_rx;
138 const struct chan_queues *queues_tx;
139 struct chan_queues td_queue;
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100140 u16 first_completion_queue;
141 u16 qmgr_num_pend;
Daniel Mackf8964962013-10-22 12:14:03 +0200142
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700143 struct list_head pending; /* Pending queued transfers */
144 spinlock_t lock; /* Lock for pending list */
145
Daniel Mackf8964962013-10-22 12:14:03 +0200146 /* context for suspend/resume */
147 unsigned int dma_tdfdq;
Tony Lindgren362f4562017-01-19 08:49:08 -0800148
149 bool is_suspended;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200150};
151
Alexandre Bailone1f40bf2017-02-15 14:56:33 +0100152static struct chan_queues am335x_usb_queues_tx[] = {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200153 /* USB0 ENDP 1 */
154 [ 0] = { .submit = 32, .complete = 93},
155 [ 1] = { .submit = 34, .complete = 94},
156 [ 2] = { .submit = 36, .complete = 95},
157 [ 3] = { .submit = 38, .complete = 96},
158 [ 4] = { .submit = 40, .complete = 97},
159 [ 5] = { .submit = 42, .complete = 98},
160 [ 6] = { .submit = 44, .complete = 99},
161 [ 7] = { .submit = 46, .complete = 100},
162 [ 8] = { .submit = 48, .complete = 101},
163 [ 9] = { .submit = 50, .complete = 102},
164 [10] = { .submit = 52, .complete = 103},
165 [11] = { .submit = 54, .complete = 104},
166 [12] = { .submit = 56, .complete = 105},
167 [13] = { .submit = 58, .complete = 106},
168 [14] = { .submit = 60, .complete = 107},
169
170 /* USB1 ENDP1 */
171 [15] = { .submit = 62, .complete = 125},
172 [16] = { .submit = 64, .complete = 126},
173 [17] = { .submit = 66, .complete = 127},
174 [18] = { .submit = 68, .complete = 128},
175 [19] = { .submit = 70, .complete = 129},
176 [20] = { .submit = 72, .complete = 130},
177 [21] = { .submit = 74, .complete = 131},
178 [22] = { .submit = 76, .complete = 132},
179 [23] = { .submit = 78, .complete = 133},
180 [24] = { .submit = 80, .complete = 134},
181 [25] = { .submit = 82, .complete = 135},
182 [26] = { .submit = 84, .complete = 136},
183 [27] = { .submit = 86, .complete = 137},
184 [28] = { .submit = 88, .complete = 138},
185 [29] = { .submit = 90, .complete = 139},
186};
187
Alexandre Bailone1f40bf2017-02-15 14:56:33 +0100188static const struct chan_queues am335x_usb_queues_rx[] = {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200189 /* USB0 ENDP 1 */
190 [ 0] = { .submit = 1, .complete = 109},
191 [ 1] = { .submit = 2, .complete = 110},
192 [ 2] = { .submit = 3, .complete = 111},
193 [ 3] = { .submit = 4, .complete = 112},
194 [ 4] = { .submit = 5, .complete = 113},
195 [ 5] = { .submit = 6, .complete = 114},
196 [ 6] = { .submit = 7, .complete = 115},
197 [ 7] = { .submit = 8, .complete = 116},
198 [ 8] = { .submit = 9, .complete = 117},
199 [ 9] = { .submit = 10, .complete = 118},
200 [10] = { .submit = 11, .complete = 119},
201 [11] = { .submit = 12, .complete = 120},
202 [12] = { .submit = 13, .complete = 121},
203 [13] = { .submit = 14, .complete = 122},
204 [14] = { .submit = 15, .complete = 123},
205
206 /* USB1 ENDP 1 */
207 [15] = { .submit = 16, .complete = 141},
208 [16] = { .submit = 17, .complete = 142},
209 [17] = { .submit = 18, .complete = 143},
210 [18] = { .submit = 19, .complete = 144},
211 [19] = { .submit = 20, .complete = 145},
212 [20] = { .submit = 21, .complete = 146},
213 [21] = { .submit = 22, .complete = 147},
214 [22] = { .submit = 23, .complete = 148},
215 [23] = { .submit = 24, .complete = 149},
216 [24] = { .submit = 25, .complete = 150},
217 [25] = { .submit = 26, .complete = 151},
218 [26] = { .submit = 27, .complete = 152},
219 [27] = { .submit = 28, .complete = 153},
220 [28] = { .submit = 29, .complete = 154},
221 [29] = { .submit = 30, .complete = 155},
222};
223
224struct cppi_glue_infos {
225 irqreturn_t (*isr)(int irq, void *data);
226 const struct chan_queues *queues_rx;
227 const struct chan_queues *queues_tx;
228 struct chan_queues td_queue;
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100229 u16 first_completion_queue;
230 u16 qmgr_num_pend;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200231};
232
233static struct cppi41_channel *to_cpp41_chan(struct dma_chan *c)
234{
235 return container_of(c, struct cppi41_channel, chan);
236}
237
238static struct cppi41_channel *desc_to_chan(struct cppi41_dd *cdd, u32 desc)
239{
240 struct cppi41_channel *c;
241 u32 descs_size;
242 u32 desc_num;
243
244 descs_size = sizeof(struct cppi41_desc) * ALLOC_DECS_NUM;
245
246 if (!((desc >= cdd->descs_phys) &&
247 (desc < (cdd->descs_phys + descs_size)))) {
248 return NULL;
249 }
250
251 desc_num = (desc - cdd->descs_phys) / sizeof(struct cppi41_desc);
Dan Carpenter2d17f7f2013-08-28 13:48:44 +0300252 BUG_ON(desc_num >= ALLOC_DECS_NUM);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200253 c = cdd->chan_busy[desc_num];
254 cdd->chan_busy[desc_num] = NULL;
Tony Lindgrenae4a3e022017-01-19 08:49:07 -0800255
256 /* Usecount for chan_busy[], paired with push_desc_queue() */
257 pm_runtime_put(cdd->ddev.dev);
258
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200259 return c;
260}
261
262static void cppi_writel(u32 val, void *__iomem *mem)
263{
264 __raw_writel(val, mem);
265}
266
267static u32 cppi_readl(void *__iomem *mem)
268{
269 return __raw_readl(mem);
270}
271
272static u32 pd_trans_len(u32 val)
273{
274 return val & ((1 << (DESC_LENGTH_BITS_NUM + 1)) - 1);
275}
276
Daniel Mack706ff622013-10-22 12:14:04 +0200277static u32 cppi41_pop_desc(struct cppi41_dd *cdd, unsigned queue_num)
278{
279 u32 desc;
280
281 desc = cppi_readl(cdd->qmgr_mem + QMGR_QUEUE_D(queue_num));
282 desc &= ~0x1f;
283 return desc;
284}
285
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200286static irqreturn_t cppi41_irq(int irq, void *data)
287{
288 struct cppi41_dd *cdd = data;
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100289 u16 first_completion_queue = cdd->first_completion_queue;
290 u16 qmgr_num_pend = cdd->qmgr_num_pend;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200291 struct cppi41_channel *c;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200292 int i;
293
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100294 for (i = QMGR_PENDING_SLOT_Q(first_completion_queue); i < qmgr_num_pend;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200295 i++) {
296 u32 val;
297 u32 q_num;
298
299 val = cppi_readl(cdd->qmgr_mem + QMGR_PEND(i));
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100300 if (i == QMGR_PENDING_SLOT_Q(first_completion_queue) && val) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200301 u32 mask;
302 /* set corresponding bit for completetion Q 93 */
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100303 mask = 1 << QMGR_PENDING_BIT_Q(first_completion_queue);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200304 /* not set all bits for queues less than Q 93 */
305 mask--;
306 /* now invert and keep only Q 93+ set */
307 val &= ~mask;
308 }
309
310 if (val)
311 __iormb();
312
313 while (val) {
Daniel Mack13bbfb52014-05-26 14:52:34 +0200314 u32 desc, len;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200315
Tony Lindgren6610d0e2017-01-20 12:07:53 -0800316 /*
317 * This should never trigger, see the comments in
318 * push_desc_queue()
319 */
320 WARN_ON(cdd->is_suspended);
Tony Lindgren098de422016-11-09 09:47:59 -0700321
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200322 q_num = __fls(val);
323 val &= ~(1 << q_num);
324 q_num += 32 * i;
Daniel Mack706ff622013-10-22 12:14:04 +0200325 desc = cppi41_pop_desc(cdd, q_num);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200326 c = desc_to_chan(cdd, desc);
327 if (WARN_ON(!c)) {
328 pr_err("%s() q %d desc %08x\n", __func__,
329 q_num, desc);
330 continue;
331 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200332
Daniel Mack13bbfb52014-05-26 14:52:34 +0200333 if (c->desc->pd2 & PD2_ZERO_LENGTH)
334 len = 0;
335 else
336 len = pd_trans_len(c->desc->pd0);
337
338 c->residue = pd_trans_len(c->desc->pd6) - len;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200339 dma_cookie_complete(&c->txd);
Dave Jiangb310a612016-07-20 13:10:54 -0700340 dmaengine_desc_get_callback_invoke(&c->txd, NULL);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200341 }
342 }
343 return IRQ_HANDLED;
344}
345
346static dma_cookie_t cppi41_tx_submit(struct dma_async_tx_descriptor *tx)
347{
348 dma_cookie_t cookie;
349
350 cookie = dma_cookie_assign(tx);
351
352 return cookie;
353}
354
355static int cppi41_dma_alloc_chan_resources(struct dma_chan *chan)
356{
357 struct cppi41_channel *c = to_cpp41_chan(chan);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700358 struct cppi41_dd *cdd = c->cdd;
359 int error;
360
361 error = pm_runtime_get_sync(cdd->ddev.dev);
Tony Lindgren740b4be2016-11-11 11:28:52 -0800362 if (error < 0) {
Tony Lindgrend5afc1b2016-11-16 10:24:15 -0800363 dev_err(cdd->ddev.dev, "%s pm runtime get: %i\n",
364 __func__, error);
Tony Lindgren740b4be2016-11-11 11:28:52 -0800365 pm_runtime_put_noidle(cdd->ddev.dev);
366
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700367 return error;
Tony Lindgren740b4be2016-11-11 11:28:52 -0800368 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200369
370 dma_cookie_init(chan);
371 dma_async_tx_descriptor_init(&c->txd, chan);
372 c->txd.tx_submit = cppi41_tx_submit;
373
374 if (!c->is_tx)
375 cppi_writel(c->q_num, c->gcr_reg + RXHPCRA0);
376
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700377 pm_runtime_mark_last_busy(cdd->ddev.dev);
378 pm_runtime_put_autosuspend(cdd->ddev.dev);
379
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200380 return 0;
381}
382
383static void cppi41_dma_free_chan_resources(struct dma_chan *chan)
384{
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700385 struct cppi41_channel *c = to_cpp41_chan(chan);
386 struct cppi41_dd *cdd = c->cdd;
387 int error;
388
389 error = pm_runtime_get_sync(cdd->ddev.dev);
Tony Lindgren740b4be2016-11-11 11:28:52 -0800390 if (error < 0) {
391 pm_runtime_put_noidle(cdd->ddev.dev);
392
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700393 return;
Tony Lindgren740b4be2016-11-11 11:28:52 -0800394 }
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700395
396 WARN_ON(!list_empty(&cdd->pending));
397
398 pm_runtime_mark_last_busy(cdd->ddev.dev);
399 pm_runtime_put_autosuspend(cdd->ddev.dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200400}
401
402static enum dma_status cppi41_dma_tx_status(struct dma_chan *chan,
403 dma_cookie_t cookie, struct dma_tx_state *txstate)
404{
405 struct cppi41_channel *c = to_cpp41_chan(chan);
406 enum dma_status ret;
407
408 /* lock */
409 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Kouled83c0c2013-10-16 13:36:28 +0530410 if (txstate && ret == DMA_COMPLETE)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200411 txstate->residue = c->residue;
412 /* unlock */
413
414 return ret;
415}
416
417static void push_desc_queue(struct cppi41_channel *c)
418{
419 struct cppi41_dd *cdd = c->cdd;
420 u32 desc_num;
421 u32 desc_phys;
422 u32 reg;
423
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200424 c->residue = 0;
425
426 reg = GCR_CHAN_ENABLE;
427 if (!c->is_tx) {
428 reg |= GCR_STARV_RETRY;
429 reg |= GCR_DESC_TYPE_HOST;
430 reg |= c->q_comp_num;
431 }
432
433 cppi_writel(reg, c->gcr_reg);
434
435 /*
436 * We don't use writel() but __raw_writel() so we have to make sure
437 * that the DMA descriptor in coherent memory made to the main memory
438 * before starting the dma engine.
439 */
440 __iowmb();
Tony Lindgren670fc2a2016-08-19 15:59:39 -0700441
Tony Lindgrenae4a3e022017-01-19 08:49:07 -0800442 /*
443 * DMA transfers can take at least 200ms to complete with USB mass
444 * storage connected. To prevent autosuspend timeouts, we must use
445 * pm_runtime_get/put() when chan_busy[] is modified. This will get
446 * cleared in desc_to_chan() or cppi41_stop_chan() depending on the
447 * outcome of the transfer.
448 */
449 pm_runtime_get(cdd->ddev.dev);
450
Tony Lindgren670fc2a2016-08-19 15:59:39 -0700451 desc_phys = lower_32_bits(c->desc_phys);
452 desc_num = (desc_phys - cdd->descs_phys) / sizeof(struct cppi41_desc);
453 WARN_ON(cdd->chan_busy[desc_num]);
454 cdd->chan_busy[desc_num] = c;
455
456 reg = (sizeof(struct cppi41_desc) - 24) / 4;
457 reg |= desc_phys;
458 cppi_writel(reg, cdd->qmgr_mem + QMGR_QUEUE_D(c->q_num));
459}
460
Tony Lindgren362f4562017-01-19 08:49:08 -0800461/*
462 * Caller must hold cdd->lock to prevent push_desc_queue()
463 * getting called out of order. We have both cppi41_dma_issue_pending()
464 * and cppi41_runtime_resume() call this function.
465 */
466static void cppi41_run_queue(struct cppi41_dd *cdd)
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700467{
Tony Lindgren362f4562017-01-19 08:49:08 -0800468 struct cppi41_channel *c, *_c;
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700469
Tony Lindgren362f4562017-01-19 08:49:08 -0800470 list_for_each_entry_safe(c, _c, &cdd->pending, node) {
471 push_desc_queue(c);
472 list_del(&c->node);
473 }
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700474}
475
Tony Lindgren670fc2a2016-08-19 15:59:39 -0700476static void cppi41_dma_issue_pending(struct dma_chan *chan)
477{
478 struct cppi41_channel *c = to_cpp41_chan(chan);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700479 struct cppi41_dd *cdd = c->cdd;
Tony Lindgren362f4562017-01-19 08:49:08 -0800480 unsigned long flags;
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700481 int error;
Tony Lindgren670fc2a2016-08-19 15:59:39 -0700482
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700483 error = pm_runtime_get(cdd->ddev.dev);
Tony Lindgrenf2f6f822016-09-13 10:22:43 -0700484 if ((error != -EINPROGRESS) && error < 0) {
Tony Lindgren740b4be2016-11-11 11:28:52 -0800485 pm_runtime_put_noidle(cdd->ddev.dev);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700486 dev_err(cdd->ddev.dev, "Failed to pm_runtime_get: %i\n",
487 error);
488
489 return;
490 }
491
Tony Lindgren362f4562017-01-19 08:49:08 -0800492 spin_lock_irqsave(&cdd->lock, flags);
493 list_add_tail(&c->node, &cdd->pending);
494 if (!cdd->is_suspended)
495 cppi41_run_queue(cdd);
496 spin_unlock_irqrestore(&cdd->lock, flags);
Tony Lindgren098de422016-11-09 09:47:59 -0700497
498 pm_runtime_mark_last_busy(cdd->ddev.dev);
499 pm_runtime_put_autosuspend(cdd->ddev.dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200500}
501
502static u32 get_host_pd0(u32 length)
503{
504 u32 reg;
505
506 reg = DESC_TYPE_HOST << DESC_TYPE;
507 reg |= length;
508
509 return reg;
510}
511
512static u32 get_host_pd1(struct cppi41_channel *c)
513{
514 u32 reg;
515
516 reg = 0;
517
518 return reg;
519}
520
521static u32 get_host_pd2(struct cppi41_channel *c)
522{
523 u32 reg;
524
525 reg = DESC_TYPE_USB;
526 reg |= c->q_comp_num;
527
528 return reg;
529}
530
531static u32 get_host_pd3(u32 length)
532{
533 u32 reg;
534
535 /* PD3 = packet size */
536 reg = length;
537
538 return reg;
539}
540
541static u32 get_host_pd6(u32 length)
542{
543 u32 reg;
544
545 /* PD6 buffer size */
546 reg = DESC_PD_COMPLETE;
547 reg |= length;
548
549 return reg;
550}
551
552static u32 get_host_pd4_or_7(u32 addr)
553{
554 u32 reg;
555
556 reg = addr;
557
558 return reg;
559}
560
561static u32 get_host_pd5(void)
562{
563 u32 reg;
564
565 reg = 0;
566
567 return reg;
568}
569
570static struct dma_async_tx_descriptor *cppi41_dma_prep_slave_sg(
571 struct dma_chan *chan, struct scatterlist *sgl, unsigned sg_len,
572 enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
573{
574 struct cppi41_channel *c = to_cpp41_chan(chan);
575 struct cppi41_desc *d;
576 struct scatterlist *sg;
577 unsigned int i;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200578
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200579 d = c->desc;
580 for_each_sg(sgl, sg, sg_len, i) {
581 u32 addr;
582 u32 len;
583
584 /* We need to use more than one desc once musb supports sg */
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200585 addr = lower_32_bits(sg_dma_address(sg));
586 len = sg_dma_len(sg);
587
588 d->pd0 = get_host_pd0(len);
589 d->pd1 = get_host_pd1(c);
590 d->pd2 = get_host_pd2(c);
591 d->pd3 = get_host_pd3(len);
592 d->pd4 = get_host_pd4_or_7(addr);
593 d->pd5 = get_host_pd5();
594 d->pd6 = get_host_pd6(len);
595 d->pd7 = get_host_pd4_or_7(addr);
596
597 d++;
598 }
599
600 return &c->txd;
601}
602
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200603static void cppi41_compute_td_desc(struct cppi41_desc *d)
604{
605 d->pd0 = DESC_TYPE_TEARD << DESC_TYPE;
606}
607
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200608static int cppi41_tear_down_chan(struct cppi41_channel *c)
609{
Alexandre Bailon25534822017-02-06 22:53:56 -0600610 struct dmaengine_result abort_result;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200611 struct cppi41_dd *cdd = c->cdd;
612 struct cppi41_desc *td;
613 u32 reg;
614 u32 desc_phys;
615 u32 td_desc_phys;
616
617 td = cdd->cd;
618 td += cdd->first_td_desc;
619
620 td_desc_phys = cdd->descs_phys;
621 td_desc_phys += cdd->first_td_desc * sizeof(struct cppi41_desc);
622
623 if (!c->td_queued) {
624 cppi41_compute_td_desc(td);
625 __iowmb();
626
627 reg = (sizeof(struct cppi41_desc) - 24) / 4;
628 reg |= td_desc_phys;
629 cppi_writel(reg, cdd->qmgr_mem +
630 QMGR_QUEUE_D(cdd->td_queue.submit));
631
632 reg = GCR_CHAN_ENABLE;
633 if (!c->is_tx) {
634 reg |= GCR_STARV_RETRY;
635 reg |= GCR_DESC_TYPE_HOST;
636 reg |= c->q_comp_num;
637 }
638 reg |= GCR_TEARDOWN;
639 cppi_writel(reg, c->gcr_reg);
640 c->td_queued = 1;
Sebastian Andrzej Siewior6f9d7052014-12-03 15:09:49 +0100641 c->td_retry = 500;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200642 }
643
Sebastian Andrzej Siewior1e378a62013-10-22 12:14:05 +0200644 if (!c->td_seen || !c->td_desc_seen) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200645
Sebastian Andrzej Siewior1e378a62013-10-22 12:14:05 +0200646 desc_phys = cppi41_pop_desc(cdd, cdd->td_queue.complete);
647 if (!desc_phys)
648 desc_phys = cppi41_pop_desc(cdd, c->q_comp_num);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200649
Sebastian Andrzej Siewior1e378a62013-10-22 12:14:05 +0200650 if (desc_phys == c->desc_phys) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200651 c->td_desc_seen = 1;
Sebastian Andrzej Siewior1e378a62013-10-22 12:14:05 +0200652
653 } else if (desc_phys == td_desc_phys) {
654 u32 pd0;
655
656 __iormb();
657 pd0 = td->pd0;
658 WARN_ON((pd0 >> DESC_TYPE) != DESC_TYPE_TEARD);
659 WARN_ON(!c->is_tx && !(pd0 & TD_DESC_IS_RX));
660 WARN_ON((pd0 & 0x1f) != c->port_num);
661 c->td_seen = 1;
662 } else if (desc_phys) {
663 WARN_ON_ONCE(1);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200664 }
665 }
666 c->td_retry--;
667 /*
668 * If the TX descriptor / channel is in use, the caller needs to poke
669 * his TD bit multiple times. After that he hardware releases the
670 * transfer descriptor followed by TD descriptor. Waiting seems not to
671 * cause any difference.
672 * RX seems to be thrown out right away. However once the TearDown
673 * descriptor gets through we are done. If we have seens the transfer
674 * descriptor before the TD we fetch it from enqueue, it has to be
675 * there waiting for us.
676 */
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +0100677 if (!c->td_seen && c->td_retry) {
678 udelay(1);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200679 return -EAGAIN;
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +0100680 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200681 WARN_ON(!c->td_retry);
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +0100682
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200683 if (!c->td_desc_seen) {
Daniel Mack706ff622013-10-22 12:14:04 +0200684 desc_phys = cppi41_pop_desc(cdd, c->q_num);
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +0100685 if (!desc_phys)
686 desc_phys = cppi41_pop_desc(cdd, c->q_comp_num);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200687 WARN_ON(!desc_phys);
688 }
689
690 c->td_queued = 0;
691 c->td_seen = 0;
692 c->td_desc_seen = 0;
693 cppi_writel(0, c->gcr_reg);
Alexandre Bailon25534822017-02-06 22:53:56 -0600694
695 /* Invoke the callback to do the necessary clean-up */
696 abort_result.result = DMA_TRANS_ABORTED;
697 dma_cookie_complete(&c->txd);
698 dmaengine_desc_get_callback_invoke(&c->txd, &abort_result);
699
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200700 return 0;
701}
702
703static int cppi41_stop_chan(struct dma_chan *chan)
704{
705 struct cppi41_channel *c = to_cpp41_chan(chan);
706 struct cppi41_dd *cdd = c->cdd;
707 u32 desc_num;
708 u32 desc_phys;
709 int ret;
710
George Cherian975faae2014-02-27 10:44:40 +0530711 desc_phys = lower_32_bits(c->desc_phys);
712 desc_num = (desc_phys - cdd->descs_phys) / sizeof(struct cppi41_desc);
713 if (!cdd->chan_busy[desc_num])
714 return 0;
715
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200716 ret = cppi41_tear_down_chan(c);
717 if (ret)
718 return ret;
719
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200720 WARN_ON(!cdd->chan_busy[desc_num]);
721 cdd->chan_busy[desc_num] = NULL;
722
Tony Lindgrenae4a3e022017-01-19 08:49:07 -0800723 /* Usecount for chan_busy[], paired with push_desc_queue() */
724 pm_runtime_put(cdd->ddev.dev);
725
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200726 return 0;
727}
728
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200729static void cleanup_chans(struct cppi41_dd *cdd)
730{
731 while (!list_empty(&cdd->ddev.channels)) {
732 struct cppi41_channel *cchan;
733
734 cchan = list_first_entry(&cdd->ddev.channels,
735 struct cppi41_channel, chan.device_node);
736 list_del(&cchan->chan.device_node);
737 kfree(cchan);
738 }
739}
740
Daniel Macke327e212013-09-22 16:50:00 +0200741static int cppi41_add_chans(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200742{
743 struct cppi41_channel *cchan;
744 int i;
745 int ret;
746 u32 n_chans;
747
Daniel Macke327e212013-09-22 16:50:00 +0200748 ret = of_property_read_u32(dev->of_node, "#dma-channels",
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200749 &n_chans);
750 if (ret)
751 return ret;
752 /*
753 * The channels can only be used as TX or as RX. So we add twice
754 * that much dma channels because USB can only do RX or TX.
755 */
756 n_chans *= 2;
757
758 for (i = 0; i < n_chans; i++) {
759 cchan = kzalloc(sizeof(*cchan), GFP_KERNEL);
760 if (!cchan)
761 goto err;
762
763 cchan->cdd = cdd;
764 if (i & 1) {
765 cchan->gcr_reg = cdd->ctrl_mem + DMA_TXGCR(i >> 1);
766 cchan->is_tx = 1;
767 } else {
768 cchan->gcr_reg = cdd->ctrl_mem + DMA_RXGCR(i >> 1);
769 cchan->is_tx = 0;
770 }
771 cchan->port_num = i >> 1;
772 cchan->desc = &cdd->cd[i];
773 cchan->desc_phys = cdd->descs_phys;
774 cchan->desc_phys += i * sizeof(struct cppi41_desc);
775 cchan->chan.device = &cdd->ddev;
776 list_add_tail(&cchan->chan.device_node, &cdd->ddev.channels);
777 }
778 cdd->first_td_desc = n_chans;
779
780 return 0;
781err:
782 cleanup_chans(cdd);
783 return -ENOMEM;
784}
785
Daniel Macke327e212013-09-22 16:50:00 +0200786static void purge_descs(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200787{
788 unsigned int mem_decs;
789 int i;
790
791 mem_decs = ALLOC_DECS_NUM * sizeof(struct cppi41_desc);
792
793 for (i = 0; i < DESCS_AREAS; i++) {
794
795 cppi_writel(0, cdd->qmgr_mem + QMGR_MEMBASE(i));
796 cppi_writel(0, cdd->qmgr_mem + QMGR_MEMCTRL(i));
797
Daniel Macke327e212013-09-22 16:50:00 +0200798 dma_free_coherent(dev, mem_decs, cdd->cd,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200799 cdd->descs_phys);
800 }
801}
802
803static void disable_sched(struct cppi41_dd *cdd)
804{
805 cppi_writel(0, cdd->sched_mem + DMA_SCHED_CTRL);
806}
807
Daniel Mackb46ce4d2013-09-22 16:50:01 +0200808static void deinit_cppi41(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200809{
810 disable_sched(cdd);
811
Daniel Macke327e212013-09-22 16:50:00 +0200812 purge_descs(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200813
814 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM0_BASE);
815 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM0_BASE);
Daniel Macke327e212013-09-22 16:50:00 +0200816 dma_free_coherent(dev, QMGR_SCRATCH_SIZE, cdd->qmgr_scratch,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200817 cdd->scratch_phys);
818}
819
Daniel Macke327e212013-09-22 16:50:00 +0200820static int init_descs(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200821{
822 unsigned int desc_size;
823 unsigned int mem_decs;
824 int i;
825 u32 reg;
826 u32 idx;
827
828 BUILD_BUG_ON(sizeof(struct cppi41_desc) &
829 (sizeof(struct cppi41_desc) - 1));
830 BUILD_BUG_ON(sizeof(struct cppi41_desc) < 32);
831 BUILD_BUG_ON(ALLOC_DECS_NUM < 32);
832
833 desc_size = sizeof(struct cppi41_desc);
834 mem_decs = ALLOC_DECS_NUM * desc_size;
835
836 idx = 0;
837 for (i = 0; i < DESCS_AREAS; i++) {
838
839 reg = idx << QMGR_MEMCTRL_IDX_SH;
840 reg |= (ilog2(desc_size) - 5) << QMGR_MEMCTRL_DESC_SH;
841 reg |= ilog2(ALLOC_DECS_NUM) - 5;
842
843 BUILD_BUG_ON(DESCS_AREAS != 1);
Daniel Macke327e212013-09-22 16:50:00 +0200844 cdd->cd = dma_alloc_coherent(dev, mem_decs,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200845 &cdd->descs_phys, GFP_KERNEL);
846 if (!cdd->cd)
847 return -ENOMEM;
848
849 cppi_writel(cdd->descs_phys, cdd->qmgr_mem + QMGR_MEMBASE(i));
850 cppi_writel(reg, cdd->qmgr_mem + QMGR_MEMCTRL(i));
851
852 idx += ALLOC_DECS_NUM;
853 }
854 return 0;
855}
856
857static void init_sched(struct cppi41_dd *cdd)
858{
859 unsigned ch;
860 unsigned word;
861 u32 reg;
862
863 word = 0;
864 cppi_writel(0, cdd->sched_mem + DMA_SCHED_CTRL);
865 for (ch = 0; ch < 15 * 2; ch += 2) {
866
867 reg = SCHED_ENTRY0_CHAN(ch);
868 reg |= SCHED_ENTRY1_CHAN(ch) | SCHED_ENTRY1_IS_RX;
869
870 reg |= SCHED_ENTRY2_CHAN(ch + 1);
871 reg |= SCHED_ENTRY3_CHAN(ch + 1) | SCHED_ENTRY3_IS_RX;
872 cppi_writel(reg, cdd->sched_mem + DMA_SCHED_WORD(word));
873 word++;
874 }
875 reg = 15 * 2 * 2 - 1;
876 reg |= DMA_SCHED_CTRL_EN;
877 cppi_writel(reg, cdd->sched_mem + DMA_SCHED_CTRL);
878}
879
Daniel Macke327e212013-09-22 16:50:00 +0200880static int init_cppi41(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200881{
882 int ret;
883
884 BUILD_BUG_ON(QMGR_SCRATCH_SIZE > ((1 << 14) - 1));
Daniel Macke327e212013-09-22 16:50:00 +0200885 cdd->qmgr_scratch = dma_alloc_coherent(dev, QMGR_SCRATCH_SIZE,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200886 &cdd->scratch_phys, GFP_KERNEL);
887 if (!cdd->qmgr_scratch)
888 return -ENOMEM;
889
890 cppi_writel(cdd->scratch_phys, cdd->qmgr_mem + QMGR_LRAM0_BASE);
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100891 cppi_writel(TOTAL_DESCS_NUM, cdd->qmgr_mem + QMGR_LRAM_SIZE);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200892 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM1_BASE);
893
Daniel Macke327e212013-09-22 16:50:00 +0200894 ret = init_descs(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200895 if (ret)
896 goto err_td;
897
898 cppi_writel(cdd->td_queue.submit, cdd->ctrl_mem + DMA_TDFDQ);
899 init_sched(cdd);
900 return 0;
901err_td:
Daniel Mackb46ce4d2013-09-22 16:50:01 +0200902 deinit_cppi41(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200903 return ret;
904}
905
906static struct platform_driver cpp41_dma_driver;
907/*
908 * The param format is:
909 * X Y
910 * X: Port
911 * Y: 0 = RX else TX
912 */
913#define INFO_PORT 0
914#define INFO_IS_TX 1
915
916static bool cpp41_dma_filter_fn(struct dma_chan *chan, void *param)
917{
918 struct cppi41_channel *cchan;
919 struct cppi41_dd *cdd;
920 const struct chan_queues *queues;
921 u32 *num = param;
922
923 if (chan->device->dev->driver != &cpp41_dma_driver.driver)
924 return false;
925
926 cchan = to_cpp41_chan(chan);
927
928 if (cchan->port_num != num[INFO_PORT])
929 return false;
930
931 if (cchan->is_tx && !num[INFO_IS_TX])
932 return false;
933 cdd = cchan->cdd;
934 if (cchan->is_tx)
935 queues = cdd->queues_tx;
936 else
937 queues = cdd->queues_rx;
938
Alexandre Bailone1f40bf2017-02-15 14:56:33 +0100939 BUILD_BUG_ON(ARRAY_SIZE(am335x_usb_queues_rx) !=
940 ARRAY_SIZE(am335x_usb_queues_tx));
941 if (WARN_ON(cchan->port_num > ARRAY_SIZE(am335x_usb_queues_rx)))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200942 return false;
943
944 cchan->q_num = queues[cchan->port_num].submit;
945 cchan->q_comp_num = queues[cchan->port_num].complete;
946 return true;
947}
948
949static struct of_dma_filter_info cpp41_dma_info = {
950 .filter_fn = cpp41_dma_filter_fn,
951};
952
953static struct dma_chan *cppi41_dma_xlate(struct of_phandle_args *dma_spec,
954 struct of_dma *ofdma)
955{
956 int count = dma_spec->args_count;
957 struct of_dma_filter_info *info = ofdma->of_dma_data;
958
959 if (!info || !info->filter_fn)
960 return NULL;
961
962 if (count != 2)
963 return NULL;
964
965 return dma_request_channel(info->dma_cap, info->filter_fn,
966 &dma_spec->args[0]);
967}
968
Alexandre Bailone1f40bf2017-02-15 14:56:33 +0100969static const struct cppi_glue_infos am335x_usb_infos = {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200970 .isr = cppi41_irq,
Alexandre Bailone1f40bf2017-02-15 14:56:33 +0100971 .queues_rx = am335x_usb_queues_rx,
972 .queues_tx = am335x_usb_queues_tx,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200973 .td_queue = { .submit = 31, .complete = 0 },
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100974 .first_completion_queue = 93,
975 .qmgr_num_pend = 5,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200976};
977
978static const struct of_device_id cppi41_dma_ids[] = {
Alexandre Bailone1f40bf2017-02-15 14:56:33 +0100979 { .compatible = "ti,am3359-cppi41", .data = &am335x_usb_infos},
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200980 {},
981};
982MODULE_DEVICE_TABLE(of, cppi41_dma_ids);
983
Daniel Macke327e212013-09-22 16:50:00 +0200984static const struct cppi_glue_infos *get_glue_info(struct device *dev)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200985{
986 const struct of_device_id *of_id;
987
Daniel Macke327e212013-09-22 16:50:00 +0200988 of_id = of_match_node(cppi41_dma_ids, dev->of_node);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200989 if (!of_id)
990 return NULL;
991 return of_id->data;
992}
993
Felipe Balbiffeb13a2015-04-08 11:45:42 -0500994#define CPPI41_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
995 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
996 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
997 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
998
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200999static int cppi41_dma_probe(struct platform_device *pdev)
1000{
1001 struct cppi41_dd *cdd;
Daniel Mack717d8182013-09-22 16:50:02 +02001002 struct device *dev = &pdev->dev;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001003 const struct cppi_glue_infos *glue_info;
Alexandre Bailon6ee60242017-02-15 14:56:32 +01001004 int index;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001005 int irq;
1006 int ret;
1007
Daniel Mack717d8182013-09-22 16:50:02 +02001008 glue_info = get_glue_info(dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001009 if (!glue_info)
1010 return -EINVAL;
1011
Kiran Padwalf0f3b5f2014-09-24 15:53:46 +05301012 cdd = devm_kzalloc(&pdev->dev, sizeof(*cdd), GFP_KERNEL);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001013 if (!cdd)
1014 return -ENOMEM;
1015
1016 dma_cap_set(DMA_SLAVE, cdd->ddev.cap_mask);
1017 cdd->ddev.device_alloc_chan_resources = cppi41_dma_alloc_chan_resources;
1018 cdd->ddev.device_free_chan_resources = cppi41_dma_free_chan_resources;
1019 cdd->ddev.device_tx_status = cppi41_dma_tx_status;
1020 cdd->ddev.device_issue_pending = cppi41_dma_issue_pending;
1021 cdd->ddev.device_prep_slave_sg = cppi41_dma_prep_slave_sg;
Maxime Ripard3b5a03a2014-11-17 14:42:10 +01001022 cdd->ddev.device_terminate_all = cppi41_stop_chan;
Felipe Balbiffeb13a2015-04-08 11:45:42 -05001023 cdd->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1024 cdd->ddev.src_addr_widths = CPPI41_DMA_BUSWIDTHS;
1025 cdd->ddev.dst_addr_widths = CPPI41_DMA_BUSWIDTHS;
1026 cdd->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Daniel Mack717d8182013-09-22 16:50:02 +02001027 cdd->ddev.dev = dev;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001028 INIT_LIST_HEAD(&cdd->ddev.channels);
1029 cpp41_dma_info.dma_cap = cdd->ddev.cap_mask;
1030
Alexandre Bailon6ee60242017-02-15 14:56:32 +01001031 index = of_property_match_string(dev->of_node,
1032 "reg-names", "controller");
1033 if (index < 0)
1034 return index;
1035
1036 cdd->ctrl_mem = of_iomap(dev->of_node, index);
1037 cdd->sched_mem = of_iomap(dev->of_node, index + 1);
1038 cdd->qmgr_mem = of_iomap(dev->of_node, index + 2);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001039 spin_lock_init(&cdd->lock);
1040 INIT_LIST_HEAD(&cdd->pending);
1041
1042 platform_set_drvdata(pdev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001043
Alexandre Bailon6ee60242017-02-15 14:56:32 +01001044 if (!cdd->ctrl_mem || !cdd->sched_mem ||
Kiran Padwalf0f3b5f2014-09-24 15:53:46 +05301045 !cdd->qmgr_mem)
1046 return -ENXIO;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001047
Daniel Mack717d8182013-09-22 16:50:02 +02001048 pm_runtime_enable(dev);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001049 pm_runtime_set_autosuspend_delay(dev, 100);
1050 pm_runtime_use_autosuspend(dev);
Daniel Mack717d8182013-09-22 16:50:02 +02001051 ret = pm_runtime_get_sync(dev);
Sebastian Andrzej Siewiorcbf1e562013-10-22 12:14:06 +02001052 if (ret < 0)
Sebastian Andrzej Siewiord6aafa22013-08-20 18:35:53 +02001053 goto err_get_sync;
1054
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001055 cdd->queues_rx = glue_info->queues_rx;
1056 cdd->queues_tx = glue_info->queues_tx;
1057 cdd->td_queue = glue_info->td_queue;
Alexandre Bailon2d535b22017-02-15 14:56:34 +01001058 cdd->qmgr_num_pend = glue_info->qmgr_num_pend;
1059 cdd->first_completion_queue = glue_info->first_completion_queue;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001060
Daniel Mack717d8182013-09-22 16:50:02 +02001061 ret = init_cppi41(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001062 if (ret)
1063 goto err_init_cppi;
1064
Daniel Mack717d8182013-09-22 16:50:02 +02001065 ret = cppi41_add_chans(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001066 if (ret)
1067 goto err_chans;
1068
Daniel Mack717d8182013-09-22 16:50:02 +02001069 irq = irq_of_parse_and_map(dev->of_node, 0);
Julia Lawallf3b77722013-12-29 23:47:23 +01001070 if (!irq) {
1071 ret = -EINVAL;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001072 goto err_irq;
Julia Lawallf3b77722013-12-29 23:47:23 +01001073 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001074
Kiran Padwalf0f3b5f2014-09-24 15:53:46 +05301075 ret = devm_request_irq(&pdev->dev, irq, glue_info->isr, IRQF_SHARED,
Daniel Mack717d8182013-09-22 16:50:02 +02001076 dev_name(dev), cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001077 if (ret)
1078 goto err_irq;
1079 cdd->irq = irq;
1080
1081 ret = dma_async_device_register(&cdd->ddev);
1082 if (ret)
1083 goto err_dma_reg;
1084
Daniel Mack717d8182013-09-22 16:50:02 +02001085 ret = of_dma_controller_register(dev->of_node,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001086 cppi41_dma_xlate, &cpp41_dma_info);
1087 if (ret)
1088 goto err_of;
1089
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001090 pm_runtime_mark_last_busy(dev);
1091 pm_runtime_put_autosuspend(dev);
1092
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001093 return 0;
1094err_of:
1095 dma_async_device_unregister(&cdd->ddev);
1096err_dma_reg:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001097err_irq:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001098 cleanup_chans(cdd);
1099err_chans:
Daniel Mack717d8182013-09-22 16:50:02 +02001100 deinit_cppi41(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001101err_init_cppi:
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001102 pm_runtime_dont_use_autosuspend(dev);
Sebastian Andrzej Siewiord6aafa22013-08-20 18:35:53 +02001103err_get_sync:
Tony Lindgrend5afc1b2016-11-16 10:24:15 -08001104 pm_runtime_put_sync(dev);
Daniel Mack717d8182013-09-22 16:50:02 +02001105 pm_runtime_disable(dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001106 iounmap(cdd->ctrl_mem);
1107 iounmap(cdd->sched_mem);
1108 iounmap(cdd->qmgr_mem);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001109 return ret;
1110}
1111
1112static int cppi41_dma_remove(struct platform_device *pdev)
1113{
1114 struct cppi41_dd *cdd = platform_get_drvdata(pdev);
Tony Lindgren12f59082016-11-09 09:47:58 -07001115 int error;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001116
Tony Lindgren12f59082016-11-09 09:47:58 -07001117 error = pm_runtime_get_sync(&pdev->dev);
1118 if (error < 0)
1119 dev_err(&pdev->dev, "%s could not pm_runtime_get: %i\n",
1120 __func__, error);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001121 of_dma_controller_free(pdev->dev.of_node);
1122 dma_async_device_unregister(&cdd->ddev);
1123
Kiran Padwalf0f3b5f2014-09-24 15:53:46 +05301124 devm_free_irq(&pdev->dev, cdd->irq, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001125 cleanup_chans(cdd);
Daniel Mackb46ce4d2013-09-22 16:50:01 +02001126 deinit_cppi41(&pdev->dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001127 iounmap(cdd->ctrl_mem);
1128 iounmap(cdd->sched_mem);
1129 iounmap(cdd->qmgr_mem);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001130 pm_runtime_dont_use_autosuspend(&pdev->dev);
1131 pm_runtime_put_sync(&pdev->dev);
Sebastian Andrzej Siewiord6aafa22013-08-20 18:35:53 +02001132 pm_runtime_disable(&pdev->dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001133 return 0;
1134}
1135
Arnd Bergmann522ef612016-09-06 15:20:05 +02001136static int __maybe_unused cppi41_suspend(struct device *dev)
Daniel Mackf97b98d2013-09-22 16:50:04 +02001137{
1138 struct cppi41_dd *cdd = dev_get_drvdata(dev);
1139
Daniel Mackf8964962013-10-22 12:14:03 +02001140 cdd->dma_tdfdq = cppi_readl(cdd->ctrl_mem + DMA_TDFDQ);
Daniel Mackf97b98d2013-09-22 16:50:04 +02001141 disable_sched(cdd);
1142
1143 return 0;
1144}
1145
Arnd Bergmann522ef612016-09-06 15:20:05 +02001146static int __maybe_unused cppi41_resume(struct device *dev)
Daniel Mackf97b98d2013-09-22 16:50:04 +02001147{
1148 struct cppi41_dd *cdd = dev_get_drvdata(dev);
Daniel Mackf8964962013-10-22 12:14:03 +02001149 struct cppi41_channel *c;
Daniel Mackf97b98d2013-09-22 16:50:04 +02001150 int i;
1151
1152 for (i = 0; i < DESCS_AREAS; i++)
1153 cppi_writel(cdd->descs_phys, cdd->qmgr_mem + QMGR_MEMBASE(i));
1154
Daniel Mackf8964962013-10-22 12:14:03 +02001155 list_for_each_entry(c, &cdd->ddev.channels, chan.device_node)
1156 if (!c->is_tx)
1157 cppi_writel(c->q_num, c->gcr_reg + RXHPCRA0);
1158
Daniel Mackf97b98d2013-09-22 16:50:04 +02001159 init_sched(cdd);
Daniel Mackf8964962013-10-22 12:14:03 +02001160
1161 cppi_writel(cdd->dma_tdfdq, cdd->ctrl_mem + DMA_TDFDQ);
1162 cppi_writel(cdd->scratch_phys, cdd->qmgr_mem + QMGR_LRAM0_BASE);
1163 cppi_writel(QMGR_SCRATCH_SIZE, cdd->qmgr_mem + QMGR_LRAM_SIZE);
1164 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM1_BASE);
1165
Daniel Mackf97b98d2013-09-22 16:50:04 +02001166 return 0;
1167}
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001168
Arnd Bergmann522ef612016-09-06 15:20:05 +02001169static int __maybe_unused cppi41_runtime_suspend(struct device *dev)
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001170{
1171 struct cppi41_dd *cdd = dev_get_drvdata(dev);
Tony Lindgren362f4562017-01-19 08:49:08 -08001172 unsigned long flags;
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001173
Tony Lindgren362f4562017-01-19 08:49:08 -08001174 spin_lock_irqsave(&cdd->lock, flags);
1175 cdd->is_suspended = true;
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001176 WARN_ON(!list_empty(&cdd->pending));
Tony Lindgren362f4562017-01-19 08:49:08 -08001177 spin_unlock_irqrestore(&cdd->lock, flags);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001178
1179 return 0;
1180}
1181
Arnd Bergmann522ef612016-09-06 15:20:05 +02001182static int __maybe_unused cppi41_runtime_resume(struct device *dev)
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001183{
1184 struct cppi41_dd *cdd = dev_get_drvdata(dev);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001185 unsigned long flags;
1186
1187 spin_lock_irqsave(&cdd->lock, flags);
Tony Lindgren362f4562017-01-19 08:49:08 -08001188 cdd->is_suspended = false;
1189 cppi41_run_queue(cdd);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001190 spin_unlock_irqrestore(&cdd->lock, flags);
1191
1192 return 0;
1193}
Daniel Mackf97b98d2013-09-22 16:50:04 +02001194
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001195static const struct dev_pm_ops cppi41_pm_ops = {
1196 SET_LATE_SYSTEM_SLEEP_PM_OPS(cppi41_suspend, cppi41_resume)
1197 SET_RUNTIME_PM_OPS(cppi41_runtime_suspend,
1198 cppi41_runtime_resume,
1199 NULL)
1200};
Daniel Mackf97b98d2013-09-22 16:50:04 +02001201
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001202static struct platform_driver cpp41_dma_driver = {
1203 .probe = cppi41_dma_probe,
1204 .remove = cppi41_dma_remove,
1205 .driver = {
1206 .name = "cppi41-dma-engine",
Daniel Mackf97b98d2013-09-22 16:50:04 +02001207 .pm = &cppi41_pm_ops,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001208 .of_match_table = of_match_ptr(cppi41_dma_ids),
1209 },
1210};
1211
1212module_platform_driver(cpp41_dma_driver);
1213MODULE_LICENSE("GPL");
1214MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");