brcmstb-reboot.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2013 Broadcom Corporation
  3. #include <linux/bitops.h>
  4. #include <linux/device.h>
  5. #include <linux/errno.h>
  6. #include <linux/init.h>
  7. #include <linux/io.h>
  8. #include <linux/jiffies.h>
  9. #include <linux/notifier.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/printk.h>
  15. #include <linux/reboot.h>
  16. #include <linux/regmap.h>
  17. #include <linux/smp.h>
  18. #include <linux/mfd/syscon.h>
  19. #define RESET_SOURCE_ENABLE_REG 1
  20. #define SW_MASTER_RESET_REG 2
  21. static struct regmap *regmap;
  22. static u32 rst_src_en;
  23. static u32 sw_mstr_rst;
  24. struct reset_reg_mask {
  25. u32 rst_src_en_mask;
  26. u32 sw_mstr_rst_mask;
  27. };
  28. static const struct reset_reg_mask *reset_masks;
  29. static int brcmstb_restart_handler(struct notifier_block *this,
  30. unsigned long mode, void *cmd)
  31. {
  32. int rc;
  33. u32 tmp;
  34. rc = regmap_write(regmap, rst_src_en, reset_masks->rst_src_en_mask);
  35. if (rc) {
  36. pr_err("failed to write rst_src_en (%d)\n", rc);
  37. return NOTIFY_DONE;
  38. }
  39. rc = regmap_read(regmap, rst_src_en, &tmp);
  40. if (rc) {
  41. pr_err("failed to read rst_src_en (%d)\n", rc);
  42. return NOTIFY_DONE;
  43. }
  44. rc = regmap_write(regmap, sw_mstr_rst, reset_masks->sw_mstr_rst_mask);
  45. if (rc) {
  46. pr_err("failed to write sw_mstr_rst (%d)\n", rc);
  47. return NOTIFY_DONE;
  48. }
  49. rc = regmap_read(regmap, sw_mstr_rst, &tmp);
  50. if (rc) {
  51. pr_err("failed to read sw_mstr_rst (%d)\n", rc);
  52. return NOTIFY_DONE;
  53. }
  54. while (1)
  55. ;
  56. return NOTIFY_DONE;
  57. }
  58. static struct notifier_block brcmstb_restart_nb = {
  59. .notifier_call = brcmstb_restart_handler,
  60. .priority = 128,
  61. };
  62. static const struct reset_reg_mask reset_bits_40nm = {
  63. .rst_src_en_mask = BIT(0),
  64. .sw_mstr_rst_mask = BIT(0),
  65. };
  66. static const struct reset_reg_mask reset_bits_65nm = {
  67. .rst_src_en_mask = BIT(3),
  68. .sw_mstr_rst_mask = BIT(31),
  69. };
  70. static const struct of_device_id of_match[] = {
  71. { .compatible = "brcm,brcmstb-reboot", .data = &reset_bits_40nm },
  72. { .compatible = "brcm,bcm7038-reboot", .data = &reset_bits_65nm },
  73. {},
  74. };
  75. static int brcmstb_reboot_probe(struct platform_device *pdev)
  76. {
  77. int rc;
  78. struct device_node *np = pdev->dev.of_node;
  79. const struct of_device_id *of_id;
  80. of_id = of_match_node(of_match, np);
  81. if (!of_id) {
  82. pr_err("failed to look up compatible string\n");
  83. return -EINVAL;
  84. }
  85. reset_masks = of_id->data;
  86. regmap = syscon_regmap_lookup_by_phandle(np, "syscon");
  87. if (IS_ERR(regmap)) {
  88. pr_err("failed to get syscon phandle\n");
  89. return -EINVAL;
  90. }
  91. rc = of_property_read_u32_index(np, "syscon", RESET_SOURCE_ENABLE_REG,
  92. &rst_src_en);
  93. if (rc) {
  94. pr_err("can't get rst_src_en offset (%d)\n", rc);
  95. return -EINVAL;
  96. }
  97. rc = of_property_read_u32_index(np, "syscon", SW_MASTER_RESET_REG,
  98. &sw_mstr_rst);
  99. if (rc) {
  100. pr_err("can't get sw_mstr_rst offset (%d)\n", rc);
  101. return -EINVAL;
  102. }
  103. rc = register_restart_handler(&brcmstb_restart_nb);
  104. if (rc)
  105. dev_err(&pdev->dev,
  106. "cannot register restart handler (err=%d)\n", rc);
  107. return rc;
  108. }
  109. static struct platform_driver brcmstb_reboot_driver = {
  110. .probe = brcmstb_reboot_probe,
  111. .driver = {
  112. .name = "brcmstb-reboot",
  113. .of_match_table = of_match,
  114. },
  115. };
  116. static int __init brcmstb_reboot_init(void)
  117. {
  118. return platform_driver_probe(&brcmstb_reboot_driver,
  119. brcmstb_reboot_probe);
  120. }
  121. subsys_initcall(brcmstb_reboot_init);