cros_ec.h 12 KB

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