Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2014 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <console/console.h> |
| 21 | #include <delay.h> |
| 22 | #include <device/device.h> |
| 23 | #include <device/pci.h> |
| 24 | #include <device/pci_ids.h> |
| 25 | #include <arch/io.h> |
Julius Werner | 4ee4bd5 | 2014-10-20 13:46:39 -0700 | [diff] [blame] | 26 | #include <soc/ramstage.h> |
| 27 | #include <soc/xhci.h> |
| 28 | #include <soc/cpu.h> |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 29 | |
| 30 | #ifdef __SMM__ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 31 | static u8 *usb_xhci_mem_base(device_t dev) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 32 | { |
| 33 | u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0); |
| 34 | |
| 35 | /* Check if the controller is disabled or not present */ |
| 36 | if (mem_base == 0 || mem_base == 0xffffffff) |
| 37 | return 0; |
| 38 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 39 | return (u8 *)(mem_base & ~0xf); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static int usb_xhci_port_count_usb3(device_t dev) |
| 43 | { |
| 44 | /* PCH-LP has 4 SS ports */ |
| 45 | return 4; |
| 46 | } |
| 47 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 48 | static void usb_xhci_reset_status_usb3(u8 *mem_base, int port) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 49 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 50 | u8 *portsc = mem_base + XHCI_USB3_PORTSC(port); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 51 | u32 status = read32(portsc); |
| 52 | /* Do not set Port Enabled/Disabled field */ |
| 53 | status &= ~XHCI_USB3_PORTSC_PED; |
| 54 | /* Clear all change status bits */ |
| 55 | status |= XHCI_USB3_PORTSC_CHST; |
| 56 | write32(portsc, status); |
| 57 | } |
| 58 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 59 | static void usb_xhci_reset_port_usb3(u8 *mem_base, int port) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 60 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 61 | u8 *portsc = mem_base + XHCI_USB3_PORTSC(port); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 62 | write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR); |
| 63 | } |
| 64 | |
| 65 | #define XHCI_RESET_DELAY_US 1000 /* 1ms */ |
| 66 | #define XHCI_RESET_TIMEOUT 100 /* 100ms */ |
| 67 | |
| 68 | /* |
| 69 | * 1) Wait until port is done polling |
| 70 | * 2) If port is disconnected |
| 71 | * a) Issue warm port reset |
| 72 | * b) Poll for warm reset complete |
| 73 | * c) Write 1 to port change status bits |
| 74 | */ |
| 75 | static void usb_xhci_reset_usb3(device_t dev, int all) |
| 76 | { |
| 77 | u32 status, port_disabled; |
| 78 | int timeout, port; |
| 79 | int port_count = usb_xhci_port_count_usb3(dev); |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 80 | u8 *mem_base = usb_xhci_mem_base(dev); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 81 | |
| 82 | if (!mem_base || !port_count) |
| 83 | return; |
| 84 | |
| 85 | /* Get mask of disabled ports */ |
| 86 | port_disabled = pci_read_config32(dev, XHCI_USB3PDO); |
| 87 | |
| 88 | /* Wait until all enabled ports are done polling */ |
| 89 | for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) { |
| 90 | int complete = 1; |
| 91 | for (port = 0; port < port_count; port++) { |
| 92 | /* Skip disabled ports */ |
| 93 | if (port_disabled & (1 << port)) |
| 94 | continue; |
| 95 | /* Read port link status field */ |
| 96 | status = read32(mem_base + XHCI_USB3_PORTSC(port)); |
| 97 | status &= XHCI_USB3_PORTSC_PLS; |
| 98 | if (status == XHCI_PLSR_POLLING) |
| 99 | complete = 0; |
| 100 | } |
| 101 | /* Exit if all ports not polling */ |
| 102 | if (complete) |
| 103 | break; |
| 104 | udelay(XHCI_RESET_DELAY_US); |
| 105 | } |
| 106 | |
| 107 | /* Reset all requested ports */ |
| 108 | for (port = 0; port < port_count; port++) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 109 | u8 *portsc = mem_base + XHCI_USB3_PORTSC(port); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 110 | /* Skip disabled ports */ |
| 111 | if (port_disabled & (1 << port)) |
| 112 | continue; |
| 113 | status = read32(portsc) & XHCI_USB3_PORTSC_PLS; |
| 114 | /* Reset all or only disconnected ports */ |
| 115 | if (all || (status == XHCI_PLSR_RXDETECT || |
| 116 | status == XHCI_PLSR_POLLING)) |
| 117 | usb_xhci_reset_port_usb3(mem_base, port); |
| 118 | else |
| 119 | port_disabled |= 1 << port; |
| 120 | } |
| 121 | |
| 122 | /* Wait for warm reset complete on all reset ports */ |
| 123 | for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) { |
| 124 | int complete = 1; |
| 125 | for (port = 0; port < port_count; port++) { |
| 126 | /* Only check ports that were reset */ |
| 127 | if (port_disabled & (1 << port)) |
| 128 | continue; |
| 129 | /* Check if warm reset is complete */ |
| 130 | status = read32(mem_base + XHCI_USB3_PORTSC(port)); |
| 131 | if (!(status & XHCI_USB3_PORTSC_WRC)) |
| 132 | complete = 0; |
| 133 | } |
| 134 | /* Check for warm reset complete in any port */ |
| 135 | if (complete) |
| 136 | break; |
| 137 | udelay(XHCI_RESET_DELAY_US); |
| 138 | } |
| 139 | |
| 140 | /* Clear port change status bits */ |
| 141 | for (port = 0; port < port_count; port++) |
| 142 | usb_xhci_reset_status_usb3(mem_base, port); |
| 143 | } |
| 144 | |
| 145 | /* Handler for XHCI controller on entry to S3/S4/S5 */ |
| 146 | void usb_xhci_sleep_prepare(device_t dev, u8 slp_typ) |
| 147 | { |
| 148 | u16 reg16; |
| 149 | u32 reg32; |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 150 | u8 *mem_base = usb_xhci_mem_base(dev); |
Kane Chen | 4613472 | 2014-08-28 17:05:06 -0700 | [diff] [blame] | 151 | u8 is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 152 | |
| 153 | if (!mem_base || slp_typ < 3) |
| 154 | return; |
| 155 | |
| 156 | /* Set D0 state */ |
| 157 | reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS); |
| 158 | reg16 &= ~XHCI_PWR_CTL_SET_MASK; |
| 159 | reg16 |= XHCI_PWR_CTL_SET_D0; |
| 160 | pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16); |
| 161 | |
Kane Chen | 4613472 | 2014-08-28 17:05:06 -0700 | [diff] [blame] | 162 | if (!is_broadwell) { |
| 163 | /* This WA is only for lpt */ |
| 164 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 165 | /* Clear PCI 0xB0[14:13] */ |
| 166 | reg32 = pci_read_config32(dev, 0xb0); |
| 167 | reg32 &= ~((1 << 14) | (1 << 13)); |
| 168 | pci_write_config32(dev, 0xb0, reg32); |
| 169 | |
| 170 | /* Clear MMIO 0x816c[14,2] */ |
| 171 | reg32 = read32(mem_base + 0x816c); |
| 172 | reg32 &= ~((1 << 14) | (1 << 2)); |
| 173 | write32(mem_base + 0x816c, reg32); |
| 174 | |
| 175 | /* Reset disconnected USB3 ports */ |
| 176 | usb_xhci_reset_usb3(dev, 0); |
| 177 | |
| 178 | /* Set MMIO 0x80e0[15] */ |
| 179 | reg32 = read32(mem_base + 0x80e0); |
| 180 | reg32 |= (1 << 15); |
| 181 | write32(mem_base + 0x80e0, reg32); |
Kane Chen | 4613472 | 2014-08-28 17:05:06 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | reg32 = read32(mem_base + 0x8154); |
| 185 | reg32 &= ~(1 << 31); |
| 186 | write32(mem_base + 0x8154, reg32); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 187 | |
| 188 | /* Set D3Hot state and enable PME */ |
| 189 | pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_SET_D3); |
| 190 | pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_STATUS_PME); |
| 191 | pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_ENABLE_PME); |
| 192 | } |
| 193 | #else /* !__SMM__ */ |
| 194 | |
Duncan Laurie | aafdddf | 2015-02-19 16:21:10 -0800 | [diff] [blame] | 195 | static void xhci_init(struct device *dev) |
| 196 | { |
| 197 | struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 198 | u16 reg16; |
| 199 | u32 reg32; |
| 200 | |
| 201 | /* Ensure controller is in D0 state */ |
| 202 | reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS); |
| 203 | reg16 &= ~XHCI_PWR_CTL_SET_MASK; |
| 204 | reg16 |= XHCI_PWR_CTL_SET_D0; |
| 205 | pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16); |
| 206 | |
| 207 | /* Disable Compliance Mode Entry */ |
| 208 | reg32 = read32(res2mmio(res, 0x80ec, 0)); |
| 209 | reg32 |= (1 << 0); |
| 210 | write32(res2mmio(res, 0x80ec, 0), reg32); |
| 211 | } |
| 212 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 213 | static struct device_operations usb_xhci_ops = { |
| 214 | .read_resources = &pci_dev_read_resources, |
| 215 | .set_resources = &pci_dev_set_resources, |
| 216 | .enable_resources = &pci_dev_enable_resources, |
| 217 | .ops_pci = &broadwell_pci_ops, |
Duncan Laurie | aafdddf | 2015-02-19 16:21:10 -0800 | [diff] [blame] | 218 | .init = &xhci_init, |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | static const unsigned short pci_device_ids[] = { |
| 222 | 0x9c31, /* LynxPoint-LP */ |
| 223 | 0x9cb1, /* WildcatPoint */ |
| 224 | 0 |
| 225 | }; |
| 226 | |
| 227 | static const struct pci_driver pch_usb_xhci __pci_driver = { |
| 228 | .ops = &usb_xhci_ops, |
| 229 | .vendor = PCI_VENDOR_ID_INTEL, |
| 230 | .devices = pci_device_ids, |
| 231 | }; |
| 232 | #endif /* !__SMM__ */ |