cros_ec.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. /* Which interface is the device on? */
  15. enum cros_ec_interface_t {
  16. CROS_EC_IF_NONE,
  17. CROS_EC_IF_SPI,
  18. CROS_EC_IF_I2C,
  19. CROS_EC_IF_LPC, /* Intel Low Pin Count interface */
  20. };
  21. /* Our configuration information */
  22. struct cros_ec_dev {
  23. enum cros_ec_interface_t interface;
  24. struct spi_slave *spi; /* Our SPI slave, if using SPI */
  25. int node; /* Our node */
  26. int parent_node; /* Our parent node (interface) */
  27. unsigned int cs; /* Our chip select */
  28. unsigned int addr; /* Device address (for I2C) */
  29. unsigned int bus_num; /* Bus number (for I2C) */
  30. unsigned int max_frequency; /* Maximum interface frequency */
  31. struct fdt_gpio_state ec_int; /* GPIO used as EC interrupt line */
  32. int protocol_version; /* Protocol version to use */
  33. int optimise_flash_write; /* Don't write erased flash blocks */
  34. /*
  35. * These two buffers will always be dword-aligned and include enough
  36. * space for up to 7 word-alignment bytes also, so we can ensure that
  37. * the body of the message is always dword-aligned (64-bit).
  38. *
  39. * We use this alignment to keep ARM and x86 happy. Probably word
  40. * alignment would be OK, there might be a small performance advantage
  41. * to using dword.
  42. */
  43. uint8_t din[ALIGN(MSG_BYTES + sizeof(int64_t), sizeof(int64_t))]
  44. __aligned(sizeof(int64_t));
  45. uint8_t dout[ALIGN(MSG_BYTES + sizeof(int64_t), sizeof(int64_t))]
  46. __aligned(sizeof(int64_t));
  47. };
  48. /*
  49. * Hard-code the number of columns we happen to know we have right now. It
  50. * would be more correct to call cros_ec_info() at startup and determine the
  51. * actual number of keyboard cols from there.
  52. */
  53. #define CROS_EC_KEYSCAN_COLS 13
  54. /* Information returned by a key scan */
  55. struct mbkp_keyscan {
  56. uint8_t data[CROS_EC_KEYSCAN_COLS];
  57. };
  58. /* Holds information about the Chrome EC */
  59. struct fdt_cros_ec {
  60. struct fmap_entry flash; /* Address and size of EC flash */
  61. /*
  62. * Byte value of erased flash, or -1 if not known. It is normally
  63. * 0xff but some flash devices use 0 (e.g. STM32Lxxx)
  64. */
  65. int flash_erase_value;
  66. struct fmap_entry region[EC_FLASH_REGION_COUNT];
  67. };
  68. /**
  69. * Read the ID of the CROS-EC device
  70. *
  71. * The ID is a string identifying the CROS-EC device.
  72. *
  73. * @param dev CROS-EC device
  74. * @param id Place to put the ID
  75. * @param maxlen Maximum length of the ID field
  76. * @return 0 if ok, -1 on error
  77. */
  78. int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen);
  79. /**
  80. * Read a keyboard scan from the CROS-EC device
  81. *
  82. * Send a message requesting a keyboard scan and return the result
  83. *
  84. * @param dev CROS-EC device
  85. * @param scan Place to put the scan results
  86. * @return 0 if ok, -1 on error
  87. */
  88. int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan);
  89. /**
  90. * Read which image is currently running on the CROS-EC device.
  91. *
  92. * @param dev CROS-EC device
  93. * @param image Destination for image identifier
  94. * @return 0 if ok, <0 on error
  95. */
  96. int cros_ec_read_current_image(struct cros_ec_dev *dev,
  97. enum ec_current_image *image);
  98. /**
  99. * Read the hash of the CROS-EC device firmware.
  100. *
  101. * @param dev CROS-EC device
  102. * @param hash Destination for hash information
  103. * @return 0 if ok, <0 on error
  104. */
  105. int cros_ec_read_hash(struct cros_ec_dev *dev,
  106. struct ec_response_vboot_hash *hash);
  107. /**
  108. * Send a reboot command to the CROS-EC device.
  109. *
  110. * Note that some reboot commands (such as EC_REBOOT_COLD) also reboot the AP.
  111. *
  112. * @param dev CROS-EC device
  113. * @param cmd Reboot command
  114. * @param flags Flags for reboot command (EC_REBOOT_FLAG_*)
  115. * @return 0 if ok, <0 on error
  116. */
  117. int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
  118. uint8_t flags);
  119. /**
  120. * Check if the CROS-EC device has an interrupt pending.
  121. *
  122. * Read the status of the external interrupt connected to the CROS-EC device.
  123. * If no external interrupt is configured, this always returns 1.
  124. *
  125. * @param dev CROS-EC device
  126. * @return 0 if no interrupt is pending
  127. */
  128. int cros_ec_interrupt_pending(struct cros_ec_dev *dev);
  129. enum {
  130. CROS_EC_OK,
  131. CROS_EC_ERR = 1,
  132. CROS_EC_ERR_FDT_DECODE,
  133. CROS_EC_ERR_CHECK_VERSION,
  134. CROS_EC_ERR_READ_ID,
  135. CROS_EC_ERR_DEV_INIT,
  136. };
  137. /**
  138. * Initialise the Chromium OS EC driver
  139. *
  140. * @param blob Device tree blob containing setup information
  141. * @param cros_ecp Returns pointer to the cros_ec device, or NULL if none
  142. * @return 0 if we got an cros_ec device and all is well (or no cros_ec is
  143. * expected), -ve if we should have an cros_ec device but failed to find
  144. * one, or init failed (-CROS_EC_ERR_...).
  145. */
  146. int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp);
  147. /**
  148. * Read information about the keyboard matrix
  149. *
  150. * @param dev CROS-EC device
  151. * @param info Place to put the info structure
  152. */
  153. int cros_ec_info(struct cros_ec_dev *dev,
  154. struct ec_response_mkbp_info *info);
  155. /**
  156. * Read the host event flags
  157. *
  158. * @param dev CROS-EC device
  159. * @param events_ptr Destination for event flags. Not changed on error.
  160. * @return 0 if ok, <0 on error
  161. */
  162. int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr);
  163. /**
  164. * Clear the specified host event flags
  165. *
  166. * @param dev CROS-EC device
  167. * @param events Event flags to clear
  168. * @return 0 if ok, <0 on error
  169. */
  170. int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events);
  171. /**
  172. * Get/set flash protection
  173. *
  174. * @param dev CROS-EC device
  175. * @param set_mask Mask of flags to set; if 0, just retrieves existing
  176. * protection state without changing it.
  177. * @param set_flags New flag values; only bits in set_mask are applied;
  178. * ignored if set_mask=0.
  179. * @param prot Destination for updated protection state from EC.
  180. * @return 0 if ok, <0 on error
  181. */
  182. int cros_ec_flash_protect(struct cros_ec_dev *dev,
  183. uint32_t set_mask, uint32_t set_flags,
  184. struct ec_response_flash_protect *resp);
  185. /**
  186. * Run internal tests on the cros_ec interface.
  187. *
  188. * @param dev CROS-EC device
  189. * @return 0 if ok, <0 if the test failed
  190. */
  191. int cros_ec_test(struct cros_ec_dev *dev);
  192. /**
  193. * Update the EC RW copy.
  194. *
  195. * @param dev CROS-EC device
  196. * @param image the content to write
  197. * @param imafge_size content length
  198. * @return 0 if ok, <0 if the test failed
  199. */
  200. int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
  201. const uint8_t *image, int image_size);
  202. /**
  203. * Return a pointer to the board's CROS-EC device
  204. *
  205. * This should be implemented by board files.
  206. *
  207. * @return pointer to CROS-EC device, or NULL if none is available
  208. */
  209. struct cros_ec_dev *board_get_cros_ec_dev(void);
  210. /* Internal interfaces */
  211. int cros_ec_i2c_init(struct cros_ec_dev *dev, const void *blob);
  212. int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob);
  213. int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob);
  214. /**
  215. * Read information from the fdt for the i2c cros_ec interface
  216. *
  217. * @param dev CROS-EC device
  218. * @param blob Device tree blob
  219. * @return 0 if ok, -1 if we failed to read all required information
  220. */
  221. int cros_ec_i2c_decode_fdt(struct cros_ec_dev *dev, const void *blob);
  222. /**
  223. * Read information from the fdt for the spi cros_ec interface
  224. *
  225. * @param dev CROS-EC device
  226. * @param blob Device tree blob
  227. * @return 0 if ok, -1 if we failed to read all required information
  228. */
  229. int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob);
  230. /**
  231. * Check whether the LPC interface supports new-style commands.
  232. *
  233. * LPC has its own way of doing this, which involves checking LPC values
  234. * visible to the host. Do this, and update dev->protocol_version accordingly.
  235. *
  236. * @param dev CROS-EC device to check
  237. */
  238. int cros_ec_lpc_check_version(struct cros_ec_dev *dev);
  239. /**
  240. * Send a command to an I2C CROS-EC device and return the reply.
  241. *
  242. * This rather complicated function deals with sending both old-style and
  243. * new-style commands. The old ones have just a command byte and arguments.
  244. * The new ones have version, command, arg-len, [args], chksum so are 3 bytes
  245. * longer.
  246. *
  247. * The device's internal input/output buffers are used.
  248. *
  249. * @param dev CROS-EC device
  250. * @param cmd Command to send (EC_CMD_...)
  251. * @param cmd_version Version of command to send (EC_VER_...)
  252. * @param dout Output data (may be NULL If dout_len=0)
  253. * @param dout_len Size of output data in bytes
  254. * @param dinp Returns pointer to response data
  255. * @param din_len Maximum size of response in bytes
  256. * @return number of bytes in response, or -1 on error
  257. */
  258. int cros_ec_i2c_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  259. const uint8_t *dout, int dout_len,
  260. uint8_t **dinp, int din_len);
  261. /**
  262. * Send a command to a LPC CROS-EC device and return the reply.
  263. *
  264. * The device's internal input/output buffers are used.
  265. *
  266. * @param dev CROS-EC device
  267. * @param cmd Command to send (EC_CMD_...)
  268. * @param cmd_version Version of command to send (EC_VER_...)
  269. * @param dout Output data (may be NULL If dout_len=0)
  270. * @param dout_len Size of output data in bytes
  271. * @param dinp Returns pointer to response data
  272. * @param din_len Maximum size of response in bytes
  273. * @return number of bytes in response, or -1 on error
  274. */
  275. int cros_ec_lpc_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  276. const uint8_t *dout, int dout_len,
  277. uint8_t **dinp, int din_len);
  278. int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  279. const uint8_t *dout, int dout_len,
  280. uint8_t **dinp, int din_len);
  281. /**
  282. * Send a packet to a CROS-EC device and return the response packet.
  283. *
  284. * Expects the request packet to be stored in dev->dout. Stores the response
  285. * packet in dev->din.
  286. *
  287. * @param dev CROS-EC device
  288. * @param out_bytes Size of request packet to output
  289. * @param in_bytes Maximum size of response packet to receive
  290. * @return number of bytes in response packet, or <0 on error
  291. */
  292. int cros_ec_spi_packet(struct cros_ec_dev *dev, int out_bytes, int in_bytes);
  293. /**
  294. * Dump a block of data for a command.
  295. *
  296. * @param name Name for data (e.g. 'in', 'out')
  297. * @param cmd Command number associated with data, or -1 for none
  298. * @param data Data block to dump
  299. * @param len Length of data block to dump
  300. */
  301. void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len);
  302. /**
  303. * Calculate a simple 8-bit checksum of a data block
  304. *
  305. * @param data Data block to checksum
  306. * @param size Size of data block in bytes
  307. * @return checksum value (0 to 255)
  308. */
  309. int cros_ec_calc_checksum(const uint8_t *data, int size);
  310. /**
  311. * Decode a flash region parameter
  312. *
  313. * @param argc Number of params remaining
  314. * @param argv List of remaining parameters
  315. * @return flash region (EC_FLASH_REGION_...) or -1 on error
  316. */
  317. int cros_ec_decode_region(int argc, char * const argv[]);
  318. int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset,
  319. uint32_t size);
  320. /**
  321. * Read data from the flash
  322. *
  323. * Read an arbitrary amount of data from the EC flash, by repeatedly reading
  324. * small blocks.
  325. *
  326. * The offset starts at 0. You can obtain the region information from
  327. * cros_ec_flash_offset() to find out where to read for a particular region.
  328. *
  329. * @param dev CROS-EC device
  330. * @param data Pointer to data buffer to read into
  331. * @param offset Offset within flash to read from
  332. * @param size Number of bytes to read
  333. * @return 0 if ok, -1 on error
  334. */
  335. int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
  336. uint32_t size);
  337. /**
  338. * Write data to the flash
  339. *
  340. * Write an arbitrary amount of data to the EC flash, by repeatedly writing
  341. * small blocks.
  342. *
  343. * The offset starts at 0. You can obtain the region information from
  344. * cros_ec_flash_offset() to find out where to write for a particular region.
  345. *
  346. * Attempting to write to the region where the EC is currently running from
  347. * will result in an error.
  348. *
  349. * @param dev CROS-EC device
  350. * @param data Pointer to data buffer to write
  351. * @param offset Offset within flash to write to.
  352. * @param size Number of bytes to write
  353. * @return 0 if ok, -1 on error
  354. */
  355. int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
  356. uint32_t offset, uint32_t size);
  357. /**
  358. * Obtain position and size of a flash region
  359. *
  360. * @param dev CROS-EC device
  361. * @param region Flash region to query
  362. * @param offset Returns offset of flash region in EC flash
  363. * @param size Returns size of flash region
  364. * @return 0 if ok, -1 on error
  365. */
  366. int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
  367. uint32_t *offset, uint32_t *size);
  368. /**
  369. * Read/write VbNvContext from/to a CROS-EC device.
  370. *
  371. * @param dev CROS-EC device
  372. * @param block Buffer of VbNvContext to be read/write
  373. * @return 0 if ok, -1 on error
  374. */
  375. int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block);
  376. int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block);
  377. /**
  378. * Read the version information for the EC images
  379. *
  380. * @param dev CROS-EC device
  381. * @param versionp This is set to point to the version information
  382. * @return 0 if ok, -1 on error
  383. */
  384. int cros_ec_read_version(struct cros_ec_dev *dev,
  385. struct ec_response_get_version **versionp);
  386. /**
  387. * Read the build information for the EC
  388. *
  389. * @param dev CROS-EC device
  390. * @param versionp This is set to point to the build string
  391. * @return 0 if ok, -1 on error
  392. */
  393. int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp);
  394. /**
  395. * Switch on/off a LDO / FET.
  396. *
  397. * @param dev CROS-EC device
  398. * @param index index of the LDO/FET to switch
  399. * @param state new state of the LDO/FET : EC_LDO_STATE_ON|OFF
  400. * @return 0 if ok, -1 on error
  401. */
  402. int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state);
  403. /**
  404. * Read back a LDO / FET current state.
  405. *
  406. * @param dev CROS-EC device
  407. * @param index index of the LDO/FET to switch
  408. * @param state current state of the LDO/FET : EC_LDO_STATE_ON|OFF
  409. * @return 0 if ok, -1 on error
  410. */
  411. int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state);
  412. /**
  413. * Initialize the Chrome OS EC at board initialization time.
  414. *
  415. * @return 0 if ok, -ve on error
  416. */
  417. int cros_ec_board_init(void);
  418. /**
  419. * Get access to the error reported when cros_ec_board_init() was called
  420. *
  421. * This permits delayed reporting of the EC error if it failed during
  422. * early init.
  423. *
  424. * @return error (0 if there was no error, -ve if there was an error)
  425. */
  426. int cros_ec_get_error(void);
  427. /**
  428. * Returns information from the FDT about the Chrome EC flash
  429. *
  430. * @param blob FDT blob to use
  431. * @param config Structure to use to return information
  432. */
  433. int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config);
  434. #endif