2 * Chromium OS cros_ec driver
4 * Copyright (c) 2012 The Chromium OS Authors.
6 * SPDX-License-Identifier: GPL-2.0+
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
25 #include <linux/errno.h>
27 #include <asm-generic/gpio.h>
28 #include <dm/device-internal.h>
29 #include <dm/uclass-internal.h>
32 #define debug_trace(fmt, b...) debug(fmt, #b)
34 #define debug_trace(fmt, b...)
38 /* Timeout waiting for a flash erase command to complete */
39 CROS_EC_CMD_TIMEOUT_MS = 5000,
40 /* Timeout waiting for a synchronous hash to be recomputed */
41 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
44 DECLARE_GLOBAL_DATA_PTR;
46 /* Note: depends on enum ec_current_image */
47 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
49 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
56 printf("cmd=%#x: ", cmd);
57 for (i = 0; i < len; i++)
58 printf("%02x ", data[i]);
64 * Calculate a simple 8-bit checksum of a data block
66 * @param data Data block to checksum
67 * @param size Size of data block in bytes
68 * @return checksum value (0 to 255)
70 int cros_ec_calc_checksum(const uint8_t *data, int size)
74 for (i = csum = 0; i < size; i++)
80 * Create a request packet for protocol version 3.
82 * The packet is stored in the device's internal output buffer.
84 * @param dev CROS-EC device
85 * @param cmd Command to send (EC_CMD_...)
86 * @param cmd_version Version of command to send (EC_VER_...)
87 * @param dout Output data (may be NULL If dout_len=0)
88 * @param dout_len Size of output data in bytes
89 * @return packet size in bytes, or <0 if error.
91 static int create_proto3_request(struct cros_ec_dev *dev,
92 int cmd, int cmd_version,
93 const void *dout, int dout_len)
95 struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
96 int out_bytes = dout_len + sizeof(*rq);
98 /* Fail if output size is too big */
99 if (out_bytes > (int)sizeof(dev->dout)) {
100 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
101 return -EC_RES_REQUEST_TRUNCATED;
104 /* Fill in request packet */
105 rq->struct_version = EC_HOST_REQUEST_VERSION;
108 rq->command_version = cmd_version;
110 rq->data_len = dout_len;
112 /* Copy data after header */
113 memcpy(rq + 1, dout, dout_len);
115 /* Write checksum field so the entire packet sums to 0 */
116 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
118 cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
120 /* Return size of request packet */
125 * Prepare the device to receive a protocol version 3 response.
127 * @param dev CROS-EC device
128 * @param din_len Maximum size of response in bytes
129 * @return maximum expected number of bytes in response, or <0 if error.
131 static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
133 int in_bytes = din_len + sizeof(struct ec_host_response);
135 /* Fail if input size is too big */
136 if (in_bytes > (int)sizeof(dev->din)) {
137 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
138 return -EC_RES_RESPONSE_TOO_BIG;
141 /* Return expected size of response packet */
146 * Handle a protocol version 3 response packet.
148 * The packet must already be stored in the device's internal input buffer.
150 * @param dev CROS-EC device
151 * @param dinp Returns pointer to response data
152 * @param din_len Maximum size of response in bytes
153 * @return number of bytes of response data, or <0 if error. Note that error
154 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
157 static int handle_proto3_response(struct cros_ec_dev *dev,
158 uint8_t **dinp, int din_len)
160 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
164 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
166 /* Check input data */
167 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
168 debug("%s: EC response version mismatch\n", __func__);
169 return -EC_RES_INVALID_RESPONSE;
173 debug("%s: EC response reserved != 0\n", __func__);
174 return -EC_RES_INVALID_RESPONSE;
177 if (rs->data_len > din_len) {
178 debug("%s: EC returned too much data\n", __func__);
179 return -EC_RES_RESPONSE_TOO_BIG;
182 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
184 /* Update in_bytes to actual data size */
185 in_bytes = sizeof(*rs) + rs->data_len;
187 /* Verify checksum */
188 csum = cros_ec_calc_checksum(dev->din, in_bytes);
190 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
192 return -EC_RES_INVALID_CHECKSUM;
195 /* Return error result, if any */
197 return -(int)rs->result;
199 /* If we're still here, set response data pointer and return length */
200 *dinp = (uint8_t *)(rs + 1);
205 static int send_command_proto3(struct cros_ec_dev *dev,
206 int cmd, int cmd_version,
207 const void *dout, int dout_len,
208 uint8_t **dinp, int din_len)
210 struct dm_cros_ec_ops *ops;
211 int out_bytes, in_bytes;
214 /* Create request packet */
215 out_bytes = create_proto3_request(dev, cmd, cmd_version,
220 /* Prepare response buffer */
221 in_bytes = prepare_proto3_response_buffer(dev, din_len);
225 ops = dm_cros_ec_get_ops(dev->dev);
226 rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS;
230 /* Process the response */
231 return handle_proto3_response(dev, dinp, din_len);
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)
238 struct dm_cros_ec_ops *ops;
241 /* Handle protocol version 3 support */
242 if (dev->protocol_version == 3) {
243 return send_command_proto3(dev, cmd, cmd_version,
244 dout, dout_len, dinp, din_len);
247 ops = dm_cros_ec_get_ops(dev->dev);
248 ret = ops->command(dev->dev, cmd, cmd_version,
249 (const uint8_t *)dout, dout_len, dinp, din_len);
255 * Send a command to the CROS-EC device and return the reply.
257 * The device's internal input/output buffers are used.
259 * @param dev CROS-EC device
260 * @param cmd Command to send (EC_CMD_...)
261 * @param cmd_version Version of command to send (EC_VER_...)
262 * @param dout Output data (may be NULL If dout_len=0)
263 * @param dout_len Size of output data in bytes
264 * @param dinp Response data (may be NULL If din_len=0).
265 * If not NULL, it will be updated to point to the data
266 * and will always be double word aligned (64-bits)
267 * @param din_len Maximum size of response in bytes
268 * @return number of bytes in response, or -ve on error
270 static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
271 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
277 len = send_command(dev, cmd, cmd_version, dout, dout_len,
280 /* If the command doesn't complete, wait a while */
281 if (len == -EC_RES_IN_PROGRESS) {
282 struct ec_response_get_comms_status *resp = NULL;
285 /* Wait for command to complete */
286 start = get_timer(0);
290 mdelay(50); /* Insert some reasonable delay */
291 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
293 (uint8_t **)&resp, sizeof(*resp));
297 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
298 debug("%s: Command %#02x timeout\n",
300 return -EC_RES_TIMEOUT;
302 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
304 /* OK it completed, so read the status response */
305 /* not sure why it was 0 for the last argument */
306 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
307 NULL, 0, &din, din_len);
310 debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp,
311 dinp ? *dinp : NULL);
313 /* If we have any data to return, it must be 64bit-aligned */
314 assert(len <= 0 || !((uintptr_t)din & 7));
322 * Send a command to the CROS-EC device and return the reply.
324 * The device's internal input/output buffers are used.
326 * @param dev CROS-EC device
327 * @param cmd Command to send (EC_CMD_...)
328 * @param cmd_version Version of command to send (EC_VER_...)
329 * @param dout Output data (may be NULL If dout_len=0)
330 * @param dout_len Size of output data in bytes
331 * @param din Response data (may be NULL If din_len=0).
332 * It not NULL, it is a place for ec_command() to copy the
334 * @param din_len Maximum size of response in bytes
335 * @return number of bytes in response, or -ve on error
337 static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
338 const void *dout, int dout_len,
339 void *din, int din_len)
344 assert((din_len == 0) || din);
345 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
346 &in_buffer, din_len);
349 * If we were asked to put it somewhere, do so, otherwise just
350 * disregard the result.
352 if (din && in_buffer) {
353 assert(len <= din_len);
354 memmove(din, in_buffer, len);
360 int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
362 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
364 if (ec_command(cdev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
365 sizeof(scan->data)) != sizeof(scan->data))
371 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
373 struct ec_response_get_version *r;
375 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
376 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
379 if (maxlen > (int)sizeof(r->version_string_ro))
380 maxlen = sizeof(r->version_string_ro);
382 switch (r->current_image) {
384 memcpy(id, r->version_string_ro, maxlen);
387 memcpy(id, r->version_string_rw, maxlen);
393 id[maxlen - 1] = '\0';
397 int cros_ec_read_version(struct cros_ec_dev *dev,
398 struct ec_response_get_version **versionp)
400 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
401 (uint8_t **)versionp, sizeof(**versionp))
402 != sizeof(**versionp))
408 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
410 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
411 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
417 int cros_ec_read_current_image(struct cros_ec_dev *dev,
418 enum ec_current_image *image)
420 struct ec_response_get_version *r;
422 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
423 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
426 *image = r->current_image;
430 static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
431 struct ec_response_vboot_hash *hash)
433 struct ec_params_vboot_hash p;
436 start = get_timer(0);
437 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
438 mdelay(50); /* Insert some reasonable delay */
440 p.cmd = EC_VBOOT_HASH_GET;
441 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
442 hash, sizeof(*hash)) < 0)
445 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
446 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
447 return -EC_RES_TIMEOUT;
454 int cros_ec_read_hash(struct cros_ec_dev *dev,
455 struct ec_response_vboot_hash *hash)
457 struct ec_params_vboot_hash p;
460 p.cmd = EC_VBOOT_HASH_GET;
461 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
462 hash, sizeof(*hash)) < 0)
465 /* If the EC is busy calculating the hash, fidget until it's done. */
466 rv = cros_ec_wait_on_hash_done(dev, hash);
470 /* If the hash is valid, we're done. Otherwise, we have to kick it off
471 * again and wait for it to complete. Note that we explicitly assume
472 * that hashing zero bytes is always wrong, even though that would
473 * produce a valid hash value. */
474 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
477 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
478 __func__, hash->status, hash->size);
480 p.cmd = EC_VBOOT_HASH_START;
481 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
483 p.offset = EC_VBOOT_HASH_OFFSET_RW;
485 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
486 hash, sizeof(*hash)) < 0)
489 rv = cros_ec_wait_on_hash_done(dev, hash);
493 debug("%s: hash done\n", __func__);
498 static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
500 struct ec_params_vboot_hash p;
501 struct ec_response_vboot_hash *hash;
503 /* We don't have an explict command for the EC to discard its current
504 * hash value, so we'll just tell it to calculate one that we know is
505 * wrong (we claim that hashing zero bytes is always invalid).
507 p.cmd = EC_VBOOT_HASH_RECALC;
508 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
513 debug("%s:\n", __func__);
515 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
516 (uint8_t **)&hash, sizeof(*hash)) < 0)
519 /* No need to wait for it to finish */
523 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
526 struct ec_params_reboot_ec p;
531 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
535 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
537 * EC reboot will take place immediately so delay to allow it
538 * to complete. Note that some reboot types (EC_REBOOT_COLD)
539 * will reboot the AP as well, in which case we won't actually
543 * TODO(rspangler@chromium.org): Would be nice if we had a
544 * better way to determine when the reboot is complete. Could
545 * we poll a memory-mapped LPC value?
553 int cros_ec_interrupt_pending(struct udevice *dev)
555 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
557 /* no interrupt support : always poll */
558 if (!dm_gpio_is_valid(&cdev->ec_int))
561 return dm_gpio_get_value(&cdev->ec_int);
564 int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
566 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
567 sizeof(*info)) != sizeof(*info))
573 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
575 struct ec_response_host_event_mask *resp;
578 * Use the B copy of the event flags, because the main copy is already
581 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
582 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
585 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
588 *events_ptr = resp->mask;
592 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
594 struct ec_params_host_event_mask params;
596 params.mask = events;
599 * Use the B copy of the event flags, so it affects the data returned
600 * by cros_ec_get_host_events().
602 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
603 ¶ms, sizeof(params), NULL, 0) < 0)
609 int cros_ec_flash_protect(struct cros_ec_dev *dev,
610 uint32_t set_mask, uint32_t set_flags,
611 struct ec_response_flash_protect *resp)
613 struct ec_params_flash_protect params;
615 params.mask = set_mask;
616 params.flags = set_flags;
618 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
619 ¶ms, sizeof(params),
620 resp, sizeof(*resp)) != sizeof(*resp))
626 static int cros_ec_check_version(struct cros_ec_dev *dev)
628 struct ec_params_hello req;
629 struct ec_response_hello *resp;
631 struct dm_cros_ec_ops *ops;
634 ops = dm_cros_ec_get_ops(dev->dev);
635 if (ops->check_version) {
636 ret = ops->check_version(dev->dev);
642 * TODO(sjg@chromium.org).
643 * There is a strange oddity here with the EC. We could just ignore
644 * the response, i.e. pass the last two parameters as NULL and 0.
645 * In this case we won't read back very many bytes from the EC.
646 * On the I2C bus the EC gets upset about this and will try to send
647 * the bytes anyway. This means that we will have to wait for that
648 * to complete before continuing with a new EC command.
650 * This problem is probably unique to the I2C bus.
652 * So for now, just read all the data anyway.
655 /* Try sending a version 3 packet */
656 dev->protocol_version = 3;
658 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
659 (uint8_t **)&resp, sizeof(*resp)) > 0) {
663 /* Try sending a version 2 packet */
664 dev->protocol_version = 2;
665 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
666 (uint8_t **)&resp, sizeof(*resp)) > 0) {
671 * Fail if we're still here, since the EC doesn't understand any
672 * protcol version we speak. Version 1 interface without command
673 * version is no longer supported, and we don't know about any new
676 dev->protocol_version = 0;
677 printf("%s: ERROR: old EC interface not supported\n", __func__);
681 int cros_ec_test(struct cros_ec_dev *dev)
683 struct ec_params_hello req;
684 struct ec_response_hello *resp;
686 req.in_data = 0x12345678;
687 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
688 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
689 printf("ec_command_inptr() returned error\n");
692 if (resp->out_data != req.in_data + 0x01020304) {
693 printf("Received invalid handshake %x\n", resp->out_data);
700 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
701 uint32_t *offset, uint32_t *size)
703 struct ec_params_flash_region_info p;
704 struct ec_response_flash_region_info *r;
708 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
709 EC_VER_FLASH_REGION_INFO,
710 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
711 if (ret != sizeof(*r))
722 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
724 struct ec_params_flash_erase p;
728 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
733 * Write a single block to the flash
735 * Write a block of data to the EC flash. The size must not exceed the flash
736 * write block size which you can obtain from cros_ec_flash_write_burst_size().
738 * The offset starts at 0. You can obtain the region information from
739 * cros_ec_flash_offset() to find out where to write for a particular region.
741 * Attempting to write to the region where the EC is currently running from
742 * will result in an error.
744 * @param dev CROS-EC device
745 * @param data Pointer to data buffer to write
746 * @param offset Offset within flash to write to.
747 * @param size Number of bytes to write
748 * @return 0 if ok, -1 on error
750 static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
751 const uint8_t *data, uint32_t offset, uint32_t size)
753 struct ec_params_flash_write p;
757 assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
758 memcpy(&p + 1, data, p.size);
760 return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
761 &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
765 * Return optimal flash write burst size
767 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
769 return EC_FLASH_WRITE_VER0_SIZE;
773 * Check if a block of data is erased (all 0xff)
775 * This function is useful when dealing with flash, for checking whether a
776 * data block is erased and thus does not need to be programmed.
778 * @param data Pointer to data to check (must be word-aligned)
779 * @param size Number of bytes to check (must be word-aligned)
780 * @return 0 if erased, non-zero if any word is not erased
782 static int cros_ec_data_is_erased(const uint32_t *data, int size)
785 size /= sizeof(uint32_t);
786 for (; size > 0; size -= 4, data++)
794 * Read back flash parameters
796 * This function reads back parameters of the flash as reported by the EC
798 * @param dev Pointer to device
799 * @param info Pointer to output flash info struct
801 int cros_ec_read_flashinfo(struct cros_ec_dev *dev,
802 struct ec_response_flash_info *info)
806 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
807 NULL, 0, info, sizeof(*info));
811 return ret < sizeof(*info) ? -1 : 0;
814 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
815 uint32_t offset, uint32_t size)
817 uint32_t burst = cros_ec_flash_write_burst_size(dev);
822 * TODO: round up to the nearest multiple of write size. Can get away
823 * without that on link right now because its write size is 4 bytes.
826 for (off = offset; off < end; off += burst, data += burst) {
829 /* If the data is empty, there is no point in programming it */
830 todo = min(end - off, burst);
831 if (dev->optimise_flash_write &&
832 cros_ec_data_is_erased((uint32_t *)data, todo))
835 ret = cros_ec_flash_write_block(dev, data, off, todo);
844 * Read a single block from the flash
846 * Read a block of data from the EC flash. The size must not exceed the flash
847 * write block size which you can obtain from cros_ec_flash_write_burst_size().
849 * The offset starts at 0. You can obtain the region information from
850 * cros_ec_flash_offset() to find out where to read for a particular region.
852 * @param dev CROS-EC device
853 * @param data Pointer to data buffer to read into
854 * @param offset Offset within flash to read from
855 * @param size Number of bytes to read
856 * @return 0 if ok, -1 on error
858 static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
859 uint32_t offset, uint32_t size)
861 struct ec_params_flash_read p;
866 return ec_command(dev, EC_CMD_FLASH_READ, 0,
867 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
870 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
873 uint32_t burst = cros_ec_flash_write_burst_size(dev);
878 for (off = offset; off < end; off += burst, data += burst) {
879 ret = cros_ec_flash_read_block(dev, data, off,
880 min(end - off, burst));
888 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
889 const uint8_t *image, int image_size)
891 uint32_t rw_offset, rw_size;
894 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
896 if (image_size > (int)rw_size)
899 /* Invalidate the existing hash, just in case the AP reboots
900 * unexpectedly during the update. If that happened, the EC RW firmware
901 * would be invalid, but the EC would still have the original hash.
903 ret = cros_ec_invalidate_hash(dev);
908 * Erase the entire RW section, so that the EC doesn't see any garbage
909 * past the new image if it's smaller than the current image.
911 * TODO: could optimize this to erase just the current image, since
912 * presumably everything past that is 0xff's. But would still need to
913 * round up to the nearest multiple of erase size.
915 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
919 /* Write the image */
920 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
927 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
929 struct ec_params_vbnvcontext p;
932 p.op = EC_VBNV_CONTEXT_OP_READ;
934 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
935 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
936 if (len < EC_VBNV_BLOCK_SIZE)
942 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
944 struct ec_params_vbnvcontext p;
947 p.op = EC_VBNV_CONTEXT_OP_WRITE;
948 memcpy(p.block, block, sizeof(p.block));
950 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
951 &p, sizeof(p), NULL, 0);
958 int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
960 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
961 struct ec_params_ldo_set params;
963 params.index = index;
964 params.state = state;
966 if (ec_command_inptr(cdev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params),
973 int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
975 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
976 struct ec_params_ldo_get params;
977 struct ec_response_ldo_get *resp;
979 params.index = index;
981 if (ec_command_inptr(cdev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params),
982 (uint8_t **)&resp, sizeof(*resp)) !=
986 *state = resp->state;
991 int cros_ec_register(struct udevice *dev)
993 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
994 const void *blob = gd->fdt_blob;
995 int node = dev->of_offset;
999 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1001 cdev->optimise_flash_write = fdtdec_get_bool(blob, node,
1002 "optimise-flash-write");
1004 if (cros_ec_check_version(cdev)) {
1005 debug("%s: Could not detect CROS-EC version\n", __func__);
1006 return -CROS_EC_ERR_CHECK_VERSION;
1009 if (cros_ec_read_id(cdev, id, sizeof(id))) {
1010 debug("%s: Could not read KBC ID\n", __func__);
1011 return -CROS_EC_ERR_READ_ID;
1014 /* Remember this device for use by the cros_ec command */
1015 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1016 cdev->protocol_version, id);
1021 int cros_ec_decode_region(int argc, char * const argv[])
1024 if (0 == strcmp(*argv, "rw"))
1025 return EC_FLASH_REGION_RW;
1026 else if (0 == strcmp(*argv, "ro"))
1027 return EC_FLASH_REGION_RO;
1029 debug("%s: Invalid region '%s'\n", __func__, *argv);
1031 debug("%s: Missing region parameter\n", __func__);
1037 int cros_ec_decode_ec_flash(const void *blob, int node,
1038 struct fdt_cros_ec *config)
1042 flash_node = fdt_subnode_offset(blob, node, "flash");
1043 if (flash_node < 0) {
1044 debug("Failed to find flash node\n");
1048 if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
1050 debug("Failed to decode flash node in chrome-ec'\n");
1054 config->flash_erase_value = fdtdec_get_int(blob, flash_node,
1056 for (node = fdt_first_subnode(blob, flash_node); node >= 0;
1057 node = fdt_next_subnode(blob, node)) {
1058 const char *name = fdt_get_name(blob, node, NULL);
1059 enum ec_flash_region region;
1061 if (0 == strcmp(name, "ro")) {
1062 region = EC_FLASH_REGION_RO;
1063 } else if (0 == strcmp(name, "rw")) {
1064 region = EC_FLASH_REGION_RW;
1065 } else if (0 == strcmp(name, "wp-ro")) {
1066 region = EC_FLASH_REGION_WP_RO;
1068 debug("Unknown EC flash region name '%s'\n", name);
1072 if (fdtdec_read_fmap_entry(blob, node, "reg",
1073 &config->region[region])) {
1074 debug("Failed to decode flash region in chrome-ec'\n");
1082 int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1085 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1087 struct ec_params_i2c_passthru p;
1088 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1091 struct ec_response_i2c_passthru r;
1092 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1094 struct ec_params_i2c_passthru *p = ¶ms.p;
1095 struct ec_response_i2c_passthru *r = &response.r;
1096 struct ec_params_i2c_passthru_msg *msg;
1097 uint8_t *pdata, *read_ptr = NULL;
1105 p->num_msgs = nmsgs;
1106 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1108 /* Create a message to write the register address and optional data */
1109 pdata = (uint8_t *)p + size;
1112 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1113 bool is_read = in->flags & I2C_M_RD;
1115 msg->addr_flags = in->addr;
1118 msg->addr_flags |= EC_I2C_FLAG_READ;
1119 read_len += in->len;
1121 if (sizeof(*r) + read_len > sizeof(response)) {
1122 puts("Read length too big for buffer\n");
1126 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1127 puts("Params too large for buffer\n");
1130 memcpy(pdata, in->buf, in->len);
1135 rv = ec_command(cdev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
1136 r, sizeof(*r) + read_len);
1140 /* Parse response */
1141 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1142 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1146 if (rv < sizeof(*r) + read_len) {
1147 puts("Truncated read response\n");
1151 /* We only support a single read message for each transfer */
1153 memcpy(read_ptr, r->data, read_len);
1158 #ifdef CONFIG_CMD_CROS_EC
1161 * Perform a flash read or write command
1163 * @param dev CROS-EC device to read/write
1164 * @param is_write 1 do to a write, 0 to do a read
1165 * @param argc Number of arguments
1166 * @param argv Arguments (2 is region, 3 is address)
1167 * @return 0 for ok, 1 for a usage error or -ve for ec command error
1168 * (negative EC_RES_...)
1170 static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
1171 char * const argv[])
1173 uint32_t offset, size = -1U, region_size;
1179 region = cros_ec_decode_region(argc - 2, argv + 2);
1184 addr = simple_strtoul(argv[3], &endp, 16);
1185 if (*argv[3] == 0 || *endp != 0)
1188 size = simple_strtoul(argv[4], &endp, 16);
1189 if (*argv[4] == 0 || *endp != 0)
1193 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
1195 debug("%s: Could not read region info\n", __func__);
1202 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
1203 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
1205 debug("%s: Could not %s region\n", __func__,
1206 is_write ? "write" : "read");
1213 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1215 struct cros_ec_dev *dev;
1216 struct udevice *udev;
1221 return CMD_RET_USAGE;
1224 if (0 == strcmp("init", cmd)) {
1225 /* Remove any existing device */
1226 ret = uclass_find_device(UCLASS_CROS_EC, 0, &udev);
1228 device_remove(udev);
1229 ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
1231 printf("Could not init cros_ec device (err %d)\n", ret);
1237 ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
1239 printf("Cannot get cros-ec device (err=%d)\n", ret);
1242 dev = dev_get_uclass_priv(udev);
1243 if (0 == strcmp("id", cmd)) {
1246 if (cros_ec_read_id(dev, id, sizeof(id))) {
1247 debug("%s: Could not read KBC ID\n", __func__);
1251 } else if (0 == strcmp("info", cmd)) {
1252 struct ec_response_mkbp_info info;
1254 if (cros_ec_info(dev, &info)) {
1255 debug("%s: Could not read KBC info\n", __func__);
1258 printf("rows = %u\n", info.rows);
1259 printf("cols = %u\n", info.cols);
1260 printf("switches = %#x\n", info.switches);
1261 } else if (0 == strcmp("curimage", cmd)) {
1262 enum ec_current_image image;
1264 if (cros_ec_read_current_image(dev, &image)) {
1265 debug("%s: Could not read KBC image\n", __func__);
1268 printf("%d\n", image);
1269 } else if (0 == strcmp("hash", cmd)) {
1270 struct ec_response_vboot_hash hash;
1273 if (cros_ec_read_hash(dev, &hash)) {
1274 debug("%s: Could not read KBC hash\n", __func__);
1278 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
1279 printf("type: SHA-256\n");
1281 printf("type: %d\n", hash.hash_type);
1283 printf("offset: 0x%08x\n", hash.offset);
1284 printf("size: 0x%08x\n", hash.size);
1287 for (i = 0; i < hash.digest_size; i++)
1288 printf("%02x", hash.hash_digest[i]);
1290 } else if (0 == strcmp("reboot", cmd)) {
1292 enum ec_reboot_cmd cmd;
1294 if (argc >= 3 && !strcmp(argv[2], "cold"))
1295 cmd = EC_REBOOT_COLD;
1297 region = cros_ec_decode_region(argc - 2, argv + 2);
1298 if (region == EC_FLASH_REGION_RO)
1299 cmd = EC_REBOOT_JUMP_RO;
1300 else if (region == EC_FLASH_REGION_RW)
1301 cmd = EC_REBOOT_JUMP_RW;
1303 return CMD_RET_USAGE;
1306 if (cros_ec_reboot(dev, cmd, 0)) {
1307 debug("%s: Could not reboot KBC\n", __func__);
1310 } else if (0 == strcmp("events", cmd)) {
1313 if (cros_ec_get_host_events(dev, &events)) {
1314 debug("%s: Could not read host events\n", __func__);
1317 printf("0x%08x\n", events);
1318 } else if (0 == strcmp("clrevents", cmd)) {
1319 uint32_t events = 0x7fffffff;
1322 events = simple_strtol(argv[2], NULL, 0);
1324 if (cros_ec_clear_host_events(dev, events)) {
1325 debug("%s: Could not clear host events\n", __func__);
1328 } else if (0 == strcmp("read", cmd)) {
1329 ret = do_read_write(dev, 0, argc, argv);
1331 return CMD_RET_USAGE;
1332 } else if (0 == strcmp("write", cmd)) {
1333 ret = do_read_write(dev, 1, argc, argv);
1335 return CMD_RET_USAGE;
1336 } else if (0 == strcmp("erase", cmd)) {
1337 int region = cros_ec_decode_region(argc - 2, argv + 2);
1338 uint32_t offset, size;
1341 return CMD_RET_USAGE;
1342 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
1343 debug("%s: Could not read region info\n", __func__);
1346 ret = cros_ec_flash_erase(dev, offset, size);
1348 debug("%s: Could not erase region\n",
1352 } else if (0 == strcmp("regioninfo", cmd)) {
1353 int region = cros_ec_decode_region(argc - 2, argv + 2);
1354 uint32_t offset, size;
1357 return CMD_RET_USAGE;
1358 ret = cros_ec_flash_offset(dev, region, &offset, &size);
1360 debug("%s: Could not read region info\n", __func__);
1362 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
1364 printf("Offset: %x\n", offset);
1365 printf("Size: %x\n", size);
1367 } else if (0 == strcmp("vbnvcontext", cmd)) {
1368 uint8_t block[EC_VBNV_BLOCK_SIZE];
1371 unsigned long result;
1374 ret = cros_ec_read_vbnvcontext(dev, block);
1376 printf("vbnv_block: ");
1377 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
1378 printf("%02x", block[i]);
1383 * TODO(clchiou): Move this to a utility function as
1384 * cmd_spi might want to call it.
1386 memset(block, 0, EC_VBNV_BLOCK_SIZE);
1387 len = strlen(argv[2]);
1389 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
1392 buf[0] = argv[2][i * 2];
1393 if (i * 2 + 1 >= len)
1396 buf[1] = argv[2][i * 2 + 1];
1397 strict_strtoul(buf, 16, &result);
1400 ret = cros_ec_write_vbnvcontext(dev, block);
1403 debug("%s: Could not %s VbNvContext\n", __func__,
1404 argc <= 2 ? "read" : "write");
1406 } else if (0 == strcmp("test", cmd)) {
1407 int result = cros_ec_test(dev);
1410 printf("Test failed with error %d\n", result);
1412 puts("Test passed\n");
1413 } else if (0 == strcmp("version", cmd)) {
1414 struct ec_response_get_version *p;
1417 ret = cros_ec_read_version(dev, &p);
1419 /* Print versions */
1420 printf("RO version: %1.*s\n",
1421 (int)sizeof(p->version_string_ro),
1422 p->version_string_ro);
1423 printf("RW version: %1.*s\n",
1424 (int)sizeof(p->version_string_rw),
1425 p->version_string_rw);
1426 printf("Firmware copy: %s\n",
1428 ARRAY_SIZE(ec_current_image_name) ?
1429 ec_current_image_name[p->current_image] :
1431 ret = cros_ec_read_build_info(dev, &build_string);
1433 printf("Build info: %s\n", build_string);
1435 } else if (0 == strcmp("ldo", cmd)) {
1436 uint8_t index, state;
1440 return CMD_RET_USAGE;
1441 index = simple_strtoul(argv[2], &endp, 10);
1442 if (*argv[2] == 0 || *endp != 0)
1443 return CMD_RET_USAGE;
1445 state = simple_strtoul(argv[3], &endp, 10);
1446 if (*argv[3] == 0 || *endp != 0)
1447 return CMD_RET_USAGE;
1448 ret = cros_ec_set_ldo(udev, index, state);
1450 ret = cros_ec_get_ldo(udev, index, &state);
1452 printf("LDO%d: %s\n", index,
1453 state == EC_LDO_STATE_ON ?
1459 debug("%s: Could not access LDO%d\n", __func__, index);
1463 return CMD_RET_USAGE;
1467 printf("Error: CROS-EC command failed (error %d)\n", ret);
1475 crosec, 6, 1, do_cros_ec,
1476 "CROS-EC utility command",
1477 "init Re-init CROS-EC (done on startup automatically)\n"
1478 "crosec id Read CROS-EC ID\n"
1479 "crosec info Read CROS-EC info\n"
1480 "crosec curimage Read CROS-EC current image\n"
1481 "crosec hash Read CROS-EC hash\n"
1482 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
1483 "crosec events Read CROS-EC host events\n"
1484 "crosec clrevents [mask] Clear CROS-EC host events\n"
1485 "crosec regioninfo <ro|rw> Read image info\n"
1486 "crosec erase <ro|rw> Erase EC image\n"
1487 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
1488 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
1489 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
1490 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
1491 "crosec test run tests on cros_ec\n"
1492 "crosec version Read CROS-EC version"
1496 UCLASS_DRIVER(cros_ec) = {
1497 .id = UCLASS_CROS_EC,
1499 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
1500 .post_bind = dm_scan_fdt_dev,