blob: c145b67931862857771536d1caaaa2cbe21a0e29 [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>
Simon Glass8e201882020-05-10 11:39:54 -060021#include <flash.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080022#include <i2c.h>
23#include <cros_ec.h>
24#include <fdtdec.h>
25#include <malloc.h>
26#include <spi.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090027#include <linux/errno.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080028#include <asm/io.h>
29#include <asm-generic/gpio.h>
Simon Glass44569b52014-10-13 23:42:14 -060030#include <dm/device-internal.h>
Simon Glassb35e6d62017-05-18 20:09:23 -060031#include <dm/of_extra.h>
Simon Glass44569b52014-10-13 23:42:14 -060032#include <dm/uclass-internal.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080033
34#ifdef DEBUG_TRACE
35#define debug_trace(fmt, b...) debug(fmt, #b)
36#else
37#define debug_trace(fmt, b...)
38#endif
39
40enum {
41 /* Timeout waiting for a flash erase command to complete */
42 CROS_EC_CMD_TIMEOUT_MS = 5000,
43 /* Timeout waiting for a synchronous hash to be recomputed */
44 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
45};
46
Simon Glass153f2522018-11-06 15:21:22 -070047#define INVALID_HCMD 0xFF
48
49/*
50 * Map UHEPI masks to non UHEPI commands in order to support old EC FW
51 * which does not support UHEPI command.
52 */
53static const struct {
54 u8 set_cmd;
55 u8 clear_cmd;
56 u8 get_cmd;
57} event_map[] = {
58 [EC_HOST_EVENT_MAIN] = {
59 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
60 INVALID_HCMD,
61 },
62 [EC_HOST_EVENT_B] = {
63 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
64 EC_CMD_HOST_EVENT_GET_B,
65 },
66 [EC_HOST_EVENT_SCI_MASK] = {
67 EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
68 EC_CMD_HOST_EVENT_GET_SCI_MASK,
69 },
70 [EC_HOST_EVENT_SMI_MASK] = {
71 EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
72 EC_CMD_HOST_EVENT_GET_SMI_MASK,
73 },
74 [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
75 INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
76 },
77 [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
78 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
79 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
80 },
81 [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
82 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
83 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
84 },
85 [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = {
86 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
87 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
88 },
89 [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
90 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
91 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
92 },
93};
94
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080095void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
96{
97#ifdef DEBUG
98 int i;
99
100 printf("%s: ", name);
101 if (cmd != -1)
102 printf("cmd=%#x: ", cmd);
103 for (i = 0; i < len; i++)
104 printf("%02x ", data[i]);
105 printf("\n");
106#endif
107}
108
109/*
110 * Calculate a simple 8-bit checksum of a data block
111 *
112 * @param data Data block to checksum
113 * @param size Size of data block in bytes
114 * @return checksum value (0 to 255)
115 */
116int cros_ec_calc_checksum(const uint8_t *data, int size)
117{
118 int csum, i;
119
120 for (i = csum = 0; i < size; i++)
121 csum += data[i];
122 return csum & 0xff;
123}
124
Simon Glass13c7c5b2014-02-27 13:26:09 -0700125/**
126 * Create a request packet for protocol version 3.
127 *
128 * The packet is stored in the device's internal output buffer.
129 *
130 * @param dev CROS-EC device
131 * @param cmd Command to send (EC_CMD_...)
132 * @param cmd_version Version of command to send (EC_VER_...)
133 * @param dout Output data (may be NULL If dout_len=0)
134 * @param dout_len Size of output data in bytes
135 * @return packet size in bytes, or <0 if error.
136 */
Simon Glassbed43522018-10-01 12:22:22 -0600137static int create_proto3_request(struct cros_ec_dev *cdev,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700138 int cmd, int cmd_version,
139 const void *dout, int dout_len)
140{
Simon Glassbed43522018-10-01 12:22:22 -0600141 struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700142 int out_bytes = dout_len + sizeof(*rq);
143
144 /* Fail if output size is too big */
Simon Glassbed43522018-10-01 12:22:22 -0600145 if (out_bytes > (int)sizeof(cdev->dout)) {
Simon Glass13c7c5b2014-02-27 13:26:09 -0700146 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
147 return -EC_RES_REQUEST_TRUNCATED;
148 }
149
150 /* Fill in request packet */
151 rq->struct_version = EC_HOST_REQUEST_VERSION;
152 rq->checksum = 0;
153 rq->command = cmd;
154 rq->command_version = cmd_version;
155 rq->reserved = 0;
156 rq->data_len = dout_len;
157
158 /* Copy data after header */
159 memcpy(rq + 1, dout, dout_len);
160
161 /* Write checksum field so the entire packet sums to 0 */
Simon Glassbed43522018-10-01 12:22:22 -0600162 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
Simon Glass13c7c5b2014-02-27 13:26:09 -0700163
Simon Glassbed43522018-10-01 12:22:22 -0600164 cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700165
166 /* Return size of request packet */
167 return out_bytes;
168}
169
170/**
171 * Prepare the device to receive a protocol version 3 response.
172 *
173 * @param dev CROS-EC device
174 * @param din_len Maximum size of response in bytes
175 * @return maximum expected number of bytes in response, or <0 if error.
176 */
Simon Glassbed43522018-10-01 12:22:22 -0600177static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
Simon Glass13c7c5b2014-02-27 13:26:09 -0700178{
179 int in_bytes = din_len + sizeof(struct ec_host_response);
180
181 /* Fail if input size is too big */
Simon Glassbed43522018-10-01 12:22:22 -0600182 if (in_bytes > (int)sizeof(cdev->din)) {
Simon Glass13c7c5b2014-02-27 13:26:09 -0700183 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
184 return -EC_RES_RESPONSE_TOO_BIG;
185 }
186
187 /* Return expected size of response packet */
188 return in_bytes;
189}
190
191/**
192 * Handle a protocol version 3 response packet.
193 *
194 * The packet must already be stored in the device's internal input buffer.
195 *
196 * @param dev CROS-EC device
197 * @param dinp Returns pointer to response data
198 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700199 * @return number of bytes of response data, or <0 if error. Note that error
200 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
201 * overlap!)
Simon Glass13c7c5b2014-02-27 13:26:09 -0700202 */
203static int handle_proto3_response(struct cros_ec_dev *dev,
204 uint8_t **dinp, int din_len)
205{
206 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
207 int in_bytes;
208 int csum;
209
210 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
211
212 /* Check input data */
213 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
214 debug("%s: EC response version mismatch\n", __func__);
215 return -EC_RES_INVALID_RESPONSE;
216 }
217
218 if (rs->reserved) {
219 debug("%s: EC response reserved != 0\n", __func__);
220 return -EC_RES_INVALID_RESPONSE;
221 }
222
223 if (rs->data_len > din_len) {
224 debug("%s: EC returned too much data\n", __func__);
225 return -EC_RES_RESPONSE_TOO_BIG;
226 }
227
228 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
229
230 /* Update in_bytes to actual data size */
231 in_bytes = sizeof(*rs) + rs->data_len;
232
233 /* Verify checksum */
234 csum = cros_ec_calc_checksum(dev->din, in_bytes);
235 if (csum) {
236 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
237 csum);
238 return -EC_RES_INVALID_CHECKSUM;
239 }
240
241 /* Return error result, if any */
242 if (rs->result)
243 return -(int)rs->result;
244
245 /* If we're still here, set response data pointer and return length */
246 *dinp = (uint8_t *)(rs + 1);
247
248 return rs->data_len;
249}
250
Simon Glassbed43522018-10-01 12:22:22 -0600251static int send_command_proto3(struct cros_ec_dev *cdev,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700252 int cmd, int cmd_version,
253 const void *dout, int dout_len,
254 uint8_t **dinp, int din_len)
255{
Simon Glass44569b52014-10-13 23:42:14 -0600256 struct dm_cros_ec_ops *ops;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700257 int out_bytes, in_bytes;
258 int rv;
259
260 /* Create request packet */
Simon Glassbed43522018-10-01 12:22:22 -0600261 out_bytes = create_proto3_request(cdev, cmd, cmd_version,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700262 dout, dout_len);
263 if (out_bytes < 0)
264 return out_bytes;
265
266 /* Prepare response buffer */
Simon Glassbed43522018-10-01 12:22:22 -0600267 in_bytes = prepare_proto3_response_buffer(cdev, din_len);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700268 if (in_bytes < 0)
269 return in_bytes;
270
Simon Glassbed43522018-10-01 12:22:22 -0600271 ops = dm_cros_ec_get_ops(cdev->dev);
272 rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
273 -ENOSYS;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700274 if (rv < 0)
275 return rv;
276
277 /* Process the response */
Simon Glassbed43522018-10-01 12:22:22 -0600278 return handle_proto3_response(cdev, dinp, din_len);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700279}
280
Simon Glass3a239512018-11-06 15:21:18 -0700281static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800282 const void *dout, int dout_len,
283 uint8_t **dinp, int din_len)
284{
Simon Glass44569b52014-10-13 23:42:14 -0600285 struct dm_cros_ec_ops *ops;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700286 int ret = -1;
287
288 /* Handle protocol version 3 support */
289 if (dev->protocol_version == 3) {
290 return send_command_proto3(dev, cmd, cmd_version,
291 dout, dout_len, dinp, din_len);
292 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800293
Simon Glass44569b52014-10-13 23:42:14 -0600294 ops = dm_cros_ec_get_ops(dev->dev);
295 ret = ops->command(dev->dev, cmd, cmd_version,
296 (const uint8_t *)dout, dout_len, dinp, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800297
298 return ret;
299}
300
301/**
302 * Send a command to the CROS-EC device and return the reply.
303 *
304 * The device's internal input/output buffers are used.
305 *
306 * @param dev CROS-EC device
307 * @param cmd Command to send (EC_CMD_...)
308 * @param cmd_version Version of command to send (EC_VER_...)
309 * @param dout Output data (may be NULL If dout_len=0)
310 * @param dout_len Size of output data in bytes
311 * @param dinp Response data (may be NULL If din_len=0).
312 * If not NULL, it will be updated to point to the data
313 * and will always be double word aligned (64-bits)
314 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700315 * @return number of bytes in response, or -ve on error
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800316 */
Michael Auchter4eaae8e2019-12-09 20:27:31 +0000317static int ec_command_inptr(struct udevice *dev, uint cmd,
Simon Glass699c9ca2018-10-01 12:22:08 -0600318 int cmd_version, const void *dout, int dout_len,
319 uint8_t **dinp, int din_len)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800320{
Simon Glassbed43522018-10-01 12:22:22 -0600321 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Simon Glass19cc2952014-02-27 13:26:11 -0700322 uint8_t *din = NULL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800323 int len;
324
Simon Glassbed43522018-10-01 12:22:22 -0600325 len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
326 din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800327
328 /* If the command doesn't complete, wait a while */
329 if (len == -EC_RES_IN_PROGRESS) {
Simon Glass19cc2952014-02-27 13:26:11 -0700330 struct ec_response_get_comms_status *resp = NULL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800331 ulong start;
332
333 /* Wait for command to complete */
334 start = get_timer(0);
335 do {
336 int ret;
337
338 mdelay(50); /* Insert some reasonable delay */
Simon Glassbed43522018-10-01 12:22:22 -0600339 ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
340 NULL, 0,
341 (uint8_t **)&resp, sizeof(*resp));
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800342 if (ret < 0)
343 return ret;
344
345 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
346 debug("%s: Command %#02x timeout\n",
347 __func__, cmd);
348 return -EC_RES_TIMEOUT;
349 }
350 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
351
352 /* OK it completed, so read the status response */
353 /* not sure why it was 0 for the last argument */
Simon Glassbed43522018-10-01 12:22:22 -0600354 len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
355 &din, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800356 }
357
Simon Glass0b8e8ce2017-05-18 20:09:22 -0600358 debug("%s: len=%d, din=%p\n", __func__, len, din);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800359 if (dinp) {
360 /* If we have any data to return, it must be 64bit-aligned */
361 assert(len <= 0 || !((uintptr_t)din & 7));
362 *dinp = din;
363 }
364
365 return len;
366}
367
368/**
369 * Send a command to the CROS-EC device and return the reply.
370 *
371 * The device's internal input/output buffers are used.
372 *
373 * @param dev CROS-EC device
374 * @param cmd Command to send (EC_CMD_...)
375 * @param cmd_version Version of command to send (EC_VER_...)
376 * @param dout Output data (may be NULL If dout_len=0)
377 * @param dout_len Size of output data in bytes
378 * @param din Response data (may be NULL If din_len=0).
379 * It not NULL, it is a place for ec_command() to copy the
380 * data to.
381 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700382 * @return number of bytes in response, or -ve on error
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800383 */
Simon Glass3a239512018-11-06 15:21:18 -0700384static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800385 const void *dout, int dout_len,
386 void *din, int din_len)
387{
388 uint8_t *in_buffer;
389 int len;
390
391 assert((din_len == 0) || din);
392 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
Simon Glassbed43522018-10-01 12:22:22 -0600393 &in_buffer, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800394 if (len > 0) {
395 /*
396 * If we were asked to put it somewhere, do so, otherwise just
397 * disregard the result.
398 */
399 if (din && in_buffer) {
400 assert(len <= din_len);
401 memmove(din, in_buffer, len);
402 }
403 }
404 return len;
405}
406
Simon Glassfb726402015-10-18 21:17:14 -0600407int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800408{
Simon Glassbed43522018-10-01 12:22:22 -0600409 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
Simon Glass19cc2952014-02-27 13:26:11 -0700410 sizeof(scan->data)) != sizeof(scan->data))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800411 return -1;
412
413 return 0;
414}
415
Simon Glassbed43522018-10-01 12:22:22 -0600416int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800417{
418 struct ec_response_get_version *r;
Simon Glassa77024d2018-11-06 15:21:19 -0700419 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800420
Simon Glassa77024d2018-11-06 15:21:19 -0700421 ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
422 (uint8_t **)&r, sizeof(*r));
423 if (ret != sizeof(*r)) {
Simon Glass48d268f2018-11-23 21:29:36 -0700424 log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800425 return -1;
Simon Glassa77024d2018-11-06 15:21:19 -0700426 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800427
Simon Glass19cc2952014-02-27 13:26:11 -0700428 if (maxlen > (int)sizeof(r->version_string_ro))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800429 maxlen = sizeof(r->version_string_ro);
430
431 switch (r->current_image) {
432 case EC_IMAGE_RO:
433 memcpy(id, r->version_string_ro, maxlen);
434 break;
435 case EC_IMAGE_RW:
436 memcpy(id, r->version_string_rw, maxlen);
437 break;
438 default:
Simon Glassa77024d2018-11-06 15:21:19 -0700439 log_err("Invalid EC image %d\n", r->current_image);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800440 return -1;
441 }
442
443 id[maxlen - 1] = '\0';
444 return 0;
445}
446
Simon Glassbed43522018-10-01 12:22:22 -0600447int cros_ec_read_version(struct udevice *dev,
448 struct ec_response_get_version **versionp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800449{
450 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
451 (uint8_t **)versionp, sizeof(**versionp))
Simon Glass19cc2952014-02-27 13:26:11 -0700452 != sizeof(**versionp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800453 return -1;
454
455 return 0;
456}
457
Simon Glassbed43522018-10-01 12:22:22 -0600458int cros_ec_read_build_info(struct udevice *dev, char **strp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800459{
460 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
Simon Glassece28862014-02-27 13:26:07 -0700461 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800462 return -1;
463
464 return 0;
465}
466
Simon Glassbed43522018-10-01 12:22:22 -0600467int cros_ec_read_current_image(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600468 enum ec_current_image *image)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800469{
470 struct ec_response_get_version *r;
471
472 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700473 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800474 return -1;
475
476 *image = r->current_image;
477 return 0;
478}
479
Simon Glassbed43522018-10-01 12:22:22 -0600480static int cros_ec_wait_on_hash_done(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600481 struct ec_response_vboot_hash *hash)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800482{
483 struct ec_params_vboot_hash p;
484 ulong start;
485
486 start = get_timer(0);
487 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
488 mdelay(50); /* Insert some reasonable delay */
489
490 p.cmd = EC_VBOOT_HASH_GET;
491 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
492 hash, sizeof(*hash)) < 0)
493 return -1;
494
495 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
496 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
497 return -EC_RES_TIMEOUT;
498 }
499 }
500 return 0;
501}
502
Simon Glass94564902018-10-01 12:22:38 -0600503int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
504 struct ec_response_vboot_hash *hash)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800505{
506 struct ec_params_vboot_hash p;
507 int rv;
508
509 p.cmd = EC_VBOOT_HASH_GET;
Simon Glass94564902018-10-01 12:22:38 -0600510 p.offset = hash_offset;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800511 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
512 hash, sizeof(*hash)) < 0)
513 return -1;
514
515 /* If the EC is busy calculating the hash, fidget until it's done. */
516 rv = cros_ec_wait_on_hash_done(dev, hash);
517 if (rv)
518 return rv;
519
520 /* If the hash is valid, we're done. Otherwise, we have to kick it off
521 * again and wait for it to complete. Note that we explicitly assume
522 * that hashing zero bytes is always wrong, even though that would
523 * produce a valid hash value. */
524 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
525 return 0;
526
527 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
528 __func__, hash->status, hash->size);
529
Simon Glassece28862014-02-27 13:26:07 -0700530 p.cmd = EC_VBOOT_HASH_START;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800531 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
532 p.nonce_size = 0;
Simon Glass94564902018-10-01 12:22:38 -0600533 p.offset = hash_offset;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800534
535 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
536 hash, sizeof(*hash)) < 0)
537 return -1;
538
539 rv = cros_ec_wait_on_hash_done(dev, hash);
540 if (rv)
541 return rv;
542
543 debug("%s: hash done\n", __func__);
544
545 return 0;
546}
547
Simon Glassbed43522018-10-01 12:22:22 -0600548static int cros_ec_invalidate_hash(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800549{
550 struct ec_params_vboot_hash p;
551 struct ec_response_vboot_hash *hash;
552
553 /* We don't have an explict command for the EC to discard its current
554 * hash value, so we'll just tell it to calculate one that we know is
555 * wrong (we claim that hashing zero bytes is always invalid).
556 */
557 p.cmd = EC_VBOOT_HASH_RECALC;
558 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
559 p.nonce_size = 0;
560 p.offset = 0;
561 p.size = 0;
562
563 debug("%s:\n", __func__);
564
565 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
566 (uint8_t **)&hash, sizeof(*hash)) < 0)
567 return -1;
568
569 /* No need to wait for it to finish */
570 return 0;
571}
572
Simon Glassbed43522018-10-01 12:22:22 -0600573int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800574{
575 struct ec_params_reboot_ec p;
576
577 p.cmd = cmd;
578 p.flags = flags;
579
580 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
581 < 0)
582 return -1;
583
584 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
585 /*
586 * EC reboot will take place immediately so delay to allow it
587 * to complete. Note that some reboot types (EC_REBOOT_COLD)
588 * will reboot the AP as well, in which case we won't actually
589 * get to this point.
590 */
591 /*
592 * TODO(rspangler@chromium.org): Would be nice if we had a
593 * better way to determine when the reboot is complete. Could
594 * we poll a memory-mapped LPC value?
595 */
596 udelay(50000);
597 }
598
599 return 0;
600}
601
Simon Glassfb726402015-10-18 21:17:14 -0600602int cros_ec_interrupt_pending(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800603{
Simon Glassfb726402015-10-18 21:17:14 -0600604 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
605
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800606 /* no interrupt support : always poll */
Simon Glassfb726402015-10-18 21:17:14 -0600607 if (!dm_gpio_is_valid(&cdev->ec_int))
Simon Glass19cc2952014-02-27 13:26:11 -0700608 return -ENOENT;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800609
Simon Glassfb726402015-10-18 21:17:14 -0600610 return dm_gpio_get_value(&cdev->ec_int);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800611}
612
Simon Glassbed43522018-10-01 12:22:22 -0600613int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800614{
Simon Glassece28862014-02-27 13:26:07 -0700615 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
Simon Glass19cc2952014-02-27 13:26:11 -0700616 sizeof(*info)) != sizeof(*info))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800617 return -1;
618
619 return 0;
620}
621
Simon Glass153f2522018-11-06 15:21:22 -0700622int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
623{
624 struct ec_response_host_event_mask rsp;
625 int ret;
626
627 ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
628 if (ret < 0)
629 return ret;
630 else if (ret != sizeof(rsp))
631 return -EINVAL;
632
633 *mask = rsp.mask;
634
635 return 0;
636}
637
638int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
639{
640 struct ec_params_host_event_mask req;
641 int ret;
642
643 req.mask = mask;
644
645 ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
646 if (ret < 0)
647 return ret;
648
649 return 0;
650}
651
Simon Glassbed43522018-10-01 12:22:22 -0600652int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800653{
654 struct ec_response_host_event_mask *resp;
655
656 /*
657 * Use the B copy of the event flags, because the main copy is already
658 * used by ACPI/SMI.
659 */
660 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700661 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800662 return -1;
663
664 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
665 return -1;
666
667 *events_ptr = resp->mask;
668 return 0;
669}
670
Simon Glassbed43522018-10-01 12:22:22 -0600671int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800672{
673 struct ec_params_host_event_mask params;
674
675 params.mask = events;
676
677 /*
678 * Use the B copy of the event flags, so it affects the data returned
679 * by cros_ec_get_host_events().
680 */
681 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
682 &params, sizeof(params), NULL, 0) < 0)
683 return -1;
684
685 return 0;
686}
687
Simon Glassbed43522018-10-01 12:22:22 -0600688int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
689 uint32_t set_flags,
Simon Glass699c9ca2018-10-01 12:22:08 -0600690 struct ec_response_flash_protect *resp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800691{
692 struct ec_params_flash_protect params;
693
694 params.mask = set_mask;
695 params.flags = set_flags;
696
697 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
698 &params, sizeof(params),
Simon Glass19cc2952014-02-27 13:26:11 -0700699 resp, sizeof(*resp)) != sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800700 return -1;
701
702 return 0;
703}
704
Simon Glass153f2522018-11-06 15:21:22 -0700705int cros_ec_entering_mode(struct udevice *dev, int mode)
706{
707 int rc;
708
709 rc = ec_command(dev, EC_CMD_ENTERING_MODE, 0, &mode, sizeof(mode),
710 NULL, 0);
711 if (rc)
712 return -1;
713 return 0;
714}
715
Simon Glassbed43522018-10-01 12:22:22 -0600716static int cros_ec_check_version(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800717{
Simon Glassbed43522018-10-01 12:22:22 -0600718 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800719 struct ec_params_hello req;
720 struct ec_response_hello *resp;
721
Simon Glass23fb1562015-03-26 09:29:30 -0600722 struct dm_cros_ec_ops *ops;
723 int ret;
724
Simon Glassbed43522018-10-01 12:22:22 -0600725 ops = dm_cros_ec_get_ops(dev);
Simon Glass23fb1562015-03-26 09:29:30 -0600726 if (ops->check_version) {
Simon Glassbed43522018-10-01 12:22:22 -0600727 ret = ops->check_version(dev);
Simon Glass23fb1562015-03-26 09:29:30 -0600728 if (ret)
729 return ret;
730 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800731
732 /*
733 * TODO(sjg@chromium.org).
734 * There is a strange oddity here with the EC. We could just ignore
735 * the response, i.e. pass the last two parameters as NULL and 0.
736 * In this case we won't read back very many bytes from the EC.
737 * On the I2C bus the EC gets upset about this and will try to send
738 * the bytes anyway. This means that we will have to wait for that
739 * to complete before continuing with a new EC command.
740 *
741 * This problem is probably unique to the I2C bus.
742 *
743 * So for now, just read all the data anyway.
744 */
Randall Spanglerf03e9702014-02-27 13:26:08 -0700745
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700746 /* Try sending a version 3 packet */
Simon Glassbed43522018-10-01 12:22:22 -0600747 cdev->protocol_version = 3;
Simon Glass73c1ecf2014-11-11 18:06:30 -0700748 req.in_data = 0;
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700749 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
Simon Glass3a239512018-11-06 15:21:18 -0700750 (uint8_t **)&resp, sizeof(*resp)) > 0)
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700751 return 0;
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700752
Randall Spanglerf03e9702014-02-27 13:26:08 -0700753 /* Try sending a version 2 packet */
Simon Glassbed43522018-10-01 12:22:22 -0600754 cdev->protocol_version = 2;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800755 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
Simon Glass3a239512018-11-06 15:21:18 -0700756 (uint8_t **)&resp, sizeof(*resp)) > 0)
Randall Spanglerf03e9702014-02-27 13:26:08 -0700757 return 0;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800758
Randall Spanglerf03e9702014-02-27 13:26:08 -0700759 /*
760 * Fail if we're still here, since the EC doesn't understand any
761 * protcol version we speak. Version 1 interface without command
762 * version is no longer supported, and we don't know about any new
763 * protocol versions.
764 */
Simon Glassbed43522018-10-01 12:22:22 -0600765 cdev->protocol_version = 0;
Randall Spanglerf03e9702014-02-27 13:26:08 -0700766 printf("%s: ERROR: old EC interface not supported\n", __func__);
767 return -1;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800768}
769
Simon Glassbed43522018-10-01 12:22:22 -0600770int cros_ec_test(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800771{
772 struct ec_params_hello req;
773 struct ec_response_hello *resp;
774
775 req.in_data = 0x12345678;
776 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
777 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
778 printf("ec_command_inptr() returned error\n");
779 return -1;
780 }
781 if (resp->out_data != req.in_data + 0x01020304) {
782 printf("Received invalid handshake %x\n", resp->out_data);
783 return -1;
784 }
785
786 return 0;
787}
788
Simon Glassbed43522018-10-01 12:22:22 -0600789int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800790 uint32_t *offset, uint32_t *size)
791{
792 struct ec_params_flash_region_info p;
793 struct ec_response_flash_region_info *r;
794 int ret;
795
796 p.region = region;
797 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
798 EC_VER_FLASH_REGION_INFO,
799 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
800 if (ret != sizeof(*r))
801 return -1;
802
803 if (offset)
804 *offset = r->offset;
805 if (size)
806 *size = r->size;
807
808 return 0;
809}
810
Simon Glassbed43522018-10-01 12:22:22 -0600811int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800812{
813 struct ec_params_flash_erase p;
814
815 p.offset = offset;
816 p.size = size;
817 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
818 NULL, 0);
819}
820
821/**
822 * Write a single block to the flash
823 *
824 * Write a block of data to the EC flash. The size must not exceed the flash
825 * write block size which you can obtain from cros_ec_flash_write_burst_size().
826 *
827 * The offset starts at 0. You can obtain the region information from
828 * cros_ec_flash_offset() to find out where to write for a particular region.
829 *
830 * Attempting to write to the region where the EC is currently running from
831 * will result in an error.
832 *
833 * @param dev CROS-EC device
834 * @param data Pointer to data buffer to write
835 * @param offset Offset within flash to write to.
836 * @param size Number of bytes to write
837 * @return 0 if ok, -1 on error
838 */
Simon Glassbed43522018-10-01 12:22:22 -0600839static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
840 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800841{
Moritz Fischere597bc72016-09-12 12:57:52 -0700842 struct ec_params_flash_write *p;
843 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800844
Moritz Fischere597bc72016-09-12 12:57:52 -0700845 p = malloc(sizeof(*p) + size);
846 if (!p)
847 return -ENOMEM;
848
849 p->offset = offset;
850 p->size = size;
851 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
852 memcpy(p + 1, data, p->size);
853
854 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
855 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
856
857 free(p);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800858
Moritz Fischere597bc72016-09-12 12:57:52 -0700859 return ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800860}
861
862/**
863 * Return optimal flash write burst size
864 */
Simon Glassbed43522018-10-01 12:22:22 -0600865static int cros_ec_flash_write_burst_size(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800866{
Simon Glassece28862014-02-27 13:26:07 -0700867 return EC_FLASH_WRITE_VER0_SIZE;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800868}
869
870/**
871 * Check if a block of data is erased (all 0xff)
872 *
873 * This function is useful when dealing with flash, for checking whether a
874 * data block is erased and thus does not need to be programmed.
875 *
876 * @param data Pointer to data to check (must be word-aligned)
877 * @param size Number of bytes to check (must be word-aligned)
878 * @return 0 if erased, non-zero if any word is not erased
879 */
880static int cros_ec_data_is_erased(const uint32_t *data, int size)
881{
882 assert(!(size & 3));
883 size /= sizeof(uint32_t);
884 for (; size > 0; size -= 4, data++)
885 if (*data != -1U)
886 return 0;
887
888 return 1;
889}
890
Moritz Fischerde5bc892016-09-13 14:44:48 -0700891/**
892 * Read back flash parameters
893 *
894 * This function reads back parameters of the flash as reported by the EC
895 *
896 * @param dev Pointer to device
897 * @param info Pointer to output flash info struct
898 */
Simon Glassbed43522018-10-01 12:22:22 -0600899int cros_ec_read_flashinfo(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600900 struct ec_response_flash_info *info)
Moritz Fischerde5bc892016-09-13 14:44:48 -0700901{
902 int ret;
903
904 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
905 NULL, 0, info, sizeof(*info));
906 if (ret < 0)
907 return ret;
908
909 return ret < sizeof(*info) ? -1 : 0;
910}
911
Simon Glassbed43522018-10-01 12:22:22 -0600912int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
Simon Glass699c9ca2018-10-01 12:22:08 -0600913 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800914{
Simon Glassbed43522018-10-01 12:22:22 -0600915 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800916 uint32_t burst = cros_ec_flash_write_burst_size(dev);
917 uint32_t end, off;
918 int ret;
919
Simon Glass411661d2018-11-06 15:21:20 -0700920 if (!burst)
921 return -EINVAL;
922
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800923 /*
924 * TODO: round up to the nearest multiple of write size. Can get away
925 * without that on link right now because its write size is 4 bytes.
926 */
927 end = offset + size;
928 for (off = offset; off < end; off += burst, data += burst) {
929 uint32_t todo;
930
931 /* If the data is empty, there is no point in programming it */
932 todo = min(end - off, burst);
Simon Glassbed43522018-10-01 12:22:22 -0600933 if (cdev->optimise_flash_write &&
Simon Glass699c9ca2018-10-01 12:22:08 -0600934 cros_ec_data_is_erased((uint32_t *)data, todo))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800935 continue;
936
937 ret = cros_ec_flash_write_block(dev, data, off, todo);
938 if (ret)
939 return ret;
940 }
941
942 return 0;
943}
944
945/**
Simon Glass153f2522018-11-06 15:21:22 -0700946 * Run verification on a slot
947 *
948 * @param me CrosEc instance
949 * @param region Region to run verification on
950 * @return 0 if success or not applicable. Non-zero if verification failed.
951 */
952int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
953{
954 struct ec_params_efs_verify p;
955 int rv;
956
957 log_info("EFS: EC is verifying updated image...\n");
958 p.region = region;
959
960 rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
961 if (rv >= 0) {
962 log_info("EFS: Verification success\n");
963 return 0;
964 }
965 if (rv == -EC_RES_INVALID_COMMAND) {
966 log_info("EFS: EC doesn't support EFS_VERIFY command\n");
967 return 0;
968 }
969 log_info("EFS: Verification failed\n");
970
971 return rv;
972}
973
974/**
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800975 * Read a single block from the flash
976 *
977 * Read a block of data from the EC flash. The size must not exceed the flash
978 * write block size which you can obtain from cros_ec_flash_write_burst_size().
979 *
980 * The offset starts at 0. You can obtain the region information from
981 * cros_ec_flash_offset() to find out where to read for a particular region.
982 *
983 * @param dev CROS-EC device
984 * @param data Pointer to data buffer to read into
985 * @param offset Offset within flash to read from
986 * @param size Number of bytes to read
987 * @return 0 if ok, -1 on error
988 */
Simon Glassbed43522018-10-01 12:22:22 -0600989static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
Simon Glass699c9ca2018-10-01 12:22:08 -0600990 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800991{
992 struct ec_params_flash_read p;
993
994 p.offset = offset;
995 p.size = size;
996
997 return ec_command(dev, EC_CMD_FLASH_READ, 0,
998 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
999}
1000
Simon Glassbed43522018-10-01 12:22:22 -06001001int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
Simon Glass699c9ca2018-10-01 12:22:08 -06001002 uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001003{
1004 uint32_t burst = cros_ec_flash_write_burst_size(dev);
1005 uint32_t end, off;
1006 int ret;
1007
1008 end = offset + size;
1009 for (off = offset; off < end; off += burst, data += burst) {
1010 ret = cros_ec_flash_read_block(dev, data, off,
1011 min(end - off, burst));
1012 if (ret)
1013 return ret;
1014 }
1015
1016 return 0;
1017}
1018
Simon Glassbed43522018-10-01 12:22:22 -06001019int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
Simon Glass699c9ca2018-10-01 12:22:08 -06001020 int image_size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001021{
1022 uint32_t rw_offset, rw_size;
1023 int ret;
1024
Simon Glass2c1e5502018-10-01 12:22:36 -06001025 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
1026 &rw_size))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001027 return -1;
Simon Glass19cc2952014-02-27 13:26:11 -07001028 if (image_size > (int)rw_size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001029 return -1;
1030
1031 /* Invalidate the existing hash, just in case the AP reboots
1032 * unexpectedly during the update. If that happened, the EC RW firmware
1033 * would be invalid, but the EC would still have the original hash.
1034 */
1035 ret = cros_ec_invalidate_hash(dev);
1036 if (ret)
1037 return ret;
1038
1039 /*
1040 * Erase the entire RW section, so that the EC doesn't see any garbage
1041 * past the new image if it's smaller than the current image.
1042 *
1043 * TODO: could optimize this to erase just the current image, since
1044 * presumably everything past that is 0xff's. But would still need to
1045 * round up to the nearest multiple of erase size.
1046 */
1047 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
1048 if (ret)
1049 return ret;
1050
1051 /* Write the image */
1052 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
1053 if (ret)
1054 return ret;
1055
1056 return 0;
1057}
1058
Simon Glassbed43522018-10-01 12:22:22 -06001059int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001060{
1061 struct ec_params_vbnvcontext p;
1062 int len;
1063
Simon Glass153f2522018-11-06 15:21:22 -07001064 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
Simon Glassbed43522018-10-01 12:22:22 -06001065 return -EINVAL;
1066
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001067 p.op = EC_VBNV_CONTEXT_OP_READ;
1068
1069 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
Simon Glass153f2522018-11-06 15:21:22 -07001070 &p, sizeof(uint32_t) + size, block, size);
1071 if (len != size) {
1072 log_err("Expected %d bytes, got %d\n", size, len);
Simon Glassbed43522018-10-01 12:22:22 -06001073 return -EIO;
Simon Glass153f2522018-11-06 15:21:22 -07001074 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001075
1076 return 0;
1077}
1078
Simon Glassbed43522018-10-01 12:22:22 -06001079int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001080{
1081 struct ec_params_vbnvcontext p;
1082 int len;
1083
Simon Glass153f2522018-11-06 15:21:22 -07001084 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
Simon Glassbed43522018-10-01 12:22:22 -06001085 return -EINVAL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001086 p.op = EC_VBNV_CONTEXT_OP_WRITE;
Simon Glass153f2522018-11-06 15:21:22 -07001087 memcpy(p.block, block, size);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001088
1089 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
Simon Glass153f2522018-11-06 15:21:22 -07001090 &p, sizeof(uint32_t) + size, NULL, 0);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001091 if (len < 0)
1092 return -1;
1093
Simon Glass153f2522018-11-06 15:21:22 -07001094 return 0;
1095}
1096
1097int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
1098{
1099 struct ec_params_battery_cutoff p;
1100 int len;
1101
1102 p.flags = flags;
1103 len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
1104 NULL, 0);
1105
1106 if (len < 0)
1107 return -1;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001108 return 0;
1109}
1110
Simon Glasseb2cc512015-08-03 08:19:24 -06001111int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001112{
1113 struct ec_params_ldo_set params;
1114
1115 params.index = index;
1116 params.state = state;
1117
Simon Glassbed43522018-10-01 12:22:22 -06001118 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
Simon Glasseb2cc512015-08-03 08:19:24 -06001119 NULL, 0))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001120 return -1;
1121
1122 return 0;
1123}
1124
Simon Glasseb2cc512015-08-03 08:19:24 -06001125int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001126{
1127 struct ec_params_ldo_get params;
1128 struct ec_response_ldo_get *resp;
1129
1130 params.index = index;
1131
Simon Glassbed43522018-10-01 12:22:22 -06001132 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
Simon Glasseb2cc512015-08-03 08:19:24 -06001133 (uint8_t **)&resp, sizeof(*resp)) !=
1134 sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001135 return -1;
1136
1137 *state = resp->state;
1138
1139 return 0;
1140}
1141
Simon Glass44569b52014-10-13 23:42:14 -06001142int cros_ec_register(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001143{
Simon Glassde0977b2015-03-05 12:25:20 -07001144 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001145 char id[MSG_BYTES];
Simon Glass44569b52014-10-13 23:42:14 -06001146
1147 cdev->dev = dev;
Simon Glass0d7cc472015-01-05 20:05:32 -07001148 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1149 GPIOD_IS_IN);
Simon Glassb35e6d62017-05-18 20:09:23 -06001150 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
Simon Glass44569b52014-10-13 23:42:14 -06001151
Simon Glassbed43522018-10-01 12:22:22 -06001152 if (cros_ec_check_version(dev)) {
Simon Glass44569b52014-10-13 23:42:14 -06001153 debug("%s: Could not detect CROS-EC version\n", __func__);
1154 return -CROS_EC_ERR_CHECK_VERSION;
1155 }
1156
Simon Glassbed43522018-10-01 12:22:22 -06001157 if (cros_ec_read_id(dev, id, sizeof(id))) {
Simon Glass44569b52014-10-13 23:42:14 -06001158 debug("%s: Could not read KBC ID\n", __func__);
1159 return -CROS_EC_ERR_READ_ID;
1160 }
1161
1162 /* Remember this device for use by the cros_ec command */
Simon Glass46e17332015-02-17 15:29:36 -07001163 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1164 cdev->protocol_version, id);
Simon Glass44569b52014-10-13 23:42:14 -06001165
1166 return 0;
1167}
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001168
Simon Glassb35e6d62017-05-18 20:09:23 -06001169int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
Simon Glass3c59abc2014-02-27 13:26:03 -07001170{
Simon Glassb35e6d62017-05-18 20:09:23 -06001171 ofnode flash_node, node;
Simon Glass3c59abc2014-02-27 13:26:03 -07001172
Simon Glassb35e6d62017-05-18 20:09:23 -06001173 flash_node = dev_read_subnode(dev, "flash");
1174 if (!ofnode_valid(flash_node)) {
Simon Glass3c59abc2014-02-27 13:26:03 -07001175 debug("Failed to find flash node\n");
1176 return -1;
1177 }
1178
Simon Glass4b580932018-06-11 13:07:17 -06001179 if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
Simon Glassb35e6d62017-05-18 20:09:23 -06001180 debug("Failed to decode flash node in chrome-ec\n");
Simon Glass3c59abc2014-02-27 13:26:03 -07001181 return -1;
1182 }
1183
Simon Glassb35e6d62017-05-18 20:09:23 -06001184 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1185 "erase-value", -1);
Simon Glass28529762017-08-05 15:45:54 -06001186 ofnode_for_each_subnode(node, flash_node) {
Simon Glassb35e6d62017-05-18 20:09:23 -06001187 const char *name = ofnode_get_name(node);
Simon Glass3c59abc2014-02-27 13:26:03 -07001188 enum ec_flash_region region;
1189
1190 if (0 == strcmp(name, "ro")) {
1191 region = EC_FLASH_REGION_RO;
1192 } else if (0 == strcmp(name, "rw")) {
Simon Glass2c1e5502018-10-01 12:22:36 -06001193 region = EC_FLASH_REGION_ACTIVE;
Simon Glass3c59abc2014-02-27 13:26:03 -07001194 } else if (0 == strcmp(name, "wp-ro")) {
1195 region = EC_FLASH_REGION_WP_RO;
1196 } else {
1197 debug("Unknown EC flash region name '%s'\n", name);
1198 return -1;
1199 }
1200
Simon Glass4b580932018-06-11 13:07:17 -06001201 if (ofnode_read_fmap_entry(node, &config->region[region])) {
Simon Glass3c59abc2014-02-27 13:26:03 -07001202 debug("Failed to decode flash region in chrome-ec'\n");
1203 return -1;
1204 }
1205 }
1206
1207 return 0;
1208}
1209
Moritz Fischer6cddc602016-09-27 15:42:07 -07001210int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1211 int nmsgs)
Simon Glass9ad07af2015-08-03 08:19:23 -06001212{
Simon Glass9ad07af2015-08-03 08:19:23 -06001213 union {
1214 struct ec_params_i2c_passthru p;
1215 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1216 } params;
1217 union {
1218 struct ec_response_i2c_passthru r;
1219 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1220 } response;
1221 struct ec_params_i2c_passthru *p = &params.p;
1222 struct ec_response_i2c_passthru *r = &response.r;
1223 struct ec_params_i2c_passthru_msg *msg;
1224 uint8_t *pdata, *read_ptr = NULL;
1225 int read_len;
1226 int size;
1227 int rv;
1228 int i;
1229
Moritz Fischer6cddc602016-09-27 15:42:07 -07001230 p->port = port;
Simon Glass9ad07af2015-08-03 08:19:23 -06001231
1232 p->num_msgs = nmsgs;
1233 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1234
1235 /* Create a message to write the register address and optional data */
1236 pdata = (uint8_t *)p + size;
1237
1238 read_len = 0;
1239 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1240 bool is_read = in->flags & I2C_M_RD;
1241
1242 msg->addr_flags = in->addr;
1243 msg->len = in->len;
1244 if (is_read) {
1245 msg->addr_flags |= EC_I2C_FLAG_READ;
1246 read_len += in->len;
1247 read_ptr = in->buf;
1248 if (sizeof(*r) + read_len > sizeof(response)) {
1249 puts("Read length too big for buffer\n");
1250 return -1;
1251 }
1252 } else {
1253 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1254 puts("Params too large for buffer\n");
1255 return -1;
1256 }
1257 memcpy(pdata, in->buf, in->len);
1258 pdata += in->len;
1259 }
1260 }
1261
Simon Glassbed43522018-10-01 12:22:22 -06001262 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
Simon Glass9ad07af2015-08-03 08:19:23 -06001263 r, sizeof(*r) + read_len);
1264 if (rv < 0)
1265 return rv;
1266
1267 /* Parse response */
1268 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1269 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1270 return -1;
1271 }
1272
1273 if (rv < sizeof(*r) + read_len) {
1274 puts("Truncated read response\n");
1275 return -1;
1276 }
1277
1278 /* We only support a single read message for each transfer */
1279 if (read_len)
1280 memcpy(read_ptr, r->data, read_len);
1281
Simon Glass153f2522018-11-06 15:21:22 -07001282 return 0;
1283}
1284
1285int cros_ec_check_feature(struct udevice *dev, int feature)
1286{
1287 struct ec_response_get_features r;
1288 int rv;
1289
1290 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0);
1291 if (rv)
1292 return rv;
1293
1294 if (feature >= 8 * sizeof(r.flags))
1295 return -1;
1296
1297 return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature);
1298}
1299
1300/*
1301 * Query the EC for specified mask indicating enabled events.
1302 * The EC maintains separate event masks for SMI, SCI and WAKE.
1303 */
1304static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
1305 uint64_t *value)
1306{
1307 int ret;
1308 struct ec_params_host_event req;
1309 struct ec_response_host_event rsp;
1310
1311 req.action = action;
1312 req.mask_type = mask;
1313 if (action != EC_HOST_EVENT_GET)
1314 req.value = *value;
1315 else
1316 *value = 0;
1317 ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
1318 sizeof(rsp));
1319
1320 if (action != EC_HOST_EVENT_GET)
1321 return ret;
1322 if (ret == 0)
1323 *value = rsp.value;
1324
1325 return ret;
1326}
1327
1328static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
1329 uint action, uint64_t *value)
1330{
1331 int ret = -1;
1332 struct ec_params_host_event_mask req;
1333 struct ec_response_host_event_mask rsp;
1334
1335 if (hcmd == INVALID_HCMD)
1336 return ret;
1337
1338 if (action != EC_HOST_EVENT_GET)
1339 req.mask = (uint32_t)*value;
1340 else
1341 *value = 0;
1342
1343 ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
1344 if (action != EC_HOST_EVENT_GET)
1345 return ret;
1346 if (ret == 0)
1347 *value = rsp.mask;
1348
1349 return ret;
1350}
1351
1352bool cros_ec_is_uhepi_supported(struct udevice *dev)
1353{
1354#define UHEPI_SUPPORTED 1
1355#define UHEPI_NOT_SUPPORTED 2
1356 static int uhepi_support;
1357
1358 if (!uhepi_support) {
1359 uhepi_support = cros_ec_check_feature(dev,
1360 EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
1361 UHEPI_NOT_SUPPORTED;
1362 log_debug("Chrome EC: UHEPI %s\n",
1363 uhepi_support == UHEPI_SUPPORTED ? "supported" :
1364 "not supported");
1365 }
1366 return uhepi_support == UHEPI_SUPPORTED;
1367}
1368
1369static int cros_ec_get_mask(struct udevice *dev, uint type)
1370{
1371 u64 value = 0;
1372
1373 if (cros_ec_is_uhepi_supported(dev)) {
1374 cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
1375 } else {
1376 assert(type < ARRAY_SIZE(event_map));
1377 cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
1378 EC_HOST_EVENT_GET, &value);
1379 }
1380 return value;
1381}
1382
1383static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
1384{
1385 if (cros_ec_is_uhepi_supported(dev))
1386 return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
1387
1388 assert(type < ARRAY_SIZE(event_map));
1389
1390 return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
1391 EC_HOST_EVENT_CLEAR, &mask);
1392}
1393
1394uint64_t cros_ec_get_events_b(struct udevice *dev)
1395{
1396 return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
1397}
1398
1399int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
1400{
1401 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
1402
1403 return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
1404}
1405
1406int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
1407{
1408 struct ec_params_charge_state p;
1409 struct ec_response_charge_state r;
1410 int ret;
1411
1412 p.cmd = CHARGE_STATE_CMD_GET_PARAM;
1413 p.get_param.param = CS_PARAM_LIMIT_POWER;
1414 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
1415 &r, sizeof(r));
1416
1417 /*
1418 * If our EC doesn't support the LIMIT_POWER parameter, assume that
1419 * LIMIT_POWER is not requested.
1420 */
1421 if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
1422 log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1423 return -ENOSYS;
1424 }
1425
1426 if (ret != sizeof(r.get_param))
1427 return -EINVAL;
1428
1429 *limit_powerp = r.get_param.value;
1430 return 0;
1431}
1432
1433int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
1434{
1435 struct ec_params_config_power_button params;
1436 int ret;
1437
1438 params.flags = flags;
1439 ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
1440 &params, sizeof(params), NULL, 0);
1441 if (ret < 0)
1442 return ret;
1443
1444 return 0;
1445}
1446
1447int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
1448{
1449 u32 mask;
1450 int ret;
1451
1452 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1453 &mask);
1454 if (ret < 0)
1455 return ret;
1456
1457 return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
1458}
1459
1460int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
1461{
1462 u32 mask;
1463 int ret;
1464
1465 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1466 &mask);
1467 if (ret < 0)
1468 return ret;
1469
Simon Glass48d268f2018-11-23 21:29:36 -07001470 /* Set lid close event state in the EC SMI event mask */
Simon Glass153f2522018-11-06 15:21:22 -07001471 if (enable)
1472 mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1473 else
1474 mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1475
1476 ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
1477 if (ret < 0)
1478 return ret;
1479
1480 printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
Simon Glass9ad07af2015-08-03 08:19:23 -06001481 return 0;
1482}
1483
Simon Glass44569b52014-10-13 23:42:14 -06001484UCLASS_DRIVER(cros_ec) = {
1485 .id = UCLASS_CROS_EC,
Simon Glassf18ca782019-05-02 10:52:11 -06001486 .name = "cros-ec",
Simon Glass44569b52014-10-13 23:42:14 -06001487 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
Simon Glass18230342016-07-05 17:10:10 -06001488 .post_bind = dm_scan_fdt_dev,
Simon Glass134022a2018-11-06 15:21:21 -07001489 .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,
Simon Glass44569b52014-10-13 23:42:14 -06001490};