1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Chromium OS cros_ec driver
5 * Copyright (c) 2012 The Chromium OS Authors.
11 #include <linux/compiler.h>
12 #include <ec_commands.h>
13 #include <cros_ec_message.h>
15 #include <dm/of_extra.h>
17 /* Our configuration information */
19 struct udevice *dev; /* Transport device */
20 struct gpio_desc ec_int; /* GPIO used as EC interrupt line */
21 int protocol_version; /* Protocol version to use */
22 int optimise_flash_write; /* Don't write erased flash blocks */
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).
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
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));
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.
44 #define CROS_EC_KEYSCAN_COLS 13
46 /* Information returned by a key scan */
48 uint8_t data[CROS_EC_KEYSCAN_COLS];
51 /* Holds information about the Chrome EC */
53 struct fmap_entry flash; /* Address and size of EC flash */
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)
58 int flash_erase_value;
59 struct fmap_entry region[EC_FLASH_REGION_COUNT];
63 * Read the ID of the CROS-EC device
65 * The ID is a string identifying the CROS-EC device.
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
72 int cros_ec_read_id(struct udevice *dev, char *id, int maxlen);
75 * Read a keyboard scan from the CROS-EC device
77 * Send a message requesting a keyboard scan and return the result
79 * @param dev CROS-EC device
80 * @param scan Place to put the scan results
81 * @return 0 if ok, -1 on error
83 int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan);
86 * Read which image is currently running on the CROS-EC device.
88 * @param dev CROS-EC device
89 * @param image Destination for image identifier
90 * @return 0 if ok, <0 on error
92 int cros_ec_read_current_image(struct udevice *dev,
93 enum ec_current_image *image);
96 * Read the hash of the CROS-EC device firmware.
98 * @param dev CROS-EC device
99 * @param hash Destination for hash information
100 * @return 0 if ok, <0 on error
102 int cros_ec_read_hash(struct udevice *dev, struct ec_response_vboot_hash *hash);
105 * Send a reboot command to the CROS-EC device.
107 * Note that some reboot commands (such as EC_REBOOT_COLD) also reboot the AP.
109 * @param dev CROS-EC device
110 * @param cmd Reboot command
111 * @param flags Flags for reboot command (EC_REBOOT_FLAG_*)
112 * @return 0 if ok, <0 on error
114 int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags);
117 * Check if the CROS-EC device has an interrupt pending.
119 * Read the status of the external interrupt connected to the CROS-EC device.
120 * If no external interrupt is configured, this always returns 1.
122 * @param dev CROS-EC device
123 * @return 0 if no interrupt is pending
125 int cros_ec_interrupt_pending(struct udevice *dev);
130 CROS_EC_ERR_FDT_DECODE,
131 CROS_EC_ERR_CHECK_VERSION,
133 CROS_EC_ERR_DEV_INIT,
137 * Initialise the Chromium OS EC driver
139 * @param blob Device tree blob containing setup information
140 * @param cros_ecp Returns pointer to the cros_ec device, or NULL if none
141 * @return 0 if we got an cros_ec device and all is well (or no cros_ec is
142 * expected), -ve if we should have an cros_ec device but failed to find
143 * one, or init failed (-CROS_EC_ERR_...).
145 int cros_ec_init(const void *blob, struct udevice**cros_ecp);
148 * Read information about the keyboard matrix
150 * @param dev CROS-EC device
151 * @param info Place to put the info structure
153 int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info);
156 * Read the host event flags
158 * @param dev CROS-EC device
159 * @param events_ptr Destination for event flags. Not changed on error.
160 * @return 0 if ok, <0 on error
162 int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr);
165 * Clear the specified host event flags
167 * @param dev CROS-EC device
168 * @param events Event flags to clear
169 * @return 0 if ok, <0 on error
171 int cros_ec_clear_host_events(struct udevice *dev, uint32_t events);
174 * Get/set flash protection
176 * @param dev CROS-EC device
177 * @param set_mask Mask of flags to set; if 0, just retrieves existing
178 * protection state without changing it.
179 * @param set_flags New flag values; only bits in set_mask are applied;
180 * ignored if set_mask=0.
181 * @param prot Destination for updated protection state from EC.
182 * @return 0 if ok, <0 on error
184 int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
186 struct ec_response_flash_protect *resp);
190 * Run internal tests on the cros_ec interface.
192 * @param dev CROS-EC device
193 * @return 0 if ok, <0 if the test failed
195 int cros_ec_test(struct udevice *dev);
198 * Update the EC RW copy.
200 * @param dev CROS-EC device
201 * @param image the content to write
202 * @param imafge_size content length
203 * @return 0 if ok, <0 if the test failed
205 int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
209 * Return a pointer to the board's CROS-EC device
211 * @return pointer to CROS-EC device, or NULL if none is available
213 struct udevice *board_get_cros_ec_dev(void);
215 struct dm_cros_ec_ops {
216 int (*check_version)(struct udevice *dev);
217 int (*command)(struct udevice *dev, uint8_t cmd, int cmd_version,
218 const uint8_t *dout, int dout_len,
219 uint8_t **dinp, int din_len);
220 int (*packet)(struct udevice *dev, int out_bytes, int in_bytes);
223 #define dm_cros_ec_get_ops(dev) \
224 ((struct dm_cros_ec_ops *)(dev)->driver->ops)
226 int cros_ec_register(struct udevice *dev);
229 * Dump a block of data for a command.
231 * @param name Name for data (e.g. 'in', 'out')
232 * @param cmd Command number associated with data, or -1 for none
233 * @param data Data block to dump
234 * @param len Length of data block to dump
236 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len);
239 * Calculate a simple 8-bit checksum of a data block
241 * @param data Data block to checksum
242 * @param size Size of data block in bytes
243 * @return checksum value (0 to 255)
245 int cros_ec_calc_checksum(const uint8_t *data, int size);
247 int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size);
250 * Read data from the flash
252 * Read an arbitrary amount of data from the EC flash, by repeatedly reading
255 * The offset starts at 0. You can obtain the region information from
256 * cros_ec_flash_offset() to find out where to read for a particular region.
258 * @param dev CROS-EC device
259 * @param data Pointer to data buffer to read into
260 * @param offset Offset within flash to read from
261 * @param size Number of bytes to read
262 * @return 0 if ok, -1 on error
264 int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
268 * Read back flash parameters
270 * This function reads back parameters of the flash as reported by the EC
272 * @param dev Pointer to device
273 * @param info Pointer to output flash info struct
275 int cros_ec_read_flashinfo(struct udevice *dev,
276 struct ec_response_flash_info *info);
279 * Write data to the flash
281 * Write an arbitrary amount of data to the EC flash, by repeatedly writing
284 * The offset starts at 0. You can obtain the region information from
285 * cros_ec_flash_offset() to find out where to write for a particular region.
287 * Attempting to write to the region where the EC is currently running from
288 * will result in an error.
290 * @param dev CROS-EC device
291 * @param data Pointer to data buffer to write
292 * @param offset Offset within flash to write to.
293 * @param size Number of bytes to write
294 * @return 0 if ok, -1 on error
296 int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
297 uint32_t offset, uint32_t size);
300 * Obtain position and size of a flash region
302 * @param dev CROS-EC device
303 * @param region Flash region to query
304 * @param offset Returns offset of flash region in EC flash
305 * @param size Returns size of flash region
306 * @return 0 if ok, -1 on error
308 int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
309 uint32_t *offset, uint32_t *size);
312 * Read/write non-volatile data from/to a CROS-EC device.
314 * @param dev CROS-EC device
315 * @param block Buffer of VbNvContext to be read/write
316 * @return 0 if ok, -1 on error
318 int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size);
319 int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size);
322 * Read the version information for the EC images
324 * @param dev CROS-EC device
325 * @param versionp This is set to point to the version information
326 * @return 0 if ok, -1 on error
328 int cros_ec_read_version(struct udevice *dev,
329 struct ec_response_get_version **versionp);
332 * Read the build information for the EC
334 * @param dev CROS-EC device
335 * @param versionp This is set to point to the build string
336 * @return 0 if ok, -1 on error
338 int cros_ec_read_build_info(struct udevice *dev, char **strp);
341 * Switch on/off a LDO / FET.
343 * @param dev CROS-EC device
344 * @param index index of the LDO/FET to switch
345 * @param state new state of the LDO/FET : EC_LDO_STATE_ON|OFF
346 * @return 0 if ok, -1 on error
348 int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state);
351 * Read back a LDO / FET current state.
353 * @param dev CROS-EC device
354 * @param index index of the LDO/FET to switch
355 * @param state current state of the LDO/FET : EC_LDO_STATE_ON|OFF
356 * @return 0 if ok, -1 on error
358 int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state);
361 * Get access to the error reported when cros_ec_board_init() was called
363 * This permits delayed reporting of the EC error if it failed during
366 * @return error (0 if there was no error, -ve if there was an error)
368 int cros_ec_get_error(void);
371 * Returns information from the FDT about the Chrome EC flash
373 * @param dev Device to read from
374 * @param config Structure to use to return information
376 int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config);
379 * Check the current keyboard state, in case recovery mode is requested.
380 * This function is for sandbox only.
382 * @param ec CROS-EC device
384 void cros_ec_check_keyboard(struct udevice *dev);
388 * Tunnel an I2C transfer to the EC
390 * @param dev CROS-EC device
391 * @param port The remote port on EC to use
392 * @param msg List of messages to transfer
393 * @param nmsgs Number of messages to transfer
395 int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *msg,