Angel Pons | f94ac9a | 2020-04-05 15:46:48 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 2 | |
| 3 | #include <console/console.h> |
Angel Pons | 9d733de | 2020-11-23 13:15:19 +0100 | [diff] [blame] | 4 | #include <cpu/intel/haswell/haswell.h> |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 5 | #include <device/device.h> |
| 6 | #include <device/pci.h> |
| 7 | #include <device/pciexp.h> |
| 8 | #include <device/pci_def.h> |
| 9 | #include <device/pci_ids.h> |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 10 | #include <device/pci_ops.h> |
Julius Werner | 4ee4bd5 | 2014-10-20 13:46:39 -0700 | [diff] [blame] | 11 | #include <soc/lpc.h> |
| 12 | #include <soc/iobp.h> |
| 13 | #include <soc/pch.h> |
| 14 | #include <soc/pci_devs.h> |
| 15 | #include <soc/rcba.h> |
Angel Pons | 3cc2c38 | 2020-10-23 20:38:23 +0200 | [diff] [blame] | 16 | #include <soc/intel/broadwell/pch/chip.h> |
Angel Pons | 733f03d | 2021-01-28 16:59:04 +0100 | [diff] [blame] | 17 | #include <southbridge/intel/lynxpoint/lp_gpio.h> |
Wenkai Du | 8306761 | 2014-12-05 14:00:26 -0800 | [diff] [blame] | 18 | #include <delay.h> |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 19 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 20 | /* Low Power variant has 6 root ports. */ |
Angel Pons | 2ead363 | 2020-09-24 16:50:05 +0200 | [diff] [blame] | 21 | #define MAX_NUM_ROOT_PORTS 6 |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 22 | |
| 23 | struct root_port_config { |
| 24 | /* RPFN is a write-once register so keep a copy until it is written */ |
| 25 | u32 orig_rpfn; |
| 26 | u32 new_rpfn; |
| 27 | u32 pin_ownership; |
| 28 | u32 strpfusecfg1; |
| 29 | u32 strpfusecfg2; |
| 30 | u32 strpfusecfg3; |
| 31 | u32 b0d28f0_32c; |
| 32 | u32 b0d28f4_32c; |
| 33 | u32 b0d28f5_32c; |
| 34 | int coalesce; |
| 35 | int gbe_port; |
| 36 | int num_ports; |
Angel Pons | 2ead363 | 2020-09-24 16:50:05 +0200 | [diff] [blame] | 37 | struct device *ports[MAX_NUM_ROOT_PORTS]; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static struct root_port_config rpc; |
| 41 | |
Elyes HAOUAS | 040aff2 | 2018-05-27 16:30:36 +0200 | [diff] [blame] | 42 | static inline int root_port_is_first(struct device *dev) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 43 | { |
| 44 | return PCI_FUNC(dev->path.pci.devfn) == 0; |
| 45 | } |
| 46 | |
Elyes HAOUAS | 040aff2 | 2018-05-27 16:30:36 +0200 | [diff] [blame] | 47 | static inline int root_port_is_last(struct device *dev) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 48 | { |
| 49 | return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1); |
| 50 | } |
| 51 | |
| 52 | /* Root ports are numbered 1..N in the documentation. */ |
Elyes HAOUAS | 040aff2 | 2018-05-27 16:30:36 +0200 | [diff] [blame] | 53 | static inline int root_port_number(struct device *dev) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 54 | { |
| 55 | return PCI_FUNC(dev->path.pci.devfn) + 1; |
| 56 | } |
| 57 | |
| 58 | static void root_port_config_update_gbe_port(void) |
| 59 | { |
| 60 | /* Is the Gbe Port enabled? */ |
| 61 | if (!((rpc.strpfusecfg1 >> 19) & 1)) |
| 62 | return; |
| 63 | |
| 64 | switch ((rpc.strpfusecfg1 >> 16) & 0x7) { |
| 65 | case 0: |
| 66 | rpc.gbe_port = 3; |
| 67 | break; |
| 68 | case 1: |
| 69 | rpc.gbe_port = 4; |
| 70 | break; |
| 71 | case 2: |
| 72 | case 3: |
| 73 | case 4: |
| 74 | case 5: |
| 75 | /* Lanes 0-4 of Root Port 5. */ |
| 76 | rpc.gbe_port = 5; |
| 77 | break; |
| 78 | default: |
| 79 | printk(BIOS_DEBUG, "Invalid GbE Port Selection.\n"); |
| 80 | } |
| 81 | } |
| 82 | |
Elyes HAOUAS | 040aff2 | 2018-05-27 16:30:36 +0200 | [diff] [blame] | 83 | static void pcie_iosf_port_grant_count(struct device *dev) |
Kenji Chen | 87d4a20 | 2014-09-24 01:18:26 +0800 | [diff] [blame] | 84 | { |
| 85 | u8 update_val; |
Patrick Georgi | e8f2ef5 | 2016-07-29 18:53:34 +0200 | [diff] [blame] | 86 | u32 rpcd = (pci_read_config32(dev, 0xfc) >> 14) & 0x3; |
Kenji Chen | 87d4a20 | 2014-09-24 01:18:26 +0800 | [diff] [blame] | 87 | |
| 88 | switch (rpcd) { |
| 89 | case 1: |
| 90 | case 3: |
| 91 | update_val = 0x02; |
| 92 | break; |
| 93 | case 2: |
| 94 | update_val = 0x22; |
| 95 | break; |
| 96 | default: |
| 97 | update_val = 0x00; |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | RCBA32(0x103C) = (RCBA32(0x103C) & (~0xff)) | update_val; |
| 102 | } |
| 103 | |
Elyes HAOUAS | 040aff2 | 2018-05-27 16:30:36 +0200 | [diff] [blame] | 104 | static void root_port_init_config(struct device *dev) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 105 | { |
| 106 | int rp; |
Martin Roth | 2b2ff7f | 2015-12-18 10:46:59 -0700 | [diff] [blame] | 107 | u32 data = 0; |
Kenji Chen | e383feb | 2014-09-26 03:14:57 +0800 | [diff] [blame] | 108 | u8 resp, id; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 109 | |
| 110 | if (root_port_is_first(dev)) { |
| 111 | rpc.orig_rpfn = RCBA32(RPFN); |
| 112 | rpc.new_rpfn = rpc.orig_rpfn; |
Angel Pons | 2ead363 | 2020-09-24 16:50:05 +0200 | [diff] [blame] | 113 | rpc.num_ports = MAX_NUM_ROOT_PORTS; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 114 | rpc.gbe_port = -1; |
Kenji Chen | 87d4a20 | 2014-09-24 01:18:26 +0800 | [diff] [blame] | 115 | /* RP0 f5[3:0] = 0101b*/ |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 116 | pci_update_config8(dev, 0xf5, ~0xa, 0x5); |
Kenji Chen | 87d4a20 | 2014-09-24 01:18:26 +0800 | [diff] [blame] | 117 | |
| 118 | pcie_iosf_port_grant_count(dev); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 119 | |
| 120 | rpc.pin_ownership = pci_read_config32(dev, 0x410); |
| 121 | root_port_config_update_gbe_port(); |
| 122 | |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 123 | pci_or_config8(dev, 0xe2, 3 << 4); |
Angel Pons | 3cc2c38 | 2020-10-23 20:38:23 +0200 | [diff] [blame] | 124 | const struct soc_intel_broadwell_pch_config *config = config_of(dev); |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 125 | rpc.coalesce = config->pcie_port_coalesce; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | rp = root_port_number(dev); |
| 129 | if (rp > rpc.num_ports) { |
| 130 | printk(BIOS_ERR, "Found Root Port %d, expecting %d\n", |
| 131 | rp, rpc.num_ports); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | /* Read the fuse configuration and pin ownership. */ |
| 136 | switch (rp) { |
| 137 | case 1: |
| 138 | rpc.strpfusecfg1 = pci_read_config32(dev, 0xfc); |
| 139 | rpc.b0d28f0_32c = pci_read_config32(dev, 0x32c); |
| 140 | break; |
| 141 | case 5: |
| 142 | rpc.strpfusecfg2 = pci_read_config32(dev, 0xfc); |
| 143 | rpc.b0d28f4_32c = pci_read_config32(dev, 0x32c); |
| 144 | break; |
| 145 | case 6: |
| 146 | rpc.b0d28f5_32c = pci_read_config32(dev, 0x32c); |
| 147 | rpc.strpfusecfg3 = pci_read_config32(dev, 0xfc); |
| 148 | break; |
| 149 | default: |
| 150 | break; |
| 151 | } |
| 152 | |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 153 | pci_write_config32(dev, 0x418, 0x02000430); |
Kenji Chen | e383feb | 2014-09-26 03:14:57 +0800 | [diff] [blame] | 154 | |
Kenji Chen | e383feb | 2014-09-26 03:14:57 +0800 | [diff] [blame] | 155 | if (root_port_is_first(dev)) { |
Kenji Chen | e8f3664 | 2014-10-04 02:59:06 +0800 | [diff] [blame] | 156 | /* |
| 157 | * set RP0 PCICFG E2h[5:4] = 11b and E1h[6] = 1 |
| 158 | * before configuring ASPM |
| 159 | */ |
Kenji Chen | e383feb | 2014-09-26 03:14:57 +0800 | [diff] [blame] | 160 | id = 0xe0 + (u8)(RCBA32(RPFN) & 0x07); |
| 161 | pch_iobp_exec(0xE00000E0, IOBP_PCICFG_READ, id, &data, &resp); |
Kenji Chen | e8f3664 | 2014-10-04 02:59:06 +0800 | [diff] [blame] | 162 | data |= ((0x30 << 16) | (0x40 << 8)); |
Kenji Chen | e383feb | 2014-09-26 03:14:57 +0800 | [diff] [blame] | 163 | pch_iobp_exec(0xE00000E0, IOBP_PCICFG_WRITE, id, &data, &resp); |
| 164 | } |
| 165 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 166 | /* Cache pci device. */ |
| 167 | rpc.ports[rp - 1] = dev; |
| 168 | } |
| 169 | |
| 170 | /* Update devicetree with new Root Port function number assignment */ |
| 171 | static void pch_pcie_device_set_func(int index, int pci_func) |
| 172 | { |
Elyes HAOUAS | 040aff2 | 2018-05-27 16:30:36 +0200 | [diff] [blame] | 173 | struct device *dev; |
Lee Leahy | 23602df | 2017-03-16 19:00:37 -0700 | [diff] [blame] | 174 | unsigned int new_devfn; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 175 | |
| 176 | dev = rpc.ports[index]; |
| 177 | |
| 178 | /* Set the new PCI function field for this Root Port. */ |
| 179 | rpc.new_rpfn &= ~RPFN_FNMASK(index); |
| 180 | rpc.new_rpfn |= RPFN_FNSET(index, pci_func); |
| 181 | |
| 182 | /* Determine the new devfn for this port */ |
| 183 | new_devfn = PCI_DEVFN(PCH_DEV_SLOT_PCIE, pci_func); |
| 184 | |
Angel Pons | d5689dd | 2020-09-25 00:32:44 +0200 | [diff] [blame] | 185 | if (dev && dev->path.pci.devfn != new_devfn) { |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 186 | printk(BIOS_DEBUG, |
| 187 | "PCH: PCIe map %02x.%1x -> %02x.%1x\n", |
| 188 | PCI_SLOT(dev->path.pci.devfn), |
| 189 | PCI_FUNC(dev->path.pci.devfn), |
| 190 | PCI_SLOT(new_devfn), PCI_FUNC(new_devfn)); |
| 191 | |
| 192 | dev->path.pci.devfn = new_devfn; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static void pcie_enable_clock_gating(void) |
| 197 | { |
| 198 | int i; |
| 199 | int enabled_ports = 0; |
Kane Chen | 4fef5a2 | 2014-08-27 15:21:32 -0700 | [diff] [blame] | 200 | int is_broadwell = !!(cpu_family_model() == BROADWELL_FAMILY_ULT); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 201 | |
| 202 | for (i = 0; i < rpc.num_ports; i++) { |
Elyes HAOUAS | 040aff2 | 2018-05-27 16:30:36 +0200 | [diff] [blame] | 203 | struct device *dev; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 204 | int rp; |
| 205 | |
| 206 | dev = rpc.ports[i]; |
Angel Pons | d5689dd | 2020-09-25 00:32:44 +0200 | [diff] [blame] | 207 | if (!dev) |
| 208 | continue; |
| 209 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 210 | rp = root_port_number(dev); |
| 211 | |
| 212 | if (!dev->enabled) { |
| 213 | /* Configure shared resource clock gating. */ |
| 214 | if (rp == 1 || rp == 5 || rp == 6) |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 215 | pci_or_config8(dev, 0xe1, 0x3c); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 216 | |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 217 | pci_or_config8(dev, 0xe2, 3 << 4); |
| 218 | pci_or_config32(dev, 0x420, 1 << 31); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 219 | |
| 220 | /* Per-Port CLKREQ# handling. */ |
| 221 | if (gpio_is_native(18 + rp - 1)) |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 222 | pci_or_config32(dev, 0x420, 3 << 29); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 223 | |
| 224 | /* Enable static clock gating. */ |
| 225 | if (rp == 1 && !rpc.ports[1]->enabled && |
| 226 | !rpc.ports[2]->enabled && !rpc.ports[3]->enabled) { |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 227 | pci_or_config8(dev, 0xe2, 1); |
| 228 | pci_or_config8(dev, 0xe1, 1 << 7); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 229 | } else if (rp == 5 || rp == 6) { |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 230 | pci_or_config8(dev, 0xe2, 1); |
| 231 | pci_or_config8(dev, 0xe1, 1 << 7); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 232 | } |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | enabled_ports++; |
| 237 | |
| 238 | /* Enable dynamic clock gating. */ |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 239 | pci_or_config8(dev, 0xe1, 0x03); |
| 240 | pci_or_config8(dev, 0xe2, 1 << 6); |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 241 | pci_update_config8(dev, 0xe8, ~(3 << 2), (2 << 2)); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 242 | |
| 243 | /* Update PECR1 register. */ |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 244 | pci_or_config8(dev, 0xe8, 3); |
| 245 | |
Kane Chen | 4fef5a2 | 2014-08-27 15:21:32 -0700 | [diff] [blame] | 246 | if (is_broadwell) { |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 247 | pci_or_config32(dev, 0x324, (1 << 5) | (1 << 14)); |
Kane Chen | 4fef5a2 | 2014-08-27 15:21:32 -0700 | [diff] [blame] | 248 | } else { |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 249 | pci_or_config32(dev, 0x324, 1 << 5); |
Kane Chen | 4fef5a2 | 2014-08-27 15:21:32 -0700 | [diff] [blame] | 250 | } |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 251 | /* Per-Port CLKREQ# handling. */ |
| 252 | if (gpio_is_native(18 + rp - 1)) |
Kenji Chen | e8f3664 | 2014-10-04 02:59:06 +0800 | [diff] [blame] | 253 | /* |
| 254 | * In addition to D28Fx PCICFG 420h[30:29] = 11b, |
| 255 | * set 420h[17] = 0b and 420[0] = 1b for L1 SubState. |
| 256 | */ |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 257 | pci_update_config32(dev, 0x420, ~(1 << 17), (3 << 29) | 1); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 258 | |
| 259 | /* Configure shared resource clock gating. */ |
| 260 | if (rp == 1 || rp == 5 || rp == 6) |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 261 | pci_or_config8(dev, 0xe1, 0x3c); |
Duncan Laurie | 446fb8e | 2014-08-08 09:59:43 -0700 | [diff] [blame] | 262 | |
| 263 | /* CLKREQ# VR Idle Enable */ |
| 264 | RCBA32_OR(0x2b1c, (1 << (16 + i))); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | if (!enabled_ports) |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 268 | pci_or_config8(rpc.ports[0], 0xe1, 1 << 6); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static void root_port_commit_config(void) |
| 272 | { |
| 273 | int i; |
| 274 | |
| 275 | /* If the first root port is disabled the coalesce ports. */ |
| 276 | if (!rpc.ports[0]->enabled) |
| 277 | rpc.coalesce = 1; |
| 278 | |
| 279 | /* Perform clock gating configuration. */ |
| 280 | pcie_enable_clock_gating(); |
| 281 | |
| 282 | for (i = 0; i < rpc.num_ports; i++) { |
Elyes HAOUAS | 040aff2 | 2018-05-27 16:30:36 +0200 | [diff] [blame] | 283 | struct device *dev; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 284 | u32 reg32; |
Wenkai Du | 8306761 | 2014-12-05 14:00:26 -0800 | [diff] [blame] | 285 | int n = 0; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 286 | |
| 287 | dev = rpc.ports[i]; |
| 288 | |
| 289 | if (dev == NULL) { |
| 290 | printk(BIOS_ERR, "Root Port %d device is NULL?\n", i+1); |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | if (dev->enabled) |
| 295 | continue; |
| 296 | |
| 297 | printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(dev)); |
| 298 | |
Wenkai Du | 8306761 | 2014-12-05 14:00:26 -0800 | [diff] [blame] | 299 | /* 8.2 Configuration of PCI Express Root Ports */ |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 300 | pci_or_config32(dev, 0x338, 1 << 26); |
Wenkai Du | 8306761 | 2014-12-05 14:00:26 -0800 | [diff] [blame] | 301 | |
| 302 | do { |
| 303 | reg32 = pci_read_config32(dev, 0x328); |
| 304 | n++; |
Duncan Laurie | cad2b7b | 2015-01-14 17:30:20 -0800 | [diff] [blame] | 305 | if (((reg32 & 0xff000000) == 0x01000000) || (n > 50)) |
Wenkai Du | 8306761 | 2014-12-05 14:00:26 -0800 | [diff] [blame] | 306 | break; |
| 307 | udelay(100); |
| 308 | } while (1); |
| 309 | |
Duncan Laurie | cad2b7b | 2015-01-14 17:30:20 -0800 | [diff] [blame] | 310 | if (n > 50) |
Wenkai Du | 8306761 | 2014-12-05 14:00:26 -0800 | [diff] [blame] | 311 | printk(BIOS_DEBUG, "%s: Timeout waiting for 328h\n", |
| 312 | dev_path(dev)); |
| 313 | |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 314 | pci_or_config32(dev, 0x408, 1 << 27); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 315 | |
| 316 | /* Disable this device if possible */ |
| 317 | pch_disable_devfn(dev); |
| 318 | } |
| 319 | |
| 320 | if (rpc.coalesce) { |
| 321 | int current_func; |
| 322 | |
| 323 | /* For all Root Ports N enabled ports get assigned the lower |
| 324 | * PCI function number. The disabled ones get upper PCI |
| 325 | * function numbers. */ |
| 326 | current_func = 0; |
| 327 | for (i = 0; i < rpc.num_ports; i++) { |
| 328 | if (!rpc.ports[i]->enabled) |
| 329 | continue; |
| 330 | pch_pcie_device_set_func(i, current_func); |
| 331 | current_func++; |
| 332 | } |
| 333 | |
| 334 | /* Allocate the disabled devices' PCI function number. */ |
| 335 | for (i = 0; i < rpc.num_ports; i++) { |
| 336 | if (rpc.ports[i]->enabled) |
| 337 | continue; |
| 338 | pch_pcie_device_set_func(i, current_func); |
| 339 | current_func++; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | printk(BIOS_SPEW, "PCH: RPFN 0x%08x -> 0x%08x\n", |
| 344 | rpc.orig_rpfn, rpc.new_rpfn); |
| 345 | RCBA32(RPFN) = rpc.new_rpfn; |
| 346 | } |
| 347 | |
Elyes HAOUAS | 040aff2 | 2018-05-27 16:30:36 +0200 | [diff] [blame] | 348 | static void root_port_mark_disable(struct device *dev) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 349 | { |
| 350 | /* Mark device as disabled. */ |
| 351 | dev->enabled = 0; |
| 352 | /* Mark device to be hidden. */ |
| 353 | rpc.new_rpfn |= RPFN_HIDE(PCI_FUNC(dev->path.pci.devfn)); |
| 354 | } |
| 355 | |
Elyes HAOUAS | 040aff2 | 2018-05-27 16:30:36 +0200 | [diff] [blame] | 356 | static void root_port_check_disable(struct device *dev) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 357 | { |
| 358 | int rp; |
| 359 | |
| 360 | /* Device already disabled. */ |
| 361 | if (!dev->enabled) { |
| 362 | root_port_mark_disable(dev); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | rp = root_port_number(dev); |
| 367 | |
| 368 | /* Is the GbE port mapped to this Root Port? */ |
| 369 | if (rp == rpc.gbe_port) { |
| 370 | root_port_mark_disable(dev); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | /* Check Root Port Configuration. */ |
| 375 | switch (rp) { |
Lee Leahy | 6ef5192 | 2017-03-17 10:56:08 -0700 | [diff] [blame] | 376 | case 2: |
| 377 | /* Root Port 2 is disabled for all lane configurations |
| 378 | * but config 00b (4x1 links). */ |
| 379 | if ((rpc.strpfusecfg1 >> 14) & 0x3) { |
| 380 | root_port_mark_disable(dev); |
| 381 | return; |
| 382 | } |
| 383 | break; |
| 384 | case 3: |
| 385 | /* Root Port 3 is disabled in config 11b (1x4 links). */ |
| 386 | if (((rpc.strpfusecfg1 >> 14) & 0x3) == 0x3) { |
| 387 | root_port_mark_disable(dev); |
| 388 | return; |
| 389 | } |
| 390 | break; |
| 391 | case 4: |
| 392 | /* Root Port 4 is disabled in configs 11b (1x4 links) |
| 393 | * and 10b (2x2 links). */ |
| 394 | if ((rpc.strpfusecfg1 >> 14) & 0x2) { |
| 395 | root_port_mark_disable(dev); |
| 396 | return; |
| 397 | } |
| 398 | break; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* Check Pin Ownership. */ |
| 402 | switch (rp) { |
| 403 | case 1: |
| 404 | /* Bit 0 is Root Port 1 ownership. */ |
| 405 | if ((rpc.pin_ownership & 0x1) == 0) { |
| 406 | root_port_mark_disable(dev); |
| 407 | return; |
| 408 | } |
| 409 | break; |
| 410 | case 2: |
| 411 | /* Bit 2 is Root Port 2 ownership. */ |
| 412 | if ((rpc.pin_ownership & 0x4) == 0) { |
| 413 | root_port_mark_disable(dev); |
| 414 | return; |
| 415 | } |
| 416 | break; |
| 417 | case 6: |
| 418 | /* Bits 7:4 are Root Port 6 pin-lane ownership. */ |
| 419 | if ((rpc.pin_ownership & 0xf0) == 0) { |
| 420 | root_port_mark_disable(dev); |
| 421 | return; |
| 422 | } |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 427 | static void pcie_add_0x0202000_iobp(u32 reg) |
| 428 | { |
| 429 | u32 reg32; |
| 430 | |
| 431 | reg32 = pch_iobp_read(reg); |
| 432 | reg32 += (0x2 << 16) | (0x2 << 8); |
| 433 | pch_iobp_write(reg, reg32); |
| 434 | } |
| 435 | |
| 436 | static void pch_pcie_early(struct device *dev) |
| 437 | { |
Angel Pons | 3cc2c38 | 2020-10-23 20:38:23 +0200 | [diff] [blame] | 438 | const struct soc_intel_broadwell_pch_config *config = config_of(dev); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 439 | int do_aspm = 0; |
| 440 | int rp = root_port_number(dev); |
| 441 | |
| 442 | switch (rp) { |
| 443 | case 1: |
| 444 | case 2: |
| 445 | case 3: |
| 446 | case 4: |
| 447 | /* |
Martin Roth | de7ed6f | 2014-12-07 14:58:18 -0700 | [diff] [blame] | 448 | * Bits 31:28 of b0d28f0 0x32c register correspond to |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 449 | * Root Ports 4:1. |
| 450 | */ |
| 451 | do_aspm = !!(rpc.b0d28f0_32c & (1 << (28 + rp - 1))); |
| 452 | break; |
| 453 | case 5: |
| 454 | /* |
Martin Roth | de7ed6f | 2014-12-07 14:58:18 -0700 | [diff] [blame] | 455 | * Bit 28 of b0d28f4 0x32c register correspond to |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 456 | * Root Ports 4:1. |
| 457 | */ |
| 458 | do_aspm = !!(rpc.b0d28f4_32c & (1 << 28)); |
| 459 | break; |
| 460 | case 6: |
| 461 | /* |
Martin Roth | de7ed6f | 2014-12-07 14:58:18 -0700 | [diff] [blame] | 462 | * Bit 28 of b0d28f5 0x32c register correspond to |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 463 | * Root Ports 4:1. |
| 464 | */ |
| 465 | do_aspm = !!(rpc.b0d28f5_32c & (1 << 28)); |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | /* Allow ASPM to be forced on in devicetree */ |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 470 | if ((config->pcie_port_force_aspm & (1 << (rp - 1)))) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 471 | do_aspm = 1; |
| 472 | |
| 473 | printk(BIOS_DEBUG, "PCIe Root Port %d ASPM is %sabled\n", |
| 474 | rp, do_aspm ? "en" : "dis"); |
| 475 | |
| 476 | if (do_aspm) { |
| 477 | /* Set ASPM bits in MPC2 register. */ |
Angel Pons | 2ead363 | 2020-09-24 16:50:05 +0200 | [diff] [blame] | 478 | pci_update_config32(dev, 0xd4, ~(0x3 << 2), (1 << 4) | (0x2 << 2)); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 479 | |
| 480 | /* Set unique clock exit latency in MPC register. */ |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 481 | pci_update_config32(dev, 0xd8, ~(0x7 << 18), (0x7 << 18)); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 482 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 483 | switch (rp) { |
| 484 | case 1: |
| 485 | pcie_add_0x0202000_iobp(0xe9002440); |
| 486 | break; |
| 487 | case 2: |
| 488 | pcie_add_0x0202000_iobp(0xe9002640); |
| 489 | break; |
| 490 | case 3: |
| 491 | pcie_add_0x0202000_iobp(0xe9000840); |
| 492 | break; |
| 493 | case 4: |
| 494 | pcie_add_0x0202000_iobp(0xe9000a40); |
| 495 | break; |
| 496 | case 5: |
| 497 | pcie_add_0x0202000_iobp(0xe9000c40); |
| 498 | pcie_add_0x0202000_iobp(0xe9000e40); |
| 499 | pcie_add_0x0202000_iobp(0xe9001040); |
| 500 | pcie_add_0x0202000_iobp(0xe9001240); |
| 501 | break; |
| 502 | case 6: |
| 503 | /* Update IOBP based on lane ownership. */ |
| 504 | if (rpc.pin_ownership & (1 << 4)) |
| 505 | pcie_add_0x0202000_iobp(0xea002040); |
| 506 | if (rpc.pin_ownership & (1 << 5)) |
| 507 | pcie_add_0x0202000_iobp(0xea002240); |
| 508 | if (rpc.pin_ownership & (1 << 6)) |
| 509 | pcie_add_0x0202000_iobp(0xea002440); |
| 510 | if (rpc.pin_ownership & (1 << 7)) |
| 511 | pcie_add_0x0202000_iobp(0xea002640); |
| 512 | break; |
| 513 | } |
| 514 | |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 515 | pci_update_config32(dev, 0x338, ~(1 << 26), 0); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Kenji Chen | c373f50 | 2014-09-26 02:48:16 +0800 | [diff] [blame] | 518 | /* Enable LTR in Root Port. Disable OBFF. */ |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 519 | pci_update_config32(dev, 0x64, ~(1 << 11) & ~(3 << 18), (1 << 11)); |
| 520 | pci_update_config32(dev, 0x68, ~(1 << 10), (1 << 10)); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 521 | |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 522 | pci_update_config32(dev, 0x318, ~(0xffff << 16), (0x1414 << 16)); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 523 | |
| 524 | /* Set L1 exit latency in LCAP register. */ |
| 525 | if (!do_aspm && (pci_read_config8(dev, 0xf5) & 0x1)) |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 526 | pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x4 << 15)); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 527 | else |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 528 | pci_update_config32(dev, 0x4c, ~(0x7 << 15), (0x2 << 15)); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 529 | |
Angel Pons | 2ead363 | 2020-09-24 16:50:05 +0200 | [diff] [blame] | 530 | pci_update_config32(dev, 0x314, 0, 0x743a361b); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 531 | |
| 532 | /* Set Common Clock Exit Latency in MPC register. */ |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 533 | pci_update_config32(dev, 0xd8, ~(0x7 << 15), (0x3 << 15)); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 534 | |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 535 | pci_update_config32(dev, 0x33c, ~0x00ffffff, 0x854d74); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 536 | |
Martin Roth | de7ed6f | 2014-12-07 14:58:18 -0700 | [diff] [blame] | 537 | /* Set Invalid Receive Range Check Enable in MPC register. */ |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 538 | pci_or_config32(dev, 0xd8, 1 << 25); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 539 | |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 540 | pci_and_config8(dev, 0xf5, 0x0f); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 541 | |
Kenji Chen | 94fea49 | 2014-09-30 14:17:35 +0800 | [diff] [blame] | 542 | /* Set AER Extended Cap ID to 01h and Next Cap Pointer to 200h. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 543 | if (CONFIG(PCIEXP_AER)) |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 544 | pci_update_config32(dev, 0x100, ~0xfffff, (1 << 29) | 0x10001); |
Youness Alaoui | 7161678 | 2018-05-04 15:34:06 -0400 | [diff] [blame] | 545 | else |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 546 | pci_update_config32(dev, 0x100, ~0xfffff, (1 << 29)); |
Kenji Chen | 8ef55ee | 2014-09-25 21:34:42 +0800 | [diff] [blame] | 547 | |
Kenji Chen | 94fea49 | 2014-09-30 14:17:35 +0800 | [diff] [blame] | 548 | /* Set L1 Sub-State Cap ID to 1Eh and Next Cap Pointer to None. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 549 | if (CONFIG(PCIEXP_L1_SUB_STATE)) |
Youness Alaoui | 1f64b01 | 2018-05-04 15:33:54 -0400 | [diff] [blame] | 550 | pci_update_config32(dev, 0x200, ~0xfffff, 0x001e); |
| 551 | else |
| 552 | pci_update_config32(dev, 0x200, ~0xfffff, 0); |
Kenji Chen | 94fea49 | 2014-09-30 14:17:35 +0800 | [diff] [blame] | 553 | |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 554 | pci_update_config32(dev, 0x320, ~(3 << 20) & ~(7 << 6), (1 << 20) | (3 << 6)); |
| 555 | |
Kenji Chen | c373f50 | 2014-09-26 02:48:16 +0800 | [diff] [blame] | 556 | /* Enable Relaxed Order from Root Port. */ |
Angel Pons | 6b486e1 | 2020-10-28 14:16:06 +0100 | [diff] [blame] | 557 | pci_or_config32(dev, 0x320, 3 << 23); |
Kenji Chen | c373f50 | 2014-09-26 02:48:16 +0800 | [diff] [blame] | 558 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 559 | if (rp == 1 || rp == 5 || rp == 6) |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 560 | pci_update_config8(dev, 0xf7, ~0xc, 0); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 561 | |
| 562 | /* Set EOI forwarding disable. */ |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 563 | pci_update_config32(dev, 0xd4, ~0, (1 << 1)); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 564 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 565 | /* Read and write back write-once capability registers. */ |
Kyösti Mälkki | 48c389e | 2013-07-26 08:53:59 +0300 | [diff] [blame] | 566 | pci_update_config32(dev, 0x34, ~0, 0); |
| 567 | pci_update_config32(dev, 0x40, ~0, 0); |
| 568 | pci_update_config32(dev, 0x80, ~0, 0); |
| 569 | pci_update_config32(dev, 0x90, ~0, 0); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static void pch_pcie_init(struct device *dev) |
| 573 | { |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 574 | printk(BIOS_DEBUG, "Initializing PCH PCIe bridge.\n"); |
| 575 | |
| 576 | /* Enable SERR */ |
Elyes HAOUAS | b887adf | 2020-04-29 10:42:34 +0200 | [diff] [blame] | 577 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_SERR); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 578 | |
| 579 | /* Enable Bus Master */ |
Elyes HAOUAS | b887adf | 2020-04-29 10:42:34 +0200 | [diff] [blame] | 580 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 581 | |
| 582 | /* Set Cache Line Size to 0x10 */ |
| 583 | pci_write_config8(dev, 0x0c, 0x10); |
| 584 | |
Angel Pons | 2ead363 | 2020-09-24 16:50:05 +0200 | [diff] [blame] | 585 | pci_and_config16(dev, PCI_BRIDGE_CONTROL, ~PCI_BRIDGE_CTL_PARITY); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 586 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 587 | /* Clear errors in status registers */ |
Angel Pons | 2ead363 | 2020-09-24 16:50:05 +0200 | [diff] [blame] | 588 | pci_update_config16(dev, 0x06, ~0, 0); |
| 589 | pci_update_config16(dev, 0x1e, ~0, 0); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Elyes HAOUAS | 040aff2 | 2018-05-27 16:30:36 +0200 | [diff] [blame] | 592 | static void pch_pcie_enable(struct device *dev) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 593 | { |
| 594 | /* Add this device to the root port config structure. */ |
| 595 | root_port_init_config(dev); |
| 596 | |
| 597 | /* Check to see if this Root Port should be disabled. */ |
| 598 | root_port_check_disable(dev); |
| 599 | |
| 600 | /* Power Management init before enumeration */ |
| 601 | if (dev->enabled) |
| 602 | pch_pcie_early(dev); |
| 603 | |
| 604 | /* |
| 605 | * When processing the last PCIe root port we can now |
| 606 | * update the Root Port Function Number and Hide register. |
| 607 | */ |
| 608 | if (root_port_is_last(dev)) |
| 609 | root_port_commit_config(); |
| 610 | } |
| 611 | |
Nico Huber | 968ef75 | 2021-03-07 01:39:18 +0100 | [diff] [blame] | 612 | static void pcie_get_ltr_max_latencies(u16 *max_snoop, u16 *max_nosnoop) |
Kenji Chen | b71d9b8 | 2014-10-10 03:08:15 +0800 | [diff] [blame] | 613 | { |
Nico Huber | 968ef75 | 2021-03-07 01:39:18 +0100 | [diff] [blame] | 614 | *max_snoop = PCIE_LTR_MAX_SNOOP_LATENCY_3146US; |
| 615 | *max_nosnoop = PCIE_LTR_MAX_NO_SNOOP_LATENCY_3146US; |
Kenji Chen | b71d9b8 | 2014-10-10 03:08:15 +0800 | [diff] [blame] | 616 | } |
| 617 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 618 | static struct pci_operations pcie_ops = { |
Subrata Banik | 15ccbf0 | 2019-03-20 15:09:44 +0530 | [diff] [blame] | 619 | .set_subsystem = pci_dev_set_subsystem, |
Nico Huber | 968ef75 | 2021-03-07 01:39:18 +0100 | [diff] [blame] | 620 | .get_ltr_max_latencies = pcie_get_ltr_max_latencies, |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 621 | }; |
| 622 | |
| 623 | static struct device_operations device_ops = { |
| 624 | .read_resources = pci_bus_read_resources, |
| 625 | .set_resources = pci_dev_set_resources, |
| 626 | .enable_resources = pci_bus_enable_resources, |
| 627 | .init = pch_pcie_init, |
| 628 | .enable = pch_pcie_enable, |
| 629 | .scan_bus = pciexp_scan_bridge, |
| 630 | .ops_pci = &pcie_ops, |
| 631 | }; |
| 632 | |
| 633 | static const unsigned short pcie_device_ids[] = { |
| 634 | /* Lynxpoint-LP */ |
| 635 | 0x9c10, 0x9c12, 0x9c14, 0x9c16, 0x9c18, 0x9c1a, |
| 636 | /* WildcatPoint */ |
| 637 | 0x9c90, 0x9c92, 0x9c94, 0x9c96, 0x9c98, 0x9c9a, 0x2448, |
| 638 | 0 |
| 639 | }; |
| 640 | |
| 641 | static const struct pci_driver pch_pcie __pci_driver = { |
| 642 | .ops = &device_ops, |
| 643 | .vendor = PCI_VENDOR_ID_INTEL, |
| 644 | .devices = pcie_device_ids, |
| 645 | }; |