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