1 // SPDX-License-Identifier: GPL-2.0
3 * STMicroelectronics STM32 USB PHY Controller driver
5 * Copyright (C) 2018 STMicroelectronics
6 * Author(s): Amelie Delaunay <amelie.delaunay@st.com>.
8 #include <linux/bitfield.h>
10 #include <linux/delay.h>
11 #include <linux/iopoll.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of_platform.h>
15 #include <linux/phy/phy.h>
16 #include <linux/reset.h>
18 #define STM32_USBPHYC_PLL 0x0
19 #define STM32_USBPHYC_MISC 0x8
20 #define STM32_USBPHYC_MONITOR(X) (0x108 + ((X) * 0x100))
21 #define STM32_USBPHYC_VERSION 0x3F4
23 /* STM32_USBPHYC_PLL bit fields */
24 #define PLLNDIV GENMASK(6, 0)
25 #define PLLFRACIN GENMASK(25, 10)
27 #define PLLSTRB BIT(27)
28 #define PLLSTRBYP BIT(28)
29 #define PLLFRACCTL BIT(29)
30 #define PLLDITHEN0 BIT(30)
31 #define PLLDITHEN1 BIT(31)
33 /* STM32_USBPHYC_MISC bit fields */
34 #define SWITHOST BIT(0)
36 /* STM32_USBPHYC_MONITOR bit fields */
37 #define STM32_USBPHYC_MON_OUT GENMASK(3, 0)
38 #define STM32_USBPHYC_MON_SEL GENMASK(8, 4)
39 #define STM32_USBPHYC_MON_SEL_LOCKP 0x1F
40 #define STM32_USBPHYC_MON_OUT_LOCKP BIT(3)
42 /* STM32_USBPHYC_VERSION bit fields */
43 #define MINREV GENMASK(3, 0)
44 #define MAJREV GENMASK(7, 4)
46 #define PLL_FVCO_MHZ 2880
47 #define PLL_INFF_MIN_RATE_HZ 19200000
48 #define PLL_INFF_MAX_RATE_HZ 38400000
49 #define HZ_PER_MHZ 1000000L
56 struct stm32_usbphyc_phy {
58 struct stm32_usbphyc *usbphyc;
63 struct stm32_usbphyc {
67 struct reset_control *rst;
68 struct stm32_usbphyc_phy **phys;
70 struct regulator *vdda1v1;
71 struct regulator *vdda1v8;
76 static inline void stm32_usbphyc_set_bits(void __iomem *reg, u32 bits)
78 writel_relaxed(readl_relaxed(reg) | bits, reg);
81 static inline void stm32_usbphyc_clr_bits(void __iomem *reg, u32 bits)
83 writel_relaxed(readl_relaxed(reg) & ~bits, reg);
86 static int stm32_usbphyc_regulators_enable(struct stm32_usbphyc *usbphyc)
90 ret = regulator_enable(usbphyc->vdda1v1);
94 ret = regulator_enable(usbphyc->vdda1v8);
101 regulator_disable(usbphyc->vdda1v1);
106 static int stm32_usbphyc_regulators_disable(struct stm32_usbphyc *usbphyc)
110 ret = regulator_disable(usbphyc->vdda1v8);
114 ret = regulator_disable(usbphyc->vdda1v1);
121 static void stm32_usbphyc_get_pll_params(u32 clk_rate,
122 struct pll_params *pll_params)
124 unsigned long long fvco, ndiv, frac;
127 * | FVCO = INFF*2*(NDIV + FRACT/2^16) when DITHER_DISABLE[1] = 1
130 * | NDIV = integer part of input bits to set the LDF
131 * |_FRACT = fractional part of input bits to set the LDF
132 * => PLLNDIV = integer part of (FVCO / (INFF*2))
133 * => PLLFRACIN = fractional part of(FVCO / INFF*2) * 2^16
134 * <=> PLLFRACIN = ((FVCO / (INFF*2)) - PLLNDIV) * 2^16
136 fvco = (unsigned long long)PLL_FVCO_MHZ * HZ_PER_MHZ;
139 do_div(ndiv, (clk_rate * 2));
140 pll_params->ndiv = (u8)ndiv;
142 frac = fvco * (1 << 16);
143 do_div(frac, (clk_rate * 2));
144 frac = frac - (ndiv * (1 << 16));
145 pll_params->frac = (u16)frac;
148 static int stm32_usbphyc_pll_init(struct stm32_usbphyc *usbphyc)
150 struct pll_params pll_params;
151 u32 clk_rate = clk_get_rate(usbphyc->clk);
155 if ((clk_rate < PLL_INFF_MIN_RATE_HZ) ||
156 (clk_rate > PLL_INFF_MAX_RATE_HZ)) {
157 dev_err(usbphyc->dev, "input clk freq (%dHz) out of range\n",
162 stm32_usbphyc_get_pll_params(clk_rate, &pll_params);
163 ndiv = FIELD_PREP(PLLNDIV, pll_params.ndiv);
164 frac = FIELD_PREP(PLLFRACIN, pll_params.frac);
166 usbphyc_pll = PLLDITHEN1 | PLLDITHEN0 | PLLSTRBYP | ndiv;
169 usbphyc_pll |= PLLFRACCTL | frac;
171 writel_relaxed(usbphyc_pll, usbphyc->base + STM32_USBPHYC_PLL);
173 dev_dbg(usbphyc->dev, "input clk freq=%dHz, ndiv=%lu, frac=%lu\n",
174 clk_rate, FIELD_GET(PLLNDIV, usbphyc_pll),
175 FIELD_GET(PLLFRACIN, usbphyc_pll));
180 static int __stm32_usbphyc_pll_disable(struct stm32_usbphyc *usbphyc)
182 void __iomem *pll_reg = usbphyc->base + STM32_USBPHYC_PLL;
185 stm32_usbphyc_clr_bits(pll_reg, PLLEN);
187 /* Wait for minimum width of powerdown pulse (ENABLE = Low) */
188 if (readl_relaxed_poll_timeout(pll_reg, pllen, !(pllen & PLLEN), 5, 50))
189 dev_err(usbphyc->dev, "PLL not reset\n");
191 return stm32_usbphyc_regulators_disable(usbphyc);
194 static int stm32_usbphyc_pll_disable(struct stm32_usbphyc *usbphyc)
196 /* Check if a phy port is still active or clk48 in use */
197 if (atomic_dec_return(&usbphyc->n_pll_cons) > 0)
200 return __stm32_usbphyc_pll_disable(usbphyc);
203 static int stm32_usbphyc_pll_enable(struct stm32_usbphyc *usbphyc)
205 void __iomem *pll_reg = usbphyc->base + STM32_USBPHYC_PLL;
206 bool pllen = readl_relaxed(pll_reg) & PLLEN;
210 * Check if a phy port or clk48 prepare has configured the pll
211 * and ensure the PLL is enabled
213 if (atomic_inc_return(&usbphyc->n_pll_cons) > 1 && pllen)
218 * PLL shouldn't be enabled without known consumer,
219 * disable it and reinit n_pll_cons
221 dev_warn(usbphyc->dev, "PLL enabled without known consumers\n");
223 ret = __stm32_usbphyc_pll_disable(usbphyc);
228 ret = stm32_usbphyc_regulators_enable(usbphyc);
232 ret = stm32_usbphyc_pll_init(usbphyc);
236 stm32_usbphyc_set_bits(pll_reg, PLLEN);
241 stm32_usbphyc_regulators_disable(usbphyc);
244 atomic_dec(&usbphyc->n_pll_cons);
249 static int stm32_usbphyc_phy_init(struct phy *phy)
251 struct stm32_usbphyc_phy *usbphyc_phy = phy_get_drvdata(phy);
252 struct stm32_usbphyc *usbphyc = usbphyc_phy->usbphyc;
253 u32 reg_mon = STM32_USBPHYC_MONITOR(usbphyc_phy->index);
254 u32 monsel = FIELD_PREP(STM32_USBPHYC_MON_SEL,
255 STM32_USBPHYC_MON_SEL_LOCKP);
259 ret = stm32_usbphyc_pll_enable(usbphyc);
263 /* Check that PLL Lock input to PHY is High */
264 writel_relaxed(monsel, usbphyc->base + reg_mon);
265 ret = readl_relaxed_poll_timeout(usbphyc->base + reg_mon, monout,
266 (monout & STM32_USBPHYC_MON_OUT_LOCKP),
269 dev_err(usbphyc->dev, "PLL Lock input to PHY is Low (val=%x)\n",
270 (u32)(monout & STM32_USBPHYC_MON_OUT));
274 usbphyc_phy->active = true;
279 return stm32_usbphyc_pll_disable(usbphyc);
282 static int stm32_usbphyc_phy_exit(struct phy *phy)
284 struct stm32_usbphyc_phy *usbphyc_phy = phy_get_drvdata(phy);
285 struct stm32_usbphyc *usbphyc = usbphyc_phy->usbphyc;
287 usbphyc_phy->active = false;
289 return stm32_usbphyc_pll_disable(usbphyc);
292 static const struct phy_ops stm32_usbphyc_phy_ops = {
293 .init = stm32_usbphyc_phy_init,
294 .exit = stm32_usbphyc_phy_exit,
295 .owner = THIS_MODULE,
298 static void stm32_usbphyc_switch_setup(struct stm32_usbphyc *usbphyc,
302 stm32_usbphyc_clr_bits(usbphyc->base + STM32_USBPHYC_MISC,
305 stm32_usbphyc_set_bits(usbphyc->base + STM32_USBPHYC_MISC,
307 usbphyc->switch_setup = utmi_switch;
310 static struct phy *stm32_usbphyc_of_xlate(struct device *dev,
311 struct of_phandle_args *args)
313 struct stm32_usbphyc *usbphyc = dev_get_drvdata(dev);
314 struct stm32_usbphyc_phy *usbphyc_phy = NULL;
315 struct device_node *phynode = args->np;
318 for (port = 0; port < usbphyc->nphys; port++) {
319 if (phynode == usbphyc->phys[port]->phy->dev.of_node) {
320 usbphyc_phy = usbphyc->phys[port];
325 dev_err(dev, "failed to find phy\n");
326 return ERR_PTR(-EINVAL);
329 if (((usbphyc_phy->index == 0) && (args->args_count != 0)) ||
330 ((usbphyc_phy->index == 1) && (args->args_count != 1))) {
331 dev_err(dev, "invalid number of cells for phy port%d\n",
333 return ERR_PTR(-EINVAL);
336 /* Configure the UTMI switch for PHY port#2 */
337 if (usbphyc_phy->index == 1) {
338 if (usbphyc->switch_setup < 0) {
339 stm32_usbphyc_switch_setup(usbphyc, args->args[0]);
341 if (args->args[0] != usbphyc->switch_setup) {
342 dev_err(dev, "phy port1 already used\n");
343 return ERR_PTR(-EBUSY);
348 return usbphyc_phy->phy;
351 static int stm32_usbphyc_probe(struct platform_device *pdev)
353 struct stm32_usbphyc *usbphyc;
354 struct device *dev = &pdev->dev;
355 struct device_node *child, *np = dev->of_node;
356 struct phy_provider *phy_provider;
360 usbphyc = devm_kzalloc(dev, sizeof(*usbphyc), GFP_KERNEL);
364 dev_set_drvdata(dev, usbphyc);
366 usbphyc->base = devm_platform_ioremap_resource(pdev, 0);
367 if (IS_ERR(usbphyc->base))
368 return PTR_ERR(usbphyc->base);
370 usbphyc->clk = devm_clk_get(dev, NULL);
371 if (IS_ERR(usbphyc->clk))
372 return dev_err_probe(dev, PTR_ERR(usbphyc->clk), "clk get_failed\n");
374 ret = clk_prepare_enable(usbphyc->clk);
376 dev_err(dev, "clk enable failed: %d\n", ret);
380 usbphyc->rst = devm_reset_control_get(dev, NULL);
381 if (!IS_ERR(usbphyc->rst)) {
382 reset_control_assert(usbphyc->rst);
384 reset_control_deassert(usbphyc->rst);
386 ret = PTR_ERR(usbphyc->rst);
387 if (ret == -EPROBE_DEFER)
390 stm32_usbphyc_clr_bits(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
394 * Wait for minimum width of powerdown pulse (ENABLE = Low):
395 * we have to ensure the PLL is disabled before phys initialization.
397 if (readl_relaxed_poll_timeout(usbphyc->base + STM32_USBPHYC_PLL,
398 pllen, !(pllen & PLLEN), 5, 50)) {
399 dev_warn(usbphyc->dev, "PLL not reset\n");
404 usbphyc->switch_setup = -EINVAL;
405 usbphyc->nphys = of_get_child_count(np);
406 usbphyc->phys = devm_kcalloc(dev, usbphyc->nphys,
407 sizeof(*usbphyc->phys), GFP_KERNEL);
408 if (!usbphyc->phys) {
413 usbphyc->vdda1v1 = devm_regulator_get(dev, "vdda1v1");
414 if (IS_ERR(usbphyc->vdda1v1)) {
415 ret = PTR_ERR(usbphyc->vdda1v1);
416 if (ret != -EPROBE_DEFER)
417 dev_err(dev, "failed to get vdda1v1 supply: %d\n", ret);
421 usbphyc->vdda1v8 = devm_regulator_get(dev, "vdda1v8");
422 if (IS_ERR(usbphyc->vdda1v8)) {
423 ret = PTR_ERR(usbphyc->vdda1v8);
424 if (ret != -EPROBE_DEFER)
425 dev_err(dev, "failed to get vdda1v8 supply: %d\n", ret);
429 for_each_child_of_node(np, child) {
430 struct stm32_usbphyc_phy *usbphyc_phy;
434 phy = devm_phy_create(dev, child, &stm32_usbphyc_phy_ops);
437 if (ret != -EPROBE_DEFER)
438 dev_err(dev, "failed to create phy%d: %d\n",
443 usbphyc_phy = devm_kzalloc(dev, sizeof(*usbphyc_phy),
450 ret = of_property_read_u32(child, "reg", &index);
451 if (ret || index > usbphyc->nphys) {
452 dev_err(&phy->dev, "invalid reg property: %d\n", ret);
456 usbphyc->phys[port] = usbphyc_phy;
457 phy_set_bus_width(phy, 8);
458 phy_set_drvdata(phy, usbphyc_phy);
460 usbphyc->phys[port]->phy = phy;
461 usbphyc->phys[port]->usbphyc = usbphyc;
462 usbphyc->phys[port]->index = index;
463 usbphyc->phys[port]->active = false;
468 phy_provider = devm_of_phy_provider_register(dev,
469 stm32_usbphyc_of_xlate);
470 if (IS_ERR(phy_provider)) {
471 ret = PTR_ERR(phy_provider);
472 dev_err(dev, "failed to register phy provider: %d\n", ret);
476 version = readl_relaxed(usbphyc->base + STM32_USBPHYC_VERSION);
477 dev_info(dev, "registered rev:%lu.%lu\n",
478 FIELD_GET(MAJREV, version), FIELD_GET(MINREV, version));
485 clk_disable_unprepare(usbphyc->clk);
490 static int stm32_usbphyc_remove(struct platform_device *pdev)
492 struct stm32_usbphyc *usbphyc = dev_get_drvdata(&pdev->dev);
495 /* Ensure PHYs are not active, to allow PLL disabling */
496 for (port = 0; port < usbphyc->nphys; port++)
497 if (usbphyc->phys[port]->active)
498 stm32_usbphyc_phy_exit(usbphyc->phys[port]->phy);
500 clk_disable_unprepare(usbphyc->clk);
505 static const struct of_device_id stm32_usbphyc_of_match[] = {
506 { .compatible = "st,stm32mp1-usbphyc", },
509 MODULE_DEVICE_TABLE(of, stm32_usbphyc_of_match);
511 static struct platform_driver stm32_usbphyc_driver = {
512 .probe = stm32_usbphyc_probe,
513 .remove = stm32_usbphyc_remove,
515 .of_match_table = stm32_usbphyc_of_match,
516 .name = "stm32-usbphyc",
519 module_platform_driver(stm32_usbphyc_driver);
521 MODULE_DESCRIPTION("STMicroelectronics STM32 USBPHYC driver");
522 MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
523 MODULE_LICENSE("GPL v2");