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