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 = simple_strtoul(argv[3], &endp, 16);
69 if (*argv[3] == 0 || *endp != 0)
72 size = simple_strtoul(argv[4], &endp, 16);
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 int do_cros_ec(struct cmd_tbl *cmdtp, int flag, int argc,
173 return CMD_RET_USAGE;
176 if (0 == strcmp("init", cmd)) {
177 /* Remove any existing device */
178 ret = uclass_find_device(UCLASS_CROS_EC, 0, &dev);
180 device_remove(dev, DM_REMOVE_NORMAL);
181 ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
183 printf("Could not init cros_ec device (err %d)\n", ret);
189 ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
191 printf("Cannot get cros-ec device (err=%d)\n", ret);
194 if (0 == strcmp("id", cmd)) {
197 if (cros_ec_read_id(dev, id, sizeof(id))) {
198 debug("%s: Could not read KBC ID\n", __func__);
202 } else if (0 == strcmp("info", cmd)) {
203 struct ec_response_mkbp_info info;
205 if (cros_ec_info(dev, &info)) {
206 debug("%s: Could not read KBC info\n", __func__);
209 printf("rows = %u\n", info.rows);
210 printf("cols = %u\n", info.cols);
211 } else if (!strcmp("features", cmd)) {
212 ret = do_show_features(dev);
215 printf("Error: %d\n", ret);
216 } else if (0 == strcmp("curimage", cmd)) {
217 enum ec_current_image image;
219 if (cros_ec_read_current_image(dev, &image)) {
220 debug("%s: Could not read KBC image\n", __func__);
223 printf("%d\n", image);
224 } else if (0 == strcmp("hash", cmd)) {
225 struct ec_response_vboot_hash hash;
228 if (cros_ec_read_hash(dev, EC_VBOOT_HASH_OFFSET_ACTIVE, &hash)) {
229 debug("%s: Could not read KBC hash\n", __func__);
233 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
234 printf("type: SHA-256\n");
236 printf("type: %d\n", hash.hash_type);
238 printf("offset: 0x%08x\n", hash.offset);
239 printf("size: 0x%08x\n", hash.size);
242 for (i = 0; i < hash.digest_size; i++)
243 printf("%02x", hash.hash_digest[i]);
245 } else if (0 == strcmp("reboot", cmd)) {
247 enum ec_reboot_cmd cmd;
249 if (argc >= 3 && !strcmp(argv[2], "cold")) {
250 cmd = EC_REBOOT_COLD;
252 region = cros_ec_decode_region(argc - 2, argv + 2);
253 if (region == EC_FLASH_REGION_RO)
254 cmd = EC_REBOOT_JUMP_RO;
255 else if (region == EC_FLASH_REGION_ACTIVE)
256 cmd = EC_REBOOT_JUMP_RW;
258 return CMD_RET_USAGE;
261 if (cros_ec_reboot(dev, cmd, 0)) {
262 debug("%s: Could not reboot KBC\n", __func__);
265 } else if (0 == strcmp("events", cmd)) {
268 if (cros_ec_get_host_events(dev, &events)) {
269 debug("%s: Could not read host events\n", __func__);
272 printf("0x%08x\n", events);
273 } else if (0 == strcmp("clrevents", cmd)) {
274 uint32_t events = 0x7fffffff;
277 events = simple_strtol(argv[2], NULL, 0);
279 if (cros_ec_clear_host_events(dev, events)) {
280 debug("%s: Could not clear host events\n", __func__);
283 } else if (0 == strcmp("read", cmd)) {
284 ret = do_read_write(dev, 0, argc, argv);
286 return CMD_RET_USAGE;
287 } else if (0 == strcmp("write", cmd)) {
288 ret = do_read_write(dev, 1, argc, argv);
290 return CMD_RET_USAGE;
291 } else if (0 == strcmp("erase", cmd)) {
292 int region = cros_ec_decode_region(argc - 2, argv + 2);
293 uint32_t offset, size;
296 return CMD_RET_USAGE;
297 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
298 debug("%s: Could not read region info\n", __func__);
301 ret = cros_ec_flash_erase(dev, offset, size);
303 debug("%s: Could not erase region\n",
307 } else if (0 == strcmp("regioninfo", cmd)) {
308 int region = cros_ec_decode_region(argc - 2, argv + 2);
309 uint32_t offset, size;
312 return CMD_RET_USAGE;
313 ret = cros_ec_flash_offset(dev, region, &offset, &size);
315 debug("%s: Could not read region info\n", __func__);
317 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
319 printf("Offset: %x\n", offset);
320 printf("Size: %x\n", size);
322 } else if (0 == strcmp("flashinfo", cmd)) {
323 struct ec_response_flash_info p;
325 ret = cros_ec_read_flashinfo(dev, &p);
327 printf("Flash size: %u\n", p.flash_size);
328 printf("Write block size: %u\n", p.write_block_size);
329 printf("Erase block size: %u\n", p.erase_block_size);
331 } else if (0 == strcmp("vbnvcontext", cmd)) {
332 uint8_t block[EC_VBNV_BLOCK_SIZE];
335 unsigned long result;
338 ret = cros_ec_read_nvdata(dev, block,
341 printf("vbnv_block: ");
342 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
343 printf("%02x", block[i]);
348 * TODO(clchiou): Move this to a utility function as
349 * cmd_spi might want to call it.
351 memset(block, 0, EC_VBNV_BLOCK_SIZE);
352 len = strlen(argv[2]);
354 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
357 buf[0] = argv[2][i * 2];
358 if (i * 2 + 1 >= len)
361 buf[1] = argv[2][i * 2 + 1];
362 strict_strtoul(buf, 16, &result);
365 ret = cros_ec_write_nvdata(dev, block,
369 debug("%s: Could not %s VbNvContext\n", __func__,
370 argc <= 2 ? "read" : "write");
372 } else if (0 == strcmp("test", cmd)) {
373 int result = cros_ec_test(dev);
376 printf("Test failed with error %d\n", result);
378 puts("Test passed\n");
379 } else if (0 == strcmp("version", cmd)) {
380 struct ec_response_get_version *p;
383 ret = cros_ec_read_version(dev, &p);
386 printf("RO version: %1.*s\n",
387 (int)sizeof(p->version_string_ro),
388 p->version_string_ro);
389 printf("RW version: %1.*s\n",
390 (int)sizeof(p->version_string_rw),
391 p->version_string_rw);
392 printf("Firmware copy: %s\n",
394 ARRAY_SIZE(ec_current_image_name) ?
395 ec_current_image_name[p->current_image] :
397 ret = cros_ec_read_build_info(dev, &build_string);
399 printf("Build info: %s\n", build_string);
401 } else if (0 == strcmp("ldo", cmd)) {
402 uint8_t index, state;
406 return CMD_RET_USAGE;
407 index = simple_strtoul(argv[2], &endp, 10);
408 if (*argv[2] == 0 || *endp != 0)
409 return CMD_RET_USAGE;
411 state = simple_strtoul(argv[3], &endp, 10);
412 if (*argv[3] == 0 || *endp != 0)
413 return CMD_RET_USAGE;
414 ret = cros_ec_set_ldo(dev, index, state);
416 ret = cros_ec_get_ldo(dev, index, &state);
418 printf("LDO%d: %s\n", index,
419 state == EC_LDO_STATE_ON ?
425 debug("%s: Could not access LDO%d\n", __func__, index);
428 } else if (!strcmp("sku", cmd)) {
429 ret = cros_ec_get_sku_id(dev);
435 printf("Error: %d\n", ret);
438 return CMD_RET_USAGE;
442 printf("Error: CROS-EC command failed (error %d)\n", ret);
450 crosec, 6, 1, do_cros_ec,
451 "CROS-EC utility command",
452 "init Re-init CROS-EC (done on startup automatically)\n"
453 "crosec id Read CROS-EC ID\n"
454 "crosec info Read CROS-EC info\n"
455 "crosec features Read CROS-EC features\n"
456 "crosec curimage Read CROS-EC current image\n"
457 "crosec hash Read CROS-EC hash\n"
458 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
459 "crosec events Read CROS-EC host events\n"
460 "crosec clrevents [mask] Clear CROS-EC host events\n"
461 "crosec regioninfo <ro|rw> Read image info\n"
462 "crosec flashinfo Read flash info\n"
463 "crosec erase <ro|rw> Erase EC image\n"
464 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
465 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
466 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
467 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
468 "crosec sku Read board SKU ID\n"
469 "crosec test run tests on cros_ec\n"
470 "crosec version Read CROS-EC version"