reset-syscfg.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013 STMicroelectronics Limited
  4. * Author: Stephen Gallimore <[email protected]>
  5. *
  6. * Inspired by mach-imx/src.c
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/module.h>
  11. #include <linux/err.h>
  12. #include <linux/types.h>
  13. #include <linux/of_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/mfd/syscon.h>
  16. #include "reset-syscfg.h"
  17. /**
  18. * struct syscfg_reset_channel - Reset channel regmap configuration
  19. *
  20. * @reset: regmap field for the channel's reset bit.
  21. * @ack: regmap field for the channel's ack bit (optional).
  22. */
  23. struct syscfg_reset_channel {
  24. struct regmap_field *reset;
  25. struct regmap_field *ack;
  26. };
  27. /**
  28. * struct syscfg_reset_controller - A reset controller which groups together
  29. * a set of related reset bits, which may be located in different system
  30. * configuration registers.
  31. *
  32. * @rst: base reset controller structure.
  33. * @active_low: are the resets in this controller active low, i.e. clearing
  34. * the reset bit puts the hardware into reset.
  35. * @channels: An array of reset channels for this controller.
  36. */
  37. struct syscfg_reset_controller {
  38. struct reset_controller_dev rst;
  39. bool active_low;
  40. struct syscfg_reset_channel *channels;
  41. };
  42. #define to_syscfg_reset_controller(_rst) \
  43. container_of(_rst, struct syscfg_reset_controller, rst)
  44. static int syscfg_reset_program_hw(struct reset_controller_dev *rcdev,
  45. unsigned long idx, int assert)
  46. {
  47. struct syscfg_reset_controller *rst = to_syscfg_reset_controller(rcdev);
  48. const struct syscfg_reset_channel *ch;
  49. u32 ctrl_val = rst->active_low ? !assert : !!assert;
  50. int err;
  51. if (idx >= rcdev->nr_resets)
  52. return -EINVAL;
  53. ch = &rst->channels[idx];
  54. err = regmap_field_write(ch->reset, ctrl_val);
  55. if (err)
  56. return err;
  57. if (ch->ack) {
  58. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  59. u32 ack_val;
  60. while (true) {
  61. err = regmap_field_read(ch->ack, &ack_val);
  62. if (err)
  63. return err;
  64. if (ack_val == ctrl_val)
  65. break;
  66. if (time_after(jiffies, timeout))
  67. return -ETIME;
  68. cpu_relax();
  69. }
  70. }
  71. return 0;
  72. }
  73. static int syscfg_reset_assert(struct reset_controller_dev *rcdev,
  74. unsigned long idx)
  75. {
  76. return syscfg_reset_program_hw(rcdev, idx, true);
  77. }
  78. static int syscfg_reset_deassert(struct reset_controller_dev *rcdev,
  79. unsigned long idx)
  80. {
  81. return syscfg_reset_program_hw(rcdev, idx, false);
  82. }
  83. static int syscfg_reset_dev(struct reset_controller_dev *rcdev,
  84. unsigned long idx)
  85. {
  86. int err;
  87. err = syscfg_reset_assert(rcdev, idx);
  88. if (err)
  89. return err;
  90. return syscfg_reset_deassert(rcdev, idx);
  91. }
  92. static int syscfg_reset_status(struct reset_controller_dev *rcdev,
  93. unsigned long idx)
  94. {
  95. struct syscfg_reset_controller *rst = to_syscfg_reset_controller(rcdev);
  96. const struct syscfg_reset_channel *ch;
  97. u32 ret_val = 0;
  98. int err;
  99. if (idx >= rcdev->nr_resets)
  100. return -EINVAL;
  101. ch = &rst->channels[idx];
  102. if (ch->ack)
  103. err = regmap_field_read(ch->ack, &ret_val);
  104. else
  105. err = regmap_field_read(ch->reset, &ret_val);
  106. if (err)
  107. return err;
  108. return rst->active_low ? !ret_val : !!ret_val;
  109. }
  110. static const struct reset_control_ops syscfg_reset_ops = {
  111. .reset = syscfg_reset_dev,
  112. .assert = syscfg_reset_assert,
  113. .deassert = syscfg_reset_deassert,
  114. .status = syscfg_reset_status,
  115. };
  116. static int syscfg_reset_controller_register(struct device *dev,
  117. const struct syscfg_reset_controller_data *data)
  118. {
  119. struct syscfg_reset_controller *rc;
  120. int i, err;
  121. rc = devm_kzalloc(dev, sizeof(*rc), GFP_KERNEL);
  122. if (!rc)
  123. return -ENOMEM;
  124. rc->channels = devm_kcalloc(dev, data->nr_channels,
  125. sizeof(*rc->channels), GFP_KERNEL);
  126. if (!rc->channels)
  127. return -ENOMEM;
  128. rc->rst.ops = &syscfg_reset_ops;
  129. rc->rst.of_node = dev->of_node;
  130. rc->rst.nr_resets = data->nr_channels;
  131. rc->active_low = data->active_low;
  132. for (i = 0; i < data->nr_channels; i++) {
  133. struct regmap *map;
  134. struct regmap_field *f;
  135. const char *compatible = data->channels[i].compatible;
  136. map = syscon_regmap_lookup_by_compatible(compatible);
  137. if (IS_ERR(map))
  138. return PTR_ERR(map);
  139. f = devm_regmap_field_alloc(dev, map, data->channels[i].reset);
  140. if (IS_ERR(f))
  141. return PTR_ERR(f);
  142. rc->channels[i].reset = f;
  143. if (!data->wait_for_ack)
  144. continue;
  145. f = devm_regmap_field_alloc(dev, map, data->channels[i].ack);
  146. if (IS_ERR(f))
  147. return PTR_ERR(f);
  148. rc->channels[i].ack = f;
  149. }
  150. err = reset_controller_register(&rc->rst);
  151. if (!err)
  152. dev_info(dev, "registered\n");
  153. return err;
  154. }
  155. int syscfg_reset_probe(struct platform_device *pdev)
  156. {
  157. struct device *dev = pdev ? &pdev->dev : NULL;
  158. const struct of_device_id *match;
  159. if (!dev || !dev->driver)
  160. return -ENODEV;
  161. match = of_match_device(dev->driver->of_match_table, dev);
  162. if (!match || !match->data)
  163. return -EINVAL;
  164. return syscfg_reset_controller_register(dev, match->data);
  165. }