blob: 00b8b8ca87a85c7a43a6f67333d17cf0941512d1 [file] [log] [blame]
Duncan Lauriec88c54c2014-04-30 16:36:13 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Duncan Lauriec88c54c2014-04-30 16:36:13 -070014 */
15
Duncan Lauriec88c54c2014-04-30 16:36:13 -070016#include <delay.h>
17#include <device/device.h>
18#include <device/pci.h>
19#include <device/pci_ids.h>
Aaron Durbin9e6d1432016-07-13 23:21:41 -050020#include <arch/acpi.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020021#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020022#include <device/pci_ops.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070023#include <soc/ramstage.h>
24#include <soc/xhci.h>
25#include <soc/cpu.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070026
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020027#ifdef __SIMPLE_DEVICE__
Elyes HAOUAS4658a982018-09-20 08:46:35 +020028static u8 *usb_xhci_mem_base(pci_devfn_t dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070029{
30 u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
31
32 /* Check if the controller is disabled or not present */
33 if (mem_base == 0 || mem_base == 0xffffffff)
34 return 0;
35
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080036 return (u8 *)(mem_base & ~0xf);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070037}
38
Elyes HAOUAS4658a982018-09-20 08:46:35 +020039static int usb_xhci_port_count_usb3(pci_devfn_t dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070040{
41 /* PCH-LP has 4 SS ports */
42 return 4;
43}
44
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080045static void usb_xhci_reset_status_usb3(u8 *mem_base, int port)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070046{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080047 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070048 u32 status = read32(portsc);
49 /* Do not set Port Enabled/Disabled field */
50 status &= ~XHCI_USB3_PORTSC_PED;
51 /* Clear all change status bits */
52 status |= XHCI_USB3_PORTSC_CHST;
53 write32(portsc, status);
54}
55
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080056static void usb_xhci_reset_port_usb3(u8 *mem_base, int port)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070057{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080058 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070059 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
60}
61
62#define XHCI_RESET_DELAY_US 1000 /* 1ms */
63#define XHCI_RESET_TIMEOUT 100 /* 100ms */
64
65/*
66 * 1) Wait until port is done polling
67 * 2) If port is disconnected
68 * a) Issue warm port reset
69 * b) Poll for warm reset complete
70 * c) Write 1 to port change status bits
71 */
Elyes HAOUAS4658a982018-09-20 08:46:35 +020072static void usb_xhci_reset_usb3(pci_devfn_t dev, int all)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070073{
74 u32 status, port_disabled;
75 int timeout, port;
76 int port_count = usb_xhci_port_count_usb3(dev);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080077 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070078
79 if (!mem_base || !port_count)
80 return;
81
82 /* Get mask of disabled ports */
83 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
84
85 /* Wait until all enabled ports are done polling */
86 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
87 int complete = 1;
88 for (port = 0; port < port_count; port++) {
89 /* Skip disabled ports */
90 if (port_disabled & (1 << port))
91 continue;
92 /* Read port link status field */
93 status = read32(mem_base + XHCI_USB3_PORTSC(port));
94 status &= XHCI_USB3_PORTSC_PLS;
95 if (status == XHCI_PLSR_POLLING)
96 complete = 0;
97 }
98 /* Exit if all ports not polling */
99 if (complete)
100 break;
101 udelay(XHCI_RESET_DELAY_US);
102 }
103
104 /* Reset all requested ports */
105 for (port = 0; port < port_count; port++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800106 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700107 /* Skip disabled ports */
108 if (port_disabled & (1 << port))
109 continue;
110 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
111 /* Reset all or only disconnected ports */
112 if (all || (status == XHCI_PLSR_RXDETECT ||
113 status == XHCI_PLSR_POLLING))
114 usb_xhci_reset_port_usb3(mem_base, port);
115 else
116 port_disabled |= 1 << port;
117 }
118
119 /* Wait for warm reset complete on all reset ports */
120 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
121 int complete = 1;
122 for (port = 0; port < port_count; port++) {
123 /* Only check ports that were reset */
124 if (port_disabled & (1 << port))
125 continue;
126 /* Check if warm reset is complete */
127 status = read32(mem_base + XHCI_USB3_PORTSC(port));
128 if (!(status & XHCI_USB3_PORTSC_WRC))
129 complete = 0;
130 }
131 /* Check for warm reset complete in any port */
132 if (complete)
133 break;
134 udelay(XHCI_RESET_DELAY_US);
135 }
136
137 /* Clear port change status bits */
138 for (port = 0; port < port_count; port++)
139 usb_xhci_reset_status_usb3(mem_base, port);
140}
141
142/* Handler for XHCI controller on entry to S3/S4/S5 */
Elyes HAOUAS4658a982018-09-20 08:46:35 +0200143void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700144{
145 u16 reg16;
146 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800147 u8 *mem_base = usb_xhci_mem_base(dev);
Kane Chen46134722014-08-28 17:05:06 -0700148 u8 is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700149
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500150 if (!mem_base || slp_typ < ACPI_S3)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700151 return;
152
153 /* Set D0 state */
154 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
155 reg16 &= ~XHCI_PWR_CTL_SET_MASK;
156 reg16 |= XHCI_PWR_CTL_SET_D0;
157 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
158
Kane Chen46134722014-08-28 17:05:06 -0700159 if (!is_broadwell) {
160 /* This WA is only for lpt */
161
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200162 /* Clear PCI 0xB0[14:13] */
163 reg32 = pci_read_config32(dev, 0xb0);
164 reg32 &= ~((1 << 14) | (1 << 13));
165 pci_write_config32(dev, 0xb0, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700166
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200167 /* Clear MMIO 0x816c[14,2] */
168 reg32 = read32(mem_base + 0x816c);
169 reg32 &= ~((1 << 14) | (1 << 2));
170 write32(mem_base + 0x816c, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700171
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200172 /* Reset disconnected USB3 ports */
173 usb_xhci_reset_usb3(dev, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700174
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200175 /* Set MMIO 0x80e0[15] */
176 reg32 = read32(mem_base + 0x80e0);
177 reg32 |= (1 << 15);
178 write32(mem_base + 0x80e0, reg32);
Todd Brochdf4081e2015-02-06 17:13:53 -0800179 } else {
180 /*
181 * Clear port change status bits. Clearing CSC alone seemed to
182 * fix wakeup from S3 if entering USB compliance state even if
183 * bit wasn't set on the port.
184 */
185 int port;
186 for (port = 0; port < usb_xhci_port_count_usb3(dev); port++)
187 usb_xhci_reset_status_usb3(mem_base, port);
Kane Chen46134722014-08-28 17:05:06 -0700188 }
189
190 reg32 = read32(mem_base + 0x8154);
191 reg32 &= ~(1 << 31);
192 write32(mem_base + 0x8154, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700193
194 /* Set D3Hot state and enable PME */
195 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_SET_D3);
196 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_STATUS_PME);
197 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_ENABLE_PME);
198}
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200199#else /* !__SIMPLE_DEVICE__ */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700200
Duncan Laurieaafdddf2015-02-19 16:21:10 -0800201static void xhci_init(struct device *dev)
202{
203 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
204 u16 reg16;
205 u32 reg32;
206
207 /* Ensure controller is in D0 state */
208 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
209 reg16 &= ~XHCI_PWR_CTL_SET_MASK;
210 reg16 |= XHCI_PWR_CTL_SET_D0;
211 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
212
213 /* Disable Compliance Mode Entry */
214 reg32 = read32(res2mmio(res, 0x80ec, 0));
215 reg32 |= (1 << 0);
216 write32(res2mmio(res, 0x80ec, 0), reg32);
217}
218
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700219static struct device_operations usb_xhci_ops = {
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100220 .read_resources = pci_dev_read_resources,
221 .set_resources = pci_dev_set_resources,
222 .enable_resources = pci_dev_enable_resources,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700223 .ops_pci = &broadwell_pci_ops,
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100224 .init = xhci_init,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700225};
226
227static const unsigned short pci_device_ids[] = {
228 0x9c31, /* LynxPoint-LP */
229 0x9cb1, /* WildcatPoint */
230 0
231};
232
233static const struct pci_driver pch_usb_xhci __pci_driver = {
234 .ops = &usb_xhci_ops,
235 .vendor = PCI_VENDOR_ID_INTEL,
236 .devices = pci_device_ids,
237};
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200238#endif /* !__SIMPLE_DEVICE__ */