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