Duncan Laurie | b39ba2e | 2013-03-22 11:21:14 -0700 | [diff] [blame] | 1 | /* |
| 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. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <arch/io.h> |
| 22 | #include <cbmem.h> |
| 23 | #include <console/console.h> |
| 24 | #include <device/device.h> |
| 25 | #include <device/pci.h> |
| 26 | #include <device/pciexp.h> |
| 27 | #include <device/pci_ids.h> |
| 28 | #include <stdlib.h> |
| 29 | #include "pch.h" |
| 30 | #include "nvs.h" |
| 31 | |
Duncan Laurie | 98c4062 | 2013-05-21 16:37:40 -0700 | [diff] [blame] | 32 | /* Enable clock in PCI mode */ |
| 33 | static void serialio_enable_clock(struct resource *bar0) |
| 34 | { |
| 35 | u32 reg32 = read32(bar0->base + SIO_REG_PPR_CLOCK); |
| 36 | reg32 |= SIO_REG_PPR_CLOCK_EN; |
| 37 | write32(bar0->base + SIO_REG_PPR_CLOCK, reg32); |
| 38 | } |
| 39 | |
Duncan Laurie | b39ba2e | 2013-03-22 11:21:14 -0700 | [diff] [blame] | 40 | /* Put Serial IO D21:F0-F6 device into desired mode. */ |
| 41 | static void serialio_d21_mode(int sio_index, int int_pin, int acpi_mode) |
| 42 | { |
| 43 | u32 portctrl = SIO_IOBP_PORTCTRL_PM_CAP_PRSNT; |
| 44 | |
| 45 | /* Snoop select 1. */ |
| 46 | portctrl |= SIO_IOBP_PORTCTRL_SNOOP_SELECT(1); |
| 47 | |
| 48 | /* Set interrupt pin. */ |
| 49 | portctrl |= SIO_IOBP_PORTCTRL_INT_PIN(int_pin); |
| 50 | |
| 51 | if (acpi_mode) { |
| 52 | /* Enable ACPI interrupt mode. */ |
| 53 | portctrl |= SIO_IOBP_PORTCTRL_ACPI_IRQ_EN; |
| 54 | |
| 55 | /* Disable PCI config space. */ |
| 56 | portctrl |= SIO_IOBP_PORTCTRL_PCI_CONF_DIS; |
| 57 | } |
| 58 | |
| 59 | pch_iobp_update(SIO_IOBP_PORTCTRLX(sio_index), 0, portctrl); |
| 60 | } |
| 61 | |
| 62 | /* Put Serial IO D23:F0 device into desired mode. */ |
| 63 | static void serialio_d23_mode(int acpi_mode) |
| 64 | { |
| 65 | u32 portctrl = 0; |
| 66 | |
| 67 | /* Snoop select 1. */ |
| 68 | pch_iobp_update(SIO_IOBP_PORTCTRL1, 0, |
| 69 | SIO_IOBP_PORTCTRL1_SNOOP_SELECT(1)); |
| 70 | |
| 71 | if (acpi_mode) { |
| 72 | /* Enable ACPI interrupt mode. */ |
| 73 | portctrl |= SIO_IOBP_PORTCTRL0_ACPI_IRQ_EN; |
| 74 | |
| 75 | /* Disable PCI config space. */ |
| 76 | portctrl |= SIO_IOBP_PORTCTRL0_PCI_CONF_DIS; |
| 77 | } |
| 78 | |
| 79 | pch_iobp_update(SIO_IOBP_PORTCTRL0, 0, portctrl); |
| 80 | } |
| 81 | |
| 82 | /* Enable LTR Auto Mode for D21:F1-F6. */ |
| 83 | static void serialio_d21_ltr(struct resource *bar0) |
| 84 | { |
| 85 | u32 reg; |
| 86 | |
| 87 | /* 1. Program BAR0 + 808h[2] = 0b */ |
| 88 | reg = read32(bar0->base + SIO_REG_PPR_GEN); |
| 89 | reg &= ~SIO_REG_PPR_GEN_LTR_MODE_MASK; |
| 90 | write32(bar0->base + SIO_REG_PPR_GEN, reg); |
| 91 | |
| 92 | /* 2. Program BAR0 + 804h[1:0] = 00b */ |
| 93 | reg = read32(bar0->base + SIO_REG_PPR_RST); |
| 94 | reg &= ~SIO_REG_PPR_RST_ASSERT; |
| 95 | write32(bar0->base + SIO_REG_PPR_RST, reg); |
| 96 | |
| 97 | /* 3. Program BAR0 + 804h[1:0] = 11b */ |
| 98 | reg = read32(bar0->base + SIO_REG_PPR_RST); |
| 99 | reg |= SIO_REG_PPR_RST_ASSERT; |
| 100 | write32(bar0->base + SIO_REG_PPR_RST, reg); |
| 101 | |
| 102 | /* 4. Program BAR0 + 814h[31:0] = 00000000h */ |
| 103 | write32(bar0->base + SIO_REG_AUTO_LTR, 0); |
| 104 | } |
| 105 | |
| 106 | /* Enable LTR Auto Mode for D23:F0. */ |
| 107 | static void serialio_d23_ltr(struct resource *bar0) |
| 108 | { |
| 109 | u32 reg; |
| 110 | |
| 111 | /* Program BAR0 + 1008h[2] = 1b */ |
| 112 | reg = read32(bar0->base + SIO_REG_SDIO_PPR_GEN); |
| 113 | reg |= SIO_REG_PPR_GEN_LTR_MODE_MASK; |
| 114 | write32(bar0->base + SIO_REG_SDIO_PPR_GEN, reg); |
| 115 | |
| 116 | /* Program BAR0 + 1010h = 0x00000000 */ |
| 117 | write32(bar0->base + SIO_REG_SDIO_PPR_SW_LTR, 0); |
| 118 | |
| 119 | /* Program BAR0 + 3Ch[30] = 1b */ |
| 120 | reg = read32(bar0->base + SIO_REG_SDIO_PPR_CMD12); |
| 121 | reg |= SIO_REG_SDIO_PPR_CMD12_B30; |
| 122 | write32(bar0->base + SIO_REG_SDIO_PPR_CMD12, reg); |
| 123 | } |
| 124 | |
| 125 | /* Select I2C voltage of 1.8V or 3.3V. */ |
| 126 | static void serialio_i2c_voltage_sel(struct resource *bar0, u8 voltage) |
| 127 | { |
| 128 | u32 reg32 = read32(bar0->base + SIO_REG_PPR_GEN); |
| 129 | reg32 &= ~SIO_REG_PPR_GEN_VOLTAGE_MASK; |
| 130 | reg32 |= SIO_REG_PPR_GEN_VOLTAGE(voltage); |
| 131 | write32(bar0->base + SIO_REG_PPR_GEN, reg32); |
| 132 | } |
| 133 | |
| 134 | /* Init sequence to be run once, done as part of D21:F0 (SDMA) init. */ |
| 135 | static void serialio_init_once(int acpi_mode) |
| 136 | { |
| 137 | if (acpi_mode) { |
| 138 | /* Enable ACPI IRQ for IRQ13, IRQ7, IRQ6, IRQ5 in RCBA. */ |
| 139 | RCBA32_OR(ACPIIRQEN, (1 << 13)|(1 << 7)|(1 << 6)|(1 << 5)); |
| 140 | } |
| 141 | |
| 142 | /* Program IOBP CB000154h[12,9:8,4:0] = 1001100011111b. */ |
| 143 | pch_iobp_update(SIO_IOBP_GPIODF, ~0x0000131f, 0x0000131f); |
| 144 | |
| 145 | /* Program IOBP CB000180h[5:0] = 111111b (undefined register) */ |
| 146 | pch_iobp_update(0xcb000180, ~0x0000003f, 0x0000003f); |
| 147 | } |
| 148 | |
| 149 | static void serialio_init(struct device *dev) |
| 150 | { |
| 151 | struct southbridge_intel_lynxpoint_config *config = dev->chip_info; |
| 152 | struct resource *bar0, *bar1; |
| 153 | int sio_index = -1; |
Duncan Laurie | 98c4062 | 2013-05-21 16:37:40 -0700 | [diff] [blame] | 154 | u32 reg32; |
Duncan Laurie | b39ba2e | 2013-03-22 11:21:14 -0700 | [diff] [blame] | 155 | |
| 156 | printk(BIOS_DEBUG, "Initializing Serial IO device\n"); |
| 157 | |
Duncan Laurie | 98c4062 | 2013-05-21 16:37:40 -0700 | [diff] [blame] | 158 | /* Ensure memory and bus master are enabled */ |
| 159 | reg32 = pci_read_config32(dev, PCI_COMMAND); |
| 160 | reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY; |
| 161 | pci_write_config32(dev, PCI_COMMAND, reg32); |
| 162 | |
Duncan Laurie | b39ba2e | 2013-03-22 11:21:14 -0700 | [diff] [blame] | 163 | /* Find BAR0 and BAR1 */ |
| 164 | bar0 = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 165 | if (!bar0) |
| 166 | return; |
| 167 | bar1 = find_resource(dev, PCI_BASE_ADDRESS_1); |
| 168 | if (!bar1) |
| 169 | return; |
| 170 | |
Duncan Laurie | 98c4062 | 2013-05-21 16:37:40 -0700 | [diff] [blame] | 171 | if (!config->sio_acpi_mode) |
| 172 | serialio_enable_clock(bar0); |
Duncan Laurie | 98c4062 | 2013-05-21 16:37:40 -0700 | [diff] [blame] | 173 | |
Duncan Laurie | b39ba2e | 2013-03-22 11:21:14 -0700 | [diff] [blame] | 174 | switch (dev->path.pci.devfn) { |
| 175 | case PCI_DEVFN(21, 0): /* SDMA */ |
| 176 | sio_index = SIO_ID_SDMA; |
| 177 | serialio_init_once(config->sio_acpi_mode); |
| 178 | serialio_d21_mode(sio_index, SIO_PIN_INTB, |
| 179 | config->sio_acpi_mode); |
| 180 | break; |
| 181 | case PCI_DEVFN(21, 1): /* I2C0 */ |
| 182 | sio_index = SIO_ID_I2C0; |
| 183 | serialio_d21_ltr(bar0); |
| 184 | serialio_i2c_voltage_sel(bar0, config->sio_i2c0_voltage); |
| 185 | serialio_d21_mode(sio_index, SIO_PIN_INTC, |
| 186 | config->sio_acpi_mode); |
| 187 | break; |
| 188 | case PCI_DEVFN(21, 2): /* I2C1 */ |
| 189 | sio_index = SIO_ID_I2C1; |
| 190 | serialio_d21_ltr(bar0); |
| 191 | serialio_i2c_voltage_sel(bar0, config->sio_i2c1_voltage); |
| 192 | serialio_d21_mode(sio_index, SIO_PIN_INTC, |
| 193 | config->sio_acpi_mode); |
| 194 | break; |
| 195 | case PCI_DEVFN(21, 3): /* SPI0 */ |
| 196 | sio_index = SIO_ID_SPI0; |
| 197 | serialio_d21_ltr(bar0); |
| 198 | serialio_d21_mode(sio_index, SIO_PIN_INTC, |
| 199 | config->sio_acpi_mode); |
| 200 | break; |
| 201 | case PCI_DEVFN(21, 4): /* SPI1 */ |
| 202 | sio_index = SIO_ID_SPI1; |
| 203 | serialio_d21_ltr(bar0); |
| 204 | serialio_d21_mode(sio_index, SIO_PIN_INTC, |
| 205 | config->sio_acpi_mode); |
| 206 | break; |
| 207 | case PCI_DEVFN(21, 5): /* UART0 */ |
| 208 | sio_index = SIO_ID_UART0; |
| 209 | serialio_d21_ltr(bar0); |
| 210 | serialio_d21_mode(sio_index, SIO_PIN_INTD, |
| 211 | config->sio_acpi_mode); |
| 212 | break; |
| 213 | case PCI_DEVFN(21, 6): /* UART1 */ |
| 214 | sio_index = SIO_ID_UART1; |
| 215 | serialio_d21_ltr(bar0); |
| 216 | serialio_d21_mode(sio_index, SIO_PIN_INTD, |
| 217 | config->sio_acpi_mode); |
| 218 | break; |
| 219 | case PCI_DEVFN(23, 0): /* SDIO */ |
| 220 | sio_index = SIO_ID_SDIO; |
| 221 | serialio_d23_ltr(bar0); |
| 222 | serialio_d23_mode(config->sio_acpi_mode); |
| 223 | break; |
| 224 | default: |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | if (config->sio_acpi_mode) { |
| 229 | global_nvs_t *gnvs; |
| 230 | |
| 231 | /* Find ACPI NVS to update BARs */ |
| 232 | gnvs = (global_nvs_t *)cbmem_find(CBMEM_ID_ACPI_GNVS); |
| 233 | if (!gnvs) { |
| 234 | printk(BIOS_ERR, "Unable to locate Global NVS\n"); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | /* Save BAR0 and BAR1 to ACPI NVS */ |
| 239 | gnvs->s0b[sio_index] = (u32)bar0->base; |
| 240 | gnvs->s1b[sio_index] = (u32)bar1->base; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static void serialio_set_subsystem(device_t dev, unsigned vendor, |
| 245 | unsigned device) |
| 246 | { |
| 247 | if (!vendor || !device) { |
| 248 | pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID, |
| 249 | pci_read_config32(dev, PCI_VENDOR_ID)); |
| 250 | } else { |
| 251 | pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID, |
| 252 | ((device & 0xffff) << 16) | (vendor & 0xffff)); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | static struct pci_operations pci_ops = { |
| 257 | .set_subsystem = serialio_set_subsystem, |
| 258 | }; |
| 259 | |
| 260 | static struct device_operations device_ops = { |
Duncan Laurie | 98c4062 | 2013-05-21 16:37:40 -0700 | [diff] [blame] | 261 | .read_resources = pci_dev_read_resources, |
Duncan Laurie | b39ba2e | 2013-03-22 11:21:14 -0700 | [diff] [blame] | 262 | .set_resources = pci_dev_set_resources, |
Duncan Laurie | 98c4062 | 2013-05-21 16:37:40 -0700 | [diff] [blame] | 263 | .enable_resources = pci_dev_enable_resources, |
Duncan Laurie | b39ba2e | 2013-03-22 11:21:14 -0700 | [diff] [blame] | 264 | .init = serialio_init, |
| 265 | .ops_pci = &pci_ops, |
| 266 | }; |
| 267 | |
| 268 | static const unsigned short pci_device_ids[] = { |
| 269 | 0x9c60, /* 0:15.0 - SDMA */ |
| 270 | 0x9c61, /* 0:15.1 - I2C0 */ |
| 271 | 0x9c62, /* 0:15.2 - I2C1 */ |
| 272 | 0x9c65, /* 0:15.3 - SPI0 */ |
| 273 | 0x9c66, /* 0:15.4 - SPI1 */ |
| 274 | 0x9c63, /* 0:15.5 - UART0 */ |
| 275 | 0x9c64, /* 0:15.6 - UART1 */ |
| 276 | 0x9c35, /* 0:17.0 - SDIO */ |
| 277 | 0 |
| 278 | }; |
| 279 | |
| 280 | static const struct pci_driver pch_pcie __pci_driver = { |
| 281 | .ops = &device_ops, |
| 282 | .vendor = PCI_VENDOR_ID_INTEL, |
| 283 | .devices = pci_device_ids, |
| 284 | }; |