blob: 3b8c23b27a0a2570c2dac34b19ac1b5b20cb399f [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
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -070029typedef struct southbridge_intel_lynxpoint_config config_t;
30
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080031static u8 *usb_xhci_mem_base(device_t dev)
Duncan Laurie1f529082013-07-30 15:53:45 -070032{
33 u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
34
35 /* Check if the controller is disabled or not present */
36 if (mem_base == 0 || mem_base == 0xffffffff)
37 return 0;
38
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080039 return (u8 *)(mem_base & ~0xf);
Duncan Laurie1f529082013-07-30 15:53:45 -070040}
41
Duncan Laurie1f529082013-07-30 15:53:45 -070042static int usb_xhci_port_count_usb3(device_t dev)
43{
44 if (pch_is_lp()) {
45 /* LynxPoint-LP has 4 SS ports */
46 return 4;
47 } else {
48 /* LynxPoint-H can have 0, 2, 4, or 6 SS ports */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080049 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -070050 u32 fus = read32(mem_base + XHCI_USB3FUS);
51 fus >>= XHCI_USB3FUS_SS_SHIFT;
52 fus &= XHCI_USB3FUS_SS_MASK;
53 switch (fus) {
54 case 3: return 0;
55 case 2: return 2;
56 case 1: return 4;
57 case 0: default: return 6;
58 }
59 }
60 return 0;
61}
62
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080063static void usb_xhci_reset_status_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070064{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080065 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -070066 u32 status = read32(portsc);
67 /* Do not set Port Enabled/Disabled field */
68 status &= ~XHCI_USB3_PORTSC_PED;
69 /* Clear all change status bits */
70 status |= XHCI_USB3_PORTSC_CHST;
71 write32(portsc, status);
Duncan Laurie1f529082013-07-30 15:53:45 -070072}
73
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080074static void usb_xhci_reset_port_usb3(u8 *mem_base, int port)
Duncan Laurie1f529082013-07-30 15:53:45 -070075{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080076 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -070077 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
78}
79
80#define XHCI_RESET_DELAY_US 1000 /* 1ms */
81#define XHCI_RESET_TIMEOUT 100 /* 100ms */
82
83/*
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 Laurie16a0f5c2013-09-25 14:08:16 -070090static void usb_xhci_reset_usb3(device_t dev, int all)
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);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080095 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -070096
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++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800124 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Laurie1f529082013-07-30 15:53:45 -0700125 /* 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 Laurie32d2e2b2013-09-25 14:08:32 -0700130 if (all || (status == XHCI_PLSR_RXDETECT ||
131 status == XHCI_PLSR_POLLING))
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700132 usb_xhci_reset_port_usb3(mem_base, port);
133 else
134 port_disabled |= 1 << port;
Duncan Laurie1f529082013-07-30 15:53:45 -0700135 }
136
137 /* Wait for warm reset complete on all reset ports */
138 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
139 int complete = 1;
140 for (port = 0; port < port_count; port++) {
141 /* Only check ports that were reset */
142 if (port_disabled & (1 << port))
143 continue;
144 /* Check if warm reset is complete */
145 status = read32(mem_base + XHCI_USB3_PORTSC(port));
146 if (!(status & XHCI_USB3_PORTSC_WRC))
147 complete = 0;
148 }
149 /* Check for warm reset complete in any port */
150 if (complete)
151 break;
152 udelay(XHCI_RESET_DELAY_US);
153 }
154
155 /* Clear port change status bits */
156 for (port = 0; port < port_count; port++)
157 usb_xhci_reset_status_usb3(mem_base, port);
158}
159
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700160#ifdef __SMM__
161
Duncan Laurie1f529082013-07-30 15:53:45 -0700162/* Handler for XHCI controller on entry to S3/S4/S5 */
163void usb_xhci_sleep_prepare(device_t dev, u8 slp_typ)
164{
165 u16 reg16;
166 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800167 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Laurie1f529082013-07-30 15:53:45 -0700168
169 if (!mem_base || slp_typ < 3)
170 return;
171
172 if (pch_is_lp()) {
173 /* Set D0 state */
174 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
175 reg16 &= ~PWR_CTL_SET_MASK;
176 reg16 |= PWR_CTL_SET_D0;
177 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
178
179 /* Clear PCI 0xB0[14:13] */
180 reg32 = pci_read_config32(dev, 0xb0);
181 reg32 &= ~((1 << 14) | (1 << 13));
182 pci_write_config32(dev, 0xb0, reg32);
183
184 /* Clear MMIO 0x816c[14,2] */
185 reg32 = read32(mem_base + 0x816c);
186 reg32 &= ~((1 << 14) | (1 << 2));
187 write32(mem_base + 0x816c, reg32);
188
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700189 /* Reset disconnected USB3 ports */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700190 usb_xhci_reset_usb3(dev, 0);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700191
Duncan Laurie1f529082013-07-30 15:53:45 -0700192 /* Set MMIO 0x80e0[15] */
193 reg32 = read32(mem_base + 0x80e0);
194 reg32 |= (1 << 15);
195 write32(mem_base + 0x80e0, reg32);
196 }
197
198 /* Set D3Hot state and enable PME */
199 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_SET_D3);
Duncan Laurie0bf1dea2013-08-13 13:32:28 -0700200 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_STATUS_PME);
Duncan Laurie1f529082013-07-30 15:53:45 -0700201 pci_or_config16(dev, XHCI_PWR_CTL_STS, PWR_CTL_ENABLE_PME);
202}
203
Duncan Laurie911cedf2013-07-30 16:05:55 -0700204/* Route all ports to XHCI controller */
205void usb_xhci_route_all(void)
206{
207 u32 port_mask, route;
208 u16 reg16;
209
210 /* Skip if EHCI is already disabled */
211 if (RCBA32(FD) & PCH_DISABLE_EHCI1)
212 return;
213
214 /* Set D0 state */
215 reg16 = pci_read_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS);
216 reg16 &= ~PWR_CTL_SET_MASK;
217 reg16 |= PWR_CTL_SET_D0;
218 pci_write_config16(PCH_XHCI_DEV, XHCI_PWR_CTL_STS, reg16);
219
220 /* Set USB3 superspeed enable */
221 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PRM);
222 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB3PR);
223 route &= ~XHCI_USB3PR_SSEN;
224 route |= XHCI_USB3PR_SSEN & port_mask;
225 pci_write_config32(PCH_XHCI_DEV, XHCI_USB3PR, route);
226
227 /* Route USB2 ports to XHCI controller */
228 port_mask = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PRM);
229 route = pci_read_config32(PCH_XHCI_DEV, XHCI_USB2PR);
230 route &= ~XHCI_USB2PR_HCSEL;
231 route |= XHCI_USB2PR_HCSEL & port_mask;
232 pci_write_config32(PCH_XHCI_DEV, XHCI_USB2PR, route);
233
234 /* Disable EHCI controller */
235 usb_ehci_disable(PCH_EHCI1_DEV);
236
237 /* LynxPoint-H has a second EHCI controller */
238 if (!pch_is_lp())
239 usb_ehci_disable(PCH_EHCI2_DEV);
240
241 /* Reset and clear port change status */
Duncan Laurie16a0f5c2013-09-25 14:08:16 -0700242 usb_xhci_reset_usb3(PCH_XHCI_DEV, 1);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700243}
244
Duncan Laurie1f529082013-07-30 15:53:45 -0700245#else /* !__SMM__ */
246
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700247static void usb_xhci_clock_gating(device_t dev)
248{
249 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700250 u16 reg16;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700251
252 /* IOBP 0xE5004001[7:6] = 11b */
253 pch_iobp_update(0xe5004001, ~0, (1 << 7)|(1 << 6));
254
255 reg32 = pci_read_config32(dev, 0x40);
256 reg32 &= ~(1 << 23); /* unsupported request */
257
258 if (pch_is_lp()) {
259 /* D20:F0:40h[18,17,8] = 111b */
260 reg32 |= (1 << 18) | (1 << 17) | (1 << 8);
261 /* D20:F0:40h[21,20,19] = 110b to enable XHCI Idle L1 */
262 reg32 &= ~(1 << 19);
263 reg32 |= (1 << 21) | (1 << 20);
264 } else {
265 /* D20:F0:40h[21,20,18,17,8] = 11111b */
266 reg32 |= (1 << 21)|(1 << 20)|(1 << 18)|(1 << 17)|(1 << 8);
267 }
268
269 /* Avoid writing upper byte as it is write-once */
270 pci_write_config16(dev, 0x40, (u16)(reg32 & 0xffff));
271 pci_write_config8(dev, 0x40 + 2, (u8)((reg32 >> 16) & 0xff));
272
273 /* D20:F0:44h[9,7,3] = 111b */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700274 reg16 = pci_read_config16(dev, 0x44);
275 reg16 |= (1 << 9) | (1 << 7) | (1 << 3);
276 pci_write_config16(dev, 0x44, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700277
278 reg32 = pci_read_config32(dev, 0xa0);
279 if (pch_is_lp()) {
280 /* D20:F0:A0h[18] = 1 */
281 reg32 |= (1 << 18);
282 } else {
283 /* D20:F0:A0h[6] = 1 */
284 reg32 |= (1 << 6);
285 }
286 pci_write_config32(dev, 0xa0, reg32);
287
288 /* D20:F0:A4h[13] = 0 */
289 reg32 = pci_read_config32(dev, 0xa4);
290 reg32 &= ~(1 << 13);
291 pci_write_config32(dev, 0xa4, reg32);
292}
293
294static void usb_xhci_init(device_t dev)
295{
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700296 u32 reg32;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700297 u16 reg16;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800298 u8 *mem_base = usb_xhci_mem_base(dev);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700299 config_t *config = dev->chip_info;
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700300
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700301 /* D20:F0:74h[1:0] = 00b (set D0 state) */
302 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
303 reg16 &= ~PWR_CTL_SET_MASK;
304 reg16 |= PWR_CTL_SET_D0;
305 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700306
307 /* Enable clock gating first */
308 usb_xhci_clock_gating(dev);
309
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700310 reg32 = read32(mem_base + 0x8144);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700311 if (pch_is_lp()) {
312 /* XHCIBAR + 8144h[8,7,6] = 111b */
313 reg32 |= (1 << 8) | (1 << 7) | (1 << 6);
314 } else {
315 /* XHCIBAR + 8144h[8,7,6] = 100b */
316 reg32 &= ~((1 << 7) | (1 << 6));
317 reg32 |= (1 << 8);
318 }
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700319 write32(mem_base + 0x8144, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700320
321 if (pch_is_lp()) {
Duncan Laurie527b03a2013-08-28 09:53:50 -0700322 /* XHCIBAR + 816Ch[19:0] = 000e0038h */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700323 reg32 = read32(mem_base + 0x816c);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700324 reg32 &= ~0x000fffff;
Duncan Laurie527b03a2013-08-28 09:53:50 -0700325 reg32 |= 0x000e0038;
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700326 write32(mem_base + 0x816c, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700327
328 /* D20:F0:B0h[17,14,13] = 100b */
329 reg32 = pci_read_config32(dev, 0xb0);
330 reg32 &= ~((1 << 14) | (1 << 13));
331 reg32 |= (1 << 17);
332 pci_write_config32(dev, 0xb0, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700333 }
334
335 reg32 = pci_read_config32(dev, 0x50);
336 if (pch_is_lp()) {
337 /* D20:F0:50h[28:0] = 0FCE2E5Fh */
338 reg32 &= ~0x1fffffff;
339 reg32 |= 0x0fce2e5f;
340 } else {
341 /* D20:F0:50h[26:0] = 07886E9Fh */
342 reg32 &= ~0x07ffffff;
343 reg32 |= 0x07886e9f;
344 }
345 pci_write_config32(dev, 0x50, reg32);
346
347 /* D20:F0:44h[31] = 1 (Access Control Bit) */
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700348 reg32 = pci_read_config32(dev, 0x44);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700349 reg32 |= (1 << 31);
Duncan Laurie0dcb5772013-07-30 15:58:18 -0700350 pci_write_config32(dev, 0x44, reg32);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700351
352 /* D20:F0:40h[31,23] = 10b (OC Configuration Done) */
353 reg32 = pci_read_config32(dev, 0x40);
354 reg32 &= ~(1 << 23); /* unsupported request */
355 reg32 |= (1 << 31);
356 pci_write_config32(dev, 0x40, reg32);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700357
358 if (acpi_is_wakeup_s3()) {
359 /* Reset ports that are disabled or
360 * polling before returning to the OS. */
361 usb_xhci_reset_usb3(dev, 0);
Stefan Reinauer6dbbe2e2013-10-17 17:00:26 -0700362 } else if (config->xhci_default) {
363 /* Route all ports to XHCI */
364 outb(0xca, 0xb2);
Duncan Laurie32d2e2b2013-09-25 14:08:32 -0700365 }
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700366}
367
368static void usb_xhci_set_subsystem(device_t dev, unsigned vendor,
369 unsigned device)
370{
371 if (!vendor || !device) {
372 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
373 pci_read_config32(dev, PCI_VENDOR_ID));
374 } else {
375 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
376 ((device & 0xffff) << 16) | (vendor & 0xffff));
377 }
378}
379
380static struct pci_operations lops_pci = {
381 .set_subsystem = &usb_xhci_set_subsystem,
382};
383
384static struct device_operations usb_xhci_ops = {
385 .read_resources = pci_dev_read_resources,
386 .set_resources = pci_dev_set_resources,
387 .enable_resources = pci_dev_enable_resources,
388 .init = usb_xhci_init,
389 .ops_pci = &lops_pci,
390};
391
392static const unsigned short pci_device_ids[] = { 0x8c31, /* LynxPoint-H */
393 0x9c31, /* LynxPoint-LP */
394 0 };
395
396static const struct pci_driver pch_usb_xhci __pci_driver = {
397 .ops = &usb_xhci_ops,
398 .vendor = PCI_VENDOR_ID_INTEL,
399 .devices = pci_device_ids,
400};
Duncan Laurie1f529082013-07-30 15:53:45 -0700401#endif /* !__SMM__ */