Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Chromium OS cros_ec driver - sandbox emulation |
| 4 | * |
| 5 | * Copyright (c) 2013 The Chromium OS Authors. |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <cros_ec.h> |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 11 | #include <ec_commands.h> |
| 12 | #include <errno.h> |
| 13 | #include <hash.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 15 | #include <os.h> |
Jeroen Hofstee | bfe88fe | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 16 | #include <u-boot/sha256.h> |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 17 | #include <spi.h> |
Simon Glass | a4f416d | 2020-02-03 07:36:18 -0700 | [diff] [blame] | 18 | #include <asm/malloc.h> |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 19 | #include <asm/state.h> |
| 20 | #include <asm/sdl.h> |
Simon Glass | 9b306e5 | 2021-01-16 14:52:22 -0700 | [diff] [blame] | 21 | #include <asm/test.h> |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 22 | #include <linux/input.h> |
| 23 | |
| 24 | /* |
| 25 | * Ultimately it shold be possible to connect an Chrome OS EC emulation |
| 26 | * to U-Boot and remove all of this code. But this provides a test |
| 27 | * environment for bringing up chromeos_sandbox and demonstrating its |
| 28 | * utility. |
| 29 | * |
| 30 | * This emulation includes the following: |
| 31 | * |
| 32 | * 1. Emulation of the keyboard, by converting keypresses received from SDL |
| 33 | * into key scan data, passed back from the EC as key scan messages. The |
| 34 | * key layout is read from the device tree. |
| 35 | * |
| 36 | * 2. Emulation of vboot context - so this can be read/written as required. |
| 37 | * |
| 38 | * 3. Save/restore of EC state, so that the vboot context, flash memory |
| 39 | * contents and current image can be preserved across boots. This is important |
| 40 | * since the EC is supposed to continue running even if the AP resets. |
| 41 | * |
| 42 | * 4. Some event support, in particular allowing Escape to be pressed on boot |
| 43 | * to enter recovery mode. The EC passes this to U-Boot through the normal |
| 44 | * event message. |
| 45 | * |
| 46 | * 5. Flash read/write/erase support, so that software sync works. The |
| 47 | * protect messages are supported but no protection is implemented. |
| 48 | * |
| 49 | * 6. Hashing of the EC image, again to support software sync. |
| 50 | * |
| 51 | * Other features can be added, although a better path is probably to link |
| 52 | * the EC image in with U-Boot (Vic has demonstrated a prototype for this). |
| 53 | */ |
| 54 | |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 55 | #define KEYBOARD_ROWS 8 |
| 56 | #define KEYBOARD_COLS 13 |
| 57 | |
| 58 | /* A single entry of the key matrix */ |
| 59 | struct ec_keymatrix_entry { |
| 60 | int row; /* key matrix row */ |
| 61 | int col; /* key matrix column */ |
| 62 | int keycode; /* corresponding linux key code */ |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * struct ec_state - Information about the EC state |
| 67 | * |
| 68 | * @vbnv_context: Vboot context data stored by EC |
| 69 | * @ec_config: FDT config information about the EC (e.g. flashmap) |
| 70 | * @flash_data: Contents of flash memory |
| 71 | * @flash_data_len: Size of flash memory |
| 72 | * @current_image: Current image the EC is running |
| 73 | * @matrix_count: Number of keys to decode in matrix |
| 74 | * @matrix: Information about keyboard matrix |
| 75 | * @keyscan: Current keyscan information (bit set for each row/column pressed) |
| 76 | * @recovery_req: Keyboard recovery requested |
Simon Glass | 9b306e5 | 2021-01-16 14:52:22 -0700 | [diff] [blame] | 77 | * @test_flags: Flags that control behaviour for tests |
Simon Glass | 9d70252 | 2021-01-16 14:52:28 -0700 | [diff] [blame] | 78 | * @switches: Current switches value (EC_SWITCH_) |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 79 | */ |
| 80 | struct ec_state { |
Simon Glass | 153f252 | 2018-11-06 15:21:22 -0700 | [diff] [blame] | 81 | u8 vbnv_context[EC_VBNV_BLOCK_SIZE_V2]; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 82 | struct fdt_cros_ec ec_config; |
| 83 | uint8_t *flash_data; |
| 84 | int flash_data_len; |
| 85 | enum ec_current_image current_image; |
| 86 | int matrix_count; |
| 87 | struct ec_keymatrix_entry *matrix; /* the key matrix info */ |
| 88 | uint8_t keyscan[KEYBOARD_COLS]; |
| 89 | bool recovery_req; |
Simon Glass | 9b306e5 | 2021-01-16 14:52:22 -0700 | [diff] [blame] | 90 | uint test_flags; |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 91 | } s_state, *g_state; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * cros_ec_read_state() - read the sandbox EC state from the state file |
| 95 | * |
| 96 | * If data is available, then blob and node will provide access to it. If |
| 97 | * not this function sets up an empty EC. |
| 98 | * |
| 99 | * @param blob: Pointer to device tree blob, or NULL if no data to read |
| 100 | * @param node: Node offset to read from |
| 101 | */ |
| 102 | static int cros_ec_read_state(const void *blob, int node) |
| 103 | { |
| 104 | struct ec_state *ec = &s_state; |
| 105 | const char *prop; |
| 106 | int len; |
| 107 | |
| 108 | /* Set everything to defaults */ |
| 109 | ec->current_image = EC_IMAGE_RO; |
| 110 | if (!blob) |
| 111 | return 0; |
| 112 | |
| 113 | /* Read the data if available */ |
| 114 | ec->current_image = fdtdec_get_int(blob, node, "current-image", |
| 115 | EC_IMAGE_RO); |
| 116 | prop = fdt_getprop(blob, node, "vbnv-context", &len); |
| 117 | if (prop && len == sizeof(ec->vbnv_context)) |
| 118 | memcpy(ec->vbnv_context, prop, len); |
| 119 | |
| 120 | prop = fdt_getprop(blob, node, "flash-data", &len); |
| 121 | if (prop) { |
| 122 | ec->flash_data_len = len; |
Simon Glass | a4f416d | 2020-02-03 07:36:18 -0700 | [diff] [blame] | 123 | ec->flash_data = malloc(len); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 124 | if (!ec->flash_data) |
| 125 | return -ENOMEM; |
| 126 | memcpy(ec->flash_data, prop, len); |
| 127 | debug("%s: Loaded EC flash data size %#x\n", __func__, len); |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * cros_ec_write_state() - Write out our state to the state file |
| 135 | * |
| 136 | * The caller will ensure that there is a node ready for the state. The node |
| 137 | * may already contain the old state, in which case it is overridden. |
| 138 | * |
| 139 | * @param blob: Device tree blob holding state |
| 140 | * @param node: Node to write our state into |
| 141 | */ |
| 142 | static int cros_ec_write_state(void *blob, int node) |
| 143 | { |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 144 | struct ec_state *ec = g_state; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 145 | |
| 146 | /* We are guaranteed enough space to write basic properties */ |
| 147 | fdt_setprop_u32(blob, node, "current-image", ec->current_image); |
| 148 | fdt_setprop(blob, node, "vbnv-context", ec->vbnv_context, |
| 149 | sizeof(ec->vbnv_context)); |
| 150 | return state_setprop(node, "flash-data", ec->flash_data, |
| 151 | ec->ec_config.flash.length); |
| 152 | } |
| 153 | |
| 154 | SANDBOX_STATE_IO(cros_ec, "google,cros-ec", cros_ec_read_state, |
| 155 | cros_ec_write_state); |
| 156 | |
| 157 | /** |
| 158 | * Return the number of bytes used in the specified image. |
| 159 | * |
| 160 | * This is the actual size of code+data in the image, as opposed to the |
| 161 | * amount of space reserved in flash for that image. This code is similar to |
| 162 | * that used by the real EC code base. |
| 163 | * |
| 164 | * @param ec Current emulated EC state |
| 165 | * @param entry Flash map entry containing the image to check |
| 166 | * @return actual image size in bytes, 0 if the image contains no content or |
| 167 | * error. |
| 168 | */ |
| 169 | static int get_image_used(struct ec_state *ec, struct fmap_entry *entry) |
| 170 | { |
| 171 | int size; |
| 172 | |
| 173 | /* |
| 174 | * Scan backwards looking for 0xea byte, which is by definition the |
| 175 | * last byte of the image. See ec.lds.S for how this is inserted at |
| 176 | * the end of the image. |
| 177 | */ |
| 178 | for (size = entry->length - 1; |
| 179 | size > 0 && ec->flash_data[entry->offset + size] != 0xea; |
| 180 | size--) |
| 181 | ; |
| 182 | |
| 183 | return size ? size + 1 : 0; /* 0xea byte IS part of the image */ |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Read the key matrix from the device tree |
| 188 | * |
| 189 | * Keymap entries in the fdt take the form of 0xRRCCKKKK where |
| 190 | * RR=Row CC=Column KKKK=Key Code |
| 191 | * |
| 192 | * @param ec Current emulated EC state |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 193 | * @param node Keyboard node of device tree containing keyscan information |
| 194 | * @return 0 if ok, -1 on error |
| 195 | */ |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 196 | static int keyscan_read_fdt_matrix(struct ec_state *ec, ofnode node) |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 197 | { |
| 198 | const u32 *cell; |
| 199 | int upto; |
| 200 | int len; |
| 201 | |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 202 | cell = ofnode_get_property(node, "linux,keymap", &len); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 203 | ec->matrix_count = len / 4; |
| 204 | ec->matrix = calloc(ec->matrix_count, sizeof(*ec->matrix)); |
| 205 | if (!ec->matrix) { |
| 206 | debug("%s: Out of memory for key matrix\n", __func__); |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | /* Now read the data */ |
| 211 | for (upto = 0; upto < ec->matrix_count; upto++) { |
| 212 | struct ec_keymatrix_entry *matrix = &ec->matrix[upto]; |
| 213 | u32 word; |
| 214 | |
| 215 | word = fdt32_to_cpu(*cell++); |
| 216 | matrix->row = word >> 24; |
| 217 | matrix->col = (word >> 16) & 0xff; |
| 218 | matrix->keycode = word & 0xffff; |
| 219 | |
| 220 | /* Hard-code some sanity limits for now */ |
| 221 | if (matrix->row >= KEYBOARD_ROWS || |
| 222 | matrix->col >= KEYBOARD_COLS) { |
| 223 | debug("%s: Matrix pos out of range (%d,%d)\n", |
| 224 | __func__, matrix->row, matrix->col); |
| 225 | return -1; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (upto != ec->matrix_count) { |
| 230 | debug("%s: Read mismatch from key matrix\n", __func__); |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Return the next keyscan message contents |
| 239 | * |
| 240 | * @param ec Current emulated EC state |
| 241 | * @param scan Place to put keyscan bytes for the keyscan message (must hold |
| 242 | * enough space for a full keyscan) |
| 243 | * @return number of bytes of valid scan data |
| 244 | */ |
| 245 | static int cros_ec_keyscan(struct ec_state *ec, uint8_t *scan) |
| 246 | { |
| 247 | const struct ec_keymatrix_entry *matrix; |
| 248 | int bytes = KEYBOARD_COLS; |
| 249 | int key[8]; /* allow up to 8 keys to be pressed at once */ |
| 250 | int count; |
| 251 | int i; |
| 252 | |
| 253 | memset(ec->keyscan, '\0', bytes); |
| 254 | count = sandbox_sdl_scan_keys(key, ARRAY_SIZE(key)); |
| 255 | |
| 256 | /* Look up keycode in matrix */ |
| 257 | for (i = 0, matrix = ec->matrix; i < ec->matrix_count; i++, matrix++) { |
| 258 | bool found; |
| 259 | int j; |
| 260 | |
| 261 | for (found = false, j = 0; j < count; j++) { |
| 262 | if (matrix->keycode == key[j]) |
| 263 | found = true; |
| 264 | } |
| 265 | |
| 266 | if (found) { |
| 267 | debug("%d: %d,%d\n", matrix->keycode, matrix->row, |
| 268 | matrix->col); |
| 269 | ec->keyscan[matrix->col] |= 1 << matrix->row; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | memcpy(scan, ec->keyscan, bytes); |
| 274 | return bytes; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Process an emulated EC command |
| 279 | * |
| 280 | * @param ec Current emulated EC state |
| 281 | * @param req_hdr Pointer to request header |
| 282 | * @param req_data Pointer to body of request |
| 283 | * @param resp_hdr Pointer to place to put response header |
| 284 | * @param resp_data Pointer to place to put response data, if any |
| 285 | * @return length of response data, or 0 for no response data, or -1 on error |
| 286 | */ |
| 287 | static int process_cmd(struct ec_state *ec, |
| 288 | struct ec_host_request *req_hdr, const void *req_data, |
| 289 | struct ec_host_response *resp_hdr, void *resp_data) |
| 290 | { |
| 291 | int len; |
| 292 | |
| 293 | /* TODO(sjg@chromium.org): Check checksums */ |
| 294 | debug("EC command %#0x\n", req_hdr->command); |
| 295 | |
| 296 | switch (req_hdr->command) { |
| 297 | case EC_CMD_HELLO: { |
| 298 | const struct ec_params_hello *req = req_data; |
| 299 | struct ec_response_hello *resp = resp_data; |
| 300 | |
| 301 | resp->out_data = req->in_data + 0x01020304; |
Simon Glass | 9b306e5 | 2021-01-16 14:52:22 -0700 | [diff] [blame] | 302 | if (ec->test_flags & CROSECT_BREAK_HELLO) |
| 303 | resp->out_data++; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 304 | len = sizeof(*resp); |
| 305 | break; |
| 306 | } |
| 307 | case EC_CMD_GET_VERSION: { |
| 308 | struct ec_response_get_version *resp = resp_data; |
| 309 | |
| 310 | strcpy(resp->version_string_ro, "sandbox_ro"); |
| 311 | strcpy(resp->version_string_rw, "sandbox_rw"); |
| 312 | resp->current_image = ec->current_image; |
| 313 | debug("Current image %d\n", resp->current_image); |
| 314 | len = sizeof(*resp); |
| 315 | break; |
| 316 | } |
| 317 | case EC_CMD_VBNV_CONTEXT: { |
| 318 | const struct ec_params_vbnvcontext *req = req_data; |
| 319 | struct ec_response_vbnvcontext *resp = resp_data; |
| 320 | |
| 321 | switch (req->op) { |
| 322 | case EC_VBNV_CONTEXT_OP_READ: |
Simon Glass | ea2cf05 | 2018-11-23 21:29:37 -0700 | [diff] [blame] | 323 | /* TODO(sjg@chromium.org): Support full-size context */ |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 324 | memcpy(resp->block, ec->vbnv_context, |
Simon Glass | ea2cf05 | 2018-11-23 21:29:37 -0700 | [diff] [blame] | 325 | EC_VBNV_BLOCK_SIZE); |
| 326 | len = 16; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 327 | break; |
| 328 | case EC_VBNV_CONTEXT_OP_WRITE: |
Simon Glass | ea2cf05 | 2018-11-23 21:29:37 -0700 | [diff] [blame] | 329 | /* TODO(sjg@chromium.org): Support full-size context */ |
| 330 | memcpy(ec->vbnv_context, req->block, |
| 331 | EC_VBNV_BLOCK_SIZE); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 332 | len = 0; |
| 333 | break; |
| 334 | default: |
| 335 | printf(" ** Unknown vbnv_context command %#02x\n", |
| 336 | req->op); |
| 337 | return -1; |
| 338 | } |
| 339 | break; |
| 340 | } |
| 341 | case EC_CMD_REBOOT_EC: { |
| 342 | const struct ec_params_reboot_ec *req = req_data; |
| 343 | |
| 344 | printf("Request reboot type %d\n", req->cmd); |
| 345 | switch (req->cmd) { |
| 346 | case EC_REBOOT_DISABLE_JUMP: |
| 347 | len = 0; |
| 348 | break; |
| 349 | case EC_REBOOT_JUMP_RW: |
| 350 | ec->current_image = EC_IMAGE_RW; |
| 351 | len = 0; |
| 352 | break; |
| 353 | default: |
| 354 | puts(" ** Unknown type"); |
| 355 | return -1; |
| 356 | } |
| 357 | break; |
| 358 | } |
| 359 | case EC_CMD_HOST_EVENT_GET_B: { |
| 360 | struct ec_response_host_event_mask *resp = resp_data; |
| 361 | |
| 362 | resp->mask = 0; |
| 363 | if (ec->recovery_req) { |
| 364 | resp->mask |= EC_HOST_EVENT_MASK( |
| 365 | EC_HOST_EVENT_KEYBOARD_RECOVERY); |
| 366 | } |
Simon Glass | 0316130 | 2021-01-16 14:52:29 -0700 | [diff] [blame^] | 367 | if (ec->test_flags & CROSECT_LID_OPEN) |
| 368 | resp->mask |= |
| 369 | EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 370 | len = sizeof(*resp); |
| 371 | break; |
| 372 | } |
Simon Glass | 0316130 | 2021-01-16 14:52:29 -0700 | [diff] [blame^] | 373 | case EC_CMD_HOST_EVENT_CLEAR_B: { |
| 374 | const struct ec_params_host_event_mask *req = req_data; |
| 375 | |
| 376 | if (req->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN)) |
| 377 | ec->test_flags &= ~CROSECT_LID_OPEN; |
| 378 | len = 0; |
| 379 | break; |
| 380 | } |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 381 | case EC_CMD_VBOOT_HASH: { |
| 382 | const struct ec_params_vboot_hash *req = req_data; |
| 383 | struct ec_response_vboot_hash *resp = resp_data; |
| 384 | struct fmap_entry *entry; |
| 385 | int ret, size; |
| 386 | |
Simon Glass | 2c1e550 | 2018-10-01 12:22:36 -0600 | [diff] [blame] | 387 | entry = &ec->ec_config.region[EC_FLASH_REGION_ACTIVE]; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 388 | |
| 389 | switch (req->cmd) { |
| 390 | case EC_VBOOT_HASH_RECALC: |
| 391 | case EC_VBOOT_HASH_GET: |
| 392 | size = SHA256_SUM_LEN; |
| 393 | len = get_image_used(ec, entry); |
| 394 | ret = hash_block("sha256", |
| 395 | ec->flash_data + entry->offset, |
| 396 | len, resp->hash_digest, &size); |
| 397 | if (ret) { |
| 398 | printf(" ** hash_block() failed\n"); |
| 399 | return -1; |
| 400 | } |
| 401 | resp->status = EC_VBOOT_HASH_STATUS_DONE; |
| 402 | resp->hash_type = EC_VBOOT_HASH_TYPE_SHA256; |
| 403 | resp->digest_size = size; |
| 404 | resp->reserved0 = 0; |
| 405 | resp->offset = entry->offset; |
| 406 | resp->size = len; |
| 407 | len = sizeof(*resp); |
| 408 | break; |
| 409 | default: |
| 410 | printf(" ** EC_CMD_VBOOT_HASH: Unknown command %d\n", |
| 411 | req->cmd); |
| 412 | return -1; |
| 413 | } |
| 414 | break; |
| 415 | } |
| 416 | case EC_CMD_FLASH_PROTECT: { |
| 417 | const struct ec_params_flash_protect *req = req_data; |
| 418 | struct ec_response_flash_protect *resp = resp_data; |
| 419 | uint32_t expect = EC_FLASH_PROTECT_ALL_NOW | |
| 420 | EC_FLASH_PROTECT_ALL_AT_BOOT; |
| 421 | |
| 422 | printf("mask=%#x, flags=%#x\n", req->mask, req->flags); |
| 423 | if (req->flags == expect || req->flags == 0) { |
| 424 | resp->flags = req->flags ? EC_FLASH_PROTECT_ALL_NOW : |
| 425 | 0; |
| 426 | resp->valid_flags = EC_FLASH_PROTECT_ALL_NOW; |
| 427 | resp->writable_flags = 0; |
| 428 | len = sizeof(*resp); |
| 429 | } else { |
| 430 | puts(" ** unexpected flash protect request\n"); |
| 431 | return -1; |
| 432 | } |
| 433 | break; |
| 434 | } |
| 435 | case EC_CMD_FLASH_REGION_INFO: { |
| 436 | const struct ec_params_flash_region_info *req = req_data; |
| 437 | struct ec_response_flash_region_info *resp = resp_data; |
| 438 | struct fmap_entry *entry; |
| 439 | |
| 440 | switch (req->region) { |
| 441 | case EC_FLASH_REGION_RO: |
Simon Glass | 2c1e550 | 2018-10-01 12:22:36 -0600 | [diff] [blame] | 442 | case EC_FLASH_REGION_ACTIVE: |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 443 | case EC_FLASH_REGION_WP_RO: |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 444 | entry = &ec->ec_config.region[req->region]; |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 445 | resp->offset = entry->offset; |
| 446 | resp->size = entry->length; |
| 447 | len = sizeof(*resp); |
| 448 | printf("EC flash region %d: offset=%#x, size=%#x\n", |
| 449 | req->region, resp->offset, resp->size); |
| 450 | break; |
| 451 | default: |
| 452 | printf("** Unknown flash region %d\n", req->region); |
| 453 | return -1; |
| 454 | } |
| 455 | break; |
| 456 | } |
| 457 | case EC_CMD_FLASH_ERASE: { |
| 458 | const struct ec_params_flash_erase *req = req_data; |
| 459 | |
| 460 | memset(ec->flash_data + req->offset, |
| 461 | ec->ec_config.flash_erase_value, |
| 462 | req->size); |
| 463 | len = 0; |
| 464 | break; |
| 465 | } |
| 466 | case EC_CMD_FLASH_WRITE: { |
| 467 | const struct ec_params_flash_write *req = req_data; |
| 468 | |
| 469 | memcpy(ec->flash_data + req->offset, req + 1, req->size); |
| 470 | len = 0; |
| 471 | break; |
| 472 | } |
| 473 | case EC_CMD_MKBP_STATE: |
| 474 | len = cros_ec_keyscan(ec, resp_data); |
| 475 | break; |
Simon Glass | ee67a94 | 2015-05-04 11:31:09 -0600 | [diff] [blame] | 476 | case EC_CMD_ENTERING_MODE: |
Daniel Schwierzeck | bfb744c | 2015-11-06 14:15:37 +0100 | [diff] [blame] | 477 | len = 0; |
Simon Glass | ee67a94 | 2015-05-04 11:31:09 -0600 | [diff] [blame] | 478 | break; |
Alper Nebi Yasak | 5370d83 | 2020-11-14 01:01:42 +0300 | [diff] [blame] | 479 | case EC_CMD_GET_NEXT_EVENT: { |
| 480 | struct ec_response_get_next_event *resp = resp_data; |
| 481 | |
| 482 | resp->event_type = EC_MKBP_EVENT_KEY_MATRIX; |
| 483 | cros_ec_keyscan(ec, resp->data.key_matrix); |
| 484 | len = sizeof(*resp); |
| 485 | break; |
| 486 | } |
Simon Glass | 33909b5 | 2021-01-16 14:52:25 -0700 | [diff] [blame] | 487 | case EC_CMD_GET_SKU_ID: { |
| 488 | struct ec_sku_id_info *resp = resp_data; |
| 489 | |
| 490 | resp->sku_id = 1234; |
| 491 | len = sizeof(*resp); |
| 492 | break; |
| 493 | } |
Simon Glass | 959e1ce | 2021-01-16 14:52:26 -0700 | [diff] [blame] | 494 | case EC_CMD_GET_FEATURES: { |
| 495 | struct ec_response_get_features *resp = resp_data; |
| 496 | |
| 497 | resp->flags[0] = EC_FEATURE_MASK_0(EC_FEATURE_FLASH) | |
| 498 | EC_FEATURE_MASK_0(EC_FEATURE_I2C); |
| 499 | resp->flags[1] = |
| 500 | EC_FEATURE_MASK_1(EC_FEATURE_UNIFIED_WAKE_MASKS) | |
| 501 | EC_FEATURE_MASK_1(EC_FEATURE_ISH); |
| 502 | len = sizeof(*resp); |
| 503 | break; |
| 504 | } |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 505 | default: |
| 506 | printf(" ** Unknown EC command %#02x\n", req_hdr->command); |
| 507 | return -1; |
| 508 | } |
| 509 | |
| 510 | return len; |
| 511 | } |
| 512 | |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 513 | int cros_ec_sandbox_packet(struct udevice *udev, int out_bytes, int in_bytes) |
| 514 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 515 | struct cros_ec_dev *dev = dev_get_uclass_priv(udev); |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 516 | struct ec_state *ec = dev_get_priv(dev->dev); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 517 | struct ec_host_request *req_hdr = (struct ec_host_request *)dev->dout; |
| 518 | const void *req_data = req_hdr + 1; |
| 519 | struct ec_host_response *resp_hdr = (struct ec_host_response *)dev->din; |
| 520 | void *resp_data = resp_hdr + 1; |
| 521 | int len; |
| 522 | |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 523 | len = process_cmd(ec, req_hdr, req_data, resp_hdr, resp_data); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 524 | if (len < 0) |
| 525 | return len; |
| 526 | |
| 527 | resp_hdr->struct_version = 3; |
| 528 | resp_hdr->result = EC_RES_SUCCESS; |
| 529 | resp_hdr->data_len = len; |
| 530 | resp_hdr->reserved = 0; |
| 531 | len += sizeof(*resp_hdr); |
| 532 | resp_hdr->checksum = 0; |
| 533 | resp_hdr->checksum = (uint8_t) |
| 534 | -cros_ec_calc_checksum((const uint8_t *)resp_hdr, len); |
| 535 | |
| 536 | return in_bytes; |
| 537 | } |
| 538 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 539 | void cros_ec_check_keyboard(struct udevice *dev) |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 540 | { |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 541 | struct ec_state *ec = dev_get_priv(dev); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 542 | ulong start; |
| 543 | |
| 544 | printf("Press keys for EC to detect on reset (ESC=recovery)..."); |
| 545 | start = get_timer(0); |
| 546 | while (get_timer(start) < 1000) |
| 547 | ; |
| 548 | putc('\n'); |
| 549 | if (!sandbox_sdl_key_pressed(KEY_ESC)) { |
| 550 | ec->recovery_req = true; |
| 551 | printf(" - EC requests recovery\n"); |
| 552 | } |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 553 | } |
| 554 | |
Simon Glass | 9d70252 | 2021-01-16 14:52:28 -0700 | [diff] [blame] | 555 | /* Return the byte of EC switch states */ |
| 556 | static int cros_ec_sandbox_get_switches(struct udevice *dev) |
| 557 | { |
| 558 | struct ec_state *ec = dev_get_priv(dev); |
| 559 | |
| 560 | return ec->test_flags & CROSECT_LID_OPEN ? EC_SWITCH_LID_OPEN : 0; |
| 561 | } |
| 562 | |
Simon Glass | 9b306e5 | 2021-01-16 14:52:22 -0700 | [diff] [blame] | 563 | void sandbox_cros_ec_set_test_flags(struct udevice *dev, uint flags) |
| 564 | { |
| 565 | struct ec_state *ec = dev_get_priv(dev); |
| 566 | |
| 567 | ec->test_flags = flags; |
| 568 | } |
| 569 | |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 570 | int cros_ec_probe(struct udevice *dev) |
| 571 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 572 | struct ec_state *ec = dev_get_priv(dev); |
| 573 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
Simon Glass | 3e5a8b9 | 2016-06-19 17:33:15 -0600 | [diff] [blame] | 574 | struct udevice *keyb_dev; |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 575 | ofnode node; |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 576 | int err; |
| 577 | |
| 578 | memcpy(ec, &s_state, sizeof(*ec)); |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 579 | err = cros_ec_decode_ec_flash(dev, &ec->ec_config); |
| 580 | if (err) { |
| 581 | debug("%s: Cannot device EC flash\n", __func__); |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 582 | return err; |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 583 | } |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 584 | |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 585 | node = ofnode_null(); |
Simon Glass | 3e5a8b9 | 2016-06-19 17:33:15 -0600 | [diff] [blame] | 586 | for (device_find_first_child(dev, &keyb_dev); |
| 587 | keyb_dev; |
| 588 | device_find_next_child(&keyb_dev)) { |
| 589 | if (device_get_uclass_id(keyb_dev) == UCLASS_KEYBOARD) { |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 590 | node = dev_ofnode(keyb_dev); |
Simon Glass | 3e5a8b9 | 2016-06-19 17:33:15 -0600 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | } |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 594 | if (!ofnode_valid(node)) { |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 595 | debug("%s: No cros_ec keyboard found\n", __func__); |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 596 | } else if (keyscan_read_fdt_matrix(ec, node)) { |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 597 | debug("%s: Could not read key matrix\n", __func__); |
| 598 | return -1; |
| 599 | } |
| 600 | |
| 601 | /* If we loaded EC data, check that the length matches */ |
| 602 | if (ec->flash_data && |
| 603 | ec->flash_data_len != ec->ec_config.flash.length) { |
| 604 | printf("EC data length is %x, expected %x, discarding data\n", |
| 605 | ec->flash_data_len, ec->ec_config.flash.length); |
Simon Glass | a4f416d | 2020-02-03 07:36:18 -0700 | [diff] [blame] | 606 | free(ec->flash_data); |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 607 | ec->flash_data = NULL; |
| 608 | } |
| 609 | |
| 610 | /* Otherwise allocate the memory */ |
| 611 | if (!ec->flash_data) { |
| 612 | ec->flash_data_len = ec->ec_config.flash.length; |
Simon Glass | a4f416d | 2020-02-03 07:36:18 -0700 | [diff] [blame] | 613 | ec->flash_data = malloc(ec->flash_data_len); |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 614 | if (!ec->flash_data) |
| 615 | return -ENOMEM; |
| 616 | } |
| 617 | |
| 618 | cdev->dev = dev; |
| 619 | g_state = ec; |
| 620 | return cros_ec_register(dev); |
Simon Glass | 36170f0 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 621 | } |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 622 | |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 623 | struct dm_cros_ec_ops cros_ec_ops = { |
| 624 | .packet = cros_ec_sandbox_packet, |
Simon Glass | 9d70252 | 2021-01-16 14:52:28 -0700 | [diff] [blame] | 625 | .get_switches = cros_ec_sandbox_get_switches, |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 626 | }; |
| 627 | |
| 628 | static const struct udevice_id cros_ec_ids[] = { |
Simon Glass | 73f220c | 2015-03-26 09:29:39 -0600 | [diff] [blame] | 629 | { .compatible = "google,cros-ec-sandbox" }, |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 630 | { } |
| 631 | }; |
| 632 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 633 | U_BOOT_DRIVER(google_cros_ec_sandbox) = { |
| 634 | .name = "google_cros_ec_sandbox", |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 635 | .id = UCLASS_CROS_EC, |
| 636 | .of_match = cros_ec_ids, |
| 637 | .probe = cros_ec_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 638 | .priv_auto = sizeof(struct ec_state), |
Simon Glass | ff425d8 | 2014-10-13 23:42:15 -0600 | [diff] [blame] | 639 | .ops = &cros_ec_ops, |
| 640 | }; |