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