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