cros_ec.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. int (*check_version)(struct udevice *dev);
  213. int (*command)(struct udevice *dev, uint8_t cmd, int cmd_version,
  214. const uint8_t *dout, int dout_len,
  215. uint8_t **dinp, int din_len);
  216. int (*packet)(struct udevice *dev, int out_bytes, int in_bytes);
  217. };
  218. #define dm_cros_ec_get_ops(dev) \
  219. ((struct dm_cros_ec_ops *)(dev)->driver->ops)
  220. int cros_ec_register(struct udevice *dev);
  221. /**
  222. * Dump a block of data for a command.
  223. *
  224. * @param name Name for data (e.g. 'in', 'out')
  225. * @param cmd Command number associated with data, or -1 for none
  226. * @param data Data block to dump
  227. * @param len Length of data block to dump
  228. */
  229. void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len);
  230. /**
  231. * Calculate a simple 8-bit checksum of a data block
  232. *
  233. * @param data Data block to checksum
  234. * @param size Size of data block in bytes
  235. * @return checksum value (0 to 255)
  236. */
  237. int cros_ec_calc_checksum(const uint8_t *data, int size);
  238. int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size);
  239. /**
  240. * Read data from the flash
  241. *
  242. * Read an arbitrary amount of data from the EC flash, by repeatedly reading
  243. * small blocks.
  244. *
  245. * The offset starts at 0. You can obtain the region information from
  246. * cros_ec_flash_offset() to find out where to read for a particular region.
  247. *
  248. * @param dev CROS-EC device
  249. * @param data Pointer to data buffer to read into
  250. * @param offset Offset within flash to read from
  251. * @param size Number of bytes to read
  252. * @return 0 if ok, -1 on error
  253. */
  254. int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
  255. uint32_t size);
  256. /**
  257. * Read back flash parameters
  258. *
  259. * This function reads back parameters of the flash as reported by the EC
  260. *
  261. * @param dev Pointer to device
  262. * @param info Pointer to output flash info struct
  263. */
  264. int cros_ec_read_flashinfo(struct udevice *dev,
  265. struct ec_response_flash_info *info);
  266. /**
  267. * Write data to the flash
  268. *
  269. * Write an arbitrary amount of data to the EC flash, by repeatedly writing
  270. * small blocks.
  271. *
  272. * The offset starts at 0. You can obtain the region information from
  273. * cros_ec_flash_offset() to find out where to write for a particular region.
  274. *
  275. * Attempting to write to the region where the EC is currently running from
  276. * will result in an error.
  277. *
  278. * @param dev CROS-EC device
  279. * @param data Pointer to data buffer to write
  280. * @param offset Offset within flash to write to.
  281. * @param size Number of bytes to write
  282. * @return 0 if ok, -1 on error
  283. */
  284. int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
  285. uint32_t offset, uint32_t size);
  286. /**
  287. * Obtain position and size of a flash region
  288. *
  289. * @param dev CROS-EC device
  290. * @param region Flash region to query
  291. * @param offset Returns offset of flash region in EC flash
  292. * @param size Returns size of flash region
  293. * @return 0 if ok, -1 on error
  294. */
  295. int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
  296. uint32_t *offset, uint32_t *size);
  297. /**
  298. * Read/write non-volatile data from/to a CROS-EC device.
  299. *
  300. * @param dev CROS-EC device
  301. * @param block Buffer of VbNvContext to be read/write
  302. * @return 0 if ok, -1 on error
  303. */
  304. int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size);
  305. int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size);
  306. /**
  307. * Read the version information for the EC images
  308. *
  309. * @param dev CROS-EC device
  310. * @param versionp This is set to point to the version information
  311. * @return 0 if ok, -1 on error
  312. */
  313. int cros_ec_read_version(struct udevice *dev,
  314. struct ec_response_get_version **versionp);
  315. /**
  316. * Read the build information for the EC
  317. *
  318. * @param dev CROS-EC device
  319. * @param versionp This is set to point to the build string
  320. * @return 0 if ok, -1 on error
  321. */
  322. int cros_ec_read_build_info(struct udevice *dev, char **strp);
  323. /**
  324. * Switch on/off a LDO / FET.
  325. *
  326. * @param dev CROS-EC device
  327. * @param index index of the LDO/FET to switch
  328. * @param state new state of the LDO/FET : EC_LDO_STATE_ON|OFF
  329. * @return 0 if ok, -1 on error
  330. */
  331. int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state);
  332. /**
  333. * Read back a LDO / FET current state.
  334. *
  335. * @param dev CROS-EC device
  336. * @param index index of the LDO/FET to switch
  337. * @param state current state of the LDO/FET : EC_LDO_STATE_ON|OFF
  338. * @return 0 if ok, -1 on error
  339. */
  340. int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state);
  341. /**
  342. * Get access to the error reported when cros_ec_board_init() was called
  343. *
  344. * This permits delayed reporting of the EC error if it failed during
  345. * early init.
  346. *
  347. * @return error (0 if there was no error, -ve if there was an error)
  348. */
  349. int cros_ec_get_error(void);
  350. /**
  351. * Returns information from the FDT about the Chrome EC flash
  352. *
  353. * @param dev Device to read from
  354. * @param config Structure to use to return information
  355. */
  356. int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config);
  357. /**
  358. * Check the current keyboard state, in case recovery mode is requested.
  359. * This function is for sandbox only.
  360. *
  361. * @param ec CROS-EC device
  362. */
  363. void cros_ec_check_keyboard(struct udevice *dev);
  364. struct i2c_msg;
  365. /*
  366. * Tunnel an I2C transfer to the EC
  367. *
  368. * @param dev CROS-EC device
  369. * @param port The remote port on EC to use
  370. * @param msg List of messages to transfer
  371. * @param nmsgs Number of messages to transfer
  372. */
  373. int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *msg,
  374. int nmsgs);
  375. /**
  376. * cros_ec_get_events_b() - Get event mask B
  377. *
  378. * @return value of event mask, default value of 0 if it could not be read
  379. */
  380. uint64_t cros_ec_get_events_b(struct udevice *dev);
  381. /**
  382. * cros_ec_clear_events_b() - Clear even mask B
  383. *
  384. * Any pending events in the B range are cleared
  385. *
  386. * @return 0 if OK, -ve on error
  387. */
  388. int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask);
  389. /**
  390. * cros_ec_efs_verify() - tell the EC to verify one of its images
  391. *
  392. * @param dev CROS-EC device
  393. * @param region Flash region to query
  394. * @return 0 if OK, -ve on error
  395. */
  396. int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region);
  397. /**
  398. * cros_ec_battery_cutoff() - Request that the battery be cut off
  399. *
  400. * This tells the battery to stop supplying power. This is used before shipping
  401. * a device to ensure that the battery remains charged while the device is
  402. * shipped or sitting on the shelf waiting to be purchased.
  403. *
  404. * @param dev CROS-EC device
  405. * @param flags Flags to use (EC_BATTERY_CUTOFF_FLAG_...)
  406. * @return 0 if OK, -ve on error
  407. */
  408. int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags);
  409. /**
  410. * cros_ec_read_limit_power() - Check if power is limited by batter/charger
  411. *
  412. * Sometimes the battery is low and / or the device is connected to a charger
  413. * that cannot supply much power.
  414. *
  415. * @param dev CROS-EC device
  416. * @param limit_powerp Returns whether power is limited (0 or 1)
  417. * @return 0 if OK, -ENOSYS if the EC does not support this comment, -EINVAL
  418. * if the EC returned an invalid response
  419. */
  420. int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp);
  421. /**
  422. * cros_ec_config_powerbtn() - Configure the behaviour of the power button
  423. *
  424. * @param dev CROS-EC device
  425. * @param flags Flags to use (EC_POWER_BUTTON_...)
  426. * @return 0 if OK, -ve on error
  427. */
  428. int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags);
  429. /**
  430. * cros_ec_get_lid_shutdown_mask() - Set the lid shutdown mask
  431. *
  432. * Determines whether a lid close event is reported
  433. *
  434. * @param dev CROS-EC device
  435. * @return shufdown mas if OK, -ve on error
  436. */
  437. int cros_ec_get_lid_shutdown_mask(struct udevice *dev);
  438. /**
  439. * cros_ec_set_lid_shutdown_mask() - Set the lid shutdown mask
  440. *
  441. * Set whether a lid close event is reported
  442. *
  443. * @param dev CROS-EC device
  444. * @param enable true to enable reporting, false to disable
  445. * @return shufdown mas if OK, -ve on error
  446. */
  447. int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable);
  448. #endif