reset-ath79.c 3.7 KB

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