blob: e3bb40f081684c70cb0ba1acbc610fab19ac47b7 [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/xhci.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070012
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020013#ifdef __SIMPLE_DEVICE__
Elyes HAOUAS4658a982018-09-20 08:46:35 +020014static u8 *usb_xhci_mem_base(pci_devfn_t dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070015{
16 u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
17
18 /* Check if the controller is disabled or not present */
19 if (mem_base == 0 || mem_base == 0xffffffff)
20 return 0;
21
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080022 return (u8 *)(mem_base & ~0xf);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070023}
24
Elyes HAOUAS4658a982018-09-20 08:46:35 +020025static int usb_xhci_port_count_usb3(pci_devfn_t dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070026{
27 /* PCH-LP has 4 SS ports */
28 return 4;
29}
30
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080031static void usb_xhci_reset_status_usb3(u8 *mem_base, int port)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070032{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080033 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070034 u32 status = read32(portsc);
35 /* Do not set Port Enabled/Disabled field */
36 status &= ~XHCI_USB3_PORTSC_PED;
37 /* Clear all change status bits */
38 status |= XHCI_USB3_PORTSC_CHST;
39 write32(portsc, status);
40}
41
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080042static void usb_xhci_reset_port_usb3(u8 *mem_base, int port)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070043{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080044 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070045 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
46}
47
48#define XHCI_RESET_DELAY_US 1000 /* 1ms */
49#define XHCI_RESET_TIMEOUT 100 /* 100ms */
50
51/*
52 * 1) Wait until port is done polling
53 * 2) If port is disconnected
54 * a) Issue warm port reset
55 * b) Poll for warm reset complete
56 * c) Write 1 to port change status bits
57 */
Elyes HAOUAS4658a982018-09-20 08:46:35 +020058static void usb_xhci_reset_usb3(pci_devfn_t dev, int all)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070059{
60 u32 status, port_disabled;
61 int timeout, port;
62 int port_count = usb_xhci_port_count_usb3(dev);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080063 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070064
65 if (!mem_base || !port_count)
66 return;
67
68 /* Get mask of disabled ports */
69 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
70
71 /* Wait until all enabled ports are done polling */
72 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
73 int complete = 1;
74 for (port = 0; port < port_count; port++) {
75 /* Skip disabled ports */
76 if (port_disabled & (1 << port))
77 continue;
78 /* Read port link status field */
79 status = read32(mem_base + XHCI_USB3_PORTSC(port));
80 status &= XHCI_USB3_PORTSC_PLS;
81 if (status == XHCI_PLSR_POLLING)
82 complete = 0;
83 }
84 /* Exit if all ports not polling */
85 if (complete)
86 break;
87 udelay(XHCI_RESET_DELAY_US);
88 }
89
90 /* Reset all requested ports */
91 for (port = 0; port < port_count; port++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080092 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070093 /* Skip disabled ports */
94 if (port_disabled & (1 << port))
95 continue;
96 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
97 /* Reset all or only disconnected ports */
98 if (all || (status == XHCI_PLSR_RXDETECT ||
99 status == XHCI_PLSR_POLLING))
100 usb_xhci_reset_port_usb3(mem_base, port);
101 else
102 port_disabled |= 1 << port;
103 }
104
105 /* Wait for warm reset complete on all reset ports */
106 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
107 int complete = 1;
108 for (port = 0; port < port_count; port++) {
109 /* Only check ports that were reset */
110 if (port_disabled & (1 << port))
111 continue;
112 /* Check if warm reset is complete */
113 status = read32(mem_base + XHCI_USB3_PORTSC(port));
114 if (!(status & XHCI_USB3_PORTSC_WRC))
115 complete = 0;
116 }
117 /* Check for warm reset complete in any port */
118 if (complete)
119 break;
120 udelay(XHCI_RESET_DELAY_US);
121 }
122
123 /* Clear port change status bits */
124 for (port = 0; port < port_count; port++)
125 usb_xhci_reset_status_usb3(mem_base, port);
126}
127
128/* Handler for XHCI controller on entry to S3/S4/S5 */
Elyes HAOUAS4658a982018-09-20 08:46:35 +0200129void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700130{
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700131 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800132 u8 *mem_base = usb_xhci_mem_base(dev);
Kane Chen46134722014-08-28 17:05:06 -0700133 u8 is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700134
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500135 if (!mem_base || slp_typ < ACPI_S3)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700136 return;
137
138 /* Set D0 state */
Angel Pons8b8b2712020-09-25 01:04:51 +0200139 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 -0700140
Kane Chen46134722014-08-28 17:05:06 -0700141 if (!is_broadwell) {
142 /* This WA is only for lpt */
143
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200144 /* Clear PCI 0xB0[14:13] */
Angel Pons8b8b2712020-09-25 01:04:51 +0200145 pci_and_config32(dev, 0xb0, ~((1 << 14) | (1 << 13)));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700146
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200147 /* Clear MMIO 0x816c[14,2] */
148 reg32 = read32(mem_base + 0x816c);
149 reg32 &= ~((1 << 14) | (1 << 2));
150 write32(mem_base + 0x816c, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700151
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200152 /* Reset disconnected USB3 ports */
153 usb_xhci_reset_usb3(dev, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700154
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200155 /* Set MMIO 0x80e0[15] */
156 reg32 = read32(mem_base + 0x80e0);
157 reg32 |= (1 << 15);
158 write32(mem_base + 0x80e0, reg32);
Todd Brochdf4081e2015-02-06 17:13:53 -0800159 } else {
160 /*
161 * Clear port change status bits. Clearing CSC alone seemed to
162 * fix wakeup from S3 if entering USB compliance state even if
163 * bit wasn't set on the port.
164 */
165 int port;
166 for (port = 0; port < usb_xhci_port_count_usb3(dev); port++)
167 usb_xhci_reset_status_usb3(mem_base, port);
Kane Chen46134722014-08-28 17:05:06 -0700168 }
169
170 reg32 = read32(mem_base + 0x8154);
171 reg32 &= ~(1 << 31);
172 write32(mem_base + 0x8154, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700173
174 /* Set D3Hot state and enable PME */
175 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_SET_D3);
176 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_STATUS_PME);
177 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_ENABLE_PME);
178}
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200179#else /* !__SIMPLE_DEVICE__ */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700180
Duncan Laurieaafdddf2015-02-19 16:21:10 -0800181static void xhci_init(struct device *dev)
182{
183 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
184 u16 reg16;
185 u32 reg32;
186
187 /* Ensure controller is in D0 state */
188 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
189 reg16 &= ~XHCI_PWR_CTL_SET_MASK;
190 reg16 |= XHCI_PWR_CTL_SET_D0;
191 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
192
193 /* Disable Compliance Mode Entry */
194 reg32 = read32(res2mmio(res, 0x80ec, 0));
195 reg32 |= (1 << 0);
196 write32(res2mmio(res, 0x80ec, 0), reg32);
197}
198
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700199static struct device_operations usb_xhci_ops = {
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100200 .read_resources = pci_dev_read_resources,
201 .set_resources = pci_dev_set_resources,
202 .enable_resources = pci_dev_enable_resources,
Angel Ponscb2080f2020-10-23 15:45:44 +0200203 .ops_pci = &pci_dev_ops_pci,
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100204 .init = xhci_init,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700205};
206
207static const unsigned short pci_device_ids[] = {
208 0x9c31, /* LynxPoint-LP */
209 0x9cb1, /* WildcatPoint */
210 0
211};
212
213static const struct pci_driver pch_usb_xhci __pci_driver = {
214 .ops = &usb_xhci_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100215 .vendor = PCI_VID_INTEL,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700216 .devices = pci_device_ids,
217};
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200218#endif /* !__SIMPLE_DEVICE__ */