blob: ac0055f0acd6ef4671345573028a3fc9f3a5771e [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}
31DM_TEST(dm_test_cros_ec_hello, UT_TESTF_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 */
41 console_record_reset();
42 ut_assertok(run_command("crosec sku", 0));
43 ut_assert_nextline("1234");
44 ut_assert_console_end();
45
46 return 0;
47}
48DM_TEST(dm_test_cros_ec_sku_id, UT_TESTF_SCAN_FDT);
Simon Glass959e1ce2021-01-16 14:52:26 -070049
50static int dm_test_cros_ec_features(struct unit_test_state *uts)
51{
52 struct udevice *dev;
53 u64 feat;
54
55 ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev));
56 ut_assertok(cros_ec_get_features(dev, &feat));
57 ut_asserteq_64(1U << EC_FEATURE_FLASH | 1U << EC_FEATURE_I2C |
Simon Glass333e3a92021-01-16 14:52:31 -070058 1u << EC_FEATURE_VSTORE |
Simon Glass959e1ce2021-01-16 14:52:26 -070059 1ULL << EC_FEATURE_UNIFIED_WAKE_MASKS | 1ULL << EC_FEATURE_ISH,
60 feat);
61
62 ut_asserteq(true, cros_ec_check_feature(dev, EC_FEATURE_I2C));
63 ut_asserteq(false, cros_ec_check_feature(dev, EC_FEATURE_MOTION_SENSE));
64 ut_asserteq(true, cros_ec_check_feature(dev, EC_FEATURE_ISH));
65
66 /* try the command */
67 console_record_reset();
68 ut_assertok(run_command("crosec features", 0));
69 ut_assert_nextline("flash");
70 ut_assert_nextline("i2c");
Simon Glass333e3a92021-01-16 14:52:31 -070071 ut_assert_nextline("vstore");
Simon Glass959e1ce2021-01-16 14:52:26 -070072 ut_assert_nextline("unified_wake_masks");
73 ut_assert_nextline("ish");
74 ut_assert_console_end();
75
76 return 0;
77}
78DM_TEST(dm_test_cros_ec_features, UT_TESTF_SCAN_FDT);
Simon Glass9d702522021-01-16 14:52:28 -070079
80static int dm_test_cros_ec_switches(struct unit_test_state *uts)
81{
82 struct udevice *dev;
83
84 ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev));
85 ut_asserteq(0, cros_ec_get_switches(dev));
86
87 /* try the command */
88 console_record_reset();
89 ut_assertok(run_command("crosec switches", 0));
90 ut_assert_console_end();
91
92 /* Open the lid and check the switch changes */
93 sandbox_cros_ec_set_test_flags(dev, CROSECT_LID_OPEN);
94 ut_asserteq(EC_SWITCH_LID_OPEN, cros_ec_get_switches(dev));
95
96 /* try the command */
97 console_record_reset();
98 ut_assertok(run_command("crosec switches", 0));
99 ut_assert_nextline("lid open");
100 ut_assert_console_end();
101
102 return 0;
103}
104DM_TEST(dm_test_cros_ec_switches, UT_TESTF_SCAN_FDT);
Simon Glass03161302021-01-16 14:52:29 -0700105
106static int dm_test_cros_ec_events(struct unit_test_state *uts)
107{
108 struct udevice *dev;
109 u32 events;
110
111 ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev));
112 ut_assertok(cros_ec_get_host_events(dev, &events));
113 ut_asserteq(0, events);
114
115 /* try the command */
116 console_record_reset();
117 ut_assertok(run_command("crosec events", 0));
118 ut_assert_nextline("00000000");
119 ut_assert_console_end();
120
121 /* Open the lid and check the event appears */
122 sandbox_cros_ec_set_test_flags(dev, CROSECT_LID_OPEN);
123 ut_assertok(cros_ec_get_host_events(dev, &events));
124 ut_asserteq(EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN), events);
125
126 /* try the command */
127 console_record_reset();
128 ut_assertok(run_command("crosec events", 0));
129 ut_assert_nextline("00000002");
130 ut_assert_nextline("lid_open");
131 ut_assert_console_end();
132
133 /* Clear the event */
134 ut_assertok(cros_ec_clear_host_events(dev,
135 EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN)));
136 ut_assertok(cros_ec_get_host_events(dev, &events));
137 ut_asserteq(0, events);
138
139 return 0;
140}
141DM_TEST(dm_test_cros_ec_events, UT_TESTF_SCAN_FDT);
Simon Glass333e3a92021-01-16 14:52:31 -0700142
143static int dm_test_cros_ec_vstore(struct unit_test_state *uts)
144{
145 const int size = EC_VSTORE_SLOT_SIZE;
146 u8 test_data[size], data[size];
147 struct udevice *dev;
148 u32 locked;
149 int i;
150
151 ut_assertok(uclass_first_device_err(UCLASS_CROS_EC, &dev));
152 ut_asserteq(true, cros_ec_vstore_supported(dev));
153
154 ut_asserteq(4, cros_ec_vstore_info(dev, &locked));
155 ut_asserteq(0, locked);
156
157 /* Write some data */
158 for (i = 0; i < size; i++)
159 test_data[i] = ' ' + i;
160 ut_assertok(cros_ec_vstore_write(dev, 2, test_data, size));
161
162 /* Check it is locked */
163 ut_asserteq(4, cros_ec_vstore_info(dev, &locked));
164 ut_asserteq(1 << 2, locked);
165
166 /* Read it back and compare */
167 ut_assertok(cros_ec_vstore_read(dev, 2, data));
168 ut_asserteq_mem(test_data, data, size);
169
170 /* Try another slot to make sure it is empty */
171 ut_assertok(cros_ec_vstore_read(dev, 0, data));
172 for (i = 0; i < size; i++)
173 ut_asserteq(0, data[i]);
174
175 return 0;
176}
177DM_TEST(dm_test_cros_ec_vstore, UT_TESTF_SCAN_FDT);