sysreset.c 3.9 KB

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