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
|
/*
* PC-specific code for
* USB Enhanced Host Controller Interface (EHCI) driver
* High speed USB 2.0.
*/
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "../port/pci.h"
#include "../port/error.h"
#include "../port/usb.h"
#include "usbehci.h"
static Ctlr* ctlrs[Nhcis];
static int maxehci = Nhcis;
static int
ehciecap(Ctlr *ctlr, int cap)
{
int i, off;
off = (ctlr->capio->capparms >> Ceecpshift) & Ceecpmask;
for(i=0; i<48; i++){
if(off < 0x40 || (off & 3) != 0)
break;
if(pcicfgr8(ctlr->pcidev, off) == cap)
return off;
off = pcicfgr8(ctlr->pcidev, off+1);
}
return -1;
}
static void
getehci(Ctlr* ctlr)
{
int i, off;
off = ehciecap(ctlr, Clegacy);
if(off == -1)
return;
if(getconf("*noehcihandoff") == nil && pcicfgr8(ctlr->pcidev, off+CLbiossem) != 0){
dprint("ehci %#p: bios active, taking over...\n", ctlr->capio);
pcicfgw8(ctlr->pcidev, off+CLossem, 1);
for(i = 0; i < 100; i++){
if(pcicfgr8(ctlr->pcidev, off+CLbiossem) == 0)
break;
delay(10);
}
if(i == 100)
print("ehci %#p: bios timed out\n", ctlr->capio);
}
pcicfgw32(ctlr->pcidev, off+CLcontrol, 0); /* no SMIs */
}
static void
ehcireset(Ctlr *ctlr)
{
Eopio *opio;
int i;
ilock(ctlr);
dprint("ehci %#p reset\n", ctlr->capio);
opio = ctlr->opio;
/*
* reclaim from bios
*/
getehci(ctlr);
/*
* halt and route ports to companion controllers
* until we are setup
*/
ehcirun(ctlr, 0);
opio->config = 0;
coherence();
/* clear high 32 bits of address signals if it's 64 bits capable.
* This is probably not needed but it does not hurt and others do it.
*/
if((ctlr->capio->capparms & C64) != 0){
dprint("ehci: 64 bits\n");
opio->seg = 0;
coherence();
}
if(ehcidebugcapio != ctlr->capio){
opio->cmd |= Chcreset; /* controller reset */
coherence();
for(i = 0; i < 100; i++){
if((opio->cmd & Chcreset) == 0)
break;
delay(1);
}
if(i == 100)
print("ehci %#p controller reset timed out\n", ctlr->capio);
}
opio->cmd |= Citc1; /* 1 intr. per µframe */
coherence();
switch(opio->cmd & Cflsmask){
case Cfls1024:
ctlr->nframes = 1024;
break;
case Cfls512:
ctlr->nframes = 512;
break;
case Cfls256:
ctlr->nframes = 256;
break;
default:
panic("ehci: unknown fls %ld", opio->cmd & Cflsmask);
}
dprint("ehci: %d frames\n", ctlr->nframes);
iunlock(ctlr);
}
static void
setdebug(Hci*, int d)
{
ehcidebug = d;
}
static void
shutdown(Hci *hp)
{
int i;
Ctlr *ctlr;
Eopio *opio;
ctlr = hp->aux;
ilock(ctlr);
opio = ctlr->opio;
opio->cmd |= Chcreset; /* controller reset */
coherence();
for(i = 0; i < 100; i++){
if((opio->cmd & Chcreset) == 0)
break;
delay(1);
}
if(i >= 100)
print("ehci %#p controller reset timed out\n", ctlr->capio);
delay(100);
ehcirun(ctlr, 0);
opio->frbase = 0;
iunlock(ctlr);
}
static void
scanpci(void)
{
static int already = 0;
int i;
uvlong io;
Ctlr *ctlr;
Pcidev *p;
Ecapio *capio;
if(already)
return;
already = 1;
p = nil;
while ((p = pcimatch(p, 0, 0)) != nil) {
/*
* Find EHCI controllers (Programming Interface = 0x20).
*/
if(p->ccrb != Pcibcserial || p->ccru != Pciscusb)
continue;
switch(p->ccrp){
case 0x20:
if(p->mem[0].bar & 1)
continue;
io = p->mem[0].bar & ~0x0f;
break;
default:
continue;
}
if(io == 0)
continue;
print("usbehci: %#x %#x: port %llux size %d irq %d\n",
p->vid, p->did, io, p->mem[0].size, p->intl);
ctlr = malloc(sizeof(Ctlr));
if(ctlr == nil){
print("usbehci: no memory\n");
continue;
}
capio = vmap(io, p->mem[0].size);
if(capio == nil){
print("usbehci: cannot map mmio\n");
free(ctlr);
continue;
}
ctlr->pcidev = p;
ctlr->base = io;
ctlr->capio = capio;
for(i = 0; i < Nhcis; i++)
if(ctlrs[i] == nil){
ctlrs[i] = ctlr;
break;
}
if(i >= Nhcis)
print("ehci: bug: more than %d controllers\n", Nhcis);
/*
* currently, if we enable a second ehci controller,
* we'll wedge solid after iunlock in init for the second one.
*/
if (i >= maxehci) {
iprint("usbehci: ignoring controllers after first %d, "
"at %.8llux\n", maxehci, io);
ctlrs[i] = nil;
}
}
}
static int
reset(Hci *hp)
{
int i;
char *s;
Ctlr *ctlr;
Ecapio *capio;
Pcidev *p;
static Lock resetlck;
s = getconf("*maxehci");
if (s != nil && s[0] >= '0' && s[0] <= '9')
maxehci = atoi(s);
if(maxehci == 0 || getconf("*nousbehci"))
return -1;
ilock(&resetlck);
scanpci();
/*
* Any adapter matches if no hp->port is supplied,
* otherwise the ports must match.
*/
ctlr = nil;
for(i = 0; i < Nhcis && ctlrs[i] != nil; i++){
ctlr = ctlrs[i];
if(ctlr->active == 0)
if(hp->port == 0 || hp->port == ctlr->base){
ctlr->active = 1;
break;
}
}
iunlock(&resetlck);
if(i >= Nhcis || ctlrs[i] == nil)
return -1;
p = ctlr->pcidev;
pcienable(p);
hp->aux = ctlr;
hp->port = ctlr->base;
hp->irq = p->intl;
hp->tbdf = p->tbdf;
capio = ctlr->capio;
hp->nports = capio->parms & Cnports;
ddprint("echi: %s, ncc %lud npcc %lud\n",
capio->parms & 0x10000 ? "leds" : "no leds",
(capio->parms >> 12) & 0xf, (capio->parms >> 8) & 0xf);
ddprint("ehci: routing %s, %sport power ctl, %d ports\n",
capio->parms & 0x40 ? "explicit" : "automatic",
capio->parms & 0x10 ? "" : "no ", hp->nports);
ctlr->opio = (Eopio*)((uintptr)capio + (capio->cap & 0xff));
ehcireset(ctlr);
ehcimeminit(ctlr);
pcisetbme(p);
/*
* Linkage to the generic HCI driver.
*/
ehcilinkage(hp);
hp->shutdown = shutdown;
hp->debug = setdebug;
intrenable(hp->irq, hp->interrupt, hp, hp->tbdf, hp->type);
return 0;
}
void
usbehcilink(void)
{
addhcitype("ehci", reset);
}
|