1 // SPDX-License-Identifier: GPL-1.0+
5 * Copyright (C) 2011 Renesas Solutions Corp.
6 * Copyright (C) 2019 Renesas Electronics Corporation
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 struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
106 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
107 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
110 /******************** spin lock ********************/
111 usbhs_lock(priv, flags);
113 usbhs_pipe_disable(pipe);
116 pkt = __usbhsf_pkt_get(pipe);
119 struct dma_chan *chan = NULL;
122 chan = usbhsf_dma_chan_get(fifo, pkt);
124 dmaengine_terminate_all(chan);
125 usbhsf_dma_unmap(pkt);
128 usbhs_pipe_clear_without_sequence(pipe, 0, 0);
129 usbhs_pipe_running(pipe, 0);
131 __usbhsf_pkt_del(pkt);
135 usbhsf_fifo_unselect(pipe, fifo);
137 usbhs_unlock(priv, flags);
138 /******************** spin unlock ******************/
149 static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
151 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
152 struct usbhs_pkt *pkt;
153 struct device *dev = usbhs_priv_to_dev(priv);
154 int (*func)(struct usbhs_pkt *pkt, int *is_done);
159 /******************** spin lock ********************/
160 usbhs_lock(priv, flags);
162 pkt = __usbhsf_pkt_get(pipe);
164 goto __usbhs_pkt_handler_end;
167 case USBHSF_PKT_PREPARE:
168 func = pkt->handler->prepare;
170 case USBHSF_PKT_TRY_RUN:
171 func = pkt->handler->try_run;
173 case USBHSF_PKT_DMA_DONE:
174 func = pkt->handler->dma_done;
177 dev_err(dev, "unknown pkt handler\n");
178 goto __usbhs_pkt_handler_end;
182 ret = func(pkt, &is_done);
185 __usbhsf_pkt_del(pkt);
187 __usbhs_pkt_handler_end:
188 usbhs_unlock(priv, flags);
189 /******************** spin unlock ******************/
192 pkt->done(priv, pkt);
193 usbhs_pkt_start(pipe);
199 void usbhs_pkt_start(struct usbhs_pipe *pipe)
201 usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
205 * irq enable/disable function
207 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
208 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
209 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
211 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
212 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
213 u16 status = (1 << usbhs_pipe_number(pipe)); \
217 mod->status |= status; \
219 mod->status &= ~status; \
220 usbhs_irq_callback_update(priv, mod); \
223 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
226 * And DCP pipe can NOT use "ready interrupt" for "send"
227 * it should use "empty" interrupt.
229 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
231 * on the other hand, normal pipe can use "ready interrupt" for "send"
232 * even though it is single/double buffer
234 if (usbhs_pipe_is_dcp(pipe))
235 usbhsf_irq_empty_ctrl(pipe, enable);
237 usbhsf_irq_ready_ctrl(pipe, enable);
240 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
242 usbhsf_irq_ready_ctrl(pipe, enable);
248 static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
249 struct usbhs_fifo *fifo)
251 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
253 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
256 static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
257 struct usbhs_fifo *fifo)
259 /* The FIFO port is accessible */
260 if (usbhs_read(priv, fifo->ctr) & FRDY)
266 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
267 struct usbhs_fifo *fifo)
269 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
272 if (!usbhs_pipe_is_dcp(pipe)) {
274 * This driver checks the pipe condition first to avoid -EBUSY
275 * from usbhsf_fifo_barrier() if the pipe is RX direction and
278 if (usbhs_pipe_is_dir_in(pipe))
279 ret = usbhs_pipe_is_accessible(pipe);
281 ret = usbhsf_fifo_barrier(priv, fifo);
285 * if non-DCP pipe, this driver should set BCLR when
286 * usbhsf_fifo_barrier() returns 0.
289 usbhs_write(priv, fifo->ctr, BCLR);
292 static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
293 struct usbhs_fifo *fifo)
295 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
298 static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
299 struct usbhs_fifo *fifo)
301 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
303 usbhs_pipe_select_fifo(pipe, NULL);
304 usbhs_write(priv, fifo->sel, 0);
307 static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
308 struct usbhs_fifo *fifo,
311 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
312 struct device *dev = usbhs_priv_to_dev(priv);
314 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
315 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
317 if (usbhs_pipe_is_busy(pipe) ||
318 usbhsf_fifo_is_busy(fifo))
321 if (usbhs_pipe_is_dcp(pipe)) {
322 base |= (1 == write) << 5; /* ISEL */
324 if (usbhs_mod_is_host(priv))
325 usbhs_dcp_dir_for_host(pipe, write);
328 /* "base" will be used below */
329 usbhs_write(priv, fifo->sel, base | MBW_32);
331 /* check ISEL and CURPIPE value */
333 if (base == (mask & usbhs_read(priv, fifo->sel))) {
334 usbhs_pipe_select_fifo(pipe, fifo);
340 dev_err(dev, "fifo select error\n");
348 static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
350 struct usbhs_pipe *pipe = pkt->pipe;
351 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
352 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
353 struct device *dev = usbhs_priv_to_dev(priv);
356 usbhs_pipe_disable(pipe);
358 ret = usbhsf_fifo_select(pipe, fifo, 1);
360 dev_err(dev, "%s() faile\n", __func__);
364 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
366 usbhsf_fifo_clear(pipe, fifo);
367 usbhsf_send_terminator(pipe, fifo);
369 usbhsf_fifo_unselect(pipe, fifo);
371 usbhsf_tx_irq_ctrl(pipe, 1);
372 usbhs_pipe_enable(pipe);
377 static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
379 struct usbhs_pipe *pipe = pkt->pipe;
380 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
381 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
382 struct device *dev = usbhs_priv_to_dev(priv);
385 usbhs_pipe_disable(pipe);
387 ret = usbhsf_fifo_select(pipe, fifo, 0);
389 dev_err(dev, "%s() fail\n", __func__);
393 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
394 usbhsf_fifo_clear(pipe, fifo);
396 usbhsf_fifo_unselect(pipe, fifo);
398 usbhsf_rx_irq_ctrl(pipe, 1);
399 usbhs_pipe_enable(pipe);
405 static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
407 struct usbhs_pipe *pipe = pkt->pipe;
409 if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
410 usbhsf_tx_irq_ctrl(pipe, 0);
412 usbhsf_rx_irq_ctrl(pipe, 0);
414 pkt->actual = pkt->length;
420 const struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
421 .prepare = usbhs_dcp_dir_switch_to_write,
422 .try_run = usbhs_dcp_dir_switch_done,
425 const struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
426 .prepare = usbhs_dcp_dir_switch_to_read,
427 .try_run = usbhs_dcp_dir_switch_done,
431 * DCP data stage (push)
433 static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
435 struct usbhs_pipe *pipe = pkt->pipe;
437 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
440 * change handler to PIO push
442 pkt->handler = &usbhs_fifo_pio_push_handler;
444 return pkt->handler->prepare(pkt, is_done);
447 const struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
448 .prepare = usbhsf_dcp_data_stage_try_push,
452 * DCP data stage (pop)
454 static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
457 struct usbhs_pipe *pipe = pkt->pipe;
458 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
459 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
461 if (usbhs_pipe_is_busy(pipe))
465 * prepare pop for DCP should
466 * - change DCP direction,
470 usbhs_pipe_disable(pipe);
472 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
474 usbhsf_fifo_select(pipe, fifo, 0);
475 usbhsf_fifo_clear(pipe, fifo);
476 usbhsf_fifo_unselect(pipe, fifo);
479 * change handler to PIO pop
481 pkt->handler = &usbhs_fifo_pio_pop_handler;
483 return pkt->handler->prepare(pkt, is_done);
486 const struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
487 .prepare = usbhsf_dcp_data_stage_prepare_pop,
493 static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
495 struct usbhs_pipe *pipe = pkt->pipe;
496 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
497 struct device *dev = usbhs_priv_to_dev(priv);
498 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
499 void __iomem *addr = priv->base + fifo->port;
501 int maxp = usbhs_pipe_get_maxpacket(pipe);
506 usbhs_pipe_data_sequence(pipe, pkt->sequence);
507 pkt->sequence = -1; /* -1 sequence will be ignored */
509 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
511 ret = usbhsf_fifo_select(pipe, fifo, 1);
515 ret = usbhs_pipe_is_accessible(pipe);
517 /* inaccessible pipe is not an error */
519 goto usbhs_fifo_write_busy;
522 ret = usbhsf_fifo_barrier(priv, fifo);
524 goto usbhs_fifo_write_busy;
526 buf = pkt->buf + pkt->actual;
527 len = pkt->length - pkt->actual;
528 len = min(len, maxp);
530 is_short = total_len < maxp;
537 if (len >= 4 && !((unsigned long)buf & 0x03)) {
538 iowrite32_rep(addr, buf, len / 4);
540 buf += total_len - len;
543 /* the rest operation */
544 if (usbhs_get_dparam(priv, cfifo_byte_addr)) {
545 for (i = 0; i < len; i++)
546 iowrite8(buf[i], addr + (i & 0x03));
548 for (i = 0; i < len; i++)
549 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
555 pkt->actual += total_len;
557 if (pkt->actual < pkt->length)
558 *is_done = 0; /* there are remainder data */
560 *is_done = 1; /* short packet */
562 *is_done = !pkt->zero; /* send zero packet ? */
568 usbhsf_send_terminator(pipe, fifo);
570 usbhsf_tx_irq_ctrl(pipe, !*is_done);
571 usbhs_pipe_running(pipe, !*is_done);
572 usbhs_pipe_enable(pipe);
574 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
575 usbhs_pipe_number(pipe),
576 pkt->length, pkt->actual, *is_done, pkt->zero);
578 usbhsf_fifo_unselect(pipe, fifo);
582 usbhs_fifo_write_busy:
583 usbhsf_fifo_unselect(pipe, fifo);
589 usbhsf_tx_irq_ctrl(pipe, 1);
590 usbhs_pipe_running(pipe, 1);
595 static int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done)
597 if (usbhs_pipe_is_running(pkt->pipe))
600 return usbhsf_pio_try_push(pkt, is_done);
603 const struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
604 .prepare = usbhsf_pio_prepare_push,
605 .try_run = usbhsf_pio_try_push,
611 static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
613 struct usbhs_pipe *pipe = pkt->pipe;
614 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
615 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
617 if (usbhs_pipe_is_busy(pipe))
620 if (usbhs_pipe_is_running(pipe))
624 * pipe enable to prepare packet receive
626 usbhs_pipe_data_sequence(pipe, pkt->sequence);
627 pkt->sequence = -1; /* -1 sequence will be ignored */
629 if (usbhs_pipe_is_dcp(pipe))
630 usbhsf_fifo_clear(pipe, fifo);
632 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
633 usbhs_pipe_enable(pipe);
634 usbhs_pipe_running(pipe, 1);
635 usbhsf_rx_irq_ctrl(pipe, 1);
640 static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
642 struct usbhs_pipe *pipe = pkt->pipe;
643 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
644 struct device *dev = usbhs_priv_to_dev(priv);
645 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
646 void __iomem *addr = priv->base + fifo->port;
649 int maxp = usbhs_pipe_get_maxpacket(pipe);
654 ret = usbhsf_fifo_select(pipe, fifo, 0);
658 ret = usbhsf_fifo_barrier(priv, fifo);
660 goto usbhs_fifo_read_busy;
662 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
664 buf = pkt->buf + pkt->actual;
665 len = pkt->length - pkt->actual;
666 len = min(len, rcv_len);
670 * update actual length first here to decide disable pipe.
671 * if this pipe keeps BUF status and all data were popped,
672 * then, next interrupt/token will be issued again
674 pkt->actual += total_len;
676 if ((pkt->actual == pkt->length) || /* receive all data */
677 (total_len < maxp)) { /* short packet */
679 usbhsf_rx_irq_ctrl(pipe, 0);
680 usbhs_pipe_running(pipe, 0);
682 * If function mode, since this controller is possible to enter
683 * Control Write status stage at this timing, this driver
684 * should not disable the pipe. If such a case happens, this
685 * controller is not able to complete the status stage.
687 if (!usbhs_mod_is_host(priv) && !usbhs_pipe_is_dcp(pipe))
688 usbhs_pipe_disable(pipe); /* disable pipe first */
692 * Buffer clear if Zero-Length packet
695 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
699 usbhsf_fifo_clear(pipe, fifo);
700 goto usbhs_fifo_read_end;
708 if (len >= 4 && !((unsigned long)buf & 0x03)) {
709 ioread32_rep(addr, buf, len / 4);
711 buf += total_len - len;
714 /* the rest operation */
715 for (i = 0; i < len; i++) {
717 data = ioread32(addr);
719 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
723 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
724 usbhs_pipe_number(pipe),
725 pkt->length, pkt->actual, *is_done, pkt->zero);
727 usbhs_fifo_read_busy:
728 usbhsf_fifo_unselect(pipe, fifo);
733 const struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
734 .prepare = usbhsf_prepare_pop,
735 .try_run = usbhsf_pio_try_pop,
739 * DCP ctrol statge handler
741 static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
743 usbhs_dcp_control_transfer_done(pkt->pipe);
750 const struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
751 .prepare = usbhsf_ctrl_stage_end,
752 .try_run = usbhsf_ctrl_stage_end,
758 static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
759 struct usbhs_pkt *pkt)
761 if (&usbhs_fifo_dma_push_handler == pkt->handler)
762 return fifo->tx_chan;
764 if (&usbhs_fifo_dma_pop_handler == pkt->handler)
765 return fifo->rx_chan;
770 static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
771 struct usbhs_pkt *pkt)
773 struct usbhs_fifo *fifo;
776 usbhs_for_each_dfifo(priv, fifo, i) {
777 if (usbhsf_dma_chan_get(fifo, pkt) &&
778 !usbhsf_fifo_is_busy(fifo))
785 #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
786 #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
787 static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
788 struct usbhs_fifo *fifo,
791 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
793 usbhs_bset(priv, fifo->sel, DREQE, dreqe);
796 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
798 struct usbhs_pipe *pipe = pkt->pipe;
799 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
800 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
801 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
802 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
804 return info->dma_map_ctrl(chan->device->dev, pkt, map);
807 static void usbhsf_dma_complete(void *arg,
808 const struct dmaengine_result *result);
809 static void usbhsf_dma_xfer_preparing(struct usbhs_pkt *pkt)
811 struct usbhs_pipe *pipe = pkt->pipe;
812 struct usbhs_fifo *fifo;
813 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
814 struct dma_async_tx_descriptor *desc;
815 struct dma_chan *chan;
816 struct device *dev = usbhs_priv_to_dev(priv);
817 enum dma_transfer_direction dir;
820 fifo = usbhs_pipe_to_fifo(pipe);
824 chan = usbhsf_dma_chan_get(fifo, pkt);
825 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
827 desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
829 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
833 desc->callback_result = usbhsf_dma_complete;
834 desc->callback_param = pkt;
836 cookie = dmaengine_submit(desc);
838 dev_err(dev, "Failed to submit dma descriptor\n");
842 dev_dbg(dev, " %s %d (%d/ %d)\n",
843 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
845 usbhs_pipe_running(pipe, 1);
846 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
847 dma_async_issue_pending(chan);
848 usbhsf_dma_start(pipe, fifo);
849 usbhs_pipe_enable(pipe);
852 static void xfer_work(struct work_struct *work)
854 struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
855 struct usbhs_pipe *pipe = pkt->pipe;
856 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
859 usbhs_lock(priv, flags);
860 usbhsf_dma_xfer_preparing(pkt);
861 usbhs_unlock(priv, flags);
867 static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
869 struct usbhs_pipe *pipe = pkt->pipe;
870 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
871 struct usbhs_fifo *fifo;
872 int len = pkt->length - pkt->actual;
874 uintptr_t align_mask;
876 if (usbhs_pipe_is_busy(pipe))
879 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
880 if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
881 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
882 goto usbhsf_pio_prepare_push;
884 /* check data length if this driver don't use USB-DMAC */
885 if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7)
886 goto usbhsf_pio_prepare_push;
888 /* check buffer alignment */
889 align_mask = usbhs_get_dparam(priv, has_usb_dmac) ?
890 USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7;
891 if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask)
892 goto usbhsf_pio_prepare_push;
894 /* return at this time if the pipe is running */
895 if (usbhs_pipe_is_running(pipe))
898 /* get enable DMA fifo */
899 fifo = usbhsf_get_dma_fifo(priv, pkt);
901 goto usbhsf_pio_prepare_push;
903 ret = usbhsf_fifo_select(pipe, fifo, 0);
905 goto usbhsf_pio_prepare_push;
907 if (usbhsf_dma_map(pkt) < 0)
908 goto usbhsf_pio_prepare_push_unselect;
912 usbhsf_tx_irq_ctrl(pipe, 0);
913 /* FIXME: Workaound for usb dmac that driver can be used in atomic */
914 if (usbhs_get_dparam(priv, has_usb_dmac)) {
915 usbhsf_dma_xfer_preparing(pkt);
917 INIT_WORK(&pkt->work, xfer_work);
918 schedule_work(&pkt->work);
923 usbhsf_pio_prepare_push_unselect:
924 usbhsf_fifo_unselect(pipe, fifo);
925 usbhsf_pio_prepare_push:
927 * change handler to PIO
929 pkt->handler = &usbhs_fifo_pio_push_handler;
931 return pkt->handler->prepare(pkt, is_done);
934 static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
936 struct usbhs_pipe *pipe = pkt->pipe;
937 int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
939 pkt->actual += pkt->trans;
941 if (pkt->actual < pkt->length)
942 *is_done = 0; /* there are remainder data */
944 *is_done = 1; /* short packet */
946 *is_done = !pkt->zero; /* send zero packet? */
948 usbhs_pipe_running(pipe, !*is_done);
950 usbhsf_dma_stop(pipe, pipe->fifo);
951 usbhsf_dma_unmap(pkt);
952 usbhsf_fifo_unselect(pipe, pipe->fifo);
955 /* change handler to PIO */
956 pkt->handler = &usbhs_fifo_pio_push_handler;
957 return pkt->handler->try_run(pkt, is_done);
963 const struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
964 .prepare = usbhsf_dma_prepare_push,
965 .dma_done = usbhsf_dma_push_done,
972 static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
975 return usbhsf_prepare_pop(pkt, is_done);
978 static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt,
981 struct usbhs_pipe *pipe = pkt->pipe;
982 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
983 struct usbhs_fifo *fifo;
986 if (usbhs_pipe_is_busy(pipe))
989 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
990 if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) ||
991 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
992 goto usbhsf_pio_prepare_pop;
994 fifo = usbhsf_get_dma_fifo(priv, pkt);
996 goto usbhsf_pio_prepare_pop;
998 if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1))
999 goto usbhsf_pio_prepare_pop;
1001 /* return at this time if the pipe is running */
1002 if (usbhs_pipe_is_running(pipe))
1005 usbhs_pipe_config_change_bfre(pipe, 1);
1007 ret = usbhsf_fifo_select(pipe, fifo, 0);
1009 goto usbhsf_pio_prepare_pop;
1011 if (usbhsf_dma_map(pkt) < 0)
1012 goto usbhsf_pio_prepare_pop_unselect;
1017 * usbhs_fifo_dma_pop_handler :: prepare
1018 * enabled irq to come here.
1019 * but it is no longer needed for DMA. disable it.
1021 usbhsf_rx_irq_ctrl(pipe, 0);
1023 pkt->trans = pkt->length;
1025 usbhsf_dma_xfer_preparing(pkt);
1029 usbhsf_pio_prepare_pop_unselect:
1030 usbhsf_fifo_unselect(pipe, fifo);
1031 usbhsf_pio_prepare_pop:
1034 * change handler to PIO
1036 pkt->handler = &usbhs_fifo_pio_pop_handler;
1037 usbhs_pipe_config_change_bfre(pipe, 0);
1039 return pkt->handler->prepare(pkt, is_done);
1042 static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
1044 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1046 if (usbhs_get_dparam(priv, has_usb_dmac))
1047 return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done);
1049 return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
1052 static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
1054 struct usbhs_pipe *pipe = pkt->pipe;
1055 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1056 struct usbhs_fifo *fifo;
1059 if (usbhs_pipe_is_busy(pipe))
1062 if (usbhs_pipe_is_dcp(pipe))
1063 goto usbhsf_pio_prepare_pop;
1065 /* get enable DMA fifo */
1066 fifo = usbhsf_get_dma_fifo(priv, pkt);
1068 goto usbhsf_pio_prepare_pop;
1070 if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
1071 goto usbhsf_pio_prepare_pop;
1073 ret = usbhsf_fifo_select(pipe, fifo, 0);
1075 goto usbhsf_pio_prepare_pop;
1077 /* use PIO if packet is less than pio_dma_border */
1078 len = usbhsf_fifo_rcv_len(priv, fifo);
1079 len = min(pkt->length - pkt->actual, len);
1080 if (len & 0x7) /* 8byte alignment */
1081 goto usbhsf_pio_prepare_pop_unselect;
1083 if (len < usbhs_get_dparam(priv, pio_dma_border))
1084 goto usbhsf_pio_prepare_pop_unselect;
1086 ret = usbhsf_fifo_barrier(priv, fifo);
1088 goto usbhsf_pio_prepare_pop_unselect;
1090 if (usbhsf_dma_map(pkt) < 0)
1091 goto usbhsf_pio_prepare_pop_unselect;
1096 * usbhs_fifo_dma_pop_handler :: prepare
1097 * enabled irq to come here.
1098 * but it is no longer needed for DMA. disable it.
1100 usbhsf_rx_irq_ctrl(pipe, 0);
1104 INIT_WORK(&pkt->work, xfer_work);
1105 schedule_work(&pkt->work);
1109 usbhsf_pio_prepare_pop_unselect:
1110 usbhsf_fifo_unselect(pipe, fifo);
1111 usbhsf_pio_prepare_pop:
1114 * change handler to PIO
1116 pkt->handler = &usbhs_fifo_pio_pop_handler;
1118 return pkt->handler->try_run(pkt, is_done);
1121 static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
1123 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1125 BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
1127 return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
1130 static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
1132 struct usbhs_pipe *pipe = pkt->pipe;
1133 int maxp = usbhs_pipe_get_maxpacket(pipe);
1135 usbhsf_dma_stop(pipe, pipe->fifo);
1136 usbhsf_dma_unmap(pkt);
1137 usbhsf_fifo_unselect(pipe, pipe->fifo);
1139 pkt->actual += pkt->trans;
1141 if ((pkt->actual == pkt->length) || /* receive all data */
1142 (pkt->trans < maxp)) { /* short packet */
1144 usbhs_pipe_running(pipe, 0);
1147 usbhs_pipe_running(pipe, 0);
1148 usbhsf_prepare_pop(pkt, is_done);
1154 static size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt,
1155 struct dma_chan *chan, int dtln)
1157 struct usbhs_pipe *pipe = pkt->pipe;
1158 size_t received_size;
1159 int maxp = usbhs_pipe_get_maxpacket(pipe);
1161 received_size = pkt->length - pkt->dma_result->residue;
1164 received_size -= USBHS_USB_DMAC_XFER_SIZE;
1165 received_size &= ~(maxp - 1);
1166 received_size += dtln;
1169 return received_size;
1172 static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt,
1175 struct usbhs_pipe *pipe = pkt->pipe;
1176 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1177 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
1178 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
1182 * Since the driver disables rx_irq in DMA mode, the interrupt handler
1183 * cannot the BRDYSTS. So, the function clears it here because the
1184 * driver may use PIO mode next time.
1186 usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe));
1188 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
1189 usbhsf_fifo_clear(pipe, fifo);
1190 pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len);
1192 usbhs_pipe_running(pipe, 0);
1193 usbhsf_dma_stop(pipe, fifo);
1194 usbhsf_dma_unmap(pkt);
1195 usbhsf_fifo_unselect(pipe, pipe->fifo);
1197 /* The driver can assume the rx transaction is always "done" */
1203 static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
1205 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1207 if (usbhs_get_dparam(priv, has_usb_dmac))
1208 return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done);
1210 return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
1213 const struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
1214 .prepare = usbhsf_dma_prepare_pop,
1215 .try_run = usbhsf_dma_try_pop,
1216 .dma_done = usbhsf_dma_pop_done
1222 static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
1224 struct sh_dmae_slave *slave = param;
1229 * usbhs doesn't recognize id = 0 as valid DMA
1231 if (0 == slave->shdma_slave.slave_id)
1234 chan->private = slave;
1239 static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
1242 dma_release_channel(fifo->tx_chan);
1244 dma_release_channel(fifo->rx_chan);
1246 fifo->tx_chan = NULL;
1247 fifo->rx_chan = NULL;
1250 static void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo)
1252 dma_cap_mask_t mask;
1255 dma_cap_set(DMA_SLAVE, mask);
1256 fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1260 dma_cap_set(DMA_SLAVE, mask);
1261 fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1265 static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo,
1271 * To avoid complex handing for DnFIFOs, the driver uses each
1272 * DnFIFO as TX or RX direction (not bi-direction).
1273 * So, the driver uses odd channels for TX, even channels for RX.
1275 snprintf(name, sizeof(name), "ch%d", channel);
1277 fifo->tx_chan = dma_request_chan(dev, name);
1278 if (IS_ERR(fifo->tx_chan))
1279 fifo->tx_chan = NULL;
1281 fifo->rx_chan = dma_request_chan(dev, name);
1282 if (IS_ERR(fifo->rx_chan))
1283 fifo->rx_chan = NULL;
1287 static void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo,
1290 struct device *dev = usbhs_priv_to_dev(priv);
1292 if (dev_of_node(dev))
1293 usbhsf_dma_init_dt(dev, fifo, channel);
1295 usbhsf_dma_init_pdev(fifo);
1297 if (fifo->tx_chan || fifo->rx_chan)
1298 dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
1300 fifo->tx_chan ? "[TX]" : " ",
1301 fifo->rx_chan ? "[RX]" : " ");
1307 static int usbhsf_irq_empty(struct usbhs_priv *priv,
1308 struct usbhs_irq_state *irq_state)
1310 struct usbhs_pipe *pipe;
1311 struct device *dev = usbhs_priv_to_dev(priv);
1314 if (!irq_state->bempsts) {
1315 dev_err(dev, "debug %s !!\n", __func__);
1319 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
1322 * search interrupted "pipe"
1325 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1326 if (!(irq_state->bempsts & (1 << i)))
1329 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1331 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
1337 static int usbhsf_irq_ready(struct usbhs_priv *priv,
1338 struct usbhs_irq_state *irq_state)
1340 struct usbhs_pipe *pipe;
1341 struct device *dev = usbhs_priv_to_dev(priv);
1344 if (!irq_state->brdysts) {
1345 dev_err(dev, "debug %s !!\n", __func__);
1349 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
1352 * search interrupted "pipe"
1355 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1356 if (!(irq_state->brdysts & (1 << i)))
1359 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1361 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
1367 static void usbhsf_dma_complete(void *arg,
1368 const struct dmaengine_result *result)
1370 struct usbhs_pkt *pkt = arg;
1371 struct usbhs_pipe *pipe = pkt->pipe;
1372 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1373 struct device *dev = usbhs_priv_to_dev(priv);
1376 pkt->dma_result = result;
1377 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
1379 dev_err(dev, "dma_complete run_error %d : %d\n",
1380 usbhs_pipe_number(pipe), ret);
1383 void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
1385 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1386 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
1388 /* clear DCP FIFO of transmission */
1389 if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
1391 usbhsf_fifo_clear(pipe, fifo);
1392 usbhsf_fifo_unselect(pipe, fifo);
1394 /* clear DCP FIFO of reception */
1395 if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
1397 usbhsf_fifo_clear(pipe, fifo);
1398 usbhsf_fifo_unselect(pipe, fifo);
1404 void usbhs_fifo_init(struct usbhs_priv *priv)
1406 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1407 struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
1408 struct usbhs_fifo *dfifo;
1411 mod->irq_empty = usbhsf_irq_empty;
1412 mod->irq_ready = usbhsf_irq_ready;
1413 mod->irq_bempsts = 0;
1414 mod->irq_brdysts = 0;
1417 usbhs_for_each_dfifo(priv, dfifo, i)
1421 void usbhs_fifo_quit(struct usbhs_priv *priv)
1423 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1425 mod->irq_empty = NULL;
1426 mod->irq_ready = NULL;
1427 mod->irq_bempsts = 0;
1428 mod->irq_brdysts = 0;
1431 #define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port) \
1433 fifo = usbhsf_get_dnfifo(priv, channel); \
1434 fifo->name = "D"#channel"FIFO"; \
1435 fifo->port = fifo_port; \
1436 fifo->sel = D##channel##FIFOSEL; \
1437 fifo->ctr = D##channel##FIFOCTR; \
1438 fifo->tx_slave.shdma_slave.slave_id = \
1439 usbhs_get_dparam(priv, d##channel##_tx_id); \
1440 fifo->rx_slave.shdma_slave.slave_id = \
1441 usbhs_get_dparam(priv, d##channel##_rx_id); \
1442 usbhsf_dma_init(priv, fifo, channel); \
1445 #define USBHS_DFIFO_INIT(priv, fifo, channel) \
1446 __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
1447 #define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel) \
1448 __USBHS_DFIFO_INIT(priv, fifo, channel, 0)
1450 int usbhs_fifo_probe(struct usbhs_priv *priv)
1452 struct usbhs_fifo *fifo;
1455 fifo = usbhsf_get_cfifo(priv);
1456 fifo->name = "CFIFO";
1458 fifo->sel = CFIFOSEL;
1459 fifo->ctr = CFIFOCTR;
1462 USBHS_DFIFO_INIT(priv, fifo, 0);
1463 USBHS_DFIFO_INIT(priv, fifo, 1);
1464 USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2);
1465 USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3);
1470 void usbhs_fifo_remove(struct usbhs_priv *priv)
1472 struct usbhs_fifo *fifo;
1475 usbhs_for_each_dfifo(priv, fifo, i)
1476 usbhsf_dma_quit(priv, fifo);