blob: ce5fa5bee3546990417ecdbe778b59da6f2c82a3 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060025#include <log.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080026#include <malloc.h>
27#include <spi.h>
Simon Glassdbd79542020-05-10 11:40:11 -060028#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090029#include <linux/errno.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080030#include <asm/io.h>
31#include <asm-generic/gpio.h>
Simon Glass44569b52014-10-13 23:42:14 -060032#include <dm/device-internal.h>
Simon Glassb35e6d62017-05-18 20:09:23 -060033#include <dm/of_extra.h>
Simon Glass44569b52014-10-13 23:42:14 -060034#include <dm/uclass-internal.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080035
36#ifdef DEBUG_TRACE
37#define debug_trace(fmt, b...) debug(fmt, #b)
38#else
39#define debug_trace(fmt, b...)
40#endif
41
42enum {
43 /* Timeout waiting for a flash erase command to complete */
44 CROS_EC_CMD_TIMEOUT_MS = 5000,
45 /* Timeout waiting for a synchronous hash to be recomputed */
46 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
Simon Glassebee5332021-01-16 14:52:23 -070047
48 /* Wait 10 ms between attempts to check if EC's hash is ready */
49 CROS_EC_HASH_CHECK_DELAY_MS = 10,
50
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080051};
52
Simon Glass153f2522018-11-06 15:21:22 -070053#define INVALID_HCMD 0xFF
54
55/*
56 * Map UHEPI masks to non UHEPI commands in order to support old EC FW
57 * which does not support UHEPI command.
58 */
59static const struct {
60 u8 set_cmd;
61 u8 clear_cmd;
62 u8 get_cmd;
63} event_map[] = {
64 [EC_HOST_EVENT_MAIN] = {
65 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
66 INVALID_HCMD,
67 },
68 [EC_HOST_EVENT_B] = {
69 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
70 EC_CMD_HOST_EVENT_GET_B,
71 },
72 [EC_HOST_EVENT_SCI_MASK] = {
73 EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
74 EC_CMD_HOST_EVENT_GET_SCI_MASK,
75 },
76 [EC_HOST_EVENT_SMI_MASK] = {
77 EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
78 EC_CMD_HOST_EVENT_GET_SMI_MASK,
79 },
80 [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
81 INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
82 },
83 [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
84 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
85 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
86 },
87 [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
88 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
89 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
90 },
91 [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = {
92 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
93 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
94 },
95 [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
96 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
97 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
98 },
99};
100
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800101void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
102{
103#ifdef DEBUG
104 int i;
105
106 printf("%s: ", name);
107 if (cmd != -1)
108 printf("cmd=%#x: ", cmd);
109 for (i = 0; i < len; i++)
110 printf("%02x ", data[i]);
111 printf("\n");
112#endif
113}
114
115/*
116 * Calculate a simple 8-bit checksum of a data block
117 *
118 * @param data Data block to checksum
119 * @param size Size of data block in bytes
120 * @return checksum value (0 to 255)
121 */
122int cros_ec_calc_checksum(const uint8_t *data, int size)
123{
124 int csum, i;
125
126 for (i = csum = 0; i < size; i++)
127 csum += data[i];
128 return csum & 0xff;
129}
130
Simon Glass13c7c5b2014-02-27 13:26:09 -0700131/**
132 * Create a request packet for protocol version 3.
133 *
134 * The packet is stored in the device's internal output buffer.
135 *
136 * @param dev CROS-EC device
137 * @param cmd Command to send (EC_CMD_...)
138 * @param cmd_version Version of command to send (EC_VER_...)
139 * @param dout Output data (may be NULL If dout_len=0)
140 * @param dout_len Size of output data in bytes
141 * @return packet size in bytes, or <0 if error.
142 */
Simon Glassbed43522018-10-01 12:22:22 -0600143static int create_proto3_request(struct cros_ec_dev *cdev,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700144 int cmd, int cmd_version,
145 const void *dout, int dout_len)
146{
Simon Glassbed43522018-10-01 12:22:22 -0600147 struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700148 int out_bytes = dout_len + sizeof(*rq);
149
150 /* Fail if output size is too big */
Simon Glassbed43522018-10-01 12:22:22 -0600151 if (out_bytes > (int)sizeof(cdev->dout)) {
Simon Glass13c7c5b2014-02-27 13:26:09 -0700152 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
153 return -EC_RES_REQUEST_TRUNCATED;
154 }
155
156 /* Fill in request packet */
157 rq->struct_version = EC_HOST_REQUEST_VERSION;
158 rq->checksum = 0;
159 rq->command = cmd;
160 rq->command_version = cmd_version;
161 rq->reserved = 0;
162 rq->data_len = dout_len;
163
164 /* Copy data after header */
165 memcpy(rq + 1, dout, dout_len);
166
167 /* Write checksum field so the entire packet sums to 0 */
Simon Glassbed43522018-10-01 12:22:22 -0600168 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
Simon Glass13c7c5b2014-02-27 13:26:09 -0700169
Simon Glassbed43522018-10-01 12:22:22 -0600170 cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700171
172 /* Return size of request packet */
173 return out_bytes;
174}
175
176/**
177 * Prepare the device to receive a protocol version 3 response.
178 *
179 * @param dev CROS-EC device
180 * @param din_len Maximum size of response in bytes
181 * @return maximum expected number of bytes in response, or <0 if error.
182 */
Simon Glassbed43522018-10-01 12:22:22 -0600183static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
Simon Glass13c7c5b2014-02-27 13:26:09 -0700184{
185 int in_bytes = din_len + sizeof(struct ec_host_response);
186
187 /* Fail if input size is too big */
Simon Glassbed43522018-10-01 12:22:22 -0600188 if (in_bytes > (int)sizeof(cdev->din)) {
Simon Glass13c7c5b2014-02-27 13:26:09 -0700189 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
190 return -EC_RES_RESPONSE_TOO_BIG;
191 }
192
193 /* Return expected size of response packet */
194 return in_bytes;
195}
196
197/**
198 * Handle a protocol version 3 response packet.
199 *
200 * The packet must already be stored in the device's internal input buffer.
201 *
202 * @param dev CROS-EC device
203 * @param dinp Returns pointer to response data
204 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700205 * @return number of bytes of response data, or <0 if error. Note that error
206 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
207 * overlap!)
Simon Glass13c7c5b2014-02-27 13:26:09 -0700208 */
209static int handle_proto3_response(struct cros_ec_dev *dev,
210 uint8_t **dinp, int din_len)
211{
212 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
213 int in_bytes;
214 int csum;
215
216 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
217
218 /* Check input data */
219 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
220 debug("%s: EC response version mismatch\n", __func__);
221 return -EC_RES_INVALID_RESPONSE;
222 }
223
224 if (rs->reserved) {
225 debug("%s: EC response reserved != 0\n", __func__);
226 return -EC_RES_INVALID_RESPONSE;
227 }
228
229 if (rs->data_len > din_len) {
230 debug("%s: EC returned too much data\n", __func__);
231 return -EC_RES_RESPONSE_TOO_BIG;
232 }
233
234 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
235
236 /* Update in_bytes to actual data size */
237 in_bytes = sizeof(*rs) + rs->data_len;
238
239 /* Verify checksum */
240 csum = cros_ec_calc_checksum(dev->din, in_bytes);
241 if (csum) {
242 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
243 csum);
244 return -EC_RES_INVALID_CHECKSUM;
245 }
246
247 /* Return error result, if any */
248 if (rs->result)
249 return -(int)rs->result;
250
251 /* If we're still here, set response data pointer and return length */
252 *dinp = (uint8_t *)(rs + 1);
253
254 return rs->data_len;
255}
256
Simon Glassbed43522018-10-01 12:22:22 -0600257static int send_command_proto3(struct cros_ec_dev *cdev,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700258 int cmd, int cmd_version,
259 const void *dout, int dout_len,
260 uint8_t **dinp, int din_len)
261{
Simon Glass44569b52014-10-13 23:42:14 -0600262 struct dm_cros_ec_ops *ops;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700263 int out_bytes, in_bytes;
264 int rv;
265
266 /* Create request packet */
Simon Glassbed43522018-10-01 12:22:22 -0600267 out_bytes = create_proto3_request(cdev, cmd, cmd_version,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700268 dout, dout_len);
269 if (out_bytes < 0)
270 return out_bytes;
271
272 /* Prepare response buffer */
Simon Glassbed43522018-10-01 12:22:22 -0600273 in_bytes = prepare_proto3_response_buffer(cdev, din_len);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700274 if (in_bytes < 0)
275 return in_bytes;
276
Simon Glassbed43522018-10-01 12:22:22 -0600277 ops = dm_cros_ec_get_ops(cdev->dev);
278 rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
279 -ENOSYS;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700280 if (rv < 0)
281 return rv;
282
283 /* Process the response */
Simon Glassbed43522018-10-01 12:22:22 -0600284 return handle_proto3_response(cdev, dinp, din_len);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700285}
286
Simon Glass3a239512018-11-06 15:21:18 -0700287static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800288 const void *dout, int dout_len,
289 uint8_t **dinp, int din_len)
290{
Simon Glass44569b52014-10-13 23:42:14 -0600291 struct dm_cros_ec_ops *ops;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700292 int ret = -1;
293
294 /* Handle protocol version 3 support */
295 if (dev->protocol_version == 3) {
296 return send_command_proto3(dev, cmd, cmd_version,
297 dout, dout_len, dinp, din_len);
298 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800299
Simon Glass44569b52014-10-13 23:42:14 -0600300 ops = dm_cros_ec_get_ops(dev->dev);
301 ret = ops->command(dev->dev, cmd, cmd_version,
302 (const uint8_t *)dout, dout_len, dinp, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800303
304 return ret;
305}
306
307/**
308 * Send a command to the CROS-EC device and return the reply.
309 *
310 * The device's internal input/output buffers are used.
311 *
312 * @param dev CROS-EC device
313 * @param cmd Command to send (EC_CMD_...)
314 * @param cmd_version Version of command to send (EC_VER_...)
315 * @param dout Output data (may be NULL If dout_len=0)
316 * @param dout_len Size of output data in bytes
317 * @param dinp Response data (may be NULL If din_len=0).
318 * If not NULL, it will be updated to point to the data
319 * and will always be double word aligned (64-bits)
320 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700321 * @return number of bytes in response, or -ve on error
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800322 */
Michael Auchter4eaae8e2019-12-09 20:27:31 +0000323static int ec_command_inptr(struct udevice *dev, uint cmd,
Simon Glass699c9ca2018-10-01 12:22:08 -0600324 int cmd_version, const void *dout, int dout_len,
325 uint8_t **dinp, int din_len)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800326{
Simon Glassbed43522018-10-01 12:22:22 -0600327 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Simon Glass19cc2952014-02-27 13:26:11 -0700328 uint8_t *din = NULL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800329 int len;
330
Simon Glassbed43522018-10-01 12:22:22 -0600331 len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
332 din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800333
334 /* If the command doesn't complete, wait a while */
335 if (len == -EC_RES_IN_PROGRESS) {
Simon Glass19cc2952014-02-27 13:26:11 -0700336 struct ec_response_get_comms_status *resp = NULL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800337 ulong start;
338
339 /* Wait for command to complete */
340 start = get_timer(0);
341 do {
342 int ret;
343
344 mdelay(50); /* Insert some reasonable delay */
Simon Glassbed43522018-10-01 12:22:22 -0600345 ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
346 NULL, 0,
347 (uint8_t **)&resp, sizeof(*resp));
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800348 if (ret < 0)
349 return ret;
350
351 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
352 debug("%s: Command %#02x timeout\n",
353 __func__, cmd);
354 return -EC_RES_TIMEOUT;
355 }
356 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
357
358 /* OK it completed, so read the status response */
359 /* not sure why it was 0 for the last argument */
Simon Glassbed43522018-10-01 12:22:22 -0600360 len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
361 &din, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800362 }
363
Simon Glass0b8e8ce2017-05-18 20:09:22 -0600364 debug("%s: len=%d, din=%p\n", __func__, len, din);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800365 if (dinp) {
366 /* If we have any data to return, it must be 64bit-aligned */
367 assert(len <= 0 || !((uintptr_t)din & 7));
368 *dinp = din;
369 }
370
371 return len;
372}
373
374/**
375 * Send a command to the CROS-EC device and return the reply.
376 *
377 * The device's internal input/output buffers are used.
378 *
379 * @param dev CROS-EC device
380 * @param cmd Command to send (EC_CMD_...)
381 * @param cmd_version Version of command to send (EC_VER_...)
382 * @param dout Output data (may be NULL If dout_len=0)
383 * @param dout_len Size of output data in bytes
384 * @param din Response data (may be NULL If din_len=0).
385 * It not NULL, it is a place for ec_command() to copy the
386 * data to.
387 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700388 * @return number of bytes in response, or -ve on error
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800389 */
Simon Glass3a239512018-11-06 15:21:18 -0700390static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800391 const void *dout, int dout_len,
392 void *din, int din_len)
393{
394 uint8_t *in_buffer;
395 int len;
396
397 assert((din_len == 0) || din);
398 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
Simon Glassbed43522018-10-01 12:22:22 -0600399 &in_buffer, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800400 if (len > 0) {
401 /*
402 * If we were asked to put it somewhere, do so, otherwise just
403 * disregard the result.
404 */
405 if (din && in_buffer) {
406 assert(len <= din_len);
407 memmove(din, in_buffer, len);
408 }
409 }
410 return len;
411}
412
Simon Glassfb726402015-10-18 21:17:14 -0600413int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800414{
Simon Glassbed43522018-10-01 12:22:22 -0600415 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
Simon Glass19cc2952014-02-27 13:26:11 -0700416 sizeof(scan->data)) != sizeof(scan->data))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800417 return -1;
418
419 return 0;
420}
421
Alper Nebi Yasak1ce07292020-10-30 20:25:20 +0300422int cros_ec_get_next_event(struct udevice *dev,
423 struct ec_response_get_next_event *event)
424{
425 int ret;
426
427 ret = ec_command(dev, EC_CMD_GET_NEXT_EVENT, 0, NULL, 0,
428 event, sizeof(*event));
429 if (ret < 0)
430 return ret;
431 else if (ret != sizeof(*event))
432 return -EC_RES_INVALID_RESPONSE;
433
434 return 0;
435}
436
Simon Glassbed43522018-10-01 12:22:22 -0600437int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800438{
439 struct ec_response_get_version *r;
Simon Glassa77024d2018-11-06 15:21:19 -0700440 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800441
Simon Glassa77024d2018-11-06 15:21:19 -0700442 ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
443 (uint8_t **)&r, sizeof(*r));
444 if (ret != sizeof(*r)) {
Simon Glass48d268f2018-11-23 21:29:36 -0700445 log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800446 return -1;
Simon Glassa77024d2018-11-06 15:21:19 -0700447 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800448
Simon Glass19cc2952014-02-27 13:26:11 -0700449 if (maxlen > (int)sizeof(r->version_string_ro))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800450 maxlen = sizeof(r->version_string_ro);
451
452 switch (r->current_image) {
453 case EC_IMAGE_RO:
454 memcpy(id, r->version_string_ro, maxlen);
455 break;
456 case EC_IMAGE_RW:
457 memcpy(id, r->version_string_rw, maxlen);
458 break;
459 default:
Simon Glassa77024d2018-11-06 15:21:19 -0700460 log_err("Invalid EC image %d\n", r->current_image);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800461 return -1;
462 }
463
464 id[maxlen - 1] = '\0';
465 return 0;
466}
467
Simon Glassbed43522018-10-01 12:22:22 -0600468int cros_ec_read_version(struct udevice *dev,
469 struct ec_response_get_version **versionp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800470{
471 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
472 (uint8_t **)versionp, sizeof(**versionp))
Simon Glass19cc2952014-02-27 13:26:11 -0700473 != sizeof(**versionp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800474 return -1;
475
476 return 0;
477}
478
Simon Glassbed43522018-10-01 12:22:22 -0600479int cros_ec_read_build_info(struct udevice *dev, char **strp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800480{
481 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
Simon Glassece28862014-02-27 13:26:07 -0700482 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800483 return -1;
484
485 return 0;
486}
487
Simon Glassbed43522018-10-01 12:22:22 -0600488int cros_ec_read_current_image(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600489 enum ec_current_image *image)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800490{
491 struct ec_response_get_version *r;
492
493 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700494 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800495 return -1;
496
497 *image = r->current_image;
498 return 0;
499}
500
Simon Glassbed43522018-10-01 12:22:22 -0600501static int cros_ec_wait_on_hash_done(struct udevice *dev,
Simon Glass97208d92020-11-09 07:14:43 -0700502 struct ec_params_vboot_hash *p,
Simon Glass699c9ca2018-10-01 12:22:08 -0600503 struct ec_response_vboot_hash *hash)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800504{
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800505 ulong start;
506
507 start = get_timer(0);
508 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
Simon Glassebee5332021-01-16 14:52:23 -0700509 mdelay(CROS_EC_HASH_CHECK_DELAY_MS);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800510
Simon Glass97208d92020-11-09 07:14:43 -0700511 p->cmd = EC_VBOOT_HASH_GET;
Simon Glassebee5332021-01-16 14:52:23 -0700512
Simon Glass97208d92020-11-09 07:14:43 -0700513 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, p, sizeof(*p), hash,
514 sizeof(*hash)) < 0)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800515 return -1;
516
517 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
518 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
519 return -EC_RES_TIMEOUT;
520 }
521 }
522 return 0;
523}
524
Simon Glass94564902018-10-01 12:22:38 -0600525int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
526 struct ec_response_vboot_hash *hash)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800527{
528 struct ec_params_vboot_hash p;
529 int rv;
530
531 p.cmd = EC_VBOOT_HASH_GET;
Simon Glass94564902018-10-01 12:22:38 -0600532 p.offset = hash_offset;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800533 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
534 hash, sizeof(*hash)) < 0)
535 return -1;
536
537 /* If the EC is busy calculating the hash, fidget until it's done. */
Simon Glass97208d92020-11-09 07:14:43 -0700538 rv = cros_ec_wait_on_hash_done(dev, &p, hash);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800539 if (rv)
540 return rv;
541
542 /* If the hash is valid, we're done. Otherwise, we have to kick it off
543 * again and wait for it to complete. Note that we explicitly assume
544 * that hashing zero bytes is always wrong, even though that would
545 * produce a valid hash value. */
546 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
547 return 0;
548
549 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
550 __func__, hash->status, hash->size);
551
Simon Glassece28862014-02-27 13:26:07 -0700552 p.cmd = EC_VBOOT_HASH_START;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800553 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
554 p.nonce_size = 0;
Simon Glass94564902018-10-01 12:22:38 -0600555 p.offset = hash_offset;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800556
557 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
558 hash, sizeof(*hash)) < 0)
559 return -1;
560
Simon Glass97208d92020-11-09 07:14:43 -0700561 rv = cros_ec_wait_on_hash_done(dev, &p, hash);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800562 if (rv)
563 return rv;
Simon Glass97208d92020-11-09 07:14:43 -0700564 if (hash->status != EC_VBOOT_HASH_STATUS_DONE) {
565 log_err("Hash did not complete, status=%d\n", hash->status);
566 return -EIO;
567 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800568
569 debug("%s: hash done\n", __func__);
570
571 return 0;
572}
573
Simon Glassbed43522018-10-01 12:22:22 -0600574static int cros_ec_invalidate_hash(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800575{
576 struct ec_params_vboot_hash p;
577 struct ec_response_vboot_hash *hash;
578
579 /* We don't have an explict command for the EC to discard its current
580 * hash value, so we'll just tell it to calculate one that we know is
581 * wrong (we claim that hashing zero bytes is always invalid).
582 */
583 p.cmd = EC_VBOOT_HASH_RECALC;
584 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
585 p.nonce_size = 0;
586 p.offset = 0;
587 p.size = 0;
588
589 debug("%s:\n", __func__);
590
591 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
592 (uint8_t **)&hash, sizeof(*hash)) < 0)
593 return -1;
594
595 /* No need to wait for it to finish */
596 return 0;
597}
598
Simon Glass9b306e52021-01-16 14:52:22 -0700599int cros_ec_hello(struct udevice *dev, uint *handshakep)
600{
601 struct ec_params_hello req;
602 struct ec_response_hello *resp;
603
604 req.in_data = 0x12345678;
605 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
606 (uint8_t **)&resp, sizeof(*resp)) < 0)
607 return -EIO;
608 if (resp->out_data != req.in_data + 0x01020304) {
609 printf("Received invalid handshake %x\n", resp->out_data);
610 if (handshakep)
611 *handshakep = req.in_data;
612 return -ENOTSYNC;
613 }
614
615 return 0;
616}
617
Simon Glassbed43522018-10-01 12:22:22 -0600618int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800619{
620 struct ec_params_reboot_ec p;
621
622 p.cmd = cmd;
623 p.flags = flags;
624
625 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
626 < 0)
627 return -1;
628
629 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
Simon Glassebee5332021-01-16 14:52:23 -0700630 ulong start;
631
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800632 /*
633 * EC reboot will take place immediately so delay to allow it
634 * to complete. Note that some reboot types (EC_REBOOT_COLD)
635 * will reboot the AP as well, in which case we won't actually
636 * get to this point.
637 */
Simon Glassebee5332021-01-16 14:52:23 -0700638 mdelay(50);
639 start = get_timer(0);
640 while (cros_ec_hello(dev, NULL)) {
641 if (get_timer(start) > 3000) {
642 log_err("EC did not return from reboot\n");
643 return -ETIMEDOUT;
644 }
645 mdelay(5);
646 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800647 }
648
649 return 0;
650}
651
Simon Glassfb726402015-10-18 21:17:14 -0600652int cros_ec_interrupt_pending(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800653{
Simon Glassfb726402015-10-18 21:17:14 -0600654 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
655
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800656 /* no interrupt support : always poll */
Simon Glassfb726402015-10-18 21:17:14 -0600657 if (!dm_gpio_is_valid(&cdev->ec_int))
Simon Glass19cc2952014-02-27 13:26:11 -0700658 return -ENOENT;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800659
Simon Glassfb726402015-10-18 21:17:14 -0600660 return dm_gpio_get_value(&cdev->ec_int);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800661}
662
Simon Glassbed43522018-10-01 12:22:22 -0600663int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800664{
Simon Glassece28862014-02-27 13:26:07 -0700665 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
Simon Glass19cc2952014-02-27 13:26:11 -0700666 sizeof(*info)) != sizeof(*info))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800667 return -1;
668
669 return 0;
670}
671
Simon Glass153f2522018-11-06 15:21:22 -0700672int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
673{
674 struct ec_response_host_event_mask rsp;
675 int ret;
676
677 ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
678 if (ret < 0)
679 return ret;
680 else if (ret != sizeof(rsp))
681 return -EINVAL;
682
683 *mask = rsp.mask;
684
685 return 0;
686}
687
688int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
689{
690 struct ec_params_host_event_mask req;
691 int ret;
692
693 req.mask = mask;
694
695 ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
696 if (ret < 0)
697 return ret;
698
699 return 0;
700}
701
Simon Glassbed43522018-10-01 12:22:22 -0600702int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800703{
704 struct ec_response_host_event_mask *resp;
705
706 /*
707 * Use the B copy of the event flags, because the main copy is already
708 * used by ACPI/SMI.
709 */
710 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700711 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800712 return -1;
713
714 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
715 return -1;
716
717 *events_ptr = resp->mask;
718 return 0;
719}
720
Simon Glassbed43522018-10-01 12:22:22 -0600721int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800722{
723 struct ec_params_host_event_mask params;
724
725 params.mask = events;
726
727 /*
728 * Use the B copy of the event flags, so it affects the data returned
729 * by cros_ec_get_host_events().
730 */
731 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
732 &params, sizeof(params), NULL, 0) < 0)
733 return -1;
734
735 return 0;
736}
737
Simon Glassbed43522018-10-01 12:22:22 -0600738int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
739 uint32_t set_flags,
Simon Glass699c9ca2018-10-01 12:22:08 -0600740 struct ec_response_flash_protect *resp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800741{
742 struct ec_params_flash_protect params;
743
744 params.mask = set_mask;
745 params.flags = set_flags;
746
747 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
748 &params, sizeof(params),
Simon Glass19cc2952014-02-27 13:26:11 -0700749 resp, sizeof(*resp)) != sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800750 return -1;
751
752 return 0;
753}
754
Simon Glass153f2522018-11-06 15:21:22 -0700755int cros_ec_entering_mode(struct udevice *dev, int mode)
756{
757 int rc;
758
759 rc = ec_command(dev, EC_CMD_ENTERING_MODE, 0, &mode, sizeof(mode),
760 NULL, 0);
761 if (rc)
762 return -1;
763 return 0;
764}
765
Simon Glassbed43522018-10-01 12:22:22 -0600766static int cros_ec_check_version(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800767{
Simon Glassbed43522018-10-01 12:22:22 -0600768 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800769 struct ec_params_hello req;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800770
Simon Glass23fb1562015-03-26 09:29:30 -0600771 struct dm_cros_ec_ops *ops;
772 int ret;
773
Simon Glassbed43522018-10-01 12:22:22 -0600774 ops = dm_cros_ec_get_ops(dev);
Simon Glass23fb1562015-03-26 09:29:30 -0600775 if (ops->check_version) {
Simon Glassbed43522018-10-01 12:22:22 -0600776 ret = ops->check_version(dev);
Simon Glass23fb1562015-03-26 09:29:30 -0600777 if (ret)
778 return ret;
779 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800780
781 /*
782 * TODO(sjg@chromium.org).
783 * There is a strange oddity here with the EC. We could just ignore
784 * the response, i.e. pass the last two parameters as NULL and 0.
785 * In this case we won't read back very many bytes from the EC.
786 * On the I2C bus the EC gets upset about this and will try to send
787 * the bytes anyway. This means that we will have to wait for that
788 * to complete before continuing with a new EC command.
789 *
790 * This problem is probably unique to the I2C bus.
791 *
792 * So for now, just read all the data anyway.
793 */
Randall Spanglerf03e9702014-02-27 13:26:08 -0700794
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700795 /* Try sending a version 3 packet */
Simon Glassbed43522018-10-01 12:22:22 -0600796 cdev->protocol_version = 3;
Simon Glass73c1ecf2014-11-11 18:06:30 -0700797 req.in_data = 0;
Simon Glass9b306e52021-01-16 14:52:22 -0700798 ret = cros_ec_hello(dev, NULL);
799 if (!ret || ret == -ENOTSYNC)
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700800 return 0;
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700801
Randall Spanglerf03e9702014-02-27 13:26:08 -0700802 /* Try sending a version 2 packet */
Simon Glassbed43522018-10-01 12:22:22 -0600803 cdev->protocol_version = 2;
Simon Glass9b306e52021-01-16 14:52:22 -0700804 ret = cros_ec_hello(dev, NULL);
805 if (!ret || ret == -ENOTSYNC)
Randall Spanglerf03e9702014-02-27 13:26:08 -0700806 return 0;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800807
Randall Spanglerf03e9702014-02-27 13:26:08 -0700808 /*
809 * Fail if we're still here, since the EC doesn't understand any
810 * protcol version we speak. Version 1 interface without command
811 * version is no longer supported, and we don't know about any new
812 * protocol versions.
813 */
Simon Glassbed43522018-10-01 12:22:22 -0600814 cdev->protocol_version = 0;
Randall Spanglerf03e9702014-02-27 13:26:08 -0700815 printf("%s: ERROR: old EC interface not supported\n", __func__);
816 return -1;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800817}
818
Simon Glassbed43522018-10-01 12:22:22 -0600819int cros_ec_test(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800820{
Simon Glass9b306e52021-01-16 14:52:22 -0700821 uint out_data;
822 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800823
Simon Glass9b306e52021-01-16 14:52:22 -0700824 ret = cros_ec_hello(dev, &out_data);
825 if (ret == -ENOTSYNC) {
826 printf("Received invalid handshake %x\n", out_data);
827 return ret;
828 } else if (ret) {
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800829 printf("ec_command_inptr() returned error\n");
Simon Glass9b306e52021-01-16 14:52:22 -0700830 return ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800831 }
832
833 return 0;
834}
835
Simon Glassbed43522018-10-01 12:22:22 -0600836int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800837 uint32_t *offset, uint32_t *size)
838{
839 struct ec_params_flash_region_info p;
840 struct ec_response_flash_region_info *r;
841 int ret;
842
843 p.region = region;
844 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
845 EC_VER_FLASH_REGION_INFO,
846 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
847 if (ret != sizeof(*r))
848 return -1;
849
850 if (offset)
851 *offset = r->offset;
852 if (size)
853 *size = r->size;
854
855 return 0;
856}
857
Simon Glassbed43522018-10-01 12:22:22 -0600858int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800859{
860 struct ec_params_flash_erase p;
861
862 p.offset = offset;
863 p.size = size;
864 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
865 NULL, 0);
866}
867
868/**
869 * Write a single block to the flash
870 *
871 * Write a block of data to the EC flash. The size must not exceed the flash
872 * write block size which you can obtain from cros_ec_flash_write_burst_size().
873 *
874 * The offset starts at 0. You can obtain the region information from
875 * cros_ec_flash_offset() to find out where to write for a particular region.
876 *
877 * Attempting to write to the region where the EC is currently running from
878 * will result in an error.
879 *
880 * @param dev CROS-EC device
881 * @param data Pointer to data buffer to write
882 * @param offset Offset within flash to write to.
883 * @param size Number of bytes to write
884 * @return 0 if ok, -1 on error
885 */
Simon Glassbed43522018-10-01 12:22:22 -0600886static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
887 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800888{
Moritz Fischere597bc72016-09-12 12:57:52 -0700889 struct ec_params_flash_write *p;
890 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800891
Moritz Fischere597bc72016-09-12 12:57:52 -0700892 p = malloc(sizeof(*p) + size);
893 if (!p)
894 return -ENOMEM;
895
896 p->offset = offset;
897 p->size = size;
898 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
899 memcpy(p + 1, data, p->size);
900
901 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
902 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
903
904 free(p);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800905
Moritz Fischere597bc72016-09-12 12:57:52 -0700906 return ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800907}
908
909/**
910 * Return optimal flash write burst size
911 */
Simon Glassbed43522018-10-01 12:22:22 -0600912static int cros_ec_flash_write_burst_size(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800913{
Simon Glassece28862014-02-27 13:26:07 -0700914 return EC_FLASH_WRITE_VER0_SIZE;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800915}
916
917/**
918 * Check if a block of data is erased (all 0xff)
919 *
920 * This function is useful when dealing with flash, for checking whether a
921 * data block is erased and thus does not need to be programmed.
922 *
923 * @param data Pointer to data to check (must be word-aligned)
924 * @param size Number of bytes to check (must be word-aligned)
925 * @return 0 if erased, non-zero if any word is not erased
926 */
927static int cros_ec_data_is_erased(const uint32_t *data, int size)
928{
929 assert(!(size & 3));
930 size /= sizeof(uint32_t);
931 for (; size > 0; size -= 4, data++)
932 if (*data != -1U)
933 return 0;
934
935 return 1;
936}
937
Moritz Fischerde5bc892016-09-13 14:44:48 -0700938/**
939 * Read back flash parameters
940 *
941 * This function reads back parameters of the flash as reported by the EC
942 *
943 * @param dev Pointer to device
944 * @param info Pointer to output flash info struct
945 */
Simon Glassbed43522018-10-01 12:22:22 -0600946int cros_ec_read_flashinfo(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600947 struct ec_response_flash_info *info)
Moritz Fischerde5bc892016-09-13 14:44:48 -0700948{
949 int ret;
950
951 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
952 NULL, 0, info, sizeof(*info));
953 if (ret < 0)
954 return ret;
955
956 return ret < sizeof(*info) ? -1 : 0;
957}
958
Simon Glassbed43522018-10-01 12:22:22 -0600959int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
Simon Glass699c9ca2018-10-01 12:22:08 -0600960 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800961{
Simon Glassbed43522018-10-01 12:22:22 -0600962 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800963 uint32_t burst = cros_ec_flash_write_burst_size(dev);
964 uint32_t end, off;
965 int ret;
966
Simon Glass411661d2018-11-06 15:21:20 -0700967 if (!burst)
968 return -EINVAL;
969
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800970 /*
971 * TODO: round up to the nearest multiple of write size. Can get away
972 * without that on link right now because its write size is 4 bytes.
973 */
974 end = offset + size;
975 for (off = offset; off < end; off += burst, data += burst) {
976 uint32_t todo;
977
978 /* If the data is empty, there is no point in programming it */
979 todo = min(end - off, burst);
Simon Glassbed43522018-10-01 12:22:22 -0600980 if (cdev->optimise_flash_write &&
Simon Glass699c9ca2018-10-01 12:22:08 -0600981 cros_ec_data_is_erased((uint32_t *)data, todo))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800982 continue;
983
984 ret = cros_ec_flash_write_block(dev, data, off, todo);
985 if (ret)
986 return ret;
987 }
988
989 return 0;
990}
991
992/**
Simon Glass153f2522018-11-06 15:21:22 -0700993 * Run verification on a slot
994 *
995 * @param me CrosEc instance
996 * @param region Region to run verification on
997 * @return 0 if success or not applicable. Non-zero if verification failed.
998 */
999int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
1000{
1001 struct ec_params_efs_verify p;
1002 int rv;
1003
1004 log_info("EFS: EC is verifying updated image...\n");
1005 p.region = region;
1006
1007 rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
1008 if (rv >= 0) {
1009 log_info("EFS: Verification success\n");
1010 return 0;
1011 }
1012 if (rv == -EC_RES_INVALID_COMMAND) {
1013 log_info("EFS: EC doesn't support EFS_VERIFY command\n");
1014 return 0;
1015 }
1016 log_info("EFS: Verification failed\n");
1017
1018 return rv;
1019}
1020
1021/**
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001022 * Read a single block from the flash
1023 *
1024 * Read a block of data from the EC flash. The size must not exceed the flash
1025 * write block size which you can obtain from cros_ec_flash_write_burst_size().
1026 *
1027 * The offset starts at 0. You can obtain the region information from
1028 * cros_ec_flash_offset() to find out where to read for a particular region.
1029 *
1030 * @param dev CROS-EC device
1031 * @param data Pointer to data buffer to read into
1032 * @param offset Offset within flash to read from
1033 * @param size Number of bytes to read
1034 * @return 0 if ok, -1 on error
1035 */
Simon Glassbed43522018-10-01 12:22:22 -06001036static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
Simon Glass699c9ca2018-10-01 12:22:08 -06001037 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001038{
1039 struct ec_params_flash_read p;
1040
1041 p.offset = offset;
1042 p.size = size;
1043
1044 return ec_command(dev, EC_CMD_FLASH_READ, 0,
1045 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
1046}
1047
Simon Glassbed43522018-10-01 12:22:22 -06001048int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
Simon Glass699c9ca2018-10-01 12:22:08 -06001049 uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001050{
1051 uint32_t burst = cros_ec_flash_write_burst_size(dev);
1052 uint32_t end, off;
1053 int ret;
1054
1055 end = offset + size;
1056 for (off = offset; off < end; off += burst, data += burst) {
1057 ret = cros_ec_flash_read_block(dev, data, off,
1058 min(end - off, burst));
1059 if (ret)
1060 return ret;
1061 }
1062
1063 return 0;
1064}
1065
Simon Glassbed43522018-10-01 12:22:22 -06001066int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
Simon Glass699c9ca2018-10-01 12:22:08 -06001067 int image_size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001068{
1069 uint32_t rw_offset, rw_size;
1070 int ret;
1071
Simon Glass2c1e5502018-10-01 12:22:36 -06001072 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
1073 &rw_size))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001074 return -1;
Simon Glass19cc2952014-02-27 13:26:11 -07001075 if (image_size > (int)rw_size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001076 return -1;
1077
1078 /* Invalidate the existing hash, just in case the AP reboots
1079 * unexpectedly during the update. If that happened, the EC RW firmware
1080 * would be invalid, but the EC would still have the original hash.
1081 */
1082 ret = cros_ec_invalidate_hash(dev);
1083 if (ret)
1084 return ret;
1085
1086 /*
1087 * Erase the entire RW section, so that the EC doesn't see any garbage
1088 * past the new image if it's smaller than the current image.
1089 *
1090 * TODO: could optimize this to erase just the current image, since
1091 * presumably everything past that is 0xff's. But would still need to
1092 * round up to the nearest multiple of erase size.
1093 */
1094 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
1095 if (ret)
1096 return ret;
1097
1098 /* Write the image */
1099 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
1100 if (ret)
1101 return ret;
1102
1103 return 0;
1104}
1105
Simon Glassbed43522018-10-01 12:22:22 -06001106int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001107{
1108 struct ec_params_vbnvcontext p;
1109 int len;
1110
Simon Glass153f2522018-11-06 15:21:22 -07001111 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
Simon Glassbed43522018-10-01 12:22:22 -06001112 return -EINVAL;
1113
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001114 p.op = EC_VBNV_CONTEXT_OP_READ;
1115
1116 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
Simon Glass153f2522018-11-06 15:21:22 -07001117 &p, sizeof(uint32_t) + size, block, size);
1118 if (len != size) {
1119 log_err("Expected %d bytes, got %d\n", size, len);
Simon Glassbed43522018-10-01 12:22:22 -06001120 return -EIO;
Simon Glass153f2522018-11-06 15:21:22 -07001121 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001122
1123 return 0;
1124}
1125
Simon Glassbed43522018-10-01 12:22:22 -06001126int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001127{
1128 struct ec_params_vbnvcontext p;
1129 int len;
1130
Simon Glass153f2522018-11-06 15:21:22 -07001131 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
Simon Glassbed43522018-10-01 12:22:22 -06001132 return -EINVAL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001133 p.op = EC_VBNV_CONTEXT_OP_WRITE;
Simon Glass153f2522018-11-06 15:21:22 -07001134 memcpy(p.block, block, size);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001135
1136 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
Simon Glass153f2522018-11-06 15:21:22 -07001137 &p, sizeof(uint32_t) + size, NULL, 0);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001138 if (len < 0)
1139 return -1;
1140
Simon Glass153f2522018-11-06 15:21:22 -07001141 return 0;
1142}
1143
1144int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
1145{
1146 struct ec_params_battery_cutoff p;
1147 int len;
1148
1149 p.flags = flags;
1150 len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
1151 NULL, 0);
1152
1153 if (len < 0)
1154 return -1;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001155 return 0;
1156}
1157
Simon Glasseb2cc512015-08-03 08:19:24 -06001158int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001159{
1160 struct ec_params_ldo_set params;
1161
1162 params.index = index;
1163 params.state = state;
1164
Simon Glassbed43522018-10-01 12:22:22 -06001165 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
Simon Glasseb2cc512015-08-03 08:19:24 -06001166 NULL, 0))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001167 return -1;
1168
1169 return 0;
1170}
1171
Simon Glasseb2cc512015-08-03 08:19:24 -06001172int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001173{
1174 struct ec_params_ldo_get params;
1175 struct ec_response_ldo_get *resp;
1176
1177 params.index = index;
1178
Simon Glassbed43522018-10-01 12:22:22 -06001179 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
Simon Glasseb2cc512015-08-03 08:19:24 -06001180 (uint8_t **)&resp, sizeof(*resp)) !=
1181 sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001182 return -1;
1183
1184 *state = resp->state;
1185
1186 return 0;
1187}
1188
Simon Glass44569b52014-10-13 23:42:14 -06001189int cros_ec_register(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001190{
Simon Glassde0977b2015-03-05 12:25:20 -07001191 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001192 char id[MSG_BYTES];
Simon Glass44569b52014-10-13 23:42:14 -06001193
1194 cdev->dev = dev;
Simon Glass0d7cc472015-01-05 20:05:32 -07001195 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1196 GPIOD_IS_IN);
Simon Glassb35e6d62017-05-18 20:09:23 -06001197 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
Simon Glass44569b52014-10-13 23:42:14 -06001198
Simon Glassbed43522018-10-01 12:22:22 -06001199 if (cros_ec_check_version(dev)) {
Simon Glass44569b52014-10-13 23:42:14 -06001200 debug("%s: Could not detect CROS-EC version\n", __func__);
1201 return -CROS_EC_ERR_CHECK_VERSION;
1202 }
1203
Simon Glassbed43522018-10-01 12:22:22 -06001204 if (cros_ec_read_id(dev, id, sizeof(id))) {
Simon Glass44569b52014-10-13 23:42:14 -06001205 debug("%s: Could not read KBC ID\n", __func__);
1206 return -CROS_EC_ERR_READ_ID;
1207 }
1208
1209 /* Remember this device for use by the cros_ec command */
Simon Glass46e17332015-02-17 15:29:36 -07001210 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1211 cdev->protocol_version, id);
Simon Glass44569b52014-10-13 23:42:14 -06001212
1213 return 0;
1214}
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001215
Simon Glassb35e6d62017-05-18 20:09:23 -06001216int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
Simon Glass3c59abc2014-02-27 13:26:03 -07001217{
Simon Glassb35e6d62017-05-18 20:09:23 -06001218 ofnode flash_node, node;
Simon Glass3c59abc2014-02-27 13:26:03 -07001219
Simon Glassb35e6d62017-05-18 20:09:23 -06001220 flash_node = dev_read_subnode(dev, "flash");
1221 if (!ofnode_valid(flash_node)) {
Simon Glass3c59abc2014-02-27 13:26:03 -07001222 debug("Failed to find flash node\n");
1223 return -1;
1224 }
1225
Simon Glass4b580932018-06-11 13:07:17 -06001226 if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
Simon Glassb35e6d62017-05-18 20:09:23 -06001227 debug("Failed to decode flash node in chrome-ec\n");
Simon Glass3c59abc2014-02-27 13:26:03 -07001228 return -1;
1229 }
1230
Simon Glassb35e6d62017-05-18 20:09:23 -06001231 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1232 "erase-value", -1);
Simon Glass28529762017-08-05 15:45:54 -06001233 ofnode_for_each_subnode(node, flash_node) {
Simon Glassb35e6d62017-05-18 20:09:23 -06001234 const char *name = ofnode_get_name(node);
Simon Glass3c59abc2014-02-27 13:26:03 -07001235 enum ec_flash_region region;
1236
1237 if (0 == strcmp(name, "ro")) {
1238 region = EC_FLASH_REGION_RO;
1239 } else if (0 == strcmp(name, "rw")) {
Simon Glass2c1e5502018-10-01 12:22:36 -06001240 region = EC_FLASH_REGION_ACTIVE;
Simon Glass3c59abc2014-02-27 13:26:03 -07001241 } else if (0 == strcmp(name, "wp-ro")) {
1242 region = EC_FLASH_REGION_WP_RO;
1243 } else {
1244 debug("Unknown EC flash region name '%s'\n", name);
1245 return -1;
1246 }
1247
Simon Glass4b580932018-06-11 13:07:17 -06001248 if (ofnode_read_fmap_entry(node, &config->region[region])) {
Simon Glass3c59abc2014-02-27 13:26:03 -07001249 debug("Failed to decode flash region in chrome-ec'\n");
1250 return -1;
1251 }
1252 }
1253
1254 return 0;
1255}
1256
Moritz Fischer6cddc602016-09-27 15:42:07 -07001257int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1258 int nmsgs)
Simon Glass9ad07af2015-08-03 08:19:23 -06001259{
Simon Glass9ad07af2015-08-03 08:19:23 -06001260 union {
1261 struct ec_params_i2c_passthru p;
1262 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1263 } params;
1264 union {
1265 struct ec_response_i2c_passthru r;
1266 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1267 } response;
1268 struct ec_params_i2c_passthru *p = &params.p;
1269 struct ec_response_i2c_passthru *r = &response.r;
1270 struct ec_params_i2c_passthru_msg *msg;
1271 uint8_t *pdata, *read_ptr = NULL;
1272 int read_len;
1273 int size;
1274 int rv;
1275 int i;
1276
Moritz Fischer6cddc602016-09-27 15:42:07 -07001277 p->port = port;
Simon Glass9ad07af2015-08-03 08:19:23 -06001278
1279 p->num_msgs = nmsgs;
1280 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1281
1282 /* Create a message to write the register address and optional data */
1283 pdata = (uint8_t *)p + size;
1284
1285 read_len = 0;
1286 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1287 bool is_read = in->flags & I2C_M_RD;
1288
1289 msg->addr_flags = in->addr;
1290 msg->len = in->len;
1291 if (is_read) {
1292 msg->addr_flags |= EC_I2C_FLAG_READ;
1293 read_len += in->len;
1294 read_ptr = in->buf;
1295 if (sizeof(*r) + read_len > sizeof(response)) {
1296 puts("Read length too big for buffer\n");
1297 return -1;
1298 }
1299 } else {
1300 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1301 puts("Params too large for buffer\n");
1302 return -1;
1303 }
1304 memcpy(pdata, in->buf, in->len);
1305 pdata += in->len;
1306 }
1307 }
1308
Simon Glassbed43522018-10-01 12:22:22 -06001309 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
Simon Glass9ad07af2015-08-03 08:19:23 -06001310 r, sizeof(*r) + read_len);
1311 if (rv < 0)
1312 return rv;
1313
1314 /* Parse response */
1315 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1316 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1317 return -1;
1318 }
1319
1320 if (rv < sizeof(*r) + read_len) {
1321 puts("Truncated read response\n");
1322 return -1;
1323 }
1324
1325 /* We only support a single read message for each transfer */
1326 if (read_len)
1327 memcpy(read_ptr, r->data, read_len);
1328
Simon Glass153f2522018-11-06 15:21:22 -07001329 return 0;
1330}
1331
1332int cros_ec_check_feature(struct udevice *dev, int feature)
1333{
1334 struct ec_response_get_features r;
1335 int rv;
1336
1337 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0);
1338 if (rv)
1339 return rv;
1340
1341 if (feature >= 8 * sizeof(r.flags))
1342 return -1;
1343
1344 return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature);
1345}
1346
1347/*
1348 * Query the EC for specified mask indicating enabled events.
1349 * The EC maintains separate event masks for SMI, SCI and WAKE.
1350 */
1351static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
1352 uint64_t *value)
1353{
1354 int ret;
1355 struct ec_params_host_event req;
1356 struct ec_response_host_event rsp;
1357
1358 req.action = action;
1359 req.mask_type = mask;
1360 if (action != EC_HOST_EVENT_GET)
1361 req.value = *value;
1362 else
1363 *value = 0;
1364 ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
1365 sizeof(rsp));
1366
1367 if (action != EC_HOST_EVENT_GET)
1368 return ret;
1369 if (ret == 0)
1370 *value = rsp.value;
1371
1372 return ret;
1373}
1374
1375static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
1376 uint action, uint64_t *value)
1377{
1378 int ret = -1;
1379 struct ec_params_host_event_mask req;
1380 struct ec_response_host_event_mask rsp;
1381
1382 if (hcmd == INVALID_HCMD)
1383 return ret;
1384
1385 if (action != EC_HOST_EVENT_GET)
1386 req.mask = (uint32_t)*value;
1387 else
1388 *value = 0;
1389
1390 ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
1391 if (action != EC_HOST_EVENT_GET)
1392 return ret;
1393 if (ret == 0)
1394 *value = rsp.mask;
1395
1396 return ret;
1397}
1398
1399bool cros_ec_is_uhepi_supported(struct udevice *dev)
1400{
1401#define UHEPI_SUPPORTED 1
1402#define UHEPI_NOT_SUPPORTED 2
1403 static int uhepi_support;
1404
1405 if (!uhepi_support) {
1406 uhepi_support = cros_ec_check_feature(dev,
1407 EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
1408 UHEPI_NOT_SUPPORTED;
1409 log_debug("Chrome EC: UHEPI %s\n",
1410 uhepi_support == UHEPI_SUPPORTED ? "supported" :
1411 "not supported");
1412 }
1413 return uhepi_support == UHEPI_SUPPORTED;
1414}
1415
1416static int cros_ec_get_mask(struct udevice *dev, uint type)
1417{
1418 u64 value = 0;
1419
1420 if (cros_ec_is_uhepi_supported(dev)) {
1421 cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
1422 } else {
1423 assert(type < ARRAY_SIZE(event_map));
1424 cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
1425 EC_HOST_EVENT_GET, &value);
1426 }
1427 return value;
1428}
1429
1430static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
1431{
1432 if (cros_ec_is_uhepi_supported(dev))
1433 return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
1434
1435 assert(type < ARRAY_SIZE(event_map));
1436
1437 return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
1438 EC_HOST_EVENT_CLEAR, &mask);
1439}
1440
1441uint64_t cros_ec_get_events_b(struct udevice *dev)
1442{
1443 return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
1444}
1445
1446int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
1447{
1448 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
1449
1450 return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
1451}
1452
1453int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
1454{
1455 struct ec_params_charge_state p;
1456 struct ec_response_charge_state r;
1457 int ret;
1458
1459 p.cmd = CHARGE_STATE_CMD_GET_PARAM;
1460 p.get_param.param = CS_PARAM_LIMIT_POWER;
1461 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
1462 &r, sizeof(r));
1463
1464 /*
1465 * If our EC doesn't support the LIMIT_POWER parameter, assume that
1466 * LIMIT_POWER is not requested.
1467 */
1468 if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
1469 log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1470 return -ENOSYS;
1471 }
1472
1473 if (ret != sizeof(r.get_param))
1474 return -EINVAL;
1475
1476 *limit_powerp = r.get_param.value;
1477 return 0;
1478}
1479
1480int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
1481{
1482 struct ec_params_config_power_button params;
1483 int ret;
1484
1485 params.flags = flags;
1486 ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
1487 &params, sizeof(params), NULL, 0);
1488 if (ret < 0)
1489 return ret;
1490
1491 return 0;
1492}
1493
1494int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
1495{
1496 u32 mask;
1497 int ret;
1498
1499 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1500 &mask);
1501 if (ret < 0)
1502 return ret;
1503
1504 return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
1505}
1506
1507int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
1508{
1509 u32 mask;
1510 int ret;
1511
1512 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1513 &mask);
1514 if (ret < 0)
1515 return ret;
1516
Simon Glass48d268f2018-11-23 21:29:36 -07001517 /* Set lid close event state in the EC SMI event mask */
Simon Glass153f2522018-11-06 15:21:22 -07001518 if (enable)
1519 mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1520 else
1521 mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1522
1523 ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
1524 if (ret < 0)
1525 return ret;
1526
1527 printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
Simon Glass9ad07af2015-08-03 08:19:23 -06001528 return 0;
1529}
1530
Simon Glass44569b52014-10-13 23:42:14 -06001531UCLASS_DRIVER(cros_ec) = {
1532 .id = UCLASS_CROS_EC,
Simon Glassf18ca782019-05-02 10:52:11 -06001533 .name = "cros-ec",
Simon Glass8a2b47f2020-12-03 16:55:17 -07001534 .per_device_auto = sizeof(struct cros_ec_dev),
Simon Glass18230342016-07-05 17:10:10 -06001535 .post_bind = dm_scan_fdt_dev,
Simon Glass134022a2018-11-06 15:21:21 -07001536 .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,
Simon Glass44569b52014-10-13 23:42:14 -06001537};