0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/bitfield.h>
0009 #include <linux/clk.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/iopoll.h>
0012 #include <linux/pci_regs.h>
0013 #include <linux/phy/phy.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/reset.h>
0016
0017 #include "../../pci.h"
0018 #include "pcie-designware.h"
0019
0020 #define PORT_AFR_N_FTS_GEN12_DFT (SZ_128 - 1)
0021 #define PORT_AFR_N_FTS_GEN3 180
0022 #define PORT_AFR_N_FTS_GEN4 196
0023
0024
0025 #define PCIE_APP_CCR 0x10
0026 #define PCIE_APP_CCR_LTSSM_ENABLE BIT(0)
0027
0028 #define PCIE_APP_MSG_CR 0x30
0029 #define PCIE_APP_MSG_XMT_PM_TURNOFF BIT(0)
0030
0031 #define PCIE_APP_PMC 0x44
0032 #define PCIE_APP_PMC_IN_L2 BIT(20)
0033
0034 #define PCIE_APP_IRNEN 0xF4
0035 #define PCIE_APP_IRNCR 0xF8
0036 #define PCIE_APP_IRN_AER_REPORT BIT(0)
0037 #define PCIE_APP_IRN_PME BIT(2)
0038 #define PCIE_APP_IRN_RX_VDM_MSG BIT(4)
0039 #define PCIE_APP_IRN_PM_TO_ACK BIT(9)
0040 #define PCIE_APP_IRN_LINK_AUTO_BW_STAT BIT(11)
0041 #define PCIE_APP_IRN_BW_MGT BIT(12)
0042 #define PCIE_APP_IRN_INTA BIT(13)
0043 #define PCIE_APP_IRN_INTB BIT(14)
0044 #define PCIE_APP_IRN_INTC BIT(15)
0045 #define PCIE_APP_IRN_INTD BIT(16)
0046 #define PCIE_APP_IRN_MSG_LTR BIT(18)
0047 #define PCIE_APP_IRN_SYS_ERR_RC BIT(29)
0048 #define PCIE_APP_INTX_OFST 12
0049
0050 #define PCIE_APP_IRN_INT \
0051 (PCIE_APP_IRN_AER_REPORT | PCIE_APP_IRN_PME | \
0052 PCIE_APP_IRN_RX_VDM_MSG | PCIE_APP_IRN_SYS_ERR_RC | \
0053 PCIE_APP_IRN_PM_TO_ACK | PCIE_APP_IRN_MSG_LTR | \
0054 PCIE_APP_IRN_BW_MGT | PCIE_APP_IRN_LINK_AUTO_BW_STAT | \
0055 PCIE_APP_IRN_INTA | PCIE_APP_IRN_INTB | \
0056 PCIE_APP_IRN_INTC | PCIE_APP_IRN_INTD)
0057
0058 #define BUS_IATU_OFFSET SZ_256M
0059 #define RESET_INTERVAL_MS 100
0060
0061 struct intel_pcie {
0062 struct dw_pcie pci;
0063 void __iomem *app_base;
0064 struct gpio_desc *reset_gpio;
0065 u32 rst_intrvl;
0066 struct clk *core_clk;
0067 struct reset_control *core_rst;
0068 struct phy *phy;
0069 };
0070
0071 static void pcie_update_bits(void __iomem *base, u32 ofs, u32 mask, u32 val)
0072 {
0073 u32 old;
0074
0075 old = readl(base + ofs);
0076 val = (old & ~mask) | (val & mask);
0077
0078 if (val != old)
0079 writel(val, base + ofs);
0080 }
0081
0082 static inline void pcie_app_wr(struct intel_pcie *pcie, u32 ofs, u32 val)
0083 {
0084 writel(val, pcie->app_base + ofs);
0085 }
0086
0087 static void pcie_app_wr_mask(struct intel_pcie *pcie, u32 ofs,
0088 u32 mask, u32 val)
0089 {
0090 pcie_update_bits(pcie->app_base, ofs, mask, val);
0091 }
0092
0093 static inline u32 pcie_rc_cfg_rd(struct intel_pcie *pcie, u32 ofs)
0094 {
0095 return dw_pcie_readl_dbi(&pcie->pci, ofs);
0096 }
0097
0098 static inline void pcie_rc_cfg_wr(struct intel_pcie *pcie, u32 ofs, u32 val)
0099 {
0100 dw_pcie_writel_dbi(&pcie->pci, ofs, val);
0101 }
0102
0103 static void pcie_rc_cfg_wr_mask(struct intel_pcie *pcie, u32 ofs,
0104 u32 mask, u32 val)
0105 {
0106 pcie_update_bits(pcie->pci.dbi_base, ofs, mask, val);
0107 }
0108
0109 static void intel_pcie_ltssm_enable(struct intel_pcie *pcie)
0110 {
0111 pcie_app_wr_mask(pcie, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE,
0112 PCIE_APP_CCR_LTSSM_ENABLE);
0113 }
0114
0115 static void intel_pcie_ltssm_disable(struct intel_pcie *pcie)
0116 {
0117 pcie_app_wr_mask(pcie, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE, 0);
0118 }
0119
0120 static void intel_pcie_link_setup(struct intel_pcie *pcie)
0121 {
0122 u32 val;
0123 u8 offset = dw_pcie_find_capability(&pcie->pci, PCI_CAP_ID_EXP);
0124
0125 val = pcie_rc_cfg_rd(pcie, offset + PCI_EXP_LNKCTL);
0126
0127 val &= ~(PCI_EXP_LNKCTL_LD | PCI_EXP_LNKCTL_ASPMC);
0128 pcie_rc_cfg_wr(pcie, offset + PCI_EXP_LNKCTL, val);
0129 }
0130
0131 static void intel_pcie_init_n_fts(struct dw_pcie *pci)
0132 {
0133 switch (pci->link_gen) {
0134 case 3:
0135 pci->n_fts[1] = PORT_AFR_N_FTS_GEN3;
0136 break;
0137 case 4:
0138 pci->n_fts[1] = PORT_AFR_N_FTS_GEN4;
0139 break;
0140 default:
0141 pci->n_fts[1] = PORT_AFR_N_FTS_GEN12_DFT;
0142 break;
0143 }
0144 pci->n_fts[0] = PORT_AFR_N_FTS_GEN12_DFT;
0145 }
0146
0147 static int intel_pcie_ep_rst_init(struct intel_pcie *pcie)
0148 {
0149 struct device *dev = pcie->pci.dev;
0150 int ret;
0151
0152 pcie->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
0153 if (IS_ERR(pcie->reset_gpio)) {
0154 ret = PTR_ERR(pcie->reset_gpio);
0155 if (ret != -EPROBE_DEFER)
0156 dev_err(dev, "Failed to request PCIe GPIO: %d\n", ret);
0157 return ret;
0158 }
0159
0160
0161 usleep_range(100, 200);
0162
0163 return 0;
0164 }
0165
0166 static void intel_pcie_core_rst_assert(struct intel_pcie *pcie)
0167 {
0168 reset_control_assert(pcie->core_rst);
0169 }
0170
0171 static void intel_pcie_core_rst_deassert(struct intel_pcie *pcie)
0172 {
0173
0174
0175
0176
0177 udelay(1);
0178 reset_control_deassert(pcie->core_rst);
0179
0180
0181
0182
0183
0184 usleep_range(1000, 2000);
0185 }
0186
0187 static void intel_pcie_device_rst_assert(struct intel_pcie *pcie)
0188 {
0189 gpiod_set_value_cansleep(pcie->reset_gpio, 1);
0190 }
0191
0192 static void intel_pcie_device_rst_deassert(struct intel_pcie *pcie)
0193 {
0194 msleep(pcie->rst_intrvl);
0195 gpiod_set_value_cansleep(pcie->reset_gpio, 0);
0196 }
0197
0198 static void intel_pcie_core_irq_disable(struct intel_pcie *pcie)
0199 {
0200 pcie_app_wr(pcie, PCIE_APP_IRNEN, 0);
0201 pcie_app_wr(pcie, PCIE_APP_IRNCR, PCIE_APP_IRN_INT);
0202 }
0203
0204 static int intel_pcie_get_resources(struct platform_device *pdev)
0205 {
0206 struct intel_pcie *pcie = platform_get_drvdata(pdev);
0207 struct dw_pcie *pci = &pcie->pci;
0208 struct device *dev = pci->dev;
0209 int ret;
0210
0211 pcie->core_clk = devm_clk_get(dev, NULL);
0212 if (IS_ERR(pcie->core_clk)) {
0213 ret = PTR_ERR(pcie->core_clk);
0214 if (ret != -EPROBE_DEFER)
0215 dev_err(dev, "Failed to get clks: %d\n", ret);
0216 return ret;
0217 }
0218
0219 pcie->core_rst = devm_reset_control_get(dev, NULL);
0220 if (IS_ERR(pcie->core_rst)) {
0221 ret = PTR_ERR(pcie->core_rst);
0222 if (ret != -EPROBE_DEFER)
0223 dev_err(dev, "Failed to get resets: %d\n", ret);
0224 return ret;
0225 }
0226
0227 ret = device_property_read_u32(dev, "reset-assert-ms",
0228 &pcie->rst_intrvl);
0229 if (ret)
0230 pcie->rst_intrvl = RESET_INTERVAL_MS;
0231
0232 pcie->app_base = devm_platform_ioremap_resource_byname(pdev, "app");
0233 if (IS_ERR(pcie->app_base))
0234 return PTR_ERR(pcie->app_base);
0235
0236 pcie->phy = devm_phy_get(dev, "pcie");
0237 if (IS_ERR(pcie->phy)) {
0238 ret = PTR_ERR(pcie->phy);
0239 if (ret != -EPROBE_DEFER)
0240 dev_err(dev, "Couldn't get pcie-phy: %d\n", ret);
0241 return ret;
0242 }
0243
0244 return 0;
0245 }
0246
0247 static int intel_pcie_wait_l2(struct intel_pcie *pcie)
0248 {
0249 u32 value;
0250 int ret;
0251 struct dw_pcie *pci = &pcie->pci;
0252
0253 if (pci->link_gen < 3)
0254 return 0;
0255
0256
0257 pcie_app_wr_mask(pcie, PCIE_APP_MSG_CR, PCIE_APP_MSG_XMT_PM_TURNOFF,
0258 PCIE_APP_MSG_XMT_PM_TURNOFF);
0259
0260
0261 ret = readl_poll_timeout(pcie->app_base + PCIE_APP_PMC, value,
0262 value & PCIE_APP_PMC_IN_L2, 20,
0263 jiffies_to_usecs(5 * HZ));
0264 if (ret)
0265 dev_err(pcie->pci.dev, "PCIe link enter L2 timeout!\n");
0266
0267 return ret;
0268 }
0269
0270 static void intel_pcie_turn_off(struct intel_pcie *pcie)
0271 {
0272 if (dw_pcie_link_up(&pcie->pci))
0273 intel_pcie_wait_l2(pcie);
0274
0275
0276 intel_pcie_device_rst_assert(pcie);
0277 pcie_rc_cfg_wr_mask(pcie, PCI_COMMAND, PCI_COMMAND_MEMORY, 0);
0278 }
0279
0280 static int intel_pcie_host_setup(struct intel_pcie *pcie)
0281 {
0282 int ret;
0283 struct dw_pcie *pci = &pcie->pci;
0284
0285 intel_pcie_core_rst_assert(pcie);
0286 intel_pcie_device_rst_assert(pcie);
0287
0288 ret = phy_init(pcie->phy);
0289 if (ret)
0290 return ret;
0291
0292 intel_pcie_core_rst_deassert(pcie);
0293
0294 ret = clk_prepare_enable(pcie->core_clk);
0295 if (ret) {
0296 dev_err(pcie->pci.dev, "Core clock enable failed: %d\n", ret);
0297 goto clk_err;
0298 }
0299
0300 pci->atu_base = pci->dbi_base + 0xC0000;
0301
0302 intel_pcie_ltssm_disable(pcie);
0303 intel_pcie_link_setup(pcie);
0304 intel_pcie_init_n_fts(pci);
0305
0306 ret = dw_pcie_setup_rc(&pci->pp);
0307 if (ret)
0308 goto app_init_err;
0309
0310 dw_pcie_upconfig_setup(pci);
0311
0312 intel_pcie_device_rst_deassert(pcie);
0313 intel_pcie_ltssm_enable(pcie);
0314
0315 ret = dw_pcie_wait_for_link(pci);
0316 if (ret)
0317 goto app_init_err;
0318
0319
0320 pcie_app_wr_mask(pcie, PCIE_APP_IRNEN, PCIE_APP_IRN_INT,
0321 PCIE_APP_IRN_INT);
0322
0323 return 0;
0324
0325 app_init_err:
0326 clk_disable_unprepare(pcie->core_clk);
0327 clk_err:
0328 intel_pcie_core_rst_assert(pcie);
0329 phy_exit(pcie->phy);
0330
0331 return ret;
0332 }
0333
0334 static void __intel_pcie_remove(struct intel_pcie *pcie)
0335 {
0336 intel_pcie_core_irq_disable(pcie);
0337 intel_pcie_turn_off(pcie);
0338 clk_disable_unprepare(pcie->core_clk);
0339 intel_pcie_core_rst_assert(pcie);
0340 phy_exit(pcie->phy);
0341 }
0342
0343 static int intel_pcie_remove(struct platform_device *pdev)
0344 {
0345 struct intel_pcie *pcie = platform_get_drvdata(pdev);
0346 struct dw_pcie_rp *pp = &pcie->pci.pp;
0347
0348 dw_pcie_host_deinit(pp);
0349 __intel_pcie_remove(pcie);
0350
0351 return 0;
0352 }
0353
0354 static int intel_pcie_suspend_noirq(struct device *dev)
0355 {
0356 struct intel_pcie *pcie = dev_get_drvdata(dev);
0357 int ret;
0358
0359 intel_pcie_core_irq_disable(pcie);
0360 ret = intel_pcie_wait_l2(pcie);
0361 if (ret)
0362 return ret;
0363
0364 phy_exit(pcie->phy);
0365 clk_disable_unprepare(pcie->core_clk);
0366 return ret;
0367 }
0368
0369 static int intel_pcie_resume_noirq(struct device *dev)
0370 {
0371 struct intel_pcie *pcie = dev_get_drvdata(dev);
0372
0373 return intel_pcie_host_setup(pcie);
0374 }
0375
0376 static int intel_pcie_rc_init(struct dw_pcie_rp *pp)
0377 {
0378 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0379 struct intel_pcie *pcie = dev_get_drvdata(pci->dev);
0380
0381 return intel_pcie_host_setup(pcie);
0382 }
0383
0384 static u64 intel_pcie_cpu_addr(struct dw_pcie *pcie, u64 cpu_addr)
0385 {
0386 return cpu_addr + BUS_IATU_OFFSET;
0387 }
0388
0389 static const struct dw_pcie_ops intel_pcie_ops = {
0390 .cpu_addr_fixup = intel_pcie_cpu_addr,
0391 };
0392
0393 static const struct dw_pcie_host_ops intel_pcie_dw_ops = {
0394 .host_init = intel_pcie_rc_init,
0395 };
0396
0397 static int intel_pcie_probe(struct platform_device *pdev)
0398 {
0399 struct device *dev = &pdev->dev;
0400 struct intel_pcie *pcie;
0401 struct dw_pcie_rp *pp;
0402 struct dw_pcie *pci;
0403 int ret;
0404
0405 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
0406 if (!pcie)
0407 return -ENOMEM;
0408
0409 platform_set_drvdata(pdev, pcie);
0410 pci = &pcie->pci;
0411 pci->dev = dev;
0412 pp = &pci->pp;
0413
0414 ret = intel_pcie_get_resources(pdev);
0415 if (ret)
0416 return ret;
0417
0418 ret = intel_pcie_ep_rst_init(pcie);
0419 if (ret)
0420 return ret;
0421
0422 pci->ops = &intel_pcie_ops;
0423 pp->ops = &intel_pcie_dw_ops;
0424
0425 ret = dw_pcie_host_init(pp);
0426 if (ret) {
0427 dev_err(dev, "Cannot initialize host\n");
0428 return ret;
0429 }
0430
0431 return 0;
0432 }
0433
0434 static const struct dev_pm_ops intel_pcie_pm_ops = {
0435 NOIRQ_SYSTEM_SLEEP_PM_OPS(intel_pcie_suspend_noirq,
0436 intel_pcie_resume_noirq)
0437 };
0438
0439 static const struct of_device_id of_intel_pcie_match[] = {
0440 { .compatible = "intel,lgm-pcie" },
0441 {}
0442 };
0443
0444 static struct platform_driver intel_pcie_driver = {
0445 .probe = intel_pcie_probe,
0446 .remove = intel_pcie_remove,
0447 .driver = {
0448 .name = "intel-gw-pcie",
0449 .of_match_table = of_intel_pcie_match,
0450 .pm = &intel_pcie_pm_ops,
0451 },
0452 };
0453 builtin_platform_driver(intel_pcie_driver);