blob: 8257cc2f6fb12ae019740cb16a931fbe65d49fd7 [file] [log] [blame]
Duncan Laurieb39ba2e2013-03-22 11:21:14 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2013 Google Inc.
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 <arch/io.h>
22#include <cbmem.h>
23#include <console/console.h>
24#include <device/device.h>
25#include <device/pci.h>
26#include <device/pciexp.h>
27#include <device/pci_ids.h>
28#include <stdlib.h>
29#include "pch.h"
30#include "nvs.h"
31
Duncan Laurie98c40622013-05-21 16:37:40 -070032/* Set D3Hot Power State in ACPI mode */
33static void serialio_enable_d3hot(struct device *dev)
34{
35 u32 reg32 = pci_read_config32(dev, PCH_PCS);
36 reg32 |= PCH_PCS_PS_D3HOT;
37 pci_write_config32(dev, PCH_PCS, reg32);
38}
39
40/* Enable clock in PCI mode */
41static void serialio_enable_clock(struct resource *bar0)
42{
43 u32 reg32 = read32(bar0->base + SIO_REG_PPR_CLOCK);
44 reg32 |= SIO_REG_PPR_CLOCK_EN;
45 write32(bar0->base + SIO_REG_PPR_CLOCK, reg32);
46}
47
Duncan Laurieb39ba2e2013-03-22 11:21:14 -070048/* Put Serial IO D21:F0-F6 device into desired mode. */
49static void serialio_d21_mode(int sio_index, int int_pin, int acpi_mode)
50{
51 u32 portctrl = SIO_IOBP_PORTCTRL_PM_CAP_PRSNT;
52
53 /* Snoop select 1. */
54 portctrl |= SIO_IOBP_PORTCTRL_SNOOP_SELECT(1);
55
56 /* Set interrupt pin. */
57 portctrl |= SIO_IOBP_PORTCTRL_INT_PIN(int_pin);
58
59 if (acpi_mode) {
60 /* Enable ACPI interrupt mode. */
61 portctrl |= SIO_IOBP_PORTCTRL_ACPI_IRQ_EN;
62
63 /* Disable PCI config space. */
64 portctrl |= SIO_IOBP_PORTCTRL_PCI_CONF_DIS;
65 }
66
67 pch_iobp_update(SIO_IOBP_PORTCTRLX(sio_index), 0, portctrl);
68}
69
70/* Put Serial IO D23:F0 device into desired mode. */
71static void serialio_d23_mode(int acpi_mode)
72{
73 u32 portctrl = 0;
74
75 /* Snoop select 1. */
76 pch_iobp_update(SIO_IOBP_PORTCTRL1, 0,
77 SIO_IOBP_PORTCTRL1_SNOOP_SELECT(1));
78
79 if (acpi_mode) {
80 /* Enable ACPI interrupt mode. */
81 portctrl |= SIO_IOBP_PORTCTRL0_ACPI_IRQ_EN;
82
83 /* Disable PCI config space. */
84 portctrl |= SIO_IOBP_PORTCTRL0_PCI_CONF_DIS;
85 }
86
87 pch_iobp_update(SIO_IOBP_PORTCTRL0, 0, portctrl);
88}
89
90/* Enable LTR Auto Mode for D21:F1-F6. */
91static void serialio_d21_ltr(struct resource *bar0)
92{
93 u32 reg;
94
95 /* 1. Program BAR0 + 808h[2] = 0b */
96 reg = read32(bar0->base + SIO_REG_PPR_GEN);
97 reg &= ~SIO_REG_PPR_GEN_LTR_MODE_MASK;
98 write32(bar0->base + SIO_REG_PPR_GEN, reg);
99
100 /* 2. Program BAR0 + 804h[1:0] = 00b */
101 reg = read32(bar0->base + SIO_REG_PPR_RST);
102 reg &= ~SIO_REG_PPR_RST_ASSERT;
103 write32(bar0->base + SIO_REG_PPR_RST, reg);
104
105 /* 3. Program BAR0 + 804h[1:0] = 11b */
106 reg = read32(bar0->base + SIO_REG_PPR_RST);
107 reg |= SIO_REG_PPR_RST_ASSERT;
108 write32(bar0->base + SIO_REG_PPR_RST, reg);
109
110 /* 4. Program BAR0 + 814h[31:0] = 00000000h */
111 write32(bar0->base + SIO_REG_AUTO_LTR, 0);
112}
113
114/* Enable LTR Auto Mode for D23:F0. */
115static void serialio_d23_ltr(struct resource *bar0)
116{
117 u32 reg;
118
119 /* Program BAR0 + 1008h[2] = 1b */
120 reg = read32(bar0->base + SIO_REG_SDIO_PPR_GEN);
121 reg |= SIO_REG_PPR_GEN_LTR_MODE_MASK;
122 write32(bar0->base + SIO_REG_SDIO_PPR_GEN, reg);
123
124 /* Program BAR0 + 1010h = 0x00000000 */
125 write32(bar0->base + SIO_REG_SDIO_PPR_SW_LTR, 0);
126
127 /* Program BAR0 + 3Ch[30] = 1b */
128 reg = read32(bar0->base + SIO_REG_SDIO_PPR_CMD12);
129 reg |= SIO_REG_SDIO_PPR_CMD12_B30;
130 write32(bar0->base + SIO_REG_SDIO_PPR_CMD12, reg);
131}
132
133/* Select I2C voltage of 1.8V or 3.3V. */
134static void serialio_i2c_voltage_sel(struct resource *bar0, u8 voltage)
135{
136 u32 reg32 = read32(bar0->base + SIO_REG_PPR_GEN);
137 reg32 &= ~SIO_REG_PPR_GEN_VOLTAGE_MASK;
138 reg32 |= SIO_REG_PPR_GEN_VOLTAGE(voltage);
139 write32(bar0->base + SIO_REG_PPR_GEN, reg32);
140}
141
142/* Init sequence to be run once, done as part of D21:F0 (SDMA) init. */
143static void serialio_init_once(int acpi_mode)
144{
145 if (acpi_mode) {
146 /* Enable ACPI IRQ for IRQ13, IRQ7, IRQ6, IRQ5 in RCBA. */
147 RCBA32_OR(ACPIIRQEN, (1 << 13)|(1 << 7)|(1 << 6)|(1 << 5));
148 }
149
150 /* Program IOBP CB000154h[12,9:8,4:0] = 1001100011111b. */
151 pch_iobp_update(SIO_IOBP_GPIODF, ~0x0000131f, 0x0000131f);
152
153 /* Program IOBP CB000180h[5:0] = 111111b (undefined register) */
154 pch_iobp_update(0xcb000180, ~0x0000003f, 0x0000003f);
155}
156
157static void serialio_init(struct device *dev)
158{
159 struct southbridge_intel_lynxpoint_config *config = dev->chip_info;
160 struct resource *bar0, *bar1;
161 int sio_index = -1;
Duncan Laurie98c40622013-05-21 16:37:40 -0700162 u32 reg32;
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700163
164 printk(BIOS_DEBUG, "Initializing Serial IO device\n");
165
Duncan Laurie98c40622013-05-21 16:37:40 -0700166 /* Ensure memory and bus master are enabled */
167 reg32 = pci_read_config32(dev, PCI_COMMAND);
168 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
169 pci_write_config32(dev, PCI_COMMAND, reg32);
170
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700171 /* Find BAR0 and BAR1 */
172 bar0 = find_resource(dev, PCI_BASE_ADDRESS_0);
173 if (!bar0)
174 return;
175 bar1 = find_resource(dev, PCI_BASE_ADDRESS_1);
176 if (!bar1)
177 return;
178
Duncan Laurie98c40622013-05-21 16:37:40 -0700179 if (!config->sio_acpi_mode)
180 serialio_enable_clock(bar0);
181 else if (dev->path.pci.devfn != PCI_DEVFN(21, 0))
182 serialio_enable_d3hot(dev); /* all but SDMA */
183
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700184 switch (dev->path.pci.devfn) {
185 case PCI_DEVFN(21, 0): /* SDMA */
186 sio_index = SIO_ID_SDMA;
187 serialio_init_once(config->sio_acpi_mode);
188 serialio_d21_mode(sio_index, SIO_PIN_INTB,
189 config->sio_acpi_mode);
190 break;
191 case PCI_DEVFN(21, 1): /* I2C0 */
192 sio_index = SIO_ID_I2C0;
193 serialio_d21_ltr(bar0);
194 serialio_i2c_voltage_sel(bar0, config->sio_i2c0_voltage);
195 serialio_d21_mode(sio_index, SIO_PIN_INTC,
196 config->sio_acpi_mode);
197 break;
198 case PCI_DEVFN(21, 2): /* I2C1 */
199 sio_index = SIO_ID_I2C1;
200 serialio_d21_ltr(bar0);
201 serialio_i2c_voltage_sel(bar0, config->sio_i2c1_voltage);
202 serialio_d21_mode(sio_index, SIO_PIN_INTC,
203 config->sio_acpi_mode);
204 break;
205 case PCI_DEVFN(21, 3): /* SPI0 */
206 sio_index = SIO_ID_SPI0;
207 serialio_d21_ltr(bar0);
208 serialio_d21_mode(sio_index, SIO_PIN_INTC,
209 config->sio_acpi_mode);
210 break;
211 case PCI_DEVFN(21, 4): /* SPI1 */
212 sio_index = SIO_ID_SPI1;
213 serialio_d21_ltr(bar0);
214 serialio_d21_mode(sio_index, SIO_PIN_INTC,
215 config->sio_acpi_mode);
216 break;
217 case PCI_DEVFN(21, 5): /* UART0 */
218 sio_index = SIO_ID_UART0;
219 serialio_d21_ltr(bar0);
220 serialio_d21_mode(sio_index, SIO_PIN_INTD,
221 config->sio_acpi_mode);
222 break;
223 case PCI_DEVFN(21, 6): /* UART1 */
224 sio_index = SIO_ID_UART1;
225 serialio_d21_ltr(bar0);
226 serialio_d21_mode(sio_index, SIO_PIN_INTD,
227 config->sio_acpi_mode);
228 break;
229 case PCI_DEVFN(23, 0): /* SDIO */
230 sio_index = SIO_ID_SDIO;
231 serialio_d23_ltr(bar0);
232 serialio_d23_mode(config->sio_acpi_mode);
233 break;
234 default:
235 return;
236 }
237
238 if (config->sio_acpi_mode) {
239 global_nvs_t *gnvs;
240
241 /* Find ACPI NVS to update BARs */
242 gnvs = (global_nvs_t *)cbmem_find(CBMEM_ID_ACPI_GNVS);
243 if (!gnvs) {
244 printk(BIOS_ERR, "Unable to locate Global NVS\n");
245 return;
246 }
247
248 /* Save BAR0 and BAR1 to ACPI NVS */
249 gnvs->s0b[sio_index] = (u32)bar0->base;
250 gnvs->s1b[sio_index] = (u32)bar1->base;
251 }
252}
253
254static void serialio_set_subsystem(device_t dev, unsigned vendor,
255 unsigned device)
256{
257 if (!vendor || !device) {
258 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
259 pci_read_config32(dev, PCI_VENDOR_ID));
260 } else {
261 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
262 ((device & 0xffff) << 16) | (vendor & 0xffff));
263 }
264}
265
266static struct pci_operations pci_ops = {
267 .set_subsystem = serialio_set_subsystem,
268};
269
270static struct device_operations device_ops = {
Duncan Laurie98c40622013-05-21 16:37:40 -0700271 .read_resources = pci_dev_read_resources,
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700272 .set_resources = pci_dev_set_resources,
Duncan Laurie98c40622013-05-21 16:37:40 -0700273 .enable_resources = pci_dev_enable_resources,
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700274 .init = serialio_init,
275 .ops_pci = &pci_ops,
276};
277
278static const unsigned short pci_device_ids[] = {
279 0x9c60, /* 0:15.0 - SDMA */
280 0x9c61, /* 0:15.1 - I2C0 */
281 0x9c62, /* 0:15.2 - I2C1 */
282 0x9c65, /* 0:15.3 - SPI0 */
283 0x9c66, /* 0:15.4 - SPI1 */
284 0x9c63, /* 0:15.5 - UART0 */
285 0x9c64, /* 0:15.6 - UART1 */
286 0x9c35, /* 0:17.0 - SDIO */
287 0
288};
289
290static const struct pci_driver pch_pcie __pci_driver = {
291 .ops = &device_ops,
292 .vendor = PCI_VENDOR_ID_INTEL,
293 .devices = pci_device_ids,
294};