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