Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 847