reset-starfive-jh7100.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Reset driver for the StarFive JH7100 SoC
  4. *
  5. * Copyright (C) 2021 Emil Renner Berthing <[email protected]>
  6. */
  7. #include <linux/bitmap.h>
  8. #include <linux/io.h>
  9. #include <linux/io-64-nonatomic-lo-hi.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/reset-controller.h>
  14. #include <linux/spinlock.h>
  15. #include <dt-bindings/reset/starfive-jh7100.h>
  16. /* register offsets */
  17. #define JH7100_RESET_ASSERT0 0x00
  18. #define JH7100_RESET_ASSERT1 0x04
  19. #define JH7100_RESET_ASSERT2 0x08
  20. #define JH7100_RESET_ASSERT3 0x0c
  21. #define JH7100_RESET_STATUS0 0x10
  22. #define JH7100_RESET_STATUS1 0x14
  23. #define JH7100_RESET_STATUS2 0x18
  24. #define JH7100_RESET_STATUS3 0x1c
  25. /*
  26. * Writing a 1 to the n'th bit of the m'th ASSERT register asserts
  27. * line 32m + n, and writing a 0 deasserts the same line.
  28. * Most reset lines have their status inverted so a 0 bit in the STATUS
  29. * register means the line is asserted and a 1 means it's deasserted. A few
  30. * lines don't though, so store the expected value of the status registers when
  31. * all lines are asserted.
  32. */
  33. static const u64 jh7100_reset_asserted[2] = {
  34. /* STATUS0 */
  35. BIT_ULL_MASK(JH7100_RST_U74) |
  36. BIT_ULL_MASK(JH7100_RST_VP6_DRESET) |
  37. BIT_ULL_MASK(JH7100_RST_VP6_BRESET) |
  38. /* STATUS1 */
  39. BIT_ULL_MASK(JH7100_RST_HIFI4_DRESET) |
  40. BIT_ULL_MASK(JH7100_RST_HIFI4_BRESET),
  41. /* STATUS2 */
  42. BIT_ULL_MASK(JH7100_RST_E24) |
  43. /* STATUS3 */
  44. 0,
  45. };
  46. struct jh7100_reset {
  47. struct reset_controller_dev rcdev;
  48. /* protect registers against concurrent read-modify-write */
  49. spinlock_t lock;
  50. void __iomem *base;
  51. };
  52. static inline struct jh7100_reset *
  53. jh7100_reset_from(struct reset_controller_dev *rcdev)
  54. {
  55. return container_of(rcdev, struct jh7100_reset, rcdev);
  56. }
  57. static int jh7100_reset_update(struct reset_controller_dev *rcdev,
  58. unsigned long id, bool assert)
  59. {
  60. struct jh7100_reset *data = jh7100_reset_from(rcdev);
  61. unsigned long offset = BIT_ULL_WORD(id);
  62. u64 mask = BIT_ULL_MASK(id);
  63. void __iomem *reg_assert = data->base + JH7100_RESET_ASSERT0 + offset * sizeof(u64);
  64. void __iomem *reg_status = data->base + JH7100_RESET_STATUS0 + offset * sizeof(u64);
  65. u64 done = jh7100_reset_asserted[offset] & mask;
  66. u64 value;
  67. unsigned long flags;
  68. int ret;
  69. if (!assert)
  70. done ^= mask;
  71. spin_lock_irqsave(&data->lock, flags);
  72. value = readq(reg_assert);
  73. if (assert)
  74. value |= mask;
  75. else
  76. value &= ~mask;
  77. writeq(value, reg_assert);
  78. /* if the associated clock is gated, deasserting might otherwise hang forever */
  79. ret = readq_poll_timeout_atomic(reg_status, value, (value & mask) == done, 0, 1000);
  80. spin_unlock_irqrestore(&data->lock, flags);
  81. return ret;
  82. }
  83. static int jh7100_reset_assert(struct reset_controller_dev *rcdev,
  84. unsigned long id)
  85. {
  86. return jh7100_reset_update(rcdev, id, true);
  87. }
  88. static int jh7100_reset_deassert(struct reset_controller_dev *rcdev,
  89. unsigned long id)
  90. {
  91. return jh7100_reset_update(rcdev, id, false);
  92. }
  93. static int jh7100_reset_reset(struct reset_controller_dev *rcdev,
  94. unsigned long id)
  95. {
  96. int ret;
  97. ret = jh7100_reset_assert(rcdev, id);
  98. if (ret)
  99. return ret;
  100. return jh7100_reset_deassert(rcdev, id);
  101. }
  102. static int jh7100_reset_status(struct reset_controller_dev *rcdev,
  103. unsigned long id)
  104. {
  105. struct jh7100_reset *data = jh7100_reset_from(rcdev);
  106. unsigned long offset = BIT_ULL_WORD(id);
  107. u64 mask = BIT_ULL_MASK(id);
  108. void __iomem *reg_status = data->base + JH7100_RESET_STATUS0 + offset * sizeof(u64);
  109. u64 value = readq(reg_status);
  110. return !((value ^ jh7100_reset_asserted[offset]) & mask);
  111. }
  112. static const struct reset_control_ops jh7100_reset_ops = {
  113. .assert = jh7100_reset_assert,
  114. .deassert = jh7100_reset_deassert,
  115. .reset = jh7100_reset_reset,
  116. .status = jh7100_reset_status,
  117. };
  118. static int __init jh7100_reset_probe(struct platform_device *pdev)
  119. {
  120. struct jh7100_reset *data;
  121. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  122. if (!data)
  123. return -ENOMEM;
  124. data->base = devm_platform_ioremap_resource(pdev, 0);
  125. if (IS_ERR(data->base))
  126. return PTR_ERR(data->base);
  127. data->rcdev.ops = &jh7100_reset_ops;
  128. data->rcdev.owner = THIS_MODULE;
  129. data->rcdev.nr_resets = JH7100_RSTN_END;
  130. data->rcdev.dev = &pdev->dev;
  131. data->rcdev.of_node = pdev->dev.of_node;
  132. spin_lock_init(&data->lock);
  133. return devm_reset_controller_register(&pdev->dev, &data->rcdev);
  134. }
  135. static const struct of_device_id jh7100_reset_dt_ids[] = {
  136. { .compatible = "starfive,jh7100-reset" },
  137. { /* sentinel */ }
  138. };
  139. static struct platform_driver jh7100_reset_driver = {
  140. .driver = {
  141. .name = "jh7100-reset",
  142. .of_match_table = jh7100_reset_dt_ids,
  143. .suppress_bind_attrs = true,
  144. },
  145. };
  146. builtin_platform_driver_probe(jh7100_reset_driver, jh7100_reset_probe);