brcmstb-reboot.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. static struct regmap *regmap;
  20. static u32 rst_src_en;
  21. static u32 sw_mstr_rst;
  22. struct reset_reg_mask {
  23. u32 rst_src_en_mask;
  24. u32 sw_mstr_rst_mask;
  25. };
  26. static const struct reset_reg_mask *reset_masks;
  27. static int brcmstb_restart_handler(struct sys_off_data *data)
  28. {
  29. int rc;
  30. u32 tmp;
  31. rc = regmap_write(regmap, rst_src_en, reset_masks->rst_src_en_mask);
  32. if (rc) {
  33. pr_err("failed to write rst_src_en (%d)\n", rc);
  34. return NOTIFY_DONE;
  35. }
  36. rc = regmap_read(regmap, rst_src_en, &tmp);
  37. if (rc) {
  38. pr_err("failed to read rst_src_en (%d)\n", rc);
  39. return NOTIFY_DONE;
  40. }
  41. rc = regmap_write(regmap, sw_mstr_rst, reset_masks->sw_mstr_rst_mask);
  42. if (rc) {
  43. pr_err("failed to write sw_mstr_rst (%d)\n", rc);
  44. return NOTIFY_DONE;
  45. }
  46. rc = regmap_read(regmap, sw_mstr_rst, &tmp);
  47. if (rc) {
  48. pr_err("failed to read sw_mstr_rst (%d)\n", rc);
  49. return NOTIFY_DONE;
  50. }
  51. return NOTIFY_DONE;
  52. }
  53. static const struct reset_reg_mask reset_bits_40nm = {
  54. .rst_src_en_mask = BIT(0),
  55. .sw_mstr_rst_mask = BIT(0),
  56. };
  57. static const struct reset_reg_mask reset_bits_65nm = {
  58. .rst_src_en_mask = BIT(3),
  59. .sw_mstr_rst_mask = BIT(31),
  60. };
  61. static int brcmstb_reboot_probe(struct platform_device *pdev)
  62. {
  63. int rc;
  64. struct device_node *np = pdev->dev.of_node;
  65. unsigned int args[2];
  66. reset_masks = device_get_match_data(&pdev->dev);
  67. if (!reset_masks) {
  68. pr_err("failed to get match data\n");
  69. return -EINVAL;
  70. }
  71. regmap = syscon_regmap_lookup_by_phandle_args(np, "syscon", ARRAY_SIZE(args), args);
  72. if (IS_ERR(regmap)) {
  73. pr_err("failed to get syscon phandle\n");
  74. return -EINVAL;
  75. }
  76. rst_src_en = args[0];
  77. sw_mstr_rst = args[1];
  78. rc = devm_register_sys_off_handler(&pdev->dev, SYS_OFF_MODE_RESTART,
  79. 128, brcmstb_restart_handler, NULL);
  80. if (rc)
  81. dev_err(&pdev->dev,
  82. "cannot register restart handler (err=%d)\n", rc);
  83. return rc;
  84. }
  85. static const struct of_device_id of_match[] = {
  86. { .compatible = "brcm,brcmstb-reboot", .data = &reset_bits_40nm },
  87. { .compatible = "brcm,bcm7038-reboot", .data = &reset_bits_65nm },
  88. {},
  89. };
  90. static struct platform_driver brcmstb_reboot_driver = {
  91. .probe = brcmstb_reboot_probe,
  92. .driver = {
  93. .name = "brcmstb-reboot",
  94. .of_match_table = of_match,
  95. },
  96. };
  97. static int __init brcmstb_reboot_init(void)
  98. {
  99. return platform_driver_register(&brcmstb_reboot_driver);
  100. }
  101. subsys_initcall(brcmstb_reboot_init);