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
25int ehci_acquire (pci_dt_t *pci_dev)
26{
27intj, k;
28uint32_tbase;
29uint8_teecp;
30uint8_tlegacy[8];
31boolisOwnershipConflict;
32boolalwaysHardBIOSReset;
33
34alwaysHardBIOSReset = false;
35if (!getBoolForKey(kEHCIhard, &alwaysHardBIOSReset, &bootInfo->bootConfig)) {
36alwaysHardBIOSReset = true;
37}
38
39pci_config_write16(pci_dev->dev.addr, 0x04, 0x0002);
40base = pci_config_read32(pci_dev->dev.addr, 0x10);
41
42verbose("EHCI controller [%04x:%04x] at %02x:%2x.%x DMA @%x\n",
43pci_dev->vendor_id, pci_dev->device_id,
44pci_dev->dev.bits.bus, pci_dev->dev.bits.dev, pci_dev->dev.bits.func,
45base);
46
47if (*((unsigned char*)base) < 0xc)
48{
49DBG("Config space too small: no legacy implementation\n");
50return 1;
51}
52eecp = *((unsigned char*)(base + 9));
53if (!eecp) {
54DBG("No extended capabilities: no legacy implementation\n");
55return 1;
56}
57
58DBG("eecp=%x\n",eecp);
59
60// bad way to do it
61// pci_conf_write(pci_dev->dev.addr, eecp, 4, 0x01000001);
62for (j = 0; j < 8; j++) {
63legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);
64DBG("%02x ", legacy[j]);
65}
66DBG("\n");
67
68//Real Job: based on orByte's AppleUSBEHCI.cpp
69//We try soft reset first - some systems hang on reboot with hard reset
70// Definitely needed during reboot on 10.4.6
71
72isOwnershipConflict = ((legacy[3] & 1 != 0) && (legacy[2] & 1 != 0));
73if (!alwaysHardBIOSReset && isOwnershipConflict) {
74DBG("EHCI - Ownership conflict - attempting soft reset ...\n");
75DBG("EHCI - toggle OS Ownership to 0\n");
76pci_config_write8(pci_dev->dev.addr, eecp + 3, 0);
77for (k = 0; k < 25; k++) {
78for (j = 0; j < 8; j++) {
79legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);
80}
81if (legacy[3] == 0) {
82break;
83}
84delay(10);
85}
86}
87
88DBG("Found USBLEGSUP_ID - value %x:%x - writing OSOwned\n", legacy[3],legacy[2]);
89pci_config_write8(pci_dev->dev.addr, eecp + 3, 1);
90
91// wait for kEHCI_USBLEGSUP_BIOSOwned bit to clear
92for (k = 0; k < 25; k++) {
93for (j = 0;j < 8; j++) {
94legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);
95}
96DBG ("%x:%x,",legacy[3],legacy[2]);
97if (legacy[2] == 0) {
98break;
99}
100delay(10);
101}
102
103for (j = 0;j < 8; j++) {
104legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);
105}
106isOwnershipConflict = ((legacy[2]) != 0);
107if (isOwnershipConflict) {
108// Soft reset has failed. Assume SMI being ignored
109// Hard reset
110// Force Clear BIOS BIT
111DBG("EHCI - Ownership conflict - attempting hard reset ...\n");
112DBG ("%x:%x\n",legacy[3],legacy[2]);
113DBG("EHCI - Force BIOS Ownership to 0\n");
114
115pci_config_write8(pci_dev->dev.addr, eecp + 2, 0);
116for (k = 0; k < 25; k++) {
117for (j = 0; j < 8; j++) {
118legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);
119}
120DBG ("%x:%x,",legacy[3],legacy[2]);
121
122if ((legacy[2]) == 0) {
123break;
124}
125delay(10);
126}
127// Disable further SMI events
128for (j = 4; j < 8; j++) {
129pci_config_write8(pci_dev->dev.addr, eecp + j, 0);
130}
131}
132
133for (j = 0; j < 8; j++) {
134legacy[j] = pci_config_read8(pci_dev->dev.addr, eecp + j);
135}
136
137DBG ("%x:%x\n",legacy[3],legacy[2]);
138
139// Final Ownership Resolution Check...
140if (legacy[2] & 1) {
141DBG("EHCI controller unable to take control from BIOS\n");
142return 0;
143}
144
145DBG("EHCI Acquire OS Ownership done\n");
146return 1;
147}
148
149int uhci_reset (pci_dt_t *pci_dev)
150{
151uint32_t base, port_base;
152
153base = pci_config_read32(pci_dev->dev.addr, 0x20);
154port_base = (base >> 5) & 0x07ff;
155
156verbose("UHCI controller [%04x:%04x] at %02x:%2x.%x base %x(%x)\n",
157pci_dev->vendor_id, pci_dev->device_id,
158pci_dev->dev.bits.bus, pci_dev->dev.bits.dev, pci_dev->dev.bits.func,
159port_base, base);
160
161pci_config_write16(pci_dev->dev.addr, 0xc0, 0x8f00);
162
163outw (port_base, 0x0002);
164delay(10);
165outw (port_base+4,0);
166delay(10);
167outw (port_base,0);
168return 1;
169}
170

Archive Download this file

Revision: 136