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