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
14 #include <dm/device-internal.h>
15 #include <dm/uclass-internal.h>
17 /* Note: depends on enum ec_current_image */
18 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
21 * Decode a flash region parameter
23 * @param argc Number of params remaining
24 * @param argv List of remaining parameters
25 * Return: flash region (EC_FLASH_REGION_...) or -1 on error
27 static int cros_ec_decode_region(int argc, char *const argv[])
30 if (0 == strcmp(*argv, "rw"))
31 return EC_FLASH_REGION_ACTIVE;
32 else if (0 == strcmp(*argv, "ro"))
33 return EC_FLASH_REGION_RO;
35 debug("%s: Invalid region '%s'\n", __func__, *argv);
37 debug("%s: Missing region parameter\n", __func__);
44 * Perform a flash read or write command
46 * @param dev CROS-EC device to read/write
47 * @param is_write 1 do to a write, 0 to do a read
48 * @param argc Number of arguments
49 * @param argv Arguments (2 is region, 3 is address)
50 * Return: 0 for ok, 1 for a usage error or -ve for ec command error
51 * (negative EC_RES_...)
53 static int do_read_write(struct udevice *dev, int is_write, int argc,
56 uint32_t offset, size = -1U, region_size;
62 region = cros_ec_decode_region(argc - 2, argv + 2);
67 addr = hextoul(argv[3], &endp);
68 if (*argv[3] == 0 || *endp != 0)
71 size = hextoul(argv[4], &endp);
72 if (*argv[4] == 0 || *endp != 0)
76 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
78 debug("%s: Could not read region info\n", __func__);
85 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
86 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
88 debug("%s: Could not %s region\n", __func__,
89 is_write ? "write" : "read");
96 static const char *const feat_name[64] = {
123 "usbc_ss_mux_virtual",
129 "unified_wake_masks",
133 "motion_sense_tight_timestamps",
134 "refined_tablet_mode_hysteresis",
139 "typec_require_ap_mode_entry",
140 "typec_mux_require_ap_ack",
143 static int do_show_features(struct udevice *dev)
149 ret = cros_ec_get_features(dev, &feat);
152 for (i = 0; i < ARRAY_SIZE(feat_name); i++) {
153 if (feat & (1ULL << i)) {
155 printf("%s\n", feat_name[i]);
157 printf("unknown %d\n", i);
164 static const char *const switch_name[8] = {
166 "power button pressed",
167 "write-protect disabled",
169 "dedicated recovery",
175 static int do_show_switches(struct udevice *dev)
181 ret = cros_ec_get_switches(dev);
183 return log_msg_ret("get", ret);
185 for (i = 0; i < ARRAY_SIZE(switch_name); i++) {
188 if (switches & mask) {
190 printf("%s\n", switch_name[i]);
192 printf("unknown %02x\n", mask);
199 static const char *const event_name[] = {
229 "keyboard_recovery_hw_reinit",
234 static int do_show_events(struct udevice *dev)
240 ret = cros_ec_get_host_events(dev, &events);
243 printf("%08x\n", events);
244 for (i = 0; i < ARRAY_SIZE(event_name); i++) {
245 enum host_event_code code = i + 1;
246 u64 mask = EC_HOST_EVENT_MASK(code);
250 printf("%s\n", event_name[i]);
252 printf("unknown code %#x\n", code);
259 static int do_cros_ec(struct cmd_tbl *cmdtp, int flag, int argc,
267 return CMD_RET_USAGE;
270 if (0 == strcmp("init", cmd)) {
271 /* Remove any existing device */
272 ret = uclass_find_device(UCLASS_CROS_EC, 0, &dev);
274 device_remove(dev, DM_REMOVE_NORMAL);
275 ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
277 printf("Could not init cros_ec device (err %d)\n", ret);
283 ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
285 printf("Cannot get cros-ec device (err=%d)\n", ret);
288 if (0 == strcmp("id", cmd)) {
291 if (cros_ec_read_id(dev, id, sizeof(id))) {
292 debug("%s: Could not read KBC ID\n", __func__);
296 } else if (0 == strcmp("info", cmd)) {
297 struct ec_response_mkbp_info info;
299 if (cros_ec_info(dev, &info)) {
300 debug("%s: Could not read KBC info\n", __func__);
303 printf("rows = %u\n", info.rows);
304 printf("cols = %u\n", info.cols);
305 } else if (!strcmp("features", cmd)) {
306 ret = do_show_features(dev);
309 printf("Error: %d\n", ret);
310 } else if (!strcmp("switches", cmd)) {
311 ret = do_show_switches(dev);
314 printf("Error: %d\n", ret);
315 } else if (0 == strcmp("curimage", cmd)) {
316 enum ec_current_image image;
318 if (cros_ec_read_current_image(dev, &image)) {
319 debug("%s: Could not read KBC image\n", __func__);
322 printf("%d\n", image);
323 } else if (0 == strcmp("hash", cmd)) {
324 struct ec_response_vboot_hash hash;
327 if (cros_ec_read_hash(dev, EC_VBOOT_HASH_OFFSET_ACTIVE, &hash)) {
328 debug("%s: Could not read KBC hash\n", __func__);
332 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
333 printf("type: SHA-256\n");
335 printf("type: %d\n", hash.hash_type);
337 printf("offset: 0x%08x\n", hash.offset);
338 printf("size: 0x%08x\n", hash.size);
341 for (i = 0; i < hash.digest_size; i++)
342 printf("%02x", hash.hash_digest[i]);
344 } else if (0 == strcmp("reboot", cmd)) {
346 enum ec_reboot_cmd cmd;
348 if (argc >= 3 && !strcmp(argv[2], "cold")) {
349 cmd = EC_REBOOT_COLD;
351 region = cros_ec_decode_region(argc - 2, argv + 2);
352 if (region == EC_FLASH_REGION_RO)
353 cmd = EC_REBOOT_JUMP_RO;
354 else if (region == EC_FLASH_REGION_ACTIVE)
355 cmd = EC_REBOOT_JUMP_RW;
357 return CMD_RET_USAGE;
360 if (cros_ec_reboot(dev, cmd, 0)) {
361 debug("%s: Could not reboot KBC\n", __func__);
364 } else if (0 == strcmp("events", cmd)) {
365 ret = do_show_events(dev);
368 printf("Error: %d\n", ret);
369 } else if (0 == strcmp("clrevents", cmd)) {
370 uint32_t events = 0x7fffffff;
373 events = simple_strtol(argv[2], NULL, 0);
375 if (cros_ec_clear_host_events(dev, events)) {
376 debug("%s: Could not clear host events\n", __func__);
379 } else if (0 == strcmp("read", cmd)) {
380 ret = do_read_write(dev, 0, argc, argv);
382 return CMD_RET_USAGE;
383 } else if (0 == strcmp("write", cmd)) {
384 ret = do_read_write(dev, 1, argc, argv);
386 return CMD_RET_USAGE;
387 } else if (0 == strcmp("erase", cmd)) {
388 int region = cros_ec_decode_region(argc - 2, argv + 2);
389 uint32_t offset, size;
392 return CMD_RET_USAGE;
393 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
394 debug("%s: Could not read region info\n", __func__);
397 ret = cros_ec_flash_erase(dev, offset, size);
399 debug("%s: Could not erase region\n",
403 } else if (0 == strcmp("regioninfo", cmd)) {
404 int region = cros_ec_decode_region(argc - 2, argv + 2);
405 uint32_t offset, size;
408 return CMD_RET_USAGE;
409 ret = cros_ec_flash_offset(dev, region, &offset, &size);
411 debug("%s: Could not read region info\n", __func__);
413 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
415 printf("Offset: %x\n", offset);
416 printf("Size: %x\n", size);
418 } else if (0 == strcmp("flashinfo", cmd)) {
419 struct ec_response_flash_info p;
421 ret = cros_ec_read_flashinfo(dev, &p);
423 printf("Flash size: %u\n", p.flash_size);
424 printf("Write block size: %u\n", p.write_block_size);
425 printf("Erase block size: %u\n", p.erase_block_size);
427 } else if (0 == strcmp("vbnvcontext", cmd)) {
428 uint8_t block[EC_VBNV_BLOCK_SIZE];
431 unsigned long result;
434 ret = cros_ec_read_nvdata(dev, block,
437 printf("vbnv_block: ");
438 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
439 printf("%02x", block[i]);
444 * TODO(clchiou): Move this to a utility function as
445 * cmd_spi might want to call it.
447 memset(block, 0, EC_VBNV_BLOCK_SIZE);
448 len = strlen(argv[2]);
450 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
453 buf[0] = argv[2][i * 2];
454 if (i * 2 + 1 >= len)
457 buf[1] = argv[2][i * 2 + 1];
458 strict_strtoul(buf, 16, &result);
461 ret = cros_ec_write_nvdata(dev, block,
465 debug("%s: Could not %s VbNvContext\n", __func__,
466 argc <= 2 ? "read" : "write");
468 } else if (0 == strcmp("test", cmd)) {
469 int result = cros_ec_test(dev);
472 printf("Test failed with error %d\n", result);
474 puts("Test passed\n");
475 } else if (0 == strcmp("version", cmd)) {
476 struct ec_response_get_version *p;
479 ret = cros_ec_read_version(dev, &p);
482 printf("RO version: %1.*s\n",
483 (int)sizeof(p->version_string_ro),
484 p->version_string_ro);
485 printf("RW version: %1.*s\n",
486 (int)sizeof(p->version_string_rw),
487 p->version_string_rw);
488 printf("Firmware copy: %s\n",
490 ARRAY_SIZE(ec_current_image_name) ?
491 ec_current_image_name[p->current_image] :
493 ret = cros_ec_read_build_info(dev, &build_string);
495 printf("Build info: %s\n", build_string);
497 } else if (0 == strcmp("ldo", cmd)) {
498 uint8_t index, state;
502 return CMD_RET_USAGE;
503 index = dectoul(argv[2], &endp);
504 if (*argv[2] == 0 || *endp != 0)
505 return CMD_RET_USAGE;
507 state = dectoul(argv[3], &endp);
508 if (*argv[3] == 0 || *endp != 0)
509 return CMD_RET_USAGE;
510 ret = cros_ec_set_ldo(dev, index, state);
512 ret = cros_ec_get_ldo(dev, index, &state);
514 printf("LDO%d: %s\n", index,
515 state == EC_LDO_STATE_ON ?
521 debug("%s: Could not access LDO%d\n", __func__, index);
524 } else if (!strcmp("sku", cmd)) {
525 ret = cros_ec_get_sku_id(dev);
531 printf("Error: %d\n", ret);
534 return CMD_RET_USAGE;
538 printf("Error: CROS-EC command failed (error %d)\n", ret);
546 crosec, 6, 1, do_cros_ec,
547 "CROS-EC utility command",
548 "init Re-init CROS-EC (done on startup automatically)\n"
549 "crosec id Read CROS-EC ID\n"
550 "crosec info Read CROS-EC info\n"
551 "crosec features Read CROS-EC features\n"
552 "crosec switches Read CROS-EC switches\n"
553 "crosec curimage Read CROS-EC current image\n"
554 "crosec hash Read CROS-EC hash\n"
555 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
556 "crosec events Read CROS-EC host events\n"
557 "crosec eventsb Read CROS-EC host events_b\n"
558 "crosec clrevents [mask] Clear CROS-EC host events\n"
559 "crosec regioninfo <ro|rw> Read image info\n"
560 "crosec flashinfo Read flash info\n"
561 "crosec erase <ro|rw> Erase EC image\n"
562 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
563 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
564 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
565 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
566 "crosec sku Read board SKU ID\n"
567 "crosec test run tests on cros_ec\n"
568 "crosec version Read CROS-EC version"