reset-rzg2l-usbphy-ctrl.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/regmap.h>
  13. #include <linux/reset.h>
  14. #include <linux/reset-controller.h>
  15. #define RESET 0x000
  16. #define VBENCTL 0x03c
  17. #define RESET_SEL_PLLRESET BIT(12)
  18. #define RESET_PLLRESET BIT(8)
  19. #define RESET_SEL_P2RESET BIT(5)
  20. #define RESET_SEL_P1RESET BIT(4)
  21. #define RESET_PHYRST_2 BIT(1)
  22. #define RESET_PHYRST_1 BIT(0)
  23. #define PHY_RESET_PORT2 (RESET_SEL_P2RESET | RESET_PHYRST_2)
  24. #define PHY_RESET_PORT1 (RESET_SEL_P1RESET | RESET_PHYRST_1)
  25. #define NUM_PORTS 2
  26. struct rzg2l_usbphy_ctrl_priv {
  27. struct reset_controller_dev rcdev;
  28. struct reset_control *rstc;
  29. void __iomem *base;
  30. struct platform_device *vdev;
  31. spinlock_t lock;
  32. };
  33. #define rcdev_to_priv(x) container_of(x, struct rzg2l_usbphy_ctrl_priv, rcdev)
  34. static int rzg2l_usbphy_ctrl_assert(struct reset_controller_dev *rcdev,
  35. unsigned long id)
  36. {
  37. struct rzg2l_usbphy_ctrl_priv *priv = rcdev_to_priv(rcdev);
  38. u32 port_mask = PHY_RESET_PORT1 | PHY_RESET_PORT2;
  39. void __iomem *base = priv->base;
  40. unsigned long flags;
  41. u32 val;
  42. spin_lock_irqsave(&priv->lock, flags);
  43. val = readl(base + RESET);
  44. val |= id ? PHY_RESET_PORT2 : PHY_RESET_PORT1;
  45. if (port_mask == (val & port_mask))
  46. val |= RESET_PLLRESET;
  47. writel(val, base + RESET);
  48. spin_unlock_irqrestore(&priv->lock, flags);
  49. return 0;
  50. }
  51. static int rzg2l_usbphy_ctrl_deassert(struct reset_controller_dev *rcdev,
  52. unsigned long id)
  53. {
  54. struct rzg2l_usbphy_ctrl_priv *priv = rcdev_to_priv(rcdev);
  55. void __iomem *base = priv->base;
  56. unsigned long flags;
  57. u32 val;
  58. spin_lock_irqsave(&priv->lock, flags);
  59. val = readl(base + RESET);
  60. val |= RESET_SEL_PLLRESET;
  61. val &= ~(RESET_PLLRESET | (id ? PHY_RESET_PORT2 : PHY_RESET_PORT1));
  62. writel(val, base + RESET);
  63. spin_unlock_irqrestore(&priv->lock, flags);
  64. return 0;
  65. }
  66. static int rzg2l_usbphy_ctrl_status(struct reset_controller_dev *rcdev,
  67. unsigned long id)
  68. {
  69. struct rzg2l_usbphy_ctrl_priv *priv = rcdev_to_priv(rcdev);
  70. u32 port_mask;
  71. port_mask = id ? PHY_RESET_PORT2 : PHY_RESET_PORT1;
  72. return !!(readl(priv->base + RESET) & port_mask);
  73. }
  74. static const struct of_device_id rzg2l_usbphy_ctrl_match_table[] = {
  75. { .compatible = "renesas,rzg2l-usbphy-ctrl" },
  76. { /* Sentinel */ }
  77. };
  78. MODULE_DEVICE_TABLE(of, rzg2l_usbphy_ctrl_match_table);
  79. static const struct reset_control_ops rzg2l_usbphy_ctrl_reset_ops = {
  80. .assert = rzg2l_usbphy_ctrl_assert,
  81. .deassert = rzg2l_usbphy_ctrl_deassert,
  82. .status = rzg2l_usbphy_ctrl_status,
  83. };
  84. static const struct regmap_config rzg2l_usb_regconf = {
  85. .reg_bits = 32,
  86. .val_bits = 32,
  87. .reg_stride = 4,
  88. .max_register = 1,
  89. };
  90. static int rzg2l_usbphy_ctrl_probe(struct platform_device *pdev)
  91. {
  92. struct device *dev = &pdev->dev;
  93. struct rzg2l_usbphy_ctrl_priv *priv;
  94. struct platform_device *vdev;
  95. struct regmap *regmap;
  96. unsigned long flags;
  97. int error;
  98. u32 val;
  99. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  100. if (!priv)
  101. return -ENOMEM;
  102. priv->base = devm_platform_ioremap_resource(pdev, 0);
  103. if (IS_ERR(priv->base))
  104. return PTR_ERR(priv->base);
  105. regmap = devm_regmap_init_mmio(dev, priv->base + VBENCTL, &rzg2l_usb_regconf);
  106. if (IS_ERR(regmap))
  107. return PTR_ERR(regmap);
  108. priv->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  109. if (IS_ERR(priv->rstc))
  110. return dev_err_probe(dev, PTR_ERR(priv->rstc),
  111. "failed to get reset\n");
  112. error = reset_control_deassert(priv->rstc);
  113. if (error)
  114. return error;
  115. spin_lock_init(&priv->lock);
  116. dev_set_drvdata(dev, priv);
  117. pm_runtime_enable(&pdev->dev);
  118. error = pm_runtime_resume_and_get(&pdev->dev);
  119. if (error < 0) {
  120. dev_err_probe(&pdev->dev, error, "pm_runtime_resume_and_get failed");
  121. goto err_pm_disable_reset_deassert;
  122. }
  123. /* put pll and phy into reset state */
  124. spin_lock_irqsave(&priv->lock, flags);
  125. val = readl(priv->base + RESET);
  126. val |= RESET_SEL_PLLRESET | RESET_PLLRESET | PHY_RESET_PORT2 | PHY_RESET_PORT1;
  127. writel(val, priv->base + RESET);
  128. spin_unlock_irqrestore(&priv->lock, flags);
  129. priv->rcdev.ops = &rzg2l_usbphy_ctrl_reset_ops;
  130. priv->rcdev.of_reset_n_cells = 1;
  131. priv->rcdev.nr_resets = NUM_PORTS;
  132. priv->rcdev.of_node = dev->of_node;
  133. priv->rcdev.dev = dev;
  134. error = devm_reset_controller_register(dev, &priv->rcdev);
  135. if (error)
  136. goto err_pm_runtime_put;
  137. vdev = platform_device_alloc("rzg2l-usb-vbus-regulator", pdev->id);
  138. if (!vdev) {
  139. error = -ENOMEM;
  140. goto err_pm_runtime_put;
  141. }
  142. vdev->dev.parent = dev;
  143. priv->vdev = vdev;
  144. device_set_of_node_from_dev(&vdev->dev, dev);
  145. error = platform_device_add(vdev);
  146. if (error)
  147. goto err_device_put;
  148. return 0;
  149. err_device_put:
  150. platform_device_put(vdev);
  151. err_pm_runtime_put:
  152. pm_runtime_put(&pdev->dev);
  153. err_pm_disable_reset_deassert:
  154. pm_runtime_disable(&pdev->dev);
  155. reset_control_assert(priv->rstc);
  156. return error;
  157. }
  158. static void rzg2l_usbphy_ctrl_remove(struct platform_device *pdev)
  159. {
  160. struct rzg2l_usbphy_ctrl_priv *priv = dev_get_drvdata(&pdev->dev);
  161. platform_device_unregister(priv->vdev);
  162. pm_runtime_put(&pdev->dev);
  163. pm_runtime_disable(&pdev->dev);
  164. reset_control_assert(priv->rstc);
  165. }
  166. static struct platform_driver rzg2l_usbphy_ctrl_driver = {
  167. .driver = {
  168. .name = "rzg2l_usbphy_ctrl",
  169. .of_match_table = rzg2l_usbphy_ctrl_match_table,
  170. },
  171. .probe = rzg2l_usbphy_ctrl_probe,
  172. .remove_new = rzg2l_usbphy_ctrl_remove,
  173. };
  174. module_platform_driver(rzg2l_usbphy_ctrl_driver);
  175. MODULE_LICENSE("GPL v2");
  176. MODULE_DESCRIPTION("Renesas RZ/G2L USBPHY Control");
  177. MODULE_AUTHOR("biju.das.jz@bp.renesas.com>");