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