cros_ec.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Chromium OS cros_ec driver
  4. *
  5. * Copyright (c) 2012 The Chromium OS Authors.
  6. */
  7. #ifndef _CROS_EC_H
  8. #define _CROS_EC_H
  9. #include <linux/compiler.h>
  10. #include <ec_commands.h>
  11. #include <cros_ec_message.h>
  12. #include <asm/gpio.h>
  13. #include <dm/of_extra.h>
  14. /* Our configuration information */
  15. struct cros_ec_dev {
  16. struct udevice *dev; /* Transport device */
  17. struct gpio_desc ec_int; /* GPIO used as EC interrupt line */
  18. int protocol_version; /* Protocol version to use */
  19. int optimise_flash_write; /* Don't write erased flash blocks */
  20. /*
  21. * These two buffers will always be dword-aligned and include enough
  22. * space for up to 7 word-alignment bytes also, so we can ensure that
  23. * the body of the message is always dword-aligned (64-bit).
  24. *
  25. * We use this alignment to keep ARM and x86 happy. Probably word
  26. * alignment would be OK, there might be a small performance advantage
  27. * to using dword.
  28. */
  29. uint8_t din[ALIGN(MSG_BYTES + sizeof(int64_t), sizeof(int64_t))]
  30. __aligned(sizeof(int64_t));
  31. uint8_t dout[ALIGN(MSG_BYTES + sizeof(int64_t), sizeof(int64_t))]
  32. __aligned(sizeof(int64_t));
  33. };
  34. /*
  35. * Hard-code the number of columns we happen to know we have right now. It
  36. * would be more correct to call cros_ec_info() at startup and determine the
  37. * actual number of keyboard cols from there.
  38. */
  39. #define CROS_EC_KEYSCAN_COLS 13
  40. /* Information returned by a key scan */
  41. struct mbkp_keyscan {
  42. uint8_t data[CROS_EC_KEYSCAN_COLS];
  43. };
  44. /* Holds information about the Chrome EC */
  45. struct fdt_cros_ec {
  46. struct fmap_entry flash; /* Address and size of EC flash */
  47. /*
  48. * Byte value of erased flash, or -1 if not known. It is normally
  49. * 0xff but some flash devices use 0 (e.g. STM32Lxxx)
  50. */
  51. int flash_erase_value;
  52. struct fmap_entry region[EC_FLASH_REGION_COUNT];
  53. };
  54. /**
  55. * Read the ID of the CROS-EC device
  56. *
  57. * The ID is a string identifying the CROS-EC device.
  58. *
  59. * @param dev CROS-EC device
  60. * @param id Place to put the ID
  61. * @param maxlen Maximum length of the ID field
  62. * @return 0 if ok, -1 on error
  63. */
  64. int cros_ec_read_id(struct udevice *dev, char *id, int maxlen);
  65. /**
  66. * Read a keyboard scan from the CROS-EC device
  67. *
  68. * Send a message requesting a keyboard scan and return the result
  69. *
  70. * @param dev CROS-EC device
  71. * @param scan Place to put the scan results
  72. * @return 0 if ok, -1 on error
  73. */
  74. int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan);
  75. /**
  76. * Get the next pending MKBP event from the ChromeOS EC device.
  77. *
  78. * Send a message requesting the next event and return the result.
  79. *
  80. * @param event Place to put the event.
  81. * @return 0 if ok, <0 on error.
  82. */
  83. int cros_ec_get_next_event(struct udevice *dev,
  84. struct ec_response_get_next_event *event);
  85. /**
  86. * Read which image is currently running on the CROS-EC device.
  87. *
  88. * @param dev CROS-EC device
  89. * @param image Destination for image identifier
  90. * @return 0 if ok, <0 on error
  91. */
  92. int cros_ec_read_current_image(struct udevice *dev,
  93. enum ec_current_image *image);
  94. /**
  95. * Read the hash of the CROS-EC device firmware.
  96. *
  97. * @param dev CROS-EC device
  98. * @param hash_offset Offset in flash to read from
  99. * @param hash Destination for hash information
  100. * @return 0 if ok, <0 on error
  101. */
  102. int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
  103. struct ec_response_vboot_hash *hash);
  104. /**
  105. * Send a reboot command to the CROS-EC device.
  106. *
  107. * Note that some reboot commands (such as EC_REBOOT_COLD) also reboot the AP.
  108. *
  109. * @param dev CROS-EC device
  110. * @param cmd Reboot command
  111. * @param flags Flags for reboot command (EC_REBOOT_FLAG_*)
  112. * @return 0 if ok, <0 on error
  113. */
  114. int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags);
  115. /**
  116. * Check if the CROS-EC device has an interrupt pending.
  117. *
  118. * Read the status of the external interrupt connected to the CROS-EC device.
  119. * If no external interrupt is configured, this always returns 1.
  120. *
  121. * @param dev CROS-EC device
  122. * @return 0 if no interrupt is pending
  123. */
  124. int cros_ec_interrupt_pending(struct udevice *dev);
  125. enum {
  126. CROS_EC_OK,
  127. CROS_EC_ERR = 1,
  128. CROS_EC_ERR_FDT_DECODE,
  129. CROS_EC_ERR_CHECK_VERSION,
  130. CROS_EC_ERR_READ_ID,
  131. CROS_EC_ERR_DEV_INIT,
  132. };
  133. /**
  134. * Initialise the Chromium OS EC driver
  135. *
  136. * @param blob Device tree blob containing setup information
  137. * @param cros_ecp Returns pointer to the cros_ec device, or NULL if none
  138. * @return 0 if we got an cros_ec device and all is well (or no cros_ec is
  139. * expected), -ve if we should have an cros_ec device but failed to find
  140. * one, or init failed (-CROS_EC_ERR_...).
  141. */
  142. int cros_ec_init(const void *blob, struct udevice**cros_ecp);
  143. /**
  144. * Read information about the keyboard matrix
  145. *
  146. * @param dev CROS-EC device
  147. * @param info Place to put the info structure
  148. */
  149. int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info);
  150. /**
  151. * Read the host event flags
  152. *
  153. * @param dev CROS-EC device
  154. * @param events_ptr Destination for event flags. Not changed on error.
  155. * @return 0 if ok, <0 on error
  156. */
  157. int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr);
  158. /**
  159. * Clear the specified host event flags
  160. *
  161. * @param dev CROS-EC device
  162. * @param events Event flags to clear
  163. * @return 0 if ok, <0 on error
  164. */
  165. int cros_ec_clear_host_events(struct udevice *dev, uint32_t events);
  166. /**
  167. * Get/set flash protection
  168. *
  169. * @param dev CROS-EC device
  170. * @param set_mask Mask of flags to set; if 0, just retrieves existing
  171. * protection state without changing it.
  172. * @param set_flags New flag values; only bits in set_mask are applied;
  173. * ignored if set_mask=0.
  174. * @param prot Destination for updated protection state from EC.
  175. * @return 0 if ok, <0 on error
  176. */
  177. int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
  178. uint32_t set_flags,
  179. struct ec_response_flash_protect *resp);
  180. /**
  181. * Notify EC of current boot mode
  182. *
  183. * @param dev CROS-EC device
  184. * @param vboot_mode Verified boot mode
  185. * @return 0 if ok, <0 on error
  186. */
  187. int cros_ec_entering_mode(struct udevice *dev, int mode);
  188. /**
  189. * Run internal tests on the cros_ec interface.
  190. *
  191. * @param dev CROS-EC device
  192. * @return 0 if ok, <0 if the test failed
  193. */
  194. int cros_ec_test(struct udevice *dev);
  195. /**
  196. * Update the EC RW copy.
  197. *
  198. * @param dev CROS-EC device
  199. * @param image the content to write
  200. * @param imafge_size content length
  201. * @return 0 if ok, <0 if the test failed
  202. */
  203. int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
  204. int image_size);
  205. /**
  206. * Return a pointer to the board's CROS-EC device
  207. *
  208. * @return pointer to CROS-EC device, or NULL if none is available
  209. */
  210. struct udevice *board_get_cros_ec_dev(void);
  211. struct dm_cros_ec_ops {
  212. /**
  213. * check_version() - Check the protocol version being used (optional)
  214. *
  215. * If provided, this function should check that the EC can be supported
  216. * by the driver. If not provided, HELLO messages will be sent to try
  217. * to determine the protocol version.
  218. *
  219. * @dev: Device to check
  220. * @return 0 if the protocol is valid, -ve if not supported
  221. */
  222. int (*check_version)(struct udevice *dev);
  223. /**
  224. * command() - Old-style command interface
  225. *
  226. * This sends a command and receives a response (deprecated, use
  227. * packet())
  228. *
  229. * @dev: Device to use
  230. * @cmd: Command to send (only supports 0-0xff)
  231. * @cmd_version: Version of command to send (often 0)
  232. * @dout: Output data (may be NULL If dout_len=0)
  233. * @dout_len: Length of output data excluding 4-byte header
  234. * @dinp: On input, set to point to input data, often struct
  235. * cros_ec_dev->din - typically this is left alone but may be
  236. * updated by the driver
  237. * @din_len: Maximum length of response
  238. * @return number of bytes in response, or -ve on error
  239. */
  240. int (*command)(struct udevice *dev, uint8_t cmd, int cmd_version,
  241. const uint8_t *dout, int dout_len,
  242. uint8_t **dinp, int din_len);
  243. /**
  244. * packet() - New-style command interface
  245. *
  246. * This interface is preferred over command(), since it is typically
  247. * easier to implement.
  248. *
  249. * @dev: Device to use
  250. * @out_bytes: Number of bytes to send (from struct cros_ec_dev->dout)
  251. * @in_bytes: Maximum number of bytes to expect in response
  252. * @return number of bytes in response, or -ve on error
  253. */
  254. int (*packet)(struct udevice *dev, int out_bytes, int in_bytes);
  255. /**
  256. * get_switches() - Get value of EC switches
  257. *
  258. * This is currently supported on the LPC EC.
  259. *
  260. * @dev: Device to use
  261. * @return current switches value, or -ENOSYS if not supported
  262. */
  263. int (*get_switches)(struct udevice *dev);
  264. };
  265. #define dm_cros_ec_get_ops(dev) \
  266. ((struct dm_cros_ec_ops *)(dev)->driver->ops)
  267. int cros_ec_register(struct udevice *dev);
  268. /**
  269. * Dump a block of data for a command.
  270. *
  271. * @param name Name for data (e.g. 'in', 'out')
  272. * @param cmd Command number associated with data, or -1 for none
  273. * @param data Data block to dump
  274. * @param len Length of data block to dump
  275. */
  276. void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len);
  277. /**
  278. * Calculate a simple 8-bit checksum of a data block
  279. *
  280. * @param data Data block to checksum
  281. * @param size Size of data block in bytes
  282. * @return checksum value (0 to 255)
  283. */
  284. int cros_ec_calc_checksum(const uint8_t *data, int size);
  285. int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size);
  286. /**
  287. * Read data from the flash
  288. *
  289. * Read an arbitrary amount of data from the EC flash, by repeatedly reading
  290. * small blocks.
  291. *
  292. * The offset starts at 0. You can obtain the region information from
  293. * cros_ec_flash_offset() to find out where to read for a particular region.
  294. *
  295. * @param dev CROS-EC device
  296. * @param data Pointer to data buffer to read into
  297. * @param offset Offset within flash to read from
  298. * @param size Number of bytes to read
  299. * @return 0 if ok, -1 on error
  300. */
  301. int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
  302. uint32_t size);
  303. /**
  304. * Read back flash parameters
  305. *
  306. * This function reads back parameters of the flash as reported by the EC
  307. *
  308. * @param dev Pointer to device
  309. * @param info Pointer to output flash info struct
  310. */
  311. int cros_ec_read_flashinfo(struct udevice *dev,
  312. struct ec_response_flash_info *info);
  313. /**
  314. * Write data to the flash
  315. *
  316. * Write an arbitrary amount of data to the EC flash, by repeatedly writing
  317. * small blocks.
  318. *
  319. * The offset starts at 0. You can obtain the region information from
  320. * cros_ec_flash_offset() to find out where to write for a particular region.
  321. *
  322. * Attempting to write to the region where the EC is currently running from
  323. * will result in an error.
  324. *
  325. * @param dev CROS-EC device
  326. * @param data Pointer to data buffer to write
  327. * @param offset Offset within flash to write to.
  328. * @param size Number of bytes to write
  329. * @return 0 if ok, -1 on error
  330. */
  331. int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
  332. uint32_t offset, uint32_t size);
  333. /**
  334. * Obtain position and size of a flash region
  335. *
  336. * @param dev CROS-EC device
  337. * @param region Flash region to query
  338. * @param offset Returns offset of flash region in EC flash
  339. * @param size Returns size of flash region
  340. * @return 0 if ok, -1 on error
  341. */
  342. int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
  343. uint32_t *offset, uint32_t *size);
  344. /**
  345. * cros_ec_get_sku_id() - Read the SKU ID
  346. *
  347. * @dev: CROS-EC device
  348. * return SKU ID, or -ve on error
  349. */
  350. int cros_ec_get_sku_id(struct udevice *dev);
  351. /**
  352. * Read/write non-volatile data from/to a CROS-EC device.
  353. *
  354. * @param dev CROS-EC device
  355. * @param block Buffer of VbNvContext to be read/write
  356. * @return 0 if ok, -1 on error
  357. */
  358. int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size);
  359. int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size);
  360. /**
  361. * Read the version information for the EC images
  362. *
  363. * @param dev CROS-EC device
  364. * @param versionp This is set to point to the version information
  365. * @return 0 if ok, -1 on error
  366. */
  367. int cros_ec_read_version(struct udevice *dev,
  368. struct ec_response_get_version **versionp);
  369. /**
  370. * Read the build information for the EC
  371. *
  372. * @param dev CROS-EC device
  373. * @param versionp This is set to point to the build string
  374. * @return 0 if ok, -1 on error
  375. */
  376. int cros_ec_read_build_info(struct udevice *dev, char **strp);
  377. /**
  378. * Switch on/off a LDO / FET.
  379. *
  380. * @param dev CROS-EC device
  381. * @param index index of the LDO/FET to switch
  382. * @param state new state of the LDO/FET : EC_LDO_STATE_ON|OFF
  383. * @return 0 if ok, -1 on error
  384. */
  385. int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state);
  386. /**
  387. * Read back a LDO / FET current state.
  388. *
  389. * @param dev CROS-EC device
  390. * @param index index of the LDO/FET to switch
  391. * @param state current state of the LDO/FET : EC_LDO_STATE_ON|OFF
  392. * @return 0 if ok, -1 on error
  393. */
  394. int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state);
  395. /**
  396. * Get access to the error reported when cros_ec_board_init() was called
  397. *
  398. * This permits delayed reporting of the EC error if it failed during
  399. * early init.
  400. *
  401. * @return error (0 if there was no error, -ve if there was an error)
  402. */
  403. int cros_ec_get_error(void);
  404. /**
  405. * Returns information from the FDT about the Chrome EC flash
  406. *
  407. * @param dev Device to read from
  408. * @param config Structure to use to return information
  409. */
  410. int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config);
  411. /**
  412. * Check the current keyboard state, in case recovery mode is requested.
  413. * This function is for sandbox only.
  414. *
  415. * @param ec CROS-EC device
  416. */
  417. void cros_ec_check_keyboard(struct udevice *dev);
  418. struct i2c_msg;
  419. /*
  420. * Tunnel an I2C transfer to the EC
  421. *
  422. * @param dev CROS-EC device
  423. * @param port The remote port on EC to use
  424. * @param msg List of messages to transfer
  425. * @param nmsgs Number of messages to transfer
  426. */
  427. int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *msg,
  428. int nmsgs);
  429. /**
  430. * cros_ec_get_events_b() - Get event mask B
  431. *
  432. * @return value of event mask, default value of 0 if it could not be read
  433. */
  434. uint64_t cros_ec_get_events_b(struct udevice *dev);
  435. /**
  436. * cros_ec_clear_events_b() - Clear even mask B
  437. *
  438. * Any pending events in the B range are cleared
  439. *
  440. * @return 0 if OK, -ve on error
  441. */
  442. int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask);
  443. /**
  444. * cros_ec_efs_verify() - tell the EC to verify one of its images
  445. *
  446. * @param dev CROS-EC device
  447. * @param region Flash region to query
  448. * @return 0 if OK, -ve on error
  449. */
  450. int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region);
  451. /**
  452. * cros_ec_battery_cutoff() - Request that the battery be cut off
  453. *
  454. * This tells the battery to stop supplying power. This is used before shipping
  455. * a device to ensure that the battery remains charged while the device is
  456. * shipped or sitting on the shelf waiting to be purchased.
  457. *
  458. * @param dev CROS-EC device
  459. * @param flags Flags to use (EC_BATTERY_CUTOFF_FLAG_...)
  460. * @return 0 if OK, -ve on error
  461. */
  462. int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags);
  463. /**
  464. * cros_ec_read_limit_power() - Check if power is limited by batter/charger
  465. *
  466. * Sometimes the battery is low and / or the device is connected to a charger
  467. * that cannot supply much power.
  468. *
  469. * @param dev CROS-EC device
  470. * @param limit_powerp Returns whether power is limited (0 or 1)
  471. * @return 0 if OK, -ENOSYS if the EC does not support this comment, -EINVAL
  472. * if the EC returned an invalid response
  473. */
  474. int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp);
  475. /**
  476. * cros_ec_config_powerbtn() - Configure the behaviour of the power button
  477. *
  478. * @param dev CROS-EC device
  479. * @param flags Flags to use (EC_POWER_BUTTON_...)
  480. * @return 0 if OK, -ve on error
  481. */
  482. int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags);
  483. /**
  484. * cros_ec_get_lid_shutdown_mask() - Set the lid shutdown mask
  485. *
  486. * Determines whether a lid close event is reported
  487. *
  488. * @param dev CROS-EC device
  489. * @return shufdown mas if OK, -ve on error
  490. */
  491. int cros_ec_get_lid_shutdown_mask(struct udevice *dev);
  492. /**
  493. * cros_ec_set_lid_shutdown_mask() - Set the lid shutdown mask
  494. *
  495. * Set whether a lid close event is reported
  496. *
  497. * @param dev CROS-EC device
  498. * @param enable true to enable reporting, false to disable
  499. * @return shufdown mas if OK, -ve on error
  500. */
  501. int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable);
  502. /**
  503. * cros_ec_hello() - Send a hello message
  504. *
  505. * Sends a message with a fixed input value and checks that the expected output
  506. * value is received
  507. *
  508. * @dev: CROS-EC device
  509. * @handshakep: If non-NULL, returns received handshake value on error
  510. * @return 0 if OK, -ve on error
  511. */
  512. int cros_ec_hello(struct udevice *dev, uint *handshakep);
  513. /**
  514. * cros_ec_get_features() - Get the set of features provided by the EC
  515. *
  516. * See enum ec_feature_code for the list of available features
  517. *
  518. * @dev: CROS-EC device
  519. * @featuresp: Returns a bitmask of supported features
  520. * @return 0 if OK, -ve on error
  521. */
  522. int cros_ec_get_features(struct udevice *dev, u64 *featuresp);
  523. /**
  524. * cros_ec_check_feature() - Check if a feature is supported
  525. *
  526. * @dev: CROS-EC device
  527. * @feature: Feature number to check (enum ec_feature_code)
  528. * @return true if supported, false if not, -ve on error
  529. */
  530. int cros_ec_check_feature(struct udevice *dev, uint feature);
  531. /**
  532. * cros_ec_get_switches() - Get switches value
  533. *
  534. * @dev: CROS-EC device
  535. * @return switches value, or -ENOSYS if not supported, or other -ve value on
  536. * other error
  537. */
  538. int cros_ec_get_switches(struct udevice *dev);
  539. /**
  540. * cros_ec_vstore_supported() - Check if vstore is supported
  541. *
  542. * @dev: CROS-EC device
  543. * @return false if not supported, true if supported, -ve on error
  544. */
  545. int cros_ec_vstore_supported(struct udevice *dev);
  546. /**
  547. * cros_ec_vstore_info() - Get vstore information
  548. *
  549. * @dev: CROS-EC device
  550. * @lockedp: mask of locked slots
  551. * @return number of vstore slots supported by the EC,, -ve on error
  552. */
  553. int cros_ec_vstore_info(struct udevice *dev, u32 *lockedp);
  554. /**
  555. * cros_ec_vstore_read() - Read data from EC vstore slot
  556. *
  557. * @dev: CROS-EC device
  558. * @slot: vstore slot to read from
  559. * @data: buffer to store read data, must be EC_VSTORE_SLOT_SIZE bytes
  560. * @return 0 if OK, -ve on error
  561. */
  562. int cros_ec_vstore_read(struct udevice *dev, int slot, uint8_t *data);
  563. /**
  564. * cros_ec_vstore_write() - Save data into EC vstore slot
  565. *
  566. * The maximum size of data is EC_VSTORE_SLOT_SIZE. It is the caller's
  567. * responsibility to check the number of implemented slots by querying the
  568. * vstore info.
  569. *
  570. * @dev: CROS-EC device
  571. * @slot: vstore slot to write into
  572. * @data: data to write
  573. * @size: size of data in bytes
  574. * @return 0 if OK, -ve on error
  575. */
  576. int cros_ec_vstore_write(struct udevice *dev, int slot, const uint8_t *data,
  577. size_t size);
  578. #endif