xref: /linux/drivers/phy/st/phy-stm32-combophy.c (revision 47e1bb6b4ba0987139ab790efa03c542ebc1b10d)
1*47e1bb6bSChristian Bruel // SPDX-License-Identifier: GPL-2.0-only
2*47e1bb6bSChristian Bruel /*
3*47e1bb6bSChristian Bruel  * STMicroelectronics COMBOPHY STM32MP25 Controller driver.
4*47e1bb6bSChristian Bruel  *
5*47e1bb6bSChristian Bruel  * Copyright (C) 2024 STMicroelectronics
6*47e1bb6bSChristian Bruel  * Author: Christian Bruel <christian.bruel@foss.st.com>
7*47e1bb6bSChristian Bruel  */
8*47e1bb6bSChristian Bruel 
9*47e1bb6bSChristian Bruel #include <linux/bitfield.h>
10*47e1bb6bSChristian Bruel #include <linux/clk.h>
11*47e1bb6bSChristian Bruel #include <linux/mfd/syscon.h>
12*47e1bb6bSChristian Bruel #include <linux/platform_device.h>
13*47e1bb6bSChristian Bruel #include <linux/phy/phy.h>
14*47e1bb6bSChristian Bruel #include <linux/pm_runtime.h>
15*47e1bb6bSChristian Bruel #include <linux/regmap.h>
16*47e1bb6bSChristian Bruel #include <linux/reset.h>
17*47e1bb6bSChristian Bruel #include <dt-bindings/phy/phy.h>
18*47e1bb6bSChristian Bruel 
19*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR1 0x4c00
20*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR2 0x4c04
21*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR4 0x4c0c
22*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR5 0x4c10
23*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_SR  0x4c14
24*47e1bb6bSChristian Bruel #define SYSCFG_PCIEPRGCR    0x6080
25*47e1bb6bSChristian Bruel 
26*47e1bb6bSChristian Bruel /* SYSCFG PCIEPRGCR */
27*47e1bb6bSChristian Bruel #define STM32MP25_PCIEPRGCR_EN	  BIT(0)
28*47e1bb6bSChristian Bruel #define STM32MP25_PCIEPRG_IMPCTRL_OHM     GENMASK(3, 1)
29*47e1bb6bSChristian Bruel #define STM32MP25_PCIEPRG_IMPCTRL_VSWING  GENMASK(5, 4)
30*47e1bb6bSChristian Bruel 
31*47e1bb6bSChristian Bruel /* SYSCFG SYSCFG_COMBOPHY_SR */
32*47e1bb6bSChristian Bruel #define STM32MP25_PIPE0_PHYSTATUS BIT(1)
33*47e1bb6bSChristian Bruel 
34*47e1bb6bSChristian Bruel /* SYSCFG CR1 */
35*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR1_REFUSEPAD BIT(0)
36*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR1_MPLLMULT GENMASK(7, 1)
37*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR1_REFCLKSEL GENMASK(16, 8)
38*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR1_REFCLKDIV2 BIT(17)
39*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR1_REFSSPEN BIT(18)
40*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR1_SSCEN BIT(19)
41*47e1bb6bSChristian Bruel 
42*47e1bb6bSChristian Bruel /* SYSCFG CR4 */
43*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR4_RX0_EQ GENMASK(2, 0)
44*47e1bb6bSChristian Bruel 
45*47e1bb6bSChristian Bruel #define MPLLMULT_19_2 (0x02u << 1)
46*47e1bb6bSChristian Bruel #define MPLLMULT_20   (0x7du << 1)
47*47e1bb6bSChristian Bruel #define MPLLMULT_24   (0x68u << 1)
48*47e1bb6bSChristian Bruel #define MPLLMULT_25   (0x64u << 1)
49*47e1bb6bSChristian Bruel #define MPLLMULT_26   (0x60u << 1)
50*47e1bb6bSChristian Bruel #define MPLLMULT_38_4 (0x41u << 1)
51*47e1bb6bSChristian Bruel #define MPLLMULT_48   (0x6cu << 1)
52*47e1bb6bSChristian Bruel #define MPLLMULT_50   (0x32u << 1)
53*47e1bb6bSChristian Bruel #define MPLLMULT_52   (0x30u << 1)
54*47e1bb6bSChristian Bruel #define MPLLMULT_100  (0x19u << 1)
55*47e1bb6bSChristian Bruel 
56*47e1bb6bSChristian Bruel #define REFCLKSEL_0   0
57*47e1bb6bSChristian Bruel #define REFCLKSEL_1   (0x108u << 8)
58*47e1bb6bSChristian Bruel 
59*47e1bb6bSChristian Bruel #define REFCLDIV_0    0
60*47e1bb6bSChristian Bruel 
61*47e1bb6bSChristian Bruel /* SYSCFG CR2 */
62*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR2_MODESEL GENMASK(1, 0)
63*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR2_ISO_DIS BIT(15)
64*47e1bb6bSChristian Bruel 
65*47e1bb6bSChristian Bruel #define COMBOPHY_MODESEL_PCIE 0
66*47e1bb6bSChristian Bruel #define COMBOPHY_MODESEL_USB  3
67*47e1bb6bSChristian Bruel 
68*47e1bb6bSChristian Bruel /* SYSCFG CR5 */
69*47e1bb6bSChristian Bruel #define SYSCFG_COMBOPHY_CR5_COMMON_CLOCKS BIT(12)
70*47e1bb6bSChristian Bruel 
71*47e1bb6bSChristian Bruel #define COMBOPHY_SUP_ANA_MPLL_LOOP_CTL 0xc0
72*47e1bb6bSChristian Bruel #define COMBOPHY_PROP_CNTRL GENMASK(7, 4)
73*47e1bb6bSChristian Bruel 
74*47e1bb6bSChristian Bruel /* Required apb/ker clocks first, optional pad last. */
75*47e1bb6bSChristian Bruel static const char * const combophy_clks[] = {"apb", "ker", "pad"};
76*47e1bb6bSChristian Bruel #define APB_CLK 0
77*47e1bb6bSChristian Bruel #define KER_CLK 1
78*47e1bb6bSChristian Bruel #define PAD_CLK 2
79*47e1bb6bSChristian Bruel 
80*47e1bb6bSChristian Bruel struct stm32_combophy {
81*47e1bb6bSChristian Bruel 	struct phy *phy;
82*47e1bb6bSChristian Bruel 	struct regmap *regmap;
83*47e1bb6bSChristian Bruel 	struct device *dev;
84*47e1bb6bSChristian Bruel 	void __iomem *base;
85*47e1bb6bSChristian Bruel 	struct reset_control *phy_reset;
86*47e1bb6bSChristian Bruel 	struct clk_bulk_data clks[ARRAY_SIZE(combophy_clks)];
87*47e1bb6bSChristian Bruel 	int num_clks;
88*47e1bb6bSChristian Bruel 	bool have_pad_clk;
89*47e1bb6bSChristian Bruel 	unsigned int type;
90*47e1bb6bSChristian Bruel 	bool is_init;
91*47e1bb6bSChristian Bruel 	int irq_wakeup;
92*47e1bb6bSChristian Bruel };
93*47e1bb6bSChristian Bruel 
94*47e1bb6bSChristian Bruel struct clk_impedance  {
95*47e1bb6bSChristian Bruel 	u32 microohm;
96*47e1bb6bSChristian Bruel 	u32 vswing[4];
97*47e1bb6bSChristian Bruel };
98*47e1bb6bSChristian Bruel 
99*47e1bb6bSChristian Bruel /*
100*47e1bb6bSChristian Bruel  * lookup table to hold the settings needed for a ref clock frequency
101*47e1bb6bSChristian Bruel  * impedance, the offset is used to set the IMP_CTL and DE_EMP bit of the
102*47e1bb6bSChristian Bruel  * PRG_IMP_CTRL register. Use ordered discrete values in the table
103*47e1bb6bSChristian Bruel  */
104*47e1bb6bSChristian Bruel static const struct clk_impedance imp_lookup[] = {
105*47e1bb6bSChristian Bruel 	{ 6090000, { 442000, 564000, 684000, 802000 } },
106*47e1bb6bSChristian Bruel 	{ 5662000, { 528000, 621000, 712000, 803000 } },
107*47e1bb6bSChristian Bruel 	{ 5292000, { 491000, 596000, 700000, 802000 } },
108*47e1bb6bSChristian Bruel 	{ 4968000, { 558000, 640000, 722000, 803000 } },
109*47e1bb6bSChristian Bruel 	{ 4684000, { 468000, 581000, 692000, 802000 } },
110*47e1bb6bSChristian Bruel 	{ 4429000, { 554000, 613000, 717000, 803000 } },
111*47e1bb6bSChristian Bruel 	{ 4204000, { 511000, 609000, 706000, 802000 } },
112*47e1bb6bSChristian Bruel 	{ 3999000, { 571000, 648000, 726000, 803000 } }
113*47e1bb6bSChristian Bruel };
114*47e1bb6bSChristian Bruel 
115*47e1bb6bSChristian Bruel static int stm32_impedance_tune(struct stm32_combophy *combophy)
116*47e1bb6bSChristian Bruel {
117*47e1bb6bSChristian Bruel 	u8 imp_size = ARRAY_SIZE(imp_lookup);
118*47e1bb6bSChristian Bruel 	u8 vswing_size = ARRAY_SIZE(imp_lookup[0].vswing);
119*47e1bb6bSChristian Bruel 	u8 imp_of, vswing_of;
120*47e1bb6bSChristian Bruel 	u32 max_imp = imp_lookup[0].microohm;
121*47e1bb6bSChristian Bruel 	u32 min_imp = imp_lookup[imp_size - 1].microohm;
122*47e1bb6bSChristian Bruel 	u32 max_vswing = imp_lookup[imp_size - 1].vswing[vswing_size - 1];
123*47e1bb6bSChristian Bruel 	u32 min_vswing = imp_lookup[0].vswing[0];
124*47e1bb6bSChristian Bruel 	u32 val;
125*47e1bb6bSChristian Bruel 
126*47e1bb6bSChristian Bruel 	if (!of_property_read_u32(combophy->dev->of_node, "st,output-micro-ohms", &val)) {
127*47e1bb6bSChristian Bruel 		if (val < min_imp || val > max_imp) {
128*47e1bb6bSChristian Bruel 			dev_err(combophy->dev, "Invalid value %u for output ohm\n", val);
129*47e1bb6bSChristian Bruel 			return -EINVAL;
130*47e1bb6bSChristian Bruel 		}
131*47e1bb6bSChristian Bruel 
132*47e1bb6bSChristian Bruel 		for (imp_of = 0; imp_of < ARRAY_SIZE(imp_lookup); imp_of++)
133*47e1bb6bSChristian Bruel 			if (imp_lookup[imp_of].microohm <= val)
134*47e1bb6bSChristian Bruel 				break;
135*47e1bb6bSChristian Bruel 
136*47e1bb6bSChristian Bruel 		dev_dbg(combophy->dev, "Set %u micro-ohms output impedance\n",
137*47e1bb6bSChristian Bruel 			imp_lookup[imp_of].microohm);
138*47e1bb6bSChristian Bruel 
139*47e1bb6bSChristian Bruel 		regmap_update_bits(combophy->regmap, SYSCFG_PCIEPRGCR,
140*47e1bb6bSChristian Bruel 				   STM32MP25_PCIEPRG_IMPCTRL_OHM,
141*47e1bb6bSChristian Bruel 				   FIELD_PREP(STM32MP25_PCIEPRG_IMPCTRL_OHM, imp_of));
142*47e1bb6bSChristian Bruel 	} else {
143*47e1bb6bSChristian Bruel 		regmap_read(combophy->regmap, SYSCFG_PCIEPRGCR, &val);
144*47e1bb6bSChristian Bruel 		imp_of = FIELD_GET(STM32MP25_PCIEPRG_IMPCTRL_OHM, val);
145*47e1bb6bSChristian Bruel 	}
146*47e1bb6bSChristian Bruel 
147*47e1bb6bSChristian Bruel 	if (!of_property_read_u32(combophy->dev->of_node, "st,output-vswing-microvolt", &val)) {
148*47e1bb6bSChristian Bruel 		if (val < min_vswing || val > max_vswing) {
149*47e1bb6bSChristian Bruel 			dev_err(combophy->dev, "Invalid value %u for output vswing\n", val);
150*47e1bb6bSChristian Bruel 			return -EINVAL;
151*47e1bb6bSChristian Bruel 		}
152*47e1bb6bSChristian Bruel 
153*47e1bb6bSChristian Bruel 		for (vswing_of = 0; vswing_of < ARRAY_SIZE(imp_lookup[imp_of].vswing); vswing_of++)
154*47e1bb6bSChristian Bruel 			if (imp_lookup[imp_of].vswing[vswing_of] >= val)
155*47e1bb6bSChristian Bruel 				break;
156*47e1bb6bSChristian Bruel 
157*47e1bb6bSChristian Bruel 		dev_dbg(combophy->dev, "Set %u microvolt swing\n",
158*47e1bb6bSChristian Bruel 			 imp_lookup[imp_of].vswing[vswing_of]);
159*47e1bb6bSChristian Bruel 
160*47e1bb6bSChristian Bruel 		regmap_update_bits(combophy->regmap, SYSCFG_PCIEPRGCR,
161*47e1bb6bSChristian Bruel 				   STM32MP25_PCIEPRG_IMPCTRL_VSWING,
162*47e1bb6bSChristian Bruel 				   FIELD_PREP(STM32MP25_PCIEPRG_IMPCTRL_VSWING, vswing_of));
163*47e1bb6bSChristian Bruel 	}
164*47e1bb6bSChristian Bruel 
165*47e1bb6bSChristian Bruel 	return 0;
166*47e1bb6bSChristian Bruel }
167*47e1bb6bSChristian Bruel 
168*47e1bb6bSChristian Bruel static int stm32_combophy_pll_init(struct stm32_combophy *combophy)
169*47e1bb6bSChristian Bruel {
170*47e1bb6bSChristian Bruel 	int ret;
171*47e1bb6bSChristian Bruel 	u32 refclksel, pllmult, propcntrl, val;
172*47e1bb6bSChristian Bruel 	u32 clk_rate;
173*47e1bb6bSChristian Bruel 	struct clk *clk;
174*47e1bb6bSChristian Bruel 	u32 cr1_val = 0, cr1_mask = 0;
175*47e1bb6bSChristian Bruel 
176*47e1bb6bSChristian Bruel 	if (combophy->have_pad_clk)
177*47e1bb6bSChristian Bruel 		clk = combophy->clks[PAD_CLK].clk;
178*47e1bb6bSChristian Bruel 	else
179*47e1bb6bSChristian Bruel 		clk = combophy->clks[KER_CLK].clk;
180*47e1bb6bSChristian Bruel 
181*47e1bb6bSChristian Bruel 	clk_rate = clk_get_rate(clk);
182*47e1bb6bSChristian Bruel 
183*47e1bb6bSChristian Bruel 	dev_dbg(combophy->dev, "%s pll init rate %d\n",
184*47e1bb6bSChristian Bruel 		combophy->have_pad_clk ? "External" : "Ker", clk_rate);
185*47e1bb6bSChristian Bruel 
186*47e1bb6bSChristian Bruel 	if (combophy->type != PHY_TYPE_PCIE) {
187*47e1bb6bSChristian Bruel 		cr1_mask |= SYSCFG_COMBOPHY_CR1_REFSSPEN;
188*47e1bb6bSChristian Bruel 		cr1_val |= SYSCFG_COMBOPHY_CR1_REFSSPEN;
189*47e1bb6bSChristian Bruel 	}
190*47e1bb6bSChristian Bruel 
191*47e1bb6bSChristian Bruel 	if (of_property_present(combophy->dev->of_node, "st,ssc-on")) {
192*47e1bb6bSChristian Bruel 		dev_dbg(combophy->dev, "Enabling clock with SSC\n");
193*47e1bb6bSChristian Bruel 		cr1_mask |= SYSCFG_COMBOPHY_CR1_SSCEN;
194*47e1bb6bSChristian Bruel 		cr1_val |= SYSCFG_COMBOPHY_CR1_SSCEN;
195*47e1bb6bSChristian Bruel 	}
196*47e1bb6bSChristian Bruel 
197*47e1bb6bSChristian Bruel 	switch (clk_rate) {
198*47e1bb6bSChristian Bruel 	case 100000000:
199*47e1bb6bSChristian Bruel 		pllmult = MPLLMULT_100;
200*47e1bb6bSChristian Bruel 		refclksel = REFCLKSEL_0;
201*47e1bb6bSChristian Bruel 		propcntrl = 0x8u << 4;
202*47e1bb6bSChristian Bruel 		break;
203*47e1bb6bSChristian Bruel 	case 19200000:
204*47e1bb6bSChristian Bruel 		pllmult = MPLLMULT_19_2;
205*47e1bb6bSChristian Bruel 		refclksel = REFCLKSEL_1;
206*47e1bb6bSChristian Bruel 		propcntrl = 0x8u << 4;
207*47e1bb6bSChristian Bruel 		break;
208*47e1bb6bSChristian Bruel 	case 25000000:
209*47e1bb6bSChristian Bruel 		pllmult = MPLLMULT_25;
210*47e1bb6bSChristian Bruel 		refclksel = REFCLKSEL_0;
211*47e1bb6bSChristian Bruel 		propcntrl = 0xeu << 4;
212*47e1bb6bSChristian Bruel 		break;
213*47e1bb6bSChristian Bruel 	case 24000000:
214*47e1bb6bSChristian Bruel 		pllmult = MPLLMULT_24;
215*47e1bb6bSChristian Bruel 		refclksel = REFCLKSEL_1;
216*47e1bb6bSChristian Bruel 		propcntrl = 0xeu << 4;
217*47e1bb6bSChristian Bruel 		break;
218*47e1bb6bSChristian Bruel 	case 20000000:
219*47e1bb6bSChristian Bruel 		pllmult = MPLLMULT_20;
220*47e1bb6bSChristian Bruel 		refclksel = REFCLKSEL_0;
221*47e1bb6bSChristian Bruel 		propcntrl = 0xeu << 4;
222*47e1bb6bSChristian Bruel 		break;
223*47e1bb6bSChristian Bruel 	default:
224*47e1bb6bSChristian Bruel 		dev_err(combophy->dev, "Invalid rate 0x%x\n", clk_rate);
225*47e1bb6bSChristian Bruel 		return -EINVAL;
226*47e1bb6bSChristian Bruel 	};
227*47e1bb6bSChristian Bruel 
228*47e1bb6bSChristian Bruel 	cr1_mask |= SYSCFG_COMBOPHY_CR1_REFCLKDIV2;
229*47e1bb6bSChristian Bruel 	cr1_val |= REFCLDIV_0;
230*47e1bb6bSChristian Bruel 
231*47e1bb6bSChristian Bruel 	cr1_mask |= SYSCFG_COMBOPHY_CR1_REFCLKSEL;
232*47e1bb6bSChristian Bruel 	cr1_val |= refclksel;
233*47e1bb6bSChristian Bruel 
234*47e1bb6bSChristian Bruel 	cr1_mask |= SYSCFG_COMBOPHY_CR1_MPLLMULT;
235*47e1bb6bSChristian Bruel 	cr1_val |= pllmult;
236*47e1bb6bSChristian Bruel 
237*47e1bb6bSChristian Bruel 	/*
238*47e1bb6bSChristian Bruel 	 * vddcombophy is interconnected with vddcore. Isolation bit should be unset
239*47e1bb6bSChristian Bruel 	 * before using the ComboPHY.
240*47e1bb6bSChristian Bruel 	 */
241*47e1bb6bSChristian Bruel 	regmap_update_bits(combophy->regmap, SYSCFG_COMBOPHY_CR2,
242*47e1bb6bSChristian Bruel 			   SYSCFG_COMBOPHY_CR2_ISO_DIS, SYSCFG_COMBOPHY_CR2_ISO_DIS);
243*47e1bb6bSChristian Bruel 
244*47e1bb6bSChristian Bruel 	reset_control_assert(combophy->phy_reset);
245*47e1bb6bSChristian Bruel 
246*47e1bb6bSChristian Bruel 	if (combophy->type == PHY_TYPE_PCIE) {
247*47e1bb6bSChristian Bruel 		ret = stm32_impedance_tune(combophy);
248*47e1bb6bSChristian Bruel 		if (ret)
249*47e1bb6bSChristian Bruel 			goto out_iso;
250*47e1bb6bSChristian Bruel 
251*47e1bb6bSChristian Bruel 		cr1_mask |= SYSCFG_COMBOPHY_CR1_REFUSEPAD;
252*47e1bb6bSChristian Bruel 		cr1_val |= combophy->have_pad_clk ? SYSCFG_COMBOPHY_CR1_REFUSEPAD : 0;
253*47e1bb6bSChristian Bruel 	}
254*47e1bb6bSChristian Bruel 
255*47e1bb6bSChristian Bruel 	if (!of_property_read_u32(combophy->dev->of_node, "st,rx-equalizer", &val)) {
256*47e1bb6bSChristian Bruel 		dev_dbg(combophy->dev, "Set RX equalizer %u\n", val);
257*47e1bb6bSChristian Bruel 		if (val > SYSCFG_COMBOPHY_CR4_RX0_EQ) {
258*47e1bb6bSChristian Bruel 			dev_err(combophy->dev, "Invalid value %u for rx0 equalizer\n", val);
259*47e1bb6bSChristian Bruel 			ret = -EINVAL;
260*47e1bb6bSChristian Bruel 			goto out_iso;
261*47e1bb6bSChristian Bruel 		}
262*47e1bb6bSChristian Bruel 
263*47e1bb6bSChristian Bruel 		regmap_update_bits(combophy->regmap, SYSCFG_COMBOPHY_CR4,
264*47e1bb6bSChristian Bruel 			   SYSCFG_COMBOPHY_CR4_RX0_EQ, val);
265*47e1bb6bSChristian Bruel 	}
266*47e1bb6bSChristian Bruel 
267*47e1bb6bSChristian Bruel 	regmap_update_bits(combophy->regmap, SYSCFG_COMBOPHY_CR1, cr1_mask, cr1_val);
268*47e1bb6bSChristian Bruel 
269*47e1bb6bSChristian Bruel 	/*
270*47e1bb6bSChristian Bruel 	 * Force elasticity buffer to be tuned for the reference clock as
271*47e1bb6bSChristian Bruel 	 * the separated clock model is not supported
272*47e1bb6bSChristian Bruel 	 */
273*47e1bb6bSChristian Bruel 	regmap_update_bits(combophy->regmap, SYSCFG_COMBOPHY_CR5,
274*47e1bb6bSChristian Bruel 			   SYSCFG_COMBOPHY_CR5_COMMON_CLOCKS, SYSCFG_COMBOPHY_CR5_COMMON_CLOCKS);
275*47e1bb6bSChristian Bruel 
276*47e1bb6bSChristian Bruel 	reset_control_deassert(combophy->phy_reset);
277*47e1bb6bSChristian Bruel 
278*47e1bb6bSChristian Bruel 	ret = regmap_read_poll_timeout(combophy->regmap, SYSCFG_COMBOPHY_SR, val,
279*47e1bb6bSChristian Bruel 				       !(val & STM32MP25_PIPE0_PHYSTATUS),
280*47e1bb6bSChristian Bruel 				       10, 1000);
281*47e1bb6bSChristian Bruel 	if (ret) {
282*47e1bb6bSChristian Bruel 		dev_err(combophy->dev, "timeout, cannot lock PLL\n");
283*47e1bb6bSChristian Bruel 		if (combophy->type == PHY_TYPE_PCIE && !combophy->have_pad_clk)
284*47e1bb6bSChristian Bruel 			regmap_update_bits(combophy->regmap, SYSCFG_PCIEPRGCR,
285*47e1bb6bSChristian Bruel 					   STM32MP25_PCIEPRGCR_EN, 0);
286*47e1bb6bSChristian Bruel 
287*47e1bb6bSChristian Bruel 		if (combophy->type != PHY_TYPE_PCIE)
288*47e1bb6bSChristian Bruel 			regmap_update_bits(combophy->regmap, SYSCFG_COMBOPHY_CR1,
289*47e1bb6bSChristian Bruel 					   SYSCFG_COMBOPHY_CR1_REFSSPEN, 0);
290*47e1bb6bSChristian Bruel 
291*47e1bb6bSChristian Bruel 		goto out;
292*47e1bb6bSChristian Bruel 	}
293*47e1bb6bSChristian Bruel 
294*47e1bb6bSChristian Bruel 
295*47e1bb6bSChristian Bruel 	if (combophy->type == PHY_TYPE_PCIE) {
296*47e1bb6bSChristian Bruel 		if (!combophy->have_pad_clk)
297*47e1bb6bSChristian Bruel 			regmap_update_bits(combophy->regmap, SYSCFG_PCIEPRGCR,
298*47e1bb6bSChristian Bruel 					   STM32MP25_PCIEPRGCR_EN, STM32MP25_PCIEPRGCR_EN);
299*47e1bb6bSChristian Bruel 
300*47e1bb6bSChristian Bruel 		val = readl_relaxed(combophy->base + COMBOPHY_SUP_ANA_MPLL_LOOP_CTL);
301*47e1bb6bSChristian Bruel 		val &= ~COMBOPHY_PROP_CNTRL;
302*47e1bb6bSChristian Bruel 		val |= propcntrl;
303*47e1bb6bSChristian Bruel 		writel_relaxed(val, combophy->base + COMBOPHY_SUP_ANA_MPLL_LOOP_CTL);
304*47e1bb6bSChristian Bruel 	}
305*47e1bb6bSChristian Bruel 
306*47e1bb6bSChristian Bruel 	return 0;
307*47e1bb6bSChristian Bruel 
308*47e1bb6bSChristian Bruel out_iso:
309*47e1bb6bSChristian Bruel 	reset_control_deassert(combophy->phy_reset);
310*47e1bb6bSChristian Bruel 
311*47e1bb6bSChristian Bruel out:
312*47e1bb6bSChristian Bruel 	regmap_update_bits(combophy->regmap, SYSCFG_COMBOPHY_CR2,
313*47e1bb6bSChristian Bruel 			   SYSCFG_COMBOPHY_CR2_ISO_DIS, 0);
314*47e1bb6bSChristian Bruel 
315*47e1bb6bSChristian Bruel 	return ret;
316*47e1bb6bSChristian Bruel }
317*47e1bb6bSChristian Bruel 
318*47e1bb6bSChristian Bruel static struct phy *stm32_combophy_xlate(struct device *dev,
319*47e1bb6bSChristian Bruel 					const struct of_phandle_args *args)
320*47e1bb6bSChristian Bruel {
321*47e1bb6bSChristian Bruel 	struct stm32_combophy *combophy = dev_get_drvdata(dev);
322*47e1bb6bSChristian Bruel 	unsigned int type;
323*47e1bb6bSChristian Bruel 
324*47e1bb6bSChristian Bruel 	if (args->args_count != 1) {
325*47e1bb6bSChristian Bruel 		dev_err(dev, "invalid number of cells in 'phy' property\n");
326*47e1bb6bSChristian Bruel 		return ERR_PTR(-EINVAL);
327*47e1bb6bSChristian Bruel 	}
328*47e1bb6bSChristian Bruel 
329*47e1bb6bSChristian Bruel 	type = args->args[0];
330*47e1bb6bSChristian Bruel 	if (type != PHY_TYPE_USB3 && type != PHY_TYPE_PCIE) {
331*47e1bb6bSChristian Bruel 		dev_err(dev, "unsupported device type: %d\n", type);
332*47e1bb6bSChristian Bruel 		return ERR_PTR(-EINVAL);
333*47e1bb6bSChristian Bruel 	}
334*47e1bb6bSChristian Bruel 
335*47e1bb6bSChristian Bruel 	if (combophy->have_pad_clk && type != PHY_TYPE_PCIE) {
336*47e1bb6bSChristian Bruel 		dev_err(dev, "Invalid use of clk_pad for USB3 mode\n");
337*47e1bb6bSChristian Bruel 		return ERR_PTR(-EINVAL);
338*47e1bb6bSChristian Bruel 	}
339*47e1bb6bSChristian Bruel 
340*47e1bb6bSChristian Bruel 	combophy->type = type;
341*47e1bb6bSChristian Bruel 
342*47e1bb6bSChristian Bruel 	return combophy->phy;
343*47e1bb6bSChristian Bruel }
344*47e1bb6bSChristian Bruel 
345*47e1bb6bSChristian Bruel static int stm32_combophy_set_mode(struct stm32_combophy *combophy)
346*47e1bb6bSChristian Bruel {
347*47e1bb6bSChristian Bruel 	int type = combophy->type;
348*47e1bb6bSChristian Bruel 	u32 val;
349*47e1bb6bSChristian Bruel 
350*47e1bb6bSChristian Bruel 	switch (type) {
351*47e1bb6bSChristian Bruel 	case PHY_TYPE_PCIE:
352*47e1bb6bSChristian Bruel 		dev_dbg(combophy->dev, "setting PCIe ComboPHY\n");
353*47e1bb6bSChristian Bruel 		val = COMBOPHY_MODESEL_PCIE;
354*47e1bb6bSChristian Bruel 		break;
355*47e1bb6bSChristian Bruel 	case PHY_TYPE_USB3:
356*47e1bb6bSChristian Bruel 		dev_dbg(combophy->dev, "setting USB3 ComboPHY\n");
357*47e1bb6bSChristian Bruel 		val = COMBOPHY_MODESEL_USB;
358*47e1bb6bSChristian Bruel 		break;
359*47e1bb6bSChristian Bruel 	default:
360*47e1bb6bSChristian Bruel 		dev_err(combophy->dev, "Invalid PHY mode %d\n", type);
361*47e1bb6bSChristian Bruel 		return -EINVAL;
362*47e1bb6bSChristian Bruel 	}
363*47e1bb6bSChristian Bruel 
364*47e1bb6bSChristian Bruel 	return regmap_update_bits(combophy->regmap, SYSCFG_COMBOPHY_CR2,
365*47e1bb6bSChristian Bruel 				  SYSCFG_COMBOPHY_CR2_MODESEL, val);
366*47e1bb6bSChristian Bruel }
367*47e1bb6bSChristian Bruel 
368*47e1bb6bSChristian Bruel static int stm32_combophy_suspend_noirq(struct device *dev)
369*47e1bb6bSChristian Bruel {
370*47e1bb6bSChristian Bruel 	struct stm32_combophy *combophy = dev_get_drvdata(dev);
371*47e1bb6bSChristian Bruel 
372*47e1bb6bSChristian Bruel 	/*
373*47e1bb6bSChristian Bruel 	 * Clocks should be turned off since it is not needed for
374*47e1bb6bSChristian Bruel 	 * wakeup capability. In case usb-remote wakeup is not enabled,
375*47e1bb6bSChristian Bruel 	 * combo-phy is already turned off by HCD driver using exit callback
376*47e1bb6bSChristian Bruel 	 */
377*47e1bb6bSChristian Bruel 	if (combophy->is_init) {
378*47e1bb6bSChristian Bruel 		clk_bulk_disable_unprepare(combophy->num_clks, combophy->clks);
379*47e1bb6bSChristian Bruel 
380*47e1bb6bSChristian Bruel 		/* since wakeup is enabled for ctrl */
381*47e1bb6bSChristian Bruel 		enable_irq_wake(combophy->irq_wakeup);
382*47e1bb6bSChristian Bruel 	}
383*47e1bb6bSChristian Bruel 
384*47e1bb6bSChristian Bruel 	return 0;
385*47e1bb6bSChristian Bruel }
386*47e1bb6bSChristian Bruel 
387*47e1bb6bSChristian Bruel static int stm32_combophy_resume_noirq(struct device *dev)
388*47e1bb6bSChristian Bruel {
389*47e1bb6bSChristian Bruel 	struct stm32_combophy *combophy = dev_get_drvdata(dev);
390*47e1bb6bSChristian Bruel 	int ret;
391*47e1bb6bSChristian Bruel 
392*47e1bb6bSChristian Bruel 	/*
393*47e1bb6bSChristian Bruel 	 * If clocks was turned off by suspend call for wakeup then needs
394*47e1bb6bSChristian Bruel 	 * to be turned back ON in resume. In case usb-remote wakeup is not
395*47e1bb6bSChristian Bruel 	 * enabled, clocks already turned ON by HCD driver using init callback
396*47e1bb6bSChristian Bruel 	 */
397*47e1bb6bSChristian Bruel 	if (combophy->is_init) {
398*47e1bb6bSChristian Bruel 		/* since wakeup was enabled for ctrl */
399*47e1bb6bSChristian Bruel 		disable_irq_wake(combophy->irq_wakeup);
400*47e1bb6bSChristian Bruel 
401*47e1bb6bSChristian Bruel 		ret = clk_bulk_prepare_enable(combophy->num_clks, combophy->clks);
402*47e1bb6bSChristian Bruel 		if (ret) {
403*47e1bb6bSChristian Bruel 			dev_err(dev, "can't enable clocks (%d)\n", ret);
404*47e1bb6bSChristian Bruel 			return ret;
405*47e1bb6bSChristian Bruel 		}
406*47e1bb6bSChristian Bruel 	}
407*47e1bb6bSChristian Bruel 
408*47e1bb6bSChristian Bruel 	return 0;
409*47e1bb6bSChristian Bruel }
410*47e1bb6bSChristian Bruel 
411*47e1bb6bSChristian Bruel static int stm32_combophy_exit(struct phy *phy)
412*47e1bb6bSChristian Bruel {
413*47e1bb6bSChristian Bruel 	struct stm32_combophy *combophy = phy_get_drvdata(phy);
414*47e1bb6bSChristian Bruel 	struct device *dev = combophy->dev;
415*47e1bb6bSChristian Bruel 
416*47e1bb6bSChristian Bruel 	combophy->is_init = false;
417*47e1bb6bSChristian Bruel 
418*47e1bb6bSChristian Bruel 	if (combophy->type == PHY_TYPE_PCIE && !combophy->have_pad_clk)
419*47e1bb6bSChristian Bruel 		regmap_update_bits(combophy->regmap, SYSCFG_PCIEPRGCR,
420*47e1bb6bSChristian Bruel 				   STM32MP25_PCIEPRGCR_EN, 0);
421*47e1bb6bSChristian Bruel 
422*47e1bb6bSChristian Bruel 	if (combophy->type != PHY_TYPE_PCIE)
423*47e1bb6bSChristian Bruel 		regmap_update_bits(combophy->regmap, SYSCFG_COMBOPHY_CR1,
424*47e1bb6bSChristian Bruel 				   SYSCFG_COMBOPHY_CR1_REFSSPEN, 0);
425*47e1bb6bSChristian Bruel 
426*47e1bb6bSChristian Bruel 	regmap_update_bits(combophy->regmap, SYSCFG_COMBOPHY_CR2,
427*47e1bb6bSChristian Bruel 			   SYSCFG_COMBOPHY_CR2_ISO_DIS, 0);
428*47e1bb6bSChristian Bruel 
429*47e1bb6bSChristian Bruel 	clk_bulk_disable_unprepare(combophy->num_clks, combophy->clks);
430*47e1bb6bSChristian Bruel 
431*47e1bb6bSChristian Bruel 	pm_runtime_put_noidle(dev);
432*47e1bb6bSChristian Bruel 
433*47e1bb6bSChristian Bruel 	return 0;
434*47e1bb6bSChristian Bruel }
435*47e1bb6bSChristian Bruel 
436*47e1bb6bSChristian Bruel static int stm32_combophy_init(struct phy *phy)
437*47e1bb6bSChristian Bruel {
438*47e1bb6bSChristian Bruel 	struct stm32_combophy *combophy = phy_get_drvdata(phy);
439*47e1bb6bSChristian Bruel 	struct device *dev = combophy->dev;
440*47e1bb6bSChristian Bruel 	int ret;
441*47e1bb6bSChristian Bruel 
442*47e1bb6bSChristian Bruel 	pm_runtime_get_noresume(dev);
443*47e1bb6bSChristian Bruel 
444*47e1bb6bSChristian Bruel 	ret = clk_bulk_prepare_enable(combophy->num_clks, combophy->clks);
445*47e1bb6bSChristian Bruel 	if (ret) {
446*47e1bb6bSChristian Bruel 		dev_err(dev, "can't enable clocks (%d)\n", ret);
447*47e1bb6bSChristian Bruel 		pm_runtime_put_noidle(dev);
448*47e1bb6bSChristian Bruel 		return ret;
449*47e1bb6bSChristian Bruel 	}
450*47e1bb6bSChristian Bruel 
451*47e1bb6bSChristian Bruel 	ret = stm32_combophy_set_mode(combophy);
452*47e1bb6bSChristian Bruel 	if (ret) {
453*47e1bb6bSChristian Bruel 		dev_err(dev, "combophy mode not set\n");
454*47e1bb6bSChristian Bruel 		clk_bulk_disable_unprepare(combophy->num_clks, combophy->clks);
455*47e1bb6bSChristian Bruel 		pm_runtime_put_noidle(dev);
456*47e1bb6bSChristian Bruel 		return ret;
457*47e1bb6bSChristian Bruel 	}
458*47e1bb6bSChristian Bruel 
459*47e1bb6bSChristian Bruel 	ret = stm32_combophy_pll_init(combophy);
460*47e1bb6bSChristian Bruel 	if (ret) {
461*47e1bb6bSChristian Bruel 		clk_bulk_disable_unprepare(combophy->num_clks, combophy->clks);
462*47e1bb6bSChristian Bruel 		pm_runtime_put_noidle(dev);
463*47e1bb6bSChristian Bruel 		return ret;
464*47e1bb6bSChristian Bruel 	}
465*47e1bb6bSChristian Bruel 
466*47e1bb6bSChristian Bruel 	pm_runtime_disable(dev);
467*47e1bb6bSChristian Bruel 	pm_runtime_set_active(dev);
468*47e1bb6bSChristian Bruel 	pm_runtime_enable(dev);
469*47e1bb6bSChristian Bruel 
470*47e1bb6bSChristian Bruel 	combophy->is_init = true;
471*47e1bb6bSChristian Bruel 
472*47e1bb6bSChristian Bruel 	return ret;
473*47e1bb6bSChristian Bruel }
474*47e1bb6bSChristian Bruel 
475*47e1bb6bSChristian Bruel static const struct phy_ops stm32_combophy_phy_data = {
476*47e1bb6bSChristian Bruel 	.init = stm32_combophy_init,
477*47e1bb6bSChristian Bruel 	.exit = stm32_combophy_exit,
478*47e1bb6bSChristian Bruel 	.owner = THIS_MODULE
479*47e1bb6bSChristian Bruel };
480*47e1bb6bSChristian Bruel 
481*47e1bb6bSChristian Bruel static irqreturn_t stm32_combophy_irq_wakeup_handler(int irq, void *dev_id)
482*47e1bb6bSChristian Bruel {
483*47e1bb6bSChristian Bruel 	return IRQ_HANDLED;
484*47e1bb6bSChristian Bruel }
485*47e1bb6bSChristian Bruel 
486*47e1bb6bSChristian Bruel static int stm32_combophy_get_clocks(struct stm32_combophy *combophy)
487*47e1bb6bSChristian Bruel {
488*47e1bb6bSChristian Bruel 	int i, ret;
489*47e1bb6bSChristian Bruel 
490*47e1bb6bSChristian Bruel 	for (i = 0; i < ARRAY_SIZE(combophy_clks); i++)
491*47e1bb6bSChristian Bruel 		combophy->clks[i].id = combophy_clks[i];
492*47e1bb6bSChristian Bruel 
493*47e1bb6bSChristian Bruel 	combophy->num_clks = ARRAY_SIZE(combophy_clks) - 1;
494*47e1bb6bSChristian Bruel 
495*47e1bb6bSChristian Bruel 	ret = devm_clk_bulk_get(combophy->dev, combophy->num_clks, combophy->clks);
496*47e1bb6bSChristian Bruel 	if (ret)
497*47e1bb6bSChristian Bruel 		return ret;
498*47e1bb6bSChristian Bruel 
499*47e1bb6bSChristian Bruel 	ret = devm_clk_bulk_get_optional(combophy->dev, 1, combophy->clks + combophy->num_clks);
500*47e1bb6bSChristian Bruel 	if (ret)
501*47e1bb6bSChristian Bruel 		return ret;
502*47e1bb6bSChristian Bruel 
503*47e1bb6bSChristian Bruel 	if (combophy->clks[combophy->num_clks].clk != NULL) {
504*47e1bb6bSChristian Bruel 		combophy->have_pad_clk = true;
505*47e1bb6bSChristian Bruel 		combophy->num_clks++;
506*47e1bb6bSChristian Bruel 	}
507*47e1bb6bSChristian Bruel 
508*47e1bb6bSChristian Bruel 	return 0;
509*47e1bb6bSChristian Bruel }
510*47e1bb6bSChristian Bruel 
511*47e1bb6bSChristian Bruel static int stm32_combophy_probe(struct platform_device *pdev)
512*47e1bb6bSChristian Bruel {
513*47e1bb6bSChristian Bruel 	struct stm32_combophy *combophy;
514*47e1bb6bSChristian Bruel 	struct device *dev = &pdev->dev;
515*47e1bb6bSChristian Bruel 	struct phy_provider *phy_provider;
516*47e1bb6bSChristian Bruel 	int ret, irq;
517*47e1bb6bSChristian Bruel 
518*47e1bb6bSChristian Bruel 	combophy = devm_kzalloc(dev, sizeof(*combophy), GFP_KERNEL);
519*47e1bb6bSChristian Bruel 	if (!combophy)
520*47e1bb6bSChristian Bruel 		return -ENOMEM;
521*47e1bb6bSChristian Bruel 
522*47e1bb6bSChristian Bruel 	combophy->dev = dev;
523*47e1bb6bSChristian Bruel 
524*47e1bb6bSChristian Bruel 	dev_set_drvdata(dev, combophy);
525*47e1bb6bSChristian Bruel 
526*47e1bb6bSChristian Bruel 	combophy->base = devm_platform_ioremap_resource(pdev, 0);
527*47e1bb6bSChristian Bruel 	if (IS_ERR(combophy->base))
528*47e1bb6bSChristian Bruel 		return PTR_ERR(combophy->base);
529*47e1bb6bSChristian Bruel 
530*47e1bb6bSChristian Bruel 	ret = stm32_combophy_get_clocks(combophy);
531*47e1bb6bSChristian Bruel 	if (ret)
532*47e1bb6bSChristian Bruel 		return ret;
533*47e1bb6bSChristian Bruel 
534*47e1bb6bSChristian Bruel 	combophy->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
535*47e1bb6bSChristian Bruel 	if (IS_ERR(combophy->phy_reset))
536*47e1bb6bSChristian Bruel 		return dev_err_probe(dev, PTR_ERR(combophy->phy_reset),
537*47e1bb6bSChristian Bruel 				     "Failed to get PHY reset\n");
538*47e1bb6bSChristian Bruel 
539*47e1bb6bSChristian Bruel 	combophy->regmap = syscon_regmap_lookup_by_compatible("st,stm32mp25-syscfg");
540*47e1bb6bSChristian Bruel 	if (IS_ERR(combophy->regmap))
541*47e1bb6bSChristian Bruel 		return dev_err_probe(dev, PTR_ERR(combophy->regmap),
542*47e1bb6bSChristian Bruel 				     "No syscfg specified\n");
543*47e1bb6bSChristian Bruel 
544*47e1bb6bSChristian Bruel 	combophy->phy = devm_phy_create(dev, NULL, &stm32_combophy_phy_data);
545*47e1bb6bSChristian Bruel 	if (IS_ERR(combophy->phy))
546*47e1bb6bSChristian Bruel 		return dev_err_probe(dev, PTR_ERR(combophy->phy),
547*47e1bb6bSChristian Bruel 				     "failed to create PCIe/USB3 ComboPHY\n");
548*47e1bb6bSChristian Bruel 
549*47e1bb6bSChristian Bruel 	if (device_property_read_bool(dev, "wakeup-source")) {
550*47e1bb6bSChristian Bruel 		irq = platform_get_irq(pdev, 0);
551*47e1bb6bSChristian Bruel 		if (irq < 0)
552*47e1bb6bSChristian Bruel 			return dev_err_probe(dev, irq, "failed to get IRQ\n");
553*47e1bb6bSChristian Bruel 		combophy->irq_wakeup = irq;
554*47e1bb6bSChristian Bruel 
555*47e1bb6bSChristian Bruel 		ret = devm_request_threaded_irq(dev, combophy->irq_wakeup, NULL,
556*47e1bb6bSChristian Bruel 						stm32_combophy_irq_wakeup_handler, IRQF_ONESHOT,
557*47e1bb6bSChristian Bruel 						NULL, NULL);
558*47e1bb6bSChristian Bruel 		if (ret)
559*47e1bb6bSChristian Bruel 			return dev_err_probe(dev, ret, "unable to request wake IRQ %d\n",
560*47e1bb6bSChristian Bruel 						 combophy->irq_wakeup);
561*47e1bb6bSChristian Bruel 	}
562*47e1bb6bSChristian Bruel 
563*47e1bb6bSChristian Bruel 	ret = devm_pm_runtime_enable(dev);
564*47e1bb6bSChristian Bruel 	if (ret)
565*47e1bb6bSChristian Bruel 		return dev_err_probe(dev, ret, "Failed to enable pm runtime\n");
566*47e1bb6bSChristian Bruel 
567*47e1bb6bSChristian Bruel 	phy_set_drvdata(combophy->phy, combophy);
568*47e1bb6bSChristian Bruel 
569*47e1bb6bSChristian Bruel 	phy_provider = devm_of_phy_provider_register(dev, stm32_combophy_xlate);
570*47e1bb6bSChristian Bruel 
571*47e1bb6bSChristian Bruel 	return PTR_ERR_OR_ZERO(phy_provider);
572*47e1bb6bSChristian Bruel }
573*47e1bb6bSChristian Bruel 
574*47e1bb6bSChristian Bruel static const struct dev_pm_ops stm32_combophy_pm_ops = {
575*47e1bb6bSChristian Bruel 	NOIRQ_SYSTEM_SLEEP_PM_OPS(stm32_combophy_suspend_noirq,
576*47e1bb6bSChristian Bruel 				  stm32_combophy_resume_noirq)
577*47e1bb6bSChristian Bruel };
578*47e1bb6bSChristian Bruel 
579*47e1bb6bSChristian Bruel static const struct of_device_id stm32_combophy_of_match[] = {
580*47e1bb6bSChristian Bruel 	{ .compatible = "st,stm32mp25-combophy", },
581*47e1bb6bSChristian Bruel 	{ },
582*47e1bb6bSChristian Bruel };
583*47e1bb6bSChristian Bruel MODULE_DEVICE_TABLE(of, stm32_combophy_of_match);
584*47e1bb6bSChristian Bruel 
585*47e1bb6bSChristian Bruel static struct platform_driver stm32_combophy_driver = {
586*47e1bb6bSChristian Bruel 	.probe = stm32_combophy_probe,
587*47e1bb6bSChristian Bruel 	.driver = {
588*47e1bb6bSChristian Bruel 		   .name = "stm32-combophy",
589*47e1bb6bSChristian Bruel 		   .of_match_table = stm32_combophy_of_match,
590*47e1bb6bSChristian Bruel 		   .pm = pm_sleep_ptr(&stm32_combophy_pm_ops)
591*47e1bb6bSChristian Bruel 	}
592*47e1bb6bSChristian Bruel };
593*47e1bb6bSChristian Bruel 
594*47e1bb6bSChristian Bruel module_platform_driver(stm32_combophy_driver);
595*47e1bb6bSChristian Bruel 
596*47e1bb6bSChristian Bruel MODULE_AUTHOR("Christian Bruel <christian.bruel@foss.st.com>");
597*47e1bb6bSChristian Bruel MODULE_DESCRIPTION("STM32MP25 Combophy USB3/PCIe controller driver");
598*47e1bb6bSChristian Bruel MODULE_LICENSE("GPL");
599