Debian Codegraph @

src/soc/qualcomm/common/pcie_common.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <boot/coreboot_tables.h>
4 #include <console/console.h>
5 #include <delay.h>
6 #include <device/device.h>
7 #include <device/mmio.h>
8 #include <device/pci.h>
9 #include <device/pci_ids.h>
10 #include <gpio.h>
11 #include <soc/clock.h>
12 #include <soc/qcom_qmp_phy.h>
13 #include <soc/pcie.h>
14 #include <timer.h>
15
16 #define ROOT_PORT_BDF 0x0
17 #define ATU_CTRL2 PCIE_ATU_UNR_REGION_CTRL2
18
19 #if CONFIG(QMP_PHY_2X2_1X4)
20 #define LINK_SPEED_DEFAULT LINK_SPEED_GEN_4
21 #else
22 #define LINK_SPEED_DEFAULT LINK_SPEED_GEN_2
23 #endif
24
25 static struct qcom_pcie_cntlr_t qcom_pcie_cfg;
26
27 static inline void dw_pcie_dbi_rd_wr(bool enable)
28 {
29 uint32_t val;
30 pcie_cntlr_cfg_t *pcierc = qcom_pcie_cfg.cntlr_cfg;
31
32 val = read32(pcierc->dbi_base + PCIE_DBI_MISC_CONTROL_1_OFF);
33
34 if (enable)
35 val |= PCIE_DBI_RO_WR_EN;
36 else
37 val &= ~PCIE_DBI_RO_WR_EN;
38
39 write32(pcierc->dbi_base + PCIE_DBI_MISC_CONTROL_1_OFF, val);
40 }
41
42 static void dw_pcie_writel_iatu(unsigned int index, uint32_t reg, uint32_t val)
43 {
44 pcie_cntlr_cfg_t *pcierc = qcom_pcie_cfg.cntlr_cfg;
45 unsigned int offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
46
47 write32(pcierc->atu_base + offset + reg, val);
48 }
49
50 static uint32_t dw_pcie_readl_iatu(unsigned int index, uint32_t reg)
51 {
52 pcie_cntlr_cfg_t *pcierc = qcom_pcie_cfg.cntlr_cfg;
53 unsigned int offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
54
55 return read32(pcierc->atu_base + offset + reg);
56 }
57
58 static void qcom_dw_pcie_prog_outbound_atu(unsigned int index,
59 unsigned int type, uint64_t cpu_addr,
60 uint64_t pcie_addr, uint32_t size)
61 {
62 dw_pcie_writel_iatu(index, PCIE_ATU_UNR_LOWER_BASE, lower_32_bits(cpu_addr));
63 dw_pcie_writel_iatu(index, PCIE_ATU_UNR_UPPER_BASE, upper_32_bits(cpu_addr));
64 dw_pcie_writel_iatu(index, PCIE_ATU_UNR_LIMIT, lower_32_bits(cpu_addr + size - 1));
65 dw_pcie_writel_iatu(index, PCIE_ATU_UNR_LOWER_TARGET, lower_32_bits(pcie_addr));
66 dw_pcie_writel_iatu(index, PCIE_ATU_UNR_UPPER_TARGET, upper_32_bits(pcie_addr));
67 dw_pcie_writel_iatu(index, PCIE_ATU_UNR_REGION_CTRL1, type);
68 dw_pcie_writel_iatu(index, PCIE_ATU_UNR_REGION_CTRL2, PCIE_ATU_ENABLE);
69
70 /* Ensure ATU enable takes effect before any subsequent config access */
71 if (retry(LINK_WAIT_MAX_IATU_RETRIES,
72 (dw_pcie_readl_iatu(index, ATU_CTRL2) & PCIE_ATU_ENABLE),
73 udelay(LINK_WAIT_IATU)))
74 return;
75
76 printk(BIOS_ERR, "PCIe o/b iATU couldn't be enabled after 5ms\n");
77 }
78
79 static void qcom_pcie_configure_gpios(pcie_cntlr_cfg_t *cfg)
80 {
81 gpio_configure(cfg->perst, 0, GPIO_NO_PULL, GPIO_16MA, GPIO_OUTPUT);
82 }
83
84 /**
85 * qcom_dw_pcie_configure() - Configure link capabilities and speed
86 *
87 * Configure the link capabilities and speed in the PCIe root complex.
88 */
89 static void qcom_dw_pcie_configure(uint32_t cap_speed)
90 {
91 pcie_cntlr_cfg_t *pcierc = qcom_pcie_cfg.cntlr_cfg;
92
93 dw_pcie_dbi_rd_wr(true);
94
95 clrsetbits32(pcierc->dbi_base + PCIE_LINK_CAPABILITY,
96 TARGET_LINK_SPEED_MASK, cap_speed);
97
98 clrsetbits32(pcierc->dbi_base + PCIE_LINK_CTL_2,
99 TARGET_LINK_SPEED_MASK, cap_speed);
100
101 dw_pcie_dbi_rd_wr(false);
102 printk(BIOS_INFO, "PCIe Link speed configured in Gen %d\n", cap_speed);
103 }
104
105 #if CONFIG(QMP_PHY_2X2_1X4)
106 static void post_phy_pwr_up_init(struct qcom_pcie_cntlr_t *pcie)
107 {
108 pcie_cntlr_cfg_t *pcierc = pcie->cntlr_cfg;
109 /* configure PCIe to RC mode */
110 write32(pcierc->parf + PCIE_PARF_DEVICE_TYPE, DEVICE_TYPE_RC);
111 dsb();
112 clrbits32(pcierc->parf + PCIE_PARF_SLAVE_AXI_ERR_REPORT, AXI_SLAVE_ERR_CRS_BRESP_EN);
113 clrsetbits32(pcierc->dbi_base + PCIE_AMBA_ERR_RESP_DEFLT_OFF, AMBA_ERR_RESP_CRS_MASK, 0x2);
114 dsb();
115 write32(pcierc->parf + PCIE_PARF_AXI_MSTR_WR_ADDR_HALT, 0x0);
116 write32(pcierc->parf + PCIE_SRIS_MODE, 0x0);
117 write32(pcierc->parf + PCIE_APP_MARGINING_CTRL, (APP_MARGINING_SW_READY | APP_MARGINING_READY));
118 dsb();
119 }
120
121 static void post_phy_pwr_up_dbi_init(struct qcom_pcie_cntlr_t *pcie)
122 {
123 pcie_cntlr_cfg_t *pcierc = pcie->cntlr_cfg;
124 dw_pcie_dbi_rd_wr(true);
125 write32(pcierc->dbi_base + PCIE_SPCIE_CAP_OFF_0CH_REG, 0x55555555);
126 write32(pcierc->dbi_base + PCIE_SPCIE_CAP_OFF_10H_REG, 0x55555555);
127 write32(pcierc->dbi_base + PCIE_PL16G_CAP_OFF_20H_REG, 0x55555555);
128 clrbits32(pcierc->dbi_base + PCIE_SLOT_CAPABILITIES_REG, PCIE_CAP_HOT_PLUG_CAPABLE);
129 dsb();
130 clrsetbits32(pcierc->dbi_base + PCIE_GEN3_RELATED_OFF, RATE_SHADOW_SEL_MASK, 0x0);
131 clrsetbits32(pcierc->dbi_base + PCIE_GEN3_EQ_FB_MODE_DIR_CHANGE_OFF, GEN3_EQ_FMDC_T_MIN_PHASE23, 0x1);
132 clrbits32(pcierc->dbi_base + PCIE_GEN3_EQ_CONTROL_OFF, GEN3_EQ_PSET_REQ_VEC);
133 dsb();
134 clrsetbits32(pcierc->dbi_base + PCIE_GEN3_RELATED_OFF, RATE_SHADOW_SEL_MASK, RATE_SHADOW_SEL_VAL);
135 clrsetbits32(pcierc->dbi_base + PCIE_GEN3_EQ_FB_MODE_DIR_CHANGE_OFF, GEN3_EQ_FMDC_T_MIN_PHASE23, 0x1);
136 clrbits32(pcierc->dbi_base + PCIE_GEN3_EQ_CONTROL_OFF, GEN3_EQ_PSET_REQ_VEC);
137 dsb();
138 dw_pcie_dbi_rd_wr(false);
139 }
140 #endif
141
142 /**
143 * is_pcie_link_up() - Return the link state
144 *
145 * Return: true for active link and false for no link
146 */
147 static bool is_pcie_link_up(struct qcom_pcie_cntlr_t *pci)
148 {
149 /* Read link status register */
150 return !!(read32(pci->cntlr_cfg->elbi + PCIE3X2_ELBI_SYS_STTS) & XMLH_LINK_UP);
151 }
152
153 /**
154 * wait_link_up() - Wait for the link to come up
155 *
156 * Return: true for active line and false for no link (timeout)
157 */
158 static bool wait_link_up(struct qcom_pcie_cntlr_t *pci)
159 {
160 /* Check if the link is up or not */
161 if (retry(LINK_WAIT_MAX_RETRIES, is_pcie_link_up(pci), mdelay(PCIE_LINK_UP_MS)))
162 return true;
163
164 printk(BIOS_ERR, "PCIe link is not up even after 1sec\n");
165 return false;
166 }
167
168 static enum cb_err qcom_pcie_dw_link_up(struct qcom_pcie_cntlr_t *pcie)
169 {
170 if (is_pcie_link_up(pcie)) {
171 printk(BIOS_INFO, "PCIe Link is already up\n");
172 return CB_SUCCESS;
173 }
174
175 /* DW pre link configurations */
176 qcom_dw_pcie_configure(LINK_SPEED_DEFAULT);
177
178 /* enable link training */
179 setbits32(pcie->cntlr_cfg->parf + PCIE_PARF_LTSSM, LTSSM_EN);
180
181 /* Check that link was established */
182 if (wait_link_up(pcie)) {
183 printk(BIOS_INFO, "PCIe link is up\n");
184 return CB_SUCCESS;
185 }
186
187 /*
188 * Link can be established in Gen 1 as it failed to establish in Gen2.
189 * So allow some time to do it.
190 */
191 udelay(100);
192
193 return CB_ERR;
194 }
195
196 /**
197 * Returns root port config space address or endpoint config space address.
198 * For endpoint config address, mapping would be done with ATU.
199 */
200 static void *qcom_dw_pcie_get_config_addr(struct qcom_pcie_cntlr_t *pcierc,
201 pci_devfn_t dev)
202 {
203 unsigned int atu_type, cfg_size;
204 void *cfg_address;
205 uint32_t bus, busdev, devfn;
206 pcie_cntlr_cfg_t *cntlr = pcierc->cntlr_cfg;
207 bus = PCI_DEV2SEGBUS(dev);
208 devfn = PCI_DEV2DEVFN(dev);
209
210 busdev = PCIE_ATU_BUS(bus) |
211 PCIE_ATU_DEV(PCI_SLOT(devfn)) |
212 PCIE_ATU_FUNC(PCI_FUNC(devfn));
213
214 /* Accessing root port configuration space */
215 if (!bus && !devfn) {
216 cfg_address = pcierc->cntlr_cfg->dbi_base;
217 return cfg_address;
218 }
219
220 /* For local bus use CFG0 type */
221 atu_type = (bus == 1) ? PCIE_ATU_TYPE_CFG0 : PCIE_ATU_TYPE_CFG1;
222
223 cfg_address = cntlr->cfg_base + (cntlr->cfg_size * devfn);
224 cfg_size = cntlr->cfg_size;
225
226 qcom_dw_pcie_prog_outbound_atu(PCIE_ATU_REGION_INDEX1, atu_type,
227 (uint64_t)cfg_address, busdev, cfg_size);
228 return cfg_address;
229 }
230
231 static void qcom_dw_pcie_setup_rc(struct qcom_pcie_cntlr_t *pcie)
232 {
233 uint32_t val;
234 pcie_cntlr_cfg_t *pcierc = pcie->cntlr_cfg;
235 /*
236 * Enable DBI read-only registers for writing/updating configuration.
237 * Write permission gets disabled towards the end of this function.
238 */
239 dw_pcie_dbi_rd_wr(true);
240
241 val = read32(pcierc->dbi_base + PCIE_PORT_LINK_CONTROL);
242 /* Set the number of lanes */
243 val &= ~PORT_LINK_MODE_MASK;
244
245 switch (pcierc->lanes) {
246 case 1:
247 val |= PORT_LINK_MODE_1_LANES;
248 break;
249 case 2:
250 val |= PORT_LINK_MODE_2_LANES;
251 break;
252 case 4:
253 val |= PORT_LINK_MODE_4_LANES;
254 break;
255 case 8:
256 val |= PORT_LINK_MODE_8_LANES;
257 break;
258 default:
259 printk(BIOS_INFO, "PCIe num-lanes %u: invalid value\n",
260 pcierc->lanes);
261 return;
262 }
263
264 write32(pcierc->dbi_base + PCIE_PORT_LINK_CONTROL, val);
265
266 /* Set link width speed control register */
267 val = read32(pcierc->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
268 val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
269
270 switch (pcierc->lanes) {
271 case 1:
272 val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
273 break;
274 case 2:
275 val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
276 break;
277 case 4:
278 val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
279 break;
280 case 8:
281 val |= PORT_LOGIC_LINK_WIDTH_8_LANES;
282 break;
283 default:
284 printk(BIOS_INFO, "PCIe invalid lanes option\n");
285 return;
286 }
287
288 write32(pcierc->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL, val);
289
290 /* Setup bus numbers */
291 clrsetbits32(pcierc->dbi_base + PCI_PRIMARY_BUS, TYPE1_HDR_BUS_NUM_MASK,
292 ROOT_PORT_BUS_NUM);
293
294 /* Disable SMMU */
295 write32(pcierc->parf + PCIE_PARF_BDF_TO_SID_CFG, BDF_TO_SID_BYPASS);
296
297 /* Configure ATU for outbound accesses */
298 qcom_dw_pcie_prog_outbound_atu(PCIE_ATU_REGION_INDEX0,
299 PCIE_ATU_TYPE_MEM,
300 pcierc->mem.phys_start,
301 ROOT_PORT_BDF, pcierc->mem.size);
302
303 /* Program correct class for RC */
304 write16(pcierc->dbi_base + PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
305
306 /* Disable write permission right after the update */
307 dw_pcie_dbi_rd_wr(false);
308 }
309
310 static void qcom_qmp_phy_config_lane(void *base, const struct qcom_qmp_phy_init_tbl tbl[],
311 unsigned int num, uint8_t ln_mask)
312 {
313 unsigned int i;
314 const struct qcom_qmp_phy_init_tbl *t = tbl;
315
316 for (i = 0; i < num; i++, t++) {
317 if (!(t->lane_mask & ln_mask))
318 continue;
319 write32(base + t->offset, t->val);
320 }
321 }
322
323 static void qcom_qmp_phy_configure(void *base, const struct qcom_qmp_phy_init_tbl tbl[],
324 unsigned int num)
325 {
326 qcom_qmp_phy_config_lane(base, tbl, num, 0xff);
327 }
328 #if !CONFIG(QMP_PHY_2X2_1X4)
329 static enum cb_err qcom_qmp_phy_power_on(pcie_qmp_phy_cfg_t *qphy)
330 {
331 uint64_t lock_us;
332
333 /* Release powerdown mode and allow endpoint refclk drive */
334 write32(qphy->pcs + QPHY_PCS_PWR_DWN_CNTRL, SW_PWRDN | REFCLK_DRV_DSBL);
335
336 /* Serdes configuration */
337 qcom_qmp_phy_configure(qphy->serdes, qphy->serdes_tbl, qphy->serdes_tbl_num);
338
339 /* Tx, Rx, and PCS configurations */
340 qcom_qmp_phy_config_lane(qphy->tx0, qphy->tx_tbl, qphy->tx_tbl_num, 1);
341 qcom_qmp_phy_config_lane(qphy->tx0, qphy->tx_tbl_sec, qphy->tx_tbl_num_sec, 1);
342 qcom_qmp_phy_config_lane(qphy->tx1, qphy->tx_tbl, qphy->tx_tbl_num, 2);
343 qcom_qmp_phy_config_lane(qphy->tx1, qphy->tx_tbl_sec, qphy->tx_tbl_num_sec, 2);
344 qcom_qmp_phy_config_lane(qphy->rx0, qphy->rx_tbl, qphy->rx_tbl_num, 1);
345 qcom_qmp_phy_config_lane(qphy->rx0, qphy->rx_tbl_sec, qphy->rx_tbl_num_sec, 1);
346 qcom_qmp_phy_config_lane(qphy->rx1, qphy->rx_tbl, qphy->rx_tbl_num, 2);
347 qcom_qmp_phy_config_lane(qphy->rx1, qphy->rx_tbl_sec, qphy->rx_tbl_num_sec, 2);
348 qcom_qmp_phy_configure(qphy->pcs, qphy->pcs_tbl, qphy->pcs_tbl_num);
349 qcom_qmp_phy_configure(qphy->pcs, qphy->pcs_tbl_sec, qphy->pcs_tbl_num_sec);
350 qcom_qmp_phy_configure(qphy->pcs_misc, qphy->pcs_misc_tbl, qphy->pcs_misc_tbl_num);
351 qcom_qmp_phy_configure(qphy->pcs_misc, qphy->pcs_misc_tbl_sec, qphy->pcs_tbl_num_sec);
352
353 /* Release software reset of PCS/Serdes */
354 clrbits32(qphy->pcs + QPHY_SW_RESET, SW_RESET);
355
356 /* start PCS/Serdes to operation mode */
357 write32(qphy->pcs + QPHY_START_CTRL, PCS_START | SERDES_START);
358
359 /*
360 * Wait for PHY initialization to be done
361 * PCS_STATUS: wait for 1ms for PHY STATUS bit to be set
362 */
363 lock_us = wait_us(PCIE_PHY_POLL_US, !(read32(qphy->qmp_phy_base + QPHY_PCS_STATUS) & PHY_STATUS));
364 if (!lock_us) {
365 printk(BIOS_ERR, "PCIe QMP PHY PLL failed to lock in 1ms\n");
366 return CB_ERR;
367 }
368
369 printk(BIOS_DEBUG, "PCIe QPHY Initialized took %lldus\n", lock_us);
370
371 qcom_dw_pcie_enable_pipe_clock();
372
373 return CB_SUCCESS;
374 }
375 #else
376 static void configure_phy_port(pcie_qmp_phy_base_t *port, pcie_qmp_phy_cfg_t *qphy, int lane_base)
377 {
378 qcom_qmp_phy_config_lane(port->tx0, qphy->tx_tbl, qphy->tx_tbl_num, lane_base);
379 qcom_qmp_phy_config_lane(port->rx0, qphy->rx_tbl, qphy->rx_tbl_num, lane_base);
380 qcom_qmp_phy_config_lane(port->tx1, qphy->tx_tbl, qphy->tx_tbl_num, lane_base + 1);
381 qcom_qmp_phy_config_lane(port->rx1, qphy->rx_tbl, qphy->rx_tbl_num, lane_base + 1);
382 qcom_qmp_phy_configure(port->ln_shrd, qphy->ln_shrd_tbl, qphy->ln_shrd_tbl_num);
383 qcom_qmp_phy_configure(port->serdes, qphy->serdes_tbl, qphy->serdes_tbl_num);
384 qcom_qmp_phy_configure(port->pcs, qphy->pcs_tbl, qphy->pcs_tbl_num);
385 qcom_qmp_phy_configure(port->pcs_misc, qphy->pcs_misc_tbl, qphy->pcs_misc_tbl_num);
386 qcom_qmp_phy_configure(port->pcs, qphy->pcs_tbl_sec, qphy->pcs_tbl_num_sec);
387 qcom_qmp_phy_configure(port->pcs_misc, qphy->pcs_misc_tbl_sec, qphy->pcs_misc_tbl_num_sec);
388 qcom_qmp_phy_configure(port->pcs_misc, qphy->pcs_misc_tbl_thrd, qphy->pcs_misc_tbl_num_thrd);
389 qcom_qmp_phy_configure(port->lane0_pcie_pcs, qphy->lane0_pcie_pcs_tbl, qphy->lane0_pcie_pcs_tbl_num);
390 qcom_qmp_phy_configure(port->lane1_pcie_pcs, qphy->lane1_pcie_pcs_tbl, qphy->lane1_pcie_pcs_tbl_num);
391 }
392
393 static enum cb_err qcom_qmp_phy_power_on(pcie_qmp_phy_cfg_t *qphy)
394 {
395 uint64_t lock_us;
396
397 qcom_dw_pcie_enable_pipe_clock();
398
399 /*
400 * The PHY has two independent CSR interfaces that control lanes 0/1 and
401 * lanes 2/3. These are called PortA and PortB.
402 *
403 * In 1x4 mode, the PCS is only used in PortA and PortB
404 * writes are ignored, except for lane-specific registers
405 */
406 /* Select Lane config 0x1 - 1x4 mode 0x0 - 2x2mode */
407 write32(TCSR_PCIE_CTRL_4LN_CONFIG_SEL, 0x1);
408 write32(qphy->porta.serdes + QSERDES_V4_PLL_BIAS_EN_CLKBUFLR_EN, 0x1c);
409 write32(qphy->portb.serdes + QSERDES_V4_PLL_BIAS_EN_CLKBUFLR_EN, 0x1c);
410
411 /* Release powerdown mode and allow endpoint refclk drive */
412 write32(qphy->porta.pcs + QPHY_PCS_PWR_DWN_CNTRL, SW_PWRDN | REFCLK_DRV_DSBL);
413
414 /* Write all PLL/TX/RX/PCS config registers on Port A */
415 configure_phy_port(&qphy->porta, qphy, 1);
416 /* Write all PLL/TX/RX/PCS config registers on Port B */
417 configure_phy_port(&qphy->portb, qphy, 3);
418
419 /* Release software reset of PCS/Serdes */
420 clrbits32(qphy->porta.pcs + QPHY_SW_RESET, SW_RESET);
421
422 /* start PCS/Serdes to operation mode */
423 write32(qphy->porta.pcs + QPHY_START_CTRL, PCS_START | SERDES_START);
424
425 /*
426 * Wait for PHY initialization to be done
427 * PCS_STATUS: wait for 1ms for PHY STATUS bit to be set
428 */
429 lock_us = wait_us(PCIE_PHY_POLL_US, !(read32(qphy->porta.qmp_phy_base + QPHY_PCS_STATUS) & PHY_STATUS));
430 if (!lock_us) {
431 printk(BIOS_ERR, "PCIe QMP PHY PLL failed to lock in 1ms\n");
432 return CB_ERR;
433 }
434
435 printk(BIOS_DEBUG, "PCIe QPHY Initialized took %lldus\n", lock_us);
436 return CB_SUCCESS;
437 }
438 #endif
439
440 /*
441 * Reset the PCIe PHY core.
442 * Allow 100us delay to ensure reset is asserted.
443 */
444 static void qcom_qmp_reset_phy(struct qcom_pcie_cntlr_t *pcie)
445 {
446 clock_reset_bcr(pcie->cntlr_cfg->qmp_phy_bcr, 1);
447 udelay(100);
448 clock_reset_bcr(pcie->cntlr_cfg->qmp_phy_bcr, 0);
449 }
450
451 /*
452 * Reset the PCIe controller core.
453 * Allow 100us delay to ensure reset is asserted.
454 */
455 static void qcom_dw_pcie_reset_core(struct qcom_pcie_cntlr_t *pcie)
456 {
457 clock_reset_bcr(pcie->cntlr_cfg->pcie_bcr, 1);
458 udelay(100);
459 clock_reset_bcr(pcie->cntlr_cfg->pcie_bcr, 0);
460 }
461
462 static enum cb_err qcom_dw_pcie_enable(struct qcom_pcie_cntlr_t *pcie)
463 {
464 int ret;
465
466 pcie_cntlr_cfg_t *pcierc = pcie->cntlr_cfg;
467 pcie_qmp_phy_cfg_t *qmp_phy = pcie->qmp_phy_cfg;
468
469 /* assert PCIe reset link to keep EP in reset */
470 gpio_set(pcierc->perst, 0);
471
472 /* Enable PCIe controller and SoC specific clocks */
473 ret = qcom_dw_pcie_enable_clock();
474 if (ret) {
475 printk(BIOS_ERR, "Enable PCIe clocks failed\n");
476 return ret;
477 }
478
479 /* Reset the controller */
480 qcom_dw_pcie_reset_core(pcie);
481
482 /* configure PCIe to RC mode */
483 write32(pcierc->parf + PCIE_PARF_DEVICE_TYPE, DEVICE_TYPE_RC);
484
485 /* Power on the PHY */
486 clrbits32(pcierc->parf + PCIE_PARF_PHY_CTRL, PHY_PWR_DOWN);
487
488 /* MAC PHY_POWERDOWN MUX DISABLE */
489 clrbits32(pcierc->parf + PCIE_PARF_SYS_CTRL, MAC_PHY_PWRDOWN_MUX_EN);
490
491 /* Bypass MHI as its not needed */
492 setbits32(pcierc->parf + PCIE_PARF_MHI_CLOCK_RESET_CTRL, MHI_BYPASS);
493
494 qcom_qmp_reset_phy(pcie);
495
496 /* Initialize QMP PHY */
497 ret = qcom_qmp_phy_power_on(qmp_phy);
498 if (ret) {
499 printk(BIOS_ERR, "PCIe PHY init failed\n");
500 return ret;
501 }
502
503 qcom_dw_pcie_setup_rc(pcie);
504 #if CONFIG(QMP_PHY_2X2_1X4)
505 post_phy_pwr_up_init(pcie);
506 post_phy_pwr_up_dbi_init(pcie);
507 #endif
508 /* de-assert PCIe reset link to bring EP out of reset */
509 gpio_set(pcierc->perst, 1);
510
511 /* establisth the link */
512 ret = qcom_pcie_dw_link_up(pcie);
513 if (ret) {
514 printk(BIOS_ERR, "PCIe Link init failed\n");
515 return ret;
516 }
517
518 return CB_SUCCESS;
519 }
520
521 /**
522 * Fill coreboot table with PCIe info.
523 * It allows exporting this info to payloads.
524 */
525 enum cb_err fill_lb_pcie(struct lb_pcie *pcie)
526 {
527 if (!mainboard_needs_pcie_init())
528 return CB_ERR_NOT_IMPLEMENTED;
529
530 pcie_cntlr_cfg_t *pcierc = qcom_pcie_cfg.cntlr_cfg;
531 pcie->ctrl_base = (uintptr_t)pcierc->dbi_base;
532 return CB_SUCCESS;
533 }
534
535 /* map_bus function for mapping pcie_s_{read/write}_configXX() functions */
536 volatile union pci_bank *pci_map_bus(pci_devfn_t dev)
537 {
538 void *config_addr = NULL;
539
540 config_addr = qcom_dw_pcie_get_config_addr(&qcom_pcie_cfg, dev);
541 return (void *)config_addr;
542 }
543
544 /* PCI domain ops read_resources callback */
545 void qcom_pci_domain_read_resources(struct device *dev)
546 {
547 struct resource *res;
548 pcie_cntlr_cfg_t *pcierc = qcom_pcie_cfg.cntlr_cfg;
549
550 /* Initialize the system-wide I/O space constraints. */
551 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
552 res->base = pcierc->io.phys_start;
553 res->limit = pcierc->io.size - 1;
554 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
555
556 /* Initialize the system-wide memory resources constraints. */
557 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
558 res->base = pcierc->mem.phys_start;
559 res->limit = pcierc->mem.size - 1;
560 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
561 }
562
563 /* PCI domain ops enable callback */
564 void qcom_setup_pcie_host(struct device *dev)
565 {
566 gcom_pcie_get_config(&qcom_pcie_cfg);
567
568 /* Ensure PCIe endpoints are powered-on before initiating PCIe link training */
569 gcom_pcie_power_on_ep();
570
571 printk(BIOS_INFO, "Setup PCIe in RC mode\n");
572
573 /* Configure PERST gpio */
574 qcom_pcie_configure_gpios(qcom_pcie_cfg.cntlr_cfg);
575
576 if (!qcom_dw_pcie_enable(&qcom_pcie_cfg))
577 printk(BIOS_NOTICE, "PCIe enumerated succussfully..\n");
578 else
579 printk(BIOS_EMERG, "Failed to enable PCIe\n");
580 }