reset-zynq.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2015, National Instruments Corp.
  3. *
  4. * Xilinx Zynq Reset controller driver
  5. *
  6. * Author: Moritz Fischer <moritz.fischer@ettus.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/init.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/reset-controller.h>
  24. #include <linux/regmap.h>
  25. #include <linux/types.h>
  26. struct zynq_reset_data {
  27. struct regmap *slcr;
  28. struct reset_controller_dev rcdev;
  29. u32 offset;
  30. };
  31. #define to_zynq_reset_data(p) \
  32. container_of((p), struct zynq_reset_data, rcdev)
  33. static int zynq_reset_assert(struct reset_controller_dev *rcdev,
  34. unsigned long id)
  35. {
  36. struct zynq_reset_data *priv = to_zynq_reset_data(rcdev);
  37. int bank = id / BITS_PER_LONG;
  38. int offset = id % BITS_PER_LONG;
  39. pr_debug("%s: %s reset bank %u offset %u\n", KBUILD_MODNAME, __func__,
  40. bank, offset);
  41. return regmap_update_bits(priv->slcr,
  42. priv->offset + (bank * 4),
  43. BIT(offset),
  44. BIT(offset));
  45. }
  46. static int zynq_reset_deassert(struct reset_controller_dev *rcdev,
  47. unsigned long id)
  48. {
  49. struct zynq_reset_data *priv = to_zynq_reset_data(rcdev);
  50. int bank = id / BITS_PER_LONG;
  51. int offset = id % BITS_PER_LONG;
  52. pr_debug("%s: %s reset bank %u offset %u\n", KBUILD_MODNAME, __func__,
  53. bank, offset);
  54. return regmap_update_bits(priv->slcr,
  55. priv->offset + (bank * 4),
  56. BIT(offset),
  57. ~BIT(offset));
  58. }
  59. static int zynq_reset_status(struct reset_controller_dev *rcdev,
  60. unsigned long id)
  61. {
  62. struct zynq_reset_data *priv = to_zynq_reset_data(rcdev);
  63. int bank = id / BITS_PER_LONG;
  64. int offset = id % BITS_PER_LONG;
  65. int ret;
  66. u32 reg;
  67. pr_debug("%s: %s reset bank %u offset %u\n", KBUILD_MODNAME, __func__,
  68. bank, offset);
  69. ret = regmap_read(priv->slcr, priv->offset + (bank * 4), &reg);
  70. if (ret)
  71. return ret;
  72. return !!(reg & BIT(offset));
  73. }
  74. static const struct reset_control_ops zynq_reset_ops = {
  75. .assert = zynq_reset_assert,
  76. .deassert = zynq_reset_deassert,
  77. .status = zynq_reset_status,
  78. };
  79. static int zynq_reset_probe(struct platform_device *pdev)
  80. {
  81. struct resource *res;
  82. struct zynq_reset_data *priv;
  83. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  84. if (!priv)
  85. return -ENOMEM;
  86. platform_set_drvdata(pdev, priv);
  87. priv->slcr = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  88. "syscon");
  89. if (IS_ERR(priv->slcr)) {
  90. dev_err(&pdev->dev, "unable to get zynq-slcr regmap");
  91. return PTR_ERR(priv->slcr);
  92. }
  93. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  94. if (!res) {
  95. dev_err(&pdev->dev, "missing IO resource\n");
  96. return -ENODEV;
  97. }
  98. priv->offset = res->start;
  99. priv->rcdev.owner = THIS_MODULE;
  100. priv->rcdev.nr_resets = resource_size(res) / 4 * BITS_PER_LONG;
  101. priv->rcdev.ops = &zynq_reset_ops;
  102. priv->rcdev.of_node = pdev->dev.of_node;
  103. return devm_reset_controller_register(&pdev->dev, &priv->rcdev);
  104. }
  105. static const struct of_device_id zynq_reset_dt_ids[] = {
  106. { .compatible = "xlnx,zynq-reset", },
  107. { /* sentinel */ },
  108. };
  109. static struct platform_driver zynq_reset_driver = {
  110. .probe = zynq_reset_probe,
  111. .driver = {
  112. .name = KBUILD_MODNAME,
  113. .of_match_table = zynq_reset_dt_ids,
  114. },
  115. };
  116. builtin_platform_driver(zynq_reset_driver);