1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2016-2018 Broadcom
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/clk.h>
7*4882a593Smuzhiyun #include <linux/delay.h>
8*4882a593Smuzhiyun #include <linux/io.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
11*4882a593Smuzhiyun #include <linux/of.h>
12*4882a593Smuzhiyun #include <linux/phy/phy.h>
13*4882a593Smuzhiyun #include <linux/platform_device.h>
14*4882a593Smuzhiyun #include <linux/regmap.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun /* we have up to 8 PAXB based RC. The 9th one is always PAXC */
17*4882a593Smuzhiyun #define SR_NR_PCIE_PHYS 9
18*4882a593Smuzhiyun #define SR_PAXC_PHY_IDX (SR_NR_PCIE_PHYS - 1)
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define PCIE_PIPEMUX_CFG_OFFSET 0x10c
21*4882a593Smuzhiyun #define PCIE_PIPEMUX_SELECT_STRAP 0xf
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #define CDRU_STRAP_DATA_LSW_OFFSET 0x5c
24*4882a593Smuzhiyun #define PCIE_PIPEMUX_SHIFT 19
25*4882a593Smuzhiyun #define PCIE_PIPEMUX_MASK 0xf
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #define MHB_MEM_PW_PAXC_OFFSET 0x1c0
28*4882a593Smuzhiyun #define MHB_PWR_ARR_POWERON 0x8
29*4882a593Smuzhiyun #define MHB_PWR_ARR_POWEROK 0x4
30*4882a593Smuzhiyun #define MHB_PWR_POWERON 0x2
31*4882a593Smuzhiyun #define MHB_PWR_POWEROK 0x1
32*4882a593Smuzhiyun #define MHB_PWR_STATUS_MASK (MHB_PWR_ARR_POWERON | \
33*4882a593Smuzhiyun MHB_PWR_ARR_POWEROK | \
34*4882a593Smuzhiyun MHB_PWR_POWERON | \
35*4882a593Smuzhiyun MHB_PWR_POWEROK)
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun struct sr_pcie_phy_core;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun /**
40*4882a593Smuzhiyun * struct sr_pcie_phy - Stingray PCIe PHY
41*4882a593Smuzhiyun *
42*4882a593Smuzhiyun * @core: pointer to the Stingray PCIe PHY core control
43*4882a593Smuzhiyun * @index: PHY index
44*4882a593Smuzhiyun * @phy: pointer to the kernel PHY device
45*4882a593Smuzhiyun */
46*4882a593Smuzhiyun struct sr_pcie_phy {
47*4882a593Smuzhiyun struct sr_pcie_phy_core *core;
48*4882a593Smuzhiyun unsigned int index;
49*4882a593Smuzhiyun struct phy *phy;
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun * struct sr_pcie_phy_core - Stingray PCIe PHY core control
54*4882a593Smuzhiyun *
55*4882a593Smuzhiyun * @dev: pointer to device
56*4882a593Smuzhiyun * @base: base register of PCIe SS
57*4882a593Smuzhiyun * @cdru: regmap to the CDRU device
58*4882a593Smuzhiyun * @mhb: regmap to the MHB device
59*4882a593Smuzhiyun * @pipemux: pipemuex strap
60*4882a593Smuzhiyun * @phys: array of PCIe PHYs
61*4882a593Smuzhiyun */
62*4882a593Smuzhiyun struct sr_pcie_phy_core {
63*4882a593Smuzhiyun struct device *dev;
64*4882a593Smuzhiyun void __iomem *base;
65*4882a593Smuzhiyun struct regmap *cdru;
66*4882a593Smuzhiyun struct regmap *mhb;
67*4882a593Smuzhiyun u32 pipemux;
68*4882a593Smuzhiyun struct sr_pcie_phy phys[SR_NR_PCIE_PHYS];
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun * PCIe PIPEMUX lookup table
73*4882a593Smuzhiyun *
74*4882a593Smuzhiyun * Each array index represents a PIPEMUX strap setting
75*4882a593Smuzhiyun * The array element represents a bitmap where a set bit means the PCIe
76*4882a593Smuzhiyun * core and associated serdes has been enabled as RC and is available for use
77*4882a593Smuzhiyun */
78*4882a593Smuzhiyun static const u8 pipemux_table[] = {
79*4882a593Smuzhiyun /* PIPEMUX = 0, EP 1x16 */
80*4882a593Smuzhiyun 0x00,
81*4882a593Smuzhiyun /* PIPEMUX = 1, EP 1x8 + RC 1x8, core 7 */
82*4882a593Smuzhiyun 0x80,
83*4882a593Smuzhiyun /* PIPEMUX = 2, EP 4x4 */
84*4882a593Smuzhiyun 0x00,
85*4882a593Smuzhiyun /* PIPEMUX = 3, RC 2x8, cores 0, 7 */
86*4882a593Smuzhiyun 0x81,
87*4882a593Smuzhiyun /* PIPEMUX = 4, RC 4x4, cores 0, 1, 6, 7 */
88*4882a593Smuzhiyun 0xc3,
89*4882a593Smuzhiyun /* PIPEMUX = 5, RC 8x2, all 8 cores */
90*4882a593Smuzhiyun 0xff,
91*4882a593Smuzhiyun /* PIPEMUX = 6, RC 3x4 + 2x2, cores 0, 2, 3, 6, 7 */
92*4882a593Smuzhiyun 0xcd,
93*4882a593Smuzhiyun /* PIPEMUX = 7, RC 1x4 + 6x2, cores 0, 2, 3, 4, 5, 6, 7 */
94*4882a593Smuzhiyun 0xfd,
95*4882a593Smuzhiyun /* PIPEMUX = 8, EP 1x8 + RC 4x2, cores 4, 5, 6, 7 */
96*4882a593Smuzhiyun 0xf0,
97*4882a593Smuzhiyun /* PIPEMUX = 9, EP 1x8 + RC 2x4, cores 6, 7 */
98*4882a593Smuzhiyun 0xc0,
99*4882a593Smuzhiyun /* PIPEMUX = 10, EP 2x4 + RC 2x4, cores 1, 6 */
100*4882a593Smuzhiyun 0x42,
101*4882a593Smuzhiyun /* PIPEMUX = 11, EP 2x4 + RC 4x2, cores 2, 3, 4, 5 */
102*4882a593Smuzhiyun 0x3c,
103*4882a593Smuzhiyun /* PIPEMUX = 12, EP 1x4 + RC 6x2, cores 2, 3, 4, 5, 6, 7 */
104*4882a593Smuzhiyun 0xfc,
105*4882a593Smuzhiyun /* PIPEMUX = 13, RC 2x4 + RC 1x4 + 2x2, cores 2, 3, 6 */
106*4882a593Smuzhiyun 0x4c,
107*4882a593Smuzhiyun };
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /*
110*4882a593Smuzhiyun * Return true if the strap setting is valid
111*4882a593Smuzhiyun */
pipemux_strap_is_valid(u32 pipemux)112*4882a593Smuzhiyun static bool pipemux_strap_is_valid(u32 pipemux)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun return !!(pipemux < ARRAY_SIZE(pipemux_table));
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun * Read the PCIe PIPEMUX from strap
119*4882a593Smuzhiyun */
pipemux_strap_read(struct sr_pcie_phy_core * core)120*4882a593Smuzhiyun static u32 pipemux_strap_read(struct sr_pcie_phy_core *core)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun u32 pipemux;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /*
125*4882a593Smuzhiyun * Read PIPEMUX configuration register to determine the pipemux setting
126*4882a593Smuzhiyun *
127*4882a593Smuzhiyun * In the case when the value indicates using HW strap, fall back to
128*4882a593Smuzhiyun * use HW strap
129*4882a593Smuzhiyun */
130*4882a593Smuzhiyun pipemux = readl(core->base + PCIE_PIPEMUX_CFG_OFFSET);
131*4882a593Smuzhiyun pipemux &= PCIE_PIPEMUX_MASK;
132*4882a593Smuzhiyun if (pipemux == PCIE_PIPEMUX_SELECT_STRAP) {
133*4882a593Smuzhiyun regmap_read(core->cdru, CDRU_STRAP_DATA_LSW_OFFSET, &pipemux);
134*4882a593Smuzhiyun pipemux >>= PCIE_PIPEMUX_SHIFT;
135*4882a593Smuzhiyun pipemux &= PCIE_PIPEMUX_MASK;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun return pipemux;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun * Given a PIPEMUX strap and PCIe core index, this function returns true if the
143*4882a593Smuzhiyun * PCIe core needs to be enabled
144*4882a593Smuzhiyun */
pcie_core_is_for_rc(struct sr_pcie_phy * phy)145*4882a593Smuzhiyun static bool pcie_core_is_for_rc(struct sr_pcie_phy *phy)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun struct sr_pcie_phy_core *core = phy->core;
148*4882a593Smuzhiyun unsigned int core_idx = phy->index;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun return !!((pipemux_table[core->pipemux] >> core_idx) & 0x1);
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
sr_pcie_phy_init(struct phy * p)153*4882a593Smuzhiyun static int sr_pcie_phy_init(struct phy *p)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun struct sr_pcie_phy *phy = phy_get_drvdata(p);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /*
158*4882a593Smuzhiyun * Check whether this PHY is for root complex or not. If yes, return
159*4882a593Smuzhiyun * zero so the host driver can proceed to enumeration. If not, return
160*4882a593Smuzhiyun * an error and that will force the host driver to bail out
161*4882a593Smuzhiyun */
162*4882a593Smuzhiyun if (pcie_core_is_for_rc(phy))
163*4882a593Smuzhiyun return 0;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun return -ENODEV;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
sr_paxc_phy_init(struct phy * p)168*4882a593Smuzhiyun static int sr_paxc_phy_init(struct phy *p)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun struct sr_pcie_phy *phy = phy_get_drvdata(p);
171*4882a593Smuzhiyun struct sr_pcie_phy_core *core = phy->core;
172*4882a593Smuzhiyun unsigned int core_idx = phy->index;
173*4882a593Smuzhiyun u32 val;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun if (core_idx != SR_PAXC_PHY_IDX)
176*4882a593Smuzhiyun return -EINVAL;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun regmap_read(core->mhb, MHB_MEM_PW_PAXC_OFFSET, &val);
179*4882a593Smuzhiyun if ((val & MHB_PWR_STATUS_MASK) != MHB_PWR_STATUS_MASK) {
180*4882a593Smuzhiyun dev_err(core->dev, "PAXC is not powered up\n");
181*4882a593Smuzhiyun return -ENODEV;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun return 0;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun static const struct phy_ops sr_pcie_phy_ops = {
188*4882a593Smuzhiyun .init = sr_pcie_phy_init,
189*4882a593Smuzhiyun .owner = THIS_MODULE,
190*4882a593Smuzhiyun };
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun static const struct phy_ops sr_paxc_phy_ops = {
193*4882a593Smuzhiyun .init = sr_paxc_phy_init,
194*4882a593Smuzhiyun .owner = THIS_MODULE,
195*4882a593Smuzhiyun };
196*4882a593Smuzhiyun
sr_pcie_phy_xlate(struct device * dev,struct of_phandle_args * args)197*4882a593Smuzhiyun static struct phy *sr_pcie_phy_xlate(struct device *dev,
198*4882a593Smuzhiyun struct of_phandle_args *args)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun struct sr_pcie_phy_core *core;
201*4882a593Smuzhiyun int phy_idx;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun core = dev_get_drvdata(dev);
204*4882a593Smuzhiyun if (!core)
205*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun phy_idx = args->args[0];
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (WARN_ON(phy_idx >= SR_NR_PCIE_PHYS))
210*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun return core->phys[phy_idx].phy;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
sr_pcie_phy_probe(struct platform_device * pdev)215*4882a593Smuzhiyun static int sr_pcie_phy_probe(struct platform_device *pdev)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun struct device *dev = &pdev->dev;
218*4882a593Smuzhiyun struct device_node *node = dev->of_node;
219*4882a593Smuzhiyun struct sr_pcie_phy_core *core;
220*4882a593Smuzhiyun struct resource *res;
221*4882a593Smuzhiyun struct phy_provider *provider;
222*4882a593Smuzhiyun unsigned int phy_idx = 0;
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
225*4882a593Smuzhiyun if (!core)
226*4882a593Smuzhiyun return -ENOMEM;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun core->dev = dev;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
231*4882a593Smuzhiyun core->base = devm_ioremap_resource(core->dev, res);
232*4882a593Smuzhiyun if (IS_ERR(core->base))
233*4882a593Smuzhiyun return PTR_ERR(core->base);
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun core->cdru = syscon_regmap_lookup_by_phandle(node, "brcm,sr-cdru");
236*4882a593Smuzhiyun if (IS_ERR(core->cdru)) {
237*4882a593Smuzhiyun dev_err(core->dev, "unable to find CDRU device\n");
238*4882a593Smuzhiyun return PTR_ERR(core->cdru);
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun core->mhb = syscon_regmap_lookup_by_phandle(node, "brcm,sr-mhb");
242*4882a593Smuzhiyun if (IS_ERR(core->mhb)) {
243*4882a593Smuzhiyun dev_err(core->dev, "unable to find MHB device\n");
244*4882a593Smuzhiyun return PTR_ERR(core->mhb);
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /* read the PCIe PIPEMUX strap setting */
248*4882a593Smuzhiyun core->pipemux = pipemux_strap_read(core);
249*4882a593Smuzhiyun if (!pipemux_strap_is_valid(core->pipemux)) {
250*4882a593Smuzhiyun dev_err(core->dev, "invalid PCIe PIPEMUX strap %u\n",
251*4882a593Smuzhiyun core->pipemux);
252*4882a593Smuzhiyun return -EIO;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun for (phy_idx = 0; phy_idx < SR_NR_PCIE_PHYS; phy_idx++) {
256*4882a593Smuzhiyun struct sr_pcie_phy *p = &core->phys[phy_idx];
257*4882a593Smuzhiyun const struct phy_ops *ops;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun if (phy_idx == SR_PAXC_PHY_IDX)
260*4882a593Smuzhiyun ops = &sr_paxc_phy_ops;
261*4882a593Smuzhiyun else
262*4882a593Smuzhiyun ops = &sr_pcie_phy_ops;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun p->phy = devm_phy_create(dev, NULL, ops);
265*4882a593Smuzhiyun if (IS_ERR(p->phy)) {
266*4882a593Smuzhiyun dev_err(dev, "failed to create PCIe PHY\n");
267*4882a593Smuzhiyun return PTR_ERR(p->phy);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun p->core = core;
271*4882a593Smuzhiyun p->index = phy_idx;
272*4882a593Smuzhiyun phy_set_drvdata(p->phy, p);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun dev_set_drvdata(dev, core);
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun provider = devm_of_phy_provider_register(dev, sr_pcie_phy_xlate);
278*4882a593Smuzhiyun if (IS_ERR(provider)) {
279*4882a593Smuzhiyun dev_err(dev, "failed to register PHY provider\n");
280*4882a593Smuzhiyun return PTR_ERR(provider);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun dev_info(dev, "Stingray PCIe PHY driver initialized\n");
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun return 0;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun static const struct of_device_id sr_pcie_phy_match_table[] = {
289*4882a593Smuzhiyun { .compatible = "brcm,sr-pcie-phy" },
290*4882a593Smuzhiyun { }
291*4882a593Smuzhiyun };
292*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, sr_pcie_phy_match_table);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun static struct platform_driver sr_pcie_phy_driver = {
295*4882a593Smuzhiyun .driver = {
296*4882a593Smuzhiyun .name = "sr-pcie-phy",
297*4882a593Smuzhiyun .of_match_table = sr_pcie_phy_match_table,
298*4882a593Smuzhiyun },
299*4882a593Smuzhiyun .probe = sr_pcie_phy_probe,
300*4882a593Smuzhiyun };
301*4882a593Smuzhiyun module_platform_driver(sr_pcie_phy_driver);
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun MODULE_AUTHOR("Ray Jui <ray.jui@broadcom.com>");
304*4882a593Smuzhiyun MODULE_DESCRIPTION("Broadcom Stingray PCIe PHY driver");
305*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
306