GNU Linux-libre 4.9.309-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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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         struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
827         struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
828
829         return info->dma_map_ctrl(chan->device->dev, pkt, map);
830 }
831
832 static void usbhsf_dma_complete(void *arg);
833 static void usbhsf_dma_xfer_preparing(struct usbhs_pkt *pkt)
834 {
835         struct usbhs_pipe *pipe = pkt->pipe;
836         struct usbhs_fifo *fifo;
837         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
838         struct dma_async_tx_descriptor *desc;
839         struct dma_chan *chan;
840         struct device *dev = usbhs_priv_to_dev(priv);
841         enum dma_transfer_direction dir;
842
843         fifo = usbhs_pipe_to_fifo(pipe);
844         if (!fifo)
845                 return;
846
847         chan = usbhsf_dma_chan_get(fifo, pkt);
848         dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
849
850         desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
851                                         pkt->trans, dir,
852                                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
853         if (!desc)
854                 return;
855
856         desc->callback          = usbhsf_dma_complete;
857         desc->callback_param    = pipe;
858
859         pkt->cookie = dmaengine_submit(desc);
860         if (pkt->cookie < 0) {
861                 dev_err(dev, "Failed to submit dma descriptor\n");
862                 return;
863         }
864
865         dev_dbg(dev, "  %s %d (%d/ %d)\n",
866                 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
867
868         usbhs_pipe_running(pipe, 1);
869         usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
870         dma_async_issue_pending(chan);
871         usbhsf_dma_start(pipe, fifo);
872         usbhs_pipe_enable(pipe);
873 }
874
875 static void xfer_work(struct work_struct *work)
876 {
877         struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
878         struct usbhs_pipe *pipe = pkt->pipe;
879         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
880         unsigned long flags;
881
882         usbhs_lock(priv, flags);
883         usbhsf_dma_xfer_preparing(pkt);
884         usbhs_unlock(priv, flags);
885 }
886
887 /*
888  *              DMA push handler
889  */
890 static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
891 {
892         struct usbhs_pipe *pipe = pkt->pipe;
893         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
894         struct usbhs_fifo *fifo;
895         int len = pkt->length - pkt->actual;
896         int ret;
897         uintptr_t align_mask;
898
899         if (usbhs_pipe_is_busy(pipe))
900                 return 0;
901
902         /* use PIO if packet is less than pio_dma_border or pipe is DCP */
903         if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
904             usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
905                 goto usbhsf_pio_prepare_push;
906
907         /* check data length if this driver don't use USB-DMAC */
908         if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7)
909                 goto usbhsf_pio_prepare_push;
910
911         /* check buffer alignment */
912         align_mask = usbhs_get_dparam(priv, has_usb_dmac) ?
913                                         USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7;
914         if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask)
915                 goto usbhsf_pio_prepare_push;
916
917         /* return at this time if the pipe is running */
918         if (usbhs_pipe_is_running(pipe))
919                 return 0;
920
921         /* get enable DMA fifo */
922         fifo = usbhsf_get_dma_fifo(priv, pkt);
923         if (!fifo)
924                 goto usbhsf_pio_prepare_push;
925
926         ret = usbhsf_fifo_select(pipe, fifo, 0);
927         if (ret < 0)
928                 goto usbhsf_pio_prepare_push;
929
930         if (usbhsf_dma_map(pkt) < 0)
931                 goto usbhsf_pio_prepare_push_unselect;
932
933         pkt->trans = len;
934
935         usbhsf_tx_irq_ctrl(pipe, 0);
936         /* FIXME: Workaound for usb dmac that driver can be used in atomic */
937         if (usbhs_get_dparam(priv, has_usb_dmac)) {
938                 usbhsf_dma_xfer_preparing(pkt);
939         } else {
940                 INIT_WORK(&pkt->work, xfer_work);
941                 schedule_work(&pkt->work);
942         }
943
944         return 0;
945
946 usbhsf_pio_prepare_push_unselect:
947         usbhsf_fifo_unselect(pipe, fifo);
948 usbhsf_pio_prepare_push:
949         /*
950          * change handler to PIO
951          */
952         pkt->handler = &usbhs_fifo_pio_push_handler;
953
954         return pkt->handler->prepare(pkt, is_done);
955 }
956
957 static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
958 {
959         struct usbhs_pipe *pipe = pkt->pipe;
960         int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
961
962         pkt->actual += pkt->trans;
963
964         if (pkt->actual < pkt->length)
965                 *is_done = 0;           /* there are remainder data */
966         else if (is_short)
967                 *is_done = 1;           /* short packet */
968         else
969                 *is_done = !pkt->zero;  /* send zero packet? */
970
971         usbhs_pipe_running(pipe, !*is_done);
972
973         usbhsf_dma_stop(pipe, pipe->fifo);
974         usbhsf_dma_unmap(pkt);
975         usbhsf_fifo_unselect(pipe, pipe->fifo);
976
977         if (!*is_done) {
978                 /* change handler to PIO */
979                 pkt->handler = &usbhs_fifo_pio_push_handler;
980                 return pkt->handler->try_run(pkt, is_done);
981         }
982
983         return 0;
984 }
985
986 const struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
987         .prepare        = usbhsf_dma_prepare_push,
988         .dma_done       = usbhsf_dma_push_done,
989 };
990
991 /*
992  *              DMA pop handler
993  */
994
995 static int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
996                                               int *is_done)
997 {
998         return usbhsf_prepare_pop(pkt, is_done);
999 }
1000
1001 static int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt,
1002                                                 int *is_done)
1003 {
1004         struct usbhs_pipe *pipe = pkt->pipe;
1005         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1006         struct usbhs_fifo *fifo;
1007         int ret;
1008
1009         if (usbhs_pipe_is_busy(pipe))
1010                 return 0;
1011
1012         /* use PIO if packet is less than pio_dma_border or pipe is DCP */
1013         if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) ||
1014             usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
1015                 goto usbhsf_pio_prepare_pop;
1016
1017         fifo = usbhsf_get_dma_fifo(priv, pkt);
1018         if (!fifo)
1019                 goto usbhsf_pio_prepare_pop;
1020
1021         if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1))
1022                 goto usbhsf_pio_prepare_pop;
1023
1024         /* return at this time if the pipe is running */
1025         if (usbhs_pipe_is_running(pipe))
1026                 return 0;
1027
1028         usbhs_pipe_config_change_bfre(pipe, 1);
1029
1030         ret = usbhsf_fifo_select(pipe, fifo, 0);
1031         if (ret < 0)
1032                 goto usbhsf_pio_prepare_pop;
1033
1034         if (usbhsf_dma_map(pkt) < 0)
1035                 goto usbhsf_pio_prepare_pop_unselect;
1036
1037         /* DMA */
1038
1039         /*
1040          * usbhs_fifo_dma_pop_handler :: prepare
1041          * enabled irq to come here.
1042          * but it is no longer needed for DMA. disable it.
1043          */
1044         usbhsf_rx_irq_ctrl(pipe, 0);
1045
1046         pkt->trans = pkt->length;
1047
1048         usbhsf_dma_xfer_preparing(pkt);
1049
1050         return 0;
1051
1052 usbhsf_pio_prepare_pop_unselect:
1053         usbhsf_fifo_unselect(pipe, fifo);
1054 usbhsf_pio_prepare_pop:
1055
1056         /*
1057          * change handler to PIO
1058          */
1059         pkt->handler = &usbhs_fifo_pio_pop_handler;
1060         usbhs_pipe_config_change_bfre(pipe, 0);
1061
1062         return pkt->handler->prepare(pkt, is_done);
1063 }
1064
1065 static int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
1066 {
1067         struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1068
1069         if (usbhs_get_dparam(priv, has_usb_dmac))
1070                 return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done);
1071         else
1072                 return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
1073 }
1074
1075 static int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
1076 {
1077         struct usbhs_pipe *pipe = pkt->pipe;
1078         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1079         struct usbhs_fifo *fifo;
1080         int len, ret;
1081
1082         if (usbhs_pipe_is_busy(pipe))
1083                 return 0;
1084
1085         if (usbhs_pipe_is_dcp(pipe))
1086                 goto usbhsf_pio_prepare_pop;
1087
1088         /* get enable DMA fifo */
1089         fifo = usbhsf_get_dma_fifo(priv, pkt);
1090         if (!fifo)
1091                 goto usbhsf_pio_prepare_pop;
1092
1093         if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
1094                 goto usbhsf_pio_prepare_pop;
1095
1096         ret = usbhsf_fifo_select(pipe, fifo, 0);
1097         if (ret < 0)
1098                 goto usbhsf_pio_prepare_pop;
1099
1100         /* use PIO if packet is less than pio_dma_border */
1101         len = usbhsf_fifo_rcv_len(priv, fifo);
1102         len = min(pkt->length - pkt->actual, len);
1103         if (len & 0x7) /* 8byte alignment */
1104                 goto usbhsf_pio_prepare_pop_unselect;
1105
1106         if (len < usbhs_get_dparam(priv, pio_dma_border))
1107                 goto usbhsf_pio_prepare_pop_unselect;
1108
1109         ret = usbhsf_fifo_barrier(priv, fifo);
1110         if (ret < 0)
1111                 goto usbhsf_pio_prepare_pop_unselect;
1112
1113         if (usbhsf_dma_map(pkt) < 0)
1114                 goto usbhsf_pio_prepare_pop_unselect;
1115
1116         /* DMA */
1117
1118         /*
1119          * usbhs_fifo_dma_pop_handler :: prepare
1120          * enabled irq to come here.
1121          * but it is no longer needed for DMA. disable it.
1122          */
1123         usbhsf_rx_irq_ctrl(pipe, 0);
1124
1125         pkt->trans = len;
1126
1127         INIT_WORK(&pkt->work, xfer_work);
1128         schedule_work(&pkt->work);
1129
1130         return 0;
1131
1132 usbhsf_pio_prepare_pop_unselect:
1133         usbhsf_fifo_unselect(pipe, fifo);
1134 usbhsf_pio_prepare_pop:
1135
1136         /*
1137          * change handler to PIO
1138          */
1139         pkt->handler = &usbhs_fifo_pio_pop_handler;
1140
1141         return pkt->handler->try_run(pkt, is_done);
1142 }
1143
1144 static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
1145 {
1146         struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1147
1148         BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
1149
1150         return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
1151 }
1152
1153 static int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
1154 {
1155         struct usbhs_pipe *pipe = pkt->pipe;
1156         int maxp = usbhs_pipe_get_maxpacket(pipe);
1157
1158         usbhsf_dma_stop(pipe, pipe->fifo);
1159         usbhsf_dma_unmap(pkt);
1160         usbhsf_fifo_unselect(pipe, pipe->fifo);
1161
1162         pkt->actual += pkt->trans;
1163
1164         if ((pkt->actual == pkt->length) ||     /* receive all data */
1165             (pkt->trans < maxp)) {              /* short packet */
1166                 *is_done = 1;
1167                 usbhs_pipe_running(pipe, 0);
1168         } else {
1169                 /* re-enable */
1170                 usbhs_pipe_running(pipe, 0);
1171                 usbhsf_prepare_pop(pkt, is_done);
1172         }
1173
1174         return 0;
1175 }
1176
1177 static size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt,
1178                                            struct dma_chan *chan, int dtln)
1179 {
1180         struct usbhs_pipe *pipe = pkt->pipe;
1181         struct dma_tx_state state;
1182         size_t received_size;
1183         int maxp = usbhs_pipe_get_maxpacket(pipe);
1184
1185         dmaengine_tx_status(chan, pkt->cookie, &state);
1186         received_size = pkt->length - state.residue;
1187
1188         if (dtln) {
1189                 received_size -= USBHS_USB_DMAC_XFER_SIZE;
1190                 received_size &= ~(maxp - 1);
1191                 received_size += dtln;
1192         }
1193
1194         return received_size;
1195 }
1196
1197 static int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt,
1198                                              int *is_done)
1199 {
1200         struct usbhs_pipe *pipe = pkt->pipe;
1201         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1202         struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
1203         struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
1204         int rcv_len;
1205
1206         /*
1207          * Since the driver disables rx_irq in DMA mode, the interrupt handler
1208          * cannot the BRDYSTS. So, the function clears it here because the
1209          * driver may use PIO mode next time.
1210          */
1211         usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe));
1212
1213         rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
1214         usbhsf_fifo_clear(pipe, fifo);
1215         pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len);
1216
1217         usbhs_pipe_running(pipe, 0);
1218         usbhsf_dma_stop(pipe, fifo);
1219         usbhsf_dma_unmap(pkt);
1220         usbhsf_fifo_unselect(pipe, pipe->fifo);
1221
1222         /* The driver can assume the rx transaction is always "done" */
1223         *is_done = 1;
1224
1225         return 0;
1226 }
1227
1228 static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
1229 {
1230         struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
1231
1232         if (usbhs_get_dparam(priv, has_usb_dmac))
1233                 return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done);
1234         else
1235                 return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
1236 }
1237
1238 const struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
1239         .prepare        = usbhsf_dma_prepare_pop,
1240         .try_run        = usbhsf_dma_try_pop,
1241         .dma_done       = usbhsf_dma_pop_done
1242 };
1243
1244 /*
1245  *              DMA setting
1246  */
1247 static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
1248 {
1249         struct sh_dmae_slave *slave = param;
1250
1251         /*
1252          * FIXME
1253          *
1254          * usbhs doesn't recognize id = 0 as valid DMA
1255          */
1256         if (0 == slave->shdma_slave.slave_id)
1257                 return false;
1258
1259         chan->private = slave;
1260
1261         return true;
1262 }
1263
1264 static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
1265 {
1266         if (fifo->tx_chan)
1267                 dma_release_channel(fifo->tx_chan);
1268         if (fifo->rx_chan)
1269                 dma_release_channel(fifo->rx_chan);
1270
1271         fifo->tx_chan = NULL;
1272         fifo->rx_chan = NULL;
1273 }
1274
1275 static void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo)
1276 {
1277         dma_cap_mask_t mask;
1278
1279         dma_cap_zero(mask);
1280         dma_cap_set(DMA_SLAVE, mask);
1281         fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1282                                             &fifo->tx_slave);
1283
1284         dma_cap_zero(mask);
1285         dma_cap_set(DMA_SLAVE, mask);
1286         fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1287                                             &fifo->rx_slave);
1288 }
1289
1290 static void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo,
1291                                int channel)
1292 {
1293         char name[16];
1294
1295         /*
1296          * To avoid complex handing for DnFIFOs, the driver uses each
1297          * DnFIFO as TX or RX direction (not bi-direction).
1298          * So, the driver uses odd channels for TX, even channels for RX.
1299          */
1300         snprintf(name, sizeof(name), "ch%d", channel);
1301         if (channel & 1) {
1302                 fifo->tx_chan = dma_request_slave_channel_reason(dev, name);
1303                 if (IS_ERR(fifo->tx_chan))
1304                         fifo->tx_chan = NULL;
1305         } else {
1306                 fifo->rx_chan = dma_request_slave_channel_reason(dev, name);
1307                 if (IS_ERR(fifo->rx_chan))
1308                         fifo->rx_chan = NULL;
1309         }
1310 }
1311
1312 static void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo,
1313                             int channel)
1314 {
1315         struct device *dev = usbhs_priv_to_dev(priv);
1316
1317         if (dev->of_node)
1318                 usbhsf_dma_init_dt(dev, fifo, channel);
1319         else
1320                 usbhsf_dma_init_pdev(fifo);
1321
1322         if (fifo->tx_chan || fifo->rx_chan)
1323                 dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
1324                          fifo->name,
1325                          fifo->tx_chan ? "[TX]" : "    ",
1326                          fifo->rx_chan ? "[RX]" : "    ");
1327 }
1328
1329 /*
1330  *              irq functions
1331  */
1332 static int usbhsf_irq_empty(struct usbhs_priv *priv,
1333                             struct usbhs_irq_state *irq_state)
1334 {
1335         struct usbhs_pipe *pipe;
1336         struct device *dev = usbhs_priv_to_dev(priv);
1337         int i, ret;
1338
1339         if (!irq_state->bempsts) {
1340                 dev_err(dev, "debug %s !!\n", __func__);
1341                 return -EIO;
1342         }
1343
1344         dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
1345
1346         /*
1347          * search interrupted "pipe"
1348          * not "uep".
1349          */
1350         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1351                 if (!(irq_state->bempsts & (1 << i)))
1352                         continue;
1353
1354                 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1355                 if (ret < 0)
1356                         dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
1357         }
1358
1359         return 0;
1360 }
1361
1362 static int usbhsf_irq_ready(struct usbhs_priv *priv,
1363                             struct usbhs_irq_state *irq_state)
1364 {
1365         struct usbhs_pipe *pipe;
1366         struct device *dev = usbhs_priv_to_dev(priv);
1367         int i, ret;
1368
1369         if (!irq_state->brdysts) {
1370                 dev_err(dev, "debug %s !!\n", __func__);
1371                 return -EIO;
1372         }
1373
1374         dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
1375
1376         /*
1377          * search interrupted "pipe"
1378          * not "uep".
1379          */
1380         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1381                 if (!(irq_state->brdysts & (1 << i)))
1382                         continue;
1383
1384                 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1385                 if (ret < 0)
1386                         dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
1387         }
1388
1389         return 0;
1390 }
1391
1392 static void usbhsf_dma_complete(void *arg)
1393 {
1394         struct usbhs_pipe *pipe = arg;
1395         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1396         struct device *dev = usbhs_priv_to_dev(priv);
1397         int ret;
1398
1399         ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
1400         if (ret < 0)
1401                 dev_err(dev, "dma_complete run_error %d : %d\n",
1402                         usbhs_pipe_number(pipe), ret);
1403 }
1404
1405 void usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
1406 {
1407         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1408         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
1409
1410         /* clear DCP FIFO of transmission */
1411         if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
1412                 return;
1413         usbhsf_fifo_clear(pipe, fifo);
1414         usbhsf_fifo_unselect(pipe, fifo);
1415
1416         /* clear DCP FIFO of reception */
1417         if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
1418                 return;
1419         usbhsf_fifo_clear(pipe, fifo);
1420         usbhsf_fifo_unselect(pipe, fifo);
1421 }
1422
1423 /*
1424  *              fifo init
1425  */
1426 void usbhs_fifo_init(struct usbhs_priv *priv)
1427 {
1428         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1429         struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
1430         struct usbhs_fifo *dfifo;
1431         int i;
1432
1433         mod->irq_empty          = usbhsf_irq_empty;
1434         mod->irq_ready          = usbhsf_irq_ready;
1435         mod->irq_bempsts        = 0;
1436         mod->irq_brdysts        = 0;
1437
1438         cfifo->pipe     = NULL;
1439         usbhs_for_each_dfifo(priv, dfifo, i)
1440                 dfifo->pipe     = NULL;
1441 }
1442
1443 void usbhs_fifo_quit(struct usbhs_priv *priv)
1444 {
1445         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1446
1447         mod->irq_empty          = NULL;
1448         mod->irq_ready          = NULL;
1449         mod->irq_bempsts        = 0;
1450         mod->irq_brdysts        = 0;
1451 }
1452
1453 #define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port)              \
1454 do {                                                                    \
1455         fifo = usbhsf_get_dnfifo(priv, channel);                        \
1456         fifo->name      = "D"#channel"FIFO";                            \
1457         fifo->port      = fifo_port;                                    \
1458         fifo->sel       = D##channel##FIFOSEL;                          \
1459         fifo->ctr       = D##channel##FIFOCTR;                          \
1460         fifo->tx_slave.shdma_slave.slave_id =                           \
1461                         usbhs_get_dparam(priv, d##channel##_tx_id);     \
1462         fifo->rx_slave.shdma_slave.slave_id =                           \
1463                         usbhs_get_dparam(priv, d##channel##_rx_id);     \
1464         usbhsf_dma_init(priv, fifo, channel);                           \
1465 } while (0)
1466
1467 #define USBHS_DFIFO_INIT(priv, fifo, channel)                           \
1468                 __USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
1469 #define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel)                   \
1470                 __USBHS_DFIFO_INIT(priv, fifo, channel, 0)
1471
1472 int usbhs_fifo_probe(struct usbhs_priv *priv)
1473 {
1474         struct usbhs_fifo *fifo;
1475
1476         /* CFIFO */
1477         fifo = usbhsf_get_cfifo(priv);
1478         fifo->name      = "CFIFO";
1479         fifo->port      = CFIFO;
1480         fifo->sel       = CFIFOSEL;
1481         fifo->ctr       = CFIFOCTR;
1482
1483         /* DFIFO */
1484         USBHS_DFIFO_INIT(priv, fifo, 0);
1485         USBHS_DFIFO_INIT(priv, fifo, 1);
1486         USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2);
1487         USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3);
1488
1489         return 0;
1490 }
1491
1492 void usbhs_fifo_remove(struct usbhs_priv *priv)
1493 {
1494         struct usbhs_fifo *fifo;
1495         int i;
1496
1497         usbhs_for_each_dfifo(priv, fifo, i)
1498                 usbhsf_dma_quit(priv, fifo);
1499 }