GNU Linux-libre 4.19.295-gnu1
[releases.git] / drivers / usb / renesas_usbhs / fifo.c
1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  * Renesas USB driver
4  *
5  * Copyright (C) 2011 Renesas Solutions Corp.
6  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7  */
8 #include <linux/delay.h>
9 #include <linux/io.h>
10 #include <linux/scatterlist.h>
11 #include "common.h"
12 #include "pipe.h"
13
14 #define usbhsf_get_cfifo(p)     (&((p)->fifo_info.cfifo))
15 #define usbhsf_is_cfifo(p, f)   (usbhsf_get_cfifo(p) == f)
16
17 #define usbhsf_fifo_is_busy(f)  ((f)->pipe) /* see usbhs_pipe_select_fifo */
18
19 /*
20  *              packet initialize
21  */
22 void usbhs_pkt_init(struct usbhs_pkt *pkt)
23 {
24         INIT_LIST_HEAD(&pkt->node);
25 }
26
27 /*
28  *              packet control function
29  */
30 static int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
31 {
32         struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
33         struct device *dev = usbhs_priv_to_dev(priv);
34
35         dev_err(dev, "null handler\n");
36
37         return -EINVAL;
38 }
39
40 static const struct usbhs_pkt_handle usbhsf_null_handler = {
41         .prepare = usbhsf_null_handle,
42         .try_run = usbhsf_null_handle,
43 };
44
45 void usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
46                     void (*done)(struct usbhs_priv *priv,
47                                  struct usbhs_pkt *pkt),
48                     void *buf, int len, int zero, int sequence)
49 {
50         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
51         struct device *dev = usbhs_priv_to_dev(priv);
52         unsigned long flags;
53
54         if (!done) {
55                 dev_err(dev, "no done function\n");
56                 return;
57         }
58
59         /********************  spin lock ********************/
60         usbhs_lock(priv, flags);
61
62         if (!pipe->handler) {
63                 dev_err(dev, "no handler function\n");
64                 pipe->handler = &usbhsf_null_handler;
65         }
66
67         list_move_tail(&pkt->node, &pipe->list);
68
69         /*
70          * each pkt must hold own handler.
71          * because handler might be changed by its situation.
72          * dma handler -> pio handler.
73          */
74         pkt->pipe       = pipe;
75         pkt->buf        = buf;
76         pkt->handler    = pipe->handler;
77         pkt->length     = len;
78         pkt->zero       = zero;
79         pkt->actual     = 0;
80         pkt->done       = done;
81         pkt->sequence   = sequence;
82
83         usbhs_unlock(priv, flags);
84         /********************  spin unlock ******************/
85 }
86
87 static void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
88 {
89         list_del_init(&pkt->node);
90 }
91
92 struct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
93 {
94         return list_first_entry_or_null(&pipe->list, struct usbhs_pkt, node);
95 }
96
97 static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
98                                  struct usbhs_fifo *fifo);
99 static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
100                                             struct usbhs_pkt *pkt);
101 #define usbhsf_dma_map(p)       __usbhsf_dma_map_ctrl(p, 1)
102 #define usbhsf_dma_unmap(p)     __usbhsf_dma_map_ctrl(p, 0)
103 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map);
104 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable);
105 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable);
106 struct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
107 {
108         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
109         struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
110         unsigned long flags;
111
112         /********************  spin lock ********************/
113         usbhs_lock(priv, flags);
114
115         usbhs_pipe_disable(pipe);
116
117         if (!pkt)
118                 pkt = __usbhsf_pkt_get(pipe);
119
120         if (pkt) {
121                 struct dma_chan *chan = NULL;
122
123                 if (fifo)
124                         chan = usbhsf_dma_chan_get(fifo, pkt);
125                 if (chan) {
126                         dmaengine_terminate_all(chan);
127                         usbhsf_dma_unmap(pkt);
128                 } else {
129                         if (usbhs_pipe_is_dir_in(pipe))
130                                 usbhsf_rx_irq_ctrl(pipe, 0);
131                         else
132                                 usbhsf_tx_irq_ctrl(pipe, 0);
133                 }
134
135                 usbhs_pipe_clear_without_sequence(pipe, 0, 0);
136                 usbhs_pipe_running(pipe, 0);
137
138                 __usbhsf_pkt_del(pkt);
139         }
140
141         if (fifo)
142                 usbhsf_fifo_unselect(pipe, fifo);
143
144         usbhs_unlock(priv, flags);
145         /********************  spin unlock ******************/
146
147         return pkt;
148 }
149
150 enum {
151         USBHSF_PKT_PREPARE,
152         USBHSF_PKT_TRY_RUN,
153         USBHSF_PKT_DMA_DONE,
154 };
155
156 static int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
157 {
158         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
159         struct usbhs_pkt *pkt;
160         struct device *dev = usbhs_priv_to_dev(priv);
161         int (*func)(struct usbhs_pkt *pkt, int *is_done);
162         unsigned long flags;
163         int ret = 0;
164         int is_done = 0;
165
166         /********************  spin lock ********************/
167         usbhs_lock(priv, flags);
168
169         pkt = __usbhsf_pkt_get(pipe);
170         if (!pkt)
171                 goto __usbhs_pkt_handler_end;
172
173         switch (type) {
174         case USBHSF_PKT_PREPARE:
175                 func = pkt->handler->prepare;
176                 break;
177         case USBHSF_PKT_TRY_RUN:
178                 func = pkt->handler->try_run;
179                 break;
180         case USBHSF_PKT_DMA_DONE:
181                 func = pkt->handler->dma_done;
182                 break;
183         default:
184                 dev_err(dev, "unknown pkt handler\n");
185                 goto __usbhs_pkt_handler_end;
186         }
187
188         if (likely(func))
189                 ret = func(pkt, &is_done);
190
191         if (is_done)
192                 __usbhsf_pkt_del(pkt);
193
194 __usbhs_pkt_handler_end:
195         usbhs_unlock(priv, flags);
196         /********************  spin unlock ******************/
197
198         if (is_done) {
199                 pkt->done(priv, pkt);
200                 usbhs_pkt_start(pipe);
201         }
202
203         return ret;
204 }
205
206 void usbhs_pkt_start(struct usbhs_pipe *pipe)
207 {
208         usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
209 }
210
211 /*
212  *              irq enable/disable function
213  */
214 #define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
215 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
216 #define usbhsf_irq_callback_ctrl(pipe, status, enable)                  \
217         ({                                                              \
218                 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);     \
219                 struct usbhs_mod *mod = usbhs_mod_get_current(priv);    \
220                 u16 status = (1 << usbhs_pipe_number(pipe));            \
221                 if (!mod)                                               \
222                         return;                                         \
223                 if (enable)                                             \
224                         mod->status |= status;                          \
225                 else                                                    \
226                         mod->status &= ~status;                         \
227                 usbhs_irq_callback_update(priv, mod);                   \
228         })
229
230 static void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
231 {
232         /*
233          * And DCP pipe can NOT use "ready interrupt" for "send"
234          * it should use "empty" interrupt.
235          * see
236          *   "Operation" - "Interrupt Function" - "BRDY Interrupt"
237          *
238          * on the other hand, normal pipe can use "ready interrupt" for "send"
239          * even though it is single/double buffer
240          */
241         if (usbhs_pipe_is_dcp(pipe))
242                 usbhsf_irq_empty_ctrl(pipe, enable);
243         else
244                 usbhsf_irq_ready_ctrl(pipe, enable);
245 }
246
247 static void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
248 {
249         usbhsf_irq_ready_ctrl(pipe, enable);
250 }
251
252 /*
253  *              FIFO ctrl
254  */
255 static void usbhsf_send_terminator(struct usbhs_pipe *pipe,
256                                    struct usbhs_fifo *fifo)
257 {
258         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
259
260         usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
261 }
262
263 static int usbhsf_fifo_barrier(struct usbhs_priv *priv,
264                                struct usbhs_fifo *fifo)
265 {
266         /* The FIFO port is accessible */
267         if (usbhs_read(priv, fifo->ctr) & FRDY)
268                 return 0;
269
270         return -EBUSY;
271 }
272
273 static void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
274                               struct usbhs_fifo *fifo)
275 {
276         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
277         int ret = 0;
278
279         if (!usbhs_pipe_is_dcp(pipe)) {
280                 /*
281                  * This driver checks the pipe condition first to avoid -EBUSY
282                  * from usbhsf_fifo_barrier() if the pipe is RX direction and
283                  * empty.
284                  */
285                 if (usbhs_pipe_is_dir_in(pipe))
286                         ret = usbhs_pipe_is_accessible(pipe);
287                 if (!ret)
288                         ret = usbhsf_fifo_barrier(priv, fifo);
289         }
290
291         /*
292          * if non-DCP pipe, this driver should set BCLR when
293          * usbhsf_fifo_barrier() returns 0.
294          */
295         if (!ret)
296                 usbhs_write(priv, fifo->ctr, BCLR);
297 }
298
299 static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
300                                struct usbhs_fifo *fifo)
301 {
302         return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
303 }
304
305 static void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
306                                  struct usbhs_fifo *fifo)
307 {
308         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
309
310         usbhs_pipe_select_fifo(pipe, NULL);
311         usbhs_write(priv, fifo->sel, 0);
312 }
313
314 static int usbhsf_fifo_select(struct usbhs_pipe *pipe,
315                               struct usbhs_fifo *fifo,
316                               int write)
317 {
318         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
319         struct device *dev = usbhs_priv_to_dev(priv);
320         int timeout = 1024;
321         u16 mask = ((1 << 5) | 0xF);            /* mask of ISEL | CURPIPE */
322         u16 base = usbhs_pipe_number(pipe);     /* CURPIPE */
323
324         if (usbhs_pipe_is_busy(pipe) ||
325             usbhsf_fifo_is_busy(fifo))
326                 return -EBUSY;
327
328         if (usbhs_pipe_is_dcp(pipe)) {
329                 base |= (1 == write) << 5;      /* ISEL */
330
331                 if (usbhs_mod_is_host(priv))
332                         usbhs_dcp_dir_for_host(pipe, write);
333         }
334
335         /* "base" will be used below  */
336         if (usbhs_get_dparam(priv, has_sudmac) && !usbhsf_is_cfifo(priv, fifo))
337                 usbhs_write(priv, fifo->sel, base);
338         else
339                 usbhs_write(priv, fifo->sel, base | MBW_32);
340
341         /* check ISEL and CURPIPE value */
342         while (timeout--) {
343                 if (base == (mask & usbhs_read(priv, fifo->sel))) {
344                         usbhs_pipe_select_fifo(pipe, fifo);
345                         return 0;
346                 }
347                 udelay(10);
348         }
349
350         dev_err(dev, "fifo select error\n");
351
352         return -EIO;
353 }
354
355 /*
356  *              DCP status stage
357  */
358 static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
359 {
360         struct usbhs_pipe *pipe = pkt->pipe;
361         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
362         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
363         struct device *dev = usbhs_priv_to_dev(priv);
364         int ret;
365
366         usbhs_pipe_disable(pipe);
367
368         ret = usbhsf_fifo_select(pipe, fifo, 1);
369         if (ret < 0) {
370                 dev_err(dev, "%s() faile\n", __func__);
371                 return ret;
372         }
373
374         usbhs_pipe_sequence_data1(pipe); /* DATA1 */
375
376         usbhsf_fifo_clear(pipe, fifo);
377         usbhsf_send_terminator(pipe, fifo);
378
379         usbhsf_fifo_unselect(pipe, fifo);
380
381         usbhsf_tx_irq_ctrl(pipe, 1);
382         usbhs_pipe_enable(pipe);
383
384         return ret;
385 }
386
387 static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
388 {
389         struct usbhs_pipe *pipe = pkt->pipe;
390         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
391         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
392         struct device *dev = usbhs_priv_to_dev(priv);
393         int ret;
394
395         usbhs_pipe_disable(pipe);
396
397         ret = usbhsf_fifo_select(pipe, fifo, 0);
398         if (ret < 0) {
399                 dev_err(dev, "%s() fail\n", __func__);
400                 return ret;
401         }
402
403         usbhs_pipe_sequence_data1(pipe); /* DATA1 */
404         usbhsf_fifo_clear(pipe, fifo);
405
406         usbhsf_fifo_unselect(pipe, fifo);
407
408         usbhsf_rx_irq_ctrl(pipe, 1);
409         usbhs_pipe_enable(pipe);
410
411         return ret;
412
413 }
414
415 static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
416 {
417         struct usbhs_pipe *pipe = pkt->pipe;
418
419         if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
420                 usbhsf_tx_irq_ctrl(pipe, 0);
421         else
422                 usbhsf_rx_irq_ctrl(pipe, 0);
423
424         pkt->actual = pkt->length;
425         *is_done = 1;
426
427         return 0;
428 }
429
430 const struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
431         .prepare = usbhs_dcp_dir_switch_to_write,
432         .try_run = usbhs_dcp_dir_switch_done,
433 };
434
435 const struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
436         .prepare = usbhs_dcp_dir_switch_to_read,
437         .try_run = usbhs_dcp_dir_switch_done,
438 };
439
440 /*
441  *              DCP data stage (push)
442  */
443 static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
444 {
445         struct usbhs_pipe *pipe = pkt->pipe;
446
447         usbhs_pipe_sequence_data1(pipe); /* DATA1 */
448
449         /*
450          * change handler to PIO push
451          */
452         pkt->handler = &usbhs_fifo_pio_push_handler;
453
454         return pkt->handler->prepare(pkt, is_done);
455 }
456
457 const struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
458         .prepare = usbhsf_dcp_data_stage_try_push,
459 };
460
461 /*
462  *              DCP data stage (pop)
463  */
464 static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
465                                              int *is_done)
466 {
467         struct usbhs_pipe *pipe = pkt->pipe;
468         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
469         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
470
471         if (usbhs_pipe_is_busy(pipe))
472                 return 0;
473
474         /*
475          * prepare pop for DCP should
476          *  - change DCP direction,
477          *  - clear fifo
478          *  - DATA1
479          */
480         usbhs_pipe_disable(pipe);
481
482         usbhs_pipe_sequence_data1(pipe); /* DATA1 */
483
484         usbhsf_fifo_select(pipe, fifo, 0);
485         usbhsf_fifo_clear(pipe, fifo);
486         usbhsf_fifo_unselect(pipe, fifo);
487
488         /*
489          * change handler to PIO pop
490          */
491         pkt->handler = &usbhs_fifo_pio_pop_handler;
492
493         return pkt->handler->prepare(pkt, is_done);
494 }
495
496 const struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
497         .prepare = usbhsf_dcp_data_stage_prepare_pop,
498 };
499
500 /*
501  *              PIO push handler
502  */
503 static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
504 {
505         struct usbhs_pipe *pipe = pkt->pipe;
506         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
507         struct device *dev = usbhs_priv_to_dev(priv);
508         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
509         void __iomem *addr = priv->base + fifo->port;
510         u8 *buf;
511         int maxp = usbhs_pipe_get_maxpacket(pipe);
512         int total_len;
513         int i, ret, len;
514         int is_short;
515
516         usbhs_pipe_data_sequence(pipe, pkt->sequence);
517         pkt->sequence = -1; /* -1 sequence will be ignored */
518
519         usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
520
521         ret = usbhsf_fifo_select(pipe, fifo, 1);
522         if (ret < 0)
523                 return 0;
524
525         ret = usbhs_pipe_is_accessible(pipe);
526         if (ret < 0) {
527                 /* inaccessible pipe is not an error */
528                 ret = 0;
529                 goto usbhs_fifo_write_busy;
530         }
531
532         ret = usbhsf_fifo_barrier(priv, fifo);
533         if (ret < 0)
534                 goto usbhs_fifo_write_busy;
535
536         buf             = pkt->buf    + pkt->actual;
537         len             = pkt->length - pkt->actual;
538         len             = min(len, maxp);
539         total_len       = len;
540         is_short        = total_len < maxp;
541
542         /*
543          * FIXME
544          *
545          * 32-bit access only
546          */
547         if (len >= 4 && !((unsigned long)buf & 0x03)) {
548                 iowrite32_rep(addr, buf, len / 4);
549                 len %= 4;
550                 buf += total_len - len;
551         }
552
553         /* the rest operation */
554         for (i = 0; i < len; i++)
555                 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
556
557         /*
558          * variable update
559          */
560         pkt->actual += total_len;
561
562         if (pkt->actual < pkt->length)
563                 *is_done = 0;           /* there are remainder data */
564         else if (is_short)
565                 *is_done = 1;           /* short packet */
566         else
567                 *is_done = !pkt->zero;  /* send zero packet ? */
568
569         /*
570          * pipe/irq handling
571          */
572         if (is_short)
573                 usbhsf_send_terminator(pipe, fifo);
574
575         usbhsf_tx_irq_ctrl(pipe, !*is_done);
576         usbhs_pipe_running(pipe, !*is_done);
577         usbhs_pipe_enable(pipe);
578
579         dev_dbg(dev, "  send %d (%d/ %d/ %d/ %d)\n",
580                 usbhs_pipe_number(pipe),
581                 pkt->length, pkt->actual, *is_done, pkt->zero);
582
583         usbhsf_fifo_unselect(pipe, fifo);
584
585         return 0;
586
587 usbhs_fifo_write_busy:
588         usbhsf_fifo_unselect(pipe, fifo);
589
590         /*
591          * pipe is busy.
592          * retry in interrupt
593          */
594         usbhsf_tx_irq_ctrl(pipe, 1);
595         usbhs_pipe_running(pipe, 1);
596
597         return ret;
598 }
599
600 static int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done)
601 {
602         if (usbhs_pipe_is_running(pkt->pipe))
603                 return 0;
604
605         return usbhsf_pio_try_push(pkt, is_done);
606 }
607
608 const struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
609         .prepare = usbhsf_pio_prepare_push,
610         .try_run = usbhsf_pio_try_push,
611 };
612
613 /*
614  *              PIO pop handler
615  */
616 static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
617 {
618         struct usbhs_pipe *pipe = pkt->pipe;
619         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
620         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
621
622         if (usbhs_pipe_is_busy(pipe))
623                 return 0;
624
625         if (usbhs_pipe_is_running(pipe))
626                 return 0;
627
628         /*
629          * pipe enable to prepare packet receive
630          */
631         usbhs_pipe_data_sequence(pipe, pkt->sequence);
632         pkt->sequence = -1; /* -1 sequence will be ignored */
633
634         if (usbhs_pipe_is_dcp(pipe))
635                 usbhsf_fifo_clear(pipe, fifo);
636
637         usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
638         usbhs_pipe_enable(pipe);
639         usbhs_pipe_running(pipe, 1);
640         usbhsf_rx_irq_ctrl(pipe, 1);
641
642         return 0;
643 }
644
645 static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
646 {
647         struct usbhs_pipe *pipe = pkt->pipe;
648         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
649         struct device *dev = usbhs_priv_to_dev(priv);
650         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
651         void __iomem *addr = priv->base + fifo->port;
652         u8 *buf;
653         u32 data = 0;
654         int maxp = usbhs_pipe_get_maxpacket(pipe);
655         int rcv_len, len;
656         int i, ret;
657         int total_len = 0;
658
659         ret = usbhsf_fifo_select(pipe, fifo, 0);
660         if (ret < 0)
661                 return 0;
662
663         ret = usbhsf_fifo_barrier(priv, fifo);
664         if (ret < 0)
665                 goto usbhs_fifo_read_busy;
666
667         rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
668
669         buf             = pkt->buf    + pkt->actual;
670         len             = pkt->length - pkt->actual;
671         len             = min(len, rcv_len);
672         total_len       = len;
673
674         /*
675          * update actual length first here to decide disable pipe.
676          * if this pipe keeps BUF status and all data were popped,
677          * then, next interrupt/token will be issued again
678          */
679         pkt->actual += total_len;
680
681         if ((pkt->actual == pkt->length) ||     /* receive all data */
682             (total_len < maxp)) {               /* short packet */
683                 *is_done = 1;
684                 usbhsf_rx_irq_ctrl(pipe, 0);
685                 usbhs_pipe_running(pipe, 0);
686                 /*
687                  * If function mode, since this controller is possible to enter
688                  * Control Write status stage at this timing, this driver
689                  * should not disable the pipe. If such a case happens, this
690                  * controller is not able to complete the status stage.
691                  */
692                 if (!usbhs_mod_is_host(priv) && !usbhs_pipe_is_dcp(pipe))
693                         usbhs_pipe_disable(pipe);       /* disable pipe first */
694         }
695
696         /*
697          * Buffer clear if Zero-Length packet
698          *
699          * see
700          * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
701          */
702         if (0 == rcv_len) {
703                 pkt->zero = 1;
704                 usbhsf_fifo_clear(pipe, fifo);
705                 goto usbhs_fifo_read_end;
706         }
707
708         /*
709          * FIXME
710          *
711          * 32-bit access only
712          */
713         if (len >= 4 && !((unsigned long)buf & 0x03)) {
714                 ioread32_rep(addr, buf, len / 4);
715                 len %= 4;
716                 buf += total_len - len;
717         }
718
719         /* the rest operation */
720         for (i = 0; i < len; i++) {
721                 if (!(i & 0x03))
722                         data = ioread32(addr);
723
724                 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
725         }
726
727 usbhs_fifo_read_end:
728         dev_dbg(dev, "  recv %d (%d/ %d/ %d/ %d)\n",
729                 usbhs_pipe_number(pipe),
730                 pkt->length, pkt->actual, *is_done, pkt->zero);
731
732 usbhs_fifo_read_busy:
733         usbhsf_fifo_unselect(pipe, fifo);
734
735         return ret;
736 }
737
738 const struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
739         .prepare = usbhsf_prepare_pop,
740         .try_run = usbhsf_pio_try_pop,
741 };
742
743 /*
744  *              DCP ctrol statge handler
745  */
746 static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
747 {
748         usbhs_dcp_control_transfer_done(pkt->pipe);
749
750         *is_done = 1;
751
752         return 0;
753 }
754
755 const struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
756         .prepare = usbhsf_ctrl_stage_end,
757         .try_run = usbhsf_ctrl_stage_end,
758 };
759
760 /*
761  *              DMA fifo functions
762  */
763 static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
764                                             struct usbhs_pkt *pkt)
765 {
766         if (&usbhs_fifo_dma_push_handler == pkt->handler)
767                 return fifo->tx_chan;
768
769         if (&usbhs_fifo_dma_pop_handler == pkt->handler)
770                 return fifo->rx_chan;
771
772         return NULL;
773 }
774
775 static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
776                                               struct usbhs_pkt *pkt)
777 {
778         struct usbhs_fifo *fifo;
779         int i;
780
781         usbhs_for_each_dfifo(priv, fifo, i) {
782                 if (usbhsf_dma_chan_get(fifo, pkt) &&
783                     !usbhsf_fifo_is_busy(fifo))
784                         return fifo;
785         }
786
787         return NULL;
788 }
789
790 #define usbhsf_dma_start(p, f)  __usbhsf_dma_ctrl(p, f, DREQE)
791 #define usbhsf_dma_stop(p, f)   __usbhsf_dma_ctrl(p, f, 0)
792 static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
793                               struct usbhs_fifo *fifo,
794                               u16 dreqe)
795 {
796         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
797
798         usbhs_bset(priv, fifo->sel, DREQE, dreqe);
799 }
800
801 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
802 {
803         struct usbhs_pipe *pipe = pkt->pipe;
804         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
805         struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
806         struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
807         struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
808
809         return info->dma_map_ctrl(chan->device->dev, pkt, map);
810 }
811
812 static void usbhsf_dma_complete(void *arg);
813 static void usbhsf_dma_xfer_preparing(struct usbhs_pkt *pkt)
814 {
815         struct usbhs_pipe *pipe = pkt->pipe;
816         struct usbhs_fifo *fifo;
817         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
818         struct dma_async_tx_descriptor *desc;
819         struct dma_chan *chan;
820         struct device *dev = usbhs_priv_to_dev(priv);
821         enum dma_transfer_direction dir;
822
823         fifo = usbhs_pipe_to_fifo(pipe);
824         if (!fifo)
825                 return;
826
827         chan = usbhsf_dma_chan_get(fifo, pkt);
828         dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
829
830         desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
831                                         pkt->trans, dir,
832                                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
833         if (!desc)
834                 return;
835
836         desc->callback          = usbhsf_dma_complete;
837         desc->callback_param    = pipe;
838
839         pkt->cookie = dmaengine_submit(desc);
840         if (pkt->cookie < 0) {
841                 dev_err(dev, "Failed to submit dma descriptor\n");
842                 return;
843         }
844
845         dev_dbg(dev, "  %s %d (%d/ %d)\n",
846                 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
847
848         usbhs_pipe_running(pipe, 1);
849         usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
850         dma_async_issue_pending(chan);
851         usbhsf_dma_start(pipe, fifo);
852         usbhs_pipe_enable(pipe);
853 }
854
855 static void xfer_work(struct work_struct *work)
856 {
857         struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
858         struct usbhs_pipe *pipe = pkt->pipe;
859         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
860         unsigned long flags;
861
862         usbhs_lock(priv, flags);
863         usbhsf_dma_xfer_preparing(pkt);
864         usbhs_unlock(priv, flags);
865 }
866
867 /*
868  *              DMA push handler
869  */
870 static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
871 {
872         struct usbhs_pipe *pipe = pkt->pipe;
873         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
874         struct usbhs_fifo *fifo;
875         int len = pkt->length - pkt->actual;
876         int ret;
877         uintptr_t align_mask;
878
879         if (usbhs_pipe_is_busy(pipe))
880                 return 0;
881
882         /* use PIO if packet is less than pio_dma_border or pipe is DCP */
883         if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
884             usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
885                 goto usbhsf_pio_prepare_push;
886
887         /* check data length if this driver don't use USB-DMAC */
888         if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7)
889                 goto usbhsf_pio_prepare_push;
890
891         /* check buffer alignment */
892         align_mask = usbhs_get_dparam(priv, has_usb_dmac) ?
893                                         USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7;
894         if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask)
895                 goto usbhsf_pio_prepare_push;
896
897         /* return at this time if the pipe is running */
898         if (usbhs_pipe_is_running(pipe))
899                 return 0;
900
901         /* get enable DMA fifo */
902         fifo = usbhsf_get_dma_fifo(priv, pkt);
903         if (!fifo)
904                 goto usbhsf_pio_prepare_push;
905
906         ret = usbhsf_fifo_select(pipe, fifo, 0);
907         if (ret < 0)
908                 goto usbhsf_pio_prepare_push;
909
910         if (usbhsf_dma_map(pkt) < 0)
911                 goto usbhsf_pio_prepare_push_unselect;
912
913         pkt->trans = len;
914
915         usbhsf_tx_irq_ctrl(pipe, 0);
916         /* FIXME: Workaound for usb dmac that driver can be used in atomic */
917         if (usbhs_get_dparam(priv, has_usb_dmac)) {
918                 usbhsf_dma_xfer_preparing(pkt);
919         } else {
920                 INIT_WORK(&pkt->work, xfer_work);
921                 schedule_work(&pkt->work);
922         }
923
924         return 0;
925
926 usbhsf_pio_prepare_push_unselect:
927         usbhsf_fifo_unselect(pipe, fifo);
928 usbhsf_pio_prepare_push:
929         /*
930          * change handler to PIO
931          */
932         pkt->handler = &usbhs_fifo_pio_push_handler;
933
934         return pkt->handler->prepare(pkt, is_done);
935 }
936
937 static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
938 {
939         struct usbhs_pipe *pipe = pkt->pipe;
940         int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
941
942         pkt->actual += pkt->trans;
943
944         if (pkt->actual < pkt->length)
945                 *is_done = 0;           /* there are remainder data */
946         else if (is_short)
947                 *is_done = 1;           /* short packet */
948         else
949                 *is_done = !pkt->zero;  /* send zero packet? */
950
951         usbhs_pipe_running(pipe, !*is_done);
952
953         usbhsf_dma_stop(pipe, pipe->fifo);
954         usbhsf_dma_unmap(pkt);
955         usbhsf_fifo_unselect(pipe, pipe->fifo);
956
957         if (!*is_done) {
958                 /* change handler to PIO */
959                 pkt->handler = &usbhs_fifo_pio_push_handler;
960                 return pkt->handler->try_run(pkt, is_done);
961         }
962
963         return 0;
964 }
965
966 const struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
967         .prepare        = usbhsf_dma_prepare_push,
968         .dma_done       = usbhsf_dma_push_done,
969 };
970
971 /*
972  *              DMA pop handler
973  */
974
975 static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
976                                               int *is_done)
977 {
978         return usbhsf_prepare_pop(pkt, is_done);
979 }
980
981 static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt,
982                                                 int *is_done)
983 {
984         struct usbhs_pipe *pipe = pkt->pipe;
985         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
986         struct usbhs_fifo *fifo;
987         int ret;
988
989         if (usbhs_pipe_is_busy(pipe))
990                 return 0;
991
992         /* use PIO if packet is less than pio_dma_border or pipe is DCP */
993         if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) ||
994             usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
995                 goto usbhsf_pio_prepare_pop;
996
997         fifo = usbhsf_get_dma_fifo(priv, pkt);
998         if (!fifo)
999                 goto usbhsf_pio_prepare_pop;
1000
1001         if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1))
1002                 goto usbhsf_pio_prepare_pop;
1003
1004         /* return at this time if the pipe is running */
1005         if (usbhs_pipe_is_running(pipe))
1006                 return 0;
1007
1008         usbhs_pipe_config_change_bfre(pipe, 1);
1009
1010         ret = usbhsf_fifo_select(pipe, fifo, 0);
1011         if (ret < 0)
1012                 goto usbhsf_pio_prepare_pop;
1013
1014         if (usbhsf_dma_map(pkt) < 0)
1015                 goto usbhsf_pio_prepare_pop_unselect;
1016
1017         /* DMA */
1018
1019         /*
1020          * usbhs_fifo_dma_pop_handler :: prepare
1021          * enabled irq to come here.
1022          * but it is no longer needed for DMA. disable it.
1023          */
1024         usbhsf_rx_irq_ctrl(pipe, 0);
1025
1026         pkt->trans = pkt->length;
1027
1028         usbhsf_dma_xfer_preparing(pkt);
1029
1030         return 0;
1031
1032 usbhsf_pio_prepare_pop_unselect:
1033         usbhsf_fifo_unselect(pipe, fifo);
1034 usbhsf_pio_prepare_pop:
1035
1036         /*
1037          * change handler to PIO
1038          */
1039         pkt->handler = &usbhs_fifo_pio_pop_handler;
1040         usbhs_pipe_config_change_bfre(pipe, 0);
1041
1042         return pkt->handler->prepare(pkt, is_done);
1043 }
1044
1045 static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
1046 {
1047         struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1048
1049         if (usbhs_get_dparam(priv, has_usb_dmac))
1050                 return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done);
1051         else
1052                 return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
1053 }
1054
1055 static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
1056 {
1057         struct usbhs_pipe *pipe = pkt->pipe;
1058         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1059         struct usbhs_fifo *fifo;
1060         int len, ret;
1061
1062         if (usbhs_pipe_is_busy(pipe))
1063                 return 0;
1064
1065         if (usbhs_pipe_is_dcp(pipe))
1066                 goto usbhsf_pio_prepare_pop;
1067
1068         /* get enable DMA fifo */
1069         fifo = usbhsf_get_dma_fifo(priv, pkt);
1070         if (!fifo)
1071                 goto usbhsf_pio_prepare_pop;
1072
1073         if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
1074                 goto usbhsf_pio_prepare_pop;
1075
1076         ret = usbhsf_fifo_select(pipe, fifo, 0);
1077         if (ret < 0)
1078                 goto usbhsf_pio_prepare_pop;
1079
1080         /* use PIO if packet is less than pio_dma_border */
1081         len = usbhsf_fifo_rcv_len(priv, fifo);
1082         len = min(pkt->length - pkt->actual, len);
1083         if (len & 0x7) /* 8byte alignment */
1084                 goto usbhsf_pio_prepare_pop_unselect;
1085
1086         if (len < usbhs_get_dparam(priv, pio_dma_border))
1087                 goto usbhsf_pio_prepare_pop_unselect;
1088
1089         ret = usbhsf_fifo_barrier(priv, fifo);
1090         if (ret < 0)
1091                 goto usbhsf_pio_prepare_pop_unselect;
1092
1093         if (usbhsf_dma_map(pkt) < 0)
1094                 goto usbhsf_pio_prepare_pop_unselect;
1095
1096         /* DMA */
1097
1098         /*
1099          * usbhs_fifo_dma_pop_handler :: prepare
1100          * enabled irq to come here.
1101          * but it is no longer needed for DMA. disable it.
1102          */
1103         usbhsf_rx_irq_ctrl(pipe, 0);
1104
1105         pkt->trans = len;
1106
1107         INIT_WORK(&pkt->work, xfer_work);
1108         schedule_work(&pkt->work);
1109
1110         return 0;
1111
1112 usbhsf_pio_prepare_pop_unselect:
1113         usbhsf_fifo_unselect(pipe, fifo);
1114 usbhsf_pio_prepare_pop:
1115
1116         /*
1117          * change handler to PIO
1118          */
1119         pkt->handler = &usbhs_fifo_pio_pop_handler;
1120
1121         return pkt->handler->try_run(pkt, is_done);
1122 }
1123
1124 static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
1125 {
1126         struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1127
1128         BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
1129
1130         return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
1131 }
1132
1133 static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
1134 {
1135         struct usbhs_pipe *pipe = pkt->pipe;
1136         int maxp = usbhs_pipe_get_maxpacket(pipe);
1137
1138         usbhsf_dma_stop(pipe, pipe->fifo);
1139         usbhsf_dma_unmap(pkt);
1140         usbhsf_fifo_unselect(pipe, pipe->fifo);
1141
1142         pkt->actual += pkt->trans;
1143
1144         if ((pkt->actual == pkt->length) ||     /* receive all data */
1145             (pkt->trans < maxp)) {              /* short packet */
1146                 *is_done = 1;
1147                 usbhs_pipe_running(pipe, 0);
1148         } else {
1149                 /* re-enable */
1150                 usbhs_pipe_running(pipe, 0);
1151                 usbhsf_prepare_pop(pkt, is_done);
1152         }
1153
1154         return 0;
1155 }
1156
1157 static size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt,
1158                                            struct dma_chan *chan, int dtln)
1159 {
1160         struct usbhs_pipe *pipe = pkt->pipe;
1161         struct dma_tx_state state;
1162         size_t received_size;
1163         int maxp = usbhs_pipe_get_maxpacket(pipe);
1164
1165         dmaengine_tx_status(chan, pkt->cookie, &state);
1166         received_size = pkt->length - state.residue;
1167
1168         if (dtln) {
1169                 received_size -= USBHS_USB_DMAC_XFER_SIZE;
1170                 received_size &= ~(maxp - 1);
1171                 received_size += dtln;
1172         }
1173
1174         return received_size;
1175 }
1176
1177 static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt,
1178                                              int *is_done)
1179 {
1180         struct usbhs_pipe *pipe = pkt->pipe;
1181         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1182         struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
1183         struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
1184         int rcv_len;
1185
1186         /*
1187          * Since the driver disables rx_irq in DMA mode, the interrupt handler
1188          * cannot the BRDYSTS. So, the function clears it here because the
1189          * driver may use PIO mode next time.
1190          */
1191         usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe));
1192
1193         rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
1194         usbhsf_fifo_clear(pipe, fifo);
1195         pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len);
1196
1197         usbhs_pipe_running(pipe, 0);
1198         usbhsf_dma_stop(pipe, fifo);
1199         usbhsf_dma_unmap(pkt);
1200         usbhsf_fifo_unselect(pipe, pipe->fifo);
1201
1202         /* The driver can assume the rx transaction is always "done" */
1203         *is_done = 1;
1204
1205         return 0;
1206 }
1207
1208 static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
1209 {
1210         struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1211
1212         if (usbhs_get_dparam(priv, has_usb_dmac))
1213                 return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done);
1214         else
1215                 return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
1216 }
1217
1218 const struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
1219         .prepare        = usbhsf_dma_prepare_pop,
1220         .try_run        = usbhsf_dma_try_pop,
1221         .dma_done       = usbhsf_dma_pop_done
1222 };
1223
1224 /*
1225  *              DMA setting
1226  */
1227 static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
1228 {
1229         struct sh_dmae_slave *slave = param;
1230
1231         /*
1232          * FIXME
1233          *
1234          * usbhs doesn't recognize id = 0 as valid DMA
1235          */
1236         if (0 == slave->shdma_slave.slave_id)
1237                 return false;
1238
1239         chan->private = slave;
1240
1241         return true;
1242 }
1243
1244 static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
1245 {
1246         if (fifo->tx_chan)
1247                 dma_release_channel(fifo->tx_chan);
1248         if (fifo->rx_chan)
1249                 dma_release_channel(fifo->rx_chan);
1250
1251         fifo->tx_chan = NULL;
1252         fifo->rx_chan = NULL;
1253 }
1254
1255 static void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo)
1256 {
1257         dma_cap_mask_t mask;
1258
1259         dma_cap_zero(mask);
1260         dma_cap_set(DMA_SLAVE, mask);
1261         fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1262                                             &fifo->tx_slave);
1263
1264         dma_cap_zero(mask);
1265         dma_cap_set(DMA_SLAVE, mask);
1266         fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1267                                             &fifo->rx_slave);
1268 }
1269
1270 static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo,
1271                                int channel)
1272 {
1273         char name[16];
1274
1275         /*
1276          * To avoid complex handing for DnFIFOs, the driver uses each
1277          * DnFIFO as TX or RX direction (not bi-direction).
1278          * So, the driver uses odd channels for TX, even channels for RX.
1279          */
1280         snprintf(name, sizeof(name), "ch%d", channel);
1281         if (channel & 1) {
1282                 fifo->tx_chan = dma_request_slave_channel_reason(dev, name);
1283                 if (IS_ERR(fifo->tx_chan))
1284                         fifo->tx_chan = NULL;
1285         } else {
1286                 fifo->rx_chan = dma_request_slave_channel_reason(dev, name);
1287                 if (IS_ERR(fifo->rx_chan))
1288                         fifo->rx_chan = NULL;
1289         }
1290 }
1291
1292 static void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo,
1293                             int channel)
1294 {
1295         struct device *dev = usbhs_priv_to_dev(priv);
1296
1297         if (dev->of_node)
1298                 usbhsf_dma_init_dt(dev, fifo, channel);
1299         else
1300                 usbhsf_dma_init_pdev(fifo);
1301
1302         if (fifo->tx_chan || fifo->rx_chan)
1303                 dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
1304                          fifo->name,
1305                          fifo->tx_chan ? "[TX]" : "    ",
1306                          fifo->rx_chan ? "[RX]" : "    ");
1307 }
1308
1309 /*
1310  *              irq functions
1311  */
1312 static int usbhsf_irq_empty(struct usbhs_priv *priv,
1313                             struct usbhs_irq_state *irq_state)
1314 {
1315         struct usbhs_pipe *pipe;
1316         struct device *dev = usbhs_priv_to_dev(priv);
1317         int i, ret;
1318
1319         if (!irq_state->bempsts) {
1320                 dev_err(dev, "debug %s !!\n", __func__);
1321                 return -EIO;
1322         }
1323
1324         dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
1325
1326         /*
1327          * search interrupted "pipe"
1328          * not "uep".
1329          */
1330         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1331                 if (!(irq_state->bempsts & (1 << i)))
1332                         continue;
1333
1334                 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1335                 if (ret < 0)
1336                         dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
1337         }
1338
1339         return 0;
1340 }
1341
1342 static int usbhsf_irq_ready(struct usbhs_priv *priv,
1343                             struct usbhs_irq_state *irq_state)
1344 {
1345         struct usbhs_pipe *pipe;
1346         struct device *dev = usbhs_priv_to_dev(priv);
1347         int i, ret;
1348
1349         if (!irq_state->brdysts) {
1350                 dev_err(dev, "debug %s !!\n", __func__);
1351                 return -EIO;
1352         }
1353
1354         dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
1355
1356         /*
1357          * search interrupted "pipe"
1358          * not "uep".
1359          */
1360         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1361                 if (!(irq_state->brdysts & (1 << i)))
1362                         continue;
1363
1364                 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1365                 if (ret < 0)
1366                         dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
1367         }
1368
1369         return 0;
1370 }
1371
1372 static void usbhsf_dma_complete(void *arg)
1373 {
1374         struct usbhs_pipe *pipe = arg;
1375         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1376         struct device *dev = usbhs_priv_to_dev(priv);
1377         int ret;
1378
1379         ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
1380         if (ret < 0)
1381                 dev_err(dev, "dma_complete run_error %d : %d\n",
1382                         usbhs_pipe_number(pipe), ret);
1383 }
1384
1385 void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
1386 {
1387         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1388         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
1389
1390         /* clear DCP FIFO of transmission */
1391         if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
1392                 return;
1393         usbhsf_fifo_clear(pipe, fifo);
1394         usbhsf_fifo_unselect(pipe, fifo);
1395
1396         /* clear DCP FIFO of reception */
1397         if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
1398                 return;
1399         usbhsf_fifo_clear(pipe, fifo);
1400         usbhsf_fifo_unselect(pipe, fifo);
1401 }
1402
1403 /*
1404  *              fifo init
1405  */
1406 void usbhs_fifo_init(struct usbhs_priv *priv)
1407 {
1408         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1409         struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
1410         struct usbhs_fifo *dfifo;
1411         int i;
1412
1413         mod->irq_empty          = usbhsf_irq_empty;
1414         mod->irq_ready          = usbhsf_irq_ready;
1415         mod->irq_bempsts        = 0;
1416         mod->irq_brdysts        = 0;
1417
1418         cfifo->pipe     = NULL;
1419         usbhs_for_each_dfifo(priv, dfifo, i)
1420                 dfifo->pipe     = NULL;
1421 }
1422
1423 void usbhs_fifo_quit(struct usbhs_priv *priv)
1424 {
1425         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1426
1427         mod->irq_empty          = NULL;
1428         mod->irq_ready          = NULL;
1429         mod->irq_bempsts        = 0;
1430         mod->irq_brdysts        = 0;
1431 }
1432
1433 #define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port)              \
1434 do {                                                                    \
1435         fifo = usbhsf_get_dnfifo(priv, channel);                        \
1436         fifo->name      = "D"#channel"FIFO";                            \
1437         fifo->port      = fifo_port;                                    \
1438         fifo->sel       = D##channel##FIFOSEL;                          \
1439         fifo->ctr       = D##channel##FIFOCTR;                          \
1440         fifo->tx_slave.shdma_slave.slave_id =                           \
1441                         usbhs_get_dparam(priv, d##channel##_tx_id);     \
1442         fifo->rx_slave.shdma_slave.slave_id =                           \
1443                         usbhs_get_dparam(priv, d##channel##_rx_id);     \
1444         usbhsf_dma_init(priv, fifo, channel);                           \
1445 } while (0)
1446
1447 #define USBHS_DFIFO_INIT(priv, fifo, channel)                           \
1448                 __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
1449 #define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel)                   \
1450                 __USBHS_DFIFO_INIT(priv, fifo, channel, 0)
1451
1452 int usbhs_fifo_probe(struct usbhs_priv *priv)
1453 {
1454         struct usbhs_fifo *fifo;
1455
1456         /* CFIFO */
1457         fifo = usbhsf_get_cfifo(priv);
1458         fifo->name      = "CFIFO";
1459         fifo->port      = CFIFO;
1460         fifo->sel       = CFIFOSEL;
1461         fifo->ctr       = CFIFOCTR;
1462
1463         /* DFIFO */
1464         USBHS_DFIFO_INIT(priv, fifo, 0);
1465         USBHS_DFIFO_INIT(priv, fifo, 1);
1466         USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2);
1467         USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3);
1468
1469         return 0;
1470 }
1471
1472 void usbhs_fifo_remove(struct usbhs_priv *priv)
1473 {
1474         struct usbhs_fifo *fifo;
1475         int i;
1476
1477         usbhs_for_each_dfifo(priv, fifo, i)
1478                 usbhsf_dma_quit(priv, fifo);
1479 }