blob: 44c9a07882a11bba253e636718085a7c278a64f8 [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.
Duncan Laurieb39ba2e2013-03-22 11:21:14 -070015 */
16
17#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020018#include <device/pci_ops.h>
Duncan Laurieb39ba2e2013-03-22 11:21:14 -070019#include <cbmem.h>
20#include <console/console.h>
21#include <device/device.h>
22#include <device/pci.h>
23#include <device/pciexp.h>
24#include <device/pci_ids.h>
25#include <stdlib.h>
26#include "pch.h"
27#include "nvs.h"
28
Duncan Laurie98c40622013-05-21 16:37:40 -070029/* Enable clock in PCI mode */
30static void serialio_enable_clock(struct resource *bar0)
31{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080032 u32 reg32 = read32(res2mmio(bar0, SIO_REG_PPR_CLOCK, 0));
Duncan Laurie98c40622013-05-21 16:37:40 -070033 reg32 |= SIO_REG_PPR_CLOCK_EN;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080034 write32(res2mmio(bar0, SIO_REG_PPR_CLOCK, 0), reg32);
Duncan Laurie98c40622013-05-21 16:37:40 -070035}
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. */
136 RCBA32_OR(ACPIIRQEN, (1 << 13)|(1 << 7)|(1 << 6)|(1 << 5));
137 }
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
146static void serialio_init(struct device *dev)
147{
148 struct southbridge_intel_lynxpoint_config *config = dev->chip_info;
149 struct resource *bar0, *bar1;
150 int sio_index = -1;
Duncan Laurie98c40622013-05-21 16:37:40 -0700151 u32 reg32;
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700152
153 printk(BIOS_DEBUG, "Initializing Serial IO device\n");
154
Duncan Laurie98c40622013-05-21 16:37:40 -0700155 /* Ensure memory and bus master are enabled */
156 reg32 = pci_read_config32(dev, PCI_COMMAND);
157 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
158 pci_write_config32(dev, PCI_COMMAND, reg32);
159
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700160 /* Find BAR0 and BAR1 */
161 bar0 = find_resource(dev, PCI_BASE_ADDRESS_0);
162 if (!bar0)
163 return;
164 bar1 = find_resource(dev, PCI_BASE_ADDRESS_1);
165 if (!bar1)
166 return;
167
Duncan Laurie98c40622013-05-21 16:37:40 -0700168 if (!config->sio_acpi_mode)
169 serialio_enable_clock(bar0);
Duncan Laurie98c40622013-05-21 16:37:40 -0700170
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700171 switch (dev->path.pci.devfn) {
172 case PCI_DEVFN(21, 0): /* SDMA */
173 sio_index = SIO_ID_SDMA;
174 serialio_init_once(config->sio_acpi_mode);
175 serialio_d21_mode(sio_index, SIO_PIN_INTB,
176 config->sio_acpi_mode);
177 break;
178 case PCI_DEVFN(21, 1): /* I2C0 */
179 sio_index = SIO_ID_I2C0;
180 serialio_d21_ltr(bar0);
181 serialio_i2c_voltage_sel(bar0, config->sio_i2c0_voltage);
182 serialio_d21_mode(sio_index, SIO_PIN_INTC,
183 config->sio_acpi_mode);
184 break;
185 case PCI_DEVFN(21, 2): /* I2C1 */
186 sio_index = SIO_ID_I2C1;
187 serialio_d21_ltr(bar0);
188 serialio_i2c_voltage_sel(bar0, config->sio_i2c1_voltage);
189 serialio_d21_mode(sio_index, SIO_PIN_INTC,
190 config->sio_acpi_mode);
191 break;
192 case PCI_DEVFN(21, 3): /* SPI0 */
193 sio_index = SIO_ID_SPI0;
194 serialio_d21_ltr(bar0);
195 serialio_d21_mode(sio_index, SIO_PIN_INTC,
196 config->sio_acpi_mode);
197 break;
198 case PCI_DEVFN(21, 4): /* SPI1 */
199 sio_index = SIO_ID_SPI1;
200 serialio_d21_ltr(bar0);
201 serialio_d21_mode(sio_index, SIO_PIN_INTC,
202 config->sio_acpi_mode);
203 break;
204 case PCI_DEVFN(21, 5): /* UART0 */
205 sio_index = SIO_ID_UART0;
206 serialio_d21_ltr(bar0);
207 serialio_d21_mode(sio_index, SIO_PIN_INTD,
208 config->sio_acpi_mode);
209 break;
210 case PCI_DEVFN(21, 6): /* UART1 */
211 sio_index = SIO_ID_UART1;
212 serialio_d21_ltr(bar0);
213 serialio_d21_mode(sio_index, SIO_PIN_INTD,
214 config->sio_acpi_mode);
215 break;
216 case PCI_DEVFN(23, 0): /* SDIO */
217 sio_index = SIO_ID_SDIO;
218 serialio_d23_ltr(bar0);
219 serialio_d23_mode(config->sio_acpi_mode);
220 break;
221 default:
222 return;
223 }
224
225 if (config->sio_acpi_mode) {
226 global_nvs_t *gnvs;
227
228 /* Find ACPI NVS to update BARs */
229 gnvs = (global_nvs_t *)cbmem_find(CBMEM_ID_ACPI_GNVS);
230 if (!gnvs) {
231 printk(BIOS_ERR, "Unable to locate Global NVS\n");
232 return;
233 }
234
235 /* Save BAR0 and BAR1 to ACPI NVS */
236 gnvs->s0b[sio_index] = (u32)bar0->base;
237 gnvs->s1b[sio_index] = (u32)bar1->base;
238 }
239}
240
Elyes HAOUAS38f1d132018-09-17 08:44:18 +0200241static void serialio_set_subsystem(struct device *dev, unsigned int vendor,
242 unsigned int device)
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700243{
244 if (!vendor || !device) {
245 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
246 pci_read_config32(dev, PCI_VENDOR_ID));
247 } else {
248 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
249 ((device & 0xffff) << 16) | (vendor & 0xffff));
250 }
251}
252
253static struct pci_operations pci_ops = {
254 .set_subsystem = serialio_set_subsystem,
255};
256
257static struct device_operations device_ops = {
Duncan Laurie98c40622013-05-21 16:37:40 -0700258 .read_resources = pci_dev_read_resources,
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700259 .set_resources = pci_dev_set_resources,
Duncan Laurie98c40622013-05-21 16:37:40 -0700260 .enable_resources = pci_dev_enable_resources,
Duncan Laurieb39ba2e2013-03-22 11:21:14 -0700261 .init = serialio_init,
262 .ops_pci = &pci_ops,
263};
264
265static const unsigned short pci_device_ids[] = {
266 0x9c60, /* 0:15.0 - SDMA */
267 0x9c61, /* 0:15.1 - I2C0 */
268 0x9c62, /* 0:15.2 - I2C1 */
269 0x9c65, /* 0:15.3 - SPI0 */
270 0x9c66, /* 0:15.4 - SPI1 */
271 0x9c63, /* 0:15.5 - UART0 */
272 0x9c64, /* 0:15.6 - UART1 */
273 0x9c35, /* 0:17.0 - SDIO */
274 0
275};
276
277static const struct pci_driver pch_pcie __pci_driver = {
278 .ops = &device_ops,
279 .vendor = PCI_VENDOR_ID_INTEL,
280 .devices = pci_device_ids,
281};