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