blob: 1fde466eb71fe0406ed7580f55b4eaf2c9986b01 [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;
Duncan Laurie1f529082013-07-30 15:53:45 -070019
20 /* Set 0xDC[0]=1 */
21 pci_or_config32(dev, 0xdc, (1 << 0));
22
23 /* Set D3Hot state and disable PME */
24 reg16 = pci_read_config16(dev, EHCI_PWR_CTL_STS);
25 reg16 &= ~(PWR_CTL_ENABLE_PME | PWR_CTL_SET_MASK);
26 reg16 |= PWR_CTL_SET_D3;
27 pci_write_config16(dev, EHCI_PWR_CTL_STS, reg16);
28
29 /* Clear memory and bus master */
30 pci_write_config32(dev, PCI_BASE_ADDRESS_0, 0);
Elyes HAOUAS73ae0762020-04-28 10:13:05 +020031 reg16 = pci_read_config16(dev, PCI_COMMAND);
32 reg16 &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
33 pci_write_config16(dev, PCI_COMMAND, reg16);
Duncan Laurie1f529082013-07-30 15:53:45 -070034
35 /* Disable device */
36 switch (dev) {
37 case PCH_EHCI1_DEV:
38 RCBA32_OR(FD, PCH_DISABLE_EHCI1);
39 break;
40 case PCH_EHCI2_DEV:
41 RCBA32_OR(FD, PCH_DISABLE_EHCI2);
42 break;
43 }
44}
45
46/* Handler for EHCI controller on entry to S3/S4/S5 */
Elyes HAOUASab72fc22018-11-29 16:13:14 +010047void usb_ehci_sleep_prepare(pci_devfn_t dev, u8 slp_typ)
Duncan Laurie1f529082013-07-30 15:53:45 -070048{
49 u32 reg32;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080050 u8 *bar0_base;
Duncan Laurie1f529082013-07-30 15:53:45 -070051 u16 pwr_state;
52 u16 pci_cmd;
53
54 /* Check if the controller is disabled or not present */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080055 bar0_base = (u8 *)pci_read_config32(dev, PCI_BASE_ADDRESS_0);
56 if (bar0_base == 0 || bar0_base == (u8 *)0xffffffff)
Duncan Laurie1f529082013-07-30 15:53:45 -070057 return;
Elyes HAOUAS73ae0762020-04-28 10:13:05 +020058 pci_cmd = pci_read_config16(dev, PCI_COMMAND);
Duncan Laurie1f529082013-07-30 15:53:45 -070059
60 switch (slp_typ) {
Aaron Durbinda5f5092016-07-13 23:23:16 -050061 case ACPI_S4:
62 case ACPI_S5:
Duncan Laurie1f529082013-07-30 15:53:45 -070063 /* Check if controller is in D3 power state */
64 pwr_state = pci_read_config16(dev, EHCI_PWR_CTL_STS);
65 if ((pwr_state & PWR_CTL_SET_MASK) == PWR_CTL_SET_D3) {
66 /* Put in D0 */
67 u32 new_state = pwr_state & ~PWR_CTL_SET_MASK;
68 new_state |= PWR_CTL_SET_D0;
69 pci_write_config16(dev, EHCI_PWR_CTL_STS, new_state);
70
71 /* Make sure memory bar is set */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080072 pci_write_config32(dev, PCI_BASE_ADDRESS_0, (u32)bar0_base);
Duncan Laurie1f529082013-07-30 15:53:45 -070073
74 /* Make sure memory space is enabled */
75 pci_write_config16(dev, PCI_COMMAND, pci_cmd |
76 PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
77 }
78
79 /*
80 * If Run/Stop (bit0) is clear in USB2.0_CMD:
81 * - Clear Async Schedule Enable (bit5) and
82 * - Clear Periodic Schedule Enable (bit4) and
83 * - Set Run/Stop (bit0)
84 */
85 reg32 = read32(bar0_base + EHCI_USB_CMD);
86 if (reg32 & EHCI_USB_CMD_RUN) {
87 reg32 &= ~(EHCI_USB_CMD_PSE | EHCI_USB_CMD_ASE);
88 reg32 |= EHCI_USB_CMD_RUN;
89 write32(bar0_base + EHCI_USB_CMD, reg32);
90 }
91
92 /* Check for Port Enabled in PORTSC(0) (RMH) */
93 reg32 = read32(bar0_base + EHCI_PORTSC(0));
94 if (reg32 & EHCI_PORTSC_ENABLED) {
95 /* Set suspend bit in PORTSC if not already set */
96 if (!(reg32 & EHCI_PORTSC_SUSPEND)) {
97 reg32 |= EHCI_PORTSC_SUSPEND;
98 write32(bar0_base + EHCI_PORTSC(0), reg32);
99 }
100
101 /* Delay 25ms !! */
102 udelay(25 * 1000);
103
104 /* Clear Run/Stop bit */
105 reg32 = read32(bar0_base + EHCI_USB_CMD);
106 reg32 &= EHCI_USB_CMD_RUN;
107 write32(bar0_base + EHCI_USB_CMD, reg32);
108 }
109
110 /* Restore state to D3 if that is what it was at the start */
111 if ((pwr_state & PWR_CTL_SET_MASK) == PWR_CTL_SET_D3) {
112 /* Restore pci command reg */
113 pci_write_config16(dev, PCI_COMMAND, pci_cmd);
114
115 /* Enable D3 */
116 pci_write_config16(dev, EHCI_PWR_CTL_STS, pwr_state);
117 }
118 }
119}
120
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200121#else /* !__SIMPLE_DEVICE__ */
Aaron Durbin76c37002012-10-30 09:03:43 -0500122
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700123static void usb_ehci_clock_gating(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500124{
125 u32 reg32;
126
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700127 /* IOBP 0xE5004001[7:6] = 11b */
128 pch_iobp_update(0xe5004001, ~0, (1 << 7)|(1 << 6));
Aaron Durbin76c37002012-10-30 09:03:43 -0500129
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700130 /* Dx:F0:DCh[5,2,1] = 111b
131 * Dx:F0:DCh[0] = 1b when EHCI controller is disabled */
132 reg32 = pci_read_config32(dev, 0xdc);
133 reg32 |= (1 << 5) | (1 << 2) | (1 << 1);
134 pci_write_config32(dev, 0xdc, reg32);
135
136 /* Dx:F0:78h[1:0] = 11b */
137 reg32 = pci_read_config32(dev, 0x78);
138 reg32 |= (1 << 1) | (1 << 0);
139 pci_write_config32(dev, 0x78, reg32);
140}
141
142static void usb_ehci_init(struct device *dev)
143{
Aaron Durbin76c37002012-10-30 09:03:43 -0500144 printk(BIOS_DEBUG, "EHCI: Setting up controller.. ");
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700145
146 usb_ehci_clock_gating(dev);
147
148 /* Disable Wake on Disconnect in RMH */
149 RCBA32_OR(0x35b0, 0x00000022);
Aaron Durbin76c37002012-10-30 09:03:43 -0500150
151 printk(BIOS_DEBUG, "done.\n");
152}
153
Elyes HAOUASab72fc22018-11-29 16:13:14 +0100154static void usb_ehci_set_subsystem(struct device *dev, unsigned int vendor,
155 unsigned int device)
Aaron Durbin76c37002012-10-30 09:03:43 -0500156{
157 u8 access_cntl;
158
159 access_cntl = pci_read_config8(dev, 0x80);
160
161 /* Enable writes to protected registers. */
162 pci_write_config8(dev, 0x80, access_cntl | 1);
163
Subrata Banik4a0f0712019-03-20 14:29:47 +0530164 pci_dev_set_subsystem(dev, vendor, device);
Aaron Durbin76c37002012-10-30 09:03:43 -0500165
166 /* Restore protection. */
167 pci_write_config8(dev, 0x80, access_cntl);
168}
169
Aaron Durbin76c37002012-10-30 09:03:43 -0500170static struct pci_operations lops_pci = {
171 .set_subsystem = &usb_ehci_set_subsystem,
172};
173
174static struct device_operations usb_ehci_ops = {
Kyösti Mälkkifb387df2013-06-07 22:16:52 +0300175 .read_resources = pci_ehci_read_resources,
176 .set_resources = pci_dev_set_resources,
Aaron Durbin76c37002012-10-30 09:03:43 -0500177 .enable_resources = pci_dev_enable_resources,
178 .init = usb_ehci_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500179 .ops_pci = &lops_pci,
180};
181
Kyösti Mälkkif55a5422013-06-14 11:16:25 +0300182static const unsigned short pci_device_ids[] = { 0x9c26, 0x8c26, 0x8c2d, 0 };
Aaron Durbin76c37002012-10-30 09:03:43 -0500183
184static const struct pci_driver pch_usb_ehci __pci_driver = {
185 .ops = &usb_ehci_ops,
186 .vendor = PCI_VENDOR_ID_INTEL,
187 .devices = pci_device_ids,
188};
Duncan Laurie1f529082013-07-30 15:53:45 -0700189
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200190#endif /* !__SIMPLE_DEVICE__ */