blob: c627c1df6c22a7475eb66434da1ed822f34f8f41 [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);
Simon Glasscd3a63e2021-01-16 14:52:24 -0700407 if (len > din_len)
408 return -ENOSPC;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800409 memmove(din, in_buffer, len);
410 }
411 }
412 return len;
413}
414
Simon Glassfb726402015-10-18 21:17:14 -0600415int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800416{
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200417 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
Simon Glass19cc2952014-02-27 13:26:11 -0700418 sizeof(scan->data)) != sizeof(scan->data))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800419 return -1;
420
421 return 0;
422}
423
Alper Nebi Yasak1ce07292020-10-30 20:25:20 +0300424int cros_ec_get_next_event(struct udevice *dev,
425 struct ec_response_get_next_event *event)
426{
427 int ret;
428
429 ret = ec_command(dev, EC_CMD_GET_NEXT_EVENT, 0, NULL, 0,
430 event, sizeof(*event));
431 if (ret < 0)
432 return ret;
433 else if (ret != sizeof(*event))
434 return -EC_RES_INVALID_RESPONSE;
435
436 return 0;
437}
438
Simon Glassbed43522018-10-01 12:22:22 -0600439int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800440{
441 struct ec_response_get_version *r;
Simon Glassa77024d2018-11-06 15:21:19 -0700442 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800443
Simon Glassa77024d2018-11-06 15:21:19 -0700444 ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
445 (uint8_t **)&r, sizeof(*r));
446 if (ret != sizeof(*r)) {
Simon Glass48d268f2018-11-23 21:29:36 -0700447 log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800448 return -1;
Simon Glassa77024d2018-11-06 15:21:19 -0700449 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800450
Simon Glass19cc2952014-02-27 13:26:11 -0700451 if (maxlen > (int)sizeof(r->version_string_ro))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800452 maxlen = sizeof(r->version_string_ro);
453
454 switch (r->current_image) {
455 case EC_IMAGE_RO:
456 memcpy(id, r->version_string_ro, maxlen);
457 break;
458 case EC_IMAGE_RW:
459 memcpy(id, r->version_string_rw, maxlen);
460 break;
461 default:
Simon Glassa77024d2018-11-06 15:21:19 -0700462 log_err("Invalid EC image %d\n", r->current_image);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800463 return -1;
464 }
465
466 id[maxlen - 1] = '\0';
467 return 0;
468}
469
Simon Glassbed43522018-10-01 12:22:22 -0600470int cros_ec_read_version(struct udevice *dev,
471 struct ec_response_get_version **versionp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800472{
473 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
474 (uint8_t **)versionp, sizeof(**versionp))
Simon Glass19cc2952014-02-27 13:26:11 -0700475 != sizeof(**versionp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800476 return -1;
477
478 return 0;
479}
480
Simon Glassbed43522018-10-01 12:22:22 -0600481int cros_ec_read_build_info(struct udevice *dev, char **strp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800482{
483 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
Simon Glassece28862014-02-27 13:26:07 -0700484 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800485 return -1;
486
487 return 0;
488}
489
Simon Glassbed43522018-10-01 12:22:22 -0600490int cros_ec_read_current_image(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600491 enum ec_current_image *image)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800492{
493 struct ec_response_get_version *r;
494
495 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700496 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800497 return -1;
498
499 *image = r->current_image;
500 return 0;
501}
502
Simon Glassbed43522018-10-01 12:22:22 -0600503static int cros_ec_wait_on_hash_done(struct udevice *dev,
Simon Glass97208d92020-11-09 07:14:43 -0700504 struct ec_params_vboot_hash *p,
Simon Glass699c9ca2018-10-01 12:22:08 -0600505 struct ec_response_vboot_hash *hash)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800506{
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800507 ulong start;
508
509 start = get_timer(0);
510 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
Simon Glassebee5332021-01-16 14:52:23 -0700511 mdelay(CROS_EC_HASH_CHECK_DELAY_MS);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800512
Simon Glass97208d92020-11-09 07:14:43 -0700513 p->cmd = EC_VBOOT_HASH_GET;
Simon Glassebee5332021-01-16 14:52:23 -0700514
Simon Glass97208d92020-11-09 07:14:43 -0700515 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, p, sizeof(*p), hash,
516 sizeof(*hash)) < 0)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800517 return -1;
518
519 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
520 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
521 return -EC_RES_TIMEOUT;
522 }
523 }
524 return 0;
525}
526
Simon Glass94564902018-10-01 12:22:38 -0600527int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
528 struct ec_response_vboot_hash *hash)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800529{
530 struct ec_params_vboot_hash p;
531 int rv;
532
533 p.cmd = EC_VBOOT_HASH_GET;
Simon Glass94564902018-10-01 12:22:38 -0600534 p.offset = hash_offset;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800535 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
536 hash, sizeof(*hash)) < 0)
537 return -1;
538
539 /* If the EC is busy calculating the hash, fidget until it's done. */
Simon Glass97208d92020-11-09 07:14:43 -0700540 rv = cros_ec_wait_on_hash_done(dev, &p, hash);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800541 if (rv)
542 return rv;
543
544 /* If the hash is valid, we're done. Otherwise, we have to kick it off
545 * again and wait for it to complete. Note that we explicitly assume
546 * that hashing zero bytes is always wrong, even though that would
547 * produce a valid hash value. */
548 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
549 return 0;
550
551 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
552 __func__, hash->status, hash->size);
553
Simon Glassece28862014-02-27 13:26:07 -0700554 p.cmd = EC_VBOOT_HASH_START;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800555 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
556 p.nonce_size = 0;
Simon Glass94564902018-10-01 12:22:38 -0600557 p.offset = hash_offset;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800558
559 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
560 hash, sizeof(*hash)) < 0)
561 return -1;
562
Simon Glass97208d92020-11-09 07:14:43 -0700563 rv = cros_ec_wait_on_hash_done(dev, &p, hash);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800564 if (rv)
565 return rv;
Simon Glass97208d92020-11-09 07:14:43 -0700566 if (hash->status != EC_VBOOT_HASH_STATUS_DONE) {
567 log_err("Hash did not complete, status=%d\n", hash->status);
568 return -EIO;
569 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800570
571 debug("%s: hash done\n", __func__);
572
573 return 0;
574}
575
Simon Glassbed43522018-10-01 12:22:22 -0600576static int cros_ec_invalidate_hash(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800577{
578 struct ec_params_vboot_hash p;
579 struct ec_response_vboot_hash *hash;
580
581 /* We don't have an explict command for the EC to discard its current
582 * hash value, so we'll just tell it to calculate one that we know is
583 * wrong (we claim that hashing zero bytes is always invalid).
584 */
585 p.cmd = EC_VBOOT_HASH_RECALC;
586 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
587 p.nonce_size = 0;
588 p.offset = 0;
589 p.size = 0;
590
591 debug("%s:\n", __func__);
592
593 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
594 (uint8_t **)&hash, sizeof(*hash)) < 0)
595 return -1;
596
597 /* No need to wait for it to finish */
598 return 0;
599}
600
Simon Glass9b306e52021-01-16 14:52:22 -0700601int cros_ec_hello(struct udevice *dev, uint *handshakep)
602{
603 struct ec_params_hello req;
604 struct ec_response_hello *resp;
605
606 req.in_data = 0x12345678;
607 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
608 (uint8_t **)&resp, sizeof(*resp)) < 0)
609 return -EIO;
610 if (resp->out_data != req.in_data + 0x01020304) {
611 printf("Received invalid handshake %x\n", resp->out_data);
612 if (handshakep)
613 *handshakep = req.in_data;
614 return -ENOTSYNC;
615 }
616
617 return 0;
618}
619
Simon Glassbed43522018-10-01 12:22:22 -0600620int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800621{
622 struct ec_params_reboot_ec p;
623
624 p.cmd = cmd;
625 p.flags = flags;
626
627 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
628 < 0)
629 return -1;
630
631 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
Simon Glassebee5332021-01-16 14:52:23 -0700632 ulong start;
633
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800634 /*
635 * EC reboot will take place immediately so delay to allow it
636 * to complete. Note that some reboot types (EC_REBOOT_COLD)
637 * will reboot the AP as well, in which case we won't actually
638 * get to this point.
639 */
Simon Glassebee5332021-01-16 14:52:23 -0700640 mdelay(50);
641 start = get_timer(0);
642 while (cros_ec_hello(dev, NULL)) {
643 if (get_timer(start) > 3000) {
644 log_err("EC did not return from reboot\n");
645 return -ETIMEDOUT;
646 }
647 mdelay(5);
648 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800649 }
650
651 return 0;
652}
653
Simon Glassfb726402015-10-18 21:17:14 -0600654int cros_ec_interrupt_pending(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800655{
Simon Glassfb726402015-10-18 21:17:14 -0600656 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
657
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800658 /* no interrupt support : always poll */
Simon Glassfb726402015-10-18 21:17:14 -0600659 if (!dm_gpio_is_valid(&cdev->ec_int))
Simon Glass19cc2952014-02-27 13:26:11 -0700660 return -ENOENT;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800661
Simon Glassfb726402015-10-18 21:17:14 -0600662 return dm_gpio_get_value(&cdev->ec_int);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800663}
664
Simon Glassbed43522018-10-01 12:22:22 -0600665int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800666{
Simon Glassece28862014-02-27 13:26:07 -0700667 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
Simon Glass19cc2952014-02-27 13:26:11 -0700668 sizeof(*info)) != sizeof(*info))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800669 return -1;
670
671 return 0;
672}
673
Simon Glass153f2522018-11-06 15:21:22 -0700674int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
675{
676 struct ec_response_host_event_mask rsp;
677 int ret;
678
679 ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
680 if (ret < 0)
681 return ret;
682 else if (ret != sizeof(rsp))
683 return -EINVAL;
684
685 *mask = rsp.mask;
686
687 return 0;
688}
689
690int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
691{
692 struct ec_params_host_event_mask req;
693 int ret;
694
695 req.mask = mask;
696
697 ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
698 if (ret < 0)
699 return ret;
700
701 return 0;
702}
703
Simon Glassbed43522018-10-01 12:22:22 -0600704int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800705{
706 struct ec_response_host_event_mask *resp;
707
708 /*
709 * Use the B copy of the event flags, because the main copy is already
710 * used by ACPI/SMI.
711 */
712 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700713 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800714 return -1;
715
716 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
717 return -1;
718
719 *events_ptr = resp->mask;
720 return 0;
721}
722
Simon Glassbed43522018-10-01 12:22:22 -0600723int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800724{
725 struct ec_params_host_event_mask params;
726
727 params.mask = events;
728
729 /*
730 * Use the B copy of the event flags, so it affects the data returned
731 * by cros_ec_get_host_events().
732 */
733 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
734 &params, sizeof(params), NULL, 0) < 0)
735 return -1;
736
737 return 0;
738}
739
Simon Glassbed43522018-10-01 12:22:22 -0600740int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
741 uint32_t set_flags,
Simon Glass699c9ca2018-10-01 12:22:08 -0600742 struct ec_response_flash_protect *resp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800743{
744 struct ec_params_flash_protect params;
745
746 params.mask = set_mask;
747 params.flags = set_flags;
748
749 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
750 &params, sizeof(params),
Simon Glass19cc2952014-02-27 13:26:11 -0700751 resp, sizeof(*resp)) != sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800752 return -1;
753
754 return 0;
755}
756
Simon Glassbed43522018-10-01 12:22:22 -0600757static int cros_ec_check_version(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800758{
Simon Glassbed43522018-10-01 12:22:22 -0600759 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800760 struct ec_params_hello req;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800761
Simon Glass23fb1562015-03-26 09:29:30 -0600762 struct dm_cros_ec_ops *ops;
763 int ret;
764
Simon Glassbed43522018-10-01 12:22:22 -0600765 ops = dm_cros_ec_get_ops(dev);
Simon Glass23fb1562015-03-26 09:29:30 -0600766 if (ops->check_version) {
Simon Glassbed43522018-10-01 12:22:22 -0600767 ret = ops->check_version(dev);
Simon Glass23fb1562015-03-26 09:29:30 -0600768 if (ret)
769 return ret;
770 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800771
772 /*
773 * TODO(sjg@chromium.org).
774 * There is a strange oddity here with the EC. We could just ignore
775 * the response, i.e. pass the last two parameters as NULL and 0.
776 * In this case we won't read back very many bytes from the EC.
777 * On the I2C bus the EC gets upset about this and will try to send
778 * the bytes anyway. This means that we will have to wait for that
779 * to complete before continuing with a new EC command.
780 *
781 * This problem is probably unique to the I2C bus.
782 *
783 * So for now, just read all the data anyway.
784 */
Randall Spanglerf03e9702014-02-27 13:26:08 -0700785
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700786 /* Try sending a version 3 packet */
Simon Glassbed43522018-10-01 12:22:22 -0600787 cdev->protocol_version = 3;
Simon Glass73c1ecf2014-11-11 18:06:30 -0700788 req.in_data = 0;
Simon Glass9b306e52021-01-16 14:52:22 -0700789 ret = cros_ec_hello(dev, NULL);
790 if (!ret || ret == -ENOTSYNC)
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700791 return 0;
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700792
Randall Spanglerf03e9702014-02-27 13:26:08 -0700793 /* Try sending a version 2 packet */
Simon Glassbed43522018-10-01 12:22:22 -0600794 cdev->protocol_version = 2;
Simon Glass9b306e52021-01-16 14:52:22 -0700795 ret = cros_ec_hello(dev, NULL);
796 if (!ret || ret == -ENOTSYNC)
Randall Spanglerf03e9702014-02-27 13:26:08 -0700797 return 0;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800798
Randall Spanglerf03e9702014-02-27 13:26:08 -0700799 /*
800 * Fail if we're still here, since the EC doesn't understand any
801 * protcol version we speak. Version 1 interface without command
802 * version is no longer supported, and we don't know about any new
803 * protocol versions.
804 */
Simon Glassbed43522018-10-01 12:22:22 -0600805 cdev->protocol_version = 0;
Randall Spanglerf03e9702014-02-27 13:26:08 -0700806 printf("%s: ERROR: old EC interface not supported\n", __func__);
807 return -1;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800808}
809
Simon Glassbed43522018-10-01 12:22:22 -0600810int cros_ec_test(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800811{
Simon Glass9b306e52021-01-16 14:52:22 -0700812 uint out_data;
813 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800814
Simon Glass9b306e52021-01-16 14:52:22 -0700815 ret = cros_ec_hello(dev, &out_data);
816 if (ret == -ENOTSYNC) {
817 printf("Received invalid handshake %x\n", out_data);
818 return ret;
819 } else if (ret) {
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800820 printf("ec_command_inptr() returned error\n");
Simon Glass9b306e52021-01-16 14:52:22 -0700821 return ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800822 }
823
824 return 0;
825}
826
Simon Glassbed43522018-10-01 12:22:22 -0600827int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800828 uint32_t *offset, uint32_t *size)
829{
830 struct ec_params_flash_region_info p;
831 struct ec_response_flash_region_info *r;
832 int ret;
833
834 p.region = region;
835 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
836 EC_VER_FLASH_REGION_INFO,
837 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
838 if (ret != sizeof(*r))
839 return -1;
840
841 if (offset)
842 *offset = r->offset;
843 if (size)
844 *size = r->size;
845
846 return 0;
847}
848
Simon Glassbed43522018-10-01 12:22:22 -0600849int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800850{
851 struct ec_params_flash_erase p;
852
853 p.offset = offset;
854 p.size = size;
855 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
856 NULL, 0);
857}
858
859/**
860 * Write a single block to the flash
861 *
862 * Write a block of data to the EC flash. The size must not exceed the flash
863 * write block size which you can obtain from cros_ec_flash_write_burst_size().
864 *
865 * The offset starts at 0. You can obtain the region information from
866 * cros_ec_flash_offset() to find out where to write for a particular region.
867 *
868 * Attempting to write to the region where the EC is currently running from
869 * will result in an error.
870 *
871 * @param dev CROS-EC device
872 * @param data Pointer to data buffer to write
873 * @param offset Offset within flash to write to.
874 * @param size Number of bytes to write
875 * @return 0 if ok, -1 on error
876 */
Simon Glassbed43522018-10-01 12:22:22 -0600877static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
878 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800879{
Moritz Fischere597bc72016-09-12 12:57:52 -0700880 struct ec_params_flash_write *p;
881 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800882
Moritz Fischere597bc72016-09-12 12:57:52 -0700883 p = malloc(sizeof(*p) + size);
884 if (!p)
885 return -ENOMEM;
886
887 p->offset = offset;
888 p->size = size;
889 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
890 memcpy(p + 1, data, p->size);
891
892 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
893 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
894
895 free(p);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800896
Moritz Fischere597bc72016-09-12 12:57:52 -0700897 return ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800898}
899
900/**
901 * Return optimal flash write burst size
902 */
Simon Glassbed43522018-10-01 12:22:22 -0600903static int cros_ec_flash_write_burst_size(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800904{
Simon Glassece28862014-02-27 13:26:07 -0700905 return EC_FLASH_WRITE_VER0_SIZE;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800906}
907
908/**
909 * Check if a block of data is erased (all 0xff)
910 *
911 * This function is useful when dealing with flash, for checking whether a
912 * data block is erased and thus does not need to be programmed.
913 *
914 * @param data Pointer to data to check (must be word-aligned)
915 * @param size Number of bytes to check (must be word-aligned)
916 * @return 0 if erased, non-zero if any word is not erased
917 */
918static int cros_ec_data_is_erased(const uint32_t *data, int size)
919{
920 assert(!(size & 3));
921 size /= sizeof(uint32_t);
922 for (; size > 0; size -= 4, data++)
923 if (*data != -1U)
924 return 0;
925
926 return 1;
927}
928
Moritz Fischerde5bc892016-09-13 14:44:48 -0700929/**
930 * Read back flash parameters
931 *
932 * This function reads back parameters of the flash as reported by the EC
933 *
934 * @param dev Pointer to device
935 * @param info Pointer to output flash info struct
936 */
Simon Glassbed43522018-10-01 12:22:22 -0600937int cros_ec_read_flashinfo(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600938 struct ec_response_flash_info *info)
Moritz Fischerde5bc892016-09-13 14:44:48 -0700939{
940 int ret;
941
942 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
943 NULL, 0, info, sizeof(*info));
944 if (ret < 0)
945 return ret;
946
947 return ret < sizeof(*info) ? -1 : 0;
948}
949
Simon Glassbed43522018-10-01 12:22:22 -0600950int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
Simon Glass699c9ca2018-10-01 12:22:08 -0600951 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800952{
Simon Glassbed43522018-10-01 12:22:22 -0600953 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800954 uint32_t burst = cros_ec_flash_write_burst_size(dev);
955 uint32_t end, off;
956 int ret;
957
Simon Glass411661d2018-11-06 15:21:20 -0700958 if (!burst)
959 return -EINVAL;
960
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800961 /*
962 * TODO: round up to the nearest multiple of write size. Can get away
963 * without that on link right now because its write size is 4 bytes.
964 */
965 end = offset + size;
966 for (off = offset; off < end; off += burst, data += burst) {
967 uint32_t todo;
968
969 /* If the data is empty, there is no point in programming it */
970 todo = min(end - off, burst);
Simon Glassbed43522018-10-01 12:22:22 -0600971 if (cdev->optimise_flash_write &&
Simon Glass699c9ca2018-10-01 12:22:08 -0600972 cros_ec_data_is_erased((uint32_t *)data, todo))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800973 continue;
974
975 ret = cros_ec_flash_write_block(dev, data, off, todo);
976 if (ret)
977 return ret;
978 }
979
980 return 0;
981}
982
983/**
Simon Glass153f2522018-11-06 15:21:22 -0700984 * Run verification on a slot
985 *
986 * @param me CrosEc instance
987 * @param region Region to run verification on
988 * @return 0 if success or not applicable. Non-zero if verification failed.
989 */
990int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
991{
992 struct ec_params_efs_verify p;
993 int rv;
994
995 log_info("EFS: EC is verifying updated image...\n");
996 p.region = region;
997
998 rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
999 if (rv >= 0) {
1000 log_info("EFS: Verification success\n");
1001 return 0;
1002 }
1003 if (rv == -EC_RES_INVALID_COMMAND) {
1004 log_info("EFS: EC doesn't support EFS_VERIFY command\n");
1005 return 0;
1006 }
1007 log_info("EFS: Verification failed\n");
1008
1009 return rv;
1010}
1011
1012/**
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001013 * Read a single block from the flash
1014 *
1015 * Read a block of data from the EC flash. The size must not exceed the flash
1016 * write block size which you can obtain from cros_ec_flash_write_burst_size().
1017 *
1018 * The offset starts at 0. You can obtain the region information from
1019 * cros_ec_flash_offset() to find out where to read for a particular region.
1020 *
1021 * @param dev CROS-EC device
1022 * @param data Pointer to data buffer to read into
1023 * @param offset Offset within flash to read from
1024 * @param size Number of bytes to read
1025 * @return 0 if ok, -1 on error
1026 */
Simon Glassbed43522018-10-01 12:22:22 -06001027static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
Simon Glass699c9ca2018-10-01 12:22:08 -06001028 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001029{
1030 struct ec_params_flash_read p;
1031
1032 p.offset = offset;
1033 p.size = size;
1034
1035 return ec_command(dev, EC_CMD_FLASH_READ, 0,
1036 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
1037}
1038
Simon Glassbed43522018-10-01 12:22:22 -06001039int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
Simon Glass699c9ca2018-10-01 12:22:08 -06001040 uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001041{
1042 uint32_t burst = cros_ec_flash_write_burst_size(dev);
1043 uint32_t end, off;
1044 int ret;
1045
1046 end = offset + size;
1047 for (off = offset; off < end; off += burst, data += burst) {
1048 ret = cros_ec_flash_read_block(dev, data, off,
1049 min(end - off, burst));
1050 if (ret)
1051 return ret;
1052 }
1053
1054 return 0;
1055}
1056
Simon Glassbed43522018-10-01 12:22:22 -06001057int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
Simon Glass699c9ca2018-10-01 12:22:08 -06001058 int image_size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001059{
1060 uint32_t rw_offset, rw_size;
1061 int ret;
1062
Simon Glass2c1e5502018-10-01 12:22:36 -06001063 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
1064 &rw_size))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001065 return -1;
Simon Glass19cc2952014-02-27 13:26:11 -07001066 if (image_size > (int)rw_size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001067 return -1;
1068
1069 /* Invalidate the existing hash, just in case the AP reboots
1070 * unexpectedly during the update. If that happened, the EC RW firmware
1071 * would be invalid, but the EC would still have the original hash.
1072 */
1073 ret = cros_ec_invalidate_hash(dev);
1074 if (ret)
1075 return ret;
1076
1077 /*
1078 * Erase the entire RW section, so that the EC doesn't see any garbage
1079 * past the new image if it's smaller than the current image.
1080 *
1081 * TODO: could optimize this to erase just the current image, since
1082 * presumably everything past that is 0xff's. But would still need to
1083 * round up to the nearest multiple of erase size.
1084 */
1085 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
1086 if (ret)
1087 return ret;
1088
1089 /* Write the image */
1090 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
1091 if (ret)
1092 return ret;
1093
1094 return 0;
1095}
1096
Simon Glass33909b52021-01-16 14:52:25 -07001097int cros_ec_get_sku_id(struct udevice *dev)
1098{
1099 struct ec_sku_id_info *r;
1100 int ret;
1101
1102 ret = ec_command_inptr(dev, EC_CMD_GET_SKU_ID, 0, NULL, 0,
1103 (uint8_t **)&r, sizeof(*r));
1104 if (ret != sizeof(*r))
1105 return -ret;
1106
1107 return r->sku_id;
1108}
1109
Simon Glassbed43522018-10-01 12:22:22 -06001110int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001111{
1112 struct ec_params_vbnvcontext p;
1113 int len;
1114
Simon Glass153f2522018-11-06 15:21:22 -07001115 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
Simon Glassbed43522018-10-01 12:22:22 -06001116 return -EINVAL;
1117
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001118 p.op = EC_VBNV_CONTEXT_OP_READ;
1119
1120 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
Simon Glass153f2522018-11-06 15:21:22 -07001121 &p, sizeof(uint32_t) + size, block, size);
1122 if (len != size) {
1123 log_err("Expected %d bytes, got %d\n", size, len);
Simon Glassbed43522018-10-01 12:22:22 -06001124 return -EIO;
Simon Glass153f2522018-11-06 15:21:22 -07001125 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001126
1127 return 0;
1128}
1129
Simon Glassbed43522018-10-01 12:22:22 -06001130int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001131{
1132 struct ec_params_vbnvcontext p;
1133 int len;
1134
Simon Glass153f2522018-11-06 15:21:22 -07001135 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
Simon Glassbed43522018-10-01 12:22:22 -06001136 return -EINVAL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001137 p.op = EC_VBNV_CONTEXT_OP_WRITE;
Simon Glass153f2522018-11-06 15:21:22 -07001138 memcpy(p.block, block, size);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001139
1140 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
Simon Glass153f2522018-11-06 15:21:22 -07001141 &p, sizeof(uint32_t) + size, NULL, 0);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001142 if (len < 0)
1143 return -1;
1144
Simon Glass153f2522018-11-06 15:21:22 -07001145 return 0;
1146}
1147
1148int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
1149{
1150 struct ec_params_battery_cutoff p;
1151 int len;
1152
1153 p.flags = flags;
1154 len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
1155 NULL, 0);
1156
1157 if (len < 0)
1158 return -1;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001159 return 0;
1160}
1161
Alper Nebi Yasak2b8031f2020-10-22 23:49:27 +03001162int cros_ec_set_pwm_duty(struct udevice *dev, uint8_t index, uint16_t duty)
1163{
1164 struct ec_params_pwm_set_duty p;
1165 int ret;
1166
1167 p.duty = duty;
1168 p.pwm_type = EC_PWM_TYPE_GENERIC;
1169 p.index = index;
1170
1171 ret = ec_command(dev, EC_CMD_PWM_SET_DUTY, 0, &p, sizeof(p),
1172 NULL, 0);
1173 if (ret < 0)
1174 return ret;
1175
1176 return 0;
1177}
1178
Simon Glasseb2cc512015-08-03 08:19:24 -06001179int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001180{
1181 struct ec_params_ldo_set params;
1182
1183 params.index = index;
1184 params.state = state;
1185
Simon Glassbed43522018-10-01 12:22:22 -06001186 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
Simon Glasseb2cc512015-08-03 08:19:24 -06001187 NULL, 0))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001188 return -1;
1189
1190 return 0;
1191}
1192
Simon Glasseb2cc512015-08-03 08:19:24 -06001193int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001194{
1195 struct ec_params_ldo_get params;
1196 struct ec_response_ldo_get *resp;
1197
1198 params.index = index;
1199
Simon Glassbed43522018-10-01 12:22:22 -06001200 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
Simon Glasseb2cc512015-08-03 08:19:24 -06001201 (uint8_t **)&resp, sizeof(*resp)) !=
1202 sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001203 return -1;
1204
1205 *state = resp->state;
1206
1207 return 0;
1208}
1209
Simon Glass44569b52014-10-13 23:42:14 -06001210int cros_ec_register(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001211{
Simon Glassde0977b2015-03-05 12:25:20 -07001212 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001213 char id[MSG_BYTES];
Simon Glass44569b52014-10-13 23:42:14 -06001214
1215 cdev->dev = dev;
Simon Glass0d7cc472015-01-05 20:05:32 -07001216 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1217 GPIOD_IS_IN);
Simon Glassb35e6d62017-05-18 20:09:23 -06001218 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
Simon Glass44569b52014-10-13 23:42:14 -06001219
Simon Glassbed43522018-10-01 12:22:22 -06001220 if (cros_ec_check_version(dev)) {
Simon Glass44569b52014-10-13 23:42:14 -06001221 debug("%s: Could not detect CROS-EC version\n", __func__);
1222 return -CROS_EC_ERR_CHECK_VERSION;
1223 }
1224
Simon Glassbed43522018-10-01 12:22:22 -06001225 if (cros_ec_read_id(dev, id, sizeof(id))) {
Simon Glass44569b52014-10-13 23:42:14 -06001226 debug("%s: Could not read KBC ID\n", __func__);
1227 return -CROS_EC_ERR_READ_ID;
1228 }
1229
1230 /* Remember this device for use by the cros_ec command */
Simon Glass46e17332015-02-17 15:29:36 -07001231 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1232 cdev->protocol_version, id);
Simon Glass44569b52014-10-13 23:42:14 -06001233
1234 return 0;
1235}
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001236
Simon Glassb35e6d62017-05-18 20:09:23 -06001237int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
Simon Glass3c59abc2014-02-27 13:26:03 -07001238{
Simon Glassb35e6d62017-05-18 20:09:23 -06001239 ofnode flash_node, node;
Simon Glass3c59abc2014-02-27 13:26:03 -07001240
Simon Glassb35e6d62017-05-18 20:09:23 -06001241 flash_node = dev_read_subnode(dev, "flash");
1242 if (!ofnode_valid(flash_node)) {
Simon Glass3c59abc2014-02-27 13:26:03 -07001243 debug("Failed to find flash node\n");
1244 return -1;
1245 }
1246
Simon Glass4b580932018-06-11 13:07:17 -06001247 if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
Simon Glassb35e6d62017-05-18 20:09:23 -06001248 debug("Failed to decode flash node in chrome-ec\n");
Simon Glass3c59abc2014-02-27 13:26:03 -07001249 return -1;
1250 }
1251
Simon Glassb35e6d62017-05-18 20:09:23 -06001252 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1253 "erase-value", -1);
Simon Glass28529762017-08-05 15:45:54 -06001254 ofnode_for_each_subnode(node, flash_node) {
Simon Glassb35e6d62017-05-18 20:09:23 -06001255 const char *name = ofnode_get_name(node);
Simon Glass3c59abc2014-02-27 13:26:03 -07001256 enum ec_flash_region region;
1257
1258 if (0 == strcmp(name, "ro")) {
1259 region = EC_FLASH_REGION_RO;
1260 } else if (0 == strcmp(name, "rw")) {
Simon Glass2c1e5502018-10-01 12:22:36 -06001261 region = EC_FLASH_REGION_ACTIVE;
Simon Glass3c59abc2014-02-27 13:26:03 -07001262 } else if (0 == strcmp(name, "wp-ro")) {
1263 region = EC_FLASH_REGION_WP_RO;
1264 } else {
1265 debug("Unknown EC flash region name '%s'\n", name);
1266 return -1;
1267 }
1268
Simon Glass4b580932018-06-11 13:07:17 -06001269 if (ofnode_read_fmap_entry(node, &config->region[region])) {
Simon Glass3c59abc2014-02-27 13:26:03 -07001270 debug("Failed to decode flash region in chrome-ec'\n");
1271 return -1;
1272 }
1273 }
1274
1275 return 0;
1276}
1277
Moritz Fischer6cddc602016-09-27 15:42:07 -07001278int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1279 int nmsgs)
Simon Glass9ad07af2015-08-03 08:19:23 -06001280{
Simon Glass9ad07af2015-08-03 08:19:23 -06001281 union {
1282 struct ec_params_i2c_passthru p;
1283 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1284 } params;
1285 union {
1286 struct ec_response_i2c_passthru r;
1287 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1288 } response;
1289 struct ec_params_i2c_passthru *p = &params.p;
1290 struct ec_response_i2c_passthru *r = &response.r;
1291 struct ec_params_i2c_passthru_msg *msg;
1292 uint8_t *pdata, *read_ptr = NULL;
1293 int read_len;
1294 int size;
1295 int rv;
1296 int i;
1297
Moritz Fischer6cddc602016-09-27 15:42:07 -07001298 p->port = port;
Simon Glass9ad07af2015-08-03 08:19:23 -06001299
1300 p->num_msgs = nmsgs;
1301 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1302
1303 /* Create a message to write the register address and optional data */
1304 pdata = (uint8_t *)p + size;
1305
1306 read_len = 0;
1307 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1308 bool is_read = in->flags & I2C_M_RD;
1309
1310 msg->addr_flags = in->addr;
1311 msg->len = in->len;
1312 if (is_read) {
1313 msg->addr_flags |= EC_I2C_FLAG_READ;
1314 read_len += in->len;
1315 read_ptr = in->buf;
1316 if (sizeof(*r) + read_len > sizeof(response)) {
1317 puts("Read length too big for buffer\n");
1318 return -1;
1319 }
1320 } else {
1321 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1322 puts("Params too large for buffer\n");
1323 return -1;
1324 }
1325 memcpy(pdata, in->buf, in->len);
1326 pdata += in->len;
1327 }
1328 }
1329
Simon Glassbed43522018-10-01 12:22:22 -06001330 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
Simon Glass9ad07af2015-08-03 08:19:23 -06001331 r, sizeof(*r) + read_len);
1332 if (rv < 0)
1333 return rv;
1334
1335 /* Parse response */
1336 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1337 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1338 return -1;
1339 }
1340
1341 if (rv < sizeof(*r) + read_len) {
1342 puts("Truncated read response\n");
1343 return -1;
1344 }
1345
1346 /* We only support a single read message for each transfer */
1347 if (read_len)
1348 memcpy(read_ptr, r->data, read_len);
1349
Simon Glass153f2522018-11-06 15:21:22 -07001350 return 0;
1351}
1352
Simon Glass959e1ce2021-01-16 14:52:26 -07001353int cros_ec_get_features(struct udevice *dev, u64 *featuresp)
Simon Glass153f2522018-11-06 15:21:22 -07001354{
1355 struct ec_response_get_features r;
1356 int rv;
1357
Simon Glass959e1ce2021-01-16 14:52:26 -07001358 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, NULL, 0, &r, sizeof(r));
1359 if (rv != sizeof(r))
1360 return -EIO;
1361 *featuresp = r.flags[0] | (u64)r.flags[1] << 32;
1362
1363 return 0;
1364}
1365
1366int cros_ec_check_feature(struct udevice *dev, uint feature)
1367{
1368 struct ec_response_get_features r;
1369 int rv;
1370
1371 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, NULL, 0, &r, sizeof(r));
1372 if (rv != sizeof(r))
1373 return -EIO;
Simon Glass153f2522018-11-06 15:21:22 -07001374
1375 if (feature >= 8 * sizeof(r.flags))
Simon Glass959e1ce2021-01-16 14:52:26 -07001376 return -EINVAL;
Simon Glass153f2522018-11-06 15:21:22 -07001377
Simon Glass959e1ce2021-01-16 14:52:26 -07001378 return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature) ? true :
1379 false;
Simon Glass153f2522018-11-06 15:21:22 -07001380}
1381
1382/*
1383 * Query the EC for specified mask indicating enabled events.
1384 * The EC maintains separate event masks for SMI, SCI and WAKE.
1385 */
1386static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
1387 uint64_t *value)
1388{
1389 int ret;
1390 struct ec_params_host_event req;
1391 struct ec_response_host_event rsp;
1392
1393 req.action = action;
1394 req.mask_type = mask;
1395 if (action != EC_HOST_EVENT_GET)
1396 req.value = *value;
1397 else
1398 *value = 0;
1399 ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
1400 sizeof(rsp));
1401
1402 if (action != EC_HOST_EVENT_GET)
1403 return ret;
1404 if (ret == 0)
1405 *value = rsp.value;
1406
1407 return ret;
1408}
1409
1410static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
1411 uint action, uint64_t *value)
1412{
1413 int ret = -1;
1414 struct ec_params_host_event_mask req;
1415 struct ec_response_host_event_mask rsp;
1416
1417 if (hcmd == INVALID_HCMD)
1418 return ret;
1419
1420 if (action != EC_HOST_EVENT_GET)
1421 req.mask = (uint32_t)*value;
1422 else
1423 *value = 0;
1424
1425 ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
1426 if (action != EC_HOST_EVENT_GET)
1427 return ret;
1428 if (ret == 0)
1429 *value = rsp.mask;
1430
1431 return ret;
1432}
1433
1434bool cros_ec_is_uhepi_supported(struct udevice *dev)
1435{
1436#define UHEPI_SUPPORTED 1
1437#define UHEPI_NOT_SUPPORTED 2
1438 static int uhepi_support;
1439
1440 if (!uhepi_support) {
1441 uhepi_support = cros_ec_check_feature(dev,
1442 EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
1443 UHEPI_NOT_SUPPORTED;
1444 log_debug("Chrome EC: UHEPI %s\n",
1445 uhepi_support == UHEPI_SUPPORTED ? "supported" :
1446 "not supported");
1447 }
1448 return uhepi_support == UHEPI_SUPPORTED;
1449}
1450
1451static int cros_ec_get_mask(struct udevice *dev, uint type)
1452{
1453 u64 value = 0;
1454
1455 if (cros_ec_is_uhepi_supported(dev)) {
1456 cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
1457 } else {
1458 assert(type < ARRAY_SIZE(event_map));
1459 cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
1460 EC_HOST_EVENT_GET, &value);
1461 }
1462 return value;
1463}
1464
1465static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
1466{
1467 if (cros_ec_is_uhepi_supported(dev))
1468 return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
1469
1470 assert(type < ARRAY_SIZE(event_map));
1471
1472 return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
1473 EC_HOST_EVENT_CLEAR, &mask);
1474}
1475
1476uint64_t cros_ec_get_events_b(struct udevice *dev)
1477{
1478 return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
1479}
1480
1481int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
1482{
1483 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
1484
1485 return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
1486}
1487
1488int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
1489{
1490 struct ec_params_charge_state p;
1491 struct ec_response_charge_state r;
1492 int ret;
1493
1494 p.cmd = CHARGE_STATE_CMD_GET_PARAM;
1495 p.get_param.param = CS_PARAM_LIMIT_POWER;
1496 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
1497 &r, sizeof(r));
1498
1499 /*
1500 * If our EC doesn't support the LIMIT_POWER parameter, assume that
1501 * LIMIT_POWER is not requested.
1502 */
1503 if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
1504 log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1505 return -ENOSYS;
1506 }
1507
1508 if (ret != sizeof(r.get_param))
1509 return -EINVAL;
1510
1511 *limit_powerp = r.get_param.value;
1512 return 0;
1513}
1514
1515int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
1516{
1517 struct ec_params_config_power_button params;
1518 int ret;
1519
1520 params.flags = flags;
1521 ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
1522 &params, sizeof(params), NULL, 0);
1523 if (ret < 0)
1524 return ret;
1525
1526 return 0;
1527}
1528
1529int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
1530{
1531 u32 mask;
1532 int ret;
1533
1534 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1535 &mask);
1536 if (ret < 0)
1537 return ret;
1538
1539 return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
1540}
1541
1542int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
1543{
1544 u32 mask;
1545 int ret;
1546
1547 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1548 &mask);
1549 if (ret < 0)
1550 return ret;
1551
Simon Glass48d268f2018-11-23 21:29:36 -07001552 /* Set lid close event state in the EC SMI event mask */
Simon Glass153f2522018-11-06 15:21:22 -07001553 if (enable)
1554 mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1555 else
1556 mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1557
1558 ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
1559 if (ret < 0)
1560 return ret;
1561
1562 printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
Simon Glass9ad07af2015-08-03 08:19:23 -06001563 return 0;
1564}
1565
Simon Glass333e3a92021-01-16 14:52:31 -07001566int cros_ec_vstore_supported(struct udevice *dev)
1567{
1568 return cros_ec_check_feature(dev, EC_FEATURE_VSTORE);
1569}
1570
1571int cros_ec_vstore_info(struct udevice *dev, u32 *lockedp)
1572{
1573 struct ec_response_vstore_info *resp;
1574
1575 if (ec_command_inptr(dev, EC_CMD_VSTORE_INFO, 0, NULL, 0,
1576 (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
1577 return -EIO;
1578
1579 if (lockedp)
1580 *lockedp = resp->slot_locked;
1581
1582 return resp->slot_count;
1583}
1584
1585/*
1586 * cros_ec_vstore_read - Read data from EC vstore slot
1587 *
1588 * @slot: vstore slot to read from
1589 * @data: buffer to store read data, must be EC_VSTORE_SLOT_SIZE bytes
1590 */
1591int cros_ec_vstore_read(struct udevice *dev, int slot, uint8_t *data)
1592{
1593 struct ec_params_vstore_read req;
1594 struct ec_response_vstore_read *resp;
1595
1596 req.slot = slot;
1597 if (ec_command_inptr(dev, EC_CMD_VSTORE_READ, 0, &req, sizeof(req),
1598 (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
1599 return -EIO;
1600
1601 if (!data || req.slot >= EC_VSTORE_SLOT_MAX)
1602 return -EINVAL;
1603
1604 memcpy(data, resp->data, sizeof(resp->data));
1605
1606 return 0;
1607}
1608
1609/*
1610 * cros_ec_vstore_write - Save data into EC vstore slot
1611 *
1612 * @slot: vstore slot to write into
1613 * @data: data to write
1614 * @size: size of data in bytes
1615 *
1616 * Maximum size of data is EC_VSTORE_SLOT_SIZE. It is the callers
1617 * responsibility to check the number of implemented slots by
1618 * querying the vstore info.
1619 */
1620int cros_ec_vstore_write(struct udevice *dev, int slot, const uint8_t *data,
1621 size_t size)
1622{
1623 struct ec_params_vstore_write req;
1624
1625 if (slot >= EC_VSTORE_SLOT_MAX || size > EC_VSTORE_SLOT_SIZE)
1626 return -EINVAL;
1627
1628 req.slot = slot;
1629 memcpy(req.data, data, size);
1630
1631 if (ec_command(dev, EC_CMD_VSTORE_WRITE, 0, &req, sizeof(req), NULL, 0))
1632 return -EIO;
1633
1634 return 0;
1635}
1636
Simon Glass9d702522021-01-16 14:52:28 -07001637int cros_ec_get_switches(struct udevice *dev)
1638{
1639 struct dm_cros_ec_ops *ops;
1640 int ret;
1641
1642 ops = dm_cros_ec_get_ops(dev);
1643 if (!ops->get_switches)
1644 return -ENOSYS;
1645
1646 ret = ops->get_switches(dev);
1647 if (ret < 0)
1648 return log_msg_ret("get", ret);
1649
1650 return ret;
1651}
1652
Simon Glasscbb191b2021-07-05 16:32:49 -06001653int cros_ec_read_batt_charge(struct udevice *dev, uint *chargep)
1654{
1655 struct ec_params_charge_state req;
1656 struct ec_response_charge_state resp;
1657 int ret;
1658
1659 req.cmd = CHARGE_STATE_CMD_GET_STATE;
1660 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &req, sizeof(req),
1661 &resp, sizeof(resp));
1662 if (ret)
1663 return log_msg_ret("read", ret);
1664
1665 *chargep = resp.get_state.batt_state_of_charge;
1666
1667 return 0;
1668}
1669
Simon Glass44569b52014-10-13 23:42:14 -06001670UCLASS_DRIVER(cros_ec) = {
1671 .id = UCLASS_CROS_EC,
Simon Glassf18ca782019-05-02 10:52:11 -06001672 .name = "cros-ec",
Simon Glass8a2b47f2020-12-03 16:55:17 -07001673 .per_device_auto = sizeof(struct cros_ec_dev),
Simon Glass92882652021-08-07 07:24:04 -06001674#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass18230342016-07-05 17:10:10 -06001675 .post_bind = dm_scan_fdt_dev,
Simon Glass692f38d2021-01-16 14:52:30 -07001676#endif
Simon Glass134022a2018-11-06 15:21:21 -07001677 .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,
Simon Glass44569b52014-10-13 23:42:14 -06001678};