phy-armada375-usb2.c 4.3 KB

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