reset-ath79.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2015 Alban Bedel <albeu@free.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/reset-controller.h>
  17. #include <linux/reboot.h>
  18. struct ath79_reset {
  19. struct reset_controller_dev rcdev;
  20. struct notifier_block restart_nb;
  21. void __iomem *base;
  22. spinlock_t lock;
  23. };
  24. #define FULL_CHIP_RESET 24
  25. static int ath79_reset_update(struct reset_controller_dev *rcdev,
  26. unsigned long id, bool assert)
  27. {
  28. struct ath79_reset *ath79_reset =
  29. container_of(rcdev, struct ath79_reset, rcdev);
  30. unsigned long flags;
  31. u32 val;
  32. spin_lock_irqsave(&ath79_reset->lock, flags);
  33. val = readl(ath79_reset->base);
  34. if (assert)
  35. val |= BIT(id);
  36. else
  37. val &= ~BIT(id);
  38. writel(val, ath79_reset->base);
  39. spin_unlock_irqrestore(&ath79_reset->lock, flags);
  40. return 0;
  41. }
  42. static int ath79_reset_assert(struct reset_controller_dev *rcdev,
  43. unsigned long id)
  44. {
  45. return ath79_reset_update(rcdev, id, true);
  46. }
  47. static int ath79_reset_deassert(struct reset_controller_dev *rcdev,
  48. unsigned long id)
  49. {
  50. return ath79_reset_update(rcdev, id, false);
  51. }
  52. static int ath79_reset_status(struct reset_controller_dev *rcdev,
  53. unsigned long id)
  54. {
  55. struct ath79_reset *ath79_reset =
  56. container_of(rcdev, struct ath79_reset, rcdev);
  57. u32 val;
  58. val = readl(ath79_reset->base);
  59. return !!(val & BIT(id));
  60. }
  61. static const struct reset_control_ops ath79_reset_ops = {
  62. .assert = ath79_reset_assert,
  63. .deassert = ath79_reset_deassert,
  64. .status = ath79_reset_status,
  65. };
  66. static int ath79_reset_restart_handler(struct notifier_block *nb,
  67. unsigned long action, void *data)
  68. {
  69. struct ath79_reset *ath79_reset =
  70. container_of(nb, struct ath79_reset, restart_nb);
  71. ath79_reset_assert(&ath79_reset->rcdev, FULL_CHIP_RESET);
  72. return NOTIFY_DONE;
  73. }
  74. static int ath79_reset_probe(struct platform_device *pdev)
  75. {
  76. struct ath79_reset *ath79_reset;
  77. struct resource *res;
  78. int err;
  79. ath79_reset = devm_kzalloc(&pdev->dev,
  80. sizeof(*ath79_reset), GFP_KERNEL);
  81. if (!ath79_reset)
  82. return -ENOMEM;
  83. platform_set_drvdata(pdev, ath79_reset);
  84. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  85. ath79_reset->base = devm_ioremap_resource(&pdev->dev, res);
  86. if (IS_ERR(ath79_reset->base))
  87. return PTR_ERR(ath79_reset->base);
  88. spin_lock_init(&ath79_reset->lock);
  89. ath79_reset->rcdev.ops = &ath79_reset_ops;
  90. ath79_reset->rcdev.owner = THIS_MODULE;
  91. ath79_reset->rcdev.of_node = pdev->dev.of_node;
  92. ath79_reset->rcdev.of_reset_n_cells = 1;
  93. ath79_reset->rcdev.nr_resets = 32;
  94. err = reset_controller_register(&ath79_reset->rcdev);
  95. if (err)
  96. return err;
  97. ath79_reset->restart_nb.notifier_call = ath79_reset_restart_handler;
  98. ath79_reset->restart_nb.priority = 128;
  99. err = register_restart_handler(&ath79_reset->restart_nb);
  100. if (err)
  101. dev_warn(&pdev->dev, "Failed to register restart handler\n");
  102. return 0;
  103. }
  104. static int ath79_reset_remove(struct platform_device *pdev)
  105. {
  106. struct ath79_reset *ath79_reset = platform_get_drvdata(pdev);
  107. unregister_restart_handler(&ath79_reset->restart_nb);
  108. reset_controller_unregister(&ath79_reset->rcdev);
  109. return 0;
  110. }
  111. static const struct of_device_id ath79_reset_dt_ids[] = {
  112. { .compatible = "qca,ar7100-reset", },
  113. { },
  114. };
  115. MODULE_DEVICE_TABLE(of, ath79_reset_dt_ids);
  116. static struct platform_driver ath79_reset_driver = {
  117. .probe = ath79_reset_probe,
  118. .remove = ath79_reset_remove,
  119. .driver = {
  120. .name = "ath79-reset",
  121. .of_match_table = ath79_reset_dt_ids,
  122. },
  123. };
  124. module_platform_driver(ath79_reset_driver);
  125. MODULE_AUTHOR("Alban Bedel <albeu@free.fr>");
  126. MODULE_DESCRIPTION("AR71xx Reset Controller Driver");
  127. MODULE_LICENSE("GPL");