0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/clk.h>
0013 #include <linux/delay.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/kernel.h>
0016 #include <linux/init.h>
0017 #include <linux/of_device.h>
0018 #include <linux/pci.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/phy/phy.h>
0021 #include <linux/regulator/consumer.h>
0022 #include <linux/module.h>
0023
0024 #include "pcie-designware.h"
0025
0026 #define to_exynos_pcie(x) dev_get_drvdata((x)->dev)
0027
0028
0029 #define PCIE_IRQ_PULSE 0x000
0030 #define IRQ_INTA_ASSERT BIT(0)
0031 #define IRQ_INTB_ASSERT BIT(2)
0032 #define IRQ_INTC_ASSERT BIT(4)
0033 #define IRQ_INTD_ASSERT BIT(6)
0034 #define PCIE_IRQ_LEVEL 0x004
0035 #define PCIE_IRQ_SPECIAL 0x008
0036 #define PCIE_IRQ_EN_PULSE 0x00c
0037 #define PCIE_IRQ_EN_LEVEL 0x010
0038 #define PCIE_IRQ_EN_SPECIAL 0x014
0039 #define PCIE_SW_WAKE 0x018
0040 #define PCIE_BUS_EN BIT(1)
0041 #define PCIE_CORE_RESET 0x01c
0042 #define PCIE_CORE_RESET_ENABLE BIT(0)
0043 #define PCIE_STICKY_RESET 0x020
0044 #define PCIE_NONSTICKY_RESET 0x024
0045 #define PCIE_APP_INIT_RESET 0x028
0046 #define PCIE_APP_LTSSM_ENABLE 0x02c
0047 #define PCIE_ELBI_RDLH_LINKUP 0x074
0048 #define PCIE_ELBI_XMLH_LINKUP BIT(4)
0049 #define PCIE_ELBI_LTSSM_ENABLE 0x1
0050 #define PCIE_ELBI_SLV_AWMISC 0x11c
0051 #define PCIE_ELBI_SLV_ARMISC 0x120
0052 #define PCIE_ELBI_SLV_DBI_ENABLE BIT(21)
0053
0054 struct exynos_pcie {
0055 struct dw_pcie pci;
0056 void __iomem *elbi_base;
0057 struct clk *clk;
0058 struct clk *bus_clk;
0059 struct phy *phy;
0060 struct regulator_bulk_data supplies[2];
0061 };
0062
0063 static int exynos_pcie_init_clk_resources(struct exynos_pcie *ep)
0064 {
0065 struct device *dev = ep->pci.dev;
0066 int ret;
0067
0068 ret = clk_prepare_enable(ep->clk);
0069 if (ret) {
0070 dev_err(dev, "cannot enable pcie rc clock");
0071 return ret;
0072 }
0073
0074 ret = clk_prepare_enable(ep->bus_clk);
0075 if (ret) {
0076 dev_err(dev, "cannot enable pcie bus clock");
0077 goto err_bus_clk;
0078 }
0079
0080 return 0;
0081
0082 err_bus_clk:
0083 clk_disable_unprepare(ep->clk);
0084
0085 return ret;
0086 }
0087
0088 static void exynos_pcie_deinit_clk_resources(struct exynos_pcie *ep)
0089 {
0090 clk_disable_unprepare(ep->bus_clk);
0091 clk_disable_unprepare(ep->clk);
0092 }
0093
0094 static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
0095 {
0096 writel(val, base + reg);
0097 }
0098
0099 static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
0100 {
0101 return readl(base + reg);
0102 }
0103
0104 static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
0105 {
0106 u32 val;
0107
0108 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_SLV_AWMISC);
0109 if (on)
0110 val |= PCIE_ELBI_SLV_DBI_ENABLE;
0111 else
0112 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
0113 exynos_pcie_writel(ep->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
0114 }
0115
0116 static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
0117 {
0118 u32 val;
0119
0120 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_SLV_ARMISC);
0121 if (on)
0122 val |= PCIE_ELBI_SLV_DBI_ENABLE;
0123 else
0124 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
0125 exynos_pcie_writel(ep->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
0126 }
0127
0128 static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
0129 {
0130 u32 val;
0131
0132 val = exynos_pcie_readl(ep->elbi_base, PCIE_CORE_RESET);
0133 val &= ~PCIE_CORE_RESET_ENABLE;
0134 exynos_pcie_writel(ep->elbi_base, val, PCIE_CORE_RESET);
0135 exynos_pcie_writel(ep->elbi_base, 0, PCIE_STICKY_RESET);
0136 exynos_pcie_writel(ep->elbi_base, 0, PCIE_NONSTICKY_RESET);
0137 }
0138
0139 static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
0140 {
0141 u32 val;
0142
0143 val = exynos_pcie_readl(ep->elbi_base, PCIE_CORE_RESET);
0144 val |= PCIE_CORE_RESET_ENABLE;
0145
0146 exynos_pcie_writel(ep->elbi_base, val, PCIE_CORE_RESET);
0147 exynos_pcie_writel(ep->elbi_base, 1, PCIE_STICKY_RESET);
0148 exynos_pcie_writel(ep->elbi_base, 1, PCIE_NONSTICKY_RESET);
0149 exynos_pcie_writel(ep->elbi_base, 1, PCIE_APP_INIT_RESET);
0150 exynos_pcie_writel(ep->elbi_base, 0, PCIE_APP_INIT_RESET);
0151 }
0152
0153 static int exynos_pcie_start_link(struct dw_pcie *pci)
0154 {
0155 struct exynos_pcie *ep = to_exynos_pcie(pci);
0156 u32 val;
0157
0158 val = exynos_pcie_readl(ep->elbi_base, PCIE_SW_WAKE);
0159 val &= ~PCIE_BUS_EN;
0160 exynos_pcie_writel(ep->elbi_base, val, PCIE_SW_WAKE);
0161
0162
0163 exynos_pcie_writel(ep->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
0164 PCIE_APP_LTSSM_ENABLE);
0165 return 0;
0166 }
0167
0168 static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
0169 {
0170 u32 val = exynos_pcie_readl(ep->elbi_base, PCIE_IRQ_PULSE);
0171
0172 exynos_pcie_writel(ep->elbi_base, val, PCIE_IRQ_PULSE);
0173 }
0174
0175 static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
0176 {
0177 struct exynos_pcie *ep = arg;
0178
0179 exynos_pcie_clear_irq_pulse(ep);
0180 return IRQ_HANDLED;
0181 }
0182
0183 static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
0184 {
0185 u32 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
0186 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
0187
0188 exynos_pcie_writel(ep->elbi_base, val, PCIE_IRQ_EN_PULSE);
0189 exynos_pcie_writel(ep->elbi_base, 0, PCIE_IRQ_EN_LEVEL);
0190 exynos_pcie_writel(ep->elbi_base, 0, PCIE_IRQ_EN_SPECIAL);
0191 }
0192
0193 static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
0194 u32 reg, size_t size)
0195 {
0196 struct exynos_pcie *ep = to_exynos_pcie(pci);
0197 u32 val;
0198
0199 exynos_pcie_sideband_dbi_r_mode(ep, true);
0200 dw_pcie_read(base + reg, size, &val);
0201 exynos_pcie_sideband_dbi_r_mode(ep, false);
0202 return val;
0203 }
0204
0205 static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
0206 u32 reg, size_t size, u32 val)
0207 {
0208 struct exynos_pcie *ep = to_exynos_pcie(pci);
0209
0210 exynos_pcie_sideband_dbi_w_mode(ep, true);
0211 dw_pcie_write(base + reg, size, val);
0212 exynos_pcie_sideband_dbi_w_mode(ep, false);
0213 }
0214
0215 static int exynos_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
0216 int where, int size, u32 *val)
0217 {
0218 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
0219
0220 if (PCI_SLOT(devfn))
0221 return PCIBIOS_DEVICE_NOT_FOUND;
0222
0223 *val = dw_pcie_read_dbi(pci, where, size);
0224 return PCIBIOS_SUCCESSFUL;
0225 }
0226
0227 static int exynos_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
0228 int where, int size, u32 val)
0229 {
0230 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
0231
0232 if (PCI_SLOT(devfn))
0233 return PCIBIOS_DEVICE_NOT_FOUND;
0234
0235 dw_pcie_write_dbi(pci, where, size, val);
0236 return PCIBIOS_SUCCESSFUL;
0237 }
0238
0239 static struct pci_ops exynos_pci_ops = {
0240 .read = exynos_pcie_rd_own_conf,
0241 .write = exynos_pcie_wr_own_conf,
0242 };
0243
0244 static int exynos_pcie_link_up(struct dw_pcie *pci)
0245 {
0246 struct exynos_pcie *ep = to_exynos_pcie(pci);
0247 u32 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_RDLH_LINKUP);
0248
0249 return (val & PCIE_ELBI_XMLH_LINKUP);
0250 }
0251
0252 static int exynos_pcie_host_init(struct dw_pcie_rp *pp)
0253 {
0254 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0255 struct exynos_pcie *ep = to_exynos_pcie(pci);
0256
0257 pp->bridge->ops = &exynos_pci_ops;
0258
0259 exynos_pcie_assert_core_reset(ep);
0260
0261 phy_init(ep->phy);
0262 phy_power_on(ep->phy);
0263
0264 exynos_pcie_deassert_core_reset(ep);
0265 exynos_pcie_enable_irq_pulse(ep);
0266
0267 return 0;
0268 }
0269
0270 static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
0271 .host_init = exynos_pcie_host_init,
0272 };
0273
0274 static int exynos_add_pcie_port(struct exynos_pcie *ep,
0275 struct platform_device *pdev)
0276 {
0277 struct dw_pcie *pci = &ep->pci;
0278 struct dw_pcie_rp *pp = &pci->pp;
0279 struct device *dev = &pdev->dev;
0280 int ret;
0281
0282 pp->irq = platform_get_irq(pdev, 0);
0283 if (pp->irq < 0)
0284 return pp->irq;
0285
0286 ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
0287 IRQF_SHARED, "exynos-pcie", ep);
0288 if (ret) {
0289 dev_err(dev, "failed to request irq\n");
0290 return ret;
0291 }
0292
0293 pp->ops = &exynos_pcie_host_ops;
0294 pp->msi_irq[0] = -ENODEV;
0295
0296 ret = dw_pcie_host_init(pp);
0297 if (ret) {
0298 dev_err(dev, "failed to initialize host\n");
0299 return ret;
0300 }
0301
0302 return 0;
0303 }
0304
0305 static const struct dw_pcie_ops dw_pcie_ops = {
0306 .read_dbi = exynos_pcie_read_dbi,
0307 .write_dbi = exynos_pcie_write_dbi,
0308 .link_up = exynos_pcie_link_up,
0309 .start_link = exynos_pcie_start_link,
0310 };
0311
0312 static int exynos_pcie_probe(struct platform_device *pdev)
0313 {
0314 struct device *dev = &pdev->dev;
0315 struct exynos_pcie *ep;
0316 struct device_node *np = dev->of_node;
0317 int ret;
0318
0319 ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
0320 if (!ep)
0321 return -ENOMEM;
0322
0323 ep->pci.dev = dev;
0324 ep->pci.ops = &dw_pcie_ops;
0325
0326 ep->phy = devm_of_phy_get(dev, np, NULL);
0327 if (IS_ERR(ep->phy))
0328 return PTR_ERR(ep->phy);
0329
0330
0331 ep->elbi_base = devm_platform_ioremap_resource_byname(pdev, "elbi");
0332 if (IS_ERR(ep->elbi_base))
0333 return PTR_ERR(ep->elbi_base);
0334
0335 ep->clk = devm_clk_get(dev, "pcie");
0336 if (IS_ERR(ep->clk)) {
0337 dev_err(dev, "Failed to get pcie rc clock\n");
0338 return PTR_ERR(ep->clk);
0339 }
0340
0341 ep->bus_clk = devm_clk_get(dev, "pcie_bus");
0342 if (IS_ERR(ep->bus_clk)) {
0343 dev_err(dev, "Failed to get pcie bus clock\n");
0344 return PTR_ERR(ep->bus_clk);
0345 }
0346
0347 ep->supplies[0].supply = "vdd18";
0348 ep->supplies[1].supply = "vdd10";
0349 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ep->supplies),
0350 ep->supplies);
0351 if (ret)
0352 return ret;
0353
0354 ret = exynos_pcie_init_clk_resources(ep);
0355 if (ret)
0356 return ret;
0357
0358 ret = regulator_bulk_enable(ARRAY_SIZE(ep->supplies), ep->supplies);
0359 if (ret)
0360 return ret;
0361
0362 platform_set_drvdata(pdev, ep);
0363
0364 ret = exynos_add_pcie_port(ep, pdev);
0365 if (ret < 0)
0366 goto fail_probe;
0367
0368 return 0;
0369
0370 fail_probe:
0371 phy_exit(ep->phy);
0372 exynos_pcie_deinit_clk_resources(ep);
0373 regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
0374
0375 return ret;
0376 }
0377
0378 static int __exit exynos_pcie_remove(struct platform_device *pdev)
0379 {
0380 struct exynos_pcie *ep = platform_get_drvdata(pdev);
0381
0382 dw_pcie_host_deinit(&ep->pci.pp);
0383 exynos_pcie_assert_core_reset(ep);
0384 phy_power_off(ep->phy);
0385 phy_exit(ep->phy);
0386 exynos_pcie_deinit_clk_resources(ep);
0387 regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
0388
0389 return 0;
0390 }
0391
0392 static int exynos_pcie_suspend_noirq(struct device *dev)
0393 {
0394 struct exynos_pcie *ep = dev_get_drvdata(dev);
0395
0396 exynos_pcie_assert_core_reset(ep);
0397 phy_power_off(ep->phy);
0398 phy_exit(ep->phy);
0399 regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
0400
0401 return 0;
0402 }
0403
0404 static int exynos_pcie_resume_noirq(struct device *dev)
0405 {
0406 struct exynos_pcie *ep = dev_get_drvdata(dev);
0407 struct dw_pcie *pci = &ep->pci;
0408 struct dw_pcie_rp *pp = &pci->pp;
0409 int ret;
0410
0411 ret = regulator_bulk_enable(ARRAY_SIZE(ep->supplies), ep->supplies);
0412 if (ret)
0413 return ret;
0414
0415
0416 exynos_pcie_host_init(pp);
0417 dw_pcie_setup_rc(pp);
0418 exynos_pcie_start_link(pci);
0419 return dw_pcie_wait_for_link(pci);
0420 }
0421
0422 static const struct dev_pm_ops exynos_pcie_pm_ops = {
0423 NOIRQ_SYSTEM_SLEEP_PM_OPS(exynos_pcie_suspend_noirq,
0424 exynos_pcie_resume_noirq)
0425 };
0426
0427 static const struct of_device_id exynos_pcie_of_match[] = {
0428 { .compatible = "samsung,exynos5433-pcie", },
0429 { },
0430 };
0431
0432 static struct platform_driver exynos_pcie_driver = {
0433 .probe = exynos_pcie_probe,
0434 .remove = __exit_p(exynos_pcie_remove),
0435 .driver = {
0436 .name = "exynos-pcie",
0437 .of_match_table = exynos_pcie_of_match,
0438 .pm = &exynos_pcie_pm_ops,
0439 },
0440 };
0441 module_platform_driver(exynos_pcie_driver);
0442 MODULE_LICENSE("GPL v2");
0443 MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);