Lines Matching +full:cros +full:- +full:ec +full:- +full:spi +full:- +full:msg +full:- +full:delay
1 // SPDX-License-Identifier: GPL-2.0+
9 * This is the interface to the Chrome OS EC. It provides keyboard functions,
11 * provided to enable the EC software to be updated, talk to the EC's I2C bus
12 * and store a small amount of data in a memory which persists while the EC
25 #include <spi.h>
28 #include <asm-generic/gpio.h>
29 #include <dm/device-internal.h>
31 #include <dm/uclass-internal.h>
49 * Map UHEPI masks to non UHEPI commands in order to support old EC FW
100 if (cmd != -1) in cros_ec_dump_data()
109 * Calculate a simple 8-bit checksum of a data block
129 * @param dev CROS-EC device
140 struct ec_host_request *rq = (struct ec_host_request *)cdev->dout; in create_proto3_request()
144 if (out_bytes > (int)sizeof(cdev->dout)) { in create_proto3_request()
146 return -EC_RES_REQUEST_TRUNCATED; in create_proto3_request()
150 rq->struct_version = EC_HOST_REQUEST_VERSION; in create_proto3_request()
151 rq->checksum = 0; in create_proto3_request()
152 rq->command = cmd; in create_proto3_request()
153 rq->command_version = cmd_version; in create_proto3_request()
154 rq->reserved = 0; in create_proto3_request()
155 rq->data_len = dout_len; in create_proto3_request()
161 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes)); in create_proto3_request()
163 cros_ec_dump_data("out", cmd, cdev->dout, out_bytes); in create_proto3_request()
172 * @param dev CROS-EC device
181 if (in_bytes > (int)sizeof(cdev->din)) { in prepare_proto3_response_buffer()
183 return -EC_RES_RESPONSE_TOO_BIG; in prepare_proto3_response_buffer()
195 * @param dev CROS-EC device
199 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
205 struct ec_host_response *rs = (struct ec_host_response *)dev->din; in handle_proto3_response()
209 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs)); in handle_proto3_response()
212 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) { in handle_proto3_response()
213 debug("%s: EC response version mismatch\n", __func__); in handle_proto3_response()
214 return -EC_RES_INVALID_RESPONSE; in handle_proto3_response()
217 if (rs->reserved) { in handle_proto3_response()
218 debug("%s: EC response reserved != 0\n", __func__); in handle_proto3_response()
219 return -EC_RES_INVALID_RESPONSE; in handle_proto3_response()
222 if (rs->data_len > din_len) { in handle_proto3_response()
223 debug("%s: EC returned too much data\n", __func__); in handle_proto3_response()
224 return -EC_RES_RESPONSE_TOO_BIG; in handle_proto3_response()
227 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len); in handle_proto3_response()
230 in_bytes = sizeof(*rs) + rs->data_len; in handle_proto3_response()
233 csum = cros_ec_calc_checksum(dev->din, in_bytes); in handle_proto3_response()
235 debug("%s: EC response checksum invalid: 0x%02x\n", __func__, in handle_proto3_response()
237 return -EC_RES_INVALID_CHECKSUM; in handle_proto3_response()
241 if (rs->result) in handle_proto3_response()
242 return -(int)rs->result; in handle_proto3_response()
247 return rs->data_len; in handle_proto3_response()
270 ops = dm_cros_ec_get_ops(cdev->dev); in send_command_proto3()
271 rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) : in send_command_proto3()
272 -ENOSYS; in send_command_proto3()
285 int ret = -1; in send_command()
288 if (dev->protocol_version == 3) { in send_command()
293 ops = dm_cros_ec_get_ops(dev->dev); in send_command()
294 ret = ops->command(dev->dev, cmd, cmd_version, in send_command()
301 * Send a command to the CROS-EC device and return the reply.
305 * @param dev CROS-EC device
312 * and will always be double word aligned (64-bits)
314 * @return number of bytes in response, or -ve on error
328 if (len == -EC_RES_IN_PROGRESS) { in ec_command_inptr()
337 mdelay(50); /* Insert some reasonable delay */ in ec_command_inptr()
347 return -EC_RES_TIMEOUT; in ec_command_inptr()
349 } while (resp->flags & EC_COMMS_STATUS_PROCESSING); in ec_command_inptr()
359 /* If we have any data to return, it must be 64bit-aligned */ in ec_command_inptr()
368 * Send a command to the CROS-EC device and return the reply.
372 * @param dev CROS-EC device
381 * @return number of bytes in response, or -ve on error
409 sizeof(scan->data)) != sizeof(scan->data)) in cros_ec_scan_keyboard()
410 return -1; in cros_ec_scan_keyboard()
424 return -1; in cros_ec_read_id()
427 if (maxlen > (int)sizeof(r->version_string_ro)) in cros_ec_read_id()
428 maxlen = sizeof(r->version_string_ro); in cros_ec_read_id()
430 switch (r->current_image) { in cros_ec_read_id()
432 memcpy(id, r->version_string_ro, maxlen); in cros_ec_read_id()
435 memcpy(id, r->version_string_rw, maxlen); in cros_ec_read_id()
438 log_err("Invalid EC image %d\n", r->current_image); in cros_ec_read_id()
439 return -1; in cros_ec_read_id()
442 id[maxlen - 1] = '\0'; in cros_ec_read_id()
452 return -1; in cros_ec_read_version()
461 return -1; in cros_ec_read_build_info()
473 return -1; in cros_ec_read_current_image()
475 *image = r->current_image; in cros_ec_read_current_image()
486 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) { in cros_ec_wait_on_hash_done()
487 mdelay(50); /* Insert some reasonable delay */ in cros_ec_wait_on_hash_done()
492 return -1; in cros_ec_wait_on_hash_done()
496 return -EC_RES_TIMEOUT; in cros_ec_wait_on_hash_done()
512 return -1; in cros_ec_read_hash()
514 /* If the EC is busy calculating the hash, fidget until it's done. */ in cros_ec_read_hash()
523 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size) in cros_ec_read_hash()
527 __func__, hash->status, hash->size); in cros_ec_read_hash()
536 return -1; in cros_ec_read_hash()
552 /* We don't have an explict command for the EC to discard its current in cros_ec_invalidate_hash()
566 return -1; in cros_ec_invalidate_hash()
581 return -1; in cros_ec_reboot()
585 * EC reboot will take place immediately so delay to allow it in cros_ec_reboot()
593 * we poll a memory-mapped LPC value? in cros_ec_reboot()
606 if (!dm_gpio_is_valid(&cdev->ec_int)) in cros_ec_interrupt_pending()
607 return -ENOENT; in cros_ec_interrupt_pending()
609 return dm_gpio_get_value(&cdev->ec_int); in cros_ec_interrupt_pending()
616 return -1; in cros_ec_info()
630 return -EINVAL; in cros_ec_get_event_mask()
661 return -1; in cros_ec_get_host_events()
663 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID)) in cros_ec_get_host_events()
664 return -1; in cros_ec_get_host_events()
666 *events_ptr = resp->mask; in cros_ec_get_host_events()
682 return -1; in cros_ec_clear_host_events()
699 return -1; in cros_ec_flash_protect()
711 return -1; in cros_ec_entering_mode()
725 if (ops->check_version) { in cros_ec_check_version()
726 ret = ops->check_version(dev); in cros_ec_check_version()
733 * There is a strange oddity here with the EC. We could just ignore in cros_ec_check_version()
735 * In this case we won't read back very many bytes from the EC. in cros_ec_check_version()
736 * On the I2C bus the EC gets upset about this and will try to send in cros_ec_check_version()
738 * to complete before continuing with a new EC command. in cros_ec_check_version()
746 cdev->protocol_version = 3; in cros_ec_check_version()
753 cdev->protocol_version = 2; in cros_ec_check_version()
759 * Fail if we're still here, since the EC doesn't understand any in cros_ec_check_version()
764 cdev->protocol_version = 0; in cros_ec_check_version()
765 printf("%s: ERROR: old EC interface not supported\n", __func__); in cros_ec_check_version()
766 return -1; in cros_ec_check_version()
778 return -1; in cros_ec_test()
780 if (resp->out_data != req.in_data + 0x01020304) { in cros_ec_test()
781 printf("Received invalid handshake %x\n", resp->out_data); in cros_ec_test()
782 return -1; in cros_ec_test()
800 return -1; in cros_ec_flash_offset()
803 *offset = r->offset; in cros_ec_flash_offset()
805 *size = r->size; in cros_ec_flash_offset()
823 * Write a block of data to the EC flash. The size must not exceed the flash
829 * Attempting to write to the region where the EC is currently running from
832 * @param dev CROS-EC device
836 * @return 0 if ok, -1 on error
846 return -ENOMEM; in cros_ec_flash_write_block()
848 p->offset = offset; in cros_ec_flash_write_block()
849 p->size = size; in cros_ec_flash_write_block()
850 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE); in cros_ec_flash_write_block()
851 memcpy(p + 1, data, p->size); in cros_ec_flash_write_block()
854 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1; in cros_ec_flash_write_block()
875 * @param data Pointer to data to check (must be word-aligned)
876 * @param size Number of bytes to check (must be word-aligned)
877 * @return 0 if erased, non-zero if any word is not erased
883 for (; size > 0; size -= 4, data++) in cros_ec_data_is_erased()
884 if (*data != -1U) in cros_ec_data_is_erased()
893 * This function reads back parameters of the flash as reported by the EC
908 return ret < sizeof(*info) ? -1 : 0; in cros_ec_read_flashinfo()
920 return -EINVAL; in cros_ec_flash_write()
931 todo = min(end - off, burst); in cros_ec_flash_write()
932 if (cdev->optimise_flash_write && in cros_ec_flash_write()
949 * @return 0 if success or not applicable. Non-zero if verification failed.
956 log_info("EFS: EC is verifying updated image...\n"); in cros_ec_efs_verify()
964 if (rv == -EC_RES_INVALID_COMMAND) { in cros_ec_efs_verify()
965 log_info("EFS: EC doesn't support EFS_VERIFY command\n"); in cros_ec_efs_verify()
976 * Read a block of data from the EC flash. The size must not exceed the flash
982 * @param dev CROS-EC device
986 * @return 0 if ok, -1 on error
997 &p, sizeof(p), data, size) >= 0 ? 0 : -1; in cros_ec_flash_read_block()
1010 min(end - off, burst)); in cros_ec_flash_read()
1026 return -1; in cros_ec_flash_update_rw()
1028 return -1; in cros_ec_flash_update_rw()
1031 * unexpectedly during the update. If that happened, the EC RW firmware in cros_ec_flash_update_rw()
1032 * would be invalid, but the EC would still have the original hash. in cros_ec_flash_update_rw()
1039 * Erase the entire RW section, so that the EC doesn't see any garbage in cros_ec_flash_update_rw()
1064 return -EINVAL; in cros_ec_read_nvdata()
1072 return -EIO; in cros_ec_read_nvdata()
1084 return -EINVAL; in cros_ec_write_nvdata()
1091 return -1; in cros_ec_write_nvdata()
1106 return -1; in cros_ec_battery_cutoff()
1119 return -1; in cros_ec_set_ldo()
1134 return -1; in cros_ec_get_ldo()
1136 *state = resp->state; in cros_ec_get_ldo()
1146 cdev->dev = dev; in cros_ec_register()
1147 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int, in cros_ec_register()
1149 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write"); in cros_ec_register()
1152 debug("%s: Could not detect CROS-EC version\n", __func__); in cros_ec_register()
1153 return -CROS_EC_ERR_CHECK_VERSION; in cros_ec_register()
1158 return -CROS_EC_ERR_READ_ID; in cros_ec_register()
1162 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n", in cros_ec_register()
1163 cdev->protocol_version, id); in cros_ec_register()
1175 return -1; in cros_ec_decode_ec_flash()
1178 if (ofnode_read_fmap_entry(flash_node, &config->flash)) { in cros_ec_decode_ec_flash()
1179 debug("Failed to decode flash node in chrome-ec\n"); in cros_ec_decode_ec_flash()
1180 return -1; in cros_ec_decode_ec_flash()
1183 config->flash_erase_value = ofnode_read_s32_default(flash_node, in cros_ec_decode_ec_flash()
1184 "erase-value", -1); in cros_ec_decode_ec_flash()
1193 } else if (0 == strcmp(name, "wp-ro")) { in cros_ec_decode_ec_flash()
1196 debug("Unknown EC flash region name '%s'\n", name); in cros_ec_decode_ec_flash()
1197 return -1; in cros_ec_decode_ec_flash()
1200 if (ofnode_read_fmap_entry(node, &config->region[region])) { in cros_ec_decode_ec_flash()
1201 debug("Failed to decode flash region in chrome-ec'\n"); in cros_ec_decode_ec_flash()
1202 return -1; in cros_ec_decode_ec_flash()
1222 struct ec_params_i2c_passthru_msg *msg; in cros_ec_i2c_tunnel() local
1229 p->port = port; in cros_ec_i2c_tunnel()
1231 p->num_msgs = nmsgs; in cros_ec_i2c_tunnel()
1232 size = sizeof(*p) + p->num_msgs * sizeof(*msg); in cros_ec_i2c_tunnel()
1238 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) { in cros_ec_i2c_tunnel()
1239 bool is_read = in->flags & I2C_M_RD; in cros_ec_i2c_tunnel()
1241 msg->addr_flags = in->addr; in cros_ec_i2c_tunnel()
1242 msg->len = in->len; in cros_ec_i2c_tunnel()
1244 msg->addr_flags |= EC_I2C_FLAG_READ; in cros_ec_i2c_tunnel()
1245 read_len += in->len; in cros_ec_i2c_tunnel()
1246 read_ptr = in->buf; in cros_ec_i2c_tunnel()
1249 return -1; in cros_ec_i2c_tunnel()
1252 if (pdata - (uint8_t *)p + in->len > sizeof(params)) { in cros_ec_i2c_tunnel()
1254 return -1; in cros_ec_i2c_tunnel()
1256 memcpy(pdata, in->buf, in->len); in cros_ec_i2c_tunnel()
1257 pdata += in->len; in cros_ec_i2c_tunnel()
1261 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p, in cros_ec_i2c_tunnel()
1267 if (r->i2c_status & EC_I2C_STATUS_ERROR) { in cros_ec_i2c_tunnel()
1268 printf("Transfer failed with status=0x%x\n", r->i2c_status); in cros_ec_i2c_tunnel()
1269 return -1; in cros_ec_i2c_tunnel()
1274 return -1; in cros_ec_i2c_tunnel()
1279 memcpy(read_ptr, r->data, read_len); in cros_ec_i2c_tunnel()
1294 return -1; in cros_ec_check_feature()
1300 * Query the EC for specified mask indicating enabled events.
1301 * The EC maintains separate event masks for SMI, SCI and WAKE.
1330 int ret = -1; in cros_ec_handle_non_uhepi_cmd()
1361 log_debug("Chrome EC: UHEPI %s\n", in cros_ec_is_uhepi_supported()
1400 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask); in cros_ec_clear_events_b()
1417 * If our EC doesn't support the LIMIT_POWER parameter, assume that in cros_ec_read_limit_power()
1420 if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) { in cros_ec_read_limit_power()
1421 log_warning("PARAM_LIMIT_POWER not supported by EC\n"); in cros_ec_read_limit_power()
1422 return -ENOSYS; in cros_ec_read_limit_power()
1426 return -EINVAL; in cros_ec_read_limit_power()
1469 /* Set lid close event state in the EC SMI event mask */ in cros_ec_set_lid_shutdown_mask()
1479 printf("EC: %sabled lid close event\n", enable ? "en" : "dis"); in cros_ec_set_lid_shutdown_mask()