blob: 818ad56a0dc2bb2f4dc3015cd0c79a226cb1d27c [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>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090028#include <linux/errno.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080029#include <asm/io.h>
30#include <asm-generic/gpio.h>
Simon Glass44569b52014-10-13 23:42:14 -060031#include <dm/device-internal.h>
Simon Glassb35e6d62017-05-18 20:09:23 -060032#include <dm/of_extra.h>
Simon Glass44569b52014-10-13 23:42:14 -060033#include <dm/uclass-internal.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080034
35#ifdef DEBUG_TRACE
36#define debug_trace(fmt, b...) debug(fmt, #b)
37#else
38#define debug_trace(fmt, b...)
39#endif
40
41enum {
42 /* Timeout waiting for a flash erase command to complete */
43 CROS_EC_CMD_TIMEOUT_MS = 5000,
44 /* Timeout waiting for a synchronous hash to be recomputed */
45 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
46};
47
Simon Glass153f2522018-11-06 15:21:22 -070048#define INVALID_HCMD 0xFF
49
50/*
51 * Map UHEPI masks to non UHEPI commands in order to support old EC FW
52 * which does not support UHEPI command.
53 */
54static const struct {
55 u8 set_cmd;
56 u8 clear_cmd;
57 u8 get_cmd;
58} event_map[] = {
59 [EC_HOST_EVENT_MAIN] = {
60 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
61 INVALID_HCMD,
62 },
63 [EC_HOST_EVENT_B] = {
64 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
65 EC_CMD_HOST_EVENT_GET_B,
66 },
67 [EC_HOST_EVENT_SCI_MASK] = {
68 EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
69 EC_CMD_HOST_EVENT_GET_SCI_MASK,
70 },
71 [EC_HOST_EVENT_SMI_MASK] = {
72 EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
73 EC_CMD_HOST_EVENT_GET_SMI_MASK,
74 },
75 [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
76 INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
77 },
78 [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
79 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
80 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
81 },
82 [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
83 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
84 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
85 },
86 [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = {
87 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
88 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
89 },
90 [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
91 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
92 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
93 },
94};
95
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080096void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
97{
98#ifdef DEBUG
99 int i;
100
101 printf("%s: ", name);
102 if (cmd != -1)
103 printf("cmd=%#x: ", cmd);
104 for (i = 0; i < len; i++)
105 printf("%02x ", data[i]);
106 printf("\n");
107#endif
108}
109
110/*
111 * Calculate a simple 8-bit checksum of a data block
112 *
113 * @param data Data block to checksum
114 * @param size Size of data block in bytes
115 * @return checksum value (0 to 255)
116 */
117int cros_ec_calc_checksum(const uint8_t *data, int size)
118{
119 int csum, i;
120
121 for (i = csum = 0; i < size; i++)
122 csum += data[i];
123 return csum & 0xff;
124}
125
Simon Glass13c7c5b2014-02-27 13:26:09 -0700126/**
127 * Create a request packet for protocol version 3.
128 *
129 * The packet is stored in the device's internal output buffer.
130 *
131 * @param dev CROS-EC device
132 * @param cmd Command to send (EC_CMD_...)
133 * @param cmd_version Version of command to send (EC_VER_...)
134 * @param dout Output data (may be NULL If dout_len=0)
135 * @param dout_len Size of output data in bytes
136 * @return packet size in bytes, or <0 if error.
137 */
Simon Glassbed43522018-10-01 12:22:22 -0600138static int create_proto3_request(struct cros_ec_dev *cdev,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700139 int cmd, int cmd_version,
140 const void *dout, int dout_len)
141{
Simon Glassbed43522018-10-01 12:22:22 -0600142 struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700143 int out_bytes = dout_len + sizeof(*rq);
144
145 /* Fail if output size is too big */
Simon Glassbed43522018-10-01 12:22:22 -0600146 if (out_bytes > (int)sizeof(cdev->dout)) {
Simon Glass13c7c5b2014-02-27 13:26:09 -0700147 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
148 return -EC_RES_REQUEST_TRUNCATED;
149 }
150
151 /* Fill in request packet */
152 rq->struct_version = EC_HOST_REQUEST_VERSION;
153 rq->checksum = 0;
154 rq->command = cmd;
155 rq->command_version = cmd_version;
156 rq->reserved = 0;
157 rq->data_len = dout_len;
158
159 /* Copy data after header */
160 memcpy(rq + 1, dout, dout_len);
161
162 /* Write checksum field so the entire packet sums to 0 */
Simon Glassbed43522018-10-01 12:22:22 -0600163 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
Simon Glass13c7c5b2014-02-27 13:26:09 -0700164
Simon Glassbed43522018-10-01 12:22:22 -0600165 cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700166
167 /* Return size of request packet */
168 return out_bytes;
169}
170
171/**
172 * Prepare the device to receive a protocol version 3 response.
173 *
174 * @param dev CROS-EC device
175 * @param din_len Maximum size of response in bytes
176 * @return maximum expected number of bytes in response, or <0 if error.
177 */
Simon Glassbed43522018-10-01 12:22:22 -0600178static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
Simon Glass13c7c5b2014-02-27 13:26:09 -0700179{
180 int in_bytes = din_len + sizeof(struct ec_host_response);
181
182 /* Fail if input size is too big */
Simon Glassbed43522018-10-01 12:22:22 -0600183 if (in_bytes > (int)sizeof(cdev->din)) {
Simon Glass13c7c5b2014-02-27 13:26:09 -0700184 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
185 return -EC_RES_RESPONSE_TOO_BIG;
186 }
187
188 /* Return expected size of response packet */
189 return in_bytes;
190}
191
192/**
193 * Handle a protocol version 3 response packet.
194 *
195 * The packet must already be stored in the device's internal input buffer.
196 *
197 * @param dev CROS-EC device
198 * @param dinp Returns pointer to response data
199 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700200 * @return number of bytes of response data, or <0 if error. Note that error
201 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
202 * overlap!)
Simon Glass13c7c5b2014-02-27 13:26:09 -0700203 */
204static int handle_proto3_response(struct cros_ec_dev *dev,
205 uint8_t **dinp, int din_len)
206{
207 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
208 int in_bytes;
209 int csum;
210
211 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
212
213 /* Check input data */
214 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
215 debug("%s: EC response version mismatch\n", __func__);
216 return -EC_RES_INVALID_RESPONSE;
217 }
218
219 if (rs->reserved) {
220 debug("%s: EC response reserved != 0\n", __func__);
221 return -EC_RES_INVALID_RESPONSE;
222 }
223
224 if (rs->data_len > din_len) {
225 debug("%s: EC returned too much data\n", __func__);
226 return -EC_RES_RESPONSE_TOO_BIG;
227 }
228
229 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
230
231 /* Update in_bytes to actual data size */
232 in_bytes = sizeof(*rs) + rs->data_len;
233
234 /* Verify checksum */
235 csum = cros_ec_calc_checksum(dev->din, in_bytes);
236 if (csum) {
237 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
238 csum);
239 return -EC_RES_INVALID_CHECKSUM;
240 }
241
242 /* Return error result, if any */
243 if (rs->result)
244 return -(int)rs->result;
245
246 /* If we're still here, set response data pointer and return length */
247 *dinp = (uint8_t *)(rs + 1);
248
249 return rs->data_len;
250}
251
Simon Glassbed43522018-10-01 12:22:22 -0600252static int send_command_proto3(struct cros_ec_dev *cdev,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700253 int cmd, int cmd_version,
254 const void *dout, int dout_len,
255 uint8_t **dinp, int din_len)
256{
Simon Glass44569b52014-10-13 23:42:14 -0600257 struct dm_cros_ec_ops *ops;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700258 int out_bytes, in_bytes;
259 int rv;
260
261 /* Create request packet */
Simon Glassbed43522018-10-01 12:22:22 -0600262 out_bytes = create_proto3_request(cdev, cmd, cmd_version,
Simon Glass13c7c5b2014-02-27 13:26:09 -0700263 dout, dout_len);
264 if (out_bytes < 0)
265 return out_bytes;
266
267 /* Prepare response buffer */
Simon Glassbed43522018-10-01 12:22:22 -0600268 in_bytes = prepare_proto3_response_buffer(cdev, din_len);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700269 if (in_bytes < 0)
270 return in_bytes;
271
Simon Glassbed43522018-10-01 12:22:22 -0600272 ops = dm_cros_ec_get_ops(cdev->dev);
273 rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
274 -ENOSYS;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700275 if (rv < 0)
276 return rv;
277
278 /* Process the response */
Simon Glassbed43522018-10-01 12:22:22 -0600279 return handle_proto3_response(cdev, dinp, din_len);
Simon Glass13c7c5b2014-02-27 13:26:09 -0700280}
281
Simon Glass3a239512018-11-06 15:21:18 -0700282static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800283 const void *dout, int dout_len,
284 uint8_t **dinp, int din_len)
285{
Simon Glass44569b52014-10-13 23:42:14 -0600286 struct dm_cros_ec_ops *ops;
Simon Glass13c7c5b2014-02-27 13:26:09 -0700287 int ret = -1;
288
289 /* Handle protocol version 3 support */
290 if (dev->protocol_version == 3) {
291 return send_command_proto3(dev, cmd, cmd_version,
292 dout, dout_len, dinp, din_len);
293 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800294
Simon Glass44569b52014-10-13 23:42:14 -0600295 ops = dm_cros_ec_get_ops(dev->dev);
296 ret = ops->command(dev->dev, cmd, cmd_version,
297 (const uint8_t *)dout, dout_len, dinp, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800298
299 return ret;
300}
301
302/**
303 * Send a command to the CROS-EC device and return the reply.
304 *
305 * The device's internal input/output buffers are used.
306 *
307 * @param dev CROS-EC device
308 * @param cmd Command to send (EC_CMD_...)
309 * @param cmd_version Version of command to send (EC_VER_...)
310 * @param dout Output data (may be NULL If dout_len=0)
311 * @param dout_len Size of output data in bytes
312 * @param dinp Response data (may be NULL If din_len=0).
313 * If not NULL, it will be updated to point to the data
314 * and will always be double word aligned (64-bits)
315 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700316 * @return number of bytes in response, or -ve on error
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800317 */
Michael Auchter4eaae8e2019-12-09 20:27:31 +0000318static int ec_command_inptr(struct udevice *dev, uint cmd,
Simon Glass699c9ca2018-10-01 12:22:08 -0600319 int cmd_version, const void *dout, int dout_len,
320 uint8_t **dinp, int din_len)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800321{
Simon Glassbed43522018-10-01 12:22:22 -0600322 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Simon Glass19cc2952014-02-27 13:26:11 -0700323 uint8_t *din = NULL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800324 int len;
325
Simon Glassbed43522018-10-01 12:22:22 -0600326 len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
327 din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800328
329 /* If the command doesn't complete, wait a while */
330 if (len == -EC_RES_IN_PROGRESS) {
Simon Glass19cc2952014-02-27 13:26:11 -0700331 struct ec_response_get_comms_status *resp = NULL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800332 ulong start;
333
334 /* Wait for command to complete */
335 start = get_timer(0);
336 do {
337 int ret;
338
339 mdelay(50); /* Insert some reasonable delay */
Simon Glassbed43522018-10-01 12:22:22 -0600340 ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
341 NULL, 0,
342 (uint8_t **)&resp, sizeof(*resp));
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800343 if (ret < 0)
344 return ret;
345
346 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
347 debug("%s: Command %#02x timeout\n",
348 __func__, cmd);
349 return -EC_RES_TIMEOUT;
350 }
351 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
352
353 /* OK it completed, so read the status response */
354 /* not sure why it was 0 for the last argument */
Simon Glassbed43522018-10-01 12:22:22 -0600355 len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
356 &din, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800357 }
358
Simon Glass0b8e8ce2017-05-18 20:09:22 -0600359 debug("%s: len=%d, din=%p\n", __func__, len, din);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800360 if (dinp) {
361 /* If we have any data to return, it must be 64bit-aligned */
362 assert(len <= 0 || !((uintptr_t)din & 7));
363 *dinp = din;
364 }
365
366 return len;
367}
368
369/**
370 * Send a command to the CROS-EC device and return the reply.
371 *
372 * The device's internal input/output buffers are used.
373 *
374 * @param dev CROS-EC device
375 * @param cmd Command to send (EC_CMD_...)
376 * @param cmd_version Version of command to send (EC_VER_...)
377 * @param dout Output data (may be NULL If dout_len=0)
378 * @param dout_len Size of output data in bytes
379 * @param din Response data (may be NULL If din_len=0).
380 * It not NULL, it is a place for ec_command() to copy the
381 * data to.
382 * @param din_len Maximum size of response in bytes
Simon Glassd3d9c062015-01-25 08:27:17 -0700383 * @return number of bytes in response, or -ve on error
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800384 */
Simon Glass3a239512018-11-06 15:21:18 -0700385static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800386 const void *dout, int dout_len,
387 void *din, int din_len)
388{
389 uint8_t *in_buffer;
390 int len;
391
392 assert((din_len == 0) || din);
393 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
Simon Glassbed43522018-10-01 12:22:22 -0600394 &in_buffer, din_len);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800395 if (len > 0) {
396 /*
397 * If we were asked to put it somewhere, do so, otherwise just
398 * disregard the result.
399 */
400 if (din && in_buffer) {
401 assert(len <= din_len);
402 memmove(din, in_buffer, len);
403 }
404 }
405 return len;
406}
407
Simon Glassfb726402015-10-18 21:17:14 -0600408int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800409{
Simon Glassbed43522018-10-01 12:22:22 -0600410 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
Simon Glass19cc2952014-02-27 13:26:11 -0700411 sizeof(scan->data)) != sizeof(scan->data))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800412 return -1;
413
414 return 0;
415}
416
Simon Glassbed43522018-10-01 12:22:22 -0600417int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800418{
419 struct ec_response_get_version *r;
Simon Glassa77024d2018-11-06 15:21:19 -0700420 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800421
Simon Glassa77024d2018-11-06 15:21:19 -0700422 ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
423 (uint8_t **)&r, sizeof(*r));
424 if (ret != sizeof(*r)) {
Simon Glass48d268f2018-11-23 21:29:36 -0700425 log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800426 return -1;
Simon Glassa77024d2018-11-06 15:21:19 -0700427 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800428
Simon Glass19cc2952014-02-27 13:26:11 -0700429 if (maxlen > (int)sizeof(r->version_string_ro))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800430 maxlen = sizeof(r->version_string_ro);
431
432 switch (r->current_image) {
433 case EC_IMAGE_RO:
434 memcpy(id, r->version_string_ro, maxlen);
435 break;
436 case EC_IMAGE_RW:
437 memcpy(id, r->version_string_rw, maxlen);
438 break;
439 default:
Simon Glassa77024d2018-11-06 15:21:19 -0700440 log_err("Invalid EC image %d\n", r->current_image);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800441 return -1;
442 }
443
444 id[maxlen - 1] = '\0';
445 return 0;
446}
447
Simon Glassbed43522018-10-01 12:22:22 -0600448int cros_ec_read_version(struct udevice *dev,
449 struct ec_response_get_version **versionp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800450{
451 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
452 (uint8_t **)versionp, sizeof(**versionp))
Simon Glass19cc2952014-02-27 13:26:11 -0700453 != sizeof(**versionp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800454 return -1;
455
456 return 0;
457}
458
Simon Glassbed43522018-10-01 12:22:22 -0600459int cros_ec_read_build_info(struct udevice *dev, char **strp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800460{
461 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
Simon Glassece28862014-02-27 13:26:07 -0700462 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800463 return -1;
464
465 return 0;
466}
467
Simon Glassbed43522018-10-01 12:22:22 -0600468int cros_ec_read_current_image(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600469 enum ec_current_image *image)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800470{
471 struct ec_response_get_version *r;
472
473 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700474 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800475 return -1;
476
477 *image = r->current_image;
478 return 0;
479}
480
Simon Glassbed43522018-10-01 12:22:22 -0600481static int cros_ec_wait_on_hash_done(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600482 struct ec_response_vboot_hash *hash)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800483{
484 struct ec_params_vboot_hash p;
485 ulong start;
486
487 start = get_timer(0);
488 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
489 mdelay(50); /* Insert some reasonable delay */
490
491 p.cmd = EC_VBOOT_HASH_GET;
492 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
493 hash, sizeof(*hash)) < 0)
494 return -1;
495
496 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
497 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
498 return -EC_RES_TIMEOUT;
499 }
500 }
501 return 0;
502}
503
Simon Glass94564902018-10-01 12:22:38 -0600504int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
505 struct ec_response_vboot_hash *hash)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800506{
507 struct ec_params_vboot_hash p;
508 int rv;
509
510 p.cmd = EC_VBOOT_HASH_GET;
Simon Glass94564902018-10-01 12:22:38 -0600511 p.offset = hash_offset;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800512 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
513 hash, sizeof(*hash)) < 0)
514 return -1;
515
516 /* If the EC is busy calculating the hash, fidget until it's done. */
517 rv = cros_ec_wait_on_hash_done(dev, hash);
518 if (rv)
519 return rv;
520
521 /* If the hash is valid, we're done. Otherwise, we have to kick it off
522 * again and wait for it to complete. Note that we explicitly assume
523 * that hashing zero bytes is always wrong, even though that would
524 * produce a valid hash value. */
525 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
526 return 0;
527
528 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
529 __func__, hash->status, hash->size);
530
Simon Glassece28862014-02-27 13:26:07 -0700531 p.cmd = EC_VBOOT_HASH_START;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800532 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
533 p.nonce_size = 0;
Simon Glass94564902018-10-01 12:22:38 -0600534 p.offset = hash_offset;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800535
536 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
537 hash, sizeof(*hash)) < 0)
538 return -1;
539
540 rv = cros_ec_wait_on_hash_done(dev, hash);
541 if (rv)
542 return rv;
543
544 debug("%s: hash done\n", __func__);
545
546 return 0;
547}
548
Simon Glassbed43522018-10-01 12:22:22 -0600549static int cros_ec_invalidate_hash(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800550{
551 struct ec_params_vboot_hash p;
552 struct ec_response_vboot_hash *hash;
553
554 /* We don't have an explict command for the EC to discard its current
555 * hash value, so we'll just tell it to calculate one that we know is
556 * wrong (we claim that hashing zero bytes is always invalid).
557 */
558 p.cmd = EC_VBOOT_HASH_RECALC;
559 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
560 p.nonce_size = 0;
561 p.offset = 0;
562 p.size = 0;
563
564 debug("%s:\n", __func__);
565
566 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
567 (uint8_t **)&hash, sizeof(*hash)) < 0)
568 return -1;
569
570 /* No need to wait for it to finish */
571 return 0;
572}
573
Simon Glassbed43522018-10-01 12:22:22 -0600574int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800575{
576 struct ec_params_reboot_ec p;
577
578 p.cmd = cmd;
579 p.flags = flags;
580
581 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
582 < 0)
583 return -1;
584
585 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
586 /*
587 * EC reboot will take place immediately so delay to allow it
588 * to complete. Note that some reboot types (EC_REBOOT_COLD)
589 * will reboot the AP as well, in which case we won't actually
590 * get to this point.
591 */
592 /*
593 * TODO(rspangler@chromium.org): Would be nice if we had a
594 * better way to determine when the reboot is complete. Could
595 * we poll a memory-mapped LPC value?
596 */
597 udelay(50000);
598 }
599
600 return 0;
601}
602
Simon Glassfb726402015-10-18 21:17:14 -0600603int cros_ec_interrupt_pending(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800604{
Simon Glassfb726402015-10-18 21:17:14 -0600605 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
606
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800607 /* no interrupt support : always poll */
Simon Glassfb726402015-10-18 21:17:14 -0600608 if (!dm_gpio_is_valid(&cdev->ec_int))
Simon Glass19cc2952014-02-27 13:26:11 -0700609 return -ENOENT;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800610
Simon Glassfb726402015-10-18 21:17:14 -0600611 return dm_gpio_get_value(&cdev->ec_int);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800612}
613
Simon Glassbed43522018-10-01 12:22:22 -0600614int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800615{
Simon Glassece28862014-02-27 13:26:07 -0700616 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
Simon Glass19cc2952014-02-27 13:26:11 -0700617 sizeof(*info)) != sizeof(*info))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800618 return -1;
619
620 return 0;
621}
622
Simon Glass153f2522018-11-06 15:21:22 -0700623int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
624{
625 struct ec_response_host_event_mask rsp;
626 int ret;
627
628 ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
629 if (ret < 0)
630 return ret;
631 else if (ret != sizeof(rsp))
632 return -EINVAL;
633
634 *mask = rsp.mask;
635
636 return 0;
637}
638
639int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
640{
641 struct ec_params_host_event_mask req;
642 int ret;
643
644 req.mask = mask;
645
646 ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
647 if (ret < 0)
648 return ret;
649
650 return 0;
651}
652
Simon Glassbed43522018-10-01 12:22:22 -0600653int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800654{
655 struct ec_response_host_event_mask *resp;
656
657 /*
658 * Use the B copy of the event flags, because the main copy is already
659 * used by ACPI/SMI.
660 */
661 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
Simon Glass19cc2952014-02-27 13:26:11 -0700662 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800663 return -1;
664
665 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
666 return -1;
667
668 *events_ptr = resp->mask;
669 return 0;
670}
671
Simon Glassbed43522018-10-01 12:22:22 -0600672int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800673{
674 struct ec_params_host_event_mask params;
675
676 params.mask = events;
677
678 /*
679 * Use the B copy of the event flags, so it affects the data returned
680 * by cros_ec_get_host_events().
681 */
682 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
683 &params, sizeof(params), NULL, 0) < 0)
684 return -1;
685
686 return 0;
687}
688
Simon Glassbed43522018-10-01 12:22:22 -0600689int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
690 uint32_t set_flags,
Simon Glass699c9ca2018-10-01 12:22:08 -0600691 struct ec_response_flash_protect *resp)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800692{
693 struct ec_params_flash_protect params;
694
695 params.mask = set_mask;
696 params.flags = set_flags;
697
698 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
699 &params, sizeof(params),
Simon Glass19cc2952014-02-27 13:26:11 -0700700 resp, sizeof(*resp)) != sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800701 return -1;
702
703 return 0;
704}
705
Simon Glass153f2522018-11-06 15:21:22 -0700706int cros_ec_entering_mode(struct udevice *dev, int mode)
707{
708 int rc;
709
710 rc = ec_command(dev, EC_CMD_ENTERING_MODE, 0, &mode, sizeof(mode),
711 NULL, 0);
712 if (rc)
713 return -1;
714 return 0;
715}
716
Simon Glassbed43522018-10-01 12:22:22 -0600717static int cros_ec_check_version(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800718{
Simon Glassbed43522018-10-01 12:22:22 -0600719 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800720 struct ec_params_hello req;
721 struct ec_response_hello *resp;
722
Simon Glass23fb1562015-03-26 09:29:30 -0600723 struct dm_cros_ec_ops *ops;
724 int ret;
725
Simon Glassbed43522018-10-01 12:22:22 -0600726 ops = dm_cros_ec_get_ops(dev);
Simon Glass23fb1562015-03-26 09:29:30 -0600727 if (ops->check_version) {
Simon Glassbed43522018-10-01 12:22:22 -0600728 ret = ops->check_version(dev);
Simon Glass23fb1562015-03-26 09:29:30 -0600729 if (ret)
730 return ret;
731 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800732
733 /*
734 * TODO(sjg@chromium.org).
735 * There is a strange oddity here with the EC. We could just ignore
736 * the response, i.e. pass the last two parameters as NULL and 0.
737 * In this case we won't read back very many bytes from the EC.
738 * On the I2C bus the EC gets upset about this and will try to send
739 * the bytes anyway. This means that we will have to wait for that
740 * to complete before continuing with a new EC command.
741 *
742 * This problem is probably unique to the I2C bus.
743 *
744 * So for now, just read all the data anyway.
745 */
Randall Spanglerf03e9702014-02-27 13:26:08 -0700746
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700747 /* Try sending a version 3 packet */
Simon Glassbed43522018-10-01 12:22:22 -0600748 cdev->protocol_version = 3;
Simon Glass73c1ecf2014-11-11 18:06:30 -0700749 req.in_data = 0;
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700750 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
Simon Glass3a239512018-11-06 15:21:18 -0700751 (uint8_t **)&resp, sizeof(*resp)) > 0)
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700752 return 0;
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700753
Randall Spanglerf03e9702014-02-27 13:26:08 -0700754 /* Try sending a version 2 packet */
Simon Glassbed43522018-10-01 12:22:22 -0600755 cdev->protocol_version = 2;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800756 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
Simon Glass3a239512018-11-06 15:21:18 -0700757 (uint8_t **)&resp, sizeof(*resp)) > 0)
Randall Spanglerf03e9702014-02-27 13:26:08 -0700758 return 0;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800759
Randall Spanglerf03e9702014-02-27 13:26:08 -0700760 /*
761 * Fail if we're still here, since the EC doesn't understand any
762 * protcol version we speak. Version 1 interface without command
763 * version is no longer supported, and we don't know about any new
764 * protocol versions.
765 */
Simon Glassbed43522018-10-01 12:22:22 -0600766 cdev->protocol_version = 0;
Randall Spanglerf03e9702014-02-27 13:26:08 -0700767 printf("%s: ERROR: old EC interface not supported\n", __func__);
768 return -1;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800769}
770
Simon Glassbed43522018-10-01 12:22:22 -0600771int cros_ec_test(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800772{
773 struct ec_params_hello req;
774 struct ec_response_hello *resp;
775
776 req.in_data = 0x12345678;
777 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
778 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
779 printf("ec_command_inptr() returned error\n");
780 return -1;
781 }
782 if (resp->out_data != req.in_data + 0x01020304) {
783 printf("Received invalid handshake %x\n", resp->out_data);
784 return -1;
785 }
786
787 return 0;
788}
789
Simon Glassbed43522018-10-01 12:22:22 -0600790int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800791 uint32_t *offset, uint32_t *size)
792{
793 struct ec_params_flash_region_info p;
794 struct ec_response_flash_region_info *r;
795 int ret;
796
797 p.region = region;
798 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
799 EC_VER_FLASH_REGION_INFO,
800 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
801 if (ret != sizeof(*r))
802 return -1;
803
804 if (offset)
805 *offset = r->offset;
806 if (size)
807 *size = r->size;
808
809 return 0;
810}
811
Simon Glassbed43522018-10-01 12:22:22 -0600812int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800813{
814 struct ec_params_flash_erase p;
815
816 p.offset = offset;
817 p.size = size;
818 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
819 NULL, 0);
820}
821
822/**
823 * Write a single block to the flash
824 *
825 * Write a block of data to the EC flash. The size must not exceed the flash
826 * write block size which you can obtain from cros_ec_flash_write_burst_size().
827 *
828 * The offset starts at 0. You can obtain the region information from
829 * cros_ec_flash_offset() to find out where to write for a particular region.
830 *
831 * Attempting to write to the region where the EC is currently running from
832 * will result in an error.
833 *
834 * @param dev CROS-EC device
835 * @param data Pointer to data buffer to write
836 * @param offset Offset within flash to write to.
837 * @param size Number of bytes to write
838 * @return 0 if ok, -1 on error
839 */
Simon Glassbed43522018-10-01 12:22:22 -0600840static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
841 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800842{
Moritz Fischere597bc72016-09-12 12:57:52 -0700843 struct ec_params_flash_write *p;
844 int ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800845
Moritz Fischere597bc72016-09-12 12:57:52 -0700846 p = malloc(sizeof(*p) + size);
847 if (!p)
848 return -ENOMEM;
849
850 p->offset = offset;
851 p->size = size;
852 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
853 memcpy(p + 1, data, p->size);
854
855 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
856 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
857
858 free(p);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800859
Moritz Fischere597bc72016-09-12 12:57:52 -0700860 return ret;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800861}
862
863/**
864 * Return optimal flash write burst size
865 */
Simon Glassbed43522018-10-01 12:22:22 -0600866static int cros_ec_flash_write_burst_size(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800867{
Simon Glassece28862014-02-27 13:26:07 -0700868 return EC_FLASH_WRITE_VER0_SIZE;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800869}
870
871/**
872 * Check if a block of data is erased (all 0xff)
873 *
874 * This function is useful when dealing with flash, for checking whether a
875 * data block is erased and thus does not need to be programmed.
876 *
877 * @param data Pointer to data to check (must be word-aligned)
878 * @param size Number of bytes to check (must be word-aligned)
879 * @return 0 if erased, non-zero if any word is not erased
880 */
881static int cros_ec_data_is_erased(const uint32_t *data, int size)
882{
883 assert(!(size & 3));
884 size /= sizeof(uint32_t);
885 for (; size > 0; size -= 4, data++)
886 if (*data != -1U)
887 return 0;
888
889 return 1;
890}
891
Moritz Fischerde5bc892016-09-13 14:44:48 -0700892/**
893 * Read back flash parameters
894 *
895 * This function reads back parameters of the flash as reported by the EC
896 *
897 * @param dev Pointer to device
898 * @param info Pointer to output flash info struct
899 */
Simon Glassbed43522018-10-01 12:22:22 -0600900int cros_ec_read_flashinfo(struct udevice *dev,
Simon Glass699c9ca2018-10-01 12:22:08 -0600901 struct ec_response_flash_info *info)
Moritz Fischerde5bc892016-09-13 14:44:48 -0700902{
903 int ret;
904
905 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
906 NULL, 0, info, sizeof(*info));
907 if (ret < 0)
908 return ret;
909
910 return ret < sizeof(*info) ? -1 : 0;
911}
912
Simon Glassbed43522018-10-01 12:22:22 -0600913int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
Simon Glass699c9ca2018-10-01 12:22:08 -0600914 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800915{
Simon Glassbed43522018-10-01 12:22:22 -0600916 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800917 uint32_t burst = cros_ec_flash_write_burst_size(dev);
918 uint32_t end, off;
919 int ret;
920
Simon Glass411661d2018-11-06 15:21:20 -0700921 if (!burst)
922 return -EINVAL;
923
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800924 /*
925 * TODO: round up to the nearest multiple of write size. Can get away
926 * without that on link right now because its write size is 4 bytes.
927 */
928 end = offset + size;
929 for (off = offset; off < end; off += burst, data += burst) {
930 uint32_t todo;
931
932 /* If the data is empty, there is no point in programming it */
933 todo = min(end - off, burst);
Simon Glassbed43522018-10-01 12:22:22 -0600934 if (cdev->optimise_flash_write &&
Simon Glass699c9ca2018-10-01 12:22:08 -0600935 cros_ec_data_is_erased((uint32_t *)data, todo))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800936 continue;
937
938 ret = cros_ec_flash_write_block(dev, data, off, todo);
939 if (ret)
940 return ret;
941 }
942
943 return 0;
944}
945
946/**
Simon Glass153f2522018-11-06 15:21:22 -0700947 * Run verification on a slot
948 *
949 * @param me CrosEc instance
950 * @param region Region to run verification on
951 * @return 0 if success or not applicable. Non-zero if verification failed.
952 */
953int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
954{
955 struct ec_params_efs_verify p;
956 int rv;
957
958 log_info("EFS: EC is verifying updated image...\n");
959 p.region = region;
960
961 rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
962 if (rv >= 0) {
963 log_info("EFS: Verification success\n");
964 return 0;
965 }
966 if (rv == -EC_RES_INVALID_COMMAND) {
967 log_info("EFS: EC doesn't support EFS_VERIFY command\n");
968 return 0;
969 }
970 log_info("EFS: Verification failed\n");
971
972 return rv;
973}
974
975/**
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800976 * Read a single block from the flash
977 *
978 * Read a block of data from the EC flash. The size must not exceed the flash
979 * write block size which you can obtain from cros_ec_flash_write_burst_size().
980 *
981 * The offset starts at 0. You can obtain the region information from
982 * cros_ec_flash_offset() to find out where to read for a particular region.
983 *
984 * @param dev CROS-EC device
985 * @param data Pointer to data buffer to read into
986 * @param offset Offset within flash to read from
987 * @param size Number of bytes to read
988 * @return 0 if ok, -1 on error
989 */
Simon Glassbed43522018-10-01 12:22:22 -0600990static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
Simon Glass699c9ca2018-10-01 12:22:08 -0600991 uint32_t offset, uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800992{
993 struct ec_params_flash_read p;
994
995 p.offset = offset;
996 p.size = size;
997
998 return ec_command(dev, EC_CMD_FLASH_READ, 0,
999 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
1000}
1001
Simon Glassbed43522018-10-01 12:22:22 -06001002int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
Simon Glass699c9ca2018-10-01 12:22:08 -06001003 uint32_t size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001004{
1005 uint32_t burst = cros_ec_flash_write_burst_size(dev);
1006 uint32_t end, off;
1007 int ret;
1008
1009 end = offset + size;
1010 for (off = offset; off < end; off += burst, data += burst) {
1011 ret = cros_ec_flash_read_block(dev, data, off,
1012 min(end - off, burst));
1013 if (ret)
1014 return ret;
1015 }
1016
1017 return 0;
1018}
1019
Simon Glassbed43522018-10-01 12:22:22 -06001020int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
Simon Glass699c9ca2018-10-01 12:22:08 -06001021 int image_size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001022{
1023 uint32_t rw_offset, rw_size;
1024 int ret;
1025
Simon Glass2c1e5502018-10-01 12:22:36 -06001026 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
1027 &rw_size))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001028 return -1;
Simon Glass19cc2952014-02-27 13:26:11 -07001029 if (image_size > (int)rw_size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001030 return -1;
1031
1032 /* Invalidate the existing hash, just in case the AP reboots
1033 * unexpectedly during the update. If that happened, the EC RW firmware
1034 * would be invalid, but the EC would still have the original hash.
1035 */
1036 ret = cros_ec_invalidate_hash(dev);
1037 if (ret)
1038 return ret;
1039
1040 /*
1041 * Erase the entire RW section, so that the EC doesn't see any garbage
1042 * past the new image if it's smaller than the current image.
1043 *
1044 * TODO: could optimize this to erase just the current image, since
1045 * presumably everything past that is 0xff's. But would still need to
1046 * round up to the nearest multiple of erase size.
1047 */
1048 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
1049 if (ret)
1050 return ret;
1051
1052 /* Write the image */
1053 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
1054 if (ret)
1055 return ret;
1056
1057 return 0;
1058}
1059
Simon Glassbed43522018-10-01 12:22:22 -06001060int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001061{
1062 struct ec_params_vbnvcontext p;
1063 int len;
1064
Simon Glass153f2522018-11-06 15:21:22 -07001065 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
Simon Glassbed43522018-10-01 12:22:22 -06001066 return -EINVAL;
1067
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001068 p.op = EC_VBNV_CONTEXT_OP_READ;
1069
1070 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
Simon Glass153f2522018-11-06 15:21:22 -07001071 &p, sizeof(uint32_t) + size, block, size);
1072 if (len != size) {
1073 log_err("Expected %d bytes, got %d\n", size, len);
Simon Glassbed43522018-10-01 12:22:22 -06001074 return -EIO;
Simon Glass153f2522018-11-06 15:21:22 -07001075 }
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001076
1077 return 0;
1078}
1079
Simon Glassbed43522018-10-01 12:22:22 -06001080int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001081{
1082 struct ec_params_vbnvcontext p;
1083 int len;
1084
Simon Glass153f2522018-11-06 15:21:22 -07001085 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
Simon Glassbed43522018-10-01 12:22:22 -06001086 return -EINVAL;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001087 p.op = EC_VBNV_CONTEXT_OP_WRITE;
Simon Glass153f2522018-11-06 15:21:22 -07001088 memcpy(p.block, block, size);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001089
1090 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
Simon Glass153f2522018-11-06 15:21:22 -07001091 &p, sizeof(uint32_t) + size, NULL, 0);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001092 if (len < 0)
1093 return -1;
1094
Simon Glass153f2522018-11-06 15:21:22 -07001095 return 0;
1096}
1097
1098int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
1099{
1100 struct ec_params_battery_cutoff p;
1101 int len;
1102
1103 p.flags = flags;
1104 len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
1105 NULL, 0);
1106
1107 if (len < 0)
1108 return -1;
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001109 return 0;
1110}
1111
Simon Glasseb2cc512015-08-03 08:19:24 -06001112int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001113{
1114 struct ec_params_ldo_set params;
1115
1116 params.index = index;
1117 params.state = state;
1118
Simon Glassbed43522018-10-01 12:22:22 -06001119 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
Simon Glasseb2cc512015-08-03 08:19:24 -06001120 NULL, 0))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001121 return -1;
1122
1123 return 0;
1124}
1125
Simon Glasseb2cc512015-08-03 08:19:24 -06001126int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001127{
1128 struct ec_params_ldo_get params;
1129 struct ec_response_ldo_get *resp;
1130
1131 params.index = index;
1132
Simon Glassbed43522018-10-01 12:22:22 -06001133 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
Simon Glasseb2cc512015-08-03 08:19:24 -06001134 (uint8_t **)&resp, sizeof(*resp)) !=
1135 sizeof(*resp))
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001136 return -1;
1137
1138 *state = resp->state;
1139
1140 return 0;
1141}
1142
Simon Glass44569b52014-10-13 23:42:14 -06001143int cros_ec_register(struct udevice *dev)
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001144{
Simon Glassde0977b2015-03-05 12:25:20 -07001145 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001146 char id[MSG_BYTES];
Simon Glass44569b52014-10-13 23:42:14 -06001147
1148 cdev->dev = dev;
Simon Glass0d7cc472015-01-05 20:05:32 -07001149 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1150 GPIOD_IS_IN);
Simon Glassb35e6d62017-05-18 20:09:23 -06001151 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
Simon Glass44569b52014-10-13 23:42:14 -06001152
Simon Glassbed43522018-10-01 12:22:22 -06001153 if (cros_ec_check_version(dev)) {
Simon Glass44569b52014-10-13 23:42:14 -06001154 debug("%s: Could not detect CROS-EC version\n", __func__);
1155 return -CROS_EC_ERR_CHECK_VERSION;
1156 }
1157
Simon Glassbed43522018-10-01 12:22:22 -06001158 if (cros_ec_read_id(dev, id, sizeof(id))) {
Simon Glass44569b52014-10-13 23:42:14 -06001159 debug("%s: Could not read KBC ID\n", __func__);
1160 return -CROS_EC_ERR_READ_ID;
1161 }
1162
1163 /* Remember this device for use by the cros_ec command */
Simon Glass46e17332015-02-17 15:29:36 -07001164 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1165 cdev->protocol_version, id);
Simon Glass44569b52014-10-13 23:42:14 -06001166
1167 return 0;
1168}
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08001169
Simon Glassb35e6d62017-05-18 20:09:23 -06001170int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
Simon Glass3c59abc2014-02-27 13:26:03 -07001171{
Simon Glassb35e6d62017-05-18 20:09:23 -06001172 ofnode flash_node, node;
Simon Glass3c59abc2014-02-27 13:26:03 -07001173
Simon Glassb35e6d62017-05-18 20:09:23 -06001174 flash_node = dev_read_subnode(dev, "flash");
1175 if (!ofnode_valid(flash_node)) {
Simon Glass3c59abc2014-02-27 13:26:03 -07001176 debug("Failed to find flash node\n");
1177 return -1;
1178 }
1179
Simon Glass4b580932018-06-11 13:07:17 -06001180 if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
Simon Glassb35e6d62017-05-18 20:09:23 -06001181 debug("Failed to decode flash node in chrome-ec\n");
Simon Glass3c59abc2014-02-27 13:26:03 -07001182 return -1;
1183 }
1184
Simon Glassb35e6d62017-05-18 20:09:23 -06001185 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1186 "erase-value", -1);
Simon Glass28529762017-08-05 15:45:54 -06001187 ofnode_for_each_subnode(node, flash_node) {
Simon Glassb35e6d62017-05-18 20:09:23 -06001188 const char *name = ofnode_get_name(node);
Simon Glass3c59abc2014-02-27 13:26:03 -07001189 enum ec_flash_region region;
1190
1191 if (0 == strcmp(name, "ro")) {
1192 region = EC_FLASH_REGION_RO;
1193 } else if (0 == strcmp(name, "rw")) {
Simon Glass2c1e5502018-10-01 12:22:36 -06001194 region = EC_FLASH_REGION_ACTIVE;
Simon Glass3c59abc2014-02-27 13:26:03 -07001195 } else if (0 == strcmp(name, "wp-ro")) {
1196 region = EC_FLASH_REGION_WP_RO;
1197 } else {
1198 debug("Unknown EC flash region name '%s'\n", name);
1199 return -1;
1200 }
1201
Simon Glass4b580932018-06-11 13:07:17 -06001202 if (ofnode_read_fmap_entry(node, &config->region[region])) {
Simon Glass3c59abc2014-02-27 13:26:03 -07001203 debug("Failed to decode flash region in chrome-ec'\n");
1204 return -1;
1205 }
1206 }
1207
1208 return 0;
1209}
1210
Moritz Fischer6cddc602016-09-27 15:42:07 -07001211int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1212 int nmsgs)
Simon Glass9ad07af2015-08-03 08:19:23 -06001213{
Simon Glass9ad07af2015-08-03 08:19:23 -06001214 union {
1215 struct ec_params_i2c_passthru p;
1216 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1217 } params;
1218 union {
1219 struct ec_response_i2c_passthru r;
1220 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1221 } response;
1222 struct ec_params_i2c_passthru *p = &params.p;
1223 struct ec_response_i2c_passthru *r = &response.r;
1224 struct ec_params_i2c_passthru_msg *msg;
1225 uint8_t *pdata, *read_ptr = NULL;
1226 int read_len;
1227 int size;
1228 int rv;
1229 int i;
1230
Moritz Fischer6cddc602016-09-27 15:42:07 -07001231 p->port = port;
Simon Glass9ad07af2015-08-03 08:19:23 -06001232
1233 p->num_msgs = nmsgs;
1234 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1235
1236 /* Create a message to write the register address and optional data */
1237 pdata = (uint8_t *)p + size;
1238
1239 read_len = 0;
1240 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1241 bool is_read = in->flags & I2C_M_RD;
1242
1243 msg->addr_flags = in->addr;
1244 msg->len = in->len;
1245 if (is_read) {
1246 msg->addr_flags |= EC_I2C_FLAG_READ;
1247 read_len += in->len;
1248 read_ptr = in->buf;
1249 if (sizeof(*r) + read_len > sizeof(response)) {
1250 puts("Read length too big for buffer\n");
1251 return -1;
1252 }
1253 } else {
1254 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1255 puts("Params too large for buffer\n");
1256 return -1;
1257 }
1258 memcpy(pdata, in->buf, in->len);
1259 pdata += in->len;
1260 }
1261 }
1262
Simon Glassbed43522018-10-01 12:22:22 -06001263 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
Simon Glass9ad07af2015-08-03 08:19:23 -06001264 r, sizeof(*r) + read_len);
1265 if (rv < 0)
1266 return rv;
1267
1268 /* Parse response */
1269 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1270 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1271 return -1;
1272 }
1273
1274 if (rv < sizeof(*r) + read_len) {
1275 puts("Truncated read response\n");
1276 return -1;
1277 }
1278
1279 /* We only support a single read message for each transfer */
1280 if (read_len)
1281 memcpy(read_ptr, r->data, read_len);
1282
Simon Glass153f2522018-11-06 15:21:22 -07001283 return 0;
1284}
1285
1286int cros_ec_check_feature(struct udevice *dev, int feature)
1287{
1288 struct ec_response_get_features r;
1289 int rv;
1290
1291 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0);
1292 if (rv)
1293 return rv;
1294
1295 if (feature >= 8 * sizeof(r.flags))
1296 return -1;
1297
1298 return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature);
1299}
1300
1301/*
1302 * Query the EC for specified mask indicating enabled events.
1303 * The EC maintains separate event masks for SMI, SCI and WAKE.
1304 */
1305static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
1306 uint64_t *value)
1307{
1308 int ret;
1309 struct ec_params_host_event req;
1310 struct ec_response_host_event rsp;
1311
1312 req.action = action;
1313 req.mask_type = mask;
1314 if (action != EC_HOST_EVENT_GET)
1315 req.value = *value;
1316 else
1317 *value = 0;
1318 ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
1319 sizeof(rsp));
1320
1321 if (action != EC_HOST_EVENT_GET)
1322 return ret;
1323 if (ret == 0)
1324 *value = rsp.value;
1325
1326 return ret;
1327}
1328
1329static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
1330 uint action, uint64_t *value)
1331{
1332 int ret = -1;
1333 struct ec_params_host_event_mask req;
1334 struct ec_response_host_event_mask rsp;
1335
1336 if (hcmd == INVALID_HCMD)
1337 return ret;
1338
1339 if (action != EC_HOST_EVENT_GET)
1340 req.mask = (uint32_t)*value;
1341 else
1342 *value = 0;
1343
1344 ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
1345 if (action != EC_HOST_EVENT_GET)
1346 return ret;
1347 if (ret == 0)
1348 *value = rsp.mask;
1349
1350 return ret;
1351}
1352
1353bool cros_ec_is_uhepi_supported(struct udevice *dev)
1354{
1355#define UHEPI_SUPPORTED 1
1356#define UHEPI_NOT_SUPPORTED 2
1357 static int uhepi_support;
1358
1359 if (!uhepi_support) {
1360 uhepi_support = cros_ec_check_feature(dev,
1361 EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
1362 UHEPI_NOT_SUPPORTED;
1363 log_debug("Chrome EC: UHEPI %s\n",
1364 uhepi_support == UHEPI_SUPPORTED ? "supported" :
1365 "not supported");
1366 }
1367 return uhepi_support == UHEPI_SUPPORTED;
1368}
1369
1370static int cros_ec_get_mask(struct udevice *dev, uint type)
1371{
1372 u64 value = 0;
1373
1374 if (cros_ec_is_uhepi_supported(dev)) {
1375 cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
1376 } else {
1377 assert(type < ARRAY_SIZE(event_map));
1378 cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
1379 EC_HOST_EVENT_GET, &value);
1380 }
1381 return value;
1382}
1383
1384static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
1385{
1386 if (cros_ec_is_uhepi_supported(dev))
1387 return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
1388
1389 assert(type < ARRAY_SIZE(event_map));
1390
1391 return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
1392 EC_HOST_EVENT_CLEAR, &mask);
1393}
1394
1395uint64_t cros_ec_get_events_b(struct udevice *dev)
1396{
1397 return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
1398}
1399
1400int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
1401{
1402 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
1403
1404 return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
1405}
1406
1407int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
1408{
1409 struct ec_params_charge_state p;
1410 struct ec_response_charge_state r;
1411 int ret;
1412
1413 p.cmd = CHARGE_STATE_CMD_GET_PARAM;
1414 p.get_param.param = CS_PARAM_LIMIT_POWER;
1415 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
1416 &r, sizeof(r));
1417
1418 /*
1419 * If our EC doesn't support the LIMIT_POWER parameter, assume that
1420 * LIMIT_POWER is not requested.
1421 */
1422 if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
1423 log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1424 return -ENOSYS;
1425 }
1426
1427 if (ret != sizeof(r.get_param))
1428 return -EINVAL;
1429
1430 *limit_powerp = r.get_param.value;
1431 return 0;
1432}
1433
1434int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
1435{
1436 struct ec_params_config_power_button params;
1437 int ret;
1438
1439 params.flags = flags;
1440 ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
1441 &params, sizeof(params), NULL, 0);
1442 if (ret < 0)
1443 return ret;
1444
1445 return 0;
1446}
1447
1448int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
1449{
1450 u32 mask;
1451 int ret;
1452
1453 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1454 &mask);
1455 if (ret < 0)
1456 return ret;
1457
1458 return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
1459}
1460
1461int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
1462{
1463 u32 mask;
1464 int ret;
1465
1466 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1467 &mask);
1468 if (ret < 0)
1469 return ret;
1470
Simon Glass48d268f2018-11-23 21:29:36 -07001471 /* Set lid close event state in the EC SMI event mask */
Simon Glass153f2522018-11-06 15:21:22 -07001472 if (enable)
1473 mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1474 else
1475 mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1476
1477 ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
1478 if (ret < 0)
1479 return ret;
1480
1481 printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
Simon Glass9ad07af2015-08-03 08:19:23 -06001482 return 0;
1483}
1484
Simon Glass44569b52014-10-13 23:42:14 -06001485UCLASS_DRIVER(cros_ec) = {
1486 .id = UCLASS_CROS_EC,
Simon Glassf18ca782019-05-02 10:52:11 -06001487 .name = "cros-ec",
Simon Glass44569b52014-10-13 23:42:14 -06001488 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
Simon Glass18230342016-07-05 17:10:10 -06001489 .post_bind = dm_scan_fdt_dev,
Simon Glass134022a2018-11-06 15:21:21 -07001490 .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,
Simon Glass44569b52014-10-13 23:42:14 -06001491};