ASoC: cros_ec_codec: read max DMIC gain from EC codec
[linux-2.6-block.git] / include / linux / platform_data / cros_ec_commands.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Host communication command constants for ChromeOS EC
4  *
5  * Copyright (C) 2012 Google, Inc
6  *
7  * NOTE: This file is auto-generated from ChromeOS EC Open Source code from
8  * https://chromium.googlesource.com/chromiumos/platform/ec/+/master/include/ec_commands.h
9  */
10
11 /* Host communication command constants for Chrome EC */
12
13 #ifndef __CROS_EC_COMMANDS_H
14 #define __CROS_EC_COMMANDS_H
15
16
17
18
19 #define BUILD_ASSERT(_cond)
20
21 /*
22  * Current version of this protocol
23  *
24  * TODO(crosbug.com/p/11223): This is effectively useless; protocol is
25  * determined in other ways.  Remove this once the kernel code no longer
26  * depends on it.
27  */
28 #define EC_PROTO_VERSION          0x00000002
29
30 /* Command version mask */
31 #define EC_VER_MASK(version) BIT(version)
32
33 /* I/O addresses for ACPI commands */
34 #define EC_LPC_ADDR_ACPI_DATA  0x62
35 #define EC_LPC_ADDR_ACPI_CMD   0x66
36
37 /* I/O addresses for host command */
38 #define EC_LPC_ADDR_HOST_DATA  0x200
39 #define EC_LPC_ADDR_HOST_CMD   0x204
40
41 /* I/O addresses for host command args and params */
42 /* Protocol version 2 */
43 #define EC_LPC_ADDR_HOST_ARGS    0x800  /* And 0x801, 0x802, 0x803 */
44 #define EC_LPC_ADDR_HOST_PARAM   0x804  /* For version 2 params; size is
45                                          * EC_PROTO2_MAX_PARAM_SIZE
46                                          */
47 /* Protocol version 3 */
48 #define EC_LPC_ADDR_HOST_PACKET  0x800  /* Offset of version 3 packet */
49 #define EC_LPC_HOST_PACKET_SIZE  0x100  /* Max size of version 3 packet */
50
51 /*
52  * The actual block is 0x800-0x8ff, but some BIOSes think it's 0x880-0x8ff
53  * and they tell the kernel that so we have to think of it as two parts.
54  */
55 #define EC_HOST_CMD_REGION0    0x800
56 #define EC_HOST_CMD_REGION1    0x880
57 #define EC_HOST_CMD_REGION_SIZE 0x80
58
59 /* EC command register bit functions */
60 #define EC_LPC_CMDR_DATA        BIT(0)  /* Data ready for host to read */
61 #define EC_LPC_CMDR_PENDING     BIT(1)  /* Write pending to EC */
62 #define EC_LPC_CMDR_BUSY        BIT(2)  /* EC is busy processing a command */
63 #define EC_LPC_CMDR_CMD         BIT(3)  /* Last host write was a command */
64 #define EC_LPC_CMDR_ACPI_BRST   BIT(4)  /* Burst mode (not used) */
65 #define EC_LPC_CMDR_SCI         BIT(5)  /* SCI event is pending */
66 #define EC_LPC_CMDR_SMI         BIT(6)  /* SMI event is pending */
67
68 #define EC_LPC_ADDR_MEMMAP       0x900
69 #define EC_MEMMAP_SIZE         255 /* ACPI IO buffer max is 255 bytes */
70 #define EC_MEMMAP_TEXT_MAX     8   /* Size of a string in the memory map */
71
72 /* The offset address of each type of data in mapped memory. */
73 #define EC_MEMMAP_TEMP_SENSOR      0x00 /* Temp sensors 0x00 - 0x0f */
74 #define EC_MEMMAP_FAN              0x10 /* Fan speeds 0x10 - 0x17 */
75 #define EC_MEMMAP_TEMP_SENSOR_B    0x18 /* More temp sensors 0x18 - 0x1f */
76 #define EC_MEMMAP_ID               0x20 /* 0x20 == 'E', 0x21 == 'C' */
77 #define EC_MEMMAP_ID_VERSION       0x22 /* Version of data in 0x20 - 0x2f */
78 #define EC_MEMMAP_THERMAL_VERSION  0x23 /* Version of data in 0x00 - 0x1f */
79 #define EC_MEMMAP_BATTERY_VERSION  0x24 /* Version of data in 0x40 - 0x7f */
80 #define EC_MEMMAP_SWITCHES_VERSION 0x25 /* Version of data in 0x30 - 0x33 */
81 #define EC_MEMMAP_EVENTS_VERSION   0x26 /* Version of data in 0x34 - 0x3f */
82 #define EC_MEMMAP_HOST_CMD_FLAGS   0x27 /* Host cmd interface flags (8 bits) */
83 /* Unused 0x28 - 0x2f */
84 #define EC_MEMMAP_SWITCHES         0x30 /* 8 bits */
85 /* Unused 0x31 - 0x33 */
86 #define EC_MEMMAP_HOST_EVENTS      0x34 /* 64 bits */
87 /* Battery values are all 32 bits, unless otherwise noted. */
88 #define EC_MEMMAP_BATT_VOLT        0x40 /* Battery Present Voltage */
89 #define EC_MEMMAP_BATT_RATE        0x44 /* Battery Present Rate */
90 #define EC_MEMMAP_BATT_CAP         0x48 /* Battery Remaining Capacity */
91 #define EC_MEMMAP_BATT_FLAG        0x4c /* Battery State, see below (8-bit) */
92 #define EC_MEMMAP_BATT_COUNT       0x4d /* Battery Count (8-bit) */
93 #define EC_MEMMAP_BATT_INDEX       0x4e /* Current Battery Data Index (8-bit) */
94 /* Unused 0x4f */
95 #define EC_MEMMAP_BATT_DCAP        0x50 /* Battery Design Capacity */
96 #define EC_MEMMAP_BATT_DVLT        0x54 /* Battery Design Voltage */
97 #define EC_MEMMAP_BATT_LFCC        0x58 /* Battery Last Full Charge Capacity */
98 #define EC_MEMMAP_BATT_CCNT        0x5c /* Battery Cycle Count */
99 /* Strings are all 8 bytes (EC_MEMMAP_TEXT_MAX) */
100 #define EC_MEMMAP_BATT_MFGR        0x60 /* Battery Manufacturer String */
101 #define EC_MEMMAP_BATT_MODEL       0x68 /* Battery Model Number String */
102 #define EC_MEMMAP_BATT_SERIAL      0x70 /* Battery Serial Number String */
103 #define EC_MEMMAP_BATT_TYPE        0x78 /* Battery Type String */
104 #define EC_MEMMAP_ALS              0x80 /* ALS readings in lux (2 X 16 bits) */
105 /* Unused 0x84 - 0x8f */
106 #define EC_MEMMAP_ACC_STATUS       0x90 /* Accelerometer status (8 bits )*/
107 /* Unused 0x91 */
108 #define EC_MEMMAP_ACC_DATA         0x92 /* Accelerometers data 0x92 - 0x9f */
109 /* 0x92: Lid Angle if available, LID_ANGLE_UNRELIABLE otherwise */
110 /* 0x94 - 0x99: 1st Accelerometer */
111 /* 0x9a - 0x9f: 2nd Accelerometer */
112 #define EC_MEMMAP_GYRO_DATA        0xa0 /* Gyroscope data 0xa0 - 0xa5 */
113 /* Unused 0xa6 - 0xdf */
114
115 /*
116  * ACPI is unable to access memory mapped data at or above this offset due to
117  * limitations of the ACPI protocol. Do not place data in the range 0xe0 - 0xfe
118  * which might be needed by ACPI.
119  */
120 #define EC_MEMMAP_NO_ACPI 0xe0
121
122 /* Define the format of the accelerometer mapped memory status byte. */
123 #define EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK  0x0f
124 #define EC_MEMMAP_ACC_STATUS_BUSY_BIT        BIT(4)
125 #define EC_MEMMAP_ACC_STATUS_PRESENCE_BIT    BIT(7)
126
127 /* Number of temp sensors at EC_MEMMAP_TEMP_SENSOR */
128 #define EC_TEMP_SENSOR_ENTRIES     16
129 /*
130  * Number of temp sensors at EC_MEMMAP_TEMP_SENSOR_B.
131  *
132  * Valid only if EC_MEMMAP_THERMAL_VERSION returns >= 2.
133  */
134 #define EC_TEMP_SENSOR_B_ENTRIES      8
135
136 /* Special values for mapped temperature sensors */
137 #define EC_TEMP_SENSOR_NOT_PRESENT    0xff
138 #define EC_TEMP_SENSOR_ERROR          0xfe
139 #define EC_TEMP_SENSOR_NOT_POWERED    0xfd
140 #define EC_TEMP_SENSOR_NOT_CALIBRATED 0xfc
141 /*
142  * The offset of temperature value stored in mapped memory.  This allows
143  * reporting a temperature range of 200K to 454K = -73C to 181C.
144  */
145 #define EC_TEMP_SENSOR_OFFSET      200
146
147 /*
148  * Number of ALS readings at EC_MEMMAP_ALS
149  */
150 #define EC_ALS_ENTRIES             2
151
152 /*
153  * The default value a temperature sensor will return when it is present but
154  * has not been read this boot.  This is a reasonable number to avoid
155  * triggering alarms on the host.
156  */
157 #define EC_TEMP_SENSOR_DEFAULT     (296 - EC_TEMP_SENSOR_OFFSET)
158
159 #define EC_FAN_SPEED_ENTRIES       4       /* Number of fans at EC_MEMMAP_FAN */
160 #define EC_FAN_SPEED_NOT_PRESENT   0xffff  /* Entry not present */
161 #define EC_FAN_SPEED_STALLED       0xfffe  /* Fan stalled */
162
163 /* Battery bit flags at EC_MEMMAP_BATT_FLAG. */
164 #define EC_BATT_FLAG_AC_PRESENT   0x01
165 #define EC_BATT_FLAG_BATT_PRESENT 0x02
166 #define EC_BATT_FLAG_DISCHARGING  0x04
167 #define EC_BATT_FLAG_CHARGING     0x08
168 #define EC_BATT_FLAG_LEVEL_CRITICAL 0x10
169 /* Set if some of the static/dynamic data is invalid (or outdated). */
170 #define EC_BATT_FLAG_INVALID_DATA 0x20
171
172 /* Switch flags at EC_MEMMAP_SWITCHES */
173 #define EC_SWITCH_LID_OPEN               0x01
174 #define EC_SWITCH_POWER_BUTTON_PRESSED   0x02
175 #define EC_SWITCH_WRITE_PROTECT_DISABLED 0x04
176 /* Was recovery requested via keyboard; now unused. */
177 #define EC_SWITCH_IGNORE1                0x08
178 /* Recovery requested via dedicated signal (from servo board) */
179 #define EC_SWITCH_DEDICATED_RECOVERY     0x10
180 /* Was fake developer mode switch; now unused.  Remove in next refactor. */
181 #define EC_SWITCH_IGNORE0                0x20
182
183 /* Host command interface flags */
184 /* Host command interface supports LPC args (LPC interface only) */
185 #define EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED  0x01
186 /* Host command interface supports version 3 protocol */
187 #define EC_HOST_CMD_FLAG_VERSION_3   0x02
188
189 /* Wireless switch flags */
190 #define EC_WIRELESS_SWITCH_ALL       ~0x00  /* All flags */
191 #define EC_WIRELESS_SWITCH_WLAN       0x01  /* WLAN radio */
192 #define EC_WIRELESS_SWITCH_BLUETOOTH  0x02  /* Bluetooth radio */
193 #define EC_WIRELESS_SWITCH_WWAN       0x04  /* WWAN power */
194 #define EC_WIRELESS_SWITCH_WLAN_POWER 0x08  /* WLAN power */
195
196 /*****************************************************************************/
197 /*
198  * ACPI commands
199  *
200  * These are valid ONLY on the ACPI command/data port.
201  */
202
203 /*
204  * ACPI Read Embedded Controller
205  *
206  * This reads from ACPI memory space on the EC (EC_ACPI_MEM_*).
207  *
208  * Use the following sequence:
209  *
210  *    - Write EC_CMD_ACPI_READ to EC_LPC_ADDR_ACPI_CMD
211  *    - Wait for EC_LPC_CMDR_PENDING bit to clear
212  *    - Write address to EC_LPC_ADDR_ACPI_DATA
213  *    - Wait for EC_LPC_CMDR_DATA bit to set
214  *    - Read value from EC_LPC_ADDR_ACPI_DATA
215  */
216 #define EC_CMD_ACPI_READ 0x0080
217
218 /*
219  * ACPI Write Embedded Controller
220  *
221  * This reads from ACPI memory space on the EC (EC_ACPI_MEM_*).
222  *
223  * Use the following sequence:
224  *
225  *    - Write EC_CMD_ACPI_WRITE to EC_LPC_ADDR_ACPI_CMD
226  *    - Wait for EC_LPC_CMDR_PENDING bit to clear
227  *    - Write address to EC_LPC_ADDR_ACPI_DATA
228  *    - Wait for EC_LPC_CMDR_PENDING bit to clear
229  *    - Write value to EC_LPC_ADDR_ACPI_DATA
230  */
231 #define EC_CMD_ACPI_WRITE 0x0081
232
233 /*
234  * ACPI Burst Enable Embedded Controller
235  *
236  * This enables burst mode on the EC to allow the host to issue several
237  * commands back-to-back. While in this mode, writes to mapped multi-byte
238  * data are locked out to ensure data consistency.
239  */
240 #define EC_CMD_ACPI_BURST_ENABLE 0x0082
241
242 /*
243  * ACPI Burst Disable Embedded Controller
244  *
245  * This disables burst mode on the EC and stops preventing EC writes to mapped
246  * multi-byte data.
247  */
248 #define EC_CMD_ACPI_BURST_DISABLE 0x0083
249
250 /*
251  * ACPI Query Embedded Controller
252  *
253  * This clears the lowest-order bit in the currently pending host events, and
254  * sets the result code to the 1-based index of the bit (event 0x00000001 = 1,
255  * event 0x80000000 = 32), or 0 if no event was pending.
256  */
257 #define EC_CMD_ACPI_QUERY_EVENT 0x0084
258
259 /* Valid addresses in ACPI memory space, for read/write commands */
260
261 /* Memory space version; set to EC_ACPI_MEM_VERSION_CURRENT */
262 #define EC_ACPI_MEM_VERSION            0x00
263 /*
264  * Test location; writing value here updates test compliment byte to (0xff -
265  * value).
266  */
267 #define EC_ACPI_MEM_TEST               0x01
268 /* Test compliment; writes here are ignored. */
269 #define EC_ACPI_MEM_TEST_COMPLIMENT    0x02
270
271 /* Keyboard backlight brightness percent (0 - 100) */
272 #define EC_ACPI_MEM_KEYBOARD_BACKLIGHT 0x03
273 /* DPTF Target Fan Duty (0-100, 0xff for auto/none) */
274 #define EC_ACPI_MEM_FAN_DUTY           0x04
275
276 /*
277  * DPTF temp thresholds. Any of the EC's temp sensors can have up to two
278  * independent thresholds attached to them. The current value of the ID
279  * register determines which sensor is affected by the THRESHOLD and COMMIT
280  * registers. The THRESHOLD register uses the same EC_TEMP_SENSOR_OFFSET scheme
281  * as the memory-mapped sensors. The COMMIT register applies those settings.
282  *
283  * The spec does not mandate any way to read back the threshold settings
284  * themselves, but when a threshold is crossed the AP needs a way to determine
285  * which sensor(s) are responsible. Each reading of the ID register clears and
286  * returns one sensor ID that has crossed one of its threshold (in either
287  * direction) since the last read. A value of 0xFF means "no new thresholds
288  * have tripped". Setting or enabling the thresholds for a sensor will clear
289  * the unread event count for that sensor.
290  */
291 #define EC_ACPI_MEM_TEMP_ID            0x05
292 #define EC_ACPI_MEM_TEMP_THRESHOLD     0x06
293 #define EC_ACPI_MEM_TEMP_COMMIT        0x07
294 /*
295  * Here are the bits for the COMMIT register:
296  *   bit 0 selects the threshold index for the chosen sensor (0/1)
297  *   bit 1 enables/disables the selected threshold (0 = off, 1 = on)
298  * Each write to the commit register affects one threshold.
299  */
300 #define EC_ACPI_MEM_TEMP_COMMIT_SELECT_MASK BIT(0)
301 #define EC_ACPI_MEM_TEMP_COMMIT_ENABLE_MASK BIT(1)
302 /*
303  * Example:
304  *
305  * Set the thresholds for sensor 2 to 50 C and 60 C:
306  *   write 2 to [0x05]      --  select temp sensor 2
307  *   write 0x7b to [0x06]   --  C_TO_K(50) - EC_TEMP_SENSOR_OFFSET
308  *   write 0x2 to [0x07]    --  enable threshold 0 with this value
309  *   write 0x85 to [0x06]   --  C_TO_K(60) - EC_TEMP_SENSOR_OFFSET
310  *   write 0x3 to [0x07]    --  enable threshold 1 with this value
311  *
312  * Disable the 60 C threshold, leaving the 50 C threshold unchanged:
313  *   write 2 to [0x05]      --  select temp sensor 2
314  *   write 0x1 to [0x07]    --  disable threshold 1
315  */
316
317 /* DPTF battery charging current limit */
318 #define EC_ACPI_MEM_CHARGING_LIMIT     0x08
319
320 /* Charging limit is specified in 64 mA steps */
321 #define EC_ACPI_MEM_CHARGING_LIMIT_STEP_MA   64
322 /* Value to disable DPTF battery charging limit */
323 #define EC_ACPI_MEM_CHARGING_LIMIT_DISABLED  0xff
324
325 /*
326  * Report device orientation
327  *  Bits       Definition
328  *  3:1        Device DPTF Profile Number (DDPN)
329  *               0   = Reserved for backward compatibility (indicates no valid
330  *                     profile number. Host should fall back to using TBMD).
331  *              1..7 = DPTF Profile number to indicate to host which table needs
332  *                     to be loaded.
333  *   0         Tablet Mode Device Indicator (TBMD)
334  */
335 #define EC_ACPI_MEM_DEVICE_ORIENTATION 0x09
336 #define EC_ACPI_MEM_TBMD_SHIFT         0
337 #define EC_ACPI_MEM_TBMD_MASK          0x1
338 #define EC_ACPI_MEM_DDPN_SHIFT         1
339 #define EC_ACPI_MEM_DDPN_MASK          0x7
340
341 /*
342  * Report device features. Uses the same format as the host command, except:
343  *
344  * bit 0 (EC_FEATURE_LIMITED) changes meaning from "EC code has a limited set
345  * of features", which is of limited interest when the system is already
346  * interpreting ACPI bytecode, to "EC_FEATURES[0-7] is not supported". Since
347  * these are supported, it defaults to 0.
348  * This allows detecting the presence of this field since older versions of
349  * the EC codebase would simply return 0xff to that unknown address. Check
350  * FEATURES0 != 0xff (or FEATURES0[0] == 0) to make sure that the other bits
351  * are valid.
352  */
353 #define EC_ACPI_MEM_DEVICE_FEATURES0 0x0a
354 #define EC_ACPI_MEM_DEVICE_FEATURES1 0x0b
355 #define EC_ACPI_MEM_DEVICE_FEATURES2 0x0c
356 #define EC_ACPI_MEM_DEVICE_FEATURES3 0x0d
357 #define EC_ACPI_MEM_DEVICE_FEATURES4 0x0e
358 #define EC_ACPI_MEM_DEVICE_FEATURES5 0x0f
359 #define EC_ACPI_MEM_DEVICE_FEATURES6 0x10
360 #define EC_ACPI_MEM_DEVICE_FEATURES7 0x11
361
362 #define EC_ACPI_MEM_BATTERY_INDEX    0x12
363
364 /*
365  * USB Port Power. Each bit indicates whether the corresponding USB ports' power
366  * is enabled (1) or disabled (0).
367  *   bit 0 USB port ID 0
368  *   ...
369  *   bit 7 USB port ID 7
370  */
371 #define EC_ACPI_MEM_USB_PORT_POWER 0x13
372
373 /*
374  * ACPI addresses 0x20 - 0xff map to EC_MEMMAP offset 0x00 - 0xdf.  This data
375  * is read-only from the AP.  Added in EC_ACPI_MEM_VERSION 2.
376  */
377 #define EC_ACPI_MEM_MAPPED_BEGIN   0x20
378 #define EC_ACPI_MEM_MAPPED_SIZE    0xe0
379
380 /* Current version of ACPI memory address space */
381 #define EC_ACPI_MEM_VERSION_CURRENT 2
382
383
384 /*
385  * This header file is used in coreboot both in C and ACPI code.  The ACPI code
386  * is pre-processed to handle constants but the ASL compiler is unable to
387  * handle actual C code so keep it separate.
388  */
389
390
391 /*
392  * Attributes for EC request and response packets.  Just defining __packed
393  * results in inefficient assembly code on ARM, if the structure is actually
394  * 32-bit aligned, as it should be for all buffers.
395  *
396  * Be very careful when adding these to existing structures.  They will round
397  * up the structure size to the specified boundary.
398  *
399  * Also be very careful to make that if a structure is included in some other
400  * parent structure that the alignment will still be true given the packing of
401  * the parent structure.  This is particularly important if the sub-structure
402  * will be passed as a pointer to another function, since that function will
403  * not know about the misaligment caused by the parent structure's packing.
404  *
405  * Also be very careful using __packed - particularly when nesting non-packed
406  * structures inside packed ones.  In fact, DO NOT use __packed directly;
407  * always use one of these attributes.
408  *
409  * Once everything is annotated properly, the following search strings should
410  * not return ANY matches in this file other than right here:
411  *
412  * "__packed" - generates inefficient code; all sub-structs must also be packed
413  *
414  * "struct [^_]" - all structs should be annotated, except for structs that are
415  * members of other structs/unions (and their original declarations should be
416  * annotated).
417  */
418
419 /*
420  * Packed structures make no assumption about alignment, so they do inefficient
421  * byte-wise reads.
422  */
423 #define __ec_align1 __packed
424 #define __ec_align2 __packed
425 #define __ec_align4 __packed
426 #define __ec_align_size1 __packed
427 #define __ec_align_offset1 __packed
428 #define __ec_align_offset2 __packed
429 #define __ec_todo_packed __packed
430 #define __ec_todo_unpacked
431
432
433 /* LPC command status byte masks */
434 /* EC has written a byte in the data register and host hasn't read it yet */
435 #define EC_LPC_STATUS_TO_HOST     0x01
436 /* Host has written a command/data byte and the EC hasn't read it yet */
437 #define EC_LPC_STATUS_FROM_HOST   0x02
438 /* EC is processing a command */
439 #define EC_LPC_STATUS_PROCESSING  0x04
440 /* Last write to EC was a command, not data */
441 #define EC_LPC_STATUS_LAST_CMD    0x08
442 /* EC is in burst mode */
443 #define EC_LPC_STATUS_BURST_MODE  0x10
444 /* SCI event is pending (requesting SCI query) */
445 #define EC_LPC_STATUS_SCI_PENDING 0x20
446 /* SMI event is pending (requesting SMI query) */
447 #define EC_LPC_STATUS_SMI_PENDING 0x40
448 /* (reserved) */
449 #define EC_LPC_STATUS_RESERVED    0x80
450
451 /*
452  * EC is busy.  This covers both the EC processing a command, and the host has
453  * written a new command but the EC hasn't picked it up yet.
454  */
455 #define EC_LPC_STATUS_BUSY_MASK \
456         (EC_LPC_STATUS_FROM_HOST | EC_LPC_STATUS_PROCESSING)
457
458 /*
459  * Host command response codes (16-bit).  Note that response codes should be
460  * stored in a uint16_t rather than directly in a value of this type.
461  */
462 enum ec_status {
463         EC_RES_SUCCESS = 0,
464         EC_RES_INVALID_COMMAND = 1,
465         EC_RES_ERROR = 2,
466         EC_RES_INVALID_PARAM = 3,
467         EC_RES_ACCESS_DENIED = 4,
468         EC_RES_INVALID_RESPONSE = 5,
469         EC_RES_INVALID_VERSION = 6,
470         EC_RES_INVALID_CHECKSUM = 7,
471         EC_RES_IN_PROGRESS = 8,         /* Accepted, command in progress */
472         EC_RES_UNAVAILABLE = 9,         /* No response available */
473         EC_RES_TIMEOUT = 10,            /* We got a timeout */
474         EC_RES_OVERFLOW = 11,           /* Table / data overflow */
475         EC_RES_INVALID_HEADER = 12,     /* Header contains invalid data */
476         EC_RES_REQUEST_TRUNCATED = 13,  /* Didn't get the entire request */
477         EC_RES_RESPONSE_TOO_BIG = 14,   /* Response was too big to handle */
478         EC_RES_BUS_ERROR = 15,          /* Communications bus error */
479         EC_RES_BUSY = 16,               /* Up but too busy.  Should retry */
480         EC_RES_INVALID_HEADER_VERSION = 17,  /* Header version invalid */
481         EC_RES_INVALID_HEADER_CRC = 18,      /* Header CRC invalid */
482         EC_RES_INVALID_DATA_CRC = 19,        /* Data CRC invalid */
483         EC_RES_DUP_UNAVAILABLE = 20,         /* Can't resend response */
484 };
485
486 /*
487  * Host event codes.  Note these are 1-based, not 0-based, because ACPI query
488  * EC command uses code 0 to mean "no event pending".  We explicitly specify
489  * each value in the enum listing so they won't change if we delete/insert an
490  * item or rearrange the list (it needs to be stable across platforms, not
491  * just within a single compiled instance).
492  */
493 enum host_event_code {
494         EC_HOST_EVENT_LID_CLOSED = 1,
495         EC_HOST_EVENT_LID_OPEN = 2,
496         EC_HOST_EVENT_POWER_BUTTON = 3,
497         EC_HOST_EVENT_AC_CONNECTED = 4,
498         EC_HOST_EVENT_AC_DISCONNECTED = 5,
499         EC_HOST_EVENT_BATTERY_LOW = 6,
500         EC_HOST_EVENT_BATTERY_CRITICAL = 7,
501         EC_HOST_EVENT_BATTERY = 8,
502         EC_HOST_EVENT_THERMAL_THRESHOLD = 9,
503         /* Event generated by a device attached to the EC */
504         EC_HOST_EVENT_DEVICE = 10,
505         EC_HOST_EVENT_THERMAL = 11,
506         EC_HOST_EVENT_USB_CHARGER = 12,
507         EC_HOST_EVENT_KEY_PRESSED = 13,
508         /*
509          * EC has finished initializing the host interface.  The host can check
510          * for this event following sending a EC_CMD_REBOOT_EC command to
511          * determine when the EC is ready to accept subsequent commands.
512          */
513         EC_HOST_EVENT_INTERFACE_READY = 14,
514         /* Keyboard recovery combo has been pressed */
515         EC_HOST_EVENT_KEYBOARD_RECOVERY = 15,
516
517         /* Shutdown due to thermal overload */
518         EC_HOST_EVENT_THERMAL_SHUTDOWN = 16,
519         /* Shutdown due to battery level too low */
520         EC_HOST_EVENT_BATTERY_SHUTDOWN = 17,
521
522         /* Suggest that the AP throttle itself */
523         EC_HOST_EVENT_THROTTLE_START = 18,
524         /* Suggest that the AP resume normal speed */
525         EC_HOST_EVENT_THROTTLE_STOP = 19,
526
527         /* Hang detect logic detected a hang and host event timeout expired */
528         EC_HOST_EVENT_HANG_DETECT = 20,
529         /* Hang detect logic detected a hang and warm rebooted the AP */
530         EC_HOST_EVENT_HANG_REBOOT = 21,
531
532         /* PD MCU triggering host event */
533         EC_HOST_EVENT_PD_MCU = 22,
534
535         /* Battery Status flags have changed */
536         EC_HOST_EVENT_BATTERY_STATUS = 23,
537
538         /* EC encountered a panic, triggering a reset */
539         EC_HOST_EVENT_PANIC = 24,
540
541         /* Keyboard fastboot combo has been pressed */
542         EC_HOST_EVENT_KEYBOARD_FASTBOOT = 25,
543
544         /* EC RTC event occurred */
545         EC_HOST_EVENT_RTC = 26,
546
547         /* Emulate MKBP event */
548         EC_HOST_EVENT_MKBP = 27,
549
550         /* EC desires to change state of host-controlled USB mux */
551         EC_HOST_EVENT_USB_MUX = 28,
552
553         /* TABLET/LAPTOP mode or detachable base attach/detach event */
554         EC_HOST_EVENT_MODE_CHANGE = 29,
555
556         /* Keyboard recovery combo with hardware reinitialization */
557         EC_HOST_EVENT_KEYBOARD_RECOVERY_HW_REINIT = 30,
558
559         /*
560          * The high bit of the event mask is not used as a host event code.  If
561          * it reads back as set, then the entire event mask should be
562          * considered invalid by the host.  This can happen when reading the
563          * raw event status via EC_MEMMAP_HOST_EVENTS but the LPC interface is
564          * not initialized on the EC, or improperly configured on the host.
565          */
566         EC_HOST_EVENT_INVALID = 32
567 };
568 /* Host event mask */
569 #define EC_HOST_EVENT_MASK(event_code) BIT_ULL((event_code) - 1)
570
571 /**
572  * struct ec_lpc_host_args - Arguments at EC_LPC_ADDR_HOST_ARGS
573  * @flags: The host argument flags.
574  * @command_version: Command version.
575  * @data_size: The length of data.
576  * @checksum: Checksum; sum of command + flags + command_version + data_size +
577  *            all params/response data bytes.
578  */
579 struct ec_lpc_host_args {
580         uint8_t flags;
581         uint8_t command_version;
582         uint8_t data_size;
583         uint8_t checksum;
584 } __ec_align4;
585
586 /* Flags for ec_lpc_host_args.flags */
587 /*
588  * Args are from host.  Data area at EC_LPC_ADDR_HOST_PARAM contains command
589  * params.
590  *
591  * If EC gets a command and this flag is not set, this is an old-style command.
592  * Command version is 0 and params from host are at EC_LPC_ADDR_OLD_PARAM with
593  * unknown length.  EC must respond with an old-style response (that is,
594  * without setting EC_HOST_ARGS_FLAG_TO_HOST).
595  */
596 #define EC_HOST_ARGS_FLAG_FROM_HOST 0x01
597 /*
598  * Args are from EC.  Data area at EC_LPC_ADDR_HOST_PARAM contains response.
599  *
600  * If EC responds to a command and this flag is not set, this is an old-style
601  * response.  Command version is 0 and response data from EC is at
602  * EC_LPC_ADDR_OLD_PARAM with unknown length.
603  */
604 #define EC_HOST_ARGS_FLAG_TO_HOST   0x02
605
606 /*****************************************************************************/
607 /*
608  * Byte codes returned by EC over SPI interface.
609  *
610  * These can be used by the AP to debug the EC interface, and to determine
611  * when the EC is not in a state where it will ever get around to responding
612  * to the AP.
613  *
614  * Example of sequence of bytes read from EC for a current good transfer:
615  *   1. -                  - AP asserts chip select (CS#)
616  *   2. EC_SPI_OLD_READY   - AP sends first byte(s) of request
617  *   3. -                  - EC starts handling CS# interrupt
618  *   4. EC_SPI_RECEIVING   - AP sends remaining byte(s) of request
619  *   5. EC_SPI_PROCESSING  - EC starts processing request; AP is clocking in
620  *                           bytes looking for EC_SPI_FRAME_START
621  *   6. -                  - EC finishes processing and sets up response
622  *   7. EC_SPI_FRAME_START - AP reads frame byte
623  *   8. (response packet)  - AP reads response packet
624  *   9. EC_SPI_PAST_END    - Any additional bytes read by AP
625  *   10 -                  - AP deasserts chip select
626  *   11 -                  - EC processes CS# interrupt and sets up DMA for
627  *                           next request
628  *
629  * If the AP is waiting for EC_SPI_FRAME_START and sees any value other than
630  * the following byte values:
631  *   EC_SPI_OLD_READY
632  *   EC_SPI_RX_READY
633  *   EC_SPI_RECEIVING
634  *   EC_SPI_PROCESSING
635  *
636  * Then the EC found an error in the request, or was not ready for the request
637  * and lost data.  The AP should give up waiting for EC_SPI_FRAME_START,
638  * because the EC is unable to tell when the AP is done sending its request.
639  */
640
641 /*
642  * Framing byte which precedes a response packet from the EC.  After sending a
643  * request, the AP will clock in bytes until it sees the framing byte, then
644  * clock in the response packet.
645  */
646 #define EC_SPI_FRAME_START    0xec
647
648 /*
649  * Padding bytes which are clocked out after the end of a response packet.
650  */
651 #define EC_SPI_PAST_END       0xed
652
653 /*
654  * EC is ready to receive, and has ignored the byte sent by the AP.  EC expects
655  * that the AP will send a valid packet header (starting with
656  * EC_COMMAND_PROTOCOL_3) in the next 32 bytes.
657  */
658 #define EC_SPI_RX_READY       0xf8
659
660 /*
661  * EC has started receiving the request from the AP, but hasn't started
662  * processing it yet.
663  */
664 #define EC_SPI_RECEIVING      0xf9
665
666 /* EC has received the entire request from the AP and is processing it. */
667 #define EC_SPI_PROCESSING     0xfa
668
669 /*
670  * EC received bad data from the AP, such as a packet header with an invalid
671  * length.  EC will ignore all data until chip select deasserts.
672  */
673 #define EC_SPI_RX_BAD_DATA    0xfb
674
675 /*
676  * EC received data from the AP before it was ready.  That is, the AP asserted
677  * chip select and started clocking data before the EC was ready to receive it.
678  * EC will ignore all data until chip select deasserts.
679  */
680 #define EC_SPI_NOT_READY      0xfc
681
682 /*
683  * EC was ready to receive a request from the AP.  EC has treated the byte sent
684  * by the AP as part of a request packet, or (for old-style ECs) is processing
685  * a fully received packet but is not ready to respond yet.
686  */
687 #define EC_SPI_OLD_READY      0xfd
688
689 /*****************************************************************************/
690
691 /*
692  * Protocol version 2 for I2C and SPI send a request this way:
693  *
694  *      0       EC_CMD_VERSION0 + (command version)
695  *      1       Command number
696  *      2       Length of params = N
697  *      3..N+2  Params, if any
698  *      N+3     8-bit checksum of bytes 0..N+2
699  *
700  * The corresponding response is:
701  *
702  *      0       Result code (EC_RES_*)
703  *      1       Length of params = M
704  *      2..M+1  Params, if any
705  *      M+2     8-bit checksum of bytes 0..M+1
706  */
707 #define EC_PROTO2_REQUEST_HEADER_BYTES 3
708 #define EC_PROTO2_REQUEST_TRAILER_BYTES 1
709 #define EC_PROTO2_REQUEST_OVERHEAD (EC_PROTO2_REQUEST_HEADER_BYTES +    \
710                                     EC_PROTO2_REQUEST_TRAILER_BYTES)
711
712 #define EC_PROTO2_RESPONSE_HEADER_BYTES 2
713 #define EC_PROTO2_RESPONSE_TRAILER_BYTES 1
714 #define EC_PROTO2_RESPONSE_OVERHEAD (EC_PROTO2_RESPONSE_HEADER_BYTES +  \
715                                      EC_PROTO2_RESPONSE_TRAILER_BYTES)
716
717 /* Parameter length was limited by the LPC interface */
718 #define EC_PROTO2_MAX_PARAM_SIZE 0xfc
719
720 /* Maximum request and response packet sizes for protocol version 2 */
721 #define EC_PROTO2_MAX_REQUEST_SIZE (EC_PROTO2_REQUEST_OVERHEAD +        \
722                                     EC_PROTO2_MAX_PARAM_SIZE)
723 #define EC_PROTO2_MAX_RESPONSE_SIZE (EC_PROTO2_RESPONSE_OVERHEAD +      \
724                                      EC_PROTO2_MAX_PARAM_SIZE)
725
726 /*****************************************************************************/
727
728 /*
729  * Value written to legacy command port / prefix byte to indicate protocol
730  * 3+ structs are being used.  Usage is bus-dependent.
731  */
732 #define EC_COMMAND_PROTOCOL_3 0xda
733
734 #define EC_HOST_REQUEST_VERSION 3
735
736 /**
737  * struct ec_host_request - Version 3 request from host.
738  * @struct_version: Should be 3. The EC will return EC_RES_INVALID_HEADER if it
739  *                  receives a header with a version it doesn't know how to
740  *                  parse.
741  * @checksum: Checksum of request and data; sum of all bytes including checksum
742  *            should total to 0.
743  * @command: Command to send (EC_CMD_...)
744  * @command_version: Command version.
745  * @reserved: Unused byte in current protocol version; set to 0.
746  * @data_len: Length of data which follows this header.
747  */
748 struct ec_host_request {
749         uint8_t struct_version;
750         uint8_t checksum;
751         uint16_t command;
752         uint8_t command_version;
753         uint8_t reserved;
754         uint16_t data_len;
755 } __ec_align4;
756
757 #define EC_HOST_RESPONSE_VERSION 3
758
759 /**
760  * struct ec_host_response - Version 3 response from EC.
761  * @struct_version: Struct version (=3).
762  * @checksum: Checksum of response and data; sum of all bytes including
763  *            checksum should total to 0.
764  * @result: EC's response to the command (separate from communication failure)
765  * @data_len: Length of data which follows this header.
766  * @reserved: Unused bytes in current protocol version; set to 0.
767  */
768 struct ec_host_response {
769         uint8_t struct_version;
770         uint8_t checksum;
771         uint16_t result;
772         uint16_t data_len;
773         uint16_t reserved;
774 } __ec_align4;
775
776 /*****************************************************************************/
777
778 /*
779  * Host command protocol V4.
780  *
781  * Packets always start with a request or response header.  They are followed
782  * by data_len bytes of data.  If the data_crc_present flag is set, the data
783  * bytes are followed by a CRC-8 of that data, using using x^8 + x^2 + x + 1
784  * polynomial.
785  *
786  * Host algorithm when sending a request q:
787  *
788  * 101) tries_left=(some value, e.g. 3);
789  * 102) q.seq_num++
790  * 103) q.seq_dup=0
791  * 104) Calculate q.header_crc.
792  * 105) Send request q to EC.
793  * 106) Wait for response r.  Go to 201 if received or 301 if timeout.
794  *
795  * 201) If r.struct_version != 4, go to 301.
796  * 202) If r.header_crc mismatches calculated CRC for r header, go to 301.
797  * 203) If r.data_crc_present and r.data_crc mismatches, go to 301.
798  * 204) If r.seq_num != q.seq_num, go to 301.
799  * 205) If r.seq_dup == q.seq_dup, return success.
800  * 207) If r.seq_dup == 1, go to 301.
801  * 208) Return error.
802  *
803  * 301) If --tries_left <= 0, return error.
804  * 302) If q.seq_dup == 1, go to 105.
805  * 303) q.seq_dup = 1
806  * 304) Go to 104.
807  *
808  * EC algorithm when receiving a request q.
809  * EC has response buffer r, error buffer e.
810  *
811  * 101) If q.struct_version != 4, set e.result = EC_RES_INVALID_HEADER_VERSION
812  *      and go to 301
813  * 102) If q.header_crc mismatches calculated CRC, set e.result =
814  *      EC_RES_INVALID_HEADER_CRC and go to 301
815  * 103) If q.data_crc_present, calculate data CRC.  If that mismatches the CRC
816  *      byte at the end of the packet, set e.result = EC_RES_INVALID_DATA_CRC
817  *      and go to 301.
818  * 104) If q.seq_dup == 0, go to 201.
819  * 105) If q.seq_num != r.seq_num, go to 201.
820  * 106) If q.seq_dup == r.seq_dup, go to 205, else go to 203.
821  *
822  * 201) Process request q into response r.
823  * 202) r.seq_num = q.seq_num
824  * 203) r.seq_dup = q.seq_dup
825  * 204) Calculate r.header_crc
826  * 205) If r.data_len > 0 and data is no longer available, set e.result =
827  *      EC_RES_DUP_UNAVAILABLE and go to 301.
828  * 206) Send response r.
829  *
830  * 301) e.seq_num = q.seq_num
831  * 302) e.seq_dup = q.seq_dup
832  * 303) Calculate e.header_crc.
833  * 304) Send error response e.
834  */
835
836 /* Version 4 request from host */
837 struct ec_host_request4 {
838         /*
839          * bits 0-3: struct_version: Structure version (=4)
840          * bit    4: is_response: Is response (=0)
841          * bits 5-6: seq_num: Sequence number
842          * bit    7: seq_dup: Sequence duplicate flag
843          */
844         uint8_t fields0;
845
846         /*
847          * bits 0-4: command_version: Command version
848          * bits 5-6: Reserved (set 0, ignore on read)
849          * bit    7: data_crc_present: Is data CRC present after data
850          */
851         uint8_t fields1;
852
853         /* Command code (EC_CMD_*) */
854         uint16_t command;
855
856         /* Length of data which follows this header (not including data CRC) */
857         uint16_t data_len;
858
859         /* Reserved (set 0, ignore on read) */
860         uint8_t reserved;
861
862         /* CRC-8 of above fields, using x^8 + x^2 + x + 1 polynomial */
863         uint8_t header_crc;
864 } __ec_align4;
865
866 /* Version 4 response from EC */
867 struct ec_host_response4 {
868         /*
869          * bits 0-3: struct_version: Structure version (=4)
870          * bit    4: is_response: Is response (=1)
871          * bits 5-6: seq_num: Sequence number
872          * bit    7: seq_dup: Sequence duplicate flag
873          */
874         uint8_t fields0;
875
876         /*
877          * bits 0-6: Reserved (set 0, ignore on read)
878          * bit    7: data_crc_present: Is data CRC present after data
879          */
880         uint8_t fields1;
881
882         /* Result code (EC_RES_*) */
883         uint16_t result;
884
885         /* Length of data which follows this header (not including data CRC) */
886         uint16_t data_len;
887
888         /* Reserved (set 0, ignore on read) */
889         uint8_t reserved;
890
891         /* CRC-8 of above fields, using x^8 + x^2 + x + 1 polynomial */
892         uint8_t header_crc;
893 } __ec_align4;
894
895 /* Fields in fields0 byte */
896 #define EC_PACKET4_0_STRUCT_VERSION_MASK        0x0f
897 #define EC_PACKET4_0_IS_RESPONSE_MASK           0x10
898 #define EC_PACKET4_0_SEQ_NUM_SHIFT              5
899 #define EC_PACKET4_0_SEQ_NUM_MASK               0x60
900 #define EC_PACKET4_0_SEQ_DUP_MASK               0x80
901
902 /* Fields in fields1 byte */
903 #define EC_PACKET4_1_COMMAND_VERSION_MASK       0x1f  /* (request only) */
904 #define EC_PACKET4_1_DATA_CRC_PRESENT_MASK      0x80
905
906 /*****************************************************************************/
907 /*
908  * Notes on commands:
909  *
910  * Each command is an 16-bit command value.  Commands which take params or
911  * return response data specify structures for that data.  If no structure is
912  * specified, the command does not input or output data, respectively.
913  * Parameter/response length is implicit in the structs.  Some underlying
914  * communication protocols (I2C, SPI) may add length or checksum headers, but
915  * those are implementation-dependent and not defined here.
916  *
917  * All commands MUST be #defined to be 4-digit UPPER CASE hex values
918  * (e.g., 0x00AB, not 0xab) for CONFIG_HOSTCMD_SECTION_SORTED to work.
919  */
920
921 /*****************************************************************************/
922 /* General / test commands */
923
924 /*
925  * Get protocol version, used to deal with non-backward compatible protocol
926  * changes.
927  */
928 #define EC_CMD_PROTO_VERSION 0x0000
929
930 /**
931  * struct ec_response_proto_version - Response to the proto version command.
932  * @version: The protocol version.
933  */
934 struct ec_response_proto_version {
935         uint32_t version;
936 } __ec_align4;
937
938 /*
939  * Hello.  This is a simple command to test the EC is responsive to
940  * commands.
941  */
942 #define EC_CMD_HELLO 0x0001
943
944 /**
945  * struct ec_params_hello - Parameters to the hello command.
946  * @in_data: Pass anything here.
947  */
948 struct ec_params_hello {
949         uint32_t in_data;
950 } __ec_align4;
951
952 /**
953  * struct ec_response_hello - Response to the hello command.
954  * @out_data: Output will be in_data + 0x01020304.
955  */
956 struct ec_response_hello {
957         uint32_t out_data;
958 } __ec_align4;
959
960 /* Get version number */
961 #define EC_CMD_GET_VERSION 0x0002
962
963 enum ec_current_image {
964         EC_IMAGE_UNKNOWN = 0,
965         EC_IMAGE_RO,
966         EC_IMAGE_RW
967 };
968
969 /**
970  * struct ec_response_get_version - Response to the get version command.
971  * @version_string_ro: Null-terminated RO firmware version string.
972  * @version_string_rw: Null-terminated RW firmware version string.
973  * @reserved: Unused bytes; was previously RW-B firmware version string.
974  * @current_image: One of ec_current_image.
975  */
976 struct ec_response_get_version {
977         char version_string_ro[32];
978         char version_string_rw[32];
979         char reserved[32];
980         uint32_t current_image;
981 } __ec_align4;
982
983 /* Read test */
984 #define EC_CMD_READ_TEST 0x0003
985
986 /**
987  * struct ec_params_read_test - Parameters for the read test command.
988  * @offset: Starting value for read buffer.
989  * @size: Size to read in bytes.
990  */
991 struct ec_params_read_test {
992         uint32_t offset;
993         uint32_t size;
994 } __ec_align4;
995
996 /**
997  * struct ec_response_read_test - Response to the read test command.
998  * @data: Data returned by the read test command.
999  */
1000 struct ec_response_read_test {
1001         uint32_t data[32];
1002 } __ec_align4;
1003
1004 /*
1005  * Get build information
1006  *
1007  * Response is null-terminated string.
1008  */
1009 #define EC_CMD_GET_BUILD_INFO 0x0004
1010
1011 /* Get chip info */
1012 #define EC_CMD_GET_CHIP_INFO 0x0005
1013
1014 /**
1015  * struct ec_response_get_chip_info - Response to the get chip info command.
1016  * @vendor: Null-terminated string for chip vendor.
1017  * @name: Null-terminated string for chip name.
1018  * @revision: Null-terminated string for chip mask version.
1019  */
1020 struct ec_response_get_chip_info {
1021         char vendor[32];
1022         char name[32];
1023         char revision[32];
1024 } __ec_align4;
1025
1026 /* Get board HW version */
1027 #define EC_CMD_GET_BOARD_VERSION 0x0006
1028
1029 /**
1030  * struct ec_response_board_version - Response to the board version command.
1031  * @board_version: A monotonously incrementing number.
1032  */
1033 struct ec_response_board_version {
1034         uint16_t board_version;
1035 } __ec_align2;
1036
1037 /*
1038  * Read memory-mapped data.
1039  *
1040  * This is an alternate interface to memory-mapped data for bus protocols
1041  * which don't support direct-mapped memory - I2C, SPI, etc.
1042  *
1043  * Response is params.size bytes of data.
1044  */
1045 #define EC_CMD_READ_MEMMAP 0x0007
1046
1047 /**
1048  * struct ec_params_read_memmap - Parameters for the read memory map command.
1049  * @offset: Offset in memmap (EC_MEMMAP_*).
1050  * @size: Size to read in bytes.
1051  */
1052 struct ec_params_read_memmap {
1053         uint8_t offset;
1054         uint8_t size;
1055 } __ec_align1;
1056
1057 /* Read versions supported for a command */
1058 #define EC_CMD_GET_CMD_VERSIONS 0x0008
1059
1060 /**
1061  * struct ec_params_get_cmd_versions - Parameters for the get command versions.
1062  * @cmd: Command to check.
1063  */
1064 struct ec_params_get_cmd_versions {
1065         uint8_t cmd;
1066 } __ec_align1;
1067
1068 /**
1069  * struct ec_params_get_cmd_versions_v1 - Parameters for the get command
1070  *         versions (v1)
1071  * @cmd: Command to check.
1072  */
1073 struct ec_params_get_cmd_versions_v1 {
1074         uint16_t cmd;
1075 } __ec_align2;
1076
1077 /**
1078  * struct ec_response_get_cmd_version - Response to the get command versions.
1079  * @version_mask: Mask of supported versions; use EC_VER_MASK() to compare with
1080  *                a desired version.
1081  */
1082 struct ec_response_get_cmd_versions {
1083         uint32_t version_mask;
1084 } __ec_align4;
1085
1086 /*
1087  * Check EC communications status (busy). This is needed on i2c/spi but not
1088  * on lpc since it has its own out-of-band busy indicator.
1089  *
1090  * lpc must read the status from the command register. Attempting this on
1091  * lpc will overwrite the args/parameter space and corrupt its data.
1092  */
1093 #define EC_CMD_GET_COMMS_STATUS         0x0009
1094
1095 /* Avoid using ec_status which is for return values */
1096 enum ec_comms_status {
1097         EC_COMMS_STATUS_PROCESSING      = BIT(0),       /* Processing cmd */
1098 };
1099
1100 /**
1101  * struct ec_response_get_comms_status - Response to the get comms status
1102  *         command.
1103  * @flags: Mask of enum ec_comms_status.
1104  */
1105 struct ec_response_get_comms_status {
1106         uint32_t flags;         /* Mask of enum ec_comms_status */
1107 } __ec_align4;
1108
1109 /* Fake a variety of responses, purely for testing purposes. */
1110 #define EC_CMD_TEST_PROTOCOL            0x000A
1111
1112 /* Tell the EC what to send back to us. */
1113 struct ec_params_test_protocol {
1114         uint32_t ec_result;
1115         uint32_t ret_len;
1116         uint8_t buf[32];
1117 } __ec_align4;
1118
1119 /* Here it comes... */
1120 struct ec_response_test_protocol {
1121         uint8_t buf[32];
1122 } __ec_align4;
1123
1124 /* Get protocol information */
1125 #define EC_CMD_GET_PROTOCOL_INFO        0x000B
1126
1127 /* Flags for ec_response_get_protocol_info.flags */
1128 /* EC_RES_IN_PROGRESS may be returned if a command is slow */
1129 #define EC_PROTOCOL_INFO_IN_PROGRESS_SUPPORTED BIT(0)
1130
1131 /**
1132  * struct ec_response_get_protocol_info - Response to the get protocol info.
1133  * @protocol_versions: Bitmask of protocol versions supported (1 << n means
1134  *                     version n).
1135  * @max_request_packet_size: Maximum request packet size in bytes.
1136  * @max_response_packet_size: Maximum response packet size in bytes.
1137  * @flags: see EC_PROTOCOL_INFO_*
1138  */
1139 struct ec_response_get_protocol_info {
1140         /* Fields which exist if at least protocol version 3 supported */
1141         uint32_t protocol_versions;
1142         uint16_t max_request_packet_size;
1143         uint16_t max_response_packet_size;
1144         uint32_t flags;
1145 } __ec_align4;
1146
1147
1148 /*****************************************************************************/
1149 /* Get/Set miscellaneous values */
1150
1151 /* The upper byte of .flags tells what to do (nothing means "get") */
1152 #define EC_GSV_SET        0x80000000
1153
1154 /*
1155  * The lower three bytes of .flags identifies the parameter, if that has
1156  * meaning for an individual command.
1157  */
1158 #define EC_GSV_PARAM_MASK 0x00ffffff
1159
1160 struct ec_params_get_set_value {
1161         uint32_t flags;
1162         uint32_t value;
1163 } __ec_align4;
1164
1165 struct ec_response_get_set_value {
1166         uint32_t flags;
1167         uint32_t value;
1168 } __ec_align4;
1169
1170 /* More than one command can use these structs to get/set parameters. */
1171 #define EC_CMD_GSV_PAUSE_IN_S5  0x000C
1172
1173 /*****************************************************************************/
1174 /* List the features supported by the firmware */
1175 #define EC_CMD_GET_FEATURES  0x000D
1176
1177 /* Supported features */
1178 enum ec_feature_code {
1179         /*
1180          * This image contains a limited set of features. Another image
1181          * in RW partition may support more features.
1182          */
1183         EC_FEATURE_LIMITED = 0,
1184         /*
1185          * Commands for probing/reading/writing/erasing the flash in the
1186          * EC are present.
1187          */
1188         EC_FEATURE_FLASH = 1,
1189         /*
1190          * Can control the fan speed directly.
1191          */
1192         EC_FEATURE_PWM_FAN = 2,
1193         /*
1194          * Can control the intensity of the keyboard backlight.
1195          */
1196         EC_FEATURE_PWM_KEYB = 3,
1197         /*
1198          * Support Google lightbar, introduced on Pixel.
1199          */
1200         EC_FEATURE_LIGHTBAR = 4,
1201         /* Control of LEDs  */
1202         EC_FEATURE_LED = 5,
1203         /* Exposes an interface to control gyro and sensors.
1204          * The host goes through the EC to access these sensors.
1205          * In addition, the EC may provide composite sensors, like lid angle.
1206          */
1207         EC_FEATURE_MOTION_SENSE = 6,
1208         /* The keyboard is controlled by the EC */
1209         EC_FEATURE_KEYB = 7,
1210         /* The AP can use part of the EC flash as persistent storage. */
1211         EC_FEATURE_PSTORE = 8,
1212         /* The EC monitors BIOS port 80h, and can return POST codes. */
1213         EC_FEATURE_PORT80 = 9,
1214         /*
1215          * Thermal management: include TMP specific commands.
1216          * Higher level than direct fan control.
1217          */
1218         EC_FEATURE_THERMAL = 10,
1219         /* Can switch the screen backlight on/off */
1220         EC_FEATURE_BKLIGHT_SWITCH = 11,
1221         /* Can switch the wifi module on/off */
1222         EC_FEATURE_WIFI_SWITCH = 12,
1223         /* Monitor host events, through for example SMI or SCI */
1224         EC_FEATURE_HOST_EVENTS = 13,
1225         /* The EC exposes GPIO commands to control/monitor connected devices. */
1226         EC_FEATURE_GPIO = 14,
1227         /* The EC can send i2c messages to downstream devices. */
1228         EC_FEATURE_I2C = 15,
1229         /* Command to control charger are included */
1230         EC_FEATURE_CHARGER = 16,
1231         /* Simple battery support. */
1232         EC_FEATURE_BATTERY = 17,
1233         /*
1234          * Support Smart battery protocol
1235          * (Common Smart Battery System Interface Specification)
1236          */
1237         EC_FEATURE_SMART_BATTERY = 18,
1238         /* EC can detect when the host hangs. */
1239         EC_FEATURE_HANG_DETECT = 19,
1240         /* Report power information, for pit only */
1241         EC_FEATURE_PMU = 20,
1242         /* Another Cros EC device is present downstream of this one */
1243         EC_FEATURE_SUB_MCU = 21,
1244         /* Support USB Power delivery (PD) commands */
1245         EC_FEATURE_USB_PD = 22,
1246         /* Control USB multiplexer, for audio through USB port for instance. */
1247         EC_FEATURE_USB_MUX = 23,
1248         /* Motion Sensor code has an internal software FIFO */
1249         EC_FEATURE_MOTION_SENSE_FIFO = 24,
1250         /* Support temporary secure vstore */
1251         EC_FEATURE_VSTORE = 25,
1252         /* EC decides on USB-C SS mux state, muxes configured by host */
1253         EC_FEATURE_USBC_SS_MUX_VIRTUAL = 26,
1254         /* EC has RTC feature that can be controlled by host commands */
1255         EC_FEATURE_RTC = 27,
1256         /* The MCU exposes a Fingerprint sensor */
1257         EC_FEATURE_FINGERPRINT = 28,
1258         /* The MCU exposes a Touchpad */
1259         EC_FEATURE_TOUCHPAD = 29,
1260         /* The MCU has RWSIG task enabled */
1261         EC_FEATURE_RWSIG = 30,
1262         /* EC has device events support */
1263         EC_FEATURE_DEVICE_EVENT = 31,
1264         /* EC supports the unified wake masks for LPC/eSPI systems */
1265         EC_FEATURE_UNIFIED_WAKE_MASKS = 32,
1266         /* EC supports 64-bit host events */
1267         EC_FEATURE_HOST_EVENT64 = 33,
1268         /* EC runs code in RAM (not in place, a.k.a. XIP) */
1269         EC_FEATURE_EXEC_IN_RAM = 34,
1270         /* EC supports CEC commands */
1271         EC_FEATURE_CEC = 35,
1272         /* EC supports tight sensor timestamping. */
1273         EC_FEATURE_MOTION_SENSE_TIGHT_TIMESTAMPS = 36,
1274         /*
1275          * EC supports tablet mode detection aligned to Chrome and allows
1276          * setting of threshold by host command using
1277          * MOTIONSENSE_CMD_TABLET_MODE_LID_ANGLE.
1278          */
1279         EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS = 37,
1280         /* The MCU is a System Companion Processor (SCP). */
1281         EC_FEATURE_SCP = 39,
1282         /* The MCU is an Integrated Sensor Hub */
1283         EC_FEATURE_ISH = 40,
1284 };
1285
1286 #define EC_FEATURE_MASK_0(event_code) BIT(event_code % 32)
1287 #define EC_FEATURE_MASK_1(event_code) BIT(event_code - 32)
1288
1289 struct ec_response_get_features {
1290         uint32_t flags[2];
1291 } __ec_align4;
1292
1293 /*****************************************************************************/
1294 /* Get the board's SKU ID from EC */
1295 #define EC_CMD_GET_SKU_ID 0x000E
1296
1297 /* Set SKU ID from AP */
1298 #define EC_CMD_SET_SKU_ID 0x000F
1299
1300 struct ec_sku_id_info {
1301         uint32_t sku_id;
1302 } __ec_align4;
1303
1304 /*****************************************************************************/
1305 /* Flash commands */
1306
1307 /* Get flash info */
1308 #define EC_CMD_FLASH_INFO 0x0010
1309 #define EC_VER_FLASH_INFO 2
1310
1311 /**
1312  * struct ec_response_flash_info - Response to the flash info command.
1313  * @flash_size: Usable flash size in bytes.
1314  * @write_block_size: Write block size. Write offset and size must be a
1315  *                    multiple of this.
1316  * @erase_block_size: Erase block size. Erase offset and size must be a
1317  *                    multiple of this.
1318  * @protect_block_size: Protection block size. Protection offset and size
1319  *                      must be a multiple of this.
1320  *
1321  * Version 0 returns these fields.
1322  */
1323 struct ec_response_flash_info {
1324         uint32_t flash_size;
1325         uint32_t write_block_size;
1326         uint32_t erase_block_size;
1327         uint32_t protect_block_size;
1328 } __ec_align4;
1329
1330 /*
1331  * Flags for version 1+ flash info command
1332  * EC flash erases bits to 0 instead of 1.
1333  */
1334 #define EC_FLASH_INFO_ERASE_TO_0 BIT(0)
1335
1336 /*
1337  * Flash must be selected for read/write/erase operations to succeed.  This may
1338  * be necessary on a chip where write/erase can be corrupted by other board
1339  * activity, or where the chip needs to enable some sort of programming voltage,
1340  * or where the read/write/erase operations require cleanly suspending other
1341  * chip functionality.
1342  */
1343 #define EC_FLASH_INFO_SELECT_REQUIRED BIT(1)
1344
1345 /**
1346  * struct ec_response_flash_info_1 - Response to the flash info v1 command.
1347  * @flash_size: Usable flash size in bytes.
1348  * @write_block_size: Write block size. Write offset and size must be a
1349  *                    multiple of this.
1350  * @erase_block_size: Erase block size. Erase offset and size must be a
1351  *                    multiple of this.
1352  * @protect_block_size: Protection block size. Protection offset and size
1353  *                      must be a multiple of this.
1354  * @write_ideal_size: Ideal write size in bytes.  Writes will be fastest if
1355  *                    size is exactly this and offset is a multiple of this.
1356  *                    For example, an EC may have a write buffer which can do
1357  *                    half-page operations if data is aligned, and a slower
1358  *                    word-at-a-time write mode.
1359  * @flags: Flags; see EC_FLASH_INFO_*
1360  *
1361  * Version 1 returns the same initial fields as version 0, with additional
1362  * fields following.
1363  *
1364  * gcc anonymous structs don't seem to get along with the __packed directive;
1365  * if they did we'd define the version 0 structure as a sub-structure of this
1366  * one.
1367  *
1368  * Version 2 supports flash banks of different sizes:
1369  * The caller specified the number of banks it has preallocated
1370  * (num_banks_desc)
1371  * The EC returns the number of banks describing the flash memory.
1372  * It adds banks descriptions up to num_banks_desc.
1373  */
1374 struct ec_response_flash_info_1 {
1375         /* Version 0 fields; see above for description */
1376         uint32_t flash_size;
1377         uint32_t write_block_size;
1378         uint32_t erase_block_size;
1379         uint32_t protect_block_size;
1380
1381         /* Version 1 adds these fields: */
1382         uint32_t write_ideal_size;
1383         uint32_t flags;
1384 } __ec_align4;
1385
1386 struct ec_params_flash_info_2 {
1387         /* Number of banks to describe */
1388         uint16_t num_banks_desc;
1389         /* Reserved; set 0; ignore on read */
1390         uint8_t reserved[2];
1391 } __ec_align4;
1392
1393 struct ec_flash_bank {
1394         /* Number of sector is in this bank. */
1395         uint16_t count;
1396         /* Size in power of 2 of each sector (8 --> 256 bytes) */
1397         uint8_t size_exp;
1398         /* Minimal write size for the sectors in this bank */
1399         uint8_t write_size_exp;
1400         /* Erase size for the sectors in this bank */
1401         uint8_t erase_size_exp;
1402         /* Size for write protection, usually identical to erase size. */
1403         uint8_t protect_size_exp;
1404         /* Reserved; set 0; ignore on read */
1405         uint8_t reserved[2];
1406 };
1407
1408 struct ec_response_flash_info_2 {
1409         /* Total flash in the EC. */
1410         uint32_t flash_size;
1411         /* Flags; see EC_FLASH_INFO_* */
1412         uint32_t flags;
1413         /* Maximum size to use to send data to write to the EC. */
1414         uint32_t write_ideal_size;
1415         /* Number of banks present in the EC. */
1416         uint16_t num_banks_total;
1417         /* Number of banks described in banks array. */
1418         uint16_t num_banks_desc;
1419         struct ec_flash_bank banks[0];
1420 } __ec_align4;
1421
1422 /*
1423  * Read flash
1424  *
1425  * Response is params.size bytes of data.
1426  */
1427 #define EC_CMD_FLASH_READ 0x0011
1428
1429 /**
1430  * struct ec_params_flash_read - Parameters for the flash read command.
1431  * @offset: Byte offset to read.
1432  * @size: Size to read in bytes.
1433  */
1434 struct ec_params_flash_read {
1435         uint32_t offset;
1436         uint32_t size;
1437 } __ec_align4;
1438
1439 /* Write flash */
1440 #define EC_CMD_FLASH_WRITE 0x0012
1441 #define EC_VER_FLASH_WRITE 1
1442
1443 /* Version 0 of the flash command supported only 64 bytes of data */
1444 #define EC_FLASH_WRITE_VER0_SIZE 64
1445
1446 /**
1447  * struct ec_params_flash_write - Parameters for the flash write command.
1448  * @offset: Byte offset to write.
1449  * @size: Size to write in bytes.
1450  */
1451 struct ec_params_flash_write {
1452         uint32_t offset;
1453         uint32_t size;
1454         /* Followed by data to write */
1455 } __ec_align4;
1456
1457 /* Erase flash */
1458 #define EC_CMD_FLASH_ERASE 0x0013
1459
1460 /**
1461  * struct ec_params_flash_erase - Parameters for the flash erase command, v0.
1462  * @offset: Byte offset to erase.
1463  * @size: Size to erase in bytes.
1464  */
1465 struct ec_params_flash_erase {
1466         uint32_t offset;
1467         uint32_t size;
1468 } __ec_align4;
1469
1470 /*
1471  * v1 add async erase:
1472  * subcommands can returns:
1473  * EC_RES_SUCCESS : erased (see ERASE_SECTOR_ASYNC case below).
1474  * EC_RES_INVALID_PARAM : offset/size are not aligned on a erase boundary.
1475  * EC_RES_ERROR : other errors.
1476  * EC_RES_BUSY : an existing erase operation is in progress.
1477  * EC_RES_ACCESS_DENIED: Trying to erase running image.
1478  *
1479  * When ERASE_SECTOR_ASYNC returns EC_RES_SUCCESS, the operation is just
1480  * properly queued. The user must call ERASE_GET_RESULT subcommand to get
1481  * the proper result.
1482  * When ERASE_GET_RESULT returns EC_RES_BUSY, the caller must wait and send
1483  * ERASE_GET_RESULT again to get the result of ERASE_SECTOR_ASYNC.
1484  * ERASE_GET_RESULT command may timeout on EC where flash access is not
1485  * permitted while erasing. (For instance, STM32F4).
1486  */
1487 enum ec_flash_erase_cmd {
1488         FLASH_ERASE_SECTOR,     /* Erase and wait for result */
1489         FLASH_ERASE_SECTOR_ASYNC,  /* Erase and return immediately. */
1490         FLASH_ERASE_GET_RESULT,  /* Ask for last erase result */
1491 };
1492
1493 /**
1494  * struct ec_params_flash_erase_v1 - Parameters for the flash erase command, v1.
1495  * @cmd: One of ec_flash_erase_cmd.
1496  * @reserved: Pad byte; currently always contains 0.
1497  * @flag: No flags defined yet; set to 0.
1498  * @params: Same as v0 parameters.
1499  */
1500 struct ec_params_flash_erase_v1 {
1501         uint8_t  cmd;
1502         uint8_t  reserved;
1503         uint16_t flag;
1504         struct ec_params_flash_erase params;
1505 } __ec_align4;
1506
1507 /*
1508  * Get/set flash protection.
1509  *
1510  * If mask!=0, sets/clear the requested bits of flags.  Depending on the
1511  * firmware write protect GPIO, not all flags will take effect immediately;
1512  * some flags require a subsequent hard reset to take effect.  Check the
1513  * returned flags bits to see what actually happened.
1514  *
1515  * If mask=0, simply returns the current flags state.
1516  */
1517 #define EC_CMD_FLASH_PROTECT 0x0015
1518 #define EC_VER_FLASH_PROTECT 1  /* Command version 1 */
1519
1520 /* Flags for flash protection */
1521 /* RO flash code protected when the EC boots */
1522 #define EC_FLASH_PROTECT_RO_AT_BOOT         BIT(0)
1523 /*
1524  * RO flash code protected now.  If this bit is set, at-boot status cannot
1525  * be changed.
1526  */
1527 #define EC_FLASH_PROTECT_RO_NOW             BIT(1)
1528 /* Entire flash code protected now, until reboot. */
1529 #define EC_FLASH_PROTECT_ALL_NOW            BIT(2)
1530 /* Flash write protect GPIO is asserted now */
1531 #define EC_FLASH_PROTECT_GPIO_ASSERTED      BIT(3)
1532 /* Error - at least one bank of flash is stuck locked, and cannot be unlocked */
1533 #define EC_FLASH_PROTECT_ERROR_STUCK        BIT(4)
1534 /*
1535  * Error - flash protection is in inconsistent state.  At least one bank of
1536  * flash which should be protected is not protected.  Usually fixed by
1537  * re-requesting the desired flags, or by a hard reset if that fails.
1538  */
1539 #define EC_FLASH_PROTECT_ERROR_INCONSISTENT BIT(5)
1540 /* Entire flash code protected when the EC boots */
1541 #define EC_FLASH_PROTECT_ALL_AT_BOOT        BIT(6)
1542 /* RW flash code protected when the EC boots */
1543 #define EC_FLASH_PROTECT_RW_AT_BOOT         BIT(7)
1544 /* RW flash code protected now. */
1545 #define EC_FLASH_PROTECT_RW_NOW             BIT(8)
1546 /* Rollback information flash region protected when the EC boots */
1547 #define EC_FLASH_PROTECT_ROLLBACK_AT_BOOT   BIT(9)
1548 /* Rollback information flash region protected now */
1549 #define EC_FLASH_PROTECT_ROLLBACK_NOW       BIT(10)
1550
1551
1552 /**
1553  * struct ec_params_flash_protect - Parameters for the flash protect command.
1554  * @mask: Bits in flags to apply.
1555  * @flags: New flags to apply.
1556  */
1557 struct ec_params_flash_protect {
1558         uint32_t mask;
1559         uint32_t flags;
1560 } __ec_align4;
1561
1562 /**
1563  * struct ec_response_flash_protect - Response to the flash protect command.
1564  * @flags: Current value of flash protect flags.
1565  * @valid_flags: Flags which are valid on this platform. This allows the
1566  *               caller to distinguish between flags which aren't set vs. flags
1567  *               which can't be set on this platform.
1568  * @writable_flags: Flags which can be changed given the current protection
1569  *                  state.
1570  */
1571 struct ec_response_flash_protect {
1572         uint32_t flags;
1573         uint32_t valid_flags;
1574         uint32_t writable_flags;
1575 } __ec_align4;
1576
1577 /*
1578  * Note: commands 0x14 - 0x19 version 0 were old commands to get/set flash
1579  * write protect.  These commands may be reused with version > 0.
1580  */
1581
1582 /* Get the region offset/size */
1583 #define EC_CMD_FLASH_REGION_INFO 0x0016
1584 #define EC_VER_FLASH_REGION_INFO 1
1585
1586 enum ec_flash_region {
1587         /* Region which holds read-only EC image */
1588         EC_FLASH_REGION_RO = 0,
1589         /*
1590          * Region which holds active RW image. 'Active' is different from
1591          * 'running'. Active means 'scheduled-to-run'. Since RO image always
1592          * scheduled to run, active/non-active applies only to RW images (for
1593          * the same reason 'update' applies only to RW images. It's a state of
1594          * an image on a flash. Running image can be RO, RW_A, RW_B but active
1595          * image can only be RW_A or RW_B. In recovery mode, an active RW image
1596          * doesn't enter 'running' state but it's still active on a flash.
1597          */
1598         EC_FLASH_REGION_ACTIVE,
1599         /*
1600          * Region which should be write-protected in the factory (a superset of
1601          * EC_FLASH_REGION_RO)
1602          */
1603         EC_FLASH_REGION_WP_RO,
1604         /* Region which holds updatable (non-active) RW image */
1605         EC_FLASH_REGION_UPDATE,
1606         /* Number of regions */
1607         EC_FLASH_REGION_COUNT,
1608 };
1609 /*
1610  * 'RW' is vague if there are multiple RW images; we mean the active one,
1611  * so the old constant is deprecated.
1612  */
1613 #define EC_FLASH_REGION_RW EC_FLASH_REGION_ACTIVE
1614
1615 /**
1616  * struct ec_params_flash_region_info - Parameters for the flash region info
1617  *         command.
1618  * @region: Flash region; see EC_FLASH_REGION_*
1619  */
1620 struct ec_params_flash_region_info {
1621         uint32_t region;
1622 } __ec_align4;
1623
1624 struct ec_response_flash_region_info {
1625         uint32_t offset;
1626         uint32_t size;
1627 } __ec_align4;
1628
1629 /* Read/write VbNvContext */
1630 #define EC_CMD_VBNV_CONTEXT 0x0017
1631 #define EC_VER_VBNV_CONTEXT 1
1632 #define EC_VBNV_BLOCK_SIZE 16
1633
1634 enum ec_vbnvcontext_op {
1635         EC_VBNV_CONTEXT_OP_READ,
1636         EC_VBNV_CONTEXT_OP_WRITE,
1637 };
1638
1639 struct ec_params_vbnvcontext {
1640         uint32_t op;
1641         uint8_t block[EC_VBNV_BLOCK_SIZE];
1642 } __ec_align4;
1643
1644 struct ec_response_vbnvcontext {
1645         uint8_t block[EC_VBNV_BLOCK_SIZE];
1646 } __ec_align4;
1647
1648
1649 /* Get SPI flash information */
1650 #define EC_CMD_FLASH_SPI_INFO 0x0018
1651
1652 struct ec_response_flash_spi_info {
1653         /* JEDEC info from command 0x9F (manufacturer, memory type, size) */
1654         uint8_t jedec[3];
1655
1656         /* Pad byte; currently always contains 0 */
1657         uint8_t reserved0;
1658
1659         /* Manufacturer / device ID from command 0x90 */
1660         uint8_t mfr_dev_id[2];
1661
1662         /* Status registers from command 0x05 and 0x35 */
1663         uint8_t sr1, sr2;
1664 } __ec_align1;
1665
1666
1667 /* Select flash during flash operations */
1668 #define EC_CMD_FLASH_SELECT 0x0019
1669
1670 /**
1671  * struct ec_params_flash_select - Parameters for the flash select command.
1672  * @select: 1 to select flash, 0 to deselect flash
1673  */
1674 struct ec_params_flash_select {
1675         uint8_t select;
1676 } __ec_align4;
1677
1678
1679 /*****************************************************************************/
1680 /* PWM commands */
1681
1682 /* Get fan target RPM */
1683 #define EC_CMD_PWM_GET_FAN_TARGET_RPM 0x0020
1684
1685 struct ec_response_pwm_get_fan_rpm {
1686         uint32_t rpm;
1687 } __ec_align4;
1688
1689 /* Set target fan RPM */
1690 #define EC_CMD_PWM_SET_FAN_TARGET_RPM 0x0021
1691
1692 /* Version 0 of input params */
1693 struct ec_params_pwm_set_fan_target_rpm_v0 {
1694         uint32_t rpm;
1695 } __ec_align4;
1696
1697 /* Version 1 of input params */
1698 struct ec_params_pwm_set_fan_target_rpm_v1 {
1699         uint32_t rpm;
1700         uint8_t fan_idx;
1701 } __ec_align_size1;
1702
1703 /* Get keyboard backlight */
1704 /* OBSOLETE - Use EC_CMD_PWM_SET_DUTY */
1705 #define EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT 0x0022
1706
1707 struct ec_response_pwm_get_keyboard_backlight {
1708         uint8_t percent;
1709         uint8_t enabled;
1710 } __ec_align1;
1711
1712 /* Set keyboard backlight */
1713 /* OBSOLETE - Use EC_CMD_PWM_SET_DUTY */
1714 #define EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT 0x0023
1715
1716 struct ec_params_pwm_set_keyboard_backlight {
1717         uint8_t percent;
1718 } __ec_align1;
1719
1720 /* Set target fan PWM duty cycle */
1721 #define EC_CMD_PWM_SET_FAN_DUTY 0x0024
1722
1723 /* Version 0 of input params */
1724 struct ec_params_pwm_set_fan_duty_v0 {
1725         uint32_t percent;
1726 } __ec_align4;
1727
1728 /* Version 1 of input params */
1729 struct ec_params_pwm_set_fan_duty_v1 {
1730         uint32_t percent;
1731         uint8_t fan_idx;
1732 } __ec_align_size1;
1733
1734 #define EC_CMD_PWM_SET_DUTY 0x0025
1735 /* 16 bit duty cycle, 0xffff = 100% */
1736 #define EC_PWM_MAX_DUTY 0xffff
1737
1738 enum ec_pwm_type {
1739         /* All types, indexed by board-specific enum pwm_channel */
1740         EC_PWM_TYPE_GENERIC = 0,
1741         /* Keyboard backlight */
1742         EC_PWM_TYPE_KB_LIGHT,
1743         /* Display backlight */
1744         EC_PWM_TYPE_DISPLAY_LIGHT,
1745         EC_PWM_TYPE_COUNT,
1746 };
1747
1748 struct ec_params_pwm_set_duty {
1749         uint16_t duty;     /* Duty cycle, EC_PWM_MAX_DUTY = 100% */
1750         uint8_t pwm_type;  /* ec_pwm_type */
1751         uint8_t index;     /* Type-specific index, or 0 if unique */
1752 } __ec_align4;
1753
1754 #define EC_CMD_PWM_GET_DUTY 0x0026
1755
1756 struct ec_params_pwm_get_duty {
1757         uint8_t pwm_type;  /* ec_pwm_type */
1758         uint8_t index;     /* Type-specific index, or 0 if unique */
1759 } __ec_align1;
1760
1761 struct ec_response_pwm_get_duty {
1762         uint16_t duty;     /* Duty cycle, EC_PWM_MAX_DUTY = 100% */
1763 } __ec_align2;
1764
1765 /*****************************************************************************/
1766 /*
1767  * Lightbar commands. This looks worse than it is. Since we only use one HOST
1768  * command to say "talk to the lightbar", we put the "and tell it to do X" part
1769  * into a subcommand. We'll make separate structs for subcommands with
1770  * different input args, so that we know how much to expect.
1771  */
1772 #define EC_CMD_LIGHTBAR_CMD 0x0028
1773
1774 struct rgb_s {
1775         uint8_t r, g, b;
1776 } __ec_todo_unpacked;
1777
1778 #define LB_BATTERY_LEVELS 4
1779
1780 /*
1781  * List of tweakable parameters. NOTE: It's __packed so it can be sent in a
1782  * host command, but the alignment is the same regardless. Keep it that way.
1783  */
1784 struct lightbar_params_v0 {
1785         /* Timing */
1786         int32_t google_ramp_up;
1787         int32_t google_ramp_down;
1788         int32_t s3s0_ramp_up;
1789         int32_t s0_tick_delay[2];               /* AC=0/1 */
1790         int32_t s0a_tick_delay[2];              /* AC=0/1 */
1791         int32_t s0s3_ramp_down;
1792         int32_t s3_sleep_for;
1793         int32_t s3_ramp_up;
1794         int32_t s3_ramp_down;
1795
1796         /* Oscillation */
1797         uint8_t new_s0;
1798         uint8_t osc_min[2];                     /* AC=0/1 */
1799         uint8_t osc_max[2];                     /* AC=0/1 */
1800         uint8_t w_ofs[2];                       /* AC=0/1 */
1801
1802         /* Brightness limits based on the backlight and AC. */
1803         uint8_t bright_bl_off_fixed[2];         /* AC=0/1 */
1804         uint8_t bright_bl_on_min[2];            /* AC=0/1 */
1805         uint8_t bright_bl_on_max[2];            /* AC=0/1 */
1806
1807         /* Battery level thresholds */
1808         uint8_t battery_threshold[LB_BATTERY_LEVELS - 1];
1809
1810         /* Map [AC][battery_level] to color index */
1811         uint8_t s0_idx[2][LB_BATTERY_LEVELS];   /* AP is running */
1812         uint8_t s3_idx[2][LB_BATTERY_LEVELS];   /* AP is sleeping */
1813
1814         /* Color palette */
1815         struct rgb_s color[8];                  /* 0-3 are Google colors */
1816 } __ec_todo_packed;
1817
1818 struct lightbar_params_v1 {
1819         /* Timing */
1820         int32_t google_ramp_up;
1821         int32_t google_ramp_down;
1822         int32_t s3s0_ramp_up;
1823         int32_t s0_tick_delay[2];               /* AC=0/1 */
1824         int32_t s0a_tick_delay[2];              /* AC=0/1 */
1825         int32_t s0s3_ramp_down;
1826         int32_t s3_sleep_for;
1827         int32_t s3_ramp_up;
1828         int32_t s3_ramp_down;
1829         int32_t s5_ramp_up;
1830         int32_t s5_ramp_down;
1831         int32_t tap_tick_delay;
1832         int32_t tap_gate_delay;
1833         int32_t tap_display_time;
1834
1835         /* Tap-for-battery params */
1836         uint8_t tap_pct_red;
1837         uint8_t tap_pct_green;
1838         uint8_t tap_seg_min_on;
1839         uint8_t tap_seg_max_on;
1840         uint8_t tap_seg_osc;
1841         uint8_t tap_idx[3];
1842
1843         /* Oscillation */
1844         uint8_t osc_min[2];                     /* AC=0/1 */
1845         uint8_t osc_max[2];                     /* AC=0/1 */
1846         uint8_t w_ofs[2];                       /* AC=0/1 */
1847
1848         /* Brightness limits based on the backlight and AC. */
1849         uint8_t bright_bl_off_fixed[2];         /* AC=0/1 */
1850         uint8_t bright_bl_on_min[2];            /* AC=0/1 */
1851         uint8_t bright_bl_on_max[2];            /* AC=0/1 */
1852
1853         /* Battery level thresholds */
1854         uint8_t battery_threshold[LB_BATTERY_LEVELS - 1];
1855
1856         /* Map [AC][battery_level] to color index */
1857         uint8_t s0_idx[2][LB_BATTERY_LEVELS];   /* AP is running */
1858         uint8_t s3_idx[2][LB_BATTERY_LEVELS];   /* AP is sleeping */
1859
1860         /* s5: single color pulse on inhibited power-up */
1861         uint8_t s5_idx;
1862
1863         /* Color palette */
1864         struct rgb_s color[8];                  /* 0-3 are Google colors */
1865 } __ec_todo_packed;
1866
1867 /* Lightbar command params v2
1868  * crbug.com/467716
1869  *
1870  * lightbar_parms_v1 was too big for i2c, therefore in v2, we split them up by
1871  * logical groups to make it more manageable ( < 120 bytes).
1872  *
1873  * NOTE: Each of these groups must be less than 120 bytes.
1874  */
1875
1876 struct lightbar_params_v2_timing {
1877         /* Timing */
1878         int32_t google_ramp_up;
1879         int32_t google_ramp_down;
1880         int32_t s3s0_ramp_up;
1881         int32_t s0_tick_delay[2];               /* AC=0/1 */
1882         int32_t s0a_tick_delay[2];              /* AC=0/1 */
1883         int32_t s0s3_ramp_down;
1884         int32_t s3_sleep_for;
1885         int32_t s3_ramp_up;
1886         int32_t s3_ramp_down;
1887         int32_t s5_ramp_up;
1888         int32_t s5_ramp_down;
1889         int32_t tap_tick_delay;
1890         int32_t tap_gate_delay;
1891         int32_t tap_display_time;
1892 } __ec_todo_packed;
1893
1894 struct lightbar_params_v2_tap {
1895         /* Tap-for-battery params */
1896         uint8_t tap_pct_red;
1897         uint8_t tap_pct_green;
1898         uint8_t tap_seg_min_on;
1899         uint8_t tap_seg_max_on;
1900         uint8_t tap_seg_osc;
1901         uint8_t tap_idx[3];
1902 } __ec_todo_packed;
1903
1904 struct lightbar_params_v2_oscillation {
1905         /* Oscillation */
1906         uint8_t osc_min[2];                     /* AC=0/1 */
1907         uint8_t osc_max[2];                     /* AC=0/1 */
1908         uint8_t w_ofs[2];                       /* AC=0/1 */
1909 } __ec_todo_packed;
1910
1911 struct lightbar_params_v2_brightness {
1912         /* Brightness limits based on the backlight and AC. */
1913         uint8_t bright_bl_off_fixed[2];         /* AC=0/1 */
1914         uint8_t bright_bl_on_min[2];            /* AC=0/1 */
1915         uint8_t bright_bl_on_max[2];            /* AC=0/1 */
1916 } __ec_todo_packed;
1917
1918 struct lightbar_params_v2_thresholds {
1919         /* Battery level thresholds */
1920         uint8_t battery_threshold[LB_BATTERY_LEVELS - 1];
1921 } __ec_todo_packed;
1922
1923 struct lightbar_params_v2_colors {
1924         /* Map [AC][battery_level] to color index */
1925         uint8_t s0_idx[2][LB_BATTERY_LEVELS];   /* AP is running */
1926         uint8_t s3_idx[2][LB_BATTERY_LEVELS];   /* AP is sleeping */
1927
1928         /* s5: single color pulse on inhibited power-up */
1929         uint8_t s5_idx;
1930
1931         /* Color palette */
1932         struct rgb_s color[8];                  /* 0-3 are Google colors */
1933 } __ec_todo_packed;
1934
1935 /* Lightbar program. */
1936 #define EC_LB_PROG_LEN 192
1937 struct lightbar_program {
1938         uint8_t size;
1939         uint8_t data[EC_LB_PROG_LEN];
1940 } __ec_todo_unpacked;
1941
1942 struct ec_params_lightbar {
1943         uint8_t cmd;                  /* Command (see enum lightbar_command) */
1944         union {
1945                 /*
1946                  * The following commands have no args:
1947                  *
1948                  * dump, off, on, init, get_seq, get_params_v0, get_params_v1,
1949                  * version, get_brightness, get_demo, suspend, resume,
1950                  * get_params_v2_timing, get_params_v2_tap, get_params_v2_osc,
1951                  * get_params_v2_bright, get_params_v2_thlds,
1952                  * get_params_v2_colors
1953                  *
1954                  * Don't use an empty struct, because C++ hates that.
1955                  */
1956
1957                 struct __ec_todo_unpacked {
1958                         uint8_t num;
1959                 } set_brightness, seq, demo;
1960
1961                 struct __ec_todo_unpacked {
1962                         uint8_t ctrl, reg, value;
1963                 } reg;
1964
1965                 struct __ec_todo_unpacked {
1966                         uint8_t led, red, green, blue;
1967                 } set_rgb;
1968
1969                 struct __ec_todo_unpacked {
1970                         uint8_t led;
1971                 } get_rgb;
1972
1973                 struct __ec_todo_unpacked {
1974                         uint8_t enable;
1975                 } manual_suspend_ctrl;
1976
1977                 struct lightbar_params_v0 set_params_v0;
1978                 struct lightbar_params_v1 set_params_v1;
1979
1980                 struct lightbar_params_v2_timing set_v2par_timing;
1981                 struct lightbar_params_v2_tap set_v2par_tap;
1982                 struct lightbar_params_v2_oscillation set_v2par_osc;
1983                 struct lightbar_params_v2_brightness set_v2par_bright;
1984                 struct lightbar_params_v2_thresholds set_v2par_thlds;
1985                 struct lightbar_params_v2_colors set_v2par_colors;
1986
1987                 struct lightbar_program set_program;
1988         };
1989 } __ec_todo_packed;
1990
1991 struct ec_response_lightbar {
1992         union {
1993                 struct __ec_todo_unpacked {
1994                         struct __ec_todo_unpacked {
1995                                 uint8_t reg;
1996                                 uint8_t ic0;
1997                                 uint8_t ic1;
1998                         } vals[23];
1999                 } dump;
2000
2001                 struct __ec_todo_unpacked {
2002                         uint8_t num;
2003                 } get_seq, get_brightness, get_demo;
2004
2005                 struct lightbar_params_v0 get_params_v0;
2006                 struct lightbar_params_v1 get_params_v1;
2007
2008
2009                 struct lightbar_params_v2_timing get_params_v2_timing;
2010                 struct lightbar_params_v2_tap get_params_v2_tap;
2011                 struct lightbar_params_v2_oscillation get_params_v2_osc;
2012                 struct lightbar_params_v2_brightness get_params_v2_bright;
2013                 struct lightbar_params_v2_thresholds get_params_v2_thlds;
2014                 struct lightbar_params_v2_colors get_params_v2_colors;
2015
2016                 struct __ec_todo_unpacked {
2017                         uint32_t num;
2018                         uint32_t flags;
2019                 } version;
2020
2021                 struct __ec_todo_unpacked {
2022                         uint8_t red, green, blue;
2023                 } get_rgb;
2024
2025                 /*
2026                  * The following commands have no response:
2027                  *
2028                  * off, on, init, set_brightness, seq, reg, set_rgb, demo,
2029                  * set_params_v0, set_params_v1, set_program,
2030                  * manual_suspend_ctrl, suspend, resume, set_v2par_timing,
2031                  * set_v2par_tap, set_v2par_osc, set_v2par_bright,
2032                  * set_v2par_thlds, set_v2par_colors
2033                  */
2034         };
2035 } __ec_todo_packed;
2036
2037 /* Lightbar commands */
2038 enum lightbar_command {
2039         LIGHTBAR_CMD_DUMP = 0,
2040         LIGHTBAR_CMD_OFF = 1,
2041         LIGHTBAR_CMD_ON = 2,
2042         LIGHTBAR_CMD_INIT = 3,
2043         LIGHTBAR_CMD_SET_BRIGHTNESS = 4,
2044         LIGHTBAR_CMD_SEQ = 5,
2045         LIGHTBAR_CMD_REG = 6,
2046         LIGHTBAR_CMD_SET_RGB = 7,
2047         LIGHTBAR_CMD_GET_SEQ = 8,
2048         LIGHTBAR_CMD_DEMO = 9,
2049         LIGHTBAR_CMD_GET_PARAMS_V0 = 10,
2050         LIGHTBAR_CMD_SET_PARAMS_V0 = 11,
2051         LIGHTBAR_CMD_VERSION = 12,
2052         LIGHTBAR_CMD_GET_BRIGHTNESS = 13,
2053         LIGHTBAR_CMD_GET_RGB = 14,
2054         LIGHTBAR_CMD_GET_DEMO = 15,
2055         LIGHTBAR_CMD_GET_PARAMS_V1 = 16,
2056         LIGHTBAR_CMD_SET_PARAMS_V1 = 17,
2057         LIGHTBAR_CMD_SET_PROGRAM = 18,
2058         LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL = 19,
2059         LIGHTBAR_CMD_SUSPEND = 20,
2060         LIGHTBAR_CMD_RESUME = 21,
2061         LIGHTBAR_CMD_GET_PARAMS_V2_TIMING = 22,
2062         LIGHTBAR_CMD_SET_PARAMS_V2_TIMING = 23,
2063         LIGHTBAR_CMD_GET_PARAMS_V2_TAP = 24,
2064         LIGHTBAR_CMD_SET_PARAMS_V2_TAP = 25,
2065         LIGHTBAR_CMD_GET_PARAMS_V2_OSCILLATION = 26,
2066         LIGHTBAR_CMD_SET_PARAMS_V2_OSCILLATION = 27,
2067         LIGHTBAR_CMD_GET_PARAMS_V2_BRIGHTNESS = 28,
2068         LIGHTBAR_CMD_SET_PARAMS_V2_BRIGHTNESS = 29,
2069         LIGHTBAR_CMD_GET_PARAMS_V2_THRESHOLDS = 30,
2070         LIGHTBAR_CMD_SET_PARAMS_V2_THRESHOLDS = 31,
2071         LIGHTBAR_CMD_GET_PARAMS_V2_COLORS = 32,
2072         LIGHTBAR_CMD_SET_PARAMS_V2_COLORS = 33,
2073         LIGHTBAR_NUM_CMDS
2074 };
2075
2076 /*****************************************************************************/
2077 /* LED control commands */
2078
2079 #define EC_CMD_LED_CONTROL 0x0029
2080
2081 enum ec_led_id {
2082         /* LED to indicate battery state of charge */
2083         EC_LED_ID_BATTERY_LED = 0,
2084         /*
2085          * LED to indicate system power state (on or in suspend).
2086          * May be on power button or on C-panel.
2087          */
2088         EC_LED_ID_POWER_LED,
2089         /* LED on power adapter or its plug */
2090         EC_LED_ID_ADAPTER_LED,
2091         /* LED to indicate left side */
2092         EC_LED_ID_LEFT_LED,
2093         /* LED to indicate right side */
2094         EC_LED_ID_RIGHT_LED,
2095         /* LED to indicate recovery mode with HW_REINIT */
2096         EC_LED_ID_RECOVERY_HW_REINIT_LED,
2097         /* LED to indicate sysrq debug mode. */
2098         EC_LED_ID_SYSRQ_DEBUG_LED,
2099
2100         EC_LED_ID_COUNT
2101 };
2102
2103 /* LED control flags */
2104 #define EC_LED_FLAGS_QUERY BIT(0) /* Query LED capability only */
2105 #define EC_LED_FLAGS_AUTO  BIT(1) /* Switch LED back to automatic control */
2106
2107 enum ec_led_colors {
2108         EC_LED_COLOR_RED = 0,
2109         EC_LED_COLOR_GREEN,
2110         EC_LED_COLOR_BLUE,
2111         EC_LED_COLOR_YELLOW,
2112         EC_LED_COLOR_WHITE,
2113         EC_LED_COLOR_AMBER,
2114
2115         EC_LED_COLOR_COUNT
2116 };
2117
2118 struct ec_params_led_control {
2119         uint8_t led_id;     /* Which LED to control */
2120         uint8_t flags;      /* Control flags */
2121
2122         uint8_t brightness[EC_LED_COLOR_COUNT];
2123 } __ec_align1;
2124
2125 struct ec_response_led_control {
2126         /*
2127          * Available brightness value range.
2128          *
2129          * Range 0 means color channel not present.
2130          * Range 1 means on/off control.
2131          * Other values means the LED is control by PWM.
2132          */
2133         uint8_t brightness_range[EC_LED_COLOR_COUNT];
2134 } __ec_align1;
2135
2136 /*****************************************************************************/
2137 /* Verified boot commands */
2138
2139 /*
2140  * Note: command code 0x29 version 0 was VBOOT_CMD in Link EVT; it may be
2141  * reused for other purposes with version > 0.
2142  */
2143
2144 /* Verified boot hash command */
2145 #define EC_CMD_VBOOT_HASH 0x002A
2146
2147 struct ec_params_vboot_hash {
2148         uint8_t cmd;             /* enum ec_vboot_hash_cmd */
2149         uint8_t hash_type;       /* enum ec_vboot_hash_type */
2150         uint8_t nonce_size;      /* Nonce size; may be 0 */
2151         uint8_t reserved0;       /* Reserved; set 0 */
2152         uint32_t offset;         /* Offset in flash to hash */
2153         uint32_t size;           /* Number of bytes to hash */
2154         uint8_t nonce_data[64];  /* Nonce data; ignored if nonce_size=0 */
2155 } __ec_align4;
2156
2157 struct ec_response_vboot_hash {
2158         uint8_t status;          /* enum ec_vboot_hash_status */
2159         uint8_t hash_type;       /* enum ec_vboot_hash_type */
2160         uint8_t digest_size;     /* Size of hash digest in bytes */
2161         uint8_t reserved0;       /* Ignore; will be 0 */
2162         uint32_t offset;         /* Offset in flash which was hashed */
2163         uint32_t size;           /* Number of bytes hashed */
2164         uint8_t hash_digest[64]; /* Hash digest data */
2165 } __ec_align4;
2166
2167 enum ec_vboot_hash_cmd {
2168         EC_VBOOT_HASH_GET = 0,       /* Get current hash status */
2169         EC_VBOOT_HASH_ABORT = 1,     /* Abort calculating current hash */
2170         EC_VBOOT_HASH_START = 2,     /* Start computing a new hash */
2171         EC_VBOOT_HASH_RECALC = 3,    /* Synchronously compute a new hash */
2172 };
2173
2174 enum ec_vboot_hash_type {
2175         EC_VBOOT_HASH_TYPE_SHA256 = 0, /* SHA-256 */
2176 };
2177
2178 enum ec_vboot_hash_status {
2179         EC_VBOOT_HASH_STATUS_NONE = 0, /* No hash (not started, or aborted) */
2180         EC_VBOOT_HASH_STATUS_DONE = 1, /* Finished computing a hash */
2181         EC_VBOOT_HASH_STATUS_BUSY = 2, /* Busy computing a hash */
2182 };
2183
2184 /*
2185  * Special values for offset for EC_VBOOT_HASH_START and EC_VBOOT_HASH_RECALC.
2186  * If one of these is specified, the EC will automatically update offset and
2187  * size to the correct values for the specified image (RO or RW).
2188  */
2189 #define EC_VBOOT_HASH_OFFSET_RO         0xfffffffe
2190 #define EC_VBOOT_HASH_OFFSET_ACTIVE     0xfffffffd
2191 #define EC_VBOOT_HASH_OFFSET_UPDATE     0xfffffffc
2192
2193 /*
2194  * 'RW' is vague if there are multiple RW images; we mean the active one,
2195  * so the old constant is deprecated.
2196  */
2197 #define EC_VBOOT_HASH_OFFSET_RW EC_VBOOT_HASH_OFFSET_ACTIVE
2198
2199 /*****************************************************************************/
2200 /*
2201  * Motion sense commands. We'll make separate structs for sub-commands with
2202  * different input args, so that we know how much to expect.
2203  */
2204 #define EC_CMD_MOTION_SENSE_CMD 0x002B
2205
2206 /* Motion sense commands */
2207 enum motionsense_command {
2208         /*
2209          * Dump command returns all motion sensor data including motion sense
2210          * module flags and individual sensor flags.
2211          */
2212         MOTIONSENSE_CMD_DUMP = 0,
2213
2214         /*
2215          * Info command returns data describing the details of a given sensor,
2216          * including enum motionsensor_type, enum motionsensor_location, and
2217          * enum motionsensor_chip.
2218          */
2219         MOTIONSENSE_CMD_INFO = 1,
2220
2221         /*
2222          * EC Rate command is a setter/getter command for the EC sampling rate
2223          * in milliseconds.
2224          * It is per sensor, the EC run sample task  at the minimum of all
2225          * sensors EC_RATE.
2226          * For sensors without hardware FIFO, EC_RATE should be equals to 1/ODR
2227          * to collect all the sensor samples.
2228          * For sensor with hardware FIFO, EC_RATE is used as the maximal delay
2229          * to process of all motion sensors in milliseconds.
2230          */
2231         MOTIONSENSE_CMD_EC_RATE = 2,
2232
2233         /*
2234          * Sensor ODR command is a setter/getter command for the output data
2235          * rate of a specific motion sensor in millihertz.
2236          */
2237         MOTIONSENSE_CMD_SENSOR_ODR = 3,
2238
2239         /*
2240          * Sensor range command is a setter/getter command for the range of
2241          * a specified motion sensor in +/-G's or +/- deg/s.
2242          */
2243         MOTIONSENSE_CMD_SENSOR_RANGE = 4,
2244
2245         /*
2246          * Setter/getter command for the keyboard wake angle. When the lid
2247          * angle is greater than this value, keyboard wake is disabled in S3,
2248          * and when the lid angle goes less than this value, keyboard wake is
2249          * enabled. Note, the lid angle measurement is an approximate,
2250          * un-calibrated value, hence the wake angle isn't exact.
2251          */
2252         MOTIONSENSE_CMD_KB_WAKE_ANGLE = 5,
2253
2254         /*
2255          * Returns a single sensor data.
2256          */
2257         MOTIONSENSE_CMD_DATA = 6,
2258
2259         /*
2260          * Return sensor fifo info.
2261          */
2262         MOTIONSENSE_CMD_FIFO_INFO = 7,
2263
2264         /*
2265          * Insert a flush element in the fifo and return sensor fifo info.
2266          * The host can use that element to synchronize its operation.
2267          */
2268         MOTIONSENSE_CMD_FIFO_FLUSH = 8,
2269
2270         /*
2271          * Return a portion of the fifo.
2272          */
2273         MOTIONSENSE_CMD_FIFO_READ = 9,
2274
2275         /*
2276          * Perform low level calibration.
2277          * On sensors that support it, ask to do offset calibration.
2278          */
2279         MOTIONSENSE_CMD_PERFORM_CALIB = 10,
2280
2281         /*
2282          * Sensor Offset command is a setter/getter command for the offset
2283          * used for calibration.
2284          * The offsets can be calculated by the host, or via
2285          * PERFORM_CALIB command.
2286          */
2287         MOTIONSENSE_CMD_SENSOR_OFFSET = 11,
2288
2289         /*
2290          * List available activities for a MOTION sensor.
2291          * Indicates if they are enabled or disabled.
2292          */
2293         MOTIONSENSE_CMD_LIST_ACTIVITIES = 12,
2294
2295         /*
2296          * Activity management
2297          * Enable/Disable activity recognition.
2298          */
2299         MOTIONSENSE_CMD_SET_ACTIVITY = 13,
2300
2301         /*
2302          * Lid Angle
2303          */
2304         MOTIONSENSE_CMD_LID_ANGLE = 14,
2305
2306         /*
2307          * Allow the FIFO to trigger interrupt via MKBP events.
2308          * By default the FIFO does not send interrupt to process the FIFO
2309          * until the AP is ready or it is coming from a wakeup sensor.
2310          */
2311         MOTIONSENSE_CMD_FIFO_INT_ENABLE = 15,
2312
2313         /*
2314          * Spoof the readings of the sensors.  The spoofed readings can be set
2315          * to arbitrary values, or will lock to the last read actual values.
2316          */
2317         MOTIONSENSE_CMD_SPOOF = 16,
2318
2319         /* Set lid angle for tablet mode detection. */
2320         MOTIONSENSE_CMD_TABLET_MODE_LID_ANGLE = 17,
2321
2322         /*
2323          * Sensor Scale command is a setter/getter command for the calibration
2324          * scale.
2325          */
2326         MOTIONSENSE_CMD_SENSOR_SCALE = 18,
2327
2328         /* Number of motionsense sub-commands. */
2329         MOTIONSENSE_NUM_CMDS
2330 };
2331
2332 /* List of motion sensor types. */
2333 enum motionsensor_type {
2334         MOTIONSENSE_TYPE_ACCEL = 0,
2335         MOTIONSENSE_TYPE_GYRO = 1,
2336         MOTIONSENSE_TYPE_MAG = 2,
2337         MOTIONSENSE_TYPE_PROX = 3,
2338         MOTIONSENSE_TYPE_LIGHT = 4,
2339         MOTIONSENSE_TYPE_ACTIVITY = 5,
2340         MOTIONSENSE_TYPE_BARO = 6,
2341         MOTIONSENSE_TYPE_SYNC = 7,
2342         MOTIONSENSE_TYPE_MAX,
2343 };
2344
2345 /* List of motion sensor locations. */
2346 enum motionsensor_location {
2347         MOTIONSENSE_LOC_BASE = 0,
2348         MOTIONSENSE_LOC_LID = 1,
2349         MOTIONSENSE_LOC_CAMERA = 2,
2350         MOTIONSENSE_LOC_MAX,
2351 };
2352
2353 /* List of motion sensor chips. */
2354 enum motionsensor_chip {
2355         MOTIONSENSE_CHIP_KXCJ9 = 0,
2356         MOTIONSENSE_CHIP_LSM6DS0 = 1,
2357         MOTIONSENSE_CHIP_BMI160 = 2,
2358         MOTIONSENSE_CHIP_SI1141 = 3,
2359         MOTIONSENSE_CHIP_SI1142 = 4,
2360         MOTIONSENSE_CHIP_SI1143 = 5,
2361         MOTIONSENSE_CHIP_KX022 = 6,
2362         MOTIONSENSE_CHIP_L3GD20H = 7,
2363         MOTIONSENSE_CHIP_BMA255 = 8,
2364         MOTIONSENSE_CHIP_BMP280 = 9,
2365         MOTIONSENSE_CHIP_OPT3001 = 10,
2366         MOTIONSENSE_CHIP_BH1730 = 11,
2367         MOTIONSENSE_CHIP_GPIO = 12,
2368         MOTIONSENSE_CHIP_LIS2DH = 13,
2369         MOTIONSENSE_CHIP_LSM6DSM = 14,
2370         MOTIONSENSE_CHIP_LIS2DE = 15,
2371         MOTIONSENSE_CHIP_LIS2MDL = 16,
2372         MOTIONSENSE_CHIP_LSM6DS3 = 17,
2373         MOTIONSENSE_CHIP_LSM6DSO = 18,
2374         MOTIONSENSE_CHIP_LNG2DM = 19,
2375         MOTIONSENSE_CHIP_MAX,
2376 };
2377
2378 /* List of orientation positions */
2379 enum motionsensor_orientation {
2380         MOTIONSENSE_ORIENTATION_LANDSCAPE = 0,
2381         MOTIONSENSE_ORIENTATION_PORTRAIT = 1,
2382         MOTIONSENSE_ORIENTATION_UPSIDE_DOWN_PORTRAIT = 2,
2383         MOTIONSENSE_ORIENTATION_UPSIDE_DOWN_LANDSCAPE = 3,
2384         MOTIONSENSE_ORIENTATION_UNKNOWN = 4,
2385 };
2386
2387 struct ec_response_motion_sensor_data {
2388         /* Flags for each sensor. */
2389         uint8_t flags;
2390         /* Sensor number the data comes from. */
2391         uint8_t sensor_num;
2392         /* Each sensor is up to 3-axis. */
2393         union {
2394                 int16_t             data[3];
2395                 struct __ec_todo_packed {
2396                         uint16_t    reserved;
2397                         uint32_t    timestamp;
2398                 };
2399                 struct __ec_todo_unpacked {
2400                         uint8_t     activity; /* motionsensor_activity */
2401                         uint8_t     state;
2402                         int16_t     add_info[2];
2403                 };
2404         };
2405 } __ec_todo_packed;
2406
2407 /* Note: used in ec_response_get_next_data */
2408 struct ec_response_motion_sense_fifo_info {
2409         /* Size of the fifo */
2410         uint16_t size;
2411         /* Amount of space used in the fifo */
2412         uint16_t count;
2413         /* Timestamp recorded in us.
2414          * aka accurate timestamp when host event was triggered.
2415          */
2416         uint32_t timestamp;
2417         /* Total amount of vector lost */
2418         uint16_t total_lost;
2419         /* Lost events since the last fifo_info, per sensors */
2420         uint16_t lost[0];
2421 } __ec_todo_packed;
2422
2423 struct ec_response_motion_sense_fifo_data {
2424         uint32_t number_data;
2425         struct ec_response_motion_sensor_data data[0];
2426 } __ec_todo_packed;
2427
2428 /* List supported activity recognition */
2429 enum motionsensor_activity {
2430         MOTIONSENSE_ACTIVITY_RESERVED = 0,
2431         MOTIONSENSE_ACTIVITY_SIG_MOTION = 1,
2432         MOTIONSENSE_ACTIVITY_DOUBLE_TAP = 2,
2433         MOTIONSENSE_ACTIVITY_ORIENTATION = 3,
2434 };
2435
2436 struct ec_motion_sense_activity {
2437         uint8_t sensor_num;
2438         uint8_t activity; /* one of enum motionsensor_activity */
2439         uint8_t enable;   /* 1: enable, 0: disable */
2440         uint8_t reserved;
2441         uint16_t parameters[3]; /* activity dependent parameters */
2442 } __ec_todo_unpacked;
2443
2444 /* Module flag masks used for the dump sub-command. */
2445 #define MOTIONSENSE_MODULE_FLAG_ACTIVE BIT(0)
2446
2447 /* Sensor flag masks used for the dump sub-command. */
2448 #define MOTIONSENSE_SENSOR_FLAG_PRESENT BIT(0)
2449
2450 /*
2451  * Flush entry for synchronization.
2452  * data contains time stamp
2453  */
2454 #define MOTIONSENSE_SENSOR_FLAG_FLUSH BIT(0)
2455 #define MOTIONSENSE_SENSOR_FLAG_TIMESTAMP BIT(1)
2456 #define MOTIONSENSE_SENSOR_FLAG_WAKEUP BIT(2)
2457 #define MOTIONSENSE_SENSOR_FLAG_TABLET_MODE BIT(3)
2458 #define MOTIONSENSE_SENSOR_FLAG_ODR BIT(4)
2459
2460 /*
2461  * Send this value for the data element to only perform a read. If you
2462  * send any other value, the EC will interpret it as data to set and will
2463  * return the actual value set.
2464  */
2465 #define EC_MOTION_SENSE_NO_VALUE -1
2466
2467 #define EC_MOTION_SENSE_INVALID_CALIB_TEMP 0x8000
2468
2469 /* MOTIONSENSE_CMD_SENSOR_OFFSET subcommand flag */
2470 /* Set Calibration information */
2471 #define MOTION_SENSE_SET_OFFSET BIT(0)
2472
2473 /* Default Scale value, factor 1. */
2474 #define MOTION_SENSE_DEFAULT_SCALE BIT(15)
2475
2476 #define LID_ANGLE_UNRELIABLE 500
2477
2478 enum motionsense_spoof_mode {
2479         /* Disable spoof mode. */
2480         MOTIONSENSE_SPOOF_MODE_DISABLE = 0,
2481
2482         /* Enable spoof mode, but use provided component values. */
2483         MOTIONSENSE_SPOOF_MODE_CUSTOM,
2484
2485         /* Enable spoof mode, but use the current sensor values. */
2486         MOTIONSENSE_SPOOF_MODE_LOCK_CURRENT,
2487
2488         /* Query the current spoof mode status for the sensor. */
2489         MOTIONSENSE_SPOOF_MODE_QUERY,
2490 };
2491
2492 struct ec_params_motion_sense {
2493         uint8_t cmd;
2494         union {
2495                 /* Used for MOTIONSENSE_CMD_DUMP. */
2496                 struct __ec_todo_unpacked {
2497                         /*
2498                          * Maximal number of sensor the host is expecting.
2499                          * 0 means the host is only interested in the number
2500                          * of sensors controlled by the EC.
2501                          */
2502                         uint8_t max_sensor_count;
2503                 } dump;
2504
2505                 /*
2506                  * Used for MOTIONSENSE_CMD_KB_WAKE_ANGLE.
2507                  */
2508                 struct __ec_todo_unpacked {
2509                         /* Data to set or EC_MOTION_SENSE_NO_VALUE to read.
2510                          * kb_wake_angle: angle to wakup AP.
2511                          */
2512                         int16_t data;
2513                 } kb_wake_angle;
2514
2515                 /*
2516                  * Used for MOTIONSENSE_CMD_INFO, MOTIONSENSE_CMD_DATA
2517                  * and MOTIONSENSE_CMD_PERFORM_CALIB.
2518                  */
2519                 struct __ec_todo_unpacked {
2520                         uint8_t sensor_num;
2521                 } info, info_3, data, fifo_flush, perform_calib,
2522                                 list_activities;
2523
2524                 /*
2525                  * Used for MOTIONSENSE_CMD_EC_RATE, MOTIONSENSE_CMD_SENSOR_ODR
2526                  * and MOTIONSENSE_CMD_SENSOR_RANGE.
2527                  */
2528                 struct __ec_todo_unpacked {
2529                         uint8_t sensor_num;
2530
2531                         /* Rounding flag, true for round-up, false for down. */
2532                         uint8_t roundup;
2533
2534                         uint16_t reserved;
2535
2536                         /* Data to set or EC_MOTION_SENSE_NO_VALUE to read. */
2537                         int32_t data;
2538                 } ec_rate, sensor_odr, sensor_range;
2539
2540                 /* Used for MOTIONSENSE_CMD_SENSOR_OFFSET */
2541                 struct __ec_todo_packed {
2542                         uint8_t sensor_num;
2543
2544                         /*
2545                          * bit 0: If set (MOTION_SENSE_SET_OFFSET), set
2546                          * the calibration information in the EC.
2547                          * If unset, just retrieve calibration information.
2548                          */
2549                         uint16_t flags;
2550
2551                         /*
2552                          * Temperature at calibration, in units of 0.01 C
2553                          * 0x8000: invalid / unknown.
2554                          * 0x0: 0C
2555                          * 0x7fff: +327.67C
2556                          */
2557                         int16_t temp;
2558
2559                         /*
2560                          * Offset for calibration.
2561                          * Unit:
2562                          * Accelerometer: 1/1024 g
2563                          * Gyro:          1/1024 deg/s
2564                          * Compass:       1/16 uT
2565                          */
2566                         int16_t offset[3];
2567                 } sensor_offset;
2568
2569                 /* Used for MOTIONSENSE_CMD_SENSOR_SCALE */
2570                 struct __ec_todo_packed {
2571                         uint8_t sensor_num;
2572
2573                         /*
2574                          * bit 0: If set (MOTION_SENSE_SET_OFFSET), set
2575                          * the calibration information in the EC.
2576                          * If unset, just retrieve calibration information.
2577                          */
2578                         uint16_t flags;
2579
2580                         /*
2581                          * Temperature at calibration, in units of 0.01 C
2582                          * 0x8000: invalid / unknown.
2583                          * 0x0: 0C
2584                          * 0x7fff: +327.67C
2585                          */
2586                         int16_t temp;
2587
2588                         /*
2589                          * Scale for calibration:
2590                          * By default scale is 1, it is encoded on 16bits:
2591                          * 1 = BIT(15)
2592                          * ~2 = 0xFFFF
2593                          * ~0 = 0.
2594                          */
2595                         uint16_t scale[3];
2596                 } sensor_scale;
2597
2598
2599                 /* Used for MOTIONSENSE_CMD_FIFO_INFO */
2600                 /* (no params) */
2601
2602                 /* Used for MOTIONSENSE_CMD_FIFO_READ */
2603                 struct __ec_todo_unpacked {
2604                         /*
2605                          * Number of expected vector to return.
2606                          * EC may return less or 0 if none available.
2607                          */
2608                         uint32_t max_data_vector;
2609                 } fifo_read;
2610
2611                 struct ec_motion_sense_activity set_activity;
2612
2613                 /* Used for MOTIONSENSE_CMD_LID_ANGLE */
2614                 /* (no params) */
2615
2616                 /* Used for MOTIONSENSE_CMD_FIFO_INT_ENABLE */
2617                 struct __ec_todo_unpacked {
2618                         /*
2619                          * 1: enable, 0 disable fifo,
2620                          * EC_MOTION_SENSE_NO_VALUE return value.
2621                          */
2622                         int8_t enable;
2623                 } fifo_int_enable;
2624
2625                 /* Used for MOTIONSENSE_CMD_SPOOF */
2626                 struct __ec_todo_packed {
2627                         uint8_t sensor_id;
2628
2629                         /* See enum motionsense_spoof_mode. */
2630                         uint8_t spoof_enable;
2631
2632                         /* Ignored, used for alignment. */
2633                         uint8_t reserved;
2634
2635                         /* Individual component values to spoof. */
2636                         int16_t components[3];
2637                 } spoof;
2638
2639                 /* Used for MOTIONSENSE_CMD_TABLET_MODE_LID_ANGLE. */
2640                 struct __ec_todo_unpacked {
2641                         /*
2642                          * Lid angle threshold for switching between tablet and
2643                          * clamshell mode.
2644                          */
2645                         int16_t lid_angle;
2646
2647                         /*
2648                          * Hysteresis degree to prevent fluctuations between
2649                          * clamshell and tablet mode if lid angle keeps
2650                          * changing around the threshold. Lid motion driver will
2651                          * use lid_angle + hys_degree to trigger tablet mode and
2652                          * lid_angle - hys_degree to trigger clamshell mode.
2653                          */
2654                         int16_t hys_degree;
2655                 } tablet_mode_threshold;
2656         };
2657 } __ec_todo_packed;
2658
2659 struct ec_response_motion_sense {
2660         union {
2661                 /* Used for MOTIONSENSE_CMD_DUMP */
2662                 struct __ec_todo_unpacked {
2663                         /* Flags representing the motion sensor module. */
2664                         uint8_t module_flags;
2665
2666                         /* Number of sensors managed directly by the EC. */
2667                         uint8_t sensor_count;
2668
2669                         /*
2670                          * Sensor data is truncated if response_max is too small
2671                          * for holding all the data.
2672                          */
2673                         struct ec_response_motion_sensor_data sensor[0];
2674                 } dump;
2675
2676                 /* Used for MOTIONSENSE_CMD_INFO. */
2677                 struct __ec_todo_unpacked {
2678                         /* Should be element of enum motionsensor_type. */
2679                         uint8_t type;
2680
2681                         /* Should be element of enum motionsensor_location. */
2682                         uint8_t location;
2683
2684                         /* Should be element of enum motionsensor_chip. */
2685                         uint8_t chip;
2686                 } info;
2687
2688                 /* Used for MOTIONSENSE_CMD_INFO version 3 */
2689                 struct __ec_todo_unpacked {
2690                         /* Should be element of enum motionsensor_type. */
2691                         uint8_t type;
2692
2693                         /* Should be element of enum motionsensor_location. */
2694                         uint8_t location;
2695
2696                         /* Should be element of enum motionsensor_chip. */
2697                         uint8_t chip;
2698
2699                         /* Minimum sensor sampling frequency */
2700                         uint32_t min_frequency;
2701
2702                         /* Maximum sensor sampling frequency */
2703                         uint32_t max_frequency;
2704
2705                         /* Max number of sensor events that could be in fifo */
2706                         uint32_t fifo_max_event_count;
2707                 } info_3;
2708
2709                 /* Used for MOTIONSENSE_CMD_DATA */
2710                 struct ec_response_motion_sensor_data data;
2711
2712                 /*
2713                  * Used for MOTIONSENSE_CMD_EC_RATE, MOTIONSENSE_CMD_SENSOR_ODR,
2714                  * MOTIONSENSE_CMD_SENSOR_RANGE,
2715                  * MOTIONSENSE_CMD_KB_WAKE_ANGLE,
2716                  * MOTIONSENSE_CMD_FIFO_INT_ENABLE and
2717                  * MOTIONSENSE_CMD_SPOOF.
2718                  */
2719                 struct __ec_todo_unpacked {
2720                         /* Current value of the parameter queried. */
2721                         int32_t ret;
2722                 } ec_rate, sensor_odr, sensor_range, kb_wake_angle,
2723                   fifo_int_enable, spoof;
2724
2725                 /*
2726                  * Used for MOTIONSENSE_CMD_SENSOR_OFFSET,
2727                  * PERFORM_CALIB.
2728                  */
2729                 struct __ec_todo_unpacked  {
2730                         int16_t temp;
2731                         int16_t offset[3];
2732                 } sensor_offset, perform_calib;
2733
2734                 /* Used for MOTIONSENSE_CMD_SENSOR_SCALE */
2735                 struct __ec_todo_unpacked  {
2736                         int16_t temp;
2737                         uint16_t scale[3];
2738                 } sensor_scale;
2739
2740                 struct ec_response_motion_sense_fifo_info fifo_info, fifo_flush;
2741
2742                 struct ec_response_motion_sense_fifo_data fifo_read;
2743
2744                 struct __ec_todo_packed {
2745                         uint16_t reserved;
2746                         uint32_t enabled;
2747                         uint32_t disabled;
2748                 } list_activities;
2749
2750                 /* No params for set activity */
2751
2752                 /* Used for MOTIONSENSE_CMD_LID_ANGLE */
2753                 struct __ec_todo_unpacked {
2754                         /*
2755                          * Angle between 0 and 360 degree if available,
2756                          * LID_ANGLE_UNRELIABLE otherwise.
2757                          */
2758                         uint16_t value;
2759                 } lid_angle;
2760
2761                 /* Used for MOTIONSENSE_CMD_TABLET_MODE_LID_ANGLE. */
2762                 struct __ec_todo_unpacked {
2763                         /*
2764                          * Lid angle threshold for switching between tablet and
2765                          * clamshell mode.
2766                          */
2767                         uint16_t lid_angle;
2768
2769                         /* Hysteresis degree. */
2770                         uint16_t hys_degree;
2771                 } tablet_mode_threshold;
2772
2773         };
2774 } __ec_todo_packed;
2775
2776 /*****************************************************************************/
2777 /* Force lid open command */
2778
2779 /* Make lid event always open */
2780 #define EC_CMD_FORCE_LID_OPEN 0x002C
2781
2782 struct ec_params_force_lid_open {
2783         uint8_t enabled;
2784 } __ec_align1;
2785
2786 /*****************************************************************************/
2787 /* Configure the behavior of the power button */
2788 #define EC_CMD_CONFIG_POWER_BUTTON 0x002D
2789
2790 enum ec_config_power_button_flags {
2791         /* Enable/Disable power button pulses for x86 devices */
2792         EC_POWER_BUTTON_ENABLE_PULSE = BIT(0),
2793 };
2794
2795 struct ec_params_config_power_button {
2796         /* See enum ec_config_power_button_flags */
2797         uint8_t flags;
2798 } __ec_align1;
2799
2800 /*****************************************************************************/
2801 /* USB charging control commands */
2802
2803 /* Set USB port charging mode */
2804 #define EC_CMD_USB_CHARGE_SET_MODE 0x0030
2805
2806 struct ec_params_usb_charge_set_mode {
2807         uint8_t usb_port_id;
2808         uint8_t mode:7;
2809         uint8_t inhibit_charge:1;
2810 } __ec_align1;
2811
2812 /*****************************************************************************/
2813 /* Persistent storage for host */
2814
2815 /* Maximum bytes that can be read/written in a single command */
2816 #define EC_PSTORE_SIZE_MAX 64
2817
2818 /* Get persistent storage info */
2819 #define EC_CMD_PSTORE_INFO 0x0040
2820
2821 struct ec_response_pstore_info {
2822         /* Persistent storage size, in bytes */
2823         uint32_t pstore_size;
2824         /* Access size; read/write offset and size must be a multiple of this */
2825         uint32_t access_size;
2826 } __ec_align4;
2827
2828 /*
2829  * Read persistent storage
2830  *
2831  * Response is params.size bytes of data.
2832  */
2833 #define EC_CMD_PSTORE_READ 0x0041
2834
2835 struct ec_params_pstore_read {
2836         uint32_t offset;   /* Byte offset to read */
2837         uint32_t size;     /* Size to read in bytes */
2838 } __ec_align4;
2839
2840 /* Write persistent storage */
2841 #define EC_CMD_PSTORE_WRITE 0x0042
2842
2843 struct ec_params_pstore_write {
2844         uint32_t offset;   /* Byte offset to write */
2845         uint32_t size;     /* Size to write in bytes */
2846         uint8_t data[EC_PSTORE_SIZE_MAX];
2847 } __ec_align4;
2848
2849 /*****************************************************************************/
2850 /* Real-time clock */
2851
2852 /* RTC params and response structures */
2853 struct ec_params_rtc {
2854         uint32_t time;
2855 } __ec_align4;
2856
2857 struct ec_response_rtc {
2858         uint32_t time;
2859 } __ec_align4;
2860
2861 /* These use ec_response_rtc */
2862 #define EC_CMD_RTC_GET_VALUE 0x0044
2863 #define EC_CMD_RTC_GET_ALARM 0x0045
2864
2865 /* These all use ec_params_rtc */
2866 #define EC_CMD_RTC_SET_VALUE 0x0046
2867 #define EC_CMD_RTC_SET_ALARM 0x0047
2868
2869 /* Pass as time param to SET_ALARM to clear the current alarm */
2870 #define EC_RTC_ALARM_CLEAR 0
2871
2872 /*****************************************************************************/
2873 /* Port80 log access */
2874
2875 /* Maximum entries that can be read/written in a single command */
2876 #define EC_PORT80_SIZE_MAX 32
2877
2878 /* Get last port80 code from previous boot */
2879 #define EC_CMD_PORT80_LAST_BOOT 0x0048
2880 #define EC_CMD_PORT80_READ 0x0048
2881
2882 enum ec_port80_subcmd {
2883         EC_PORT80_GET_INFO = 0,
2884         EC_PORT80_READ_BUFFER,
2885 };
2886
2887 struct ec_params_port80_read {
2888         uint16_t subcmd;
2889         union {
2890                 struct __ec_todo_unpacked {
2891                         uint32_t offset;
2892                         uint32_t num_entries;
2893                 } read_buffer;
2894         };
2895 } __ec_todo_packed;
2896
2897 struct ec_response_port80_read {
2898         union {
2899                 struct __ec_todo_unpacked {
2900                         uint32_t writes;
2901                         uint32_t history_size;
2902                         uint32_t last_boot;
2903                 } get_info;
2904                 struct __ec_todo_unpacked {
2905                         uint16_t codes[EC_PORT80_SIZE_MAX];
2906                 } data;
2907         };
2908 } __ec_todo_packed;
2909
2910 struct ec_response_port80_last_boot {
2911         uint16_t code;
2912 } __ec_align2;
2913
2914 /*****************************************************************************/
2915 /* Temporary secure storage for host verified boot use */
2916
2917 /* Number of bytes in a vstore slot */
2918 #define EC_VSTORE_SLOT_SIZE 64
2919
2920 /* Maximum number of vstore slots */
2921 #define EC_VSTORE_SLOT_MAX 32
2922
2923 /* Get persistent storage info */
2924 #define EC_CMD_VSTORE_INFO 0x0049
2925 struct ec_response_vstore_info {
2926         /* Indicates which slots are locked */
2927         uint32_t slot_locked;
2928         /* Total number of slots available */
2929         uint8_t slot_count;
2930 } __ec_align_size1;
2931
2932 /*
2933  * Read temporary secure storage
2934  *
2935  * Response is EC_VSTORE_SLOT_SIZE bytes of data.
2936  */
2937 #define EC_CMD_VSTORE_READ 0x004A
2938
2939 struct ec_params_vstore_read {
2940         uint8_t slot; /* Slot to read from */
2941 } __ec_align1;
2942
2943 struct ec_response_vstore_read {
2944         uint8_t data[EC_VSTORE_SLOT_SIZE];
2945 } __ec_align1;
2946
2947 /*
2948  * Write temporary secure storage and lock it.
2949  */
2950 #define EC_CMD_VSTORE_WRITE 0x004B
2951
2952 struct ec_params_vstore_write {
2953         uint8_t slot; /* Slot to write to */
2954         uint8_t data[EC_VSTORE_SLOT_SIZE];
2955 } __ec_align1;
2956
2957 /*****************************************************************************/
2958 /* Thermal engine commands. Note that there are two implementations. We'll
2959  * reuse the command number, but the data and behavior is incompatible.
2960  * Version 0 is what originally shipped on Link.
2961  * Version 1 separates the CPU thermal limits from the fan control.
2962  */
2963
2964 #define EC_CMD_THERMAL_SET_THRESHOLD 0x0050
2965 #define EC_CMD_THERMAL_GET_THRESHOLD 0x0051
2966
2967 /* The version 0 structs are opaque. You have to know what they are for
2968  * the get/set commands to make any sense.
2969  */
2970
2971 /* Version 0 - set */
2972 struct ec_params_thermal_set_threshold {
2973         uint8_t sensor_type;
2974         uint8_t threshold_id;
2975         uint16_t value;
2976 } __ec_align2;
2977
2978 /* Version 0 - get */
2979 struct ec_params_thermal_get_threshold {
2980         uint8_t sensor_type;
2981         uint8_t threshold_id;
2982 } __ec_align1;
2983
2984 struct ec_response_thermal_get_threshold {
2985         uint16_t value;
2986 } __ec_align2;
2987
2988
2989 /* The version 1 structs are visible. */
2990 enum ec_temp_thresholds {
2991         EC_TEMP_THRESH_WARN = 0,
2992         EC_TEMP_THRESH_HIGH,
2993         EC_TEMP_THRESH_HALT,
2994
2995         EC_TEMP_THRESH_COUNT
2996 };
2997
2998 /*
2999  * Thermal configuration for one temperature sensor. Temps are in degrees K.
3000  * Zero values will be silently ignored by the thermal task.
3001  *
3002  * Set 'temp_host' value allows thermal task to trigger some event with 1 degree
3003  * hysteresis.
3004  * For example,
3005  *      temp_host[EC_TEMP_THRESH_HIGH] = 300 K
3006  *      temp_host_release[EC_TEMP_THRESH_HIGH] = 0 K
3007  * EC will throttle ap when temperature >= 301 K, and release throttling when
3008  * temperature <= 299 K.
3009  *
3010  * Set 'temp_host_release' value allows thermal task has a custom hysteresis.
3011  * For example,
3012  *      temp_host[EC_TEMP_THRESH_HIGH] = 300 K
3013  *      temp_host_release[EC_TEMP_THRESH_HIGH] = 295 K
3014  * EC will throttle ap when temperature >= 301 K, and release throttling when
3015  * temperature <= 294 K.
3016  *
3017  * Note that this structure is a sub-structure of
3018  * ec_params_thermal_set_threshold_v1, but maintains its alignment there.
3019  */
3020 struct ec_thermal_config {
3021         uint32_t temp_host[EC_TEMP_THRESH_COUNT]; /* levels of hotness */
3022         uint32_t temp_host_release[EC_TEMP_THRESH_COUNT]; /* release levels */
3023         uint32_t temp_fan_off;          /* no active cooling needed */
3024         uint32_t temp_fan_max;          /* max active cooling needed */
3025 } __ec_align4;
3026
3027 /* Version 1 - get config for one sensor. */
3028 struct ec_params_thermal_get_threshold_v1 {
3029         uint32_t sensor_num;
3030 } __ec_align4;
3031 /* This returns a struct ec_thermal_config */
3032
3033 /*
3034  * Version 1 - set config for one sensor.
3035  * Use read-modify-write for best results!
3036  */
3037 struct ec_params_thermal_set_threshold_v1 {
3038         uint32_t sensor_num;
3039         struct ec_thermal_config cfg;
3040 } __ec_align4;
3041 /* This returns no data */
3042
3043 /****************************************************************************/
3044
3045 /* Toggle automatic fan control */
3046 #define EC_CMD_THERMAL_AUTO_FAN_CTRL 0x0052
3047
3048 /* Version 1 of input params */
3049 struct ec_params_auto_fan_ctrl_v1 {
3050         uint8_t fan_idx;
3051 } __ec_align1;
3052
3053 /* Get/Set TMP006 calibration data */
3054 #define EC_CMD_TMP006_GET_CALIBRATION 0x0053
3055 #define EC_CMD_TMP006_SET_CALIBRATION 0x0054
3056
3057 /*
3058  * The original TMP006 calibration only needed four params, but now we need
3059  * more. Since the algorithm is nothing but magic numbers anyway, we'll leave
3060  * the params opaque. The v1 "get" response will include the algorithm number
3061  * and how many params it requires. That way we can change the EC code without
3062  * needing to update this file. We can also use a different algorithm on each
3063  * sensor.
3064  */
3065
3066 /* This is the same struct for both v0 and v1. */
3067 struct ec_params_tmp006_get_calibration {
3068         uint8_t index;
3069 } __ec_align1;
3070
3071 /* Version 0 */
3072 struct ec_response_tmp006_get_calibration_v0 {
3073         float s0;
3074         float b0;
3075         float b1;
3076         float b2;
3077 } __ec_align4;
3078
3079 struct ec_params_tmp006_set_calibration_v0 {
3080         uint8_t index;
3081         uint8_t reserved[3];
3082         float s0;
3083         float b0;
3084         float b1;
3085         float b2;
3086 } __ec_align4;
3087
3088 /* Version 1 */
3089 struct ec_response_tmp006_get_calibration_v1 {
3090         uint8_t algorithm;
3091         uint8_t num_params;
3092         uint8_t reserved[2];
3093         float val[0];
3094 } __ec_align4;
3095
3096 struct ec_params_tmp006_set_calibration_v1 {
3097         uint8_t index;
3098         uint8_t algorithm;
3099         uint8_t num_params;
3100         uint8_t reserved;
3101         float val[0];
3102 } __ec_align4;
3103
3104
3105 /* Read raw TMP006 data */
3106 #define EC_CMD_TMP006_GET_RAW 0x0055
3107
3108 struct ec_params_tmp006_get_raw {
3109         uint8_t index;
3110 } __ec_align1;
3111
3112 struct ec_response_tmp006_get_raw {
3113         int32_t t;  /* In 1/100 K */
3114         int32_t v;  /* In nV */
3115 } __ec_align4;
3116
3117 /*****************************************************************************/
3118 /* MKBP - Matrix KeyBoard Protocol */
3119
3120 /*
3121  * Read key state
3122  *
3123  * Returns raw data for keyboard cols; see ec_response_mkbp_info.cols for
3124  * expected response size.
3125  *
3126  * NOTE: This has been superseded by EC_CMD_MKBP_GET_NEXT_EVENT.  If you wish
3127  * to obtain the instantaneous state, use EC_CMD_MKBP_INFO with the type
3128  * EC_MKBP_INFO_CURRENT and event EC_MKBP_EVENT_KEY_MATRIX.
3129  */
3130 #define EC_CMD_MKBP_STATE 0x0060
3131
3132 /*
3133  * Provide information about various MKBP things.  See enum ec_mkbp_info_type.
3134  */
3135 #define EC_CMD_MKBP_INFO 0x0061
3136
3137 struct ec_response_mkbp_info {
3138         uint32_t rows;
3139         uint32_t cols;
3140         /* Formerly "switches", which was 0. */
3141         uint8_t reserved;
3142 } __ec_align_size1;
3143
3144 struct ec_params_mkbp_info {
3145         uint8_t info_type;
3146         uint8_t event_type;
3147 } __ec_align1;
3148
3149 enum ec_mkbp_info_type {
3150         /*
3151          * Info about the keyboard matrix: number of rows and columns.
3152          *
3153          * Returns struct ec_response_mkbp_info.
3154          */
3155         EC_MKBP_INFO_KBD = 0,
3156
3157         /*
3158          * For buttons and switches, info about which specifically are
3159          * supported.  event_type must be set to one of the values in enum
3160          * ec_mkbp_event.
3161          *
3162          * For EC_MKBP_EVENT_BUTTON and EC_MKBP_EVENT_SWITCH, returns a 4 byte
3163          * bitmask indicating which buttons or switches are present.  See the
3164          * bit inidices below.
3165          */
3166         EC_MKBP_INFO_SUPPORTED = 1,
3167
3168         /*
3169          * Instantaneous state of buttons and switches.
3170          *
3171          * event_type must be set to one of the values in enum ec_mkbp_event.
3172          *
3173          * For EC_MKBP_EVENT_KEY_MATRIX, returns uint8_t key_matrix[13]
3174          * indicating the current state of the keyboard matrix.
3175          *
3176          * For EC_MKBP_EVENT_HOST_EVENT, return uint32_t host_event, the raw
3177          * event state.
3178          *
3179          * For EC_MKBP_EVENT_BUTTON, returns uint32_t buttons, indicating the
3180          * state of supported buttons.
3181          *
3182          * For EC_MKBP_EVENT_SWITCH, returns uint32_t switches, indicating the
3183          * state of supported switches.
3184          */
3185         EC_MKBP_INFO_CURRENT = 2,
3186 };
3187
3188 /* Simulate key press */
3189 #define EC_CMD_MKBP_SIMULATE_KEY 0x0062
3190
3191 struct ec_params_mkbp_simulate_key {
3192         uint8_t col;
3193         uint8_t row;
3194         uint8_t pressed;
3195 } __ec_align1;
3196
3197 #define EC_CMD_GET_KEYBOARD_ID 0x0063
3198
3199 struct ec_response_keyboard_id {
3200         uint32_t keyboard_id;
3201 } __ec_align4;
3202
3203 enum keyboard_id {
3204         KEYBOARD_ID_UNSUPPORTED = 0,
3205         KEYBOARD_ID_UNREADABLE = 0xffffffff,
3206 };
3207
3208 /* Configure keyboard scanning */
3209 #define EC_CMD_MKBP_SET_CONFIG 0x0064
3210 #define EC_CMD_MKBP_GET_CONFIG 0x0065
3211
3212 /* flags */
3213 enum mkbp_config_flags {
3214         EC_MKBP_FLAGS_ENABLE = 1,       /* Enable keyboard scanning */
3215 };
3216
3217 enum mkbp_config_valid {
3218         EC_MKBP_VALID_SCAN_PERIOD               = BIT(0),
3219         EC_MKBP_VALID_POLL_TIMEOUT              = BIT(1),
3220         EC_MKBP_VALID_MIN_POST_SCAN_DELAY       = BIT(3),
3221         EC_MKBP_VALID_OUTPUT_SETTLE             = BIT(4),
3222         EC_MKBP_VALID_DEBOUNCE_DOWN             = BIT(5),
3223         EC_MKBP_VALID_DEBOUNCE_UP               = BIT(6),
3224         EC_MKBP_VALID_FIFO_MAX_DEPTH            = BIT(7),
3225 };
3226
3227 /*
3228  * Configuration for our key scanning algorithm.
3229  *
3230  * Note that this is used as a sub-structure of
3231  * ec_{params/response}_mkbp_get_config.
3232  */
3233 struct ec_mkbp_config {
3234         uint32_t valid_mask;            /* valid fields */
3235         uint8_t flags;          /* some flags (enum mkbp_config_flags) */
3236         uint8_t valid_flags;            /* which flags are valid */
3237         uint16_t scan_period_us;        /* period between start of scans */
3238         /* revert to interrupt mode after no activity for this long */
3239         uint32_t poll_timeout_us;
3240         /*
3241          * minimum post-scan relax time. Once we finish a scan we check
3242          * the time until we are due to start the next one. If this time is
3243          * shorter this field, we use this instead.
3244          */
3245         uint16_t min_post_scan_delay_us;
3246         /* delay between setting up output and waiting for it to settle */
3247         uint16_t output_settle_us;
3248         uint16_t debounce_down_us;      /* time for debounce on key down */
3249         uint16_t debounce_up_us;        /* time for debounce on key up */
3250         /* maximum depth to allow for fifo (0 = no keyscan output) */
3251         uint8_t fifo_max_depth;
3252 } __ec_align_size1;
3253
3254 struct ec_params_mkbp_set_config {
3255         struct ec_mkbp_config config;
3256 } __ec_align_size1;
3257
3258 struct ec_response_mkbp_get_config {
3259         struct ec_mkbp_config config;
3260 } __ec_align_size1;
3261
3262 /* Run the key scan emulation */
3263 #define EC_CMD_KEYSCAN_SEQ_CTRL 0x0066
3264
3265 enum ec_keyscan_seq_cmd {
3266         EC_KEYSCAN_SEQ_STATUS = 0,      /* Get status information */
3267         EC_KEYSCAN_SEQ_CLEAR = 1,       /* Clear sequence */
3268         EC_KEYSCAN_SEQ_ADD = 2,         /* Add item to sequence */
3269         EC_KEYSCAN_SEQ_START = 3,       /* Start running sequence */
3270         EC_KEYSCAN_SEQ_COLLECT = 4,     /* Collect sequence summary data */
3271 };
3272
3273 enum ec_collect_flags {
3274         /*
3275          * Indicates this scan was processed by the EC. Due to timing, some
3276          * scans may be skipped.
3277          */
3278         EC_KEYSCAN_SEQ_FLAG_DONE        = BIT(0),
3279 };
3280
3281 struct ec_collect_item {
3282         uint8_t flags;          /* some flags (enum ec_collect_flags) */
3283 } __ec_align1;
3284
3285 struct ec_params_keyscan_seq_ctrl {
3286         uint8_t cmd;    /* Command to send (enum ec_keyscan_seq_cmd) */
3287         union {
3288                 struct __ec_align1 {
3289                         uint8_t active;         /* still active */
3290                         uint8_t num_items;      /* number of items */
3291                         /* Current item being presented */
3292                         uint8_t cur_item;
3293                 } status;
3294                 struct __ec_todo_unpacked {
3295                         /*
3296                          * Absolute time for this scan, measured from the
3297                          * start of the sequence.
3298                          */
3299                         uint32_t time_us;
3300                         uint8_t scan[0];        /* keyscan data */
3301                 } add;
3302                 struct __ec_align1 {
3303                         uint8_t start_item;     /* First item to return */
3304                         uint8_t num_items;      /* Number of items to return */
3305                 } collect;
3306         };
3307 } __ec_todo_packed;
3308
3309 struct ec_result_keyscan_seq_ctrl {
3310         union {
3311                 struct __ec_todo_unpacked {
3312                         uint8_t num_items;      /* Number of items */
3313                         /* Data for each item */
3314                         struct ec_collect_item item[0];
3315                 } collect;
3316         };
3317 } __ec_todo_packed;
3318
3319 /*
3320  * Get the next pending MKBP event.
3321  *
3322  * Returns EC_RES_UNAVAILABLE if there is no event pending.
3323  */
3324 #define EC_CMD_GET_NEXT_EVENT 0x0067
3325
3326 #define EC_MKBP_HAS_MORE_EVENTS_SHIFT 7
3327
3328 /*
3329  * We use the most significant bit of the event type to indicate to the host
3330  * that the EC has more MKBP events available to provide.
3331  */
3332 #define EC_MKBP_HAS_MORE_EVENTS BIT(EC_MKBP_HAS_MORE_EVENTS_SHIFT)
3333
3334 /* The mask to apply to get the raw event type */
3335 #define EC_MKBP_EVENT_TYPE_MASK (BIT(EC_MKBP_HAS_MORE_EVENTS_SHIFT) - 1)
3336
3337 enum ec_mkbp_event {
3338         /* Keyboard matrix changed. The event data is the new matrix state. */
3339         EC_MKBP_EVENT_KEY_MATRIX = 0,
3340
3341         /* New host event. The event data is 4 bytes of host event flags. */
3342         EC_MKBP_EVENT_HOST_EVENT = 1,
3343
3344         /* New Sensor FIFO data. The event data is fifo_info structure. */
3345         EC_MKBP_EVENT_SENSOR_FIFO = 2,
3346
3347         /* The state of the non-matrixed buttons have changed. */
3348         EC_MKBP_EVENT_BUTTON = 3,
3349
3350         /* The state of the switches have changed. */
3351         EC_MKBP_EVENT_SWITCH = 4,
3352
3353         /* New Fingerprint sensor event, the event data is fp_events bitmap. */
3354         EC_MKBP_EVENT_FINGERPRINT = 5,
3355
3356         /*
3357          * Sysrq event: send emulated sysrq. The event data is sysrq,
3358          * corresponding to the key to be pressed.
3359          */
3360         EC_MKBP_EVENT_SYSRQ = 6,
3361
3362         /*
3363          * New 64-bit host event.
3364          * The event data is 8 bytes of host event flags.
3365          */
3366         EC_MKBP_EVENT_HOST_EVENT64 = 7,
3367
3368         /* Notify the AP that something happened on CEC */
3369         EC_MKBP_EVENT_CEC_EVENT = 8,
3370
3371         /* Send an incoming CEC message to the AP */
3372         EC_MKBP_EVENT_CEC_MESSAGE = 9,
3373
3374         /* Number of MKBP events */
3375         EC_MKBP_EVENT_COUNT,
3376 };
3377 BUILD_ASSERT(EC_MKBP_EVENT_COUNT <= EC_MKBP_EVENT_TYPE_MASK);
3378
3379 union __ec_align_offset1 ec_response_get_next_data {
3380         uint8_t key_matrix[13];
3381
3382         /* Unaligned */
3383         uint32_t host_event;
3384         uint64_t host_event64;
3385
3386         struct __ec_todo_unpacked {
3387                 /* For aligning the fifo_info */
3388                 uint8_t reserved[3];
3389                 struct ec_response_motion_sense_fifo_info info;
3390         } sensor_fifo;
3391
3392         uint32_t buttons;
3393
3394         uint32_t switches;
3395
3396         uint32_t fp_events;
3397
3398         uint32_t sysrq;
3399
3400         /* CEC events from enum mkbp_cec_event */
3401         uint32_t cec_events;
3402 };
3403
3404 union __ec_align_offset1 ec_response_get_next_data_v1 {
3405         uint8_t key_matrix[16];
3406
3407         /* Unaligned */
3408         uint32_t host_event;
3409         uint64_t host_event64;
3410
3411         struct __ec_todo_unpacked {
3412                 /* For aligning the fifo_info */
3413                 uint8_t reserved[3];
3414                 struct ec_response_motion_sense_fifo_info info;
3415         } sensor_fifo;
3416
3417         uint32_t buttons;
3418
3419         uint32_t switches;
3420
3421         uint32_t fp_events;
3422
3423         uint32_t sysrq;
3424
3425         /* CEC events from enum mkbp_cec_event */
3426         uint32_t cec_events;
3427
3428         uint8_t cec_message[16];
3429 };
3430 BUILD_ASSERT(sizeof(union ec_response_get_next_data_v1) == 16);
3431
3432 struct ec_response_get_next_event {
3433         uint8_t event_type;
3434         /* Followed by event data if any */
3435         union ec_response_get_next_data data;
3436 } __ec_align1;
3437
3438 struct ec_response_get_next_event_v1 {
3439         uint8_t event_type;
3440         /* Followed by event data if any */
3441         union ec_response_get_next_data_v1 data;
3442 } __ec_align1;
3443
3444 /* Bit indices for buttons and switches.*/
3445 /* Buttons */
3446 #define EC_MKBP_POWER_BUTTON    0
3447 #define EC_MKBP_VOL_UP          1
3448 #define EC_MKBP_VOL_DOWN        2
3449 #define EC_MKBP_RECOVERY        3
3450
3451 /* Switches */
3452 #define EC_MKBP_LID_OPEN        0
3453 #define EC_MKBP_TABLET_MODE     1
3454 #define EC_MKBP_BASE_ATTACHED   2
3455
3456 /* Run keyboard factory test scanning */
3457 #define EC_CMD_KEYBOARD_FACTORY_TEST 0x0068
3458
3459 struct ec_response_keyboard_factory_test {
3460         uint16_t shorted;       /* Keyboard pins are shorted */
3461 } __ec_align2;
3462
3463 /* Fingerprint events in 'fp_events' for EC_MKBP_EVENT_FINGERPRINT */
3464 #define EC_MKBP_FP_RAW_EVENT(fp_events) ((fp_events) & 0x00FFFFFF)
3465 #define EC_MKBP_FP_ERRCODE(fp_events)   ((fp_events) & 0x0000000F)
3466 #define EC_MKBP_FP_ENROLL_PROGRESS_OFFSET 4
3467 #define EC_MKBP_FP_ENROLL_PROGRESS(fpe) (((fpe) & 0x00000FF0) \
3468                                          >> EC_MKBP_FP_ENROLL_PROGRESS_OFFSET)
3469 #define EC_MKBP_FP_MATCH_IDX_OFFSET 12
3470 #define EC_MKBP_FP_MATCH_IDX_MASK 0x0000F000
3471 #define EC_MKBP_FP_MATCH_IDX(fpe) (((fpe) & EC_MKBP_FP_MATCH_IDX_MASK) \
3472                                          >> EC_MKBP_FP_MATCH_IDX_OFFSET)
3473 #define EC_MKBP_FP_ENROLL               BIT(27)
3474 #define EC_MKBP_FP_MATCH                BIT(28)
3475 #define EC_MKBP_FP_FINGER_DOWN          BIT(29)
3476 #define EC_MKBP_FP_FINGER_UP            BIT(30)
3477 #define EC_MKBP_FP_IMAGE_READY          BIT(31)
3478 /* code given by EC_MKBP_FP_ERRCODE() when EC_MKBP_FP_ENROLL is set */
3479 #define EC_MKBP_FP_ERR_ENROLL_OK               0
3480 #define EC_MKBP_FP_ERR_ENROLL_LOW_QUALITY      1
3481 #define EC_MKBP_FP_ERR_ENROLL_IMMOBILE         2
3482 #define EC_MKBP_FP_ERR_ENROLL_LOW_COVERAGE     3
3483 #define EC_MKBP_FP_ERR_ENROLL_INTERNAL         5
3484 /* Can be used to detect if image was usable for enrollment or not. */
3485 #define EC_MKBP_FP_ERR_ENROLL_PROBLEM_MASK     1
3486 /* code given by EC_MKBP_FP_ERRCODE() when EC_MKBP_FP_MATCH is set */
3487 #define EC_MKBP_FP_ERR_MATCH_NO                0
3488 #define EC_MKBP_FP_ERR_MATCH_NO_INTERNAL       6
3489 #define EC_MKBP_FP_ERR_MATCH_NO_TEMPLATES      7
3490 #define EC_MKBP_FP_ERR_MATCH_NO_LOW_QUALITY    2
3491 #define EC_MKBP_FP_ERR_MATCH_NO_LOW_COVERAGE   4
3492 #define EC_MKBP_FP_ERR_MATCH_YES               1
3493 #define EC_MKBP_FP_ERR_MATCH_YES_UPDATED       3
3494 #define EC_MKBP_FP_ERR_MATCH_YES_UPDATE_FAILED 5
3495
3496
3497 /*****************************************************************************/
3498 /* Temperature sensor commands */
3499
3500 /* Read temperature sensor info */
3501 #define EC_CMD_TEMP_SENSOR_GET_INFO 0x0070
3502
3503 struct ec_params_temp_sensor_get_info {
3504         uint8_t id;
3505 } __ec_align1;
3506
3507 struct ec_response_temp_sensor_get_info {
3508         char sensor_name[32];
3509         uint8_t sensor_type;
3510 } __ec_align1;
3511
3512 /*****************************************************************************/
3513
3514 /*
3515  * Note: host commands 0x80 - 0x87 are reserved to avoid conflict with ACPI
3516  * commands accidentally sent to the wrong interface.  See the ACPI section
3517  * below.
3518  */
3519
3520 /*****************************************************************************/
3521 /* Host event commands */
3522
3523
3524 /* Obsolete. New implementation should use EC_CMD_HOST_EVENT instead */
3525 /*
3526  * Host event mask params and response structures, shared by all of the host
3527  * event commands below.
3528  */
3529 struct ec_params_host_event_mask {
3530         uint32_t mask;
3531 } __ec_align4;
3532
3533 struct ec_response_host_event_mask {
3534         uint32_t mask;
3535 } __ec_align4;
3536
3537 /* These all use ec_response_host_event_mask */
3538 #define EC_CMD_HOST_EVENT_GET_B         0x0087
3539 #define EC_CMD_HOST_EVENT_GET_SMI_MASK  0x0088
3540 #define EC_CMD_HOST_EVENT_GET_SCI_MASK  0x0089
3541 #define EC_CMD_HOST_EVENT_GET_WAKE_MASK 0x008D
3542
3543 /* These all use ec_params_host_event_mask */
3544 #define EC_CMD_HOST_EVENT_SET_SMI_MASK  0x008A
3545 #define EC_CMD_HOST_EVENT_SET_SCI_MASK  0x008B
3546 #define EC_CMD_HOST_EVENT_CLEAR         0x008C
3547 #define EC_CMD_HOST_EVENT_SET_WAKE_MASK 0x008E
3548 #define EC_CMD_HOST_EVENT_CLEAR_B       0x008F
3549
3550 /*
3551  * Unified host event programming interface - Should be used by newer versions
3552  * of BIOS/OS to program host events and masks
3553  */
3554
3555 struct ec_params_host_event {
3556
3557         /* Action requested by host - one of enum ec_host_event_action. */
3558         uint8_t action;
3559
3560         /*
3561          * Mask type that the host requested the action on - one of
3562          * enum ec_host_event_mask_type.
3563          */
3564         uint8_t mask_type;
3565
3566         /* Set to 0, ignore on read */
3567         uint16_t reserved;
3568
3569         /* Value to be used in case of set operations. */
3570         uint64_t value;
3571 } __ec_align4;
3572
3573 /*
3574  * Response structure returned by EC_CMD_HOST_EVENT.
3575  * Update the value on a GET request. Set to 0 on GET/CLEAR
3576  */
3577
3578 struct ec_response_host_event {
3579
3580         /* Mask value in case of get operation */
3581         uint64_t value;
3582 } __ec_align4;
3583
3584 enum ec_host_event_action {
3585         /*
3586          * params.value is ignored. Value of mask_type populated
3587          * in response.value
3588          */
3589         EC_HOST_EVENT_GET,
3590
3591         /* Bits in params.value are set */
3592         EC_HOST_EVENT_SET,
3593
3594         /* Bits in params.value are cleared */
3595         EC_HOST_EVENT_CLEAR,
3596 };
3597
3598 enum ec_host_event_mask_type {
3599
3600         /* Main host event copy */
3601         EC_HOST_EVENT_MAIN,
3602
3603         /* Copy B of host events */
3604         EC_HOST_EVENT_B,
3605
3606         /* SCI Mask */
3607         EC_HOST_EVENT_SCI_MASK,
3608
3609         /* SMI Mask */
3610         EC_HOST_EVENT_SMI_MASK,
3611
3612         /* Mask of events that should be always reported in hostevents */
3613         EC_HOST_EVENT_ALWAYS_REPORT_MASK,
3614
3615         /* Active wake mask */
3616         EC_HOST_EVENT_ACTIVE_WAKE_MASK,
3617
3618         /* Lazy wake mask for S0ix */
3619         EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX,
3620
3621         /* Lazy wake mask for S3 */
3622         EC_HOST_EVENT_LAZY_WAKE_MASK_S3,
3623
3624         /* Lazy wake mask for S5 */
3625         EC_HOST_EVENT_LAZY_WAKE_MASK_S5,
3626 };
3627
3628 #define EC_CMD_HOST_EVENT       0x00A4
3629
3630 /*****************************************************************************/
3631 /* Switch commands */
3632
3633 /* Enable/disable LCD backlight */
3634 #define EC_CMD_SWITCH_ENABLE_BKLIGHT 0x0090
3635
3636 struct ec_params_switch_enable_backlight {
3637         uint8_t enabled;
3638 } __ec_align1;
3639
3640 /* Enable/disable WLAN/Bluetooth */
3641 #define EC_CMD_SWITCH_ENABLE_WIRELESS 0x0091
3642 #define EC_VER_SWITCH_ENABLE_WIRELESS 1
3643
3644 /* Version 0 params; no response */
3645 struct ec_params_switch_enable_wireless_v0 {
3646         uint8_t enabled;
3647 } __ec_align1;
3648
3649 /* Version 1 params */
3650 struct ec_params_switch_enable_wireless_v1 {
3651         /* Flags to enable now */
3652         uint8_t now_flags;
3653
3654         /* Which flags to copy from now_flags */
3655         uint8_t now_mask;
3656
3657         /*
3658          * Flags to leave enabled in S3, if they're on at the S0->S3
3659          * transition.  (Other flags will be disabled by the S0->S3
3660          * transition.)
3661          */
3662         uint8_t suspend_flags;
3663
3664         /* Which flags to copy from suspend_flags */
3665         uint8_t suspend_mask;
3666 } __ec_align1;
3667
3668 /* Version 1 response */
3669 struct ec_response_switch_enable_wireless_v1 {
3670         /* Flags to enable now */
3671         uint8_t now_flags;
3672
3673         /* Flags to leave enabled in S3 */
3674         uint8_t suspend_flags;
3675 } __ec_align1;
3676
3677 /*****************************************************************************/
3678 /* GPIO commands. Only available on EC if write protect has been disabled. */
3679
3680 /* Set GPIO output value */
3681 #define EC_CMD_GPIO_SET 0x0092
3682
3683 struct ec_params_gpio_set {
3684         char name[32];
3685         uint8_t val;
3686 } __ec_align1;
3687
3688 /* Get GPIO value */
3689 #define EC_CMD_GPIO_GET 0x0093
3690
3691 /* Version 0 of input params and response */
3692 struct ec_params_gpio_get {
3693         char name[32];
3694 } __ec_align1;
3695
3696 struct ec_response_gpio_get {
3697         uint8_t val;
3698 } __ec_align1;
3699
3700 /* Version 1 of input params and response */
3701 struct ec_params_gpio_get_v1 {
3702         uint8_t subcmd;
3703         union {
3704                 struct __ec_align1 {
3705                         char name[32];
3706                 } get_value_by_name;
3707                 struct __ec_align1 {
3708                         uint8_t index;
3709                 } get_info;
3710         };
3711 } __ec_align1;
3712
3713 struct ec_response_gpio_get_v1 {
3714         union {
3715                 struct __ec_align1 {
3716                         uint8_t val;
3717                 } get_value_by_name, get_count;
3718                 struct __ec_todo_unpacked {
3719                         uint8_t val;
3720                         char name[32];
3721                         uint32_t flags;
3722                 } get_info;
3723         };
3724 } __ec_todo_packed;
3725
3726 enum gpio_get_subcmd {
3727         EC_GPIO_GET_BY_NAME = 0,
3728         EC_GPIO_GET_COUNT = 1,
3729         EC_GPIO_GET_INFO = 2,
3730 };
3731
3732 /*****************************************************************************/
3733 /* I2C commands. Only available when flash write protect is unlocked. */
3734
3735 /*
3736  * CAUTION: These commands are deprecated, and are not supported anymore in EC
3737  * builds >= 8398.0.0 (see crosbug.com/p/23570).
3738  *
3739  * Use EC_CMD_I2C_PASSTHRU instead.
3740  */
3741
3742 /* Read I2C bus */
3743 #define EC_CMD_I2C_READ 0x0094
3744
3745 struct ec_params_i2c_read {
3746         uint16_t addr; /* 8-bit address (7-bit shifted << 1) */
3747         uint8_t read_size; /* Either 8 or 16. */
3748         uint8_t port;
3749         uint8_t offset;
3750 } __ec_align_size1;
3751
3752 struct ec_response_i2c_read {
3753         uint16_t data;
3754 } __ec_align2;
3755
3756 /* Write I2C bus */
3757 #define EC_CMD_I2C_WRITE 0x0095
3758
3759 struct ec_params_i2c_write {
3760         uint16_t data;
3761         uint16_t addr; /* 8-bit address (7-bit shifted << 1) */
3762         uint8_t write_size; /* Either 8 or 16. */
3763         uint8_t port;
3764         uint8_t offset;
3765 } __ec_align_size1;
3766
3767 /*****************************************************************************/
3768 /* Charge state commands. Only available when flash write protect unlocked. */
3769
3770 /* Force charge state machine to stop charging the battery or force it to
3771  * discharge the battery.
3772  */
3773 #define EC_CMD_CHARGE_CONTROL 0x0096
3774 #define EC_VER_CHARGE_CONTROL 1
3775
3776 enum ec_charge_control_mode {
3777         CHARGE_CONTROL_NORMAL = 0,
3778         CHARGE_CONTROL_IDLE,
3779         CHARGE_CONTROL_DISCHARGE,
3780 };
3781
3782 struct ec_params_charge_control {
3783         uint32_t mode;  /* enum charge_control_mode */
3784 } __ec_align4;
3785
3786 /*****************************************************************************/
3787
3788 /* Snapshot console output buffer for use by EC_CMD_CONSOLE_READ. */
3789 #define EC_CMD_CONSOLE_SNAPSHOT 0x0097
3790
3791 /*
3792  * Read data from the saved snapshot. If the subcmd parameter is
3793  * CONSOLE_READ_NEXT, this will return data starting from the beginning of
3794  * the latest snapshot. If it is CONSOLE_READ_RECENT, it will start from the
3795  * end of the previous snapshot.
3796  *
3797  * The params are only looked at in version >= 1 of this command. Prior
3798  * versions will just default to CONSOLE_READ_NEXT behavior.
3799  *
3800  * Response is null-terminated string.  Empty string, if there is no more
3801  * remaining output.
3802  */
3803 #define EC_CMD_CONSOLE_READ 0x0098
3804
3805 enum ec_console_read_subcmd {
3806         CONSOLE_READ_NEXT = 0,
3807         CONSOLE_READ_RECENT
3808 };
3809
3810 struct ec_params_console_read_v1 {
3811         uint8_t subcmd; /* enum ec_console_read_subcmd */
3812 } __ec_align1;
3813
3814 /*****************************************************************************/
3815
3816 /*
3817  * Cut off battery power immediately or after the host has shut down.
3818  *
3819  * return EC_RES_INVALID_COMMAND if unsupported by a board/battery.
3820  *        EC_RES_SUCCESS if the command was successful.
3821  *        EC_RES_ERROR if the cut off command failed.
3822  */
3823 #define EC_CMD_BATTERY_CUT_OFF 0x0099
3824
3825 #define EC_BATTERY_CUTOFF_FLAG_AT_SHUTDOWN      BIT(0)
3826
3827 struct ec_params_battery_cutoff {
3828         uint8_t flags;
3829 } __ec_align1;
3830
3831 /*****************************************************************************/
3832 /* USB port mux control. */
3833
3834 /*
3835  * Switch USB mux or return to automatic switching.
3836  */
3837 #define EC_CMD_USB_MUX 0x009A
3838
3839 struct ec_params_usb_mux {
3840         uint8_t mux;
3841 } __ec_align1;
3842
3843 /*****************************************************************************/
3844 /* LDOs / FETs control. */
3845
3846 enum ec_ldo_state {
3847         EC_LDO_STATE_OFF = 0,   /* the LDO / FET is shut down */
3848         EC_LDO_STATE_ON = 1,    /* the LDO / FET is ON / providing power */
3849 };
3850
3851 /*
3852  * Switch on/off a LDO.
3853  */
3854 #define EC_CMD_LDO_SET 0x009B
3855
3856 struct ec_params_ldo_set {
3857         uint8_t index;
3858         uint8_t state;
3859 } __ec_align1;
3860
3861 /*
3862  * Get LDO state.
3863  */
3864 #define EC_CMD_LDO_GET 0x009C
3865
3866 struct ec_params_ldo_get {
3867         uint8_t index;
3868 } __ec_align1;
3869
3870 struct ec_response_ldo_get {
3871         uint8_t state;
3872 } __ec_align1;
3873
3874 /*****************************************************************************/
3875 /* Power info. */
3876
3877 /*
3878  * Get power info.
3879  */
3880 #define EC_CMD_POWER_INFO 0x009D
3881
3882 struct ec_response_power_info {
3883         uint32_t usb_dev_type;
3884         uint16_t voltage_ac;
3885         uint16_t voltage_system;
3886         uint16_t current_system;
3887         uint16_t usb_current_limit;
3888 } __ec_align4;
3889
3890 /*****************************************************************************/
3891 /* I2C passthru command */
3892
3893 #define EC_CMD_I2C_PASSTHRU 0x009E
3894
3895 /* Read data; if not present, message is a write */
3896 #define EC_I2C_FLAG_READ        BIT(15)
3897
3898 /* Mask for address */
3899 #define EC_I2C_ADDR_MASK        0x3ff
3900
3901 #define EC_I2C_STATUS_NAK       BIT(0) /* Transfer was not acknowledged */
3902 #define EC_I2C_STATUS_TIMEOUT   BIT(1) /* Timeout during transfer */
3903
3904 /* Any error */
3905 #define EC_I2C_STATUS_ERROR     (EC_I2C_STATUS_NAK | EC_I2C_STATUS_TIMEOUT)
3906
3907 struct ec_params_i2c_passthru_msg {
3908         uint16_t addr_flags;    /* I2C slave address (7 or 10 bits) and flags */
3909         uint16_t len;           /* Number of bytes to read or write */
3910 } __ec_align2;
3911
3912 struct ec_params_i2c_passthru {
3913         uint8_t port;           /* I2C port number */
3914         uint8_t num_msgs;       /* Number of messages */
3915         struct ec_params_i2c_passthru_msg msg[];
3916         /* Data to write for all messages is concatenated here */
3917 } __ec_align2;
3918
3919 struct ec_response_i2c_passthru {
3920         uint8_t i2c_status;     /* Status flags (EC_I2C_STATUS_...) */
3921         uint8_t num_msgs;       /* Number of messages processed */
3922         uint8_t data[];         /* Data read by messages concatenated here */
3923 } __ec_align1;
3924
3925 /*****************************************************************************/
3926 /* Power button hang detect */
3927
3928 #define EC_CMD_HANG_DETECT 0x009F
3929
3930 /* Reasons to start hang detection timer */
3931 /* Power button pressed */
3932 #define EC_HANG_START_ON_POWER_PRESS  BIT(0)
3933
3934 /* Lid closed */
3935 #define EC_HANG_START_ON_LID_CLOSE    BIT(1)
3936
3937  /* Lid opened */
3938 #define EC_HANG_START_ON_LID_OPEN     BIT(2)
3939
3940 /* Start of AP S3->S0 transition (booting or resuming from suspend) */
3941 #define EC_HANG_START_ON_RESUME       BIT(3)
3942
3943 /* Reasons to cancel hang detection */
3944
3945 /* Power button released */
3946 #define EC_HANG_STOP_ON_POWER_RELEASE BIT(8)
3947
3948 /* Any host command from AP received */
3949 #define EC_HANG_STOP_ON_HOST_COMMAND  BIT(9)
3950
3951 /* Stop on end of AP S0->S3 transition (suspending or shutting down) */
3952 #define EC_HANG_STOP_ON_SUSPEND       BIT(10)
3953
3954 /*
3955  * If this flag is set, all the other fields are ignored, and the hang detect
3956  * timer is started.  This provides the AP a way to start the hang timer
3957  * without reconfiguring any of the other hang detect settings.  Note that
3958  * you must previously have configured the timeouts.
3959  */
3960 #define EC_HANG_START_NOW             BIT(30)
3961
3962 /*
3963  * If this flag is set, all the other fields are ignored (including
3964  * EC_HANG_START_NOW).  This provides the AP a way to stop the hang timer
3965  * without reconfiguring any of the other hang detect settings.
3966  */
3967 #define EC_HANG_STOP_NOW              BIT(31)
3968
3969 struct ec_params_hang_detect {
3970         /* Flags; see EC_HANG_* */
3971         uint32_t flags;
3972
3973         /* Timeout in msec before generating host event, if enabled */
3974         uint16_t host_event_timeout_msec;
3975
3976         /* Timeout in msec before generating warm reboot, if enabled */
3977         uint16_t warm_reboot_timeout_msec;
3978 } __ec_align4;
3979
3980 /*****************************************************************************/
3981 /* Commands for battery charging */
3982
3983 /*
3984  * This is the single catch-all host command to exchange data regarding the
3985  * charge state machine (v2 and up).
3986  */
3987 #define EC_CMD_CHARGE_STATE 0x00A0
3988
3989 /* Subcommands for this host command */
3990 enum charge_state_command {
3991         CHARGE_STATE_CMD_GET_STATE,
3992         CHARGE_STATE_CMD_GET_PARAM,
3993         CHARGE_STATE_CMD_SET_PARAM,
3994         CHARGE_STATE_NUM_CMDS
3995 };
3996
3997 /*
3998  * Known param numbers are defined here. Ranges are reserved for board-specific
3999  * params, which are handled by the particular implementations.
4000  */
4001 enum charge_state_params {
4002         CS_PARAM_CHG_VOLTAGE,         /* charger voltage limit */
4003         CS_PARAM_CHG_CURRENT,         /* charger current limit */
4004         CS_PARAM_CHG_INPUT_CURRENT,   /* charger input current limit */
4005         CS_PARAM_CHG_STATUS,          /* charger-specific status */
4006         CS_PARAM_CHG_OPTION,          /* charger-specific options */
4007         CS_PARAM_LIMIT_POWER,         /*
4008                                        * Check if power is limited due to
4009                                        * low battery and / or a weak external
4010                                        * charger. READ ONLY.
4011                                        */
4012         /* How many so far? */
4013         CS_NUM_BASE_PARAMS,
4014
4015         /* Range for CONFIG_CHARGER_PROFILE_OVERRIDE params */
4016         CS_PARAM_CUSTOM_PROFILE_MIN = 0x10000,
4017         CS_PARAM_CUSTOM_PROFILE_MAX = 0x1ffff,
4018
4019         /* Range for CONFIG_CHARGE_STATE_DEBUG params */
4020         CS_PARAM_DEBUG_MIN = 0x20000,
4021         CS_PARAM_DEBUG_CTL_MODE = 0x20000,
4022         CS_PARAM_DEBUG_MANUAL_MODE,
4023         CS_PARAM_DEBUG_SEEMS_DEAD,
4024         CS_PARAM_DEBUG_SEEMS_DISCONNECTED,
4025         CS_PARAM_DEBUG_BATT_REMOVED,
4026         CS_PARAM_DEBUG_MANUAL_CURRENT,
4027         CS_PARAM_DEBUG_MANUAL_VOLTAGE,
4028         CS_PARAM_DEBUG_MAX = 0x2ffff,
4029
4030         /* Other custom param ranges go here... */
4031 };
4032
4033 struct ec_params_charge_state {
4034         uint8_t cmd;                            /* enum charge_state_command */
4035         union {
4036                 /* get_state has no args */
4037
4038                 struct __ec_todo_unpacked {
4039                         uint32_t param;         /* enum charge_state_param */
4040                 } get_param;
4041
4042                 struct __ec_todo_unpacked {
4043                         uint32_t param;         /* param to set */
4044                         uint32_t value;         /* value to set */
4045                 } set_param;
4046         };
4047 } __ec_todo_packed;
4048
4049 struct ec_response_charge_state {
4050         union {
4051                 struct __ec_align4 {
4052                         int ac;
4053                         int chg_voltage;
4054                         int chg_current;
4055                         int chg_input_current;
4056                         int batt_state_of_charge;
4057                 } get_state;
4058
4059                 struct __ec_align4 {
4060                         uint32_t value;
4061                 } get_param;
4062
4063                 /* set_param returns no args */
4064         };
4065 } __ec_align4;
4066
4067
4068 /*
4069  * Set maximum battery charging current.
4070  */
4071 #define EC_CMD_CHARGE_CURRENT_LIMIT 0x00A1
4072
4073 struct ec_params_current_limit {
4074         uint32_t limit; /* in mA */
4075 } __ec_align4;
4076
4077 /*
4078  * Set maximum external voltage / current.
4079  */
4080 #define EC_CMD_EXTERNAL_POWER_LIMIT 0x00A2
4081
4082 /* Command v0 is used only on Spring and is obsolete + unsupported */
4083 struct ec_params_external_power_limit_v1 {
4084         uint16_t current_lim; /* in mA, or EC_POWER_LIMIT_NONE to clear limit */
4085         uint16_t voltage_lim; /* in mV, or EC_POWER_LIMIT_NONE to clear limit */
4086 } __ec_align2;
4087
4088 #define EC_POWER_LIMIT_NONE 0xffff
4089
4090 /*
4091  * Set maximum voltage & current of a dedicated charge port
4092  */
4093 #define EC_CMD_OVERRIDE_DEDICATED_CHARGER_LIMIT 0x00A3
4094
4095 struct ec_params_dedicated_charger_limit {
4096         uint16_t current_lim; /* in mA */
4097         uint16_t voltage_lim; /* in mV */
4098 } __ec_align2;
4099
4100 /*****************************************************************************/
4101 /* Hibernate/Deep Sleep Commands */
4102
4103 /* Set the delay before going into hibernation. */
4104 #define EC_CMD_HIBERNATION_DELAY 0x00A8
4105
4106 struct ec_params_hibernation_delay {
4107         /*
4108          * Seconds to wait in G3 before hibernate.  Pass in 0 to read the
4109          * current settings without changing them.
4110          */
4111         uint32_t seconds;
4112 } __ec_align4;
4113
4114 struct ec_response_hibernation_delay {
4115         /*
4116          * The current time in seconds in which the system has been in the G3
4117          * state.  This value is reset if the EC transitions out of G3.
4118          */
4119         uint32_t time_g3;
4120
4121         /*
4122          * The current time remaining in seconds until the EC should hibernate.
4123          * This value is also reset if the EC transitions out of G3.
4124          */
4125         uint32_t time_remaining;
4126
4127         /*
4128          * The current time in seconds that the EC should wait in G3 before
4129          * hibernating.
4130          */
4131         uint32_t hibernate_delay;
4132 } __ec_align4;
4133
4134 /* Inform the EC when entering a sleep state */
4135 #define EC_CMD_HOST_SLEEP_EVENT 0x00A9
4136
4137 enum host_sleep_event {
4138         HOST_SLEEP_EVENT_S3_SUSPEND   = 1,
4139         HOST_SLEEP_EVENT_S3_RESUME    = 2,
4140         HOST_SLEEP_EVENT_S0IX_SUSPEND = 3,
4141         HOST_SLEEP_EVENT_S0IX_RESUME  = 4,
4142         /* S3 suspend with additional enabled wake sources */
4143         HOST_SLEEP_EVENT_S3_WAKEABLE_SUSPEND = 5,
4144 };
4145
4146 struct ec_params_host_sleep_event {
4147         uint8_t sleep_event;
4148 } __ec_align1;
4149
4150 /*
4151  * Use a default timeout value (CONFIG_SLEEP_TIMEOUT_MS) for detecting sleep
4152  * transition failures
4153  */
4154 #define EC_HOST_SLEEP_TIMEOUT_DEFAULT 0
4155
4156 /* Disable timeout detection for this sleep transition */
4157 #define EC_HOST_SLEEP_TIMEOUT_INFINITE 0xFFFF
4158
4159 struct ec_params_host_sleep_event_v1 {
4160         /* The type of sleep being entered or exited. */
4161         uint8_t sleep_event;
4162
4163         /* Padding */
4164         uint8_t reserved;
4165         union {
4166                 /* Parameters that apply for suspend messages. */
4167                 struct {
4168                         /*
4169                          * The timeout in milliseconds between when this message
4170                          * is received and when the EC will declare sleep
4171                          * transition failure if the sleep signal is not
4172                          * asserted.
4173                          */
4174                         uint16_t sleep_timeout_ms;
4175                 } suspend_params;
4176
4177                 /* No parameters for non-suspend messages. */
4178         };
4179 } __ec_align2;
4180
4181 /* A timeout occurred when this bit is set */
4182 #define EC_HOST_RESUME_SLEEP_TIMEOUT 0x80000000
4183
4184 /*
4185  * The mask defining which bits correspond to the number of sleep transitions,
4186  * as well as the maximum number of suspend line transitions that will be
4187  * reported back to the host.
4188  */
4189 #define EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK 0x7FFFFFFF
4190
4191 struct ec_response_host_sleep_event_v1 {
4192         union {
4193                 /* Response fields that apply for resume messages. */
4194                 struct {
4195                         /*
4196                          * The number of sleep power signal transitions that
4197                          * occurred since the suspend message. The high bit
4198                          * indicates a timeout occurred.
4199                          */
4200                         uint32_t sleep_transitions;
4201                 } resume_response;
4202
4203                 /* No response fields for non-resume messages. */
4204         };
4205 } __ec_align4;
4206
4207 /*****************************************************************************/
4208 /* Device events */
4209 #define EC_CMD_DEVICE_EVENT 0x00AA
4210
4211 enum ec_device_event {
4212         EC_DEVICE_EVENT_TRACKPAD,
4213         EC_DEVICE_EVENT_DSP,
4214         EC_DEVICE_EVENT_WIFI,
4215 };
4216
4217 enum ec_device_event_param {
4218         /* Get and clear pending device events */
4219         EC_DEVICE_EVENT_PARAM_GET_CURRENT_EVENTS,
4220         /* Get device event mask */
4221         EC_DEVICE_EVENT_PARAM_GET_ENABLED_EVENTS,
4222         /* Set device event mask */
4223         EC_DEVICE_EVENT_PARAM_SET_ENABLED_EVENTS,
4224 };
4225
4226 #define EC_DEVICE_EVENT_MASK(event_code) BIT(event_code % 32)
4227
4228 struct ec_params_device_event {
4229         uint32_t event_mask;
4230         uint8_t param;
4231 } __ec_align_size1;
4232
4233 struct ec_response_device_event {
4234         uint32_t event_mask;
4235 } __ec_align4;
4236
4237 /*****************************************************************************/
4238 /* Smart battery pass-through */
4239
4240 /* Get / Set 16-bit smart battery registers */
4241 #define EC_CMD_SB_READ_WORD   0x00B0
4242 #define EC_CMD_SB_WRITE_WORD  0x00B1
4243
4244 /* Get / Set string smart battery parameters
4245  * formatted as SMBUS "block".
4246  */
4247 #define EC_CMD_SB_READ_BLOCK  0x00B2
4248 #define EC_CMD_SB_WRITE_BLOCK 0x00B3
4249
4250 struct ec_params_sb_rd {
4251         uint8_t reg;
4252 } __ec_align1;
4253
4254 struct ec_response_sb_rd_word {
4255         uint16_t value;
4256 } __ec_align2;
4257
4258 struct ec_params_sb_wr_word {
4259         uint8_t reg;
4260         uint16_t value;
4261 } __ec_align1;
4262
4263 struct ec_response_sb_rd_block {
4264         uint8_t data[32];
4265 } __ec_align1;
4266
4267 struct ec_params_sb_wr_block {
4268         uint8_t reg;
4269         uint16_t data[32];
4270 } __ec_align1;
4271
4272 /*****************************************************************************/
4273 /* Battery vendor parameters
4274  *
4275  * Get or set vendor-specific parameters in the battery. Implementations may
4276  * differ between boards or batteries. On a set operation, the response
4277  * contains the actual value set, which may be rounded or clipped from the
4278  * requested value.
4279  */
4280
4281 #define EC_CMD_BATTERY_VENDOR_PARAM 0x00B4
4282
4283 enum ec_battery_vendor_param_mode {
4284         BATTERY_VENDOR_PARAM_MODE_GET = 0,
4285         BATTERY_VENDOR_PARAM_MODE_SET,
4286 };
4287
4288 struct ec_params_battery_vendor_param {
4289         uint32_t param;
4290         uint32_t value;
4291         uint8_t mode;
4292 } __ec_align_size1;
4293
4294 struct ec_response_battery_vendor_param {
4295         uint32_t value;
4296 } __ec_align4;
4297
4298 /*****************************************************************************/
4299 /*
4300  * Smart Battery Firmware Update Commands
4301  */
4302 #define EC_CMD_SB_FW_UPDATE 0x00B5
4303
4304 enum ec_sb_fw_update_subcmd {
4305         EC_SB_FW_UPDATE_PREPARE  = 0x0,
4306         EC_SB_FW_UPDATE_INFO     = 0x1, /*query sb info */
4307         EC_SB_FW_UPDATE_BEGIN    = 0x2, /*check if protected */
4308         EC_SB_FW_UPDATE_WRITE    = 0x3, /*check if protected */
4309         EC_SB_FW_UPDATE_END      = 0x4,
4310         EC_SB_FW_UPDATE_STATUS   = 0x5,
4311         EC_SB_FW_UPDATE_PROTECT  = 0x6,
4312         EC_SB_FW_UPDATE_MAX      = 0x7,
4313 };
4314
4315 #define SB_FW_UPDATE_CMD_WRITE_BLOCK_SIZE 32
4316 #define SB_FW_UPDATE_CMD_STATUS_SIZE 2
4317 #define SB_FW_UPDATE_CMD_INFO_SIZE 8
4318
4319 struct ec_sb_fw_update_header {
4320         uint16_t subcmd;  /* enum ec_sb_fw_update_subcmd */
4321         uint16_t fw_id;   /* firmware id */
4322 } __ec_align4;
4323
4324 struct ec_params_sb_fw_update {
4325         struct ec_sb_fw_update_header hdr;
4326         union {
4327                 /* EC_SB_FW_UPDATE_PREPARE  = 0x0 */
4328                 /* EC_SB_FW_UPDATE_INFO     = 0x1 */
4329                 /* EC_SB_FW_UPDATE_BEGIN    = 0x2 */
4330                 /* EC_SB_FW_UPDATE_END      = 0x4 */
4331                 /* EC_SB_FW_UPDATE_STATUS   = 0x5 */
4332                 /* EC_SB_FW_UPDATE_PROTECT  = 0x6 */
4333                 /* Those have no args */
4334
4335                 /* EC_SB_FW_UPDATE_WRITE    = 0x3 */
4336                 struct __ec_align4 {
4337                         uint8_t  data[SB_FW_UPDATE_CMD_WRITE_BLOCK_SIZE];
4338                 } write;
4339         };
4340 } __ec_align4;
4341
4342 struct ec_response_sb_fw_update {
4343         union {
4344                 /* EC_SB_FW_UPDATE_INFO     = 0x1 */
4345                 struct __ec_align1 {
4346                         uint8_t data[SB_FW_UPDATE_CMD_INFO_SIZE];
4347                 } info;
4348
4349                 /* EC_SB_FW_UPDATE_STATUS   = 0x5 */
4350                 struct __ec_align1 {
4351                         uint8_t data[SB_FW_UPDATE_CMD_STATUS_SIZE];
4352                 } status;
4353         };
4354 } __ec_align1;
4355
4356 /*
4357  * Entering Verified Boot Mode Command
4358  * Default mode is VBOOT_MODE_NORMAL if EC did not receive this command.
4359  * Valid Modes are: normal, developer, and recovery.
4360  */
4361 #define EC_CMD_ENTERING_MODE 0x00B6
4362
4363 struct ec_params_entering_mode {
4364         int vboot_mode;
4365 } __ec_align4;
4366
4367 #define VBOOT_MODE_NORMAL    0
4368 #define VBOOT_MODE_DEVELOPER 1
4369 #define VBOOT_MODE_RECOVERY  2
4370
4371 /*****************************************************************************/
4372 /*
4373  * I2C passthru protection command: Protects I2C tunnels against access on
4374  * certain addresses (board-specific).
4375  */
4376 #define EC_CMD_I2C_PASSTHRU_PROTECT 0x00B7
4377
4378 enum ec_i2c_passthru_protect_subcmd {
4379         EC_CMD_I2C_PASSTHRU_PROTECT_STATUS = 0x0,
4380         EC_CMD_I2C_PASSTHRU_PROTECT_ENABLE = 0x1,
4381 };
4382
4383 struct ec_params_i2c_passthru_protect {
4384         uint8_t subcmd;
4385         uint8_t port;           /* I2C port number */
4386 } __ec_align1;
4387
4388 struct ec_response_i2c_passthru_protect {
4389         uint8_t status;         /* Status flags (0: unlocked, 1: locked) */
4390 } __ec_align1;
4391
4392
4393 /*****************************************************************************/
4394 /*
4395  * HDMI CEC commands
4396  *
4397  * These commands are for sending and receiving message via HDMI CEC
4398  */
4399
4400 #define MAX_CEC_MSG_LEN 16
4401
4402 /* CEC message from the AP to be written on the CEC bus */
4403 #define EC_CMD_CEC_WRITE_MSG 0x00B8
4404
4405 /**
4406  * struct ec_params_cec_write - Message to write to the CEC bus
4407  * @msg: message content to write to the CEC bus
4408  */
4409 struct ec_params_cec_write {
4410         uint8_t msg[MAX_CEC_MSG_LEN];
4411 } __ec_align1;
4412
4413 /* Set various CEC parameters */
4414 #define EC_CMD_CEC_SET 0x00BA
4415
4416 /**
4417  * struct ec_params_cec_set - CEC parameters set
4418  * @cmd: parameter type, can be CEC_CMD_ENABLE or CEC_CMD_LOGICAL_ADDRESS
4419  * @val: in case cmd is CEC_CMD_ENABLE, this field can be 0 to disable CEC
4420  *      or 1 to enable CEC functionality, in case cmd is
4421  *      CEC_CMD_LOGICAL_ADDRESS, this field encodes the requested logical
4422  *      address between 0 and 15 or 0xff to unregister
4423  */
4424 struct ec_params_cec_set {
4425         uint8_t cmd; /* enum cec_command */
4426         uint8_t val;
4427 } __ec_align1;
4428
4429 /* Read various CEC parameters */
4430 #define EC_CMD_CEC_GET 0x00BB
4431
4432 /**
4433  * struct ec_params_cec_get - CEC parameters get
4434  * @cmd: parameter type, can be CEC_CMD_ENABLE or CEC_CMD_LOGICAL_ADDRESS
4435  */
4436 struct ec_params_cec_get {
4437         uint8_t cmd; /* enum cec_command */
4438 } __ec_align1;
4439
4440 /**
4441  * struct ec_response_cec_get - CEC parameters get response
4442  * @val: in case cmd was CEC_CMD_ENABLE, this field will 0 if CEC is
4443  *      disabled or 1 if CEC functionality is enabled,
4444  *      in case cmd was CEC_CMD_LOGICAL_ADDRESS, this will encode the
4445  *      configured logical address between 0 and 15 or 0xff if unregistered
4446  */
4447 struct ec_response_cec_get {
4448         uint8_t val;
4449 } __ec_align1;
4450
4451 /* CEC parameters command */
4452 enum cec_command {
4453         /* CEC reading, writing and events enable */
4454         CEC_CMD_ENABLE,
4455         /* CEC logical address  */
4456         CEC_CMD_LOGICAL_ADDRESS,
4457 };
4458
4459 /* Events from CEC to AP */
4460 enum mkbp_cec_event {
4461         /* Outgoing message was acknowledged by a follower */
4462         EC_MKBP_CEC_SEND_OK                     = BIT(0),
4463         /* Outgoing message was not acknowledged */
4464         EC_MKBP_CEC_SEND_FAILED                 = BIT(1),
4465 };
4466
4467 /*****************************************************************************/
4468
4469 /* Commands for audio codec. */
4470 #define EC_CMD_EC_CODEC 0x00BC
4471
4472 enum ec_codec_subcmd {
4473         EC_CODEC_GET_CAPABILITIES = 0x0,
4474         EC_CODEC_GET_SHM_ADDR = 0x1,
4475         EC_CODEC_SET_SHM_ADDR = 0x2,
4476         EC_CODEC_SUBCMD_COUNT,
4477 };
4478
4479 enum ec_codec_cap {
4480         EC_CODEC_CAP_LAST = 32,
4481 };
4482
4483 enum ec_codec_shm_id {
4484         EC_CODEC_SHM_ID_LAST,
4485 };
4486
4487 enum ec_codec_shm_type {
4488         EC_CODEC_SHM_TYPE_EC_RAM = 0x0,
4489         EC_CODEC_SHM_TYPE_SYSTEM_RAM = 0x1,
4490 };
4491
4492 struct __ec_align1 ec_param_ec_codec_get_shm_addr {
4493         uint8_t shm_id;
4494         uint8_t reserved[3];
4495 };
4496
4497 struct __ec_align4 ec_param_ec_codec_set_shm_addr {
4498         uint64_t phys_addr;
4499         uint32_t len;
4500         uint8_t shm_id;
4501         uint8_t reserved[3];
4502 };
4503
4504 struct __ec_align4 ec_param_ec_codec {
4505         uint8_t cmd; /* enum ec_codec_subcmd */
4506         uint8_t reserved[3];
4507
4508         union {
4509                 struct ec_param_ec_codec_get_shm_addr
4510                                 get_shm_addr_param;
4511                 struct ec_param_ec_codec_set_shm_addr
4512                                 set_shm_addr_param;
4513         };
4514 };
4515
4516 struct __ec_align4 ec_response_ec_codec_get_capabilities {
4517         uint32_t capabilities;
4518 };
4519
4520 struct __ec_align4 ec_response_ec_codec_get_shm_addr {
4521         uint64_t phys_addr;
4522         uint32_t len;
4523         uint8_t type;
4524         uint8_t reserved[3];
4525 };
4526
4527 /*****************************************************************************/
4528
4529 /* Commands for DMIC on audio codec. */
4530 #define EC_CMD_EC_CODEC_DMIC 0x00BD
4531
4532 enum ec_codec_dmic_subcmd {
4533         EC_CODEC_DMIC_GET_MAX_GAIN = 0x0,
4534         EC_CODEC_DMIC_SET_GAIN_IDX = 0x1,
4535         EC_CODEC_DMIC_GET_GAIN_IDX = 0x2,
4536         EC_CODEC_DMIC_SUBCMD_COUNT,
4537 };
4538
4539 enum ec_codec_dmic_channel {
4540         EC_CODEC_DMIC_CHANNEL_0 = 0x0,
4541         EC_CODEC_DMIC_CHANNEL_1 = 0x1,
4542         EC_CODEC_DMIC_CHANNEL_2 = 0x2,
4543         EC_CODEC_DMIC_CHANNEL_3 = 0x3,
4544         EC_CODEC_DMIC_CHANNEL_4 = 0x4,
4545         EC_CODEC_DMIC_CHANNEL_5 = 0x5,
4546         EC_CODEC_DMIC_CHANNEL_6 = 0x6,
4547         EC_CODEC_DMIC_CHANNEL_7 = 0x7,
4548         EC_CODEC_DMIC_CHANNEL_COUNT,
4549 };
4550
4551 struct __ec_align1 ec_param_ec_codec_dmic_set_gain_idx {
4552         uint8_t channel; /* enum ec_codec_dmic_channel */
4553         uint8_t gain;
4554         uint8_t reserved[2];
4555 };
4556
4557 struct __ec_align1 ec_param_ec_codec_dmic_get_gain_idx {
4558         uint8_t channel; /* enum ec_codec_dmic_channel */
4559         uint8_t reserved[3];
4560 };
4561
4562 struct __ec_align4 ec_param_ec_codec_dmic {
4563         uint8_t cmd; /* enum ec_codec_dmic_subcmd */
4564         uint8_t reserved[3];
4565
4566         union {
4567                 struct ec_param_ec_codec_dmic_set_gain_idx
4568                                 set_gain_idx_param;
4569                 struct ec_param_ec_codec_dmic_get_gain_idx
4570                                 get_gain_idx_param;
4571         };
4572 };
4573
4574 struct __ec_align1 ec_response_ec_codec_dmic_get_max_gain {
4575         uint8_t max_gain;
4576 };
4577
4578 struct __ec_align1 ec_response_ec_codec_dmic_get_gain_idx {
4579         uint8_t gain;
4580 };
4581
4582 /*****************************************************************************/
4583
4584 /* Commands for I2S RX on audio codec. */
4585
4586 #define EC_CMD_EC_CODEC_I2S_RX 0x00BE
4587
4588 enum ec_codec_i2s_rx_subcmd {
4589         EC_CODEC_I2S_RX_ENABLE = 0x0,
4590         EC_CODEC_I2S_RX_DISABLE = 0x1,
4591         EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH = 0x2,
4592         EC_CODEC_I2S_RX_SET_DAIFMT = 0x3,
4593         EC_CODEC_I2S_RX_SET_BCLK = 0x4,
4594         EC_CODEC_I2S_RX_SUBCMD_COUNT,
4595 };
4596
4597 enum ec_codec_i2s_rx_sample_depth {
4598         EC_CODEC_I2S_RX_SAMPLE_DEPTH_16 = 0x0,
4599         EC_CODEC_I2S_RX_SAMPLE_DEPTH_24 = 0x1,
4600         EC_CODEC_I2S_RX_SAMPLE_DEPTH_COUNT,
4601 };
4602
4603 enum ec_codec_i2s_rx_daifmt {
4604         EC_CODEC_I2S_RX_DAIFMT_I2S = 0x0,
4605         EC_CODEC_I2S_RX_DAIFMT_RIGHT_J = 0x1,
4606         EC_CODEC_I2S_RX_DAIFMT_LEFT_J = 0x2,
4607         EC_CODEC_I2S_RX_DAIFMT_COUNT,
4608 };
4609
4610 struct __ec_align1 ec_param_ec_codec_i2s_rx_set_sample_depth {
4611         uint8_t depth;
4612         uint8_t reserved[3];
4613 };
4614
4615 struct __ec_align1 ec_param_ec_codec_i2s_rx_set_gain {
4616         uint8_t left;
4617         uint8_t right;
4618         uint8_t reserved[2];
4619 };
4620
4621 struct __ec_align1 ec_param_ec_codec_i2s_rx_set_daifmt {
4622         uint8_t daifmt;
4623         uint8_t reserved[3];
4624 };
4625
4626 struct __ec_align4 ec_param_ec_codec_i2s_rx_set_bclk {
4627         uint32_t bclk;
4628 };
4629
4630 struct __ec_align4 ec_param_ec_codec_i2s_rx {
4631         uint8_t cmd; /* enum ec_codec_i2s_rx_subcmd */
4632         uint8_t reserved[3];
4633
4634         union {
4635                 struct ec_param_ec_codec_i2s_rx_set_sample_depth
4636                                 set_sample_depth_param;
4637                 struct ec_param_ec_codec_i2s_rx_set_daifmt
4638                                 set_daifmt_param;
4639                 struct ec_param_ec_codec_i2s_rx_set_bclk
4640                                 set_bclk_param;
4641         };
4642 };
4643
4644 /*****************************************************************************/
4645 /* System commands */
4646
4647 /*
4648  * TODO(crosbug.com/p/23747): This is a confusing name, since it doesn't
4649  * necessarily reboot the EC.  Rename to "image" or something similar?
4650  */
4651 #define EC_CMD_REBOOT_EC 0x00D2
4652
4653 /* Command */
4654 enum ec_reboot_cmd {
4655         EC_REBOOT_CANCEL = 0,        /* Cancel a pending reboot */
4656         EC_REBOOT_JUMP_RO = 1,       /* Jump to RO without rebooting */
4657         EC_REBOOT_JUMP_RW = 2,       /* Jump to active RW without rebooting */
4658         /* (command 3 was jump to RW-B) */
4659         EC_REBOOT_COLD = 4,          /* Cold-reboot */
4660         EC_REBOOT_DISABLE_JUMP = 5,  /* Disable jump until next reboot */
4661         EC_REBOOT_HIBERNATE = 6,     /* Hibernate EC */
4662         EC_REBOOT_HIBERNATE_CLEAR_AP_OFF = 7, /* and clears AP_OFF flag */
4663 };
4664
4665 /* Flags for ec_params_reboot_ec.reboot_flags */
4666 #define EC_REBOOT_FLAG_RESERVED0      BIT(0)  /* Was recovery request */
4667 #define EC_REBOOT_FLAG_ON_AP_SHUTDOWN BIT(1)  /* Reboot after AP shutdown */
4668 #define EC_REBOOT_FLAG_SWITCH_RW_SLOT BIT(2)  /* Switch RW slot */
4669
4670 struct ec_params_reboot_ec {
4671         uint8_t cmd;           /* enum ec_reboot_cmd */
4672         uint8_t flags;         /* See EC_REBOOT_FLAG_* */
4673 } __ec_align1;
4674
4675 /*
4676  * Get information on last EC panic.
4677  *
4678  * Returns variable-length platform-dependent panic information.  See panic.h
4679  * for details.
4680  */
4681 #define EC_CMD_GET_PANIC_INFO 0x00D3
4682
4683 /*****************************************************************************/
4684 /*
4685  * Special commands
4686  *
4687  * These do not follow the normal rules for commands.  See each command for
4688  * details.
4689  */
4690
4691 /*
4692  * Reboot NOW
4693  *
4694  * This command will work even when the EC LPC interface is busy, because the
4695  * reboot command is processed at interrupt level.  Note that when the EC
4696  * reboots, the host will reboot too, so there is no response to this command.
4697  *
4698  * Use EC_CMD_REBOOT_EC to reboot the EC more politely.
4699  */
4700 #define EC_CMD_REBOOT 0x00D1  /* Think "die" */
4701
4702 /*
4703  * Resend last response (not supported on LPC).
4704  *
4705  * Returns EC_RES_UNAVAILABLE if there is no response available - for example,
4706  * there was no previous command, or the previous command's response was too
4707  * big to save.
4708  */
4709 #define EC_CMD_RESEND_RESPONSE 0x00DB
4710
4711 /*
4712  * This header byte on a command indicate version 0. Any header byte less
4713  * than this means that we are talking to an old EC which doesn't support
4714  * versioning. In that case, we assume version 0.
4715  *
4716  * Header bytes greater than this indicate a later version. For example,
4717  * EC_CMD_VERSION0 + 1 means we are using version 1.
4718  *
4719  * The old EC interface must not use commands 0xdc or higher.
4720  */
4721 #define EC_CMD_VERSION0 0x00DC
4722
4723 /*****************************************************************************/
4724 /*
4725  * PD commands
4726  *
4727  * These commands are for PD MCU communication.
4728  */
4729
4730 /* EC to PD MCU exchange status command */
4731 #define EC_CMD_PD_EXCHANGE_STATUS 0x0100
4732 #define EC_VER_PD_EXCHANGE_STATUS 2
4733
4734 enum pd_charge_state {
4735         PD_CHARGE_NO_CHANGE = 0, /* Don't change charge state */
4736         PD_CHARGE_NONE,          /* No charging allowed */
4737         PD_CHARGE_5V,            /* 5V charging only */
4738         PD_CHARGE_MAX            /* Charge at max voltage */
4739 };
4740
4741 /* Status of EC being sent to PD */
4742 #define EC_STATUS_HIBERNATING   BIT(0)
4743
4744 struct ec_params_pd_status {
4745         uint8_t status;       /* EC status */
4746         int8_t batt_soc;      /* battery state of charge */
4747         uint8_t charge_state; /* charging state (from enum pd_charge_state) */
4748 } __ec_align1;
4749
4750 /* Status of PD being sent back to EC */
4751 #define PD_STATUS_HOST_EVENT      BIT(0) /* Forward host event to AP */
4752 #define PD_STATUS_IN_RW           BIT(1) /* Running RW image */
4753 #define PD_STATUS_JUMPED_TO_IMAGE BIT(2) /* Current image was jumped to */
4754 #define PD_STATUS_TCPC_ALERT_0    BIT(3) /* Alert active in port 0 TCPC */
4755 #define PD_STATUS_TCPC_ALERT_1    BIT(4) /* Alert active in port 1 TCPC */
4756 #define PD_STATUS_TCPC_ALERT_2    BIT(5) /* Alert active in port 2 TCPC */
4757 #define PD_STATUS_TCPC_ALERT_3    BIT(6) /* Alert active in port 3 TCPC */
4758 #define PD_STATUS_EC_INT_ACTIVE  (PD_STATUS_TCPC_ALERT_0 | \
4759                                       PD_STATUS_TCPC_ALERT_1 | \
4760                                       PD_STATUS_HOST_EVENT)
4761 struct ec_response_pd_status {
4762         uint32_t curr_lim_ma;       /* input current limit */
4763         uint16_t status;            /* PD MCU status */
4764         int8_t active_charge_port;  /* active charging port */
4765 } __ec_align_size1;
4766
4767 /* AP to PD MCU host event status command, cleared on read */
4768 #define EC_CMD_PD_HOST_EVENT_STATUS 0x0104
4769
4770 /* PD MCU host event status bits */
4771 #define PD_EVENT_UPDATE_DEVICE     BIT(0)
4772 #define PD_EVENT_POWER_CHANGE      BIT(1)
4773 #define PD_EVENT_IDENTITY_RECEIVED BIT(2)
4774 #define PD_EVENT_DATA_SWAP         BIT(3)
4775 struct ec_response_host_event_status {
4776         uint32_t status;      /* PD MCU host event status */
4777 } __ec_align4;
4778
4779 /* Set USB type-C port role and muxes */
4780 #define EC_CMD_USB_PD_CONTROL 0x0101
4781
4782 enum usb_pd_control_role {
4783         USB_PD_CTRL_ROLE_NO_CHANGE = 0,
4784         USB_PD_CTRL_ROLE_TOGGLE_ON = 1, /* == AUTO */
4785         USB_PD_CTRL_ROLE_TOGGLE_OFF = 2,
4786         USB_PD_CTRL_ROLE_FORCE_SINK = 3,
4787         USB_PD_CTRL_ROLE_FORCE_SOURCE = 4,
4788         USB_PD_CTRL_ROLE_FREEZE = 5,
4789         USB_PD_CTRL_ROLE_COUNT
4790 };
4791
4792 enum usb_pd_control_mux {
4793         USB_PD_CTRL_MUX_NO_CHANGE = 0,
4794         USB_PD_CTRL_MUX_NONE = 1,
4795         USB_PD_CTRL_MUX_USB = 2,
4796         USB_PD_CTRL_MUX_DP = 3,
4797         USB_PD_CTRL_MUX_DOCK = 4,
4798         USB_PD_CTRL_MUX_AUTO = 5,
4799         USB_PD_CTRL_MUX_COUNT
4800 };
4801
4802 enum usb_pd_control_swap {
4803         USB_PD_CTRL_SWAP_NONE = 0,
4804         USB_PD_CTRL_SWAP_DATA = 1,
4805         USB_PD_CTRL_SWAP_POWER = 2,
4806         USB_PD_CTRL_SWAP_VCONN = 3,
4807         USB_PD_CTRL_SWAP_COUNT
4808 };
4809
4810 struct ec_params_usb_pd_control {
4811         uint8_t port;
4812         uint8_t role;
4813         uint8_t mux;
4814         uint8_t swap;
4815 } __ec_align1;
4816
4817 #define PD_CTRL_RESP_ENABLED_COMMS      BIT(0) /* Communication enabled */
4818 #define PD_CTRL_RESP_ENABLED_CONNECTED  BIT(1) /* Device connected */
4819 #define PD_CTRL_RESP_ENABLED_PD_CAPABLE BIT(2) /* Partner is PD capable */
4820
4821 #define PD_CTRL_RESP_ROLE_POWER         BIT(0) /* 0=SNK/1=SRC */
4822 #define PD_CTRL_RESP_ROLE_DATA          BIT(1) /* 0=UFP/1=DFP */
4823 #define PD_CTRL_RESP_ROLE_VCONN         BIT(2) /* Vconn status */
4824 #define PD_CTRL_RESP_ROLE_DR_POWER      BIT(3) /* Partner is dualrole power */
4825 #define PD_CTRL_RESP_ROLE_DR_DATA       BIT(4) /* Partner is dualrole data */
4826 #define PD_CTRL_RESP_ROLE_USB_COMM      BIT(5) /* Partner USB comm capable */
4827 #define PD_CTRL_RESP_ROLE_EXT_POWERED   BIT(6) /* Partner externally powerd */
4828
4829 struct ec_response_usb_pd_control {
4830         uint8_t enabled;
4831         uint8_t role;
4832         uint8_t polarity;
4833         uint8_t state;
4834 } __ec_align1;
4835
4836 struct ec_response_usb_pd_control_v1 {
4837         uint8_t enabled;
4838         uint8_t role;
4839         uint8_t polarity;
4840         char state[32];
4841 } __ec_align1;
4842
4843 /* Values representing usbc PD CC state */
4844 #define USBC_PD_CC_NONE         0 /* No accessory connected */
4845 #define USBC_PD_CC_NO_UFP       1 /* No UFP accessory connected */
4846 #define USBC_PD_CC_AUDIO_ACC    2 /* Audio accessory connected */
4847 #define USBC_PD_CC_DEBUG_ACC    3 /* Debug accessory connected */
4848 #define USBC_PD_CC_UFP_ATTACHED 4 /* UFP attached to usbc */
4849 #define USBC_PD_CC_DFP_ATTACHED 5 /* DPF attached to usbc */
4850
4851 struct ec_response_usb_pd_control_v2 {
4852         uint8_t enabled;
4853         uint8_t role;
4854         uint8_t polarity;
4855         char state[32];
4856         uint8_t cc_state; /* USBC_PD_CC_*Encoded cc state */
4857         uint8_t dp_mode;  /* Current DP pin mode (MODE_DP_PIN_[A-E]) */
4858         /* CL:1500994 Current cable type */
4859         uint8_t reserved_cable_type;
4860 } __ec_align1;
4861
4862 #define EC_CMD_USB_PD_PORTS 0x0102
4863
4864 /* Maximum number of PD ports on a device, num_ports will be <= this */
4865 #define EC_USB_PD_MAX_PORTS 8
4866
4867 struct ec_response_usb_pd_ports {
4868         uint8_t num_ports;
4869 } __ec_align1;
4870
4871 #define EC_CMD_USB_PD_POWER_INFO 0x0103
4872
4873 #define PD_POWER_CHARGING_PORT 0xff
4874 struct ec_params_usb_pd_power_info {
4875         uint8_t port;
4876 } __ec_align1;
4877
4878 enum usb_chg_type {
4879         USB_CHG_TYPE_NONE,
4880         USB_CHG_TYPE_PD,
4881         USB_CHG_TYPE_C,
4882         USB_CHG_TYPE_PROPRIETARY,
4883         USB_CHG_TYPE_BC12_DCP,
4884         USB_CHG_TYPE_BC12_CDP,
4885         USB_CHG_TYPE_BC12_SDP,
4886         USB_CHG_TYPE_OTHER,
4887         USB_CHG_TYPE_VBUS,
4888         USB_CHG_TYPE_UNKNOWN,
4889         USB_CHG_TYPE_DEDICATED,
4890 };
4891 enum usb_power_roles {
4892         USB_PD_PORT_POWER_DISCONNECTED,
4893         USB_PD_PORT_POWER_SOURCE,
4894         USB_PD_PORT_POWER_SINK,
4895         USB_PD_PORT_POWER_SINK_NOT_CHARGING,
4896 };
4897
4898 struct usb_chg_measures {
4899         uint16_t voltage_max;
4900         uint16_t voltage_now;
4901         uint16_t current_max;
4902         uint16_t current_lim;
4903 } __ec_align2;
4904
4905 struct ec_response_usb_pd_power_info {
4906         uint8_t role;
4907         uint8_t type;
4908         uint8_t dualrole;
4909         uint8_t reserved1;
4910         struct usb_chg_measures meas;
4911         uint32_t max_power;
4912 } __ec_align4;
4913
4914
4915 /*
4916  * This command will return the number of USB PD charge port + the number
4917  * of dedicated port present.
4918  * EC_CMD_USB_PD_PORTS does NOT include the dedicated ports
4919  */
4920 #define EC_CMD_CHARGE_PORT_COUNT 0x0105
4921 struct ec_response_charge_port_count {
4922         uint8_t port_count;
4923 } __ec_align1;
4924
4925 /* Write USB-PD device FW */
4926 #define EC_CMD_USB_PD_FW_UPDATE 0x0110
4927
4928 enum usb_pd_fw_update_cmds {
4929         USB_PD_FW_REBOOT,
4930         USB_PD_FW_FLASH_ERASE,
4931         USB_PD_FW_FLASH_WRITE,
4932         USB_PD_FW_ERASE_SIG,
4933 };
4934
4935 struct ec_params_usb_pd_fw_update {
4936         uint16_t dev_id;
4937         uint8_t cmd;
4938         uint8_t port;
4939         uint32_t size;     /* Size to write in bytes */
4940         /* Followed by data to write */
4941 } __ec_align4;
4942
4943 /* Write USB-PD Accessory RW_HASH table entry */
4944 #define EC_CMD_USB_PD_RW_HASH_ENTRY 0x0111
4945 /* RW hash is first 20 bytes of SHA-256 of RW section */
4946 #define PD_RW_HASH_SIZE 20
4947 struct ec_params_usb_pd_rw_hash_entry {
4948         uint16_t dev_id;
4949         uint8_t dev_rw_hash[PD_RW_HASH_SIZE];
4950         uint8_t reserved;        /*
4951                                   * For alignment of current_image
4952                                   * TODO(rspangler) but it's not aligned!
4953                                   * Should have been reserved[2].
4954                                   */
4955         uint32_t current_image;  /* One of ec_current_image */
4956 } __ec_align1;
4957
4958 /* Read USB-PD Accessory info */
4959 #define EC_CMD_USB_PD_DEV_INFO 0x0112
4960
4961 struct ec_params_usb_pd_info_request {
4962         uint8_t port;
4963 } __ec_align1;
4964
4965 /* Read USB-PD Device discovery info */
4966 #define EC_CMD_USB_PD_DISCOVERY 0x0113
4967 struct ec_params_usb_pd_discovery_entry {
4968         uint16_t vid;  /* USB-IF VID */
4969         uint16_t pid;  /* USB-IF PID */
4970         uint8_t ptype; /* product type (hub,periph,cable,ama) */
4971 } __ec_align_size1;
4972
4973 /* Override default charge behavior */
4974 #define EC_CMD_PD_CHARGE_PORT_OVERRIDE 0x0114
4975
4976 /* Negative port parameters have special meaning */
4977 enum usb_pd_override_ports {
4978         OVERRIDE_DONT_CHARGE = -2,
4979         OVERRIDE_OFF = -1,
4980         /* [0, CONFIG_USB_PD_PORT_COUNT): Port# */
4981 };
4982
4983 struct ec_params_charge_port_override {
4984         int16_t override_port; /* Override port# */
4985 } __ec_align2;
4986
4987 /*
4988  * Read (and delete) one entry of PD event log.
4989  * TODO(crbug.com/751742): Make this host command more generic to accommodate
4990  * future non-PD logs that use the same internal EC event_log.
4991  */
4992 #define EC_CMD_PD_GET_LOG_ENTRY 0x0115
4993
4994 struct ec_response_pd_log {
4995         uint32_t timestamp; /* relative timestamp in milliseconds */
4996         uint8_t type;       /* event type : see PD_EVENT_xx below */
4997         uint8_t size_port;  /* [7:5] port number [4:0] payload size in bytes */
4998         uint16_t data;      /* type-defined data payload */
4999         uint8_t payload[0]; /* optional additional data payload: 0..16 bytes */
5000 } __ec_align4;
5001
5002 /* The timestamp is the microsecond counter shifted to get about a ms. */
5003 #define PD_LOG_TIMESTAMP_SHIFT 10 /* 1 LSB = 1024us */
5004
5005 #define PD_LOG_SIZE_MASK  0x1f
5006 #define PD_LOG_PORT_MASK  0xe0
5007 #define PD_LOG_PORT_SHIFT    5
5008 #define PD_LOG_PORT_SIZE(port, size) (((port) << PD_LOG_PORT_SHIFT) | \
5009                                       ((size) & PD_LOG_SIZE_MASK))
5010 #define PD_LOG_PORT(size_port) ((size_port) >> PD_LOG_PORT_SHIFT)
5011 #define PD_LOG_SIZE(size_port) ((size_port) & PD_LOG_SIZE_MASK)
5012
5013 /* PD event log : entry types */
5014 /* PD MCU events */
5015 #define PD_EVENT_MCU_BASE       0x00
5016 #define PD_EVENT_MCU_CHARGE             (PD_EVENT_MCU_BASE+0)
5017 #define PD_EVENT_MCU_CONNECT            (PD_EVENT_MCU_BASE+1)
5018 /* Reserved for custom board event */
5019 #define PD_EVENT_MCU_BOARD_CUSTOM       (PD_EVENT_MCU_BASE+2)
5020 /* PD generic accessory events */
5021 #define PD_EVENT_ACC_BASE       0x20
5022 #define PD_EVENT_ACC_RW_FAIL   (PD_EVENT_ACC_BASE+0)
5023 #define PD_EVENT_ACC_RW_ERASE  (PD_EVENT_ACC_BASE+1)
5024 /* PD power supply events */
5025 #define PD_EVENT_PS_BASE        0x40
5026 #define PD_EVENT_PS_FAULT      (PD_EVENT_PS_BASE+0)
5027 /* PD video dongles events */
5028 #define PD_EVENT_VIDEO_BASE     0x60
5029 #define PD_EVENT_VIDEO_DP_MODE (PD_EVENT_VIDEO_BASE+0)
5030 #define PD_EVENT_VIDEO_CODEC   (PD_EVENT_VIDEO_BASE+1)
5031 /* Returned in the "type" field, when there is no entry available */
5032 #define PD_EVENT_NO_ENTRY       0xff
5033
5034 /*
5035  * PD_EVENT_MCU_CHARGE event definition :
5036  * the payload is "struct usb_chg_measures"
5037  * the data field contains the port state flags as defined below :
5038  */
5039 /* Port partner is a dual role device */
5040 #define CHARGE_FLAGS_DUAL_ROLE         BIT(15)
5041 /* Port is the pending override port */
5042 #define CHARGE_FLAGS_DELAYED_OVERRIDE  BIT(14)
5043 /* Port is the override port */
5044 #define CHARGE_FLAGS_OVERRIDE          BIT(13)
5045 /* Charger type */
5046 #define CHARGE_FLAGS_TYPE_SHIFT               3
5047 #define CHARGE_FLAGS_TYPE_MASK       (0xf << CHARGE_FLAGS_TYPE_SHIFT)
5048 /* Power delivery role */
5049 #define CHARGE_FLAGS_ROLE_MASK         (7 <<  0)
5050
5051 /*
5052  * PD_EVENT_PS_FAULT data field flags definition :
5053  */
5054 #define PS_FAULT_OCP                          1
5055 #define PS_FAULT_FAST_OCP                     2
5056 #define PS_FAULT_OVP                          3
5057 #define PS_FAULT_DISCH                        4
5058
5059 /*
5060  * PD_EVENT_VIDEO_CODEC payload is "struct mcdp_info".
5061  */
5062 struct mcdp_version {
5063         uint8_t major;
5064         uint8_t minor;
5065         uint16_t build;
5066 } __ec_align4;
5067
5068 struct mcdp_info {
5069         uint8_t family[2];
5070         uint8_t chipid[2];
5071         struct mcdp_version irom;
5072         struct mcdp_version fw;
5073 } __ec_align4;
5074
5075 /* struct mcdp_info field decoding */
5076 #define MCDP_CHIPID(chipid) ((chipid[0] << 8) | chipid[1])
5077 #define MCDP_FAMILY(family) ((family[0] << 8) | family[1])
5078
5079 /* Get/Set USB-PD Alternate mode info */
5080 #define EC_CMD_USB_PD_GET_AMODE 0x0116
5081 struct ec_params_usb_pd_get_mode_request {
5082         uint16_t svid_idx; /* SVID index to get */
5083         uint8_t port;      /* port */
5084 } __ec_align_size1;
5085
5086 struct ec_params_usb_pd_get_mode_response {
5087         uint16_t svid;   /* SVID */
5088         uint16_t opos;    /* Object Position */
5089         uint32_t vdo[6]; /* Mode VDOs */
5090 } __ec_align4;
5091
5092 #define EC_CMD_USB_PD_SET_AMODE 0x0117
5093
5094 enum pd_mode_cmd {
5095         PD_EXIT_MODE = 0,
5096         PD_ENTER_MODE = 1,
5097         /* Not a command.  Do NOT remove. */
5098         PD_MODE_CMD_COUNT,
5099 };
5100
5101 struct ec_params_usb_pd_set_mode_request {
5102         uint32_t cmd;  /* enum pd_mode_cmd */
5103         uint16_t svid; /* SVID to set */
5104         uint8_t opos;  /* Object Position */
5105         uint8_t port;  /* port */
5106 } __ec_align4;
5107
5108 /* Ask the PD MCU to record a log of a requested type */
5109 #define EC_CMD_PD_WRITE_LOG_ENTRY 0x0118
5110
5111 struct ec_params_pd_write_log_entry {
5112         uint8_t type; /* event type : see PD_EVENT_xx above */
5113         uint8_t port; /* port#, or 0 for events unrelated to a given port */
5114 } __ec_align1;
5115
5116
5117 /* Control USB-PD chip */
5118 #define EC_CMD_PD_CONTROL 0x0119
5119
5120 enum ec_pd_control_cmd {
5121         PD_SUSPEND = 0,      /* Suspend the PD chip (EC: stop talking to PD) */
5122         PD_RESUME,           /* Resume the PD chip (EC: start talking to PD) */
5123         PD_RESET,            /* Force reset the PD chip */
5124         PD_CONTROL_DISABLE,  /* Disable further calls to this command */
5125         PD_CHIP_ON,          /* Power on the PD chip */
5126 };
5127
5128 struct ec_params_pd_control {
5129         uint8_t chip;         /* chip id */
5130         uint8_t subcmd;
5131 } __ec_align1;
5132
5133 /* Get info about USB-C SS muxes */
5134 #define EC_CMD_USB_PD_MUX_INFO 0x011A
5135
5136 struct ec_params_usb_pd_mux_info {
5137         uint8_t port; /* USB-C port number */
5138 } __ec_align1;
5139
5140 /* Flags representing mux state */
5141 #define USB_PD_MUX_USB_ENABLED       BIT(0) /* USB connected */
5142 #define USB_PD_MUX_DP_ENABLED        BIT(1) /* DP connected */
5143 #define USB_PD_MUX_POLARITY_INVERTED BIT(2) /* CC line Polarity inverted */
5144 #define USB_PD_MUX_HPD_IRQ           BIT(3) /* HPD IRQ is asserted */
5145 #define USB_PD_MUX_HPD_LVL           BIT(4) /* HPD level is asserted */
5146
5147 struct ec_response_usb_pd_mux_info {
5148         uint8_t flags; /* USB_PD_MUX_*-encoded USB mux state */
5149 } __ec_align1;
5150
5151 #define EC_CMD_PD_CHIP_INFO             0x011B
5152
5153 struct ec_params_pd_chip_info {
5154         uint8_t port;   /* USB-C port number */
5155         uint8_t renew;  /* Force renewal */
5156 } __ec_align1;
5157
5158 struct ec_response_pd_chip_info {
5159         uint16_t vendor_id;
5160         uint16_t product_id;
5161         uint16_t device_id;
5162         union {
5163                 uint8_t fw_version_string[8];
5164                 uint64_t fw_version_number;
5165         };
5166 } __ec_align2;
5167
5168 struct ec_response_pd_chip_info_v1 {
5169         uint16_t vendor_id;
5170         uint16_t product_id;
5171         uint16_t device_id;
5172         union {
5173                 uint8_t fw_version_string[8];
5174                 uint64_t fw_version_number;
5175         };
5176         union {
5177                 uint8_t min_req_fw_version_string[8];
5178                 uint64_t min_req_fw_version_number;
5179         };
5180 } __ec_align2;
5181
5182 /* Run RW signature verification and get status */
5183 #define EC_CMD_RWSIG_CHECK_STATUS       0x011C
5184
5185 struct ec_response_rwsig_check_status {
5186         uint32_t status;
5187 } __ec_align4;
5188
5189 /* For controlling RWSIG task */
5190 #define EC_CMD_RWSIG_ACTION     0x011D
5191
5192 enum rwsig_action {
5193         RWSIG_ACTION_ABORT = 0,         /* Abort RWSIG and prevent jumping */
5194         RWSIG_ACTION_CONTINUE = 1,      /* Jump to RW immediately */
5195 };
5196
5197 struct ec_params_rwsig_action {
5198         uint32_t action;
5199 } __ec_align4;
5200
5201 /* Run verification on a slot */
5202 #define EC_CMD_EFS_VERIFY       0x011E
5203
5204 struct ec_params_efs_verify {
5205         uint8_t region;         /* enum ec_flash_region */
5206 } __ec_align1;
5207
5208 /*
5209  * Retrieve info from Cros Board Info store. Response is based on the data
5210  * type. Integers return a uint32. Strings return a string, using the response
5211  * size to determine how big it is.
5212  */
5213 #define EC_CMD_GET_CROS_BOARD_INFO      0x011F
5214 /*
5215  * Write info into Cros Board Info on EEPROM. Write fails if the board has
5216  * hardware write-protect enabled.
5217  */
5218 #define EC_CMD_SET_CROS_BOARD_INFO      0x0120
5219
5220 enum cbi_data_tag {
5221         CBI_TAG_BOARD_VERSION = 0, /* uint32_t or smaller */
5222         CBI_TAG_OEM_ID = 1,        /* uint32_t or smaller */
5223         CBI_TAG_SKU_ID = 2,        /* uint32_t or smaller */
5224         CBI_TAG_DRAM_PART_NUM = 3, /* variable length ascii, nul terminated. */
5225         CBI_TAG_OEM_NAME = 4,      /* variable length ascii, nul terminated. */
5226         CBI_TAG_MODEL_ID = 5,      /* uint32_t or smaller */
5227         CBI_TAG_COUNT,
5228 };
5229
5230 /*
5231  * Flags to control read operation
5232  *
5233  * RELOAD:  Invalidate cache and read data from EEPROM. Useful to verify
5234  *          write was successful without reboot.
5235  */
5236 #define CBI_GET_RELOAD          BIT(0)
5237
5238 struct ec_params_get_cbi {
5239         uint32_t tag;           /* enum cbi_data_tag */
5240         uint32_t flag;          /* CBI_GET_* */
5241 } __ec_align4;
5242
5243 /*
5244  * Flags to control write behavior.
5245  *
5246  * NO_SYNC: Makes EC update data in RAM but skip writing to EEPROM. It's
5247  *          useful when writing multiple fields in a row.
5248  * INIT:    Need to be set when creating a new CBI from scratch. All fields
5249  *          will be initialized to zero first.
5250  */
5251 #define CBI_SET_NO_SYNC         BIT(0)
5252 #define CBI_SET_INIT            BIT(1)
5253
5254 struct ec_params_set_cbi {
5255         uint32_t tag;           /* enum cbi_data_tag */
5256         uint32_t flag;          /* CBI_SET_* */
5257         uint32_t size;          /* Data size */
5258         uint8_t data[];         /* For string and raw data */
5259 } __ec_align1;
5260
5261 /*
5262  * Information about resets of the AP by the EC and the EC's own uptime.
5263  */
5264 #define EC_CMD_GET_UPTIME_INFO 0x0121
5265
5266 struct ec_response_uptime_info {
5267         /*
5268          * Number of milliseconds since the last EC boot. Sysjump resets
5269          * typically do not restart the EC's time_since_boot epoch.
5270          *
5271          * WARNING: The EC's sense of time is much less accurate than the AP's
5272          * sense of time, in both phase and frequency.  This timebase is similar
5273          * to CLOCK_MONOTONIC_RAW, but with 1% or more frequency error.
5274          */
5275         uint32_t time_since_ec_boot_ms;
5276
5277         /*
5278          * Number of times the AP was reset by the EC since the last EC boot.
5279          * Note that the AP may be held in reset by the EC during the initial
5280          * boot sequence, such that the very first AP boot may count as more
5281          * than one here.
5282          */
5283         uint32_t ap_resets_since_ec_boot;
5284
5285         /*
5286          * The set of flags which describe the EC's most recent reset.  See
5287          * include/system.h RESET_FLAG_* for details.
5288          */
5289         uint32_t ec_reset_flags;
5290
5291         /* Empty log entries have both the cause and timestamp set to zero. */
5292         struct ap_reset_log_entry {
5293                 /*
5294                  * See include/chipset.h: enum chipset_{reset,shutdown}_reason
5295                  * for details.
5296                  */
5297                 uint16_t reset_cause;
5298
5299                 /* Reserved for protocol growth. */
5300                 uint16_t reserved;
5301
5302                 /*
5303                  * The time of the reset's assertion, in milliseconds since the
5304                  * last EC boot, in the same epoch as time_since_ec_boot_ms.
5305                  * Set to zero if the log entry is empty.
5306                  */
5307                 uint32_t reset_time_ms;
5308         } recent_ap_reset[4];
5309 } __ec_align4;
5310
5311 /*
5312  * Add entropy to the device secret (stored in the rollback region).
5313  *
5314  * Depending on the chip, the operation may take a long time (e.g. to erase
5315  * flash), so the commands are asynchronous.
5316  */
5317 #define EC_CMD_ADD_ENTROPY      0x0122
5318
5319 enum add_entropy_action {
5320         /* Add entropy to the current secret. */
5321         ADD_ENTROPY_ASYNC = 0,
5322         /*
5323          * Add entropy, and also make sure that the previous secret is erased.
5324          * (this can be implemented by adding entropy multiple times until
5325          * all rolback blocks have been overwritten).
5326          */
5327         ADD_ENTROPY_RESET_ASYNC = 1,
5328         /* Read back result from the previous operation. */
5329         ADD_ENTROPY_GET_RESULT = 2,
5330 };
5331
5332 struct ec_params_rollback_add_entropy {
5333         uint8_t action;
5334 } __ec_align1;
5335
5336 /*
5337  * Perform a single read of a given ADC channel.
5338  */
5339 #define EC_CMD_ADC_READ         0x0123
5340
5341 struct ec_params_adc_read {
5342         uint8_t adc_channel;
5343 } __ec_align1;
5344
5345 struct ec_response_adc_read {
5346         int32_t adc_value;
5347 } __ec_align4;
5348
5349 /*
5350  * Read back rollback info
5351  */
5352 #define EC_CMD_ROLLBACK_INFO            0x0124
5353
5354 struct ec_response_rollback_info {
5355         int32_t id; /* Incrementing number to indicate which region to use. */
5356         int32_t rollback_min_version;
5357         int32_t rw_rollback_version;
5358 } __ec_align4;
5359
5360
5361 /* Issue AP reset */
5362 #define EC_CMD_AP_RESET 0x0125
5363
5364 /*****************************************************************************/
5365 /* The command range 0x200-0x2FF is reserved for Rotor. */
5366
5367 /*****************************************************************************/
5368 /*
5369  * Reserve a range of host commands for the CR51 firmware.
5370  */
5371 #define EC_CMD_CR51_BASE 0x0300
5372 #define EC_CMD_CR51_LAST 0x03FF
5373
5374 /*****************************************************************************/
5375 /* Fingerprint MCU commands: range 0x0400-0x040x */
5376
5377 /* Fingerprint SPI sensor passthru command: prototyping ONLY */
5378 #define EC_CMD_FP_PASSTHRU 0x0400
5379
5380 #define EC_FP_FLAG_NOT_COMPLETE 0x1
5381
5382 struct ec_params_fp_passthru {
5383         uint16_t len;           /* Number of bytes to write then read */
5384         uint16_t flags;         /* EC_FP_FLAG_xxx */
5385         uint8_t data[];         /* Data to send */
5386 } __ec_align2;
5387
5388 /* Configure the Fingerprint MCU behavior */
5389 #define EC_CMD_FP_MODE 0x0402
5390
5391 /* Put the sensor in its lowest power mode */
5392 #define FP_MODE_DEEPSLEEP      BIT(0)
5393 /* Wait to see a finger on the sensor */
5394 #define FP_MODE_FINGER_DOWN    BIT(1)
5395 /* Poll until the finger has left the sensor */
5396 #define FP_MODE_FINGER_UP      BIT(2)
5397 /* Capture the current finger image */
5398 #define FP_MODE_CAPTURE        BIT(3)
5399 /* Finger enrollment session on-going */
5400 #define FP_MODE_ENROLL_SESSION BIT(4)
5401 /* Enroll the current finger image */
5402 #define FP_MODE_ENROLL_IMAGE   BIT(5)
5403 /* Try to match the current finger image */
5404 #define FP_MODE_MATCH          BIT(6)
5405 /* Reset and re-initialize the sensor. */
5406 #define FP_MODE_RESET_SENSOR   BIT(7)
5407 /* special value: don't change anything just read back current mode */
5408 #define FP_MODE_DONT_CHANGE    BIT(31)
5409
5410 #define FP_VALID_MODES (FP_MODE_DEEPSLEEP      | \
5411                         FP_MODE_FINGER_DOWN    | \
5412                         FP_MODE_FINGER_UP      | \
5413                         FP_MODE_CAPTURE        | \
5414                         FP_MODE_ENROLL_SESSION | \
5415                         FP_MODE_ENROLL_IMAGE   | \
5416                         FP_MODE_MATCH          | \
5417                         FP_MODE_RESET_SENSOR   | \
5418                         FP_MODE_DONT_CHANGE)
5419
5420 /* Capture types defined in bits [30..28] */
5421 #define FP_MODE_CAPTURE_TYPE_SHIFT 28
5422 #define FP_MODE_CAPTURE_TYPE_MASK  (0x7 << FP_MODE_CAPTURE_TYPE_SHIFT)
5423 /*
5424  * This enum must remain ordered, if you add new values you must ensure that
5425  * FP_CAPTURE_TYPE_MAX is still the last one.
5426  */
5427 enum fp_capture_type {
5428         /* Full blown vendor-defined capture (produces 'frame_size' bytes) */
5429         FP_CAPTURE_VENDOR_FORMAT = 0,
5430         /* Simple raw image capture (produces width x height x bpp bits) */
5431         FP_CAPTURE_SIMPLE_IMAGE = 1,
5432         /* Self test pattern (e.g. checkerboard) */
5433         FP_CAPTURE_PATTERN0 = 2,
5434         /* Self test pattern (e.g. inverted checkerboard) */
5435         FP_CAPTURE_PATTERN1 = 3,
5436         /* Capture for Quality test with fixed contrast */
5437         FP_CAPTURE_QUALITY_TEST = 4,
5438         /* Capture for pixel reset value test */
5439         FP_CAPTURE_RESET_TEST = 5,
5440         FP_CAPTURE_TYPE_MAX,
5441 };
5442 /* Extracts the capture type from the sensor 'mode' word */
5443 #define FP_CAPTURE_TYPE(mode) (((mode) & FP_MODE_CAPTURE_TYPE_MASK) \
5444                                        >> FP_MODE_CAPTURE_TYPE_SHIFT)
5445
5446 struct ec_params_fp_mode {
5447         uint32_t mode; /* as defined by FP_MODE_ constants */
5448 } __ec_align4;
5449
5450 struct ec_response_fp_mode {
5451         uint32_t mode; /* as defined by FP_MODE_ constants */
5452 } __ec_align4;
5453
5454 /* Retrieve Fingerprint sensor information */
5455 #define EC_CMD_FP_INFO 0x0403
5456
5457 /* Number of dead pixels detected on the last maintenance */
5458 #define FP_ERROR_DEAD_PIXELS(errors) ((errors) & 0x3FF)
5459 /* Unknown number of dead pixels detected on the last maintenance */
5460 #define FP_ERROR_DEAD_PIXELS_UNKNOWN (0x3FF)
5461 /* No interrupt from the sensor */
5462 #define FP_ERROR_NO_IRQ    BIT(12)
5463 /* SPI communication error */
5464 #define FP_ERROR_SPI_COMM  BIT(13)
5465 /* Invalid sensor Hardware ID */
5466 #define FP_ERROR_BAD_HWID  BIT(14)
5467 /* Sensor initialization failed */
5468 #define FP_ERROR_INIT_FAIL BIT(15)
5469
5470 struct ec_response_fp_info_v0 {
5471         /* Sensor identification */
5472         uint32_t vendor_id;
5473         uint32_t product_id;
5474         uint32_t model_id;
5475         uint32_t version;
5476         /* Image frame characteristics */
5477         uint32_t frame_size;
5478         uint32_t pixel_format; /* using V4L2_PIX_FMT_ */
5479         uint16_t width;
5480         uint16_t height;
5481         uint16_t bpp;
5482         uint16_t errors; /* see FP_ERROR_ flags above */
5483 } __ec_align4;
5484
5485 struct ec_response_fp_info {
5486         /* Sensor identification */
5487         uint32_t vendor_id;
5488         uint32_t product_id;
5489         uint32_t model_id;
5490         uint32_t version;
5491         /* Image frame characteristics */
5492         uint32_t frame_size;
5493         uint32_t pixel_format; /* using V4L2_PIX_FMT_ */
5494         uint16_t width;
5495         uint16_t height;
5496         uint16_t bpp;
5497         uint16_t errors; /* see FP_ERROR_ flags above */
5498         /* Template/finger current information */
5499         uint32_t template_size;  /* max template size in bytes */
5500         uint16_t template_max;   /* maximum number of fingers/templates */
5501         uint16_t template_valid; /* number of valid fingers/templates */
5502         uint32_t template_dirty; /* bitmap of templates with MCU side changes */
5503         uint32_t template_version; /* version of the template format */
5504 } __ec_align4;
5505
5506 /* Get the last captured finger frame or a template content */
5507 #define EC_CMD_FP_FRAME 0x0404
5508
5509 /* constants defining the 'offset' field which also contains the frame index */
5510 #define FP_FRAME_INDEX_SHIFT       28
5511 /* Frame buffer where the captured image is stored */
5512 #define FP_FRAME_INDEX_RAW_IMAGE    0
5513 /* First frame buffer holding a template */
5514 #define FP_FRAME_INDEX_TEMPLATE     1
5515 #define FP_FRAME_GET_BUFFER_INDEX(offset) ((offset) >> FP_FRAME_INDEX_SHIFT)
5516 #define FP_FRAME_OFFSET_MASK       0x0FFFFFFF
5517
5518 /* Version of the format of the encrypted templates. */
5519 #define FP_TEMPLATE_FORMAT_VERSION 3
5520
5521 /* Constants for encryption parameters */
5522 #define FP_CONTEXT_NONCE_BYTES 12
5523 #define FP_CONTEXT_USERID_WORDS (32 / sizeof(uint32_t))
5524 #define FP_CONTEXT_TAG_BYTES 16
5525 #define FP_CONTEXT_SALT_BYTES 16
5526 #define FP_CONTEXT_TPM_BYTES 32
5527
5528 struct ec_fp_template_encryption_metadata {
5529         /*
5530          * Version of the structure format (N=3).
5531          */
5532         uint16_t struct_version;
5533         /* Reserved bytes, set to 0. */
5534         uint16_t reserved;
5535         /*
5536          * The salt is *only* ever used for key derivation. The nonce is unique,
5537          * a different one is used for every message.
5538          */
5539         uint8_t nonce[FP_CONTEXT_NONCE_BYTES];
5540         uint8_t salt[FP_CONTEXT_SALT_BYTES];
5541         uint8_t tag[FP_CONTEXT_TAG_BYTES];
5542 };
5543
5544 struct ec_params_fp_frame {
5545         /*
5546          * The offset contains the template index or FP_FRAME_INDEX_RAW_IMAGE
5547          * in the high nibble, and the real offset within the frame in
5548          * FP_FRAME_OFFSET_MASK.
5549          */
5550         uint32_t offset;
5551         uint32_t size;
5552 } __ec_align4;
5553
5554 /* Load a template into the MCU */
5555 #define EC_CMD_FP_TEMPLATE 0x0405
5556
5557 /* Flag in the 'size' field indicating that the full template has been sent */
5558 #define FP_TEMPLATE_COMMIT 0x80000000
5559
5560 struct ec_params_fp_template {
5561         uint32_t offset;
5562         uint32_t size;
5563         uint8_t data[];
5564 } __ec_align4;
5565
5566 /* Clear the current fingerprint user context and set a new one */
5567 #define EC_CMD_FP_CONTEXT 0x0406
5568
5569 struct ec_params_fp_context {
5570         uint32_t userid[FP_CONTEXT_USERID_WORDS];
5571 } __ec_align4;
5572
5573 #define EC_CMD_FP_STATS 0x0407
5574
5575 #define FPSTATS_CAPTURE_INV  BIT(0)
5576 #define FPSTATS_MATCHING_INV BIT(1)
5577
5578 struct ec_response_fp_stats {
5579         uint32_t capture_time_us;
5580         uint32_t matching_time_us;
5581         uint32_t overall_time_us;
5582         struct {
5583                 uint32_t lo;
5584                 uint32_t hi;
5585         } overall_t0;
5586         uint8_t timestamps_invalid;
5587         int8_t template_matched;
5588 } __ec_align2;
5589
5590 #define EC_CMD_FP_SEED 0x0408
5591 struct ec_params_fp_seed {
5592         /*
5593          * Version of the structure format (N=3).
5594          */
5595         uint16_t struct_version;
5596         /* Reserved bytes, set to 0. */
5597         uint16_t reserved;
5598         /* Seed from the TPM. */
5599         uint8_t seed[FP_CONTEXT_TPM_BYTES];
5600 } __ec_align4;
5601
5602 #define EC_CMD_FP_ENC_STATUS 0x0409
5603
5604 /* FP TPM seed has been set or not */
5605 #define FP_ENC_STATUS_SEED_SET BIT(0)
5606
5607 struct ec_response_fp_encryption_status {
5608         /* Used bits in encryption engine status */
5609         uint32_t valid_flags;
5610         /* Encryption engine status */
5611         uint32_t status;
5612 } __ec_align4;
5613
5614 /*****************************************************************************/
5615 /* Touchpad MCU commands: range 0x0500-0x05FF */
5616
5617 /* Perform touchpad self test */
5618 #define EC_CMD_TP_SELF_TEST 0x0500
5619
5620 /* Get number of frame types, and the size of each type */
5621 #define EC_CMD_TP_FRAME_INFO 0x0501
5622
5623 struct ec_response_tp_frame_info {
5624         uint32_t n_frames;
5625         uint32_t frame_sizes[0];
5626 } __ec_align4;
5627
5628 /* Create a snapshot of current frame readings */
5629 #define EC_CMD_TP_FRAME_SNAPSHOT 0x0502
5630
5631 /* Read the frame */
5632 #define EC_CMD_TP_FRAME_GET 0x0503
5633
5634 struct ec_params_tp_frame_get {
5635         uint32_t frame_index;
5636         uint32_t offset;
5637         uint32_t size;
5638 } __ec_align4;
5639
5640 /*****************************************************************************/
5641 /* EC-EC communication commands: range 0x0600-0x06FF */
5642
5643 #define EC_COMM_TEXT_MAX 8
5644
5645 /*
5646  * Get battery static information, i.e. information that never changes, or
5647  * very infrequently.
5648  */
5649 #define EC_CMD_BATTERY_GET_STATIC 0x0600
5650
5651 /**
5652  * struct ec_params_battery_static_info - Battery static info parameters
5653  * @index: Battery index.
5654  */
5655 struct ec_params_battery_static_info {
5656         uint8_t index;
5657 } __ec_align_size1;
5658
5659 /**
5660  * struct ec_response_battery_static_info - Battery static info response
5661  * @design_capacity: Battery Design Capacity (mAh)
5662  * @design_voltage: Battery Design Voltage (mV)
5663  * @manufacturer: Battery Manufacturer String
5664  * @model: Battery Model Number String
5665  * @serial: Battery Serial Number String
5666  * @type: Battery Type String
5667  * @cycle_count: Battery Cycle Count
5668  */
5669 struct ec_response_battery_static_info {
5670         uint16_t design_capacity;
5671         uint16_t design_voltage;
5672         char manufacturer[EC_COMM_TEXT_MAX];
5673         char model[EC_COMM_TEXT_MAX];
5674         char serial[EC_COMM_TEXT_MAX];
5675         char type[EC_COMM_TEXT_MAX];
5676         /* TODO(crbug.com/795991): Consider moving to dynamic structure. */
5677         uint32_t cycle_count;
5678 } __ec_align4;
5679
5680 /*
5681  * Get battery dynamic information, i.e. information that is likely to change
5682  * every time it is read.
5683  */
5684 #define EC_CMD_BATTERY_GET_DYNAMIC 0x0601
5685
5686 /**
5687  * struct ec_params_battery_dynamic_info - Battery dynamic info parameters
5688  * @index: Battery index.
5689  */
5690 struct ec_params_battery_dynamic_info {
5691         uint8_t index;
5692 } __ec_align_size1;
5693
5694 /**
5695  * struct ec_response_battery_dynamic_info - Battery dynamic info response
5696  * @actual_voltage: Battery voltage (mV)
5697  * @actual_current: Battery current (mA); negative=discharging
5698  * @remaining_capacity: Remaining capacity (mAh)
5699  * @full_capacity: Capacity (mAh, might change occasionally)
5700  * @flags: Flags, see EC_BATT_FLAG_*
5701  * @desired_voltage: Charging voltage desired by battery (mV)
5702  * @desired_current: Charging current desired by battery (mA)
5703  */
5704 struct ec_response_battery_dynamic_info {
5705         int16_t actual_voltage;
5706         int16_t actual_current;
5707         int16_t remaining_capacity;
5708         int16_t full_capacity;
5709         int16_t flags;
5710         int16_t desired_voltage;
5711         int16_t desired_current;
5712 } __ec_align2;
5713
5714 /*
5715  * Control charger chip. Used to control charger chip on the slave.
5716  */
5717 #define EC_CMD_CHARGER_CONTROL 0x0602
5718
5719 /**
5720  * struct ec_params_charger_control - Charger control parameters
5721  * @max_current: Charger current (mA). Positive to allow base to draw up to
5722  *     max_current and (possibly) charge battery, negative to request current
5723  *     from base (OTG).
5724  * @otg_voltage: Voltage (mV) to use in OTG mode, ignored if max_current is
5725  *     >= 0.
5726  * @allow_charging: Allow base battery charging (only makes sense if
5727  *     max_current > 0).
5728  */
5729 struct ec_params_charger_control {
5730         int16_t max_current;
5731         uint16_t otg_voltage;
5732         uint8_t allow_charging;
5733 } __ec_align_size1;
5734
5735 /*****************************************************************************/
5736 /*
5737  * Reserve a range of host commands for board-specific, experimental, or
5738  * special purpose features. These can be (re)used without updating this file.
5739  *
5740  * CAUTION: Don't go nuts with this. Shipping products should document ALL
5741  * their EC commands for easier development, testing, debugging, and support.
5742  *
5743  * All commands MUST be #defined to be 4-digit UPPER CASE hex values
5744  * (e.g., 0x00AB, not 0xab) for CONFIG_HOSTCMD_SECTION_SORTED to work.
5745  *
5746  * In your experimental code, you may want to do something like this:
5747  *
5748  *   #define EC_CMD_MAGIC_FOO 0x0000
5749  *   #define EC_CMD_MAGIC_BAR 0x0001
5750  *   #define EC_CMD_MAGIC_HEY 0x0002
5751  *
5752  *   DECLARE_PRIVATE_HOST_COMMAND(EC_CMD_MAGIC_FOO, magic_foo_handler,
5753  *      EC_VER_MASK(0);
5754  *
5755  *   DECLARE_PRIVATE_HOST_COMMAND(EC_CMD_MAGIC_BAR, magic_bar_handler,
5756  *      EC_VER_MASK(0);
5757  *
5758  *   DECLARE_PRIVATE_HOST_COMMAND(EC_CMD_MAGIC_HEY, magic_hey_handler,
5759  *      EC_VER_MASK(0);
5760  */
5761 #define EC_CMD_BOARD_SPECIFIC_BASE 0x3E00
5762 #define EC_CMD_BOARD_SPECIFIC_LAST 0x3FFF
5763
5764 /*
5765  * Given the private host command offset, calculate the true private host
5766  * command value.
5767  */
5768 #define EC_PRIVATE_HOST_COMMAND_VALUE(command) \
5769         (EC_CMD_BOARD_SPECIFIC_BASE + (command))
5770
5771 /*****************************************************************************/
5772 /*
5773  * Passthru commands
5774  *
5775  * Some platforms have sub-processors chained to each other.  For example.
5776  *
5777  *     AP <--> EC <--> PD MCU
5778  *
5779  * The top 2 bits of the command number are used to indicate which device the
5780  * command is intended for.  Device 0 is always the device receiving the
5781  * command; other device mapping is board-specific.
5782  *
5783  * When a device receives a command to be passed to a sub-processor, it passes
5784  * it on with the device number set back to 0.  This allows the sub-processor
5785  * to remain blissfully unaware of whether the command originated on the next
5786  * device up the chain, or was passed through from the AP.
5787  *
5788  * In the above example, if the AP wants to send command 0x0002 to the PD MCU,
5789  *     AP sends command 0x4002 to the EC
5790  *     EC sends command 0x0002 to the PD MCU
5791  *     EC forwards PD MCU response back to the AP
5792  */
5793
5794 /* Offset and max command number for sub-device n */
5795 #define EC_CMD_PASSTHRU_OFFSET(n) (0x4000 * (n))
5796 #define EC_CMD_PASSTHRU_MAX(n) (EC_CMD_PASSTHRU_OFFSET(n) + 0x3fff)
5797
5798 /*****************************************************************************/
5799 /*
5800  * Deprecated constants. These constants have been renamed for clarity. The
5801  * meaning and size has not changed. Programs that use the old names should
5802  * switch to the new names soon, as the old names may not be carried forward
5803  * forever.
5804  */
5805 #define EC_HOST_PARAM_SIZE      EC_PROTO2_MAX_PARAM_SIZE
5806 #define EC_LPC_ADDR_OLD_PARAM   EC_HOST_CMD_REGION1
5807 #define EC_OLD_PARAM_SIZE       EC_HOST_CMD_REGION_SIZE
5808
5809
5810
5811 #endif  /* __CROS_EC_COMMANDS_H */