blob: beea47caa33bf0600520135a96a1dc2880867114 [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 <common.h>
11#include <cros_ec.h>
Simon Glassff425d82014-10-13 23:42:15 -060012#include <dm.h>
Simon Glass36170f02014-02-27 13:26:12 -070013#include <ec_commands.h>
14#include <errno.h>
15#include <hash.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Simon Glass36170f02014-02-27 13:26:12 -070017#include <os.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020018#include <u-boot/sha256.h>
Simon Glass36170f02014-02-27 13:26:12 -070019#include <spi.h>
Simon Glassa4f416d2020-02-03 07:36:18 -070020#include <asm/malloc.h>
Simon Glass36170f02014-02-27 13:26:12 -070021#include <asm/state.h>
22#include <asm/sdl.h>
Simon Glass9b306e52021-01-16 14:52:22 -070023#include <asm/test.h>
Simon Glass36170f02014-02-27 13:26:12 -070024#include <linux/input.h>
25
26/*
27 * Ultimately it shold be possible to connect an Chrome OS EC emulation
28 * to U-Boot and remove all of this code. But this provides a test
29 * environment for bringing up chromeos_sandbox and demonstrating its
30 * utility.
31 *
32 * This emulation includes the following:
33 *
34 * 1. Emulation of the keyboard, by converting keypresses received from SDL
35 * into key scan data, passed back from the EC as key scan messages. The
36 * key layout is read from the device tree.
37 *
38 * 2. Emulation of vboot context - so this can be read/written as required.
39 *
40 * 3. Save/restore of EC state, so that the vboot context, flash memory
41 * contents and current image can be preserved across boots. This is important
42 * since the EC is supposed to continue running even if the AP resets.
43 *
44 * 4. Some event support, in particular allowing Escape to be pressed on boot
45 * to enter recovery mode. The EC passes this to U-Boot through the normal
46 * event message.
47 *
48 * 5. Flash read/write/erase support, so that software sync works. The
49 * protect messages are supported but no protection is implemented.
50 *
51 * 6. Hashing of the EC image, again to support software sync.
52 *
53 * Other features can be added, although a better path is probably to link
54 * the EC image in with U-Boot (Vic has demonstrated a prototype for this).
55 */
56
Simon Glass36170f02014-02-27 13:26:12 -070057#define KEYBOARD_ROWS 8
58#define KEYBOARD_COLS 13
59
60/* A single entry of the key matrix */
61struct ec_keymatrix_entry {
62 int row; /* key matrix row */
63 int col; /* key matrix column */
64 int keycode; /* corresponding linux key code */
65};
66
Simon Glass333e3a92021-01-16 14:52:31 -070067enum {
68 VSTORE_SLOT_COUNT = 4,
Alper Nebi Yasak8a8cd4f2021-05-19 19:33:31 +030069 PWM_CHANNEL_COUNT = 4,
Simon Glass333e3a92021-01-16 14:52:31 -070070};
71
72struct vstore_slot {
73 bool locked;
74 u8 data[EC_VSTORE_SLOT_SIZE];
75};
76
Alper Nebi Yasak8a8cd4f2021-05-19 19:33:31 +030077struct ec_pwm_channel {
78 uint duty; /* not ns, EC_PWM_MAX_DUTY = 100% */
79};
80
Simon Glass36170f02014-02-27 13:26:12 -070081/**
82 * struct ec_state - Information about the EC state
83 *
84 * @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 Glass153f2522018-11-06 15:21:22 -070098 u8 vbnv_context[EC_VBNV_BLOCK_SIZE_V2];
Simon Glass36170f02014-02-27 13:26:12 -070099 struct fdt_cros_ec ec_config;
100 uint8_t *flash_data;
101 int flash_data_len;
102 enum ec_current_image current_image;
103 int matrix_count;
104 struct ec_keymatrix_entry *matrix; /* the key matrix info */
105 uint8_t keyscan[KEYBOARD_COLS];
106 bool recovery_req;
Simon Glass9b306e52021-01-16 14:52:22 -0700107 uint test_flags;
Simon Glass333e3a92021-01-16 14:52:31 -0700108 struct vstore_slot slot[VSTORE_SLOT_COUNT];
Alper Nebi Yasak8a8cd4f2021-05-19 19:33:31 +0300109 struct ec_pwm_channel pwm[PWM_CHANNEL_COUNT];
Simon Glassff425d82014-10-13 23:42:15 -0600110} s_state, *g_state;
Simon Glass36170f02014-02-27 13:26:12 -0700111
112/**
113 * cros_ec_read_state() - read the sandbox EC state from the state file
114 *
115 * If data is available, then blob and node will provide access to it. If
116 * not this function sets up an empty EC.
117 *
118 * @param blob: Pointer to device tree blob, or NULL if no data to read
119 * @param node: Node offset to read from
120 */
121static int cros_ec_read_state(const void *blob, int node)
122{
123 struct ec_state *ec = &s_state;
124 const char *prop;
125 int len;
126
127 /* Set everything to defaults */
128 ec->current_image = EC_IMAGE_RO;
129 if (!blob)
130 return 0;
131
132 /* Read the data if available */
133 ec->current_image = fdtdec_get_int(blob, node, "current-image",
134 EC_IMAGE_RO);
135 prop = fdt_getprop(blob, node, "vbnv-context", &len);
136 if (prop && len == sizeof(ec->vbnv_context))
137 memcpy(ec->vbnv_context, prop, len);
138
139 prop = fdt_getprop(blob, node, "flash-data", &len);
140 if (prop) {
141 ec->flash_data_len = len;
Simon Glassa4f416d2020-02-03 07:36:18 -0700142 ec->flash_data = malloc(len);
Simon Glass36170f02014-02-27 13:26:12 -0700143 if (!ec->flash_data)
144 return -ENOMEM;
145 memcpy(ec->flash_data, prop, len);
146 debug("%s: Loaded EC flash data size %#x\n", __func__, len);
147 }
148
149 return 0;
150}
151
152/**
153 * cros_ec_write_state() - Write out our state to the state file
154 *
155 * The caller will ensure that there is a node ready for the state. The node
156 * may already contain the old state, in which case it is overridden.
157 *
158 * @param blob: Device tree blob holding state
159 * @param node: Node to write our state into
160 */
161static int cros_ec_write_state(void *blob, int node)
162{
Simon Glassff425d82014-10-13 23:42:15 -0600163 struct ec_state *ec = g_state;
Simon Glass36170f02014-02-27 13:26:12 -0700164
Simon Glassc747f8b2021-03-15 18:11:08 +1300165 if (!g_state)
166 return 0;
167
Simon Glass36170f02014-02-27 13:26:12 -0700168 /* We are guaranteed enough space to write basic properties */
169 fdt_setprop_u32(blob, node, "current-image", ec->current_image);
170 fdt_setprop(blob, node, "vbnv-context", ec->vbnv_context,
171 sizeof(ec->vbnv_context));
Simon Glassc747f8b2021-03-15 18:11:08 +1300172
Simon Glass36170f02014-02-27 13:26:12 -0700173 return state_setprop(node, "flash-data", ec->flash_data,
174 ec->ec_config.flash.length);
175}
176
177SANDBOX_STATE_IO(cros_ec, "google,cros-ec", cros_ec_read_state,
178 cros_ec_write_state);
179
180/**
181 * Return the number of bytes used in the specified image.
182 *
183 * This is the actual size of code+data in the image, as opposed to the
184 * amount of space reserved in flash for that image. This code is similar to
185 * that used by the real EC code base.
186 *
187 * @param ec Current emulated EC state
188 * @param entry Flash map entry containing the image to check
189 * @return actual image size in bytes, 0 if the image contains no content or
190 * error.
191 */
192static int get_image_used(struct ec_state *ec, struct fmap_entry *entry)
193{
194 int size;
195
196 /*
197 * Scan backwards looking for 0xea byte, which is by definition the
198 * last byte of the image. See ec.lds.S for how this is inserted at
199 * the end of the image.
200 */
201 for (size = entry->length - 1;
202 size > 0 && ec->flash_data[entry->offset + size] != 0xea;
203 size--)
204 ;
205
206 return size ? size + 1 : 0; /* 0xea byte IS part of the image */
207}
208
209/**
210 * Read the key matrix from the device tree
211 *
212 * Keymap entries in the fdt take the form of 0xRRCCKKKK where
213 * RR=Row CC=Column KKKK=Key Code
214 *
215 * @param ec Current emulated EC state
Simon Glass36170f02014-02-27 13:26:12 -0700216 * @param node Keyboard node of device tree containing keyscan information
217 * @return 0 if ok, -1 on error
218 */
Simon Glassb35e6d62017-05-18 20:09:23 -0600219static int keyscan_read_fdt_matrix(struct ec_state *ec, ofnode node)
Simon Glass36170f02014-02-27 13:26:12 -0700220{
221 const u32 *cell;
222 int upto;
223 int len;
224
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900225 cell = ofnode_get_property(node, "linux,keymap", &len);
Simon Glass82930812021-05-13 19:39:26 -0600226 if (!cell)
227 return log_msg_ret("prop", -EINVAL);
Simon Glass36170f02014-02-27 13:26:12 -0700228 ec->matrix_count = len / 4;
229 ec->matrix = calloc(ec->matrix_count, sizeof(*ec->matrix));
230 if (!ec->matrix) {
Simon Glass82930812021-05-13 19:39:26 -0600231 return log_msg_ret("mem", -ENOMEM);
Simon Glass36170f02014-02-27 13:26:12 -0700232 }
233
234 /* Now read the data */
235 for (upto = 0; upto < ec->matrix_count; upto++) {
236 struct ec_keymatrix_entry *matrix = &ec->matrix[upto];
237 u32 word;
238
239 word = fdt32_to_cpu(*cell++);
240 matrix->row = word >> 24;
241 matrix->col = (word >> 16) & 0xff;
242 matrix->keycode = word & 0xffff;
243
244 /* Hard-code some sanity limits for now */
245 if (matrix->row >= KEYBOARD_ROWS ||
246 matrix->col >= KEYBOARD_COLS) {
247 debug("%s: Matrix pos out of range (%d,%d)\n",
248 __func__, matrix->row, matrix->col);
Simon Glass82930812021-05-13 19:39:26 -0600249 return log_msg_ret("matrix", -ERANGE);
Simon Glass36170f02014-02-27 13:26:12 -0700250 }
251 }
252
253 if (upto != ec->matrix_count) {
Simon Glass82930812021-05-13 19:39:26 -0600254 return log_msg_ret("matrix", -E2BIG);
Simon Glass36170f02014-02-27 13:26:12 -0700255 }
256
257 return 0;
258}
259
260/**
261 * Return the next keyscan message contents
262 *
263 * @param ec Current emulated EC state
264 * @param scan Place to put keyscan bytes for the keyscan message (must hold
265 * enough space for a full keyscan)
266 * @return number of bytes of valid scan data
267 */
268static int cros_ec_keyscan(struct ec_state *ec, uint8_t *scan)
269{
270 const struct ec_keymatrix_entry *matrix;
271 int bytes = KEYBOARD_COLS;
272 int key[8]; /* allow up to 8 keys to be pressed at once */
273 int count;
274 int i;
275
276 memset(ec->keyscan, '\0', bytes);
277 count = sandbox_sdl_scan_keys(key, ARRAY_SIZE(key));
278
279 /* Look up keycode in matrix */
280 for (i = 0, matrix = ec->matrix; i < ec->matrix_count; i++, matrix++) {
281 bool found;
282 int j;
283
284 for (found = false, j = 0; j < count; j++) {
285 if (matrix->keycode == key[j])
286 found = true;
287 }
288
289 if (found) {
290 debug("%d: %d,%d\n", matrix->keycode, matrix->row,
291 matrix->col);
292 ec->keyscan[matrix->col] |= 1 << matrix->row;
293 }
294 }
295
296 memcpy(scan, ec->keyscan, bytes);
297 return bytes;
298}
299
300/**
301 * Process an emulated EC command
302 *
303 * @param ec Current emulated EC state
304 * @param req_hdr Pointer to request header
305 * @param req_data Pointer to body of request
306 * @param resp_hdr Pointer to place to put response header
307 * @param resp_data Pointer to place to put response data, if any
308 * @return length of response data, or 0 for no response data, or -1 on error
309 */
310static int process_cmd(struct ec_state *ec,
311 struct ec_host_request *req_hdr, const void *req_data,
312 struct ec_host_response *resp_hdr, void *resp_data)
313{
314 int len;
315
316 /* TODO(sjg@chromium.org): Check checksums */
317 debug("EC command %#0x\n", req_hdr->command);
318
319 switch (req_hdr->command) {
320 case EC_CMD_HELLO: {
321 const struct ec_params_hello *req = req_data;
322 struct ec_response_hello *resp = resp_data;
323
324 resp->out_data = req->in_data + 0x01020304;
Simon Glass9b306e52021-01-16 14:52:22 -0700325 if (ec->test_flags & CROSECT_BREAK_HELLO)
326 resp->out_data++;
Simon Glass36170f02014-02-27 13:26:12 -0700327 len = sizeof(*resp);
328 break;
329 }
330 case EC_CMD_GET_VERSION: {
331 struct ec_response_get_version *resp = resp_data;
332
333 strcpy(resp->version_string_ro, "sandbox_ro");
334 strcpy(resp->version_string_rw, "sandbox_rw");
335 resp->current_image = ec->current_image;
336 debug("Current image %d\n", resp->current_image);
337 len = sizeof(*resp);
338 break;
339 }
340 case EC_CMD_VBNV_CONTEXT: {
341 const struct ec_params_vbnvcontext *req = req_data;
342 struct ec_response_vbnvcontext *resp = resp_data;
343
344 switch (req->op) {
345 case EC_VBNV_CONTEXT_OP_READ:
Simon Glassea2cf052018-11-23 21:29:37 -0700346 /* TODO(sjg@chromium.org): Support full-size context */
Simon Glass36170f02014-02-27 13:26:12 -0700347 memcpy(resp->block, ec->vbnv_context,
Simon Glassea2cf052018-11-23 21:29:37 -0700348 EC_VBNV_BLOCK_SIZE);
349 len = 16;
Simon Glass36170f02014-02-27 13:26:12 -0700350 break;
351 case EC_VBNV_CONTEXT_OP_WRITE:
Simon Glassea2cf052018-11-23 21:29:37 -0700352 /* TODO(sjg@chromium.org): Support full-size context */
353 memcpy(ec->vbnv_context, req->block,
354 EC_VBNV_BLOCK_SIZE);
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;
Simon Glassee67a942015-05-04 11:31:09 -0600499 case EC_CMD_ENTERING_MODE:
Daniel Schwierzeckbfb744c2015-11-06 14:15:37 +0100500 len = 0;
Simon Glassee67a942015-05-04 11:31:09 -0600501 break;
Alper Nebi Yasak5370d832020-11-14 01:01:42 +0300502 case EC_CMD_GET_NEXT_EVENT: {
503 struct ec_response_get_next_event *resp = resp_data;
504
505 resp->event_type = EC_MKBP_EVENT_KEY_MATRIX;
506 cros_ec_keyscan(ec, resp->data.key_matrix);
507 len = sizeof(*resp);
508 break;
509 }
Simon Glass33909b52021-01-16 14:52:25 -0700510 case EC_CMD_GET_SKU_ID: {
511 struct ec_sku_id_info *resp = resp_data;
512
513 resp->sku_id = 1234;
514 len = sizeof(*resp);
515 break;
516 }
Simon Glass959e1ce2021-01-16 14:52:26 -0700517 case EC_CMD_GET_FEATURES: {
518 struct ec_response_get_features *resp = resp_data;
519
520 resp->flags[0] = EC_FEATURE_MASK_0(EC_FEATURE_FLASH) |
Simon Glass333e3a92021-01-16 14:52:31 -0700521 EC_FEATURE_MASK_0(EC_FEATURE_I2C) |
522 EC_FEATURE_MASK_0(EC_FEATURE_VSTORE);
Simon Glass959e1ce2021-01-16 14:52:26 -0700523 resp->flags[1] =
524 EC_FEATURE_MASK_1(EC_FEATURE_UNIFIED_WAKE_MASKS) |
525 EC_FEATURE_MASK_1(EC_FEATURE_ISH);
526 len = sizeof(*resp);
527 break;
528 }
Simon Glass333e3a92021-01-16 14:52:31 -0700529 case EC_CMD_VSTORE_INFO: {
530 struct ec_response_vstore_info *resp = resp_data;
531 int i;
532
533 resp->slot_count = VSTORE_SLOT_COUNT;
534 resp->slot_locked = 0;
535 for (i = 0; i < VSTORE_SLOT_COUNT; i++) {
536 if (ec->slot[i].locked)
537 resp->slot_locked |= 1 << i;
538 }
539 len = sizeof(*resp);
540 break;
541 };
542 case EC_CMD_VSTORE_WRITE: {
543 const struct ec_params_vstore_write *req = req_data;
544 struct vstore_slot *slot;
545
546 if (req->slot >= EC_VSTORE_SLOT_MAX)
547 return -EINVAL;
548 slot = &ec->slot[req->slot];
549 slot->locked = true;
550 memcpy(slot->data, req->data, EC_VSTORE_SLOT_SIZE);
551 len = 0;
552 break;
553 }
554 case EC_CMD_VSTORE_READ: {
555 const struct ec_params_vstore_read *req = req_data;
556 struct ec_response_vstore_read *resp = resp_data;
557 struct vstore_slot *slot;
558
559 if (req->slot >= EC_VSTORE_SLOT_MAX)
560 return -EINVAL;
561 slot = &ec->slot[req->slot];
562 memcpy(resp->data, slot->data, EC_VSTORE_SLOT_SIZE);
563 len = sizeof(*resp);
564 break;
565 }
Alper Nebi Yasak8a8cd4f2021-05-19 19:33:31 +0300566 case EC_CMD_PWM_GET_DUTY: {
567 const struct ec_params_pwm_get_duty *req = req_data;
568 struct ec_response_pwm_get_duty *resp = resp_data;
569 struct ec_pwm_channel *pwm;
570
571 if (req->pwm_type != EC_PWM_TYPE_GENERIC)
572 return -EINVAL;
573 if (req->index >= PWM_CHANNEL_COUNT)
574 return -EINVAL;
575 pwm = &ec->pwm[req->index];
576 resp->duty = pwm->duty;
577 len = sizeof(*resp);
578 break;
579 }
580 case EC_CMD_PWM_SET_DUTY: {
581 const struct ec_params_pwm_set_duty *req = req_data;
582 struct ec_pwm_channel *pwm;
583
584 if (req->pwm_type != EC_PWM_TYPE_GENERIC)
585 return -EINVAL;
586 if (req->index >= PWM_CHANNEL_COUNT)
587 return -EINVAL;
588 pwm = &ec->pwm[req->index];
589 pwm->duty = req->duty;
590 len = 0;
591 break;
592 }
Simon Glass36170f02014-02-27 13:26:12 -0700593 default:
594 printf(" ** Unknown EC command %#02x\n", req_hdr->command);
595 return -1;
596 }
597
598 return len;
599}
600
Simon Glassff425d82014-10-13 23:42:15 -0600601int cros_ec_sandbox_packet(struct udevice *udev, int out_bytes, int in_bytes)
602{
Simon Glassde0977b2015-03-05 12:25:20 -0700603 struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
Simon Glassff425d82014-10-13 23:42:15 -0600604 struct ec_state *ec = dev_get_priv(dev->dev);
Simon Glass36170f02014-02-27 13:26:12 -0700605 struct ec_host_request *req_hdr = (struct ec_host_request *)dev->dout;
606 const void *req_data = req_hdr + 1;
607 struct ec_host_response *resp_hdr = (struct ec_host_response *)dev->din;
608 void *resp_data = resp_hdr + 1;
609 int len;
610
Simon Glassff425d82014-10-13 23:42:15 -0600611 len = process_cmd(ec, req_hdr, req_data, resp_hdr, resp_data);
Simon Glass36170f02014-02-27 13:26:12 -0700612 if (len < 0)
613 return len;
614
615 resp_hdr->struct_version = 3;
616 resp_hdr->result = EC_RES_SUCCESS;
617 resp_hdr->data_len = len;
618 resp_hdr->reserved = 0;
619 len += sizeof(*resp_hdr);
620 resp_hdr->checksum = 0;
621 resp_hdr->checksum = (uint8_t)
622 -cros_ec_calc_checksum((const uint8_t *)resp_hdr, len);
623
624 return in_bytes;
625}
626
Simon Glassbed43522018-10-01 12:22:22 -0600627void cros_ec_check_keyboard(struct udevice *dev)
Simon Glass36170f02014-02-27 13:26:12 -0700628{
Simon Glassbed43522018-10-01 12:22:22 -0600629 struct ec_state *ec = dev_get_priv(dev);
Simon Glass36170f02014-02-27 13:26:12 -0700630 ulong start;
631
632 printf("Press keys for EC to detect on reset (ESC=recovery)...");
633 start = get_timer(0);
634 while (get_timer(start) < 1000)
635 ;
636 putc('\n');
637 if (!sandbox_sdl_key_pressed(KEY_ESC)) {
638 ec->recovery_req = true;
639 printf(" - EC requests recovery\n");
640 }
Simon Glassff425d82014-10-13 23:42:15 -0600641}
642
Simon Glass9d702522021-01-16 14:52:28 -0700643/* Return the byte of EC switch states */
644static int cros_ec_sandbox_get_switches(struct udevice *dev)
645{
646 struct ec_state *ec = dev_get_priv(dev);
647
648 return ec->test_flags & CROSECT_LID_OPEN ? EC_SWITCH_LID_OPEN : 0;
649}
650
Simon Glass9b306e52021-01-16 14:52:22 -0700651void sandbox_cros_ec_set_test_flags(struct udevice *dev, uint flags)
652{
653 struct ec_state *ec = dev_get_priv(dev);
654
655 ec->test_flags = flags;
656}
657
Alper Nebi Yasak8a8cd4f2021-05-19 19:33:31 +0300658int sandbox_cros_ec_get_pwm_duty(struct udevice *dev, uint index, uint *duty)
659{
660 struct ec_state *ec = dev_get_priv(dev);
661 struct ec_pwm_channel *pwm;
662
663 if (index >= PWM_CHANNEL_COUNT)
664 return -ENOSPC;
665 pwm = &ec->pwm[index];
666 *duty = pwm->duty;
667
668 return 0;
669}
670
Simon Glassff425d82014-10-13 23:42:15 -0600671int cros_ec_probe(struct udevice *dev)
672{
Simon Glass95588622020-12-22 19:30:28 -0700673 struct ec_state *ec = dev_get_priv(dev);
674 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
Simon Glass3e5a8b92016-06-19 17:33:15 -0600675 struct udevice *keyb_dev;
Simon Glassb35e6d62017-05-18 20:09:23 -0600676 ofnode node;
Simon Glassff425d82014-10-13 23:42:15 -0600677 int err;
678
679 memcpy(ec, &s_state, sizeof(*ec));
Simon Glassb35e6d62017-05-18 20:09:23 -0600680 err = cros_ec_decode_ec_flash(dev, &ec->ec_config);
681 if (err) {
682 debug("%s: Cannot device EC flash\n", __func__);
Simon Glassff425d82014-10-13 23:42:15 -0600683 return err;
Simon Glassb35e6d62017-05-18 20:09:23 -0600684 }
Simon Glassff425d82014-10-13 23:42:15 -0600685
Simon Glassb35e6d62017-05-18 20:09:23 -0600686 node = ofnode_null();
Simon Glass3e5a8b92016-06-19 17:33:15 -0600687 for (device_find_first_child(dev, &keyb_dev);
688 keyb_dev;
689 device_find_next_child(&keyb_dev)) {
690 if (device_get_uclass_id(keyb_dev) == UCLASS_KEYBOARD) {
Simon Glassb35e6d62017-05-18 20:09:23 -0600691 node = dev_ofnode(keyb_dev);
Simon Glass3e5a8b92016-06-19 17:33:15 -0600692 break;
693 }
694 }
Simon Glassb35e6d62017-05-18 20:09:23 -0600695 if (!ofnode_valid(node)) {
Simon Glassff425d82014-10-13 23:42:15 -0600696 debug("%s: No cros_ec keyboard found\n", __func__);
Simon Glassb35e6d62017-05-18 20:09:23 -0600697 } else if (keyscan_read_fdt_matrix(ec, node)) {
Simon Glassff425d82014-10-13 23:42:15 -0600698 debug("%s: Could not read key matrix\n", __func__);
699 return -1;
700 }
701
702 /* If we loaded EC data, check that the length matches */
703 if (ec->flash_data &&
704 ec->flash_data_len != ec->ec_config.flash.length) {
705 printf("EC data length is %x, expected %x, discarding data\n",
706 ec->flash_data_len, ec->ec_config.flash.length);
Simon Glassa4f416d2020-02-03 07:36:18 -0700707 free(ec->flash_data);
Simon Glassff425d82014-10-13 23:42:15 -0600708 ec->flash_data = NULL;
709 }
710
711 /* Otherwise allocate the memory */
712 if (!ec->flash_data) {
713 ec->flash_data_len = ec->ec_config.flash.length;
Simon Glassa4f416d2020-02-03 07:36:18 -0700714 ec->flash_data = malloc(ec->flash_data_len);
Simon Glassff425d82014-10-13 23:42:15 -0600715 if (!ec->flash_data)
716 return -ENOMEM;
717 }
718
719 cdev->dev = dev;
720 g_state = ec;
721 return cros_ec_register(dev);
Simon Glass36170f02014-02-27 13:26:12 -0700722}
Simon Glassff425d82014-10-13 23:42:15 -0600723
Simon Glassff425d82014-10-13 23:42:15 -0600724struct dm_cros_ec_ops cros_ec_ops = {
725 .packet = cros_ec_sandbox_packet,
Simon Glass9d702522021-01-16 14:52:28 -0700726 .get_switches = cros_ec_sandbox_get_switches,
Simon Glassff425d82014-10-13 23:42:15 -0600727};
728
729static const struct udevice_id cros_ec_ids[] = {
Simon Glass73f220c2015-03-26 09:29:39 -0600730 { .compatible = "google,cros-ec-sandbox" },
Simon Glassff425d82014-10-13 23:42:15 -0600731 { }
732};
733
Walter Lozano2901ac62020-06-25 01:10:04 -0300734U_BOOT_DRIVER(google_cros_ec_sandbox) = {
735 .name = "google_cros_ec_sandbox",
Simon Glassff425d82014-10-13 23:42:15 -0600736 .id = UCLASS_CROS_EC,
737 .of_match = cros_ec_ids,
738 .probe = cros_ec_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700739 .priv_auto = sizeof(struct ec_state),
Simon Glassff425d82014-10-13 23:42:15 -0600740 .ops = &cros_ec_ops,
741};