blob: 33b9390d3e31f6c69b61fe2d566624bf971f4587 [file] [log] [blame]
Hung-ying Tyan88364382013-05-15 18:27:28 +08001/*
2 * Chromium OS cros_ec driver
3 *
4 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyan88364382013-05-15 18:27:28 +08005 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Hung-ying Tyan88364382013-05-15 18:27:28 +08007 */
8
9/*
Simon Glass836bb6e2014-02-27 13:26:07 -070010 * This is the interface to the Chrome OS EC. It provides keyboard functions,
11 * power control and battery management. Quite a few other functions are
12 * provided to enable the EC software to be updated, talk to the EC's I2C bus
13 * and store a small amount of data in a memory which persists while the EC
14 * is not reset.
Hung-ying Tyan88364382013-05-15 18:27:28 +080015 */
16
17#include <common.h>
18#include <command.h>
19#include <i2c.h>
20#include <cros_ec.h>
21#include <fdtdec.h>
22#include <malloc.h>
23#include <spi.h>
24#include <asm/io.h>
25#include <asm-generic/gpio.h>
26
27#ifdef DEBUG_TRACE
28#define debug_trace(fmt, b...) debug(fmt, #b)
29#else
30#define debug_trace(fmt, b...)
31#endif
32
33enum {
34 /* Timeout waiting for a flash erase command to complete */
35 CROS_EC_CMD_TIMEOUT_MS = 5000,
36 /* Timeout waiting for a synchronous hash to be recomputed */
37 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
38};
39
40static struct cros_ec_dev static_dev, *last_dev;
41
42DECLARE_GLOBAL_DATA_PTR;
43
44/* Note: depends on enum ec_current_image */
45static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
46
47void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
48{
49#ifdef DEBUG
50 int i;
51
52 printf("%s: ", name);
53 if (cmd != -1)
54 printf("cmd=%#x: ", cmd);
55 for (i = 0; i < len; i++)
56 printf("%02x ", data[i]);
57 printf("\n");
58#endif
59}
60
61/*
62 * Calculate a simple 8-bit checksum of a data block
63 *
64 * @param data Data block to checksum
65 * @param size Size of data block in bytes
66 * @return checksum value (0 to 255)
67 */
68int cros_ec_calc_checksum(const uint8_t *data, int size)
69{
70 int csum, i;
71
72 for (i = csum = 0; i < size; i++)
73 csum += data[i];
74 return csum & 0xff;
75}
76
77static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
78 const void *dout, int dout_len,
79 uint8_t **dinp, int din_len)
80{
81 int ret;
82
83 switch (dev->interface) {
84#ifdef CONFIG_CROS_EC_SPI
85 case CROS_EC_IF_SPI:
86 ret = cros_ec_spi_command(dev, cmd, cmd_version,
87 (const uint8_t *)dout, dout_len,
88 dinp, din_len);
89 break;
90#endif
91#ifdef CONFIG_CROS_EC_I2C
92 case CROS_EC_IF_I2C:
93 ret = cros_ec_i2c_command(dev, cmd, cmd_version,
94 (const uint8_t *)dout, dout_len,
95 dinp, din_len);
96 break;
97#endif
98#ifdef CONFIG_CROS_EC_LPC
99 case CROS_EC_IF_LPC:
100 ret = cros_ec_lpc_command(dev, cmd, cmd_version,
101 (const uint8_t *)dout, dout_len,
102 dinp, din_len);
103 break;
104#endif
105 case CROS_EC_IF_NONE:
106 default:
107 ret = -1;
108 }
109
110 return ret;
111}
112
113/**
114 * Send a command to the CROS-EC device and return the reply.
115 *
116 * The device's internal input/output buffers are used.
117 *
118 * @param dev CROS-EC device
119 * @param cmd Command to send (EC_CMD_...)
120 * @param cmd_version Version of command to send (EC_VER_...)
121 * @param dout Output data (may be NULL If dout_len=0)
122 * @param dout_len Size of output data in bytes
123 * @param dinp Response data (may be NULL If din_len=0).
124 * If not NULL, it will be updated to point to the data
125 * and will always be double word aligned (64-bits)
126 * @param din_len Maximum size of response in bytes
127 * @return number of bytes in response, or -1 on error
128 */
129static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
130 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
131 int din_len)
132{
133 uint8_t *din;
134 int len;
135
Hung-ying Tyan88364382013-05-15 18:27:28 +0800136 len = send_command(dev, cmd, cmd_version, dout, dout_len,
137 &din, din_len);
138
139 /* If the command doesn't complete, wait a while */
140 if (len == -EC_RES_IN_PROGRESS) {
141 struct ec_response_get_comms_status *resp;
142 ulong start;
143
144 /* Wait for command to complete */
145 start = get_timer(0);
146 do {
147 int ret;
148
149 mdelay(50); /* Insert some reasonable delay */
150 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
151 NULL, 0,
152 (uint8_t **)&resp, sizeof(*resp));
153 if (ret < 0)
154 return ret;
155
156 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
157 debug("%s: Command %#02x timeout\n",
158 __func__, cmd);
159 return -EC_RES_TIMEOUT;
160 }
161 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
162
163 /* OK it completed, so read the status response */
164 /* not sure why it was 0 for the last argument */
165 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
166 NULL, 0, &din, din_len);
167 }
168
169 debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp, *dinp);
170 if (dinp) {
171 /* If we have any data to return, it must be 64bit-aligned */
172 assert(len <= 0 || !((uintptr_t)din & 7));
173 *dinp = din;
174 }
175
176 return len;
177}
178
179/**
180 * Send a command to the CROS-EC device and return the reply.
181 *
182 * The device's internal input/output buffers are used.
183 *
184 * @param dev CROS-EC device
185 * @param cmd Command to send (EC_CMD_...)
186 * @param cmd_version Version of command to send (EC_VER_...)
187 * @param dout Output data (may be NULL If dout_len=0)
188 * @param dout_len Size of output data in bytes
189 * @param din Response data (may be NULL If din_len=0).
190 * It not NULL, it is a place for ec_command() to copy the
191 * data to.
192 * @param din_len Maximum size of response in bytes
193 * @return number of bytes in response, or -1 on error
194 */
195static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
196 const void *dout, int dout_len,
197 void *din, int din_len)
198{
199 uint8_t *in_buffer;
200 int len;
201
202 assert((din_len == 0) || din);
203 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
204 &in_buffer, din_len);
205 if (len > 0) {
206 /*
207 * If we were asked to put it somewhere, do so, otherwise just
208 * disregard the result.
209 */
210 if (din && in_buffer) {
211 assert(len <= din_len);
212 memmove(din, in_buffer, len);
213 }
214 }
215 return len;
216}
217
218int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
219{
Simon Glass836bb6e2014-02-27 13:26:07 -0700220 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
Hung-ying Tyan88364382013-05-15 18:27:28 +0800221 sizeof(scan->data)) < sizeof(scan->data))
222 return -1;
223
224 return 0;
225}
226
227int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
228{
229 struct ec_response_get_version *r;
230
231 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
232 (uint8_t **)&r, sizeof(*r)) < sizeof(*r))
233 return -1;
234
235 if (maxlen > sizeof(r->version_string_ro))
236 maxlen = sizeof(r->version_string_ro);
237
238 switch (r->current_image) {
239 case EC_IMAGE_RO:
240 memcpy(id, r->version_string_ro, maxlen);
241 break;
242 case EC_IMAGE_RW:
243 memcpy(id, r->version_string_rw, maxlen);
244 break;
245 default:
246 return -1;
247 }
248
249 id[maxlen - 1] = '\0';
250 return 0;
251}
252
253int cros_ec_read_version(struct cros_ec_dev *dev,
254 struct ec_response_get_version **versionp)
255{
256 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
257 (uint8_t **)versionp, sizeof(**versionp))
258 < sizeof(**versionp))
259 return -1;
260
261 return 0;
262}
263
264int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
265{
266 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
Simon Glass836bb6e2014-02-27 13:26:07 -0700267 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800268 return -1;
269
270 return 0;
271}
272
273int cros_ec_read_current_image(struct cros_ec_dev *dev,
274 enum ec_current_image *image)
275{
276 struct ec_response_get_version *r;
277
278 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
279 (uint8_t **)&r, sizeof(*r)) < sizeof(*r))
280 return -1;
281
282 *image = r->current_image;
283 return 0;
284}
285
286static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
287 struct ec_response_vboot_hash *hash)
288{
289 struct ec_params_vboot_hash p;
290 ulong start;
291
292 start = get_timer(0);
293 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
294 mdelay(50); /* Insert some reasonable delay */
295
296 p.cmd = EC_VBOOT_HASH_GET;
297 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
298 hash, sizeof(*hash)) < 0)
299 return -1;
300
301 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
302 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
303 return -EC_RES_TIMEOUT;
304 }
305 }
306 return 0;
307}
308
309
310int cros_ec_read_hash(struct cros_ec_dev *dev,
311 struct ec_response_vboot_hash *hash)
312{
313 struct ec_params_vboot_hash p;
314 int rv;
315
316 p.cmd = EC_VBOOT_HASH_GET;
317 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
318 hash, sizeof(*hash)) < 0)
319 return -1;
320
321 /* If the EC is busy calculating the hash, fidget until it's done. */
322 rv = cros_ec_wait_on_hash_done(dev, hash);
323 if (rv)
324 return rv;
325
326 /* If the hash is valid, we're done. Otherwise, we have to kick it off
327 * again and wait for it to complete. Note that we explicitly assume
328 * that hashing zero bytes is always wrong, even though that would
329 * produce a valid hash value. */
330 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
331 return 0;
332
333 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
334 __func__, hash->status, hash->size);
335
Simon Glass836bb6e2014-02-27 13:26:07 -0700336 p.cmd = EC_VBOOT_HASH_START;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800337 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
338 p.nonce_size = 0;
339 p.offset = EC_VBOOT_HASH_OFFSET_RW;
340
341 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
342 hash, sizeof(*hash)) < 0)
343 return -1;
344
345 rv = cros_ec_wait_on_hash_done(dev, hash);
346 if (rv)
347 return rv;
348
349 debug("%s: hash done\n", __func__);
350
351 return 0;
352}
353
354static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
355{
356 struct ec_params_vboot_hash p;
357 struct ec_response_vboot_hash *hash;
358
359 /* We don't have an explict command for the EC to discard its current
360 * hash value, so we'll just tell it to calculate one that we know is
361 * wrong (we claim that hashing zero bytes is always invalid).
362 */
363 p.cmd = EC_VBOOT_HASH_RECALC;
364 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
365 p.nonce_size = 0;
366 p.offset = 0;
367 p.size = 0;
368
369 debug("%s:\n", __func__);
370
371 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
372 (uint8_t **)&hash, sizeof(*hash)) < 0)
373 return -1;
374
375 /* No need to wait for it to finish */
376 return 0;
377}
378
379int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
380 uint8_t flags)
381{
382 struct ec_params_reboot_ec p;
383
384 p.cmd = cmd;
385 p.flags = flags;
386
387 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
388 < 0)
389 return -1;
390
391 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
392 /*
393 * EC reboot will take place immediately so delay to allow it
394 * to complete. Note that some reboot types (EC_REBOOT_COLD)
395 * will reboot the AP as well, in which case we won't actually
396 * get to this point.
397 */
398 /*
399 * TODO(rspangler@chromium.org): Would be nice if we had a
400 * better way to determine when the reboot is complete. Could
401 * we poll a memory-mapped LPC value?
402 */
403 udelay(50000);
404 }
405
406 return 0;
407}
408
409int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
410{
411 /* no interrupt support : always poll */
412 if (!fdt_gpio_isvalid(&dev->ec_int))
413 return 1;
414
415 return !gpio_get_value(dev->ec_int.gpio);
416}
417
Simon Glass836bb6e2014-02-27 13:26:07 -0700418int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800419{
Simon Glass836bb6e2014-02-27 13:26:07 -0700420 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
421 sizeof(*info)) < sizeof(*info))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800422 return -1;
423
424 return 0;
425}
426
427int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
428{
429 struct ec_response_host_event_mask *resp;
430
431 /*
432 * Use the B copy of the event flags, because the main copy is already
433 * used by ACPI/SMI.
434 */
435 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
436 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
437 return -1;
438
439 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
440 return -1;
441
442 *events_ptr = resp->mask;
443 return 0;
444}
445
446int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
447{
448 struct ec_params_host_event_mask params;
449
450 params.mask = events;
451
452 /*
453 * Use the B copy of the event flags, so it affects the data returned
454 * by cros_ec_get_host_events().
455 */
456 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
457 &params, sizeof(params), NULL, 0) < 0)
458 return -1;
459
460 return 0;
461}
462
463int cros_ec_flash_protect(struct cros_ec_dev *dev,
464 uint32_t set_mask, uint32_t set_flags,
465 struct ec_response_flash_protect *resp)
466{
467 struct ec_params_flash_protect params;
468
469 params.mask = set_mask;
470 params.flags = set_flags;
471
472 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
473 &params, sizeof(params),
474 resp, sizeof(*resp)) < sizeof(*resp))
475 return -1;
476
477 return 0;
478}
479
480static int cros_ec_check_version(struct cros_ec_dev *dev)
481{
482 struct ec_params_hello req;
483 struct ec_response_hello *resp;
484
485#ifdef CONFIG_CROS_EC_LPC
486 /* LPC has its own way of doing this */
487 if (dev->interface == CROS_EC_IF_LPC)
488 return cros_ec_lpc_check_version(dev);
489#endif
490
491 /*
492 * TODO(sjg@chromium.org).
493 * There is a strange oddity here with the EC. We could just ignore
494 * the response, i.e. pass the last two parameters as NULL and 0.
495 * In this case we won't read back very many bytes from the EC.
496 * On the I2C bus the EC gets upset about this and will try to send
497 * the bytes anyway. This means that we will have to wait for that
498 * to complete before continuing with a new EC command.
499 *
500 * This problem is probably unique to the I2C bus.
501 *
502 * So for now, just read all the data anyway.
503 */
Randall Spanglere8c12662014-02-27 13:26:08 -0700504
505 /* Try sending a version 2 packet */
506 dev->protocol_version = 2;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800507 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
508 (uint8_t **)&resp, sizeof(*resp)) > 0) {
Randall Spanglere8c12662014-02-27 13:26:08 -0700509 return 0;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800510 }
511
Randall Spanglere8c12662014-02-27 13:26:08 -0700512 /*
513 * Fail if we're still here, since the EC doesn't understand any
514 * protcol version we speak. Version 1 interface without command
515 * version is no longer supported, and we don't know about any new
516 * protocol versions.
517 */
518 dev->protocol_version = 0;
519 printf("%s: ERROR: old EC interface not supported\n", __func__);
520 return -1;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800521}
522
523int cros_ec_test(struct cros_ec_dev *dev)
524{
525 struct ec_params_hello req;
526 struct ec_response_hello *resp;
527
528 req.in_data = 0x12345678;
529 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
530 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
531 printf("ec_command_inptr() returned error\n");
532 return -1;
533 }
534 if (resp->out_data != req.in_data + 0x01020304) {
535 printf("Received invalid handshake %x\n", resp->out_data);
536 return -1;
537 }
538
539 return 0;
540}
541
542int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
543 uint32_t *offset, uint32_t *size)
544{
545 struct ec_params_flash_region_info p;
546 struct ec_response_flash_region_info *r;
547 int ret;
548
549 p.region = region;
550 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
551 EC_VER_FLASH_REGION_INFO,
552 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
553 if (ret != sizeof(*r))
554 return -1;
555
556 if (offset)
557 *offset = r->offset;
558 if (size)
559 *size = r->size;
560
561 return 0;
562}
563
564int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
565{
566 struct ec_params_flash_erase p;
567
568 p.offset = offset;
569 p.size = size;
570 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
571 NULL, 0);
572}
573
574/**
575 * Write a single block to the flash
576 *
577 * Write a block of data to the EC flash. The size must not exceed the flash
578 * write block size which you can obtain from cros_ec_flash_write_burst_size().
579 *
580 * The offset starts at 0. You can obtain the region information from
581 * cros_ec_flash_offset() to find out where to write for a particular region.
582 *
583 * Attempting to write to the region where the EC is currently running from
584 * will result in an error.
585 *
586 * @param dev CROS-EC device
587 * @param data Pointer to data buffer to write
588 * @param offset Offset within flash to write to.
589 * @param size Number of bytes to write
590 * @return 0 if ok, -1 on error
591 */
592static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
593 const uint8_t *data, uint32_t offset, uint32_t size)
594{
595 struct ec_params_flash_write p;
596
597 p.offset = offset;
598 p.size = size;
Simon Glass836bb6e2014-02-27 13:26:07 -0700599 assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
600 memcpy(&p + 1, data, p.size);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800601
602 return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
603 &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
604}
605
606/**
607 * Return optimal flash write burst size
608 */
609static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
610{
Simon Glass836bb6e2014-02-27 13:26:07 -0700611 return EC_FLASH_WRITE_VER0_SIZE;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800612}
613
614/**
615 * Check if a block of data is erased (all 0xff)
616 *
617 * This function is useful when dealing with flash, for checking whether a
618 * data block is erased and thus does not need to be programmed.
619 *
620 * @param data Pointer to data to check (must be word-aligned)
621 * @param size Number of bytes to check (must be word-aligned)
622 * @return 0 if erased, non-zero if any word is not erased
623 */
624static int cros_ec_data_is_erased(const uint32_t *data, int size)
625{
626 assert(!(size & 3));
627 size /= sizeof(uint32_t);
628 for (; size > 0; size -= 4, data++)
629 if (*data != -1U)
630 return 0;
631
632 return 1;
633}
634
635int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
636 uint32_t offset, uint32_t size)
637{
638 uint32_t burst = cros_ec_flash_write_burst_size(dev);
639 uint32_t end, off;
640 int ret;
641
642 /*
643 * TODO: round up to the nearest multiple of write size. Can get away
644 * without that on link right now because its write size is 4 bytes.
645 */
646 end = offset + size;
647 for (off = offset; off < end; off += burst, data += burst) {
648 uint32_t todo;
649
650 /* If the data is empty, there is no point in programming it */
651 todo = min(end - off, burst);
652 if (dev->optimise_flash_write &&
653 cros_ec_data_is_erased((uint32_t *)data, todo))
654 continue;
655
656 ret = cros_ec_flash_write_block(dev, data, off, todo);
657 if (ret)
658 return ret;
659 }
660
661 return 0;
662}
663
664/**
665 * Read a single block from the flash
666 *
667 * Read a block of data from the EC flash. The size must not exceed the flash
668 * write block size which you can obtain from cros_ec_flash_write_burst_size().
669 *
670 * The offset starts at 0. You can obtain the region information from
671 * cros_ec_flash_offset() to find out where to read for a particular region.
672 *
673 * @param dev CROS-EC device
674 * @param data Pointer to data buffer to read into
675 * @param offset Offset within flash to read from
676 * @param size Number of bytes to read
677 * @return 0 if ok, -1 on error
678 */
679static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
680 uint32_t offset, uint32_t size)
681{
682 struct ec_params_flash_read p;
683
684 p.offset = offset;
685 p.size = size;
686
687 return ec_command(dev, EC_CMD_FLASH_READ, 0,
688 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
689}
690
691int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
692 uint32_t size)
693{
694 uint32_t burst = cros_ec_flash_write_burst_size(dev);
695 uint32_t end, off;
696 int ret;
697
698 end = offset + size;
699 for (off = offset; off < end; off += burst, data += burst) {
700 ret = cros_ec_flash_read_block(dev, data, off,
701 min(end - off, burst));
702 if (ret)
703 return ret;
704 }
705
706 return 0;
707}
708
709int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
710 const uint8_t *image, int image_size)
711{
712 uint32_t rw_offset, rw_size;
713 int ret;
714
715 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
716 return -1;
717 if (image_size > rw_size)
718 return -1;
719
720 /* Invalidate the existing hash, just in case the AP reboots
721 * unexpectedly during the update. If that happened, the EC RW firmware
722 * would be invalid, but the EC would still have the original hash.
723 */
724 ret = cros_ec_invalidate_hash(dev);
725 if (ret)
726 return ret;
727
728 /*
729 * Erase the entire RW section, so that the EC doesn't see any garbage
730 * past the new image if it's smaller than the current image.
731 *
732 * TODO: could optimize this to erase just the current image, since
733 * presumably everything past that is 0xff's. But would still need to
734 * round up to the nearest multiple of erase size.
735 */
736 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
737 if (ret)
738 return ret;
739
740 /* Write the image */
741 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
742 if (ret)
743 return ret;
744
745 return 0;
746}
747
748int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
749{
750 struct ec_params_vbnvcontext p;
751 int len;
752
753 p.op = EC_VBNV_CONTEXT_OP_READ;
754
755 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
756 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
757 if (len < EC_VBNV_BLOCK_SIZE)
758 return -1;
759
760 return 0;
761}
762
763int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
764{
765 struct ec_params_vbnvcontext p;
766 int len;
767
768 p.op = EC_VBNV_CONTEXT_OP_WRITE;
769 memcpy(p.block, block, sizeof(p.block));
770
771 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
772 &p, sizeof(p), NULL, 0);
773 if (len < 0)
774 return -1;
775
776 return 0;
777}
778
779int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
780{
781 struct ec_params_ldo_set params;
782
783 params.index = index;
784 params.state = state;
785
786 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
787 &params, sizeof(params),
788 NULL, 0))
789 return -1;
790
791 return 0;
792}
793
794int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
795{
796 struct ec_params_ldo_get params;
797 struct ec_response_ldo_get *resp;
798
799 params.index = index;
800
801 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
802 &params, sizeof(params),
803 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
804 return -1;
805
806 *state = resp->state;
807
808 return 0;
809}
810
811/**
Simon Glass836bb6e2014-02-27 13:26:07 -0700812 * Decode EC interface details from the device tree and allocate a suitable
813 * device.
Hung-ying Tyan88364382013-05-15 18:27:28 +0800814 *
815 * @param blob Device tree blob
816 * @param node Node to decode from
817 * @param devp Returns a pointer to the new allocated device
818 * @return 0 if ok, -1 on error
819 */
820static int cros_ec_decode_fdt(const void *blob, int node,
821 struct cros_ec_dev **devp)
822{
823 enum fdt_compat_id compat;
824 struct cros_ec_dev *dev;
825 int parent;
826
827 /* See what type of parent we are inside (this is expensive) */
828 parent = fdt_parent_offset(blob, node);
829 if (parent < 0) {
830 debug("%s: Cannot find node parent\n", __func__);
831 return -1;
832 }
833
834 dev = &static_dev;
835 dev->node = node;
836 dev->parent_node = parent;
837
838 compat = fdtdec_lookup(blob, parent);
839 switch (compat) {
840#ifdef CONFIG_CROS_EC_SPI
841 case COMPAT_SAMSUNG_EXYNOS_SPI:
842 dev->interface = CROS_EC_IF_SPI;
843 if (cros_ec_spi_decode_fdt(dev, blob))
844 return -1;
845 break;
846#endif
847#ifdef CONFIG_CROS_EC_I2C
848 case COMPAT_SAMSUNG_S3C2440_I2C:
849 dev->interface = CROS_EC_IF_I2C;
850 if (cros_ec_i2c_decode_fdt(dev, blob))
851 return -1;
852 break;
853#endif
854#ifdef CONFIG_CROS_EC_LPC
855 case COMPAT_INTEL_LPC:
856 dev->interface = CROS_EC_IF_LPC;
857 break;
858#endif
859 default:
860 debug("%s: Unknown compat id %d\n", __func__, compat);
861 return -1;
862 }
863
864 fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int);
865 dev->optimise_flash_write = fdtdec_get_bool(blob, node,
866 "optimise-flash-write");
867 *devp = dev;
868
869 return 0;
870}
871
872int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
873{
874 char id[MSG_BYTES];
875 struct cros_ec_dev *dev;
876 int node = 0;
877
878 *cros_ecp = NULL;
879 do {
880 node = fdtdec_next_compatible(blob, node,
881 COMPAT_GOOGLE_CROS_EC);
882 if (node < 0) {
883 debug("%s: Node not found\n", __func__);
884 return 0;
885 }
886 } while (!fdtdec_get_is_enabled(blob, node));
887
888 if (cros_ec_decode_fdt(blob, node, &dev)) {
889 debug("%s: Failed to decode device.\n", __func__);
890 return -CROS_EC_ERR_FDT_DECODE;
891 }
892
893 switch (dev->interface) {
894#ifdef CONFIG_CROS_EC_SPI
895 case CROS_EC_IF_SPI:
896 if (cros_ec_spi_init(dev, blob)) {
897 debug("%s: Could not setup SPI interface\n", __func__);
898 return -CROS_EC_ERR_DEV_INIT;
899 }
900 break;
901#endif
902#ifdef CONFIG_CROS_EC_I2C
903 case CROS_EC_IF_I2C:
904 if (cros_ec_i2c_init(dev, blob))
905 return -CROS_EC_ERR_DEV_INIT;
906 break;
907#endif
908#ifdef CONFIG_CROS_EC_LPC
909 case CROS_EC_IF_LPC:
910 if (cros_ec_lpc_init(dev, blob))
911 return -CROS_EC_ERR_DEV_INIT;
912 break;
913#endif
914 case CROS_EC_IF_NONE:
915 default:
916 return 0;
917 }
918
919 /* we will poll the EC interrupt line */
920 fdtdec_setup_gpio(&dev->ec_int);
921 if (fdt_gpio_isvalid(&dev->ec_int))
922 gpio_direction_input(dev->ec_int.gpio);
923
924 if (cros_ec_check_version(dev)) {
925 debug("%s: Could not detect CROS-EC version\n", __func__);
926 return -CROS_EC_ERR_CHECK_VERSION;
927 }
928
929 if (cros_ec_read_id(dev, id, sizeof(id))) {
930 debug("%s: Could not read KBC ID\n", __func__);
931 return -CROS_EC_ERR_READ_ID;
932 }
933
934 /* Remember this device for use by the cros_ec command */
935 last_dev = *cros_ecp = dev;
936 debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
937
938 return 0;
939}
940
Hung-ying Tyan88364382013-05-15 18:27:28 +0800941int cros_ec_decode_region(int argc, char * const argv[])
942{
943 if (argc > 0) {
944 if (0 == strcmp(*argv, "rw"))
945 return EC_FLASH_REGION_RW;
946 else if (0 == strcmp(*argv, "ro"))
947 return EC_FLASH_REGION_RO;
948
949 debug("%s: Invalid region '%s'\n", __func__, *argv);
950 } else {
951 debug("%s: Missing region parameter\n", __func__);
952 }
953
954 return -1;
955}
956
Simon Glassd7f25f32014-02-27 13:26:03 -0700957int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config)
958{
959 int flash_node, node;
960
961 node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
962 if (node < 0) {
963 debug("Failed to find chrome-ec node'\n");
964 return -1;
965 }
966
967 flash_node = fdt_subnode_offset(blob, node, "flash");
968 if (flash_node < 0) {
969 debug("Failed to find flash node\n");
970 return -1;
971 }
972
973 if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
974 &config->flash)) {
975 debug("Failed to decode flash node in chrome-ec'\n");
976 return -1;
977 }
978
979 config->flash_erase_value = fdtdec_get_int(blob, flash_node,
980 "erase-value", -1);
981 for (node = fdt_first_subnode(blob, flash_node); node >= 0;
982 node = fdt_next_subnode(blob, node)) {
983 const char *name = fdt_get_name(blob, node, NULL);
984 enum ec_flash_region region;
985
986 if (0 == strcmp(name, "ro")) {
987 region = EC_FLASH_REGION_RO;
988 } else if (0 == strcmp(name, "rw")) {
989 region = EC_FLASH_REGION_RW;
990 } else if (0 == strcmp(name, "wp-ro")) {
991 region = EC_FLASH_REGION_WP_RO;
992 } else {
993 debug("Unknown EC flash region name '%s'\n", name);
994 return -1;
995 }
996
997 if (fdtdec_read_fmap_entry(blob, node, "reg",
998 &config->region[region])) {
999 debug("Failed to decode flash region in chrome-ec'\n");
1000 return -1;
1001 }
1002 }
1003
1004 return 0;
1005}
1006
Simon Glass1c266b92014-02-27 13:26:06 -07001007#ifdef CONFIG_CMD_CROS_EC
1008
Hung-ying Tyan88364382013-05-15 18:27:28 +08001009/**
1010 * Perform a flash read or write command
1011 *
1012 * @param dev CROS-EC device to read/write
1013 * @param is_write 1 do to a write, 0 to do a read
1014 * @param argc Number of arguments
1015 * @param argv Arguments (2 is region, 3 is address)
1016 * @return 0 for ok, 1 for a usage error or -ve for ec command error
1017 * (negative EC_RES_...)
1018 */
1019static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
1020 char * const argv[])
1021{
1022 uint32_t offset, size = -1U, region_size;
1023 unsigned long addr;
1024 char *endp;
1025 int region;
1026 int ret;
1027
1028 region = cros_ec_decode_region(argc - 2, argv + 2);
1029 if (region == -1)
1030 return 1;
1031 if (argc < 4)
1032 return 1;
1033 addr = simple_strtoul(argv[3], &endp, 16);
1034 if (*argv[3] == 0 || *endp != 0)
1035 return 1;
1036 if (argc > 4) {
1037 size = simple_strtoul(argv[4], &endp, 16);
1038 if (*argv[4] == 0 || *endp != 0)
1039 return 1;
1040 }
1041
1042 ret = cros_ec_flash_offset(dev, region, &offset, &region_size);
1043 if (ret) {
1044 debug("%s: Could not read region info\n", __func__);
1045 return ret;
1046 }
1047 if (size == -1U)
1048 size = region_size;
1049
1050 ret = is_write ?
1051 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
1052 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
1053 if (ret) {
1054 debug("%s: Could not %s region\n", __func__,
1055 is_write ? "write" : "read");
1056 return ret;
1057 }
1058
1059 return 0;
1060}
1061
1062static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1063{
1064 struct cros_ec_dev *dev = last_dev;
1065 const char *cmd;
1066 int ret = 0;
1067
1068 if (argc < 2)
1069 return CMD_RET_USAGE;
1070
1071 cmd = argv[1];
1072 if (0 == strcmp("init", cmd)) {
1073 ret = cros_ec_init(gd->fdt_blob, &dev);
1074 if (ret) {
1075 printf("Could not init cros_ec device (err %d)\n", ret);
1076 return 1;
1077 }
1078 return 0;
1079 }
1080
1081 /* Just use the last allocated device; there should be only one */
1082 if (!last_dev) {
1083 printf("No CROS-EC device available\n");
1084 return 1;
1085 }
1086 if (0 == strcmp("id", cmd)) {
1087 char id[MSG_BYTES];
1088
1089 if (cros_ec_read_id(dev, id, sizeof(id))) {
1090 debug("%s: Could not read KBC ID\n", __func__);
1091 return 1;
1092 }
1093 printf("%s\n", id);
1094 } else if (0 == strcmp("info", cmd)) {
Simon Glass836bb6e2014-02-27 13:26:07 -07001095 struct ec_response_mkbp_info info;
Hung-ying Tyan88364382013-05-15 18:27:28 +08001096
1097 if (cros_ec_info(dev, &info)) {
1098 debug("%s: Could not read KBC info\n", __func__);
1099 return 1;
1100 }
1101 printf("rows = %u\n", info.rows);
1102 printf("cols = %u\n", info.cols);
1103 printf("switches = %#x\n", info.switches);
1104 } else if (0 == strcmp("curimage", cmd)) {
1105 enum ec_current_image image;
1106
1107 if (cros_ec_read_current_image(dev, &image)) {
1108 debug("%s: Could not read KBC image\n", __func__);
1109 return 1;
1110 }
1111 printf("%d\n", image);
1112 } else if (0 == strcmp("hash", cmd)) {
1113 struct ec_response_vboot_hash hash;
1114 int i;
1115
1116 if (cros_ec_read_hash(dev, &hash)) {
1117 debug("%s: Could not read KBC hash\n", __func__);
1118 return 1;
1119 }
1120
1121 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
1122 printf("type: SHA-256\n");
1123 else
1124 printf("type: %d\n", hash.hash_type);
1125
1126 printf("offset: 0x%08x\n", hash.offset);
1127 printf("size: 0x%08x\n", hash.size);
1128
1129 printf("digest: ");
1130 for (i = 0; i < hash.digest_size; i++)
1131 printf("%02x", hash.hash_digest[i]);
1132 printf("\n");
1133 } else if (0 == strcmp("reboot", cmd)) {
1134 int region;
1135 enum ec_reboot_cmd cmd;
1136
1137 if (argc >= 3 && !strcmp(argv[2], "cold"))
1138 cmd = EC_REBOOT_COLD;
1139 else {
1140 region = cros_ec_decode_region(argc - 2, argv + 2);
1141 if (region == EC_FLASH_REGION_RO)
1142 cmd = EC_REBOOT_JUMP_RO;
1143 else if (region == EC_FLASH_REGION_RW)
1144 cmd = EC_REBOOT_JUMP_RW;
1145 else
1146 return CMD_RET_USAGE;
1147 }
1148
1149 if (cros_ec_reboot(dev, cmd, 0)) {
1150 debug("%s: Could not reboot KBC\n", __func__);
1151 return 1;
1152 }
1153 } else if (0 == strcmp("events", cmd)) {
1154 uint32_t events;
1155
1156 if (cros_ec_get_host_events(dev, &events)) {
1157 debug("%s: Could not read host events\n", __func__);
1158 return 1;
1159 }
1160 printf("0x%08x\n", events);
1161 } else if (0 == strcmp("clrevents", cmd)) {
1162 uint32_t events = 0x7fffffff;
1163
1164 if (argc >= 3)
1165 events = simple_strtol(argv[2], NULL, 0);
1166
1167 if (cros_ec_clear_host_events(dev, events)) {
1168 debug("%s: Could not clear host events\n", __func__);
1169 return 1;
1170 }
1171 } else if (0 == strcmp("read", cmd)) {
1172 ret = do_read_write(dev, 0, argc, argv);
1173 if (ret > 0)
1174 return CMD_RET_USAGE;
1175 } else if (0 == strcmp("write", cmd)) {
1176 ret = do_read_write(dev, 1, argc, argv);
1177 if (ret > 0)
1178 return CMD_RET_USAGE;
1179 } else if (0 == strcmp("erase", cmd)) {
1180 int region = cros_ec_decode_region(argc - 2, argv + 2);
1181 uint32_t offset, size;
1182
1183 if (region == -1)
1184 return CMD_RET_USAGE;
1185 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
1186 debug("%s: Could not read region info\n", __func__);
1187 ret = -1;
1188 } else {
1189 ret = cros_ec_flash_erase(dev, offset, size);
1190 if (ret) {
1191 debug("%s: Could not erase region\n",
1192 __func__);
1193 }
1194 }
1195 } else if (0 == strcmp("regioninfo", cmd)) {
1196 int region = cros_ec_decode_region(argc - 2, argv + 2);
1197 uint32_t offset, size;
1198
1199 if (region == -1)
1200 return CMD_RET_USAGE;
1201 ret = cros_ec_flash_offset(dev, region, &offset, &size);
1202 if (ret) {
1203 debug("%s: Could not read region info\n", __func__);
1204 } else {
1205 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
1206 "RO" : "RW");
1207 printf("Offset: %x\n", offset);
1208 printf("Size: %x\n", size);
1209 }
1210 } else if (0 == strcmp("vbnvcontext", cmd)) {
1211 uint8_t block[EC_VBNV_BLOCK_SIZE];
1212 char buf[3];
1213 int i, len;
1214 unsigned long result;
1215
1216 if (argc <= 2) {
1217 ret = cros_ec_read_vbnvcontext(dev, block);
1218 if (!ret) {
1219 printf("vbnv_block: ");
1220 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
1221 printf("%02x", block[i]);
1222 putc('\n');
1223 }
1224 } else {
1225 /*
1226 * TODO(clchiou): Move this to a utility function as
1227 * cmd_spi might want to call it.
1228 */
1229 memset(block, 0, EC_VBNV_BLOCK_SIZE);
1230 len = strlen(argv[2]);
1231 buf[2] = '\0';
1232 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
1233 if (i * 2 >= len)
1234 break;
1235 buf[0] = argv[2][i * 2];
1236 if (i * 2 + 1 >= len)
1237 buf[1] = '0';
1238 else
1239 buf[1] = argv[2][i * 2 + 1];
1240 strict_strtoul(buf, 16, &result);
1241 block[i] = result;
1242 }
1243 ret = cros_ec_write_vbnvcontext(dev, block);
1244 }
1245 if (ret) {
1246 debug("%s: Could not %s VbNvContext\n", __func__,
1247 argc <= 2 ? "read" : "write");
1248 }
1249 } else if (0 == strcmp("test", cmd)) {
1250 int result = cros_ec_test(dev);
1251
1252 if (result)
1253 printf("Test failed with error %d\n", result);
1254 else
1255 puts("Test passed\n");
1256 } else if (0 == strcmp("version", cmd)) {
1257 struct ec_response_get_version *p;
1258 char *build_string;
1259
1260 ret = cros_ec_read_version(dev, &p);
1261 if (!ret) {
1262 /* Print versions */
1263 printf("RO version: %1.*s\n",
1264 sizeof(p->version_string_ro),
1265 p->version_string_ro);
1266 printf("RW version: %1.*s\n",
1267 sizeof(p->version_string_rw),
1268 p->version_string_rw);
1269 printf("Firmware copy: %s\n",
1270 (p->current_image <
1271 ARRAY_SIZE(ec_current_image_name) ?
1272 ec_current_image_name[p->current_image] :
1273 "?"));
1274 ret = cros_ec_read_build_info(dev, &build_string);
1275 if (!ret)
1276 printf("Build info: %s\n", build_string);
1277 }
1278 } else if (0 == strcmp("ldo", cmd)) {
1279 uint8_t index, state;
1280 char *endp;
1281
1282 if (argc < 3)
1283 return CMD_RET_USAGE;
1284 index = simple_strtoul(argv[2], &endp, 10);
1285 if (*argv[2] == 0 || *endp != 0)
1286 return CMD_RET_USAGE;
1287 if (argc > 3) {
1288 state = simple_strtoul(argv[3], &endp, 10);
1289 if (*argv[3] == 0 || *endp != 0)
1290 return CMD_RET_USAGE;
1291 ret = cros_ec_set_ldo(dev, index, state);
1292 } else {
1293 ret = cros_ec_get_ldo(dev, index, &state);
1294 if (!ret) {
1295 printf("LDO%d: %s\n", index,
1296 state == EC_LDO_STATE_ON ?
1297 "on" : "off");
1298 }
1299 }
1300
1301 if (ret) {
1302 debug("%s: Could not access LDO%d\n", __func__, index);
1303 return ret;
1304 }
1305 } else {
1306 return CMD_RET_USAGE;
1307 }
1308
1309 if (ret < 0) {
1310 printf("Error: CROS-EC command failed (error %d)\n", ret);
1311 ret = 1;
1312 }
1313
1314 return ret;
1315}
1316
1317U_BOOT_CMD(
1318 crosec, 5, 1, do_cros_ec,
1319 "CROS-EC utility command",
1320 "init Re-init CROS-EC (done on startup automatically)\n"
1321 "crosec id Read CROS-EC ID\n"
1322 "crosec info Read CROS-EC info\n"
1323 "crosec curimage Read CROS-EC current image\n"
1324 "crosec hash Read CROS-EC hash\n"
1325 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
1326 "crosec events Read CROS-EC host events\n"
1327 "crosec clrevents [mask] Clear CROS-EC host events\n"
1328 "crosec regioninfo <ro|rw> Read image info\n"
1329 "crosec erase <ro|rw> Erase EC image\n"
1330 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
1331 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
1332 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
1333 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
1334 "crosec test run tests on cros_ec\n"
1335 "crosec version Read CROS-EC version"
1336);
1337#endif