phy-armada375-usb2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * USB cluster support for Armada 375 platform.
  4. *
  5. * Copyright (C) 2014 Marvell
  6. *
  7. * Gregory CLEMENT <[email protected]>
  8. *
  9. * Armada 375 comes with an USB2 host and device controller and an
  10. * USB3 controller. The USB cluster control register allows to manage
  11. * common features of both USB controllers.
  12. */
  13. #include <dt-bindings/phy/phy.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/of_address.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/platform_device.h>
  20. #define USB2_PHY_CONFIG_DISABLE BIT(0)
  21. struct armada375_cluster_phy {
  22. struct phy *phy;
  23. void __iomem *reg;
  24. bool use_usb3;
  25. int phy_provided;
  26. };
  27. static int armada375_usb_phy_init(struct phy *phy)
  28. {
  29. struct armada375_cluster_phy *cluster_phy;
  30. u32 reg;
  31. cluster_phy = phy_get_drvdata(phy);
  32. if (!cluster_phy)
  33. return -ENODEV;
  34. reg = readl(cluster_phy->reg);
  35. if (cluster_phy->use_usb3)
  36. reg |= USB2_PHY_CONFIG_DISABLE;
  37. else
  38. reg &= ~USB2_PHY_CONFIG_DISABLE;
  39. writel(reg, cluster_phy->reg);
  40. return 0;
  41. }
  42. static const struct phy_ops armada375_usb_phy_ops = {
  43. .init = armada375_usb_phy_init,
  44. .owner = THIS_MODULE,
  45. };
  46. /*
  47. * Only one controller can use this PHY. We shouldn't have the case
  48. * when two controllers want to use this PHY. But if this case occurs
  49. * then we provide a phy to the first one and return an error for the
  50. * next one. This error has also to be an error returned by
  51. * devm_phy_optional_get() so different from ENODEV for USB2. In the
  52. * USB3 case it still optional and we use ENODEV.
  53. */
  54. static struct phy *armada375_usb_phy_xlate(struct device *dev,
  55. struct of_phandle_args *args)
  56. {
  57. struct armada375_cluster_phy *cluster_phy = dev_get_drvdata(dev);
  58. if (!cluster_phy)
  59. return ERR_PTR(-ENODEV);
  60. /*
  61. * Either the phy had never been requested and then the first
  62. * usb claiming it can get it, or it had already been
  63. * requested in this case, we only allow to use it with the
  64. * same configuration.
  65. */
  66. if (WARN_ON((cluster_phy->phy_provided != PHY_NONE) &&
  67. (cluster_phy->phy_provided != args->args[0]))) {
  68. dev_err(dev, "This PHY has already been provided!\n");
  69. dev_err(dev, "Check your device tree, only one controller can use it\n.");
  70. if (args->args[0] == PHY_TYPE_USB2)
  71. return ERR_PTR(-EBUSY);
  72. else
  73. return ERR_PTR(-ENODEV);
  74. }
  75. if (args->args[0] == PHY_TYPE_USB2)
  76. cluster_phy->use_usb3 = false;
  77. else if (args->args[0] == PHY_TYPE_USB3)
  78. cluster_phy->use_usb3 = true;
  79. else {
  80. dev_err(dev, "Invalid PHY mode\n");
  81. return ERR_PTR(-ENODEV);
  82. }
  83. /* Store which phy mode is used for next test */
  84. cluster_phy->phy_provided = args->args[0];
  85. return cluster_phy->phy;
  86. }
  87. static int armada375_usb_phy_probe(struct platform_device *pdev)
  88. {
  89. struct device *dev = &pdev->dev;
  90. struct phy *phy;
  91. struct phy_provider *phy_provider;
  92. void __iomem *usb_cluster_base;
  93. struct armada375_cluster_phy *cluster_phy;
  94. cluster_phy = devm_kzalloc(dev, sizeof(*cluster_phy), GFP_KERNEL);
  95. if (!cluster_phy)
  96. return -ENOMEM;
  97. usb_cluster_base = devm_platform_ioremap_resource(pdev, 0);
  98. if (IS_ERR(usb_cluster_base))
  99. return PTR_ERR(usb_cluster_base);
  100. phy = devm_phy_create(dev, NULL, &armada375_usb_phy_ops);
  101. if (IS_ERR(phy)) {
  102. dev_err(dev, "failed to create PHY\n");
  103. return PTR_ERR(phy);
  104. }
  105. cluster_phy->phy = phy;
  106. cluster_phy->reg = usb_cluster_base;
  107. dev_set_drvdata(dev, cluster_phy);
  108. phy_set_drvdata(phy, cluster_phy);
  109. phy_provider = devm_of_phy_provider_register(&pdev->dev,
  110. armada375_usb_phy_xlate);
  111. return PTR_ERR_OR_ZERO(phy_provider);
  112. }
  113. static const struct of_device_id of_usb_cluster_table[] = {
  114. { .compatible = "marvell,armada-375-usb-cluster", },
  115. { /* end of list */ },
  116. };
  117. static struct platform_driver armada375_usb_phy_driver = {
  118. .probe = armada375_usb_phy_probe,
  119. .driver = {
  120. .of_match_table = of_usb_cluster_table,
  121. .name = "armada-375-usb-cluster",
  122. }
  123. };
  124. builtin_platform_driver(armada375_usb_phy_driver);