reset-ath79.c 4.0 KB

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