reset-ath79.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * AR71xx Reset Controller Driver
  4. * Author: Alban Bedel
  5. *
  6. * Copyright (C) 2015 Alban Bedel <albeu@free.fr>
  7. */
  8. #include <linux/io.h>
  9. #include <linux/init.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/reset-controller.h>
  13. #include <linux/reboot.h>
  14. struct ath79_reset {
  15. struct reset_controller_dev rcdev;
  16. struct notifier_block restart_nb;
  17. void __iomem *base;
  18. spinlock_t lock;
  19. };
  20. #define FULL_CHIP_RESET 24
  21. static int ath79_reset_update(struct reset_controller_dev *rcdev,
  22. unsigned long id, bool assert)
  23. {
  24. struct ath79_reset *ath79_reset =
  25. container_of(rcdev, struct ath79_reset, rcdev);
  26. unsigned long flags;
  27. u32 val;
  28. spin_lock_irqsave(&ath79_reset->lock, flags);
  29. val = readl(ath79_reset->base);
  30. if (assert)
  31. val |= BIT(id);
  32. else
  33. val &= ~BIT(id);
  34. writel(val, ath79_reset->base);
  35. spin_unlock_irqrestore(&ath79_reset->lock, flags);
  36. return 0;
  37. }
  38. static int ath79_reset_assert(struct reset_controller_dev *rcdev,
  39. unsigned long id)
  40. {
  41. return ath79_reset_update(rcdev, id, true);
  42. }
  43. static int ath79_reset_deassert(struct reset_controller_dev *rcdev,
  44. unsigned long id)
  45. {
  46. return ath79_reset_update(rcdev, id, false);
  47. }
  48. static int ath79_reset_status(struct reset_controller_dev *rcdev,
  49. unsigned long id)
  50. {
  51. struct ath79_reset *ath79_reset =
  52. container_of(rcdev, struct ath79_reset, rcdev);
  53. u32 val;
  54. val = readl(ath79_reset->base);
  55. return !!(val & BIT(id));
  56. }
  57. static const struct reset_control_ops ath79_reset_ops = {
  58. .assert = ath79_reset_assert,
  59. .deassert = ath79_reset_deassert,
  60. .status = ath79_reset_status,
  61. };
  62. static int ath79_reset_restart_handler(struct notifier_block *nb,
  63. unsigned long action, void *data)
  64. {
  65. struct ath79_reset *ath79_reset =
  66. container_of(nb, struct ath79_reset, restart_nb);
  67. ath79_reset_assert(&ath79_reset->rcdev, FULL_CHIP_RESET);
  68. return NOTIFY_DONE;
  69. }
  70. static int ath79_reset_probe(struct platform_device *pdev)
  71. {
  72. struct ath79_reset *ath79_reset;
  73. struct resource *res;
  74. int err;
  75. ath79_reset = devm_kzalloc(&pdev->dev,
  76. sizeof(*ath79_reset), GFP_KERNEL);
  77. if (!ath79_reset)
  78. return -ENOMEM;
  79. platform_set_drvdata(pdev, ath79_reset);
  80. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  81. ath79_reset->base = devm_ioremap_resource(&pdev->dev, res);
  82. if (IS_ERR(ath79_reset->base))
  83. return PTR_ERR(ath79_reset->base);
  84. spin_lock_init(&ath79_reset->lock);
  85. ath79_reset->rcdev.ops = &ath79_reset_ops;
  86. ath79_reset->rcdev.owner = THIS_MODULE;
  87. ath79_reset->rcdev.of_node = pdev->dev.of_node;
  88. ath79_reset->rcdev.of_reset_n_cells = 1;
  89. ath79_reset->rcdev.nr_resets = 32;
  90. err = devm_reset_controller_register(&pdev->dev, &ath79_reset->rcdev);
  91. if (err)
  92. return err;
  93. ath79_reset->restart_nb.notifier_call = ath79_reset_restart_handler;
  94. ath79_reset->restart_nb.priority = 128;
  95. err = register_restart_handler(&ath79_reset->restart_nb);
  96. if (err)
  97. dev_warn(&pdev->dev, "Failed to register restart handler\n");
  98. return 0;
  99. }
  100. static const struct of_device_id ath79_reset_dt_ids[] = {
  101. { .compatible = "qca,ar7100-reset", },
  102. { },
  103. };
  104. static struct platform_driver ath79_reset_driver = {
  105. .probe = ath79_reset_probe,
  106. .driver = {
  107. .name = "ath79-reset",
  108. .of_match_table = ath79_reset_dt_ids,
  109. .suppress_bind_attrs = true,
  110. },
  111. };
  112. builtin_platform_driver(ath79_reset_driver);