blob: 10fa9214b9929ac8748b19de3cb677fc1f26f7ba [file] [log] [blame]
Angel Ponsf94ac9a2020-04-05 15:46:48 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Duncan Lauriec88c54c2014-04-30 16:36:13 -07003
Duncan Lauriec88c54c2014-04-30 16:36:13 -07004#include <delay.h>
5#include <device/device.h>
6#include <device/pci.h>
7#include <device/pci_ids.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07008#include <acpi/acpi.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02009#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020010#include <device/pci_ops.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070011#include <soc/ramstage.h>
12#include <soc/xhci.h>
13#include <soc/cpu.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070014
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020015#ifdef __SIMPLE_DEVICE__
Elyes HAOUAS4658a982018-09-20 08:46:35 +020016static u8 *usb_xhci_mem_base(pci_devfn_t dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070017{
18 u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
19
20 /* Check if the controller is disabled or not present */
21 if (mem_base == 0 || mem_base == 0xffffffff)
22 return 0;
23
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080024 return (u8 *)(mem_base & ~0xf);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070025}
26
Elyes HAOUAS4658a982018-09-20 08:46:35 +020027static int usb_xhci_port_count_usb3(pci_devfn_t dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070028{
29 /* PCH-LP has 4 SS ports */
30 return 4;
31}
32
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080033static void usb_xhci_reset_status_usb3(u8 *mem_base, int port)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070034{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080035 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070036 u32 status = read32(portsc);
37 /* Do not set Port Enabled/Disabled field */
38 status &= ~XHCI_USB3_PORTSC_PED;
39 /* Clear all change status bits */
40 status |= XHCI_USB3_PORTSC_CHST;
41 write32(portsc, status);
42}
43
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080044static void usb_xhci_reset_port_usb3(u8 *mem_base, int port)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070045{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080046 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070047 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
48}
49
50#define XHCI_RESET_DELAY_US 1000 /* 1ms */
51#define XHCI_RESET_TIMEOUT 100 /* 100ms */
52
53/*
54 * 1) Wait until port is done polling
55 * 2) If port is disconnected
56 * a) Issue warm port reset
57 * b) Poll for warm reset complete
58 * c) Write 1 to port change status bits
59 */
Elyes HAOUAS4658a982018-09-20 08:46:35 +020060static void usb_xhci_reset_usb3(pci_devfn_t dev, int all)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070061{
62 u32 status, port_disabled;
63 int timeout, port;
64 int port_count = usb_xhci_port_count_usb3(dev);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080065 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070066
67 if (!mem_base || !port_count)
68 return;
69
70 /* Get mask of disabled ports */
71 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
72
73 /* Wait until all enabled ports are done polling */
74 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
75 int complete = 1;
76 for (port = 0; port < port_count; port++) {
77 /* Skip disabled ports */
78 if (port_disabled & (1 << port))
79 continue;
80 /* Read port link status field */
81 status = read32(mem_base + XHCI_USB3_PORTSC(port));
82 status &= XHCI_USB3_PORTSC_PLS;
83 if (status == XHCI_PLSR_POLLING)
84 complete = 0;
85 }
86 /* Exit if all ports not polling */
87 if (complete)
88 break;
89 udelay(XHCI_RESET_DELAY_US);
90 }
91
92 /* Reset all requested ports */
93 for (port = 0; port < port_count; port++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080094 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070095 /* Skip disabled ports */
96 if (port_disabled & (1 << port))
97 continue;
98 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
99 /* Reset all or only disconnected ports */
100 if (all || (status == XHCI_PLSR_RXDETECT ||
101 status == XHCI_PLSR_POLLING))
102 usb_xhci_reset_port_usb3(mem_base, port);
103 else
104 port_disabled |= 1 << port;
105 }
106
107 /* Wait for warm reset complete on all reset ports */
108 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
109 int complete = 1;
110 for (port = 0; port < port_count; port++) {
111 /* Only check ports that were reset */
112 if (port_disabled & (1 << port))
113 continue;
114 /* Check if warm reset is complete */
115 status = read32(mem_base + XHCI_USB3_PORTSC(port));
116 if (!(status & XHCI_USB3_PORTSC_WRC))
117 complete = 0;
118 }
119 /* Check for warm reset complete in any port */
120 if (complete)
121 break;
122 udelay(XHCI_RESET_DELAY_US);
123 }
124
125 /* Clear port change status bits */
126 for (port = 0; port < port_count; port++)
127 usb_xhci_reset_status_usb3(mem_base, port);
128}
129
130/* Handler for XHCI controller on entry to S3/S4/S5 */
Elyes HAOUAS4658a982018-09-20 08:46:35 +0200131void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700132{
133 u16 reg16;
134 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800135 u8 *mem_base = usb_xhci_mem_base(dev);
Kane Chen46134722014-08-28 17:05:06 -0700136 u8 is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700137
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500138 if (!mem_base || slp_typ < ACPI_S3)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700139 return;
140
141 /* Set D0 state */
142 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
143 reg16 &= ~XHCI_PWR_CTL_SET_MASK;
144 reg16 |= XHCI_PWR_CTL_SET_D0;
145 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
146
Kane Chen46134722014-08-28 17:05:06 -0700147 if (!is_broadwell) {
148 /* This WA is only for lpt */
149
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200150 /* Clear PCI 0xB0[14:13] */
151 reg32 = pci_read_config32(dev, 0xb0);
152 reg32 &= ~((1 << 14) | (1 << 13));
153 pci_write_config32(dev, 0xb0, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700154
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200155 /* Clear MMIO 0x816c[14,2] */
156 reg32 = read32(mem_base + 0x816c);
157 reg32 &= ~((1 << 14) | (1 << 2));
158 write32(mem_base + 0x816c, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700159
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200160 /* Reset disconnected USB3 ports */
161 usb_xhci_reset_usb3(dev, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700162
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200163 /* Set MMIO 0x80e0[15] */
164 reg32 = read32(mem_base + 0x80e0);
165 reg32 |= (1 << 15);
166 write32(mem_base + 0x80e0, reg32);
Todd Brochdf4081e2015-02-06 17:13:53 -0800167 } else {
168 /*
169 * Clear port change status bits. Clearing CSC alone seemed to
170 * fix wakeup from S3 if entering USB compliance state even if
171 * bit wasn't set on the port.
172 */
173 int port;
174 for (port = 0; port < usb_xhci_port_count_usb3(dev); port++)
175 usb_xhci_reset_status_usb3(mem_base, port);
Kane Chen46134722014-08-28 17:05:06 -0700176 }
177
178 reg32 = read32(mem_base + 0x8154);
179 reg32 &= ~(1 << 31);
180 write32(mem_base + 0x8154, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700181
182 /* Set D3Hot state and enable PME */
183 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_SET_D3);
184 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_STATUS_PME);
185 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_ENABLE_PME);
186}
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200187#else /* !__SIMPLE_DEVICE__ */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700188
Duncan Laurieaafdddf2015-02-19 16:21:10 -0800189static void xhci_init(struct device *dev)
190{
191 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
192 u16 reg16;
193 u32 reg32;
194
195 /* Ensure controller is in D0 state */
196 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
197 reg16 &= ~XHCI_PWR_CTL_SET_MASK;
198 reg16 |= XHCI_PWR_CTL_SET_D0;
199 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
200
201 /* Disable Compliance Mode Entry */
202 reg32 = read32(res2mmio(res, 0x80ec, 0));
203 reg32 |= (1 << 0);
204 write32(res2mmio(res, 0x80ec, 0), reg32);
205}
206
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700207static struct device_operations usb_xhci_ops = {
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100208 .read_resources = pci_dev_read_resources,
209 .set_resources = pci_dev_set_resources,
210 .enable_resources = pci_dev_enable_resources,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700211 .ops_pci = &broadwell_pci_ops,
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100212 .init = xhci_init,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700213};
214
215static const unsigned short pci_device_ids[] = {
216 0x9c31, /* LynxPoint-LP */
217 0x9cb1, /* WildcatPoint */
218 0
219};
220
221static const struct pci_driver pch_usb_xhci __pci_driver = {
222 .ops = &usb_xhci_ops,
223 .vendor = PCI_VENDOR_ID_INTEL,
224 .devices = pci_device_ids,
225};
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200226#endif /* !__SIMPLE_DEVICE__ */