Simon Glass | 9b306e5 | 2021-01-16 14:52:22 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright 2021 Google LLC |
| 4 | */ |
| 5 | |
Simon Glass | 9b306e5 | 2021-01-16 14:52:22 -0700 | [diff] [blame] | 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 | |
| 12 | static int dm_test_cros_ec_hello(struct unit_test_state *uts) |
| 13 | { |
| 14 | struct udevice *dev; |
| 15 | uint val; |
| 16 | |
| 17 | ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev)); |
| 18 | |
| 19 | ut_assertok(cros_ec_hello(dev, NULL)); |
| 20 | |
| 21 | val = 0xdead1357; |
| 22 | ut_assertok(cros_ec_hello(dev, &val)); |
| 23 | ut_asserteq(0xdead1357, val); |
| 24 | |
| 25 | sandbox_cros_ec_set_test_flags(dev, CROSECT_BREAK_HELLO); |
| 26 | ut_asserteq(-ENOTSYNC, cros_ec_hello(dev, &val)); |
| 27 | ut_asserteq(0x12345678, val); |
| 28 | |
| 29 | return 0; |
| 30 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 31 | DM_TEST(dm_test_cros_ec_hello, UTF_SCAN_FDT); |
Simon Glass | 33909b5 | 2021-01-16 14:52:25 -0700 | [diff] [blame] | 32 | |
| 33 | static int dm_test_cros_ec_sku_id(struct unit_test_state *uts) |
| 34 | { |
| 35 | struct udevice *dev; |
| 36 | |
| 37 | ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev)); |
| 38 | ut_asserteq(1234, cros_ec_get_sku_id(dev)); |
| 39 | |
| 40 | /* try the command */ |
Simon Glass | 33909b5 | 2021-01-16 14:52:25 -0700 | [diff] [blame] | 41 | ut_assertok(run_command("crosec sku", 0)); |
| 42 | ut_assert_nextline("1234"); |
| 43 | ut_assert_console_end(); |
| 44 | |
| 45 | return 0; |
| 46 | } |
Simon Glass | f54458b | 2024-08-22 07:57:59 -0600 | [diff] [blame] | 47 | DM_TEST(dm_test_cros_ec_sku_id, UTF_SCAN_FDT | UTF_CONSOLE); |
Simon Glass | 959e1ce | 2021-01-16 14:52:26 -0700 | [diff] [blame] | 48 | |
| 49 | static int dm_test_cros_ec_features(struct unit_test_state *uts) |
| 50 | { |
| 51 | struct udevice *dev; |
| 52 | u64 feat; |
| 53 | |
| 54 | ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev)); |
| 55 | ut_assertok(cros_ec_get_features(dev, &feat)); |
| 56 | ut_asserteq_64(1U << EC_FEATURE_FLASH | 1U << EC_FEATURE_I2C | |
Simon Glass | 333e3a9 | 2021-01-16 14:52:31 -0700 | [diff] [blame] | 57 | 1u << EC_FEATURE_VSTORE | |
Simon Glass | 959e1ce | 2021-01-16 14:52:26 -0700 | [diff] [blame] | 58 | 1ULL << EC_FEATURE_UNIFIED_WAKE_MASKS | 1ULL << EC_FEATURE_ISH, |
| 59 | feat); |
| 60 | |
| 61 | ut_asserteq(true, cros_ec_check_feature(dev, EC_FEATURE_I2C)); |
| 62 | ut_asserteq(false, cros_ec_check_feature(dev, EC_FEATURE_MOTION_SENSE)); |
| 63 | ut_asserteq(true, cros_ec_check_feature(dev, EC_FEATURE_ISH)); |
| 64 | |
| 65 | /* try the command */ |
Simon Glass | 959e1ce | 2021-01-16 14:52:26 -0700 | [diff] [blame] | 66 | ut_assertok(run_command("crosec features", 0)); |
| 67 | ut_assert_nextline("flash"); |
| 68 | ut_assert_nextline("i2c"); |
Simon Glass | 333e3a9 | 2021-01-16 14:52:31 -0700 | [diff] [blame] | 69 | ut_assert_nextline("vstore"); |
Simon Glass | 959e1ce | 2021-01-16 14:52:26 -0700 | [diff] [blame] | 70 | ut_assert_nextline("unified_wake_masks"); |
| 71 | ut_assert_nextline("ish"); |
| 72 | ut_assert_console_end(); |
| 73 | |
| 74 | return 0; |
| 75 | } |
Simon Glass | f54458b | 2024-08-22 07:57:59 -0600 | [diff] [blame] | 76 | DM_TEST(dm_test_cros_ec_features, UTF_SCAN_FDT | UTF_CONSOLE); |
Simon Glass | 9d70252 | 2021-01-16 14:52:28 -0700 | [diff] [blame] | 77 | |
| 78 | static int dm_test_cros_ec_switches(struct unit_test_state *uts) |
| 79 | { |
| 80 | struct udevice *dev; |
| 81 | |
| 82 | ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev)); |
| 83 | ut_asserteq(0, cros_ec_get_switches(dev)); |
| 84 | |
| 85 | /* try the command */ |
Simon Glass | 9d70252 | 2021-01-16 14:52:28 -0700 | [diff] [blame] | 86 | ut_assertok(run_command("crosec switches", 0)); |
| 87 | ut_assert_console_end(); |
| 88 | |
| 89 | /* Open the lid and check the switch changes */ |
| 90 | sandbox_cros_ec_set_test_flags(dev, CROSECT_LID_OPEN); |
| 91 | ut_asserteq(EC_SWITCH_LID_OPEN, cros_ec_get_switches(dev)); |
| 92 | |
| 93 | /* try the command */ |
Simon Glass | 9d70252 | 2021-01-16 14:52:28 -0700 | [diff] [blame] | 94 | ut_assertok(run_command("crosec switches", 0)); |
| 95 | ut_assert_nextline("lid open"); |
| 96 | ut_assert_console_end(); |
| 97 | |
| 98 | return 0; |
| 99 | } |
Simon Glass | f54458b | 2024-08-22 07:57:59 -0600 | [diff] [blame] | 100 | DM_TEST(dm_test_cros_ec_switches, UTF_SCAN_FDT | UTF_CONSOLE); |
Simon Glass | 0316130 | 2021-01-16 14:52:29 -0700 | [diff] [blame] | 101 | |
| 102 | static int dm_test_cros_ec_events(struct unit_test_state *uts) |
| 103 | { |
| 104 | struct udevice *dev; |
| 105 | u32 events; |
| 106 | |
| 107 | ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev)); |
| 108 | ut_assertok(cros_ec_get_host_events(dev, &events)); |
| 109 | ut_asserteq(0, events); |
| 110 | |
| 111 | /* try the command */ |
Simon Glass | 0316130 | 2021-01-16 14:52:29 -0700 | [diff] [blame] | 112 | ut_assertok(run_command("crosec events", 0)); |
| 113 | ut_assert_nextline("00000000"); |
| 114 | ut_assert_console_end(); |
| 115 | |
| 116 | /* Open the lid and check the event appears */ |
| 117 | sandbox_cros_ec_set_test_flags(dev, CROSECT_LID_OPEN); |
| 118 | ut_assertok(cros_ec_get_host_events(dev, &events)); |
| 119 | ut_asserteq(EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN), events); |
| 120 | |
| 121 | /* try the command */ |
Simon Glass | 0316130 | 2021-01-16 14:52:29 -0700 | [diff] [blame] | 122 | ut_assertok(run_command("crosec events", 0)); |
| 123 | ut_assert_nextline("00000002"); |
| 124 | ut_assert_nextline("lid_open"); |
| 125 | ut_assert_console_end(); |
| 126 | |
| 127 | /* Clear the event */ |
| 128 | ut_assertok(cros_ec_clear_host_events(dev, |
| 129 | EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN))); |
| 130 | ut_assertok(cros_ec_get_host_events(dev, &events)); |
| 131 | ut_asserteq(0, events); |
| 132 | |
| 133 | return 0; |
| 134 | } |
Simon Glass | f54458b | 2024-08-22 07:57:59 -0600 | [diff] [blame] | 135 | DM_TEST(dm_test_cros_ec_events, UTF_SCAN_FDT | UTF_CONSOLE); |
Simon Glass | 333e3a9 | 2021-01-16 14:52:31 -0700 | [diff] [blame] | 136 | |
| 137 | static int dm_test_cros_ec_vstore(struct unit_test_state *uts) |
| 138 | { |
| 139 | const int size = EC_VSTORE_SLOT_SIZE; |
| 140 | u8 test_data[size], data[size]; |
| 141 | struct udevice *dev; |
| 142 | u32 locked; |
| 143 | int i; |
| 144 | |
| 145 | ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev)); |
| 146 | ut_asserteq(true, cros_ec_vstore_supported(dev)); |
| 147 | |
| 148 | ut_asserteq(4, cros_ec_vstore_info(dev, &locked)); |
| 149 | ut_asserteq(0, locked); |
| 150 | |
| 151 | /* Write some data */ |
| 152 | for (i = 0; i < size; i++) |
| 153 | test_data[i] = ' ' + i; |
| 154 | ut_assertok(cros_ec_vstore_write(dev, 2, test_data, size)); |
| 155 | |
| 156 | /* Check it is locked */ |
| 157 | ut_asserteq(4, cros_ec_vstore_info(dev, &locked)); |
| 158 | ut_asserteq(1 << 2, locked); |
| 159 | |
| 160 | /* Read it back and compare */ |
| 161 | ut_assertok(cros_ec_vstore_read(dev, 2, data)); |
| 162 | ut_asserteq_mem(test_data, data, size); |
| 163 | |
| 164 | /* Try another slot to make sure it is empty */ |
| 165 | ut_assertok(cros_ec_vstore_read(dev, 0, data)); |
| 166 | for (i = 0; i < size; i++) |
| 167 | ut_asserteq(0, data[i]); |
| 168 | |
| 169 | return 0; |
| 170 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 171 | DM_TEST(dm_test_cros_ec_vstore, UTF_SCAN_FDT); |