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 | |
Elyes HAOUAS | b13fac3 | 2018-05-24 22:29:44 +0200 | [diff] [blame] | 32 | static inline int root_port_offset(struct device *dev) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 33 | { |
| 34 | return PCI_FUNC(dev->path.pci.devfn); |
| 35 | } |
| 36 | |
Elyes HAOUAS | b13fac3 | 2018-05-24 22:29:44 +0200 | [diff] [blame] | 37 | static inline int is_first_port(struct device *dev) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 38 | { |
| 39 | return root_port_offset(dev) == PCIE_PORT1_FUNC; |
| 40 | } |
| 41 | |
Elyes HAOUAS | b13fac3 | 2018-05-24 22:29:44 +0200 | [diff] [blame] | 42 | static void pcie_init(struct device *dev) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 43 | { |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 44 | printk(BIOS_SPEW, "%s/%s (%s)\n", |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 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 | |
Elyes HAOUAS | b13fac3 | 2018-05-24 22:29:44 +0200 | [diff] [blame] | 55 | static void check_port_enabled(struct device *dev) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 56 | { |
| 57 | int rp_config = (strpfusecfg & LANECFG_MASK) >> LANECFG_SHIFT; |
| 58 | |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 59 | printk(BIOS_SPEW, "%s/%s (%s)\n", |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 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 | |
Elyes HAOUAS | b13fac3 | 2018-05-24 22:29:44 +0200 | [diff] [blame] | 84 | static void check_device_present(struct device *dev) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 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 */ |
Elyes HAOUAS | b13fac3 | 2018-05-24 22:29:44 +0200 | [diff] [blame] | 87 | static struct device *port1_dev; |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 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 | |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 102 | printk(BIOS_SPEW, "%s/%s (%s)\n", |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 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 | |
Elyes HAOUAS | b13fac3 | 2018-05-24 22:29:44 +0200 | [diff] [blame] | 138 | static void pcie_enable(struct device *dev) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 139 | { |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 140 | printk(BIOS_SPEW, "%s/%s (%s)\n", |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 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 | |
Elyes HAOUAS | b13fac3 | 2018-05-24 22:29:44 +0200 | [diff] [blame] | 162 | static void pcie_root_set_subsystem(struct device *dev, unsigned int vid, |
Lee Leahy | 1072e7d | 2017-03-16 17:35:32 -0700 | [diff] [blame] | 163 | unsigned int did) |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 164 | { |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 165 | printk(BIOS_SPEW, "%s/%s (%s, 0x%04x, 0x%04x)\n", |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 166 | __FILE__, __func__, dev_name(dev), vid, did); |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 167 | uint32_t didvid = ((did & 0xffff) << 16) | (vid & 0xffff); |
| 168 | |
| 169 | if (!didvid) |
| 170 | didvid = pci_read_config32(dev, PCI_VENDOR_ID); |
| 171 | pci_write_config32(dev, 0x94, didvid); |
| 172 | } |
| 173 | |
| 174 | static struct pci_operations pcie_root_ops = { |
| 175 | .set_subsystem = &pcie_root_set_subsystem, |
| 176 | }; |
| 177 | |
| 178 | static struct device_operations device_ops = { |
| 179 | .read_resources = pci_bus_read_resources, |
| 180 | .set_resources = pci_dev_set_resources, |
| 181 | .enable_resources = pci_bus_enable_resources, |
Lee Leahy | 3247172 | 2015-04-20 15:20:28 -0700 | [diff] [blame] | 182 | .init = pcie_init, |
| 183 | .scan_bus = pciexp_scan_bridge, |
| 184 | .enable = pcie_enable, |
Lee Leahy | 77ff0b1 | 2015-05-05 15:07:29 -0700 | [diff] [blame] | 185 | .ops_pci = &pcie_root_ops, |
| 186 | }; |
| 187 | |
| 188 | static const unsigned short pci_device_ids[] = { |
| 189 | PCIE_PORT1_DEVID, PCIE_PORT2_DEVID, PCIE_PORT3_DEVID, PCIE_PORT4_DEVID, |
| 190 | 0 |
| 191 | }; |
| 192 | |
| 193 | static const struct pci_driver pcie_root_ports __pci_driver = { |
| 194 | .ops = &device_ops, |
| 195 | .vendor = PCI_VENDOR_ID_INTEL, |
| 196 | .devices = pci_device_ids, |
| 197 | }; |