cros_ec.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2021 Google LLC
  4. */
  5. #include <common.h>
  6. #include <cros_ec.h>
  7. #include <dm.h>
  8. #include <asm/test.h>
  9. #include <dm/test.h>
  10. #include <test/ut.h>
  11. static int dm_test_cros_ec_hello(struct unit_test_state *uts)
  12. {
  13. struct udevice *dev;
  14. uint val;
  15. ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev));
  16. ut_assertok(cros_ec_hello(dev, NULL));
  17. val = 0xdead1357;
  18. ut_assertok(cros_ec_hello(dev, &val));
  19. ut_asserteq(0xdead1357, val);
  20. sandbox_cros_ec_set_test_flags(dev, CROSECT_BREAK_HELLO);
  21. ut_asserteq(-ENOTSYNC, cros_ec_hello(dev, &val));
  22. ut_asserteq(0x12345678, val);
  23. return 0;
  24. }
  25. DM_TEST(dm_test_cros_ec_hello, UT_TESTF_SCAN_FDT);
  26. static int dm_test_cros_ec_sku_id(struct unit_test_state *uts)
  27. {
  28. struct udevice *dev;
  29. ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev));
  30. ut_asserteq(1234, cros_ec_get_sku_id(dev));
  31. /* try the command */
  32. console_record_reset();
  33. ut_assertok(run_command("crosec sku", 0));
  34. ut_assert_nextline("1234");
  35. ut_assert_console_end();
  36. return 0;
  37. }
  38. DM_TEST(dm_test_cros_ec_sku_id, UT_TESTF_SCAN_FDT);
  39. static int dm_test_cros_ec_features(struct unit_test_state *uts)
  40. {
  41. struct udevice *dev;
  42. u64 feat;
  43. ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev));
  44. ut_assertok(cros_ec_get_features(dev, &feat));
  45. ut_asserteq_64(1U << EC_FEATURE_FLASH | 1U << EC_FEATURE_I2C |
  46. 1u << EC_FEATURE_VSTORE |
  47. 1ULL << EC_FEATURE_UNIFIED_WAKE_MASKS | 1ULL << EC_FEATURE_ISH,
  48. feat);
  49. ut_asserteq(true, cros_ec_check_feature(dev, EC_FEATURE_I2C));
  50. ut_asserteq(false, cros_ec_check_feature(dev, EC_FEATURE_MOTION_SENSE));
  51. ut_asserteq(true, cros_ec_check_feature(dev, EC_FEATURE_ISH));
  52. /* try the command */
  53. console_record_reset();
  54. ut_assertok(run_command("crosec features", 0));
  55. ut_assert_nextline("flash");
  56. ut_assert_nextline("i2c");
  57. ut_assert_nextline("vstore");
  58. ut_assert_nextline("unified_wake_masks");
  59. ut_assert_nextline("ish");
  60. ut_assert_console_end();
  61. return 0;
  62. }
  63. DM_TEST(dm_test_cros_ec_features, UT_TESTF_SCAN_FDT);
  64. static int dm_test_cros_ec_switches(struct unit_test_state *uts)
  65. {
  66. struct udevice *dev;
  67. ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev));
  68. ut_asserteq(0, cros_ec_get_switches(dev));
  69. /* try the command */
  70. console_record_reset();
  71. ut_assertok(run_command("crosec switches", 0));
  72. ut_assert_console_end();
  73. /* Open the lid and check the switch changes */
  74. sandbox_cros_ec_set_test_flags(dev, CROSECT_LID_OPEN);
  75. ut_asserteq(EC_SWITCH_LID_OPEN, cros_ec_get_switches(dev));
  76. /* try the command */
  77. console_record_reset();
  78. ut_assertok(run_command("crosec switches", 0));
  79. ut_assert_nextline("lid open");
  80. ut_assert_console_end();
  81. return 0;
  82. }
  83. DM_TEST(dm_test_cros_ec_switches, UT_TESTF_SCAN_FDT);
  84. static int dm_test_cros_ec_events(struct unit_test_state *uts)
  85. {
  86. struct udevice *dev;
  87. u32 events;
  88. ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev));
  89. ut_assertok(cros_ec_get_host_events(dev, &events));
  90. ut_asserteq(0, events);
  91. /* try the command */
  92. console_record_reset();
  93. ut_assertok(run_command("crosec events", 0));
  94. ut_assert_nextline("00000000");
  95. ut_assert_console_end();
  96. /* Open the lid and check the event appears */
  97. sandbox_cros_ec_set_test_flags(dev, CROSECT_LID_OPEN);
  98. ut_assertok(cros_ec_get_host_events(dev, &events));
  99. ut_asserteq(EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN), events);
  100. /* try the command */
  101. console_record_reset();
  102. ut_assertok(run_command("crosec events", 0));
  103. ut_assert_nextline("00000002");
  104. ut_assert_nextline("lid_open");
  105. ut_assert_console_end();
  106. /* Clear the event */
  107. ut_assertok(cros_ec_clear_host_events(dev,
  108. EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN)));
  109. ut_assertok(cros_ec_get_host_events(dev, &events));
  110. ut_asserteq(0, events);
  111. return 0;
  112. }
  113. DM_TEST(dm_test_cros_ec_events, UT_TESTF_SCAN_FDT);
  114. static int dm_test_cros_ec_vstore(struct unit_test_state *uts)
  115. {
  116. const int size = EC_VSTORE_SLOT_SIZE;
  117. u8 test_data[size], data[size];
  118. struct udevice *dev;
  119. u32 locked;
  120. int i;
  121. ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev));
  122. ut_asserteq(true, cros_ec_vstore_supported(dev));
  123. ut_asserteq(4, cros_ec_vstore_info(dev, &locked));
  124. ut_asserteq(0, locked);
  125. /* Write some data */
  126. for (i = 0; i < size; i++)
  127. test_data[i] = ' ' + i;
  128. ut_assertok(cros_ec_vstore_write(dev, 2, test_data, size));
  129. /* Check it is locked */
  130. ut_asserteq(4, cros_ec_vstore_info(dev, &locked));
  131. ut_asserteq(1 << 2, locked);
  132. /* Read it back and compare */
  133. ut_assertok(cros_ec_vstore_read(dev, 2, data));
  134. ut_asserteq_mem(test_data, data, size);
  135. /* Try another slot to make sure it is empty */
  136. ut_assertok(cros_ec_vstore_read(dev, 0, data));
  137. for (i = 0; i < size; i++)
  138. ut_asserteq(0, data[i]);
  139. return 0;
  140. }
  141. DM_TEST(dm_test_cros_ec_vstore, UT_TESTF_SCAN_FDT);