phy-bcm-kona-usb2.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * phy-bcm-kona-usb2.c - Broadcom Kona USB2 Phy Driver
  3. *
  4. * Copyright (C) 2013 Linaro Limited
  5. * Matt Porter <mporter@linaro.org>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/platform_device.h>
  24. #define OTGCTL (0)
  25. #define OTGCTL_OTGSTAT2 BIT(31)
  26. #define OTGCTL_OTGSTAT1 BIT(30)
  27. #define OTGCTL_PRST_N_SW BIT(11)
  28. #define OTGCTL_HRESET_N BIT(10)
  29. #define OTGCTL_UTMI_LINE_STATE1 BIT(9)
  30. #define OTGCTL_UTMI_LINE_STATE0 BIT(8)
  31. #define P1CTL (8)
  32. #define P1CTL_SOFT_RESET BIT(1)
  33. #define P1CTL_NON_DRIVING BIT(0)
  34. struct bcm_kona_usb {
  35. void __iomem *regs;
  36. };
  37. static void bcm_kona_usb_phy_power(struct bcm_kona_usb *phy, int on)
  38. {
  39. u32 val;
  40. val = readl(phy->regs + OTGCTL);
  41. if (on) {
  42. /* Configure and power PHY */
  43. val &= ~(OTGCTL_OTGSTAT2 | OTGCTL_OTGSTAT1 |
  44. OTGCTL_UTMI_LINE_STATE1 | OTGCTL_UTMI_LINE_STATE0);
  45. val |= OTGCTL_PRST_N_SW | OTGCTL_HRESET_N;
  46. } else {
  47. val &= ~(OTGCTL_PRST_N_SW | OTGCTL_HRESET_N);
  48. }
  49. writel(val, phy->regs + OTGCTL);
  50. }
  51. static int bcm_kona_usb_phy_init(struct phy *gphy)
  52. {
  53. struct bcm_kona_usb *phy = phy_get_drvdata(gphy);
  54. u32 val;
  55. /* Soft reset PHY */
  56. val = readl(phy->regs + P1CTL);
  57. val &= ~P1CTL_NON_DRIVING;
  58. val |= P1CTL_SOFT_RESET;
  59. writel(val, phy->regs + P1CTL);
  60. writel(val & ~P1CTL_SOFT_RESET, phy->regs + P1CTL);
  61. /* Reset needs to be asserted for 2ms */
  62. mdelay(2);
  63. writel(val | P1CTL_SOFT_RESET, phy->regs + P1CTL);
  64. return 0;
  65. }
  66. static int bcm_kona_usb_phy_power_on(struct phy *gphy)
  67. {
  68. struct bcm_kona_usb *phy = phy_get_drvdata(gphy);
  69. bcm_kona_usb_phy_power(phy, 1);
  70. return 0;
  71. }
  72. static int bcm_kona_usb_phy_power_off(struct phy *gphy)
  73. {
  74. struct bcm_kona_usb *phy = phy_get_drvdata(gphy);
  75. bcm_kona_usb_phy_power(phy, 0);
  76. return 0;
  77. }
  78. static const struct phy_ops ops = {
  79. .init = bcm_kona_usb_phy_init,
  80. .power_on = bcm_kona_usb_phy_power_on,
  81. .power_off = bcm_kona_usb_phy_power_off,
  82. .owner = THIS_MODULE,
  83. };
  84. static int bcm_kona_usb2_probe(struct platform_device *pdev)
  85. {
  86. struct device *dev = &pdev->dev;
  87. struct bcm_kona_usb *phy;
  88. struct resource *res;
  89. struct phy *gphy;
  90. struct phy_provider *phy_provider;
  91. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  92. if (!phy)
  93. return -ENOMEM;
  94. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  95. phy->regs = devm_ioremap_resource(&pdev->dev, res);
  96. if (IS_ERR(phy->regs))
  97. return PTR_ERR(phy->regs);
  98. platform_set_drvdata(pdev, phy);
  99. gphy = devm_phy_create(dev, NULL, &ops);
  100. if (IS_ERR(gphy))
  101. return PTR_ERR(gphy);
  102. /* The Kona PHY supports an 8-bit wide UTMI interface */
  103. phy_set_bus_width(gphy, 8);
  104. phy_set_drvdata(gphy, phy);
  105. phy_provider = devm_of_phy_provider_register(dev,
  106. of_phy_simple_xlate);
  107. return PTR_ERR_OR_ZERO(phy_provider);
  108. }
  109. static const struct of_device_id bcm_kona_usb2_dt_ids[] = {
  110. { .compatible = "brcm,kona-usb2-phy" },
  111. { /* sentinel */ }
  112. };
  113. MODULE_DEVICE_TABLE(of, bcm_kona_usb2_dt_ids);
  114. static struct platform_driver bcm_kona_usb2_driver = {
  115. .probe = bcm_kona_usb2_probe,
  116. .driver = {
  117. .name = "bcm-kona-usb2",
  118. .of_match_table = bcm_kona_usb2_dt_ids,
  119. },
  120. };
  121. module_platform_driver(bcm_kona_usb2_driver);
  122. MODULE_ALIAS("platform:bcm-kona-usb2");
  123. MODULE_AUTHOR("Matt Porter <mporter@linaro.org>");
  124. MODULE_DESCRIPTION("BCM Kona USB 2.0 PHY driver");
  125. MODULE_LICENSE("GPL v2");