blob: 7ab184fd101f81c9f845c66918327f15de33e2cf [file] [log] [blame]
Lee Leahy77ff0b12015-05-05 15:07:29 -07001/*
2 * This file is part of the coreboot project.
3 *
Lee Leahy77ff0b12015-05-05 15:07:29 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Lee Leahy77ff0b12015-05-05 15:07:29 -070013 */
14
Lee Leahy32471722015-04-20 15:20:28 -070015#include "chip.h"
Lee Leahy77ff0b12015-05-05 15:07:29 -070016#include <console/console.h>
17#include <device/device.h>
18#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020019#include <device/pci_ops.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070020#include <device/pciexp.h>
21#include <device/pci_ids.h>
22#include <reg_script.h>
Lee Leahy77ff0b12015-05-05 15:07:29 -070023#include <soc/pci_devs.h>
24#include <soc/pcie.h>
25#include <soc/ramstage.h>
26#include <soc/smm.h>
27
Lee Leahy77ff0b12015-05-05 15:07:29 -070028static int pll_en_off;
29static uint32_t strpfusecfg;
30
Elyes HAOUASb13fac32018-05-24 22:29:44 +020031static inline int root_port_offset(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070032{
33 return PCI_FUNC(dev->path.pci.devfn);
34}
35
Elyes HAOUASb13fac32018-05-24 22:29:44 +020036static inline int is_first_port(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070037{
38 return root_port_offset(dev) == PCIE_PORT1_FUNC;
39}
40
Elyes HAOUASb13fac32018-05-24 22:29:44 +020041static void pcie_init(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070042{
Elyes HAOUASa342f392018-10-17 10:56:26 +020043 printk(BIOS_SPEW, "%s/%s (%s)\n",
Lee Leahy32471722015-04-20 15:20:28 -070044 __FILE__, __func__, dev_name(dev));
Lee Leahy77ff0b12015-05-05 15:07:29 -070045}
46
47static const struct reg_script no_dev_behind_port[] = {
48 REG_PCI_OR32(PCIEALC, (1 << 26)),
49 REG_PCI_POLL32(PCIESTS1, 0x1f000000, (1 << 24), 50000),
50 REG_PCI_OR32(PHYCTL4, SQDIS),
51 REG_SCRIPT_END,
52};
53
Elyes HAOUASb13fac32018-05-24 22:29:44 +020054static void check_port_enabled(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070055{
56 int rp_config = (strpfusecfg & LANECFG_MASK) >> LANECFG_SHIFT;
57
Elyes HAOUASa342f392018-10-17 10:56:26 +020058 printk(BIOS_SPEW, "%s/%s (%s)\n",
Lee Leahy32471722015-04-20 15:20:28 -070059 __FILE__, __func__, dev_name(dev));
60
Lee Leahy77ff0b12015-05-05 15:07:29 -070061 switch (root_port_offset(dev)) {
62 case PCIE_PORT1_FUNC:
63 /* Port 1 cannot be disabled from strapping config. */
64 break;
65 case PCIE_PORT2_FUNC:
66 /* Port 2 disabled in all configs but 4x1. */
67 if (rp_config != 0x0)
68 dev->enabled = 0;
69 break;
70 case PCIE_PORT3_FUNC:
71 /* Port 3 disabled only in 1x4 config. */
72 if (rp_config == 0x3)
73 dev->enabled = 0;
74 break;
75 case PCIE_PORT4_FUNC:
76 /* Port 4 disabled in 1x4 and 2x2 config. */
77 if (rp_config >= 0x2)
78 dev->enabled = 0;
79 break;
80 }
81}
82
Elyes HAOUASb13fac32018-05-24 22:29:44 +020083static void check_device_present(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070084{
Lee Leahy32471722015-04-20 15:20:28 -070085 /* port1_dev will store the dev struct pointer of the PORT1 */
Elyes HAOUASb13fac32018-05-24 22:29:44 +020086 static struct device *port1_dev;
Lee Leahy32471722015-04-20 15:20:28 -070087
88 /*
89 * The SOC has 4 ROOT ports defined with MAX_ROOT_PORTS_BSW.
90 * For each port initial assumption is that, each port will have
91 * devices connected to it. Later we will scan each PORT and if
92 * the device is not attached to that port we will update
93 * rootports_in_use. If none of the root port is in use we will
94 * disable PORT1 otherwise we will keep PORT1 enabled per spec.
95 * In future if the Soc has more number of PCIe Root ports then
96 * change MAX_ROOT_PORTS_BSW value accordingly.
97 */
98
99 static uint32_t rootports_in_use = MAX_ROOT_PORTS_BSW;
100
Elyes HAOUASa342f392018-10-17 10:56:26 +0200101 printk(BIOS_SPEW, "%s/%s (%s)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700102 __FILE__, __func__, dev_name(dev));
Lee Leahy77ff0b12015-05-05 15:07:29 -0700103 /* Set slot implemented. */
104 pci_write_config32(dev, XCAP, pci_read_config32(dev, XCAP) | SI);
105
106 /* No device present. */
107 if (!(pci_read_config32(dev, SLCTL_SLSTS) & PDS)) {
Lee Leahy32471722015-04-20 15:20:28 -0700108 rootports_in_use--;
109 printk(BIOS_DEBUG, "No PCIe device present.");
110
111 /*
112 * Defer PORT1 disabling for now. When we are at Last port
113 * we will check rootports_in_use and disable PORT1 if none
114 * of the port has any device connected
115 */
116 if (!is_first_port(dev)) {
Lee Leahy77ff0b12015-05-05 15:07:29 -0700117 reg_script_run_on_dev(dev, no_dev_behind_port);
118 dev->enabled = 0;
Lee Leahy32471722015-04-20 15:20:28 -0700119 } else
120 port1_dev = dev;
121 /*
122 * If none of the ROOT PORT has devices connected then
123 * disable PORT1 else keep the PORT1 enable
124 */
125 if (!rootports_in_use) {
126 reg_script_run_on_dev(port1_dev, no_dev_behind_port);
127 port1_dev->enabled = 0;
128 southcluster_enable_dev(port1_dev);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700129 }
Lee Leahy32471722015-04-20 15:20:28 -0700130 } else if (!dev->enabled) {
Lee Leahy77ff0b12015-05-05 15:07:29 -0700131 /* Port is disabled, but device present. Disable link. */
132 pci_write_config32(dev, LCTL,
133 pci_read_config32(dev, LCTL) | LD);
134 }
135}
136
Elyes HAOUASb13fac32018-05-24 22:29:44 +0200137static void pcie_enable(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700138{
Elyes HAOUASa342f392018-10-17 10:56:26 +0200139 printk(BIOS_SPEW, "%s/%s (%s)\n",
Lee Leahy32471722015-04-20 15:20:28 -0700140 __FILE__, __func__, dev_name(dev));
Lee Leahy77ff0b12015-05-05 15:07:29 -0700141 if (is_first_port(dev)) {
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300142 struct soc_intel_braswell_config *config = config_of(dev);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700143 uint32_t reg = pci_read_config32(dev, PHYCTL2_IOSFBCTL);
144 pll_en_off = !!(reg & PLL_OFF_EN);
145
146 strpfusecfg = pci_read_config32(dev, STRPFUSECFG);
147
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300148 if (config->pcie_wake_enable)
Kyösti Mälkkifaf20d32019-08-14 05:41:41 +0300149 smm_southcluster_save_param(
Lee Leahy77ff0b12015-05-05 15:07:29 -0700150 SMM_SAVE_PARAM_PCIE_WAKE_ENABLE, 1);
151 }
152
153 /* Check if device is enabled in strapping. */
154 check_port_enabled(dev);
155 /* Determine if device is behind port. */
156 check_device_present(dev);
157
158 southcluster_enable_dev(dev);
159}
160
Lee Leahy77ff0b12015-05-05 15:07:29 -0700161static struct pci_operations pcie_root_ops = {
Kyösti Mälkki25200322019-03-20 18:36:37 +0200162 .set_subsystem = pci_dev_set_subsystem,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700163};
164
165static struct device_operations device_ops = {
166 .read_resources = pci_bus_read_resources,
167 .set_resources = pci_dev_set_resources,
168 .enable_resources = pci_bus_enable_resources,
Lee Leahy32471722015-04-20 15:20:28 -0700169 .init = pcie_init,
170 .scan_bus = pciexp_scan_bridge,
171 .enable = pcie_enable,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700172 .ops_pci = &pcie_root_ops,
173};
174
175static const unsigned short pci_device_ids[] = {
176 PCIE_PORT1_DEVID, PCIE_PORT2_DEVID, PCIE_PORT3_DEVID, PCIE_PORT4_DEVID,
177 0
178};
179
180static const struct pci_driver pcie_root_ports __pci_driver = {
181 .ops = &device_ops,
182 .vendor = PCI_VENDOR_ID_INTEL,
183 .devices = pci_device_ids,
184};