blob: f6409b4381077e94e72464584240be764c2a8678 [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Duncan Laurie1f529082013-07-30 15:53:45 -070021#include <console/console.h>
22#include <delay.h>
Duncan Laurie2d9d39a2013-05-29 15:27:55 -070023#include <device/device.h>
24#include <device/pci.h>
25#include <device/pci_ids.h>
26#include <arch/io.h>
27#include "pch.h"
28
Duncan Laurie1f529082013-07-30 15:53:45 -070029static u32 usb_xhci_mem_base(device_t dev)
30{
31 u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
32
33 /* Check if the controller is disabled or not present */
34 if (mem_base == 0 || mem_base == 0xffffffff)
35 return 0;
36
37 return mem_base & ~0xf;
38}
39
Duncan Laurie1f529082013-07-30 15:53:45 -070040static int usb_xhci_port_count_usb3(device_t dev)
41{
42 if (pch_is_lp()) {
43 /* LynxPoint-LP has 4 SS ports */
44 return 4;
45 } else {
46 /* LynxPoint-H can have 0, 2, 4, or 6 SS ports */
47 u32 mem_base = usb_xhci_mem_base(dev);
48 u32 fus = read32(mem_base + XHCI_USB3FUS);
49 fus >>= XHCI_USB3FUS_SS_SHIFT;
50 fus &= XHCI_USB3FUS_SS_MASK;
51 switch (fus) {
52 case 3: return 0;
53 case 2: return 2;
54 case 1: return 4;
55 case 0: default: return 6;
56 }
57 }
58 return 0;
59}
60
61static void usb_xhci_reset_status_usb3(u32 mem_base, int port)
62{
63 u32 portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -070064 u32 status = read32(portsc);
65 /* Do not set Port Enabled/Disabled field */
66 status &= ~XHCI_USB3_PORTSC_PED;
67 /* Clear all change status bits */
68 status |= XHCI_USB3_PORTSC_CHST;
69 write32(portsc, status);
Duncan Laurie1f529082013-07-30 15:53:45 -070070}
71
72static void usb_xhci_reset_port_usb3(u32 mem_base, int port)
73{
74 u32 portsc = mem_base + XHCI_USB3_PORTSC(port);
75 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
76}
77
78#define XHCI_RESET_DELAY_US 1000 /* 1ms */
79#define XHCI_RESET_TIMEOUT 100 /* 100ms */
80
Duncan Laurie88c873a2013-09-16 13:51:08 -070081#ifdef __SMM__
82
Duncan Laurie1f529082013-07-30 15:53:45 -070083/*
84 * 1) Wait until port is done polling
85 * 2) If port is disconnected
86 * a) Issue warm port reset
87 * b) Poll for warm reset complete
88 * c) Write 1 to port change status bits
89 */
Duncan Laurie88c873a2013-09-16 13:51:08 -070090static void usb_xhci_reset_usb3(device_t dev)
Duncan Laurie1f529082013-07-30 15:53:45 -070091{
92 u32 status, port_disabled;
93 int timeout, port;
94 int port_count = usb_xhci_port_count_usb3(dev);
95 u32 mem_base = usb_xhci_mem_base(dev);
96
97 if (!mem_base || !port_count)
98 return;
99
100 /* Get mask of disabled ports */
101 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
102
103 /* Wait until all enabled ports are done polling */
104 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
105 int complete = 1;
106 for (port = 0; port < port_count; port++) {
107 /* Skip disabled ports */
108 if (port_disabled & (1 << port))
109 continue;
110 /* Read port link status field */
111 status = read32(mem_base + XHCI_USB3_PORTSC(port));
112 status &= XHCI_USB3_PORTSC_PLS;
113 if (status == XHCI_PLSR_POLLING)
114 complete = 0;
115 }
116 /* Exit if all ports not polling */
117 if (complete)
118 break;
119 udelay(XHCI_RESET_DELAY_US);
120 }
121
122 /* Reset all requested ports */
123 for (port = 0; port < port_count; port++) {
124 u32 portsc = mem_base + XHCI_USB3_PORTSC(port);
125 /* Skip disabled ports */
126 if (port_disabled & (1 << port))
127 continue;
128 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
129 /* Reset all or only disconnected ports */
Duncan Laurie88c873a2013-09-16 13:51:08 -0700130 usb_xhci_reset_port_usb3(mem_base, 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
Duncan Laurie1f529082013-07-30 15:53:45 -0700156/* Handler for XHCI controller on entry to S3/S4/S5 */
157void usb_xhci_sleep_prepare(device_t dev, u8 slp_typ)
158{
159 u16 reg16;
160 u32 reg32;
161 u32 mem_base = usb_xhci_mem_base(dev);
162
163 if (!mem_base || slp_typ < 3)
164 return;
165
166 if (pch_is_lp()) {
167 /* Set D0 state */
168 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
169 reg16 &= ~PWR_CTL_SET_MASK;
170 reg16 |= PWR_CTL_SET_D0;
171 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
172
173 /* Clear PCI 0xB0[14:13] */
174 reg32 = pci_read_config32(dev, 0xb0);
175 reg32 &= ~((1 << 14) | (1 << 13));
176 pci_write_config32(dev, 0xb0, reg32);
177
178 /* Clear MMIO 0x816c[14,2] */
179 reg32 = read32(mem_base + 0x816c);
180 reg32 &= ~((1 << 14) | (1 << 2));
181 write32(mem_base + 0x816c, reg32);
182
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700183 /* Reset disconnected USB3 ports */
Duncan Laurie88c873a2013-09-16 13:51:08 -0700184 usb_xhci_reset_usb3(dev);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700185
Duncan Laurie1f529082013-07-30 15:53:45 -0700186 /* Set MMIO 0x80e0[15] */
187 reg32 = read32(mem_base + 0x80e0);
188 reg32 |= (1 << 15);
189 write32(mem_base + 0x80e0, reg32);
190 }
191
192 /* Set D3Hot state and enable PME */
193 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_SET_D3);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700194 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_STATUS_PME);
Duncan Laurie1f529082013-07-30 15:53:45 -0700195 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_ENABLE_PME);
196}
197
Duncan Laurie911cedf2013-07-30 16:05:55 -0700198/* Route all ports to XHCI controller */
199void usb_xhci_route_all(void)
200{
201 u32 port_mask, route;
202 u16 reg16;
203
204 /* Skip if EHCI is already disabled */
205 if (RCBA32(FD) & PCH_DISABLE_EHCI1)
206 return;
207
208 /* Set D0 state */
209 reg16 = pci_read_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS);
210 reg16 &= ~PWR_CTL_SET_MASK;
211 reg16 |= PWR_CTL_SET_D0;
212 pci_write_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS, reg16);
213
214 /* Set USB3 superspeed enable */
215 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PRM);
216 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PR);
217 route &= ~XHCI_USB3PR_SSEN;
218 route |= XHCI_USB3PR_SSEN & port_mask;
219 pci_write_config32(PCH_XHCI_DEV, XHCI_USB3PR, route);
220
221 /* Route USB2 ports to XHCI controller */
222 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PRM);
223 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PR);
224 route &= ~XHCI_USB2PR_HCSEL;
225 route |= XHCI_USB2PR_HCSEL & port_mask;
226 pci_write_config32(PCH_XHCI_DEV, XHCI_USB2PR, route);
227
228 /* Disable EHCI controller */
229 usb_ehci_disable(PCH_EHCI1_DEV);
230
231 /* LynxPoint-H has a second EHCI controller */
232 if (!pch_is_lp())
233 usb_ehci_disable(PCH_EHCI2_DEV);
234
235 /* Reset and clear port change status */
Duncan Laurie88c873a2013-09-16 13:51:08 -0700236 usb_xhci_reset_usb3(PCH_XHCI_DEV);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700237}
238
Duncan Laurie1f529082013-07-30 15:53:45 -0700239#else /* !__SMM__ */
240
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700241static void usb_xhci_clock_gating(device_t dev)
242{
243 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700244 u16 reg16;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700245
246 /* IOBP 0xE5004001[7:6] = 11b */
247 pch_iobp_update(0xe5004001, ~0, (1 << 7)|(1 << 6));
248
249 reg32 = pci_read_config32(dev, 0x40);
250 reg32 &= ~(1 << 23); /* unsupported request */
251
252 if (pch_is_lp()) {
253 /* D20:F0:40h[18,17,8] = 111b */
254 reg32 |= (1 << 18) | (1 << 17) | (1 << 8);
255 /* D20:F0:40h[21,20,19] = 110b to enable XHCI Idle L1 */
256 reg32 &= ~(1 << 19);
257 reg32 |= (1 << 21) | (1 << 20);
258 } else {
259 /* D20:F0:40h[21,20,18,17,8] = 11111b */
260 reg32 |= (1 << 21)|(1 << 20)|(1 << 18)|(1 << 17)|(1 << 8);
261 }
262
263 /* Avoid writing upper byte as it is write-once */
264 pci_write_config16(dev, 0x40, (u16)(reg32 & 0xffff));
265 pci_write_config8(dev, 0x40 + 2, (u8)((reg32 >> 16) & 0xff));
266
267 /* D20:F0:44h[9,7,3] = 111b */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700268 reg16 = pci_read_config16(dev, 0x44);
269 reg16 |= (1 << 9) | (1 << 7) | (1 << 3);
270 pci_write_config16(dev, 0x44, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700271
272 reg32 = pci_read_config32(dev, 0xa0);
273 if (pch_is_lp()) {
274 /* D20:F0:A0h[18] = 1 */
275 reg32 |= (1 << 18);
276 } else {
277 /* D20:F0:A0h[6] = 1 */
278 reg32 |= (1 << 6);
279 }
280 pci_write_config32(dev, 0xa0, reg32);
281
282 /* D20:F0:A4h[13] = 0 */
283 reg32 = pci_read_config32(dev, 0xa4);
284 reg32 &= ~(1 << 13);
285 pci_write_config32(dev, 0xa4, reg32);
286}
287
Duncan Laurie911cedf2013-07-30 16:05:55 -0700288/* Re-enable ports that are disabled */
289static void usb_xhci_enable_ports_usb3(device_t dev)
290{
291#if CONFIG_FINALIZE_USB_ROUTE_XHCI
292 int port;
293 u32 portsc, status, disabled;
294 u32 mem_base = usb_xhci_mem_base(dev);
295 int port_count = usb_xhci_port_count_usb3(dev);
Duncan Laurie88c873a2013-09-16 13:51:08 -0700296 u8 port_reset = 0;
297 int timeout;
Duncan Laurie911cedf2013-07-30 16:05:55 -0700298
299 if (!mem_base || !port_count)
300 return;
301
302 /* Get port disable override map */
303 disabled = pci_read_config32(dev, XHCI_USB3PDO);
304
305 for (port = 0; port < port_count; port++) {
306 /* Skip overridden ports */
307 if (disabled & (1 << port))
308 continue;
309 portsc = mem_base + XHCI_USB3_PORTSC(port);
310 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
Duncan Laurie911cedf2013-07-30 16:05:55 -0700311 switch (status) {
312 case XHCI_PLSR_RXDETECT:
313 /* Clear change status */
Duncan Laurie88c873a2013-09-16 13:51:08 -0700314 printk(BIOS_DEBUG, "usb_xhci reset status %d\n", port);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700315 usb_xhci_reset_status_usb3(mem_base, port);
316 break;
317 case XHCI_PLSR_DISABLED:
318 default:
Duncan Laurie88c873a2013-09-16 13:51:08 -0700319 /* Reset port */
320 printk(BIOS_DEBUG, "usb_xhci reset port %d\n", port);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700321 usb_xhci_reset_port_usb3(mem_base, port);
Duncan Laurie88c873a2013-09-16 13:51:08 -0700322 port_reset |= 1 << port;
Duncan Laurie911cedf2013-07-30 16:05:55 -0700323 break;
324 }
325 }
Duncan Laurie88c873a2013-09-16 13:51:08 -0700326
327 if (!port_reset)
328 return;
329
330 /* Wait for warm reset complete on all reset ports */
331 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
332 int complete = 1;
333 for (port = 0; port < port_count; port++) {
334 /* Only check ports that were reset */
335 if (!(port_reset & (1 << port)))
336 continue;
337 /* Check if warm reset is complete */
338 status = read32(mem_base + XHCI_USB3_PORTSC(port));
339 if (!(status & XHCI_USB3_PORTSC_WRC))
340 complete = 0;
341 }
342 /* Check for warm reset complete in any port */
343 if (complete)
344 break;
345 udelay(XHCI_RESET_DELAY_US);
346 }
347
348 /* Enable ports that were reset */
349 for (port = 0; port < port_count; port++) {
350 /* Only check ports that were reset */
351 if (!(port_reset & (1 << port)))
352 continue;
353 /* Transition to enabled */
354 portsc = mem_base + XHCI_USB3_PORTSC(port);
355 status = read32(portsc);
356 status &= ~(XHCI_USB3_PORTSC_PLS | XHCI_USB3_PORTSC_PED);
357 status |= XHCI_PLSW_ENABLE | XHCI_USB3_PORTSC_LWS;
358 write32(portsc, status);
359 }
Duncan Laurie911cedf2013-07-30 16:05:55 -0700360#endif
361}
362
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700363static void usb_xhci_init(device_t dev)
364{
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700365 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700366 u16 reg16;
367 u32 mem_base = usb_xhci_mem_base(dev);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700368
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700369 /* D20:F0:74h[1:0] = 00b (set D0 state) */
370 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
371 reg16 &= ~PWR_CTL_SET_MASK;
372 reg16 |= PWR_CTL_SET_D0;
373 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700374
375 /* Enable clock gating first */
376 usb_xhci_clock_gating(dev);
377
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700378 reg32 = read32(mem_base + 0x8144);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700379 if (pch_is_lp()) {
380 /* XHCIBAR + 8144h[8,7,6] = 111b */
381 reg32 |= (1 << 8) | (1 << 7) | (1 << 6);
382 } else {
383 /* XHCIBAR + 8144h[8,7,6] = 100b */
384 reg32 &= ~((1 << 7) | (1 << 6));
385 reg32 |= (1 << 8);
386 }
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700387 write32(mem_base + 0x8144, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700388
389 if (pch_is_lp()) {
Duncan Laurie527b03a2013-08-28 09:53:50 -0700390 /* XHCIBAR + 816Ch[19:0] = 000e0038h */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700391 reg32 = read32(mem_base + 0x816c);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700392 reg32 &= ~0x000fffff;
Duncan Laurie527b03a2013-08-28 09:53:50 -0700393 reg32 |= 0x000e0038;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700394 write32(mem_base + 0x816c, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700395
396 /* D20:F0:B0h[17,14,13] = 100b */
397 reg32 = pci_read_config32(dev, 0xb0);
398 reg32 &= ~((1 << 14) | (1 << 13));
399 reg32 |= (1 << 17);
400 pci_write_config32(dev, 0xb0, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700401 }
402
403 reg32 = pci_read_config32(dev, 0x50);
404 if (pch_is_lp()) {
405 /* D20:F0:50h[28:0] = 0FCE2E5Fh */
406 reg32 &= ~0x1fffffff;
407 reg32 |= 0x0fce2e5f;
408 } else {
409 /* D20:F0:50h[26:0] = 07886E9Fh */
410 reg32 &= ~0x07ffffff;
411 reg32 |= 0x07886e9f;
412 }
413 pci_write_config32(dev, 0x50, reg32);
414
415 /* D20:F0:44h[31] = 1 (Access Control Bit) */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700416 reg32 = pci_read_config32(dev, 0x44);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700417 reg32 |= (1 << 31);
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700418 pci_write_config32(dev, 0x44, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700419
420 /* D20:F0:40h[31,23] = 10b (OC Configuration Done) */
421 reg32 = pci_read_config32(dev, 0x40);
422 reg32 &= ~(1 << 23); /* unsupported request */
423 reg32 |= (1 << 31);
424 pci_write_config32(dev, 0x40, reg32);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700425
Duncan Laurie911cedf2013-07-30 16:05:55 -0700426 /* Enable ports that are disabled before returning to OS */
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +0300427 if (acpi_is_wakeup_s3())
Duncan Laurie911cedf2013-07-30 16:05:55 -0700428 usb_xhci_enable_ports_usb3(dev);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700429}
430
431static void usb_xhci_set_subsystem(device_t dev, unsigned vendor,
432 unsigned device)
433{
434 if (!vendor || !device) {
435 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
436 pci_read_config32(dev, PCI_VENDOR_ID));
437 } else {
438 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
439 ((device & 0xffff) << 16) | (vendor & 0xffff));
440 }
441}
442
443static struct pci_operations lops_pci = {
444 .set_subsystem = &usb_xhci_set_subsystem,
445};
446
447static struct device_operations usb_xhci_ops = {
448 .read_resources = pci_dev_read_resources,
449 .set_resources = pci_dev_set_resources,
450 .enable_resources = pci_dev_enable_resources,
451 .init = usb_xhci_init,
452 .ops_pci = &lops_pci,
453};
454
455static const unsigned short pci_device_ids[] = { 0x8c31, /* LynxPoint-H */
456 0x9c31, /* LynxPoint-LP */
457 0 };
458
459static const struct pci_driver pch_usb_xhci __pci_driver = {
460 .ops = &usb_xhci_ops,
461 .vendor = PCI_VENDOR_ID_INTEL,
462 .devices = pci_device_ids,
463};
Duncan Laurie1f529082013-07-30 15:53:45 -0700464#endif /* !__SMM__ */