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 if (list_empty(&pipe->list))
106 return list_first_entry(&pipe->list, struct usbhs_pkt, node);
109 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
110 struct usbhs_fifo *fifo);
111 static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
112 struct usbhs_fifo *fifo);
113 static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
114 struct usbhs_pkt *pkt);
115 #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
116 #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
117 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map);
118 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable);
119 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable);
120 struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
122 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
123 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
126 /******************** spin lock ********************/
127 usbhs_lock(priv, flags);
129 usbhs_pipe_disable(pipe);
132 pkt = __usbhsf_pkt_get(pipe);
135 struct dma_chan *chan = NULL;
138 chan = usbhsf_dma_chan_get(fifo, pkt);
140 dmaengine_terminate_all(chan);
141 usbhsf_fifo_clear(pipe, fifo);
142 usbhsf_dma_unmap(pkt);
144 if (usbhs_pipe_is_dir_in(pipe))
145 usbhsf_rx_irq_ctrl(pipe, 0);
147 usbhsf_tx_irq_ctrl(pipe, 0);
150 usbhs_pipe_running(pipe, 0);
152 __usbhsf_pkt_del(pkt);
156 usbhsf_fifo_unselect(pipe, fifo);
158 usbhs_unlock(priv, flags);
159 /******************** spin unlock ******************/
170 static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
172 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
173 struct usbhs_pkt *pkt;
174 struct device *dev = usbhs_priv_to_dev(priv);
175 int (*func)(struct usbhs_pkt *pkt, int *is_done);
180 /******************** spin lock ********************/
181 usbhs_lock(priv, flags);
183 pkt = __usbhsf_pkt_get(pipe);
185 goto __usbhs_pkt_handler_end;
188 case USBHSF_PKT_PREPARE:
189 func = pkt->handler->prepare;
191 case USBHSF_PKT_TRY_RUN:
192 func = pkt->handler->try_run;
194 case USBHSF_PKT_DMA_DONE:
195 func = pkt->handler->dma_done;
198 dev_err(dev, "unknown pkt handler\n");
199 goto __usbhs_pkt_handler_end;
203 ret = func(pkt, &is_done);
206 __usbhsf_pkt_del(pkt);
208 __usbhs_pkt_handler_end:
209 usbhs_unlock(priv, flags);
210 /******************** spin unlock ******************/
213 pkt->done(priv, pkt);
214 usbhs_pkt_start(pipe);
220 void usbhs_pkt_start(struct usbhs_pipe *pipe)
222 usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
226 * irq enable/disable function
228 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
229 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
230 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
232 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
233 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
234 u16 status = (1 << usbhs_pipe_number(pipe)); \
238 mod->status |= status; \
240 mod->status &= ~status; \
241 usbhs_irq_callback_update(priv, mod); \
244 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
247 * And DCP pipe can NOT use "ready interrupt" for "send"
248 * it should use "empty" interrupt.
250 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
252 * on the other hand, normal pipe can use "ready interrupt" for "send"
253 * even though it is single/double buffer
255 if (usbhs_pipe_is_dcp(pipe))
256 usbhsf_irq_empty_ctrl(pipe, enable);
258 usbhsf_irq_ready_ctrl(pipe, enable);
261 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
263 usbhsf_irq_ready_ctrl(pipe, enable);
269 static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
270 struct usbhs_fifo *fifo)
272 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
274 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
277 static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
278 struct usbhs_fifo *fifo)
283 /* The FIFO port is accessible */
284 if (usbhs_read(priv, fifo->ctr) & FRDY)
293 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
294 struct usbhs_fifo *fifo)
296 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
299 if (!usbhs_pipe_is_dcp(pipe)) {
301 * This driver checks the pipe condition first to avoid -EBUSY
302 * from usbhsf_fifo_barrier() with about 10 msec delay in
303 * the interrupt handler if the pipe is RX direction and empty.
305 if (usbhs_pipe_is_dir_in(pipe))
306 ret = usbhs_pipe_is_accessible(pipe);
308 ret = usbhsf_fifo_barrier(priv, fifo);
312 * if non-DCP pipe, this driver should set BCLR when
313 * usbhsf_fifo_barrier() returns 0.
316 usbhs_write(priv, fifo->ctr, BCLR);
319 static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
320 struct usbhs_fifo *fifo)
322 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
325 static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
326 struct usbhs_fifo *fifo)
328 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
330 usbhs_pipe_select_fifo(pipe, NULL);
331 usbhs_write(priv, fifo->sel, 0);
334 static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
335 struct usbhs_fifo *fifo,
338 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
339 struct device *dev = usbhs_priv_to_dev(priv);
341 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
342 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
344 if (usbhs_pipe_is_busy(pipe) ||
345 usbhsf_fifo_is_busy(fifo))
348 if (usbhs_pipe_is_dcp(pipe)) {
349 base |= (1 == write) << 5; /* ISEL */
351 if (usbhs_mod_is_host(priv))
352 usbhs_dcp_dir_for_host(pipe, write);
355 /* "base" will be used below */
356 if (usbhs_get_dparam(priv, has_sudmac) && !usbhsf_is_cfifo(priv, fifo))
357 usbhs_write(priv, fifo->sel, base);
359 usbhs_write(priv, fifo->sel, base | MBW_32);
361 /* check ISEL and CURPIPE value */
363 if (base == (mask & usbhs_read(priv, fifo->sel))) {
364 usbhs_pipe_select_fifo(pipe, fifo);
370 dev_err(dev, "fifo select error\n");
378 static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
380 struct usbhs_pipe *pipe = pkt->pipe;
381 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
382 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
383 struct device *dev = usbhs_priv_to_dev(priv);
386 usbhs_pipe_disable(pipe);
388 ret = usbhsf_fifo_select(pipe, fifo, 1);
390 dev_err(dev, "%s() faile\n", __func__);
394 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
396 usbhsf_fifo_clear(pipe, fifo);
397 usbhsf_send_terminator(pipe, fifo);
399 usbhsf_fifo_unselect(pipe, fifo);
401 usbhsf_tx_irq_ctrl(pipe, 1);
402 usbhs_pipe_enable(pipe);
407 static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
409 struct usbhs_pipe *pipe = pkt->pipe;
410 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
411 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
412 struct device *dev = usbhs_priv_to_dev(priv);
415 usbhs_pipe_disable(pipe);
417 ret = usbhsf_fifo_select(pipe, fifo, 0);
419 dev_err(dev, "%s() fail\n", __func__);
423 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
424 usbhsf_fifo_clear(pipe, fifo);
426 usbhsf_fifo_unselect(pipe, fifo);
428 usbhsf_rx_irq_ctrl(pipe, 1);
429 usbhs_pipe_enable(pipe);
435 static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
437 struct usbhs_pipe *pipe = pkt->pipe;
439 if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
440 usbhsf_tx_irq_ctrl(pipe, 0);
442 usbhsf_rx_irq_ctrl(pipe, 0);
444 pkt->actual = pkt->length;
450 const struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
451 .prepare = usbhs_dcp_dir_switch_to_write,
452 .try_run = usbhs_dcp_dir_switch_done,
455 const struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
456 .prepare = usbhs_dcp_dir_switch_to_read,
457 .try_run = usbhs_dcp_dir_switch_done,
461 * DCP data stage (push)
463 static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
465 struct usbhs_pipe *pipe = pkt->pipe;
467 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
470 * change handler to PIO push
472 pkt->handler = &usbhs_fifo_pio_push_handler;
474 return pkt->handler->prepare(pkt, is_done);
477 const struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
478 .prepare = usbhsf_dcp_data_stage_try_push,
482 * DCP data stage (pop)
484 static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
487 struct usbhs_pipe *pipe = pkt->pipe;
488 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
489 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
491 if (usbhs_pipe_is_busy(pipe))
495 * prepare pop for DCP should
496 * - change DCP direction,
500 usbhs_pipe_disable(pipe);
502 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
504 usbhsf_fifo_select(pipe, fifo, 0);
505 usbhsf_fifo_clear(pipe, fifo);
506 usbhsf_fifo_unselect(pipe, fifo);
509 * change handler to PIO pop
511 pkt->handler = &usbhs_fifo_pio_pop_handler;
513 return pkt->handler->prepare(pkt, is_done);
516 const struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
517 .prepare = usbhsf_dcp_data_stage_prepare_pop,
523 static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
525 struct usbhs_pipe *pipe = pkt->pipe;
526 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
527 struct device *dev = usbhs_priv_to_dev(priv);
528 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
529 void __iomem *addr = priv->base + fifo->port;
531 int maxp = usbhs_pipe_get_maxpacket(pipe);
536 usbhs_pipe_data_sequence(pipe, pkt->sequence);
537 pkt->sequence = -1; /* -1 sequence will be ignored */
539 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
541 ret = usbhsf_fifo_select(pipe, fifo, 1);
545 ret = usbhs_pipe_is_accessible(pipe);
547 /* inaccessible pipe is not an error */
549 goto usbhs_fifo_write_busy;
552 ret = usbhsf_fifo_barrier(priv, fifo);
554 goto usbhs_fifo_write_busy;
556 buf = pkt->buf + pkt->actual;
557 len = pkt->length - pkt->actual;
558 len = min(len, maxp);
560 is_short = total_len < maxp;
567 if (len >= 4 && !((unsigned long)buf & 0x03)) {
568 iowrite32_rep(addr, buf, len / 4);
570 buf += total_len - len;
573 /* the rest operation */
574 for (i = 0; i < len; i++)
575 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
580 pkt->actual += total_len;
582 if (pkt->actual < pkt->length)
583 *is_done = 0; /* there are remainder data */
585 *is_done = 1; /* short packet */
587 *is_done = !pkt->zero; /* send zero packet ? */
593 usbhsf_send_terminator(pipe, fifo);
595 usbhsf_tx_irq_ctrl(pipe, !*is_done);
596 usbhs_pipe_running(pipe, !*is_done);
597 usbhs_pipe_enable(pipe);
599 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
600 usbhs_pipe_number(pipe),
601 pkt->length, pkt->actual, *is_done, pkt->zero);
603 usbhsf_fifo_unselect(pipe, fifo);
607 usbhs_fifo_write_busy:
608 usbhsf_fifo_unselect(pipe, fifo);
614 usbhsf_tx_irq_ctrl(pipe, 1);
615 usbhs_pipe_running(pipe, 1);
620 static int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done)
622 if (usbhs_pipe_is_running(pkt->pipe))
625 return usbhsf_pio_try_push(pkt, is_done);
628 const struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
629 .prepare = usbhsf_pio_prepare_push,
630 .try_run = usbhsf_pio_try_push,
636 static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
638 struct usbhs_pipe *pipe = pkt->pipe;
639 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
640 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
642 if (usbhs_pipe_is_busy(pipe))
645 if (usbhs_pipe_is_running(pipe))
649 * pipe enable to prepare packet receive
651 usbhs_pipe_data_sequence(pipe, pkt->sequence);
652 pkt->sequence = -1; /* -1 sequence will be ignored */
654 if (usbhs_pipe_is_dcp(pipe))
655 usbhsf_fifo_clear(pipe, fifo);
657 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
658 usbhs_pipe_enable(pipe);
659 usbhs_pipe_running(pipe, 1);
660 usbhsf_rx_irq_ctrl(pipe, 1);
665 static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
667 struct usbhs_pipe *pipe = pkt->pipe;
668 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
669 struct device *dev = usbhs_priv_to_dev(priv);
670 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
671 void __iomem *addr = priv->base + fifo->port;
674 int maxp = usbhs_pipe_get_maxpacket(pipe);
679 ret = usbhsf_fifo_select(pipe, fifo, 0);
683 ret = usbhsf_fifo_barrier(priv, fifo);
685 goto usbhs_fifo_read_busy;
687 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
689 buf = pkt->buf + pkt->actual;
690 len = pkt->length - pkt->actual;
691 len = min(len, rcv_len);
695 * update actual length first here to decide disable pipe.
696 * if this pipe keeps BUF status and all data were popped,
697 * then, next interrupt/token will be issued again
699 pkt->actual += total_len;
701 if ((pkt->actual == pkt->length) || /* receive all data */
702 (total_len < maxp)) { /* short packet */
704 usbhsf_rx_irq_ctrl(pipe, 0);
705 usbhs_pipe_running(pipe, 0);
707 * If function mode, since this controller is possible to enter
708 * Control Write status stage at this timing, this driver
709 * should not disable the pipe. If such a case happens, this
710 * controller is not able to complete the status stage.
712 if (!usbhs_mod_is_host(priv) && !usbhs_pipe_is_dcp(pipe))
713 usbhs_pipe_disable(pipe); /* disable pipe first */
717 * Buffer clear if Zero-Length packet
720 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
724 usbhsf_fifo_clear(pipe, fifo);
725 goto usbhs_fifo_read_end;
733 if (len >= 4 && !((unsigned long)buf & 0x03)) {
734 ioread32_rep(addr, buf, len / 4);
736 buf += total_len - len;
739 /* the rest operation */
740 for (i = 0; i < len; i++) {
742 data = ioread32(addr);
744 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
748 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
749 usbhs_pipe_number(pipe),
750 pkt->length, pkt->actual, *is_done, pkt->zero);
752 usbhs_fifo_read_busy:
753 usbhsf_fifo_unselect(pipe, fifo);
758 const struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
759 .prepare = usbhsf_prepare_pop,
760 .try_run = usbhsf_pio_try_pop,
764 * DCP ctrol statge handler
766 static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
768 usbhs_dcp_control_transfer_done(pkt->pipe);
775 const struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
776 .prepare = usbhsf_ctrl_stage_end,
777 .try_run = usbhsf_ctrl_stage_end,
783 static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
784 struct usbhs_pkt *pkt)
786 if (&usbhs_fifo_dma_push_handler == pkt->handler)
787 return fifo->tx_chan;
789 if (&usbhs_fifo_dma_pop_handler == pkt->handler)
790 return fifo->rx_chan;
795 static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
796 struct usbhs_pkt *pkt)
798 struct usbhs_fifo *fifo;
801 usbhs_for_each_dfifo(priv, fifo, i) {
802 if (usbhsf_dma_chan_get(fifo, pkt) &&
803 !usbhsf_fifo_is_busy(fifo))
810 #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
811 #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
812 static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
813 struct usbhs_fifo *fifo,
816 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
818 usbhs_bset(priv, fifo->sel, DREQE, dreqe);
821 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
823 struct usbhs_pipe *pipe = pkt->pipe;
824 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
825 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
826 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
827 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
829 return info->dma_map_ctrl(chan->device->dev, pkt, map);
832 static void usbhsf_dma_complete(void *arg);
833 static void usbhsf_dma_xfer_preparing(struct usbhs_pkt *pkt)
835 struct usbhs_pipe *pipe = pkt->pipe;
836 struct usbhs_fifo *fifo;
837 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
838 struct dma_async_tx_descriptor *desc;
839 struct dma_chan *chan;
840 struct device *dev = usbhs_priv_to_dev(priv);
841 enum dma_transfer_direction dir;
843 fifo = usbhs_pipe_to_fifo(pipe);
847 chan = usbhsf_dma_chan_get(fifo, pkt);
848 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
850 desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
852 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
856 desc->callback = usbhsf_dma_complete;
857 desc->callback_param = pipe;
859 pkt->cookie = dmaengine_submit(desc);
860 if (pkt->cookie < 0) {
861 dev_err(dev, "Failed to submit dma descriptor\n");
865 dev_dbg(dev, " %s %d (%d/ %d)\n",
866 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
868 usbhs_pipe_running(pipe, 1);
869 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
870 dma_async_issue_pending(chan);
871 usbhsf_dma_start(pipe, fifo);
872 usbhs_pipe_enable(pipe);
875 static void xfer_work(struct work_struct *work)
877 struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
878 struct usbhs_pipe *pipe = pkt->pipe;
879 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
882 usbhs_lock(priv, flags);
883 usbhsf_dma_xfer_preparing(pkt);
884 usbhs_unlock(priv, flags);
890 static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
892 struct usbhs_pipe *pipe = pkt->pipe;
893 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
894 struct usbhs_fifo *fifo;
895 int len = pkt->length - pkt->actual;
897 uintptr_t align_mask;
899 if (usbhs_pipe_is_busy(pipe))
902 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
903 if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
904 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
905 goto usbhsf_pio_prepare_push;
907 /* check data length if this driver don't use USB-DMAC */
908 if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7)
909 goto usbhsf_pio_prepare_push;
911 /* check buffer alignment */
912 align_mask = usbhs_get_dparam(priv, has_usb_dmac) ?
913 USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7;
914 if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask)
915 goto usbhsf_pio_prepare_push;
917 /* return at this time if the pipe is running */
918 if (usbhs_pipe_is_running(pipe))
921 /* get enable DMA fifo */
922 fifo = usbhsf_get_dma_fifo(priv, pkt);
924 goto usbhsf_pio_prepare_push;
926 ret = usbhsf_fifo_select(pipe, fifo, 0);
928 goto usbhsf_pio_prepare_push;
930 if (usbhsf_dma_map(pkt) < 0)
931 goto usbhsf_pio_prepare_push_unselect;
935 usbhsf_tx_irq_ctrl(pipe, 0);
936 /* FIXME: Workaound for usb dmac that driver can be used in atomic */
937 if (usbhs_get_dparam(priv, has_usb_dmac)) {
938 usbhsf_dma_xfer_preparing(pkt);
940 INIT_WORK(&pkt->work, xfer_work);
941 schedule_work(&pkt->work);
946 usbhsf_pio_prepare_push_unselect:
947 usbhsf_fifo_unselect(pipe, fifo);
948 usbhsf_pio_prepare_push:
950 * change handler to PIO
952 pkt->handler = &usbhs_fifo_pio_push_handler;
954 return pkt->handler->prepare(pkt, is_done);
957 static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
959 struct usbhs_pipe *pipe = pkt->pipe;
960 int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
962 pkt->actual += pkt->trans;
964 if (pkt->actual < pkt->length)
965 *is_done = 0; /* there are remainder data */
967 *is_done = 1; /* short packet */
969 *is_done = !pkt->zero; /* send zero packet? */
971 usbhs_pipe_running(pipe, !*is_done);
973 usbhsf_dma_stop(pipe, pipe->fifo);
974 usbhsf_dma_unmap(pkt);
975 usbhsf_fifo_unselect(pipe, pipe->fifo);
978 /* change handler to PIO */
979 pkt->handler = &usbhs_fifo_pio_push_handler;
980 return pkt->handler->try_run(pkt, is_done);
986 const struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
987 .prepare = usbhsf_dma_prepare_push,
988 .dma_done = usbhsf_dma_push_done,
995 static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
998 return usbhsf_prepare_pop(pkt, is_done);
1001 static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt,
1004 struct usbhs_pipe *pipe = pkt->pipe;
1005 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1006 struct usbhs_fifo *fifo;
1009 if (usbhs_pipe_is_busy(pipe))
1012 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
1013 if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) ||
1014 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
1015 goto usbhsf_pio_prepare_pop;
1017 fifo = usbhsf_get_dma_fifo(priv, pkt);
1019 goto usbhsf_pio_prepare_pop;
1021 if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1))
1022 goto usbhsf_pio_prepare_pop;
1024 /* return at this time if the pipe is running */
1025 if (usbhs_pipe_is_running(pipe))
1028 usbhs_pipe_config_change_bfre(pipe, 1);
1030 ret = usbhsf_fifo_select(pipe, fifo, 0);
1032 goto usbhsf_pio_prepare_pop;
1034 if (usbhsf_dma_map(pkt) < 0)
1035 goto usbhsf_pio_prepare_pop_unselect;
1040 * usbhs_fifo_dma_pop_handler :: prepare
1041 * enabled irq to come here.
1042 * but it is no longer needed for DMA. disable it.
1044 usbhsf_rx_irq_ctrl(pipe, 0);
1046 pkt->trans = pkt->length;
1048 usbhsf_dma_xfer_preparing(pkt);
1052 usbhsf_pio_prepare_pop_unselect:
1053 usbhsf_fifo_unselect(pipe, fifo);
1054 usbhsf_pio_prepare_pop:
1057 * change handler to PIO
1059 pkt->handler = &usbhs_fifo_pio_pop_handler;
1060 usbhs_pipe_config_change_bfre(pipe, 0);
1062 return pkt->handler->prepare(pkt, is_done);
1065 static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
1067 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1069 if (usbhs_get_dparam(priv, has_usb_dmac))
1070 return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done);
1072 return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
1075 static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
1077 struct usbhs_pipe *pipe = pkt->pipe;
1078 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1079 struct usbhs_fifo *fifo;
1082 if (usbhs_pipe_is_busy(pipe))
1085 if (usbhs_pipe_is_dcp(pipe))
1086 goto usbhsf_pio_prepare_pop;
1088 /* get enable DMA fifo */
1089 fifo = usbhsf_get_dma_fifo(priv, pkt);
1091 goto usbhsf_pio_prepare_pop;
1093 if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
1094 goto usbhsf_pio_prepare_pop;
1096 ret = usbhsf_fifo_select(pipe, fifo, 0);
1098 goto usbhsf_pio_prepare_pop;
1100 /* use PIO if packet is less than pio_dma_border */
1101 len = usbhsf_fifo_rcv_len(priv, fifo);
1102 len = min(pkt->length - pkt->actual, len);
1103 if (len & 0x7) /* 8byte alignment */
1104 goto usbhsf_pio_prepare_pop_unselect;
1106 if (len < usbhs_get_dparam(priv, pio_dma_border))
1107 goto usbhsf_pio_prepare_pop_unselect;
1109 ret = usbhsf_fifo_barrier(priv, fifo);
1111 goto usbhsf_pio_prepare_pop_unselect;
1113 if (usbhsf_dma_map(pkt) < 0)
1114 goto usbhsf_pio_prepare_pop_unselect;
1119 * usbhs_fifo_dma_pop_handler :: prepare
1120 * enabled irq to come here.
1121 * but it is no longer needed for DMA. disable it.
1123 usbhsf_rx_irq_ctrl(pipe, 0);
1127 INIT_WORK(&pkt->work, xfer_work);
1128 schedule_work(&pkt->work);
1132 usbhsf_pio_prepare_pop_unselect:
1133 usbhsf_fifo_unselect(pipe, fifo);
1134 usbhsf_pio_prepare_pop:
1137 * change handler to PIO
1139 pkt->handler = &usbhs_fifo_pio_pop_handler;
1141 return pkt->handler->try_run(pkt, is_done);
1144 static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
1146 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1148 BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
1150 return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
1153 static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
1155 struct usbhs_pipe *pipe = pkt->pipe;
1156 int maxp = usbhs_pipe_get_maxpacket(pipe);
1158 usbhsf_dma_stop(pipe, pipe->fifo);
1159 usbhsf_dma_unmap(pkt);
1160 usbhsf_fifo_unselect(pipe, pipe->fifo);
1162 pkt->actual += pkt->trans;
1164 if ((pkt->actual == pkt->length) || /* receive all data */
1165 (pkt->trans < maxp)) { /* short packet */
1167 usbhs_pipe_running(pipe, 0);
1170 usbhs_pipe_running(pipe, 0);
1171 usbhsf_prepare_pop(pkt, is_done);
1177 static size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt,
1178 struct dma_chan *chan, int dtln)
1180 struct usbhs_pipe *pipe = pkt->pipe;
1181 struct dma_tx_state state;
1182 size_t received_size;
1183 int maxp = usbhs_pipe_get_maxpacket(pipe);
1185 dmaengine_tx_status(chan, pkt->cookie, &state);
1186 received_size = pkt->length - state.residue;
1189 received_size -= USBHS_USB_DMAC_XFER_SIZE;
1190 received_size &= ~(maxp - 1);
1191 received_size += dtln;
1194 return received_size;
1197 static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt,
1200 struct usbhs_pipe *pipe = pkt->pipe;
1201 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1202 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
1203 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
1207 * Since the driver disables rx_irq in DMA mode, the interrupt handler
1208 * cannot the BRDYSTS. So, the function clears it here because the
1209 * driver may use PIO mode next time.
1211 usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe));
1213 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
1214 usbhsf_fifo_clear(pipe, fifo);
1215 pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len);
1217 usbhs_pipe_running(pipe, 0);
1218 usbhsf_dma_stop(pipe, fifo);
1219 usbhsf_dma_unmap(pkt);
1220 usbhsf_fifo_unselect(pipe, pipe->fifo);
1222 /* The driver can assume the rx transaction is always "done" */
1228 static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
1230 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1232 if (usbhs_get_dparam(priv, has_usb_dmac))
1233 return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done);
1235 return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
1238 const struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
1239 .prepare = usbhsf_dma_prepare_pop,
1240 .try_run = usbhsf_dma_try_pop,
1241 .dma_done = usbhsf_dma_pop_done
1247 static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
1249 struct sh_dmae_slave *slave = param;
1254 * usbhs doesn't recognize id = 0 as valid DMA
1256 if (0 == slave->shdma_slave.slave_id)
1259 chan->private = slave;
1264 static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
1267 dma_release_channel(fifo->tx_chan);
1269 dma_release_channel(fifo->rx_chan);
1271 fifo->tx_chan = NULL;
1272 fifo->rx_chan = NULL;
1275 static void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo)
1277 dma_cap_mask_t mask;
1280 dma_cap_set(DMA_SLAVE, mask);
1281 fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1285 dma_cap_set(DMA_SLAVE, mask);
1286 fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1290 static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo,
1296 * To avoid complex handing for DnFIFOs, the driver uses each
1297 * DnFIFO as TX or RX direction (not bi-direction).
1298 * So, the driver uses odd channels for TX, even channels for RX.
1300 snprintf(name, sizeof(name), "ch%d", channel);
1302 fifo->tx_chan = dma_request_slave_channel_reason(dev, name);
1303 if (IS_ERR(fifo->tx_chan))
1304 fifo->tx_chan = NULL;
1306 fifo->rx_chan = dma_request_slave_channel_reason(dev, name);
1307 if (IS_ERR(fifo->rx_chan))
1308 fifo->rx_chan = NULL;
1312 static void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo,
1315 struct device *dev = usbhs_priv_to_dev(priv);
1318 usbhsf_dma_init_dt(dev, fifo, channel);
1320 usbhsf_dma_init_pdev(fifo);
1322 if (fifo->tx_chan || fifo->rx_chan)
1323 dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
1325 fifo->tx_chan ? "[TX]" : " ",
1326 fifo->rx_chan ? "[RX]" : " ");
1332 static int usbhsf_irq_empty(struct usbhs_priv *priv,
1333 struct usbhs_irq_state *irq_state)
1335 struct usbhs_pipe *pipe;
1336 struct device *dev = usbhs_priv_to_dev(priv);
1339 if (!irq_state->bempsts) {
1340 dev_err(dev, "debug %s !!\n", __func__);
1344 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
1347 * search interrupted "pipe"
1350 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1351 if (!(irq_state->bempsts & (1 << i)))
1354 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1356 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
1362 static int usbhsf_irq_ready(struct usbhs_priv *priv,
1363 struct usbhs_irq_state *irq_state)
1365 struct usbhs_pipe *pipe;
1366 struct device *dev = usbhs_priv_to_dev(priv);
1369 if (!irq_state->brdysts) {
1370 dev_err(dev, "debug %s !!\n", __func__);
1374 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
1377 * search interrupted "pipe"
1380 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1381 if (!(irq_state->brdysts & (1 << i)))
1384 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1386 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
1392 static void usbhsf_dma_complete(void *arg)
1394 struct usbhs_pipe *pipe = arg;
1395 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1396 struct device *dev = usbhs_priv_to_dev(priv);
1399 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
1401 dev_err(dev, "dma_complete run_error %d : %d\n",
1402 usbhs_pipe_number(pipe), ret);
1405 void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
1407 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1408 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
1410 /* clear DCP FIFO of transmission */
1411 if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
1413 usbhsf_fifo_clear(pipe, fifo);
1414 usbhsf_fifo_unselect(pipe, fifo);
1416 /* clear DCP FIFO of reception */
1417 if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
1419 usbhsf_fifo_clear(pipe, fifo);
1420 usbhsf_fifo_unselect(pipe, fifo);
1426 void usbhs_fifo_init(struct usbhs_priv *priv)
1428 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1429 struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
1430 struct usbhs_fifo *dfifo;
1433 mod->irq_empty = usbhsf_irq_empty;
1434 mod->irq_ready = usbhsf_irq_ready;
1435 mod->irq_bempsts = 0;
1436 mod->irq_brdysts = 0;
1439 usbhs_for_each_dfifo(priv, dfifo, i)
1443 void usbhs_fifo_quit(struct usbhs_priv *priv)
1445 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1447 mod->irq_empty = NULL;
1448 mod->irq_ready = NULL;
1449 mod->irq_bempsts = 0;
1450 mod->irq_brdysts = 0;
1453 #define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port) \
1455 fifo = usbhsf_get_dnfifo(priv, channel); \
1456 fifo->name = "D"#channel"FIFO"; \
1457 fifo->port = fifo_port; \
1458 fifo->sel = D##channel##FIFOSEL; \
1459 fifo->ctr = D##channel##FIFOCTR; \
1460 fifo->tx_slave.shdma_slave.slave_id = \
1461 usbhs_get_dparam(priv, d##channel##_tx_id); \
1462 fifo->rx_slave.shdma_slave.slave_id = \
1463 usbhs_get_dparam(priv, d##channel##_rx_id); \
1464 usbhsf_dma_init(priv, fifo, channel); \
1467 #define USBHS_DFIFO_INIT(priv, fifo, channel) \
1468 __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
1469 #define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel) \
1470 __USBHS_DFIFO_INIT(priv, fifo, channel, 0)
1472 int usbhs_fifo_probe(struct usbhs_priv *priv)
1474 struct usbhs_fifo *fifo;
1477 fifo = usbhsf_get_cfifo(priv);
1478 fifo->name = "CFIFO";
1480 fifo->sel = CFIFOSEL;
1481 fifo->ctr = CFIFOCTR;
1484 USBHS_DFIFO_INIT(priv, fifo, 0);
1485 USBHS_DFIFO_INIT(priv, fifo, 1);
1486 USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2);
1487 USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3);
1492 void usbhs_fifo_remove(struct usbhs_priv *priv)
1494 struct usbhs_fifo *fifo;
1497 usbhs_for_each_dfifo(priv, fifo, i)
1498 usbhsf_dma_quit(priv, fifo);