Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * PCIe host controller driver for HiSilicon STB SoCs
0004  *
0005  * Copyright (C) 2016-2017 HiSilicon Co., Ltd. http://www.hisilicon.com
0006  *
0007  * Authors: Ruqiang Ju <juruqiang@hisilicon.com>
0008  *          Jianguo Sun <sunjianguo1@huawei.com>
0009  */
0010 
0011 #include <linux/clk.h>
0012 #include <linux/delay.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/of.h>
0017 #include <linux/of_gpio.h>
0018 #include <linux/pci.h>
0019 #include <linux/phy/phy.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/resource.h>
0022 #include <linux/reset.h>
0023 
0024 #include "pcie-designware.h"
0025 
0026 #define to_histb_pcie(x)    dev_get_drvdata((x)->dev)
0027 
0028 #define PCIE_SYS_CTRL0          0x0000
0029 #define PCIE_SYS_CTRL1          0x0004
0030 #define PCIE_SYS_CTRL7          0x001C
0031 #define PCIE_SYS_CTRL13         0x0034
0032 #define PCIE_SYS_CTRL15         0x003C
0033 #define PCIE_SYS_CTRL16         0x0040
0034 #define PCIE_SYS_CTRL17         0x0044
0035 
0036 #define PCIE_SYS_STAT0          0x0100
0037 #define PCIE_SYS_STAT4          0x0110
0038 
0039 #define PCIE_RDLH_LINK_UP       BIT(5)
0040 #define PCIE_XMLH_LINK_UP       BIT(15)
0041 #define PCIE_ELBI_SLV_DBI_ENABLE    BIT(21)
0042 #define PCIE_APP_LTSSM_ENABLE       BIT(11)
0043 
0044 #define PCIE_DEVICE_TYPE_MASK       GENMASK(31, 28)
0045 #define PCIE_WM_EP          0
0046 #define PCIE_WM_LEGACY          BIT(1)
0047 #define PCIE_WM_RC          BIT(30)
0048 
0049 #define PCIE_LTSSM_STATE_MASK       GENMASK(5, 0)
0050 #define PCIE_LTSSM_STATE_ACTIVE     0x11
0051 
0052 struct histb_pcie {
0053     struct dw_pcie *pci;
0054     struct clk *aux_clk;
0055     struct clk *pipe_clk;
0056     struct clk *sys_clk;
0057     struct clk *bus_clk;
0058     struct phy *phy;
0059     struct reset_control *soft_reset;
0060     struct reset_control *sys_reset;
0061     struct reset_control *bus_reset;
0062     void __iomem *ctrl;
0063     int reset_gpio;
0064     struct regulator *vpcie;
0065 };
0066 
0067 static u32 histb_pcie_readl(struct histb_pcie *histb_pcie, u32 reg)
0068 {
0069     return readl(histb_pcie->ctrl + reg);
0070 }
0071 
0072 static void histb_pcie_writel(struct histb_pcie *histb_pcie, u32 reg, u32 val)
0073 {
0074     writel(val, histb_pcie->ctrl + reg);
0075 }
0076 
0077 static void histb_pcie_dbi_w_mode(struct dw_pcie_rp *pp, bool enable)
0078 {
0079     struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0080     struct histb_pcie *hipcie = to_histb_pcie(pci);
0081     u32 val;
0082 
0083     val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
0084     if (enable)
0085         val |= PCIE_ELBI_SLV_DBI_ENABLE;
0086     else
0087         val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
0088     histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, val);
0089 }
0090 
0091 static void histb_pcie_dbi_r_mode(struct dw_pcie_rp *pp, bool enable)
0092 {
0093     struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0094     struct histb_pcie *hipcie = to_histb_pcie(pci);
0095     u32 val;
0096 
0097     val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL1);
0098     if (enable)
0099         val |= PCIE_ELBI_SLV_DBI_ENABLE;
0100     else
0101         val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
0102     histb_pcie_writel(hipcie, PCIE_SYS_CTRL1, val);
0103 }
0104 
0105 static u32 histb_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
0106                    u32 reg, size_t size)
0107 {
0108     u32 val;
0109 
0110     histb_pcie_dbi_r_mode(&pci->pp, true);
0111     dw_pcie_read(base + reg, size, &val);
0112     histb_pcie_dbi_r_mode(&pci->pp, false);
0113 
0114     return val;
0115 }
0116 
0117 static void histb_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
0118                  u32 reg, size_t size, u32 val)
0119 {
0120     histb_pcie_dbi_w_mode(&pci->pp, true);
0121     dw_pcie_write(base + reg, size, val);
0122     histb_pcie_dbi_w_mode(&pci->pp, false);
0123 }
0124 
0125 static int histb_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
0126                   int where, int size, u32 *val)
0127 {
0128     struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
0129 
0130     if (PCI_SLOT(devfn))
0131         return PCIBIOS_DEVICE_NOT_FOUND;
0132 
0133     *val = dw_pcie_read_dbi(pci, where, size);
0134     return PCIBIOS_SUCCESSFUL;
0135 }
0136 
0137 static int histb_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
0138                   int where, int size, u32 val)
0139 {
0140     struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
0141 
0142     if (PCI_SLOT(devfn))
0143         return PCIBIOS_DEVICE_NOT_FOUND;
0144 
0145     dw_pcie_write_dbi(pci, where, size, val);
0146     return PCIBIOS_SUCCESSFUL;
0147 }
0148 
0149 static struct pci_ops histb_pci_ops = {
0150     .read = histb_pcie_rd_own_conf,
0151     .write = histb_pcie_wr_own_conf,
0152 };
0153 
0154 static int histb_pcie_link_up(struct dw_pcie *pci)
0155 {
0156     struct histb_pcie *hipcie = to_histb_pcie(pci);
0157     u32 regval;
0158     u32 status;
0159 
0160     regval = histb_pcie_readl(hipcie, PCIE_SYS_STAT0);
0161     status = histb_pcie_readl(hipcie, PCIE_SYS_STAT4);
0162     status &= PCIE_LTSSM_STATE_MASK;
0163     if ((regval & PCIE_XMLH_LINK_UP) && (regval & PCIE_RDLH_LINK_UP) &&
0164         (status == PCIE_LTSSM_STATE_ACTIVE))
0165         return 1;
0166 
0167     return 0;
0168 }
0169 
0170 static int histb_pcie_start_link(struct dw_pcie *pci)
0171 {
0172     struct histb_pcie *hipcie = to_histb_pcie(pci);
0173     u32 regval;
0174 
0175     /* assert LTSSM enable */
0176     regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL7);
0177     regval |= PCIE_APP_LTSSM_ENABLE;
0178     histb_pcie_writel(hipcie, PCIE_SYS_CTRL7, regval);
0179 
0180     return 0;
0181 }
0182 
0183 static int histb_pcie_host_init(struct dw_pcie_rp *pp)
0184 {
0185     struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0186     struct histb_pcie *hipcie = to_histb_pcie(pci);
0187     u32 regval;
0188 
0189     pp->bridge->ops = &histb_pci_ops;
0190 
0191     /* PCIe RC work mode */
0192     regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
0193     regval &= ~PCIE_DEVICE_TYPE_MASK;
0194     regval |= PCIE_WM_RC;
0195     histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, regval);
0196 
0197     return 0;
0198 }
0199 
0200 static const struct dw_pcie_host_ops histb_pcie_host_ops = {
0201     .host_init = histb_pcie_host_init,
0202 };
0203 
0204 static void histb_pcie_host_disable(struct histb_pcie *hipcie)
0205 {
0206     reset_control_assert(hipcie->soft_reset);
0207     reset_control_assert(hipcie->sys_reset);
0208     reset_control_assert(hipcie->bus_reset);
0209 
0210     clk_disable_unprepare(hipcie->aux_clk);
0211     clk_disable_unprepare(hipcie->pipe_clk);
0212     clk_disable_unprepare(hipcie->sys_clk);
0213     clk_disable_unprepare(hipcie->bus_clk);
0214 
0215     if (gpio_is_valid(hipcie->reset_gpio))
0216         gpio_set_value_cansleep(hipcie->reset_gpio, 0);
0217 
0218     if (hipcie->vpcie)
0219         regulator_disable(hipcie->vpcie);
0220 }
0221 
0222 static int histb_pcie_host_enable(struct dw_pcie_rp *pp)
0223 {
0224     struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
0225     struct histb_pcie *hipcie = to_histb_pcie(pci);
0226     struct device *dev = pci->dev;
0227     int ret;
0228 
0229     /* power on PCIe device if have */
0230     if (hipcie->vpcie) {
0231         ret = regulator_enable(hipcie->vpcie);
0232         if (ret) {
0233             dev_err(dev, "failed to enable regulator: %d\n", ret);
0234             return ret;
0235         }
0236     }
0237 
0238     if (gpio_is_valid(hipcie->reset_gpio))
0239         gpio_set_value_cansleep(hipcie->reset_gpio, 1);
0240 
0241     ret = clk_prepare_enable(hipcie->bus_clk);
0242     if (ret) {
0243         dev_err(dev, "cannot prepare/enable bus clk\n");
0244         goto err_bus_clk;
0245     }
0246 
0247     ret = clk_prepare_enable(hipcie->sys_clk);
0248     if (ret) {
0249         dev_err(dev, "cannot prepare/enable sys clk\n");
0250         goto err_sys_clk;
0251     }
0252 
0253     ret = clk_prepare_enable(hipcie->pipe_clk);
0254     if (ret) {
0255         dev_err(dev, "cannot prepare/enable pipe clk\n");
0256         goto err_pipe_clk;
0257     }
0258 
0259     ret = clk_prepare_enable(hipcie->aux_clk);
0260     if (ret) {
0261         dev_err(dev, "cannot prepare/enable aux clk\n");
0262         goto err_aux_clk;
0263     }
0264 
0265     reset_control_assert(hipcie->soft_reset);
0266     reset_control_deassert(hipcie->soft_reset);
0267 
0268     reset_control_assert(hipcie->sys_reset);
0269     reset_control_deassert(hipcie->sys_reset);
0270 
0271     reset_control_assert(hipcie->bus_reset);
0272     reset_control_deassert(hipcie->bus_reset);
0273 
0274     return 0;
0275 
0276 err_aux_clk:
0277     clk_disable_unprepare(hipcie->pipe_clk);
0278 err_pipe_clk:
0279     clk_disable_unprepare(hipcie->sys_clk);
0280 err_sys_clk:
0281     clk_disable_unprepare(hipcie->bus_clk);
0282 err_bus_clk:
0283     if (hipcie->vpcie)
0284         regulator_disable(hipcie->vpcie);
0285 
0286     return ret;
0287 }
0288 
0289 static const struct dw_pcie_ops dw_pcie_ops = {
0290     .read_dbi = histb_pcie_read_dbi,
0291     .write_dbi = histb_pcie_write_dbi,
0292     .link_up = histb_pcie_link_up,
0293     .start_link = histb_pcie_start_link,
0294 };
0295 
0296 static int histb_pcie_probe(struct platform_device *pdev)
0297 {
0298     struct histb_pcie *hipcie;
0299     struct dw_pcie *pci;
0300     struct dw_pcie_rp *pp;
0301     struct device_node *np = pdev->dev.of_node;
0302     struct device *dev = &pdev->dev;
0303     enum of_gpio_flags of_flags;
0304     unsigned long flag = GPIOF_DIR_OUT;
0305     int ret;
0306 
0307     hipcie = devm_kzalloc(dev, sizeof(*hipcie), GFP_KERNEL);
0308     if (!hipcie)
0309         return -ENOMEM;
0310 
0311     pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
0312     if (!pci)
0313         return -ENOMEM;
0314 
0315     hipcie->pci = pci;
0316     pp = &pci->pp;
0317     pci->dev = dev;
0318     pci->ops = &dw_pcie_ops;
0319 
0320     hipcie->ctrl = devm_platform_ioremap_resource_byname(pdev, "control");
0321     if (IS_ERR(hipcie->ctrl)) {
0322         dev_err(dev, "cannot get control reg base\n");
0323         return PTR_ERR(hipcie->ctrl);
0324     }
0325 
0326     pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "rc-dbi");
0327     if (IS_ERR(pci->dbi_base)) {
0328         dev_err(dev, "cannot get rc-dbi base\n");
0329         return PTR_ERR(pci->dbi_base);
0330     }
0331 
0332     hipcie->vpcie = devm_regulator_get_optional(dev, "vpcie");
0333     if (IS_ERR(hipcie->vpcie)) {
0334         if (PTR_ERR(hipcie->vpcie) != -ENODEV)
0335             return PTR_ERR(hipcie->vpcie);
0336         hipcie->vpcie = NULL;
0337     }
0338 
0339     hipcie->reset_gpio = of_get_named_gpio_flags(np,
0340                 "reset-gpios", 0, &of_flags);
0341     if (of_flags & OF_GPIO_ACTIVE_LOW)
0342         flag |= GPIOF_ACTIVE_LOW;
0343     if (gpio_is_valid(hipcie->reset_gpio)) {
0344         ret = devm_gpio_request_one(dev, hipcie->reset_gpio,
0345                 flag, "PCIe device power control");
0346         if (ret) {
0347             dev_err(dev, "unable to request gpio\n");
0348             return ret;
0349         }
0350     }
0351 
0352     hipcie->aux_clk = devm_clk_get(dev, "aux");
0353     if (IS_ERR(hipcie->aux_clk)) {
0354         dev_err(dev, "Failed to get PCIe aux clk\n");
0355         return PTR_ERR(hipcie->aux_clk);
0356     }
0357 
0358     hipcie->pipe_clk = devm_clk_get(dev, "pipe");
0359     if (IS_ERR(hipcie->pipe_clk)) {
0360         dev_err(dev, "Failed to get PCIe pipe clk\n");
0361         return PTR_ERR(hipcie->pipe_clk);
0362     }
0363 
0364     hipcie->sys_clk = devm_clk_get(dev, "sys");
0365     if (IS_ERR(hipcie->sys_clk)) {
0366         dev_err(dev, "Failed to get PCIEe sys clk\n");
0367         return PTR_ERR(hipcie->sys_clk);
0368     }
0369 
0370     hipcie->bus_clk = devm_clk_get(dev, "bus");
0371     if (IS_ERR(hipcie->bus_clk)) {
0372         dev_err(dev, "Failed to get PCIe bus clk\n");
0373         return PTR_ERR(hipcie->bus_clk);
0374     }
0375 
0376     hipcie->soft_reset = devm_reset_control_get(dev, "soft");
0377     if (IS_ERR(hipcie->soft_reset)) {
0378         dev_err(dev, "couldn't get soft reset\n");
0379         return PTR_ERR(hipcie->soft_reset);
0380     }
0381 
0382     hipcie->sys_reset = devm_reset_control_get(dev, "sys");
0383     if (IS_ERR(hipcie->sys_reset)) {
0384         dev_err(dev, "couldn't get sys reset\n");
0385         return PTR_ERR(hipcie->sys_reset);
0386     }
0387 
0388     hipcie->bus_reset = devm_reset_control_get(dev, "bus");
0389     if (IS_ERR(hipcie->bus_reset)) {
0390         dev_err(dev, "couldn't get bus reset\n");
0391         return PTR_ERR(hipcie->bus_reset);
0392     }
0393 
0394     hipcie->phy = devm_phy_get(dev, "phy");
0395     if (IS_ERR(hipcie->phy)) {
0396         dev_info(dev, "no pcie-phy found\n");
0397         hipcie->phy = NULL;
0398         /* fall through here!
0399          * if no pcie-phy found, phy init
0400          * should be done under boot!
0401          */
0402     } else {
0403         phy_init(hipcie->phy);
0404     }
0405 
0406     pp->ops = &histb_pcie_host_ops;
0407 
0408     platform_set_drvdata(pdev, hipcie);
0409 
0410     ret = histb_pcie_host_enable(pp);
0411     if (ret) {
0412         dev_err(dev, "failed to enable host\n");
0413         return ret;
0414     }
0415 
0416     ret = dw_pcie_host_init(pp);
0417     if (ret) {
0418         dev_err(dev, "failed to initialize host\n");
0419         return ret;
0420     }
0421 
0422     return 0;
0423 }
0424 
0425 static int histb_pcie_remove(struct platform_device *pdev)
0426 {
0427     struct histb_pcie *hipcie = platform_get_drvdata(pdev);
0428 
0429     histb_pcie_host_disable(hipcie);
0430 
0431     if (hipcie->phy)
0432         phy_exit(hipcie->phy);
0433 
0434     return 0;
0435 }
0436 
0437 static const struct of_device_id histb_pcie_of_match[] = {
0438     { .compatible = "hisilicon,hi3798cv200-pcie", },
0439     {},
0440 };
0441 MODULE_DEVICE_TABLE(of, histb_pcie_of_match);
0442 
0443 static struct platform_driver histb_pcie_platform_driver = {
0444     .probe  = histb_pcie_probe,
0445     .remove = histb_pcie_remove,
0446     .driver = {
0447         .name = "histb-pcie",
0448         .of_match_table = histb_pcie_of_match,
0449     },
0450 };
0451 module_platform_driver(histb_pcie_platform_driver);
0452 
0453 MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver");
0454 MODULE_LICENSE("GPL v2");