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