Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

phy: renesas: rcar-gen3-usb2: enable/disable independent irqs

Since the previous code enabled/disabled the irqs both OHCI and EHCI,
it is possible to cause unexpected interruptions. To avoid this,
this patch creates multiple phy instances from phandle and
enables/disables independent irqs by the instances.

Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
Reviewed-by: Simon Horman <horms+renesas@verge.net.au>
Reviewed-by: Fabrizio Castro <fabrizio.castro@bp.renesas.com>
Tested-by: Fabrizio Castro <fabrizio.castro@bp.renesas.com>
Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>

authored by

Yoshihiro Shimoda and committed by
Kishon Vijay Abraham I
549b6b55 92fec1c2

+160 -25
+160 -25
drivers/phy/renesas/phy-rcar-gen3-usb2.c
··· 37 37 38 38 /* INT_ENABLE */ 39 39 #define USB2_INT_ENABLE_UCOM_INTEN BIT(3) 40 - #define USB2_INT_ENABLE_USBH_INTB_EN BIT(2) 41 - #define USB2_INT_ENABLE_USBH_INTA_EN BIT(1) 42 - #define USB2_INT_ENABLE_INIT (USB2_INT_ENABLE_UCOM_INTEN | \ 43 - USB2_INT_ENABLE_USBH_INTB_EN | \ 44 - USB2_INT_ENABLE_USBH_INTA_EN) 40 + #define USB2_INT_ENABLE_USBH_INTB_EN BIT(2) /* For EHCI */ 41 + #define USB2_INT_ENABLE_USBH_INTA_EN BIT(1) /* For OHCI */ 45 42 46 43 /* USBCTR */ 47 44 #define USB2_USBCTR_DIRPD BIT(2) ··· 75 78 #define USB2_ADPCTRL_IDPULLUP BIT(5) /* 1 = ID sampling is enabled */ 76 79 #define USB2_ADPCTRL_DRVVBUS BIT(4) 77 80 81 + #define NUM_OF_PHYS 4 82 + enum rcar_gen3_phy_index { 83 + PHY_INDEX_BOTH_HC, 84 + PHY_INDEX_OHCI, 85 + PHY_INDEX_EHCI, 86 + PHY_INDEX_HSUSB 87 + }; 88 + 89 + static const u32 rcar_gen3_int_enable[NUM_OF_PHYS] = { 90 + USB2_INT_ENABLE_USBH_INTB_EN | USB2_INT_ENABLE_USBH_INTA_EN, 91 + USB2_INT_ENABLE_USBH_INTA_EN, 92 + USB2_INT_ENABLE_USBH_INTB_EN, 93 + 0 94 + }; 95 + 96 + struct rcar_gen3_phy { 97 + struct phy *phy; 98 + struct rcar_gen3_chan *ch; 99 + u32 int_enable_bits; 100 + bool initialized; 101 + bool otg_initialized; 102 + bool powered; 103 + }; 104 + 78 105 struct rcar_gen3_chan { 79 106 void __iomem *base; 80 107 struct device *dev; /* platform_device's device */ 81 108 struct extcon_dev *extcon; 82 - struct phy *phy; 109 + struct rcar_gen3_phy rphys[NUM_OF_PHYS]; 83 110 struct regulator *vbus; 84 111 struct work_struct work; 85 112 enum usb_dr_mode dr_mode; ··· 271 250 return PHY_MODE_USB_DEVICE; 272 251 } 273 252 253 + static bool rcar_gen3_is_any_rphy_initialized(struct rcar_gen3_chan *ch) 254 + { 255 + int i; 256 + 257 + for (i = 0; i < NUM_OF_PHYS; i++) { 258 + if (ch->rphys[i].initialized) 259 + return true; 260 + } 261 + 262 + return false; 263 + } 264 + 265 + static bool rcar_gen3_needs_init_otg(struct rcar_gen3_chan *ch) 266 + { 267 + int i; 268 + 269 + for (i = 0; i < NUM_OF_PHYS; i++) { 270 + if (ch->rphys[i].otg_initialized) 271 + return false; 272 + } 273 + 274 + return true; 275 + } 276 + 277 + static bool rcar_gen3_are_all_rphys_power_off(struct rcar_gen3_chan *ch) 278 + { 279 + int i; 280 + 281 + for (i = 0; i < NUM_OF_PHYS; i++) { 282 + if (ch->rphys[i].powered) 283 + return false; 284 + } 285 + 286 + return true; 287 + } 288 + 274 289 static ssize_t role_store(struct device *dev, struct device_attribute *attr, 275 290 const char *buf, size_t count) 276 291 { ··· 314 257 bool is_b_device; 315 258 enum phy_mode cur_mode, new_mode; 316 259 317 - if (!ch->is_otg_channel || !ch->phy->init_count) 260 + if (!ch->is_otg_channel || !rcar_gen3_is_any_rphy_initialized(ch)) 318 261 return -EIO; 319 262 320 263 if (!strncmp(buf, "host", strlen("host"))) ··· 352 295 { 353 296 struct rcar_gen3_chan *ch = dev_get_drvdata(dev); 354 297 355 - if (!ch->is_otg_channel || !ch->phy->init_count) 298 + if (!ch->is_otg_channel || !rcar_gen3_is_any_rphy_initialized(ch)) 356 299 return -EIO; 357 300 358 301 return sprintf(buf, "%s\n", rcar_gen3_is_host(ch) ? "host" : ··· 386 329 387 330 static int rcar_gen3_phy_usb2_init(struct phy *p) 388 331 { 389 - struct rcar_gen3_chan *channel = phy_get_drvdata(p); 332 + struct rcar_gen3_phy *rphy = phy_get_drvdata(p); 333 + struct rcar_gen3_chan *channel = rphy->ch; 390 334 void __iomem *usb2_base = channel->base; 335 + u32 val; 391 336 392 337 /* Initialize USB2 part */ 393 - writel(USB2_INT_ENABLE_INIT, usb2_base + USB2_INT_ENABLE); 338 + val = readl(usb2_base + USB2_INT_ENABLE); 339 + val |= USB2_INT_ENABLE_UCOM_INTEN | rphy->int_enable_bits; 340 + writel(val, usb2_base + USB2_INT_ENABLE); 394 341 writel(USB2_SPD_RSM_TIMSET_INIT, usb2_base + USB2_SPD_RSM_TIMSET); 395 342 writel(USB2_OC_TIMSET_INIT, usb2_base + USB2_OC_TIMSET); 396 343 397 344 /* Initialize otg part */ 398 - if (channel->is_otg_channel) 399 - rcar_gen3_init_otg(channel); 345 + if (channel->is_otg_channel) { 346 + if (rcar_gen3_needs_init_otg(channel)) 347 + rcar_gen3_init_otg(channel); 348 + rphy->otg_initialized = true; 349 + } 350 + 351 + rphy->initialized = true; 400 352 401 353 return 0; 402 354 } 403 355 404 356 static int rcar_gen3_phy_usb2_exit(struct phy *p) 405 357 { 406 - struct rcar_gen3_chan *channel = phy_get_drvdata(p); 358 + struct rcar_gen3_phy *rphy = phy_get_drvdata(p); 359 + struct rcar_gen3_chan *channel = rphy->ch; 360 + void __iomem *usb2_base = channel->base; 361 + u32 val; 407 362 408 - writel(0, channel->base + USB2_INT_ENABLE); 363 + rphy->initialized = false; 364 + 365 + if (channel->is_otg_channel) 366 + rphy->otg_initialized = false; 367 + 368 + val = readl(usb2_base + USB2_INT_ENABLE); 369 + val &= ~rphy->int_enable_bits; 370 + if (!rcar_gen3_is_any_rphy_initialized(channel)) 371 + val &= ~USB2_INT_ENABLE_UCOM_INTEN; 372 + writel(val, usb2_base + USB2_INT_ENABLE); 409 373 410 374 return 0; 411 375 } 412 376 413 377 static int rcar_gen3_phy_usb2_power_on(struct phy *p) 414 378 { 415 - struct rcar_gen3_chan *channel = phy_get_drvdata(p); 379 + struct rcar_gen3_phy *rphy = phy_get_drvdata(p); 380 + struct rcar_gen3_chan *channel = rphy->ch; 416 381 void __iomem *usb2_base = channel->base; 417 382 u32 val; 418 383 int ret; 384 + 385 + if (!rcar_gen3_are_all_rphys_power_off(channel)) 386 + return 0; 419 387 420 388 if (channel->vbus) { 421 389 ret = regulator_enable(channel->vbus); ··· 454 372 val &= ~USB2_USBCTR_PLL_RST; 455 373 writel(val, usb2_base + USB2_USBCTR); 456 374 375 + rphy->powered = true; 376 + 457 377 return 0; 458 378 } 459 379 460 380 static int rcar_gen3_phy_usb2_power_off(struct phy *p) 461 381 { 462 - struct rcar_gen3_chan *channel = phy_get_drvdata(p); 382 + struct rcar_gen3_phy *rphy = phy_get_drvdata(p); 383 + struct rcar_gen3_chan *channel = rphy->ch; 463 384 int ret = 0; 385 + 386 + rphy->powered = false; 387 + 388 + if (!rcar_gen3_are_all_rphys_power_off(channel)) 389 + return 0; 464 390 465 391 if (channel->vbus) 466 392 ret = regulator_disable(channel->vbus); ··· 538 448 EXTCON_NONE, 539 449 }; 540 450 451 + static struct phy *rcar_gen3_phy_usb2_xlate(struct device *dev, 452 + struct of_phandle_args *args) 453 + { 454 + struct rcar_gen3_chan *ch = dev_get_drvdata(dev); 455 + 456 + if (args->args_count == 0) /* For old version dts */ 457 + return ch->rphys[PHY_INDEX_BOTH_HC].phy; 458 + else if (args->args_count > 1) /* Prevent invalid args count */ 459 + return ERR_PTR(-ENODEV); 460 + 461 + if (args->args[0] >= NUM_OF_PHYS) 462 + return ERR_PTR(-ENODEV); 463 + 464 + return ch->rphys[args->args[0]].phy; 465 + } 466 + 467 + static enum usb_dr_mode rcar_gen3_get_dr_mode(struct device_node *np) 468 + { 469 + enum usb_dr_mode candidate = USB_DR_MODE_UNKNOWN; 470 + int i; 471 + 472 + /* 473 + * If one of device nodes has other dr_mode except UNKNOWN, 474 + * this function returns UNKNOWN. To achieve backward compatibility, 475 + * this loop starts the index as 0. 476 + */ 477 + for (i = 0; i < NUM_OF_PHYS; i++) { 478 + enum usb_dr_mode mode = of_usb_get_dr_mode_by_phy(np, i); 479 + 480 + if (mode != USB_DR_MODE_UNKNOWN) { 481 + if (candidate == USB_DR_MODE_UNKNOWN) 482 + candidate = mode; 483 + else if (candidate != mode) 484 + return USB_DR_MODE_UNKNOWN; 485 + } 486 + } 487 + 488 + return candidate; 489 + } 490 + 541 491 static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev) 542 492 { 543 493 struct device *dev = &pdev->dev; ··· 585 455 struct phy_provider *provider; 586 456 struct resource *res; 587 457 const struct phy_ops *phy_usb2_ops; 588 - int irq, ret = 0; 458 + int irq, ret = 0, i; 589 459 590 460 if (!dev->of_node) { 591 461 dev_err(dev, "This driver needs device tree\n"); ··· 611 481 dev_err(dev, "No irq handler (%d)\n", irq); 612 482 } 613 483 614 - channel->dr_mode = of_usb_get_dr_mode_by_phy(dev->of_node, 0); 484 + channel->dr_mode = rcar_gen3_get_dr_mode(dev->of_node); 615 485 if (channel->dr_mode != USB_DR_MODE_UNKNOWN) { 616 486 int ret; 617 487 ··· 639 509 if (!phy_usb2_ops) 640 510 return -EINVAL; 641 511 642 - channel->phy = devm_phy_create(dev, NULL, phy_usb2_ops); 643 - if (IS_ERR(channel->phy)) { 644 - dev_err(dev, "Failed to create USB2 PHY\n"); 645 - ret = PTR_ERR(channel->phy); 646 - goto error; 512 + for (i = 0; i < NUM_OF_PHYS; i++) { 513 + channel->rphys[i].phy = devm_phy_create(dev, NULL, 514 + phy_usb2_ops); 515 + if (IS_ERR(channel->rphys[i].phy)) { 516 + dev_err(dev, "Failed to create USB2 PHY\n"); 517 + ret = PTR_ERR(channel->rphys[i].phy); 518 + goto error; 519 + } 520 + channel->rphys[i].ch = channel; 521 + channel->rphys[i].int_enable_bits = rcar_gen3_int_enable[i]; 522 + phy_set_drvdata(channel->rphys[i].phy, &channel->rphys[i]); 647 523 } 648 524 649 525 channel->vbus = devm_regulator_get_optional(dev, "vbus"); ··· 662 526 } 663 527 664 528 platform_set_drvdata(pdev, channel); 665 - phy_set_drvdata(channel->phy, channel); 666 529 channel->dev = dev; 667 530 668 - provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); 531 + provider = devm_of_phy_provider_register(dev, rcar_gen3_phy_usb2_xlate); 669 532 if (IS_ERR(provider)) { 670 533 dev_err(dev, "Failed to register PHY provider\n"); 671 534 ret = PTR_ERR(provider);