1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas R-Car Gen3 PCIe PHY driver
5 * Copyright (C) 2018 Cogent Embedded, Inc.
10 #include <linux/module.h>
12 #include <linux/phy/phy.h>
13 #include <linux/platform_device.h>
14 #include <linux/spinlock.h>
16 #define PHY_CTRL 0x4000 /* R8A77980 only */
18 /* PHY control register (PHY_CTRL) */
19 #define PHY_CTRL_PHY_PWDN BIT(2)
21 struct rcar_gen3_phy {
27 static void rcar_gen3_phy_pcie_modify_reg(struct phy *p, unsigned int reg,
30 struct rcar_gen3_phy *phy = phy_get_drvdata(p);
31 void __iomem *base = phy->base;
35 spin_lock_irqsave(&phy->lock, flags);
37 value = readl(base + reg);
40 writel(value, base + reg);
42 spin_unlock_irqrestore(&phy->lock, flags);
45 static int r8a77980_phy_pcie_power_on(struct phy *p)
47 /* Power on the PCIe PHY */
48 rcar_gen3_phy_pcie_modify_reg(p, PHY_CTRL, PHY_CTRL_PHY_PWDN, 0);
53 static int r8a77980_phy_pcie_power_off(struct phy *p)
55 /* Power off the PCIe PHY */
56 rcar_gen3_phy_pcie_modify_reg(p, PHY_CTRL, 0, PHY_CTRL_PHY_PWDN);
61 static const struct phy_ops r8a77980_phy_pcie_ops = {
62 .power_on = r8a77980_phy_pcie_power_on,
63 .power_off = r8a77980_phy_pcie_power_off,
67 static const struct of_device_id rcar_gen3_phy_pcie_match_table[] = {
68 { .compatible = "renesas,r8a77980-pcie-phy" },
71 MODULE_DEVICE_TABLE(of, rcar_gen3_phy_pcie_match_table);
73 static int rcar_gen3_phy_pcie_probe(struct platform_device *pdev)
75 struct device *dev = &pdev->dev;
76 struct phy_provider *provider;
77 struct rcar_gen3_phy *phy;
83 "This driver must only be instantiated from the device tree\n");
87 base = devm_platform_ioremap_resource(pdev, 0);
91 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
95 spin_lock_init(&phy->lock);
100 * devm_phy_create() will call pm_runtime_enable(&phy->dev);
101 * And then, phy-core will manage runtime PM for this device.
103 pm_runtime_enable(dev);
105 phy->phy = devm_phy_create(dev, NULL, &r8a77980_phy_pcie_ops);
106 if (IS_ERR(phy->phy)) {
107 dev_err(dev, "Failed to create PCIe PHY\n");
108 error = PTR_ERR(phy->phy);
111 phy_set_drvdata(phy->phy, phy);
113 provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
114 if (IS_ERR(provider)) {
115 dev_err(dev, "Failed to register PHY provider\n");
116 error = PTR_ERR(provider);
123 pm_runtime_disable(dev);
128 static void rcar_gen3_phy_pcie_remove(struct platform_device *pdev)
130 pm_runtime_disable(&pdev->dev);
133 static struct platform_driver rcar_gen3_phy_driver = {
135 .name = "phy_rcar_gen3_pcie",
136 .of_match_table = rcar_gen3_phy_pcie_match_table,
138 .probe = rcar_gen3_phy_pcie_probe,
139 .remove_new = rcar_gen3_phy_pcie_remove,
142 module_platform_driver(rcar_gen3_phy_driver);
144 MODULE_LICENSE("GPL v2");
145 MODULE_DESCRIPTION("Renesas R-Car Gen3 PCIe PHY");
146 MODULE_AUTHOR("Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>");