cros_ec_sandbox.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. memcpy(resp->block, ec->vbnv_context,
  306. EC_VBNV_BLOCK_SIZE_V2);
  307. len = EC_VBNV_BLOCK_SIZE_V2;
  308. break;
  309. case EC_VBNV_CONTEXT_OP_WRITE:
  310. memcpy(ec->vbnv_context, req->block,
  311. EC_VBNV_BLOCK_SIZE_V2);
  312. len = 0;
  313. break;
  314. default:
  315. printf(" ** Unknown vbnv_context command %#02x\n",
  316. req->op);
  317. return -1;
  318. }
  319. break;
  320. }
  321. case EC_CMD_REBOOT_EC: {
  322. const struct ec_params_reboot_ec *req = req_data;
  323. printf("Request reboot type %d\n", req->cmd);
  324. switch (req->cmd) {
  325. case EC_REBOOT_DISABLE_JUMP:
  326. len = 0;
  327. break;
  328. case EC_REBOOT_JUMP_RW:
  329. ec->current_image = EC_IMAGE_RW;
  330. len = 0;
  331. break;
  332. default:
  333. puts(" ** Unknown type");
  334. return -1;
  335. }
  336. break;
  337. }
  338. case EC_CMD_HOST_EVENT_GET_B: {
  339. struct ec_response_host_event_mask *resp = resp_data;
  340. resp->mask = 0;
  341. if (ec->recovery_req) {
  342. resp->mask |= EC_HOST_EVENT_MASK(
  343. EC_HOST_EVENT_KEYBOARD_RECOVERY);
  344. }
  345. if (ec->test_flags & CROSECT_LID_OPEN)
  346. resp->mask |=
  347. EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN);
  348. len = sizeof(*resp);
  349. break;
  350. }
  351. case EC_CMD_HOST_EVENT_CLEAR_B: {
  352. const struct ec_params_host_event_mask *req = req_data;
  353. if (req->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN))
  354. ec->test_flags &= ~CROSECT_LID_OPEN;
  355. len = 0;
  356. break;
  357. }
  358. case EC_CMD_VBOOT_HASH: {
  359. const struct ec_params_vboot_hash *req = req_data;
  360. struct ec_response_vboot_hash *resp = resp_data;
  361. struct fmap_entry *entry;
  362. int ret, size;
  363. entry = &ec->ec_config.region[EC_FLASH_REGION_ACTIVE];
  364. switch (req->cmd) {
  365. case EC_VBOOT_HASH_RECALC:
  366. case EC_VBOOT_HASH_GET:
  367. size = SHA256_SUM_LEN;
  368. len = get_image_used(ec, entry);
  369. ret = hash_block("sha256",
  370. ec->flash_data + entry->offset,
  371. len, resp->hash_digest, &size);
  372. if (ret) {
  373. printf(" ** hash_block() failed\n");
  374. return -1;
  375. }
  376. resp->status = EC_VBOOT_HASH_STATUS_DONE;
  377. resp->hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  378. resp->digest_size = size;
  379. resp->reserved0 = 0;
  380. resp->offset = entry->offset;
  381. resp->size = len;
  382. len = sizeof(*resp);
  383. break;
  384. default:
  385. printf(" ** EC_CMD_VBOOT_HASH: Unknown command %d\n",
  386. req->cmd);
  387. return -1;
  388. }
  389. break;
  390. }
  391. case EC_CMD_FLASH_PROTECT: {
  392. const struct ec_params_flash_protect *req = req_data;
  393. struct ec_response_flash_protect *resp = resp_data;
  394. uint32_t expect = EC_FLASH_PROTECT_ALL_NOW |
  395. EC_FLASH_PROTECT_ALL_AT_BOOT;
  396. printf("mask=%#x, flags=%#x\n", req->mask, req->flags);
  397. if (req->flags == expect || req->flags == 0) {
  398. resp->flags = req->flags ? EC_FLASH_PROTECT_ALL_NOW :
  399. 0;
  400. resp->valid_flags = EC_FLASH_PROTECT_ALL_NOW;
  401. resp->writable_flags = 0;
  402. len = sizeof(*resp);
  403. } else {
  404. puts(" ** unexpected flash protect request\n");
  405. return -1;
  406. }
  407. break;
  408. }
  409. case EC_CMD_FLASH_REGION_INFO: {
  410. const struct ec_params_flash_region_info *req = req_data;
  411. struct ec_response_flash_region_info *resp = resp_data;
  412. struct fmap_entry *entry;
  413. switch (req->region) {
  414. case EC_FLASH_REGION_RO:
  415. case EC_FLASH_REGION_ACTIVE:
  416. case EC_FLASH_REGION_WP_RO:
  417. entry = &ec->ec_config.region[req->region];
  418. resp->offset = entry->offset;
  419. resp->size = entry->length;
  420. len = sizeof(*resp);
  421. printf("EC flash region %d: offset=%#x, size=%#x\n",
  422. req->region, resp->offset, resp->size);
  423. break;
  424. default:
  425. printf("** Unknown flash region %d\n", req->region);
  426. return -1;
  427. }
  428. break;
  429. }
  430. case EC_CMD_FLASH_ERASE: {
  431. const struct ec_params_flash_erase *req = req_data;
  432. memset(ec->flash_data + req->offset,
  433. ec->ec_config.flash_erase_value,
  434. req->size);
  435. len = 0;
  436. break;
  437. }
  438. case EC_CMD_FLASH_WRITE: {
  439. const struct ec_params_flash_write *req = req_data;
  440. memcpy(ec->flash_data + req->offset, req + 1, req->size);
  441. len = 0;
  442. break;
  443. }
  444. case EC_CMD_MKBP_STATE:
  445. len = cros_ec_keyscan(ec, resp_data);
  446. break;
  447. case EC_CMD_GET_NEXT_EVENT: {
  448. struct ec_response_get_next_event *resp = resp_data;
  449. resp->event_type = EC_MKBP_EVENT_KEY_MATRIX;
  450. cros_ec_keyscan(ec, resp->data.key_matrix);
  451. len = sizeof(*resp);
  452. break;
  453. }
  454. case EC_CMD_GET_SKU_ID: {
  455. struct ec_sku_id_info *resp = resp_data;
  456. resp->sku_id = 1234;
  457. len = sizeof(*resp);
  458. break;
  459. }
  460. case EC_CMD_GET_FEATURES: {
  461. struct ec_response_get_features *resp = resp_data;
  462. resp->flags[0] = EC_FEATURE_MASK_0(EC_FEATURE_FLASH) |
  463. EC_FEATURE_MASK_0(EC_FEATURE_I2C) |
  464. EC_FEATURE_MASK_0(EC_FEATURE_VSTORE);
  465. resp->flags[1] =
  466. EC_FEATURE_MASK_1(EC_FEATURE_UNIFIED_WAKE_MASKS) |
  467. EC_FEATURE_MASK_1(EC_FEATURE_ISH);
  468. len = sizeof(*resp);
  469. break;
  470. }
  471. case EC_CMD_VSTORE_INFO: {
  472. struct ec_response_vstore_info *resp = resp_data;
  473. int i;
  474. resp->slot_count = VSTORE_SLOT_COUNT;
  475. resp->slot_locked = 0;
  476. for (i = 0; i < VSTORE_SLOT_COUNT; i++) {
  477. if (ec->slot[i].locked)
  478. resp->slot_locked |= 1 << i;
  479. }
  480. len = sizeof(*resp);
  481. break;
  482. };
  483. case EC_CMD_VSTORE_WRITE: {
  484. const struct ec_params_vstore_write *req = req_data;
  485. struct vstore_slot *slot;
  486. if (req->slot >= EC_VSTORE_SLOT_MAX)
  487. return -EINVAL;
  488. slot = &ec->slot[req->slot];
  489. slot->locked = true;
  490. memcpy(slot->data, req->data, EC_VSTORE_SLOT_SIZE);
  491. len = 0;
  492. break;
  493. }
  494. case EC_CMD_VSTORE_READ: {
  495. const struct ec_params_vstore_read *req = req_data;
  496. struct ec_response_vstore_read *resp = resp_data;
  497. struct vstore_slot *slot;
  498. if (req->slot >= EC_VSTORE_SLOT_MAX)
  499. return -EINVAL;
  500. slot = &ec->slot[req->slot];
  501. memcpy(resp->data, slot->data, EC_VSTORE_SLOT_SIZE);
  502. len = sizeof(*resp);
  503. break;
  504. }
  505. case EC_CMD_PWM_GET_DUTY: {
  506. const struct ec_params_pwm_get_duty *req = req_data;
  507. struct ec_response_pwm_get_duty *resp = resp_data;
  508. struct ec_pwm_channel *pwm;
  509. if (req->pwm_type != EC_PWM_TYPE_GENERIC)
  510. return -EINVAL;
  511. if (req->index >= PWM_CHANNEL_COUNT)
  512. return -EINVAL;
  513. pwm = &ec->pwm[req->index];
  514. resp->duty = pwm->duty;
  515. len = sizeof(*resp);
  516. break;
  517. }
  518. case EC_CMD_PWM_SET_DUTY: {
  519. const struct ec_params_pwm_set_duty *req = req_data;
  520. struct ec_pwm_channel *pwm;
  521. if (req->pwm_type != EC_PWM_TYPE_GENERIC)
  522. return -EINVAL;
  523. if (req->index >= PWM_CHANNEL_COUNT)
  524. return -EINVAL;
  525. pwm = &ec->pwm[req->index];
  526. pwm->duty = req->duty;
  527. len = 0;
  528. break;
  529. }
  530. default:
  531. printf(" ** Unknown EC command %#02x\n", req_hdr->command);
  532. return -1;
  533. }
  534. return len;
  535. }
  536. int cros_ec_sandbox_packet(struct udevice *udev, int out_bytes, int in_bytes)
  537. {
  538. struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
  539. struct ec_state *ec = dev_get_priv(dev->dev);
  540. struct ec_host_request *req_hdr = (struct ec_host_request *)dev->dout;
  541. const void *req_data = req_hdr + 1;
  542. struct ec_host_response *resp_hdr = (struct ec_host_response *)dev->din;
  543. void *resp_data = resp_hdr + 1;
  544. int len;
  545. len = process_cmd(ec, req_hdr, req_data, resp_hdr, resp_data);
  546. if (len < 0)
  547. return len;
  548. resp_hdr->struct_version = 3;
  549. resp_hdr->result = EC_RES_SUCCESS;
  550. resp_hdr->data_len = len;
  551. resp_hdr->reserved = 0;
  552. len += sizeof(*resp_hdr);
  553. resp_hdr->checksum = 0;
  554. resp_hdr->checksum = (uint8_t)
  555. -cros_ec_calc_checksum((const uint8_t *)resp_hdr, len);
  556. return in_bytes;
  557. }
  558. void cros_ec_check_keyboard(struct udevice *dev)
  559. {
  560. struct ec_state *ec = dev_get_priv(dev);
  561. ulong start;
  562. printf("\nPress keys for EC to detect on reset (ESC=recovery)...");
  563. start = get_timer(0);
  564. while (get_timer(start) < 2000) {
  565. if (tstc()) {
  566. int ch = getchar();
  567. if (ch == 0x1b) {
  568. ec->recovery_req = true;
  569. printf("EC requests recovery");
  570. }
  571. }
  572. }
  573. putc('\n');
  574. }
  575. /* Return the byte of EC switch states */
  576. static int cros_ec_sandbox_get_switches(struct udevice *dev)
  577. {
  578. struct ec_state *ec = dev_get_priv(dev);
  579. return ec->test_flags & CROSECT_LID_OPEN ? EC_SWITCH_LID_OPEN : 0;
  580. }
  581. void sandbox_cros_ec_set_test_flags(struct udevice *dev, uint flags)
  582. {
  583. struct ec_state *ec = dev_get_priv(dev);
  584. ec->test_flags = flags;
  585. }
  586. int sandbox_cros_ec_get_pwm_duty(struct udevice *dev, uint index, uint *duty)
  587. {
  588. struct ec_state *ec = dev_get_priv(dev);
  589. struct ec_pwm_channel *pwm;
  590. if (index >= PWM_CHANNEL_COUNT)
  591. return -ENOSPC;
  592. pwm = &ec->pwm[index];
  593. *duty = pwm->duty;
  594. return 0;
  595. }
  596. int cros_ec_probe(struct udevice *dev)
  597. {
  598. struct ec_state *ec = dev_get_priv(dev);
  599. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  600. struct udevice *keyb_dev;
  601. ofnode node;
  602. int err;
  603. memcpy(ec, &s_state, sizeof(*ec));
  604. err = cros_ec_decode_ec_flash(dev, &ec->ec_config);
  605. if (err) {
  606. debug("%s: Cannot device EC flash\n", __func__);
  607. return err;
  608. }
  609. node = ofnode_null();
  610. for (device_find_first_child(dev, &keyb_dev);
  611. keyb_dev;
  612. device_find_next_child(&keyb_dev)) {
  613. if (device_get_uclass_id(keyb_dev) == UCLASS_KEYBOARD) {
  614. node = dev_ofnode(keyb_dev);
  615. break;
  616. }
  617. }
  618. if (!ofnode_valid(node)) {
  619. debug("%s: No cros_ec keyboard found\n", __func__);
  620. } else if (keyscan_read_fdt_matrix(ec, node)) {
  621. debug("%s: Could not read key matrix\n", __func__);
  622. return -1;
  623. }
  624. /* If we loaded EC data, check that the length matches */
  625. if (ec->flash_data &&
  626. ec->flash_data_len != ec->ec_config.flash.length) {
  627. printf("EC data length is %x, expected %x, discarding data\n",
  628. ec->flash_data_len, ec->ec_config.flash.length);
  629. free(ec->flash_data);
  630. ec->flash_data = NULL;
  631. }
  632. /* Otherwise allocate the memory */
  633. if (!ec->flash_data) {
  634. ec->flash_data_len = ec->ec_config.flash.length;
  635. ec->flash_data = malloc(ec->flash_data_len);
  636. if (!ec->flash_data)
  637. return -ENOMEM;
  638. }
  639. cdev->dev = dev;
  640. g_state = ec;
  641. return cros_ec_register(dev);
  642. }
  643. struct dm_cros_ec_ops cros_ec_ops = {
  644. .packet = cros_ec_sandbox_packet,
  645. .get_switches = cros_ec_sandbox_get_switches,
  646. };
  647. static const struct udevice_id cros_ec_ids[] = {
  648. { .compatible = "google,cros-ec-sandbox" },
  649. { }
  650. };
  651. U_BOOT_DRIVER(google_cros_ec_sandbox) = {
  652. .name = "google_cros_ec_sandbox",
  653. .id = UCLASS_CROS_EC,
  654. .of_match = cros_ec_ids,
  655. .probe = cros_ec_probe,
  656. .priv_auto = sizeof(struct ec_state),
  657. .ops = &cros_ec_ops,
  658. };