reset-rzg2l-usbphy-ctrl.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas RZ/G2L USBPHY control driver
  4. *
  5. * Copyright (C) 2021 Renesas Electronics Corporation
  6. */
  7. #include <linux/io.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/reset.h>
  13. #include <linux/reset-controller.h>
  14. #define RESET 0x000
  15. #define RESET_SEL_PLLRESET BIT(12)
  16. #define RESET_PLLRESET BIT(8)
  17. #define RESET_SEL_P2RESET BIT(5)
  18. #define RESET_SEL_P1RESET BIT(4)
  19. #define RESET_PHYRST_2 BIT(1)
  20. #define RESET_PHYRST_1 BIT(0)
  21. #define PHY_RESET_PORT2 (RESET_SEL_P2RESET | RESET_PHYRST_2)
  22. #define PHY_RESET_PORT1 (RESET_SEL_P1RESET | RESET_PHYRST_1)
  23. #define NUM_PORTS 2
  24. struct rzg2l_usbphy_ctrl_priv {
  25. struct reset_controller_dev rcdev;
  26. struct reset_control *rstc;
  27. void __iomem *base;
  28. spinlock_t lock;
  29. };
  30. #define rcdev_to_priv(x) container_of(x, struct rzg2l_usbphy_ctrl_priv, rcdev)
  31. static int rzg2l_usbphy_ctrl_assert(struct reset_controller_dev *rcdev,
  32. unsigned long id)
  33. {
  34. struct rzg2l_usbphy_ctrl_priv *priv = rcdev_to_priv(rcdev);
  35. u32 port_mask = PHY_RESET_PORT1 | PHY_RESET_PORT2;
  36. void __iomem *base = priv->base;
  37. unsigned long flags;
  38. u32 val;
  39. spin_lock_irqsave(&priv->lock, flags);
  40. val = readl(base + RESET);
  41. val |= id ? PHY_RESET_PORT2 : PHY_RESET_PORT1;
  42. if (port_mask == (val & port_mask))
  43. val |= RESET_PLLRESET;
  44. writel(val, base + RESET);
  45. spin_unlock_irqrestore(&priv->lock, flags);
  46. return 0;
  47. }
  48. static int rzg2l_usbphy_ctrl_deassert(struct reset_controller_dev *rcdev,
  49. unsigned long id)
  50. {
  51. struct rzg2l_usbphy_ctrl_priv *priv = rcdev_to_priv(rcdev);
  52. void __iomem *base = priv->base;
  53. unsigned long flags;
  54. u32 val;
  55. spin_lock_irqsave(&priv->lock, flags);
  56. val = readl(base + RESET);
  57. val |= RESET_SEL_PLLRESET;
  58. val &= ~(RESET_PLLRESET | (id ? PHY_RESET_PORT2 : PHY_RESET_PORT1));
  59. writel(val, base + RESET);
  60. spin_unlock_irqrestore(&priv->lock, flags);
  61. return 0;
  62. }
  63. static int rzg2l_usbphy_ctrl_status(struct reset_controller_dev *rcdev,
  64. unsigned long id)
  65. {
  66. struct rzg2l_usbphy_ctrl_priv *priv = rcdev_to_priv(rcdev);
  67. u32 port_mask;
  68. port_mask = id ? PHY_RESET_PORT2 : PHY_RESET_PORT1;
  69. return !!(readl(priv->base + RESET) & port_mask);
  70. }
  71. static const struct of_device_id rzg2l_usbphy_ctrl_match_table[] = {
  72. { .compatible = "renesas,rzg2l-usbphy-ctrl" },
  73. { /* Sentinel */ }
  74. };
  75. MODULE_DEVICE_TABLE(of, rzg2l_usbphy_ctrl_match_table);
  76. static const struct reset_control_ops rzg2l_usbphy_ctrl_reset_ops = {
  77. .assert = rzg2l_usbphy_ctrl_assert,
  78. .deassert = rzg2l_usbphy_ctrl_deassert,
  79. .status = rzg2l_usbphy_ctrl_status,
  80. };
  81. static int rzg2l_usbphy_ctrl_probe(struct platform_device *pdev)
  82. {
  83. struct device *dev = &pdev->dev;
  84. struct rzg2l_usbphy_ctrl_priv *priv;
  85. unsigned long flags;
  86. int error;
  87. u32 val;
  88. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  89. if (!priv)
  90. return -ENOMEM;
  91. priv->base = devm_platform_ioremap_resource(pdev, 0);
  92. if (IS_ERR(priv->base))
  93. return PTR_ERR(priv->base);
  94. priv->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  95. if (IS_ERR(priv->rstc))
  96. return dev_err_probe(dev, PTR_ERR(priv->rstc),
  97. "failed to get reset\n");
  98. error = reset_control_deassert(priv->rstc);
  99. if (error)
  100. return error;
  101. priv->rcdev.ops = &rzg2l_usbphy_ctrl_reset_ops;
  102. priv->rcdev.of_reset_n_cells = 1;
  103. priv->rcdev.nr_resets = NUM_PORTS;
  104. priv->rcdev.of_node = dev->of_node;
  105. priv->rcdev.dev = dev;
  106. error = devm_reset_controller_register(dev, &priv->rcdev);
  107. if (error)
  108. return error;
  109. spin_lock_init(&priv->lock);
  110. dev_set_drvdata(dev, priv);
  111. pm_runtime_enable(&pdev->dev);
  112. error = pm_runtime_resume_and_get(&pdev->dev);
  113. if (error < 0) {
  114. pm_runtime_disable(&pdev->dev);
  115. reset_control_assert(priv->rstc);
  116. return dev_err_probe(&pdev->dev, error, "pm_runtime_resume_and_get failed");
  117. }
  118. /* put pll and phy into reset state */
  119. spin_lock_irqsave(&priv->lock, flags);
  120. val = readl(priv->base + RESET);
  121. val |= RESET_SEL_PLLRESET | RESET_PLLRESET | PHY_RESET_PORT2 | PHY_RESET_PORT1;
  122. writel(val, priv->base + RESET);
  123. spin_unlock_irqrestore(&priv->lock, flags);
  124. return 0;
  125. }
  126. static int rzg2l_usbphy_ctrl_remove(struct platform_device *pdev)
  127. {
  128. struct rzg2l_usbphy_ctrl_priv *priv = dev_get_drvdata(&pdev->dev);
  129. pm_runtime_put(&pdev->dev);
  130. pm_runtime_disable(&pdev->dev);
  131. reset_control_assert(priv->rstc);
  132. return 0;
  133. }
  134. static struct platform_driver rzg2l_usbphy_ctrl_driver = {
  135. .driver = {
  136. .name = "rzg2l_usbphy_ctrl",
  137. .of_match_table = rzg2l_usbphy_ctrl_match_table,
  138. },
  139. .probe = rzg2l_usbphy_ctrl_probe,
  140. .remove = rzg2l_usbphy_ctrl_remove,
  141. };
  142. module_platform_driver(rzg2l_usbphy_ctrl_driver);
  143. MODULE_LICENSE("GPL v2");
  144. MODULE_DESCRIPTION("Renesas RZ/G2L USBPHY Control");
  145. MODULE_AUTHOR("[email protected]>");