| 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 | void google_chromeec_ap_poweroff(void); |
| 98 | |
| 99 | /** |
| 100 | * Get data from Cros Board Info |
| 101 | * |
| 102 | * @param id/fw_config/buf [OUT] value from CBI. |
| 103 | * @return 0 on success or negative integer for errors. |
| 104 | */ |
| 105 | int google_chromeec_cbi_get_oem_id(uint32_t *id); |
| 106 | int google_chromeec_cbi_get_sku_id(uint32_t *id); |
| 107 | int google_chromeec_cbi_get_fw_config(uint64_t *fw_config); |
| 108 | int google_chromeec_cbi_get_dram_part_num(char *buf, size_t bufsize); |
| 109 | int google_chromeec_cbi_get_oem_name(char *buf, size_t bufsize); |
| 110 | /* version may be stored in CBI as a smaller integer width, but the EC code |
| 111 | handles it correctly. */ |
| 112 | int google_chromeec_cbi_get_board_version(uint32_t *version); |
| 113 | int google_chromeec_cbi_get_ssfc(uint32_t *ssfc); |
| 114 | |
| 115 | #define CROS_SKU_UNKNOWN 0xFFFFFFFF |
| 116 | #define CROS_SKU_UNPROVISIONED 0x7FFFFFFF |
| 117 | /* Returns CROS_SKU_UNKNOWN on failure. */ |
| 118 | uint32_t google_chromeec_get_board_sku(void); |
| 119 | const char *google_chromeec_smbios_system_sku(void); |
| 120 | |
| 121 | int google_chromeec_set_usb_charge_mode(uint8_t port_id, enum usb_charge_mode mode); |
| 122 | int google_chromeec_set_usb_pd_role(uint8_t port, enum usb_pd_control_role role); |
| 123 | /* |
| 124 | * Retrieve the charger type and max wattage. |
| 125 | * |
| 126 | * @param type charger type |
| 127 | * @param current_max charger max current |
| 128 | * @param voltage_max charger max voltage |
| 129 | * @return non-zero for error, otherwise 0. |
| 130 | */ |
| 131 | int google_chromeec_get_usb_pd_power_info(enum usb_chg_type *type, |
| 132 | uint16_t *current_max, uint16_t *voltage_max); |
| 133 | |
| 134 | /* Check if a USB Power Delivery (PD) charger is attached */ |
| 135 | bool google_chromeec_is_usb_pd_attached(void); |
| 136 | |
| 137 | /** |
| 138 | * Check if charger is present. |
| 139 | * |
| 140 | * @return true: if the charger is present |
| 141 | * false: if the charger is not present |
| 142 | */ |
| 143 | bool google_chromeec_is_charger_present(void); |
| 144 | |
| 145 | /** |
| 146 | * Check if barrel charger is present. |
| 147 | * |
| 148 | * @return true: if the barrel charger is present |
| 149 | * false: if the barrel charger is not present |
| 150 | */ |
| 151 | bool google_chromeec_is_barrel_charger_present(void); |
| 152 | |
| 153 | /* |
| 154 | * Set max current and voltage of a dedicated charger. |
| 155 | * |
| 156 | * @param current_lim Max current in mA |
| 157 | * @param voltage_lim Max voltage in mV |
| 158 | * @return non-zero for error, otherwise 0. |
| 159 | */ |
| 160 | int google_chromeec_override_dedicated_charger_limit(uint16_t current_lim, |
| 161 | uint16_t voltage_lim); |
| 162 | |
| 163 | /* internal structure to send a command to the EC and wait for response. */ |
| 164 | struct chromeec_command { |
| 165 | uint16_t cmd_code; /* command code in, status out */ |
| 166 | uint8_t cmd_version; /* command version */ |
| 167 | const void* cmd_data_in; /* command data, if any */ |
| 168 | void* cmd_data_out; /* command response, if any */ |
| 169 | uint16_t cmd_size_in; /* size of command data */ |
| 170 | uint16_t cmd_size_out; /* expected size of command response in, |
| 171 | * actual received size out */ |
| 172 | int cmd_dev_index;/* device index for passthru */ |
| 173 | }; |
| 174 | |
| 175 | /* |
| 176 | * There are transport level constraints for sending protov3 packets. Because |
| 177 | * of this provide a way for the generic protocol layer to request buffers |
| 178 | * so that there is zero copying being done through the layers. |
| 179 | * |
| 180 | * Request the buffer provided the size. If 'req' is non-zero then the |
| 181 | * buffer requested is for EC requests. Otherwise it's for responses. Return |
| 182 | * non-NULL on success, NULL on error. |
| 183 | */ |
| 184 | void *crosec_get_buffer(size_t size, int req); |
| 185 | |
| 186 | /* |
| 187 | * The lower level transport works on the buffers handed out to the |
| 188 | * upper level. Therefore, only the size of the request and response |
| 189 | * are required. |
| 190 | */ |
| 191 | typedef int (*crosec_io_t)(size_t req_size, size_t resp_size, void *context); |
| 192 | int crosec_command_proto(struct chromeec_command *cec_command, |
| 193 | crosec_io_t crosec_io, void *context); |
| 194 | |
| 195 | /** |
| 196 | * Performs light verification of the EC<->AP communication channel. |
| 197 | * |
| 198 | * @return 0 on success, -1 on error |
| 199 | */ |
| 200 | int google_chromeec_hello(void); |
| 201 | |
| 202 | /** |
| 203 | * Send a command to a CrOS EC |
| 204 | * |
| 205 | * @param cec_command: CrOS EC command to send |
| 206 | * @return 0 for success. Non-zero for error. |
| 207 | */ |
| 208 | int google_chromeec_command(struct chromeec_command *cec_command); |
| 209 | |
| 210 | struct google_chromeec_event_info { |
| 211 | uint64_t log_events; |
| 212 | uint64_t sci_events; |
| 213 | uint64_t smi_events; |
| 214 | uint64_t s3_wake_events; |
| 215 | uint64_t s3_device_events; |
| 216 | uint64_t s5_wake_events; |
| 217 | uint64_t s0ix_wake_events; |
| 218 | }; |
| 219 | void google_chromeec_events_init(const struct google_chromeec_event_info *info, |
| 220 | bool is_s3_wakeup); |
| 221 | |
| 222 | /* |
| 223 | * Get next available MKBP event in ec_response_get_next_event. Returns 0 on |
| 224 | * success, < 0 otherwise. |
| 225 | */ |
| 226 | int google_chromeec_get_mkbp_event(struct ec_response_get_next_event *event); |
| 227 | |
| 228 | /* Log host events to eventlog based on the mask provided. */ |
| 229 | void google_chromeec_log_events(uint64_t mask); |
| 230 | |
| 231 | /** |
| 232 | * Protect/un-protect EC flash regions. |
| 233 | * |
| 234 | * @param mask Set/clear the requested bits in 'flags' |
| 235 | * @param flags Flash protection flags |
| 236 | * @param resp Pointer to response structure |
| 237 | * @return 0 on success, -1 on error |
| 238 | */ |
| 239 | int google_chromeec_flash_protect(uint32_t mask, uint32_t flags, |
| 240 | struct ec_response_flash_protect *resp); |
| 241 | /** |
| 242 | * Calculate image hash for vboot. |
| 243 | * |
| 244 | * @param hash_type The hash types supported by the EC for vboot |
| 245 | * @param offset The offset to start hashing in flash |
| 246 | * @param resp Pointer to response structure |
| 247 | * @return 0 on success, -1 on error |
| 248 | */ |
| 249 | int google_chromeec_start_vboot_hash(enum ec_vboot_hash_type hash_type, |
| 250 | uint32_t offset, |
| 251 | struct ec_response_vboot_hash *resp); |
| 252 | /** |
| 253 | * Return the EC's vboot image hash. |
| 254 | * |
| 255 | * @param offset Get hash for flash region beginning here |
| 256 | * @param resp Pointer to response structure |
| 257 | * @return 0 on success, -1 on error |
| 258 | * |
| 259 | */ |
| 260 | int google_chromeec_get_vboot_hash(uint32_t offset, struct ec_response_vboot_hash *resp); |
| 261 | |
| 262 | /** |
| 263 | * Get offset and size of the specified EC flash region. |
| 264 | * |
| 265 | * @param region Which region of EC flash |
| 266 | * @param offset Gets filled with region's offset |
| 267 | * @param size Gets filled with region's size |
| 268 | * @return 0 on success, -1 on error |
| 269 | */ |
| 270 | int google_chromeec_flash_region_info(enum ec_flash_region region, |
| 271 | uint32_t *offset, uint32_t *size); |
| 272 | /** |
| 273 | * Erase a region of EC flash. |
| 274 | * |
| 275 | * @param offset Where to begin erasing |
| 276 | * @param size Size of area to erase |
| 277 | * @return 0 on success, -1 on error |
| 278 | */ |
| 279 | int google_chromeec_flash_erase(uint32_t region_offset, uint32_t region_size); |
| 280 | |
| 281 | /** |
| 282 | * Return information about the entire flash. |
| 283 | * |
| 284 | * @param info Pointer to response structure |
| 285 | * @return 0 on success, -1 on error |
| 286 | */ |
| 287 | int google_chromeec_flash_info(struct ec_response_flash_info *info); |
| 288 | |
| 289 | /** |
| 290 | * Write a block into EC flash. |
| 291 | * |
| 292 | * @param data Pointer to data to write to flash, prefixed by a |
| 293 | * struct ec_params_flash_write |
| 294 | * @param offset Offset to begin writing data |
| 295 | * @param size Number of bytes to be written to flash from data |
| 296 | * @return 0 on success, -1 on error |
| 297 | */ |
| 298 | int google_chromeec_flash_write_block(const uint8_t *data, uint32_t size); |
| 299 | |
| 300 | /** |
| 301 | * Verify flash using EFS if available. |
| 302 | * |
| 303 | * @param region Which flash region to verify |
| 304 | * @return 0 on success, -1 on error |
| 305 | */ |
| 306 | int google_chromeec_efs_verify(enum ec_flash_region region); |
| 307 | |
| 308 | /** |
| 309 | * Command EC to perform battery cutoff. |
| 310 | * |
| 311 | * @param flags Flags to pass to the EC |
| 312 | * @return 0 on success, -1 on error |
| 313 | */ |
| 314 | int google_chromeec_battery_cutoff(uint8_t flags); |
| 315 | |
| 316 | /** |
| 317 | * Check if the EC is requesting the system to limit input power. |
| 318 | * |
| 319 | * @param limit_power If successful, limit_power is 1 if EC is requesting |
| 320 | * input power limits, otherwise 0. |
| 321 | * @return 0 on success, -1 on error |
| 322 | */ |
| 323 | int google_chromeec_read_limit_power_request(int *limit_power); |
| 324 | |
| 325 | /** |
| 326 | * Get information about the protocol that the EC speaks. |
| 327 | * |
| 328 | * @param resp Filled with host command protocol information. |
| 329 | * @return 0 on success, -1 on error |
| 330 | */ |
| 331 | int google_chromeec_get_protocol_info( |
| 332 | struct ec_response_get_protocol_info *resp); |
| 333 | |
| 334 | /** |
| 335 | * Get available versions of the specified command. |
| 336 | * |
| 337 | * @param command Command ID |
| 338 | * @param pmask Pointer to version mask |
| 339 | * @return 0 on success, -1 on error |
| 340 | */ |
| 341 | int google_chromeec_get_cmd_versions(int command, uint32_t *pmask); |
| 342 | |
| 343 | /** |
| 344 | * Get number of PD-capable USB ports from EC. |
| 345 | * |
| 346 | * @param *num_ports If successful, num_ports is the number |
| 347 | * of PD-capable USB ports according to the EC. |
| 348 | * @return 0 on success, -1 on error |
| 349 | */ |
| 350 | int google_chromeec_get_num_pd_ports(unsigned int *num_ports); |
| 351 | |
| 352 | /** |
| 353 | * Return a port's PD chip information. |
| 354 | * |
| 355 | * @param port The desired port number |
| 356 | * @param renew Refresh cached value |
| 357 | * @param r Result buffer for chip info |
| 358 | * |
| 359 | * @return 0 if ok, -1 on error |
| 360 | */ |
| 361 | int google_chromeec_get_pd_chip_info(int port, int renew, |
| 362 | struct ec_response_pd_chip_info *r); |
| 363 | |
| 364 | /* Structure representing the capabilities of a USB-PD port */ |
| 365 | struct usb_pd_port_caps { |
| 366 | enum ec_pd_power_role_caps power_role_cap; |
| 367 | enum ec_pd_try_power_role_caps try_power_role_cap; |
| 368 | enum ec_pd_data_role_caps data_role_cap; |
| 369 | enum ec_pd_port_location port_location; |
| 370 | }; |
| 371 | |
| 372 | /** |
| 373 | * Get role-based capabilities for a USB-PD port |
| 374 | * |
| 375 | * @param port Which port to get information about |
| 376 | * @param *power_role_cap The power-role capability of the port |
| 377 | * @param *try_power_role_cap The Try-power-role capability of the port |
| 378 | * @param *data_role_cap The data role capability of the port |
| 379 | * @param *port_location Location of the port on the device |
| 380 | * @return 0 on success, -1 on error |
| 381 | */ |
| 382 | int google_chromeec_get_pd_port_caps(int port, |
| 383 | struct usb_pd_port_caps *port_caps); |
| 384 | |
| 385 | /** |
| 386 | * Get the keyboard configuration / layout information from EC |
| 387 | * |
| 388 | * @param *keybd If successful, this is filled with EC filled parameters |
| 389 | * @return 0 on success, -1 on error |
| 390 | */ |
| 391 | int google_chromeec_get_keybd_config(struct ec_response_keybd_config *keybd); |
| 392 | |
| 393 | /** |
| 394 | * Send EC command to perform AP reset |
| 395 | * |
| 396 | * @return 0 on success, -1 on error |
| 397 | */ |
| 398 | int google_chromeec_ap_reset(void); |
| 399 | |
| 400 | /** |
| 401 | * Configure the regulator as enabled / disabled. |
| 402 | * |
| 403 | * @param index Regulator ID |
| 404 | * @param enable Set to enable / disable the regulator |
| 405 | * @return 0 on success, -1 on error |
| 406 | */ |
| 407 | int google_chromeec_regulator_enable(uint32_t index, uint8_t enable); |
| 408 | |
| 409 | /** |
| 410 | * Query if the regulator is enabled. |
| 411 | * |
| 412 | * @param index Regulator ID |
| 413 | * @param *enabled If successful, enabled indicates enable/disable status. |
| 414 | * @return 0 on success, -1 on error |
| 415 | */ |
| 416 | int google_chromeec_regulator_is_enabled(uint32_t index, uint8_t *enabled); |
| 417 | |
| 418 | /** |
| 419 | * Set voltage for the voltage regulator within the range specified. |
| 420 | * |
| 421 | * @param index Regulator ID |
| 422 | * @param min_mv Minimum voltage |
| 423 | * @param max_mv Maximum voltage |
| 424 | * @return 0 on success, -1 on error |
| 425 | */ |
| 426 | int google_chromeec_regulator_set_voltage(uint32_t index, uint32_t min_mv, |
| 427 | uint32_t max_mv); |
| 428 | |
| 429 | /** |
| 430 | * Get the currently configured voltage for the voltage regulator. |
| 431 | * |
| 432 | * @param index Regulator ID |
| 433 | * @param *voltage_mv If successful, voltage_mv is filled with current voltage |
| 434 | * @return 0 on success, -1 on error |
| 435 | */ |
| 436 | int google_chromeec_regulator_get_voltage(uint32_t index, uint32_t *voltage_mv); |
| 437 | |
| 438 | /** |
| 439 | * Clear EC AP_IDLE flag |
| 440 | */ |
| 441 | void google_chromeec_clear_ec_ap_idle(void); |
| 442 | |
| 443 | /** |
| 444 | * Check if battery is present and battery level is above critical threshold. |
| 445 | * |
| 446 | * @return true: if the battery is present and battery level is above critical threshold |
| 447 | * false: any of the above conditions is not true |
| 448 | */ |
| 449 | bool google_chromeec_is_battery_present_and_above_critical_threshold(void); |
| 450 | |
| 451 | /** |
| 452 | * Check if battery level is below critical threshold. |
| 453 | * |
| 454 | * @return true: if the battery level is below critical threshold |
| 455 | * false: any the above conditions is not true |
| 456 | */ |
| 457 | bool google_chromeec_is_below_critical_threshold(void); |
| 458 | |
| 459 | /** |
| 460 | * Check if battery is present. |
| 461 | * |
| 462 | * @return true: if the battery is present |
| 463 | * false: if the battery is not present |
| 464 | */ |
| 465 | bool google_chromeec_is_battery_present(void); |
| 466 | |
| 467 | /** |
| 468 | * Determine if the UCSI stack is currently active. |
| 469 | * |
| 470 | * @return true if EC implements the UCSI stack |
| 471 | */ |
| 472 | bool google_chromeec_get_ucsi_enabled(void); |
| 473 | |
| 474 | #if CONFIG(HAVE_ACPI_TABLES) |
| 475 | /** |
| 476 | * Writes USB Type-C PD related information to the SSDT |
| 477 | * |
| 478 | * @param dev EC device |
| 479 | */ |
| 480 | void google_chromeec_fill_ssdt_generator(const struct device *dev); |
| 481 | |
| 482 | /** |
| 483 | * Returns the ACPI name for the EC device. |
| 484 | * |
| 485 | * @param dev EC device |
| 486 | */ |
| 487 | const char *google_chromeec_acpi_name(const struct device *dev); |
| 488 | |
| 489 | #endif /* HAVE_ACPI_TABLES */ |
| 490 | |
| 491 | /** |
| 492 | * Read bytes from the EMI. |
| 493 | * |
| 494 | * @param port IO port number |
| 495 | * @param length Length of the data to read |
| 496 | * @param dest Pointer to the destination buffer |
| 497 | * @param csum Pointer to the checksum buffer |
| 498 | * |
| 499 | * @return true indicates that the EC processes the read through the EMI interface |
| 500 | * and does not need to handle it through the IO port; otherwise, an IO read operation |
| 501 | * should be issued. |
| 502 | */ |
| 503 | bool chipset_emi_read_bytes(u16 port, size_t length, u8 *dest, u8 *csum); |
| 504 | |
| 505 | /** |
| 506 | * Write bytes to the EMI. |
| 507 | * |
| 508 | * @param port IO port number |
| 509 | * @param length Length of the data to write |
| 510 | * @param msg Pointer to the message buffer |
| 511 | * @param csum Pointer to the checksum buffer |
| 512 | * |
| 513 | * @return true indicates that the EC processes the write through the EMI interface |
| 514 | * and does not need to handle it through the IO port; otherwise, an IO write operation |
| 515 | * should be issued. |
| 516 | */ |
| 517 | bool chipset_emi_write_bytes(u16 port, size_t length, u8 *msg, u8 *csum); |
| 518 | |
| 519 | /** |
| 520 | * Get the IO port range. implement this function if the EC requires different IO ports. |
| 521 | * |
| 522 | * @param base Pointer to the base of the IO port range |
| 523 | * @param size Pointer to the size of the IO port range |
| 524 | */ |
| 525 | void chipset_ioport_range(uint16_t *base, size_t *size); |
| 526 | |
| 527 | #endif /* _EC_GOOGLE_CHROMEEC_EC_H */ |