blob: 208c52ab44e66c89aeaccdb97e6f01f06e18c447 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Laurieb39ba2e2013-03-22 11:21:14 -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>
Kyösti Mälkki5daa1d32020-06-14 12:01:58 +03005#include <acpi/acpi_gnvs.h>
Duncan Laurieb39ba2e2013-03-22 11:21:14 -07006#include <console/console.h>
7#include <device/device.h>
8#include <device/pci.h>
Duncan Laurieb39ba2e2013-03-22 11:21:14 -07009#include <device/pci_ids.h>
Kyösti Mälkki661ad462020-12-29 06:26:21 +020010#include <soc/nvs.h>
Angel Ponsaced1f02021-04-18 23:57:21 +020011#include <types.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030012#include "chip.h"
Angel Pons2178b722020-05-31 00:55:35 +020013#include "iobp.h"
Duncan Laurieb39ba2e2013-03-22 11:21:14 -070014#include "pch.h"
Duncan Laurieb39ba2e2013-03-22 11:21:14 -070015
Duncan Laurie98c40622013-05-21 16:37:40 -070016/* Enable clock in PCI mode */
17static void serialio_enable_clock(struct resource *bar0)
18{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080019 u32 reg32 = read32(res2mmio(bar0, SIO_REG_PPR_CLOCK, 0));
Duncan Laurie98c40622013-05-21 16:37:40 -070020 reg32 |= SIO_REG_PPR_CLOCK_EN;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080021 write32(res2mmio(bar0, SIO_REG_PPR_CLOCK, 0), reg32);
Duncan Laurie98c40622013-05-21 16:37:40 -070022}
23
Angel Ponsaced1f02021-04-18 23:57:21 +020024static bool serialio_uart_is_debug(struct device *dev)
25{
26 if (CONFIG(SERIALIO_UART_CONSOLE)) {
27 switch (dev->path.pci.devfn) {
28 case PCH_DEVFN_UART0:
29 return CONFIG_UART_FOR_CONSOLE == 0;
30 case PCH_DEVFN_UART1:
31 return CONFIG_UART_FOR_CONSOLE == 1;
32 }
33 }
34 return 0;
35}
36
Duncan Laurieb39ba2e2013-03-22 11:21:14 -070037/* Put Serial IO D21:F0-F6 device into desired mode. */
38static void serialio_d21_mode(int sio_index, int int_pin, int acpi_mode)
39{
40 u32 portctrl = SIO_IOBP_PORTCTRL_PM_CAP_PRSNT;
41
42 /* Snoop select 1. */
43 portctrl |= SIO_IOBP_PORTCTRL_SNOOP_SELECT(1);
44
45 /* Set interrupt pin. */
46 portctrl |= SIO_IOBP_PORTCTRL_INT_PIN(int_pin);
47
48 if (acpi_mode) {
49 /* Enable ACPI interrupt mode. */
50 portctrl |= SIO_IOBP_PORTCTRL_ACPI_IRQ_EN;
51
52 /* Disable PCI config space. */
53 portctrl |= SIO_IOBP_PORTCTRL_PCI_CONF_DIS;
54 }
55
56 pch_iobp_update(SIO_IOBP_PORTCTRLX(sio_index), 0, portctrl);
57}
58
59/* Put Serial IO D23:F0 device into desired mode. */
60static void serialio_d23_mode(int acpi_mode)
61{
62 u32 portctrl = 0;
63
64 /* Snoop select 1. */
65 pch_iobp_update(SIO_IOBP_PORTCTRL1, 0,
66 SIO_IOBP_PORTCTRL1_SNOOP_SELECT(1));
67
68 if (acpi_mode) {
69 /* Enable ACPI interrupt mode. */
70 portctrl |= SIO_IOBP_PORTCTRL0_ACPI_IRQ_EN;
71
72 /* Disable PCI config space. */
73 portctrl |= SIO_IOBP_PORTCTRL0_PCI_CONF_DIS;
74 }
75
76 pch_iobp_update(SIO_IOBP_PORTCTRL0, 0, portctrl);
77}
78
79/* Enable LTR Auto Mode for D21:F1-F6. */
80static void serialio_d21_ltr(struct resource *bar0)
81{
82 u32 reg;
83
84 /* 1. Program BAR0 + 808h[2] = 0b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080085 reg = read32(res2mmio(bar0, SIO_REG_PPR_GEN, 0));
Duncan Laurieb39ba2e2013-03-22 11:21:14 -070086 reg &= ~SIO_REG_PPR_GEN_LTR_MODE_MASK;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080087 write32(res2mmio(bar0, SIO_REG_PPR_GEN, 0), reg);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -070088
89 /* 2. Program BAR0 + 804h[1:0] = 00b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080090 reg = read32(res2mmio(bar0, SIO_REG_PPR_RST, 0));
Duncan Laurieb39ba2e2013-03-22 11:21:14 -070091 reg &= ~SIO_REG_PPR_RST_ASSERT;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080092 write32(res2mmio(bar0, SIO_REG_PPR_RST, 0), reg);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -070093
94 /* 3. Program BAR0 + 804h[1:0] = 11b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080095 reg = read32(res2mmio(bar0, SIO_REG_PPR_RST, 0));
Duncan Laurieb39ba2e2013-03-22 11:21:14 -070096 reg |= SIO_REG_PPR_RST_ASSERT;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080097 write32(res2mmio(bar0, SIO_REG_PPR_RST, 0), reg);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -070098
99 /* 4. Program BAR0 + 814h[31:0] = 00000000h */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800100 write32(res2mmio(bar0, SIO_REG_AUTO_LTR, 0), 0);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700101}
102
103/* Enable LTR Auto Mode for D23:F0. */
104static void serialio_d23_ltr(struct resource *bar0)
105{
106 u32 reg;
107
108 /* Program BAR0 + 1008h[2] = 1b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800109 reg = read32(res2mmio(bar0, SIO_REG_SDIO_PPR_GEN, 0));
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700110 reg |= SIO_REG_PPR_GEN_LTR_MODE_MASK;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800111 write32(res2mmio(bar0, SIO_REG_SDIO_PPR_GEN, 0), reg);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700112
113 /* Program BAR0 + 1010h = 0x00000000 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800114 write32(res2mmio(bar0, SIO_REG_SDIO_PPR_SW_LTR, 0), 0);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700115
116 /* Program BAR0 + 3Ch[30] = 1b */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800117 reg = read32(res2mmio(bar0, SIO_REG_SDIO_PPR_CMD12, 0));
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700118 reg |= SIO_REG_SDIO_PPR_CMD12_B30;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800119 write32(res2mmio(bar0, SIO_REG_SDIO_PPR_CMD12, 0), reg);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700120}
121
122/* Select I2C voltage of 1.8V or 3.3V. */
123static void serialio_i2c_voltage_sel(struct resource *bar0, u8 voltage)
124{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800125 u32 reg32 = read32(res2mmio(bar0, SIO_REG_PPR_GEN, 0));
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700126 reg32 &= ~SIO_REG_PPR_GEN_VOLTAGE_MASK;
127 reg32 |= SIO_REG_PPR_GEN_VOLTAGE(voltage);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800128 write32(res2mmio(bar0, SIO_REG_PPR_GEN, 0), reg32);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700129}
130
131/* Init sequence to be run once, done as part of D21:F0 (SDMA) init. */
132static void serialio_init_once(int acpi_mode)
133{
134 if (acpi_mode) {
135 /* Enable ACPI IRQ for IRQ13, IRQ7, IRQ6, IRQ5 in RCBA. */
Angel Pons84fa2242020-10-24 11:53:47 +0200136 RCBA32_OR(ACPIIRQEN, (1 << 13) | (1 << 7) | (1 << 6) | (1 << 5));
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700137 }
138
139 /* Program IOBP CB000154h[12,9:8,4:0] = 1001100011111b. */
140 pch_iobp_update(SIO_IOBP_GPIODF, ~0x0000131f, 0x0000131f);
141
142 /* Program IOBP CB000180h[5:0] = 111111b (undefined register) */
143 pch_iobp_update(0xcb000180, ~0x0000003f, 0x0000003f);
144}
145
Kyösti Mälkki5e82d442020-12-22 11:31:39 +0200146static void update_bars(int sio_index, u32 bar0, u32 bar1)
147{
148 /* Find ACPI NVS to update BARs */
149 struct global_nvs *gnvs = acpi_get_gnvs();
150 if (!gnvs)
151 return;
152
153 gnvs->s0b[sio_index] = bar0;
154 gnvs->s1b[sio_index] = bar1;
155}
156
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700157static void serialio_init(struct device *dev)
158{
Angel Ponscbcbb672020-10-23 00:11:26 +0200159 struct southbridge_intel_lynxpoint_config *config = config_of(dev);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700160 struct resource *bar0, *bar1;
161 int sio_index = -1;
162
163 printk(BIOS_DEBUG, "Initializing Serial IO device\n");
164
Duncan Laurie98c40622013-05-21 16:37:40 -0700165 /* Ensure memory and bus master are enabled */
Angel Ponsd5d4fbc2020-05-31 01:03:59 +0200166 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
Duncan Laurie98c40622013-05-21 16:37:40 -0700167
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700168 /* Find BAR0 and BAR1 */
Angel Ponsf32ae102021-11-03 13:07:14 +0100169 bar0 = probe_resource(dev, PCI_BASE_ADDRESS_0);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700170 if (!bar0)
171 return;
Angel Ponsf32ae102021-11-03 13:07:14 +0100172 bar1 = probe_resource(dev, PCI_BASE_ADDRESS_1);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700173 if (!bar1)
174 return;
175
Duncan Laurie98c40622013-05-21 16:37:40 -0700176 if (!config->sio_acpi_mode)
177 serialio_enable_clock(bar0);
Duncan Laurie98c40622013-05-21 16:37:40 -0700178
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700179 switch (dev->path.pci.devfn) {
Angel Pons30392ae2020-07-12 01:06:23 +0200180 case PCH_DEVFN_SDMA: /* SDMA */
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700181 sio_index = SIO_ID_SDMA;
182 serialio_init_once(config->sio_acpi_mode);
183 serialio_d21_mode(sio_index, SIO_PIN_INTB,
184 config->sio_acpi_mode);
185 break;
Angel Pons30392ae2020-07-12 01:06:23 +0200186 case PCH_DEVFN_I2C0: /* I2C0 */
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700187 sio_index = SIO_ID_I2C0;
188 serialio_d21_ltr(bar0);
189 serialio_i2c_voltage_sel(bar0, config->sio_i2c0_voltage);
190 serialio_d21_mode(sio_index, SIO_PIN_INTC,
191 config->sio_acpi_mode);
192 break;
Angel Pons30392ae2020-07-12 01:06:23 +0200193 case PCH_DEVFN_I2C1: /* I2C1 */
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700194 sio_index = SIO_ID_I2C1;
195 serialio_d21_ltr(bar0);
196 serialio_i2c_voltage_sel(bar0, config->sio_i2c1_voltage);
197 serialio_d21_mode(sio_index, SIO_PIN_INTC,
198 config->sio_acpi_mode);
199 break;
Angel Pons30392ae2020-07-12 01:06:23 +0200200 case PCH_DEVFN_SPI0: /* SPI0 */
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700201 sio_index = SIO_ID_SPI0;
202 serialio_d21_ltr(bar0);
203 serialio_d21_mode(sio_index, SIO_PIN_INTC,
204 config->sio_acpi_mode);
205 break;
Angel Pons30392ae2020-07-12 01:06:23 +0200206 case PCH_DEVFN_SPI1: /* SPI1 */
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700207 sio_index = SIO_ID_SPI1;
208 serialio_d21_ltr(bar0);
209 serialio_d21_mode(sio_index, SIO_PIN_INTC,
210 config->sio_acpi_mode);
211 break;
Angel Pons30392ae2020-07-12 01:06:23 +0200212 case PCH_DEVFN_UART0: /* UART0 */
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700213 sio_index = SIO_ID_UART0;
Angel Ponsaced1f02021-04-18 23:57:21 +0200214 if (!serialio_uart_is_debug(dev))
215 serialio_d21_ltr(bar0);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700216 serialio_d21_mode(sio_index, SIO_PIN_INTD,
217 config->sio_acpi_mode);
218 break;
Angel Pons30392ae2020-07-12 01:06:23 +0200219 case PCH_DEVFN_UART1: /* UART1 */
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700220 sio_index = SIO_ID_UART1;
Angel Ponsaced1f02021-04-18 23:57:21 +0200221 if (!serialio_uart_is_debug(dev))
222 serialio_d21_ltr(bar0);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700223 serialio_d21_mode(sio_index, SIO_PIN_INTD,
224 config->sio_acpi_mode);
225 break;
Angel Pons30392ae2020-07-12 01:06:23 +0200226 case PCH_DEVFN_SDIO: /* SDIO */
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700227 sio_index = SIO_ID_SDIO;
228 serialio_d23_ltr(bar0);
229 serialio_d23_mode(config->sio_acpi_mode);
230 break;
231 default:
232 return;
233 }
234
Kyösti Mälkki5e82d442020-12-22 11:31:39 +0200235 /* Save BAR0 and BAR1 to ACPI NVS */
236 if (config->sio_acpi_mode)
237 update_bars(sio_index, (u32)bar0->base, (u32)bar1->base);
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700238}
239
Angel Ponsaced1f02021-04-18 23:57:21 +0200240static void serialio_read_resources(struct device *dev)
241{
242 pci_dev_read_resources(dev);
243
244 /* Set the configured UART base address for the debug port */
245 if (CONFIG(SERIALIO_UART_CONSOLE) && serialio_uart_is_debug(dev)) {
246 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
247 res->base = CONFIG_CONSOLE_UART_BASE_ADDRESS;
248 res->size = 0x1000;
249 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
250 }
251}
252
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700253static struct device_operations device_ops = {
Angel Ponsaced1f02021-04-18 23:57:21 +0200254 .read_resources = serialio_read_resources,
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700255 .set_resources = pci_dev_set_resources,
Duncan Laurie98c40622013-05-21 16:37:40 -0700256 .enable_resources = pci_dev_enable_resources,
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700257 .init = serialio_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200258 .ops_pci = &pci_dev_ops_pci,
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700259};
260
261static const unsigned short pci_device_ids[] = {
Felix Singer43b7f412022-03-07 04:34:52 +0100262 PCI_DID_INTEL_LPT_LP_SDMA,
263 PCI_DID_INTEL_LPT_LP_I2C0,
264 PCI_DID_INTEL_LPT_LP_I2C1,
265 PCI_DID_INTEL_LPT_LP_GSPI0,
266 PCI_DID_INTEL_LPT_LP_GSPI1,
267 PCI_DID_INTEL_LPT_LP_UART0,
268 PCI_DID_INTEL_LPT_LP_UART1,
269 PCI_DID_INTEL_LPT_LP_SD,
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700270 0
271};
272
273static const struct pci_driver pch_pcie __pci_driver = {
274 .ops = &device_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100275 .vendor = PCI_VID_INTEL,
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700276 .devices = pci_device_ids,
277};