0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
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
0027 #define EC_MAILBOX_VERSION 0
0028
0029
0030 #define EC_MAILBOX_START_COMMAND 0xda
0031
0032
0033 #define EC_MAILBOX_PROTO_VERSION 3
0034
0035
0036 #define EC_MAILBOX_DATA_EXTRA 2
0037
0038
0039 #define EC_MAILBOX_TIMEOUT HZ
0040
0041
0042 #define EC_CMDR_DATA BIT(0)
0043 #define EC_CMDR_PENDING BIT(1)
0044 #define EC_CMDR_BUSY BIT(2)
0045 #define EC_CMDR_CMD BIT(3)
0046
0047
0048
0049
0050
0051
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
0069
0070
0071
0072
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
0088
0089
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
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
0108
0109
0110
0111
0112
0113
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
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
0129 outb(EC_MAILBOX_START_COMMAND, ec->io_command->start);
0130
0131
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
0138 if (wilco_ec_response_timed_out(ec)) {
0139 dev_dbg(ec->dev, "response timed out\n");
0140 return -ETIMEDOUT;
0141 }
0142
0143
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
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
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
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
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);