blob: 3ac690a3733695105290aca7c87235fe0d13dbd1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass36170f02014-02-27 13:26:12 -07002/*
3 * Chromium OS cros_ec driver - sandbox emulation
4 *
5 * Copyright (c) 2013 The Chromium OS Authors.
Simon Glass36170f02014-02-27 13:26:12 -07006 */
7
Simon Glass82930812021-05-13 19:39:26 -06008#define LOG_CATEGORY UCLASS_CROS_EC
9
Simon Glass36170f02014-02-27 13:26:12 -070010#include <cros_ec.h>
Simon Glassff425d82014-10-13 23:42:15 -060011#include <dm.h>
Simon Glass36170f02014-02-27 13:26:12 -070012#include <ec_commands.h>
13#include <errno.h>
14#include <hash.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass36170f02014-02-27 13:26:12 -070016#include <os.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020017#include <u-boot/sha256.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060018#include <time.h>
Simon Glassa4f416d2020-02-03 07:36:18 -070019#include <asm/malloc.h>
Simon Glass36170f02014-02-27 13:26:12 -070020#include <asm/state.h>
21#include <asm/sdl.h>
Simon Glass9b306e52021-01-16 14:52:22 -070022#include <asm/test.h>
Simon Glass36170f02014-02-27 13:26:12 -070023#include <linux/input.h>
24
25/*
26 * Ultimately it shold be possible to connect an Chrome OS EC emulation
27 * to U-Boot and remove all of this code. But this provides a test
28 * environment for bringing up chromeos_sandbox and demonstrating its
29 * utility.
30 *
31 * This emulation includes the following:
32 *
33 * 1. Emulation of the keyboard, by converting keypresses received from SDL
34 * into key scan data, passed back from the EC as key scan messages. The
35 * key layout is read from the device tree.
36 *
37 * 2. Emulation of vboot context - so this can be read/written as required.
38 *
39 * 3. Save/restore of EC state, so that the vboot context, flash memory
40 * contents and current image can be preserved across boots. This is important
41 * since the EC is supposed to continue running even if the AP resets.
42 *
43 * 4. Some event support, in particular allowing Escape to be pressed on boot
44 * to enter recovery mode. The EC passes this to U-Boot through the normal
45 * event message.
46 *
47 * 5. Flash read/write/erase support, so that software sync works. The
48 * protect messages are supported but no protection is implemented.
49 *
50 * 6. Hashing of the EC image, again to support software sync.
51 *
52 * Other features can be added, although a better path is probably to link
53 * the EC image in with U-Boot (Vic has demonstrated a prototype for this).
54 */
55
Simon Glass36170f02014-02-27 13:26:12 -070056#define KEYBOARD_ROWS 8
57#define KEYBOARD_COLS 13
58
59/* A single entry of the key matrix */
60struct ec_keymatrix_entry {
61 int row; /* key matrix row */
62 int col; /* key matrix column */
63 int keycode; /* corresponding linux key code */
64};
65
Simon Glass333e3a92021-01-16 14:52:31 -070066enum {
67 VSTORE_SLOT_COUNT = 4,
Alper Nebi Yasak8a8cd4f2021-05-19 19:33:31 +030068 PWM_CHANNEL_COUNT = 4,
Simon Glass333e3a92021-01-16 14:52:31 -070069};
70
71struct vstore_slot {
72 bool locked;
73 u8 data[EC_VSTORE_SLOT_SIZE];
74};
75
Alper Nebi Yasak8a8cd4f2021-05-19 19:33:31 +030076struct ec_pwm_channel {
77 uint duty; /* not ns, EC_PWM_MAX_DUTY = 100% */
78};
79
Simon Glass36170f02014-02-27 13:26:12 -070080/**
81 * struct ec_state - Information about the EC state
82 *
Simon Glass6f52b382023-09-26 08:14:48 -060083 * @valid: true if this struct contains valid state data
Simon Glass36170f02014-02-27 13:26:12 -070084 * @vbnv_context: Vboot context data stored by EC
85 * @ec_config: FDT config information about the EC (e.g. flashmap)
86 * @flash_data: Contents of flash memory
87 * @flash_data_len: Size of flash memory
88 * @current_image: Current image the EC is running
89 * @matrix_count: Number of keys to decode in matrix
90 * @matrix: Information about keyboard matrix
91 * @keyscan: Current keyscan information (bit set for each row/column pressed)
92 * @recovery_req: Keyboard recovery requested
Simon Glass9b306e52021-01-16 14:52:22 -070093 * @test_flags: Flags that control behaviour for tests
Simon Glass333e3a92021-01-16 14:52:31 -070094 * @slot_locked: Locked vstore slots (mask)
Alper Nebi Yasak8a8cd4f2021-05-19 19:33:31 +030095 * @pwm: Information per PWM channel
Simon Glass36170f02014-02-27 13:26:12 -070096 */
97struct ec_state {
Simon Glass6f52b382023-09-26 08:14:48 -060098 bool valid;
Simon Glass153f2522018-11-06 15:21:22 -070099 u8 vbnv_context[EC_VBNV_BLOCK_SIZE_V2];
Simon Glass36170f02014-02-27 13:26:12 -0700100 struct fdt_cros_ec ec_config;
101 uint8_t *flash_data;
102 int flash_data_len;
103 enum ec_current_image current_image;
104 int matrix_count;
105 struct ec_keymatrix_entry *matrix; /* the key matrix info */
106 uint8_t keyscan[KEYBOARD_COLS];
107 bool recovery_req;
Simon Glass9b306e52021-01-16 14:52:22 -0700108 uint test_flags;
Simon Glass333e3a92021-01-16 14:52:31 -0700109 struct vstore_slot slot[VSTORE_SLOT_COUNT];
Alper Nebi Yasak8a8cd4f2021-05-19 19:33:31 +0300110 struct ec_pwm_channel pwm[PWM_CHANNEL_COUNT];
Simon Glassff425d82014-10-13 23:42:15 -0600111} s_state, *g_state;
Simon Glass36170f02014-02-27 13:26:12 -0700112
113/**
114 * cros_ec_read_state() - read the sandbox EC state from the state file
115 *
116 * If data is available, then blob and node will provide access to it. If
117 * not this function sets up an empty EC.
118 *
119 * @param blob: Pointer to device tree blob, or NULL if no data to read
120 * @param node: Node offset to read from
121 */
122static int cros_ec_read_state(const void *blob, int node)
123{
124 struct ec_state *ec = &s_state;
125 const char *prop;
126 int len;
127
128 /* Set everything to defaults */
129 ec->current_image = EC_IMAGE_RO;
130 if (!blob)
131 return 0;
132
133 /* Read the data if available */
134 ec->current_image = fdtdec_get_int(blob, node, "current-image",
135 EC_IMAGE_RO);
136 prop = fdt_getprop(blob, node, "vbnv-context", &len);
137 if (prop && len == sizeof(ec->vbnv_context))
138 memcpy(ec->vbnv_context, prop, len);
139
140 prop = fdt_getprop(blob, node, "flash-data", &len);
141 if (prop) {
142 ec->flash_data_len = len;
Simon Glassa4f416d2020-02-03 07:36:18 -0700143 ec->flash_data = malloc(len);
Simon Glass36170f02014-02-27 13:26:12 -0700144 if (!ec->flash_data)
145 return -ENOMEM;
146 memcpy(ec->flash_data, prop, len);
147 debug("%s: Loaded EC flash data size %#x\n", __func__, len);
148 }
Simon Glass6f52b382023-09-26 08:14:48 -0600149 ec->valid = true;
Simon Glass36170f02014-02-27 13:26:12 -0700150
151 return 0;
152}
153
154/**
155 * cros_ec_write_state() - Write out our state to the state file
156 *
157 * The caller will ensure that there is a node ready for the state. The node
158 * may already contain the old state, in which case it is overridden.
159 *
160 * @param blob: Device tree blob holding state
161 * @param node: Node to write our state into
162 */
163static int cros_ec_write_state(void *blob, int node)
164{
Simon Glassff425d82014-10-13 23:42:15 -0600165 struct ec_state *ec = g_state;
Simon Glass36170f02014-02-27 13:26:12 -0700166
Simon Glassc747f8b2021-03-15 18:11:08 +1300167 if (!g_state)
168 return 0;
169
Simon Glass36170f02014-02-27 13:26:12 -0700170 /* We are guaranteed enough space to write basic properties */
171 fdt_setprop_u32(blob, node, "current-image", ec->current_image);
172 fdt_setprop(blob, node, "vbnv-context", ec->vbnv_context,
173 sizeof(ec->vbnv_context));
Simon Glassc747f8b2021-03-15 18:11:08 +1300174
Simon Glass36170f02014-02-27 13:26:12 -0700175 return state_setprop(node, "flash-data", ec->flash_data,
176 ec->ec_config.flash.length);
177}
178
179SANDBOX_STATE_IO(cros_ec, "google,cros-ec", cros_ec_read_state,
180 cros_ec_write_state);
181
182/**
183 * Return the number of bytes used in the specified image.
184 *
185 * This is the actual size of code+data in the image, as opposed to the
186 * amount of space reserved in flash for that image. This code is similar to
187 * that used by the real EC code base.
188 *
189 * @param ec Current emulated EC state
190 * @param entry Flash map entry containing the image to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100191 * Return: actual image size in bytes, 0 if the image contains no content or
Simon Glass36170f02014-02-27 13:26:12 -0700192 * error.
193 */
194static int get_image_used(struct ec_state *ec, struct fmap_entry *entry)
195{
196 int size;
197
198 /*
199 * Scan backwards looking for 0xea byte, which is by definition the
200 * last byte of the image. See ec.lds.S for how this is inserted at
201 * the end of the image.
202 */
203 for (size = entry->length - 1;
204 size > 0 && ec->flash_data[entry->offset + size] != 0xea;
205 size--)
206 ;
207
208 return size ? size + 1 : 0; /* 0xea byte IS part of the image */
209}
210
211/**
212 * Read the key matrix from the device tree
213 *
214 * Keymap entries in the fdt take the form of 0xRRCCKKKK where
215 * RR=Row CC=Column KKKK=Key Code
216 *
217 * @param ec Current emulated EC state
Simon Glass36170f02014-02-27 13:26:12 -0700218 * @param node Keyboard node of device tree containing keyscan information
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100219 * Return: 0 if ok, -1 on error
Simon Glass36170f02014-02-27 13:26:12 -0700220 */
Simon Glassb35e6d62017-05-18 20:09:23 -0600221static int keyscan_read_fdt_matrix(struct ec_state *ec, ofnode node)
Simon Glass36170f02014-02-27 13:26:12 -0700222{
223 const u32 *cell;
224 int upto;
225 int len;
226
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900227 cell = ofnode_get_property(node, "linux,keymap", &len);
Simon Glass82930812021-05-13 19:39:26 -0600228 if (!cell)
229 return log_msg_ret("prop", -EINVAL);
Simon Glass36170f02014-02-27 13:26:12 -0700230 ec->matrix_count = len / 4;
231 ec->matrix = calloc(ec->matrix_count, sizeof(*ec->matrix));
232 if (!ec->matrix) {
Simon Glass82930812021-05-13 19:39:26 -0600233 return log_msg_ret("mem", -ENOMEM);
Simon Glass36170f02014-02-27 13:26:12 -0700234 }
235
236 /* Now read the data */
237 for (upto = 0; upto < ec->matrix_count; upto++) {
238 struct ec_keymatrix_entry *matrix = &ec->matrix[upto];
239 u32 word;
240
241 word = fdt32_to_cpu(*cell++);
242 matrix->row = word >> 24;
243 matrix->col = (word >> 16) & 0xff;
244 matrix->keycode = word & 0xffff;
245
246 /* Hard-code some sanity limits for now */
247 if (matrix->row >= KEYBOARD_ROWS ||
248 matrix->col >= KEYBOARD_COLS) {
249 debug("%s: Matrix pos out of range (%d,%d)\n",
250 __func__, matrix->row, matrix->col);
Simon Glass82930812021-05-13 19:39:26 -0600251 return log_msg_ret("matrix", -ERANGE);
Simon Glass36170f02014-02-27 13:26:12 -0700252 }
253 }
254
255 if (upto != ec->matrix_count) {
Simon Glass82930812021-05-13 19:39:26 -0600256 return log_msg_ret("matrix", -E2BIG);
Simon Glass36170f02014-02-27 13:26:12 -0700257 }
258
259 return 0;
260}
261
262/**
263 * Return the next keyscan message contents
264 *
265 * @param ec Current emulated EC state
266 * @param scan Place to put keyscan bytes for the keyscan message (must hold
267 * enough space for a full keyscan)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100268 * Return: number of bytes of valid scan data
Simon Glass36170f02014-02-27 13:26:12 -0700269 */
270static int cros_ec_keyscan(struct ec_state *ec, uint8_t *scan)
271{
272 const struct ec_keymatrix_entry *matrix;
273 int bytes = KEYBOARD_COLS;
274 int key[8]; /* allow up to 8 keys to be pressed at once */
275 int count;
276 int i;
277
278 memset(ec->keyscan, '\0', bytes);
279 count = sandbox_sdl_scan_keys(key, ARRAY_SIZE(key));
280
281 /* Look up keycode in matrix */
282 for (i = 0, matrix = ec->matrix; i < ec->matrix_count; i++, matrix++) {
283 bool found;
284 int j;
285
286 for (found = false, j = 0; j < count; j++) {
287 if (matrix->keycode == key[j])
288 found = true;
289 }
290
291 if (found) {
292 debug("%d: %d,%d\n", matrix->keycode, matrix->row,
293 matrix->col);
294 ec->keyscan[matrix->col] |= 1 << matrix->row;
295 }
296 }
297
298 memcpy(scan, ec->keyscan, bytes);
299 return bytes;
300}
301
302/**
303 * Process an emulated EC command
304 *
305 * @param ec Current emulated EC state
306 * @param req_hdr Pointer to request header
307 * @param req_data Pointer to body of request
308 * @param resp_hdr Pointer to place to put response header
309 * @param resp_data Pointer to place to put response data, if any
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100310 * Return: length of response data, or 0 for no response data, or -1 on error
Simon Glass36170f02014-02-27 13:26:12 -0700311 */
312static int process_cmd(struct ec_state *ec,
313 struct ec_host_request *req_hdr, const void *req_data,
314 struct ec_host_response *resp_hdr, void *resp_data)
315{
316 int len;
317
318 /* TODO(sjg@chromium.org): Check checksums */
319 debug("EC command %#0x\n", req_hdr->command);
320
321 switch (req_hdr->command) {
322 case EC_CMD_HELLO: {
323 const struct ec_params_hello *req = req_data;
324 struct ec_response_hello *resp = resp_data;
325
326 resp->out_data = req->in_data + 0x01020304;
Simon Glass9b306e52021-01-16 14:52:22 -0700327 if (ec->test_flags & CROSECT_BREAK_HELLO)
328 resp->out_data++;
Simon Glass36170f02014-02-27 13:26:12 -0700329 len = sizeof(*resp);
330 break;
331 }
332 case EC_CMD_GET_VERSION: {
333 struct ec_response_get_version *resp = resp_data;
334
335 strcpy(resp->version_string_ro, "sandbox_ro");
336 strcpy(resp->version_string_rw, "sandbox_rw");
337 resp->current_image = ec->current_image;
338 debug("Current image %d\n", resp->current_image);
339 len = sizeof(*resp);
340 break;
341 }
342 case EC_CMD_VBNV_CONTEXT: {
343 const struct ec_params_vbnvcontext *req = req_data;
344 struct ec_response_vbnvcontext *resp = resp_data;
345
346 switch (req->op) {
347 case EC_VBNV_CONTEXT_OP_READ:
348 memcpy(resp->block, ec->vbnv_context,
Simon Glass01da40b2021-07-05 16:32:51 -0600349 EC_VBNV_BLOCK_SIZE_V2);
350 len = EC_VBNV_BLOCK_SIZE_V2;
Simon Glass36170f02014-02-27 13:26:12 -0700351 break;
352 case EC_VBNV_CONTEXT_OP_WRITE:
Simon Glassea2cf052018-11-23 21:29:37 -0700353 memcpy(ec->vbnv_context, req->block,
Simon Glass01da40b2021-07-05 16:32:51 -0600354 EC_VBNV_BLOCK_SIZE_V2);
Simon Glass36170f02014-02-27 13:26:12 -0700355 len = 0;
356 break;
357 default:
358 printf(" ** Unknown vbnv_context command %#02x\n",
359 req->op);
360 return -1;
361 }
362 break;
363 }
364 case EC_CMD_REBOOT_EC: {
365 const struct ec_params_reboot_ec *req = req_data;
366
367 printf("Request reboot type %d\n", req->cmd);
368 switch (req->cmd) {
369 case EC_REBOOT_DISABLE_JUMP:
370 len = 0;
371 break;
372 case EC_REBOOT_JUMP_RW:
373 ec->current_image = EC_IMAGE_RW;
374 len = 0;
375 break;
376 default:
377 puts(" ** Unknown type");
378 return -1;
379 }
380 break;
381 }
382 case EC_CMD_HOST_EVENT_GET_B: {
383 struct ec_response_host_event_mask *resp = resp_data;
384
385 resp->mask = 0;
386 if (ec->recovery_req) {
387 resp->mask |= EC_HOST_EVENT_MASK(
388 EC_HOST_EVENT_KEYBOARD_RECOVERY);
389 }
Simon Glass03161302021-01-16 14:52:29 -0700390 if (ec->test_flags & CROSECT_LID_OPEN)
391 resp->mask |=
392 EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN);
Simon Glass36170f02014-02-27 13:26:12 -0700393 len = sizeof(*resp);
394 break;
395 }
Simon Glass03161302021-01-16 14:52:29 -0700396 case EC_CMD_HOST_EVENT_CLEAR_B: {
397 const struct ec_params_host_event_mask *req = req_data;
398
399 if (req->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN))
400 ec->test_flags &= ~CROSECT_LID_OPEN;
401 len = 0;
402 break;
403 }
Simon Glass36170f02014-02-27 13:26:12 -0700404 case EC_CMD_VBOOT_HASH: {
405 const struct ec_params_vboot_hash *req = req_data;
406 struct ec_response_vboot_hash *resp = resp_data;
407 struct fmap_entry *entry;
408 int ret, size;
409
Simon Glass2c1e5502018-10-01 12:22:36 -0600410 entry = &ec->ec_config.region[EC_FLASH_REGION_ACTIVE];
Simon Glass36170f02014-02-27 13:26:12 -0700411
412 switch (req->cmd) {
413 case EC_VBOOT_HASH_RECALC:
414 case EC_VBOOT_HASH_GET:
415 size = SHA256_SUM_LEN;
416 len = get_image_used(ec, entry);
417 ret = hash_block("sha256",
418 ec->flash_data + entry->offset,
419 len, resp->hash_digest, &size);
420 if (ret) {
421 printf(" ** hash_block() failed\n");
422 return -1;
423 }
424 resp->status = EC_VBOOT_HASH_STATUS_DONE;
425 resp->hash_type = EC_VBOOT_HASH_TYPE_SHA256;
426 resp->digest_size = size;
427 resp->reserved0 = 0;
428 resp->offset = entry->offset;
429 resp->size = len;
430 len = sizeof(*resp);
431 break;
432 default:
433 printf(" ** EC_CMD_VBOOT_HASH: Unknown command %d\n",
434 req->cmd);
435 return -1;
436 }
437 break;
438 }
439 case EC_CMD_FLASH_PROTECT: {
440 const struct ec_params_flash_protect *req = req_data;
441 struct ec_response_flash_protect *resp = resp_data;
442 uint32_t expect = EC_FLASH_PROTECT_ALL_NOW |
443 EC_FLASH_PROTECT_ALL_AT_BOOT;
444
445 printf("mask=%#x, flags=%#x\n", req->mask, req->flags);
446 if (req->flags == expect || req->flags == 0) {
447 resp->flags = req->flags ? EC_FLASH_PROTECT_ALL_NOW :
448 0;
449 resp->valid_flags = EC_FLASH_PROTECT_ALL_NOW;
450 resp->writable_flags = 0;
451 len = sizeof(*resp);
452 } else {
453 puts(" ** unexpected flash protect request\n");
454 return -1;
455 }
456 break;
457 }
458 case EC_CMD_FLASH_REGION_INFO: {
459 const struct ec_params_flash_region_info *req = req_data;
460 struct ec_response_flash_region_info *resp = resp_data;
461 struct fmap_entry *entry;
462
463 switch (req->region) {
464 case EC_FLASH_REGION_RO:
Simon Glass2c1e5502018-10-01 12:22:36 -0600465 case EC_FLASH_REGION_ACTIVE:
Simon Glass36170f02014-02-27 13:26:12 -0700466 case EC_FLASH_REGION_WP_RO:
Simon Glassff425d82014-10-13 23:42:15 -0600467 entry = &ec->ec_config.region[req->region];
Simon Glass36170f02014-02-27 13:26:12 -0700468 resp->offset = entry->offset;
469 resp->size = entry->length;
470 len = sizeof(*resp);
471 printf("EC flash region %d: offset=%#x, size=%#x\n",
472 req->region, resp->offset, resp->size);
473 break;
474 default:
475 printf("** Unknown flash region %d\n", req->region);
476 return -1;
477 }
478 break;
479 }
480 case EC_CMD_FLASH_ERASE: {
481 const struct ec_params_flash_erase *req = req_data;
482
483 memset(ec->flash_data + req->offset,
484 ec->ec_config.flash_erase_value,
485 req->size);
486 len = 0;
487 break;
488 }
489 case EC_CMD_FLASH_WRITE: {
490 const struct ec_params_flash_write *req = req_data;
491
492 memcpy(ec->flash_data + req->offset, req + 1, req->size);
493 len = 0;
494 break;
495 }
496 case EC_CMD_MKBP_STATE:
497 len = cros_ec_keyscan(ec, resp_data);
498 break;
Alper Nebi Yasak5370d832020-11-14 01:01:42 +0300499 case EC_CMD_GET_NEXT_EVENT: {
500 struct ec_response_get_next_event *resp = resp_data;
501
502 resp->event_type = EC_MKBP_EVENT_KEY_MATRIX;
503 cros_ec_keyscan(ec, resp->data.key_matrix);
504 len = sizeof(*resp);
505 break;
506 }
Simon Glass33909b52021-01-16 14:52:25 -0700507 case EC_CMD_GET_SKU_ID: {
508 struct ec_sku_id_info *resp = resp_data;
509
510 resp->sku_id = 1234;
511 len = sizeof(*resp);
512 break;
513 }
Simon Glass959e1ce2021-01-16 14:52:26 -0700514 case EC_CMD_GET_FEATURES: {
515 struct ec_response_get_features *resp = resp_data;
516
517 resp->flags[0] = EC_FEATURE_MASK_0(EC_FEATURE_FLASH) |
Simon Glass333e3a92021-01-16 14:52:31 -0700518 EC_FEATURE_MASK_0(EC_FEATURE_I2C) |
519 EC_FEATURE_MASK_0(EC_FEATURE_VSTORE);
Simon Glass959e1ce2021-01-16 14:52:26 -0700520 resp->flags[1] =
521 EC_FEATURE_MASK_1(EC_FEATURE_UNIFIED_WAKE_MASKS) |
522 EC_FEATURE_MASK_1(EC_FEATURE_ISH);
523 len = sizeof(*resp);
524 break;
525 }
Simon Glass333e3a92021-01-16 14:52:31 -0700526 case EC_CMD_VSTORE_INFO: {
527 struct ec_response_vstore_info *resp = resp_data;
528 int i;
529
530 resp->slot_count = VSTORE_SLOT_COUNT;
531 resp->slot_locked = 0;
532 for (i = 0; i < VSTORE_SLOT_COUNT; i++) {
533 if (ec->slot[i].locked)
534 resp->slot_locked |= 1 << i;
535 }
536 len = sizeof(*resp);
537 break;
538 };
539 case EC_CMD_VSTORE_WRITE: {
540 const struct ec_params_vstore_write *req = req_data;
541 struct vstore_slot *slot;
542
543 if (req->slot >= EC_VSTORE_SLOT_MAX)
544 return -EINVAL;
545 slot = &ec->slot[req->slot];
546 slot->locked = true;
547 memcpy(slot->data, req->data, EC_VSTORE_SLOT_SIZE);
548 len = 0;
549 break;
550 }
551 case EC_CMD_VSTORE_READ: {
552 const struct ec_params_vstore_read *req = req_data;
553 struct ec_response_vstore_read *resp = resp_data;
554 struct vstore_slot *slot;
555
556 if (req->slot >= EC_VSTORE_SLOT_MAX)
557 return -EINVAL;
558 slot = &ec->slot[req->slot];
559 memcpy(resp->data, slot->data, EC_VSTORE_SLOT_SIZE);
560 len = sizeof(*resp);
561 break;
562 }
Alper Nebi Yasak8a8cd4f2021-05-19 19:33:31 +0300563 case EC_CMD_PWM_GET_DUTY: {
564 const struct ec_params_pwm_get_duty *req = req_data;
565 struct ec_response_pwm_get_duty *resp = resp_data;
566 struct ec_pwm_channel *pwm;
567
568 if (req->pwm_type != EC_PWM_TYPE_GENERIC)
569 return -EINVAL;
570 if (req->index >= PWM_CHANNEL_COUNT)
571 return -EINVAL;
572 pwm = &ec->pwm[req->index];
573 resp->duty = pwm->duty;
574 len = sizeof(*resp);
575 break;
576 }
577 case EC_CMD_PWM_SET_DUTY: {
578 const struct ec_params_pwm_set_duty *req = req_data;
579 struct ec_pwm_channel *pwm;
580
581 if (req->pwm_type != EC_PWM_TYPE_GENERIC)
582 return -EINVAL;
583 if (req->index >= PWM_CHANNEL_COUNT)
584 return -EINVAL;
585 pwm = &ec->pwm[req->index];
586 pwm->duty = req->duty;
587 len = 0;
588 break;
589 }
Simon Glass36170f02014-02-27 13:26:12 -0700590 default:
591 printf(" ** Unknown EC command %#02x\n", req_hdr->command);
592 return -1;
593 }
Simon Glass6f52b382023-09-26 08:14:48 -0600594 debug(" - EC command %#0x, result %d\n", req_hdr->command, len);
Simon Glass36170f02014-02-27 13:26:12 -0700595
596 return len;
597}
598
Simon Glassff425d82014-10-13 23:42:15 -0600599int cros_ec_sandbox_packet(struct udevice *udev, int out_bytes, int in_bytes)
600{
Simon Glassde0977b2015-03-05 12:25:20 -0700601 struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
Simon Glassff425d82014-10-13 23:42:15 -0600602 struct ec_state *ec = dev_get_priv(dev->dev);
Simon Glass36170f02014-02-27 13:26:12 -0700603 struct ec_host_request *req_hdr = (struct ec_host_request *)dev->dout;
604 const void *req_data = req_hdr + 1;
605 struct ec_host_response *resp_hdr = (struct ec_host_response *)dev->din;
606 void *resp_data = resp_hdr + 1;
607 int len;
608
Simon Glassff425d82014-10-13 23:42:15 -0600609 len = process_cmd(ec, req_hdr, req_data, resp_hdr, resp_data);
Simon Glass36170f02014-02-27 13:26:12 -0700610 if (len < 0)
611 return len;
612
613 resp_hdr->struct_version = 3;
614 resp_hdr->result = EC_RES_SUCCESS;
615 resp_hdr->data_len = len;
616 resp_hdr->reserved = 0;
617 len += sizeof(*resp_hdr);
618 resp_hdr->checksum = 0;
619 resp_hdr->checksum = (uint8_t)
620 -cros_ec_calc_checksum((const uint8_t *)resp_hdr, len);
621
622 return in_bytes;
623}
624
Simon Glassbed43522018-10-01 12:22:22 -0600625void cros_ec_check_keyboard(struct udevice *dev)
Simon Glass36170f02014-02-27 13:26:12 -0700626{
Simon Glassbed43522018-10-01 12:22:22 -0600627 struct ec_state *ec = dev_get_priv(dev);
Simon Glass36170f02014-02-27 13:26:12 -0700628 ulong start;
629
Simon Glass52ac65f2021-07-05 16:32:52 -0600630 printf("\nPress keys for EC to detect on reset (ESC=recovery)...");
Simon Glass36170f02014-02-27 13:26:12 -0700631 start = get_timer(0);
Simon Glass52ac65f2021-07-05 16:32:52 -0600632 while (get_timer(start) < 2000) {
633 if (tstc()) {
634 int ch = getchar();
635
636 if (ch == 0x1b) {
637 ec->recovery_req = true;
638 printf("EC requests recovery");
639 }
640 }
Simon Glass36170f02014-02-27 13:26:12 -0700641 }
Simon Glass52ac65f2021-07-05 16:32:52 -0600642 putc('\n');
Simon Glassff425d82014-10-13 23:42:15 -0600643}
644
Simon Glass9d702522021-01-16 14:52:28 -0700645/* Return the byte of EC switch states */
646static int cros_ec_sandbox_get_switches(struct udevice *dev)
647{
648 struct ec_state *ec = dev_get_priv(dev);
649
650 return ec->test_flags & CROSECT_LID_OPEN ? EC_SWITCH_LID_OPEN : 0;
651}
652
Simon Glass9b306e52021-01-16 14:52:22 -0700653void sandbox_cros_ec_set_test_flags(struct udevice *dev, uint flags)
654{
655 struct ec_state *ec = dev_get_priv(dev);
656
657 ec->test_flags = flags;
658}
659
Alper Nebi Yasak8a8cd4f2021-05-19 19:33:31 +0300660int sandbox_cros_ec_get_pwm_duty(struct udevice *dev, uint index, uint *duty)
661{
662 struct ec_state *ec = dev_get_priv(dev);
663 struct ec_pwm_channel *pwm;
664
665 if (index >= PWM_CHANNEL_COUNT)
666 return -ENOSPC;
667 pwm = &ec->pwm[index];
668 *duty = pwm->duty;
669
670 return 0;
671}
672
Simon Glassff425d82014-10-13 23:42:15 -0600673int cros_ec_probe(struct udevice *dev)
674{
Simon Glass95588622020-12-22 19:30:28 -0700675 struct ec_state *ec = dev_get_priv(dev);
676 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Simon Glass3e5a8b92016-06-19 17:33:15 -0600677 struct udevice *keyb_dev;
Simon Glassb35e6d62017-05-18 20:09:23 -0600678 ofnode node;
Simon Glassff425d82014-10-13 23:42:15 -0600679 int err;
680
Simon Glass6f52b382023-09-26 08:14:48 -0600681 if (s_state.valid)
682 memcpy(ec, &s_state, sizeof(*ec));
683 else
684 ec->current_image = EC_IMAGE_RO;
Simon Glassb35e6d62017-05-18 20:09:23 -0600685 err = cros_ec_decode_ec_flash(dev, &ec->ec_config);
686 if (err) {
687 debug("%s: Cannot device EC flash\n", __func__);
Simon Glassff425d82014-10-13 23:42:15 -0600688 return err;
Simon Glassb35e6d62017-05-18 20:09:23 -0600689 }
Simon Glassff425d82014-10-13 23:42:15 -0600690
Simon Glassb35e6d62017-05-18 20:09:23 -0600691 node = ofnode_null();
Simon Glass3e5a8b92016-06-19 17:33:15 -0600692 for (device_find_first_child(dev, &keyb_dev);
693 keyb_dev;
694 device_find_next_child(&keyb_dev)) {
695 if (device_get_uclass_id(keyb_dev) == UCLASS_KEYBOARD) {
Simon Glassb35e6d62017-05-18 20:09:23 -0600696 node = dev_ofnode(keyb_dev);
Simon Glass3e5a8b92016-06-19 17:33:15 -0600697 break;
698 }
699 }
Simon Glassb35e6d62017-05-18 20:09:23 -0600700 if (!ofnode_valid(node)) {
Simon Glassff425d82014-10-13 23:42:15 -0600701 debug("%s: No cros_ec keyboard found\n", __func__);
Simon Glassb35e6d62017-05-18 20:09:23 -0600702 } else if (keyscan_read_fdt_matrix(ec, node)) {
Simon Glassff425d82014-10-13 23:42:15 -0600703 debug("%s: Could not read key matrix\n", __func__);
704 return -1;
705 }
706
707 /* If we loaded EC data, check that the length matches */
708 if (ec->flash_data &&
709 ec->flash_data_len != ec->ec_config.flash.length) {
710 printf("EC data length is %x, expected %x, discarding data\n",
711 ec->flash_data_len, ec->ec_config.flash.length);
Simon Glassa4f416d2020-02-03 07:36:18 -0700712 free(ec->flash_data);
Simon Glassff425d82014-10-13 23:42:15 -0600713 ec->flash_data = NULL;
714 }
715
716 /* Otherwise allocate the memory */
717 if (!ec->flash_data) {
718 ec->flash_data_len = ec->ec_config.flash.length;
Simon Glassa4f416d2020-02-03 07:36:18 -0700719 ec->flash_data = malloc(ec->flash_data_len);
Simon Glassff425d82014-10-13 23:42:15 -0600720 if (!ec->flash_data)
721 return -ENOMEM;
722 }
723
724 cdev->dev = dev;
725 g_state = ec;
726 return cros_ec_register(dev);
Simon Glass36170f02014-02-27 13:26:12 -0700727}
Simon Glassff425d82014-10-13 23:42:15 -0600728
Simon Glassff425d82014-10-13 23:42:15 -0600729struct dm_cros_ec_ops cros_ec_ops = {
730 .packet = cros_ec_sandbox_packet,
Simon Glass9d702522021-01-16 14:52:28 -0700731 .get_switches = cros_ec_sandbox_get_switches,
Simon Glassff425d82014-10-13 23:42:15 -0600732};
733
734static const struct udevice_id cros_ec_ids[] = {
Simon Glass73f220c2015-03-26 09:29:39 -0600735 { .compatible = "google,cros-ec-sandbox" },
Simon Glassff425d82014-10-13 23:42:15 -0600736 { }
737};
738
Walter Lozano2901ac62020-06-25 01:10:04 -0300739U_BOOT_DRIVER(google_cros_ec_sandbox) = {
740 .name = "google_cros_ec_sandbox",
Simon Glassff425d82014-10-13 23:42:15 -0600741 .id = UCLASS_CROS_EC,
742 .of_match = cros_ec_ids,
743 .probe = cros_ec_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700744 .priv_auto = sizeof(struct ec_state),
Simon Glassff425d82014-10-13 23:42:15 -0600745 .ops = &cros_ec_ops,
746};