Stefan Reinauer | d6682e8 | 2013-02-21 15:39:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 The Chromium OS Authors. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
Stefan Reinauer | d6682e8 | 2013-02-21 15:39:35 -0800 | [diff] [blame] | 15 | * Mailbox EC communication interface for Google Chrome Embedded Controller. |
| 16 | */ |
| 17 | |
| 18 | #ifndef _EC_GOOGLE_CHROMEEC_EC_H |
| 19 | #define _EC_GOOGLE_CHROMEEC_EC_H |
Julius Werner | a214837 | 2019-11-13 19:50:33 -0800 | [diff] [blame] | 20 | #include <types.h> |
Duncan Laurie | e2cea4f | 2015-12-01 19:14:09 -0800 | [diff] [blame] | 21 | #include "ec_commands.h" |
Stefan Reinauer | d6682e8 | 2013-02-21 15:39:35 -0800 | [diff] [blame] | 22 | |
Aaron Durbin | fbb3e6c | 2016-05-10 17:00:06 -0500 | [diff] [blame] | 23 | /* Fill in base and size of the IO port resources used. */ |
| 24 | void google_chromeec_ioport_range(uint16_t *base, size_t *size); |
| 25 | |
Gabe Black | 9f96aa6 | 2013-06-28 14:24:33 -0700 | [diff] [blame] | 26 | int google_chromeec_i2c_xfer(uint8_t chip, uint8_t addr, int alen, |
| 27 | uint8_t *buffer, int len, int is_read); |
Furquan Shaikh | 8788fd6 | 2017-11-20 20:28:18 -0800 | [diff] [blame] | 28 | uint64_t google_chromeec_get_wake_mask(void); |
| 29 | int google_chromeec_set_sci_mask(uint64_t mask); |
| 30 | int google_chromeec_set_smi_mask(uint64_t mask); |
| 31 | int google_chromeec_set_wake_mask(uint64_t mask); |
Tim Wawrzynczak | b5c345a | 2019-10-24 10:12:01 -0600 | [diff] [blame] | 32 | uint8_t google_chromeec_get_event(void); |
Jenny TC | 1dfc2c3 | 2017-12-14 14:24:39 +0530 | [diff] [blame] | 33 | |
| 34 | /* Check if EC supports feature EC_FEATURE_UNIFIED_WAKE_MASKS */ |
| 35 | bool google_chromeec_is_uhepi_supported(void); |
Stefan Reinauer | d6682e8 | 2013-02-21 15:39:35 -0800 | [diff] [blame] | 36 | int google_ec_running_ro(void); |
Tim Wawrzynczak | 1966b5c80 | 2019-10-21 13:09:09 -0600 | [diff] [blame] | 37 | enum ec_current_image google_chromeec_get_current_image(void); |
Hung-Te Lin | 76720d0 | 2013-04-15 18:06:32 +0800 | [diff] [blame] | 38 | void google_chromeec_init(void); |
Daisuke Nojiri | 476c2c5 | 2018-01-26 17:36:44 -0800 | [diff] [blame] | 39 | int google_chromeec_pd_get_amode(uint16_t svid); |
Daisuke Nojiri | d182b63 | 2018-02-16 17:50:06 -0800 | [diff] [blame] | 40 | int google_chromeec_wait_for_displayport(long timeout); |
Stefan Reinauer | d6682e8 | 2013-02-21 15:39:35 -0800 | [diff] [blame] | 41 | |
Duncan Laurie | 7378a17 | 2017-06-29 23:52:17 -0700 | [diff] [blame] | 42 | /* Device events */ |
Furquan Shaikh | 8788fd6 | 2017-11-20 20:28:18 -0800 | [diff] [blame] | 43 | uint64_t google_chromeec_get_device_enabled_events(void); |
| 44 | int google_chromeec_set_device_enabled_events(uint64_t mask); |
| 45 | uint64_t google_chromeec_get_device_current_events(void); |
Duncan Laurie | 7378a17 | 2017-06-29 23:52:17 -0700 | [diff] [blame] | 46 | |
Duncan Laurie | e2cea4f | 2015-12-01 19:14:09 -0800 | [diff] [blame] | 47 | int google_chromeec_check_feature(int feature); |
Hung-Te Lin | 6bfbb33 | 2013-04-15 18:27:24 +0800 | [diff] [blame] | 48 | uint8_t google_chromeec_calc_checksum(const uint8_t *data, int size); |
Karthikeyan Ramasubramanian | c80ff84 | 2018-09-17 16:19:34 -0600 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * google_chromeec_get_board_version() - Get the board version |
| 52 | * @version: Out parameter to retrieve the board Version |
| 53 | * |
| 54 | * Return: 0 on success or -1 on failure/error. |
| 55 | * |
| 56 | * This function is used to get the board version information from EC. |
| 57 | */ |
| 58 | int google_chromeec_get_board_version(uint32_t *version); |
Tim Wawrzynczak | b5c345a | 2019-10-24 10:12:01 -0600 | [diff] [blame] | 59 | uint32_t google_chromeec_get_sku_id(void); |
| 60 | int google_chromeec_set_sku_id(uint32_t skuid); |
Furquan Shaikh | 8788fd6 | 2017-11-20 20:28:18 -0800 | [diff] [blame] | 61 | uint64_t google_chromeec_get_events_b(void); |
| 62 | int google_chromeec_clear_events_b(uint64_t mask); |
Stefan Reinauer | d6682e8 | 2013-02-21 15:39:35 -0800 | [diff] [blame] | 63 | int google_chromeec_kbbacklight(int percent); |
Tim Wawrzynczak | b5c345a | 2019-10-24 10:12:01 -0600 | [diff] [blame] | 64 | void google_chromeec_post(uint8_t postcode); |
Stefan Reinauer | aaaf689 | 2013-08-29 15:57:11 -0700 | [diff] [blame] | 65 | int google_chromeec_vbnv_context(int is_read, uint8_t *data, int len); |
Duncan Laurie | 699c788 | 2015-08-13 12:52:08 -0700 | [diff] [blame] | 66 | uint8_t google_chromeec_get_switches(void); |
You-Cheng Syu | 85bb874 | 2019-03-12 13:02:18 +0800 | [diff] [blame] | 67 | bool google_chromeec_get_ap_watchdog_flag(void); |
Stefan Reinauer | d6682e8 | 2013-02-21 15:39:35 -0800 | [diff] [blame] | 68 | |
Duncan Laurie | e2cea4f | 2015-12-01 19:14:09 -0800 | [diff] [blame] | 69 | /* Temporary secure storage commands */ |
| 70 | int google_chromeec_vstore_supported(void); |
| 71 | int google_chromeec_vstore_info(uint32_t *locked); |
| 72 | int google_chromeec_vstore_read(int slot, uint8_t *data); |
| 73 | int google_chromeec_vstore_write(int slot, uint8_t *data, size_t size); |
| 74 | |
Aaron Durbin | e68d22f | 2017-05-04 12:32:52 -0500 | [diff] [blame] | 75 | /* Issue reboot command to EC with specified type and flags. Returns 0 on |
| 76 | success, < 0 otherwise. */ |
| 77 | int google_chromeec_reboot(int dev_idx, enum ec_reboot_cmd type, uint8_t flags); |
| 78 | |
Daisuke Nojiri | 07f9748f | 2018-02-01 07:46:02 -0800 | [diff] [blame] | 79 | /** |
Jett Rink | 8db8a61 | 2020-01-14 11:41:47 -0700 | [diff] [blame^] | 80 | * Get data from Cros Board Info |
Daisuke Nojiri | 07f9748f | 2018-02-01 07:46:02 -0800 | [diff] [blame] | 81 | * |
Jett Rink | 8db8a61 | 2020-01-14 11:41:47 -0700 | [diff] [blame^] | 82 | * @param id/fw_config/buf [OUT] value from from CBI. |
Daisuke Nojiri | 07f9748f | 2018-02-01 07:46:02 -0800 | [diff] [blame] | 83 | * @return 0 on success or negative integer for errors. |
| 84 | */ |
| 85 | int google_chromeec_cbi_get_oem_id(uint32_t *id); |
| 86 | int google_chromeec_cbi_get_sku_id(uint32_t *id); |
Jett Rink | 8db8a61 | 2020-01-14 11:41:47 -0700 | [diff] [blame^] | 87 | int google_chromeec_cbi_get_fw_config(uint32_t *fw_config); |
Aaron Durbin | b388c0e | 2018-08-07 12:24:21 -0600 | [diff] [blame] | 88 | int google_chromeec_cbi_get_dram_part_num(char *buf, size_t bufsize); |
Wisley Chen | c1efec7 | 2018-11-06 09:28:23 +0800 | [diff] [blame] | 89 | int google_chromeec_cbi_get_oem_name(char *buf, size_t bufsize); |
Daisuke Nojiri | 07f9748f | 2018-02-01 07:46:02 -0800 | [diff] [blame] | 90 | |
Alexandru Gagniuc | 851ef96 | 2016-03-30 14:38:44 -0700 | [diff] [blame] | 91 | /* MEC uses 0x800/0x804 as register/index pair, thus an 8-byte resource. */ |
| 92 | #define MEC_EMI_BASE 0x800 |
| 93 | #define MEC_EMI_SIZE 8 |
| 94 | |
Shawn Nematbakhsh | 5725ea3 | 2015-04-01 16:52:37 -0700 | [diff] [blame] | 95 | /* For MEC, access ranges 0x800 thru 0x9ff using EMI interface instead of LPC */ |
| 96 | #define MEC_EMI_RANGE_START EC_HOST_CMD_REGION0 |
| 97 | #define MEC_EMI_RANGE_END (EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE) |
| 98 | |
Tim Wawrzynczak | b5c345a | 2019-10-24 10:12:01 -0600 | [diff] [blame] | 99 | int google_chromeec_set_usb_charge_mode(uint8_t port_id, enum usb_charge_mode mode); |
| 100 | int google_chromeec_set_usb_pd_role(uint8_t port, enum usb_pd_control_role role); |
Shelley Chen | ebd5330 | 2017-09-29 14:15:11 -0700 | [diff] [blame] | 101 | /* |
| 102 | * Retrieve the charger type and max wattage. |
| 103 | * |
| 104 | * @param type charger type |
| 105 | * @param max_watts charger max wattage |
| 106 | * @return non-zero for error, otherwise 0. |
| 107 | */ |
| 108 | int google_chromeec_get_usb_pd_power_info(enum usb_chg_type *type, |
Tim Wawrzynczak | b5c345a | 2019-10-24 10:12:01 -0600 | [diff] [blame] | 109 | uint32_t *max_watts); |
Stefan Reinauer | d6682e8 | 2013-02-21 15:39:35 -0800 | [diff] [blame] | 110 | |
Daisuke Nojiri | 93fd8fa | 2017-11-28 14:11:30 -0800 | [diff] [blame] | 111 | /* |
| 112 | * Set max current and voltage of a dedicated charger. |
| 113 | * |
| 114 | * @param current_lim Max current in mA |
| 115 | * @param voltage_lim Max voltage in mV |
| 116 | * @return non-zero for error, otherwise 0. |
| 117 | */ |
Tim Wawrzynczak | b5c345a | 2019-10-24 10:12:01 -0600 | [diff] [blame] | 118 | int google_chromeec_override_dedicated_charger_limit(uint16_t current_lim, |
| 119 | uint16_t voltage_lim); |
Daisuke Nojiri | 93fd8fa | 2017-11-28 14:11:30 -0800 | [diff] [blame] | 120 | |
Hung-Te Lin | a904f9e | 2013-04-11 15:58:12 +0800 | [diff] [blame] | 121 | /* internal structure to send a command to the EC and wait for response. */ |
| 122 | struct chromeec_command { |
Hung-Te Lin | e946f98 | 2013-06-22 11:18:39 +0800 | [diff] [blame] | 123 | uint16_t cmd_code; /* command code in, status out */ |
Hung-Te Lin | a904f9e | 2013-04-11 15:58:12 +0800 | [diff] [blame] | 124 | uint8_t cmd_version; /* command version */ |
| 125 | const void* cmd_data_in; /* command data, if any */ |
| 126 | void* cmd_data_out; /* command response, if any */ |
| 127 | uint16_t cmd_size_in; /* size of command data */ |
| 128 | uint16_t cmd_size_out; /* expected size of command response in, |
| 129 | * actual received size out */ |
Duncan Laurie | fc0f517 | 2014-09-18 12:54:02 -0700 | [diff] [blame] | 130 | int cmd_dev_index;/* device index for passthru */ |
Hung-Te Lin | a904f9e | 2013-04-11 15:58:12 +0800 | [diff] [blame] | 131 | }; |
| 132 | |
Aaron Durbin | 8282727 | 2014-08-06 14:34:57 -0500 | [diff] [blame] | 133 | /* |
| 134 | * There are transport level constraints for sending protov3 packets. Because |
| 135 | * of this provide a way for the generic protocol layer to request buffers |
| 136 | * so that there is zero copying being done through the layers. |
| 137 | * |
| 138 | * Request the buffer provided the size. If 'req' is non-zero then the |
| 139 | * buffer requested is for EC requests. Otherwise it's for responses. Return |
| 140 | * non-NULL on success, NULL on error. |
| 141 | */ |
| 142 | void *crosec_get_buffer(size_t size, int req); |
| 143 | |
| 144 | /* |
| 145 | * The lower level transport works on the buffers handed out to the |
| 146 | * upper level. Therefore, only the size of the request and response |
| 147 | * are required. |
| 148 | */ |
| 149 | typedef int (*crosec_io_t)(size_t req_size, size_t resp_size, void *context); |
Hung-Te Lin | 23fb997 | 2013-06-21 20:11:47 +0800 | [diff] [blame] | 150 | int crosec_command_proto(struct chromeec_command *cec_command, |
| 151 | crosec_io_t crosec_io, void *context); |
| 152 | |
Daisuke Nojiri | 7bbed26 | 2019-02-25 13:56:43 -0800 | [diff] [blame] | 153 | /** |
Tim Wawrzynczak | 1966b5c80 | 2019-10-21 13:09:09 -0600 | [diff] [blame] | 154 | * Performs light verification of the EC<->AP communcation channel. |
| 155 | * |
| 156 | * @return 0 on success, -1 on error |
| 157 | */ |
| 158 | int google_chromeec_hello(void); |
| 159 | |
| 160 | /** |
Daisuke Nojiri | 7bbed26 | 2019-02-25 13:56:43 -0800 | [diff] [blame] | 161 | * Send a command to a CrOS EC |
| 162 | * |
| 163 | * @param cec_command: CrOS EC command to send |
| 164 | * @return 0 for success. Non-zero for error. |
| 165 | */ |
Hung-Te Lin | a904f9e | 2013-04-11 15:58:12 +0800 | [diff] [blame] | 166 | int google_chromeec_command(struct chromeec_command *cec_command); |
| 167 | |
Furquan Shaikh | 2749c52 | 2017-10-04 14:01:41 -0700 | [diff] [blame] | 168 | struct google_chromeec_event_info { |
Furquan Shaikh | 8788fd6 | 2017-11-20 20:28:18 -0800 | [diff] [blame] | 169 | uint64_t log_events; |
| 170 | uint64_t sci_events; |
Furquan Shaikh | 1a5b7c6 | 2018-05-30 11:51:23 -0700 | [diff] [blame] | 171 | uint64_t smi_events; |
Furquan Shaikh | 8788fd6 | 2017-11-20 20:28:18 -0800 | [diff] [blame] | 172 | uint64_t s3_wake_events; |
| 173 | uint64_t s3_device_events; |
| 174 | uint64_t s5_wake_events; |
Jenny TC | 1dfc2c3 | 2017-12-14 14:24:39 +0530 | [diff] [blame] | 175 | uint64_t s0ix_wake_events; |
Furquan Shaikh | 2749c52 | 2017-10-04 14:01:41 -0700 | [diff] [blame] | 176 | }; |
| 177 | void google_chromeec_events_init(const struct google_chromeec_event_info *info, |
| 178 | bool is_s3_wakeup); |
| 179 | |
Furquan Shaikh | e01bf64 | 2017-10-13 10:59:51 -0700 | [diff] [blame] | 180 | /* |
| 181 | * Get next available MKBP event in ec_response_get_next_event. Returns 0 on |
| 182 | * success, < 0 otherwise. |
| 183 | */ |
| 184 | int google_chromeec_get_mkbp_event(struct ec_response_get_next_event *event); |
| 185 | |
Furquan Shaikh | 70b257f | 2017-10-16 23:00:27 -0700 | [diff] [blame] | 186 | /* Log host events to eventlog based on the mask provided. */ |
Furquan Shaikh | 8788fd6 | 2017-11-20 20:28:18 -0800 | [diff] [blame] | 187 | void google_chromeec_log_events(uint64_t mask); |
Furquan Shaikh | 70b257f | 2017-10-16 23:00:27 -0700 | [diff] [blame] | 188 | |
Tim Wawrzynczak | 1966b5c80 | 2019-10-21 13:09:09 -0600 | [diff] [blame] | 189 | /** |
| 190 | * Protect/un-protect EC flash regions. |
| 191 | * |
| 192 | * @param mask Set/clear the requested bits in 'flags' |
| 193 | * @param flags Flash protection flags |
| 194 | * @param resp Pointer to response structure |
| 195 | * @return 0 on success, -1 on error |
| 196 | */ |
| 197 | int google_chromeec_flash_protect(uint32_t mask, uint32_t flags, |
| 198 | struct ec_response_flash_protect *resp); |
| 199 | /** |
| 200 | * Calculate image hash for vboot. |
| 201 | * |
| 202 | * @param hash_type The hash types supported by the EC for vboot |
| 203 | * @param offset The offset to start hashing in flash |
| 204 | * @param resp Pointer to response structure |
| 205 | * @return 0 on success, -1 on error |
| 206 | */ |
| 207 | int google_chromeec_start_vboot_hash(enum ec_vboot_hash_type hash_type, |
| 208 | uint32_t offset, |
| 209 | struct ec_response_vboot_hash *resp); |
| 210 | /** |
| 211 | * Return the EC's vboot image hash. |
| 212 | * |
| 213 | * @param offset Get hash for flash region beginning here |
| 214 | * @param resp Pointer to response structure |
| 215 | * @return 0 on success, -1 on error |
| 216 | * |
| 217 | */ |
| 218 | int google_chromeec_get_vboot_hash(uint32_t offset, |
| 219 | struct ec_response_vboot_hash *resp); |
| 220 | |
| 221 | /** |
| 222 | * Get offset and size of the specified EC flash region. |
| 223 | * |
| 224 | * @param region Which region of EC flash |
| 225 | * @param offset Gets filled with region's offset |
| 226 | * @param size Gets filled with region's size |
| 227 | * @return 0 on success, -1 on error |
| 228 | */ |
| 229 | int google_chromeec_flash_region_info(enum ec_flash_region region, |
| 230 | uint32_t *offset, uint32_t *size); |
| 231 | /** |
| 232 | * Erase a region of EC flash. |
| 233 | * |
| 234 | * @param offset Where to begin erasing |
| 235 | * @param size Size of area to erase |
| 236 | * @return 0 on success, -1 on error |
| 237 | */ |
| 238 | int google_chromeec_flash_erase(uint32_t region_offset, uint32_t region_size); |
| 239 | |
| 240 | /** |
| 241 | * Return information about the entire flash. |
| 242 | * |
| 243 | * @param info Pointer to response structure |
| 244 | * @return 0 on success, -1 on error |
| 245 | */ |
| 246 | int google_chromeec_flash_info(struct ec_response_flash_info *info); |
| 247 | |
| 248 | /** |
| 249 | * Write a block into EC flash. |
| 250 | * |
| 251 | * @param data Pointer to data to write to flash, prefixed by a |
| 252 | * struct ec_params_flash_write |
| 253 | * @param offset Offset to begin writing data |
| 254 | * @param size Number of bytes to be written to flash from data |
| 255 | * @return 0 on success, -1 on error |
| 256 | */ |
| 257 | int google_chromeec_flash_write_block(const uint8_t *data, uint32_t size); |
| 258 | |
| 259 | /** |
| 260 | * Verify flash using EFS if available. |
| 261 | * |
| 262 | * @param region Which flash region to verify |
| 263 | * @return 0 on success, -1 on error |
| 264 | */ |
| 265 | int google_chromeec_efs_verify(enum ec_flash_region region); |
| 266 | |
| 267 | /** |
| 268 | * Command EC to perform battery cutoff. |
| 269 | * |
| 270 | * @param flags Flags to pass to the EC |
| 271 | * @return 0 on success, -1 on error |
| 272 | */ |
| 273 | int google_chromeec_battery_cutoff(uint8_t flags); |
| 274 | |
| 275 | /** |
| 276 | * Check if the EC is requesting the system to limit input power. |
| 277 | * |
| 278 | * @param limit_power If successful, limit_power is 1 if EC is requesting |
| 279 | * input power limits, otherwise 0. |
| 280 | * @return 0 on success, -1 on error |
| 281 | */ |
| 282 | int google_chromeec_read_limit_power_request(int *limit_power); |
| 283 | |
| 284 | /** |
| 285 | * Get information about the protocol that the EC speaks. |
| 286 | * |
| 287 | * @param resp Filled with host command protocol information. |
| 288 | * @return 0 on success, -1 on error |
| 289 | */ |
| 290 | int google_chromeec_get_protocol_info( |
| 291 | struct ec_response_get_protocol_info *resp); |
| 292 | |
| 293 | /** |
| 294 | * Get available versions of the specified command. |
| 295 | * |
| 296 | * @param command Command ID |
| 297 | * @param pmask Pointer to version mask |
| 298 | * @return 0 on success, -1 on error |
| 299 | */ |
| 300 | int google_chromeec_get_cmd_versions(int command, uint32_t *pmask); |
| 301 | |
Stefan Reinauer | d6682e8 | 2013-02-21 15:39:35 -0800 | [diff] [blame] | 302 | #endif /* _EC_GOOGLE_CHROMEEC_EC_H */ |