blob: 9d751e5c2f005df3b5e2dca1531f24eefa008500 [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.
Duncan Lauriec88c54c2014-04-30 16:36:13 -070014 */
15
16#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020017#include <device/pci_ops.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070018#include <cbmem.h>
19#include <console/console.h>
20#include <device/device.h>
21#include <device/pci.h>
22#include <device/pciexp.h>
23#include <device/pci_ids.h>
24#include <stdlib.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070025#include <soc/iobp.h>
26#include <soc/nvs.h>
27#include <soc/pci_devs.h>
28#include <soc/pch.h>
29#include <soc/ramstage.h>
30#include <soc/rcba.h>
31#include <soc/serialio.h>
32#include <soc/intel/broadwell/chip.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070033
34/* Set D3Hot Power State in ACPI mode */
Duncan Laurie61680272014-05-05 12:42:35 -050035static void serialio_enable_d3hot(struct resource *res)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070036{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080037 u32 reg32 = read32(res2mmio(res, PCH_PCS, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -070038 reg32 |= PCH_PCS_PS_D3HOT;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080039 write32(res2mmio(res, PCH_PCS, 0), reg32);
Duncan Laurie61680272014-05-05 12:42:35 -050040}
41
42static int serialio_uart_is_debug(struct device *dev)
43{
Martin Rothe6ff1592017-06-24 21:34:29 -060044#if IS_ENABLED(CONFIG_INTEL_PCH_UART_CONSOLE)
Duncan Laurie61680272014-05-05 12:42:35 -050045 switch (dev->path.pci.devfn) {
46 case PCH_DEVFN_UART0: /* UART0 */
47 return !!(CONFIG_INTEL_PCH_UART_CONSOLE_NUMBER == 0);
48 case PCH_DEVFN_UART1: /* UART1 */
49 return !!(CONFIG_INTEL_PCH_UART_CONSOLE_NUMBER == 1);
50 }
51#endif
52 return 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070053}
54
55/* Enable clock in PCI mode */
56static void serialio_enable_clock(struct resource *bar0)
57{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080058 u32 reg32 = read32(res2mmio(bar0, SIO_REG_PPR_CLOCK, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -070059 reg32 |= SIO_REG_PPR_CLOCK_EN;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080060 write32(res2mmio(bar0, SIO_REG_PPR_CLOCK, 0), reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070061}
62
63/* Put Serial IO D21:F0-F6 device into desired mode. */
64static void serialio_d21_mode(int sio_index, int int_pin, int acpi_mode)
65{
66 u32 portctrl = SIO_IOBP_PORTCTRL_PM_CAP_PRSNT;
67
68 /* Snoop select 1. */
69 portctrl |= SIO_IOBP_PORTCTRL_SNOOP_SELECT(1);
70
71 /* Set interrupt pin. */
72 portctrl |= SIO_IOBP_PORTCTRL_INT_PIN(int_pin);
73
74 if (acpi_mode) {
75 /* Enable ACPI interrupt mode. */
76 portctrl |= SIO_IOBP_PORTCTRL_ACPI_IRQ_EN;
77
78 /* Disable PCI config space. */
79 portctrl |= SIO_IOBP_PORTCTRL_PCI_CONF_DIS;
80 }
81
82 pch_iobp_update(SIO_IOBP_PORTCTRLX(sio_index), 0, portctrl);
83}
84
85/* Put Serial IO D23:F0 device into desired mode. */
86static void serialio_d23_mode(int acpi_mode)
87{
88 u32 portctrl = 0;
89
90 /* Snoop select 1. */
91 pch_iobp_update(SIO_IOBP_PORTCTRL1, 0,
92 SIO_IOBP_PORTCTRL1_SNOOP_SELECT(1));
93
94 if (acpi_mode) {
95 /* Enable ACPI interrupt mode. */
96 portctrl |= SIO_IOBP_PORTCTRL0_ACPI_IRQ_EN;
97
98 /* Disable PCI config space. */
99 portctrl |= SIO_IOBP_PORTCTRL0_PCI_CONF_DIS;
100 }
101
102 pch_iobp_update(SIO_IOBP_PORTCTRL0, 0, portctrl);
103}
104
105/* Enable LTR Auto Mode for D21:F1-F6. */
106static void serialio_d21_ltr(struct resource *bar0)
107{
108 u32 reg;
109
110 /* 1. Program BAR0 + 808h[2] = 0b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800111 reg = read32(res2mmio(bar0, SIO_REG_PPR_GEN, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700112 reg &= ~SIO_REG_PPR_GEN_LTR_MODE_MASK;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800113 write32(res2mmio(bar0, SIO_REG_PPR_GEN, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700114
115 /* 2. Program BAR0 + 804h[1:0] = 00b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800116 reg = read32(res2mmio(bar0, SIO_REG_PPR_RST, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700117 reg &= ~SIO_REG_PPR_RST_ASSERT;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800118 write32(res2mmio(bar0, SIO_REG_PPR_RST, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700119
120 /* 3. Program BAR0 + 804h[1:0] = 11b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800121 reg = read32(res2mmio(bar0, SIO_REG_PPR_RST, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700122 reg |= SIO_REG_PPR_RST_ASSERT;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800123 write32(res2mmio(bar0, SIO_REG_PPR_RST, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700124
125 /* 4. Program BAR0 + 814h[31:0] = 00000000h */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800126 write32(res2mmio(bar0, SIO_REG_AUTO_LTR, 0), 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700127}
128
129/* Enable LTR Auto Mode for D23:F0. */
130static void serialio_d23_ltr(struct resource *bar0)
131{
132 u32 reg;
133
134 /* Program BAR0 + 1008h[2] = 1b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800135 reg = read32(res2mmio(bar0, SIO_REG_SDIO_PPR_GEN, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700136 reg |= SIO_REG_PPR_GEN_LTR_MODE_MASK;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800137 write32(res2mmio(bar0, SIO_REG_SDIO_PPR_GEN, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700138
139 /* Program BAR0 + 1010h = 0x00000000 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800140 write32(res2mmio(bar0, SIO_REG_SDIO_PPR_SW_LTR, 0), 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700141
142 /* Program BAR0 + 3Ch[30] = 1b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800143 reg = read32(res2mmio(bar0, SIO_REG_SDIO_PPR_CMD12, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700144 reg |= SIO_REG_SDIO_PPR_CMD12_B30;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800145 write32(res2mmio(bar0, SIO_REG_SDIO_PPR_CMD12, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700146}
147
148/* Select I2C voltage of 1.8V or 3.3V. */
149static void serialio_i2c_voltage_sel(struct resource *bar0, u8 voltage)
150{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800151 u32 reg32 = read32(res2mmio(bar0, SIO_REG_PPR_GEN, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700152 reg32 &= ~SIO_REG_PPR_GEN_VOLTAGE_MASK;
153 reg32 |= SIO_REG_PPR_GEN_VOLTAGE(voltage);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800154 write32(res2mmio(bar0, SIO_REG_PPR_GEN, 0), reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700155}
156
157/* Init sequence to be run once, done as part of D21:F0 (SDMA) init. */
158static void serialio_init_once(int acpi_mode)
159{
160 if (acpi_mode) {
161 /* Enable ACPI IRQ for IRQ13, IRQ7, IRQ6, IRQ5 in RCBA. */
162 RCBA32_OR(ACPIIRQEN, (1 << 13)|(1 << 7)|(1 << 6)|(1 << 5));
163 }
164
165 /* Program IOBP CB000154h[12,9:8,4:0] = 1001100011111b. */
166 pch_iobp_update(SIO_IOBP_GPIODF, ~0x0000131f, 0x0000131f);
167
168 /* Program IOBP CB000180h[5:0] = 111111b (undefined register) */
169 pch_iobp_update(0xcb000180, ~0x0000003f, 0x0000003f);
170}
171
172static void serialio_init(struct device *dev)
173{
174 config_t *config = dev->chip_info;
175 struct resource *bar0, *bar1;
176 int sio_index = -1;
177 u32 reg32;
178
179 printk(BIOS_DEBUG, "Initializing Serial IO device\n");
180
181 /* Ensure memory and bus master are enabled */
182 reg32 = pci_read_config32(dev, PCI_COMMAND);
183 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
184 pci_write_config32(dev, PCI_COMMAND, reg32);
185
186 /* Find BAR0 and BAR1 */
187 bar0 = find_resource(dev, PCI_BASE_ADDRESS_0);
188 if (!bar0)
189 return;
190 bar1 = find_resource(dev, PCI_BASE_ADDRESS_1);
191 if (!bar1)
192 return;
193
194 if (!config->sio_acpi_mode)
195 serialio_enable_clock(bar0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700196
197 switch (dev->path.pci.devfn) {
Duncan Laurie61680272014-05-05 12:42:35 -0500198 case PCH_DEVFN_SDMA: /* SDMA */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700199 sio_index = SIO_ID_SDMA;
200 serialio_init_once(config->sio_acpi_mode);
201 serialio_d21_mode(sio_index, SIO_PIN_INTB,
202 config->sio_acpi_mode);
203 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500204 case PCH_DEVFN_I2C0: /* I2C0 */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700205 sio_index = SIO_ID_I2C0;
206 serialio_d21_ltr(bar0);
207 serialio_i2c_voltage_sel(bar0, config->sio_i2c0_voltage);
208 serialio_d21_mode(sio_index, SIO_PIN_INTC,
209 config->sio_acpi_mode);
210 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500211 case PCH_DEVFN_I2C1: /* I2C1 */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700212 sio_index = SIO_ID_I2C1;
213 serialio_d21_ltr(bar0);
214 serialio_i2c_voltage_sel(bar0, config->sio_i2c1_voltage);
215 serialio_d21_mode(sio_index, SIO_PIN_INTC,
216 config->sio_acpi_mode);
217 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500218 case PCH_DEVFN_SPI0: /* SPI0 */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700219 sio_index = SIO_ID_SPI0;
220 serialio_d21_ltr(bar0);
221 serialio_d21_mode(sio_index, SIO_PIN_INTC,
222 config->sio_acpi_mode);
223 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500224 case PCH_DEVFN_SPI1: /* SPI1 */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700225 sio_index = SIO_ID_SPI1;
226 serialio_d21_ltr(bar0);
227 serialio_d21_mode(sio_index, SIO_PIN_INTC,
228 config->sio_acpi_mode);
229 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500230 case PCH_DEVFN_UART0: /* UART0 */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700231 sio_index = SIO_ID_UART0;
Duncan Laurie61680272014-05-05 12:42:35 -0500232 if (!serialio_uart_is_debug(dev))
233 serialio_d21_ltr(bar0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700234 serialio_d21_mode(sio_index, SIO_PIN_INTD,
235 config->sio_acpi_mode);
236 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500237 case PCH_DEVFN_UART1: /* UART1 */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700238 sio_index = SIO_ID_UART1;
Duncan Laurie61680272014-05-05 12:42:35 -0500239 if (!serialio_uart_is_debug(dev))
240 serialio_d21_ltr(bar0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700241 serialio_d21_mode(sio_index, SIO_PIN_INTD,
242 config->sio_acpi_mode);
243 break;
Duncan Laurie61680272014-05-05 12:42:35 -0500244 case PCH_DEVFN_SDIO: /* SDIO */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700245 sio_index = SIO_ID_SDIO;
246 serialio_d23_ltr(bar0);
247 serialio_d23_mode(config->sio_acpi_mode);
248 break;
249 default:
250 return;
251 }
252
253 if (config->sio_acpi_mode) {
254 global_nvs_t *gnvs;
255
256 /* Find ACPI NVS to update BARs */
257 gnvs = (global_nvs_t *)cbmem_find(CBMEM_ID_ACPI_GNVS);
258 if (!gnvs) {
259 printk(BIOS_ERR, "Unable to locate Global NVS\n");
260 return;
261 }
262
263 /* Save BAR0 and BAR1 to ACPI NVS */
264 gnvs->dev.bar0[sio_index] = (u32)bar0->base;
265 gnvs->dev.bar1[sio_index] = (u32)bar1->base;
Duncan Laurie61680272014-05-05 12:42:35 -0500266
267 /* Do not enable UART if it is used as debug port */
268 if (!serialio_uart_is_debug(dev))
269 gnvs->dev.enable[sio_index] = 1;
270
271 /* Put device in D3hot state via BAR1 */
272 if (dev->path.pci.devfn != PCH_DEVFN_SDMA)
273 serialio_enable_d3hot(bar1); /* all but SDMA */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700274 }
275}
276
277static void serialio_set_resources(struct device *dev)
278{
279 pci_dev_set_resources(dev);
280
Martin Rothe6ff1592017-06-24 21:34:29 -0600281#if IS_ENABLED(CONFIG_INTEL_PCH_UART_CONSOLE)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700282 /* Update UART base address if used for debug */
283 if (serialio_uart_is_debug(dev)) {
284 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
285 if (res)
286 uartmem_setbaseaddr(res->base);
287 }
288#endif
289}
290
291static struct device_operations device_ops = {
292 .read_resources = &pci_dev_read_resources,
293 .set_resources = &serialio_set_resources,
294 .enable_resources = &pci_dev_enable_resources,
295 .init = &serialio_init,
296 .ops_pci = &broadwell_pci_ops,
297};
298
299static const unsigned short pci_device_ids[] = {
300 0x9c60, 0x9ce0, /* 0:15.0 - SDMA */
301 0x9c61, 0x9ce1, /* 0:15.1 - I2C0 */
302 0x9c62, 0x9ce2, /* 0:15.2 - I2C1 */
303 0x9c65, 0x9ce5, /* 0:15.3 - SPI0 */
304 0x9c66, 0x9ce6, /* 0:15.4 - SPI1 */
305 0x9c63, 0x9ce3, /* 0:15.5 - UART0 */
306 0x9c64, 0x9ce4, /* 0:15.6 - UART1 */
307 0x9c35, 0x9cb5, /* 0:17.0 - SDIO */
308 0
309};
310
311static const struct pci_driver pch_pcie __pci_driver = {
312 .ops = &device_ops,
313 .vendor = PCI_VENDOR_ID_INTEL,
314 .devices = pci_device_ids,
315};