1 // SPDX-License-Identifier: GPL-2.0
2 // ChromeOS EC communication protocol helper functions
4 // Copyright (C) 2015 Google, Inc
6 #include <linux/delay.h>
7 #include <linux/device.h>
8 #include <linux/module.h>
9 #include <linux/platform_data/cros_ec_commands.h>
10 #include <linux/platform_data/cros_ec_proto.h>
11 #include <linux/slab.h>
12 #include <asm/unaligned.h>
14 #include "cros_ec_trace.h"
16 #define EC_COMMAND_RETRIES 50
18 static int prepare_packet(struct cros_ec_device *ec_dev,
19 struct cros_ec_command *msg)
21 struct ec_host_request *request;
26 BUG_ON(ec_dev->proto_version != EC_HOST_REQUEST_VERSION);
27 BUG_ON(msg->outsize + sizeof(*request) > ec_dev->dout_size);
30 request = (struct ec_host_request *)out;
31 request->struct_version = EC_HOST_REQUEST_VERSION;
32 request->checksum = 0;
33 request->command = msg->command;
34 request->command_version = msg->version;
35 request->reserved = 0;
36 request->data_len = msg->outsize;
38 for (i = 0; i < sizeof(*request); i++)
41 /* Copy data and update checksum */
42 memcpy(out + sizeof(*request), msg->data, msg->outsize);
43 for (i = 0; i < msg->outsize; i++)
46 request->checksum = -csum;
48 return sizeof(*request) + msg->outsize;
51 static int send_command(struct cros_ec_device *ec_dev,
52 struct cros_ec_command *msg)
55 int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
57 if (ec_dev->proto_version > 2)
58 xfer_fxn = ec_dev->pkt_xfer;
60 xfer_fxn = ec_dev->cmd_xfer;
64 * This error can happen if a communication error happened and
65 * the EC is trying to use protocol v2, on an underlying
66 * communication mechanism that does not support v2.
68 dev_err_once(ec_dev->dev,
69 "missing EC transfer API, cannot send command\n");
73 trace_cros_ec_request_start(msg);
74 ret = (*xfer_fxn)(ec_dev, msg);
75 trace_cros_ec_request_done(msg, ret);
76 if (msg->result == EC_RES_IN_PROGRESS) {
78 struct cros_ec_command *status_msg;
79 struct ec_response_get_comms_status *status;
81 status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
86 status_msg->version = 0;
87 status_msg->command = EC_CMD_GET_COMMS_STATUS;
88 status_msg->insize = sizeof(*status);
89 status_msg->outsize = 0;
92 * Query the EC's status until it's no longer busy or
93 * we encounter an error.
95 for (i = 0; i < EC_COMMAND_RETRIES; i++) {
96 usleep_range(10000, 11000);
98 trace_cros_ec_request_start(status_msg);
99 ret = (*xfer_fxn)(ec_dev, status_msg);
100 trace_cros_ec_request_done(status_msg, ret);
106 msg->result = status_msg->result;
107 if (status_msg->result != EC_RES_SUCCESS)
110 status = (struct ec_response_get_comms_status *)
112 if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
123 * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
124 * @ec_dev: Device to register.
125 * @msg: Message to write.
127 * This is intended to be used by all ChromeOS EC drivers, but at present
128 * only SPI uses it. Once LPC uses the same protocol it can start using it.
129 * I2C could use it now, with a refactor of the existing code.
131 * Return: 0 on success or negative error code.
133 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
134 struct cros_ec_command *msg)
140 if (ec_dev->proto_version > 2)
141 return prepare_packet(ec_dev, msg);
143 BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
145 out[0] = EC_CMD_VERSION0 + msg->version;
146 out[1] = msg->command;
147 out[2] = msg->outsize;
148 csum = out[0] + out[1] + out[2];
149 for (i = 0; i < msg->outsize; i++)
150 csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
151 out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
153 return EC_MSG_TX_PROTO_BYTES + msg->outsize;
155 EXPORT_SYMBOL(cros_ec_prepare_tx);
158 * cros_ec_check_result() - Check ec_msg->result.
159 * @ec_dev: EC device.
160 * @msg: Message to check.
162 * This is used by ChromeOS EC drivers to check the ec_msg->result for
163 * errors and to warn about them.
165 * Return: 0 on success or negative error code.
167 int cros_ec_check_result(struct cros_ec_device *ec_dev,
168 struct cros_ec_command *msg)
170 switch (msg->result) {
173 case EC_RES_IN_PROGRESS:
174 dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
178 dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
179 msg->command, msg->result);
183 EXPORT_SYMBOL(cros_ec_check_result);
186 * cros_ec_get_host_event_wake_mask
188 * Get the mask of host events that cause wake from suspend.
190 * @ec_dev: EC device to call
191 * @msg: message structure to use
192 * @mask: result when function returns >=0.
195 * the caller has ec_dev->lock mutex, or the caller knows there is
196 * no other command in progress.
198 static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev,
199 struct cros_ec_command *msg,
202 struct ec_response_host_event_mask *r;
205 msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
208 msg->insize = sizeof(*r);
210 ret = send_command(ec_dev, msg);
212 r = (struct ec_response_host_event_mask *)msg->data;
219 static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
221 struct cros_ec_command *msg)
224 * Try using v3+ to query for supported protocols. If this
225 * command fails, fall back to v2. Returns the highest protocol
226 * supported by the EC.
227 * Also sets the max request/response/passthru size.
231 if (!ec_dev->pkt_xfer)
232 return -EPROTONOSUPPORT;
234 memset(msg, 0, sizeof(*msg));
235 msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
236 msg->insize = sizeof(struct ec_response_get_protocol_info);
238 ret = send_command(ec_dev, msg);
242 "failed to check for EC[%d] protocol version: %d\n",
247 if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
249 else if (msg->result != EC_RES_SUCCESS)
255 static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
257 struct cros_ec_command *msg;
258 struct ec_params_hello *hello_params;
259 struct ec_response_hello *hello_response;
261 int len = max(sizeof(*hello_params), sizeof(*hello_response));
263 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
268 msg->command = EC_CMD_HELLO;
269 hello_params = (struct ec_params_hello *)msg->data;
270 msg->outsize = sizeof(*hello_params);
271 hello_response = (struct ec_response_hello *)msg->data;
272 msg->insize = sizeof(*hello_response);
274 hello_params->in_data = 0xa0b0c0d0;
276 ret = send_command(ec_dev, msg);
280 "EC failed to respond to v2 hello: %d\n",
283 } else if (msg->result != EC_RES_SUCCESS) {
285 "EC responded to v2 hello with error: %d\n",
289 } else if (hello_response->out_data != 0xa1b2c3d4) {
291 "EC responded to v2 hello with bad result: %u\n",
292 hello_response->out_data);
305 * cros_ec_get_host_command_version_mask
307 * Get the version mask of a given command.
309 * @ec_dev: EC device to call
310 * @msg: message structure to use
311 * @cmd: command to get the version of.
312 * @mask: result when function returns 0.
314 * @return 0 on success, error code otherwise
317 * the caller has ec_dev->lock mutex or the caller knows there is
318 * no other command in progress.
320 static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
323 struct ec_params_get_cmd_versions *pver;
324 struct ec_response_get_cmd_versions *rver;
325 struct cros_ec_command *msg;
328 msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
334 msg->command = EC_CMD_GET_CMD_VERSIONS;
335 msg->insize = sizeof(*rver);
336 msg->outsize = sizeof(*pver);
338 pver = (struct ec_params_get_cmd_versions *)msg->data;
341 ret = send_command(ec_dev, msg);
343 rver = (struct ec_response_get_cmd_versions *)msg->data;
344 *mask = rver->version_mask;
353 * cros_ec_query_all() - Query the protocol version supported by the
355 * @ec_dev: Device to register.
357 * Return: 0 on success or negative error code.
359 int cros_ec_query_all(struct cros_ec_device *ec_dev)
361 struct device *dev = ec_dev->dev;
362 struct cros_ec_command *proto_msg;
363 struct ec_response_get_protocol_info *proto_info;
367 proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
372 /* First try sending with proto v3. */
373 ec_dev->proto_version = 3;
374 ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
377 proto_info = (struct ec_response_get_protocol_info *)
379 ec_dev->max_request = proto_info->max_request_packet_size -
380 sizeof(struct ec_host_request);
381 ec_dev->max_response = proto_info->max_response_packet_size -
382 sizeof(struct ec_host_response);
383 ec_dev->proto_version =
384 min(EC_HOST_REQUEST_VERSION,
385 fls(proto_info->protocol_versions) - 1);
388 ec_dev->proto_version);
390 ec_dev->din_size = ec_dev->max_response +
391 sizeof(struct ec_host_response) +
392 EC_MAX_RESPONSE_OVERHEAD;
393 ec_dev->dout_size = ec_dev->max_request +
394 sizeof(struct ec_host_request) +
395 EC_MAX_REQUEST_OVERHEAD;
400 ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
403 dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
404 ec_dev->max_passthru = 0;
406 dev_dbg(ec_dev->dev, "found PD chip\n");
407 ec_dev->max_passthru =
408 proto_info->max_request_packet_size -
409 sizeof(struct ec_host_request);
412 /* Try querying with a v2 hello message. */
413 ec_dev->proto_version = 2;
414 ret = cros_ec_host_command_proto_query_v2(ec_dev);
417 /* V2 hello succeeded. */
418 dev_dbg(ec_dev->dev, "falling back to proto v2\n");
420 ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
421 ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
422 ec_dev->max_passthru = 0;
423 ec_dev->pkt_xfer = NULL;
424 ec_dev->din_size = EC_PROTO2_MSG_BYTES;
425 ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
428 * It's possible for a test to occur too early when
429 * the EC isn't listening. If this happens, we'll
430 * test later when the first command is run.
432 ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
433 dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
438 devm_kfree(dev, ec_dev->din);
439 devm_kfree(dev, ec_dev->dout);
441 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
447 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
449 devm_kfree(dev, ec_dev->din);
454 /* Probe if MKBP event is supported */
455 ret = cros_ec_get_host_command_version_mask(ec_dev,
456 EC_CMD_GET_NEXT_EVENT,
458 if (ret < 0 || ver_mask == 0)
459 ec_dev->mkbp_event_supported = 0;
461 ec_dev->mkbp_event_supported = fls(ver_mask);
463 dev_dbg(ec_dev->dev, "MKBP support version %u\n",
464 ec_dev->mkbp_event_supported - 1);
466 /* Probe if host sleep v1 is supported for S0ix failure detection. */
467 ret = cros_ec_get_host_command_version_mask(ec_dev,
468 EC_CMD_HOST_SLEEP_EVENT,
470 ec_dev->host_sleep_v1 = (ret >= 0 && (ver_mask & EC_VER_MASK(1)));
473 * Get host event wake mask, assume all events are wake events
476 ret = cros_ec_get_host_event_wake_mask(ec_dev, proto_msg,
477 &ec_dev->host_event_wake_mask);
479 ec_dev->host_event_wake_mask = U32_MAX;
487 EXPORT_SYMBOL(cros_ec_query_all);
490 * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
491 * @ec_dev: EC device.
492 * @msg: Message to write.
494 * Call this to send a command to the ChromeOS EC. This should be used
495 * instead of calling the EC's cmd_xfer() callback directly.
497 * Return: 0 on success or negative error code.
499 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
500 struct cros_ec_command *msg)
504 mutex_lock(&ec_dev->lock);
505 if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
506 ret = cros_ec_query_all(ec_dev);
509 "EC version unknown and query failed; aborting command\n");
510 mutex_unlock(&ec_dev->lock);
515 if (msg->insize > ec_dev->max_response) {
516 dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
517 msg->insize = ec_dev->max_response;
520 if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
521 if (msg->outsize > ec_dev->max_request) {
523 "request of size %u is too big (max: %u)\n",
525 ec_dev->max_request);
526 mutex_unlock(&ec_dev->lock);
530 if (msg->outsize > ec_dev->max_passthru) {
532 "passthru rq of size %u is too big (max: %u)\n",
534 ec_dev->max_passthru);
535 mutex_unlock(&ec_dev->lock);
539 ret = send_command(ec_dev, msg);
540 mutex_unlock(&ec_dev->lock);
544 EXPORT_SYMBOL(cros_ec_cmd_xfer);
547 * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
548 * @ec_dev: EC device.
549 * @msg: Message to write.
551 * This function is identical to cros_ec_cmd_xfer, except it returns success
552 * status only if both the command was transmitted successfully and the EC
553 * replied with success status. It's not necessary to check msg->result when
554 * using this function.
556 * Return: The number of bytes transferred on success or negative error code.
558 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
559 struct cros_ec_command *msg)
563 ret = cros_ec_cmd_xfer(ec_dev, msg);
565 dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
566 } else if (msg->result != EC_RES_SUCCESS) {
567 dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
573 EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
575 static int get_next_event_xfer(struct cros_ec_device *ec_dev,
576 struct cros_ec_command *msg,
577 struct ec_response_get_next_event_v1 *event,
578 int version, uint32_t size)
582 msg->version = version;
583 msg->command = EC_CMD_GET_NEXT_EVENT;
587 ret = cros_ec_cmd_xfer(ec_dev, msg);
589 ec_dev->event_size = ret - 1;
590 ec_dev->event_data = *event;
596 static int get_next_event(struct cros_ec_device *ec_dev)
599 struct cros_ec_command msg;
600 struct ec_response_get_next_event_v1 event;
602 struct cros_ec_command *msg = &buf.msg;
603 struct ec_response_get_next_event_v1 *event = &buf.event;
604 const int cmd_version = ec_dev->mkbp_event_supported - 1;
606 memset(msg, 0, sizeof(*msg));
607 if (ec_dev->suspended) {
608 dev_dbg(ec_dev->dev, "Device suspended.\n");
612 if (cmd_version == 0)
613 return get_next_event_xfer(ec_dev, msg, event, 0,
614 sizeof(struct ec_response_get_next_event));
616 return get_next_event_xfer(ec_dev, msg, event, cmd_version,
617 sizeof(struct ec_response_get_next_event_v1));
620 static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
622 u8 buffer[sizeof(struct cros_ec_command) +
623 sizeof(ec_dev->event_data.data)];
624 struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
627 msg->command = EC_CMD_MKBP_STATE;
628 msg->insize = sizeof(ec_dev->event_data.data);
631 ec_dev->event_size = cros_ec_cmd_xfer(ec_dev, msg);
632 ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
633 memcpy(&ec_dev->event_data.data, msg->data,
634 sizeof(ec_dev->event_data.data));
636 return ec_dev->event_size;
640 * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
641 * @ec_dev: Device to fetch event from.
642 * @wake_event: Pointer to a bool set to true upon return if the event might be
643 * treated as a wake event. Ignored if null.
644 * @has_more_events: Pointer to bool set to true if more than one event is
646 * Some EC will set this flag to indicate cros_ec_get_next_event()
647 * can be called multiple times in a row.
648 * It is an optimization to prevent issuing a EC command for
649 * nothing or wait for another interrupt from the EC to process
653 * Return: negative error code on errors; 0 for no data; or else number of
654 * bytes received (i.e., an event was retrieved successfully). Event types are
655 * written out to @ec_dev->event_data.event_type on success.
657 int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
659 bool *has_more_events)
666 * Default value for wake_event.
667 * Wake up on keyboard event, wake up for spurious interrupt or link
674 * Default value for has_more_events.
675 * EC will raise another interrupt if AP does not process all events
679 *has_more_events = false;
681 if (!ec_dev->mkbp_event_supported)
682 return get_keyboard_state_event(ec_dev);
684 ret = get_next_event(ec_dev);
689 *has_more_events = ec_dev->event_data.event_type &
690 EC_MKBP_HAS_MORE_EVENTS;
691 ec_dev->event_data.event_type &= EC_MKBP_EVENT_TYPE_MASK;
694 event_type = ec_dev->event_data.event_type;
695 host_event = cros_ec_get_host_event(ec_dev);
698 * Sensor events need to be parsed by the sensor sub-device.
699 * Defer them, and don't report the wakeup here.
701 if (event_type == EC_MKBP_EVENT_SENSOR_FIFO)
703 /* Masked host-events should not count as wake events. */
704 else if (host_event &&
705 !(host_event & ec_dev->host_event_wake_mask))
711 EXPORT_SYMBOL(cros_ec_get_next_event);
714 * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
715 * @ec_dev: Device to fetch event from.
717 * When MKBP is supported, when the EC raises an interrupt, we collect the
718 * events raised and call the functions in the ec notifier. This function
719 * is a helper to know which events are raised.
721 * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
723 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
727 BUG_ON(!ec_dev->mkbp_event_supported);
729 if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
732 if (ec_dev->event_size != sizeof(host_event)) {
733 dev_warn(ec_dev->dev, "Invalid host event size\n");
737 host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
741 EXPORT_SYMBOL(cros_ec_get_host_event);
744 * cros_ec_check_features() - Test for the presence of EC features
746 * @ec: EC device, does not have to be connected directly to the AP,
747 * can be daisy chained through another device.
748 * @feature: One of ec_feature_code bit.
750 * Call this function to test whether the ChromeOS EC supports a feature.
752 * Return: 1 if supported, 0 if not
754 int cros_ec_check_features(struct cros_ec_dev *ec, int feature)
756 struct cros_ec_command *msg;
759 if (ec->features[0] == -1U && ec->features[1] == -1U) {
760 /* features bitmap not read yet */
761 msg = kzalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL);
765 msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset;
766 msg->insize = sizeof(ec->features);
768 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
770 dev_warn(ec->dev, "cannot get EC features: %d/%d\n",
772 memset(ec->features, 0, sizeof(ec->features));
774 memcpy(ec->features, msg->data, sizeof(ec->features));
777 dev_dbg(ec->dev, "EC features %08x %08x\n",
778 ec->features[0], ec->features[1]);
783 return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature);
785 EXPORT_SYMBOL_GPL(cros_ec_check_features);
788 * cros_ec_get_sensor_count() - Return the number of MEMS sensors supported.
790 * @ec: EC device, does not have to be connected directly to the AP,
791 * can be daisy chained through another device.
792 * Return: < 0 in case of error.
794 int cros_ec_get_sensor_count(struct cros_ec_dev *ec)
797 * Issue a command to get the number of sensor reported.
798 * If not supported, check for legacy mode.
800 int ret, sensor_count;
801 struct ec_params_motion_sense *params;
802 struct ec_response_motion_sense *resp;
803 struct cros_ec_command *msg;
804 struct cros_ec_device *ec_dev = ec->ec_dev;
807 msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*resp)),
813 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
814 msg->outsize = sizeof(*params);
815 msg->insize = sizeof(*resp);
817 params = (struct ec_params_motion_sense *)msg->data;
818 params->cmd = MOTIONSENSE_CMD_DUMP;
820 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
823 } else if (msg->result != EC_RES_SUCCESS) {
824 sensor_count = -EPROTO;
826 resp = (struct ec_response_motion_sense *)msg->data;
827 sensor_count = resp->dump.sensor_count;
832 * Check legacy mode: Let's find out if sensors are accessible
835 if (sensor_count == -EPROTO &&
836 ec->cmd_offset == 0 &&
837 ec_dev->cmd_readmem) {
838 ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS,
841 (status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
843 * We have 2 sensors, one in the lid, one in the base.
848 * EC uses LPC interface and no sensors are presented.
852 } else if (sensor_count == -EPROTO) {
853 /* EC responded, but does not understand DUMP command. */
858 EXPORT_SYMBOL_GPL(cros_ec_get_sensor_count);