| 1 |
|
| 2 |
|
| 3 |
#include <cpu/intel/haswell/haswell.h> |
| 4 |
#include <delay.h> |
| 5 |
#include <device/device.h> |
| 6 |
#include <device/pci.h> |
| 7 |
#include <device/pci_ids.h> |
| 8 |
#include <acpi/acpi.h> |
| 9 |
#include <device/mmio.h> |
| 10 |
#include <device/pci_ops.h> |
| 11 |
#include <soc/xhci.h> |
| 12 |
|
| 13 |
#ifdef __SIMPLE_DEVICE__ |
| 14 |
static u8 *usb_xhci_mem_base(pci_devfn_t dev) |
| 15 |
{ |
| 16 |
u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0); |
| 17 |
|
| 18 |
|
| 19 |
if (mem_base == 0 || mem_base == 0xffffffff) |
| 20 |
return 0; |
| 21 |
|
| 22 |
return (u8 *)(mem_base & ~0xf); |
| 23 |
} |
| 24 |
|
| 25 |
static int usb_xhci_port_count_usb3(pci_devfn_t dev) |
| 26 |
{ |
| 27 |
|
| 28 |
return 4; |
| 29 |
} |
| 30 |
|
| 31 |
static void usb_xhci_reset_status_usb3(u8 *mem_base, int port) |
| 32 |
{ |
| 33 |
u8 *portsc = mem_base + XHCI_USB3_PORTSC(port); |
| 34 |
u32 status = read32(portsc); |
| 35 |
|
| 36 |
status &= ~XHCI_USB3_PORTSC_PED; |
| 37 |
|
| 38 |
status |= XHCI_USB3_PORTSC_CHST; |
| 39 |
write32(portsc, status); |
| 40 |
} |
| 41 |
|
| 42 |
static void usb_xhci_reset_port_usb3(u8 *mem_base, int port) |
| 43 |
{ |
| 44 |
u8 *portsc = mem_base + XHCI_USB3_PORTSC(port); |
| 45 |
write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR); |
| 46 |
} |
| 47 |
|
| 48 |
#define XHCI_RESET_DELAY_US 1000 |
| 49 |
#define XHCI_RESET_TIMEOUT 100 |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static void usb_xhci_reset_usb3(pci_devfn_t dev, int all) |
| 59 |
{ |
| 60 |
u32 status, port_disabled; |
| 61 |
int timeout, port; |
| 62 |
int port_count = usb_xhci_port_count_usb3(dev); |
| 63 |
u8 *mem_base = usb_xhci_mem_base(dev); |
| 64 |
|
| 65 |
if (!mem_base || !port_count) |
| 66 |
return; |
| 67 |
|
| 68 |
|
| 69 |
port_disabled = pci_read_config32(dev, XHCI_USB3PDO); |
| 70 |
|
| 71 |
|
| 72 |
for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) { |
| 73 |
int complete = 1; |
| 74 |
for (port = 0; port < port_count; port++) { |
| 75 |
|
| 76 |
if (port_disabled & (1 << port)) |
| 77 |
continue; |
| 78 |
|
| 79 |
status = read32(mem_base + XHCI_USB3_PORTSC(port)); |
| 80 |
status &= XHCI_USB3_PORTSC_PLS; |
| 81 |
if (status == XHCI_PLSR_POLLING) |
| 82 |
complete = 0; |
| 83 |
} |
| 84 |
|
| 85 |
if (complete) |
| 86 |
break; |
| 87 |
udelay(XHCI_RESET_DELAY_US); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
for (port = 0; port < port_count; port++) { |
| 92 |
u8 *portsc = mem_base + XHCI_USB3_PORTSC(port); |
| 93 |
|
| 94 |
if (port_disabled & (1 << port)) |
| 95 |
continue; |
| 96 |
status = read32(portsc) & XHCI_USB3_PORTSC_PLS; |
| 97 |
|
| 98 |
if (all || (status == XHCI_PLSR_RXDETECT || |
| 99 |
status == XHCI_PLSR_POLLING)) |
| 100 |
usb_xhci_reset_port_usb3(mem_base, port); |
| 101 |
else |
| 102 |
port_disabled |= 1 << port; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) { |
| 107 |
int complete = 1; |
| 108 |
for (port = 0; port < port_count; port++) { |
| 109 |
|
| 110 |
if (port_disabled & (1 << port)) |
| 111 |
continue; |
| 112 |
|
| 113 |
status = read32(mem_base + XHCI_USB3_PORTSC(port)); |
| 114 |
if (!(status & XHCI_USB3_PORTSC_WRC)) |
| 115 |
complete = 0; |
| 116 |
} |
| 117 |
|
| 118 |
if (complete) |
| 119 |
break; |
| 120 |
udelay(XHCI_RESET_DELAY_US); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
for (port = 0; port < port_count; port++) |
| 125 |
usb_xhci_reset_status_usb3(mem_base, port); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ) |
| 130 |
{ |
| 131 |
u32 reg32; |
| 132 |
u8 *mem_base = usb_xhci_mem_base(dev); |
| 133 |
u8 is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT); |
| 134 |
|
| 135 |
if (!mem_base || slp_typ < ACPI_S3) |
| 136 |
return; |
| 137 |
|
| 138 |
|
| 139 |
pci_update_config16(dev, XHCI_PWR_CTL_STS, ~XHCI_PWR_CTL_SET_MASK, XHCI_PWR_CTL_SET_D0); |
| 140 |
|
| 141 |
if (!is_broadwell) { |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
pci_and_config32(dev, 0xb0, ~((1 << 14) | (1 << 13))); |
| 146 |
|
| 147 |
|
| 148 |
reg32 = read32(mem_base + 0x816c); |
| 149 |
reg32 &= ~((1 << 14) | (1 << 2)); |
| 150 |
write32(mem_base + 0x816c, reg32); |
| 151 |
|
| 152 |
|
| 153 |
usb_xhci_reset_usb3(dev, 0); |
| 154 |
|
| 155 |
|
| 156 |
reg32 = read32(mem_base + 0x80e0); |
| 157 |
reg32 |= (1 << 15); |
| 158 |
write32(mem_base + 0x80e0, reg32); |
| 159 |
} else { |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
int port; |
| 166 |
for (port = 0; port < usb_xhci_port_count_usb3(dev); port++) |
| 167 |
usb_xhci_reset_status_usb3(mem_base, port); |
| 168 |
} |
| 169 |
|
| 170 |
reg32 = read32(mem_base + 0x8154); |
| 171 |
reg32 &= ~(1 << 31); |
| 172 |
write32(mem_base + 0x8154, reg32); |
| 173 |
|
| 174 |
|
| 175 |
pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_SET_D3); |
| 176 |
pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_STATUS_PME); |
| 177 |
pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_ENABLE_PME); |
| 178 |
} |
| 179 |
#else |
| 180 |
|
| 181 |
static void xhci_init(struct device *dev) |
| 182 |
{ |
| 183 |
struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 184 |
u16 reg16; |
| 185 |
u32 reg32; |
| 186 |
|
| 187 |
|
| 188 |
reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS); |
| 189 |
reg16 &= ~XHCI_PWR_CTL_SET_MASK; |
| 190 |
reg16 |= XHCI_PWR_CTL_SET_D0; |
| 191 |
pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16); |
| 192 |
|
| 193 |
|
| 194 |
reg32 = read32(res2mmio(res, 0x80ec, 0)); |
| 195 |
reg32 |= (1 << 0); |
| 196 |
write32(res2mmio(res, 0x80ec, 0), reg32); |
| 197 |
} |
| 198 |
|
| 199 |
static struct device_operations usb_xhci_ops = { |
| 200 |
.read_resources = pci_dev_read_resources, |
| 201 |
.set_resources = pci_dev_set_resources, |
| 202 |
.enable_resources = pci_dev_enable_resources, |
| 203 |
.ops_pci = &pci_dev_ops_pci, |
| 204 |
.init = xhci_init, |
| 205 |
}; |
| 206 |
|
| 207 |
static const unsigned short pci_device_ids[] = { |
| 208 |
0x9c31, |
| 209 |
0x9cb1, |
| 210 |
0 |
| 211 |
}; |
| 212 |
|
| 213 |
static const struct pci_driver pch_usb_xhci __pci_driver = { |
| 214 |
.ops = &usb_xhci_ops, |
| 215 |
.vendor = PCI_VID_INTEL, |
| 216 |
.devices = pci_device_ids, |
| 217 |
}; |
| 218 |
#endif |