Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1119