blob: 319c9b125ba2d85f5130a4ffbb816b968d74c1ff [file] [log] [blame]
Angel Ponsf94ac9a2020-04-05 15:46:48 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Lauriec88c54c2014-04-30 16:36:13 -07002
Duncan Lauriec88c54c2014-04-30 16:36:13 -07003#include <delay.h>
4#include <device/device.h>
5#include <device/pci.h>
6#include <device/pci_ids.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07007#include <acpi/acpi.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070010#include <soc/ramstage.h>
11#include <soc/xhci.h>
12#include <soc/cpu.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070013
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020014#ifdef __SIMPLE_DEVICE__
Elyes HAOUAS4658a982018-09-20 08:46:35 +020015static u8 *usb_xhci_mem_base(pci_devfn_t dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070016{
17 u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
18
19 /* Check if the controller is disabled or not present */
20 if (mem_base == 0 || mem_base == 0xffffffff)
21 return 0;
22
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080023 return (u8 *)(mem_base & ~0xf);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070024}
25
Elyes HAOUAS4658a982018-09-20 08:46:35 +020026static int usb_xhci_port_count_usb3(pci_devfn_t dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070027{
28 /* PCH-LP has 4 SS ports */
29 return 4;
30}
31
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080032static void usb_xhci_reset_status_usb3(u8 *mem_base, int port)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070033{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080034 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070035 u32 status = read32(portsc);
36 /* Do not set Port Enabled/Disabled field */
37 status &= ~XHCI_USB3_PORTSC_PED;
38 /* Clear all change status bits */
39 status |= XHCI_USB3_PORTSC_CHST;
40 write32(portsc, status);
41}
42
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080043static void usb_xhci_reset_port_usb3(u8 *mem_base, int port)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070044{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080045 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070046 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
47}
48
49#define XHCI_RESET_DELAY_US 1000 /* 1ms */
50#define XHCI_RESET_TIMEOUT 100 /* 100ms */
51
52/*
53 * 1) Wait until port is done polling
54 * 2) If port is disconnected
55 * a) Issue warm port reset
56 * b) Poll for warm reset complete
57 * c) Write 1 to port change status bits
58 */
Elyes HAOUAS4658a982018-09-20 08:46:35 +020059static void usb_xhci_reset_usb3(pci_devfn_t dev, int all)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070060{
61 u32 status, port_disabled;
62 int timeout, port;
63 int port_count = usb_xhci_port_count_usb3(dev);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080064 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070065
66 if (!mem_base || !port_count)
67 return;
68
69 /* Get mask of disabled ports */
70 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
71
72 /* Wait until all enabled ports are done polling */
73 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
74 int complete = 1;
75 for (port = 0; port < port_count; port++) {
76 /* Skip disabled ports */
77 if (port_disabled & (1 << port))
78 continue;
79 /* Read port link status field */
80 status = read32(mem_base + XHCI_USB3_PORTSC(port));
81 status &= XHCI_USB3_PORTSC_PLS;
82 if (status == XHCI_PLSR_POLLING)
83 complete = 0;
84 }
85 /* Exit if all ports not polling */
86 if (complete)
87 break;
88 udelay(XHCI_RESET_DELAY_US);
89 }
90
91 /* Reset all requested ports */
92 for (port = 0; port < port_count; port++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080093 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070094 /* Skip disabled ports */
95 if (port_disabled & (1 << port))
96 continue;
97 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
98 /* Reset all or only disconnected ports */
99 if (all || (status == XHCI_PLSR_RXDETECT ||
100 status == XHCI_PLSR_POLLING))
101 usb_xhci_reset_port_usb3(mem_base, port);
102 else
103 port_disabled |= 1 << port;
104 }
105
106 /* Wait for warm reset complete on all reset ports */
107 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
108 int complete = 1;
109 for (port = 0; port < port_count; port++) {
110 /* Only check ports that were reset */
111 if (port_disabled & (1 << port))
112 continue;
113 /* Check if warm reset is complete */
114 status = read32(mem_base + XHCI_USB3_PORTSC(port));
115 if (!(status & XHCI_USB3_PORTSC_WRC))
116 complete = 0;
117 }
118 /* Check for warm reset complete in any port */
119 if (complete)
120 break;
121 udelay(XHCI_RESET_DELAY_US);
122 }
123
124 /* Clear port change status bits */
125 for (port = 0; port < port_count; port++)
126 usb_xhci_reset_status_usb3(mem_base, port);
127}
128
129/* Handler for XHCI controller on entry to S3/S4/S5 */
Elyes HAOUAS4658a982018-09-20 08:46:35 +0200130void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700131{
132 u16 reg16;
133 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800134 u8 *mem_base = usb_xhci_mem_base(dev);
Kane Chen46134722014-08-28 17:05:06 -0700135 u8 is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700136
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500137 if (!mem_base || slp_typ < ACPI_S3)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700138 return;
139
140 /* Set D0 state */
141 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
142 reg16 &= ~XHCI_PWR_CTL_SET_MASK;
143 reg16 |= XHCI_PWR_CTL_SET_D0;
144 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
145
Kane Chen46134722014-08-28 17:05:06 -0700146 if (!is_broadwell) {
147 /* This WA is only for lpt */
148
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200149 /* Clear PCI 0xB0[14:13] */
150 reg32 = pci_read_config32(dev, 0xb0);
151 reg32 &= ~((1 << 14) | (1 << 13));
152 pci_write_config32(dev, 0xb0, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700153
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200154 /* Clear MMIO 0x816c[14,2] */
155 reg32 = read32(mem_base + 0x816c);
156 reg32 &= ~((1 << 14) | (1 << 2));
157 write32(mem_base + 0x816c, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700158
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200159 /* Reset disconnected USB3 ports */
160 usb_xhci_reset_usb3(dev, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700161
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200162 /* Set MMIO 0x80e0[15] */
163 reg32 = read32(mem_base + 0x80e0);
164 reg32 |= (1 << 15);
165 write32(mem_base + 0x80e0, reg32);
Todd Brochdf4081e2015-02-06 17:13:53 -0800166 } else {
167 /*
168 * Clear port change status bits. Clearing CSC alone seemed to
169 * fix wakeup from S3 if entering USB compliance state even if
170 * bit wasn't set on the port.
171 */
172 int port;
173 for (port = 0; port < usb_xhci_port_count_usb3(dev); port++)
174 usb_xhci_reset_status_usb3(mem_base, port);
Kane Chen46134722014-08-28 17:05:06 -0700175 }
176
177 reg32 = read32(mem_base + 0x8154);
178 reg32 &= ~(1 << 31);
179 write32(mem_base + 0x8154, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700180
181 /* Set D3Hot state and enable PME */
182 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_SET_D3);
183 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_STATUS_PME);
184 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_ENABLE_PME);
185}
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200186#else /* !__SIMPLE_DEVICE__ */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700187
Duncan Laurieaafdddf2015-02-19 16:21:10 -0800188static void xhci_init(struct device *dev)
189{
190 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
191 u16 reg16;
192 u32 reg32;
193
194 /* Ensure controller is in D0 state */
195 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
196 reg16 &= ~XHCI_PWR_CTL_SET_MASK;
197 reg16 |= XHCI_PWR_CTL_SET_D0;
198 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
199
200 /* Disable Compliance Mode Entry */
201 reg32 = read32(res2mmio(res, 0x80ec, 0));
202 reg32 |= (1 << 0);
203 write32(res2mmio(res, 0x80ec, 0), reg32);
204}
205
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700206static struct device_operations usb_xhci_ops = {
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100207 .read_resources = pci_dev_read_resources,
208 .set_resources = pci_dev_set_resources,
209 .enable_resources = pci_dev_enable_resources,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700210 .ops_pci = &broadwell_pci_ops,
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100211 .init = xhci_init,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700212};
213
214static const unsigned short pci_device_ids[] = {
215 0x9c31, /* LynxPoint-LP */
216 0x9cb1, /* WildcatPoint */
217 0
218};
219
220static const struct pci_driver pch_usb_xhci __pci_driver = {
221 .ops = &usb_xhci_ops,
222 .vendor = PCI_VENDOR_ID_INTEL,
223 .devices = pci_device_ids,
224};
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200225#endif /* !__SIMPLE_DEVICE__ */