1 // SPDX-License-Identifier: GPL-1.0+
5 * Copyright (C) 2011 Renesas Solutions Corp.
6 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 #include <linux/delay.h>
10 #include <linux/scatterlist.h>
14 #define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
15 #define usbhsf_is_cfifo(p, f) (usbhsf_get_cfifo(p) == f)
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);
171 goto __usbhs_pkt_handler_end;
174 case USBHSF_PKT_PREPARE:
175 func = pkt->handler->prepare;
177 case USBHSF_PKT_TRY_RUN:
178 func = pkt->handler->try_run;
180 case USBHSF_PKT_DMA_DONE:
181 func = pkt->handler->dma_done;
184 dev_err(dev, "unknown pkt handler\n");
185 goto __usbhs_pkt_handler_end;
189 ret = func(pkt, &is_done);
192 __usbhsf_pkt_del(pkt);
194 __usbhs_pkt_handler_end:
195 usbhs_unlock(priv, flags);
196 /******************** spin unlock ******************/
199 pkt->done(priv, pkt);
200 usbhs_pkt_start(pipe);
206 void usbhs_pkt_start(struct usbhs_pipe *pipe)
208 usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
212 * irq enable/disable function
214 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
215 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
216 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
218 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
219 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
220 u16 status = (1 << usbhs_pipe_number(pipe)); \
224 mod->status |= status; \
226 mod->status &= ~status; \
227 usbhs_irq_callback_update(priv, mod); \
230 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
233 * And DCP pipe can NOT use "ready interrupt" for "send"
234 * it should use "empty" interrupt.
236 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
238 * on the other hand, normal pipe can use "ready interrupt" for "send"
239 * even though it is single/double buffer
241 if (usbhs_pipe_is_dcp(pipe))
242 usbhsf_irq_empty_ctrl(pipe, enable);
244 usbhsf_irq_ready_ctrl(pipe, enable);
247 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
249 usbhsf_irq_ready_ctrl(pipe, enable);
255 static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
256 struct usbhs_fifo *fifo)
258 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
260 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
263 static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
264 struct usbhs_fifo *fifo)
266 /* The FIFO port is accessible */
267 if (usbhs_read(priv, fifo->ctr) & FRDY)
273 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
274 struct usbhs_fifo *fifo)
276 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
279 if (!usbhs_pipe_is_dcp(pipe)) {
281 * This driver checks the pipe condition first to avoid -EBUSY
282 * from usbhsf_fifo_barrier() if the pipe is RX direction and
285 if (usbhs_pipe_is_dir_in(pipe))
286 ret = usbhs_pipe_is_accessible(pipe);
288 ret = usbhsf_fifo_barrier(priv, fifo);
292 * if non-DCP pipe, this driver should set BCLR when
293 * usbhsf_fifo_barrier() returns 0.
296 usbhs_write(priv, fifo->ctr, BCLR);
299 static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
300 struct usbhs_fifo *fifo)
302 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
305 static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
306 struct usbhs_fifo *fifo)
308 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
310 usbhs_pipe_select_fifo(pipe, NULL);
311 usbhs_write(priv, fifo->sel, 0);
314 static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
315 struct usbhs_fifo *fifo,
318 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
319 struct device *dev = usbhs_priv_to_dev(priv);
321 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
322 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
324 if (usbhs_pipe_is_busy(pipe) ||
325 usbhsf_fifo_is_busy(fifo))
328 if (usbhs_pipe_is_dcp(pipe)) {
329 base |= (1 == write) << 5; /* ISEL */
331 if (usbhs_mod_is_host(priv))
332 usbhs_dcp_dir_for_host(pipe, write);
335 /* "base" will be used below */
336 if (usbhs_get_dparam(priv, has_sudmac) && !usbhsf_is_cfifo(priv, fifo))
337 usbhs_write(priv, fifo->sel, base);
339 usbhs_write(priv, fifo->sel, base | MBW_32);
341 /* check ISEL and CURPIPE value */
343 if (base == (mask & usbhs_read(priv, fifo->sel))) {
344 usbhs_pipe_select_fifo(pipe, fifo);
350 dev_err(dev, "fifo select error\n");
358 static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
360 struct usbhs_pipe *pipe = pkt->pipe;
361 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
362 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
363 struct device *dev = usbhs_priv_to_dev(priv);
366 usbhs_pipe_disable(pipe);
368 ret = usbhsf_fifo_select(pipe, fifo, 1);
370 dev_err(dev, "%s() faile\n", __func__);
374 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
376 usbhsf_fifo_clear(pipe, fifo);
377 usbhsf_send_terminator(pipe, fifo);
379 usbhsf_fifo_unselect(pipe, fifo);
381 usbhsf_tx_irq_ctrl(pipe, 1);
382 usbhs_pipe_enable(pipe);
387 static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
389 struct usbhs_pipe *pipe = pkt->pipe;
390 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
391 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
392 struct device *dev = usbhs_priv_to_dev(priv);
395 usbhs_pipe_disable(pipe);
397 ret = usbhsf_fifo_select(pipe, fifo, 0);
399 dev_err(dev, "%s() fail\n", __func__);
403 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
404 usbhsf_fifo_clear(pipe, fifo);
406 usbhsf_fifo_unselect(pipe, fifo);
408 usbhsf_rx_irq_ctrl(pipe, 1);
409 usbhs_pipe_enable(pipe);
415 static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
417 struct usbhs_pipe *pipe = pkt->pipe;
419 if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
420 usbhsf_tx_irq_ctrl(pipe, 0);
422 usbhsf_rx_irq_ctrl(pipe, 0);
424 pkt->actual = pkt->length;
430 const struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
431 .prepare = usbhs_dcp_dir_switch_to_write,
432 .try_run = usbhs_dcp_dir_switch_done,
435 const struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
436 .prepare = usbhs_dcp_dir_switch_to_read,
437 .try_run = usbhs_dcp_dir_switch_done,
441 * DCP data stage (push)
443 static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
445 struct usbhs_pipe *pipe = pkt->pipe;
447 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
450 * change handler to PIO push
452 pkt->handler = &usbhs_fifo_pio_push_handler;
454 return pkt->handler->prepare(pkt, is_done);
457 const struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
458 .prepare = usbhsf_dcp_data_stage_try_push,
462 * DCP data stage (pop)
464 static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
467 struct usbhs_pipe *pipe = pkt->pipe;
468 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
469 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
471 if (usbhs_pipe_is_busy(pipe))
475 * prepare pop for DCP should
476 * - change DCP direction,
480 usbhs_pipe_disable(pipe);
482 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
484 usbhsf_fifo_select(pipe, fifo, 0);
485 usbhsf_fifo_clear(pipe, fifo);
486 usbhsf_fifo_unselect(pipe, fifo);
489 * change handler to PIO pop
491 pkt->handler = &usbhs_fifo_pio_pop_handler;
493 return pkt->handler->prepare(pkt, is_done);
496 const struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
497 .prepare = usbhsf_dcp_data_stage_prepare_pop,
503 static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
505 struct usbhs_pipe *pipe = pkt->pipe;
506 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
507 struct device *dev = usbhs_priv_to_dev(priv);
508 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
509 void __iomem *addr = priv->base + fifo->port;
511 int maxp = usbhs_pipe_get_maxpacket(pipe);
516 usbhs_pipe_data_sequence(pipe, pkt->sequence);
517 pkt->sequence = -1; /* -1 sequence will be ignored */
519 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
521 ret = usbhsf_fifo_select(pipe, fifo, 1);
525 ret = usbhs_pipe_is_accessible(pipe);
527 /* inaccessible pipe is not an error */
529 goto usbhs_fifo_write_busy;
532 ret = usbhsf_fifo_barrier(priv, fifo);
534 goto usbhs_fifo_write_busy;
536 buf = pkt->buf + pkt->actual;
537 len = pkt->length - pkt->actual;
538 len = min(len, maxp);
540 is_short = total_len < maxp;
547 if (len >= 4 && !((unsigned long)buf & 0x03)) {
548 iowrite32_rep(addr, buf, len / 4);
550 buf += total_len - len;
553 /* the rest operation */
554 for (i = 0; i < len; i++)
555 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
560 pkt->actual += total_len;
562 if (pkt->actual < pkt->length)
563 *is_done = 0; /* there are remainder data */
565 *is_done = 1; /* short packet */
567 *is_done = !pkt->zero; /* send zero packet ? */
573 usbhsf_send_terminator(pipe, fifo);
575 usbhsf_tx_irq_ctrl(pipe, !*is_done);
576 usbhs_pipe_running(pipe, !*is_done);
577 usbhs_pipe_enable(pipe);
579 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
580 usbhs_pipe_number(pipe),
581 pkt->length, pkt->actual, *is_done, pkt->zero);
583 usbhsf_fifo_unselect(pipe, fifo);
587 usbhs_fifo_write_busy:
588 usbhsf_fifo_unselect(pipe, fifo);
594 usbhsf_tx_irq_ctrl(pipe, 1);
595 usbhs_pipe_running(pipe, 1);
600 static int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done)
602 if (usbhs_pipe_is_running(pkt->pipe))
605 return usbhsf_pio_try_push(pkt, is_done);
608 const struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
609 .prepare = usbhsf_pio_prepare_push,
610 .try_run = usbhsf_pio_try_push,
616 static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
618 struct usbhs_pipe *pipe = pkt->pipe;
619 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
620 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
622 if (usbhs_pipe_is_busy(pipe))
625 if (usbhs_pipe_is_running(pipe))
629 * pipe enable to prepare packet receive
631 usbhs_pipe_data_sequence(pipe, pkt->sequence);
632 pkt->sequence = -1; /* -1 sequence will be ignored */
634 if (usbhs_pipe_is_dcp(pipe))
635 usbhsf_fifo_clear(pipe, fifo);
637 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
638 usbhs_pipe_enable(pipe);
639 usbhs_pipe_running(pipe, 1);
640 usbhsf_rx_irq_ctrl(pipe, 1);
645 static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
647 struct usbhs_pipe *pipe = pkt->pipe;
648 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
649 struct device *dev = usbhs_priv_to_dev(priv);
650 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
651 void __iomem *addr = priv->base + fifo->port;
654 int maxp = usbhs_pipe_get_maxpacket(pipe);
659 ret = usbhsf_fifo_select(pipe, fifo, 0);
663 ret = usbhsf_fifo_barrier(priv, fifo);
665 goto usbhs_fifo_read_busy;
667 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
669 buf = pkt->buf + pkt->actual;
670 len = pkt->length - pkt->actual;
671 len = min(len, rcv_len);
675 * update actual length first here to decide disable pipe.
676 * if this pipe keeps BUF status and all data were popped,
677 * then, next interrupt/token will be issued again
679 pkt->actual += total_len;
681 if ((pkt->actual == pkt->length) || /* receive all data */
682 (total_len < maxp)) { /* short packet */
684 usbhsf_rx_irq_ctrl(pipe, 0);
685 usbhs_pipe_running(pipe, 0);
687 * If function mode, since this controller is possible to enter
688 * Control Write status stage at this timing, this driver
689 * should not disable the pipe. If such a case happens, this
690 * controller is not able to complete the status stage.
692 if (!usbhs_mod_is_host(priv) && !usbhs_pipe_is_dcp(pipe))
693 usbhs_pipe_disable(pipe); /* disable pipe first */
697 * Buffer clear if Zero-Length packet
700 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
704 usbhsf_fifo_clear(pipe, fifo);
705 goto usbhs_fifo_read_end;
713 if (len >= 4 && !((unsigned long)buf & 0x03)) {
714 ioread32_rep(addr, buf, len / 4);
716 buf += total_len - len;
719 /* the rest operation */
720 for (i = 0; i < len; i++) {
722 data = ioread32(addr);
724 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
728 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
729 usbhs_pipe_number(pipe),
730 pkt->length, pkt->actual, *is_done, pkt->zero);
732 usbhs_fifo_read_busy:
733 usbhsf_fifo_unselect(pipe, fifo);
738 const struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
739 .prepare = usbhsf_prepare_pop,
740 .try_run = usbhsf_pio_try_pop,
744 * DCP ctrol statge handler
746 static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
748 usbhs_dcp_control_transfer_done(pkt->pipe);
755 const struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
756 .prepare = usbhsf_ctrl_stage_end,
757 .try_run = usbhsf_ctrl_stage_end,
763 static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
764 struct usbhs_pkt *pkt)
766 if (&usbhs_fifo_dma_push_handler == pkt->handler)
767 return fifo->tx_chan;
769 if (&usbhs_fifo_dma_pop_handler == pkt->handler)
770 return fifo->rx_chan;
775 static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
776 struct usbhs_pkt *pkt)
778 struct usbhs_fifo *fifo;
781 usbhs_for_each_dfifo(priv, fifo, i) {
782 if (usbhsf_dma_chan_get(fifo, pkt) &&
783 !usbhsf_fifo_is_busy(fifo))
790 #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
791 #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
792 static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
793 struct usbhs_fifo *fifo,
796 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
798 usbhs_bset(priv, fifo->sel, DREQE, dreqe);
801 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
803 struct usbhs_pipe *pipe = pkt->pipe;
804 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
805 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
806 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
807 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
809 return info->dma_map_ctrl(chan->device->dev, pkt, map);
812 static void usbhsf_dma_complete(void *arg);
813 static void usbhsf_dma_xfer_preparing(struct usbhs_pkt *pkt)
815 struct usbhs_pipe *pipe = pkt->pipe;
816 struct usbhs_fifo *fifo;
817 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
818 struct dma_async_tx_descriptor *desc;
819 struct dma_chan *chan;
820 struct device *dev = usbhs_priv_to_dev(priv);
821 enum dma_transfer_direction dir;
823 fifo = usbhs_pipe_to_fifo(pipe);
827 chan = usbhsf_dma_chan_get(fifo, pkt);
828 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
830 desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
832 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
836 desc->callback = usbhsf_dma_complete;
837 desc->callback_param = pipe;
839 pkt->cookie = dmaengine_submit(desc);
840 if (pkt->cookie < 0) {
841 dev_err(dev, "Failed to submit dma descriptor\n");
845 dev_dbg(dev, " %s %d (%d/ %d)\n",
846 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
848 usbhs_pipe_running(pipe, 1);
849 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
850 dma_async_issue_pending(chan);
851 usbhsf_dma_start(pipe, fifo);
852 usbhs_pipe_enable(pipe);
855 static void xfer_work(struct work_struct *work)
857 struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
858 struct usbhs_pipe *pipe = pkt->pipe;
859 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
862 usbhs_lock(priv, flags);
863 usbhsf_dma_xfer_preparing(pkt);
864 usbhs_unlock(priv, flags);
870 static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
872 struct usbhs_pipe *pipe = pkt->pipe;
873 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
874 struct usbhs_fifo *fifo;
875 int len = pkt->length - pkt->actual;
877 uintptr_t align_mask;
879 if (usbhs_pipe_is_busy(pipe))
882 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
883 if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
884 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
885 goto usbhsf_pio_prepare_push;
887 /* check data length if this driver don't use USB-DMAC */
888 if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7)
889 goto usbhsf_pio_prepare_push;
891 /* check buffer alignment */
892 align_mask = usbhs_get_dparam(priv, has_usb_dmac) ?
893 USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7;
894 if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask)
895 goto usbhsf_pio_prepare_push;
897 /* return at this time if the pipe is running */
898 if (usbhs_pipe_is_running(pipe))
901 /* get enable DMA fifo */
902 fifo = usbhsf_get_dma_fifo(priv, pkt);
904 goto usbhsf_pio_prepare_push;
906 ret = usbhsf_fifo_select(pipe, fifo, 0);
908 goto usbhsf_pio_prepare_push;
910 if (usbhsf_dma_map(pkt) < 0)
911 goto usbhsf_pio_prepare_push_unselect;
915 usbhsf_tx_irq_ctrl(pipe, 0);
916 /* FIXME: Workaound for usb dmac that driver can be used in atomic */
917 if (usbhs_get_dparam(priv, has_usb_dmac)) {
918 usbhsf_dma_xfer_preparing(pkt);
920 INIT_WORK(&pkt->work, xfer_work);
921 schedule_work(&pkt->work);
926 usbhsf_pio_prepare_push_unselect:
927 usbhsf_fifo_unselect(pipe, fifo);
928 usbhsf_pio_prepare_push:
930 * change handler to PIO
932 pkt->handler = &usbhs_fifo_pio_push_handler;
934 return pkt->handler->prepare(pkt, is_done);
937 static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
939 struct usbhs_pipe *pipe = pkt->pipe;
940 int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
942 pkt->actual += pkt->trans;
944 if (pkt->actual < pkt->length)
945 *is_done = 0; /* there are remainder data */
947 *is_done = 1; /* short packet */
949 *is_done = !pkt->zero; /* send zero packet? */
951 usbhs_pipe_running(pipe, !*is_done);
953 usbhsf_dma_stop(pipe, pipe->fifo);
954 usbhsf_dma_unmap(pkt);
955 usbhsf_fifo_unselect(pipe, pipe->fifo);
958 /* change handler to PIO */
959 pkt->handler = &usbhs_fifo_pio_push_handler;
960 return pkt->handler->try_run(pkt, is_done);
966 const struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
967 .prepare = usbhsf_dma_prepare_push,
968 .dma_done = usbhsf_dma_push_done,
975 static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
978 return usbhsf_prepare_pop(pkt, is_done);
981 static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt,
984 struct usbhs_pipe *pipe = pkt->pipe;
985 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
986 struct usbhs_fifo *fifo;
989 if (usbhs_pipe_is_busy(pipe))
992 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
993 if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) ||
994 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
995 goto usbhsf_pio_prepare_pop;
997 fifo = usbhsf_get_dma_fifo(priv, pkt);
999 goto usbhsf_pio_prepare_pop;
1001 if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1))
1002 goto usbhsf_pio_prepare_pop;
1004 /* return at this time if the pipe is running */
1005 if (usbhs_pipe_is_running(pipe))
1008 usbhs_pipe_config_change_bfre(pipe, 1);
1010 ret = usbhsf_fifo_select(pipe, fifo, 0);
1012 goto usbhsf_pio_prepare_pop;
1014 if (usbhsf_dma_map(pkt) < 0)
1015 goto usbhsf_pio_prepare_pop_unselect;
1020 * usbhs_fifo_dma_pop_handler :: prepare
1021 * enabled irq to come here.
1022 * but it is no longer needed for DMA. disable it.
1024 usbhsf_rx_irq_ctrl(pipe, 0);
1026 pkt->trans = pkt->length;
1028 usbhsf_dma_xfer_preparing(pkt);
1032 usbhsf_pio_prepare_pop_unselect:
1033 usbhsf_fifo_unselect(pipe, fifo);
1034 usbhsf_pio_prepare_pop:
1037 * change handler to PIO
1039 pkt->handler = &usbhs_fifo_pio_pop_handler;
1040 usbhs_pipe_config_change_bfre(pipe, 0);
1042 return pkt->handler->prepare(pkt, is_done);
1045 static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
1047 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1049 if (usbhs_get_dparam(priv, has_usb_dmac))
1050 return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done);
1052 return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
1055 static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
1057 struct usbhs_pipe *pipe = pkt->pipe;
1058 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1059 struct usbhs_fifo *fifo;
1062 if (usbhs_pipe_is_busy(pipe))
1065 if (usbhs_pipe_is_dcp(pipe))
1066 goto usbhsf_pio_prepare_pop;
1068 /* get enable DMA fifo */
1069 fifo = usbhsf_get_dma_fifo(priv, pkt);
1071 goto usbhsf_pio_prepare_pop;
1073 if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
1074 goto usbhsf_pio_prepare_pop;
1076 ret = usbhsf_fifo_select(pipe, fifo, 0);
1078 goto usbhsf_pio_prepare_pop;
1080 /* use PIO if packet is less than pio_dma_border */
1081 len = usbhsf_fifo_rcv_len(priv, fifo);
1082 len = min(pkt->length - pkt->actual, len);
1083 if (len & 0x7) /* 8byte alignment */
1084 goto usbhsf_pio_prepare_pop_unselect;
1086 if (len < usbhs_get_dparam(priv, pio_dma_border))
1087 goto usbhsf_pio_prepare_pop_unselect;
1089 ret = usbhsf_fifo_barrier(priv, fifo);
1091 goto usbhsf_pio_prepare_pop_unselect;
1093 if (usbhsf_dma_map(pkt) < 0)
1094 goto usbhsf_pio_prepare_pop_unselect;
1099 * usbhs_fifo_dma_pop_handler :: prepare
1100 * enabled irq to come here.
1101 * but it is no longer needed for DMA. disable it.
1103 usbhsf_rx_irq_ctrl(pipe, 0);
1107 INIT_WORK(&pkt->work, xfer_work);
1108 schedule_work(&pkt->work);
1112 usbhsf_pio_prepare_pop_unselect:
1113 usbhsf_fifo_unselect(pipe, fifo);
1114 usbhsf_pio_prepare_pop:
1117 * change handler to PIO
1119 pkt->handler = &usbhs_fifo_pio_pop_handler;
1121 return pkt->handler->try_run(pkt, is_done);
1124 static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
1126 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1128 BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
1130 return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
1133 static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
1135 struct usbhs_pipe *pipe = pkt->pipe;
1136 int maxp = usbhs_pipe_get_maxpacket(pipe);
1138 usbhsf_dma_stop(pipe, pipe->fifo);
1139 usbhsf_dma_unmap(pkt);
1140 usbhsf_fifo_unselect(pipe, pipe->fifo);
1142 pkt->actual += pkt->trans;
1144 if ((pkt->actual == pkt->length) || /* receive all data */
1145 (pkt->trans < maxp)) { /* short packet */
1147 usbhs_pipe_running(pipe, 0);
1150 usbhs_pipe_running(pipe, 0);
1151 usbhsf_prepare_pop(pkt, is_done);
1157 static size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt,
1158 struct dma_chan *chan, int dtln)
1160 struct usbhs_pipe *pipe = pkt->pipe;
1161 struct dma_tx_state state;
1162 size_t received_size;
1163 int maxp = usbhs_pipe_get_maxpacket(pipe);
1165 dmaengine_tx_status(chan, pkt->cookie, &state);
1166 received_size = pkt->length - state.residue;
1169 received_size -= USBHS_USB_DMAC_XFER_SIZE;
1170 received_size &= ~(maxp - 1);
1171 received_size += dtln;
1174 return received_size;
1177 static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt,
1180 struct usbhs_pipe *pipe = pkt->pipe;
1181 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1182 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
1183 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
1187 * Since the driver disables rx_irq in DMA mode, the interrupt handler
1188 * cannot the BRDYSTS. So, the function clears it here because the
1189 * driver may use PIO mode next time.
1191 usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe));
1193 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
1194 usbhsf_fifo_clear(pipe, fifo);
1195 pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len);
1197 usbhs_pipe_running(pipe, 0);
1198 usbhsf_dma_stop(pipe, fifo);
1199 usbhsf_dma_unmap(pkt);
1200 usbhsf_fifo_unselect(pipe, pipe->fifo);
1202 /* The driver can assume the rx transaction is always "done" */
1208 static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
1210 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1212 if (usbhs_get_dparam(priv, has_usb_dmac))
1213 return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done);
1215 return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
1218 const struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
1219 .prepare = usbhsf_dma_prepare_pop,
1220 .try_run = usbhsf_dma_try_pop,
1221 .dma_done = usbhsf_dma_pop_done
1227 static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
1229 struct sh_dmae_slave *slave = param;
1234 * usbhs doesn't recognize id = 0 as valid DMA
1236 if (0 == slave->shdma_slave.slave_id)
1239 chan->private = slave;
1244 static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
1247 dma_release_channel(fifo->tx_chan);
1249 dma_release_channel(fifo->rx_chan);
1251 fifo->tx_chan = NULL;
1252 fifo->rx_chan = NULL;
1255 static void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo)
1257 dma_cap_mask_t mask;
1260 dma_cap_set(DMA_SLAVE, mask);
1261 fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1265 dma_cap_set(DMA_SLAVE, mask);
1266 fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1270 static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo,
1276 * To avoid complex handing for DnFIFOs, the driver uses each
1277 * DnFIFO as TX or RX direction (not bi-direction).
1278 * So, the driver uses odd channels for TX, even channels for RX.
1280 snprintf(name, sizeof(name), "ch%d", channel);
1282 fifo->tx_chan = dma_request_slave_channel_reason(dev, name);
1283 if (IS_ERR(fifo->tx_chan))
1284 fifo->tx_chan = NULL;
1286 fifo->rx_chan = dma_request_slave_channel_reason(dev, name);
1287 if (IS_ERR(fifo->rx_chan))
1288 fifo->rx_chan = NULL;
1292 static void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo,
1295 struct device *dev = usbhs_priv_to_dev(priv);
1298 usbhsf_dma_init_dt(dev, fifo, channel);
1300 usbhsf_dma_init_pdev(fifo);
1302 if (fifo->tx_chan || fifo->rx_chan)
1303 dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
1305 fifo->tx_chan ? "[TX]" : " ",
1306 fifo->rx_chan ? "[RX]" : " ");
1312 static int usbhsf_irq_empty(struct usbhs_priv *priv,
1313 struct usbhs_irq_state *irq_state)
1315 struct usbhs_pipe *pipe;
1316 struct device *dev = usbhs_priv_to_dev(priv);
1319 if (!irq_state->bempsts) {
1320 dev_err(dev, "debug %s !!\n", __func__);
1324 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
1327 * search interrupted "pipe"
1330 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1331 if (!(irq_state->bempsts & (1 << i)))
1334 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1336 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
1342 static int usbhsf_irq_ready(struct usbhs_priv *priv,
1343 struct usbhs_irq_state *irq_state)
1345 struct usbhs_pipe *pipe;
1346 struct device *dev = usbhs_priv_to_dev(priv);
1349 if (!irq_state->brdysts) {
1350 dev_err(dev, "debug %s !!\n", __func__);
1354 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
1357 * search interrupted "pipe"
1360 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1361 if (!(irq_state->brdysts & (1 << i)))
1364 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1366 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
1372 static void usbhsf_dma_complete(void *arg)
1374 struct usbhs_pipe *pipe = arg;
1375 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1376 struct device *dev = usbhs_priv_to_dev(priv);
1379 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
1381 dev_err(dev, "dma_complete run_error %d : %d\n",
1382 usbhs_pipe_number(pipe), ret);
1385 void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
1387 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1388 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
1390 /* clear DCP FIFO of transmission */
1391 if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
1393 usbhsf_fifo_clear(pipe, fifo);
1394 usbhsf_fifo_unselect(pipe, fifo);
1396 /* clear DCP FIFO of reception */
1397 if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
1399 usbhsf_fifo_clear(pipe, fifo);
1400 usbhsf_fifo_unselect(pipe, fifo);
1406 void usbhs_fifo_init(struct usbhs_priv *priv)
1408 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1409 struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
1410 struct usbhs_fifo *dfifo;
1413 mod->irq_empty = usbhsf_irq_empty;
1414 mod->irq_ready = usbhsf_irq_ready;
1415 mod->irq_bempsts = 0;
1416 mod->irq_brdysts = 0;
1419 usbhs_for_each_dfifo(priv, dfifo, i)
1423 void usbhs_fifo_quit(struct usbhs_priv *priv)
1425 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1427 mod->irq_empty = NULL;
1428 mod->irq_ready = NULL;
1429 mod->irq_bempsts = 0;
1430 mod->irq_brdysts = 0;
1433 #define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port) \
1435 fifo = usbhsf_get_dnfifo(priv, channel); \
1436 fifo->name = "D"#channel"FIFO"; \
1437 fifo->port = fifo_port; \
1438 fifo->sel = D##channel##FIFOSEL; \
1439 fifo->ctr = D##channel##FIFOCTR; \
1440 fifo->tx_slave.shdma_slave.slave_id = \
1441 usbhs_get_dparam(priv, d##channel##_tx_id); \
1442 fifo->rx_slave.shdma_slave.slave_id = \
1443 usbhs_get_dparam(priv, d##channel##_rx_id); \
1444 usbhsf_dma_init(priv, fifo, channel); \
1447 #define USBHS_DFIFO_INIT(priv, fifo, channel) \
1448 __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
1449 #define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel) \
1450 __USBHS_DFIFO_INIT(priv, fifo, channel, 0)
1452 int usbhs_fifo_probe(struct usbhs_priv *priv)
1454 struct usbhs_fifo *fifo;
1457 fifo = usbhsf_get_cfifo(priv);
1458 fifo->name = "CFIFO";
1460 fifo->sel = CFIFOSEL;
1461 fifo->ctr = CFIFOCTR;
1464 USBHS_DFIFO_INIT(priv, fifo, 0);
1465 USBHS_DFIFO_INIT(priv, fifo, 1);
1466 USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2);
1467 USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3);
1472 void usbhs_fifo_remove(struct usbhs_priv *priv)
1474 struct usbhs_fifo *fifo;
1477 usbhs_for_each_dfifo(priv, fifo, i)
1478 usbhsf_dma_quit(priv, fifo);