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>
23 * packet info function
25 static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
27 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
28 struct device *dev = usbhs_priv_to_dev(priv);
30 dev_err(dev, "null handler\n");
35 static struct usbhs_pkt_handle usbhsf_null_handler = {
36 .prepare = usbhsf_null_handle,
37 .try_run = usbhsf_null_handle,
40 void usbhs_pkt_init(struct usbhs_pkt *pkt)
42 INIT_LIST_HEAD(&pkt->node);
45 void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
46 struct usbhs_pkt_handle *handler,
47 void *buf, int len, int zero)
49 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
50 struct device *dev = usbhs_priv_to_dev(priv);
53 /******************** spin lock ********************/
54 usbhs_lock(priv, flags);
57 dev_err(dev, "no handler function\n");
58 handler = &usbhsf_null_handler;
61 list_del_init(&pkt->node);
62 list_add_tail(&pkt->node, &pipe->list);
66 pkt->handler = handler;
71 usbhs_unlock(priv, flags);
72 /******************** spin unlock ******************/
75 static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
77 list_del_init(&pkt->node);
80 static struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
82 if (list_empty(&pipe->list))
85 return list_entry(pipe->list.next, struct usbhs_pkt, node);
88 struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
90 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
93 /******************** spin lock ********************/
94 usbhs_lock(priv, flags);
97 pkt = __usbhsf_pkt_get(pipe);
100 __usbhsf_pkt_del(pkt);
102 usbhs_unlock(priv, flags);
103 /******************** spin unlock ******************/
108 int __usbhs_pkt_handler(struct usbhs_pipe *pipe, int type)
110 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
111 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
112 struct usbhs_pkt *pkt;
113 struct device *dev = usbhs_priv_to_dev(priv);
114 int (*func)(struct usbhs_pkt *pkt, int *is_done);
119 /******************** spin lock ********************/
120 usbhs_lock(priv, flags);
122 pkt = __usbhsf_pkt_get(pipe);
124 goto __usbhs_pkt_handler_end;
127 case USBHSF_PKT_PREPARE:
128 func = pkt->handler->prepare;
130 case USBHSF_PKT_TRY_RUN:
131 func = pkt->handler->try_run;
134 dev_err(dev, "unknown pkt hander\n");
135 goto __usbhs_pkt_handler_end;
138 ret = func(pkt, &is_done);
141 __usbhsf_pkt_del(pkt);
143 __usbhs_pkt_handler_end:
144 usbhs_unlock(priv, flags);
145 /******************** spin unlock ******************/
154 * irq enable/disable function
156 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, bempsts, e)
157 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, brdysts, e)
158 #define usbhsf_irq_callback_ctrl(pipe, status, enable) \
160 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
161 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
162 u16 status = (1 << usbhs_pipe_number(pipe)); \
166 mod->irq_##status |= status; \
168 mod->irq_##status &= ~status; \
169 usbhs_irq_callback_update(priv, mod); \
172 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
175 * And DCP pipe can NOT use "ready interrupt" for "send"
176 * it should use "empty" interrupt.
178 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
180 * on the other hand, normal pipe can use "ready interrupt" for "send"
181 * even though it is single/double buffer
183 if (usbhs_pipe_is_dcp(pipe))
184 usbhsf_irq_empty_ctrl(pipe, enable);
186 usbhsf_irq_ready_ctrl(pipe, enable);
189 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
191 usbhsf_irq_ready_ctrl(pipe, enable);
197 static void usbhsf_send_terminator(struct usbhs_pipe *pipe)
199 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
201 usbhs_bset(priv, CFIFOCTR, BVAL, BVAL);
204 static int usbhsf_fifo_barrier(struct usbhs_priv *priv)
209 /* The FIFO port is accessible */
210 if (usbhs_read(priv, CFIFOCTR) & FRDY)
219 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe)
221 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
223 if (!usbhs_pipe_is_dcp(pipe))
224 usbhsf_fifo_barrier(priv);
226 usbhs_write(priv, CFIFOCTR, BCLR);
229 static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv)
231 return usbhs_read(priv, CFIFOCTR) & DTLN_MASK;
234 static int usbhsf_fifo_select(struct usbhs_pipe *pipe, int write)
236 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
237 struct device *dev = usbhs_priv_to_dev(priv);
239 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
240 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
242 if (usbhs_pipe_is_dcp(pipe))
243 base |= (1 == write) << 5; /* ISEL */
245 /* "base" will be used below */
246 usbhs_write(priv, CFIFOSEL, base | MBW_32);
248 /* check ISEL and CURPIPE value */
250 if (base == (mask & usbhs_read(priv, CFIFOSEL)))
255 dev_err(dev, "fifo select error\n");
263 static int usbhsf_try_push(struct usbhs_pkt *pkt, int *is_done)
265 struct usbhs_pipe *pipe = pkt->pipe;
266 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
267 struct device *dev = usbhs_priv_to_dev(priv);
268 void __iomem *addr = priv->base + CFIFO;
270 int maxp = usbhs_pipe_get_maxpacket(pipe);
275 ret = usbhsf_fifo_select(pipe, 1);
277 goto usbhs_fifo_write_busy;
279 ret = usbhs_pipe_is_accessible(pipe);
281 goto usbhs_fifo_write_busy;
283 ret = usbhsf_fifo_barrier(priv);
285 goto usbhs_fifo_write_busy;
287 buf = pkt->buf + pkt->actual;
288 len = pkt->length - pkt->actual;
289 len = min(len, maxp);
291 is_short = total_len < maxp;
298 if (len >= 4 && !((unsigned long)buf & 0x03)) {
299 iowrite32_rep(addr, buf, len / 4);
301 buf += total_len - len;
304 /* the rest operation */
305 for (i = 0; i < len; i++)
306 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
311 pkt->actual += total_len;
313 if (pkt->actual < pkt->length)
314 *is_done = 0; /* there are remainder data */
316 *is_done = 1; /* short packet */
318 *is_done = !pkt->zero; /* send zero packet ? */
324 usbhsf_send_terminator(pipe);
326 usbhsf_tx_irq_ctrl(pipe, !*is_done);
327 usbhs_pipe_enable(pipe);
329 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
330 usbhs_pipe_number(pipe),
331 pkt->length, pkt->actual, *is_done, pkt->zero);
337 if (usbhs_pipe_is_dcp(pipe))
338 usbhs_dcp_control_transfer_done(pipe);
343 usbhs_fifo_write_busy:
348 usbhsf_tx_irq_ctrl(pipe, 1);
353 struct usbhs_pkt_handle usbhs_fifo_push_handler = {
354 .prepare = usbhsf_try_push,
355 .try_run = usbhsf_try_push,
358 static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
360 struct usbhs_pipe *pipe = pkt->pipe;
364 * select pipe and enable it to prepare packet receive
366 ret = usbhsf_fifo_select(pipe, 0);
370 usbhs_pipe_enable(pipe);
371 usbhsf_rx_irq_ctrl(pipe, 1);
376 static int usbhsf_try_pop(struct usbhs_pkt *pkt, int *is_done)
378 struct usbhs_pipe *pipe = pkt->pipe;
379 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
380 struct device *dev = usbhs_priv_to_dev(priv);
381 void __iomem *addr = priv->base + CFIFO;
384 int maxp = usbhs_pipe_get_maxpacket(pipe);
389 ret = usbhsf_fifo_select(pipe, 0);
393 ret = usbhsf_fifo_barrier(priv);
397 rcv_len = usbhsf_fifo_rcv_len(priv);
399 buf = pkt->buf + pkt->actual;
400 len = pkt->length - pkt->actual;
401 len = min(len, rcv_len);
405 * Buffer clear if Zero-Length packet
408 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
411 usbhsf_fifo_clear(pipe);
412 goto usbhs_fifo_read_end;
420 if (len >= 4 && !((unsigned long)buf & 0x03)) {
421 ioread32_rep(addr, buf, len / 4);
423 buf += total_len - len;
426 /* the rest operation */
427 for (i = 0; i < len; i++) {
429 data = ioread32(addr);
431 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
434 pkt->actual += total_len;
437 if ((pkt->actual == pkt->length) || /* receive all data */
438 (total_len < maxp)) { /* short packet */
440 usbhsf_rx_irq_ctrl(pipe, 0);
441 usbhs_pipe_disable(pipe);
444 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
445 usbhs_pipe_number(pipe),
446 pkt->length, pkt->actual, *is_done, pkt->zero);
451 struct usbhs_pkt_handle usbhs_fifo_pop_handler = {
452 .prepare = usbhsf_prepare_pop,
453 .try_run = usbhsf_try_pop,
459 static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
461 usbhs_dcp_control_transfer_done(pkt->pipe);
468 struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
469 .prepare = usbhsf_ctrl_stage_end,
470 .try_run = usbhsf_ctrl_stage_end,
476 static int usbhsf_irq_empty(struct usbhs_priv *priv,
477 struct usbhs_irq_state *irq_state)
479 struct usbhs_pipe *pipe;
480 struct device *dev = usbhs_priv_to_dev(priv);
483 if (!irq_state->bempsts) {
484 dev_err(dev, "debug %s !!\n", __func__);
488 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
491 * search interrupted "pipe"
494 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
495 if (!(irq_state->bempsts & (1 << i)))
498 ret = usbhs_pkt_run(pipe);
500 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
506 static int usbhsf_irq_ready(struct usbhs_priv *priv,
507 struct usbhs_irq_state *irq_state)
509 struct usbhs_pipe *pipe;
510 struct device *dev = usbhs_priv_to_dev(priv);
513 if (!irq_state->brdysts) {
514 dev_err(dev, "debug %s !!\n", __func__);
518 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
521 * search interrupted "pipe"
524 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
525 if (!(irq_state->brdysts & (1 << i)))
528 ret = usbhs_pkt_run(pipe);
530 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
539 void usbhs_fifo_init(struct usbhs_priv *priv)
541 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
543 mod->irq_empty = usbhsf_irq_empty;
544 mod->irq_ready = usbhsf_irq_ready;
545 mod->irq_bempsts = 0;
546 mod->irq_brdysts = 0;
549 void usbhs_fifo_quit(struct usbhs_priv *priv)
551 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
553 mod->irq_empty = NULL;
554 mod->irq_ready = NULL;
555 mod->irq_bempsts = 0;
556 mod->irq_brdysts = 0;