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