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