blob: ff4891350e3811a69ea7531501d130b4b58ebd72 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <console/console.h>
21#include <device/device.h>
22#include <device/pci.h>
23#include <device/pciexp.h>
24#include <device/pci_ids.h>
25#include <reg_script.h>
26
Julius Werner18ea2d32014-10-07 16:42:17 -070027#include <soc/pci_devs.h>
28#include <soc/pcie.h>
29#include <soc/ramstage.h>
30#include <soc/smm.h>
Aaron Durbinae31f7d2013-11-22 14:16:49 -060031
32#include "chip.h"
33
34static int pll_en_off;
35static uint32_t strpfusecfg;
36
37static inline int root_port_offset(device_t dev)
38{
39 return PCI_FUNC(dev->path.pci.devfn);
40}
41
42static inline int is_first_port(device_t dev)
43{
44 return root_port_offset(dev) == PCIE_PORT1_FUNC;
45}
46
47static const struct reg_script init_static_before_exit_latency[] = {
48 /* Disable optimized buffer flush fill and latency tolerant reporting */
49 REG_PCI_RMW32(DCAP2, ~(OBFFS | LTRMS), 0),
50 REG_PCI_RMW32(DSTS2, ~(OBFFEN| LTRME), 0),
51 /* Set maximum payload size. */
52 REG_PCI_RMW32(DCAP, ~MPS_MASK, 0),
53 /* Disable transmit datapath flush timer, clear transmit config change
54 * wait time, clear sideband interface idle counter. */
55 REG_PCI_RMW32(PHYCTL2_IOSFBCTL, ~(TDFT | TXCFGCHWAIT | SIID), 0),
56 REG_SCRIPT_END,
57};
58
59static const struct reg_script init_static_after_exit_latency[] = {
60 /* Set common clock configuration. */
61 REG_PCI_OR16(LCTL, CCC),
62 /* Set NFTS to 0x743a361b */
63 REG_PCI_WRITE32(NFTS, 0x743a361b),
64 /* Set common clock latency to 0x3 */
65 REG_PCI_RMW32(MPC, ~CCEL_MASK, (0x3 << CCEL_SHIFT)),
66 /* Set relay timer policy. */
67 REG_PCI_RMW32(RTP, 0xff000000, 0x854c74),
68 /* Set IOSF packet fast transmit mode and link speed training policy. */
69 REG_PCI_OR16(MPC2, IPF | LSTP),
70 /* Channel configuration - enable upstream posted split, set non-posted
71 * and posted request size */
72 REG_PCI_RMW32(CHCFG, ~UPSD, UNRS | UPRS),
73 /* Completion status replay enable and set TLP grant count */
74 REG_PCI_RMW32(CFG2, ~(LATGC_MASK), CSREN | (3 << LATGC_SHIFT)),
75 /* Assume no IOAPIC behind root port -- disable EOI forwarding. */
76 REG_PCI_OR16(MPC2, EOIFD),
77 /* Expose AER */
78 REG_PCI_RMW32(AERCH, ~0, (1 << 16) | (1 << 0)),
79 /* set completion timeout to 160ms to 170ms */
80 REG_PCI_RMW16(DSTS2, ~CTD, 0x6),
81 /* Enable AER */
82 REG_PCI_OR16(DCTL_DSTS, URE | FEE | NFE | CEE),
Martin Roth99a3bba2014-12-07 14:57:26 -070083 /* Read and write back capability registers. */
Aaron Durbinae31f7d2013-11-22 14:16:49 -060084 REG_PCI_OR32(0x34, 0),
85 REG_PCI_OR32(0x80, 0),
86 /* Retrain the link. */
87 REG_PCI_OR16(LCTL, RL),
88 REG_SCRIPT_END,
89};
90
91static void byt_pcie_init(device_t dev)
92{
93 struct reg_script init_script[] = {
Aaron Durbinae31f7d2013-11-22 14:16:49 -060094 REG_SCRIPT_NEXT(init_static_before_exit_latency),
95 /* Exit latency configuration based on
96 * PHYCTL2_IOSFBCTL[PLL_OFF_EN] set in root port 1*/
97 REG_PCI_RMW32(LCAP, ~L1EXIT_MASK,
98 2 << (L1EXIT_MASK + pll_en_off)),
99 REG_SCRIPT_NEXT(init_static_after_exit_latency),
100 /* Disable hot plug, set power to 10W, set slot number. */
101 REG_PCI_RMW32(SLCAP, ~(HPC | HPS),
102 (1 << SLS_SHIFT) | (100 << SLV_SHIFT) |
103 (root_port_offset(dev) << SLN_SHIFT)),
104 /* Dynamic clock gating. */
105 REG_PCI_OR32(RPPGEN, RPDLCGEN | RPDBCGEN | RPSCGEN),
106 REG_PCI_OR32(PWRCTL, RPL1SQPOL | RPDTSQPOL),
107 REG_PCI_OR32(PCIEDBG, SPCE),
108 REG_SCRIPT_END,
109 };
110
Aaron Durbin616f3942013-12-10 17:12:44 -0800111 reg_script_run_on_dev(dev, init_script);
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600112
113 if (is_first_port(dev)) {
114 struct soc_intel_baytrail_config *config = dev->chip_info;
115 uint32_t reg = pci_read_config32(dev, RPPGEN);
116 reg |= SRDLCGEN | SRDBCGEN;
117
118 if (config && config->clkreq_enable)
119 reg |= LCLKREQEN | BBCLKREQEN;
120
121 pci_write_config32(dev, RPPGEN, reg);
122 }
123}
124
125static const struct reg_script no_dev_behind_port[] = {
126 REG_PCI_OR32(PCIEALC, (1 << 26)),
127 REG_PCI_POLL32(PCIESTS1, 0x1f000000, (1 << 24), 50000),
128 REG_PCI_OR32(PHYCTL4, SQDIS),
129 REG_SCRIPT_END,
130};
131
132static void check_port_enabled(device_t dev)
133{
134 int rp_config = (strpfusecfg & LANECFG_MASK) >> LANECFG_SHIFT;
135
136 switch (root_port_offset(dev)) {
137 case PCIE_PORT1_FUNC:
138 /* Port 1 cannot be disabled from strapping config. */
139 break;
140 case PCIE_PORT2_FUNC:
141 /* Port 2 disabled in all configs but 4x1. */
142 if (rp_config != 0x0)
143 dev->enabled = 0;
144 break;
145 case PCIE_PORT3_FUNC:
146 /* Port 3 disabled only in 1x4 config. */
147 if (rp_config == 0x3)
148 dev->enabled = 0;
149 break;
150 case PCIE_PORT4_FUNC:
151 /* Port 4 disabled in 1x4 and 2x2 config. */
152 if (rp_config >= 0x2)
153 dev->enabled = 0;
154 break;
155 }
156}
157
Kenji Chene237f5a2014-09-12 02:10:53 +0800158static u8 all_ports_no_dev_present(device_t dev)
159{
160 u8 func;
161 u8 temp = dev->path.pci.devfn;
162 u8 device_not_present = 1;
163 u8 data;
164
165 for (func = 1; func < PCIE_ROOT_PORT_COUNT; func++) {
166 dev->path.pci.devfn &= ~0x7;
167 dev->path.pci.devfn |= func;
168
169 /* is pcie device there */
170 if (pci_read_config32(dev, 0) == 0xFFFFFFFF)
171 continue;
172
173 data = pci_read_config8(dev, XCAP + 3) | (SI >> 24);
174 pci_write_config8(dev, XCAP + 3, data);
175
176 /* is any device present */
177 if ((pci_read_config32(dev, SLCTL_SLSTS) & PDS)) {
178 device_not_present = 0;
179 break;
180 }
181 }
182
183 dev->path.pci.devfn = temp;
184 return device_not_present;
185}
186
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600187static void check_device_present(device_t dev)
188{
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600189 /* Set slot implemented. */
190 pci_write_config32(dev, XCAP, pci_read_config32(dev, XCAP) | SI);
191
192 /* No device present. */
193 if (!(pci_read_config32(dev, SLCTL_SLSTS) & PDS)) {
194 printk(BIOS_DEBUG, "No PCIe device present.\n");
Kenji Chene237f5a2014-09-12 02:10:53 +0800195 if (is_first_port(dev)) {
196 if (all_ports_no_dev_present(dev)) {
197 reg_script_run_on_dev(dev, no_dev_behind_port);
198 dev->enabled = 0;
199 }
200 } else {
Kenji Chen97acc5e2014-10-31 00:32:09 -0700201 reg_script_run_on_dev(dev, no_dev_behind_port);
Kenji Chene237f5a2014-09-12 02:10:53 +0800202 dev->enabled = 0;
203 }
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600204 } else if(!dev->enabled) {
205 /* Port is disabled, but device present. Disable link. */
206 pci_write_config32(dev, LCTL,
207 pci_read_config32(dev, LCTL) | LD);
208 }
209}
210
211static void byt_pcie_enable(device_t dev)
212{
213 if (is_first_port(dev)) {
Kein Yuan35110232014-02-22 12:26:55 -0800214 struct soc_intel_baytrail_config *config = dev->chip_info;
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600215 uint32_t reg = pci_read_config32(dev, PHYCTL2_IOSFBCTL);
216 pll_en_off = !!(reg & PLL_OFF_EN);
217
218 strpfusecfg = pci_read_config32(dev, STRPFUSECFG);
Kein Yuan35110232014-02-22 12:26:55 -0800219
220 if (config && config->pcie_wake_enable)
221 southcluster_smm_save_param(
222 SMM_SAVE_PARAM_PCIE_WAKE_ENABLE, 1);
Aaron Durbinae31f7d2013-11-22 14:16:49 -0600223 }
224
225 /* Check if device is enabled in strapping. */
226 check_port_enabled(dev);
227 /* Determine if device is behind port. */
228 check_device_present(dev);
229
230 southcluster_enable_dev(dev);
231}
232
233static void pcie_root_set_subsystem(device_t dev, unsigned vid, unsigned did)
234{
235 uint32_t didvid = ((did & 0xffff) << 16) | (vid & 0xffff);
236
237 if (!didvid)
238 didvid = pci_read_config32(dev, PCI_VENDOR_ID);
239 pci_write_config32(dev, 0x94, didvid);
240}
241
242static struct pci_operations pcie_root_ops = {
243 .set_subsystem = &pcie_root_set_subsystem,
244};
245
246static struct device_operations device_ops = {
247 .read_resources = pci_bus_read_resources,
248 .set_resources = pci_dev_set_resources,
249 .enable_resources = pci_bus_enable_resources,
250 .init = byt_pcie_init,
251 .scan_bus = pciexp_scan_bridge,
252 .enable = byt_pcie_enable,
253 .ops_pci = &pcie_root_ops,
254};
255
256static const unsigned short pci_device_ids[] = {
257 PCIE_PORT1_DEVID, PCIE_PORT2_DEVID, PCIE_PORT3_DEVID, PCIE_PORT4_DEVID,
258 0
259};
260
261static const struct pci_driver pcie_root_ports __pci_driver = {
262 .ops = &device_ops,
263 .vendor = PCI_VENDOR_ID_INTEL,
264 .devices = pci_device_ids,
265};