blob: 07e662a51bc6d1c528cb1ad4e2b818a3d542659b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08002/*
3 * Chromium OS cros_ec driver
4 *
5 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08006 */
7
8/*
Simon Glassece28862014-02-27 13:26:07 -07009 * This is the interface to the Chrome OS EC. It provides keyboard functions,
10 * power control and battery management. Quite a few other functions are
11 * provided to enable the EC software to be updated, talk to the EC's I2C bus
12 * and store a small amount of data in a memory which persists while the EC
13 * is not reset.
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080014 */
15
16#include <common.h>
17#include <command.h>
Simon Glass44569b52014-10-13 23:42:14 -060018#include <dm.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080019#include <i2c.h>
20#include <cros_ec.h>
21#include <fdtdec.h>
22#include <malloc.h>
23#include <spi.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090024#include <linux/errno.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080025#include <asm/io.h>
26#include <asm-generic/gpio.h>
Simon Glass44569b52014-10-13 23:42:14 -060027#include <dm/device-internal.h>
Simon Glassb35e6d62017-05-18 20:09:23 -060028#include <dm/of_extra.h>
Simon Glass44569b52014-10-13 23:42:14 -060029#include <dm/uclass-internal.h>
Hung-ying Tyanc48ca88f2013-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 Tyanc48ca88f2013-05-15 18:27:28 +080044void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
45{
46#ifdef DEBUG
47 int i;
48
49 printf("%s: ", name);
50 if (cmd != -1)
51 printf("cmd=%#x: ", cmd);
52 for (i = 0; i < len; i++)
53 printf("%02x ", data[i]);
54 printf("\n");
55#endif
56}
57
58/*
59 * Calculate a simple 8-bit checksum of a data block
60 *
61 * @param data Data block to checksum
62 * @param size Size of data block in bytes
63 * @return checksum value (0 to 255)
64 */
65int cros_ec_calc_checksum(const uint8_t *data, int size)
66{
67 int csum, i;
68
69 for (i = csum = 0; i < size; i++)
70 csum += data[i];
71 return csum & 0xff;
72}
73
Simon Glass13c7c5b2014-02-27 13:26:09 -070074/**
75 * Create a request packet for protocol version 3.
76 *
77 * The packet is stored in the device's internal output buffer.
78 *
79 * @param dev CROS-EC device
80 * @param cmd Command to send (EC_CMD_...)
81 * @param cmd_version Version of command to send (EC_VER_...)
82 * @param dout Output data (may be NULL If dout_len=0)
83 * @param dout_len Size of output data in bytes
84 * @return packet size in bytes, or <0 if error.
85 */
Simon Glassbed43522018-10-01 12:22:22 -060086static int create_proto3_request(struct cros_ec_dev *cdev,
Simon Glass13c7c5b2014-02-27 13:26:09 -070087 int cmd, int cmd_version,
88 const void *dout, int dout_len)
89{
Simon Glassbed43522018-10-01 12:22:22 -060090 struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
Simon Glass13c7c5b2014-02-27 13:26:09 -070091 int out_bytes = dout_len + sizeof(*rq);
92
93 /* Fail if output size is too big */
Simon Glassbed43522018-10-01 12:22:22 -060094 if (out_bytes > (int)sizeof(cdev->dout)) {
Simon Glass13c7c5b2014-02-27 13:26:09 -070095 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
96 return -EC_RES_REQUEST_TRUNCATED;
97 }
98
99 /* Fill in request packet */
100 rq->struct_version = EC_HOST_REQUEST_VERSION;
101 rq->checksum = 0;
102 rq->command = cmd;
103 rq->command_version = cmd_version;
104 rq->reserved = 0;
105 rq->data_len = dout_len;
106
107 /* Copy data after header */
108 memcpy(rq + 1, dout, dout_len);
109
110 /* Write checksum field so the entire packet sums to 0 */
Simon Glassbed43522018-10-01 12:22:22 -0600111 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
Simon Glass13c7c5b2014-02-27 13:26:09 -0700112
Simon Glassbed43522018-10-01 12:22:22 -0600113 cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700114
115 /* Return size of request packet */
116 return out_bytes;
117}
118
119/**
120 * Prepare the device to receive a protocol version 3 response.
121 *
122 * @param dev CROS-EC device
123 * @param din_len Maximum size of response in bytes
124 * @return maximum expected number of bytes in response, or <0 if error.
125 */
Simon Glassbed43522018-10-01 12:22:22 -0600126static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
Simon Glass13c7c5b2014-02-27 13:26:09 -0700127{
128 int in_bytes = din_len + sizeof(struct ec_host_response);
129
130 /* Fail if input size is too big */
Simon Glassbed43522018-10-01 12:22:22 -0600131 if (in_bytes > (int)sizeof(cdev->din)) {
Simon Glass13c7c5b2014-02-27 13:26:09 -0700132 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
133 return -EC_RES_RESPONSE_TOO_BIG;
134 }
135
136 /* Return expected size of response packet */
137 return in_bytes;
138}
139
140/**
141 * Handle a protocol version 3 response packet.
142 *
143 * The packet must already be stored in the device's internal input buffer.
144 *
145 * @param dev CROS-EC device
146 * @param dinp Returns pointer to response data
147 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700148 * @return number of bytes of response data, or <0 if error. Note that error
149 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
150 * overlap!)
Simon Glass13c7c5b2014-02-27 13:26:09 -0700151 */
152static int handle_proto3_response(struct cros_ec_dev *dev,
153 uint8_t **dinp, int din_len)
154{
155 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
156 int in_bytes;
157 int csum;
158
159 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
160
161 /* Check input data */
162 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
163 debug("%s: EC response version mismatch\n", __func__);
164 return -EC_RES_INVALID_RESPONSE;
165 }
166
167 if (rs->reserved) {
168 debug("%s: EC response reserved != 0\n", __func__);
169 return -EC_RES_INVALID_RESPONSE;
170 }
171
172 if (rs->data_len > din_len) {
173 debug("%s: EC returned too much data\n", __func__);
174 return -EC_RES_RESPONSE_TOO_BIG;
175 }
176
177 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
178
179 /* Update in_bytes to actual data size */
180 in_bytes = sizeof(*rs) + rs->data_len;
181
182 /* Verify checksum */
183 csum = cros_ec_calc_checksum(dev->din, in_bytes);
184 if (csum) {
185 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
186 csum);
187 return -EC_RES_INVALID_CHECKSUM;
188 }
189
190 /* Return error result, if any */
191 if (rs->result)
192 return -(int)rs->result;
193
194 /* If we're still here, set response data pointer and return length */
195 *dinp = (uint8_t *)(rs + 1);
196
197 return rs->data_len;
198}
199
Simon Glassbed43522018-10-01 12:22:22 -0600200static int send_command_proto3(struct cros_ec_dev *cdev,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700201 int cmd, int cmd_version,
202 const void *dout, int dout_len,
203 uint8_t **dinp, int din_len)
204{
Simon Glass44569b52014-10-13 23:42:14 -0600205 struct dm_cros_ec_ops *ops;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700206 int out_bytes, in_bytes;
207 int rv;
208
209 /* Create request packet */
Simon Glassbed43522018-10-01 12:22:22 -0600210 out_bytes = create_proto3_request(cdev, cmd, cmd_version,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700211 dout, dout_len);
212 if (out_bytes < 0)
213 return out_bytes;
214
215 /* Prepare response buffer */
Simon Glassbed43522018-10-01 12:22:22 -0600216 in_bytes = prepare_proto3_response_buffer(cdev, din_len);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700217 if (in_bytes < 0)
218 return in_bytes;
219
Simon Glassbed43522018-10-01 12:22:22 -0600220 ops = dm_cros_ec_get_ops(cdev->dev);
221 rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
222 -ENOSYS;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700223 if (rv < 0)
224 return rv;
225
226 /* Process the response */
Simon Glassbed43522018-10-01 12:22:22 -0600227 return handle_proto3_response(cdev, dinp, din_len);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700228}
229
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800230static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
231 const void *dout, int dout_len,
232 uint8_t **dinp, int din_len)
233{
Simon Glass44569b52014-10-13 23:42:14 -0600234 struct dm_cros_ec_ops *ops;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700235 int ret = -1;
236
237 /* Handle protocol version 3 support */
238 if (dev->protocol_version == 3) {
239 return send_command_proto3(dev, cmd, cmd_version,
240 dout, dout_len, dinp, din_len);
241 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800242
Simon Glass44569b52014-10-13 23:42:14 -0600243 ops = dm_cros_ec_get_ops(dev->dev);
244 ret = ops->command(dev->dev, cmd, cmd_version,
245 (const uint8_t *)dout, dout_len, dinp, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800246
247 return ret;
248}
249
250/**
251 * Send a command to the CROS-EC device and return the reply.
252 *
253 * The device's internal input/output buffers are used.
254 *
255 * @param dev CROS-EC device
256 * @param cmd Command to send (EC_CMD_...)
257 * @param cmd_version Version of command to send (EC_VER_...)
258 * @param dout Output data (may be NULL If dout_len=0)
259 * @param dout_len Size of output data in bytes
260 * @param dinp Response data (may be NULL If din_len=0).
261 * If not NULL, it will be updated to point to the data
262 * and will always be double word aligned (64-bits)
263 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700264 * @return number of bytes in response, or -ve on error
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800265 */
Simon Glassbed43522018-10-01 12:22:22 -0600266static int ec_command_inptr(struct udevice *dev, uint8_t cmd,
Simon Glass699c9ca2018-10-01 12:22:08 -0600267 int cmd_version, const void *dout, int dout_len,
268 uint8_t **dinp, int din_len)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800269{
Simon Glassbed43522018-10-01 12:22:22 -0600270 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Simon Glass19cc2952014-02-27 13:26:11 -0700271 uint8_t *din = NULL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800272 int len;
273
Simon Glassbed43522018-10-01 12:22:22 -0600274 len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
275 din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800276
277 /* If the command doesn't complete, wait a while */
278 if (len == -EC_RES_IN_PROGRESS) {
Simon Glass19cc2952014-02-27 13:26:11 -0700279 struct ec_response_get_comms_status *resp = NULL;
Hung-ying Tyanc48ca88f2013-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 */
Simon Glassbed43522018-10-01 12:22:22 -0600288 ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
289 NULL, 0,
290 (uint8_t **)&resp, sizeof(*resp));
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800291 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 */
Simon Glassbed43522018-10-01 12:22:22 -0600303 len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
304 &din, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800305 }
306
Simon Glass0b8e8ce2017-05-18 20:09:22 -0600307 debug("%s: len=%d, din=%p\n", __func__, len, din);
Hung-ying Tyanc48ca88f2013-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 Glassd3d9c062015-01-25 08:27:17 -0700331 * @return number of bytes in response, or -ve on error
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800332 */
Simon Glassbed43522018-10-01 12:22:22 -0600333static int ec_command(struct udevice *dev, uint8_t cmd, int cmd_version,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800334 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,
Simon Glassbed43522018-10-01 12:22:22 -0600342 &in_buffer, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800343 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 Glassfb726402015-10-18 21:17:14 -0600356int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800357{
Simon Glassbed43522018-10-01 12:22:22 -0600358 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
Simon Glass19cc2952014-02-27 13:26:11 -0700359 sizeof(scan->data)) != sizeof(scan->data))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800360 return -1;
361
362 return 0;
363}
364
Simon Glassbed43522018-10-01 12:22:22 -0600365int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800366{
367 struct ec_response_get_version *r;
368
369 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700370 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800371 return -1;
372
Simon Glass19cc2952014-02-27 13:26:11 -0700373 if (maxlen > (int)sizeof(r->version_string_ro))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800374 maxlen = sizeof(r->version_string_ro);
375
376 switch (r->current_image) {
377 case EC_IMAGE_RO:
378 memcpy(id, r->version_string_ro, maxlen);
379 break;
380 case EC_IMAGE_RW:
381 memcpy(id, r->version_string_rw, maxlen);
382 break;
383 default:
384 return -1;
385 }
386
387 id[maxlen - 1] = '\0';
388 return 0;
389}
390
Simon Glassbed43522018-10-01 12:22:22 -0600391int cros_ec_read_version(struct udevice *dev,
392 struct ec_response_get_version **versionp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800393{
394 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
395 (uint8_t **)versionp, sizeof(**versionp))
Simon Glass19cc2952014-02-27 13:26:11 -0700396 != sizeof(**versionp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800397 return -1;
398
399 return 0;
400}
401
Simon Glassbed43522018-10-01 12:22:22 -0600402int cros_ec_read_build_info(struct udevice *dev, char **strp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800403{
404 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
Simon Glassece28862014-02-27 13:26:07 -0700405 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800406 return -1;
407
408 return 0;
409}
410
Simon Glassbed43522018-10-01 12:22:22 -0600411int cros_ec_read_current_image(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600412 enum ec_current_image *image)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800413{
414 struct ec_response_get_version *r;
415
416 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700417 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800418 return -1;
419
420 *image = r->current_image;
421 return 0;
422}
423
Simon Glassbed43522018-10-01 12:22:22 -0600424static int cros_ec_wait_on_hash_done(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600425 struct ec_response_vboot_hash *hash)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800426{
427 struct ec_params_vboot_hash p;
428 ulong start;
429
430 start = get_timer(0);
431 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
432 mdelay(50); /* Insert some reasonable delay */
433
434 p.cmd = EC_VBOOT_HASH_GET;
435 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
436 hash, sizeof(*hash)) < 0)
437 return -1;
438
439 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
440 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
441 return -EC_RES_TIMEOUT;
442 }
443 }
444 return 0;
445}
446
447
Simon Glassbed43522018-10-01 12:22:22 -0600448int cros_ec_read_hash(struct udevice *dev, struct ec_response_vboot_hash *hash)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800449{
450 struct ec_params_vboot_hash p;
451 int rv;
452
453 p.cmd = EC_VBOOT_HASH_GET;
454 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
455 hash, sizeof(*hash)) < 0)
456 return -1;
457
458 /* If the EC is busy calculating the hash, fidget until it's done. */
459 rv = cros_ec_wait_on_hash_done(dev, hash);
460 if (rv)
461 return rv;
462
463 /* If the hash is valid, we're done. Otherwise, we have to kick it off
464 * again and wait for it to complete. Note that we explicitly assume
465 * that hashing zero bytes is always wrong, even though that would
466 * produce a valid hash value. */
467 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
468 return 0;
469
470 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
471 __func__, hash->status, hash->size);
472
Simon Glassece28862014-02-27 13:26:07 -0700473 p.cmd = EC_VBOOT_HASH_START;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800474 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
475 p.nonce_size = 0;
Simon Glass2c1e5502018-10-01 12:22:36 -0600476 p.offset = EC_VBOOT_HASH_OFFSET_ACTIVE;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800477
478 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
479 hash, sizeof(*hash)) < 0)
480 return -1;
481
482 rv = cros_ec_wait_on_hash_done(dev, hash);
483 if (rv)
484 return rv;
485
486 debug("%s: hash done\n", __func__);
487
488 return 0;
489}
490
Simon Glassbed43522018-10-01 12:22:22 -0600491static int cros_ec_invalidate_hash(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800492{
493 struct ec_params_vboot_hash p;
494 struct ec_response_vboot_hash *hash;
495
496 /* We don't have an explict command for the EC to discard its current
497 * hash value, so we'll just tell it to calculate one that we know is
498 * wrong (we claim that hashing zero bytes is always invalid).
499 */
500 p.cmd = EC_VBOOT_HASH_RECALC;
501 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
502 p.nonce_size = 0;
503 p.offset = 0;
504 p.size = 0;
505
506 debug("%s:\n", __func__);
507
508 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
509 (uint8_t **)&hash, sizeof(*hash)) < 0)
510 return -1;
511
512 /* No need to wait for it to finish */
513 return 0;
514}
515
Simon Glassbed43522018-10-01 12:22:22 -0600516int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800517{
518 struct ec_params_reboot_ec p;
519
520 p.cmd = cmd;
521 p.flags = flags;
522
523 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
524 < 0)
525 return -1;
526
527 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
528 /*
529 * EC reboot will take place immediately so delay to allow it
530 * to complete. Note that some reboot types (EC_REBOOT_COLD)
531 * will reboot the AP as well, in which case we won't actually
532 * get to this point.
533 */
534 /*
535 * TODO(rspangler@chromium.org): Would be nice if we had a
536 * better way to determine when the reboot is complete. Could
537 * we poll a memory-mapped LPC value?
538 */
539 udelay(50000);
540 }
541
542 return 0;
543}
544
Simon Glassfb726402015-10-18 21:17:14 -0600545int cros_ec_interrupt_pending(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800546{
Simon Glassfb726402015-10-18 21:17:14 -0600547 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
548
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800549 /* no interrupt support : always poll */
Simon Glassfb726402015-10-18 21:17:14 -0600550 if (!dm_gpio_is_valid(&cdev->ec_int))
Simon Glass19cc2952014-02-27 13:26:11 -0700551 return -ENOENT;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800552
Simon Glassfb726402015-10-18 21:17:14 -0600553 return dm_gpio_get_value(&cdev->ec_int);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800554}
555
Simon Glassbed43522018-10-01 12:22:22 -0600556int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800557{
Simon Glassece28862014-02-27 13:26:07 -0700558 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
Simon Glass19cc2952014-02-27 13:26:11 -0700559 sizeof(*info)) != sizeof(*info))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800560 return -1;
561
562 return 0;
563}
564
Simon Glassbed43522018-10-01 12:22:22 -0600565int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800566{
567 struct ec_response_host_event_mask *resp;
568
569 /*
570 * Use the B copy of the event flags, because the main copy is already
571 * used by ACPI/SMI.
572 */
573 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700574 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800575 return -1;
576
577 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
578 return -1;
579
580 *events_ptr = resp->mask;
581 return 0;
582}
583
Simon Glassbed43522018-10-01 12:22:22 -0600584int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800585{
586 struct ec_params_host_event_mask params;
587
588 params.mask = events;
589
590 /*
591 * Use the B copy of the event flags, so it affects the data returned
592 * by cros_ec_get_host_events().
593 */
594 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
595 &params, sizeof(params), NULL, 0) < 0)
596 return -1;
597
598 return 0;
599}
600
Simon Glassbed43522018-10-01 12:22:22 -0600601int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
602 uint32_t set_flags,
Simon Glass699c9ca2018-10-01 12:22:08 -0600603 struct ec_response_flash_protect *resp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800604{
605 struct ec_params_flash_protect params;
606
607 params.mask = set_mask;
608 params.flags = set_flags;
609
610 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
611 &params, sizeof(params),
Simon Glass19cc2952014-02-27 13:26:11 -0700612 resp, sizeof(*resp)) != sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800613 return -1;
614
615 return 0;
616}
617
Simon Glassbed43522018-10-01 12:22:22 -0600618static int cros_ec_check_version(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800619{
Simon Glassbed43522018-10-01 12:22:22 -0600620 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800621 struct ec_params_hello req;
622 struct ec_response_hello *resp;
623
Simon Glass23fb1562015-03-26 09:29:30 -0600624 struct dm_cros_ec_ops *ops;
625 int ret;
626
Simon Glassbed43522018-10-01 12:22:22 -0600627 ops = dm_cros_ec_get_ops(dev);
Simon Glass23fb1562015-03-26 09:29:30 -0600628 if (ops->check_version) {
Simon Glassbed43522018-10-01 12:22:22 -0600629 ret = ops->check_version(dev);
Simon Glass23fb1562015-03-26 09:29:30 -0600630 if (ret)
631 return ret;
632 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800633
634 /*
635 * TODO(sjg@chromium.org).
636 * There is a strange oddity here with the EC. We could just ignore
637 * the response, i.e. pass the last two parameters as NULL and 0.
638 * In this case we won't read back very many bytes from the EC.
639 * On the I2C bus the EC gets upset about this and will try to send
640 * the bytes anyway. This means that we will have to wait for that
641 * to complete before continuing with a new EC command.
642 *
643 * This problem is probably unique to the I2C bus.
644 *
645 * So for now, just read all the data anyway.
646 */
Randall Spanglerf03e9702014-02-27 13:26:08 -0700647
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700648 /* Try sending a version 3 packet */
Simon Glassbed43522018-10-01 12:22:22 -0600649 cdev->protocol_version = 3;
Simon Glass73c1ecf2014-11-11 18:06:30 -0700650 req.in_data = 0;
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700651 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
652 (uint8_t **)&resp, sizeof(*resp)) > 0) {
653 return 0;
654 }
655
Randall Spanglerf03e9702014-02-27 13:26:08 -0700656 /* Try sending a version 2 packet */
Simon Glassbed43522018-10-01 12:22:22 -0600657 cdev->protocol_version = 2;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800658 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
Simon Glassbed43522018-10-01 12:22:22 -0600659 (uint8_t **)&resp, sizeof(*resp)) > 0) {
Randall Spanglerf03e9702014-02-27 13:26:08 -0700660 return 0;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800661 }
662
Randall Spanglerf03e9702014-02-27 13:26:08 -0700663 /*
664 * Fail if we're still here, since the EC doesn't understand any
665 * protcol version we speak. Version 1 interface without command
666 * version is no longer supported, and we don't know about any new
667 * protocol versions.
668 */
Simon Glassbed43522018-10-01 12:22:22 -0600669 cdev->protocol_version = 0;
Randall Spanglerf03e9702014-02-27 13:26:08 -0700670 printf("%s: ERROR: old EC interface not supported\n", __func__);
671 return -1;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800672}
673
Simon Glassbed43522018-10-01 12:22:22 -0600674int cros_ec_test(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800675{
676 struct ec_params_hello req;
677 struct ec_response_hello *resp;
678
679 req.in_data = 0x12345678;
680 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
681 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
682 printf("ec_command_inptr() returned error\n");
683 return -1;
684 }
685 if (resp->out_data != req.in_data + 0x01020304) {
686 printf("Received invalid handshake %x\n", resp->out_data);
687 return -1;
688 }
689
690 return 0;
691}
692
Simon Glassbed43522018-10-01 12:22:22 -0600693int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800694 uint32_t *offset, uint32_t *size)
695{
696 struct ec_params_flash_region_info p;
697 struct ec_response_flash_region_info *r;
698 int ret;
699
700 p.region = region;
701 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
702 EC_VER_FLASH_REGION_INFO,
703 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
704 if (ret != sizeof(*r))
705 return -1;
706
707 if (offset)
708 *offset = r->offset;
709 if (size)
710 *size = r->size;
711
712 return 0;
713}
714
Simon Glassbed43522018-10-01 12:22:22 -0600715int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800716{
717 struct ec_params_flash_erase p;
718
719 p.offset = offset;
720 p.size = size;
721 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
722 NULL, 0);
723}
724
725/**
726 * Write a single block to the flash
727 *
728 * Write a block of data to the EC flash. The size must not exceed the flash
729 * write block size which you can obtain from cros_ec_flash_write_burst_size().
730 *
731 * The offset starts at 0. You can obtain the region information from
732 * cros_ec_flash_offset() to find out where to write for a particular region.
733 *
734 * Attempting to write to the region where the EC is currently running from
735 * will result in an error.
736 *
737 * @param dev CROS-EC device
738 * @param data Pointer to data buffer to write
739 * @param offset Offset within flash to write to.
740 * @param size Number of bytes to write
741 * @return 0 if ok, -1 on error
742 */
Simon Glassbed43522018-10-01 12:22:22 -0600743static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
744 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800745{
Moritz Fischere597bc72016-09-12 12:57:52 -0700746 struct ec_params_flash_write *p;
747 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800748
Moritz Fischere597bc72016-09-12 12:57:52 -0700749 p = malloc(sizeof(*p) + size);
750 if (!p)
751 return -ENOMEM;
752
753 p->offset = offset;
754 p->size = size;
755 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
756 memcpy(p + 1, data, p->size);
757
758 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
759 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
760
761 free(p);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800762
Moritz Fischere597bc72016-09-12 12:57:52 -0700763 return ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800764}
765
766/**
767 * Return optimal flash write burst size
768 */
Simon Glassbed43522018-10-01 12:22:22 -0600769static int cros_ec_flash_write_burst_size(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800770{
Simon Glassece28862014-02-27 13:26:07 -0700771 return EC_FLASH_WRITE_VER0_SIZE;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800772}
773
774/**
775 * Check if a block of data is erased (all 0xff)
776 *
777 * This function is useful when dealing with flash, for checking whether a
778 * data block is erased and thus does not need to be programmed.
779 *
780 * @param data Pointer to data to check (must be word-aligned)
781 * @param size Number of bytes to check (must be word-aligned)
782 * @return 0 if erased, non-zero if any word is not erased
783 */
784static int cros_ec_data_is_erased(const uint32_t *data, int size)
785{
786 assert(!(size & 3));
787 size /= sizeof(uint32_t);
788 for (; size > 0; size -= 4, data++)
789 if (*data != -1U)
790 return 0;
791
792 return 1;
793}
794
Moritz Fischerde5bc892016-09-13 14:44:48 -0700795/**
796 * Read back flash parameters
797 *
798 * This function reads back parameters of the flash as reported by the EC
799 *
800 * @param dev Pointer to device
801 * @param info Pointer to output flash info struct
802 */
Simon Glassbed43522018-10-01 12:22:22 -0600803int cros_ec_read_flashinfo(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600804 struct ec_response_flash_info *info)
Moritz Fischerde5bc892016-09-13 14:44:48 -0700805{
806 int ret;
807
808 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
809 NULL, 0, info, sizeof(*info));
810 if (ret < 0)
811 return ret;
812
813 return ret < sizeof(*info) ? -1 : 0;
814}
815
Simon Glassbed43522018-10-01 12:22:22 -0600816int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
Simon Glass699c9ca2018-10-01 12:22:08 -0600817 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800818{
Simon Glassbed43522018-10-01 12:22:22 -0600819 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800820 uint32_t burst = cros_ec_flash_write_burst_size(dev);
821 uint32_t end, off;
822 int ret;
823
824 /*
825 * TODO: round up to the nearest multiple of write size. Can get away
826 * without that on link right now because its write size is 4 bytes.
827 */
828 end = offset + size;
829 for (off = offset; off < end; off += burst, data += burst) {
830 uint32_t todo;
831
832 /* If the data is empty, there is no point in programming it */
833 todo = min(end - off, burst);
Simon Glassbed43522018-10-01 12:22:22 -0600834 if (cdev->optimise_flash_write &&
Simon Glass699c9ca2018-10-01 12:22:08 -0600835 cros_ec_data_is_erased((uint32_t *)data, todo))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800836 continue;
837
838 ret = cros_ec_flash_write_block(dev, data, off, todo);
839 if (ret)
840 return ret;
841 }
842
843 return 0;
844}
845
846/**
847 * Read a single block from the flash
848 *
849 * Read a block of data from the EC flash. The size must not exceed the flash
850 * write block size which you can obtain from cros_ec_flash_write_burst_size().
851 *
852 * The offset starts at 0. You can obtain the region information from
853 * cros_ec_flash_offset() to find out where to read for a particular region.
854 *
855 * @param dev CROS-EC device
856 * @param data Pointer to data buffer to read into
857 * @param offset Offset within flash to read from
858 * @param size Number of bytes to read
859 * @return 0 if ok, -1 on error
860 */
Simon Glassbed43522018-10-01 12:22:22 -0600861static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
Simon Glass699c9ca2018-10-01 12:22:08 -0600862 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800863{
864 struct ec_params_flash_read p;
865
866 p.offset = offset;
867 p.size = size;
868
869 return ec_command(dev, EC_CMD_FLASH_READ, 0,
870 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
871}
872
Simon Glassbed43522018-10-01 12:22:22 -0600873int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
Simon Glass699c9ca2018-10-01 12:22:08 -0600874 uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800875{
876 uint32_t burst = cros_ec_flash_write_burst_size(dev);
877 uint32_t end, off;
878 int ret;
879
880 end = offset + size;
881 for (off = offset; off < end; off += burst, data += burst) {
882 ret = cros_ec_flash_read_block(dev, data, off,
883 min(end - off, burst));
884 if (ret)
885 return ret;
886 }
887
888 return 0;
889}
890
Simon Glassbed43522018-10-01 12:22:22 -0600891int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
Simon Glass699c9ca2018-10-01 12:22:08 -0600892 int image_size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800893{
894 uint32_t rw_offset, rw_size;
895 int ret;
896
Simon Glass2c1e5502018-10-01 12:22:36 -0600897 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
898 &rw_size))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800899 return -1;
Simon Glass19cc2952014-02-27 13:26:11 -0700900 if (image_size > (int)rw_size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800901 return -1;
902
903 /* Invalidate the existing hash, just in case the AP reboots
904 * unexpectedly during the update. If that happened, the EC RW firmware
905 * would be invalid, but the EC would still have the original hash.
906 */
907 ret = cros_ec_invalidate_hash(dev);
908 if (ret)
909 return ret;
910
911 /*
912 * Erase the entire RW section, so that the EC doesn't see any garbage
913 * past the new image if it's smaller than the current image.
914 *
915 * TODO: could optimize this to erase just the current image, since
916 * presumably everything past that is 0xff's. But would still need to
917 * round up to the nearest multiple of erase size.
918 */
919 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
920 if (ret)
921 return ret;
922
923 /* Write the image */
924 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
925 if (ret)
926 return ret;
927
928 return 0;
929}
930
Simon Glassbed43522018-10-01 12:22:22 -0600931int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800932{
933 struct ec_params_vbnvcontext p;
934 int len;
935
Simon Glassbed43522018-10-01 12:22:22 -0600936 if (size != EC_VBNV_BLOCK_SIZE)
937 return -EINVAL;
938
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800939 p.op = EC_VBNV_CONTEXT_OP_READ;
940
941 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
942 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
943 if (len < EC_VBNV_BLOCK_SIZE)
Simon Glassbed43522018-10-01 12:22:22 -0600944 return -EIO;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800945
946 return 0;
947}
948
Simon Glassbed43522018-10-01 12:22:22 -0600949int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800950{
951 struct ec_params_vbnvcontext p;
952 int len;
953
Simon Glassbed43522018-10-01 12:22:22 -0600954 if (size != EC_VBNV_BLOCK_SIZE)
955 return -EINVAL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800956 p.op = EC_VBNV_CONTEXT_OP_WRITE;
957 memcpy(p.block, block, sizeof(p.block));
958
959 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
960 &p, sizeof(p), NULL, 0);
961 if (len < 0)
962 return -1;
963
964 return 0;
965}
966
Simon Glasseb2cc512015-08-03 08:19:24 -0600967int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800968{
969 struct ec_params_ldo_set params;
970
971 params.index = index;
972 params.state = state;
973
Simon Glassbed43522018-10-01 12:22:22 -0600974 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
Simon Glasseb2cc512015-08-03 08:19:24 -0600975 NULL, 0))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800976 return -1;
977
978 return 0;
979}
980
Simon Glasseb2cc512015-08-03 08:19:24 -0600981int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800982{
983 struct ec_params_ldo_get params;
984 struct ec_response_ldo_get *resp;
985
986 params.index = index;
987
Simon Glassbed43522018-10-01 12:22:22 -0600988 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
Simon Glasseb2cc512015-08-03 08:19:24 -0600989 (uint8_t **)&resp, sizeof(*resp)) !=
990 sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800991 return -1;
992
993 *state = resp->state;
994
995 return 0;
996}
997
Simon Glass44569b52014-10-13 23:42:14 -0600998int cros_ec_register(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800999{
Simon Glassde0977b2015-03-05 12:25:20 -07001000 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001001 char id[MSG_BYTES];
Simon Glass44569b52014-10-13 23:42:14 -06001002
1003 cdev->dev = dev;
Simon Glass0d7cc472015-01-05 20:05:32 -07001004 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1005 GPIOD_IS_IN);
Simon Glassb35e6d62017-05-18 20:09:23 -06001006 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
Simon Glass44569b52014-10-13 23:42:14 -06001007
Simon Glassbed43522018-10-01 12:22:22 -06001008 if (cros_ec_check_version(dev)) {
Simon Glass44569b52014-10-13 23:42:14 -06001009 debug("%s: Could not detect CROS-EC version\n", __func__);
1010 return -CROS_EC_ERR_CHECK_VERSION;
1011 }
1012
Simon Glassbed43522018-10-01 12:22:22 -06001013 if (cros_ec_read_id(dev, id, sizeof(id))) {
Simon Glass44569b52014-10-13 23:42:14 -06001014 debug("%s: Could not read KBC ID\n", __func__);
1015 return -CROS_EC_ERR_READ_ID;
1016 }
1017
1018 /* Remember this device for use by the cros_ec command */
Simon Glass46e17332015-02-17 15:29:36 -07001019 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1020 cdev->protocol_version, id);
Simon Glass44569b52014-10-13 23:42:14 -06001021
1022 return 0;
1023}
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001024
Simon Glassb35e6d62017-05-18 20:09:23 -06001025int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
Simon Glass3c59abc2014-02-27 13:26:03 -07001026{
Simon Glassb35e6d62017-05-18 20:09:23 -06001027 ofnode flash_node, node;
Simon Glass3c59abc2014-02-27 13:26:03 -07001028
Simon Glassb35e6d62017-05-18 20:09:23 -06001029 flash_node = dev_read_subnode(dev, "flash");
1030 if (!ofnode_valid(flash_node)) {
Simon Glass3c59abc2014-02-27 13:26:03 -07001031 debug("Failed to find flash node\n");
1032 return -1;
1033 }
1034
Simon Glass4b580932018-06-11 13:07:17 -06001035 if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
Simon Glassb35e6d62017-05-18 20:09:23 -06001036 debug("Failed to decode flash node in chrome-ec\n");
Simon Glass3c59abc2014-02-27 13:26:03 -07001037 return -1;
1038 }
1039
Simon Glassb35e6d62017-05-18 20:09:23 -06001040 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1041 "erase-value", -1);
Simon Glass28529762017-08-05 15:45:54 -06001042 ofnode_for_each_subnode(node, flash_node) {
Simon Glassb35e6d62017-05-18 20:09:23 -06001043 const char *name = ofnode_get_name(node);
Simon Glass3c59abc2014-02-27 13:26:03 -07001044 enum ec_flash_region region;
1045
1046 if (0 == strcmp(name, "ro")) {
1047 region = EC_FLASH_REGION_RO;
1048 } else if (0 == strcmp(name, "rw")) {
Simon Glass2c1e5502018-10-01 12:22:36 -06001049 region = EC_FLASH_REGION_ACTIVE;
Simon Glass3c59abc2014-02-27 13:26:03 -07001050 } else if (0 == strcmp(name, "wp-ro")) {
1051 region = EC_FLASH_REGION_WP_RO;
1052 } else {
1053 debug("Unknown EC flash region name '%s'\n", name);
1054 return -1;
1055 }
1056
Simon Glass4b580932018-06-11 13:07:17 -06001057 if (ofnode_read_fmap_entry(node, &config->region[region])) {
Simon Glass3c59abc2014-02-27 13:26:03 -07001058 debug("Failed to decode flash region in chrome-ec'\n");
1059 return -1;
1060 }
1061 }
1062
1063 return 0;
1064}
1065
Moritz Fischer6cddc602016-09-27 15:42:07 -07001066int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1067 int nmsgs)
Simon Glass9ad07af2015-08-03 08:19:23 -06001068{
Simon Glass9ad07af2015-08-03 08:19:23 -06001069 union {
1070 struct ec_params_i2c_passthru p;
1071 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1072 } params;
1073 union {
1074 struct ec_response_i2c_passthru r;
1075 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1076 } response;
1077 struct ec_params_i2c_passthru *p = &params.p;
1078 struct ec_response_i2c_passthru *r = &response.r;
1079 struct ec_params_i2c_passthru_msg *msg;
1080 uint8_t *pdata, *read_ptr = NULL;
1081 int read_len;
1082 int size;
1083 int rv;
1084 int i;
1085
Moritz Fischer6cddc602016-09-27 15:42:07 -07001086 p->port = port;
Simon Glass9ad07af2015-08-03 08:19:23 -06001087
1088 p->num_msgs = nmsgs;
1089 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1090
1091 /* Create a message to write the register address and optional data */
1092 pdata = (uint8_t *)p + size;
1093
1094 read_len = 0;
1095 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1096 bool is_read = in->flags & I2C_M_RD;
1097
1098 msg->addr_flags = in->addr;
1099 msg->len = in->len;
1100 if (is_read) {
1101 msg->addr_flags |= EC_I2C_FLAG_READ;
1102 read_len += in->len;
1103 read_ptr = in->buf;
1104 if (sizeof(*r) + read_len > sizeof(response)) {
1105 puts("Read length too big for buffer\n");
1106 return -1;
1107 }
1108 } else {
1109 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1110 puts("Params too large for buffer\n");
1111 return -1;
1112 }
1113 memcpy(pdata, in->buf, in->len);
1114 pdata += in->len;
1115 }
1116 }
1117
Simon Glassbed43522018-10-01 12:22:22 -06001118 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
Simon Glass9ad07af2015-08-03 08:19:23 -06001119 r, sizeof(*r) + read_len);
1120 if (rv < 0)
1121 return rv;
1122
1123 /* Parse response */
1124 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1125 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1126 return -1;
1127 }
1128
1129 if (rv < sizeof(*r) + read_len) {
1130 puts("Truncated read response\n");
1131 return -1;
1132 }
1133
1134 /* We only support a single read message for each transfer */
1135 if (read_len)
1136 memcpy(read_ptr, r->data, read_len);
1137
1138 return 0;
1139}
1140
Simon Glass44569b52014-10-13 23:42:14 -06001141UCLASS_DRIVER(cros_ec) = {
1142 .id = UCLASS_CROS_EC,
1143 .name = "cros_ec",
1144 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
Simon Glass18230342016-07-05 17:10:10 -06001145 .post_bind = dm_scan_fdt_dev,
Simon Glass44569b52014-10-13 23:42:14 -06001146};