blob: 1219bccf8c7ec361dae0d2bc7b5ccb787202f84f [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
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010014#ifdef __SIMPLE_DEVICE__
15static u8 *usb_xhci_mem_base(pci_devfn_t dev)
16#else
17static u8 *usb_xhci_mem_base(struct device *dev)
18#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070019{
20 u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
21
22 /* Check if the controller is disabled or not present */
23 if (mem_base == 0 || mem_base == 0xffffffff)
24 return 0;
25
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080026 return (u8 *)(mem_base & ~0xf);
Duncan Laurie1f529082013-07-30 15:53:45 -070027}
28
Angel Pons418190c2021-03-03 17:30:52 +010029static int usb_xhci_port_count_usb3(u8 *mem_base)
Duncan Laurie1f529082013-07-30 15:53:45 -070030{
Angel Pons418190c2021-03-03 17:30:52 +010031 if (!mem_base) {
32 /* Do not proceed if BAR is invalid */
33 return 0;
34 }
35
Duncan Laurie1f529082013-07-30 15:53:45 -070036 if (pch_is_lp()) {
37 /* LynxPoint-LP has 4 SS ports */
38 return 4;
Duncan Laurie1f529082013-07-30 15:53:45 -070039 }
Angel Pons418190c2021-03-03 17:30:52 +010040
41 /* LynxPoint-H can have 0, 2, 4, or 6 SS ports */
Elyes HAOUAS54f94242018-10-25 10:57:39 +020042 u32 fus = read32(mem_base + XHCI_USB3FUS);
43 fus >>= XHCI_USB3FUS_SS_SHIFT;
44 fus &= XHCI_USB3FUS_SS_MASK;
45 switch (fus) {
46 case 3: return 0;
47 case 2: return 2;
48 case 1: return 4;
49 case 0:
50 default: return 6;
51 }
Duncan Laurie1f529082013-07-30 15:53:45 -070052}
53
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080054static void usb_xhci_reset_status_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070055{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080056 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -070057 u32 status = read32(portsc);
58 /* Do not set Port Enabled/Disabled field */
59 status &= ~XHCI_USB3_PORTSC_PED;
60 /* Clear all change status bits */
61 status |= XHCI_USB3_PORTSC_CHST;
62 write32(portsc, status);
Duncan Laurie1f529082013-07-30 15:53:45 -070063}
64
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080065static void usb_xhci_reset_port_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 Laurie1f529082013-07-30 15:53:45 -070068 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
69}
70
71#define XHCI_RESET_DELAY_US 1000 /* 1ms */
72#define XHCI_RESET_TIMEOUT 100 /* 100ms */
73
74/*
75 * 1) Wait until port is done polling
76 * 2) If port is disconnected
77 * a) Issue warm port reset
78 * b) Poll for warm reset complete
79 * c) Write 1 to port change status bits
80 */
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010081#ifdef __SIMPLE_DEVICE__
82static void usb_xhci_reset_usb3(pci_devfn_t dev, int all)
83#else
84static void usb_xhci_reset_usb3(struct device *dev, int all)
85#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070086{
87 u32 status, port_disabled;
88 int timeout, port;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080089 u8 *mem_base = usb_xhci_mem_base(dev);
Angel Pons418190c2021-03-03 17:30:52 +010090 int port_count = usb_xhci_port_count_usb3(mem_base);
Duncan Laurie1f529082013-07-30 15:53:45 -070091
Angel Pons418190c2021-03-03 17:30:52 +010092 if (!port_count)
Duncan Laurie1f529082013-07-30 15:53:45 -070093 return;
94
95 /* Get mask of disabled ports */
96 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
97
98 /* Wait until all enabled ports are done polling */
99 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
100 int complete = 1;
101 for (port = 0; port < port_count; port++) {
102 /* Skip disabled ports */
103 if (port_disabled & (1 << port))
104 continue;
105 /* Read port link status field */
106 status = read32(mem_base + XHCI_USB3_PORTSC(port));
107 status &= XHCI_USB3_PORTSC_PLS;
108 if (status == XHCI_PLSR_POLLING)
109 complete = 0;
110 }
111 /* Exit if all ports not polling */
112 if (complete)
113 break;
114 udelay(XHCI_RESET_DELAY_US);
115 }
116
117 /* Reset all requested ports */
118 for (port = 0; port < port_count; port++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800119 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -0700120 /* Skip disabled ports */
121 if (port_disabled & (1 << port))
122 continue;
123 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
124 /* Reset all or only disconnected ports */
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700125 if (all || (status == XHCI_PLSR_RXDETECT ||
126 status == XHCI_PLSR_POLLING))
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700127 usb_xhci_reset_port_usb3(mem_base, port);
128 else
129 port_disabled |= 1 << port;
Duncan Laurie1f529082013-07-30 15:53:45 -0700130 }
131
132 /* Wait for warm reset complete on all reset ports */
133 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
134 int complete = 1;
135 for (port = 0; port < port_count; port++) {
136 /* Only check ports that were reset */
137 if (port_disabled & (1 << port))
138 continue;
139 /* Check if warm reset is complete */
140 status = read32(mem_base + XHCI_USB3_PORTSC(port));
141 if (!(status & XHCI_USB3_PORTSC_WRC))
142 complete = 0;
143 }
144 /* Check for warm reset complete in any port */
145 if (complete)
146 break;
147 udelay(XHCI_RESET_DELAY_US);
148 }
149
150 /* Clear port change status bits */
151 for (port = 0; port < port_count; port++)
152 usb_xhci_reset_status_usb3(mem_base, port);
153}
154
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200155#ifdef __SIMPLE_DEVICE__
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700156
Duncan Laurie1f529082013-07-30 15:53:45 -0700157/* Handler for XHCI controller on entry to S3/S4/S5 */
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100158void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Laurie1f529082013-07-30 15:53:45 -0700159{
Duncan Laurie1f529082013-07-30 15:53:45 -0700160 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800161 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -0700162
Aaron Durbinda5f5092016-07-13 23:23:16 -0500163 if (!mem_base || slp_typ < ACPI_S3)
Duncan Laurie1f529082013-07-30 15:53:45 -0700164 return;
165
166 if (pch_is_lp()) {
167 /* Set D0 state */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200168 pci_update_config16(dev, XHCI_PWR_CTL_STS, ~PWR_CTL_SET_MASK, PWR_CTL_SET_D0);
Duncan Laurie1f529082013-07-30 15:53:45 -0700169
170 /* Clear PCI 0xB0[14:13] */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200171 pci_and_config32(dev, 0xb0, ~((1 << 14) | (1 << 13)));
Duncan Laurie1f529082013-07-30 15:53:45 -0700172
173 /* Clear MMIO 0x816c[14,2] */
174 reg32 = read32(mem_base + 0x816c);
175 reg32 &= ~((1 << 14) | (1 << 2));
176 write32(mem_base + 0x816c, reg32);
177
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700178 /* Reset disconnected USB3 ports */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700179 usb_xhci_reset_usb3(dev, 0);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700180
Duncan Laurie1f529082013-07-30 15:53:45 -0700181 /* Set MMIO 0x80e0[15] */
182 reg32 = read32(mem_base + 0x80e0);
183 reg32 |= (1 << 15);
184 write32(mem_base + 0x80e0, reg32);
185 }
186
187 /* Set D3Hot state and enable PME */
188 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_SET_D3);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700189 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_STATUS_PME);
Duncan Laurie1f529082013-07-30 15:53:45 -0700190 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_ENABLE_PME);
191}
192
Duncan Laurie911cedf2013-07-30 16:05:55 -0700193/* Route all ports to XHCI controller */
194void usb_xhci_route_all(void)
195{
196 u32 port_mask, route;
Duncan Laurie911cedf2013-07-30 16:05:55 -0700197
198 /* Skip if EHCI is already disabled */
199 if (RCBA32(FD) & PCH_DISABLE_EHCI1)
200 return;
201
202 /* Set D0 state */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200203 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 -0700204
205 /* Set USB3 superspeed enable */
206 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PRM);
207 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PR);
208 route &= ~XHCI_USB3PR_SSEN;
209 route |= XHCI_USB3PR_SSEN & port_mask;
210 pci_write_config32(PCH_XHCI_DEV, XHCI_USB3PR, route);
211
212 /* Route USB2 ports to XHCI controller */
213 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PRM);
214 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PR);
215 route &= ~XHCI_USB2PR_HCSEL;
216 route |= XHCI_USB2PR_HCSEL & port_mask;
217 pci_write_config32(PCH_XHCI_DEV, XHCI_USB2PR, route);
218
219 /* Disable EHCI controller */
220 usb_ehci_disable(PCH_EHCI1_DEV);
221
222 /* LynxPoint-H has a second EHCI controller */
223 if (!pch_is_lp())
224 usb_ehci_disable(PCH_EHCI2_DEV);
225
226 /* Reset and clear port change status */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700227 usb_xhci_reset_usb3(PCH_XHCI_DEV, 1);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700228}
229
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200230#else /* !__SIMPLE_DEVICE__ */
Duncan Laurie1f529082013-07-30 15:53:45 -0700231
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100232static void usb_xhci_clock_gating(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700233{
234 u32 reg32;
235
236 /* IOBP 0xE5004001[7:6] = 11b */
Angel Pons84fa2242020-10-24 11:53:47 +0200237 pch_iobp_update(0xe5004001, ~0, (1 << 7) | (1 << 6));
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700238
239 reg32 = pci_read_config32(dev, 0x40);
240 reg32 &= ~(1 << 23); /* unsupported request */
241
242 if (pch_is_lp()) {
243 /* D20:F0:40h[18,17,8] = 111b */
244 reg32 |= (1 << 18) | (1 << 17) | (1 << 8);
245 /* D20:F0:40h[21,20,19] = 110b to enable XHCI Idle L1 */
246 reg32 &= ~(1 << 19);
247 reg32 |= (1 << 21) | (1 << 20);
248 } else {
249 /* D20:F0:40h[21,20,18,17,8] = 11111b */
Angel Pons84fa2242020-10-24 11:53:47 +0200250 reg32 |= (1 << 21) | (1 << 20) | (1 << 18) | (1 << 17) | (1 << 8);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700251 }
252
253 /* Avoid writing upper byte as it is write-once */
254 pci_write_config16(dev, 0x40, (u16)(reg32 & 0xffff));
255 pci_write_config8(dev, 0x40 + 2, (u8)((reg32 >> 16) & 0xff));
256
257 /* D20:F0:44h[9,7,3] = 111b */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200258 pci_or_config16(dev, 0x44, (1 << 9) | (1 << 7) | (1 << 3));
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700259
260 reg32 = pci_read_config32(dev, 0xa0);
261 if (pch_is_lp()) {
262 /* D20:F0:A0h[18] = 1 */
263 reg32 |= (1 << 18);
264 } else {
265 /* D20:F0:A0h[6] = 1 */
266 reg32 |= (1 << 6);
267 }
268 pci_write_config32(dev, 0xa0, reg32);
269
270 /* D20:F0:A4h[13] = 0 */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200271 pci_and_config32(dev, 0xa4, ~(1 << 13));
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700272}
273
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100274static void usb_xhci_init(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700275{
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700276 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800277 u8 *mem_base = usb_xhci_mem_base(dev);
Angel Ponsefebedd2021-09-08 16:16:58 +0200278 struct southbridge_intel_lynxpoint_config *config = dev->chip_info;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700279
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700280 /* D20:F0:74h[1:0] = 00b (set D0 state) */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200281 pci_update_config16(dev, XHCI_PWR_CTL_STS, ~PWR_CTL_SET_MASK, PWR_CTL_SET_D0);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700282
283 /* Enable clock gating first */
284 usb_xhci_clock_gating(dev);
285
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700286 reg32 = read32(mem_base + 0x8144);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700287 if (pch_is_lp()) {
288 /* XHCIBAR + 8144h[8,7,6] = 111b */
289 reg32 |= (1 << 8) | (1 << 7) | (1 << 6);
290 } else {
291 /* XHCIBAR + 8144h[8,7,6] = 100b */
292 reg32 &= ~((1 << 7) | (1 << 6));
293 reg32 |= (1 << 8);
294 }
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700295 write32(mem_base + 0x8144, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700296
297 if (pch_is_lp()) {
Duncan Laurie527b03a2013-08-28 09:53:50 -0700298 /* XHCIBAR + 816Ch[19:0] = 000e0038h */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700299 reg32 = read32(mem_base + 0x816c);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700300 reg32 &= ~0x000fffff;
Duncan Laurie527b03a2013-08-28 09:53:50 -0700301 reg32 |= 0x000e0038;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700302 write32(mem_base + 0x816c, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700303
304 /* D20:F0:B0h[17,14,13] = 100b */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200305 pci_update_config32(dev, 0xb0, ~((1 << 14) | (1 << 13)), 1 << 17);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700306 }
307
308 reg32 = pci_read_config32(dev, 0x50);
309 if (pch_is_lp()) {
310 /* D20:F0:50h[28:0] = 0FCE2E5Fh */
311 reg32 &= ~0x1fffffff;
312 reg32 |= 0x0fce2e5f;
313 } else {
314 /* D20:F0:50h[26:0] = 07886E9Fh */
315 reg32 &= ~0x07ffffff;
316 reg32 |= 0x07886e9f;
317 }
318 pci_write_config32(dev, 0x50, reg32);
319
320 /* D20:F0:44h[31] = 1 (Access Control Bit) */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200321 pci_or_config32(dev, 0x44, 1 << 31);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700322
323 /* D20:F0:40h[31,23] = 10b (OC Configuration Done) */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200324 pci_update_config32(dev, 0x40, ~(1 << 23), 1 << 31); /* unsupported request */
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700325
326 if (acpi_is_wakeup_s3()) {
327 /* Reset ports that are disabled or
328 * polling before returning to the OS. */
329 usb_xhci_reset_usb3(dev, 0);
Angel Ponscbcbb672020-10-23 00:11:26 +0200330 } else if (config && config->xhci_default) {
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700331 /* Route all ports to XHCI */
Kyösti Mälkkib486f292020-06-18 14:05:35 +0300332 apm_control(APM_CNT_ROUTE_ALL_XHCI);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700333 }
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700334}
335
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700336static struct device_operations usb_xhci_ops = {
337 .read_resources = pci_dev_read_resources,
338 .set_resources = pci_dev_set_resources,
339 .enable_resources = pci_dev_enable_resources,
340 .init = usb_xhci_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200341 .ops_pci = &pci_dev_ops_pci,
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700342};
343
Felix Singer4ea08f92020-11-20 12:56:44 +0000344static const unsigned short pci_device_ids[] = {
Felix Singer43b7f412022-03-07 04:34:52 +0100345 PCI_DID_INTEL_LPT_H_XHCI,
346 PCI_DID_INTEL_LPT_LP_XHCI,
Felix Singer4ea08f92020-11-20 12:56:44 +0000347 0
348};
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700349
350static const struct pci_driver pch_usb_xhci __pci_driver = {
351 .ops = &usb_xhci_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100352 .vendor = PCI_VID_INTEL,
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700353 .devices = pci_device_ids,
354};
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200355#endif /* !__SIMPLE_DEVICE__ */