Debian Codegraph @

src/ec/google/wilco/mailbox.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <arch/io.h>
4 #include <console/console.h>
5 #include <delay.h>
6 #include <ec/google/common/mec.h>
7 #include <string.h>
8 #include <timer.h>
9 #include <types.h>
10
11 #include "ec.h"
12
13 /* Mailbox ID */
14 #define EC_MAILBOX_ID 0x00f0
15
16 /* Version of mailbox interface */
17 #define EC_MAILBOX_VERSION 0
18
19 /* Command to start mailbox transaction */
20 #define EC_MAILBOX_START_COMMAND 0xda
21
22 /* Version of EC protocol */
23 #define EC_MAILBOX_PROTO_VERSION 3
24
25 /* Max number of bytes in protocol data payload */
26 #define EC_MAILBOX_DATA_SIZE 32
27
28 /* Number of header bytes to be counted as data bytes */
29 #define EC_MAILBOX_DATA_EXTRA 2
30
31 /* Maximum timeout */
32 #define EC_MAILBOX_TIMEOUT_MS MSECS_PER_SEC
33
34 /* EC response flags */
35 #define EC_CMDR_DATA BIT(0) /* Data ready for host to read */
36 #define EC_CMDR_PENDING BIT(1) /* Write pending to EC */
37 #define EC_CMDR_BUSY BIT(2) /* EC is busy processing a command */
38 #define EC_CMDR_CMD BIT(3) /* Last host write was a command */
39
40 /* Request to EC */
41 struct wilco_ec_request {
42 uint8_t struct_version; /* version (=3) */
43 uint8_t checksum; /* sum of all bytes must be 0 */
44 uint16_t mailbox_id; /* mailbox identifier */
45 uint8_t mailbox_version; /* mailbox version (=0) */
46 uint8_t reserved1; /* unused (=0) */
47 uint16_t data_size; /* length (data + 2 bytes of header) */
48 uint8_t command; /* mailbox command */
49 uint8_t reserved2; /* unused (=0) */
50 } __packed;
51
52 /* Response from EC */
53 struct wilco_ec_response {
54 uint8_t struct_version; /* version (=3) */
55 uint8_t checksum; /* sum of all bytes must be 0 */
56 uint16_t result; /* result code */
57 uint16_t data_size; /* length of data buffer (always 32) */
58 uint8_t reserved[3]; /* unused (=0) */
59 uint8_t data[EC_MAILBOX_DATA_SIZE];
60 } __packed;
61
62 struct wilco_ec_message {
63 uint8_t command; /* mailbox command code */
64 uint8_t result; /* request result */
65 size_t request_size; /* bytes to send to the EC */
66 size_t response_size; /* bytes expected from the EC */
67 enum wilco_ec_msg_type type; /* message type */
68 /*
69 * This data buffer will contain the request data when passed to
70 * wilco_ec_message() and will contain the response data on return.
71 */
72 uint8_t data[EC_MAILBOX_DATA_SIZE];
73 };
74
75 static bool wilco_ec_response_timed_out(void)
76 {
77 uint8_t mask = EC_CMDR_PENDING | EC_CMDR_BUSY;
78 struct stopwatch sw;
79
80 stopwatch_init_msecs_expire(&sw, EC_MAILBOX_TIMEOUT_MS);
81
82 while (inb(CONFIG_EC_BASE_HOST_COMMAND) & mask) {
83 if (stopwatch_expired(&sw)) {
84 printk(BIOS_ERR, "%s: Command timeout\n", __func__);
85 return true; /* Timed out */
86 }
87 mdelay(1);
88 }
89
90 return false; /* Did not time out */
91 }
92
93 static uint8_t wilco_ec_checksum(void *data, size_t size)
94 {
95 uint8_t *data_bytes = (uint8_t *)data;
96 uint8_t checksum = 0;
97 size_t i;
98
99 for (i = 0; i < size; i++)
100 checksum += data_bytes[i];
101
102 return checksum;
103 }
104
105 static void wilco_ec_prepare(struct wilco_ec_message *msg,
106 struct wilco_ec_request *rq)
107 {
108 memset(rq, 0, sizeof(*rq));
109
110 /* Fill in request packet */
111 rq->struct_version = EC_MAILBOX_PROTO_VERSION;
112 rq->mailbox_id = EC_MAILBOX_ID;
113 rq->mailbox_version = EC_MAILBOX_VERSION;
114 rq->data_size = msg->request_size + EC_MAILBOX_DATA_EXTRA;
115 rq->command = msg->command;
116
117 /* Checksum header and data */
118 rq->checksum = wilco_ec_checksum(rq, sizeof(*rq));
119 rq->checksum += wilco_ec_checksum(msg->data, msg->request_size);
120 rq->checksum = -rq->checksum;
121 }
122
123 static int wilco_ec_transfer(struct wilco_ec_message *msg)
124 {
125 struct wilco_ec_request rq;
126 struct wilco_ec_response rs;
127 uint8_t checksum;
128 size_t skip_size;
129
130 /* Prepare request packet */
131 wilco_ec_prepare(msg, &rq);
132
133 /* Write request header */
134 mec_io_bytes(MEC_IO_WRITE, CONFIG_EC_BASE_PACKET, 0, &rq, sizeof(rq));
135
136 /* Write request data */
137 mec_io_bytes(MEC_IO_WRITE, CONFIG_EC_BASE_PACKET, sizeof(rq),
138 msg->data, msg->request_size);
139
140 /* Start the command */
141 outb(EC_MAILBOX_START_COMMAND, CONFIG_EC_BASE_HOST_COMMAND);
142
143 /* Some commands will put the EC into a state where it cannot respond */
144 if (msg->type == WILCO_EC_MSG_NO_RESPONSE) {
145 printk(BIOS_DEBUG, "%s: EC does not respond to this command\n",
146 __func__);
147 return 0;
148 }
149
150 /* Wait for it to complete */
151 if (wilco_ec_response_timed_out()) {
152 printk(BIOS_ERR, "%s: response timed out\n", __func__);
153 return -1;
154 }
155
156 /* Check result */
157 msg->result = inb(CONFIG_EC_BASE_HOST_DATA);
158 if (msg->result != 0) {
159 printk(BIOS_ERR, "%s: bad response: 0x%02x\n",
160 __func__, msg->result);
161 return -1;
162 }
163
164 /* Read back response */
165 checksum = mec_io_bytes(MEC_IO_READ, CONFIG_EC_BASE_PACKET, 0,
166 &rs, sizeof(rs));
167 if (checksum) {
168 printk(BIOS_ERR, "%s: bad checksum %02x\n", __func__, checksum);
169 return -1;
170 }
171 msg->result = rs.result;
172
173 /* EC always returns EC_MAILBOX_DATA_SIZE bytes */
174 if (rs.data_size > EC_MAILBOX_DATA_SIZE) {
175 printk(BIOS_ERR, "%s: packet too long (%d bytes, expected %d)",
176 __func__, rs.data_size, EC_MAILBOX_DATA_SIZE);
177 return -1;
178 }
179
180 /* Skip response data bytes as requested */
181 skip_size = (msg->type == WILCO_EC_MSG_DEFAULT) ? 1 : 0;
182
183 if (msg->response_size > rs.data_size - skip_size) {
184 printk(BIOS_ERR, "%s: data too short (%zu bytes, expected %zu)",
185 __func__, rs.data_size - skip_size, msg->response_size);
186 return -1;
187 }
188
189 memcpy(msg->data, rs.data + skip_size, msg->response_size);
190
191 /* Return actual amount of data received */
192 return msg->response_size;
193 }
194
195 int wilco_ec_mailbox(enum wilco_ec_msg_type type, uint8_t command,
196 const void *request_data, size_t request_size,
197 void *response_data, size_t response_size)
198 {
199 struct wilco_ec_message msg = {
200 .command = command,
201 .request_size = request_size,
202 .response_size = response_size,
203 .type = type,
204 };
205 int ret;
206
207 if (request_size > EC_MAILBOX_DATA_SIZE) {
208 printk(BIOS_ERR, "%s: provided request data too large: %zu\n",
209 __func__, request_size);
210 return -1;
211 }
212 if (response_size > EC_MAILBOX_DATA_SIZE) {
213 printk(BIOS_ERR, "%s: expected response data too large: %zu\n",
214 __func__, response_size);
215 return -1;
216 }
217 if (request_size && !request_data) {
218 printk(BIOS_ERR, "%s: request data missing\n", __func__);
219 return -1;
220 }
221 if (response_size && !response_data) {
222 printk(BIOS_ERR, "%s: request data missing\n", __func__);
223 return -1;
224 }
225
226 /* Copy request data if present */
227 if (request_size)
228 memcpy(msg.data, request_data, request_size);
229
230 /* Do the EC transfer */
231 ret = wilco_ec_transfer(&msg);
232
233 /* Copy response data if present */
234 if (ret > 0 && response_size)
235 memcpy(response_data, msg.data, response_size);
236
237 /* Return error if message result is non-zero */
238 if (ret >= 0 && msg.result)
239 ret = -1;
240
241 return ret;
242 }