cros_ec.h 15 KB

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