sysreset.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <sysreset.h>
  8. #include <asm/state.h>
  9. #include <asm/test.h>
  10. #include <dm/test.h>
  11. #include <test/test.h>
  12. #include <test/ut.h>
  13. /* Test that we can use particular sysreset devices */
  14. static int dm_test_sysreset_base(struct unit_test_state *uts)
  15. {
  16. struct sandbox_state *state = state_get_current();
  17. struct udevice *dev;
  18. /* Device 0 is the platform data device - it should never respond */
  19. ut_assertok(uclass_get_device(UCLASS_SYSRESET, 0, &dev));
  20. ut_asserteq(-ENODEV, sysreset_request(dev, SYSRESET_WARM));
  21. ut_asserteq(-ENODEV, sysreset_request(dev, SYSRESET_COLD));
  22. ut_asserteq(-ENODEV, sysreset_request(dev, SYSRESET_POWER));
  23. /* Device 1 is the warm sysreset device */
  24. ut_assertok(uclass_get_device(UCLASS_SYSRESET, 1, &dev));
  25. ut_asserteq(-EACCES, sysreset_request(dev, SYSRESET_WARM));
  26. ut_asserteq(-ENOSYS, sysreset_request(dev, SYSRESET_COLD));
  27. ut_asserteq(-ENOSYS, sysreset_request(dev, SYSRESET_POWER));
  28. state->sysreset_allowed[SYSRESET_WARM] = true;
  29. ut_asserteq(-EINPROGRESS, sysreset_request(dev, SYSRESET_WARM));
  30. state->sysreset_allowed[SYSRESET_WARM] = false;
  31. /* Device 2 is the cold sysreset device */
  32. ut_assertok(uclass_get_device(UCLASS_SYSRESET, 2, &dev));
  33. ut_asserteq(-ENOSYS, sysreset_request(dev, SYSRESET_WARM));
  34. state->sysreset_allowed[SYSRESET_COLD] = false;
  35. ut_asserteq(-EACCES, sysreset_request(dev, SYSRESET_COLD));
  36. state->sysreset_allowed[SYSRESET_COLD] = true;
  37. state->sysreset_allowed[SYSRESET_POWER] = false;
  38. ut_asserteq(-EACCES, sysreset_request(dev, SYSRESET_POWER));
  39. state->sysreset_allowed[SYSRESET_POWER] = true;
  40. return 0;
  41. }
  42. DM_TEST(dm_test_sysreset_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  43. static int dm_test_sysreset_get_status(struct unit_test_state *uts)
  44. {
  45. struct udevice *dev;
  46. char msg[64];
  47. /* Device 1 is the warm sysreset device */
  48. ut_assertok(uclass_get_device(UCLASS_SYSRESET, 1, &dev));
  49. ut_assertok(sysreset_get_status(dev, msg, sizeof(msg)));
  50. ut_asserteq_str("Reset Status: WARM", msg);
  51. /* Device 2 is the cold sysreset device */
  52. ut_assertok(uclass_get_device(UCLASS_SYSRESET, 2, &dev));
  53. ut_assertok(sysreset_get_status(dev, msg, sizeof(msg)));
  54. ut_asserteq_str("Reset Status: COLD", msg);
  55. return 0;
  56. }
  57. DM_TEST(dm_test_sysreset_get_status, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  58. /* Test that we can walk through the sysreset devices */
  59. static int dm_test_sysreset_walk(struct unit_test_state *uts)
  60. {
  61. struct sandbox_state *state = state_get_current();
  62. /* If we generate a power sysreset, we will exit sandbox! */
  63. state->sysreset_allowed[SYSRESET_WARM] = false;
  64. state->sysreset_allowed[SYSRESET_COLD] = false;
  65. state->sysreset_allowed[SYSRESET_POWER] = false;
  66. state->sysreset_allowed[SYSRESET_POWER_OFF] = false;
  67. ut_asserteq(-EACCES, sysreset_walk(SYSRESET_WARM));
  68. ut_asserteq(-EACCES, sysreset_walk(SYSRESET_COLD));
  69. ut_asserteq(-EACCES, sysreset_walk(SYSRESET_POWER));
  70. ut_asserteq(-EACCES, sysreset_walk(SYSRESET_POWER_OFF));
  71. /*
  72. * Enable cold system reset - this should make cold system reset work,
  73. * plus a warm system reset should be promoted to cold, since this is
  74. * the next step along.
  75. */
  76. state->sysreset_allowed[SYSRESET_WARM] = true;
  77. ut_asserteq(-EINPROGRESS, sysreset_walk(SYSRESET_WARM));
  78. ut_asserteq(-EACCES, sysreset_walk(SYSRESET_COLD));
  79. ut_asserteq(-EACCES, sysreset_walk(SYSRESET_POWER));
  80. state->sysreset_allowed[SYSRESET_COLD] = true;
  81. state->sysreset_allowed[SYSRESET_POWER] = true;
  82. return 0;
  83. }
  84. DM_TEST(dm_test_sysreset_walk, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  85. static int dm_test_sysreset_get_last(struct unit_test_state *uts)
  86. {
  87. struct udevice *dev;
  88. /* Device 1 is the warm sysreset device */
  89. ut_assertok(uclass_get_device(UCLASS_SYSRESET, 1, &dev));
  90. ut_asserteq(SYSRESET_WARM, sysreset_get_last(dev));
  91. /* Device 2 is the cold sysreset device */
  92. ut_assertok(uclass_get_device(UCLASS_SYSRESET, 2, &dev));
  93. ut_asserteq(SYSRESET_POWER, sysreset_get_last(dev));
  94. /* This is device 0, the non-DT one */
  95. ut_asserteq(SYSRESET_POWER, sysreset_get_last_walk());
  96. return 0;
  97. }
  98. DM_TEST(dm_test_sysreset_get_last, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);