Tom Rini | 4606fc7 | 2018-05-20 09:47:45 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2018, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
Patrick Delaunay | d182ee5 | 2020-11-06 19:01:51 +0100 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_PHY |
| 7 | |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <clk.h> |
| 10 | #include <div64.h> |
| 11 | #include <dm.h> |
| 12 | #include <fdtdec.h> |
| 13 | #include <generic-phy.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 15 | #include <reset.h> |
| 16 | #include <syscon.h> |
| 17 | #include <usb.h> |
| 18 | #include <asm/io.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 19 | #include <dm/device_compat.h> |
Patrice Chotard | 6a07f77 | 2022-04-22 09:39:00 +0200 | [diff] [blame] | 20 | #include <dm/of_access.h> |
| 21 | #include <linux/bitfield.h> |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 22 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 23 | #include <linux/delay.h> |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 24 | #include <power/regulator.h> |
| 25 | |
| 26 | /* USBPHYC registers */ |
| 27 | #define STM32_USBPHYC_PLL 0x0 |
| 28 | #define STM32_USBPHYC_MISC 0x8 |
Patrice Chotard | 6a07f77 | 2022-04-22 09:39:00 +0200 | [diff] [blame] | 29 | #define STM32_USBPHYC_TUNE(X) (0x10C + ((X) * 0x100)) |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 30 | |
| 31 | /* STM32_USBPHYC_PLL bit fields */ |
| 32 | #define PLLNDIV GENMASK(6, 0) |
| 33 | #define PLLNDIV_SHIFT 0 |
| 34 | #define PLLFRACIN GENMASK(25, 10) |
| 35 | #define PLLFRACIN_SHIFT 10 |
| 36 | #define PLLEN BIT(26) |
| 37 | #define PLLSTRB BIT(27) |
| 38 | #define PLLSTRBYP BIT(28) |
| 39 | #define PLLFRACCTL BIT(29) |
| 40 | #define PLLDITHEN0 BIT(30) |
| 41 | #define PLLDITHEN1 BIT(31) |
| 42 | |
| 43 | /* STM32_USBPHYC_MISC bit fields */ |
| 44 | #define SWITHOST BIT(0) |
| 45 | |
Patrice Chotard | 6a07f77 | 2022-04-22 09:39:00 +0200 | [diff] [blame] | 46 | /* STM32_USBPHYC_TUNE bit fields */ |
| 47 | #define INCURREN BIT(0) |
| 48 | #define INCURRINT BIT(1) |
| 49 | #define LFSCAPEN BIT(2) |
| 50 | #define HSDRVSLEW BIT(3) |
| 51 | #define HSDRVDCCUR BIT(4) |
| 52 | #define HSDRVDCLEV BIT(5) |
| 53 | #define HSDRVCURINCR BIT(6) |
| 54 | #define FSDRVRFADJ BIT(7) |
| 55 | #define HSDRVRFRED BIT(8) |
| 56 | #define HSDRVCHKITRM GENMASK(12, 9) |
| 57 | #define HSDRVCHKZTRM GENMASK(14, 13) |
| 58 | #define OTPCOMP GENMASK(19, 15) |
| 59 | #define SQLCHCTL GENMASK(21, 20) |
| 60 | #define HDRXGNEQEN BIT(22) |
| 61 | #define HSRXOFF GENMASK(24, 23) |
| 62 | #define HSFALLPREEM BIT(25) |
| 63 | #define SHTCCTCTLPROT BIT(26) |
| 64 | #define STAGSEL BIT(27) |
| 65 | |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 66 | #define MAX_PHYS 2 |
| 67 | |
Patrick Delaunay | 733b316 | 2019-03-29 15:42:13 +0100 | [diff] [blame] | 68 | /* max 100 us for PLL lock and 100 us for PHY init */ |
| 69 | #define PLL_INIT_TIME_US 200 |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 70 | #define PLL_PWR_DOWN_TIME_US 5 |
| 71 | #define PLL_FVCO 2880 /* in MHz */ |
| 72 | #define PLL_INFF_MIN_RATE 19200000 /* in Hz */ |
| 73 | #define PLL_INFF_MAX_RATE 38400000 /* in Hz */ |
| 74 | |
Patrice Chotard | 6a07f77 | 2022-04-22 09:39:00 +0200 | [diff] [blame] | 75 | enum boosting_vals { |
| 76 | BOOST_1000_UA = 1000, |
| 77 | BOOST_2000_UA = 2000, |
| 78 | }; |
| 79 | |
| 80 | enum dc_level_vals { |
| 81 | DC_MINUS_5_TO_7_MV, |
| 82 | DC_PLUS_5_TO_7_MV, |
| 83 | DC_PLUS_10_TO_14_MV, |
| 84 | DC_MAX, |
| 85 | }; |
| 86 | |
| 87 | enum current_trim { |
| 88 | CUR_NOMINAL, |
| 89 | CUR_PLUS_1_56_PCT, |
| 90 | CUR_PLUS_3_12_PCT, |
| 91 | CUR_PLUS_4_68_PCT, |
| 92 | CUR_PLUS_6_24_PCT, |
| 93 | CUR_PLUS_7_8_PCT, |
| 94 | CUR_PLUS_9_36_PCT, |
| 95 | CUR_PLUS_10_92_PCT, |
| 96 | CUR_PLUS_12_48_PCT, |
| 97 | CUR_PLUS_14_04_PCT, |
| 98 | CUR_PLUS_15_6_PCT, |
| 99 | CUR_PLUS_17_16_PCT, |
| 100 | CUR_PLUS_19_01_PCT, |
| 101 | CUR_PLUS_20_58_PCT, |
| 102 | CUR_PLUS_22_16_PCT, |
| 103 | CUR_PLUS_23_73_PCT, |
| 104 | CUR_MAX, |
| 105 | }; |
| 106 | |
| 107 | enum impedance_trim { |
| 108 | IMP_NOMINAL, |
| 109 | IMP_MINUS_2_OHMS, |
| 110 | IMP_MINUS_4_OMHS, |
| 111 | IMP_MINUS_6_OHMS, |
| 112 | IMP_MAX, |
| 113 | }; |
| 114 | |
| 115 | enum squelch_level { |
| 116 | SQLCH_NOMINAL, |
| 117 | SQLCH_PLUS_7_MV, |
| 118 | SQLCH_MINUS_5_MV, |
| 119 | SQLCH_PLUS_14_MV, |
| 120 | SQLCH_MAX, |
| 121 | }; |
| 122 | |
| 123 | enum rx_offset { |
| 124 | NO_RX_OFFSET, |
| 125 | RX_OFFSET_PLUS_5_MV, |
| 126 | RX_OFFSET_PLUS_10_MV, |
| 127 | RX_OFFSET_MINUS_5_MV, |
| 128 | RX_OFFSET_MAX, |
| 129 | }; |
| 130 | |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 131 | struct pll_params { |
| 132 | u8 ndiv; |
| 133 | u16 frac; |
| 134 | }; |
| 135 | |
| 136 | struct stm32_usbphyc { |
| 137 | fdt_addr_t base; |
| 138 | struct clk clk; |
Patrick Delaunay | 6ba88cd | 2019-03-29 15:42:11 +0100 | [diff] [blame] | 139 | struct udevice *vdda1v1; |
| 140 | struct udevice *vdda1v8; |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 141 | struct stm32_usbphyc_phy { |
| 142 | struct udevice *vdd; |
Patrick Delaunay | c8a755d | 2020-10-15 14:50:57 +0200 | [diff] [blame] | 143 | struct udevice *vbus; |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 144 | bool init; |
| 145 | bool powered; |
| 146 | } phys[MAX_PHYS]; |
| 147 | }; |
| 148 | |
Patrick Delaunay | 1effca2 | 2019-03-29 15:42:12 +0100 | [diff] [blame] | 149 | static void stm32_usbphyc_get_pll_params(u32 clk_rate, |
| 150 | struct pll_params *pll_params) |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 151 | { |
| 152 | unsigned long long fvco, ndiv, frac; |
| 153 | |
| 154 | /* |
| 155 | * | FVCO = INFF*2*(NDIV + FRACT/2^16 ) when DITHER_DISABLE[1] = 1 |
| 156 | * | FVCO = 2880MHz |
| 157 | * | NDIV = integer part of input bits to set the LDF |
| 158 | * | FRACT = fractional part of input bits to set the LDF |
| 159 | * => PLLNDIV = integer part of (FVCO / (INFF*2)) |
| 160 | * => PLLFRACIN = fractional part of(FVCO / INFF*2) * 2^16 |
| 161 | * <=> PLLFRACIN = ((FVCO / (INFF*2)) - PLLNDIV) * 2^16 |
| 162 | */ |
| 163 | fvco = (unsigned long long)PLL_FVCO * 1000000; /* In Hz */ |
| 164 | |
| 165 | ndiv = fvco; |
| 166 | do_div(ndiv, (clk_rate * 2)); |
| 167 | pll_params->ndiv = (u8)ndiv; |
| 168 | |
| 169 | frac = fvco * (1 << 16); |
| 170 | do_div(frac, (clk_rate * 2)); |
| 171 | frac = frac - (ndiv * (1 << 16)); |
| 172 | pll_params->frac = (u16)frac; |
| 173 | } |
| 174 | |
| 175 | static int stm32_usbphyc_pll_init(struct stm32_usbphyc *usbphyc) |
| 176 | { |
| 177 | struct pll_params pll_params; |
| 178 | u32 clk_rate = clk_get_rate(&usbphyc->clk); |
| 179 | u32 usbphyc_pll; |
| 180 | |
| 181 | if ((clk_rate < PLL_INFF_MIN_RATE) || (clk_rate > PLL_INFF_MAX_RATE)) { |
Patrick Delaunay | d182ee5 | 2020-11-06 19:01:51 +0100 | [diff] [blame] | 182 | log_debug("input clk freq (%dHz) out of range\n", |
| 183 | clk_rate); |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 184 | return -EINVAL; |
| 185 | } |
| 186 | |
| 187 | stm32_usbphyc_get_pll_params(clk_rate, &pll_params); |
| 188 | |
| 189 | usbphyc_pll = PLLDITHEN1 | PLLDITHEN0 | PLLSTRBYP; |
| 190 | usbphyc_pll |= ((pll_params.ndiv << PLLNDIV_SHIFT) & PLLNDIV); |
| 191 | |
| 192 | if (pll_params.frac) { |
| 193 | usbphyc_pll |= PLLFRACCTL; |
| 194 | usbphyc_pll |= ((pll_params.frac << PLLFRACIN_SHIFT) |
| 195 | & PLLFRACIN); |
| 196 | } |
| 197 | |
| 198 | writel(usbphyc_pll, usbphyc->base + STM32_USBPHYC_PLL); |
| 199 | |
Patrick Delaunay | d182ee5 | 2020-11-06 19:01:51 +0100 | [diff] [blame] | 200 | log_debug("input clk freq=%dHz, ndiv=%d, frac=%d\n", |
| 201 | clk_rate, pll_params.ndiv, pll_params.frac); |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static bool stm32_usbphyc_is_init(struct stm32_usbphyc *usbphyc) |
| 207 | { |
| 208 | int i; |
| 209 | |
| 210 | for (i = 0; i < MAX_PHYS; i++) { |
| 211 | if (usbphyc->phys[i].init) |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | static bool stm32_usbphyc_is_powered(struct stm32_usbphyc *usbphyc) |
| 219 | { |
| 220 | int i; |
| 221 | |
| 222 | for (i = 0; i < MAX_PHYS; i++) { |
| 223 | if (usbphyc->phys[i].powered) |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | static int stm32_usbphyc_phy_init(struct phy *phy) |
| 231 | { |
| 232 | struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev); |
| 233 | struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id; |
| 234 | bool pllen = readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN ? |
| 235 | true : false; |
| 236 | int ret; |
| 237 | |
Patrick Delaunay | d182ee5 | 2020-11-06 19:01:51 +0100 | [diff] [blame] | 238 | dev_dbg(phy->dev, "phy ID = %lu\n", phy->id); |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 239 | /* Check if one phy port has already configured the pll */ |
| 240 | if (pllen && stm32_usbphyc_is_init(usbphyc)) |
| 241 | goto initialized; |
| 242 | |
Patrick Delaunay | 1effca2 | 2019-03-29 15:42:12 +0100 | [diff] [blame] | 243 | if (usbphyc->vdda1v1) { |
| 244 | ret = regulator_set_enable(usbphyc->vdda1v1, true); |
| 245 | if (ret) |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | if (usbphyc->vdda1v8) { |
| 250 | ret = regulator_set_enable(usbphyc->vdda1v8, true); |
| 251 | if (ret) |
| 252 | return ret; |
| 253 | } |
| 254 | |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 255 | if (pllen) { |
| 256 | clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN); |
| 257 | udelay(PLL_PWR_DOWN_TIME_US); |
| 258 | } |
| 259 | |
| 260 | ret = stm32_usbphyc_pll_init(usbphyc); |
| 261 | if (ret) |
| 262 | return ret; |
| 263 | |
| 264 | setbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN); |
| 265 | |
Patrick Delaunay | 733b316 | 2019-03-29 15:42:13 +0100 | [diff] [blame] | 266 | /* We must wait PLL_INIT_TIME_US before using PHY */ |
| 267 | udelay(PLL_INIT_TIME_US); |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 268 | |
| 269 | if (!(readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN)) |
| 270 | return -EIO; |
| 271 | |
| 272 | initialized: |
| 273 | usbphyc_phy->init = true; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int stm32_usbphyc_phy_exit(struct phy *phy) |
| 279 | { |
| 280 | struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev); |
| 281 | struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id; |
Patrick Delaunay | 1effca2 | 2019-03-29 15:42:12 +0100 | [diff] [blame] | 282 | int ret; |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 283 | |
Patrick Delaunay | d182ee5 | 2020-11-06 19:01:51 +0100 | [diff] [blame] | 284 | dev_dbg(phy->dev, "phy ID = %lu\n", phy->id); |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 285 | usbphyc_phy->init = false; |
| 286 | |
| 287 | /* Check if other phy port requires pllen */ |
| 288 | if (stm32_usbphyc_is_init(usbphyc)) |
| 289 | return 0; |
| 290 | |
| 291 | clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN); |
| 292 | |
| 293 | /* |
| 294 | * We must wait PLL_PWR_DOWN_TIME_US before checking that PLLEN |
| 295 | * bit is still clear |
| 296 | */ |
| 297 | udelay(PLL_PWR_DOWN_TIME_US); |
| 298 | |
| 299 | if (readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN) |
| 300 | return -EIO; |
| 301 | |
Patrick Delaunay | 6ba88cd | 2019-03-29 15:42:11 +0100 | [diff] [blame] | 302 | if (usbphyc->vdda1v1) { |
Patrick Delaunay | 1effca2 | 2019-03-29 15:42:12 +0100 | [diff] [blame] | 303 | ret = regulator_set_enable(usbphyc->vdda1v1, false); |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 304 | if (ret) |
| 305 | return ret; |
| 306 | } |
| 307 | |
Patrick Delaunay | 6ba88cd | 2019-03-29 15:42:11 +0100 | [diff] [blame] | 308 | if (usbphyc->vdda1v8) { |
Patrick Delaunay | 1effca2 | 2019-03-29 15:42:12 +0100 | [diff] [blame] | 309 | ret = regulator_set_enable(usbphyc->vdda1v8, false); |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 310 | if (ret) |
| 311 | return ret; |
| 312 | } |
Patrick Delaunay | 6ba88cd | 2019-03-29 15:42:11 +0100 | [diff] [blame] | 313 | |
Patrick Delaunay | 1effca2 | 2019-03-29 15:42:12 +0100 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int stm32_usbphyc_phy_power_on(struct phy *phy) |
| 318 | { |
| 319 | struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev); |
| 320 | struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id; |
| 321 | int ret; |
| 322 | |
Patrick Delaunay | d182ee5 | 2020-11-06 19:01:51 +0100 | [diff] [blame] | 323 | dev_dbg(phy->dev, "phy ID = %lu\n", phy->id); |
Patrick Delaunay | 1effca2 | 2019-03-29 15:42:12 +0100 | [diff] [blame] | 324 | if (usbphyc_phy->vdd) { |
| 325 | ret = regulator_set_enable(usbphyc_phy->vdd, true); |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 326 | if (ret) |
| 327 | return ret; |
| 328 | } |
Patrick Delaunay | c8a755d | 2020-10-15 14:50:57 +0200 | [diff] [blame] | 329 | if (usbphyc_phy->vbus) { |
| 330 | ret = regulator_set_enable(usbphyc_phy->vbus, true); |
| 331 | if (ret) |
| 332 | return ret; |
| 333 | } |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 334 | |
| 335 | usbphyc_phy->powered = true; |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int stm32_usbphyc_phy_power_off(struct phy *phy) |
| 341 | { |
| 342 | struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev); |
| 343 | struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id; |
| 344 | int ret; |
| 345 | |
Patrick Delaunay | d182ee5 | 2020-11-06 19:01:51 +0100 | [diff] [blame] | 346 | dev_dbg(phy->dev, "phy ID = %lu\n", phy->id); |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 347 | usbphyc_phy->powered = false; |
| 348 | |
| 349 | if (stm32_usbphyc_is_powered(usbphyc)) |
| 350 | return 0; |
| 351 | |
Patrick Delaunay | c8a755d | 2020-10-15 14:50:57 +0200 | [diff] [blame] | 352 | if (usbphyc_phy->vbus) { |
| 353 | ret = regulator_set_enable(usbphyc_phy->vbus, false); |
| 354 | if (ret) |
| 355 | return ret; |
| 356 | } |
Patrick Delaunay | 1effca2 | 2019-03-29 15:42:12 +0100 | [diff] [blame] | 357 | if (usbphyc_phy->vdd) { |
Patrick Delaunay | 611bba8 | 2020-07-03 19:13:02 +0200 | [diff] [blame] | 358 | ret = regulator_set_enable_if_allowed(usbphyc_phy->vdd, false); |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 359 | if (ret) |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Patrick Delaunay | c8a755d | 2020-10-15 14:50:57 +0200 | [diff] [blame] | 366 | static int stm32_usbphyc_get_regulator(ofnode node, |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 367 | char *supply_name, |
| 368 | struct udevice **regulator) |
| 369 | { |
| 370 | struct ofnode_phandle_args regulator_phandle; |
| 371 | int ret; |
| 372 | |
| 373 | ret = ofnode_parse_phandle_with_args(node, supply_name, |
| 374 | NULL, 0, 0, |
| 375 | ®ulator_phandle); |
Patrick Delaunay | c8a755d | 2020-10-15 14:50:57 +0200 | [diff] [blame] | 376 | if (ret) |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 377 | return ret; |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 378 | |
| 379 | ret = uclass_get_device_by_ofnode(UCLASS_REGULATOR, |
| 380 | regulator_phandle.node, |
| 381 | regulator); |
Patrick Delaunay | c8a755d | 2020-10-15 14:50:57 +0200 | [diff] [blame] | 382 | if (ret) |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 383 | return ret; |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int stm32_usbphyc_of_xlate(struct phy *phy, |
| 389 | struct ofnode_phandle_args *args) |
| 390 | { |
Patrick Delaunay | 2ad166e | 2019-03-29 15:42:10 +0100 | [diff] [blame] | 391 | if (args->args_count < 1) |
| 392 | return -ENODEV; |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 393 | |
| 394 | if (args->args[0] >= MAX_PHYS) |
| 395 | return -ENODEV; |
| 396 | |
Patrick Delaunay | 2ad166e | 2019-03-29 15:42:10 +0100 | [diff] [blame] | 397 | phy->id = args->args[0]; |
| 398 | |
| 399 | if ((phy->id == 0 && args->args_count != 1) || |
| 400 | (phy->id == 1 && args->args_count != 2)) { |
Sean Anderson | 30080c3 | 2020-09-15 10:45:06 -0400 | [diff] [blame] | 401 | dev_err(phy->dev, "invalid number of cells for phy port%ld\n", |
Patrick Delaunay | 2ad166e | 2019-03-29 15:42:10 +0100 | [diff] [blame] | 402 | phy->id); |
| 403 | return -EINVAL; |
| 404 | } |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
Patrice Chotard | 6a07f77 | 2022-04-22 09:39:00 +0200 | [diff] [blame] | 409 | static void stm32_usbphyc_tuning(struct udevice *dev, ofnode node, u32 index) |
| 410 | { |
| 411 | struct stm32_usbphyc *usbphyc = dev_get_priv(dev); |
| 412 | u32 reg = STM32_USBPHYC_TUNE(index); |
| 413 | u32 otpcomp, val, tune = 0; |
| 414 | int ret; |
| 415 | |
| 416 | /* Backup OTP compensation code */ |
| 417 | otpcomp = FIELD_GET(OTPCOMP, readl(usbphyc->base + reg)); |
| 418 | |
| 419 | ret = ofnode_read_u32(node, "st,current-boost-microamp", &val); |
| 420 | if (!ret && (val == BOOST_1000_UA || val == BOOST_2000_UA)) { |
| 421 | val = (val == BOOST_2000_UA) ? 1 : 0; |
| 422 | tune |= INCURREN | FIELD_PREP(INCURRINT, val); |
| 423 | } else if (ret != -EINVAL) { |
| 424 | dev_warn(dev, "phy%d: invalid st,current-boost-microamp value\n", index); |
| 425 | } |
| 426 | |
| 427 | if (!ofnode_read_bool(node, "st,no-lsfs-fb-cap")) |
| 428 | tune |= LFSCAPEN; |
| 429 | |
| 430 | if (ofnode_read_bool(node, "st,decrease-hs-slew-rate")) |
| 431 | tune |= HSDRVSLEW; |
| 432 | |
| 433 | ret = ofnode_read_u32(node, "st,tune-hs-dc-level", &val); |
| 434 | if (!ret && val < DC_MAX) { |
| 435 | if (val == DC_MINUS_5_TO_7_MV) { |
| 436 | tune |= HSDRVDCCUR; |
| 437 | } else { |
| 438 | val = (val == DC_PLUS_10_TO_14_MV) ? 1 : 0; |
| 439 | tune |= HSDRVCURINCR | FIELD_PREP(HSDRVDCLEV, val); |
| 440 | } |
| 441 | } else if (ret != -EINVAL) { |
| 442 | dev_warn(dev, "phy%d: invalid st,tune-hs-dc-level value\n", index); |
| 443 | } |
| 444 | |
| 445 | if (ofnode_read_bool(node, "st,enable-fs-rftime-tuning")) |
| 446 | tune |= FSDRVRFADJ; |
| 447 | |
| 448 | if (ofnode_read_bool(node, "st,enable-hs-rftime-reduction")) |
| 449 | tune |= HSDRVRFRED; |
| 450 | |
| 451 | ret = ofnode_read_u32(node, "st,trim-hs-current", &val); |
| 452 | if (!ret && val < CUR_MAX) |
| 453 | tune |= FIELD_PREP(HSDRVCHKITRM, val); |
| 454 | else if (ret != -EINVAL) |
| 455 | dev_warn(dev, "phy%d: invalid st,trim-hs-current value\n", index); |
| 456 | |
| 457 | ret = ofnode_read_u32(node, "st,trim-hs-impedance", &val); |
| 458 | if (!ret && val < IMP_MAX) |
| 459 | tune |= FIELD_PREP(HSDRVCHKZTRM, val); |
| 460 | else if (ret != -EINVAL) |
| 461 | dev_warn(dev, "phy%d: invalid trim-hs-impedance value\n", index); |
| 462 | |
| 463 | ret = ofnode_read_u32(node, "st,tune-squelch-level", &val); |
| 464 | if (!ret && val < SQLCH_MAX) |
| 465 | tune |= FIELD_PREP(SQLCHCTL, val); |
| 466 | else if (ret != -EINVAL) |
| 467 | dev_warn(dev, "phy%d: invalid st,tune-squelch-level value\n", index); |
| 468 | |
| 469 | if (ofnode_read_bool(node, "st,enable-hs-rx-gain-eq")) |
| 470 | tune |= HDRXGNEQEN; |
| 471 | |
| 472 | ret = ofnode_read_u32(node, "st,tune-hs-rx-offset", &val); |
| 473 | if (!ret && val < RX_OFFSET_MAX) |
| 474 | tune |= FIELD_PREP(HSRXOFF, val); |
| 475 | else if (ret != -EINVAL) |
| 476 | dev_warn(dev, "phy%d: invalid st,tune-hs-rx-offset value\n", index); |
| 477 | |
| 478 | if (ofnode_read_bool(node, "st,no-hs-ftime-ctrl")) |
| 479 | tune |= HSFALLPREEM; |
| 480 | |
| 481 | if (!ofnode_read_bool(node, "st,no-lsfs-sc")) |
| 482 | tune |= SHTCCTCTLPROT; |
| 483 | |
| 484 | if (ofnode_read_bool(node, "st,enable-hs-tx-staggering")) |
| 485 | tune |= STAGSEL; |
| 486 | |
| 487 | /* Restore OTP compensation code */ |
| 488 | tune |= FIELD_PREP(OTPCOMP, otpcomp); |
| 489 | |
| 490 | writel(tune, usbphyc->base + reg); |
| 491 | } |
| 492 | |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 493 | static const struct phy_ops stm32_usbphyc_phy_ops = { |
| 494 | .init = stm32_usbphyc_phy_init, |
| 495 | .exit = stm32_usbphyc_phy_exit, |
| 496 | .power_on = stm32_usbphyc_phy_power_on, |
| 497 | .power_off = stm32_usbphyc_phy_power_off, |
| 498 | .of_xlate = stm32_usbphyc_of_xlate, |
| 499 | }; |
| 500 | |
| 501 | static int stm32_usbphyc_probe(struct udevice *dev) |
| 502 | { |
| 503 | struct stm32_usbphyc *usbphyc = dev_get_priv(dev); |
| 504 | struct reset_ctl reset; |
Patrick Delaunay | 4bd95e4 | 2021-09-14 14:31:16 +0200 | [diff] [blame] | 505 | ofnode node, connector; |
Patrick Delaunay | 35aa20d | 2021-09-14 14:31:17 +0200 | [diff] [blame] | 506 | int ret; |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 507 | |
| 508 | usbphyc->base = dev_read_addr(dev); |
| 509 | if (usbphyc->base == FDT_ADDR_T_NONE) |
| 510 | return -EINVAL; |
| 511 | |
| 512 | /* Enable clock */ |
| 513 | ret = clk_get_by_index(dev, 0, &usbphyc->clk); |
| 514 | if (ret) |
| 515 | return ret; |
| 516 | |
| 517 | ret = clk_enable(&usbphyc->clk); |
| 518 | if (ret) |
| 519 | return ret; |
| 520 | |
| 521 | /* Reset */ |
| 522 | ret = reset_get_by_index(dev, 0, &reset); |
| 523 | if (!ret) { |
| 524 | reset_assert(&reset); |
| 525 | udelay(2); |
| 526 | reset_deassert(&reset); |
| 527 | } |
| 528 | |
Patrick Delaunay | 6ba88cd | 2019-03-29 15:42:11 +0100 | [diff] [blame] | 529 | /* get usbphyc regulator */ |
| 530 | ret = device_get_supply_regulator(dev, "vdda1v1-supply", |
| 531 | &usbphyc->vdda1v1); |
| 532 | if (ret) { |
| 533 | dev_err(dev, "Can't get vdda1v1-supply regulator\n"); |
| 534 | return ret; |
| 535 | } |
| 536 | |
| 537 | ret = device_get_supply_regulator(dev, "vdda1v8-supply", |
| 538 | &usbphyc->vdda1v8); |
| 539 | if (ret) { |
| 540 | dev_err(dev, "Can't get vdda1v8-supply regulator\n"); |
| 541 | return ret; |
| 542 | } |
| 543 | |
Patrick Delaunay | 35aa20d | 2021-09-14 14:31:17 +0200 | [diff] [blame] | 544 | /* parse all PHY subnodes to populate regulator associated to each PHY port */ |
| 545 | dev_for_each_subnode(node, dev) { |
| 546 | fdt_addr_t phy_id; |
| 547 | struct stm32_usbphyc_phy *usbphyc_phy; |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 548 | |
Patrick Delaunay | 35aa20d | 2021-09-14 14:31:17 +0200 | [diff] [blame] | 549 | phy_id = ofnode_read_u32_default(node, "reg", FDT_ADDR_T_NONE); |
| 550 | if (phy_id >= MAX_PHYS) { |
| 551 | dev_err(dev, "invalid reg value %lx for %s\n", |
| 552 | phy_id, ofnode_get_name(node)); |
| 553 | return -ENOENT; |
| 554 | } |
Patrice Chotard | 6a07f77 | 2022-04-22 09:39:00 +0200 | [diff] [blame] | 555 | |
| 556 | /* Configure phy tuning */ |
| 557 | stm32_usbphyc_tuning(dev, node, phy_id); |
| 558 | |
Patrick Delaunay | 35aa20d | 2021-09-14 14:31:17 +0200 | [diff] [blame] | 559 | usbphyc_phy = usbphyc->phys + phy_id; |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 560 | usbphyc_phy->init = false; |
| 561 | usbphyc_phy->powered = false; |
Patrick Delaunay | c8a755d | 2020-10-15 14:50:57 +0200 | [diff] [blame] | 562 | ret = stm32_usbphyc_get_regulator(node, "phy-supply", |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 563 | &usbphyc_phy->vdd); |
Patrick Delaunay | c8a755d | 2020-10-15 14:50:57 +0200 | [diff] [blame] | 564 | if (ret) { |
| 565 | dev_err(dev, "Can't get phy-supply regulator\n"); |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 566 | return ret; |
Patrick Delaunay | c8a755d | 2020-10-15 14:50:57 +0200 | [diff] [blame] | 567 | } |
| 568 | |
Patrick Delaunay | 4bd95e4 | 2021-09-14 14:31:16 +0200 | [diff] [blame] | 569 | usbphyc_phy->vbus = NULL; |
| 570 | connector = ofnode_find_subnode(node, "connector"); |
| 571 | if (ofnode_valid(connector)) { |
| 572 | ret = stm32_usbphyc_get_regulator(connector, "vbus-supply", |
| 573 | &usbphyc_phy->vbus); |
| 574 | } |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* Check if second port has to be used for host controller */ |
| 578 | if (dev_read_bool(dev, "st,port2-switch-to-host")) |
| 579 | setbits_le32(usbphyc->base + STM32_USBPHYC_MISC, SWITHOST); |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | static const struct udevice_id stm32_usbphyc_of_match[] = { |
| 585 | { .compatible = "st,stm32mp1-usbphyc", }, |
| 586 | { }, |
| 587 | }; |
| 588 | |
| 589 | U_BOOT_DRIVER(stm32_usb_phyc) = { |
| 590 | .name = "stm32-usbphyc", |
| 591 | .id = UCLASS_PHY, |
| 592 | .of_match = stm32_usbphyc_of_match, |
| 593 | .ops = &stm32_usbphyc_phy_ops, |
| 594 | .probe = stm32_usbphyc_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 595 | .priv_auto = sizeof(struct stm32_usbphyc), |
Patrice Chotard | 226d87b | 2018-04-27 11:01:55 +0200 | [diff] [blame] | 596 | }; |