blob: 490740e730c72da1fc971eb34e59791319b5a970 [file] [log] [blame]
Duncan Laurie2d9d39a2013-05-29 15:27:55 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Duncan Laurie2d9d39a2013-05-29 15:27:55 -070015 */
16
Duncan Laurie1f529082013-07-30 15:53:45 -070017#include <delay.h>
Duncan Laurie2d9d39a2013-05-29 15:27:55 -070018#include <device/device.h>
19#include <device/pci.h>
20#include <device/pci_ids.h>
21#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020022#include <device/pci_ops.h>
Duncan Laurie2d9d39a2013-05-29 15:27:55 -070023#include "pch.h"
24
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -070025typedef struct southbridge_intel_lynxpoint_config config_t;
26
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010027#ifdef __SIMPLE_DEVICE__
28static u8 *usb_xhci_mem_base(pci_devfn_t dev)
29#else
30static u8 *usb_xhci_mem_base(struct device *dev)
31#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070032{
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 Herbertbde6d302014-12-24 18:43:20 -080039 return (u8 *)(mem_base & ~0xf);
Duncan Laurie1f529082013-07-30 15:53:45 -070040}
41
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010042#ifdef __SIMPLE_DEVICE__
43static int usb_xhci_port_count_usb3(pci_devfn_t dev)
44#else
45static int usb_xhci_port_count_usb3(struct device *dev)
46#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070047{
48 if (pch_is_lp()) {
49 /* LynxPoint-LP has 4 SS ports */
50 return 4;
Duncan Laurie1f529082013-07-30 15:53:45 -070051 }
Elyes HAOUAS54f94242018-10-25 10:57:39 +020052 /* LynxPoint-H can have 0, 2, 4, or 6 SS ports */
53 u8 *mem_base = usb_xhci_mem_base(dev);
54 u32 fus = read32(mem_base + XHCI_USB3FUS);
55 fus >>= XHCI_USB3FUS_SS_SHIFT;
56 fus &= XHCI_USB3FUS_SS_MASK;
57 switch (fus) {
58 case 3: return 0;
59 case 2: return 2;
60 case 1: return 4;
61 case 0:
62 default: return 6;
63 }
Duncan Laurie1f529082013-07-30 15:53:45 -070064}
65
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080066static void usb_xhci_reset_status_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070067{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080068 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -070069 u32 status = read32(portsc);
70 /* Do not set Port Enabled/Disabled field */
71 status &= ~XHCI_USB3_PORTSC_PED;
72 /* Clear all change status bits */
73 status |= XHCI_USB3_PORTSC_CHST;
74 write32(portsc, status);
Duncan Laurie1f529082013-07-30 15:53:45 -070075}
76
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080077static void usb_xhci_reset_port_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070078{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080079 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -070080 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
81}
82
83#define XHCI_RESET_DELAY_US 1000 /* 1ms */
84#define XHCI_RESET_TIMEOUT 100 /* 100ms */
85
86/*
87 * 1) Wait until port is done polling
88 * 2) If port is disconnected
89 * a) Issue warm port reset
90 * b) Poll for warm reset complete
91 * c) Write 1 to port change status bits
92 */
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010093#ifdef __SIMPLE_DEVICE__
94static void usb_xhci_reset_usb3(pci_devfn_t dev, int all)
95#else
96static void usb_xhci_reset_usb3(struct device *dev, int all)
97#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070098{
99 u32 status, port_disabled;
100 int timeout, port;
101 int port_count = usb_xhci_port_count_usb3(dev);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800102 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -0700103
104 if (!mem_base || !port_count)
105 return;
106
107 /* Get mask of disabled ports */
108 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
109
110 /* Wait until all enabled ports are done polling */
111 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
112 int complete = 1;
113 for (port = 0; port < port_count; port++) {
114 /* Skip disabled ports */
115 if (port_disabled & (1 << port))
116 continue;
117 /* Read port link status field */
118 status = read32(mem_base + XHCI_USB3_PORTSC(port));
119 status &= XHCI_USB3_PORTSC_PLS;
120 if (status == XHCI_PLSR_POLLING)
121 complete = 0;
122 }
123 /* Exit if all ports not polling */
124 if (complete)
125 break;
126 udelay(XHCI_RESET_DELAY_US);
127 }
128
129 /* Reset all requested ports */
130 for (port = 0; port < port_count; port++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800131 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -0700132 /* Skip disabled ports */
133 if (port_disabled & (1 << port))
134 continue;
135 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
136 /* Reset all or only disconnected ports */
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700137 if (all || (status == XHCI_PLSR_RXDETECT ||
138 status == XHCI_PLSR_POLLING))
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700139 usb_xhci_reset_port_usb3(mem_base, port);
140 else
141 port_disabled |= 1 << port;
Duncan Laurie1f529082013-07-30 15:53:45 -0700142 }
143
144 /* Wait for warm reset complete on all reset ports */
145 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
146 int complete = 1;
147 for (port = 0; port < port_count; port++) {
148 /* Only check ports that were reset */
149 if (port_disabled & (1 << port))
150 continue;
151 /* Check if warm reset is complete */
152 status = read32(mem_base + XHCI_USB3_PORTSC(port));
153 if (!(status & XHCI_USB3_PORTSC_WRC))
154 complete = 0;
155 }
156 /* Check for warm reset complete in any port */
157 if (complete)
158 break;
159 udelay(XHCI_RESET_DELAY_US);
160 }
161
162 /* Clear port change status bits */
163 for (port = 0; port < port_count; port++)
164 usb_xhci_reset_status_usb3(mem_base, port);
165}
166
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700167#ifdef __SMM__
168
Duncan Laurie1f529082013-07-30 15:53:45 -0700169/* Handler for XHCI controller on entry to S3/S4/S5 */
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100170void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Laurie1f529082013-07-30 15:53:45 -0700171{
172 u16 reg16;
173 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800174 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -0700175
Aaron Durbinda5f5092016-07-13 23:23:16 -0500176 if (!mem_base || slp_typ < ACPI_S3)
Duncan Laurie1f529082013-07-30 15:53:45 -0700177 return;
178
179 if (pch_is_lp()) {
180 /* Set D0 state */
181 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
182 reg16 &= ~PWR_CTL_SET_MASK;
183 reg16 |= PWR_CTL_SET_D0;
184 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
185
186 /* Clear PCI 0xB0[14:13] */
187 reg32 = pci_read_config32(dev, 0xb0);
188 reg32 &= ~((1 << 14) | (1 << 13));
189 pci_write_config32(dev, 0xb0, reg32);
190
191 /* Clear MMIO 0x816c[14,2] */
192 reg32 = read32(mem_base + 0x816c);
193 reg32 &= ~((1 << 14) | (1 << 2));
194 write32(mem_base + 0x816c, reg32);
195
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700196 /* Reset disconnected USB3 ports */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700197 usb_xhci_reset_usb3(dev, 0);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700198
Duncan Laurie1f529082013-07-30 15:53:45 -0700199 /* Set MMIO 0x80e0[15] */
200 reg32 = read32(mem_base + 0x80e0);
201 reg32 |= (1 << 15);
202 write32(mem_base + 0x80e0, reg32);
203 }
204
205 /* Set D3Hot state and enable PME */
206 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_SET_D3);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700207 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_STATUS_PME);
Duncan Laurie1f529082013-07-30 15:53:45 -0700208 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_ENABLE_PME);
209}
210
Duncan Laurie911cedf2013-07-30 16:05:55 -0700211/* Route all ports to XHCI controller */
212void usb_xhci_route_all(void)
213{
214 u32 port_mask, route;
215 u16 reg16;
216
217 /* Skip if EHCI is already disabled */
218 if (RCBA32(FD) & PCH_DISABLE_EHCI1)
219 return;
220
221 /* Set D0 state */
222 reg16 = pci_read_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS);
223 reg16 &= ~PWR_CTL_SET_MASK;
224 reg16 |= PWR_CTL_SET_D0;
225 pci_write_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS, reg16);
226
227 /* Set USB3 superspeed enable */
228 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PRM);
229 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PR);
230 route &= ~XHCI_USB3PR_SSEN;
231 route |= XHCI_USB3PR_SSEN & port_mask;
232 pci_write_config32(PCH_XHCI_DEV, XHCI_USB3PR, route);
233
234 /* Route USB2 ports to XHCI controller */
235 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PRM);
236 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PR);
237 route &= ~XHCI_USB2PR_HCSEL;
238 route |= XHCI_USB2PR_HCSEL & port_mask;
239 pci_write_config32(PCH_XHCI_DEV, XHCI_USB2PR, route);
240
241 /* Disable EHCI controller */
242 usb_ehci_disable(PCH_EHCI1_DEV);
243
244 /* LynxPoint-H has a second EHCI controller */
245 if (!pch_is_lp())
246 usb_ehci_disable(PCH_EHCI2_DEV);
247
248 /* Reset and clear port change status */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700249 usb_xhci_reset_usb3(PCH_XHCI_DEV, 1);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700250}
251
Duncan Laurie1f529082013-07-30 15:53:45 -0700252#else /* !__SMM__ */
253
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100254static void usb_xhci_clock_gating(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700255{
256 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700257 u16 reg16;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700258
259 /* IOBP 0xE5004001[7:6] = 11b */
260 pch_iobp_update(0xe5004001, ~0, (1 << 7)|(1 << 6));
261
262 reg32 = pci_read_config32(dev, 0x40);
263 reg32 &= ~(1 << 23); /* unsupported request */
264
265 if (pch_is_lp()) {
266 /* D20:F0:40h[18,17,8] = 111b */
267 reg32 |= (1 << 18) | (1 << 17) | (1 << 8);
268 /* D20:F0:40h[21,20,19] = 110b to enable XHCI Idle L1 */
269 reg32 &= ~(1 << 19);
270 reg32 |= (1 << 21) | (1 << 20);
271 } else {
272 /* D20:F0:40h[21,20,18,17,8] = 11111b */
273 reg32 |= (1 << 21)|(1 << 20)|(1 << 18)|(1 << 17)|(1 << 8);
274 }
275
276 /* Avoid writing upper byte as it is write-once */
277 pci_write_config16(dev, 0x40, (u16)(reg32 & 0xffff));
278 pci_write_config8(dev, 0x40 + 2, (u8)((reg32 >> 16) & 0xff));
279
280 /* D20:F0:44h[9,7,3] = 111b */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700281 reg16 = pci_read_config16(dev, 0x44);
282 reg16 |= (1 << 9) | (1 << 7) | (1 << 3);
283 pci_write_config16(dev, 0x44, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700284
285 reg32 = pci_read_config32(dev, 0xa0);
286 if (pch_is_lp()) {
287 /* D20:F0:A0h[18] = 1 */
288 reg32 |= (1 << 18);
289 } else {
290 /* D20:F0:A0h[6] = 1 */
291 reg32 |= (1 << 6);
292 }
293 pci_write_config32(dev, 0xa0, reg32);
294
295 /* D20:F0:A4h[13] = 0 */
296 reg32 = pci_read_config32(dev, 0xa4);
297 reg32 &= ~(1 << 13);
298 pci_write_config32(dev, 0xa4, reg32);
299}
300
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100301static void usb_xhci_init(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700302{
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700303 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700304 u16 reg16;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800305 u8 *mem_base = usb_xhci_mem_base(dev);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700306 config_t *config = dev->chip_info;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700307
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700308 /* D20:F0:74h[1:0] = 00b (set D0 state) */
309 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
310 reg16 &= ~PWR_CTL_SET_MASK;
311 reg16 |= PWR_CTL_SET_D0;
312 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700313
314 /* Enable clock gating first */
315 usb_xhci_clock_gating(dev);
316
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700317 reg32 = read32(mem_base + 0x8144);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700318 if (pch_is_lp()) {
319 /* XHCIBAR + 8144h[8,7,6] = 111b */
320 reg32 |= (1 << 8) | (1 << 7) | (1 << 6);
321 } else {
322 /* XHCIBAR + 8144h[8,7,6] = 100b */
323 reg32 &= ~((1 << 7) | (1 << 6));
324 reg32 |= (1 << 8);
325 }
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700326 write32(mem_base + 0x8144, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700327
328 if (pch_is_lp()) {
Duncan Laurie527b03a2013-08-28 09:53:50 -0700329 /* XHCIBAR + 816Ch[19:0] = 000e0038h */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700330 reg32 = read32(mem_base + 0x816c);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700331 reg32 &= ~0x000fffff;
Duncan Laurie527b03a2013-08-28 09:53:50 -0700332 reg32 |= 0x000e0038;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700333 write32(mem_base + 0x816c, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700334
335 /* D20:F0:B0h[17,14,13] = 100b */
336 reg32 = pci_read_config32(dev, 0xb0);
337 reg32 &= ~((1 << 14) | (1 << 13));
338 reg32 |= (1 << 17);
339 pci_write_config32(dev, 0xb0, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700340 }
341
342 reg32 = pci_read_config32(dev, 0x50);
343 if (pch_is_lp()) {
344 /* D20:F0:50h[28:0] = 0FCE2E5Fh */
345 reg32 &= ~0x1fffffff;
346 reg32 |= 0x0fce2e5f;
347 } else {
348 /* D20:F0:50h[26:0] = 07886E9Fh */
349 reg32 &= ~0x07ffffff;
350 reg32 |= 0x07886e9f;
351 }
352 pci_write_config32(dev, 0x50, reg32);
353
354 /* D20:F0:44h[31] = 1 (Access Control Bit) */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700355 reg32 = pci_read_config32(dev, 0x44);
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700356 reg32 |= (1UL << 31);
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700357 pci_write_config32(dev, 0x44, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700358
359 /* D20:F0:40h[31,23] = 10b (OC Configuration Done) */
360 reg32 = pci_read_config32(dev, 0x40);
361 reg32 &= ~(1 << 23); /* unsupported request */
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700362 reg32 |= (1UL << 31);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700363 pci_write_config32(dev, 0x40, reg32);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700364
365 if (acpi_is_wakeup_s3()) {
366 /* Reset ports that are disabled or
367 * polling before returning to the OS. */
368 usb_xhci_reset_usb3(dev, 0);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700369 } else if (config->xhci_default) {
370 /* Route all ports to XHCI */
371 outb(0xca, 0xb2);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700372 }
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700373}
374
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100375static void usb_xhci_set_subsystem(struct device *dev, unsigned int vendor,
376 unsigned int device)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700377{
378 if (!vendor || !device) {
379 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
380 pci_read_config32(dev, PCI_VENDOR_ID));
381 } else {
382 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
383 ((device & 0xffff) << 16) | (vendor & 0xffff));
384 }
385}
386
387static struct pci_operations lops_pci = {
388 .set_subsystem = &usb_xhci_set_subsystem,
389};
390
391static struct device_operations usb_xhci_ops = {
392 .read_resources = pci_dev_read_resources,
393 .set_resources = pci_dev_set_resources,
394 .enable_resources = pci_dev_enable_resources,
395 .init = usb_xhci_init,
396 .ops_pci = &lops_pci,
397};
398
399static const unsigned short pci_device_ids[] = { 0x8c31, /* LynxPoint-H */
400 0x9c31, /* LynxPoint-LP */
401 0 };
402
403static const struct pci_driver pch_usb_xhci __pci_driver = {
404 .ops = &usb_xhci_ops,
405 .vendor = PCI_VENDOR_ID_INTEL,
406 .devices = pci_device_ids,
407};
Duncan Laurie1f529082013-07-30 15:53:45 -0700408#endif /* !__SMM__ */