reset-syscfg.c 4.4 KB

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