Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Mailbox interface for Wilco Embedded Controller
0004  *
0005  * Copyright 2018 Google LLC
0006  *
0007  * The Wilco EC is similar to a typical ChromeOS embedded controller.
0008  * It uses the same MEC based low-level communication and a similar
0009  * protocol, but with some important differences.  The EC firmware does
0010  * not support the same mailbox commands so it is not registered as a
0011  * cros_ec device type.
0012  *
0013  * Most messages follow a standard format, but there are some exceptions
0014  * and an interface is provided to do direct/raw transactions that do not
0015  * make assumptions about byte placement.
0016  */
0017 
0018 #include <linux/delay.h>
0019 #include <linux/device.h>
0020 #include <linux/io.h>
0021 #include <linux/platform_data/wilco-ec.h>
0022 #include <linux/platform_device.h>
0023 
0024 #include "../cros_ec_lpc_mec.h"
0025 
0026 /* Version of mailbox interface */
0027 #define EC_MAILBOX_VERSION      0
0028 
0029 /* Command to start mailbox transaction */
0030 #define EC_MAILBOX_START_COMMAND    0xda
0031 
0032 /* Version of EC protocol */
0033 #define EC_MAILBOX_PROTO_VERSION    3
0034 
0035 /* Number of header bytes to be counted as data bytes */
0036 #define EC_MAILBOX_DATA_EXTRA       2
0037 
0038 /* Maximum timeout */
0039 #define EC_MAILBOX_TIMEOUT      HZ
0040 
0041 /* EC response flags */
0042 #define EC_CMDR_DATA        BIT(0)  /* Data ready for host to read */
0043 #define EC_CMDR_PENDING     BIT(1)  /* Write pending to EC */
0044 #define EC_CMDR_BUSY        BIT(2)  /* EC is busy processing a command */
0045 #define EC_CMDR_CMD     BIT(3)  /* Last host write was a command */
0046 
0047 /**
0048  * wilco_ec_response_timed_out() - Wait for EC response.
0049  * @ec: EC device.
0050  *
0051  * Return: true if EC timed out, false if EC did not time out.
0052  */
0053 static bool wilco_ec_response_timed_out(struct wilco_ec_device *ec)
0054 {
0055     unsigned long timeout = jiffies + EC_MAILBOX_TIMEOUT;
0056 
0057     do {
0058         if (!(inb(ec->io_command->start) &
0059               (EC_CMDR_PENDING | EC_CMDR_BUSY)))
0060             return false;
0061         usleep_range(100, 200);
0062     } while (time_before(jiffies, timeout));
0063 
0064     return true;
0065 }
0066 
0067 /**
0068  * wilco_ec_checksum() - Compute 8-bit checksum over data range.
0069  * @data: Data to checksum.
0070  * @size: Number of bytes to checksum.
0071  *
0072  * Return: 8-bit checksum of provided data.
0073  */
0074 static u8 wilco_ec_checksum(const void *data, size_t size)
0075 {
0076     u8 *data_bytes = (u8 *)data;
0077     u8 checksum = 0;
0078     size_t i;
0079 
0080     for (i = 0; i < size; i++)
0081         checksum += data_bytes[i];
0082 
0083     return checksum;
0084 }
0085 
0086 /**
0087  * wilco_ec_prepare() - Prepare the request structure for the EC.
0088  * @msg: EC message with request information.
0089  * @rq: EC request structure to fill.
0090  */
0091 static void wilco_ec_prepare(struct wilco_ec_message *msg,
0092                  struct wilco_ec_request *rq)
0093 {
0094     memset(rq, 0, sizeof(*rq));
0095     rq->struct_version = EC_MAILBOX_PROTO_VERSION;
0096     rq->mailbox_id = msg->type;
0097     rq->mailbox_version = EC_MAILBOX_VERSION;
0098     rq->data_size = msg->request_size;
0099 
0100     /* Checksum header and data */
0101     rq->checksum = wilco_ec_checksum(rq, sizeof(*rq));
0102     rq->checksum += wilco_ec_checksum(msg->request_data, msg->request_size);
0103     rq->checksum = -rq->checksum;
0104 }
0105 
0106 /**
0107  * wilco_ec_transfer() - Perform actual data transfer.
0108  * @ec: EC device.
0109  * @msg: EC message data for request and response.
0110  * @rq: Filled in request structure
0111  *
0112  * Context: ec->mailbox_lock should be held while using this function.
0113  * Return: number of bytes received or negative error code on failure.
0114  */
0115 static int wilco_ec_transfer(struct wilco_ec_device *ec,
0116                  struct wilco_ec_message *msg,
0117                  struct wilco_ec_request *rq)
0118 {
0119     struct wilco_ec_response *rs;
0120     u8 checksum;
0121     u8 flag;
0122 
0123     /* Write request header, then data */
0124     cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, 0, sizeof(*rq), (u8 *)rq);
0125     cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, sizeof(*rq), msg->request_size,
0126                  msg->request_data);
0127 
0128     /* Start the command */
0129     outb(EC_MAILBOX_START_COMMAND, ec->io_command->start);
0130 
0131     /* For some commands (eg shutdown) the EC will not respond, that's OK */
0132     if (msg->flags & WILCO_EC_FLAG_NO_RESPONSE) {
0133         dev_dbg(ec->dev, "EC does not respond to this command\n");
0134         return 0;
0135     }
0136 
0137     /* Wait for it to complete */
0138     if (wilco_ec_response_timed_out(ec)) {
0139         dev_dbg(ec->dev, "response timed out\n");
0140         return -ETIMEDOUT;
0141     }
0142 
0143     /* Check result */
0144     flag = inb(ec->io_data->start);
0145     if (flag) {
0146         dev_dbg(ec->dev, "bad response: 0x%02x\n", flag);
0147         return -EIO;
0148     }
0149 
0150     /* Read back response */
0151     rs = ec->data_buffer;
0152     checksum = cros_ec_lpc_io_bytes_mec(MEC_IO_READ, 0,
0153                         sizeof(*rs) + EC_MAILBOX_DATA_SIZE,
0154                         (u8 *)rs);
0155     if (checksum) {
0156         dev_dbg(ec->dev, "bad packet checksum 0x%02x\n", rs->checksum);
0157         return -EBADMSG;
0158     }
0159 
0160     if (rs->result) {
0161         dev_dbg(ec->dev, "EC reported failure: 0x%02x\n", rs->result);
0162         return -EBADMSG;
0163     }
0164 
0165     if (rs->data_size != EC_MAILBOX_DATA_SIZE) {
0166         dev_dbg(ec->dev, "unexpected packet size (%u != %u)\n",
0167             rs->data_size, EC_MAILBOX_DATA_SIZE);
0168         return -EMSGSIZE;
0169     }
0170 
0171     if (rs->data_size < msg->response_size) {
0172         dev_dbg(ec->dev, "EC didn't return enough data (%u < %zu)\n",
0173             rs->data_size, msg->response_size);
0174         return -EMSGSIZE;
0175     }
0176 
0177     memcpy(msg->response_data, rs->data, msg->response_size);
0178 
0179     return rs->data_size;
0180 }
0181 
0182 /**
0183  * wilco_ec_mailbox() - Send EC request and receive EC response.
0184  * @ec: EC device.
0185  * @msg: EC message data for request and response.
0186  *
0187  * On entry msg->type, msg->request_size, and msg->request_data should all be
0188  * filled in. If desired, msg->flags can be set.
0189  *
0190  * If a response is expected, msg->response_size should be set, and
0191  * msg->response_data should point to a buffer with enough space. On exit
0192  * msg->response_data will be filled.
0193  *
0194  * Return: number of bytes received or negative error code on failure.
0195  */
0196 int wilco_ec_mailbox(struct wilco_ec_device *ec, struct wilco_ec_message *msg)
0197 {
0198     struct wilco_ec_request *rq;
0199     int ret;
0200 
0201     dev_dbg(ec->dev, "type=%04x flags=%02x rslen=%zu rqlen=%zu\n",
0202         msg->type, msg->flags, msg->response_size, msg->request_size);
0203 
0204     mutex_lock(&ec->mailbox_lock);
0205     /* Prepare request packet */
0206     rq = ec->data_buffer;
0207     wilco_ec_prepare(msg, rq);
0208 
0209     ret = wilco_ec_transfer(ec, msg, rq);
0210     mutex_unlock(&ec->mailbox_lock);
0211 
0212     return ret;
0213 
0214 }
0215 EXPORT_SYMBOL_GPL(wilco_ec_mailbox);