1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2015 Broadcom Corporation
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or
5*4882a593Smuzhiyun * modify it under the terms of the GNU General Public License as
6*4882a593Smuzhiyun * published by the Free Software Foundation version 2.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9*4882a593Smuzhiyun * kind, whether express or implied; without even the implied warranty
10*4882a593Smuzhiyun * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11*4882a593Smuzhiyun * GNU General Public License for more details.
12*4882a593Smuzhiyun */
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/delay.h>
15*4882a593Smuzhiyun #include <linux/io.h>
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun #include <linux/of.h>
18*4882a593Smuzhiyun #include <linux/phy/phy.h>
19*4882a593Smuzhiyun #include <linux/platform_device.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define PCIE_CFG_OFFSET 0x00
22*4882a593Smuzhiyun #define PCIE1_PHY_IDDQ_SHIFT 10
23*4882a593Smuzhiyun #define PCIE0_PHY_IDDQ_SHIFT 2
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun enum cygnus_pcie_phy_id {
26*4882a593Smuzhiyun CYGNUS_PHY_PCIE0 = 0,
27*4882a593Smuzhiyun CYGNUS_PHY_PCIE1,
28*4882a593Smuzhiyun MAX_NUM_PHYS,
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun struct cygnus_pcie_phy_core;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /**
34*4882a593Smuzhiyun * struct cygnus_pcie_phy - Cygnus PCIe PHY device
35*4882a593Smuzhiyun * @core: pointer to the Cygnus PCIe PHY core control
36*4882a593Smuzhiyun * @id: internal ID to identify the Cygnus PCIe PHY
37*4882a593Smuzhiyun * @phy: pointer to the kernel PHY device
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun struct cygnus_pcie_phy {
40*4882a593Smuzhiyun struct cygnus_pcie_phy_core *core;
41*4882a593Smuzhiyun enum cygnus_pcie_phy_id id;
42*4882a593Smuzhiyun struct phy *phy;
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /**
46*4882a593Smuzhiyun * struct cygnus_pcie_phy_core - Cygnus PCIe PHY core control
47*4882a593Smuzhiyun * @dev: pointer to device
48*4882a593Smuzhiyun * @base: base register
49*4882a593Smuzhiyun * @lock: mutex to protect access to individual PHYs
50*4882a593Smuzhiyun * @phys: pointer to Cygnus PHY device
51*4882a593Smuzhiyun */
52*4882a593Smuzhiyun struct cygnus_pcie_phy_core {
53*4882a593Smuzhiyun struct device *dev;
54*4882a593Smuzhiyun void __iomem *base;
55*4882a593Smuzhiyun struct mutex lock;
56*4882a593Smuzhiyun struct cygnus_pcie_phy phys[MAX_NUM_PHYS];
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun
cygnus_pcie_power_config(struct cygnus_pcie_phy * phy,bool enable)59*4882a593Smuzhiyun static int cygnus_pcie_power_config(struct cygnus_pcie_phy *phy, bool enable)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun struct cygnus_pcie_phy_core *core = phy->core;
62*4882a593Smuzhiyun unsigned shift;
63*4882a593Smuzhiyun u32 val;
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun mutex_lock(&core->lock);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun switch (phy->id) {
68*4882a593Smuzhiyun case CYGNUS_PHY_PCIE0:
69*4882a593Smuzhiyun shift = PCIE0_PHY_IDDQ_SHIFT;
70*4882a593Smuzhiyun break;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun case CYGNUS_PHY_PCIE1:
73*4882a593Smuzhiyun shift = PCIE1_PHY_IDDQ_SHIFT;
74*4882a593Smuzhiyun break;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun default:
77*4882a593Smuzhiyun mutex_unlock(&core->lock);
78*4882a593Smuzhiyun dev_err(core->dev, "PCIe PHY %d invalid\n", phy->id);
79*4882a593Smuzhiyun return -EINVAL;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun if (enable) {
83*4882a593Smuzhiyun val = readl(core->base + PCIE_CFG_OFFSET);
84*4882a593Smuzhiyun val &= ~BIT(shift);
85*4882a593Smuzhiyun writel(val, core->base + PCIE_CFG_OFFSET);
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * Wait 50 ms for the PCIe Serdes to stabilize after the analog
88*4882a593Smuzhiyun * front end is brought up
89*4882a593Smuzhiyun */
90*4882a593Smuzhiyun msleep(50);
91*4882a593Smuzhiyun } else {
92*4882a593Smuzhiyun val = readl(core->base + PCIE_CFG_OFFSET);
93*4882a593Smuzhiyun val |= BIT(shift);
94*4882a593Smuzhiyun writel(val, core->base + PCIE_CFG_OFFSET);
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun mutex_unlock(&core->lock);
98*4882a593Smuzhiyun dev_dbg(core->dev, "PCIe PHY %d %s\n", phy->id,
99*4882a593Smuzhiyun enable ? "enabled" : "disabled");
100*4882a593Smuzhiyun return 0;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
cygnus_pcie_phy_power_on(struct phy * p)103*4882a593Smuzhiyun static int cygnus_pcie_phy_power_on(struct phy *p)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun struct cygnus_pcie_phy *phy = phy_get_drvdata(p);
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun return cygnus_pcie_power_config(phy, true);
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
cygnus_pcie_phy_power_off(struct phy * p)110*4882a593Smuzhiyun static int cygnus_pcie_phy_power_off(struct phy *p)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun struct cygnus_pcie_phy *phy = phy_get_drvdata(p);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun return cygnus_pcie_power_config(phy, false);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun static const struct phy_ops cygnus_pcie_phy_ops = {
118*4882a593Smuzhiyun .power_on = cygnus_pcie_phy_power_on,
119*4882a593Smuzhiyun .power_off = cygnus_pcie_phy_power_off,
120*4882a593Smuzhiyun .owner = THIS_MODULE,
121*4882a593Smuzhiyun };
122*4882a593Smuzhiyun
cygnus_pcie_phy_probe(struct platform_device * pdev)123*4882a593Smuzhiyun static int cygnus_pcie_phy_probe(struct platform_device *pdev)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun struct device *dev = &pdev->dev;
126*4882a593Smuzhiyun struct device_node *node = dev->of_node, *child;
127*4882a593Smuzhiyun struct cygnus_pcie_phy_core *core;
128*4882a593Smuzhiyun struct phy_provider *provider;
129*4882a593Smuzhiyun struct resource *res;
130*4882a593Smuzhiyun unsigned cnt = 0;
131*4882a593Smuzhiyun int ret;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (of_get_child_count(node) == 0) {
134*4882a593Smuzhiyun dev_err(dev, "PHY no child node\n");
135*4882a593Smuzhiyun return -ENODEV;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
139*4882a593Smuzhiyun if (!core)
140*4882a593Smuzhiyun return -ENOMEM;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun core->dev = dev;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
145*4882a593Smuzhiyun core->base = devm_ioremap_resource(dev, res);
146*4882a593Smuzhiyun if (IS_ERR(core->base))
147*4882a593Smuzhiyun return PTR_ERR(core->base);
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun mutex_init(&core->lock);
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun for_each_available_child_of_node(node, child) {
152*4882a593Smuzhiyun unsigned int id;
153*4882a593Smuzhiyun struct cygnus_pcie_phy *p;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun if (of_property_read_u32(child, "reg", &id)) {
156*4882a593Smuzhiyun dev_err(dev, "missing reg property for %pOFn\n",
157*4882a593Smuzhiyun child);
158*4882a593Smuzhiyun ret = -EINVAL;
159*4882a593Smuzhiyun goto put_child;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun if (id >= MAX_NUM_PHYS) {
163*4882a593Smuzhiyun dev_err(dev, "invalid PHY id: %u\n", id);
164*4882a593Smuzhiyun ret = -EINVAL;
165*4882a593Smuzhiyun goto put_child;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun if (core->phys[id].phy) {
169*4882a593Smuzhiyun dev_err(dev, "duplicated PHY id: %u\n", id);
170*4882a593Smuzhiyun ret = -EINVAL;
171*4882a593Smuzhiyun goto put_child;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun p = &core->phys[id];
175*4882a593Smuzhiyun p->phy = devm_phy_create(dev, child, &cygnus_pcie_phy_ops);
176*4882a593Smuzhiyun if (IS_ERR(p->phy)) {
177*4882a593Smuzhiyun dev_err(dev, "failed to create PHY\n");
178*4882a593Smuzhiyun ret = PTR_ERR(p->phy);
179*4882a593Smuzhiyun goto put_child;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun p->core = core;
183*4882a593Smuzhiyun p->id = id;
184*4882a593Smuzhiyun phy_set_drvdata(p->phy, p);
185*4882a593Smuzhiyun cnt++;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun dev_set_drvdata(dev, core);
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
191*4882a593Smuzhiyun if (IS_ERR(provider)) {
192*4882a593Smuzhiyun dev_err(dev, "failed to register PHY provider\n");
193*4882a593Smuzhiyun return PTR_ERR(provider);
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun dev_dbg(dev, "registered %u PCIe PHY(s)\n", cnt);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun return 0;
199*4882a593Smuzhiyun put_child:
200*4882a593Smuzhiyun of_node_put(child);
201*4882a593Smuzhiyun return ret;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun static const struct of_device_id cygnus_pcie_phy_match_table[] = {
205*4882a593Smuzhiyun { .compatible = "brcm,cygnus-pcie-phy" },
206*4882a593Smuzhiyun { /* sentinel */ }
207*4882a593Smuzhiyun };
208*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, cygnus_pcie_phy_match_table);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun static struct platform_driver cygnus_pcie_phy_driver = {
211*4882a593Smuzhiyun .driver = {
212*4882a593Smuzhiyun .name = "cygnus-pcie-phy",
213*4882a593Smuzhiyun .of_match_table = cygnus_pcie_phy_match_table,
214*4882a593Smuzhiyun },
215*4882a593Smuzhiyun .probe = cygnus_pcie_phy_probe,
216*4882a593Smuzhiyun };
217*4882a593Smuzhiyun module_platform_driver(cygnus_pcie_phy_driver);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
220*4882a593Smuzhiyun MODULE_DESCRIPTION("Broadcom Cygnus PCIe PHY driver");
221*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
222