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 {
68 static void stm32_usbphyc_get_pll_params(u32 clk_rate,
69 struct pll_params *pll_params)
71 unsigned long long fvco, ndiv, frac;
74 * | FVCO = INFF*2*(NDIV + FRACT/2^16 ) when DITHER_DISABLE[1] = 1
76 * | NDIV = integer part of input bits to set the LDF
77 * | FRACT = fractional part of input bits to set the LDF
78 * => PLLNDIV = integer part of (FVCO / (INFF*2))
79 * => PLLFRACIN = fractional part of(FVCO / INFF*2) * 2^16
80 * <=> PLLFRACIN = ((FVCO / (INFF*2)) - PLLNDIV) * 2^16
82 fvco = (unsigned long long)PLL_FVCO * 1000000; /* In Hz */
85 do_div(ndiv, (clk_rate * 2));
86 pll_params->ndiv = (u8)ndiv;
88 frac = fvco * (1 << 16);
89 do_div(frac, (clk_rate * 2));
90 frac = frac - (ndiv * (1 << 16));
91 pll_params->frac = (u16)frac;
94 static int stm32_usbphyc_pll_init(struct stm32_usbphyc *usbphyc)
96 struct pll_params pll_params;
97 u32 clk_rate = clk_get_rate(&usbphyc->clk);
100 if ((clk_rate < PLL_INFF_MIN_RATE) || (clk_rate > PLL_INFF_MAX_RATE)) {
101 pr_debug("%s: input clk freq (%dHz) out of range\n",
106 stm32_usbphyc_get_pll_params(clk_rate, &pll_params);
108 usbphyc_pll = PLLDITHEN1 | PLLDITHEN0 | PLLSTRBYP;
109 usbphyc_pll |= ((pll_params.ndiv << PLLNDIV_SHIFT) & PLLNDIV);
111 if (pll_params.frac) {
112 usbphyc_pll |= PLLFRACCTL;
113 usbphyc_pll |= ((pll_params.frac << PLLFRACIN_SHIFT)
117 writel(usbphyc_pll, usbphyc->base + STM32_USBPHYC_PLL);
119 pr_debug("%s: input clk freq=%dHz, ndiv=%d, frac=%d\n", __func__,
120 clk_rate, pll_params.ndiv, pll_params.frac);
125 static bool stm32_usbphyc_is_init(struct stm32_usbphyc *usbphyc)
129 for (i = 0; i < MAX_PHYS; i++) {
130 if (usbphyc->phys[i].init)
137 static bool stm32_usbphyc_is_powered(struct stm32_usbphyc *usbphyc)
141 for (i = 0; i < MAX_PHYS; i++) {
142 if (usbphyc->phys[i].powered)
149 static int stm32_usbphyc_phy_init(struct phy *phy)
151 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
152 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
153 bool pllen = readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN ?
157 pr_debug("%s phy ID = %lu\n", __func__, phy->id);
158 /* Check if one phy port has already configured the pll */
159 if (pllen && stm32_usbphyc_is_init(usbphyc))
162 if (usbphyc->vdda1v1) {
163 ret = regulator_set_enable(usbphyc->vdda1v1, true);
168 if (usbphyc->vdda1v8) {
169 ret = regulator_set_enable(usbphyc->vdda1v8, true);
175 clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
176 udelay(PLL_PWR_DOWN_TIME_US);
179 ret = stm32_usbphyc_pll_init(usbphyc);
183 setbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
185 /* We must wait PLL_INIT_TIME_US before using PHY */
186 udelay(PLL_INIT_TIME_US);
188 if (!(readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN))
192 usbphyc_phy->init = true;
197 static int stm32_usbphyc_phy_exit(struct phy *phy)
199 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
200 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
203 pr_debug("%s phy ID = %lu\n", __func__, phy->id);
204 usbphyc_phy->init = false;
206 /* Check if other phy port requires pllen */
207 if (stm32_usbphyc_is_init(usbphyc))
210 clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
213 * We must wait PLL_PWR_DOWN_TIME_US before checking that PLLEN
216 udelay(PLL_PWR_DOWN_TIME_US);
218 if (readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN)
221 if (usbphyc->vdda1v1) {
222 ret = regulator_set_enable(usbphyc->vdda1v1, false);
227 if (usbphyc->vdda1v8) {
228 ret = regulator_set_enable(usbphyc->vdda1v8, false);
236 static int stm32_usbphyc_phy_power_on(struct phy *phy)
238 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
239 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
242 pr_debug("%s phy ID = %lu\n", __func__, phy->id);
243 if (usbphyc_phy->vdd) {
244 ret = regulator_set_enable(usbphyc_phy->vdd, true);
248 if (usbphyc_phy->vbus) {
249 ret = regulator_set_enable(usbphyc_phy->vbus, true);
254 usbphyc_phy->powered = true;
259 static int stm32_usbphyc_phy_power_off(struct phy *phy)
261 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
262 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
265 pr_debug("%s phy ID = %lu\n", __func__, phy->id);
266 usbphyc_phy->powered = false;
268 if (stm32_usbphyc_is_powered(usbphyc))
271 if (usbphyc_phy->vbus) {
272 ret = regulator_set_enable(usbphyc_phy->vbus, false);
276 if (usbphyc_phy->vdd) {
277 ret = regulator_set_enable_if_allowed(usbphyc_phy->vdd, false);
285 static int stm32_usbphyc_get_regulator(ofnode node,
287 struct udevice **regulator)
289 struct ofnode_phandle_args regulator_phandle;
292 ret = ofnode_parse_phandle_with_args(node, supply_name,
298 ret = uclass_get_device_by_ofnode(UCLASS_REGULATOR,
299 regulator_phandle.node,
307 static int stm32_usbphyc_of_xlate(struct phy *phy,
308 struct ofnode_phandle_args *args)
310 if (args->args_count < 1)
313 if (args->args[0] >= MAX_PHYS)
316 phy->id = args->args[0];
318 if ((phy->id == 0 && args->args_count != 1) ||
319 (phy->id == 1 && args->args_count != 2)) {
320 dev_err(phy->dev, "invalid number of cells for phy port%ld\n",
328 static const struct phy_ops stm32_usbphyc_phy_ops = {
329 .init = stm32_usbphyc_phy_init,
330 .exit = stm32_usbphyc_phy_exit,
331 .power_on = stm32_usbphyc_phy_power_on,
332 .power_off = stm32_usbphyc_phy_power_off,
333 .of_xlate = stm32_usbphyc_of_xlate,
336 static int stm32_usbphyc_probe(struct udevice *dev)
338 struct stm32_usbphyc *usbphyc = dev_get_priv(dev);
339 struct reset_ctl reset;
343 usbphyc->base = dev_read_addr(dev);
344 if (usbphyc->base == FDT_ADDR_T_NONE)
348 ret = clk_get_by_index(dev, 0, &usbphyc->clk);
352 ret = clk_enable(&usbphyc->clk);
357 ret = reset_get_by_index(dev, 0, &reset);
359 reset_assert(&reset);
361 reset_deassert(&reset);
364 /* get usbphyc regulator */
365 ret = device_get_supply_regulator(dev, "vdda1v1-supply",
368 dev_err(dev, "Can't get vdda1v1-supply regulator\n");
372 ret = device_get_supply_regulator(dev, "vdda1v8-supply",
375 dev_err(dev, "Can't get vdda1v8-supply regulator\n");
380 * parse all PHY subnodes in order to populate regulator associated
383 node = dev_read_first_subnode(dev);
384 for (i = 0; i < MAX_PHYS; i++) {
385 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + i;
387 usbphyc_phy->init = false;
388 usbphyc_phy->powered = false;
389 ret = stm32_usbphyc_get_regulator(node, "phy-supply",
392 dev_err(dev, "Can't get phy-supply regulator\n");
396 ret = stm32_usbphyc_get_regulator(node, "vbus-supply",
399 usbphyc_phy->vbus = NULL;
401 node = dev_read_next_subnode(node);
404 /* Check if second port has to be used for host controller */
405 if (dev_read_bool(dev, "st,port2-switch-to-host"))
406 setbits_le32(usbphyc->base + STM32_USBPHYC_MISC, SWITHOST);
411 static const struct udevice_id stm32_usbphyc_of_match[] = {
412 { .compatible = "st,stm32mp1-usbphyc", },
416 U_BOOT_DRIVER(stm32_usb_phyc) = {
417 .name = "stm32-usbphyc",
419 .of_match = stm32_usbphyc_of_match,
420 .ops = &stm32_usbphyc_phy_ops,
421 .probe = stm32_usbphyc_probe,
422 .priv_auto = sizeof(struct stm32_usbphyc),