blob: efcad89eb0bb9c2ebe2eb464f5f6a5c06174fa19 [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>
Simon Glass84d6cbd2014-10-13 23:42:14 -060019#include <dm.h>
Hung-ying Tyan88364382013-05-15 18:27:28 +080020#include <i2c.h>
21#include <cros_ec.h>
22#include <fdtdec.h>
23#include <malloc.h>
24#include <spi.h>
Simon Glass2ab83f02014-02-27 13:26:11 -070025#include <asm/errno.h>
Hung-ying Tyan88364382013-05-15 18:27:28 +080026#include <asm/io.h>
27#include <asm-generic/gpio.h>
Simon Glass84d6cbd2014-10-13 23:42:14 -060028#include <dm/device-internal.h>
29#include <dm/uclass-internal.h>
Hung-ying Tyan88364382013-05-15 18:27:28 +080030
31#ifdef DEBUG_TRACE
32#define debug_trace(fmt, b...) debug(fmt, #b)
33#else
34#define debug_trace(fmt, b...)
35#endif
36
37enum {
38 /* Timeout waiting for a flash erase command to complete */
39 CROS_EC_CMD_TIMEOUT_MS = 5000,
40 /* Timeout waiting for a synchronous hash to be recomputed */
41 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
42};
43
Simon Glass84d6cbd2014-10-13 23:42:14 -060044#ifndef CONFIG_DM_CROS_EC
Hung-ying Tyan88364382013-05-15 18:27:28 +080045static struct cros_ec_dev static_dev, *last_dev;
Simon Glass84d6cbd2014-10-13 23:42:14 -060046#endif
Hung-ying Tyan88364382013-05-15 18:27:28 +080047
48DECLARE_GLOBAL_DATA_PTR;
49
50/* Note: depends on enum ec_current_image */
51static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
52
53void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
54{
55#ifdef DEBUG
56 int i;
57
58 printf("%s: ", name);
59 if (cmd != -1)
60 printf("cmd=%#x: ", cmd);
61 for (i = 0; i < len; i++)
62 printf("%02x ", data[i]);
63 printf("\n");
64#endif
65}
66
67/*
68 * Calculate a simple 8-bit checksum of a data block
69 *
70 * @param data Data block to checksum
71 * @param size Size of data block in bytes
72 * @return checksum value (0 to 255)
73 */
74int cros_ec_calc_checksum(const uint8_t *data, int size)
75{
76 int csum, i;
77
78 for (i = csum = 0; i < size; i++)
79 csum += data[i];
80 return csum & 0xff;
81}
82
Simon Glass2d8ede52014-02-27 13:26:09 -070083/**
84 * Create a request packet for protocol version 3.
85 *
86 * The packet is stored in the device's internal output buffer.
87 *
88 * @param dev CROS-EC device
89 * @param cmd Command to send (EC_CMD_...)
90 * @param cmd_version Version of command to send (EC_VER_...)
91 * @param dout Output data (may be NULL If dout_len=0)
92 * @param dout_len Size of output data in bytes
93 * @return packet size in bytes, or <0 if error.
94 */
95static int create_proto3_request(struct cros_ec_dev *dev,
96 int cmd, int cmd_version,
97 const void *dout, int dout_len)
98{
99 struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
100 int out_bytes = dout_len + sizeof(*rq);
101
102 /* Fail if output size is too big */
103 if (out_bytes > (int)sizeof(dev->dout)) {
104 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
105 return -EC_RES_REQUEST_TRUNCATED;
106 }
107
108 /* Fill in request packet */
109 rq->struct_version = EC_HOST_REQUEST_VERSION;
110 rq->checksum = 0;
111 rq->command = cmd;
112 rq->command_version = cmd_version;
113 rq->reserved = 0;
114 rq->data_len = dout_len;
115
116 /* Copy data after header */
117 memcpy(rq + 1, dout, dout_len);
118
119 /* Write checksum field so the entire packet sums to 0 */
120 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
121
122 cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
123
124 /* Return size of request packet */
125 return out_bytes;
126}
127
128/**
129 * Prepare the device to receive a protocol version 3 response.
130 *
131 * @param dev CROS-EC device
132 * @param din_len Maximum size of response in bytes
133 * @return maximum expected number of bytes in response, or <0 if error.
134 */
135static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
136{
137 int in_bytes = din_len + sizeof(struct ec_host_response);
138
139 /* Fail if input size is too big */
140 if (in_bytes > (int)sizeof(dev->din)) {
141 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
142 return -EC_RES_RESPONSE_TOO_BIG;
143 }
144
145 /* Return expected size of response packet */
146 return in_bytes;
147}
148
149/**
150 * Handle a protocol version 3 response packet.
151 *
152 * The packet must already be stored in the device's internal input buffer.
153 *
154 * @param dev CROS-EC device
155 * @param dinp Returns pointer to response data
156 * @param din_len Maximum size of response in bytes
Simon Glass8bbb38b2015-01-25 08:27:17 -0700157 * @return number of bytes of response data, or <0 if error. Note that error
158 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
159 * overlap!)
Simon Glass2d8ede52014-02-27 13:26:09 -0700160 */
161static int handle_proto3_response(struct cros_ec_dev *dev,
162 uint8_t **dinp, int din_len)
163{
164 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
165 int in_bytes;
166 int csum;
167
168 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
169
170 /* Check input data */
171 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
172 debug("%s: EC response version mismatch\n", __func__);
173 return -EC_RES_INVALID_RESPONSE;
174 }
175
176 if (rs->reserved) {
177 debug("%s: EC response reserved != 0\n", __func__);
178 return -EC_RES_INVALID_RESPONSE;
179 }
180
181 if (rs->data_len > din_len) {
182 debug("%s: EC returned too much data\n", __func__);
183 return -EC_RES_RESPONSE_TOO_BIG;
184 }
185
186 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
187
188 /* Update in_bytes to actual data size */
189 in_bytes = sizeof(*rs) + rs->data_len;
190
191 /* Verify checksum */
192 csum = cros_ec_calc_checksum(dev->din, in_bytes);
193 if (csum) {
194 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
195 csum);
196 return -EC_RES_INVALID_CHECKSUM;
197 }
198
199 /* Return error result, if any */
200 if (rs->result)
201 return -(int)rs->result;
202
203 /* If we're still here, set response data pointer and return length */
204 *dinp = (uint8_t *)(rs + 1);
205
206 return rs->data_len;
207}
208
209static int send_command_proto3(struct cros_ec_dev *dev,
210 int cmd, int cmd_version,
211 const void *dout, int dout_len,
212 uint8_t **dinp, int din_len)
213{
Simon Glass84d6cbd2014-10-13 23:42:14 -0600214#ifdef CONFIG_DM_CROS_EC
215 struct dm_cros_ec_ops *ops;
216#endif
Simon Glass2d8ede52014-02-27 13:26:09 -0700217 int out_bytes, in_bytes;
218 int rv;
219
220 /* Create request packet */
221 out_bytes = create_proto3_request(dev, cmd, cmd_version,
222 dout, dout_len);
223 if (out_bytes < 0)
224 return out_bytes;
225
226 /* Prepare response buffer */
227 in_bytes = prepare_proto3_response_buffer(dev, din_len);
228 if (in_bytes < 0)
229 return in_bytes;
230
Simon Glass84d6cbd2014-10-13 23:42:14 -0600231#ifdef CONFIG_DM_CROS_EC
232 ops = dm_cros_ec_get_ops(dev->dev);
Simon Glass8bbb38b2015-01-25 08:27:17 -0700233 rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS;
Simon Glass84d6cbd2014-10-13 23:42:14 -0600234#else
Simon Glass2d8ede52014-02-27 13:26:09 -0700235 switch (dev->interface) {
Randall Spanglera6070282014-02-27 13:26:10 -0700236#ifdef CONFIG_CROS_EC_SPI
237 case CROS_EC_IF_SPI:
238 rv = cros_ec_spi_packet(dev, out_bytes, in_bytes);
239 break;
240#endif
Simon Glassdf93d902014-02-27 13:26:12 -0700241#ifdef CONFIG_CROS_EC_SANDBOX
242 case CROS_EC_IF_SANDBOX:
243 rv = cros_ec_sandbox_packet(dev, out_bytes, in_bytes);
244 break;
245#endif
Simon Glass2d8ede52014-02-27 13:26:09 -0700246 case CROS_EC_IF_NONE:
247 /* TODO: support protocol 3 for LPC, I2C; for now fall through */
248 default:
249 debug("%s: Unsupported interface\n", __func__);
250 rv = -1;
251 }
Simon Glass84d6cbd2014-10-13 23:42:14 -0600252#endif
Simon Glass2d8ede52014-02-27 13:26:09 -0700253 if (rv < 0)
254 return rv;
255
256 /* Process the response */
257 return handle_proto3_response(dev, dinp, din_len);
258}
259
Hung-ying Tyan88364382013-05-15 18:27:28 +0800260static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
261 const void *dout, int dout_len,
262 uint8_t **dinp, int din_len)
263{
Simon Glass84d6cbd2014-10-13 23:42:14 -0600264#ifdef CONFIG_DM_CROS_EC
265 struct dm_cros_ec_ops *ops;
266#endif
Simon Glass2d8ede52014-02-27 13:26:09 -0700267 int ret = -1;
268
269 /* Handle protocol version 3 support */
270 if (dev->protocol_version == 3) {
271 return send_command_proto3(dev, cmd, cmd_version,
272 dout, dout_len, dinp, din_len);
273 }
Hung-ying Tyan88364382013-05-15 18:27:28 +0800274
Simon Glass84d6cbd2014-10-13 23:42:14 -0600275#ifdef CONFIG_DM_CROS_EC
276 ops = dm_cros_ec_get_ops(dev->dev);
277 ret = ops->command(dev->dev, cmd, cmd_version,
278 (const uint8_t *)dout, dout_len, dinp, din_len);
279#else
Hung-ying Tyan88364382013-05-15 18:27:28 +0800280 switch (dev->interface) {
281#ifdef CONFIG_CROS_EC_SPI
282 case CROS_EC_IF_SPI:
283 ret = cros_ec_spi_command(dev, cmd, cmd_version,
284 (const uint8_t *)dout, dout_len,
285 dinp, din_len);
286 break;
287#endif
288#ifdef CONFIG_CROS_EC_I2C
289 case CROS_EC_IF_I2C:
290 ret = cros_ec_i2c_command(dev, cmd, cmd_version,
291 (const uint8_t *)dout, dout_len,
292 dinp, din_len);
293 break;
294#endif
295#ifdef CONFIG_CROS_EC_LPC
296 case CROS_EC_IF_LPC:
297 ret = cros_ec_lpc_command(dev, cmd, cmd_version,
298 (const uint8_t *)dout, dout_len,
299 dinp, din_len);
300 break;
301#endif
302 case CROS_EC_IF_NONE:
303 default:
304 ret = -1;
305 }
Simon Glass84d6cbd2014-10-13 23:42:14 -0600306#endif
Hung-ying Tyan88364382013-05-15 18:27:28 +0800307
308 return ret;
309}
310
311/**
312 * Send a command to the CROS-EC device and return the reply.
313 *
314 * The device's internal input/output buffers are used.
315 *
316 * @param dev CROS-EC device
317 * @param cmd Command to send (EC_CMD_...)
318 * @param cmd_version Version of command to send (EC_VER_...)
319 * @param dout Output data (may be NULL If dout_len=0)
320 * @param dout_len Size of output data in bytes
321 * @param dinp Response data (may be NULL If din_len=0).
322 * If not NULL, it will be updated to point to the data
323 * and will always be double word aligned (64-bits)
324 * @param din_len Maximum size of response in bytes
Simon Glass8bbb38b2015-01-25 08:27:17 -0700325 * @return number of bytes in response, or -ve on error
Hung-ying Tyan88364382013-05-15 18:27:28 +0800326 */
327static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
328 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
329 int din_len)
330{
Simon Glass2ab83f02014-02-27 13:26:11 -0700331 uint8_t *din = NULL;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800332 int len;
333
Hung-ying Tyan88364382013-05-15 18:27:28 +0800334 len = send_command(dev, cmd, cmd_version, dout, dout_len,
335 &din, din_len);
336
337 /* If the command doesn't complete, wait a while */
338 if (len == -EC_RES_IN_PROGRESS) {
Simon Glass2ab83f02014-02-27 13:26:11 -0700339 struct ec_response_get_comms_status *resp = NULL;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800340 ulong start;
341
342 /* Wait for command to complete */
343 start = get_timer(0);
344 do {
345 int ret;
346
347 mdelay(50); /* Insert some reasonable delay */
348 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
349 NULL, 0,
350 (uint8_t **)&resp, sizeof(*resp));
351 if (ret < 0)
352 return ret;
353
354 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
355 debug("%s: Command %#02x timeout\n",
356 __func__, cmd);
357 return -EC_RES_TIMEOUT;
358 }
359 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
360
361 /* OK it completed, so read the status response */
362 /* not sure why it was 0 for the last argument */
363 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
364 NULL, 0, &din, din_len);
365 }
366
Simon Glass2ab83f02014-02-27 13:26:11 -0700367 debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp,
368 dinp ? *dinp : NULL);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800369 if (dinp) {
370 /* If we have any data to return, it must be 64bit-aligned */
371 assert(len <= 0 || !((uintptr_t)din & 7));
372 *dinp = din;
373 }
374
375 return len;
376}
377
378/**
379 * Send a command to the CROS-EC device and return the reply.
380 *
381 * The device's internal input/output buffers are used.
382 *
383 * @param dev CROS-EC device
384 * @param cmd Command to send (EC_CMD_...)
385 * @param cmd_version Version of command to send (EC_VER_...)
386 * @param dout Output data (may be NULL If dout_len=0)
387 * @param dout_len Size of output data in bytes
388 * @param din Response data (may be NULL If din_len=0).
389 * It not NULL, it is a place for ec_command() to copy the
390 * data to.
391 * @param din_len Maximum size of response in bytes
Simon Glass8bbb38b2015-01-25 08:27:17 -0700392 * @return number of bytes in response, or -ve on error
Hung-ying Tyan88364382013-05-15 18:27:28 +0800393 */
394static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
395 const void *dout, int dout_len,
396 void *din, int din_len)
397{
398 uint8_t *in_buffer;
399 int len;
400
401 assert((din_len == 0) || din);
402 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
403 &in_buffer, din_len);
404 if (len > 0) {
405 /*
406 * If we were asked to put it somewhere, do so, otherwise just
407 * disregard the result.
408 */
409 if (din && in_buffer) {
410 assert(len <= din_len);
411 memmove(din, in_buffer, len);
412 }
413 }
414 return len;
415}
416
417int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
418{
Simon Glass836bb6e2014-02-27 13:26:07 -0700419 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
Simon Glass2ab83f02014-02-27 13:26:11 -0700420 sizeof(scan->data)) != sizeof(scan->data))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800421 return -1;
422
423 return 0;
424}
425
426int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
427{
428 struct ec_response_get_version *r;
429
430 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
Simon Glass2ab83f02014-02-27 13:26:11 -0700431 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800432 return -1;
433
Simon Glass2ab83f02014-02-27 13:26:11 -0700434 if (maxlen > (int)sizeof(r->version_string_ro))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800435 maxlen = sizeof(r->version_string_ro);
436
437 switch (r->current_image) {
438 case EC_IMAGE_RO:
439 memcpy(id, r->version_string_ro, maxlen);
440 break;
441 case EC_IMAGE_RW:
442 memcpy(id, r->version_string_rw, maxlen);
443 break;
444 default:
445 return -1;
446 }
447
448 id[maxlen - 1] = '\0';
449 return 0;
450}
451
452int cros_ec_read_version(struct cros_ec_dev *dev,
453 struct ec_response_get_version **versionp)
454{
455 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
456 (uint8_t **)versionp, sizeof(**versionp))
Simon Glass2ab83f02014-02-27 13:26:11 -0700457 != sizeof(**versionp))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800458 return -1;
459
460 return 0;
461}
462
463int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
464{
465 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
Simon Glass836bb6e2014-02-27 13:26:07 -0700466 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800467 return -1;
468
469 return 0;
470}
471
472int cros_ec_read_current_image(struct cros_ec_dev *dev,
473 enum ec_current_image *image)
474{
475 struct ec_response_get_version *r;
476
477 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
Simon Glass2ab83f02014-02-27 13:26:11 -0700478 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800479 return -1;
480
481 *image = r->current_image;
482 return 0;
483}
484
485static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
486 struct ec_response_vboot_hash *hash)
487{
488 struct ec_params_vboot_hash p;
489 ulong start;
490
491 start = get_timer(0);
492 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
493 mdelay(50); /* Insert some reasonable delay */
494
495 p.cmd = EC_VBOOT_HASH_GET;
496 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
497 hash, sizeof(*hash)) < 0)
498 return -1;
499
500 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
501 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
502 return -EC_RES_TIMEOUT;
503 }
504 }
505 return 0;
506}
507
508
509int cros_ec_read_hash(struct cros_ec_dev *dev,
510 struct ec_response_vboot_hash *hash)
511{
512 struct ec_params_vboot_hash p;
513 int rv;
514
515 p.cmd = EC_VBOOT_HASH_GET;
516 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
517 hash, sizeof(*hash)) < 0)
518 return -1;
519
520 /* If the EC is busy calculating the hash, fidget until it's done. */
521 rv = cros_ec_wait_on_hash_done(dev, hash);
522 if (rv)
523 return rv;
524
525 /* If the hash is valid, we're done. Otherwise, we have to kick it off
526 * again and wait for it to complete. Note that we explicitly assume
527 * that hashing zero bytes is always wrong, even though that would
528 * produce a valid hash value. */
529 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
530 return 0;
531
532 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
533 __func__, hash->status, hash->size);
534
Simon Glass836bb6e2014-02-27 13:26:07 -0700535 p.cmd = EC_VBOOT_HASH_START;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800536 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
537 p.nonce_size = 0;
538 p.offset = EC_VBOOT_HASH_OFFSET_RW;
539
540 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
541 hash, sizeof(*hash)) < 0)
542 return -1;
543
544 rv = cros_ec_wait_on_hash_done(dev, hash);
545 if (rv)
546 return rv;
547
548 debug("%s: hash done\n", __func__);
549
550 return 0;
551}
552
553static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
554{
555 struct ec_params_vboot_hash p;
556 struct ec_response_vboot_hash *hash;
557
558 /* We don't have an explict command for the EC to discard its current
559 * hash value, so we'll just tell it to calculate one that we know is
560 * wrong (we claim that hashing zero bytes is always invalid).
561 */
562 p.cmd = EC_VBOOT_HASH_RECALC;
563 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
564 p.nonce_size = 0;
565 p.offset = 0;
566 p.size = 0;
567
568 debug("%s:\n", __func__);
569
570 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
571 (uint8_t **)&hash, sizeof(*hash)) < 0)
572 return -1;
573
574 /* No need to wait for it to finish */
575 return 0;
576}
577
578int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
579 uint8_t flags)
580{
581 struct ec_params_reboot_ec p;
582
583 p.cmd = cmd;
584 p.flags = flags;
585
586 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
587 < 0)
588 return -1;
589
590 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
591 /*
592 * EC reboot will take place immediately so delay to allow it
593 * to complete. Note that some reboot types (EC_REBOOT_COLD)
594 * will reboot the AP as well, in which case we won't actually
595 * get to this point.
596 */
597 /*
598 * TODO(rspangler@chromium.org): Would be nice if we had a
599 * better way to determine when the reboot is complete. Could
600 * we poll a memory-mapped LPC value?
601 */
602 udelay(50000);
603 }
604
605 return 0;
606}
607
608int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
609{
610 /* no interrupt support : always poll */
Simon Glass32f8a192015-01-05 20:05:32 -0700611 if (!dm_gpio_is_valid(&dev->ec_int))
Simon Glass2ab83f02014-02-27 13:26:11 -0700612 return -ENOENT;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800613
Simon Glass32f8a192015-01-05 20:05:32 -0700614 return dm_gpio_get_value(&dev->ec_int);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800615}
616
Simon Glass836bb6e2014-02-27 13:26:07 -0700617int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800618{
Simon Glass836bb6e2014-02-27 13:26:07 -0700619 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
Simon Glass2ab83f02014-02-27 13:26:11 -0700620 sizeof(*info)) != sizeof(*info))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800621 return -1;
622
623 return 0;
624}
625
626int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
627{
628 struct ec_response_host_event_mask *resp;
629
630 /*
631 * Use the B copy of the event flags, because the main copy is already
632 * used by ACPI/SMI.
633 */
634 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
Simon Glass2ab83f02014-02-27 13:26:11 -0700635 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800636 return -1;
637
638 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
639 return -1;
640
641 *events_ptr = resp->mask;
642 return 0;
643}
644
645int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
646{
647 struct ec_params_host_event_mask params;
648
649 params.mask = events;
650
651 /*
652 * Use the B copy of the event flags, so it affects the data returned
653 * by cros_ec_get_host_events().
654 */
655 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
656 &params, sizeof(params), NULL, 0) < 0)
657 return -1;
658
659 return 0;
660}
661
662int cros_ec_flash_protect(struct cros_ec_dev *dev,
663 uint32_t set_mask, uint32_t set_flags,
664 struct ec_response_flash_protect *resp)
665{
666 struct ec_params_flash_protect params;
667
668 params.mask = set_mask;
669 params.flags = set_flags;
670
671 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
672 &params, sizeof(params),
Simon Glass2ab83f02014-02-27 13:26:11 -0700673 resp, sizeof(*resp)) != sizeof(*resp))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800674 return -1;
675
676 return 0;
677}
678
679static int cros_ec_check_version(struct cros_ec_dev *dev)
680{
681 struct ec_params_hello req;
682 struct ec_response_hello *resp;
683
Simon Glass72a38e02015-03-26 09:29:30 -0600684#ifdef CONFIG_DM_CROS_EC
685 struct dm_cros_ec_ops *ops;
686 int ret;
687
688 ops = dm_cros_ec_get_ops(dev->dev);
689 if (ops->check_version) {
690 ret = ops->check_version(dev->dev);
691 if (ret)
692 return ret;
693 }
694#else
Hung-ying Tyan88364382013-05-15 18:27:28 +0800695#ifdef CONFIG_CROS_EC_LPC
696 /* LPC has its own way of doing this */
697 if (dev->interface == CROS_EC_IF_LPC)
698 return cros_ec_lpc_check_version(dev);
699#endif
Simon Glass72a38e02015-03-26 09:29:30 -0600700#endif
Hung-ying Tyan88364382013-05-15 18:27:28 +0800701
702 /*
703 * TODO(sjg@chromium.org).
704 * There is a strange oddity here with the EC. We could just ignore
705 * the response, i.e. pass the last two parameters as NULL and 0.
706 * In this case we won't read back very many bytes from the EC.
707 * On the I2C bus the EC gets upset about this and will try to send
708 * the bytes anyway. This means that we will have to wait for that
709 * to complete before continuing with a new EC command.
710 *
711 * This problem is probably unique to the I2C bus.
712 *
713 * So for now, just read all the data anyway.
714 */
Randall Spanglere8c12662014-02-27 13:26:08 -0700715
Randall Spanglera6070282014-02-27 13:26:10 -0700716 /* Try sending a version 3 packet */
717 dev->protocol_version = 3;
Simon Glassd11e8fd2014-11-11 18:06:30 -0700718 req.in_data = 0;
Randall Spanglera6070282014-02-27 13:26:10 -0700719 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
720 (uint8_t **)&resp, sizeof(*resp)) > 0) {
721 return 0;
722 }
723
Randall Spanglere8c12662014-02-27 13:26:08 -0700724 /* Try sending a version 2 packet */
725 dev->protocol_version = 2;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800726 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
727 (uint8_t **)&resp, sizeof(*resp)) > 0) {
Randall Spanglere8c12662014-02-27 13:26:08 -0700728 return 0;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800729 }
730
Randall Spanglere8c12662014-02-27 13:26:08 -0700731 /*
732 * Fail if we're still here, since the EC doesn't understand any
733 * protcol version we speak. Version 1 interface without command
734 * version is no longer supported, and we don't know about any new
735 * protocol versions.
736 */
737 dev->protocol_version = 0;
738 printf("%s: ERROR: old EC interface not supported\n", __func__);
739 return -1;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800740}
741
742int cros_ec_test(struct cros_ec_dev *dev)
743{
744 struct ec_params_hello req;
745 struct ec_response_hello *resp;
746
747 req.in_data = 0x12345678;
748 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
749 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
750 printf("ec_command_inptr() returned error\n");
751 return -1;
752 }
753 if (resp->out_data != req.in_data + 0x01020304) {
754 printf("Received invalid handshake %x\n", resp->out_data);
755 return -1;
756 }
757
758 return 0;
759}
760
761int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
762 uint32_t *offset, uint32_t *size)
763{
764 struct ec_params_flash_region_info p;
765 struct ec_response_flash_region_info *r;
766 int ret;
767
768 p.region = region;
769 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
770 EC_VER_FLASH_REGION_INFO,
771 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
772 if (ret != sizeof(*r))
773 return -1;
774
775 if (offset)
776 *offset = r->offset;
777 if (size)
778 *size = r->size;
779
780 return 0;
781}
782
783int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
784{
785 struct ec_params_flash_erase p;
786
787 p.offset = offset;
788 p.size = size;
789 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
790 NULL, 0);
791}
792
793/**
794 * Write a single block to the flash
795 *
796 * Write a block of data to the EC flash. The size must not exceed the flash
797 * write block size which you can obtain from cros_ec_flash_write_burst_size().
798 *
799 * The offset starts at 0. You can obtain the region information from
800 * cros_ec_flash_offset() to find out where to write for a particular region.
801 *
802 * Attempting to write to the region where the EC is currently running from
803 * will result in an error.
804 *
805 * @param dev CROS-EC device
806 * @param data Pointer to data buffer to write
807 * @param offset Offset within flash to write to.
808 * @param size Number of bytes to write
809 * @return 0 if ok, -1 on error
810 */
811static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
812 const uint8_t *data, uint32_t offset, uint32_t size)
813{
814 struct ec_params_flash_write p;
815
816 p.offset = offset;
817 p.size = size;
Simon Glass836bb6e2014-02-27 13:26:07 -0700818 assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
819 memcpy(&p + 1, data, p.size);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800820
821 return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
822 &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
823}
824
825/**
826 * Return optimal flash write burst size
827 */
828static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
829{
Simon Glass836bb6e2014-02-27 13:26:07 -0700830 return EC_FLASH_WRITE_VER0_SIZE;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800831}
832
833/**
834 * Check if a block of data is erased (all 0xff)
835 *
836 * This function is useful when dealing with flash, for checking whether a
837 * data block is erased and thus does not need to be programmed.
838 *
839 * @param data Pointer to data to check (must be word-aligned)
840 * @param size Number of bytes to check (must be word-aligned)
841 * @return 0 if erased, non-zero if any word is not erased
842 */
843static int cros_ec_data_is_erased(const uint32_t *data, int size)
844{
845 assert(!(size & 3));
846 size /= sizeof(uint32_t);
847 for (; size > 0; size -= 4, data++)
848 if (*data != -1U)
849 return 0;
850
851 return 1;
852}
853
854int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
855 uint32_t offset, uint32_t size)
856{
857 uint32_t burst = cros_ec_flash_write_burst_size(dev);
858 uint32_t end, off;
859 int ret;
860
861 /*
862 * TODO: round up to the nearest multiple of write size. Can get away
863 * without that on link right now because its write size is 4 bytes.
864 */
865 end = offset + size;
866 for (off = offset; off < end; off += burst, data += burst) {
867 uint32_t todo;
868
869 /* If the data is empty, there is no point in programming it */
870 todo = min(end - off, burst);
871 if (dev->optimise_flash_write &&
872 cros_ec_data_is_erased((uint32_t *)data, todo))
873 continue;
874
875 ret = cros_ec_flash_write_block(dev, data, off, todo);
876 if (ret)
877 return ret;
878 }
879
880 return 0;
881}
882
883/**
884 * Read a single block from the flash
885 *
886 * Read a block of data from the EC flash. The size must not exceed the flash
887 * write block size which you can obtain from cros_ec_flash_write_burst_size().
888 *
889 * The offset starts at 0. You can obtain the region information from
890 * cros_ec_flash_offset() to find out where to read for a particular region.
891 *
892 * @param dev CROS-EC device
893 * @param data Pointer to data buffer to read into
894 * @param offset Offset within flash to read from
895 * @param size Number of bytes to read
896 * @return 0 if ok, -1 on error
897 */
898static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
899 uint32_t offset, uint32_t size)
900{
901 struct ec_params_flash_read p;
902
903 p.offset = offset;
904 p.size = size;
905
906 return ec_command(dev, EC_CMD_FLASH_READ, 0,
907 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
908}
909
910int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
911 uint32_t size)
912{
913 uint32_t burst = cros_ec_flash_write_burst_size(dev);
914 uint32_t end, off;
915 int ret;
916
917 end = offset + size;
918 for (off = offset; off < end; off += burst, data += burst) {
919 ret = cros_ec_flash_read_block(dev, data, off,
920 min(end - off, burst));
921 if (ret)
922 return ret;
923 }
924
925 return 0;
926}
927
928int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
929 const uint8_t *image, int image_size)
930{
931 uint32_t rw_offset, rw_size;
932 int ret;
933
934 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
935 return -1;
Simon Glass2ab83f02014-02-27 13:26:11 -0700936 if (image_size > (int)rw_size)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800937 return -1;
938
939 /* Invalidate the existing hash, just in case the AP reboots
940 * unexpectedly during the update. If that happened, the EC RW firmware
941 * would be invalid, but the EC would still have the original hash.
942 */
943 ret = cros_ec_invalidate_hash(dev);
944 if (ret)
945 return ret;
946
947 /*
948 * Erase the entire RW section, so that the EC doesn't see any garbage
949 * past the new image if it's smaller than the current image.
950 *
951 * TODO: could optimize this to erase just the current image, since
952 * presumably everything past that is 0xff's. But would still need to
953 * round up to the nearest multiple of erase size.
954 */
955 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
956 if (ret)
957 return ret;
958
959 /* Write the image */
960 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
961 if (ret)
962 return ret;
963
964 return 0;
965}
966
967int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
968{
969 struct ec_params_vbnvcontext p;
970 int len;
971
972 p.op = EC_VBNV_CONTEXT_OP_READ;
973
974 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
975 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
976 if (len < EC_VBNV_BLOCK_SIZE)
977 return -1;
978
979 return 0;
980}
981
982int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
983{
984 struct ec_params_vbnvcontext p;
985 int len;
986
987 p.op = EC_VBNV_CONTEXT_OP_WRITE;
988 memcpy(p.block, block, sizeof(p.block));
989
990 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
991 &p, sizeof(p), NULL, 0);
992 if (len < 0)
993 return -1;
994
995 return 0;
996}
997
998int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
999{
1000 struct ec_params_ldo_set params;
1001
1002 params.index = index;
1003 params.state = state;
1004
1005 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
1006 &params, sizeof(params),
1007 NULL, 0))
1008 return -1;
1009
1010 return 0;
1011}
1012
1013int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
1014{
1015 struct ec_params_ldo_get params;
1016 struct ec_response_ldo_get *resp;
1017
1018 params.index = index;
1019
1020 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
1021 &params, sizeof(params),
Simon Glass2ab83f02014-02-27 13:26:11 -07001022 (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
Hung-ying Tyan88364382013-05-15 18:27:28 +08001023 return -1;
1024
1025 *state = resp->state;
1026
1027 return 0;
1028}
1029
Simon Glass84d6cbd2014-10-13 23:42:14 -06001030#ifndef CONFIG_DM_CROS_EC
Hung-ying Tyan88364382013-05-15 18:27:28 +08001031/**
Simon Glass836bb6e2014-02-27 13:26:07 -07001032 * Decode EC interface details from the device tree and allocate a suitable
1033 * device.
Hung-ying Tyan88364382013-05-15 18:27:28 +08001034 *
1035 * @param blob Device tree blob
1036 * @param node Node to decode from
1037 * @param devp Returns a pointer to the new allocated device
1038 * @return 0 if ok, -1 on error
1039 */
1040static int cros_ec_decode_fdt(const void *blob, int node,
1041 struct cros_ec_dev **devp)
1042{
1043 enum fdt_compat_id compat;
1044 struct cros_ec_dev *dev;
1045 int parent;
1046
1047 /* See what type of parent we are inside (this is expensive) */
1048 parent = fdt_parent_offset(blob, node);
1049 if (parent < 0) {
1050 debug("%s: Cannot find node parent\n", __func__);
1051 return -1;
1052 }
1053
1054 dev = &static_dev;
1055 dev->node = node;
1056 dev->parent_node = parent;
1057
1058 compat = fdtdec_lookup(blob, parent);
1059 switch (compat) {
1060#ifdef CONFIG_CROS_EC_SPI
1061 case COMPAT_SAMSUNG_EXYNOS_SPI:
1062 dev->interface = CROS_EC_IF_SPI;
1063 if (cros_ec_spi_decode_fdt(dev, blob))
1064 return -1;
1065 break;
1066#endif
1067#ifdef CONFIG_CROS_EC_I2C
1068 case COMPAT_SAMSUNG_S3C2440_I2C:
1069 dev->interface = CROS_EC_IF_I2C;
1070 if (cros_ec_i2c_decode_fdt(dev, blob))
1071 return -1;
1072 break;
1073#endif
1074#ifdef CONFIG_CROS_EC_LPC
1075 case COMPAT_INTEL_LPC:
1076 dev->interface = CROS_EC_IF_LPC;
1077 break;
1078#endif
Simon Glassdf93d902014-02-27 13:26:12 -07001079#ifdef CONFIG_CROS_EC_SANDBOX
1080 case COMPAT_SANDBOX_HOST_EMULATION:
1081 dev->interface = CROS_EC_IF_SANDBOX;
1082 break;
1083#endif
Hung-ying Tyan88364382013-05-15 18:27:28 +08001084 default:
1085 debug("%s: Unknown compat id %d\n", __func__, compat);
1086 return -1;
1087 }
1088
Simon Glass32f8a192015-01-05 20:05:32 -07001089 gpio_request_by_name_nodev(blob, node, "ec-interrupt", 0, &dev->ec_int,
1090 GPIOD_IS_IN);
Hung-ying Tyan88364382013-05-15 18:27:28 +08001091 dev->optimise_flash_write = fdtdec_get_bool(blob, node,
1092 "optimise-flash-write");
1093 *devp = dev;
1094
1095 return 0;
1096}
Simon Glass84d6cbd2014-10-13 23:42:14 -06001097#endif
Hung-ying Tyan88364382013-05-15 18:27:28 +08001098
Simon Glass84d6cbd2014-10-13 23:42:14 -06001099#ifdef CONFIG_DM_CROS_EC
1100int cros_ec_register(struct udevice *dev)
1101{
Simon Glasse564f052015-03-05 12:25:20 -07001102 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Simon Glass84d6cbd2014-10-13 23:42:14 -06001103 const void *blob = gd->fdt_blob;
1104 int node = dev->of_offset;
1105 char id[MSG_BYTES];
1106
1107 cdev->dev = dev;
Simon Glass32f8a192015-01-05 20:05:32 -07001108 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1109 GPIOD_IS_IN);
Simon Glass84d6cbd2014-10-13 23:42:14 -06001110 cdev->optimise_flash_write = fdtdec_get_bool(blob, node,
1111 "optimise-flash-write");
1112
Simon Glass84d6cbd2014-10-13 23:42:14 -06001113 if (cros_ec_check_version(cdev)) {
1114 debug("%s: Could not detect CROS-EC version\n", __func__);
1115 return -CROS_EC_ERR_CHECK_VERSION;
1116 }
1117
1118 if (cros_ec_read_id(cdev, id, sizeof(id))) {
1119 debug("%s: Could not read KBC ID\n", __func__);
1120 return -CROS_EC_ERR_READ_ID;
1121 }
1122
1123 /* Remember this device for use by the cros_ec command */
1124 debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
1125
1126 return 0;
1127}
1128#else
Hung-ying Tyan88364382013-05-15 18:27:28 +08001129int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
1130{
Hung-ying Tyan88364382013-05-15 18:27:28 +08001131 struct cros_ec_dev *dev;
Simon Glass84d6cbd2014-10-13 23:42:14 -06001132 char id[MSG_BYTES];
1133#ifdef CONFIG_DM_CROS_EC
1134 struct udevice *udev;
1135 int ret;
1136
1137 ret = uclass_find_device(UCLASS_CROS_EC, 0, &udev);
1138 if (!ret)
1139 device_remove(udev);
1140 ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
1141 if (ret)
1142 return ret;
Simon Glasse564f052015-03-05 12:25:20 -07001143 dev = dev_get_uclass_priv(udev);
Simon Glass84d6cbd2014-10-13 23:42:14 -06001144 return 0;
1145#else
Hung-ying Tyan88364382013-05-15 18:27:28 +08001146 int node = 0;
1147
1148 *cros_ecp = NULL;
1149 do {
1150 node = fdtdec_next_compatible(blob, node,
1151 COMPAT_GOOGLE_CROS_EC);
1152 if (node < 0) {
1153 debug("%s: Node not found\n", __func__);
1154 return 0;
1155 }
1156 } while (!fdtdec_get_is_enabled(blob, node));
1157
1158 if (cros_ec_decode_fdt(blob, node, &dev)) {
1159 debug("%s: Failed to decode device.\n", __func__);
1160 return -CROS_EC_ERR_FDT_DECODE;
1161 }
1162
1163 switch (dev->interface) {
1164#ifdef CONFIG_CROS_EC_SPI
1165 case CROS_EC_IF_SPI:
1166 if (cros_ec_spi_init(dev, blob)) {
1167 debug("%s: Could not setup SPI interface\n", __func__);
1168 return -CROS_EC_ERR_DEV_INIT;
1169 }
1170 break;
1171#endif
1172#ifdef CONFIG_CROS_EC_I2C
1173 case CROS_EC_IF_I2C:
1174 if (cros_ec_i2c_init(dev, blob))
1175 return -CROS_EC_ERR_DEV_INIT;
1176 break;
1177#endif
1178#ifdef CONFIG_CROS_EC_LPC
1179 case CROS_EC_IF_LPC:
1180 if (cros_ec_lpc_init(dev, blob))
1181 return -CROS_EC_ERR_DEV_INIT;
1182 break;
1183#endif
Simon Glassdf93d902014-02-27 13:26:12 -07001184#ifdef CONFIG_CROS_EC_SANDBOX
1185 case CROS_EC_IF_SANDBOX:
1186 if (cros_ec_sandbox_init(dev, blob))
1187 return -CROS_EC_ERR_DEV_INIT;
1188 break;
1189#endif
Hung-ying Tyan88364382013-05-15 18:27:28 +08001190 case CROS_EC_IF_NONE:
1191 default:
1192 return 0;
1193 }
Simon Glass84d6cbd2014-10-13 23:42:14 -06001194#endif
Hung-ying Tyan88364382013-05-15 18:27:28 +08001195
Hung-ying Tyan88364382013-05-15 18:27:28 +08001196 if (cros_ec_check_version(dev)) {
1197 debug("%s: Could not detect CROS-EC version\n", __func__);
1198 return -CROS_EC_ERR_CHECK_VERSION;
1199 }
1200
1201 if (cros_ec_read_id(dev, id, sizeof(id))) {
1202 debug("%s: Could not read KBC ID\n", __func__);
1203 return -CROS_EC_ERR_READ_ID;
1204 }
1205
1206 /* Remember this device for use by the cros_ec command */
Simon Glass84d6cbd2014-10-13 23:42:14 -06001207 *cros_ecp = dev;
1208#ifndef CONFIG_DM_CROS_EC
1209 last_dev = dev;
1210#endif
Hung-ying Tyan88364382013-05-15 18:27:28 +08001211 debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
1212
1213 return 0;
1214}
Simon Glass84d6cbd2014-10-13 23:42:14 -06001215#endif
Hung-ying Tyan88364382013-05-15 18:27:28 +08001216
Hung-ying Tyan88364382013-05-15 18:27:28 +08001217int cros_ec_decode_region(int argc, char * const argv[])
1218{
1219 if (argc > 0) {
1220 if (0 == strcmp(*argv, "rw"))
1221 return EC_FLASH_REGION_RW;
1222 else if (0 == strcmp(*argv, "ro"))
1223 return EC_FLASH_REGION_RO;
1224
1225 debug("%s: Invalid region '%s'\n", __func__, *argv);
1226 } else {
1227 debug("%s: Missing region parameter\n", __func__);
1228 }
1229
1230 return -1;
1231}
1232
Simon Glass84d6cbd2014-10-13 23:42:14 -06001233int cros_ec_decode_ec_flash(const void *blob, int node,
1234 struct fdt_cros_ec *config)
Simon Glassd7f25f32014-02-27 13:26:03 -07001235{
Simon Glass84d6cbd2014-10-13 23:42:14 -06001236 int flash_node;
Simon Glassd7f25f32014-02-27 13:26:03 -07001237
1238 flash_node = fdt_subnode_offset(blob, node, "flash");
1239 if (flash_node < 0) {
1240 debug("Failed to find flash node\n");
1241 return -1;
1242 }
1243
1244 if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
1245 &config->flash)) {
1246 debug("Failed to decode flash node in chrome-ec'\n");
1247 return -1;
1248 }
1249
1250 config->flash_erase_value = fdtdec_get_int(blob, flash_node,
1251 "erase-value", -1);
1252 for (node = fdt_first_subnode(blob, flash_node); node >= 0;
1253 node = fdt_next_subnode(blob, node)) {
1254 const char *name = fdt_get_name(blob, node, NULL);
1255 enum ec_flash_region region;
1256
1257 if (0 == strcmp(name, "ro")) {
1258 region = EC_FLASH_REGION_RO;
1259 } else if (0 == strcmp(name, "rw")) {
1260 region = EC_FLASH_REGION_RW;
1261 } else if (0 == strcmp(name, "wp-ro")) {
1262 region = EC_FLASH_REGION_WP_RO;
1263 } else {
1264 debug("Unknown EC flash region name '%s'\n", name);
1265 return -1;
1266 }
1267
1268 if (fdtdec_read_fmap_entry(blob, node, "reg",
1269 &config->region[region])) {
1270 debug("Failed to decode flash region in chrome-ec'\n");
1271 return -1;
1272 }
1273 }
1274
1275 return 0;
1276}
1277
Simon Glassb2a668b2014-02-27 13:26:14 -07001278int cros_ec_i2c_xfer(struct cros_ec_dev *dev, uchar chip, uint addr,
1279 int alen, uchar *buffer, int len, int is_read)
1280{
1281 union {
1282 struct ec_params_i2c_passthru p;
1283 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1284 } params;
1285 union {
1286 struct ec_response_i2c_passthru r;
1287 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1288 } response;
1289 struct ec_params_i2c_passthru *p = &params.p;
1290 struct ec_response_i2c_passthru *r = &response.r;
1291 struct ec_params_i2c_passthru_msg *msg = p->msg;
1292 uint8_t *pdata;
1293 int read_len, write_len;
1294 int size;
1295 int rv;
1296
1297 p->port = 0;
1298
1299 if (alen != 1) {
1300 printf("Unsupported address length %d\n", alen);
1301 return -1;
1302 }
1303 if (is_read) {
1304 read_len = len;
1305 write_len = alen;
1306 p->num_msgs = 2;
1307 } else {
1308 read_len = 0;
1309 write_len = alen + len;
1310 p->num_msgs = 1;
1311 }
1312
1313 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1314 if (size + write_len > sizeof(params)) {
1315 puts("Params too large for buffer\n");
1316 return -1;
1317 }
1318 if (sizeof(*r) + read_len > sizeof(response)) {
1319 puts("Read length too big for buffer\n");
1320 return -1;
1321 }
1322
1323 /* Create a message to write the register address and optional data */
1324 pdata = (uint8_t *)p + size;
1325 msg->addr_flags = chip;
1326 msg->len = write_len;
1327 pdata[0] = addr;
1328 if (!is_read)
1329 memcpy(pdata + 1, buffer, len);
1330 msg++;
1331
1332 if (read_len) {
1333 msg->addr_flags = chip | EC_I2C_FLAG_READ;
1334 msg->len = read_len;
1335 }
1336
1337 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, size + write_len,
1338 r, sizeof(*r) + read_len);
1339 if (rv < 0)
1340 return rv;
1341
1342 /* Parse response */
1343 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1344 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1345 return -1;
1346 }
1347
1348 if (rv < sizeof(*r) + read_len) {
1349 puts("Truncated read response\n");
1350 return -1;
1351 }
1352
1353 if (read_len)
1354 memcpy(buffer, r->data, read_len);
1355
1356 return 0;
1357}
1358
Simon Glass1c266b92014-02-27 13:26:06 -07001359#ifdef CONFIG_CMD_CROS_EC
1360
Hung-ying Tyan88364382013-05-15 18:27:28 +08001361/**
1362 * Perform a flash read or write command
1363 *
1364 * @param dev CROS-EC device to read/write
1365 * @param is_write 1 do to a write, 0 to do a read
1366 * @param argc Number of arguments
1367 * @param argv Arguments (2 is region, 3 is address)
1368 * @return 0 for ok, 1 for a usage error or -ve for ec command error
1369 * (negative EC_RES_...)
1370 */
1371static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
1372 char * const argv[])
1373{
1374 uint32_t offset, size = -1U, region_size;
1375 unsigned long addr;
1376 char *endp;
1377 int region;
1378 int ret;
1379
1380 region = cros_ec_decode_region(argc - 2, argv + 2);
1381 if (region == -1)
1382 return 1;
1383 if (argc < 4)
1384 return 1;
1385 addr = simple_strtoul(argv[3], &endp, 16);
1386 if (*argv[3] == 0 || *endp != 0)
1387 return 1;
1388 if (argc > 4) {
1389 size = simple_strtoul(argv[4], &endp, 16);
1390 if (*argv[4] == 0 || *endp != 0)
1391 return 1;
1392 }
1393
1394 ret = cros_ec_flash_offset(dev, region, &offset, &region_size);
1395 if (ret) {
1396 debug("%s: Could not read region info\n", __func__);
1397 return ret;
1398 }
1399 if (size == -1U)
1400 size = region_size;
1401
1402 ret = is_write ?
1403 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
1404 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
1405 if (ret) {
1406 debug("%s: Could not %s region\n", __func__,
1407 is_write ? "write" : "read");
1408 return ret;
1409 }
1410
1411 return 0;
1412}
1413
Simon Glassb2a668b2014-02-27 13:26:14 -07001414/**
1415 * get_alen() - Small parser helper function to get address length
1416 *
1417 * Returns the address length.
1418 */
1419static uint get_alen(char *arg)
1420{
1421 int j;
1422 int alen;
1423
1424 alen = 1;
1425 for (j = 0; j < 8; j++) {
1426 if (arg[j] == '.') {
1427 alen = arg[j+1] - '0';
1428 break;
1429 } else if (arg[j] == '\0') {
1430 break;
1431 }
1432 }
1433 return alen;
1434}
1435
1436#define DISP_LINE_LEN 16
1437
1438/*
1439 * TODO(sjg@chromium.org): This code copied almost verbatim from cmd_i2c.c
1440 * so we can remove it later.
1441 */
1442static int cros_ec_i2c_md(struct cros_ec_dev *dev, int flag, int argc,
1443 char * const argv[])
1444{
1445 u_char chip;
1446 uint addr, alen, length = 0x10;
1447 int j, nbytes, linebytes;
1448
1449 if (argc < 2)
1450 return CMD_RET_USAGE;
1451
1452 if (1 || (flag & CMD_FLAG_REPEAT) == 0) {
1453 /*
1454 * New command specified.
1455 */
1456
1457 /*
1458 * I2C chip address
1459 */
1460 chip = simple_strtoul(argv[0], NULL, 16);
1461
1462 /*
1463 * I2C data address within the chip. This can be 1 or
1464 * 2 bytes long. Some day it might be 3 bytes long :-).
1465 */
1466 addr = simple_strtoul(argv[1], NULL, 16);
1467 alen = get_alen(argv[1]);
1468 if (alen > 3)
1469 return CMD_RET_USAGE;
1470
1471 /*
1472 * If another parameter, it is the length to display.
1473 * Length is the number of objects, not number of bytes.
1474 */
1475 if (argc > 2)
1476 length = simple_strtoul(argv[2], NULL, 16);
1477 }
1478
1479 /*
1480 * Print the lines.
1481 *
1482 * We buffer all read data, so we can make sure data is read only
1483 * once.
1484 */
1485 nbytes = length;
1486 do {
1487 unsigned char linebuf[DISP_LINE_LEN];
1488 unsigned char *cp;
1489
1490 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
1491
1492 if (cros_ec_i2c_xfer(dev, chip, addr, alen, linebuf, linebytes,
1493 1))
1494 puts("Error reading the chip.\n");
1495 else {
1496 printf("%04x:", addr);
1497 cp = linebuf;
1498 for (j = 0; j < linebytes; j++) {
1499 printf(" %02x", *cp++);
1500 addr++;
1501 }
1502 puts(" ");
1503 cp = linebuf;
1504 for (j = 0; j < linebytes; j++) {
1505 if ((*cp < 0x20) || (*cp > 0x7e))
1506 puts(".");
1507 else
1508 printf("%c", *cp);
1509 cp++;
1510 }
1511 putc('\n');
1512 }
1513 nbytes -= linebytes;
1514 } while (nbytes > 0);
1515
1516 return 0;
1517}
1518
1519static int cros_ec_i2c_mw(struct cros_ec_dev *dev, int flag, int argc,
1520 char * const argv[])
1521{
1522 uchar chip;
1523 ulong addr;
1524 uint alen;
1525 uchar byte;
1526 int count;
1527
1528 if ((argc < 3) || (argc > 4))
1529 return CMD_RET_USAGE;
1530
1531 /*
1532 * Chip is always specified.
1533 */
1534 chip = simple_strtoul(argv[0], NULL, 16);
1535
1536 /*
1537 * Address is always specified.
1538 */
1539 addr = simple_strtoul(argv[1], NULL, 16);
1540 alen = get_alen(argv[1]);
1541 if (alen > 3)
1542 return CMD_RET_USAGE;
1543
1544 /*
1545 * Value to write is always specified.
1546 */
1547 byte = simple_strtoul(argv[2], NULL, 16);
1548
1549 /*
1550 * Optional count
1551 */
1552 if (argc == 4)
1553 count = simple_strtoul(argv[3], NULL, 16);
1554 else
1555 count = 1;
1556
1557 while (count-- > 0) {
1558 if (cros_ec_i2c_xfer(dev, chip, addr++, alen, &byte, 1, 0))
1559 puts("Error writing the chip.\n");
1560 /*
1561 * Wait for the write to complete. The write can take
1562 * up to 10mSec (we allow a little more time).
1563 */
1564/*
1565 * No write delay with FRAM devices.
1566 */
1567#if !defined(CONFIG_SYS_I2C_FRAM)
1568 udelay(11000);
1569#endif
1570 }
1571
1572 return 0;
1573}
1574
1575/* Temporary code until we have driver model and can use the i2c command */
1576static int cros_ec_i2c_passthrough(struct cros_ec_dev *dev, int flag,
1577 int argc, char * const argv[])
1578{
1579 const char *cmd;
1580
1581 if (argc < 1)
1582 return CMD_RET_USAGE;
1583 cmd = *argv++;
1584 argc--;
1585 if (0 == strcmp("md", cmd))
1586 cros_ec_i2c_md(dev, flag, argc, argv);
1587 else if (0 == strcmp("mw", cmd))
1588 cros_ec_i2c_mw(dev, flag, argc, argv);
1589 else
1590 return CMD_RET_USAGE;
1591
1592 return 0;
1593}
1594
Hung-ying Tyan88364382013-05-15 18:27:28 +08001595static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1596{
Simon Glass84d6cbd2014-10-13 23:42:14 -06001597 struct cros_ec_dev *dev;
1598#ifdef CONFIG_DM_CROS_EC
1599 struct udevice *udev;
1600#endif
Hung-ying Tyan88364382013-05-15 18:27:28 +08001601 const char *cmd;
1602 int ret = 0;
1603
1604 if (argc < 2)
1605 return CMD_RET_USAGE;
1606
1607 cmd = argv[1];
1608 if (0 == strcmp("init", cmd)) {
Simon Glass84d6cbd2014-10-13 23:42:14 -06001609#ifndef CONFIG_DM_CROS_EC
Hung-ying Tyan88364382013-05-15 18:27:28 +08001610 ret = cros_ec_init(gd->fdt_blob, &dev);
1611 if (ret) {
1612 printf("Could not init cros_ec device (err %d)\n", ret);
1613 return 1;
1614 }
Simon Glass84d6cbd2014-10-13 23:42:14 -06001615#endif
Hung-ying Tyan88364382013-05-15 18:27:28 +08001616 return 0;
1617 }
1618
Simon Glass84d6cbd2014-10-13 23:42:14 -06001619#ifdef CONFIG_DM_CROS_EC
1620 ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
1621 if (ret) {
1622 printf("Cannot get cros-ec device (err=%d)\n", ret);
1623 return 1;
1624 }
Simon Glasse564f052015-03-05 12:25:20 -07001625 dev = dev_get_uclass_priv(udev);
Simon Glass84d6cbd2014-10-13 23:42:14 -06001626#else
Hung-ying Tyan88364382013-05-15 18:27:28 +08001627 /* Just use the last allocated device; there should be only one */
1628 if (!last_dev) {
1629 printf("No CROS-EC device available\n");
1630 return 1;
1631 }
Simon Glass84d6cbd2014-10-13 23:42:14 -06001632 dev = last_dev;
1633#endif
Hung-ying Tyan88364382013-05-15 18:27:28 +08001634 if (0 == strcmp("id", cmd)) {
1635 char id[MSG_BYTES];
1636
1637 if (cros_ec_read_id(dev, id, sizeof(id))) {
1638 debug("%s: Could not read KBC ID\n", __func__);
1639 return 1;
1640 }
1641 printf("%s\n", id);
1642 } else if (0 == strcmp("info", cmd)) {
Simon Glass836bb6e2014-02-27 13:26:07 -07001643 struct ec_response_mkbp_info info;
Hung-ying Tyan88364382013-05-15 18:27:28 +08001644
1645 if (cros_ec_info(dev, &info)) {
1646 debug("%s: Could not read KBC info\n", __func__);
1647 return 1;
1648 }
1649 printf("rows = %u\n", info.rows);
1650 printf("cols = %u\n", info.cols);
1651 printf("switches = %#x\n", info.switches);
1652 } else if (0 == strcmp("curimage", cmd)) {
1653 enum ec_current_image image;
1654
1655 if (cros_ec_read_current_image(dev, &image)) {
1656 debug("%s: Could not read KBC image\n", __func__);
1657 return 1;
1658 }
1659 printf("%d\n", image);
1660 } else if (0 == strcmp("hash", cmd)) {
1661 struct ec_response_vboot_hash hash;
1662 int i;
1663
1664 if (cros_ec_read_hash(dev, &hash)) {
1665 debug("%s: Could not read KBC hash\n", __func__);
1666 return 1;
1667 }
1668
1669 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
1670 printf("type: SHA-256\n");
1671 else
1672 printf("type: %d\n", hash.hash_type);
1673
1674 printf("offset: 0x%08x\n", hash.offset);
1675 printf("size: 0x%08x\n", hash.size);
1676
1677 printf("digest: ");
1678 for (i = 0; i < hash.digest_size; i++)
1679 printf("%02x", hash.hash_digest[i]);
1680 printf("\n");
1681 } else if (0 == strcmp("reboot", cmd)) {
1682 int region;
1683 enum ec_reboot_cmd cmd;
1684
1685 if (argc >= 3 && !strcmp(argv[2], "cold"))
1686 cmd = EC_REBOOT_COLD;
1687 else {
1688 region = cros_ec_decode_region(argc - 2, argv + 2);
1689 if (region == EC_FLASH_REGION_RO)
1690 cmd = EC_REBOOT_JUMP_RO;
1691 else if (region == EC_FLASH_REGION_RW)
1692 cmd = EC_REBOOT_JUMP_RW;
1693 else
1694 return CMD_RET_USAGE;
1695 }
1696
1697 if (cros_ec_reboot(dev, cmd, 0)) {
1698 debug("%s: Could not reboot KBC\n", __func__);
1699 return 1;
1700 }
1701 } else if (0 == strcmp("events", cmd)) {
1702 uint32_t events;
1703
1704 if (cros_ec_get_host_events(dev, &events)) {
1705 debug("%s: Could not read host events\n", __func__);
1706 return 1;
1707 }
1708 printf("0x%08x\n", events);
1709 } else if (0 == strcmp("clrevents", cmd)) {
1710 uint32_t events = 0x7fffffff;
1711
1712 if (argc >= 3)
1713 events = simple_strtol(argv[2], NULL, 0);
1714
1715 if (cros_ec_clear_host_events(dev, events)) {
1716 debug("%s: Could not clear host events\n", __func__);
1717 return 1;
1718 }
1719 } else if (0 == strcmp("read", cmd)) {
1720 ret = do_read_write(dev, 0, argc, argv);
1721 if (ret > 0)
1722 return CMD_RET_USAGE;
1723 } else if (0 == strcmp("write", cmd)) {
1724 ret = do_read_write(dev, 1, argc, argv);
1725 if (ret > 0)
1726 return CMD_RET_USAGE;
1727 } else if (0 == strcmp("erase", cmd)) {
1728 int region = cros_ec_decode_region(argc - 2, argv + 2);
1729 uint32_t offset, size;
1730
1731 if (region == -1)
1732 return CMD_RET_USAGE;
1733 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
1734 debug("%s: Could not read region info\n", __func__);
1735 ret = -1;
1736 } else {
1737 ret = cros_ec_flash_erase(dev, offset, size);
1738 if (ret) {
1739 debug("%s: Could not erase region\n",
1740 __func__);
1741 }
1742 }
1743 } else if (0 == strcmp("regioninfo", cmd)) {
1744 int region = cros_ec_decode_region(argc - 2, argv + 2);
1745 uint32_t offset, size;
1746
1747 if (region == -1)
1748 return CMD_RET_USAGE;
1749 ret = cros_ec_flash_offset(dev, region, &offset, &size);
1750 if (ret) {
1751 debug("%s: Could not read region info\n", __func__);
1752 } else {
1753 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
1754 "RO" : "RW");
1755 printf("Offset: %x\n", offset);
1756 printf("Size: %x\n", size);
1757 }
1758 } else if (0 == strcmp("vbnvcontext", cmd)) {
1759 uint8_t block[EC_VBNV_BLOCK_SIZE];
1760 char buf[3];
1761 int i, len;
1762 unsigned long result;
1763
1764 if (argc <= 2) {
1765 ret = cros_ec_read_vbnvcontext(dev, block);
1766 if (!ret) {
1767 printf("vbnv_block: ");
1768 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
1769 printf("%02x", block[i]);
1770 putc('\n');
1771 }
1772 } else {
1773 /*
1774 * TODO(clchiou): Move this to a utility function as
1775 * cmd_spi might want to call it.
1776 */
1777 memset(block, 0, EC_VBNV_BLOCK_SIZE);
1778 len = strlen(argv[2]);
1779 buf[2] = '\0';
1780 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
1781 if (i * 2 >= len)
1782 break;
1783 buf[0] = argv[2][i * 2];
1784 if (i * 2 + 1 >= len)
1785 buf[1] = '0';
1786 else
1787 buf[1] = argv[2][i * 2 + 1];
1788 strict_strtoul(buf, 16, &result);
1789 block[i] = result;
1790 }
1791 ret = cros_ec_write_vbnvcontext(dev, block);
1792 }
1793 if (ret) {
1794 debug("%s: Could not %s VbNvContext\n", __func__,
1795 argc <= 2 ? "read" : "write");
1796 }
1797 } else if (0 == strcmp("test", cmd)) {
1798 int result = cros_ec_test(dev);
1799
1800 if (result)
1801 printf("Test failed with error %d\n", result);
1802 else
1803 puts("Test passed\n");
1804 } else if (0 == strcmp("version", cmd)) {
1805 struct ec_response_get_version *p;
1806 char *build_string;
1807
1808 ret = cros_ec_read_version(dev, &p);
1809 if (!ret) {
1810 /* Print versions */
1811 printf("RO version: %1.*s\n",
Simon Glass2ab83f02014-02-27 13:26:11 -07001812 (int)sizeof(p->version_string_ro),
Hung-ying Tyan88364382013-05-15 18:27:28 +08001813 p->version_string_ro);
1814 printf("RW version: %1.*s\n",
Simon Glass2ab83f02014-02-27 13:26:11 -07001815 (int)sizeof(p->version_string_rw),
Hung-ying Tyan88364382013-05-15 18:27:28 +08001816 p->version_string_rw);
1817 printf("Firmware copy: %s\n",
1818 (p->current_image <
1819 ARRAY_SIZE(ec_current_image_name) ?
1820 ec_current_image_name[p->current_image] :
1821 "?"));
1822 ret = cros_ec_read_build_info(dev, &build_string);
1823 if (!ret)
1824 printf("Build info: %s\n", build_string);
1825 }
1826 } else if (0 == strcmp("ldo", cmd)) {
1827 uint8_t index, state;
1828 char *endp;
1829
1830 if (argc < 3)
1831 return CMD_RET_USAGE;
1832 index = simple_strtoul(argv[2], &endp, 10);
1833 if (*argv[2] == 0 || *endp != 0)
1834 return CMD_RET_USAGE;
1835 if (argc > 3) {
1836 state = simple_strtoul(argv[3], &endp, 10);
1837 if (*argv[3] == 0 || *endp != 0)
1838 return CMD_RET_USAGE;
1839 ret = cros_ec_set_ldo(dev, index, state);
1840 } else {
1841 ret = cros_ec_get_ldo(dev, index, &state);
1842 if (!ret) {
1843 printf("LDO%d: %s\n", index,
1844 state == EC_LDO_STATE_ON ?
1845 "on" : "off");
1846 }
1847 }
1848
1849 if (ret) {
1850 debug("%s: Could not access LDO%d\n", __func__, index);
1851 return ret;
1852 }
Simon Glassb2a668b2014-02-27 13:26:14 -07001853 } else if (0 == strcmp("i2c", cmd)) {
1854 ret = cros_ec_i2c_passthrough(dev, flag, argc - 2, argv + 2);
Hung-ying Tyan88364382013-05-15 18:27:28 +08001855 } else {
1856 return CMD_RET_USAGE;
1857 }
1858
1859 if (ret < 0) {
1860 printf("Error: CROS-EC command failed (error %d)\n", ret);
1861 ret = 1;
1862 }
1863
1864 return ret;
1865}
1866
1867U_BOOT_CMD(
Simon Glassb2a668b2014-02-27 13:26:14 -07001868 crosec, 6, 1, do_cros_ec,
Hung-ying Tyan88364382013-05-15 18:27:28 +08001869 "CROS-EC utility command",
1870 "init Re-init CROS-EC (done on startup automatically)\n"
1871 "crosec id Read CROS-EC ID\n"
1872 "crosec info Read CROS-EC info\n"
1873 "crosec curimage Read CROS-EC current image\n"
1874 "crosec hash Read CROS-EC hash\n"
1875 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
1876 "crosec events Read CROS-EC host events\n"
1877 "crosec clrevents [mask] Clear CROS-EC host events\n"
1878 "crosec regioninfo <ro|rw> Read image info\n"
1879 "crosec erase <ro|rw> Erase EC image\n"
1880 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
1881 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
1882 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
1883 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
1884 "crosec test run tests on cros_ec\n"
Simon Glassb2a668b2014-02-27 13:26:14 -07001885 "crosec version Read CROS-EC version\n"
1886 "crosec i2c md chip address[.0, .1, .2] [# of objects] - read from I2C passthru\n"
1887 "crosec i2c mw chip address[.0, .1, .2] value [count] - write to I2C passthru (fill)"
Hung-ying Tyan88364382013-05-15 18:27:28 +08001888);
1889#endif
Simon Glass84d6cbd2014-10-13 23:42:14 -06001890
1891#ifdef CONFIG_DM_CROS_EC
1892UCLASS_DRIVER(cros_ec) = {
1893 .id = UCLASS_CROS_EC,
1894 .name = "cros_ec",
1895 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
1896};
1897#endif