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