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