blob: 2dcdb3d8d6100c00559906dce287544e60483c63 [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
Simon Glassa77024d2018-11-06 15:21:19 -070016#define LOG_CATEGORY UCLASS_CROS_EC
17
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080018#include <common.h>
19#include <command.h>
Simon Glass44569b52014-10-13 23:42:14 -060020#include <dm.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080021#include <i2c.h>
22#include <cros_ec.h>
23#include <fdtdec.h>
24#include <malloc.h>
25#include <spi.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090026#include <linux/errno.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080027#include <asm/io.h>
28#include <asm-generic/gpio.h>
Simon Glass44569b52014-10-13 23:42:14 -060029#include <dm/device-internal.h>
Simon Glassb35e6d62017-05-18 20:09:23 -060030#include <dm/of_extra.h>
Simon Glass44569b52014-10-13 23:42:14 -060031#include <dm/uclass-internal.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080032
33#ifdef DEBUG_TRACE
34#define debug_trace(fmt, b...) debug(fmt, #b)
35#else
36#define debug_trace(fmt, b...)
37#endif
38
39enum {
40 /* Timeout waiting for a flash erase command to complete */
41 CROS_EC_CMD_TIMEOUT_MS = 5000,
42 /* Timeout waiting for a synchronous hash to be recomputed */
43 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
44};
45
Simon Glass153f2522018-11-06 15:21:22 -070046#define INVALID_HCMD 0xFF
47
48/*
49 * Map UHEPI masks to non UHEPI commands in order to support old EC FW
50 * which does not support UHEPI command.
51 */
52static const struct {
53 u8 set_cmd;
54 u8 clear_cmd;
55 u8 get_cmd;
56} event_map[] = {
57 [EC_HOST_EVENT_MAIN] = {
58 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
59 INVALID_HCMD,
60 },
61 [EC_HOST_EVENT_B] = {
62 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
63 EC_CMD_HOST_EVENT_GET_B,
64 },
65 [EC_HOST_EVENT_SCI_MASK] = {
66 EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
67 EC_CMD_HOST_EVENT_GET_SCI_MASK,
68 },
69 [EC_HOST_EVENT_SMI_MASK] = {
70 EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
71 EC_CMD_HOST_EVENT_GET_SMI_MASK,
72 },
73 [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
74 INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
75 },
76 [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
77 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
78 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
79 },
80 [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
81 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
82 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
83 },
84 [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = {
85 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
86 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
87 },
88 [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
89 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
90 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
91 },
92};
93
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080094void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
95{
96#ifdef DEBUG
97 int i;
98
99 printf("%s: ", name);
100 if (cmd != -1)
101 printf("cmd=%#x: ", cmd);
102 for (i = 0; i < len; i++)
103 printf("%02x ", data[i]);
104 printf("\n");
105#endif
106}
107
108/*
109 * Calculate a simple 8-bit checksum of a data block
110 *
111 * @param data Data block to checksum
112 * @param size Size of data block in bytes
113 * @return checksum value (0 to 255)
114 */
115int cros_ec_calc_checksum(const uint8_t *data, int size)
116{
117 int csum, i;
118
119 for (i = csum = 0; i < size; i++)
120 csum += data[i];
121 return csum & 0xff;
122}
123
Simon Glass13c7c5b2014-02-27 13:26:09 -0700124/**
125 * Create a request packet for protocol version 3.
126 *
127 * The packet is stored in the device's internal output buffer.
128 *
129 * @param dev CROS-EC device
130 * @param cmd Command to send (EC_CMD_...)
131 * @param cmd_version Version of command to send (EC_VER_...)
132 * @param dout Output data (may be NULL If dout_len=0)
133 * @param dout_len Size of output data in bytes
134 * @return packet size in bytes, or <0 if error.
135 */
Simon Glassbed43522018-10-01 12:22:22 -0600136static int create_proto3_request(struct cros_ec_dev *cdev,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700137 int cmd, int cmd_version,
138 const void *dout, int dout_len)
139{
Simon Glassbed43522018-10-01 12:22:22 -0600140 struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700141 int out_bytes = dout_len + sizeof(*rq);
142
143 /* Fail if output size is too big */
Simon Glassbed43522018-10-01 12:22:22 -0600144 if (out_bytes > (int)sizeof(cdev->dout)) {
Simon Glass13c7c5b2014-02-27 13:26:09 -0700145 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
146 return -EC_RES_REQUEST_TRUNCATED;
147 }
148
149 /* Fill in request packet */
150 rq->struct_version = EC_HOST_REQUEST_VERSION;
151 rq->checksum = 0;
152 rq->command = cmd;
153 rq->command_version = cmd_version;
154 rq->reserved = 0;
155 rq->data_len = dout_len;
156
157 /* Copy data after header */
158 memcpy(rq + 1, dout, dout_len);
159
160 /* Write checksum field so the entire packet sums to 0 */
Simon Glassbed43522018-10-01 12:22:22 -0600161 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
Simon Glass13c7c5b2014-02-27 13:26:09 -0700162
Simon Glassbed43522018-10-01 12:22:22 -0600163 cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700164
165 /* Return size of request packet */
166 return out_bytes;
167}
168
169/**
170 * Prepare the device to receive a protocol version 3 response.
171 *
172 * @param dev CROS-EC device
173 * @param din_len Maximum size of response in bytes
174 * @return maximum expected number of bytes in response, or <0 if error.
175 */
Simon Glassbed43522018-10-01 12:22:22 -0600176static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
Simon Glass13c7c5b2014-02-27 13:26:09 -0700177{
178 int in_bytes = din_len + sizeof(struct ec_host_response);
179
180 /* Fail if input size is too big */
Simon Glassbed43522018-10-01 12:22:22 -0600181 if (in_bytes > (int)sizeof(cdev->din)) {
Simon Glass13c7c5b2014-02-27 13:26:09 -0700182 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
183 return -EC_RES_RESPONSE_TOO_BIG;
184 }
185
186 /* Return expected size of response packet */
187 return in_bytes;
188}
189
190/**
191 * Handle a protocol version 3 response packet.
192 *
193 * The packet must already be stored in the device's internal input buffer.
194 *
195 * @param dev CROS-EC device
196 * @param dinp Returns pointer to response data
197 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700198 * @return number of bytes of response data, or <0 if error. Note that error
199 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
200 * overlap!)
Simon Glass13c7c5b2014-02-27 13:26:09 -0700201 */
202static int handle_proto3_response(struct cros_ec_dev *dev,
203 uint8_t **dinp, int din_len)
204{
205 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
206 int in_bytes;
207 int csum;
208
209 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
210
211 /* Check input data */
212 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
213 debug("%s: EC response version mismatch\n", __func__);
214 return -EC_RES_INVALID_RESPONSE;
215 }
216
217 if (rs->reserved) {
218 debug("%s: EC response reserved != 0\n", __func__);
219 return -EC_RES_INVALID_RESPONSE;
220 }
221
222 if (rs->data_len > din_len) {
223 debug("%s: EC returned too much data\n", __func__);
224 return -EC_RES_RESPONSE_TOO_BIG;
225 }
226
227 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
228
229 /* Update in_bytes to actual data size */
230 in_bytes = sizeof(*rs) + rs->data_len;
231
232 /* Verify checksum */
233 csum = cros_ec_calc_checksum(dev->din, in_bytes);
234 if (csum) {
235 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
236 csum);
237 return -EC_RES_INVALID_CHECKSUM;
238 }
239
240 /* Return error result, if any */
241 if (rs->result)
242 return -(int)rs->result;
243
244 /* If we're still here, set response data pointer and return length */
245 *dinp = (uint8_t *)(rs + 1);
246
247 return rs->data_len;
248}
249
Simon Glassbed43522018-10-01 12:22:22 -0600250static int send_command_proto3(struct cros_ec_dev *cdev,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700251 int cmd, int cmd_version,
252 const void *dout, int dout_len,
253 uint8_t **dinp, int din_len)
254{
Simon Glass44569b52014-10-13 23:42:14 -0600255 struct dm_cros_ec_ops *ops;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700256 int out_bytes, in_bytes;
257 int rv;
258
259 /* Create request packet */
Simon Glassbed43522018-10-01 12:22:22 -0600260 out_bytes = create_proto3_request(cdev, cmd, cmd_version,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700261 dout, dout_len);
262 if (out_bytes < 0)
263 return out_bytes;
264
265 /* Prepare response buffer */
Simon Glassbed43522018-10-01 12:22:22 -0600266 in_bytes = prepare_proto3_response_buffer(cdev, din_len);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700267 if (in_bytes < 0)
268 return in_bytes;
269
Simon Glassbed43522018-10-01 12:22:22 -0600270 ops = dm_cros_ec_get_ops(cdev->dev);
271 rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
272 -ENOSYS;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700273 if (rv < 0)
274 return rv;
275
276 /* Process the response */
Simon Glassbed43522018-10-01 12:22:22 -0600277 return handle_proto3_response(cdev, dinp, din_len);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700278}
279
Simon Glass3a239512018-11-06 15:21:18 -0700280static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800281 const void *dout, int dout_len,
282 uint8_t **dinp, int din_len)
283{
Simon Glass44569b52014-10-13 23:42:14 -0600284 struct dm_cros_ec_ops *ops;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700285 int ret = -1;
286
287 /* Handle protocol version 3 support */
288 if (dev->protocol_version == 3) {
289 return send_command_proto3(dev, cmd, cmd_version,
290 dout, dout_len, dinp, din_len);
291 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800292
Simon Glass44569b52014-10-13 23:42:14 -0600293 ops = dm_cros_ec_get_ops(dev->dev);
294 ret = ops->command(dev->dev, cmd, cmd_version,
295 (const uint8_t *)dout, dout_len, dinp, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800296
297 return ret;
298}
299
300/**
301 * Send a command to the CROS-EC device and return the reply.
302 *
303 * The device's internal input/output buffers are used.
304 *
305 * @param dev CROS-EC device
306 * @param cmd Command to send (EC_CMD_...)
307 * @param cmd_version Version of command to send (EC_VER_...)
308 * @param dout Output data (may be NULL If dout_len=0)
309 * @param dout_len Size of output data in bytes
310 * @param dinp Response data (may be NULL If din_len=0).
311 * If not NULL, it will be updated to point to the data
312 * and will always be double word aligned (64-bits)
313 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700314 * @return number of bytes in response, or -ve on error
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800315 */
Simon Glassbed43522018-10-01 12:22:22 -0600316static int ec_command_inptr(struct udevice *dev, uint8_t cmd,
Simon Glass699c9ca2018-10-01 12:22:08 -0600317 int cmd_version, const void *dout, int dout_len,
318 uint8_t **dinp, int din_len)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800319{
Simon Glassbed43522018-10-01 12:22:22 -0600320 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Simon Glass19cc2952014-02-27 13:26:11 -0700321 uint8_t *din = NULL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800322 int len;
323
Simon Glassbed43522018-10-01 12:22:22 -0600324 len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
325 din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800326
327 /* If the command doesn't complete, wait a while */
328 if (len == -EC_RES_IN_PROGRESS) {
Simon Glass19cc2952014-02-27 13:26:11 -0700329 struct ec_response_get_comms_status *resp = NULL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800330 ulong start;
331
332 /* Wait for command to complete */
333 start = get_timer(0);
334 do {
335 int ret;
336
337 mdelay(50); /* Insert some reasonable delay */
Simon Glassbed43522018-10-01 12:22:22 -0600338 ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
339 NULL, 0,
340 (uint8_t **)&resp, sizeof(*resp));
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800341 if (ret < 0)
342 return ret;
343
344 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
345 debug("%s: Command %#02x timeout\n",
346 __func__, cmd);
347 return -EC_RES_TIMEOUT;
348 }
349 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
350
351 /* OK it completed, so read the status response */
352 /* not sure why it was 0 for the last argument */
Simon Glassbed43522018-10-01 12:22:22 -0600353 len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
354 &din, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800355 }
356
Simon Glass0b8e8ce2017-05-18 20:09:22 -0600357 debug("%s: len=%d, din=%p\n", __func__, len, din);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800358 if (dinp) {
359 /* If we have any data to return, it must be 64bit-aligned */
360 assert(len <= 0 || !((uintptr_t)din & 7));
361 *dinp = din;
362 }
363
364 return len;
365}
366
367/**
368 * Send a command to the CROS-EC device and return the reply.
369 *
370 * The device's internal input/output buffers are used.
371 *
372 * @param dev CROS-EC device
373 * @param cmd Command to send (EC_CMD_...)
374 * @param cmd_version Version of command to send (EC_VER_...)
375 * @param dout Output data (may be NULL If dout_len=0)
376 * @param dout_len Size of output data in bytes
377 * @param din Response data (may be NULL If din_len=0).
378 * It not NULL, it is a place for ec_command() to copy the
379 * data to.
380 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700381 * @return number of bytes in response, or -ve on error
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800382 */
Simon Glass3a239512018-11-06 15:21:18 -0700383static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800384 const void *dout, int dout_len,
385 void *din, int din_len)
386{
387 uint8_t *in_buffer;
388 int len;
389
390 assert((din_len == 0) || din);
391 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
Simon Glassbed43522018-10-01 12:22:22 -0600392 &in_buffer, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800393 if (len > 0) {
394 /*
395 * If we were asked to put it somewhere, do so, otherwise just
396 * disregard the result.
397 */
398 if (din && in_buffer) {
399 assert(len <= din_len);
400 memmove(din, in_buffer, len);
401 }
402 }
403 return len;
404}
405
Simon Glassfb726402015-10-18 21:17:14 -0600406int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800407{
Simon Glassbed43522018-10-01 12:22:22 -0600408 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
Simon Glass19cc2952014-02-27 13:26:11 -0700409 sizeof(scan->data)) != sizeof(scan->data))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800410 return -1;
411
412 return 0;
413}
414
Simon Glassbed43522018-10-01 12:22:22 -0600415int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800416{
417 struct ec_response_get_version *r;
Simon Glassa77024d2018-11-06 15:21:19 -0700418 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800419
Simon Glassa77024d2018-11-06 15:21:19 -0700420 ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
421 (uint8_t **)&r, sizeof(*r));
422 if (ret != sizeof(*r)) {
423 log_err("Got rc %d, expected %d\n", ret, sizeof(*r));
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800424 return -1;
Simon Glassa77024d2018-11-06 15:21:19 -0700425 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800426
Simon Glass19cc2952014-02-27 13:26:11 -0700427 if (maxlen > (int)sizeof(r->version_string_ro))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800428 maxlen = sizeof(r->version_string_ro);
429
430 switch (r->current_image) {
431 case EC_IMAGE_RO:
432 memcpy(id, r->version_string_ro, maxlen);
433 break;
434 case EC_IMAGE_RW:
435 memcpy(id, r->version_string_rw, maxlen);
436 break;
437 default:
Simon Glassa77024d2018-11-06 15:21:19 -0700438 log_err("Invalid EC image %d\n", r->current_image);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800439 return -1;
440 }
441
442 id[maxlen - 1] = '\0';
443 return 0;
444}
445
Simon Glassbed43522018-10-01 12:22:22 -0600446int cros_ec_read_version(struct udevice *dev,
447 struct ec_response_get_version **versionp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800448{
449 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
450 (uint8_t **)versionp, sizeof(**versionp))
Simon Glass19cc2952014-02-27 13:26:11 -0700451 != sizeof(**versionp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800452 return -1;
453
454 return 0;
455}
456
Simon Glassbed43522018-10-01 12:22:22 -0600457int cros_ec_read_build_info(struct udevice *dev, char **strp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800458{
459 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
Simon Glassece28862014-02-27 13:26:07 -0700460 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800461 return -1;
462
463 return 0;
464}
465
Simon Glassbed43522018-10-01 12:22:22 -0600466int cros_ec_read_current_image(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600467 enum ec_current_image *image)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800468{
469 struct ec_response_get_version *r;
470
471 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700472 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800473 return -1;
474
475 *image = r->current_image;
476 return 0;
477}
478
Simon Glassbed43522018-10-01 12:22:22 -0600479static int cros_ec_wait_on_hash_done(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600480 struct ec_response_vboot_hash *hash)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800481{
482 struct ec_params_vboot_hash p;
483 ulong start;
484
485 start = get_timer(0);
486 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
487 mdelay(50); /* Insert some reasonable delay */
488
489 p.cmd = EC_VBOOT_HASH_GET;
490 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
491 hash, sizeof(*hash)) < 0)
492 return -1;
493
494 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
495 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
496 return -EC_RES_TIMEOUT;
497 }
498 }
499 return 0;
500}
501
Simon Glass94564902018-10-01 12:22:38 -0600502int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
503 struct ec_response_vboot_hash *hash)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800504{
505 struct ec_params_vboot_hash p;
506 int rv;
507
508 p.cmd = EC_VBOOT_HASH_GET;
Simon Glass94564902018-10-01 12:22:38 -0600509 p.offset = hash_offset;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800510 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
511 hash, sizeof(*hash)) < 0)
512 return -1;
513
514 /* If the EC is busy calculating the hash, fidget until it's done. */
515 rv = cros_ec_wait_on_hash_done(dev, hash);
516 if (rv)
517 return rv;
518
519 /* If the hash is valid, we're done. Otherwise, we have to kick it off
520 * again and wait for it to complete. Note that we explicitly assume
521 * that hashing zero bytes is always wrong, even though that would
522 * produce a valid hash value. */
523 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
524 return 0;
525
526 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
527 __func__, hash->status, hash->size);
528
Simon Glassece28862014-02-27 13:26:07 -0700529 p.cmd = EC_VBOOT_HASH_START;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800530 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
531 p.nonce_size = 0;
Simon Glass94564902018-10-01 12:22:38 -0600532 p.offset = hash_offset;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800533
534 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
535 hash, sizeof(*hash)) < 0)
536 return -1;
537
538 rv = cros_ec_wait_on_hash_done(dev, hash);
539 if (rv)
540 return rv;
541
542 debug("%s: hash done\n", __func__);
543
544 return 0;
545}
546
Simon Glassbed43522018-10-01 12:22:22 -0600547static int cros_ec_invalidate_hash(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800548{
549 struct ec_params_vboot_hash p;
550 struct ec_response_vboot_hash *hash;
551
552 /* We don't have an explict command for the EC to discard its current
553 * hash value, so we'll just tell it to calculate one that we know is
554 * wrong (we claim that hashing zero bytes is always invalid).
555 */
556 p.cmd = EC_VBOOT_HASH_RECALC;
557 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
558 p.nonce_size = 0;
559 p.offset = 0;
560 p.size = 0;
561
562 debug("%s:\n", __func__);
563
564 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
565 (uint8_t **)&hash, sizeof(*hash)) < 0)
566 return -1;
567
568 /* No need to wait for it to finish */
569 return 0;
570}
571
Simon Glassbed43522018-10-01 12:22:22 -0600572int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800573{
574 struct ec_params_reboot_ec p;
575
576 p.cmd = cmd;
577 p.flags = flags;
578
579 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
580 < 0)
581 return -1;
582
583 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
584 /*
585 * EC reboot will take place immediately so delay to allow it
586 * to complete. Note that some reboot types (EC_REBOOT_COLD)
587 * will reboot the AP as well, in which case we won't actually
588 * get to this point.
589 */
590 /*
591 * TODO(rspangler@chromium.org): Would be nice if we had a
592 * better way to determine when the reboot is complete. Could
593 * we poll a memory-mapped LPC value?
594 */
595 udelay(50000);
596 }
597
598 return 0;
599}
600
Simon Glassfb726402015-10-18 21:17:14 -0600601int cros_ec_interrupt_pending(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800602{
Simon Glassfb726402015-10-18 21:17:14 -0600603 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
604
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800605 /* no interrupt support : always poll */
Simon Glassfb726402015-10-18 21:17:14 -0600606 if (!dm_gpio_is_valid(&cdev->ec_int))
Simon Glass19cc2952014-02-27 13:26:11 -0700607 return -ENOENT;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800608
Simon Glassfb726402015-10-18 21:17:14 -0600609 return dm_gpio_get_value(&cdev->ec_int);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800610}
611
Simon Glassbed43522018-10-01 12:22:22 -0600612int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800613{
Simon Glassece28862014-02-27 13:26:07 -0700614 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
Simon Glass19cc2952014-02-27 13:26:11 -0700615 sizeof(*info)) != sizeof(*info))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800616 return -1;
617
618 return 0;
619}
620
Simon Glass153f2522018-11-06 15:21:22 -0700621int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
622{
623 struct ec_response_host_event_mask rsp;
624 int ret;
625
626 ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
627 if (ret < 0)
628 return ret;
629 else if (ret != sizeof(rsp))
630 return -EINVAL;
631
632 *mask = rsp.mask;
633
634 return 0;
635}
636
637int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
638{
639 struct ec_params_host_event_mask req;
640 int ret;
641
642 req.mask = mask;
643
644 ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
645 if (ret < 0)
646 return ret;
647
648 return 0;
649}
650
Simon Glassbed43522018-10-01 12:22:22 -0600651int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800652{
653 struct ec_response_host_event_mask *resp;
654
655 /*
656 * Use the B copy of the event flags, because the main copy is already
657 * used by ACPI/SMI.
658 */
659 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700660 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800661 return -1;
662
663 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
664 return -1;
665
666 *events_ptr = resp->mask;
667 return 0;
668}
669
Simon Glassbed43522018-10-01 12:22:22 -0600670int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800671{
672 struct ec_params_host_event_mask params;
673
674 params.mask = events;
675
676 /*
677 * Use the B copy of the event flags, so it affects the data returned
678 * by cros_ec_get_host_events().
679 */
680 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
681 &params, sizeof(params), NULL, 0) < 0)
682 return -1;
683
684 return 0;
685}
686
Simon Glassbed43522018-10-01 12:22:22 -0600687int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
688 uint32_t set_flags,
Simon Glass699c9ca2018-10-01 12:22:08 -0600689 struct ec_response_flash_protect *resp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800690{
691 struct ec_params_flash_protect params;
692
693 params.mask = set_mask;
694 params.flags = set_flags;
695
696 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
697 &params, sizeof(params),
Simon Glass19cc2952014-02-27 13:26:11 -0700698 resp, sizeof(*resp)) != sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800699 return -1;
700
701 return 0;
702}
703
Simon Glass153f2522018-11-06 15:21:22 -0700704int cros_ec_entering_mode(struct udevice *dev, int mode)
705{
706 int rc;
707
708 rc = ec_command(dev, EC_CMD_ENTERING_MODE, 0, &mode, sizeof(mode),
709 NULL, 0);
710 if (rc)
711 return -1;
712 return 0;
713}
714
Simon Glassbed43522018-10-01 12:22:22 -0600715static int cros_ec_check_version(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800716{
Simon Glassbed43522018-10-01 12:22:22 -0600717 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800718 struct ec_params_hello req;
719 struct ec_response_hello *resp;
720
Simon Glass23fb1562015-03-26 09:29:30 -0600721 struct dm_cros_ec_ops *ops;
722 int ret;
723
Simon Glassbed43522018-10-01 12:22:22 -0600724 ops = dm_cros_ec_get_ops(dev);
Simon Glass23fb1562015-03-26 09:29:30 -0600725 if (ops->check_version) {
Simon Glassbed43522018-10-01 12:22:22 -0600726 ret = ops->check_version(dev);
Simon Glass23fb1562015-03-26 09:29:30 -0600727 if (ret)
728 return ret;
729 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800730
731 /*
732 * TODO(sjg@chromium.org).
733 * There is a strange oddity here with the EC. We could just ignore
734 * the response, i.e. pass the last two parameters as NULL and 0.
735 * In this case we won't read back very many bytes from the EC.
736 * On the I2C bus the EC gets upset about this and will try to send
737 * the bytes anyway. This means that we will have to wait for that
738 * to complete before continuing with a new EC command.
739 *
740 * This problem is probably unique to the I2C bus.
741 *
742 * So for now, just read all the data anyway.
743 */
Randall Spanglerf03e9702014-02-27 13:26:08 -0700744
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700745 /* Try sending a version 3 packet */
Simon Glassbed43522018-10-01 12:22:22 -0600746 cdev->protocol_version = 3;
Simon Glass73c1ecf2014-11-11 18:06:30 -0700747 req.in_data = 0;
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700748 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
Simon Glass3a239512018-11-06 15:21:18 -0700749 (uint8_t **)&resp, sizeof(*resp)) > 0)
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700750 return 0;
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700751
Randall Spanglerf03e9702014-02-27 13:26:08 -0700752 /* Try sending a version 2 packet */
Simon Glassbed43522018-10-01 12:22:22 -0600753 cdev->protocol_version = 2;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800754 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
Simon Glass3a239512018-11-06 15:21:18 -0700755 (uint8_t **)&resp, sizeof(*resp)) > 0)
Randall Spanglerf03e9702014-02-27 13:26:08 -0700756 return 0;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800757
Randall Spanglerf03e9702014-02-27 13:26:08 -0700758 /*
759 * Fail if we're still here, since the EC doesn't understand any
760 * protcol version we speak. Version 1 interface without command
761 * version is no longer supported, and we don't know about any new
762 * protocol versions.
763 */
Simon Glassbed43522018-10-01 12:22:22 -0600764 cdev->protocol_version = 0;
Randall Spanglerf03e9702014-02-27 13:26:08 -0700765 printf("%s: ERROR: old EC interface not supported\n", __func__);
766 return -1;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800767}
768
Simon Glassbed43522018-10-01 12:22:22 -0600769int cros_ec_test(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800770{
771 struct ec_params_hello req;
772 struct ec_response_hello *resp;
773
774 req.in_data = 0x12345678;
775 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
776 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
777 printf("ec_command_inptr() returned error\n");
778 return -1;
779 }
780 if (resp->out_data != req.in_data + 0x01020304) {
781 printf("Received invalid handshake %x\n", resp->out_data);
782 return -1;
783 }
784
785 return 0;
786}
787
Simon Glassbed43522018-10-01 12:22:22 -0600788int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800789 uint32_t *offset, uint32_t *size)
790{
791 struct ec_params_flash_region_info p;
792 struct ec_response_flash_region_info *r;
793 int ret;
794
795 p.region = region;
796 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
797 EC_VER_FLASH_REGION_INFO,
798 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
799 if (ret != sizeof(*r))
800 return -1;
801
802 if (offset)
803 *offset = r->offset;
804 if (size)
805 *size = r->size;
806
807 return 0;
808}
809
Simon Glassbed43522018-10-01 12:22:22 -0600810int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800811{
812 struct ec_params_flash_erase p;
813
814 p.offset = offset;
815 p.size = size;
816 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
817 NULL, 0);
818}
819
820/**
821 * Write a single block to the flash
822 *
823 * Write a block of data to the EC flash. The size must not exceed the flash
824 * write block size which you can obtain from cros_ec_flash_write_burst_size().
825 *
826 * The offset starts at 0. You can obtain the region information from
827 * cros_ec_flash_offset() to find out where to write for a particular region.
828 *
829 * Attempting to write to the region where the EC is currently running from
830 * will result in an error.
831 *
832 * @param dev CROS-EC device
833 * @param data Pointer to data buffer to write
834 * @param offset Offset within flash to write to.
835 * @param size Number of bytes to write
836 * @return 0 if ok, -1 on error
837 */
Simon Glassbed43522018-10-01 12:22:22 -0600838static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
839 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800840{
Moritz Fischere597bc72016-09-12 12:57:52 -0700841 struct ec_params_flash_write *p;
842 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800843
Moritz Fischere597bc72016-09-12 12:57:52 -0700844 p = malloc(sizeof(*p) + size);
845 if (!p)
846 return -ENOMEM;
847
848 p->offset = offset;
849 p->size = size;
850 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
851 memcpy(p + 1, data, p->size);
852
853 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
854 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
855
856 free(p);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800857
Moritz Fischere597bc72016-09-12 12:57:52 -0700858 return ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800859}
860
861/**
862 * Return optimal flash write burst size
863 */
Simon Glassbed43522018-10-01 12:22:22 -0600864static int cros_ec_flash_write_burst_size(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800865{
Simon Glassece28862014-02-27 13:26:07 -0700866 return EC_FLASH_WRITE_VER0_SIZE;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800867}
868
869/**
870 * Check if a block of data is erased (all 0xff)
871 *
872 * This function is useful when dealing with flash, for checking whether a
873 * data block is erased and thus does not need to be programmed.
874 *
875 * @param data Pointer to data to check (must be word-aligned)
876 * @param size Number of bytes to check (must be word-aligned)
877 * @return 0 if erased, non-zero if any word is not erased
878 */
879static int cros_ec_data_is_erased(const uint32_t *data, int size)
880{
881 assert(!(size & 3));
882 size /= sizeof(uint32_t);
883 for (; size > 0; size -= 4, data++)
884 if (*data != -1U)
885 return 0;
886
887 return 1;
888}
889
Moritz Fischerde5bc892016-09-13 14:44:48 -0700890/**
891 * Read back flash parameters
892 *
893 * This function reads back parameters of the flash as reported by the EC
894 *
895 * @param dev Pointer to device
896 * @param info Pointer to output flash info struct
897 */
Simon Glassbed43522018-10-01 12:22:22 -0600898int cros_ec_read_flashinfo(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600899 struct ec_response_flash_info *info)
Moritz Fischerde5bc892016-09-13 14:44:48 -0700900{
901 int ret;
902
903 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
904 NULL, 0, info, sizeof(*info));
905 if (ret < 0)
906 return ret;
907
908 return ret < sizeof(*info) ? -1 : 0;
909}
910
Simon Glassbed43522018-10-01 12:22:22 -0600911int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
Simon Glass699c9ca2018-10-01 12:22:08 -0600912 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800913{
Simon Glassbed43522018-10-01 12:22:22 -0600914 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800915 uint32_t burst = cros_ec_flash_write_burst_size(dev);
916 uint32_t end, off;
917 int ret;
918
Simon Glass411661d2018-11-06 15:21:20 -0700919 if (!burst)
920 return -EINVAL;
921
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800922 /*
923 * TODO: round up to the nearest multiple of write size. Can get away
924 * without that on link right now because its write size is 4 bytes.
925 */
926 end = offset + size;
927 for (off = offset; off < end; off += burst, data += burst) {
928 uint32_t todo;
929
930 /* If the data is empty, there is no point in programming it */
931 todo = min(end - off, burst);
Simon Glassbed43522018-10-01 12:22:22 -0600932 if (cdev->optimise_flash_write &&
Simon Glass699c9ca2018-10-01 12:22:08 -0600933 cros_ec_data_is_erased((uint32_t *)data, todo))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800934 continue;
935
936 ret = cros_ec_flash_write_block(dev, data, off, todo);
937 if (ret)
938 return ret;
939 }
940
941 return 0;
942}
943
944/**
Simon Glass153f2522018-11-06 15:21:22 -0700945 * Run verification on a slot
946 *
947 * @param me CrosEc instance
948 * @param region Region to run verification on
949 * @return 0 if success or not applicable. Non-zero if verification failed.
950 */
951int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
952{
953 struct ec_params_efs_verify p;
954 int rv;
955
956 log_info("EFS: EC is verifying updated image...\n");
957 p.region = region;
958
959 rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
960 if (rv >= 0) {
961 log_info("EFS: Verification success\n");
962 return 0;
963 }
964 if (rv == -EC_RES_INVALID_COMMAND) {
965 log_info("EFS: EC doesn't support EFS_VERIFY command\n");
966 return 0;
967 }
968 log_info("EFS: Verification failed\n");
969
970 return rv;
971}
972
973/**
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800974 * Read a single block from the flash
975 *
976 * Read a block of data from the EC flash. The size must not exceed the flash
977 * write block size which you can obtain from cros_ec_flash_write_burst_size().
978 *
979 * The offset starts at 0. You can obtain the region information from
980 * cros_ec_flash_offset() to find out where to read for a particular region.
981 *
982 * @param dev CROS-EC device
983 * @param data Pointer to data buffer to read into
984 * @param offset Offset within flash to read from
985 * @param size Number of bytes to read
986 * @return 0 if ok, -1 on error
987 */
Simon Glassbed43522018-10-01 12:22:22 -0600988static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
Simon Glass699c9ca2018-10-01 12:22:08 -0600989 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800990{
991 struct ec_params_flash_read p;
992
993 p.offset = offset;
994 p.size = size;
995
996 return ec_command(dev, EC_CMD_FLASH_READ, 0,
997 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
998}
999
Simon Glassbed43522018-10-01 12:22:22 -06001000int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
Simon Glass699c9ca2018-10-01 12:22:08 -06001001 uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001002{
1003 uint32_t burst = cros_ec_flash_write_burst_size(dev);
1004 uint32_t end, off;
1005 int ret;
1006
1007 end = offset + size;
1008 for (off = offset; off < end; off += burst, data += burst) {
1009 ret = cros_ec_flash_read_block(dev, data, off,
1010 min(end - off, burst));
1011 if (ret)
1012 return ret;
1013 }
1014
1015 return 0;
1016}
1017
Simon Glassbed43522018-10-01 12:22:22 -06001018int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
Simon Glass699c9ca2018-10-01 12:22:08 -06001019 int image_size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001020{
1021 uint32_t rw_offset, rw_size;
1022 int ret;
1023
Simon Glass2c1e5502018-10-01 12:22:36 -06001024 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
1025 &rw_size))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001026 return -1;
Simon Glass19cc2952014-02-27 13:26:11 -07001027 if (image_size > (int)rw_size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001028 return -1;
1029
1030 /* Invalidate the existing hash, just in case the AP reboots
1031 * unexpectedly during the update. If that happened, the EC RW firmware
1032 * would be invalid, but the EC would still have the original hash.
1033 */
1034 ret = cros_ec_invalidate_hash(dev);
1035 if (ret)
1036 return ret;
1037
1038 /*
1039 * Erase the entire RW section, so that the EC doesn't see any garbage
1040 * past the new image if it's smaller than the current image.
1041 *
1042 * TODO: could optimize this to erase just the current image, since
1043 * presumably everything past that is 0xff's. But would still need to
1044 * round up to the nearest multiple of erase size.
1045 */
1046 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
1047 if (ret)
1048 return ret;
1049
1050 /* Write the image */
1051 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
1052 if (ret)
1053 return ret;
1054
1055 return 0;
1056}
1057
Simon Glassbed43522018-10-01 12:22:22 -06001058int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001059{
1060 struct ec_params_vbnvcontext p;
1061 int len;
1062
Simon Glass153f2522018-11-06 15:21:22 -07001063 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
Simon Glassbed43522018-10-01 12:22:22 -06001064 return -EINVAL;
1065
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001066 p.op = EC_VBNV_CONTEXT_OP_READ;
1067
1068 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
Simon Glass153f2522018-11-06 15:21:22 -07001069 &p, sizeof(uint32_t) + size, block, size);
1070 if (len != size) {
1071 log_err("Expected %d bytes, got %d\n", size, len);
Simon Glassbed43522018-10-01 12:22:22 -06001072 return -EIO;
Simon Glass153f2522018-11-06 15:21:22 -07001073 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001074
1075 return 0;
1076}
1077
Simon Glassbed43522018-10-01 12:22:22 -06001078int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001079{
1080 struct ec_params_vbnvcontext p;
1081 int len;
1082
Simon Glass153f2522018-11-06 15:21:22 -07001083 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
Simon Glassbed43522018-10-01 12:22:22 -06001084 return -EINVAL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001085 p.op = EC_VBNV_CONTEXT_OP_WRITE;
Simon Glass153f2522018-11-06 15:21:22 -07001086 memcpy(p.block, block, size);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001087
1088 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
Simon Glass153f2522018-11-06 15:21:22 -07001089 &p, sizeof(uint32_t) + size, NULL, 0);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001090 if (len < 0)
1091 return -1;
1092
Simon Glass153f2522018-11-06 15:21:22 -07001093 return 0;
1094}
1095
1096int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
1097{
1098 struct ec_params_battery_cutoff p;
1099 int len;
1100
1101 p.flags = flags;
1102 len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
1103 NULL, 0);
1104
1105 if (len < 0)
1106 return -1;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001107 return 0;
1108}
1109
Simon Glasseb2cc512015-08-03 08:19:24 -06001110int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001111{
1112 struct ec_params_ldo_set params;
1113
1114 params.index = index;
1115 params.state = state;
1116
Simon Glassbed43522018-10-01 12:22:22 -06001117 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
Simon Glasseb2cc512015-08-03 08:19:24 -06001118 NULL, 0))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001119 return -1;
1120
1121 return 0;
1122}
1123
Simon Glasseb2cc512015-08-03 08:19:24 -06001124int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001125{
1126 struct ec_params_ldo_get params;
1127 struct ec_response_ldo_get *resp;
1128
1129 params.index = index;
1130
Simon Glassbed43522018-10-01 12:22:22 -06001131 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
Simon Glasseb2cc512015-08-03 08:19:24 -06001132 (uint8_t **)&resp, sizeof(*resp)) !=
1133 sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001134 return -1;
1135
1136 *state = resp->state;
1137
1138 return 0;
1139}
1140
Simon Glass44569b52014-10-13 23:42:14 -06001141int cros_ec_register(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001142{
Simon Glassde0977b2015-03-05 12:25:20 -07001143 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001144 char id[MSG_BYTES];
Simon Glass44569b52014-10-13 23:42:14 -06001145
1146 cdev->dev = dev;
Simon Glass0d7cc472015-01-05 20:05:32 -07001147 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1148 GPIOD_IS_IN);
Simon Glassb35e6d62017-05-18 20:09:23 -06001149 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
Simon Glass44569b52014-10-13 23:42:14 -06001150
Simon Glassbed43522018-10-01 12:22:22 -06001151 if (cros_ec_check_version(dev)) {
Simon Glass44569b52014-10-13 23:42:14 -06001152 debug("%s: Could not detect CROS-EC version\n", __func__);
1153 return -CROS_EC_ERR_CHECK_VERSION;
1154 }
1155
Simon Glassbed43522018-10-01 12:22:22 -06001156 if (cros_ec_read_id(dev, id, sizeof(id))) {
Simon Glass44569b52014-10-13 23:42:14 -06001157 debug("%s: Could not read KBC ID\n", __func__);
1158 return -CROS_EC_ERR_READ_ID;
1159 }
1160
1161 /* Remember this device for use by the cros_ec command */
Simon Glass46e17332015-02-17 15:29:36 -07001162 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1163 cdev->protocol_version, id);
Simon Glass44569b52014-10-13 23:42:14 -06001164
1165 return 0;
1166}
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001167
Simon Glassb35e6d62017-05-18 20:09:23 -06001168int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
Simon Glass3c59abc2014-02-27 13:26:03 -07001169{
Simon Glassb35e6d62017-05-18 20:09:23 -06001170 ofnode flash_node, node;
Simon Glass3c59abc2014-02-27 13:26:03 -07001171
Simon Glassb35e6d62017-05-18 20:09:23 -06001172 flash_node = dev_read_subnode(dev, "flash");
1173 if (!ofnode_valid(flash_node)) {
Simon Glass3c59abc2014-02-27 13:26:03 -07001174 debug("Failed to find flash node\n");
1175 return -1;
1176 }
1177
Simon Glass4b580932018-06-11 13:07:17 -06001178 if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
Simon Glassb35e6d62017-05-18 20:09:23 -06001179 debug("Failed to decode flash node in chrome-ec\n");
Simon Glass3c59abc2014-02-27 13:26:03 -07001180 return -1;
1181 }
1182
Simon Glassb35e6d62017-05-18 20:09:23 -06001183 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1184 "erase-value", -1);
Simon Glass28529762017-08-05 15:45:54 -06001185 ofnode_for_each_subnode(node, flash_node) {
Simon Glassb35e6d62017-05-18 20:09:23 -06001186 const char *name = ofnode_get_name(node);
Simon Glass3c59abc2014-02-27 13:26:03 -07001187 enum ec_flash_region region;
1188
1189 if (0 == strcmp(name, "ro")) {
1190 region = EC_FLASH_REGION_RO;
1191 } else if (0 == strcmp(name, "rw")) {
Simon Glass2c1e5502018-10-01 12:22:36 -06001192 region = EC_FLASH_REGION_ACTIVE;
Simon Glass3c59abc2014-02-27 13:26:03 -07001193 } else if (0 == strcmp(name, "wp-ro")) {
1194 region = EC_FLASH_REGION_WP_RO;
1195 } else {
1196 debug("Unknown EC flash region name '%s'\n", name);
1197 return -1;
1198 }
1199
Simon Glass4b580932018-06-11 13:07:17 -06001200 if (ofnode_read_fmap_entry(node, &config->region[region])) {
Simon Glass3c59abc2014-02-27 13:26:03 -07001201 debug("Failed to decode flash region in chrome-ec'\n");
1202 return -1;
1203 }
1204 }
1205
1206 return 0;
1207}
1208
Moritz Fischer6cddc602016-09-27 15:42:07 -07001209int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1210 int nmsgs)
Simon Glass9ad07af2015-08-03 08:19:23 -06001211{
Simon Glass9ad07af2015-08-03 08:19:23 -06001212 union {
1213 struct ec_params_i2c_passthru p;
1214 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1215 } params;
1216 union {
1217 struct ec_response_i2c_passthru r;
1218 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1219 } response;
1220 struct ec_params_i2c_passthru *p = &params.p;
1221 struct ec_response_i2c_passthru *r = &response.r;
1222 struct ec_params_i2c_passthru_msg *msg;
1223 uint8_t *pdata, *read_ptr = NULL;
1224 int read_len;
1225 int size;
1226 int rv;
1227 int i;
1228
Moritz Fischer6cddc602016-09-27 15:42:07 -07001229 p->port = port;
Simon Glass9ad07af2015-08-03 08:19:23 -06001230
1231 p->num_msgs = nmsgs;
1232 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1233
1234 /* Create a message to write the register address and optional data */
1235 pdata = (uint8_t *)p + size;
1236
1237 read_len = 0;
1238 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1239 bool is_read = in->flags & I2C_M_RD;
1240
1241 msg->addr_flags = in->addr;
1242 msg->len = in->len;
1243 if (is_read) {
1244 msg->addr_flags |= EC_I2C_FLAG_READ;
1245 read_len += in->len;
1246 read_ptr = in->buf;
1247 if (sizeof(*r) + read_len > sizeof(response)) {
1248 puts("Read length too big for buffer\n");
1249 return -1;
1250 }
1251 } else {
1252 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1253 puts("Params too large for buffer\n");
1254 return -1;
1255 }
1256 memcpy(pdata, in->buf, in->len);
1257 pdata += in->len;
1258 }
1259 }
1260
Simon Glassbed43522018-10-01 12:22:22 -06001261 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
Simon Glass9ad07af2015-08-03 08:19:23 -06001262 r, sizeof(*r) + read_len);
1263 if (rv < 0)
1264 return rv;
1265
1266 /* Parse response */
1267 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1268 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1269 return -1;
1270 }
1271
1272 if (rv < sizeof(*r) + read_len) {
1273 puts("Truncated read response\n");
1274 return -1;
1275 }
1276
1277 /* We only support a single read message for each transfer */
1278 if (read_len)
1279 memcpy(read_ptr, r->data, read_len);
1280
Simon Glass153f2522018-11-06 15:21:22 -07001281 return 0;
1282}
1283
1284int cros_ec_check_feature(struct udevice *dev, int feature)
1285{
1286 struct ec_response_get_features r;
1287 int rv;
1288
1289 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0);
1290 if (rv)
1291 return rv;
1292
1293 if (feature >= 8 * sizeof(r.flags))
1294 return -1;
1295
1296 return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature);
1297}
1298
1299/*
1300 * Query the EC for specified mask indicating enabled events.
1301 * The EC maintains separate event masks for SMI, SCI and WAKE.
1302 */
1303static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
1304 uint64_t *value)
1305{
1306 int ret;
1307 struct ec_params_host_event req;
1308 struct ec_response_host_event rsp;
1309
1310 req.action = action;
1311 req.mask_type = mask;
1312 if (action != EC_HOST_EVENT_GET)
1313 req.value = *value;
1314 else
1315 *value = 0;
1316 ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
1317 sizeof(rsp));
1318
1319 if (action != EC_HOST_EVENT_GET)
1320 return ret;
1321 if (ret == 0)
1322 *value = rsp.value;
1323
1324 return ret;
1325}
1326
1327static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
1328 uint action, uint64_t *value)
1329{
1330 int ret = -1;
1331 struct ec_params_host_event_mask req;
1332 struct ec_response_host_event_mask rsp;
1333
1334 if (hcmd == INVALID_HCMD)
1335 return ret;
1336
1337 if (action != EC_HOST_EVENT_GET)
1338 req.mask = (uint32_t)*value;
1339 else
1340 *value = 0;
1341
1342 ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
1343 if (action != EC_HOST_EVENT_GET)
1344 return ret;
1345 if (ret == 0)
1346 *value = rsp.mask;
1347
1348 return ret;
1349}
1350
1351bool cros_ec_is_uhepi_supported(struct udevice *dev)
1352{
1353#define UHEPI_SUPPORTED 1
1354#define UHEPI_NOT_SUPPORTED 2
1355 static int uhepi_support;
1356
1357 if (!uhepi_support) {
1358 uhepi_support = cros_ec_check_feature(dev,
1359 EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
1360 UHEPI_NOT_SUPPORTED;
1361 log_debug("Chrome EC: UHEPI %s\n",
1362 uhepi_support == UHEPI_SUPPORTED ? "supported" :
1363 "not supported");
1364 }
1365 return uhepi_support == UHEPI_SUPPORTED;
1366}
1367
1368static int cros_ec_get_mask(struct udevice *dev, uint type)
1369{
1370 u64 value = 0;
1371
1372 if (cros_ec_is_uhepi_supported(dev)) {
1373 cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
1374 } else {
1375 assert(type < ARRAY_SIZE(event_map));
1376 cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
1377 EC_HOST_EVENT_GET, &value);
1378 }
1379 return value;
1380}
1381
1382static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
1383{
1384 if (cros_ec_is_uhepi_supported(dev))
1385 return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
1386
1387 assert(type < ARRAY_SIZE(event_map));
1388
1389 return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
1390 EC_HOST_EVENT_CLEAR, &mask);
1391}
1392
1393uint64_t cros_ec_get_events_b(struct udevice *dev)
1394{
1395 return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
1396}
1397
1398int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
1399{
1400 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
1401
1402 return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
1403}
1404
1405int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
1406{
1407 struct ec_params_charge_state p;
1408 struct ec_response_charge_state r;
1409 int ret;
1410
1411 p.cmd = CHARGE_STATE_CMD_GET_PARAM;
1412 p.get_param.param = CS_PARAM_LIMIT_POWER;
1413 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
1414 &r, sizeof(r));
1415
1416 /*
1417 * If our EC doesn't support the LIMIT_POWER parameter, assume that
1418 * LIMIT_POWER is not requested.
1419 */
1420 if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
1421 log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1422 return -ENOSYS;
1423 }
1424
1425 if (ret != sizeof(r.get_param))
1426 return -EINVAL;
1427
1428 *limit_powerp = r.get_param.value;
1429 return 0;
1430}
1431
1432int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
1433{
1434 struct ec_params_config_power_button params;
1435 int ret;
1436
1437 params.flags = flags;
1438 ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
1439 &params, sizeof(params), NULL, 0);
1440 if (ret < 0)
1441 return ret;
1442
1443 return 0;
1444}
1445
1446int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
1447{
1448 u32 mask;
1449 int ret;
1450
1451 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1452 &mask);
1453 if (ret < 0)
1454 return ret;
1455
1456 return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
1457}
1458
1459int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
1460{
1461 u32 mask;
1462 int ret;
1463
1464 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1465 &mask);
1466 if (ret < 0)
1467 return ret;
1468
1469 // Set lid close event state in the EC SMI event mask
1470 if (enable)
1471 mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1472 else
1473 mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1474
1475 ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
1476 if (ret < 0)
1477 return ret;
1478
1479 printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
Simon Glass9ad07af2015-08-03 08:19:23 -06001480 return 0;
1481}
1482
Simon Glass44569b52014-10-13 23:42:14 -06001483UCLASS_DRIVER(cros_ec) = {
1484 .id = UCLASS_CROS_EC,
1485 .name = "cros_ec",
1486 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
Simon Glass18230342016-07-05 17:10:10 -06001487 .post_bind = dm_scan_fdt_dev,
Simon Glass134022a2018-11-06 15:21:21 -07001488 .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,
Simon Glass44569b52014-10-13 23:42:14 -06001489};