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_INTA BIT(13)
43 #define PCIE_APP_IRN_INTB BIT(14)
44 #define PCIE_APP_IRN_INTC BIT(15)
45 #define PCIE_APP_IRN_INTD BIT(16)
46 #define PCIE_APP_IRN_MSG_LTR BIT(18)
47 #define PCIE_APP_IRN_SYS_ERR_RC BIT(29)
48 #define PCIE_APP_INTX_OFST 12
50 #define PCIE_APP_IRN_INT \
51 (PCIE_APP_IRN_AER_REPORT | PCIE_APP_IRN_PME | \
52 PCIE_APP_IRN_RX_VDM_MSG | PCIE_APP_IRN_SYS_ERR_RC | \
53 PCIE_APP_IRN_PM_TO_ACK | PCIE_APP_IRN_MSG_LTR | \
54 PCIE_APP_IRN_BW_MGT | PCIE_APP_IRN_LINK_AUTO_BW_STAT | \
55 PCIE_APP_IRN_INTA | PCIE_APP_IRN_INTB | \
56 PCIE_APP_IRN_INTC | PCIE_APP_IRN_INTD)
58 #define BUS_IATU_OFFSET SZ_256M
59 #define RESET_INTERVAL_MS 100
63 void __iomem *app_base;
64 struct gpio_desc *reset_gpio;
67 struct reset_control *core_rst;
71 static void pcie_update_bits(void __iomem *base, u32 ofs, u32 mask, u32 val)
75 old = readl(base + ofs);
76 val = (old & ~mask) | (val & mask);
79 writel(val, base + ofs);
82 static inline void pcie_app_wr(struct intel_pcie *pcie, u32 ofs, u32 val)
84 writel(val, pcie->app_base + ofs);
87 static void pcie_app_wr_mask(struct intel_pcie *pcie, u32 ofs,
90 pcie_update_bits(pcie->app_base, ofs, mask, val);
93 static inline u32 pcie_rc_cfg_rd(struct intel_pcie *pcie, u32 ofs)
95 return dw_pcie_readl_dbi(&pcie->pci, ofs);
98 static inline void pcie_rc_cfg_wr(struct intel_pcie *pcie, u32 ofs, u32 val)
100 dw_pcie_writel_dbi(&pcie->pci, ofs, val);
103 static void pcie_rc_cfg_wr_mask(struct intel_pcie *pcie, u32 ofs,
106 pcie_update_bits(pcie->pci.dbi_base, ofs, mask, val);
109 static void intel_pcie_ltssm_enable(struct intel_pcie *pcie)
111 pcie_app_wr_mask(pcie, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE,
112 PCIE_APP_CCR_LTSSM_ENABLE);
115 static void intel_pcie_ltssm_disable(struct intel_pcie *pcie)
117 pcie_app_wr_mask(pcie, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE, 0);
120 static void intel_pcie_link_setup(struct intel_pcie *pcie)
123 u8 offset = dw_pcie_find_capability(&pcie->pci, PCI_CAP_ID_EXP);
125 val = pcie_rc_cfg_rd(pcie, offset + PCI_EXP_LNKCTL);
127 val &= ~(PCI_EXP_LNKCTL_LD | PCI_EXP_LNKCTL_ASPMC);
128 pcie_rc_cfg_wr(pcie, offset + PCI_EXP_LNKCTL, val);
131 static void intel_pcie_init_n_fts(struct dw_pcie *pci)
133 switch (pci->link_gen) {
135 pci->n_fts[1] = PORT_AFR_N_FTS_GEN3;
138 pci->n_fts[1] = PORT_AFR_N_FTS_GEN4;
141 pci->n_fts[1] = PORT_AFR_N_FTS_GEN12_DFT;
144 pci->n_fts[0] = PORT_AFR_N_FTS_GEN12_DFT;
147 static int intel_pcie_ep_rst_init(struct intel_pcie *pcie)
149 struct device *dev = pcie->pci.dev;
152 pcie->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
153 if (IS_ERR(pcie->reset_gpio)) {
154 ret = PTR_ERR(pcie->reset_gpio);
155 if (ret != -EPROBE_DEFER)
156 dev_err(dev, "Failed to request PCIe GPIO: %d\n", ret);
160 /* Make initial reset last for 100us */
161 usleep_range(100, 200);
166 static void intel_pcie_core_rst_assert(struct intel_pcie *pcie)
168 reset_control_assert(pcie->core_rst);
171 static void intel_pcie_core_rst_deassert(struct intel_pcie *pcie)
174 * One micro-second delay to make sure the reset pulse
175 * wide enough so that core reset is clean.
178 reset_control_deassert(pcie->core_rst);
181 * Some SoC core reset also reset PHY, more delay needed
182 * to make sure the reset process is done.
184 usleep_range(1000, 2000);
187 static void intel_pcie_device_rst_assert(struct intel_pcie *pcie)
189 gpiod_set_value_cansleep(pcie->reset_gpio, 1);
192 static void intel_pcie_device_rst_deassert(struct intel_pcie *pcie)
194 msleep(pcie->rst_intrvl);
195 gpiod_set_value_cansleep(pcie->reset_gpio, 0);
198 static void intel_pcie_core_irq_disable(struct intel_pcie *pcie)
200 pcie_app_wr(pcie, PCIE_APP_IRNEN, 0);
201 pcie_app_wr(pcie, PCIE_APP_IRNCR, PCIE_APP_IRN_INT);
204 static int intel_pcie_get_resources(struct platform_device *pdev)
206 struct intel_pcie *pcie = platform_get_drvdata(pdev);
207 struct dw_pcie *pci = &pcie->pci;
208 struct device *dev = pci->dev;
211 pcie->core_clk = devm_clk_get(dev, NULL);
212 if (IS_ERR(pcie->core_clk)) {
213 ret = PTR_ERR(pcie->core_clk);
214 if (ret != -EPROBE_DEFER)
215 dev_err(dev, "Failed to get clks: %d\n", ret);
219 pcie->core_rst = devm_reset_control_get(dev, NULL);
220 if (IS_ERR(pcie->core_rst)) {
221 ret = PTR_ERR(pcie->core_rst);
222 if (ret != -EPROBE_DEFER)
223 dev_err(dev, "Failed to get resets: %d\n", ret);
227 ret = device_property_read_u32(dev, "reset-assert-ms",
230 pcie->rst_intrvl = RESET_INTERVAL_MS;
232 pcie->app_base = devm_platform_ioremap_resource_byname(pdev, "app");
233 if (IS_ERR(pcie->app_base))
234 return PTR_ERR(pcie->app_base);
236 pcie->phy = devm_phy_get(dev, "pcie");
237 if (IS_ERR(pcie->phy)) {
238 ret = PTR_ERR(pcie->phy);
239 if (ret != -EPROBE_DEFER)
240 dev_err(dev, "Couldn't get pcie-phy: %d\n", ret);
247 static int intel_pcie_wait_l2(struct intel_pcie *pcie)
251 struct dw_pcie *pci = &pcie->pci;
253 if (pci->link_gen < 3)
256 /* Send PME_TURN_OFF message */
257 pcie_app_wr_mask(pcie, PCIE_APP_MSG_CR, PCIE_APP_MSG_XMT_PM_TURNOFF,
258 PCIE_APP_MSG_XMT_PM_TURNOFF);
260 /* Read PMC status and wait for falling into L2 link state */
261 ret = readl_poll_timeout(pcie->app_base + PCIE_APP_PMC, value,
262 value & PCIE_APP_PMC_IN_L2, 20,
263 jiffies_to_usecs(5 * HZ));
265 dev_err(pcie->pci.dev, "PCIe link enter L2 timeout!\n");
270 static void intel_pcie_turn_off(struct intel_pcie *pcie)
272 if (dw_pcie_link_up(&pcie->pci))
273 intel_pcie_wait_l2(pcie);
275 /* Put endpoint device in reset state */
276 intel_pcie_device_rst_assert(pcie);
277 pcie_rc_cfg_wr_mask(pcie, PCI_COMMAND, PCI_COMMAND_MEMORY, 0);
280 static int intel_pcie_host_setup(struct intel_pcie *pcie)
283 struct dw_pcie *pci = &pcie->pci;
285 intel_pcie_core_rst_assert(pcie);
286 intel_pcie_device_rst_assert(pcie);
288 ret = phy_init(pcie->phy);
292 intel_pcie_core_rst_deassert(pcie);
294 ret = clk_prepare_enable(pcie->core_clk);
296 dev_err(pcie->pci.dev, "Core clock enable failed: %d\n", ret);
300 pci->atu_base = pci->dbi_base + 0xC0000;
302 intel_pcie_ltssm_disable(pcie);
303 intel_pcie_link_setup(pcie);
304 intel_pcie_init_n_fts(pci);
306 ret = dw_pcie_setup_rc(&pci->pp);
310 dw_pcie_upconfig_setup(pci);
312 intel_pcie_device_rst_deassert(pcie);
313 intel_pcie_ltssm_enable(pcie);
315 ret = dw_pcie_wait_for_link(pci);
319 /* Enable integrated interrupts */
320 pcie_app_wr_mask(pcie, PCIE_APP_IRNEN, PCIE_APP_IRN_INT,
326 clk_disable_unprepare(pcie->core_clk);
328 intel_pcie_core_rst_assert(pcie);
334 static void __intel_pcie_remove(struct intel_pcie *pcie)
336 intel_pcie_core_irq_disable(pcie);
337 intel_pcie_turn_off(pcie);
338 clk_disable_unprepare(pcie->core_clk);
339 intel_pcie_core_rst_assert(pcie);
343 static int intel_pcie_remove(struct platform_device *pdev)
345 struct intel_pcie *pcie = platform_get_drvdata(pdev);
346 struct dw_pcie_rp *pp = &pcie->pci.pp;
348 dw_pcie_host_deinit(pp);
349 __intel_pcie_remove(pcie);
354 static int intel_pcie_suspend_noirq(struct device *dev)
356 struct intel_pcie *pcie = dev_get_drvdata(dev);
359 intel_pcie_core_irq_disable(pcie);
360 ret = intel_pcie_wait_l2(pcie);
365 clk_disable_unprepare(pcie->core_clk);
369 static int intel_pcie_resume_noirq(struct device *dev)
371 struct intel_pcie *pcie = dev_get_drvdata(dev);
373 return intel_pcie_host_setup(pcie);
376 static int intel_pcie_rc_init(struct dw_pcie_rp *pp)
378 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
379 struct intel_pcie *pcie = dev_get_drvdata(pci->dev);
381 return intel_pcie_host_setup(pcie);
384 static u64 intel_pcie_cpu_addr(struct dw_pcie *pcie, u64 cpu_addr)
386 return cpu_addr + BUS_IATU_OFFSET;
389 static const struct dw_pcie_ops intel_pcie_ops = {
390 .cpu_addr_fixup = intel_pcie_cpu_addr,
393 static const struct dw_pcie_host_ops intel_pcie_dw_ops = {
394 .host_init = intel_pcie_rc_init,
397 static int intel_pcie_probe(struct platform_device *pdev)
399 struct device *dev = &pdev->dev;
400 struct intel_pcie *pcie;
401 struct dw_pcie_rp *pp;
405 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
409 platform_set_drvdata(pdev, pcie);
414 ret = intel_pcie_get_resources(pdev);
418 ret = intel_pcie_ep_rst_init(pcie);
422 pci->ops = &intel_pcie_ops;
423 pp->ops = &intel_pcie_dw_ops;
425 ret = dw_pcie_host_init(pp);
427 dev_err(dev, "Cannot initialize host\n");
434 static const struct dev_pm_ops intel_pcie_pm_ops = {
435 NOIRQ_SYSTEM_SLEEP_PM_OPS(intel_pcie_suspend_noirq,
436 intel_pcie_resume_noirq)
439 static const struct of_device_id of_intel_pcie_match[] = {
440 { .compatible = "intel,lgm-pcie" },
444 static struct platform_driver intel_pcie_driver = {
445 .probe = intel_pcie_probe,
446 .remove = intel_pcie_remove,
448 .name = "intel-gw-pcie",
449 .of_match_table = of_intel_pcie_match,
450 .pm = &intel_pcie_pm_ops,
453 builtin_platform_driver(intel_pcie_driver);