reset-lantiq.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2010 John Crispin <blogic@phrozen.org>
  7. * Copyright (C) 2013-2015 Lantiq Beteiligungs-GmbH & Co.KG
  8. * Copyright (C) 2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
  9. * Copyright (C) 2017 Hauke Mehrtens <hauke@hauke-m.de>
  10. */
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/module.h>
  13. #include <linux/regmap.h>
  14. #include <linux/reset-controller.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/property.h>
  19. #define LANTIQ_RCU_RESET_TIMEOUT 10000
  20. struct lantiq_rcu_reset_priv {
  21. struct reset_controller_dev rcdev;
  22. struct device *dev;
  23. struct regmap *regmap;
  24. u32 reset_offset;
  25. u32 status_offset;
  26. };
  27. static struct lantiq_rcu_reset_priv *to_lantiq_rcu_reset_priv(
  28. struct reset_controller_dev *rcdev)
  29. {
  30. return container_of(rcdev, struct lantiq_rcu_reset_priv, rcdev);
  31. }
  32. static int lantiq_rcu_reset_status(struct reset_controller_dev *rcdev,
  33. unsigned long id)
  34. {
  35. struct lantiq_rcu_reset_priv *priv = to_lantiq_rcu_reset_priv(rcdev);
  36. unsigned int status = (id >> 8) & 0x1f;
  37. u32 val;
  38. int ret;
  39. ret = regmap_read(priv->regmap, priv->status_offset, &val);
  40. if (ret)
  41. return ret;
  42. return !!(val & BIT(status));
  43. }
  44. static int lantiq_rcu_reset_status_timeout(struct reset_controller_dev *rcdev,
  45. unsigned long id, bool assert)
  46. {
  47. int ret;
  48. int retry = LANTIQ_RCU_RESET_TIMEOUT;
  49. do {
  50. ret = lantiq_rcu_reset_status(rcdev, id);
  51. if (ret < 0)
  52. return ret;
  53. if (ret == assert)
  54. return 0;
  55. usleep_range(20, 40);
  56. } while (--retry);
  57. return -ETIMEDOUT;
  58. }
  59. static int lantiq_rcu_reset_update(struct reset_controller_dev *rcdev,
  60. unsigned long id, bool assert)
  61. {
  62. struct lantiq_rcu_reset_priv *priv = to_lantiq_rcu_reset_priv(rcdev);
  63. unsigned int set = id & 0x1f;
  64. u32 val = assert ? BIT(set) : 0;
  65. int ret;
  66. ret = regmap_update_bits(priv->regmap, priv->reset_offset, BIT(set),
  67. val);
  68. if (ret) {
  69. dev_err(priv->dev, "Failed to set reset bit %u\n", set);
  70. return ret;
  71. }
  72. ret = lantiq_rcu_reset_status_timeout(rcdev, id, assert);
  73. if (ret)
  74. dev_err(priv->dev, "Failed to %s bit %u\n",
  75. assert ? "assert" : "deassert", set);
  76. return ret;
  77. }
  78. static int lantiq_rcu_reset_assert(struct reset_controller_dev *rcdev,
  79. unsigned long id)
  80. {
  81. return lantiq_rcu_reset_update(rcdev, id, true);
  82. }
  83. static int lantiq_rcu_reset_deassert(struct reset_controller_dev *rcdev,
  84. unsigned long id)
  85. {
  86. return lantiq_rcu_reset_update(rcdev, id, false);
  87. }
  88. static int lantiq_rcu_reset_reset(struct reset_controller_dev *rcdev,
  89. unsigned long id)
  90. {
  91. int ret;
  92. ret = lantiq_rcu_reset_assert(rcdev, id);
  93. if (ret)
  94. return ret;
  95. return lantiq_rcu_reset_deassert(rcdev, id);
  96. }
  97. static const struct reset_control_ops lantiq_rcu_reset_ops = {
  98. .assert = lantiq_rcu_reset_assert,
  99. .deassert = lantiq_rcu_reset_deassert,
  100. .status = lantiq_rcu_reset_status,
  101. .reset = lantiq_rcu_reset_reset,
  102. };
  103. static int lantiq_rcu_reset_of_parse(struct platform_device *pdev,
  104. struct lantiq_rcu_reset_priv *priv)
  105. {
  106. struct device *dev = &pdev->dev;
  107. const __be32 *offset;
  108. priv->regmap = syscon_node_to_regmap(dev->of_node->parent);
  109. if (IS_ERR(priv->regmap)) {
  110. dev_err(&pdev->dev, "Failed to lookup RCU regmap\n");
  111. return PTR_ERR(priv->regmap);
  112. }
  113. offset = of_get_address(dev->of_node, 0, NULL, NULL);
  114. if (!offset) {
  115. dev_err(&pdev->dev, "Failed to get RCU reset offset\n");
  116. return -ENOENT;
  117. }
  118. priv->reset_offset = __be32_to_cpu(*offset);
  119. offset = of_get_address(dev->of_node, 1, NULL, NULL);
  120. if (!offset) {
  121. dev_err(&pdev->dev, "Failed to get RCU status offset\n");
  122. return -ENOENT;
  123. }
  124. priv->status_offset = __be32_to_cpu(*offset);
  125. return 0;
  126. }
  127. static int lantiq_rcu_reset_xlate(struct reset_controller_dev *rcdev,
  128. const struct of_phandle_args *reset_spec)
  129. {
  130. unsigned int status, set;
  131. set = reset_spec->args[0];
  132. status = reset_spec->args[1];
  133. if (set >= rcdev->nr_resets || status >= rcdev->nr_resets)
  134. return -EINVAL;
  135. return (status << 8) | set;
  136. }
  137. static int lantiq_rcu_reset_probe(struct platform_device *pdev)
  138. {
  139. struct lantiq_rcu_reset_priv *priv;
  140. int err;
  141. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  142. if (!priv)
  143. return -ENOMEM;
  144. priv->dev = &pdev->dev;
  145. platform_set_drvdata(pdev, priv);
  146. err = lantiq_rcu_reset_of_parse(pdev, priv);
  147. if (err)
  148. return err;
  149. priv->rcdev.ops = &lantiq_rcu_reset_ops;
  150. priv->rcdev.owner = THIS_MODULE;
  151. priv->rcdev.of_node = pdev->dev.of_node;
  152. priv->rcdev.nr_resets = 32;
  153. priv->rcdev.of_xlate = lantiq_rcu_reset_xlate;
  154. priv->rcdev.of_reset_n_cells = 2;
  155. return reset_controller_register(&priv->rcdev);
  156. }
  157. static const struct of_device_id lantiq_rcu_reset_dt_ids[] = {
  158. { .compatible = "lantiq,danube-reset", },
  159. { .compatible = "lantiq,xrx200-reset", },
  160. { },
  161. };
  162. MODULE_DEVICE_TABLE(of, lantiq_rcu_reset_dt_ids);
  163. static struct platform_driver lantiq_rcu_reset_driver = {
  164. .probe = lantiq_rcu_reset_probe,
  165. .driver = {
  166. .name = "lantiq-reset",
  167. .of_match_table = lantiq_rcu_reset_dt_ids,
  168. },
  169. };
  170. module_platform_driver(lantiq_rcu_reset_driver);
  171. MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
  172. MODULE_DESCRIPTION("Lantiq XWAY RCU Reset Controller Driver");
  173. MODULE_LICENSE("GPL");