77656a2308c9a4d7a7654c9e85abf45198554863
[platform/kernel/u-boot.git] / cmd / cros_ec.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Chromium OS cros_ec driver
4  *
5  * Copyright (c) 2016 The Chromium OS Authors.
6  * Copyright (c) 2016 National Instruments Corp
7  */
8
9 #include <common.h>
10 #include <command.h>
11 #include <cros_ec.h>
12 #include <dm.h>
13 #include <flash.h>
14 #include <log.h>
15 #include <dm/device-internal.h>
16 #include <dm/uclass-internal.h>
17
18 /* Note: depends on enum ec_current_image */
19 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
20
21 /**
22  * Decode a flash region parameter
23  *
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
27  */
28 static int cros_ec_decode_region(int argc, char *const argv[])
29 {
30         if (argc > 0) {
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;
35
36                 debug("%s: Invalid region '%s'\n", __func__, *argv);
37         } else {
38                 debug("%s: Missing region parameter\n", __func__);
39         }
40
41         return -1;
42 }
43
44 /**
45  * Perform a flash read or write command
46  *
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_...)
53  */
54 static int do_read_write(struct udevice *dev, int is_write, int argc,
55                          char *const argv[])
56 {
57         uint32_t offset, size = -1U, region_size;
58         unsigned long addr;
59         char *endp;
60         int region;
61         int ret;
62
63         region = cros_ec_decode_region(argc - 2, argv + 2);
64         if (region == -1)
65                 return 1;
66         if (argc < 4)
67                 return 1;
68         addr = simple_strtoul(argv[3], &endp, 16);
69         if (*argv[3] == 0 || *endp != 0)
70                 return 1;
71         if (argc > 4) {
72                 size = simple_strtoul(argv[4], &endp, 16);
73                 if (*argv[4] == 0 || *endp != 0)
74                         return 1;
75         }
76
77         ret = cros_ec_flash_offset(dev, region, &offset, &region_size);
78         if (ret) {
79                 debug("%s: Could not read region info\n", __func__);
80                 return ret;
81         }
82         if (size == -1U)
83                 size = region_size;
84
85         ret = is_write ?
86                 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
87                 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
88         if (ret) {
89                 debug("%s: Could not %s region\n", __func__,
90                       is_write ? "write" : "read");
91                 return ret;
92         }
93
94         return 0;
95 }
96
97 static const char *const feat_name[64] = {
98         "limited",
99         "flash",
100         "pwm_fan",
101         "pwm_keyb",
102         "lightbar",
103         "led",
104         "motion_sense",
105         "keyb",
106         "pstore",
107         "port80",
108         "thermal",
109         "bklight_switch",
110         "wifi_switch",
111         "host_events",
112         "gpio",
113         "i2c",
114         "charger",
115         "battery",
116         "smart_battery",
117         "hang_detect",
118         "pmu",
119         "sub_mcu",
120         "usb_pd",
121         "usb_mux",
122         "motion_sense_fifo",
123         "vstore",
124         "usbc_ss_mux_virtual",
125         "rtc",
126         "fingerprint",
127         "touchpad",
128         "rwsig",
129         "device_event",
130         "unified_wake_masks",
131         "host_event64",
132         "exec_in_ram",
133         "cec",
134         "motion_sense_tight_timestamps",
135         "refined_tablet_mode_hysteresis",
136         "efs2",
137         "scp",
138         "ish",
139         "typec_cmd",
140         "typec_require_ap_mode_entry",
141         "typec_mux_require_ap_ack",
142 };
143
144 static int do_show_features(struct udevice *dev)
145 {
146         u64 feat;
147         int ret;
148         uint i;
149
150         ret = cros_ec_get_features(dev, &feat);
151         if (ret)
152                 return ret;
153         for (i = 0; i < ARRAY_SIZE(feat_name); i++) {
154                 if (feat & (1ULL << i)) {
155                         if (feat_name[i])
156                                 printf("%s\n", feat_name[i]);
157                         else
158                                 printf("unknown %d\n", i);
159                 }
160         }
161
162         return 0;
163 }
164
165 static int do_cros_ec(struct cmd_tbl *cmdtp, int flag, int argc,
166                       char *const argv[])
167 {
168         struct udevice *dev;
169         const char *cmd;
170         int ret = 0;
171
172         if (argc < 2)
173                 return CMD_RET_USAGE;
174
175         cmd = argv[1];
176         if (0 == strcmp("init", cmd)) {
177                 /* Remove any existing device */
178                 ret = uclass_find_device(UCLASS_CROS_EC, 0, &dev);
179                 if (!ret)
180                         device_remove(dev, DM_REMOVE_NORMAL);
181                 ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
182                 if (ret) {
183                         printf("Could not init cros_ec device (err %d)\n", ret);
184                         return 1;
185                 }
186                 return 0;
187         }
188
189         ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
190         if (ret) {
191                 printf("Cannot get cros-ec device (err=%d)\n", ret);
192                 return 1;
193         }
194         if (0 == strcmp("id", cmd)) {
195                 char id[MSG_BYTES];
196
197                 if (cros_ec_read_id(dev, id, sizeof(id))) {
198                         debug("%s: Could not read KBC ID\n", __func__);
199                         return 1;
200                 }
201                 printf("%s\n", id);
202         } else if (0 == strcmp("info", cmd)) {
203                 struct ec_response_mkbp_info info;
204
205                 if (cros_ec_info(dev, &info)) {
206                         debug("%s: Could not read KBC info\n", __func__);
207                         return 1;
208                 }
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);
213
214                 if (ret)
215                         printf("Error: %d\n", ret);
216         } else if (0 == strcmp("curimage", cmd)) {
217                 enum ec_current_image image;
218
219                 if (cros_ec_read_current_image(dev, &image)) {
220                         debug("%s: Could not read KBC image\n", __func__);
221                         return 1;
222                 }
223                 printf("%d\n", image);
224         } else if (0 == strcmp("hash", cmd)) {
225                 struct ec_response_vboot_hash hash;
226                 int i;
227
228                 if (cros_ec_read_hash(dev, EC_VBOOT_HASH_OFFSET_ACTIVE, &hash)) {
229                         debug("%s: Could not read KBC hash\n", __func__);
230                         return 1;
231                 }
232
233                 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
234                         printf("type:    SHA-256\n");
235                 else
236                         printf("type:    %d\n", hash.hash_type);
237
238                 printf("offset:  0x%08x\n", hash.offset);
239                 printf("size:    0x%08x\n", hash.size);
240
241                 printf("digest:  ");
242                 for (i = 0; i < hash.digest_size; i++)
243                         printf("%02x", hash.hash_digest[i]);
244                 printf("\n");
245         } else if (0 == strcmp("reboot", cmd)) {
246                 int region;
247                 enum ec_reboot_cmd cmd;
248
249                 if (argc >= 3 && !strcmp(argv[2], "cold")) {
250                         cmd = EC_REBOOT_COLD;
251                 } else {
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;
257                         else
258                                 return CMD_RET_USAGE;
259                 }
260
261                 if (cros_ec_reboot(dev, cmd, 0)) {
262                         debug("%s: Could not reboot KBC\n", __func__);
263                         return 1;
264                 }
265         } else if (0 == strcmp("events", cmd)) {
266                 uint32_t events;
267
268                 if (cros_ec_get_host_events(dev, &events)) {
269                         debug("%s: Could not read host events\n", __func__);
270                         return 1;
271                 }
272                 printf("0x%08x\n", events);
273         } else if (0 == strcmp("clrevents", cmd)) {
274                 uint32_t events = 0x7fffffff;
275
276                 if (argc >= 3)
277                         events = simple_strtol(argv[2], NULL, 0);
278
279                 if (cros_ec_clear_host_events(dev, events)) {
280                         debug("%s: Could not clear host events\n", __func__);
281                         return 1;
282                 }
283         } else if (0 == strcmp("read", cmd)) {
284                 ret = do_read_write(dev, 0, argc, argv);
285                 if (ret > 0)
286                         return CMD_RET_USAGE;
287         } else if (0 == strcmp("write", cmd)) {
288                 ret = do_read_write(dev, 1, argc, argv);
289                 if (ret > 0)
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;
294
295                 if (region == -1)
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__);
299                         ret = -1;
300                 } else {
301                         ret = cros_ec_flash_erase(dev, offset, size);
302                         if (ret) {
303                                 debug("%s: Could not erase region\n",
304                                       __func__);
305                         }
306                 }
307         } else if (0 == strcmp("regioninfo", cmd)) {
308                 int region = cros_ec_decode_region(argc - 2, argv + 2);
309                 uint32_t offset, size;
310
311                 if (region == -1)
312                         return CMD_RET_USAGE;
313                 ret = cros_ec_flash_offset(dev, region, &offset, &size);
314                 if (ret) {
315                         debug("%s: Could not read region info\n", __func__);
316                 } else {
317                         printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
318                                         "RO" : "RW");
319                         printf("Offset: %x\n", offset);
320                         printf("Size:   %x\n", size);
321                 }
322         } else if (0 == strcmp("flashinfo", cmd)) {
323                 struct ec_response_flash_info p;
324
325                 ret = cros_ec_read_flashinfo(dev, &p);
326                 if (!ret) {
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);
330                 }
331         } else if (0 == strcmp("vbnvcontext", cmd)) {
332                 uint8_t block[EC_VBNV_BLOCK_SIZE];
333                 char buf[3];
334                 int i, len;
335                 unsigned long result;
336
337                 if (argc <= 2) {
338                         ret = cros_ec_read_nvdata(dev, block,
339                                                   EC_VBNV_BLOCK_SIZE);
340                         if (!ret) {
341                                 printf("vbnv_block: ");
342                                 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
343                                         printf("%02x", block[i]);
344                                 putc('\n');
345                         }
346                 } else {
347                         /*
348                          * TODO(clchiou): Move this to a utility function as
349                          * cmd_spi might want to call it.
350                          */
351                         memset(block, 0, EC_VBNV_BLOCK_SIZE);
352                         len = strlen(argv[2]);
353                         buf[2] = '\0';
354                         for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
355                                 if (i * 2 >= len)
356                                         break;
357                                 buf[0] = argv[2][i * 2];
358                                 if (i * 2 + 1 >= len)
359                                         buf[1] = '0';
360                                 else
361                                         buf[1] = argv[2][i * 2 + 1];
362                                 strict_strtoul(buf, 16, &result);
363                                 block[i] = result;
364                         }
365                         ret = cros_ec_write_nvdata(dev, block,
366                                                    EC_VBNV_BLOCK_SIZE);
367                 }
368                 if (ret) {
369                         debug("%s: Could not %s VbNvContext\n", __func__,
370                               argc <= 2 ?  "read" : "write");
371                 }
372         } else if (0 == strcmp("test", cmd)) {
373                 int result = cros_ec_test(dev);
374
375                 if (result)
376                         printf("Test failed with error %d\n", result);
377                 else
378                         puts("Test passed\n");
379         } else if (0 == strcmp("version", cmd)) {
380                 struct ec_response_get_version *p;
381                 char *build_string;
382
383                 ret = cros_ec_read_version(dev, &p);
384                 if (!ret) {
385                         /* Print versions */
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",
393                                (p->current_image <
394                                ARRAY_SIZE(ec_current_image_name) ?
395                                ec_current_image_name[p->current_image] :
396                                "?"));
397                         ret = cros_ec_read_build_info(dev, &build_string);
398                         if (!ret)
399                                 printf("Build info:    %s\n", build_string);
400                 }
401         } else if (0 == strcmp("ldo", cmd)) {
402                 uint8_t index, state;
403                 char *endp;
404
405                 if (argc < 3)
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;
410                 if (argc > 3) {
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);
415                 } else {
416                         ret = cros_ec_get_ldo(dev, index, &state);
417                         if (!ret) {
418                                 printf("LDO%d: %s\n", index,
419                                        state == EC_LDO_STATE_ON ?
420                                        "on" : "off");
421                         }
422                 }
423
424                 if (ret) {
425                         debug("%s: Could not access LDO%d\n", __func__, index);
426                         return ret;
427                 }
428         } else if (!strcmp("sku", cmd)) {
429                 ret = cros_ec_get_sku_id(dev);
430
431                 if (ret >= 0) {
432                         printf("%d\n", ret);
433                         ret = 0;
434                 } else {
435                         printf("Error: %d\n", ret);
436                 }
437         } else {
438                 return CMD_RET_USAGE;
439         }
440
441         if (ret < 0) {
442                 printf("Error: CROS-EC command failed (error %d)\n", ret);
443                 ret = 1;
444         }
445
446         return ret;
447 }
448
449 U_BOOT_CMD(
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"
471 );