phy-ma35d1-usb2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2024 Nuvoton Technology Corp.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/io.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/phy/phy.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. /* USB PHY Miscellaneous Control Register */
  17. #define MA35_SYS_REG_USBPMISCR 0x60
  18. #define PHY0POR BIT(0) /* PHY Power-On Reset Control Bit */
  19. #define PHY0SUSPEND BIT(1) /* PHY Suspend; 0: suspend, 1: operaion */
  20. #define PHY0COMN BIT(2) /* PHY Common Block Power-Down Control */
  21. #define PHY0DEVCKSTB BIT(10) /* PHY 60 MHz UTMI clock stable bit */
  22. struct ma35_usb_phy {
  23. struct clk *clk;
  24. struct device *dev;
  25. struct regmap *sysreg;
  26. };
  27. static int ma35_usb_phy_power_on(struct phy *phy)
  28. {
  29. struct ma35_usb_phy *p_phy = phy_get_drvdata(phy);
  30. unsigned int val;
  31. int ret;
  32. ret = clk_prepare_enable(p_phy->clk);
  33. if (ret < 0) {
  34. dev_err(p_phy->dev, "Failed to enable PHY clock: %d\n", ret);
  35. return ret;
  36. }
  37. regmap_read(p_phy->sysreg, MA35_SYS_REG_USBPMISCR, &val);
  38. if (val & PHY0SUSPEND) {
  39. /*
  40. * USB PHY0 is in operation mode already
  41. * make sure USB PHY 60 MHz UTMI Interface Clock ready
  42. */
  43. ret = regmap_read_poll_timeout(p_phy->sysreg, MA35_SYS_REG_USBPMISCR, val,
  44. val & PHY0DEVCKSTB, 10, 1000);
  45. if (ret == 0)
  46. return 0;
  47. }
  48. /*
  49. * reset USB PHY0.
  50. * wait until USB PHY0 60 MHz UTMI Interface Clock ready
  51. */
  52. regmap_update_bits(p_phy->sysreg, MA35_SYS_REG_USBPMISCR, 0x7, (PHY0POR | PHY0SUSPEND));
  53. udelay(20);
  54. /* make USB PHY0 enter operation mode */
  55. regmap_update_bits(p_phy->sysreg, MA35_SYS_REG_USBPMISCR, 0x7, PHY0SUSPEND);
  56. /* make sure USB PHY 60 MHz UTMI Interface Clock ready */
  57. ret = regmap_read_poll_timeout(p_phy->sysreg, MA35_SYS_REG_USBPMISCR, val,
  58. val & PHY0DEVCKSTB, 10, 1000);
  59. if (ret == -ETIMEDOUT) {
  60. dev_err(p_phy->dev, "Check PHY clock, Timeout: %d\n", ret);
  61. clk_disable_unprepare(p_phy->clk);
  62. return ret;
  63. }
  64. return 0;
  65. }
  66. static int ma35_usb_phy_power_off(struct phy *phy)
  67. {
  68. struct ma35_usb_phy *p_phy = phy_get_drvdata(phy);
  69. clk_disable_unprepare(p_phy->clk);
  70. return 0;
  71. }
  72. static const struct phy_ops ma35_usb_phy_ops = {
  73. .power_on = ma35_usb_phy_power_on,
  74. .power_off = ma35_usb_phy_power_off,
  75. .owner = THIS_MODULE,
  76. };
  77. static int ma35_usb_phy_probe(struct platform_device *pdev)
  78. {
  79. struct phy_provider *provider;
  80. struct ma35_usb_phy *p_phy;
  81. struct phy *phy;
  82. p_phy = devm_kzalloc(&pdev->dev, sizeof(*p_phy), GFP_KERNEL);
  83. if (!p_phy)
  84. return -ENOMEM;
  85. p_phy->dev = &pdev->dev;
  86. platform_set_drvdata(pdev, p_phy);
  87. p_phy->sysreg = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "nuvoton,sys");
  88. if (IS_ERR(p_phy->sysreg))
  89. return dev_err_probe(&pdev->dev, PTR_ERR(p_phy->sysreg),
  90. "Failed to get SYS registers\n");
  91. p_phy->clk = of_clk_get(pdev->dev.of_node, 0);
  92. if (IS_ERR(p_phy->clk))
  93. return dev_err_probe(&pdev->dev, PTR_ERR(p_phy->clk),
  94. "failed to find usb_phy clock\n");
  95. phy = devm_phy_create(&pdev->dev, NULL, &ma35_usb_phy_ops);
  96. if (IS_ERR(phy))
  97. return dev_err_probe(&pdev->dev, PTR_ERR(phy), "Failed to create PHY\n");
  98. phy_set_drvdata(phy, p_phy);
  99. provider = devm_of_phy_provider_register(&pdev->dev, of_phy_simple_xlate);
  100. if (IS_ERR(provider))
  101. return dev_err_probe(&pdev->dev, PTR_ERR(provider),
  102. "Failed to register PHY provider\n");
  103. return 0;
  104. }
  105. static const struct of_device_id ma35_usb_phy_of_match[] = {
  106. { .compatible = "nuvoton,ma35d1-usb2-phy", },
  107. { },
  108. };
  109. MODULE_DEVICE_TABLE(of, ma35_usb_phy_of_match);
  110. static struct platform_driver ma35_usb_phy_driver = {
  111. .probe = ma35_usb_phy_probe,
  112. .driver = {
  113. .name = "ma35d1-usb2-phy",
  114. .of_match_table = ma35_usb_phy_of_match,
  115. },
  116. };
  117. module_platform_driver(ma35_usb_phy_driver);
  118. MODULE_DESCRIPTION("Nuvoton ma35d1 USB2.0 PHY driver");
  119. MODULE_AUTHOR("Hui-Ping Chen <hpchen0nvt@gmail.com>");
  120. MODULE_LICENSE("GPL");