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
|
// Code for handling OHCI USB controllers.
//
// Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU LGPLv3 license.
#include "util.h" // dprintf
#include "pci.h" // pci_bdf_to_bus
#include "config.h" // CONFIG_*
#include "usb-ohci.h" // struct ohci_hcca
#include "pci_regs.h" // PCI_BASE_ADDRESS_0
#include "usb.h" // struct usb_s
#include "farptr.h" // GET_FLATPTR
#define FIT (1 << 31)
static int
start_ohci(struct usb_s *cntl, struct ohci_hcca *hcca)
{
u32 oldfminterval = readl(&cntl->ohci.regs->fminterval);
u32 oldrwc = readl(&cntl->ohci.regs->control) & OHCI_CTRL_RWC;
// XXX - check if already running?
// Do reset
writel(&cntl->ohci.regs->control, OHCI_USB_RESET | oldrwc);
readl(&cntl->ohci.regs->control); // flush writes
msleep(50);
// Do software init (min 10us, max 2ms)
u64 end = calc_future_tsc_usec(10);
writel(&cntl->ohci.regs->cmdstatus, OHCI_HCR);
for (;;) {
u32 status = readl(&cntl->ohci.regs->cmdstatus);
if (! status & OHCI_HCR)
break;
if (check_time(end)) {
dprintf(1, "Timeout on ohci software reset\n");
return -1;
}
}
// Init memory
writel(&cntl->ohci.regs->ed_controlhead, (u32)cntl->ohci.control_ed);
writel(&cntl->ohci.regs->ed_bulkhead, 0);
writel(&cntl->ohci.regs->hcca, (u32)hcca);
// Init fminterval
u32 fi = oldfminterval & 0x3fff;
writel(&cntl->ohci.regs->fminterval
, (((oldfminterval & FIT) ^ FIT)
| fi | (((6 * (fi - 210)) / 7) << 16)));
writel(&cntl->ohci.regs->periodicstart, ((9 * fi) / 10) & 0x3fff);
readl(&cntl->ohci.regs->control); // flush writes
// XXX - verify that fminterval was setup correctly.
// Go into operational state
writel(&cntl->ohci.regs->control
, (OHCI_CTRL_CBSR | OHCI_CTRL_CLE | OHCI_CTRL_PLE
| OHCI_USB_OPER | oldrwc));
readl(&cntl->ohci.regs->control); // flush writes
return 0;
}
static void
stop_ohci(struct usb_s *cntl)
{
u32 oldrwc = readl(&cntl->ohci.regs->control) & OHCI_CTRL_RWC;
writel(&cntl->ohci.regs->control, oldrwc);
readl(&cntl->ohci.regs->control); // flush writes
}
// Find any devices connected to the root hub.
static int
check_ohci_ports(struct usb_s *cntl)
{
// Turn on power for all devices on roothub.
u32 rha = readl(&cntl->ohci.regs->roothub_a);
rha &= ~(RH_A_PSM | RH_A_OCPM);
writel(&cntl->ohci.regs->roothub_status, RH_HS_LPSC);
writel(&cntl->ohci.regs->roothub_b, RH_B_PPCM);
msleep((rha >> 24) * 2);
// Count and reset connected devices
int ports = rha & RH_A_NDP;
int totalcount = 0;
int i;
for (i=0; i<ports; i++)
if (readl(&cntl->ohci.regs->roothub_portstatus[i]) & RH_PS_CCS) {
writel(&cntl->ohci.regs->roothub_portstatus[i], RH_PS_PRS);
totalcount++;
}
if (!totalcount)
// No devices connected
goto shutdown;
msleep(60); // XXX - should poll instead of using timer.
totalcount = 0;
for (i=0; i<ports; i++) {
u32 sts = readl(&cntl->ohci.regs->roothub_portstatus[i]);
if ((sts & (RH_PS_CCS|RH_PS_PES)) == (RH_PS_CCS|RH_PS_PES)) {
int count = configure_usb_device(cntl, !!(sts & RH_PS_LSDA));
if (! count)
// Shutdown port
writel(&cntl->ohci.regs->roothub_portstatus[i]
, RH_PS_CCS|RH_PS_LSDA);
totalcount += count;
}
}
if (!totalcount)
goto shutdown;
return totalcount;
shutdown:
// Turn off power to all ports
writel(&cntl->ohci.regs->roothub_status, RH_HS_LPS);
return 0;
}
void
ohci_init(void *data)
{
if (! CONFIG_USB_OHCI)
return;
struct usb_s *cntl = data;
// XXX - don't call pci_config_XXX from a thread
cntl->type = USB_TYPE_OHCI;
u32 baseaddr = pci_config_readl(cntl->bdf, PCI_BASE_ADDRESS_0);
cntl->ohci.regs = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
dprintf(3, "OHCI init on dev %02x:%02x.%x (regs=%p)\n"
, pci_bdf_to_bus(cntl->bdf), pci_bdf_to_dev(cntl->bdf)
, pci_bdf_to_fn(cntl->bdf), cntl->ohci.regs);
// Enable bus mastering and memory access.
pci_config_maskw(cntl->bdf, PCI_COMMAND
, 0, PCI_COMMAND_MASTER|PCI_COMMAND_MEMORY);
// XXX - check for and disable SMM control?
// Disable interrupts
writel(&cntl->ohci.regs->intrdisable, ~0);
writel(&cntl->ohci.regs->intrstatus, ~0);
// Allocate memory
struct ohci_hcca *hcca = memalign_high(256, sizeof(*hcca));
struct ohci_ed *control_ed = malloc_high(sizeof(*control_ed));
if (!hcca || !control_ed) {
dprintf(1, "No ram for ohci init\n");
return;
}
memset(hcca, 0, sizeof(*hcca));
memset(control_ed, 0, sizeof(*control_ed));
control_ed->hwINFO = ED_SKIP;
cntl->ohci.control_ed = control_ed;
int ret = start_ohci(cntl, hcca);
if (ret)
goto err;
int count = check_ohci_ports(cntl);
if (! count)
goto err;
return;
err:
stop_ohci(cntl);
free(hcca);
free(control_ed);
}
static int
wait_ed(struct ohci_ed *ed)
{
// XXX - 500ms just a guess
u64 end = calc_future_tsc(500);
for (;;) {
if (ed->hwHeadP == ed->hwTailP)
return 0;
if (check_time(end)) {
dprintf(1, "Timeout on wait_ed %p\n", ed);
return -1;
}
yield();
}
}
int
ohci_control(u32 endp, int dir, const void *cmd, int cmdsize
, void *data, int datasize)
{
if (! CONFIG_USB_OHCI)
return -1;
dprintf(5, "ohci_control %x\n", endp);
struct usb_s *cntl = endp2cntl(endp);
int maxpacket = endp2maxsize(endp);
int lowspeed = endp2speed(endp);
int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
// Setup transfer descriptors
struct ohci_td *tds = malloc_tmphigh(sizeof(*tds) * 3);
tds[0].hwINFO = TD_DP_SETUP | TD_T_DATA0 | TD_CC;
tds[0].hwCBP = (u32)cmd;
tds[0].hwNextTD = (u32)&tds[1];
tds[0].hwBE = (u32)cmd + cmdsize - 1;
tds[1].hwINFO = (dir ? TD_DP_IN : TD_DP_OUT) | TD_T_DATA1 | TD_CC;
tds[1].hwCBP = datasize ? (u32)data : 0;
tds[1].hwNextTD = (u32)&tds[2];
tds[1].hwBE = (u32)data + datasize - 1;
tds[2].hwINFO = (dir ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1 | TD_CC;
tds[2].hwCBP = 0;
tds[2].hwNextTD = (u32)&tds[3];
tds[2].hwBE = 0;
// Transfer data
struct ohci_ed *ed = cntl->ohci.control_ed;
ed->hwINFO = ED_SKIP;
barrier();
ed->hwHeadP = (u32)&tds[0];
ed->hwTailP = (u32)&tds[3];
barrier();
ed->hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
writel(&cntl->ohci.regs->cmdstatus, OHCI_CLF);
int ret = wait_ed(ed);
ed->hwINFO = ED_SKIP;
if (ret)
usleep(1); // XXX - in case controller still accessing tds
free(tds);
return ret;
}
struct ohci_pipe {
struct ohci_ed ed;
struct usb_pipe pipe;
void *data;
int count;
struct ohci_td *tds;
};
struct usb_pipe *
ohci_alloc_intr_pipe(u32 endp, int period)
{
if (! CONFIG_USB_OHCI)
return NULL;
dprintf(7, "ohci_alloc_intr_pipe %x %d\n", endp, period);
struct usb_s *cntl = endp2cntl(endp);
int maxpacket = endp2maxsize(endp);
int lowspeed = endp2speed(endp);
int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
// XXX - just grab 20 for now.
int count = 20;
struct ohci_pipe *pipe = malloc_low(sizeof(*pipe));
struct ohci_td *tds = malloc_low(sizeof(*tds) * count);
void *data = malloc_low(maxpacket * count);
if (!pipe || !tds || !data)
goto err;
struct ohci_ed *ed = &pipe->ed;
ed->hwHeadP = (u32)&tds[0];
ed->hwTailP = (u32)&tds[count-1];
ed->hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
ed->hwNextED = 0;
int i;
for (i=0; i<count-1; i++) {
tds[i].hwINFO = TD_DP_IN | TD_T_TOGGLE | TD_CC;
tds[i].hwCBP = (u32)data + maxpacket * i;
tds[i].hwNextTD = (u32)&tds[i+1];
tds[i].hwBE = tds[i].hwCBP + maxpacket - 1;
}
// XXX - need schedule - just add to primary list for now.
barrier();
struct ohci_hcca *hcca = (void*)cntl->ohci.regs->hcca;
for (i=0; i<ARRAY_SIZE(hcca->int_table); i++)
hcca->int_table[i] = (u32)ed;
pipe->data = data;
pipe->count = count;
pipe->tds = tds;
pipe->pipe.endp = endp;
return &pipe->pipe;
err:
free(pipe);
free(tds);
free(data);
return NULL;
}
int
ohci_poll_intr(struct usb_pipe *pipe, void *data)
{
ASSERT16();
if (! CONFIG_USB_OHCI)
return -1;
struct ohci_pipe *p = container_of(pipe, struct ohci_pipe, pipe);
struct ohci_td *tds = GET_FLATPTR(p->tds);
struct ohci_td *head = (void*)GET_FLATPTR(p->ed.hwHeadP);
struct ohci_td *tail = (void*)GET_FLATPTR(p->ed.hwTailP);
int count = GET_FLATPTR(p->count);
int pos = (tail - tds + 1) % count;
struct ohci_td *next = &tds[pos];
if (head == next)
// No intrs found.
return -1;
// XXX - check for errors.
// Copy data.
u32 endp = GET_FLATPTR(p->pipe.endp);
int maxpacket = endp2maxsize(endp);
void *pipedata = GET_FLATPTR(p->data);
void *intrdata = pipedata + maxpacket * pos;
memcpy_far(GET_SEG(SS), data
, FLATPTR_TO_SEG(intrdata), (void*)FLATPTR_TO_OFFSET(intrdata)
, maxpacket);
// Reenable this td.
SET_FLATPTR(tail->hwINFO, TD_DP_IN | TD_T_TOGGLE | TD_CC);
intrdata = pipedata + maxpacket * (tail-tds);
SET_FLATPTR(tail->hwCBP, (u32)intrdata);
SET_FLATPTR(tail->hwNextTD, (u32)next);
SET_FLATPTR(tail->hwBE, (u32)intrdata + maxpacket - 1);
SET_FLATPTR(p->ed.hwTailP, (u32)next);
return 0;
}
|