blob: 4818d626f04fef9d0a1ba0646346692d5c331356 [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>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030024#include "chip.h"
Duncan Laurie2d9d39a2013-05-29 15:27:55 -070025#include "pch.h"
26
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -070027typedef struct southbridge_intel_lynxpoint_config config_t;
28
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010029#ifdef __SIMPLE_DEVICE__
30static u8 *usb_xhci_mem_base(pci_devfn_t dev)
31#else
32static u8 *usb_xhci_mem_base(struct device *dev)
33#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070034{
35 u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
36
37 /* Check if the controller is disabled or not present */
38 if (mem_base == 0 || mem_base == 0xffffffff)
39 return 0;
40
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080041 return (u8 *)(mem_base & ~0xf);
Duncan Laurie1f529082013-07-30 15:53:45 -070042}
43
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010044#ifdef __SIMPLE_DEVICE__
45static int usb_xhci_port_count_usb3(pci_devfn_t dev)
46#else
47static int usb_xhci_port_count_usb3(struct device *dev)
48#endif
Duncan Laurie1f529082013-07-30 15:53:45 -070049{
50 if (pch_is_lp()) {
51 /* LynxPoint-LP has 4 SS ports */
52 return 4;
Duncan Laurie1f529082013-07-30 15:53:45 -070053 }
Elyes HAOUAS54f94242018-10-25 10:57:39 +020054 /* LynxPoint-H can have 0, 2, 4, or 6 SS ports */
55 u8 *mem_base = usb_xhci_mem_base(dev);
56 u32 fus = read32(mem_base + XHCI_USB3FUS);
57 fus >>= XHCI_USB3FUS_SS_SHIFT;
58 fus &= XHCI_USB3FUS_SS_MASK;
59 switch (fus) {
60 case 3: return 0;
61 case 2: return 2;
62 case 1: return 4;
63 case 0:
64 default: return 6;
65 }
Duncan Laurie1f529082013-07-30 15:53:45 -070066}
67
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080068static void usb_xhci_reset_status_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070069{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080070 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -070071 u32 status = read32(portsc);
72 /* Do not set Port Enabled/Disabled field */
73 status &= ~XHCI_USB3_PORTSC_PED;
74 /* Clear all change status bits */
75 status |= XHCI_USB3_PORTSC_CHST;
76 write32(portsc, status);
Duncan Laurie1f529082013-07-30 15:53:45 -070077}
78
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080079static void usb_xhci_reset_port_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070080{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080081 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -070082 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
83}
84
85#define XHCI_RESET_DELAY_US 1000 /* 1ms */
86#define XHCI_RESET_TIMEOUT 100 /* 100ms */
87
88/*
89 * 1) Wait until port is done polling
90 * 2) If port is disconnected
91 * a) Issue warm port reset
92 * b) Poll for warm reset complete
93 * c) Write 1 to port change status bits
94 */
Elyes HAOUAS1dcd8db2018-12-05 10:59:42 +010095#ifdef __SIMPLE_DEVICE__
96static void usb_xhci_reset_usb3(pci_devfn_t dev, int all)
97#else
98static void usb_xhci_reset_usb3(struct device *dev, int all)
99#endif
Duncan Laurie1f529082013-07-30 15:53:45 -0700100{
101 u32 status, port_disabled;
102 int timeout, port;
103 int port_count = usb_xhci_port_count_usb3(dev);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800104 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -0700105
106 if (!mem_base || !port_count)
107 return;
108
109 /* Get mask of disabled ports */
110 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
111
112 /* Wait until all enabled ports are done polling */
113 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
114 int complete = 1;
115 for (port = 0; port < port_count; port++) {
116 /* Skip disabled ports */
117 if (port_disabled & (1 << port))
118 continue;
119 /* Read port link status field */
120 status = read32(mem_base + XHCI_USB3_PORTSC(port));
121 status &= XHCI_USB3_PORTSC_PLS;
122 if (status == XHCI_PLSR_POLLING)
123 complete = 0;
124 }
125 /* Exit if all ports not polling */
126 if (complete)
127 break;
128 udelay(XHCI_RESET_DELAY_US);
129 }
130
131 /* Reset all requested ports */
132 for (port = 0; port < port_count; port++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800133 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -0700134 /* Skip disabled ports */
135 if (port_disabled & (1 << port))
136 continue;
137 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
138 /* Reset all or only disconnected ports */
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700139 if (all || (status == XHCI_PLSR_RXDETECT ||
140 status == XHCI_PLSR_POLLING))
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700141 usb_xhci_reset_port_usb3(mem_base, port);
142 else
143 port_disabled |= 1 << port;
Duncan Laurie1f529082013-07-30 15:53:45 -0700144 }
145
146 /* Wait for warm reset complete on all reset ports */
147 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
148 int complete = 1;
149 for (port = 0; port < port_count; port++) {
150 /* Only check ports that were reset */
151 if (port_disabled & (1 << port))
152 continue;
153 /* Check if warm reset is complete */
154 status = read32(mem_base + XHCI_USB3_PORTSC(port));
155 if (!(status & XHCI_USB3_PORTSC_WRC))
156 complete = 0;
157 }
158 /* Check for warm reset complete in any port */
159 if (complete)
160 break;
161 udelay(XHCI_RESET_DELAY_US);
162 }
163
164 /* Clear port change status bits */
165 for (port = 0; port < port_count; port++)
166 usb_xhci_reset_status_usb3(mem_base, port);
167}
168
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200169#ifdef __SIMPLE_DEVICE__
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700170
Duncan Laurie1f529082013-07-30 15:53:45 -0700171/* Handler for XHCI controller on entry to S3/S4/S5 */
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100172void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Laurie1f529082013-07-30 15:53:45 -0700173{
174 u16 reg16;
175 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800176 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -0700177
Aaron Durbinda5f5092016-07-13 23:23:16 -0500178 if (!mem_base || slp_typ < ACPI_S3)
Duncan Laurie1f529082013-07-30 15:53:45 -0700179 return;
180
181 if (pch_is_lp()) {
182 /* Set D0 state */
183 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
184 reg16 &= ~PWR_CTL_SET_MASK;
185 reg16 |= PWR_CTL_SET_D0;
186 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
187
188 /* Clear PCI 0xB0[14:13] */
189 reg32 = pci_read_config32(dev, 0xb0);
190 reg32 &= ~((1 << 14) | (1 << 13));
191 pci_write_config32(dev, 0xb0, reg32);
192
193 /* Clear MMIO 0x816c[14,2] */
194 reg32 = read32(mem_base + 0x816c);
195 reg32 &= ~((1 << 14) | (1 << 2));
196 write32(mem_base + 0x816c, reg32);
197
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700198 /* Reset disconnected USB3 ports */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700199 usb_xhci_reset_usb3(dev, 0);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700200
Duncan Laurie1f529082013-07-30 15:53:45 -0700201 /* Set MMIO 0x80e0[15] */
202 reg32 = read32(mem_base + 0x80e0);
203 reg32 |= (1 << 15);
204 write32(mem_base + 0x80e0, reg32);
205 }
206
207 /* Set D3Hot state and enable PME */
208 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_SET_D3);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700209 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_STATUS_PME);
Duncan Laurie1f529082013-07-30 15:53:45 -0700210 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_ENABLE_PME);
211}
212
Duncan Laurie911cedf2013-07-30 16:05:55 -0700213/* Route all ports to XHCI controller */
214void usb_xhci_route_all(void)
215{
216 u32 port_mask, route;
217 u16 reg16;
218
219 /* Skip if EHCI is already disabled */
220 if (RCBA32(FD) & PCH_DISABLE_EHCI1)
221 return;
222
223 /* Set D0 state */
224 reg16 = pci_read_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS);
225 reg16 &= ~PWR_CTL_SET_MASK;
226 reg16 |= PWR_CTL_SET_D0;
227 pci_write_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS, reg16);
228
229 /* Set USB3 superspeed enable */
230 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PRM);
231 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PR);
232 route &= ~XHCI_USB3PR_SSEN;
233 route |= XHCI_USB3PR_SSEN & port_mask;
234 pci_write_config32(PCH_XHCI_DEV, XHCI_USB3PR, route);
235
236 /* Route USB2 ports to XHCI controller */
237 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PRM);
238 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PR);
239 route &= ~XHCI_USB2PR_HCSEL;
240 route |= XHCI_USB2PR_HCSEL & port_mask;
241 pci_write_config32(PCH_XHCI_DEV, XHCI_USB2PR, route);
242
243 /* Disable EHCI controller */
244 usb_ehci_disable(PCH_EHCI1_DEV);
245
246 /* LynxPoint-H has a second EHCI controller */
247 if (!pch_is_lp())
248 usb_ehci_disable(PCH_EHCI2_DEV);
249
250 /* Reset and clear port change status */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700251 usb_xhci_reset_usb3(PCH_XHCI_DEV, 1);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700252}
253
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200254#else /* !__SIMPLE_DEVICE__ */
Duncan Laurie1f529082013-07-30 15:53:45 -0700255
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100256static void usb_xhci_clock_gating(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700257{
258 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700259 u16 reg16;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700260
261 /* IOBP 0xE5004001[7:6] = 11b */
262 pch_iobp_update(0xe5004001, ~0, (1 << 7)|(1 << 6));
263
264 reg32 = pci_read_config32(dev, 0x40);
265 reg32 &= ~(1 << 23); /* unsupported request */
266
267 if (pch_is_lp()) {
268 /* D20:F0:40h[18,17,8] = 111b */
269 reg32 |= (1 << 18) | (1 << 17) | (1 << 8);
270 /* D20:F0:40h[21,20,19] = 110b to enable XHCI Idle L1 */
271 reg32 &= ~(1 << 19);
272 reg32 |= (1 << 21) | (1 << 20);
273 } else {
274 /* D20:F0:40h[21,20,18,17,8] = 11111b */
275 reg32 |= (1 << 21)|(1 << 20)|(1 << 18)|(1 << 17)|(1 << 8);
276 }
277
278 /* Avoid writing upper byte as it is write-once */
279 pci_write_config16(dev, 0x40, (u16)(reg32 & 0xffff));
280 pci_write_config8(dev, 0x40 + 2, (u8)((reg32 >> 16) & 0xff));
281
282 /* D20:F0:44h[9,7,3] = 111b */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700283 reg16 = pci_read_config16(dev, 0x44);
284 reg16 |= (1 << 9) | (1 << 7) | (1 << 3);
285 pci_write_config16(dev, 0x44, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700286
287 reg32 = pci_read_config32(dev, 0xa0);
288 if (pch_is_lp()) {
289 /* D20:F0:A0h[18] = 1 */
290 reg32 |= (1 << 18);
291 } else {
292 /* D20:F0:A0h[6] = 1 */
293 reg32 |= (1 << 6);
294 }
295 pci_write_config32(dev, 0xa0, reg32);
296
297 /* D20:F0:A4h[13] = 0 */
298 reg32 = pci_read_config32(dev, 0xa4);
299 reg32 &= ~(1 << 13);
300 pci_write_config32(dev, 0xa4, reg32);
301}
302
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100303static void usb_xhci_init(struct device *dev)
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700304{
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700305 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700306 u16 reg16;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800307 u8 *mem_base = usb_xhci_mem_base(dev);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700308 config_t *config = dev->chip_info;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700309
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700310 /* D20:F0:74h[1:0] = 00b (set D0 state) */
311 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
312 reg16 &= ~PWR_CTL_SET_MASK;
313 reg16 |= PWR_CTL_SET_D0;
314 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700315
316 /* Enable clock gating first */
317 usb_xhci_clock_gating(dev);
318
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700319 reg32 = read32(mem_base + 0x8144);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700320 if (pch_is_lp()) {
321 /* XHCIBAR + 8144h[8,7,6] = 111b */
322 reg32 |= (1 << 8) | (1 << 7) | (1 << 6);
323 } else {
324 /* XHCIBAR + 8144h[8,7,6] = 100b */
325 reg32 &= ~((1 << 7) | (1 << 6));
326 reg32 |= (1 << 8);
327 }
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700328 write32(mem_base + 0x8144, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700329
330 if (pch_is_lp()) {
Duncan Laurie527b03a2013-08-28 09:53:50 -0700331 /* XHCIBAR + 816Ch[19:0] = 000e0038h */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700332 reg32 = read32(mem_base + 0x816c);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700333 reg32 &= ~0x000fffff;
Duncan Laurie527b03a2013-08-28 09:53:50 -0700334 reg32 |= 0x000e0038;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700335 write32(mem_base + 0x816c, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700336
337 /* D20:F0:B0h[17,14,13] = 100b */
338 reg32 = pci_read_config32(dev, 0xb0);
339 reg32 &= ~((1 << 14) | (1 << 13));
340 reg32 |= (1 << 17);
341 pci_write_config32(dev, 0xb0, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700342 }
343
344 reg32 = pci_read_config32(dev, 0x50);
345 if (pch_is_lp()) {
346 /* D20:F0:50h[28:0] = 0FCE2E5Fh */
347 reg32 &= ~0x1fffffff;
348 reg32 |= 0x0fce2e5f;
349 } else {
350 /* D20:F0:50h[26:0] = 07886E9Fh */
351 reg32 &= ~0x07ffffff;
352 reg32 |= 0x07886e9f;
353 }
354 pci_write_config32(dev, 0x50, reg32);
355
356 /* D20:F0:44h[31] = 1 (Access Control Bit) */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700357 reg32 = pci_read_config32(dev, 0x44);
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700358 reg32 |= (1UL << 31);
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700359 pci_write_config32(dev, 0x44, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700360
361 /* D20:F0:40h[31,23] = 10b (OC Configuration Done) */
362 reg32 = pci_read_config32(dev, 0x40);
363 reg32 &= ~(1 << 23); /* unsupported request */
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700364 reg32 |= (1UL << 31);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700365 pci_write_config32(dev, 0x40, reg32);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700366
367 if (acpi_is_wakeup_s3()) {
368 /* Reset ports that are disabled or
369 * polling before returning to the OS. */
370 usb_xhci_reset_usb3(dev, 0);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700371 } else if (config->xhci_default) {
372 /* Route all ports to XHCI */
373 outb(0xca, 0xb2);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700374 }
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700375}
376
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700377static struct pci_operations lops_pci = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530378 .set_subsystem = &pci_dev_set_subsystem,
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700379};
380
381static struct device_operations usb_xhci_ops = {
382 .read_resources = pci_dev_read_resources,
383 .set_resources = pci_dev_set_resources,
384 .enable_resources = pci_dev_enable_resources,
385 .init = usb_xhci_init,
386 .ops_pci = &lops_pci,
387};
388
389static const unsigned short pci_device_ids[] = { 0x8c31, /* LynxPoint-H */
390 0x9c31, /* LynxPoint-LP */
391 0 };
392
393static const struct pci_driver pch_usb_xhci __pci_driver = {
394 .ops = &usb_xhci_ops,
395 .vendor = PCI_VENDOR_ID_INTEL,
396 .devices = pci_device_ids,
397};
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200398#endif /* !__SIMPLE_DEVICE__ */