blob: dbf0f4095d8dc638be42c51a15e81d73a0f893bf [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);
64 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_CHST);
65}
66
67static void usb_xhci_reset_port_usb3(u32 mem_base, int port)
68{
69 u32 portsc = mem_base + XHCI_USB3_PORTSC(port);
70 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
71}
72
Duncan Laurie911cedf2013-07-30 16:05:55 -070073#ifdef __SMM__
74
Duncan Laurie1f529082013-07-30 15:53:45 -070075#define XHCI_RESET_DELAY_US 1000 /* 1ms */
76#define XHCI_RESET_TIMEOUT 100 /* 100ms */
77
78/*
79 * 1) Wait until port is done polling
80 * 2) If port is disconnected
81 * a) Issue warm port reset
82 * b) Poll for warm reset complete
83 * c) Write 1 to port change status bits
84 */
85static void usb_xhci_reset_usb3(device_t dev, int all)
86{
87 u32 status, port_disabled;
88 int timeout, port;
89 int port_count = usb_xhci_port_count_usb3(dev);
90 u32 mem_base = usb_xhci_mem_base(dev);
91
92 if (!mem_base || !port_count)
93 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++) {
119 u32 portsc = mem_base + XHCI_USB3_PORTSC(port);
120 /* 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 */
125 if (all || status == XHCI_PLSR_RXDETECT)
126 usb_xhci_reset_port_usb3(mem_base, port);
127 else
128 port_disabled |= 1 << port; /* No reset */
129 }
130
131 /* Wait for warm reset complete on all reset ports */
132 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
133 int complete = 1;
134 for (port = 0; port < port_count; port++) {
135 /* Only check ports that were reset */
136 if (port_disabled & (1 << port))
137 continue;
138 /* Check if warm reset is complete */
139 status = read32(mem_base + XHCI_USB3_PORTSC(port));
140 if (!(status & XHCI_USB3_PORTSC_WRC))
141 complete = 0;
142 }
143 /* Check for warm reset complete in any port */
144 if (complete)
145 break;
146 udelay(XHCI_RESET_DELAY_US);
147 }
148
149 /* Clear port change status bits */
150 for (port = 0; port < port_count; port++)
151 usb_xhci_reset_status_usb3(mem_base, port);
152}
153
Duncan Laurie1f529082013-07-30 15:53:45 -0700154/* Handler for XHCI controller on entry to S3/S4/S5 */
155void usb_xhci_sleep_prepare(device_t dev, u8 slp_typ)
156{
157 u16 reg16;
158 u32 reg32;
159 u32 mem_base = usb_xhci_mem_base(dev);
160
161 if (!mem_base || slp_typ < 3)
162 return;
163
164 if (pch_is_lp()) {
165 /* Set D0 state */
166 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
167 reg16 &= ~PWR_CTL_SET_MASK;
168 reg16 |= PWR_CTL_SET_D0;
169 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
170
171 /* Clear PCI 0xB0[14:13] */
172 reg32 = pci_read_config32(dev, 0xb0);
173 reg32 &= ~((1 << 14) | (1 << 13));
174 pci_write_config32(dev, 0xb0, reg32);
175
176 /* Clear MMIO 0x816c[14,2] */
177 reg32 = read32(mem_base + 0x816c);
178 reg32 &= ~((1 << 14) | (1 << 2));
179 write32(mem_base + 0x816c, reg32);
180
181 /* 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);
189 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_ENABLE_PME);
190}
191
Duncan Laurie911cedf2013-07-30 16:05:55 -0700192/* Route all ports to XHCI controller */
193void usb_xhci_route_all(void)
194{
195 u32 port_mask, route;
196 u16 reg16;
197
198 /* Skip if EHCI is already disabled */
199 if (RCBA32(FD) & PCH_DISABLE_EHCI1)
200 return;
201
202 /* Set D0 state */
203 reg16 = pci_read_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS);
204 reg16 &= ~PWR_CTL_SET_MASK;
205 reg16 |= PWR_CTL_SET_D0;
206 pci_write_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS, reg16);
207
208 /* Set USB3 superspeed enable */
209 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PRM);
210 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PR);
211 route &= ~XHCI_USB3PR_SSEN;
212 route |= XHCI_USB3PR_SSEN & port_mask;
213 pci_write_config32(PCH_XHCI_DEV, XHCI_USB3PR, route);
214
215 /* Route USB2 ports to XHCI controller */
216 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PRM);
217 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PR);
218 route &= ~XHCI_USB2PR_HCSEL;
219 route |= XHCI_USB2PR_HCSEL & port_mask;
220 pci_write_config32(PCH_XHCI_DEV, XHCI_USB2PR, route);
221
222 /* Disable EHCI controller */
223 usb_ehci_disable(PCH_EHCI1_DEV);
224
225 /* LynxPoint-H has a second EHCI controller */
226 if (!pch_is_lp())
227 usb_ehci_disable(PCH_EHCI2_DEV);
228
229 /* Reset and clear port change status */
230 usb_xhci_reset_usb3(PCH_XHCI_DEV, 1);
231}
232
Duncan Laurie1f529082013-07-30 15:53:45 -0700233#else /* !__SMM__ */
234
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700235static void usb_xhci_clock_gating(device_t dev)
236{
237 u32 reg32;
238
239 /* IOBP 0xE5004001[7:6] = 11b */
240 pch_iobp_update(0xe5004001, ~0, (1 << 7)|(1 << 6));
241
242 reg32 = pci_read_config32(dev, 0x40);
243 reg32 &= ~(1 << 23); /* unsupported request */
244
245 if (pch_is_lp()) {
246 /* D20:F0:40h[18,17,8] = 111b */
247 reg32 |= (1 << 18) | (1 << 17) | (1 << 8);
248 /* D20:F0:40h[21,20,19] = 110b to enable XHCI Idle L1 */
249 reg32 &= ~(1 << 19);
250 reg32 |= (1 << 21) | (1 << 20);
251 } else {
252 /* D20:F0:40h[21,20,18,17,8] = 11111b */
253 reg32 |= (1 << 21)|(1 << 20)|(1 << 18)|(1 << 17)|(1 << 8);
254 }
255
256 /* Avoid writing upper byte as it is write-once */
257 pci_write_config16(dev, 0x40, (u16)(reg32 & 0xffff));
258 pci_write_config8(dev, 0x40 + 2, (u8)((reg32 >> 16) & 0xff));
259
260 /* D20:F0:44h[9,7,3] = 111b */
261 reg32 = pci_read_config32(dev, 0x44);
262 reg32 |= (1 << 9) | (1 << 7) | (1 << 3);
263 pci_write_config32(dev, 0x44, reg32);
264
265 reg32 = pci_read_config32(dev, 0xa0);
266 if (pch_is_lp()) {
267 /* D20:F0:A0h[18] = 1 */
268 reg32 |= (1 << 18);
269 } else {
270 /* D20:F0:A0h[6] = 1 */
271 reg32 |= (1 << 6);
272 }
273 pci_write_config32(dev, 0xa0, reg32);
274
275 /* D20:F0:A4h[13] = 0 */
276 reg32 = pci_read_config32(dev, 0xa4);
277 reg32 &= ~(1 << 13);
278 pci_write_config32(dev, 0xa4, reg32);
279}
280
Duncan Laurie911cedf2013-07-30 16:05:55 -0700281/* Re-enable ports that are disabled */
282static void usb_xhci_enable_ports_usb3(device_t dev)
283{
284#if CONFIG_FINALIZE_USB_ROUTE_XHCI
285 int port;
286 u32 portsc, status, disabled;
287 u32 mem_base = usb_xhci_mem_base(dev);
288 int port_count = usb_xhci_port_count_usb3(dev);
289
290 if (!mem_base || !port_count)
291 return;
292
293 /* Get port disable override map */
294 disabled = pci_read_config32(dev, XHCI_USB3PDO);
295
296 for (port = 0; port < port_count; port++) {
297 /* Skip overridden ports */
298 if (disabled & (1 << port))
299 continue;
300 portsc = mem_base + XHCI_USB3_PORTSC(port);
301 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
302
303 switch (status) {
304 case XHCI_PLSR_RXDETECT:
305 /* Clear change status */
306 printk(BIOS_DEBUG, "usb_xhci reset port %d\n", port);
307 usb_xhci_reset_status_usb3(mem_base, port);
308 break;
309 case XHCI_PLSR_DISABLED:
310 default:
311 /* Transition to enabled */
312 printk(BIOS_DEBUG, "usb_xhci enable port %d\n", port);
313 usb_xhci_reset_port_usb3(mem_base, port);
314 status = read32(portsc);
315 status &= ~XHCI_USB3_PORTSC_PLS;
316 status |= XHCI_PLSW_ENABLE | XHCI_USB3_PORTSC_LWS;
317 write32(portsc, status);
318 break;
319 }
320 }
321#endif
322}
323
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700324static void usb_xhci_init(device_t dev)
325{
326 struct resource *bar0 = find_resource(dev, PCI_BASE_ADDRESS_0);
327 u32 reg32;
328
329 if (!bar0 || bar0->base == 0 || bar0->base == 0xffffffff)
330 return;
331
332 /* Enable clock gating first */
333 usb_xhci_clock_gating(dev);
334
335 /* D20:F0:74h[1:0] = 11b (set D3Hot state) */
336 reg32 = pci_read_config16(dev, 0x74);
337 reg32 |= (1 << 1) | (1 << 0);
338 pci_write_config16(dev, 0x74, reg32);
339
340 reg32 = read32(bar0->base + 0x8144);
341 if (pch_is_lp()) {
342 /* XHCIBAR + 8144h[8,7,6] = 111b */
343 reg32 |= (1 << 8) | (1 << 7) | (1 << 6);
344 } else {
345 /* XHCIBAR + 8144h[8,7,6] = 100b */
346 reg32 &= ~((1 << 7) | (1 << 6));
347 reg32 |= (1 << 8);
348 }
349 write32(bar0->base + 0x8144, reg32);
350
351 if (pch_is_lp()) {
352 /* XHCIBAR + 816Ch[19:0] = 000f0038h */
353 reg32 = read32(bar0->base + 0x816c);
354 reg32 &= ~0x000fffff;
355 reg32 |= 0x000f0038;
356 write32(bar0->base + 0x816c, reg32);
357
358 /* D20:F0:B0h[17,14,13] = 100b */
359 reg32 = pci_read_config32(dev, 0xb0);
360 reg32 &= ~((1 << 14) | (1 << 13));
361 reg32 |= (1 << 17);
362 pci_write_config32(dev, 0xb0, reg32);
363
364 /* XHCIBAR + 818Ch[7:0] = FFh */
365 reg32 = read32(bar0->base + 0x818c);
366 reg32 |= 0xff;
367 write32(bar0->base + 0x818c, reg32);
368 }
369
370 reg32 = pci_read_config32(dev, 0x50);
371 if (pch_is_lp()) {
372 /* D20:F0:50h[28:0] = 0FCE2E5Fh */
373 reg32 &= ~0x1fffffff;
374 reg32 |= 0x0fce2e5f;
375 } else {
376 /* D20:F0:50h[26:0] = 07886E9Fh */
377 reg32 &= ~0x07ffffff;
378 reg32 |= 0x07886e9f;
379 }
380 pci_write_config32(dev, 0x50, reg32);
381
382 /* D20:F0:44h[31] = 1 (Access Control Bit) */
383 reg32 = pci_read_config32(dev, 0x40);
384 reg32 |= (1 << 31);
385 pci_write_config32(dev, 0x40, reg32);
386
387 /* D20:F0:40h[31,23] = 10b (OC Configuration Done) */
388 reg32 = pci_read_config32(dev, 0x40);
389 reg32 &= ~(1 << 23); /* unsupported request */
390 reg32 |= (1 << 31);
391 pci_write_config32(dev, 0x40, reg32);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700392
393#if CONFIG_HAVE_ACPI_RESUME
394 /* Enable ports that are disabled before returning to OS */
395 if (acpi_slp_type == 3)
396 usb_xhci_enable_ports_usb3(dev);
397#endif
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700398}
399
400static void usb_xhci_set_subsystem(device_t dev, unsigned vendor,
401 unsigned device)
402{
403 if (!vendor || !device) {
404 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
405 pci_read_config32(dev, PCI_VENDOR_ID));
406 } else {
407 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
408 ((device & 0xffff) << 16) | (vendor & 0xffff));
409 }
410}
411
412static struct pci_operations lops_pci = {
413 .set_subsystem = &usb_xhci_set_subsystem,
414};
415
416static struct device_operations usb_xhci_ops = {
417 .read_resources = pci_dev_read_resources,
418 .set_resources = pci_dev_set_resources,
419 .enable_resources = pci_dev_enable_resources,
420 .init = usb_xhci_init,
421 .ops_pci = &lops_pci,
422};
423
424static const unsigned short pci_device_ids[] = { 0x8c31, /* LynxPoint-H */
425 0x9c31, /* LynxPoint-LP */
426 0 };
427
428static const struct pci_driver pch_usb_xhci __pci_driver = {
429 .ops = &usb_xhci_ops,
430 .vendor = PCI_VENDOR_ID_INTEL,
431 .devices = pci_device_ids,
432};
Duncan Laurie1f529082013-07-30 15:53:45 -0700433#endif /* !__SMM__ */