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