blob: 336b501b32c7b37ad0eeae78bc10a29c48ec9405 [file] [log] [blame]
Angel Ponsf94ac9a2020-04-05 15:46:48 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Lauriec88c54c2014-04-30 16:36:13 -07002
Kyösti Mälkki13f66502019-03-03 08:01:05 +02003#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02004#include <device/pci_ops.h>
Furquan Shaikh93078ba2021-06-18 12:56:21 +00005#include <acpi/acpi_gnvs.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -07006#include <console/console.h>
7#include <device/device.h>
8#include <device/pci.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -07009#include <device/pci_ids.h>
Furquan Shaikh93078ba2021-06-18 12:56:21 +000010#include <soc/device_nvs.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070011#include <soc/pci_devs.h>
12#include <soc/pch.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070013#include <soc/rcba.h>
14#include <soc/serialio.h>
Angel Pons3cc2c382020-10-23 20:38:23 +020015#include <soc/intel/broadwell/pch/chip.h>
Angel Ponsc423ce22021-04-19 16:13:31 +020016#include <southbridge/intel/lynxpoint/iobp.h>
Angel Pons07baa7a2021-04-19 17:12:42 +020017#include <types.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070018
19/* Set D3Hot Power State in ACPI mode */
Duncan Laurie61680272014-05-05 12:42:35 -050020static void serialio_enable_d3hot(struct resource *res)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070021{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080022 u32 reg32 = read32(res2mmio(res, PCH_PCS, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -070023 reg32 |= PCH_PCS_PS_D3HOT;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080024 write32(res2mmio(res, PCH_PCS, 0), reg32);
Duncan Laurie61680272014-05-05 12:42:35 -050025}
26
Angel Pons07baa7a2021-04-19 17:12:42 +020027static bool serialio_uart_is_debug(struct device *dev)
Duncan Laurie61680272014-05-05 12:42:35 -050028{
Angel Pons07baa7a2021-04-19 17:12:42 +020029 if (CONFIG(SERIALIO_UART_CONSOLE)) {
30 switch (dev->path.pci.devfn) {
31 case PCH_DEVFN_UART0:
32 return CONFIG_UART_FOR_CONSOLE == 0;
33 case PCH_DEVFN_UART1:
34 return CONFIG_UART_FOR_CONSOLE == 1;
35 }
Duncan Laurie61680272014-05-05 12:42:35 -050036 }
Angel Pons07baa7a2021-04-19 17:12:42 +020037 return false;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070038}
39
40/* Enable clock in PCI mode */
41static void serialio_enable_clock(struct resource *bar0)
42{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080043 u32 reg32 = read32(res2mmio(bar0, SIO_REG_PPR_CLOCK, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -070044 reg32 |= SIO_REG_PPR_CLOCK_EN;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080045 write32(res2mmio(bar0, SIO_REG_PPR_CLOCK, 0), reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070046}
47
48/* 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 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080096 reg = read32(res2mmio(bar0, SIO_REG_PPR_GEN, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -070097 reg &= ~SIO_REG_PPR_GEN_LTR_MODE_MASK;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080098 write32(res2mmio(bar0, SIO_REG_PPR_GEN, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070099
100 /* 2. Program BAR0 + 804h[1:0] = 00b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800101 reg = read32(res2mmio(bar0, SIO_REG_PPR_RST, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700102 reg &= ~SIO_REG_PPR_RST_ASSERT;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800103 write32(res2mmio(bar0, SIO_REG_PPR_RST, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700104
105 /* 3. Program BAR0 + 804h[1:0] = 11b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800106 reg = read32(res2mmio(bar0, SIO_REG_PPR_RST, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700107 reg |= SIO_REG_PPR_RST_ASSERT;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800108 write32(res2mmio(bar0, SIO_REG_PPR_RST, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700109
110 /* 4. Program BAR0 + 814h[31:0] = 00000000h */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800111 write32(res2mmio(bar0, SIO_REG_AUTO_LTR, 0), 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700112}
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 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800120 reg = read32(res2mmio(bar0, SIO_REG_SDIO_PPR_GEN, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700121 reg |= SIO_REG_PPR_GEN_LTR_MODE_MASK;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800122 write32(res2mmio(bar0, SIO_REG_SDIO_PPR_GEN, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700123
124 /* Program BAR0 + 1010h = 0x00000000 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800125 write32(res2mmio(bar0, SIO_REG_SDIO_PPR_SW_LTR, 0), 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700126
127 /* Program BAR0 + 3Ch[30] = 1b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800128 reg = read32(res2mmio(bar0, SIO_REG_SDIO_PPR_CMD12, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700129 reg |= SIO_REG_SDIO_PPR_CMD12_B30;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800130 write32(res2mmio(bar0, SIO_REG_SDIO_PPR_CMD12, 0), reg);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700131}
132
133/* Select I2C voltage of 1.8V or 3.3V. */
134static void serialio_i2c_voltage_sel(struct resource *bar0, u8 voltage)
135{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800136 u32 reg32 = read32(res2mmio(bar0, SIO_REG_PPR_GEN, 0));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700137 reg32 &= ~SIO_REG_PPR_GEN_VOLTAGE_MASK;
138 reg32 |= SIO_REG_PPR_GEN_VOLTAGE(voltage);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800139 write32(res2mmio(bar0, SIO_REG_PPR_GEN, 0), reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700140}
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{
Angel Pons3cc2c382020-10-23 20:38:23 +0200159 const struct soc_intel_broadwell_pch_config *config = config_of(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700160 struct resource *bar0, *bar1;
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000161 int sio_index = -1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700162
163 printk(BIOS_DEBUG, "Initializing Serial IO device\n");
164
165 /* Ensure memory and bus master are enabled */
Elyes HAOUASb887adf2020-04-29 10:42:34 +0200166 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700167
168 /* Find BAR0 and BAR1 */
Angel Ponsc1bfbe02021-11-03 13:18:53 +0100169 bar0 = probe_resource(dev, PCI_BASE_ADDRESS_0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700170 if (!bar0)
171 return;
Angel Ponsc1bfbe02021-11-03 13:18:53 +0100172 bar1 = probe_resource(dev, PCI_BASE_ADDRESS_1);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700173 if (!bar1)
174 return;
175
176 if (!config->sio_acpi_mode)
177 serialio_enable_clock(bar0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700178
179 switch (dev->path.pci.devfn) {
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000180 case PCH_DEVFN_SDMA: /* SDMA */
181 sio_index = SIO_ID_SDMA;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700182 serialio_init_once(config->sio_acpi_mode);
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000183 serialio_d21_mode(sio_index, SIO_PIN_INTB,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700184 config->sio_acpi_mode);
185 break;
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000186 case PCH_DEVFN_I2C0: /* I2C0 */
187 sio_index = SIO_ID_I2C0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700188 serialio_d21_ltr(bar0);
189 serialio_i2c_voltage_sel(bar0, config->sio_i2c0_voltage);
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000190 serialio_d21_mode(sio_index, SIO_PIN_INTC,
191 config->sio_acpi_mode);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700192 break;
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000193 case PCH_DEVFN_I2C1: /* I2C1 */
194 sio_index = SIO_ID_I2C1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700195 serialio_d21_ltr(bar0);
196 serialio_i2c_voltage_sel(bar0, config->sio_i2c1_voltage);
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000197 serialio_d21_mode(sio_index, SIO_PIN_INTC,
198 config->sio_acpi_mode);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700199 break;
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000200 case PCH_DEVFN_SPI0: /* SPI0 */
201 sio_index = SIO_ID_SPI0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700202 serialio_d21_ltr(bar0);
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000203 serialio_d21_mode(sio_index, SIO_PIN_INTC,
204 config->sio_acpi_mode);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700205 break;
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000206 case PCH_DEVFN_SPI1: /* SPI1 */
207 sio_index = SIO_ID_SPI1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700208 serialio_d21_ltr(bar0);
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000209 serialio_d21_mode(sio_index, SIO_PIN_INTC,
210 config->sio_acpi_mode);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700211 break;
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000212 case PCH_DEVFN_UART0: /* UART0 */
213 sio_index = SIO_ID_UART0;
Duncan Laurie61680272014-05-05 12:42:35 -0500214 if (!serialio_uart_is_debug(dev))
215 serialio_d21_ltr(bar0);
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000216 serialio_d21_mode(sio_index, SIO_PIN_INTD,
217 config->sio_acpi_mode);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700218 break;
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000219 case PCH_DEVFN_UART1: /* UART1 */
220 sio_index = SIO_ID_UART1;
Duncan Laurie61680272014-05-05 12:42:35 -0500221 if (!serialio_uart_is_debug(dev))
222 serialio_d21_ltr(bar0);
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000223 serialio_d21_mode(sio_index, SIO_PIN_INTD,
224 config->sio_acpi_mode);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700225 break;
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000226 case PCH_DEVFN_SDIO: /* SDIO */
227 sio_index = SIO_ID_SDIO;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700228 serialio_d23_ltr(bar0);
229 serialio_d23_mode(config->sio_acpi_mode);
230 break;
231 default:
232 return;
233 }
234
235 if (config->sio_acpi_mode) {
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000236 struct device_nvs *dev_nvs = acpi_get_device_nvs();
237
238 /* Save BAR0 and BAR1 to ACPI NVS */
239 dev_nvs->bar0[sio_index] = (u32)bar0->base;
240 dev_nvs->bar1[sio_index] = (u32)bar1->base;
Duncan Laurie61680272014-05-05 12:42:35 -0500241
Angel Pons07baa7a2021-04-19 17:12:42 +0200242 if (!serialio_uart_is_debug(dev)) {
243 /* Do not enable UART if it is used as debug port */
Furquan Shaikh93078ba2021-06-18 12:56:21 +0000244 dev_nvs->enable[sio_index] = 1;
Duncan Laurie61680272014-05-05 12:42:35 -0500245
Angel Pons07baa7a2021-04-19 17:12:42 +0200246 /* Put device in D3hot state via BAR1 */
247 if (dev->path.pci.devfn != PCH_DEVFN_SDMA)
248 serialio_enable_d3hot(bar1); /* all but SDMA */
249 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700250 }
251}
252
Angel Pons07baa7a2021-04-19 17:12:42 +0200253static void serialio_read_resources(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700254{
Angel Pons07baa7a2021-04-19 17:12:42 +0200255 pci_dev_read_resources(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700256
Angel Pons07baa7a2021-04-19 17:12:42 +0200257 /* Set the configured UART base address for the debug port */
258 if (CONFIG(SERIALIO_UART_CONSOLE) && serialio_uart_is_debug(dev)) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700259 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
Angel Pons07baa7a2021-04-19 17:12:42 +0200260 res->base = CONFIG_CONSOLE_UART_BASE_ADDRESS;
261 res->size = 0x1000;
262 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700263 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700264}
265
266static struct device_operations device_ops = {
Angel Pons07baa7a2021-04-19 17:12:42 +0200267 .read_resources = &serialio_read_resources,
268 .set_resources = &pci_dev_set_resources,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700269 .enable_resources = &pci_dev_enable_resources,
270 .init = &serialio_init,
Angel Ponscb2080f2020-10-23 15:45:44 +0200271 .ops_pci = &pci_dev_ops_pci,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700272};
273
274static const unsigned short pci_device_ids[] = {
275 0x9c60, 0x9ce0, /* 0:15.0 - SDMA */
276 0x9c61, 0x9ce1, /* 0:15.1 - I2C0 */
277 0x9c62, 0x9ce2, /* 0:15.2 - I2C1 */
278 0x9c65, 0x9ce5, /* 0:15.3 - SPI0 */
279 0x9c66, 0x9ce6, /* 0:15.4 - SPI1 */
280 0x9c63, 0x9ce3, /* 0:15.5 - UART0 */
281 0x9c64, 0x9ce4, /* 0:15.6 - UART1 */
282 0x9c35, 0x9cb5, /* 0:17.0 - SDIO */
283 0
284};
285
286static const struct pci_driver pch_pcie __pci_driver = {
287 .ops = &device_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100288 .vendor = PCI_VID_INTEL,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700289 .devices = pci_device_ids,
290};