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