Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google Inc. |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 5 | * Copyright (C) 2015 Intel Corp. |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 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. |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 17 | #include "chip.h" |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 18 | #include <console/console.h> |
| 19 | #include <device/device.h> |
| 20 | #include <device/pci.h> |
| 21 | #include <device/pciexp.h> |
| 22 | #include <device/pci_ids.h> |
| 23 | #include <reg_script.h> |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 24 | #include <soc/pci_devs.h> |
| 25 | #include <soc/pcie.h> |
| 26 | #include <soc/ramstage.h> |
| 27 | #include <soc/smm.h> |
| 28 | |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 29 | static int pll_en_off; |
| 30 | static uint32_t strpfusecfg; |
| 31 | |
| 32 | static inline int root_port_offset(device_t dev) |
| 33 | { |
| 34 | return PCI_FUNC(dev->path.pci.devfn); |
| 35 | } |
| 36 | |
| 37 | static inline int is_first_port(device_t dev) |
| 38 | { |
| 39 | return root_port_offset(dev) == PCIE_PORT1_FUNC; |
| 40 | } |
| 41 | |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 42 | static void pcie_init(device_t dev) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 43 | { |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 44 | printk(BIOS_SPEW, "%s/%s ( %s )\n", |
| 45 | __FILE__, __func__, dev_name(dev)); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static const struct reg_script no_dev_behind_port[] = { |
| 49 | REG_PCI_OR32(PCIEALC, (1 << 26)), |
| 50 | REG_PCI_POLL32(PCIESTS1, 0x1f000000, (1 << 24), 50000), |
| 51 | REG_PCI_OR32(PHYCTL4, SQDIS), |
| 52 | REG_SCRIPT_END, |
| 53 | }; |
| 54 | |
| 55 | static void check_port_enabled(device_t dev) |
| 56 | { |
| 57 | int rp_config = (strpfusecfg & LANECFG_MASK) >> LANECFG_SHIFT; |
| 58 | |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 59 | printk(BIOS_SPEW, "%s/%s ( %s )\n", |
| 60 | __FILE__, __func__, dev_name(dev)); |
| 61 | |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 62 | switch (root_port_offset(dev)) { |
| 63 | case PCIE_PORT1_FUNC: |
| 64 | /* Port 1 cannot be disabled from strapping config. */ |
| 65 | break; |
| 66 | case PCIE_PORT2_FUNC: |
| 67 | /* Port 2 disabled in all configs but 4x1. */ |
| 68 | if (rp_config != 0x0) |
| 69 | dev->enabled = 0; |
| 70 | break; |
| 71 | case PCIE_PORT3_FUNC: |
| 72 | /* Port 3 disabled only in 1x4 config. */ |
| 73 | if (rp_config == 0x3) |
| 74 | dev->enabled = 0; |
| 75 | break; |
| 76 | case PCIE_PORT4_FUNC: |
| 77 | /* Port 4 disabled in 1x4 and 2x2 config. */ |
| 78 | if (rp_config >= 0x2) |
| 79 | dev->enabled = 0; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 84 | static void check_device_present(device_t dev) |
| 85 | { |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 86 | /* port1_dev will store the dev struct pointer of the PORT1 */ |
| 87 | static device_t port1_dev; |
| 88 | |
| 89 | /* |
| 90 | * The SOC has 4 ROOT ports defined with MAX_ROOT_PORTS_BSW. |
| 91 | * For each port initial assumption is that, each port will have |
| 92 | * devices connected to it. Later we will scan each PORT and if |
| 93 | * the device is not attached to that port we will update |
| 94 | * rootports_in_use. If none of the root port is in use we will |
| 95 | * disable PORT1 otherwise we will keep PORT1 enabled per spec. |
| 96 | * In future if the Soc has more number of PCIe Root ports then |
| 97 | * change MAX_ROOT_PORTS_BSW value accordingly. |
| 98 | */ |
| 99 | |
| 100 | static uint32_t rootports_in_use = MAX_ROOT_PORTS_BSW; |
| 101 | |
| 102 | printk(BIOS_SPEW, "%s/%s ( %s )\n", |
| 103 | __FILE__, __func__, dev_name(dev)); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 104 | /* Set slot implemented. */ |
| 105 | pci_write_config32(dev, XCAP, pci_read_config32(dev, XCAP) | SI); |
| 106 | |
| 107 | /* No device present. */ |
| 108 | if (!(pci_read_config32(dev, SLCTL_SLSTS) & PDS)) { |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 109 | rootports_in_use--; |
| 110 | printk(BIOS_DEBUG, "No PCIe device present."); |
| 111 | |
| 112 | /* |
| 113 | * Defer PORT1 disabling for now. When we are at Last port |
| 114 | * we will check rootports_in_use and disable PORT1 if none |
| 115 | * of the port has any device connected |
| 116 | */ |
| 117 | if (!is_first_port(dev)) { |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 118 | reg_script_run_on_dev(dev, no_dev_behind_port); |
| 119 | dev->enabled = 0; |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 120 | } else |
| 121 | port1_dev = dev; |
| 122 | /* |
| 123 | * If none of the ROOT PORT has devices connected then |
| 124 | * disable PORT1 else keep the PORT1 enable |
| 125 | */ |
| 126 | if (!rootports_in_use) { |
| 127 | reg_script_run_on_dev(port1_dev, no_dev_behind_port); |
| 128 | port1_dev->enabled = 0; |
| 129 | southcluster_enable_dev(port1_dev); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 130 | } |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 131 | } else if (!dev->enabled) { |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 132 | /* Port is disabled, but device present. Disable link. */ |
| 133 | pci_write_config32(dev, LCTL, |
| 134 | pci_read_config32(dev, LCTL) | LD); |
| 135 | } |
| 136 | } |
| 137 | |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 138 | static void pcie_enable(device_t dev) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 139 | { |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 140 | printk(BIOS_SPEW, "%s/%s ( %s )\n", |
| 141 | __FILE__, __func__, dev_name(dev)); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 142 | if (is_first_port(dev)) { |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 143 | struct soc_intel_braswell_config *config = dev->chip_info; |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 144 | uint32_t reg = pci_read_config32(dev, PHYCTL2_IOSFBCTL); |
| 145 | pll_en_off = !!(reg & PLL_OFF_EN); |
| 146 | |
| 147 | strpfusecfg = pci_read_config32(dev, STRPFUSECFG); |
| 148 | |
| 149 | if (config && config->pcie_wake_enable) |
| 150 | southcluster_smm_save_param( |
| 151 | SMM_SAVE_PARAM_PCIE_WAKE_ENABLE, 1); |
| 152 | } |
| 153 | |
| 154 | /* Check if device is enabled in strapping. */ |
| 155 | check_port_enabled(dev); |
| 156 | /* Determine if device is behind port. */ |
| 157 | check_device_present(dev); |
| 158 | |
| 159 | southcluster_enable_dev(dev); |
| 160 | } |
| 161 | |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 162 | static void pcie_root_set_subsystem(device_t dev, unsigned vid, unsigned did) |
| 163 | { |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 164 | printk(BIOS_SPEW, "%s/%s ( %s, 0x%04x, 0x%04x )\n", |
| 165 | __FILE__, __func__, dev_name(dev), vid, did); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 166 | uint32_t didvid = ((did & 0xffff) << 16) | (vid & 0xffff); |
| 167 | |
| 168 | if (!didvid) |
| 169 | didvid = pci_read_config32(dev, PCI_VENDOR_ID); |
| 170 | pci_write_config32(dev, 0x94, didvid); |
| 171 | } |
| 172 | |
| 173 | static struct pci_operations pcie_root_ops = { |
| 174 | .set_subsystem = &pcie_root_set_subsystem, |
| 175 | }; |
| 176 | |
| 177 | static struct device_operations device_ops = { |
| 178 | .read_resources = pci_bus_read_resources, |
| 179 | .set_resources = pci_dev_set_resources, |
| 180 | .enable_resources = pci_bus_enable_resources, |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 181 | .init = pcie_init, |
| 182 | .scan_bus = pciexp_scan_bridge, |
| 183 | .enable = pcie_enable, |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 184 | .ops_pci = &pcie_root_ops, |
| 185 | }; |
| 186 | |
| 187 | static const unsigned short pci_device_ids[] = { |
| 188 | PCIE_PORT1_DEVID, PCIE_PORT2_DEVID, PCIE_PORT3_DEVID, PCIE_PORT4_DEVID, |
| 189 | 0 |
| 190 | }; |
| 191 | |
| 192 | static const struct pci_driver pcie_root_ports __pci_driver = { |
| 193 | .ops = &device_ops, |
| 194 | .vendor = PCI_VENDOR_ID_INTEL, |
| 195 | .devices = pci_device_ids, |
| 196 | }; |