Lines Matching +full:cros +full:- +full:ec +full:- +full:i2c
6 * SPDX-License-Identifier: GPL-2.0+
10 * This is the interface to the Chrome OS EC. It provides keyboard functions,
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
20 #include <i2c.h>
27 #include <asm-generic/gpio.h>
28 #include <dm/device-internal.h>
30 #include <dm/uclass-internal.h>
53 if (cmd != -1) in cros_ec_dump_data()
62 * Calculate a simple 8-bit checksum of a data block
82 * @param dev CROS-EC device
93 struct ec_host_request *rq = (struct ec_host_request *)dev->dout; in create_proto3_request()
97 if (out_bytes > (int)sizeof(dev->dout)) { in create_proto3_request()
99 return -EC_RES_REQUEST_TRUNCATED; in create_proto3_request()
103 rq->struct_version = EC_HOST_REQUEST_VERSION; in create_proto3_request()
104 rq->checksum = 0; in create_proto3_request()
105 rq->command = cmd; in create_proto3_request()
106 rq->command_version = cmd_version; in create_proto3_request()
107 rq->reserved = 0; in create_proto3_request()
108 rq->data_len = dout_len; in create_proto3_request()
114 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes)); in create_proto3_request()
116 cros_ec_dump_data("out", cmd, dev->dout, out_bytes); in create_proto3_request()
125 * @param dev CROS-EC device
134 if (in_bytes > (int)sizeof(dev->din)) { in prepare_proto3_response_buffer()
136 return -EC_RES_RESPONSE_TOO_BIG; in prepare_proto3_response_buffer()
148 * @param dev CROS-EC device
152 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
158 struct ec_host_response *rs = (struct ec_host_response *)dev->din; in handle_proto3_response()
162 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs)); in handle_proto3_response()
165 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) { in handle_proto3_response()
166 debug("%s: EC response version mismatch\n", __func__); in handle_proto3_response()
167 return -EC_RES_INVALID_RESPONSE; in handle_proto3_response()
170 if (rs->reserved) { in handle_proto3_response()
171 debug("%s: EC response reserved != 0\n", __func__); in handle_proto3_response()
172 return -EC_RES_INVALID_RESPONSE; in handle_proto3_response()
175 if (rs->data_len > din_len) { in handle_proto3_response()
176 debug("%s: EC returned too much data\n", __func__); in handle_proto3_response()
177 return -EC_RES_RESPONSE_TOO_BIG; in handle_proto3_response()
180 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len); in handle_proto3_response()
183 in_bytes = sizeof(*rs) + rs->data_len; in handle_proto3_response()
186 csum = cros_ec_calc_checksum(dev->din, in_bytes); in handle_proto3_response()
188 debug("%s: EC response checksum invalid: 0x%02x\n", __func__, in handle_proto3_response()
190 return -EC_RES_INVALID_CHECKSUM; in handle_proto3_response()
194 if (rs->result) in handle_proto3_response()
195 return -(int)rs->result; in handle_proto3_response()
200 return rs->data_len; in handle_proto3_response()
223 ops = dm_cros_ec_get_ops(dev->dev); in send_command_proto3()
224 rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS; in send_command_proto3()
237 int ret = -1; in send_command()
240 if (dev->protocol_version == 3) { in send_command()
245 ops = dm_cros_ec_get_ops(dev->dev); in send_command()
246 ret = ops->command(dev->dev, cmd, cmd_version, in send_command()
253 * Send a command to the CROS-EC device and return the reply.
257 * @param dev CROS-EC device
264 * and will always be double word aligned (64-bits)
266 * @return number of bytes in response, or -ve on error
279 if (len == -EC_RES_IN_PROGRESS) { in ec_command_inptr()
298 return -EC_RES_TIMEOUT; in ec_command_inptr()
300 } while (resp->flags & EC_COMMS_STATUS_PROCESSING); in ec_command_inptr()
310 /* If we have any data to return, it must be 64bit-aligned */ in ec_command_inptr()
319 * Send a command to the CROS-EC device and return the reply.
323 * @param dev CROS-EC device
332 * @return number of bytes in response, or -ve on error
362 sizeof(scan->data)) != sizeof(scan->data)) in cros_ec_scan_keyboard()
363 return -1; in cros_ec_scan_keyboard()
374 return -1; in cros_ec_read_id()
376 if (maxlen > (int)sizeof(r->version_string_ro)) in cros_ec_read_id()
377 maxlen = sizeof(r->version_string_ro); in cros_ec_read_id()
379 switch (r->current_image) { in cros_ec_read_id()
381 memcpy(id, r->version_string_ro, maxlen); in cros_ec_read_id()
384 memcpy(id, r->version_string_rw, maxlen); in cros_ec_read_id()
387 return -1; in cros_ec_read_id()
390 id[maxlen - 1] = '\0'; in cros_ec_read_id()
400 return -1; in cros_ec_read_version()
409 return -1; in cros_ec_read_build_info()
421 return -1; in cros_ec_read_current_image()
423 *image = r->current_image; in cros_ec_read_current_image()
434 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) { in cros_ec_wait_on_hash_done()
440 return -1; in cros_ec_wait_on_hash_done()
444 return -EC_RES_TIMEOUT; in cros_ec_wait_on_hash_done()
460 return -1; in cros_ec_read_hash()
462 /* If the EC is busy calculating the hash, fidget until it's done. */ in cros_ec_read_hash()
471 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size) in cros_ec_read_hash()
475 __func__, hash->status, hash->size); in cros_ec_read_hash()
484 return -1; in cros_ec_read_hash()
500 /* We don't have an explict command for the EC to discard its current in cros_ec_invalidate_hash()
514 return -1; in cros_ec_invalidate_hash()
530 return -1; in cros_ec_reboot()
534 * EC reboot will take place immediately so delay to allow it in cros_ec_reboot()
542 * we poll a memory-mapped LPC value? in cros_ec_reboot()
555 if (!dm_gpio_is_valid(&cdev->ec_int)) in cros_ec_interrupt_pending()
556 return -ENOENT; in cros_ec_interrupt_pending()
558 return dm_gpio_get_value(&cdev->ec_int); in cros_ec_interrupt_pending()
565 return -1; in cros_ec_info()
580 return -1; in cros_ec_get_host_events()
582 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID)) in cros_ec_get_host_events()
583 return -1; in cros_ec_get_host_events()
585 *events_ptr = resp->mask; in cros_ec_get_host_events()
601 return -1; in cros_ec_clear_host_events()
618 return -1; in cros_ec_flash_protect()
631 ops = dm_cros_ec_get_ops(dev->dev); in cros_ec_check_version()
632 if (ops->check_version) { in cros_ec_check_version()
633 ret = ops->check_version(dev->dev); in cros_ec_check_version()
640 * There is a strange oddity here with the EC. We could just ignore in cros_ec_check_version()
642 * In this case we won't read back very many bytes from the EC. in cros_ec_check_version()
643 * On the I2C bus the EC gets upset about this and will try to send in cros_ec_check_version()
645 * to complete before continuing with a new EC command. in cros_ec_check_version()
647 * This problem is probably unique to the I2C bus. in cros_ec_check_version()
653 dev->protocol_version = 3; in cros_ec_check_version()
661 dev->protocol_version = 2; in cros_ec_check_version()
668 * Fail if we're still here, since the EC doesn't understand any in cros_ec_check_version()
673 dev->protocol_version = 0; in cros_ec_check_version()
674 printf("%s: ERROR: old EC interface not supported\n", __func__); in cros_ec_check_version()
675 return -1; in cros_ec_check_version()
687 return -1; in cros_ec_test()
689 if (resp->out_data != req.in_data + 0x01020304) { in cros_ec_test()
690 printf("Received invalid handshake %x\n", resp->out_data); in cros_ec_test()
691 return -1; in cros_ec_test()
709 return -1; in cros_ec_flash_offset()
712 *offset = r->offset; in cros_ec_flash_offset()
714 *size = r->size; in cros_ec_flash_offset()
732 * Write a block of data to the EC flash. The size must not exceed the flash
738 * Attempting to write to the region where the EC is currently running from
741 * @param dev CROS-EC device
745 * @return 0 if ok, -1 on error
755 return -ENOMEM; in cros_ec_flash_write_block()
757 p->offset = offset; in cros_ec_flash_write_block()
758 p->size = size; in cros_ec_flash_write_block()
759 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE); in cros_ec_flash_write_block()
760 memcpy(p + 1, data, p->size); in cros_ec_flash_write_block()
763 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1; in cros_ec_flash_write_block()
784 * @param data Pointer to data to check (must be word-aligned)
785 * @param size Number of bytes to check (must be word-aligned)
786 * @return 0 if erased, non-zero if any word is not erased
792 for (; size > 0; size -= 4, data++) in cros_ec_data_is_erased()
793 if (*data != -1U) in cros_ec_data_is_erased()
802 * This function reads back parameters of the flash as reported by the EC
817 return ret < sizeof(*info) ? -1 : 0; in cros_ec_read_flashinfo()
836 todo = min(end - off, burst); in cros_ec_flash_write()
837 if (dev->optimise_flash_write && in cros_ec_flash_write()
852 * Read a block of data from the EC flash. The size must not exceed the flash
858 * @param dev CROS-EC device
862 * @return 0 if ok, -1 on error
873 &p, sizeof(p), data, size) >= 0 ? 0 : -1; in cros_ec_flash_read_block()
886 min(end - off, burst)); in cros_ec_flash_read()
901 return -1; in cros_ec_flash_update_rw()
903 return -1; in cros_ec_flash_update_rw()
906 * unexpectedly during the update. If that happened, the EC RW firmware in cros_ec_flash_update_rw()
907 * would be invalid, but the EC would still have the original hash. in cros_ec_flash_update_rw()
914 * Erase the entire RW section, so that the EC doesn't see any garbage in cros_ec_flash_update_rw()
943 return -1; in cros_ec_read_vbnvcontext()
959 return -1; in cros_ec_write_vbnvcontext()
974 return -1; in cros_ec_set_ldo()
990 return -1; in cros_ec_get_ldo()
992 *state = resp->state; in cros_ec_get_ldo()
1002 cdev->dev = dev; in cros_ec_register()
1003 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int, in cros_ec_register()
1005 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write"); in cros_ec_register()
1008 debug("%s: Could not detect CROS-EC version\n", __func__); in cros_ec_register()
1009 return -CROS_EC_ERR_CHECK_VERSION; in cros_ec_register()
1014 return -CROS_EC_ERR_READ_ID; in cros_ec_register()
1018 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n", in cros_ec_register()
1019 cdev->protocol_version, id); in cros_ec_register()
1031 return -1; in cros_ec_decode_ec_flash()
1034 if (of_read_fmap_entry(flash_node, "flash", &config->flash)) { in cros_ec_decode_ec_flash()
1035 debug("Failed to decode flash node in chrome-ec\n"); in cros_ec_decode_ec_flash()
1036 return -1; in cros_ec_decode_ec_flash()
1039 config->flash_erase_value = ofnode_read_s32_default(flash_node, in cros_ec_decode_ec_flash()
1040 "erase-value", -1); in cros_ec_decode_ec_flash()
1049 } else if (0 == strcmp(name, "wp-ro")) { in cros_ec_decode_ec_flash()
1052 debug("Unknown EC flash region name '%s'\n", name); in cros_ec_decode_ec_flash()
1053 return -1; in cros_ec_decode_ec_flash()
1056 if (of_read_fmap_entry(node, "reg", &config->region[region])) { in cros_ec_decode_ec_flash()
1057 debug("Failed to decode flash region in chrome-ec'\n"); in cros_ec_decode_ec_flash()
1058 return -1; in cros_ec_decode_ec_flash()
1086 p->port = port; in cros_ec_i2c_tunnel()
1088 p->num_msgs = nmsgs; in cros_ec_i2c_tunnel()
1089 size = sizeof(*p) + p->num_msgs * sizeof(*msg); in cros_ec_i2c_tunnel()
1095 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) { in cros_ec_i2c_tunnel()
1096 bool is_read = in->flags & I2C_M_RD; in cros_ec_i2c_tunnel()
1098 msg->addr_flags = in->addr; in cros_ec_i2c_tunnel()
1099 msg->len = in->len; in cros_ec_i2c_tunnel()
1101 msg->addr_flags |= EC_I2C_FLAG_READ; in cros_ec_i2c_tunnel()
1102 read_len += in->len; in cros_ec_i2c_tunnel()
1103 read_ptr = in->buf; in cros_ec_i2c_tunnel()
1106 return -1; in cros_ec_i2c_tunnel()
1109 if (pdata - (uint8_t *)p + in->len > sizeof(params)) { in cros_ec_i2c_tunnel()
1111 return -1; in cros_ec_i2c_tunnel()
1113 memcpy(pdata, in->buf, in->len); in cros_ec_i2c_tunnel()
1114 pdata += in->len; in cros_ec_i2c_tunnel()
1118 rv = ec_command(cdev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p, in cros_ec_i2c_tunnel()
1124 if (r->i2c_status & EC_I2C_STATUS_ERROR) { in cros_ec_i2c_tunnel()
1125 printf("Transfer failed with status=0x%x\n", r->i2c_status); in cros_ec_i2c_tunnel()
1126 return -1; in cros_ec_i2c_tunnel()
1131 return -1; in cros_ec_i2c_tunnel()
1136 memcpy(read_ptr, r->data, read_len); in cros_ec_i2c_tunnel()