blob: d90d24c767dffa2de2750c269fcc9c414fe668ee [file] [log] [blame]
Stefan Reinauerd6682e82013-02-21 15:39:35 -08001/*
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 Reinauerd6682e82013-02-21 15:39:35 -080015 * 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 Wernera2148372019-11-13 19:50:33 -080020#include <types.h>
Duncan Lauriee2cea4f2015-12-01 19:14:09 -080021#include "ec_commands.h"
Stefan Reinauerd6682e82013-02-21 15:39:35 -080022
Aaron Durbinfbb3e6c2016-05-10 17:00:06 -050023/* Fill in base and size of the IO port resources used. */
24void google_chromeec_ioport_range(uint16_t *base, size_t *size);
25
Gabe Black9f96aa62013-06-28 14:24:33 -070026int google_chromeec_i2c_xfer(uint8_t chip, uint8_t addr, int alen,
27 uint8_t *buffer, int len, int is_read);
Furquan Shaikh8788fd62017-11-20 20:28:18 -080028uint64_t google_chromeec_get_wake_mask(void);
29int google_chromeec_set_sci_mask(uint64_t mask);
30int google_chromeec_set_smi_mask(uint64_t mask);
31int google_chromeec_set_wake_mask(uint64_t mask);
Tim Wawrzynczakb5c345a2019-10-24 10:12:01 -060032uint8_t google_chromeec_get_event(void);
Jenny TC1dfc2c32017-12-14 14:24:39 +053033
34/* Check if EC supports feature EC_FEATURE_UNIFIED_WAKE_MASKS */
35bool google_chromeec_is_uhepi_supported(void);
Stefan Reinauerd6682e82013-02-21 15:39:35 -080036int google_ec_running_ro(void);
Tim Wawrzynczak1966b5c802019-10-21 13:09:09 -060037enum ec_current_image google_chromeec_get_current_image(void);
Hung-Te Lin76720d02013-04-15 18:06:32 +080038void google_chromeec_init(void);
Daisuke Nojiri476c2c52018-01-26 17:36:44 -080039int google_chromeec_pd_get_amode(uint16_t svid);
Daisuke Nojirid182b632018-02-16 17:50:06 -080040int google_chromeec_wait_for_displayport(long timeout);
Stefan Reinauerd6682e82013-02-21 15:39:35 -080041
Duncan Laurie7378a172017-06-29 23:52:17 -070042/* Device events */
Furquan Shaikh8788fd62017-11-20 20:28:18 -080043uint64_t google_chromeec_get_device_enabled_events(void);
44int google_chromeec_set_device_enabled_events(uint64_t mask);
45uint64_t google_chromeec_get_device_current_events(void);
Duncan Laurie7378a172017-06-29 23:52:17 -070046
Duncan Lauriee2cea4f2015-12-01 19:14:09 -080047int google_chromeec_check_feature(int feature);
Hung-Te Lin6bfbb332013-04-15 18:27:24 +080048uint8_t google_chromeec_calc_checksum(const uint8_t *data, int size);
Karthikeyan Ramasubramanianc80ff842018-09-17 16:19:34 -060049
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 */
58int google_chromeec_get_board_version(uint32_t *version);
Tim Wawrzynczakb5c345a2019-10-24 10:12:01 -060059uint32_t google_chromeec_get_sku_id(void);
60int google_chromeec_set_sku_id(uint32_t skuid);
Furquan Shaikh8788fd62017-11-20 20:28:18 -080061uint64_t google_chromeec_get_events_b(void);
62int google_chromeec_clear_events_b(uint64_t mask);
Stefan Reinauerd6682e82013-02-21 15:39:35 -080063int google_chromeec_kbbacklight(int percent);
Tim Wawrzynczakb5c345a2019-10-24 10:12:01 -060064void google_chromeec_post(uint8_t postcode);
Stefan Reinaueraaaf6892013-08-29 15:57:11 -070065int google_chromeec_vbnv_context(int is_read, uint8_t *data, int len);
Duncan Laurie699c7882015-08-13 12:52:08 -070066uint8_t google_chromeec_get_switches(void);
You-Cheng Syu85bb8742019-03-12 13:02:18 +080067bool google_chromeec_get_ap_watchdog_flag(void);
Stefan Reinauerd6682e82013-02-21 15:39:35 -080068
Duncan Lauriee2cea4f2015-12-01 19:14:09 -080069/* Temporary secure storage commands */
70int google_chromeec_vstore_supported(void);
71int google_chromeec_vstore_info(uint32_t *locked);
72int google_chromeec_vstore_read(int slot, uint8_t *data);
73int google_chromeec_vstore_write(int slot, uint8_t *data, size_t size);
74
Aaron Durbine68d22f2017-05-04 12:32:52 -050075/* Issue reboot command to EC with specified type and flags. Returns 0 on
76 success, < 0 otherwise. */
77int google_chromeec_reboot(int dev_idx, enum ec_reboot_cmd type, uint8_t flags);
78
Daisuke Nojiri07f9748f2018-02-01 07:46:02 -080079/**
Jett Rink8db8a612020-01-14 11:41:47 -070080 * Get data from Cros Board Info
Daisuke Nojiri07f9748f2018-02-01 07:46:02 -080081 *
Jett Rink8db8a612020-01-14 11:41:47 -070082 * @param id/fw_config/buf [OUT] value from from CBI.
Daisuke Nojiri07f9748f2018-02-01 07:46:02 -080083 * @return 0 on success or negative integer for errors.
84 */
85int google_chromeec_cbi_get_oem_id(uint32_t *id);
86int google_chromeec_cbi_get_sku_id(uint32_t *id);
Jett Rink8db8a612020-01-14 11:41:47 -070087int google_chromeec_cbi_get_fw_config(uint32_t *fw_config);
Aaron Durbinb388c0e2018-08-07 12:24:21 -060088int google_chromeec_cbi_get_dram_part_num(char *buf, size_t bufsize);
Wisley Chenc1efec72018-11-06 09:28:23 +080089int google_chromeec_cbi_get_oem_name(char *buf, size_t bufsize);
Daisuke Nojiri07f9748f2018-02-01 07:46:02 -080090
Alexandru Gagniuc851ef962016-03-30 14:38:44 -070091/* 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 Nematbakhsh5725ea32015-04-01 16:52:37 -070095/* 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 Wawrzynczakb5c345a2019-10-24 10:12:01 -060099int google_chromeec_set_usb_charge_mode(uint8_t port_id, enum usb_charge_mode mode);
100int google_chromeec_set_usb_pd_role(uint8_t port, enum usb_pd_control_role role);
Shelley Chenebd53302017-09-29 14:15:11 -0700101/*
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 */
108int google_chromeec_get_usb_pd_power_info(enum usb_chg_type *type,
Tim Wawrzynczakb5c345a2019-10-24 10:12:01 -0600109 uint32_t *max_watts);
Stefan Reinauerd6682e82013-02-21 15:39:35 -0800110
Daisuke Nojiri93fd8fa2017-11-28 14:11:30 -0800111/*
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 Wawrzynczakb5c345a2019-10-24 10:12:01 -0600118int google_chromeec_override_dedicated_charger_limit(uint16_t current_lim,
119 uint16_t voltage_lim);
Daisuke Nojiri93fd8fa2017-11-28 14:11:30 -0800120
Hung-Te Lina904f9e2013-04-11 15:58:12 +0800121/* internal structure to send a command to the EC and wait for response. */
122struct chromeec_command {
Hung-Te Line946f982013-06-22 11:18:39 +0800123 uint16_t cmd_code; /* command code in, status out */
Hung-Te Lina904f9e2013-04-11 15:58:12 +0800124 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 Lauriefc0f5172014-09-18 12:54:02 -0700130 int cmd_dev_index;/* device index for passthru */
Hung-Te Lina904f9e2013-04-11 15:58:12 +0800131};
132
Aaron Durbin82827272014-08-06 14:34:57 -0500133/*
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 */
142void *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 */
149typedef int (*crosec_io_t)(size_t req_size, size_t resp_size, void *context);
Hung-Te Lin23fb9972013-06-21 20:11:47 +0800150int crosec_command_proto(struct chromeec_command *cec_command,
151 crosec_io_t crosec_io, void *context);
152
Daisuke Nojiri7bbed262019-02-25 13:56:43 -0800153/**
Tim Wawrzynczak1966b5c802019-10-21 13:09:09 -0600154 * Performs light verification of the EC<->AP communcation channel.
155 *
156 * @return 0 on success, -1 on error
157 */
158int google_chromeec_hello(void);
159
160/**
Daisuke Nojiri7bbed262019-02-25 13:56:43 -0800161 * 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 Lina904f9e2013-04-11 15:58:12 +0800166int google_chromeec_command(struct chromeec_command *cec_command);
167
Furquan Shaikh2749c522017-10-04 14:01:41 -0700168struct google_chromeec_event_info {
Furquan Shaikh8788fd62017-11-20 20:28:18 -0800169 uint64_t log_events;
170 uint64_t sci_events;
Furquan Shaikh1a5b7c62018-05-30 11:51:23 -0700171 uint64_t smi_events;
Furquan Shaikh8788fd62017-11-20 20:28:18 -0800172 uint64_t s3_wake_events;
173 uint64_t s3_device_events;
174 uint64_t s5_wake_events;
Jenny TC1dfc2c32017-12-14 14:24:39 +0530175 uint64_t s0ix_wake_events;
Furquan Shaikh2749c522017-10-04 14:01:41 -0700176};
177void google_chromeec_events_init(const struct google_chromeec_event_info *info,
178 bool is_s3_wakeup);
179
Furquan Shaikhe01bf642017-10-13 10:59:51 -0700180/*
181 * Get next available MKBP event in ec_response_get_next_event. Returns 0 on
182 * success, < 0 otherwise.
183 */
184int google_chromeec_get_mkbp_event(struct ec_response_get_next_event *event);
185
Furquan Shaikh70b257f2017-10-16 23:00:27 -0700186/* Log host events to eventlog based on the mask provided. */
Furquan Shaikh8788fd62017-11-20 20:28:18 -0800187void google_chromeec_log_events(uint64_t mask);
Furquan Shaikh70b257f2017-10-16 23:00:27 -0700188
Tim Wawrzynczak1966b5c802019-10-21 13:09:09 -0600189/**
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 */
197int 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 */
207int 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 */
218int 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 */
229int 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 */
238int 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 */
246int 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 */
257int 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 */
265int 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 */
273int 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 */
282int 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 */
290int 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 */
300int google_chromeec_get_cmd_versions(int command, uint32_t *pmask);
301
Stefan Reinauerd6682e82013-02-21 15:39:35 -0800302#endif /* _EC_GOOGLE_CHROMEEC_EC_H */