blob: ee25e5c409a52fc6feafc1bddf18538c8e9e45ee [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älkki13f66502019-03-03 08:01:05 +020022#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020023#include <device/pci_ops.h>
Duncan Laurie2d9d39a2013-05-29 15:27:55 -070024#include "pch.h"
25
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -070026typedef struct southbridge_intel_lynxpoint_config config_t;
27
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010028#ifdef __SIMPLE_DEVICE__
29static u8 *usb_xhci_mem_base(pci_devfn_t dev)
30#else
31static u8 *usb_xhci_mem_base(struct device *dev)
32#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070033{
34 u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
35
36 /* Check if the controller is disabled or not present */
37 if (mem_base == 0 || mem_base == 0xffffffff)
38 return 0;
39
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080040 return (u8 *)(mem_base & ~0xf);
Duncan Laurie1f529082013-07-30 15:53:45 -070041}
42
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010043#ifdef __SIMPLE_DEVICE__
44static int usb_xhci_port_count_usb3(pci_devfn_t dev)
45#else
46static int usb_xhci_port_count_usb3(struct device *dev)
47#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070048{
49 if (pch_is_lp()) {
50 /* LynxPoint-LP has 4 SS ports */
51 return 4;
Duncan Laurie1f529082013-07-30 15:53:45 -070052 }
Elyes HAOUAS54f94242018-10-25 10:57:39 +020053 /* LynxPoint-H can have 0, 2, 4, or 6 SS ports */
54 u8 *mem_base = usb_xhci_mem_base(dev);
55 u32 fus = read32(mem_base + XHCI_USB3FUS);
56 fus >>= XHCI_USB3FUS_SS_SHIFT;
57 fus &= XHCI_USB3FUS_SS_MASK;
58 switch (fus) {
59 case 3: return 0;
60 case 2: return 2;
61 case 1: return 4;
62 case 0:
63 default: return 6;
64 }
Duncan Laurie1f529082013-07-30 15:53:45 -070065}
66
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080067static void usb_xhci_reset_status_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070068{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080069 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -070070 u32 status = read32(portsc);
71 /* Do not set Port Enabled/Disabled field */
72 status &= ~XHCI_USB3_PORTSC_PED;
73 /* Clear all change status bits */
74 status |= XHCI_USB3_PORTSC_CHST;
75 write32(portsc, status);
Duncan Laurie1f529082013-07-30 15:53:45 -070076}
77
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080078static void usb_xhci_reset_port_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070079{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080080 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -070081 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
82}
83
84#define XHCI_RESET_DELAY_US 1000 /* 1ms */
85#define XHCI_RESET_TIMEOUT 100 /* 100ms */
86
87/*
88 * 1) Wait until port is done polling
89 * 2) If port is disconnected
90 * a) Issue warm port reset
91 * b) Poll for warm reset complete
92 * c) Write 1 to port change status bits
93 */
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010094#ifdef __SIMPLE_DEVICE__
95static void usb_xhci_reset_usb3(pci_devfn_t dev, int all)
96#else
97static void usb_xhci_reset_usb3(struct device *dev, int all)
98#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070099{
100 u32 status, port_disabled;
101 int timeout, port;
102 int port_count = usb_xhci_port_count_usb3(dev);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800103 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -0700104
105 if (!mem_base || !port_count)
106 return;
107
108 /* Get mask of disabled ports */
109 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
110
111 /* Wait until all enabled ports are done polling */
112 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
113 int complete = 1;
114 for (port = 0; port < port_count; port++) {
115 /* Skip disabled ports */
116 if (port_disabled & (1 << port))
117 continue;
118 /* Read port link status field */
119 status = read32(mem_base + XHCI_USB3_PORTSC(port));
120 status &= XHCI_USB3_PORTSC_PLS;
121 if (status == XHCI_PLSR_POLLING)
122 complete = 0;
123 }
124 /* Exit if all ports not polling */
125 if (complete)
126 break;
127 udelay(XHCI_RESET_DELAY_US);
128 }
129
130 /* Reset all requested ports */
131 for (port = 0; port < port_count; port++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800132 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -0700133 /* Skip disabled ports */
134 if (port_disabled & (1 << port))
135 continue;
136 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
137 /* Reset all or only disconnected ports */
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700138 if (all || (status == XHCI_PLSR_RXDETECT ||
139 status == XHCI_PLSR_POLLING))
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700140 usb_xhci_reset_port_usb3(mem_base, port);
141 else
142 port_disabled |= 1 << port;
Duncan Laurie1f529082013-07-30 15:53:45 -0700143 }
144
145 /* Wait for warm reset complete on all reset ports */
146 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
147 int complete = 1;
148 for (port = 0; port < port_count; port++) {
149 /* Only check ports that were reset */
150 if (port_disabled & (1 << port))
151 continue;
152 /* Check if warm reset is complete */
153 status = read32(mem_base + XHCI_USB3_PORTSC(port));
154 if (!(status & XHCI_USB3_PORTSC_WRC))
155 complete = 0;
156 }
157 /* Check for warm reset complete in any port */
158 if (complete)
159 break;
160 udelay(XHCI_RESET_DELAY_US);
161 }
162
163 /* Clear port change status bits */
164 for (port = 0; port < port_count; port++)
165 usb_xhci_reset_status_usb3(mem_base, port);
166}
167
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700168#ifdef __SMM__
169
Duncan Laurie1f529082013-07-30 15:53:45 -0700170/* Handler for XHCI controller on entry to S3/S4/S5 */
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100171void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Laurie1f529082013-07-30 15:53:45 -0700172{
173 u16 reg16;
174 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800175 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -0700176
Aaron Durbinda5f5092016-07-13 23:23:16 -0500177 if (!mem_base || slp_typ < ACPI_S3)
Duncan Laurie1f529082013-07-30 15:53:45 -0700178 return;
179
180 if (pch_is_lp()) {
181 /* Set D0 state */
182 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
183 reg16 &= ~PWR_CTL_SET_MASK;
184 reg16 |= PWR_CTL_SET_D0;
185 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
186
187 /* Clear PCI 0xB0[14:13] */
188 reg32 = pci_read_config32(dev, 0xb0);
189 reg32 &= ~((1 << 14) | (1 << 13));
190 pci_write_config32(dev, 0xb0, reg32);
191
192 /* Clear MMIO 0x816c[14,2] */
193 reg32 = read32(mem_base + 0x816c);
194 reg32 &= ~((1 << 14) | (1 << 2));
195 write32(mem_base + 0x816c, reg32);
196
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700197 /* Reset disconnected USB3 ports */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700198 usb_xhci_reset_usb3(dev, 0);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700199
Duncan Laurie1f529082013-07-30 15:53:45 -0700200 /* Set MMIO 0x80e0[15] */
201 reg32 = read32(mem_base + 0x80e0);
202 reg32 |= (1 << 15);
203 write32(mem_base + 0x80e0, reg32);
204 }
205
206 /* Set D3Hot state and enable PME */
207 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_SET_D3);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700208 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_STATUS_PME);
Duncan Laurie1f529082013-07-30 15:53:45 -0700209 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_ENABLE_PME);
210}
211
Duncan Laurie911cedf2013-07-30 16:05:55 -0700212/* Route all ports to XHCI controller */
213void usb_xhci_route_all(void)
214{
215 u32 port_mask, route;
216 u16 reg16;
217
218 /* Skip if EHCI is already disabled */
219 if (RCBA32(FD) & PCH_DISABLE_EHCI1)
220 return;
221
222 /* Set D0 state */
223 reg16 = pci_read_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS);
224 reg16 &= ~PWR_CTL_SET_MASK;
225 reg16 |= PWR_CTL_SET_D0;
226 pci_write_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS, reg16);
227
228 /* Set USB3 superspeed enable */
229 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PRM);
230 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PR);
231 route &= ~XHCI_USB3PR_SSEN;
232 route |= XHCI_USB3PR_SSEN & port_mask;
233 pci_write_config32(PCH_XHCI_DEV, XHCI_USB3PR, route);
234
235 /* Route USB2 ports to XHCI controller */
236 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PRM);
237 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PR);
238 route &= ~XHCI_USB2PR_HCSEL;
239 route |= XHCI_USB2PR_HCSEL & port_mask;
240 pci_write_config32(PCH_XHCI_DEV, XHCI_USB2PR, route);
241
242 /* Disable EHCI controller */
243 usb_ehci_disable(PCH_EHCI1_DEV);
244
245 /* LynxPoint-H has a second EHCI controller */
246 if (!pch_is_lp())
247 usb_ehci_disable(PCH_EHCI2_DEV);
248
249 /* Reset and clear port change status */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700250 usb_xhci_reset_usb3(PCH_XHCI_DEV, 1);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700251}
252
Duncan Laurie1f529082013-07-30 15:53:45 -0700253#else /* !__SMM__ */
254
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100255static void usb_xhci_clock_gating(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700256{
257 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700258 u16 reg16;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700259
260 /* IOBP 0xE5004001[7:6] = 11b */
261 pch_iobp_update(0xe5004001, ~0, (1 << 7)|(1 << 6));
262
263 reg32 = pci_read_config32(dev, 0x40);
264 reg32 &= ~(1 << 23); /* unsupported request */
265
266 if (pch_is_lp()) {
267 /* D20:F0:40h[18,17,8] = 111b */
268 reg32 |= (1 << 18) | (1 << 17) | (1 << 8);
269 /* D20:F0:40h[21,20,19] = 110b to enable XHCI Idle L1 */
270 reg32 &= ~(1 << 19);
271 reg32 |= (1 << 21) | (1 << 20);
272 } else {
273 /* D20:F0:40h[21,20,18,17,8] = 11111b */
274 reg32 |= (1 << 21)|(1 << 20)|(1 << 18)|(1 << 17)|(1 << 8);
275 }
276
277 /* Avoid writing upper byte as it is write-once */
278 pci_write_config16(dev, 0x40, (u16)(reg32 & 0xffff));
279 pci_write_config8(dev, 0x40 + 2, (u8)((reg32 >> 16) & 0xff));
280
281 /* D20:F0:44h[9,7,3] = 111b */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700282 reg16 = pci_read_config16(dev, 0x44);
283 reg16 |= (1 << 9) | (1 << 7) | (1 << 3);
284 pci_write_config16(dev, 0x44, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700285
286 reg32 = pci_read_config32(dev, 0xa0);
287 if (pch_is_lp()) {
288 /* D20:F0:A0h[18] = 1 */
289 reg32 |= (1 << 18);
290 } else {
291 /* D20:F0:A0h[6] = 1 */
292 reg32 |= (1 << 6);
293 }
294 pci_write_config32(dev, 0xa0, reg32);
295
296 /* D20:F0:A4h[13] = 0 */
297 reg32 = pci_read_config32(dev, 0xa4);
298 reg32 &= ~(1 << 13);
299 pci_write_config32(dev, 0xa4, reg32);
300}
301
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100302static void usb_xhci_init(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700303{
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700304 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700305 u16 reg16;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800306 u8 *mem_base = usb_xhci_mem_base(dev);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700307 config_t *config = dev->chip_info;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700308
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700309 /* D20:F0:74h[1:0] = 00b (set D0 state) */
310 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
311 reg16 &= ~PWR_CTL_SET_MASK;
312 reg16 |= PWR_CTL_SET_D0;
313 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700314
315 /* Enable clock gating first */
316 usb_xhci_clock_gating(dev);
317
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700318 reg32 = read32(mem_base + 0x8144);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700319 if (pch_is_lp()) {
320 /* XHCIBAR + 8144h[8,7,6] = 111b */
321 reg32 |= (1 << 8) | (1 << 7) | (1 << 6);
322 } else {
323 /* XHCIBAR + 8144h[8,7,6] = 100b */
324 reg32 &= ~((1 << 7) | (1 << 6));
325 reg32 |= (1 << 8);
326 }
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700327 write32(mem_base + 0x8144, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700328
329 if (pch_is_lp()) {
Duncan Laurie527b03a2013-08-28 09:53:50 -0700330 /* XHCIBAR + 816Ch[19:0] = 000e0038h */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700331 reg32 = read32(mem_base + 0x816c);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700332 reg32 &= ~0x000fffff;
Duncan Laurie527b03a2013-08-28 09:53:50 -0700333 reg32 |= 0x000e0038;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700334 write32(mem_base + 0x816c, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700335
336 /* D20:F0:B0h[17,14,13] = 100b */
337 reg32 = pci_read_config32(dev, 0xb0);
338 reg32 &= ~((1 << 14) | (1 << 13));
339 reg32 |= (1 << 17);
340 pci_write_config32(dev, 0xb0, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700341 }
342
343 reg32 = pci_read_config32(dev, 0x50);
344 if (pch_is_lp()) {
345 /* D20:F0:50h[28:0] = 0FCE2E5Fh */
346 reg32 &= ~0x1fffffff;
347 reg32 |= 0x0fce2e5f;
348 } else {
349 /* D20:F0:50h[26:0] = 07886E9Fh */
350 reg32 &= ~0x07ffffff;
351 reg32 |= 0x07886e9f;
352 }
353 pci_write_config32(dev, 0x50, reg32);
354
355 /* D20:F0:44h[31] = 1 (Access Control Bit) */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700356 reg32 = pci_read_config32(dev, 0x44);
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700357 reg32 |= (1UL << 31);
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700358 pci_write_config32(dev, 0x44, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700359
360 /* D20:F0:40h[31,23] = 10b (OC Configuration Done) */
361 reg32 = pci_read_config32(dev, 0x40);
362 reg32 &= ~(1 << 23); /* unsupported request */
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700363 reg32 |= (1UL << 31);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700364 pci_write_config32(dev, 0x40, reg32);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700365
366 if (acpi_is_wakeup_s3()) {
367 /* Reset ports that are disabled or
368 * polling before returning to the OS. */
369 usb_xhci_reset_usb3(dev, 0);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700370 } else if (config->xhci_default) {
371 /* Route all ports to XHCI */
372 outb(0xca, 0xb2);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700373 }
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700374}
375
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100376static void usb_xhci_set_subsystem(struct device *dev, unsigned int vendor,
377 unsigned int device)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700378{
379 if (!vendor || !device) {
380 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
381 pci_read_config32(dev, PCI_VENDOR_ID));
382 } else {
383 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
384 ((device & 0xffff) << 16) | (vendor & 0xffff));
385 }
386}
387
388static struct pci_operations lops_pci = {
389 .set_subsystem = &usb_xhci_set_subsystem,
390};
391
392static struct device_operations usb_xhci_ops = {
393 .read_resources = pci_dev_read_resources,
394 .set_resources = pci_dev_set_resources,
395 .enable_resources = pci_dev_enable_resources,
396 .init = usb_xhci_init,
397 .ops_pci = &lops_pci,
398};
399
400static const unsigned short pci_device_ids[] = { 0x8c31, /* LynxPoint-H */
401 0x9c31, /* LynxPoint-LP */
402 0 };
403
404static const struct pci_driver pch_usb_xhci __pci_driver = {
405 .ops = &usb_xhci_ops,
406 .vendor = PCI_VENDOR_ID_INTEL,
407 .devices = pci_device_ids,
408};
Duncan Laurie1f529082013-07-30 15:53:45 -0700409#endif /* !__SMM__ */