blob: 52b3ed8b3e6fadfc3a993b0869d8f0b51bea7319 [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>
Duncan Laurie1f529082013-07-30 15:53:45 -070011#include "pch.h"
12
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020013#ifdef __SIMPLE_DEVICE__
Duncan Laurie1f529082013-07-30 15:53:45 -070014
Elyes HAOUASab72fc22018-11-29 16:13:14 +010015void usb_ehci_disable(pci_devfn_t dev)
Duncan Laurie1f529082013-07-30 15:53:45 -070016{
17 u16 reg16;
Duncan Laurie1f529082013-07-30 15:53:45 -070018
19 /* Set 0xDC[0]=1 */
20 pci_or_config32(dev, 0xdc, (1 << 0));
21
22 /* Set D3Hot state and disable PME */
23 reg16 = pci_read_config16(dev, EHCI_PWR_CTL_STS);
24 reg16 &= ~(PWR_CTL_ENABLE_PME | PWR_CTL_SET_MASK);
25 reg16 |= PWR_CTL_SET_D3;
26 pci_write_config16(dev, EHCI_PWR_CTL_STS, reg16);
27
28 /* Clear memory and bus master */
29 pci_write_config32(dev, PCI_BASE_ADDRESS_0, 0);
Elyes HAOUAS73ae0762020-04-28 10:13:05 +020030 reg16 = pci_read_config16(dev, PCI_COMMAND);
31 reg16 &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
32 pci_write_config16(dev, PCI_COMMAND, reg16);
Duncan Laurie1f529082013-07-30 15:53:45 -070033
34 /* Disable device */
35 switch (dev) {
36 case PCH_EHCI1_DEV:
37 RCBA32_OR(FD, PCH_DISABLE_EHCI1);
38 break;
39 case PCH_EHCI2_DEV:
40 RCBA32_OR(FD, PCH_DISABLE_EHCI2);
41 break;
42 }
43}
44
45/* Handler for EHCI controller on entry to S3/S4/S5 */
Elyes HAOUASab72fc22018-11-29 16:13:14 +010046void usb_ehci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Laurie1f529082013-07-30 15:53:45 -070047{
48 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080049 u8 *bar0_base;
Duncan Laurie1f529082013-07-30 15:53:45 -070050 u16 pwr_state;
51 u16 pci_cmd;
52
53 /* Check if the controller is disabled or not present */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080054 bar0_base = (u8 *)pci_read_config32(dev, PCI_BASE_ADDRESS_0);
55 if (bar0_base == 0 || bar0_base == (u8 *)0xffffffff)
Duncan Laurie1f529082013-07-30 15:53:45 -070056 return;
Elyes HAOUAS73ae0762020-04-28 10:13:05 +020057 pci_cmd = pci_read_config16(dev, PCI_COMMAND);
Duncan Laurie1f529082013-07-30 15:53:45 -070058
59 switch (slp_typ) {
Aaron Durbinda5f5092016-07-13 23:23:16 -050060 case ACPI_S4:
61 case ACPI_S5:
Duncan Laurie1f529082013-07-30 15:53:45 -070062 /* Check if controller is in D3 power state */
63 pwr_state = pci_read_config16(dev, EHCI_PWR_CTL_STS);
64 if ((pwr_state & PWR_CTL_SET_MASK) == PWR_CTL_SET_D3) {
65 /* Put in D0 */
66 u32 new_state = pwr_state & ~PWR_CTL_SET_MASK;
67 new_state |= PWR_CTL_SET_D0;
68 pci_write_config16(dev, EHCI_PWR_CTL_STS, new_state);
69
70 /* Make sure memory bar is set */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080071 pci_write_config32(dev, PCI_BASE_ADDRESS_0, (u32)bar0_base);
Duncan Laurie1f529082013-07-30 15:53:45 -070072
73 /* Make sure memory space is enabled */
74 pci_write_config16(dev, PCI_COMMAND, pci_cmd |
75 PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
76 }
77
78 /*
79 * If Run/Stop (bit0) is clear in USB2.0_CMD:
80 * - Clear Async Schedule Enable (bit5) and
81 * - Clear Periodic Schedule Enable (bit4) and
82 * - Set Run/Stop (bit0)
83 */
84 reg32 = read32(bar0_base + EHCI_USB_CMD);
85 if (reg32 & EHCI_USB_CMD_RUN) {
86 reg32 &= ~(EHCI_USB_CMD_PSE | EHCI_USB_CMD_ASE);
87 reg32 |= EHCI_USB_CMD_RUN;
88 write32(bar0_base + EHCI_USB_CMD, reg32);
89 }
90
91 /* Check for Port Enabled in PORTSC(0) (RMH) */
92 reg32 = read32(bar0_base + EHCI_PORTSC(0));
93 if (reg32 & EHCI_PORTSC_ENABLED) {
94 /* Set suspend bit in PORTSC if not already set */
95 if (!(reg32 & EHCI_PORTSC_SUSPEND)) {
96 reg32 |= EHCI_PORTSC_SUSPEND;
97 write32(bar0_base + EHCI_PORTSC(0), reg32);
98 }
99
100 /* Delay 25ms !! */
101 udelay(25 * 1000);
102
103 /* Clear Run/Stop bit */
104 reg32 = read32(bar0_base + EHCI_USB_CMD);
105 reg32 &= EHCI_USB_CMD_RUN;
106 write32(bar0_base + EHCI_USB_CMD, reg32);
107 }
108
109 /* Restore state to D3 if that is what it was at the start */
110 if ((pwr_state & PWR_CTL_SET_MASK) == PWR_CTL_SET_D3) {
111 /* Restore pci command reg */
112 pci_write_config16(dev, PCI_COMMAND, pci_cmd);
113
114 /* Enable D3 */
115 pci_write_config16(dev, EHCI_PWR_CTL_STS, pwr_state);
116 }
117 }
118}
119
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200120#else /* !__SIMPLE_DEVICE__ */
Aaron Durbin76c37002012-10-30 09:03:43 -0500121
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700122static void usb_ehci_clock_gating(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500123{
124 u32 reg32;
125
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700126 /* IOBP 0xE5004001[7:6] = 11b */
127 pch_iobp_update(0xe5004001, ~0, (1 << 7)|(1 << 6));
Aaron Durbin76c37002012-10-30 09:03:43 -0500128
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700129 /* Dx:F0:DCh[5,2,1] = 111b
130 * Dx:F0:DCh[0] = 1b when EHCI controller is disabled */
131 reg32 = pci_read_config32(dev, 0xdc);
132 reg32 |= (1 << 5) | (1 << 2) | (1 << 1);
133 pci_write_config32(dev, 0xdc, reg32);
134
135 /* Dx:F0:78h[1:0] = 11b */
136 reg32 = pci_read_config32(dev, 0x78);
137 reg32 |= (1 << 1) | (1 << 0);
138 pci_write_config32(dev, 0x78, reg32);
139}
140
141static void usb_ehci_init(struct device *dev)
142{
Aaron Durbin76c37002012-10-30 09:03:43 -0500143 printk(BIOS_DEBUG, "EHCI: Setting up controller.. ");
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700144
145 usb_ehci_clock_gating(dev);
146
147 /* Disable Wake on Disconnect in RMH */
148 RCBA32_OR(0x35b0, 0x00000022);
Aaron Durbin76c37002012-10-30 09:03:43 -0500149
150 printk(BIOS_DEBUG, "done.\n");
151}
152
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100153static void usb_ehci_set_subsystem(struct device *dev, unsigned int vendor,
154 unsigned int device)
Aaron Durbin76c37002012-10-30 09:03:43 -0500155{
156 u8 access_cntl;
157
158 access_cntl = pci_read_config8(dev, 0x80);
159
160 /* Enable writes to protected registers. */
161 pci_write_config8(dev, 0x80, access_cntl | 1);
162
Subrata Banik4a0f0712019-03-20 14:29:47 +0530163 pci_dev_set_subsystem(dev, vendor, device);
Aaron Durbin76c37002012-10-30 09:03:43 -0500164
165 /* Restore protection. */
166 pci_write_config8(dev, 0x80, access_cntl);
167}
168
Aaron Durbin76c37002012-10-30 09:03:43 -0500169static struct pci_operations lops_pci = {
170 .set_subsystem = &usb_ehci_set_subsystem,
171};
172
173static struct device_operations usb_ehci_ops = {
Kyösti Mälkkifb387df2013-06-07 22:16:52 +0300174 .read_resources = pci_ehci_read_resources,
175 .set_resources = pci_dev_set_resources,
Aaron Durbin76c37002012-10-30 09:03:43 -0500176 .enable_resources = pci_dev_enable_resources,
177 .init = usb_ehci_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500178 .ops_pci = &lops_pci,
179};
180
Kyösti Mälkkif55a5422013-06-14 11:16:25 +0300181static const unsigned short pci_device_ids[] = { 0x9c26, 0x8c26, 0x8c2d, 0 };
Aaron Durbin76c37002012-10-30 09:03:43 -0500182
183static const struct pci_driver pch_usb_ehci __pci_driver = {
184 .ops = &usb_ehci_ops,
185 .vendor = PCI_VENDOR_ID_INTEL,
186 .devices = pci_device_ids,
187};
Duncan Laurie1f529082013-07-30 15:53:45 -0700188
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200189#endif /* !__SIMPLE_DEVICE__ */