blob: a6c9eb1fc12d709dc9b719747a2ceb34a6902c1a [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Laurie2d9d39a2013-05-29 15:27:55 -07002
Kyösti Mälkkib486f292020-06-18 14:05:35 +03003#include <cpu/x86/smm.h>
Duncan Laurie1f529082013-07-30 15:53:45 -07004#include <delay.h>
Duncan Laurie2d9d39a2013-05-29 15:27:55 -07005#include <device/device.h>
6#include <device/pci.h>
7#include <device/pci_ids.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030010#include "chip.h"
Angel Pons2178b722020-05-31 00:55:35 +020011#include "iobp.h"
Duncan Laurie2d9d39a2013-05-29 15:27:55 -070012#include "pch.h"
13
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -070014typedef struct southbridge_intel_lynxpoint_config config_t;
15
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010016#ifdef __SIMPLE_DEVICE__
17static u8 *usb_xhci_mem_base(pci_devfn_t dev)
18#else
19static u8 *usb_xhci_mem_base(struct device *dev)
20#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070021{
22 u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
23
24 /* Check if the controller is disabled or not present */
25 if (mem_base == 0 || mem_base == 0xffffffff)
26 return 0;
27
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080028 return (u8 *)(mem_base & ~0xf);
Duncan Laurie1f529082013-07-30 15:53:45 -070029}
30
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010031#ifdef __SIMPLE_DEVICE__
32static int usb_xhci_port_count_usb3(pci_devfn_t dev)
33#else
34static int usb_xhci_port_count_usb3(struct device *dev)
35#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070036{
37 if (pch_is_lp()) {
38 /* LynxPoint-LP has 4 SS ports */
39 return 4;
Duncan Laurie1f529082013-07-30 15:53:45 -070040 }
Elyes HAOUAS54f94242018-10-25 10:57:39 +020041 /* LynxPoint-H can have 0, 2, 4, or 6 SS ports */
42 u8 *mem_base = usb_xhci_mem_base(dev);
43 u32 fus = read32(mem_base + XHCI_USB3FUS);
44 fus >>= XHCI_USB3FUS_SS_SHIFT;
45 fus &= XHCI_USB3FUS_SS_MASK;
46 switch (fus) {
47 case 3: return 0;
48 case 2: return 2;
49 case 1: return 4;
50 case 0:
51 default: return 6;
52 }
Duncan Laurie1f529082013-07-30 15:53:45 -070053}
54
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080055static void usb_xhci_reset_status_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070056{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080057 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -070058 u32 status = read32(portsc);
59 /* Do not set Port Enabled/Disabled field */
60 status &= ~XHCI_USB3_PORTSC_PED;
61 /* Clear all change status bits */
62 status |= XHCI_USB3_PORTSC_CHST;
63 write32(portsc, status);
Duncan Laurie1f529082013-07-30 15:53:45 -070064}
65
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080066static void usb_xhci_reset_port_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 Laurie1f529082013-07-30 15:53:45 -070069 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
70}
71
72#define XHCI_RESET_DELAY_US 1000 /* 1ms */
73#define XHCI_RESET_TIMEOUT 100 /* 100ms */
74
75/*
76 * 1) Wait until port is done polling
77 * 2) If port is disconnected
78 * a) Issue warm port reset
79 * b) Poll for warm reset complete
80 * c) Write 1 to port change status bits
81 */
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010082#ifdef __SIMPLE_DEVICE__
83static void usb_xhci_reset_usb3(pci_devfn_t dev, int all)
84#else
85static void usb_xhci_reset_usb3(struct device *dev, int all)
86#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070087{
88 u32 status, port_disabled;
89 int timeout, port;
90 int port_count = usb_xhci_port_count_usb3(dev);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080091 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -070092
93 if (!mem_base || !port_count)
94 return;
95
96 /* Get mask of disabled ports */
97 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
98
99 /* Wait until all enabled ports are done polling */
100 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
101 int complete = 1;
102 for (port = 0; port < port_count; port++) {
103 /* Skip disabled ports */
104 if (port_disabled & (1 << port))
105 continue;
106 /* Read port link status field */
107 status = read32(mem_base + XHCI_USB3_PORTSC(port));
108 status &= XHCI_USB3_PORTSC_PLS;
109 if (status == XHCI_PLSR_POLLING)
110 complete = 0;
111 }
112 /* Exit if all ports not polling */
113 if (complete)
114 break;
115 udelay(XHCI_RESET_DELAY_US);
116 }
117
118 /* Reset all requested ports */
119 for (port = 0; port < port_count; port++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800120 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -0700121 /* Skip disabled ports */
122 if (port_disabled & (1 << port))
123 continue;
124 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
125 /* Reset all or only disconnected ports */
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700126 if (all || (status == XHCI_PLSR_RXDETECT ||
127 status == XHCI_PLSR_POLLING))
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700128 usb_xhci_reset_port_usb3(mem_base, port);
129 else
130 port_disabled |= 1 << port;
Duncan Laurie1f529082013-07-30 15:53:45 -0700131 }
132
133 /* Wait for warm reset complete on all reset ports */
134 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
135 int complete = 1;
136 for (port = 0; port < port_count; port++) {
137 /* Only check ports that were reset */
138 if (port_disabled & (1 << port))
139 continue;
140 /* Check if warm reset is complete */
141 status = read32(mem_base + XHCI_USB3_PORTSC(port));
142 if (!(status & XHCI_USB3_PORTSC_WRC))
143 complete = 0;
144 }
145 /* Check for warm reset complete in any port */
146 if (complete)
147 break;
148 udelay(XHCI_RESET_DELAY_US);
149 }
150
151 /* Clear port change status bits */
152 for (port = 0; port < port_count; port++)
153 usb_xhci_reset_status_usb3(mem_base, port);
154}
155
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200156#ifdef __SIMPLE_DEVICE__
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700157
Duncan Laurie1f529082013-07-30 15:53:45 -0700158/* Handler for XHCI controller on entry to S3/S4/S5 */
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100159void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Laurie1f529082013-07-30 15:53:45 -0700160{
Duncan Laurie1f529082013-07-30 15:53:45 -0700161 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800162 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -0700163
Aaron Durbinda5f5092016-07-13 23:23:16 -0500164 if (!mem_base || slp_typ < ACPI_S3)
Duncan Laurie1f529082013-07-30 15:53:45 -0700165 return;
166
167 if (pch_is_lp()) {
168 /* Set D0 state */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200169 pci_update_config16(dev, XHCI_PWR_CTL_STS, ~PWR_CTL_SET_MASK, PWR_CTL_SET_D0);
Duncan Laurie1f529082013-07-30 15:53:45 -0700170
171 /* Clear PCI 0xB0[14:13] */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200172 pci_and_config32(dev, 0xb0, ~((1 << 14) | (1 << 13)));
Duncan Laurie1f529082013-07-30 15:53:45 -0700173
174 /* Clear MMIO 0x816c[14,2] */
175 reg32 = read32(mem_base + 0x816c);
176 reg32 &= ~((1 << 14) | (1 << 2));
177 write32(mem_base + 0x816c, reg32);
178
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700179 /* Reset disconnected USB3 ports */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700180 usb_xhci_reset_usb3(dev, 0);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700181
Duncan Laurie1f529082013-07-30 15:53:45 -0700182 /* Set MMIO 0x80e0[15] */
183 reg32 = read32(mem_base + 0x80e0);
184 reg32 |= (1 << 15);
185 write32(mem_base + 0x80e0, reg32);
186 }
187
188 /* Set D3Hot state and enable PME */
189 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_SET_D3);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700190 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_STATUS_PME);
Duncan Laurie1f529082013-07-30 15:53:45 -0700191 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_ENABLE_PME);
192}
193
Duncan Laurie911cedf2013-07-30 16:05:55 -0700194/* Route all ports to XHCI controller */
195void usb_xhci_route_all(void)
196{
197 u32 port_mask, route;
Duncan Laurie911cedf2013-07-30 16:05:55 -0700198
199 /* Skip if EHCI is already disabled */
200 if (RCBA32(FD) & PCH_DISABLE_EHCI1)
201 return;
202
203 /* Set D0 state */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200204 pci_update_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS, ~PWR_CTL_SET_MASK, PWR_CTL_SET_D0);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700205
206 /* Set USB3 superspeed enable */
207 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PRM);
208 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PR);
209 route &= ~XHCI_USB3PR_SSEN;
210 route |= XHCI_USB3PR_SSEN & port_mask;
211 pci_write_config32(PCH_XHCI_DEV, XHCI_USB3PR, route);
212
213 /* Route USB2 ports to XHCI controller */
214 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PRM);
215 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PR);
216 route &= ~XHCI_USB2PR_HCSEL;
217 route |= XHCI_USB2PR_HCSEL & port_mask;
218 pci_write_config32(PCH_XHCI_DEV, XHCI_USB2PR, route);
219
220 /* Disable EHCI controller */
221 usb_ehci_disable(PCH_EHCI1_DEV);
222
223 /* LynxPoint-H has a second EHCI controller */
224 if (!pch_is_lp())
225 usb_ehci_disable(PCH_EHCI2_DEV);
226
227 /* Reset and clear port change status */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700228 usb_xhci_reset_usb3(PCH_XHCI_DEV, 1);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700229}
230
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200231#else /* !__SIMPLE_DEVICE__ */
Duncan Laurie1f529082013-07-30 15:53:45 -0700232
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100233static void usb_xhci_clock_gating(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700234{
235 u32 reg32;
236
237 /* IOBP 0xE5004001[7:6] = 11b */
Angel Pons84fa2242020-10-24 11:53:47 +0200238 pch_iobp_update(0xe5004001, ~0, (1 << 7) | (1 << 6));
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700239
240 reg32 = pci_read_config32(dev, 0x40);
241 reg32 &= ~(1 << 23); /* unsupported request */
242
243 if (pch_is_lp()) {
244 /* D20:F0:40h[18,17,8] = 111b */
245 reg32 |= (1 << 18) | (1 << 17) | (1 << 8);
246 /* D20:F0:40h[21,20,19] = 110b to enable XHCI Idle L1 */
247 reg32 &= ~(1 << 19);
248 reg32 |= (1 << 21) | (1 << 20);
249 } else {
250 /* D20:F0:40h[21,20,18,17,8] = 11111b */
Angel Pons84fa2242020-10-24 11:53:47 +0200251 reg32 |= (1 << 21) | (1 << 20) | (1 << 18) | (1 << 17) | (1 << 8);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700252 }
253
254 /* Avoid writing upper byte as it is write-once */
255 pci_write_config16(dev, 0x40, (u16)(reg32 & 0xffff));
256 pci_write_config8(dev, 0x40 + 2, (u8)((reg32 >> 16) & 0xff));
257
258 /* D20:F0:44h[9,7,3] = 111b */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200259 pci_or_config16(dev, 0x44, (1 << 9) | (1 << 7) | (1 << 3));
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700260
261 reg32 = pci_read_config32(dev, 0xa0);
262 if (pch_is_lp()) {
263 /* D20:F0:A0h[18] = 1 */
264 reg32 |= (1 << 18);
265 } else {
266 /* D20:F0:A0h[6] = 1 */
267 reg32 |= (1 << 6);
268 }
269 pci_write_config32(dev, 0xa0, reg32);
270
271 /* D20:F0:A4h[13] = 0 */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200272 pci_and_config32(dev, 0xa4, ~(1 << 13));
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700273}
274
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100275static void usb_xhci_init(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700276{
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700277 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800278 u8 *mem_base = usb_xhci_mem_base(dev);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700279 config_t *config = dev->chip_info;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700280
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700281 /* D20:F0:74h[1:0] = 00b (set D0 state) */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200282 pci_update_config16(dev, XHCI_PWR_CTL_STS, ~PWR_CTL_SET_MASK, PWR_CTL_SET_D0);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700283
284 /* Enable clock gating first */
285 usb_xhci_clock_gating(dev);
286
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700287 reg32 = read32(mem_base + 0x8144);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700288 if (pch_is_lp()) {
289 /* XHCIBAR + 8144h[8,7,6] = 111b */
290 reg32 |= (1 << 8) | (1 << 7) | (1 << 6);
291 } else {
292 /* XHCIBAR + 8144h[8,7,6] = 100b */
293 reg32 &= ~((1 << 7) | (1 << 6));
294 reg32 |= (1 << 8);
295 }
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700296 write32(mem_base + 0x8144, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700297
298 if (pch_is_lp()) {
Duncan Laurie527b03a2013-08-28 09:53:50 -0700299 /* XHCIBAR + 816Ch[19:0] = 000e0038h */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700300 reg32 = read32(mem_base + 0x816c);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700301 reg32 &= ~0x000fffff;
Duncan Laurie527b03a2013-08-28 09:53:50 -0700302 reg32 |= 0x000e0038;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700303 write32(mem_base + 0x816c, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700304
305 /* D20:F0:B0h[17,14,13] = 100b */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200306 pci_update_config32(dev, 0xb0, ~((1 << 14) | (1 << 13)), 1 << 17);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700307 }
308
309 reg32 = pci_read_config32(dev, 0x50);
310 if (pch_is_lp()) {
311 /* D20:F0:50h[28:0] = 0FCE2E5Fh */
312 reg32 &= ~0x1fffffff;
313 reg32 |= 0x0fce2e5f;
314 } else {
315 /* D20:F0:50h[26:0] = 07886E9Fh */
316 reg32 &= ~0x07ffffff;
317 reg32 |= 0x07886e9f;
318 }
319 pci_write_config32(dev, 0x50, reg32);
320
321 /* D20:F0:44h[31] = 1 (Access Control Bit) */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200322 pci_or_config32(dev, 0x44, 1 << 31);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700323
324 /* D20:F0:40h[31,23] = 10b (OC Configuration Done) */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200325 pci_update_config32(dev, 0x40, ~(1 << 23), 1 << 31); /* unsupported request */
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700326
327 if (acpi_is_wakeup_s3()) {
328 /* Reset ports that are disabled or
329 * polling before returning to the OS. */
330 usb_xhci_reset_usb3(dev, 0);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700331 } else if (config->xhci_default) {
332 /* Route all ports to XHCI */
Kyösti Mälkkib486f292020-06-18 14:05:35 +0300333 apm_control(APM_CNT_ROUTE_ALL_XHCI);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700334 }
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700335}
336
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700337static struct device_operations usb_xhci_ops = {
338 .read_resources = pci_dev_read_resources,
339 .set_resources = pci_dev_set_resources,
340 .enable_resources = pci_dev_enable_resources,
341 .init = usb_xhci_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200342 .ops_pci = &pci_dev_ops_pci,
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700343};
344
345static const unsigned short pci_device_ids[] = { 0x8c31, /* LynxPoint-H */
346 0x9c31, /* LynxPoint-LP */
347 0 };
348
349static const struct pci_driver pch_usb_xhci __pci_driver = {
350 .ops = &usb_xhci_ops,
351 .vendor = PCI_VENDOR_ID_INTEL,
352 .devices = pci_device_ids,
353};
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200354#endif /* !__SIMPLE_DEVICE__ */