sysreset_sandbox.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <sysreset.h>
  10. #include <asm/state.h>
  11. #include <asm/test.h>
  12. static int sandbox_warm_sysreset_request(struct udevice *dev,
  13. enum sysreset_t type)
  14. {
  15. struct sandbox_state *state = state_get_current();
  16. switch (type) {
  17. case SYSRESET_WARM:
  18. state->last_sysreset = type;
  19. break;
  20. default:
  21. return -ENOSYS;
  22. }
  23. if (!state->sysreset_allowed[type])
  24. return -EACCES;
  25. return -EINPROGRESS;
  26. }
  27. int sandbox_warm_sysreset_get_status(struct udevice *dev, char *buf, int size)
  28. {
  29. strlcpy(buf, "Reset Status: WARM", size);
  30. return 0;
  31. }
  32. int sandbox_warm_sysreset_get_last(struct udevice *dev)
  33. {
  34. return SYSRESET_WARM;
  35. }
  36. static int sandbox_sysreset_request(struct udevice *dev, enum sysreset_t type)
  37. {
  38. struct sandbox_state *state = state_get_current();
  39. /*
  40. * If we have a device tree, the device we created from platform data
  41. * (see the U_BOOT_DEVICE() declaration below) should not do anything.
  42. * If we are that device, return an error.
  43. */
  44. if (state->fdt_fname && !dev_of_valid(dev))
  45. return -ENODEV;
  46. switch (type) {
  47. case SYSRESET_COLD:
  48. state->last_sysreset = type;
  49. break;
  50. case SYSRESET_POWER_OFF:
  51. state->last_sysreset = type;
  52. if (!state->sysreset_allowed[type])
  53. return -EACCES;
  54. sandbox_exit();
  55. break;
  56. case SYSRESET_POWER:
  57. if (!state->sysreset_allowed[type])
  58. return -EACCES;
  59. sandbox_exit();
  60. default:
  61. return -ENOSYS;
  62. }
  63. if (!state->sysreset_allowed[type])
  64. return -EACCES;
  65. return -EINPROGRESS;
  66. }
  67. int sandbox_sysreset_get_status(struct udevice *dev, char *buf, int size)
  68. {
  69. strlcpy(buf, "Reset Status: COLD", size);
  70. return 0;
  71. }
  72. int sandbox_sysreset_get_last(struct udevice *dev)
  73. {
  74. struct sandbox_state *state = state_get_current();
  75. /*
  76. * The first phase is a power reset, after that we assume we don't
  77. * know.
  78. */
  79. return state->jumped_fname ? SYSRESET_WARM : SYSRESET_POWER;
  80. }
  81. static struct sysreset_ops sandbox_sysreset_ops = {
  82. .request = sandbox_sysreset_request,
  83. .get_status = sandbox_sysreset_get_status,
  84. .get_last = sandbox_sysreset_get_last,
  85. };
  86. static const struct udevice_id sandbox_sysreset_ids[] = {
  87. { .compatible = "sandbox,reset" },
  88. { }
  89. };
  90. U_BOOT_DRIVER(sysreset_sandbox) = {
  91. .name = "sysreset_sandbox",
  92. .id = UCLASS_SYSRESET,
  93. .of_match = sandbox_sysreset_ids,
  94. .ops = &sandbox_sysreset_ops,
  95. };
  96. static struct sysreset_ops sandbox_warm_sysreset_ops = {
  97. .request = sandbox_warm_sysreset_request,
  98. .get_status = sandbox_warm_sysreset_get_status,
  99. .get_last = sandbox_warm_sysreset_get_last,
  100. };
  101. static const struct udevice_id sandbox_warm_sysreset_ids[] = {
  102. { .compatible = "sandbox,warm-reset" },
  103. { }
  104. };
  105. U_BOOT_DRIVER(warm_sysreset_sandbox) = {
  106. .name = "warm_sysreset_sandbox",
  107. .id = UCLASS_SYSRESET,
  108. .of_match = sandbox_warm_sysreset_ids,
  109. .ops = &sandbox_warm_sysreset_ops,
  110. };
  111. /* This is here in case we don't have a device tree */
  112. U_BOOT_DEVICE(sysreset_sandbox_non_fdt) = {
  113. .name = "sysreset_sandbox",
  114. };