1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * omap-usb2.c - USB PHY, talking to USB controller on TI SoCs. 4 * 5 * Copyright (C) 2012-2020 Texas Instruments Incorporated - http://www.ti.com 6 * Author: Kishon Vijay Abraham I <kishon@ti.com> 7 */ 8 9 #include <linux/module.h> 10 #include <linux/platform_device.h> 11 #include <linux/slab.h> 12 #include <linux/of.h> 13 #include <linux/io.h> 14 #include <linux/phy/omap_usb.h> 15 #include <linux/usb/phy_companion.h> 16 #include <linux/clk.h> 17 #include <linux/err.h> 18 #include <linux/pm_runtime.h> 19 #include <linux/delay.h> 20 #include <linux/phy/omap_control_phy.h> 21 #include <linux/phy/phy.h> 22 #include <linux/mfd/syscon.h> 23 #include <linux/regmap.h> 24 #include <linux/of_platform.h> 25 26 #define USB2PHY_ANA_CONFIG1 0x4c 27 #define USB2PHY_DISCON_BYP_LATCH BIT(31) 28 29 /* SoC Specific USB2_OTG register definitions */ 30 #define AM654_USB2_OTG_PD BIT(8) 31 #define AM654_USB2_VBUS_DET_EN BIT(5) 32 #define AM654_USB2_VBUSVALID_DET_EN BIT(4) 33 34 #define OMAP_DEV_PHY_PD BIT(0) 35 #define OMAP_USB2_PHY_PD BIT(28) 36 37 #define AM437X_USB2_PHY_PD BIT(0) 38 #define AM437X_USB2_OTG_PD BIT(1) 39 #define AM437X_USB2_OTGVDET_EN BIT(19) 40 #define AM437X_USB2_OTGSESSEND_EN BIT(20) 41 42 /* Driver Flags */ 43 #define OMAP_USB2_HAS_START_SRP BIT(0) 44 #define OMAP_USB2_HAS_SET_VBUS BIT(1) 45 #define OMAP_USB2_CALIBRATE_FALSE_DISCONNECT BIT(2) 46 47 struct omap_usb { 48 struct usb_phy phy; 49 struct phy_companion *comparator; 50 void __iomem *pll_ctrl_base; 51 void __iomem *phy_base; 52 struct device *dev; 53 struct device *control_dev; 54 struct clk *wkupclk; 55 struct clk *optclk; 56 u8 flags; 57 struct regmap *syscon_phy_power; /* ctrl. reg. acces */ 58 unsigned int power_reg; /* power reg. index within syscon */ 59 u32 mask; 60 u32 power_on; 61 u32 power_off; 62 }; 63 64 #define phy_to_omapusb(x) container_of((x), struct omap_usb, phy) 65 66 struct usb_phy_data { 67 const char *label; 68 u8 flags; 69 u32 mask; 70 u32 power_on; 71 u32 power_off; 72 }; 73 74 static inline u32 omap_usb_readl(void __iomem *addr, unsigned int offset) 75 { 76 return __raw_readl(addr + offset); 77 } 78 79 static inline void omap_usb_writel(void __iomem *addr, unsigned int offset, 80 u32 data) 81 { 82 __raw_writel(data, addr + offset); 83 } 84 85 /** 86 * omap_usb2_set_comparator - links the comparator present in the sytem with 87 * this phy 88 * @comparator - the companion phy(comparator) for this phy 89 * 90 * The phy companion driver should call this API passing the phy_companion 91 * filled with set_vbus and start_srp to be used by usb phy. 92 * 93 * For use by phy companion driver 94 */ 95 int omap_usb2_set_comparator(struct phy_companion *comparator) 96 { 97 struct omap_usb *phy; 98 struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2); 99 100 if (IS_ERR(x)) 101 return -ENODEV; 102 103 phy = phy_to_omapusb(x); 104 phy->comparator = comparator; 105 return 0; 106 } 107 EXPORT_SYMBOL_GPL(omap_usb2_set_comparator); 108 109 static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled) 110 { 111 struct omap_usb *phy = phy_to_omapusb(otg->usb_phy); 112 113 if (!phy->comparator) 114 return -ENODEV; 115 116 return phy->comparator->set_vbus(phy->comparator, enabled); 117 } 118 119 static int omap_usb_start_srp(struct usb_otg *otg) 120 { 121 struct omap_usb *phy = phy_to_omapusb(otg->usb_phy); 122 123 if (!phy->comparator) 124 return -ENODEV; 125 126 return phy->comparator->start_srp(phy->comparator); 127 } 128 129 static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host) 130 { 131 otg->host = host; 132 if (!host) 133 otg->state = OTG_STATE_UNDEFINED; 134 135 return 0; 136 } 137 138 static int omap_usb_set_peripheral(struct usb_otg *otg, 139 struct usb_gadget *gadget) 140 { 141 otg->gadget = gadget; 142 if (!gadget) 143 otg->state = OTG_STATE_UNDEFINED; 144 145 return 0; 146 } 147 148 static int omap_usb_phy_power(struct omap_usb *phy, int on) 149 { 150 u32 val; 151 int ret; 152 153 if (!phy->syscon_phy_power) { 154 omap_control_phy_power(phy->control_dev, on); 155 return 0; 156 } 157 158 if (on) 159 val = phy->power_on; 160 else 161 val = phy->power_off; 162 163 ret = regmap_update_bits(phy->syscon_phy_power, phy->power_reg, 164 phy->mask, val); 165 return ret; 166 } 167 168 static int omap_usb_power_off(struct phy *x) 169 { 170 struct omap_usb *phy = phy_get_drvdata(x); 171 172 return omap_usb_phy_power(phy, false); 173 } 174 175 static int omap_usb_power_on(struct phy *x) 176 { 177 struct omap_usb *phy = phy_get_drvdata(x); 178 179 return omap_usb_phy_power(phy, true); 180 } 181 182 static int omap_usb2_disable_clocks(struct omap_usb *phy) 183 { 184 clk_disable_unprepare(phy->wkupclk); 185 if (!IS_ERR(phy->optclk)) 186 clk_disable_unprepare(phy->optclk); 187 188 return 0; 189 } 190 191 static int omap_usb2_enable_clocks(struct omap_usb *phy) 192 { 193 int ret; 194 195 ret = clk_prepare_enable(phy->wkupclk); 196 if (ret < 0) { 197 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret); 198 goto err0; 199 } 200 201 if (!IS_ERR(phy->optclk)) { 202 ret = clk_prepare_enable(phy->optclk); 203 if (ret < 0) { 204 dev_err(phy->dev, "Failed to enable optclk %d\n", ret); 205 goto err1; 206 } 207 } 208 209 return 0; 210 211 err1: 212 clk_disable(phy->wkupclk); 213 214 err0: 215 return ret; 216 } 217 218 static int omap_usb_init(struct phy *x) 219 { 220 struct omap_usb *phy = phy_get_drvdata(x); 221 u32 val; 222 223 omap_usb2_enable_clocks(phy); 224 225 if (phy->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) { 226 /* 227 * 228 * Reduce the sensitivity of internal PHY by enabling the 229 * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This 230 * resolves issues with certain devices which can otherwise 231 * be prone to false disconnects. 232 * 233 */ 234 val = omap_usb_readl(phy->phy_base, USB2PHY_ANA_CONFIG1); 235 val |= USB2PHY_DISCON_BYP_LATCH; 236 omap_usb_writel(phy->phy_base, USB2PHY_ANA_CONFIG1, val); 237 } 238 239 return 0; 240 } 241 242 static int omap_usb_exit(struct phy *x) 243 { 244 struct omap_usb *phy = phy_get_drvdata(x); 245 246 return omap_usb2_disable_clocks(phy); 247 } 248 249 static const struct phy_ops ops = { 250 .init = omap_usb_init, 251 .exit = omap_usb_exit, 252 .power_on = omap_usb_power_on, 253 .power_off = omap_usb_power_off, 254 .owner = THIS_MODULE, 255 }; 256 257 static const struct usb_phy_data omap_usb2_data = { 258 .label = "omap_usb2", 259 .flags = OMAP_USB2_HAS_START_SRP | OMAP_USB2_HAS_SET_VBUS, 260 .mask = OMAP_DEV_PHY_PD, 261 .power_off = OMAP_DEV_PHY_PD, 262 }; 263 264 static const struct usb_phy_data omap5_usb2_data = { 265 .label = "omap5_usb2", 266 .flags = 0, 267 .mask = OMAP_DEV_PHY_PD, 268 .power_off = OMAP_DEV_PHY_PD, 269 }; 270 271 static const struct usb_phy_data dra7x_usb2_data = { 272 .label = "dra7x_usb2", 273 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT, 274 .mask = OMAP_DEV_PHY_PD, 275 .power_off = OMAP_DEV_PHY_PD, 276 }; 277 278 static const struct usb_phy_data dra7x_usb2_phy2_data = { 279 .label = "dra7x_usb2_phy2", 280 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT, 281 .mask = OMAP_USB2_PHY_PD, 282 .power_off = OMAP_USB2_PHY_PD, 283 }; 284 285 static const struct usb_phy_data am437x_usb2_data = { 286 .label = "am437x_usb2", 287 .flags = 0, 288 .mask = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD | 289 AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN, 290 .power_on = AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN, 291 .power_off = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD, 292 }; 293 294 static const struct usb_phy_data am654_usb2_data = { 295 .label = "am654_usb2", 296 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT, 297 .mask = AM654_USB2_OTG_PD | AM654_USB2_VBUS_DET_EN | 298 AM654_USB2_VBUSVALID_DET_EN, 299 .power_on = AM654_USB2_VBUS_DET_EN | AM654_USB2_VBUSVALID_DET_EN, 300 .power_off = AM654_USB2_OTG_PD, 301 }; 302 303 static const struct of_device_id omap_usb2_id_table[] = { 304 { 305 .compatible = "ti,omap-usb2", 306 .data = &omap_usb2_data, 307 }, 308 { 309 .compatible = "ti,omap5-usb2", 310 .data = &omap5_usb2_data, 311 }, 312 { 313 .compatible = "ti,dra7x-usb2", 314 .data = &dra7x_usb2_data, 315 }, 316 { 317 .compatible = "ti,dra7x-usb2-phy2", 318 .data = &dra7x_usb2_phy2_data, 319 }, 320 { 321 .compatible = "ti,am437x-usb2", 322 .data = &am437x_usb2_data, 323 }, 324 { 325 .compatible = "ti,am654-usb2", 326 .data = &am654_usb2_data, 327 }, 328 {}, 329 }; 330 MODULE_DEVICE_TABLE(of, omap_usb2_id_table); 331 332 static int omap_usb2_probe(struct platform_device *pdev) 333 { 334 struct omap_usb *phy; 335 struct phy *generic_phy; 336 struct resource *res; 337 struct phy_provider *phy_provider; 338 struct usb_otg *otg; 339 struct device_node *node = pdev->dev.of_node; 340 struct device_node *control_node; 341 struct platform_device *control_pdev; 342 const struct of_device_id *of_id; 343 struct usb_phy_data *phy_data; 344 345 of_id = of_match_device(omap_usb2_id_table, &pdev->dev); 346 347 if (!of_id) 348 return -EINVAL; 349 350 phy_data = (struct usb_phy_data *)of_id->data; 351 352 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL); 353 if (!phy) 354 return -ENOMEM; 355 356 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL); 357 if (!otg) 358 return -ENOMEM; 359 360 phy->dev = &pdev->dev; 361 362 phy->phy.dev = phy->dev; 363 phy->phy.label = phy_data->label; 364 phy->phy.otg = otg; 365 phy->phy.type = USB_PHY_TYPE_USB2; 366 phy->mask = phy_data->mask; 367 phy->power_on = phy_data->power_on; 368 phy->power_off = phy_data->power_off; 369 370 if (phy_data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) { 371 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 372 phy->phy_base = devm_ioremap_resource(&pdev->dev, res); 373 if (IS_ERR(phy->phy_base)) 374 return PTR_ERR(phy->phy_base); 375 phy->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT; 376 } 377 378 phy->syscon_phy_power = syscon_regmap_lookup_by_phandle(node, 379 "syscon-phy-power"); 380 if (IS_ERR(phy->syscon_phy_power)) { 381 dev_dbg(&pdev->dev, 382 "can't get syscon-phy-power, using control device\n"); 383 phy->syscon_phy_power = NULL; 384 385 control_node = of_parse_phandle(node, "ctrl-module", 0); 386 if (!control_node) { 387 dev_err(&pdev->dev, 388 "Failed to get control device phandle\n"); 389 return -EINVAL; 390 } 391 392 control_pdev = of_find_device_by_node(control_node); 393 if (!control_pdev) { 394 dev_err(&pdev->dev, "Failed to get control device\n"); 395 return -EINVAL; 396 } 397 phy->control_dev = &control_pdev->dev; 398 } else { 399 if (of_property_read_u32_index(node, 400 "syscon-phy-power", 1, 401 &phy->power_reg)) { 402 dev_err(&pdev->dev, 403 "couldn't get power reg. offset\n"); 404 return -EINVAL; 405 } 406 } 407 408 409 phy->wkupclk = devm_clk_get(phy->dev, "wkupclk"); 410 if (IS_ERR(phy->wkupclk)) { 411 if (PTR_ERR(phy->wkupclk) == -EPROBE_DEFER) 412 return -EPROBE_DEFER; 413 414 dev_warn(&pdev->dev, "unable to get wkupclk %ld, trying old name\n", 415 PTR_ERR(phy->wkupclk)); 416 phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k"); 417 418 if (IS_ERR(phy->wkupclk)) { 419 if (PTR_ERR(phy->wkupclk) != -EPROBE_DEFER) 420 dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n"); 421 return PTR_ERR(phy->wkupclk); 422 } else { 423 dev_warn(&pdev->dev, 424 "found usb_phy_cm_clk32k, please fix DTS\n"); 425 } 426 } 427 428 phy->optclk = devm_clk_get(phy->dev, "refclk"); 429 if (IS_ERR(phy->optclk)) { 430 if (PTR_ERR(phy->optclk) == -EPROBE_DEFER) 431 return -EPROBE_DEFER; 432 433 dev_dbg(&pdev->dev, "unable to get refclk, trying old name\n"); 434 phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m"); 435 436 if (IS_ERR(phy->optclk)) { 437 if (PTR_ERR(phy->optclk) != -EPROBE_DEFER) { 438 dev_dbg(&pdev->dev, 439 "unable to get usb_otg_ss_refclk960m\n"); 440 } 441 } else { 442 dev_warn(&pdev->dev, 443 "found usb_otg_ss_refclk960m, please fix DTS\n"); 444 } 445 } 446 447 otg->set_host = omap_usb_set_host; 448 otg->set_peripheral = omap_usb_set_peripheral; 449 if (phy_data->flags & OMAP_USB2_HAS_SET_VBUS) 450 otg->set_vbus = omap_usb_set_vbus; 451 if (phy_data->flags & OMAP_USB2_HAS_START_SRP) 452 otg->start_srp = omap_usb_start_srp; 453 otg->usb_phy = &phy->phy; 454 455 platform_set_drvdata(pdev, phy); 456 pm_runtime_enable(phy->dev); 457 458 generic_phy = devm_phy_create(phy->dev, NULL, &ops); 459 if (IS_ERR(generic_phy)) { 460 pm_runtime_disable(phy->dev); 461 return PTR_ERR(generic_phy); 462 } 463 464 phy_set_drvdata(generic_phy, phy); 465 omap_usb_power_off(generic_phy); 466 467 phy_provider = devm_of_phy_provider_register(phy->dev, 468 of_phy_simple_xlate); 469 if (IS_ERR(phy_provider)) { 470 pm_runtime_disable(phy->dev); 471 return PTR_ERR(phy_provider); 472 } 473 474 475 usb_add_phy_dev(&phy->phy); 476 477 return 0; 478 } 479 480 static int omap_usb2_remove(struct platform_device *pdev) 481 { 482 struct omap_usb *phy = platform_get_drvdata(pdev); 483 484 usb_remove_phy(&phy->phy); 485 pm_runtime_disable(phy->dev); 486 487 return 0; 488 } 489 490 static struct platform_driver omap_usb2_driver = { 491 .probe = omap_usb2_probe, 492 .remove = omap_usb2_remove, 493 .driver = { 494 .name = "omap-usb2", 495 .of_match_table = omap_usb2_id_table, 496 }, 497 }; 498 499 module_platform_driver(omap_usb2_driver); 500 501 MODULE_ALIAS("platform:omap_usb2"); 502 MODULE_AUTHOR("Texas Instruments Inc."); 503 MODULE_DESCRIPTION("OMAP USB2 phy driver"); 504 MODULE_LICENSE("GPL v2"); 505