-
Notifications
You must be signed in to change notification settings - Fork 32
/
usbio.c
357 lines (307 loc) · 7.86 KB
/
usbio.c
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "common.h"
#include "channel.h"
#if !CHIP
#define USB_BASE usb_base
static uint32_t _usb_base;
#define USB_BASE_INIT \
uint32_t usb_base = _usb_base;
#else
#define USB_BASE_INIT
#if CHIP == 3 // SC6530
#define USB_BASE 0x20300000
#else
#define USB_BASE 0x90000000
#endif
#endif
#define USB_CR(o) MEM4(USB_BASE + o)
#define USB_MAXREAD 64
// not necessary, because USB is already
// initialized by the bootloader
#define INIT_USB 1
#define USB_BUFSIZE 0x800
#if (USB_BUFSIZE) & (USB_BUFSIZE - 1)
#error
#endif
typedef struct {
uint32_t rpos, wpos;
uint8_t buf[USB_BUFSIZE];
} usb_buf_t;
usb_buf_t usb_buf;
static const uint8_t dev_desc[] ALIGN(4) = {
0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x40,
0x82, 0x17, 0x00, 0x4d, 0x02, 0x02, 0x00, 0x00,
0x00, 0x01
};
static const uint8_t config_desc[] ALIGN(4) = {
0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0xc0, 0x32,
0x09, 0x04, 0x00, 0x00, 0x02, 0xff, 0x00, 0x00, 0x00,
0x07, 0x05, 0x83, 0x02, 0x40, 0x00, 0x00,
0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00
};
enum {
USB_CTRL = 0,
INT_STS = 0x18,
INT_CLR = 0x1c,
TIMEOUT_LMT = 0x28,
TR_SIZE_IN_ENDP0 = 0x40,
REQ_SETUP_LOW = 0x5c,
REQ_SETUP_HIGH = 0x60,
ENDP0_CTRL = 0x64,
INT_CTRL_ENDP0 = 0x68,
INT_STS_ENDP0 = 0x6c,
INT_CLR_ENDP0 = 0x70,
ENDP1_CTRL = 0xc0,
TRANS_SIZE_ENDP1 = 0xc8,
INT_CTRL_ENDP1 = 0xcc,
INT_STS_ENDP1 = 0xd0,
INT_CLR_ENDP1 = 0xd4,
ENDP2_CTRL = 0x100,
RCV_DATA_ENDP2 = 0x104,
INT_CTRL_ENDP2 = 0x10c,
INT_STS_ENDP2 = 0x110,
INT_CLR_ENDP2 = 0x114,
ENDP3_CTRL = 0x140,
TRANS_SIZE_ENDP3 = 0x148,
INT_CTRL_ENDP3 = 0x14c,
INT_STS_ENDP3 = 0x150,
INT_CLR_ENDP3 = 0x154,
};
#define FIFO_entry_endp0_in (uint32_t*)(USB_BASE + 0x80000)
#define FIFO_entry_endp1 (FIFO_entry_endp0_in + 1)
#define FIFO_entry_endp3 (FIFO_entry_endp0_in + 2)
#if CHIP == 1 || CHIP == 3 // SC6531E, SC6530
#define FIFO_entry_endp_out (uint32_t*)(USB_BASE + 0x8000c)
#elif CHIP == 2 // SC6531DA
#define FIFO_entry_endp_out (uint32_t*)(USB_BASE + 0x80020)
#endif
#define FIFO_entry_endp2 (FIFO_entry_endp_out + 1)
/* max packet size */
#define USB_MAXPSIZE(o, n) \
(USB_CR(o) = (USB_CR(o) & ~0x7ff000) | (n) << 12)
/* transfer size */
#define USB_TRSIZE(o, n) \
(USB_CR(o) = (USB_CR(o) & ~0x1ffff) | (n))
#if INIT_USB
static void usb_init_endp0() {
USB_BASE_INIT
USB_MAXPSIZE(ENDP0_CTRL, 8);
USB_CR(INT_CLR_ENDP0) |= 1 << 8;
USB_CR(INT_CTRL_ENDP0) |= 1 << 8;
USB_CR(ENDP0_CTRL) |= 1 << 28; // buffer ready
}
static void usb_init_endp2() {
USB_BASE_INIT
USB_MAXPSIZE(ENDP2_CTRL, 0x40);
USB_TRSIZE(RCV_DATA_ENDP2, 0x2000);
USB_CR(INT_CLR_ENDP2) = 0x3fff;
USB_CR(INT_CTRL_ENDP2) = 0;
USB_CR(INT_CLR_ENDP2) |= 1;
USB_CR(INT_CTRL_ENDP2) |= 1;
USB_CR(ENDP2_CTRL) |= 1 << 25; // endpoint enable
USB_CR(ENDP2_CTRL) |= 1 << 28; // buffer ready
}
static void usb_init_endp3() {
USB_BASE_INIT
USB_MAXPSIZE(ENDP3_CTRL, 0x40);
USB_TRSIZE(TRANS_SIZE_ENDP3, 0x40);
USB_CR(INT_CLR_ENDP3) = 0x3fff;
USB_CR(INT_CTRL_ENDP3) = 0;
USB_CR(INT_CLR_ENDP3) |= 1 << 9;
USB_CR(INT_CTRL_ENDP3) |= 1 << 9;
USB_CR(ENDP3_CTRL) |= 1 << 25; // endpoint enable
}
#endif
// len = 0..0x7ff
static void usb_send(uint32_t ep, const void *src, uint32_t len) {
uint32_t i, ctrl, tr_size; uint32_t *fifo;
const uint32_t *s = (const uint32_t*)src;
USB_BASE_INIT
do {
if (ep == 0) {
ctrl = ENDP0_CTRL;
tr_size = TR_SIZE_IN_ENDP0;
fifo = FIFO_entry_endp0_in;
} else if (ep == 4) {
ctrl = ENDP3_CTRL;
tr_size = TRANS_SIZE_ENDP3;
fifo = FIFO_entry_endp3;
} else break;
USB_MAXPSIZE(ctrl, len);
USB_TRSIZE(tr_size, len);
for (i = 0; i < len; i += 4)
*(volatile uint32_t*)fifo = swap_be32(*s++);
USB_CR(ctrl) |= 1 << 27;
if (ep == 4) {
// TRANSFER_END
while ((USB_CR(INT_STS_ENDP3) & 1 << 9) == 0);
USB_CR(INT_CLR_ENDP3) |= 1 << 9;
}
} while (0);
}
static void usb_recv(uint32_t ep, uint32_t *dst, uint32_t len) {
uint32_t i, ctrl; uint32_t *fifo;
USB_BASE_INIT
do {
#if !CHIP
fifo = (uint32_t*)(USB_BASE + 0x8000c);
if (_chip == 2)
fifo += (uint32_t*)0x80020 - (uint32_t*)0x8000c;
#else
fifo = FIFO_entry_endp_out;
#endif
if (ep == 1) {
ctrl = ENDP0_CTRL;
} else if (ep == 3) {
ctrl = ENDP2_CTRL;
fifo += 1; // FIFO_entry_endp2
} else break;
for (i = 0; i < len; i += 8) {
*dst++ = swap_be32(*(volatile uint32_t*)fifo);
*dst++ = swap_be32(*(volatile uint32_t*)fifo);
}
USB_CR(ctrl) |= 1 << 28;
} while (0);
}
#define USB_REC_DEVICE 0
#define USB_REC_INTERFACE 1
#define USB_REC_MASK 0x1f
#define USB_REQ_STANDARD (0 << 5)
#define USB_REQ_CLASS (1 << 5)
#define USB_REQ_VENDOR (2 << 5)
#define USB_REQ_MASK (3 << 5)
#define USB_REQUEST_GET_DESCRIPTOR 6
#define USB_DEVICE_DESCRIPTOR_TYPE 1
#define USB_CONFIGURATION_DESCRIPTOR_TYPE 2
static void usb_send_desc(int type, int len) {
const void *p; int n;
if (type == USB_DEVICE_DESCRIPTOR_TYPE) {
p = dev_desc; n = sizeof(dev_desc);
} else if (type == USB_CONFIGURATION_DESCRIPTOR_TYPE) {
p = config_desc; n = sizeof(config_desc);
} else return;
if (len > n) len = n;
usb_send(0, p, len);
}
static void usb_int_endp0() {
uint32_t a, b, len, req;
USB_BASE_INIT
if (USB_CR(INT_STS_ENDP0) & 1 << 8) { // SETUP_TRANS_END
a = USB_CR(REQ_SETUP_LOW);
len = USB_CR(REQ_SETUP_HIGH) >> 16; // wLength
req = (a >> 8) & 0xff;
b = a & (USB_REC_MASK | USB_REQ_MASK);
if (b == (USB_REC_DEVICE | USB_REQ_STANDARD)) {
if (req == USB_REQUEST_GET_DESCRIPTOR)
usb_send_desc(a >> 24, len);
}
}
USB_CR(INT_CLR_ENDP0) = 0x3fff;
}
static void usb_int_endp2() {
usb_buf_t *p; int i, len, wpos;
USB_BASE_INIT
if (USB_CR(INT_STS_ENDP2) & 1) { // TRANSACTION_END
uint8_t buf[USB_MAXREAD];
len = USB_CR(ENDP2_CTRL) & (USB_BUFSIZE - 1);
if (len > USB_MAXREAD) len = USB_MAXREAD;
usb_recv(3, (uint32_t*)buf, len);
p = &usb_buf;
wpos = p->wpos;
for (i = 0; i < len; i++) {
p->buf[wpos++] = buf[i];
wpos &= (USB_BUFSIZE - 1);
}
p->wpos = wpos;
USB_CR(ENDP2_CTRL) |= 1 << 28;
}
USB_CR(INT_CLR_ENDP2) = 0x3fff;
}
static void usb_int_endp3() {
USB_BASE_INIT
USB_CR(INT_CLR_ENDP3) = 0x3fff;
}
static void usb_check_int() {
USB_BASE_INIT
if (_chip != 3 ?
MEM4(0x80001004) & 1 << 5 /* SC6531(DA/E) */ :
MEM4(0x80000004) & 1 << 25 /* SC6530 */) {
int mask = USB_CR(INT_STS);
if (mask & 0x3fff) {
if (mask & 1 << 10) usb_int_endp2();
if (mask & 1 << 11) usb_int_endp3();
if (mask & 1 << 8) usb_int_endp0();
}
USB_CR(INT_CLR) = 0x7f;
}
}
static int usb_channel_open(dl_channel_t *channel,
int baudrate) {
if (!channel->priv) {
#if !CHIP
uint32_t usb_base = _usb_base =
_chip == 3 ? 0x20300000 : 0x90000000;
#endif
usb_buf_t *p = &usb_buf;
p->rpos = 0;
p->wpos = 0;
channel->priv = (void*)1;
#if INIT_USB
USB_CR(USB_CTRL) |= 1; // USB_ENABLE
usb_init_endp0();
usb_init_endp2();
usb_init_endp3();
// 12MHz / 15 = 800kHz
USB_CR(TIMEOUT_LMT) = 15;
#endif
} else {
// set baudrate
}
return 0;
}
static int usb_channel_getchar(dl_channel_t *channel, int wait) {
usb_buf_t *p = &usb_buf;
unsigned rpos; int ret;
if (wait) {
for (;;) {
rpos = p->rpos;
if (rpos != p->wpos) break;
usb_check_int();
}
} else {
// usb_buf_free - 1 >= USB_MAXREAD
if (((p->rpos - p->wpos - 1) & (USB_BUFSIZE - 1)) >= USB_MAXREAD)
usb_check_int();
rpos = p->rpos;
if (rpos == p->wpos) return -1;
}
ret = p->buf[rpos++];
p->rpos = rpos & (USB_BUFSIZE - 1);
return ret;
}
static int usb_channel_write(dl_channel_t *channel,
const void *src, unsigned len) {
const uint8_t *s = (const uint8_t*)src;
for (; len > USB_MAXREAD; len -= USB_MAXREAD) {
usb_send(4, s, USB_MAXREAD);
s += USB_MAXREAD;
}
if (len) {
if (len == USB_MAXREAD) {
len >>= 1;
usb_send(4, s, len);
s += len;
}
usb_send(4, s, len);
}
return s - (uint8_t*)src;
}
static int usb_channel_close(dl_channel_t *channel) {
return 0;
}
dl_channel_t dl_usb_channel = {
usb_channel_open,
usb_channel_getchar,
usb_channel_write,
usb_channel_close,
NULL
};