Newer
Older
/*
* Chromium OS cros_ec driver
*
* Copyright (c) 2012 The Chromium OS Authors.
*
* SPDX-License-Identifier: GPL-2.0+
* This is the interface to the Chrome OS EC. It provides keyboard functions,
* power control and battery management. Quite a few other functions are
* provided to enable the EC software to be updated, talk to the EC's I2C bus
* and store a small amount of data in a memory which persists while the EC
* is not reset.
*/
#include <common.h>
#include <command.h>
#include <i2c.h>
#include <cros_ec.h>
#include <fdtdec.h>
#include <malloc.h>
#include <spi.h>
#include <linux/errno.h>
#include <asm/io.h>
#include <asm-generic/gpio.h>
#include <dm/device-internal.h>
#include <dm/uclass-internal.h>
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifdef DEBUG_TRACE
#define debug_trace(fmt, b...) debug(fmt, #b)
#else
#define debug_trace(fmt, b...)
#endif
enum {
/* Timeout waiting for a flash erase command to complete */
CROS_EC_CMD_TIMEOUT_MS = 5000,
/* Timeout waiting for a synchronous hash to be recomputed */
CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
};
DECLARE_GLOBAL_DATA_PTR;
/* Note: depends on enum ec_current_image */
static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
{
#ifdef DEBUG
int i;
printf("%s: ", name);
if (cmd != -1)
printf("cmd=%#x: ", cmd);
for (i = 0; i < len; i++)
printf("%02x ", data[i]);
printf("\n");
#endif
}
/*
* Calculate a simple 8-bit checksum of a data block
*
* @param data Data block to checksum
* @param size Size of data block in bytes
* @return checksum value (0 to 255)
*/
int cros_ec_calc_checksum(const uint8_t *data, int size)
{
int csum, i;
for (i = csum = 0; i < size; i++)
csum += data[i];
return csum & 0xff;
}
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Create a request packet for protocol version 3.
*
* The packet is stored in the device's internal output buffer.
*
* @param dev CROS-EC device
* @param cmd Command to send (EC_CMD_...)
* @param cmd_version Version of command to send (EC_VER_...)
* @param dout Output data (may be NULL If dout_len=0)
* @param dout_len Size of output data in bytes
* @return packet size in bytes, or <0 if error.
*/
static int create_proto3_request(struct cros_ec_dev *dev,
int cmd, int cmd_version,
const void *dout, int dout_len)
{
struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
int out_bytes = dout_len + sizeof(*rq);
/* Fail if output size is too big */
if (out_bytes > (int)sizeof(dev->dout)) {
debug("%s: Cannot send %d bytes\n", __func__, dout_len);
return -EC_RES_REQUEST_TRUNCATED;
}
/* Fill in request packet */
rq->struct_version = EC_HOST_REQUEST_VERSION;
rq->checksum = 0;
rq->command = cmd;
rq->command_version = cmd_version;
rq->reserved = 0;
rq->data_len = dout_len;
/* Copy data after header */
memcpy(rq + 1, dout, dout_len);
/* Write checksum field so the entire packet sums to 0 */
rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
/* Return size of request packet */
return out_bytes;
}
/**
* Prepare the device to receive a protocol version 3 response.
*
* @param dev CROS-EC device
* @param din_len Maximum size of response in bytes
* @return maximum expected number of bytes in response, or <0 if error.
*/
static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
{
int in_bytes = din_len + sizeof(struct ec_host_response);
/* Fail if input size is too big */
if (in_bytes > (int)sizeof(dev->din)) {
debug("%s: Cannot receive %d bytes\n", __func__, din_len);
return -EC_RES_RESPONSE_TOO_BIG;
}
/* Return expected size of response packet */
return in_bytes;
}
/**
* Handle a protocol version 3 response packet.
*
* The packet must already be stored in the device's internal input buffer.
*
* @param dev CROS-EC device
* @param dinp Returns pointer to response data
* @param din_len Maximum size of response in bytes
* @return number of bytes of response data, or <0 if error. Note that error
* codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
* overlap!)
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
*/
static int handle_proto3_response(struct cros_ec_dev *dev,
uint8_t **dinp, int din_len)
{
struct ec_host_response *rs = (struct ec_host_response *)dev->din;
int in_bytes;
int csum;
cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
/* Check input data */
if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
debug("%s: EC response version mismatch\n", __func__);
return -EC_RES_INVALID_RESPONSE;
}
if (rs->reserved) {
debug("%s: EC response reserved != 0\n", __func__);
return -EC_RES_INVALID_RESPONSE;
}
if (rs->data_len > din_len) {
debug("%s: EC returned too much data\n", __func__);
return -EC_RES_RESPONSE_TOO_BIG;
}
cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
/* Update in_bytes to actual data size */
in_bytes = sizeof(*rs) + rs->data_len;
/* Verify checksum */
csum = cros_ec_calc_checksum(dev->din, in_bytes);
if (csum) {
debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
csum);
return -EC_RES_INVALID_CHECKSUM;
}
/* Return error result, if any */
if (rs->result)
return -(int)rs->result;
/* If we're still here, set response data pointer and return length */
*dinp = (uint8_t *)(rs + 1);
return rs->data_len;
}
static int send_command_proto3(struct cros_ec_dev *dev,
int cmd, int cmd_version,
const void *dout, int dout_len,
uint8_t **dinp, int din_len)
{
int out_bytes, in_bytes;
int rv;
/* Create request packet */
out_bytes = create_proto3_request(dev, cmd, cmd_version,
dout, dout_len);
if (out_bytes < 0)
return out_bytes;
/* Prepare response buffer */
in_bytes = prepare_proto3_response_buffer(dev, din_len);
if (in_bytes < 0)
return in_bytes;
ops = dm_cros_ec_get_ops(dev->dev);
rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS;
if (rv < 0)
return rv;
/* Process the response */
return handle_proto3_response(dev, dinp, din_len);
}
static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
const void *dout, int dout_len,
uint8_t **dinp, int din_len)
{
int ret = -1;
/* Handle protocol version 3 support */
if (dev->protocol_version == 3) {
return send_command_proto3(dev, cmd, cmd_version,
dout, dout_len, dinp, din_len);
}
ops = dm_cros_ec_get_ops(dev->dev);
ret = ops->command(dev->dev, cmd, cmd_version,
(const uint8_t *)dout, dout_len, dinp, din_len);
return ret;
}
/**
* Send a command to the CROS-EC device and return the reply.
*
* The device's internal input/output buffers are used.
*
* @param dev CROS-EC device
* @param cmd Command to send (EC_CMD_...)
* @param cmd_version Version of command to send (EC_VER_...)
* @param dout Output data (may be NULL If dout_len=0)
* @param dout_len Size of output data in bytes
* @param dinp Response data (may be NULL If din_len=0).
* If not NULL, it will be updated to point to the data
* and will always be double word aligned (64-bits)
* @param din_len Maximum size of response in bytes
* @return number of bytes in response, or -ve on error
*/
static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
int din_len)
{
uint8_t *din = NULL;
int len;
len = send_command(dev, cmd, cmd_version, dout, dout_len,
&din, din_len);
/* If the command doesn't complete, wait a while */
if (len == -EC_RES_IN_PROGRESS) {
struct ec_response_get_comms_status *resp = NULL;
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
ulong start;
/* Wait for command to complete */
start = get_timer(0);
do {
int ret;
mdelay(50); /* Insert some reasonable delay */
ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
NULL, 0,
(uint8_t **)&resp, sizeof(*resp));
if (ret < 0)
return ret;
if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
debug("%s: Command %#02x timeout\n",
__func__, cmd);
return -EC_RES_TIMEOUT;
}
} while (resp->flags & EC_COMMS_STATUS_PROCESSING);
/* OK it completed, so read the status response */
/* not sure why it was 0 for the last argument */
len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
NULL, 0, &din, din_len);
}
debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp,
dinp ? *dinp : NULL);
if (dinp) {
/* If we have any data to return, it must be 64bit-aligned */
assert(len <= 0 || !((uintptr_t)din & 7));
*dinp = din;
}
return len;
}
/**
* Send a command to the CROS-EC device and return the reply.
*
* The device's internal input/output buffers are used.
*
* @param dev CROS-EC device
* @param cmd Command to send (EC_CMD_...)
* @param cmd_version Version of command to send (EC_VER_...)
* @param dout Output data (may be NULL If dout_len=0)
* @param dout_len Size of output data in bytes
* @param din Response data (may be NULL If din_len=0).
* It not NULL, it is a place for ec_command() to copy the
* data to.
* @param din_len Maximum size of response in bytes
* @return number of bytes in response, or -ve on error
*/
static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
const void *dout, int dout_len,
void *din, int din_len)
{
uint8_t *in_buffer;
int len;
assert((din_len == 0) || din);
len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
&in_buffer, din_len);
if (len > 0) {
/*
* If we were asked to put it somewhere, do so, otherwise just
* disregard the result.
*/
if (din && in_buffer) {
assert(len <= din_len);
memmove(din, in_buffer, len);
}
}
return len;
}
int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
if (ec_command(cdev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
sizeof(scan->data)) != sizeof(scan->data))
return -1;
return 0;
}
int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
{
struct ec_response_get_version *r;
if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
(uint8_t **)&r, sizeof(*r)) != sizeof(*r))
if (maxlen > (int)sizeof(r->version_string_ro))
maxlen = sizeof(r->version_string_ro);
switch (r->current_image) {
case EC_IMAGE_RO:
memcpy(id, r->version_string_ro, maxlen);
break;
case EC_IMAGE_RW:
memcpy(id, r->version_string_rw, maxlen);
break;
default:
return -1;
}
id[maxlen - 1] = '\0';
return 0;
}
int cros_ec_read_version(struct cros_ec_dev *dev,
struct ec_response_get_version **versionp)
{
if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
(uint8_t **)versionp, sizeof(**versionp))
!= sizeof(**versionp))
return -1;
return 0;
}
int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
{
if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
(uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
return -1;
return 0;
}
int cros_ec_read_current_image(struct cros_ec_dev *dev,
enum ec_current_image *image)
{
struct ec_response_get_version *r;
if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
(uint8_t **)&r, sizeof(*r)) != sizeof(*r))
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
return -1;
*image = r->current_image;
return 0;
}
static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
struct ec_response_vboot_hash *hash)
{
struct ec_params_vboot_hash p;
ulong start;
start = get_timer(0);
while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
mdelay(50); /* Insert some reasonable delay */
p.cmd = EC_VBOOT_HASH_GET;
if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
hash, sizeof(*hash)) < 0)
return -1;
if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
return -EC_RES_TIMEOUT;
}
}
return 0;
}
int cros_ec_read_hash(struct cros_ec_dev *dev,
struct ec_response_vboot_hash *hash)
{
struct ec_params_vboot_hash p;
int rv;
p.cmd = EC_VBOOT_HASH_GET;
if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
hash, sizeof(*hash)) < 0)
return -1;
/* If the EC is busy calculating the hash, fidget until it's done. */
rv = cros_ec_wait_on_hash_done(dev, hash);
if (rv)
return rv;
/* If the hash is valid, we're done. Otherwise, we have to kick it off
* again and wait for it to complete. Note that we explicitly assume
* that hashing zero bytes is always wrong, even though that would
* produce a valid hash value. */
if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
return 0;
debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
__func__, hash->status, hash->size);
p.cmd = EC_VBOOT_HASH_START;
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
p.nonce_size = 0;
p.offset = EC_VBOOT_HASH_OFFSET_RW;
if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
hash, sizeof(*hash)) < 0)
return -1;
rv = cros_ec_wait_on_hash_done(dev, hash);
if (rv)
return rv;
debug("%s: hash done\n", __func__);
return 0;
}
static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
{
struct ec_params_vboot_hash p;
struct ec_response_vboot_hash *hash;
/* We don't have an explict command for the EC to discard its current
* hash value, so we'll just tell it to calculate one that we know is
* wrong (we claim that hashing zero bytes is always invalid).
*/
p.cmd = EC_VBOOT_HASH_RECALC;
p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
p.nonce_size = 0;
p.offset = 0;
p.size = 0;
debug("%s:\n", __func__);
if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
(uint8_t **)&hash, sizeof(*hash)) < 0)
return -1;
/* No need to wait for it to finish */
return 0;
}
int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
uint8_t flags)
{
struct ec_params_reboot_ec p;
p.cmd = cmd;
p.flags = flags;
if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
< 0)
return -1;
if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
/*
* EC reboot will take place immediately so delay to allow it
* to complete. Note that some reboot types (EC_REBOOT_COLD)
* will reboot the AP as well, in which case we won't actually
* get to this point.
*/
/*
* TODO(rspangler@chromium.org): Would be nice if we had a
* better way to determine when the reboot is complete. Could
* we poll a memory-mapped LPC value?
*/
udelay(50000);
}
return 0;
}
int cros_ec_interrupt_pending(struct udevice *dev)
struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
if (!dm_gpio_is_valid(&cdev->ec_int))
return -ENOENT;
return dm_gpio_get_value(&cdev->ec_int);
int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
sizeof(*info)) != sizeof(*info))
return -1;
return 0;
}
int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
{
struct ec_response_host_event_mask *resp;
/*
* Use the B copy of the event flags, because the main copy is already
* used by ACPI/SMI.
*/
if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
(uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
return -1;
if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
return -1;
*events_ptr = resp->mask;
return 0;
}
int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
{
struct ec_params_host_event_mask params;
params.mask = events;
/*
* Use the B copy of the event flags, so it affects the data returned
* by cros_ec_get_host_events().
*/
if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
¶ms, sizeof(params), NULL, 0) < 0)
return -1;
return 0;
}
int cros_ec_flash_protect(struct cros_ec_dev *dev,
uint32_t set_mask, uint32_t set_flags,
struct ec_response_flash_protect *resp)
{
struct ec_params_flash_protect params;
params.mask = set_mask;
params.flags = set_flags;
if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
¶ms, sizeof(params),
resp, sizeof(*resp)) != sizeof(*resp))
return -1;
return 0;
}
static int cros_ec_check_version(struct cros_ec_dev *dev)
{
struct ec_params_hello req;
struct ec_response_hello *resp;
struct dm_cros_ec_ops *ops;
int ret;
ops = dm_cros_ec_get_ops(dev->dev);
if (ops->check_version) {
ret = ops->check_version(dev->dev);
if (ret)
return ret;
}
/*
* TODO(sjg@chromium.org).
* There is a strange oddity here with the EC. We could just ignore
* the response, i.e. pass the last two parameters as NULL and 0.
* In this case we won't read back very many bytes from the EC.
* On the I2C bus the EC gets upset about this and will try to send
* the bytes anyway. This means that we will have to wait for that
* to complete before continuing with a new EC command.
*
* This problem is probably unique to the I2C bus.
*
* So for now, just read all the data anyway.
*/
/* Try sending a version 3 packet */
dev->protocol_version = 3;
if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
(uint8_t **)&resp, sizeof(*resp)) > 0) {
return 0;
}
/* Try sending a version 2 packet */
dev->protocol_version = 2;
if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
(uint8_t **)&resp, sizeof(*resp)) > 0) {
/*
* Fail if we're still here, since the EC doesn't understand any
* protcol version we speak. Version 1 interface without command
* version is no longer supported, and we don't know about any new
* protocol versions.
*/
dev->protocol_version = 0;
printf("%s: ERROR: old EC interface not supported\n", __func__);
return -1;
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
}
int cros_ec_test(struct cros_ec_dev *dev)
{
struct ec_params_hello req;
struct ec_response_hello *resp;
req.in_data = 0x12345678;
if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
(uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
printf("ec_command_inptr() returned error\n");
return -1;
}
if (resp->out_data != req.in_data + 0x01020304) {
printf("Received invalid handshake %x\n", resp->out_data);
return -1;
}
return 0;
}
int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
uint32_t *offset, uint32_t *size)
{
struct ec_params_flash_region_info p;
struct ec_response_flash_region_info *r;
int ret;
p.region = region;
ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
EC_VER_FLASH_REGION_INFO,
&p, sizeof(p), (uint8_t **)&r, sizeof(*r));
if (ret != sizeof(*r))
return -1;
if (offset)
*offset = r->offset;
if (size)
*size = r->size;
return 0;
}
int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
{
struct ec_params_flash_erase p;
p.offset = offset;
p.size = size;
return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
NULL, 0);
}
/**
* Write a single block to the flash
*
* Write a block of data to the EC flash. The size must not exceed the flash
* write block size which you can obtain from cros_ec_flash_write_burst_size().
*
* The offset starts at 0. You can obtain the region information from
* cros_ec_flash_offset() to find out where to write for a particular region.
*
* Attempting to write to the region where the EC is currently running from
* will result in an error.
*
* @param dev CROS-EC device
* @param data Pointer to data buffer to write
* @param offset Offset within flash to write to.
* @param size Number of bytes to write
* @return 0 if ok, -1 on error
*/
static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
const uint8_t *data, uint32_t offset, uint32_t size)
{
struct ec_params_flash_write p;
p.offset = offset;
p.size = size;
assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
memcpy(&p + 1, data, p.size);
return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
&p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
}
/**
* Return optimal flash write burst size
*/
static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
{
return EC_FLASH_WRITE_VER0_SIZE;
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
}
/**
* Check if a block of data is erased (all 0xff)
*
* This function is useful when dealing with flash, for checking whether a
* data block is erased and thus does not need to be programmed.
*
* @param data Pointer to data to check (must be word-aligned)
* @param size Number of bytes to check (must be word-aligned)
* @return 0 if erased, non-zero if any word is not erased
*/
static int cros_ec_data_is_erased(const uint32_t *data, int size)
{
assert(!(size & 3));
size /= sizeof(uint32_t);
for (; size > 0; size -= 4, data++)
if (*data != -1U)
return 0;
return 1;
}
int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
uint32_t offset, uint32_t size)
{
uint32_t burst = cros_ec_flash_write_burst_size(dev);
uint32_t end, off;
int ret;
/*
* TODO: round up to the nearest multiple of write size. Can get away
* without that on link right now because its write size is 4 bytes.
*/
end = offset + size;
for (off = offset; off < end; off += burst, data += burst) {
uint32_t todo;
/* If the data is empty, there is no point in programming it */
todo = min(end - off, burst);
if (dev->optimise_flash_write &&
cros_ec_data_is_erased((uint32_t *)data, todo))
continue;
ret = cros_ec_flash_write_block(dev, data, off, todo);
if (ret)
return ret;
}
return 0;
}
/**
* Read a single block from the flash
*
* Read a block of data from the EC flash. The size must not exceed the flash
* write block size which you can obtain from cros_ec_flash_write_burst_size().
*
* The offset starts at 0. You can obtain the region information from
* cros_ec_flash_offset() to find out where to read for a particular region.
*
* @param dev CROS-EC device
* @param data Pointer to data buffer to read into
* @param offset Offset within flash to read from
* @param size Number of bytes to read
* @return 0 if ok, -1 on error
*/
static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
uint32_t offset, uint32_t size)
{
struct ec_params_flash_read p;
p.offset = offset;
p.size = size;
return ec_command(dev, EC_CMD_FLASH_READ, 0,
&p, sizeof(p), data, size) >= 0 ? 0 : -1;
}
int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
uint32_t size)
{
uint32_t burst = cros_ec_flash_write_burst_size(dev);
uint32_t end, off;
int ret;
end = offset + size;
for (off = offset; off < end; off += burst, data += burst) {
ret = cros_ec_flash_read_block(dev, data, off,
min(end - off, burst));
if (ret)
return ret;
}
return 0;
}
int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
const uint8_t *image, int image_size)
{
uint32_t rw_offset, rw_size;
int ret;
if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
return -1;
if (image_size > (int)rw_size)
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
return -1;
/* Invalidate the existing hash, just in case the AP reboots
* unexpectedly during the update. If that happened, the EC RW firmware
* would be invalid, but the EC would still have the original hash.
*/
ret = cros_ec_invalidate_hash(dev);
if (ret)
return ret;
/*
* Erase the entire RW section, so that the EC doesn't see any garbage
* past the new image if it's smaller than the current image.
*
* TODO: could optimize this to erase just the current image, since
* presumably everything past that is 0xff's. But would still need to
* round up to the nearest multiple of erase size.
*/
ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
if (ret)
return ret;
/* Write the image */
ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
if (ret)
return ret;
return 0;
}
int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
{
struct ec_params_vbnvcontext p;
int len;
p.op = EC_VBNV_CONTEXT_OP_READ;
len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
&p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
if (len < EC_VBNV_BLOCK_SIZE)
return -1;
return 0;
}
int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
{
struct ec_params_vbnvcontext p;
int len;
p.op = EC_VBNV_CONTEXT_OP_WRITE;
memcpy(p.block, block, sizeof(p.block));
len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
&p, sizeof(p), NULL, 0);
if (len < 0)
return -1;
return 0;
}
int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
struct ec_params_ldo_set params;
params.index = index;
params.state = state;
if (ec_command_inptr(cdev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params),
NULL, 0))
int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
struct ec_params_ldo_get params;
struct ec_response_ldo_get *resp;
params.index = index;
if (ec_command_inptr(cdev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params),
(uint8_t **)&resp, sizeof(*resp)) !=
sizeof(*resp))
return -1;
*state = resp->state;
return 0;
}
int cros_ec_register(struct udevice *dev)
struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
const void *blob = gd->fdt_blob;
int node = dev->of_offset;
gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
GPIOD_IS_IN);
cdev->optimise_flash_write = fdtdec_get_bool(blob, node,
"optimise-flash-write");
if (cros_ec_check_version(cdev)) {
debug("%s: Could not detect CROS-EC version\n", __func__);
return -CROS_EC_ERR_CHECK_VERSION;
}
if (cros_ec_read_id(cdev, id, sizeof(id))) {
debug("%s: Could not read KBC ID\n", __func__);
return -CROS_EC_ERR_READ_ID;
}
/* Remember this device for use by the cros_ec command */
debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
cdev->protocol_version, id);
int cros_ec_decode_region(int argc, char * const argv[])