3efece3c72a30771f542b65b35187aca7ed9458d
[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 #define usbhsf_is_cfifo(p, f)   (usbhsf_get_cfifo(p) == f)
27
28 #define usbhsf_fifo_is_busy(f)  ((f)->pipe) /* see usbhs_pipe_select_fifo */
29
30 /*
31  *              packet initialize
32  */
33 void usbhs_pkt_init(struct usbhs_pkt *pkt)
34 {
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, int sequence)
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         if (!done) {
66                 dev_err(dev, "no done function\n");
67                 return;
68         }
69
70         /********************  spin lock ********************/
71         usbhs_lock(priv, flags);
72
73         if (!pipe->handler) {
74                 dev_err(dev, "no handler function\n");
75                 pipe->handler = &usbhsf_null_handler;
76         }
77
78         list_move_tail(&pkt->node, &pipe->list);
79
80         /*
81          * each pkt must hold own handler.
82          * because handler might be changed by its situation.
83          * dma handler -> pio handler.
84          */
85         pkt->pipe       = pipe;
86         pkt->buf        = buf;
87         pkt->handler    = pipe->handler;
88         pkt->length     = len;
89         pkt->zero       = zero;
90         pkt->actual     = 0;
91         pkt->done       = done;
92         pkt->sequence   = sequence;
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_first_entry(&pipe->list, 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 handler\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, irq_bempsts, e)
195 #define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_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->status |= status;                          \
205                 else                                                    \
206                         mod->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         if (usbhs_get_dparam(priv, has_sudmac) && !usbhsf_is_cfifo(priv, fifo))
308                 usbhs_write(priv, fifo->sel, base);
309         else
310                 usbhs_write(priv, fifo->sel, base | MBW_32);
311
312         /* check ISEL and CURPIPE value */
313         while (timeout--) {
314                 if (base == (mask & usbhs_read(priv, fifo->sel))) {
315                         usbhs_pipe_select_fifo(pipe, fifo);
316                         return 0;
317                 }
318                 udelay(10);
319         }
320
321         dev_err(dev, "fifo select error\n");
322
323         return -EIO;
324 }
325
326 /*
327  *              DCP status stage
328  */
329 static int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
330 {
331         struct usbhs_pipe *pipe = pkt->pipe;
332         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
333         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
334         struct device *dev = usbhs_priv_to_dev(priv);
335         int ret;
336
337         usbhs_pipe_disable(pipe);
338
339         ret = usbhsf_fifo_select(pipe, fifo, 1);
340         if (ret < 0) {
341                 dev_err(dev, "%s() faile\n", __func__);
342                 return ret;
343         }
344
345         usbhs_pipe_sequence_data1(pipe); /* DATA1 */
346
347         usbhsf_fifo_clear(pipe, fifo);
348         usbhsf_send_terminator(pipe, fifo);
349
350         usbhsf_fifo_unselect(pipe, fifo);
351
352         usbhsf_tx_irq_ctrl(pipe, 1);
353         usbhs_pipe_enable(pipe);
354
355         return ret;
356 }
357
358 static int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
359 {
360         struct usbhs_pipe *pipe = pkt->pipe;
361         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
362         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
363         struct device *dev = usbhs_priv_to_dev(priv);
364         int ret;
365
366         usbhs_pipe_disable(pipe);
367
368         ret = usbhsf_fifo_select(pipe, fifo, 0);
369         if (ret < 0) {
370                 dev_err(dev, "%s() fail\n", __func__);
371                 return ret;
372         }
373
374         usbhs_pipe_sequence_data1(pipe); /* DATA1 */
375         usbhsf_fifo_clear(pipe, fifo);
376
377         usbhsf_fifo_unselect(pipe, fifo);
378
379         usbhsf_rx_irq_ctrl(pipe, 1);
380         usbhs_pipe_enable(pipe);
381
382         return ret;
383
384 }
385
386 static int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
387 {
388         struct usbhs_pipe *pipe = pkt->pipe;
389
390         if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
391                 usbhsf_tx_irq_ctrl(pipe, 0);
392         else
393                 usbhsf_rx_irq_ctrl(pipe, 0);
394
395         pkt->actual = pkt->length;
396         *is_done = 1;
397
398         return 0;
399 }
400
401 struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
402         .prepare = usbhs_dcp_dir_switch_to_write,
403         .try_run = usbhs_dcp_dir_switch_done,
404 };
405
406 struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
407         .prepare = usbhs_dcp_dir_switch_to_read,
408         .try_run = usbhs_dcp_dir_switch_done,
409 };
410
411 /*
412  *              DCP data stage (push)
413  */
414 static int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
415 {
416         struct usbhs_pipe *pipe = pkt->pipe;
417
418         usbhs_pipe_sequence_data1(pipe); /* DATA1 */
419
420         /*
421          * change handler to PIO push
422          */
423         pkt->handler = &usbhs_fifo_pio_push_handler;
424
425         return pkt->handler->prepare(pkt, is_done);
426 }
427
428 struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
429         .prepare = usbhsf_dcp_data_stage_try_push,
430 };
431
432 /*
433  *              DCP data stage (pop)
434  */
435 static int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
436                                              int *is_done)
437 {
438         struct usbhs_pipe *pipe = pkt->pipe;
439         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
440         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
441
442         if (usbhs_pipe_is_busy(pipe))
443                 return 0;
444
445         /*
446          * prepare pop for DCP should
447          *  - change DCP direction,
448          *  - clear fifo
449          *  - DATA1
450          */
451         usbhs_pipe_disable(pipe);
452
453         usbhs_pipe_sequence_data1(pipe); /* DATA1 */
454
455         usbhsf_fifo_select(pipe, fifo, 0);
456         usbhsf_fifo_clear(pipe, fifo);
457         usbhsf_fifo_unselect(pipe, fifo);
458
459         /*
460          * change handler to PIO pop
461          */
462         pkt->handler = &usbhs_fifo_pio_pop_handler;
463
464         return pkt->handler->prepare(pkt, is_done);
465 }
466
467 struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
468         .prepare = usbhsf_dcp_data_stage_prepare_pop,
469 };
470
471 /*
472  *              PIO push handler
473  */
474 static int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
475 {
476         struct usbhs_pipe *pipe = pkt->pipe;
477         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
478         struct device *dev = usbhs_priv_to_dev(priv);
479         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
480         void __iomem *addr = priv->base + fifo->port;
481         u8 *buf;
482         int maxp = usbhs_pipe_get_maxpacket(pipe);
483         int total_len;
484         int i, ret, len;
485         int is_short;
486
487         usbhs_pipe_data_sequence(pipe, pkt->sequence);
488         pkt->sequence = -1; /* -1 sequence will be ignored */
489
490         usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
491
492         ret = usbhsf_fifo_select(pipe, fifo, 1);
493         if (ret < 0)
494                 return 0;
495
496         ret = usbhs_pipe_is_accessible(pipe);
497         if (ret < 0) {
498                 /* inaccessible pipe is not an error */
499                 ret = 0;
500                 goto usbhs_fifo_write_busy;
501         }
502
503         ret = usbhsf_fifo_barrier(priv, fifo);
504         if (ret < 0)
505                 goto usbhs_fifo_write_busy;
506
507         buf             = pkt->buf    + pkt->actual;
508         len             = pkt->length - pkt->actual;
509         len             = min(len, maxp);
510         total_len       = len;
511         is_short        = total_len < maxp;
512
513         /*
514          * FIXME
515          *
516          * 32-bit access only
517          */
518         if (len >= 4 && !((unsigned long)buf & 0x03)) {
519                 iowrite32_rep(addr, buf, len / 4);
520                 len %= 4;
521                 buf += total_len - len;
522         }
523
524         /* the rest operation */
525         for (i = 0; i < len; i++)
526                 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
527
528         /*
529          * variable update
530          */
531         pkt->actual += total_len;
532
533         if (pkt->actual < pkt->length)
534                 *is_done = 0;           /* there are remainder data */
535         else if (is_short)
536                 *is_done = 1;           /* short packet */
537         else
538                 *is_done = !pkt->zero;  /* send zero packet ? */
539
540         /*
541          * pipe/irq handling
542          */
543         if (is_short)
544                 usbhsf_send_terminator(pipe, fifo);
545
546         usbhsf_tx_irq_ctrl(pipe, !*is_done);
547         usbhs_pipe_running(pipe, !*is_done);
548         usbhs_pipe_enable(pipe);
549
550         dev_dbg(dev, "  send %d (%d/ %d/ %d/ %d)\n",
551                 usbhs_pipe_number(pipe),
552                 pkt->length, pkt->actual, *is_done, pkt->zero);
553
554         /*
555          * Transmission end
556          */
557         if (*is_done) {
558                 if (usbhs_pipe_is_dcp(pipe))
559                         usbhs_dcp_control_transfer_done(pipe);
560         }
561
562         usbhsf_fifo_unselect(pipe, fifo);
563
564         return 0;
565
566 usbhs_fifo_write_busy:
567         usbhsf_fifo_unselect(pipe, fifo);
568
569         /*
570          * pipe is busy.
571          * retry in interrupt
572          */
573         usbhsf_tx_irq_ctrl(pipe, 1);
574         usbhs_pipe_running(pipe, 1);
575
576         return ret;
577 }
578
579 static int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done)
580 {
581         if (usbhs_pipe_is_running(pkt->pipe))
582                 return 0;
583
584         return usbhsf_pio_try_push(pkt, is_done);
585 }
586
587 struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
588         .prepare = usbhsf_pio_prepare_push,
589         .try_run = usbhsf_pio_try_push,
590 };
591
592 /*
593  *              PIO pop handler
594  */
595 static int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
596 {
597         struct usbhs_pipe *pipe = pkt->pipe;
598
599         if (usbhs_pipe_is_busy(pipe))
600                 return 0;
601
602         if (usbhs_pipe_is_running(pipe))
603                 return 0;
604
605         /*
606          * pipe enable to prepare packet receive
607          */
608         usbhs_pipe_data_sequence(pipe, pkt->sequence);
609         pkt->sequence = -1; /* -1 sequence will be ignored */
610
611         usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
612         usbhs_pipe_enable(pipe);
613         usbhs_pipe_running(pipe, 1);
614         usbhsf_rx_irq_ctrl(pipe, 1);
615
616         return 0;
617 }
618
619 static int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
620 {
621         struct usbhs_pipe *pipe = pkt->pipe;
622         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
623         struct device *dev = usbhs_priv_to_dev(priv);
624         struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
625         void __iomem *addr = priv->base + fifo->port;
626         u8 *buf;
627         u32 data = 0;
628         int maxp = usbhs_pipe_get_maxpacket(pipe);
629         int rcv_len, len;
630         int i, ret;
631         int total_len = 0;
632
633         ret = usbhsf_fifo_select(pipe, fifo, 0);
634         if (ret < 0)
635                 return 0;
636
637         ret = usbhsf_fifo_barrier(priv, fifo);
638         if (ret < 0)
639                 goto usbhs_fifo_read_busy;
640
641         rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
642
643         buf             = pkt->buf    + pkt->actual;
644         len             = pkt->length - pkt->actual;
645         len             = min(len, rcv_len);
646         total_len       = len;
647
648         /*
649          * update actual length first here to decide disable pipe.
650          * if this pipe keeps BUF status and all data were popped,
651          * then, next interrupt/token will be issued again
652          */
653         pkt->actual += total_len;
654
655         if ((pkt->actual == pkt->length) ||     /* receive all data */
656             (total_len < maxp)) {               /* short packet */
657                 *is_done = 1;
658                 usbhsf_rx_irq_ctrl(pipe, 0);
659                 usbhs_pipe_running(pipe, 0);
660                 usbhs_pipe_disable(pipe);       /* disable pipe first */
661         }
662
663         /*
664          * Buffer clear if Zero-Length packet
665          *
666          * see
667          * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
668          */
669         if (0 == rcv_len) {
670                 pkt->zero = 1;
671                 usbhsf_fifo_clear(pipe, fifo);
672                 goto usbhs_fifo_read_end;
673         }
674
675         /*
676          * FIXME
677          *
678          * 32-bit access only
679          */
680         if (len >= 4 && !((unsigned long)buf & 0x03)) {
681                 ioread32_rep(addr, buf, len / 4);
682                 len %= 4;
683                 buf += total_len - len;
684         }
685
686         /* the rest operation */
687         for (i = 0; i < len; i++) {
688                 if (!(i & 0x03))
689                         data = ioread32(addr);
690
691                 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
692         }
693
694 usbhs_fifo_read_end:
695         dev_dbg(dev, "  recv %d (%d/ %d/ %d/ %d)\n",
696                 usbhs_pipe_number(pipe),
697                 pkt->length, pkt->actual, *is_done, pkt->zero);
698
699         /*
700          * Transmission end
701          */
702         if (*is_done) {
703                 if (usbhs_pipe_is_dcp(pipe))
704                         usbhs_dcp_control_transfer_done(pipe);
705         }
706
707 usbhs_fifo_read_busy:
708         usbhsf_fifo_unselect(pipe, fifo);
709
710         return ret;
711 }
712
713 struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
714         .prepare = usbhsf_prepare_pop,
715         .try_run = usbhsf_pio_try_pop,
716 };
717
718 /*
719  *              DCP ctrol statge handler
720  */
721 static int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
722 {
723         usbhs_dcp_control_transfer_done(pkt->pipe);
724
725         *is_done = 1;
726
727         return 0;
728 }
729
730 struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
731         .prepare = usbhsf_ctrl_stage_end,
732         .try_run = usbhsf_ctrl_stage_end,
733 };
734
735 /*
736  *              DMA fifo functions
737  */
738 static struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
739                                             struct usbhs_pkt *pkt)
740 {
741         if (&usbhs_fifo_dma_push_handler == pkt->handler)
742                 return fifo->tx_chan;
743
744         if (&usbhs_fifo_dma_pop_handler == pkt->handler)
745                 return fifo->rx_chan;
746
747         return NULL;
748 }
749
750 static struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
751                                               struct usbhs_pkt *pkt)
752 {
753         struct usbhs_fifo *fifo;
754
755         /* DMA :: D0FIFO */
756         fifo = usbhsf_get_d0fifo(priv);
757         if (usbhsf_dma_chan_get(fifo, pkt) &&
758             !usbhsf_fifo_is_busy(fifo))
759                 return fifo;
760
761         /* DMA :: D1FIFO */
762         fifo = usbhsf_get_d1fifo(priv);
763         if (usbhsf_dma_chan_get(fifo, pkt) &&
764             !usbhsf_fifo_is_busy(fifo))
765                 return fifo;
766
767         return NULL;
768 }
769
770 #define usbhsf_dma_start(p, f)  __usbhsf_dma_ctrl(p, f, DREQE)
771 #define usbhsf_dma_stop(p, f)   __usbhsf_dma_ctrl(p, f, 0)
772 static void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
773                               struct usbhs_fifo *fifo,
774                               u16 dreqe)
775 {
776         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
777
778         usbhs_bset(priv, fifo->sel, DREQE, dreqe);
779 }
780
781 #define usbhsf_dma_map(p)       __usbhsf_dma_map_ctrl(p, 1)
782 #define usbhsf_dma_unmap(p)     __usbhsf_dma_map_ctrl(p, 0)
783 static int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
784 {
785         struct usbhs_pipe *pipe = pkt->pipe;
786         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
787         struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
788
789         return info->dma_map_ctrl(pkt, map);
790 }
791
792 static void usbhsf_dma_complete(void *arg);
793 static void xfer_work(struct work_struct *work)
794 {
795         struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
796         struct usbhs_pipe *pipe = pkt->pipe;
797         struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
798         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
799         struct dma_async_tx_descriptor *desc;
800         struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
801         struct device *dev = usbhs_priv_to_dev(priv);
802         enum dma_transfer_direction dir;
803
804         dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
805
806         desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
807                                         pkt->trans, dir,
808                                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
809         if (!desc)
810                 return;
811
812         desc->callback          = usbhsf_dma_complete;
813         desc->callback_param    = pipe;
814
815         if (dmaengine_submit(desc) < 0) {
816                 dev_err(dev, "Failed to submit dma descriptor\n");
817                 return;
818         }
819
820         dev_dbg(dev, "  %s %d (%d/ %d)\n",
821                 fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
822
823         usbhs_pipe_running(pipe, 1);
824         usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
825         usbhs_pipe_enable(pipe);
826         usbhsf_dma_start(pipe, fifo);
827         dma_async_issue_pending(chan);
828 }
829
830 /*
831  *              DMA push handler
832  */
833 static int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
834 {
835         struct usbhs_pipe *pipe = pkt->pipe;
836         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
837         struct usbhs_fifo *fifo;
838         int len = pkt->length - pkt->actual;
839         int ret;
840
841         if (usbhs_pipe_is_busy(pipe))
842                 return 0;
843
844         /* use PIO if packet is less than pio_dma_border or pipe is DCP */
845         if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
846             usbhs_pipe_is_dcp(pipe))
847                 goto usbhsf_pio_prepare_push;
848
849         if (len & 0x7) /* 8byte alignment */
850                 goto usbhsf_pio_prepare_push;
851
852         if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
853                 goto usbhsf_pio_prepare_push;
854
855         /* return at this time if the pipe is running */
856         if (usbhs_pipe_is_running(pipe))
857                 return 0;
858
859         /* get enable DMA fifo */
860         fifo = usbhsf_get_dma_fifo(priv, pkt);
861         if (!fifo)
862                 goto usbhsf_pio_prepare_push;
863
864         if (usbhsf_dma_map(pkt) < 0)
865                 goto usbhsf_pio_prepare_push;
866
867         ret = usbhsf_fifo_select(pipe, fifo, 0);
868         if (ret < 0)
869                 goto usbhsf_pio_prepare_push_unmap;
870
871         pkt->trans = len;
872
873         INIT_WORK(&pkt->work, xfer_work);
874         schedule_work(&pkt->work);
875
876         return 0;
877
878 usbhsf_pio_prepare_push_unmap:
879         usbhsf_dma_unmap(pkt);
880 usbhsf_pio_prepare_push:
881         /*
882          * change handler to PIO
883          */
884         pkt->handler = &usbhs_fifo_pio_push_handler;
885
886         return pkt->handler->prepare(pkt, is_done);
887 }
888
889 static int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
890 {
891         struct usbhs_pipe *pipe = pkt->pipe;
892
893         pkt->actual = pkt->trans;
894
895         *is_done = !pkt->zero;  /* send zero packet ? */
896         usbhs_pipe_running(pipe, !*is_done);
897
898         usbhsf_dma_stop(pipe, pipe->fifo);
899         usbhsf_dma_unmap(pkt);
900         usbhsf_fifo_unselect(pipe, pipe->fifo);
901
902         return 0;
903 }
904
905 struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
906         .prepare        = usbhsf_dma_prepare_push,
907         .dma_done       = usbhsf_dma_push_done,
908 };
909
910 /*
911  *              DMA pop handler
912  */
913 static int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
914 {
915         struct usbhs_pipe *pipe = pkt->pipe;
916         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
917         struct usbhs_fifo *fifo;
918         int len, ret;
919
920         if (usbhs_pipe_is_busy(pipe))
921                 return 0;
922
923         if (usbhs_pipe_is_dcp(pipe))
924                 goto usbhsf_pio_prepare_pop;
925
926         /* get enable DMA fifo */
927         fifo = usbhsf_get_dma_fifo(priv, pkt);
928         if (!fifo)
929                 goto usbhsf_pio_prepare_pop;
930
931         if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
932                 goto usbhsf_pio_prepare_pop;
933
934         ret = usbhsf_fifo_select(pipe, fifo, 0);
935         if (ret < 0)
936                 goto usbhsf_pio_prepare_pop;
937
938         /* use PIO if packet is less than pio_dma_border */
939         len = usbhsf_fifo_rcv_len(priv, fifo);
940         len = min(pkt->length - pkt->actual, len);
941         if (len & 0x7) /* 8byte alignment */
942                 goto usbhsf_pio_prepare_pop_unselect;
943
944         if (len < usbhs_get_dparam(priv, pio_dma_border))
945                 goto usbhsf_pio_prepare_pop_unselect;
946
947         ret = usbhsf_fifo_barrier(priv, fifo);
948         if (ret < 0)
949                 goto usbhsf_pio_prepare_pop_unselect;
950
951         if (usbhsf_dma_map(pkt) < 0)
952                 goto usbhsf_pio_prepare_pop_unselect;
953
954         /* DMA */
955
956         /*
957          * usbhs_fifo_dma_pop_handler :: prepare
958          * enabled irq to come here.
959          * but it is no longer needed for DMA. disable it.
960          */
961         usbhsf_rx_irq_ctrl(pipe, 0);
962
963         pkt->trans = len;
964
965         INIT_WORK(&pkt->work, xfer_work);
966         schedule_work(&pkt->work);
967
968         return 0;
969
970 usbhsf_pio_prepare_pop_unselect:
971         usbhsf_fifo_unselect(pipe, fifo);
972 usbhsf_pio_prepare_pop:
973
974         /*
975          * change handler to PIO
976          */
977         pkt->handler = &usbhs_fifo_pio_pop_handler;
978
979         return pkt->handler->try_run(pkt, is_done);
980 }
981
982 static int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
983 {
984         struct usbhs_pipe *pipe = pkt->pipe;
985         int maxp = usbhs_pipe_get_maxpacket(pipe);
986
987         usbhsf_dma_stop(pipe, pipe->fifo);
988         usbhsf_dma_unmap(pkt);
989         usbhsf_fifo_unselect(pipe, pipe->fifo);
990
991         pkt->actual += pkt->trans;
992
993         if ((pkt->actual == pkt->length) ||     /* receive all data */
994             (pkt->trans < maxp)) {              /* short packet */
995                 *is_done = 1;
996                 usbhs_pipe_running(pipe, 0);
997         } else {
998                 /* re-enable */
999                 usbhs_pipe_running(pipe, 0);
1000                 usbhsf_prepare_pop(pkt, is_done);
1001         }
1002
1003         return 0;
1004 }
1005
1006 struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
1007         .prepare        = usbhsf_prepare_pop,
1008         .try_run        = usbhsf_dma_try_pop,
1009         .dma_done       = usbhsf_dma_pop_done
1010 };
1011
1012 /*
1013  *              DMA setting
1014  */
1015 static bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
1016 {
1017         struct sh_dmae_slave *slave = param;
1018
1019         /*
1020          * FIXME
1021          *
1022          * usbhs doesn't recognize id = 0 as valid DMA
1023          */
1024         if (0 == slave->shdma_slave.slave_id)
1025                 return false;
1026
1027         chan->private = slave;
1028
1029         return true;
1030 }
1031
1032 static void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
1033 {
1034         if (fifo->tx_chan)
1035                 dma_release_channel(fifo->tx_chan);
1036         if (fifo->rx_chan)
1037                 dma_release_channel(fifo->rx_chan);
1038
1039         fifo->tx_chan = NULL;
1040         fifo->rx_chan = NULL;
1041 }
1042
1043 static void usbhsf_dma_init(struct usbhs_priv *priv,
1044                             struct usbhs_fifo *fifo)
1045 {
1046         struct device *dev = usbhs_priv_to_dev(priv);
1047         dma_cap_mask_t mask;
1048
1049         dma_cap_zero(mask);
1050         dma_cap_set(DMA_SLAVE, mask);
1051         fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1052                                             &fifo->tx_slave);
1053
1054         dma_cap_zero(mask);
1055         dma_cap_set(DMA_SLAVE, mask);
1056         fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
1057                                             &fifo->rx_slave);
1058
1059         if (fifo->tx_chan || fifo->rx_chan)
1060                 dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
1061                          fifo->name,
1062                          fifo->tx_chan ? "[TX]" : "    ",
1063                          fifo->rx_chan ? "[RX]" : "    ");
1064 }
1065
1066 /*
1067  *              irq functions
1068  */
1069 static int usbhsf_irq_empty(struct usbhs_priv *priv,
1070                             struct usbhs_irq_state *irq_state)
1071 {
1072         struct usbhs_pipe *pipe;
1073         struct device *dev = usbhs_priv_to_dev(priv);
1074         int i, ret;
1075
1076         if (!irq_state->bempsts) {
1077                 dev_err(dev, "debug %s !!\n", __func__);
1078                 return -EIO;
1079         }
1080
1081         dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
1082
1083         /*
1084          * search interrupted "pipe"
1085          * not "uep".
1086          */
1087         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1088                 if (!(irq_state->bempsts & (1 << i)))
1089                         continue;
1090
1091                 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1092                 if (ret < 0)
1093                         dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
1094         }
1095
1096         return 0;
1097 }
1098
1099 static int usbhsf_irq_ready(struct usbhs_priv *priv,
1100                             struct usbhs_irq_state *irq_state)
1101 {
1102         struct usbhs_pipe *pipe;
1103         struct device *dev = usbhs_priv_to_dev(priv);
1104         int i, ret;
1105
1106         if (!irq_state->brdysts) {
1107                 dev_err(dev, "debug %s !!\n", __func__);
1108                 return -EIO;
1109         }
1110
1111         dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
1112
1113         /*
1114          * search interrupted "pipe"
1115          * not "uep".
1116          */
1117         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
1118                 if (!(irq_state->brdysts & (1 << i)))
1119                         continue;
1120
1121                 ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
1122                 if (ret < 0)
1123                         dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
1124         }
1125
1126         return 0;
1127 }
1128
1129 static void usbhsf_dma_complete(void *arg)
1130 {
1131         struct usbhs_pipe *pipe = arg;
1132         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1133         struct device *dev = usbhs_priv_to_dev(priv);
1134         int ret;
1135
1136         ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
1137         if (ret < 0)
1138                 dev_err(dev, "dma_complete run_error %d : %d\n",
1139                         usbhs_pipe_number(pipe), ret);
1140 }
1141
1142 /*
1143  *              fifo init
1144  */
1145 void usbhs_fifo_init(struct usbhs_priv *priv)
1146 {
1147         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1148         struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
1149         struct usbhs_fifo *d0fifo = usbhsf_get_d0fifo(priv);
1150         struct usbhs_fifo *d1fifo = usbhsf_get_d1fifo(priv);
1151
1152         mod->irq_empty          = usbhsf_irq_empty;
1153         mod->irq_ready          = usbhsf_irq_ready;
1154         mod->irq_bempsts        = 0;
1155         mod->irq_brdysts        = 0;
1156
1157         cfifo->pipe     = NULL;
1158         d0fifo->pipe    = NULL;
1159         d1fifo->pipe    = NULL;
1160 }
1161
1162 void usbhs_fifo_quit(struct usbhs_priv *priv)
1163 {
1164         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1165
1166         mod->irq_empty          = NULL;
1167         mod->irq_ready          = NULL;
1168         mod->irq_bempsts        = 0;
1169         mod->irq_brdysts        = 0;
1170 }
1171
1172 int usbhs_fifo_probe(struct usbhs_priv *priv)
1173 {
1174         struct usbhs_fifo *fifo;
1175
1176         /* CFIFO */
1177         fifo = usbhsf_get_cfifo(priv);
1178         fifo->name      = "CFIFO";
1179         fifo->port      = CFIFO;
1180         fifo->sel       = CFIFOSEL;
1181         fifo->ctr       = CFIFOCTR;
1182
1183         /* D0FIFO */
1184         fifo = usbhsf_get_d0fifo(priv);
1185         fifo->name      = "D0FIFO";
1186         fifo->port      = D0FIFO;
1187         fifo->sel       = D0FIFOSEL;
1188         fifo->ctr       = D0FIFOCTR;
1189         fifo->tx_slave.shdma_slave.slave_id     = usbhs_get_dparam(priv, d0_tx_id);
1190         fifo->rx_slave.shdma_slave.slave_id     = usbhs_get_dparam(priv, d0_rx_id);
1191         usbhsf_dma_init(priv, fifo);
1192
1193         /* D1FIFO */
1194         fifo = usbhsf_get_d1fifo(priv);
1195         fifo->name      = "D1FIFO";
1196         fifo->port      = D1FIFO;
1197         fifo->sel       = D1FIFOSEL;
1198         fifo->ctr       = D1FIFOCTR;
1199         fifo->tx_slave.shdma_slave.slave_id     = usbhs_get_dparam(priv, d1_tx_id);
1200         fifo->rx_slave.shdma_slave.slave_id     = usbhs_get_dparam(priv, d1_rx_id);
1201         usbhsf_dma_init(priv, fifo);
1202
1203         return 0;
1204 }
1205
1206 void usbhs_fifo_remove(struct usbhs_priv *priv)
1207 {
1208         usbhsf_dma_quit(priv, usbhsf_get_d0fifo(priv));
1209         usbhsf_dma_quit(priv, usbhsf_get_d1fifo(priv));
1210 }