Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 4 | * Copyright (C) 2008-2009 coresystems GmbH |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 5 | * |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 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. |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 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. |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <console/console.h> |
| 18 | #include <device/device.h> |
| 19 | #include <device/pci.h> |
| 20 | #include <device/pci_ids.h> |
Arthur Heymans | e6e5ecb | 2018-12-20 01:44:50 +0100 | [diff] [blame] | 21 | #include "i82801gx.h" |
| 22 | |
| 23 | /* Low Power variant has 6 root ports. */ |
| 24 | #define NUM_ROOT_PORTS 6 |
| 25 | |
| 26 | struct root_port_config { |
| 27 | /* RPFN is a write-once register so keep a copy until it is written */ |
| 28 | u32 orig_rpfn; |
| 29 | u32 new_rpfn; |
| 30 | int num_ports; |
| 31 | struct device *ports[NUM_ROOT_PORTS]; |
| 32 | }; |
| 33 | |
| 34 | static struct root_port_config rpc; |
| 35 | |
| 36 | static inline int root_port_is_first(struct device *dev) |
| 37 | { |
| 38 | return PCI_FUNC(dev->path.pci.devfn) == 0; |
| 39 | } |
| 40 | |
| 41 | static inline int root_port_is_last(struct device *dev) |
| 42 | { |
| 43 | return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1); |
| 44 | } |
| 45 | |
| 46 | /* Root ports are numbered 1..N in the documentation. */ |
| 47 | static inline int root_port_number(struct device *dev) |
| 48 | { |
| 49 | return PCI_FUNC(dev->path.pci.devfn) + 1; |
| 50 | } |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 51 | |
| 52 | static void pci_init(struct device *dev) |
| 53 | { |
| 54 | u16 reg16; |
| 55 | u32 reg32; |
| 56 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 57 | printk(BIOS_DEBUG, "Initializing ICH7 PCIe bridge.\n"); |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 58 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 59 | /* Enable Bus Master */ |
| 60 | reg32 = pci_read_config32(dev, PCI_COMMAND); |
| 61 | reg32 |= PCI_COMMAND_MASTER; |
| 62 | pci_write_config32(dev, PCI_COMMAND, reg32); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 63 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 64 | /* Set Cache Line Size to 0x10 */ |
| 65 | // This has no effect but the OS might expect it |
| 66 | pci_write_config8(dev, 0x0c, 0x10); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 67 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 68 | reg16 = pci_read_config16(dev, 0x3e); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 69 | reg16 &= ~(1 << 0); /* disable parity error response */ |
| 70 | // reg16 &= ~(1 << 1); /* disable SERR */ |
| 71 | reg16 |= (1 << 2); /* ISA enable */ |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 72 | pci_write_config16(dev, 0x3e, reg16); |
| 73 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 74 | /* Enable IO xAPIC on this PCIe port */ |
| 75 | reg32 = pci_read_config32(dev, 0xd8); |
| 76 | reg32 |= (1 << 7); |
| 77 | pci_write_config32(dev, 0xd8, reg32); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 78 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 79 | /* Enable Backbone Clock Gating */ |
| 80 | reg32 = pci_read_config32(dev, 0xe1); |
| 81 | reg32 |= (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0); |
| 82 | pci_write_config32(dev, 0xe1, reg32); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 83 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 84 | /* Set VC0 transaction class */ |
Kyösti Mälkki | 8aa7e83 | 2013-07-26 08:52:10 +0300 | [diff] [blame] | 85 | reg32 = pci_read_config32(dev, 0x114); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 86 | reg32 &= 0xffffff00; |
| 87 | reg32 |= 1; |
Kyösti Mälkki | 8aa7e83 | 2013-07-26 08:52:10 +0300 | [diff] [blame] | 88 | pci_write_config32(dev, 0x114, reg32); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 89 | |
| 90 | /* Mask completion timeouts */ |
Kyösti Mälkki | 8aa7e83 | 2013-07-26 08:52:10 +0300 | [diff] [blame] | 91 | reg32 = pci_read_config32(dev, 0x148); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 92 | reg32 |= (1 << 14); |
Kyösti Mälkki | 8aa7e83 | 2013-07-26 08:52:10 +0300 | [diff] [blame] | 93 | pci_write_config32(dev, 0x148, reg32); |
| 94 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 95 | /* Enable common clock configuration */ |
| 96 | // Are there cases when we don't want that? |
| 97 | reg16 = pci_read_config16(dev, 0x50); |
| 98 | reg16 |= (1 << 6); |
| 99 | pci_write_config16(dev, 0x50, reg16); |
| 100 | |
Stefan Reinauer | de3206a | 2010-02-22 06:09:43 +0000 | [diff] [blame] | 101 | #ifdef EVEN_MORE_DEBUG |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 102 | reg32 = pci_read_config32(dev, 0x20); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 103 | printk(BIOS_SPEW, " MBL = 0x%08x\n", reg32); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 104 | reg32 = pci_read_config32(dev, 0x24); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 105 | printk(BIOS_SPEW, " PMBL = 0x%08x\n", reg32); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 106 | reg32 = pci_read_config32(dev, 0x28); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 107 | printk(BIOS_SPEW, " PMBU32 = 0x%08x\n", reg32); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 108 | reg32 = pci_read_config32(dev, 0x2c); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 109 | printk(BIOS_SPEW, " PMLU32 = 0x%08x\n", reg32); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 110 | #endif |
| 111 | |
| 112 | /* Clear errors in status registers */ |
| 113 | reg16 = pci_read_config16(dev, 0x06); |
| 114 | //reg16 |= 0xf900; |
| 115 | pci_write_config16(dev, 0x06, reg16); |
| 116 | |
| 117 | reg16 = pci_read_config16(dev, 0x1e); |
| 118 | //reg16 |= 0xf900; |
| 119 | pci_write_config16(dev, 0x1e, reg16); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Arthur Heymans | e6e5ecb | 2018-12-20 01:44:50 +0100 | [diff] [blame] | 122 | static int get_num_ports(void) |
| 123 | { |
| 124 | struct device *dev = pcidev_on_root(31, 0); |
| 125 | if (pci_read_config32(dev, FDVCT) & PCIE_4_PORTS_MAX) |
| 126 | return 4; |
| 127 | else |
| 128 | return 6; |
| 129 | } |
| 130 | |
| 131 | static void root_port_init_config(struct device *dev) |
| 132 | { |
| 133 | int rp; |
| 134 | |
| 135 | if (root_port_is_first(dev)) { |
| 136 | rpc.orig_rpfn = RCBA32(RPFN); |
| 137 | rpc.new_rpfn = rpc.orig_rpfn; |
| 138 | rpc.num_ports = get_num_ports(); |
| 139 | } |
| 140 | |
| 141 | rp = root_port_number(dev); |
| 142 | if (rp > rpc.num_ports) { |
| 143 | printk(BIOS_ERR, "Found Root Port %d, expecting %d\n", |
| 144 | rp, rpc.num_ports); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | /* Cache pci device. */ |
| 149 | rpc.ports[rp - 1] = dev; |
| 150 | } |
| 151 | |
| 152 | /* Update devicetree with new Root Port function number assignment */ |
| 153 | static void ich_pcie_device_set_func(int index, int pci_func) |
| 154 | { |
| 155 | struct device *dev; |
| 156 | unsigned int new_devfn; |
| 157 | |
| 158 | dev = rpc.ports[index]; |
| 159 | |
| 160 | /* Set the new PCI function field for this Root Port. */ |
| 161 | rpc.new_rpfn &= ~RPFN_FNMASK(index); |
| 162 | rpc.new_rpfn |= RPFN_FNSET(index, pci_func); |
| 163 | |
| 164 | /* Determine the new devfn for this port */ |
| 165 | new_devfn = PCI_DEVFN(ICH_PCIE_DEV_SLOT, pci_func); |
| 166 | |
| 167 | if (dev->path.pci.devfn != new_devfn) { |
| 168 | printk(BIOS_DEBUG, |
| 169 | "ICH: PCIe map %02x.%1x -> %02x.%1x\n", |
| 170 | PCI_SLOT(dev->path.pci.devfn), |
| 171 | PCI_FUNC(dev->path.pci.devfn), |
| 172 | PCI_SLOT(new_devfn), PCI_FUNC(new_devfn)); |
| 173 | |
| 174 | dev->path.pci.devfn = new_devfn; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static void root_port_commit_config(struct device *dev) |
| 179 | { |
| 180 | int i; |
| 181 | int coalesce = 0; |
| 182 | |
| 183 | if (dev->chip_info != NULL) { |
| 184 | struct southbridge_intel_i82801gx_config *config |
| 185 | = dev->chip_info; |
| 186 | coalesce = config->pcie_port_coalesce; |
| 187 | } |
| 188 | |
| 189 | if (!rpc.ports[0]->enabled) |
| 190 | coalesce = 1; |
| 191 | |
| 192 | for (i = 0; i < rpc.num_ports; i++) { |
| 193 | struct device *pcie_dev; |
| 194 | |
| 195 | pcie_dev = rpc.ports[i]; |
| 196 | |
| 197 | if (dev == NULL) { |
| 198 | printk(BIOS_ERR, "Root Port %d device is NULL?\n", |
| 199 | i + 1); |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | if (pcie_dev->enabled) |
| 204 | continue; |
| 205 | |
| 206 | printk(BIOS_DEBUG, "%s: Disabling device\n", |
| 207 | dev_path(pcie_dev)); |
| 208 | |
| 209 | /* Disable this device if possible */ |
| 210 | i82801gx_enable(pcie_dev); |
| 211 | } |
| 212 | |
| 213 | if (coalesce) { |
| 214 | int current_func; |
| 215 | |
| 216 | /* For all Root Ports N enabled ports get assigned the lower |
| 217 | * PCI function number. The disabled ones get upper PCI |
| 218 | * function numbers. */ |
| 219 | current_func = 0; |
| 220 | for (i = 0; i < rpc.num_ports; i++) { |
| 221 | if (!rpc.ports[i]->enabled) |
| 222 | continue; |
| 223 | ich_pcie_device_set_func(i, current_func); |
| 224 | current_func++; |
| 225 | } |
| 226 | |
| 227 | /* Allocate the disabled devices' PCI function number. */ |
| 228 | for (i = 0; i < rpc.num_ports; i++) { |
| 229 | if (rpc.ports[i]->enabled) |
| 230 | continue; |
| 231 | ich_pcie_device_set_func(i, current_func); |
| 232 | current_func++; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | printk(BIOS_SPEW, "ICH: RPFN 0x%08x -> 0x%08x\n", |
| 237 | rpc.orig_rpfn, rpc.new_rpfn); |
| 238 | RCBA32(RPFN) = rpc.new_rpfn; |
| 239 | } |
| 240 | |
| 241 | static void ich_pcie_enable(struct device *dev) |
| 242 | { |
| 243 | /* Add this device to the root port config structure. */ |
| 244 | root_port_init_config(dev); |
| 245 | |
| 246 | /* |
| 247 | * When processing the last PCIe root port we can now |
| 248 | * update the Root Port Function Number and Hide register. |
| 249 | */ |
| 250 | if (root_port_is_last(dev)) |
| 251 | root_port_commit_config(dev); |
| 252 | } |
| 253 | |
| 254 | |
Elyes HAOUAS | 9966703 | 2018-05-13 12:47:28 +0200 | [diff] [blame] | 255 | static void pcie_set_subsystem(struct device *dev, unsigned int vendor, |
| 256 | unsigned int device) |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 257 | { |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 258 | /* NOTE: This is not the default position! */ |
| 259 | if (!vendor || !device) { |
| 260 | pci_write_config32(dev, 0x94, |
| 261 | pci_read_config32(dev, 0)); |
| 262 | } else { |
| 263 | pci_write_config32(dev, 0x94, |
| 264 | ((device & 0xffff) << 16) | (vendor & 0xffff)); |
| 265 | } |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static struct pci_operations pci_ops = { |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 269 | .set_subsystem = pcie_set_subsystem, |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | static struct device_operations device_ops = { |
| 273 | .read_resources = pci_bus_read_resources, |
| 274 | .set_resources = pci_dev_set_resources, |
| 275 | .enable_resources = pci_bus_enable_resources, |
| 276 | .init = pci_init, |
Arthur Heymans | e6e5ecb | 2018-12-20 01:44:50 +0100 | [diff] [blame] | 277 | .enable = ich_pcie_enable, |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 278 | .scan_bus = pci_scan_bridge, |
| 279 | .ops_pci = &pci_ops, |
| 280 | }; |
| 281 | |
Patrick Georgi | efff733 | 2012-07-26 19:48:23 +0200 | [diff] [blame] | 282 | static const unsigned short i82801gx_pcie_ids[] = { |
| 283 | 0x27d0, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */ |
| 284 | 0x27d2, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */ |
| 285 | 0x27d4, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */ |
| 286 | 0x27d6, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */ |
| 287 | 0x27e0, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */ |
| 288 | 0x27e2, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */ |
| 289 | 0 |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 290 | }; |
| 291 | |
Patrick Georgi | efff733 | 2012-07-26 19:48:23 +0200 | [diff] [blame] | 292 | static const struct pci_driver i82801gx_pcie __pci_driver = { |
Arthur Heymans | 3f111b0 | 2017-03-09 12:02:52 +0100 | [diff] [blame] | 293 | .ops = &device_ops, |
| 294 | .vendor = PCI_VENDOR_ID_INTEL, |
| 295 | .devices = i82801gx_pcie_ids, |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 296 | }; |