1
2
3
4
5
6
7
8
9
10
11#ifndef __UBOOT__
12#include <dm/device_compat.h>
13#include <dm/devres.h>
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/delay.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/list.h>
22#include <linux/dma-mapping.h>
23#else
24#include <common.h>
25#include <dm.h>
26#include <dm/device_compat.h>
27#include <usb.h>
28#include <linux/bug.h>
29#include <linux/usb/usb_urb_compat.h>
30#include "linux-compat.h"
31#endif
32
33#include "musb_core.h"
34#include "musb_host.h"
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87static void musb_ep_program(struct musb *musb, u8 epnum,
88 struct urb *urb, int is_out,
89 u8 *buf, u32 offset, u32 len);
90
91
92
93
94static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
95{
96 struct musb *musb = ep->musb;
97 void __iomem *epio = ep->regs;
98 u16 csr;
99 u16 lastcsr = 0;
100 int retries = 1000;
101
102 csr = musb_readw(epio, MUSB_TXCSR);
103 while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
104 if (csr != lastcsr)
105 dev_dbg(musb->controller, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
106 lastcsr = csr;
107 csr |= MUSB_TXCSR_FLUSHFIFO;
108 musb_writew(epio, MUSB_TXCSR, csr);
109 csr = musb_readw(epio, MUSB_TXCSR);
110 if (WARN(retries-- < 1,
111 "Could not flush host TX%d fifo: csr: %04x\n",
112 ep->epnum, csr))
113 return;
114 mdelay(1);
115 }
116}
117
118static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
119{
120 void __iomem *epio = ep->regs;
121 u16 csr;
122 int retries = 5;
123
124
125 do {
126 csr = musb_readw(epio, MUSB_TXCSR);
127 if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
128 break;
129 musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
130 csr = musb_readw(epio, MUSB_TXCSR);
131 udelay(10);
132 } while (--retries);
133
134 WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
135 ep->epnum, csr);
136
137
138 musb_writew(epio, MUSB_TXCSR, 0);
139}
140
141
142
143
144
145static inline void musb_h_tx_start(struct musb_hw_ep *ep)
146{
147 u16 txcsr;
148
149
150 if (ep->epnum) {
151 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
152 txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
153 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
154 } else {
155 txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
156 musb_writew(ep->regs, MUSB_CSR0, txcsr);
157 }
158
159}
160
161static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
162{
163 u16 txcsr;
164
165
166 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
167 txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
168 if (is_cppi_enabled())
169 txcsr |= MUSB_TXCSR_DMAMODE;
170 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
171}
172
173static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
174{
175 if (is_in != 0 || ep->is_shared_fifo)
176 ep->in_qh = qh;
177 if (is_in == 0 || ep->is_shared_fifo)
178 ep->out_qh = qh;
179}
180
181static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
182{
183 return is_in ? ep->in_qh : ep->out_qh;
184}
185
186
187
188
189
190
191
192static void
193musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
194{
195 u16 frame;
196 u32 len;
197 void __iomem *mbase = musb->mregs;
198 struct urb *urb = next_urb(qh);
199 void *buf = urb->transfer_buffer;
200 u32 offset = 0;
201 struct musb_hw_ep *hw_ep = qh->hw_ep;
202 unsigned pipe = urb->pipe;
203 u8 address = usb_pipedevice(pipe);
204 int epnum = hw_ep->epnum;
205
206
207 qh->offset = 0;
208 qh->segsize = 0;
209
210
211 switch (qh->type) {
212 case USB_ENDPOINT_XFER_CONTROL:
213
214 is_in = 0;
215 musb->ep0_stage = MUSB_EP0_START;
216 buf = urb->setup_packet;
217 len = 8;
218 break;
219#ifndef __UBOOT__
220 case USB_ENDPOINT_XFER_ISOC:
221 qh->iso_idx = 0;
222 qh->frame = 0;
223 offset = urb->iso_frame_desc[0].offset;
224 len = urb->iso_frame_desc[0].length;
225 break;
226#endif
227 default:
228
229 buf = urb->transfer_buffer + urb->actual_length;
230 len = urb->transfer_buffer_length - urb->actual_length;
231 }
232
233 dev_dbg(musb->controller, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
234 qh, urb, address, qh->epnum,
235 is_in ? "in" : "out",
236 ({char *s; switch (qh->type) {
237 case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
238 case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
239#ifndef __UBOOT__
240 case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
241#endif
242 default: s = "-intr"; break;
243 }; s; }),
244 epnum, buf + offset, len);
245
246
247 musb_ep_set_qh(hw_ep, is_in, qh);
248 musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
249
250
251 if (is_in)
252 return;
253
254
255 switch (qh->type) {
256#ifndef __UBOOT__
257 case USB_ENDPOINT_XFER_ISOC:
258#endif
259 case USB_ENDPOINT_XFER_INT:
260 dev_dbg(musb->controller, "check whether there's still time for periodic Tx\n");
261 frame = musb_readw(mbase, MUSB_FRAME);
262
263
264
265#ifndef __UBOOT__
266 if ((urb->transfer_flags & URB_ISO_ASAP)
267 || (frame >= urb->start_frame)) {
268
269
270
271 qh->frame = 0;
272 goto start;
273 } else {
274#endif
275 qh->frame = urb->start_frame;
276
277 dev_dbg(musb->controller, "SOF for %d\n", epnum);
278#if 1
279 musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
280#endif
281#ifndef __UBOOT__
282 }
283#endif
284 break;
285 default:
286start:
287 dev_dbg(musb->controller, "Start TX%d %s\n", epnum,
288 hw_ep->tx_channel ? "dma" : "pio");
289
290 if (!hw_ep->tx_channel)
291 musb_h_tx_start(hw_ep);
292 else if (is_cppi_enabled() || tusb_dma_omap())
293 musb_h_tx_dma_start(hw_ep);
294 }
295}
296
297
298static void musb_giveback(struct musb *musb, struct urb *urb, int status)
299__releases(musb->lock)
300__acquires(musb->lock)
301{
302 dev_dbg(musb->controller,
303 "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
304 urb, urb->complete, status,
305 usb_pipedevice(urb->pipe),
306 usb_pipeendpoint(urb->pipe),
307 usb_pipein(urb->pipe) ? "in" : "out",
308 urb->actual_length, urb->transfer_buffer_length
309 );
310
311 usb_hcd_unlink_urb_from_ep(musb_to_hcd(musb), urb);
312 spin_unlock(&musb->lock);
313 usb_hcd_giveback_urb(musb_to_hcd(musb), urb, status);
314 spin_lock(&musb->lock);
315}
316
317
318static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
319 struct urb *urb)
320{
321 void __iomem *epio = qh->hw_ep->regs;
322 u16 csr;
323
324
325
326
327
328
329 if (is_in)
330 csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
331 else
332 csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
333
334 usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
335}
336
337
338
339
340
341
342
343
344static void musb_advance_schedule(struct musb *musb, struct urb *urb,
345 struct musb_hw_ep *hw_ep, int is_in)
346{
347 struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
348 struct musb_hw_ep *ep = qh->hw_ep;
349 int ready = qh->is_ready;
350 int status;
351
352 status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
353
354
355 switch (qh->type) {
356 case USB_ENDPOINT_XFER_BULK:
357 case USB_ENDPOINT_XFER_INT:
358 musb_save_toggle(qh, is_in, urb);
359 break;
360#ifndef __UBOOT__
361 case USB_ENDPOINT_XFER_ISOC:
362 if (status == 0 && urb->error_count)
363 status = -EXDEV;
364 break;
365#endif
366 }
367
368 qh->is_ready = 0;
369 musb_giveback(musb, urb, status);
370 qh->is_ready = ready;
371
372
373
374
375 if (list_empty(&qh->hep->urb_list)) {
376 struct list_head *head;
377 struct dma_controller *dma = musb->dma_controller;
378
379 if (is_in) {
380 ep->rx_reinit = 1;
381 if (ep->rx_channel) {
382 dma->channel_release(ep->rx_channel);
383 ep->rx_channel = NULL;
384 }
385 } else {
386 ep->tx_reinit = 1;
387 if (ep->tx_channel) {
388 dma->channel_release(ep->tx_channel);
389 ep->tx_channel = NULL;
390 }
391 }
392
393
394 musb_ep_set_qh(ep, is_in, NULL);
395 qh->hep->hcpriv = NULL;
396
397 switch (qh->type) {
398
399 case USB_ENDPOINT_XFER_CONTROL:
400 case USB_ENDPOINT_XFER_BULK:
401
402
403
404 if (qh->mux == 1) {
405 head = qh->ring.prev;
406 list_del(&qh->ring);
407 kfree(qh);
408 qh = first_qh(head);
409 break;
410 }
411
412 case USB_ENDPOINT_XFER_ISOC:
413 case USB_ENDPOINT_XFER_INT:
414
415
416
417
418 kfree(qh);
419 qh = NULL;
420 break;
421 }
422 }
423
424 if (qh != NULL && qh->is_ready) {
425 dev_dbg(musb->controller, "... next ep%d %cX urb %p\n",
426 hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
427 musb_start_urb(musb, is_in, qh);
428 }
429}
430
431static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
432{
433
434
435
436
437 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
438 csr &= ~(MUSB_RXCSR_H_REQPKT
439 | MUSB_RXCSR_H_AUTOREQ
440 | MUSB_RXCSR_AUTOCLEAR);
441
442
443 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
444 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
445
446
447 return musb_readw(hw_ep->regs, MUSB_RXCSR);
448}
449
450
451
452
453static bool
454musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
455{
456 u16 rx_count;
457 u8 *buf;
458 u16 csr;
459 bool done = false;
460 u32 length;
461 int do_flush = 0;
462 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
463 void __iomem *epio = hw_ep->regs;
464 struct musb_qh *qh = hw_ep->in_qh;
465 int pipe = urb->pipe;
466 void *buffer = urb->transfer_buffer;
467
468
469 rx_count = musb_readw(epio, MUSB_RXCOUNT);
470 dev_dbg(musb->controller, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
471 urb->transfer_buffer, qh->offset,
472 urb->transfer_buffer_length);
473
474
475#ifndef __UBOOT__
476 if (usb_pipeisoc(pipe)) {
477 int status = 0;
478 struct usb_iso_packet_descriptor *d;
479
480 if (iso_err) {
481 status = -EILSEQ;
482 urb->error_count++;
483 }
484
485 d = urb->iso_frame_desc + qh->iso_idx;
486 buf = buffer + d->offset;
487 length = d->length;
488 if (rx_count > length) {
489 if (status == 0) {
490 status = -EOVERFLOW;
491 urb->error_count++;
492 }
493 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
494 do_flush = 1;
495 } else
496 length = rx_count;
497 urb->actual_length += length;
498 d->actual_length = length;
499
500 d->status = status;
501
502
503 done = (++qh->iso_idx >= urb->number_of_packets);
504 } else {
505#endif
506
507 buf = buffer + qh->offset;
508 length = urb->transfer_buffer_length - qh->offset;
509 if (rx_count > length) {
510 if (urb->status == -EINPROGRESS)
511 urb->status = -EOVERFLOW;
512 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
513 do_flush = 1;
514 } else
515 length = rx_count;
516 urb->actual_length += length;
517 qh->offset += length;
518
519
520 done = (urb->actual_length == urb->transfer_buffer_length)
521 || (rx_count < qh->maxpacket)
522 || (urb->status != -EINPROGRESS);
523 if (done
524 && (urb->status == -EINPROGRESS)
525 && (urb->transfer_flags & URB_SHORT_NOT_OK)
526 && (urb->actual_length
527 < urb->transfer_buffer_length))
528 urb->status = -EREMOTEIO;
529#ifndef __UBOOT__
530 }
531#endif
532
533 musb_read_fifo(hw_ep, length, buf);
534
535 csr = musb_readw(epio, MUSB_RXCSR);
536 csr |= MUSB_RXCSR_H_WZC_BITS;
537 if (unlikely(do_flush))
538 musb_h_flush_rxfifo(hw_ep, csr);
539 else {
540
541 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
542 if (!done)
543 csr |= MUSB_RXCSR_H_REQPKT;
544 musb_writew(epio, MUSB_RXCSR, csr);
545 }
546
547 return done;
548}
549
550
551
552
553
554
555
556
557
558static void
559musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
560{
561 u16 csr;
562
563
564
565
566
567
568
569 if (ep->is_shared_fifo) {
570 csr = musb_readw(ep->regs, MUSB_TXCSR);
571 if (csr & MUSB_TXCSR_MODE) {
572 musb_h_tx_flush_fifo(ep);
573 csr = musb_readw(ep->regs, MUSB_TXCSR);
574 musb_writew(ep->regs, MUSB_TXCSR,
575 csr | MUSB_TXCSR_FRCDATATOG);
576 }
577
578
579
580
581
582 if (csr & MUSB_TXCSR_DMAMODE)
583 musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
584 musb_writew(ep->regs, MUSB_TXCSR, 0);
585
586
587 } else {
588 csr = musb_readw(ep->regs, MUSB_RXCSR);
589 if (csr & MUSB_RXCSR_RXPKTRDY)
590 WARNING("rx%d, packet/%d ready?\n", ep->epnum,
591 musb_readw(ep->regs, MUSB_RXCOUNT));
592
593 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
594 }
595
596
597 if (musb->is_multipoint) {
598 musb_write_rxfunaddr(ep->target_regs, qh->addr_reg);
599 musb_write_rxhubaddr(ep->target_regs, qh->h_addr_reg);
600 musb_write_rxhubport(ep->target_regs, qh->h_port_reg);
601
602 } else
603 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
604
605
606 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
607 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
608
609
610
611
612 if (musb->double_buffer_not_ok)
613 musb_writew(ep->regs, MUSB_RXMAXP, ep->max_packet_sz_rx);
614 else
615 musb_writew(ep->regs, MUSB_RXMAXP,
616 qh->maxpacket | ((qh->hb_mult - 1) << 11));
617
618 ep->rx_reinit = 0;
619}
620
621static bool musb_tx_dma_program(struct dma_controller *dma,
622 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
623 struct urb *urb, u32 offset, u32 length)
624{
625 struct dma_channel *channel = hw_ep->tx_channel;
626 void __iomem *epio = hw_ep->regs;
627 u16 pkt_size = qh->maxpacket;
628 u16 csr;
629 u8 mode;
630
631#ifdef CONFIG_USB_INVENTRA_DMA
632 if (length > channel->max_len)
633 length = channel->max_len;
634
635 csr = musb_readw(epio, MUSB_TXCSR);
636 if (length > pkt_size) {
637 mode = 1;
638 csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
639
640 if (qh->hb_mult == 1)
641 csr |= MUSB_TXCSR_AUTOSET;
642 } else {
643 mode = 0;
644 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
645 csr |= MUSB_TXCSR_DMAENAB;
646 }
647 channel->desired_mode = mode;
648 musb_writew(epio, MUSB_TXCSR, csr);
649#else
650 if (!is_cppi_enabled() && !tusb_dma_omap())
651 return false;
652
653 channel->actual_len = 0;
654
655
656
657
658
659 mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
660#endif
661
662 qh->segsize = length;
663
664
665
666
667
668 wmb();
669
670 if (!dma->channel_program(channel, pkt_size, mode,
671 urb->transfer_dma + offset, length)) {
672 dma->channel_release(channel);
673 hw_ep->tx_channel = NULL;
674
675 csr = musb_readw(epio, MUSB_TXCSR);
676 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
677 musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
678 return false;
679 }
680 return true;
681}
682
683
684
685
686
687static void musb_ep_program(struct musb *musb, u8 epnum,
688 struct urb *urb, int is_out,
689 u8 *buf, u32 offset, u32 len)
690{
691 struct dma_controller *dma_controller;
692 struct dma_channel *dma_channel;
693 u8 dma_ok;
694 void __iomem *mbase = musb->mregs;
695 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
696 void __iomem *epio = hw_ep->regs;
697 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
698 u16 packet_sz = qh->maxpacket;
699
700 dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s "
701 "h_addr%02x h_port%02x bytes %d\n",
702 is_out ? "-->" : "<--",
703 epnum, urb, urb->dev->speed,
704 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
705 qh->h_addr_reg, qh->h_port_reg,
706 len);
707
708 musb_ep_select(mbase, epnum);
709
710
711 dma_controller = musb->dma_controller;
712 if (is_dma_capable() && epnum && dma_controller) {
713 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
714 if (!dma_channel) {
715 dma_channel = dma_controller->channel_alloc(
716 dma_controller, hw_ep, is_out);
717 if (is_out)
718 hw_ep->tx_channel = dma_channel;
719 else
720 hw_ep->rx_channel = dma_channel;
721 }
722 } else
723 dma_channel = NULL;
724
725
726
727
728 if (is_out) {
729 u16 csr;
730 u16 int_txe;
731 u16 load_count;
732
733 csr = musb_readw(epio, MUSB_TXCSR);
734
735
736 int_txe = musb_readw(mbase, MUSB_INTRTXE);
737 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
738
739
740 if (epnum) {
741
742 musb_h_tx_flush_fifo(hw_ep);
743
744
745
746
747
748
749 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
750 | MUSB_TXCSR_AUTOSET
751 | MUSB_TXCSR_DMAENAB
752 | MUSB_TXCSR_FRCDATATOG
753 | MUSB_TXCSR_H_RXSTALL
754 | MUSB_TXCSR_H_ERROR
755 | MUSB_TXCSR_TXPKTRDY
756 );
757 csr |= MUSB_TXCSR_MODE;
758
759 if (usb_gettoggle(urb->dev, qh->epnum, 1))
760 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
761 | MUSB_TXCSR_H_DATATOGGLE;
762 else
763 csr |= MUSB_TXCSR_CLRDATATOG;
764
765 musb_writew(epio, MUSB_TXCSR, csr);
766
767 csr &= ~MUSB_TXCSR_DMAMODE;
768 musb_writew(epio, MUSB_TXCSR, csr);
769 csr = musb_readw(epio, MUSB_TXCSR);
770 } else {
771
772 musb_h_ep0_flush_fifo(hw_ep);
773 }
774
775
776 if (musb->is_multipoint) {
777 musb_write_txfunaddr(mbase, epnum, qh->addr_reg);
778 musb_write_txhubaddr(mbase, epnum, qh->h_addr_reg);
779 musb_write_txhubport(mbase, epnum, qh->h_port_reg);
780
781 } else
782 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
783
784
785 if (epnum) {
786 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
787 if (musb->double_buffer_not_ok)
788 musb_writew(epio, MUSB_TXMAXP,
789 hw_ep->max_packet_sz_tx);
790 else if (can_bulk_split(musb, qh->type))
791 musb_writew(epio, MUSB_TXMAXP, packet_sz
792 | ((hw_ep->max_packet_sz_tx /
793 packet_sz) - 1) << 11);
794 else
795 musb_writew(epio, MUSB_TXMAXP,
796 qh->maxpacket |
797 ((qh->hb_mult - 1) << 11));
798 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
799 } else {
800 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
801 if (musb->is_multipoint)
802 musb_writeb(epio, MUSB_TYPE0,
803 qh->type_reg);
804 }
805
806 if (can_bulk_split(musb, qh->type))
807 load_count = min((u32) hw_ep->max_packet_sz_tx,
808 len);
809 else
810 load_count = min((u32) packet_sz, len);
811
812 if (dma_channel && musb_tx_dma_program(dma_controller,
813 hw_ep, qh, urb, offset, len))
814 load_count = 0;
815
816 if (load_count) {
817
818 qh->segsize = load_count;
819 musb_write_fifo(hw_ep, load_count, buf);
820 }
821
822
823 musb_writew(mbase, MUSB_INTRTXE, int_txe);
824
825
826 } else {
827 u16 csr;
828
829 if (hw_ep->rx_reinit) {
830 musb_rx_reinit(musb, qh, hw_ep);
831
832
833 if (usb_gettoggle(urb->dev, qh->epnum, 0))
834 csr = MUSB_RXCSR_H_WR_DATATOGGLE
835 | MUSB_RXCSR_H_DATATOGGLE;
836 else
837 csr = 0;
838 if (qh->type == USB_ENDPOINT_XFER_INT)
839 csr |= MUSB_RXCSR_DISNYET;
840
841 } else {
842 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
843
844 if (csr & (MUSB_RXCSR_RXPKTRDY
845 | MUSB_RXCSR_DMAENAB
846 | MUSB_RXCSR_H_REQPKT))
847 ERR("broken !rx_reinit, ep%d csr %04x\n",
848 hw_ep->epnum, csr);
849
850
851 csr &= MUSB_RXCSR_DISNYET;
852 }
853
854
855
856 if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
857
858 dma_channel->actual_len = 0L;
859 qh->segsize = len;
860
861
862 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
863 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
864
865
866
867
868
869 dma_ok = dma_controller->channel_program(dma_channel,
870 packet_sz, !(urb->transfer_flags &
871 URB_SHORT_NOT_OK),
872 urb->transfer_dma + offset,
873 qh->segsize);
874 if (!dma_ok) {
875 dma_controller->channel_release(dma_channel);
876 hw_ep->rx_channel = dma_channel = NULL;
877 } else
878 csr |= MUSB_RXCSR_DMAENAB;
879 }
880
881 csr |= MUSB_RXCSR_H_REQPKT;
882 dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr);
883 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
884 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
885 }
886}
887
888
889
890
891
892
893static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
894{
895 bool more = false;
896 u8 *fifo_dest = NULL;
897 u16 fifo_count = 0;
898 struct musb_hw_ep *hw_ep = musb->control_ep;
899 struct musb_qh *qh = hw_ep->in_qh;
900 struct usb_ctrlrequest *request;
901
902 switch (musb->ep0_stage) {
903 case MUSB_EP0_IN:
904 fifo_dest = urb->transfer_buffer + urb->actual_length;
905 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
906 urb->actual_length);
907 if (fifo_count < len)
908 urb->status = -EOVERFLOW;
909
910 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
911
912 urb->actual_length += fifo_count;
913 if (len < qh->maxpacket) {
914
915
916
917 } else if (urb->actual_length <
918 urb->transfer_buffer_length)
919 more = true;
920 break;
921 case MUSB_EP0_START:
922 request = (struct usb_ctrlrequest *) urb->setup_packet;
923
924 if (!request->wLength) {
925 dev_dbg(musb->controller, "start no-DATA\n");
926 break;
927 } else if (request->bRequestType & USB_DIR_IN) {
928 dev_dbg(musb->controller, "start IN-DATA\n");
929 musb->ep0_stage = MUSB_EP0_IN;
930 more = true;
931 break;
932 } else {
933 dev_dbg(musb->controller, "start OUT-DATA\n");
934 musb->ep0_stage = MUSB_EP0_OUT;
935 more = true;
936 }
937
938 case MUSB_EP0_OUT:
939 fifo_count = min_t(size_t, qh->maxpacket,
940 urb->transfer_buffer_length -
941 urb->actual_length);
942 if (fifo_count) {
943 fifo_dest = (u8 *) (urb->transfer_buffer
944 + urb->actual_length);
945 dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n",
946 fifo_count,
947 (fifo_count == 1) ? "" : "s",
948 fifo_dest);
949 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
950
951 urb->actual_length += fifo_count;
952 more = true;
953 }
954 break;
955 default:
956 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
957 break;
958 }
959
960 return more;
961}
962
963
964
965
966
967
968
969irqreturn_t musb_h_ep0_irq(struct musb *musb)
970{
971 struct urb *urb;
972 u16 csr, len;
973 int status = 0;
974 void __iomem *mbase = musb->mregs;
975 struct musb_hw_ep *hw_ep = musb->control_ep;
976 void __iomem *epio = hw_ep->regs;
977 struct musb_qh *qh = hw_ep->in_qh;
978 bool complete = false;
979 irqreturn_t retval = IRQ_NONE;
980
981
982 urb = next_urb(qh);
983
984 musb_ep_select(mbase, 0);
985 csr = musb_readw(epio, MUSB_CSR0);
986 len = (csr & MUSB_CSR0_RXPKTRDY)
987 ? musb_readb(epio, MUSB_COUNT0)
988 : 0;
989
990 dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
991 csr, qh, len, urb, musb->ep0_stage);
992
993
994 if (MUSB_EP0_STATUS == musb->ep0_stage) {
995 retval = IRQ_HANDLED;
996 complete = true;
997 }
998
999
1000 if (csr & MUSB_CSR0_H_RXSTALL) {
1001 dev_dbg(musb->controller, "STALLING ENDPOINT\n");
1002 status = -EPIPE;
1003
1004 } else if (csr & MUSB_CSR0_H_ERROR) {
1005 dev_dbg(musb->controller, "no response, csr0 %04x\n", csr);
1006 status = -EPROTO;
1007
1008 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
1009 dev_dbg(musb->controller, "control NAK timeout\n");
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019 musb_writew(epio, MUSB_CSR0, 0);
1020 retval = IRQ_HANDLED;
1021 }
1022
1023 if (status) {
1024 dev_dbg(musb->controller, "aborting\n");
1025 retval = IRQ_HANDLED;
1026 if (urb)
1027 urb->status = status;
1028 complete = true;
1029
1030
1031 if (csr & MUSB_CSR0_H_REQPKT) {
1032 csr &= ~MUSB_CSR0_H_REQPKT;
1033 musb_writew(epio, MUSB_CSR0, csr);
1034 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1035 musb_writew(epio, MUSB_CSR0, csr);
1036 } else {
1037 musb_h_ep0_flush_fifo(hw_ep);
1038 }
1039
1040 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1041
1042
1043 musb_writew(epio, MUSB_CSR0, 0);
1044 }
1045
1046 if (unlikely(!urb)) {
1047
1048
1049 ERR("no URB for end 0\n");
1050
1051 musb_h_ep0_flush_fifo(hw_ep);
1052 goto done;
1053 }
1054
1055 if (!complete) {
1056
1057 if (musb_h_ep0_continue(musb, len, urb)) {
1058
1059 csr = (MUSB_EP0_IN == musb->ep0_stage)
1060 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1061 } else {
1062
1063 if (usb_pipeout(urb->pipe)
1064 || !urb->transfer_buffer_length)
1065 csr = MUSB_CSR0_H_STATUSPKT
1066 | MUSB_CSR0_H_REQPKT;
1067 else
1068 csr = MUSB_CSR0_H_STATUSPKT
1069 | MUSB_CSR0_TXPKTRDY;
1070
1071
1072 musb->ep0_stage = MUSB_EP0_STATUS;
1073
1074 dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr);
1075
1076 }
1077 musb_writew(epio, MUSB_CSR0, csr);
1078 retval = IRQ_HANDLED;
1079 } else
1080 musb->ep0_stage = MUSB_EP0_IDLE;
1081
1082
1083 if (complete)
1084 musb_advance_schedule(musb, urb, hw_ep, 1);
1085done:
1086 return retval;
1087}
1088
1089
1090#ifdef CONFIG_USB_INVENTRA_DMA
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104#endif
1105
1106
1107void musb_host_tx(struct musb *musb, u8 epnum)
1108{
1109 int pipe;
1110 bool done = false;
1111 u16 tx_csr;
1112 size_t length = 0;
1113 size_t offset = 0;
1114 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1115 void __iomem *epio = hw_ep->regs;
1116 struct musb_qh *qh = hw_ep->out_qh;
1117 struct urb *urb = next_urb(qh);
1118 u32 status = 0;
1119 void __iomem *mbase = musb->mregs;
1120 struct dma_channel *dma;
1121 bool transfer_pending = false;
1122
1123 musb_ep_select(mbase, epnum);
1124 tx_csr = musb_readw(epio, MUSB_TXCSR);
1125
1126
1127 if (!urb) {
1128 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1129 return;
1130 }
1131
1132 pipe = urb->pipe;
1133 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
1134 dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
1135 dma ? ", dma" : "");
1136
1137
1138 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1139
1140 dev_dbg(musb->controller, "TX end %d stall\n", epnum);
1141
1142
1143 status = -EPIPE;
1144
1145 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1146
1147 dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum);
1148
1149 status = -ETIMEDOUT;
1150
1151 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
1152 dev_dbg(musb->controller, "TX end=%d device not responding\n", epnum);
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162 musb_ep_select(mbase, epnum);
1163 musb_writew(epio, MUSB_TXCSR,
1164 MUSB_TXCSR_H_WZC_BITS
1165 | MUSB_TXCSR_TXPKTRDY);
1166 return;
1167 }
1168
1169 if (status) {
1170 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1171 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1172 (void) musb->dma_controller->channel_abort(dma);
1173 }
1174
1175
1176
1177
1178 musb_h_tx_flush_fifo(hw_ep);
1179 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1180 | MUSB_TXCSR_DMAENAB
1181 | MUSB_TXCSR_H_ERROR
1182 | MUSB_TXCSR_H_RXSTALL
1183 | MUSB_TXCSR_H_NAKTIMEOUT
1184 );
1185
1186 musb_ep_select(mbase, epnum);
1187 musb_writew(epio, MUSB_TXCSR, tx_csr);
1188
1189 musb_writew(epio, MUSB_TXCSR, tx_csr);
1190 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1191
1192 done = true;
1193 }
1194
1195
1196 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1197 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1198 return;
1199 }
1200
1201 if (is_dma_capable() && dma && !status) {
1202
1203
1204
1205
1206
1207
1208
1209
1210 if (tx_csr & MUSB_TXCSR_DMAMODE) {
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227 tx_csr &= musb_readw(epio, MUSB_TXCSR);
1228 if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1229 tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1230 MUSB_TXCSR_TXPKTRDY);
1231 musb_writew(epio, MUSB_TXCSR,
1232 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1233 }
1234 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1235 MUSB_TXCSR_TXPKTRDY);
1236 musb_writew(epio, MUSB_TXCSR,
1237 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1238
1239
1240
1241
1242
1243
1244
1245 tx_csr = musb_readw(epio, MUSB_TXCSR);
1246 }
1247
1248
1249
1250
1251
1252
1253
1254
1255 if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
1256 dev_dbg(musb->controller, "DMA complete but packet still in FIFO, "
1257 "CSR %04x\n", tx_csr);
1258 return;
1259 }
1260 }
1261
1262 if (!status || dma || usb_pipeisoc(pipe)) {
1263 if (dma)
1264 length = dma->actual_len;
1265 else
1266 length = qh->segsize;
1267 qh->offset += length;
1268
1269 if (usb_pipeisoc(pipe)) {
1270#ifndef __UBOOT__
1271 struct usb_iso_packet_descriptor *d;
1272
1273 d = urb->iso_frame_desc + qh->iso_idx;
1274 d->actual_length = length;
1275 d->status = status;
1276 if (++qh->iso_idx >= urb->number_of_packets) {
1277 done = true;
1278 } else {
1279 d++;
1280 offset = d->offset;
1281 length = d->length;
1282 }
1283#endif
1284 } else if (dma && urb->transfer_buffer_length == qh->offset) {
1285 done = true;
1286 } else {
1287
1288 if (qh->segsize < qh->maxpacket)
1289 done = true;
1290 else if (qh->offset == urb->transfer_buffer_length
1291 && !(urb->transfer_flags
1292 & URB_ZERO_PACKET))
1293 done = true;
1294 if (!done) {
1295 offset = qh->offset;
1296 length = urb->transfer_buffer_length - offset;
1297 transfer_pending = true;
1298 }
1299 }
1300 }
1301
1302
1303
1304
1305 if (urb->status != -EINPROGRESS) {
1306 done = true;
1307 if (status == 0)
1308 status = urb->status;
1309 }
1310
1311 if (done) {
1312
1313 urb->status = status;
1314 urb->actual_length = qh->offset;
1315 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
1316 return;
1317 } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
1318 if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
1319 offset, length)) {
1320 if (is_cppi_enabled() || tusb_dma_omap())
1321 musb_h_tx_dma_start(hw_ep);
1322 return;
1323 }
1324 } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
1325 dev_dbg(musb->controller, "not complete, but DMA enabled?\n");
1326 return;
1327 }
1328
1329
1330
1331
1332
1333
1334
1335
1336 if (length > qh->maxpacket)
1337 length = qh->maxpacket;
1338
1339 usb_hcd_unmap_urb_for_dma(musb_to_hcd(musb), urb);
1340 musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
1341 qh->segsize = length;
1342
1343 musb_ep_select(mbase, epnum);
1344 musb_writew(epio, MUSB_TXCSR,
1345 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
1346}
1347
1348
1349#ifdef CONFIG_USB_INVENTRA_DMA
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386#endif
1387
1388
1389
1390
1391static void musb_bulk_rx_nak_timeout(struct musb *musb, struct musb_hw_ep *ep)
1392{
1393 struct dma_channel *dma;
1394 struct urb *urb;
1395 void __iomem *mbase = musb->mregs;
1396 void __iomem *epio = ep->regs;
1397 struct musb_qh *cur_qh, *next_qh;
1398 u16 rx_csr;
1399
1400 musb_ep_select(mbase, ep->epnum);
1401 dma = is_dma_capable() ? ep->rx_channel : NULL;
1402
1403
1404 rx_csr = musb_readw(epio, MUSB_RXCSR);
1405 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1406 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1407 musb_writew(epio, MUSB_RXCSR, rx_csr);
1408
1409 cur_qh = first_qh(&musb->in_bulk);
1410 if (cur_qh) {
1411 urb = next_urb(cur_qh);
1412 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1413 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1414 musb->dma_controller->channel_abort(dma);
1415 urb->actual_length += dma->actual_len;
1416 dma->actual_len = 0L;
1417 }
1418 musb_save_toggle(cur_qh, 1, urb);
1419
1420
1421 list_move_tail(&cur_qh->ring, &musb->in_bulk);
1422
1423
1424 next_qh = first_qh(&musb->in_bulk);
1425
1426
1427 ep->rx_reinit = 1;
1428 musb_start_urb(musb, 1, next_qh);
1429 }
1430}
1431
1432
1433
1434
1435
1436void musb_host_rx(struct musb *musb, u8 epnum)
1437{
1438 struct urb *urb;
1439 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1440 void __iomem *epio = hw_ep->regs;
1441 struct musb_qh *qh = hw_ep->in_qh;
1442 size_t xfer_len;
1443 void __iomem *mbase = musb->mregs;
1444 int pipe;
1445 u16 rx_csr, val;
1446 bool iso_err = false;
1447 bool done = false;
1448 u32 status;
1449 struct dma_channel *dma;
1450
1451 musb_ep_select(mbase, epnum);
1452
1453 urb = next_urb(qh);
1454 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1455 status = 0;
1456 xfer_len = 0;
1457
1458 rx_csr = musb_readw(epio, MUSB_RXCSR);
1459 val = rx_csr;
1460
1461 if (unlikely(!urb)) {
1462
1463
1464
1465
1466 dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
1467 musb_readw(epio, MUSB_RXCOUNT));
1468 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1469 return;
1470 }
1471
1472 pipe = urb->pipe;
1473
1474 dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
1475 epnum, rx_csr, urb->actual_length,
1476 dma ? dma->actual_len : 0);
1477
1478
1479
1480 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
1481 dev_dbg(musb->controller, "RX end %d STALL\n", epnum);
1482
1483
1484 status = -EPIPE;
1485
1486 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
1487 dev_dbg(musb->controller, "end %d RX proto error\n", epnum);
1488
1489 status = -EPROTO;
1490 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1491
1492 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1493
1494 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
1495 dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum);
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505 if (usb_pipebulk(urb->pipe)
1506 && qh->mux == 1
1507 && !list_is_singular(&musb->in_bulk)) {
1508 musb_bulk_rx_nak_timeout(musb, hw_ep);
1509 return;
1510 }
1511 musb_ep_select(mbase, epnum);
1512 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1513 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1514 musb_writew(epio, MUSB_RXCSR, rx_csr);
1515
1516 goto finish;
1517 } else {
1518 dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum);
1519
1520 iso_err = true;
1521 }
1522 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
1523 dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n",
1524 epnum);
1525 status = -EPROTO;
1526 }
1527
1528
1529 if (status) {
1530
1531 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1532 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1533 (void) musb->dma_controller->channel_abort(dma);
1534 xfer_len = dma->actual_len;
1535 }
1536 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1537 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1538 done = true;
1539 goto finish;
1540 }
1541
1542 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1543
1544 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1545 goto finish;
1546 }
1547
1548
1549
1550
1551
1552
1553
1554
1555#ifndef CONFIG_USB_INVENTRA_DMA
1556 if (rx_csr & MUSB_RXCSR_H_REQPKT) {
1557
1558
1559
1560
1561
1562 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1563 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1564 (void) musb->dma_controller->channel_abort(dma);
1565 xfer_len = dma->actual_len;
1566 done = true;
1567 }
1568
1569 dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
1570 xfer_len, dma ? ", dma" : "");
1571 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1572
1573 musb_ep_select(mbase, epnum);
1574 musb_writew(epio, MUSB_RXCSR,
1575 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1576 }
1577#endif
1578 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1579 xfer_len = dma->actual_len;
1580
1581 val &= ~(MUSB_RXCSR_DMAENAB
1582 | MUSB_RXCSR_H_AUTOREQ
1583 | MUSB_RXCSR_AUTOCLEAR
1584 | MUSB_RXCSR_RXPKTRDY);
1585 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1586
1587#ifdef CONFIG_USB_INVENTRA_DMA
1588 if (usb_pipeisoc(pipe)) {
1589 struct usb_iso_packet_descriptor *d;
1590
1591 d = urb->iso_frame_desc + qh->iso_idx;
1592 d->actual_length = xfer_len;
1593
1594
1595
1596
1597 if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1598 d->status = 0;
1599
1600 if (++qh->iso_idx >= urb->number_of_packets)
1601 done = true;
1602 else
1603 done = false;
1604
1605 } else {
1606
1607 done = (urb->actual_length + xfer_len >=
1608 urb->transfer_buffer_length
1609 || dma->actual_len < qh->maxpacket);
1610 }
1611
1612
1613 if (!done) {
1614 val |= MUSB_RXCSR_H_REQPKT;
1615 musb_writew(epio, MUSB_RXCSR,
1616 MUSB_RXCSR_H_WZC_BITS | val);
1617 }
1618
1619 dev_dbg(musb->controller, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
1620 done ? "off" : "reset",
1621 musb_readw(epio, MUSB_RXCSR),
1622 musb_readw(epio, MUSB_RXCOUNT));
1623#else
1624 done = true;
1625#endif
1626 } else if (urb->status == -EINPROGRESS) {
1627
1628 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1629 status = -EPROTO;
1630 ERR("Rx interrupt with no errors or packet!\n");
1631
1632
1633
1634
1635
1636 musb_ep_select(mbase, epnum);
1637 val &= ~MUSB_RXCSR_H_REQPKT;
1638 musb_writew(epio, MUSB_RXCSR, val);
1639 goto finish;
1640 }
1641
1642
1643#ifdef CONFIG_USB_INVENTRA_DMA
1644 if (dma) {
1645 struct dma_controller *c;
1646 u16 rx_count;
1647 int ret, length;
1648 dma_addr_t buf;
1649
1650 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1651
1652 dev_dbg(musb->controller, "RX%d count %d, buffer 0x%x len %d/%d\n",
1653 epnum, rx_count,
1654 urb->transfer_dma
1655 + urb->actual_length,
1656 qh->offset,
1657 urb->transfer_buffer_length);
1658
1659 c = musb->dma_controller;
1660
1661 if (usb_pipeisoc(pipe)) {
1662 int d_status = 0;
1663 struct usb_iso_packet_descriptor *d;
1664
1665 d = urb->iso_frame_desc + qh->iso_idx;
1666
1667 if (iso_err) {
1668 d_status = -EILSEQ;
1669 urb->error_count++;
1670 }
1671 if (rx_count > d->length) {
1672 if (d_status == 0) {
1673 d_status = -EOVERFLOW;
1674 urb->error_count++;
1675 }
1676 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",\
1677 rx_count, d->length);
1678
1679 length = d->length;
1680 } else
1681 length = rx_count;
1682 d->status = d_status;
1683 buf = urb->transfer_dma + d->offset;
1684 } else {
1685 length = rx_count;
1686 buf = urb->transfer_dma +
1687 urb->actual_length;
1688 }
1689
1690 dma->desired_mode = 0;
1691#ifdef USE_MODE1
1692
1693
1694
1695 if ((urb->transfer_flags &
1696 URB_SHORT_NOT_OK)
1697 && (urb->transfer_buffer_length -
1698 urb->actual_length)
1699 > qh->maxpacket)
1700 dma->desired_mode = 1;
1701 if (rx_count < hw_ep->max_packet_sz_rx) {
1702 length = rx_count;
1703 dma->desired_mode = 0;
1704 } else {
1705 length = urb->transfer_buffer_length;
1706 }
1707#endif
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726 val = musb_readw(epio, MUSB_RXCSR);
1727 val &= ~MUSB_RXCSR_H_REQPKT;
1728
1729 if (dma->desired_mode == 0)
1730 val &= ~MUSB_RXCSR_H_AUTOREQ;
1731 else
1732 val |= MUSB_RXCSR_H_AUTOREQ;
1733 val |= MUSB_RXCSR_DMAENAB;
1734
1735
1736 if (qh->hb_mult == 1)
1737 val |= MUSB_RXCSR_AUTOCLEAR;
1738
1739 musb_writew(epio, MUSB_RXCSR,
1740 MUSB_RXCSR_H_WZC_BITS | val);
1741
1742
1743
1744
1745
1746 ret = c->channel_program(
1747 dma, qh->maxpacket,
1748 dma->desired_mode, buf, length);
1749
1750 if (!ret) {
1751 c->channel_release(dma);
1752 hw_ep->rx_channel = NULL;
1753 dma = NULL;
1754 val = musb_readw(epio, MUSB_RXCSR);
1755 val &= ~(MUSB_RXCSR_DMAENAB
1756 | MUSB_RXCSR_H_AUTOREQ
1757 | MUSB_RXCSR_AUTOCLEAR);
1758 musb_writew(epio, MUSB_RXCSR, val);
1759 }
1760 }
1761#endif
1762
1763 if (!dma) {
1764
1765 usb_hcd_unmap_urb_for_dma(musb_to_hcd(musb), urb);
1766 done = musb_host_packet_rx(musb, urb,
1767 epnum, iso_err);
1768 dev_dbg(musb->controller, "read %spacket\n", done ? "last " : "");
1769 }
1770 }
1771
1772finish:
1773 urb->actual_length += xfer_len;
1774 qh->offset += xfer_len;
1775 if (done) {
1776 if (urb->status == -EINPROGRESS)
1777 urb->status = status;
1778 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
1779 }
1780}
1781
1782
1783
1784
1785
1786
1787static int musb_schedule(
1788 struct musb *musb,
1789 struct musb_qh *qh,
1790 int is_in)
1791{
1792 int idle;
1793 int best_diff;
1794 int best_end, epnum;
1795 struct musb_hw_ep *hw_ep = NULL;
1796 struct list_head *head = NULL;
1797 u8 toggle;
1798 u8 txtype;
1799 struct urb *urb = next_urb(qh);
1800
1801
1802 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
1803 head = &musb->control;
1804 hw_ep = musb->control_ep;
1805 goto success;
1806 }
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817 best_diff = 4096;
1818 best_end = -1;
1819
1820 for (epnum = 1, hw_ep = musb->endpoints + 1;
1821 epnum < musb->nr_endpoints;
1822 epnum++, hw_ep++) {
1823 int diff;
1824
1825 if (musb_ep_get_qh(hw_ep, is_in) != NULL)
1826 continue;
1827
1828 if (hw_ep == musb->bulk_ep)
1829 continue;
1830
1831 if (is_in)
1832 diff = hw_ep->max_packet_sz_rx;
1833 else
1834 diff = hw_ep->max_packet_sz_tx;
1835 diff -= (qh->maxpacket * qh->hb_mult);
1836
1837 if (diff >= 0 && best_diff > diff) {
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851 hw_ep = musb->endpoints + epnum;
1852 toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
1853 txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
1854 >> 4) & 0x3;
1855 if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
1856 toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
1857 continue;
1858
1859 best_diff = diff;
1860 best_end = epnum;
1861 }
1862 }
1863
1864 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
1865 hw_ep = musb->bulk_ep;
1866 if (is_in)
1867 head = &musb->in_bulk;
1868 else
1869 head = &musb->out_bulk;
1870
1871
1872
1873
1874
1875
1876
1877
1878 if (is_in && qh->dev)
1879 qh->intv_reg =
1880 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
1881 goto success;
1882 } else if (best_end < 0) {
1883 return -ENOSPC;
1884 }
1885
1886 idle = 1;
1887 qh->mux = 0;
1888 hw_ep = musb->endpoints + best_end;
1889 dev_dbg(musb->controller, "qh %p periodic slot %d\n", qh, best_end);
1890success:
1891 if (head) {
1892 idle = list_empty(head);
1893 list_add_tail(&qh->ring, head);
1894 qh->mux = 1;
1895 }
1896 qh->hw_ep = hw_ep;
1897 qh->hep->hcpriv = qh;
1898 if (idle)
1899 musb_start_urb(musb, is_in, qh);
1900 return 0;
1901}
1902
1903#ifdef __UBOOT__
1904
1905static int tt_needed(struct musb *musb, struct usb_device *dev)
1906{
1907 if ((musb_readb(musb->mregs, MUSB_POWER) & MUSB_POWER_HSMODE) &&
1908 (dev->speed < USB_SPEED_HIGH))
1909 return 1;
1910 return 0;
1911}
1912#endif
1913
1914#ifndef __UBOOT__
1915static int musb_urb_enqueue(
1916#else
1917int musb_urb_enqueue(
1918#endif
1919 struct usb_hcd *hcd,
1920 struct urb *urb,
1921 gfp_t mem_flags)
1922{
1923 unsigned long flags;
1924 struct musb *musb = hcd_to_musb(hcd);
1925 struct usb_host_endpoint *hep = urb->ep;
1926 struct musb_qh *qh;
1927 struct usb_endpoint_descriptor *epd = &hep->desc;
1928 int ret;
1929 unsigned type_reg;
1930 unsigned interval;
1931
1932
1933 if (!is_host_active(musb) || !musb->is_active)
1934 return -ENODEV;
1935
1936 spin_lock_irqsave(&musb->lock, flags);
1937 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1938 qh = ret ? NULL : hep->hcpriv;
1939 if (qh)
1940 urb->hcpriv = qh;
1941 spin_unlock_irqrestore(&musb->lock, flags);
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951 if (qh || ret)
1952 return ret;
1953
1954
1955
1956
1957
1958
1959
1960 qh = kzalloc(sizeof *qh, mem_flags);
1961 if (!qh) {
1962 spin_lock_irqsave(&musb->lock, flags);
1963 usb_hcd_unlink_urb_from_ep(hcd, urb);
1964 spin_unlock_irqrestore(&musb->lock, flags);
1965 return -ENOMEM;
1966 }
1967
1968 qh->hep = hep;
1969 qh->dev = urb->dev;
1970 INIT_LIST_HEAD(&qh->ring);
1971 qh->is_ready = 1;
1972
1973 qh->maxpacket = usb_endpoint_maxp(epd);
1974 qh->type = usb_endpoint_type(epd);
1975
1976
1977
1978
1979
1980 qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
1981 if (qh->hb_mult > 1) {
1982 int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
1983
1984 if (ok)
1985 ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
1986 || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
1987 if (!ok) {
1988 ret = -EMSGSIZE;
1989 goto done;
1990 }
1991 qh->maxpacket &= 0x7ff;
1992 }
1993
1994 qh->epnum = usb_endpoint_num(epd);
1995
1996
1997 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
1998
1999
2000 type_reg = (qh->type << 4) | qh->epnum;
2001 switch (urb->dev->speed) {
2002 case USB_SPEED_LOW:
2003 type_reg |= 0xc0;
2004 break;
2005 case USB_SPEED_FULL:
2006 type_reg |= 0x80;
2007 break;
2008 default:
2009 type_reg |= 0x40;
2010 }
2011 qh->type_reg = type_reg;
2012
2013
2014 switch (qh->type) {
2015 case USB_ENDPOINT_XFER_INT:
2016
2017
2018
2019
2020 if (urb->dev->speed <= USB_SPEED_FULL) {
2021 interval = max_t(u8, epd->bInterval, 1);
2022 break;
2023 }
2024
2025 case USB_ENDPOINT_XFER_ISOC:
2026
2027 interval = min_t(u8, epd->bInterval, 16);
2028 break;
2029 default:
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044 interval = 0;
2045 }
2046 qh->intv_reg = interval;
2047
2048
2049 if (musb->is_multipoint) {
2050#ifndef __UBOOT__
2051 struct usb_device *parent = urb->dev->parent;
2052#else
2053 struct usb_device *parent = usb_dev_get_parent(urb->dev);
2054#endif
2055
2056#ifndef __UBOOT__
2057 if (parent != hcd->self.root_hub) {
2058#else
2059 if (parent) {
2060#endif
2061 qh->h_addr_reg = (u8) parent->devnum;
2062
2063#ifndef __UBOOT__
2064
2065 if (urb->dev->tt) {
2066 qh->h_port_reg = (u8) urb->dev->ttport;
2067 if (urb->dev->tt->hub)
2068 qh->h_addr_reg =
2069 (u8) urb->dev->tt->hub->devnum;
2070 if (urb->dev->tt->multi)
2071 qh->h_addr_reg |= 0x80;
2072 }
2073#else
2074 if (tt_needed(musb, urb->dev)) {
2075 uint8_t portnr = 0;
2076 uint8_t hubaddr = 0;
2077 usb_find_usb2_hub_address_port(urb->dev,
2078 &hubaddr,
2079 &portnr);
2080 qh->h_addr_reg = hubaddr;
2081 qh->h_port_reg = portnr;
2082 }
2083#endif
2084 }
2085 }
2086
2087
2088
2089
2090
2091 spin_lock_irqsave(&musb->lock, flags);
2092 if (hep->hcpriv) {
2093
2094
2095
2096 kfree(qh);
2097 qh = NULL;
2098 ret = 0;
2099 } else
2100 ret = musb_schedule(musb, qh,
2101 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2102
2103 if (ret == 0) {
2104 urb->hcpriv = qh;
2105
2106
2107
2108 }
2109 spin_unlock_irqrestore(&musb->lock, flags);
2110
2111done:
2112 if (ret != 0) {
2113 spin_lock_irqsave(&musb->lock, flags);
2114 usb_hcd_unlink_urb_from_ep(hcd, urb);
2115 spin_unlock_irqrestore(&musb->lock, flags);
2116 kfree(qh);
2117 }
2118 return ret;
2119}
2120
2121
2122
2123
2124
2125
2126static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
2127{
2128 struct musb_hw_ep *ep = qh->hw_ep;
2129 struct musb *musb = ep->musb;
2130 void __iomem *epio = ep->regs;
2131 unsigned hw_end = ep->epnum;
2132 void __iomem *regs = ep->musb->mregs;
2133 int is_in = usb_pipein(urb->pipe);
2134 int status = 0;
2135 u16 csr;
2136
2137 musb_ep_select(regs, hw_end);
2138
2139 if (is_dma_capable()) {
2140 struct dma_channel *dma;
2141
2142 dma = is_in ? ep->rx_channel : ep->tx_channel;
2143 if (dma) {
2144 status = ep->musb->dma_controller->channel_abort(dma);
2145 dev_dbg(musb->controller,
2146 "abort %cX%d DMA for urb %p --> %d\n",
2147 is_in ? 'R' : 'T', ep->epnum,
2148 urb, status);
2149 urb->actual_length += dma->actual_len;
2150 }
2151 }
2152
2153
2154 if (ep->epnum && is_in) {
2155
2156 csr = musb_h_flush_rxfifo(ep, 0);
2157
2158
2159
2160
2161
2162 } else if (ep->epnum) {
2163 musb_h_tx_flush_fifo(ep);
2164 csr = musb_readw(epio, MUSB_TXCSR);
2165 csr &= ~(MUSB_TXCSR_AUTOSET
2166 | MUSB_TXCSR_DMAENAB
2167 | MUSB_TXCSR_H_RXSTALL
2168 | MUSB_TXCSR_H_NAKTIMEOUT
2169 | MUSB_TXCSR_H_ERROR
2170 | MUSB_TXCSR_TXPKTRDY);
2171 musb_writew(epio, MUSB_TXCSR, csr);
2172
2173 musb_writew(epio, MUSB_TXCSR, csr);
2174
2175 csr = musb_readw(epio, MUSB_TXCSR);
2176 } else {
2177 musb_h_ep0_flush_fifo(ep);
2178 }
2179 if (status == 0)
2180 musb_advance_schedule(ep->musb, urb, ep, is_in);
2181 return status;
2182}
2183
2184#ifndef __UBOOT__
2185static int musb_urb_dequeue(
2186#else
2187int musb_urb_dequeue(
2188#endif
2189 struct usb_hcd *hcd,
2190 struct urb *urb,
2191 int status)
2192{
2193 struct musb *musb = hcd_to_musb(hcd);
2194 struct musb_qh *qh;
2195 unsigned long flags;
2196 int is_in = usb_pipein(urb->pipe);
2197 int ret;
2198
2199 dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb,
2200 usb_pipedevice(urb->pipe),
2201 usb_pipeendpoint(urb->pipe),
2202 is_in ? "in" : "out");
2203
2204 spin_lock_irqsave(&musb->lock, flags);
2205 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2206 if (ret)
2207 goto done;
2208
2209 qh = urb->hcpriv;
2210 if (!qh)
2211 goto done;
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225 if (!qh->is_ready
2226 || urb->urb_list.prev != &qh->hep->urb_list
2227 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
2228 int ready = qh->is_ready;
2229
2230 qh->is_ready = 0;
2231 musb_giveback(musb, urb, 0);
2232 qh->is_ready = ready;
2233
2234
2235
2236
2237 if (ready && list_empty(&qh->hep->urb_list)) {
2238 qh->hep->hcpriv = NULL;
2239 list_del(&qh->ring);
2240 kfree(qh);
2241 }
2242 } else
2243 ret = musb_cleanup_urb(urb, qh);
2244done:
2245 spin_unlock_irqrestore(&musb->lock, flags);
2246 return ret;
2247}
2248
2249#ifndef __UBOOT__
2250
2251static void
2252musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2253{
2254 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
2255 unsigned long flags;
2256 struct musb *musb = hcd_to_musb(hcd);
2257 struct musb_qh *qh;
2258 struct urb *urb;
2259
2260 spin_lock_irqsave(&musb->lock, flags);
2261
2262 qh = hep->hcpriv;
2263 if (qh == NULL)
2264 goto exit;
2265
2266
2267
2268
2269 qh->is_ready = 0;
2270 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
2271 urb = next_urb(qh);
2272
2273
2274 if (!urb->unlinked)
2275 urb->status = -ESHUTDOWN;
2276
2277
2278 musb_cleanup_urb(urb, qh);
2279
2280
2281
2282
2283 while (!list_empty(&hep->urb_list)) {
2284 urb = next_urb(qh);
2285 urb->status = -ESHUTDOWN;
2286 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2287 }
2288 } else {
2289
2290
2291
2292
2293 while (!list_empty(&hep->urb_list))
2294 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
2295
2296 hep->hcpriv = NULL;
2297 list_del(&qh->ring);
2298 kfree(qh);
2299 }
2300exit:
2301 spin_unlock_irqrestore(&musb->lock, flags);
2302}
2303
2304static int musb_h_get_frame_number(struct usb_hcd *hcd)
2305{
2306 struct musb *musb = hcd_to_musb(hcd);
2307
2308 return musb_readw(musb->mregs, MUSB_FRAME);
2309}
2310
2311static int musb_h_start(struct usb_hcd *hcd)
2312{
2313 struct musb *musb = hcd_to_musb(hcd);
2314
2315
2316
2317
2318 hcd->state = HC_STATE_RUNNING;
2319 musb->port1_status = 0;
2320 return 0;
2321}
2322
2323static void musb_h_stop(struct usb_hcd *hcd)
2324{
2325 musb_stop(hcd_to_musb(hcd));
2326 hcd->state = HC_STATE_HALT;
2327}
2328
2329static int musb_bus_suspend(struct usb_hcd *hcd)
2330{
2331 struct musb *musb = hcd_to_musb(hcd);
2332 u8 devctl;
2333
2334 if (!is_host_active(musb))
2335 return 0;
2336
2337 switch (musb->xceiv->state) {
2338 case OTG_STATE_A_SUSPEND:
2339 return 0;
2340 case OTG_STATE_A_WAIT_VRISE:
2341
2342
2343
2344
2345 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2346 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2347 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
2348 break;
2349 default:
2350 break;
2351 }
2352
2353 if (musb->is_active) {
2354 WARNING("trying to suspend as %s while active\n",
2355 otg_state_string(musb->xceiv->state));
2356 return -EBUSY;
2357 } else
2358 return 0;
2359}
2360
2361static int musb_bus_resume(struct usb_hcd *hcd)
2362{
2363
2364 return 0;
2365}
2366
2367const struct hc_driver musb_hc_driver = {
2368 .description = "musb-hcd",
2369 .product_desc = "MUSB HDRC host driver",
2370 .hcd_priv_size = sizeof(struct musb),
2371 .flags = HCD_USB2 | HCD_MEMORY,
2372
2373
2374
2375
2376
2377 .start = musb_h_start,
2378 .stop = musb_h_stop,
2379
2380 .get_frame_number = musb_h_get_frame_number,
2381
2382 .urb_enqueue = musb_urb_enqueue,
2383 .urb_dequeue = musb_urb_dequeue,
2384 .endpoint_disable = musb_h_disable,
2385
2386 .hub_status_data = musb_hub_status_data,
2387 .hub_control = musb_hub_control,
2388 .bus_suspend = musb_bus_suspend,
2389 .bus_resume = musb_bus_resume,
2390
2391
2392};
2393#endif
2394