1 // SPDX-License-Identifier: GPL-1.0+
5 * Copyright (C) 2011 Renesas Solutions Corp.
6 * Copyright (C) 2019 Renesas Electronics Corporation
7 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
9 #include <linux/delay.h>
11 #include <linux/scatterlist.h>
15 #define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
17 #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
22 void usbhs_pkt_init(struct usbhs_pkt
*pkt
)
24 INIT_LIST_HEAD(&pkt
->node
);
28 * packet control function
30 static int usbhsf_null_handle(struct usbhs_pkt
*pkt
, int *is_done
)
32 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pkt
->pipe
);
33 struct device
*dev
= usbhs_priv_to_dev(priv
);
35 dev_err(dev
, "null handler\n");
40 static const struct usbhs_pkt_handle usbhsf_null_handler
= {
41 .prepare
= usbhsf_null_handle
,
42 .try_run
= usbhsf_null_handle
,
45 void usbhs_pkt_push(struct usbhs_pipe
*pipe
, struct usbhs_pkt
*pkt
,
46 void (*done
)(struct usbhs_priv
*priv
,
47 struct usbhs_pkt
*pkt
),
48 void *buf
, int len
, int zero
, int sequence
)
50 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
51 struct device
*dev
= usbhs_priv_to_dev(priv
);
55 dev_err(dev
, "no done function\n");
59 /******************** spin lock ********************/
60 usbhs_lock(priv
, flags
);
63 dev_err(dev
, "no handler function\n");
64 pipe
->handler
= &usbhsf_null_handler
;
67 list_move_tail(&pkt
->node
, &pipe
->list
);
70 * each pkt must hold own handler.
71 * because handler might be changed by its situation.
72 * dma handler -> pio handler.
76 pkt
->handler
= pipe
->handler
;
81 pkt
->sequence
= sequence
;
83 usbhs_unlock(priv
, flags
);
84 /******************** spin unlock ******************/
87 static void __usbhsf_pkt_del(struct usbhs_pkt
*pkt
)
89 list_del_init(&pkt
->node
);
92 struct usbhs_pkt
*__usbhsf_pkt_get(struct usbhs_pipe
*pipe
)
94 return list_first_entry_or_null(&pipe
->list
, struct usbhs_pkt
, node
);
97 static void usbhsf_fifo_unselect(struct usbhs_pipe
*pipe
,
98 struct usbhs_fifo
*fifo
);
99 static struct dma_chan
*usbhsf_dma_chan_get(struct usbhs_fifo
*fifo
,
100 struct usbhs_pkt
*pkt
);
101 #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
102 #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
103 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt
*pkt
, int map
);
104 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe
*pipe
, int enable
);
105 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe
*pipe
, int enable
);
106 struct usbhs_pkt
*usbhs_pkt_pop(struct usbhs_pipe
*pipe
, struct usbhs_pkt
*pkt
)
108 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
109 struct usbhs_fifo
*fifo
= usbhs_pipe_to_fifo(pipe
);
112 /******************** spin lock ********************/
113 usbhs_lock(priv
, flags
);
115 usbhs_pipe_disable(pipe
);
118 pkt
= __usbhsf_pkt_get(pipe
);
121 struct dma_chan
*chan
= NULL
;
124 chan
= usbhsf_dma_chan_get(fifo
, pkt
);
126 dmaengine_terminate_all(chan
);
127 usbhsf_dma_unmap(pkt
);
129 if (usbhs_pipe_is_dir_in(pipe
))
130 usbhsf_rx_irq_ctrl(pipe
, 0);
132 usbhsf_tx_irq_ctrl(pipe
, 0);
135 usbhs_pipe_clear_without_sequence(pipe
, 0, 0);
136 usbhs_pipe_running(pipe
, 0);
138 __usbhsf_pkt_del(pkt
);
142 usbhsf_fifo_unselect(pipe
, fifo
);
144 usbhs_unlock(priv
, flags
);
145 /******************** spin unlock ******************/
156 static int usbhsf_pkt_handler(struct usbhs_pipe
*pipe
, int type
)
158 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
159 struct usbhs_pkt
*pkt
;
160 struct device
*dev
= usbhs_priv_to_dev(priv
);
161 int (*func
)(struct usbhs_pkt
*pkt
, int *is_done
);
166 /******************** spin lock ********************/
167 usbhs_lock(priv
, flags
);
169 pkt
= __usbhsf_pkt_get(pipe
);
172 goto __usbhs_pkt_handler_end
;
176 case USBHSF_PKT_PREPARE
:
177 func
= pkt
->handler
->prepare
;
179 case USBHSF_PKT_TRY_RUN
:
180 func
= pkt
->handler
->try_run
;
182 case USBHSF_PKT_DMA_DONE
:
183 func
= pkt
->handler
->dma_done
;
186 dev_err(dev
, "unknown pkt handler\n");
187 goto __usbhs_pkt_handler_end
;
191 ret
= func(pkt
, &is_done
);
194 __usbhsf_pkt_del(pkt
);
196 __usbhs_pkt_handler_end
:
197 usbhs_unlock(priv
, flags
);
198 /******************** spin unlock ******************/
201 pkt
->done(priv
, pkt
);
202 usbhs_pkt_start(pipe
);
208 void usbhs_pkt_start(struct usbhs_pipe
*pipe
)
210 usbhsf_pkt_handler(pipe
, USBHSF_PKT_PREPARE
);
214 * irq enable/disable function
216 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
217 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
218 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
220 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
221 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
222 u16 status = (1 << usbhs_pipe_number(pipe)); \
226 mod->status |= status; \
228 mod->status &= ~status; \
229 usbhs_irq_callback_update(priv, mod); \
232 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe
*pipe
, int enable
)
235 * And DCP pipe can NOT use "ready interrupt" for "send"
236 * it should use "empty" interrupt.
238 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
240 * on the other hand, normal pipe can use "ready interrupt" for "send"
241 * even though it is single/double buffer
243 if (usbhs_pipe_is_dcp(pipe
))
244 usbhsf_irq_empty_ctrl(pipe
, enable
);
246 usbhsf_irq_ready_ctrl(pipe
, enable
);
249 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe
*pipe
, int enable
)
251 usbhsf_irq_ready_ctrl(pipe
, enable
);
257 static void usbhsf_send_terminator(struct usbhs_pipe
*pipe
,
258 struct usbhs_fifo
*fifo
)
260 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
262 usbhs_bset(priv
, fifo
->ctr
, BVAL
, BVAL
);
265 static int usbhsf_fifo_barrier(struct usbhs_priv
*priv
,
266 struct usbhs_fifo
*fifo
)
268 /* The FIFO port is accessible */
269 if (usbhs_read(priv
, fifo
->ctr
) & FRDY
)
275 static void usbhsf_fifo_clear(struct usbhs_pipe
*pipe
,
276 struct usbhs_fifo
*fifo
)
278 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
281 if (!usbhs_pipe_is_dcp(pipe
)) {
283 * This driver checks the pipe condition first to avoid -EBUSY
284 * from usbhsf_fifo_barrier() if the pipe is RX direction and
287 if (usbhs_pipe_is_dir_in(pipe
))
288 ret
= usbhs_pipe_is_accessible(pipe
);
290 ret
= usbhsf_fifo_barrier(priv
, fifo
);
294 * if non-DCP pipe, this driver should set BCLR when
295 * usbhsf_fifo_barrier() returns 0.
298 usbhs_write(priv
, fifo
->ctr
, BCLR
);
301 static int usbhsf_fifo_rcv_len(struct usbhs_priv
*priv
,
302 struct usbhs_fifo
*fifo
)
304 return usbhs_read(priv
, fifo
->ctr
) & DTLN_MASK
;
307 static void usbhsf_fifo_unselect(struct usbhs_pipe
*pipe
,
308 struct usbhs_fifo
*fifo
)
310 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
312 usbhs_pipe_select_fifo(pipe
, NULL
);
313 usbhs_write(priv
, fifo
->sel
, 0);
316 static int usbhsf_fifo_select(struct usbhs_pipe
*pipe
,
317 struct usbhs_fifo
*fifo
,
320 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
321 struct device
*dev
= usbhs_priv_to_dev(priv
);
323 u16 mask
= ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
324 u16 base
= usbhs_pipe_number(pipe
); /* CURPIPE */
326 if (usbhs_pipe_is_busy(pipe
) ||
327 usbhsf_fifo_is_busy(fifo
))
330 if (usbhs_pipe_is_dcp(pipe
)) {
331 base
|= (1 == write
) << 5; /* ISEL */
333 if (usbhs_mod_is_host(priv
))
334 usbhs_dcp_dir_for_host(pipe
, write
);
337 /* "base" will be used below */
338 usbhs_write(priv
, fifo
->sel
, base
| MBW_32
);
340 /* check ISEL and CURPIPE value */
342 if (base
== (mask
& usbhs_read(priv
, fifo
->sel
))) {
343 usbhs_pipe_select_fifo(pipe
, fifo
);
349 dev_err(dev
, "fifo select error\n");
357 static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt
*pkt
, int *is_done
)
359 struct usbhs_pipe
*pipe
= pkt
->pipe
;
360 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
361 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
362 struct device
*dev
= usbhs_priv_to_dev(priv
);
365 usbhs_pipe_disable(pipe
);
367 ret
= usbhsf_fifo_select(pipe
, fifo
, 1);
369 dev_err(dev
, "%s() failed\n", __func__
);
373 usbhs_pipe_sequence_data1(pipe
); /* DATA1 */
375 usbhsf_fifo_clear(pipe
, fifo
);
376 usbhsf_send_terminator(pipe
, fifo
);
378 usbhsf_fifo_unselect(pipe
, fifo
);
380 usbhsf_tx_irq_ctrl(pipe
, 1);
381 usbhs_pipe_enable(pipe
);
386 static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt
*pkt
, int *is_done
)
388 struct usbhs_pipe
*pipe
= pkt
->pipe
;
389 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
390 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
391 struct device
*dev
= usbhs_priv_to_dev(priv
);
394 usbhs_pipe_disable(pipe
);
396 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
398 dev_err(dev
, "%s() fail\n", __func__
);
402 usbhs_pipe_sequence_data1(pipe
); /* DATA1 */
403 usbhsf_fifo_clear(pipe
, fifo
);
405 usbhsf_fifo_unselect(pipe
, fifo
);
407 usbhsf_rx_irq_ctrl(pipe
, 1);
408 usbhs_pipe_enable(pipe
);
414 static int usbhs_dcp_dir_switch_done(struct usbhs_pkt
*pkt
, int *is_done
)
416 struct usbhs_pipe
*pipe
= pkt
->pipe
;
418 if (pkt
->handler
== &usbhs_dcp_status_stage_in_handler
)
419 usbhsf_tx_irq_ctrl(pipe
, 0);
421 usbhsf_rx_irq_ctrl(pipe
, 0);
423 pkt
->actual
= pkt
->length
;
429 const struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler
= {
430 .prepare
= usbhs_dcp_dir_switch_to_write
,
431 .try_run
= usbhs_dcp_dir_switch_done
,
434 const struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler
= {
435 .prepare
= usbhs_dcp_dir_switch_to_read
,
436 .try_run
= usbhs_dcp_dir_switch_done
,
440 * DCP data stage (push)
442 static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt
*pkt
, int *is_done
)
444 struct usbhs_pipe
*pipe
= pkt
->pipe
;
446 usbhs_pipe_sequence_data1(pipe
); /* DATA1 */
449 * change handler to PIO push
451 pkt
->handler
= &usbhs_fifo_pio_push_handler
;
453 return pkt
->handler
->prepare(pkt
, is_done
);
456 const struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler
= {
457 .prepare
= usbhsf_dcp_data_stage_try_push
,
461 * DCP data stage (pop)
463 static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt
*pkt
,
466 struct usbhs_pipe
*pipe
= pkt
->pipe
;
467 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
468 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
);
470 if (usbhs_pipe_is_busy(pipe
))
474 * prepare pop for DCP should
475 * - change DCP direction,
479 usbhs_pipe_disable(pipe
);
481 usbhs_pipe_sequence_data1(pipe
); /* DATA1 */
483 usbhsf_fifo_select(pipe
, fifo
, 0);
484 usbhsf_fifo_clear(pipe
, fifo
);
485 usbhsf_fifo_unselect(pipe
, fifo
);
488 * change handler to PIO pop
490 pkt
->handler
= &usbhs_fifo_pio_pop_handler
;
492 return pkt
->handler
->prepare(pkt
, is_done
);
495 const struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler
= {
496 .prepare
= usbhsf_dcp_data_stage_prepare_pop
,
502 static int usbhsf_pio_try_push(struct usbhs_pkt
*pkt
, int *is_done
)
504 struct usbhs_pipe
*pipe
= pkt
->pipe
;
505 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
506 struct device
*dev
= usbhs_priv_to_dev(priv
);
507 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
508 void __iomem
*addr
= priv
->base
+ fifo
->port
;
510 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
515 usbhs_pipe_data_sequence(pipe
, pkt
->sequence
);
516 pkt
->sequence
= -1; /* -1 sequence will be ignored */
518 usbhs_pipe_set_trans_count_if_bulk(pipe
, pkt
->length
);
520 ret
= usbhsf_fifo_select(pipe
, fifo
, 1);
524 ret
= usbhs_pipe_is_accessible(pipe
);
526 /* inaccessible pipe is not an error */
528 goto usbhs_fifo_write_busy
;
531 ret
= usbhsf_fifo_barrier(priv
, fifo
);
533 goto usbhs_fifo_write_busy
;
535 buf
= pkt
->buf
+ pkt
->actual
;
536 len
= pkt
->length
- pkt
->actual
;
537 len
= min(len
, maxp
);
539 is_short
= total_len
< maxp
;
546 if (len
>= 4 && !((unsigned long)buf
& 0x03)) {
547 iowrite32_rep(addr
, buf
, len
/ 4);
549 buf
+= total_len
- len
;
552 /* the rest operation */
553 if (usbhs_get_dparam(priv
, cfifo_byte_addr
)) {
554 for (i
= 0; i
< len
; i
++)
555 iowrite8(buf
[i
], addr
+ (i
& 0x03));
557 for (i
= 0; i
< len
; i
++)
558 iowrite8(buf
[i
], addr
+ (0x03 - (i
& 0x03)));
564 pkt
->actual
+= total_len
;
566 if (pkt
->actual
< pkt
->length
)
567 *is_done
= 0; /* there are remainder data */
569 *is_done
= 1; /* short packet */
571 *is_done
= !pkt
->zero
; /* send zero packet ? */
577 usbhsf_send_terminator(pipe
, fifo
);
579 usbhsf_tx_irq_ctrl(pipe
, !*is_done
);
580 usbhs_pipe_running(pipe
, !*is_done
);
581 usbhs_pipe_enable(pipe
);
583 dev_dbg(dev
, " send %d (%d/ %d/ %d/ %d)\n",
584 usbhs_pipe_number(pipe
),
585 pkt
->length
, pkt
->actual
, *is_done
, pkt
->zero
);
587 usbhsf_fifo_unselect(pipe
, fifo
);
591 usbhs_fifo_write_busy
:
592 usbhsf_fifo_unselect(pipe
, fifo
);
598 usbhsf_tx_irq_ctrl(pipe
, 1);
599 usbhs_pipe_running(pipe
, 1);
604 static int usbhsf_pio_prepare_push(struct usbhs_pkt
*pkt
, int *is_done
)
606 if (usbhs_pipe_is_running(pkt
->pipe
))
609 return usbhsf_pio_try_push(pkt
, is_done
);
612 const struct usbhs_pkt_handle usbhs_fifo_pio_push_handler
= {
613 .prepare
= usbhsf_pio_prepare_push
,
614 .try_run
= usbhsf_pio_try_push
,
620 static int usbhsf_prepare_pop(struct usbhs_pkt
*pkt
, int *is_done
)
622 struct usbhs_pipe
*pipe
= pkt
->pipe
;
623 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
624 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
);
626 if (usbhs_pipe_is_busy(pipe
))
629 if (usbhs_pipe_is_running(pipe
))
633 * pipe enable to prepare packet receive
635 usbhs_pipe_data_sequence(pipe
, pkt
->sequence
);
636 pkt
->sequence
= -1; /* -1 sequence will be ignored */
638 if (usbhs_pipe_is_dcp(pipe
))
639 usbhsf_fifo_clear(pipe
, fifo
);
641 usbhs_pipe_set_trans_count_if_bulk(pipe
, pkt
->length
);
642 usbhs_pipe_enable(pipe
);
643 usbhs_pipe_running(pipe
, 1);
644 usbhsf_rx_irq_ctrl(pipe
, 1);
649 static int usbhsf_pio_try_pop(struct usbhs_pkt
*pkt
, int *is_done
)
651 struct usbhs_pipe
*pipe
= pkt
->pipe
;
652 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
653 struct device
*dev
= usbhs_priv_to_dev(priv
);
654 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
655 void __iomem
*addr
= priv
->base
+ fifo
->port
;
658 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
663 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
667 ret
= usbhsf_fifo_barrier(priv
, fifo
);
669 goto usbhs_fifo_read_busy
;
671 rcv_len
= usbhsf_fifo_rcv_len(priv
, fifo
);
673 buf
= pkt
->buf
+ pkt
->actual
;
674 len
= pkt
->length
- pkt
->actual
;
675 len
= min(len
, rcv_len
);
679 * update actual length first here to decide disable pipe.
680 * if this pipe keeps BUF status and all data were popped,
681 * then, next interrupt/token will be issued again
683 pkt
->actual
+= total_len
;
685 if ((pkt
->actual
== pkt
->length
) || /* receive all data */
686 (total_len
< maxp
)) { /* short packet */
688 usbhsf_rx_irq_ctrl(pipe
, 0);
689 usbhs_pipe_running(pipe
, 0);
691 * If function mode, since this controller is possible to enter
692 * Control Write status stage at this timing, this driver
693 * should not disable the pipe. If such a case happens, this
694 * controller is not able to complete the status stage.
696 if (!usbhs_mod_is_host(priv
) && !usbhs_pipe_is_dcp(pipe
))
697 usbhs_pipe_disable(pipe
); /* disable pipe first */
701 * Buffer clear if Zero-Length packet
704 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
708 usbhsf_fifo_clear(pipe
, fifo
);
709 goto usbhs_fifo_read_end
;
717 if (len
>= 4 && !((unsigned long)buf
& 0x03)) {
718 ioread32_rep(addr
, buf
, len
/ 4);
720 buf
+= total_len
- len
;
723 /* the rest operation */
724 for (i
= 0; i
< len
; i
++) {
726 data
= ioread32(addr
);
728 buf
[i
] = (data
>> ((i
& 0x03) * 8)) & 0xff;
732 dev_dbg(dev
, " recv %d (%d/ %d/ %d/ %d)\n",
733 usbhs_pipe_number(pipe
),
734 pkt
->length
, pkt
->actual
, *is_done
, pkt
->zero
);
736 usbhs_fifo_read_busy
:
737 usbhsf_fifo_unselect(pipe
, fifo
);
742 const struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler
= {
743 .prepare
= usbhsf_prepare_pop
,
744 .try_run
= usbhsf_pio_try_pop
,
748 * DCP ctrol statge handler
750 static int usbhsf_ctrl_stage_end(struct usbhs_pkt
*pkt
, int *is_done
)
752 usbhs_dcp_control_transfer_done(pkt
->pipe
);
759 const struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler
= {
760 .prepare
= usbhsf_ctrl_stage_end
,
761 .try_run
= usbhsf_ctrl_stage_end
,
767 static struct dma_chan
*usbhsf_dma_chan_get(struct usbhs_fifo
*fifo
,
768 struct usbhs_pkt
*pkt
)
770 if (&usbhs_fifo_dma_push_handler
== pkt
->handler
)
771 return fifo
->tx_chan
;
773 if (&usbhs_fifo_dma_pop_handler
== pkt
->handler
)
774 return fifo
->rx_chan
;
779 static struct usbhs_fifo
*usbhsf_get_dma_fifo(struct usbhs_priv
*priv
,
780 struct usbhs_pkt
*pkt
)
782 struct usbhs_fifo
*fifo
;
785 usbhs_for_each_dfifo(priv
, fifo
, i
) {
786 if (usbhsf_dma_chan_get(fifo
, pkt
) &&
787 !usbhsf_fifo_is_busy(fifo
))
794 #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
795 #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
796 static void __usbhsf_dma_ctrl(struct usbhs_pipe
*pipe
,
797 struct usbhs_fifo
*fifo
,
800 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
802 usbhs_bset(priv
, fifo
->sel
, DREQE
, dreqe
);
805 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt
*pkt
, int map
)
807 struct usbhs_pipe
*pipe
= pkt
->pipe
;
808 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
809 struct usbhs_pipe_info
*info
= usbhs_priv_to_pipeinfo(priv
);
810 struct usbhs_fifo
*fifo
= usbhs_pipe_to_fifo(pipe
);
811 struct dma_chan
*chan
= usbhsf_dma_chan_get(fifo
, pkt
);
813 return info
->dma_map_ctrl(chan
->device
->dev
, pkt
, map
);
816 static void usbhsf_dma_complete(void *arg
,
817 const struct dmaengine_result
*result
);
818 static void usbhsf_dma_xfer_preparing(struct usbhs_pkt
*pkt
)
820 struct usbhs_pipe
*pipe
= pkt
->pipe
;
821 struct usbhs_fifo
*fifo
;
822 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
823 struct dma_async_tx_descriptor
*desc
;
824 struct dma_chan
*chan
;
825 struct device
*dev
= usbhs_priv_to_dev(priv
);
826 enum dma_transfer_direction dir
;
829 fifo
= usbhs_pipe_to_fifo(pipe
);
833 chan
= usbhsf_dma_chan_get(fifo
, pkt
);
834 dir
= usbhs_pipe_is_dir_in(pipe
) ? DMA_DEV_TO_MEM
: DMA_MEM_TO_DEV
;
836 desc
= dmaengine_prep_slave_single(chan
, pkt
->dma
+ pkt
->actual
,
838 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
842 desc
->callback_result
= usbhsf_dma_complete
;
843 desc
->callback_param
= pkt
;
845 cookie
= dmaengine_submit(desc
);
847 dev_err(dev
, "Failed to submit dma descriptor\n");
851 dev_dbg(dev
, " %s %d (%d/ %d)\n",
852 fifo
->name
, usbhs_pipe_number(pipe
), pkt
->length
, pkt
->zero
);
854 usbhs_pipe_running(pipe
, 1);
855 usbhs_pipe_set_trans_count_if_bulk(pipe
, pkt
->trans
);
856 dma_async_issue_pending(chan
);
857 usbhsf_dma_start(pipe
, fifo
);
858 usbhs_pipe_enable(pipe
);
861 static void xfer_work(struct work_struct
*work
)
863 struct usbhs_pkt
*pkt
= container_of(work
, struct usbhs_pkt
, work
);
864 struct usbhs_pipe
*pipe
= pkt
->pipe
;
865 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
868 usbhs_lock(priv
, flags
);
869 usbhsf_dma_xfer_preparing(pkt
);
870 usbhs_unlock(priv
, flags
);
876 static int usbhsf_dma_prepare_push(struct usbhs_pkt
*pkt
, int *is_done
)
878 struct usbhs_pipe
*pipe
= pkt
->pipe
;
879 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
880 struct usbhs_fifo
*fifo
;
881 int len
= pkt
->length
- pkt
->actual
;
883 uintptr_t align_mask
;
885 if (usbhs_pipe_is_busy(pipe
))
888 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
889 if ((len
< usbhs_get_dparam(priv
, pio_dma_border
)) ||
890 usbhs_pipe_type_is(pipe
, USB_ENDPOINT_XFER_ISOC
))
891 goto usbhsf_pio_prepare_push
;
893 /* check data length if this driver don't use USB-DMAC */
894 if (!usbhs_get_dparam(priv
, has_usb_dmac
) && len
& 0x7)
895 goto usbhsf_pio_prepare_push
;
897 /* check buffer alignment */
898 align_mask
= usbhs_get_dparam(priv
, has_usb_dmac
) ?
899 USBHS_USB_DMAC_XFER_SIZE
- 1 : 0x7;
900 if ((uintptr_t)(pkt
->buf
+ pkt
->actual
) & align_mask
)
901 goto usbhsf_pio_prepare_push
;
903 /* return at this time if the pipe is running */
904 if (usbhs_pipe_is_running(pipe
))
907 /* get enable DMA fifo */
908 fifo
= usbhsf_get_dma_fifo(priv
, pkt
);
910 goto usbhsf_pio_prepare_push
;
912 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
914 goto usbhsf_pio_prepare_push
;
916 if (usbhsf_dma_map(pkt
) < 0)
917 goto usbhsf_pio_prepare_push_unselect
;
921 usbhsf_tx_irq_ctrl(pipe
, 0);
922 /* FIXME: Workaound for usb dmac that driver can be used in atomic */
923 if (usbhs_get_dparam(priv
, has_usb_dmac
)) {
924 usbhsf_dma_xfer_preparing(pkt
);
926 INIT_WORK(&pkt
->work
, xfer_work
);
927 schedule_work(&pkt
->work
);
932 usbhsf_pio_prepare_push_unselect
:
933 usbhsf_fifo_unselect(pipe
, fifo
);
934 usbhsf_pio_prepare_push
:
936 * change handler to PIO
938 pkt
->handler
= &usbhs_fifo_pio_push_handler
;
940 return pkt
->handler
->prepare(pkt
, is_done
);
943 static int usbhsf_dma_push_done(struct usbhs_pkt
*pkt
, int *is_done
)
945 struct usbhs_pipe
*pipe
= pkt
->pipe
;
946 int is_short
= pkt
->trans
% usbhs_pipe_get_maxpacket(pipe
);
948 pkt
->actual
+= pkt
->trans
;
950 if (pkt
->actual
< pkt
->length
)
951 *is_done
= 0; /* there are remainder data */
953 *is_done
= 1; /* short packet */
955 *is_done
= !pkt
->zero
; /* send zero packet? */
957 usbhs_pipe_running(pipe
, !*is_done
);
959 usbhsf_dma_stop(pipe
, pipe
->fifo
);
960 usbhsf_dma_unmap(pkt
);
961 usbhsf_fifo_unselect(pipe
, pipe
->fifo
);
964 /* change handler to PIO */
965 pkt
->handler
= &usbhs_fifo_pio_push_handler
;
966 return pkt
->handler
->try_run(pkt
, is_done
);
972 const struct usbhs_pkt_handle usbhs_fifo_dma_push_handler
= {
973 .prepare
= usbhsf_dma_prepare_push
,
974 .dma_done
= usbhsf_dma_push_done
,
981 static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt
*pkt
,
984 return usbhsf_prepare_pop(pkt
, is_done
);
987 static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt
*pkt
,
990 struct usbhs_pipe
*pipe
= pkt
->pipe
;
991 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
992 struct usbhs_fifo
*fifo
;
995 if (usbhs_pipe_is_busy(pipe
))
998 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
999 if ((pkt
->length
< usbhs_get_dparam(priv
, pio_dma_border
)) ||
1000 usbhs_pipe_type_is(pipe
, USB_ENDPOINT_XFER_ISOC
))
1001 goto usbhsf_pio_prepare_pop
;
1003 fifo
= usbhsf_get_dma_fifo(priv
, pkt
);
1005 goto usbhsf_pio_prepare_pop
;
1007 if ((uintptr_t)pkt
->buf
& (USBHS_USB_DMAC_XFER_SIZE
- 1))
1008 goto usbhsf_pio_prepare_pop
;
1010 /* return at this time if the pipe is running */
1011 if (usbhs_pipe_is_running(pipe
))
1014 usbhs_pipe_config_change_bfre(pipe
, 1);
1016 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
1018 goto usbhsf_pio_prepare_pop
;
1020 if (usbhsf_dma_map(pkt
) < 0)
1021 goto usbhsf_pio_prepare_pop_unselect
;
1026 * usbhs_fifo_dma_pop_handler :: prepare
1027 * enabled irq to come here.
1028 * but it is no longer needed for DMA. disable it.
1030 usbhsf_rx_irq_ctrl(pipe
, 0);
1032 pkt
->trans
= pkt
->length
;
1034 usbhsf_dma_xfer_preparing(pkt
);
1038 usbhsf_pio_prepare_pop_unselect
:
1039 usbhsf_fifo_unselect(pipe
, fifo
);
1040 usbhsf_pio_prepare_pop
:
1043 * change handler to PIO
1045 pkt
->handler
= &usbhs_fifo_pio_pop_handler
;
1046 usbhs_pipe_config_change_bfre(pipe
, 0);
1048 return pkt
->handler
->prepare(pkt
, is_done
);
1051 static int usbhsf_dma_prepare_pop(struct usbhs_pkt
*pkt
, int *is_done
)
1053 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pkt
->pipe
);
1055 if (usbhs_get_dparam(priv
, has_usb_dmac
))
1056 return usbhsf_dma_prepare_pop_with_usb_dmac(pkt
, is_done
);
1058 return usbhsf_dma_prepare_pop_with_rx_irq(pkt
, is_done
);
1061 static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt
*pkt
, int *is_done
)
1063 struct usbhs_pipe
*pipe
= pkt
->pipe
;
1064 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
1065 struct usbhs_fifo
*fifo
;
1068 if (usbhs_pipe_is_busy(pipe
))
1071 if (usbhs_pipe_is_dcp(pipe
))
1072 goto usbhsf_pio_prepare_pop
;
1074 /* get enable DMA fifo */
1075 fifo
= usbhsf_get_dma_fifo(priv
, pkt
);
1077 goto usbhsf_pio_prepare_pop
;
1079 if ((uintptr_t)(pkt
->buf
+ pkt
->actual
) & 0x7) /* 8byte alignment */
1080 goto usbhsf_pio_prepare_pop
;
1082 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
1084 goto usbhsf_pio_prepare_pop
;
1086 /* use PIO if packet is less than pio_dma_border */
1087 len
= usbhsf_fifo_rcv_len(priv
, fifo
);
1088 len
= min(pkt
->length
- pkt
->actual
, len
);
1089 if (len
& 0x7) /* 8byte alignment */
1090 goto usbhsf_pio_prepare_pop_unselect
;
1092 if (len
< usbhs_get_dparam(priv
, pio_dma_border
))
1093 goto usbhsf_pio_prepare_pop_unselect
;
1095 ret
= usbhsf_fifo_barrier(priv
, fifo
);
1097 goto usbhsf_pio_prepare_pop_unselect
;
1099 if (usbhsf_dma_map(pkt
) < 0)
1100 goto usbhsf_pio_prepare_pop_unselect
;
1105 * usbhs_fifo_dma_pop_handler :: prepare
1106 * enabled irq to come here.
1107 * but it is no longer needed for DMA. disable it.
1109 usbhsf_rx_irq_ctrl(pipe
, 0);
1113 INIT_WORK(&pkt
->work
, xfer_work
);
1114 schedule_work(&pkt
->work
);
1118 usbhsf_pio_prepare_pop_unselect
:
1119 usbhsf_fifo_unselect(pipe
, fifo
);
1120 usbhsf_pio_prepare_pop
:
1123 * change handler to PIO
1125 pkt
->handler
= &usbhs_fifo_pio_pop_handler
;
1127 return pkt
->handler
->try_run(pkt
, is_done
);
1130 static int usbhsf_dma_try_pop(struct usbhs_pkt
*pkt
, int *is_done
)
1132 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pkt
->pipe
);
1134 BUG_ON(usbhs_get_dparam(priv
, has_usb_dmac
));
1136 return usbhsf_dma_try_pop_with_rx_irq(pkt
, is_done
);
1139 static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt
*pkt
, int *is_done
)
1141 struct usbhs_pipe
*pipe
= pkt
->pipe
;
1142 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
1144 usbhsf_dma_stop(pipe
, pipe
->fifo
);
1145 usbhsf_dma_unmap(pkt
);
1146 usbhsf_fifo_unselect(pipe
, pipe
->fifo
);
1148 pkt
->actual
+= pkt
->trans
;
1150 if ((pkt
->actual
== pkt
->length
) || /* receive all data */
1151 (pkt
->trans
< maxp
)) { /* short packet */
1153 usbhs_pipe_running(pipe
, 0);
1156 usbhs_pipe_running(pipe
, 0);
1157 usbhsf_prepare_pop(pkt
, is_done
);
1163 static size_t usbhs_dma_calc_received_size(struct usbhs_pkt
*pkt
,
1164 struct dma_chan
*chan
, int dtln
)
1166 struct usbhs_pipe
*pipe
= pkt
->pipe
;
1167 size_t received_size
;
1168 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
1170 received_size
= pkt
->length
- pkt
->dma_result
->residue
;
1173 received_size
-= USBHS_USB_DMAC_XFER_SIZE
;
1174 received_size
&= ~(maxp
- 1);
1175 received_size
+= dtln
;
1178 return received_size
;
1181 static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt
*pkt
,
1184 struct usbhs_pipe
*pipe
= pkt
->pipe
;
1185 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
1186 struct usbhs_fifo
*fifo
= usbhs_pipe_to_fifo(pipe
);
1187 struct dma_chan
*chan
= usbhsf_dma_chan_get(fifo
, pkt
);
1191 * Since the driver disables rx_irq in DMA mode, the interrupt handler
1192 * cannot the BRDYSTS. So, the function clears it here because the
1193 * driver may use PIO mode next time.
1195 usbhs_xxxsts_clear(priv
, BRDYSTS
, usbhs_pipe_number(pipe
));
1197 rcv_len
= usbhsf_fifo_rcv_len(priv
, fifo
);
1198 usbhsf_fifo_clear(pipe
, fifo
);
1199 pkt
->actual
= usbhs_dma_calc_received_size(pkt
, chan
, rcv_len
);
1201 usbhs_pipe_running(pipe
, 0);
1202 usbhsf_dma_stop(pipe
, fifo
);
1203 usbhsf_dma_unmap(pkt
);
1204 usbhsf_fifo_unselect(pipe
, pipe
->fifo
);
1206 /* The driver can assume the rx transaction is always "done" */
1212 static int usbhsf_dma_pop_done(struct usbhs_pkt
*pkt
, int *is_done
)
1214 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pkt
->pipe
);
1216 if (usbhs_get_dparam(priv
, has_usb_dmac
))
1217 return usbhsf_dma_pop_done_with_usb_dmac(pkt
, is_done
);
1219 return usbhsf_dma_pop_done_with_rx_irq(pkt
, is_done
);
1222 const struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler
= {
1223 .prepare
= usbhsf_dma_prepare_pop
,
1224 .try_run
= usbhsf_dma_try_pop
,
1225 .dma_done
= usbhsf_dma_pop_done
1231 static bool usbhsf_dma_filter(struct dma_chan
*chan
, void *param
)
1233 struct sh_dmae_slave
*slave
= param
;
1238 * usbhs doesn't recognize id = 0 as valid DMA
1240 if (0 == slave
->shdma_slave
.slave_id
)
1243 chan
->private = slave
;
1248 static void usbhsf_dma_quit(struct usbhs_priv
*priv
, struct usbhs_fifo
*fifo
)
1251 dma_release_channel(fifo
->tx_chan
);
1253 dma_release_channel(fifo
->rx_chan
);
1255 fifo
->tx_chan
= NULL
;
1256 fifo
->rx_chan
= NULL
;
1259 static void usbhsf_dma_init_pdev(struct usbhs_fifo
*fifo
)
1261 dma_cap_mask_t mask
;
1264 dma_cap_set(DMA_SLAVE
, mask
);
1265 fifo
->tx_chan
= dma_request_channel(mask
, usbhsf_dma_filter
,
1269 dma_cap_set(DMA_SLAVE
, mask
);
1270 fifo
->rx_chan
= dma_request_channel(mask
, usbhsf_dma_filter
,
1274 static void usbhsf_dma_init_dt(struct device
*dev
, struct usbhs_fifo
*fifo
,
1280 * To avoid complex handing for DnFIFOs, the driver uses each
1281 * DnFIFO as TX or RX direction (not bi-direction).
1282 * So, the driver uses odd channels for TX, even channels for RX.
1284 snprintf(name
, sizeof(name
), "ch%d", channel
);
1286 fifo
->tx_chan
= dma_request_chan(dev
, name
);
1287 if (IS_ERR(fifo
->tx_chan
))
1288 fifo
->tx_chan
= NULL
;
1290 fifo
->rx_chan
= dma_request_chan(dev
, name
);
1291 if (IS_ERR(fifo
->rx_chan
))
1292 fifo
->rx_chan
= NULL
;
1296 static void usbhsf_dma_init(struct usbhs_priv
*priv
, struct usbhs_fifo
*fifo
,
1299 struct device
*dev
= usbhs_priv_to_dev(priv
);
1301 if (dev_of_node(dev
))
1302 usbhsf_dma_init_dt(dev
, fifo
, channel
);
1304 usbhsf_dma_init_pdev(fifo
);
1306 if (fifo
->tx_chan
|| fifo
->rx_chan
)
1307 dev_dbg(dev
, "enable DMAEngine (%s%s%s)\n",
1309 fifo
->tx_chan
? "[TX]" : " ",
1310 fifo
->rx_chan
? "[RX]" : " ");
1316 static int usbhsf_irq_empty(struct usbhs_priv
*priv
,
1317 struct usbhs_irq_state
*irq_state
)
1319 struct usbhs_pipe
*pipe
;
1320 struct device
*dev
= usbhs_priv_to_dev(priv
);
1323 if (!irq_state
->bempsts
) {
1324 dev_err(dev
, "debug %s !!\n", __func__
);
1328 dev_dbg(dev
, "irq empty [0x%04x]\n", irq_state
->bempsts
);
1331 * search interrupted "pipe"
1334 usbhs_for_each_pipe_with_dcp(pipe
, priv
, i
) {
1335 if (!(irq_state
->bempsts
& (1 << i
)))
1338 ret
= usbhsf_pkt_handler(pipe
, USBHSF_PKT_TRY_RUN
);
1340 dev_err(dev
, "irq_empty run_error %d : %d\n", i
, ret
);
1346 static int usbhsf_irq_ready(struct usbhs_priv
*priv
,
1347 struct usbhs_irq_state
*irq_state
)
1349 struct usbhs_pipe
*pipe
;
1350 struct device
*dev
= usbhs_priv_to_dev(priv
);
1353 if (!irq_state
->brdysts
) {
1354 dev_err(dev
, "debug %s !!\n", __func__
);
1358 dev_dbg(dev
, "irq ready [0x%04x]\n", irq_state
->brdysts
);
1361 * search interrupted "pipe"
1364 usbhs_for_each_pipe_with_dcp(pipe
, priv
, i
) {
1365 if (!(irq_state
->brdysts
& (1 << i
)))
1368 ret
= usbhsf_pkt_handler(pipe
, USBHSF_PKT_TRY_RUN
);
1370 dev_err(dev
, "irq_ready run_error %d : %d\n", i
, ret
);
1376 static void usbhsf_dma_complete(void *arg
,
1377 const struct dmaengine_result
*result
)
1379 struct usbhs_pkt
*pkt
= arg
;
1380 struct usbhs_pipe
*pipe
= pkt
->pipe
;
1381 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
1382 struct device
*dev
= usbhs_priv_to_dev(priv
);
1385 pkt
->dma_result
= result
;
1386 ret
= usbhsf_pkt_handler(pipe
, USBHSF_PKT_DMA_DONE
);
1388 dev_err(dev
, "dma_complete run_error %d : %d\n",
1389 usbhs_pipe_number(pipe
), ret
);
1392 void usbhs_fifo_clear_dcp(struct usbhs_pipe
*pipe
)
1394 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
1395 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
1397 /* clear DCP FIFO of transmission */
1398 if (usbhsf_fifo_select(pipe
, fifo
, 1) < 0)
1400 usbhsf_fifo_clear(pipe
, fifo
);
1401 usbhsf_fifo_unselect(pipe
, fifo
);
1403 /* clear DCP FIFO of reception */
1404 if (usbhsf_fifo_select(pipe
, fifo
, 0) < 0)
1406 usbhsf_fifo_clear(pipe
, fifo
);
1407 usbhsf_fifo_unselect(pipe
, fifo
);
1413 void usbhs_fifo_init(struct usbhs_priv
*priv
)
1415 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
1416 struct usbhs_fifo
*cfifo
= usbhsf_get_cfifo(priv
);
1417 struct usbhs_fifo
*dfifo
;
1420 mod
->irq_empty
= usbhsf_irq_empty
;
1421 mod
->irq_ready
= usbhsf_irq_ready
;
1422 mod
->irq_bempsts
= 0;
1423 mod
->irq_brdysts
= 0;
1426 usbhs_for_each_dfifo(priv
, dfifo
, i
)
1430 void usbhs_fifo_quit(struct usbhs_priv
*priv
)
1432 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
1434 mod
->irq_empty
= NULL
;
1435 mod
->irq_ready
= NULL
;
1436 mod
->irq_bempsts
= 0;
1437 mod
->irq_brdysts
= 0;
1440 #define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port) \
1442 fifo = usbhsf_get_dnfifo(priv, channel); \
1443 fifo->name = "D"#channel"FIFO"; \
1444 fifo->port = fifo_port; \
1445 fifo->sel = D##channel##FIFOSEL; \
1446 fifo->ctr = D##channel##FIFOCTR; \
1447 fifo->tx_slave.shdma_slave.slave_id = \
1448 usbhs_get_dparam(priv, d##channel##_tx_id); \
1449 fifo->rx_slave.shdma_slave.slave_id = \
1450 usbhs_get_dparam(priv, d##channel##_rx_id); \
1451 usbhsf_dma_init(priv, fifo, channel); \
1454 #define USBHS_DFIFO_INIT(priv, fifo, channel) \
1455 __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
1456 #define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel) \
1457 __USBHS_DFIFO_INIT(priv, fifo, channel, 0)
1459 int usbhs_fifo_probe(struct usbhs_priv
*priv
)
1461 struct usbhs_fifo
*fifo
;
1464 fifo
= usbhsf_get_cfifo(priv
);
1465 fifo
->name
= "CFIFO";
1467 fifo
->sel
= CFIFOSEL
;
1468 fifo
->ctr
= CFIFOCTR
;
1471 USBHS_DFIFO_INIT(priv
, fifo
, 0);
1472 USBHS_DFIFO_INIT(priv
, fifo
, 1);
1473 USBHS_DFIFO_INIT_NO_PORT(priv
, fifo
, 2);
1474 USBHS_DFIFO_INIT_NO_PORT(priv
, fifo
, 3);
1479 void usbhs_fifo_remove(struct usbhs_priv
*priv
)
1481 struct usbhs_fifo
*fifo
;
1484 usbhs_for_each_dfifo(priv
, fifo
, i
)
1485 usbhsf_dma_quit(priv
, fifo
);