blob: 8520049b08c7ec23a1b4a7e782a4066c8d51bd81 [file] [log] [blame]
Angel Ponsc3f58f62020-04-05 15:46:41 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Aaron Durbinae31f7d2013-11-22 14:16:49 -06003
4#include <console/console.h>
5#include <device/device.h>
6#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02007#include <device/pci_ops.h>
Aaron Durbinae31f7d2013-11-22 14:16:49 -06008#include <device/pciexp.h>
9#include <device/pci_ids.h>
10#include <reg_script.h>
11
Julius Werner18ea2d32014-10-07 16:42:17 -070012#include <soc/pci_devs.h>
13#include <soc/pcie.h>
14#include <soc/ramstage.h>
15#include <soc/smm.h>
Aaron Durbinae31f7d2013-11-22 14:16:49 -060016
17#include "chip.h"
18
19static int pll_en_off;
20static uint32_t strpfusecfg;
21
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020022static inline int root_port_offset(struct device *dev)
Aaron Durbinae31f7d2013-11-22 14:16:49 -060023{
24 return PCI_FUNC(dev->path.pci.devfn);
25}
26
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020027static inline int is_first_port(struct device *dev)
Aaron Durbinae31f7d2013-11-22 14:16:49 -060028{
29 return root_port_offset(dev) == PCIE_PORT1_FUNC;
30}
31
32static const struct reg_script init_static_before_exit_latency[] = {
33 /* Disable optimized buffer flush fill and latency tolerant reporting */
34 REG_PCI_RMW32(DCAP2, ~(OBFFS | LTRMS), 0),
35 REG_PCI_RMW32(DSTS2, ~(OBFFEN| LTRME), 0),
36 /* Set maximum payload size. */
37 REG_PCI_RMW32(DCAP, ~MPS_MASK, 0),
38 /* Disable transmit datapath flush timer, clear transmit config change
39 * wait time, clear sideband interface idle counter. */
40 REG_PCI_RMW32(PHYCTL2_IOSFBCTL, ~(TDFT | TXCFGCHWAIT | SIID), 0),
41 REG_SCRIPT_END,
42};
43
44static const struct reg_script init_static_after_exit_latency[] = {
45 /* Set common clock configuration. */
46 REG_PCI_OR16(LCTL, CCC),
47 /* Set NFTS to 0x743a361b */
48 REG_PCI_WRITE32(NFTS, 0x743a361b),
49 /* Set common clock latency to 0x3 */
50 REG_PCI_RMW32(MPC, ~CCEL_MASK, (0x3 << CCEL_SHIFT)),
51 /* Set relay timer policy. */
52 REG_PCI_RMW32(RTP, 0xff000000, 0x854c74),
53 /* Set IOSF packet fast transmit mode and link speed training policy. */
54 REG_PCI_OR16(MPC2, IPF | LSTP),
55 /* Channel configuration - enable upstream posted split, set non-posted
56 * and posted request size */
57 REG_PCI_RMW32(CHCFG, ~UPSD, UNRS | UPRS),
58 /* Completion status replay enable and set TLP grant count */
59 REG_PCI_RMW32(CFG2, ~(LATGC_MASK), CSREN | (3 << LATGC_SHIFT)),
60 /* Assume no IOAPIC behind root port -- disable EOI forwarding. */
61 REG_PCI_OR16(MPC2, EOIFD),
62 /* Expose AER */
63 REG_PCI_RMW32(AERCH, ~0, (1 << 16) | (1 << 0)),
64 /* set completion timeout to 160ms to 170ms */
65 REG_PCI_RMW16(DSTS2, ~CTD, 0x6),
66 /* Enable AER */
67 REG_PCI_OR16(DCTL_DSTS, URE | FEE | NFE | CEE),
Martin Roth99a3bba2014-12-07 14:57:26 -070068 /* Read and write back capability registers. */
Aaron Durbinae31f7d2013-11-22 14:16:49 -060069 REG_PCI_OR32(0x34, 0),
70 REG_PCI_OR32(0x80, 0),
71 /* Retrain the link. */
72 REG_PCI_OR16(LCTL, RL),
73 REG_SCRIPT_END,
74};
75
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020076static void byt_pcie_init(struct device *dev)
Aaron Durbinae31f7d2013-11-22 14:16:49 -060077{
78 struct reg_script init_script[] = {
Aaron Durbinae31f7d2013-11-22 14:16:49 -060079 REG_SCRIPT_NEXT(init_static_before_exit_latency),
80 /* Exit latency configuration based on
81 * PHYCTL2_IOSFBCTL[PLL_OFF_EN] set in root port 1*/
82 REG_PCI_RMW32(LCAP, ~L1EXIT_MASK,
Kevin L Lee5c8d43e2014-12-12 14:02:43 +080083 2 << (L1EXIT_SHIFT + pll_en_off)),
Aaron Durbinae31f7d2013-11-22 14:16:49 -060084 REG_SCRIPT_NEXT(init_static_after_exit_latency),
85 /* Disable hot plug, set power to 10W, set slot number. */
86 REG_PCI_RMW32(SLCAP, ~(HPC | HPS),
87 (1 << SLS_SHIFT) | (100 << SLV_SHIFT) |
88 (root_port_offset(dev) << SLN_SHIFT)),
89 /* Dynamic clock gating. */
90 REG_PCI_OR32(RPPGEN, RPDLCGEN | RPDBCGEN | RPSCGEN),
91 REG_PCI_OR32(PWRCTL, RPL1SQPOL | RPDTSQPOL),
92 REG_PCI_OR32(PCIEDBG, SPCE),
93 REG_SCRIPT_END,
94 };
95
Aaron Durbin616f3942013-12-10 17:12:44 -080096 reg_script_run_on_dev(dev, init_script);
Aaron Durbinae31f7d2013-11-22 14:16:49 -060097
98 if (is_first_port(dev)) {
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +030099 struct soc_intel_baytrail_config *config = config_of(dev);
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600100 uint32_t reg = pci_read_config32(dev, RPPGEN);
101 reg |= SRDLCGEN | SRDBCGEN;
102
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300103 if (config->clkreq_enable)
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600104 reg |= LCLKREQEN | BBCLKREQEN;
105
106 pci_write_config32(dev, RPPGEN, reg);
107 }
108}
109
110static const struct reg_script no_dev_behind_port[] = {
111 REG_PCI_OR32(PCIEALC, (1 << 26)),
112 REG_PCI_POLL32(PCIESTS1, 0x1f000000, (1 << 24), 50000),
113 REG_PCI_OR32(PHYCTL4, SQDIS),
114 REG_SCRIPT_END,
115};
116
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200117static void check_port_enabled(struct device *dev)
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600118{
119 int rp_config = (strpfusecfg & LANECFG_MASK) >> LANECFG_SHIFT;
120
121 switch (root_port_offset(dev)) {
122 case PCIE_PORT1_FUNC:
123 /* Port 1 cannot be disabled from strapping config. */
124 break;
125 case PCIE_PORT2_FUNC:
126 /* Port 2 disabled in all configs but 4x1. */
127 if (rp_config != 0x0)
128 dev->enabled = 0;
129 break;
130 case PCIE_PORT3_FUNC:
131 /* Port 3 disabled only in 1x4 config. */
132 if (rp_config == 0x3)
133 dev->enabled = 0;
134 break;
135 case PCIE_PORT4_FUNC:
136 /* Port 4 disabled in 1x4 and 2x2 config. */
137 if (rp_config >= 0x2)
138 dev->enabled = 0;
139 break;
140 }
141}
142
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200143static u8 all_ports_no_dev_present(struct device *dev)
Kenji Chene237f5a2014-09-12 02:10:53 +0800144{
145 u8 func;
146 u8 temp = dev->path.pci.devfn;
147 u8 device_not_present = 1;
148 u8 data;
149
150 for (func = 1; func < PCIE_ROOT_PORT_COUNT; func++) {
151 dev->path.pci.devfn &= ~0x7;
152 dev->path.pci.devfn |= func;
153
Elyes HAOUAS79ccc692020-02-24 13:43:39 +0100154 /* is PCIe device there */
Kenji Chene237f5a2014-09-12 02:10:53 +0800155 if (pci_read_config32(dev, 0) == 0xFFFFFFFF)
156 continue;
157
158 data = pci_read_config8(dev, XCAP + 3) | (SI >> 24);
159 pci_write_config8(dev, XCAP + 3, data);
160
161 /* is any device present */
162 if ((pci_read_config32(dev, SLCTL_SLSTS) & PDS)) {
163 device_not_present = 0;
164 break;
165 }
166 }
167
168 dev->path.pci.devfn = temp;
169 return device_not_present;
170}
171
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200172static void check_device_present(struct device *dev)
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600173{
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600174 /* Set slot implemented. */
175 pci_write_config32(dev, XCAP, pci_read_config32(dev, XCAP) | SI);
176
177 /* No device present. */
178 if (!(pci_read_config32(dev, SLCTL_SLSTS) & PDS)) {
179 printk(BIOS_DEBUG, "No PCIe device present.\n");
Kenji Chene237f5a2014-09-12 02:10:53 +0800180 if (is_first_port(dev)) {
181 if (all_ports_no_dev_present(dev)) {
182 reg_script_run_on_dev(dev, no_dev_behind_port);
183 dev->enabled = 0;
184 }
185 } else {
Kenji Chen97acc5e2014-10-31 00:32:09 -0700186 reg_script_run_on_dev(dev, no_dev_behind_port);
Kenji Chene237f5a2014-09-12 02:10:53 +0800187 dev->enabled = 0;
188 }
Elyes HAOUAS4a83f1c2016-08-25 21:07:59 +0200189 } else if (!dev->enabled) {
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600190 /* Port is disabled, but device present. Disable link. */
191 pci_write_config32(dev, LCTL,
192 pci_read_config32(dev, LCTL) | LD);
193 }
194}
195
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200196static void byt_pcie_enable(struct device *dev)
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600197{
198 if (is_first_port(dev)) {
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300199 struct soc_intel_baytrail_config *config = config_of(dev);
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600200 uint32_t reg = pci_read_config32(dev, PHYCTL2_IOSFBCTL);
201 pll_en_off = !!(reg & PLL_OFF_EN);
202
203 strpfusecfg = pci_read_config32(dev, STRPFUSECFG);
Kein Yuan35110232014-02-22 12:26:55 -0800204
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300205 if (config->pcie_wake_enable)
Kyösti Mälkkifaf20d32019-08-14 05:41:41 +0300206 smm_southcluster_save_param(
Kein Yuan35110232014-02-22 12:26:55 -0800207 SMM_SAVE_PARAM_PCIE_WAKE_ENABLE, 1);
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600208 }
209
210 /* Check if device is enabled in strapping. */
211 check_port_enabled(dev);
212 /* Determine if device is behind port. */
213 check_device_present(dev);
214
215 southcluster_enable_dev(dev);
216}
217
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200218static void byt_pciexp_scan_bridge(struct device *dev)
Kevin Hsiehd946f5e2014-11-26 03:08:18 +0800219{
220 static const struct reg_script wait_for_link_active[] = {
Elyes HAOUASa342f392018-10-17 10:56:26 +0200221 REG_PCI_POLL32(LCTL, (1 << 29), (1 << 29), 50000),
Kevin Hsiehd946f5e2014-11-26 03:08:18 +0800222 REG_SCRIPT_END,
223 };
224
225 /* wait for Link Active with 50ms timeout */
226 reg_script_run_on_dev(dev, wait_for_link_active);
227
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200228 do_pci_scan_bridge(dev, pciexp_scan_bus);
Kevin Hsiehd946f5e2014-11-26 03:08:18 +0800229}
230
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600231static struct pci_operations pcie_root_ops = {
Kyösti Mälkki25200322019-03-20 18:36:37 +0200232 .set_subsystem = pci_dev_set_subsystem,
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600233};
234
235static struct device_operations device_ops = {
236 .read_resources = pci_bus_read_resources,
237 .set_resources = pci_dev_set_resources,
238 .enable_resources = pci_bus_enable_resources,
239 .init = byt_pcie_init,
Kevin Hsiehd946f5e2014-11-26 03:08:18 +0800240 .scan_bus = byt_pciexp_scan_bridge,
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600241 .enable = byt_pcie_enable,
242 .ops_pci = &pcie_root_ops,
243};
244
245static const unsigned short pci_device_ids[] = {
246 PCIE_PORT1_DEVID, PCIE_PORT2_DEVID, PCIE_PORT3_DEVID, PCIE_PORT4_DEVID,
247 0
248};
249
250static const struct pci_driver pcie_root_ports __pci_driver = {
251 .ops = &device_ops,
252 .vendor = PCI_VENDOR_ID_INTEL,
253 .devices = pci_device_ids,
254};