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 * Perform a flash read or write command
25 * @param dev CROS-EC device to read/write
26 * @param is_write 1 do to a write, 0 to do a read
27 * @param argc Number of arguments
28 * @param argv Arguments (2 is region, 3 is address)
29 * @return 0 for ok, 1 for a usage error or -ve for ec command error
30 * (negative EC_RES_...)
32 static int do_read_write(struct cros_ec_dev
*dev
, int is_write
, int argc
,
35 uint32_t offset
, size
= -1U, region_size
;
41 region
= cros_ec_decode_region(argc
- 2, argv
+ 2);
46 addr
= simple_strtoul(argv
[3], &endp
, 16);
47 if (*argv
[3] == 0 || *endp
!= 0)
50 size
= simple_strtoul(argv
[4], &endp
, 16);
51 if (*argv
[4] == 0 || *endp
!= 0)
55 ret
= cros_ec_flash_offset(dev
, region
, &offset
, ®ion_size
);
57 debug("%s: Could not read region info\n", __func__
);
64 cros_ec_flash_write(dev
, (uint8_t *)addr
, offset
, size
) :
65 cros_ec_flash_read(dev
, (uint8_t *)addr
, offset
, size
);
67 debug("%s: Could not %s region\n", __func__
,
68 is_write
? "write" : "read");
75 static int do_cros_ec(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
77 struct cros_ec_dev
*dev
;
86 if (0 == strcmp("init", cmd
)) {
87 /* Remove any existing device */
88 ret
= uclass_find_device(UCLASS_CROS_EC
, 0, &udev
);
91 ret
= uclass_get_device(UCLASS_CROS_EC
, 0, &udev
);
93 printf("Could not init cros_ec device (err %d)\n", ret
);
99 ret
= uclass_get_device(UCLASS_CROS_EC
, 0, &udev
);
101 printf("Cannot get cros-ec device (err=%d)\n", ret
);
104 dev
= dev_get_uclass_priv(udev
);
105 if (0 == strcmp("id", cmd
)) {
108 if (cros_ec_read_id(dev
, id
, sizeof(id
))) {
109 debug("%s: Could not read KBC ID\n", __func__
);
113 } else if (0 == strcmp("info", cmd
)) {
114 struct ec_response_mkbp_info info
;
116 if (cros_ec_info(dev
, &info
)) {
117 debug("%s: Could not read KBC info\n", __func__
);
120 printf("rows = %u\n", info
.rows
);
121 printf("cols = %u\n", info
.cols
);
122 printf("switches = %#x\n", info
.switches
);
123 } else if (0 == strcmp("curimage", cmd
)) {
124 enum ec_current_image image
;
126 if (cros_ec_read_current_image(dev
, &image
)) {
127 debug("%s: Could not read KBC image\n", __func__
);
130 printf("%d\n", image
);
131 } else if (0 == strcmp("hash", cmd
)) {
132 struct ec_response_vboot_hash hash
;
135 if (cros_ec_read_hash(dev
, &hash
)) {
136 debug("%s: Could not read KBC hash\n", __func__
);
140 if (hash
.hash_type
== EC_VBOOT_HASH_TYPE_SHA256
)
141 printf("type: SHA-256\n");
143 printf("type: %d\n", hash
.hash_type
);
145 printf("offset: 0x%08x\n", hash
.offset
);
146 printf("size: 0x%08x\n", hash
.size
);
149 for (i
= 0; i
< hash
.digest_size
; i
++)
150 printf("%02x", hash
.hash_digest
[i
]);
152 } else if (0 == strcmp("reboot", cmd
)) {
154 enum ec_reboot_cmd cmd
;
156 if (argc
>= 3 && !strcmp(argv
[2], "cold")) {
157 cmd
= EC_REBOOT_COLD
;
159 region
= cros_ec_decode_region(argc
- 2, argv
+ 2);
160 if (region
== EC_FLASH_REGION_RO
)
161 cmd
= EC_REBOOT_JUMP_RO
;
162 else if (region
== EC_FLASH_REGION_RW
)
163 cmd
= EC_REBOOT_JUMP_RW
;
165 return CMD_RET_USAGE
;
168 if (cros_ec_reboot(dev
, cmd
, 0)) {
169 debug("%s: Could not reboot KBC\n", __func__
);
172 } else if (0 == strcmp("events", cmd
)) {
175 if (cros_ec_get_host_events(dev
, &events
)) {
176 debug("%s: Could not read host events\n", __func__
);
179 printf("0x%08x\n", events
);
180 } else if (0 == strcmp("clrevents", cmd
)) {
181 uint32_t events
= 0x7fffffff;
184 events
= simple_strtol(argv
[2], NULL
, 0);
186 if (cros_ec_clear_host_events(dev
, events
)) {
187 debug("%s: Could not clear host events\n", __func__
);
190 } else if (0 == strcmp("read", cmd
)) {
191 ret
= do_read_write(dev
, 0, argc
, argv
);
193 return CMD_RET_USAGE
;
194 } else if (0 == strcmp("write", cmd
)) {
195 ret
= do_read_write(dev
, 1, argc
, argv
);
197 return CMD_RET_USAGE
;
198 } else if (0 == strcmp("erase", cmd
)) {
199 int region
= cros_ec_decode_region(argc
- 2, argv
+ 2);
200 uint32_t offset
, size
;
203 return CMD_RET_USAGE
;
204 if (cros_ec_flash_offset(dev
, region
, &offset
, &size
)) {
205 debug("%s: Could not read region info\n", __func__
);
208 ret
= cros_ec_flash_erase(dev
, offset
, size
);
210 debug("%s: Could not erase region\n",
214 } else if (0 == strcmp("regioninfo", cmd
)) {
215 int region
= cros_ec_decode_region(argc
- 2, argv
+ 2);
216 uint32_t offset
, size
;
219 return CMD_RET_USAGE
;
220 ret
= cros_ec_flash_offset(dev
, region
, &offset
, &size
);
222 debug("%s: Could not read region info\n", __func__
);
224 printf("Region: %s\n", region
== EC_FLASH_REGION_RO
?
226 printf("Offset: %x\n", offset
);
227 printf("Size: %x\n", size
);
229 } else if (0 == strcmp("flashinfo", cmd
)) {
230 struct ec_response_flash_info p
;
232 ret
= cros_ec_read_flashinfo(dev
, &p
);
234 printf("Flash size: %u\n", p
.flash_size
);
235 printf("Write block size: %u\n", p
.write_block_size
);
236 printf("Erase block size: %u\n", p
.erase_block_size
);
238 } else if (0 == strcmp("vbnvcontext", cmd
)) {
239 uint8_t block
[EC_VBNV_BLOCK_SIZE
];
242 unsigned long result
;
245 ret
= cros_ec_read_vbnvcontext(dev
, block
);
247 printf("vbnv_block: ");
248 for (i
= 0; i
< EC_VBNV_BLOCK_SIZE
; i
++)
249 printf("%02x", block
[i
]);
254 * TODO(clchiou): Move this to a utility function as
255 * cmd_spi might want to call it.
257 memset(block
, 0, EC_VBNV_BLOCK_SIZE
);
258 len
= strlen(argv
[2]);
260 for (i
= 0; i
< EC_VBNV_BLOCK_SIZE
; i
++) {
263 buf
[0] = argv
[2][i
* 2];
264 if (i
* 2 + 1 >= len
)
267 buf
[1] = argv
[2][i
* 2 + 1];
268 strict_strtoul(buf
, 16, &result
);
271 ret
= cros_ec_write_vbnvcontext(dev
, block
);
274 debug("%s: Could not %s VbNvContext\n", __func__
,
275 argc
<= 2 ? "read" : "write");
277 } else if (0 == strcmp("test", cmd
)) {
278 int result
= cros_ec_test(dev
);
281 printf("Test failed with error %d\n", result
);
283 puts("Test passed\n");
284 } else if (0 == strcmp("version", cmd
)) {
285 struct ec_response_get_version
*p
;
288 ret
= cros_ec_read_version(dev
, &p
);
291 printf("RO version: %1.*s\n",
292 (int)sizeof(p
->version_string_ro
),
293 p
->version_string_ro
);
294 printf("RW version: %1.*s\n",
295 (int)sizeof(p
->version_string_rw
),
296 p
->version_string_rw
);
297 printf("Firmware copy: %s\n",
299 ARRAY_SIZE(ec_current_image_name
) ?
300 ec_current_image_name
[p
->current_image
] :
302 ret
= cros_ec_read_build_info(dev
, &build_string
);
304 printf("Build info: %s\n", build_string
);
306 } else if (0 == strcmp("ldo", cmd
)) {
307 uint8_t index
, state
;
311 return CMD_RET_USAGE
;
312 index
= simple_strtoul(argv
[2], &endp
, 10);
313 if (*argv
[2] == 0 || *endp
!= 0)
314 return CMD_RET_USAGE
;
316 state
= simple_strtoul(argv
[3], &endp
, 10);
317 if (*argv
[3] == 0 || *endp
!= 0)
318 return CMD_RET_USAGE
;
319 ret
= cros_ec_set_ldo(udev
, index
, state
);
321 ret
= cros_ec_get_ldo(udev
, index
, &state
);
323 printf("LDO%d: %s\n", index
,
324 state
== EC_LDO_STATE_ON
?
330 debug("%s: Could not access LDO%d\n", __func__
, index
);
334 return CMD_RET_USAGE
;
338 printf("Error: CROS-EC command failed (error %d)\n", ret
);
346 crosec
, 6, 1, do_cros_ec
,
347 "CROS-EC utility command",
348 "init Re-init CROS-EC (done on startup automatically)\n"
349 "crosec id Read CROS-EC ID\n"
350 "crosec info Read CROS-EC info\n"
351 "crosec curimage Read CROS-EC current image\n"
352 "crosec hash Read CROS-EC hash\n"
353 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
354 "crosec events Read CROS-EC host events\n"
355 "crosec clrevents [mask] Clear CROS-EC host events\n"
356 "crosec regioninfo <ro|rw> Read image info\n"
357 "crosec flashinfo Read flash info\n"
358 "crosec erase <ro|rw> Erase EC image\n"
359 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
360 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
361 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
362 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
363 "crosec test run tests on cros_ec\n"
364 "crosec version Read CROS-EC version"