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