1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
11 #include <generic-phy.h>
17 #include <dm/device_compat.h>
18 #include <linux/bitops.h>
19 #include <linux/delay.h>
20 #include <power/regulator.h>
22 /* USBPHYC registers */
23 #define STM32_USBPHYC_PLL 0x0
24 #define STM32_USBPHYC_MISC 0x8
26 /* STM32_USBPHYC_PLL bit fields */
27 #define PLLNDIV GENMASK(6, 0)
28 #define PLLNDIV_SHIFT 0
29 #define PLLFRACIN GENMASK(25, 10)
30 #define PLLFRACIN_SHIFT 10
32 #define PLLSTRB BIT(27)
33 #define PLLSTRBYP BIT(28)
34 #define PLLFRACCTL BIT(29)
35 #define PLLDITHEN0 BIT(30)
36 #define PLLDITHEN1 BIT(31)
38 /* STM32_USBPHYC_MISC bit fields */
39 #define SWITHOST BIT(0)
43 /* max 100 us for PLL lock and 100 us for PHY init */
44 #define PLL_INIT_TIME_US 200
45 #define PLL_PWR_DOWN_TIME_US 5
46 #define PLL_FVCO 2880 /* in MHz */
47 #define PLL_INFF_MIN_RATE 19200000 /* in Hz */
48 #define PLL_INFF_MAX_RATE 38400000 /* in Hz */
55 struct stm32_usbphyc {
58 struct udevice *vdda1v1;
59 struct udevice *vdda1v8;
60 struct stm32_usbphyc_phy {
67 static void stm32_usbphyc_get_pll_params(u32 clk_rate,
68 struct pll_params *pll_params)
70 unsigned long long fvco, ndiv, frac;
73 * | FVCO = INFF*2*(NDIV + FRACT/2^16 ) when DITHER_DISABLE[1] = 1
75 * | NDIV = integer part of input bits to set the LDF
76 * | FRACT = fractional part of input bits to set the LDF
77 * => PLLNDIV = integer part of (FVCO / (INFF*2))
78 * => PLLFRACIN = fractional part of(FVCO / INFF*2) * 2^16
79 * <=> PLLFRACIN = ((FVCO / (INFF*2)) - PLLNDIV) * 2^16
81 fvco = (unsigned long long)PLL_FVCO * 1000000; /* In Hz */
84 do_div(ndiv, (clk_rate * 2));
85 pll_params->ndiv = (u8)ndiv;
87 frac = fvco * (1 << 16);
88 do_div(frac, (clk_rate * 2));
89 frac = frac - (ndiv * (1 << 16));
90 pll_params->frac = (u16)frac;
93 static int stm32_usbphyc_pll_init(struct stm32_usbphyc *usbphyc)
95 struct pll_params pll_params;
96 u32 clk_rate = clk_get_rate(&usbphyc->clk);
99 if ((clk_rate < PLL_INFF_MIN_RATE) || (clk_rate > PLL_INFF_MAX_RATE)) {
100 pr_debug("%s: input clk freq (%dHz) out of range\n",
105 stm32_usbphyc_get_pll_params(clk_rate, &pll_params);
107 usbphyc_pll = PLLDITHEN1 | PLLDITHEN0 | PLLSTRBYP;
108 usbphyc_pll |= ((pll_params.ndiv << PLLNDIV_SHIFT) & PLLNDIV);
110 if (pll_params.frac) {
111 usbphyc_pll |= PLLFRACCTL;
112 usbphyc_pll |= ((pll_params.frac << PLLFRACIN_SHIFT)
116 writel(usbphyc_pll, usbphyc->base + STM32_USBPHYC_PLL);
118 pr_debug("%s: input clk freq=%dHz, ndiv=%d, frac=%d\n", __func__,
119 clk_rate, pll_params.ndiv, pll_params.frac);
124 static bool stm32_usbphyc_is_init(struct stm32_usbphyc *usbphyc)
128 for (i = 0; i < MAX_PHYS; i++) {
129 if (usbphyc->phys[i].init)
136 static bool stm32_usbphyc_is_powered(struct stm32_usbphyc *usbphyc)
140 for (i = 0; i < MAX_PHYS; i++) {
141 if (usbphyc->phys[i].powered)
148 static int stm32_usbphyc_phy_init(struct phy *phy)
150 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
151 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
152 bool pllen = readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN ?
156 pr_debug("%s phy ID = %lu\n", __func__, phy->id);
157 /* Check if one phy port has already configured the pll */
158 if (pllen && stm32_usbphyc_is_init(usbphyc))
161 if (usbphyc->vdda1v1) {
162 ret = regulator_set_enable(usbphyc->vdda1v1, true);
167 if (usbphyc->vdda1v8) {
168 ret = regulator_set_enable(usbphyc->vdda1v8, true);
174 clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
175 udelay(PLL_PWR_DOWN_TIME_US);
178 ret = stm32_usbphyc_pll_init(usbphyc);
182 setbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
184 /* We must wait PLL_INIT_TIME_US before using PHY */
185 udelay(PLL_INIT_TIME_US);
187 if (!(readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN))
191 usbphyc_phy->init = true;
196 static int stm32_usbphyc_phy_exit(struct phy *phy)
198 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
199 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
202 pr_debug("%s phy ID = %lu\n", __func__, phy->id);
203 usbphyc_phy->init = false;
205 /* Check if other phy port requires pllen */
206 if (stm32_usbphyc_is_init(usbphyc))
209 clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
212 * We must wait PLL_PWR_DOWN_TIME_US before checking that PLLEN
215 udelay(PLL_PWR_DOWN_TIME_US);
217 if (readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN)
220 if (usbphyc->vdda1v1) {
221 ret = regulator_set_enable(usbphyc->vdda1v1, false);
226 if (usbphyc->vdda1v8) {
227 ret = regulator_set_enable(usbphyc->vdda1v8, false);
235 static int stm32_usbphyc_phy_power_on(struct phy *phy)
237 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
238 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
241 pr_debug("%s phy ID = %lu\n", __func__, phy->id);
242 if (usbphyc_phy->vdd) {
243 ret = regulator_set_enable(usbphyc_phy->vdd, true);
248 usbphyc_phy->powered = true;
253 static int stm32_usbphyc_phy_power_off(struct phy *phy)
255 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
256 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
259 pr_debug("%s phy ID = %lu\n", __func__, phy->id);
260 usbphyc_phy->powered = false;
262 if (stm32_usbphyc_is_powered(usbphyc))
265 if (usbphyc_phy->vdd) {
266 ret = regulator_set_enable_if_allowed(usbphyc_phy->vdd, false);
274 static int stm32_usbphyc_get_regulator(struct udevice *dev, ofnode node,
276 struct udevice **regulator)
278 struct ofnode_phandle_args regulator_phandle;
281 ret = ofnode_parse_phandle_with_args(node, supply_name,
285 dev_err(dev, "Can't find %s property (%d)\n", supply_name, ret);
289 ret = uclass_get_device_by_ofnode(UCLASS_REGULATOR,
290 regulator_phandle.node,
294 dev_err(dev, "Can't get %s regulator (%d)\n", supply_name, ret);
301 static int stm32_usbphyc_of_xlate(struct phy *phy,
302 struct ofnode_phandle_args *args)
304 if (args->args_count < 1)
307 if (args->args[0] >= MAX_PHYS)
310 phy->id = args->args[0];
312 if ((phy->id == 0 && args->args_count != 1) ||
313 (phy->id == 1 && args->args_count != 2)) {
314 dev_err(dev, "invalid number of cells for phy port%ld\n",
322 static const struct phy_ops stm32_usbphyc_phy_ops = {
323 .init = stm32_usbphyc_phy_init,
324 .exit = stm32_usbphyc_phy_exit,
325 .power_on = stm32_usbphyc_phy_power_on,
326 .power_off = stm32_usbphyc_phy_power_off,
327 .of_xlate = stm32_usbphyc_of_xlate,
330 static int stm32_usbphyc_probe(struct udevice *dev)
332 struct stm32_usbphyc *usbphyc = dev_get_priv(dev);
333 struct reset_ctl reset;
337 usbphyc->base = dev_read_addr(dev);
338 if (usbphyc->base == FDT_ADDR_T_NONE)
342 ret = clk_get_by_index(dev, 0, &usbphyc->clk);
346 ret = clk_enable(&usbphyc->clk);
351 ret = reset_get_by_index(dev, 0, &reset);
353 reset_assert(&reset);
355 reset_deassert(&reset);
358 /* get usbphyc regulator */
359 ret = device_get_supply_regulator(dev, "vdda1v1-supply",
362 dev_err(dev, "Can't get vdda1v1-supply regulator\n");
366 ret = device_get_supply_regulator(dev, "vdda1v8-supply",
369 dev_err(dev, "Can't get vdda1v8-supply regulator\n");
374 * parse all PHY subnodes in order to populate regulator associated
377 node = dev_read_first_subnode(dev);
378 for (i = 0; i < MAX_PHYS; i++) {
379 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + i;
381 usbphyc_phy->init = false;
382 usbphyc_phy->powered = false;
383 ret = stm32_usbphyc_get_regulator(dev, node, "phy-supply",
388 node = dev_read_next_subnode(node);
391 /* Check if second port has to be used for host controller */
392 if (dev_read_bool(dev, "st,port2-switch-to-host"))
393 setbits_le32(usbphyc->base + STM32_USBPHYC_MISC, SWITHOST);
398 static const struct udevice_id stm32_usbphyc_of_match[] = {
399 { .compatible = "st,stm32mp1-usbphyc", },
403 U_BOOT_DRIVER(stm32_usb_phyc) = {
404 .name = "stm32-usbphyc",
406 .of_match = stm32_usbphyc_of_match,
407 .ops = &stm32_usbphyc_phy_ops,
408 .probe = stm32_usbphyc_probe,
409 .priv_auto_alloc_size = sizeof(struct stm32_usbphyc),