coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
pcie.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <device/pci_ops.h>
7 #include <device/pciexp.h>
8 #include <device/pci_ids.h>
9 #include <reg_script.h>
10 
11 #include <soc/pci_devs.h>
12 #include <soc/pcie.h>
13 #include <soc/ramstage.h>
14 #include <soc/smm.h>
15 
16 #include "chip.h"
17 
18 static int pll_en_off;
20 
21 static inline int root_port_offset(struct device *dev)
22 {
23  return PCI_FUNC(dev->path.pci.devfn);
24 }
25 
26 static inline int is_first_port(struct device *dev)
27 {
28  return root_port_offset(dev) == PCIE_PORT1_FUNC;
29 }
30 
31 static const struct reg_script init_static_before_exit_latency[] = {
32  /* Disable optimized buffer flush fill and latency tolerant reporting */
33  REG_PCI_RMW32(DCAP2, ~(OBFFS | LTRMS), 0),
35  /* Set maximum payload size. */
37  /* Disable transmit datapath flush timer, clear transmit config change
38  * wait time, clear sideband interface idle counter. */
41 };
42 
43 static const struct reg_script init_static_after_exit_latency[] = {
44  /* Set common clock configuration. */
46  /* Set NFTS to 0x743a361b */
47  REG_PCI_WRITE32(NFTS, 0x743a361b),
48  /* Set common clock latency to 0x3 */
50  /* Set relay timer policy. */
51  REG_PCI_RMW32(RTP, 0xff000000, 0x854c74),
52  /* Set IOSF packet fast transmit mode and link speed training policy. */
54  /* Channel configuration - enable upstream posted split, set non-posted
55  * and posted request size */
57  /* Completion status replay enable and set TLP grant count */
59  /* Assume no IOAPIC behind root port -- disable EOI forwarding. */
61  /* Expose AER */
62  REG_PCI_RMW32(AERCH, ~0, (1 << 16) | (1 << 0)),
63  /* set completion timeout to 160ms to 170ms */
64  REG_PCI_RMW16(DSTS2, ~CTD, 0x6),
65  /* Enable AER */
67  /* Read and write back capability registers. */
68  REG_PCI_OR32(0x34, 0),
69  REG_PCI_OR32(0x80, 0),
70  /* Retrain the link. */
73 };
74 
75 static void byt_pcie_init(struct device *dev)
76 {
77  struct reg_script init_script[] = {
79  /* Exit latency configuration based on
80  * PHYCTL2_IOSFBCTL[PLL_OFF_EN] set in root port 1*/
82  2 << (L1EXIT_SHIFT + pll_en_off)),
84  /* Disable hot plug, set power to 10W, set slot number. */
85  REG_PCI_RMW32(SLCAP, ~(HPC | HPS),
86  (1 << SLS_SHIFT) | (100 << SLV_SHIFT) |
88  /* Dynamic clock gating. */
93  };
94 
95  reg_script_run_on_dev(dev, init_script);
96 
97  if (is_first_port(dev)) {
99  uint32_t reg = pci_read_config32(dev, RPPGEN);
100  reg |= SRDLCGEN | SRDBCGEN;
101 
102  if (config->clkreq_enable)
103  reg |= LCLKREQEN | BBCLKREQEN;
104 
105  pci_write_config32(dev, RPPGEN, reg);
106  }
107 }
108 
109 static const struct reg_script no_dev_behind_port[] = {
110  REG_PCI_OR32(PCIEALC, (1 << 26)),
111  REG_PCI_POLL32(PCIESTS1, 0x1f000000, (1 << 24), 50000),
114 };
115 
116 static void check_port_enabled(struct device *dev)
117 {
118  int rp_config = (strpfusecfg & LANECFG_MASK) >> LANECFG_SHIFT;
119 
120  switch (root_port_offset(dev)) {
121  case PCIE_PORT1_FUNC:
122  /* Port 1 cannot be disabled from strapping config. */
123  break;
124  case PCIE_PORT2_FUNC:
125  /* Port 2 disabled in all configs but 4x1. */
126  if (rp_config != 0x0)
127  dev->enabled = 0;
128  break;
129  case PCIE_PORT3_FUNC:
130  /* Port 3 disabled only in 1x4 config. */
131  if (rp_config == 0x3)
132  dev->enabled = 0;
133  break;
134  case PCIE_PORT4_FUNC:
135  /* Port 4 disabled in 1x4 and 2x2 config. */
136  if (rp_config >= 0x2)
137  dev->enabled = 0;
138  break;
139  }
140 }
141 
143 {
144  u8 func;
145  u8 temp = dev->path.pci.devfn;
146  u8 device_not_present = 1;
147  u8 data;
148 
149  for (func = 1; func < PCIE_ROOT_PORT_COUNT; func++) {
150  dev->path.pci.devfn &= ~0x7;
151  dev->path.pci.devfn |= func;
152 
153  /* is PCIe device there */
154  if (pci_read_config32(dev, 0) == 0xFFFFFFFF)
155  continue;
156 
157  data = pci_read_config8(dev, XCAP + 3) | (SI >> 24);
158  pci_write_config8(dev, XCAP + 3, data);
159 
160  /* is any device present */
161  if ((pci_read_config32(dev, SLCTL_SLSTS) & PDS)) {
162  device_not_present = 0;
163  break;
164  }
165  }
166 
167  dev->path.pci.devfn = temp;
168  return device_not_present;
169 }
170 
171 static void check_device_present(struct device *dev)
172 {
173  /* Set slot implemented. */
175 
176  /* No device present. */
177  if (!(pci_read_config32(dev, SLCTL_SLSTS) & PDS)) {
178  printk(BIOS_DEBUG, "No PCIe device present.\n");
179  if (is_first_port(dev)) {
182  dev->enabled = 0;
183  }
184  } else {
186  dev->enabled = 0;
187  }
188  } else if (!dev->enabled) {
189  /* Port is disabled, but device present. Disable link. */
192  }
193 }
194 
195 static void byt_pcie_enable(struct device *dev)
196 {
197  if (is_first_port(dev)) {
200  pll_en_off = !!(reg & PLL_OFF_EN);
201 
203 
204  if (config->pcie_wake_enable)
207  }
208 
209  /* Check if device is enabled in strapping. */
210  check_port_enabled(dev);
211  /* Determine if device is behind port. */
213 
215 }
216 
217 static void byt_pciexp_scan_bridge(struct device *dev)
218 {
219  static const struct reg_script wait_for_link_active[] = {
220  REG_PCI_POLL32(LCTL, (1 << 29), (1 << 29), 50000),
222  };
223 
224  /* wait for Link Active with 50ms timeout */
225  reg_script_run_on_dev(dev, wait_for_link_active);
226 
228 }
229 
230 static struct device_operations device_ops = {
232  .set_resources = pci_dev_set_resources,
233  .enable_resources = pci_bus_enable_resources,
234  .init = byt_pcie_init,
235  .scan_bus = byt_pciexp_scan_bridge,
236  .enable = byt_pcie_enable,
237  .ops_pci = &pci_dev_ops_pci,
238 };
239 
240 static const unsigned short pci_device_ids[] = {
242  0
243 };
244 
245 static const struct pci_driver pcie_root_ports __pci_driver = {
246  .ops = &device_ops,
247  .vendor = PCI_VID_INTEL,
248  .devices = pci_device_ids,
249 };
#define printk(level,...)
Definition: stdlib.h:16
#define CFG2
Definition: gl9750.h:10
static DEVTREE_CONST void * config_of(const struct device *dev)
Definition: device.h:382
static __always_inline void pci_write_config32(const struct device *dev, u16 reg, u32 val)
Definition: pci_ops.h:76
static __always_inline u32 pci_read_config32(const struct device *dev, u16 reg)
Definition: pci_ops.h:58
static __always_inline u8 pci_read_config8(const struct device *dev, u16 reg)
Definition: pci_ops.h:46
static __always_inline void pci_write_config8(const struct device *dev, u16 reg, u8 val)
Definition: pci_ops.h:64
#define PWRCTL
Definition: pcie.h:58
#define DCAP2
Definition: pcie.h:32
#define CCEL_SHIFT
Definition: pcie.h:48
#define LTRME
Definition: pcie.h:37
#define MPS_MASK
Definition: pcie.h:10
#define CSREN
Definition: pcie.h:73
#define PDS
Definition: pcie.h:31
#define RL
Definition: pcie.h:21
#define L1EXIT_MASK
Definition: pcie.h:18
#define RPPGEN
Definition: pcie.h:50
#define LCAP
Definition: pcie.h:16
#define SLCTL_SLSTS
Definition: pcie.h:30
#define TDFT
Definition: pcie.h:63
#define LATGC_SHIFT
Definition: pcie.h:74
#define PCIE_ROOT_PORT_COUNT
Definition: pcie.h:84
#define MPC
Definition: pcie.h:47
#define CHCFG
Definition: pcie.h:39
#define PLL_OFF_EN
Definition: pcie.h:62
#define DSTS2
Definition: pcie.h:35
#define SLCAP
Definition: pcie.h:24
#define RPSCGEN
Definition: pcie.h:51
#define UPRS
Definition: pcie.h:42
#define AERCH
Definition: pcie.h:69
#define PCIEDBG
Definition: pcie.h:76
#define LTRMS
Definition: pcie.h:34
#define CTD
Definition: pcie.h:38
#define FEE
Definition: pcie.h:13
#define LANECFG_MASK
Definition: pcie.h:68
#define BBCLKREQEN
Definition: pcie.h:53
#define LANECFG_SHIFT
Definition: pcie.h:67
#define SPCE
Definition: pcie.h:77
#define URE
Definition: pcie.h:12
#define OBFFEN
Definition: pcie.h:36
#define PCIESTS1
Definition: pcie.h:78
#define RPL1SQPOL
Definition: pcie.h:59
#define DCTL_DSTS
Definition: pcie.h:11
#define PHYCTL2_IOSFBCTL
Definition: pcie.h:61
#define LSTP
Definition: pcie.h:45
#define HPC
Definition: pcie.h:28
#define SRDBCGEN
Definition: pcie.h:55
#define OBFFS
Definition: pcie.h:33
#define MPC2
Definition: pcie.h:43
#define IPF
Definition: pcie.h:44
#define LD
Definition: pcie.h:22
#define RPDBCGEN
Definition: pcie.h:57
#define SLN_SHIFT
Definition: pcie.h:25
#define NFE
Definition: pcie.h:14
#define TXCFGCHWAIT
Definition: pcie.h:64
#define SI
Definition: pcie.h:8
#define SLV_SHIFT
Definition: pcie.h:27
#define NFTS
Definition: pcie.h:70
#define CCC
Definition: pcie.h:20
#define L1EXIT_SHIFT
Definition: pcie.h:17
#define CEE
Definition: pcie.h:15
#define LATGC_MASK
Definition: pcie.h:75
#define UNRS
Definition: pcie.h:41
#define CCEL_MASK
Definition: pcie.h:49
#define XCAP
Definition: pcie.h:7
#define SIID
Definition: pcie.h:65
#define STRPFUSECFG
Definition: pcie.h:66
#define SQDIS
Definition: pcie.h:82
#define HPS
Definition: pcie.h:29
#define RTP
Definition: pcie.h:80
#define UPSD
Definition: pcie.h:40
#define PHYCTL4
Definition: pcie.h:81
#define SRDLCGEN
Definition: pcie.h:54
#define PCIEALC
Definition: pcie.h:79
#define LCTL
Definition: pcie.h:19
#define RPDLCGEN
Definition: pcie.h:56
#define LCLKREQEN
Definition: pcie.h:52
#define EOIFD
Definition: pcie.h:46
#define DCAP
Definition: pcie.h:9
#define RPDTSQPOL
Definition: pcie.h:60
#define SLS_SHIFT
Definition: pcie.h:26
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
enum board_config config
Definition: memory.c:448
#define PCI_FUNC(devfn)
Definition: pci_def.h:550
void pci_bus_enable_resources(struct device *dev)
Definition: pci_device.c:758
void pci_bus_read_resources(struct device *dev)
Definition: pci_device.c:540
void do_pci_scan_bridge(struct device *dev, void(*do_scan_bus)(struct bus *bus, unsigned int min_devfn, unsigned int max_devfn))
Scan a PCI bridge and the buses behind the bridge.
Definition: pci_device.c:1558
struct pci_operations pci_dev_ops_pci
Default device operation for PCI devices.
Definition: pci_device.c:911
void pci_dev_set_resources(struct device *dev)
Definition: pci_device.c:691
#define PCI_VID_INTEL
Definition: pci_ids.h:2157
void pciexp_scan_bus(struct bus *bus, unsigned int min_devfn, unsigned int max_devfn)
#define REG_PCI_OR32(reg_, value_)
Definition: reg_script.h:187
#define REG_PCI_RMW16(reg_, mask_, value_)
Definition: reg_script.h:173
#define REG_SCRIPT_NEXT(next_)
Definition: reg_script.h:411
void reg_script_run_on_dev(struct device *dev, const struct reg_script *step)
Definition: reg_script.c:689
#define REG_PCI_OR16(reg_, value_)
Definition: reg_script.h:185
#define REG_PCI_RMW32(reg_, mask_, value_)
Definition: reg_script.h:175
#define REG_PCI_WRITE32(reg_, value_)
Definition: reg_script.h:169
#define REG_SCRIPT_END
Definition: reg_script.h:427
#define REG_PCI_POLL32(reg_, mask_, value_, timeout_)
Definition: reg_script.h:193
#define PCIE_PORT1_FUNC
Definition: pci_devs.h:74
#define PCIE_PORT3_FUNC
Definition: pci_devs.h:78
#define PCIE_PORT1_DEVID
Definition: pci_devs.h:130
#define PCIE_PORT2_FUNC
Definition: pci_devs.h:76
#define PCIE_PORT3_DEVID
Definition: pci_devs.h:132
#define PCIE_PORT2_DEVID
Definition: pci_devs.h:131
#define PCIE_PORT4_FUNC
Definition: pci_devs.h:80
#define PCIE_PORT4_DEVID
Definition: pci_devs.h:133
void southcluster_enable_dev(struct device *dev)
Definition: southcluster.c:452
void smm_southcluster_save_param(int param, uint32_t data)
Definition: smm.c:17
@ SMM_SAVE_PARAM_PCIE_WAKE_ENABLE
Definition: smm.h:10
static struct device_operations device_ops
Definition: pcie.c:230
static const struct reg_script init_static_after_exit_latency[]
Definition: pcie.c:43
static const struct reg_script no_dev_behind_port[]
Definition: pcie.c:109
static void byt_pcie_enable(struct device *dev)
Definition: pcie.c:195
static int is_first_port(struct device *dev)
Definition: pcie.c:26
static void check_device_present(struct device *dev)
Definition: pcie.c:171
static const struct reg_script init_static_before_exit_latency[]
Definition: pcie.c:31
static int root_port_offset(struct device *dev)
Definition: pcie.c:21
static const struct pci_driver pcie_root_ports __pci_driver
Definition: pcie.c:245
static int pll_en_off
Definition: pcie.c:18
static u8 all_ports_no_dev_present(struct device *dev)
Definition: pcie.c:142
static void byt_pcie_init(struct device *dev)
Definition: pcie.c:75
static void check_port_enabled(struct device *dev)
Definition: pcie.c:116
static const unsigned short pci_device_ids[]
Definition: pcie.c:240
static uint32_t strpfusecfg
Definition: pcie.c:19
static void byt_pciexp_scan_bridge(struct device *dev)
Definition: pcie.c:217
unsigned int uint32_t
Definition: stdint.h:14
uint8_t u8
Definition: stdint.h:45
void(* read_resources)(struct device *dev)
Definition: device.h:39
struct pci_path pci
Definition: path.h:116
Definition: device.h:107
struct device_path path
Definition: device.h:115
unsigned int enabled
Definition: device.h:122
unsigned int devfn
Definition: path.h:54
struct device * dev
Definition: reg_script.h:78