blob: 6eba7ac020a5ba5498874d2be6185acdd2c3239a [file] [log] [blame]
Duncan Lauriec88c54c2014-04-30 16:36:13 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <arch/io.h>
21#include <cbmem.h>
22#include <console/console.h>
23#include <device/device.h>
24#include <device/pci.h>
25#include <device/pciexp.h>
26#include <device/pci_ids.h>
27#include <stdlib.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070028#include <soc/iobp.h>
29#include <soc/nvs.h>
30#include <soc/pci_devs.h>
31#include <soc/pch.h>
32#include <soc/ramstage.h>
33#include <soc/rcba.h>
34#include <soc/serialio.h>
35#include <soc/intel/broadwell/chip.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070036
37/* Set D3Hot Power State in ACPI mode */
Duncan Laurie61680272014-05-05 12:42:35 -050038static void serialio_enable_d3hot(struct resource *res)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070039{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080040 u32 reg32 = read32(res2mmio(res, PCH_PCS, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -070041 reg32 |= PCH_PCS_PS_D3HOT;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080042 write32(res2mmio(res, PCH_PCS, 0), reg32);
Duncan Laurie61680272014-05-05 12:42:35 -050043}
44
45static int serialio_uart_is_debug(struct device *dev)
46{
47#if CONFIG_INTEL_PCH_UART_CONSOLE
48 switch (dev->path.pci.devfn) {
49 case PCH_DEVFN_UART0: /* UART0 */
50 return !!(CONFIG_INTEL_PCH_UART_CONSOLE_NUMBER == 0);
51 case PCH_DEVFN_UART1: /* UART1 */
52 return !!(CONFIG_INTEL_PCH_UART_CONSOLE_NUMBER == 1);
53 }
54#endif
55 return 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070056}
57
58/* Enable clock in PCI mode */
59static void serialio_enable_clock(struct resource *bar0)
60{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080061 u32 reg32 = read32(res2mmio(bar0, SIO_REG_PPR_CLOCK, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -070062 reg32 |= SIO_REG_PPR_CLOCK_EN;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080063 write32(res2mmio(bar0, SIO_REG_PPR_CLOCK, 0), reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070064}
65
66/* Put Serial IO D21:F0-F6 device into desired mode. */
67static void serialio_d21_mode(int sio_index, int int_pin, int acpi_mode)
68{
69 u32 portctrl = SIO_IOBP_PORTCTRL_PM_CAP_PRSNT;
70
71 /* Snoop select 1. */
72 portctrl |= SIO_IOBP_PORTCTRL_SNOOP_SELECT(1);
73
74 /* Set interrupt pin. */
75 portctrl |= SIO_IOBP_PORTCTRL_INT_PIN(int_pin);
76
77 if (acpi_mode) {
78 /* Enable ACPI interrupt mode. */
79 portctrl |= SIO_IOBP_PORTCTRL_ACPI_IRQ_EN;
80
81 /* Disable PCI config space. */
82 portctrl |= SIO_IOBP_PORTCTRL_PCI_CONF_DIS;
83 }
84
85 pch_iobp_update(SIO_IOBP_PORTCTRLX(sio_index), 0, portctrl);
86}
87
88/* Put Serial IO D23:F0 device into desired mode. */
89static void serialio_d23_mode(int acpi_mode)
90{
91 u32 portctrl = 0;
92
93 /* Snoop select 1. */
94 pch_iobp_update(SIO_IOBP_PORTCTRL1, 0,
95 SIO_IOBP_PORTCTRL1_SNOOP_SELECT(1));
96
97 if (acpi_mode) {
98 /* Enable ACPI interrupt mode. */
99 portctrl |= SIO_IOBP_PORTCTRL0_ACPI_IRQ_EN;
100
101 /* Disable PCI config space. */
102 portctrl |= SIO_IOBP_PORTCTRL0_PCI_CONF_DIS;
103 }
104
105 pch_iobp_update(SIO_IOBP_PORTCTRL0, 0, portctrl);
106}
107
108/* Enable LTR Auto Mode for D21:F1-F6. */
109static void serialio_d21_ltr(struct resource *bar0)
110{
111 u32 reg;
112
113 /* 1. Program BAR0 + 808h[2] = 0b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800114 reg = read32(res2mmio(bar0, SIO_REG_PPR_GEN, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700115 reg &= ~SIO_REG_PPR_GEN_LTR_MODE_MASK;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800116 write32(res2mmio(bar0, SIO_REG_PPR_GEN, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700117
118 /* 2. Program BAR0 + 804h[1:0] = 00b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800119 reg = read32(res2mmio(bar0, SIO_REG_PPR_RST, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700120 reg &= ~SIO_REG_PPR_RST_ASSERT;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800121 write32(res2mmio(bar0, SIO_REG_PPR_RST, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700122
123 /* 3. Program BAR0 + 804h[1:0] = 11b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800124 reg = read32(res2mmio(bar0, SIO_REG_PPR_RST, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700125 reg |= SIO_REG_PPR_RST_ASSERT;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800126 write32(res2mmio(bar0, SIO_REG_PPR_RST, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700127
128 /* 4. Program BAR0 + 814h[31:0] = 00000000h */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800129 write32(res2mmio(bar0, SIO_REG_AUTO_LTR, 0), 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700130}
131
132/* Enable LTR Auto Mode for D23:F0. */
133static void serialio_d23_ltr(struct resource *bar0)
134{
135 u32 reg;
136
137 /* Program BAR0 + 1008h[2] = 1b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800138 reg = read32(res2mmio(bar0, SIO_REG_SDIO_PPR_GEN, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700139 reg |= SIO_REG_PPR_GEN_LTR_MODE_MASK;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800140 write32(res2mmio(bar0, SIO_REG_SDIO_PPR_GEN, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700141
142 /* Program BAR0 + 1010h = 0x00000000 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800143 write32(res2mmio(bar0, SIO_REG_SDIO_PPR_SW_LTR, 0), 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700144
145 /* Program BAR0 + 3Ch[30] = 1b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800146 reg = read32(res2mmio(bar0, SIO_REG_SDIO_PPR_CMD12, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700147 reg |= SIO_REG_SDIO_PPR_CMD12_B30;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800148 write32(res2mmio(bar0, SIO_REG_SDIO_PPR_CMD12, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700149}
150
151/* Select I2C voltage of 1.8V or 3.3V. */
152static void serialio_i2c_voltage_sel(struct resource *bar0, u8 voltage)
153{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800154 u32 reg32 = read32(res2mmio(bar0, SIO_REG_PPR_GEN, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700155 reg32 &= ~SIO_REG_PPR_GEN_VOLTAGE_MASK;
156 reg32 |= SIO_REG_PPR_GEN_VOLTAGE(voltage);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800157 write32(res2mmio(bar0, SIO_REG_PPR_GEN, 0), reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700158}
159
160/* Init sequence to be run once, done as part of D21:F0 (SDMA) init. */
161static void serialio_init_once(int acpi_mode)
162{
163 if (acpi_mode) {
164 /* Enable ACPI IRQ for IRQ13, IRQ7, IRQ6, IRQ5 in RCBA. */
165 RCBA32_OR(ACPIIRQEN, (1 << 13)|(1 << 7)|(1 << 6)|(1 << 5));
166 }
167
168 /* Program IOBP CB000154h[12,9:8,4:0] = 1001100011111b. */
169 pch_iobp_update(SIO_IOBP_GPIODF, ~0x0000131f, 0x0000131f);
170
171 /* Program IOBP CB000180h[5:0] = 111111b (undefined register) */
172 pch_iobp_update(0xcb000180, ~0x0000003f, 0x0000003f);
173}
174
175static void serialio_init(struct device *dev)
176{
177 config_t *config = dev->chip_info;
178 struct resource *bar0, *bar1;
179 int sio_index = -1;
180 u32 reg32;
181
182 printk(BIOS_DEBUG, "Initializing Serial IO device\n");
183
184 /* Ensure memory and bus master are enabled */
185 reg32 = pci_read_config32(dev, PCI_COMMAND);
186 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
187 pci_write_config32(dev, PCI_COMMAND, reg32);
188
189 /* Find BAR0 and BAR1 */
190 bar0 = find_resource(dev, PCI_BASE_ADDRESS_0);
191 if (!bar0)
192 return;
193 bar1 = find_resource(dev, PCI_BASE_ADDRESS_1);
194 if (!bar1)
195 return;
196
197 if (!config->sio_acpi_mode)
198 serialio_enable_clock(bar0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700199
200 switch (dev->path.pci.devfn) {
Duncan Laurie61680272014-05-05 12:42:35 -0500201 case PCH_DEVFN_SDMA: /* SDMA */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700202 sio_index = SIO_ID_SDMA;
203 serialio_init_once(config->sio_acpi_mode);
204 serialio_d21_mode(sio_index, SIO_PIN_INTB,
205 config->sio_acpi_mode);
206 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500207 case PCH_DEVFN_I2C0: /* I2C0 */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700208 sio_index = SIO_ID_I2C0;
209 serialio_d21_ltr(bar0);
210 serialio_i2c_voltage_sel(bar0, config->sio_i2c0_voltage);
211 serialio_d21_mode(sio_index, SIO_PIN_INTC,
212 config->sio_acpi_mode);
213 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500214 case PCH_DEVFN_I2C1: /* I2C1 */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700215 sio_index = SIO_ID_I2C1;
216 serialio_d21_ltr(bar0);
217 serialio_i2c_voltage_sel(bar0, config->sio_i2c1_voltage);
218 serialio_d21_mode(sio_index, SIO_PIN_INTC,
219 config->sio_acpi_mode);
220 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500221 case PCH_DEVFN_SPI0: /* SPI0 */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700222 sio_index = SIO_ID_SPI0;
223 serialio_d21_ltr(bar0);
224 serialio_d21_mode(sio_index, SIO_PIN_INTC,
225 config->sio_acpi_mode);
226 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500227 case PCH_DEVFN_SPI1: /* SPI1 */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700228 sio_index = SIO_ID_SPI1;
229 serialio_d21_ltr(bar0);
230 serialio_d21_mode(sio_index, SIO_PIN_INTC,
231 config->sio_acpi_mode);
232 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500233 case PCH_DEVFN_UART0: /* UART0 */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700234 sio_index = SIO_ID_UART0;
Duncan Laurie61680272014-05-05 12:42:35 -0500235 if (!serialio_uart_is_debug(dev))
236 serialio_d21_ltr(bar0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700237 serialio_d21_mode(sio_index, SIO_PIN_INTD,
238 config->sio_acpi_mode);
239 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500240 case PCH_DEVFN_UART1: /* UART1 */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700241 sio_index = SIO_ID_UART1;
Duncan Laurie61680272014-05-05 12:42:35 -0500242 if (!serialio_uart_is_debug(dev))
243 serialio_d21_ltr(bar0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700244 serialio_d21_mode(sio_index, SIO_PIN_INTD,
245 config->sio_acpi_mode);
246 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500247 case PCH_DEVFN_SDIO: /* SDIO */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700248 sio_index = SIO_ID_SDIO;
249 serialio_d23_ltr(bar0);
250 serialio_d23_mode(config->sio_acpi_mode);
251 break;
252 default:
253 return;
254 }
255
256 if (config->sio_acpi_mode) {
257 global_nvs_t *gnvs;
258
259 /* Find ACPI NVS to update BARs */
260 gnvs = (global_nvs_t *)cbmem_find(CBMEM_ID_ACPI_GNVS);
261 if (!gnvs) {
262 printk(BIOS_ERR, "Unable to locate Global NVS\n");
263 return;
264 }
265
266 /* Save BAR0 and BAR1 to ACPI NVS */
267 gnvs->dev.bar0[sio_index] = (u32)bar0->base;
268 gnvs->dev.bar1[sio_index] = (u32)bar1->base;
Duncan Laurie61680272014-05-05 12:42:35 -0500269
270 /* Do not enable UART if it is used as debug port */
271 if (!serialio_uart_is_debug(dev))
272 gnvs->dev.enable[sio_index] = 1;
273
274 /* Put device in D3hot state via BAR1 */
275 if (dev->path.pci.devfn != PCH_DEVFN_SDMA)
276 serialio_enable_d3hot(bar1); /* all but SDMA */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700277 }
278}
279
280static void serialio_set_resources(struct device *dev)
281{
282 pci_dev_set_resources(dev);
283
284#if CONFIG_INTEL_PCH_UART_CONSOLE
285 /* Update UART base address if used for debug */
286 if (serialio_uart_is_debug(dev)) {
287 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
288 if (res)
289 uartmem_setbaseaddr(res->base);
290 }
291#endif
292}
293
294static struct device_operations device_ops = {
295 .read_resources = &pci_dev_read_resources,
296 .set_resources = &serialio_set_resources,
297 .enable_resources = &pci_dev_enable_resources,
298 .init = &serialio_init,
299 .ops_pci = &broadwell_pci_ops,
300};
301
302static const unsigned short pci_device_ids[] = {
303 0x9c60, 0x9ce0, /* 0:15.0 - SDMA */
304 0x9c61, 0x9ce1, /* 0:15.1 - I2C0 */
305 0x9c62, 0x9ce2, /* 0:15.2 - I2C1 */
306 0x9c65, 0x9ce5, /* 0:15.3 - SPI0 */
307 0x9c66, 0x9ce6, /* 0:15.4 - SPI1 */
308 0x9c63, 0x9ce3, /* 0:15.5 - UART0 */
309 0x9c64, 0x9ce4, /* 0:15.6 - UART1 */
310 0x9c35, 0x9cb5, /* 0:17.0 - SDIO */
311 0
312};
313
314static const struct pci_driver pch_pcie __pci_driver = {
315 .ops = &device_ops,
316 .vendor = PCI_VENDOR_ID_INTEL,
317 .devices = pci_device_ids,
318};