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