Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
427 farthen 6
//    This file is part of emCORE.
15 theseven 7
//
427 farthen 8
//    emCORE is free software: you can redistribute it and/or
15 theseven 9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
427 farthen 13
//    emCORE is distributed in the hope that it will be useful,
15 theseven 14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
427 farthen 19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
15 theseven 20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "mmu.h"
26
#include "panic.h"
27
#include "usbdrv.h"
28
#include "thread.h"
29
#include "timer.h"
30
#include "usb.h"
31
#include "usb_ch9.h"
32
#include "synopsysotg.h"
58 theseven 33
#include "util.h"
85 theseven 34
#include "interrupt.h"
87 theseven 35
#include "clockgates.h"
221 theseven 36
#include "power.h"
15 theseven 37
 
38
 
39
struct ep_type
40
{
41
    bool active;
42
    bool busy;
43
    bool done;
44
    int rc;
45
    int size;
46
    struct wakeup complete;
47
} ;
48
 
49
static struct ep_type endpoints[5];
50
static struct usb_ctrlrequest ctrlreq CACHEALIGN_ATTR;
429 theseven 51
static struct scheduler_thread synopsysotg_thread_handle;
221 theseven 52
static uint32_t synopsysotg_stack[0x40] STACK_ATTR;
15 theseven 53
 
54
int usb_drv_port_speed(void)
55
{
56
    return (DSTS & 2) == 0 ? 1 : 0;
57
}
58
 
66 theseven 59
static void reset_endpoints(int reinit)
15 theseven 60
{
61
    unsigned int i;
62
    for (i = 0; i < sizeof(endpoints)/sizeof(struct ep_type); i++)
63
    {
64
        if (reinit) endpoints[i].active = false;
65
        endpoints[i].busy = false;
66
        endpoints[i].rc = -1;
67
        endpoints[i].done = true;
68
        wakeup_signal(&endpoints[i].complete);
69
    }
70
    DIEPCTL0 = 0x8800;  /* EP0 IN ACTIVE NEXT=1 */
71
    DOEPCTL0 = 0x8000;  /* EP0 OUT ACTIVE */
72
    DOEPTSIZ0 = 0x20080040;  /* EP0 OUT Transfer Size:
73
                                64 Bytes, 1 Packet, 1 Setup Packet */
265 theseven 74
    DOEPDMA0 = &ctrlreq;
15 theseven 75
    DOEPCTL0 |= 0x84000000;  /* EP0 OUT ENABLE CLEARNAK */
76
    if (reinit)
77
    {
78
        /* The size is getting set to zero, because we don't know
79
           whether we are Full Speed or High Speed at this stage */
80
        /* EP1 IN INACTIVE DATA0 SIZE=0 NEXT=3 */
81
        DIEPCTL1 = 0x10001800;
82
        /* EP2 OUT INACTIVE DATA0 SIZE=0 */
83
        DOEPCTL2 = 0x10000000;
84
        /* EP3 IN INACTIVE DATA0 SIZE=0 NEXT=0 */
85
        DIEPCTL3 = 0x10000000;
86
        /* EP4 OUT INACTIVE DATA0 SIZE=0 */
87
        DOEPCTL4 = 0x10000000;
88
    }
89
    else
90
    {
91
        /* INACTIVE DATA0 */
92
        DIEPCTL1 = (DIEPCTL1 & ~0x00008000) | 0x10000000;
93
        DOEPCTL2 = (DOEPCTL2 & ~0x00008000) | 0x10000000;
94
        DIEPCTL3 = (DIEPCTL3 & ~0x00008000) | 0x10000000;
95
        DOEPCTL4 = (DOEPCTL4 & ~0x00008000) | 0x10000000;
96
    }
97
    DAINTMSK = 0xFFFFFFFF;  /* Enable interrupts on all EPs */
98
}
99
 
100
int usb_drv_request_endpoint(int type, int dir)
101
{
102
    size_t ep;
103
    int ret = -1;
104
 
105
    if (dir == USB_DIR_IN) ep = 1;
106
    else ep = 2;
107
 
108
    while (ep < 5)
109
    {
110
        if (!endpoints[ep].active)
111
        {
112
            endpoints[ep].active = true;
113
            ret = ep | dir;
114
            uint32_t newbits = (type << 18) | 0x10000000;
115
            if (dir) DIEPCTL(ep) = (DIEPCTL(ep) & ~0x000C0000) | newbits;
116
            else DOEPCTL(ep) = (DOEPCTL(ep) & ~0x000C0000) | newbits;
117
            break;
118
        }
119
        ep += 2;
120
    }
121
 
122
    return ret;
123
}
124
 
