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
15 #include <dm/device-internal.h>
16 #include <dm/uclass-internal.h>
18 /* Note: depends on enum ec_current_image */
19 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
22 * Decode a flash region parameter
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
28 static int cros_ec_decode_region(int argc, char *const argv[])
31 if (0 == strcmp(*argv, "rw"))
32 return EC_FLASH_REGION_ACTIVE;
33 else if (0 == strcmp(*argv, "ro"))
34 return EC_FLASH_REGION_RO;
36 debug("%s: Invalid region '%s'\n", __func__, *argv);
38 debug("%s: Missing region parameter\n", __func__);
45 * Perform a flash read or write command
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_...)
54 static int do_read_write(struct udevice *dev, int is_write, int argc,
57 uint32_t offset, size = -1U, region_size;
63 region = cros_ec_decode_region(argc - 2, argv + 2);
68 addr = hextoul(argv[3], &endp);
69 if (*argv[3] == 0 || *endp != 0)
72 size = hextoul(argv[4], &endp);
73 if (*argv[4] == 0 || *endp != 0)
77 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
79 debug("%s: Could not read region info\n", __func__);
86 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
87 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
89 debug("%s: Could not %s region\n", __func__,
90 is_write ? "write" : "read");
97 static const char *const feat_name[64] = {
124 "usbc_ss_mux_virtual",
130 "unified_wake_masks",
134 "motion_sense_tight_timestamps",
135 "refined_tablet_mode_hysteresis",
140 "typec_require_ap_mode_entry",
141 "typec_mux_require_ap_ack",
144 static int do_show_features(struct udevice *dev)
150 ret = cros_ec_get_features(dev, &feat);
153 for (i = 0; i < ARRAY_SIZE(feat_name); i++) {
154 if (feat & (1ULL << i)) {
156 printf("%s\n", feat_name[i]);
158 printf("unknown %d\n", i);
165 static const char *const switch_name[8] = {
167 "power button pressed",
168 "write-protect disabled",
170 "dedicated recovery",
176 static int do_show_switches(struct udevice *dev)
182 ret = cros_ec_get_switches(dev);
184 return log_msg_ret("get", ret);
186 for (i = 0; i < ARRAY_SIZE(switch_name); i++) {
189 if (switches & mask) {
191 printf("%s\n", switch_name[i]);
193 printf("unknown %02x\n", mask);
200 static const char *const event_name[] = {
230 "keyboard_recovery_hw_reinit",
235 static int do_show_events(struct udevice *dev)
241 ret = cros_ec_get_host_events(dev, &events);
244 printf("%08x\n", events);
245 for (i = 0; i < ARRAY_SIZE(event_name); i++) {
246 enum host_event_code code = i + 1;
247 u64 mask = EC_HOST_EVENT_MASK(code);
251 printf("%s\n", event_name[i]);
253 printf("unknown code %#x\n", code);
260 static int do_cros_ec(struct cmd_tbl *cmdtp, int flag, int argc,
268 return CMD_RET_USAGE;
271 if (0 == strcmp("init", cmd)) {
272 /* Remove any existing device */
273 ret = uclass_find_device(UCLASS_CROS_EC, 0, &dev);
275 device_remove(dev, DM_REMOVE_NORMAL);
276 ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
278 printf("Could not init cros_ec device (err %d)\n", ret);
284 ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
286 printf("Cannot get cros-ec device (err=%d)\n", ret);
289 if (0 == strcmp("id", cmd)) {
292 if (cros_ec_read_id(dev, id, sizeof(id))) {
293 debug("%s: Could not read KBC ID\n", __func__);
297 } else if (0 == strcmp("info", cmd)) {
298 struct ec_response_mkbp_info info;
300 if (cros_ec_info(dev, &info)) {
301 debug("%s: Could not read KBC info\n", __func__);
304 printf("rows = %u\n", info.rows);
305 printf("cols = %u\n", info.cols);
306 } else if (!strcmp("features", cmd)) {
307 ret = do_show_features(dev);
310 printf("Error: %d\n", ret);
311 } else if (!strcmp("switches", cmd)) {
312 ret = do_show_switches(dev);
315 printf("Error: %d\n", ret);
316 } else if (0 == strcmp("curimage", cmd)) {
317 enum ec_current_image image;
319 if (cros_ec_read_current_image(dev, &image)) {
320 debug("%s: Could not read KBC image\n", __func__);
323 printf("%d\n", image);
324 } else if (0 == strcmp("hash", cmd)) {
325 struct ec_response_vboot_hash hash;
328 if (cros_ec_read_hash(dev, EC_VBOOT_HASH_OFFSET_ACTIVE, &hash)) {
329 debug("%s: Could not read KBC hash\n", __func__);
333 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
334 printf("type: SHA-256\n");
336 printf("type: %d\n", hash.hash_type);
338 printf("offset: 0x%08x\n", hash.offset);
339 printf("size: 0x%08x\n", hash.size);
342 for (i = 0; i < hash.digest_size; i++)
343 printf("%02x", hash.hash_digest[i]);
345 } else if (0 == strcmp("reboot", cmd)) {
347 enum ec_reboot_cmd cmd;
349 if (argc >= 3 && !strcmp(argv[2], "cold")) {
350 cmd = EC_REBOOT_COLD;
352 region = cros_ec_decode_region(argc - 2, argv + 2);
353 if (region == EC_FLASH_REGION_RO)
354 cmd = EC_REBOOT_JUMP_RO;
355 else if (region == EC_FLASH_REGION_ACTIVE)
356 cmd = EC_REBOOT_JUMP_RW;
358 return CMD_RET_USAGE;
361 if (cros_ec_reboot(dev, cmd, 0)) {
362 debug("%s: Could not reboot KBC\n", __func__);
365 } else if (0 == strcmp("events", cmd)) {
366 ret = do_show_events(dev);
369 printf("Error: %d\n", ret);
370 } else if (0 == strcmp("clrevents", cmd)) {
371 uint32_t events = 0x7fffffff;
374 events = simple_strtol(argv[2], NULL, 0);
376 if (cros_ec_clear_host_events(dev, events)) {
377 debug("%s: Could not clear host events\n", __func__);
380 } else if (0 == strcmp("read", cmd)) {
381 ret = do_read_write(dev, 0, argc, argv);
383 return CMD_RET_USAGE;
384 } else if (0 == strcmp("write", cmd)) {
385 ret = do_read_write(dev, 1, argc, argv);
387 return CMD_RET_USAGE;
388 } else if (0 == strcmp("erase", cmd)) {
389 int region = cros_ec_decode_region(argc - 2, argv + 2);
390 uint32_t offset, size;
393 return CMD_RET_USAGE;
394 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
395 debug("%s: Could not read region info\n", __func__);
398 ret = cros_ec_flash_erase(dev, offset, size);
400 debug("%s: Could not erase region\n",
404 } else if (0 == strcmp("regioninfo", cmd)) {
405 int region = cros_ec_decode_region(argc - 2, argv + 2);
406 uint32_t offset, size;
409 return CMD_RET_USAGE;
410 ret = cros_ec_flash_offset(dev, region, &offset, &size);
412 debug("%s: Could not read region info\n", __func__);
414 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
416 printf("Offset: %x\n", offset);
417 printf("Size: %x\n", size);
419 } else if (0 == strcmp("flashinfo", cmd)) {
420 struct ec_response_flash_info p;
422 ret = cros_ec_read_flashinfo(dev, &p);
424 printf("Flash size: %u\n", p.flash_size);
425 printf("Write block size: %u\n", p.write_block_size);
426 printf("Erase block size: %u\n", p.erase_block_size);
428 } else if (0 == strcmp("vbnvcontext", cmd)) {
429 uint8_t block[EC_VBNV_BLOCK_SIZE];
432 unsigned long result;
435 ret = cros_ec_read_nvdata(dev, block,
438 printf("vbnv_block: ");
439 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
440 printf("%02x", block[i]);
445 * TODO(clchiou): Move this to a utility function as
446 * cmd_spi might want to call it.
448 memset(block, 0, EC_VBNV_BLOCK_SIZE);
449 len = strlen(argv[2]);
451 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
454 buf[0] = argv[2][i * 2];
455 if (i * 2 + 1 >= len)
458 buf[1] = argv[2][i * 2 + 1];
459 strict_strtoul(buf, 16, &result);
462 ret = cros_ec_write_nvdata(dev, block,
466 debug("%s: Could not %s VbNvContext\n", __func__,
467 argc <= 2 ? "read" : "write");
469 } else if (0 == strcmp("test", cmd)) {
470 int result = cros_ec_test(dev);
473 printf("Test failed with error %d\n", result);
475 puts("Test passed\n");
476 } else if (0 == strcmp("version", cmd)) {
477 struct ec_response_get_version *p;
480 ret = cros_ec_read_version(dev, &p);
483 printf("RO version: %1.*s\n",
484 (int)sizeof(p->version_string_ro),
485 p->version_string_ro);
486 printf("RW version: %1.*s\n",
487 (int)sizeof(p->version_string_rw),
488 p->version_string_rw);
489 printf("Firmware copy: %s\n",
491 ARRAY_SIZE(ec_current_image_name) ?
492 ec_current_image_name[p->current_image] :
494 ret = cros_ec_read_build_info(dev, &build_string);
496 printf("Build info: %s\n", build_string);
498 } else if (0 == strcmp("ldo", cmd)) {
499 uint8_t index, state;
503 return CMD_RET_USAGE;
504 index = dectoul(argv[2], &endp);
505 if (*argv[2] == 0 || *endp != 0)
506 return CMD_RET_USAGE;
508 state = dectoul(argv[3], &endp);
509 if (*argv[3] == 0 || *endp != 0)
510 return CMD_RET_USAGE;
511 ret = cros_ec_set_ldo(dev, index, state);
513 ret = cros_ec_get_ldo(dev, index, &state);
515 printf("LDO%d: %s\n", index,
516 state == EC_LDO_STATE_ON ?
522 debug("%s: Could not access LDO%d\n", __func__, index);
525 } else if (!strcmp("sku", cmd)) {
526 ret = cros_ec_get_sku_id(dev);
532 printf("Error: %d\n", ret);
535 return CMD_RET_USAGE;
539 printf("Error: CROS-EC command failed (error %d)\n", ret);
547 crosec, 6, 1, do_cros_ec,
548 "CROS-EC utility command",
549 "init Re-init CROS-EC (done on startup automatically)\n"
550 "crosec id Read CROS-EC ID\n"
551 "crosec info Read CROS-EC info\n"
552 "crosec features Read CROS-EC features\n"
553 "crosec switches Read CROS-EC switches\n"
554 "crosec curimage Read CROS-EC current image\n"
555 "crosec hash Read CROS-EC hash\n"
556 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
557 "crosec events Read CROS-EC host events\n"
558 "crosec eventsb Read CROS-EC host events_b\n"
559 "crosec clrevents [mask] Clear CROS-EC host events\n"
560 "crosec regioninfo <ro|rw> Read image info\n"
561 "crosec flashinfo Read flash info\n"
562 "crosec erase <ro|rw> Erase EC image\n"
563 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
564 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
565 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
566 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
567 "crosec sku Read board SKU ID\n"
568 "crosec test run tests on cros_ec\n"
569 "crosec version Read CROS-EC version"