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))
27 #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
32 void usbhs_pkt_init(struct usbhs_pkt
*pkt
)
34 pkt
->dma
= DMA_ADDR_INVALID
;
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
)
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_del_init(&pkt
->node
);
79 list_add_tail(&pkt
->node
, &pipe
->list
);
82 * each pkt must hold own handler.
83 * because handler might be changed by its situation.
84 * dma handler -> pio handler.
88 pkt
->handler
= pipe
->handler
;
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_entry(pipe
->list
.next
, struct usbhs_pkt
, node
);
111 struct usbhs_pkt
*usbhs_pkt_pop(struct usbhs_pipe
*pipe
, struct usbhs_pkt
*pkt
)
113 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
116 /******************** spin lock ********************/
117 usbhs_lock(priv
, flags
);
120 pkt
= __usbhsf_pkt_get(pipe
);
123 __usbhsf_pkt_del(pkt
);
125 usbhs_unlock(priv
, flags
);
126 /******************** spin unlock ******************/
137 static int usbhsf_pkt_handler(struct usbhs_pipe
*pipe
, int type
)
139 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
140 struct usbhs_pkt
*pkt
;
141 struct device
*dev
= usbhs_priv_to_dev(priv
);
142 int (*func
)(struct usbhs_pkt
*pkt
, int *is_done
);
147 /******************** spin lock ********************/
148 usbhs_lock(priv
, flags
);
150 pkt
= __usbhsf_pkt_get(pipe
);
152 goto __usbhs_pkt_handler_end
;
155 case USBHSF_PKT_PREPARE
:
156 func
= pkt
->handler
->prepare
;
158 case USBHSF_PKT_TRY_RUN
:
159 func
= pkt
->handler
->try_run
;
161 case USBHSF_PKT_DMA_DONE
:
162 func
= pkt
->handler
->dma_done
;
165 dev_err(dev
, "unknown pkt hander\n");
166 goto __usbhs_pkt_handler_end
;
169 ret
= func(pkt
, &is_done
);
172 __usbhsf_pkt_del(pkt
);
174 __usbhs_pkt_handler_end
:
175 usbhs_unlock(priv
, flags
);
176 /******************** spin unlock ******************/
179 pkt
->done(priv
, pkt
);
180 usbhs_pkt_start(pipe
);
186 void usbhs_pkt_start(struct usbhs_pipe
*pipe
)
188 usbhsf_pkt_handler(pipe
, USBHSF_PKT_PREPARE
);
192 * irq enable/disable function
194 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
195 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
196 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
198 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
199 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
200 u16 status = (1 << usbhs_pipe_number(pipe)); \
204 mod->irq_##status |= status; \
206 mod->irq_##status &= ~status; \
207 usbhs_irq_callback_update(priv, mod); \
210 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe
*pipe
, int enable
)
213 * And DCP pipe can NOT use "ready interrupt" for "send"
214 * it should use "empty" interrupt.
216 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
218 * on the other hand, normal pipe can use "ready interrupt" for "send"
219 * even though it is single/double buffer
221 if (usbhs_pipe_is_dcp(pipe
))
222 usbhsf_irq_empty_ctrl(pipe
, enable
);
224 usbhsf_irq_ready_ctrl(pipe
, enable
);
227 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe
*pipe
, int enable
)
229 usbhsf_irq_ready_ctrl(pipe
, enable
);
235 static void usbhsf_send_terminator(struct usbhs_pipe
*pipe
,
236 struct usbhs_fifo
*fifo
)
238 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
240 usbhs_bset(priv
, fifo
->ctr
, BVAL
, BVAL
);
243 static int usbhsf_fifo_barrier(struct usbhs_priv
*priv
,
244 struct usbhs_fifo
*fifo
)
249 /* The FIFO port is accessible */
250 if (usbhs_read(priv
, fifo
->ctr
) & FRDY
)
259 static void usbhsf_fifo_clear(struct usbhs_pipe
*pipe
,
260 struct usbhs_fifo
*fifo
)
262 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
264 if (!usbhs_pipe_is_dcp(pipe
))
265 usbhsf_fifo_barrier(priv
, fifo
);
267 usbhs_write(priv
, fifo
->ctr
, BCLR
);
270 static int usbhsf_fifo_rcv_len(struct usbhs_priv
*priv
,
271 struct usbhs_fifo
*fifo
)
273 return usbhs_read(priv
, fifo
->ctr
) & DTLN_MASK
;
276 static void usbhsf_fifo_unselect(struct usbhs_pipe
*pipe
,
277 struct usbhs_fifo
*fifo
)
279 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
281 usbhs_pipe_select_fifo(pipe
, NULL
);
282 usbhs_write(priv
, fifo
->sel
, 0);
285 static int usbhsf_fifo_select(struct usbhs_pipe
*pipe
,
286 struct usbhs_fifo
*fifo
,
289 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
290 struct device
*dev
= usbhs_priv_to_dev(priv
);
292 u16 mask
= ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
293 u16 base
= usbhs_pipe_number(pipe
); /* CURPIPE */
295 if (usbhs_pipe_is_busy(pipe
) ||
296 usbhsf_fifo_is_busy(fifo
))
299 if (usbhs_pipe_is_dcp(pipe
)) {
300 base
|= (1 == write
) << 5; /* ISEL */
302 if (usbhs_mod_is_host(priv
))
303 usbhs_dcp_dir_for_host(pipe
, write
);
306 /* "base" will be used below */
307 usbhs_write(priv
, fifo
->sel
, base
| MBW_32
);
309 /* check ISEL and CURPIPE value */
311 if (base
== (mask
& usbhs_read(priv
, fifo
->sel
))) {
312 usbhs_pipe_select_fifo(pipe
, fifo
);
318 dev_err(dev
, "fifo select error\n");
326 static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt
*pkt
, int *is_done
)
328 struct usbhs_pipe
*pipe
= pkt
->pipe
;
329 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
330 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
331 struct device
*dev
= usbhs_priv_to_dev(priv
);
334 usbhs_pipe_disable(pipe
);
336 ret
= usbhsf_fifo_select(pipe
, fifo
, 1);
338 dev_err(dev
, "%s() faile\n", __func__
);
342 usbhs_pipe_sequence_data1(pipe
); /* DATA1 */
344 usbhsf_fifo_clear(pipe
, fifo
);
345 usbhsf_send_terminator(pipe
, fifo
);
347 usbhsf_fifo_unselect(pipe
, fifo
);
349 usbhsf_tx_irq_ctrl(pipe
, 1);
350 usbhs_pipe_enable(pipe
);
355 static int usbhs_dcp_dir_switch_to_read(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
, 0);
367 dev_err(dev
, "%s() fail\n", __func__
);
371 usbhs_pipe_sequence_data1(pipe
); /* DATA1 */
372 usbhsf_fifo_clear(pipe
, fifo
);
374 usbhsf_fifo_unselect(pipe
, fifo
);
376 usbhsf_rx_irq_ctrl(pipe
, 1);
377 usbhs_pipe_enable(pipe
);
383 static int usbhs_dcp_dir_switch_done(struct usbhs_pkt
*pkt
, int *is_done
)
385 struct usbhs_pipe
*pipe
= pkt
->pipe
;
387 if (pkt
->handler
== &usbhs_dcp_status_stage_in_handler
)
388 usbhsf_tx_irq_ctrl(pipe
, 0);
390 usbhsf_rx_irq_ctrl(pipe
, 0);
392 pkt
->actual
= pkt
->length
;
398 struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler
= {
399 .prepare
= usbhs_dcp_dir_switch_to_write
,
400 .try_run
= usbhs_dcp_dir_switch_done
,
403 struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler
= {
404 .prepare
= usbhs_dcp_dir_switch_to_read
,
405 .try_run
= usbhs_dcp_dir_switch_done
,
409 * DCP data stage (push)
411 static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt
*pkt
, int *is_done
)
413 struct usbhs_pipe
*pipe
= pkt
->pipe
;
415 usbhs_pipe_sequence_data1(pipe
); /* DATA1 */
418 * change handler to PIO push
420 pkt
->handler
= &usbhs_fifo_pio_push_handler
;
422 return pkt
->handler
->prepare(pkt
, is_done
);
425 struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler
= {
426 .prepare
= usbhsf_dcp_data_stage_try_push
,
430 * DCP data stage (pop)
432 static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt
*pkt
,
435 struct usbhs_pipe
*pipe
= pkt
->pipe
;
436 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
437 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
);
439 if (usbhs_pipe_is_busy(pipe
))
443 * prepare pop for DCP should
444 * - change DCP direction,
448 usbhs_pipe_disable(pipe
);
450 usbhs_pipe_sequence_data1(pipe
); /* DATA1 */
452 usbhsf_fifo_select(pipe
, fifo
, 0);
453 usbhsf_fifo_clear(pipe
, fifo
);
454 usbhsf_fifo_unselect(pipe
, fifo
);
457 * change handler to PIO pop
459 pkt
->handler
= &usbhs_fifo_pio_pop_handler
;
461 return pkt
->handler
->prepare(pkt
, is_done
);
464 struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler
= {
465 .prepare
= usbhsf_dcp_data_stage_prepare_pop
,
471 static int usbhsf_pio_try_push(struct usbhs_pkt
*pkt
, int *is_done
)
473 struct usbhs_pipe
*pipe
= pkt
->pipe
;
474 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
475 struct device
*dev
= usbhs_priv_to_dev(priv
);
476 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
477 void __iomem
*addr
= priv
->base
+ fifo
->port
;
479 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
484 ret
= usbhsf_fifo_select(pipe
, fifo
, 1);
488 ret
= usbhs_pipe_is_accessible(pipe
);
490 /* inaccessible pipe is not an error */
492 goto usbhs_fifo_write_busy
;
495 ret
= usbhsf_fifo_barrier(priv
, fifo
);
497 goto usbhs_fifo_write_busy
;
499 buf
= pkt
->buf
+ pkt
->actual
;
500 len
= pkt
->length
- pkt
->actual
;
501 len
= min(len
, maxp
);
503 is_short
= total_len
< maxp
;
510 if (len
>= 4 && !((unsigned long)buf
& 0x03)) {
511 iowrite32_rep(addr
, buf
, len
/ 4);
513 buf
+= total_len
- len
;
516 /* the rest operation */
517 for (i
= 0; i
< len
; i
++)
518 iowrite8(buf
[i
], addr
+ (0x03 - (i
& 0x03)));
523 pkt
->actual
+= total_len
;
525 if (pkt
->actual
< pkt
->length
)
526 *is_done
= 0; /* there are remainder data */
528 *is_done
= 1; /* short packet */
530 *is_done
= !pkt
->zero
; /* send zero packet ? */
536 usbhsf_send_terminator(pipe
, fifo
);
538 usbhsf_tx_irq_ctrl(pipe
, !*is_done
);
539 usbhs_pipe_enable(pipe
);
541 dev_dbg(dev
, " send %d (%d/ %d/ %d/ %d)\n",
542 usbhs_pipe_number(pipe
),
543 pkt
->length
, pkt
->actual
, *is_done
, pkt
->zero
);
549 if (usbhs_pipe_is_dcp(pipe
))
550 usbhs_dcp_control_transfer_done(pipe
);
553 usbhsf_fifo_unselect(pipe
, fifo
);
557 usbhs_fifo_write_busy
:
558 usbhsf_fifo_unselect(pipe
, fifo
);
564 usbhsf_tx_irq_ctrl(pipe
, 1);
569 struct usbhs_pkt_handle usbhs_fifo_pio_push_handler
= {
570 .prepare
= usbhsf_pio_try_push
,
571 .try_run
= usbhsf_pio_try_push
,
577 static int usbhsf_prepare_pop(struct usbhs_pkt
*pkt
, int *is_done
)
579 struct usbhs_pipe
*pipe
= pkt
->pipe
;
581 if (usbhs_pipe_is_busy(pipe
))
585 * pipe enable to prepare packet receive
588 usbhs_pipe_enable(pipe
);
589 usbhsf_rx_irq_ctrl(pipe
, 1);
594 static int usbhsf_pio_try_pop(struct usbhs_pkt
*pkt
, int *is_done
)
596 struct usbhs_pipe
*pipe
= pkt
->pipe
;
597 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
598 struct device
*dev
= usbhs_priv_to_dev(priv
);
599 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
600 void __iomem
*addr
= priv
->base
+ fifo
->port
;
603 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
608 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
612 ret
= usbhsf_fifo_barrier(priv
, fifo
);
614 goto usbhs_fifo_read_busy
;
616 rcv_len
= usbhsf_fifo_rcv_len(priv
, fifo
);
618 buf
= pkt
->buf
+ pkt
->actual
;
619 len
= pkt
->length
- pkt
->actual
;
620 len
= min(len
, rcv_len
);
624 * update actual length first here to decide disable pipe.
625 * if this pipe keeps BUF status and all data were popped,
626 * then, next interrupt/token will be issued again
628 pkt
->actual
+= total_len
;
630 if ((pkt
->actual
== pkt
->length
) || /* receive all data */
631 (total_len
< maxp
)) { /* short packet */
633 usbhsf_rx_irq_ctrl(pipe
, 0);
634 usbhs_pipe_disable(pipe
); /* disable pipe first */
638 * Buffer clear if Zero-Length packet
641 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
644 usbhsf_fifo_clear(pipe
, fifo
);
645 goto usbhs_fifo_read_end
;
653 if (len
>= 4 && !((unsigned long)buf
& 0x03)) {
654 ioread32_rep(addr
, buf
, len
/ 4);
656 buf
+= total_len
- len
;
659 /* the rest operation */
660 for (i
= 0; i
< len
; i
++) {
662 data
= ioread32(addr
);
664 buf
[i
] = (data
>> ((i
& 0x03) * 8)) & 0xff;
668 dev_dbg(dev
, " recv %d (%d/ %d/ %d/ %d)\n",
669 usbhs_pipe_number(pipe
),
670 pkt
->length
, pkt
->actual
, *is_done
, pkt
->zero
);
672 usbhs_fifo_read_busy
:
673 usbhsf_fifo_unselect(pipe
, fifo
);
678 struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler
= {
679 .prepare
= usbhsf_prepare_pop
,
680 .try_run
= usbhsf_pio_try_pop
,
684 * DCP ctrol statge handler
686 static int usbhsf_ctrl_stage_end(struct usbhs_pkt
*pkt
, int *is_done
)
688 usbhs_dcp_control_transfer_done(pkt
->pipe
);
695 struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler
= {
696 .prepare
= usbhsf_ctrl_stage_end
,
697 .try_run
= usbhsf_ctrl_stage_end
,
703 static struct dma_chan
*usbhsf_dma_chan_get(struct usbhs_fifo
*fifo
,
704 struct usbhs_pkt
*pkt
)
706 if (&usbhs_fifo_dma_push_handler
== pkt
->handler
)
707 return fifo
->tx_chan
;
709 if (&usbhs_fifo_dma_pop_handler
== pkt
->handler
)
710 return fifo
->rx_chan
;
715 static struct usbhs_fifo
*usbhsf_get_dma_fifo(struct usbhs_priv
*priv
,
716 struct usbhs_pkt
*pkt
)
718 struct usbhs_fifo
*fifo
;
721 fifo
= usbhsf_get_d0fifo(priv
);
722 if (usbhsf_dma_chan_get(fifo
, pkt
) &&
723 !usbhsf_fifo_is_busy(fifo
))
727 fifo
= usbhsf_get_d1fifo(priv
);
728 if (usbhsf_dma_chan_get(fifo
, pkt
) &&
729 !usbhsf_fifo_is_busy(fifo
))
735 #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
736 #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
737 static void __usbhsf_dma_ctrl(struct usbhs_pipe
*pipe
,
738 struct usbhs_fifo
*fifo
,
741 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
743 usbhs_bset(priv
, fifo
->sel
, DREQE
, dreqe
);
746 #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
747 #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
748 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt
*pkt
, int map
)
750 struct usbhs_pipe
*pipe
= pkt
->pipe
;
751 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
752 struct usbhs_pipe_info
*info
= usbhs_priv_to_pipeinfo(priv
);
754 return info
->dma_map_ctrl(pkt
, map
);
757 static void usbhsf_dma_complete(void *arg
);
758 static void usbhsf_dma_prepare_tasklet(unsigned long data
)
760 struct usbhs_pkt
*pkt
= (struct usbhs_pkt
*)data
;
761 struct usbhs_pipe
*pipe
= pkt
->pipe
;
762 struct usbhs_fifo
*fifo
= usbhs_pipe_to_fifo(pipe
);
763 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
764 struct scatterlist sg
;
765 struct dma_async_tx_descriptor
*desc
;
766 struct dma_chan
*chan
= usbhsf_dma_chan_get(fifo
, pkt
);
767 struct device
*dev
= usbhs_priv_to_dev(priv
);
768 enum dma_data_direction dir
;
771 dir
= usbhs_pipe_is_dir_in(pipe
) ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
773 sg_init_table(&sg
, 1);
774 sg_set_page(&sg
, virt_to_page(pkt
->dma
),
775 pkt
->length
, offset_in_page(pkt
->dma
));
776 sg_dma_address(&sg
) = pkt
->dma
+ pkt
->actual
;
777 sg_dma_len(&sg
) = pkt
->trans
;
779 desc
= chan
->device
->device_prep_slave_sg(chan
, &sg
, 1, dir
,
785 desc
->callback
= usbhsf_dma_complete
;
786 desc
->callback_param
= pipe
;
788 cookie
= desc
->tx_submit(desc
);
790 dev_err(dev
, "Failed to submit dma descriptor\n");
794 dev_dbg(dev
, " %s %d (%d/ %d)\n",
795 fifo
->name
, usbhs_pipe_number(pipe
), pkt
->length
, pkt
->zero
);
797 usbhsf_dma_start(pipe
, fifo
);
798 dma_async_issue_pending(chan
);
804 static int usbhsf_dma_prepare_push(struct usbhs_pkt
*pkt
, int *is_done
)
806 struct usbhs_pipe
*pipe
= pkt
->pipe
;
807 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
808 struct usbhs_fifo
*fifo
;
809 int len
= pkt
->length
- pkt
->actual
;
812 if (usbhs_pipe_is_busy(pipe
))
815 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
816 if ((len
< usbhs_get_dparam(priv
, pio_dma_border
)) ||
817 usbhs_pipe_is_dcp(pipe
))
818 goto usbhsf_pio_prepare_push
;
820 if (len
% 4) /* 32bit alignment */
821 goto usbhsf_pio_prepare_push
;
823 if ((*(u32
*) pkt
->buf
+ pkt
->actual
) & 0x7) /* 8byte alignment */
824 goto usbhsf_pio_prepare_push
;
826 /* get enable DMA fifo */
827 fifo
= usbhsf_get_dma_fifo(priv
, pkt
);
829 goto usbhsf_pio_prepare_push
;
831 if (usbhsf_dma_map(pkt
) < 0)
832 goto usbhsf_pio_prepare_push
;
834 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
836 goto usbhsf_pio_prepare_push_unmap
;
840 tasklet_init(&fifo
->tasklet
,
841 usbhsf_dma_prepare_tasklet
,
844 tasklet_schedule(&fifo
->tasklet
);
848 usbhsf_pio_prepare_push_unmap
:
849 usbhsf_dma_unmap(pkt
);
850 usbhsf_pio_prepare_push
:
852 * change handler to PIO
854 pkt
->handler
= &usbhs_fifo_pio_push_handler
;
856 return pkt
->handler
->prepare(pkt
, is_done
);
859 static int usbhsf_dma_push_done(struct usbhs_pkt
*pkt
, int *is_done
)
861 struct usbhs_pipe
*pipe
= pkt
->pipe
;
863 pkt
->actual
= pkt
->trans
;
865 *is_done
= !pkt
->zero
; /* send zero packet ? */
867 usbhsf_dma_stop(pipe
, pipe
->fifo
);
868 usbhsf_dma_unmap(pkt
);
869 usbhsf_fifo_unselect(pipe
, pipe
->fifo
);
874 struct usbhs_pkt_handle usbhs_fifo_dma_push_handler
= {
875 .prepare
= usbhsf_dma_prepare_push
,
876 .dma_done
= usbhsf_dma_push_done
,
882 static int usbhsf_dma_try_pop(struct usbhs_pkt
*pkt
, int *is_done
)
884 struct usbhs_pipe
*pipe
= pkt
->pipe
;
885 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
886 struct usbhs_fifo
*fifo
;
889 if (usbhs_pipe_is_busy(pipe
))
892 if (usbhs_pipe_is_dcp(pipe
))
893 goto usbhsf_pio_prepare_pop
;
895 /* get enable DMA fifo */
896 fifo
= usbhsf_get_dma_fifo(priv
, pkt
);
898 goto usbhsf_pio_prepare_pop
;
900 if ((*(u32
*) pkt
->buf
+ pkt
->actual
) & 0x7) /* 8byte alignment */
901 goto usbhsf_pio_prepare_pop
;
903 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
905 goto usbhsf_pio_prepare_pop
;
907 /* use PIO if packet is less than pio_dma_border */
908 len
= usbhsf_fifo_rcv_len(priv
, fifo
);
909 len
= min(pkt
->length
- pkt
->actual
, len
);
910 if (len
% 4) /* 32bit alignment */
911 goto usbhsf_pio_prepare_pop_unselect
;
913 if (len
< usbhs_get_dparam(priv
, pio_dma_border
))
914 goto usbhsf_pio_prepare_pop_unselect
;
916 ret
= usbhsf_fifo_barrier(priv
, fifo
);
918 goto usbhsf_pio_prepare_pop_unselect
;
920 if (usbhsf_dma_map(pkt
) < 0)
921 goto usbhsf_pio_prepare_pop_unselect
;
926 * usbhs_fifo_dma_pop_handler :: prepare
927 * enabled irq to come here.
928 * but it is no longer needed for DMA. disable it.
930 usbhsf_rx_irq_ctrl(pipe
, 0);
934 tasklet_init(&fifo
->tasklet
,
935 usbhsf_dma_prepare_tasklet
,
938 tasklet_schedule(&fifo
->tasklet
);
942 usbhsf_pio_prepare_pop_unselect
:
943 usbhsf_fifo_unselect(pipe
, fifo
);
944 usbhsf_pio_prepare_pop
:
947 * change handler to PIO
949 pkt
->handler
= &usbhs_fifo_pio_pop_handler
;
951 return pkt
->handler
->try_run(pkt
, is_done
);
954 static int usbhsf_dma_pop_done(struct usbhs_pkt
*pkt
, int *is_done
)
956 struct usbhs_pipe
*pipe
= pkt
->pipe
;
957 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
959 usbhsf_dma_stop(pipe
, pipe
->fifo
);
960 usbhsf_dma_unmap(pkt
);
961 usbhsf_fifo_unselect(pipe
, pipe
->fifo
);
963 pkt
->actual
+= pkt
->trans
;
965 if ((pkt
->actual
== pkt
->length
) || /* receive all data */
966 (pkt
->trans
< maxp
)) { /* short packet */
970 usbhsf_prepare_pop(pkt
, is_done
);
976 struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler
= {
977 .prepare
= usbhsf_prepare_pop
,
978 .try_run
= usbhsf_dma_try_pop
,
979 .dma_done
= usbhsf_dma_pop_done
985 static bool usbhsf_dma_filter(struct dma_chan
*chan
, void *param
)
987 struct sh_dmae_slave
*slave
= param
;
992 * usbhs doesn't recognize id = 0 as valid DMA
994 if (0 == slave
->slave_id
)
997 chan
->private = slave
;
1002 static void usbhsf_dma_quit(struct usbhs_priv
*priv
, struct usbhs_fifo
*fifo
)
1005 dma_release_channel(fifo
->tx_chan
);
1007 dma_release_channel(fifo
->rx_chan
);
1009 fifo
->tx_chan
= NULL
;
1010 fifo
->rx_chan
= NULL
;
1013 static void usbhsf_dma_init(struct usbhs_priv
*priv
,
1014 struct usbhs_fifo
*fifo
)
1016 struct device
*dev
= usbhs_priv_to_dev(priv
);
1017 dma_cap_mask_t mask
;
1020 dma_cap_set(DMA_SLAVE
, mask
);
1021 fifo
->tx_chan
= dma_request_channel(mask
, usbhsf_dma_filter
,
1025 dma_cap_set(DMA_SLAVE
, mask
);
1026 fifo
->rx_chan
= dma_request_channel(mask
, usbhsf_dma_filter
,
1029 if (fifo
->tx_chan
|| fifo
->rx_chan
)
1030 dev_dbg(dev
, "enable DMAEngine (%s%s%s)\n",
1032 fifo
->tx_chan
? "[TX]" : " ",
1033 fifo
->rx_chan
? "[RX]" : " ");
1039 static int usbhsf_irq_empty(struct usbhs_priv
*priv
,
1040 struct usbhs_irq_state
*irq_state
)
1042 struct usbhs_pipe
*pipe
;
1043 struct device
*dev
= usbhs_priv_to_dev(priv
);
1046 if (!irq_state
->bempsts
) {
1047 dev_err(dev
, "debug %s !!\n", __func__
);
1051 dev_dbg(dev
, "irq empty [0x%04x]\n", irq_state
->bempsts
);
1054 * search interrupted "pipe"
1057 usbhs_for_each_pipe_with_dcp(pipe
, priv
, i
) {
1058 if (!(irq_state
->bempsts
& (1 << i
)))
1061 ret
= usbhsf_pkt_handler(pipe
, USBHSF_PKT_TRY_RUN
);
1063 dev_err(dev
, "irq_empty run_error %d : %d\n", i
, ret
);
1069 static int usbhsf_irq_ready(struct usbhs_priv
*priv
,
1070 struct usbhs_irq_state
*irq_state
)
1072 struct usbhs_pipe
*pipe
;
1073 struct device
*dev
= usbhs_priv_to_dev(priv
);
1076 if (!irq_state
->brdysts
) {
1077 dev_err(dev
, "debug %s !!\n", __func__
);
1081 dev_dbg(dev
, "irq ready [0x%04x]\n", irq_state
->brdysts
);
1084 * search interrupted "pipe"
1087 usbhs_for_each_pipe_with_dcp(pipe
, priv
, i
) {
1088 if (!(irq_state
->brdysts
& (1 << i
)))
1091 ret
= usbhsf_pkt_handler(pipe
, USBHSF_PKT_TRY_RUN
);
1093 dev_err(dev
, "irq_ready run_error %d : %d\n", i
, ret
);
1099 static void usbhsf_dma_complete(void *arg
)
1101 struct usbhs_pipe
*pipe
= arg
;
1102 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
1103 struct device
*dev
= usbhs_priv_to_dev(priv
);
1106 ret
= usbhsf_pkt_handler(pipe
, USBHSF_PKT_DMA_DONE
);
1108 dev_err(dev
, "dma_complete run_error %d : %d\n",
1109 usbhs_pipe_number(pipe
), ret
);
1115 void usbhs_fifo_init(struct usbhs_priv
*priv
)
1117 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
1118 struct usbhs_fifo
*cfifo
= usbhsf_get_cfifo(priv
);
1119 struct usbhs_fifo
*d0fifo
= usbhsf_get_d0fifo(priv
);
1120 struct usbhs_fifo
*d1fifo
= usbhsf_get_d1fifo(priv
);
1122 mod
->irq_empty
= usbhsf_irq_empty
;
1123 mod
->irq_ready
= usbhsf_irq_ready
;
1124 mod
->irq_bempsts
= 0;
1125 mod
->irq_brdysts
= 0;
1128 cfifo
->tx_chan
= NULL
;
1129 cfifo
->rx_chan
= NULL
;
1131 d0fifo
->pipe
= NULL
;
1132 d0fifo
->tx_chan
= NULL
;
1133 d0fifo
->rx_chan
= NULL
;
1135 d1fifo
->pipe
= NULL
;
1136 d1fifo
->tx_chan
= NULL
;
1137 d1fifo
->rx_chan
= NULL
;
1139 usbhsf_dma_init(priv
, usbhsf_get_d0fifo(priv
));
1140 usbhsf_dma_init(priv
, usbhsf_get_d1fifo(priv
));
1143 void usbhs_fifo_quit(struct usbhs_priv
*priv
)
1145 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
1147 mod
->irq_empty
= NULL
;
1148 mod
->irq_ready
= NULL
;
1149 mod
->irq_bempsts
= 0;
1150 mod
->irq_brdysts
= 0;
1152 usbhsf_dma_quit(priv
, usbhsf_get_d0fifo(priv
));
1153 usbhsf_dma_quit(priv
, usbhsf_get_d1fifo(priv
));
1156 int usbhs_fifo_probe(struct usbhs_priv
*priv
)
1158 struct usbhs_fifo
*fifo
;
1161 fifo
= usbhsf_get_cfifo(priv
);
1162 fifo
->name
= "CFIFO";
1164 fifo
->sel
= CFIFOSEL
;
1165 fifo
->ctr
= CFIFOCTR
;
1168 fifo
= usbhsf_get_d0fifo(priv
);
1169 fifo
->name
= "D0FIFO";
1170 fifo
->port
= D0FIFO
;
1171 fifo
->sel
= D0FIFOSEL
;
1172 fifo
->ctr
= D0FIFOCTR
;
1173 fifo
->tx_slave
.slave_id
= usbhs_get_dparam(priv
, d0_tx_id
);
1174 fifo
->rx_slave
.slave_id
= usbhs_get_dparam(priv
, d0_rx_id
);
1177 fifo
= usbhsf_get_d1fifo(priv
);
1178 fifo
->name
= "D1FIFO";
1179 fifo
->port
= D1FIFO
;
1180 fifo
->sel
= D1FIFOSEL
;
1181 fifo
->ctr
= D1FIFOCTR
;
1182 fifo
->tx_slave
.slave_id
= usbhs_get_dparam(priv
, d1_tx_id
);
1183 fifo
->rx_slave
.slave_id
= usbhs_get_dparam(priv
, d1_rx_id
);
1188 void usbhs_fifo_remove(struct usbhs_priv
*priv
)