2 * Chromium OS cros_ec driver
4 * Copyright (c) 2016 The Chromium OS Authors.
5 * Copyright (c) 2016 National Instruments Corp
7 * SPDX-License-Identifier: GPL-2.0+
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"};
20 DECLARE_GLOBAL_DATA_PTR;
23 * Decode a flash region parameter
25 * @param argc Number of params remaining
26 * @param argv List of remaining parameters
27 * @return flash region (EC_FLASH_REGION_...) or -1 on error
29 static int cros_ec_decode_region(int argc, char * const argv[])
32 if (0 == strcmp(*argv, "rw"))
33 return EC_FLASH_REGION_RW;
34 else if (0 == strcmp(*argv, "ro"))
35 return EC_FLASH_REGION_RO;
37 debug("%s: Invalid region '%s'\n", __func__, *argv);
39 debug("%s: Missing region parameter\n", __func__);
46 * Perform a flash read or write command
48 * @param dev CROS-EC device to read/write
49 * @param is_write 1 do to a write, 0 to do a read
50 * @param argc Number of arguments
51 * @param argv Arguments (2 is region, 3 is address)
52 * @return 0 for ok, 1 for a usage error or -ve for ec command error
53 * (negative EC_RES_...)
55 static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
58 uint32_t offset, size = -1U, region_size;
64 region = cros_ec_decode_region(argc - 2, argv + 2);
69 addr = simple_strtoul(argv[3], &endp, 16);
70 if (*argv[3] == 0 || *endp != 0)
73 size = simple_strtoul(argv[4], &endp, 16);
74 if (*argv[4] == 0 || *endp != 0)
78 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
80 debug("%s: Could not read region info\n", __func__);
87 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
88 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
90 debug("%s: Could not %s region\n", __func__,
91 is_write ? "write" : "read");
98 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
100 struct cros_ec_dev *dev;
101 struct udevice *udev;
106 return CMD_RET_USAGE;
109 if (0 == strcmp("init", cmd)) {
110 /* Remove any existing device */
111 ret = uclass_find_device(UCLASS_CROS_EC, 0, &udev);
114 ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
116 printf("Could not init cros_ec device (err %d)\n", ret);
122 ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
124 printf("Cannot get cros-ec device (err=%d)\n", ret);
127 dev = dev_get_uclass_priv(udev);
128 if (0 == strcmp("id", cmd)) {
131 if (cros_ec_read_id(dev, id, sizeof(id))) {
132 debug("%s: Could not read KBC ID\n", __func__);
136 } else if (0 == strcmp("info", cmd)) {
137 struct ec_response_mkbp_info info;
139 if (cros_ec_info(dev, &info)) {
140 debug("%s: Could not read KBC info\n", __func__);
143 printf("rows = %u\n", info.rows);
144 printf("cols = %u\n", info.cols);
145 printf("switches = %#x\n", info.switches);
146 } else if (0 == strcmp("curimage", cmd)) {
147 enum ec_current_image image;
149 if (cros_ec_read_current_image(dev, &image)) {
150 debug("%s: Could not read KBC image\n", __func__);
153 printf("%d\n", image);
154 } else if (0 == strcmp("hash", cmd)) {
155 struct ec_response_vboot_hash hash;
158 if (cros_ec_read_hash(dev, &hash)) {
159 debug("%s: Could not read KBC hash\n", __func__);
163 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
164 printf("type: SHA-256\n");
166 printf("type: %d\n", hash.hash_type);
168 printf("offset: 0x%08x\n", hash.offset);
169 printf("size: 0x%08x\n", hash.size);
172 for (i = 0; i < hash.digest_size; i++)
173 printf("%02x", hash.hash_digest[i]);
175 } else if (0 == strcmp("reboot", cmd)) {
177 enum ec_reboot_cmd cmd;
179 if (argc >= 3 && !strcmp(argv[2], "cold")) {
180 cmd = EC_REBOOT_COLD;
182 region = cros_ec_decode_region(argc - 2, argv + 2);
183 if (region == EC_FLASH_REGION_RO)
184 cmd = EC_REBOOT_JUMP_RO;
185 else if (region == EC_FLASH_REGION_RW)
186 cmd = EC_REBOOT_JUMP_RW;
188 return CMD_RET_USAGE;
191 if (cros_ec_reboot(dev, cmd, 0)) {
192 debug("%s: Could not reboot KBC\n", __func__);
195 } else if (0 == strcmp("events", cmd)) {
198 if (cros_ec_get_host_events(dev, &events)) {
199 debug("%s: Could not read host events\n", __func__);
202 printf("0x%08x\n", events);
203 } else if (0 == strcmp("clrevents", cmd)) {
204 uint32_t events = 0x7fffffff;
207 events = simple_strtol(argv[2], NULL, 0);
209 if (cros_ec_clear_host_events(dev, events)) {
210 debug("%s: Could not clear host events\n", __func__);
213 } else if (0 == strcmp("read", cmd)) {
214 ret = do_read_write(dev, 0, argc, argv);
216 return CMD_RET_USAGE;
217 } else if (0 == strcmp("write", cmd)) {
218 ret = do_read_write(dev, 1, argc, argv);
220 return CMD_RET_USAGE;
221 } else if (0 == strcmp("erase", cmd)) {
222 int region = cros_ec_decode_region(argc - 2, argv + 2);
223 uint32_t offset, size;
226 return CMD_RET_USAGE;
227 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
228 debug("%s: Could not read region info\n", __func__);
231 ret = cros_ec_flash_erase(dev, offset, size);
233 debug("%s: Could not erase region\n",
237 } else if (0 == strcmp("regioninfo", cmd)) {
238 int region = cros_ec_decode_region(argc - 2, argv + 2);
239 uint32_t offset, size;
242 return CMD_RET_USAGE;
243 ret = cros_ec_flash_offset(dev, region, &offset, &size);
245 debug("%s: Could not read region info\n", __func__);
247 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
249 printf("Offset: %x\n", offset);
250 printf("Size: %x\n", size);
252 } else if (0 == strcmp("flashinfo", cmd)) {
253 struct ec_response_flash_info p;
255 ret = cros_ec_read_flashinfo(dev, &p);
257 printf("Flash size: %u\n", p.flash_size);
258 printf("Write block size: %u\n", p.write_block_size);
259 printf("Erase block size: %u\n", p.erase_block_size);
261 } else if (0 == strcmp("vbnvcontext", cmd)) {
262 uint8_t block[EC_VBNV_BLOCK_SIZE];
265 unsigned long result;
268 ret = cros_ec_read_vbnvcontext(dev, block);
270 printf("vbnv_block: ");
271 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
272 printf("%02x", block[i]);
277 * TODO(clchiou): Move this to a utility function as
278 * cmd_spi might want to call it.
280 memset(block, 0, EC_VBNV_BLOCK_SIZE);
281 len = strlen(argv[2]);
283 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
286 buf[0] = argv[2][i * 2];
287 if (i * 2 + 1 >= len)
290 buf[1] = argv[2][i * 2 + 1];
291 strict_strtoul(buf, 16, &result);
294 ret = cros_ec_write_vbnvcontext(dev, block);
297 debug("%s: Could not %s VbNvContext\n", __func__,
298 argc <= 2 ? "read" : "write");
300 } else if (0 == strcmp("test", cmd)) {
301 int result = cros_ec_test(dev);
304 printf("Test failed with error %d\n", result);
306 puts("Test passed\n");
307 } else if (0 == strcmp("version", cmd)) {
308 struct ec_response_get_version *p;
311 ret = cros_ec_read_version(dev, &p);
314 printf("RO version: %1.*s\n",
315 (int)sizeof(p->version_string_ro),
316 p->version_string_ro);
317 printf("RW version: %1.*s\n",
318 (int)sizeof(p->version_string_rw),
319 p->version_string_rw);
320 printf("Firmware copy: %s\n",
322 ARRAY_SIZE(ec_current_image_name) ?
323 ec_current_image_name[p->current_image] :
325 ret = cros_ec_read_build_info(dev, &build_string);
327 printf("Build info: %s\n", build_string);
329 } else if (0 == strcmp("ldo", cmd)) {
330 uint8_t index, state;
334 return CMD_RET_USAGE;
335 index = simple_strtoul(argv[2], &endp, 10);
336 if (*argv[2] == 0 || *endp != 0)
337 return CMD_RET_USAGE;
339 state = simple_strtoul(argv[3], &endp, 10);
340 if (*argv[3] == 0 || *endp != 0)
341 return CMD_RET_USAGE;
342 ret = cros_ec_set_ldo(udev, index, state);
344 ret = cros_ec_get_ldo(udev, index, &state);
346 printf("LDO%d: %s\n", index,
347 state == EC_LDO_STATE_ON ?
353 debug("%s: Could not access LDO%d\n", __func__, index);
357 return CMD_RET_USAGE;
361 printf("Error: CROS-EC command failed (error %d)\n", ret);
369 crosec, 6, 1, do_cros_ec,
370 "CROS-EC utility command",
371 "init Re-init CROS-EC (done on startup automatically)\n"
372 "crosec id Read CROS-EC ID\n"
373 "crosec info Read CROS-EC info\n"
374 "crosec curimage Read CROS-EC current image\n"
375 "crosec hash Read CROS-EC hash\n"
376 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
377 "crosec events Read CROS-EC host events\n"
378 "crosec clrevents [mask] Clear CROS-EC host events\n"
379 "crosec regioninfo <ro|rw> Read image info\n"
380 "crosec flashinfo Read flash info\n"
381 "crosec erase <ro|rw> Erase EC image\n"
382 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
383 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
384 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
385 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
386 "crosec test run tests on cros_ec\n"
387 "crosec version Read CROS-EC version"