syscon-reboot.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic Syscon Reboot Driver
  4. *
  5. * Copyright (c) 2013, Applied Micro Circuits Corporation
  6. * Author: Feng Kan <fkan@apm.com>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/notifier.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/reboot.h>
  16. #include <linux/regmap.h>
  17. struct syscon_reboot_context {
  18. struct regmap *map;
  19. u32 offset;
  20. u32 value;
  21. u32 mask;
  22. struct notifier_block restart_handler;
  23. };
  24. static int syscon_restart_handle(struct notifier_block *this,
  25. unsigned long mode, void *cmd)
  26. {
  27. struct syscon_reboot_context *ctx =
  28. container_of(this, struct syscon_reboot_context,
  29. restart_handler);
  30. /* Issue the reboot */
  31. regmap_update_bits(ctx->map, ctx->offset, ctx->mask, ctx->value);
  32. mdelay(1000);
  33. pr_emerg("Unable to restart system\n");
  34. return NOTIFY_DONE;
  35. }
  36. static int syscon_reboot_probe(struct platform_device *pdev)
  37. {
  38. struct syscon_reboot_context *ctx;
  39. struct device *dev = &pdev->dev;
  40. int mask_err, value_err;
  41. int err;
  42. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  43. if (!ctx)
  44. return -ENOMEM;
  45. ctx->map = syscon_regmap_lookup_by_phandle(dev->of_node, "regmap");
  46. if (IS_ERR(ctx->map)) {
  47. ctx->map = syscon_node_to_regmap(dev->parent->of_node);
  48. if (IS_ERR(ctx->map))
  49. return PTR_ERR(ctx->map);
  50. }
  51. if (of_property_read_u32(pdev->dev.of_node, "offset", &ctx->offset))
  52. return -EINVAL;
  53. value_err = of_property_read_u32(pdev->dev.of_node, "value", &ctx->value);
  54. mask_err = of_property_read_u32(pdev->dev.of_node, "mask", &ctx->mask);
  55. if (value_err && mask_err) {
  56. dev_err(dev, "unable to read 'value' and 'mask'");
  57. return -EINVAL;
  58. }
  59. if (value_err) {
  60. /* support old binding */
  61. ctx->value = ctx->mask;
  62. ctx->mask = 0xFFFFFFFF;
  63. } else if (mask_err) {
  64. /* support value without mask*/
  65. ctx->mask = 0xFFFFFFFF;
  66. }
  67. ctx->restart_handler.notifier_call = syscon_restart_handle;
  68. ctx->restart_handler.priority = 192;
  69. err = register_restart_handler(&ctx->restart_handler);
  70. if (err)
  71. dev_err(dev, "can't register restart notifier (err=%d)\n", err);
  72. return err;
  73. }
  74. static const struct of_device_id syscon_reboot_of_match[] = {
  75. { .compatible = "syscon-reboot" },
  76. {}
  77. };
  78. static struct platform_driver syscon_reboot_driver = {
  79. .probe = syscon_reboot_probe,
  80. .driver = {
  81. .name = "syscon-reboot",
  82. .of_match_table = syscon_reboot_of_match,
  83. },
  84. };
  85. builtin_platform_driver(syscon_reboot_driver);