blob: 8853a143ca5f4924620ebd3a215a7c514bff0a5e [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin76c37002012-10-30 09:03:43 -05002
3#include <console/console.h>
Duncan Laurie1f529082013-07-30 15:53:45 -07004#include <delay.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05005#include <device/device.h>
6#include <device/pci.h>
7#include <device/pci_ids.h>
Kyösti Mälkkie2227a22014-02-05 13:02:55 +02008#include <device/pci_ehci.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>
Angel Pons2178b722020-05-31 00:55:35 +020011#include "iobp.h"
Duncan Laurie1f529082013-07-30 15:53:45 -070012#include "pch.h"
13
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020014#ifdef __SIMPLE_DEVICE__
Duncan Laurie1f529082013-07-30 15:53:45 -070015
Elyes HAOUASab72fc22018-11-29 16:13:14 +010016void usb_ehci_disable(pci_devfn_t dev)
Duncan Laurie1f529082013-07-30 15:53:45 -070017{
Duncan Laurie1f529082013-07-30 15:53:45 -070018 /* Set 0xDC[0]=1 */
19 pci_or_config32(dev, 0xdc, (1 << 0));
20
21 /* Set D3Hot state and disable PME */
Angel Ponsbf9bc502020-06-08 00:12:43 +020022 pci_update_config16(dev, EHCI_PWR_CTL_STS, ~(PWR_CTL_ENABLE_PME | PWR_CTL_SET_MASK),
23 PWR_CTL_SET_D3);
Duncan Laurie1f529082013-07-30 15:53:45 -070024
25 /* Clear memory and bus master */
26 pci_write_config32(dev, PCI_BASE_ADDRESS_0, 0);
Angel Ponsbf9bc502020-06-08 00:12:43 +020027
28 pci_and_config16(dev, PCI_COMMAND,
29 ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
Duncan Laurie1f529082013-07-30 15:53:45 -070030
31 /* Disable device */
32 switch (dev) {
33 case PCH_EHCI1_DEV:
34 RCBA32_OR(FD, PCH_DISABLE_EHCI1);
35 break;
36 case PCH_EHCI2_DEV:
37 RCBA32_OR(FD, PCH_DISABLE_EHCI2);
38 break;
39 }
40}
41
42/* Handler for EHCI controller on entry to S3/S4/S5 */
Elyes HAOUASab72fc22018-11-29 16:13:14 +010043void usb_ehci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Laurie1f529082013-07-30 15:53:45 -070044{
45 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080046 u8 *bar0_base;
Duncan Laurie1f529082013-07-30 15:53:45 -070047 u16 pwr_state;
48 u16 pci_cmd;
49
50 /* Check if the controller is disabled or not present */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080051 bar0_base = (u8 *)pci_read_config32(dev, PCI_BASE_ADDRESS_0);
52 if (bar0_base == 0 || bar0_base == (u8 *)0xffffffff)
Duncan Laurie1f529082013-07-30 15:53:45 -070053 return;
Elyes HAOUAS73ae0762020-04-28 10:13:05 +020054 pci_cmd = pci_read_config16(dev, PCI_COMMAND);
Duncan Laurie1f529082013-07-30 15:53:45 -070055
56 switch (slp_typ) {
Aaron Durbinda5f5092016-07-13 23:23:16 -050057 case ACPI_S4:
58 case ACPI_S5:
Duncan Laurie1f529082013-07-30 15:53:45 -070059 /* Check if controller is in D3 power state */
60 pwr_state = pci_read_config16(dev, EHCI_PWR_CTL_STS);
61 if ((pwr_state & PWR_CTL_SET_MASK) == PWR_CTL_SET_D3) {
62 /* Put in D0 */
63 u32 new_state = pwr_state & ~PWR_CTL_SET_MASK;
64 new_state |= PWR_CTL_SET_D0;
65 pci_write_config16(dev, EHCI_PWR_CTL_STS, new_state);
66
67 /* Make sure memory bar is set */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080068 pci_write_config32(dev, PCI_BASE_ADDRESS_0, (u32)bar0_base);
Duncan Laurie1f529082013-07-30 15:53:45 -070069
70 /* Make sure memory space is enabled */
71 pci_write_config16(dev, PCI_COMMAND, pci_cmd |
72 PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
73 }
74
75 /*
76 * If Run/Stop (bit0) is clear in USB2.0_CMD:
77 * - Clear Async Schedule Enable (bit5) and
78 * - Clear Periodic Schedule Enable (bit4) and
79 * - Set Run/Stop (bit0)
80 */
81 reg32 = read32(bar0_base + EHCI_USB_CMD);
82 if (reg32 & EHCI_USB_CMD_RUN) {
83 reg32 &= ~(EHCI_USB_CMD_PSE | EHCI_USB_CMD_ASE);
84 reg32 |= EHCI_USB_CMD_RUN;
85 write32(bar0_base + EHCI_USB_CMD, reg32);
86 }
87
88 /* Check for Port Enabled in PORTSC(0) (RMH) */
89 reg32 = read32(bar0_base + EHCI_PORTSC(0));
90 if (reg32 & EHCI_PORTSC_ENABLED) {
91 /* Set suspend bit in PORTSC if not already set */
92 if (!(reg32 & EHCI_PORTSC_SUSPEND)) {
93 reg32 |= EHCI_PORTSC_SUSPEND;
94 write32(bar0_base + EHCI_PORTSC(0), reg32);
95 }
96
97 /* Delay 25ms !! */
98 udelay(25 * 1000);
99
100 /* Clear Run/Stop bit */
101 reg32 = read32(bar0_base + EHCI_USB_CMD);
102 reg32 &= EHCI_USB_CMD_RUN;
103 write32(bar0_base + EHCI_USB_CMD, reg32);
104 }
105
106 /* Restore state to D3 if that is what it was at the start */
107 if ((pwr_state & PWR_CTL_SET_MASK) == PWR_CTL_SET_D3) {
108 /* Restore pci command reg */
109 pci_write_config16(dev, PCI_COMMAND, pci_cmd);
110
111 /* Enable D3 */
112 pci_write_config16(dev, EHCI_PWR_CTL_STS, pwr_state);
113 }
114 }
115}
116
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200117#else /* !__SIMPLE_DEVICE__ */
Aaron Durbin76c37002012-10-30 09:03:43 -0500118
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700119static void usb_ehci_clock_gating(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500120{
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700121 /* IOBP 0xE5004001[7:6] = 11b */
Angel Pons84fa2242020-10-24 11:53:47 +0200122 pch_iobp_update(0xe5004001, ~0, (1 << 7) | (1 << 6));
Aaron Durbin76c37002012-10-30 09:03:43 -0500123
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700124 /* Dx:F0:DCh[5,2,1] = 111b
125 * Dx:F0:DCh[0] = 1b when EHCI controller is disabled */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200126 pci_or_config32(dev, 0xdc, (1 << 5) | (1 << 2) | (1 << 1));
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700127
128 /* Dx:F0:78h[1:0] = 11b */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200129 pci_or_config32(dev, 0x78, (1 << 1) | (1 << 0));
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700130}
131
132static void usb_ehci_init(struct device *dev)
133{
Aaron Durbin76c37002012-10-30 09:03:43 -0500134 printk(BIOS_DEBUG, "EHCI: Setting up controller.. ");
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700135
136 usb_ehci_clock_gating(dev);
137
138 /* Disable Wake on Disconnect in RMH */
139 RCBA32_OR(0x35b0, 0x00000022);
Aaron Durbin76c37002012-10-30 09:03:43 -0500140
141 printk(BIOS_DEBUG, "done.\n");
142}
143
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100144static void usb_ehci_set_subsystem(struct device *dev, unsigned int vendor,
145 unsigned int device)
Aaron Durbin76c37002012-10-30 09:03:43 -0500146{
147 u8 access_cntl;
148
149 access_cntl = pci_read_config8(dev, 0x80);
150
151 /* Enable writes to protected registers. */
152 pci_write_config8(dev, 0x80, access_cntl | 1);
153
Subrata Banik4a0f0712019-03-20 14:29:47 +0530154 pci_dev_set_subsystem(dev, vendor, device);
Aaron Durbin76c37002012-10-30 09:03:43 -0500155
156 /* Restore protection. */
157 pci_write_config8(dev, 0x80, access_cntl);
158}
159
Aaron Durbin76c37002012-10-30 09:03:43 -0500160static struct pci_operations lops_pci = {
161 .set_subsystem = &usb_ehci_set_subsystem,
162};
163
164static struct device_operations usb_ehci_ops = {
Kyösti Mälkkifb387df2013-06-07 22:16:52 +0300165 .read_resources = pci_ehci_read_resources,
166 .set_resources = pci_dev_set_resources,
Aaron Durbin76c37002012-10-30 09:03:43 -0500167 .enable_resources = pci_dev_enable_resources,
168 .init = usb_ehci_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500169 .ops_pci = &lops_pci,
170};
171
Felix Singer4ea08f92020-11-20 12:56:44 +0000172static const unsigned short pci_device_ids[] = {
Felix Singer43b7f412022-03-07 04:34:52 +0100173 PCI_DID_INTEL_LPT_LP_EHCI,
174 PCI_DID_INTEL_LPT_H_EHCI_1,
175 PCI_DID_INTEL_LPT_H_EHCI_2,
Felix Singer4ea08f92020-11-20 12:56:44 +0000176 0
177};
Aaron Durbin76c37002012-10-30 09:03:43 -0500178
179static const struct pci_driver pch_usb_ehci __pci_driver = {
180 .ops = &usb_ehci_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100181 .vendor = PCI_VID_INTEL,
Aaron Durbin76c37002012-10-30 09:03:43 -0500182 .devices = pci_device_ids,
183};
Duncan Laurie1f529082013-07-30 15:53:45 -0700184
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200185#endif /* !__SIMPLE_DEVICE__ */