~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to roms/u-boot/drivers/usb/musb/musb_udc.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

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