blob: 6016b2987d5e423d25aed670c0314c9e7d378c32 [file] [log] [blame]
Vitaliy Vasylskyyd8e5fc82024-09-09 01:06:24 +02001// SPDX-License-Identifier: GPL-1.0+
2/*
3 * Renesas USB driver
4 *
5 * Copyright (C) 2011 Renesas Solutions Corp.
6 * Copyright (C) 2019 Renesas Electronics Corporation
7 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 */
9#include <linux/delay.h>
10#include <linux/io.h>
11#include "common.h"
12#include "pipe.h"
13
14#define usbhsf_get_cfifo(p) (&((p)->fifo_info.cfifo))
15
16#define usbhsf_fifo_is_busy(f) ((f)->pipe) /* see usbhs_pipe_select_fifo */
17
18/*
19 * packet initialize
20 */
21void usbhs_pkt_init(struct usbhs_pkt *pkt)
22{
23 INIT_LIST_HEAD(&pkt->node);
24}
25
26/*
27 * packet control function
28 */
29static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
30{
31 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
32 struct device *dev = usbhs_priv_to_dev(priv);
33
34 dev_err(dev, "null handler\n");
35
36 return -EINVAL;
37}
38
39static const struct usbhs_pkt_handle usbhsf_null_handler = {
40 .prepare = usbhsf_null_handle,
41 .try_run = usbhsf_null_handle,
42};
43
44void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
45 void (*done)(struct usbhs_priv *priv,
46 struct usbhs_pkt *pkt),
47 void *buf, int len, int zero, int sequence)
48{
49 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
50 struct device *dev = usbhs_priv_to_dev(priv);
51 unsigned long flags;
52
53 if (!done) {
54 dev_err(dev, "no done function\n");
55 return;
56 }
57
58 /******************** spin lock ********************/
59 usbhs_lock(priv, flags);
60
61 if (!pipe->handler) {
62 dev_err(dev, "no handler function\n");
63 pipe->handler = &usbhsf_null_handler;
64 }
65
66 list_move_tail(&pkt->node, &pipe->list);
67
68 /*
69 * each pkt must hold own handler.
70 * because handler might be changed by its situation.
71 * dma handler -> pio handler.
72 */
73 pkt->pipe = pipe;
74 pkt->buf = buf;
75 pkt->handler = pipe->handler;
76 pkt->length = len;
77 pkt->zero = zero;
78 pkt->actual = 0;
79 pkt->done = done;
80 pkt->sequence = sequence;
81
82 usbhs_unlock(priv, flags);
83 /******************** spin unlock ******************/
84}
85
86static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
87{
88 list_del_init(&pkt->node);
89}
90
91struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
92{
93 return list_first_entry_or_null(&pipe->list, struct usbhs_pkt, node);
94}
95
96static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
97 struct usbhs_fifo *fifo);
98static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
99 struct usbhs_pkt *pkt);
100#define usbhsf_dma_map(p) __usbhsf_dma_map_ctrl(p, 1)
101#define usbhsf_dma_unmap(p) __usbhsf_dma_map_ctrl(p, 0)
102static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map);
103static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable);
104static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable);
105struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
106{
107 struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
108 unsigned long flags;
109
110 /******************** spin lock ********************/
111 usbhs_lock(priv, flags);
112
113 usbhs_pipe_disable(pipe);
114
115 if (!pkt)
116 pkt = __usbhsf_pkt_get(pipe);
117
118 if (pkt) {
119 struct dma_chan *chan = NULL;
120
121 if (fifo)
122 chan = usbhsf_dma_chan_get(fifo, pkt);
123 if (chan)
124 usbhsf_dma_unmap(pkt);
125
126 usbhs_pipe_clear_without_sequence(pipe, 0, 0);
127 usbhs_pipe_running(pipe, 0);
128
129 __usbhsf_pkt_del(pkt);
130 }
131
132 if (fifo)
133 usbhsf_fifo_unselect(pipe, fifo);
134
135 usbhs_unlock(priv, flags);
136 /******************** spin unlock ******************/
137
138 return pkt;
139}
140
141enum {
142 USBHSF_PKT_PREPARE,
143 USBHSF_PKT_TRY_RUN,
144 USBHSF_PKT_DMA_DONE,
145};
146
147static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
148{
149 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
150 struct usbhs_pkt *pkt;
151 struct device *dev = usbhs_priv_to_dev(priv);
152 int (*func)(struct usbhs_pkt *pkt, int *is_done);
153 unsigned long flags;
154 int ret = 0;
155 int is_done = 0;
156
157 /******************** spin lock ********************/
158 usbhs_lock(priv, flags);
159
160 pkt = __usbhsf_pkt_get(pipe);
161 if (!pkt) {
162 ret = -EINVAL;
163 goto __usbhs_pkt_handler_end;
164 }
165
166 switch (type) {
167 case USBHSF_PKT_PREPARE:
168 func = pkt->handler->prepare;
169 break;
170 case USBHSF_PKT_TRY_RUN:
171 func = pkt->handler->try_run;
172 break;
173 case USBHSF_PKT_DMA_DONE:
174 func = pkt->handler->dma_done;
175 break;
176 default:
177 dev_err(dev, "unknown pkt handler\n");
178 goto __usbhs_pkt_handler_end;
179 }
180
181 if (likely(func))
182 ret = func(pkt, &is_done);
183
184 if (is_done)
185 __usbhsf_pkt_del(pkt);
186
187__usbhs_pkt_handler_end:
188 usbhs_unlock(priv, flags);
189 /******************** spin unlock ******************/
190
191 if (is_done) {
192 pkt->done(priv, pkt);
193 usbhs_pkt_start(pipe);
194 }
195
196 return ret;
197}
198
199void usbhs_pkt_start(struct usbhs_pipe *pipe)
200{
201 usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
202}
203
204/*
205 * irq enable/disable function
206 */
207#define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
208#define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
209#define usbhsf_irq_callback_ctrl(pipe, status, enable) \
210 ({ \
211 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); \
212 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
213 u16 status = (1 << usbhs_pipe_number(pipe)); \
214 if (!mod) \
215 return; \
216 if (enable) \
217 mod->status |= status; \
218 else \
219 mod->status &= ~status; \
220 usbhs_irq_callback_update(priv, mod); \
221 })
222
223static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
224{
225 /*
226 * And DCP pipe can NOT use "ready interrupt" for "send"
227 * it should use "empty" interrupt.
228 * see
229 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
230 *
231 * on the other hand, normal pipe can use "ready interrupt" for "send"
232 * even though it is single/double buffer
233 */
234 if (usbhs_pipe_is_dcp(pipe))
235 usbhsf_irq_empty_ctrl(pipe, enable);
236 else
237 usbhsf_irq_ready_ctrl(pipe, enable);
238}
239
240static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
241{
242 usbhsf_irq_ready_ctrl(pipe, enable);
243}
244
245/*
246 * FIFO ctrl
247 */
248static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
249 struct usbhs_fifo *fifo)
250{
251 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
252
253 usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
254}
255
256static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
257 struct usbhs_fifo *fifo)
258{
259 /* The FIFO port is accessible */
260 if (usbhs_read(priv, fifo->ctr) & FRDY)
261 return 0;
262
263 return -EBUSY;
264}
265
266static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
267 struct usbhs_fifo *fifo)
268{
269 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
270 int ret = 0;
271
272 if (!usbhs_pipe_is_dcp(pipe)) {
273 /*
274 * This driver checks the pipe condition first to avoid -EBUSY
275 * from usbhsf_fifo_barrier() if the pipe is RX direction and
276 * empty.
277 */
278 if (usbhs_pipe_is_dir_in(pipe))
279 ret = usbhs_pipe_is_accessible(pipe);
280 if (!ret)
281 ret = usbhsf_fifo_barrier(priv, fifo);
282 }
283
284 /*
285 * if non-DCP pipe, this driver should set BCLR when
286 * usbhsf_fifo_barrier() returns 0.
287 */
288 if (!ret)
289 usbhs_write(priv, fifo->ctr, BCLR);
290}
291
292static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
293 struct usbhs_fifo *fifo)
294{
295 return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
296}
297
298static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
299 struct usbhs_fifo *fifo)
300{
301 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
302
303 usbhs_pipe_select_fifo(pipe, NULL);
304 usbhs_write(priv, fifo->sel, 0);
305}
306
307static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
308 struct usbhs_fifo *fifo,
309 int write)
310{
311 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
312 struct device *dev = usbhs_priv_to_dev(priv);
313 int timeout = 1024;
314 u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
315 u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
316
317 if (usbhs_pipe_is_busy(pipe) ||
318 usbhsf_fifo_is_busy(fifo))
319 return -EBUSY;
320
321 if (usbhs_pipe_is_dcp(pipe)) {
322 base |= (1 == write) << 5; /* ISEL */
323
324 if (usbhs_mod_is_host(priv))
325 usbhs_dcp_dir_for_host(pipe, write);
326 }
327
328 /* "base" will be used below */
329 usbhs_write(priv, fifo->sel, base | MBW_32);
330
331 /* check ISEL and CURPIPE value */
332 while (timeout--) {
333 if (base == (mask & usbhs_read(priv, fifo->sel))) {
334 usbhs_pipe_select_fifo(pipe, fifo);
335 return 0;
336 }
337 udelay(10);
338 }
339
340 dev_err(dev, "fifo select error\n");
341
342 return -EIO;
343}
344
345/*
346 * DCP status stage
347 */
348static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
349{
350 struct usbhs_pipe *pipe = pkt->pipe;
351 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
352 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
353 struct device *dev = usbhs_priv_to_dev(priv);
354 int ret;
355
356 usbhs_pipe_disable(pipe);
357
358 ret = usbhsf_fifo_select(pipe, fifo, 1);
359 if (ret < 0) {
360 dev_err(dev, "%s() failed\n", __func__);
361 return ret;
362 }
363
364 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
365
366 usbhsf_fifo_clear(pipe, fifo);
367 usbhsf_send_terminator(pipe, fifo);
368
369 usbhsf_fifo_unselect(pipe, fifo);
370
371 usbhsf_tx_irq_ctrl(pipe, 1);
372 usbhs_pipe_enable(pipe);
373
374 return ret;
375}
376
377static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
378{
379 struct usbhs_pipe *pipe = pkt->pipe;
380 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
381 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
382 struct device *dev = usbhs_priv_to_dev(priv);
383 int ret;
384
385 usbhs_pipe_disable(pipe);
386
387 ret = usbhsf_fifo_select(pipe, fifo, 0);
388 if (ret < 0) {
389 dev_err(dev, "%s() fail\n", __func__);
390 return ret;
391 }
392
393 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
394 usbhsf_fifo_clear(pipe, fifo);
395
396 usbhsf_fifo_unselect(pipe, fifo);
397
398 usbhsf_rx_irq_ctrl(pipe, 1);
399 usbhs_pipe_enable(pipe);
400
401 return ret;
402
403}
404
405static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
406{
407 struct usbhs_pipe *pipe = pkt->pipe;
408
409 if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
410 usbhsf_tx_irq_ctrl(pipe, 0);
411 else
412 usbhsf_rx_irq_ctrl(pipe, 0);
413
414 pkt->actual = pkt->length;
415 *is_done = 1;
416
417 return 0;
418}
419
420const struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
421 .prepare = usbhs_dcp_dir_switch_to_write,
422 .try_run = usbhs_dcp_dir_switch_done,
423};
424
425const struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
426 .prepare = usbhs_dcp_dir_switch_to_read,
427 .try_run = usbhs_dcp_dir_switch_done,
428};
429
430/*
431 * DCP data stage (push)
432 */
433static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
434{
435 struct usbhs_pipe *pipe = pkt->pipe;
436
437 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
438
439 /*
440 * change handler to PIO push
441 */
442 pkt->handler = &usbhs_fifo_pio_push_handler;
443
444 return pkt->handler->prepare(pkt, is_done);
445}
446
447const struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
448 .prepare = usbhsf_dcp_data_stage_try_push,
449};
450
451/*
452 * DCP data stage (pop)
453 */
454static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
455 int *is_done)
456{
457 struct usbhs_pipe *pipe = pkt->pipe;
458 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
459 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
460
461 if (usbhs_pipe_is_busy(pipe))
462 return 0;
463
464 /*
465 * prepare pop for DCP should
466 * - change DCP direction,
467 * - clear fifo
468 * - DATA1
469 */
470 usbhs_pipe_disable(pipe);
471
472 usbhs_pipe_sequence_data1(pipe); /* DATA1 */
473
474 usbhsf_fifo_select(pipe, fifo, 0);
475 usbhsf_fifo_clear(pipe, fifo);
476 usbhsf_fifo_unselect(pipe, fifo);
477
478 /*
479 * change handler to PIO pop
480 */
481 pkt->handler = &usbhs_fifo_pio_pop_handler;
482
483 return pkt->handler->prepare(pkt, is_done);
484}
485
486const struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
487 .prepare = usbhsf_dcp_data_stage_prepare_pop,
488};
489
490/*
491 * PIO push handler
492 */
493static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
494{
495 struct usbhs_pipe *pipe = pkt->pipe;
496 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
497 struct device *dev = usbhs_priv_to_dev(priv);
498 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
499 void __iomem *addr = priv->base + fifo->port;
500 u8 *buf;
501 int maxp = usbhs_pipe_get_maxpacket(pipe);
502 int total_len;
503 int i, ret, len;
504 int is_short;
505
506 usbhs_pipe_data_sequence(pipe, pkt->sequence);
507 pkt->sequence = -1; /* -1 sequence will be ignored */
508
509 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
510
511 ret = usbhsf_fifo_select(pipe, fifo, 1);
512 if (ret < 0)
513 return 0;
514
515 ret = usbhs_pipe_is_accessible(pipe);
516 if (ret < 0) {
517 /* inaccessible pipe is not an error */
518 ret = 0;
519 goto usbhs_fifo_write_busy;
520 }
521
522 ret = usbhsf_fifo_barrier(priv, fifo);
523 if (ret < 0)
524 goto usbhs_fifo_write_busy;
525
526 buf = pkt->buf + pkt->actual;
527 len = pkt->length - pkt->actual;
528 len = min(len, maxp);
529 total_len = len;
530 is_short = total_len < maxp;
531
532 /*
533 * FIXME
534 *
535 * 32-bit access only
536 */
537 if (len >= 4 && !((unsigned long)buf & 0x03)) {
538 iowrite32_rep(addr, buf, len / 4);
539 len %= 4;
540 buf += total_len - len;
541 }
542
543 /* the rest operation */
544 if (usbhs_get_dparam(priv, cfifo_byte_addr)) {
545 for (i = 0; i < len; i++)
546 iowrite8(buf[i], addr + (i & 0x03));
547 } else {
548 for (i = 0; i < len; i++)
549 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
550 }
551
552 /*
553 * variable update
554 */
555 pkt->actual += total_len;
556
557 if (pkt->actual < pkt->length)
558 *is_done = 0; /* there are remainder data */
559 else if (is_short)
560 *is_done = 1; /* short packet */
561 else
562 *is_done = !pkt->zero; /* send zero packet ? */
563
564 /*
565 * pipe/irq handling
566 */
567 if (is_short)
568 usbhsf_send_terminator(pipe, fifo);
569
570 usbhsf_tx_irq_ctrl(pipe, !*is_done);
571 usbhs_pipe_running(pipe, !*is_done);
572 usbhs_pipe_enable(pipe);
573
574 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
575 usbhs_pipe_number(pipe),
576 pkt->length, pkt->actual, *is_done, pkt->zero);
577
578 usbhsf_fifo_unselect(pipe, fifo);
579
580 return 0;
581
582usbhs_fifo_write_busy:
583 usbhsf_fifo_unselect(pipe, fifo);
584
585 /*
586 * pipe is busy.
587 * retry in interrupt
588 */
589 usbhsf_tx_irq_ctrl(pipe, 1);
590 usbhs_pipe_running(pipe, 1);
591
592 return ret;
593}
594
595static int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done)
596{
597 if (usbhs_pipe_is_running(pkt->pipe))
598 return 0;
599
600 return usbhsf_pio_try_push(pkt, is_done);
601}
602
603const struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
604 .prepare = usbhsf_pio_prepare_push,
605 .try_run = usbhsf_pio_try_push,
606};
607
608/*
609 * PIO pop handler
610 */
611static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
612{
613 struct usbhs_pipe *pipe = pkt->pipe;
614 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
615 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
616
617 if (usbhs_pipe_is_busy(pipe))
618 return 0;
619
620 if (usbhs_pipe_is_running(pipe))
621 return 0;
622
623 /*
624 * pipe enable to prepare packet receive
625 */
626 usbhs_pipe_data_sequence(pipe, pkt->sequence);
627 pkt->sequence = -1; /* -1 sequence will be ignored */
628
629 if (usbhs_pipe_is_dcp(pipe))
630 usbhsf_fifo_clear(pipe, fifo);
631
632 usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
633 usbhs_pipe_enable(pipe);
634 usbhs_pipe_running(pipe, 1);
635 usbhsf_rx_irq_ctrl(pipe, 1);
636
637 return 0;
638}
639
640static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
641{
642 struct usbhs_pipe *pipe = pkt->pipe;
643 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
644 struct device *dev = usbhs_priv_to_dev(priv);
645 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
646 void __iomem *addr = priv->base + fifo->port;
647 u8 *buf;
648 u32 data = 0;
649 int maxp = usbhs_pipe_get_maxpacket(pipe);
650 int rcv_len, len;
651 int i, ret;
652 int total_len = 0;
653
654 ret = usbhsf_fifo_select(pipe, fifo, 0);
655 if (ret < 0)
656 return 0;
657
658 ret = usbhsf_fifo_barrier(priv, fifo);
659 if (ret < 0)
660 goto usbhs_fifo_read_busy;
661
662 rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
663
664 buf = pkt->buf + pkt->actual;
665 len = pkt->length - pkt->actual;
666 len = min(len, rcv_len);
667 total_len = len;
668
669 /*
670 * update actual length first here to decide disable pipe.
671 * if this pipe keeps BUF status and all data were popped,
672 * then, next interrupt/token will be issued again
673 */
674 pkt->actual += total_len;
675
676 if ((pkt->actual == pkt->length) || /* receive all data */
677 (total_len < maxp)) { /* short packet */
678 *is_done = 1;
679 usbhsf_rx_irq_ctrl(pipe, 0);
680 usbhs_pipe_running(pipe, 0);
681 /*
682 * If function mode, since this controller is possible to enter
683 * Control Write status stage at this timing, this driver
684 * should not disable the pipe. If such a case happens, this
685 * controller is not able to complete the status stage.
686 */
687 if (!usbhs_mod_is_host(priv) && !usbhs_pipe_is_dcp(pipe))
688 usbhs_pipe_disable(pipe); /* disable pipe first */
689 }
690
691 /*
692 * Buffer clear if Zero-Length packet
693 *
694 * see
695 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
696 */
697 if (0 == rcv_len) {
698 pkt->zero = 1;
699 usbhsf_fifo_clear(pipe, fifo);
700 goto usbhs_fifo_read_end;
701 }
702
703 /*
704 * FIXME
705 *
706 * 32-bit access only
707 */
708 if (len >= 4 && !((unsigned long)buf & 0x03)) {
709 ioread32_rep(addr, buf, len / 4);
710 len %= 4;
711 buf += total_len - len;
712 }
713
714 /* the rest operation */
715 for (i = 0; i < len; i++) {
716 if (!(i & 0x03))
717 data = ioread32(addr);
718
719 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
720 }
721
722usbhs_fifo_read_end:
723 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
724 usbhs_pipe_number(pipe),
725 pkt->length, pkt->actual, *is_done, pkt->zero);
726
727usbhs_fifo_read_busy:
728 usbhsf_fifo_unselect(pipe, fifo);
729
730 return ret;
731}
732
733const struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
734 .prepare = usbhsf_prepare_pop,
735 .try_run = usbhsf_pio_try_pop,
736};
737
738/*
739 * DCP ctrol statge handler
740 */
741static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
742{
743 usbhs_dcp_control_transfer_done(pkt->pipe);
744
745 *is_done = 1;
746
747 return 0;
748}
749
750const struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
751 .prepare = usbhsf_ctrl_stage_end,
752 .try_run = usbhsf_ctrl_stage_end,
753};
754
755/*
756 * DMA fifo functions
757 */
758static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
759 struct usbhs_pkt *pkt)
760{
761 if (&usbhs_fifo_dma_push_handler == pkt->handler)
762 return fifo->tx_chan;
763
764 if (&usbhs_fifo_dma_pop_handler == pkt->handler)
765 return fifo->rx_chan;
766
767 return NULL;
768}
769
770#define usbhsf_dma_start(p, f) __usbhsf_dma_ctrl(p, f, DREQE)
771#define usbhsf_dma_stop(p, f) __usbhsf_dma_ctrl(p, f, 0)
772static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
773 struct usbhs_fifo *fifo,
774 u16 dreqe)
775{
776 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
777
778 usbhs_bset(priv, fifo->sel, DREQE, dreqe);
779}
780
781static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
782{
783 struct usbhs_pipe *pipe = pkt->pipe;
784 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
785 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
786
787 return info->dma_map_ctrl(pkt, map);
788}
789
790/*
791 * DMA push handler
792 */
793static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
794{
795 struct usbhs_pipe *pipe = pkt->pipe;
796
797 if (usbhs_pipe_is_busy(pipe))
798 return 0;
799
800 /*
801 * change handler to PIO
802 */
803 pkt->handler = &usbhs_fifo_pio_push_handler;
804
805 return pkt->handler->prepare(pkt, is_done);
806}
807
808static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
809{
810 struct usbhs_pipe *pipe = pkt->pipe;
811 int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
812
813 pkt->actual += pkt->trans;
814
815 if (pkt->actual < pkt->length)
816 *is_done = 0; /* there are remainder data */
817 else if (is_short)
818 *is_done = 1; /* short packet */
819 else
820 *is_done = !pkt->zero; /* send zero packet? */
821
822 usbhs_pipe_running(pipe, !*is_done);
823
824 usbhsf_dma_stop(pipe, pipe->fifo);
825 usbhsf_dma_unmap(pkt);
826 usbhsf_fifo_unselect(pipe, pipe->fifo);
827
828 if (!*is_done) {
829 /* change handler to PIO */
830 pkt->handler = &usbhs_fifo_pio_push_handler;
831 return pkt->handler->try_run(pkt, is_done);
832 }
833
834 return 0;
835}
836
837const struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
838 .prepare = usbhsf_dma_prepare_push,
839 .dma_done = usbhsf_dma_push_done,
840};
841
842/*
843 * DMA pop handler
844 */
845
846static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
847 int *is_done)
848{
849 return usbhsf_prepare_pop(pkt, is_done);
850}
851
852static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
853{
854 return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
855}
856
857static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
858{
859 struct usbhs_pipe *pipe = pkt->pipe;
860
861 if (usbhs_pipe_is_busy(pipe))
862 return 0;
863
864 /*
865 * change handler to PIO
866 */
867 pkt->handler = &usbhs_fifo_pio_pop_handler;
868
869 return pkt->handler->try_run(pkt, is_done);
870}
871
872static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
873{
874 struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
875
876 BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
877
878 return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
879}
880
881static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
882{
883 struct usbhs_pipe *pipe = pkt->pipe;
884 int maxp = usbhs_pipe_get_maxpacket(pipe);
885
886 usbhsf_dma_stop(pipe, pipe->fifo);
887 usbhsf_dma_unmap(pkt);
888 usbhsf_fifo_unselect(pipe, pipe->fifo);
889
890 pkt->actual += pkt->trans;
891
892 if ((pkt->actual == pkt->length) || /* receive all data */
893 (pkt->trans < maxp)) { /* short packet */
894 *is_done = 1;
895 usbhs_pipe_running(pipe, 0);
896 } else {
897 /* re-enable */
898 usbhs_pipe_running(pipe, 0);
899 usbhsf_prepare_pop(pkt, is_done);
900 }
901
902 return 0;
903}
904
905static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
906{
907 return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
908}
909
910const struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
911 .prepare = usbhsf_dma_prepare_pop,
912 .try_run = usbhsf_dma_try_pop,
913 .dma_done = usbhsf_dma_pop_done
914};
915
916/*
917 * irq functions
918 */
919static int usbhsf_irq_empty(struct usbhs_priv *priv,
920 struct usbhs_irq_state *irq_state)
921{
922 struct usbhs_pipe *pipe;
923 struct device *dev = usbhs_priv_to_dev(priv);
924 int i, ret;
925
926 if (!irq_state->bempsts) {
927 dev_err(dev, "debug %s !!\n", __func__);
928 return -EIO;
929 }
930
931 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
932
933 /*
934 * search interrupted "pipe"
935 * not "uep".
936 */
937 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
938 if (!(irq_state->bempsts & (1 << i)))
939 continue;
940
941 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
942 if (ret < 0)
943 dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
944 }
945
946 return 0;
947}
948
949static int usbhsf_irq_ready(struct usbhs_priv *priv,
950 struct usbhs_irq_state *irq_state)
951{
952 struct usbhs_pipe *pipe;
953 struct device *dev = usbhs_priv_to_dev(priv);
954 int i, ret;
955
956 if (!irq_state->brdysts) {
957 dev_err(dev, "debug %s !!\n", __func__);
958 return -EIO;
959 }
960
961 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
962
963 /*
964 * search interrupted "pipe"
965 * not "uep".
966 */
967 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
968 if (!(irq_state->brdysts & (1 << i)))
969 continue;
970
971 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
972 if (ret < 0)
973 dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
974 }
975
976 return 0;
977}
978
979void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
980{
981 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
982 struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
983
984 /* clear DCP FIFO of transmission */
985 if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
986 return;
987 usbhsf_fifo_clear(pipe, fifo);
988 usbhsf_fifo_unselect(pipe, fifo);
989
990 /* clear DCP FIFO of reception */
991 if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
992 return;
993 usbhsf_fifo_clear(pipe, fifo);
994 usbhsf_fifo_unselect(pipe, fifo);
995}
996
997/*
998 * fifo init
999 */
1000void usbhs_fifo_init(struct usbhs_priv *priv)
1001{
1002 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1003 struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
1004 struct usbhs_fifo *dfifo;
1005 int i;
1006
1007 mod->irq_empty = usbhsf_irq_empty;
1008 mod->irq_ready = usbhsf_irq_ready;
1009 mod->irq_bempsts = 0;
1010 mod->irq_brdysts = 0;
1011
1012 cfifo->pipe = NULL;
1013 usbhs_for_each_dfifo(priv, dfifo, i)
1014 dfifo->pipe = NULL;
1015}
1016
1017void usbhs_fifo_quit(struct usbhs_priv *priv)
1018{
1019 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1020
1021 mod->irq_empty = NULL;
1022 mod->irq_ready = NULL;
1023 mod->irq_bempsts = 0;
1024 mod->irq_brdysts = 0;
1025}
1026
1027#define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port) \
1028do { \
1029 fifo = usbhsf_get_dnfifo(priv, channel); \
1030 fifo->name = "D"#channel"FIFO"; \
1031 fifo->port = fifo_port; \
1032 fifo->sel = D##channel##FIFOSEL; \
1033 fifo->ctr = D##channel##FIFOCTR; \
1034 fifo->tx_slave.shdma_slave.slave_id = \
1035 usbhs_get_dparam(priv, d##channel##_tx_id); \
1036 fifo->rx_slave.shdma_slave.slave_id = \
1037 usbhs_get_dparam(priv, d##channel##_rx_id); \
1038} while (0)
1039
1040#define USBHS_DFIFO_INIT(priv, fifo, channel) \
1041 __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
1042#define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel) \
1043 __USBHS_DFIFO_INIT(priv, fifo, channel, 0)
1044
1045int usbhs_fifo_probe(struct usbhs_priv *priv)
1046{
1047 struct usbhs_fifo *fifo;
1048
1049 /* CFIFO */
1050 fifo = usbhsf_get_cfifo(priv);
1051 fifo->name = "CFIFO";
1052 fifo->port = CFIFO;
1053 fifo->sel = CFIFOSEL;
1054 fifo->ctr = CFIFOCTR;
1055
1056 /* DFIFO */
1057 USBHS_DFIFO_INIT(priv, fifo, 0);
1058 USBHS_DFIFO_INIT(priv, fifo, 1);
1059 USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2);
1060 USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3);
1061
1062 return 0;
1063}
1064
1065void usbhs_fifo_remove(struct usbhs_priv *priv)
1066{
1067}