reset-ath79.c 3.7 KB

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