blob: b2b2d3c3b42233252e80dca2c9df05f66399aaf5 [file] [log] [blame]
Aaron Durbinae31f7d2013-11-22 14:16:49 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbinae31f7d2013-11-22 14:16:49 -060014 */
15
16#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>
Aaron Durbinae31f7d2013-11-22 14:16:49 -060020#include <device/pciexp.h>
21#include <device/pci_ids.h>
22#include <reg_script.h>
23
Julius Werner18ea2d32014-10-07 16:42:17 -070024#include <soc/pci_devs.h>
25#include <soc/pcie.h>
26#include <soc/ramstage.h>
27#include <soc/smm.h>
Aaron Durbinae31f7d2013-11-22 14:16:49 -060028
29#include "chip.h"
30
31static int pll_en_off;
32static uint32_t strpfusecfg;
33
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020034static inline int root_port_offset(struct device *dev)
Aaron Durbinae31f7d2013-11-22 14:16:49 -060035{
36 return PCI_FUNC(dev->path.pci.devfn);
37}
38
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020039static inline int is_first_port(struct device *dev)
Aaron Durbinae31f7d2013-11-22 14:16:49 -060040{
41 return root_port_offset(dev) == PCIE_PORT1_FUNC;
42}
43
44static const struct reg_script init_static_before_exit_latency[] = {
45 /* Disable optimized buffer flush fill and latency tolerant reporting */
46 REG_PCI_RMW32(DCAP2, ~(OBFFS | LTRMS), 0),
47 REG_PCI_RMW32(DSTS2, ~(OBFFEN| LTRME), 0),
48 /* Set maximum payload size. */
49 REG_PCI_RMW32(DCAP, ~MPS_MASK, 0),
50 /* Disable transmit datapath flush timer, clear transmit config change
51 * wait time, clear sideband interface idle counter. */
52 REG_PCI_RMW32(PHYCTL2_IOSFBCTL, ~(TDFT | TXCFGCHWAIT | SIID), 0),
53 REG_SCRIPT_END,
54};
55
56static const struct reg_script init_static_after_exit_latency[] = {
57 /* Set common clock configuration. */
58 REG_PCI_OR16(LCTL, CCC),
59 /* Set NFTS to 0x743a361b */
60 REG_PCI_WRITE32(NFTS, 0x743a361b),
61 /* Set common clock latency to 0x3 */
62 REG_PCI_RMW32(MPC, ~CCEL_MASK, (0x3 << CCEL_SHIFT)),
63 /* Set relay timer policy. */
64 REG_PCI_RMW32(RTP, 0xff000000, 0x854c74),
65 /* Set IOSF packet fast transmit mode and link speed training policy. */
66 REG_PCI_OR16(MPC2, IPF | LSTP),
67 /* Channel configuration - enable upstream posted split, set non-posted
68 * and posted request size */
69 REG_PCI_RMW32(CHCFG, ~UPSD, UNRS | UPRS),
70 /* Completion status replay enable and set TLP grant count */
71 REG_PCI_RMW32(CFG2, ~(LATGC_MASK), CSREN | (3 << LATGC_SHIFT)),
72 /* Assume no IOAPIC behind root port -- disable EOI forwarding. */
73 REG_PCI_OR16(MPC2, EOIFD),
74 /* Expose AER */
75 REG_PCI_RMW32(AERCH, ~0, (1 << 16) | (1 << 0)),
76 /* set completion timeout to 160ms to 170ms */
77 REG_PCI_RMW16(DSTS2, ~CTD, 0x6),
78 /* Enable AER */
79 REG_PCI_OR16(DCTL_DSTS, URE | FEE | NFE | CEE),
Martin Roth99a3bba2014-12-07 14:57:26 -070080 /* Read and write back capability registers. */
Aaron Durbinae31f7d2013-11-22 14:16:49 -060081 REG_PCI_OR32(0x34, 0),
82 REG_PCI_OR32(0x80, 0),
83 /* Retrain the link. */
84 REG_PCI_OR16(LCTL, RL),
85 REG_SCRIPT_END,
86};
87
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020088static void byt_pcie_init(struct device *dev)
Aaron Durbinae31f7d2013-11-22 14:16:49 -060089{
90 struct reg_script init_script[] = {
Aaron Durbinae31f7d2013-11-22 14:16:49 -060091 REG_SCRIPT_NEXT(init_static_before_exit_latency),
92 /* Exit latency configuration based on
93 * PHYCTL2_IOSFBCTL[PLL_OFF_EN] set in root port 1*/
94 REG_PCI_RMW32(LCAP, ~L1EXIT_MASK,
Kevin L Lee5c8d43e2014-12-12 14:02:43 +080095 2 << (L1EXIT_SHIFT + pll_en_off)),
Aaron Durbinae31f7d2013-11-22 14:16:49 -060096 REG_SCRIPT_NEXT(init_static_after_exit_latency),
97 /* Disable hot plug, set power to 10W, set slot number. */
98 REG_PCI_RMW32(SLCAP, ~(HPC | HPS),
99 (1 << SLS_SHIFT) | (100 << SLV_SHIFT) |
100 (root_port_offset(dev) << SLN_SHIFT)),
101 /* Dynamic clock gating. */
102 REG_PCI_OR32(RPPGEN, RPDLCGEN | RPDBCGEN | RPSCGEN),
103 REG_PCI_OR32(PWRCTL, RPL1SQPOL | RPDTSQPOL),
104 REG_PCI_OR32(PCIEDBG, SPCE),
105 REG_SCRIPT_END,
106 };
107
Aaron Durbin616f3942013-12-10 17:12:44 -0800108 reg_script_run_on_dev(dev, init_script);
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600109
110 if (is_first_port(dev)) {
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300111 struct soc_intel_baytrail_config *config = config_of(dev);
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600112 uint32_t reg = pci_read_config32(dev, RPPGEN);
113 reg |= SRDLCGEN | SRDBCGEN;
114
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300115 if (config->clkreq_enable)
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600116 reg |= LCLKREQEN | BBCLKREQEN;
117
118 pci_write_config32(dev, RPPGEN, reg);
119 }
120}
121
122static const struct reg_script no_dev_behind_port[] = {
123 REG_PCI_OR32(PCIEALC, (1 << 26)),
124 REG_PCI_POLL32(PCIESTS1, 0x1f000000, (1 << 24), 50000),
125 REG_PCI_OR32(PHYCTL4, SQDIS),
126 REG_SCRIPT_END,
127};
128
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200129static void check_port_enabled(struct device *dev)
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600130{
131 int rp_config = (strpfusecfg & LANECFG_MASK) >> LANECFG_SHIFT;
132
133 switch (root_port_offset(dev)) {
134 case PCIE_PORT1_FUNC:
135 /* Port 1 cannot be disabled from strapping config. */
136 break;
137 case PCIE_PORT2_FUNC:
138 /* Port 2 disabled in all configs but 4x1. */
139 if (rp_config != 0x0)
140 dev->enabled = 0;
141 break;
142 case PCIE_PORT3_FUNC:
143 /* Port 3 disabled only in 1x4 config. */
144 if (rp_config == 0x3)
145 dev->enabled = 0;
146 break;
147 case PCIE_PORT4_FUNC:
148 /* Port 4 disabled in 1x4 and 2x2 config. */
149 if (rp_config >= 0x2)
150 dev->enabled = 0;
151 break;
152 }
153}
154
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200155static u8 all_ports_no_dev_present(struct device *dev)
Kenji Chene237f5a2014-09-12 02:10:53 +0800156{
157 u8 func;
158 u8 temp = dev->path.pci.devfn;
159 u8 device_not_present = 1;
160 u8 data;
161
162 for (func = 1; func < PCIE_ROOT_PORT_COUNT; func++) {
163 dev->path.pci.devfn &= ~0x7;
164 dev->path.pci.devfn |= func;
165
166 /* is pcie device there */
167 if (pci_read_config32(dev, 0) == 0xFFFFFFFF)
168 continue;
169
170 data = pci_read_config8(dev, XCAP + 3) | (SI >> 24);
171 pci_write_config8(dev, XCAP + 3, data);
172
173 /* is any device present */
174 if ((pci_read_config32(dev, SLCTL_SLSTS) & PDS)) {
175 device_not_present = 0;
176 break;
177 }
178 }
179
180 dev->path.pci.devfn = temp;
181 return device_not_present;
182}
183
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200184static void check_device_present(struct device *dev)
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600185{
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600186 /* Set slot implemented. */
187 pci_write_config32(dev, XCAP, pci_read_config32(dev, XCAP) | SI);
188
189 /* No device present. */
190 if (!(pci_read_config32(dev, SLCTL_SLSTS) & PDS)) {
191 printk(BIOS_DEBUG, "No PCIe device present.\n");
Kenji Chene237f5a2014-09-12 02:10:53 +0800192 if (is_first_port(dev)) {
193 if (all_ports_no_dev_present(dev)) {
194 reg_script_run_on_dev(dev, no_dev_behind_port);
195 dev->enabled = 0;
196 }
197 } else {
Kenji Chen97acc5e2014-10-31 00:32:09 -0700198 reg_script_run_on_dev(dev, no_dev_behind_port);
Kenji Chene237f5a2014-09-12 02:10:53 +0800199 dev->enabled = 0;
200 }
Elyes HAOUAS4a83f1c2016-08-25 21:07:59 +0200201 } else if (!dev->enabled) {
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600202 /* Port is disabled, but device present. Disable link. */
203 pci_write_config32(dev, LCTL,
204 pci_read_config32(dev, LCTL) | LD);
205 }
206}
207
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200208static void byt_pcie_enable(struct device *dev)
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600209{
210 if (is_first_port(dev)) {
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300211 struct soc_intel_baytrail_config *config = config_of(dev);
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600212 uint32_t reg = pci_read_config32(dev, PHYCTL2_IOSFBCTL);
213 pll_en_off = !!(reg & PLL_OFF_EN);
214
215 strpfusecfg = pci_read_config32(dev, STRPFUSECFG);
Kein Yuan35110232014-02-22 12:26:55 -0800216
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300217 if (config->pcie_wake_enable)
Kein Yuan35110232014-02-22 12:26:55 -0800218 southcluster_smm_save_param(
219 SMM_SAVE_PARAM_PCIE_WAKE_ENABLE, 1);
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600220 }
221
222 /* Check if device is enabled in strapping. */
223 check_port_enabled(dev);
224 /* Determine if device is behind port. */
225 check_device_present(dev);
226
227 southcluster_enable_dev(dev);
228}
229
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +0200230static void byt_pciexp_scan_bridge(struct device *dev)
Kevin Hsiehd946f5e2014-11-26 03:08:18 +0800231{
232 static const struct reg_script wait_for_link_active[] = {
Elyes HAOUASa342f392018-10-17 10:56:26 +0200233 REG_PCI_POLL32(LCTL, (1 << 29), (1 << 29), 50000),
Kevin Hsiehd946f5e2014-11-26 03:08:18 +0800234 REG_SCRIPT_END,
235 };
236
237 /* wait for Link Active with 50ms timeout */
238 reg_script_run_on_dev(dev, wait_for_link_active);
239
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200240 do_pci_scan_bridge(dev, pciexp_scan_bus);
Kevin Hsiehd946f5e2014-11-26 03:08:18 +0800241}
242
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600243static struct pci_operations pcie_root_ops = {
Kyösti Mälkki25200322019-03-20 18:36:37 +0200244 .set_subsystem = pci_dev_set_subsystem,
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600245};
246
247static struct device_operations device_ops = {
248 .read_resources = pci_bus_read_resources,
249 .set_resources = pci_dev_set_resources,
250 .enable_resources = pci_bus_enable_resources,
251 .init = byt_pcie_init,
Kevin Hsiehd946f5e2014-11-26 03:08:18 +0800252 .scan_bus = byt_pciexp_scan_bridge,
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600253 .enable = byt_pcie_enable,
254 .ops_pci = &pcie_root_ops,
255};
256
257static const unsigned short pci_device_ids[] = {
258 PCIE_PORT1_DEVID, PCIE_PORT2_DEVID, PCIE_PORT3_DEVID, PCIE_PORT4_DEVID,
259 0
260};
261
262static const struct pci_driver pcie_root_ports __pci_driver = {
263 .ops = &device_ops,
264 .vendor = PCI_VENDOR_ID_INTEL,
265 .devices = pci_device_ids,
266};