1 // SPDX-License-Identifier: GPL-2.0+
3 * Chromium OS cros_ec driver
5 * Copyright (c) 2016 The Chromium OS Authors.
6 * Copyright (c) 2016 National Instruments Corp
13 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
16 /* Note: depends on enum ec_current_image */
17 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
20 * Decode a flash region parameter
22 * @param argc Number of params remaining
23 * @param argv List of remaining parameters
24 * Return: flash region (EC_FLASH_REGION_...) or -1 on error
26 static int cros_ec_decode_region(int argc, char *const argv[])
29 if (0 == strcmp(*argv, "rw"))
30 return EC_FLASH_REGION_ACTIVE;
31 else if (0 == strcmp(*argv, "ro"))
32 return EC_FLASH_REGION_RO;
34 debug("%s: Invalid region '%s'\n", __func__, *argv);
36 debug("%s: Missing region parameter\n", __func__);
43 * Perform a flash read or write command
45 * @param dev CROS-EC device to read/write
46 * @param is_write 1 do to a write, 0 to do a read
47 * @param argc Number of arguments
48 * @param argv Arguments (2 is region, 3 is address)
49 * Return: 0 for ok, 1 for a usage error or -ve for ec command error
50 * (negative EC_RES_...)
52 static int do_read_write(struct udevice *dev, int is_write, int argc,
55 uint32_t offset, size = -1U, region_size;
61 region = cros_ec_decode_region(argc - 2, argv + 2);
66 addr = hextoul(argv[3], &endp);
67 if (*argv[3] == 0 || *endp != 0)
70 size = hextoul(argv[4], &endp);
71 if (*argv[4] == 0 || *endp != 0)
75 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
77 debug("%s: Could not read region info\n", __func__);
84 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
85 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
87 debug("%s: Could not %s region\n", __func__,
88 is_write ? "write" : "read");
95 static const char *const feat_name[64] = {
122 "usbc_ss_mux_virtual",
128 "unified_wake_masks",
132 "motion_sense_tight_timestamps",
133 "refined_tablet_mode_hysteresis",
138 "typec_require_ap_mode_entry",
139 "typec_mux_require_ap_ack",
142 static int do_show_features(struct udevice *dev)
148 ret = cros_ec_get_features(dev, &feat);
151 for (i = 0; i < ARRAY_SIZE(feat_name); i++) {
152 if (feat & (1ULL << i)) {
154 printf("%s\n", feat_name[i]);
156 printf("unknown %d\n", i);
163 static const char *const switch_name[8] = {
165 "power button pressed",
166 "write-protect disabled",
168 "dedicated recovery",
174 static int do_show_switches(struct udevice *dev)
180 ret = cros_ec_get_switches(dev);
182 return log_msg_ret("get", ret);
184 for (i = 0; i < ARRAY_SIZE(switch_name); i++) {
187 if (switches & mask) {
189 printf("%s\n", switch_name[i]);
191 printf("unknown %02x\n", mask);
198 static const char *const event_name[] = {
228 "keyboard_recovery_hw_reinit",
233 static int do_show_events(struct udevice *dev)
239 ret = cros_ec_get_host_events(dev, &events);
242 printf("%08x\n", events);
243 for (i = 0; i < ARRAY_SIZE(event_name); i++) {
244 enum host_event_code code = i + 1;
245 u64 mask = EC_HOST_EVENT_MASK(code);
249 printf("%s\n", event_name[i]);
251 printf("unknown code %#x\n", code);
258 static int do_cros_ec(struct cmd_tbl *cmdtp, int flag, int argc,
266 return CMD_RET_USAGE;
269 if (0 == strcmp("init", cmd)) {
270 /* Remove any existing device */
271 ret = uclass_find_device(UCLASS_CROS_EC, 0, &dev);
273 device_remove(dev, DM_REMOVE_NORMAL);
274 ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
276 printf("Could not init cros_ec device (err %d)\n", ret);
282 ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
284 printf("Cannot get cros-ec device (err=%d)\n", ret);
287 if (0 == strcmp("id", cmd)) {
290 if (cros_ec_read_id(dev, id, sizeof(id))) {
291 debug("%s: Could not read KBC ID\n", __func__);
295 } else if (0 == strcmp("info", cmd)) {
296 struct ec_response_mkbp_info info;
298 if (cros_ec_info(dev, &info)) {
299 debug("%s: Could not read KBC info\n", __func__);
302 printf("rows = %u\n", info.rows);
303 printf("cols = %u\n", info.cols);
304 } else if (!strcmp("features", cmd)) {
305 ret = do_show_features(dev);
308 printf("Error: %d\n", ret);
309 } else if (!strcmp("switches", cmd)) {
310 ret = do_show_switches(dev);
313 printf("Error: %d\n", ret);
314 } else if (0 == strcmp("curimage", cmd)) {
315 enum ec_current_image image;
317 if (cros_ec_read_current_image(dev, &image)) {
318 debug("%s: Could not read KBC image\n", __func__);
321 printf("%d\n", image);
322 } else if (0 == strcmp("hash", cmd)) {
323 struct ec_response_vboot_hash hash;
326 if (cros_ec_read_hash(dev, EC_VBOOT_HASH_OFFSET_ACTIVE, &hash)) {
327 debug("%s: Could not read KBC hash\n", __func__);
331 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
332 printf("type: SHA-256\n");
334 printf("type: %d\n", hash.hash_type);
336 printf("offset: 0x%08x\n", hash.offset);
337 printf("size: 0x%08x\n", hash.size);
340 for (i = 0; i < hash.digest_size; i++)
341 printf("%02x", hash.hash_digest[i]);
343 } else if (0 == strcmp("reboot", cmd)) {
345 enum ec_reboot_cmd cmd;
347 if (argc >= 3 && !strcmp(argv[2], "cold")) {
348 cmd = EC_REBOOT_COLD;
350 region = cros_ec_decode_region(argc - 2, argv + 2);
351 if (region == EC_FLASH_REGION_RO)
352 cmd = EC_REBOOT_JUMP_RO;
353 else if (region == EC_FLASH_REGION_ACTIVE)
354 cmd = EC_REBOOT_JUMP_RW;
356 return CMD_RET_USAGE;
359 if (cros_ec_reboot(dev, cmd, 0)) {
360 debug("%s: Could not reboot KBC\n", __func__);
363 } else if (0 == strcmp("events", cmd)) {
364 ret = do_show_events(dev);
367 printf("Error: %d\n", ret);
368 } else if (0 == strcmp("clrevents", cmd)) {
369 uint32_t events = 0x7fffffff;
372 events = simple_strtol(argv[2], NULL, 0);
374 if (cros_ec_clear_host_events(dev, events)) {
375 debug("%s: Could not clear host events\n", __func__);
378 } else if (0 == strcmp("read", cmd)) {
379 ret = do_read_write(dev, 0, argc, argv);
381 return CMD_RET_USAGE;
382 } else if (0 == strcmp("write", cmd)) {
383 ret = do_read_write(dev, 1, argc, argv);
385 return CMD_RET_USAGE;
386 } else if (0 == strcmp("erase", cmd)) {
387 int region = cros_ec_decode_region(argc - 2, argv + 2);
388 uint32_t offset, size;
391 return CMD_RET_USAGE;
392 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
393 debug("%s: Could not read region info\n", __func__);
396 ret = cros_ec_flash_erase(dev, offset, size);
398 debug("%s: Could not erase region\n",
402 } else if (0 == strcmp("regioninfo", cmd)) {
403 int region = cros_ec_decode_region(argc - 2, argv + 2);
404 uint32_t offset, size;
407 return CMD_RET_USAGE;
408 ret = cros_ec_flash_offset(dev, region, &offset, &size);
410 debug("%s: Could not read region info\n", __func__);
412 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
414 printf("Offset: %x\n", offset);
415 printf("Size: %x\n", size);
417 } else if (0 == strcmp("flashinfo", cmd)) {
418 struct ec_response_flash_info p;
420 ret = cros_ec_read_flashinfo(dev, &p);
422 printf("Flash size: %u\n", p.flash_size);
423 printf("Write block size: %u\n", p.write_block_size);
424 printf("Erase block size: %u\n", p.erase_block_size);
426 } else if (0 == strcmp("vbnvcontext", cmd)) {
427 uint8_t block[EC_VBNV_BLOCK_SIZE];
430 unsigned long result;
433 ret = cros_ec_read_nvdata(dev, block,
436 printf("vbnv_block: ");
437 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
438 printf("%02x", block[i]);
443 * TODO(clchiou): Move this to a utility function as
444 * cmd_spi might want to call it.
446 memset(block, 0, EC_VBNV_BLOCK_SIZE);
447 len = strlen(argv[2]);
449 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
452 buf[0] = argv[2][i * 2];
453 if (i * 2 + 1 >= len)
456 buf[1] = argv[2][i * 2 + 1];
457 strict_strtoul(buf, 16, &result);
460 ret = cros_ec_write_nvdata(dev, block,
464 debug("%s: Could not %s VbNvContext\n", __func__,
465 argc <= 2 ? "read" : "write");
467 } else if (0 == strcmp("test", cmd)) {
468 int result = cros_ec_test(dev);
471 printf("Test failed with error %d\n", result);
473 puts("Test passed\n");
474 } else if (0 == strcmp("version", cmd)) {
475 struct ec_response_get_version *p;
478 ret = cros_ec_read_version(dev, &p);
481 printf("RO version: %1.*s\n",
482 (int)sizeof(p->version_string_ro),
483 p->version_string_ro);
484 printf("RW version: %1.*s\n",
485 (int)sizeof(p->version_string_rw),
486 p->version_string_rw);
487 printf("Firmware copy: %s\n",
489 ARRAY_SIZE(ec_current_image_name) ?
490 ec_current_image_name[p->current_image] :
492 ret = cros_ec_read_build_info(dev, &build_string);
494 printf("Build info: %s\n", build_string);
496 } else if (0 == strcmp("ldo", cmd)) {
497 uint8_t index, state;
501 return CMD_RET_USAGE;
502 index = dectoul(argv[2], &endp);
503 if (*argv[2] == 0 || *endp != 0)
504 return CMD_RET_USAGE;
506 state = dectoul(argv[3], &endp);
507 if (*argv[3] == 0 || *endp != 0)
508 return CMD_RET_USAGE;
509 ret = cros_ec_set_ldo(dev, index, state);
511 ret = cros_ec_get_ldo(dev, index, &state);
513 printf("LDO%d: %s\n", index,
514 state == EC_LDO_STATE_ON ?
520 debug("%s: Could not access LDO%d\n", __func__, index);
523 } else if (!strcmp("sku", cmd)) {
524 ret = cros_ec_get_sku_id(dev);
530 printf("Error: %d\n", ret);
533 return CMD_RET_USAGE;
537 printf("Error: CROS-EC command failed (error %d)\n", ret);
545 crosec, 6, 1, do_cros_ec,
546 "CROS-EC utility command",
547 "init Re-init CROS-EC (done on startup automatically)\n"
548 "crosec id Read CROS-EC ID\n"
549 "crosec info Read CROS-EC info\n"
550 "crosec features Read CROS-EC features\n"
551 "crosec switches Read CROS-EC switches\n"
552 "crosec curimage Read CROS-EC current image\n"
553 "crosec hash Read CROS-EC hash\n"
554 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
555 "crosec events Read CROS-EC host events\n"
556 "crosec eventsb Read CROS-EC host events_b\n"
557 "crosec clrevents [mask] Clear CROS-EC host events\n"
558 "crosec regioninfo <ro|rw> Read image info\n"
559 "crosec flashinfo Read flash info\n"
560 "crosec erase <ro|rw> Erase EC image\n"
561 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
562 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
563 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
564 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
565 "crosec sku Read board SKU ID\n"
566 "crosec test run tests on cros_ec\n"
567 "crosec version Read CROS-EC version"