1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for Intel Gateway SoCs
5 * Copyright (c) 2019 Intel Corporation.
8 #include <linux/bitfield.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/iopoll.h>
12 #include <linux/pci_regs.h>
13 #include <linux/phy/phy.h>
14 #include <linux/platform_device.h>
15 #include <linux/reset.h>
17 #include "../../pci.h"
18 #include "pcie-designware.h"
20 #define PORT_AFR_N_FTS_GEN12_DFT (SZ_128 - 1)
21 #define PORT_AFR_N_FTS_GEN3 180
22 #define PORT_AFR_N_FTS_GEN4 196
24 /* PCIe Application logic Registers */
25 #define PCIE_APP_CCR 0x10
26 #define PCIE_APP_CCR_LTSSM_ENABLE BIT(0)
28 #define PCIE_APP_MSG_CR 0x30
29 #define PCIE_APP_MSG_XMT_PM_TURNOFF BIT(0)
31 #define PCIE_APP_PMC 0x44
32 #define PCIE_APP_PMC_IN_L2 BIT(20)
34 #define PCIE_APP_IRNEN 0xF4
35 #define PCIE_APP_IRNCR 0xF8
36 #define PCIE_APP_IRN_AER_REPORT BIT(0)
37 #define PCIE_APP_IRN_PME BIT(2)
38 #define PCIE_APP_IRN_RX_VDM_MSG BIT(4)
39 #define PCIE_APP_IRN_PM_TO_ACK BIT(9)
40 #define PCIE_APP_IRN_LINK_AUTO_BW_STAT BIT(11)
41 #define PCIE_APP_IRN_BW_MGT BIT(12)
42 #define PCIE_APP_IRN_MSG_LTR BIT(18)
43 #define PCIE_APP_IRN_SYS_ERR_RC BIT(29)
44 #define PCIE_APP_INTX_OFST 12
46 #define PCIE_APP_IRN_INT \
47 (PCIE_APP_IRN_AER_REPORT | PCIE_APP_IRN_PME | \
48 PCIE_APP_IRN_RX_VDM_MSG | PCIE_APP_IRN_SYS_ERR_RC | \
49 PCIE_APP_IRN_PM_TO_ACK | PCIE_APP_IRN_MSG_LTR | \
50 PCIE_APP_IRN_BW_MGT | PCIE_APP_IRN_LINK_AUTO_BW_STAT | \
51 (PCIE_APP_INTX_OFST + PCI_INTERRUPT_INTA) | \
52 (PCIE_APP_INTX_OFST + PCI_INTERRUPT_INTB) | \
53 (PCIE_APP_INTX_OFST + PCI_INTERRUPT_INTC) | \
54 (PCIE_APP_INTX_OFST + PCI_INTERRUPT_INTD))
56 #define BUS_IATU_OFFSET SZ_256M
57 #define RESET_INTERVAL_MS 100
59 struct intel_pcie_soc {
60 unsigned int pcie_ver;
61 unsigned int pcie_atu_offset;
65 struct intel_pcie_port {
67 void __iomem *app_base;
68 struct gpio_desc *reset_gpio;
75 struct reset_control *core_rst;
80 static void pcie_update_bits(void __iomem *base, u32 ofs, u32 mask, u32 val)
84 old = readl(base + ofs);
85 val = (old & ~mask) | (val & mask);
88 writel(val, base + ofs);
91 static inline u32 pcie_app_rd(struct intel_pcie_port *lpp, u32 ofs)
93 return readl(lpp->app_base + ofs);
96 static inline void pcie_app_wr(struct intel_pcie_port *lpp, u32 ofs, u32 val)
98 writel(val, lpp->app_base + ofs);
101 static void pcie_app_wr_mask(struct intel_pcie_port *lpp, u32 ofs,
104 pcie_update_bits(lpp->app_base, ofs, mask, val);
107 static inline u32 pcie_rc_cfg_rd(struct intel_pcie_port *lpp, u32 ofs)
109 return dw_pcie_readl_dbi(&lpp->pci, ofs);
112 static inline void pcie_rc_cfg_wr(struct intel_pcie_port *lpp, u32 ofs, u32 val)
114 dw_pcie_writel_dbi(&lpp->pci, ofs, val);
117 static void pcie_rc_cfg_wr_mask(struct intel_pcie_port *lpp, u32 ofs,
120 pcie_update_bits(lpp->pci.dbi_base, ofs, mask, val);
123 static void intel_pcie_ltssm_enable(struct intel_pcie_port *lpp)
125 pcie_app_wr_mask(lpp, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE,
126 PCIE_APP_CCR_LTSSM_ENABLE);
129 static void intel_pcie_ltssm_disable(struct intel_pcie_port *lpp)
131 pcie_app_wr_mask(lpp, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE, 0);
134 static void intel_pcie_link_setup(struct intel_pcie_port *lpp)
137 u8 offset = lpp->pcie_cap_ofst;
139 val = pcie_rc_cfg_rd(lpp, offset + PCI_EXP_LNKCAP);
140 lpp->max_speed = FIELD_GET(PCI_EXP_LNKCAP_SLS, val);
141 lpp->max_width = FIELD_GET(PCI_EXP_LNKCAP_MLW, val);
143 val = pcie_rc_cfg_rd(lpp, offset + PCI_EXP_LNKCTL);
145 val &= ~(PCI_EXP_LNKCTL_LD | PCI_EXP_LNKCTL_ASPMC);
146 pcie_rc_cfg_wr(lpp, offset + PCI_EXP_LNKCTL, val);
149 static void intel_pcie_port_logic_setup(struct intel_pcie_port *lpp)
153 switch (pcie_link_speed[lpp->max_speed]) {
154 case PCIE_SPEED_8_0GT:
155 lpp->n_fts = PORT_AFR_N_FTS_GEN3;
157 case PCIE_SPEED_16_0GT:
158 lpp->n_fts = PORT_AFR_N_FTS_GEN4;
161 lpp->n_fts = PORT_AFR_N_FTS_GEN12_DFT;
165 mask = PORT_AFR_N_FTS_MASK | PORT_AFR_CC_N_FTS_MASK;
166 val = FIELD_PREP(PORT_AFR_N_FTS_MASK, lpp->n_fts) |
167 FIELD_PREP(PORT_AFR_CC_N_FTS_MASK, lpp->n_fts);
168 pcie_rc_cfg_wr_mask(lpp, PCIE_PORT_AFR, mask, val);
170 /* Port Link Control Register */
171 pcie_rc_cfg_wr_mask(lpp, PCIE_PORT_LINK_CONTROL, PORT_LINK_DLL_LINK_EN,
172 PORT_LINK_DLL_LINK_EN);
175 static void intel_pcie_rc_setup(struct intel_pcie_port *lpp)
177 intel_pcie_ltssm_disable(lpp);
178 intel_pcie_link_setup(lpp);
179 dw_pcie_setup_rc(&lpp->pci.pp);
180 dw_pcie_upconfig_setup(&lpp->pci);
181 intel_pcie_port_logic_setup(lpp);
182 dw_pcie_link_set_max_speed(&lpp->pci, lpp->link_gen);
183 dw_pcie_link_set_n_fts(&lpp->pci, lpp->n_fts);
186 static int intel_pcie_ep_rst_init(struct intel_pcie_port *lpp)
188 struct device *dev = lpp->pci.dev;
191 lpp->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
192 if (IS_ERR(lpp->reset_gpio)) {
193 ret = PTR_ERR(lpp->reset_gpio);
194 if (ret != -EPROBE_DEFER)
195 dev_err(dev, "Failed to request PCIe GPIO: %d\n", ret);
199 /* Make initial reset last for 100us */
200 usleep_range(100, 200);
205 static void intel_pcie_core_rst_assert(struct intel_pcie_port *lpp)
207 reset_control_assert(lpp->core_rst);
210 static void intel_pcie_core_rst_deassert(struct intel_pcie_port *lpp)
213 * One micro-second delay to make sure the reset pulse
214 * wide enough so that core reset is clean.
217 reset_control_deassert(lpp->core_rst);
220 * Some SoC core reset also reset PHY, more delay needed
221 * to make sure the reset process is done.
223 usleep_range(1000, 2000);
226 static void intel_pcie_device_rst_assert(struct intel_pcie_port *lpp)
228 gpiod_set_value_cansleep(lpp->reset_gpio, 1);
231 static void intel_pcie_device_rst_deassert(struct intel_pcie_port *lpp)
233 msleep(lpp->rst_intrvl);
234 gpiod_set_value_cansleep(lpp->reset_gpio, 0);
237 static int intel_pcie_app_logic_setup(struct intel_pcie_port *lpp)
239 intel_pcie_device_rst_deassert(lpp);
240 intel_pcie_ltssm_enable(lpp);
242 return dw_pcie_wait_for_link(&lpp->pci);
245 static void intel_pcie_core_irq_disable(struct intel_pcie_port *lpp)
247 pcie_app_wr(lpp, PCIE_APP_IRNEN, 0);
248 pcie_app_wr(lpp, PCIE_APP_IRNCR, PCIE_APP_IRN_INT);
251 static int intel_pcie_get_resources(struct platform_device *pdev)
253 struct intel_pcie_port *lpp = platform_get_drvdata(pdev);
254 struct dw_pcie *pci = &lpp->pci;
255 struct device *dev = pci->dev;
258 pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "dbi");
259 if (IS_ERR(pci->dbi_base))
260 return PTR_ERR(pci->dbi_base);
262 lpp->core_clk = devm_clk_get(dev, NULL);
263 if (IS_ERR(lpp->core_clk)) {
264 ret = PTR_ERR(lpp->core_clk);
265 if (ret != -EPROBE_DEFER)
266 dev_err(dev, "Failed to get clks: %d\n", ret);
270 lpp->core_rst = devm_reset_control_get(dev, NULL);
271 if (IS_ERR(lpp->core_rst)) {
272 ret = PTR_ERR(lpp->core_rst);
273 if (ret != -EPROBE_DEFER)
274 dev_err(dev, "Failed to get resets: %d\n", ret);
278 ret = device_property_match_string(dev, "device_type", "pci");
280 dev_err(dev, "Failed to find pci device type: %d\n", ret);
284 ret = device_property_read_u32(dev, "reset-assert-ms",
287 lpp->rst_intrvl = RESET_INTERVAL_MS;
289 ret = of_pci_get_max_link_speed(dev->of_node);
290 lpp->link_gen = ret < 0 ? 0 : ret;
292 lpp->app_base = devm_platform_ioremap_resource_byname(pdev, "app");
293 if (IS_ERR(lpp->app_base))
294 return PTR_ERR(lpp->app_base);
296 lpp->phy = devm_phy_get(dev, "pcie");
297 if (IS_ERR(lpp->phy)) {
298 ret = PTR_ERR(lpp->phy);
299 if (ret != -EPROBE_DEFER)
300 dev_err(dev, "Couldn't get pcie-phy: %d\n", ret);
307 static void intel_pcie_deinit_phy(struct intel_pcie_port *lpp)
312 static int intel_pcie_wait_l2(struct intel_pcie_port *lpp)
317 if (pcie_link_speed[lpp->max_speed] < PCIE_SPEED_8_0GT)
320 /* Send PME_TURN_OFF message */
321 pcie_app_wr_mask(lpp, PCIE_APP_MSG_CR, PCIE_APP_MSG_XMT_PM_TURNOFF,
322 PCIE_APP_MSG_XMT_PM_TURNOFF);
324 /* Read PMC status and wait for falling into L2 link state */
325 ret = readl_poll_timeout(lpp->app_base + PCIE_APP_PMC, value,
326 value & PCIE_APP_PMC_IN_L2, 20,
327 jiffies_to_usecs(5 * HZ));
329 dev_err(lpp->pci.dev, "PCIe link enter L2 timeout!\n");
334 static void intel_pcie_turn_off(struct intel_pcie_port *lpp)
336 if (dw_pcie_link_up(&lpp->pci))
337 intel_pcie_wait_l2(lpp);
339 /* Put endpoint device in reset state */
340 intel_pcie_device_rst_assert(lpp);
341 pcie_rc_cfg_wr_mask(lpp, PCI_COMMAND, PCI_COMMAND_MEMORY, 0);
344 static int intel_pcie_host_setup(struct intel_pcie_port *lpp)
346 struct device *dev = lpp->pci.dev;
349 intel_pcie_core_rst_assert(lpp);
350 intel_pcie_device_rst_assert(lpp);
352 ret = phy_init(lpp->phy);
356 intel_pcie_core_rst_deassert(lpp);
358 ret = clk_prepare_enable(lpp->core_clk);
360 dev_err(lpp->pci.dev, "Core clock enable failed: %d\n", ret);
364 if (!lpp->pcie_cap_ofst) {
365 ret = dw_pcie_find_capability(&lpp->pci, PCI_CAP_ID_EXP);
368 dev_err(dev, "Invalid PCIe capability offset\n");
372 lpp->pcie_cap_ofst = ret;
375 intel_pcie_rc_setup(lpp);
376 ret = intel_pcie_app_logic_setup(lpp);
380 /* Enable integrated interrupts */
381 pcie_app_wr_mask(lpp, PCIE_APP_IRNEN, PCIE_APP_IRN_INT,
387 clk_disable_unprepare(lpp->core_clk);
389 intel_pcie_core_rst_assert(lpp);
390 intel_pcie_deinit_phy(lpp);
395 static void __intel_pcie_remove(struct intel_pcie_port *lpp)
397 intel_pcie_core_irq_disable(lpp);
398 intel_pcie_turn_off(lpp);
399 clk_disable_unprepare(lpp->core_clk);
400 intel_pcie_core_rst_assert(lpp);
401 intel_pcie_deinit_phy(lpp);
404 static int intel_pcie_remove(struct platform_device *pdev)
406 struct intel_pcie_port *lpp = platform_get_drvdata(pdev);
407 struct pcie_port *pp = &lpp->pci.pp;
409 dw_pcie_host_deinit(pp);
410 __intel_pcie_remove(lpp);
415 static int __maybe_unused intel_pcie_suspend_noirq(struct device *dev)
417 struct intel_pcie_port *lpp = dev_get_drvdata(dev);
420 intel_pcie_core_irq_disable(lpp);
421 ret = intel_pcie_wait_l2(lpp);
425 intel_pcie_deinit_phy(lpp);
426 clk_disable_unprepare(lpp->core_clk);
430 static int __maybe_unused intel_pcie_resume_noirq(struct device *dev)
432 struct intel_pcie_port *lpp = dev_get_drvdata(dev);
434 return intel_pcie_host_setup(lpp);
437 static int intel_pcie_rc_init(struct pcie_port *pp)
439 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
440 struct intel_pcie_port *lpp = dev_get_drvdata(pci->dev);
442 return intel_pcie_host_setup(lpp);
446 * Dummy function so that DW core doesn't configure MSI
448 static int intel_pcie_msi_init(struct pcie_port *pp)
453 static u64 intel_pcie_cpu_addr(struct dw_pcie *pcie, u64 cpu_addr)
455 return cpu_addr + BUS_IATU_OFFSET;
458 static const struct dw_pcie_ops intel_pcie_ops = {
459 .cpu_addr_fixup = intel_pcie_cpu_addr,
462 static const struct dw_pcie_host_ops intel_pcie_dw_ops = {
463 .host_init = intel_pcie_rc_init,
464 .msi_host_init = intel_pcie_msi_init,
467 static const struct intel_pcie_soc pcie_data = {
469 .pcie_atu_offset = 0xC0000,
473 static int intel_pcie_probe(struct platform_device *pdev)
475 const struct intel_pcie_soc *data;
476 struct device *dev = &pdev->dev;
477 struct intel_pcie_port *lpp;
478 struct pcie_port *pp;
482 lpp = devm_kzalloc(dev, sizeof(*lpp), GFP_KERNEL);
486 platform_set_drvdata(pdev, lpp);
491 ret = intel_pcie_get_resources(pdev);
495 ret = intel_pcie_ep_rst_init(lpp);
499 data = device_get_match_data(dev);
503 pci->ops = &intel_pcie_ops;
504 pci->version = data->pcie_ver;
505 pci->atu_base = pci->dbi_base + data->pcie_atu_offset;
506 pp->ops = &intel_pcie_dw_ops;
508 ret = dw_pcie_host_init(pp);
510 dev_err(dev, "Cannot initialize host\n");
515 * Intel PCIe doesn't configure IO region, so set viewport
516 * to not perform IO region access.
518 pci->num_viewport = data->num_viewport;
523 static const struct dev_pm_ops intel_pcie_pm_ops = {
524 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(intel_pcie_suspend_noirq,
525 intel_pcie_resume_noirq)
528 static const struct of_device_id of_intel_pcie_match[] = {
529 { .compatible = "intel,lgm-pcie", .data = &pcie_data },
533 static struct platform_driver intel_pcie_driver = {
534 .probe = intel_pcie_probe,
535 .remove = intel_pcie_remove,
537 .name = "intel-gw-pcie",
538 .of_match_table = of_intel_pcie_match,
539 .pm = &intel_pcie_pm_ops,
542 builtin_platform_driver(intel_pcie_driver);