]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/misc/cros_ec.c
cros_ec: Add base support for protocol v3
[karo-tx-uboot.git] / drivers / misc / cros_ec.c
1 /*
2  * Chromium OS cros_ec driver
3  *
4  * Copyright (c) 2012 The Chromium OS Authors.
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 /*
10  * This is the interface to the Chrome OS EC. It provides keyboard functions,
11  * power control and battery management. Quite a few other functions are
12  * provided to enable the EC software to be updated, talk to the EC's I2C bus
13  * and store a small amount of data in a memory which persists while the EC
14  * is not reset.
15  */
16
17 #include <common.h>
18 #include <command.h>
19 #include <i2c.h>
20 #include <cros_ec.h>
21 #include <fdtdec.h>
22 #include <malloc.h>
23 #include <spi.h>
24 #include <asm/io.h>
25 #include <asm-generic/gpio.h>
26
27 #ifdef DEBUG_TRACE
28 #define debug_trace(fmt, b...)  debug(fmt, #b)
29 #else
30 #define debug_trace(fmt, b...)
31 #endif
32
33 enum {
34         /* Timeout waiting for a flash erase command to complete */
35         CROS_EC_CMD_TIMEOUT_MS  = 5000,
36         /* Timeout waiting for a synchronous hash to be recomputed */
37         CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
38 };
39
40 static struct cros_ec_dev static_dev, *last_dev;
41
42 DECLARE_GLOBAL_DATA_PTR;
43
44 /* Note: depends on enum ec_current_image */
45 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
46
47 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
48 {
49 #ifdef DEBUG
50         int i;
51
52         printf("%s: ", name);
53         if (cmd != -1)
54                 printf("cmd=%#x: ", cmd);
55         for (i = 0; i < len; i++)
56                 printf("%02x ", data[i]);
57         printf("\n");
58 #endif
59 }
60
61 /*
62  * Calculate a simple 8-bit checksum of a data block
63  *
64  * @param data  Data block to checksum
65  * @param size  Size of data block in bytes
66  * @return checksum value (0 to 255)
67  */
68 int cros_ec_calc_checksum(const uint8_t *data, int size)
69 {
70         int csum, i;
71
72         for (i = csum = 0; i < size; i++)
73                 csum += data[i];
74         return csum & 0xff;
75 }
76
77 /**
78  * Create a request packet for protocol version 3.
79  *
80  * The packet is stored in the device's internal output buffer.
81  *
82  * @param dev           CROS-EC device
83  * @param cmd           Command to send (EC_CMD_...)
84  * @param cmd_version   Version of command to send (EC_VER_...)
85  * @param dout          Output data (may be NULL If dout_len=0)
86  * @param dout_len      Size of output data in bytes
87  * @return packet size in bytes, or <0 if error.
88  */
89 static int create_proto3_request(struct cros_ec_dev *dev,
90                                  int cmd, int cmd_version,
91                                  const void *dout, int dout_len)
92 {
93         struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
94         int out_bytes = dout_len + sizeof(*rq);
95
96         /* Fail if output size is too big */
97         if (out_bytes > (int)sizeof(dev->dout)) {
98                 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
99                 return -EC_RES_REQUEST_TRUNCATED;
100         }
101
102         /* Fill in request packet */
103         rq->struct_version = EC_HOST_REQUEST_VERSION;
104         rq->checksum = 0;
105         rq->command = cmd;
106         rq->command_version = cmd_version;
107         rq->reserved = 0;
108         rq->data_len = dout_len;
109
110         /* Copy data after header */
111         memcpy(rq + 1, dout, dout_len);
112
113         /* Write checksum field so the entire packet sums to 0 */
114         rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
115
116         cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
117
118         /* Return size of request packet */
119         return out_bytes;
120 }
121
122 /**
123  * Prepare the device to receive a protocol version 3 response.
124  *
125  * @param dev           CROS-EC device
126  * @param din_len       Maximum size of response in bytes
127  * @return maximum expected number of bytes in response, or <0 if error.
128  */
129 static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
130 {
131         int in_bytes = din_len + sizeof(struct ec_host_response);
132
133         /* Fail if input size is too big */
134         if (in_bytes > (int)sizeof(dev->din)) {
135                 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
136                 return -EC_RES_RESPONSE_TOO_BIG;
137         }
138
139         /* Return expected size of response packet */
140         return in_bytes;
141 }
142
143 /**
144  * Handle a protocol version 3 response packet.
145  *
146  * The packet must already be stored in the device's internal input buffer.
147  *
148  * @param dev           CROS-EC device
149  * @param dinp          Returns pointer to response data
150  * @param din_len       Maximum size of response in bytes
151  * @return number of bytes of response data, or <0 if error
152  */
153 static int handle_proto3_response(struct cros_ec_dev *dev,
154                                   uint8_t **dinp, int din_len)
155 {
156         struct ec_host_response *rs = (struct ec_host_response *)dev->din;
157         int in_bytes;
158         int csum;
159
160         cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
161
162         /* Check input data */
163         if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
164                 debug("%s: EC response version mismatch\n", __func__);
165                 return -EC_RES_INVALID_RESPONSE;
166         }
167
168         if (rs->reserved) {
169                 debug("%s: EC response reserved != 0\n", __func__);
170                 return -EC_RES_INVALID_RESPONSE;
171         }
172
173         if (rs->data_len > din_len) {
174                 debug("%s: EC returned too much data\n", __func__);
175                 return -EC_RES_RESPONSE_TOO_BIG;
176         }
177
178         cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
179
180         /* Update in_bytes to actual data size */
181         in_bytes = sizeof(*rs) + rs->data_len;
182
183         /* Verify checksum */
184         csum = cros_ec_calc_checksum(dev->din, in_bytes);
185         if (csum) {
186                 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
187                       csum);
188                 return -EC_RES_INVALID_CHECKSUM;
189         }
190
191         /* Return error result, if any */
192         if (rs->result)
193                 return -(int)rs->result;
194
195         /* If we're still here, set response data pointer and return length */
196         *dinp = (uint8_t *)(rs + 1);
197
198         return rs->data_len;
199 }
200
201 static int send_command_proto3(struct cros_ec_dev *dev,
202                                int cmd, int cmd_version,
203                                const void *dout, int dout_len,
204                                uint8_t **dinp, int din_len)
205 {
206         int out_bytes, in_bytes;
207         int rv;
208
209         /* Create request packet */
210         out_bytes = create_proto3_request(dev, cmd, cmd_version,
211                                           dout, dout_len);
212         if (out_bytes < 0)
213                 return out_bytes;
214
215         /* Prepare response buffer */
216         in_bytes = prepare_proto3_response_buffer(dev, din_len);
217         if (in_bytes < 0)
218                 return in_bytes;
219
220         switch (dev->interface) {
221         case CROS_EC_IF_NONE:
222         /* TODO: support protocol 3 for LPC, I2C; for now fall through */
223         default:
224                 debug("%s: Unsupported interface\n", __func__);
225                 rv = -1;
226         }
227         if (rv < 0)
228                 return rv;
229
230         /* Process the response */
231         return handle_proto3_response(dev, dinp, din_len);
232 }
233
234 static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
235                         const void *dout, int dout_len,
236                         uint8_t **dinp, int din_len)
237 {
238         int ret = -1;
239
240         /* Handle protocol version 3 support */
241         if (dev->protocol_version == 3) {
242                 return send_command_proto3(dev, cmd, cmd_version,
243                                            dout, dout_len, dinp, din_len);
244         }
245
246         switch (dev->interface) {
247 #ifdef CONFIG_CROS_EC_SPI
248         case CROS_EC_IF_SPI:
249                 ret = cros_ec_spi_command(dev, cmd, cmd_version,
250                                         (const uint8_t *)dout, dout_len,
251                                         dinp, din_len);
252                 break;
253 #endif
254 #ifdef CONFIG_CROS_EC_I2C
255         case CROS_EC_IF_I2C:
256                 ret = cros_ec_i2c_command(dev, cmd, cmd_version,
257                                         (const uint8_t *)dout, dout_len,
258                                         dinp, din_len);
259                 break;
260 #endif
261 #ifdef CONFIG_CROS_EC_LPC
262         case CROS_EC_IF_LPC:
263                 ret = cros_ec_lpc_command(dev, cmd, cmd_version,
264                                         (const uint8_t *)dout, dout_len,
265                                         dinp, din_len);
266                 break;
267 #endif
268         case CROS_EC_IF_NONE:
269         default:
270                 ret = -1;
271         }
272
273         return ret;
274 }
275
276 /**
277  * Send a command to the CROS-EC device and return the reply.
278  *
279  * The device's internal input/output buffers are used.
280  *
281  * @param dev           CROS-EC device
282  * @param cmd           Command to send (EC_CMD_...)
283  * @param cmd_version   Version of command to send (EC_VER_...)
284  * @param dout          Output data (may be NULL If dout_len=0)
285  * @param dout_len      Size of output data in bytes
286  * @param dinp          Response data (may be NULL If din_len=0).
287  *                      If not NULL, it will be updated to point to the data
288  *                      and will always be double word aligned (64-bits)
289  * @param din_len       Maximum size of response in bytes
290  * @return number of bytes in response, or -1 on error
291  */
292 static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
293                 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
294                 int din_len)
295 {
296         uint8_t *din;
297         int len;
298
299         len = send_command(dev, cmd, cmd_version, dout, dout_len,
300                                 &din, din_len);
301
302         /* If the command doesn't complete, wait a while */
303         if (len == -EC_RES_IN_PROGRESS) {
304                 struct ec_response_get_comms_status *resp;
305                 ulong start;
306
307                 /* Wait for command to complete */
308                 start = get_timer(0);
309                 do {
310                         int ret;
311
312                         mdelay(50);     /* Insert some reasonable delay */
313                         ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
314                                         NULL, 0,
315                                         (uint8_t **)&resp, sizeof(*resp));
316                         if (ret < 0)
317                                 return ret;
318
319                         if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
320                                 debug("%s: Command %#02x timeout\n",
321                                       __func__, cmd);
322                                 return -EC_RES_TIMEOUT;
323                         }
324                 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
325
326                 /* OK it completed, so read the status response */
327                 /* not sure why it was 0 for the last argument */
328                 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
329                                 NULL, 0, &din, din_len);
330         }
331
332         debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp, *dinp);
333         if (dinp) {
334                 /* If we have any data to return, it must be 64bit-aligned */
335                 assert(len <= 0 || !((uintptr_t)din & 7));
336                 *dinp = din;
337         }
338
339         return len;
340 }
341
342 /**
343  * Send a command to the CROS-EC device and return the reply.
344  *
345  * The device's internal input/output buffers are used.
346  *
347  * @param dev           CROS-EC device
348  * @param cmd           Command to send (EC_CMD_...)
349  * @param cmd_version   Version of command to send (EC_VER_...)
350  * @param dout          Output data (may be NULL If dout_len=0)
351  * @param dout_len      Size of output data in bytes
352  * @param din           Response data (may be NULL If din_len=0).
353  *                      It not NULL, it is a place for ec_command() to copy the
354  *      data to.
355  * @param din_len       Maximum size of response in bytes
356  * @return number of bytes in response, or -1 on error
357  */
358 static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
359                       const void *dout, int dout_len,
360                       void *din, int din_len)
361 {
362         uint8_t *in_buffer;
363         int len;
364
365         assert((din_len == 0) || din);
366         len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
367                         &in_buffer, din_len);
368         if (len > 0) {
369                 /*
370                  * If we were asked to put it somewhere, do so, otherwise just
371                  * disregard the result.
372                  */
373                 if (din && in_buffer) {
374                         assert(len <= din_len);
375                         memmove(din, in_buffer, len);
376                 }
377         }
378         return len;
379 }
380
381 int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
382 {
383         if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
384                        sizeof(scan->data)) < sizeof(scan->data))
385                 return -1;
386
387         return 0;
388 }
389
390 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
391 {
392         struct ec_response_get_version *r;
393
394         if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
395                         (uint8_t **)&r, sizeof(*r)) < sizeof(*r))
396                 return -1;
397
398         if (maxlen > sizeof(r->version_string_ro))
399                 maxlen = sizeof(r->version_string_ro);
400
401         switch (r->current_image) {
402         case EC_IMAGE_RO:
403                 memcpy(id, r->version_string_ro, maxlen);
404                 break;
405         case EC_IMAGE_RW:
406                 memcpy(id, r->version_string_rw, maxlen);
407                 break;
408         default:
409                 return -1;
410         }
411
412         id[maxlen - 1] = '\0';
413         return 0;
414 }
415
416 int cros_ec_read_version(struct cros_ec_dev *dev,
417                        struct ec_response_get_version **versionp)
418 {
419         if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
420                         (uint8_t **)versionp, sizeof(**versionp))
421                         < sizeof(**versionp))
422                 return -1;
423
424         return 0;
425 }
426
427 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
428 {
429         if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
430                         (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
431                 return -1;
432
433         return 0;
434 }
435
436 int cros_ec_read_current_image(struct cros_ec_dev *dev,
437                 enum ec_current_image *image)
438 {
439         struct ec_response_get_version *r;
440
441         if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
442                         (uint8_t **)&r, sizeof(*r)) < sizeof(*r))
443                 return -1;
444
445         *image = r->current_image;
446         return 0;
447 }
448
449 static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
450                                   struct ec_response_vboot_hash *hash)
451 {
452         struct ec_params_vboot_hash p;
453         ulong start;
454
455         start = get_timer(0);
456         while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
457                 mdelay(50);     /* Insert some reasonable delay */
458
459                 p.cmd = EC_VBOOT_HASH_GET;
460                 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
461                        hash, sizeof(*hash)) < 0)
462                         return -1;
463
464                 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
465                         debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
466                         return -EC_RES_TIMEOUT;
467                 }
468         }
469         return 0;
470 }
471
472
473 int cros_ec_read_hash(struct cros_ec_dev *dev,
474                 struct ec_response_vboot_hash *hash)
475 {
476         struct ec_params_vboot_hash p;
477         int rv;
478
479         p.cmd = EC_VBOOT_HASH_GET;
480         if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
481                        hash, sizeof(*hash)) < 0)
482                 return -1;
483
484         /* If the EC is busy calculating the hash, fidget until it's done. */
485         rv = cros_ec_wait_on_hash_done(dev, hash);
486         if (rv)
487                 return rv;
488
489         /* If the hash is valid, we're done. Otherwise, we have to kick it off
490          * again and wait for it to complete. Note that we explicitly assume
491          * that hashing zero bytes is always wrong, even though that would
492          * produce a valid hash value. */
493         if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
494                 return 0;
495
496         debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
497               __func__, hash->status, hash->size);
498
499         p.cmd = EC_VBOOT_HASH_START;
500         p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
501         p.nonce_size = 0;
502         p.offset = EC_VBOOT_HASH_OFFSET_RW;
503
504         if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
505                        hash, sizeof(*hash)) < 0)
506                 return -1;
507
508         rv = cros_ec_wait_on_hash_done(dev, hash);
509         if (rv)
510                 return rv;
511
512         debug("%s: hash done\n", __func__);
513
514         return 0;
515 }
516
517 static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
518 {
519         struct ec_params_vboot_hash p;
520         struct ec_response_vboot_hash *hash;
521
522         /* We don't have an explict command for the EC to discard its current
523          * hash value, so we'll just tell it to calculate one that we know is
524          * wrong (we claim that hashing zero bytes is always invalid).
525          */
526         p.cmd = EC_VBOOT_HASH_RECALC;
527         p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
528         p.nonce_size = 0;
529         p.offset = 0;
530         p.size = 0;
531
532         debug("%s:\n", __func__);
533
534         if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
535                        (uint8_t **)&hash, sizeof(*hash)) < 0)
536                 return -1;
537
538         /* No need to wait for it to finish */
539         return 0;
540 }
541
542 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
543                 uint8_t flags)
544 {
545         struct ec_params_reboot_ec p;
546
547         p.cmd = cmd;
548         p.flags = flags;
549
550         if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
551                         < 0)
552                 return -1;
553
554         if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
555                 /*
556                  * EC reboot will take place immediately so delay to allow it
557                  * to complete.  Note that some reboot types (EC_REBOOT_COLD)
558                  * will reboot the AP as well, in which case we won't actually
559                  * get to this point.
560                  */
561                 /*
562                  * TODO(rspangler@chromium.org): Would be nice if we had a
563                  * better way to determine when the reboot is complete.  Could
564                  * we poll a memory-mapped LPC value?
565                  */
566                 udelay(50000);
567         }
568
569         return 0;
570 }
571
572 int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
573 {
574         /* no interrupt support : always poll */
575         if (!fdt_gpio_isvalid(&dev->ec_int))
576                 return 1;
577
578         return !gpio_get_value(dev->ec_int.gpio);
579 }
580
581 int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
582 {
583         if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
584                        sizeof(*info)) < sizeof(*info))
585                 return -1;
586
587         return 0;
588 }
589
590 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
591 {
592         struct ec_response_host_event_mask *resp;
593
594         /*
595          * Use the B copy of the event flags, because the main copy is already
596          * used by ACPI/SMI.
597          */
598         if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
599                        (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
600                 return -1;
601
602         if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
603                 return -1;
604
605         *events_ptr = resp->mask;
606         return 0;
607 }
608
609 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
610 {
611         struct ec_params_host_event_mask params;
612
613         params.mask = events;
614
615         /*
616          * Use the B copy of the event flags, so it affects the data returned
617          * by cros_ec_get_host_events().
618          */
619         if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
620                        &params, sizeof(params), NULL, 0) < 0)
621                 return -1;
622
623         return 0;
624 }
625
626 int cros_ec_flash_protect(struct cros_ec_dev *dev,
627                        uint32_t set_mask, uint32_t set_flags,
628                        struct ec_response_flash_protect *resp)
629 {
630         struct ec_params_flash_protect params;
631
632         params.mask = set_mask;
633         params.flags = set_flags;
634
635         if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
636                        &params, sizeof(params),
637                        resp, sizeof(*resp)) < sizeof(*resp))
638                 return -1;
639
640         return 0;
641 }
642
643 static int cros_ec_check_version(struct cros_ec_dev *dev)
644 {
645         struct ec_params_hello req;
646         struct ec_response_hello *resp;
647
648 #ifdef CONFIG_CROS_EC_LPC
649         /* LPC has its own way of doing this */
650         if (dev->interface == CROS_EC_IF_LPC)
651                 return cros_ec_lpc_check_version(dev);
652 #endif
653
654         /*
655          * TODO(sjg@chromium.org).
656          * There is a strange oddity here with the EC. We could just ignore
657          * the response, i.e. pass the last two parameters as NULL and 0.
658          * In this case we won't read back very many bytes from the EC.
659          * On the I2C bus the EC gets upset about this and will try to send
660          * the bytes anyway. This means that we will have to wait for that
661          * to complete before continuing with a new EC command.
662          *
663          * This problem is probably unique to the I2C bus.
664          *
665          * So for now, just read all the data anyway.
666          */
667
668         /* Try sending a version 2 packet */
669         dev->protocol_version = 2;
670         if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
671                        (uint8_t **)&resp, sizeof(*resp)) > 0) {
672                 return 0;
673         }
674
675         /*
676          * Fail if we're still here, since the EC doesn't understand any
677          * protcol version we speak.  Version 1 interface without command
678          * version is no longer supported, and we don't know about any new
679          * protocol versions.
680          */
681         dev->protocol_version = 0;
682         printf("%s: ERROR: old EC interface not supported\n", __func__);
683         return -1;
684 }
685
686 int cros_ec_test(struct cros_ec_dev *dev)
687 {
688         struct ec_params_hello req;
689         struct ec_response_hello *resp;
690
691         req.in_data = 0x12345678;
692         if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
693                        (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
694                 printf("ec_command_inptr() returned error\n");
695                 return -1;
696         }
697         if (resp->out_data != req.in_data + 0x01020304) {
698                 printf("Received invalid handshake %x\n", resp->out_data);
699                 return -1;
700         }
701
702         return 0;
703 }
704
705 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
706                       uint32_t *offset, uint32_t *size)
707 {
708         struct ec_params_flash_region_info p;
709         struct ec_response_flash_region_info *r;
710         int ret;
711
712         p.region = region;
713         ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
714                          EC_VER_FLASH_REGION_INFO,
715                          &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
716         if (ret != sizeof(*r))
717                 return -1;
718
719         if (offset)
720                 *offset = r->offset;
721         if (size)
722                 *size = r->size;
723
724         return 0;
725 }
726
727 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
728 {
729         struct ec_params_flash_erase p;
730
731         p.offset = offset;
732         p.size = size;
733         return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
734                         NULL, 0);
735 }
736
737 /**
738  * Write a single block to the flash
739  *
740  * Write a block of data to the EC flash. The size must not exceed the flash
741  * write block size which you can obtain from cros_ec_flash_write_burst_size().
742  *
743  * The offset starts at 0. You can obtain the region information from
744  * cros_ec_flash_offset() to find out where to write for a particular region.
745  *
746  * Attempting to write to the region where the EC is currently running from
747  * will result in an error.
748  *
749  * @param dev           CROS-EC device
750  * @param data          Pointer to data buffer to write
751  * @param offset        Offset within flash to write to.
752  * @param size          Number of bytes to write
753  * @return 0 if ok, -1 on error
754  */
755 static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
756                 const uint8_t *data, uint32_t offset, uint32_t size)
757 {
758         struct ec_params_flash_write p;
759
760         p.offset = offset;
761         p.size = size;
762         assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
763         memcpy(&p + 1, data, p.size);
764
765         return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
766                           &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
767 }
768
769 /**
770  * Return optimal flash write burst size
771  */
772 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
773 {
774         return EC_FLASH_WRITE_VER0_SIZE;
775 }
776
777 /**
778  * Check if a block of data is erased (all 0xff)
779  *
780  * This function is useful when dealing with flash, for checking whether a
781  * data block is erased and thus does not need to be programmed.
782  *
783  * @param data          Pointer to data to check (must be word-aligned)
784  * @param size          Number of bytes to check (must be word-aligned)
785  * @return 0 if erased, non-zero if any word is not erased
786  */
787 static int cros_ec_data_is_erased(const uint32_t *data, int size)
788 {
789         assert(!(size & 3));
790         size /= sizeof(uint32_t);
791         for (; size > 0; size -= 4, data++)
792                 if (*data != -1U)
793                         return 0;
794
795         return 1;
796 }
797
798 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
799                      uint32_t offset, uint32_t size)
800 {
801         uint32_t burst = cros_ec_flash_write_burst_size(dev);
802         uint32_t end, off;
803         int ret;
804
805         /*
806          * TODO: round up to the nearest multiple of write size.  Can get away
807          * without that on link right now because its write size is 4 bytes.
808          */
809         end = offset + size;
810         for (off = offset; off < end; off += burst, data += burst) {
811                 uint32_t todo;
812
813                 /* If the data is empty, there is no point in programming it */
814                 todo = min(end - off, burst);
815                 if (dev->optimise_flash_write &&
816                                 cros_ec_data_is_erased((uint32_t *)data, todo))
817                         continue;
818
819                 ret = cros_ec_flash_write_block(dev, data, off, todo);
820                 if (ret)
821                         return ret;
822         }
823
824         return 0;
825 }
826
827 /**
828  * Read a single block from the flash
829  *
830  * Read a block of data from the EC flash. The size must not exceed the flash
831  * write block size which you can obtain from cros_ec_flash_write_burst_size().
832  *
833  * The offset starts at 0. You can obtain the region information from
834  * cros_ec_flash_offset() to find out where to read for a particular region.
835  *
836  * @param dev           CROS-EC device
837  * @param data          Pointer to data buffer to read into
838  * @param offset        Offset within flash to read from
839  * @param size          Number of bytes to read
840  * @return 0 if ok, -1 on error
841  */
842 static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
843                                  uint32_t offset, uint32_t size)
844 {
845         struct ec_params_flash_read p;
846
847         p.offset = offset;
848         p.size = size;
849
850         return ec_command(dev, EC_CMD_FLASH_READ, 0,
851                           &p, sizeof(p), data, size) >= 0 ? 0 : -1;
852 }
853
854 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
855                     uint32_t size)
856 {
857         uint32_t burst = cros_ec_flash_write_burst_size(dev);
858         uint32_t end, off;
859         int ret;
860
861         end = offset + size;
862         for (off = offset; off < end; off += burst, data += burst) {
863                 ret = cros_ec_flash_read_block(dev, data, off,
864                                             min(end - off, burst));
865                 if (ret)
866                         return ret;
867         }
868
869         return 0;
870 }
871
872 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
873                          const uint8_t *image, int image_size)
874 {
875         uint32_t rw_offset, rw_size;
876         int ret;
877
878         if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
879                 return -1;
880         if (image_size > rw_size)
881                 return -1;
882
883         /* Invalidate the existing hash, just in case the AP reboots
884          * unexpectedly during the update. If that happened, the EC RW firmware
885          * would be invalid, but the EC would still have the original hash.
886          */
887         ret = cros_ec_invalidate_hash(dev);
888         if (ret)
889                 return ret;
890
891         /*
892          * Erase the entire RW section, so that the EC doesn't see any garbage
893          * past the new image if it's smaller than the current image.
894          *
895          * TODO: could optimize this to erase just the current image, since
896          * presumably everything past that is 0xff's.  But would still need to
897          * round up to the nearest multiple of erase size.
898          */
899         ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
900         if (ret)
901                 return ret;
902
903         /* Write the image */
904         ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
905         if (ret)
906                 return ret;
907
908         return 0;
909 }
910
911 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
912 {
913         struct ec_params_vbnvcontext p;
914         int len;
915
916         p.op = EC_VBNV_CONTEXT_OP_READ;
917
918         len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
919                         &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
920         if (len < EC_VBNV_BLOCK_SIZE)
921                 return -1;
922
923         return 0;
924 }
925
926 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
927 {
928         struct ec_params_vbnvcontext p;
929         int len;
930
931         p.op = EC_VBNV_CONTEXT_OP_WRITE;
932         memcpy(p.block, block, sizeof(p.block));
933
934         len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
935                         &p, sizeof(p), NULL, 0);
936         if (len < 0)
937                 return -1;
938
939         return 0;
940 }
941
942 int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
943 {
944         struct ec_params_ldo_set params;
945
946         params.index = index;
947         params.state = state;
948
949         if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
950                        &params, sizeof(params),
951                        NULL, 0))
952                 return -1;
953
954         return 0;
955 }
956
957 int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
958 {
959         struct ec_params_ldo_get params;
960         struct ec_response_ldo_get *resp;
961
962         params.index = index;
963
964         if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
965                        &params, sizeof(params),
966                        (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
967                 return -1;
968
969         *state = resp->state;
970
971         return 0;
972 }
973
974 /**
975  * Decode EC interface details from the device tree and allocate a suitable
976  * device.
977  *
978  * @param blob          Device tree blob
979  * @param node          Node to decode from
980  * @param devp          Returns a pointer to the new allocated device
981  * @return 0 if ok, -1 on error
982  */
983 static int cros_ec_decode_fdt(const void *blob, int node,
984                 struct cros_ec_dev **devp)
985 {
986         enum fdt_compat_id compat;
987         struct cros_ec_dev *dev;
988         int parent;
989
990         /* See what type of parent we are inside (this is expensive) */
991         parent = fdt_parent_offset(blob, node);
992         if (parent < 0) {
993                 debug("%s: Cannot find node parent\n", __func__);
994                 return -1;
995         }
996
997         dev = &static_dev;
998         dev->node = node;
999         dev->parent_node = parent;
1000
1001         compat = fdtdec_lookup(blob, parent);
1002         switch (compat) {
1003 #ifdef CONFIG_CROS_EC_SPI
1004         case COMPAT_SAMSUNG_EXYNOS_SPI:
1005                 dev->interface = CROS_EC_IF_SPI;
1006                 if (cros_ec_spi_decode_fdt(dev, blob))
1007                         return -1;
1008                 break;
1009 #endif
1010 #ifdef CONFIG_CROS_EC_I2C
1011         case COMPAT_SAMSUNG_S3C2440_I2C:
1012                 dev->interface = CROS_EC_IF_I2C;
1013                 if (cros_ec_i2c_decode_fdt(dev, blob))
1014                         return -1;
1015                 break;
1016 #endif
1017 #ifdef CONFIG_CROS_EC_LPC
1018         case COMPAT_INTEL_LPC:
1019                 dev->interface = CROS_EC_IF_LPC;
1020                 break;
1021 #endif
1022         default:
1023                 debug("%s: Unknown compat id %d\n", __func__, compat);
1024                 return -1;
1025         }
1026
1027         fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int);
1028         dev->optimise_flash_write = fdtdec_get_bool(blob, node,
1029                                                     "optimise-flash-write");
1030         *devp = dev;
1031
1032         return 0;
1033 }
1034
1035 int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
1036 {
1037         char id[MSG_BYTES];
1038         struct cros_ec_dev *dev;
1039         int node = 0;
1040
1041         *cros_ecp = NULL;
1042         do {
1043                 node = fdtdec_next_compatible(blob, node,
1044                                               COMPAT_GOOGLE_CROS_EC);
1045                 if (node < 0) {
1046                         debug("%s: Node not found\n", __func__);
1047                         return 0;
1048                 }
1049         } while (!fdtdec_get_is_enabled(blob, node));
1050
1051         if (cros_ec_decode_fdt(blob, node, &dev)) {
1052                 debug("%s: Failed to decode device.\n", __func__);
1053                 return -CROS_EC_ERR_FDT_DECODE;
1054         }
1055
1056         switch (dev->interface) {
1057 #ifdef CONFIG_CROS_EC_SPI
1058         case CROS_EC_IF_SPI:
1059                 if (cros_ec_spi_init(dev, blob)) {
1060                         debug("%s: Could not setup SPI interface\n", __func__);
1061                         return -CROS_EC_ERR_DEV_INIT;
1062                 }
1063                 break;
1064 #endif
1065 #ifdef CONFIG_CROS_EC_I2C
1066         case CROS_EC_IF_I2C:
1067                 if (cros_ec_i2c_init(dev, blob))
1068                         return -CROS_EC_ERR_DEV_INIT;
1069                 break;
1070 #endif
1071 #ifdef CONFIG_CROS_EC_LPC
1072         case CROS_EC_IF_LPC:
1073                 if (cros_ec_lpc_init(dev, blob))
1074                         return -CROS_EC_ERR_DEV_INIT;
1075                 break;
1076 #endif
1077         case CROS_EC_IF_NONE:
1078         default:
1079                 return 0;
1080         }
1081
1082         /* we will poll the EC interrupt line */
1083         fdtdec_setup_gpio(&dev->ec_int);
1084         if (fdt_gpio_isvalid(&dev->ec_int))
1085                 gpio_direction_input(dev->ec_int.gpio);
1086
1087         if (cros_ec_check_version(dev)) {
1088                 debug("%s: Could not detect CROS-EC version\n", __func__);
1089                 return -CROS_EC_ERR_CHECK_VERSION;
1090         }
1091
1092         if (cros_ec_read_id(dev, id, sizeof(id))) {
1093                 debug("%s: Could not read KBC ID\n", __func__);
1094                 return -CROS_EC_ERR_READ_ID;
1095         }
1096
1097         /* Remember this device for use by the cros_ec command */
1098         last_dev = *cros_ecp = dev;
1099         debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
1100
1101         return 0;
1102 }
1103
1104 int cros_ec_decode_region(int argc, char * const argv[])
1105 {
1106         if (argc > 0) {
1107                 if (0 == strcmp(*argv, "rw"))
1108                         return EC_FLASH_REGION_RW;
1109                 else if (0 == strcmp(*argv, "ro"))
1110                         return EC_FLASH_REGION_RO;
1111
1112                 debug("%s: Invalid region '%s'\n", __func__, *argv);
1113         } else {
1114                 debug("%s: Missing region parameter\n", __func__);
1115         }
1116
1117         return -1;
1118 }
1119
1120 int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config)
1121 {
1122         int flash_node, node;
1123
1124         node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
1125         if (node < 0) {
1126                 debug("Failed to find chrome-ec node'\n");
1127                 return -1;
1128         }
1129
1130         flash_node = fdt_subnode_offset(blob, node, "flash");
1131         if (flash_node < 0) {
1132                 debug("Failed to find flash node\n");
1133                 return -1;
1134         }
1135
1136         if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
1137                                    &config->flash)) {
1138                 debug("Failed to decode flash node in chrome-ec'\n");
1139                 return -1;
1140         }
1141
1142         config->flash_erase_value = fdtdec_get_int(blob, flash_node,
1143                                                     "erase-value", -1);
1144         for (node = fdt_first_subnode(blob, flash_node); node >= 0;
1145              node = fdt_next_subnode(blob, node)) {
1146                 const char *name = fdt_get_name(blob, node, NULL);
1147                 enum ec_flash_region region;
1148
1149                 if (0 == strcmp(name, "ro")) {
1150                         region = EC_FLASH_REGION_RO;
1151                 } else if (0 == strcmp(name, "rw")) {
1152                         region = EC_FLASH_REGION_RW;
1153                 } else if (0 == strcmp(name, "wp-ro")) {
1154                         region = EC_FLASH_REGION_WP_RO;
1155                 } else {
1156                         debug("Unknown EC flash region name '%s'\n", name);
1157                         return -1;
1158                 }
1159
1160                 if (fdtdec_read_fmap_entry(blob, node, "reg",
1161                                            &config->region[region])) {
1162                         debug("Failed to decode flash region in chrome-ec'\n");
1163                         return -1;
1164                 }
1165         }
1166
1167         return 0;
1168 }
1169
1170 #ifdef CONFIG_CMD_CROS_EC
1171
1172 /**
1173  * Perform a flash read or write command
1174  *
1175  * @param dev           CROS-EC device to read/write
1176  * @param is_write      1 do to a write, 0 to do a read
1177  * @param argc          Number of arguments
1178  * @param argv          Arguments (2 is region, 3 is address)
1179  * @return 0 for ok, 1 for a usage error or -ve for ec command error
1180  *      (negative EC_RES_...)
1181  */
1182 static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
1183                          char * const argv[])
1184 {
1185         uint32_t offset, size = -1U, region_size;
1186         unsigned long addr;
1187         char *endp;
1188         int region;
1189         int ret;
1190
1191         region = cros_ec_decode_region(argc - 2, argv + 2);
1192         if (region == -1)
1193                 return 1;
1194         if (argc < 4)
1195                 return 1;
1196         addr = simple_strtoul(argv[3], &endp, 16);
1197         if (*argv[3] == 0 || *endp != 0)
1198                 return 1;
1199         if (argc > 4) {
1200                 size = simple_strtoul(argv[4], &endp, 16);
1201                 if (*argv[4] == 0 || *endp != 0)
1202                         return 1;
1203         }
1204
1205         ret = cros_ec_flash_offset(dev, region, &offset, &region_size);
1206         if (ret) {
1207                 debug("%s: Could not read region info\n", __func__);
1208                 return ret;
1209         }
1210         if (size == -1U)
1211                 size = region_size;
1212
1213         ret = is_write ?
1214                 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
1215                 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
1216         if (ret) {
1217                 debug("%s: Could not %s region\n", __func__,
1218                       is_write ? "write" : "read");
1219                 return ret;
1220         }
1221
1222         return 0;
1223 }
1224
1225 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1226 {
1227         struct cros_ec_dev *dev = last_dev;
1228         const char *cmd;
1229         int ret = 0;
1230
1231         if (argc < 2)
1232                 return CMD_RET_USAGE;
1233
1234         cmd = argv[1];
1235         if (0 == strcmp("init", cmd)) {
1236                 ret = cros_ec_init(gd->fdt_blob, &dev);
1237                 if (ret) {
1238                         printf("Could not init cros_ec device (err %d)\n", ret);
1239                         return 1;
1240                 }
1241                 return 0;
1242         }
1243
1244         /* Just use the last allocated device; there should be only one */
1245         if (!last_dev) {
1246                 printf("No CROS-EC device available\n");
1247                 return 1;
1248         }
1249         if (0 == strcmp("id", cmd)) {
1250                 char id[MSG_BYTES];
1251
1252                 if (cros_ec_read_id(dev, id, sizeof(id))) {
1253                         debug("%s: Could not read KBC ID\n", __func__);
1254                         return 1;
1255                 }
1256                 printf("%s\n", id);
1257         } else if (0 == strcmp("info", cmd)) {
1258                 struct ec_response_mkbp_info info;
1259
1260                 if (cros_ec_info(dev, &info)) {
1261                         debug("%s: Could not read KBC info\n", __func__);
1262                         return 1;
1263                 }
1264                 printf("rows     = %u\n", info.rows);
1265                 printf("cols     = %u\n", info.cols);
1266                 printf("switches = %#x\n", info.switches);
1267         } else if (0 == strcmp("curimage", cmd)) {
1268                 enum ec_current_image image;
1269
1270                 if (cros_ec_read_current_image(dev, &image)) {
1271                         debug("%s: Could not read KBC image\n", __func__);
1272                         return 1;
1273                 }
1274                 printf("%d\n", image);
1275         } else if (0 == strcmp("hash", cmd)) {
1276                 struct ec_response_vboot_hash hash;
1277                 int i;
1278
1279                 if (cros_ec_read_hash(dev, &hash)) {
1280                         debug("%s: Could not read KBC hash\n", __func__);
1281                         return 1;
1282                 }
1283
1284                 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
1285                         printf("type:    SHA-256\n");
1286                 else
1287                         printf("type:    %d\n", hash.hash_type);
1288
1289                 printf("offset:  0x%08x\n", hash.offset);
1290                 printf("size:    0x%08x\n", hash.size);
1291
1292                 printf("digest:  ");
1293                 for (i = 0; i < hash.digest_size; i++)
1294                         printf("%02x", hash.hash_digest[i]);
1295                 printf("\n");
1296         } else if (0 == strcmp("reboot", cmd)) {
1297                 int region;
1298                 enum ec_reboot_cmd cmd;
1299
1300                 if (argc >= 3 && !strcmp(argv[2], "cold"))
1301                         cmd = EC_REBOOT_COLD;
1302                 else {
1303                         region = cros_ec_decode_region(argc - 2, argv + 2);
1304                         if (region == EC_FLASH_REGION_RO)
1305                                 cmd = EC_REBOOT_JUMP_RO;
1306                         else if (region == EC_FLASH_REGION_RW)
1307                                 cmd = EC_REBOOT_JUMP_RW;
1308                         else
1309                                 return CMD_RET_USAGE;
1310                 }
1311
1312                 if (cros_ec_reboot(dev, cmd, 0)) {
1313                         debug("%s: Could not reboot KBC\n", __func__);
1314                         return 1;
1315                 }
1316         } else if (0 == strcmp("events", cmd)) {
1317                 uint32_t events;
1318
1319                 if (cros_ec_get_host_events(dev, &events)) {
1320                         debug("%s: Could not read host events\n", __func__);
1321                         return 1;
1322                 }
1323                 printf("0x%08x\n", events);
1324         } else if (0 == strcmp("clrevents", cmd)) {
1325                 uint32_t events = 0x7fffffff;
1326
1327                 if (argc >= 3)
1328                         events = simple_strtol(argv[2], NULL, 0);
1329
1330                 if (cros_ec_clear_host_events(dev, events)) {
1331                         debug("%s: Could not clear host events\n", __func__);
1332                         return 1;
1333                 }
1334         } else if (0 == strcmp("read", cmd)) {
1335                 ret = do_read_write(dev, 0, argc, argv);
1336                 if (ret > 0)
1337                         return CMD_RET_USAGE;
1338         } else if (0 == strcmp("write", cmd)) {
1339                 ret = do_read_write(dev, 1, argc, argv);
1340                 if (ret > 0)
1341                         return CMD_RET_USAGE;
1342         } else if (0 == strcmp("erase", cmd)) {
1343                 int region = cros_ec_decode_region(argc - 2, argv + 2);
1344                 uint32_t offset, size;
1345
1346                 if (region == -1)
1347                         return CMD_RET_USAGE;
1348                 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
1349                         debug("%s: Could not read region info\n", __func__);
1350                         ret = -1;
1351                 } else {
1352                         ret = cros_ec_flash_erase(dev, offset, size);
1353                         if (ret) {
1354                                 debug("%s: Could not erase region\n",
1355                                       __func__);
1356                         }
1357                 }
1358         } else if (0 == strcmp("regioninfo", cmd)) {
1359                 int region = cros_ec_decode_region(argc - 2, argv + 2);
1360                 uint32_t offset, size;
1361
1362                 if (region == -1)
1363                         return CMD_RET_USAGE;
1364                 ret = cros_ec_flash_offset(dev, region, &offset, &size);
1365                 if (ret) {
1366                         debug("%s: Could not read region info\n", __func__);
1367                 } else {
1368                         printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
1369                                         "RO" : "RW");
1370                         printf("Offset: %x\n", offset);
1371                         printf("Size:   %x\n", size);
1372                 }
1373         } else if (0 == strcmp("vbnvcontext", cmd)) {
1374                 uint8_t block[EC_VBNV_BLOCK_SIZE];
1375                 char buf[3];
1376                 int i, len;
1377                 unsigned long result;
1378
1379                 if (argc <= 2) {
1380                         ret = cros_ec_read_vbnvcontext(dev, block);
1381                         if (!ret) {
1382                                 printf("vbnv_block: ");
1383                                 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
1384                                         printf("%02x", block[i]);
1385                                 putc('\n');
1386                         }
1387                 } else {
1388                         /*
1389                          * TODO(clchiou): Move this to a utility function as
1390                          * cmd_spi might want to call it.
1391                          */
1392                         memset(block, 0, EC_VBNV_BLOCK_SIZE);
1393                         len = strlen(argv[2]);
1394                         buf[2] = '\0';
1395                         for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
1396                                 if (i * 2 >= len)
1397                                         break;
1398                                 buf[0] = argv[2][i * 2];
1399                                 if (i * 2 + 1 >= len)
1400                                         buf[1] = '0';
1401                                 else
1402                                         buf[1] = argv[2][i * 2 + 1];
1403                                 strict_strtoul(buf, 16, &result);
1404                                 block[i] = result;
1405                         }
1406                         ret = cros_ec_write_vbnvcontext(dev, block);
1407                 }
1408                 if (ret) {
1409                         debug("%s: Could not %s VbNvContext\n", __func__,
1410                                         argc <= 2 ?  "read" : "write");
1411                 }
1412         } else if (0 == strcmp("test", cmd)) {
1413                 int result = cros_ec_test(dev);
1414
1415                 if (result)
1416                         printf("Test failed with error %d\n", result);
1417                 else
1418                         puts("Test passed\n");
1419         } else if (0 == strcmp("version", cmd)) {
1420                 struct ec_response_get_version *p;
1421                 char *build_string;
1422
1423                 ret = cros_ec_read_version(dev, &p);
1424                 if (!ret) {
1425                         /* Print versions */
1426                         printf("RO version:    %1.*s\n",
1427                                sizeof(p->version_string_ro),
1428                                p->version_string_ro);
1429                         printf("RW version:    %1.*s\n",
1430                                sizeof(p->version_string_rw),
1431                                p->version_string_rw);
1432                         printf("Firmware copy: %s\n",
1433                                 (p->current_image <
1434                                         ARRAY_SIZE(ec_current_image_name) ?
1435                                 ec_current_image_name[p->current_image] :
1436                                 "?"));
1437                         ret = cros_ec_read_build_info(dev, &build_string);
1438                         if (!ret)
1439                                 printf("Build info:    %s\n", build_string);
1440                 }
1441         } else if (0 == strcmp("ldo", cmd)) {
1442                 uint8_t index, state;
1443                 char *endp;
1444
1445                 if (argc < 3)
1446                         return CMD_RET_USAGE;
1447                 index = simple_strtoul(argv[2], &endp, 10);
1448                 if (*argv[2] == 0 || *endp != 0)
1449                         return CMD_RET_USAGE;
1450                 if (argc > 3) {
1451                         state = simple_strtoul(argv[3], &endp, 10);
1452                         if (*argv[3] == 0 || *endp != 0)
1453                                 return CMD_RET_USAGE;
1454                         ret = cros_ec_set_ldo(dev, index, state);
1455                 } else {
1456                         ret = cros_ec_get_ldo(dev, index, &state);
1457                         if (!ret) {
1458                                 printf("LDO%d: %s\n", index,
1459                                         state == EC_LDO_STATE_ON ?
1460                                         "on" : "off");
1461                         }
1462                 }
1463
1464                 if (ret) {
1465                         debug("%s: Could not access LDO%d\n", __func__, index);
1466                         return ret;
1467                 }
1468         } else {
1469                 return CMD_RET_USAGE;
1470         }
1471
1472         if (ret < 0) {
1473                 printf("Error: CROS-EC command failed (error %d)\n", ret);
1474                 ret = 1;
1475         }
1476
1477         return ret;
1478 }
1479
1480 U_BOOT_CMD(
1481         crosec, 5,      1,      do_cros_ec,
1482         "CROS-EC utility command",
1483         "init                Re-init CROS-EC (done on startup automatically)\n"
1484         "crosec id                  Read CROS-EC ID\n"
1485         "crosec info                Read CROS-EC info\n"
1486         "crosec curimage            Read CROS-EC current image\n"
1487         "crosec hash                Read CROS-EC hash\n"
1488         "crosec reboot [rw | ro | cold]  Reboot CROS-EC\n"
1489         "crosec events              Read CROS-EC host events\n"
1490         "crosec clrevents [mask]    Clear CROS-EC host events\n"
1491         "crosec regioninfo <ro|rw>  Read image info\n"
1492         "crosec erase <ro|rw>       Erase EC image\n"
1493         "crosec read <ro|rw> <addr> [<size>]   Read EC image\n"
1494         "crosec write <ro|rw> <addr> [<size>]  Write EC image\n"
1495         "crosec vbnvcontext [hexstring]        Read [write] VbNvContext from EC\n"
1496         "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
1497         "crosec test                run tests on cros_ec\n"
1498         "crosec version             Read CROS-EC version"
1499 );
1500 #endif