reset-rzv2h-usb2phy.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas RZ/V2H(P) USB2PHY Port reset control driver
  4. *
  5. * Copyright (C) 2025 Renesas Electronics Corporation
  6. */
  7. #include <linux/cleanup.h>
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/reset.h>
  15. #include <linux/reset-controller.h>
  16. struct rzv2h_usb2phy_regval {
  17. u16 reg;
  18. u16 val;
  19. };
  20. struct rzv2h_usb2phy_reset_of_data {
  21. const struct rzv2h_usb2phy_regval *init_vals;
  22. unsigned int init_val_count;
  23. u16 reset_reg;
  24. u16 reset_assert_val;
  25. u16 reset_deassert_val;
  26. u16 reset_status_bits;
  27. u16 reset_release_val;
  28. u16 reset2_reg;
  29. u16 reset2_acquire_val;
  30. u16 reset2_release_val;
  31. };
  32. struct rzv2h_usb2phy_reset_priv {
  33. const struct rzv2h_usb2phy_reset_of_data *data;
  34. void __iomem *base;
  35. struct device *dev;
  36. struct reset_controller_dev rcdev;
  37. spinlock_t lock; /* protects register accesses */
  38. };
  39. static inline struct rzv2h_usb2phy_reset_priv
  40. *rzv2h_usbphy_rcdev_to_priv(struct reset_controller_dev *rcdev)
  41. {
  42. return container_of(rcdev, struct rzv2h_usb2phy_reset_priv, rcdev);
  43. }
  44. /* This function must be called only after pm_runtime_resume_and_get() has been called */
  45. static void rzv2h_usbphy_assert_helper(struct rzv2h_usb2phy_reset_priv *priv)
  46. {
  47. const struct rzv2h_usb2phy_reset_of_data *data = priv->data;
  48. scoped_guard(spinlock, &priv->lock) {
  49. writel(data->reset2_acquire_val, priv->base + data->reset2_reg);
  50. writel(data->reset_assert_val, priv->base + data->reset_reg);
  51. }
  52. usleep_range(11, 20);
  53. }
  54. static int rzv2h_usbphy_reset_assert(struct reset_controller_dev *rcdev,
  55. unsigned long id)
  56. {
  57. struct rzv2h_usb2phy_reset_priv *priv = rzv2h_usbphy_rcdev_to_priv(rcdev);
  58. struct device *dev = priv->dev;
  59. int ret;
  60. ret = pm_runtime_resume_and_get(dev);
  61. if (ret) {
  62. dev_err(dev, "pm_runtime_resume_and_get failed\n");
  63. return ret;
  64. }
  65. rzv2h_usbphy_assert_helper(priv);
  66. pm_runtime_put(dev);
  67. return 0;
  68. }
  69. static int rzv2h_usbphy_reset_deassert(struct reset_controller_dev *rcdev,
  70. unsigned long id)
  71. {
  72. struct rzv2h_usb2phy_reset_priv *priv = rzv2h_usbphy_rcdev_to_priv(rcdev);
  73. const struct rzv2h_usb2phy_reset_of_data *data = priv->data;
  74. struct device *dev = priv->dev;
  75. int ret;
  76. ret = pm_runtime_resume_and_get(dev);
  77. if (ret) {
  78. dev_err(dev, "pm_runtime_resume_and_get failed\n");
  79. return ret;
  80. }
  81. scoped_guard(spinlock, &priv->lock) {
  82. writel(data->reset_deassert_val, priv->base + data->reset_reg);
  83. writel(data->reset2_release_val, priv->base + data->reset2_reg);
  84. writel(data->reset_release_val, priv->base + data->reset_reg);
  85. }
  86. pm_runtime_put(dev);
  87. return 0;
  88. }
  89. static int rzv2h_usbphy_reset_status(struct reset_controller_dev *rcdev,
  90. unsigned long id)
  91. {
  92. struct rzv2h_usb2phy_reset_priv *priv = rzv2h_usbphy_rcdev_to_priv(rcdev);
  93. struct device *dev = priv->dev;
  94. int ret;
  95. u32 reg;
  96. ret = pm_runtime_resume_and_get(dev);
  97. if (ret) {
  98. dev_err(dev, "pm_runtime_resume_and_get failed\n");
  99. return ret;
  100. }
  101. reg = readl(priv->base + priv->data->reset_reg);
  102. pm_runtime_put(dev);
  103. return (reg & priv->data->reset_status_bits) == priv->data->reset_status_bits;
  104. }
  105. static const struct reset_control_ops rzv2h_usbphy_reset_ops = {
  106. .assert = rzv2h_usbphy_reset_assert,
  107. .deassert = rzv2h_usbphy_reset_deassert,
  108. .status = rzv2h_usbphy_reset_status,
  109. };
  110. static int rzv2h_usb2phy_reset_of_xlate(struct reset_controller_dev *rcdev,
  111. const struct of_phandle_args *reset_spec)
  112. {
  113. /* No special handling needed, we have only one reset line per device */
  114. return 0;
  115. }
  116. static int rzv2h_usb2phy_reset_probe(struct platform_device *pdev)
  117. {
  118. const struct rzv2h_usb2phy_reset_of_data *data;
  119. struct rzv2h_usb2phy_reset_priv *priv;
  120. struct device *dev = &pdev->dev;
  121. struct reset_control *rstc;
  122. int error;
  123. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  124. if (!priv)
  125. return -ENOMEM;
  126. data = of_device_get_match_data(dev);
  127. priv->data = data;
  128. priv->dev = dev;
  129. priv->base = devm_platform_ioremap_resource(pdev, 0);
  130. if (IS_ERR(priv->base))
  131. return PTR_ERR(priv->base);
  132. rstc = devm_reset_control_get_shared_deasserted(dev, NULL);
  133. if (IS_ERR(rstc))
  134. return dev_err_probe(dev, PTR_ERR(rstc),
  135. "failed to get deasserted reset\n");
  136. spin_lock_init(&priv->lock);
  137. error = devm_pm_runtime_enable(dev);
  138. if (error)
  139. return dev_err_probe(dev, error, "Failed to enable pm_runtime\n");
  140. error = pm_runtime_resume_and_get(dev);
  141. if (error)
  142. return dev_err_probe(dev, error, "pm_runtime_resume_and_get failed\n");
  143. for (unsigned int i = 0; i < data->init_val_count; i++)
  144. writel(data->init_vals[i].val, priv->base + data->init_vals[i].reg);
  145. /* keep usb2phy in asserted state */
  146. rzv2h_usbphy_assert_helper(priv);
  147. pm_runtime_put(dev);
  148. priv->rcdev.ops = &rzv2h_usbphy_reset_ops;
  149. priv->rcdev.of_reset_n_cells = 0;
  150. priv->rcdev.nr_resets = 1;
  151. priv->rcdev.of_xlate = rzv2h_usb2phy_reset_of_xlate;
  152. priv->rcdev.of_node = dev->of_node;
  153. priv->rcdev.dev = dev;
  154. return devm_reset_controller_register(dev, &priv->rcdev);
  155. }
  156. /*
  157. * initialization values required to prepare the PHY to receive
  158. * assert and deassert requests.
  159. */
  160. static const struct rzv2h_usb2phy_regval rzv2h_init_vals[] = {
  161. { .reg = 0xc10, .val = 0x67c },
  162. { .reg = 0xc14, .val = 0x1f },
  163. { .reg = 0x600, .val = 0x909 },
  164. };
  165. static const struct rzv2h_usb2phy_reset_of_data rzv2h_reset_of_data = {
  166. .init_vals = rzv2h_init_vals,
  167. .init_val_count = ARRAY_SIZE(rzv2h_init_vals),
  168. .reset_reg = 0,
  169. .reset_assert_val = 0x206,
  170. .reset_status_bits = BIT(2),
  171. .reset_deassert_val = 0x200,
  172. .reset_release_val = 0x0,
  173. .reset2_reg = 0xb04,
  174. .reset2_acquire_val = 0x303,
  175. .reset2_release_val = 0x3,
  176. };
  177. static const struct of_device_id rzv2h_usb2phy_reset_of_match[] = {
  178. { .compatible = "renesas,r9a09g057-usb2phy-reset", .data = &rzv2h_reset_of_data },
  179. { /* Sentinel */ }
  180. };
  181. MODULE_DEVICE_TABLE(of, rzv2h_usb2phy_reset_of_match);
  182. static struct platform_driver rzv2h_usb2phy_reset_driver = {
  183. .driver = {
  184. .name = "rzv2h_usb2phy_reset",
  185. .of_match_table = rzv2h_usb2phy_reset_of_match,
  186. },
  187. .probe = rzv2h_usb2phy_reset_probe,
  188. };
  189. module_platform_driver(rzv2h_usb2phy_reset_driver);
  190. MODULE_LICENSE("GPL");
  191. MODULE_AUTHOR("Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>");
  192. MODULE_DESCRIPTION("Renesas RZ/V2H(P) USB2PHY Control");