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_get_d0fifo(p) (&((p)->fifo_info.d0fifo))
25 #define usbhsf_get_d1fifo(p) (&((p)->fifo_info.d1fifo))
26 #define usbhsf_is_cfifo(p, f) (usbhsf_get_cfifo(p) == f)
28 #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
33 void usbhs_pkt_init(struct usbhs_pkt *pkt)
35 INIT_LIST_HEAD(&pkt->node);
39 * packet control function
41 static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
43 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
44 struct device *dev = usbhs_priv_to_dev(priv);
46 dev_err(dev, "null handler\n");
51 static struct usbhs_pkt_handle usbhsf_null_handler = {
52 .prepare = usbhsf_null_handle,
53 .try_run = usbhsf_null_handle,
56 void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
57 void (*done)(struct usbhs_priv *priv,
58 struct usbhs_pkt *pkt),
59 void *buf, int len, int zero, int sequence)
61 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
62 struct device *dev = usbhs_priv_to_dev(priv);
66 dev_err(dev, "no done function\n");
70 /******************** spin lock ********************/
71 usbhs_lock(priv, flags);
74 dev_err(dev, "no handler function\n");
75 pipe->handler = &usbhsf_null_handler;
78 list_move_tail(&pkt->node, &pipe->list);
81 * each pkt must hold own handler.
82 * because handler might be changed by its situation.
83 * dma handler -> pio handler.
87 pkt->handler = pipe->handler;
92 pkt->sequence = sequence;
94 usbhs_unlock(priv, flags);
95 /******************** spin unlock ******************/
98 static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
100 list_del_init(&pkt->node);
103 static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
105 if (list_empty(&pipe->list))
108 return list_first_entry(&pipe->list, struct usbhs_pkt, node);
111 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
112 struct usbhs_fifo *fifo);
113 static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
114 struct usbhs_fifo *fifo);
115 static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
116 struct usbhs_pkt *pkt);
117 #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
118 #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
119 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map);
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);
145 __usbhsf_pkt_del(pkt);
149 usbhsf_fifo_unselect(pipe, fifo);
151 usbhs_unlock(priv, flags);
152 /******************** spin unlock ******************/
163 static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
165 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
166 struct usbhs_pkt *pkt;
167 struct device *dev = usbhs_priv_to_dev(priv);
168 int (*func)(struct usbhs_pkt *pkt, int *is_done);
173 /******************** spin lock ********************/
174 usbhs_lock(priv, flags);
176 pkt = __usbhsf_pkt_get(pipe);
178 goto __usbhs_pkt_handler_end;
181 case USBHSF_PKT_PREPARE:
182 func = pkt->handler->prepare;
184 case USBHSF_PKT_TRY_RUN:
185 func = pkt->handler->try_run;
187 case USBHSF_PKT_DMA_DONE:
188 func = pkt->handler->dma_done;
191 dev_err(dev, "unknown pkt handler\n");
192 goto __usbhs_pkt_handler_end;
195 ret = func(pkt, &is_done);
198 __usbhsf_pkt_del(pkt);
200 __usbhs_pkt_handler_end:
201 usbhs_unlock(priv, flags);
202 /******************** spin unlock ******************/
205 pkt->done(priv, pkt);
206 usbhs_pkt_start(pipe);
212 void usbhs_pkt_start(struct usbhs_pipe *pipe)
214 usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
218 * irq enable/disable function
220 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
221 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
222 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
224 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
225 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
226 u16 status = (1 << usbhs_pipe_number(pipe)); \
230 mod->status |= status; \
232 mod->status &= ~status; \
233 usbhs_irq_callback_update(priv, mod); \
236 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
239 * And DCP pipe can NOT use "ready interrupt" for "send"
240 * it should use "empty" interrupt.
242 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
244 * on the other hand, normal pipe can use "ready interrupt" for "send"
245 * even though it is single/double buffer
247 if (usbhs_pipe_is_dcp(pipe))
248 usbhsf_irq_empty_ctrl(pipe, enable);
250 usbhsf_irq_ready_ctrl(pipe, enable);
253 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
255 usbhsf_irq_ready_ctrl(pipe, enable);
261 static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
262 struct usbhs_fifo *fifo)
264 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
266 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
269 static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
270 struct usbhs_fifo *fifo)
275 /* The FIFO port is accessible */
276 if (usbhs_read(priv, fifo->ctr) & FRDY)
285 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
286 struct usbhs_fifo *fifo)
288 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
290 if (!usbhs_pipe_is_dcp(pipe))
291 usbhsf_fifo_barrier(priv, fifo);
293 usbhs_write(priv, fifo->ctr, BCLR);
296 static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
297 struct usbhs_fifo *fifo)
299 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
302 static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
303 struct usbhs_fifo *fifo)
305 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
307 usbhs_pipe_select_fifo(pipe, NULL);
308 usbhs_write(priv, fifo->sel, 0);
311 static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
312 struct usbhs_fifo *fifo,
315 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
316 struct device *dev = usbhs_priv_to_dev(priv);
318 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
319 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
321 if (usbhs_pipe_is_busy(pipe) ||
322 usbhsf_fifo_is_busy(fifo))
325 if (usbhs_pipe_is_dcp(pipe)) {
326 base |= (1 == write) << 5; /* ISEL */
328 if (usbhs_mod_is_host(priv))
329 usbhs_dcp_dir_for_host(pipe, write);
332 /* "base" will be used below */
333 if (usbhs_get_dparam(priv, has_sudmac) && !usbhsf_is_cfifo(priv, fifo))
334 usbhs_write(priv, fifo->sel, base);
336 usbhs_write(priv, fifo->sel, base | MBW_32);
338 /* check ISEL and CURPIPE value */
340 if (base == (mask & usbhs_read(priv, fifo->sel))) {
341 usbhs_pipe_select_fifo(pipe, fifo);
347 dev_err(dev, "fifo select error\n");
355 static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
357 struct usbhs_pipe *pipe = pkt->pipe;
358 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
359 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
360 struct device *dev = usbhs_priv_to_dev(priv);
363 usbhs_pipe_disable(pipe);
365 ret = usbhsf_fifo_select(pipe, fifo, 1);
367 dev_err(dev, "%s() faile\n", __func__);
371 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
373 usbhsf_fifo_clear(pipe, fifo);
374 usbhsf_send_terminator(pipe, fifo);
376 usbhsf_fifo_unselect(pipe, fifo);
378 usbhsf_tx_irq_ctrl(pipe, 1);
379 usbhs_pipe_enable(pipe);
384 static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
386 struct usbhs_pipe *pipe = pkt->pipe;
387 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
388 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
389 struct device *dev = usbhs_priv_to_dev(priv);
392 usbhs_pipe_disable(pipe);
394 ret = usbhsf_fifo_select(pipe, fifo, 0);
396 dev_err(dev, "%s() fail\n", __func__);
400 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
401 usbhsf_fifo_clear(pipe, fifo);
403 usbhsf_fifo_unselect(pipe, fifo);
405 usbhsf_rx_irq_ctrl(pipe, 1);
406 usbhs_pipe_enable(pipe);
412 static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
414 struct usbhs_pipe *pipe = pkt->pipe;
416 if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
417 usbhsf_tx_irq_ctrl(pipe, 0);
419 usbhsf_rx_irq_ctrl(pipe, 0);
421 pkt->actual = pkt->length;
427 struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
428 .prepare = usbhs_dcp_dir_switch_to_write,
429 .try_run = usbhs_dcp_dir_switch_done,
432 struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
433 .prepare = usbhs_dcp_dir_switch_to_read,
434 .try_run = usbhs_dcp_dir_switch_done,
438 * DCP data stage (push)
440 static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
442 struct usbhs_pipe *pipe = pkt->pipe;
444 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
447 * change handler to PIO push
449 pkt->handler = &usbhs_fifo_pio_push_handler;
451 return pkt->handler->prepare(pkt, is_done);
454 struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
455 .prepare = usbhsf_dcp_data_stage_try_push,
459 * DCP data stage (pop)
461 static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
464 struct usbhs_pipe *pipe = pkt->pipe;
465 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
466 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
468 if (usbhs_pipe_is_busy(pipe))
472 * prepare pop for DCP should
473 * - change DCP direction,
477 usbhs_pipe_disable(pipe);
479 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
481 usbhsf_fifo_select(pipe, fifo, 0);
482 usbhsf_fifo_clear(pipe, fifo);
483 usbhsf_fifo_unselect(pipe, fifo);
486 * change handler to PIO pop
488 pkt->handler = &usbhs_fifo_pio_pop_handler;
490 return pkt->handler->prepare(pkt, is_done);
493 struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
494 .prepare = usbhsf_dcp_data_stage_prepare_pop,
500 static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
502 struct usbhs_pipe *pipe = pkt->pipe;
503 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
504 struct device *dev = usbhs_priv_to_dev(priv);
505 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
506 void __iomem *addr = priv->base + fifo->port;
508 int maxp = usbhs_pipe_get_maxpacket(pipe);
513 usbhs_pipe_data_sequence(pipe, pkt->sequence);
514 pkt->sequence = -1; /* -1 sequence will be ignored */
516 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
518 ret = usbhsf_fifo_select(pipe, fifo, 1);
522 ret = usbhs_pipe_is_accessible(pipe);
524 /* inaccessible pipe is not an error */
526 goto usbhs_fifo_write_busy;
529 ret = usbhsf_fifo_barrier(priv, fifo);
531 goto usbhs_fifo_write_busy;
533 buf = pkt->buf + pkt->actual;
534 len = pkt->length - pkt->actual;
535 len = min(len, maxp);
537 is_short = total_len < maxp;
544 if (len >= 4 && !((unsigned long)buf & 0x03)) {
545 iowrite32_rep(addr, buf, len / 4);
547 buf += total_len - len;
550 /* the rest operation */
551 for (i = 0; i < len; i++)
552 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
557 pkt->actual += total_len;
559 if (pkt->actual < pkt->length)
560 *is_done = 0; /* there are remainder data */
562 *is_done = 1; /* short packet */
564 *is_done = !pkt->zero; /* send zero packet ? */
570 usbhsf_send_terminator(pipe, fifo);
572 usbhsf_tx_irq_ctrl(pipe, !*is_done);
573 usbhs_pipe_running(pipe, !*is_done);
574 usbhs_pipe_enable(pipe);
576 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
577 usbhs_pipe_number(pipe),
578 pkt->length, pkt->actual, *is_done, pkt->zero);
580 usbhsf_fifo_unselect(pipe, fifo);
584 usbhs_fifo_write_busy:
585 usbhsf_fifo_unselect(pipe, fifo);
591 usbhsf_tx_irq_ctrl(pipe, 1);
592 usbhs_pipe_running(pipe, 1);
597 static int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done)
599 if (usbhs_pipe_is_running(pkt->pipe))
602 return usbhsf_pio_try_push(pkt, is_done);
605 struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
606 .prepare = usbhsf_pio_prepare_push,
607 .try_run = usbhsf_pio_try_push,
613 static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
615 struct usbhs_pipe *pipe = pkt->pipe;
617 if (usbhs_pipe_is_busy(pipe))
620 if (usbhs_pipe_is_running(pipe))
624 * pipe enable to prepare packet receive
626 usbhs_pipe_data_sequence(pipe, pkt->sequence);
627 pkt->sequence = -1; /* -1 sequence will be ignored */
629 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
630 usbhs_pipe_enable(pipe);
631 usbhs_pipe_running(pipe, 1);
632 usbhsf_rx_irq_ctrl(pipe, 1);
637 static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
639 struct usbhs_pipe *pipe = pkt->pipe;
640 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
641 struct device *dev = usbhs_priv_to_dev(priv);
642 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
643 void __iomem *addr = priv->base + fifo->port;
646 int maxp = usbhs_pipe_get_maxpacket(pipe);
651 ret = usbhsf_fifo_select(pipe, fifo, 0);
655 ret = usbhsf_fifo_barrier(priv, fifo);
657 goto usbhs_fifo_read_busy;
659 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
661 buf = pkt->buf + pkt->actual;
662 len = pkt->length - pkt->actual;
663 len = min(len, rcv_len);
667 * update actual length first here to decide disable pipe.
668 * if this pipe keeps BUF status and all data were popped,
669 * then, next interrupt/token will be issued again
671 pkt->actual += total_len;
673 if ((pkt->actual == pkt->length) || /* receive all data */
674 (total_len < maxp)) { /* short packet */
676 usbhsf_rx_irq_ctrl(pipe, 0);
677 usbhs_pipe_running(pipe, 0);
678 usbhs_pipe_disable(pipe); /* disable pipe first */
682 * Buffer clear if Zero-Length packet
685 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
689 usbhsf_fifo_clear(pipe, fifo);
690 goto usbhs_fifo_read_end;
698 if (len >= 4 && !((unsigned long)buf & 0x03)) {
699 ioread32_rep(addr, buf, len / 4);
701 buf += total_len - len;
704 /* the rest operation */
705 for (i = 0; i < len; i++) {
707 data = ioread32(addr);
709 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
713 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
714 usbhs_pipe_number(pipe),
715 pkt->length, pkt->actual, *is_done, pkt->zero);
717 usbhs_fifo_read_busy:
718 usbhsf_fifo_unselect(pipe, fifo);
723 struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
724 .prepare = usbhsf_prepare_pop,
725 .try_run = usbhsf_pio_try_pop,
729 * DCP ctrol statge handler
731 static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
733 usbhs_dcp_control_transfer_done(pkt->pipe);
740 struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
741 .prepare = usbhsf_ctrl_stage_end,
742 .try_run = usbhsf_ctrl_stage_end,
748 static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
749 struct usbhs_pkt *pkt)
751 if (&usbhs_fifo_dma_push_handler == pkt->handler)
752 return fifo->tx_chan;
754 if (&usbhs_fifo_dma_pop_handler == pkt->handler)
755 return fifo->rx_chan;
760 static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
761 struct usbhs_pkt *pkt)
763 struct usbhs_fifo *fifo;
766 fifo = usbhsf_get_d0fifo(priv);
767 if (usbhsf_dma_chan_get(fifo, pkt) &&
768 !usbhsf_fifo_is_busy(fifo))
772 fifo = usbhsf_get_d1fifo(priv);
773 if (usbhsf_dma_chan_get(fifo, pkt) &&
774 !usbhsf_fifo_is_busy(fifo))
780 #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
781 #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
782 static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
783 struct usbhs_fifo *fifo,
786 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
788 usbhs_bset(priv, fifo->sel, DREQE, dreqe);
791 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
793 struct usbhs_pipe *pipe = pkt->pipe;
794 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
795 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
797 return info->dma_map_ctrl(pkt, map);
800 static void usbhsf_dma_complete(void *arg);
801 static void xfer_work(struct work_struct *work)
803 struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
804 struct usbhs_pipe *pipe = pkt->pipe;
805 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
806 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
807 struct dma_async_tx_descriptor *desc;
808 struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
809 struct device *dev = usbhs_priv_to_dev(priv);
810 enum dma_transfer_direction dir;
812 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
814 desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
816 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
820 desc->callback = usbhsf_dma_complete;
821 desc->callback_param = pipe;
823 if (dmaengine_submit(desc) < 0) {
824 dev_err(dev, "Failed to submit dma descriptor\n");
828 dev_dbg(dev, " %s %d (%d/ %d)\n",
829 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
831 usbhs_pipe_running(pipe, 1);
832 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
833 usbhs_pipe_enable(pipe);
834 usbhsf_dma_start(pipe, fifo);
835 dma_async_issue_pending(chan);
841 static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
843 struct usbhs_pipe *pipe = pkt->pipe;
844 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
845 struct usbhs_fifo *fifo;
846 int len = pkt->length - pkt->actual;
849 if (usbhs_pipe_is_busy(pipe))
852 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
853 if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
854 usbhs_pipe_is_dcp(pipe))
855 goto usbhsf_pio_prepare_push;
857 if (len & 0x7) /* 8byte alignment */
858 goto usbhsf_pio_prepare_push;
860 if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
861 goto usbhsf_pio_prepare_push;
863 /* return at this time if the pipe is running */
864 if (usbhs_pipe_is_running(pipe))
867 /* get enable DMA fifo */
868 fifo = usbhsf_get_dma_fifo(priv, pkt);
870 goto usbhsf_pio_prepare_push;
872 if (usbhsf_dma_map(pkt) < 0)
873 goto usbhsf_pio_prepare_push;
875 ret = usbhsf_fifo_select(pipe, fifo, 0);
877 goto usbhsf_pio_prepare_push_unmap;
881 INIT_WORK(&pkt->work, xfer_work);
882 schedule_work(&pkt->work);
886 usbhsf_pio_prepare_push_unmap:
887 usbhsf_dma_unmap(pkt);
888 usbhsf_pio_prepare_push:
890 * change handler to PIO
892 pkt->handler = &usbhs_fifo_pio_push_handler;
894 return pkt->handler->prepare(pkt, is_done);
897 static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
899 struct usbhs_pipe *pipe = pkt->pipe;
900 int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
902 pkt->actual += pkt->trans;
904 if (pkt->actual < pkt->length)
905 *is_done = 0; /* there are remainder data */
907 *is_done = 1; /* short packet */
909 *is_done = !pkt->zero; /* send zero packet? */
911 usbhs_pipe_running(pipe, !*is_done);
913 usbhsf_dma_stop(pipe, pipe->fifo);
914 usbhsf_dma_unmap(pkt);
915 usbhsf_fifo_unselect(pipe, pipe->fifo);
918 /* change handler to PIO */
919 pkt->handler = &usbhs_fifo_pio_push_handler;
920 return pkt->handler->try_run(pkt, is_done);
926 struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
927 .prepare = usbhsf_dma_prepare_push,
928 .dma_done = usbhsf_dma_push_done,
934 static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
936 struct usbhs_pipe *pipe = pkt->pipe;
937 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
938 struct usbhs_fifo *fifo;
941 if (usbhs_pipe_is_busy(pipe))
944 if (usbhs_pipe_is_dcp(pipe))
945 goto usbhsf_pio_prepare_pop;
947 /* get enable DMA fifo */
948 fifo = usbhsf_get_dma_fifo(priv, pkt);
950 goto usbhsf_pio_prepare_pop;
952 if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
953 goto usbhsf_pio_prepare_pop;
955 ret = usbhsf_fifo_select(pipe, fifo, 0);
957 goto usbhsf_pio_prepare_pop;
959 /* use PIO if packet is less than pio_dma_border */
960 len = usbhsf_fifo_rcv_len(priv, fifo);
961 len = min(pkt->length - pkt->actual, len);
962 if (len & 0x7) /* 8byte alignment */
963 goto usbhsf_pio_prepare_pop_unselect;
965 if (len < usbhs_get_dparam(priv, pio_dma_border))
966 goto usbhsf_pio_prepare_pop_unselect;
968 ret = usbhsf_fifo_barrier(priv, fifo);
970 goto usbhsf_pio_prepare_pop_unselect;
972 if (usbhsf_dma_map(pkt) < 0)
973 goto usbhsf_pio_prepare_pop_unselect;
978 * usbhs_fifo_dma_pop_handler :: prepare
979 * enabled irq to come here.
980 * but it is no longer needed for DMA. disable it.
982 usbhsf_rx_irq_ctrl(pipe, 0);
986 INIT_WORK(&pkt->work, xfer_work);
987 schedule_work(&pkt->work);
991 usbhsf_pio_prepare_pop_unselect:
992 usbhsf_fifo_unselect(pipe, fifo);
993 usbhsf_pio_prepare_pop:
996 * change handler to PIO
998 pkt->handler = &usbhs_fifo_pio_pop_handler;
1000 return pkt->handler->try_run(pkt, is_done);
1003 static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
1005 struct usbhs_pipe *pipe = pkt->pipe;
1006 int maxp = usbhs_pipe_get_maxpacket(pipe);
1008 usbhsf_dma_stop(pipe, pipe->fifo);
1009 usbhsf_dma_unmap(pkt);
1010 usbhsf_fifo_unselect(pipe, pipe->fifo);
1012 pkt->actual += pkt->trans;
1014 if ((pkt->actual == pkt->length) || /* receive all data */
1015 (pkt->trans < maxp)) { /* short packet */
1017 usbhs_pipe_running(pipe, 0);
1020 usbhs_pipe_running(pipe, 0);
1021 usbhsf_prepare_pop(pkt, is_done);
1027 struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
1028 .prepare = usbhsf_prepare_pop,
1029 .try_run = usbhsf_dma_try_pop,
1030 .dma_done = usbhsf_dma_pop_done
1036 static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
1038 struct sh_dmae_slave *slave = param;
1043 * usbhs doesn't recognize id = 0 as valid DMA
1045 if (0 == slave->shdma_slave.slave_id)
1048 chan->private = slave;
1053 static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
1056 dma_release_channel(fifo->tx_chan);
1058 dma_release_channel(fifo->rx_chan);
1060 fifo->tx_chan = NULL;
1061 fifo->rx_chan = NULL;
1064 static void usbhsf_dma_init(struct usbhs_priv *priv,
1065 struct usbhs_fifo *fifo)
1067 struct device *dev = usbhs_priv_to_dev(priv);
1068 dma_cap_mask_t mask;
1071 dma_cap_set(DMA_SLAVE, mask);
1072 fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1076 dma_cap_set(DMA_SLAVE, mask);
1077 fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1080 if (fifo->tx_chan || fifo->rx_chan)
1081 dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
1083 fifo->tx_chan ? "[TX]" : " ",
1084 fifo->rx_chan ? "[RX]" : " ");
1090 static int usbhsf_irq_empty(struct usbhs_priv *priv,
1091 struct usbhs_irq_state *irq_state)
1093 struct usbhs_pipe *pipe;
1094 struct device *dev = usbhs_priv_to_dev(priv);
1097 if (!irq_state->bempsts) {
1098 dev_err(dev, "debug %s !!\n", __func__);
1102 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
1105 * search interrupted "pipe"
1108 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1109 if (!(irq_state->bempsts & (1 << i)))
1112 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1114 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
1120 static int usbhsf_irq_ready(struct usbhs_priv *priv,
1121 struct usbhs_irq_state *irq_state)
1123 struct usbhs_pipe *pipe;
1124 struct device *dev = usbhs_priv_to_dev(priv);
1127 if (!irq_state->brdysts) {
1128 dev_err(dev, "debug %s !!\n", __func__);
1132 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
1135 * search interrupted "pipe"
1138 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1139 if (!(irq_state->brdysts & (1 << i)))
1142 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1144 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
1150 static void usbhsf_dma_complete(void *arg)
1152 struct usbhs_pipe *pipe = arg;
1153 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1154 struct device *dev = usbhs_priv_to_dev(priv);
1157 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
1159 dev_err(dev, "dma_complete run_error %d : %d\n",
1160 usbhs_pipe_number(pipe), ret);
1163 void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
1165 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1166 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
1168 /* clear DCP FIFO of transmission */
1169 if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
1171 usbhsf_fifo_clear(pipe, fifo);
1172 usbhsf_fifo_unselect(pipe, fifo);
1174 /* clear DCP FIFO of reception */
1175 if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
1177 usbhsf_fifo_clear(pipe, fifo);
1178 usbhsf_fifo_unselect(pipe, fifo);
1184 void usbhs_fifo_init(struct usbhs_priv *priv)
1186 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1187 struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
1188 struct usbhs_fifo *d0fifo = usbhsf_get_d0fifo(priv);
1189 struct usbhs_fifo *d1fifo = usbhsf_get_d1fifo(priv);
1191 mod->irq_empty = usbhsf_irq_empty;
1192 mod->irq_ready = usbhsf_irq_ready;
1193 mod->irq_bempsts = 0;
1194 mod->irq_brdysts = 0;
1197 d0fifo->pipe = NULL;
1198 d1fifo->pipe = NULL;
1201 void usbhs_fifo_quit(struct usbhs_priv *priv)
1203 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1205 mod->irq_empty = NULL;
1206 mod->irq_ready = NULL;
1207 mod->irq_bempsts = 0;
1208 mod->irq_brdysts = 0;
1211 int usbhs_fifo_probe(struct usbhs_priv *priv)
1213 struct usbhs_fifo *fifo;
1216 fifo = usbhsf_get_cfifo(priv);
1217 fifo->name = "CFIFO";
1219 fifo->sel = CFIFOSEL;
1220 fifo->ctr = CFIFOCTR;
1223 fifo = usbhsf_get_d0fifo(priv);
1224 fifo->name = "D0FIFO";
1225 fifo->port = D0FIFO;
1226 fifo->sel = D0FIFOSEL;
1227 fifo->ctr = D0FIFOCTR;
1228 fifo->tx_slave.shdma_slave.slave_id = usbhs_get_dparam(priv, d0_tx_id);
1229 fifo->rx_slave.shdma_slave.slave_id = usbhs_get_dparam(priv, d0_rx_id);
1230 usbhsf_dma_init(priv, fifo);
1233 fifo = usbhsf_get_d1fifo(priv);
1234 fifo->name = "D1FIFO";
1235 fifo->port = D1FIFO;
1236 fifo->sel = D1FIFOSEL;
1237 fifo->ctr = D1FIFOCTR;
1238 fifo->tx_slave.shdma_slave.slave_id = usbhs_get_dparam(priv, d1_tx_id);
1239 fifo->rx_slave.shdma_slave.slave_id = usbhs_get_dparam(priv, d1_rx_id);
1240 usbhsf_dma_init(priv, fifo);
1245 void usbhs_fifo_remove(struct usbhs_priv *priv)
1247 usbhsf_dma_quit(priv, usbhsf_get_d0fifo(priv));
1248 usbhsf_dma_quit(priv, usbhsf_get_d1fifo(priv));