1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Rockchip USB2.0 PHY with Naneng IP block driver 4 * 5 * Copyright (C) 2020 Fuzhou Rockchip Electronics Co., Ltd 6 */ 7 8 #include <common.h> 9 #include <dm.h> 10 #include <dm/lists.h> 11 #include <generic-phy.h> 12 #include <syscon.h> 13 #include <asm/io.h> 14 #include <asm/arch/clock.h> 15 #include <reset-uclass.h> 16 17 #define U2PHY_BIT_WRITEABLE_SHIFT 16 18 19 struct rockchip_usb2phy; 20 21 enum power_supply_type { 22 POWER_SUPPLY_TYPE_UNKNOWN = 0, 23 POWER_SUPPLY_TYPE_USB, /* Standard Downstream Port */ 24 POWER_SUPPLY_TYPE_USB_DCP, /* Dedicated Charging Port */ 25 POWER_SUPPLY_TYPE_USB_CDP, /* Charging Downstream Port */ 26 POWER_SUPPLY_TYPE_USB_FLOATING, /* DCP without shorting D+/D- */ 27 }; 28 29 enum rockchip_usb2phy_port_id { 30 USB2PHY_PORT_OTG, 31 USB2PHY_PORT_HOST, 32 USB2PHY_NUM_PORTS, 33 }; 34 35 struct usb2phy_reg { 36 u32 offset; 37 u32 bitend; 38 u32 bitstart; 39 u32 disable; 40 u32 enable; 41 }; 42 43 /** 44 * struct rockchip_chg_det_reg: usb charger detect registers 45 * @chg_valid: charge valid signal. 46 * @phy_connect: PHY start handshake signal. 47 * @chg_en: charge detector enable signal. 48 * @chg_rst: charge detector reset signal, active high. 49 */ 50 struct rockchip_chg_det_reg { 51 struct usb2phy_reg chg_valid; 52 struct usb2phy_reg phy_connect; 53 struct usb2phy_reg chg_en; 54 struct usb2phy_reg chg_rst; 55 }; 56 57 /** 58 * struct rockchip_usb2phy_port_cfg: usb phy port configuration. 59 * @bypass_otgsuspendm: otg-suspendm bypass control register. 60 * 0: iddig; 1: grf. 61 * @bvalidfall_det_en: vbus valid fall detection enable register. 62 * @bvalidfall_det_st: vbus valid fall detection status register. 63 * @bvalidfall_det_clr: vbus valid fall detection clear register. 64 * @bvalidrise_det_en: vbus valid rise detection enable register. 65 * @bvalidrise_det_st: vbus valid rise detection status register. 66 * @bvalidrise_det_clr: vbus valid rise detection clear register. 67 * @disconfall_det_en: host connect detection enable register. 68 * @disconfall_det_st: host connect detection status register. 69 * @disconfall_det_clr: host connect detection clear register. 70 * @disconrise_det_en: host disconnect detection enable register. 71 * @disconrise_det_st: host disconnect detection status register. 72 * @disconrise_det_clr: host disconnect detection clear register. 73 * @idfall_det_en: id fall detection enable register. 74 * @idfall_det_st: id fall detection state register. 75 * @idfall_det_clr: id fall detection clear register. 76 * @idpullup: id pin pullup or pulldown control register. 77 * @idrise_det_en: id rise detection enable register. 78 * @idrise_det_st: id rise detection state register. 79 * @idrise_det_clr: id rise detection clear register. 80 * @ls_det_en: linestate detection enable register. 81 * @ls_det_st: linestate detection state register. 82 * @ls_det_clr: linestate detection clear register. 83 * @phy_sus: phy suspend register. 84 * @utmi_bvalid: utmi vbus bvalid status register. 85 * @utmi_iddig: otg port id pin status register. 86 * @utmi_hostdet: utmi host disconnect status register. 87 */ 88 struct rockchip_usb2phy_port_cfg { 89 struct usb2phy_reg bypass_otgsuspendm; 90 struct usb2phy_reg bvalidfall_det_en; 91 struct usb2phy_reg bvalidfall_det_st; 92 struct usb2phy_reg bvalidfall_det_clr; 93 struct usb2phy_reg bvalidrise_det_en; 94 struct usb2phy_reg bvalidrise_det_st; 95 struct usb2phy_reg bvalidrise_det_clr; 96 struct usb2phy_reg disconfall_det_en; 97 struct usb2phy_reg disconfall_det_st; 98 struct usb2phy_reg disconfall_det_clr; 99 struct usb2phy_reg disconrise_det_en; 100 struct usb2phy_reg disconrise_det_st; 101 struct usb2phy_reg disconrise_det_clr; 102 struct usb2phy_reg idfall_det_en; 103 struct usb2phy_reg idfall_det_st; 104 struct usb2phy_reg idfall_det_clr; 105 struct usb2phy_reg idpullup; 106 struct usb2phy_reg idrise_det_en; 107 struct usb2phy_reg idrise_det_st; 108 struct usb2phy_reg idrise_det_clr; 109 struct usb2phy_reg ls_det_en; 110 struct usb2phy_reg ls_det_st; 111 struct usb2phy_reg ls_det_clr; 112 struct usb2phy_reg phy_sus; 113 struct usb2phy_reg utmi_bvalid; 114 struct usb2phy_reg utmi_iddig; 115 struct usb2phy_reg utmi_hostdet; 116 }; 117 118 /** 119 * struct rockchip_usb2phy_cfg: usb phy configuration. 120 * @reg: the address offset of grf for usb-phy config. 121 * @num_ports: specify how many ports that the phy has. 122 * @phy_tuning: phy default parameters tuning. 123 * @clkout_ctl: keep on/turn off output clk of phy. 124 * @port_cfgs: ports register configuration, assigned by driver data. 125 * @chg_det: charger detection registers. 126 * @last: indicate the last one. 127 */ 128 struct rockchip_usb2phy_cfg { 129 unsigned int reg; 130 unsigned int num_ports; 131 int (*phy_tuning)(struct rockchip_usb2phy *rphy); 132 struct usb2phy_reg clkout_ctl; 133 const struct rockchip_usb2phy_port_cfg port_cfgs[USB2PHY_NUM_PORTS]; 134 const struct rockchip_chg_det_reg chg_det; 135 bool last; 136 }; 137 138 /** 139 * struct rockchip_usb2phy: usb2.0 phy driver data. 140 * @grf: General Register Files register base. 141 * @reset: power reset signal for phy. 142 * @phy_cfg: phy register configuration, assigned by driver data. 143 */ 144 struct rockchip_usb2phy { 145 void __iomem *grf; 146 struct reset_ctl *reset; 147 const struct rockchip_usb2phy_cfg *phy_cfg; 148 }; 149 150 static inline int property_enable(void __iomem *base, 151 const struct usb2phy_reg *reg, bool en) 152 { 153 u32 val, mask, tmp; 154 155 tmp = en ? reg->enable : reg->disable; 156 mask = GENMASK(reg->bitend, reg->bitstart); 157 val = (tmp << reg->bitstart) | (mask << U2PHY_BIT_WRITEABLE_SHIFT); 158 159 return writel(val, base + reg->offset); 160 } 161 162 static inline bool property_enabled(void __iomem *base, 163 const struct usb2phy_reg *reg) 164 { 165 u32 tmp, orig; 166 u32 mask = GENMASK(reg->bitend, reg->bitstart); 167 168 orig = readl(base + reg->offset); 169 170 tmp = (orig & mask) >> reg->bitstart; 171 172 return tmp == reg->enable; 173 } 174 175 static const char *chg_to_string(enum power_supply_type chg_type) 176 { 177 switch (chg_type) { 178 case POWER_SUPPLY_TYPE_USB: 179 return "USB_SDP_CHARGER"; 180 case POWER_SUPPLY_TYPE_USB_DCP: 181 return "USB_DCP_CHARGER"; 182 case POWER_SUPPLY_TYPE_USB_CDP: 183 return "USB_CDP_CHARGER"; 184 case POWER_SUPPLY_TYPE_USB_FLOATING: 185 return "USB_FLOATING_CHARGER"; 186 default: 187 return "INVALID_CHARGER"; 188 } 189 } 190 191 int rockchip_chg_get_type(void) 192 { 193 const struct rockchip_usb2phy_port_cfg *port_cfg; 194 enum power_supply_type chg_type; 195 struct rockchip_usb2phy *rphy; 196 struct udevice *udev; 197 bool chg_valid, phy_connect; 198 int cnt; 199 int ret; 200 201 ret = uclass_get_device_by_name(UCLASS_PHY, "usb2-phy", &udev); 202 if (ret == -ENODEV) { 203 pr_err("%s: get u2phy node failed: %d\n", __func__, ret); 204 return ret; 205 } 206 207 rphy = dev_get_priv(udev); 208 port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_OTG]; 209 210 /* Check USB-Vbus status first */ 211 if (!property_enabled(rphy->grf, &port_cfg->utmi_bvalid)) { 212 pr_info("%s: no charger found\n", __func__); 213 return POWER_SUPPLY_TYPE_UNKNOWN; 214 } 215 216 reset_assert(rphy->reset); 217 218 /* CHG_RST is set to 1'b0 to start charge detection */ 219 property_enable(rphy->grf, &rphy->phy_cfg->chg_det.chg_en, true); 220 property_enable(rphy->grf, &rphy->phy_cfg->chg_det.chg_rst, false); 221 222 for (cnt = 0; cnt < 12; cnt++) { 223 mdelay(100); 224 225 chg_valid = property_enabled(rphy->grf, 226 &rphy->phy_cfg->chg_det.chg_valid); 227 phy_connect = 228 property_enabled(rphy->grf, 229 &rphy->phy_cfg->chg_det.phy_connect); 230 chg_type = (chg_valid << 1) | phy_connect; 231 232 if (chg_type) 233 goto compeleted; 234 } 235 236 compeleted: 237 debug("charger = %s\n", chg_to_string(chg_type)); 238 239 mdelay(1); 240 reset_deassert(rphy->reset); 241 /* disable the chg detection module */ 242 property_enable(rphy->grf, &rphy->phy_cfg->chg_det.chg_rst, true); 243 property_enable(rphy->grf, &rphy->phy_cfg->chg_det.chg_en, false); 244 245 return chg_type; 246 } 247 248 int rockchip_u2phy_vbus_detect(void) 249 { 250 int chg_type; 251 252 chg_type = rockchip_chg_get_type(); 253 254 return (chg_type == POWER_SUPPLY_TYPE_USB || 255 chg_type == POWER_SUPPLY_TYPE_USB_CDP) ? 1 : 0; 256 } 257 258 static int rockchip_usb2phy_init(struct phy *phy) 259 { 260 struct udevice *parent = phy->dev->parent; 261 struct rockchip_usb2phy *rphy = dev_get_priv(parent); 262 const struct rockchip_usb2phy_port_cfg *port_cfg; 263 264 if (phy->id == USB2PHY_PORT_OTG) { 265 port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_OTG]; 266 } else if (phy->id == USB2PHY_PORT_HOST) { 267 port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_HOST]; 268 } else { 269 dev_err(phy->dev, "phy id %lu not support", phy->id); 270 return -EINVAL; 271 } 272 273 property_enable(rphy->grf, &port_cfg->phy_sus, false); 274 275 /* waiting for the utmi_clk to become stable */ 276 udelay(2000); 277 278 return 0; 279 } 280 281 static int rockchip_usb2phy_exit(struct phy *phy) 282 { 283 struct udevice *parent = phy->dev->parent; 284 struct rockchip_usb2phy *rphy = dev_get_priv(parent); 285 const struct rockchip_usb2phy_port_cfg *port_cfg; 286 287 if (phy->id == USB2PHY_PORT_OTG) { 288 port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_OTG]; 289 } else if (phy->id == USB2PHY_PORT_HOST) { 290 port_cfg = &rphy->phy_cfg->port_cfgs[USB2PHY_PORT_HOST]; 291 } else { 292 dev_err(phy->dev, "phy id %lu not support", phy->id); 293 return -EINVAL; 294 } 295 296 property_enable(rphy->grf, &port_cfg->phy_sus, true); 297 298 return 0; 299 } 300 301 static int rockchip_usb2phy_of_xlate(struct phy *phy, 302 struct ofnode_phandle_args *args) 303 { 304 const char *dev_name = phy->dev->name; 305 306 if (!strcasecmp(dev_name, "host-port")) { 307 phy->id = USB2PHY_PORT_HOST; 308 } else if (!strcasecmp(dev_name, "otg-port")) { 309 phy->id = USB2PHY_PORT_OTG; 310 } else { 311 pr_err("%s: invalid dev name\n", __func__); 312 return -EINVAL; 313 } 314 315 return 0; 316 } 317 318 static int rockchip_usb2phy_bind(struct udevice *dev) 319 { 320 struct udevice *child; 321 ofnode subnode; 322 const char *node_name; 323 int ret; 324 325 dev_for_each_subnode(subnode, dev) { 326 if (!ofnode_valid(subnode)) { 327 debug("%s: %s subnode not found", __func__, dev->name); 328 return -ENXIO; 329 } 330 331 node_name = ofnode_get_name(subnode); 332 debug("%s: subnode %s\n", __func__, node_name); 333 334 ret = device_bind_driver_to_node(dev, "rockchip_usb2phy_port", 335 node_name, subnode, &child); 336 if (ret) { 337 pr_err("%s: '%s' cannot bind 'rockchip_usb2phy_port'\n", 338 __func__, node_name); 339 return ret; 340 } 341 } 342 343 return 0; 344 } 345 346 static int rockchip_usb2phy_probe(struct udevice *dev) 347 { 348 const struct rockchip_usb2phy_cfg *phy_cfgs; 349 struct rockchip_usb2phy *rphy = dev_get_priv(dev); 350 u32 reg, index; 351 352 rphy->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF); 353 354 /* get phy power reset control */ 355 if (reset_get_by_name(dev, "u2phy", rphy->reset)) { 356 pr_err("can't get phy power reset for %s", dev->name); 357 return -EINVAL; 358 } 359 360 if (rphy->grf <= 0) { 361 dev_err(dev, "get syscon grf failed\n"); 362 return -EINVAL; 363 } 364 365 if (ofnode_read_u32(dev_ofnode(dev), "reg", ®)) { 366 dev_err(dev, "could not read reg\n"); 367 return -EINVAL; 368 } 369 370 phy_cfgs = 371 (const struct rockchip_usb2phy_cfg *)dev_get_driver_data(dev); 372 if (!phy_cfgs) { 373 dev_err(dev, "unable to get phy_cfgs\n"); 374 return -EINVAL; 375 } 376 377 /* find out a proper config which can be matched with dt. */ 378 index = 0; 379 do { 380 if (phy_cfgs[index].reg == reg) { 381 rphy->phy_cfg = &phy_cfgs[index]; 382 break; 383 } 384 } while (!phy_cfgs[index++].last); 385 386 if (!rphy->phy_cfg) { 387 dev_err(dev, "no phy-config can be matched\n"); 388 return -EINVAL; 389 } 390 391 if (rphy->phy_cfg->phy_tuning) 392 rphy->phy_cfg->phy_tuning(rphy); 393 394 return 0; 395 } 396 397 static int rv1126_usb2phy_tuning(struct rockchip_usb2phy *rphy) 398 { 399 return 0; 400 } 401 402 static struct phy_ops rockchip_usb2phy_ops = { 403 .init = rockchip_usb2phy_init, 404 .exit = rockchip_usb2phy_exit, 405 .of_xlate = rockchip_usb2phy_of_xlate, 406 }; 407 408 static const struct rockchip_usb2phy_cfg rv1126_phy_cfgs[] = { 409 { 410 .reg = 0xff4c0000, 411 .num_ports = 1, 412 .phy_tuning = rv1126_usb2phy_tuning, 413 .clkout_ctl = { 0x10230, 14, 14, 0, 1 }, 414 .port_cfgs = { 415 [USB2PHY_PORT_OTG] = { 416 .bypass_otgsuspendm = { 0x10234, 12, 12, 0, 1 }, 417 .bvalidfall_det_en = { 0x10300, 3, 3, 0, 1 }, 418 .bvalidfall_det_st = { 0x10304, 3, 3, 0, 1 }, 419 .bvalidfall_det_clr = { 0x10308, 3, 3, 0, 1 }, 420 .bvalidrise_det_en = { 0x10300, 2, 2, 0, 1 }, 421 .bvalidrise_det_st = { 0x10304, 2, 2, 0, 1 }, 422 .bvalidrise_det_clr = { 0x10308, 2, 2, 0, 1 }, 423 .disconfall_det_en = { 0x10300, 7, 7, 0, 1 }, 424 .disconfall_det_st = { 0x10304, 7, 7, 0, 1 }, 425 .disconfall_det_clr = { 0x10308, 7, 7, 0, 1 }, 426 .disconrise_det_en = { 0x10300, 6, 6, 0, 1 }, 427 .disconrise_det_st = { 0x10304, 6, 6, 0, 1 }, 428 .disconrise_det_clr = { 0x10308, 6, 6, 0, 1 }, 429 .idfall_det_en = { 0x10300, 5, 5, 0, 1 }, 430 .idfall_det_st = { 0x10304, 5, 5, 0, 1 }, 431 .idfall_det_clr = { 0x10308, 5, 5, 0, 1 }, 432 .idpullup = { 0x10230, 11, 11, 0, 1 }, 433 .idrise_det_en = { 0x10300, 4, 4, 0, 1 }, 434 .idrise_det_st = { 0x10304, 4, 4, 0, 1 }, 435 .idrise_det_clr = { 0x10308, 4, 4, 0, 1 }, 436 .ls_det_en = { 0x10300, 0, 0, 0, 1 }, 437 .ls_det_st = { 0x10304, 0, 0, 0, 1 }, 438 .ls_det_clr = { 0x10308, 0, 0, 0, 1 }, 439 .phy_sus = { 0x10230, 8, 0, 0x052, 0x1d9 }, 440 .utmi_bvalid = { 0x10248, 9, 9, 0, 1 }, 441 .utmi_iddig = { 0x10248, 6, 6, 0, 1 }, 442 .utmi_hostdet = { 0x10248, 7, 7, 0, 1 }, 443 } 444 }, 445 .chg_det = { 446 .chg_en = { 0x10234, 14, 14, 0, 1 }, 447 .chg_rst = { 0x10234, 15, 15, 0, 1 }, 448 .chg_valid = { 0x10248, 12, 12, 0, 1 }, 449 .phy_connect = { 0x10248, 13, 13, 0, 1 }, 450 }, 451 }, 452 { 453 .reg = 0xff4c8000, 454 .num_ports = 1, 455 .phy_tuning = rv1126_usb2phy_tuning, 456 .clkout_ctl = { 0x10238, 9, 9, 0, 1 }, 457 .port_cfgs = { 458 [USB2PHY_PORT_HOST] = { 459 .disconfall_det_en = { 0x10300, 9, 9, 0, 1 }, 460 .disconfall_det_st = { 0x10304, 9, 9, 0, 1 }, 461 .disconfall_det_clr = { 0x10308, 9, 9, 0, 1 }, 462 .disconrise_det_en = { 0x10300, 8, 8, 0, 1 }, 463 .disconrise_det_st = { 0x10304, 8, 8, 0, 1 }, 464 .disconrise_det_clr = { 0x10308, 8, 8, 0, 1 }, 465 .ls_det_en = { 0x10300, 1, 1, 0, 1 }, 466 .ls_det_st = { 0x10304, 1, 1, 0, 1 }, 467 .ls_det_clr = { 0x10308, 1, 1, 0, 1 }, 468 .phy_sus = { 0x10238, 3, 0, 0x2, 0x9 }, 469 .utmi_hostdet = { 0x10248, 23, 23, 0, 1 }, 470 } 471 }, 472 .chg_det = { 473 .chg_en = { 0x10238, 7, 7, 0, 1 }, 474 .chg_rst = { 0x10238, 8, 8, 0, 1 }, 475 .chg_valid = { 0x10248, 28, 28, 0, 1 }, 476 .phy_connect = { 0x10248, 29, 29, 0, 1 }, 477 }, 478 .last = true, 479 }, 480 }; 481 482 static const struct udevice_id rockchip_usb2phy_ids[] = { 483 { .compatible = "rockchip,rv1126-usb2phy", .data = (ulong)&rv1126_phy_cfgs }, 484 { } 485 }; 486 487 U_BOOT_DRIVER(rockchip_usb2phy_port) = { 488 .name = "rockchip_usb2phy_port", 489 .id = UCLASS_PHY, 490 .ops = &rockchip_usb2phy_ops, 491 }; 492 493 U_BOOT_DRIVER(rockchip_usb2phy) = { 494 .name = "rockchip_usb2phy", 495 .id = UCLASS_PHY, 496 .of_match = rockchip_usb2phy_ids, 497 .probe = rockchip_usb2phy_probe, 498 .bind = rockchip_usb2phy_bind, 499 .priv_auto_alloc_size = sizeof(struct rockchip_usb2phy), 500 }; 501