blob: 997ef6109805f054b0578b4fff57bd2a2d7d7273 [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
81/*
82 * 1) Wait until port is done polling
83 * 2) If port is disconnected
84 * a) Issue warm port reset
85 * b) Poll for warm reset complete
86 * c) Write 1 to port change status bits
87 */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -070088static void usb_xhci_reset_usb3(device_t dev, int all)
Duncan Laurie1f529082013-07-30 15:53:45 -070089{
90 u32 status, port_disabled;
91 int timeout, port;
92 int port_count = usb_xhci_port_count_usb3(dev);
93 u32 mem_base = usb_xhci_mem_base(dev);
94
95 if (!mem_base || !port_count)
96 return;
97
98 /* Get mask of disabled ports */
99 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
100
101 /* Wait until all enabled ports are done polling */
102 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
103 int complete = 1;
104 for (port = 0; port < port_count; port++) {
105 /* Skip disabled ports */
106 if (port_disabled & (1 << port))
107 continue;
108 /* Read port link status field */
109 status = read32(mem_base + XHCI_USB3_PORTSC(port));
110 status &= XHCI_USB3_PORTSC_PLS;
111 if (status == XHCI_PLSR_POLLING)
112 complete = 0;
113 }
114 /* Exit if all ports not polling */
115 if (complete)
116 break;
117 udelay(XHCI_RESET_DELAY_US);
118 }
119
120 /* Reset all requested ports */
121 for (port = 0; port < port_count; port++) {
122 u32 portsc = mem_base + XHCI_USB3_PORTSC(port);
123 /* Skip disabled ports */
124 if (port_disabled & (1 << port))
125 continue;
126 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
127 /* Reset all or only disconnected ports */
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700128 if (all || (status == XHCI_PLSR_RXDETECT ||
129 status == XHCI_PLSR_POLLING))
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700130 usb_xhci_reset_port_usb3(mem_base, port);
131 else
132 port_disabled |= 1 << port;
Duncan Laurie1f529082013-07-30 15:53:45 -0700133 }
134
135 /* Wait for warm reset complete on all reset ports */
136 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
137 int complete = 1;
138 for (port = 0; port < port_count; port++) {
139 /* Only check ports that were reset */
140 if (port_disabled & (1 << port))
141 continue;
142 /* Check if warm reset is complete */
143 status = read32(mem_base + XHCI_USB3_PORTSC(port));
144 if (!(status & XHCI_USB3_PORTSC_WRC))
145 complete = 0;
146 }
147 /* Check for warm reset complete in any port */
148 if (complete)
149 break;
150 udelay(XHCI_RESET_DELAY_US);
151 }
152
153 /* Clear port change status bits */
154 for (port = 0; port < port_count; port++)
155 usb_xhci_reset_status_usb3(mem_base, port);
156}
157
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700158#ifdef __SMM__
159
Duncan Laurie1f529082013-07-30 15:53:45 -0700160/* Handler for XHCI controller on entry to S3/S4/S5 */
161void usb_xhci_sleep_prepare(device_t dev, u8 slp_typ)
162{
163 u16 reg16;
164 u32 reg32;
165 u32 mem_base = usb_xhci_mem_base(dev);
166
167 if (!mem_base || slp_typ < 3)
168 return;
169
170 if (pch_is_lp()) {
171 /* Set D0 state */
172 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
173 reg16 &= ~PWR_CTL_SET_MASK;
174 reg16 |= PWR_CTL_SET_D0;
175 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
176
177 /* Clear PCI 0xB0[14:13] */
178 reg32 = pci_read_config32(dev, 0xb0);
179 reg32 &= ~((1 << 14) | (1 << 13));
180 pci_write_config32(dev, 0xb0, reg32);
181
182 /* Clear MMIO 0x816c[14,2] */
183 reg32 = read32(mem_base + 0x816c);
184 reg32 &= ~((1 << 14) | (1 << 2));
185 write32(mem_base + 0x816c, reg32);
186
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700187 /* Reset disconnected USB3 ports */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700188 usb_xhci_reset_usb3(dev, 0);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700189
Duncan Laurie1f529082013-07-30 15:53:45 -0700190 /* Set MMIO 0x80e0[15] */
191 reg32 = read32(mem_base + 0x80e0);
192 reg32 |= (1 << 15);
193 write32(mem_base + 0x80e0, reg32);
194 }
195
196 /* Set D3Hot state and enable PME */
197 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_SET_D3);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700198 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_STATUS_PME);
Duncan Laurie1f529082013-07-30 15:53:45 -0700199 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_ENABLE_PME);
200}
201
Duncan Laurie911cedf2013-07-30 16:05:55 -0700202/* Route all ports to XHCI controller */
203void usb_xhci_route_all(void)
204{
205 u32 port_mask, route;
206 u16 reg16;
207
208 /* Skip if EHCI is already disabled */
209 if (RCBA32(FD) & PCH_DISABLE_EHCI1)
210 return;
211
212 /* Set D0 state */
213 reg16 = pci_read_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS);
214 reg16 &= ~PWR_CTL_SET_MASK;
215 reg16 |= PWR_CTL_SET_D0;
216 pci_write_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS, reg16);
217
218 /* Set USB3 superspeed enable */
219 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PRM);
220 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PR);
221 route &= ~XHCI_USB3PR_SSEN;
222 route |= XHCI_USB3PR_SSEN & port_mask;
223 pci_write_config32(PCH_XHCI_DEV, XHCI_USB3PR, route);
224
225 /* Route USB2 ports to XHCI controller */
226 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PRM);
227 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PR);
228 route &= ~XHCI_USB2PR_HCSEL;
229 route |= XHCI_USB2PR_HCSEL & port_mask;
230 pci_write_config32(PCH_XHCI_DEV, XHCI_USB2PR, route);
231
232 /* Disable EHCI controller */
233 usb_ehci_disable(PCH_EHCI1_DEV);
234
235 /* LynxPoint-H has a second EHCI controller */
236 if (!pch_is_lp())
237 usb_ehci_disable(PCH_EHCI2_DEV);
238
239 /* Reset and clear port change status */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700240 usb_xhci_reset_usb3(PCH_XHCI_DEV, 1);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700241}
242
Duncan Laurie1f529082013-07-30 15:53:45 -0700243#else /* !__SMM__ */
244
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700245static void usb_xhci_clock_gating(device_t dev)
246{
247 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700248 u16 reg16;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700249
250 /* IOBP 0xE5004001[7:6] = 11b */
251 pch_iobp_update(0xe5004001, ~0, (1 << 7)|(1 << 6));
252
253 reg32 = pci_read_config32(dev, 0x40);
254 reg32 &= ~(1 << 23); /* unsupported request */
255
256 if (pch_is_lp()) {
257 /* D20:F0:40h[18,17,8] = 111b */
258 reg32 |= (1 << 18) | (1 << 17) | (1 << 8);
259 /* D20:F0:40h[21,20,19] = 110b to enable XHCI Idle L1 */
260 reg32 &= ~(1 << 19);
261 reg32 |= (1 << 21) | (1 << 20);
262 } else {
263 /* D20:F0:40h[21,20,18,17,8] = 11111b */
264 reg32 |= (1 << 21)|(1 << 20)|(1 << 18)|(1 << 17)|(1 << 8);
265 }
266
267 /* Avoid writing upper byte as it is write-once */
268 pci_write_config16(dev, 0x40, (u16)(reg32 & 0xffff));
269 pci_write_config8(dev, 0x40 + 2, (u8)((reg32 >> 16) & 0xff));
270
271 /* D20:F0:44h[9,7,3] = 111b */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700272 reg16 = pci_read_config16(dev, 0x44);
273 reg16 |= (1 << 9) | (1 << 7) | (1 << 3);
274 pci_write_config16(dev, 0x44, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700275
276 reg32 = pci_read_config32(dev, 0xa0);
277 if (pch_is_lp()) {
278 /* D20:F0:A0h[18] = 1 */
279 reg32 |= (1 << 18);
280 } else {
281 /* D20:F0:A0h[6] = 1 */
282 reg32 |= (1 << 6);
283 }
284 pci_write_config32(dev, 0xa0, reg32);
285
286 /* D20:F0:A4h[13] = 0 */
287 reg32 = pci_read_config32(dev, 0xa4);
288 reg32 &= ~(1 << 13);
289 pci_write_config32(dev, 0xa4, reg32);
290}
291
292static void usb_xhci_init(device_t dev)
293{
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700294 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700295 u16 reg16;
296 u32 mem_base = usb_xhci_mem_base(dev);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700297
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700298 /* D20:F0:74h[1:0] = 00b (set D0 state) */
299 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
300 reg16 &= ~PWR_CTL_SET_MASK;
301 reg16 |= PWR_CTL_SET_D0;
302 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700303
304 /* Enable clock gating first */
305 usb_xhci_clock_gating(dev);
306
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700307 reg32 = read32(mem_base + 0x8144);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700308 if (pch_is_lp()) {
309 /* XHCIBAR + 8144h[8,7,6] = 111b */
310 reg32 |= (1 << 8) | (1 << 7) | (1 << 6);
311 } else {
312 /* XHCIBAR + 8144h[8,7,6] = 100b */
313 reg32 &= ~((1 << 7) | (1 << 6));
314 reg32 |= (1 << 8);
315 }
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700316 write32(mem_base + 0x8144, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700317
318 if (pch_is_lp()) {
Duncan Laurie527b03a2013-08-28 09:53:50 -0700319 /* XHCIBAR + 816Ch[19:0] = 000e0038h */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700320 reg32 = read32(mem_base + 0x816c);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700321 reg32 &= ~0x000fffff;
Duncan Laurie527b03a2013-08-28 09:53:50 -0700322 reg32 |= 0x000e0038;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700323 write32(mem_base + 0x816c, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700324
325 /* D20:F0:B0h[17,14,13] = 100b */
326 reg32 = pci_read_config32(dev, 0xb0);
327 reg32 &= ~((1 << 14) | (1 << 13));
328 reg32 |= (1 << 17);
329 pci_write_config32(dev, 0xb0, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700330 }
331
332 reg32 = pci_read_config32(dev, 0x50);
333 if (pch_is_lp()) {
334 /* D20:F0:50h[28:0] = 0FCE2E5Fh */
335 reg32 &= ~0x1fffffff;
336 reg32 |= 0x0fce2e5f;
337 } else {
338 /* D20:F0:50h[26:0] = 07886E9Fh */
339 reg32 &= ~0x07ffffff;
340 reg32 |= 0x07886e9f;
341 }
342 pci_write_config32(dev, 0x50, reg32);
343
344 /* D20:F0:44h[31] = 1 (Access Control Bit) */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700345 reg32 = pci_read_config32(dev, 0x44);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700346 reg32 |= (1 << 31);
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700347 pci_write_config32(dev, 0x44, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700348
349 /* D20:F0:40h[31,23] = 10b (OC Configuration Done) */
350 reg32 = pci_read_config32(dev, 0x40);
351 reg32 &= ~(1 << 23); /* unsupported request */
352 reg32 |= (1 << 31);
353 pci_write_config32(dev, 0x40, reg32);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700354
355 if (acpi_is_wakeup_s3()) {
356 /* Reset ports that are disabled or
357 * polling before returning to the OS. */
358 usb_xhci_reset_usb3(dev, 0);
359 }
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700360}
361
362static void usb_xhci_set_subsystem(device_t dev, unsigned vendor,
363 unsigned device)
364{
365 if (!vendor || !device) {
366 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
367 pci_read_config32(dev, PCI_VENDOR_ID));
368 } else {
369 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
370 ((device & 0xffff) << 16) | (vendor & 0xffff));
371 }
372}
373
374static struct pci_operations lops_pci = {
375 .set_subsystem = &usb_xhci_set_subsystem,
376};
377
378static struct device_operations usb_xhci_ops = {
379 .read_resources = pci_dev_read_resources,
380 .set_resources = pci_dev_set_resources,
381 .enable_resources = pci_dev_enable_resources,
382 .init = usb_xhci_init,
383 .ops_pci = &lops_pci,
384};
385
386static const unsigned short pci_device_ids[] = { 0x8c31, /* LynxPoint-H */
387 0x9c31, /* LynxPoint-LP */
388 0 };
389
390static const struct pci_driver pch_usb_xhci __pci_driver = {
391 .ops = &usb_xhci_ops,
392 .vendor = PCI_VENDOR_ID_INTEL,
393 .devices = pci_device_ids,
394};
Duncan Laurie1f529082013-07-30 15:53:45 -0700395#endif /* !__SMM__ */