hi6220_reset.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Hisilicon Hi6220 reset controller driver
  4. *
  5. * Copyright (c) 2016 Linaro Limited.
  6. * Copyright (c) 2015-2016 HiSilicon Limited.
  7. *
  8. * Author: Feng Chen <puck.chen@hisilicon.com>
  9. */
  10. #include <linux/io.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/bitops.h>
  14. #include <linux/of.h>
  15. #include <linux/regmap.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/reset-controller.h>
  18. #include <linux/reset.h>
  19. #include <linux/platform_device.h>
  20. #define PERIPH_ASSERT_OFFSET 0x300
  21. #define PERIPH_DEASSERT_OFFSET 0x304
  22. #define PERIPH_MAX_INDEX 0x509
  23. #define SC_MEDIA_RSTEN 0x052C
  24. #define SC_MEDIA_RSTDIS 0x0530
  25. #define MEDIA_MAX_INDEX 8
  26. #define to_reset_data(x) container_of(x, struct hi6220_reset_data, rc_dev)
  27. enum hi6220_reset_ctrl_type {
  28. PERIPHERAL,
  29. MEDIA,
  30. AO,
  31. };
  32. struct hi6220_reset_data {
  33. struct reset_controller_dev rc_dev;
  34. struct regmap *regmap;
  35. };
  36. static int hi6220_peripheral_assert(struct reset_controller_dev *rc_dev,
  37. unsigned long idx)
  38. {
  39. struct hi6220_reset_data *data = to_reset_data(rc_dev);
  40. struct regmap *regmap = data->regmap;
  41. u32 bank = idx >> 8;
  42. u32 offset = idx & 0xff;
  43. u32 reg = PERIPH_ASSERT_OFFSET + bank * 0x10;
  44. return regmap_write(regmap, reg, BIT(offset));
  45. }
  46. static int hi6220_peripheral_deassert(struct reset_controller_dev *rc_dev,
  47. unsigned long idx)
  48. {
  49. struct hi6220_reset_data *data = to_reset_data(rc_dev);
  50. struct regmap *regmap = data->regmap;
  51. u32 bank = idx >> 8;
  52. u32 offset = idx & 0xff;
  53. u32 reg = PERIPH_DEASSERT_OFFSET + bank * 0x10;
  54. return regmap_write(regmap, reg, BIT(offset));
  55. }
  56. static const struct reset_control_ops hi6220_peripheral_reset_ops = {
  57. .assert = hi6220_peripheral_assert,
  58. .deassert = hi6220_peripheral_deassert,
  59. };
  60. static int hi6220_media_assert(struct reset_controller_dev *rc_dev,
  61. unsigned long idx)
  62. {
  63. struct hi6220_reset_data *data = to_reset_data(rc_dev);
  64. struct regmap *regmap = data->regmap;
  65. return regmap_write(regmap, SC_MEDIA_RSTEN, BIT(idx));
  66. }
  67. static int hi6220_media_deassert(struct reset_controller_dev *rc_dev,
  68. unsigned long idx)
  69. {
  70. struct hi6220_reset_data *data = to_reset_data(rc_dev);
  71. struct regmap *regmap = data->regmap;
  72. return regmap_write(regmap, SC_MEDIA_RSTDIS, BIT(idx));
  73. }
  74. static const struct reset_control_ops hi6220_media_reset_ops = {
  75. .assert = hi6220_media_assert,
  76. .deassert = hi6220_media_deassert,
  77. };
  78. #define AO_SCTRL_SC_PW_CLKEN0 0x800
  79. #define AO_SCTRL_SC_PW_CLKDIS0 0x804
  80. #define AO_SCTRL_SC_PW_RSTEN0 0x810
  81. #define AO_SCTRL_SC_PW_RSTDIS0 0x814
  82. #define AO_SCTRL_SC_PW_ISOEN0 0x820
  83. #define AO_SCTRL_SC_PW_ISODIS0 0x824
  84. #define AO_MAX_INDEX 12
  85. static int hi6220_ao_assert(struct reset_controller_dev *rc_dev,
  86. unsigned long idx)
  87. {
  88. struct hi6220_reset_data *data = to_reset_data(rc_dev);
  89. struct regmap *regmap = data->regmap;
  90. int ret;
  91. ret = regmap_write(regmap, AO_SCTRL_SC_PW_RSTEN0, BIT(idx));
  92. if (ret)
  93. return ret;
  94. ret = regmap_write(regmap, AO_SCTRL_SC_PW_ISOEN0, BIT(idx));
  95. if (ret)
  96. return ret;
  97. ret = regmap_write(regmap, AO_SCTRL_SC_PW_CLKDIS0, BIT(idx));
  98. return ret;
  99. }
  100. static int hi6220_ao_deassert(struct reset_controller_dev *rc_dev,
  101. unsigned long idx)
  102. {
  103. struct hi6220_reset_data *data = to_reset_data(rc_dev);
  104. struct regmap *regmap = data->regmap;
  105. int ret;
  106. /*
  107. * It was suggested to disable isolation before enabling
  108. * the clocks and deasserting reset, to avoid glitches.
  109. * But this order is preserved to keep it matching the
  110. * vendor code.
  111. */
  112. ret = regmap_write(regmap, AO_SCTRL_SC_PW_RSTDIS0, BIT(idx));
  113. if (ret)
  114. return ret;
  115. ret = regmap_write(regmap, AO_SCTRL_SC_PW_ISODIS0, BIT(idx));
  116. if (ret)
  117. return ret;
  118. ret = regmap_write(regmap, AO_SCTRL_SC_PW_CLKEN0, BIT(idx));
  119. return ret;
  120. }
  121. static const struct reset_control_ops hi6220_ao_reset_ops = {
  122. .assert = hi6220_ao_assert,
  123. .deassert = hi6220_ao_deassert,
  124. };
  125. static int hi6220_reset_probe(struct platform_device *pdev)
  126. {
  127. struct device_node *np = pdev->dev.of_node;
  128. struct device *dev = &pdev->dev;
  129. enum hi6220_reset_ctrl_type type;
  130. struct hi6220_reset_data *data;
  131. struct regmap *regmap;
  132. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  133. if (!data)
  134. return -ENOMEM;
  135. type = (uintptr_t)of_device_get_match_data(dev);
  136. regmap = syscon_node_to_regmap(np);
  137. if (IS_ERR(regmap)) {
  138. dev_err(dev, "failed to get reset controller regmap\n");
  139. return PTR_ERR(regmap);
  140. }
  141. data->regmap = regmap;
  142. data->rc_dev.of_node = np;
  143. if (type == MEDIA) {
  144. data->rc_dev.ops = &hi6220_media_reset_ops;
  145. data->rc_dev.nr_resets = MEDIA_MAX_INDEX;
  146. } else if (type == PERIPHERAL) {
  147. data->rc_dev.ops = &hi6220_peripheral_reset_ops;
  148. data->rc_dev.nr_resets = PERIPH_MAX_INDEX;
  149. } else {
  150. data->rc_dev.ops = &hi6220_ao_reset_ops;
  151. data->rc_dev.nr_resets = AO_MAX_INDEX;
  152. }
  153. return reset_controller_register(&data->rc_dev);
  154. }
  155. static const struct of_device_id hi6220_reset_match[] = {
  156. {
  157. .compatible = "hisilicon,hi6220-sysctrl",
  158. .data = (void *)PERIPHERAL,
  159. },
  160. {
  161. .compatible = "hisilicon,hi6220-mediactrl",
  162. .data = (void *)MEDIA,
  163. },
  164. {
  165. .compatible = "hisilicon,hi6220-aoctrl",
  166. .data = (void *)AO,
  167. },
  168. { /* sentinel */ },
  169. };
  170. MODULE_DEVICE_TABLE(of, hi6220_reset_match);
  171. static struct platform_driver hi6220_reset_driver = {
  172. .probe = hi6220_reset_probe,
  173. .driver = {
  174. .name = "reset-hi6220",
  175. .of_match_table = hi6220_reset_match,
  176. },
  177. };
  178. static int __init hi6220_reset_init(void)
  179. {
  180. return platform_driver_register(&hi6220_reset_driver);
  181. }
  182. postcore_initcall(hi6220_reset_init);
  183. MODULE_DESCRIPTION("Hisilicon Hi6220 reset controller driver");
  184. MODULE_LICENSE("GPL v2");