hi6220_reset.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Hisilicon Hi6220 reset controller driver
  3. *
  4. * Copyright (c) 2016 Linaro Limited.
  5. * Copyright (c) 2015-2016 Hisilicon Limited.
  6. *
  7. * Author: Feng Chen <puck.chen@hisilicon.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/io.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/bitops.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/reset-controller.h>
  22. #include <linux/reset.h>
  23. #include <linux/platform_device.h>
  24. #define PERIPH_ASSERT_OFFSET 0x300
  25. #define PERIPH_DEASSERT_OFFSET 0x304
  26. #define PERIPH_MAX_INDEX 0x509
  27. #define SC_MEDIA_RSTEN 0x052C
  28. #define SC_MEDIA_RSTDIS 0x0530
  29. #define MEDIA_MAX_INDEX 8
  30. #define to_reset_data(x) container_of(x, struct hi6220_reset_data, rc_dev)
  31. enum hi6220_reset_ctrl_type {
  32. PERIPHERAL,
  33. MEDIA,
  34. };
  35. struct hi6220_reset_data {
  36. struct reset_controller_dev rc_dev;
  37. struct regmap *regmap;
  38. };
  39. static int hi6220_peripheral_assert(struct reset_controller_dev *rc_dev,
  40. unsigned long idx)
  41. {
  42. struct hi6220_reset_data *data = to_reset_data(rc_dev);
  43. struct regmap *regmap = data->regmap;
  44. u32 bank = idx >> 8;
  45. u32 offset = idx & 0xff;
  46. u32 reg = PERIPH_ASSERT_OFFSET + bank * 0x10;
  47. return regmap_write(regmap, reg, BIT(offset));
  48. }
  49. static int hi6220_peripheral_deassert(struct reset_controller_dev *rc_dev,
  50. unsigned long idx)
  51. {
  52. struct hi6220_reset_data *data = to_reset_data(rc_dev);
  53. struct regmap *regmap = data->regmap;
  54. u32 bank = idx >> 8;
  55. u32 offset = idx & 0xff;
  56. u32 reg = PERIPH_DEASSERT_OFFSET + bank * 0x10;
  57. return regmap_write(regmap, reg, BIT(offset));
  58. }
  59. static const struct reset_control_ops hi6220_peripheral_reset_ops = {
  60. .assert = hi6220_peripheral_assert,
  61. .deassert = hi6220_peripheral_deassert,
  62. };
  63. static int hi6220_media_assert(struct reset_controller_dev *rc_dev,
  64. unsigned long idx)
  65. {
  66. struct hi6220_reset_data *data = to_reset_data(rc_dev);
  67. struct regmap *regmap = data->regmap;
  68. return regmap_write(regmap, SC_MEDIA_RSTEN, BIT(idx));
  69. }
  70. static int hi6220_media_deassert(struct reset_controller_dev *rc_dev,
  71. unsigned long idx)
  72. {
  73. struct hi6220_reset_data *data = to_reset_data(rc_dev);
  74. struct regmap *regmap = data->regmap;
  75. return regmap_write(regmap, SC_MEDIA_RSTDIS, BIT(idx));
  76. }
  77. static const struct reset_control_ops hi6220_media_reset_ops = {
  78. .assert = hi6220_media_assert,
  79. .deassert = hi6220_media_deassert,
  80. };
  81. static int hi6220_reset_probe(struct platform_device *pdev)
  82. {
  83. struct device_node *np = pdev->dev.of_node;
  84. struct device *dev = &pdev->dev;
  85. enum hi6220_reset_ctrl_type type;
  86. struct hi6220_reset_data *data;
  87. struct regmap *regmap;
  88. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  89. if (!data)
  90. return -ENOMEM;
  91. type = (enum hi6220_reset_ctrl_type)of_device_get_match_data(dev);
  92. regmap = syscon_node_to_regmap(np);
  93. if (IS_ERR(regmap)) {
  94. dev_err(dev, "failed to get reset controller regmap\n");
  95. return PTR_ERR(regmap);
  96. }
  97. data->regmap = regmap;
  98. data->rc_dev.of_node = np;
  99. if (type == MEDIA) {
  100. data->rc_dev.ops = &hi6220_media_reset_ops;
  101. data->rc_dev.nr_resets = MEDIA_MAX_INDEX;
  102. } else {
  103. data->rc_dev.ops = &hi6220_peripheral_reset_ops;
  104. data->rc_dev.nr_resets = PERIPH_MAX_INDEX;
  105. }
  106. return reset_controller_register(&data->rc_dev);
  107. }
  108. static const struct of_device_id hi6220_reset_match[] = {
  109. {
  110. .compatible = "hisilicon,hi6220-sysctrl",
  111. .data = (void *)PERIPHERAL,
  112. },
  113. {
  114. .compatible = "hisilicon,hi6220-mediactrl",
  115. .data = (void *)MEDIA,
  116. },
  117. { /* sentinel */ },
  118. };
  119. MODULE_DEVICE_TABLE(of, hi6220_reset_match);
  120. static struct platform_driver hi6220_reset_driver = {
  121. .probe = hi6220_reset_probe,
  122. .driver = {
  123. .name = "reset-hi6220",
  124. .of_match_table = hi6220_reset_match,
  125. },
  126. };
  127. static int __init hi6220_reset_init(void)
  128. {
  129. return platform_driver_register(&hi6220_reset_driver);
  130. }
  131. postcore_initcall(hi6220_reset_init);