]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
bfeba017 MF |
2 | /* |
3 | * Chromium OS cros_ec driver | |
4 | * | |
5 | * Copyright (c) 2016 The Chromium OS Authors. | |
6 | * Copyright (c) 2016 National Instruments Corp | |
bfeba017 MF |
7 | */ |
8 | ||
9 | #include <common.h> | |
10 | #include <command.h> | |
11 | #include <cros_ec.h> | |
12 | #include <dm.h> | |
b79fdc76 | 13 | #include <flash.h> |
f7ae49fc | 14 | #include <log.h> |
bfeba017 MF |
15 | #include <dm/device-internal.h> |
16 | #include <dm/uclass-internal.h> | |
17 | ||
18 | /* Note: depends on enum ec_current_image */ | |
19 | static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"}; | |
20 | ||
a2558e87 MF |
21 | /** |
22 | * Decode a flash region parameter | |
23 | * | |
24 | * @param argc Number of params remaining | |
25 | * @param argv List of remaining parameters | |
26 | * @return flash region (EC_FLASH_REGION_...) or -1 on error | |
27 | */ | |
09140113 | 28 | static int cros_ec_decode_region(int argc, char *const argv[]) |
a2558e87 MF |
29 | { |
30 | if (argc > 0) { | |
31 | if (0 == strcmp(*argv, "rw")) | |
6f1c0430 | 32 | return EC_FLASH_REGION_ACTIVE; |
a2558e87 MF |
33 | else if (0 == strcmp(*argv, "ro")) |
34 | return EC_FLASH_REGION_RO; | |
35 | ||
36 | debug("%s: Invalid region '%s'\n", __func__, *argv); | |
37 | } else { | |
38 | debug("%s: Missing region parameter\n", __func__); | |
39 | } | |
40 | ||
41 | return -1; | |
42 | } | |
43 | ||
bfeba017 MF |
44 | /** |
45 | * Perform a flash read or write command | |
46 | * | |
47 | * @param dev CROS-EC device to read/write | |
48 | * @param is_write 1 do to a write, 0 to do a read | |
49 | * @param argc Number of arguments | |
50 | * @param argv Arguments (2 is region, 3 is address) | |
51 | * @return 0 for ok, 1 for a usage error or -ve for ec command error | |
52 | * (negative EC_RES_...) | |
53 | */ | |
6322a7b6 | 54 | static int do_read_write(struct udevice *dev, int is_write, int argc, |
09140113 | 55 | char *const argv[]) |
bfeba017 MF |
56 | { |
57 | uint32_t offset, size = -1U, region_size; | |
58 | unsigned long addr; | |
59 | char *endp; | |
60 | int region; | |
61 | int ret; | |
62 | ||
63 | region = cros_ec_decode_region(argc - 2, argv + 2); | |
64 | if (region == -1) | |
65 | return 1; | |
66 | if (argc < 4) | |
67 | return 1; | |
68 | addr = simple_strtoul(argv[3], &endp, 16); | |
69 | if (*argv[3] == 0 || *endp != 0) | |
70 | return 1; | |
71 | if (argc > 4) { | |
72 | size = simple_strtoul(argv[4], &endp, 16); | |
73 | if (*argv[4] == 0 || *endp != 0) | |
74 | return 1; | |
75 | } | |
76 | ||
77 | ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size); | |
78 | if (ret) { | |
79 | debug("%s: Could not read region info\n", __func__); | |
80 | return ret; | |
81 | } | |
82 | if (size == -1U) | |
83 | size = region_size; | |
84 | ||
85 | ret = is_write ? | |
86 | cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) : | |
87 | cros_ec_flash_read(dev, (uint8_t *)addr, offset, size); | |
88 | if (ret) { | |
89 | debug("%s: Could not %s region\n", __func__, | |
90 | is_write ? "write" : "read"); | |
91 | return ret; | |
92 | } | |
93 | ||
94 | return 0; | |
95 | } | |
96 | ||
8aec32f6 SG |
97 | static const char *const feat_name[64] = { |
98 | "limited", | |
99 | "flash", | |
100 | "pwm_fan", | |
101 | "pwm_keyb", | |
102 | "lightbar", | |
103 | "led", | |
104 | "motion_sense", | |
105 | "keyb", | |
106 | "pstore", | |
107 | "port80", | |
108 | "thermal", | |
109 | "bklight_switch", | |
110 | "wifi_switch", | |
111 | "host_events", | |
112 | "gpio", | |
113 | "i2c", | |
114 | "charger", | |
115 | "battery", | |
116 | "smart_battery", | |
117 | "hang_detect", | |
118 | "pmu", | |
119 | "sub_mcu", | |
120 | "usb_pd", | |
121 | "usb_mux", | |
122 | "motion_sense_fifo", | |
123 | "vstore", | |
124 | "usbc_ss_mux_virtual", | |
125 | "rtc", | |
126 | "fingerprint", | |
127 | "touchpad", | |
128 | "rwsig", | |
129 | "device_event", | |
130 | "unified_wake_masks", | |
131 | "host_event64", | |
132 | "exec_in_ram", | |
133 | "cec", | |
134 | "motion_sense_tight_timestamps", | |
135 | "refined_tablet_mode_hysteresis", | |
136 | "efs2", | |
137 | "scp", | |
138 | "ish", | |
139 | "typec_cmd", | |
140 | "typec_require_ap_mode_entry", | |
141 | "typec_mux_require_ap_ack", | |
142 | }; | |
143 | ||
144 | static int do_show_features(struct udevice *dev) | |
145 | { | |
146 | u64 feat; | |
147 | int ret; | |
148 | uint i; | |
149 | ||
150 | ret = cros_ec_get_features(dev, &feat); | |
151 | if (ret) | |
152 | return ret; | |
153 | for (i = 0; i < ARRAY_SIZE(feat_name); i++) { | |
154 | if (feat & (1ULL << i)) { | |
155 | if (feat_name[i]) | |
156 | printf("%s\n", feat_name[i]); | |
157 | else | |
158 | printf("unknown %d\n", i); | |
159 | } | |
160 | } | |
161 | ||
162 | return 0; | |
163 | } | |
164 | ||
3a6c994f SG |
165 | static const char *const switch_name[8] = { |
166 | "lid open", | |
167 | "power button pressed", | |
168 | "write-protect disabled", | |
169 | NULL, | |
170 | "dedicated recovery", | |
171 | NULL, | |
172 | NULL, | |
173 | NULL, | |
174 | }; | |
175 | ||
176 | static int do_show_switches(struct udevice *dev) | |
177 | { | |
178 | uint switches; | |
179 | int ret; | |
180 | uint i; | |
181 | ||
182 | ret = cros_ec_get_switches(dev); | |
183 | if (ret < 0) | |
184 | return log_msg_ret("get", ret); | |
185 | switches = ret; | |
186 | for (i = 0; i < ARRAY_SIZE(switch_name); i++) { | |
187 | uint mask = 1 << i; | |
188 | ||
189 | if (switches & mask) { | |
190 | if (switch_name[i]) | |
191 | printf("%s\n", switch_name[i]); | |
192 | else | |
193 | printf("unknown %02x\n", mask); | |
194 | } | |
195 | } | |
196 | ||
197 | return 0; | |
198 | } | |
199 | ||
09140113 SG |
200 | static int do_cros_ec(struct cmd_tbl *cmdtp, int flag, int argc, |
201 | char *const argv[]) | |
bfeba017 | 202 | { |
6322a7b6 | 203 | struct udevice *dev; |
bfeba017 MF |
204 | const char *cmd; |
205 | int ret = 0; | |
206 | ||
207 | if (argc < 2) | |
208 | return CMD_RET_USAGE; | |
209 | ||
210 | cmd = argv[1]; | |
211 | if (0 == strcmp("init", cmd)) { | |
212 | /* Remove any existing device */ | |
6322a7b6 | 213 | ret = uclass_find_device(UCLASS_CROS_EC, 0, &dev); |
bfeba017 | 214 | if (!ret) |
6322a7b6 SG |
215 | device_remove(dev, DM_REMOVE_NORMAL); |
216 | ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev); | |
bfeba017 MF |
217 | if (ret) { |
218 | printf("Could not init cros_ec device (err %d)\n", ret); | |
219 | return 1; | |
220 | } | |
221 | return 0; | |
222 | } | |
223 | ||
6322a7b6 | 224 | ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev); |
bfeba017 MF |
225 | if (ret) { |
226 | printf("Cannot get cros-ec device (err=%d)\n", ret); | |
227 | return 1; | |
228 | } | |
bfeba017 MF |
229 | if (0 == strcmp("id", cmd)) { |
230 | char id[MSG_BYTES]; | |
231 | ||
232 | if (cros_ec_read_id(dev, id, sizeof(id))) { | |
233 | debug("%s: Could not read KBC ID\n", __func__); | |
234 | return 1; | |
235 | } | |
236 | printf("%s\n", id); | |
237 | } else if (0 == strcmp("info", cmd)) { | |
238 | struct ec_response_mkbp_info info; | |
239 | ||
240 | if (cros_ec_info(dev, &info)) { | |
241 | debug("%s: Could not read KBC info\n", __func__); | |
242 | return 1; | |
243 | } | |
244 | printf("rows = %u\n", info.rows); | |
245 | printf("cols = %u\n", info.cols); | |
8aec32f6 SG |
246 | } else if (!strcmp("features", cmd)) { |
247 | ret = do_show_features(dev); | |
248 | ||
3a6c994f SG |
249 | if (ret) |
250 | printf("Error: %d\n", ret); | |
251 | } else if (!strcmp("switches", cmd)) { | |
252 | ret = do_show_switches(dev); | |
253 | ||
8aec32f6 SG |
254 | if (ret) |
255 | printf("Error: %d\n", ret); | |
bfeba017 MF |
256 | } else if (0 == strcmp("curimage", cmd)) { |
257 | enum ec_current_image image; | |
258 | ||
259 | if (cros_ec_read_current_image(dev, &image)) { | |
260 | debug("%s: Could not read KBC image\n", __func__); | |
261 | return 1; | |
262 | } | |
263 | printf("%d\n", image); | |
264 | } else if (0 == strcmp("hash", cmd)) { | |
265 | struct ec_response_vboot_hash hash; | |
266 | int i; | |
267 | ||
a12ef7e2 | 268 | if (cros_ec_read_hash(dev, EC_VBOOT_HASH_OFFSET_ACTIVE, &hash)) { |
bfeba017 MF |
269 | debug("%s: Could not read KBC hash\n", __func__); |
270 | return 1; | |
271 | } | |
272 | ||
273 | if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256) | |
274 | printf("type: SHA-256\n"); | |
275 | else | |
276 | printf("type: %d\n", hash.hash_type); | |
277 | ||
278 | printf("offset: 0x%08x\n", hash.offset); | |
279 | printf("size: 0x%08x\n", hash.size); | |
280 | ||
281 | printf("digest: "); | |
282 | for (i = 0; i < hash.digest_size; i++) | |
283 | printf("%02x", hash.hash_digest[i]); | |
284 | printf("\n"); | |
285 | } else if (0 == strcmp("reboot", cmd)) { | |
286 | int region; | |
287 | enum ec_reboot_cmd cmd; | |
288 | ||
289 | if (argc >= 3 && !strcmp(argv[2], "cold")) { | |
290 | cmd = EC_REBOOT_COLD; | |
291 | } else { | |
292 | region = cros_ec_decode_region(argc - 2, argv + 2); | |
293 | if (region == EC_FLASH_REGION_RO) | |
294 | cmd = EC_REBOOT_JUMP_RO; | |
6f1c0430 | 295 | else if (region == EC_FLASH_REGION_ACTIVE) |
bfeba017 MF |
296 | cmd = EC_REBOOT_JUMP_RW; |
297 | else | |
298 | return CMD_RET_USAGE; | |
299 | } | |
300 | ||
301 | if (cros_ec_reboot(dev, cmd, 0)) { | |
302 | debug("%s: Could not reboot KBC\n", __func__); | |
303 | return 1; | |
304 | } | |
305 | } else if (0 == strcmp("events", cmd)) { | |
306 | uint32_t events; | |
307 | ||
308 | if (cros_ec_get_host_events(dev, &events)) { | |
309 | debug("%s: Could not read host events\n", __func__); | |
310 | return 1; | |
311 | } | |
312 | printf("0x%08x\n", events); | |
313 | } else if (0 == strcmp("clrevents", cmd)) { | |
314 | uint32_t events = 0x7fffffff; | |
315 | ||
316 | if (argc >= 3) | |
317 | events = simple_strtol(argv[2], NULL, 0); | |
318 | ||
319 | if (cros_ec_clear_host_events(dev, events)) { | |
320 | debug("%s: Could not clear host events\n", __func__); | |
321 | return 1; | |
322 | } | |
323 | } else if (0 == strcmp("read", cmd)) { | |
324 | ret = do_read_write(dev, 0, argc, argv); | |
325 | if (ret > 0) | |
326 | return CMD_RET_USAGE; | |
327 | } else if (0 == strcmp("write", cmd)) { | |
328 | ret = do_read_write(dev, 1, argc, argv); | |
329 | if (ret > 0) | |
330 | return CMD_RET_USAGE; | |
331 | } else if (0 == strcmp("erase", cmd)) { | |
332 | int region = cros_ec_decode_region(argc - 2, argv + 2); | |
333 | uint32_t offset, size; | |
334 | ||
335 | if (region == -1) | |
336 | return CMD_RET_USAGE; | |
337 | if (cros_ec_flash_offset(dev, region, &offset, &size)) { | |
338 | debug("%s: Could not read region info\n", __func__); | |
339 | ret = -1; | |
340 | } else { | |
341 | ret = cros_ec_flash_erase(dev, offset, size); | |
342 | if (ret) { | |
343 | debug("%s: Could not erase region\n", | |
344 | __func__); | |
345 | } | |
346 | } | |
347 | } else if (0 == strcmp("regioninfo", cmd)) { | |
348 | int region = cros_ec_decode_region(argc - 2, argv + 2); | |
349 | uint32_t offset, size; | |
350 | ||
351 | if (region == -1) | |
352 | return CMD_RET_USAGE; | |
353 | ret = cros_ec_flash_offset(dev, region, &offset, &size); | |
354 | if (ret) { | |
355 | debug("%s: Could not read region info\n", __func__); | |
356 | } else { | |
357 | printf("Region: %s\n", region == EC_FLASH_REGION_RO ? | |
358 | "RO" : "RW"); | |
359 | printf("Offset: %x\n", offset); | |
360 | printf("Size: %x\n", size); | |
361 | } | |
362 | } else if (0 == strcmp("flashinfo", cmd)) { | |
363 | struct ec_response_flash_info p; | |
364 | ||
365 | ret = cros_ec_read_flashinfo(dev, &p); | |
366 | if (!ret) { | |
367 | printf("Flash size: %u\n", p.flash_size); | |
368 | printf("Write block size: %u\n", p.write_block_size); | |
369 | printf("Erase block size: %u\n", p.erase_block_size); | |
370 | } | |
371 | } else if (0 == strcmp("vbnvcontext", cmd)) { | |
372 | uint8_t block[EC_VBNV_BLOCK_SIZE]; | |
373 | char buf[3]; | |
374 | int i, len; | |
375 | unsigned long result; | |
376 | ||
377 | if (argc <= 2) { | |
6322a7b6 SG |
378 | ret = cros_ec_read_nvdata(dev, block, |
379 | EC_VBNV_BLOCK_SIZE); | |
bfeba017 MF |
380 | if (!ret) { |
381 | printf("vbnv_block: "); | |
382 | for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) | |
383 | printf("%02x", block[i]); | |
384 | putc('\n'); | |
385 | } | |
386 | } else { | |
387 | /* | |
388 | * TODO(clchiou): Move this to a utility function as | |
389 | * cmd_spi might want to call it. | |
390 | */ | |
391 | memset(block, 0, EC_VBNV_BLOCK_SIZE); | |
392 | len = strlen(argv[2]); | |
393 | buf[2] = '\0'; | |
394 | for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) { | |
395 | if (i * 2 >= len) | |
396 | break; | |
397 | buf[0] = argv[2][i * 2]; | |
398 | if (i * 2 + 1 >= len) | |
399 | buf[1] = '0'; | |
400 | else | |
401 | buf[1] = argv[2][i * 2 + 1]; | |
402 | strict_strtoul(buf, 16, &result); | |
403 | block[i] = result; | |
404 | } | |
6322a7b6 SG |
405 | ret = cros_ec_write_nvdata(dev, block, |
406 | EC_VBNV_BLOCK_SIZE); | |
bfeba017 MF |
407 | } |
408 | if (ret) { | |
409 | debug("%s: Could not %s VbNvContext\n", __func__, | |
410 | argc <= 2 ? "read" : "write"); | |
411 | } | |
412 | } else if (0 == strcmp("test", cmd)) { | |
413 | int result = cros_ec_test(dev); | |
414 | ||
415 | if (result) | |
416 | printf("Test failed with error %d\n", result); | |
417 | else | |
418 | puts("Test passed\n"); | |
419 | } else if (0 == strcmp("version", cmd)) { | |
420 | struct ec_response_get_version *p; | |
421 | char *build_string; | |
422 | ||
423 | ret = cros_ec_read_version(dev, &p); | |
424 | if (!ret) { | |
425 | /* Print versions */ | |
426 | printf("RO version: %1.*s\n", | |
427 | (int)sizeof(p->version_string_ro), | |
428 | p->version_string_ro); | |
429 | printf("RW version: %1.*s\n", | |
430 | (int)sizeof(p->version_string_rw), | |
431 | p->version_string_rw); | |
432 | printf("Firmware copy: %s\n", | |
433 | (p->current_image < | |
434 | ARRAY_SIZE(ec_current_image_name) ? | |
435 | ec_current_image_name[p->current_image] : | |
436 | "?")); | |
437 | ret = cros_ec_read_build_info(dev, &build_string); | |
438 | if (!ret) | |
439 | printf("Build info: %s\n", build_string); | |
440 | } | |
441 | } else if (0 == strcmp("ldo", cmd)) { | |
442 | uint8_t index, state; | |
443 | char *endp; | |
444 | ||
445 | if (argc < 3) | |
446 | return CMD_RET_USAGE; | |
447 | index = simple_strtoul(argv[2], &endp, 10); | |
448 | if (*argv[2] == 0 || *endp != 0) | |
449 | return CMD_RET_USAGE; | |
450 | if (argc > 3) { | |
451 | state = simple_strtoul(argv[3], &endp, 10); | |
452 | if (*argv[3] == 0 || *endp != 0) | |
453 | return CMD_RET_USAGE; | |
6322a7b6 | 454 | ret = cros_ec_set_ldo(dev, index, state); |
bfeba017 | 455 | } else { |
6322a7b6 | 456 | ret = cros_ec_get_ldo(dev, index, &state); |
bfeba017 MF |
457 | if (!ret) { |
458 | printf("LDO%d: %s\n", index, | |
459 | state == EC_LDO_STATE_ON ? | |
460 | "on" : "off"); | |
461 | } | |
462 | } | |
463 | ||
464 | if (ret) { | |
465 | debug("%s: Could not access LDO%d\n", __func__, index); | |
466 | return ret; | |
467 | } | |
7791df57 SG |
468 | } else if (!strcmp("sku", cmd)) { |
469 | ret = cros_ec_get_sku_id(dev); | |
470 | ||
471 | if (ret >= 0) { | |
472 | printf("%d\n", ret); | |
473 | ret = 0; | |
474 | } else { | |
475 | printf("Error: %d\n", ret); | |
476 | } | |
bfeba017 MF |
477 | } else { |
478 | return CMD_RET_USAGE; | |
479 | } | |
480 | ||
481 | if (ret < 0) { | |
482 | printf("Error: CROS-EC command failed (error %d)\n", ret); | |
483 | ret = 1; | |
484 | } | |
485 | ||
486 | return ret; | |
487 | } | |
488 | ||
489 | U_BOOT_CMD( | |
490 | crosec, 6, 1, do_cros_ec, | |
491 | "CROS-EC utility command", | |
492 | "init Re-init CROS-EC (done on startup automatically)\n" | |
493 | "crosec id Read CROS-EC ID\n" | |
494 | "crosec info Read CROS-EC info\n" | |
8aec32f6 | 495 | "crosec features Read CROS-EC features\n" |
3a6c994f | 496 | "crosec switches Read CROS-EC switches\n" |
bfeba017 MF |
497 | "crosec curimage Read CROS-EC current image\n" |
498 | "crosec hash Read CROS-EC hash\n" | |
499 | "crosec reboot [rw | ro | cold] Reboot CROS-EC\n" | |
500 | "crosec events Read CROS-EC host events\n" | |
501 | "crosec clrevents [mask] Clear CROS-EC host events\n" | |
502 | "crosec regioninfo <ro|rw> Read image info\n" | |
503 | "crosec flashinfo Read flash info\n" | |
504 | "crosec erase <ro|rw> Erase EC image\n" | |
505 | "crosec read <ro|rw> <addr> [<size>] Read EC image\n" | |
506 | "crosec write <ro|rw> <addr> [<size>] Write EC image\n" | |
507 | "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n" | |
508 | "crosec ldo <idx> [<state>] Switch/Read LDO state\n" | |
7791df57 | 509 | "crosec sku Read board SKU ID\n" |
bfeba017 MF |
510 | "crosec test run tests on cros_ec\n" |
511 | "crosec version Read CROS-EC version" | |
512 | ); |