125
void usb_drv_release_endpoint(int ep)
126
{
127
    ep = ep & 0x7f;
128
 
129
    if (ep < 1 || ep > USB_NUM_ENDPOINTS) return;
130
 
131
    endpoints[ep].active = false;
132
}
133
 
134
static void usb_reset(void)
135
{
136
    DCTL = 0x802;  /* Soft Disconnect */
137
 
138
    OPHYPWR = 0;  /* PHY: Power up */
85 theseven 139
    OPHYUNK1 = 1;
15 theseven 140
    OPHYUNK2 = 0xE3F;
85 theseven 141
    OPHYCLK = SYNOPSYSOTG_CLOCK;
15 theseven 142
    ORSTCON = 1;  /* PHY: Assert Software Reset */
143
    udelay(10);
144
    ORSTCON = 0;  /* PHY: Deassert Software Reset */
274 theseven 145
    OPHYUNK3 = 0x600;
15 theseven 146
 
147
    GRSTCTL = 1;  /* OTG: Assert Software Reset */
148
    while (GRSTCTL & 1);  /* Wait for OTG to ack reset */
149
    while (!(GRSTCTL & 0x80000000));  /* Wait for OTG AHB master idle */
150
 
151
    GRXFSIZ = 0x00000200;  /* RX FIFO: 512 bytes */
152
    GNPTXFSIZ = 0x02000200;  /* Non-periodic TX FIFO: 512 bytes */
85 theseven 153
    GAHBCFG = SYNOPSYSOTG_AHBCFG;
15 theseven 154
    GUSBCFG = 0x1408;  /* OTG: 16bit PHY and some reserved bits */
155
 
156
    DCFG = 4;  /* Address 0 */
157
    DCTL = 0x800;  /* Soft Reconnect */
158
    DIEPMSK = 0x0D;  /* IN EP interrupt mask */
159
    DOEPMSK = 0x0D;  /* IN EP interrupt mask */
160
    DAINTMSK = 0xFFFFFFFF;  /* Enable interrupts on all endpoints */
161
    GINTMSK = 0xC3000;  /* Interrupt mask: IN event, OUT event, bus reset */
162
 
163
    reset_endpoints(1);
164
}
165
 
