]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
88364387 HT |
2 | /* |
3 | * Chromium OS cros_ec driver | |
4 | * | |
5 | * Copyright (c) 2012 The Chromium OS Authors. | |
88364387 HT |
6 | */ |
7 | ||
8 | /* | |
836bb6e8 SG |
9 | * This is the interface to the Chrome OS EC. It provides keyboard functions, |
10 | * power control and battery management. Quite a few other functions are | |
11 | * provided to enable the EC software to be updated, talk to the EC's I2C bus | |
12 | * and store a small amount of data in a memory which persists while the EC | |
13 | * is not reset. | |
88364387 HT |
14 | */ |
15 | ||
ac806523 SG |
16 | #define LOG_CATEGORY UCLASS_CROS_EC |
17 | ||
88364387 HT |
18 | #include <common.h> |
19 | #include <command.h> | |
84d6cbd3 | 20 | #include <dm.h> |
b79fdc76 | 21 | #include <flash.h> |
88364387 HT |
22 | #include <i2c.h> |
23 | #include <cros_ec.h> | |
24 | #include <fdtdec.h> | |
f7ae49fc | 25 | #include <log.h> |
88364387 HT |
26 | #include <malloc.h> |
27 | #include <spi.h> | |
1221ce45 | 28 | #include <linux/errno.h> |
88364387 HT |
29 | #include <asm/io.h> |
30 | #include <asm-generic/gpio.h> | |
84d6cbd3 | 31 | #include <dm/device-internal.h> |
2ec9d171 | 32 | #include <dm/of_extra.h> |
84d6cbd3 | 33 | #include <dm/uclass-internal.h> |
88364387 HT |
34 | |
35 | #ifdef DEBUG_TRACE | |
36 | #define debug_trace(fmt, b...) debug(fmt, #b) | |
37 | #else | |
38 | #define debug_trace(fmt, b...) | |
39 | #endif | |
40 | ||
41 | enum { | |
42 | /* Timeout waiting for a flash erase command to complete */ | |
43 | CROS_EC_CMD_TIMEOUT_MS = 5000, | |
44 | /* Timeout waiting for a synchronous hash to be recomputed */ | |
45 | CROS_EC_CMD_HASH_TIMEOUT_MS = 2000, | |
46 | }; | |
47 | ||
72ef8bfd SG |
48 | #define INVALID_HCMD 0xFF |
49 | ||
50 | /* | |
51 | * Map UHEPI masks to non UHEPI commands in order to support old EC FW | |
52 | * which does not support UHEPI command. | |
53 | */ | |
54 | static const struct { | |
55 | u8 set_cmd; | |
56 | u8 clear_cmd; | |
57 | u8 get_cmd; | |
58 | } event_map[] = { | |
59 | [EC_HOST_EVENT_MAIN] = { | |
60 | INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR, | |
61 | INVALID_HCMD, | |
62 | }, | |
63 | [EC_HOST_EVENT_B] = { | |
64 | INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B, | |
65 | EC_CMD_HOST_EVENT_GET_B, | |
66 | }, | |
67 | [EC_HOST_EVENT_SCI_MASK] = { | |
68 | EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD, | |
69 | EC_CMD_HOST_EVENT_GET_SCI_MASK, | |
70 | }, | |
71 | [EC_HOST_EVENT_SMI_MASK] = { | |
72 | EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD, | |
73 | EC_CMD_HOST_EVENT_GET_SMI_MASK, | |
74 | }, | |
75 | [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = { | |
76 | INVALID_HCMD, INVALID_HCMD, INVALID_HCMD, | |
77 | }, | |
78 | [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = { | |
79 | EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD, | |
80 | EC_CMD_HOST_EVENT_GET_WAKE_MASK, | |
81 | }, | |
82 | [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = { | |
83 | EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD, | |
84 | EC_CMD_HOST_EVENT_GET_WAKE_MASK, | |
85 | }, | |
86 | [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = { | |
87 | EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD, | |
88 | EC_CMD_HOST_EVENT_GET_WAKE_MASK, | |
89 | }, | |
90 | [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = { | |
91 | EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD, | |
92 | EC_CMD_HOST_EVENT_GET_WAKE_MASK, | |
93 | }, | |
94 | }; | |
95 | ||
88364387 HT |
96 | void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len) |
97 | { | |
98 | #ifdef DEBUG | |
99 | int i; | |
100 | ||
101 | printf("%s: ", name); | |
102 | if (cmd != -1) | |
103 | printf("cmd=%#x: ", cmd); | |
104 | for (i = 0; i < len; i++) | |
105 | printf("%02x ", data[i]); | |
106 | printf("\n"); | |
107 | #endif | |
108 | } | |
109 | ||
110 | /* | |
111 | * Calculate a simple 8-bit checksum of a data block | |
112 | * | |
113 | * @param data Data block to checksum | |
114 | * @param size Size of data block in bytes | |
115 | * @return checksum value (0 to 255) | |
116 | */ | |
117 | int cros_ec_calc_checksum(const uint8_t *data, int size) | |
118 | { | |
119 | int csum, i; | |
120 | ||
121 | for (i = csum = 0; i < size; i++) | |
122 | csum += data[i]; | |
123 | return csum & 0xff; | |
124 | } | |
125 | ||
2d8ede58 SG |
126 | /** |
127 | * Create a request packet for protocol version 3. | |
128 | * | |
129 | * The packet is stored in the device's internal output buffer. | |
130 | * | |
131 | * @param dev CROS-EC device | |
132 | * @param cmd Command to send (EC_CMD_...) | |
133 | * @param cmd_version Version of command to send (EC_VER_...) | |
134 | * @param dout Output data (may be NULL If dout_len=0) | |
135 | * @param dout_len Size of output data in bytes | |
136 | * @return packet size in bytes, or <0 if error. | |
137 | */ | |
6322a7b6 | 138 | static int create_proto3_request(struct cros_ec_dev *cdev, |
2d8ede58 SG |
139 | int cmd, int cmd_version, |
140 | const void *dout, int dout_len) | |
141 | { | |
6322a7b6 | 142 | struct ec_host_request *rq = (struct ec_host_request *)cdev->dout; |
2d8ede58 SG |
143 | int out_bytes = dout_len + sizeof(*rq); |
144 | ||
145 | /* Fail if output size is too big */ | |
6322a7b6 | 146 | if (out_bytes > (int)sizeof(cdev->dout)) { |
2d8ede58 SG |
147 | debug("%s: Cannot send %d bytes\n", __func__, dout_len); |
148 | return -EC_RES_REQUEST_TRUNCATED; | |
149 | } | |
150 | ||
151 | /* Fill in request packet */ | |
152 | rq->struct_version = EC_HOST_REQUEST_VERSION; | |
153 | rq->checksum = 0; | |
154 | rq->command = cmd; | |
155 | rq->command_version = cmd_version; | |
156 | rq->reserved = 0; | |
157 | rq->data_len = dout_len; | |
158 | ||
159 | /* Copy data after header */ | |
160 | memcpy(rq + 1, dout, dout_len); | |
161 | ||
162 | /* Write checksum field so the entire packet sums to 0 */ | |
6322a7b6 | 163 | rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes)); |
2d8ede58 | 164 | |
6322a7b6 | 165 | cros_ec_dump_data("out", cmd, cdev->dout, out_bytes); |
2d8ede58 SG |
166 | |
167 | /* Return size of request packet */ | |
168 | return out_bytes; | |
169 | } | |
170 | ||
171 | /** | |
172 | * Prepare the device to receive a protocol version 3 response. | |
173 | * | |
174 | * @param dev CROS-EC device | |
175 | * @param din_len Maximum size of response in bytes | |
176 | * @return maximum expected number of bytes in response, or <0 if error. | |
177 | */ | |
6322a7b6 | 178 | static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len) |
2d8ede58 SG |
179 | { |
180 | int in_bytes = din_len + sizeof(struct ec_host_response); | |
181 | ||
182 | /* Fail if input size is too big */ | |
6322a7b6 | 183 | if (in_bytes > (int)sizeof(cdev->din)) { |
2d8ede58 SG |
184 | debug("%s: Cannot receive %d bytes\n", __func__, din_len); |
185 | return -EC_RES_RESPONSE_TOO_BIG; | |
186 | } | |
187 | ||
188 | /* Return expected size of response packet */ | |
189 | return in_bytes; | |
190 | } | |
191 | ||
192 | /** | |
193 | * Handle a protocol version 3 response packet. | |
194 | * | |
195 | * The packet must already be stored in the device's internal input buffer. | |
196 | * | |
197 | * @param dev CROS-EC device | |
198 | * @param dinp Returns pointer to response data | |
199 | * @param din_len Maximum size of response in bytes | |
8bbb38b1 SG |
200 | * @return number of bytes of response data, or <0 if error. Note that error |
201 | * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they | |
202 | * overlap!) | |
2d8ede58 SG |
203 | */ |
204 | static int handle_proto3_response(struct cros_ec_dev *dev, | |
205 | uint8_t **dinp, int din_len) | |
206 | { | |
207 | struct ec_host_response *rs = (struct ec_host_response *)dev->din; | |
208 | int in_bytes; | |
209 | int csum; | |
210 | ||
211 | cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs)); | |
212 | ||
213 | /* Check input data */ | |
214 | if (rs->struct_version != EC_HOST_RESPONSE_VERSION) { | |
215 | debug("%s: EC response version mismatch\n", __func__); | |
216 | return -EC_RES_INVALID_RESPONSE; | |
217 | } | |
218 | ||
219 | if (rs->reserved) { | |
220 | debug("%s: EC response reserved != 0\n", __func__); | |
221 | return -EC_RES_INVALID_RESPONSE; | |
222 | } | |
223 | ||
224 | if (rs->data_len > din_len) { | |
225 | debug("%s: EC returned too much data\n", __func__); | |
226 | return -EC_RES_RESPONSE_TOO_BIG; | |
227 | } | |
228 | ||
229 | cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len); | |
230 | ||
231 | /* Update in_bytes to actual data size */ | |
232 | in_bytes = sizeof(*rs) + rs->data_len; | |
233 | ||
234 | /* Verify checksum */ | |
235 | csum = cros_ec_calc_checksum(dev->din, in_bytes); | |
236 | if (csum) { | |
237 | debug("%s: EC response checksum invalid: 0x%02x\n", __func__, | |
238 | csum); | |
239 | return -EC_RES_INVALID_CHECKSUM; | |
240 | } | |
241 | ||
242 | /* Return error result, if any */ | |
243 | if (rs->result) | |
244 | return -(int)rs->result; | |
245 | ||
246 | /* If we're still here, set response data pointer and return length */ | |
247 | *dinp = (uint8_t *)(rs + 1); | |
248 | ||
249 | return rs->data_len; | |
250 | } | |
251 | ||
6322a7b6 | 252 | static int send_command_proto3(struct cros_ec_dev *cdev, |
2d8ede58 SG |
253 | int cmd, int cmd_version, |
254 | const void *dout, int dout_len, | |
255 | uint8_t **dinp, int din_len) | |
256 | { | |
84d6cbd3 | 257 | struct dm_cros_ec_ops *ops; |
2d8ede58 SG |
258 | int out_bytes, in_bytes; |
259 | int rv; | |
260 | ||
261 | /* Create request packet */ | |
6322a7b6 | 262 | out_bytes = create_proto3_request(cdev, cmd, cmd_version, |
2d8ede58 SG |
263 | dout, dout_len); |
264 | if (out_bytes < 0) | |
265 | return out_bytes; | |
266 | ||
267 | /* Prepare response buffer */ | |
6322a7b6 | 268 | in_bytes = prepare_proto3_response_buffer(cdev, din_len); |
2d8ede58 SG |
269 | if (in_bytes < 0) |
270 | return in_bytes; | |
271 | ||
6322a7b6 SG |
272 | ops = dm_cros_ec_get_ops(cdev->dev); |
273 | rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) : | |
274 | -ENOSYS; | |
2d8ede58 SG |
275 | if (rv < 0) |
276 | return rv; | |
277 | ||
278 | /* Process the response */ | |
6322a7b6 | 279 | return handle_proto3_response(cdev, dinp, din_len); |
2d8ede58 SG |
280 | } |
281 | ||
9fea76f5 | 282 | static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version, |
88364387 HT |
283 | const void *dout, int dout_len, |
284 | uint8_t **dinp, int din_len) | |
285 | { | |
84d6cbd3 | 286 | struct dm_cros_ec_ops *ops; |
2d8ede58 SG |
287 | int ret = -1; |
288 | ||
289 | /* Handle protocol version 3 support */ | |
290 | if (dev->protocol_version == 3) { | |
291 | return send_command_proto3(dev, cmd, cmd_version, | |
292 | dout, dout_len, dinp, din_len); | |
293 | } | |
88364387 | 294 | |
84d6cbd3 SG |
295 | ops = dm_cros_ec_get_ops(dev->dev); |
296 | ret = ops->command(dev->dev, cmd, cmd_version, | |
297 | (const uint8_t *)dout, dout_len, dinp, din_len); | |
88364387 HT |
298 | |
299 | return ret; | |
300 | } | |
301 | ||
302 | /** | |
303 | * Send a command to the CROS-EC device and return the reply. | |
304 | * | |
305 | * The device's internal input/output buffers are used. | |
306 | * | |
307 | * @param dev CROS-EC device | |
308 | * @param cmd Command to send (EC_CMD_...) | |
309 | * @param cmd_version Version of command to send (EC_VER_...) | |
310 | * @param dout Output data (may be NULL If dout_len=0) | |
311 | * @param dout_len Size of output data in bytes | |
312 | * @param dinp Response data (may be NULL If din_len=0). | |
313 | * If not NULL, it will be updated to point to the data | |
314 | * and will always be double word aligned (64-bits) | |
315 | * @param din_len Maximum size of response in bytes | |
8bbb38b1 | 316 | * @return number of bytes in response, or -ve on error |
88364387 | 317 | */ |
b4f98b3b | 318 | static int ec_command_inptr(struct udevice *dev, uint cmd, |
e6c5c94a SG |
319 | int cmd_version, const void *dout, int dout_len, |
320 | uint8_t **dinp, int din_len) | |
88364387 | 321 | { |
6322a7b6 | 322 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
2ab83f0d | 323 | uint8_t *din = NULL; |
88364387 HT |
324 | int len; |
325 | ||
6322a7b6 SG |
326 | len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din, |
327 | din_len); | |
88364387 HT |
328 | |
329 | /* If the command doesn't complete, wait a while */ | |
330 | if (len == -EC_RES_IN_PROGRESS) { | |
2ab83f0d | 331 | struct ec_response_get_comms_status *resp = NULL; |
88364387 HT |
332 | ulong start; |
333 | ||
334 | /* Wait for command to complete */ | |
335 | start = get_timer(0); | |
336 | do { | |
337 | int ret; | |
338 | ||
339 | mdelay(50); /* Insert some reasonable delay */ | |
6322a7b6 SG |
340 | ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0, |
341 | NULL, 0, | |
342 | (uint8_t **)&resp, sizeof(*resp)); | |
88364387 HT |
343 | if (ret < 0) |
344 | return ret; | |
345 | ||
346 | if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) { | |
347 | debug("%s: Command %#02x timeout\n", | |
348 | __func__, cmd); | |
349 | return -EC_RES_TIMEOUT; | |
350 | } | |
351 | } while (resp->flags & EC_COMMS_STATUS_PROCESSING); | |
352 | ||
353 | /* OK it completed, so read the status response */ | |
354 | /* not sure why it was 0 for the last argument */ | |
6322a7b6 SG |
355 | len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0, |
356 | &din, din_len); | |
88364387 HT |
357 | } |
358 | ||
e907bf2d | 359 | debug("%s: len=%d, din=%p\n", __func__, len, din); |
88364387 HT |
360 | if (dinp) { |
361 | /* If we have any data to return, it must be 64bit-aligned */ | |
362 | assert(len <= 0 || !((uintptr_t)din & 7)); | |
363 | *dinp = din; | |
364 | } | |
365 | ||
366 | return len; | |
367 | } | |
368 | ||
369 | /** | |
370 | * Send a command to the CROS-EC device and return the reply. | |
371 | * | |
372 | * The device's internal input/output buffers are used. | |
373 | * | |
374 | * @param dev CROS-EC device | |
375 | * @param cmd Command to send (EC_CMD_...) | |
376 | * @param cmd_version Version of command to send (EC_VER_...) | |
377 | * @param dout Output data (may be NULL If dout_len=0) | |
378 | * @param dout_len Size of output data in bytes | |
379 | * @param din Response data (may be NULL If din_len=0). | |
380 | * It not NULL, it is a place for ec_command() to copy the | |
381 | * data to. | |
382 | * @param din_len Maximum size of response in bytes | |
8bbb38b1 | 383 | * @return number of bytes in response, or -ve on error |
88364387 | 384 | */ |
9fea76f5 | 385 | static int ec_command(struct udevice *dev, uint cmd, int cmd_version, |
88364387 HT |
386 | const void *dout, int dout_len, |
387 | void *din, int din_len) | |
388 | { | |
389 | uint8_t *in_buffer; | |
390 | int len; | |
391 | ||
392 | assert((din_len == 0) || din); | |
393 | len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len, | |
6322a7b6 | 394 | &in_buffer, din_len); |
88364387 HT |
395 | if (len > 0) { |
396 | /* | |
397 | * If we were asked to put it somewhere, do so, otherwise just | |
398 | * disregard the result. | |
399 | */ | |
400 | if (din && in_buffer) { | |
401 | assert(len <= din_len); | |
402 | memmove(din, in_buffer, len); | |
403 | } | |
404 | } | |
405 | return len; | |
406 | } | |
407 | ||
745009c4 | 408 | int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan) |
88364387 | 409 | { |
6322a7b6 | 410 | if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan, |
2ab83f0d | 411 | sizeof(scan->data)) != sizeof(scan->data)) |
88364387 HT |
412 | return -1; |
413 | ||
414 | return 0; | |
415 | } | |
416 | ||
6322a7b6 | 417 | int cros_ec_read_id(struct udevice *dev, char *id, int maxlen) |
88364387 HT |
418 | { |
419 | struct ec_response_get_version *r; | |
ac806523 | 420 | int ret; |
88364387 | 421 | |
ac806523 SG |
422 | ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, |
423 | (uint8_t **)&r, sizeof(*r)); | |
424 | if (ret != sizeof(*r)) { | |
a749c09a | 425 | log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r)); |
88364387 | 426 | return -1; |
ac806523 | 427 | } |
88364387 | 428 | |
2ab83f0d | 429 | if (maxlen > (int)sizeof(r->version_string_ro)) |
88364387 HT |
430 | maxlen = sizeof(r->version_string_ro); |
431 | ||
432 | switch (r->current_image) { | |
433 | case EC_IMAGE_RO: | |
434 | memcpy(id, r->version_string_ro, maxlen); | |
435 | break; | |
436 | case EC_IMAGE_RW: | |
437 | memcpy(id, r->version_string_rw, maxlen); | |
438 | break; | |
439 | default: | |
ac806523 | 440 | log_err("Invalid EC image %d\n", r->current_image); |
88364387 HT |
441 | return -1; |
442 | } | |
443 | ||
444 | id[maxlen - 1] = '\0'; | |
445 | return 0; | |
446 | } | |
447 | ||
6322a7b6 SG |
448 | int cros_ec_read_version(struct udevice *dev, |
449 | struct ec_response_get_version **versionp) | |
88364387 HT |
450 | { |
451 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, | |
452 | (uint8_t **)versionp, sizeof(**versionp)) | |
2ab83f0d | 453 | != sizeof(**versionp)) |
88364387 HT |
454 | return -1; |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
6322a7b6 | 459 | int cros_ec_read_build_info(struct udevice *dev, char **strp) |
88364387 HT |
460 | { |
461 | if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0, | |
836bb6e8 | 462 | (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0) |
88364387 HT |
463 | return -1; |
464 | ||
465 | return 0; | |
466 | } | |
467 | ||
6322a7b6 | 468 | int cros_ec_read_current_image(struct udevice *dev, |
e6c5c94a | 469 | enum ec_current_image *image) |
88364387 HT |
470 | { |
471 | struct ec_response_get_version *r; | |
472 | ||
473 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, | |
2ab83f0d | 474 | (uint8_t **)&r, sizeof(*r)) != sizeof(*r)) |
88364387 HT |
475 | return -1; |
476 | ||
477 | *image = r->current_image; | |
478 | return 0; | |
479 | } | |
480 | ||
6322a7b6 | 481 | static int cros_ec_wait_on_hash_done(struct udevice *dev, |
e6c5c94a | 482 | struct ec_response_vboot_hash *hash) |
88364387 HT |
483 | { |
484 | struct ec_params_vboot_hash p; | |
485 | ulong start; | |
486 | ||
487 | start = get_timer(0); | |
488 | while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) { | |
489 | mdelay(50); /* Insert some reasonable delay */ | |
490 | ||
491 | p.cmd = EC_VBOOT_HASH_GET; | |
492 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), | |
493 | hash, sizeof(*hash)) < 0) | |
494 | return -1; | |
495 | ||
496 | if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) { | |
497 | debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__); | |
498 | return -EC_RES_TIMEOUT; | |
499 | } | |
500 | } | |
501 | return 0; | |
502 | } | |
503 | ||
a12ef7e2 SG |
504 | int cros_ec_read_hash(struct udevice *dev, uint hash_offset, |
505 | struct ec_response_vboot_hash *hash) | |
88364387 HT |
506 | { |
507 | struct ec_params_vboot_hash p; | |
508 | int rv; | |
509 | ||
510 | p.cmd = EC_VBOOT_HASH_GET; | |
a12ef7e2 | 511 | p.offset = hash_offset; |
88364387 HT |
512 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
513 | hash, sizeof(*hash)) < 0) | |
514 | return -1; | |
515 | ||
516 | /* If the EC is busy calculating the hash, fidget until it's done. */ | |
517 | rv = cros_ec_wait_on_hash_done(dev, hash); | |
518 | if (rv) | |
519 | return rv; | |
520 | ||
521 | /* If the hash is valid, we're done. Otherwise, we have to kick it off | |
522 | * again and wait for it to complete. Note that we explicitly assume | |
523 | * that hashing zero bytes is always wrong, even though that would | |
524 | * produce a valid hash value. */ | |
525 | if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size) | |
526 | return 0; | |
527 | ||
528 | debug("%s: No valid hash (status=%d size=%d). Compute one...\n", | |
529 | __func__, hash->status, hash->size); | |
530 | ||
836bb6e8 | 531 | p.cmd = EC_VBOOT_HASH_START; |
88364387 HT |
532 | p.hash_type = EC_VBOOT_HASH_TYPE_SHA256; |
533 | p.nonce_size = 0; | |
a12ef7e2 | 534 | p.offset = hash_offset; |
88364387 HT |
535 | |
536 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), | |
537 | hash, sizeof(*hash)) < 0) | |
538 | return -1; | |
539 | ||
540 | rv = cros_ec_wait_on_hash_done(dev, hash); | |
541 | if (rv) | |
542 | return rv; | |
543 | ||
544 | debug("%s: hash done\n", __func__); | |
545 | ||
546 | return 0; | |
547 | } | |
548 | ||
6322a7b6 | 549 | static int cros_ec_invalidate_hash(struct udevice *dev) |
88364387 HT |
550 | { |
551 | struct ec_params_vboot_hash p; | |
552 | struct ec_response_vboot_hash *hash; | |
553 | ||
554 | /* We don't have an explict command for the EC to discard its current | |
555 | * hash value, so we'll just tell it to calculate one that we know is | |
556 | * wrong (we claim that hashing zero bytes is always invalid). | |
557 | */ | |
558 | p.cmd = EC_VBOOT_HASH_RECALC; | |
559 | p.hash_type = EC_VBOOT_HASH_TYPE_SHA256; | |
560 | p.nonce_size = 0; | |
561 | p.offset = 0; | |
562 | p.size = 0; | |
563 | ||
564 | debug("%s:\n", __func__); | |
565 | ||
566 | if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), | |
567 | (uint8_t **)&hash, sizeof(*hash)) < 0) | |
568 | return -1; | |
569 | ||
570 | /* No need to wait for it to finish */ | |
571 | return 0; | |
572 | } | |
573 | ||
6322a7b6 | 574 | int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags) |
88364387 HT |
575 | { |
576 | struct ec_params_reboot_ec p; | |
577 | ||
578 | p.cmd = cmd; | |
579 | p.flags = flags; | |
580 | ||
581 | if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0) | |
582 | < 0) | |
583 | return -1; | |
584 | ||
585 | if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) { | |
586 | /* | |
587 | * EC reboot will take place immediately so delay to allow it | |
588 | * to complete. Note that some reboot types (EC_REBOOT_COLD) | |
589 | * will reboot the AP as well, in which case we won't actually | |
590 | * get to this point. | |
591 | */ | |
592 | /* | |
593 | * TODO([email protected]): Would be nice if we had a | |
594 | * better way to determine when the reboot is complete. Could | |
595 | * we poll a memory-mapped LPC value? | |
596 | */ | |
597 | udelay(50000); | |
598 | } | |
599 | ||
600 | return 0; | |
601 | } | |
602 | ||
745009c4 | 603 | int cros_ec_interrupt_pending(struct udevice *dev) |
88364387 | 604 | { |
745009c4 SG |
605 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
606 | ||
88364387 | 607 | /* no interrupt support : always poll */ |
745009c4 | 608 | if (!dm_gpio_is_valid(&cdev->ec_int)) |
2ab83f0d | 609 | return -ENOENT; |
88364387 | 610 | |
745009c4 | 611 | return dm_gpio_get_value(&cdev->ec_int); |
88364387 HT |
612 | } |
613 | ||
6322a7b6 | 614 | int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info) |
88364387 | 615 | { |
836bb6e8 | 616 | if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info, |
2ab83f0d | 617 | sizeof(*info)) != sizeof(*info)) |
88364387 HT |
618 | return -1; |
619 | ||
620 | return 0; | |
621 | } | |
622 | ||
72ef8bfd SG |
623 | int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask) |
624 | { | |
625 | struct ec_response_host_event_mask rsp; | |
626 | int ret; | |
627 | ||
628 | ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp)); | |
629 | if (ret < 0) | |
630 | return ret; | |
631 | else if (ret != sizeof(rsp)) | |
632 | return -EINVAL; | |
633 | ||
634 | *mask = rsp.mask; | |
635 | ||
636 | return 0; | |
637 | } | |
638 | ||
639 | int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask) | |
640 | { | |
641 | struct ec_params_host_event_mask req; | |
642 | int ret; | |
643 | ||
644 | req.mask = mask; | |
645 | ||
646 | ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0); | |
647 | if (ret < 0) | |
648 | return ret; | |
649 | ||
650 | return 0; | |
651 | } | |
652 | ||
6322a7b6 | 653 | int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr) |
88364387 HT |
654 | { |
655 | struct ec_response_host_event_mask *resp; | |
656 | ||
657 | /* | |
658 | * Use the B copy of the event flags, because the main copy is already | |
659 | * used by ACPI/SMI. | |
660 | */ | |
661 | if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0, | |
2ab83f0d | 662 | (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp)) |
88364387 HT |
663 | return -1; |
664 | ||
665 | if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID)) | |
666 | return -1; | |
667 | ||
668 | *events_ptr = resp->mask; | |
669 | return 0; | |
670 | } | |
671 | ||
6322a7b6 | 672 | int cros_ec_clear_host_events(struct udevice *dev, uint32_t events) |
88364387 HT |
673 | { |
674 | struct ec_params_host_event_mask params; | |
675 | ||
676 | params.mask = events; | |
677 | ||
678 | /* | |
679 | * Use the B copy of the event flags, so it affects the data returned | |
680 | * by cros_ec_get_host_events(). | |
681 | */ | |
682 | if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0, | |
683 | ¶ms, sizeof(params), NULL, 0) < 0) | |
684 | return -1; | |
685 | ||
686 | return 0; | |
687 | } | |
688 | ||
6322a7b6 SG |
689 | int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask, |
690 | uint32_t set_flags, | |
e6c5c94a | 691 | struct ec_response_flash_protect *resp) |
88364387 HT |
692 | { |
693 | struct ec_params_flash_protect params; | |
694 | ||
695 | params.mask = set_mask; | |
696 | params.flags = set_flags; | |
697 | ||
698 | if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT, | |
699 | ¶ms, sizeof(params), | |
2ab83f0d | 700 | resp, sizeof(*resp)) != sizeof(*resp)) |
88364387 HT |
701 | return -1; |
702 | ||
703 | return 0; | |
704 | } | |
705 | ||
72ef8bfd SG |
706 | int cros_ec_entering_mode(struct udevice *dev, int mode) |
707 | { | |
708 | int rc; | |
709 | ||
710 | rc = ec_command(dev, EC_CMD_ENTERING_MODE, 0, &mode, sizeof(mode), | |
711 | NULL, 0); | |
712 | if (rc) | |
713 | return -1; | |
714 | return 0; | |
715 | } | |
716 | ||
6322a7b6 | 717 | static int cros_ec_check_version(struct udevice *dev) |
88364387 | 718 | { |
6322a7b6 | 719 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
88364387 HT |
720 | struct ec_params_hello req; |
721 | struct ec_response_hello *resp; | |
722 | ||
72a38e06 SG |
723 | struct dm_cros_ec_ops *ops; |
724 | int ret; | |
725 | ||
6322a7b6 | 726 | ops = dm_cros_ec_get_ops(dev); |
72a38e06 | 727 | if (ops->check_version) { |
6322a7b6 | 728 | ret = ops->check_version(dev); |
72a38e06 SG |
729 | if (ret) |
730 | return ret; | |
731 | } | |
88364387 HT |
732 | |
733 | /* | |
734 | * TODO([email protected]). | |
735 | * There is a strange oddity here with the EC. We could just ignore | |
736 | * the response, i.e. pass the last two parameters as NULL and 0. | |
737 | * In this case we won't read back very many bytes from the EC. | |
738 | * On the I2C bus the EC gets upset about this and will try to send | |
739 | * the bytes anyway. This means that we will have to wait for that | |
740 | * to complete before continuing with a new EC command. | |
741 | * | |
742 | * This problem is probably unique to the I2C bus. | |
743 | * | |
744 | * So for now, just read all the data anyway. | |
745 | */ | |
e8c12662 | 746 | |
a6070283 | 747 | /* Try sending a version 3 packet */ |
6322a7b6 | 748 | cdev->protocol_version = 3; |
d11e8fd8 | 749 | req.in_data = 0; |
a6070283 | 750 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
9fea76f5 | 751 | (uint8_t **)&resp, sizeof(*resp)) > 0) |
a6070283 | 752 | return 0; |
a6070283 | 753 | |
e8c12662 | 754 | /* Try sending a version 2 packet */ |
6322a7b6 | 755 | cdev->protocol_version = 2; |
88364387 | 756 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
9fea76f5 | 757 | (uint8_t **)&resp, sizeof(*resp)) > 0) |
e8c12662 | 758 | return 0; |
88364387 | 759 | |
e8c12662 RS |
760 | /* |
761 | * Fail if we're still here, since the EC doesn't understand any | |
762 | * protcol version we speak. Version 1 interface without command | |
763 | * version is no longer supported, and we don't know about any new | |
764 | * protocol versions. | |
765 | */ | |
6322a7b6 | 766 | cdev->protocol_version = 0; |
e8c12662 RS |
767 | printf("%s: ERROR: old EC interface not supported\n", __func__); |
768 | return -1; | |
88364387 HT |
769 | } |
770 | ||
6322a7b6 | 771 | int cros_ec_test(struct udevice *dev) |
88364387 HT |
772 | { |
773 | struct ec_params_hello req; | |
774 | struct ec_response_hello *resp; | |
775 | ||
776 | req.in_data = 0x12345678; | |
777 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), | |
778 | (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) { | |
779 | printf("ec_command_inptr() returned error\n"); | |
780 | return -1; | |
781 | } | |
782 | if (resp->out_data != req.in_data + 0x01020304) { | |
783 | printf("Received invalid handshake %x\n", resp->out_data); | |
784 | return -1; | |
785 | } | |
786 | ||
787 | return 0; | |
788 | } | |
789 | ||
6322a7b6 | 790 | int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region, |
88364387 HT |
791 | uint32_t *offset, uint32_t *size) |
792 | { | |
793 | struct ec_params_flash_region_info p; | |
794 | struct ec_response_flash_region_info *r; | |
795 | int ret; | |
796 | ||
797 | p.region = region; | |
798 | ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO, | |
799 | EC_VER_FLASH_REGION_INFO, | |
800 | &p, sizeof(p), (uint8_t **)&r, sizeof(*r)); | |
801 | if (ret != sizeof(*r)) | |
802 | return -1; | |
803 | ||
804 | if (offset) | |
805 | *offset = r->offset; | |
806 | if (size) | |
807 | *size = r->size; | |
808 | ||
809 | return 0; | |
810 | } | |
811 | ||
6322a7b6 | 812 | int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size) |
88364387 HT |
813 | { |
814 | struct ec_params_flash_erase p; | |
815 | ||
816 | p.offset = offset; | |
817 | p.size = size; | |
818 | return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p), | |
819 | NULL, 0); | |
820 | } | |
821 | ||
822 | /** | |
823 | * Write a single block to the flash | |
824 | * | |
825 | * Write a block of data to the EC flash. The size must not exceed the flash | |
826 | * write block size which you can obtain from cros_ec_flash_write_burst_size(). | |
827 | * | |
828 | * The offset starts at 0. You can obtain the region information from | |
829 | * cros_ec_flash_offset() to find out where to write for a particular region. | |
830 | * | |
831 | * Attempting to write to the region where the EC is currently running from | |
832 | * will result in an error. | |
833 | * | |
834 | * @param dev CROS-EC device | |
835 | * @param data Pointer to data buffer to write | |
836 | * @param offset Offset within flash to write to. | |
837 | * @param size Number of bytes to write | |
838 | * @return 0 if ok, -1 on error | |
839 | */ | |
6322a7b6 SG |
840 | static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data, |
841 | uint32_t offset, uint32_t size) | |
88364387 | 842 | { |
bae5b97e MF |
843 | struct ec_params_flash_write *p; |
844 | int ret; | |
88364387 | 845 | |
bae5b97e MF |
846 | p = malloc(sizeof(*p) + size); |
847 | if (!p) | |
848 | return -ENOMEM; | |
849 | ||
850 | p->offset = offset; | |
851 | p->size = size; | |
852 | assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE); | |
853 | memcpy(p + 1, data, p->size); | |
88364387 | 854 | |
bae5b97e MF |
855 | ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0, |
856 | p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1; | |
857 | ||
858 | free(p); | |
859 | ||
860 | return ret; | |
88364387 HT |
861 | } |
862 | ||
863 | /** | |
864 | * Return optimal flash write burst size | |
865 | */ | |
6322a7b6 | 866 | static int cros_ec_flash_write_burst_size(struct udevice *dev) |
88364387 | 867 | { |
836bb6e8 | 868 | return EC_FLASH_WRITE_VER0_SIZE; |
88364387 HT |
869 | } |
870 | ||
871 | /** | |
872 | * Check if a block of data is erased (all 0xff) | |
873 | * | |
874 | * This function is useful when dealing with flash, for checking whether a | |
875 | * data block is erased and thus does not need to be programmed. | |
876 | * | |
877 | * @param data Pointer to data to check (must be word-aligned) | |
878 | * @param size Number of bytes to check (must be word-aligned) | |
879 | * @return 0 if erased, non-zero if any word is not erased | |
880 | */ | |
881 | static int cros_ec_data_is_erased(const uint32_t *data, int size) | |
882 | { | |
883 | assert(!(size & 3)); | |
884 | size /= sizeof(uint32_t); | |
885 | for (; size > 0; size -= 4, data++) | |
886 | if (*data != -1U) | |
887 | return 0; | |
888 | ||
889 | return 1; | |
890 | } | |
891 | ||
281ca88f MF |
892 | /** |
893 | * Read back flash parameters | |
894 | * | |
895 | * This function reads back parameters of the flash as reported by the EC | |
896 | * | |
897 | * @param dev Pointer to device | |
898 | * @param info Pointer to output flash info struct | |
899 | */ | |
6322a7b6 | 900 | int cros_ec_read_flashinfo(struct udevice *dev, |
e6c5c94a | 901 | struct ec_response_flash_info *info) |
281ca88f MF |
902 | { |
903 | int ret; | |
904 | ||
905 | ret = ec_command(dev, EC_CMD_FLASH_INFO, 0, | |
906 | NULL, 0, info, sizeof(*info)); | |
907 | if (ret < 0) | |
908 | return ret; | |
909 | ||
910 | return ret < sizeof(*info) ? -1 : 0; | |
911 | } | |
912 | ||
6322a7b6 | 913 | int cros_ec_flash_write(struct udevice *dev, const uint8_t *data, |
e6c5c94a | 914 | uint32_t offset, uint32_t size) |
88364387 | 915 | { |
6322a7b6 | 916 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
88364387 HT |
917 | uint32_t burst = cros_ec_flash_write_burst_size(dev); |
918 | uint32_t end, off; | |
919 | int ret; | |
920 | ||
dc05ac0f SG |
921 | if (!burst) |
922 | return -EINVAL; | |
923 | ||
88364387 HT |
924 | /* |
925 | * TODO: round up to the nearest multiple of write size. Can get away | |
926 | * without that on link right now because its write size is 4 bytes. | |
927 | */ | |
928 | end = offset + size; | |
929 | for (off = offset; off < end; off += burst, data += burst) { | |
930 | uint32_t todo; | |
931 | ||
932 | /* If the data is empty, there is no point in programming it */ | |
933 | todo = min(end - off, burst); | |
6322a7b6 | 934 | if (cdev->optimise_flash_write && |
e6c5c94a | 935 | cros_ec_data_is_erased((uint32_t *)data, todo)) |
88364387 HT |
936 | continue; |
937 | ||
938 | ret = cros_ec_flash_write_block(dev, data, off, todo); | |
939 | if (ret) | |
940 | return ret; | |
941 | } | |
942 | ||
943 | return 0; | |
944 | } | |
945 | ||
72ef8bfd SG |
946 | /** |
947 | * Run verification on a slot | |
948 | * | |
949 | * @param me CrosEc instance | |
950 | * @param region Region to run verification on | |
951 | * @return 0 if success or not applicable. Non-zero if verification failed. | |
952 | */ | |
953 | int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region) | |
954 | { | |
955 | struct ec_params_efs_verify p; | |
956 | int rv; | |
957 | ||
958 | log_info("EFS: EC is verifying updated image...\n"); | |
959 | p.region = region; | |
960 | ||
961 | rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0); | |
962 | if (rv >= 0) { | |
963 | log_info("EFS: Verification success\n"); | |
964 | return 0; | |
965 | } | |
966 | if (rv == -EC_RES_INVALID_COMMAND) { | |
967 | log_info("EFS: EC doesn't support EFS_VERIFY command\n"); | |
968 | return 0; | |
969 | } | |
970 | log_info("EFS: Verification failed\n"); | |
971 | ||
972 | return rv; | |
973 | } | |
974 | ||
88364387 HT |
975 | /** |
976 | * Read a single block from the flash | |
977 | * | |
978 | * Read a block of data from the EC flash. The size must not exceed the flash | |
979 | * write block size which you can obtain from cros_ec_flash_write_burst_size(). | |
980 | * | |
981 | * The offset starts at 0. You can obtain the region information from | |
982 | * cros_ec_flash_offset() to find out where to read for a particular region. | |
983 | * | |
984 | * @param dev CROS-EC device | |
985 | * @param data Pointer to data buffer to read into | |
986 | * @param offset Offset within flash to read from | |
987 | * @param size Number of bytes to read | |
988 | * @return 0 if ok, -1 on error | |
989 | */ | |
6322a7b6 | 990 | static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data, |
e6c5c94a | 991 | uint32_t offset, uint32_t size) |
88364387 HT |
992 | { |
993 | struct ec_params_flash_read p; | |
994 | ||
995 | p.offset = offset; | |
996 | p.size = size; | |
997 | ||
998 | return ec_command(dev, EC_CMD_FLASH_READ, 0, | |
999 | &p, sizeof(p), data, size) >= 0 ? 0 : -1; | |
1000 | } | |
1001 | ||
6322a7b6 | 1002 | int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset, |
e6c5c94a | 1003 | uint32_t size) |
88364387 HT |
1004 | { |
1005 | uint32_t burst = cros_ec_flash_write_burst_size(dev); | |
1006 | uint32_t end, off; | |
1007 | int ret; | |
1008 | ||
1009 | end = offset + size; | |
1010 | for (off = offset; off < end; off += burst, data += burst) { | |
1011 | ret = cros_ec_flash_read_block(dev, data, off, | |
1012 | min(end - off, burst)); | |
1013 | if (ret) | |
1014 | return ret; | |
1015 | } | |
1016 | ||
1017 | return 0; | |
1018 | } | |
1019 | ||
6322a7b6 | 1020 | int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image, |
e6c5c94a | 1021 | int image_size) |
88364387 HT |
1022 | { |
1023 | uint32_t rw_offset, rw_size; | |
1024 | int ret; | |
1025 | ||
6f1c0430 SG |
1026 | if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset, |
1027 | &rw_size)) | |
88364387 | 1028 | return -1; |
2ab83f0d | 1029 | if (image_size > (int)rw_size) |
88364387 HT |
1030 | return -1; |
1031 | ||
1032 | /* Invalidate the existing hash, just in case the AP reboots | |
1033 | * unexpectedly during the update. If that happened, the EC RW firmware | |
1034 | * would be invalid, but the EC would still have the original hash. | |
1035 | */ | |
1036 | ret = cros_ec_invalidate_hash(dev); | |
1037 | if (ret) | |
1038 | return ret; | |
1039 | ||
1040 | /* | |
1041 | * Erase the entire RW section, so that the EC doesn't see any garbage | |
1042 | * past the new image if it's smaller than the current image. | |
1043 | * | |
1044 | * TODO: could optimize this to erase just the current image, since | |
1045 | * presumably everything past that is 0xff's. But would still need to | |
1046 | * round up to the nearest multiple of erase size. | |
1047 | */ | |
1048 | ret = cros_ec_flash_erase(dev, rw_offset, rw_size); | |
1049 | if (ret) | |
1050 | return ret; | |
1051 | ||
1052 | /* Write the image */ | |
1053 | ret = cros_ec_flash_write(dev, image, rw_offset, image_size); | |
1054 | if (ret) | |
1055 | return ret; | |
1056 | ||
1057 | return 0; | |
1058 | } | |
1059 | ||
6322a7b6 | 1060 | int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size) |
88364387 HT |
1061 | { |
1062 | struct ec_params_vbnvcontext p; | |
1063 | int len; | |
1064 | ||
72ef8bfd | 1065 | if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2) |
6322a7b6 SG |
1066 | return -EINVAL; |
1067 | ||
88364387 HT |
1068 | p.op = EC_VBNV_CONTEXT_OP_READ; |
1069 | ||
1070 | len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT, | |
72ef8bfd SG |
1071 | &p, sizeof(uint32_t) + size, block, size); |
1072 | if (len != size) { | |
1073 | log_err("Expected %d bytes, got %d\n", size, len); | |
6322a7b6 | 1074 | return -EIO; |
72ef8bfd | 1075 | } |
88364387 HT |
1076 | |
1077 | return 0; | |
1078 | } | |
1079 | ||
6322a7b6 | 1080 | int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size) |
88364387 HT |
1081 | { |
1082 | struct ec_params_vbnvcontext p; | |
1083 | int len; | |
1084 | ||
72ef8bfd | 1085 | if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2) |
6322a7b6 | 1086 | return -EINVAL; |
88364387 | 1087 | p.op = EC_VBNV_CONTEXT_OP_WRITE; |
72ef8bfd | 1088 | memcpy(p.block, block, size); |
88364387 HT |
1089 | |
1090 | len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT, | |
72ef8bfd | 1091 | &p, sizeof(uint32_t) + size, NULL, 0); |
88364387 HT |
1092 | if (len < 0) |
1093 | return -1; | |
1094 | ||
1095 | return 0; | |
1096 | } | |
1097 | ||
72ef8bfd SG |
1098 | int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags) |
1099 | { | |
1100 | struct ec_params_battery_cutoff p; | |
1101 | int len; | |
1102 | ||
1103 | p.flags = flags; | |
1104 | len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p), | |
1105 | NULL, 0); | |
1106 | ||
1107 | if (len < 0) | |
1108 | return -1; | |
1109 | return 0; | |
1110 | } | |
1111 | ||
f48eaf01 | 1112 | int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state) |
88364387 HT |
1113 | { |
1114 | struct ec_params_ldo_set params; | |
1115 | ||
1116 | params.index = index; | |
1117 | params.state = state; | |
1118 | ||
6322a7b6 | 1119 | if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params), |
f48eaf01 | 1120 | NULL, 0)) |
88364387 HT |
1121 | return -1; |
1122 | ||
1123 | return 0; | |
1124 | } | |
1125 | ||
f48eaf01 | 1126 | int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state) |
88364387 HT |
1127 | { |
1128 | struct ec_params_ldo_get params; | |
1129 | struct ec_response_ldo_get *resp; | |
1130 | ||
1131 | params.index = index; | |
1132 | ||
6322a7b6 | 1133 | if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params), |
f48eaf01 SG |
1134 | (uint8_t **)&resp, sizeof(*resp)) != |
1135 | sizeof(*resp)) | |
88364387 HT |
1136 | return -1; |
1137 | ||
1138 | *state = resp->state; | |
1139 | ||
1140 | return 0; | |
1141 | } | |
1142 | ||
84d6cbd3 | 1143 | int cros_ec_register(struct udevice *dev) |
88364387 | 1144 | { |
e564f054 | 1145 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
88364387 | 1146 | char id[MSG_BYTES]; |
84d6cbd3 SG |
1147 | |
1148 | cdev->dev = dev; | |
32f8a19f SG |
1149 | gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int, |
1150 | GPIOD_IS_IN); | |
2ec9d171 | 1151 | cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write"); |
84d6cbd3 | 1152 | |
6322a7b6 | 1153 | if (cros_ec_check_version(dev)) { |
84d6cbd3 SG |
1154 | debug("%s: Could not detect CROS-EC version\n", __func__); |
1155 | return -CROS_EC_ERR_CHECK_VERSION; | |
1156 | } | |
1157 | ||
6322a7b6 | 1158 | if (cros_ec_read_id(dev, id, sizeof(id))) { |
84d6cbd3 SG |
1159 | debug("%s: Could not read KBC ID\n", __func__); |
1160 | return -CROS_EC_ERR_READ_ID; | |
1161 | } | |
1162 | ||
1163 | /* Remember this device for use by the cros_ec command */ | |
c4b206df SG |
1164 | debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n", |
1165 | cdev->protocol_version, id); | |
84d6cbd3 SG |
1166 | |
1167 | return 0; | |
1168 | } | |
88364387 | 1169 | |
2ec9d171 | 1170 | int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config) |
d7f25f35 | 1171 | { |
2ec9d171 | 1172 | ofnode flash_node, node; |
d7f25f35 | 1173 | |
2ec9d171 SG |
1174 | flash_node = dev_read_subnode(dev, "flash"); |
1175 | if (!ofnode_valid(flash_node)) { | |
d7f25f35 SG |
1176 | debug("Failed to find flash node\n"); |
1177 | return -1; | |
1178 | } | |
1179 | ||
5e0a7341 | 1180 | if (ofnode_read_fmap_entry(flash_node, &config->flash)) { |
2ec9d171 | 1181 | debug("Failed to decode flash node in chrome-ec\n"); |
d7f25f35 SG |
1182 | return -1; |
1183 | } | |
1184 | ||
2ec9d171 SG |
1185 | config->flash_erase_value = ofnode_read_s32_default(flash_node, |
1186 | "erase-value", -1); | |
3991f42e | 1187 | ofnode_for_each_subnode(node, flash_node) { |
2ec9d171 | 1188 | const char *name = ofnode_get_name(node); |
d7f25f35 SG |
1189 | enum ec_flash_region region; |
1190 | ||
1191 | if (0 == strcmp(name, "ro")) { | |
1192 | region = EC_FLASH_REGION_RO; | |
1193 | } else if (0 == strcmp(name, "rw")) { | |
6f1c0430 | 1194 | region = EC_FLASH_REGION_ACTIVE; |
d7f25f35 SG |
1195 | } else if (0 == strcmp(name, "wp-ro")) { |
1196 | region = EC_FLASH_REGION_WP_RO; | |
1197 | } else { | |
1198 | debug("Unknown EC flash region name '%s'\n", name); | |
1199 | return -1; | |
1200 | } | |
1201 | ||
5e0a7341 | 1202 | if (ofnode_read_fmap_entry(node, &config->region[region])) { |
d7f25f35 SG |
1203 | debug("Failed to decode flash region in chrome-ec'\n"); |
1204 | return -1; | |
1205 | } | |
1206 | } | |
1207 | ||
1208 | return 0; | |
1209 | } | |
1210 | ||
6d1a718f MF |
1211 | int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in, |
1212 | int nmsgs) | |
cc456bd7 | 1213 | { |
cc456bd7 SG |
1214 | union { |
1215 | struct ec_params_i2c_passthru p; | |
1216 | uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE]; | |
1217 | } params; | |
1218 | union { | |
1219 | struct ec_response_i2c_passthru r; | |
1220 | uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE]; | |
1221 | } response; | |
1222 | struct ec_params_i2c_passthru *p = ¶ms.p; | |
1223 | struct ec_response_i2c_passthru *r = &response.r; | |
1224 | struct ec_params_i2c_passthru_msg *msg; | |
1225 | uint8_t *pdata, *read_ptr = NULL; | |
1226 | int read_len; | |
1227 | int size; | |
1228 | int rv; | |
1229 | int i; | |
1230 | ||
6d1a718f | 1231 | p->port = port; |
cc456bd7 SG |
1232 | |
1233 | p->num_msgs = nmsgs; | |
1234 | size = sizeof(*p) + p->num_msgs * sizeof(*msg); | |
1235 | ||
1236 | /* Create a message to write the register address and optional data */ | |
1237 | pdata = (uint8_t *)p + size; | |
1238 | ||
1239 | read_len = 0; | |
1240 | for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) { | |
1241 | bool is_read = in->flags & I2C_M_RD; | |
1242 | ||
1243 | msg->addr_flags = in->addr; | |
1244 | msg->len = in->len; | |
1245 | if (is_read) { | |
1246 | msg->addr_flags |= EC_I2C_FLAG_READ; | |
1247 | read_len += in->len; | |
1248 | read_ptr = in->buf; | |
1249 | if (sizeof(*r) + read_len > sizeof(response)) { | |
1250 | puts("Read length too big for buffer\n"); | |
1251 | return -1; | |
1252 | } | |
1253 | } else { | |
1254 | if (pdata - (uint8_t *)p + in->len > sizeof(params)) { | |
1255 | puts("Params too large for buffer\n"); | |
1256 | return -1; | |
1257 | } | |
1258 | memcpy(pdata, in->buf, in->len); | |
1259 | pdata += in->len; | |
1260 | } | |
1261 | } | |
1262 | ||
6322a7b6 | 1263 | rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p, |
cc456bd7 SG |
1264 | r, sizeof(*r) + read_len); |
1265 | if (rv < 0) | |
1266 | return rv; | |
1267 | ||
1268 | /* Parse response */ | |
1269 | if (r->i2c_status & EC_I2C_STATUS_ERROR) { | |
1270 | printf("Transfer failed with status=0x%x\n", r->i2c_status); | |
1271 | return -1; | |
1272 | } | |
1273 | ||
1274 | if (rv < sizeof(*r) + read_len) { | |
1275 | puts("Truncated read response\n"); | |
1276 | return -1; | |
1277 | } | |
1278 | ||
1279 | /* We only support a single read message for each transfer */ | |
1280 | if (read_len) | |
1281 | memcpy(read_ptr, r->data, read_len); | |
1282 | ||
1283 | return 0; | |
1284 | } | |
1285 | ||
72ef8bfd SG |
1286 | int cros_ec_check_feature(struct udevice *dev, int feature) |
1287 | { | |
1288 | struct ec_response_get_features r; | |
1289 | int rv; | |
1290 | ||
1291 | rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0); | |
1292 | if (rv) | |
1293 | return rv; | |
1294 | ||
1295 | if (feature >= 8 * sizeof(r.flags)) | |
1296 | return -1; | |
1297 | ||
1298 | return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature); | |
1299 | } | |
1300 | ||
1301 | /* | |
1302 | * Query the EC for specified mask indicating enabled events. | |
1303 | * The EC maintains separate event masks for SMI, SCI and WAKE. | |
1304 | */ | |
1305 | static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action, | |
1306 | uint64_t *value) | |
1307 | { | |
1308 | int ret; | |
1309 | struct ec_params_host_event req; | |
1310 | struct ec_response_host_event rsp; | |
1311 | ||
1312 | req.action = action; | |
1313 | req.mask_type = mask; | |
1314 | if (action != EC_HOST_EVENT_GET) | |
1315 | req.value = *value; | |
1316 | else | |
1317 | *value = 0; | |
1318 | ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp, | |
1319 | sizeof(rsp)); | |
1320 | ||
1321 | if (action != EC_HOST_EVENT_GET) | |
1322 | return ret; | |
1323 | if (ret == 0) | |
1324 | *value = rsp.value; | |
1325 | ||
1326 | return ret; | |
1327 | } | |
1328 | ||
1329 | static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd, | |
1330 | uint action, uint64_t *value) | |
1331 | { | |
1332 | int ret = -1; | |
1333 | struct ec_params_host_event_mask req; | |
1334 | struct ec_response_host_event_mask rsp; | |
1335 | ||
1336 | if (hcmd == INVALID_HCMD) | |
1337 | return ret; | |
1338 | ||
1339 | if (action != EC_HOST_EVENT_GET) | |
1340 | req.mask = (uint32_t)*value; | |
1341 | else | |
1342 | *value = 0; | |
1343 | ||
1344 | ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp)); | |
1345 | if (action != EC_HOST_EVENT_GET) | |
1346 | return ret; | |
1347 | if (ret == 0) | |
1348 | *value = rsp.mask; | |
1349 | ||
1350 | return ret; | |
1351 | } | |
1352 | ||
1353 | bool cros_ec_is_uhepi_supported(struct udevice *dev) | |
1354 | { | |
1355 | #define UHEPI_SUPPORTED 1 | |
1356 | #define UHEPI_NOT_SUPPORTED 2 | |
1357 | static int uhepi_support; | |
1358 | ||
1359 | if (!uhepi_support) { | |
1360 | uhepi_support = cros_ec_check_feature(dev, | |
1361 | EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED : | |
1362 | UHEPI_NOT_SUPPORTED; | |
1363 | log_debug("Chrome EC: UHEPI %s\n", | |
1364 | uhepi_support == UHEPI_SUPPORTED ? "supported" : | |
1365 | "not supported"); | |
1366 | } | |
1367 | return uhepi_support == UHEPI_SUPPORTED; | |
1368 | } | |
1369 | ||
1370 | static int cros_ec_get_mask(struct udevice *dev, uint type) | |
1371 | { | |
1372 | u64 value = 0; | |
1373 | ||
1374 | if (cros_ec_is_uhepi_supported(dev)) { | |
1375 | cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value); | |
1376 | } else { | |
1377 | assert(type < ARRAY_SIZE(event_map)); | |
1378 | cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd, | |
1379 | EC_HOST_EVENT_GET, &value); | |
1380 | } | |
1381 | return value; | |
1382 | } | |
1383 | ||
1384 | static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask) | |
1385 | { | |
1386 | if (cros_ec_is_uhepi_supported(dev)) | |
1387 | return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask); | |
1388 | ||
1389 | assert(type < ARRAY_SIZE(event_map)); | |
1390 | ||
1391 | return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd, | |
1392 | EC_HOST_EVENT_CLEAR, &mask); | |
1393 | } | |
1394 | ||
1395 | uint64_t cros_ec_get_events_b(struct udevice *dev) | |
1396 | { | |
1397 | return cros_ec_get_mask(dev, EC_HOST_EVENT_B); | |
1398 | } | |
1399 | ||
1400 | int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask) | |
1401 | { | |
1402 | log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask); | |
1403 | ||
1404 | return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask); | |
1405 | } | |
1406 | ||
1407 | int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp) | |
1408 | { | |
1409 | struct ec_params_charge_state p; | |
1410 | struct ec_response_charge_state r; | |
1411 | int ret; | |
1412 | ||
1413 | p.cmd = CHARGE_STATE_CMD_GET_PARAM; | |
1414 | p.get_param.param = CS_PARAM_LIMIT_POWER; | |
1415 | ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p), | |
1416 | &r, sizeof(r)); | |
1417 | ||
1418 | /* | |
1419 | * If our EC doesn't support the LIMIT_POWER parameter, assume that | |
1420 | * LIMIT_POWER is not requested. | |
1421 | */ | |
1422 | if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) { | |
1423 | log_warning("PARAM_LIMIT_POWER not supported by EC\n"); | |
1424 | return -ENOSYS; | |
1425 | } | |
1426 | ||
1427 | if (ret != sizeof(r.get_param)) | |
1428 | return -EINVAL; | |
1429 | ||
1430 | *limit_powerp = r.get_param.value; | |
1431 | return 0; | |
1432 | } | |
1433 | ||
1434 | int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags) | |
1435 | { | |
1436 | struct ec_params_config_power_button params; | |
1437 | int ret; | |
1438 | ||
1439 | params.flags = flags; | |
1440 | ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0, | |
1441 | ¶ms, sizeof(params), NULL, 0); | |
1442 | if (ret < 0) | |
1443 | return ret; | |
1444 | ||
1445 | return 0; | |
1446 | } | |
1447 | ||
1448 | int cros_ec_get_lid_shutdown_mask(struct udevice *dev) | |
1449 | { | |
1450 | u32 mask; | |
1451 | int ret; | |
1452 | ||
1453 | ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK, | |
1454 | &mask); | |
1455 | if (ret < 0) | |
1456 | return ret; | |
1457 | ||
1458 | return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED)); | |
1459 | } | |
1460 | ||
1461 | int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable) | |
1462 | { | |
1463 | u32 mask; | |
1464 | int ret; | |
1465 | ||
1466 | ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK, | |
1467 | &mask); | |
1468 | if (ret < 0) | |
1469 | return ret; | |
1470 | ||
a749c09a | 1471 | /* Set lid close event state in the EC SMI event mask */ |
72ef8bfd SG |
1472 | if (enable) |
1473 | mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED); | |
1474 | else | |
1475 | mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED); | |
1476 | ||
1477 | ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask); | |
1478 | if (ret < 0) | |
1479 | return ret; | |
1480 | ||
1481 | printf("EC: %sabled lid close event\n", enable ? "en" : "dis"); | |
1482 | return 0; | |
1483 | } | |
1484 | ||
84d6cbd3 SG |
1485 | UCLASS_DRIVER(cros_ec) = { |
1486 | .id = UCLASS_CROS_EC, | |
16f4d051 | 1487 | .name = "cros-ec", |
84d6cbd3 | 1488 | .per_device_auto_alloc_size = sizeof(struct cros_ec_dev), |
91195485 | 1489 | .post_bind = dm_scan_fdt_dev, |
4bf6f2ad | 1490 | .flags = DM_UC_FLAG_ALLOC_PRIV_DMA, |
84d6cbd3 | 1491 | }; |