1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/device.h>
3 #include <linux/dma-mapping.h>
4 #include <linux/dmaengine.h>
5 #include <linux/sizes.h>
6 #include <linux/platform_device.h>
10 #include "musb_core.h"
11 #include "musb_trace.h"
13 #define RNDIS_REG(x) (0x80 + ((x - 1) * 4))
15 #define EP_MODE_AUTOREQ_NONE 0
16 #define EP_MODE_AUTOREQ_ALL_NEOP 1
17 #define EP_MODE_AUTOREQ_ALWAYS 3
19 #define EP_MODE_DMA_TRANSPARENT 0
20 #define EP_MODE_DMA_RNDIS 1
21 #define EP_MODE_DMA_GEN_RNDIS 3
23 #define USB_CTRL_TX_MODE 0x70
24 #define USB_CTRL_RX_MODE 0x74
25 #define USB_CTRL_AUTOREQ 0xd0
26 #define USB_TDOWN 0xd8
28 #define MUSB_DMA_NUM_CHANNELS 15
30 #define DA8XX_USB_MODE 0x10
31 #define DA8XX_USB_AUTOREQ 0x14
32 #define DA8XX_USB_TEARDOWN 0x1c
34 #define DA8XX_DMA_NUM_CHANNELS 4
36 struct cppi41_dma_controller
{
37 struct dma_controller controller
;
38 struct cppi41_dma_channel
*rx_channel
;
39 struct cppi41_dma_channel
*tx_channel
;
40 struct hrtimer early_tx
;
41 struct list_head early_tx_list
;
49 void (*set_dma_mode
)(struct cppi41_dma_channel
*cppi41_channel
,
54 static void save_rx_toggle(struct cppi41_dma_channel
*cppi41_channel
)
59 if (cppi41_channel
->is_tx
)
61 if (!is_host_active(cppi41_channel
->controller
->controller
.musb
))
64 csr
= musb_readw(cppi41_channel
->hw_ep
->regs
, MUSB_RXCSR
);
65 toggle
= csr
& MUSB_RXCSR_H_DATATOGGLE
? 1 : 0;
67 cppi41_channel
->usb_toggle
= toggle
;
70 static void update_rx_toggle(struct cppi41_dma_channel
*cppi41_channel
)
72 struct musb_hw_ep
*hw_ep
= cppi41_channel
->hw_ep
;
73 struct musb
*musb
= hw_ep
->musb
;
77 if (cppi41_channel
->is_tx
)
79 if (!is_host_active(musb
))
82 musb_ep_select(musb
->mregs
, hw_ep
->epnum
);
83 csr
= musb_readw(hw_ep
->regs
, MUSB_RXCSR
);
84 toggle
= csr
& MUSB_RXCSR_H_DATATOGGLE
? 1 : 0;
87 * AM335x Advisory 1.0.13: Due to internal synchronisation error the
88 * data toggle may reset from DATA1 to DATA0 during receiving data from
89 * more than one endpoint.
91 if (!toggle
&& toggle
== cppi41_channel
->usb_toggle
) {
92 csr
|= MUSB_RXCSR_H_DATATOGGLE
| MUSB_RXCSR_H_WR_DATATOGGLE
;
93 musb_writew(cppi41_channel
->hw_ep
->regs
, MUSB_RXCSR
, csr
);
94 musb_dbg(musb
, "Restoring DATA1 toggle.");
97 cppi41_channel
->usb_toggle
= toggle
;
100 static bool musb_is_tx_fifo_empty(struct musb_hw_ep
*hw_ep
)
102 u8 epnum
= hw_ep
->epnum
;
103 struct musb
*musb
= hw_ep
->musb
;
104 void __iomem
*epio
= musb
->endpoints
[epnum
].regs
;
107 musb_ep_select(musb
->mregs
, hw_ep
->epnum
);
108 csr
= musb_readw(epio
, MUSB_TXCSR
);
109 if (csr
& MUSB_TXCSR_TXPKTRDY
)
114 static void cppi41_dma_callback(void *private_data
,
115 const struct dmaengine_result
*result
);
117 static void cppi41_trans_done(struct cppi41_dma_channel
*cppi41_channel
)
119 struct musb_hw_ep
*hw_ep
= cppi41_channel
->hw_ep
;
120 struct musb
*musb
= hw_ep
->musb
;
121 void __iomem
*epio
= hw_ep
->regs
;
124 if (!cppi41_channel
->prog_len
||
125 (cppi41_channel
->channel
.status
== MUSB_DMA_STATUS_FREE
)) {
128 cppi41_channel
->channel
.actual_len
=
129 cppi41_channel
->transferred
;
130 cppi41_channel
->channel
.status
= MUSB_DMA_STATUS_FREE
;
131 cppi41_channel
->channel
.rx_packet_done
= true;
134 * transmit ZLP using PIO mode for transfers which size is
135 * multiple of EP packet size.
137 if (cppi41_channel
->tx_zlp
&& (cppi41_channel
->transferred
%
138 cppi41_channel
->packet_sz
) == 0) {
139 musb_ep_select(musb
->mregs
, hw_ep
->epnum
);
140 csr
= MUSB_TXCSR_MODE
| MUSB_TXCSR_TXPKTRDY
;
141 musb_writew(epio
, MUSB_TXCSR
, csr
);
144 trace_musb_cppi41_done(cppi41_channel
);
145 musb_dma_completion(musb
, hw_ep
->epnum
, cppi41_channel
->is_tx
);
147 /* next iteration, reload */
148 struct dma_chan
*dc
= cppi41_channel
->dc
;
149 struct dma_async_tx_descriptor
*dma_desc
;
150 enum dma_transfer_direction direction
;
153 cppi41_channel
->buf_addr
+= cppi41_channel
->packet_sz
;
155 remain_bytes
= cppi41_channel
->total_len
;
156 remain_bytes
-= cppi41_channel
->transferred
;
157 remain_bytes
= min(remain_bytes
, cppi41_channel
->packet_sz
);
158 cppi41_channel
->prog_len
= remain_bytes
;
160 direction
= cppi41_channel
->is_tx
? DMA_MEM_TO_DEV
162 dma_desc
= dmaengine_prep_slave_single(dc
,
163 cppi41_channel
->buf_addr
,
166 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
167 if (WARN_ON(!dma_desc
))
170 dma_desc
->callback_result
= cppi41_dma_callback
;
171 dma_desc
->callback_param
= &cppi41_channel
->channel
;
172 cppi41_channel
->cookie
= dma_desc
->tx_submit(dma_desc
);
173 trace_musb_cppi41_cont(cppi41_channel
);
174 dma_async_issue_pending(dc
);
176 if (!cppi41_channel
->is_tx
) {
177 musb_ep_select(musb
->mregs
, hw_ep
->epnum
);
178 csr
= musb_readw(epio
, MUSB_RXCSR
);
179 csr
|= MUSB_RXCSR_H_REQPKT
;
180 musb_writew(epio
, MUSB_RXCSR
, csr
);
185 static enum hrtimer_restart
cppi41_recheck_tx_req(struct hrtimer
*timer
)
187 struct cppi41_dma_controller
*controller
;
188 struct cppi41_dma_channel
*cppi41_channel
, *n
;
191 enum hrtimer_restart ret
= HRTIMER_NORESTART
;
193 controller
= container_of(timer
, struct cppi41_dma_controller
,
195 musb
= controller
->controller
.musb
;
197 spin_lock_irqsave(&musb
->lock
, flags
);
198 list_for_each_entry_safe(cppi41_channel
, n
, &controller
->early_tx_list
,
201 struct musb_hw_ep
*hw_ep
= cppi41_channel
->hw_ep
;
203 empty
= musb_is_tx_fifo_empty(hw_ep
);
205 list_del_init(&cppi41_channel
->tx_check
);
206 cppi41_trans_done(cppi41_channel
);
210 if (!list_empty(&controller
->early_tx_list
) &&
211 !hrtimer_is_queued(&controller
->early_tx
)) {
212 ret
= HRTIMER_RESTART
;
213 hrtimer_forward_now(&controller
->early_tx
, 20 * NSEC_PER_USEC
);
216 spin_unlock_irqrestore(&musb
->lock
, flags
);
220 static void cppi41_dma_callback(void *private_data
,
221 const struct dmaengine_result
*result
)
223 struct dma_channel
*channel
= private_data
;
224 struct cppi41_dma_channel
*cppi41_channel
= channel
->private_data
;
225 struct musb_hw_ep
*hw_ep
= cppi41_channel
->hw_ep
;
226 struct cppi41_dma_controller
*controller
;
227 struct musb
*musb
= hw_ep
->musb
;
229 struct dma_tx_state txstate
;
234 controller
= cppi41_channel
->controller
;
235 if (controller
->controller
.dma_callback
)
236 controller
->controller
.dma_callback(&controller
->controller
);
238 if (result
->result
== DMA_TRANS_ABORTED
)
241 spin_lock_irqsave(&musb
->lock
, flags
);
243 dmaengine_tx_status(cppi41_channel
->dc
, cppi41_channel
->cookie
,
245 transferred
= cppi41_channel
->prog_len
- txstate
.residue
;
246 cppi41_channel
->transferred
+= transferred
;
248 trace_musb_cppi41_gb(cppi41_channel
);
249 update_rx_toggle(cppi41_channel
);
251 if (cppi41_channel
->transferred
== cppi41_channel
->total_len
||
252 transferred
< cppi41_channel
->packet_sz
)
253 cppi41_channel
->prog_len
= 0;
255 if (cppi41_channel
->is_tx
) {
258 if (is_host_active(musb
))
259 type
= hw_ep
->out_qh
->type
;
261 type
= hw_ep
->ep_in
.type
;
263 if (type
== USB_ENDPOINT_XFER_ISOC
)
265 * Don't use the early-TX-interrupt workaround below
266 * for Isoch transfter. Since Isoch are periodic
267 * transfer, by the time the next transfer is
268 * scheduled, the current one should be done already.
270 * This avoids audio playback underrun issue.
274 empty
= musb_is_tx_fifo_empty(hw_ep
);
277 if (!cppi41_channel
->is_tx
|| empty
) {
278 cppi41_trans_done(cppi41_channel
);
283 * On AM335x it has been observed that the TX interrupt fires
284 * too early that means the TXFIFO is not yet empty but the DMA
285 * engine says that it is done with the transfer. We don't
286 * receive a FIFO empty interrupt so the only thing we can do is
287 * to poll for the bit. On HS it usually takes 2us, on FS around
288 * 110us - 150us depending on the transfer size.
289 * We spin on HS (no longer than than 25us and setup a timer on
290 * FS to check for the bit and complete the transfer.
292 if (is_host_active(musb
)) {
293 if (musb
->port1_status
& USB_PORT_STAT_HIGH_SPEED
)
296 if (musb
->g
.speed
== USB_SPEED_HIGH
)
303 empty
= musb_is_tx_fifo_empty(hw_ep
);
305 cppi41_trans_done(cppi41_channel
);
314 list_add_tail(&cppi41_channel
->tx_check
,
315 &controller
->early_tx_list
);
316 if (!hrtimer_is_queued(&controller
->early_tx
)) {
317 unsigned long usecs
= cppi41_channel
->total_len
/ 10;
319 hrtimer_start_range_ns(&controller
->early_tx
,
320 usecs
* NSEC_PER_USEC
,
326 spin_unlock_irqrestore(&musb
->lock
, flags
);
329 static u32
update_ep_mode(unsigned ep
, unsigned mode
, u32 old
)
333 shift
= (ep
- 1) * 2;
334 old
&= ~(3 << shift
);
335 old
|= mode
<< shift
;
339 static void cppi41_set_dma_mode(struct cppi41_dma_channel
*cppi41_channel
,
342 struct cppi41_dma_controller
*controller
= cppi41_channel
->controller
;
343 struct musb
*musb
= controller
->controller
.musb
;
348 if (cppi41_channel
->is_tx
)
349 old_mode
= controller
->tx_mode
;
351 old_mode
= controller
->rx_mode
;
352 port
= cppi41_channel
->port_num
;
353 new_mode
= update_ep_mode(port
, mode
, old_mode
);
355 if (new_mode
== old_mode
)
357 if (cppi41_channel
->is_tx
) {
358 controller
->tx_mode
= new_mode
;
359 musb_writel(musb
->ctrl_base
, USB_CTRL_TX_MODE
, new_mode
);
361 controller
->rx_mode
= new_mode
;
362 musb_writel(musb
->ctrl_base
, USB_CTRL_RX_MODE
, new_mode
);
366 static void da8xx_set_dma_mode(struct cppi41_dma_channel
*cppi41_channel
,
369 struct cppi41_dma_controller
*controller
= cppi41_channel
->controller
;
370 struct musb
*musb
= controller
->controller
.musb
;
376 old_mode
= controller
->tx_mode
;
377 port
= cppi41_channel
->port_num
;
379 shift
= (port
- 1) * 4;
380 if (!cppi41_channel
->is_tx
)
382 new_mode
= old_mode
& ~(3 << shift
);
383 new_mode
|= mode
<< shift
;
385 if (new_mode
== old_mode
)
387 controller
->tx_mode
= new_mode
;
388 musb_writel(musb
->ctrl_base
, DA8XX_USB_MODE
, new_mode
);
392 static void cppi41_set_autoreq_mode(struct cppi41_dma_channel
*cppi41_channel
,
395 struct cppi41_dma_controller
*controller
= cppi41_channel
->controller
;
400 old_mode
= controller
->auto_req
;
401 port
= cppi41_channel
->port_num
;
402 new_mode
= update_ep_mode(port
, mode
, old_mode
);
404 if (new_mode
== old_mode
)
406 controller
->auto_req
= new_mode
;
407 musb_writel(controller
->controller
.musb
->ctrl_base
,
408 controller
->autoreq_reg
, new_mode
);
411 static bool cppi41_configure_channel(struct dma_channel
*channel
,
412 u16 packet_sz
, u8 mode
,
413 dma_addr_t dma_addr
, u32 len
)
415 struct cppi41_dma_channel
*cppi41_channel
= channel
->private_data
;
416 struct cppi41_dma_controller
*controller
= cppi41_channel
->controller
;
417 struct dma_chan
*dc
= cppi41_channel
->dc
;
418 struct dma_async_tx_descriptor
*dma_desc
;
419 enum dma_transfer_direction direction
;
420 struct musb
*musb
= cppi41_channel
->controller
->controller
.musb
;
421 unsigned use_gen_rndis
= 0;
423 cppi41_channel
->buf_addr
= dma_addr
;
424 cppi41_channel
->total_len
= len
;
425 cppi41_channel
->transferred
= 0;
426 cppi41_channel
->packet_sz
= packet_sz
;
427 cppi41_channel
->tx_zlp
= (cppi41_channel
->is_tx
&& mode
) ? 1 : 0;
430 * Due to AM335x' Advisory 1.0.13 we are not allowed to transfer more
431 * than max packet size at a time.
433 if (cppi41_channel
->is_tx
)
438 if (len
> packet_sz
) {
439 musb_writel(musb
->ctrl_base
,
440 RNDIS_REG(cppi41_channel
->port_num
), len
);
442 controller
->set_dma_mode(cppi41_channel
,
443 EP_MODE_DMA_GEN_RNDIS
);
446 cppi41_set_autoreq_mode(cppi41_channel
,
447 EP_MODE_AUTOREQ_ALL_NEOP
);
449 musb_writel(musb
->ctrl_base
,
450 RNDIS_REG(cppi41_channel
->port_num
), 0);
451 controller
->set_dma_mode(cppi41_channel
,
452 EP_MODE_DMA_TRANSPARENT
);
453 cppi41_set_autoreq_mode(cppi41_channel
,
454 EP_MODE_AUTOREQ_NONE
);
458 controller
->set_dma_mode(cppi41_channel
,
459 EP_MODE_DMA_TRANSPARENT
);
460 cppi41_set_autoreq_mode(cppi41_channel
, EP_MODE_AUTOREQ_NONE
);
461 len
= min_t(u32
, packet_sz
, len
);
463 cppi41_channel
->prog_len
= len
;
464 direction
= cppi41_channel
->is_tx
? DMA_MEM_TO_DEV
: DMA_DEV_TO_MEM
;
465 dma_desc
= dmaengine_prep_slave_single(dc
, dma_addr
, len
, direction
,
466 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
470 dma_desc
->callback_result
= cppi41_dma_callback
;
471 dma_desc
->callback_param
= channel
;
472 cppi41_channel
->cookie
= dma_desc
->tx_submit(dma_desc
);
473 cppi41_channel
->channel
.rx_packet_done
= false;
475 trace_musb_cppi41_config(cppi41_channel
);
477 save_rx_toggle(cppi41_channel
);
478 dma_async_issue_pending(dc
);
482 static struct dma_channel
*cppi41_dma_channel_allocate(struct dma_controller
*c
,
483 struct musb_hw_ep
*hw_ep
, u8 is_tx
)
485 struct cppi41_dma_controller
*controller
= container_of(c
,
486 struct cppi41_dma_controller
, controller
);
487 struct cppi41_dma_channel
*cppi41_channel
= NULL
;
488 u8 ch_num
= hw_ep
->epnum
- 1;
490 if (ch_num
>= controller
->num_channels
)
494 cppi41_channel
= &controller
->tx_channel
[ch_num
];
496 cppi41_channel
= &controller
->rx_channel
[ch_num
];
498 if (!cppi41_channel
->dc
)
501 if (cppi41_channel
->is_allocated
)
504 cppi41_channel
->hw_ep
= hw_ep
;
505 cppi41_channel
->is_allocated
= 1;
507 trace_musb_cppi41_alloc(cppi41_channel
);
508 return &cppi41_channel
->channel
;
511 static void cppi41_dma_channel_release(struct dma_channel
*channel
)
513 struct cppi41_dma_channel
*cppi41_channel
= channel
->private_data
;
515 trace_musb_cppi41_free(cppi41_channel
);
516 if (cppi41_channel
->is_allocated
) {
517 cppi41_channel
->is_allocated
= 0;
518 channel
->status
= MUSB_DMA_STATUS_FREE
;
519 channel
->actual_len
= 0;
523 static int cppi41_dma_channel_program(struct dma_channel
*channel
,
524 u16 packet_sz
, u8 mode
,
525 dma_addr_t dma_addr
, u32 len
)
528 struct cppi41_dma_channel
*cppi41_channel
= channel
->private_data
;
531 BUG_ON(channel
->status
== MUSB_DMA_STATUS_UNKNOWN
||
532 channel
->status
== MUSB_DMA_STATUS_BUSY
);
534 if (is_host_active(cppi41_channel
->controller
->controller
.musb
)) {
535 if (cppi41_channel
->is_tx
)
536 hb_mult
= cppi41_channel
->hw_ep
->out_qh
->hb_mult
;
538 hb_mult
= cppi41_channel
->hw_ep
->in_qh
->hb_mult
;
541 channel
->status
= MUSB_DMA_STATUS_BUSY
;
542 channel
->actual_len
= 0;
545 packet_sz
= hb_mult
* (packet_sz
& 0x7FF);
547 ret
= cppi41_configure_channel(channel
, packet_sz
, mode
, dma_addr
, len
);
549 channel
->status
= MUSB_DMA_STATUS_FREE
;
554 static int cppi41_is_compatible(struct dma_channel
*channel
, u16 maxpacket
,
555 void *buf
, u32 length
)
557 struct cppi41_dma_channel
*cppi41_channel
= channel
->private_data
;
558 struct cppi41_dma_controller
*controller
= cppi41_channel
->controller
;
559 struct musb
*musb
= controller
->controller
.musb
;
561 if (is_host_active(musb
)) {
565 if (cppi41_channel
->hw_ep
->ep_in
.type
!= USB_ENDPOINT_XFER_BULK
)
567 if (cppi41_channel
->is_tx
)
569 /* AM335x Advisory 1.0.13. No workaround for device RX mode */
573 static int cppi41_dma_channel_abort(struct dma_channel
*channel
)
575 struct cppi41_dma_channel
*cppi41_channel
= channel
->private_data
;
576 struct cppi41_dma_controller
*controller
= cppi41_channel
->controller
;
577 struct musb
*musb
= controller
->controller
.musb
;
578 void __iomem
*epio
= cppi41_channel
->hw_ep
->regs
;
584 is_tx
= cppi41_channel
->is_tx
;
585 trace_musb_cppi41_abort(cppi41_channel
);
587 if (cppi41_channel
->channel
.status
== MUSB_DMA_STATUS_FREE
)
590 list_del_init(&cppi41_channel
->tx_check
);
592 csr
= musb_readw(epio
, MUSB_TXCSR
);
593 csr
&= ~MUSB_TXCSR_DMAENAB
;
594 musb_writew(epio
, MUSB_TXCSR
, csr
);
596 cppi41_set_autoreq_mode(cppi41_channel
, EP_MODE_AUTOREQ_NONE
);
598 /* delay to drain to cppi dma pipeline for isoch */
601 csr
= musb_readw(epio
, MUSB_RXCSR
);
602 csr
&= ~(MUSB_RXCSR_H_REQPKT
| MUSB_RXCSR_DMAENAB
);
603 musb_writew(epio
, MUSB_RXCSR
, csr
);
605 /* wait to drain cppi dma pipe line */
608 csr
= musb_readw(epio
, MUSB_RXCSR
);
609 if (csr
& MUSB_RXCSR_RXPKTRDY
) {
610 csr
|= MUSB_RXCSR_FLUSHFIFO
;
611 musb_writew(epio
, MUSB_RXCSR
, csr
);
612 musb_writew(epio
, MUSB_RXCSR
, csr
);
616 /* DA8xx Advisory 2.3.27: wait 250 ms before to start the teardown */
617 if (musb
->io
.quirks
& MUSB_DA8XX
)
620 tdbit
= 1 << cppi41_channel
->port_num
;
626 musb_writel(musb
->ctrl_base
, controller
->tdown_reg
,
628 ret
= dmaengine_terminate_all(cppi41_channel
->dc
);
629 } while (ret
== -EAGAIN
);
632 musb_writel(musb
->ctrl_base
, controller
->tdown_reg
, tdbit
);
634 csr
= musb_readw(epio
, MUSB_TXCSR
);
635 if (csr
& MUSB_TXCSR_TXPKTRDY
) {
636 csr
|= MUSB_TXCSR_FLUSHFIFO
;
637 musb_writew(epio
, MUSB_TXCSR
, csr
);
641 cppi41_channel
->channel
.status
= MUSB_DMA_STATUS_FREE
;
645 static void cppi41_release_all_dma_chans(struct cppi41_dma_controller
*ctrl
)
650 for (i
= 0; i
< ctrl
->num_channels
; i
++) {
651 dc
= ctrl
->tx_channel
[i
].dc
;
653 dma_release_channel(dc
);
654 dc
= ctrl
->rx_channel
[i
].dc
;
656 dma_release_channel(dc
);
660 static void cppi41_dma_controller_stop(struct cppi41_dma_controller
*controller
)
662 cppi41_release_all_dma_chans(controller
);
665 static int cppi41_dma_controller_start(struct cppi41_dma_controller
*controller
)
667 struct musb
*musb
= controller
->controller
.musb
;
668 struct device
*dev
= musb
->controller
;
669 struct device_node
*np
= dev
->parent
->of_node
;
670 struct cppi41_dma_channel
*cppi41_channel
;
675 count
= of_property_count_strings(np
, "dma-names");
679 for (i
= 0; i
< count
; i
++) {
681 struct dma_channel
*musb_dma
;
686 ret
= of_property_read_string_index(np
, "dma-names", i
, &str
);
689 if (strstarts(str
, "tx"))
691 else if (strstarts(str
, "rx"))
694 dev_err(dev
, "Wrong dmatype %s\n", str
);
697 ret
= kstrtouint(str
+ 2, 0, &port
);
702 if (port
> controller
->num_channels
|| !port
)
705 cppi41_channel
= &controller
->tx_channel
[port
- 1];
707 cppi41_channel
= &controller
->rx_channel
[port
- 1];
709 cppi41_channel
->controller
= controller
;
710 cppi41_channel
->port_num
= port
;
711 cppi41_channel
->is_tx
= is_tx
;
712 INIT_LIST_HEAD(&cppi41_channel
->tx_check
);
714 musb_dma
= &cppi41_channel
->channel
;
715 musb_dma
->private_data
= cppi41_channel
;
716 musb_dma
->status
= MUSB_DMA_STATUS_FREE
;
717 musb_dma
->max_len
= SZ_4M
;
719 dc
= dma_request_chan(dev
->parent
, str
);
722 if (ret
!= -EPROBE_DEFER
)
723 dev_err(dev
, "Failed to request %s: %d.\n",
728 cppi41_channel
->dc
= dc
;
732 cppi41_release_all_dma_chans(controller
);
736 void cppi41_dma_controller_destroy(struct dma_controller
*c
)
738 struct cppi41_dma_controller
*controller
= container_of(c
,
739 struct cppi41_dma_controller
, controller
);
741 hrtimer_cancel(&controller
->early_tx
);
742 cppi41_dma_controller_stop(controller
);
743 kfree(controller
->rx_channel
);
744 kfree(controller
->tx_channel
);
747 EXPORT_SYMBOL_GPL(cppi41_dma_controller_destroy
);
749 struct dma_controller
*
750 cppi41_dma_controller_create(struct musb
*musb
, void __iomem
*base
)
752 struct cppi41_dma_controller
*controller
;
756 if (!musb
->controller
->parent
->of_node
) {
757 dev_err(musb
->controller
, "Need DT for the DMA engine.\n");
761 controller
= kzalloc(sizeof(*controller
), GFP_KERNEL
);
765 hrtimer_init(&controller
->early_tx
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
766 controller
->early_tx
.function
= cppi41_recheck_tx_req
;
767 INIT_LIST_HEAD(&controller
->early_tx_list
);
769 controller
->controller
.channel_alloc
= cppi41_dma_channel_allocate
;
770 controller
->controller
.channel_release
= cppi41_dma_channel_release
;
771 controller
->controller
.channel_program
= cppi41_dma_channel_program
;
772 controller
->controller
.channel_abort
= cppi41_dma_channel_abort
;
773 controller
->controller
.is_compatible
= cppi41_is_compatible
;
774 controller
->controller
.musb
= musb
;
776 if (musb
->io
.quirks
& MUSB_DA8XX
) {
777 controller
->tdown_reg
= DA8XX_USB_TEARDOWN
;
778 controller
->autoreq_reg
= DA8XX_USB_AUTOREQ
;
779 controller
->set_dma_mode
= da8xx_set_dma_mode
;
780 controller
->num_channels
= DA8XX_DMA_NUM_CHANNELS
;
782 controller
->tdown_reg
= USB_TDOWN
;
783 controller
->autoreq_reg
= USB_CTRL_AUTOREQ
;
784 controller
->set_dma_mode
= cppi41_set_dma_mode
;
785 controller
->num_channels
= MUSB_DMA_NUM_CHANNELS
;
788 channel_size
= controller
->num_channels
*
789 sizeof(struct cppi41_dma_channel
);
790 controller
->rx_channel
= kzalloc(channel_size
, GFP_KERNEL
);
791 if (!controller
->rx_channel
)
792 goto rx_channel_alloc_fail
;
793 controller
->tx_channel
= kzalloc(channel_size
, GFP_KERNEL
);
794 if (!controller
->tx_channel
)
795 goto tx_channel_alloc_fail
;
797 ret
= cppi41_dma_controller_start(controller
);
800 return &controller
->controller
;
803 kfree(controller
->tx_channel
);
804 tx_channel_alloc_fail
:
805 kfree(controller
->rx_channel
);
806 rx_channel_alloc_fail
:
809 if (ret
== -EPROBE_DEFER
)
813 EXPORT_SYMBOL_GPL(cppi41_dma_controller_create
);