blob: 4382611cf730c46f259f6f167cbfacb95735826c [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <console/console.h>
Duncan Laurie1f529082013-07-30 15:53:45 -070022#include <delay.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050023#include <device/device.h>
24#include <device/pci.h>
25#include <device/pci_ids.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050026#include <usbdebug.h>
27#include <arch/io.h>
Duncan Laurie1f529082013-07-30 15:53:45 -070028#include "pch.h"
29
30#ifdef __SMM__
31
32void usb_ehci_disable(device_t dev)
33{
34 u16 reg16;
35 u32 reg32;
36
37 /* Set 0xDC[0]=1 */
38 pci_or_config32(dev, 0xdc, (1 << 0));
39
40 /* Set D3Hot state and disable PME */
41 reg16 = pci_read_config16(dev, EHCI_PWR_CTL_STS);
42 reg16 &= ~(PWR_CTL_ENABLE_PME | PWR_CTL_SET_MASK);
43 reg16 |= PWR_CTL_SET_D3;
44 pci_write_config16(dev, EHCI_PWR_CTL_STS, reg16);
45
46 /* Clear memory and bus master */
47 pci_write_config32(dev, PCI_BASE_ADDRESS_0, 0);
48 reg32 = pci_read_config32(dev, PCI_COMMAND);
49 reg32 &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
50 pci_write_config32(dev, PCI_COMMAND, reg32);
51
52 /* Disable device */
53 switch (dev) {
54 case PCH_EHCI1_DEV:
55 RCBA32_OR(FD, PCH_DISABLE_EHCI1);
56 break;
57 case PCH_EHCI2_DEV:
58 RCBA32_OR(FD, PCH_DISABLE_EHCI2);
59 break;
60 }
61}
62
63/* Handler for EHCI controller on entry to S3/S4/S5 */
64void usb_ehci_sleep_prepare(device_t dev, u8 slp_typ)
65{
66 u32 reg32;
67 u32 bar0_base;
68 u16 pwr_state;
69 u16 pci_cmd;
70
71 /* Check if the controller is disabled or not present */
72 bar0_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
73 if (bar0_base == 0 || bar0_base == 0xffffffff)
74 return;
75 pci_cmd = pci_read_config32(dev, PCI_COMMAND);
76
77 switch (slp_typ) {
78 case SLP_TYP_S4:
79 case SLP_TYP_S5:
80 /* Check if controller is in D3 power state */
81 pwr_state = pci_read_config16(dev, EHCI_PWR_CTL_STS);
82 if ((pwr_state & PWR_CTL_SET_MASK) == PWR_CTL_SET_D3) {
83 /* Put in D0 */
84 u32 new_state = pwr_state & ~PWR_CTL_SET_MASK;
85 new_state |= PWR_CTL_SET_D0;
86 pci_write_config16(dev, EHCI_PWR_CTL_STS, new_state);
87
88 /* Make sure memory bar is set */
89 pci_write_config32(dev, PCI_BASE_ADDRESS_0, bar0_base);
90
91 /* Make sure memory space is enabled */
92 pci_write_config16(dev, PCI_COMMAND, pci_cmd |
93 PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
94 }
95
96 /*
97 * If Run/Stop (bit0) is clear in USB2.0_CMD:
98 * - Clear Async Schedule Enable (bit5) and
99 * - Clear Periodic Schedule Enable (bit4) and
100 * - Set Run/Stop (bit0)
101 */
102 reg32 = read32(bar0_base + EHCI_USB_CMD);
103 if (reg32 & EHCI_USB_CMD_RUN) {
104 reg32 &= ~(EHCI_USB_CMD_PSE | EHCI_USB_CMD_ASE);
105 reg32 |= EHCI_USB_CMD_RUN;
106 write32(bar0_base + EHCI_USB_CMD, reg32);
107 }
108
109 /* Check for Port Enabled in PORTSC(0) (RMH) */
110 reg32 = read32(bar0_base + EHCI_PORTSC(0));
111 if (reg32 & EHCI_PORTSC_ENABLED) {
112 /* Set suspend bit in PORTSC if not already set */
113 if (!(reg32 & EHCI_PORTSC_SUSPEND)) {
114 reg32 |= EHCI_PORTSC_SUSPEND;
115 write32(bar0_base + EHCI_PORTSC(0), reg32);
116 }
117
118 /* Delay 25ms !! */
119 udelay(25 * 1000);
120
121 /* Clear Run/Stop bit */
122 reg32 = read32(bar0_base + EHCI_USB_CMD);
123 reg32 &= EHCI_USB_CMD_RUN;
124 write32(bar0_base + EHCI_USB_CMD, reg32);
125 }
126
127 /* Restore state to D3 if that is what it was at the start */
128 if ((pwr_state & PWR_CTL_SET_MASK) == PWR_CTL_SET_D3) {
129 /* Restore pci command reg */
130 pci_write_config16(dev, PCI_COMMAND, pci_cmd);
131
132 /* Enable D3 */
133 pci_write_config16(dev, EHCI_PWR_CTL_STS, pwr_state);
134 }
135 }
136}
137
138#else /* !__SMM__ */
Aaron Durbin76c37002012-10-30 09:03:43 -0500139
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700140static void usb_ehci_clock_gating(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500141{
142 u32 reg32;
143
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700144 /* IOBP 0xE5004001[7:6] = 11b */
145 pch_iobp_update(0xe5004001, ~0, (1 << 7)|(1 << 6));
Aaron Durbin76c37002012-10-30 09:03:43 -0500146
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700147 /* Dx:F0:DCh[5,2,1] = 111b
148 * Dx:F0:DCh[0] = 1b when EHCI controller is disabled */
149 reg32 = pci_read_config32(dev, 0xdc);
150 reg32 |= (1 << 5) | (1 << 2) | (1 << 1);
151 pci_write_config32(dev, 0xdc, reg32);
152
153 /* Dx:F0:78h[1:0] = 11b */
154 reg32 = pci_read_config32(dev, 0x78);
155 reg32 |= (1 << 1) | (1 << 0);
156 pci_write_config32(dev, 0x78, reg32);
157}
158
159static void usb_ehci_init(struct device *dev)
160{
Aaron Durbin76c37002012-10-30 09:03:43 -0500161 printk(BIOS_DEBUG, "EHCI: Setting up controller.. ");
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700162
163 usb_ehci_clock_gating(dev);
164
165 /* Disable Wake on Disconnect in RMH */
166 RCBA32_OR(0x35b0, 0x00000022);
Aaron Durbin76c37002012-10-30 09:03:43 -0500167
168 printk(BIOS_DEBUG, "done.\n");
169}
170
171static void usb_ehci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
172{
173 u8 access_cntl;
174
175 access_cntl = pci_read_config8(dev, 0x80);
176
177 /* Enable writes to protected registers. */
178 pci_write_config8(dev, 0x80, access_cntl | 1);
179
180 if (!vendor || !device) {
181 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
182 pci_read_config32(dev, PCI_VENDOR_ID));
183 } else {
184 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
185 ((device & 0xffff) << 16) | (vendor & 0xffff));
186 }
187
188 /* Restore protection. */
189 pci_write_config8(dev, 0x80, access_cntl);
190}
191
Aaron Durbin76c37002012-10-30 09:03:43 -0500192static struct pci_operations lops_pci = {
193 .set_subsystem = &usb_ehci_set_subsystem,
194};
195
196static struct device_operations usb_ehci_ops = {
Kyösti Mälkkifb387df2013-06-07 22:16:52 +0300197 .read_resources = pci_ehci_read_resources,
198 .set_resources = pci_dev_set_resources,
Aaron Durbin76c37002012-10-30 09:03:43 -0500199 .enable_resources = pci_dev_enable_resources,
200 .init = usb_ehci_init,
201 .scan_bus = 0,
202 .ops_pci = &lops_pci,
203};
204
Kyösti Mälkkif55a5422013-06-14 11:16:25 +0300205static const unsigned short pci_device_ids[] = { 0x9c26, 0x8c26, 0x8c2d, 0 };
Aaron Durbin76c37002012-10-30 09:03:43 -0500206
207static const struct pci_driver pch_usb_ehci __pci_driver = {
208 .ops = &usb_ehci_ops,
209 .vendor = PCI_VENDOR_ID_INTEL,
210 .devices = pci_device_ids,
211};
Duncan Laurie1f529082013-07-30 15:53:45 -0700212
213#endif /* !__SMM__ */