Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Chromium OS cros_ec driver |
| 4 | * |
| 5 | * Copyright (c) 2012 The Chromium OS Authors. |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef _CROS_EC_H |
| 9 | #define _CROS_EC_H |
| 10 | |
| 11 | #include <linux/compiler.h> |
| 12 | #include <ec_commands.h> |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 13 | #include <cros_ec_message.h> |
Simon Glass | 32f8a19 | 2015-01-05 20:05:32 -0700 | [diff] [blame] | 14 | #include <asm/gpio.h> |
Simon Glass | b7e0d73 | 2017-05-18 20:09:02 -0600 | [diff] [blame] | 15 | #include <dm/of_extra.h> |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 16 | |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 17 | /* Our configuration information */ |
| 18 | struct cros_ec_dev { |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 19 | struct udevice *dev; /* Transport device */ |
Simon Glass | 32f8a19 | 2015-01-05 20:05:32 -0700 | [diff] [blame] | 20 | struct gpio_desc ec_int; /* GPIO used as EC interrupt line */ |
Randall Spangler | e8c1266 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 21 | int protocol_version; /* Protocol version to use */ |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 22 | int optimise_flash_write; /* Don't write erased flash blocks */ |
| 23 | |
| 24 | /* |
| 25 | * These two buffers will always be dword-aligned and include enough |
| 26 | * space for up to 7 word-alignment bytes also, so we can ensure that |
| 27 | * the body of the message is always dword-aligned (64-bit). |
| 28 | * |
| 29 | * We use this alignment to keep ARM and x86 happy. Probably word |
| 30 | * alignment would be OK, there might be a small performance advantage |
| 31 | * to using dword. |
| 32 | */ |
| 33 | uint8_t din[ALIGN(MSG_BYTES + sizeof(int64_t), sizeof(int64_t))] |
| 34 | __aligned(sizeof(int64_t)); |
| 35 | uint8_t dout[ALIGN(MSG_BYTES + sizeof(int64_t), sizeof(int64_t))] |
| 36 | __aligned(sizeof(int64_t)); |
| 37 | }; |
| 38 | |
| 39 | /* |
| 40 | * Hard-code the number of columns we happen to know we have right now. It |
| 41 | * would be more correct to call cros_ec_info() at startup and determine the |
| 42 | * actual number of keyboard cols from there. |
| 43 | */ |
| 44 | #define CROS_EC_KEYSCAN_COLS 13 |
| 45 | |
| 46 | /* Information returned by a key scan */ |
| 47 | struct mbkp_keyscan { |
| 48 | uint8_t data[CROS_EC_KEYSCAN_COLS]; |
| 49 | }; |
| 50 | |
Simon Glass | d7f25f3 | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 51 | /* Holds information about the Chrome EC */ |
| 52 | struct fdt_cros_ec { |
| 53 | struct fmap_entry flash; /* Address and size of EC flash */ |
| 54 | /* |
| 55 | * Byte value of erased flash, or -1 if not known. It is normally |
| 56 | * 0xff but some flash devices use 0 (e.g. STM32Lxxx) |
| 57 | */ |
| 58 | int flash_erase_value; |
| 59 | struct fmap_entry region[EC_FLASH_REGION_COUNT]; |
| 60 | }; |
| 61 | |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 62 | /** |
| 63 | * Read the ID of the CROS-EC device |
| 64 | * |
| 65 | * The ID is a string identifying the CROS-EC device. |
| 66 | * |
| 67 | * @param dev CROS-EC device |
| 68 | * @param id Place to put the ID |
| 69 | * @param maxlen Maximum length of the ID field |
| 70 | * @return 0 if ok, -1 on error |
| 71 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 72 | int cros_ec_read_id(struct udevice *dev, char *id, int maxlen); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Read a keyboard scan from the CROS-EC device |
| 76 | * |
| 77 | * Send a message requesting a keyboard scan and return the result |
| 78 | * |
| 79 | * @param dev CROS-EC device |
| 80 | * @param scan Place to put the scan results |
| 81 | * @return 0 if ok, -1 on error |
| 82 | */ |
Simon Glass | 745009c | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 83 | int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 84 | |
| 85 | /** |
Alper Nebi Yasak | 6900797 | 2020-10-30 20:25:20 +0300 | [diff] [blame] | 86 | * Get the next pending MKBP event from the ChromeOS EC device. |
| 87 | * |
| 88 | * Send a message requesting the next event and return the result. |
| 89 | * |
| 90 | * @param event Place to put the event. |
| 91 | * @return 0 if ok, <0 on error. |
| 92 | */ |
| 93 | int cros_ec_get_next_event(struct udevice *dev, |
| 94 | struct ec_response_get_next_event *event); |
| 95 | |
| 96 | /** |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 97 | * Read which image is currently running on the CROS-EC device. |
| 98 | * |
| 99 | * @param dev CROS-EC device |
| 100 | * @param image Destination for image identifier |
| 101 | * @return 0 if ok, <0 on error |
| 102 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 103 | int cros_ec_read_current_image(struct udevice *dev, |
| 104 | enum ec_current_image *image); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Read the hash of the CROS-EC device firmware. |
| 108 | * |
| 109 | * @param dev CROS-EC device |
Simon Glass | a12ef7e | 2018-10-01 12:22:38 -0600 | [diff] [blame] | 110 | * @param hash_offset Offset in flash to read from |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 111 | * @param hash Destination for hash information |
| 112 | * @return 0 if ok, <0 on error |
| 113 | */ |
Simon Glass | a12ef7e | 2018-10-01 12:22:38 -0600 | [diff] [blame] | 114 | int cros_ec_read_hash(struct udevice *dev, uint hash_offset, |
| 115 | struct ec_response_vboot_hash *hash); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Send a reboot command to the CROS-EC device. |
| 119 | * |
| 120 | * Note that some reboot commands (such as EC_REBOOT_COLD) also reboot the AP. |
| 121 | * |
| 122 | * @param dev CROS-EC device |
| 123 | * @param cmd Reboot command |
| 124 | * @param flags Flags for reboot command (EC_REBOOT_FLAG_*) |
| 125 | * @return 0 if ok, <0 on error |
| 126 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 127 | int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Check if the CROS-EC device has an interrupt pending. |
| 131 | * |
| 132 | * Read the status of the external interrupt connected to the CROS-EC device. |
| 133 | * If no external interrupt is configured, this always returns 1. |
| 134 | * |
| 135 | * @param dev CROS-EC device |
| 136 | * @return 0 if no interrupt is pending |
| 137 | */ |
Simon Glass | 745009c | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 138 | int cros_ec_interrupt_pending(struct udevice *dev); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 139 | |
| 140 | enum { |
| 141 | CROS_EC_OK, |
| 142 | CROS_EC_ERR = 1, |
| 143 | CROS_EC_ERR_FDT_DECODE, |
| 144 | CROS_EC_ERR_CHECK_VERSION, |
| 145 | CROS_EC_ERR_READ_ID, |
| 146 | CROS_EC_ERR_DEV_INIT, |
| 147 | }; |
| 148 | |
| 149 | /** |
Simon Glass | 836bb6e | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 150 | * Initialise the Chromium OS EC driver |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 151 | * |
| 152 | * @param blob Device tree blob containing setup information |
| 153 | * @param cros_ecp Returns pointer to the cros_ec device, or NULL if none |
| 154 | * @return 0 if we got an cros_ec device and all is well (or no cros_ec is |
| 155 | * expected), -ve if we should have an cros_ec device but failed to find |
| 156 | * one, or init failed (-CROS_EC_ERR_...). |
| 157 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 158 | int cros_ec_init(const void *blob, struct udevice**cros_ecp); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * Read information about the keyboard matrix |
| 162 | * |
| 163 | * @param dev CROS-EC device |
| 164 | * @param info Place to put the info structure |
| 165 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 166 | int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * Read the host event flags |
| 170 | * |
| 171 | * @param dev CROS-EC device |
| 172 | * @param events_ptr Destination for event flags. Not changed on error. |
| 173 | * @return 0 if ok, <0 on error |
| 174 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 175 | int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * Clear the specified host event flags |
| 179 | * |
| 180 | * @param dev CROS-EC device |
| 181 | * @param events Event flags to clear |
| 182 | * @return 0 if ok, <0 on error |
| 183 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 184 | int cros_ec_clear_host_events(struct udevice *dev, uint32_t events); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * Get/set flash protection |
| 188 | * |
| 189 | * @param dev CROS-EC device |
| 190 | * @param set_mask Mask of flags to set; if 0, just retrieves existing |
| 191 | * protection state without changing it. |
| 192 | * @param set_flags New flag values; only bits in set_mask are applied; |
| 193 | * ignored if set_mask=0. |
| 194 | * @param prot Destination for updated protection state from EC. |
| 195 | * @return 0 if ok, <0 on error |
| 196 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 197 | int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask, |
| 198 | uint32_t set_flags, |
| 199 | struct ec_response_flash_protect *resp); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 200 | |
Simon Glass | 72ef8bf | 2018-11-06 15:21:22 -0700 | [diff] [blame] | 201 | /** |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 202 | * Run internal tests on the cros_ec interface. |
| 203 | * |
| 204 | * @param dev CROS-EC device |
| 205 | * @return 0 if ok, <0 if the test failed |
| 206 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 207 | int cros_ec_test(struct udevice *dev); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * Update the EC RW copy. |
| 211 | * |
| 212 | * @param dev CROS-EC device |
| 213 | * @param image the content to write |
| 214 | * @param imafge_size content length |
| 215 | * @return 0 if ok, <0 if the test failed |
| 216 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 217 | int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image, |
| 218 | int image_size); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * Return a pointer to the board's CROS-EC device |
| 222 | * |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 223 | * @return pointer to CROS-EC device, or NULL if none is available |
| 224 | */ |
Simon Glass | 42116f6 | 2018-10-01 12:22:23 -0600 | [diff] [blame] | 225 | struct udevice *board_get_cros_ec_dev(void); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 226 | |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 227 | struct dm_cros_ec_ops { |
Simon Glass | 2b4b653 | 2021-01-16 14:52:27 -0700 | [diff] [blame] | 228 | /** |
| 229 | * check_version() - Check the protocol version being used (optional) |
| 230 | * |
| 231 | * If provided, this function should check that the EC can be supported |
| 232 | * by the driver. If not provided, HELLO messages will be sent to try |
| 233 | * to determine the protocol version. |
| 234 | * |
| 235 | * @dev: Device to check |
| 236 | * @return 0 if the protocol is valid, -ve if not supported |
| 237 | */ |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 238 | int (*check_version)(struct udevice *dev); |
Simon Glass | 2b4b653 | 2021-01-16 14:52:27 -0700 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * command() - Old-style command interface |
| 242 | * |
| 243 | * This sends a command and receives a response (deprecated, use |
| 244 | * packet()) |
| 245 | * |
| 246 | * @dev: Device to use |
| 247 | * @cmd: Command to send (only supports 0-0xff) |
| 248 | * @cmd_version: Version of command to send (often 0) |
| 249 | * @dout: Output data (may be NULL If dout_len=0) |
| 250 | * @dout_len: Length of output data excluding 4-byte header |
| 251 | * @dinp: On input, set to point to input data, often struct |
| 252 | * cros_ec_dev->din - typically this is left alone but may be |
| 253 | * updated by the driver |
| 254 | * @din_len: Maximum length of response |
| 255 | * @return number of bytes in response, or -ve on error |
| 256 | */ |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 257 | int (*command)(struct udevice *dev, uint8_t cmd, int cmd_version, |
| 258 | const uint8_t *dout, int dout_len, |
| 259 | uint8_t **dinp, int din_len); |
Simon Glass | 2b4b653 | 2021-01-16 14:52:27 -0700 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * packet() - New-style command interface |
| 263 | * |
| 264 | * This interface is preferred over command(), since it is typically |
| 265 | * easier to implement. |
| 266 | * |
| 267 | * @dev: Device to use |
| 268 | * @out_bytes: Number of bytes to send (from struct cros_ec_dev->dout) |
| 269 | * @in_bytes: Maximum number of bytes to expect in response |
| 270 | * @return number of bytes in response, or -ve on error |
| 271 | */ |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 272 | int (*packet)(struct udevice *dev, int out_bytes, int in_bytes); |
Simon Glass | 3a6c994 | 2021-01-16 14:52:28 -0700 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * get_switches() - Get value of EC switches |
| 276 | * |
| 277 | * This is currently supported on the LPC EC. |
| 278 | * |
| 279 | * @dev: Device to use |
| 280 | * @return current switches value, or -ENOSYS if not supported |
| 281 | */ |
| 282 | int (*get_switches)(struct udevice *dev); |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | #define dm_cros_ec_get_ops(dev) \ |
| 286 | ((struct dm_cros_ec_ops *)(dev)->driver->ops) |
| 287 | |
| 288 | int cros_ec_register(struct udevice *dev); |
| 289 | |
Randall Spangler | a607028 | 2014-02-27 13:26:10 -0700 | [diff] [blame] | 290 | /** |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 291 | * Dump a block of data for a command. |
| 292 | * |
| 293 | * @param name Name for data (e.g. 'in', 'out') |
| 294 | * @param cmd Command number associated with data, or -1 for none |
| 295 | * @param data Data block to dump |
| 296 | * @param len Length of data block to dump |
| 297 | */ |
| 298 | void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len); |
| 299 | |
| 300 | /** |
| 301 | * Calculate a simple 8-bit checksum of a data block |
| 302 | * |
| 303 | * @param data Data block to checksum |
| 304 | * @param size Size of data block in bytes |
| 305 | * @return checksum value (0 to 255) |
| 306 | */ |
| 307 | int cros_ec_calc_checksum(const uint8_t *data, int size); |
| 308 | |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 309 | int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * Read data from the flash |
| 313 | * |
| 314 | * Read an arbitrary amount of data from the EC flash, by repeatedly reading |
| 315 | * small blocks. |
| 316 | * |
| 317 | * The offset starts at 0. You can obtain the region information from |
| 318 | * cros_ec_flash_offset() to find out where to read for a particular region. |
| 319 | * |
| 320 | * @param dev CROS-EC device |
| 321 | * @param data Pointer to data buffer to read into |
| 322 | * @param offset Offset within flash to read from |
| 323 | * @param size Number of bytes to read |
| 324 | * @return 0 if ok, -1 on error |
| 325 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 326 | int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset, |
| 327 | uint32_t size); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 328 | |
| 329 | /** |
Moritz Fischer | bfeba01 | 2016-10-04 17:08:08 -0700 | [diff] [blame] | 330 | * Read back flash parameters |
| 331 | * |
| 332 | * This function reads back parameters of the flash as reported by the EC |
| 333 | * |
| 334 | * @param dev Pointer to device |
| 335 | * @param info Pointer to output flash info struct |
| 336 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 337 | int cros_ec_read_flashinfo(struct udevice *dev, |
| 338 | struct ec_response_flash_info *info); |
Moritz Fischer | bfeba01 | 2016-10-04 17:08:08 -0700 | [diff] [blame] | 339 | |
| 340 | /** |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 341 | * Write data to the flash |
| 342 | * |
| 343 | * Write an arbitrary amount of data to the EC flash, by repeatedly writing |
| 344 | * small blocks. |
| 345 | * |
| 346 | * The offset starts at 0. You can obtain the region information from |
| 347 | * cros_ec_flash_offset() to find out where to write for a particular region. |
| 348 | * |
| 349 | * Attempting to write to the region where the EC is currently running from |
| 350 | * will result in an error. |
| 351 | * |
| 352 | * @param dev CROS-EC device |
| 353 | * @param data Pointer to data buffer to write |
| 354 | * @param offset Offset within flash to write to. |
| 355 | * @param size Number of bytes to write |
| 356 | * @return 0 if ok, -1 on error |
| 357 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 358 | int cros_ec_flash_write(struct udevice *dev, const uint8_t *data, |
| 359 | uint32_t offset, uint32_t size); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 360 | |
| 361 | /** |
| 362 | * Obtain position and size of a flash region |
| 363 | * |
| 364 | * @param dev CROS-EC device |
| 365 | * @param region Flash region to query |
| 366 | * @param offset Returns offset of flash region in EC flash |
| 367 | * @param size Returns size of flash region |
| 368 | * @return 0 if ok, -1 on error |
| 369 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 370 | int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region, |
| 371 | uint32_t *offset, uint32_t *size); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 372 | |
| 373 | /** |
Simon Glass | 7791df5 | 2021-01-16 14:52:25 -0700 | [diff] [blame] | 374 | * cros_ec_get_sku_id() - Read the SKU ID |
| 375 | * |
| 376 | * @dev: CROS-EC device |
| 377 | * return SKU ID, or -ve on error |
| 378 | */ |
| 379 | int cros_ec_get_sku_id(struct udevice *dev); |
| 380 | |
| 381 | /** |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 382 | * Read/write non-volatile data from/to a CROS-EC device. |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 383 | * |
| 384 | * @param dev CROS-EC device |
| 385 | * @param block Buffer of VbNvContext to be read/write |
| 386 | * @return 0 if ok, -1 on error |
| 387 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 388 | int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size); |
| 389 | int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 390 | |
| 391 | /** |
| 392 | * Read the version information for the EC images |
| 393 | * |
| 394 | * @param dev CROS-EC device |
| 395 | * @param versionp This is set to point to the version information |
| 396 | * @return 0 if ok, -1 on error |
| 397 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 398 | int cros_ec_read_version(struct udevice *dev, |
| 399 | struct ec_response_get_version **versionp); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 400 | |
| 401 | /** |
| 402 | * Read the build information for the EC |
| 403 | * |
| 404 | * @param dev CROS-EC device |
| 405 | * @param versionp This is set to point to the build string |
| 406 | * @return 0 if ok, -1 on error |
| 407 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 408 | int cros_ec_read_build_info(struct udevice *dev, char **strp); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 409 | |
| 410 | /** |
| 411 | * Switch on/off a LDO / FET. |
| 412 | * |
| 413 | * @param dev CROS-EC device |
| 414 | * @param index index of the LDO/FET to switch |
| 415 | * @param state new state of the LDO/FET : EC_LDO_STATE_ON|OFF |
| 416 | * @return 0 if ok, -1 on error |
| 417 | */ |
Simon Glass | f48eaf0 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 418 | int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 419 | |
| 420 | /** |
| 421 | * Read back a LDO / FET current state. |
| 422 | * |
| 423 | * @param dev CROS-EC device |
| 424 | * @param index index of the LDO/FET to switch |
| 425 | * @param state current state of the LDO/FET : EC_LDO_STATE_ON|OFF |
| 426 | * @return 0 if ok, -1 on error |
| 427 | */ |
Simon Glass | f48eaf0 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 428 | int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state); |
Vadim Bendebury | 41364f0 | 2014-02-27 13:26:02 -0700 | [diff] [blame] | 429 | |
| 430 | /** |
Vadim Bendebury | 41364f0 | 2014-02-27 13:26:02 -0700 | [diff] [blame] | 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 | |
Simon Glass | d7f25f3 | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 440 | /** |
| 441 | * Returns information from the FDT about the Chrome EC flash |
| 442 | * |
Simon Glass | 2ec9d17 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 443 | * @param dev Device to read from |
Simon Glass | d7f25f3 | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 444 | * @param config Structure to use to return information |
| 445 | */ |
Simon Glass | 2ec9d17 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 446 | int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config); |
Simon Glass | d7f25f3 | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 447 | |
Simon Glass | df93d90 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 448 | /** |
| 449 | * Check the current keyboard state, in case recovery mode is requested. |
| 450 | * This function is for sandbox only. |
| 451 | * |
| 452 | * @param ec CROS-EC device |
| 453 | */ |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 454 | void cros_ec_check_keyboard(struct udevice *dev); |
Simon Glass | df93d90 | 2014-02-27 13:26:12 -0700 | [diff] [blame] | 455 | |
Simon Glass | cc456bd | 2015-08-03 08:19:23 -0600 | [diff] [blame] | 456 | struct i2c_msg; |
| 457 | /* |
| 458 | * Tunnel an I2C transfer to the EC |
| 459 | * |
| 460 | * @param dev CROS-EC device |
Moritz Fischer | 6d1a718 | 2016-09-27 15:42:07 -0700 | [diff] [blame] | 461 | * @param port The remote port on EC to use |
Simon Glass | cc456bd | 2015-08-03 08:19:23 -0600 | [diff] [blame] | 462 | * @param msg List of messages to transfer |
| 463 | * @param nmsgs Number of messages to transfer |
| 464 | */ |
Moritz Fischer | 6d1a718 | 2016-09-27 15:42:07 -0700 | [diff] [blame] | 465 | int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *msg, |
| 466 | int nmsgs); |
Simon Glass | cc456bd | 2015-08-03 08:19:23 -0600 | [diff] [blame] | 467 | |
Simon Glass | 72ef8bf | 2018-11-06 15:21:22 -0700 | [diff] [blame] | 468 | /** |
| 469 | * cros_ec_get_events_b() - Get event mask B |
| 470 | * |
| 471 | * @return value of event mask, default value of 0 if it could not be read |
| 472 | */ |
| 473 | uint64_t cros_ec_get_events_b(struct udevice *dev); |
| 474 | |
| 475 | /** |
| 476 | * cros_ec_clear_events_b() - Clear even mask B |
| 477 | * |
| 478 | * Any pending events in the B range are cleared |
| 479 | * |
| 480 | * @return 0 if OK, -ve on error |
| 481 | */ |
| 482 | int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask); |
| 483 | |
| 484 | /** |
| 485 | * cros_ec_efs_verify() - tell the EC to verify one of its images |
| 486 | * |
| 487 | * @param dev CROS-EC device |
| 488 | * @param region Flash region to query |
| 489 | * @return 0 if OK, -ve on error |
| 490 | */ |
| 491 | int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region); |
| 492 | |
| 493 | /** |
| 494 | * cros_ec_battery_cutoff() - Request that the battery be cut off |
| 495 | * |
| 496 | * This tells the battery to stop supplying power. This is used before shipping |
| 497 | * a device to ensure that the battery remains charged while the device is |
| 498 | * shipped or sitting on the shelf waiting to be purchased. |
| 499 | * |
| 500 | * @param dev CROS-EC device |
| 501 | * @param flags Flags to use (EC_BATTERY_CUTOFF_FLAG_...) |
| 502 | * @return 0 if OK, -ve on error |
| 503 | */ |
| 504 | int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags); |
| 505 | |
| 506 | /** |
Alper Nebi Yasak | 1b9ee28 | 2020-10-22 23:49:27 +0300 | [diff] [blame] | 507 | * cros_ec_set_pwm_duty() - Set duty cycle of a generic pwm |
| 508 | * |
| 509 | * Note that duty value needs to be passed to the EC as a 16 bit number |
| 510 | * for increased precision. |
| 511 | * |
| 512 | * @param dev CROS-EC device |
| 513 | * @param index Index of the pwm |
| 514 | * @param duty Desired duty cycle, in 0..EC_PWM_MAX_DUTY range. |
| 515 | * @return 0 if OK, -ve on error |
| 516 | */ |
| 517 | int cros_ec_set_pwm_duty(struct udevice *dev, uint8_t index, uint16_t duty); |
| 518 | |
| 519 | /** |
Simon Glass | 72ef8bf | 2018-11-06 15:21:22 -0700 | [diff] [blame] | 520 | * cros_ec_read_limit_power() - Check if power is limited by batter/charger |
| 521 | * |
| 522 | * Sometimes the battery is low and / or the device is connected to a charger |
| 523 | * that cannot supply much power. |
| 524 | * |
| 525 | * @param dev CROS-EC device |
| 526 | * @param limit_powerp Returns whether power is limited (0 or 1) |
| 527 | * @return 0 if OK, -ENOSYS if the EC does not support this comment, -EINVAL |
| 528 | * if the EC returned an invalid response |
| 529 | */ |
| 530 | int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp); |
| 531 | |
| 532 | /** |
| 533 | * cros_ec_config_powerbtn() - Configure the behaviour of the power button |
| 534 | * |
| 535 | * @param dev CROS-EC device |
| 536 | * @param flags Flags to use (EC_POWER_BUTTON_...) |
| 537 | * @return 0 if OK, -ve on error |
| 538 | */ |
| 539 | int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags); |
| 540 | |
| 541 | /** |
| 542 | * cros_ec_get_lid_shutdown_mask() - Set the lid shutdown mask |
| 543 | * |
| 544 | * Determines whether a lid close event is reported |
| 545 | * |
| 546 | * @param dev CROS-EC device |
| 547 | * @return shufdown mas if OK, -ve on error |
| 548 | */ |
| 549 | int cros_ec_get_lid_shutdown_mask(struct udevice *dev); |
| 550 | |
| 551 | /** |
| 552 | * cros_ec_set_lid_shutdown_mask() - Set the lid shutdown mask |
| 553 | * |
| 554 | * Set whether a lid close event is reported |
| 555 | * |
| 556 | * @param dev CROS-EC device |
| 557 | * @param enable true to enable reporting, false to disable |
| 558 | * @return shufdown mas if OK, -ve on error |
| 559 | */ |
| 560 | int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable); |
| 561 | |
Simon Glass | d8e9a93 | 2021-01-16 14:52:22 -0700 | [diff] [blame] | 562 | /** |
| 563 | * cros_ec_hello() - Send a hello message |
| 564 | * |
| 565 | * Sends a message with a fixed input value and checks that the expected output |
| 566 | * value is received |
| 567 | * |
| 568 | * @dev: CROS-EC device |
| 569 | * @handshakep: If non-NULL, returns received handshake value on error |
| 570 | * @return 0 if OK, -ve on error |
| 571 | */ |
| 572 | int cros_ec_hello(struct udevice *dev, uint *handshakep); |
Simon Glass | 8aec32f | 2021-01-16 14:52:26 -0700 | [diff] [blame] | 573 | |
| 574 | /** |
| 575 | * cros_ec_get_features() - Get the set of features provided by the EC |
| 576 | * |
| 577 | * See enum ec_feature_code for the list of available features |
| 578 | * |
| 579 | * @dev: CROS-EC device |
| 580 | * @featuresp: Returns a bitmask of supported features |
| 581 | * @return 0 if OK, -ve on error |
| 582 | */ |
| 583 | int cros_ec_get_features(struct udevice *dev, u64 *featuresp); |
| 584 | |
| 585 | /** |
| 586 | * cros_ec_check_feature() - Check if a feature is supported |
| 587 | * |
| 588 | * @dev: CROS-EC device |
| 589 | * @feature: Feature number to check (enum ec_feature_code) |
| 590 | * @return true if supported, false if not, -ve on error |
| 591 | */ |
| 592 | int cros_ec_check_feature(struct udevice *dev, uint feature); |
| 593 | |
Simon Glass | 3a6c994 | 2021-01-16 14:52:28 -0700 | [diff] [blame] | 594 | /** |
| 595 | * cros_ec_get_switches() - Get switches value |
| 596 | * |
| 597 | * @dev: CROS-EC device |
| 598 | * @return switches value, or -ENOSYS if not supported, or other -ve value on |
| 599 | * other error |
| 600 | */ |
| 601 | int cros_ec_get_switches(struct udevice *dev); |
| 602 | |
Simon Glass | 10f7465 | 2021-01-16 14:52:31 -0700 | [diff] [blame] | 603 | /** |
| 604 | * cros_ec_vstore_supported() - Check if vstore is supported |
| 605 | * |
| 606 | * @dev: CROS-EC device |
| 607 | * @return false if not supported, true if supported, -ve on error |
| 608 | */ |
| 609 | int cros_ec_vstore_supported(struct udevice *dev); |
| 610 | |
| 611 | /** |
| 612 | * cros_ec_vstore_info() - Get vstore information |
| 613 | * |
| 614 | * @dev: CROS-EC device |
| 615 | * @lockedp: mask of locked slots |
| 616 | * @return number of vstore slots supported by the EC,, -ve on error |
| 617 | */ |
| 618 | int cros_ec_vstore_info(struct udevice *dev, u32 *lockedp); |
| 619 | |
| 620 | /** |
| 621 | * cros_ec_vstore_read() - Read data from EC vstore slot |
| 622 | * |
| 623 | * @dev: CROS-EC device |
| 624 | * @slot: vstore slot to read from |
| 625 | * @data: buffer to store read data, must be EC_VSTORE_SLOT_SIZE bytes |
| 626 | * @return 0 if OK, -ve on error |
| 627 | */ |
| 628 | int cros_ec_vstore_read(struct udevice *dev, int slot, uint8_t *data); |
| 629 | |
| 630 | /** |
| 631 | * cros_ec_vstore_write() - Save data into EC vstore slot |
| 632 | * |
| 633 | * The maximum size of data is EC_VSTORE_SLOT_SIZE. It is the caller's |
| 634 | * responsibility to check the number of implemented slots by querying the |
| 635 | * vstore info. |
| 636 | * |
| 637 | * @dev: CROS-EC device |
| 638 | * @slot: vstore slot to write into |
| 639 | * @data: data to write |
| 640 | * @size: size of data in bytes |
| 641 | * @return 0 if OK, -ve on error |
| 642 | */ |
| 643 | int cros_ec_vstore_write(struct udevice *dev, int slot, const uint8_t *data, |
| 644 | size_t size); |
| 645 | |
Simon Glass | 201efb2 | 2021-07-05 16:32:49 -0600 | [diff] [blame] | 646 | /** |
| 647 | * cros_ec_read_batt_charge() - Read the battery-charge state |
| 648 | * |
| 649 | * @dev: CROS-EC device |
| 650 | * @chargep: Return battery-charge state as a percentage |
| 651 | */ |
| 652 | int cros_ec_read_batt_charge(struct udevice *dev, uint *chargep); |
| 653 | |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 654 | #endif |