blob: f187bd0d4b5bc5d67e7cbb2e1014a044d87256c6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08002/*
3 * Chromium OS cros_ec driver
4 *
5 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +08006 */
7
8#ifndef _CROS_EC_H
9#define _CROS_EC_H
10
11#include <linux/compiler.h>
12#include <ec_commands.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080013#include <cros_ec_message.h>
Simon Glass0d7cc472015-01-05 20:05:32 -070014#include <asm/gpio.h>
Simon Glass0fbf66b2017-05-18 20:09:02 -060015#include <dm/of_extra.h>
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080016
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080017/* Our configuration information */
18struct cros_ec_dev {
Simon Glass44569b52014-10-13 23:42:14 -060019 struct udevice *dev; /* Transport device */
Simon Glass0d7cc472015-01-05 20:05:32 -070020 struct gpio_desc ec_int; /* GPIO used as EC interrupt line */
Randall Spanglerf03e9702014-02-27 13:26:08 -070021 int protocol_version; /* Protocol version to use */
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080022 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 */
47struct mbkp_keyscan {
48 uint8_t data[CROS_EC_KEYSCAN_COLS];
49};
50
Simon Glass3c59abc2014-02-27 13:26:03 -070051/* Holds information about the Chrome EC */
52struct 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 Tyanc48ca88f2013-05-15 18:27:28 +080062/**
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 Glassbed43522018-10-01 12:22:22 -060072int cros_ec_read_id(struct udevice *dev, char *id, int maxlen);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080073
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 Glassfb726402015-10-18 21:17:14 -060083int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080084
85/**
Alper Nebi Yasak1ce07292020-10-30 20:25:20 +030086 * 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 */
93int cros_ec_get_next_event(struct udevice *dev,
94 struct ec_response_get_next_event *event);
95
96/**
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080097 * 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 Glassbed43522018-10-01 12:22:22 -0600103int cros_ec_read_current_image(struct udevice *dev,
104 enum ec_current_image *image);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800105
106/**
107 * Read the hash of the CROS-EC device firmware.
108 *
109 * @param dev CROS-EC device
Simon Glass94564902018-10-01 12:22:38 -0600110 * @param hash_offset Offset in flash to read from
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800111 * @param hash Destination for hash information
112 * @return 0 if ok, <0 on error
113 */
Simon Glass94564902018-10-01 12:22:38 -0600114int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
115 struct ec_response_vboot_hash *hash);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800116
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 Glassbed43522018-10-01 12:22:22 -0600127int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800128
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 Glassfb726402015-10-18 21:17:14 -0600138int cros_ec_interrupt_pending(struct udevice *dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800139
140enum {
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 Glassece28862014-02-27 13:26:07 -0700150 * Initialise the Chromium OS EC driver
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800151 *
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 Glassbed43522018-10-01 12:22:22 -0600158int cros_ec_init(const void *blob, struct udevice**cros_ecp);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800159
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 Glassbed43522018-10-01 12:22:22 -0600166int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800167
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 Glassbed43522018-10-01 12:22:22 -0600175int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800176
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 Glassbed43522018-10-01 12:22:22 -0600184int cros_ec_clear_host_events(struct udevice *dev, uint32_t events);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800185
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 Glassbed43522018-10-01 12:22:22 -0600197int 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 Tyanc48ca88f2013-05-15 18:27:28 +0800200
Simon Glass153f2522018-11-06 15:21:22 -0700201/**
202 * Notify EC of current boot mode
203 *
204 * @param dev CROS-EC device
205 * @param vboot_mode Verified boot mode
206 * @return 0 if ok, <0 on error
207 */
208int cros_ec_entering_mode(struct udevice *dev, int mode);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800209
210/**
211 * Run internal tests on the cros_ec interface.
212 *
213 * @param dev CROS-EC device
214 * @return 0 if ok, <0 if the test failed
215 */
Simon Glassbed43522018-10-01 12:22:22 -0600216int cros_ec_test(struct udevice *dev);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800217
218/**
219 * Update the EC RW copy.
220 *
221 * @param dev CROS-EC device
222 * @param image the content to write
223 * @param imafge_size content length
224 * @return 0 if ok, <0 if the test failed
225 */
Simon Glassbed43522018-10-01 12:22:22 -0600226int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
227 int image_size);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800228
229/**
230 * Return a pointer to the board's CROS-EC device
231 *
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800232 * @return pointer to CROS-EC device, or NULL if none is available
233 */
Simon Glassb2ed2222018-10-01 12:22:23 -0600234struct udevice *board_get_cros_ec_dev(void);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800235
Simon Glass44569b52014-10-13 23:42:14 -0600236struct dm_cros_ec_ops {
237 int (*check_version)(struct udevice *dev);
238 int (*command)(struct udevice *dev, uint8_t cmd, int cmd_version,
239 const uint8_t *dout, int dout_len,
240 uint8_t **dinp, int din_len);
241 int (*packet)(struct udevice *dev, int out_bytes, int in_bytes);
242};
243
244#define dm_cros_ec_get_ops(dev) \
245 ((struct dm_cros_ec_ops *)(dev)->driver->ops)
246
247int cros_ec_register(struct udevice *dev);
248
Randall Spanglerce3ebb92014-02-27 13:26:10 -0700249/**
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800250 * Dump a block of data for a command.
251 *
252 * @param name Name for data (e.g. 'in', 'out')
253 * @param cmd Command number associated with data, or -1 for none
254 * @param data Data block to dump
255 * @param len Length of data block to dump
256 */
257void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len);
258
259/**
260 * Calculate a simple 8-bit checksum of a data block
261 *
262 * @param data Data block to checksum
263 * @param size Size of data block in bytes
264 * @return checksum value (0 to 255)
265 */
266int cros_ec_calc_checksum(const uint8_t *data, int size);
267
Simon Glassbed43522018-10-01 12:22:22 -0600268int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800269
270/**
271 * Read data from the flash
272 *
273 * Read an arbitrary amount of data from the EC flash, by repeatedly reading
274 * small blocks.
275 *
276 * The offset starts at 0. You can obtain the region information from
277 * cros_ec_flash_offset() to find out where to read for a particular region.
278 *
279 * @param dev CROS-EC device
280 * @param data Pointer to data buffer to read into
281 * @param offset Offset within flash to read from
282 * @param size Number of bytes to read
283 * @return 0 if ok, -1 on error
284 */
Simon Glassbed43522018-10-01 12:22:22 -0600285int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
286 uint32_t size);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800287
288/**
Moritz Fischer62b30272016-10-04 17:08:08 -0700289 * Read back flash parameters
290 *
291 * This function reads back parameters of the flash as reported by the EC
292 *
293 * @param dev Pointer to device
294 * @param info Pointer to output flash info struct
295 */
Simon Glassbed43522018-10-01 12:22:22 -0600296int cros_ec_read_flashinfo(struct udevice *dev,
297 struct ec_response_flash_info *info);
Moritz Fischer62b30272016-10-04 17:08:08 -0700298
299/**
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800300 * Write data to the flash
301 *
302 * Write an arbitrary amount of data to the EC flash, by repeatedly writing
303 * small blocks.
304 *
305 * The offset starts at 0. You can obtain the region information from
306 * cros_ec_flash_offset() to find out where to write for a particular region.
307 *
308 * Attempting to write to the region where the EC is currently running from
309 * will result in an error.
310 *
311 * @param dev CROS-EC device
312 * @param data Pointer to data buffer to write
313 * @param offset Offset within flash to write to.
314 * @param size Number of bytes to write
315 * @return 0 if ok, -1 on error
316 */
Simon Glassbed43522018-10-01 12:22:22 -0600317int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
318 uint32_t offset, uint32_t size);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800319
320/**
321 * Obtain position and size of a flash region
322 *
323 * @param dev CROS-EC device
324 * @param region Flash region to query
325 * @param offset Returns offset of flash region in EC flash
326 * @param size Returns size of flash region
327 * @return 0 if ok, -1 on error
328 */
Simon Glassbed43522018-10-01 12:22:22 -0600329int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
330 uint32_t *offset, uint32_t *size);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800331
332/**
Simon Glassbed43522018-10-01 12:22:22 -0600333 * Read/write non-volatile data from/to a CROS-EC device.
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800334 *
335 * @param dev CROS-EC device
336 * @param block Buffer of VbNvContext to be read/write
337 * @return 0 if ok, -1 on error
338 */
Simon Glassbed43522018-10-01 12:22:22 -0600339int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size);
340int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800341
342/**
343 * Read the version information for the EC images
344 *
345 * @param dev CROS-EC device
346 * @param versionp This is set to point to the version information
347 * @return 0 if ok, -1 on error
348 */
Simon Glassbed43522018-10-01 12:22:22 -0600349int cros_ec_read_version(struct udevice *dev,
350 struct ec_response_get_version **versionp);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800351
352/**
353 * Read the build information for the EC
354 *
355 * @param dev CROS-EC device
356 * @param versionp This is set to point to the build string
357 * @return 0 if ok, -1 on error
358 */
Simon Glassbed43522018-10-01 12:22:22 -0600359int cros_ec_read_build_info(struct udevice *dev, char **strp);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800360
361/**
362 * Switch on/off a LDO / FET.
363 *
364 * @param dev CROS-EC device
365 * @param index index of the LDO/FET to switch
366 * @param state new state of the LDO/FET : EC_LDO_STATE_ON|OFF
367 * @return 0 if ok, -1 on error
368 */
Simon Glasseb2cc512015-08-03 08:19:24 -0600369int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state);
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800370
371/**
372 * Read back a LDO / FET current state.
373 *
374 * @param dev CROS-EC device
375 * @param index index of the LDO/FET to switch
376 * @param state current state of the LDO/FET : EC_LDO_STATE_ON|OFF
377 * @return 0 if ok, -1 on error
378 */
Simon Glasseb2cc512015-08-03 08:19:24 -0600379int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state);
Vadim Bendeburye5d15792014-02-27 13:26:02 -0700380
381/**
Vadim Bendeburye5d15792014-02-27 13:26:02 -0700382 * Get access to the error reported when cros_ec_board_init() was called
383 *
384 * This permits delayed reporting of the EC error if it failed during
385 * early init.
386 *
387 * @return error (0 if there was no error, -ve if there was an error)
388 */
389int cros_ec_get_error(void);
390
Simon Glass3c59abc2014-02-27 13:26:03 -0700391/**
392 * Returns information from the FDT about the Chrome EC flash
393 *
Simon Glassb35e6d62017-05-18 20:09:23 -0600394 * @param dev Device to read from
Simon Glass3c59abc2014-02-27 13:26:03 -0700395 * @param config Structure to use to return information
396 */
Simon Glassb35e6d62017-05-18 20:09:23 -0600397int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config);
Simon Glass3c59abc2014-02-27 13:26:03 -0700398
Simon Glass36170f02014-02-27 13:26:12 -0700399/**
400 * Check the current keyboard state, in case recovery mode is requested.
401 * This function is for sandbox only.
402 *
403 * @param ec CROS-EC device
404 */
Simon Glassbed43522018-10-01 12:22:22 -0600405void cros_ec_check_keyboard(struct udevice *dev);
Simon Glass36170f02014-02-27 13:26:12 -0700406
Simon Glass9ad07af2015-08-03 08:19:23 -0600407struct i2c_msg;
Simon Glass9b0d1c92014-02-27 13:26:14 -0700408/*
409 * Tunnel an I2C transfer to the EC
410 *
411 * @param dev CROS-EC device
Moritz Fischer6cddc602016-09-27 15:42:07 -0700412 * @param port The remote port on EC to use
Simon Glass9ad07af2015-08-03 08:19:23 -0600413 * @param msg List of messages to transfer
414 * @param nmsgs Number of messages to transfer
415 */
Moritz Fischer6cddc602016-09-27 15:42:07 -0700416int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *msg,
417 int nmsgs);
Simon Glass9ad07af2015-08-03 08:19:23 -0600418
Simon Glass153f2522018-11-06 15:21:22 -0700419/**
420 * cros_ec_get_events_b() - Get event mask B
421 *
422 * @return value of event mask, default value of 0 if it could not be read
423 */
424uint64_t cros_ec_get_events_b(struct udevice *dev);
425
426/**
427 * cros_ec_clear_events_b() - Clear even mask B
428 *
429 * Any pending events in the B range are cleared
430 *
431 * @return 0 if OK, -ve on error
432 */
433int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask);
434
435/**
436 * cros_ec_efs_verify() - tell the EC to verify one of its images
437 *
438 * @param dev CROS-EC device
439 * @param region Flash region to query
440 * @return 0 if OK, -ve on error
441 */
442int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region);
443
444/**
445 * cros_ec_battery_cutoff() - Request that the battery be cut off
446 *
447 * This tells the battery to stop supplying power. This is used before shipping
448 * a device to ensure that the battery remains charged while the device is
449 * shipped or sitting on the shelf waiting to be purchased.
450 *
451 * @param dev CROS-EC device
452 * @param flags Flags to use (EC_BATTERY_CUTOFF_FLAG_...)
453 * @return 0 if OK, -ve on error
454 */
455int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags);
456
457/**
458 * cros_ec_read_limit_power() - Check if power is limited by batter/charger
459 *
460 * Sometimes the battery is low and / or the device is connected to a charger
461 * that cannot supply much power.
462 *
463 * @param dev CROS-EC device
464 * @param limit_powerp Returns whether power is limited (0 or 1)
465 * @return 0 if OK, -ENOSYS if the EC does not support this comment, -EINVAL
466 * if the EC returned an invalid response
467 */
468int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp);
469
470/**
471 * cros_ec_config_powerbtn() - Configure the behaviour of the power button
472 *
473 * @param dev CROS-EC device
474 * @param flags Flags to use (EC_POWER_BUTTON_...)
475 * @return 0 if OK, -ve on error
476 */
477int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags);
478
479/**
480 * cros_ec_get_lid_shutdown_mask() - Set the lid shutdown mask
481 *
482 * Determines whether a lid close event is reported
483 *
484 * @param dev CROS-EC device
485 * @return shufdown mas if OK, -ve on error
486 */
487int cros_ec_get_lid_shutdown_mask(struct udevice *dev);
488
489/**
490 * cros_ec_set_lid_shutdown_mask() - Set the lid shutdown mask
491 *
492 * Set whether a lid close event is reported
493 *
494 * @param dev CROS-EC device
495 * @param enable true to enable reporting, false to disable
496 * @return shufdown mas if OK, -ve on error
497 */
498int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable);
499
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +0800500#endif