blob: 03341da829691cb5588f15f070f3cb7f6c39cb15 [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>
22#include "pch.h"
23
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -070024typedef struct southbridge_intel_lynxpoint_config config_t;
25
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010026#ifdef __SIMPLE_DEVICE__
27static u8 *usb_xhci_mem_base(pci_devfn_t dev)
28#else
29static u8 *usb_xhci_mem_base(struct device *dev)
30#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070031{
32 u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
33
34 /* Check if the controller is disabled or not present */
35 if (mem_base == 0 || mem_base == 0xffffffff)
36 return 0;
37
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080038 return (u8 *)(mem_base & ~0xf);
Duncan Laurie1f529082013-07-30 15:53:45 -070039}
40
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010041#ifdef __SIMPLE_DEVICE__
42static int usb_xhci_port_count_usb3(pci_devfn_t dev)
43#else
44static int usb_xhci_port_count_usb3(struct device *dev)
45#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070046{
47 if (pch_is_lp()) {
48 /* LynxPoint-LP has 4 SS ports */
49 return 4;
Duncan Laurie1f529082013-07-30 15:53:45 -070050 }
Elyes HAOUAS54f94242018-10-25 10:57:39 +020051 /* LynxPoint-H can have 0, 2, 4, or 6 SS ports */
52 u8 *mem_base = usb_xhci_mem_base(dev);
53 u32 fus = read32(mem_base + XHCI_USB3FUS);
54 fus >>= XHCI_USB3FUS_SS_SHIFT;
55 fus &= XHCI_USB3FUS_SS_MASK;
56 switch (fus) {
57 case 3: return 0;
58 case 2: return 2;
59 case 1: return 4;
60 case 0:
61 default: return 6;
62 }
Duncan Laurie1f529082013-07-30 15:53:45 -070063}
64
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080065static void usb_xhci_reset_status_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070066{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080067 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -070068 u32 status = read32(portsc);
69 /* Do not set Port Enabled/Disabled field */
70 status &= ~XHCI_USB3_PORTSC_PED;
71 /* Clear all change status bits */
72 status |= XHCI_USB3_PORTSC_CHST;
73 write32(portsc, status);
Duncan Laurie1f529082013-07-30 15:53:45 -070074}
75
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080076static void usb_xhci_reset_port_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070077{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080078 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -070079 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
80}
81
82#define XHCI_RESET_DELAY_US 1000 /* 1ms */
83#define XHCI_RESET_TIMEOUT 100 /* 100ms */
84
85/*
86 * 1) Wait until port is done polling
87 * 2) If port is disconnected
88 * a) Issue warm port reset
89 * b) Poll for warm reset complete
90 * c) Write 1 to port change status bits
91 */
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010092#ifdef __SIMPLE_DEVICE__
93static void usb_xhci_reset_usb3(pci_devfn_t dev, int all)
94#else
95static void usb_xhci_reset_usb3(struct device *dev, int all)
96#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070097{
98 u32 status, port_disabled;
99 int timeout, port;
100 int port_count = usb_xhci_port_count_usb3(dev);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800101 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -0700102
103 if (!mem_base || !port_count)
104 return;
105
106 /* Get mask of disabled ports */
107 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
108
109 /* Wait until all enabled ports are done polling */
110 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
111 int complete = 1;
112 for (port = 0; port < port_count; port++) {
113 /* Skip disabled ports */
114 if (port_disabled & (1 << port))
115 continue;
116 /* Read port link status field */
117 status = read32(mem_base + XHCI_USB3_PORTSC(port));
118 status &= XHCI_USB3_PORTSC_PLS;
119 if (status == XHCI_PLSR_POLLING)
120 complete = 0;
121 }
122 /* Exit if all ports not polling */
123 if (complete)
124 break;
125 udelay(XHCI_RESET_DELAY_US);
126 }
127
128 /* Reset all requested ports */
129 for (port = 0; port < port_count; port++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800130 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -0700131 /* Skip disabled ports */
132 if (port_disabled & (1 << port))
133 continue;
134 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
135 /* Reset all or only disconnected ports */
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700136 if (all || (status == XHCI_PLSR_RXDETECT ||
137 status == XHCI_PLSR_POLLING))
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700138 usb_xhci_reset_port_usb3(mem_base, port);
139 else
140 port_disabled |= 1 << port;
Duncan Laurie1f529082013-07-30 15:53:45 -0700141 }
142
143 /* Wait for warm reset complete on all reset ports */
144 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
145 int complete = 1;
146 for (port = 0; port < port_count; port++) {
147 /* Only check ports that were reset */
148 if (port_disabled & (1 << port))
149 continue;
150 /* Check if warm reset is complete */
151 status = read32(mem_base + XHCI_USB3_PORTSC(port));
152 if (!(status & XHCI_USB3_PORTSC_WRC))
153 complete = 0;
154 }
155 /* Check for warm reset complete in any port */
156 if (complete)
157 break;
158 udelay(XHCI_RESET_DELAY_US);
159 }
160
161 /* Clear port change status bits */
162 for (port = 0; port < port_count; port++)
163 usb_xhci_reset_status_usb3(mem_base, port);
164}
165
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700166#ifdef __SMM__
167
Duncan Laurie1f529082013-07-30 15:53:45 -0700168/* Handler for XHCI controller on entry to S3/S4/S5 */
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100169void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Laurie1f529082013-07-30 15:53:45 -0700170{
171 u16 reg16;
172 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800173 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -0700174
Aaron Durbinda5f5092016-07-13 23:23:16 -0500175 if (!mem_base || slp_typ < ACPI_S3)
Duncan Laurie1f529082013-07-30 15:53:45 -0700176 return;
177
178 if (pch_is_lp()) {
179 /* Set D0 state */
180 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
181 reg16 &= ~PWR_CTL_SET_MASK;
182 reg16 |= PWR_CTL_SET_D0;
183 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
184
185 /* Clear PCI 0xB0[14:13] */
186 reg32 = pci_read_config32(dev, 0xb0);
187 reg32 &= ~((1 << 14) | (1 << 13));
188 pci_write_config32(dev, 0xb0, reg32);
189
190 /* Clear MMIO 0x816c[14,2] */
191 reg32 = read32(mem_base + 0x816c);
192 reg32 &= ~((1 << 14) | (1 << 2));
193 write32(mem_base + 0x816c, reg32);
194
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700195 /* Reset disconnected USB3 ports */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700196 usb_xhci_reset_usb3(dev, 0);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700197
Duncan Laurie1f529082013-07-30 15:53:45 -0700198 /* Set MMIO 0x80e0[15] */
199 reg32 = read32(mem_base + 0x80e0);
200 reg32 |= (1 << 15);
201 write32(mem_base + 0x80e0, reg32);
202 }
203
204 /* Set D3Hot state and enable PME */
205 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_SET_D3);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700206 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_STATUS_PME);
Duncan Laurie1f529082013-07-30 15:53:45 -0700207 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_ENABLE_PME);
208}
209
Duncan Laurie911cedf2013-07-30 16:05:55 -0700210/* Route all ports to XHCI controller */
211void usb_xhci_route_all(void)
212{
213 u32 port_mask, route;
214 u16 reg16;
215
216 /* Skip if EHCI is already disabled */
217 if (RCBA32(FD) & PCH_DISABLE_EHCI1)
218 return;
219
220 /* Set D0 state */
221 reg16 = pci_read_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS);
222 reg16 &= ~PWR_CTL_SET_MASK;
223 reg16 |= PWR_CTL_SET_D0;
224 pci_write_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS, reg16);
225
226 /* Set USB3 superspeed enable */
227 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PRM);
228 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PR);
229 route &= ~XHCI_USB3PR_SSEN;
230 route |= XHCI_USB3PR_SSEN & port_mask;
231 pci_write_config32(PCH_XHCI_DEV, XHCI_USB3PR, route);
232
233 /* Route USB2 ports to XHCI controller */
234 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PRM);
235 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PR);
236 route &= ~XHCI_USB2PR_HCSEL;
237 route |= XHCI_USB2PR_HCSEL & port_mask;
238 pci_write_config32(PCH_XHCI_DEV, XHCI_USB2PR, route);
239
240 /* Disable EHCI controller */
241 usb_ehci_disable(PCH_EHCI1_DEV);
242
243 /* LynxPoint-H has a second EHCI controller */
244 if (!pch_is_lp())
245 usb_ehci_disable(PCH_EHCI2_DEV);
246
247 /* Reset and clear port change status */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700248 usb_xhci_reset_usb3(PCH_XHCI_DEV, 1);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700249}
250
Duncan Laurie1f529082013-07-30 15:53:45 -0700251#else /* !__SMM__ */
252
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100253static void usb_xhci_clock_gating(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700254{
255 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700256 u16 reg16;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700257
258 /* IOBP 0xE5004001[7:6] = 11b */
259 pch_iobp_update(0xe5004001, ~0, (1 << 7)|(1 << 6));
260
261 reg32 = pci_read_config32(dev, 0x40);
262 reg32 &= ~(1 << 23); /* unsupported request */
263
264 if (pch_is_lp()) {
265 /* D20:F0:40h[18,17,8] = 111b */
266 reg32 |= (1 << 18) | (1 << 17) | (1 << 8);
267 /* D20:F0:40h[21,20,19] = 110b to enable XHCI Idle L1 */
268 reg32 &= ~(1 << 19);
269 reg32 |= (1 << 21) | (1 << 20);
270 } else {
271 /* D20:F0:40h[21,20,18,17,8] = 11111b */
272 reg32 |= (1 << 21)|(1 << 20)|(1 << 18)|(1 << 17)|(1 << 8);
273 }
274
275 /* Avoid writing upper byte as it is write-once */
276 pci_write_config16(dev, 0x40, (u16)(reg32 & 0xffff));
277 pci_write_config8(dev, 0x40 + 2, (u8)((reg32 >> 16) & 0xff));
278
279 /* D20:F0:44h[9,7,3] = 111b */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700280 reg16 = pci_read_config16(dev, 0x44);
281 reg16 |= (1 << 9) | (1 << 7) | (1 << 3);
282 pci_write_config16(dev, 0x44, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700283
284 reg32 = pci_read_config32(dev, 0xa0);
285 if (pch_is_lp()) {
286 /* D20:F0:A0h[18] = 1 */
287 reg32 |= (1 << 18);
288 } else {
289 /* D20:F0:A0h[6] = 1 */
290 reg32 |= (1 << 6);
291 }
292 pci_write_config32(dev, 0xa0, reg32);
293
294 /* D20:F0:A4h[13] = 0 */
295 reg32 = pci_read_config32(dev, 0xa4);
296 reg32 &= ~(1 << 13);
297 pci_write_config32(dev, 0xa4, reg32);
298}
299
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100300static void usb_xhci_init(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700301{
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700302 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700303 u16 reg16;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800304 u8 *mem_base = usb_xhci_mem_base(dev);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700305 config_t *config = dev->chip_info;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700306
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700307 /* D20:F0:74h[1:0] = 00b (set D0 state) */
308 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
309 reg16 &= ~PWR_CTL_SET_MASK;
310 reg16 |= PWR_CTL_SET_D0;
311 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700312
313 /* Enable clock gating first */
314 usb_xhci_clock_gating(dev);
315
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700316 reg32 = read32(mem_base + 0x8144);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700317 if (pch_is_lp()) {
318 /* XHCIBAR + 8144h[8,7,6] = 111b */
319 reg32 |= (1 << 8) | (1 << 7) | (1 << 6);
320 } else {
321 /* XHCIBAR + 8144h[8,7,6] = 100b */
322 reg32 &= ~((1 << 7) | (1 << 6));
323 reg32 |= (1 << 8);
324 }
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700325 write32(mem_base + 0x8144, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700326
327 if (pch_is_lp()) {
Duncan Laurie527b03a2013-08-28 09:53:50 -0700328 /* XHCIBAR + 816Ch[19:0] = 000e0038h */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700329 reg32 = read32(mem_base + 0x816c);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700330 reg32 &= ~0x000fffff;
Duncan Laurie527b03a2013-08-28 09:53:50 -0700331 reg32 |= 0x000e0038;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700332 write32(mem_base + 0x816c, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700333
334 /* D20:F0:B0h[17,14,13] = 100b */
335 reg32 = pci_read_config32(dev, 0xb0);
336 reg32 &= ~((1 << 14) | (1 << 13));
337 reg32 |= (1 << 17);
338 pci_write_config32(dev, 0xb0, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700339 }
340
341 reg32 = pci_read_config32(dev, 0x50);
342 if (pch_is_lp()) {
343 /* D20:F0:50h[28:0] = 0FCE2E5Fh */
344 reg32 &= ~0x1fffffff;
345 reg32 |= 0x0fce2e5f;
346 } else {
347 /* D20:F0:50h[26:0] = 07886E9Fh */
348 reg32 &= ~0x07ffffff;
349 reg32 |= 0x07886e9f;
350 }
351 pci_write_config32(dev, 0x50, reg32);
352
353 /* D20:F0:44h[31] = 1 (Access Control Bit) */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700354 reg32 = pci_read_config32(dev, 0x44);
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700355 reg32 |= (1UL << 31);
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700356 pci_write_config32(dev, 0x44, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700357
358 /* D20:F0:40h[31,23] = 10b (OC Configuration Done) */
359 reg32 = pci_read_config32(dev, 0x40);
360 reg32 &= ~(1 << 23); /* unsupported request */
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700361 reg32 |= (1UL << 31);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700362 pci_write_config32(dev, 0x40, reg32);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700363
364 if (acpi_is_wakeup_s3()) {
365 /* Reset ports that are disabled or
366 * polling before returning to the OS. */
367 usb_xhci_reset_usb3(dev, 0);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700368 } else if (config->xhci_default) {
369 /* Route all ports to XHCI */
370 outb(0xca, 0xb2);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700371 }
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700372}
373
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100374static void usb_xhci_set_subsystem(struct device *dev, unsigned int vendor,
375 unsigned int device)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700376{
377 if (!vendor || !device) {
378 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
379 pci_read_config32(dev, PCI_VENDOR_ID));
380 } else {
381 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
382 ((device & 0xffff) << 16) | (vendor & 0xffff));
383 }
384}
385
386static struct pci_operations lops_pci = {
387 .set_subsystem = &usb_xhci_set_subsystem,
388};
389
390static struct device_operations usb_xhci_ops = {
391 .read_resources = pci_dev_read_resources,
392 .set_resources = pci_dev_set_resources,
393 .enable_resources = pci_dev_enable_resources,
394 .init = usb_xhci_init,
395 .ops_pci = &lops_pci,
396};
397
398static const unsigned short pci_device_ids[] = { 0x8c31, /* LynxPoint-H */
399 0x9c31, /* LynxPoint-LP */
400 0 };
401
402static const struct pci_driver pch_usb_xhci __pci_driver = {
403 .ops = &usb_xhci_ops,
404 .vendor = PCI_VENDOR_ID_INTEL,
405 .devices = pci_device_ids,
406};
Duncan Laurie1f529082013-07-30 15:53:45 -0700407#endif /* !__SMM__ */