blob: fcf8617ee9b3308083f062237f31e7dc7b5c03f4 [file] [log] [blame]
Tom Rini4606fc72018-05-20 09:47:45 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrice Chotard226d87b2018-04-27 11:01:55 +02002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 */
5
Patrick Delaunayd182ee52020-11-06 19:01:51 +01006#define LOG_CATEGORY UCLASS_PHY
7
Patrice Chotard226d87b2018-04-27 11:01:55 +02008#include <clk.h>
Patrick Delaunay77c30052022-04-26 14:37:48 +02009#include <clk-uclass.h>
Patrice Chotard226d87b2018-04-27 11:01:55 +020010#include <div64.h>
11#include <dm.h>
12#include <fdtdec.h>
13#include <generic-phy.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Patrice Chotard226d87b2018-04-27 11:01:55 +020015#include <reset.h>
16#include <syscon.h>
17#include <usb.h>
18#include <asm/io.h>
Patrick Delaunayaaa9b7a2025-05-27 15:27:51 +020019#include <dm/device.h>
Simon Glass9bc15642020-02-03 07:36:16 -070020#include <dm/device_compat.h>
Patrick Delaunayaaa9b7a2025-05-27 15:27:51 +020021#include <dm/device-internal.h>
Patrick Delaunay77c30052022-04-26 14:37:48 +020022#include <dm/lists.h>
Patrice Chotard6a07f772022-04-22 09:39:00 +020023#include <dm/of_access.h>
24#include <linux/bitfield.h>
Patrice Chotard226d87b2018-04-27 11:01:55 +020025#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060026#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060027#include <linux/printk.h>
Patrice Chotard226d87b2018-04-27 11:01:55 +020028#include <power/regulator.h>
29
30/* USBPHYC registers */
31#define STM32_USBPHYC_PLL 0x0
32#define STM32_USBPHYC_MISC 0x8
Patrice Chotard6a07f772022-04-22 09:39:00 +020033#define STM32_USBPHYC_TUNE(X) (0x10C + ((X) * 0x100))
Patrice Chotard226d87b2018-04-27 11:01:55 +020034
35/* STM32_USBPHYC_PLL bit fields */
36#define PLLNDIV GENMASK(6, 0)
37#define PLLNDIV_SHIFT 0
38#define PLLFRACIN GENMASK(25, 10)
39#define PLLFRACIN_SHIFT 10
40#define PLLEN BIT(26)
41#define PLLSTRB BIT(27)
42#define PLLSTRBYP BIT(28)
43#define PLLFRACCTL BIT(29)
44#define PLLDITHEN0 BIT(30)
45#define PLLDITHEN1 BIT(31)
46
47/* STM32_USBPHYC_MISC bit fields */
48#define SWITHOST BIT(0)
49
Patrice Chotard6a07f772022-04-22 09:39:00 +020050/* STM32_USBPHYC_TUNE bit fields */
51#define INCURREN BIT(0)
52#define INCURRINT BIT(1)
53#define LFSCAPEN BIT(2)
54#define HSDRVSLEW BIT(3)
55#define HSDRVDCCUR BIT(4)
56#define HSDRVDCLEV BIT(5)
57#define HSDRVCURINCR BIT(6)
58#define FSDRVRFADJ BIT(7)
59#define HSDRVRFRED BIT(8)
60#define HSDRVCHKITRM GENMASK(12, 9)
61#define HSDRVCHKZTRM GENMASK(14, 13)
62#define OTPCOMP GENMASK(19, 15)
63#define SQLCHCTL GENMASK(21, 20)
64#define HDRXGNEQEN BIT(22)
65#define HSRXOFF GENMASK(24, 23)
66#define HSFALLPREEM BIT(25)
67#define SHTCCTCTLPROT BIT(26)
68#define STAGSEL BIT(27)
69
Patrice Chotard226d87b2018-04-27 11:01:55 +020070#define MAX_PHYS 2
71
Patrick Delaunay733b3162019-03-29 15:42:13 +010072/* max 100 us for PLL lock and 100 us for PHY init */
73#define PLL_INIT_TIME_US 200
Patrice Chotard226d87b2018-04-27 11:01:55 +020074#define PLL_PWR_DOWN_TIME_US 5
75#define PLL_FVCO 2880 /* in MHz */
76#define PLL_INFF_MIN_RATE 19200000 /* in Hz */
77#define PLL_INFF_MAX_RATE 38400000 /* in Hz */
78
Patrick Delaunay77c30052022-04-26 14:37:48 +020079/* USBPHYC_CLK48 */
80#define USBPHYC_CLK48_FREQ 48000000 /* in Hz */
81
Patrice Chotard6a07f772022-04-22 09:39:00 +020082enum boosting_vals {
83 BOOST_1000_UA = 1000,
84 BOOST_2000_UA = 2000,
85};
86
87enum dc_level_vals {
88 DC_MINUS_5_TO_7_MV,
89 DC_PLUS_5_TO_7_MV,
90 DC_PLUS_10_TO_14_MV,
91 DC_MAX,
92};
93
94enum current_trim {
95 CUR_NOMINAL,
96 CUR_PLUS_1_56_PCT,
97 CUR_PLUS_3_12_PCT,
98 CUR_PLUS_4_68_PCT,
99 CUR_PLUS_6_24_PCT,
100 CUR_PLUS_7_8_PCT,
101 CUR_PLUS_9_36_PCT,
102 CUR_PLUS_10_92_PCT,
103 CUR_PLUS_12_48_PCT,
104 CUR_PLUS_14_04_PCT,
105 CUR_PLUS_15_6_PCT,
106 CUR_PLUS_17_16_PCT,
107 CUR_PLUS_19_01_PCT,
108 CUR_PLUS_20_58_PCT,
109 CUR_PLUS_22_16_PCT,
110 CUR_PLUS_23_73_PCT,
111 CUR_MAX,
112};
113
114enum impedance_trim {
115 IMP_NOMINAL,
116 IMP_MINUS_2_OHMS,
117 IMP_MINUS_4_OMHS,
118 IMP_MINUS_6_OHMS,
119 IMP_MAX,
120};
121
122enum squelch_level {
123 SQLCH_NOMINAL,
124 SQLCH_PLUS_7_MV,
125 SQLCH_MINUS_5_MV,
126 SQLCH_PLUS_14_MV,
127 SQLCH_MAX,
128};
129
130enum rx_offset {
131 NO_RX_OFFSET,
132 RX_OFFSET_PLUS_5_MV,
133 RX_OFFSET_PLUS_10_MV,
134 RX_OFFSET_MINUS_5_MV,
135 RX_OFFSET_MAX,
136};
137
Patrice Chotard226d87b2018-04-27 11:01:55 +0200138struct pll_params {
139 u8 ndiv;
140 u16 frac;
141};
142
143struct stm32_usbphyc {
144 fdt_addr_t base;
145 struct clk clk;
Patrick Delaunay6ba88cd2019-03-29 15:42:11 +0100146 struct udevice *vdda1v1;
147 struct udevice *vdda1v8;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200148 struct stm32_usbphyc_phy {
149 struct udevice *vdd;
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200150 struct udevice *vbus;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200151 bool init;
152 bool powered;
153 } phys[MAX_PHYS];
Patrick Delaunayb6db7e82022-04-26 14:37:47 +0200154 int n_pll_cons;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200155};
156
Patrick Delaunay1effca22019-03-29 15:42:12 +0100157static void stm32_usbphyc_get_pll_params(u32 clk_rate,
158 struct pll_params *pll_params)
Patrice Chotard226d87b2018-04-27 11:01:55 +0200159{
160 unsigned long long fvco, ndiv, frac;
161
162 /*
163 * | FVCO = INFF*2*(NDIV + FRACT/2^16 ) when DITHER_DISABLE[1] = 1
164 * | FVCO = 2880MHz
165 * | NDIV = integer part of input bits to set the LDF
166 * | FRACT = fractional part of input bits to set the LDF
167 * => PLLNDIV = integer part of (FVCO / (INFF*2))
168 * => PLLFRACIN = fractional part of(FVCO / INFF*2) * 2^16
169 * <=> PLLFRACIN = ((FVCO / (INFF*2)) - PLLNDIV) * 2^16
170 */
171 fvco = (unsigned long long)PLL_FVCO * 1000000; /* In Hz */
172
173 ndiv = fvco;
174 do_div(ndiv, (clk_rate * 2));
175 pll_params->ndiv = (u8)ndiv;
176
177 frac = fvco * (1 << 16);
178 do_div(frac, (clk_rate * 2));
179 frac = frac - (ndiv * (1 << 16));
180 pll_params->frac = (u16)frac;
181}
182
183static int stm32_usbphyc_pll_init(struct stm32_usbphyc *usbphyc)
184{
185 struct pll_params pll_params;
186 u32 clk_rate = clk_get_rate(&usbphyc->clk);
187 u32 usbphyc_pll;
188
189 if ((clk_rate < PLL_INFF_MIN_RATE) || (clk_rate > PLL_INFF_MAX_RATE)) {
Patrick Delaunayd182ee52020-11-06 19:01:51 +0100190 log_debug("input clk freq (%dHz) out of range\n",
191 clk_rate);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200192 return -EINVAL;
193 }
194
195 stm32_usbphyc_get_pll_params(clk_rate, &pll_params);
196
197 usbphyc_pll = PLLDITHEN1 | PLLDITHEN0 | PLLSTRBYP;
198 usbphyc_pll |= ((pll_params.ndiv << PLLNDIV_SHIFT) & PLLNDIV);
199
200 if (pll_params.frac) {
201 usbphyc_pll |= PLLFRACCTL;
202 usbphyc_pll |= ((pll_params.frac << PLLFRACIN_SHIFT)
203 & PLLFRACIN);
204 }
205
206 writel(usbphyc_pll, usbphyc->base + STM32_USBPHYC_PLL);
207
Patrick Delaunayd182ee52020-11-06 19:01:51 +0100208 log_debug("input clk freq=%dHz, ndiv=%d, frac=%d\n",
209 clk_rate, pll_params.ndiv, pll_params.frac);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200210
211 return 0;
212}
213
Patrice Chotard226d87b2018-04-27 11:01:55 +0200214static bool stm32_usbphyc_is_powered(struct stm32_usbphyc *usbphyc)
215{
216 int i;
217
218 for (i = 0; i < MAX_PHYS; i++) {
219 if (usbphyc->phys[i].powered)
220 return true;
221 }
222
223 return false;
224}
225
Patrick Delaunayb6db7e82022-04-26 14:37:47 +0200226static int stm32_usbphyc_pll_enable(struct stm32_usbphyc *usbphyc)
Patrice Chotard226d87b2018-04-27 11:01:55 +0200227{
Patrice Chotard226d87b2018-04-27 11:01:55 +0200228 bool pllen = readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN ?
229 true : false;
230 int ret;
231
Patrick Delaunayb6db7e82022-04-26 14:37:47 +0200232 /* Check if one consumer has already configured the pll */
233 if (pllen && usbphyc->n_pll_cons) {
234 usbphyc->n_pll_cons++;
235 return 0;
236 }
Patrice Chotard226d87b2018-04-27 11:01:55 +0200237
Patrick Delaunay1effca22019-03-29 15:42:12 +0100238 if (usbphyc->vdda1v1) {
239 ret = regulator_set_enable(usbphyc->vdda1v1, true);
240 if (ret)
241 return ret;
242 }
243
244 if (usbphyc->vdda1v8) {
245 ret = regulator_set_enable(usbphyc->vdda1v8, true);
246 if (ret)
247 return ret;
248 }
249
Patrice Chotard226d87b2018-04-27 11:01:55 +0200250 if (pllen) {
251 clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
252 udelay(PLL_PWR_DOWN_TIME_US);
253 }
254
255 ret = stm32_usbphyc_pll_init(usbphyc);
256 if (ret)
257 return ret;
258
259 setbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
260
Patrick Delaunay733b3162019-03-29 15:42:13 +0100261 /* We must wait PLL_INIT_TIME_US before using PHY */
262 udelay(PLL_INIT_TIME_US);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200263
264 if (!(readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN))
265 return -EIO;
266
Patrick Delaunayb6db7e82022-04-26 14:37:47 +0200267 usbphyc->n_pll_cons++;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200268
269 return 0;
270}
271
Patrick Delaunayb6db7e82022-04-26 14:37:47 +0200272static int stm32_usbphyc_pll_disable(struct stm32_usbphyc *usbphyc)
Patrice Chotard226d87b2018-04-27 11:01:55 +0200273{
Patrick Delaunay1effca22019-03-29 15:42:12 +0100274 int ret;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200275
Patrick Delaunayb6db7e82022-04-26 14:37:47 +0200276 usbphyc->n_pll_cons--;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200277
Patrick Delaunayb6db7e82022-04-26 14:37:47 +0200278 /* Check if other consumer requires pllen */
279 if (usbphyc->n_pll_cons)
Patrice Chotard226d87b2018-04-27 11:01:55 +0200280 return 0;
281
282 clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
283
284 /*
285 * We must wait PLL_PWR_DOWN_TIME_US before checking that PLLEN
286 * bit is still clear
287 */
288 udelay(PLL_PWR_DOWN_TIME_US);
289
290 if (readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN)
291 return -EIO;
292
Patrick Delaunay6ba88cd2019-03-29 15:42:11 +0100293 if (usbphyc->vdda1v1) {
Patrick Delaunay1effca22019-03-29 15:42:12 +0100294 ret = regulator_set_enable(usbphyc->vdda1v1, false);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200295 if (ret)
296 return ret;
297 }
298
Patrick Delaunay6ba88cd2019-03-29 15:42:11 +0100299 if (usbphyc->vdda1v8) {
Patrick Delaunay1effca22019-03-29 15:42:12 +0100300 ret = regulator_set_enable(usbphyc->vdda1v8, false);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200301 if (ret)
302 return ret;
303 }
Patrick Delaunay6ba88cd2019-03-29 15:42:11 +0100304
Patrick Delaunay1effca22019-03-29 15:42:12 +0100305 return 0;
306}
307
Patrick Delaunayb6db7e82022-04-26 14:37:47 +0200308static int stm32_usbphyc_phy_init(struct phy *phy)
309{
310 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
311 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
312 int ret;
313
314 dev_dbg(phy->dev, "phy ID = %lu\n", phy->id);
315 if (usbphyc_phy->init)
316 return 0;
317
318 ret = stm32_usbphyc_pll_enable(usbphyc);
319 if (ret)
320 return log_ret(ret);
321
322 usbphyc_phy->init = true;
323
324 return 0;
325}
326
327static int stm32_usbphyc_phy_exit(struct phy *phy)
328{
329 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
330 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
331 int ret;
332
333 dev_dbg(phy->dev, "phy ID = %lu\n", phy->id);
334 if (!usbphyc_phy->init)
335 return 0;
336
337 ret = stm32_usbphyc_pll_disable(usbphyc);
338
339 usbphyc_phy->init = false;
340
341 return log_ret(ret);
342}
343
Patrick Delaunay1effca22019-03-29 15:42:12 +0100344static int stm32_usbphyc_phy_power_on(struct phy *phy)
345{
346 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
347 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
348 int ret;
349
Patrick Delaunayd182ee52020-11-06 19:01:51 +0100350 dev_dbg(phy->dev, "phy ID = %lu\n", phy->id);
Patrick Delaunay1effca22019-03-29 15:42:12 +0100351 if (usbphyc_phy->vdd) {
352 ret = regulator_set_enable(usbphyc_phy->vdd, true);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200353 if (ret)
354 return ret;
355 }
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200356 if (usbphyc_phy->vbus) {
357 ret = regulator_set_enable(usbphyc_phy->vbus, true);
358 if (ret)
359 return ret;
360 }
Patrice Chotard226d87b2018-04-27 11:01:55 +0200361
362 usbphyc_phy->powered = true;
363
364 return 0;
365}
366
367static int stm32_usbphyc_phy_power_off(struct phy *phy)
368{
369 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
370 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
371 int ret;
372
Patrick Delaunayd182ee52020-11-06 19:01:51 +0100373 dev_dbg(phy->dev, "phy ID = %lu\n", phy->id);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200374 usbphyc_phy->powered = false;
375
376 if (stm32_usbphyc_is_powered(usbphyc))
377 return 0;
378
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200379 if (usbphyc_phy->vbus) {
Patrick Delaunay8b6a6642022-09-20 13:39:56 +0200380 ret = regulator_set_enable_if_allowed(usbphyc_phy->vbus, false);
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200381 if (ret)
382 return ret;
383 }
Patrick Delaunay1effca22019-03-29 15:42:12 +0100384 if (usbphyc_phy->vdd) {
Patrick Delaunay611bba82020-07-03 19:13:02 +0200385 ret = regulator_set_enable_if_allowed(usbphyc_phy->vdd, false);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200386 if (ret)
387 return ret;
388 }
389
390 return 0;
391}
392
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200393static int stm32_usbphyc_get_regulator(ofnode node,
Patrice Chotard226d87b2018-04-27 11:01:55 +0200394 char *supply_name,
395 struct udevice **regulator)
396{
397 struct ofnode_phandle_args regulator_phandle;
398 int ret;
399
400 ret = ofnode_parse_phandle_with_args(node, supply_name,
401 NULL, 0, 0,
402 &regulator_phandle);
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200403 if (ret)
Patrice Chotard226d87b2018-04-27 11:01:55 +0200404 return ret;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200405
406 ret = uclass_get_device_by_ofnode(UCLASS_REGULATOR,
407 regulator_phandle.node,
408 regulator);
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200409 if (ret)
Patrice Chotard226d87b2018-04-27 11:01:55 +0200410 return ret;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200411
412 return 0;
413}
414
415static int stm32_usbphyc_of_xlate(struct phy *phy,
416 struct ofnode_phandle_args *args)
417{
Patrick Delaunay2ad166e2019-03-29 15:42:10 +0100418 if (args->args_count < 1)
419 return -ENODEV;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200420
421 if (args->args[0] >= MAX_PHYS)
422 return -ENODEV;
423
Patrick Delaunay2ad166e2019-03-29 15:42:10 +0100424 phy->id = args->args[0];
425
426 if ((phy->id == 0 && args->args_count != 1) ||
427 (phy->id == 1 && args->args_count != 2)) {
Sean Anderson30080c32020-09-15 10:45:06 -0400428 dev_err(phy->dev, "invalid number of cells for phy port%ld\n",
Patrick Delaunay2ad166e2019-03-29 15:42:10 +0100429 phy->id);
430 return -EINVAL;
431 }
Patrice Chotard226d87b2018-04-27 11:01:55 +0200432
433 return 0;
434}
435
Patrice Chotard6a07f772022-04-22 09:39:00 +0200436static void stm32_usbphyc_tuning(struct udevice *dev, ofnode node, u32 index)
437{
438 struct stm32_usbphyc *usbphyc = dev_get_priv(dev);
439 u32 reg = STM32_USBPHYC_TUNE(index);
440 u32 otpcomp, val, tune = 0;
441 int ret;
442
443 /* Backup OTP compensation code */
444 otpcomp = FIELD_GET(OTPCOMP, readl(usbphyc->base + reg));
445
446 ret = ofnode_read_u32(node, "st,current-boost-microamp", &val);
447 if (!ret && (val == BOOST_1000_UA || val == BOOST_2000_UA)) {
448 val = (val == BOOST_2000_UA) ? 1 : 0;
449 tune |= INCURREN | FIELD_PREP(INCURRINT, val);
450 } else if (ret != -EINVAL) {
451 dev_warn(dev, "phy%d: invalid st,current-boost-microamp value\n", index);
452 }
453
454 if (!ofnode_read_bool(node, "st,no-lsfs-fb-cap"))
455 tune |= LFSCAPEN;
456
457 if (ofnode_read_bool(node, "st,decrease-hs-slew-rate"))
458 tune |= HSDRVSLEW;
459
460 ret = ofnode_read_u32(node, "st,tune-hs-dc-level", &val);
461 if (!ret && val < DC_MAX) {
462 if (val == DC_MINUS_5_TO_7_MV) {
463 tune |= HSDRVDCCUR;
464 } else {
465 val = (val == DC_PLUS_10_TO_14_MV) ? 1 : 0;
466 tune |= HSDRVCURINCR | FIELD_PREP(HSDRVDCLEV, val);
467 }
468 } else if (ret != -EINVAL) {
469 dev_warn(dev, "phy%d: invalid st,tune-hs-dc-level value\n", index);
470 }
471
472 if (ofnode_read_bool(node, "st,enable-fs-rftime-tuning"))
473 tune |= FSDRVRFADJ;
474
475 if (ofnode_read_bool(node, "st,enable-hs-rftime-reduction"))
476 tune |= HSDRVRFRED;
477
478 ret = ofnode_read_u32(node, "st,trim-hs-current", &val);
479 if (!ret && val < CUR_MAX)
480 tune |= FIELD_PREP(HSDRVCHKITRM, val);
481 else if (ret != -EINVAL)
482 dev_warn(dev, "phy%d: invalid st,trim-hs-current value\n", index);
483
484 ret = ofnode_read_u32(node, "st,trim-hs-impedance", &val);
485 if (!ret && val < IMP_MAX)
486 tune |= FIELD_PREP(HSDRVCHKZTRM, val);
487 else if (ret != -EINVAL)
488 dev_warn(dev, "phy%d: invalid trim-hs-impedance value\n", index);
489
490 ret = ofnode_read_u32(node, "st,tune-squelch-level", &val);
491 if (!ret && val < SQLCH_MAX)
492 tune |= FIELD_PREP(SQLCHCTL, val);
493 else if (ret != -EINVAL)
494 dev_warn(dev, "phy%d: invalid st,tune-squelch-level value\n", index);
495
496 if (ofnode_read_bool(node, "st,enable-hs-rx-gain-eq"))
497 tune |= HDRXGNEQEN;
498
499 ret = ofnode_read_u32(node, "st,tune-hs-rx-offset", &val);
500 if (!ret && val < RX_OFFSET_MAX)
501 tune |= FIELD_PREP(HSRXOFF, val);
502 else if (ret != -EINVAL)
503 dev_warn(dev, "phy%d: invalid st,tune-hs-rx-offset value\n", index);
504
505 if (ofnode_read_bool(node, "st,no-hs-ftime-ctrl"))
506 tune |= HSFALLPREEM;
507
508 if (!ofnode_read_bool(node, "st,no-lsfs-sc"))
509 tune |= SHTCCTCTLPROT;
510
511 if (ofnode_read_bool(node, "st,enable-hs-tx-staggering"))
512 tune |= STAGSEL;
513
514 /* Restore OTP compensation code */
515 tune |= FIELD_PREP(OTPCOMP, otpcomp);
516
517 writel(tune, usbphyc->base + reg);
518}
519
Patrice Chotard226d87b2018-04-27 11:01:55 +0200520static const struct phy_ops stm32_usbphyc_phy_ops = {
521 .init = stm32_usbphyc_phy_init,
522 .exit = stm32_usbphyc_phy_exit,
523 .power_on = stm32_usbphyc_phy_power_on,
524 .power_off = stm32_usbphyc_phy_power_off,
525 .of_xlate = stm32_usbphyc_of_xlate,
526};
527
Patrick Delaunay77c30052022-04-26 14:37:48 +0200528static int stm32_usbphyc_bind(struct udevice *dev)
529{
530 int ret;
531
532 ret = device_bind_driver_to_node(dev, "stm32-usbphyc-clk", "ck_usbo_48m",
533 dev_ofnode(dev), NULL);
534
535 return log_ret(ret);
536}
537
Patrice Chotard226d87b2018-04-27 11:01:55 +0200538static int stm32_usbphyc_probe(struct udevice *dev)
539{
540 struct stm32_usbphyc *usbphyc = dev_get_priv(dev);
541 struct reset_ctl reset;
Patrick Delaunay4bd95e42021-09-14 14:31:16 +0200542 ofnode node, connector;
Patrick Delaunay35aa20d2021-09-14 14:31:17 +0200543 int ret;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200544
545 usbphyc->base = dev_read_addr(dev);
546 if (usbphyc->base == FDT_ADDR_T_NONE)
547 return -EINVAL;
548
549 /* Enable clock */
550 ret = clk_get_by_index(dev, 0, &usbphyc->clk);
551 if (ret)
552 return ret;
553
554 ret = clk_enable(&usbphyc->clk);
555 if (ret)
556 return ret;
557
558 /* Reset */
559 ret = reset_get_by_index(dev, 0, &reset);
560 if (!ret) {
561 reset_assert(&reset);
562 udelay(2);
563 reset_deassert(&reset);
564 }
565
Patrick Delaunay6ba88cd2019-03-29 15:42:11 +0100566 /* get usbphyc regulator */
567 ret = device_get_supply_regulator(dev, "vdda1v1-supply",
568 &usbphyc->vdda1v1);
569 if (ret) {
570 dev_err(dev, "Can't get vdda1v1-supply regulator\n");
571 return ret;
572 }
573
574 ret = device_get_supply_regulator(dev, "vdda1v8-supply",
575 &usbphyc->vdda1v8);
576 if (ret) {
577 dev_err(dev, "Can't get vdda1v8-supply regulator\n");
578 return ret;
579 }
580
Patrick Delaunay35aa20d2021-09-14 14:31:17 +0200581 /* parse all PHY subnodes to populate regulator associated to each PHY port */
582 dev_for_each_subnode(node, dev) {
583 fdt_addr_t phy_id;
584 struct stm32_usbphyc_phy *usbphyc_phy;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200585
Patrick Delaunay35aa20d2021-09-14 14:31:17 +0200586 phy_id = ofnode_read_u32_default(node, "reg", FDT_ADDR_T_NONE);
587 if (phy_id >= MAX_PHYS) {
Johan Jonker511a0472023-03-13 01:32:57 +0100588 dev_err(dev, "invalid reg value %llx for %s\n",
589 (fdt64_t)phy_id, ofnode_get_name(node));
Patrick Delaunay35aa20d2021-09-14 14:31:17 +0200590 return -ENOENT;
591 }
Patrice Chotard6a07f772022-04-22 09:39:00 +0200592
593 /* Configure phy tuning */
594 stm32_usbphyc_tuning(dev, node, phy_id);
595
Patrick Delaunay35aa20d2021-09-14 14:31:17 +0200596 usbphyc_phy = usbphyc->phys + phy_id;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200597 usbphyc_phy->init = false;
598 usbphyc_phy->powered = false;
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200599 ret = stm32_usbphyc_get_regulator(node, "phy-supply",
Patrice Chotard226d87b2018-04-27 11:01:55 +0200600 &usbphyc_phy->vdd);
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200601 if (ret) {
602 dev_err(dev, "Can't get phy-supply regulator\n");
Patrice Chotard226d87b2018-04-27 11:01:55 +0200603 return ret;
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200604 }
605
Patrick Delaunay4bd95e42021-09-14 14:31:16 +0200606 usbphyc_phy->vbus = NULL;
607 connector = ofnode_find_subnode(node, "connector");
608 if (ofnode_valid(connector)) {
609 ret = stm32_usbphyc_get_regulator(connector, "vbus-supply",
610 &usbphyc_phy->vbus);
611 }
Patrice Chotard226d87b2018-04-27 11:01:55 +0200612 }
613
614 /* Check if second port has to be used for host controller */
615 if (dev_read_bool(dev, "st,port2-switch-to-host"))
616 setbits_le32(usbphyc->base + STM32_USBPHYC_MISC, SWITHOST);
617
618 return 0;
619}
620
621static const struct udevice_id stm32_usbphyc_of_match[] = {
622 { .compatible = "st,stm32mp1-usbphyc", },
623 { },
624};
625
626U_BOOT_DRIVER(stm32_usb_phyc) = {
627 .name = "stm32-usbphyc",
628 .id = UCLASS_PHY,
629 .of_match = stm32_usbphyc_of_match,
630 .ops = &stm32_usbphyc_phy_ops,
Patrick Delaunay77c30052022-04-26 14:37:48 +0200631 .bind = stm32_usbphyc_bind,
Patrice Chotard226d87b2018-04-27 11:01:55 +0200632 .probe = stm32_usbphyc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700633 .priv_auto = sizeof(struct stm32_usbphyc),
Patrice Chotard226d87b2018-04-27 11:01:55 +0200634};
Patrick Delaunay77c30052022-04-26 14:37:48 +0200635
636struct stm32_usbphyc_clk {
637 bool enable;
Patrick Delaunayaaa9b7a2025-05-27 15:27:51 +0200638 struct clk clkp;
Patrick Delaunay77c30052022-04-26 14:37:48 +0200639};
640
641static ulong stm32_usbphyc_clk48_get_rate(struct clk *clk)
642{
643 return USBPHYC_CLK48_FREQ;
644}
645
646static int stm32_usbphyc_clk48_enable(struct clk *clk)
647{
648 struct stm32_usbphyc_clk *usbphyc_clk = dev_get_priv(clk->dev);
649 struct stm32_usbphyc *usbphyc;
650 int ret;
651
652 if (usbphyc_clk->enable)
653 return 0;
654
655 usbphyc = dev_get_priv(clk->dev->parent);
656
657 /* ck_usbo_48m is generated by usbphyc PLL */
658 ret = stm32_usbphyc_pll_enable(usbphyc);
659 if (ret)
660 return ret;
661
662 usbphyc_clk->enable = true;
663
664 return 0;
665}
666
667static int stm32_usbphyc_clk48_disable(struct clk *clk)
668{
669 struct stm32_usbphyc_clk *usbphyc_clk = dev_get_priv(clk->dev);
670 struct stm32_usbphyc *usbphyc;
671 int ret;
672
673 if (!usbphyc_clk->enable)
674 return 0;
675
676 usbphyc = dev_get_priv(clk->dev->parent);
677
678 ret = stm32_usbphyc_pll_disable(usbphyc);
679 if (ret)
680 return ret;
681
682 usbphyc_clk->enable = false;
683
684 return 0;
685}
686
687const struct clk_ops usbphyc_clk48_ops = {
688 .get_rate = stm32_usbphyc_clk48_get_rate,
689 .enable = stm32_usbphyc_clk48_enable,
690 .disable = stm32_usbphyc_clk48_disable,
691};
692
Patrick Delaunayaaa9b7a2025-05-27 15:27:51 +0200693int usbphyc_clk48_probe(struct udevice *dev)
694{
695 struct stm32_usbphyc_clk *priv = dev_get_priv(dev);
696
697 /* prepare clkp to correctly register clock with CCF */
698 priv->clkp.dev = dev;
699 priv->clkp.id = CLK_ID(dev, 0);
700
701 /* Store back pointer to clk from udevice */
702 /* FIXME: This is not allowed...should be allocated by driver model */
703 dev_set_uclass_priv(dev, &priv->clkp);
704
705 return 0;
706}
707
Patrick Delaunay77c30052022-04-26 14:37:48 +0200708U_BOOT_DRIVER(stm32_usb_phyc_clk) = {
709 .name = "stm32-usbphyc-clk",
710 .id = UCLASS_CLK,
711 .ops = &usbphyc_clk48_ops,
Patrick Delaunayaaa9b7a2025-05-27 15:27:51 +0200712 .probe = &usbphyc_clk48_probe,
Patrick Delaunay77c30052022-04-26 14:37:48 +0200713 .priv_auto = sizeof(struct stm32_usbphyc_clk),
714};