blob: 089f6670a3f85d45035edcfa64b03095238a37b5 [file] [log] [blame]
Simon Glass9b306e52021-01-16 14:52:22 -07001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright 2021 Google LLC
4 */
5
Simon Glass9b306e52021-01-16 14:52:22 -07006#include <cros_ec.h>
7#include <dm.h>
8#include <asm/test.h>
9#include <dm/test.h>
10#include <test/ut.h>
11
12static 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 Glass1a92f832024-08-22 07:57:48 -060031DM_TEST(dm_test_cros_ec_hello, UTF_SCAN_FDT);
Simon Glass33909b52021-01-16 14:52:25 -070032
33static 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 Glass33909b52021-01-16 14:52:25 -070041 ut_assertok(run_command("crosec sku", 0));
42 ut_assert_nextline("1234");
43 ut_assert_console_end();
44
45 return 0;
46}
Simon Glassf54458b2024-08-22 07:57:59 -060047DM_TEST(dm_test_cros_ec_sku_id, UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass959e1ce2021-01-16 14:52:26 -070048
49static 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 Glass333e3a92021-01-16 14:52:31 -070057 1u << EC_FEATURE_VSTORE |
Simon Glass959e1ce2021-01-16 14:52:26 -070058 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 Glass959e1ce2021-01-16 14:52:26 -070066 ut_assertok(run_command("crosec features", 0));
67 ut_assert_nextline("flash");
68 ut_assert_nextline("i2c");
Simon Glass333e3a92021-01-16 14:52:31 -070069 ut_assert_nextline("vstore");
Simon Glass959e1ce2021-01-16 14:52:26 -070070 ut_assert_nextline("unified_wake_masks");
71 ut_assert_nextline("ish");
72 ut_assert_console_end();
73
74 return 0;
75}
Simon Glassf54458b2024-08-22 07:57:59 -060076DM_TEST(dm_test_cros_ec_features, UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass9d702522021-01-16 14:52:28 -070077
78static 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 Glass9d702522021-01-16 14:52:28 -070086 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 Glass9d702522021-01-16 14:52:28 -070094 ut_assertok(run_command("crosec switches", 0));
95 ut_assert_nextline("lid open");
96 ut_assert_console_end();
97
98 return 0;
99}
Simon Glassf54458b2024-08-22 07:57:59 -0600100DM_TEST(dm_test_cros_ec_switches, UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass03161302021-01-16 14:52:29 -0700101
102static 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 Glass03161302021-01-16 14:52:29 -0700112 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 Glass03161302021-01-16 14:52:29 -0700122 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 Glassf54458b2024-08-22 07:57:59 -0600135DM_TEST(dm_test_cros_ec_events, UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass333e3a92021-01-16 14:52:31 -0700136
137static 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 Glass1a92f832024-08-22 07:57:48 -0600171DM_TEST(dm_test_cros_ec_vstore, UTF_SCAN_FDT);