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>
22 #define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
23 #define usbhsf_get_d0fifo(p) (&((p)->fifo_info.d0fifo))
24 #define usbhsf_get_d1fifo(p) (&((p)->fifo_info.d1fifo))
26 #define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
29 * packet info function
31 static int usbhsf_null_handle(struct usbhs_pkt
*pkt
, int *is_done
)
33 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pkt
->pipe
);
34 struct device
*dev
= usbhs_priv_to_dev(priv
);
36 dev_err(dev
, "null handler\n");
41 static struct usbhs_pkt_handle usbhsf_null_handler
= {
42 .prepare
= usbhsf_null_handle
,
43 .try_run
= usbhsf_null_handle
,
46 void usbhs_pkt_init(struct usbhs_pkt
*pkt
)
48 pkt
->dma
= DMA_ADDR_INVALID
;
49 INIT_LIST_HEAD(&pkt
->node
);
52 void usbhs_pkt_push(struct usbhs_pipe
*pipe
, struct usbhs_pkt
*pkt
,
53 struct usbhs_pkt_handle
*handler
,
54 void *buf
, int len
, int zero
)
56 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
57 struct device
*dev
= usbhs_priv_to_dev(priv
);
60 /******************** spin lock ********************/
61 usbhs_lock(priv
, flags
);
64 dev_err(dev
, "no handler function\n");
65 handler
= &usbhsf_null_handler
;
68 list_del_init(&pkt
->node
);
69 list_add_tail(&pkt
->node
, &pipe
->list
);
73 pkt
->handler
= handler
;
78 usbhs_unlock(priv
, flags
);
79 /******************** spin unlock ******************/
81 usbhs_pkt_start(pipe
);
84 static void __usbhsf_pkt_del(struct usbhs_pkt
*pkt
)
86 list_del_init(&pkt
->node
);
89 static struct usbhs_pkt
*__usbhsf_pkt_get(struct usbhs_pipe
*pipe
)
91 if (list_empty(&pipe
->list
))
94 return list_entry(pipe
->list
.next
, struct usbhs_pkt
, node
);
97 struct usbhs_pkt
*usbhs_pkt_pop(struct usbhs_pipe
*pipe
, struct usbhs_pkt
*pkt
)
99 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
102 /******************** spin lock ********************/
103 usbhs_lock(priv
, flags
);
106 pkt
= __usbhsf_pkt_get(pipe
);
109 __usbhsf_pkt_del(pkt
);
111 usbhs_unlock(priv
, flags
);
112 /******************** spin unlock ******************/
117 int __usbhs_pkt_handler(struct usbhs_pipe
*pipe
, int type
)
119 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
120 struct usbhs_pipe_info
*info
= usbhs_priv_to_pipeinfo(priv
);
121 struct usbhs_pkt
*pkt
;
122 struct device
*dev
= usbhs_priv_to_dev(priv
);
123 int (*func
)(struct usbhs_pkt
*pkt
, int *is_done
);
128 /******************** spin lock ********************/
129 usbhs_lock(priv
, flags
);
131 pkt
= __usbhsf_pkt_get(pipe
);
133 goto __usbhs_pkt_handler_end
;
136 case USBHSF_PKT_PREPARE
:
137 func
= pkt
->handler
->prepare
;
139 case USBHSF_PKT_TRY_RUN
:
140 func
= pkt
->handler
->try_run
;
142 case USBHSF_PKT_DMA_DONE
:
143 func
= pkt
->handler
->dma_done
;
146 dev_err(dev
, "unknown pkt hander\n");
147 goto __usbhs_pkt_handler_end
;
150 ret
= func(pkt
, &is_done
);
153 __usbhsf_pkt_del(pkt
);
155 __usbhs_pkt_handler_end
:
156 usbhs_unlock(priv
, flags
);
157 /******************** spin unlock ******************/
161 usbhs_pkt_start(pipe
);
168 * irq enable/disable function
170 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
171 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
172 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
174 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
175 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
176 u16 status = (1 << usbhs_pipe_number(pipe)); \
180 mod->irq_##status |= status; \
182 mod->irq_##status &= ~status; \
183 usbhs_irq_callback_update(priv, mod); \
186 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe
*pipe
, int enable
)
189 * And DCP pipe can NOT use "ready interrupt" for "send"
190 * it should use "empty" interrupt.
192 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
194 * on the other hand, normal pipe can use "ready interrupt" for "send"
195 * even though it is single/double buffer
197 if (usbhs_pipe_is_dcp(pipe
))
198 usbhsf_irq_empty_ctrl(pipe
, enable
);
200 usbhsf_irq_ready_ctrl(pipe
, enable
);
203 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe
*pipe
, int enable
)
205 usbhsf_irq_ready_ctrl(pipe
, enable
);
211 static void usbhsf_send_terminator(struct usbhs_pipe
*pipe
,
212 struct usbhs_fifo
*fifo
)
214 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
216 usbhs_bset(priv
, fifo
->ctr
, BVAL
, BVAL
);
219 static int usbhsf_fifo_barrier(struct usbhs_priv
*priv
,
220 struct usbhs_fifo
*fifo
)
225 /* The FIFO port is accessible */
226 if (usbhs_read(priv
, fifo
->ctr
) & FRDY
)
235 static void usbhsf_fifo_clear(struct usbhs_pipe
*pipe
,
236 struct usbhs_fifo
*fifo
)
238 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
240 if (!usbhs_pipe_is_dcp(pipe
))
241 usbhsf_fifo_barrier(priv
, fifo
);
243 usbhs_write(priv
, fifo
->ctr
, BCLR
);
246 static int usbhsf_fifo_rcv_len(struct usbhs_priv
*priv
,
247 struct usbhs_fifo
*fifo
)
249 return usbhs_read(priv
, fifo
->ctr
) & DTLN_MASK
;
252 static void usbhsf_fifo_unselect(struct usbhs_pipe
*pipe
,
253 struct usbhs_fifo
*fifo
)
255 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
257 usbhs_pipe_select_fifo(pipe
, NULL
);
258 usbhs_write(priv
, fifo
->sel
, 0);
261 static int usbhsf_fifo_select(struct usbhs_pipe
*pipe
,
262 struct usbhs_fifo
*fifo
,
265 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
266 struct device
*dev
= usbhs_priv_to_dev(priv
);
268 u16 mask
= ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
269 u16 base
= usbhs_pipe_number(pipe
); /* CURPIPE */
271 if (usbhs_pipe_is_busy(pipe
) ||
272 usbhsf_fifo_is_busy(fifo
))
275 if (usbhs_pipe_is_dcp(pipe
))
276 base
|= (1 == write
) << 5; /* ISEL */
278 /* "base" will be used below */
279 usbhs_write(priv
, fifo
->sel
, base
| MBW_32
);
281 /* check ISEL and CURPIPE value */
283 if (base
== (mask
& usbhs_read(priv
, fifo
->sel
))) {
284 usbhs_pipe_select_fifo(pipe
, fifo
);
290 dev_err(dev
, "fifo select error\n");
298 static int usbhsf_pio_try_push(struct usbhs_pkt
*pkt
, int *is_done
)
300 struct usbhs_pipe
*pipe
= pkt
->pipe
;
301 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
302 struct device
*dev
= usbhs_priv_to_dev(priv
);
303 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
304 void __iomem
*addr
= priv
->base
+ fifo
->port
;
306 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
311 ret
= usbhsf_fifo_select(pipe
, fifo
, 1);
315 ret
= usbhs_pipe_is_accessible(pipe
);
317 goto usbhs_fifo_write_busy
;
319 ret
= usbhsf_fifo_barrier(priv
, fifo
);
321 goto usbhs_fifo_write_busy
;
323 buf
= pkt
->buf
+ pkt
->actual
;
324 len
= pkt
->length
- pkt
->actual
;
325 len
= min(len
, maxp
);
327 is_short
= total_len
< maxp
;
334 if (len
>= 4 && !((unsigned long)buf
& 0x03)) {
335 iowrite32_rep(addr
, buf
, len
/ 4);
337 buf
+= total_len
- len
;
340 /* the rest operation */
341 for (i
= 0; i
< len
; i
++)
342 iowrite8(buf
[i
], addr
+ (0x03 - (i
& 0x03)));
347 pkt
->actual
+= total_len
;
349 if (pkt
->actual
< pkt
->length
)
350 *is_done
= 0; /* there are remainder data */
352 *is_done
= 1; /* short packet */
354 *is_done
= !pkt
->zero
; /* send zero packet ? */
360 usbhsf_send_terminator(pipe
, fifo
);
362 usbhsf_tx_irq_ctrl(pipe
, !*is_done
);
363 usbhs_pipe_enable(pipe
);
365 dev_dbg(dev
, " send %d (%d/ %d/ %d/ %d)\n",
366 usbhs_pipe_number(pipe
),
367 pkt
->length
, pkt
->actual
, *is_done
, pkt
->zero
);
373 if (usbhs_pipe_is_dcp(pipe
))
374 usbhs_dcp_control_transfer_done(pipe
);
377 usbhsf_fifo_unselect(pipe
, fifo
);
381 usbhs_fifo_write_busy
:
382 usbhsf_fifo_unselect(pipe
, fifo
);
388 usbhsf_tx_irq_ctrl(pipe
, 1);
393 struct usbhs_pkt_handle usbhs_fifo_pio_push_handler
= {
394 .prepare
= usbhsf_pio_try_push
,
395 .try_run
= usbhsf_pio_try_push
,
398 static int usbhsf_prepare_pop(struct usbhs_pkt
*pkt
, int *is_done
)
400 struct usbhs_pipe
*pipe
= pkt
->pipe
;
402 if (usbhs_pipe_is_busy(pipe
))
406 * pipe enable to prepare packet receive
409 usbhs_pipe_enable(pipe
);
410 usbhsf_rx_irq_ctrl(pipe
, 1);
415 static int usbhsf_pio_try_pop(struct usbhs_pkt
*pkt
, int *is_done
)
417 struct usbhs_pipe
*pipe
= pkt
->pipe
;
418 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
419 struct device
*dev
= usbhs_priv_to_dev(priv
);
420 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
421 void __iomem
*addr
= priv
->base
+ fifo
->port
;
424 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
429 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
433 ret
= usbhsf_fifo_barrier(priv
, fifo
);
435 goto usbhs_fifo_read_busy
;
437 rcv_len
= usbhsf_fifo_rcv_len(priv
, fifo
);
439 buf
= pkt
->buf
+ pkt
->actual
;
440 len
= pkt
->length
- pkt
->actual
;
441 len
= min(len
, rcv_len
);
445 * Buffer clear if Zero-Length packet
448 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
451 usbhsf_fifo_clear(pipe
, fifo
);
452 goto usbhs_fifo_read_end
;
460 if (len
>= 4 && !((unsigned long)buf
& 0x03)) {
461 ioread32_rep(addr
, buf
, len
/ 4);
463 buf
+= total_len
- len
;
466 /* the rest operation */
467 for (i
= 0; i
< len
; i
++) {
469 data
= ioread32(addr
);
471 buf
[i
] = (data
>> ((i
& 0x03) * 8)) & 0xff;
474 pkt
->actual
+= total_len
;
477 if ((pkt
->actual
== pkt
->length
) || /* receive all data */
478 (total_len
< maxp
)) { /* short packet */
480 usbhsf_rx_irq_ctrl(pipe
, 0);
481 usbhs_pipe_disable(pipe
);
484 dev_dbg(dev
, " recv %d (%d/ %d/ %d/ %d)\n",
485 usbhs_pipe_number(pipe
),
486 pkt
->length
, pkt
->actual
, *is_done
, pkt
->zero
);
488 usbhs_fifo_read_busy
:
489 usbhsf_fifo_unselect(pipe
, fifo
);
494 struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler
= {
495 .prepare
= usbhsf_prepare_pop
,
496 .try_run
= usbhsf_pio_try_pop
,
502 static int usbhsf_ctrl_stage_end(struct usbhs_pkt
*pkt
, int *is_done
)
504 usbhs_dcp_control_transfer_done(pkt
->pipe
);
511 struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler
= {
512 .prepare
= usbhsf_ctrl_stage_end
,
513 .try_run
= usbhsf_ctrl_stage_end
,
519 static struct dma_chan
*usbhsf_dma_chan_get(struct usbhs_fifo
*fifo
,
520 struct usbhs_pkt
*pkt
)
522 if (&usbhs_fifo_dma_push_handler
== pkt
->handler
)
523 return fifo
->tx_chan
;
525 if (&usbhs_fifo_dma_pop_handler
== pkt
->handler
)
526 return fifo
->rx_chan
;
531 static struct usbhs_fifo
*usbhsf_get_dma_fifo(struct usbhs_priv
*priv
,
532 struct usbhs_pkt
*pkt
)
534 struct usbhs_fifo
*fifo
;
537 fifo
= usbhsf_get_d0fifo(priv
);
538 if (usbhsf_dma_chan_get(fifo
, pkt
) &&
539 !usbhsf_fifo_is_busy(fifo
))
543 fifo
= usbhsf_get_d1fifo(priv
);
544 if (usbhsf_dma_chan_get(fifo
, pkt
) &&
545 !usbhsf_fifo_is_busy(fifo
))
551 #define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
552 #define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
553 static void __usbhsf_dma_ctrl(struct usbhs_pipe
*pipe
,
554 struct usbhs_fifo
*fifo
,
557 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
559 usbhs_bset(priv
, fifo
->sel
, DREQE
, dreqe
);
562 #define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
563 #define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
564 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt
*pkt
, int map
)
566 struct usbhs_pipe
*pipe
= pkt
->pipe
;
567 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
568 struct usbhs_pipe_info
*info
= usbhs_priv_to_pipeinfo(priv
);
570 return info
->dma_map_ctrl(pkt
, map
);
573 static void usbhsf_dma_complete(void *arg
);
574 static void usbhsf_dma_prepare_tasklet(unsigned long data
)
576 struct usbhs_pkt
*pkt
= (struct usbhs_pkt
*)data
;
577 struct usbhs_pipe
*pipe
= pkt
->pipe
;
578 struct usbhs_fifo
*fifo
= usbhs_pipe_to_fifo(pipe
);
579 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
580 struct scatterlist sg
;
581 struct dma_async_tx_descriptor
*desc
;
582 struct dma_chan
*chan
= usbhsf_dma_chan_get(fifo
, pkt
);
583 struct device
*dev
= usbhs_priv_to_dev(priv
);
584 enum dma_data_direction dir
;
587 dir
= usbhs_pipe_is_dir_in(pipe
) ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
589 sg_init_table(&sg
, 1);
590 sg_set_page(&sg
, virt_to_page(pkt
->dma
),
591 pkt
->length
, offset_in_page(pkt
->dma
));
592 sg_dma_address(&sg
) = pkt
->dma
+ pkt
->actual
;
593 sg_dma_len(&sg
) = pkt
->trans
;
595 desc
= chan
->device
->device_prep_slave_sg(chan
, &sg
, 1, dir
,
601 desc
->callback
= usbhsf_dma_complete
;
602 desc
->callback_param
= pipe
;
604 cookie
= desc
->tx_submit(desc
);
606 dev_err(dev
, "Failed to submit dma descriptor\n");
610 dev_dbg(dev
, " %s %d (%d/ %d)\n",
611 fifo
->name
, usbhs_pipe_number(pipe
), pkt
->length
, pkt
->zero
);
613 usbhsf_dma_start(pipe
, fifo
);
614 dma_async_issue_pending(chan
);
617 static int usbhsf_dma_prepare_push(struct usbhs_pkt
*pkt
, int *is_done
)
619 struct usbhs_pipe
*pipe
= pkt
->pipe
;
620 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
621 struct usbhs_fifo
*fifo
;
622 int len
= pkt
->length
- pkt
->actual
;
625 if (usbhs_pipe_is_busy(pipe
))
628 /* use PIO if packet is less than pio_dma_border or pipe is DCP */
629 if ((len
< usbhs_get_dparam(priv
, pio_dma_border
)) ||
630 usbhs_pipe_is_dcp(pipe
))
631 goto usbhsf_pio_prepare_push
;
633 if (len
% 4) /* 32bit alignment */
634 goto usbhsf_pio_prepare_push
;
636 /* get enable DMA fifo */
637 fifo
= usbhsf_get_dma_fifo(priv
, pkt
);
639 goto usbhsf_pio_prepare_push
;
641 if (usbhsf_dma_map(pkt
) < 0)
642 goto usbhsf_pio_prepare_push
;
644 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
646 goto usbhsf_pio_prepare_push_unmap
;
650 tasklet_init(&fifo
->tasklet
,
651 usbhsf_dma_prepare_tasklet
,
654 tasklet_schedule(&fifo
->tasklet
);
658 usbhsf_pio_prepare_push_unmap
:
659 usbhsf_dma_unmap(pkt
);
660 usbhsf_pio_prepare_push
:
662 * change handler to PIO
664 pkt
->handler
= &usbhs_fifo_pio_push_handler
;
666 return pkt
->handler
->prepare(pkt
, is_done
);
669 static int usbhsf_dma_push_done(struct usbhs_pkt
*pkt
, int *is_done
)
671 struct usbhs_pipe
*pipe
= pkt
->pipe
;
673 pkt
->actual
= pkt
->trans
;
675 *is_done
= !pkt
->zero
; /* send zero packet ? */
677 usbhsf_dma_stop(pipe
, pipe
->fifo
);
678 usbhsf_dma_unmap(pkt
);
679 usbhsf_fifo_unselect(pipe
, pipe
->fifo
);
684 struct usbhs_pkt_handle usbhs_fifo_dma_push_handler
= {
685 .prepare
= usbhsf_dma_prepare_push
,
686 .dma_done
= usbhsf_dma_push_done
,
689 static int usbhsf_dma_try_pop(struct usbhs_pkt
*pkt
, int *is_done
)
691 struct usbhs_pipe
*pipe
= pkt
->pipe
;
692 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
693 struct usbhs_fifo
*fifo
;
696 if (usbhs_pipe_is_busy(pipe
))
699 if (usbhs_pipe_is_dcp(pipe
))
700 goto usbhsf_pio_prepare_pop
;
702 /* get enable DMA fifo */
703 fifo
= usbhsf_get_dma_fifo(priv
, pkt
);
705 goto usbhsf_pio_prepare_pop
;
707 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
709 goto usbhsf_pio_prepare_pop
;
711 /* use PIO if packet is less than pio_dma_border */
712 len
= usbhsf_fifo_rcv_len(priv
, fifo
);
713 len
= min(pkt
->length
- pkt
->actual
, len
);
714 if (len
% 4) /* 32bit alignment */
715 goto usbhsf_pio_prepare_pop_unselect
;
717 if (len
< usbhs_get_dparam(priv
, pio_dma_border
))
718 goto usbhsf_pio_prepare_pop_unselect
;
720 ret
= usbhsf_fifo_barrier(priv
, fifo
);
722 goto usbhsf_pio_prepare_pop_unselect
;
724 if (usbhsf_dma_map(pkt
) < 0)
725 goto usbhsf_pio_prepare_pop_unselect
;
730 * usbhs_fifo_dma_pop_handler :: prepare
731 * enabled irq to come here.
732 * but it is no longer needed for DMA. disable it.
734 usbhsf_rx_irq_ctrl(pipe
, 0);
738 tasklet_init(&fifo
->tasklet
,
739 usbhsf_dma_prepare_tasklet
,
742 tasklet_schedule(&fifo
->tasklet
);
746 usbhsf_pio_prepare_pop_unselect
:
747 usbhsf_fifo_unselect(pipe
, fifo
);
748 usbhsf_pio_prepare_pop
:
751 * change handler to PIO
753 pkt
->handler
= &usbhs_fifo_pio_pop_handler
;
755 return pkt
->handler
->try_run(pkt
, is_done
);
758 static int usbhsf_dma_pop_done(struct usbhs_pkt
*pkt
, int *is_done
)
760 struct usbhs_pipe
*pipe
= pkt
->pipe
;
761 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
763 usbhsf_dma_stop(pipe
, pipe
->fifo
);
764 usbhsf_dma_unmap(pkt
);
765 usbhsf_fifo_unselect(pipe
, pipe
->fifo
);
767 pkt
->actual
+= pkt
->trans
;
769 if ((pkt
->actual
== pkt
->length
) || /* receive all data */
770 (pkt
->trans
< maxp
)) { /* short packet */
774 usbhsf_prepare_pop(pkt
, is_done
);
780 struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler
= {
781 .prepare
= usbhsf_prepare_pop
,
782 .try_run
= usbhsf_dma_try_pop
,
783 .dma_done
= usbhsf_dma_pop_done
789 static bool usbhsf_dma_filter(struct dma_chan
*chan
, void *param
)
791 struct sh_dmae_slave
*slave
= param
;
796 * usbhs doesn't recognize id = 0 as valid DMA
798 if (0 == slave
->slave_id
)
801 chan
->private = slave
;
806 static void usbhsf_dma_quit(struct usbhs_priv
*priv
, struct usbhs_fifo
*fifo
)
809 dma_release_channel(fifo
->tx_chan
);
811 dma_release_channel(fifo
->rx_chan
);
813 fifo
->tx_chan
= NULL
;
814 fifo
->rx_chan
= NULL
;
817 static void usbhsf_dma_init(struct usbhs_priv
*priv
,
818 struct usbhs_fifo
*fifo
)
820 struct device
*dev
= usbhs_priv_to_dev(priv
);
824 dma_cap_set(DMA_SLAVE
, mask
);
825 fifo
->tx_chan
= dma_request_channel(mask
, usbhsf_dma_filter
,
829 dma_cap_set(DMA_SLAVE
, mask
);
830 fifo
->rx_chan
= dma_request_channel(mask
, usbhsf_dma_filter
,
833 if (fifo
->tx_chan
|| fifo
->rx_chan
)
834 dev_dbg(dev
, "enable DMAEngine (%s%s%s)\n",
836 fifo
->tx_chan
? "[TX]" : " ",
837 fifo
->rx_chan
? "[RX]" : " ");
843 static int usbhsf_irq_empty(struct usbhs_priv
*priv
,
844 struct usbhs_irq_state
*irq_state
)
846 struct usbhs_pipe
*pipe
;
847 struct device
*dev
= usbhs_priv_to_dev(priv
);
850 if (!irq_state
->bempsts
) {
851 dev_err(dev
, "debug %s !!\n", __func__
);
855 dev_dbg(dev
, "irq empty [0x%04x]\n", irq_state
->bempsts
);
858 * search interrupted "pipe"
861 usbhs_for_each_pipe_with_dcp(pipe
, priv
, i
) {
862 if (!(irq_state
->bempsts
& (1 << i
)))
865 ret
= usbhs_pkt_run(pipe
);
867 dev_err(dev
, "irq_empty run_error %d : %d\n", i
, ret
);
873 static int usbhsf_irq_ready(struct usbhs_priv
*priv
,
874 struct usbhs_irq_state
*irq_state
)
876 struct usbhs_pipe
*pipe
;
877 struct device
*dev
= usbhs_priv_to_dev(priv
);
880 if (!irq_state
->brdysts
) {
881 dev_err(dev
, "debug %s !!\n", __func__
);
885 dev_dbg(dev
, "irq ready [0x%04x]\n", irq_state
->brdysts
);
888 * search interrupted "pipe"
891 usbhs_for_each_pipe_with_dcp(pipe
, priv
, i
) {
892 if (!(irq_state
->brdysts
& (1 << i
)))
895 ret
= usbhs_pkt_run(pipe
);
897 dev_err(dev
, "irq_ready run_error %d : %d\n", i
, ret
);
903 static void usbhsf_dma_complete(void *arg
)
905 struct usbhs_pipe
*pipe
= arg
;
906 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
907 struct device
*dev
= usbhs_priv_to_dev(priv
);
910 ret
= usbhs_pkt_dmadone(pipe
);
912 dev_err(dev
, "dma_complete run_error %d : %d\n",
913 usbhs_pipe_number(pipe
), ret
);
919 void usbhs_fifo_init(struct usbhs_priv
*priv
)
921 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
922 struct usbhs_fifo
*cfifo
= usbhsf_get_cfifo(priv
);
923 struct usbhs_fifo
*d0fifo
= usbhsf_get_d0fifo(priv
);
924 struct usbhs_fifo
*d1fifo
= usbhsf_get_d1fifo(priv
);
926 mod
->irq_empty
= usbhsf_irq_empty
;
927 mod
->irq_ready
= usbhsf_irq_ready
;
928 mod
->irq_bempsts
= 0;
929 mod
->irq_brdysts
= 0;
932 cfifo
->tx_chan
= NULL
;
933 cfifo
->rx_chan
= NULL
;
936 d0fifo
->tx_chan
= NULL
;
937 d0fifo
->rx_chan
= NULL
;
940 d1fifo
->tx_chan
= NULL
;
941 d1fifo
->rx_chan
= NULL
;
943 usbhsf_dma_init(priv
, usbhsf_get_d0fifo(priv
));
944 usbhsf_dma_init(priv
, usbhsf_get_d1fifo(priv
));
947 void usbhs_fifo_quit(struct usbhs_priv
*priv
)
949 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
951 mod
->irq_empty
= NULL
;
952 mod
->irq_ready
= NULL
;
953 mod
->irq_bempsts
= 0;
954 mod
->irq_brdysts
= 0;
956 usbhsf_dma_quit(priv
, usbhsf_get_d0fifo(priv
));
957 usbhsf_dma_quit(priv
, usbhsf_get_d1fifo(priv
));
960 int usbhs_fifo_probe(struct usbhs_priv
*priv
)
962 struct usbhs_fifo
*fifo
;
965 fifo
= usbhsf_get_cfifo(priv
);
966 fifo
->name
= "CFIFO";
968 fifo
->sel
= CFIFOSEL
;
969 fifo
->ctr
= CFIFOCTR
;
972 fifo
= usbhsf_get_d0fifo(priv
);
973 fifo
->name
= "D0FIFO";
975 fifo
->sel
= D0FIFOSEL
;
976 fifo
->ctr
= D0FIFOCTR
;
977 fifo
->tx_slave
.slave_id
= usbhs_get_dparam(priv
, d0_tx_id
);
978 fifo
->rx_slave
.slave_id
= usbhs_get_dparam(priv
, d0_rx_id
);
981 fifo
= usbhsf_get_d1fifo(priv
);
982 fifo
->name
= "D1FIFO";
984 fifo
->sel
= D1FIFOSEL
;
985 fifo
->ctr
= D1FIFOCTR
;
986 fifo
->tx_slave
.slave_id
= usbhs_get_dparam(priv
, d1_tx_id
);
987 fifo
->rx_slave
.slave_id
= usbhs_get_dparam(priv
, d1_rx_id
);
992 void usbhs_fifo_remove(struct usbhs_priv
*priv
)