This source file includes following definitions.
- wilco_ec_response_timed_out
- wilco_ec_checksum
- wilco_ec_prepare
- wilco_ec_transfer
- wilco_ec_mailbox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <linux/delay.h>
19 #include <linux/device.h>
20 #include <linux/io.h>
21 #include <linux/platform_data/wilco-ec.h>
22 #include <linux/platform_device.h>
23
24 #include "../cros_ec_lpc_mec.h"
25
26
27 #define EC_MAILBOX_VERSION 0
28
29
30 #define EC_MAILBOX_START_COMMAND 0xda
31
32
33 #define EC_MAILBOX_PROTO_VERSION 3
34
35
36 #define EC_MAILBOX_DATA_EXTRA 2
37
38
39 #define EC_MAILBOX_TIMEOUT HZ
40
41
42 #define EC_CMDR_DATA BIT(0)
43 #define EC_CMDR_PENDING BIT(1)
44 #define EC_CMDR_BUSY BIT(2)
45 #define EC_CMDR_CMD BIT(3)
46
47
48
49
50
51
52
53 static bool wilco_ec_response_timed_out(struct wilco_ec_device *ec)
54 {
55 unsigned long timeout = jiffies + EC_MAILBOX_TIMEOUT;
56
57 do {
58 if (!(inb(ec->io_command->start) &
59 (EC_CMDR_PENDING | EC_CMDR_BUSY)))
60 return false;
61 usleep_range(100, 200);
62 } while (time_before(jiffies, timeout));
63
64 return true;
65 }
66
67
68
69
70
71
72
73
74 static u8 wilco_ec_checksum(const void *data, size_t size)
75 {
76 u8 *data_bytes = (u8 *)data;
77 u8 checksum = 0;
78 size_t i;
79
80 for (i = 0; i < size; i++)
81 checksum += data_bytes[i];
82
83 return checksum;
84 }
85
86
87
88
89
90
91 static void wilco_ec_prepare(struct wilco_ec_message *msg,
92 struct wilco_ec_request *rq)
93 {
94 memset(rq, 0, sizeof(*rq));
95 rq->struct_version = EC_MAILBOX_PROTO_VERSION;
96 rq->mailbox_id = msg->type;
97 rq->mailbox_version = EC_MAILBOX_VERSION;
98 rq->data_size = msg->request_size;
99
100
101 rq->checksum = wilco_ec_checksum(rq, sizeof(*rq));
102 rq->checksum += wilco_ec_checksum(msg->request_data, msg->request_size);
103 rq->checksum = -rq->checksum;
104 }
105
106
107
108
109
110
111
112
113
114
115 static int wilco_ec_transfer(struct wilco_ec_device *ec,
116 struct wilco_ec_message *msg,
117 struct wilco_ec_request *rq)
118 {
119 struct wilco_ec_response *rs;
120 u8 checksum;
121 u8 flag;
122
123
124 cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, 0, sizeof(*rq), (u8 *)rq);
125 cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, sizeof(*rq), msg->request_size,
126 msg->request_data);
127
128
129 outb(EC_MAILBOX_START_COMMAND, ec->io_command->start);
130
131
132 if (msg->flags & WILCO_EC_FLAG_NO_RESPONSE) {
133 dev_dbg(ec->dev, "EC does not respond to this command\n");
134 return 0;
135 }
136
137
138 if (wilco_ec_response_timed_out(ec)) {
139 dev_dbg(ec->dev, "response timed out\n");
140 return -ETIMEDOUT;
141 }
142
143
144 flag = inb(ec->io_data->start);
145 if (flag) {
146 dev_dbg(ec->dev, "bad response: 0x%02x\n", flag);
147 return -EIO;
148 }
149
150
151 rs = ec->data_buffer;
152 checksum = cros_ec_lpc_io_bytes_mec(MEC_IO_READ, 0,
153 sizeof(*rs) + EC_MAILBOX_DATA_SIZE,
154 (u8 *)rs);
155 if (checksum) {
156 dev_dbg(ec->dev, "bad packet checksum 0x%02x\n", rs->checksum);
157 return -EBADMSG;
158 }
159
160 if (rs->result) {
161 dev_dbg(ec->dev, "EC reported failure: 0x%02x\n", rs->result);
162 return -EBADMSG;
163 }
164
165 if (rs->data_size != EC_MAILBOX_DATA_SIZE) {
166 dev_dbg(ec->dev, "unexpected packet size (%u != %u)",
167 rs->data_size, EC_MAILBOX_DATA_SIZE);
168 return -EMSGSIZE;
169 }
170
171 if (rs->data_size < msg->response_size) {
172 dev_dbg(ec->dev, "EC didn't return enough data (%u < %zu)",
173 rs->data_size, msg->response_size);
174 return -EMSGSIZE;
175 }
176
177 memcpy(msg->response_data, rs->data, msg->response_size);
178
179 return rs->data_size;
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 int wilco_ec_mailbox(struct wilco_ec_device *ec, struct wilco_ec_message *msg)
197 {
198 struct wilco_ec_request *rq;
199 int ret;
200
201 dev_dbg(ec->dev, "type=%04x flags=%02x rslen=%zu rqlen=%zu\n",
202 msg->type, msg->flags, msg->response_size, msg->request_size);
203
204 mutex_lock(&ec->mailbox_lock);
205
206 rq = ec->data_buffer;
207 wilco_ec_prepare(msg, rq);
208
209 ret = wilco_ec_transfer(ec, msg, rq);
210 mutex_unlock(&ec->mailbox_lock);
211
212 return ret;
213
214 }
215 EXPORT_SYMBOL_GPL(wilco_ec_mailbox);