4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 #include <linux/delay.h>
19 #include <linux/scatterlist.h>
23 #define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
24 #define usbhsf_is_cfifo(p, f) (usbhsf_get_cfifo(p) == f)
26 #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
31 void usbhs_pkt_init(struct usbhs_pkt *pkt)
33 INIT_LIST_HEAD(&pkt->node);
37 * packet control function
39 static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
41 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
42 struct device *dev = usbhs_priv_to_dev(priv);
44 dev_err(dev, "null handler\n");
49 static const struct usbhs_pkt_handle usbhsf_null_handler = {
50 .prepare = usbhsf_null_handle,
51 .try_run = usbhsf_null_handle,
54 void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
55 void (*done)(struct usbhs_priv *priv,
56 struct usbhs_pkt *pkt),
57 void *buf, int len, int zero, int sequence)
59 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
60 struct device *dev = usbhs_priv_to_dev(priv);
64 dev_err(dev, "no done function\n");
68 /******************** spin lock ********************/
69 usbhs_lock(priv, flags);
72 dev_err(dev, "no handler function\n");
73 pipe->handler = &usbhsf_null_handler;
76 list_move_tail(&pkt->node, &pipe->list);
79 * each pkt must hold own handler.
80 * because handler might be changed by its situation.
81 * dma handler -> pio handler.
85 pkt->handler = pipe->handler;
90 pkt->sequence = sequence;
92 usbhs_unlock(priv, flags);
93 /******************** spin unlock ******************/
96 static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
98 list_del_init(&pkt->node);
101 struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
103 return list_first_entry_or_null(&pipe->list, struct usbhs_pkt, node);
106 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
107 struct usbhs_fifo *fifo);
108 static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
109 struct usbhs_fifo *fifo);
110 static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
111 struct usbhs_pkt *pkt);
112 #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
113 #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
114 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map);
115 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable);
116 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable);
117 struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
119 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
120 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
123 /******************** spin lock ********************/
124 usbhs_lock(priv, flags);
126 usbhs_pipe_disable(pipe);
129 pkt = __usbhsf_pkt_get(pipe);
132 struct dma_chan *chan = NULL;
135 chan = usbhsf_dma_chan_get(fifo, pkt);
137 dmaengine_terminate_all(chan);
138 usbhsf_fifo_clear(pipe, fifo);
139 usbhsf_dma_unmap(pkt);
141 if (usbhs_pipe_is_dir_in(pipe))
142 usbhsf_rx_irq_ctrl(pipe, 0);
144 usbhsf_tx_irq_ctrl(pipe, 0);
147 usbhs_pipe_running(pipe, 0);
149 __usbhsf_pkt_del(pkt);
153 usbhsf_fifo_unselect(pipe, fifo);
155 usbhs_unlock(priv, flags);
156 /******************** spin unlock ******************/
167 static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
169 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
170 struct usbhs_pkt *pkt;
171 struct device *dev = usbhs_priv_to_dev(priv);
172 int (*func)(struct usbhs_pkt *pkt, int *is_done);
177 /******************** spin lock ********************/
178 usbhs_lock(priv, flags);
180 pkt = __usbhsf_pkt_get(pipe);
182 goto __usbhs_pkt_handler_end;
185 case USBHSF_PKT_PREPARE:
186 func = pkt->handler->prepare;
188 case USBHSF_PKT_TRY_RUN:
189 func = pkt->handler->try_run;
191 case USBHSF_PKT_DMA_DONE:
192 func = pkt->handler->dma_done;
195 dev_err(dev, "unknown pkt handler\n");
196 goto __usbhs_pkt_handler_end;
200 ret = func(pkt, &is_done);
203 __usbhsf_pkt_del(pkt);
205 __usbhs_pkt_handler_end:
206 usbhs_unlock(priv, flags);
207 /******************** spin unlock ******************/
210 pkt->done(priv, pkt);
211 usbhs_pkt_start(pipe);
217 void usbhs_pkt_start(struct usbhs_pipe *pipe)
219 usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
223 * irq enable/disable function
225 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
226 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
227 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
229 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
230 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
231 u16 status = (1 << usbhs_pipe_number(pipe)); \
235 mod->status |= status; \
237 mod->status &= ~status; \
238 usbhs_irq_callback_update(priv, mod); \
241 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
244 * And DCP pipe can NOT use "ready interrupt" for "send"
245 * it should use "empty" interrupt.
247 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
249 * on the other hand, normal pipe can use "ready interrupt" for "send"
250 * even though it is single/double buffer
252 if (usbhs_pipe_is_dcp(pipe))
253 usbhsf_irq_empty_ctrl(pipe, enable);
255 usbhsf_irq_ready_ctrl(pipe, enable);
258 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
260 usbhsf_irq_ready_ctrl(pipe, enable);
266 static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
267 struct usbhs_fifo *fifo)
269 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
271 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
274 static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
275 struct usbhs_fifo *fifo)
280 /* The FIFO port is accessible */
281 if (usbhs_read(priv, fifo->ctr) & FRDY)
290 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
291 struct usbhs_fifo *fifo)
293 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
296 if (!usbhs_pipe_is_dcp(pipe)) {
298 * This driver checks the pipe condition first to avoid -EBUSY
299 * from usbhsf_fifo_barrier() with about 10 msec delay in
300 * the interrupt handler if the pipe is RX direction and empty.
302 if (usbhs_pipe_is_dir_in(pipe))
303 ret = usbhs_pipe_is_accessible(pipe);
305 ret = usbhsf_fifo_barrier(priv, fifo);
309 * if non-DCP pipe, this driver should set BCLR when
310 * usbhsf_fifo_barrier() returns 0.
313 usbhs_write(priv, fifo->ctr, BCLR);
316 static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
317 struct usbhs_fifo *fifo)
319 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
322 static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
323 struct usbhs_fifo *fifo)
325 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
327 usbhs_pipe_select_fifo(pipe, NULL);
328 usbhs_write(priv, fifo->sel, 0);
331 static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
332 struct usbhs_fifo *fifo,
335 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
336 struct device *dev = usbhs_priv_to_dev(priv);
338 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
339 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
341 if (usbhs_pipe_is_busy(pipe) ||
342 usbhsf_fifo_is_busy(fifo))
345 if (usbhs_pipe_is_dcp(pipe)) {
346 base |= (1 == write) << 5; /* ISEL */
348 if (usbhs_mod_is_host(priv))
349 usbhs_dcp_dir_for_host(pipe, write);
352 /* "base" will be used below */
353 if (usbhs_get_dparam(priv, has_sudmac) && !usbhsf_is_cfifo(priv, fifo))
354 usbhs_write(priv, fifo->sel, base);
356 usbhs_write(priv, fifo->sel, base | MBW_32);
358 /* check ISEL and CURPIPE value */
360 if (base == (mask & usbhs_read(priv, fifo->sel))) {
361 usbhs_pipe_select_fifo(pipe, fifo);
367 dev_err(dev, "fifo select error\n");
375 static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
377 struct usbhs_pipe *pipe = pkt->pipe;
378 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
379 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
380 struct device *dev = usbhs_priv_to_dev(priv);
383 usbhs_pipe_disable(pipe);
385 ret = usbhsf_fifo_select(pipe, fifo, 1);
387 dev_err(dev, "%s() faile\n", __func__);
391 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
393 usbhsf_fifo_clear(pipe, fifo);
394 usbhsf_send_terminator(pipe, fifo);
396 usbhsf_fifo_unselect(pipe, fifo);
398 usbhsf_tx_irq_ctrl(pipe, 1);
399 usbhs_pipe_enable(pipe);
404 static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
406 struct usbhs_pipe *pipe = pkt->pipe;
407 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
408 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
409 struct device *dev = usbhs_priv_to_dev(priv);
412 usbhs_pipe_disable(pipe);
414 ret = usbhsf_fifo_select(pipe, fifo, 0);
416 dev_err(dev, "%s() fail\n", __func__);
420 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
421 usbhsf_fifo_clear(pipe, fifo);
423 usbhsf_fifo_unselect(pipe, fifo);
425 usbhsf_rx_irq_ctrl(pipe, 1);
426 usbhs_pipe_enable(pipe);
432 static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
434 struct usbhs_pipe *pipe = pkt->pipe;
436 if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
437 usbhsf_tx_irq_ctrl(pipe, 0);
439 usbhsf_rx_irq_ctrl(pipe, 0);
441 pkt->actual = pkt->length;
447 const struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
448 .prepare = usbhs_dcp_dir_switch_to_write,
449 .try_run = usbhs_dcp_dir_switch_done,
452 const struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
453 .prepare = usbhs_dcp_dir_switch_to_read,
454 .try_run = usbhs_dcp_dir_switch_done,
458 * DCP data stage (push)
460 static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
462 struct usbhs_pipe *pipe = pkt->pipe;
464 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
467 * change handler to PIO push
469 pkt->handler = &usbhs_fifo_pio_push_handler;
471 return pkt->handler->prepare(pkt, is_done);
474 const struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
475 .prepare = usbhsf_dcp_data_stage_try_push,
479 * DCP data stage (pop)
481 static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
484 struct usbhs_pipe *pipe = pkt->pipe;
485 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
486 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
488 if (usbhs_pipe_is_busy(pipe))
492 * prepare pop for DCP should
493 * - change DCP direction,
497 usbhs_pipe_disable(pipe);
499 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
501 usbhsf_fifo_select(pipe, fifo, 0);
502 usbhsf_fifo_clear(pipe, fifo);
503 usbhsf_fifo_unselect(pipe, fifo);
506 * change handler to PIO pop
508 pkt->handler = &usbhs_fifo_pio_pop_handler;
510 return pkt->handler->prepare(pkt, is_done);
513 const struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
514 .prepare = usbhsf_dcp_data_stage_prepare_pop,
520 static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
522 struct usbhs_pipe *pipe = pkt->pipe;
523 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
524 struct device *dev = usbhs_priv_to_dev(priv);
525 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
526 void __iomem *addr = priv->base + fifo->port;
528 int maxp = usbhs_pipe_get_maxpacket(pipe);
533 usbhs_pipe_data_sequence(pipe, pkt->sequence);
534 pkt->sequence = -1; /* -1 sequence will be ignored */
536 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
538 ret = usbhsf_fifo_select(pipe, fifo, 1);
542 ret = usbhs_pipe_is_accessible(pipe);
544 /* inaccessible pipe is not an error */
546 goto usbhs_fifo_write_busy;
549 ret = usbhsf_fifo_barrier(priv, fifo);
551 goto usbhs_fifo_write_busy;
553 buf = pkt->buf + pkt->actual;
554 len = pkt->length - pkt->actual;
555 len = min(len, maxp);
557 is_short = total_len < maxp;
564 if (len >= 4 && !((unsigned long)buf & 0x03)) {
565 iowrite32_rep(addr, buf, len / 4);
567 buf += total_len - len;
570 /* the rest operation */
571 for (i = 0; i < len; i++)
572 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
577 pkt->actual += total_len;
579 if (pkt->actual < pkt->length)
580 *is_done = 0; /* there are remainder data */
582 *is_done = 1; /* short packet */
584 *is_done = !pkt->zero; /* send zero packet ? */
590 usbhsf_send_terminator(pipe, fifo);
592 usbhsf_tx_irq_ctrl(pipe, !*is_done);
593 usbhs_pipe_running(pipe, !*is_done);
594 usbhs_pipe_enable(pipe);
596 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
597 usbhs_pipe_number(pipe),
598 pkt->length, pkt->actual, *is_done, pkt->zero);
600 usbhsf_fifo_unselect(pipe, fifo);
604 usbhs_fifo_write_busy:
605 usbhsf_fifo_unselect(pipe, fifo);
611 usbhsf_tx_irq_ctrl(pipe, 1);
612 usbhs_pipe_running(pipe, 1);
617 static int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done)
619 if (usbhs_pipe_is_running(pkt->pipe))
622 return usbhsf_pio_try_push(pkt, is_done);
625 const struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
626 .prepare = usbhsf_pio_prepare_push,
627 .try_run = usbhsf_pio_try_push,
633 static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
635 struct usbhs_pipe *pipe = pkt->pipe;
636 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
637 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
639 if (usbhs_pipe_is_busy(pipe))
642 if (usbhs_pipe_is_running(pipe))
646 * pipe enable to prepare packet receive
648 usbhs_pipe_data_sequence(pipe, pkt->sequence);
649 pkt->sequence = -1; /* -1 sequence will be ignored */
651 if (usbhs_pipe_is_dcp(pipe))
652 usbhsf_fifo_clear(pipe, fifo);
654 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
655 usbhs_pipe_enable(pipe);
656 usbhs_pipe_running(pipe, 1);
657 usbhsf_rx_irq_ctrl(pipe, 1);
662 static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
664 struct usbhs_pipe *pipe = pkt->pipe;
665 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
666 struct device *dev = usbhs_priv_to_dev(priv);
667 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
668 void __iomem *addr = priv->base + fifo->port;
671 int maxp = usbhs_pipe_get_maxpacket(pipe);
676 ret = usbhsf_fifo_select(pipe, fifo, 0);
680 ret = usbhsf_fifo_barrier(priv, fifo);
682 goto usbhs_fifo_read_busy;
684 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
686 buf = pkt->buf + pkt->actual;
687 len = pkt->length - pkt->actual;
688 len = min(len, rcv_len);
692 * update actual length first here to decide disable pipe.
693 * if this pipe keeps BUF status and all data were popped,
694 * then, next interrupt/token will be issued again
696 pkt->actual += total_len;
698 if ((pkt->actual == pkt->length) || /* receive all data */
699 (total_len < maxp)) { /* short packet */
701 usbhsf_rx_irq_ctrl(pipe, 0);
702 usbhs_pipe_running(pipe, 0);
704 * If function mode, since this controller is possible to enter
705 * Control Write status stage at this timing, this driver
706 * should not disable the pipe. If such a case happens, this
707 * controller is not able to complete the status stage.
709 if (!usbhs_mod_is_host(priv) && !usbhs_pipe_is_dcp(pipe))
710 usbhs_pipe_disable(pipe); /* disable pipe first */
714 * Buffer clear if Zero-Length packet
717 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
721 usbhsf_fifo_clear(pipe, fifo);
722 goto usbhs_fifo_read_end;
730 if (len >= 4 && !((unsigned long)buf & 0x03)) {
731 ioread32_rep(addr, buf, len / 4);
733 buf += total_len - len;
736 /* the rest operation */
737 for (i = 0; i < len; i++) {
739 data = ioread32(addr);
741 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
745 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
746 usbhs_pipe_number(pipe),
747 pkt->length, pkt->actual, *is_done, pkt->zero);
749 usbhs_fifo_read_busy:
750 usbhsf_fifo_unselect(pipe, fifo);
755 const struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
756 .prepare = usbhsf_prepare_pop,
757 .try_run = usbhsf_pio_try_pop,
761 * DCP ctrol statge handler
763 static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
765 usbhs_dcp_control_transfer_done(pkt->pipe);
772 const struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
773 .prepare = usbhsf_ctrl_stage_end,
774 .try_run = usbhsf_ctrl_stage_end,
780 static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
781 struct usbhs_pkt *pkt)
783 if (&usbhs_fifo_dma_push_handler == pkt->handler)
784 return fifo->tx_chan;
786 if (&usbhs_fifo_dma_pop_handler == pkt->handler)
787 return fifo->rx_chan;
792 static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
793 struct usbhs_pkt *pkt)
795 struct usbhs_fifo *fifo;
798 usbhs_for_each_dfifo(priv, fifo, i) {
799 if (usbhsf_dma_chan_get(fifo, pkt) &&
800 !usbhsf_fifo_is_busy(fifo))
807 #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
808 #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
809 static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
810 struct usbhs_fifo *fifo,
813 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
815 usbhs_bset(priv, fifo->sel, DREQE, dreqe);
818 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
820 struct usbhs_pipe *pipe = pkt->pipe;
821 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
822 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
823 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
824 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
826 return info->dma_map_ctrl(chan->device->dev, pkt, map);
829 static void usbhsf_dma_complete(void *arg);
830 static void usbhsf_dma_xfer_preparing(struct usbhs_pkt *pkt)
832 struct usbhs_pipe *pipe = pkt->pipe;
833 struct usbhs_fifo *fifo;
834 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
835 struct dma_async_tx_descriptor *desc;
836 struct dma_chan *chan;
837 struct device *dev = usbhs_priv_to_dev(priv);
838 enum dma_transfer_direction dir;
840 fifo = usbhs_pipe_to_fifo(pipe);
844 chan = usbhsf_dma_chan_get(fifo, pkt);
845 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
847 desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
849 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
853 desc->callback = usbhsf_dma_complete;
854 desc->callback_param = pipe;
856 pkt->cookie = dmaengine_submit(desc);
857 if (pkt->cookie < 0) {
858 dev_err(dev, "Failed to submit dma descriptor\n");
862 dev_dbg(dev, " %s %d (%d/ %d)\n",
863 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
865 usbhs_pipe_running(pipe, 1);
866 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
867 dma_async_issue_pending(chan);
868 usbhsf_dma_start(pipe, fifo);
869 usbhs_pipe_enable(pipe);
872 static void xfer_work(struct work_struct *work)
874 struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
875 struct usbhs_pipe *pipe = pkt->pipe;
876 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
879 usbhs_lock(priv, flags);
880 usbhsf_dma_xfer_preparing(pkt);
881 usbhs_unlock(priv, flags);
887 static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
889 struct usbhs_pipe *pipe = pkt->pipe;
890 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
891 struct usbhs_fifo *fifo;
892 int len = pkt->length - pkt->actual;
894 uintptr_t align_mask;
896 if (usbhs_pipe_is_busy(pipe))
899 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
900 if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
901 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
902 goto usbhsf_pio_prepare_push;
904 /* check data length if this driver don't use USB-DMAC */
905 if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7)
906 goto usbhsf_pio_prepare_push;
908 /* check buffer alignment */
909 align_mask = usbhs_get_dparam(priv, has_usb_dmac) ?
910 USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7;
911 if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask)
912 goto usbhsf_pio_prepare_push;
914 /* return at this time if the pipe is running */
915 if (usbhs_pipe_is_running(pipe))
918 /* get enable DMA fifo */
919 fifo = usbhsf_get_dma_fifo(priv, pkt);
921 goto usbhsf_pio_prepare_push;
923 ret = usbhsf_fifo_select(pipe, fifo, 0);
925 goto usbhsf_pio_prepare_push;
927 if (usbhsf_dma_map(pkt) < 0)
928 goto usbhsf_pio_prepare_push_unselect;
932 usbhsf_tx_irq_ctrl(pipe, 0);
933 /* FIXME: Workaound for usb dmac that driver can be used in atomic */
934 if (usbhs_get_dparam(priv, has_usb_dmac)) {
935 usbhsf_dma_xfer_preparing(pkt);
937 INIT_WORK(&pkt->work, xfer_work);
938 schedule_work(&pkt->work);
943 usbhsf_pio_prepare_push_unselect:
944 usbhsf_fifo_unselect(pipe, fifo);
945 usbhsf_pio_prepare_push:
947 * change handler to PIO
949 pkt->handler = &usbhs_fifo_pio_push_handler;
951 return pkt->handler->prepare(pkt, is_done);
954 static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
956 struct usbhs_pipe *pipe = pkt->pipe;
957 int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
959 pkt->actual += pkt->trans;
961 if (pkt->actual < pkt->length)
962 *is_done = 0; /* there are remainder data */
964 *is_done = 1; /* short packet */
966 *is_done = !pkt->zero; /* send zero packet? */
968 usbhs_pipe_running(pipe, !*is_done);
970 usbhsf_dma_stop(pipe, pipe->fifo);
971 usbhsf_dma_unmap(pkt);
972 usbhsf_fifo_unselect(pipe, pipe->fifo);
975 /* change handler to PIO */
976 pkt->handler = &usbhs_fifo_pio_push_handler;
977 return pkt->handler->try_run(pkt, is_done);
983 const struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
984 .prepare = usbhsf_dma_prepare_push,
985 .dma_done = usbhsf_dma_push_done,
992 static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
995 return usbhsf_prepare_pop(pkt, is_done);
998 static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt,
1001 struct usbhs_pipe *pipe = pkt->pipe;
1002 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1003 struct usbhs_fifo *fifo;
1006 if (usbhs_pipe_is_busy(pipe))
1009 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
1010 if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) ||
1011 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
1012 goto usbhsf_pio_prepare_pop;
1014 fifo = usbhsf_get_dma_fifo(priv, pkt);
1016 goto usbhsf_pio_prepare_pop;
1018 if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1))
1019 goto usbhsf_pio_prepare_pop;
1021 /* return at this time if the pipe is running */
1022 if (usbhs_pipe_is_running(pipe))
1025 usbhs_pipe_config_change_bfre(pipe, 1);
1027 ret = usbhsf_fifo_select(pipe, fifo, 0);
1029 goto usbhsf_pio_prepare_pop;
1031 if (usbhsf_dma_map(pkt) < 0)
1032 goto usbhsf_pio_prepare_pop_unselect;
1037 * usbhs_fifo_dma_pop_handler :: prepare
1038 * enabled irq to come here.
1039 * but it is no longer needed for DMA. disable it.
1041 usbhsf_rx_irq_ctrl(pipe, 0);
1043 pkt->trans = pkt->length;
1045 usbhsf_dma_xfer_preparing(pkt);
1049 usbhsf_pio_prepare_pop_unselect:
1050 usbhsf_fifo_unselect(pipe, fifo);
1051 usbhsf_pio_prepare_pop:
1054 * change handler to PIO
1056 pkt->handler = &usbhs_fifo_pio_pop_handler;
1057 usbhs_pipe_config_change_bfre(pipe, 0);
1059 return pkt->handler->prepare(pkt, is_done);
1062 static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
1064 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1066 if (usbhs_get_dparam(priv, has_usb_dmac))
1067 return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done);
1069 return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
1072 static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
1074 struct usbhs_pipe *pipe = pkt->pipe;
1075 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1076 struct usbhs_fifo *fifo;
1079 if (usbhs_pipe_is_busy(pipe))
1082 if (usbhs_pipe_is_dcp(pipe))
1083 goto usbhsf_pio_prepare_pop;
1085 /* get enable DMA fifo */
1086 fifo = usbhsf_get_dma_fifo(priv, pkt);
1088 goto usbhsf_pio_prepare_pop;
1090 if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
1091 goto usbhsf_pio_prepare_pop;
1093 ret = usbhsf_fifo_select(pipe, fifo, 0);
1095 goto usbhsf_pio_prepare_pop;
1097 /* use PIO if packet is less than pio_dma_border */
1098 len = usbhsf_fifo_rcv_len(priv, fifo);
1099 len = min(pkt->length - pkt->actual, len);
1100 if (len & 0x7) /* 8byte alignment */
1101 goto usbhsf_pio_prepare_pop_unselect;
1103 if (len < usbhs_get_dparam(priv, pio_dma_border))
1104 goto usbhsf_pio_prepare_pop_unselect;
1106 ret = usbhsf_fifo_barrier(priv, fifo);
1108 goto usbhsf_pio_prepare_pop_unselect;
1110 if (usbhsf_dma_map(pkt) < 0)
1111 goto usbhsf_pio_prepare_pop_unselect;
1116 * usbhs_fifo_dma_pop_handler :: prepare
1117 * enabled irq to come here.
1118 * but it is no longer needed for DMA. disable it.
1120 usbhsf_rx_irq_ctrl(pipe, 0);
1124 INIT_WORK(&pkt->work, xfer_work);
1125 schedule_work(&pkt->work);
1129 usbhsf_pio_prepare_pop_unselect:
1130 usbhsf_fifo_unselect(pipe, fifo);
1131 usbhsf_pio_prepare_pop:
1134 * change handler to PIO
1136 pkt->handler = &usbhs_fifo_pio_pop_handler;
1138 return pkt->handler->try_run(pkt, is_done);
1141 static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
1143 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1145 BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
1147 return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
1150 static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
1152 struct usbhs_pipe *pipe = pkt->pipe;
1153 int maxp = usbhs_pipe_get_maxpacket(pipe);
1155 usbhsf_dma_stop(pipe, pipe->fifo);
1156 usbhsf_dma_unmap(pkt);
1157 usbhsf_fifo_unselect(pipe, pipe->fifo);
1159 pkt->actual += pkt->trans;
1161 if ((pkt->actual == pkt->length) || /* receive all data */
1162 (pkt->trans < maxp)) { /* short packet */
1164 usbhs_pipe_running(pipe, 0);
1167 usbhs_pipe_running(pipe, 0);
1168 usbhsf_prepare_pop(pkt, is_done);
1174 static size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt,
1175 struct dma_chan *chan, int dtln)
1177 struct usbhs_pipe *pipe = pkt->pipe;
1178 struct dma_tx_state state;
1179 size_t received_size;
1180 int maxp = usbhs_pipe_get_maxpacket(pipe);
1182 dmaengine_tx_status(chan, pkt->cookie, &state);
1183 received_size = pkt->length - state.residue;
1186 received_size -= USBHS_USB_DMAC_XFER_SIZE;
1187 received_size &= ~(maxp - 1);
1188 received_size += dtln;
1191 return received_size;
1194 static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt,
1197 struct usbhs_pipe *pipe = pkt->pipe;
1198 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1199 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
1200 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
1204 * Since the driver disables rx_irq in DMA mode, the interrupt handler
1205 * cannot the BRDYSTS. So, the function clears it here because the
1206 * driver may use PIO mode next time.
1208 usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe));
1210 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
1211 usbhsf_fifo_clear(pipe, fifo);
1212 pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len);
1214 usbhs_pipe_running(pipe, 0);
1215 usbhsf_dma_stop(pipe, fifo);
1216 usbhsf_dma_unmap(pkt);
1217 usbhsf_fifo_unselect(pipe, pipe->fifo);
1219 /* The driver can assume the rx transaction is always "done" */
1225 static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
1227 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1229 if (usbhs_get_dparam(priv, has_usb_dmac))
1230 return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done);
1232 return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
1235 const struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
1236 .prepare = usbhsf_dma_prepare_pop,
1237 .try_run = usbhsf_dma_try_pop,
1238 .dma_done = usbhsf_dma_pop_done
1244 static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
1246 struct sh_dmae_slave *slave = param;
1251 * usbhs doesn't recognize id = 0 as valid DMA
1253 if (0 == slave->shdma_slave.slave_id)
1256 chan->private = slave;
1261 static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
1264 dma_release_channel(fifo->tx_chan);
1266 dma_release_channel(fifo->rx_chan);
1268 fifo->tx_chan = NULL;
1269 fifo->rx_chan = NULL;
1272 static void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo)
1274 dma_cap_mask_t mask;
1277 dma_cap_set(DMA_SLAVE, mask);
1278 fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1282 dma_cap_set(DMA_SLAVE, mask);
1283 fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1287 static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo,
1293 * To avoid complex handing for DnFIFOs, the driver uses each
1294 * DnFIFO as TX or RX direction (not bi-direction).
1295 * So, the driver uses odd channels for TX, even channels for RX.
1297 snprintf(name, sizeof(name), "ch%d", channel);
1299 fifo->tx_chan = dma_request_slave_channel_reason(dev, name);
1300 if (IS_ERR(fifo->tx_chan))
1301 fifo->tx_chan = NULL;
1303 fifo->rx_chan = dma_request_slave_channel_reason(dev, name);
1304 if (IS_ERR(fifo->rx_chan))
1305 fifo->rx_chan = NULL;
1309 static void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo,
1312 struct device *dev = usbhs_priv_to_dev(priv);
1315 usbhsf_dma_init_dt(dev, fifo, channel);
1317 usbhsf_dma_init_pdev(fifo);
1319 if (fifo->tx_chan || fifo->rx_chan)
1320 dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
1322 fifo->tx_chan ? "[TX]" : " ",
1323 fifo->rx_chan ? "[RX]" : " ");
1329 static int usbhsf_irq_empty(struct usbhs_priv *priv,
1330 struct usbhs_irq_state *irq_state)
1332 struct usbhs_pipe *pipe;
1333 struct device *dev = usbhs_priv_to_dev(priv);
1336 if (!irq_state->bempsts) {
1337 dev_err(dev, "debug %s !!\n", __func__);
1341 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
1344 * search interrupted "pipe"
1347 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1348 if (!(irq_state->bempsts & (1 << i)))
1351 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1353 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
1359 static int usbhsf_irq_ready(struct usbhs_priv *priv,
1360 struct usbhs_irq_state *irq_state)
1362 struct usbhs_pipe *pipe;
1363 struct device *dev = usbhs_priv_to_dev(priv);
1366 if (!irq_state->brdysts) {
1367 dev_err(dev, "debug %s !!\n", __func__);
1371 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
1374 * search interrupted "pipe"
1377 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1378 if (!(irq_state->brdysts & (1 << i)))
1381 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1383 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
1389 static void usbhsf_dma_complete(void *arg)
1391 struct usbhs_pipe *pipe = arg;
1392 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1393 struct device *dev = usbhs_priv_to_dev(priv);
1396 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
1398 dev_err(dev, "dma_complete run_error %d : %d\n",
1399 usbhs_pipe_number(pipe), ret);
1402 void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
1404 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1405 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
1407 /* clear DCP FIFO of transmission */
1408 if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
1410 usbhsf_fifo_clear(pipe, fifo);
1411 usbhsf_fifo_unselect(pipe, fifo);
1413 /* clear DCP FIFO of reception */
1414 if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
1416 usbhsf_fifo_clear(pipe, fifo);
1417 usbhsf_fifo_unselect(pipe, fifo);
1423 void usbhs_fifo_init(struct usbhs_priv *priv)
1425 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1426 struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
1427 struct usbhs_fifo *dfifo;
1430 mod->irq_empty = usbhsf_irq_empty;
1431 mod->irq_ready = usbhsf_irq_ready;
1432 mod->irq_bempsts = 0;
1433 mod->irq_brdysts = 0;
1436 usbhs_for_each_dfifo(priv, dfifo, i)
1440 void usbhs_fifo_quit(struct usbhs_priv *priv)
1442 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1444 mod->irq_empty = NULL;
1445 mod->irq_ready = NULL;
1446 mod->irq_bempsts = 0;
1447 mod->irq_brdysts = 0;
1450 #define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port) \
1452 fifo = usbhsf_get_dnfifo(priv, channel); \
1453 fifo->name = "D"#channel"FIFO"; \
1454 fifo->port = fifo_port; \
1455 fifo->sel = D##channel##FIFOSEL; \
1456 fifo->ctr = D##channel##FIFOCTR; \
1457 fifo->tx_slave.shdma_slave.slave_id = \
1458 usbhs_get_dparam(priv, d##channel##_tx_id); \
1459 fifo->rx_slave.shdma_slave.slave_id = \
1460 usbhs_get_dparam(priv, d##channel##_rx_id); \
1461 usbhsf_dma_init(priv, fifo, channel); \
1464 #define USBHS_DFIFO_INIT(priv, fifo, channel) \
1465 __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
1466 #define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel) \
1467 __USBHS_DFIFO_INIT(priv, fifo, channel, 0)
1469 int usbhs_fifo_probe(struct usbhs_priv *priv)
1471 struct usbhs_fifo *fifo;
1474 fifo = usbhsf_get_cfifo(priv);
1475 fifo->name = "CFIFO";
1477 fifo->sel = CFIFOSEL;
1478 fifo->ctr = CFIFOCTR;
1481 USBHS_DFIFO_INIT(priv, fifo, 0);
1482 USBHS_DFIFO_INIT(priv, fifo, 1);
1483 USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2);
1484 USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3);
1489 void usbhs_fifo_remove(struct usbhs_priv *priv)
1491 struct usbhs_fifo *fifo;
1494 usbhs_for_each_dfifo(priv, fifo, i)
1495 usbhsf_dma_quit(priv, fifo);