reset-a10sr.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright Intel Corporation (C) 2017. All Rights Reserved
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Reset driver for Altera Arria10 MAX5 System Resource Chip
  17. *
  18. * Adapted from reset-socfpga.c
  19. */
  20. #include <linux/err.h>
  21. #include <linux/mfd/altera-a10sr.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/reset-controller.h>
  26. #include <dt-bindings/reset/altr,rst-mgr-a10sr.h>
  27. struct a10sr_reset {
  28. struct reset_controller_dev rcdev;
  29. struct regmap *regmap;
  30. };
  31. static inline struct a10sr_reset *to_a10sr_rst(struct reset_controller_dev *rc)
  32. {
  33. return container_of(rc, struct a10sr_reset, rcdev);
  34. }
  35. static inline int a10sr_reset_shift(unsigned long id)
  36. {
  37. switch (id) {
  38. case A10SR_RESET_ENET_HPS:
  39. return 1;
  40. case A10SR_RESET_PCIE:
  41. case A10SR_RESET_FILE:
  42. case A10SR_RESET_BQSPI:
  43. case A10SR_RESET_USB:
  44. return id + 11;
  45. default:
  46. return -EINVAL;
  47. }
  48. }
  49. static int a10sr_reset_update(struct reset_controller_dev *rcdev,
  50. unsigned long id, bool assert)
  51. {
  52. struct a10sr_reset *a10r = to_a10sr_rst(rcdev);
  53. int offset = a10sr_reset_shift(id);
  54. u8 mask = ALTR_A10SR_REG_BIT_MASK(offset);
  55. int index = ALTR_A10SR_HPS_RST_REG + ALTR_A10SR_REG_OFFSET(offset);
  56. return regmap_update_bits(a10r->regmap, index, mask, assert ? 0 : mask);
  57. }
  58. static int a10sr_reset_assert(struct reset_controller_dev *rcdev,
  59. unsigned long id)
  60. {
  61. return a10sr_reset_update(rcdev, id, true);
  62. }
  63. static int a10sr_reset_deassert(struct reset_controller_dev *rcdev,
  64. unsigned long id)
  65. {
  66. return a10sr_reset_update(rcdev, id, false);
  67. }
  68. static int a10sr_reset_status(struct reset_controller_dev *rcdev,
  69. unsigned long id)
  70. {
  71. int ret;
  72. struct a10sr_reset *a10r = to_a10sr_rst(rcdev);
  73. int offset = a10sr_reset_shift(id);
  74. u8 mask = ALTR_A10SR_REG_BIT_MASK(offset);
  75. int index = ALTR_A10SR_HPS_RST_REG + ALTR_A10SR_REG_OFFSET(offset);
  76. unsigned int value;
  77. ret = regmap_read(a10r->regmap, index, &value);
  78. if (ret < 0)
  79. return ret;
  80. return !!(value & mask);
  81. }
  82. static const struct reset_control_ops a10sr_reset_ops = {
  83. .assert = a10sr_reset_assert,
  84. .deassert = a10sr_reset_deassert,
  85. .status = a10sr_reset_status,
  86. };
  87. static int a10sr_reset_probe(struct platform_device *pdev)
  88. {
  89. struct altr_a10sr *a10sr = dev_get_drvdata(pdev->dev.parent);
  90. struct a10sr_reset *a10r;
  91. a10r = devm_kzalloc(&pdev->dev, sizeof(struct a10sr_reset),
  92. GFP_KERNEL);
  93. if (!a10r)
  94. return -ENOMEM;
  95. a10r->rcdev.owner = THIS_MODULE;
  96. a10r->rcdev.nr_resets = A10SR_RESET_NUM;
  97. a10r->rcdev.ops = &a10sr_reset_ops;
  98. a10r->rcdev.of_node = pdev->dev.of_node;
  99. a10r->regmap = a10sr->regmap;
  100. platform_set_drvdata(pdev, a10r);
  101. return devm_reset_controller_register(&pdev->dev, &a10r->rcdev);
  102. }
  103. static const struct of_device_id a10sr_reset_of_match[] = {
  104. { .compatible = "altr,a10sr-reset" },
  105. { },
  106. };
  107. MODULE_DEVICE_TABLE(of, a10sr_reset_of_match);
  108. static struct platform_driver a10sr_reset_driver = {
  109. .probe = a10sr_reset_probe,
  110. .driver = {
  111. .name = "altr_a10sr_reset",
  112. .of_match_table = a10sr_reset_of_match,
  113. },
  114. };
  115. module_platform_driver(a10sr_reset_driver);
  116. MODULE_AUTHOR("Thor Thayer <thor.thayer@linux.intel.com>");
  117. MODULE_DESCRIPTION("Altera Arria10 System Resource Reset Controller Driver");
  118. MODULE_LICENSE("GPL v2");