reset-jh7110.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2022 StarFive Technology Co., Ltd.
  4. * Author: Yanhong Wang <yanhong.wang@starfivetech.com>
  5. *
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <dm/ofnode.h>
  10. #include <dt-bindings/reset/starfive,jh7110-crg.h>
  11. #include <errno.h>
  12. #include <linux/iopoll.h>
  13. #include <reset-uclass.h>
  14. struct jh7110_reset_priv {
  15. void __iomem *reg;
  16. u32 assert;
  17. u32 status;
  18. u32 resets;
  19. };
  20. struct reset_info {
  21. const char *compat;
  22. const u32 nr_resets;
  23. const u32 assert_offset;
  24. const u32 status_offset;
  25. };
  26. static const struct reset_info jh7110_rst_info[] = {
  27. {
  28. .compat = "starfive,jh7110-syscrg",
  29. .nr_resets = JH7110_SYSRST_END,
  30. .assert_offset = 0x2F8,
  31. .status_offset = 0x308,
  32. },
  33. {
  34. .compat = "starfive,jh7110-aoncrg",
  35. .nr_resets = JH7110_AONRST_END,
  36. .assert_offset = 0x38,
  37. .status_offset = 0x3C,
  38. },
  39. {
  40. .compat = "starfive,jh7110-stgcrg",
  41. .nr_resets = JH7110_STGRST_END,
  42. .assert_offset = 0x74,
  43. .status_offset = 0x78,
  44. }
  45. };
  46. static const struct reset_info *jh7110_reset_get_cfg(const char *compat)
  47. {
  48. int i;
  49. for (i = 0; i < ARRAY_SIZE(jh7110_rst_info); i++)
  50. if (!strcmp(compat, jh7110_rst_info[i].compat))
  51. return &jh7110_rst_info[i];
  52. return NULL;
  53. }
  54. static int jh7110_reset_trigger(struct jh7110_reset_priv *priv,
  55. unsigned long id, bool assert)
  56. {
  57. ulong group;
  58. u32 mask, value, done = 0;
  59. ulong addr;
  60. group = id / 32;
  61. mask = BIT(id % 32);
  62. if (!assert)
  63. done ^= mask;
  64. addr = (ulong)priv->reg + priv->assert + group * sizeof(u32);
  65. value = readl((ulong *)addr);
  66. if (assert)
  67. value |= mask;
  68. else
  69. value &= ~mask;
  70. writel(value, (ulong *)addr);
  71. addr = (ulong)priv->reg + priv->status + group * sizeof(u32);
  72. return readl_poll_timeout((ulong *)addr, value,
  73. (value & mask) == done, 1000);
  74. }
  75. static int jh7110_reset_assert(struct reset_ctl *rst)
  76. {
  77. struct jh7110_reset_priv *priv = dev_get_priv(rst->dev);
  78. jh7110_reset_trigger(priv, rst->id, true);
  79. return 0;
  80. }
  81. static int jh7110_reset_deassert(struct reset_ctl *rst)
  82. {
  83. struct jh7110_reset_priv *priv = dev_get_priv(rst->dev);
  84. jh7110_reset_trigger(priv, rst->id, false);
  85. return 0;
  86. }
  87. static int jh7110_reset_free(struct reset_ctl *rst)
  88. {
  89. return 0;
  90. }
  91. static int jh7110_reset_request(struct reset_ctl *rst)
  92. {
  93. struct jh7110_reset_priv *priv = dev_get_priv(rst->dev);
  94. if (rst->id >= priv->resets)
  95. return -EINVAL;
  96. return 0;
  97. }
  98. static int jh7110_reset_probe(struct udevice *dev)
  99. {
  100. struct jh7110_reset_priv *priv = dev_get_priv(dev);
  101. const struct reset_info *cfg;
  102. const char *compat;
  103. compat = ofnode_get_property(dev_ofnode(dev), "compatible", NULL);
  104. if (!compat)
  105. return -EINVAL;
  106. cfg = jh7110_reset_get_cfg(compat);
  107. if (!cfg)
  108. return -EINVAL;
  109. priv->assert = cfg->assert_offset;
  110. priv->status = cfg->status_offset;
  111. priv->resets = cfg->nr_resets;
  112. priv->reg = (void __iomem *)dev_read_addr_index(dev, 0);
  113. return 0;
  114. }
  115. const struct reset_ops jh7110_reset_reset_ops = {
  116. .rfree = jh7110_reset_free,
  117. .request = jh7110_reset_request,
  118. .rst_assert = jh7110_reset_assert,
  119. .rst_deassert = jh7110_reset_deassert,
  120. };
  121. U_BOOT_DRIVER(jh7110_reset) = {
  122. .name = "jh7110_reset",
  123. .id = UCLASS_RESET,
  124. .ops = &jh7110_reset_reset_ops,
  125. .probe = jh7110_reset_probe,
  126. .priv_auto = sizeof(struct jh7110_reset_priv),
  127. };