cros_ec.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 Destination for hash information
  89. * @return 0 if ok, <0 on error
  90. */
  91. int cros_ec_read_hash(struct udevice *dev, struct ec_response_vboot_hash *hash);
  92. /**
  93. * Send a reboot command to the CROS-EC device.
  94. *
  95. * Note that some reboot commands (such as EC_REBOOT_COLD) also reboot the AP.
  96. *
  97. * @param dev CROS-EC device
  98. * @param cmd Reboot command
  99. * @param flags Flags for reboot command (EC_REBOOT_FLAG_*)
  100. * @return 0 if ok, <0 on error
  101. */
  102. int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags);
  103. /**
  104. * Check if the CROS-EC device has an interrupt pending.
  105. *
  106. * Read the status of the external interrupt connected to the CROS-EC device.
  107. * If no external interrupt is configured, this always returns 1.
  108. *
  109. * @param dev CROS-EC device
  110. * @return 0 if no interrupt is pending
  111. */
  112. int cros_ec_interrupt_pending(struct udevice *dev);
  113. enum {
  114. CROS_EC_OK,
  115. CROS_EC_ERR = 1,
  116. CROS_EC_ERR_FDT_DECODE,
  117. CROS_EC_ERR_CHECK_VERSION,
  118. CROS_EC_ERR_READ_ID,
  119. CROS_EC_ERR_DEV_INIT,
  120. };
  121. /**
  122. * Initialise the Chromium OS EC driver
  123. *
  124. * @param blob Device tree blob containing setup information
  125. * @param cros_ecp Returns pointer to the cros_ec device, or NULL if none
  126. * @return 0 if we got an cros_ec device and all is well (or no cros_ec is
  127. * expected), -ve if we should have an cros_ec device but failed to find
  128. * one, or init failed (-CROS_EC_ERR_...).
  129. */
  130. int cros_ec_init(const void *blob, struct udevice**cros_ecp);
  131. /**
  132. * Read information about the keyboard matrix
  133. *
  134. * @param dev CROS-EC device
  135. * @param info Place to put the info structure
  136. */
  137. int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info);
  138. /**
  139. * Read the host event flags
  140. *
  141. * @param dev CROS-EC device
  142. * @param events_ptr Destination for event flags. Not changed on error.
  143. * @return 0 if ok, <0 on error
  144. */
  145. int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr);
  146. /**
  147. * Clear the specified host event flags
  148. *
  149. * @param dev CROS-EC device
  150. * @param events Event flags to clear
  151. * @return 0 if ok, <0 on error
  152. */
  153. int cros_ec_clear_host_events(struct udevice *dev, uint32_t events);
  154. /**
  155. * Get/set flash protection
  156. *
  157. * @param dev CROS-EC device
  158. * @param set_mask Mask of flags to set; if 0, just retrieves existing
  159. * protection state without changing it.
  160. * @param set_flags New flag values; only bits in set_mask are applied;
  161. * ignored if set_mask=0.
  162. * @param prot Destination for updated protection state from EC.
  163. * @return 0 if ok, <0 on error
  164. */
  165. int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
  166. uint32_t set_flags,
  167. struct ec_response_flash_protect *resp);
  168. /**
  169. * Run internal tests on the cros_ec interface.
  170. *
  171. * @param dev CROS-EC device
  172. * @return 0 if ok, <0 if the test failed
  173. */
  174. int cros_ec_test(struct udevice *dev);
  175. /**
  176. * Update the EC RW copy.
  177. *
  178. * @param dev CROS-EC device
  179. * @param image the content to write
  180. * @param imafge_size content length
  181. * @return 0 if ok, <0 if the test failed
  182. */
  183. int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
  184. int image_size);
  185. /**
  186. * Return a pointer to the board's CROS-EC device
  187. *
  188. * This should be implemented by board files.
  189. *
  190. * @return pointer to CROS-EC device, or NULL if none is available
  191. */
  192. struct cros_ec_dev *board_get_cros_ec_dev(void);
  193. struct dm_cros_ec_ops {
  194. int (*check_version)(struct udevice *dev);
  195. int (*command)(struct udevice *dev, uint8_t cmd, int cmd_version,
  196. const uint8_t *dout, int dout_len,
  197. uint8_t **dinp, int din_len);
  198. int (*packet)(struct udevice *dev, int out_bytes, int in_bytes);
  199. };
  200. #define dm_cros_ec_get_ops(dev) \
  201. ((struct dm_cros_ec_ops *)(dev)->driver->ops)
  202. int cros_ec_register(struct udevice *dev);
  203. /**
  204. * Dump a block of data for a command.
  205. *
  206. * @param name Name for data (e.g. 'in', 'out')
  207. * @param cmd Command number associated with data, or -1 for none
  208. * @param data Data block to dump
  209. * @param len Length of data block to dump
  210. */
  211. void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len);
  212. /**
  213. * Calculate a simple 8-bit checksum of a data block
  214. *
  215. * @param data Data block to checksum
  216. * @param size Size of data block in bytes
  217. * @return checksum value (0 to 255)
  218. */
  219. int cros_ec_calc_checksum(const uint8_t *data, int size);
  220. int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size);
  221. /**
  222. * Read data from the flash
  223. *
  224. * Read an arbitrary amount of data from the EC flash, by repeatedly reading
  225. * small blocks.
  226. *
  227. * The offset starts at 0. You can obtain the region information from
  228. * cros_ec_flash_offset() to find out where to read for a particular region.
  229. *
  230. * @param dev CROS-EC device
  231. * @param data Pointer to data buffer to read into
  232. * @param offset Offset within flash to read from
  233. * @param size Number of bytes to read
  234. * @return 0 if ok, -1 on error
  235. */
  236. int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
  237. uint32_t size);
  238. /**
  239. * Read back flash parameters
  240. *
  241. * This function reads back parameters of the flash as reported by the EC
  242. *
  243. * @param dev Pointer to device
  244. * @param info Pointer to output flash info struct
  245. */
  246. int cros_ec_read_flashinfo(struct udevice *dev,
  247. struct ec_response_flash_info *info);
  248. /**
  249. * Write data to the flash
  250. *
  251. * Write an arbitrary amount of data to the EC flash, by repeatedly writing
  252. * small blocks.
  253. *
  254. * The offset starts at 0. You can obtain the region information from
  255. * cros_ec_flash_offset() to find out where to write for a particular region.
  256. *
  257. * Attempting to write to the region where the EC is currently running from
  258. * will result in an error.
  259. *
  260. * @param dev CROS-EC device
  261. * @param data Pointer to data buffer to write
  262. * @param offset Offset within flash to write to.
  263. * @param size Number of bytes to write
  264. * @return 0 if ok, -1 on error
  265. */
  266. int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
  267. uint32_t offset, uint32_t size);
  268. /**
  269. * Obtain position and size of a flash region
  270. *
  271. * @param dev CROS-EC device
  272. * @param region Flash region to query
  273. * @param offset Returns offset of flash region in EC flash
  274. * @param size Returns size of flash region
  275. * @return 0 if ok, -1 on error
  276. */
  277. int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
  278. uint32_t *offset, uint32_t *size);
  279. /**
  280. * Read/write non-volatile data from/to a CROS-EC device.
  281. *
  282. * @param dev CROS-EC device
  283. * @param block Buffer of VbNvContext to be read/write
  284. * @return 0 if ok, -1 on error
  285. */
  286. int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size);
  287. int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size);
  288. /**
  289. * Read the version information for the EC images
  290. *
  291. * @param dev CROS-EC device
  292. * @param versionp This is set to point to the version information
  293. * @return 0 if ok, -1 on error
  294. */
  295. int cros_ec_read_version(struct udevice *dev,
  296. struct ec_response_get_version **versionp);
  297. /**
  298. * Read the build information for the EC
  299. *
  300. * @param dev CROS-EC device
  301. * @param versionp This is set to point to the build string
  302. * @return 0 if ok, -1 on error
  303. */
  304. int cros_ec_read_build_info(struct udevice *dev, char **strp);
  305. /**
  306. * Switch on/off a LDO / FET.
  307. *
  308. * @param dev CROS-EC device
  309. * @param index index of the LDO/FET to switch
  310. * @param state new state of the LDO/FET : EC_LDO_STATE_ON|OFF
  311. * @return 0 if ok, -1 on error
  312. */
  313. int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state);
  314. /**
  315. * Read back a LDO / FET current state.
  316. *
  317. * @param dev CROS-EC device
  318. * @param index index of the LDO/FET to switch
  319. * @param state current state of the LDO/FET : EC_LDO_STATE_ON|OFF
  320. * @return 0 if ok, -1 on error
  321. */
  322. int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state);
  323. /**
  324. * Get access to the error reported when cros_ec_board_init() was called
  325. *
  326. * This permits delayed reporting of the EC error if it failed during
  327. * early init.
  328. *
  329. * @return error (0 if there was no error, -ve if there was an error)
  330. */
  331. int cros_ec_get_error(void);
  332. /**
  333. * Returns information from the FDT about the Chrome EC flash
  334. *
  335. * @param dev Device to read from
  336. * @param config Structure to use to return information
  337. */
  338. int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config);
  339. /**
  340. * Check the current keyboard state, in case recovery mode is requested.
  341. * This function is for sandbox only.
  342. *
  343. * @param ec CROS-EC device
  344. */
  345. void cros_ec_check_keyboard(struct udevice *dev);
  346. struct i2c_msg;
  347. /*
  348. * Tunnel an I2C transfer to the EC
  349. *
  350. * @param dev CROS-EC device
  351. * @param port The remote port on EC to use
  352. * @param msg List of messages to transfer
  353. * @param nmsgs Number of messages to transfer
  354. */
  355. int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *msg,
  356. int nmsgs);
  357. #endif