blob: f1d6d8586289ae746f7e5e5c7cc955eaafff3548 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Rixf298e4b2009-10-31 12:37:41 -05002/*
3 * Copyright (c) 2009 Wind River Systems, Inc.
4 * Tom Rix <Tom.Rix@windriver.com>
5 *
6 * This file is a rewrite of the usb device part of
7 * repository git.omapzoom.org/repo/u-boot.git, branch master,
8 * file cpu/omap3/fastboot.c
9 *
10 * This is the unique part of its copyright :
11 *
12 * -------------------------------------------------------------------------
13 *
14 * (C) Copyright 2008 - 2009
15 * Windriver, <www.windriver.com>
16 * Tom Rix <Tom.Rix@windriver.com>
17 *
18 * -------------------------------------------------------------------------
19 *
20 * The details of connecting the device to the uboot usb device subsystem
21 * came from the old omap3 repository www.sakoman.net/u-boot-omap3.git,
22 * branch omap3-dev-usb, file drivers/usb/usbdcore_musb.c
23 *
24 * This is the unique part of its copyright :
25 *
26 * -------------------------------------------------------------------------
27 *
28 * (C) Copyright 2008 Texas Instruments Incorporated.
29 *
30 * Based on
31 * u-boot OMAP1510 USB drivers (drivers/usbdcore_omap1510.c)
32 * twl4030 init based on linux (drivers/i2c/chips/twl4030_usb.c)
33 *
34 * Author: Diego Dompe (diego.dompe@ridgerun.com)
35 * Atin Malaviya (atin.malaviya@gmail.com)
36 *
37 * -------------------------------------------------------------------------
Tom Rixf298e4b2009-10-31 12:37:41 -050038 */
39
40#include <common.h>
Troy Kisky449697f2013-10-10 15:28:04 -070041#include <usbdevice.h>
42#include <usb/udc.h>
Tom Rixf298e4b2009-10-31 12:37:41 -050043#include "../gadget/ep0.h"
44#include "musb_core.h"
45#if defined(CONFIG_USB_OMAP3)
46#include "omap3.h"
Ajay Kumar Guptadbea3242010-07-09 11:43:48 +053047#elif defined(CONFIG_USB_AM35X)
48#include "am35x.h"
Tom Rixf298e4b2009-10-31 12:37:41 -050049#elif defined(CONFIG_USB_DAVINCI)
50#include "davinci.h"
51#endif
52
53/* Define MUSB_DEBUG for debugging */
54/* #define MUSB_DEBUG */
55#include "musb_debug.h"
56
57#define MAX_ENDPOINT 15
58
59#define GET_ENDPOINT(dev,ep) \
60(((struct usb_device_instance *)(dev))->bus->endpoint_array + ep)
61
62#define SET_EP0_STATE(s) \
63do { \
64 if ((0 <= (s)) && (SET_ADDRESS >= (s))) { \
65 if ((s) != ep0_state) { \
66 if ((debug_setup) && (debug_level > 1)) \
67 serial_printf("INFO : Changing state " \
68 "from %s to %s in %s at " \
69 "line %d\n", \
70 ep0_state_strings[ep0_state],\
71 ep0_state_strings[s], \
72 __PRETTY_FUNCTION__, \
73 __LINE__); \
74 ep0_state = s; \
75 } \
76 } else { \
77 if (debug_level > 0) \
78 serial_printf("Error at %s %d with setting " \
79 "state %d is invalid\n", \
80 __PRETTY_FUNCTION__, __LINE__, s); \
81 } \
82} while (0)
83
84/* static implies these initialized to 0 or NULL */
85static int debug_setup;
86static int debug_level;
Heinrich Schuchardt7f2e59a2017-04-15 14:29:54 +020087static struct musb_epinfo epinfo[MAX_ENDPOINT * 2 + 2];
Tom Rixf298e4b2009-10-31 12:37:41 -050088static enum ep0_state_enum {
89 IDLE = 0,
90 TX,
91 RX,
92 SET_ADDRESS
93} ep0_state = IDLE;
94static char *ep0_state_strings[4] = {
95 "IDLE",
96 "TX",
97 "RX",
98 "SET_ADDRESS",
99};
100
101static struct urb *ep0_urb;
102struct usb_endpoint_instance *ep0_endpoint;
103static struct usb_device_instance *udc_device;
104static int enabled;
105
106#ifdef MUSB_DEBUG
107static void musb_db_regs(void)
108{
109 u8 b;
110 u16 w;
111
112 b = readb(&musbr->faddr);
113 serial_printf("\tfaddr 0x%2.2x\n", b);
114
115 b = readb(&musbr->power);
116 musb_print_pwr(b);
117
118 w = readw(&musbr->ep[0].ep0.csr0);
119 musb_print_csr0(w);
120
121 b = readb(&musbr->devctl);
122 musb_print_devctl(b);
123
124 b = readb(&musbr->ep[0].ep0.configdata);
125 musb_print_config(b);
126
127 w = readw(&musbr->frame);
128 serial_printf("\tframe 0x%4.4x\n", w);
129
130 b = readb(&musbr->index);
131 serial_printf("\tindex 0x%2.2x\n", b);
132
133 w = readw(&musbr->ep[1].epN.rxmaxp);
134 musb_print_rxmaxp(w);
135
136 w = readw(&musbr->ep[1].epN.rxcsr);
137 musb_print_rxcsr(w);
138
139 w = readw(&musbr->ep[1].epN.txmaxp);
140 musb_print_txmaxp(w);
141
142 w = readw(&musbr->ep[1].epN.txcsr);
143 musb_print_txcsr(w);
144}
145#else
146#define musb_db_regs()
147#endif /* DEBUG_MUSB */
148
149static void musb_peri_softconnect(void)
150{
151 u8 power, devctl;
Tom Rixf298e4b2009-10-31 12:37:41 -0500152
153 /* Power off MUSB */
154 power = readb(&musbr->power);
155 power &= ~MUSB_POWER_SOFTCONN;
156 writeb(power, &musbr->power);
157
158 /* Read intr to clear */
Anatolij Gustschinc594a8d2011-12-03 06:46:10 +0000159 readb(&musbr->intrusb);
160 readw(&musbr->intrrx);
161 readw(&musbr->intrtx);
Tom Rixf298e4b2009-10-31 12:37:41 -0500162
163 udelay(1000 * 1000); /* 1 sec */
164
165 /* Power on MUSB */
166 power = readb(&musbr->power);
167 power |= MUSB_POWER_SOFTCONN;
168 /*
169 * The usb device interface is usb 1.1
170 * Disable 2.0 high speed by clearring the hsenable bit.
171 */
172 power &= ~MUSB_POWER_HSENAB;
173 writeb(power, &musbr->power);
174
175 /* Check if device is in b-peripheral mode */
176 devctl = readb(&musbr->devctl);
177 if (!(devctl & MUSB_DEVCTL_BDEVICE) ||
178 (devctl & MUSB_DEVCTL_HM)) {
179 serial_printf("ERROR : Unsupport USB mode\n");
180 serial_printf("Check that mini-B USB cable is attached "
181 "to the device\n");
182 }
183
184 if (debug_setup && (debug_level > 1))
185 musb_db_regs();
186}
187
188static void musb_peri_reset(void)
189{
190 if ((debug_setup) && (debug_level > 1))
191 serial_printf("INFO : %s reset\n", __PRETTY_FUNCTION__);
192
193 if (ep0_endpoint)
194 ep0_endpoint->endpoint_address = 0xff;
195
196 /* Sync sw and hw addresses */
197 writeb(udc_device->address, &musbr->faddr);
198
199 SET_EP0_STATE(IDLE);
200}
201
202static void musb_peri_resume(void)
203{
204 /* noop */
205}
206
207static void musb_peri_ep0_stall(void)
208{
209 u16 csr0;
210
211 csr0 = readw(&musbr->ep[0].ep0.csr0);
212 csr0 |= MUSB_CSR0_P_SENDSTALL;
213 writew(csr0, &musbr->ep[0].ep0.csr0);
214 if ((debug_setup) && (debug_level > 1))
215 serial_printf("INFO : %s stall\n", __PRETTY_FUNCTION__);
216}
217
218static void musb_peri_ep0_ack_req(void)
219{
220 u16 csr0;
221
222 csr0 = readw(&musbr->ep[0].ep0.csr0);
223 csr0 |= MUSB_CSR0_P_SVDRXPKTRDY;
224 writew(csr0, &musbr->ep[0].ep0.csr0);
225}
226
227static void musb_ep0_tx_ready(void)
228{
229 u16 csr0;
230
231 csr0 = readw(&musbr->ep[0].ep0.csr0);
232 csr0 |= MUSB_CSR0_TXPKTRDY;
233 writew(csr0, &musbr->ep[0].ep0.csr0);
234}
235
236static void musb_ep0_tx_ready_and_last(void)
237{
238 u16 csr0;
239
240 csr0 = readw(&musbr->ep[0].ep0.csr0);
241 csr0 |= (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_P_DATAEND);
242 writew(csr0, &musbr->ep[0].ep0.csr0);
243}
244
245static void musb_peri_ep0_last(void)
246{
247 u16 csr0;
248
249 csr0 = readw(&musbr->ep[0].ep0.csr0);
250 csr0 |= MUSB_CSR0_P_DATAEND;
251 writew(csr0, &musbr->ep[0].ep0.csr0);
252}
253
254static void musb_peri_ep0_set_address(void)
255{
256 u8 faddr;
257 writeb(udc_device->address, &musbr->faddr);
258
259 /* Verify */
260 faddr = readb(&musbr->faddr);
261 if (udc_device->address == faddr) {
262 SET_EP0_STATE(IDLE);
263 usbd_device_event_irq(udc_device, DEVICE_ADDRESS_ASSIGNED, 0);
264 if ((debug_setup) && (debug_level > 1))
265 serial_printf("INFO : %s Address set to %d\n",
266 __PRETTY_FUNCTION__, udc_device->address);
267 } else {
268 if (debug_level > 0)
269 serial_printf("ERROR : %s Address missmatch "
270 "sw %d vs hw %d\n",
271 __PRETTY_FUNCTION__,
272 udc_device->address, faddr);
273 }
274}
275
276static void musb_peri_rx_ack(unsigned int ep)
277{
278 u16 peri_rxcsr;
279
280 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
281 peri_rxcsr &= ~MUSB_RXCSR_RXPKTRDY;
282 writew(peri_rxcsr, &musbr->ep[ep].epN.rxcsr);
283}
284
285static void musb_peri_tx_ready(unsigned int ep)
286{
287 u16 peri_txcsr;
288
289 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
290 peri_txcsr |= MUSB_TXCSR_TXPKTRDY;
291 writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
292}
293
294static void musb_peri_ep0_zero_data_request(int err)
295{
296 musb_peri_ep0_ack_req();
297
298 if (err) {
299 musb_peri_ep0_stall();
300 SET_EP0_STATE(IDLE);
301 } else {
302
303 musb_peri_ep0_last();
304
305 /* USBD state */
306 switch (ep0_urb->device_request.bRequest) {
307 case USB_REQ_SET_ADDRESS:
308 if ((debug_setup) && (debug_level > 1))
309 serial_printf("INFO : %s received set "
310 "address\n", __PRETTY_FUNCTION__);
311 break;
312
313 case USB_REQ_SET_CONFIGURATION:
314 if ((debug_setup) && (debug_level > 1))
315 serial_printf("INFO : %s Configured\n",
316 __PRETTY_FUNCTION__);
317 usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
318 break;
319 }
320
321 /* EP0 state */
322 if (USB_REQ_SET_ADDRESS == ep0_urb->device_request.bRequest) {
323 SET_EP0_STATE(SET_ADDRESS);
324 } else {
325 SET_EP0_STATE(IDLE);
326 }
327 }
328}
329
330static void musb_peri_ep0_rx_data_request(void)
331{
332 /*
333 * This is the completion of the data OUT / RX
334 *
335 * Host is sending data to ep0 that is not
336 * part of setup. This comes from the cdc_recv_setup
337 * op that is device specific.
338 *
339 */
340 musb_peri_ep0_ack_req();
341
342 ep0_endpoint->rcv_urb = ep0_urb;
343 ep0_urb->actual_length = 0;
344 SET_EP0_STATE(RX);
345}
346
347static void musb_peri_ep0_tx_data_request(int err)
348{
349 if (err) {
350 musb_peri_ep0_stall();
351 SET_EP0_STATE(IDLE);
352 } else {
353 musb_peri_ep0_ack_req();
354
355 ep0_endpoint->tx_urb = ep0_urb;
356 ep0_endpoint->sent = 0;
357 SET_EP0_STATE(TX);
358 }
359}
360
361static void musb_peri_ep0_idle(void)
362{
363 u16 count0;
364 int err;
365 u16 csr0;
366
367 /*
368 * Verify addresses
369 * A lot of confusion can be caused if the address
370 * in software, udc layer, does not agree with the
371 * hardware. Since the setting of the hardware address
372 * must be set after the set address request, the
373 * usb state machine is out of sync for a few frame.
374 * It is a good idea to run this check when changes
375 * are made to the state machine.
376 */
377 if ((debug_level > 0) &&
378 (ep0_state != SET_ADDRESS)) {
379 u8 faddr;
380
381 faddr = readb(&musbr->faddr);
382 if (udc_device->address != faddr) {
383 serial_printf("ERROR : %s addresses do not"
384 "match sw %d vs hw %d\n",
385 __PRETTY_FUNCTION__,
386 udc_device->address, faddr);
387 udelay(1000 * 1000);
388 hang();
389 }
390 }
391
392 csr0 = readw(&musbr->ep[0].ep0.csr0);
393
394 if (!(MUSB_CSR0_RXPKTRDY & csr0))
395 goto end;
396
397 count0 = readw(&musbr->ep[0].ep0.count0);
398 if (count0 == 0)
399 goto end;
400
401 if (count0 != 8) {
402 if ((debug_setup) && (debug_level > 1))
403 serial_printf("WARN : %s SETUP incorrect size %d\n",
404 __PRETTY_FUNCTION__, count0);
405 musb_peri_ep0_stall();
406 goto end;
407 }
408
409 read_fifo(0, count0, &ep0_urb->device_request);
410
411 if (debug_level > 2)
412 print_usb_device_request(&ep0_urb->device_request);
413
414 if (ep0_urb->device_request.wLength == 0) {
415 err = ep0_recv_setup(ep0_urb);
416
417 /* Zero data request */
418 musb_peri_ep0_zero_data_request(err);
419 } else {
420 /* Is data coming or going ? */
421 u8 reqType = ep0_urb->device_request.bmRequestType;
422
423 if (USB_REQ_DEVICE2HOST == (reqType & USB_REQ_DIRECTION_MASK)) {
424 err = ep0_recv_setup(ep0_urb);
425 /* Device to host */
426 musb_peri_ep0_tx_data_request(err);
427 } else {
428 /*
429 * Host to device
430 *
431 * The RX routine will call ep0_recv_setup
432 * when the data packet has arrived.
433 */
434 musb_peri_ep0_rx_data_request();
435 }
436 }
437
438end:
439 return;
440}
441
442static void musb_peri_ep0_rx(void)
443{
444 /*
445 * This is the completion of the data OUT / RX
446 *
447 * Host is sending data to ep0 that is not
448 * part of setup. This comes from the cdc_recv_setup
449 * op that is device specific.
450 *
451 * Pass the data back to driver ep0_recv_setup which
452 * should give the cdc_recv_setup the chance to handle
453 * the rx
454 */
455 u16 csr0;
456 u16 count0;
457
458 if (debug_level > 3) {
459 if (0 != ep0_urb->actual_length) {
460 serial_printf("%s finished ? %d of %d\n",
461 __PRETTY_FUNCTION__,
462 ep0_urb->actual_length,
463 ep0_urb->device_request.wLength);
464 }
465 }
466
467 if (ep0_urb->device_request.wLength == ep0_urb->actual_length) {
468 musb_peri_ep0_last();
469 SET_EP0_STATE(IDLE);
470 ep0_recv_setup(ep0_urb);
471 return;
472 }
473
474 csr0 = readw(&musbr->ep[0].ep0.csr0);
475 if (!(MUSB_CSR0_RXPKTRDY & csr0))
476 return;
477
478 count0 = readw(&musbr->ep[0].ep0.count0);
479
480 if (count0) {
481 struct usb_endpoint_instance *endpoint;
482 u32 length;
483 u8 *data;
484
485 endpoint = ep0_endpoint;
486 if (endpoint && endpoint->rcv_urb) {
487 struct urb *urb = endpoint->rcv_urb;
488 unsigned int remaining_space = urb->buffer_length -
489 urb->actual_length;
490
491 if (remaining_space) {
492 int urb_bad = 0; /* urb is good */
493
494 if (count0 > remaining_space)
495 length = remaining_space;
496 else
497 length = count0;
498
499 data = (u8 *) urb->buffer_data;
500 data += urb->actual_length;
501
502 /* The common musb fifo reader */
503 read_fifo(0, length, data);
504
505 musb_peri_ep0_ack_req();
506
507 /*
508 * urb's actual_length is updated in
509 * usbd_rcv_complete
510 */
511 usbd_rcv_complete(endpoint, length, urb_bad);
512
513 } else {
514 if (debug_level > 0)
515 serial_printf("ERROR : %s no space in "
516 "rcv buffer\n",
517 __PRETTY_FUNCTION__);
518 }
519 } else {
520 if (debug_level > 0)
521 serial_printf("ERROR : %s problem with "
522 "endpoint\n",
523 __PRETTY_FUNCTION__);
524 }
525 } else {
526 if (debug_level > 0)
527 serial_printf("ERROR : %s with nothing to do\n",
528 __PRETTY_FUNCTION__);
529 }
530}
531
532static void musb_peri_ep0_tx(void)
533{
534 u16 csr0;
535 int transfer_size = 0;
536 unsigned int p, pm;
537
538 csr0 = readw(&musbr->ep[0].ep0.csr0);
539
540 /* Check for pending tx */
541 if (csr0 & MUSB_CSR0_TXPKTRDY)
542 goto end;
543
544 /* Check if this is the last packet sent */
545 if (ep0_endpoint->sent >= ep0_urb->actual_length) {
546 SET_EP0_STATE(IDLE);
547 goto end;
548 }
549
550 transfer_size = ep0_urb->actual_length - ep0_endpoint->sent;
551 /* Is the transfer size negative ? */
552 if (transfer_size <= 0) {
553 if (debug_level > 0)
554 serial_printf("ERROR : %s problem with the"
555 " transfer size %d\n",
556 __PRETTY_FUNCTION__,
557 transfer_size);
558 SET_EP0_STATE(IDLE);
559 goto end;
560 }
561
562 /* Truncate large transfers to the fifo size */
563 if (transfer_size > ep0_endpoint->tx_packetSize)
564 transfer_size = ep0_endpoint->tx_packetSize;
565
566 write_fifo(0, transfer_size, &ep0_urb->buffer[ep0_endpoint->sent]);
567 ep0_endpoint->sent += transfer_size;
568
569 /* Done or more to send ? */
570 if (ep0_endpoint->sent >= ep0_urb->actual_length)
571 musb_ep0_tx_ready_and_last();
572 else
573 musb_ep0_tx_ready();
574
575 /* Wait a bit */
576 pm = 10;
577 for (p = 0; p < pm; p++) {
578 csr0 = readw(&musbr->ep[0].ep0.csr0);
579 if (!(csr0 & MUSB_CSR0_TXPKTRDY))
580 break;
581
582 /* Double the delay. */
583 udelay(1 << pm);
584 }
585
586 if ((ep0_endpoint->sent >= ep0_urb->actual_length) && (p < pm))
587 SET_EP0_STATE(IDLE);
588
589end:
590 return;
591}
592
593static void musb_peri_ep0(void)
594{
595 u16 csr0;
596
597 if (SET_ADDRESS == ep0_state)
598 return;
599
600 csr0 = readw(&musbr->ep[0].ep0.csr0);
601
602 /* Error conditions */
603 if (MUSB_CSR0_P_SENTSTALL & csr0) {
604 csr0 &= ~MUSB_CSR0_P_SENTSTALL;
605 writew(csr0, &musbr->ep[0].ep0.csr0);
606 SET_EP0_STATE(IDLE);
607 }
608 if (MUSB_CSR0_P_SETUPEND & csr0) {
609 csr0 |= MUSB_CSR0_P_SVDSETUPEND;
610 writew(csr0, &musbr->ep[0].ep0.csr0);
611 SET_EP0_STATE(IDLE);
612 if ((debug_setup) && (debug_level > 1))
613 serial_printf("WARN: %s SETUPEND\n",
614 __PRETTY_FUNCTION__);
615 }
616
617 /* Normal states */
618 if (IDLE == ep0_state)
619 musb_peri_ep0_idle();
620
621 if (TX == ep0_state)
622 musb_peri_ep0_tx();
623
624 if (RX == ep0_state)
625 musb_peri_ep0_rx();
626}
627
628static void musb_peri_rx_ep(unsigned int ep)
629{
Pankaj Bharadiya3f0be8e2012-09-13 09:38:16 +0000630 u16 peri_rxcount;
631 u8 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
Tom Rixf298e4b2009-10-31 12:37:41 -0500632
Pankaj Bharadiya3f0be8e2012-09-13 09:38:16 +0000633 if (!(peri_rxcsr & MUSB_RXCSR_RXPKTRDY)) {
634 if (debug_level > 0)
635 serial_printf("ERROR : %s %d without MUSB_RXCSR_RXPKTRDY set\n",
636 __PRETTY_FUNCTION__, ep);
637 return;
638 }
639
640 peri_rxcount = readw(&musbr->ep[ep].epN.rxcount);
Tom Rixf298e4b2009-10-31 12:37:41 -0500641 if (peri_rxcount) {
642 struct usb_endpoint_instance *endpoint;
643 u32 length;
644 u8 *data;
645
646 endpoint = GET_ENDPOINT(udc_device, ep);
647 if (endpoint && endpoint->rcv_urb) {
648 struct urb *urb = endpoint->rcv_urb;
649 unsigned int remaining_space = urb->buffer_length -
650 urb->actual_length;
651
652 if (remaining_space) {
653 int urb_bad = 0; /* urb is good */
654
655 if (peri_rxcount > remaining_space)
656 length = remaining_space;
657 else
658 length = peri_rxcount;
659
660 data = (u8 *) urb->buffer_data;
661 data += urb->actual_length;
662
663 /* The common musb fifo reader */
664 read_fifo(ep, length, data);
665
666 musb_peri_rx_ack(ep);
667
668 /*
669 * urb's actual_length is updated in
670 * usbd_rcv_complete
671 */
672 usbd_rcv_complete(endpoint, length, urb_bad);
673
674 } else {
675 if (debug_level > 0)
676 serial_printf("ERROR : %s %d no space "
677 "in rcv buffer\n",
678 __PRETTY_FUNCTION__, ep);
679 }
680 } else {
681 if (debug_level > 0)
682 serial_printf("ERROR : %s %d problem with "
683 "endpoint\n",
684 __PRETTY_FUNCTION__, ep);
685 }
686
687 } else {
688 if (debug_level > 0)
689 serial_printf("ERROR : %s %d with nothing to do\n",
690 __PRETTY_FUNCTION__, ep);
691 }
692}
693
694static void musb_peri_rx(u16 intr)
695{
696 unsigned int ep;
697
698 /* Check for EP0 */
699 if (0x01 & intr)
700 musb_peri_ep0();
701
702 for (ep = 1; ep < 16; ep++) {
703 if ((1 << ep) & intr)
704 musb_peri_rx_ep(ep);
705 }
706}
707
708static void musb_peri_tx(u16 intr)
709{
710 /* Check for EP0 */
711 if (0x01 & intr)
712 musb_peri_ep0_tx();
713
714 /*
715 * Use this in the future when handling epN tx
716 *
717 * u8 ep;
718 *
719 * for (ep = 1; ep < 16; ep++) {
720 * if ((1 << ep) & intr) {
721 * / * handle tx for this endpoint * /
722 * }
723 * }
724 */
725}
726
727void udc_irq(void)
728{
729 /* This is a high freq called function */
730 if (enabled) {
731 u8 intrusb;
732
733 intrusb = readb(&musbr->intrusb);
734
735 /*
736 * See drivers/usb/gadget/mpc8xx_udc.c for
737 * state diagram going from detached through
738 * configuration.
739 */
740 if (MUSB_INTR_RESUME & intrusb) {
741 usbd_device_event_irq(udc_device,
742 DEVICE_BUS_ACTIVITY, 0);
743 musb_peri_resume();
744 }
745
746 musb_peri_ep0();
747
748 if (MUSB_INTR_RESET & intrusb) {
749 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
750 musb_peri_reset();
751 }
752
753 if (MUSB_INTR_DISCONNECT & intrusb) {
754 /* cable unplugged from hub/host */
755 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
756 musb_peri_reset();
757 usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
758 }
759
760 if (MUSB_INTR_SOF & intrusb) {
761 usbd_device_event_irq(udc_device,
762 DEVICE_BUS_ACTIVITY, 0);
763 musb_peri_resume();
764 }
765
766 if (MUSB_INTR_SUSPEND & intrusb) {
767 usbd_device_event_irq(udc_device,
768 DEVICE_BUS_INACTIVE, 0);
769 }
770
771 if (ep0_state != SET_ADDRESS) {
772 u16 intrrx, intrtx;
773
774 intrrx = readw(&musbr->intrrx);
775 intrtx = readw(&musbr->intrtx);
776
777 if (intrrx)
778 musb_peri_rx(intrrx);
779
780 if (intrtx)
781 musb_peri_tx(intrtx);
782 } else {
783 if (MUSB_INTR_SOF & intrusb) {
784 u8 faddr;
785 faddr = readb(&musbr->faddr);
786 /*
787 * Setting of the address can fail.
788 * Normally it succeeds the second time.
789 */
790 if (udc_device->address != faddr)
791 musb_peri_ep0_set_address();
792 }
793 }
794 }
795}
796
797void udc_set_nak(int ep_num)
798{
799 /* noop */
800}
801
802void udc_unset_nak(int ep_num)
803{
804 /* noop */
805}
806
807int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
808{
809 int ret = 0;
810
811 /* Transmit only if the hardware is available */
812 if (endpoint->tx_urb && endpoint->state == 0) {
813 unsigned int ep = endpoint->endpoint_address &
814 USB_ENDPOINT_NUMBER_MASK;
815
816 u16 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
817
818 /* Error conditions */
819 if (peri_txcsr & MUSB_TXCSR_P_UNDERRUN) {
820 peri_txcsr &= ~MUSB_TXCSR_P_UNDERRUN;
821 writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
822 }
823
824 if (debug_level > 1)
825 musb_print_txcsr(peri_txcsr);
826
827 /* Check if a packet is waiting to be sent */
828 if (!(peri_txcsr & MUSB_TXCSR_TXPKTRDY)) {
829 u32 length;
830 u8 *data;
831 struct urb *urb = endpoint->tx_urb;
832 unsigned int remaining_packet = urb->actual_length -
833 endpoint->sent;
834
835 if (endpoint->tx_packetSize < remaining_packet)
836 length = endpoint->tx_packetSize;
837 else
838 length = remaining_packet;
839
840 data = (u8 *) urb->buffer;
841 data += endpoint->sent;
842
843 /* common musb fifo function */
844 write_fifo(ep, length, data);
845
846 musb_peri_tx_ready(ep);
847
848 endpoint->last = length;
849 /* usbd_tx_complete will take care of updating 'sent' */
850 usbd_tx_complete(endpoint);
851 }
852 } else {
853 if (debug_level > 0)
854 serial_printf("ERROR : %s Problem with urb %p "
855 "or ep state %d\n",
856 __PRETTY_FUNCTION__,
857 endpoint->tx_urb, endpoint->state);
858 }
859
860 return ret;
861}
862
863void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
864 struct usb_endpoint_instance *endpoint)
865{
866 if (0 == id) {
867 /* EP0 */
868 ep0_endpoint = endpoint;
869 ep0_endpoint->endpoint_address = 0xff;
870 ep0_urb = usbd_alloc_urb(device, endpoint);
871 } else if (MAX_ENDPOINT >= id) {
872 int ep_addr;
873
874 /* Check the direction */
875 ep_addr = endpoint->endpoint_address;
876 if (USB_DIR_IN == (ep_addr & USB_ENDPOINT_DIR_MASK)) {
877 /* IN */
878 epinfo[(id * 2) + 1].epsize = endpoint->tx_packetSize;
879 } else {
880 /* OUT */
881 epinfo[id * 2].epsize = endpoint->rcv_packetSize;
882 }
883
Axel Line31dc612013-06-23 00:57:46 +0800884 musb_configure_ep(&epinfo[0], ARRAY_SIZE(epinfo));
Tom Rixf298e4b2009-10-31 12:37:41 -0500885 } else {
886 if (debug_level > 0)
887 serial_printf("ERROR : %s endpoint request %d "
888 "exceeds maximum %d\n",
889 __PRETTY_FUNCTION__, id, MAX_ENDPOINT);
890 }
891}
892
893void udc_connect(void)
894{
895 /* noop */
896}
897
898void udc_disconnect(void)
899{
900 /* noop */
901}
902
903void udc_enable(struct usb_device_instance *device)
904{
905 /* Save the device structure pointer */
906 udc_device = device;
907
908 enabled = 1;
909}
910
911void udc_disable(void)
912{
913 enabled = 0;
914}
915
916void udc_startup_events(struct usb_device_instance *device)
917{
918 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
919 usbd_device_event_irq(device, DEVICE_INIT, 0);
920
921 /*
922 * The DEVICE_CREATE event puts the USB device in the state
923 * STATE_ATTACHED.
924 */
925 usbd_device_event_irq(device, DEVICE_CREATE, 0);
926
927 /* Resets the address to 0 */
928 usbd_device_event_irq(device, DEVICE_RESET, 0);
929
930 udc_enable(device);
931}
932
933int udc_init(void)
934{
935 int ret;
936 int ep_loop;
937
938 ret = musb_platform_init();
939 if (ret < 0)
940 goto end;
941
942 /* Configure all the endpoint FIFO's and start usb controller */
943 musbr = musb_cfg.regs;
944
945 /* Initialize the endpoints */
Heinrich Schuchardt7f2e59a2017-04-15 14:29:54 +0200946 for (ep_loop = 0; ep_loop <= MAX_ENDPOINT * 2; ep_loop++) {
Tom Rixf298e4b2009-10-31 12:37:41 -0500947 epinfo[ep_loop].epnum = (ep_loop / 2) + 1;
948 epinfo[ep_loop].epdir = ep_loop % 2; /* OUT, IN */
949 epinfo[ep_loop].epsize = 0;
950 }
951
952 musb_peri_softconnect();
953
954 ret = 0;
955end:
956
957 return ret;
958}