166
/* IRQ handler */
167
void INT_USB_FUNC(void)
168
{
169
    int i;
170
    uint32_t ints = GINTSTS;
171
    uint32_t epints;
172
    if (ints & 0x1000)  /* bus reset */
173
    {
174
        DCFG = 4;  /* Address 0 */
175
        reset_endpoints(1);
176
        usb_handle_bus_reset();
177
    }
178
 
179
    if (ints & 0x2000)  /* enumeration done, we now know the speed */
180
    {
181
        /* Set up the maximum packet sizes accordingly */
182
        uint32_t maxpacket = usb_drv_port_speed() ? 512 : 64;
183
        DIEPCTL1 = (DIEPCTL1 & ~0x000003FF) | maxpacket;
184
        DOEPCTL2 = (DOEPCTL2 & ~0x000003FF) | maxpacket;
185
        DIEPCTL3 = (DIEPCTL3 & ~0x000003FF) | maxpacket;
186
        DOEPCTL4 = (DOEPCTL4 & ~0x000003FF) | maxpacket;
187
    }
188
 
189
    if (ints & 0x40000)  /* IN EP event */
190
        for (i = 0; i < 4; i += i + 1)  // 0, 1, 3
191
            if (epints = DIEPINT(i))
192
            {
193
                if (epints & 1)  /* Transfer completed */
194
                {
195
                    int bytes = endpoints[i].size - (DIEPTSIZ(i) & 0x3FFFF);
196
                    if (endpoints[i].busy)
197
                    {
198
                        endpoints[i].busy = false;
199
                        endpoints[i].rc = 0;
200
                        endpoints[i].done = true;
201
                        usb_handle_transfer_complete(i, USB_DIR_IN, 0, bytes);
202
                        wakeup_signal(&endpoints[i].complete);
203
                    }
204
                }
205
                if (epints & 4)  /* AHB error */
206
                    panicf(PANIC_FATAL, "USB: AHB error on IN EP%d", i);
207
                if (epints & 8)  /* Timeout */
208
                {
209
                    if (endpoints[i].busy)
210
                    {
211
                        endpoints[i].busy = false;
212
                        endpoints[i].rc = 1;
213
                        endpoints[i].done = true;
214
                        wakeup_signal(&endpoints[i].complete);
215
                    }
216
                }
217
                DIEPINT(i) = epints;
218
            }
219
 
220
    if (ints & 0x80000)  /* OUT EP event */
221
        for (i = 0; i < 5; i += 2)  // 0, 2, 4
222
            if (epints = DOEPINT(i))
223
            {
224
                if (epints & 1)  /* Transfer completed */
225
                {
226
                    int bytes = endpoints[i].size - (DOEPTSIZ(i) & 0x3FFFF);
227
                    if (endpoints[i].busy)
228
                    {
229
                        endpoints[i].busy = false;
230
                        endpoints[i].rc = 0;
231
                        endpoints[i].done = true;
232
                        usb_handle_transfer_complete(i, USB_DIR_OUT, 0, bytes);
233
                        wakeup_signal(&endpoints[i].complete);
234
                    }
235
                }
236
                if (epints & 4)  /* AHB error */
237
                    panicf(PANIC_FATAL, "USB: AHB error on OUT EP%d", i);
238
                if (epints & 8)  /* SETUP phase done */
239
                {
240
                    invalidate_dcache();
241
                    if (i == 0) usb_handle_control_request(&ctrlreq);
242
                    else panicf(PANIC_FATAL, "USB: SETUP done on OUT EP%d!?", i);
243
                }
244
                /* Make sure EP0 OUT is set up to accept the next request */
245
                if (!i)
246
                {
247
                    DOEPTSIZ0 = 0x20080040;
265 theseven 248
                    DOEPDMA0 = &ctrlreq;
15 theseven 249
                    DOEPCTL0 |= 0x84000000;
250
                }
251
                DOEPINT(i) = epints;
252
            }
253
 
254
    GINTSTS = ints;
255
}
256
 
257
void usb_drv_set_address(int address)
258
{
259
    DCFG = (DCFG & ~0x7F0) | (address << 4);
260
}
261
 
66 theseven 262
static void ep_send(int ep, const void *ptr, int length)
15 theseven 263
{
264
    endpoints[ep].busy = true;
265
    endpoints[ep].size = length;
266
    DIEPCTL(ep) |= 0x8000;  /* EPx OUT ACTIVE */
267
    int blocksize = usb_drv_port_speed() ? 512 : 64;
268
    int packets = (length + blocksize - 1) / blocksize;
269
    if (!length)
270
    {
271
        DIEPTSIZ(ep) = 1 << 19;  /* one empty packet */
265 theseven 272
        DIEPDMA(ep) = NULL;  /* dummy address */
15 theseven 273
    }
274
    else
275
    {
276
        DIEPTSIZ(ep) = length | (packets << 19);
265 theseven 277
        DIEPDMA(ep) = ptr;
15 theseven 278
    }
279
    clean_dcache();
280
    DIEPCTL(ep) |= 0x84000000;  /* EPx OUT ENABLE CLEARNAK */
281
}
282
 
66 theseven 283
static void ep_recv(int ep, void *ptr, int length)
15 theseven 284
{
285
    endpoints[ep].busy = true;
286
    endpoints[ep].size = length;
287
    DOEPCTL(ep) &= ~0x20000;  /* EPx UNSTALL */
288
    DOEPCTL(ep) |= 0x8000;  /* EPx OUT ACTIVE */
289
    int blocksize = usb_drv_port_speed() ? 512 : 64;
290
    int packets = (length + blocksize - 1) / blocksize;
291
    if (!length)
292
    {
293
        DOEPTSIZ(ep) = 1 << 19;  /* one empty packet */
265 theseven 294
        DOEPDMA(ep) = NULL;  /* dummy address */
15 theseven 295
    }
296
    else
297
    {
298
        DOEPTSIZ(ep) = length | (packets << 19);
265 theseven 299
        DOEPDMA(ep) = ptr;
15 theseven 300
    }
636 theseven 301
    invalidate_dcache();
15 theseven 302
    DOEPCTL(ep) |= 0x84000000;  /* EPx OUT ENABLE CLEARNAK */
303
}
304
 
