blob: 135fb45a96f0925fc85a9708dd7f84ab41d69495 [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;
Alexandre Bailon5e46fe92017-02-15 14:56:35 +0100142 u32 n_chans;
143 u8 platform;
Daniel Mackf8964962013-10-22 12:14:03 +0200144
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700145 struct list_head pending; /* Pending queued transfers */
146 spinlock_t lock; /* Lock for pending list */
147
Daniel Mackf8964962013-10-22 12:14:03 +0200148 /* context for suspend/resume */
149 unsigned int dma_tdfdq;
Tony Lindgren362f4562017-01-19 08:49:08 -0800150
151 bool is_suspended;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200152};
153
Alexandre Bailone1f40bf2017-02-15 14:56:33 +0100154static struct chan_queues am335x_usb_queues_tx[] = {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200155 /* USB0 ENDP 1 */
156 [ 0] = { .submit = 32, .complete = 93},
157 [ 1] = { .submit = 34, .complete = 94},
158 [ 2] = { .submit = 36, .complete = 95},
159 [ 3] = { .submit = 38, .complete = 96},
160 [ 4] = { .submit = 40, .complete = 97},
161 [ 5] = { .submit = 42, .complete = 98},
162 [ 6] = { .submit = 44, .complete = 99},
163 [ 7] = { .submit = 46, .complete = 100},
164 [ 8] = { .submit = 48, .complete = 101},
165 [ 9] = { .submit = 50, .complete = 102},
166 [10] = { .submit = 52, .complete = 103},
167 [11] = { .submit = 54, .complete = 104},
168 [12] = { .submit = 56, .complete = 105},
169 [13] = { .submit = 58, .complete = 106},
170 [14] = { .submit = 60, .complete = 107},
171
172 /* USB1 ENDP1 */
173 [15] = { .submit = 62, .complete = 125},
174 [16] = { .submit = 64, .complete = 126},
175 [17] = { .submit = 66, .complete = 127},
176 [18] = { .submit = 68, .complete = 128},
177 [19] = { .submit = 70, .complete = 129},
178 [20] = { .submit = 72, .complete = 130},
179 [21] = { .submit = 74, .complete = 131},
180 [22] = { .submit = 76, .complete = 132},
181 [23] = { .submit = 78, .complete = 133},
182 [24] = { .submit = 80, .complete = 134},
183 [25] = { .submit = 82, .complete = 135},
184 [26] = { .submit = 84, .complete = 136},
185 [27] = { .submit = 86, .complete = 137},
186 [28] = { .submit = 88, .complete = 138},
187 [29] = { .submit = 90, .complete = 139},
188};
189
Alexandre Bailone1f40bf2017-02-15 14:56:33 +0100190static const struct chan_queues am335x_usb_queues_rx[] = {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200191 /* USB0 ENDP 1 */
192 [ 0] = { .submit = 1, .complete = 109},
193 [ 1] = { .submit = 2, .complete = 110},
194 [ 2] = { .submit = 3, .complete = 111},
195 [ 3] = { .submit = 4, .complete = 112},
196 [ 4] = { .submit = 5, .complete = 113},
197 [ 5] = { .submit = 6, .complete = 114},
198 [ 6] = { .submit = 7, .complete = 115},
199 [ 7] = { .submit = 8, .complete = 116},
200 [ 8] = { .submit = 9, .complete = 117},
201 [ 9] = { .submit = 10, .complete = 118},
202 [10] = { .submit = 11, .complete = 119},
203 [11] = { .submit = 12, .complete = 120},
204 [12] = { .submit = 13, .complete = 121},
205 [13] = { .submit = 14, .complete = 122},
206 [14] = { .submit = 15, .complete = 123},
207
208 /* USB1 ENDP 1 */
209 [15] = { .submit = 16, .complete = 141},
210 [16] = { .submit = 17, .complete = 142},
211 [17] = { .submit = 18, .complete = 143},
212 [18] = { .submit = 19, .complete = 144},
213 [19] = { .submit = 20, .complete = 145},
214 [20] = { .submit = 21, .complete = 146},
215 [21] = { .submit = 22, .complete = 147},
216 [22] = { .submit = 23, .complete = 148},
217 [23] = { .submit = 24, .complete = 149},
218 [24] = { .submit = 25, .complete = 150},
219 [25] = { .submit = 26, .complete = 151},
220 [26] = { .submit = 27, .complete = 152},
221 [27] = { .submit = 28, .complete = 153},
222 [28] = { .submit = 29, .complete = 154},
223 [29] = { .submit = 30, .complete = 155},
224};
225
Alexandre Bailone3fa49a2017-01-30 18:49:20 +0100226static const struct chan_queues da8xx_usb_queues_tx[] = {
227 [0] = { .submit = 16, .complete = 24},
228 [1] = { .submit = 18, .complete = 24},
229 [2] = { .submit = 20, .complete = 24},
230 [3] = { .submit = 22, .complete = 24},
231};
232
233static const struct chan_queues da8xx_usb_queues_rx[] = {
234 [0] = { .submit = 1, .complete = 26},
235 [1] = { .submit = 3, .complete = 26},
236 [2] = { .submit = 5, .complete = 26},
237 [3] = { .submit = 7, .complete = 26},
238};
239
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200240struct cppi_glue_infos {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200241 const struct chan_queues *queues_rx;
242 const struct chan_queues *queues_tx;
243 struct chan_queues td_queue;
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100244 u16 first_completion_queue;
245 u16 qmgr_num_pend;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200246};
247
248static struct cppi41_channel *to_cpp41_chan(struct dma_chan *c)
249{
250 return container_of(c, struct cppi41_channel, chan);
251}
252
253static struct cppi41_channel *desc_to_chan(struct cppi41_dd *cdd, u32 desc)
254{
255 struct cppi41_channel *c;
256 u32 descs_size;
257 u32 desc_num;
258
259 descs_size = sizeof(struct cppi41_desc) * ALLOC_DECS_NUM;
260
261 if (!((desc >= cdd->descs_phys) &&
262 (desc < (cdd->descs_phys + descs_size)))) {
263 return NULL;
264 }
265
266 desc_num = (desc - cdd->descs_phys) / sizeof(struct cppi41_desc);
Dan Carpenter2d17f7f2013-08-28 13:48:44 +0300267 BUG_ON(desc_num >= ALLOC_DECS_NUM);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200268 c = cdd->chan_busy[desc_num];
269 cdd->chan_busy[desc_num] = NULL;
Tony Lindgrenae4a3e022017-01-19 08:49:07 -0800270
271 /* Usecount for chan_busy[], paired with push_desc_queue() */
272 pm_runtime_put(cdd->ddev.dev);
273
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200274 return c;
275}
276
277static void cppi_writel(u32 val, void *__iomem *mem)
278{
279 __raw_writel(val, mem);
280}
281
282static u32 cppi_readl(void *__iomem *mem)
283{
284 return __raw_readl(mem);
285}
286
287static u32 pd_trans_len(u32 val)
288{
289 return val & ((1 << (DESC_LENGTH_BITS_NUM + 1)) - 1);
290}
291
Daniel Mack706ff622013-10-22 12:14:04 +0200292static u32 cppi41_pop_desc(struct cppi41_dd *cdd, unsigned queue_num)
293{
294 u32 desc;
295
296 desc = cppi_readl(cdd->qmgr_mem + QMGR_QUEUE_D(queue_num));
297 desc &= ~0x1f;
298 return desc;
299}
300
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200301static irqreturn_t cppi41_irq(int irq, void *data)
302{
303 struct cppi41_dd *cdd = data;
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100304 u16 first_completion_queue = cdd->first_completion_queue;
305 u16 qmgr_num_pend = cdd->qmgr_num_pend;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200306 struct cppi41_channel *c;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200307 int i;
308
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100309 for (i = QMGR_PENDING_SLOT_Q(first_completion_queue); i < qmgr_num_pend;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200310 i++) {
311 u32 val;
312 u32 q_num;
313
314 val = cppi_readl(cdd->qmgr_mem + QMGR_PEND(i));
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100315 if (i == QMGR_PENDING_SLOT_Q(first_completion_queue) && val) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200316 u32 mask;
317 /* set corresponding bit for completetion Q 93 */
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100318 mask = 1 << QMGR_PENDING_BIT_Q(first_completion_queue);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200319 /* not set all bits for queues less than Q 93 */
320 mask--;
321 /* now invert and keep only Q 93+ set */
322 val &= ~mask;
323 }
324
325 if (val)
326 __iormb();
327
328 while (val) {
Daniel Mack13bbfb52014-05-26 14:52:34 +0200329 u32 desc, len;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200330
Tony Lindgren6610d0e2017-01-20 12:07:53 -0800331 /*
332 * This should never trigger, see the comments in
333 * push_desc_queue()
334 */
335 WARN_ON(cdd->is_suspended);
Tony Lindgren098de422016-11-09 09:47:59 -0700336
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200337 q_num = __fls(val);
338 val &= ~(1 << q_num);
339 q_num += 32 * i;
Daniel Mack706ff622013-10-22 12:14:04 +0200340 desc = cppi41_pop_desc(cdd, q_num);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200341 c = desc_to_chan(cdd, desc);
342 if (WARN_ON(!c)) {
343 pr_err("%s() q %d desc %08x\n", __func__,
344 q_num, desc);
345 continue;
346 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200347
Daniel Mack13bbfb52014-05-26 14:52:34 +0200348 if (c->desc->pd2 & PD2_ZERO_LENGTH)
349 len = 0;
350 else
351 len = pd_trans_len(c->desc->pd0);
352
353 c->residue = pd_trans_len(c->desc->pd6) - len;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200354 dma_cookie_complete(&c->txd);
Dave Jiangb310a612016-07-20 13:10:54 -0700355 dmaengine_desc_get_callback_invoke(&c->txd, NULL);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200356 }
357 }
358 return IRQ_HANDLED;
359}
360
361static dma_cookie_t cppi41_tx_submit(struct dma_async_tx_descriptor *tx)
362{
363 dma_cookie_t cookie;
364
365 cookie = dma_cookie_assign(tx);
366
367 return cookie;
368}
369
370static int cppi41_dma_alloc_chan_resources(struct dma_chan *chan)
371{
372 struct cppi41_channel *c = to_cpp41_chan(chan);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700373 struct cppi41_dd *cdd = c->cdd;
374 int error;
375
376 error = pm_runtime_get_sync(cdd->ddev.dev);
Tony Lindgren740b4be2016-11-11 11:28:52 -0800377 if (error < 0) {
Tony Lindgrend5afc1b2016-11-16 10:24:15 -0800378 dev_err(cdd->ddev.dev, "%s pm runtime get: %i\n",
379 __func__, error);
Tony Lindgren740b4be2016-11-11 11:28:52 -0800380 pm_runtime_put_noidle(cdd->ddev.dev);
381
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700382 return error;
Tony Lindgren740b4be2016-11-11 11:28:52 -0800383 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200384
385 dma_cookie_init(chan);
386 dma_async_tx_descriptor_init(&c->txd, chan);
387 c->txd.tx_submit = cppi41_tx_submit;
388
389 if (!c->is_tx)
390 cppi_writel(c->q_num, c->gcr_reg + RXHPCRA0);
391
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700392 pm_runtime_mark_last_busy(cdd->ddev.dev);
393 pm_runtime_put_autosuspend(cdd->ddev.dev);
394
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200395 return 0;
396}
397
398static void cppi41_dma_free_chan_resources(struct dma_chan *chan)
399{
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700400 struct cppi41_channel *c = to_cpp41_chan(chan);
401 struct cppi41_dd *cdd = c->cdd;
402 int error;
403
404 error = pm_runtime_get_sync(cdd->ddev.dev);
Tony Lindgren740b4be2016-11-11 11:28:52 -0800405 if (error < 0) {
406 pm_runtime_put_noidle(cdd->ddev.dev);
407
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700408 return;
Tony Lindgren740b4be2016-11-11 11:28:52 -0800409 }
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700410
411 WARN_ON(!list_empty(&cdd->pending));
412
413 pm_runtime_mark_last_busy(cdd->ddev.dev);
414 pm_runtime_put_autosuspend(cdd->ddev.dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200415}
416
417static enum dma_status cppi41_dma_tx_status(struct dma_chan *chan,
418 dma_cookie_t cookie, struct dma_tx_state *txstate)
419{
420 struct cppi41_channel *c = to_cpp41_chan(chan);
421 enum dma_status ret;
422
423 /* lock */
424 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Kouled83c0c2013-10-16 13:36:28 +0530425 if (txstate && ret == DMA_COMPLETE)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200426 txstate->residue = c->residue;
427 /* unlock */
428
429 return ret;
430}
431
432static void push_desc_queue(struct cppi41_channel *c)
433{
434 struct cppi41_dd *cdd = c->cdd;
435 u32 desc_num;
436 u32 desc_phys;
437 u32 reg;
438
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200439 c->residue = 0;
440
441 reg = GCR_CHAN_ENABLE;
442 if (!c->is_tx) {
443 reg |= GCR_STARV_RETRY;
444 reg |= GCR_DESC_TYPE_HOST;
445 reg |= c->q_comp_num;
446 }
447
448 cppi_writel(reg, c->gcr_reg);
449
450 /*
451 * We don't use writel() but __raw_writel() so we have to make sure
452 * that the DMA descriptor in coherent memory made to the main memory
453 * before starting the dma engine.
454 */
455 __iowmb();
Tony Lindgren670fc2a2016-08-19 15:59:39 -0700456
Tony Lindgrenae4a3e022017-01-19 08:49:07 -0800457 /*
458 * DMA transfers can take at least 200ms to complete with USB mass
459 * storage connected. To prevent autosuspend timeouts, we must use
460 * pm_runtime_get/put() when chan_busy[] is modified. This will get
461 * cleared in desc_to_chan() or cppi41_stop_chan() depending on the
462 * outcome of the transfer.
463 */
464 pm_runtime_get(cdd->ddev.dev);
465
Tony Lindgren670fc2a2016-08-19 15:59:39 -0700466 desc_phys = lower_32_bits(c->desc_phys);
467 desc_num = (desc_phys - cdd->descs_phys) / sizeof(struct cppi41_desc);
468 WARN_ON(cdd->chan_busy[desc_num]);
469 cdd->chan_busy[desc_num] = c;
470
471 reg = (sizeof(struct cppi41_desc) - 24) / 4;
472 reg |= desc_phys;
473 cppi_writel(reg, cdd->qmgr_mem + QMGR_QUEUE_D(c->q_num));
474}
475
Tony Lindgren362f4562017-01-19 08:49:08 -0800476/*
477 * Caller must hold cdd->lock to prevent push_desc_queue()
478 * getting called out of order. We have both cppi41_dma_issue_pending()
479 * and cppi41_runtime_resume() call this function.
480 */
481static void cppi41_run_queue(struct cppi41_dd *cdd)
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700482{
Tony Lindgren362f4562017-01-19 08:49:08 -0800483 struct cppi41_channel *c, *_c;
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700484
Tony Lindgren362f4562017-01-19 08:49:08 -0800485 list_for_each_entry_safe(c, _c, &cdd->pending, node) {
486 push_desc_queue(c);
487 list_del(&c->node);
488 }
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700489}
490
Tony Lindgren670fc2a2016-08-19 15:59:39 -0700491static void cppi41_dma_issue_pending(struct dma_chan *chan)
492{
493 struct cppi41_channel *c = to_cpp41_chan(chan);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700494 struct cppi41_dd *cdd = c->cdd;
Tony Lindgren362f4562017-01-19 08:49:08 -0800495 unsigned long flags;
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700496 int error;
Tony Lindgren670fc2a2016-08-19 15:59:39 -0700497
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700498 error = pm_runtime_get(cdd->ddev.dev);
Tony Lindgrenf2f6f822016-09-13 10:22:43 -0700499 if ((error != -EINPROGRESS) && error < 0) {
Tony Lindgren740b4be2016-11-11 11:28:52 -0800500 pm_runtime_put_noidle(cdd->ddev.dev);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -0700501 dev_err(cdd->ddev.dev, "Failed to pm_runtime_get: %i\n",
502 error);
503
504 return;
505 }
506
Tony Lindgren362f4562017-01-19 08:49:08 -0800507 spin_lock_irqsave(&cdd->lock, flags);
508 list_add_tail(&c->node, &cdd->pending);
509 if (!cdd->is_suspended)
510 cppi41_run_queue(cdd);
511 spin_unlock_irqrestore(&cdd->lock, flags);
Tony Lindgren098de422016-11-09 09:47:59 -0700512
513 pm_runtime_mark_last_busy(cdd->ddev.dev);
514 pm_runtime_put_autosuspend(cdd->ddev.dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200515}
516
517static u32 get_host_pd0(u32 length)
518{
519 u32 reg;
520
521 reg = DESC_TYPE_HOST << DESC_TYPE;
522 reg |= length;
523
524 return reg;
525}
526
527static u32 get_host_pd1(struct cppi41_channel *c)
528{
529 u32 reg;
530
531 reg = 0;
532
533 return reg;
534}
535
536static u32 get_host_pd2(struct cppi41_channel *c)
537{
538 u32 reg;
539
540 reg = DESC_TYPE_USB;
541 reg |= c->q_comp_num;
542
543 return reg;
544}
545
546static u32 get_host_pd3(u32 length)
547{
548 u32 reg;
549
550 /* PD3 = packet size */
551 reg = length;
552
553 return reg;
554}
555
556static u32 get_host_pd6(u32 length)
557{
558 u32 reg;
559
560 /* PD6 buffer size */
561 reg = DESC_PD_COMPLETE;
562 reg |= length;
563
564 return reg;
565}
566
567static u32 get_host_pd4_or_7(u32 addr)
568{
569 u32 reg;
570
571 reg = addr;
572
573 return reg;
574}
575
576static u32 get_host_pd5(void)
577{
578 u32 reg;
579
580 reg = 0;
581
582 return reg;
583}
584
585static struct dma_async_tx_descriptor *cppi41_dma_prep_slave_sg(
586 struct dma_chan *chan, struct scatterlist *sgl, unsigned sg_len,
587 enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
588{
589 struct cppi41_channel *c = to_cpp41_chan(chan);
590 struct cppi41_desc *d;
591 struct scatterlist *sg;
592 unsigned int i;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200593
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200594 d = c->desc;
595 for_each_sg(sgl, sg, sg_len, i) {
596 u32 addr;
597 u32 len;
598
599 /* We need to use more than one desc once musb supports sg */
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200600 addr = lower_32_bits(sg_dma_address(sg));
601 len = sg_dma_len(sg);
602
603 d->pd0 = get_host_pd0(len);
604 d->pd1 = get_host_pd1(c);
605 d->pd2 = get_host_pd2(c);
606 d->pd3 = get_host_pd3(len);
607 d->pd4 = get_host_pd4_or_7(addr);
608 d->pd5 = get_host_pd5();
609 d->pd6 = get_host_pd6(len);
610 d->pd7 = get_host_pd4_or_7(addr);
611
612 d++;
613 }
614
615 return &c->txd;
616}
617
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200618static void cppi41_compute_td_desc(struct cppi41_desc *d)
619{
620 d->pd0 = DESC_TYPE_TEARD << DESC_TYPE;
621}
622
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200623static int cppi41_tear_down_chan(struct cppi41_channel *c)
624{
Alexandre Bailon25534822017-02-06 22:53:56 -0600625 struct dmaengine_result abort_result;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200626 struct cppi41_dd *cdd = c->cdd;
627 struct cppi41_desc *td;
628 u32 reg;
629 u32 desc_phys;
630 u32 td_desc_phys;
631
632 td = cdd->cd;
633 td += cdd->first_td_desc;
634
635 td_desc_phys = cdd->descs_phys;
636 td_desc_phys += cdd->first_td_desc * sizeof(struct cppi41_desc);
637
638 if (!c->td_queued) {
639 cppi41_compute_td_desc(td);
640 __iowmb();
641
642 reg = (sizeof(struct cppi41_desc) - 24) / 4;
643 reg |= td_desc_phys;
644 cppi_writel(reg, cdd->qmgr_mem +
645 QMGR_QUEUE_D(cdd->td_queue.submit));
646
647 reg = GCR_CHAN_ENABLE;
648 if (!c->is_tx) {
649 reg |= GCR_STARV_RETRY;
650 reg |= GCR_DESC_TYPE_HOST;
651 reg |= c->q_comp_num;
652 }
653 reg |= GCR_TEARDOWN;
654 cppi_writel(reg, c->gcr_reg);
655 c->td_queued = 1;
Sebastian Andrzej Siewior6f9d7052014-12-03 15:09:49 +0100656 c->td_retry = 500;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200657 }
658
Sebastian Andrzej Siewior1e378a62013-10-22 12:14:05 +0200659 if (!c->td_seen || !c->td_desc_seen) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200660
Sebastian Andrzej Siewior1e378a62013-10-22 12:14:05 +0200661 desc_phys = cppi41_pop_desc(cdd, cdd->td_queue.complete);
662 if (!desc_phys)
663 desc_phys = cppi41_pop_desc(cdd, c->q_comp_num);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200664
Sebastian Andrzej Siewior1e378a62013-10-22 12:14:05 +0200665 if (desc_phys == c->desc_phys) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200666 c->td_desc_seen = 1;
Sebastian Andrzej Siewior1e378a62013-10-22 12:14:05 +0200667
668 } else if (desc_phys == td_desc_phys) {
669 u32 pd0;
670
671 __iormb();
672 pd0 = td->pd0;
673 WARN_ON((pd0 >> DESC_TYPE) != DESC_TYPE_TEARD);
674 WARN_ON(!c->is_tx && !(pd0 & TD_DESC_IS_RX));
675 WARN_ON((pd0 & 0x1f) != c->port_num);
676 c->td_seen = 1;
677 } else if (desc_phys) {
678 WARN_ON_ONCE(1);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200679 }
680 }
681 c->td_retry--;
682 /*
683 * If the TX descriptor / channel is in use, the caller needs to poke
684 * his TD bit multiple times. After that he hardware releases the
685 * transfer descriptor followed by TD descriptor. Waiting seems not to
686 * cause any difference.
687 * RX seems to be thrown out right away. However once the TearDown
688 * descriptor gets through we are done. If we have seens the transfer
689 * descriptor before the TD we fetch it from enqueue, it has to be
690 * there waiting for us.
691 */
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +0100692 if (!c->td_seen && c->td_retry) {
693 udelay(1);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200694 return -EAGAIN;
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +0100695 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200696 WARN_ON(!c->td_retry);
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +0100697
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200698 if (!c->td_desc_seen) {
Daniel Mack706ff622013-10-22 12:14:04 +0200699 desc_phys = cppi41_pop_desc(cdd, c->q_num);
Sebastian Andrzej Siewior754416e2014-12-03 15:09:50 +0100700 if (!desc_phys)
701 desc_phys = cppi41_pop_desc(cdd, c->q_comp_num);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200702 WARN_ON(!desc_phys);
703 }
704
705 c->td_queued = 0;
706 c->td_seen = 0;
707 c->td_desc_seen = 0;
708 cppi_writel(0, c->gcr_reg);
Alexandre Bailon25534822017-02-06 22:53:56 -0600709
710 /* Invoke the callback to do the necessary clean-up */
711 abort_result.result = DMA_TRANS_ABORTED;
712 dma_cookie_complete(&c->txd);
713 dmaengine_desc_get_callback_invoke(&c->txd, &abort_result);
714
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200715 return 0;
716}
717
718static int cppi41_stop_chan(struct dma_chan *chan)
719{
720 struct cppi41_channel *c = to_cpp41_chan(chan);
721 struct cppi41_dd *cdd = c->cdd;
722 u32 desc_num;
723 u32 desc_phys;
724 int ret;
725
George Cherian975faae2014-02-27 10:44:40 +0530726 desc_phys = lower_32_bits(c->desc_phys);
727 desc_num = (desc_phys - cdd->descs_phys) / sizeof(struct cppi41_desc);
728 if (!cdd->chan_busy[desc_num])
729 return 0;
730
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200731 ret = cppi41_tear_down_chan(c);
732 if (ret)
733 return ret;
734
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200735 WARN_ON(!cdd->chan_busy[desc_num]);
736 cdd->chan_busy[desc_num] = NULL;
737
Tony Lindgrenae4a3e022017-01-19 08:49:07 -0800738 /* Usecount for chan_busy[], paired with push_desc_queue() */
739 pm_runtime_put(cdd->ddev.dev);
740
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200741 return 0;
742}
743
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200744static void cleanup_chans(struct cppi41_dd *cdd)
745{
746 while (!list_empty(&cdd->ddev.channels)) {
747 struct cppi41_channel *cchan;
748
749 cchan = list_first_entry(&cdd->ddev.channels,
750 struct cppi41_channel, chan.device_node);
751 list_del(&cchan->chan.device_node);
752 kfree(cchan);
753 }
754}
755
Daniel Macke327e212013-09-22 16:50:00 +0200756static int cppi41_add_chans(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200757{
758 struct cppi41_channel *cchan;
759 int i;
Alexandre Bailon5e46fe92017-02-15 14:56:35 +0100760 u32 n_chans = cdd->n_chans;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200761
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200762 /*
763 * The channels can only be used as TX or as RX. So we add twice
764 * that much dma channels because USB can only do RX or TX.
765 */
766 n_chans *= 2;
767
768 for (i = 0; i < n_chans; i++) {
769 cchan = kzalloc(sizeof(*cchan), GFP_KERNEL);
770 if (!cchan)
771 goto err;
772
773 cchan->cdd = cdd;
774 if (i & 1) {
775 cchan->gcr_reg = cdd->ctrl_mem + DMA_TXGCR(i >> 1);
776 cchan->is_tx = 1;
777 } else {
778 cchan->gcr_reg = cdd->ctrl_mem + DMA_RXGCR(i >> 1);
779 cchan->is_tx = 0;
780 }
781 cchan->port_num = i >> 1;
782 cchan->desc = &cdd->cd[i];
783 cchan->desc_phys = cdd->descs_phys;
784 cchan->desc_phys += i * sizeof(struct cppi41_desc);
785 cchan->chan.device = &cdd->ddev;
786 list_add_tail(&cchan->chan.device_node, &cdd->ddev.channels);
787 }
788 cdd->first_td_desc = n_chans;
789
790 return 0;
791err:
792 cleanup_chans(cdd);
793 return -ENOMEM;
794}
795
Daniel Macke327e212013-09-22 16:50:00 +0200796static void purge_descs(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200797{
798 unsigned int mem_decs;
799 int i;
800
801 mem_decs = ALLOC_DECS_NUM * sizeof(struct cppi41_desc);
802
803 for (i = 0; i < DESCS_AREAS; i++) {
804
805 cppi_writel(0, cdd->qmgr_mem + QMGR_MEMBASE(i));
806 cppi_writel(0, cdd->qmgr_mem + QMGR_MEMCTRL(i));
807
Daniel Macke327e212013-09-22 16:50:00 +0200808 dma_free_coherent(dev, mem_decs, cdd->cd,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200809 cdd->descs_phys);
810 }
811}
812
813static void disable_sched(struct cppi41_dd *cdd)
814{
815 cppi_writel(0, cdd->sched_mem + DMA_SCHED_CTRL);
816}
817
Daniel Mackb46ce4d2013-09-22 16:50:01 +0200818static void deinit_cppi41(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200819{
820 disable_sched(cdd);
821
Daniel Macke327e212013-09-22 16:50:00 +0200822 purge_descs(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200823
824 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM0_BASE);
825 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM0_BASE);
Daniel Macke327e212013-09-22 16:50:00 +0200826 dma_free_coherent(dev, QMGR_SCRATCH_SIZE, cdd->qmgr_scratch,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200827 cdd->scratch_phys);
828}
829
Daniel Macke327e212013-09-22 16:50:00 +0200830static int init_descs(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200831{
832 unsigned int desc_size;
833 unsigned int mem_decs;
834 int i;
835 u32 reg;
836 u32 idx;
837
838 BUILD_BUG_ON(sizeof(struct cppi41_desc) &
839 (sizeof(struct cppi41_desc) - 1));
840 BUILD_BUG_ON(sizeof(struct cppi41_desc) < 32);
841 BUILD_BUG_ON(ALLOC_DECS_NUM < 32);
842
843 desc_size = sizeof(struct cppi41_desc);
844 mem_decs = ALLOC_DECS_NUM * desc_size;
845
846 idx = 0;
847 for (i = 0; i < DESCS_AREAS; i++) {
848
849 reg = idx << QMGR_MEMCTRL_IDX_SH;
850 reg |= (ilog2(desc_size) - 5) << QMGR_MEMCTRL_DESC_SH;
851 reg |= ilog2(ALLOC_DECS_NUM) - 5;
852
853 BUILD_BUG_ON(DESCS_AREAS != 1);
Daniel Macke327e212013-09-22 16:50:00 +0200854 cdd->cd = dma_alloc_coherent(dev, mem_decs,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200855 &cdd->descs_phys, GFP_KERNEL);
856 if (!cdd->cd)
857 return -ENOMEM;
858
859 cppi_writel(cdd->descs_phys, cdd->qmgr_mem + QMGR_MEMBASE(i));
860 cppi_writel(reg, cdd->qmgr_mem + QMGR_MEMCTRL(i));
861
862 idx += ALLOC_DECS_NUM;
863 }
864 return 0;
865}
866
867static void init_sched(struct cppi41_dd *cdd)
868{
869 unsigned ch;
870 unsigned word;
871 u32 reg;
872
873 word = 0;
874 cppi_writel(0, cdd->sched_mem + DMA_SCHED_CTRL);
Alexandre Bailon5e46fe92017-02-15 14:56:35 +0100875 for (ch = 0; ch < cdd->n_chans; ch += 2) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200876
877 reg = SCHED_ENTRY0_CHAN(ch);
878 reg |= SCHED_ENTRY1_CHAN(ch) | SCHED_ENTRY1_IS_RX;
879
880 reg |= SCHED_ENTRY2_CHAN(ch + 1);
881 reg |= SCHED_ENTRY3_CHAN(ch + 1) | SCHED_ENTRY3_IS_RX;
882 cppi_writel(reg, cdd->sched_mem + DMA_SCHED_WORD(word));
883 word++;
884 }
Alexandre Bailon5e46fe92017-02-15 14:56:35 +0100885 reg = cdd->n_chans * 2 - 1;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200886 reg |= DMA_SCHED_CTRL_EN;
887 cppi_writel(reg, cdd->sched_mem + DMA_SCHED_CTRL);
888}
889
Daniel Macke327e212013-09-22 16:50:00 +0200890static int init_cppi41(struct device *dev, struct cppi41_dd *cdd)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200891{
892 int ret;
893
894 BUILD_BUG_ON(QMGR_SCRATCH_SIZE > ((1 << 14) - 1));
Daniel Macke327e212013-09-22 16:50:00 +0200895 cdd->qmgr_scratch = dma_alloc_coherent(dev, QMGR_SCRATCH_SIZE,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200896 &cdd->scratch_phys, GFP_KERNEL);
897 if (!cdd->qmgr_scratch)
898 return -ENOMEM;
899
900 cppi_writel(cdd->scratch_phys, cdd->qmgr_mem + QMGR_LRAM0_BASE);
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100901 cppi_writel(TOTAL_DESCS_NUM, cdd->qmgr_mem + QMGR_LRAM_SIZE);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200902 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM1_BASE);
903
Daniel Macke327e212013-09-22 16:50:00 +0200904 ret = init_descs(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200905 if (ret)
906 goto err_td;
907
908 cppi_writel(cdd->td_queue.submit, cdd->ctrl_mem + DMA_TDFDQ);
909 init_sched(cdd);
Alexandre Bailon5e46fe92017-02-15 14:56:35 +0100910
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200911 return 0;
912err_td:
Daniel Mackb46ce4d2013-09-22 16:50:01 +0200913 deinit_cppi41(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200914 return ret;
915}
916
917static struct platform_driver cpp41_dma_driver;
918/*
919 * The param format is:
920 * X Y
921 * X: Port
922 * Y: 0 = RX else TX
923 */
924#define INFO_PORT 0
925#define INFO_IS_TX 1
926
927static bool cpp41_dma_filter_fn(struct dma_chan *chan, void *param)
928{
929 struct cppi41_channel *cchan;
930 struct cppi41_dd *cdd;
931 const struct chan_queues *queues;
932 u32 *num = param;
933
934 if (chan->device->dev->driver != &cpp41_dma_driver.driver)
935 return false;
936
937 cchan = to_cpp41_chan(chan);
938
939 if (cchan->port_num != num[INFO_PORT])
940 return false;
941
942 if (cchan->is_tx && !num[INFO_IS_TX])
943 return false;
944 cdd = cchan->cdd;
945 if (cchan->is_tx)
946 queues = cdd->queues_tx;
947 else
948 queues = cdd->queues_rx;
949
Alexandre Bailone1f40bf2017-02-15 14:56:33 +0100950 BUILD_BUG_ON(ARRAY_SIZE(am335x_usb_queues_rx) !=
951 ARRAY_SIZE(am335x_usb_queues_tx));
952 if (WARN_ON(cchan->port_num > ARRAY_SIZE(am335x_usb_queues_rx)))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200953 return false;
954
955 cchan->q_num = queues[cchan->port_num].submit;
956 cchan->q_comp_num = queues[cchan->port_num].complete;
957 return true;
958}
959
960static struct of_dma_filter_info cpp41_dma_info = {
961 .filter_fn = cpp41_dma_filter_fn,
962};
963
964static struct dma_chan *cppi41_dma_xlate(struct of_phandle_args *dma_spec,
965 struct of_dma *ofdma)
966{
967 int count = dma_spec->args_count;
968 struct of_dma_filter_info *info = ofdma->of_dma_data;
969
970 if (!info || !info->filter_fn)
971 return NULL;
972
973 if (count != 2)
974 return NULL;
975
976 return dma_request_channel(info->dma_cap, info->filter_fn,
977 &dma_spec->args[0]);
978}
979
Alexandre Bailone1f40bf2017-02-15 14:56:33 +0100980static const struct cppi_glue_infos am335x_usb_infos = {
Alexandre Bailone1f40bf2017-02-15 14:56:33 +0100981 .queues_rx = am335x_usb_queues_rx,
982 .queues_tx = am335x_usb_queues_tx,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200983 .td_queue = { .submit = 31, .complete = 0 },
Alexandre Bailon2d535b22017-02-15 14:56:34 +0100984 .first_completion_queue = 93,
985 .qmgr_num_pend = 5,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200986};
987
Alexandre Bailone3fa49a2017-01-30 18:49:20 +0100988static const struct cppi_glue_infos da8xx_usb_infos = {
989 .queues_rx = da8xx_usb_queues_rx,
990 .queues_tx = da8xx_usb_queues_tx,
991 .td_queue = { .submit = 31, .complete = 0 },
992 .first_completion_queue = 24,
993 .qmgr_num_pend = 2,
994};
995
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200996static const struct of_device_id cppi41_dma_ids[] = {
Alexandre Bailone1f40bf2017-02-15 14:56:33 +0100997 { .compatible = "ti,am3359-cppi41", .data = &am335x_usb_infos},
Alexandre Bailone3fa49a2017-01-30 18:49:20 +0100998 { .compatible = "ti,da830-cppi41", .data = &da8xx_usb_infos},
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200999 {},
1000};
1001MODULE_DEVICE_TABLE(of, cppi41_dma_ids);
1002
Daniel Macke327e212013-09-22 16:50:00 +02001003static const struct cppi_glue_infos *get_glue_info(struct device *dev)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001004{
1005 const struct of_device_id *of_id;
1006
Daniel Macke327e212013-09-22 16:50:00 +02001007 of_id = of_match_node(cppi41_dma_ids, dev->of_node);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001008 if (!of_id)
1009 return NULL;
1010 return of_id->data;
1011}
1012
Felipe Balbiffeb13a2015-04-08 11:45:42 -05001013#define CPPI41_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1014 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
1015 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
1016 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1017
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001018static int cppi41_dma_probe(struct platform_device *pdev)
1019{
1020 struct cppi41_dd *cdd;
Daniel Mack717d8182013-09-22 16:50:02 +02001021 struct device *dev = &pdev->dev;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001022 const struct cppi_glue_infos *glue_info;
Alexandre Bailon6ee60242017-02-15 14:56:32 +01001023 int index;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001024 int irq;
1025 int ret;
1026
Daniel Mack717d8182013-09-22 16:50:02 +02001027 glue_info = get_glue_info(dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001028 if (!glue_info)
1029 return -EINVAL;
1030
Kiran Padwalf0f3b5f2014-09-24 15:53:46 +05301031 cdd = devm_kzalloc(&pdev->dev, sizeof(*cdd), GFP_KERNEL);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001032 if (!cdd)
1033 return -ENOMEM;
1034
1035 dma_cap_set(DMA_SLAVE, cdd->ddev.cap_mask);
1036 cdd->ddev.device_alloc_chan_resources = cppi41_dma_alloc_chan_resources;
1037 cdd->ddev.device_free_chan_resources = cppi41_dma_free_chan_resources;
1038 cdd->ddev.device_tx_status = cppi41_dma_tx_status;
1039 cdd->ddev.device_issue_pending = cppi41_dma_issue_pending;
1040 cdd->ddev.device_prep_slave_sg = cppi41_dma_prep_slave_sg;
Maxime Ripard3b5a03a2014-11-17 14:42:10 +01001041 cdd->ddev.device_terminate_all = cppi41_stop_chan;
Felipe Balbiffeb13a2015-04-08 11:45:42 -05001042 cdd->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1043 cdd->ddev.src_addr_widths = CPPI41_DMA_BUSWIDTHS;
1044 cdd->ddev.dst_addr_widths = CPPI41_DMA_BUSWIDTHS;
1045 cdd->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Daniel Mack717d8182013-09-22 16:50:02 +02001046 cdd->ddev.dev = dev;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001047 INIT_LIST_HEAD(&cdd->ddev.channels);
1048 cpp41_dma_info.dma_cap = cdd->ddev.cap_mask;
1049
Alexandre Bailon6ee60242017-02-15 14:56:32 +01001050 index = of_property_match_string(dev->of_node,
1051 "reg-names", "controller");
1052 if (index < 0)
1053 return index;
1054
1055 cdd->ctrl_mem = of_iomap(dev->of_node, index);
1056 cdd->sched_mem = of_iomap(dev->of_node, index + 1);
1057 cdd->qmgr_mem = of_iomap(dev->of_node, index + 2);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001058 spin_lock_init(&cdd->lock);
1059 INIT_LIST_HEAD(&cdd->pending);
1060
1061 platform_set_drvdata(pdev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001062
Alexandre Bailon6ee60242017-02-15 14:56:32 +01001063 if (!cdd->ctrl_mem || !cdd->sched_mem ||
Kiran Padwalf0f3b5f2014-09-24 15:53:46 +05301064 !cdd->qmgr_mem)
1065 return -ENXIO;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001066
Daniel Mack717d8182013-09-22 16:50:02 +02001067 pm_runtime_enable(dev);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001068 pm_runtime_set_autosuspend_delay(dev, 100);
1069 pm_runtime_use_autosuspend(dev);
Daniel Mack717d8182013-09-22 16:50:02 +02001070 ret = pm_runtime_get_sync(dev);
Sebastian Andrzej Siewiorcbf1e562013-10-22 12:14:06 +02001071 if (ret < 0)
Sebastian Andrzej Siewiord6aafa22013-08-20 18:35:53 +02001072 goto err_get_sync;
1073
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001074 cdd->queues_rx = glue_info->queues_rx;
1075 cdd->queues_tx = glue_info->queues_tx;
1076 cdd->td_queue = glue_info->td_queue;
Alexandre Bailon2d535b22017-02-15 14:56:34 +01001077 cdd->qmgr_num_pend = glue_info->qmgr_num_pend;
1078 cdd->first_completion_queue = glue_info->first_completion_queue;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001079
Alexandre Bailon5e46fe92017-02-15 14:56:35 +01001080 ret = of_property_read_u32(dev->of_node,
1081 "#dma-channels", &cdd->n_chans);
1082 if (ret)
1083 goto err_get_n_chans;
1084
Daniel Mack717d8182013-09-22 16:50:02 +02001085 ret = init_cppi41(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001086 if (ret)
1087 goto err_init_cppi;
1088
Daniel Mack717d8182013-09-22 16:50:02 +02001089 ret = cppi41_add_chans(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001090 if (ret)
1091 goto err_chans;
1092
Daniel Mack717d8182013-09-22 16:50:02 +02001093 irq = irq_of_parse_and_map(dev->of_node, 0);
Julia Lawallf3b77722013-12-29 23:47:23 +01001094 if (!irq) {
1095 ret = -EINVAL;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001096 goto err_irq;
Julia Lawallf3b77722013-12-29 23:47:23 +01001097 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001098
Alexandre Bailona15382b2017-02-15 14:56:36 +01001099 ret = devm_request_irq(&pdev->dev, irq, cppi41_irq, IRQF_SHARED,
Daniel Mack717d8182013-09-22 16:50:02 +02001100 dev_name(dev), cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001101 if (ret)
1102 goto err_irq;
1103 cdd->irq = irq;
1104
1105 ret = dma_async_device_register(&cdd->ddev);
1106 if (ret)
1107 goto err_dma_reg;
1108
Daniel Mack717d8182013-09-22 16:50:02 +02001109 ret = of_dma_controller_register(dev->of_node,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001110 cppi41_dma_xlate, &cpp41_dma_info);
1111 if (ret)
1112 goto err_of;
1113
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001114 pm_runtime_mark_last_busy(dev);
1115 pm_runtime_put_autosuspend(dev);
1116
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001117 return 0;
1118err_of:
1119 dma_async_device_unregister(&cdd->ddev);
1120err_dma_reg:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001121err_irq:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001122 cleanup_chans(cdd);
1123err_chans:
Daniel Mack717d8182013-09-22 16:50:02 +02001124 deinit_cppi41(dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001125err_init_cppi:
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001126 pm_runtime_dont_use_autosuspend(dev);
Alexandre Bailon5e46fe92017-02-15 14:56:35 +01001127err_get_n_chans:
Sebastian Andrzej Siewiord6aafa22013-08-20 18:35:53 +02001128err_get_sync:
Tony Lindgrend5afc1b2016-11-16 10:24:15 -08001129 pm_runtime_put_sync(dev);
Daniel Mack717d8182013-09-22 16:50:02 +02001130 pm_runtime_disable(dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001131 iounmap(cdd->ctrl_mem);
1132 iounmap(cdd->sched_mem);
1133 iounmap(cdd->qmgr_mem);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001134 return ret;
1135}
1136
1137static int cppi41_dma_remove(struct platform_device *pdev)
1138{
1139 struct cppi41_dd *cdd = platform_get_drvdata(pdev);
Tony Lindgren12f59082016-11-09 09:47:58 -07001140 int error;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001141
Tony Lindgren12f59082016-11-09 09:47:58 -07001142 error = pm_runtime_get_sync(&pdev->dev);
1143 if (error < 0)
1144 dev_err(&pdev->dev, "%s could not pm_runtime_get: %i\n",
1145 __func__, error);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001146 of_dma_controller_free(pdev->dev.of_node);
1147 dma_async_device_unregister(&cdd->ddev);
1148
Kiran Padwalf0f3b5f2014-09-24 15:53:46 +05301149 devm_free_irq(&pdev->dev, cdd->irq, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001150 cleanup_chans(cdd);
Daniel Mackb46ce4d2013-09-22 16:50:01 +02001151 deinit_cppi41(&pdev->dev, cdd);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001152 iounmap(cdd->ctrl_mem);
1153 iounmap(cdd->sched_mem);
1154 iounmap(cdd->qmgr_mem);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001155 pm_runtime_dont_use_autosuspend(&pdev->dev);
1156 pm_runtime_put_sync(&pdev->dev);
Sebastian Andrzej Siewiord6aafa22013-08-20 18:35:53 +02001157 pm_runtime_disable(&pdev->dev);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001158 return 0;
1159}
1160
Arnd Bergmann522ef612016-09-06 15:20:05 +02001161static int __maybe_unused cppi41_suspend(struct device *dev)
Daniel Mackf97b98d2013-09-22 16:50:04 +02001162{
1163 struct cppi41_dd *cdd = dev_get_drvdata(dev);
1164
Daniel Mackf8964962013-10-22 12:14:03 +02001165 cdd->dma_tdfdq = cppi_readl(cdd->ctrl_mem + DMA_TDFDQ);
Daniel Mackf97b98d2013-09-22 16:50:04 +02001166 disable_sched(cdd);
1167
1168 return 0;
1169}
1170
Arnd Bergmann522ef612016-09-06 15:20:05 +02001171static int __maybe_unused cppi41_resume(struct device *dev)
Daniel Mackf97b98d2013-09-22 16:50:04 +02001172{
1173 struct cppi41_dd *cdd = dev_get_drvdata(dev);
Daniel Mackf8964962013-10-22 12:14:03 +02001174 struct cppi41_channel *c;
Daniel Mackf97b98d2013-09-22 16:50:04 +02001175 int i;
1176
1177 for (i = 0; i < DESCS_AREAS; i++)
1178 cppi_writel(cdd->descs_phys, cdd->qmgr_mem + QMGR_MEMBASE(i));
1179
Daniel Mackf8964962013-10-22 12:14:03 +02001180 list_for_each_entry(c, &cdd->ddev.channels, chan.device_node)
1181 if (!c->is_tx)
1182 cppi_writel(c->q_num, c->gcr_reg + RXHPCRA0);
1183
Daniel Mackf97b98d2013-09-22 16:50:04 +02001184 init_sched(cdd);
Daniel Mackf8964962013-10-22 12:14:03 +02001185
1186 cppi_writel(cdd->dma_tdfdq, cdd->ctrl_mem + DMA_TDFDQ);
1187 cppi_writel(cdd->scratch_phys, cdd->qmgr_mem + QMGR_LRAM0_BASE);
1188 cppi_writel(QMGR_SCRATCH_SIZE, cdd->qmgr_mem + QMGR_LRAM_SIZE);
1189 cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM1_BASE);
1190
Daniel Mackf97b98d2013-09-22 16:50:04 +02001191 return 0;
1192}
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001193
Arnd Bergmann522ef612016-09-06 15:20:05 +02001194static int __maybe_unused cppi41_runtime_suspend(struct device *dev)
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001195{
1196 struct cppi41_dd *cdd = dev_get_drvdata(dev);
Tony Lindgren362f4562017-01-19 08:49:08 -08001197 unsigned long flags;
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001198
Tony Lindgren362f4562017-01-19 08:49:08 -08001199 spin_lock_irqsave(&cdd->lock, flags);
1200 cdd->is_suspended = true;
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001201 WARN_ON(!list_empty(&cdd->pending));
Tony Lindgren362f4562017-01-19 08:49:08 -08001202 spin_unlock_irqrestore(&cdd->lock, flags);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001203
1204 return 0;
1205}
1206
Arnd Bergmann522ef612016-09-06 15:20:05 +02001207static int __maybe_unused cppi41_runtime_resume(struct device *dev)
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001208{
1209 struct cppi41_dd *cdd = dev_get_drvdata(dev);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001210 unsigned long flags;
1211
1212 spin_lock_irqsave(&cdd->lock, flags);
Tony Lindgren362f4562017-01-19 08:49:08 -08001213 cdd->is_suspended = false;
1214 cppi41_run_queue(cdd);
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001215 spin_unlock_irqrestore(&cdd->lock, flags);
1216
1217 return 0;
1218}
Daniel Mackf97b98d2013-09-22 16:50:04 +02001219
Tony Lindgrenfdea2d02016-08-31 07:19:59 -07001220static const struct dev_pm_ops cppi41_pm_ops = {
1221 SET_LATE_SYSTEM_SLEEP_PM_OPS(cppi41_suspend, cppi41_resume)
1222 SET_RUNTIME_PM_OPS(cppi41_runtime_suspend,
1223 cppi41_runtime_resume,
1224 NULL)
1225};
Daniel Mackf97b98d2013-09-22 16:50:04 +02001226
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001227static struct platform_driver cpp41_dma_driver = {
1228 .probe = cppi41_dma_probe,
1229 .remove = cppi41_dma_remove,
1230 .driver = {
1231 .name = "cppi41-dma-engine",
Daniel Mackf97b98d2013-09-22 16:50:04 +02001232 .pm = &cppi41_pm_ops,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001233 .of_match_table = of_match_ptr(cppi41_dma_ids),
1234 },
1235};
1236
1237module_platform_driver(cpp41_dma_driver);
1238MODULE_LICENSE("GPL");
1239MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");