1 | /*␊ |
2 | * usb.c␊ |
3 | * ␊ |
4 | *␊ |
5 | * Created by mackerintel on 12/20/08.␊ |
6 | * Copyright 2008 mackerintel. All rights reserved.␊ |
7 | *␊ |
8 | */␊ |
9 | ␊ |
10 | #include "libsaio.h"␊ |
11 | #include "bootstruct.h"␊ |
12 | #include "pci.h"␊ |
13 | ␊ |
14 | #ifndef DEBUG_USB␊ |
15 | #define DEBUG_USB 0␊ |
16 | #endif␊ |
17 | ␊ |
18 | #if DEBUG_USB␊ |
19 | #define DBG(x...)␉printf(x)␊ |
20 | #else␊ |
21 | #define DBG(x...)␊ |
22 | #endif␊ |
23 | ␊ |
24 | #define kUSBBusFix␉␉␉"USBBusFix"␉␉␉␊ |
25 | #define kEHCIacquire␉␉"EHCIacquire"␉␉␊ |
26 | #define kUHCIreset␉␉␉"UHCIreset"␉␉␉␊ |
27 | #define kLegacyOff␉␉␉"USBLegacyOff"␉␉␊ |
28 | #define kEHCIhard␉␉␉"EHCIhard"␉␉␉␊ |
29 | ␊ |
30 | int usb_loop();␊ |
31 | void notify_usb_dev(pci_dt_t *pci_dev);␊ |
32 | ␊ |
33 | struct pciList* usbList = NULL;␊ |
34 | ␊ |
35 | static int legacy_off (pci_dt_t *pci_dev);␊ |
36 | static int ehci_acquire (pci_dt_t *pci_dev);␊ |
37 | static int uhci_reset (pci_dt_t *pci_dev);␊ |
38 | ␊ |
39 | // Add usb device to the list␊ |
40 | void notify_usb_dev(pci_dt_t *pci_dev)␊ |
41 | {␊ |
42 | ␉struct pciList* current = usbList;␊ |
43 | ␉if(!usbList)␊ |
44 | ␉{␊ |
45 | ␉␉usbList = (struct pciList*)malloc(sizeof(struct pciList));␊ |
46 | ␉␉usbList->next = NULL;␊ |
47 | ␉␉usbList->pciDev = pci_dev;␊ |
48 | ␉␉␊ |
49 | ␉}␊ |
50 | ␉else␊ |
51 | ␉{␊ |
52 | ␉␉while(current != NULL && current->next != NULL)␊ |
53 | ␉␉{␊ |
54 | ␉␉␉current = current->next;␊ |
55 | ␉␉}␊ |
56 | ␉␉current->next = (struct pciList*)malloc(sizeof(struct pciList));␊ |
57 | ␉␉current = current->next;␊ |
58 | ␉␉␊ |
59 | ␉␉current->pciDev = pci_dev;␊ |
60 | ␉␉current->next = NULL;␊ |
61 | ␉}␊ |
62 | }␊ |
63 | ␊ |
64 | // Loop through the list and call the apropriate patch function␊ |
65 | int usb_loop(void);␊ |
66 | int usb_loop(void)␊ |
67 | {␊ |
68 | ␉int retVal = 1;␊ |
69 | ␉bool fix_ehci = true, fix_uhci = true, fix_usb = true, fix_legacy = true;␊ |
70 | ␉␊ |
71 | ␉if (getBoolForKey(kUSBBusFix, &fix_usb, DEFAULT_BOOT_CONFIG))␊ |
72 | ␉{␊ |
73 | ␉␉fix_ehci = fix_uhci = fix_legacy = fix_usb;␉// Disable all if none set␊ |
74 | ␉}␊ |
75 | ␉else ␊ |
76 | ␉{␊ |
77 | ␉␉getBoolForKey(kEHCIacquire, &fix_ehci, DEFAULT_BOOT_CONFIG);␊ |
78 | ␉␉getBoolForKey(kUHCIreset, &fix_uhci, DEFAULT_BOOT_CONFIG);␊ |
79 | ␉␉getBoolForKey(kLegacyOff, &fix_legacy, DEFAULT_BOOT_CONFIG);␊ |
80 | ␉}␊ |
81 | ␉␉␉␉␊ |
82 | ␉DBG("\n");␊ |
83 | ␉struct pciList* current = usbList;␊ |
84 | ␉␊ |
85 | ␉while(current)␊ |
86 | ␉{␊ |
87 | ␉␉switch (pci_config_read8(current->pciDev->dev.addr, PCI_CLASS_PROG))␊ |
88 | ␉␉{␊ |
89 | ␉␉␉// EHCI␊ |
90 | ␉␉␉case 0x20:␊ |
91 | ␉␉ ␉if(fix_legacy) retVal &= legacy_off(current->pciDev);␊ |
92 | ␉␉ ␉if(fix_ehci) retVal &= ehci_acquire(current->pciDev);␊ |
93 | ␉␉␉␉␊ |
94 | ␉␉␉␉break;␊ |
95 | ␉␉␉␉␊ |
96 | ␉␉␉// UHCI␊ |
97 | ␉␉␉case 0x00:␊ |
98 | ␉␉␉␉if (fix_uhci) retVal &= uhci_reset(current->pciDev);␊ |
99 | ␊ |
100 | ␉␉␉␉break;␊ |
101 | ␉␉␉default:␊ |
102 | ␉␉␉␉break;␊ |
103 | ␉␉}␊ |
104 | ␉␉␊ |
105 | ␉␉current = current->next;␊ |
106 | ␉}␊ |
107 | ␉return retVal;␊ |
108 | }␊ |
109 | ␊ |
110 | static int legacy_off (pci_dt_t *pci_dev)␊ |
111 | {␊ |
112 | ␉// Set usb legacy off modification by Signal64␊ |
113 | ␉// NOTE: This *must* be called after the last file is loaded from the drive in the event that we are booting form usb.␊ |
114 | ␉// NOTE2: This should be called after any getc() call. (aka, after the Wait=y keyworkd is used)␊ |
115 | ␉// AKA: Make this run immediatly before the kernel is called␊ |
116 | ␉␉␊ |
117 | ␉DBG("Setting Legacy USB Off on controller [%04x:%04x] at %02x:%2x.%x\n", ␊ |
118 | ␉␉␉pci_dev->vendor_id, pci_dev->device_id,␊ |
119 | ␉␉␉pci_dev->dev.bits.bus, pci_dev->dev.bits.dev, pci_dev->dev.bits.func);␊ |
120 | ␉␊ |
121 | ␉␊ |
122 | ␉// capaddr = Capability Registers = dev.addr + offset stored in dev.addr + 0x10 (USBBASE)␊ |
123 | ␉uint32_t capaddr = pci_config_read32(pci_dev->dev.addr, 0x10);␉␊ |
124 | ␉␊ |
125 | ␉// opaddr = Operational Registers = capaddr + offset (8bit CAPLENGTH in Capability Registers + offset 0)␊ |
126 | ␉uint32_t opaddr = capaddr + *((unsigned char*)(capaddr)); ␉␉␊ |
127 | ␉␊ |
128 | ␉// eecp = EHCI Extended Capabilities offset = capaddr HCCPARAMS bits 15:8␊ |
129 | ␉uint8_t eecp=*((unsigned char*)(capaddr + 9));␊ |
130 | ␉␊ |
131 | ␉DBG("capaddr=%x opaddr=%x eecp=%x\n", capaddr, opaddr, eecp);␊ |
132 | ␉␊ |
133 | ␉␊ |
134 | #if DEBUG_USB␊ |
135 | uint32_t usbcmd = *((unsigned int*)(opaddr));␉␉␉// Command Register␊ |
136 | ␉uint32_t usbsts = *((unsigned int*)(opaddr + 4));␉␉// Status Register␊ |
137 | ␉uint32_t usbintr = *((unsigned int*)(opaddr + 8));␉␉// Interrupt Enable Register␊ |
138 | ␉DBG("usbcmd=%08x usbsts=%08x usbintr=%08x\n", usbcmd, usbsts, usbintr);␊ |
139 | ␊ |
140 | // read PCI Config 32bit USBLEGSUP (eecp+0) ␊ |
141 | ␉uint32_t usblegsup = pci_config_read32(pci_dev->dev.addr, eecp);␊ |
142 | ␉␊ |
143 | ␉// informational only␊ |
144 | ␉int isBIOSowned = !!((usblegsup) & (1 << (16)));␊ |
145 | ␉int isOSowned = !!((usblegsup) & (1 << (24)));␊ |
146 | ␉␊ |
147 | ␉// read PCI Config 32bit USBLEGCTLSTS (eecp+4) ␊ |
148 | ␉uint32_t usblegctlsts = pci_config_read32(pci_dev->dev.addr, eecp + 4);␊ |
149 | ␉␊ |
150 | ␉DBG("usblegsup=%08x isOSowned=%d isBIOSowned=%d usblegctlsts=%08x\n", usblegsup, isOSowned, isBIOSowned, usblegctlsts);␊ |
151 | #else␊ |
152 | uint32_t usbcmd;␊ |
153 | #endif␉␊ |
154 | ␉␊ |
155 | ␉␊ |
156 | ␉// Reset registers to Legacy OFF␊ |
157 | ␉DBG("Clearing USBLEGCTLSTS\n");␊ |
158 | ␉pci_config_write32(pci_dev->dev.addr, eecp + 4, 0);␉//usblegctlsts␊ |
159 | ␉␊ |
160 | ␉// if delay value is in milliseconds it doesn't appear to work. ␊ |
161 | ␉// setting value to anything up to 65535 does not add the expected delay here.␊ |
162 | ␉delay(100);␊ |
163 | ␉␊ |
164 | ␉usbcmd = *((unsigned int*)(opaddr));␊ |
165 | #if DEBUG_USB␊ |
166 | ␊ |
167 | ␉usbsts = *((unsigned int*)(opaddr + 4));␊ |
168 | ␉usbintr = *((unsigned int*)(opaddr + 8));␊ |
169 | ␉␊ |
170 | ␉DBG("usbcmd=%08x usbsts=%08x usbintr=%08x\n", usbcmd, usbsts, usbintr);␊ |
171 | #endif␊ |
172 | ␉DBG("Clearing Registers\n");␊ |
173 | ␉␊ |
174 | ␉// clear registers to default␊ |
175 | ␉usbcmd = (usbcmd & 0xffffff00);␊ |
176 | ␉*((unsigned int*)(opaddr)) = usbcmd;␊ |
177 | ␉*((unsigned int*)(opaddr + 8)) = 0;␉␉␉␉␉//usbintr - clear interrupt registers␊ |
178 | ␉*((unsigned int*)(opaddr + 4)) = 0x1000;␉␉␉//usbsts - clear status registers ␉␊ |
179 | ␉pci_config_write32(pci_dev->dev.addr, eecp, 1);␉␉//usblegsup␊ |
180 | ␉␊ |
181 | ␉␊ |
182 | #if DEBUG_USB␊ |
183 | // get the results␊ |
184 | ␉usbcmd = *((unsigned int*)(opaddr));␊ |
185 | ␉usbsts = *((unsigned int*)(opaddr + 4));␊ |
186 | ␉usbintr = *((unsigned int*)(opaddr + 8));␊ |
187 | ␉␊ |
188 | ␉DBG("usbcmd=%08x usbsts=%08x usbintr=%08x\n", usbcmd, usbsts, usbintr);␊ |
189 | ␊ |
190 | // read 32bit USBLEGSUP (eecp+0) ␊ |
191 | ␉usblegsup = pci_config_read32(pci_dev->dev.addr, eecp);␊ |
192 | ␉␊ |
193 | ␉// informational only␊ |
194 | ␉isBIOSowned = !!((usblegsup) & (1 << (16)));␊ |
195 | ␉isOSowned = !!((usblegsup) & (1 << (24)));␊ |
196 | ␉␊ |
197 | ␉// read 32bit USBLEGCTLSTS (eecp+4) ␊ |
198 | ␉usblegctlsts = pci_config_read32(pci_dev->dev.addr, eecp + 4);␊ |
199 | ␉␊ |
200 | ␉DBG("usblegsup=%08x isOSowned=%d isBIOSowned=%d usblegctlsts=%08x\n", usblegsup, isOSowned, isBIOSowned, usblegctlsts);␊ |
201 | #endif␉␊ |
202 | ␉␊ |
203 | ␉DBG("Legacy USB Off Done\n");␉␊ |
204 | ␉return 1;␊ |
205 | }␊ |
206 | ␊ |
207 | static int ehci_acquire (pci_dt_t *pci_dev)␊ |
208 | {␊ |
209 | ␉int␉␉j, k;␉␊ |
210 | ␉uint8_t␉␉legacy[8];␊ |
211 | ␉bool␉␉alwaysHardBIOSReset = false;␊ |
212 | ␉␉␊ |
213 | ␉if (!getBoolForKey(kEHCIhard, &alwaysHardBIOSReset, DEFAULT_BOOT_CONFIG)) {␊ |
214 | ␉␉alwaysHardBIOSReset = true;␊ |
215 | ␉}␊ |
216 | ␊ |
217 | ␉pci_config_write16(pci_dev->dev.addr, 0x04, 0x0002);␊ |
218 | ␉uint32_t base = pci_config_read32(pci_dev->dev.addr, 0x10);␊ |
219 | ␊ |
220 | ␉DBG("EHCI controller [%04x:%04x] at %02x:%2x.%x DMA @%x\n", ␊ |
221 | ␉␉pci_dev->vendor_id, pci_dev->device_id,␊ |
222 | ␉␉pci_dev->dev.bits.bus, pci_dev->dev.bits.dev, pci_dev->dev.bits.func, ␊ |
223 | ␉␉base);␊ |
224 | ␊ |
225 | ␉if (*((unsigned char*)base) < 0xc)␊ |
226 | ␉{␊ |
227 | ␉␉DBG("Config space too small: no legacy implementation\n");␊ |
228 | ␉␉return 1;␊ |
229 | ␉}␊ |
230 | ␉uint8_t eecp = *((unsigned char*)(base + 9));␊ |
231 | ␉if (!eecp) {␊ |
232 | ␉␉DBG("No extended capabilities: no legacy implementation\n");␊ |
233 | ␉␉return 1;␊ |
234 | ␉}␊ |
235 | ␊ |
236 | ␉DBG("eecp=%x\n",eecp);␊ |
237 | ␊ |
238 | ␉// bad way to do it␊ |
239 | ␉// pci_conf_write(pci_dev->dev.addr, eecp, 4, 0x01000001);␊ |
240 | ␉for (j = 0; j < 8; j++) {␊ |
241 | ␉␉legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);␊ |
242 | ␉␉DBG("%02x ", legacy[j]);␊ |
243 | ␉}␊ |
244 | ␉DBG("\n");␊ |
245 | ␊ |
246 | ␉//Real Job: based on orByte's AppleUSBEHCI.cpp␊ |
247 | ␉//We try soft reset first - some systems hang on reboot with hard reset␊ |
248 | ␉// Definitely needed during reboot on 10.4.6␊ |
249 | ␊ |
250 | ␉bool isOwnershipConflict = (((legacy[3] & 1) != 0) && ((legacy[2] & 1) != 0));␊ |
251 | ␉if (!alwaysHardBIOSReset && isOwnershipConflict) {␊ |
252 | ␉␉DBG("EHCI - Ownership conflict - attempting soft reset ...\n");␊ |
253 | ␉␉DBG("EHCI - toggle OS Ownership to 0\n");␊ |
254 | ␉␉pci_config_write8(pci_dev->dev.addr, eecp + 3, 0);␊ |
255 | ␉␉for (k = 0; k < 25; k++) {␊ |
256 | ␉␉␉for (j = 0; j < 8; j++) {␊ |
257 | ␉␉␉␉legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);␊ |
258 | ␉␉␉}␊ |
259 | ␉␉␉if (legacy[3] == 0) {␊ |
260 | ␉␉␉␉break;␊ |
261 | ␉␉␉}␊ |
262 | ␉␉␉delay(10);␊ |
263 | ␉␉}␊ |
264 | ␉}␉␊ |
265 | ␊ |
266 | ␉DBG("Found USBLEGSUP_ID - value %x:%x - writing OSOwned\n", legacy[3],legacy[2]);␊ |
267 | ␉pci_config_write8(pci_dev->dev.addr, eecp + 3, 1);␊ |
268 | ␊ |
269 | ␉// wait for kEHCI_USBLEGSUP_BIOSOwned bit to clear␊ |
270 | ␉for (k = 0; k < 25; k++) {␊ |
271 | ␉␉for (j = 0;j < 8; j++) {␊ |
272 | ␉␉␉legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);␊ |
273 | ␉␉}␊ |
274 | ␉␉DBG ("%x:%x,",legacy[3],legacy[2]);␊ |
275 | ␉␉if (legacy[2] == 0) {␊ |
276 | ␉␉␉break;␊ |
277 | ␉␉}␊ |
278 | ␉␉delay(10);␊ |
279 | ␉}␊ |
280 | ␊ |
281 | ␉for (j = 0;j < 8; j++) {␊ |
282 | ␉␉legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);␊ |
283 | ␉}␊ |
284 | ␉isOwnershipConflict = ((legacy[2]) != 0);␊ |
285 | ␉if (isOwnershipConflict) {␊ |
286 | ␉␉// Soft reset has failed. Assume SMI being ignored␊ |
287 | ␉␉// Hard reset␊ |
288 | ␉␉// Force Clear BIOS BIT␊ |
289 | ␉␉DBG("EHCI - Ownership conflict - attempting hard reset ...\n");␉␉␉␊ |
290 | ␉␉DBG ("%x:%x\n",legacy[3],legacy[2]);␊ |
291 | ␉␉DBG("EHCI - Force BIOS Ownership to 0\n");␊ |
292 | ␊ |
293 | ␉␉pci_config_write8(pci_dev->dev.addr, eecp + 2, 0);␊ |
294 | ␉␉for (k = 0; k < 25; k++) {␊ |
295 | ␉␉␉for (j = 0; j < 8; j++) {␊ |
296 | ␉␉␉␉legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);␊ |
297 | ␉␉␉}␊ |
298 | ␉␉␉DBG ("%x:%x,",legacy[3],legacy[2]);␊ |
299 | ␊ |
300 | ␉␉␉if ((legacy[2]) == 0) {␊ |
301 | ␉␉␉␉break;␊ |
302 | ␉␉␉}␊ |
303 | ␉␉␉delay(10);␉␊ |
304 | ␉␉}␉␉␊ |
305 | ␉␉// Disable further SMI events␊ |
306 | ␉␉for (j = 4; j < 8; j++) {␊ |
307 | ␉␉␉pci_config_write8(pci_dev->dev.addr, eecp + j, 0);␊ |
308 | ␉␉}␊ |
309 | ␉}␊ |
310 | ␊ |
311 | ␉for (j = 0; j < 8; j++) {␊ |
312 | ␉␉legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);␊ |
313 | ␉}␊ |
314 | ␊ |
315 | ␉DBG ("%x:%x\n",legacy[3],legacy[2]);␊ |
316 | ␊ |
317 | ␉// Final Ownership Resolution Check...␊ |
318 | ␉if (legacy[2] & 1) {␉␉␉␉␉␊ |
319 | ␉␉DBG("EHCI controller unable to take control from BIOS\n");␊ |
320 | ␉␉return 0;␊ |
321 | ␉}␊ |
322 | ␊ |
323 | ␉DBG("EHCI Acquire OS Ownership done\n");␉␊ |
324 | ␉return 1;␊ |
325 | }␊ |
326 | ␊ |
327 | static int uhci_reset (pci_dt_t *pci_dev)␊ |
328 | {␊ |
329 | ␉uint32_t base, port_base;␊ |
330 | ␉␊ |
331 | ␉base = pci_config_read32(pci_dev->dev.addr, 0x20);␊ |
332 | ␉port_base = (base >> 5) & 0x07ff;␊ |
333 | ␊ |
334 | ␉DBG("UHCI controller [%04x:%04x] at %02x:%2x.%x base %x(%x)\n", ␊ |
335 | ␉␉pci_dev->vendor_id, pci_dev->device_id,␊ |
336 | ␉␉pci_dev->dev.bits.bus, pci_dev->dev.bits.dev, pci_dev->dev.bits.func, ␊ |
337 | ␉␉port_base, base);␊ |
338 | ␉␊ |
339 | ␉pci_config_write16(pci_dev->dev.addr, 0xc0, 0x8f00);␊ |
340 | ␊ |
341 | ␉outw (port_base, 0x0002);␊ |
342 | ␉delay(10);␊ |
343 | ␉outw (port_base+4,0);␊ |
344 | ␉delay(10);␊ |
345 | ␉outw (port_base,0);␊ |
346 | ␉return 1;␊ |
347 | }␊ |
348 | |