305
int usb_drv_send(int endpoint, const void *ptr, int length)
306
{
307
    endpoint &= 0x7f;
308
    endpoints[endpoint].done = false;
309
    ep_send(endpoint, ptr, length);
310
    while (!endpoints[endpoint].done && endpoints[endpoint].busy)
311
        wakeup_wait(&endpoints[endpoint].complete, TIMEOUT_BLOCK);
312
    return endpoints[endpoint].rc;
313
}
314
 
315
int usb_drv_send_nonblocking(int endpoint, const void *ptr, int length)
316
{
317
    ep_send(endpoint & 0x7f, ptr, length);
318
    return 0;
319
}
320
 
321
int usb_drv_recv(int endpoint, void* ptr, int length)
322
{
323
    ep_recv(endpoint & 0x7f, ptr, length);
324
    return 0;
325
}
326
 
327
void usb_drv_cancel_all_transfers(void)
328
{
329
    uint32_t mode = enter_critical_section();
330
    reset_endpoints(0);
331
    leave_critical_section(mode);
332
}
333
 
334
bool usb_drv_stalled(int endpoint, bool in)
335
{
336
    if (in) return DIEPCTL(endpoint) & 0x00200000 ? true : false;
337
    else return DOEPCTL(endpoint) & 0x00200000 ? true : false;
338
}
339
 
340
void usb_drv_stall(int endpoint, bool stall, bool in)
341
{
342
    if (in)
343
    {
344
        if (stall) DIEPCTL(endpoint) |= 0x00200000;
345
        else DIEPCTL(endpoint) &= ~0x00200000;
346
    }
347
    else
348
    {
349
        if (stall) DOEPCTL(endpoint) |= 0x00200000;
350
        else DOEPCTL(endpoint) &= ~0x00200000;
351
    }
352
}
353
 
225 theseven 354
void usb_drv_power_up(void)
355
{
356
    /* Enable USB clock */
221 theseven 357
    clockgate_enable(CLOCKGATE_USB_1, true);
358
    clockgate_enable(CLOCKGATE_USB_2, true);
225 theseven 359
    PCGCCTL = 0;
360
 
361
    /* reset the beast */
362
    usb_reset();
363
}
364
 
365
void usb_drv_power_down(void)
366
{
367
    DCTL = 0x802;  /* Soft Disconnect */
368
 
369
    ORSTCON = 1;  /* Put the PHY into reset (needed to get current down) */
370
    PCGCCTL = 1;  /* Shut down PHY clock */
371
    OPHYPWR = 0xF;  /* PHY: Power down */
372
 
221 theseven 373
    clockgate_enable(CLOCKGATE_USB_1, false);
374
    clockgate_enable(CLOCKGATE_USB_2, false);
225 theseven 375
}
376
 
221 theseven 377
void usb_check_vbus()
378
{
379
    bool oldstate = false;
380
    while (true)
381
    {
382
        sleep(200000);
383
        bool newstate = vbus_state();
384
        if (oldstate != newstate)
385
        {
386
            if (newstate) usb_drv_power_up();
387
            else usb_drv_power_down();
388
            oldstate = newstate;
389
        }
390
    }
391
}
392
 
15 theseven 393
void usb_drv_init(void)
394
{
395
    unsigned int i;
396
    for (i = 0; i < sizeof(endpoints)/sizeof(struct ep_type); i++)
397
        wakeup_init(&endpoints[i].complete);
398
 
399
    /* Enable USB clock */
87 theseven 400
    clockgate_enable(CLOCKGATE_USB_1, true);
401
    clockgate_enable(CLOCKGATE_USB_2, true);
15 theseven 402
    PCGCCTL = 0;
403
 
404
    /* unmask irq */
85 theseven 405
    interrupt_enable(IRQ_USB_FUNC, true);
15 theseven 406
 
429 theseven 407
    thread_create(&synopsysotg_thread_handle, "synopsysotg", usb_check_vbus,
408
                  synopsysotg_stack, sizeof(synopsysotg_stack), OS_THREAD, 63, true);
221 theseven 409
 
410
    usb_drv_power_down();
15 theseven 411
}
28 theseven 412
 
240 theseven 413
void usb_drv_exit(void)
414
{
415
    usb_drv_power_down();
416
}
417
 
28 theseven 418
int usb_drv_get_max_out_size()
419
{
420
    return usb_drv_port_speed() ? 262144 : 32768;
421
}
422
 
423
int usb_drv_get_max_in_size()
424
{
425
    return usb_drv_port_speed() ? 262144 : 32768;
426
}