cros_ec_sandbox.c 20 KB

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