cros_ec.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Chromium OS cros_ec driver
  3. *
  4. * Copyright (c) 2012 The Chromium OS Authors.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #ifndef _CROS_EC_H
  9. #define _CROS_EC_H
  10. #include <linux/compiler.h>
  11. #include <ec_commands.h>
  12. #include <fdtdec.h>
  13. #include <cros_ec_message.h>
  14. #include <asm/gpio.h>
  15. /* Our configuration information */
  16. struct cros_ec_dev {
  17. struct udevice *dev; /* Transport device */
  18. struct gpio_desc ec_int; /* GPIO used as EC interrupt line */
  19. int protocol_version; /* Protocol version to use */
  20. int optimise_flash_write; /* Don't write erased flash blocks */
  21. /*
  22. * These two buffers will always be dword-aligned and include enough
  23. * space for up to 7 word-alignment bytes also, so we can ensure that
  24. * the body of the message is always dword-aligned (64-bit).
  25. *
  26. * We use this alignment to keep ARM and x86 happy. Probably word
  27. * alignment would be OK, there might be a small performance advantage
  28. * to using dword.
  29. */
  30. uint8_t din[ALIGN(MSG_BYTES + sizeof(int64_t), sizeof(int64_t))]
  31. __aligned(sizeof(int64_t));
  32. uint8_t dout[ALIGN(MSG_BYTES + sizeof(int64_t), sizeof(int64_t))]
  33. __aligned(sizeof(int64_t));
  34. };
  35. /*
  36. * Hard-code the number of columns we happen to know we have right now. It
  37. * would be more correct to call cros_ec_info() at startup and determine the
  38. * actual number of keyboard cols from there.
  39. */
  40. #define CROS_EC_KEYSCAN_COLS 13
  41. /* Information returned by a key scan */
  42. struct mbkp_keyscan {
  43. uint8_t data[CROS_EC_KEYSCAN_COLS];
  44. };
  45. /* Holds information about the Chrome EC */
  46. struct fdt_cros_ec {
  47. struct fmap_entry flash; /* Address and size of EC flash */
  48. /*
  49. * Byte value of erased flash, or -1 if not known. It is normally
  50. * 0xff but some flash devices use 0 (e.g. STM32Lxxx)
  51. */
  52. int flash_erase_value;
  53. struct fmap_entry region[EC_FLASH_REGION_COUNT];
  54. };
  55. /**
  56. * Read the ID of the CROS-EC device
  57. *
  58. * The ID is a string identifying the CROS-EC device.
  59. *
  60. * @param dev CROS-EC device
  61. * @param id Place to put the ID
  62. * @param maxlen Maximum length of the ID field
  63. * @return 0 if ok, -1 on error
  64. */
  65. int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen);
  66. /**
  67. * Read a keyboard scan from the CROS-EC device
  68. *
  69. * Send a message requesting a keyboard scan and return the result
  70. *
  71. * @param dev CROS-EC device
  72. * @param scan Place to put the scan results
  73. * @return 0 if ok, -1 on error
  74. */
  75. int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan);
  76. /**
  77. * Read which image is currently running on the CROS-EC device.
  78. *
  79. * @param dev CROS-EC device
  80. * @param image Destination for image identifier
  81. * @return 0 if ok, <0 on error
  82. */
  83. int cros_ec_read_current_image(struct cros_ec_dev *dev,
  84. enum ec_current_image *image);
  85. /**
  86. * Read the hash of the CROS-EC device firmware.
  87. *
  88. * @param dev CROS-EC device
  89. * @param hash Destination for hash information
  90. * @return 0 if ok, <0 on error
  91. */
  92. int cros_ec_read_hash(struct cros_ec_dev *dev,
  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 cros_ec_dev *dev, enum ec_reboot_cmd cmd,
  105. uint8_t flags);
  106. /**
  107. * Check if the CROS-EC device has an interrupt pending.
  108. *
  109. * Read the status of the external interrupt connected to the CROS-EC device.
  110. * If no external interrupt is configured, this always returns 1.
  111. *
  112. * @param dev CROS-EC device
  113. * @return 0 if no interrupt is pending
  114. */
  115. int cros_ec_interrupt_pending(struct udevice *dev);
  116. enum {
  117. CROS_EC_OK,
  118. CROS_EC_ERR = 1,
  119. CROS_EC_ERR_FDT_DECODE,
  120. CROS_EC_ERR_CHECK_VERSION,
  121. CROS_EC_ERR_READ_ID,
  122. CROS_EC_ERR_DEV_INIT,
  123. };
  124. /**
  125. * Initialise the Chromium OS EC driver
  126. *
  127. * @param blob Device tree blob containing setup information
  128. * @param cros_ecp Returns pointer to the cros_ec device, or NULL if none
  129. * @return 0 if we got an cros_ec device and all is well (or no cros_ec is
  130. * expected), -ve if we should have an cros_ec device but failed to find
  131. * one, or init failed (-CROS_EC_ERR_...).
  132. */
  133. int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp);
  134. /**
  135. * Read information about the keyboard matrix
  136. *
  137. * @param dev CROS-EC device
  138. * @param info Place to put the info structure
  139. */
  140. int cros_ec_info(struct cros_ec_dev *dev,
  141. struct ec_response_mkbp_info *info);
  142. /**
  143. * Read the host event flags
  144. *
  145. * @param dev CROS-EC device
  146. * @param events_ptr Destination for event flags. Not changed on error.
  147. * @return 0 if ok, <0 on error
  148. */
  149. int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr);
  150. /**
  151. * Clear the specified host event flags
  152. *
  153. * @param dev CROS-EC device
  154. * @param events Event flags to clear
  155. * @return 0 if ok, <0 on error
  156. */
  157. int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events);
  158. /**
  159. * Get/set flash protection
  160. *
  161. * @param dev CROS-EC device
  162. * @param set_mask Mask of flags to set; if 0, just retrieves existing
  163. * protection state without changing it.
  164. * @param set_flags New flag values; only bits in set_mask are applied;
  165. * ignored if set_mask=0.
  166. * @param prot Destination for updated protection state from EC.
  167. * @return 0 if ok, <0 on error
  168. */
  169. int cros_ec_flash_protect(struct cros_ec_dev *dev,
  170. uint32_t set_mask, uint32_t set_flags,
  171. struct ec_response_flash_protect *resp);
  172. /**
  173. * Run internal tests on the cros_ec interface.
  174. *
  175. * @param dev CROS-EC device
  176. * @return 0 if ok, <0 if the test failed
  177. */
  178. int cros_ec_test(struct cros_ec_dev *dev);
  179. /**
  180. * Update the EC RW copy.
  181. *
  182. * @param dev CROS-EC device
  183. * @param image the content to write
  184. * @param imafge_size content length
  185. * @return 0 if ok, <0 if the test failed
  186. */
  187. int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
  188. const uint8_t *image, int image_size);
  189. /**
  190. * Return a pointer to the board's CROS-EC device
  191. *
  192. * This should be implemented by board files.
  193. *
  194. * @return pointer to CROS-EC device, or NULL if none is available
  195. */
  196. struct cros_ec_dev *board_get_cros_ec_dev(void);
  197. struct dm_cros_ec_ops {
  198. int (*check_version)(struct udevice *dev);
  199. int (*command)(struct udevice *dev, uint8_t cmd, int cmd_version,
  200. const uint8_t *dout, int dout_len,
  201. uint8_t **dinp, int din_len);
  202. int (*packet)(struct udevice *dev, int out_bytes, int in_bytes);
  203. };
  204. #define dm_cros_ec_get_ops(dev) \
  205. ((struct dm_cros_ec_ops *)(dev)->driver->ops)
  206. int cros_ec_register(struct udevice *dev);
  207. /**
  208. * Dump a block of data for a command.
  209. *
  210. * @param name Name for data (e.g. 'in', 'out')
  211. * @param cmd Command number associated with data, or -1 for none
  212. * @param data Data block to dump
  213. * @param len Length of data block to dump
  214. */
  215. void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len);
  216. /**
  217. * Calculate a simple 8-bit checksum of a data block
  218. *
  219. * @param data Data block to checksum
  220. * @param size Size of data block in bytes
  221. * @return checksum value (0 to 255)
  222. */
  223. int cros_ec_calc_checksum(const uint8_t *data, int size);
  224. int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset,
  225. uint32_t size);
  226. /**
  227. * Read data from the flash
  228. *
  229. * Read an arbitrary amount of data from the EC flash, by repeatedly reading
  230. * small blocks.
  231. *
  232. * The offset starts at 0. You can obtain the region information from
  233. * cros_ec_flash_offset() to find out where to read for a particular region.
  234. *
  235. * @param dev CROS-EC device
  236. * @param data Pointer to data buffer to read into
  237. * @param offset Offset within flash to read from
  238. * @param size Number of bytes to read
  239. * @return 0 if ok, -1 on error
  240. */
  241. int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
  242. uint32_t size);
  243. /**
  244. * Read back flash parameters
  245. *
  246. * This function reads back parameters of the flash as reported by the EC
  247. *
  248. * @param dev Pointer to device
  249. * @param info Pointer to output flash info struct
  250. */
  251. int cros_ec_read_flashinfo(struct cros_ec_dev *dev,
  252. struct ec_response_flash_info *info);
  253. /**
  254. * Write data to the flash
  255. *
  256. * Write an arbitrary amount of data to the EC flash, by repeatedly writing
  257. * small blocks.
  258. *
  259. * The offset starts at 0. You can obtain the region information from
  260. * cros_ec_flash_offset() to find out where to write for a particular region.
  261. *
  262. * Attempting to write to the region where the EC is currently running from
  263. * will result in an error.
  264. *
  265. * @param dev CROS-EC device
  266. * @param data Pointer to data buffer to write
  267. * @param offset Offset within flash to write to.
  268. * @param size Number of bytes to write
  269. * @return 0 if ok, -1 on error
  270. */
  271. int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
  272. uint32_t offset, uint32_t size);
  273. /**
  274. * Obtain position and size of a flash region
  275. *
  276. * @param dev CROS-EC device
  277. * @param region Flash region to query
  278. * @param offset Returns offset of flash region in EC flash
  279. * @param size Returns size of flash region
  280. * @return 0 if ok, -1 on error
  281. */
  282. int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
  283. uint32_t *offset, uint32_t *size);
  284. /**
  285. * Read/write VbNvContext from/to a CROS-EC device.
  286. *
  287. * @param dev CROS-EC device
  288. * @param block Buffer of VbNvContext to be read/write
  289. * @return 0 if ok, -1 on error
  290. */
  291. int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block);
  292. int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block);
  293. /**
  294. * Read the version information for the EC images
  295. *
  296. * @param dev CROS-EC device
  297. * @param versionp This is set to point to the version information
  298. * @return 0 if ok, -1 on error
  299. */
  300. int cros_ec_read_version(struct cros_ec_dev *dev,
  301. struct ec_response_get_version **versionp);
  302. /**
  303. * Read the build information for the EC
  304. *
  305. * @param dev CROS-EC device
  306. * @param versionp This is set to point to the build string
  307. * @return 0 if ok, -1 on error
  308. */
  309. int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp);
  310. /**
  311. * Switch on/off a LDO / FET.
  312. *
  313. * @param dev CROS-EC device
  314. * @param index index of the LDO/FET to switch
  315. * @param state new state of the LDO/FET : EC_LDO_STATE_ON|OFF
  316. * @return 0 if ok, -1 on error
  317. */
  318. int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state);
  319. /**
  320. * Read back a LDO / FET current state.
  321. *
  322. * @param dev CROS-EC device
  323. * @param index index of the LDO/FET to switch
  324. * @param state current state of the LDO/FET : EC_LDO_STATE_ON|OFF
  325. * @return 0 if ok, -1 on error
  326. */
  327. int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state);
  328. /**
  329. * Get access to the error reported when cros_ec_board_init() was called
  330. *
  331. * This permits delayed reporting of the EC error if it failed during
  332. * early init.
  333. *
  334. * @return error (0 if there was no error, -ve if there was an error)
  335. */
  336. int cros_ec_get_error(void);
  337. /**
  338. * Returns information from the FDT about the Chrome EC flash
  339. *
  340. * @param blob FDT blob to use
  341. * @param node Node offset to read from
  342. * @param config Structure to use to return information
  343. */
  344. int cros_ec_decode_ec_flash(const void *blob, int node,
  345. struct fdt_cros_ec *config);
  346. /**
  347. * Check the current keyboard state, in case recovery mode is requested.
  348. * This function is for sandbox only.
  349. *
  350. * @param ec CROS-EC device
  351. */
  352. void cros_ec_check_keyboard(struct cros_ec_dev *dev);
  353. struct i2c_msg;
  354. /*
  355. * Tunnel an I2C transfer to the EC
  356. *
  357. * @param dev CROS-EC device
  358. * @param port The remote port on EC to use
  359. * @param msg List of messages to transfer
  360. * @param nmsgs Number of messages to transfer
  361. */
  362. int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *msg,
  363. int nmsgs);
  364. #endif