oxnas: pcie: model shared resource as external pcie-phy driver
[openwrt/staging/wigyori.git] / target / linux / oxnas / files / drivers / pci / host / pcie-oxnas.c
index d69820d2aadb7c3237dc4810cf4e3fe1daccd877..68898c3beb87c74c36e13b22509401624ef13757 100644 (file)
@@ -1,6 +1,8 @@
 /*
  * PCIe driver for PLX NAS782X SoCs
  *
+ * Author: Ma Haijun <mahaijuns@gmail.com>
+ *
  * This file is licensed under the terms of the GNU General Public
  * License version 2.  This program is licensed "as is" without any
  * warranty of any kind, whether express or implied.
@@ -23,6 +25,8 @@
 #include <linux/gpio.h>
 #include <linux/delay.h>
 #include <linux/clk.h>
+#include <linux/phy.h>
+#include <linux/phy/phy.h>
 #include <linux/regmap.h>
 #include <linux/reset.h>
 #include <linux/io.h>
@@ -73,23 +77,6 @@ enum {
        PCIE_OBTRANS = BIT(12),
 };
 
-enum {
-       HCSL_BIAS_ON = BIT(0),
-       HCSL_PCIE_EN = BIT(1),
-       HCSL_PCIEA_EN = BIT(2),
-       HCSL_PCIEB_EN = BIT(3),
-};
-
-enum {
-       /* pcie phy reg offset */
-       PHY_ADDR = 0,
-       PHY_DATA = 4,
-       /* phy data reg bits */
-       READ_EN = BIT(16),
-       WRITE_EN = BIT(17),
-       CAP_DATA = BIT(18),
-};
-
 /* core config registers */
 enum {
        PCI_CONFIG_VERSION_DEVICEID = 0,
@@ -125,9 +112,6 @@ enum {
        PCIE_SLAVE_BE_SHIFT     = 22,
 };
 
-#define ADDR_VAL(val)  ((val) & 0xFFFF)
-#define DATA_VAL(val)  ((val) & 0xFFFF)
-
 #define PCIE_SLAVE_BE(val)     ((val) << PCIE_SLAVE_BE_SHIFT)
 #define PCIE_SLAVE_BE_MASK     PCIE_SLAVE_BE(0xF)
 
@@ -144,7 +128,7 @@ struct oxnas_pcie {
        struct regmap *sys_ctrl;
        unsigned int outbound_offset;
        unsigned int pcie_ctrl_offset;
-
+       struct phy *phy;
        int haslink;
        struct platform_device *pdev;
        struct resource io;
@@ -192,7 +176,7 @@ static int oxnas_pcie_link_up(struct oxnas_pcie *pcie)
        return 0;
 }
 
-static void __init oxnas_pcie_setup_hw(struct oxnas_pcie *pcie)
+static void oxnas_pcie_setup_hw(struct oxnas_pcie *pcie)
 {
        /* We won't have any inbound address translation. This allows PCI
         * devices to access anywhere in the AHB address map. Might be regarded
@@ -362,7 +346,7 @@ static struct pci_ops oxnas_pcie_ops = {
        .write = oxnas_pcie_wr_conf,
 };
 
-static int __init oxnas_pcie_setup(int nr, struct pci_sys_data *sys)
+static int oxnas_pcie_setup(int nr, struct pci_sys_data *sys)
 {
        struct oxnas_pcie *pcie = sys_to_pcie(sys);
 
@@ -384,7 +368,7 @@ static int __init oxnas_pcie_setup(int nr, struct pci_sys_data *sys)
        return 1;
 }
 
-static void __init oxnas_pcie_enable(struct device *dev, struct oxnas_pcie *pcie)
+static void oxnas_pcie_enable(struct device *dev, struct oxnas_pcie *pcie)
 {
        struct hw_pci hw;
        int i;
@@ -404,52 +388,11 @@ static void __init oxnas_pcie_enable(struct device *dev, struct oxnas_pcie *pcie
        pci_common_init_dev(dev, &hw);
 }
 
-void oxnas_pcie_init_shared_hw(struct platform_device *pdev,
-                              void __iomem *phybase,
-                              struct regmap *sys_ctrl)
-{
-       struct reset_control *rstc;
-       int ret;
-
-       /* generate clocks from HCSL buffers, shared parts */
-       regmap_write(sys_ctrl, SYS_CTRL_HCSL_CTRL_REGOFFSET, HCSL_BIAS_ON|HCSL_PCIE_EN);
-
-       /* Ensure PCIe PHY is properly reset */
-       rstc = reset_control_get(&pdev->dev, "phy");
-       if (IS_ERR(rstc)) {
-               ret = PTR_ERR(rstc);
-       } else {
-               ret = reset_control_reset(rstc);
-               reset_control_put(rstc);
-       }
-
-       if (ret) {
-               dev_err(&pdev->dev, "phy reset failed %d\n", ret);
-               return;
-       }
-
-       /* Enable PCIe Pre-Emphasis: What these value means? */
-       writel(ADDR_VAL(0x0014), phybase + PHY_ADDR);
-       writel(DATA_VAL(0xce10) | CAP_DATA, phybase + PHY_DATA);
-       writel(DATA_VAL(0xce10) | WRITE_EN, phybase + PHY_DATA);
-
-       writel(ADDR_VAL(0x2004), phybase + PHY_ADDR);
-       writel(DATA_VAL(0x82c7) | CAP_DATA, phybase + PHY_DATA);
-       writel(DATA_VAL(0x82c7) | WRITE_EN, phybase + PHY_DATA);
-}
-
-static int oxnas_pcie_shared_init(struct platform_device *pdev, struct regmap *sys_ctrl)
+static int oxnas_pcie_shared_init(struct platform_device *pdev, struct oxnas_pcie *pcie)
 {
        if (++pcie_shared.refcount == 1) {
-               /* we are the first */
-               struct device_node *np = pdev->dev.of_node;
-               void __iomem *phy = of_iomap(np, 2);
-               if (!phy) {
-                       --pcie_shared.refcount;
-                       return -ENOMEM;
-               }
-               oxnas_pcie_init_shared_hw(pdev, phy, sys_ctrl);
-               iounmap(phy);
+               phy_init(pcie->phy);
+               phy_power_on(pcie->phy);
                return 0;
        } else {
                return 0;
@@ -466,7 +409,7 @@ static void oxnas_pcie_shared_deinit(struct platform_device *pdev)
 }
 #endif
 
-static int __init
+static int
 oxnas_pcie_map_registers(struct platform_device *pdev,
                         struct device_node *np,
                         struct oxnas_pcie *pcie)
@@ -476,7 +419,6 @@ oxnas_pcie_map_registers(struct platform_device *pdev,
        u32 outbound_ctrl_offset;
        u32 pcie_ctrl_offset;
 
-       /* 2 is reserved for shared phy */
        ret = of_address_to_resource(np, 0, &regs);
        if (ret)
                return -EINVAL;
@@ -491,6 +433,12 @@ oxnas_pcie_map_registers(struct platform_device *pdev,
        if (!pcie->inbound)
                return -ENOMEM;
 
+       pcie->phy = devm_of_phy_get(&pdev->dev, np, NULL);
+       if (IS_ERR(pcie->phy)) {
+               if (PTR_ERR(pcie->phy) == -EPROBE_DEFER)
+                       return PTR_ERR(pcie->phy);
+               pcie->phy = NULL;
+       }
 
        if (of_property_read_u32(np, "plxtech,pcie-outbound-offset",
                                 &outbound_ctrl_offset))
@@ -505,7 +453,7 @@ oxnas_pcie_map_registers(struct platform_device *pdev,
        return 0;
 }
 
-static int __init oxnas_pcie_init_res(struct platform_device *pdev,
+static int oxnas_pcie_init_res(struct platform_device *pdev,
                                      struct oxnas_pcie *pcie,
                                      struct device_node *np)
 {
@@ -587,6 +535,7 @@ static void oxnas_pcie_init_hw(struct platform_device *pdev,
                mdelay(100);
        }
 
+       /* ToDo: use phy power-on port... */
        regmap_update_bits(pcie->sys_ctrl, SYS_CTRL_HCSL_CTRL_REGOFFSET,
                           BIT(pcie->hcsl_en), BIT(pcie->hcsl_en));
 
@@ -624,7 +573,7 @@ static void oxnas_pcie_init_hw(struct platform_device *pdev,
        wmb();
 }
 
-static int __init oxnas_pcie_probe(struct platform_device *pdev)
+static int oxnas_pcie_probe(struct platform_device *pdev)
 {
        struct oxnas_pcie *pcie;
        struct device_node *np = pdev->dev.of_node;
@@ -662,7 +611,7 @@ static int __init oxnas_pcie_probe(struct platform_device *pdev)
                goto err_free_gpio;
        }
 
-       ret = oxnas_pcie_shared_init(pdev, pcie->sys_ctrl);
+       ret = oxnas_pcie_shared_init(pdev, pcie);
        if (ret)
                goto err_free_gpio;
 
@@ -693,25 +642,14 @@ static const struct of_device_id oxnas_pcie_of_match_table[] = {
        { .compatible = "plxtech,nas782x-pcie", },
        {},
 };
-MODULE_DEVICE_TABLE(of, oxnas_pcie_of_match_table);
 
 static struct platform_driver oxnas_pcie_driver = {
        .driver = {
-               .owner = THIS_MODULE,
                .name = "oxnas-pcie",
-               .of_match_table =
-                  of_match_ptr(oxnas_pcie_of_match_table),
+               .suppress_bind_attrs = true,
+               .of_match_table = oxnas_pcie_of_match_table,
        },
+       .probe = oxnas_pcie_probe,
 };
 
-static int __init oxnas_pcie_init(void)
-{
-       return platform_driver_probe(&oxnas_pcie_driver,
-                                    oxnas_pcie_probe);
-}
-
-subsys_initcall(oxnas_pcie_init);
-
-MODULE_AUTHOR("Ma Haijun <mahaijuns@gmail.com>");
-MODULE_DESCRIPTION("NAS782x PCIe driver");
-MODULE_LICENSE("GPLv2");
+builtin_platform_driver(oxnas_pcie_driver);