xref: /aosp_15_r20/external/coreboot/src/ec/google/chromeec/ec.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 /*
4  * Mailbox EC communication interface for Google Chrome Embedded Controller.
5  */
6 
7 #ifndef _EC_GOOGLE_CHROMEEC_EC_H
8 #define _EC_GOOGLE_CHROMEEC_EC_H
9 #include <types.h>
10 #include <device/device.h>
11 #include "ec_commands.h"
12 #include <device/usbc_mux.h>
13 
14 /* Fill in base and size of the IO port resources used. */
15 void google_chromeec_ioport_range(uint16_t *base, size_t *size);
16 
17 int google_chromeec_i2c_xfer(uint8_t chip, uint8_t addr, int alen,
18 			     uint8_t *buffer, int len, int is_read);
19 uint64_t google_chromeec_get_wake_mask(void);
20 int google_chromeec_set_sci_mask(uint64_t mask);
21 int google_chromeec_set_smi_mask(uint64_t mask);
22 int google_chromeec_set_wake_mask(uint64_t mask);
23 enum host_event_code google_chromeec_get_event(void);
24 
25 /* Check if EC supports feature EC_FEATURE_UNIFIED_WAKE_MASKS */
26 bool google_chromeec_is_uhepi_supported(void);
27 int google_ec_running_ro(void);
28 enum ec_image google_chromeec_get_current_image(void);
29 void google_chromeec_init(void);
30 /* Check for the current mux state in EC
31  * in: int port physical port number of the type-c port
32  * out: uint8_t flags representing the status of the mux such as
33  *	usb capability, dp capability, cable type, etc
34  */
35 int google_chromeec_usb_get_pd_mux_info(int port, uint8_t *flags);
36 /* Poll (up to `timeout_ms` ms) for DisplayPort to be ready
37  * Return:	-1: Error. 0: Timeout.
38  *              >=1: Bitmask of the ports that DP device is connected
39  */
40 int google_chromeec_wait_for_displayport(long timeout_ms);
41 /* Poll (up to `timeout_ms` ms) for the DP mode entry
42  * event on the specified port.
43  * Return: 0 on DP mode entry success, -1 on timeout */
44 int google_chromeec_wait_for_dp_mode_entry(int port, long timeout_ms);
45 /* Poll (up to `timeout_ms` ms) for a Hot-Plug Detect (HPD)
46  * event on the specified port.
47  * Return: 0 on HPD ready, -1 on timeout */
48 int google_chromeec_wait_for_hpd(int port, long timeout_ms);
49 /* Send command to EC to request to enter DisplayPort ALT mode on the
50  * specified port.
51  * Return: 0 on success, -1 on error */
52 int google_chromeec_typec_control_enter_dp_mode(int port);
53 /*
54  * Obtain any USB-C mux data needed for the specified port
55  * in: int port physical port number of the type-c port
56  * out: struct usbc_mux_info mux_info stores USB-C mux data
57  * Return: 0 on success, -1 on error
58  */
59 int google_chromeec_get_usbc_mux_info(int port, struct usbc_mux_info *mux_info);
60 
61 /* Device events */
62 uint64_t google_chromeec_get_device_enabled_events(void);
63 int google_chromeec_set_device_enabled_events(uint64_t mask);
64 uint64_t google_chromeec_get_device_current_events(void);
65 
66 int google_chromeec_check_feature(int feature);
67 uint8_t google_chromeec_calc_checksum(const uint8_t *data, int size);
68 
69 /**
70  * google_chromeec_get_board_version() - Get the board version
71  * @version: Out parameter to retrieve the board Version
72  *
73  * Return: 0 on success or -1 on failure/error.
74  *
75  * This function is used to get the board version information from EC.
76  */
77 int google_chromeec_get_board_version(uint32_t *version);
78 uint32_t google_chromeec_get_sku_id(void);
79 int google_chromeec_set_sku_id(uint32_t skuid);
80 uint64_t  google_chromeec_get_events_b(void);
81 int google_chromeec_clear_events_b(uint64_t mask);
82 int google_chromeec_kbbacklight(int percent);
83 void google_chromeec_post(uint8_t postcode);
84 uint8_t google_chromeec_get_switches(void);
85 bool google_chromeec_get_ap_watchdog_flag(void);
86 
87 /* Temporary secure storage commands */
88 int google_chromeec_vstore_supported(void);
89 int google_chromeec_vstore_info(uint32_t *locked);
90 int google_chromeec_vstore_read(int slot, uint8_t *data);
91 int google_chromeec_vstore_write(int slot, uint8_t *data, size_t size);
92 
93 /* Issue reboot command to EC with specified type and flags. Returns 0 on
94    success, < 0 otherwise. */
95 int google_chromeec_reboot(enum ec_reboot_cmd type, uint8_t flags);
96 
97 /**
98  * Get data from Cros Board Info
99  *
100  * @param id/fw_config/buf [OUT] value from CBI.
101  * @return 0 on success or negative integer for errors.
102  */
103 int google_chromeec_cbi_get_oem_id(uint32_t *id);
104 int google_chromeec_cbi_get_sku_id(uint32_t *id);
105 int google_chromeec_cbi_get_fw_config(uint64_t *fw_config);
106 int google_chromeec_cbi_get_dram_part_num(char *buf, size_t bufsize);
107 int google_chromeec_cbi_get_oem_name(char *buf, size_t bufsize);
108 /* version may be stored in CBI as a smaller integer width, but the EC code
109    handles it correctly. */
110 int google_chromeec_cbi_get_board_version(uint32_t *version);
111 int google_chromeec_cbi_get_ssfc(uint32_t *ssfc);
112 
113 #define CROS_SKU_UNKNOWN	0xFFFFFFFF
114 #define CROS_SKU_UNPROVISIONED	0x7FFFFFFF
115 /* Returns CROS_SKU_UNKNOWN on failure. */
116 uint32_t google_chromeec_get_board_sku(void);
117 const char *google_chromeec_smbios_system_sku(void);
118 
119 /* MEC uses 0x800/0x804 as register/index pair, thus an 8-byte resource. */
120 #define MEC_EMI_BASE		0x800
121 #define MEC_EMI_SIZE		8
122 
123 /* For MEC, access ranges 0x800 thru 0x9ff using EMI interface instead of LPC */
124 #define MEC_EMI_RANGE_START EC_HOST_CMD_REGION0
125 #define MEC_EMI_RANGE_END   (EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE)
126 
127 int google_chromeec_set_usb_charge_mode(uint8_t port_id, enum usb_charge_mode mode);
128 int google_chromeec_set_usb_pd_role(uint8_t port, enum usb_pd_control_role role);
129 /*
130  * Retrieve the charger type and max wattage.
131  *
132  * @param type      charger type
133  * @param current_max charger max current
134  * @param voltage_max charger max voltage
135  * @return non-zero for error, otherwise 0.
136  */
137 int google_chromeec_get_usb_pd_power_info(enum usb_chg_type *type,
138 					  uint16_t *current_max, uint16_t *voltage_max);
139 
140 /*
141  * Set max current and voltage of a dedicated charger.
142  *
143  * @param current_lim Max current in mA
144  * @param voltage_lim Max voltage in mV
145  * @return non-zero for error, otherwise 0.
146  */
147 int google_chromeec_override_dedicated_charger_limit(uint16_t current_lim,
148 						     uint16_t voltage_lim);
149 
150 /* internal structure to send a command to the EC and wait for response. */
151 struct chromeec_command {
152 	uint16_t    cmd_code;	  /* command code in, status out */
153 	uint8_t     cmd_version;  /* command version */
154 	const void* cmd_data_in;  /* command data, if any */
155 	void*	    cmd_data_out; /* command response, if any */
156 	uint16_t    cmd_size_in;  /* size of command data */
157 	uint16_t    cmd_size_out; /* expected size of command response in,
158 				   * actual received size out */
159 	int         cmd_dev_index;/* device index for passthru */
160 };
161 
162 /*
163  * There are transport level constraints for sending protov3 packets. Because
164  * of this provide a way for the generic protocol layer to request buffers
165  * so that there is zero copying being done through the layers.
166  *
167  * Request the buffer provided the size. If 'req' is non-zero then the
168  * buffer requested is for EC requests. Otherwise it's for responses. Return
169  * non-NULL on success, NULL on error.
170  */
171 void *crosec_get_buffer(size_t size, int req);
172 
173 /*
174  * The lower level transport works on the buffers handed out to the
175  * upper level. Therefore, only the size of the request and response
176  * are required.
177  */
178 typedef int (*crosec_io_t)(size_t req_size, size_t resp_size, void *context);
179 int crosec_command_proto(struct chromeec_command *cec_command,
180 			 crosec_io_t crosec_io, void *context);
181 
182 /**
183  * Performs light verification of the EC<->AP communication channel.
184  *
185  * @return		0 on success, -1 on error
186  */
187 int google_chromeec_hello(void);
188 
189 /**
190  * Send a command to a CrOS EC
191  *
192  * @param cec_command: CrOS EC command to send
193  * @return 0 for success. Non-zero for error.
194  */
195 int google_chromeec_command(struct chromeec_command *cec_command);
196 
197 struct google_chromeec_event_info {
198 	uint64_t log_events;
199 	uint64_t sci_events;
200 	uint64_t smi_events;
201 	uint64_t s3_wake_events;
202 	uint64_t s3_device_events;
203 	uint64_t s5_wake_events;
204 	uint64_t s0ix_wake_events;
205 };
206 void google_chromeec_events_init(const struct google_chromeec_event_info *info,
207 					bool is_s3_wakeup);
208 
209 /*
210  * Get next available MKBP event in ec_response_get_next_event. Returns 0 on
211  * success, < 0 otherwise.
212  */
213 int google_chromeec_get_mkbp_event(struct ec_response_get_next_event *event);
214 
215 /* Log host events to eventlog based on the mask provided. */
216 void google_chromeec_log_events(uint64_t mask);
217 
218 /**
219  * Protect/un-protect EC flash regions.
220  *
221  * @param mask		Set/clear the requested bits in 'flags'
222  * @param flags		Flash protection flags
223  * @param resp		Pointer to response structure
224  * @return		0 on success, -1 on error
225  */
226 int google_chromeec_flash_protect(uint32_t mask, uint32_t flags,
227 				  struct ec_response_flash_protect *resp);
228 /**
229  * Calculate image hash for vboot.
230  *
231  * @param hash_type	The hash types supported by the EC for vboot
232  * @param offset	The offset to start hashing in flash
233  * @param resp		Pointer to response structure
234  * @return		0 on success, -1 on error
235  */
236 int google_chromeec_start_vboot_hash(enum ec_vboot_hash_type hash_type,
237 				     uint32_t offset,
238 				     struct ec_response_vboot_hash *resp);
239 /**
240  * Return the EC's vboot image hash.
241  *
242  * @param offset	Get hash for flash region beginning here
243  * @param resp		Pointer to response structure
244  * @return		0 on success, -1 on error
245  *
246  */
247 int google_chromeec_get_vboot_hash(uint32_t offset,
248 				   struct ec_response_vboot_hash *resp);
249 
250 /**
251  * Get offset and size of the specified EC flash region.
252  *
253  * @param region	Which region of EC flash
254  * @param offset	Gets filled with region's offset
255  * @param size		Gets filled with region's size
256  * @return		0 on success, -1 on error
257  */
258 int google_chromeec_flash_region_info(enum ec_flash_region region,
259 				      uint32_t *offset, uint32_t *size);
260 /**
261  * Erase a region of EC flash.
262  *
263  * @param offset	Where to begin erasing
264  * @param size		Size of area to erase
265  * @return		0 on success, -1 on error
266  */
267 int google_chromeec_flash_erase(uint32_t region_offset, uint32_t region_size);
268 
269 /**
270  * Return information about the entire flash.
271  *
272  * @param info		Pointer to response structure
273  * @return		0 on success, -1 on error
274  */
275 int google_chromeec_flash_info(struct ec_response_flash_info *info);
276 
277 /**
278  * Write a block into EC flash.
279  *
280  * @param data		Pointer to data to write to flash, prefixed by a
281  *			struct ec_params_flash_write
282  * @param offset        Offset to begin writing data
283  * @param size		Number of bytes to be written to flash from data
284  * @return		0 on success, -1 on error
285  */
286 int google_chromeec_flash_write_block(const uint8_t *data, uint32_t size);
287 
288 /**
289  * Verify flash using EFS if available.
290  *
291  * @param region	Which flash region to verify
292  * @return		0 on success, -1 on error
293  */
294 int google_chromeec_efs_verify(enum ec_flash_region region);
295 
296 /**
297  * Command EC to perform battery cutoff.
298  *
299  * @param flags		Flags to pass to the EC
300  * @return		0 on success, -1 on error
301  */
302 int google_chromeec_battery_cutoff(uint8_t flags);
303 
304 /**
305  * Check if the EC is requesting the system to limit input power.
306  *
307  * @param limit_power	If successful, limit_power is 1 if EC is requesting
308  *			input power limits, otherwise 0.
309  * @return		0 on success, -1 on error
310  */
311 int google_chromeec_read_limit_power_request(int *limit_power);
312 
313 /**
314  * Get information about the protocol that the EC speaks.
315  *
316  * @param resp		Filled with host command protocol information.
317  * @return		0 on success, -1 on error
318  */
319 int google_chromeec_get_protocol_info(
320 	struct ec_response_get_protocol_info *resp);
321 
322 /**
323  * Get available versions of the specified command.
324  *
325  * @param command	Command ID
326  * @param pmask		Pointer to version mask
327  * @return		0 on success, -1 on error
328  */
329 int google_chromeec_get_cmd_versions(int command, uint32_t *pmask);
330 
331 /**
332  * Get number of PD-capable USB ports from EC.
333  *
334  * @param *num_ports	If successful, num_ports is the number
335  *			of PD-capable USB ports according to the EC.
336  * @return		0 on success, -1 on error
337  */
338 int google_chromeec_get_num_pd_ports(unsigned int *num_ports);
339 
340 /* Structure representing the capabilities of a USB-PD port */
341 struct usb_pd_port_caps {
342 	enum ec_pd_power_role_caps power_role_cap;
343 	enum ec_pd_try_power_role_caps try_power_role_cap;
344 	enum ec_pd_data_role_caps data_role_cap;
345 	enum ec_pd_port_location port_location;
346 };
347 
348 /**
349  * Get role-based capabilities for a USB-PD port
350  *
351  * @param port			Which port to get information about
352  * @param *power_role_cap	The power-role capability of the port
353  * @param *try_power_role_cap	The Try-power-role capability of the port
354  * @param *data_role_cap	The data role capability of the port
355  * @param *port_location	Location of the port on the device
356  * @return			0 on success, -1 on error
357  */
358 int google_chromeec_get_pd_port_caps(int port,
359 				struct usb_pd_port_caps *port_caps);
360 
361 /**
362  * Get the keyboard configuration / layout information from EC
363  *
364  * @param *keybd	If successful, this is filled with EC filled parameters
365  * @return		0 on success, -1 on error
366  */
367 int google_chromeec_get_keybd_config(struct ec_response_keybd_config *keybd);
368 
369 /**
370  * Send EC command to perform AP reset
371  *
372  * @return	0 on success, -1 on error
373  */
374 int google_chromeec_ap_reset(void);
375 
376 /**
377  * Configure the regulator as enabled / disabled.
378  *
379  * @param index		Regulator ID
380  * @param enable	Set to enable / disable the regulator
381  * @return		0 on success, -1 on error
382  */
383 int google_chromeec_regulator_enable(uint32_t index, uint8_t enable);
384 
385 /**
386  * Query if the regulator is enabled.
387  *
388  * @param index		Regulator ID
389  * @param *enabled	If successful, enabled indicates enable/disable status.
390  * @return		0 on success, -1 on error
391  */
392 int google_chromeec_regulator_is_enabled(uint32_t index, uint8_t *enabled);
393 
394 /**
395  * Set voltage for the voltage regulator within the range specified.
396  *
397  * @param index		Regulator ID
398  * @param min_mv	Minimum voltage
399  * @param max_mv	Maximum voltage
400  * @return		0 on success, -1 on error
401  */
402 int google_chromeec_regulator_set_voltage(uint32_t index, uint32_t min_mv,
403 					  uint32_t max_mv);
404 
405 /**
406  * Get the currently configured voltage for the voltage regulator.
407  *
408  * @param index		Regulator ID
409  * @param *voltage_mv	If successful, voltage_mv is filled with current voltage
410  * @return		0 on success, -1 on error
411  */
412 int google_chromeec_regulator_get_voltage(uint32_t index, uint32_t *voltage_mv);
413 
414 /**
415  * Clear EC AP_IDLE flag
416  */
417 void google_chromeec_clear_ec_ap_idle(void);
418 
419 /**
420  * Check if battery is present and battery level is above critical threshold.
421  *
422  * @return		true: if the battery is present and battery level is above critical threshold
423  *			false: any of the above conditions is not true
424  */
425 bool google_chromeec_is_battery_present_and_above_critical_threshold(void);
426 
427 /**
428  * Determine if the UCSI stack is currently active.
429  *
430  * @return true if EC implements the UCSI stack
431  */
432 bool google_chromeec_get_ucsi_enabled(void);
433 
434 #if CONFIG(HAVE_ACPI_TABLES)
435 /**
436  * Writes USB Type-C PD related information to the SSDT
437  *
438  * @param dev			EC device
439  */
440 void google_chromeec_fill_ssdt_generator(const struct device *dev);
441 
442 /**
443  * Returns the ACPI name for the EC device.
444  *
445  * @param dev			EC device
446  */
447 const char *google_chromeec_acpi_name(const struct device *dev);
448 
449 #endif /* HAVE_ACPI_TABLES */
450 
451 #endif /* _EC_GOOGLE_CHROMEEC_EC_H */
452