blob: fd36fad4a07e3317d8d0605b7b202bff418408eb [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
Angel Pons9d733de2020-11-23 13:15:19 +01003#include <cpu/intel/haswell/haswell.h>
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>
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{
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700132 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800133 u8 *mem_base = usb_xhci_mem_base(dev);
Kane Chen46134722014-08-28 17:05:06 -0700134 u8 is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700135
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500136 if (!mem_base || slp_typ < ACPI_S3)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700137 return;
138
139 /* Set D0 state */
Angel Pons8b8b2712020-09-25 01:04:51 +0200140 pci_update_config16(dev, XHCI_PWR_CTL_STS, ~XHCI_PWR_CTL_SET_MASK, XHCI_PWR_CTL_SET_D0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700141
Kane Chen46134722014-08-28 17:05:06 -0700142 if (!is_broadwell) {
143 /* This WA is only for lpt */
144
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200145 /* Clear PCI 0xB0[14:13] */
Angel Pons8b8b2712020-09-25 01:04:51 +0200146 pci_and_config32(dev, 0xb0, ~((1 << 14) | (1 << 13)));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700147
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200148 /* Clear MMIO 0x816c[14,2] */
149 reg32 = read32(mem_base + 0x816c);
150 reg32 &= ~((1 << 14) | (1 << 2));
151 write32(mem_base + 0x816c, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700152
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200153 /* Reset disconnected USB3 ports */
154 usb_xhci_reset_usb3(dev, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700155
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200156 /* Set MMIO 0x80e0[15] */
157 reg32 = read32(mem_base + 0x80e0);
158 reg32 |= (1 << 15);
159 write32(mem_base + 0x80e0, reg32);
Todd Brochdf4081e2015-02-06 17:13:53 -0800160 } else {
161 /*
162 * Clear port change status bits. Clearing CSC alone seemed to
163 * fix wakeup from S3 if entering USB compliance state even if
164 * bit wasn't set on the port.
165 */
166 int port;
167 for (port = 0; port < usb_xhci_port_count_usb3(dev); port++)
168 usb_xhci_reset_status_usb3(mem_base, port);
Kane Chen46134722014-08-28 17:05:06 -0700169 }
170
171 reg32 = read32(mem_base + 0x8154);
172 reg32 &= ~(1 << 31);
173 write32(mem_base + 0x8154, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700174
175 /* Set D3Hot state and enable PME */
176 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_SET_D3);
177 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_STATUS_PME);
178 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_ENABLE_PME);
179}
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200180#else /* !__SIMPLE_DEVICE__ */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700181
Duncan Laurieaafdddf2015-02-19 16:21:10 -0800182static void xhci_init(struct device *dev)
183{
184 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
185 u16 reg16;
186 u32 reg32;
187
188 /* Ensure controller is in D0 state */
189 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
190 reg16 &= ~XHCI_PWR_CTL_SET_MASK;
191 reg16 |= XHCI_PWR_CTL_SET_D0;
192 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
193
194 /* Disable Compliance Mode Entry */
195 reg32 = read32(res2mmio(res, 0x80ec, 0));
196 reg32 |= (1 << 0);
197 write32(res2mmio(res, 0x80ec, 0), reg32);
198}
199
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700200static struct device_operations usb_xhci_ops = {
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100201 .read_resources = pci_dev_read_resources,
202 .set_resources = pci_dev_set_resources,
203 .enable_resources = pci_dev_enable_resources,
Angel Ponscb2080f2020-10-23 15:45:44 +0200204 .ops_pci = &pci_dev_ops_pci,
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100205 .init = xhci_init,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700206};
207
208static const unsigned short pci_device_ids[] = {
209 0x9c31, /* LynxPoint-LP */
210 0x9cb1, /* WildcatPoint */
211 0
212};
213
214static const struct pci_driver pch_usb_xhci __pci_driver = {
215 .ops = &usb_xhci_ops,
216 .vendor = PCI_VENDOR_ID_INTEL,
217 .devices = pci_device_ids,
218};
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200219#endif /* !__SIMPLE_DEVICE__ */