sysreset_sandbox.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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:
  51. state->last_sysreset = type;
  52. if (!state->sysreset_allowed[type])
  53. return -EACCES;
  54. sandbox_exit();
  55. break;
  56. case SYSRESET_POWER_OFF:
  57. if (!state->sysreset_allowed[type])
  58. return -EACCES;
  59. default:
  60. return -ENOSYS;
  61. }
  62. if (!state->sysreset_allowed[type])
  63. return -EACCES;
  64. return -EINPROGRESS;
  65. }
  66. int sandbox_sysreset_get_status(struct udevice *dev, char *buf, int size)
  67. {
  68. strlcpy(buf, "Reset Status: COLD", size);
  69. return 0;
  70. }
  71. int sandbox_sysreset_get_last(struct udevice *dev)
  72. {
  73. return SYSRESET_COLD;
  74. }
  75. static struct sysreset_ops sandbox_sysreset_ops = {
  76. .request = sandbox_sysreset_request,
  77. .get_status = sandbox_sysreset_get_status,
  78. .get_last = sandbox_sysreset_get_last,
  79. };
  80. static const struct udevice_id sandbox_sysreset_ids[] = {
  81. { .compatible = "sandbox,reset" },
  82. { }
  83. };
  84. U_BOOT_DRIVER(sysreset_sandbox) = {
  85. .name = "sysreset_sandbox",
  86. .id = UCLASS_SYSRESET,
  87. .of_match = sandbox_sysreset_ids,
  88. .ops = &sandbox_sysreset_ops,
  89. };
  90. static struct sysreset_ops sandbox_warm_sysreset_ops = {
  91. .request = sandbox_warm_sysreset_request,
  92. .get_status = sandbox_warm_sysreset_get_status,
  93. .get_last = sandbox_warm_sysreset_get_last,
  94. };
  95. static const struct udevice_id sandbox_warm_sysreset_ids[] = {
  96. { .compatible = "sandbox,warm-reset" },
  97. { }
  98. };
  99. U_BOOT_DRIVER(warm_sysreset_sandbox) = {
  100. .name = "warm_sysreset_sandbox",
  101. .id = UCLASS_SYSRESET,
  102. .of_match = sandbox_warm_sysreset_ids,
  103. .ops = &sandbox_warm_sysreset_ops,
  104. };
  105. /* This is here in case we don't have a device tree */
  106. U_BOOT_DEVICE(sysreset_sandbox_non_fdt) = {
  107. .name = "sysreset_sandbox",
  108. };