1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
6 #define LOG_CATEGORY UCLASS_PHY
13 #include <generic-phy.h>
19 #include <dm/device_compat.h>
20 #include <linux/bitops.h>
21 #include <linux/delay.h>
22 #include <power/regulator.h>
24 /* USBPHYC registers */
25 #define STM32_USBPHYC_PLL 0x0
26 #define STM32_USBPHYC_MISC 0x8
28 /* STM32_USBPHYC_PLL bit fields */
29 #define PLLNDIV GENMASK(6, 0)
30 #define PLLNDIV_SHIFT 0
31 #define PLLFRACIN GENMASK(25, 10)
32 #define PLLFRACIN_SHIFT 10
34 #define PLLSTRB BIT(27)
35 #define PLLSTRBYP BIT(28)
36 #define PLLFRACCTL BIT(29)
37 #define PLLDITHEN0 BIT(30)
38 #define PLLDITHEN1 BIT(31)
40 /* STM32_USBPHYC_MISC bit fields */
41 #define SWITHOST BIT(0)
45 /* max 100 us for PLL lock and 100 us for PHY init */
46 #define PLL_INIT_TIME_US 200
47 #define PLL_PWR_DOWN_TIME_US 5
48 #define PLL_FVCO 2880 /* in MHz */
49 #define PLL_INFF_MIN_RATE 19200000 /* in Hz */
50 #define PLL_INFF_MAX_RATE 38400000 /* in Hz */
57 struct stm32_usbphyc {
60 struct udevice *vdda1v1;
61 struct udevice *vdda1v8;
62 struct stm32_usbphyc_phy {
70 static void stm32_usbphyc_get_pll_params(u32 clk_rate,
71 struct pll_params *pll_params)
73 unsigned long long fvco, ndiv, frac;
76 * | FVCO = INFF*2*(NDIV + FRACT/2^16 ) when DITHER_DISABLE[1] = 1
78 * | NDIV = integer part of input bits to set the LDF
79 * | FRACT = fractional part of input bits to set the LDF
80 * => PLLNDIV = integer part of (FVCO / (INFF*2))
81 * => PLLFRACIN = fractional part of(FVCO / INFF*2) * 2^16
82 * <=> PLLFRACIN = ((FVCO / (INFF*2)) - PLLNDIV) * 2^16
84 fvco = (unsigned long long)PLL_FVCO * 1000000; /* In Hz */
87 do_div(ndiv, (clk_rate * 2));
88 pll_params->ndiv = (u8)ndiv;
90 frac = fvco * (1 << 16);
91 do_div(frac, (clk_rate * 2));
92 frac = frac - (ndiv * (1 << 16));
93 pll_params->frac = (u16)frac;
96 static int stm32_usbphyc_pll_init(struct stm32_usbphyc *usbphyc)
98 struct pll_params pll_params;
99 u32 clk_rate = clk_get_rate(&usbphyc->clk);
102 if ((clk_rate < PLL_INFF_MIN_RATE) || (clk_rate > PLL_INFF_MAX_RATE)) {
103 log_debug("input clk freq (%dHz) out of range\n",
108 stm32_usbphyc_get_pll_params(clk_rate, &pll_params);
110 usbphyc_pll = PLLDITHEN1 | PLLDITHEN0 | PLLSTRBYP;
111 usbphyc_pll |= ((pll_params.ndiv << PLLNDIV_SHIFT) & PLLNDIV);
113 if (pll_params.frac) {
114 usbphyc_pll |= PLLFRACCTL;
115 usbphyc_pll |= ((pll_params.frac << PLLFRACIN_SHIFT)
119 writel(usbphyc_pll, usbphyc->base + STM32_USBPHYC_PLL);
121 log_debug("input clk freq=%dHz, ndiv=%d, frac=%d\n",
122 clk_rate, pll_params.ndiv, pll_params.frac);
127 static bool stm32_usbphyc_is_init(struct stm32_usbphyc *usbphyc)
131 for (i = 0; i < MAX_PHYS; i++) {
132 if (usbphyc->phys[i].init)
139 static bool stm32_usbphyc_is_powered(struct stm32_usbphyc *usbphyc)
143 for (i = 0; i < MAX_PHYS; i++) {
144 if (usbphyc->phys[i].powered)
151 static int stm32_usbphyc_phy_init(struct phy *phy)
153 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
154 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
155 bool pllen = readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN ?
159 dev_dbg(phy->dev, "phy ID = %lu\n", phy->id);
160 /* Check if one phy port has already configured the pll */
161 if (pllen && stm32_usbphyc_is_init(usbphyc))
164 if (usbphyc->vdda1v1) {
165 ret = regulator_set_enable(usbphyc->vdda1v1, true);
170 if (usbphyc->vdda1v8) {
171 ret = regulator_set_enable(usbphyc->vdda1v8, true);
177 clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
178 udelay(PLL_PWR_DOWN_TIME_US);
181 ret = stm32_usbphyc_pll_init(usbphyc);
185 setbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
187 /* We must wait PLL_INIT_TIME_US before using PHY */
188 udelay(PLL_INIT_TIME_US);
190 if (!(readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN))
194 usbphyc_phy->init = true;
199 static int stm32_usbphyc_phy_exit(struct phy *phy)
201 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
202 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
205 dev_dbg(phy->dev, "phy ID = %lu\n", phy->id);
206 usbphyc_phy->init = false;
208 /* Check if other phy port requires pllen */
209 if (stm32_usbphyc_is_init(usbphyc))
212 clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
215 * We must wait PLL_PWR_DOWN_TIME_US before checking that PLLEN
218 udelay(PLL_PWR_DOWN_TIME_US);
220 if (readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN)
223 if (usbphyc->vdda1v1) {
224 ret = regulator_set_enable(usbphyc->vdda1v1, false);
229 if (usbphyc->vdda1v8) {
230 ret = regulator_set_enable(usbphyc->vdda1v8, false);
238 static int stm32_usbphyc_phy_power_on(struct phy *phy)
240 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
241 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
244 dev_dbg(phy->dev, "phy ID = %lu\n", phy->id);
245 if (usbphyc_phy->vdd) {
246 ret = regulator_set_enable(usbphyc_phy->vdd, true);
250 if (usbphyc_phy->vbus) {
251 ret = regulator_set_enable(usbphyc_phy->vbus, true);
256 usbphyc_phy->powered = true;
261 static int stm32_usbphyc_phy_power_off(struct phy *phy)
263 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
264 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
267 dev_dbg(phy->dev, "phy ID = %lu\n", phy->id);
268 usbphyc_phy->powered = false;
270 if (stm32_usbphyc_is_powered(usbphyc))
273 if (usbphyc_phy->vbus) {
274 ret = regulator_set_enable(usbphyc_phy->vbus, false);
278 if (usbphyc_phy->vdd) {
279 ret = regulator_set_enable_if_allowed(usbphyc_phy->vdd, false);
287 static int stm32_usbphyc_get_regulator(ofnode node,
289 struct udevice **regulator)
291 struct ofnode_phandle_args regulator_phandle;
294 ret = ofnode_parse_phandle_with_args(node, supply_name,
300 ret = uclass_get_device_by_ofnode(UCLASS_REGULATOR,
301 regulator_phandle.node,
309 static int stm32_usbphyc_of_xlate(struct phy *phy,
310 struct ofnode_phandle_args *args)
312 if (args->args_count < 1)
315 if (args->args[0] >= MAX_PHYS)
318 phy->id = args->args[0];
320 if ((phy->id == 0 && args->args_count != 1) ||
321 (phy->id == 1 && args->args_count != 2)) {
322 dev_err(phy->dev, "invalid number of cells for phy port%ld\n",
330 static const struct phy_ops stm32_usbphyc_phy_ops = {
331 .init = stm32_usbphyc_phy_init,
332 .exit = stm32_usbphyc_phy_exit,
333 .power_on = stm32_usbphyc_phy_power_on,
334 .power_off = stm32_usbphyc_phy_power_off,
335 .of_xlate = stm32_usbphyc_of_xlate,
338 static int stm32_usbphyc_probe(struct udevice *dev)
340 struct stm32_usbphyc *usbphyc = dev_get_priv(dev);
341 struct reset_ctl reset;
342 ofnode node, connector;
345 usbphyc->base = dev_read_addr(dev);
346 if (usbphyc->base == FDT_ADDR_T_NONE)
350 ret = clk_get_by_index(dev, 0, &usbphyc->clk);
354 ret = clk_enable(&usbphyc->clk);
359 ret = reset_get_by_index(dev, 0, &reset);
361 reset_assert(&reset);
363 reset_deassert(&reset);
366 /* get usbphyc regulator */
367 ret = device_get_supply_regulator(dev, "vdda1v1-supply",
370 dev_err(dev, "Can't get vdda1v1-supply regulator\n");
374 ret = device_get_supply_regulator(dev, "vdda1v8-supply",
377 dev_err(dev, "Can't get vdda1v8-supply regulator\n");
381 /* parse all PHY subnodes to populate regulator associated to each PHY port */
382 dev_for_each_subnode(node, dev) {
384 struct stm32_usbphyc_phy *usbphyc_phy;
386 phy_id = ofnode_read_u32_default(node, "reg", FDT_ADDR_T_NONE);
387 if (phy_id >= MAX_PHYS) {
388 dev_err(dev, "invalid reg value %lx for %s\n",
389 phy_id, ofnode_get_name(node));
392 usbphyc_phy = usbphyc->phys + phy_id;
393 usbphyc_phy->init = false;
394 usbphyc_phy->powered = false;
395 ret = stm32_usbphyc_get_regulator(node, "phy-supply",
398 dev_err(dev, "Can't get phy-supply regulator\n");
402 usbphyc_phy->vbus = NULL;
403 connector = ofnode_find_subnode(node, "connector");
404 if (ofnode_valid(connector)) {
405 ret = stm32_usbphyc_get_regulator(connector, "vbus-supply",
410 /* Check if second port has to be used for host controller */
411 if (dev_read_bool(dev, "st,port2-switch-to-host"))
412 setbits_le32(usbphyc->base + STM32_USBPHYC_MISC, SWITHOST);
417 static const struct udevice_id stm32_usbphyc_of_match[] = {
418 { .compatible = "st,stm32mp1-usbphyc", },
422 U_BOOT_DRIVER(stm32_usb_phyc) = {
423 .name = "stm32-usbphyc",
425 .of_match = stm32_usbphyc_of_match,
426 .ops = &stm32_usbphyc_phy_ops,
427 .probe = stm32_usbphyc_probe,
428 .priv_auto = sizeof(struct stm32_usbphyc),