Angel Pons | c3f58f6 | 2020-04-05 15:46:41 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 2 | |
| 3 | #include <console/console.h> |
| 4 | #include <device/device.h> |
| 5 | #include <device/pci.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 6 | #include <device/pci_ops.h> |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 7 | #include <device/pciexp.h> |
| 8 | #include <device/pci_ids.h> |
| 9 | #include <reg_script.h> |
| 10 | |
Julius Werner | 18ea2d3 | 2014-10-07 16:42:17 -0700 | [diff] [blame] | 11 | #include <soc/pci_devs.h> |
| 12 | #include <soc/pcie.h> |
| 13 | #include <soc/ramstage.h> |
| 14 | #include <soc/smm.h> |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 15 | |
| 16 | #include "chip.h" |
| 17 | |
| 18 | static int pll_en_off; |
| 19 | static uint32_t strpfusecfg; |
| 20 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 21 | static inline int root_port_offset(struct device *dev) |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 22 | { |
| 23 | return PCI_FUNC(dev->path.pci.devfn); |
| 24 | } |
| 25 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 26 | static inline int is_first_port(struct device *dev) |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 27 | { |
| 28 | return root_port_offset(dev) == PCIE_PORT1_FUNC; |
| 29 | } |
| 30 | |
| 31 | static const struct reg_script init_static_before_exit_latency[] = { |
| 32 | /* Disable optimized buffer flush fill and latency tolerant reporting */ |
| 33 | REG_PCI_RMW32(DCAP2, ~(OBFFS | LTRMS), 0), |
| 34 | REG_PCI_RMW32(DSTS2, ~(OBFFEN| LTRME), 0), |
| 35 | /* Set maximum payload size. */ |
| 36 | REG_PCI_RMW32(DCAP, ~MPS_MASK, 0), |
| 37 | /* Disable transmit datapath flush timer, clear transmit config change |
| 38 | * wait time, clear sideband interface idle counter. */ |
| 39 | REG_PCI_RMW32(PHYCTL2_IOSFBCTL, ~(TDFT | TXCFGCHWAIT | SIID), 0), |
| 40 | REG_SCRIPT_END, |
| 41 | }; |
| 42 | |
| 43 | static const struct reg_script init_static_after_exit_latency[] = { |
| 44 | /* Set common clock configuration. */ |
| 45 | REG_PCI_OR16(LCTL, CCC), |
| 46 | /* Set NFTS to 0x743a361b */ |
| 47 | REG_PCI_WRITE32(NFTS, 0x743a361b), |
| 48 | /* Set common clock latency to 0x3 */ |
| 49 | REG_PCI_RMW32(MPC, ~CCEL_MASK, (0x3 << CCEL_SHIFT)), |
| 50 | /* Set relay timer policy. */ |
| 51 | REG_PCI_RMW32(RTP, 0xff000000, 0x854c74), |
| 52 | /* Set IOSF packet fast transmit mode and link speed training policy. */ |
| 53 | REG_PCI_OR16(MPC2, IPF | LSTP), |
| 54 | /* Channel configuration - enable upstream posted split, set non-posted |
| 55 | * and posted request size */ |
| 56 | REG_PCI_RMW32(CHCFG, ~UPSD, UNRS | UPRS), |
| 57 | /* Completion status replay enable and set TLP grant count */ |
| 58 | REG_PCI_RMW32(CFG2, ~(LATGC_MASK), CSREN | (3 << LATGC_SHIFT)), |
| 59 | /* Assume no IOAPIC behind root port -- disable EOI forwarding. */ |
| 60 | REG_PCI_OR16(MPC2, EOIFD), |
| 61 | /* Expose AER */ |
| 62 | REG_PCI_RMW32(AERCH, ~0, (1 << 16) | (1 << 0)), |
| 63 | /* set completion timeout to 160ms to 170ms */ |
| 64 | REG_PCI_RMW16(DSTS2, ~CTD, 0x6), |
| 65 | /* Enable AER */ |
| 66 | REG_PCI_OR16(DCTL_DSTS, URE | FEE | NFE | CEE), |
Martin Roth | 99a3bba | 2014-12-07 14:57:26 -0700 | [diff] [blame] | 67 | /* Read and write back capability registers. */ |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 68 | REG_PCI_OR32(0x34, 0), |
| 69 | REG_PCI_OR32(0x80, 0), |
| 70 | /* Retrain the link. */ |
| 71 | REG_PCI_OR16(LCTL, RL), |
| 72 | REG_SCRIPT_END, |
| 73 | }; |
| 74 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 75 | static void byt_pcie_init(struct device *dev) |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 76 | { |
| 77 | struct reg_script init_script[] = { |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 78 | REG_SCRIPT_NEXT(init_static_before_exit_latency), |
| 79 | /* Exit latency configuration based on |
| 80 | * PHYCTL2_IOSFBCTL[PLL_OFF_EN] set in root port 1*/ |
| 81 | REG_PCI_RMW32(LCAP, ~L1EXIT_MASK, |
Kevin L Lee | 5c8d43e | 2014-12-12 14:02:43 +0800 | [diff] [blame] | 82 | 2 << (L1EXIT_SHIFT + pll_en_off)), |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 83 | REG_SCRIPT_NEXT(init_static_after_exit_latency), |
| 84 | /* Disable hot plug, set power to 10W, set slot number. */ |
| 85 | REG_PCI_RMW32(SLCAP, ~(HPC | HPS), |
| 86 | (1 << SLS_SHIFT) | (100 << SLV_SHIFT) | |
| 87 | (root_port_offset(dev) << SLN_SHIFT)), |
| 88 | /* Dynamic clock gating. */ |
| 89 | REG_PCI_OR32(RPPGEN, RPDLCGEN | RPDBCGEN | RPSCGEN), |
| 90 | REG_PCI_OR32(PWRCTL, RPL1SQPOL | RPDTSQPOL), |
| 91 | REG_PCI_OR32(PCIEDBG, SPCE), |
| 92 | REG_SCRIPT_END, |
| 93 | }; |
| 94 | |
Aaron Durbin | 616f394 | 2013-12-10 17:12:44 -0800 | [diff] [blame] | 95 | reg_script_run_on_dev(dev, init_script); |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 96 | |
| 97 | if (is_first_port(dev)) { |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 98 | struct soc_intel_baytrail_config *config = config_of(dev); |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 99 | uint32_t reg = pci_read_config32(dev, RPPGEN); |
| 100 | reg |= SRDLCGEN | SRDBCGEN; |
| 101 | |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 102 | if (config->clkreq_enable) |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 103 | reg |= LCLKREQEN | BBCLKREQEN; |
| 104 | |
| 105 | pci_write_config32(dev, RPPGEN, reg); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static const struct reg_script no_dev_behind_port[] = { |
| 110 | REG_PCI_OR32(PCIEALC, (1 << 26)), |
| 111 | REG_PCI_POLL32(PCIESTS1, 0x1f000000, (1 << 24), 50000), |
| 112 | REG_PCI_OR32(PHYCTL4, SQDIS), |
| 113 | REG_SCRIPT_END, |
| 114 | }; |
| 115 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 116 | static void check_port_enabled(struct device *dev) |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 117 | { |
| 118 | int rp_config = (strpfusecfg & LANECFG_MASK) >> LANECFG_SHIFT; |
| 119 | |
| 120 | switch (root_port_offset(dev)) { |
| 121 | case PCIE_PORT1_FUNC: |
| 122 | /* Port 1 cannot be disabled from strapping config. */ |
| 123 | break; |
| 124 | case PCIE_PORT2_FUNC: |
| 125 | /* Port 2 disabled in all configs but 4x1. */ |
| 126 | if (rp_config != 0x0) |
| 127 | dev->enabled = 0; |
| 128 | break; |
| 129 | case PCIE_PORT3_FUNC: |
| 130 | /* Port 3 disabled only in 1x4 config. */ |
| 131 | if (rp_config == 0x3) |
| 132 | dev->enabled = 0; |
| 133 | break; |
| 134 | case PCIE_PORT4_FUNC: |
| 135 | /* Port 4 disabled in 1x4 and 2x2 config. */ |
| 136 | if (rp_config >= 0x2) |
| 137 | dev->enabled = 0; |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 142 | static u8 all_ports_no_dev_present(struct device *dev) |
Kenji Chen | e237f5a | 2014-09-12 02:10:53 +0800 | [diff] [blame] | 143 | { |
| 144 | u8 func; |
| 145 | u8 temp = dev->path.pci.devfn; |
| 146 | u8 device_not_present = 1; |
| 147 | u8 data; |
| 148 | |
| 149 | for (func = 1; func < PCIE_ROOT_PORT_COUNT; func++) { |
| 150 | dev->path.pci.devfn &= ~0x7; |
| 151 | dev->path.pci.devfn |= func; |
| 152 | |
Elyes HAOUAS | 79ccc69 | 2020-02-24 13:43:39 +0100 | [diff] [blame] | 153 | /* is PCIe device there */ |
Kenji Chen | e237f5a | 2014-09-12 02:10:53 +0800 | [diff] [blame] | 154 | if (pci_read_config32(dev, 0) == 0xFFFFFFFF) |
| 155 | continue; |
| 156 | |
| 157 | data = pci_read_config8(dev, XCAP + 3) | (SI >> 24); |
| 158 | pci_write_config8(dev, XCAP + 3, data); |
| 159 | |
| 160 | /* is any device present */ |
| 161 | if ((pci_read_config32(dev, SLCTL_SLSTS) & PDS)) { |
| 162 | device_not_present = 0; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | dev->path.pci.devfn = temp; |
| 168 | return device_not_present; |
| 169 | } |
| 170 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 171 | static void check_device_present(struct device *dev) |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 172 | { |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 173 | /* Set slot implemented. */ |
| 174 | pci_write_config32(dev, XCAP, pci_read_config32(dev, XCAP) | SI); |
| 175 | |
| 176 | /* No device present. */ |
| 177 | if (!(pci_read_config32(dev, SLCTL_SLSTS) & PDS)) { |
| 178 | printk(BIOS_DEBUG, "No PCIe device present.\n"); |
Kenji Chen | e237f5a | 2014-09-12 02:10:53 +0800 | [diff] [blame] | 179 | if (is_first_port(dev)) { |
| 180 | if (all_ports_no_dev_present(dev)) { |
| 181 | reg_script_run_on_dev(dev, no_dev_behind_port); |
| 182 | dev->enabled = 0; |
| 183 | } |
| 184 | } else { |
Kenji Chen | 97acc5e | 2014-10-31 00:32:09 -0700 | [diff] [blame] | 185 | reg_script_run_on_dev(dev, no_dev_behind_port); |
Kenji Chen | e237f5a | 2014-09-12 02:10:53 +0800 | [diff] [blame] | 186 | dev->enabled = 0; |
| 187 | } |
Elyes HAOUAS | 4a83f1c | 2016-08-25 21:07:59 +0200 | [diff] [blame] | 188 | } else if (!dev->enabled) { |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 189 | /* Port is disabled, but device present. Disable link. */ |
| 190 | pci_write_config32(dev, LCTL, |
| 191 | pci_read_config32(dev, LCTL) | LD); |
| 192 | } |
| 193 | } |
| 194 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 195 | static void byt_pcie_enable(struct device *dev) |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 196 | { |
| 197 | if (is_first_port(dev)) { |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 198 | struct soc_intel_baytrail_config *config = config_of(dev); |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 199 | uint32_t reg = pci_read_config32(dev, PHYCTL2_IOSFBCTL); |
| 200 | pll_en_off = !!(reg & PLL_OFF_EN); |
| 201 | |
| 202 | strpfusecfg = pci_read_config32(dev, STRPFUSECFG); |
Kein Yuan | 3511023 | 2014-02-22 12:26:55 -0800 | [diff] [blame] | 203 | |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 204 | if (config->pcie_wake_enable) |
Kyösti Mälkki | faf20d3 | 2019-08-14 05:41:41 +0300 | [diff] [blame] | 205 | smm_southcluster_save_param( |
Kein Yuan | 3511023 | 2014-02-22 12:26:55 -0800 | [diff] [blame] | 206 | SMM_SAVE_PARAM_PCIE_WAKE_ENABLE, 1); |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* Check if device is enabled in strapping. */ |
| 210 | check_port_enabled(dev); |
| 211 | /* Determine if device is behind port. */ |
| 212 | check_device_present(dev); |
| 213 | |
| 214 | southcluster_enable_dev(dev); |
| 215 | } |
| 216 | |
Elyes HAOUAS | 17a3ceb | 2018-05-22 10:42:28 +0200 | [diff] [blame] | 217 | static void byt_pciexp_scan_bridge(struct device *dev) |
Kevin Hsieh | d946f5e | 2014-11-26 03:08:18 +0800 | [diff] [blame] | 218 | { |
| 219 | static const struct reg_script wait_for_link_active[] = { |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 220 | REG_PCI_POLL32(LCTL, (1 << 29), (1 << 29), 50000), |
Kevin Hsieh | d946f5e | 2014-11-26 03:08:18 +0800 | [diff] [blame] | 221 | REG_SCRIPT_END, |
| 222 | }; |
| 223 | |
| 224 | /* wait for Link Active with 50ms timeout */ |
| 225 | reg_script_run_on_dev(dev, wait_for_link_active); |
| 226 | |
Kyösti Mälkki | 580e722 | 2015-03-19 21:04:23 +0200 | [diff] [blame] | 227 | do_pci_scan_bridge(dev, pciexp_scan_bus); |
Kevin Hsieh | d946f5e | 2014-11-26 03:08:18 +0800 | [diff] [blame] | 228 | } |
| 229 | |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 230 | static struct pci_operations pcie_root_ops = { |
Kyösti Mälkki | 2520032 | 2019-03-20 18:36:37 +0200 | [diff] [blame] | 231 | .set_subsystem = pci_dev_set_subsystem, |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | static struct device_operations device_ops = { |
| 235 | .read_resources = pci_bus_read_resources, |
| 236 | .set_resources = pci_dev_set_resources, |
| 237 | .enable_resources = pci_bus_enable_resources, |
| 238 | .init = byt_pcie_init, |
Kevin Hsieh | d946f5e | 2014-11-26 03:08:18 +0800 | [diff] [blame] | 239 | .scan_bus = byt_pciexp_scan_bridge, |
Aaron Durbin | ae31f7d | 2013-11-22 14:16:49 -0600 | [diff] [blame] | 240 | .enable = byt_pcie_enable, |
| 241 | .ops_pci = &pcie_root_ops, |
| 242 | }; |
| 243 | |
| 244 | static const unsigned short pci_device_ids[] = { |
| 245 | PCIE_PORT1_DEVID, PCIE_PORT2_DEVID, PCIE_PORT3_DEVID, PCIE_PORT4_DEVID, |
| 246 | 0 |
| 247 | }; |
| 248 | |
| 249 | static const struct pci_driver pcie_root_ports __pci_driver = { |
| 250 | .ops = &device_ops, |
| 251 | .vendor = PCI_VENDOR_ID_INTEL, |
| 252 | .devices = pci_device_ids, |
| 253 | }; |