Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/usb.c

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 "boot.h"
12#include "bootstruct.h"
13#include "pci.h"
14
15#ifndef DEBUG_USB
16#define DEBUG_USB 0
17#endif
18
19#if DEBUG_USB
20#define DBG(x...)printf(x)
21#else
22#define DBG(x...)
23#endif
24
25
26struct pciList
27{
28pci_dt_t* pciDev;
29struct pciList* next;
30};
31
32struct pciList* usbList = NULL;
33
34static int legacy_off (pci_dt_t *pci_dev);
35static int ehci_acquire (pci_dt_t *pci_dev);
36static int uhci_reset (pci_dt_t *pci_dev);
37static int xhci_legacy_off(pci_dt_t *pci_dev);
38
39// Add usb device to the list
40void notify_usb_dev(pci_dt_t *pci_dev)
41{
42struct pciList* current = usbList;
43if(!usbList)
44{
45usbList = (struct pciList*)malloc(sizeof(struct pciList));
46usbList->next = NULL;
47usbList->pciDev = pci_dev;
48
49}
50else
51{
52while(current != NULL && current->next != NULL)
53{
54current = current->next;
55}
56current->next = (struct pciList*)malloc(sizeof(struct pciList));
57current = current->next;
58
59current->pciDev = pci_dev;
60current->next = NULL;
61}
62}
63
64// Loop through the list and call the apropriate patch function
65int usb_loop()
66{
67int retVal = 1;
68bool fix_xhci, fix_ehci, fix_uhci, fix_usb, fix_legacy;
69fix_xhci = fix_ehci = fix_uhci = fix_usb = fix_legacy = false;
70
71if (getBoolForKey(kUSBBusFix, &fix_usb, &bootInfo->chameleonConfig))
72{
73fix_xhci = fix_ehci = fix_uhci = fix_legacy = fix_usb;// Disable all if none set
74}
75else
76{
77getBoolForKey(kXHCILegacyOff, &fix_xhci, &bootInfo->chameleonConfig);
78getBoolForKey(kEHCIacquire, &fix_ehci, &bootInfo->chameleonConfig);
79getBoolForKey(kUHCIreset, &fix_uhci, &bootInfo->chameleonConfig);
80getBoolForKey(kLegacyOff, &fix_legacy, &bootInfo->chameleonConfig);
81}
82
83struct pciList* current = usbList;
84
85while(current)
86{
87switch (pci_config_read8(current->pciDev->dev.addr, PCI_CLASS_PROG))
88{
89// XHCI
90case PCI_IF_XHCI:
91if(fix_xhci || fix_legacy) retVal &= xhci_legacy_off(current->pciDev);
92break;
93
94// EHCI
95case PCI_IF_EHCI:
96 if(fix_ehci) retVal &= ehci_acquire(current->pciDev);
97 if(fix_legacy) retVal &= legacy_off(current->pciDev);
98
99break;
100
101// UHCI
102case PCI_IF_UHCI:
103if (fix_uhci) retVal &= uhci_reset(current->pciDev);
104
105break;
106}
107
108current = current->next;
109}
110return retVal;
111}
112
113static int legacy_off (pci_dt_t *pci_dev)
114{
115// Set usb legacy off modification by Signal64
116// NOTE: This *must* be called after the last file is loaded from the drive in the event that we are booting form usb.
117// NOTE2: This should be called after any getc()/getchar() call. (aka, after the Wait=y keyworkd is used)
118// AKA: Make this run immediatly before the kernel is called
119uint32_tcapaddr, opaddr;
120uint8_teecp;
121uint32_tusbcmd, usbsts, usbintr;
122uint32_tusblegsup, usblegctlsts;
123
124int isOSowned;
125int isBIOSowned;
126
127verbose("Setting Legacy USB Off on controller [%04x:%04x] at %02x:%2x.%x\n",
128pci_dev->vendor_id, pci_dev->device_id,
129pci_dev->dev.bits.bus, pci_dev->dev.bits.dev, pci_dev->dev.bits.func);
130
131
132// capaddr = Capability Registers = dev.addr + offset stored in dev.addr + 0x10 (USBBASE)
133capaddr = pci_config_read32(pci_dev->dev.addr, 0x10);
134
135// opaddr = Operational Registers = capaddr + offset (8bit CAPLENGTH in Capability Registers + offset 0)
136opaddr = capaddr + *((unsigned char*)(capaddr));
137
138// eecp = EHCI Extended Capabilities offset = capaddr HCCPARAMS bits 15:8
139eecp=*((unsigned char*)(capaddr + 9));
140
141DBG("capaddr=%x opaddr=%x eecp=%x\n", capaddr, opaddr, eecp);
142
143usbcmd = *((unsigned int*)(opaddr));// Command Register
144usbsts = *((unsigned int*)(opaddr + 4));// Status Register
145usbintr = *((unsigned int*)(opaddr + 8));// Interrupt Enable Register
146
147DBG("usbcmd=%08x usbsts=%08x usbintr=%08x\n", usbcmd, usbsts, usbintr);
148
149// read PCI Config 32bit USBLEGSUP (eecp+0)
150usblegsup = pci_config_read32(pci_dev->dev.addr, eecp);
151
152// informational only
153isBIOSowned = !!((usblegsup) & (1 << (16)));
154isOSowned = !!((usblegsup) & (1 << (24)));
155
156// read PCI Config 32bit USBLEGCTLSTS (eecp+4)
157usblegctlsts = pci_config_read32(pci_dev->dev.addr, eecp + 4);
158
159DBG("usblegsup=%08x isOSowned=%d isBIOSowned=%d usblegctlsts=%08x\n", usblegsup, isOSowned, isBIOSowned, usblegctlsts);
160
161// Reset registers to Legacy OFF
162DBG("Clearing USBLEGCTLSTS\n");
163pci_config_write32(pci_dev->dev.addr, eecp + 4, 0);//usblegctlsts
164
165delay(100000);
166
167usbcmd = *((unsigned int*)(opaddr));
168usbsts = *((unsigned int*)(opaddr + 4));
169usbintr = *((unsigned int*)(opaddr + 8));
170
171DBG("usbcmd=%08x usbsts=%08x usbintr=%08x\n", usbcmd, usbsts, usbintr);
172
173DBG("Clearing Registers\n");
174
175// clear registers to default
176usbcmd = (usbcmd & 0xffffff00);
177*((unsigned int*)(opaddr)) = usbcmd;
178*((unsigned int*)(opaddr + 8)) = 0;//usbintr - clear interrupt registers
179*((unsigned int*)(opaddr + 4)) = 0x1000;//usbsts - clear status registers
180pci_config_write32(pci_dev->dev.addr, eecp, 1);//usblegsup
181
182// get the results
183usbcmd = *((unsigned int*)(opaddr));
184usbsts = *((unsigned int*)(opaddr + 4));
185usbintr = *((unsigned int*)(opaddr + 8));
186
187DBG("usbcmd=%08x usbsts=%08x usbintr=%08x\n", usbcmd, usbsts, usbintr);
188
189// read 32bit USBLEGSUP (eecp+0)
190usblegsup = pci_config_read32(pci_dev->dev.addr, eecp);
191
192// informational only
193isBIOSowned = !!((usblegsup) & (1 << (16)));
194isOSowned = !!((usblegsup) & (1 << (24)));
195
196// read 32bit USBLEGCTLSTS (eecp+4)
197usblegctlsts = pci_config_read32(pci_dev->dev.addr, eecp + 4);
198
199DBG("usblegsup=%08x isOSowned=%d isBIOSowned=%d usblegctlsts=%08x\n", usblegsup, isOSowned, isBIOSowned, usblegctlsts);
200
201verbose("Legacy USB Off Done\n");
202return 1;
203}
204
205static int ehci_acquire (pci_dt_t *pci_dev)
206{
207intj, k;
208uint32_tbase;
209uint8_teecp;
210uint8_tlegacy[8];
211boolisOwnershipConflict;
212boolalwaysHardBIOSReset;
213
214alwaysHardBIOSReset = false;
215if (!getBoolForKey(kEHCIhard, &alwaysHardBIOSReset, &bootInfo->chameleonConfig)) {
216alwaysHardBIOSReset = true;
217}
218
219pci_config_write16(pci_dev->dev.addr, 0x04, 0x0002);
220base = pci_config_read32(pci_dev->dev.addr, 0x10);
221
222verbose("EHCI controller [%04x:%04x] at %02x:%2x.%x DMA @%x\n",
223pci_dev->vendor_id, pci_dev->device_id,
224pci_dev->dev.bits.bus, pci_dev->dev.bits.dev, pci_dev->dev.bits.func,
225base);
226
227if (*((unsigned char*)base) < 0xc)
228{
229DBG("Config space too small: no legacy implementation\n");
230return 1;
231}
232eecp = *((unsigned char*)(base + 9));
233if (!eecp) {
234DBG("No extended capabilities: no legacy implementation\n");
235return 1;
236}
237
238DBG("eecp=%x\n",eecp);
239
240// bad way to do it
241// pci_conf_write(pci_dev->dev.addr, eecp, 4, 0x01000001);
242for (j = 0; j < 8; j++) {
243legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);
244DBG("%02x ", legacy[j]);
245}
246DBG("\n");
247
248//Real Job: based on orByte's AppleUSBEHCI.cpp
249//We try soft reset first - some systems hang on reboot with hard reset
250// Definitely needed during reboot on 10.4.6
251
252isOwnershipConflict = (((legacy[3] & 1) != 0) && ((legacy[2] & 1) != 0));
253if (!alwaysHardBIOSReset && isOwnershipConflict) {
254DBG("EHCI - Ownership conflict - attempting soft reset ...\n");
255DBG("EHCI - toggle OS Ownership to 0\n");
256pci_config_write8(pci_dev->dev.addr, eecp + 3, 0);
257for (k = 0; k < 25; k++) {
258for (j = 0; j < 8; j++) {
259legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);
260}
261if (legacy[3] == 0) {
262break;
263}
264delay(10);
265}
266}
267
268DBG("Found USBLEGSUP_ID - value %x:%x - writing OSOwned\n", legacy[3],legacy[2]);
269pci_config_write8(pci_dev->dev.addr, eecp + 3, 1);
270
271// wait for kEHCI_USBLEGSUP_BIOSOwned bit to clear
272for (k = 0; k < 25; k++) {
273for (j = 0;j < 8; j++) {
274legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);
275}
276DBG ("%x:%x,",legacy[3],legacy[2]);
277if (legacy[2] == 0) {
278break;
279}
280delay(10);
281}
282
283for (j = 0;j < 8; j++) {
284legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);
285}
286isOwnershipConflict = ((legacy[2]) != 0);
287if (isOwnershipConflict) {
288// Soft reset has failed. Assume SMI being ignored
289// Hard reset
290// Force Clear BIOS BIT
291DBG("EHCI - Ownership conflict - attempting hard reset ...\n");
292DBG ("%x:%x\n",legacy[3],legacy[2]);
293DBG("EHCI - Force BIOS Ownership to 0\n");
294
295pci_config_write8(pci_dev->dev.addr, eecp + 2, 0);
296for (k = 0; k < 25; k++) {
297for (j = 0; j < 8; j++) {
298legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);
299}
300DBG ("%x:%x,",legacy[3],legacy[2]);
301
302if ((legacy[2]) == 0) {
303break;
304}
305delay(10);
306}
307// Disable further SMI events
308for (j = 4; j < 8; j++) {
309pci_config_write8(pci_dev->dev.addr, eecp + j, 0);
310}
311}
312
313for (j = 0; j < 8; j++) {
314legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);
315}
316
317DBG ("%x:%x\n",legacy[3],legacy[2]);
318
319// Final Ownership Resolution Check...
320if (legacy[2] & 1) {
321DBG("EHCI controller unable to take control from BIOS\n");
322return 0;
323}
324
325DBG("EHCI Acquire OS Ownership done\n");
326return 1;
327}
328
329static int uhci_reset (pci_dt_t *pci_dev)
330{
331uint32_t base, port_base;
332
333base = pci_config_read32(pci_dev->dev.addr, 0x20);
334port_base = (base >> 5) & 0x07ff;
335
336verbose("UHCI controller [%04x:%04x] at %02x:%2x.%x base %x(%x)\n",
337pci_dev->vendor_id, pci_dev->device_id,
338pci_dev->dev.bits.bus, pci_dev->dev.bits.dev, pci_dev->dev.bits.func,
339port_base, base);
340
341pci_config_write16(pci_dev->dev.addr, 0xc0, 0x8f00);
342
343outw (port_base, 0x0002);
344delay(10);
345outw (port_base+4,0);
346delay(10);
347outw (port_base,0);
348return 1;
349}
350
351static int xhci_legacy_off(pci_dt_t *pci_dev)
352{
353uint32_t bar0, hccparams1, extendCap, value;
354int32_t timeOut;
355
356verbose("Setting Legacy USB Off on xHC [%04x:%04x] at %02x:%2x.%x\n",
357pci_dev->vendor_id, pci_dev->device_id,
358pci_dev->dev.bits.bus, pci_dev->dev.bits.dev, pci_dev->dev.bits.func);
359
360bar0 = pci_config_read32(pci_dev->dev.addr, 16);
361/*
362 * Check if memory bar
363 */
364if (bar0 & 1)
365{
366DBG("%s: BAR0 not a memory range\n", __FUNCTION__);
367return 0;
368}
369/*
370 * Check if outside 32-bit physical address space
371 */
372if (((bar0 & 6) == 4) &&
373pci_config_read32(pci_dev->dev.addr, 20))
374{
375DBG("%s: BAR0 outside 32-bit physical address space\n", __FUNCTION__);
376return 0;
377}
378bar0 &= ~15;
379
380hccparams1 = *(uint32_t const volatile*) (bar0 + 16);
381if (hccparams1 == ~0)
382{
383DBG("%s: hccparams1 invalid 0x%x\n", __FUNCTION__, hccparams1);
384return 0;
385}
386extendCap = (hccparams1 >> 14) & 0x3fffc;
387while (extendCap) {
388value = *(uint32_t const volatile*) (bar0 + extendCap);
389if (value == ~0)
390{
391break;
392}
393if ((value & 0xff) == 1) {
394#if DEBUG_USB
395verbose("%s: Found USBLEGSUP 0x%x, USBLEGCTLSTS 0x%x\n", __FUNCTION__,
396value, *(uint32_t const volatile*) (bar0 + extendCap + 4));
397#endif
398value |= (1 << 24);
399*(uint32_t volatile*) (bar0 + extendCap) = value;
400timeOut = 40;
401while (timeOut--)
402{
403delay(500);
404value = *(uint32_t const volatile*) (bar0 + extendCap);
405if (value == ~0)
406{
407timeOut = -1;
408break;
409}
410if ((value & 0x01010000) == 0x01000000)
411{
412timeOut = -1;/* Optional - always disable the SMI */
413break;
414}
415}
416#if DEBUG_USB
417verbose("%s: USBLEGSUP 0x%x, USBLEGCTLSTS 0x%x\n", __FUNCTION__,
418value, *(uint32_t const volatile*) (bar0 + extendCap + 4));
419#endif
420if (timeOut >= 0)
421{
422break;
423}
424/*
425 * Disable the SMI in USBLEGCTLSTS if BIOS doesn't respond
426 */
427value = *(uint32_t const volatile*) (bar0 + extendCap + 4);
428if (value == ~0)
429{
430break;
431}
432value &= 0x1f1fee;
433value |= 0xe0000000;
434*(uint32_t volatile*) (bar0 + extendCap + 4) = value;
435break;
436}
437if (!(value & 0xff00))
438break;
439extendCap += ((value >> 6) & 0x3fc);
440}
441verbose("XHCI Legacy Off Done\n");
442return 1;
443}
444

Archive Download this file

Revision: 2571