blob: a5534b16673b5b0f29802a997bbcf3e1b76ed7fd [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hung-ying Tyan88364382013-05-15 18:27:28 +08002/*
3 * Chromium OS cros_ec driver
4 *
5 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyan88364382013-05-15 18:27:28 +08006 */
7
8/*
Simon Glass836bb6e2014-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 Tyan88364382013-05-15 18:27:28 +080014 */
15
Simon Glassac806522018-11-06 15:21:19 -070016#define LOG_CATEGORY UCLASS_CROS_EC
17
Hung-ying Tyan88364382013-05-15 18:27:28 +080018#include <common.h>
19#include <command.h>
Simon Glass84d6cbd2014-10-13 23:42:14 -060020#include <dm.h>
Simon Glassb79fdc72020-05-10 11:39:54 -060021#include <flash.h>
Hung-ying Tyan88364382013-05-15 18:27:28 +080022#include <i2c.h>
23#include <cros_ec.h>
24#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060025#include <log.h>
Hung-ying Tyan88364382013-05-15 18:27:28 +080026#include <malloc.h>
27#include <spi.h>
Simon Glassc05ed002020-05-10 11:40:11 -060028#include <linux/delay.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090029#include <linux/errno.h>
Hung-ying Tyan88364382013-05-15 18:27:28 +080030#include <asm/io.h>
31#include <asm-generic/gpio.h>
Simon Glass84d6cbd2014-10-13 23:42:14 -060032#include <dm/device-internal.h>
Simon Glass2ec9d172017-05-18 20:09:23 -060033#include <dm/of_extra.h>
Simon Glass84d6cbd2014-10-13 23:42:14 -060034#include <dm/uclass-internal.h>
Hung-ying Tyan88364382013-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,
47};
48
Simon Glass72ef8bf2018-11-06 15:21:22 -070049#define INVALID_HCMD 0xFF
50
51/*
52 * Map UHEPI masks to non UHEPI commands in order to support old EC FW
53 * which does not support UHEPI command.
54 */
55static const struct {
56 u8 set_cmd;
57 u8 clear_cmd;
58 u8 get_cmd;
59} event_map[] = {
60 [EC_HOST_EVENT_MAIN] = {
61 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
62 INVALID_HCMD,
63 },
64 [EC_HOST_EVENT_B] = {
65 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
66 EC_CMD_HOST_EVENT_GET_B,
67 },
68 [EC_HOST_EVENT_SCI_MASK] = {
69 EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
70 EC_CMD_HOST_EVENT_GET_SCI_MASK,
71 },
72 [EC_HOST_EVENT_SMI_MASK] = {
73 EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
74 EC_CMD_HOST_EVENT_GET_SMI_MASK,
75 },
76 [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
77 INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
78 },
79 [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
80 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
81 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
82 },
83 [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
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_S3] = {
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_S5] = {
92 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
93 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
94 },
95};
96
Hung-ying Tyan88364382013-05-15 18:27:28 +080097void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
98{
99#ifdef DEBUG
100 int i;
101
102 printf("%s: ", name);
103 if (cmd != -1)
104 printf("cmd=%#x: ", cmd);
105 for (i = 0; i < len; i++)
106 printf("%02x ", data[i]);
107 printf("\n");
108#endif
109}
110
111/*
112 * Calculate a simple 8-bit checksum of a data block
113 *
114 * @param data Data block to checksum
115 * @param size Size of data block in bytes
116 * @return checksum value (0 to 255)
117 */
118int cros_ec_calc_checksum(const uint8_t *data, int size)
119{
120 int csum, i;
121
122 for (i = csum = 0; i < size; i++)
123 csum += data[i];
124 return csum & 0xff;
125}
126
Simon Glass2d8ede52014-02-27 13:26:09 -0700127/**
128 * Create a request packet for protocol version 3.
129 *
130 * The packet is stored in the device's internal output buffer.
131 *
132 * @param dev CROS-EC device
133 * @param cmd Command to send (EC_CMD_...)
134 * @param cmd_version Version of command to send (EC_VER_...)
135 * @param dout Output data (may be NULL If dout_len=0)
136 * @param dout_len Size of output data in bytes
137 * @return packet size in bytes, or <0 if error.
138 */
Simon Glass6322a7b2018-10-01 12:22:22 -0600139static int create_proto3_request(struct cros_ec_dev *cdev,
Simon Glass2d8ede52014-02-27 13:26:09 -0700140 int cmd, int cmd_version,
141 const void *dout, int dout_len)
142{
Simon Glass6322a7b2018-10-01 12:22:22 -0600143 struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
Simon Glass2d8ede52014-02-27 13:26:09 -0700144 int out_bytes = dout_len + sizeof(*rq);
145
146 /* Fail if output size is too big */
Simon Glass6322a7b2018-10-01 12:22:22 -0600147 if (out_bytes > (int)sizeof(cdev->dout)) {
Simon Glass2d8ede52014-02-27 13:26:09 -0700148 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
149 return -EC_RES_REQUEST_TRUNCATED;
150 }
151
152 /* Fill in request packet */
153 rq->struct_version = EC_HOST_REQUEST_VERSION;
154 rq->checksum = 0;
155 rq->command = cmd;
156 rq->command_version = cmd_version;
157 rq->reserved = 0;
158 rq->data_len = dout_len;
159
160 /* Copy data after header */
161 memcpy(rq + 1, dout, dout_len);
162
163 /* Write checksum field so the entire packet sums to 0 */
Simon Glass6322a7b2018-10-01 12:22:22 -0600164 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
Simon Glass2d8ede52014-02-27 13:26:09 -0700165
Simon Glass6322a7b2018-10-01 12:22:22 -0600166 cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
Simon Glass2d8ede52014-02-27 13:26:09 -0700167
168 /* Return size of request packet */
169 return out_bytes;
170}
171
172/**
173 * Prepare the device to receive a protocol version 3 response.
174 *
175 * @param dev CROS-EC device
176 * @param din_len Maximum size of response in bytes
177 * @return maximum expected number of bytes in response, or <0 if error.
178 */
Simon Glass6322a7b2018-10-01 12:22:22 -0600179static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
Simon Glass2d8ede52014-02-27 13:26:09 -0700180{
181 int in_bytes = din_len + sizeof(struct ec_host_response);
182
183 /* Fail if input size is too big */
Simon Glass6322a7b2018-10-01 12:22:22 -0600184 if (in_bytes > (int)sizeof(cdev->din)) {
Simon Glass2d8ede52014-02-27 13:26:09 -0700185 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
186 return -EC_RES_RESPONSE_TOO_BIG;
187 }
188
189 /* Return expected size of response packet */
190 return in_bytes;
191}
192
193/**
194 * Handle a protocol version 3 response packet.
195 *
196 * The packet must already be stored in the device's internal input buffer.
197 *
198 * @param dev CROS-EC device
199 * @param dinp Returns pointer to response data
200 * @param din_len Maximum size of response in bytes
Simon Glass8bbb38b2015-01-25 08:27:17 -0700201 * @return number of bytes of response data, or <0 if error. Note that error
202 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
203 * overlap!)
Simon Glass2d8ede52014-02-27 13:26:09 -0700204 */
205static int handle_proto3_response(struct cros_ec_dev *dev,
206 uint8_t **dinp, int din_len)
207{
208 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
209 int in_bytes;
210 int csum;
211
212 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
213
214 /* Check input data */
215 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
216 debug("%s: EC response version mismatch\n", __func__);
217 return -EC_RES_INVALID_RESPONSE;
218 }
219
220 if (rs->reserved) {
221 debug("%s: EC response reserved != 0\n", __func__);
222 return -EC_RES_INVALID_RESPONSE;
223 }
224
225 if (rs->data_len > din_len) {
226 debug("%s: EC returned too much data\n", __func__);
227 return -EC_RES_RESPONSE_TOO_BIG;
228 }
229
230 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
231
232 /* Update in_bytes to actual data size */
233 in_bytes = sizeof(*rs) + rs->data_len;
234
235 /* Verify checksum */
236 csum = cros_ec_calc_checksum(dev->din, in_bytes);
237 if (csum) {
238 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
239 csum);
240 return -EC_RES_INVALID_CHECKSUM;
241 }
242
243 /* Return error result, if any */
244 if (rs->result)
245 return -(int)rs->result;
246
247 /* If we're still here, set response data pointer and return length */
248 *dinp = (uint8_t *)(rs + 1);
249
250 return rs->data_len;
251}
252
Simon Glass6322a7b2018-10-01 12:22:22 -0600253static int send_command_proto3(struct cros_ec_dev *cdev,
Simon Glass2d8ede52014-02-27 13:26:09 -0700254 int cmd, int cmd_version,
255 const void *dout, int dout_len,
256 uint8_t **dinp, int din_len)
257{
Simon Glass84d6cbd2014-10-13 23:42:14 -0600258 struct dm_cros_ec_ops *ops;
Simon Glass2d8ede52014-02-27 13:26:09 -0700259 int out_bytes, in_bytes;
260 int rv;
261
262 /* Create request packet */
Simon Glass6322a7b2018-10-01 12:22:22 -0600263 out_bytes = create_proto3_request(cdev, cmd, cmd_version,
Simon Glass2d8ede52014-02-27 13:26:09 -0700264 dout, dout_len);
265 if (out_bytes < 0)
266 return out_bytes;
267
268 /* Prepare response buffer */
Simon Glass6322a7b2018-10-01 12:22:22 -0600269 in_bytes = prepare_proto3_response_buffer(cdev, din_len);
Simon Glass2d8ede52014-02-27 13:26:09 -0700270 if (in_bytes < 0)
271 return in_bytes;
272
Simon Glass6322a7b2018-10-01 12:22:22 -0600273 ops = dm_cros_ec_get_ops(cdev->dev);
274 rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
275 -ENOSYS;
Simon Glass2d8ede52014-02-27 13:26:09 -0700276 if (rv < 0)
277 return rv;
278
279 /* Process the response */
Simon Glass6322a7b2018-10-01 12:22:22 -0600280 return handle_proto3_response(cdev, dinp, din_len);
Simon Glass2d8ede52014-02-27 13:26:09 -0700281}
282
Simon Glass9fea76f2018-11-06 15:21:18 -0700283static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
Hung-ying Tyan88364382013-05-15 18:27:28 +0800284 const void *dout, int dout_len,
285 uint8_t **dinp, int din_len)
286{
Simon Glass84d6cbd2014-10-13 23:42:14 -0600287 struct dm_cros_ec_ops *ops;
Simon Glass2d8ede52014-02-27 13:26:09 -0700288 int ret = -1;
289
290 /* Handle protocol version 3 support */
291 if (dev->protocol_version == 3) {
292 return send_command_proto3(dev, cmd, cmd_version,
293 dout, dout_len, dinp, din_len);
294 }
Hung-ying Tyan88364382013-05-15 18:27:28 +0800295
Simon Glass84d6cbd2014-10-13 23:42:14 -0600296 ops = dm_cros_ec_get_ops(dev->dev);
297 ret = ops->command(dev->dev, cmd, cmd_version,
298 (const uint8_t *)dout, dout_len, dinp, din_len);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800299
300 return ret;
301}
302
303/**
304 * Send a command to the CROS-EC device and return the reply.
305 *
306 * The device's internal input/output buffers are used.
307 *
308 * @param dev CROS-EC device
309 * @param cmd Command to send (EC_CMD_...)
310 * @param cmd_version Version of command to send (EC_VER_...)
311 * @param dout Output data (may be NULL If dout_len=0)
312 * @param dout_len Size of output data in bytes
313 * @param dinp Response data (may be NULL If din_len=0).
314 * If not NULL, it will be updated to point to the data
315 * and will always be double word aligned (64-bits)
316 * @param din_len Maximum size of response in bytes
Simon Glass8bbb38b2015-01-25 08:27:17 -0700317 * @return number of bytes in response, or -ve on error
Hung-ying Tyan88364382013-05-15 18:27:28 +0800318 */
Michael Auchterb4f98b32019-12-09 20:27:31 +0000319static int ec_command_inptr(struct udevice *dev, uint cmd,
Simon Glasse6c5c942018-10-01 12:22:08 -0600320 int cmd_version, const void *dout, int dout_len,
321 uint8_t **dinp, int din_len)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800322{
Simon Glass6322a7b2018-10-01 12:22:22 -0600323 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Simon Glass2ab83f02014-02-27 13:26:11 -0700324 uint8_t *din = NULL;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800325 int len;
326
Simon Glass6322a7b2018-10-01 12:22:22 -0600327 len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
328 din_len);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800329
330 /* If the command doesn't complete, wait a while */
331 if (len == -EC_RES_IN_PROGRESS) {
Simon Glass2ab83f02014-02-27 13:26:11 -0700332 struct ec_response_get_comms_status *resp = NULL;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800333 ulong start;
334
335 /* Wait for command to complete */
336 start = get_timer(0);
337 do {
338 int ret;
339
340 mdelay(50); /* Insert some reasonable delay */
Simon Glass6322a7b2018-10-01 12:22:22 -0600341 ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
342 NULL, 0,
343 (uint8_t **)&resp, sizeof(*resp));
Hung-ying Tyan88364382013-05-15 18:27:28 +0800344 if (ret < 0)
345 return ret;
346
347 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
348 debug("%s: Command %#02x timeout\n",
349 __func__, cmd);
350 return -EC_RES_TIMEOUT;
351 }
352 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
353
354 /* OK it completed, so read the status response */
355 /* not sure why it was 0 for the last argument */
Simon Glass6322a7b2018-10-01 12:22:22 -0600356 len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
357 &din, din_len);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800358 }
359
Simon Glasse907bf22017-05-18 20:09:22 -0600360 debug("%s: len=%d, din=%p\n", __func__, len, din);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800361 if (dinp) {
362 /* If we have any data to return, it must be 64bit-aligned */
363 assert(len <= 0 || !((uintptr_t)din & 7));
364 *dinp = din;
365 }
366
367 return len;
368}
369
370/**
371 * Send a command to the CROS-EC device and return the reply.
372 *
373 * The device's internal input/output buffers are used.
374 *
375 * @param dev CROS-EC device
376 * @param cmd Command to send (EC_CMD_...)
377 * @param cmd_version Version of command to send (EC_VER_...)
378 * @param dout Output data (may be NULL If dout_len=0)
379 * @param dout_len Size of output data in bytes
380 * @param din Response data (may be NULL If din_len=0).
381 * It not NULL, it is a place for ec_command() to copy the
382 * data to.
383 * @param din_len Maximum size of response in bytes
Simon Glass8bbb38b2015-01-25 08:27:17 -0700384 * @return number of bytes in response, or -ve on error
Hung-ying Tyan88364382013-05-15 18:27:28 +0800385 */
Simon Glass9fea76f2018-11-06 15:21:18 -0700386static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
Hung-ying Tyan88364382013-05-15 18:27:28 +0800387 const void *dout, int dout_len,
388 void *din, int din_len)
389{
390 uint8_t *in_buffer;
391 int len;
392
393 assert((din_len == 0) || din);
394 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
Simon Glass6322a7b2018-10-01 12:22:22 -0600395 &in_buffer, din_len);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800396 if (len > 0) {
397 /*
398 * If we were asked to put it somewhere, do so, otherwise just
399 * disregard the result.
400 */
401 if (din && in_buffer) {
402 assert(len <= din_len);
403 memmove(din, in_buffer, len);
404 }
405 }
406 return len;
407}
408
Simon Glass745009c2015-10-18 21:17:14 -0600409int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800410{
Simon Glass6322a7b2018-10-01 12:22:22 -0600411 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
Simon Glass2ab83f02014-02-27 13:26:11 -0700412 sizeof(scan->data)) != sizeof(scan->data))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800413 return -1;
414
415 return 0;
416}
417
Simon Glass6322a7b2018-10-01 12:22:22 -0600418int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800419{
420 struct ec_response_get_version *r;
Simon Glassac806522018-11-06 15:21:19 -0700421 int ret;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800422
Simon Glassac806522018-11-06 15:21:19 -0700423 ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
424 (uint8_t **)&r, sizeof(*r));
425 if (ret != sizeof(*r)) {
Simon Glassa749c092018-11-23 21:29:36 -0700426 log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
Hung-ying Tyan88364382013-05-15 18:27:28 +0800427 return -1;
Simon Glassac806522018-11-06 15:21:19 -0700428 }
Hung-ying Tyan88364382013-05-15 18:27:28 +0800429
Simon Glass2ab83f02014-02-27 13:26:11 -0700430 if (maxlen > (int)sizeof(r->version_string_ro))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800431 maxlen = sizeof(r->version_string_ro);
432
433 switch (r->current_image) {
434 case EC_IMAGE_RO:
435 memcpy(id, r->version_string_ro, maxlen);
436 break;
437 case EC_IMAGE_RW:
438 memcpy(id, r->version_string_rw, maxlen);
439 break;
440 default:
Simon Glassac806522018-11-06 15:21:19 -0700441 log_err("Invalid EC image %d\n", r->current_image);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800442 return -1;
443 }
444
445 id[maxlen - 1] = '\0';
446 return 0;
447}
448
Simon Glass6322a7b2018-10-01 12:22:22 -0600449int cros_ec_read_version(struct udevice *dev,
450 struct ec_response_get_version **versionp)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800451{
452 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
453 (uint8_t **)versionp, sizeof(**versionp))
Simon Glass2ab83f02014-02-27 13:26:11 -0700454 != sizeof(**versionp))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800455 return -1;
456
457 return 0;
458}
459
Simon Glass6322a7b2018-10-01 12:22:22 -0600460int cros_ec_read_build_info(struct udevice *dev, char **strp)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800461{
462 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
Simon Glass836bb6e2014-02-27 13:26:07 -0700463 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800464 return -1;
465
466 return 0;
467}
468
Simon Glass6322a7b2018-10-01 12:22:22 -0600469int cros_ec_read_current_image(struct udevice *dev,
Simon Glasse6c5c942018-10-01 12:22:08 -0600470 enum ec_current_image *image)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800471{
472 struct ec_response_get_version *r;
473
474 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
Simon Glass2ab83f02014-02-27 13:26:11 -0700475 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800476 return -1;
477
478 *image = r->current_image;
479 return 0;
480}
481
Simon Glass6322a7b2018-10-01 12:22:22 -0600482static int cros_ec_wait_on_hash_done(struct udevice *dev,
Simon Glasse6c5c942018-10-01 12:22:08 -0600483 struct ec_response_vboot_hash *hash)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800484{
485 struct ec_params_vboot_hash p;
486 ulong start;
487
488 start = get_timer(0);
489 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
490 mdelay(50); /* Insert some reasonable delay */
491
492 p.cmd = EC_VBOOT_HASH_GET;
493 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
494 hash, sizeof(*hash)) < 0)
495 return -1;
496
497 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
498 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
499 return -EC_RES_TIMEOUT;
500 }
501 }
502 return 0;
503}
504
Simon Glassa12ef7e2018-10-01 12:22:38 -0600505int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
506 struct ec_response_vboot_hash *hash)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800507{
508 struct ec_params_vboot_hash p;
509 int rv;
510
511 p.cmd = EC_VBOOT_HASH_GET;
Simon Glassa12ef7e2018-10-01 12:22:38 -0600512 p.offset = hash_offset;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800513 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
514 hash, sizeof(*hash)) < 0)
515 return -1;
516
517 /* If the EC is busy calculating the hash, fidget until it's done. */
518 rv = cros_ec_wait_on_hash_done(dev, hash);
519 if (rv)
520 return rv;
521
522 /* If the hash is valid, we're done. Otherwise, we have to kick it off
523 * again and wait for it to complete. Note that we explicitly assume
524 * that hashing zero bytes is always wrong, even though that would
525 * produce a valid hash value. */
526 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
527 return 0;
528
529 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
530 __func__, hash->status, hash->size);
531
Simon Glass836bb6e2014-02-27 13:26:07 -0700532 p.cmd = EC_VBOOT_HASH_START;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800533 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
534 p.nonce_size = 0;
Simon Glassa12ef7e2018-10-01 12:22:38 -0600535 p.offset = hash_offset;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800536
537 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
538 hash, sizeof(*hash)) < 0)
539 return -1;
540
541 rv = cros_ec_wait_on_hash_done(dev, hash);
542 if (rv)
543 return rv;
544
545 debug("%s: hash done\n", __func__);
546
547 return 0;
548}
549
Simon Glass6322a7b2018-10-01 12:22:22 -0600550static int cros_ec_invalidate_hash(struct udevice *dev)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800551{
552 struct ec_params_vboot_hash p;
553 struct ec_response_vboot_hash *hash;
554
555 /* We don't have an explict command for the EC to discard its current
556 * hash value, so we'll just tell it to calculate one that we know is
557 * wrong (we claim that hashing zero bytes is always invalid).
558 */
559 p.cmd = EC_VBOOT_HASH_RECALC;
560 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
561 p.nonce_size = 0;
562 p.offset = 0;
563 p.size = 0;
564
565 debug("%s:\n", __func__);
566
567 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
568 (uint8_t **)&hash, sizeof(*hash)) < 0)
569 return -1;
570
571 /* No need to wait for it to finish */
572 return 0;
573}
574
Simon Glass6322a7b2018-10-01 12:22:22 -0600575int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800576{
577 struct ec_params_reboot_ec p;
578
579 p.cmd = cmd;
580 p.flags = flags;
581
582 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
583 < 0)
584 return -1;
585
586 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
587 /*
588 * EC reboot will take place immediately so delay to allow it
589 * to complete. Note that some reboot types (EC_REBOOT_COLD)
590 * will reboot the AP as well, in which case we won't actually
591 * get to this point.
592 */
593 /*
594 * TODO(rspangler@chromium.org): Would be nice if we had a
595 * better way to determine when the reboot is complete. Could
596 * we poll a memory-mapped LPC value?
597 */
598 udelay(50000);
599 }
600
601 return 0;
602}
603
Simon Glass745009c2015-10-18 21:17:14 -0600604int cros_ec_interrupt_pending(struct udevice *dev)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800605{
Simon Glass745009c2015-10-18 21:17:14 -0600606 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
607
Hung-ying Tyan88364382013-05-15 18:27:28 +0800608 /* no interrupt support : always poll */
Simon Glass745009c2015-10-18 21:17:14 -0600609 if (!dm_gpio_is_valid(&cdev->ec_int))
Simon Glass2ab83f02014-02-27 13:26:11 -0700610 return -ENOENT;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800611
Simon Glass745009c2015-10-18 21:17:14 -0600612 return dm_gpio_get_value(&cdev->ec_int);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800613}
614
Simon Glass6322a7b2018-10-01 12:22:22 -0600615int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800616{
Simon Glass836bb6e2014-02-27 13:26:07 -0700617 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
Simon Glass2ab83f02014-02-27 13:26:11 -0700618 sizeof(*info)) != sizeof(*info))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800619 return -1;
620
621 return 0;
622}
623
Simon Glass72ef8bf2018-11-06 15:21:22 -0700624int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
625{
626 struct ec_response_host_event_mask rsp;
627 int ret;
628
629 ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
630 if (ret < 0)
631 return ret;
632 else if (ret != sizeof(rsp))
633 return -EINVAL;
634
635 *mask = rsp.mask;
636
637 return 0;
638}
639
640int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
641{
642 struct ec_params_host_event_mask req;
643 int ret;
644
645 req.mask = mask;
646
647 ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
648 if (ret < 0)
649 return ret;
650
651 return 0;
652}
653
Simon Glass6322a7b2018-10-01 12:22:22 -0600654int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800655{
656 struct ec_response_host_event_mask *resp;
657
658 /*
659 * Use the B copy of the event flags, because the main copy is already
660 * used by ACPI/SMI.
661 */
662 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
Simon Glass2ab83f02014-02-27 13:26:11 -0700663 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800664 return -1;
665
666 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
667 return -1;
668
669 *events_ptr = resp->mask;
670 return 0;
671}
672
Simon Glass6322a7b2018-10-01 12:22:22 -0600673int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800674{
675 struct ec_params_host_event_mask params;
676
677 params.mask = events;
678
679 /*
680 * Use the B copy of the event flags, so it affects the data returned
681 * by cros_ec_get_host_events().
682 */
683 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
684 &params, sizeof(params), NULL, 0) < 0)
685 return -1;
686
687 return 0;
688}
689
Simon Glass6322a7b2018-10-01 12:22:22 -0600690int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
691 uint32_t set_flags,
Simon Glasse6c5c942018-10-01 12:22:08 -0600692 struct ec_response_flash_protect *resp)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800693{
694 struct ec_params_flash_protect params;
695
696 params.mask = set_mask;
697 params.flags = set_flags;
698
699 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
700 &params, sizeof(params),
Simon Glass2ab83f02014-02-27 13:26:11 -0700701 resp, sizeof(*resp)) != sizeof(*resp))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800702 return -1;
703
704 return 0;
705}
706
Simon Glass72ef8bf2018-11-06 15:21:22 -0700707int cros_ec_entering_mode(struct udevice *dev, int mode)
708{
709 int rc;
710
711 rc = ec_command(dev, EC_CMD_ENTERING_MODE, 0, &mode, sizeof(mode),
712 NULL, 0);
713 if (rc)
714 return -1;
715 return 0;
716}
717
Simon Glass6322a7b2018-10-01 12:22:22 -0600718static int cros_ec_check_version(struct udevice *dev)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800719{
Simon Glass6322a7b2018-10-01 12:22:22 -0600720 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800721 struct ec_params_hello req;
722 struct ec_response_hello *resp;
723
Simon Glass72a38e02015-03-26 09:29:30 -0600724 struct dm_cros_ec_ops *ops;
725 int ret;
726
Simon Glass6322a7b2018-10-01 12:22:22 -0600727 ops = dm_cros_ec_get_ops(dev);
Simon Glass72a38e02015-03-26 09:29:30 -0600728 if (ops->check_version) {
Simon Glass6322a7b2018-10-01 12:22:22 -0600729 ret = ops->check_version(dev);
Simon Glass72a38e02015-03-26 09:29:30 -0600730 if (ret)
731 return ret;
732 }
Hung-ying Tyan88364382013-05-15 18:27:28 +0800733
734 /*
735 * TODO(sjg@chromium.org).
736 * There is a strange oddity here with the EC. We could just ignore
737 * the response, i.e. pass the last two parameters as NULL and 0.
738 * In this case we won't read back very many bytes from the EC.
739 * On the I2C bus the EC gets upset about this and will try to send
740 * the bytes anyway. This means that we will have to wait for that
741 * to complete before continuing with a new EC command.
742 *
743 * This problem is probably unique to the I2C bus.
744 *
745 * So for now, just read all the data anyway.
746 */
Randall Spanglere8c12662014-02-27 13:26:08 -0700747
Randall Spanglera6070282014-02-27 13:26:10 -0700748 /* Try sending a version 3 packet */
Simon Glass6322a7b2018-10-01 12:22:22 -0600749 cdev->protocol_version = 3;
Simon Glassd11e8fd2014-11-11 18:06:30 -0700750 req.in_data = 0;
Randall Spanglera6070282014-02-27 13:26:10 -0700751 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
Simon Glass9fea76f2018-11-06 15:21:18 -0700752 (uint8_t **)&resp, sizeof(*resp)) > 0)
Randall Spanglera6070282014-02-27 13:26:10 -0700753 return 0;
Randall Spanglera6070282014-02-27 13:26:10 -0700754
Randall Spanglere8c12662014-02-27 13:26:08 -0700755 /* Try sending a version 2 packet */
Simon Glass6322a7b2018-10-01 12:22:22 -0600756 cdev->protocol_version = 2;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800757 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
Simon Glass9fea76f2018-11-06 15:21:18 -0700758 (uint8_t **)&resp, sizeof(*resp)) > 0)
Randall Spanglere8c12662014-02-27 13:26:08 -0700759 return 0;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800760
Randall Spanglere8c12662014-02-27 13:26:08 -0700761 /*
762 * Fail if we're still here, since the EC doesn't understand any
763 * protcol version we speak. Version 1 interface without command
764 * version is no longer supported, and we don't know about any new
765 * protocol versions.
766 */
Simon Glass6322a7b2018-10-01 12:22:22 -0600767 cdev->protocol_version = 0;
Randall Spanglere8c12662014-02-27 13:26:08 -0700768 printf("%s: ERROR: old EC interface not supported\n", __func__);
769 return -1;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800770}
771
Simon Glass6322a7b2018-10-01 12:22:22 -0600772int cros_ec_test(struct udevice *dev)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800773{
774 struct ec_params_hello req;
775 struct ec_response_hello *resp;
776
777 req.in_data = 0x12345678;
778 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
779 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
780 printf("ec_command_inptr() returned error\n");
781 return -1;
782 }
783 if (resp->out_data != req.in_data + 0x01020304) {
784 printf("Received invalid handshake %x\n", resp->out_data);
785 return -1;
786 }
787
788 return 0;
789}
790
Simon Glass6322a7b2018-10-01 12:22:22 -0600791int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
Hung-ying Tyan88364382013-05-15 18:27:28 +0800792 uint32_t *offset, uint32_t *size)
793{
794 struct ec_params_flash_region_info p;
795 struct ec_response_flash_region_info *r;
796 int ret;
797
798 p.region = region;
799 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
800 EC_VER_FLASH_REGION_INFO,
801 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
802 if (ret != sizeof(*r))
803 return -1;
804
805 if (offset)
806 *offset = r->offset;
807 if (size)
808 *size = r->size;
809
810 return 0;
811}
812
Simon Glass6322a7b2018-10-01 12:22:22 -0600813int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800814{
815 struct ec_params_flash_erase p;
816
817 p.offset = offset;
818 p.size = size;
819 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
820 NULL, 0);
821}
822
823/**
824 * Write a single block to the flash
825 *
826 * Write a block of data to the EC flash. The size must not exceed the flash
827 * write block size which you can obtain from cros_ec_flash_write_burst_size().
828 *
829 * The offset starts at 0. You can obtain the region information from
830 * cros_ec_flash_offset() to find out where to write for a particular region.
831 *
832 * Attempting to write to the region where the EC is currently running from
833 * will result in an error.
834 *
835 * @param dev CROS-EC device
836 * @param data Pointer to data buffer to write
837 * @param offset Offset within flash to write to.
838 * @param size Number of bytes to write
839 * @return 0 if ok, -1 on error
840 */
Simon Glass6322a7b2018-10-01 12:22:22 -0600841static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
842 uint32_t offset, uint32_t size)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800843{
Moritz Fischerbae5b972016-09-12 12:57:52 -0700844 struct ec_params_flash_write *p;
845 int ret;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800846
Moritz Fischerbae5b972016-09-12 12:57:52 -0700847 p = malloc(sizeof(*p) + size);
848 if (!p)
849 return -ENOMEM;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800850
Moritz Fischerbae5b972016-09-12 12:57:52 -0700851 p->offset = offset;
852 p->size = size;
853 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
854 memcpy(p + 1, data, p->size);
855
856 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
857 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
858
859 free(p);
860
861 return ret;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800862}
863
864/**
865 * Return optimal flash write burst size
866 */
Simon Glass6322a7b2018-10-01 12:22:22 -0600867static int cros_ec_flash_write_burst_size(struct udevice *dev)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800868{
Simon Glass836bb6e2014-02-27 13:26:07 -0700869 return EC_FLASH_WRITE_VER0_SIZE;
Hung-ying Tyan88364382013-05-15 18:27:28 +0800870}
871
872/**
873 * Check if a block of data is erased (all 0xff)
874 *
875 * This function is useful when dealing with flash, for checking whether a
876 * data block is erased and thus does not need to be programmed.
877 *
878 * @param data Pointer to data to check (must be word-aligned)
879 * @param size Number of bytes to check (must be word-aligned)
880 * @return 0 if erased, non-zero if any word is not erased
881 */
882static int cros_ec_data_is_erased(const uint32_t *data, int size)
883{
884 assert(!(size & 3));
885 size /= sizeof(uint32_t);
886 for (; size > 0; size -= 4, data++)
887 if (*data != -1U)
888 return 0;
889
890 return 1;
891}
892
Moritz Fischer281ca882016-09-13 14:44:48 -0700893/**
894 * Read back flash parameters
895 *
896 * This function reads back parameters of the flash as reported by the EC
897 *
898 * @param dev Pointer to device
899 * @param info Pointer to output flash info struct
900 */
Simon Glass6322a7b2018-10-01 12:22:22 -0600901int cros_ec_read_flashinfo(struct udevice *dev,
Simon Glasse6c5c942018-10-01 12:22:08 -0600902 struct ec_response_flash_info *info)
Moritz Fischer281ca882016-09-13 14:44:48 -0700903{
904 int ret;
905
906 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
907 NULL, 0, info, sizeof(*info));
908 if (ret < 0)
909 return ret;
910
911 return ret < sizeof(*info) ? -1 : 0;
912}
913
Simon Glass6322a7b2018-10-01 12:22:22 -0600914int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
Simon Glasse6c5c942018-10-01 12:22:08 -0600915 uint32_t offset, uint32_t size)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800916{
Simon Glass6322a7b2018-10-01 12:22:22 -0600917 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Hung-ying Tyan88364382013-05-15 18:27:28 +0800918 uint32_t burst = cros_ec_flash_write_burst_size(dev);
919 uint32_t end, off;
920 int ret;
921
Simon Glassdc05ac02018-11-06 15:21:20 -0700922 if (!burst)
923 return -EINVAL;
924
Hung-ying Tyan88364382013-05-15 18:27:28 +0800925 /*
926 * TODO: round up to the nearest multiple of write size. Can get away
927 * without that on link right now because its write size is 4 bytes.
928 */
929 end = offset + size;
930 for (off = offset; off < end; off += burst, data += burst) {
931 uint32_t todo;
932
933 /* If the data is empty, there is no point in programming it */
934 todo = min(end - off, burst);
Simon Glass6322a7b2018-10-01 12:22:22 -0600935 if (cdev->optimise_flash_write &&
Simon Glasse6c5c942018-10-01 12:22:08 -0600936 cros_ec_data_is_erased((uint32_t *)data, todo))
Hung-ying Tyan88364382013-05-15 18:27:28 +0800937 continue;
938
939 ret = cros_ec_flash_write_block(dev, data, off, todo);
940 if (ret)
941 return ret;
942 }
943
944 return 0;
945}
946
947/**
Simon Glass72ef8bf2018-11-06 15:21:22 -0700948 * Run verification on a slot
949 *
950 * @param me CrosEc instance
951 * @param region Region to run verification on
952 * @return 0 if success or not applicable. Non-zero if verification failed.
953 */
954int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
955{
956 struct ec_params_efs_verify p;
957 int rv;
958
959 log_info("EFS: EC is verifying updated image...\n");
960 p.region = region;
961
962 rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
963 if (rv >= 0) {
964 log_info("EFS: Verification success\n");
965 return 0;
966 }
967 if (rv == -EC_RES_INVALID_COMMAND) {
968 log_info("EFS: EC doesn't support EFS_VERIFY command\n");
969 return 0;
970 }
971 log_info("EFS: Verification failed\n");
972
973 return rv;
974}
975
976/**
Hung-ying Tyan88364382013-05-15 18:27:28 +0800977 * Read a single block from the flash
978 *
979 * Read a block of data from the EC flash. The size must not exceed the flash
980 * write block size which you can obtain from cros_ec_flash_write_burst_size().
981 *
982 * The offset starts at 0. You can obtain the region information from
983 * cros_ec_flash_offset() to find out where to read for a particular region.
984 *
985 * @param dev CROS-EC device
986 * @param data Pointer to data buffer to read into
987 * @param offset Offset within flash to read from
988 * @param size Number of bytes to read
989 * @return 0 if ok, -1 on error
990 */
Simon Glass6322a7b2018-10-01 12:22:22 -0600991static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
Simon Glasse6c5c942018-10-01 12:22:08 -0600992 uint32_t offset, uint32_t size)
Hung-ying Tyan88364382013-05-15 18:27:28 +0800993{
994 struct ec_params_flash_read p;
995
996 p.offset = offset;
997 p.size = size;
998
999 return ec_command(dev, EC_CMD_FLASH_READ, 0,
1000 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
1001}
1002
Simon Glass6322a7b2018-10-01 12:22:22 -06001003int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
Simon Glasse6c5c942018-10-01 12:22:08 -06001004 uint32_t size)
Hung-ying Tyan88364382013-05-15 18:27:28 +08001005{
1006 uint32_t burst = cros_ec_flash_write_burst_size(dev);
1007 uint32_t end, off;
1008 int ret;
1009
1010 end = offset + size;
1011 for (off = offset; off < end; off += burst, data += burst) {
1012 ret = cros_ec_flash_read_block(dev, data, off,
1013 min(end - off, burst));
1014 if (ret)
1015 return ret;
1016 }
1017
1018 return 0;
1019}
1020
Simon Glass6322a7b2018-10-01 12:22:22 -06001021int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
Simon Glasse6c5c942018-10-01 12:22:08 -06001022 int image_size)
Hung-ying Tyan88364382013-05-15 18:27:28 +08001023{
1024 uint32_t rw_offset, rw_size;
1025 int ret;
1026
Simon Glass6f1c0432018-10-01 12:22:36 -06001027 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
1028 &rw_size))
Hung-ying Tyan88364382013-05-15 18:27:28 +08001029 return -1;
Simon Glass2ab83f02014-02-27 13:26:11 -07001030 if (image_size > (int)rw_size)
Hung-ying Tyan88364382013-05-15 18:27:28 +08001031 return -1;
1032
1033 /* Invalidate the existing hash, just in case the AP reboots
1034 * unexpectedly during the update. If that happened, the EC RW firmware
1035 * would be invalid, but the EC would still have the original hash.
1036 */
1037 ret = cros_ec_invalidate_hash(dev);
1038 if (ret)
1039 return ret;
1040
1041 /*
1042 * Erase the entire RW section, so that the EC doesn't see any garbage
1043 * past the new image if it's smaller than the current image.
1044 *
1045 * TODO: could optimize this to erase just the current image, since
1046 * presumably everything past that is 0xff's. But would still need to
1047 * round up to the nearest multiple of erase size.
1048 */
1049 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
1050 if (ret)
1051 return ret;
1052
1053 /* Write the image */
1054 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
1055 if (ret)
1056 return ret;
1057
1058 return 0;
1059}
1060
Simon Glass6322a7b2018-10-01 12:22:22 -06001061int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
Hung-ying Tyan88364382013-05-15 18:27:28 +08001062{
1063 struct ec_params_vbnvcontext p;
1064 int len;
1065
Simon Glass72ef8bf2018-11-06 15:21:22 -07001066 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
Simon Glass6322a7b2018-10-01 12:22:22 -06001067 return -EINVAL;
1068
Hung-ying Tyan88364382013-05-15 18:27:28 +08001069 p.op = EC_VBNV_CONTEXT_OP_READ;
1070
1071 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
Simon Glass72ef8bf2018-11-06 15:21:22 -07001072 &p, sizeof(uint32_t) + size, block, size);
1073 if (len != size) {
1074 log_err("Expected %d bytes, got %d\n", size, len);
Simon Glass6322a7b2018-10-01 12:22:22 -06001075 return -EIO;
Simon Glass72ef8bf2018-11-06 15:21:22 -07001076 }
Hung-ying Tyan88364382013-05-15 18:27:28 +08001077
1078 return 0;
1079}
1080
Simon Glass6322a7b2018-10-01 12:22:22 -06001081int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
Hung-ying Tyan88364382013-05-15 18:27:28 +08001082{
1083 struct ec_params_vbnvcontext p;
1084 int len;
1085
Simon Glass72ef8bf2018-11-06 15:21:22 -07001086 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
Simon Glass6322a7b2018-10-01 12:22:22 -06001087 return -EINVAL;
Hung-ying Tyan88364382013-05-15 18:27:28 +08001088 p.op = EC_VBNV_CONTEXT_OP_WRITE;
Simon Glass72ef8bf2018-11-06 15:21:22 -07001089 memcpy(p.block, block, size);
Hung-ying Tyan88364382013-05-15 18:27:28 +08001090
1091 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
Simon Glass72ef8bf2018-11-06 15:21:22 -07001092 &p, sizeof(uint32_t) + size, NULL, 0);
Hung-ying Tyan88364382013-05-15 18:27:28 +08001093 if (len < 0)
1094 return -1;
1095
1096 return 0;
1097}
1098
Simon Glass72ef8bf2018-11-06 15:21:22 -07001099int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
1100{
1101 struct ec_params_battery_cutoff p;
1102 int len;
1103
1104 p.flags = flags;
1105 len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
1106 NULL, 0);
1107
1108 if (len < 0)
1109 return -1;
1110 return 0;
1111}
1112
Simon Glassf48eaf02015-08-03 08:19:24 -06001113int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
Hung-ying Tyan88364382013-05-15 18:27:28 +08001114{
1115 struct ec_params_ldo_set params;
1116
1117 params.index = index;
1118 params.state = state;
1119
Simon Glass6322a7b2018-10-01 12:22:22 -06001120 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
Simon Glassf48eaf02015-08-03 08:19:24 -06001121 NULL, 0))
Hung-ying Tyan88364382013-05-15 18:27:28 +08001122 return -1;
1123
1124 return 0;
1125}
1126
Simon Glassf48eaf02015-08-03 08:19:24 -06001127int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
Hung-ying Tyan88364382013-05-15 18:27:28 +08001128{
1129 struct ec_params_ldo_get params;
1130 struct ec_response_ldo_get *resp;
1131
1132 params.index = index;
1133
Simon Glass6322a7b2018-10-01 12:22:22 -06001134 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
Simon Glassf48eaf02015-08-03 08:19:24 -06001135 (uint8_t **)&resp, sizeof(*resp)) !=
1136 sizeof(*resp))
Hung-ying Tyan88364382013-05-15 18:27:28 +08001137 return -1;
1138
1139 *state = resp->state;
1140
1141 return 0;
1142}
1143
Simon Glass84d6cbd2014-10-13 23:42:14 -06001144int cros_ec_register(struct udevice *dev)
1145{
Simon Glasse564f052015-03-05 12:25:20 -07001146 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Simon Glass84d6cbd2014-10-13 23:42:14 -06001147 char id[MSG_BYTES];
1148
1149 cdev->dev = dev;
Simon Glass32f8a192015-01-05 20:05:32 -07001150 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1151 GPIOD_IS_IN);
Simon Glass2ec9d172017-05-18 20:09:23 -06001152 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
Simon Glass84d6cbd2014-10-13 23:42:14 -06001153
Simon Glass6322a7b2018-10-01 12:22:22 -06001154 if (cros_ec_check_version(dev)) {
Simon Glass84d6cbd2014-10-13 23:42:14 -06001155 debug("%s: Could not detect CROS-EC version\n", __func__);
1156 return -CROS_EC_ERR_CHECK_VERSION;
1157 }
1158
Simon Glass6322a7b2018-10-01 12:22:22 -06001159 if (cros_ec_read_id(dev, id, sizeof(id))) {
Simon Glass84d6cbd2014-10-13 23:42:14 -06001160 debug("%s: Could not read KBC ID\n", __func__);
1161 return -CROS_EC_ERR_READ_ID;
1162 }
1163
1164 /* Remember this device for use by the cros_ec command */
Simon Glassc4b206d2015-02-17 15:29:36 -07001165 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1166 cdev->protocol_version, id);
Simon Glass84d6cbd2014-10-13 23:42:14 -06001167
1168 return 0;
1169}
Hung-ying Tyan88364382013-05-15 18:27:28 +08001170
Simon Glass2ec9d172017-05-18 20:09:23 -06001171int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
Simon Glassd7f25f32014-02-27 13:26:03 -07001172{
Simon Glass2ec9d172017-05-18 20:09:23 -06001173 ofnode flash_node, node;
Simon Glassd7f25f32014-02-27 13:26:03 -07001174
Simon Glass2ec9d172017-05-18 20:09:23 -06001175 flash_node = dev_read_subnode(dev, "flash");
1176 if (!ofnode_valid(flash_node)) {
Simon Glassd7f25f32014-02-27 13:26:03 -07001177 debug("Failed to find flash node\n");
1178 return -1;
1179 }
1180
Simon Glass5e0a7342018-06-11 13:07:17 -06001181 if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
Simon Glass2ec9d172017-05-18 20:09:23 -06001182 debug("Failed to decode flash node in chrome-ec\n");
Simon Glassd7f25f32014-02-27 13:26:03 -07001183 return -1;
1184 }
1185
Simon Glass2ec9d172017-05-18 20:09:23 -06001186 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1187 "erase-value", -1);
Simon Glass3991f422017-08-05 15:45:54 -06001188 ofnode_for_each_subnode(node, flash_node) {
Simon Glass2ec9d172017-05-18 20:09:23 -06001189 const char *name = ofnode_get_name(node);
Simon Glassd7f25f32014-02-27 13:26:03 -07001190 enum ec_flash_region region;
1191
1192 if (0 == strcmp(name, "ro")) {
1193 region = EC_FLASH_REGION_RO;
1194 } else if (0 == strcmp(name, "rw")) {
Simon Glass6f1c0432018-10-01 12:22:36 -06001195 region = EC_FLASH_REGION_ACTIVE;
Simon Glassd7f25f32014-02-27 13:26:03 -07001196 } else if (0 == strcmp(name, "wp-ro")) {
1197 region = EC_FLASH_REGION_WP_RO;
1198 } else {
1199 debug("Unknown EC flash region name '%s'\n", name);
1200 return -1;
1201 }
1202
Simon Glass5e0a7342018-06-11 13:07:17 -06001203 if (ofnode_read_fmap_entry(node, &config->region[region])) {
Simon Glassd7f25f32014-02-27 13:26:03 -07001204 debug("Failed to decode flash region in chrome-ec'\n");
1205 return -1;
1206 }
1207 }
1208
1209 return 0;
1210}
1211
Moritz Fischer6d1a7182016-09-27 15:42:07 -07001212int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1213 int nmsgs)
Simon Glasscc456bd2015-08-03 08:19:23 -06001214{
Simon Glasscc456bd2015-08-03 08:19:23 -06001215 union {
1216 struct ec_params_i2c_passthru p;
1217 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1218 } params;
1219 union {
1220 struct ec_response_i2c_passthru r;
1221 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1222 } response;
1223 struct ec_params_i2c_passthru *p = &params.p;
1224 struct ec_response_i2c_passthru *r = &response.r;
1225 struct ec_params_i2c_passthru_msg *msg;
1226 uint8_t *pdata, *read_ptr = NULL;
1227 int read_len;
1228 int size;
1229 int rv;
1230 int i;
1231
Moritz Fischer6d1a7182016-09-27 15:42:07 -07001232 p->port = port;
Simon Glasscc456bd2015-08-03 08:19:23 -06001233
1234 p->num_msgs = nmsgs;
1235 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1236
1237 /* Create a message to write the register address and optional data */
1238 pdata = (uint8_t *)p + size;
1239
1240 read_len = 0;
1241 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1242 bool is_read = in->flags & I2C_M_RD;
1243
1244 msg->addr_flags = in->addr;
1245 msg->len = in->len;
1246 if (is_read) {
1247 msg->addr_flags |= EC_I2C_FLAG_READ;
1248 read_len += in->len;
1249 read_ptr = in->buf;
1250 if (sizeof(*r) + read_len > sizeof(response)) {
1251 puts("Read length too big for buffer\n");
1252 return -1;
1253 }
1254 } else {
1255 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1256 puts("Params too large for buffer\n");
1257 return -1;
1258 }
1259 memcpy(pdata, in->buf, in->len);
1260 pdata += in->len;
1261 }
1262 }
1263
Simon Glass6322a7b2018-10-01 12:22:22 -06001264 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
Simon Glasscc456bd2015-08-03 08:19:23 -06001265 r, sizeof(*r) + read_len);
1266 if (rv < 0)
1267 return rv;
1268
1269 /* Parse response */
1270 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1271 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1272 return -1;
1273 }
1274
1275 if (rv < sizeof(*r) + read_len) {
1276 puts("Truncated read response\n");
1277 return -1;
1278 }
1279
1280 /* We only support a single read message for each transfer */
1281 if (read_len)
1282 memcpy(read_ptr, r->data, read_len);
1283
1284 return 0;
1285}
1286
Simon Glass72ef8bf2018-11-06 15:21:22 -07001287int cros_ec_check_feature(struct udevice *dev, int feature)
1288{
1289 struct ec_response_get_features r;
1290 int rv;
1291
1292 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0);
1293 if (rv)
1294 return rv;
1295
1296 if (feature >= 8 * sizeof(r.flags))
1297 return -1;
1298
1299 return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature);
1300}
1301
1302/*
1303 * Query the EC for specified mask indicating enabled events.
1304 * The EC maintains separate event masks for SMI, SCI and WAKE.
1305 */
1306static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
1307 uint64_t *value)
1308{
1309 int ret;
1310 struct ec_params_host_event req;
1311 struct ec_response_host_event rsp;
1312
1313 req.action = action;
1314 req.mask_type = mask;
1315 if (action != EC_HOST_EVENT_GET)
1316 req.value = *value;
1317 else
1318 *value = 0;
1319 ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
1320 sizeof(rsp));
1321
1322 if (action != EC_HOST_EVENT_GET)
1323 return ret;
1324 if (ret == 0)
1325 *value = rsp.value;
1326
1327 return ret;
1328}
1329
1330static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
1331 uint action, uint64_t *value)
1332{
1333 int ret = -1;
1334 struct ec_params_host_event_mask req;
1335 struct ec_response_host_event_mask rsp;
1336
1337 if (hcmd == INVALID_HCMD)
1338 return ret;
1339
1340 if (action != EC_HOST_EVENT_GET)
1341 req.mask = (uint32_t)*value;
1342 else
1343 *value = 0;
1344
1345 ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
1346 if (action != EC_HOST_EVENT_GET)
1347 return ret;
1348 if (ret == 0)
1349 *value = rsp.mask;
1350
1351 return ret;
1352}
1353
1354bool cros_ec_is_uhepi_supported(struct udevice *dev)
1355{
1356#define UHEPI_SUPPORTED 1
1357#define UHEPI_NOT_SUPPORTED 2
1358 static int uhepi_support;
1359
1360 if (!uhepi_support) {
1361 uhepi_support = cros_ec_check_feature(dev,
1362 EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
1363 UHEPI_NOT_SUPPORTED;
1364 log_debug("Chrome EC: UHEPI %s\n",
1365 uhepi_support == UHEPI_SUPPORTED ? "supported" :
1366 "not supported");
1367 }
1368 return uhepi_support == UHEPI_SUPPORTED;
1369}
1370
1371static int cros_ec_get_mask(struct udevice *dev, uint type)
1372{
1373 u64 value = 0;
1374
1375 if (cros_ec_is_uhepi_supported(dev)) {
1376 cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
1377 } else {
1378 assert(type < ARRAY_SIZE(event_map));
1379 cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
1380 EC_HOST_EVENT_GET, &value);
1381 }
1382 return value;
1383}
1384
1385static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
1386{
1387 if (cros_ec_is_uhepi_supported(dev))
1388 return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
1389
1390 assert(type < ARRAY_SIZE(event_map));
1391
1392 return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
1393 EC_HOST_EVENT_CLEAR, &mask);
1394}
1395
1396uint64_t cros_ec_get_events_b(struct udevice *dev)
1397{
1398 return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
1399}
1400
1401int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
1402{
1403 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
1404
1405 return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
1406}
1407
1408int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
1409{
1410 struct ec_params_charge_state p;
1411 struct ec_response_charge_state r;
1412 int ret;
1413
1414 p.cmd = CHARGE_STATE_CMD_GET_PARAM;
1415 p.get_param.param = CS_PARAM_LIMIT_POWER;
1416 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
1417 &r, sizeof(r));
1418
1419 /*
1420 * If our EC doesn't support the LIMIT_POWER parameter, assume that
1421 * LIMIT_POWER is not requested.
1422 */
1423 if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
1424 log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1425 return -ENOSYS;
1426 }
1427
1428 if (ret != sizeof(r.get_param))
1429 return -EINVAL;
1430
1431 *limit_powerp = r.get_param.value;
1432 return 0;
1433}
1434
1435int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
1436{
1437 struct ec_params_config_power_button params;
1438 int ret;
1439
1440 params.flags = flags;
1441 ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
1442 &params, sizeof(params), NULL, 0);
1443 if (ret < 0)
1444 return ret;
1445
1446 return 0;
1447}
1448
1449int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
1450{
1451 u32 mask;
1452 int ret;
1453
1454 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1455 &mask);
1456 if (ret < 0)
1457 return ret;
1458
1459 return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
1460}
1461
1462int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
1463{
1464 u32 mask;
1465 int ret;
1466
1467 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1468 &mask);
1469 if (ret < 0)
1470 return ret;
1471
Simon Glassa749c092018-11-23 21:29:36 -07001472 /* Set lid close event state in the EC SMI event mask */
Simon Glass72ef8bf2018-11-06 15:21:22 -07001473 if (enable)
1474 mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1475 else
1476 mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1477
1478 ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
1479 if (ret < 0)
1480 return ret;
1481
1482 printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
1483 return 0;
1484}
1485
Simon Glass84d6cbd2014-10-13 23:42:14 -06001486UCLASS_DRIVER(cros_ec) = {
1487 .id = UCLASS_CROS_EC,
Simon Glass16f4d052019-05-02 10:52:11 -06001488 .name = "cros-ec",
Simon Glass84d6cbd2014-10-13 23:42:14 -06001489 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
Simon Glass91195482016-07-05 17:10:10 -06001490 .post_bind = dm_scan_fdt_dev,
Simon Glass4bf6f2a2018-11-06 15:21:21 -07001491 .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,
Simon Glass84d6cbd2014-10-13 23:42:14 -06001492};