0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/delay.h>
0011 #include <linux/kernel.h>
0012 #include <linux/init.h>
0013 #include <linux/of_device.h>
0014 #include <linux/pci.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/resource.h>
0017 #include <linux/signal.h>
0018 #include <linux/types.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/mfd/syscon.h>
0021 #include <linux/regmap.h>
0022
0023 #include "pcie-designware.h"
0024
0025 #define to_artpec6_pcie(x) dev_get_drvdata((x)->dev)
0026
0027 enum artpec_pcie_variants {
0028 ARTPEC6,
0029 ARTPEC7,
0030 };
0031
0032 struct artpec6_pcie {
0033 struct dw_pcie *pci;
0034 struct regmap *regmap;
0035 void __iomem *phy_base;
0036 enum artpec_pcie_variants variant;
0037 enum dw_pcie_device_mode mode;
0038 };
0039
0040 struct artpec_pcie_of_data {
0041 enum artpec_pcie_variants variant;
0042 enum dw_pcie_device_mode mode;
0043 };
0044
0045 static const struct of_device_id artpec6_pcie_of_match[];
0046
0047
0048 #define PCIECFG 0x18
0049 #define PCIECFG_DBG_OEN BIT(24)
0050 #define PCIECFG_CORE_RESET_REQ BIT(21)
0051 #define PCIECFG_LTSSM_ENABLE BIT(20)
0052 #define PCIECFG_DEVICE_TYPE_MASK GENMASK(19, 16)
0053 #define PCIECFG_CLKREQ_B BIT(11)
0054 #define PCIECFG_REFCLK_ENABLE BIT(10)
0055 #define PCIECFG_PLL_ENABLE BIT(9)
0056 #define PCIECFG_PCLK_ENABLE BIT(8)
0057 #define PCIECFG_RISRCREN BIT(4)
0058 #define PCIECFG_MODE_TX_DRV_EN BIT(3)
0059 #define PCIECFG_CISRREN BIT(2)
0060 #define PCIECFG_MACRO_ENABLE BIT(0)
0061
0062 #define PCIECFG_REFCLKSEL BIT(23)
0063 #define PCIECFG_NOC_RESET BIT(3)
0064
0065 #define PCIESTAT 0x1c
0066
0067 #define PCIESTAT_EXTREFCLK BIT(3)
0068
0069 #define NOCCFG 0x40
0070 #define NOCCFG_ENABLE_CLK_PCIE BIT(4)
0071 #define NOCCFG_POWER_PCIE_IDLEACK BIT(3)
0072 #define NOCCFG_POWER_PCIE_IDLE BIT(2)
0073 #define NOCCFG_POWER_PCIE_IDLEREQ BIT(1)
0074
0075 #define PHY_STATUS 0x118
0076 #define PHY_COSPLLLOCK BIT(0)
0077
0078 #define PHY_TX_ASIC_OUT 0x4040
0079 #define PHY_TX_ASIC_OUT_TX_ACK BIT(0)
0080
0081 #define PHY_RX_ASIC_OUT 0x405c
0082 #define PHY_RX_ASIC_OUT_ACK BIT(0)
0083
0084 static u32 artpec6_pcie_readl(struct artpec6_pcie *artpec6_pcie, u32 offset)
0085 {
0086 u32 val;
0087
0088 regmap_read(artpec6_pcie->regmap, offset, &val);
0089 return val;
0090 }
0091
0092 static void artpec6_pcie_writel(struct artpec6_pcie *artpec6_pcie, u32 offset, u32 val)
0093 {
0094 regmap_write(artpec6_pcie->regmap, offset, val);
0095 }
0096
0097 static u64 artpec6_pcie_cpu_addr_fixup(struct dw_pcie *pci, u64 pci_addr)
0098 {
0099 struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
0100 struct dw_pcie_rp *pp = &pci->pp;
0101 struct dw_pcie_ep *ep = &pci->ep;
0102
0103 switch (artpec6_pcie->mode) {
0104 case DW_PCIE_RC_TYPE:
0105 return pci_addr - pp->cfg0_base;
0106 case DW_PCIE_EP_TYPE:
0107 return pci_addr - ep->phys_base;
0108 default:
0109 dev_err(pci->dev, "UNKNOWN device type\n");
0110 }
0111 return pci_addr;
0112 }
0113
0114 static int artpec6_pcie_establish_link(struct dw_pcie *pci)
0115 {
0116 struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
0117 u32 val;
0118
0119 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
0120 val |= PCIECFG_LTSSM_ENABLE;
0121 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
0122
0123 return 0;
0124 }
0125
0126 static void artpec6_pcie_stop_link(struct dw_pcie *pci)
0127 {
0128 struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
0129 u32 val;
0130
0131 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
0132 val &= ~PCIECFG_LTSSM_ENABLE;
0133 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
0134 }
0135
0136 static const struct dw_pcie_ops dw_pcie_ops = {
0137 .cpu_addr_fixup = artpec6_pcie_cpu_addr_fixup,
0138 .start_link = artpec6_pcie_establish_link,
0139 .stop_link = artpec6_pcie_stop_link,
0140 };
0141
0142 static void artpec6_pcie_wait_for_phy_a6(struct artpec6_pcie *artpec6_pcie)
0143 {
0144 struct dw_pcie *pci = artpec6_pcie->pci;
0145 struct device *dev = pci->dev;
0146 u32 val;
0147 unsigned int retries;
0148
0149 retries = 50;
0150 do {
0151 usleep_range(1000, 2000);
0152 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
0153 retries--;
0154 } while (retries &&
0155 (val & (NOCCFG_POWER_PCIE_IDLEACK | NOCCFG_POWER_PCIE_IDLE)));
0156 if (!retries)
0157 dev_err(dev, "PCIe clock manager did not leave idle state\n");
0158
0159 retries = 50;
0160 do {
0161 usleep_range(1000, 2000);
0162 val = readl(artpec6_pcie->phy_base + PHY_STATUS);
0163 retries--;
0164 } while (retries && !(val & PHY_COSPLLLOCK));
0165 if (!retries)
0166 dev_err(dev, "PHY PLL did not lock\n");
0167 }
0168
0169 static void artpec6_pcie_wait_for_phy_a7(struct artpec6_pcie *artpec6_pcie)
0170 {
0171 struct dw_pcie *pci = artpec6_pcie->pci;
0172 struct device *dev = pci->dev;
0173 u32 val;
0174 u16 phy_status_tx, phy_status_rx;
0175 unsigned int retries;
0176
0177 retries = 50;
0178 do {
0179 usleep_range(1000, 2000);
0180 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
0181 retries--;
0182 } while (retries &&
0183 (val & (NOCCFG_POWER_PCIE_IDLEACK | NOCCFG_POWER_PCIE_IDLE)));
0184 if (!retries)
0185 dev_err(dev, "PCIe clock manager did not leave idle state\n");
0186
0187 retries = 50;
0188 do {
0189 usleep_range(1000, 2000);
0190 phy_status_tx = readw(artpec6_pcie->phy_base + PHY_TX_ASIC_OUT);
0191 phy_status_rx = readw(artpec6_pcie->phy_base + PHY_RX_ASIC_OUT);
0192 retries--;
0193 } while (retries && ((phy_status_tx & PHY_TX_ASIC_OUT_TX_ACK) ||
0194 (phy_status_rx & PHY_RX_ASIC_OUT_ACK)));
0195 if (!retries)
0196 dev_err(dev, "PHY did not enter Pn state\n");
0197 }
0198
0199 static void artpec6_pcie_wait_for_phy(struct artpec6_pcie *artpec6_pcie)
0200 {
0201 switch (artpec6_pcie->variant) {
0202 case ARTPEC6:
0203 artpec6_pcie_wait_for_phy_a6(artpec6_pcie);
0204 break;
0205 case ARTPEC7:
0206 artpec6_pcie_wait_for_phy_a7(artpec6_pcie);
0207 break;
0208 }
0209 }
0210
0211 static void artpec6_pcie_init_phy_a6(struct artpec6_pcie *artpec6_pcie)
0212 {
0213 u32 val;
0214
0215 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
0216 val |= PCIECFG_RISRCREN |
0217 PCIECFG_MODE_TX_DRV_EN |
0218 PCIECFG_CISRREN |
0219 PCIECFG_MACRO_ENABLE;
0220 val |= PCIECFG_REFCLK_ENABLE;
0221 val &= ~PCIECFG_DBG_OEN;
0222 val &= ~PCIECFG_CLKREQ_B;
0223 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
0224 usleep_range(5000, 6000);
0225
0226 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
0227 val |= NOCCFG_ENABLE_CLK_PCIE;
0228 artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
0229 usleep_range(20, 30);
0230
0231 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
0232 val |= PCIECFG_PCLK_ENABLE | PCIECFG_PLL_ENABLE;
0233 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
0234 usleep_range(6000, 7000);
0235
0236 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
0237 val &= ~NOCCFG_POWER_PCIE_IDLEREQ;
0238 artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
0239 }
0240
0241 static void artpec6_pcie_init_phy_a7(struct artpec6_pcie *artpec6_pcie)
0242 {
0243 struct dw_pcie *pci = artpec6_pcie->pci;
0244 u32 val;
0245 bool extrefclk;
0246
0247
0248 val = artpec6_pcie_readl(artpec6_pcie, PCIESTAT);
0249 extrefclk = !!(val & PCIESTAT_EXTREFCLK);
0250 dev_dbg(pci->dev, "Using reference clock: %s\n",
0251 extrefclk ? "external" : "internal");
0252
0253 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
0254 val |= PCIECFG_RISRCREN |
0255 PCIECFG_PCLK_ENABLE;
0256 if (extrefclk)
0257 val |= PCIECFG_REFCLKSEL;
0258 else
0259 val &= ~PCIECFG_REFCLKSEL;
0260 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
0261 usleep_range(10, 20);
0262
0263 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
0264 val |= NOCCFG_ENABLE_CLK_PCIE;
0265 artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
0266 usleep_range(20, 30);
0267
0268 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
0269 val &= ~NOCCFG_POWER_PCIE_IDLEREQ;
0270 artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
0271 }
0272
0273 static void artpec6_pcie_init_phy(struct artpec6_pcie *artpec6_pcie)
0274 {
0275 switch (artpec6_pcie->variant) {
0276 case ARTPEC6:
0277 artpec6_pcie_init_phy_a6(artpec6_pcie);
0278 break;
0279 case ARTPEC7:
0280 artpec6_pcie_init_phy_a7(artpec6_pcie);
0281 break;
0282 }
0283 }
0284
0285 static void artpec6_pcie_assert_core_reset(struct artpec6_pcie *artpec6_pcie)
0286 {
0287 u32 val;
0288
0289 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
0290 switch (artpec6_pcie->variant) {
0291 case ARTPEC6:
0292 val |= PCIECFG_CORE_RESET_REQ;
0293 break;
0294 case ARTPEC7:
0295 val &= ~PCIECFG_NOC_RESET;
0296 break;
0297 }
0298 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
0299 }
0300
0301 static void artpec6_pcie_deassert_core_reset(struct artpec6_pcie *artpec6_pcie)
0302 {
0303 u32 val;
0304
0305 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
0306 switch (artpec6_pcie->variant) {
0307 case ARTPEC6:
0308 val &= ~PCIECFG_CORE_RESET_REQ;
0309 break;
0310 case ARTPEC7:
0311 val |= PCIECFG_NOC_RESET;
0312 break;
0313 }
0314 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
0315 usleep_range(100, 200);
0316 }
0317
0318 static int artpec6_pcie_host_init(struct dw_pcie_rp *pp)
0319 {
0320 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0321 struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
0322
0323 if (artpec6_pcie->variant == ARTPEC7) {
0324 pci->n_fts[0] = 180;
0325 pci->n_fts[1] = 180;
0326 }
0327 artpec6_pcie_assert_core_reset(artpec6_pcie);
0328 artpec6_pcie_init_phy(artpec6_pcie);
0329 artpec6_pcie_deassert_core_reset(artpec6_pcie);
0330 artpec6_pcie_wait_for_phy(artpec6_pcie);
0331
0332 return 0;
0333 }
0334
0335 static const struct dw_pcie_host_ops artpec6_pcie_host_ops = {
0336 .host_init = artpec6_pcie_host_init,
0337 };
0338
0339 static void artpec6_pcie_ep_init(struct dw_pcie_ep *ep)
0340 {
0341 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0342 struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
0343 enum pci_barno bar;
0344
0345 artpec6_pcie_assert_core_reset(artpec6_pcie);
0346 artpec6_pcie_init_phy(artpec6_pcie);
0347 artpec6_pcie_deassert_core_reset(artpec6_pcie);
0348 artpec6_pcie_wait_for_phy(artpec6_pcie);
0349
0350 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
0351 dw_pcie_ep_reset_bar(pci, bar);
0352 }
0353
0354 static int artpec6_pcie_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
0355 enum pci_epc_irq_type type, u16 interrupt_num)
0356 {
0357 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
0358
0359 switch (type) {
0360 case PCI_EPC_IRQ_LEGACY:
0361 dev_err(pci->dev, "EP cannot trigger legacy IRQs\n");
0362 return -EINVAL;
0363 case PCI_EPC_IRQ_MSI:
0364 return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
0365 default:
0366 dev_err(pci->dev, "UNKNOWN IRQ type\n");
0367 }
0368
0369 return 0;
0370 }
0371
0372 static const struct dw_pcie_ep_ops pcie_ep_ops = {
0373 .ep_init = artpec6_pcie_ep_init,
0374 .raise_irq = artpec6_pcie_raise_irq,
0375 };
0376
0377 static int artpec6_pcie_probe(struct platform_device *pdev)
0378 {
0379 struct device *dev = &pdev->dev;
0380 struct dw_pcie *pci;
0381 struct artpec6_pcie *artpec6_pcie;
0382 int ret;
0383 const struct artpec_pcie_of_data *data;
0384 enum artpec_pcie_variants variant;
0385 enum dw_pcie_device_mode mode;
0386 u32 val;
0387
0388 data = of_device_get_match_data(dev);
0389 if (!data)
0390 return -EINVAL;
0391
0392 variant = (enum artpec_pcie_variants)data->variant;
0393 mode = (enum dw_pcie_device_mode)data->mode;
0394
0395 artpec6_pcie = devm_kzalloc(dev, sizeof(*artpec6_pcie), GFP_KERNEL);
0396 if (!artpec6_pcie)
0397 return -ENOMEM;
0398
0399 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
0400 if (!pci)
0401 return -ENOMEM;
0402
0403 pci->dev = dev;
0404 pci->ops = &dw_pcie_ops;
0405
0406 artpec6_pcie->pci = pci;
0407 artpec6_pcie->variant = variant;
0408 artpec6_pcie->mode = mode;
0409
0410 artpec6_pcie->phy_base =
0411 devm_platform_ioremap_resource_byname(pdev, "phy");
0412 if (IS_ERR(artpec6_pcie->phy_base))
0413 return PTR_ERR(artpec6_pcie->phy_base);
0414
0415 artpec6_pcie->regmap =
0416 syscon_regmap_lookup_by_phandle(dev->of_node,
0417 "axis,syscon-pcie");
0418 if (IS_ERR(artpec6_pcie->regmap))
0419 return PTR_ERR(artpec6_pcie->regmap);
0420
0421 platform_set_drvdata(pdev, artpec6_pcie);
0422
0423 switch (artpec6_pcie->mode) {
0424 case DW_PCIE_RC_TYPE:
0425 if (!IS_ENABLED(CONFIG_PCIE_ARTPEC6_HOST))
0426 return -ENODEV;
0427
0428 pci->pp.ops = &artpec6_pcie_host_ops;
0429
0430 ret = dw_pcie_host_init(&pci->pp);
0431 if (ret < 0)
0432 return ret;
0433 break;
0434 case DW_PCIE_EP_TYPE:
0435 if (!IS_ENABLED(CONFIG_PCIE_ARTPEC6_EP))
0436 return -ENODEV;
0437
0438 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
0439 val &= ~PCIECFG_DEVICE_TYPE_MASK;
0440 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
0441
0442 pci->ep.ops = &pcie_ep_ops;
0443
0444 return dw_pcie_ep_init(&pci->ep);
0445 default:
0446 dev_err(dev, "INVALID device type %d\n", artpec6_pcie->mode);
0447 }
0448
0449 return 0;
0450 }
0451
0452 static const struct artpec_pcie_of_data artpec6_pcie_rc_of_data = {
0453 .variant = ARTPEC6,
0454 .mode = DW_PCIE_RC_TYPE,
0455 };
0456
0457 static const struct artpec_pcie_of_data artpec6_pcie_ep_of_data = {
0458 .variant = ARTPEC6,
0459 .mode = DW_PCIE_EP_TYPE,
0460 };
0461
0462 static const struct artpec_pcie_of_data artpec7_pcie_rc_of_data = {
0463 .variant = ARTPEC7,
0464 .mode = DW_PCIE_RC_TYPE,
0465 };
0466
0467 static const struct artpec_pcie_of_data artpec7_pcie_ep_of_data = {
0468 .variant = ARTPEC7,
0469 .mode = DW_PCIE_EP_TYPE,
0470 };
0471
0472 static const struct of_device_id artpec6_pcie_of_match[] = {
0473 {
0474 .compatible = "axis,artpec6-pcie",
0475 .data = &artpec6_pcie_rc_of_data,
0476 },
0477 {
0478 .compatible = "axis,artpec6-pcie-ep",
0479 .data = &artpec6_pcie_ep_of_data,
0480 },
0481 {
0482 .compatible = "axis,artpec7-pcie",
0483 .data = &artpec7_pcie_rc_of_data,
0484 },
0485 {
0486 .compatible = "axis,artpec7-pcie-ep",
0487 .data = &artpec7_pcie_ep_of_data,
0488 },
0489 {},
0490 };
0491
0492 static struct platform_driver artpec6_pcie_driver = {
0493 .probe = artpec6_pcie_probe,
0494 .driver = {
0495 .name = "artpec6-pcie",
0496 .of_match_table = artpec6_pcie_of_match,
0497 .suppress_bind_attrs = true,
0498 },
0499 };
0500 builtin_platform_driver(artpec6_pcie_driver);