blob: 681d0983743e28f49f3ee769ad29726bb00c5c32 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Aaron Durbin76c37002012-10-30 09:03:43 -05003
4#include <console/console.h>
Duncan Laurie1f529082013-07-30 15:53:45 -07005#include <delay.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05006#include <device/device.h>
7#include <device/pci.h>
8#include <device/pci_ids.h>
Kyösti Mälkkie2227a22014-02-05 13:02:55 +02009#include <device/pci_ehci.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020010#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020011#include <device/pci_ops.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{
18 u16 reg16;
19 u32 reg32;
20
21 /* Set 0xDC[0]=1 */
22 pci_or_config32(dev, 0xdc, (1 << 0));
23
24 /* Set D3Hot state and disable PME */
25 reg16 = pci_read_config16(dev, EHCI_PWR_CTL_STS);
26 reg16 &= ~(PWR_CTL_ENABLE_PME | PWR_CTL_SET_MASK);
27 reg16 |= PWR_CTL_SET_D3;
28 pci_write_config16(dev, EHCI_PWR_CTL_STS, reg16);
29
30 /* Clear memory and bus master */
31 pci_write_config32(dev, PCI_BASE_ADDRESS_0, 0);
32 reg32 = pci_read_config32(dev, PCI_COMMAND);
33 reg32 &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
34 pci_write_config32(dev, PCI_COMMAND, reg32);
35
36 /* Disable device */
37 switch (dev) {
38 case PCH_EHCI1_DEV:
39 RCBA32_OR(FD, PCH_DISABLE_EHCI1);
40 break;
41 case PCH_EHCI2_DEV:
42 RCBA32_OR(FD, PCH_DISABLE_EHCI2);
43 break;
44 }
45}
46
47/* Handler for EHCI controller on entry to S3/S4/S5 */
Elyes HAOUASab72fc22018-11-29 16:13:14 +010048void usb_ehci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Laurie1f529082013-07-30 15:53:45 -070049{
50 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080051 u8 *bar0_base;
Duncan Laurie1f529082013-07-30 15:53:45 -070052 u16 pwr_state;
53 u16 pci_cmd;
54
55 /* Check if the controller is disabled or not present */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080056 bar0_base = (u8 *)pci_read_config32(dev, PCI_BASE_ADDRESS_0);
57 if (bar0_base == 0 || bar0_base == (u8 *)0xffffffff)
Duncan Laurie1f529082013-07-30 15:53:45 -070058 return;
59 pci_cmd = pci_read_config32(dev, PCI_COMMAND);
60
61 switch (slp_typ) {
Aaron Durbinda5f5092016-07-13 23:23:16 -050062 case ACPI_S4:
63 case ACPI_S5:
Duncan Laurie1f529082013-07-30 15:53:45 -070064 /* Check if controller is in D3 power state */
65 pwr_state = pci_read_config16(dev, EHCI_PWR_CTL_STS);
66 if ((pwr_state & PWR_CTL_SET_MASK) == PWR_CTL_SET_D3) {
67 /* Put in D0 */
68 u32 new_state = pwr_state & ~PWR_CTL_SET_MASK;
69 new_state |= PWR_CTL_SET_D0;
70 pci_write_config16(dev, EHCI_PWR_CTL_STS, new_state);
71
72 /* Make sure memory bar is set */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080073 pci_write_config32(dev, PCI_BASE_ADDRESS_0, (u32)bar0_base);
Duncan Laurie1f529082013-07-30 15:53:45 -070074
75 /* Make sure memory space is enabled */
76 pci_write_config16(dev, PCI_COMMAND, pci_cmd |
77 PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
78 }
79
80 /*
81 * If Run/Stop (bit0) is clear in USB2.0_CMD:
82 * - Clear Async Schedule Enable (bit5) and
83 * - Clear Periodic Schedule Enable (bit4) and
84 * - Set Run/Stop (bit0)
85 */
86 reg32 = read32(bar0_base + EHCI_USB_CMD);
87 if (reg32 & EHCI_USB_CMD_RUN) {
88 reg32 &= ~(EHCI_USB_CMD_PSE | EHCI_USB_CMD_ASE);
89 reg32 |= EHCI_USB_CMD_RUN;
90 write32(bar0_base + EHCI_USB_CMD, reg32);
91 }
92
93 /* Check for Port Enabled in PORTSC(0) (RMH) */
94 reg32 = read32(bar0_base + EHCI_PORTSC(0));
95 if (reg32 & EHCI_PORTSC_ENABLED) {
96 /* Set suspend bit in PORTSC if not already set */
97 if (!(reg32 & EHCI_PORTSC_SUSPEND)) {
98 reg32 |= EHCI_PORTSC_SUSPEND;
99 write32(bar0_base + EHCI_PORTSC(0), reg32);
100 }
101
102 /* Delay 25ms !! */
103 udelay(25 * 1000);
104
105 /* Clear Run/Stop bit */
106 reg32 = read32(bar0_base + EHCI_USB_CMD);
107 reg32 &= EHCI_USB_CMD_RUN;
108 write32(bar0_base + EHCI_USB_CMD, reg32);
109 }
110
111 /* Restore state to D3 if that is what it was at the start */
112 if ((pwr_state & PWR_CTL_SET_MASK) == PWR_CTL_SET_D3) {
113 /* Restore pci command reg */
114 pci_write_config16(dev, PCI_COMMAND, pci_cmd);
115
116 /* Enable D3 */
117 pci_write_config16(dev, EHCI_PWR_CTL_STS, pwr_state);
118 }
119 }
120}
121
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200122#else /* !__SIMPLE_DEVICE__ */
Aaron Durbin76c37002012-10-30 09:03:43 -0500123
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700124static void usb_ehci_clock_gating(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500125{
126 u32 reg32;
127
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700128 /* IOBP 0xE5004001[7:6] = 11b */
129 pch_iobp_update(0xe5004001, ~0, (1 << 7)|(1 << 6));
Aaron Durbin76c37002012-10-30 09:03:43 -0500130
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700131 /* Dx:F0:DCh[5,2,1] = 111b
132 * Dx:F0:DCh[0] = 1b when EHCI controller is disabled */
133 reg32 = pci_read_config32(dev, 0xdc);
134 reg32 |= (1 << 5) | (1 << 2) | (1 << 1);
135 pci_write_config32(dev, 0xdc, reg32);
136
137 /* Dx:F0:78h[1:0] = 11b */
138 reg32 = pci_read_config32(dev, 0x78);
139 reg32 |= (1 << 1) | (1 << 0);
140 pci_write_config32(dev, 0x78, reg32);
141}
142
143static void usb_ehci_init(struct device *dev)
144{
Aaron Durbin76c37002012-10-30 09:03:43 -0500145 printk(BIOS_DEBUG, "EHCI: Setting up controller.. ");
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700146
147 usb_ehci_clock_gating(dev);
148
149 /* Disable Wake on Disconnect in RMH */
150 RCBA32_OR(0x35b0, 0x00000022);
Aaron Durbin76c37002012-10-30 09:03:43 -0500151
152 printk(BIOS_DEBUG, "done.\n");
153}
154
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100155static void usb_ehci_set_subsystem(struct device *dev, unsigned int vendor,
156 unsigned int device)
Aaron Durbin76c37002012-10-30 09:03:43 -0500157{
158 u8 access_cntl;
159
160 access_cntl = pci_read_config8(dev, 0x80);
161
162 /* Enable writes to protected registers. */
163 pci_write_config8(dev, 0x80, access_cntl | 1);
164
Subrata Banik4a0f0712019-03-20 14:29:47 +0530165 pci_dev_set_subsystem(dev, vendor, device);
Aaron Durbin76c37002012-10-30 09:03:43 -0500166
167 /* Restore protection. */
168 pci_write_config8(dev, 0x80, access_cntl);
169}
170
Aaron Durbin76c37002012-10-30 09:03:43 -0500171static struct pci_operations lops_pci = {
172 .set_subsystem = &usb_ehci_set_subsystem,
173};
174
175static struct device_operations usb_ehci_ops = {
Kyösti Mälkkifb387df2013-06-07 22:16:52 +0300176 .read_resources = pci_ehci_read_resources,
177 .set_resources = pci_dev_set_resources,
Aaron Durbin76c37002012-10-30 09:03:43 -0500178 .enable_resources = pci_dev_enable_resources,
179 .init = usb_ehci_init,
180 .scan_bus = 0,
181 .ops_pci = &lops_pci,
182};
183
Kyösti Mälkkif55a5422013-06-14 11:16:25 +0300184static const unsigned short pci_device_ids[] = { 0x9c26, 0x8c26, 0x8c2d, 0 };
Aaron Durbin76c37002012-10-30 09:03:43 -0500185
186static const struct pci_driver pch_usb_ehci __pci_driver = {
187 .ops = &usb_ehci_ops,
188 .vendor = PCI_VENDOR_ID_INTEL,
189 .devices = pci_device_ids,
190};
Duncan Laurie1f529082013-07-30 15:53:45 -0700191
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200192#endif /* !__SIMPLE_DEVICE__ */