1 #include <linux/delay.h>
2 #include <linux/dmaengine.h>
3 #include <linux/dma-mapping.h>
4 #include <linux/platform_device.h>
5 #include <linux/module.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>
13 #include <linux/pm_runtime.h>
14 #include "dmaengine.h"
17 #define DESC_TYPE_HOST 0x10
18 #define DESC_TYPE_TEARD 0x13
20 #define TD_DESC_IS_RX (1 << 16)
21 #define TD_DESC_DMA_NUM 10
23 #define DESC_LENGTH_BITS_NUM 21
25 #define DESC_TYPE_USB (5 << 26)
26 #define DESC_PD_COMPLETE (1 << 31)
30 #define DMA_TXGCR(x) (0x800 + (x) * 0x20)
31 #define DMA_RXGCR(x) (0x808 + (x) * 0x20)
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)
40 #define DMA_SCHED_CTRL 0
41 #define DMA_SCHED_CTRL_EN (1 << 31)
42 #define DMA_SCHED_WORD(x) ((x) * 4 + 0x800)
44 #define SCHED_ENTRY0_CHAN(x) ((x) << 0)
45 #define SCHED_ENTRY0_IS_RX (1 << 7)
47 #define SCHED_ENTRY1_CHAN(x) ((x) << 8)
48 #define SCHED_ENTRY1_IS_RX (1 << 15)
50 #define SCHED_ENTRY2_CHAN(x) ((x) << 16)
51 #define SCHED_ENTRY2_IS_RX (1 << 23)
53 #define SCHED_ENTRY3_CHAN(x) ((x) << 24)
54 #define SCHED_ENTRY3_IS_RX (1 << 31)
57 /* 4 KiB of memory for descriptors, 2 for each endpoint */
58 #define ALLOC_DECS_NUM 128
60 #define TOTAL_DESCS_NUM (ALLOC_DECS_NUM * DESCS_AREAS)
61 #define QMGR_SCRATCH_SIZE (TOTAL_DESCS_NUM * 4)
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
71 #define QMGR_NUM_PEND 5
72 #define QMGR_PEND(x) (0x90 + (x) * 4)
74 #define QMGR_PENDING_SLOT_Q(x) (x / 32)
75 #define QMGR_PENDING_BIT_Q(x) (x % 32)
77 #define QMGR_QUEUE_A(n) (0x2000 + (n) * 0x10)
78 #define QMGR_QUEUE_B(n) (0x2004 + (n) * 0x10)
79 #define QMGR_QUEUE_C(n) (0x2008 + (n) * 0x10)
80 #define QMGR_QUEUE_D(n) (0x200c + (n) * 0x10)
82 /* Packet Descriptor */
83 #define PD2_ZERO_LENGTH (1 << 19)
85 struct cppi41_channel
{
87 struct dma_async_tx_descriptor txd
;
88 struct cppi41_dd
*cdd
;
89 struct cppi41_desc
*desc
;
91 void __iomem
*gcr_reg
;
96 unsigned int q_comp_num
;
97 unsigned int port_num
;
100 unsigned td_queued
:1;
102 unsigned td_desc_seen
:1;
104 struct list_head node
; /* Node for pending list */
124 struct dma_device ddev
;
127 dma_addr_t scratch_phys
;
129 struct cppi41_desc
*cd
;
130 dma_addr_t descs_phys
;
132 struct cppi41_channel
*chan_busy
[ALLOC_DECS_NUM
];
134 void __iomem
*usbss_mem
;
135 void __iomem
*ctrl_mem
;
136 void __iomem
*sched_mem
;
137 void __iomem
*qmgr_mem
;
139 const struct chan_queues
*queues_rx
;
140 const struct chan_queues
*queues_tx
;
141 struct chan_queues td_queue
;
143 struct list_head pending
; /* Pending queued transfers */
144 spinlock_t lock
; /* Lock for pending list */
146 /* context for suspend/resume */
147 unsigned int dma_tdfdq
;
152 #define FIST_COMPLETION_QUEUE 93
153 static struct chan_queues usb_queues_tx
[] = {
155 [ 0] = { .submit
= 32, .complete
= 93},
156 [ 1] = { .submit
= 34, .complete
= 94},
157 [ 2] = { .submit
= 36, .complete
= 95},
158 [ 3] = { .submit
= 38, .complete
= 96},
159 [ 4] = { .submit
= 40, .complete
= 97},
160 [ 5] = { .submit
= 42, .complete
= 98},
161 [ 6] = { .submit
= 44, .complete
= 99},
162 [ 7] = { .submit
= 46, .complete
= 100},
163 [ 8] = { .submit
= 48, .complete
= 101},
164 [ 9] = { .submit
= 50, .complete
= 102},
165 [10] = { .submit
= 52, .complete
= 103},
166 [11] = { .submit
= 54, .complete
= 104},
167 [12] = { .submit
= 56, .complete
= 105},
168 [13] = { .submit
= 58, .complete
= 106},
169 [14] = { .submit
= 60, .complete
= 107},
172 [15] = { .submit
= 62, .complete
= 125},
173 [16] = { .submit
= 64, .complete
= 126},
174 [17] = { .submit
= 66, .complete
= 127},
175 [18] = { .submit
= 68, .complete
= 128},
176 [19] = { .submit
= 70, .complete
= 129},
177 [20] = { .submit
= 72, .complete
= 130},
178 [21] = { .submit
= 74, .complete
= 131},
179 [22] = { .submit
= 76, .complete
= 132},
180 [23] = { .submit
= 78, .complete
= 133},
181 [24] = { .submit
= 80, .complete
= 134},
182 [25] = { .submit
= 82, .complete
= 135},
183 [26] = { .submit
= 84, .complete
= 136},
184 [27] = { .submit
= 86, .complete
= 137},
185 [28] = { .submit
= 88, .complete
= 138},
186 [29] = { .submit
= 90, .complete
= 139},
189 static const struct chan_queues usb_queues_rx
[] = {
191 [ 0] = { .submit
= 1, .complete
= 109},
192 [ 1] = { .submit
= 2, .complete
= 110},
193 [ 2] = { .submit
= 3, .complete
= 111},
194 [ 3] = { .submit
= 4, .complete
= 112},
195 [ 4] = { .submit
= 5, .complete
= 113},
196 [ 5] = { .submit
= 6, .complete
= 114},
197 [ 6] = { .submit
= 7, .complete
= 115},
198 [ 7] = { .submit
= 8, .complete
= 116},
199 [ 8] = { .submit
= 9, .complete
= 117},
200 [ 9] = { .submit
= 10, .complete
= 118},
201 [10] = { .submit
= 11, .complete
= 119},
202 [11] = { .submit
= 12, .complete
= 120},
203 [12] = { .submit
= 13, .complete
= 121},
204 [13] = { .submit
= 14, .complete
= 122},
205 [14] = { .submit
= 15, .complete
= 123},
208 [15] = { .submit
= 16, .complete
= 141},
209 [16] = { .submit
= 17, .complete
= 142},
210 [17] = { .submit
= 18, .complete
= 143},
211 [18] = { .submit
= 19, .complete
= 144},
212 [19] = { .submit
= 20, .complete
= 145},
213 [20] = { .submit
= 21, .complete
= 146},
214 [21] = { .submit
= 22, .complete
= 147},
215 [22] = { .submit
= 23, .complete
= 148},
216 [23] = { .submit
= 24, .complete
= 149},
217 [24] = { .submit
= 25, .complete
= 150},
218 [25] = { .submit
= 26, .complete
= 151},
219 [26] = { .submit
= 27, .complete
= 152},
220 [27] = { .submit
= 28, .complete
= 153},
221 [28] = { .submit
= 29, .complete
= 154},
222 [29] = { .submit
= 30, .complete
= 155},
225 struct cppi_glue_infos
{
226 irqreturn_t (*isr
)(int irq
, void *data
);
227 const struct chan_queues
*queues_rx
;
228 const struct chan_queues
*queues_tx
;
229 struct chan_queues td_queue
;
232 static struct cppi41_channel
*to_cpp41_chan(struct dma_chan
*c
)
234 return container_of(c
, struct cppi41_channel
, chan
);
237 static struct cppi41_channel
*desc_to_chan(struct cppi41_dd
*cdd
, u32 desc
)
239 struct cppi41_channel
*c
;
243 descs_size
= sizeof(struct cppi41_desc
) * ALLOC_DECS_NUM
;
245 if (!((desc
>= cdd
->descs_phys
) &&
246 (desc
< (cdd
->descs_phys
+ descs_size
)))) {
250 desc_num
= (desc
- cdd
->descs_phys
) / sizeof(struct cppi41_desc
);
251 BUG_ON(desc_num
>= ALLOC_DECS_NUM
);
252 c
= cdd
->chan_busy
[desc_num
];
253 cdd
->chan_busy
[desc_num
] = NULL
;
255 /* Usecount for chan_busy[], paired with push_desc_queue() */
256 pm_runtime_put(cdd
->ddev
.dev
);
261 static void cppi_writel(u32 val
, void *__iomem
*mem
)
263 __raw_writel(val
, mem
);
266 static u32
cppi_readl(void *__iomem
*mem
)
268 return __raw_readl(mem
);
271 static u32
pd_trans_len(u32 val
)
273 return val
& ((1 << (DESC_LENGTH_BITS_NUM
+ 1)) - 1);
276 static u32
cppi41_pop_desc(struct cppi41_dd
*cdd
, unsigned queue_num
)
280 desc
= cppi_readl(cdd
->qmgr_mem
+ QMGR_QUEUE_D(queue_num
));
285 static irqreturn_t
cppi41_irq(int irq
, void *data
)
287 struct cppi41_dd
*cdd
= data
;
288 struct cppi41_channel
*c
;
291 for (i
= QMGR_PENDING_SLOT_Q(FIST_COMPLETION_QUEUE
); i
< QMGR_NUM_PEND
;
296 val
= cppi_readl(cdd
->qmgr_mem
+ QMGR_PEND(i
));
297 if (i
== QMGR_PENDING_SLOT_Q(FIST_COMPLETION_QUEUE
) && val
) {
299 /* set corresponding bit for completetion Q 93 */
300 mask
= 1 << QMGR_PENDING_BIT_Q(FIST_COMPLETION_QUEUE
);
301 /* not set all bits for queues less than Q 93 */
303 /* now invert and keep only Q 93+ set */
314 * This should never trigger, see the comments in
317 WARN_ON(cdd
->is_suspended
);
320 val
&= ~(1 << q_num
);
322 desc
= cppi41_pop_desc(cdd
, q_num
);
323 c
= desc_to_chan(cdd
, desc
);
325 pr_err("%s() q %d desc %08x\n", __func__
,
330 if (c
->desc
->pd2
& PD2_ZERO_LENGTH
)
333 len
= pd_trans_len(c
->desc
->pd0
);
335 c
->residue
= pd_trans_len(c
->desc
->pd6
) - len
;
336 dma_cookie_complete(&c
->txd
);
337 dmaengine_desc_get_callback_invoke(&c
->txd
, NULL
);
343 static dma_cookie_t
cppi41_tx_submit(struct dma_async_tx_descriptor
*tx
)
347 cookie
= dma_cookie_assign(tx
);
352 static int cppi41_dma_alloc_chan_resources(struct dma_chan
*chan
)
354 struct cppi41_channel
*c
= to_cpp41_chan(chan
);
355 struct cppi41_dd
*cdd
= c
->cdd
;
358 error
= pm_runtime_get_sync(cdd
->ddev
.dev
);
360 dev_err(cdd
->ddev
.dev
, "%s pm runtime get: %i\n",
362 pm_runtime_put_noidle(cdd
->ddev
.dev
);
367 dma_cookie_init(chan
);
368 dma_async_tx_descriptor_init(&c
->txd
, chan
);
369 c
->txd
.tx_submit
= cppi41_tx_submit
;
372 cppi_writel(c
->q_num
, c
->gcr_reg
+ RXHPCRA0
);
374 pm_runtime_mark_last_busy(cdd
->ddev
.dev
);
375 pm_runtime_put_autosuspend(cdd
->ddev
.dev
);
380 static void cppi41_dma_free_chan_resources(struct dma_chan
*chan
)
382 struct cppi41_channel
*c
= to_cpp41_chan(chan
);
383 struct cppi41_dd
*cdd
= c
->cdd
;
386 error
= pm_runtime_get_sync(cdd
->ddev
.dev
);
388 pm_runtime_put_noidle(cdd
->ddev
.dev
);
393 WARN_ON(!list_empty(&cdd
->pending
));
395 pm_runtime_mark_last_busy(cdd
->ddev
.dev
);
396 pm_runtime_put_autosuspend(cdd
->ddev
.dev
);
399 static enum dma_status
cppi41_dma_tx_status(struct dma_chan
*chan
,
400 dma_cookie_t cookie
, struct dma_tx_state
*txstate
)
402 struct cppi41_channel
*c
= to_cpp41_chan(chan
);
406 ret
= dma_cookie_status(chan
, cookie
, txstate
);
407 if (txstate
&& ret
== DMA_COMPLETE
)
408 txstate
->residue
= c
->residue
;
414 static void push_desc_queue(struct cppi41_channel
*c
)
416 struct cppi41_dd
*cdd
= c
->cdd
;
423 reg
= GCR_CHAN_ENABLE
;
425 reg
|= GCR_STARV_RETRY
;
426 reg
|= GCR_DESC_TYPE_HOST
;
427 reg
|= c
->q_comp_num
;
430 cppi_writel(reg
, c
->gcr_reg
);
433 * We don't use writel() but __raw_writel() so we have to make sure
434 * that the DMA descriptor in coherent memory made to the main memory
435 * before starting the dma engine.
440 * DMA transfers can take at least 200ms to complete with USB mass
441 * storage connected. To prevent autosuspend timeouts, we must use
442 * pm_runtime_get/put() when chan_busy[] is modified. This will get
443 * cleared in desc_to_chan() or cppi41_stop_chan() depending on the
444 * outcome of the transfer.
446 pm_runtime_get(cdd
->ddev
.dev
);
448 desc_phys
= lower_32_bits(c
->desc_phys
);
449 desc_num
= (desc_phys
- cdd
->descs_phys
) / sizeof(struct cppi41_desc
);
450 WARN_ON(cdd
->chan_busy
[desc_num
]);
451 cdd
->chan_busy
[desc_num
] = c
;
453 reg
= (sizeof(struct cppi41_desc
) - 24) / 4;
455 cppi_writel(reg
, cdd
->qmgr_mem
+ QMGR_QUEUE_D(c
->q_num
));
459 * Caller must hold cdd->lock to prevent push_desc_queue()
460 * getting called out of order. We have both cppi41_dma_issue_pending()
461 * and cppi41_runtime_resume() call this function.
463 static void cppi41_run_queue(struct cppi41_dd
*cdd
)
465 struct cppi41_channel
*c
, *_c
;
467 list_for_each_entry_safe(c
, _c
, &cdd
->pending
, node
) {
473 static void cppi41_dma_issue_pending(struct dma_chan
*chan
)
475 struct cppi41_channel
*c
= to_cpp41_chan(chan
);
476 struct cppi41_dd
*cdd
= c
->cdd
;
480 error
= pm_runtime_get(cdd
->ddev
.dev
);
481 if ((error
!= -EINPROGRESS
) && error
< 0) {
482 pm_runtime_put_noidle(cdd
->ddev
.dev
);
483 dev_err(cdd
->ddev
.dev
, "Failed to pm_runtime_get: %i\n",
489 spin_lock_irqsave(&cdd
->lock
, flags
);
490 list_add_tail(&c
->node
, &cdd
->pending
);
491 if (!cdd
->is_suspended
)
492 cppi41_run_queue(cdd
);
493 spin_unlock_irqrestore(&cdd
->lock
, flags
);
495 pm_runtime_mark_last_busy(cdd
->ddev
.dev
);
496 pm_runtime_put_autosuspend(cdd
->ddev
.dev
);
499 static u32
get_host_pd0(u32 length
)
503 reg
= DESC_TYPE_HOST
<< DESC_TYPE
;
509 static u32
get_host_pd1(struct cppi41_channel
*c
)
518 static u32
get_host_pd2(struct cppi41_channel
*c
)
523 reg
|= c
->q_comp_num
;
528 static u32
get_host_pd3(u32 length
)
532 /* PD3 = packet size */
538 static u32
get_host_pd6(u32 length
)
542 /* PD6 buffer size */
543 reg
= DESC_PD_COMPLETE
;
549 static u32
get_host_pd4_or_7(u32 addr
)
558 static u32
get_host_pd5(void)
567 static struct dma_async_tx_descriptor
*cppi41_dma_prep_slave_sg(
568 struct dma_chan
*chan
, struct scatterlist
*sgl
, unsigned sg_len
,
569 enum dma_transfer_direction dir
, unsigned long tx_flags
, void *context
)
571 struct cppi41_channel
*c
= to_cpp41_chan(chan
);
572 struct cppi41_desc
*d
;
573 struct scatterlist
*sg
;
577 for_each_sg(sgl
, sg
, sg_len
, i
) {
581 /* We need to use more than one desc once musb supports sg */
582 addr
= lower_32_bits(sg_dma_address(sg
));
583 len
= sg_dma_len(sg
);
585 d
->pd0
= get_host_pd0(len
);
586 d
->pd1
= get_host_pd1(c
);
587 d
->pd2
= get_host_pd2(c
);
588 d
->pd3
= get_host_pd3(len
);
589 d
->pd4
= get_host_pd4_or_7(addr
);
590 d
->pd5
= get_host_pd5();
591 d
->pd6
= get_host_pd6(len
);
592 d
->pd7
= get_host_pd4_or_7(addr
);
600 static void cppi41_compute_td_desc(struct cppi41_desc
*d
)
602 d
->pd0
= DESC_TYPE_TEARD
<< DESC_TYPE
;
605 static int cppi41_tear_down_chan(struct cppi41_channel
*c
)
607 struct dmaengine_result abort_result
;
608 struct cppi41_dd
*cdd
= c
->cdd
;
609 struct cppi41_desc
*td
;
615 td
+= cdd
->first_td_desc
;
617 td_desc_phys
= cdd
->descs_phys
;
618 td_desc_phys
+= cdd
->first_td_desc
* sizeof(struct cppi41_desc
);
621 cppi41_compute_td_desc(td
);
624 reg
= (sizeof(struct cppi41_desc
) - 24) / 4;
626 cppi_writel(reg
, cdd
->qmgr_mem
+
627 QMGR_QUEUE_D(cdd
->td_queue
.submit
));
629 reg
= GCR_CHAN_ENABLE
;
631 reg
|= GCR_STARV_RETRY
;
632 reg
|= GCR_DESC_TYPE_HOST
;
633 reg
|= c
->q_comp_num
;
636 cppi_writel(reg
, c
->gcr_reg
);
641 if (!c
->td_seen
|| !c
->td_desc_seen
) {
643 desc_phys
= cppi41_pop_desc(cdd
, cdd
->td_queue
.complete
);
645 desc_phys
= cppi41_pop_desc(cdd
, c
->q_comp_num
);
647 if (desc_phys
== c
->desc_phys
) {
650 } else if (desc_phys
== td_desc_phys
) {
655 WARN_ON((pd0
>> DESC_TYPE
) != DESC_TYPE_TEARD
);
656 WARN_ON(!c
->is_tx
&& !(pd0
& TD_DESC_IS_RX
));
657 WARN_ON((pd0
& 0x1f) != c
->port_num
);
659 } else if (desc_phys
) {
665 * If the TX descriptor / channel is in use, the caller needs to poke
666 * his TD bit multiple times. After that he hardware releases the
667 * transfer descriptor followed by TD descriptor. Waiting seems not to
668 * cause any difference.
669 * RX seems to be thrown out right away. However once the TearDown
670 * descriptor gets through we are done. If we have seens the transfer
671 * descriptor before the TD we fetch it from enqueue, it has to be
672 * there waiting for us.
674 if (!c
->td_seen
&& c
->td_retry
) {
678 WARN_ON(!c
->td_retry
);
680 if (!c
->td_desc_seen
) {
681 desc_phys
= cppi41_pop_desc(cdd
, c
->q_num
);
683 desc_phys
= cppi41_pop_desc(cdd
, c
->q_comp_num
);
690 cppi_writel(0, c
->gcr_reg
);
692 /* Invoke the callback to do the necessary clean-up */
693 abort_result
.result
= DMA_TRANS_ABORTED
;
694 dma_cookie_complete(&c
->txd
);
695 dmaengine_desc_get_callback_invoke(&c
->txd
, &abort_result
);
700 static int cppi41_stop_chan(struct dma_chan
*chan
)
702 struct cppi41_channel
*c
= to_cpp41_chan(chan
);
703 struct cppi41_dd
*cdd
= c
->cdd
;
708 desc_phys
= lower_32_bits(c
->desc_phys
);
709 desc_num
= (desc_phys
- cdd
->descs_phys
) / sizeof(struct cppi41_desc
);
710 if (!cdd
->chan_busy
[desc_num
])
713 ret
= cppi41_tear_down_chan(c
);
717 WARN_ON(!cdd
->chan_busy
[desc_num
]);
718 cdd
->chan_busy
[desc_num
] = NULL
;
720 /* Usecount for chan_busy[], paired with push_desc_queue() */
721 pm_runtime_put(cdd
->ddev
.dev
);
726 static void cleanup_chans(struct cppi41_dd
*cdd
)
728 while (!list_empty(&cdd
->ddev
.channels
)) {
729 struct cppi41_channel
*cchan
;
731 cchan
= list_first_entry(&cdd
->ddev
.channels
,
732 struct cppi41_channel
, chan
.device_node
);
733 list_del(&cchan
->chan
.device_node
);
738 static int cppi41_add_chans(struct device
*dev
, struct cppi41_dd
*cdd
)
740 struct cppi41_channel
*cchan
;
745 ret
= of_property_read_u32(dev
->of_node
, "#dma-channels",
750 * The channels can only be used as TX or as RX. So we add twice
751 * that much dma channels because USB can only do RX or TX.
755 for (i
= 0; i
< n_chans
; i
++) {
756 cchan
= kzalloc(sizeof(*cchan
), GFP_KERNEL
);
762 cchan
->gcr_reg
= cdd
->ctrl_mem
+ DMA_TXGCR(i
>> 1);
765 cchan
->gcr_reg
= cdd
->ctrl_mem
+ DMA_RXGCR(i
>> 1);
768 cchan
->port_num
= i
>> 1;
769 cchan
->desc
= &cdd
->cd
[i
];
770 cchan
->desc_phys
= cdd
->descs_phys
;
771 cchan
->desc_phys
+= i
* sizeof(struct cppi41_desc
);
772 cchan
->chan
.device
= &cdd
->ddev
;
773 list_add_tail(&cchan
->chan
.device_node
, &cdd
->ddev
.channels
);
775 cdd
->first_td_desc
= n_chans
;
783 static void purge_descs(struct device
*dev
, struct cppi41_dd
*cdd
)
785 unsigned int mem_decs
;
788 mem_decs
= ALLOC_DECS_NUM
* sizeof(struct cppi41_desc
);
790 for (i
= 0; i
< DESCS_AREAS
; i
++) {
792 cppi_writel(0, cdd
->qmgr_mem
+ QMGR_MEMBASE(i
));
793 cppi_writel(0, cdd
->qmgr_mem
+ QMGR_MEMCTRL(i
));
795 dma_free_coherent(dev
, mem_decs
, cdd
->cd
,
800 static void disable_sched(struct cppi41_dd
*cdd
)
802 cppi_writel(0, cdd
->sched_mem
+ DMA_SCHED_CTRL
);
805 static void deinit_cppi41(struct device
*dev
, struct cppi41_dd
*cdd
)
809 purge_descs(dev
, cdd
);
811 cppi_writel(0, cdd
->qmgr_mem
+ QMGR_LRAM0_BASE
);
812 cppi_writel(0, cdd
->qmgr_mem
+ QMGR_LRAM0_BASE
);
813 dma_free_coherent(dev
, QMGR_SCRATCH_SIZE
, cdd
->qmgr_scratch
,
817 static int init_descs(struct device
*dev
, struct cppi41_dd
*cdd
)
819 unsigned int desc_size
;
820 unsigned int mem_decs
;
825 BUILD_BUG_ON(sizeof(struct cppi41_desc
) &
826 (sizeof(struct cppi41_desc
) - 1));
827 BUILD_BUG_ON(sizeof(struct cppi41_desc
) < 32);
828 BUILD_BUG_ON(ALLOC_DECS_NUM
< 32);
830 desc_size
= sizeof(struct cppi41_desc
);
831 mem_decs
= ALLOC_DECS_NUM
* desc_size
;
834 for (i
= 0; i
< DESCS_AREAS
; i
++) {
836 reg
= idx
<< QMGR_MEMCTRL_IDX_SH
;
837 reg
|= (ilog2(desc_size
) - 5) << QMGR_MEMCTRL_DESC_SH
;
838 reg
|= ilog2(ALLOC_DECS_NUM
) - 5;
840 BUILD_BUG_ON(DESCS_AREAS
!= 1);
841 cdd
->cd
= dma_alloc_coherent(dev
, mem_decs
,
842 &cdd
->descs_phys
, GFP_KERNEL
);
846 cppi_writel(cdd
->descs_phys
, cdd
->qmgr_mem
+ QMGR_MEMBASE(i
));
847 cppi_writel(reg
, cdd
->qmgr_mem
+ QMGR_MEMCTRL(i
));
849 idx
+= ALLOC_DECS_NUM
;
854 static void init_sched(struct cppi41_dd
*cdd
)
861 cppi_writel(0, cdd
->sched_mem
+ DMA_SCHED_CTRL
);
862 for (ch
= 0; ch
< 15 * 2; ch
+= 2) {
864 reg
= SCHED_ENTRY0_CHAN(ch
);
865 reg
|= SCHED_ENTRY1_CHAN(ch
) | SCHED_ENTRY1_IS_RX
;
867 reg
|= SCHED_ENTRY2_CHAN(ch
+ 1);
868 reg
|= SCHED_ENTRY3_CHAN(ch
+ 1) | SCHED_ENTRY3_IS_RX
;
869 cppi_writel(reg
, cdd
->sched_mem
+ DMA_SCHED_WORD(word
));
872 reg
= 15 * 2 * 2 - 1;
873 reg
|= DMA_SCHED_CTRL_EN
;
874 cppi_writel(reg
, cdd
->sched_mem
+ DMA_SCHED_CTRL
);
877 static int init_cppi41(struct device
*dev
, struct cppi41_dd
*cdd
)
881 BUILD_BUG_ON(QMGR_SCRATCH_SIZE
> ((1 << 14) - 1));
882 cdd
->qmgr_scratch
= dma_alloc_coherent(dev
, QMGR_SCRATCH_SIZE
,
883 &cdd
->scratch_phys
, GFP_KERNEL
);
884 if (!cdd
->qmgr_scratch
)
887 cppi_writel(cdd
->scratch_phys
, cdd
->qmgr_mem
+ QMGR_LRAM0_BASE
);
888 cppi_writel(QMGR_SCRATCH_SIZE
, cdd
->qmgr_mem
+ QMGR_LRAM_SIZE
);
889 cppi_writel(0, cdd
->qmgr_mem
+ QMGR_LRAM1_BASE
);
891 ret
= init_descs(dev
, cdd
);
895 cppi_writel(cdd
->td_queue
.submit
, cdd
->ctrl_mem
+ DMA_TDFDQ
);
899 deinit_cppi41(dev
, cdd
);
903 static struct platform_driver cpp41_dma_driver
;
905 * The param format is:
913 static bool cpp41_dma_filter_fn(struct dma_chan
*chan
, void *param
)
915 struct cppi41_channel
*cchan
;
916 struct cppi41_dd
*cdd
;
917 const struct chan_queues
*queues
;
920 if (chan
->device
->dev
->driver
!= &cpp41_dma_driver
.driver
)
923 cchan
= to_cpp41_chan(chan
);
925 if (cchan
->port_num
!= num
[INFO_PORT
])
928 if (cchan
->is_tx
&& !num
[INFO_IS_TX
])
932 queues
= cdd
->queues_tx
;
934 queues
= cdd
->queues_rx
;
936 BUILD_BUG_ON(ARRAY_SIZE(usb_queues_rx
) != ARRAY_SIZE(usb_queues_tx
));
937 if (WARN_ON(cchan
->port_num
> ARRAY_SIZE(usb_queues_rx
)))
940 cchan
->q_num
= queues
[cchan
->port_num
].submit
;
941 cchan
->q_comp_num
= queues
[cchan
->port_num
].complete
;
945 static struct of_dma_filter_info cpp41_dma_info
= {
946 .filter_fn
= cpp41_dma_filter_fn
,
949 static struct dma_chan
*cppi41_dma_xlate(struct of_phandle_args
*dma_spec
,
950 struct of_dma
*ofdma
)
952 int count
= dma_spec
->args_count
;
953 struct of_dma_filter_info
*info
= ofdma
->of_dma_data
;
955 if (!info
|| !info
->filter_fn
)
961 return dma_request_channel(info
->dma_cap
, info
->filter_fn
,
965 static const struct cppi_glue_infos usb_infos
= {
967 .queues_rx
= usb_queues_rx
,
968 .queues_tx
= usb_queues_tx
,
969 .td_queue
= { .submit
= 31, .complete
= 0 },
972 static const struct of_device_id cppi41_dma_ids
[] = {
973 { .compatible
= "ti,am3359-cppi41", .data
= &usb_infos
},
976 MODULE_DEVICE_TABLE(of
, cppi41_dma_ids
);
978 static const struct cppi_glue_infos
*get_glue_info(struct device
*dev
)
980 const struct of_device_id
*of_id
;
982 of_id
= of_match_node(cppi41_dma_ids
, dev
->of_node
);
988 #define CPPI41_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
989 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
990 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
991 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
993 static int cppi41_dma_probe(struct platform_device
*pdev
)
995 struct cppi41_dd
*cdd
;
996 struct device
*dev
= &pdev
->dev
;
997 const struct cppi_glue_infos
*glue_info
;
1001 glue_info
= get_glue_info(dev
);
1005 cdd
= devm_kzalloc(&pdev
->dev
, sizeof(*cdd
), GFP_KERNEL
);
1009 dma_cap_set(DMA_SLAVE
, cdd
->ddev
.cap_mask
);
1010 cdd
->ddev
.device_alloc_chan_resources
= cppi41_dma_alloc_chan_resources
;
1011 cdd
->ddev
.device_free_chan_resources
= cppi41_dma_free_chan_resources
;
1012 cdd
->ddev
.device_tx_status
= cppi41_dma_tx_status
;
1013 cdd
->ddev
.device_issue_pending
= cppi41_dma_issue_pending
;
1014 cdd
->ddev
.device_prep_slave_sg
= cppi41_dma_prep_slave_sg
;
1015 cdd
->ddev
.device_terminate_all
= cppi41_stop_chan
;
1016 cdd
->ddev
.directions
= BIT(DMA_DEV_TO_MEM
) | BIT(DMA_MEM_TO_DEV
);
1017 cdd
->ddev
.src_addr_widths
= CPPI41_DMA_BUSWIDTHS
;
1018 cdd
->ddev
.dst_addr_widths
= CPPI41_DMA_BUSWIDTHS
;
1019 cdd
->ddev
.residue_granularity
= DMA_RESIDUE_GRANULARITY_BURST
;
1020 cdd
->ddev
.dev
= dev
;
1021 INIT_LIST_HEAD(&cdd
->ddev
.channels
);
1022 cpp41_dma_info
.dma_cap
= cdd
->ddev
.cap_mask
;
1024 cdd
->usbss_mem
= of_iomap(dev
->of_node
, 0);
1025 cdd
->ctrl_mem
= of_iomap(dev
->of_node
, 1);
1026 cdd
->sched_mem
= of_iomap(dev
->of_node
, 2);
1027 cdd
->qmgr_mem
= of_iomap(dev
->of_node
, 3);
1028 spin_lock_init(&cdd
->lock
);
1029 INIT_LIST_HEAD(&cdd
->pending
);
1031 platform_set_drvdata(pdev
, cdd
);
1033 if (!cdd
->usbss_mem
|| !cdd
->ctrl_mem
|| !cdd
->sched_mem
||
1037 pm_runtime_enable(dev
);
1038 pm_runtime_set_autosuspend_delay(dev
, 100);
1039 pm_runtime_use_autosuspend(dev
);
1040 ret
= pm_runtime_get_sync(dev
);
1044 cdd
->queues_rx
= glue_info
->queues_rx
;
1045 cdd
->queues_tx
= glue_info
->queues_tx
;
1046 cdd
->td_queue
= glue_info
->td_queue
;
1048 ret
= init_cppi41(dev
, cdd
);
1052 ret
= cppi41_add_chans(dev
, cdd
);
1056 irq
= irq_of_parse_and_map(dev
->of_node
, 0);
1062 ret
= devm_request_irq(&pdev
->dev
, irq
, glue_info
->isr
, IRQF_SHARED
,
1063 dev_name(dev
), cdd
);
1068 ret
= dma_async_device_register(&cdd
->ddev
);
1072 ret
= of_dma_controller_register(dev
->of_node
,
1073 cppi41_dma_xlate
, &cpp41_dma_info
);
1077 pm_runtime_mark_last_busy(dev
);
1078 pm_runtime_put_autosuspend(dev
);
1082 dma_async_device_unregister(&cdd
->ddev
);
1087 deinit_cppi41(dev
, cdd
);
1089 pm_runtime_dont_use_autosuspend(dev
);
1091 pm_runtime_put_sync(dev
);
1092 pm_runtime_disable(dev
);
1093 iounmap(cdd
->usbss_mem
);
1094 iounmap(cdd
->ctrl_mem
);
1095 iounmap(cdd
->sched_mem
);
1096 iounmap(cdd
->qmgr_mem
);
1100 static int cppi41_dma_remove(struct platform_device
*pdev
)
1102 struct cppi41_dd
*cdd
= platform_get_drvdata(pdev
);
1105 error
= pm_runtime_get_sync(&pdev
->dev
);
1107 dev_err(&pdev
->dev
, "%s could not pm_runtime_get: %i\n",
1109 of_dma_controller_free(pdev
->dev
.of_node
);
1110 dma_async_device_unregister(&cdd
->ddev
);
1112 devm_free_irq(&pdev
->dev
, cdd
->irq
, cdd
);
1114 deinit_cppi41(&pdev
->dev
, cdd
);
1115 iounmap(cdd
->usbss_mem
);
1116 iounmap(cdd
->ctrl_mem
);
1117 iounmap(cdd
->sched_mem
);
1118 iounmap(cdd
->qmgr_mem
);
1119 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
1120 pm_runtime_put_sync(&pdev
->dev
);
1121 pm_runtime_disable(&pdev
->dev
);
1125 static int __maybe_unused
cppi41_suspend(struct device
*dev
)
1127 struct cppi41_dd
*cdd
= dev_get_drvdata(dev
);
1129 cdd
->dma_tdfdq
= cppi_readl(cdd
->ctrl_mem
+ DMA_TDFDQ
);
1135 static int __maybe_unused
cppi41_resume(struct device
*dev
)
1137 struct cppi41_dd
*cdd
= dev_get_drvdata(dev
);
1138 struct cppi41_channel
*c
;
1141 for (i
= 0; i
< DESCS_AREAS
; i
++)
1142 cppi_writel(cdd
->descs_phys
, cdd
->qmgr_mem
+ QMGR_MEMBASE(i
));
1144 list_for_each_entry(c
, &cdd
->ddev
.channels
, chan
.device_node
)
1146 cppi_writel(c
->q_num
, c
->gcr_reg
+ RXHPCRA0
);
1150 cppi_writel(cdd
->dma_tdfdq
, cdd
->ctrl_mem
+ DMA_TDFDQ
);
1151 cppi_writel(cdd
->scratch_phys
, cdd
->qmgr_mem
+ QMGR_LRAM0_BASE
);
1152 cppi_writel(QMGR_SCRATCH_SIZE
, cdd
->qmgr_mem
+ QMGR_LRAM_SIZE
);
1153 cppi_writel(0, cdd
->qmgr_mem
+ QMGR_LRAM1_BASE
);
1158 static int __maybe_unused
cppi41_runtime_suspend(struct device
*dev
)
1160 struct cppi41_dd
*cdd
= dev_get_drvdata(dev
);
1161 unsigned long flags
;
1163 spin_lock_irqsave(&cdd
->lock
, flags
);
1164 cdd
->is_suspended
= true;
1165 WARN_ON(!list_empty(&cdd
->pending
));
1166 spin_unlock_irqrestore(&cdd
->lock
, flags
);
1171 static int __maybe_unused
cppi41_runtime_resume(struct device
*dev
)
1173 struct cppi41_dd
*cdd
= dev_get_drvdata(dev
);
1174 unsigned long flags
;
1176 spin_lock_irqsave(&cdd
->lock
, flags
);
1177 cdd
->is_suspended
= false;
1178 cppi41_run_queue(cdd
);
1179 spin_unlock_irqrestore(&cdd
->lock
, flags
);
1184 static const struct dev_pm_ops cppi41_pm_ops
= {
1185 SET_LATE_SYSTEM_SLEEP_PM_OPS(cppi41_suspend
, cppi41_resume
)
1186 SET_RUNTIME_PM_OPS(cppi41_runtime_suspend
,
1187 cppi41_runtime_resume
,
1191 static struct platform_driver cpp41_dma_driver
= {
1192 .probe
= cppi41_dma_probe
,
1193 .remove
= cppi41_dma_remove
,
1195 .name
= "cppi41-dma-engine",
1196 .pm
= &cppi41_pm_ops
,
1197 .of_match_table
= of_match_ptr(cppi41_dma_ids
),
1201 module_platform_driver(cpp41_dma_driver
);
1202 MODULE_LICENSE("GPL");
1203 MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");