summaryrefslogtreecommitdiffstats
path: root/utils/hwstub/stub/atj213x/usb_drv_atj213x.c
blob: ef6676652732c6101915a87150652d48c2c3184a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2014 by Marcin Bukat
                         Amaury Pouly
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "usb_drv.h"
#include "config.h"
#include "memory.h"
#include "target.h"
#include "atj213x.h"

#define USB_FULL_SPEED 0
#define USB_HIGH_SPEED 1

volatile bool setup_data_valid = false;
volatile int udc_speed = USB_FULL_SPEED;

static void usb_copy_from(void *ptr, volatile void *reg, size_t sz)
{
    uint32_t *p = ptr;
    volatile uint32_t *rp = reg;
    /* do not overflow the destination buffer ! */
    while(sz >= 4)
    {
        *p++ = *rp++;
        sz -= 4;
    }

    if(sz == 0)
        return;

    /* reminder */
    uint32_t cache = *rp;
    uint8_t *p8 = (void *)p;
    while(sz-- > 0)
    {
        *p8++ = cache;
        cache >>= 8;
    }
}

static void usb_copy_to(volatile void *reg, void *ptr, size_t sz)
{
    uint32_t *p = ptr;
    volatile uint32_t *rp = reg;
    sz = (sz + 3) / 4;
    /* read may overflow the source buffer but
     * it will not overwrite anything
     */
    while(sz-- > 0)
        *rp++ = *p++;
}

void INT_UDC(void)
{
    /* get possible sources */
    unsigned int usbirq = OTG_USBIRQ;
    unsigned int otgirq = OTG_OTGIRQ;
#if 0
    unsigned int usbeirq = OTG_USBEIRQ;
    unsigned int epinirq = OTG_IN04IRQ;
    unsigned int epoutirq = OTG_OUT04IRQ;
#endif

    /* HS, Reset, Setup */
    if (usbirq)
    {

        if (usbirq & (1<<5))
        {
            /* HS irq */
            udc_speed = USB_HIGH_SPEED;
        }
        else if (usbirq & (1<<4))
        {
            /* Reset */
            udc_speed = USB_FULL_SPEED;

            /* clear all pending irqs */
            OTG_OUT04IRQ = 0xff;
            OTG_IN04IRQ = 0xff;
        }
        else if (usbirq & (1<<0))
        {
            /* Setup data valid */
            setup_data_valid = true;
        }

        /* clear irq flags */
        OTG_USBIRQ = usbirq;
    }

#if 0
    if (epoutirq)
    {
        OTG_OUT04IRQ = epoutirq;
    }

    if (epinirq)
    {
        OTG_IN04IRQ = epinirq;
    }
#endif

    if (otgirq)
    {
        OTG_OTGIRQ = otgirq;
    }

    OTG_USBEIRQ = 0x50;
}

void usb_drv_init(void)
{
    OTG_USBCS |= 0x40;                    /* soft disconnect */

    OTG_ENDPRST = 0x10;                   /* reset all ep fifos */
    OTG_ENDPRST = 0x70;
    OTG_ENDPRST = 0x00;
    OTG_ENDPRST = 0x60;

    OTG_USBIRQ = 0xff;                    /* clear all pending interrupts */
    OTG_OTGIRQ = 0xff;
    OTG_IN04IRQ = 0xff;
    OTG_OUT04IRQ = 0xff;
    OTG_USBEIRQ = 0x50;                   /* UDC ? with 0x40 there is irq storm */

    OTG_USBIEN = (1<<5) | (1<<4) | (1<<0); /* HS, Reset, Setup_data */
    OTG_OTGIEN = 0;

    /* disable interrupts from ep0 */
    OTG_IN04IEN = 0;
    OTG_OUT04IEN = 0;

    /* unmask UDC interrupt in interrupt controller */
    INTC_MSK = (1<<4);

    target_mdelay(100);

    OTG_USBCS &= ~0x40;                  /* soft connect */
}

int usb_drv_recv_setup(struct usb_ctrlrequest *req)
{
    while (!setup_data_valid)
        ;

    usb_copy_from(req, &OTG_SETUPDAT, sizeof(struct usb_ctrlrequest));
    setup_data_valid = false;
    return 0;
}

int usb_drv_port_speed(void)
{
    return (int)udc_speed;
}

/* Set the address (usually it's in a register).
 * There is a problem here: some controller want the address to be set between
 * control out and ack and some want to wait for the end of the transaction.
 * In the first case, you need to write some code special code when getting
 * setup packets and ignore this function (have a look at other drives)
 */
void usb_drv_set_address(int address)
{
    (void)address;
    /* UDC sets this automaticaly */
}

/* TODO: Maybe adapt to irq scheme */
int usb_drv_send(int endpoint, void *ptr, int length)
{
    (void)endpoint;

    int xfer_size, cnt = length;

    while (cnt)
    {
        xfer_size = MIN(cnt, 64);

        /* copy data to ep0in buffer */
        usb_copy_to(&OTG_EP0INDAT, ptr, xfer_size);

        /* this marks data as ready to send */
        OTG_IN0BC = xfer_size;

        /* wait for the transfer end */
        while(OTG_EP0CS & 0x04)
            ;

        cnt -= xfer_size;
        ptr += xfer_size;
    }

    /* ZLP stage */
    if((length % 64) == 0)
        OTG_EP0CS = 2;

    return 0;
}

/* TODO:  Maybe adapt to irq scheme */
int usb_drv_recv(int endpoint, void* ptr, int length)
{
   (void)endpoint;
   int xfer_size, cnt = 0;

    while (cnt < length)
    {
        /* Arm receiving buffer by writing
         * any value to OUT0BC. This sets
         * OUT_BUSY bit in EP0CS until the data
         * are correctly received and ACK'd
         */
        OTG_OUT0BC = 0;

        while (OTG_EP0CS & 0x08)
            ;

        xfer_size = OTG_OUT0BC;

        usb_copy_from(ptr, &OTG_EP0OUTDAT, xfer_size);
        cnt += xfer_size;
        ptr += xfer_size;

        if (xfer_size < 64)
            break;
    }

    /* ZLP stage */
    if (length == 0)
        OTG_EP0CS = 2;

    return cnt;
}

void usb_drv_stall(int endpoint, bool stall, bool in)
{
    (void)endpoint;
    (void)in;

    /* only EP0 in hwstub */
    if (stall)
        OTG_EP0CS |= 1;
    else
        OTG_EP0CS &= ~1;
}

void usb_drv_exit(void)
{
}