blob: e2027ea5d208f16575e46f7510126536162d8dbc [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>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090025#include <linux/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
Hung-ying Tyan88364382013-05-15 18:27:28 +080044DECLARE_GLOBAL_DATA_PTR;
45
Hung-ying Tyan88364382013-05-15 18:27:28 +080046void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
47{
48#ifdef DEBUG
49 int i;
50
51 printf("%s: ", name);
52 if (cmd != -1)
53 printf("cmd=%#x: ", cmd);
54 for (i = 0; i < len; i++)
55 printf("%02x ", data[i]);
56 printf("\n");
57#endif
58}
59
60/*
61 * Calculate a simple 8-bit checksum of a data block
62 *
63 * @param data Data block to checksum
64 * @param size Size of data block in bytes
65 * @return checksum value (0 to 255)
66 */
67int cros_ec_calc_checksum(const uint8_t *data, int size)
68{
69 int csum, i;
70
71 for (i = csum = 0; i < size; i++)
72 csum += data[i];
73 return csum & 0xff;
74}
75
Simon Glass2d8ede52014-02-27 13:26:09 -070076/**
77 * Create a request packet for protocol version 3.
78 *
79 * The packet is stored in the device's internal output buffer.
80 *
81 * @param dev CROS-EC device
82 * @param cmd Command to send (EC_CMD_...)
83 * @param cmd_version Version of command to send (EC_VER_...)
84 * @param dout Output data (may be NULL If dout_len=0)
85 * @param dout_len Size of output data in bytes
86 * @return packet size in bytes, or <0 if error.
87 */
88static int create_proto3_request(struct cros_ec_dev *dev,
89 int cmd, int cmd_version,
90 const void *dout, int dout_len)
91{
92 struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
93 int out_bytes = dout_len + sizeof(*rq);
94
95 /* Fail if output size is too big */
96 if (out_bytes > (int)sizeof(dev->dout)) {
97 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
98 return -EC_RES_REQUEST_TRUNCATED;
99 }
100
101 /* Fill in request packet */
102 rq->struct_version = EC_HOST_REQUEST_VERSION;
103 rq->checksum = 0;
104 rq->command = cmd;
105 rq->command_version = cmd_version;
106 rq->reserved = 0;
107 rq->data_len = dout_len;
108
109 /* Copy data after header */
110 memcpy(rq + 1, dout, dout_len);
111
112 /* Write checksum field so the entire packet sums to 0 */
113 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
114
115 cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
116
117 /* Return size of request packet */
118 return out_bytes;
119}
120
121/**
122 * Prepare the device to receive a protocol version 3 response.
123 *
124 * @param dev CROS-EC device
125 * @param din_len Maximum size of response in bytes
126 * @return maximum expected number of bytes in response, or <0 if error.
127 */
128static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
129{
130 int in_bytes = din_len + sizeof(struct ec_host_response);
131
132 /* Fail if input size is too big */
133 if (in_bytes > (int)sizeof(dev->din)) {
134 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
135 return -EC_RES_RESPONSE_TOO_BIG;
136 }
137
138 /* Return expected size of response packet */
139 return in_bytes;
140}
141
142/**
143 * Handle a protocol version 3 response packet.
144 *
145 * The packet must already be stored in the device's internal input buffer.
146 *
147 * @param dev CROS-EC device
148 * @param dinp Returns pointer to response data
149 * @param din_len Maximum size of response in bytes
Simon Glass8bbb38b2015-01-25 08:27:17 -0700150 * @return number of bytes of response data, or <0 if error. Note that error
151 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
152 * overlap!)
Simon Glass2d8ede52014-02-27 13:26:09 -0700153 */
154static int handle_proto3_response(struct cros_ec_dev *dev,
155 uint8_t **dinp, int din_len)
156{
157 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
158 int in_bytes;
159 int csum;
160
161 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
162
163 /* Check input data */
164 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
165 debug("%s: EC response version mismatch\n", __func__);
166 return -EC_RES_INVALID_RESPONSE;
167 }
168
169 if (rs->reserved) {
170 debug("%s: EC response reserved != 0\n", __func__);
171 return -EC_RES_INVALID_RESPONSE;
172 }
173
174 if (rs->data_len > din_len) {
175 debug("%s: EC returned too much data\n", __func__);
176 return -EC_RES_RESPONSE_TOO_BIG;
177 }
178
179 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
180
181 /* Update in_bytes to actual data size */
182 in_bytes = sizeof(*rs) + rs->data_len;
183
184 /* Verify checksum */
185 csum = cros_ec_calc_checksum(dev->din, in_bytes);
186 if (csum) {
187 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
188 csum);
189 return -EC_RES_INVALID_CHECKSUM;
190 }
191
192 /* Return error result, if any */
193 if (rs->result)
194 return -(int)rs->result;
195
196 /* If we're still here, set response data pointer and return length */
197 *dinp = (uint8_t *)(rs + 1);
198
199 return rs->data_len;
200}
201
202static int send_command_proto3(struct cros_ec_dev *dev,
203 int cmd, int cmd_version,
204 const void *dout, int dout_len,
205 uint8_t **dinp, int din_len)
206{
Simon Glass84d6cbd2014-10-13 23:42:14 -0600207 struct dm_cros_ec_ops *ops;
Simon Glass2d8ede52014-02-27 13:26:09 -0700208 int out_bytes, in_bytes;
209 int rv;
210
211 /* Create request packet */
212 out_bytes = create_proto3_request(dev, cmd, cmd_version,
213 dout, dout_len);
214 if (out_bytes < 0)
215 return out_bytes;
216
217 /* Prepare response buffer */
218 in_bytes = prepare_proto3_response_buffer(dev, din_len);
219 if (in_bytes < 0)
220 return in_bytes;
221
Simon Glass84d6cbd2014-10-13 23:42:14 -0600222 ops = dm_cros_ec_get_ops(dev->dev);
Simon Glass8bbb38b2015-01-25 08:27:17 -0700223 rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS;
Simon Glass2d8ede52014-02-27 13:26:09 -0700224 if (rv < 0)
225 return rv;
226
227 /* Process the response */
228 return handle_proto3_response(dev, dinp, din_len);
229}
230
Hung-ying Tyan88364382013-05-15 18:27:28 +0800231static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
232 const void *dout, int dout_len,
233 uint8_t **dinp, int din_len)
234{
Simon Glass84d6cbd2014-10-13 23:42:14 -0600235 struct dm_cros_ec_ops *ops;
Simon Glass2d8ede52014-02-27 13:26:09 -0700236 int ret = -1;
237
238 /* Handle protocol version 3 support */
239 if (dev->protocol_version == 3) {
240 return send_command_proto3(dev, cmd, cmd_version,
241 dout, dout_len, dinp, din_len);
242 }
Hung-ying Tyan88364382013-05-15 18:27:28 +0800243
Simon Glass84d6cbd2014-10-13 23:42:14 -0600244 ops = dm_cros_ec_get_ops(dev->dev);
245 ret = ops->command(dev->dev, cmd, cmd_version,
246 (const uint8_t *)dout, dout_len, dinp, din_len);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800247
248 return ret;
249}
250
251/**
252 * Send a command to the CROS-EC device and return the reply.
253 *
254 * The device's internal input/output buffers are used.
255 *
256 * @param dev CROS-EC device
257 * @param cmd Command to send (EC_CMD_...)
258 * @param cmd_version Version of command to send (EC_VER_...)
259 * @param dout Output data (may be NULL If dout_len=0)
260 * @param dout_len Size of output data in bytes
261 * @param dinp Response data (may be NULL If din_len=0).
262 * If not NULL, it will be updated to point to the data
263 * and will always be double word aligned (64-bits)
264 * @param din_len Maximum size of response in bytes
Simon Glass8bbb38b2015-01-25 08:27:17 -0700265 * @return number of bytes in response, or -ve on error
Hung-ying Tyan88364382013-05-15 18:27:28 +0800266 */
267static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
268 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
269 int din_len)
270{
Simon Glass2ab83f02014-02-27 13:26:11 -0700271 uint8_t *din = NULL;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800272 int len;
273
Hung-ying Tyan88364382013-05-15 18:27:28 +0800274 len = send_command(dev, cmd, cmd_version, dout, dout_len,
275 &din, din_len);
276
277 /* If the command doesn't complete, wait a while */
278 if (len == -EC_RES_IN_PROGRESS) {
Simon Glass2ab83f02014-02-27 13:26:11 -0700279 struct ec_response_get_comms_status *resp = NULL;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800280 ulong start;
281
282 /* Wait for command to complete */
283 start = get_timer(0);
284 do {
285 int ret;
286
287 mdelay(50); /* Insert some reasonable delay */
288 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
289 NULL, 0,
290 (uint8_t **)&resp, sizeof(*resp));
291 if (ret < 0)
292 return ret;
293
294 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
295 debug("%s: Command %#02x timeout\n",
296 __func__, cmd);
297 return -EC_RES_TIMEOUT;
298 }
299 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
300
301 /* OK it completed, so read the status response */
302 /* not sure why it was 0 for the last argument */
303 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
304 NULL, 0, &din, din_len);
305 }
306
Simon Glasse907bf22017-05-18 20:09:22 -0600307 debug("%s: len=%d, din=%p\n", __func__, len, din);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800308 if (dinp) {
309 /* If we have any data to return, it must be 64bit-aligned */
310 assert(len <= 0 || !((uintptr_t)din & 7));
311 *dinp = din;
312 }
313
314 return len;
315}
316
317/**
318 * Send a command to the CROS-EC device and return the reply.
319 *
320 * The device's internal input/output buffers are used.
321 *
322 * @param dev CROS-EC device
323 * @param cmd Command to send (EC_CMD_...)
324 * @param cmd_version Version of command to send (EC_VER_...)
325 * @param dout Output data (may be NULL If dout_len=0)
326 * @param dout_len Size of output data in bytes
327 * @param din Response data (may be NULL If din_len=0).
328 * It not NULL, it is a place for ec_command() to copy the
329 * data to.
330 * @param din_len Maximum size of response in bytes
Simon Glass8bbb38b2015-01-25 08:27:17 -0700331 * @return number of bytes in response, or -ve on error
Hung-ying Tyan88364382013-05-15 18:27:28 +0800332 */
333static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
334 const void *dout, int dout_len,
335 void *din, int din_len)
336{
337 uint8_t *in_buffer;
338 int len;
339
340 assert((din_len == 0) || din);
341 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
342 &in_buffer, din_len);
343 if (len > 0) {
344 /*
345 * If we were asked to put it somewhere, do so, otherwise just
346 * disregard the result.
347 */
348 if (din && in_buffer) {
349 assert(len <= din_len);
350 memmove(din, in_buffer, len);
351 }
352 }
353 return len;
354}
355
Simon Glass745009c2015-10-18 21:17:14 -0600356int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800357{
Simon Glass745009c2015-10-18 21:17:14 -0600358 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
359
360 if (ec_command(cdev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
Simon Glass2ab83f02014-02-27 13:26:11 -0700361 sizeof(scan->data)) != sizeof(scan->data))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800362 return -1;
363
364 return 0;
365}
366
367int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
368{
369 struct ec_response_get_version *r;
370
371 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
Simon Glass2ab83f02014-02-27 13:26:11 -0700372 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800373 return -1;
374
Simon Glass2ab83f02014-02-27 13:26:11 -0700375 if (maxlen > (int)sizeof(r->version_string_ro))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800376 maxlen = sizeof(r->version_string_ro);
377
378 switch (r->current_image) {
379 case EC_IMAGE_RO:
380 memcpy(id, r->version_string_ro, maxlen);
381 break;
382 case EC_IMAGE_RW:
383 memcpy(id, r->version_string_rw, maxlen);
384 break;
385 default:
386 return -1;
387 }
388
389 id[maxlen - 1] = '\0';
390 return 0;
391}
392
393int cros_ec_read_version(struct cros_ec_dev *dev,
394 struct ec_response_get_version **versionp)
395{
396 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
397 (uint8_t **)versionp, sizeof(**versionp))
Simon Glass2ab83f02014-02-27 13:26:11 -0700398 != sizeof(**versionp))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800399 return -1;
400
401 return 0;
402}
403
404int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
405{
406 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
Simon Glass836bb6e2014-02-27 13:26:07 -0700407 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800408 return -1;
409
410 return 0;
411}
412
413int cros_ec_read_current_image(struct cros_ec_dev *dev,
414 enum ec_current_image *image)
415{
416 struct ec_response_get_version *r;
417
418 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
Simon Glass2ab83f02014-02-27 13:26:11 -0700419 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800420 return -1;
421
422 *image = r->current_image;
423 return 0;
424}
425
426static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
427 struct ec_response_vboot_hash *hash)
428{
429 struct ec_params_vboot_hash p;
430 ulong start;
431
432 start = get_timer(0);
433 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
434 mdelay(50); /* Insert some reasonable delay */
435
436 p.cmd = EC_VBOOT_HASH_GET;
437 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
438 hash, sizeof(*hash)) < 0)
439 return -1;
440
441 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
442 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
443 return -EC_RES_TIMEOUT;
444 }
445 }
446 return 0;
447}
448
449
450int cros_ec_read_hash(struct cros_ec_dev *dev,
451 struct ec_response_vboot_hash *hash)
452{
453 struct ec_params_vboot_hash p;
454 int rv;
455
456 p.cmd = EC_VBOOT_HASH_GET;
457 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
458 hash, sizeof(*hash)) < 0)
459 return -1;
460
461 /* If the EC is busy calculating the hash, fidget until it's done. */
462 rv = cros_ec_wait_on_hash_done(dev, hash);
463 if (rv)
464 return rv;
465
466 /* If the hash is valid, we're done. Otherwise, we have to kick it off
467 * again and wait for it to complete. Note that we explicitly assume
468 * that hashing zero bytes is always wrong, even though that would
469 * produce a valid hash value. */
470 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
471 return 0;
472
473 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
474 __func__, hash->status, hash->size);
475
Simon Glass836bb6e2014-02-27 13:26:07 -0700476 p.cmd = EC_VBOOT_HASH_START;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800477 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
478 p.nonce_size = 0;
479 p.offset = EC_VBOOT_HASH_OFFSET_RW;
480
481 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
482 hash, sizeof(*hash)) < 0)
483 return -1;
484
485 rv = cros_ec_wait_on_hash_done(dev, hash);
486 if (rv)
487 return rv;
488
489 debug("%s: hash done\n", __func__);
490
491 return 0;
492}
493
494static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
495{
496 struct ec_params_vboot_hash p;
497 struct ec_response_vboot_hash *hash;
498
499 /* We don't have an explict command for the EC to discard its current
500 * hash value, so we'll just tell it to calculate one that we know is
501 * wrong (we claim that hashing zero bytes is always invalid).
502 */
503 p.cmd = EC_VBOOT_HASH_RECALC;
504 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
505 p.nonce_size = 0;
506 p.offset = 0;
507 p.size = 0;
508
509 debug("%s:\n", __func__);
510
511 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
512 (uint8_t **)&hash, sizeof(*hash)) < 0)
513 return -1;
514
515 /* No need to wait for it to finish */
516 return 0;
517}
518
519int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
520 uint8_t flags)
521{
522 struct ec_params_reboot_ec p;
523
524 p.cmd = cmd;
525 p.flags = flags;
526
527 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
528 < 0)
529 return -1;
530
531 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
532 /*
533 * EC reboot will take place immediately so delay to allow it
534 * to complete. Note that some reboot types (EC_REBOOT_COLD)
535 * will reboot the AP as well, in which case we won't actually
536 * get to this point.
537 */
538 /*
539 * TODO(rspangler@chromium.org): Would be nice if we had a
540 * better way to determine when the reboot is complete. Could
541 * we poll a memory-mapped LPC value?
542 */
543 udelay(50000);
544 }
545
546 return 0;
547}
548
Simon Glass745009c2015-10-18 21:17:14 -0600549int cros_ec_interrupt_pending(struct udevice *dev)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800550{
Simon Glass745009c2015-10-18 21:17:14 -0600551 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
552
Hung-ying Tyan88364382013-05-15 18:27:28 +0800553 /* no interrupt support : always poll */
Simon Glass745009c2015-10-18 21:17:14 -0600554 if (!dm_gpio_is_valid(&cdev->ec_int))
Simon Glass2ab83f02014-02-27 13:26:11 -0700555 return -ENOENT;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800556
Simon Glass745009c2015-10-18 21:17:14 -0600557 return dm_gpio_get_value(&cdev->ec_int);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800558}
559
Simon Glass836bb6e2014-02-27 13:26:07 -0700560int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800561{
Simon Glass836bb6e2014-02-27 13:26:07 -0700562 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
Simon Glass2ab83f02014-02-27 13:26:11 -0700563 sizeof(*info)) != sizeof(*info))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800564 return -1;
565
566 return 0;
567}
568
569int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
570{
571 struct ec_response_host_event_mask *resp;
572
573 /*
574 * Use the B copy of the event flags, because the main copy is already
575 * used by ACPI/SMI.
576 */
577 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
Simon Glass2ab83f02014-02-27 13:26:11 -0700578 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800579 return -1;
580
581 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
582 return -1;
583
584 *events_ptr = resp->mask;
585 return 0;
586}
587
588int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
589{
590 struct ec_params_host_event_mask params;
591
592 params.mask = events;
593
594 /*
595 * Use the B copy of the event flags, so it affects the data returned
596 * by cros_ec_get_host_events().
597 */
598 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
599 &params, sizeof(params), NULL, 0) < 0)
600 return -1;
601
602 return 0;
603}
604
605int cros_ec_flash_protect(struct cros_ec_dev *dev,
606 uint32_t set_mask, uint32_t set_flags,
607 struct ec_response_flash_protect *resp)
608{
609 struct ec_params_flash_protect params;
610
611 params.mask = set_mask;
612 params.flags = set_flags;
613
614 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
615 &params, sizeof(params),
Simon Glass2ab83f02014-02-27 13:26:11 -0700616 resp, sizeof(*resp)) != sizeof(*resp))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800617 return -1;
618
619 return 0;
620}
621
622static int cros_ec_check_version(struct cros_ec_dev *dev)
623{
624 struct ec_params_hello req;
625 struct ec_response_hello *resp;
626
Simon Glass72a38e02015-03-26 09:29:30 -0600627 struct dm_cros_ec_ops *ops;
628 int ret;
629
630 ops = dm_cros_ec_get_ops(dev->dev);
631 if (ops->check_version) {
632 ret = ops->check_version(dev->dev);
633 if (ret)
634 return ret;
635 }
Hung-ying Tyan88364382013-05-15 18:27:28 +0800636
637 /*
638 * TODO(sjg@chromium.org).
639 * There is a strange oddity here with the EC. We could just ignore
640 * the response, i.e. pass the last two parameters as NULL and 0.
641 * In this case we won't read back very many bytes from the EC.
642 * On the I2C bus the EC gets upset about this and will try to send
643 * the bytes anyway. This means that we will have to wait for that
644 * to complete before continuing with a new EC command.
645 *
646 * This problem is probably unique to the I2C bus.
647 *
648 * So for now, just read all the data anyway.
649 */
Randall Spanglere8c12662014-02-27 13:26:08 -0700650
Randall Spanglera6070282014-02-27 13:26:10 -0700651 /* Try sending a version 3 packet */
652 dev->protocol_version = 3;
Simon Glassd11e8fd2014-11-11 18:06:30 -0700653 req.in_data = 0;
Randall Spanglera6070282014-02-27 13:26:10 -0700654 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
655 (uint8_t **)&resp, sizeof(*resp)) > 0) {
656 return 0;
657 }
658
Randall Spanglere8c12662014-02-27 13:26:08 -0700659 /* Try sending a version 2 packet */
660 dev->protocol_version = 2;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800661 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
662 (uint8_t **)&resp, sizeof(*resp)) > 0) {
Randall Spanglere8c12662014-02-27 13:26:08 -0700663 return 0;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800664 }
665
Randall Spanglere8c12662014-02-27 13:26:08 -0700666 /*
667 * Fail if we're still here, since the EC doesn't understand any
668 * protcol version we speak. Version 1 interface without command
669 * version is no longer supported, and we don't know about any new
670 * protocol versions.
671 */
672 dev->protocol_version = 0;
673 printf("%s: ERROR: old EC interface not supported\n", __func__);
674 return -1;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800675}
676
677int cros_ec_test(struct cros_ec_dev *dev)
678{
679 struct ec_params_hello req;
680 struct ec_response_hello *resp;
681
682 req.in_data = 0x12345678;
683 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
684 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
685 printf("ec_command_inptr() returned error\n");
686 return -1;
687 }
688 if (resp->out_data != req.in_data + 0x01020304) {
689 printf("Received invalid handshake %x\n", resp->out_data);
690 return -1;
691 }
692
693 return 0;
694}
695
696int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
697 uint32_t *offset, uint32_t *size)
698{
699 struct ec_params_flash_region_info p;
700 struct ec_response_flash_region_info *r;
701 int ret;
702
703 p.region = region;
704 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
705 EC_VER_FLASH_REGION_INFO,
706 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
707 if (ret != sizeof(*r))
708 return -1;
709
710 if (offset)
711 *offset = r->offset;
712 if (size)
713 *size = r->size;
714
715 return 0;
716}
717
718int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
719{
720 struct ec_params_flash_erase p;
721
722 p.offset = offset;
723 p.size = size;
724 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
725 NULL, 0);
726}
727
728/**
729 * Write a single block to the flash
730 *
731 * Write a block of data to the EC flash. The size must not exceed the flash
732 * write block size which you can obtain from cros_ec_flash_write_burst_size().
733 *
734 * The offset starts at 0. You can obtain the region information from
735 * cros_ec_flash_offset() to find out where to write for a particular region.
736 *
737 * Attempting to write to the region where the EC is currently running from
738 * will result in an error.
739 *
740 * @param dev CROS-EC device
741 * @param data Pointer to data buffer to write
742 * @param offset Offset within flash to write to.
743 * @param size Number of bytes to write
744 * @return 0 if ok, -1 on error
745 */
746static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
747 const uint8_t *data, uint32_t offset, uint32_t size)
748{
Moritz Fischerbae5b972016-09-12 12:57:52 -0700749 struct ec_params_flash_write *p;
750 int ret;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800751
Moritz Fischerbae5b972016-09-12 12:57:52 -0700752 p = malloc(sizeof(*p) + size);
753 if (!p)
754 return -ENOMEM;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800755
Moritz Fischerbae5b972016-09-12 12:57:52 -0700756 p->offset = offset;
757 p->size = size;
758 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
759 memcpy(p + 1, data, p->size);
760
761 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
762 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
763
764 free(p);
765
766 return ret;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800767}
768
769/**
770 * Return optimal flash write burst size
771 */
772static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
773{
Simon Glass836bb6e2014-02-27 13:26:07 -0700774 return EC_FLASH_WRITE_VER0_SIZE;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800775}
776
777/**
778 * Check if a block of data is erased (all 0xff)
779 *
780 * This function is useful when dealing with flash, for checking whether a
781 * data block is erased and thus does not need to be programmed.
782 *
783 * @param data Pointer to data to check (must be word-aligned)
784 * @param size Number of bytes to check (must be word-aligned)
785 * @return 0 if erased, non-zero if any word is not erased
786 */
787static int cros_ec_data_is_erased(const uint32_t *data, int size)
788{
789 assert(!(size & 3));
790 size /= sizeof(uint32_t);
791 for (; size > 0; size -= 4, data++)
792 if (*data != -1U)
793 return 0;
794
795 return 1;
796}
797
Moritz Fischer281ca882016-09-13 14:44:48 -0700798/**
799 * Read back flash parameters
800 *
801 * This function reads back parameters of the flash as reported by the EC
802 *
803 * @param dev Pointer to device
804 * @param info Pointer to output flash info struct
805 */
806int cros_ec_read_flashinfo(struct cros_ec_dev *dev,
807 struct ec_response_flash_info *info)
808{
809 int ret;
810
811 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
812 NULL, 0, info, sizeof(*info));
813 if (ret < 0)
814 return ret;
815
816 return ret < sizeof(*info) ? -1 : 0;
817}
818
Hung-ying Tyan88364382013-05-15 18:27:28 +0800819int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
820 uint32_t offset, uint32_t size)
821{
822 uint32_t burst = cros_ec_flash_write_burst_size(dev);
823 uint32_t end, off;
824 int ret;
825
826 /*
827 * TODO: round up to the nearest multiple of write size. Can get away
828 * without that on link right now because its write size is 4 bytes.
829 */
830 end = offset + size;
831 for (off = offset; off < end; off += burst, data += burst) {
832 uint32_t todo;
833
834 /* If the data is empty, there is no point in programming it */
835 todo = min(end - off, burst);
836 if (dev->optimise_flash_write &&
837 cros_ec_data_is_erased((uint32_t *)data, todo))
838 continue;
839
840 ret = cros_ec_flash_write_block(dev, data, off, todo);
841 if (ret)
842 return ret;
843 }
844
845 return 0;
846}
847
848/**
849 * Read a single block from the flash
850 *
851 * Read a block of data from the EC flash. The size must not exceed the flash
852 * write block size which you can obtain from cros_ec_flash_write_burst_size().
853 *
854 * The offset starts at 0. You can obtain the region information from
855 * cros_ec_flash_offset() to find out where to read for a particular region.
856 *
857 * @param dev CROS-EC device
858 * @param data Pointer to data buffer to read into
859 * @param offset Offset within flash to read from
860 * @param size Number of bytes to read
861 * @return 0 if ok, -1 on error
862 */
863static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
864 uint32_t offset, uint32_t size)
865{
866 struct ec_params_flash_read p;
867
868 p.offset = offset;
869 p.size = size;
870
871 return ec_command(dev, EC_CMD_FLASH_READ, 0,
872 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
873}
874
875int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
876 uint32_t size)
877{
878 uint32_t burst = cros_ec_flash_write_burst_size(dev);
879 uint32_t end, off;
880 int ret;
881
882 end = offset + size;
883 for (off = offset; off < end; off += burst, data += burst) {
884 ret = cros_ec_flash_read_block(dev, data, off,
885 min(end - off, burst));
886 if (ret)
887 return ret;
888 }
889
890 return 0;
891}
892
893int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
894 const uint8_t *image, int image_size)
895{
896 uint32_t rw_offset, rw_size;
897 int ret;
898
899 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
900 return -1;
Simon Glass2ab83f02014-02-27 13:26:11 -0700901 if (image_size > (int)rw_size)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800902 return -1;
903
904 /* Invalidate the existing hash, just in case the AP reboots
905 * unexpectedly during the update. If that happened, the EC RW firmware
906 * would be invalid, but the EC would still have the original hash.
907 */
908 ret = cros_ec_invalidate_hash(dev);
909 if (ret)
910 return ret;
911
912 /*
913 * Erase the entire RW section, so that the EC doesn't see any garbage
914 * past the new image if it's smaller than the current image.
915 *
916 * TODO: could optimize this to erase just the current image, since
917 * presumably everything past that is 0xff's. But would still need to
918 * round up to the nearest multiple of erase size.
919 */
920 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
921 if (ret)
922 return ret;
923
924 /* Write the image */
925 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
926 if (ret)
927 return ret;
928
929 return 0;
930}
931
932int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
933{
934 struct ec_params_vbnvcontext p;
935 int len;
936
937 p.op = EC_VBNV_CONTEXT_OP_READ;
938
939 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
940 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
941 if (len < EC_VBNV_BLOCK_SIZE)
942 return -1;
943
944 return 0;
945}
946
947int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
948{
949 struct ec_params_vbnvcontext p;
950 int len;
951
952 p.op = EC_VBNV_CONTEXT_OP_WRITE;
953 memcpy(p.block, block, sizeof(p.block));
954
955 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
956 &p, sizeof(p), NULL, 0);
957 if (len < 0)
958 return -1;
959
960 return 0;
961}
962
Simon Glassf48eaf02015-08-03 08:19:24 -0600963int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800964{
Simon Glassf48eaf02015-08-03 08:19:24 -0600965 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800966 struct ec_params_ldo_set params;
967
968 params.index = index;
969 params.state = state;
970
Simon Glassf48eaf02015-08-03 08:19:24 -0600971 if (ec_command_inptr(cdev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
972 NULL, 0))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800973 return -1;
974
975 return 0;
976}
977
Simon Glassf48eaf02015-08-03 08:19:24 -0600978int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800979{
Simon Glassf48eaf02015-08-03 08:19:24 -0600980 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800981 struct ec_params_ldo_get params;
982 struct ec_response_ldo_get *resp;
983
984 params.index = index;
985
Simon Glassf48eaf02015-08-03 08:19:24 -0600986 if (ec_command_inptr(cdev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
987 (uint8_t **)&resp, sizeof(*resp)) !=
988 sizeof(*resp))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800989 return -1;
990
991 *state = resp->state;
992
993 return 0;
994}
995
Simon Glass84d6cbd2014-10-13 23:42:14 -0600996int cros_ec_register(struct udevice *dev)
997{
Simon Glasse564f052015-03-05 12:25:20 -0700998 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Simon Glass84d6cbd2014-10-13 23:42:14 -0600999 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -07001000 int node = dev_of_offset(dev);
Simon Glass84d6cbd2014-10-13 23:42:14 -06001001 char id[MSG_BYTES];
1002
1003 cdev->dev = dev;
Simon Glass32f8a192015-01-05 20:05:32 -07001004 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1005 GPIOD_IS_IN);
Simon Glass84d6cbd2014-10-13 23:42:14 -06001006 cdev->optimise_flash_write = fdtdec_get_bool(blob, node,
1007 "optimise-flash-write");
1008
Simon Glass84d6cbd2014-10-13 23:42:14 -06001009 if (cros_ec_check_version(cdev)) {
1010 debug("%s: Could not detect CROS-EC version\n", __func__);
1011 return -CROS_EC_ERR_CHECK_VERSION;
1012 }
1013
1014 if (cros_ec_read_id(cdev, id, sizeof(id))) {
1015 debug("%s: Could not read KBC ID\n", __func__);
1016 return -CROS_EC_ERR_READ_ID;
1017 }
1018
1019 /* Remember this device for use by the cros_ec command */
Simon Glassc4b206d2015-02-17 15:29:36 -07001020 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1021 cdev->protocol_version, id);
Simon Glass84d6cbd2014-10-13 23:42:14 -06001022
1023 return 0;
1024}
Hung-ying Tyan88364382013-05-15 18:27:28 +08001025
Simon Glass84d6cbd2014-10-13 23:42:14 -06001026int cros_ec_decode_ec_flash(const void *blob, int node,
1027 struct fdt_cros_ec *config)
Simon Glassd7f25f32014-02-27 13:26:03 -07001028{
Simon Glass84d6cbd2014-10-13 23:42:14 -06001029 int flash_node;
Simon Glassd7f25f32014-02-27 13:26:03 -07001030
1031 flash_node = fdt_subnode_offset(blob, node, "flash");
1032 if (flash_node < 0) {
1033 debug("Failed to find flash node\n");
1034 return -1;
1035 }
1036
1037 if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
1038 &config->flash)) {
1039 debug("Failed to decode flash node in chrome-ec'\n");
1040 return -1;
1041 }
1042
1043 config->flash_erase_value = fdtdec_get_int(blob, flash_node,
1044 "erase-value", -1);
1045 for (node = fdt_first_subnode(blob, flash_node); node >= 0;
1046 node = fdt_next_subnode(blob, node)) {
1047 const char *name = fdt_get_name(blob, node, NULL);
1048 enum ec_flash_region region;
1049
1050 if (0 == strcmp(name, "ro")) {
1051 region = EC_FLASH_REGION_RO;
1052 } else if (0 == strcmp(name, "rw")) {
1053 region = EC_FLASH_REGION_RW;
1054 } else if (0 == strcmp(name, "wp-ro")) {
1055 region = EC_FLASH_REGION_WP_RO;
1056 } else {
1057 debug("Unknown EC flash region name '%s'\n", name);
1058 return -1;
1059 }
1060
1061 if (fdtdec_read_fmap_entry(blob, node, "reg",
1062 &config->region[region])) {
1063 debug("Failed to decode flash region in chrome-ec'\n");
1064 return -1;
1065 }
1066 }
1067
1068 return 0;
1069}
1070
Moritz Fischer6d1a7182016-09-27 15:42:07 -07001071int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1072 int nmsgs)
Simon Glasscc456bd2015-08-03 08:19:23 -06001073{
1074 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1075 union {
1076 struct ec_params_i2c_passthru p;
1077 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1078 } params;
1079 union {
1080 struct ec_response_i2c_passthru r;
1081 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1082 } response;
1083 struct ec_params_i2c_passthru *p = &params.p;
1084 struct ec_response_i2c_passthru *r = &response.r;
1085 struct ec_params_i2c_passthru_msg *msg;
1086 uint8_t *pdata, *read_ptr = NULL;
1087 int read_len;
1088 int size;
1089 int rv;
1090 int i;
1091
Moritz Fischer6d1a7182016-09-27 15:42:07 -07001092 p->port = port;
Simon Glasscc456bd2015-08-03 08:19:23 -06001093
1094 p->num_msgs = nmsgs;
1095 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1096
1097 /* Create a message to write the register address and optional data */
1098 pdata = (uint8_t *)p + size;
1099
1100 read_len = 0;
1101 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1102 bool is_read = in->flags & I2C_M_RD;
1103
1104 msg->addr_flags = in->addr;
1105 msg->len = in->len;
1106 if (is_read) {
1107 msg->addr_flags |= EC_I2C_FLAG_READ;
1108 read_len += in->len;
1109 read_ptr = in->buf;
1110 if (sizeof(*r) + read_len > sizeof(response)) {
1111 puts("Read length too big for buffer\n");
1112 return -1;
1113 }
1114 } else {
1115 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1116 puts("Params too large for buffer\n");
1117 return -1;
1118 }
1119 memcpy(pdata, in->buf, in->len);
1120 pdata += in->len;
1121 }
1122 }
1123
1124 rv = ec_command(cdev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
1125 r, sizeof(*r) + read_len);
1126 if (rv < 0)
1127 return rv;
1128
1129 /* Parse response */
1130 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1131 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1132 return -1;
1133 }
1134
1135 if (rv < sizeof(*r) + read_len) {
1136 puts("Truncated read response\n");
1137 return -1;
1138 }
1139
1140 /* We only support a single read message for each transfer */
1141 if (read_len)
1142 memcpy(read_ptr, r->data, read_len);
1143
1144 return 0;
1145}
1146
Simon Glass84d6cbd2014-10-13 23:42:14 -06001147UCLASS_DRIVER(cros_ec) = {
1148 .id = UCLASS_CROS_EC,
1149 .name = "cros_ec",
1150 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
Simon Glass91195482016-07-05 17:10:10 -06001151 .post_bind = dm_scan_fdt_dev,
Simon Glass84d6cbd2014-10-13 23:42:14 -06001152};