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))
25 * packet info function
27 static int usbhsf_null_handle(struct usbhs_pkt
*pkt
, int *is_done
)
29 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pkt
->pipe
);
30 struct device
*dev
= usbhs_priv_to_dev(priv
);
32 dev_err(dev
, "null handler\n");
37 static struct usbhs_pkt_handle usbhsf_null_handler
= {
38 .prepare
= usbhsf_null_handle
,
39 .try_run
= usbhsf_null_handle
,
42 void usbhs_pkt_init(struct usbhs_pkt
*pkt
)
44 INIT_LIST_HEAD(&pkt
->node
);
47 void usbhs_pkt_push(struct usbhs_pipe
*pipe
, struct usbhs_pkt
*pkt
,
48 struct usbhs_pkt_handle
*handler
,
49 void *buf
, int len
, int zero
)
51 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
52 struct device
*dev
= usbhs_priv_to_dev(priv
);
55 /******************** spin lock ********************/
56 usbhs_lock(priv
, flags
);
59 dev_err(dev
, "no handler function\n");
60 handler
= &usbhsf_null_handler
;
63 list_del_init(&pkt
->node
);
64 list_add_tail(&pkt
->node
, &pipe
->list
);
68 pkt
->handler
= handler
;
73 usbhs_unlock(priv
, flags
);
74 /******************** spin unlock ******************/
77 static void __usbhsf_pkt_del(struct usbhs_pkt
*pkt
)
79 list_del_init(&pkt
->node
);
82 static struct usbhs_pkt
*__usbhsf_pkt_get(struct usbhs_pipe
*pipe
)
84 if (list_empty(&pipe
->list
))
87 return list_entry(pipe
->list
.next
, struct usbhs_pkt
, node
);
90 struct usbhs_pkt
*usbhs_pkt_pop(struct usbhs_pipe
*pipe
, struct usbhs_pkt
*pkt
)
92 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
95 /******************** spin lock ********************/
96 usbhs_lock(priv
, flags
);
99 pkt
= __usbhsf_pkt_get(pipe
);
102 __usbhsf_pkt_del(pkt
);
104 usbhs_unlock(priv
, flags
);
105 /******************** spin unlock ******************/
110 int __usbhs_pkt_handler(struct usbhs_pipe
*pipe
, int type
)
112 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
113 struct usbhs_pipe_info
*info
= usbhs_priv_to_pipeinfo(priv
);
114 struct usbhs_pkt
*pkt
;
115 struct device
*dev
= usbhs_priv_to_dev(priv
);
116 int (*func
)(struct usbhs_pkt
*pkt
, int *is_done
);
121 /******************** spin lock ********************/
122 usbhs_lock(priv
, flags
);
124 pkt
= __usbhsf_pkt_get(pipe
);
126 goto __usbhs_pkt_handler_end
;
129 case USBHSF_PKT_PREPARE
:
130 func
= pkt
->handler
->prepare
;
132 case USBHSF_PKT_TRY_RUN
:
133 func
= pkt
->handler
->try_run
;
136 dev_err(dev
, "unknown pkt hander\n");
137 goto __usbhs_pkt_handler_end
;
140 ret
= func(pkt
, &is_done
);
143 __usbhsf_pkt_del(pkt
);
145 __usbhs_pkt_handler_end
:
146 usbhs_unlock(priv
, flags
);
147 /******************** spin unlock ******************/
156 * irq enable/disable function
158 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
159 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
160 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
162 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
163 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
164 u16 status = (1 << usbhs_pipe_number(pipe)); \
168 mod->irq_##status |= status; \
170 mod->irq_##status &= ~status; \
171 usbhs_irq_callback_update(priv, mod); \
174 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe
*pipe
, int enable
)
177 * And DCP pipe can NOT use "ready interrupt" for "send"
178 * it should use "empty" interrupt.
180 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
182 * on the other hand, normal pipe can use "ready interrupt" for "send"
183 * even though it is single/double buffer
185 if (usbhs_pipe_is_dcp(pipe
))
186 usbhsf_irq_empty_ctrl(pipe
, enable
);
188 usbhsf_irq_ready_ctrl(pipe
, enable
);
191 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe
*pipe
, int enable
)
193 usbhsf_irq_ready_ctrl(pipe
, enable
);
199 static void usbhsf_send_terminator(struct usbhs_pipe
*pipe
,
200 struct usbhs_fifo
*fifo
)
202 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
204 usbhs_bset(priv
, fifo
->ctr
, BVAL
, BVAL
);
207 static int usbhsf_fifo_barrier(struct usbhs_priv
*priv
,
208 struct usbhs_fifo
*fifo
)
213 /* The FIFO port is accessible */
214 if (usbhs_read(priv
, fifo
->ctr
) & FRDY
)
223 static void usbhsf_fifo_clear(struct usbhs_pipe
*pipe
,
224 struct usbhs_fifo
*fifo
)
226 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
228 if (!usbhs_pipe_is_dcp(pipe
))
229 usbhsf_fifo_barrier(priv
, fifo
);
231 usbhs_write(priv
, fifo
->ctr
, BCLR
);
234 static int usbhsf_fifo_rcv_len(struct usbhs_priv
*priv
,
235 struct usbhs_fifo
*fifo
)
237 return usbhs_read(priv
, fifo
->ctr
) & DTLN_MASK
;
240 static int usbhsf_fifo_select(struct usbhs_pipe
*pipe
,
241 struct usbhs_fifo
*fifo
,
244 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
245 struct device
*dev
= usbhs_priv_to_dev(priv
);
247 u16 mask
= ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
248 u16 base
= usbhs_pipe_number(pipe
); /* CURPIPE */
250 if (usbhs_pipe_is_dcp(pipe
))
251 base
|= (1 == write
) << 5; /* ISEL */
253 /* "base" will be used below */
254 usbhs_write(priv
, fifo
->sel
, base
| MBW_32
);
256 /* check ISEL and CURPIPE value */
258 if (base
== (mask
& usbhs_read(priv
, fifo
->sel
)))
263 dev_err(dev
, "fifo select error\n");
271 static int usbhsf_try_push(struct usbhs_pkt
*pkt
, int *is_done
)
273 struct usbhs_pipe
*pipe
= pkt
->pipe
;
274 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
275 struct device
*dev
= usbhs_priv_to_dev(priv
);
276 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
277 void __iomem
*addr
= priv
->base
+ fifo
->port
;
279 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
284 ret
= usbhsf_fifo_select(pipe
, fifo
, 1);
286 goto usbhs_fifo_write_busy
;
288 ret
= usbhs_pipe_is_accessible(pipe
);
290 goto usbhs_fifo_write_busy
;
292 ret
= usbhsf_fifo_barrier(priv
, fifo
);
294 goto usbhs_fifo_write_busy
;
296 buf
= pkt
->buf
+ pkt
->actual
;
297 len
= pkt
->length
- pkt
->actual
;
298 len
= min(len
, maxp
);
300 is_short
= total_len
< maxp
;
307 if (len
>= 4 && !((unsigned long)buf
& 0x03)) {
308 iowrite32_rep(addr
, buf
, len
/ 4);
310 buf
+= total_len
- len
;
313 /* the rest operation */
314 for (i
= 0; i
< len
; i
++)
315 iowrite8(buf
[i
], addr
+ (0x03 - (i
& 0x03)));
320 pkt
->actual
+= total_len
;
322 if (pkt
->actual
< pkt
->length
)
323 *is_done
= 0; /* there are remainder data */
325 *is_done
= 1; /* short packet */
327 *is_done
= !pkt
->zero
; /* send zero packet ? */
333 usbhsf_send_terminator(pipe
, fifo
);
335 usbhsf_tx_irq_ctrl(pipe
, !*is_done
);
336 usbhs_pipe_enable(pipe
);
338 dev_dbg(dev
, " send %d (%d/ %d/ %d/ %d)\n",
339 usbhs_pipe_number(pipe
),
340 pkt
->length
, pkt
->actual
, *is_done
, pkt
->zero
);
346 if (usbhs_pipe_is_dcp(pipe
))
347 usbhs_dcp_control_transfer_done(pipe
);
352 usbhs_fifo_write_busy
:
357 usbhsf_tx_irq_ctrl(pipe
, 1);
362 struct usbhs_pkt_handle usbhs_fifo_push_handler
= {
363 .prepare
= usbhsf_try_push
,
364 .try_run
= usbhsf_try_push
,
367 static int usbhsf_prepare_pop(struct usbhs_pkt
*pkt
, int *is_done
)
369 struct usbhs_pipe
*pipe
= pkt
->pipe
;
370 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
371 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
375 * select pipe and enable it to prepare packet receive
377 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
381 usbhs_pipe_enable(pipe
);
382 usbhsf_rx_irq_ctrl(pipe
, 1);
387 static int usbhsf_try_pop(struct usbhs_pkt
*pkt
, int *is_done
)
389 struct usbhs_pipe
*pipe
= pkt
->pipe
;
390 struct usbhs_priv
*priv
= usbhs_pipe_to_priv(pipe
);
391 struct device
*dev
= usbhs_priv_to_dev(priv
);
392 struct usbhs_fifo
*fifo
= usbhsf_get_cfifo(priv
); /* CFIFO */
393 void __iomem
*addr
= priv
->base
+ fifo
->port
;
396 int maxp
= usbhs_pipe_get_maxpacket(pipe
);
401 ret
= usbhsf_fifo_select(pipe
, fifo
, 0);
405 ret
= usbhsf_fifo_barrier(priv
, fifo
);
409 rcv_len
= usbhsf_fifo_rcv_len(priv
, fifo
);
411 buf
= pkt
->buf
+ pkt
->actual
;
412 len
= pkt
->length
- pkt
->actual
;
413 len
= min(len
, rcv_len
);
417 * Buffer clear if Zero-Length packet
420 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
423 usbhsf_fifo_clear(pipe
, fifo
);
424 goto usbhs_fifo_read_end
;
432 if (len
>= 4 && !((unsigned long)buf
& 0x03)) {
433 ioread32_rep(addr
, buf
, len
/ 4);
435 buf
+= total_len
- len
;
438 /* the rest operation */
439 for (i
= 0; i
< len
; i
++) {
441 data
= ioread32(addr
);
443 buf
[i
] = (data
>> ((i
& 0x03) * 8)) & 0xff;
446 pkt
->actual
+= total_len
;
449 if ((pkt
->actual
== pkt
->length
) || /* receive all data */
450 (total_len
< maxp
)) { /* short packet */
452 usbhsf_rx_irq_ctrl(pipe
, 0);
453 usbhs_pipe_disable(pipe
);
456 dev_dbg(dev
, " recv %d (%d/ %d/ %d/ %d)\n",
457 usbhs_pipe_number(pipe
),
458 pkt
->length
, pkt
->actual
, *is_done
, pkt
->zero
);
463 struct usbhs_pkt_handle usbhs_fifo_pop_handler
= {
464 .prepare
= usbhsf_prepare_pop
,
465 .try_run
= usbhsf_try_pop
,
471 static int usbhsf_ctrl_stage_end(struct usbhs_pkt
*pkt
, int *is_done
)
473 usbhs_dcp_control_transfer_done(pkt
->pipe
);
480 struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler
= {
481 .prepare
= usbhsf_ctrl_stage_end
,
482 .try_run
= usbhsf_ctrl_stage_end
,
488 static int usbhsf_irq_empty(struct usbhs_priv
*priv
,
489 struct usbhs_irq_state
*irq_state
)
491 struct usbhs_pipe
*pipe
;
492 struct device
*dev
= usbhs_priv_to_dev(priv
);
495 if (!irq_state
->bempsts
) {
496 dev_err(dev
, "debug %s !!\n", __func__
);
500 dev_dbg(dev
, "irq empty [0x%04x]\n", irq_state
->bempsts
);
503 * search interrupted "pipe"
506 usbhs_for_each_pipe_with_dcp(pipe
, priv
, i
) {
507 if (!(irq_state
->bempsts
& (1 << i
)))
510 ret
= usbhs_pkt_run(pipe
);
512 dev_err(dev
, "irq_empty run_error %d : %d\n", i
, ret
);
518 static int usbhsf_irq_ready(struct usbhs_priv
*priv
,
519 struct usbhs_irq_state
*irq_state
)
521 struct usbhs_pipe
*pipe
;
522 struct device
*dev
= usbhs_priv_to_dev(priv
);
525 if (!irq_state
->brdysts
) {
526 dev_err(dev
, "debug %s !!\n", __func__
);
530 dev_dbg(dev
, "irq ready [0x%04x]\n", irq_state
->brdysts
);
533 * search interrupted "pipe"
536 usbhs_for_each_pipe_with_dcp(pipe
, priv
, i
) {
537 if (!(irq_state
->brdysts
& (1 << i
)))
540 ret
= usbhs_pkt_run(pipe
);
542 dev_err(dev
, "irq_ready run_error %d : %d\n", i
, ret
);
551 void usbhs_fifo_init(struct usbhs_priv
*priv
)
553 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
555 mod
->irq_empty
= usbhsf_irq_empty
;
556 mod
->irq_ready
= usbhsf_irq_ready
;
557 mod
->irq_bempsts
= 0;
558 mod
->irq_brdysts
= 0;
561 void usbhs_fifo_quit(struct usbhs_priv
*priv
)
563 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
565 mod
->irq_empty
= NULL
;
566 mod
->irq_ready
= NULL
;
567 mod
->irq_bempsts
= 0;
568 mod
->irq_brdysts
= 0;
571 int usbhs_fifo_probe(struct usbhs_priv
*priv
)
573 struct usbhs_fifo
*fifo
;
576 fifo
= usbhsf_get_cfifo(priv
);
578 fifo
->sel
= CFIFOSEL
;
579 fifo
->ctr
= CFIFOCTR
;
584 void usbhs_fifo_remove(struct usbhs_priv
*priv
)