cros_ec.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. * Read which image is currently running on the CROS-EC device.
  77. *
  78. * @param dev CROS-EC device
  79. * @param image Destination for image identifier
  80. * @return 0 if ok, <0 on error
  81. */
  82. int cros_ec_read_current_image(struct udevice *dev,
  83. enum ec_current_image *image);
  84. /**
  85. * Read the hash of the CROS-EC device firmware.
  86. *
  87. * @param dev CROS-EC device
  88. * @param hash_offset Offset in flash to read from
  89. * @param hash Destination for hash information
  90. * @return 0 if ok, <0 on error
  91. */
  92. int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
  93. struct ec_response_vboot_hash *hash);
  94. /**
  95. * Send a reboot command to the CROS-EC device.
  96. *
  97. * Note that some reboot commands (such as EC_REBOOT_COLD) also reboot the AP.
  98. *
  99. * @param dev CROS-EC device
  100. * @param cmd Reboot command
  101. * @param flags Flags for reboot command (EC_REBOOT_FLAG_*)
  102. * @return 0 if ok, <0 on error
  103. */
  104. int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags);
  105. /**
  106. * Check if the CROS-EC device has an interrupt pending.
  107. *
  108. * Read the status of the external interrupt connected to the CROS-EC device.
  109. * If no external interrupt is configured, this always returns 1.
  110. *
  111. * @param dev CROS-EC device
  112. * @return 0 if no interrupt is pending
  113. */
  114. int cros_ec_interrupt_pending(struct udevice *dev);
  115. enum {
  116. CROS_EC_OK,
  117. CROS_EC_ERR = 1,
  118. CROS_EC_ERR_FDT_DECODE,
  119. CROS_EC_ERR_CHECK_VERSION,
  120. CROS_EC_ERR_READ_ID,
  121. CROS_EC_ERR_DEV_INIT,
  122. };
  123. /**
  124. * Initialise the Chromium OS EC driver
  125. *
  126. * @param blob Device tree blob containing setup information
  127. * @param cros_ecp Returns pointer to the cros_ec device, or NULL if none
  128. * @return 0 if we got an cros_ec device and all is well (or no cros_ec is
  129. * expected), -ve if we should have an cros_ec device but failed to find
  130. * one, or init failed (-CROS_EC_ERR_...).
  131. */
  132. int cros_ec_init(const void *blob, struct udevice**cros_ecp);
  133. /**
  134. * Read information about the keyboard matrix
  135. *
  136. * @param dev CROS-EC device
  137. * @param info Place to put the info structure
  138. */
  139. int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info);
  140. /**
  141. * Read the host event flags
  142. *
  143. * @param dev CROS-EC device
  144. * @param events_ptr Destination for event flags. Not changed on error.
  145. * @return 0 if ok, <0 on error
  146. */
  147. int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr);
  148. /**
  149. * Clear the specified host event flags
  150. *
  151. * @param dev CROS-EC device
  152. * @param events Event flags to clear
  153. * @return 0 if ok, <0 on error
  154. */
  155. int cros_ec_clear_host_events(struct udevice *dev, uint32_t events);
  156. /**
  157. * Get/set flash protection
  158. *
  159. * @param dev CROS-EC device
  160. * @param set_mask Mask of flags to set; if 0, just retrieves existing
  161. * protection state without changing it.
  162. * @param set_flags New flag values; only bits in set_mask are applied;
  163. * ignored if set_mask=0.
  164. * @param prot Destination for updated protection state from EC.
  165. * @return 0 if ok, <0 on error
  166. */
  167. int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
  168. uint32_t set_flags,
  169. struct ec_response_flash_protect *resp);
  170. /**
  171. * Notify EC of current boot mode
  172. *
  173. * @param dev CROS-EC device
  174. * @param vboot_mode Verified boot mode
  175. * @return 0 if ok, <0 on error
  176. */
  177. int cros_ec_entering_mode(struct udevice *dev, int mode);
  178. /**
  179. * Run internal tests on the cros_ec interface.
  180. *
  181. * @param dev CROS-EC device
  182. * @return 0 if ok, <0 if the test failed
  183. */
  184. int cros_ec_test(struct udevice *dev);
  185. /**
  186. * Update the EC RW copy.
  187. *
  188. * @param dev CROS-EC device
  189. * @param image the content to write
  190. * @param imafge_size content length
  191. * @return 0 if ok, <0 if the test failed
  192. */
  193. int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
  194. int image_size);
  195. /**
  196. * Return a pointer to the board's CROS-EC device
  197. *
  198. * @return pointer to CROS-EC device, or NULL if none is available
  199. */
  200. struct udevice *board_get_cros_ec_dev(void);
  201. struct dm_cros_ec_ops {
  202. int (*check_version)(struct udevice *dev);
  203. int (*command)(struct udevice *dev, uint8_t cmd, int cmd_version,
  204. const uint8_t *dout, int dout_len,
  205. uint8_t **dinp, int din_len);
  206. int (*packet)(struct udevice *dev, int out_bytes, int in_bytes);
  207. };
  208. #define dm_cros_ec_get_ops(dev) \
  209. ((struct dm_cros_ec_ops *)(dev)->driver->ops)
  210. int cros_ec_register(struct udevice *dev);
  211. /**
  212. * Dump a block of data for a command.
  213. *
  214. * @param name Name for data (e.g. 'in', 'out')
  215. * @param cmd Command number associated with data, or -1 for none
  216. * @param data Data block to dump
  217. * @param len Length of data block to dump
  218. */
  219. void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len);
  220. /**
  221. * Calculate a simple 8-bit checksum of a data block
  222. *
  223. * @param data Data block to checksum
  224. * @param size Size of data block in bytes
  225. * @return checksum value (0 to 255)
  226. */
  227. int cros_ec_calc_checksum(const uint8_t *data, int size);
  228. int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size);
  229. /**
  230. * Read data from the flash
  231. *
  232. * Read an arbitrary amount of data from the EC flash, by repeatedly reading
  233. * small blocks.
  234. *
  235. * The offset starts at 0. You can obtain the region information from
  236. * cros_ec_flash_offset() to find out where to read for a particular region.
  237. *
  238. * @param dev CROS-EC device
  239. * @param data Pointer to data buffer to read into
  240. * @param offset Offset within flash to read from
  241. * @param size Number of bytes to read
  242. * @return 0 if ok, -1 on error
  243. */
  244. int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
  245. uint32_t size);
  246. /**
  247. * Read back flash parameters
  248. *
  249. * This function reads back parameters of the flash as reported by the EC
  250. *
  251. * @param dev Pointer to device
  252. * @param info Pointer to output flash info struct
  253. */
  254. int cros_ec_read_flashinfo(struct udevice *dev,
  255. struct ec_response_flash_info *info);
  256. /**
  257. * Write data to the flash
  258. *
  259. * Write an arbitrary amount of data to the EC flash, by repeatedly writing
  260. * small blocks.
  261. *
  262. * The offset starts at 0. You can obtain the region information from
  263. * cros_ec_flash_offset() to find out where to write for a particular region.
  264. *
  265. * Attempting to write to the region where the EC is currently running from
  266. * will result in an error.
  267. *
  268. * @param dev CROS-EC device
  269. * @param data Pointer to data buffer to write
  270. * @param offset Offset within flash to write to.
  271. * @param size Number of bytes to write
  272. * @return 0 if ok, -1 on error
  273. */
  274. int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
  275. uint32_t offset, uint32_t size);
  276. /**
  277. * Obtain position and size of a flash region
  278. *
  279. * @param dev CROS-EC device
  280. * @param region Flash region to query
  281. * @param offset Returns offset of flash region in EC flash
  282. * @param size Returns size of flash region
  283. * @return 0 if ok, -1 on error
  284. */
  285. int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
  286. uint32_t *offset, uint32_t *size);
  287. /**
  288. * Read/write non-volatile data from/to a CROS-EC device.
  289. *
  290. * @param dev CROS-EC device
  291. * @param block Buffer of VbNvContext to be read/write
  292. * @return 0 if ok, -1 on error
  293. */
  294. int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size);
  295. int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size);
  296. /**
  297. * Read the version information for the EC images
  298. *
  299. * @param dev CROS-EC device
  300. * @param versionp This is set to point to the version information
  301. * @return 0 if ok, -1 on error
  302. */
  303. int cros_ec_read_version(struct udevice *dev,
  304. struct ec_response_get_version **versionp);
  305. /**
  306. * Read the build information for the EC
  307. *
  308. * @param dev CROS-EC device
  309. * @param versionp This is set to point to the build string
  310. * @return 0 if ok, -1 on error
  311. */
  312. int cros_ec_read_build_info(struct udevice *dev, char **strp);
  313. /**
  314. * Switch on/off a LDO / FET.
  315. *
  316. * @param dev CROS-EC device
  317. * @param index index of the LDO/FET to switch
  318. * @param state new state of the LDO/FET : EC_LDO_STATE_ON|OFF
  319. * @return 0 if ok, -1 on error
  320. */
  321. int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state);
  322. /**
  323. * Read back a LDO / FET current state.
  324. *
  325. * @param dev CROS-EC device
  326. * @param index index of the LDO/FET to switch
  327. * @param state current state of the LDO/FET : EC_LDO_STATE_ON|OFF
  328. * @return 0 if ok, -1 on error
  329. */
  330. int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state);
  331. /**
  332. * Get access to the error reported when cros_ec_board_init() was called
  333. *
  334. * This permits delayed reporting of the EC error if it failed during
  335. * early init.
  336. *
  337. * @return error (0 if there was no error, -ve if there was an error)
  338. */
  339. int cros_ec_get_error(void);
  340. /**
  341. * Returns information from the FDT about the Chrome EC flash
  342. *
  343. * @param dev Device to read from
  344. * @param config Structure to use to return information
  345. */
  346. int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config);
  347. /**
  348. * Check the current keyboard state, in case recovery mode is requested.
  349. * This function is for sandbox only.
  350. *
  351. * @param ec CROS-EC device
  352. */
  353. void cros_ec_check_keyboard(struct udevice *dev);
  354. struct i2c_msg;
  355. /*
  356. * Tunnel an I2C transfer to the EC
  357. *
  358. * @param dev CROS-EC device
  359. * @param port The remote port on EC to use
  360. * @param msg List of messages to transfer
  361. * @param nmsgs Number of messages to transfer
  362. */
  363. int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *msg,
  364. int nmsgs);
  365. /**
  366. * cros_ec_get_events_b() - Get event mask B
  367. *
  368. * @return value of event mask, default value of 0 if it could not be read
  369. */
  370. uint64_t cros_ec_get_events_b(struct udevice *dev);
  371. /**
  372. * cros_ec_clear_events_b() - Clear even mask B
  373. *
  374. * Any pending events in the B range are cleared
  375. *
  376. * @return 0 if OK, -ve on error
  377. */
  378. int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask);
  379. /**
  380. * cros_ec_efs_verify() - tell the EC to verify one of its images
  381. *
  382. * @param dev CROS-EC device
  383. * @param region Flash region to query
  384. * @return 0 if OK, -ve on error
  385. */
  386. int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region);
  387. /**
  388. * cros_ec_battery_cutoff() - Request that the battery be cut off
  389. *
  390. * This tells the battery to stop supplying power. This is used before shipping
  391. * a device to ensure that the battery remains charged while the device is
  392. * shipped or sitting on the shelf waiting to be purchased.
  393. *
  394. * @param dev CROS-EC device
  395. * @param flags Flags to use (EC_BATTERY_CUTOFF_FLAG_...)
  396. * @return 0 if OK, -ve on error
  397. */
  398. int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags);
  399. /**
  400. * cros_ec_read_limit_power() - Check if power is limited by batter/charger
  401. *
  402. * Sometimes the battery is low and / or the device is connected to a charger
  403. * that cannot supply much power.
  404. *
  405. * @param dev CROS-EC device
  406. * @param limit_powerp Returns whether power is limited (0 or 1)
  407. * @return 0 if OK, -ENOSYS if the EC does not support this comment, -EINVAL
  408. * if the EC returned an invalid response
  409. */
  410. int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp);
  411. /**
  412. * cros_ec_config_powerbtn() - Configure the behaviour of the power button
  413. *
  414. * @param dev CROS-EC device
  415. * @param flags Flags to use (EC_POWER_BUTTON_...)
  416. * @return 0 if OK, -ve on error
  417. */
  418. int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags);
  419. /**
  420. * cros_ec_get_lid_shutdown_mask() - Set the lid shutdown mask
  421. *
  422. * Determines whether a lid close event is reported
  423. *
  424. * @param dev CROS-EC device
  425. * @return shufdown mas if OK, -ve on error
  426. */
  427. int cros_ec_get_lid_shutdown_mask(struct udevice *dev);
  428. /**
  429. * cros_ec_set_lid_shutdown_mask() - Set the lid shutdown mask
  430. *
  431. * Set whether a lid close event is reported
  432. *
  433. * @param dev CROS-EC device
  434. * @param enable true to enable reporting, false to disable
  435. * @return shufdown mas if OK, -ve on error
  436. */
  437. int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable);
  438. #endif