Frank Wang | 9354394 | 2023-05-29 13:01:33 +0300 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Rockchip USBDP Combo PHY with Samsung IP block driver |
| 4 | * |
| 5 | * Copyright (C) 2021 Rockchip Electronics Co., Ltd |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <clk.h> |
| 10 | #include <dm.h> |
| 11 | #include <dm/device_compat.h> |
| 12 | #include <dm/devres.h> |
| 13 | #include <dm/lists.h> |
| 14 | #include <dm/of.h> |
| 15 | #include <dm/of_access.h> |
| 16 | #include <generic-phy.h> |
| 17 | #include <linux/bitfield.h> |
| 18 | #include <linux/usb/ch9.h> |
| 19 | #include <linux/usb/otg.h> |
| 20 | #include <regmap.h> |
| 21 | #include <reset.h> |
| 22 | #include <syscon.h> |
| 23 | #include <asm/arch-rockchip/clock.h> |
| 24 | |
| 25 | #include <linux/usb/phy-rockchip-usbdp.h> |
| 26 | |
| 27 | #define BIT_WRITEABLE_SHIFT 16 |
| 28 | |
| 29 | enum { |
| 30 | UDPHY_MODE_NONE = 0, |
| 31 | UDPHY_MODE_USB = BIT(0), |
| 32 | UDPHY_MODE_DP = BIT(1), |
| 33 | UDPHY_MODE_DP_USB = BIT(1) | BIT(0), |
| 34 | }; |
| 35 | |
| 36 | struct udphy_grf_reg { |
| 37 | unsigned int offset; |
| 38 | unsigned int bitend; |
| 39 | unsigned int bitstart; |
| 40 | unsigned int disable; |
| 41 | unsigned int enable; |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * struct reg_sequence - An individual write from a sequence of writes. |
| 46 | * |
| 47 | * @reg: Register address. |
| 48 | * @def: Register value. |
| 49 | * @delay_us: Delay to be applied after the register write in microseconds |
| 50 | * |
| 51 | * Register/value pairs for sequences of writes with an optional delay in |
| 52 | * microseconds to be applied after each write. |
| 53 | */ |
| 54 | struct reg_sequence { |
| 55 | unsigned int reg; |
| 56 | unsigned int def; |
| 57 | unsigned int delay_us; |
| 58 | }; |
| 59 | |
| 60 | struct udphy_grf_cfg { |
| 61 | /* u2phy-grf */ |
| 62 | struct udphy_grf_reg bvalid_phy_con; |
| 63 | struct udphy_grf_reg bvalid_grf_con; |
| 64 | |
| 65 | /* usb-grf */ |
| 66 | struct udphy_grf_reg usb3otg0_cfg; |
| 67 | struct udphy_grf_reg usb3otg1_cfg; |
| 68 | |
| 69 | /* usbdpphy-grf */ |
| 70 | struct udphy_grf_reg low_pwrn; |
| 71 | struct udphy_grf_reg rx_lfps; |
| 72 | }; |
| 73 | |
| 74 | struct rockchip_udphy; |
| 75 | |
| 76 | struct rockchip_udphy_cfg { |
| 77 | /* resets to be requested */ |
| 78 | const char * const *rst_list; |
| 79 | int num_rsts; |
| 80 | |
| 81 | struct udphy_grf_cfg grfcfg; |
| 82 | int (*combophy_init)(struct rockchip_udphy *udphy); |
| 83 | }; |
| 84 | |
| 85 | struct rockchip_udphy { |
| 86 | struct udevice *dev; |
| 87 | struct regmap *pma_regmap; |
| 88 | struct regmap *u2phygrf; |
| 89 | struct regmap *udphygrf; |
| 90 | struct regmap *usbgrf; |
| 91 | struct regmap *vogrf; |
| 92 | |
| 93 | /* clocks and rests */ |
| 94 | struct reset_ctl *rsts; |
| 95 | |
| 96 | /* PHY status management */ |
| 97 | bool flip; |
| 98 | bool mode_change; |
| 99 | u8 mode; |
| 100 | u8 status; |
| 101 | |
| 102 | /* utilized for USB */ |
| 103 | bool hs; /* flag for high-speed */ |
| 104 | |
| 105 | /* utilized for DP */ |
| 106 | struct gpio_desc *sbu1_dc_gpio; |
| 107 | struct gpio_desc *sbu2_dc_gpio; |
| 108 | u32 lane_mux_sel[4]; |
| 109 | u32 dp_lane_sel[4]; |
| 110 | u32 dp_aux_dout_sel; |
| 111 | u32 dp_aux_din_sel; |
| 112 | int id; |
| 113 | |
| 114 | /* PHY const config */ |
| 115 | const struct rockchip_udphy_cfg *cfgs; |
| 116 | }; |
| 117 | |
| 118 | static const struct reg_sequence rk3588_udphy_24m_refclk_cfg[] = { |
| 119 | {0x0090, 0x68}, {0x0094, 0x68}, |
| 120 | {0x0128, 0x24}, {0x012c, 0x44}, |
| 121 | {0x0130, 0x3f}, {0x0134, 0x44}, |
| 122 | {0x015c, 0xa9}, {0x0160, 0x71}, |
| 123 | {0x0164, 0x71}, {0x0168, 0xa9}, |
| 124 | {0x0174, 0xa9}, {0x0178, 0x71}, |
| 125 | {0x017c, 0x71}, {0x0180, 0xa9}, |
| 126 | {0x018c, 0x41}, {0x0190, 0x00}, |
| 127 | {0x0194, 0x05}, {0x01ac, 0x2a}, |
| 128 | {0x01b0, 0x17}, {0x01b4, 0x17}, |
| 129 | {0x01b8, 0x2a}, {0x01c8, 0x04}, |
| 130 | {0x01cc, 0x08}, {0x01d0, 0x08}, |
| 131 | {0x01d4, 0x04}, {0x01d8, 0x20}, |
| 132 | {0x01dc, 0x01}, {0x01e0, 0x09}, |
| 133 | {0x01e4, 0x03}, {0x01f0, 0x29}, |
| 134 | {0x01f4, 0x02}, {0x01f8, 0x02}, |
| 135 | {0x01fc, 0x29}, {0x0208, 0x2a}, |
| 136 | {0x020c, 0x17}, {0x0210, 0x17}, |
| 137 | {0x0214, 0x2a}, {0x0224, 0x20}, |
| 138 | {0x03f0, 0x0d}, {0x03f4, 0x09}, |
| 139 | {0x03f8, 0x09}, {0x03fc, 0x0d}, |
| 140 | {0x0404, 0x0e}, {0x0408, 0x14}, |
| 141 | {0x040c, 0x14}, {0x0410, 0x3b}, |
| 142 | {0x0ce0, 0x68}, {0x0ce8, 0xd0}, |
| 143 | {0x0cf0, 0x87}, {0x0cf8, 0x70}, |
| 144 | {0x0d00, 0x70}, {0x0d08, 0xa9}, |
| 145 | {0x1ce0, 0x68}, {0x1ce8, 0xd0}, |
| 146 | {0x1cf0, 0x87}, {0x1cf8, 0x70}, |
| 147 | {0x1d00, 0x70}, {0x1d08, 0xa9}, |
| 148 | {0x0a3c, 0xd0}, {0x0a44, 0xd0}, |
| 149 | {0x0a48, 0x01}, {0x0a4c, 0x0d}, |
| 150 | {0x0a54, 0xe0}, {0x0a5c, 0xe0}, |
| 151 | {0x0a64, 0xa8}, {0x1a3c, 0xd0}, |
| 152 | {0x1a44, 0xd0}, {0x1a48, 0x01}, |
| 153 | {0x1a4c, 0x0d}, {0x1a54, 0xe0}, |
| 154 | {0x1a5c, 0xe0}, {0x1a64, 0xa8} |
| 155 | }; |
| 156 | |
| 157 | static const struct reg_sequence rk3588_udphy_init_sequence[] = { |
| 158 | {0x0104, 0x44}, {0x0234, 0xE8}, |
| 159 | {0x0248, 0x44}, {0x028C, 0x18}, |
| 160 | {0x081C, 0xE5}, {0x0878, 0x00}, |
| 161 | {0x0994, 0x1C}, {0x0AF0, 0x00}, |
| 162 | {0x181C, 0xE5}, {0x1878, 0x00}, |
| 163 | {0x1994, 0x1C}, {0x1AF0, 0x00}, |
| 164 | {0x0428, 0x60}, {0x0D58, 0x33}, |
| 165 | {0x1D58, 0x33}, {0x0990, 0x74}, |
| 166 | {0x0D64, 0x17}, {0x08C8, 0x13}, |
| 167 | {0x1990, 0x74}, {0x1D64, 0x17}, |
| 168 | {0x18C8, 0x13}, {0x0D90, 0x40}, |
| 169 | {0x0DA8, 0x40}, {0x0DC0, 0x40}, |
| 170 | {0x0DD8, 0x40}, {0x1D90, 0x40}, |
| 171 | {0x1DA8, 0x40}, {0x1DC0, 0x40}, |
| 172 | {0x1DD8, 0x40}, {0x03C0, 0x30}, |
| 173 | {0x03C4, 0x06}, {0x0E10, 0x00}, |
| 174 | {0x1E10, 0x00}, {0x043C, 0x0F}, |
| 175 | {0x0D2C, 0xFF}, {0x1D2C, 0xFF}, |
| 176 | {0x0D34, 0x0F}, {0x1D34, 0x0F}, |
| 177 | {0x08FC, 0x2A}, {0x0914, 0x28}, |
| 178 | {0x0A30, 0x03}, {0x0E38, 0x05}, |
| 179 | {0x0ECC, 0x27}, {0x0ED0, 0x22}, |
| 180 | {0x0ED4, 0x26}, {0x18FC, 0x2A}, |
| 181 | {0x1914, 0x28}, {0x1A30, 0x03}, |
| 182 | {0x1E38, 0x05}, {0x1ECC, 0x27}, |
| 183 | {0x1ED0, 0x22}, {0x1ED4, 0x26}, |
| 184 | {0x0048, 0x0F}, {0x0060, 0x3C}, |
| 185 | {0x0064, 0xF7}, {0x006C, 0x20}, |
| 186 | {0x0070, 0x7D}, {0x0074, 0x68}, |
| 187 | {0x0AF4, 0x1A}, {0x1AF4, 0x1A}, |
| 188 | {0x0440, 0x3F}, {0x10D4, 0x08}, |
| 189 | {0x20D4, 0x08}, {0x00D4, 0x30}, |
| 190 | {0x0024, 0x6e}, |
| 191 | }; |
| 192 | |
| 193 | static inline int grfreg_write(struct regmap *base, |
| 194 | const struct udphy_grf_reg *reg, bool en) |
| 195 | { |
| 196 | u32 val, mask, tmp; |
| 197 | |
| 198 | tmp = en ? reg->enable : reg->disable; |
| 199 | mask = GENMASK(reg->bitend, reg->bitstart); |
| 200 | val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT); |
| 201 | |
| 202 | return regmap_write(base, reg->offset, val); |
| 203 | } |
| 204 | |
| 205 | static int __regmap_multi_reg_write(struct regmap *map, |
| 206 | const struct reg_sequence *regs, |
| 207 | int num_regs) |
| 208 | { |
| 209 | int i, ret = 0; |
| 210 | |
| 211 | for (i = 0; i < num_regs; i++) { |
| 212 | ret = regmap_write(map, regs[i].reg, regs[i].def); |
| 213 | |
| 214 | if (regs[i].delay_us) |
| 215 | udelay(regs[i].delay_us); |
| 216 | } |
| 217 | |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | static int udphy_clk_init(struct rockchip_udphy *udphy, struct udevice *dev) |
| 222 | { |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int udphy_reset_init(struct rockchip_udphy *udphy, struct udevice *dev) |
| 227 | { |
| 228 | const struct rockchip_udphy_cfg *cfg = udphy->cfgs; |
| 229 | int idx; |
| 230 | int ret; |
| 231 | |
| 232 | udphy->rsts = devm_kcalloc(dev, cfg->num_rsts, |
| 233 | sizeof(*udphy->rsts), GFP_KERNEL); |
| 234 | if (!udphy->rsts) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | for (idx = 0; idx < cfg->num_rsts; idx++) { |
| 238 | const char *name = cfg->rst_list[idx]; |
| 239 | |
| 240 | ret = reset_get_by_name(dev, name, &udphy->rsts[idx]); |
| 241 | if (ret) { |
| 242 | dev_err(dev, "failed to get %s reset\n", name); |
| 243 | goto err; |
| 244 | } |
| 245 | |
| 246 | reset_assert(&udphy->rsts[idx]); |
| 247 | } |
| 248 | |
| 249 | return 0; |
| 250 | |
| 251 | err: |
| 252 | devm_kfree(dev, udphy->rsts); |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | static int udphy_get_rst_idx(const char * const *list, int num, char *name) |
| 257 | { |
| 258 | int idx; |
| 259 | |
| 260 | for (idx = 0; idx < num; idx++) { |
| 261 | if (!strcmp(list[idx], name)) |
| 262 | return idx; |
| 263 | } |
| 264 | |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
| 268 | static int udphy_reset_assert(struct rockchip_udphy *udphy, char *name) |
| 269 | { |
| 270 | const struct rockchip_udphy_cfg *cfg = udphy->cfgs; |
| 271 | int idx; |
| 272 | |
| 273 | idx = udphy_get_rst_idx(cfg->rst_list, cfg->num_rsts, name); |
| 274 | if (idx < 0) |
| 275 | return idx; |
| 276 | |
| 277 | return reset_assert(&udphy->rsts[idx]); |
| 278 | } |
| 279 | |
| 280 | static int udphy_reset_deassert(struct rockchip_udphy *udphy, char *name) |
| 281 | { |
| 282 | const struct rockchip_udphy_cfg *cfg = udphy->cfgs; |
| 283 | int idx; |
| 284 | |
| 285 | idx = udphy_get_rst_idx(cfg->rst_list, cfg->num_rsts, name); |
| 286 | if (idx < 0) |
| 287 | return idx; |
| 288 | |
| 289 | return reset_deassert(&udphy->rsts[idx]); |
| 290 | } |
| 291 | |
| 292 | static void udphy_u3_port_disable(struct rockchip_udphy *udphy, u8 disable) |
| 293 | { |
| 294 | const struct rockchip_udphy_cfg *cfg = udphy->cfgs; |
| 295 | const struct udphy_grf_reg *preg; |
| 296 | |
| 297 | preg = udphy->id ? &cfg->grfcfg.usb3otg1_cfg : &cfg->grfcfg.usb3otg0_cfg; |
| 298 | grfreg_write(udphy->usbgrf, preg, disable); |
| 299 | } |
| 300 | |
| 301 | __maybe_unused |
| 302 | static void udphy_usb_bvalid_enable(struct rockchip_udphy *udphy, u8 enable) |
| 303 | { |
| 304 | const struct rockchip_udphy_cfg *cfg = udphy->cfgs; |
| 305 | |
| 306 | grfreg_write(udphy->u2phygrf, &cfg->grfcfg.bvalid_phy_con, enable); |
| 307 | grfreg_write(udphy->u2phygrf, &cfg->grfcfg.bvalid_grf_con, enable); |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * In usb/dp combo phy driver, here are 2 ways to mapping lanes. |
| 312 | * |
| 313 | * 1 Type-C Mapping table (DP_Alt_Mode V1.0b remove ABF pin mapping) |
| 314 | * --------------------------------------------------------------------------- |
| 315 | * Type-C Pin B11-B10 A2-A3 A11-A10 B2-B3 |
| 316 | * PHY Pad ln0(tx/rx) ln1(tx) ln2(tx/rx) ln3(tx) |
| 317 | * C/E(Normal) dpln3 dpln2 dpln0 dpln1 |
| 318 | * C/E(Flip ) dpln0 dpln1 dpln3 dpln2 |
| 319 | * D/F(Normal) usbrx usbtx dpln0 dpln1 |
| 320 | * D/F(Flip ) dpln0 dpln1 usbrx usbtx |
| 321 | * A(Normal ) dpln3 dpln1 dpln2 dpln0 |
| 322 | * A(Flip ) dpln2 dpln0 dpln3 dpln1 |
| 323 | * B(Normal ) usbrx usbtx dpln1 dpln0 |
| 324 | * B(Flip ) dpln1 dpln0 usbrx usbtx |
| 325 | * --------------------------------------------------------------------------- |
| 326 | * |
| 327 | * 2 Mapping the lanes in dtsi |
| 328 | * if all 4 lane assignment for dp function, define rockchip,dp-lane-mux = <x x x x>; |
| 329 | * sample as follow: |
| 330 | * --------------------------------------------------------------------------- |
| 331 | * B11-B10 A2-A3 A11-A10 B2-B3 |
| 332 | * rockchip,dp-lane-mux ln0(tx/rx) ln1(tx) ln2(tx/rx) ln3(tx) |
| 333 | * <0 1 2 3> dpln0 dpln1 dpln2 dpln3 |
| 334 | * <2 3 0 1> dpln2 dpln3 dpln0 dpln1 |
| 335 | * --------------------------------------------------------------------------- |
| 336 | * if 2 lane for dp function, 2 lane for usb function, define rockchip,dp-lane-mux = <x x>; |
| 337 | * sample as follow: |
| 338 | * --------------------------------------------------------------------------- |
| 339 | * B11-B10 A2-A3 A11-A10 B2-B3 |
| 340 | * rockchip,dp-lane-mux ln0(tx/rx) ln1(tx) ln2(tx/rx) ln3(tx) |
| 341 | * <0 1> dpln0 dpln1 usbrx usbtx |
| 342 | * <2 3> usbrx usbtx dpln0 dpln1 |
| 343 | * --------------------------------------------------------------------------- |
| 344 | */ |
| 345 | |
| 346 | __maybe_unused |
| 347 | static int upphy_set_typec_default_mapping(struct rockchip_udphy *udphy) |
| 348 | { |
| 349 | if (udphy->flip) { |
| 350 | udphy->dp_lane_sel[0] = 0; |
| 351 | udphy->dp_lane_sel[1] = 1; |
| 352 | udphy->dp_lane_sel[2] = 3; |
| 353 | udphy->dp_lane_sel[3] = 2; |
| 354 | udphy->lane_mux_sel[0] = PHY_LANE_MUX_DP; |
| 355 | udphy->lane_mux_sel[1] = PHY_LANE_MUX_DP; |
| 356 | udphy->lane_mux_sel[2] = PHY_LANE_MUX_USB; |
| 357 | udphy->lane_mux_sel[3] = PHY_LANE_MUX_USB; |
| 358 | udphy->dp_aux_dout_sel = PHY_AUX_DP_DATA_POL_INVERT; |
| 359 | udphy->dp_aux_din_sel = PHY_AUX_DP_DATA_POL_INVERT; |
| 360 | } else { |
| 361 | udphy->dp_lane_sel[0] = 2; |
| 362 | udphy->dp_lane_sel[1] = 3; |
| 363 | udphy->dp_lane_sel[2] = 1; |
| 364 | udphy->dp_lane_sel[3] = 0; |
| 365 | udphy->lane_mux_sel[0] = PHY_LANE_MUX_USB; |
| 366 | udphy->lane_mux_sel[1] = PHY_LANE_MUX_USB; |
| 367 | udphy->lane_mux_sel[2] = PHY_LANE_MUX_DP; |
| 368 | udphy->lane_mux_sel[3] = PHY_LANE_MUX_DP; |
| 369 | udphy->dp_aux_dout_sel = PHY_AUX_DP_DATA_POL_NORMAL; |
| 370 | udphy->dp_aux_din_sel = PHY_AUX_DP_DATA_POL_NORMAL; |
| 371 | } |
| 372 | |
| 373 | udphy->mode = UDPHY_MODE_DP_USB; |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static int udphy_setup(struct rockchip_udphy *udphy) |
| 379 | { |
| 380 | const struct rockchip_udphy_cfg *cfg = udphy->cfgs; |
| 381 | int ret = 0; |
| 382 | |
| 383 | if (cfg->combophy_init) { |
| 384 | ret = cfg->combophy_init(udphy); |
| 385 | if (ret) |
| 386 | dev_err(udphy->dev, "failed to init usbdp combophy\n"); |
| 387 | } |
| 388 | |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | static int udphy_disable(struct rockchip_udphy *udphy) |
| 393 | { |
| 394 | const struct rockchip_udphy_cfg *cfg = udphy->cfgs; |
| 395 | int i; |
| 396 | |
| 397 | for (i = 0; i < cfg->num_rsts; i++) |
| 398 | reset_assert(&udphy->rsts[i]); |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static int udphy_parse_lane_mux_data(struct rockchip_udphy *udphy, |
| 404 | const struct device_node *np) |
| 405 | { |
| 406 | struct property *prop; |
| 407 | int ret, i, len, num_lanes; |
| 408 | |
| 409 | prop = of_find_property(np, "rockchip,dp-lane-mux", &len); |
| 410 | if (!prop) { |
| 411 | dev_dbg(udphy->dev, |
| 412 | "failed to find dp lane mux, following dp alt mode\n"); |
| 413 | udphy->mode = UDPHY_MODE_USB; |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | num_lanes = len / sizeof(u32); |
| 418 | |
| 419 | if (num_lanes != 2 && num_lanes != 4) { |
| 420 | dev_err(udphy->dev, "invalid number of lane mux\n"); |
| 421 | return -EINVAL; |
| 422 | } |
| 423 | |
| 424 | ret = of_read_u32_array(np, "rockchip,dp-lane-mux", udphy->dp_lane_sel, |
| 425 | num_lanes); |
| 426 | if (ret) { |
| 427 | dev_err(udphy->dev, "get dp lane mux failed\n"); |
| 428 | return -EINVAL; |
| 429 | } |
| 430 | |
| 431 | for (i = 0; i < num_lanes; i++) { |
| 432 | int j; |
| 433 | |
| 434 | if (udphy->dp_lane_sel[i] > 3) { |
| 435 | dev_err(udphy->dev, |
| 436 | "lane mux between 0 and 3, exceeding the range\n"); |
| 437 | return -EINVAL; |
| 438 | } |
| 439 | |
| 440 | udphy->lane_mux_sel[udphy->dp_lane_sel[i]] = PHY_LANE_MUX_DP; |
| 441 | |
| 442 | for (j = i + 1; j < num_lanes; j++) { |
| 443 | if (udphy->dp_lane_sel[i] == udphy->dp_lane_sel[j]) { |
| 444 | dev_err(udphy->dev, |
| 445 | "set repeat lane mux value\n"); |
| 446 | return -EINVAL; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | udphy->mode = UDPHY_MODE_DP; |
| 452 | if (num_lanes == 2) |
| 453 | udphy->mode |= UDPHY_MODE_USB; |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static int udphy_parse_dt(struct rockchip_udphy *udphy, struct udevice *dev) |
| 459 | { |
| 460 | const struct device_node *np = ofnode_to_np(dev_ofnode(dev)); |
| 461 | enum usb_device_speed maximum_speed; |
| 462 | int ret; |
| 463 | |
| 464 | udphy->u2phygrf = syscon_regmap_lookup_by_phandle(dev, |
| 465 | "rockchip,u2phy-grf"); |
| 466 | if (IS_ERR(udphy->u2phygrf)) { |
| 467 | if (PTR_ERR(udphy->u2phygrf) == -ENODEV) { |
| 468 | dev_warn(dev, "missing u2phy-grf dt node\n"); |
| 469 | udphy->u2phygrf = NULL; |
| 470 | } else { |
| 471 | return PTR_ERR(udphy->u2phygrf); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | udphy->udphygrf = syscon_regmap_lookup_by_phandle(dev, |
| 476 | "rockchip,usbdpphy-grf"); |
| 477 | if (IS_ERR(udphy->udphygrf)) { |
| 478 | if (PTR_ERR(udphy->udphygrf) == -ENODEV) { |
| 479 | dev_warn(dev, "missing usbdpphy-grf dt node\n"); |
| 480 | udphy->udphygrf = NULL; |
| 481 | } else { |
| 482 | return PTR_ERR(udphy->udphygrf); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | udphy->usbgrf = syscon_regmap_lookup_by_phandle(dev, |
| 487 | "rockchip,usb-grf"); |
| 488 | if (IS_ERR(udphy->usbgrf)) { |
| 489 | if (PTR_ERR(udphy->usbgrf) == -ENODEV) { |
| 490 | dev_warn(dev, "missing usb-grf dt node\n"); |
| 491 | udphy->usbgrf = NULL; |
| 492 | } else { |
| 493 | return PTR_ERR(udphy->usbgrf); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | udphy->vogrf = syscon_regmap_lookup_by_phandle(dev, "rockchip,vo-grf"); |
| 498 | if (IS_ERR(udphy->vogrf)) { |
| 499 | if (PTR_ERR(udphy->vogrf) == -ENODEV) { |
| 500 | dev_warn(dev, "missing vo-grf dt node\n"); |
| 501 | udphy->vogrf = NULL; |
| 502 | } else { |
| 503 | return PTR_ERR(udphy->vogrf); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | ret = udphy_parse_lane_mux_data(udphy, np); |
| 508 | if (ret) |
| 509 | return ret; |
| 510 | |
| 511 | if (dev_read_prop(dev, "maximum-speed", NULL)) { |
| 512 | maximum_speed = usb_get_maximum_speed(dev_ofnode(dev)); |
| 513 | udphy->hs = maximum_speed <= USB_SPEED_HIGH ? true : false; |
| 514 | } |
| 515 | |
| 516 | ret = udphy_clk_init(udphy, dev); |
| 517 | if (ret) |
| 518 | return ret; |
| 519 | |
| 520 | ret = udphy_reset_init(udphy, dev); |
| 521 | if (ret) |
| 522 | return ret; |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static int udphy_power_on(struct rockchip_udphy *udphy, u8 mode) |
| 528 | { |
| 529 | int ret; |
| 530 | |
| 531 | if (!(udphy->mode & mode)) { |
| 532 | dev_info(udphy->dev, "mode 0x%02x is not support\n", mode); |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | if (udphy->status == UDPHY_MODE_NONE) { |
| 537 | udphy->mode_change = false; |
| 538 | ret = udphy_setup(udphy); |
| 539 | if (ret) |
| 540 | return ret; |
| 541 | |
| 542 | if (udphy->mode & UDPHY_MODE_USB) |
| 543 | udphy_u3_port_disable(udphy, false); |
| 544 | } else if (udphy->mode_change) { |
| 545 | udphy->mode_change = false; |
| 546 | udphy->status = UDPHY_MODE_NONE; |
| 547 | if (udphy->mode == UDPHY_MODE_DP) |
| 548 | udphy_u3_port_disable(udphy, true); |
| 549 | |
| 550 | ret = udphy_disable(udphy); |
| 551 | if (ret) |
| 552 | return ret; |
| 553 | ret = udphy_setup(udphy); |
| 554 | if (ret) |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | udphy->status |= mode; |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int udphy_power_off(struct rockchip_udphy *udphy, u8 mode) |
| 564 | { |
| 565 | int ret; |
| 566 | |
| 567 | if (!(udphy->mode & mode)) { |
| 568 | dev_info(udphy->dev, "mode 0x%02x is not supported\n", mode); |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | if (!udphy->status) |
| 573 | return 0; |
| 574 | |
| 575 | udphy->status &= ~mode; |
| 576 | |
| 577 | if (udphy->status == UDPHY_MODE_NONE) { |
| 578 | ret = udphy_disable(udphy); |
| 579 | if (ret) |
| 580 | return ret; |
| 581 | } |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static int rockchip_u3phy_init(struct phy *phy) |
| 587 | { |
| 588 | struct udevice *parent = phy->dev->parent; |
| 589 | struct rockchip_udphy *udphy = dev_get_priv(parent); |
| 590 | |
| 591 | /* DP only or high-speed, disable U3 port */ |
| 592 | if (!(udphy->mode & UDPHY_MODE_USB) || udphy->hs) { |
| 593 | udphy_u3_port_disable(udphy, true); |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | return udphy_power_on(udphy, UDPHY_MODE_USB); |
| 598 | } |
| 599 | |
| 600 | static int rockchip_u3phy_exit(struct phy *phy) |
| 601 | { |
| 602 | struct udevice *parent = phy->dev->parent; |
| 603 | struct rockchip_udphy *udphy = dev_get_priv(parent); |
| 604 | |
| 605 | /* DP only or high-speed */ |
| 606 | if (!(udphy->mode & UDPHY_MODE_USB) || udphy->hs) |
| 607 | return 0; |
| 608 | |
| 609 | return udphy_power_off(udphy, UDPHY_MODE_USB); |
| 610 | } |
| 611 | |
| 612 | static const struct phy_ops rockchip_u3phy_ops = { |
| 613 | .init = rockchip_u3phy_init, |
| 614 | .exit = rockchip_u3phy_exit, |
| 615 | }; |
| 616 | |
| 617 | int rockchip_u3phy_uboot_init(void) |
| 618 | { |
| 619 | struct udevice *udev; |
| 620 | struct rockchip_udphy *udphy; |
| 621 | int ret; |
| 622 | |
| 623 | ret = uclass_get_device_by_driver(UCLASS_PHY, |
| 624 | DM_DRIVER_GET(rockchip_udphy_u3_port), |
| 625 | &udev); |
| 626 | if (ret) { |
| 627 | pr_err("%s: get u3-port failed: %d\n", __func__, ret); |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | /* DP only or high-speed, disable U3 port */ |
| 632 | udphy = dev_get_priv(udev->parent); |
| 633 | if (!(udphy->mode & UDPHY_MODE_USB) || udphy->hs) { |
| 634 | udphy_u3_port_disable(udphy, true); |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | return udphy_power_on(udphy, UDPHY_MODE_USB); |
| 639 | } |
| 640 | |
| 641 | static int rockchip_udphy_probe(struct udevice *dev) |
| 642 | { |
| 643 | const struct device_node *np = ofnode_to_np(dev_ofnode(dev)); |
| 644 | struct rockchip_udphy *udphy = dev_get_priv(dev); |
| 645 | const struct rockchip_udphy_cfg *phy_cfgs; |
| 646 | int id, ret; |
| 647 | |
| 648 | udphy->dev = dev; |
| 649 | |
| 650 | id = of_alias_get_id(np, "usbdp"); |
| 651 | if (id < 0) |
| 652 | id = 0; |
| 653 | udphy->id = id; |
| 654 | |
| 655 | phy_cfgs = (const struct rockchip_udphy_cfg *)dev_get_driver_data(dev); |
| 656 | if (!phy_cfgs) { |
| 657 | dev_err(dev, "unable to get phy_cfgs\n"); |
| 658 | return -EINVAL; |
| 659 | } |
| 660 | udphy->cfgs = phy_cfgs; |
| 661 | |
| 662 | ret = regmap_init_mem(dev_ofnode(dev), &udphy->pma_regmap); |
| 663 | if (ret) |
| 664 | return ret; |
| 665 | udphy->pma_regmap->ranges[0].start += UDPHY_PMA; |
| 666 | |
| 667 | ret = udphy_parse_dt(udphy, dev); |
| 668 | if (ret) |
| 669 | return ret; |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | static int rockchip_udphy_bind(struct udevice *parent) |
| 675 | { |
| 676 | struct udevice *child; |
| 677 | ofnode subnode; |
| 678 | const char *node_name; |
| 679 | int ret; |
| 680 | |
| 681 | dev_for_each_subnode(subnode, parent) { |
| 682 | if (!ofnode_valid(subnode)) { |
| 683 | printf("%s: no subnode for %s", __func__, parent->name); |
| 684 | return -ENXIO; |
| 685 | } |
| 686 | |
| 687 | node_name = ofnode_get_name(subnode); |
| 688 | debug("%s: subnode %s\n", __func__, node_name); |
| 689 | |
| 690 | /* if there is no match, continue */ |
| 691 | if (strcasecmp(node_name, "usb3-port")) |
| 692 | continue; |
| 693 | |
| 694 | /* node name is usb3-port */ |
| 695 | ret = device_bind_driver_to_node(parent, |
| 696 | "rockchip_udphy_u3_port", |
| 697 | node_name, subnode, &child); |
| 698 | if (ret) { |
| 699 | printf("%s: '%s' cannot bind its driver\n", |
| 700 | __func__, node_name); |
| 701 | return ret; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | static int rk3588_udphy_refclk_set(struct rockchip_udphy *udphy) |
| 709 | { |
| 710 | /* configure phy reference clock */ |
| 711 | return __regmap_multi_reg_write(udphy->pma_regmap, |
| 712 | rk3588_udphy_24m_refclk_cfg, |
| 713 | ARRAY_SIZE(rk3588_udphy_24m_refclk_cfg)); |
| 714 | } |
| 715 | |
| 716 | static int rk3588_udphy_status_check(struct rockchip_udphy *udphy) |
| 717 | { |
| 718 | unsigned int val; |
| 719 | int ret; |
| 720 | |
| 721 | if (!(udphy->mode & UDPHY_MODE_USB)) |
| 722 | return 0; |
| 723 | |
| 724 | /* LCPLL check */ |
| 725 | ret = regmap_read_poll_timeout(udphy->pma_regmap, |
| 726 | CMN_ANA_LCPLL_DONE_OFFSET, |
| 727 | val, (val & CMN_ANA_LCPLL_AFC_DONE) && |
| 728 | (val & CMN_ANA_LCPLL_LOCK_DONE), |
| 729 | 200, 100); |
| 730 | if (ret) { |
| 731 | dev_err(udphy->dev, "cmn ana lcpll lock timeout\n"); |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | if (!udphy->flip) { |
| 736 | ret = regmap_read_poll_timeout(udphy->pma_regmap, |
| 737 | TRSV_LN0_MON_RX_CDR_DONE_OFFSET, |
| 738 | val, |
| 739 | val & TRSV_LN0_MON_RX_CDR_LOCK_DONE, |
| 740 | 200, 100); |
| 741 | if (ret) |
| 742 | dev_err(udphy->dev, "trsv ln0 mon rx cdr lock timeout\n"); |
| 743 | } else { |
| 744 | ret = regmap_read_poll_timeout(udphy->pma_regmap, |
| 745 | TRSV_LN2_MON_RX_CDR_DONE_OFFSET, |
| 746 | val, |
| 747 | val & TRSV_LN2_MON_RX_CDR_LOCK_DONE, |
| 748 | 200, 100); |
| 749 | if (ret) |
| 750 | dev_err(udphy->dev, "trsv ln2 mon rx cdr lock timeout\n"); |
| 751 | } |
| 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | static int rk3588_udphy_init(struct rockchip_udphy *udphy) |
| 757 | { |
| 758 | const struct rockchip_udphy_cfg *cfg = udphy->cfgs; |
| 759 | int ret; |
| 760 | |
| 761 | /* enable rx lfps for usb */ |
| 762 | if (udphy->mode & UDPHY_MODE_USB) |
| 763 | grfreg_write(udphy->udphygrf, &cfg->grfcfg.rx_lfps, true); |
| 764 | |
| 765 | /* Step 1: power on pma and deassert apb rstn */ |
| 766 | grfreg_write(udphy->udphygrf, &cfg->grfcfg.low_pwrn, true); |
| 767 | |
| 768 | udphy_reset_deassert(udphy, "pma_apb"); |
| 769 | udphy_reset_deassert(udphy, "pcs_apb"); |
| 770 | |
| 771 | /* Step 2: set init sequence and phy refclk */ |
| 772 | ret = __regmap_multi_reg_write(udphy->pma_regmap, |
| 773 | rk3588_udphy_init_sequence, |
| 774 | ARRAY_SIZE(rk3588_udphy_init_sequence)); |
| 775 | if (ret) { |
| 776 | dev_err(udphy->dev, "init sequence set error %d\n", ret); |
| 777 | goto assert_apb; |
| 778 | } |
| 779 | |
| 780 | ret = rk3588_udphy_refclk_set(udphy); |
| 781 | if (ret) { |
| 782 | dev_err(udphy->dev, "refclk set error %d\n", ret); |
| 783 | goto assert_apb; |
| 784 | } |
| 785 | |
| 786 | /* Step 3: configure lane mux */ |
| 787 | regmap_update_bits(udphy->pma_regmap, CMN_LANE_MUX_AND_EN_OFFSET, |
| 788 | CMN_DP_LANE_MUX_ALL | CMN_DP_LANE_EN_ALL, |
| 789 | FIELD_PREP(CMN_DP_LANE_MUX_N(3), |
| 790 | udphy->lane_mux_sel[3]) | |
| 791 | FIELD_PREP(CMN_DP_LANE_MUX_N(2), |
| 792 | udphy->lane_mux_sel[2]) | |
| 793 | FIELD_PREP(CMN_DP_LANE_MUX_N(1), |
| 794 | udphy->lane_mux_sel[1]) | |
| 795 | FIELD_PREP(CMN_DP_LANE_MUX_N(0), |
| 796 | udphy->lane_mux_sel[0]) | |
| 797 | FIELD_PREP(CMN_DP_LANE_EN_ALL, 0)); |
| 798 | |
| 799 | /* Step 4: deassert init rstn and wait for 200ns from datasheet */ |
| 800 | if (udphy->mode & UDPHY_MODE_USB) |
| 801 | udphy_reset_deassert(udphy, "init"); |
| 802 | |
| 803 | if (udphy->mode & UDPHY_MODE_DP) { |
| 804 | regmap_update_bits(udphy->pma_regmap, CMN_DP_RSTN_OFFSET, |
| 805 | CMN_DP_INIT_RSTN, |
| 806 | FIELD_PREP(CMN_DP_INIT_RSTN, 0x1)); |
| 807 | } |
| 808 | |
| 809 | udelay(1); |
| 810 | |
| 811 | /* Step 5: deassert cmn/lane rstn */ |
| 812 | if (udphy->mode & UDPHY_MODE_USB) { |
| 813 | udphy_reset_deassert(udphy, "cmn"); |
| 814 | udphy_reset_deassert(udphy, "lane"); |
| 815 | } |
| 816 | |
| 817 | /* Step 6: wait for lock done of pll */ |
| 818 | ret = rk3588_udphy_status_check(udphy); |
| 819 | if (ret) |
| 820 | goto assert_phy; |
| 821 | |
| 822 | return 0; |
| 823 | |
| 824 | assert_phy: |
| 825 | udphy_reset_assert(udphy, "init"); |
| 826 | udphy_reset_assert(udphy, "cmn"); |
| 827 | udphy_reset_assert(udphy, "lane"); |
| 828 | |
| 829 | assert_apb: |
| 830 | udphy_reset_assert(udphy, "pma_apb"); |
| 831 | udphy_reset_assert(udphy, "pcs_apb"); |
| 832 | |
| 833 | return ret; |
| 834 | } |
| 835 | |
| 836 | static const char * const rk3588_udphy_rst_l[] = { |
| 837 | "init", "cmn", "lane", "pcs_apb", "pma_apb" |
| 838 | }; |
| 839 | |
| 840 | static const struct rockchip_udphy_cfg rk3588_udphy_cfgs = { |
| 841 | .num_rsts = ARRAY_SIZE(rk3588_udphy_rst_l), |
| 842 | .rst_list = rk3588_udphy_rst_l, |
| 843 | .grfcfg = { |
| 844 | /* u2phy-grf */ |
| 845 | .bvalid_phy_con = { 0x0008, 1, 0, 0x2, 0x3 }, |
| 846 | .bvalid_grf_con = { 0x0010, 3, 2, 0x2, 0x3 }, |
| 847 | |
| 848 | /* usb-grf */ |
| 849 | .usb3otg0_cfg = { 0x001c, 15, 0, 0x1100, 0x0188 }, |
| 850 | .usb3otg1_cfg = { 0x0034, 15, 0, 0x1100, 0x0188 }, |
| 851 | |
| 852 | /* usbdpphy-grf */ |
| 853 | .low_pwrn = { 0x0004, 13, 13, 0, 1 }, |
| 854 | .rx_lfps = { 0x0004, 14, 14, 0, 1 }, |
| 855 | }, |
| 856 | .combophy_init = rk3588_udphy_init, |
| 857 | }; |
| 858 | |
| 859 | static const struct udevice_id rockchip_udphy_dt_match[] = { |
| 860 | { |
| 861 | .compatible = "rockchip,rk3588-usbdp-phy", |
| 862 | .data = (ulong)&rk3588_udphy_cfgs |
| 863 | }, |
| 864 | { /* sentinel */ } |
| 865 | }; |
| 866 | |
| 867 | U_BOOT_DRIVER(rockchip_udphy_u3_port) = { |
| 868 | .name = "rockchip_udphy_u3_port", |
| 869 | .id = UCLASS_PHY, |
| 870 | .ops = &rockchip_u3phy_ops, |
| 871 | }; |
| 872 | |
| 873 | U_BOOT_DRIVER(rockchip_udphy) = { |
| 874 | .name = "rockchip_udphy", |
| 875 | .id = UCLASS_PHY, |
| 876 | .of_match = rockchip_udphy_dt_match, |
| 877 | .probe = rockchip_udphy_probe, |
| 878 | .bind = rockchip_udphy_bind, |
| 879 | .priv_auto = sizeof(struct rockchip_udphy), |
| 880 | }; |