blob: b5980df3fda6788836828feaf2bf6a4691689ec2 [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>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070021#include <arch/io.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070022#include <soc/ramstage.h>
23#include <soc/xhci.h>
24#include <soc/cpu.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070025
26#ifdef __SMM__
Elyes HAOUAS4658a982018-09-20 08:46:35 +020027static u8 *usb_xhci_mem_base(pci_devfn_t dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070028{
29 u32 mem_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
30
31 /* Check if the controller is disabled or not present */
32 if (mem_base == 0 || mem_base == 0xffffffff)
33 return 0;
34
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080035 return (u8 *)(mem_base & ~0xf);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070036}
37
Elyes HAOUAS4658a982018-09-20 08:46:35 +020038static int usb_xhci_port_count_usb3(pci_devfn_t dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070039{
40 /* PCH-LP has 4 SS ports */
41 return 4;
42}
43
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080044static void usb_xhci_reset_status_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 u32 status = read32(portsc);
48 /* Do not set Port Enabled/Disabled field */
49 status &= ~XHCI_USB3_PORTSC_PED;
50 /* Clear all change status bits */
51 status |= XHCI_USB3_PORTSC_CHST;
52 write32(portsc, status);
53}
54
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080055static void usb_xhci_reset_port_usb3(u8 *mem_base, int port)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070056{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080057 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070058 write32(portsc, read32(portsc) | XHCI_USB3_PORTSC_WPR);
59}
60
61#define XHCI_RESET_DELAY_US 1000 /* 1ms */
62#define XHCI_RESET_TIMEOUT 100 /* 100ms */
63
64/*
65 * 1) Wait until port is done polling
66 * 2) If port is disconnected
67 * a) Issue warm port reset
68 * b) Poll for warm reset complete
69 * c) Write 1 to port change status bits
70 */
Elyes HAOUAS4658a982018-09-20 08:46:35 +020071static void usb_xhci_reset_usb3(pci_devfn_t dev, int all)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070072{
73 u32 status, port_disabled;
74 int timeout, port;
75 int port_count = usb_xhci_port_count_usb3(dev);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080076 u8 *mem_base = usb_xhci_mem_base(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070077
78 if (!mem_base || !port_count)
79 return;
80
81 /* Get mask of disabled ports */
82 port_disabled = pci_read_config32(dev, XHCI_USB3PDO);
83
84 /* Wait until all enabled ports are done polling */
85 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
86 int complete = 1;
87 for (port = 0; port < port_count; port++) {
88 /* Skip disabled ports */
89 if (port_disabled & (1 << port))
90 continue;
91 /* Read port link status field */
92 status = read32(mem_base + XHCI_USB3_PORTSC(port));
93 status &= XHCI_USB3_PORTSC_PLS;
94 if (status == XHCI_PLSR_POLLING)
95 complete = 0;
96 }
97 /* Exit if all ports not polling */
98 if (complete)
99 break;
100 udelay(XHCI_RESET_DELAY_US);
101 }
102
103 /* Reset all requested ports */
104 for (port = 0; port < port_count; port++) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800105 u8 *portsc = mem_base + XHCI_USB3_PORTSC(port);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700106 /* Skip disabled ports */
107 if (port_disabled & (1 << port))
108 continue;
109 status = read32(portsc) & XHCI_USB3_PORTSC_PLS;
110 /* Reset all or only disconnected ports */
111 if (all || (status == XHCI_PLSR_RXDETECT ||
112 status == XHCI_PLSR_POLLING))
113 usb_xhci_reset_port_usb3(mem_base, port);
114 else
115 port_disabled |= 1 << port;
116 }
117
118 /* Wait for warm reset complete on all reset ports */
119 for (timeout = XHCI_RESET_TIMEOUT; timeout; timeout--) {
120 int complete = 1;
121 for (port = 0; port < port_count; port++) {
122 /* Only check ports that were reset */
123 if (port_disabled & (1 << port))
124 continue;
125 /* Check if warm reset is complete */
126 status = read32(mem_base + XHCI_USB3_PORTSC(port));
127 if (!(status & XHCI_USB3_PORTSC_WRC))
128 complete = 0;
129 }
130 /* Check for warm reset complete in any port */
131 if (complete)
132 break;
133 udelay(XHCI_RESET_DELAY_US);
134 }
135
136 /* Clear port change status bits */
137 for (port = 0; port < port_count; port++)
138 usb_xhci_reset_status_usb3(mem_base, port);
139}
140
141/* Handler for XHCI controller on entry to S3/S4/S5 */
Elyes HAOUAS4658a982018-09-20 08:46:35 +0200142void usb_xhci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700143{
144 u16 reg16;
145 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800146 u8 *mem_base = usb_xhci_mem_base(dev);
Kane Chen46134722014-08-28 17:05:06 -0700147 u8 is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700148
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500149 if (!mem_base || slp_typ < ACPI_S3)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700150 return;
151
152 /* Set D0 state */
153 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
154 reg16 &= ~XHCI_PWR_CTL_SET_MASK;
155 reg16 |= XHCI_PWR_CTL_SET_D0;
156 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
157
Kane Chen46134722014-08-28 17:05:06 -0700158 if (!is_broadwell) {
159 /* This WA is only for lpt */
160
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200161 /* Clear PCI 0xB0[14:13] */
162 reg32 = pci_read_config32(dev, 0xb0);
163 reg32 &= ~((1 << 14) | (1 << 13));
164 pci_write_config32(dev, 0xb0, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700165
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200166 /* Clear MMIO 0x816c[14,2] */
167 reg32 = read32(mem_base + 0x816c);
168 reg32 &= ~((1 << 14) | (1 << 2));
169 write32(mem_base + 0x816c, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700170
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200171 /* Reset disconnected USB3 ports */
172 usb_xhci_reset_usb3(dev, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700173
Patrick Georgi46d3ac12015-04-20 10:27:59 +0200174 /* Set MMIO 0x80e0[15] */
175 reg32 = read32(mem_base + 0x80e0);
176 reg32 |= (1 << 15);
177 write32(mem_base + 0x80e0, reg32);
Todd Brochdf4081e2015-02-06 17:13:53 -0800178 } else {
179 /*
180 * Clear port change status bits. Clearing CSC alone seemed to
181 * fix wakeup from S3 if entering USB compliance state even if
182 * bit wasn't set on the port.
183 */
184 int port;
185 for (port = 0; port < usb_xhci_port_count_usb3(dev); port++)
186 usb_xhci_reset_status_usb3(mem_base, port);
Kane Chen46134722014-08-28 17:05:06 -0700187 }
188
189 reg32 = read32(mem_base + 0x8154);
190 reg32 &= ~(1 << 31);
191 write32(mem_base + 0x8154, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700192
193 /* Set D3Hot state and enable PME */
194 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_SET_D3);
195 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_STATUS_PME);
196 pci_or_config16(dev, XHCI_PWR_CTL_STS, XHCI_PWR_CTL_ENABLE_PME);
197}
198#else /* !__SMM__ */
199
Duncan Laurieaafdddf2015-02-19 16:21:10 -0800200static void xhci_init(struct device *dev)
201{
202 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
203 u16 reg16;
204 u32 reg32;
205
206 /* Ensure controller is in D0 state */
207 reg16 = pci_read_config16(dev, XHCI_PWR_CTL_STS);
208 reg16 &= ~XHCI_PWR_CTL_SET_MASK;
209 reg16 |= XHCI_PWR_CTL_SET_D0;
210 pci_write_config16(dev, XHCI_PWR_CTL_STS, reg16);
211
212 /* Disable Compliance Mode Entry */
213 reg32 = read32(res2mmio(res, 0x80ec, 0));
214 reg32 |= (1 << 0);
215 write32(res2mmio(res, 0x80ec, 0), reg32);
216}
217
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700218static struct device_operations usb_xhci_ops = {
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100219 .read_resources = pci_dev_read_resources,
220 .set_resources = pci_dev_set_resources,
221 .enable_resources = pci_dev_enable_resources,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700222 .ops_pci = &broadwell_pci_ops,
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100223 .init = xhci_init,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700224};
225
226static const unsigned short pci_device_ids[] = {
227 0x9c31, /* LynxPoint-LP */
228 0x9cb1, /* WildcatPoint */
229 0
230};
231
232static const struct pci_driver pch_usb_xhci __pci_driver = {
233 .ops = &usb_xhci_ops,
234 .vendor = PCI_VENDOR_ID_INTEL,
235 .devices = pci_device_ids,
236};
237#endif /* !__SMM__ */