blob: ce851a30094223a0dc60d8977ccc66ad5c523121 [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
Angel Pons418190c2021-03-03 17:30:52 +010031static int usb_xhci_port_count_usb3(u8 *mem_base)
Duncan Laurie1f529082013-07-30 15:53:45 -070032{
Angel Pons418190c2021-03-03 17:30:52 +010033 if (!mem_base) {
34 /* Do not proceed if BAR is invalid */
35 return 0;
36 }
37
Duncan Laurie1f529082013-07-30 15:53:45 -070038 if (pch_is_lp()) {
39 /* LynxPoint-LP has 4 SS ports */
40 return 4;
Duncan Laurie1f529082013-07-30 15:53:45 -070041 }
Angel Pons418190c2021-03-03 17:30:52 +010042
43 /* LynxPoint-H can have 0, 2, 4, or 6 SS ports */
Elyes HAOUAS54f94242018-10-25 10:57:39 +020044 u32 fus = read32(mem_base + XHCI_USB3FUS);
45 fus >>= XHCI_USB3FUS_SS_SHIFT;
46 fus &= XHCI_USB3FUS_SS_MASK;
47 switch (fus) {
48 case 3: return 0;
49 case 2: return 2;
50 case 1: return 4;
51 case 0:
52 default: return 6;
53 }
Duncan Laurie1f529082013-07-30 15:53:45 -070054}
55
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080056static void usb_xhci_reset_status_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070057{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080058 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -070059 u32 status = read32(portsc);
60 /* Do not set Port Enabled/Disabled field */
61 status &= ~XHCI_USB3_PORTSC_PED;
62 /* Clear all change status bits */
63 status |= XHCI_USB3_PORTSC_CHST;
64 write32(portsc, status);
Duncan Laurie1f529082013-07-30 15:53:45 -070065}
66
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080067static void usb_xhci_reset_port_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 Laurie1f529082013-07-30 15:53:45 -070070 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
71}
72
73#define XHCI_RESET_DELAY_US 1000 /* 1ms */
74#define XHCI_RESET_TIMEOUT 100 /* 100ms */
75
76/*
77 * 1) Wait until port is done polling
78 * 2) If port is disconnected
79 * a) Issue warm port reset
80 * b) Poll for warm reset complete
81 * c) Write 1 to port change status bits
82 */
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010083#ifdef __SIMPLE_DEVICE__
84static void usb_xhci_reset_usb3(pci_devfn_t dev, int all)
85#else
86static void usb_xhci_reset_usb3(struct device *dev, int all)
87#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070088{
89 u32 status, port_disabled;
90 int timeout, port;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080091 u8 *mem_base = usb_xhci_mem_base(dev);
Angel Pons418190c2021-03-03 17:30:52 +010092 int port_count = usb_xhci_port_count_usb3(mem_base);
Duncan Laurie1f529082013-07-30 15:53:45 -070093
Angel Pons418190c2021-03-03 17:30:52 +010094 if (!port_count)
Duncan Laurie1f529082013-07-30 15:53:45 -070095 return;
96
97 /* Get mask of disabled ports */
98 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
99
100 /* Wait until all enabled ports are done polling */
101 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
102 int complete = 1;
103 for (port = 0; port < port_count; port++) {
104 /* Skip disabled ports */
105 if (port_disabled & (1 << port))
106 continue;
107 /* Read port link status field */
108 status = read32(mem_base + XHCI_USB3_PORTSC(port));
109 status &= XHCI_USB3_PORTSC_PLS;
110 if (status == XHCI_PLSR_POLLING)
111 complete = 0;
112 }
113 /* Exit if all ports not polling */
114 if (complete)
115 break;
116 udelay(XHCI_RESET_DELAY_US);
117 }
118
119 /* Reset all requested ports */
120 for (port = 0; port < port_count; port++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800121 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -0700122 /* Skip disabled ports */
123 if (port_disabled & (1 << port))
124 continue;
125 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
126 /* Reset all or only disconnected ports */
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700127 if (all || (status == XHCI_PLSR_RXDETECT ||
128 status == XHCI_PLSR_POLLING))
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700129 usb_xhci_reset_port_usb3(mem_base, port);
130 else
131 port_disabled |= 1 << port;
Duncan Laurie1f529082013-07-30 15:53:45 -0700132 }
133
134 /* Wait for warm reset complete on all reset ports */
135 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
136 int complete = 1;
137 for (port = 0; port < port_count; port++) {
138 /* Only check ports that were reset */
139 if (port_disabled & (1 << port))
140 continue;
141 /* Check if warm reset is complete */
142 status = read32(mem_base + XHCI_USB3_PORTSC(port));
143 if (!(status & XHCI_USB3_PORTSC_WRC))
144 complete = 0;
145 }
146 /* Check for warm reset complete in any port */
147 if (complete)
148 break;
149 udelay(XHCI_RESET_DELAY_US);
150 }
151
152 /* Clear port change status bits */
153 for (port = 0; port < port_count; port++)
154 usb_xhci_reset_status_usb3(mem_base, port);
155}
156
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200157#ifdef __SIMPLE_DEVICE__
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700158
Duncan Laurie1f529082013-07-30 15:53:45 -0700159/* Handler for XHCI controller on entry to S3/S4/S5 */
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100160void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Laurie1f529082013-07-30 15:53:45 -0700161{
Duncan Laurie1f529082013-07-30 15:53:45 -0700162 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800163 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -0700164
Aaron Durbinda5f5092016-07-13 23:23:16 -0500165 if (!mem_base || slp_typ < ACPI_S3)
Duncan Laurie1f529082013-07-30 15:53:45 -0700166 return;
167
168 if (pch_is_lp()) {
169 /* Set D0 state */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200170 pci_update_config16(dev, XHCI_PWR_CTL_STS, ~PWR_CTL_SET_MASK, PWR_CTL_SET_D0);
Duncan Laurie1f529082013-07-30 15:53:45 -0700171
172 /* Clear PCI 0xB0[14:13] */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200173 pci_and_config32(dev, 0xb0, ~((1 << 14) | (1 << 13)));
Duncan Laurie1f529082013-07-30 15:53:45 -0700174
175 /* Clear MMIO 0x816c[14,2] */
176 reg32 = read32(mem_base + 0x816c);
177 reg32 &= ~((1 << 14) | (1 << 2));
178 write32(mem_base + 0x816c, reg32);
179
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700180 /* Reset disconnected USB3 ports */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700181 usb_xhci_reset_usb3(dev, 0);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700182
Duncan Laurie1f529082013-07-30 15:53:45 -0700183 /* Set MMIO 0x80e0[15] */
184 reg32 = read32(mem_base + 0x80e0);
185 reg32 |= (1 << 15);
186 write32(mem_base + 0x80e0, reg32);
187 }
188
189 /* Set D3Hot state and enable PME */
190 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_SET_D3);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700191 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_STATUS_PME);
Duncan Laurie1f529082013-07-30 15:53:45 -0700192 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_ENABLE_PME);
193}
194
Duncan Laurie911cedf2013-07-30 16:05:55 -0700195/* Route all ports to XHCI controller */
196void usb_xhci_route_all(void)
197{
198 u32 port_mask, route;
Duncan Laurie911cedf2013-07-30 16:05:55 -0700199
200 /* Skip if EHCI is already disabled */
201 if (RCBA32(FD) & PCH_DISABLE_EHCI1)
202 return;
203
204 /* Set D0 state */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200205 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 -0700206
207 /* Set USB3 superspeed enable */
208 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PRM);
209 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PR);
210 route &= ~XHCI_USB3PR_SSEN;
211 route |= XHCI_USB3PR_SSEN & port_mask;
212 pci_write_config32(PCH_XHCI_DEV, XHCI_USB3PR, route);
213
214 /* Route USB2 ports to XHCI controller */
215 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PRM);
216 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PR);
217 route &= ~XHCI_USB2PR_HCSEL;
218 route |= XHCI_USB2PR_HCSEL & port_mask;
219 pci_write_config32(PCH_XHCI_DEV, XHCI_USB2PR, route);
220
221 /* Disable EHCI controller */
222 usb_ehci_disable(PCH_EHCI1_DEV);
223
224 /* LynxPoint-H has a second EHCI controller */
225 if (!pch_is_lp())
226 usb_ehci_disable(PCH_EHCI2_DEV);
227
228 /* Reset and clear port change status */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700229 usb_xhci_reset_usb3(PCH_XHCI_DEV, 1);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700230}
231
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200232#else /* !__SIMPLE_DEVICE__ */
Duncan Laurie1f529082013-07-30 15:53:45 -0700233
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100234static void usb_xhci_clock_gating(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700235{
236 u32 reg32;
237
238 /* IOBP 0xE5004001[7:6] = 11b */
Angel Pons84fa2242020-10-24 11:53:47 +0200239 pch_iobp_update(0xe5004001, ~0, (1 << 7) | (1 << 6));
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700240
241 reg32 = pci_read_config32(dev, 0x40);
242 reg32 &= ~(1 << 23); /* unsupported request */
243
244 if (pch_is_lp()) {
245 /* D20:F0:40h[18,17,8] = 111b */
246 reg32 |= (1 << 18) | (1 << 17) | (1 << 8);
247 /* D20:F0:40h[21,20,19] = 110b to enable XHCI Idle L1 */
248 reg32 &= ~(1 << 19);
249 reg32 |= (1 << 21) | (1 << 20);
250 } else {
251 /* D20:F0:40h[21,20,18,17,8] = 11111b */
Angel Pons84fa2242020-10-24 11:53:47 +0200252 reg32 |= (1 << 21) | (1 << 20) | (1 << 18) | (1 << 17) | (1 << 8);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700253 }
254
255 /* Avoid writing upper byte as it is write-once */
256 pci_write_config16(dev, 0x40, (u16)(reg32 & 0xffff));
257 pci_write_config8(dev, 0x40 + 2, (u8)((reg32 >> 16) & 0xff));
258
259 /* D20:F0:44h[9,7,3] = 111b */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200260 pci_or_config16(dev, 0x44, (1 << 9) | (1 << 7) | (1 << 3));
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700261
262 reg32 = pci_read_config32(dev, 0xa0);
263 if (pch_is_lp()) {
264 /* D20:F0:A0h[18] = 1 */
265 reg32 |= (1 << 18);
266 } else {
267 /* D20:F0:A0h[6] = 1 */
268 reg32 |= (1 << 6);
269 }
270 pci_write_config32(dev, 0xa0, reg32);
271
272 /* D20:F0:A4h[13] = 0 */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200273 pci_and_config32(dev, 0xa4, ~(1 << 13));
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700274}
275
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100276static void usb_xhci_init(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700277{
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700278 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800279 u8 *mem_base = usb_xhci_mem_base(dev);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700280 config_t *config = dev->chip_info;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700281
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700282 /* D20:F0:74h[1:0] = 00b (set D0 state) */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200283 pci_update_config16(dev, XHCI_PWR_CTL_STS, ~PWR_CTL_SET_MASK, PWR_CTL_SET_D0);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700284
285 /* Enable clock gating first */
286 usb_xhci_clock_gating(dev);
287
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700288 reg32 = read32(mem_base + 0x8144);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700289 if (pch_is_lp()) {
290 /* XHCIBAR + 8144h[8,7,6] = 111b */
291 reg32 |= (1 << 8) | (1 << 7) | (1 << 6);
292 } else {
293 /* XHCIBAR + 8144h[8,7,6] = 100b */
294 reg32 &= ~((1 << 7) | (1 << 6));
295 reg32 |= (1 << 8);
296 }
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700297 write32(mem_base + 0x8144, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700298
299 if (pch_is_lp()) {
Duncan Laurie527b03a2013-08-28 09:53:50 -0700300 /* XHCIBAR + 816Ch[19:0] = 000e0038h */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700301 reg32 = read32(mem_base + 0x816c);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700302 reg32 &= ~0x000fffff;
Duncan Laurie527b03a2013-08-28 09:53:50 -0700303 reg32 |= 0x000e0038;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700304 write32(mem_base + 0x816c, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700305
306 /* D20:F0:B0h[17,14,13] = 100b */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200307 pci_update_config32(dev, 0xb0, ~((1 << 14) | (1 << 13)), 1 << 17);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700308 }
309
310 reg32 = pci_read_config32(dev, 0x50);
311 if (pch_is_lp()) {
312 /* D20:F0:50h[28:0] = 0FCE2E5Fh */
313 reg32 &= ~0x1fffffff;
314 reg32 |= 0x0fce2e5f;
315 } else {
316 /* D20:F0:50h[26:0] = 07886E9Fh */
317 reg32 &= ~0x07ffffff;
318 reg32 |= 0x07886e9f;
319 }
320 pci_write_config32(dev, 0x50, reg32);
321
322 /* D20:F0:44h[31] = 1 (Access Control Bit) */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200323 pci_or_config32(dev, 0x44, 1 << 31);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700324
325 /* D20:F0:40h[31,23] = 10b (OC Configuration Done) */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200326 pci_update_config32(dev, 0x40, ~(1 << 23), 1 << 31); /* unsupported request */
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700327
328 if (acpi_is_wakeup_s3()) {
329 /* Reset ports that are disabled or
330 * polling before returning to the OS. */
331 usb_xhci_reset_usb3(dev, 0);
Angel Ponscbcbb672020-10-23 00:11:26 +0200332 } else if (config && config->xhci_default) {
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700333 /* Route all ports to XHCI */
Kyösti Mälkkib486f292020-06-18 14:05:35 +0300334 apm_control(APM_CNT_ROUTE_ALL_XHCI);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700335 }
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700336}
337
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700338static struct device_operations usb_xhci_ops = {
339 .read_resources = pci_dev_read_resources,
340 .set_resources = pci_dev_set_resources,
341 .enable_resources = pci_dev_enable_resources,
342 .init = usb_xhci_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200343 .ops_pci = &pci_dev_ops_pci,
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700344};
345
Felix Singer4ea08f92020-11-20 12:56:44 +0000346static const unsigned short pci_device_ids[] = {
347 PCI_DEVICE_ID_INTEL_LPT_H_XHCI,
348 PCI_DEVICE_ID_INTEL_LPT_LP_XHCI,
349 0
350};
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700351
352static const struct pci_driver pch_usb_xhci __pci_driver = {
353 .ops = &usb_xhci_ops,
354 .vendor = PCI_VENDOR_ID_INTEL,
355 .devices = pci_device_ids,
356};
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200357#endif /* !__SIMPLE_DEVICE__ */