blob: 1a127f41904e93953f600cf1965b8e6cde8edcc4 [file] [log] [blame]
Lee Leahy77ff0b12015-05-05 15:07:29 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc.
Lee Leahy32471722015-04-20 15:20:28 -07005 * Copyright (C) 2015 Intel Corp.
Lee Leahy77ff0b12015-05-05 15:07:29 -07006 *
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 Leahy77ff0b12015-05-05 15:07:29 -070015 */
16
Lee Leahy32471722015-04-20 15:20:28 -070017#include "chip.h"
Lee Leahy77ff0b12015-05-05 15:07:29 -070018#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 Leahy77ff0b12015-05-05 15:07:29 -070024#include <soc/pci_devs.h>
25#include <soc/pcie.h>
26#include <soc/ramstage.h>
27#include <soc/smm.h>
28
Lee Leahy77ff0b12015-05-05 15:07:29 -070029static int pll_en_off;
30static uint32_t strpfusecfg;
31
Elyes HAOUASb13fac32018-05-24 22:29:44 +020032static inline int root_port_offset(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070033{
34 return PCI_FUNC(dev->path.pci.devfn);
35}
36
Elyes HAOUASb13fac32018-05-24 22:29:44 +020037static inline int is_first_port(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070038{
39 return root_port_offset(dev) == PCIE_PORT1_FUNC;
40}
41
Elyes HAOUASb13fac32018-05-24 22:29:44 +020042static void pcie_init(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070043{
Lee Leahy32471722015-04-20 15:20:28 -070044 printk(BIOS_SPEW, "%s/%s ( %s )\n",
45 __FILE__, __func__, dev_name(dev));
Lee Leahy77ff0b12015-05-05 15:07:29 -070046}
47
48static 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 HAOUASb13fac32018-05-24 22:29:44 +020055static void check_port_enabled(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070056{
57 int rp_config = (strpfusecfg & LANECFG_MASK) >> LANECFG_SHIFT;
58
Lee Leahy32471722015-04-20 15:20:28 -070059 printk(BIOS_SPEW, "%s/%s ( %s )\n",
60 __FILE__, __func__, dev_name(dev));
61
Lee Leahy77ff0b12015-05-05 15:07:29 -070062 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 HAOUASb13fac32018-05-24 22:29:44 +020084static void check_device_present(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -070085{
Lee Leahy32471722015-04-20 15:20:28 -070086 /* port1_dev will store the dev struct pointer of the PORT1 */
Elyes HAOUASb13fac32018-05-24 22:29:44 +020087 static struct device *port1_dev;
Lee Leahy32471722015-04-20 15:20:28 -070088
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 Leahy77ff0b12015-05-05 15:07:29 -0700104 /* 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 Leahy32471722015-04-20 15:20:28 -0700109 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 Leahy77ff0b12015-05-05 15:07:29 -0700118 reg_script_run_on_dev(dev, no_dev_behind_port);
119 dev->enabled = 0;
Lee Leahy32471722015-04-20 15:20:28 -0700120 } 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 Leahy77ff0b12015-05-05 15:07:29 -0700130 }
Lee Leahy32471722015-04-20 15:20:28 -0700131 } else if (!dev->enabled) {
Lee Leahy77ff0b12015-05-05 15:07:29 -0700132 /* 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 HAOUASb13fac32018-05-24 22:29:44 +0200138static void pcie_enable(struct device *dev)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700139{
Lee Leahy32471722015-04-20 15:20:28 -0700140 printk(BIOS_SPEW, "%s/%s ( %s )\n",
141 __FILE__, __func__, dev_name(dev));
Lee Leahy77ff0b12015-05-05 15:07:29 -0700142 if (is_first_port(dev)) {
Lee Leahy32471722015-04-20 15:20:28 -0700143 struct soc_intel_braswell_config *config = dev->chip_info;
Lee Leahy77ff0b12015-05-05 15:07:29 -0700144 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 HAOUASb13fac32018-05-24 22:29:44 +0200162static void pcie_root_set_subsystem(struct device *dev, unsigned int vid,
Lee Leahy1072e7d2017-03-16 17:35:32 -0700163 unsigned int did)
Lee Leahy77ff0b12015-05-05 15:07:29 -0700164{
Lee Leahy32471722015-04-20 15:20:28 -0700165 printk(BIOS_SPEW, "%s/%s ( %s, 0x%04x, 0x%04x )\n",
166 __FILE__, __func__, dev_name(dev), vid, did);
Lee Leahy77ff0b12015-05-05 15:07:29 -0700167 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
174static struct pci_operations pcie_root_ops = {
175 .set_subsystem = &pcie_root_set_subsystem,
176};
177
178static 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 Leahy32471722015-04-20 15:20:28 -0700182 .init = pcie_init,
183 .scan_bus = pciexp_scan_bridge,
184 .enable = pcie_enable,
Lee Leahy77ff0b12015-05-05 15:07:29 -0700185 .ops_pci = &pcie_root_ops,
186};
187
188static const unsigned short pci_device_ids[] = {
189 PCIE_PORT1_DEVID, PCIE_PORT2_DEVID, PCIE_PORT3_DEVID, PCIE_PORT4_DEVID,
190 0
191};
192
193static 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};