brcmstb-reboot.c 3.7 KB

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