]> asedeno.scripts.mit.edu Git - linux.git/blob - include/linux/mfd/cros_ec_commands.h
c12ae9742e20b6da30af08d2e448d81744a9a374
[linux.git] / include / linux / mfd / cros_ec_commands.h
1 /* SPDX-License-Identifier: GPL-2.0 */
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) (1UL << (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        (1 << 0)  /* Data ready for host to read */
61 #define EC_LPC_CMDR_PENDING     (1 << 1)  /* Write pending to EC */
62 #define EC_LPC_CMDR_BUSY        (1 << 2)  /* EC is busy processing a command */
63 #define EC_LPC_CMDR_CMD         (1 << 3)  /* Last host write was a command */
64 #define EC_LPC_CMDR_ACPI_BRST   (1 << 4)  /* Burst mode (not used) */
65 #define EC_LPC_CMDR_SCI         (1 << 5)  /* SCI event is pending */
66 #define EC_LPC_CMDR_SMI         (1 << 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 /* 32 bits */
87 /* Reserve 0x38 - 0x3f for additional host event-related stuff */
88 /* Battery values are all 32 bits */
89 #define EC_MEMMAP_BATT_VOLT        0x40 /* Battery Present Voltage */
90 #define EC_MEMMAP_BATT_RATE        0x44 /* Battery Present Rate */
91 #define EC_MEMMAP_BATT_CAP         0x48 /* Battery Remaining Capacity */
92 #define EC_MEMMAP_BATT_FLAG        0x4c /* Battery State, defined below */
93 #define EC_MEMMAP_BATT_DCAP        0x50 /* Battery Design Capacity */
94 #define EC_MEMMAP_BATT_DVLT        0x54 /* Battery Design Voltage */
95 #define EC_MEMMAP_BATT_LFCC        0x58 /* Battery Last Full Charge Capacity */
96 #define EC_MEMMAP_BATT_CCNT        0x5c /* Battery Cycle Count */
97 /* Strings are all 8 bytes (EC_MEMMAP_TEXT_MAX) */
98 #define EC_MEMMAP_BATT_MFGR        0x60 /* Battery Manufacturer String */
99 #define EC_MEMMAP_BATT_MODEL       0x68 /* Battery Model Number String */
100 #define EC_MEMMAP_BATT_SERIAL      0x70 /* Battery Serial Number String */
101 #define EC_MEMMAP_BATT_TYPE        0x78 /* Battery Type String */
102 #define EC_MEMMAP_ALS              0x80 /* ALS readings in lux (2 X 16 bits) */
103 /* Unused 0x84 - 0x8f */
104 #define EC_MEMMAP_ACC_STATUS       0x90 /* Accelerometer status (8 bits )*/
105 /* Unused 0x91 */
106 #define EC_MEMMAP_ACC_DATA         0x92 /* Accelerometer data 0x92 - 0x9f */
107 #define EC_MEMMAP_GYRO_DATA        0xa0 /* Gyroscope data 0xa0 - 0xa5 */
108 /* Unused 0xa6 - 0xfe (remember, 0xff is NOT part of the memmap region) */
109
110
111 /* Define the format of the accelerometer mapped memory status byte. */
112 #define EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK  0x0f
113 #define EC_MEMMAP_ACC_STATUS_BUSY_BIT        (1 << 4)
114 #define EC_MEMMAP_ACC_STATUS_PRESENCE_BIT    (1 << 7)
115
116 /* Number of temp sensors at EC_MEMMAP_TEMP_SENSOR */
117 #define EC_TEMP_SENSOR_ENTRIES     16
118 /*
119  * Number of temp sensors at EC_MEMMAP_TEMP_SENSOR_B.
120  *
121  * Valid only if EC_MEMMAP_THERMAL_VERSION returns >= 2.
122  */
123 #define EC_TEMP_SENSOR_B_ENTRIES      8
124
125 /* Special values for mapped temperature sensors */
126 #define EC_TEMP_SENSOR_NOT_PRESENT    0xff
127 #define EC_TEMP_SENSOR_ERROR          0xfe
128 #define EC_TEMP_SENSOR_NOT_POWERED    0xfd
129 #define EC_TEMP_SENSOR_NOT_CALIBRATED 0xfc
130 /*
131  * The offset of temperature value stored in mapped memory.  This allows
132  * reporting a temperature range of 200K to 454K = -73C to 181C.
133  */
134 #define EC_TEMP_SENSOR_OFFSET      200
135
136 /*
137  * Number of ALS readings at EC_MEMMAP_ALS
138  */
139 #define EC_ALS_ENTRIES             2
140
141 /*
142  * The default value a temperature sensor will return when it is present but
143  * has not been read this boot.  This is a reasonable number to avoid
144  * triggering alarms on the host.
145  */
146 #define EC_TEMP_SENSOR_DEFAULT     (296 - EC_TEMP_SENSOR_OFFSET)
147
148 #define EC_FAN_SPEED_ENTRIES       4       /* Number of fans at EC_MEMMAP_FAN */
149 #define EC_FAN_SPEED_NOT_PRESENT   0xffff  /* Entry not present */
150 #define EC_FAN_SPEED_STALLED       0xfffe  /* Fan stalled */
151
152 /* Battery bit flags at EC_MEMMAP_BATT_FLAG. */
153 #define EC_BATT_FLAG_AC_PRESENT   0x01
154 #define EC_BATT_FLAG_BATT_PRESENT 0x02
155 #define EC_BATT_FLAG_DISCHARGING  0x04
156 #define EC_BATT_FLAG_CHARGING     0x08
157 #define EC_BATT_FLAG_LEVEL_CRITICAL 0x10
158
159 /* Switch flags at EC_MEMMAP_SWITCHES */
160 #define EC_SWITCH_LID_OPEN               0x01
161 #define EC_SWITCH_POWER_BUTTON_PRESSED   0x02
162 #define EC_SWITCH_WRITE_PROTECT_DISABLED 0x04
163 /* Was recovery requested via keyboard; now unused. */
164 #define EC_SWITCH_IGNORE1                0x08
165 /* Recovery requested via dedicated signal (from servo board) */
166 #define EC_SWITCH_DEDICATED_RECOVERY     0x10
167 /* Was fake developer mode switch; now unused.  Remove in next refactor. */
168 #define EC_SWITCH_IGNORE0                0x20
169
170 /* Host command interface flags */
171 /* Host command interface supports LPC args (LPC interface only) */
172 #define EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED  0x01
173 /* Host command interface supports version 3 protocol */
174 #define EC_HOST_CMD_FLAG_VERSION_3   0x02
175
176 /* Wireless switch flags */
177 #define EC_WIRELESS_SWITCH_ALL       ~0x00  /* All flags */
178 #define EC_WIRELESS_SWITCH_WLAN       0x01  /* WLAN radio */
179 #define EC_WIRELESS_SWITCH_BLUETOOTH  0x02  /* Bluetooth radio */
180 #define EC_WIRELESS_SWITCH_WWAN       0x04  /* WWAN power */
181 #define EC_WIRELESS_SWITCH_WLAN_POWER 0x08  /* WLAN power */
182
183 /*
184  * This header file is used in coreboot both in C and ACPI code.  The ACPI code
185  * is pre-processed to handle constants but the ASL compiler is unable to
186  * handle actual C code so keep it separate.
187  */
188 #ifndef __ACPI__
189
190 /*
191  * Attributes for EC request and response packets.  Just defining __packed
192  * results in inefficient assembly code on ARM, if the structure is actually
193  * 32-bit aligned, as it should be for all buffers.
194  *
195  * Be very careful when adding these to existing structures.  They will round
196  * up the structure size to the specified boundary.
197  *
198  * Also be very careful to make that if a structure is included in some other
199  * parent structure that the alignment will still be true given the packing of
200  * the parent structure.  This is particularly important if the sub-structure
201  * will be passed as a pointer to another function, since that function will
202  * not know about the misaligment caused by the parent structure's packing.
203  *
204  * Also be very careful using __packed - particularly when nesting non-packed
205  * structures inside packed ones.  In fact, DO NOT use __packed directly;
206  * always use one of these attributes.
207  *
208  * Once everything is annotated properly, the following search strings should
209  * not return ANY matches in this file other than right here:
210  *
211  * "__packed" - generates inefficient code; all sub-structs must also be packed
212  *
213  * "struct [^_]" - all structs should be annotated, except for structs that are
214  * members of other structs/unions (and their original declarations should be
215  * annotated).
216  */
217
218 /*
219  * Packed structures make no assumption about alignment, so they do inefficient
220  * byte-wise reads.
221  */
222 #define __ec_align1 __packed
223 #define __ec_align2 __packed
224 #define __ec_align4 __packed
225 #define __ec_align_size1 __packed
226 #define __ec_align_offset1 __packed
227 #define __ec_align_offset2 __packed
228 #define __ec_todo_packed __packed
229 #define __ec_todo_unpacked
230
231
232 /* LPC command status byte masks */
233 /* EC has written a byte in the data register and host hasn't read it yet */
234 #define EC_LPC_STATUS_TO_HOST     0x01
235 /* Host has written a command/data byte and the EC hasn't read it yet */
236 #define EC_LPC_STATUS_FROM_HOST   0x02
237 /* EC is processing a command */
238 #define EC_LPC_STATUS_PROCESSING  0x04
239 /* Last write to EC was a command, not data */
240 #define EC_LPC_STATUS_LAST_CMD    0x08
241 /* EC is in burst mode.  Unsupported by Chrome EC, so this bit is never set */
242 #define EC_LPC_STATUS_BURST_MODE  0x10
243 /* SCI event is pending (requesting SCI query) */
244 #define EC_LPC_STATUS_SCI_PENDING 0x20
245 /* SMI event is pending (requesting SMI query) */
246 #define EC_LPC_STATUS_SMI_PENDING 0x40
247 /* (reserved) */
248 #define EC_LPC_STATUS_RESERVED    0x80
249
250 /*
251  * EC is busy.  This covers both the EC processing a command, and the host has
252  * written a new command but the EC hasn't picked it up yet.
253  */
254 #define EC_LPC_STATUS_BUSY_MASK \
255         (EC_LPC_STATUS_FROM_HOST | EC_LPC_STATUS_PROCESSING)
256
257 /* Host command response codes */
258 enum ec_status {
259         EC_RES_SUCCESS = 0,
260         EC_RES_INVALID_COMMAND = 1,
261         EC_RES_ERROR = 2,
262         EC_RES_INVALID_PARAM = 3,
263         EC_RES_ACCESS_DENIED = 4,
264         EC_RES_INVALID_RESPONSE = 5,
265         EC_RES_INVALID_VERSION = 6,
266         EC_RES_INVALID_CHECKSUM = 7,
267         EC_RES_IN_PROGRESS = 8,         /* Accepted, command in progress */
268         EC_RES_UNAVAILABLE = 9,         /* No response available */
269         EC_RES_TIMEOUT = 10,            /* We got a timeout */
270         EC_RES_OVERFLOW = 11,           /* Table / data overflow */
271         EC_RES_INVALID_HEADER = 12,     /* Header contains invalid data */
272         EC_RES_REQUEST_TRUNCATED = 13,  /* Didn't get the entire request */
273         EC_RES_RESPONSE_TOO_BIG = 14    /* Response was too big to handle */
274 };
275
276 /*
277  * Host event codes.  Note these are 1-based, not 0-based, because ACPI query
278  * EC command uses code 0 to mean "no event pending".  We explicitly specify
279  * each value in the enum listing so they won't change if we delete/insert an
280  * item or rearrange the list (it needs to be stable across platforms, not
281  * just within a single compiled instance).
282  */
283 enum host_event_code {
284         EC_HOST_EVENT_LID_CLOSED = 1,
285         EC_HOST_EVENT_LID_OPEN = 2,
286         EC_HOST_EVENT_POWER_BUTTON = 3,
287         EC_HOST_EVENT_AC_CONNECTED = 4,
288         EC_HOST_EVENT_AC_DISCONNECTED = 5,
289         EC_HOST_EVENT_BATTERY_LOW = 6,
290         EC_HOST_EVENT_BATTERY_CRITICAL = 7,
291         EC_HOST_EVENT_BATTERY = 8,
292         EC_HOST_EVENT_THERMAL_THRESHOLD = 9,
293         EC_HOST_EVENT_THERMAL_OVERLOAD = 10,
294         EC_HOST_EVENT_THERMAL = 11,
295         EC_HOST_EVENT_USB_CHARGER = 12,
296         EC_HOST_EVENT_KEY_PRESSED = 13,
297         /*
298          * EC has finished initializing the host interface.  The host can check
299          * for this event following sending a EC_CMD_REBOOT_EC command to
300          * determine when the EC is ready to accept subsequent commands.
301          */
302         EC_HOST_EVENT_INTERFACE_READY = 14,
303         /* Keyboard recovery combo has been pressed */
304         EC_HOST_EVENT_KEYBOARD_RECOVERY = 15,
305
306         /* Shutdown due to thermal overload */
307         EC_HOST_EVENT_THERMAL_SHUTDOWN = 16,
308         /* Shutdown due to battery level too low */
309         EC_HOST_EVENT_BATTERY_SHUTDOWN = 17,
310
311         /* Suggest that the AP throttle itself */
312         EC_HOST_EVENT_THROTTLE_START = 18,
313         /* Suggest that the AP resume normal speed */
314         EC_HOST_EVENT_THROTTLE_STOP = 19,
315
316         /* Hang detect logic detected a hang and host event timeout expired */
317         EC_HOST_EVENT_HANG_DETECT = 20,
318         /* Hang detect logic detected a hang and warm rebooted the AP */
319         EC_HOST_EVENT_HANG_REBOOT = 21,
320         /* PD MCU triggering host event */
321         EC_HOST_EVENT_PD_MCU = 22,
322
323         /* EC desires to change state of host-controlled USB mux */
324         EC_HOST_EVENT_USB_MUX = 28,
325
326         /* EC RTC event occurred */
327         EC_HOST_EVENT_RTC = 26,
328
329         /*
330          * The high bit of the event mask is not used as a host event code.  If
331          * it reads back as set, then the entire event mask should be
332          * considered invalid by the host.  This can happen when reading the
333          * raw event status via EC_MEMMAP_HOST_EVENTS but the LPC interface is
334          * not initialized on the EC, or improperly configured on the host.
335          */
336         EC_HOST_EVENT_INVALID = 32
337 };
338 /* Host event mask */
339 #define EC_HOST_EVENT_MASK(event_code) (1UL << ((event_code) - 1))
340
341 /**
342  * struct ec_lpc_host_args - Arguments at EC_LPC_ADDR_HOST_ARGS
343  * @flags: The host argument flags.
344  * @command_version: Command version.
345  * @data_size: The length of data.
346  * @checksum: Checksum; sum of command + flags + command_version + data_size +
347  *            all params/response data bytes.
348  */
349 struct ec_lpc_host_args {
350         uint8_t flags;
351         uint8_t command_version;
352         uint8_t data_size;
353         uint8_t checksum;
354 } __ec_align4;
355
356 /* Flags for ec_lpc_host_args.flags */
357 /*
358  * Args are from host.  Data area at EC_LPC_ADDR_HOST_PARAM contains command
359  * params.
360  *
361  * If EC gets a command and this flag is not set, this is an old-style command.
362  * Command version is 0 and params from host are at EC_LPC_ADDR_OLD_PARAM with
363  * unknown length.  EC must respond with an old-style response (that is,
364  * without setting EC_HOST_ARGS_FLAG_TO_HOST).
365  */
366 #define EC_HOST_ARGS_FLAG_FROM_HOST 0x01
367 /*
368  * Args are from EC.  Data area at EC_LPC_ADDR_HOST_PARAM contains response.
369  *
370  * If EC responds to a command and this flag is not set, this is an old-style
371  * response.  Command version is 0 and response data from EC is at
372  * EC_LPC_ADDR_OLD_PARAM with unknown length.
373  */
374 #define EC_HOST_ARGS_FLAG_TO_HOST   0x02
375
376 /*****************************************************************************/
377 /*
378  * Byte codes returned by EC over SPI interface.
379  *
380  * These can be used by the AP to debug the EC interface, and to determine
381  * when the EC is not in a state where it will ever get around to responding
382  * to the AP.
383  *
384  * Example of sequence of bytes read from EC for a current good transfer:
385  *   1. -                  - AP asserts chip select (CS#)
386  *   2. EC_SPI_OLD_READY   - AP sends first byte(s) of request
387  *   3. -                  - EC starts handling CS# interrupt
388  *   4. EC_SPI_RECEIVING   - AP sends remaining byte(s) of request
389  *   5. EC_SPI_PROCESSING  - EC starts processing request; AP is clocking in
390  *                           bytes looking for EC_SPI_FRAME_START
391  *   6. -                  - EC finishes processing and sets up response
392  *   7. EC_SPI_FRAME_START - AP reads frame byte
393  *   8. (response packet)  - AP reads response packet
394  *   9. EC_SPI_PAST_END    - Any additional bytes read by AP
395  *   10 -                  - AP deasserts chip select
396  *   11 -                  - EC processes CS# interrupt and sets up DMA for
397  *                           next request
398  *
399  * If the AP is waiting for EC_SPI_FRAME_START and sees any value other than
400  * the following byte values:
401  *   EC_SPI_OLD_READY
402  *   EC_SPI_RX_READY
403  *   EC_SPI_RECEIVING
404  *   EC_SPI_PROCESSING
405  *
406  * Then the EC found an error in the request, or was not ready for the request
407  * and lost data.  The AP should give up waiting for EC_SPI_FRAME_START,
408  * because the EC is unable to tell when the AP is done sending its request.
409  */
410
411 /*
412  * Framing byte which precedes a response packet from the EC.  After sending a
413  * request, the AP will clock in bytes until it sees the framing byte, then
414  * clock in the response packet.
415  */
416 #define EC_SPI_FRAME_START    0xec
417
418 /*
419  * Padding bytes which are clocked out after the end of a response packet.
420  */
421 #define EC_SPI_PAST_END       0xed
422
423 /*
424  * EC is ready to receive, and has ignored the byte sent by the AP.  EC expects
425  * that the AP will send a valid packet header (starting with
426  * EC_COMMAND_PROTOCOL_3) in the next 32 bytes.
427  */
428 #define EC_SPI_RX_READY       0xf8
429
430 /*
431  * EC has started receiving the request from the AP, but hasn't started
432  * processing it yet.
433  */
434 #define EC_SPI_RECEIVING      0xf9
435
436 /* EC has received the entire request from the AP and is processing it. */
437 #define EC_SPI_PROCESSING     0xfa
438
439 /*
440  * EC received bad data from the AP, such as a packet header with an invalid
441  * length.  EC will ignore all data until chip select deasserts.
442  */
443 #define EC_SPI_RX_BAD_DATA    0xfb
444
445 /*
446  * EC received data from the AP before it was ready.  That is, the AP asserted
447  * chip select and started clocking data before the EC was ready to receive it.
448  * EC will ignore all data until chip select deasserts.
449  */
450 #define EC_SPI_NOT_READY      0xfc
451
452 /*
453  * EC was ready to receive a request from the AP.  EC has treated the byte sent
454  * by the AP as part of a request packet, or (for old-style ECs) is processing
455  * a fully received packet but is not ready to respond yet.
456  */
457 #define EC_SPI_OLD_READY      0xfd
458
459 /*****************************************************************************/
460
461 /*
462  * Protocol version 2 for I2C and SPI send a request this way:
463  *
464  *      0       EC_CMD_VERSION0 + (command version)
465  *      1       Command number
466  *      2       Length of params = N
467  *      3..N+2  Params, if any
468  *      N+3     8-bit checksum of bytes 0..N+2
469  *
470  * The corresponding response is:
471  *
472  *      0       Result code (EC_RES_*)
473  *      1       Length of params = M
474  *      2..M+1  Params, if any
475  *      M+2     8-bit checksum of bytes 0..M+1
476  */
477 #define EC_PROTO2_REQUEST_HEADER_BYTES 3
478 #define EC_PROTO2_REQUEST_TRAILER_BYTES 1
479 #define EC_PROTO2_REQUEST_OVERHEAD (EC_PROTO2_REQUEST_HEADER_BYTES +    \
480                                     EC_PROTO2_REQUEST_TRAILER_BYTES)
481
482 #define EC_PROTO2_RESPONSE_HEADER_BYTES 2
483 #define EC_PROTO2_RESPONSE_TRAILER_BYTES 1
484 #define EC_PROTO2_RESPONSE_OVERHEAD (EC_PROTO2_RESPONSE_HEADER_BYTES +  \
485                                      EC_PROTO2_RESPONSE_TRAILER_BYTES)
486
487 /* Parameter length was limited by the LPC interface */
488 #define EC_PROTO2_MAX_PARAM_SIZE 0xfc
489
490 /* Maximum request and response packet sizes for protocol version 2 */
491 #define EC_PROTO2_MAX_REQUEST_SIZE (EC_PROTO2_REQUEST_OVERHEAD +        \
492                                     EC_PROTO2_MAX_PARAM_SIZE)
493 #define EC_PROTO2_MAX_RESPONSE_SIZE (EC_PROTO2_RESPONSE_OVERHEAD +      \
494                                      EC_PROTO2_MAX_PARAM_SIZE)
495
496 /*****************************************************************************/
497
498 /*
499  * Value written to legacy command port / prefix byte to indicate protocol
500  * 3+ structs are being used.  Usage is bus-dependent.
501  */
502 #define EC_COMMAND_PROTOCOL_3 0xda
503
504 #define EC_HOST_REQUEST_VERSION 3
505
506 /**
507  * struct ec_host_request - Version 3 request from host.
508  * @struct_version: Should be 3. The EC will return EC_RES_INVALID_HEADER if it
509  *                  receives a header with a version it doesn't know how to
510  *                  parse.
511  * @checksum: Checksum of request and data; sum of all bytes including checksum
512  *            should total to 0.
513  * @command: Command to send (EC_CMD_...)
514  * @command_version: Command version.
515  * @reserved: Unused byte in current protocol version; set to 0.
516  * @data_len: Length of data which follows this header.
517  */
518 struct ec_host_request {
519         uint8_t struct_version;
520         uint8_t checksum;
521         uint16_t command;
522         uint8_t command_version;
523         uint8_t reserved;
524         uint16_t data_len;
525 } __ec_align4;
526
527 #define EC_HOST_RESPONSE_VERSION 3
528
529 /**
530  * struct ec_host_response - Version 3 response from EC.
531  * @struct_version: Struct version (=3).
532  * @checksum: Checksum of response and data; sum of all bytes including
533  *            checksum should total to 0.
534  * @result: EC's response to the command (separate from communication failure)
535  * @data_len: Length of data which follows this header.
536  * @reserved: Unused bytes in current protocol version; set to 0.
537  */
538 struct ec_host_response {
539         uint8_t struct_version;
540         uint8_t checksum;
541         uint16_t result;
542         uint16_t data_len;
543         uint16_t reserved;
544 } __ec_align4;
545
546 /*****************************************************************************/
547 /*
548  * Notes on commands:
549  *
550  * Each command is an 16-bit command value.  Commands which take params or
551  * return response data specify structures for that data.  If no structure is
552  * specified, the command does not input or output data, respectively.
553  * Parameter/response length is implicit in the structs.  Some underlying
554  * communication protocols (I2C, SPI) may add length or checksum headers, but
555  * those are implementation-dependent and not defined here.
556  */
557
558 /*****************************************************************************/
559 /* General / test commands */
560
561 /*
562  * Get protocol version, used to deal with non-backward compatible protocol
563  * changes.
564  */
565 #define EC_CMD_PROTO_VERSION 0x00
566
567 /**
568  * struct ec_response_proto_version - Response to the proto version command.
569  * @version: The protocol version.
570  */
571 struct ec_response_proto_version {
572         uint32_t version;
573 } __ec_align4;
574
575 /*
576  * Hello.  This is a simple command to test the EC is responsive to
577  * commands.
578  */
579 #define EC_CMD_HELLO 0x01
580
581 /**
582  * struct ec_params_hello - Parameters to the hello command.
583  * @in_data: Pass anything here.
584  */
585 struct ec_params_hello {
586         uint32_t in_data;
587 } __ec_align4;
588
589 /**
590  * struct ec_response_hello - Response to the hello command.
591  * @out_data: Output will be in_data + 0x01020304.
592  */
593 struct ec_response_hello {
594         uint32_t out_data;
595 } __ec_align4;
596
597 /* Get version number */
598 #define EC_CMD_GET_VERSION 0x02
599
600 enum ec_current_image {
601         EC_IMAGE_UNKNOWN = 0,
602         EC_IMAGE_RO,
603         EC_IMAGE_RW
604 };
605
606 /**
607  * struct ec_response_get_version - Response to the get version command.
608  * @version_string_ro: Null-terminated RO firmware version string.
609  * @version_string_rw: Null-terminated RW firmware version string.
610  * @reserved: Unused bytes; was previously RW-B firmware version string.
611  * @current_image: One of ec_current_image.
612  */
613 struct ec_response_get_version {
614         char version_string_ro[32];
615         char version_string_rw[32];
616         char reserved[32];
617         uint32_t current_image;
618 } __ec_align4;
619
620 /* Read test */
621 #define EC_CMD_READ_TEST 0x03
622
623 /**
624  * struct ec_params_read_test - Parameters for the read test command.
625  * @offset: Starting value for read buffer.
626  * @size: Size to read in bytes.
627  */
628 struct ec_params_read_test {
629         uint32_t offset;
630         uint32_t size;
631 } __ec_align4;
632
633 /**
634  * struct ec_response_read_test - Response to the read test command.
635  * @data: Data returned by the read test command.
636  */
637 struct ec_response_read_test {
638         uint32_t data[32];
639 } __ec_align4;
640
641 /*
642  * Get build information
643  *
644  * Response is null-terminated string.
645  */
646 #define EC_CMD_GET_BUILD_INFO 0x04
647
648 /* Get chip info */
649 #define EC_CMD_GET_CHIP_INFO 0x05
650
651 /**
652  * struct ec_response_get_chip_info - Response to the get chip info command.
653  * @vendor: Null-terminated string for chip vendor.
654  * @name: Null-terminated string for chip name.
655  * @revision: Null-terminated string for chip mask version.
656  */
657 struct ec_response_get_chip_info {
658         char vendor[32];
659         char name[32];
660         char revision[32];
661 } __ec_align4;
662
663 /* Get board HW version */
664 #define EC_CMD_GET_BOARD_VERSION 0x06
665
666 /**
667  * struct ec_response_board_version - Response to the board version command.
668  * @board_version: A monotonously incrementing number.
669  */
670 struct ec_response_board_version {
671         uint16_t board_version;
672 } __ec_align2;
673
674 /*
675  * Read memory-mapped data.
676  *
677  * This is an alternate interface to memory-mapped data for bus protocols
678  * which don't support direct-mapped memory - I2C, SPI, etc.
679  *
680  * Response is params.size bytes of data.
681  */
682 #define EC_CMD_READ_MEMMAP 0x07
683
684 /**
685  * struct ec_params_read_memmap - Parameters for the read memory map command.
686  * @offset: Offset in memmap (EC_MEMMAP_*).
687  * @size: Size to read in bytes.
688  */
689 struct ec_params_read_memmap {
690         uint8_t offset;
691         uint8_t size;
692 } __ec_align1;
693
694 /* Read versions supported for a command */
695 #define EC_CMD_GET_CMD_VERSIONS 0x08
696
697 /**
698  * struct ec_params_get_cmd_versions - Parameters for the get command versions.
699  * @cmd: Command to check.
700  */
701 struct ec_params_get_cmd_versions {
702         uint8_t cmd;
703 } __ec_align1;
704
705 /**
706  * struct ec_params_get_cmd_versions_v1 - Parameters for the get command
707  *         versions (v1)
708  * @cmd: Command to check.
709  */
710 struct ec_params_get_cmd_versions_v1 {
711         uint16_t cmd;
712 } __ec_align2;
713
714 /**
715  * struct ec_response_get_cmd_version - Response to the get command versions.
716  * @version_mask: Mask of supported versions; use EC_VER_MASK() to compare with
717  *                a desired version.
718  */
719 struct ec_response_get_cmd_versions {
720         uint32_t version_mask;
721 } __ec_align4;
722
723 /*
724  * Check EC communications status (busy). This is needed on i2c/spi but not
725  * on lpc since it has its own out-of-band busy indicator.
726  *
727  * lpc must read the status from the command register. Attempting this on
728  * lpc will overwrite the args/parameter space and corrupt its data.
729  */
730 #define EC_CMD_GET_COMMS_STATUS         0x09
731
732 /* Avoid using ec_status which is for return values */
733 enum ec_comms_status {
734         EC_COMMS_STATUS_PROCESSING      = 1 << 0,       /* Processing cmd */
735 };
736
737 /**
738  * struct ec_response_get_comms_status - Response to the get comms status
739  *         command.
740  * @flags: Mask of enum ec_comms_status.
741  */
742 struct ec_response_get_comms_status {
743         uint32_t flags;         /* Mask of enum ec_comms_status */
744 } __ec_align4;
745
746 /* Fake a variety of responses, purely for testing purposes. */
747 #define EC_CMD_TEST_PROTOCOL            0x0a
748
749 /* Tell the EC what to send back to us. */
750 struct ec_params_test_protocol {
751         uint32_t ec_result;
752         uint32_t ret_len;
753         uint8_t buf[32];
754 } __ec_align4;
755
756 /* Here it comes... */
757 struct ec_response_test_protocol {
758         uint8_t buf[32];
759 } __ec_align4;
760
761 /* Get protocol information */
762 #define EC_CMD_GET_PROTOCOL_INFO        0x0b
763
764 /* Flags for ec_response_get_protocol_info.flags */
765 /* EC_RES_IN_PROGRESS may be returned if a command is slow */
766 #define EC_PROTOCOL_INFO_IN_PROGRESS_SUPPORTED (1 << 0)
767
768 /**
769  * struct ec_response_get_protocol_info - Response to the get protocol info.
770  * @protocol_versions: Bitmask of protocol versions supported (1 << n means
771  *                     version n).
772  * @max_request_packet_size: Maximum request packet size in bytes.
773  * @max_response_packet_size: Maximum response packet size in bytes.
774  * @flags: see EC_PROTOCOL_INFO_*
775  */
776 struct ec_response_get_protocol_info {
777         /* Fields which exist if at least protocol version 3 supported */
778         uint32_t protocol_versions;
779         uint16_t max_request_packet_size;
780         uint16_t max_response_packet_size;
781         uint32_t flags;
782 } __ec_align4;
783
784
785 /*****************************************************************************/
786 /* Get/Set miscellaneous values */
787
788 /* The upper byte of .flags tells what to do (nothing means "get") */
789 #define EC_GSV_SET        0x80000000
790
791 /*
792  * The lower three bytes of .flags identifies the parameter, if that has
793  * meaning for an individual command.
794  */
795 #define EC_GSV_PARAM_MASK 0x00ffffff
796
797 struct ec_params_get_set_value {
798         uint32_t flags;
799         uint32_t value;
800 } __ec_align4;
801
802 struct ec_response_get_set_value {
803         uint32_t flags;
804         uint32_t value;
805 } __ec_align4;
806
807 /* More than one command can use these structs to get/set parameters. */
808 #define EC_CMD_GSV_PAUSE_IN_S5  0x0c
809
810 /*****************************************************************************/
811 /* List the features supported by the firmware */
812 #define EC_CMD_GET_FEATURES  0x0d
813
814 /* Supported features */
815 enum ec_feature_code {
816         /*
817          * This image contains a limited set of features. Another image
818          * in RW partition may support more features.
819          */
820         EC_FEATURE_LIMITED = 0,
821         /*
822          * Commands for probing/reading/writing/erasing the flash in the
823          * EC are present.
824          */
825         EC_FEATURE_FLASH = 1,
826         /*
827          * Can control the fan speed directly.
828          */
829         EC_FEATURE_PWM_FAN = 2,
830         /*
831          * Can control the intensity of the keyboard backlight.
832          */
833         EC_FEATURE_PWM_KEYB = 3,
834         /*
835          * Support Google lightbar, introduced on Pixel.
836          */
837         EC_FEATURE_LIGHTBAR = 4,
838         /* Control of LEDs  */
839         EC_FEATURE_LED = 5,
840         /* Exposes an interface to control gyro and sensors.
841          * The host goes through the EC to access these sensors.
842          * In addition, the EC may provide composite sensors, like lid angle.
843          */
844         EC_FEATURE_MOTION_SENSE = 6,
845         /* The keyboard is controlled by the EC */
846         EC_FEATURE_KEYB = 7,
847         /* The AP can use part of the EC flash as persistent storage. */
848         EC_FEATURE_PSTORE = 8,
849         /* The EC monitors BIOS port 80h, and can return POST codes. */
850         EC_FEATURE_PORT80 = 9,
851         /*
852          * Thermal management: include TMP specific commands.
853          * Higher level than direct fan control.
854          */
855         EC_FEATURE_THERMAL = 10,
856         /* Can switch the screen backlight on/off */
857         EC_FEATURE_BKLIGHT_SWITCH = 11,
858         /* Can switch the wifi module on/off */
859         EC_FEATURE_WIFI_SWITCH = 12,
860         /* Monitor host events, through for example SMI or SCI */
861         EC_FEATURE_HOST_EVENTS = 13,
862         /* The EC exposes GPIO commands to control/monitor connected devices. */
863         EC_FEATURE_GPIO = 14,
864         /* The EC can send i2c messages to downstream devices. */
865         EC_FEATURE_I2C = 15,
866         /* Command to control charger are included */
867         EC_FEATURE_CHARGER = 16,
868         /* Simple battery support. */
869         EC_FEATURE_BATTERY = 17,
870         /*
871          * Support Smart battery protocol
872          * (Common Smart Battery System Interface Specification)
873          */
874         EC_FEATURE_SMART_BATTERY = 18,
875         /* EC can detect when the host hangs. */
876         EC_FEATURE_HANG_DETECT = 19,
877         /* Report power information, for pit only */
878         EC_FEATURE_PMU = 20,
879         /* Another Cros EC device is present downstream of this one */
880         EC_FEATURE_SUB_MCU = 21,
881         /* Support USB Power delivery (PD) commands */
882         EC_FEATURE_USB_PD = 22,
883         /* Control USB multiplexer, for audio through USB port for instance. */
884         EC_FEATURE_USB_MUX = 23,
885         /* Motion Sensor code has an internal software FIFO */
886         EC_FEATURE_MOTION_SENSE_FIFO = 24,
887         /* Support temporary secure vstore */
888         EC_FEATURE_VSTORE = 25,
889         /* EC decides on USB-C SS mux state, muxes configured by host */
890         EC_FEATURE_USBC_SS_MUX_VIRTUAL = 26,
891         /* EC has RTC feature that can be controlled by host commands */
892         EC_FEATURE_RTC = 27,
893         /* The MCU exposes a Fingerprint sensor */
894         EC_FEATURE_FINGERPRINT = 28,
895         /* The MCU exposes a Touchpad */
896         EC_FEATURE_TOUCHPAD = 29,
897         /* The MCU has RWSIG task enabled */
898         EC_FEATURE_RWSIG = 30,
899         /* EC has device events support */
900         EC_FEATURE_DEVICE_EVENT = 31,
901         /* EC supports the unified wake masks for LPC/eSPI systems */
902         EC_FEATURE_UNIFIED_WAKE_MASKS = 32,
903         /* EC supports 64-bit host events */
904         EC_FEATURE_HOST_EVENT64 = 33,
905         /* EC runs code in RAM (not in place, a.k.a. XIP) */
906         EC_FEATURE_EXEC_IN_RAM = 34,
907         /* EC supports CEC commands */
908         EC_FEATURE_CEC = 35,
909         /* EC supports tight sensor timestamping. */
910         EC_FEATURE_MOTION_SENSE_TIGHT_TIMESTAMPS = 36,
911         /*
912          * EC supports tablet mode detection aligned to Chrome and allows
913          * setting of threshold by host command using
914          * MOTIONSENSE_CMD_TABLET_MODE_LID_ANGLE.
915          */
916         EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS = 37,
917         /* EC supports audio codec. */
918         EC_FEATURE_AUDIO_CODEC = 38,
919         /* EC Supports SCP. */
920         EC_FEATURE_SCP = 39,
921         /* The MCU is an Integrated Sensor Hub */
922         EC_FEATURE_ISH = 40,
923 };
924
925 #define EC_FEATURE_MASK_0(event_code) (1UL << (event_code % 32))
926 #define EC_FEATURE_MASK_1(event_code) (1UL << (event_code - 32))
927
928 struct ec_response_get_features {
929         uint32_t flags[2];
930 } __ec_align4;
931
932 /*****************************************************************************/
933 /* Flash commands */
934
935 /* Get flash info */
936 #define EC_CMD_FLASH_INFO 0x10
937
938 /**
939  * struct ec_response_flash_info - Response to the flash info command.
940  * @flash_size: Usable flash size in bytes.
941  * @write_block_size: Write block size. Write offset and size must be a
942  *                    multiple of this.
943  * @erase_block_size: Erase block size. Erase offset and size must be a
944  *                    multiple of this.
945  * @protect_block_size: Protection block size. Protection offset and size
946  *                      must be a multiple of this.
947  *
948  * Version 0 returns these fields.
949  */
950 struct ec_response_flash_info {
951         uint32_t flash_size;
952         uint32_t write_block_size;
953         uint32_t erase_block_size;
954         uint32_t protect_block_size;
955 } __ec_align4;
956
957 /*
958  * Flags for version 1+ flash info command
959  * EC flash erases bits to 0 instead of 1.
960  */
961 #define EC_FLASH_INFO_ERASE_TO_0 (1 << 0)
962
963 /**
964  * struct ec_response_flash_info_1 - Response to the flash info v1 command.
965  * @flash_size: Usable flash size in bytes.
966  * @write_block_size: Write block size. Write offset and size must be a
967  *                    multiple of this.
968  * @erase_block_size: Erase block size. Erase offset and size must be a
969  *                    multiple of this.
970  * @protect_block_size: Protection block size. Protection offset and size
971  *                      must be a multiple of this.
972  * @write_ideal_size: Ideal write size in bytes.  Writes will be fastest if
973  *                    size is exactly this and offset is a multiple of this.
974  *                    For example, an EC may have a write buffer which can do
975  *                    half-page operations if data is aligned, and a slower
976  *                    word-at-a-time write mode.
977  * @flags: Flags; see EC_FLASH_INFO_*
978  *
979  * Version 1 returns the same initial fields as version 0, with additional
980  * fields following.
981  *
982  * gcc anonymous structs don't seem to get along with the __packed directive;
983  * if they did we'd define the version 0 structure as a sub-structure of this
984  * one.
985  */
986 struct ec_response_flash_info_1 {
987         /* Version 0 fields; see above for description */
988         uint32_t flash_size;
989         uint32_t write_block_size;
990         uint32_t erase_block_size;
991         uint32_t protect_block_size;
992
993         /* Version 1 adds these fields: */
994         uint32_t write_ideal_size;
995         uint32_t flags;
996 } __ec_align4;
997
998 /*
999  * Read flash
1000  *
1001  * Response is params.size bytes of data.
1002  */
1003 #define EC_CMD_FLASH_READ 0x11
1004
1005 /**
1006  * struct ec_params_flash_read - Parameters for the flash read command.
1007  * @offset: Byte offset to read.
1008  * @size: Size to read in bytes.
1009  */
1010 struct ec_params_flash_read {
1011         uint32_t offset;
1012         uint32_t size;
1013 } __ec_align4;
1014
1015 /* Write flash */
1016 #define EC_CMD_FLASH_WRITE 0x12
1017 #define EC_VER_FLASH_WRITE 1
1018
1019 /* Version 0 of the flash command supported only 64 bytes of data */
1020 #define EC_FLASH_WRITE_VER0_SIZE 64
1021
1022 /**
1023  * struct ec_params_flash_write - Parameters for the flash write command.
1024  * @offset: Byte offset to write.
1025  * @size: Size to write in bytes.
1026  */
1027 struct ec_params_flash_write {
1028         uint32_t offset;
1029         uint32_t size;
1030         /* Followed by data to write */
1031 } __ec_align4;
1032
1033 /* Erase flash */
1034 #define EC_CMD_FLASH_ERASE 0x13
1035
1036 /**
1037  * struct ec_params_flash_erase - Parameters for the flash erase command.
1038  * @offset: Byte offset to erase.
1039  * @size: Size to erase in bytes.
1040  */
1041 struct ec_params_flash_erase {
1042         uint32_t offset;
1043         uint32_t size;
1044 } __ec_align4;
1045
1046 /*
1047  * Get/set flash protection.
1048  *
1049  * If mask!=0, sets/clear the requested bits of flags.  Depending on the
1050  * firmware write protect GPIO, not all flags will take effect immediately;
1051  * some flags require a subsequent hard reset to take effect.  Check the
1052  * returned flags bits to see what actually happened.
1053  *
1054  * If mask=0, simply returns the current flags state.
1055  */
1056 #define EC_CMD_FLASH_PROTECT 0x15
1057 #define EC_VER_FLASH_PROTECT 1  /* Command version 1 */
1058
1059 /* Flags for flash protection */
1060 /* RO flash code protected when the EC boots */
1061 #define EC_FLASH_PROTECT_RO_AT_BOOT         (1 << 0)
1062 /*
1063  * RO flash code protected now.  If this bit is set, at-boot status cannot
1064  * be changed.
1065  */
1066 #define EC_FLASH_PROTECT_RO_NOW             (1 << 1)
1067 /* Entire flash code protected now, until reboot. */
1068 #define EC_FLASH_PROTECT_ALL_NOW            (1 << 2)
1069 /* Flash write protect GPIO is asserted now */
1070 #define EC_FLASH_PROTECT_GPIO_ASSERTED      (1 << 3)
1071 /* Error - at least one bank of flash is stuck locked, and cannot be unlocked */
1072 #define EC_FLASH_PROTECT_ERROR_STUCK        (1 << 4)
1073 /*
1074  * Error - flash protection is in inconsistent state.  At least one bank of
1075  * flash which should be protected is not protected.  Usually fixed by
1076  * re-requesting the desired flags, or by a hard reset if that fails.
1077  */
1078 #define EC_FLASH_PROTECT_ERROR_INCONSISTENT (1 << 5)
1079 /* Entire flash code protected when the EC boots */
1080 #define EC_FLASH_PROTECT_ALL_AT_BOOT        (1 << 6)
1081
1082 /**
1083  * struct ec_params_flash_protect - Parameters for the flash protect command.
1084  * @mask: Bits in flags to apply.
1085  * @flags: New flags to apply.
1086  */
1087 struct ec_params_flash_protect {
1088         uint32_t mask;
1089         uint32_t flags;
1090 } __ec_align4;
1091
1092 /**
1093  * struct ec_response_flash_protect - Response to the flash protect command.
1094  * @flags: Current value of flash protect flags.
1095  * @valid_flags: Flags which are valid on this platform. This allows the
1096  *               caller to distinguish between flags which aren't set vs. flags
1097  *               which can't be set on this platform.
1098  * @writable_flags: Flags which can be changed given the current protection
1099  *                  state.
1100  */
1101 struct ec_response_flash_protect {
1102         uint32_t flags;
1103         uint32_t valid_flags;
1104         uint32_t writable_flags;
1105 } __ec_align4;
1106
1107 /*
1108  * Note: commands 0x14 - 0x19 version 0 were old commands to get/set flash
1109  * write protect.  These commands may be reused with version > 0.
1110  */
1111
1112 /* Get the region offset/size */
1113 #define EC_CMD_FLASH_REGION_INFO 0x16
1114 #define EC_VER_FLASH_REGION_INFO 1
1115
1116 enum ec_flash_region {
1117         /* Region which holds read-only EC image */
1118         EC_FLASH_REGION_RO = 0,
1119         /* Region which holds rewritable EC image */
1120         EC_FLASH_REGION_RW,
1121         /*
1122          * Region which should be write-protected in the factory (a superset of
1123          * EC_FLASH_REGION_RO)
1124          */
1125         EC_FLASH_REGION_WP_RO,
1126         /* Number of regions */
1127         EC_FLASH_REGION_COUNT,
1128 };
1129
1130 /**
1131  * struct ec_params_flash_region_info - Parameters for the flash region info
1132  *         command.
1133  * @region: Flash region; see EC_FLASH_REGION_*
1134  */
1135 struct ec_params_flash_region_info {
1136         uint32_t region;
1137 } __ec_align4;
1138
1139 struct ec_response_flash_region_info {
1140         uint32_t offset;
1141         uint32_t size;
1142 } __ec_align4;
1143
1144 /* Read/write VbNvContext */
1145 #define EC_CMD_VBNV_CONTEXT 0x17
1146 #define EC_VER_VBNV_CONTEXT 1
1147 #define EC_VBNV_BLOCK_SIZE 16
1148
1149 enum ec_vbnvcontext_op {
1150         EC_VBNV_CONTEXT_OP_READ,
1151         EC_VBNV_CONTEXT_OP_WRITE,
1152 };
1153
1154 struct ec_params_vbnvcontext {
1155         uint32_t op;
1156         uint8_t block[EC_VBNV_BLOCK_SIZE];
1157 } __ec_align4;
1158
1159 struct ec_response_vbnvcontext {
1160         uint8_t block[EC_VBNV_BLOCK_SIZE];
1161 } __ec_align4;
1162
1163 /*****************************************************************************/
1164 /* PWM commands */
1165
1166 /* Get fan target RPM */
1167 #define EC_CMD_PWM_GET_FAN_TARGET_RPM 0x20
1168
1169 struct ec_response_pwm_get_fan_rpm {
1170         uint32_t rpm;
1171 } __ec_align4;
1172
1173 /* Set target fan RPM */
1174 #define EC_CMD_PWM_SET_FAN_TARGET_RPM 0x0021
1175
1176 struct ec_params_pwm_set_fan_target_rpm {
1177         uint32_t rpm;
1178 } __ec_align_size1;
1179
1180 /* Get keyboard backlight */
1181 #define EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT 0x22
1182
1183 struct ec_response_pwm_get_keyboard_backlight {
1184         uint8_t percent;
1185         uint8_t enabled;
1186 } __ec_align1;
1187
1188 /* Set keyboard backlight */
1189 #define EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT 0x23
1190
1191 struct ec_params_pwm_set_keyboard_backlight {
1192         uint8_t percent;
1193 } __ec_align1;
1194
1195 /* Set target fan PWM duty cycle */
1196 #define EC_CMD_PWM_SET_FAN_DUTY 0x24
1197
1198 struct ec_params_pwm_set_fan_duty {
1199         uint32_t percent;
1200 } __ec_align4;
1201
1202 #define EC_CMD_PWM_SET_DUTY 0x25
1203 /* 16 bit duty cycle, 0xffff = 100% */
1204 #define EC_PWM_MAX_DUTY 0xffff
1205
1206 enum ec_pwm_type {
1207         /* All types, indexed by board-specific enum pwm_channel */
1208         EC_PWM_TYPE_GENERIC = 0,
1209         /* Keyboard backlight */
1210         EC_PWM_TYPE_KB_LIGHT,
1211         /* Display backlight */
1212         EC_PWM_TYPE_DISPLAY_LIGHT,
1213         EC_PWM_TYPE_COUNT,
1214 };
1215
1216 struct ec_params_pwm_set_duty {
1217         uint16_t duty;     /* Duty cycle, EC_PWM_MAX_DUTY = 100% */
1218         uint8_t pwm_type;  /* ec_pwm_type */
1219         uint8_t index;     /* Type-specific index, or 0 if unique */
1220 } __ec_align4;
1221
1222 #define EC_CMD_PWM_GET_DUTY 0x26
1223
1224 struct ec_params_pwm_get_duty {
1225         uint8_t pwm_type;  /* ec_pwm_type */
1226         uint8_t index;     /* Type-specific index, or 0 if unique */
1227 } __ec_align1;
1228
1229 struct ec_response_pwm_get_duty {
1230         uint16_t duty;     /* Duty cycle, EC_PWM_MAX_DUTY = 100% */
1231 } __ec_align2;
1232
1233 /*****************************************************************************/
1234 /*
1235  * Lightbar commands. This looks worse than it is. Since we only use one HOST
1236  * command to say "talk to the lightbar", we put the "and tell it to do X" part
1237  * into a subcommand. We'll make separate structs for subcommands with
1238  * different input args, so that we know how much to expect.
1239  */
1240 #define EC_CMD_LIGHTBAR_CMD 0x28
1241
1242 struct rgb_s {
1243         uint8_t r, g, b;
1244 } __ec_todo_unpacked;
1245
1246 #define LB_BATTERY_LEVELS 4
1247
1248 /*
1249  * List of tweakable parameters. NOTE: It's __packed so it can be sent in a
1250  * host command, but the alignment is the same regardless. Keep it that way.
1251  */
1252 struct lightbar_params_v0 {
1253         /* Timing */
1254         int32_t google_ramp_up;
1255         int32_t google_ramp_down;
1256         int32_t s3s0_ramp_up;
1257         int32_t s0_tick_delay[2];               /* AC=0/1 */
1258         int32_t s0a_tick_delay[2];              /* AC=0/1 */
1259         int32_t s0s3_ramp_down;
1260         int32_t s3_sleep_for;
1261         int32_t s3_ramp_up;
1262         int32_t s3_ramp_down;
1263
1264         /* Oscillation */
1265         uint8_t new_s0;
1266         uint8_t osc_min[2];                     /* AC=0/1 */
1267         uint8_t osc_max[2];                     /* AC=0/1 */
1268         uint8_t w_ofs[2];                       /* AC=0/1 */
1269
1270         /* Brightness limits based on the backlight and AC. */
1271         uint8_t bright_bl_off_fixed[2];         /* AC=0/1 */
1272         uint8_t bright_bl_on_min[2];            /* AC=0/1 */
1273         uint8_t bright_bl_on_max[2];            /* AC=0/1 */
1274
1275         /* Battery level thresholds */
1276         uint8_t battery_threshold[LB_BATTERY_LEVELS - 1];
1277
1278         /* Map [AC][battery_level] to color index */
1279         uint8_t s0_idx[2][LB_BATTERY_LEVELS];   /* AP is running */
1280         uint8_t s3_idx[2][LB_BATTERY_LEVELS];   /* AP is sleeping */
1281
1282         /* Color palette */
1283         struct rgb_s color[8];                  /* 0-3 are Google colors */
1284 } __ec_todo_packed;
1285
1286 struct lightbar_params_v1 {
1287         /* Timing */
1288         int32_t google_ramp_up;
1289         int32_t google_ramp_down;
1290         int32_t s3s0_ramp_up;
1291         int32_t s0_tick_delay[2];               /* AC=0/1 */
1292         int32_t s0a_tick_delay[2];              /* AC=0/1 */
1293         int32_t s0s3_ramp_down;
1294         int32_t s3_sleep_for;
1295         int32_t s3_ramp_up;
1296         int32_t s3_ramp_down;
1297         int32_t tap_tick_delay;
1298         int32_t tap_display_time;
1299
1300         /* Tap-for-battery params */
1301         uint8_t tap_pct_red;
1302         uint8_t tap_pct_green;
1303         uint8_t tap_seg_min_on;
1304         uint8_t tap_seg_max_on;
1305         uint8_t tap_seg_osc;
1306         uint8_t tap_idx[3];
1307
1308         /* Oscillation */
1309         uint8_t osc_min[2];                     /* AC=0/1 */
1310         uint8_t osc_max[2];                     /* AC=0/1 */
1311         uint8_t w_ofs[2];                       /* AC=0/1 */
1312
1313         /* Brightness limits based on the backlight and AC. */
1314         uint8_t bright_bl_off_fixed[2];         /* AC=0/1 */
1315         uint8_t bright_bl_on_min[2];            /* AC=0/1 */
1316         uint8_t bright_bl_on_max[2];            /* AC=0/1 */
1317
1318         /* Battery level thresholds */
1319         uint8_t battery_threshold[LB_BATTERY_LEVELS - 1];
1320
1321         /* Map [AC][battery_level] to color index */
1322         uint8_t s0_idx[2][LB_BATTERY_LEVELS];   /* AP is running */
1323         uint8_t s3_idx[2][LB_BATTERY_LEVELS];   /* AP is sleeping */
1324
1325         /* Color palette */
1326         struct rgb_s color[8];                  /* 0-3 are Google colors */
1327 } __ec_todo_packed;
1328
1329 /* Lightbar program */
1330 #define EC_LB_PROG_LEN 192
1331 struct lightbar_program {
1332         uint8_t size;
1333         uint8_t data[EC_LB_PROG_LEN];
1334 } __ec_todo_unpacked;
1335
1336 struct ec_params_lightbar {
1337         uint8_t cmd;                  /* Command (see enum lightbar_command) */
1338         union {
1339                 struct {
1340                         /* no args */
1341                 } dump, off, on, init, get_seq, get_params_v0, get_params_v1,
1342                         version, get_brightness, get_demo, suspend, resume;
1343
1344                 struct __ec_todo_unpacked {
1345                         uint8_t num;
1346                 } set_brightness, seq, demo;
1347
1348                 struct __ec_todo_unpacked {
1349                         uint8_t ctrl, reg, value;
1350                 } reg;
1351
1352                 struct __ec_todo_unpacked {
1353                         uint8_t led, red, green, blue;
1354                 } set_rgb;
1355
1356                 struct __ec_todo_unpacked {
1357                         uint8_t led;
1358                 } get_rgb;
1359
1360                 struct __ec_todo_unpacked {
1361                         uint8_t enable;
1362                 } manual_suspend_ctrl;
1363
1364                 struct lightbar_params_v0 set_params_v0;
1365                 struct lightbar_params_v1 set_params_v1;
1366                 struct lightbar_program set_program;
1367         };
1368 } __ec_todo_packed;
1369
1370 struct ec_response_lightbar {
1371         union {
1372                 struct __ec_todo_unpacked {
1373                         struct __ec_todo_unpacked {
1374                                 uint8_t reg;
1375                                 uint8_t ic0;
1376                                 uint8_t ic1;
1377                         } vals[23];
1378                 } dump;
1379
1380                 struct __ec_todo_unpacked {
1381                         uint8_t num;
1382                 } get_seq, get_brightness, get_demo;
1383
1384                 struct lightbar_params_v0 get_params_v0;
1385                 struct lightbar_params_v1 get_params_v1;
1386
1387                 struct __ec_todo_unpacked {
1388                         uint32_t num;
1389                         uint32_t flags;
1390                 } version;
1391
1392                 struct __ec_todo_unpacked {
1393                         uint8_t red, green, blue;
1394                 } get_rgb;
1395
1396                 struct {
1397                         /* no return params */
1398                 } off, on, init, set_brightness, seq, reg, set_rgb,
1399                         demo, set_params_v0, set_params_v1,
1400                         set_program, manual_suspend_ctrl, suspend, resume;
1401         };
1402 } __ec_todo_packed;
1403
1404 /* Lightbar commands */
1405 enum lightbar_command {
1406         LIGHTBAR_CMD_DUMP = 0,
1407         LIGHTBAR_CMD_OFF = 1,
1408         LIGHTBAR_CMD_ON = 2,
1409         LIGHTBAR_CMD_INIT = 3,
1410         LIGHTBAR_CMD_SET_BRIGHTNESS = 4,
1411         LIGHTBAR_CMD_SEQ = 5,
1412         LIGHTBAR_CMD_REG = 6,
1413         LIGHTBAR_CMD_SET_RGB = 7,
1414         LIGHTBAR_CMD_GET_SEQ = 8,
1415         LIGHTBAR_CMD_DEMO = 9,
1416         LIGHTBAR_CMD_GET_PARAMS_V0 = 10,
1417         LIGHTBAR_CMD_SET_PARAMS_V0 = 11,
1418         LIGHTBAR_CMD_VERSION = 12,
1419         LIGHTBAR_CMD_GET_BRIGHTNESS = 13,
1420         LIGHTBAR_CMD_GET_RGB = 14,
1421         LIGHTBAR_CMD_GET_DEMO = 15,
1422         LIGHTBAR_CMD_GET_PARAMS_V1 = 16,
1423         LIGHTBAR_CMD_SET_PARAMS_V1 = 17,
1424         LIGHTBAR_CMD_SET_PROGRAM = 18,
1425         LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL = 19,
1426         LIGHTBAR_CMD_SUSPEND = 20,
1427         LIGHTBAR_CMD_RESUME = 21,
1428         LIGHTBAR_NUM_CMDS
1429 };
1430
1431 /*****************************************************************************/
1432 /* LED control commands */
1433
1434 #define EC_CMD_LED_CONTROL 0x29
1435
1436 enum ec_led_id {
1437         /* LED to indicate battery state of charge */
1438         EC_LED_ID_BATTERY_LED = 0,
1439         /*
1440          * LED to indicate system power state (on or in suspend).
1441          * May be on power button or on C-panel.
1442          */
1443         EC_LED_ID_POWER_LED,
1444         /* LED on power adapter or its plug */
1445         EC_LED_ID_ADAPTER_LED,
1446
1447         EC_LED_ID_COUNT
1448 };
1449
1450 /* LED control flags */
1451 #define EC_LED_FLAGS_QUERY (1 << 0) /* Query LED capability only */
1452 #define EC_LED_FLAGS_AUTO  (1 << 1) /* Switch LED back to automatic control */
1453
1454 enum ec_led_colors {
1455         EC_LED_COLOR_RED = 0,
1456         EC_LED_COLOR_GREEN,
1457         EC_LED_COLOR_BLUE,
1458         EC_LED_COLOR_YELLOW,
1459         EC_LED_COLOR_WHITE,
1460
1461         EC_LED_COLOR_COUNT
1462 };
1463
1464 struct ec_params_led_control {
1465         uint8_t led_id;     /* Which LED to control */
1466         uint8_t flags;      /* Control flags */
1467
1468         uint8_t brightness[EC_LED_COLOR_COUNT];
1469 } __ec_align1;
1470
1471 struct ec_response_led_control {
1472         /*
1473          * Available brightness value range.
1474          *
1475          * Range 0 means color channel not present.
1476          * Range 1 means on/off control.
1477          * Other values means the LED is control by PWM.
1478          */
1479         uint8_t brightness_range[EC_LED_COLOR_COUNT];
1480 } __ec_align1;
1481
1482 /*****************************************************************************/
1483 /* Verified boot commands */
1484
1485 /*
1486  * Note: command code 0x29 version 0 was VBOOT_CMD in Link EVT; it may be
1487  * reused for other purposes with version > 0.
1488  */
1489
1490 /* Verified boot hash command */
1491 #define EC_CMD_VBOOT_HASH 0x2A
1492
1493 struct ec_params_vboot_hash {
1494         uint8_t cmd;             /* enum ec_vboot_hash_cmd */
1495         uint8_t hash_type;       /* enum ec_vboot_hash_type */
1496         uint8_t nonce_size;      /* Nonce size; may be 0 */
1497         uint8_t reserved0;       /* Reserved; set 0 */
1498         uint32_t offset;         /* Offset in flash to hash */
1499         uint32_t size;           /* Number of bytes to hash */
1500         uint8_t nonce_data[64];  /* Nonce data; ignored if nonce_size=0 */
1501 } __ec_align4;
1502
1503 struct ec_response_vboot_hash {
1504         uint8_t status;          /* enum ec_vboot_hash_status */
1505         uint8_t hash_type;       /* enum ec_vboot_hash_type */
1506         uint8_t digest_size;     /* Size of hash digest in bytes */
1507         uint8_t reserved0;       /* Ignore; will be 0 */
1508         uint32_t offset;         /* Offset in flash which was hashed */
1509         uint32_t size;           /* Number of bytes hashed */
1510         uint8_t hash_digest[64]; /* Hash digest data */
1511 } __ec_align4;
1512
1513 enum ec_vboot_hash_cmd {
1514         EC_VBOOT_HASH_GET = 0,       /* Get current hash status */
1515         EC_VBOOT_HASH_ABORT = 1,     /* Abort calculating current hash */
1516         EC_VBOOT_HASH_START = 2,     /* Start computing a new hash */
1517         EC_VBOOT_HASH_RECALC = 3,    /* Synchronously compute a new hash */
1518 };
1519
1520 enum ec_vboot_hash_type {
1521         EC_VBOOT_HASH_TYPE_SHA256 = 0, /* SHA-256 */
1522 };
1523
1524 enum ec_vboot_hash_status {
1525         EC_VBOOT_HASH_STATUS_NONE = 0, /* No hash (not started, or aborted) */
1526         EC_VBOOT_HASH_STATUS_DONE = 1, /* Finished computing a hash */
1527         EC_VBOOT_HASH_STATUS_BUSY = 2, /* Busy computing a hash */
1528 };
1529
1530 /*
1531  * Special values for offset for EC_VBOOT_HASH_START and EC_VBOOT_HASH_RECALC.
1532  * If one of these is specified, the EC will automatically update offset and
1533  * size to the correct values for the specified image (RO or RW).
1534  */
1535 #define EC_VBOOT_HASH_OFFSET_RO 0xfffffffe
1536 #define EC_VBOOT_HASH_OFFSET_RW 0xfffffffd
1537
1538 /*****************************************************************************/
1539 /*
1540  * Motion sense commands. We'll make separate structs for sub-commands with
1541  * different input args, so that we know how much to expect.
1542  */
1543 #define EC_CMD_MOTION_SENSE_CMD 0x2B
1544
1545 /* Motion sense commands */
1546 enum motionsense_command {
1547         /*
1548          * Dump command returns all motion sensor data including motion sense
1549          * module flags and individual sensor flags.
1550          */
1551         MOTIONSENSE_CMD_DUMP = 0,
1552
1553         /*
1554          * Info command returns data describing the details of a given sensor,
1555          * including enum motionsensor_type, enum motionsensor_location, and
1556          * enum motionsensor_chip.
1557          */
1558         MOTIONSENSE_CMD_INFO = 1,
1559
1560         /*
1561          * EC Rate command is a setter/getter command for the EC sampling rate
1562          * of all motion sensors in milliseconds.
1563          */
1564         MOTIONSENSE_CMD_EC_RATE = 2,
1565
1566         /*
1567          * Sensor ODR command is a setter/getter command for the output data
1568          * rate of a specific motion sensor in millihertz.
1569          */
1570         MOTIONSENSE_CMD_SENSOR_ODR = 3,
1571
1572         /*
1573          * Sensor range command is a setter/getter command for the range of
1574          * a specified motion sensor in +/-G's or +/- deg/s.
1575          */
1576         MOTIONSENSE_CMD_SENSOR_RANGE = 4,
1577
1578         /*
1579          * Setter/getter command for the keyboard wake angle. When the lid
1580          * angle is greater than this value, keyboard wake is disabled in S3,
1581          * and when the lid angle goes less than this value, keyboard wake is
1582          * enabled. Note, the lid angle measurement is an approximate,
1583          * un-calibrated value, hence the wake angle isn't exact.
1584          */
1585         MOTIONSENSE_CMD_KB_WAKE_ANGLE = 5,
1586
1587         /*
1588          * Returns a single sensor data.
1589          */
1590         MOTIONSENSE_CMD_DATA = 6,
1591
1592         /*
1593          * Perform low level calibration.. On sensors that support it, ask to
1594          * do offset calibration.
1595          */
1596         MOTIONSENSE_CMD_PERFORM_CALIB = 10,
1597
1598         /*
1599          * Sensor Offset command is a setter/getter command for the offset used
1600          * for calibration. The offsets can be calculated by the host, or via
1601          * PERFORM_CALIB command.
1602          */
1603         MOTIONSENSE_CMD_SENSOR_OFFSET = 11,
1604
1605         /* Number of motionsense sub-commands. */
1606         MOTIONSENSE_NUM_CMDS
1607 };
1608
1609 enum motionsensor_id {
1610         EC_MOTION_SENSOR_ACCEL_BASE = 0,
1611         EC_MOTION_SENSOR_ACCEL_LID = 1,
1612         EC_MOTION_SENSOR_GYRO = 2,
1613
1614         /*
1615          * Note, if more sensors are added and this count changes, the padding
1616          * in ec_response_motion_sense dump command must be modified.
1617          */
1618         EC_MOTION_SENSOR_COUNT = 3
1619 };
1620
1621 /* List of motion sensor types. */
1622 enum motionsensor_type {
1623         MOTIONSENSE_TYPE_ACCEL = 0,
1624         MOTIONSENSE_TYPE_GYRO = 1,
1625         MOTIONSENSE_TYPE_MAG = 2,
1626         MOTIONSENSE_TYPE_PROX = 3,
1627         MOTIONSENSE_TYPE_LIGHT = 4,
1628         MOTIONSENSE_TYPE_ACTIVITY = 5,
1629         MOTIONSENSE_TYPE_BARO = 6,
1630         MOTIONSENSE_TYPE_MAX,
1631 };
1632
1633 /* List of motion sensor locations. */
1634 enum motionsensor_location {
1635         MOTIONSENSE_LOC_BASE = 0,
1636         MOTIONSENSE_LOC_LID = 1,
1637         MOTIONSENSE_LOC_MAX,
1638 };
1639
1640 /* List of motion sensor chips. */
1641 enum motionsensor_chip {
1642         MOTIONSENSE_CHIP_KXCJ9 = 0,
1643 };
1644
1645 /* Module flag masks used for the dump sub-command. */
1646 #define MOTIONSENSE_MODULE_FLAG_ACTIVE (1<<0)
1647
1648 /* Sensor flag masks used for the dump sub-command. */
1649 #define MOTIONSENSE_SENSOR_FLAG_PRESENT (1<<0)
1650
1651 /*
1652  * Send this value for the data element to only perform a read. If you
1653  * send any other value, the EC will interpret it as data to set and will
1654  * return the actual value set.
1655  */
1656 #define EC_MOTION_SENSE_NO_VALUE -1
1657
1658 #define EC_MOTION_SENSE_INVALID_CALIB_TEMP 0x8000
1659
1660 /* Set Calibration information */
1661 #define MOTION_SENSE_SET_OFFSET 1
1662
1663 struct ec_response_motion_sensor_data {
1664         /* Flags for each sensor. */
1665         uint8_t flags;
1666         /* Sensor number the data comes from */
1667         uint8_t sensor_num;
1668         /* Each sensor is up to 3-axis. */
1669         union {
1670                 int16_t             data[3];
1671                 struct __ec_todo_packed {
1672                         uint16_t    reserved;
1673                         uint32_t    timestamp;
1674                 };
1675                 struct __ec_todo_unpacked {
1676                         uint8_t     activity; /* motionsensor_activity */
1677                         uint8_t     state;
1678                         int16_t     add_info[2];
1679                 };
1680         };
1681 } __ec_todo_packed;
1682
1683 struct ec_params_motion_sense {
1684         uint8_t cmd;
1685         union {
1686                 /* Used for MOTIONSENSE_CMD_DUMP. */
1687                 struct __ec_todo_unpacked {
1688                         /* no args */
1689                 } dump;
1690
1691                 /*
1692                  * Used for MOTIONSENSE_CMD_EC_RATE and
1693                  * MOTIONSENSE_CMD_KB_WAKE_ANGLE.
1694                  */
1695                 struct __ec_todo_unpacked {
1696                         /* Data to set or EC_MOTION_SENSE_NO_VALUE to read. */
1697                         int16_t data;
1698                 } ec_rate, kb_wake_angle;
1699
1700                 /* Used for MOTIONSENSE_CMD_SENSOR_OFFSET */
1701                 struct __ec_todo_packed {
1702                         uint8_t sensor_num;
1703
1704                         /*
1705                          * bit 0: If set (MOTION_SENSE_SET_OFFSET), set
1706                          * the calibration information in the EC.
1707                          * If unset, just retrieve calibration information.
1708                          */
1709                         uint16_t flags;
1710
1711                         /*
1712                          * Temperature at calibration, in units of 0.01 C
1713                          * 0x8000: invalid / unknown.
1714                          * 0x0: 0C
1715                          * 0x7fff: +327.67C
1716                          */
1717                         int16_t temp;
1718
1719                         /*
1720                          * Offset for calibration.
1721                          * Unit:
1722                          * Accelerometer: 1/1024 g
1723                          * Gyro:          1/1024 deg/s
1724                          * Compass:       1/16 uT
1725                          */
1726                         int16_t offset[3];
1727                 } sensor_offset;
1728
1729                 /* Used for MOTIONSENSE_CMD_INFO. */
1730                 struct __ec_todo_packed {
1731                         uint8_t sensor_num;
1732                 } info;
1733
1734                 /*
1735                  * Used for MOTIONSENSE_CMD_SENSOR_ODR and
1736                  * MOTIONSENSE_CMD_SENSOR_RANGE.
1737                  */
1738                 struct {
1739                         /* Should be element of enum motionsensor_id. */
1740                         uint8_t sensor_num;
1741
1742                         /* Rounding flag, true for round-up, false for down. */
1743                         uint8_t roundup;
1744
1745                         uint16_t reserved;
1746
1747                         /* Data to set or EC_MOTION_SENSE_NO_VALUE to read. */
1748                         int32_t data;
1749                 } sensor_odr, sensor_range;
1750         };
1751 } __ec_todo_packed;
1752
1753 struct ec_response_motion_sense {
1754         union {
1755                 /* Used for MOTIONSENSE_CMD_DUMP. */
1756                 struct __ec_todo_unpacked {
1757                         /* Flags representing the motion sensor module. */
1758                         uint8_t module_flags;
1759
1760                         /* Number of sensors managed directly by the EC. */
1761                         uint8_t sensor_count;
1762
1763                         /*
1764                          * Sensor data is truncated if response_max is too small
1765                          * for holding all the data.
1766                          */
1767                         struct ec_response_motion_sensor_data sensor[0];
1768                 } dump;
1769
1770                 /* Used for MOTIONSENSE_CMD_INFO. */
1771                 struct __ec_todo_unpacked {
1772                         /* Should be element of enum motionsensor_type. */
1773                         uint8_t type;
1774
1775                         /* Should be element of enum motionsensor_location. */
1776                         uint8_t location;
1777
1778                         /* Should be element of enum motionsensor_chip. */
1779                         uint8_t chip;
1780                 } info;
1781
1782                 /* Used for MOTIONSENSE_CMD_DATA */
1783                 struct ec_response_motion_sensor_data data;
1784
1785                 /*
1786                  * Used for MOTIONSENSE_CMD_EC_RATE, MOTIONSENSE_CMD_SENSOR_ODR,
1787                  * MOTIONSENSE_CMD_SENSOR_RANGE, and
1788                  * MOTIONSENSE_CMD_KB_WAKE_ANGLE.
1789                  */
1790                 struct __ec_todo_unpacked {
1791                         /* Current value of the parameter queried. */
1792                         int32_t ret;
1793                 } ec_rate, sensor_odr, sensor_range, kb_wake_angle;
1794
1795                 /* Used for MOTIONSENSE_CMD_SENSOR_OFFSET */
1796                 struct __ec_todo_unpacked  {
1797                         int16_t temp;
1798                         int16_t offset[3];
1799                 } sensor_offset, perform_calib;
1800         };
1801 } __ec_todo_packed;
1802
1803 /*****************************************************************************/
1804 /* USB charging control commands */
1805
1806 /* Set USB port charging mode */
1807 #define EC_CMD_USB_CHARGE_SET_MODE 0x30
1808
1809 struct ec_params_usb_charge_set_mode {
1810         uint8_t usb_port_id;
1811         uint8_t mode;
1812 } __ec_align1;
1813
1814 /*****************************************************************************/
1815 /* Persistent storage for host */
1816
1817 /* Maximum bytes that can be read/written in a single command */
1818 #define EC_PSTORE_SIZE_MAX 64
1819
1820 /* Get persistent storage info */
1821 #define EC_CMD_PSTORE_INFO 0x40
1822
1823 struct ec_response_pstore_info {
1824         /* Persistent storage size, in bytes */
1825         uint32_t pstore_size;
1826         /* Access size; read/write offset and size must be a multiple of this */
1827         uint32_t access_size;
1828 } __ec_align4;
1829
1830 /*
1831  * Read persistent storage
1832  *
1833  * Response is params.size bytes of data.
1834  */
1835 #define EC_CMD_PSTORE_READ 0x41
1836
1837 struct ec_params_pstore_read {
1838         uint32_t offset;   /* Byte offset to read */
1839         uint32_t size;     /* Size to read in bytes */
1840 } __ec_align4;
1841
1842 /* Write persistent storage */
1843 #define EC_CMD_PSTORE_WRITE 0x42
1844
1845 struct ec_params_pstore_write {
1846         uint32_t offset;   /* Byte offset to write */
1847         uint32_t size;     /* Size to write in bytes */
1848         uint8_t data[EC_PSTORE_SIZE_MAX];
1849 } __ec_align4;
1850
1851 /*****************************************************************************/
1852 /* Real-time clock */
1853
1854 /* RTC params and response structures */
1855 struct ec_params_rtc {
1856         uint32_t time;
1857 } __ec_align4;
1858
1859 struct ec_response_rtc {
1860         uint32_t time;
1861 } __ec_align4;
1862
1863 /* These use ec_response_rtc */
1864 #define EC_CMD_RTC_GET_VALUE 0x44
1865 #define EC_CMD_RTC_GET_ALARM 0x45
1866
1867 /* These all use ec_params_rtc */
1868 #define EC_CMD_RTC_SET_VALUE 0x46
1869 #define EC_CMD_RTC_SET_ALARM 0x47
1870
1871 /* Pass as time param to SET_ALARM to clear the current alarm */
1872 #define EC_RTC_ALARM_CLEAR 0
1873
1874 /*****************************************************************************/
1875 /* Port80 log access */
1876
1877 /* Maximum entries that can be read/written in a single command */
1878 #define EC_PORT80_SIZE_MAX 32
1879
1880 /* Get last port80 code from previous boot */
1881 #define EC_CMD_PORT80_LAST_BOOT 0x48
1882 #define EC_CMD_PORT80_READ 0x48
1883
1884 enum ec_port80_subcmd {
1885         EC_PORT80_GET_INFO = 0,
1886         EC_PORT80_READ_BUFFER,
1887 };
1888
1889 struct ec_params_port80_read {
1890         uint16_t subcmd;
1891         union {
1892                 struct __ec_todo_unpacked {
1893                         uint32_t offset;
1894                         uint32_t num_entries;
1895                 } read_buffer;
1896         };
1897 } __ec_todo_packed;
1898
1899 struct ec_response_port80_read {
1900         union {
1901                 struct __ec_todo_unpacked {
1902                         uint32_t writes;
1903                         uint32_t history_size;
1904                         uint32_t last_boot;
1905                 } get_info;
1906                 struct __ec_todo_unpacked {
1907                         uint16_t codes[EC_PORT80_SIZE_MAX];
1908                 } data;
1909         };
1910 } __ec_todo_packed;
1911
1912 struct ec_response_port80_last_boot {
1913         uint16_t code;
1914 } __ec_align2;
1915
1916 /*****************************************************************************/
1917 /* Thermal engine commands. Note that there are two implementations. We'll
1918  * reuse the command number, but the data and behavior is incompatible.
1919  * Version 0 is what originally shipped on Link.
1920  * Version 1 separates the CPU thermal limits from the fan control.
1921  */
1922
1923 #define EC_CMD_THERMAL_SET_THRESHOLD 0x50
1924 #define EC_CMD_THERMAL_GET_THRESHOLD 0x51
1925
1926 /* The version 0 structs are opaque. You have to know what they are for
1927  * the get/set commands to make any sense.
1928  */
1929
1930 /* Version 0 - set */
1931 struct ec_params_thermal_set_threshold {
1932         uint8_t sensor_type;
1933         uint8_t threshold_id;
1934         uint16_t value;
1935 } __ec_align2;
1936
1937 /* Version 0 - get */
1938 struct ec_params_thermal_get_threshold {
1939         uint8_t sensor_type;
1940         uint8_t threshold_id;
1941 } __ec_align1;
1942
1943 struct ec_response_thermal_get_threshold {
1944         uint16_t value;
1945 } __ec_align2;
1946
1947
1948 /* The version 1 structs are visible. */
1949 enum ec_temp_thresholds {
1950         EC_TEMP_THRESH_WARN = 0,
1951         EC_TEMP_THRESH_HIGH,
1952         EC_TEMP_THRESH_HALT,
1953
1954         EC_TEMP_THRESH_COUNT
1955 };
1956
1957 /*
1958  * Thermal configuration for one temperature sensor. Temps are in degrees K.
1959  * Zero values will be silently ignored by the thermal task.
1960  */
1961 struct ec_thermal_config {
1962         uint32_t temp_host[EC_TEMP_THRESH_COUNT]; /* levels of hotness */
1963         uint32_t temp_fan_off;          /* no active cooling needed */
1964         uint32_t temp_fan_max;          /* max active cooling needed */
1965 } __ec_align4;
1966
1967 /* Version 1 - get config for one sensor. */
1968 struct ec_params_thermal_get_threshold_v1 {
1969         uint32_t sensor_num;
1970 } __ec_align4;
1971 /* This returns a struct ec_thermal_config */
1972
1973 /*
1974  * Version 1 - set config for one sensor.
1975  * Use read-modify-write for best results!
1976  */
1977 struct ec_params_thermal_set_threshold_v1 {
1978         uint32_t sensor_num;
1979         struct ec_thermal_config cfg;
1980 } __ec_align4;
1981 /* This returns no data */
1982
1983 /****************************************************************************/
1984
1985 /* Toggle automatic fan control */
1986 #define EC_CMD_THERMAL_AUTO_FAN_CTRL 0x52
1987
1988 /* Get TMP006 calibration data */
1989 #define EC_CMD_TMP006_GET_CALIBRATION 0x53
1990
1991 struct ec_params_tmp006_get_calibration {
1992         uint8_t index;
1993 } __ec_align1;
1994
1995 struct ec_response_tmp006_get_calibration {
1996         float s0;
1997         float b0;
1998         float b1;
1999         float b2;
2000 } __ec_align4;
2001
2002 /* Set TMP006 calibration data */
2003 #define EC_CMD_TMP006_SET_CALIBRATION 0x54
2004
2005 struct ec_params_tmp006_set_calibration {
2006         uint8_t index;
2007         uint8_t reserved[3];  /* Reserved; set 0 */
2008         float s0;
2009         float b0;
2010         float b1;
2011         float b2;
2012 } __ec_align4;
2013
2014 /* Read raw TMP006 data */
2015 #define EC_CMD_TMP006_GET_RAW 0x55
2016
2017 struct ec_params_tmp006_get_raw {
2018         uint8_t index;
2019 } __ec_align1;
2020
2021 struct ec_response_tmp006_get_raw {
2022         int32_t t;  /* In 1/100 K */
2023         int32_t v;  /* In nV */
2024 } __ec_align4;
2025
2026 /*****************************************************************************/
2027 /* MKBP - Matrix KeyBoard Protocol */
2028
2029 /*
2030  * Read key state
2031  *
2032  * Returns raw data for keyboard cols; see ec_response_mkbp_info.cols for
2033  * expected response size.
2034  *
2035  * NOTE: This has been superseded by EC_CMD_MKBP_GET_NEXT_EVENT.  If you wish
2036  * to obtain the instantaneous state, use EC_CMD_MKBP_INFO with the type
2037  * EC_MKBP_INFO_CURRENT and event EC_MKBP_EVENT_KEY_MATRIX.
2038  */
2039 #define EC_CMD_MKBP_STATE 0x60
2040
2041 /*
2042  * Provide information about various MKBP things.  See enum ec_mkbp_info_type.
2043  */
2044 #define EC_CMD_MKBP_INFO 0x61
2045
2046 struct ec_response_mkbp_info {
2047         uint32_t rows;
2048         uint32_t cols;
2049         /* Formerly "switches", which was 0. */
2050         uint8_t reserved;
2051 } __ec_align_size1;
2052
2053 struct ec_params_mkbp_info {
2054         uint8_t info_type;
2055         uint8_t event_type;
2056 } __ec_align1;
2057
2058 enum ec_mkbp_info_type {
2059         /*
2060          * Info about the keyboard matrix: number of rows and columns.
2061          *
2062          * Returns struct ec_response_mkbp_info.
2063          */
2064         EC_MKBP_INFO_KBD = 0,
2065
2066         /*
2067          * For buttons and switches, info about which specifically are
2068          * supported.  event_type must be set to one of the values in enum
2069          * ec_mkbp_event.
2070          *
2071          * For EC_MKBP_EVENT_BUTTON and EC_MKBP_EVENT_SWITCH, returns a 4 byte
2072          * bitmask indicating which buttons or switches are present.  See the
2073          * bit inidices below.
2074          */
2075         EC_MKBP_INFO_SUPPORTED = 1,
2076
2077         /*
2078          * Instantaneous state of buttons and switches.
2079          *
2080          * event_type must be set to one of the values in enum ec_mkbp_event.
2081          *
2082          * For EC_MKBP_EVENT_KEY_MATRIX, returns uint8_t key_matrix[13]
2083          * indicating the current state of the keyboard matrix.
2084          *
2085          * For EC_MKBP_EVENT_HOST_EVENT, return uint32_t host_event, the raw
2086          * event state.
2087          *
2088          * For EC_MKBP_EVENT_BUTTON, returns uint32_t buttons, indicating the
2089          * state of supported buttons.
2090          *
2091          * For EC_MKBP_EVENT_SWITCH, returns uint32_t switches, indicating the
2092          * state of supported switches.
2093          */
2094         EC_MKBP_INFO_CURRENT = 2,
2095 };
2096
2097 /* Simulate key press */
2098 #define EC_CMD_MKBP_SIMULATE_KEY 0x62
2099
2100 struct ec_params_mkbp_simulate_key {
2101         uint8_t col;
2102         uint8_t row;
2103         uint8_t pressed;
2104 } __ec_align1;
2105
2106 /* Configure keyboard scanning */
2107 #define EC_CMD_MKBP_SET_CONFIG 0x64
2108 #define EC_CMD_MKBP_GET_CONFIG 0x65
2109
2110 /* flags */
2111 enum mkbp_config_flags {
2112         EC_MKBP_FLAGS_ENABLE = 1,       /* Enable keyboard scanning */
2113 };
2114
2115 enum mkbp_config_valid {
2116         EC_MKBP_VALID_SCAN_PERIOD               = 1 << 0,
2117         EC_MKBP_VALID_POLL_TIMEOUT              = 1 << 1,
2118         EC_MKBP_VALID_MIN_POST_SCAN_DELAY       = 1 << 3,
2119         EC_MKBP_VALID_OUTPUT_SETTLE             = 1 << 4,
2120         EC_MKBP_VALID_DEBOUNCE_DOWN             = 1 << 5,
2121         EC_MKBP_VALID_DEBOUNCE_UP               = 1 << 6,
2122         EC_MKBP_VALID_FIFO_MAX_DEPTH            = 1 << 7,
2123 };
2124
2125 /*
2126  * Configuration for our key scanning algorithm.
2127  *
2128  * Note that this is used as a sub-structure of
2129  * ec_{params/response}_mkbp_get_config.
2130  */
2131 struct ec_mkbp_config {
2132         uint32_t valid_mask;            /* valid fields */
2133         uint8_t flags;          /* some flags (enum mkbp_config_flags) */
2134         uint8_t valid_flags;            /* which flags are valid */
2135         uint16_t scan_period_us;        /* period between start of scans */
2136         /* revert to interrupt mode after no activity for this long */
2137         uint32_t poll_timeout_us;
2138         /*
2139          * minimum post-scan relax time. Once we finish a scan we check
2140          * the time until we are due to start the next one. If this time is
2141          * shorter this field, we use this instead.
2142          */
2143         uint16_t min_post_scan_delay_us;
2144         /* delay between setting up output and waiting for it to settle */
2145         uint16_t output_settle_us;
2146         uint16_t debounce_down_us;      /* time for debounce on key down */
2147         uint16_t debounce_up_us;        /* time for debounce on key up */
2148         /* maximum depth to allow for fifo (0 = no keyscan output) */
2149         uint8_t fifo_max_depth;
2150 } __ec_align_size1;
2151
2152 struct ec_params_mkbp_set_config {
2153         struct ec_mkbp_config config;
2154 } __ec_align_size1;
2155
2156 struct ec_response_mkbp_get_config {
2157         struct ec_mkbp_config config;
2158 } __ec_align_size1;
2159
2160 /* Run the key scan emulation */
2161 #define EC_CMD_KEYSCAN_SEQ_CTRL 0x66
2162
2163 enum ec_keyscan_seq_cmd {
2164         EC_KEYSCAN_SEQ_STATUS = 0,      /* Get status information */
2165         EC_KEYSCAN_SEQ_CLEAR = 1,       /* Clear sequence */
2166         EC_KEYSCAN_SEQ_ADD = 2,         /* Add item to sequence */
2167         EC_KEYSCAN_SEQ_START = 3,       /* Start running sequence */
2168         EC_KEYSCAN_SEQ_COLLECT = 4,     /* Collect sequence summary data */
2169 };
2170
2171 enum ec_collect_flags {
2172         /*
2173          * Indicates this scan was processed by the EC. Due to timing, some
2174          * scans may be skipped.
2175          */
2176         EC_KEYSCAN_SEQ_FLAG_DONE        = 1 << 0,
2177 };
2178
2179 struct ec_collect_item {
2180         uint8_t flags;          /* some flags (enum ec_collect_flags) */
2181 } __ec_align1;
2182
2183 struct ec_params_keyscan_seq_ctrl {
2184         uint8_t cmd;    /* Command to send (enum ec_keyscan_seq_cmd) */
2185         union {
2186                 struct __ec_align1 {
2187                         uint8_t active;         /* still active */
2188                         uint8_t num_items;      /* number of items */
2189                         /* Current item being presented */
2190                         uint8_t cur_item;
2191                 } status;
2192                 struct __ec_todo_unpacked {
2193                         /*
2194                          * Absolute time for this scan, measured from the
2195                          * start of the sequence.
2196                          */
2197                         uint32_t time_us;
2198                         uint8_t scan[0];        /* keyscan data */
2199                 } add;
2200                 struct __ec_align1 {
2201                         uint8_t start_item;     /* First item to return */
2202                         uint8_t num_items;      /* Number of items to return */
2203                 } collect;
2204         };
2205 } __ec_todo_packed;
2206
2207 struct ec_result_keyscan_seq_ctrl {
2208         union {
2209                 struct __ec_todo_unpacked {
2210                         uint8_t num_items;      /* Number of items */
2211                         /* Data for each item */
2212                         struct ec_collect_item item[0];
2213                 } collect;
2214         };
2215 } __ec_todo_packed;
2216
2217 /*
2218  * Command for retrieving the next pending MKBP event from the EC device
2219  *
2220  * The device replies with UNAVAILABLE if there aren't any pending events.
2221  */
2222 #define EC_CMD_GET_NEXT_EVENT 0x67
2223
2224 enum ec_mkbp_event {
2225         /* Keyboard matrix changed. The event data is the new matrix state. */
2226         EC_MKBP_EVENT_KEY_MATRIX = 0,
2227
2228         /* New host event. The event data is 4 bytes of host event flags. */
2229         EC_MKBP_EVENT_HOST_EVENT = 1,
2230
2231         /* New Sensor FIFO data. The event data is fifo_info structure. */
2232         EC_MKBP_EVENT_SENSOR_FIFO = 2,
2233
2234         /* The state of the non-matrixed buttons have changed. */
2235         EC_MKBP_EVENT_BUTTON = 3,
2236
2237         /* The state of the switches have changed. */
2238         EC_MKBP_EVENT_SWITCH = 4,
2239
2240         /* EC sent a sysrq command */
2241         EC_MKBP_EVENT_SYSRQ = 6,
2242
2243         /* Notify the AP that something happened on CEC */
2244         EC_MKBP_EVENT_CEC_EVENT = 8,
2245
2246         /* Send an incoming CEC message to the AP */
2247         EC_MKBP_EVENT_CEC_MESSAGE = 9,
2248
2249         /* Number of MKBP events */
2250         EC_MKBP_EVENT_COUNT,
2251 };
2252
2253 union __ec_align_offset1 ec_response_get_next_data {
2254         uint8_t key_matrix[13];
2255
2256         /* Unaligned */
2257         uint32_t  host_event;
2258
2259         uint32_t   buttons;
2260         uint32_t   switches;
2261         uint32_t   sysrq;
2262 };
2263
2264 union __ec_align_offset1 ec_response_get_next_data_v1 {
2265         uint8_t key_matrix[16];
2266         uint32_t host_event;
2267         uint32_t buttons;
2268         uint32_t switches;
2269         uint32_t sysrq;
2270         uint32_t cec_events;
2271         uint8_t cec_message[16];
2272 };
2273
2274 struct ec_response_get_next_event {
2275         uint8_t event_type;
2276         /* Followed by event data if any */
2277         union ec_response_get_next_data data;
2278 } __ec_align1;
2279
2280 struct ec_response_get_next_event_v1 {
2281         uint8_t event_type;
2282         /* Followed by event data if any */
2283         union ec_response_get_next_data_v1 data;
2284 } __ec_align1;
2285
2286 /* Bit indices for buttons and switches.*/
2287 /* Buttons */
2288 #define EC_MKBP_POWER_BUTTON    0
2289 #define EC_MKBP_VOL_UP          1
2290 #define EC_MKBP_VOL_DOWN        2
2291
2292 /* Switches */
2293 #define EC_MKBP_LID_OPEN        0
2294 #define EC_MKBP_TABLET_MODE     1
2295 #define EC_MKBP_BASE_ATTACHED   2
2296
2297 /*****************************************************************************/
2298 /* Temperature sensor commands */
2299
2300 /* Read temperature sensor info */
2301 #define EC_CMD_TEMP_SENSOR_GET_INFO 0x70
2302
2303 struct ec_params_temp_sensor_get_info {
2304         uint8_t id;
2305 } __ec_align1;
2306
2307 struct ec_response_temp_sensor_get_info {
2308         char sensor_name[32];
2309         uint8_t sensor_type;
2310 } __ec_align1;
2311
2312 /*****************************************************************************/
2313
2314 /*
2315  * Note: host commands 0x80 - 0x87 are reserved to avoid conflict with ACPI
2316  * commands accidentally sent to the wrong interface.  See the ACPI section
2317  * below.
2318  */
2319
2320 /*****************************************************************************/
2321 /* Host event commands */
2322
2323 /*
2324  * Host event mask params and response structures, shared by all of the host
2325  * event commands below.
2326  */
2327 struct ec_params_host_event_mask {
2328         uint32_t mask;
2329 } __ec_align4;
2330
2331 struct ec_response_host_event_mask {
2332         uint32_t mask;
2333 } __ec_align4;
2334
2335 /* These all use ec_response_host_event_mask */
2336 #define EC_CMD_HOST_EVENT_GET_B         0x87
2337 #define EC_CMD_HOST_EVENT_GET_SMI_MASK  0x88
2338 #define EC_CMD_HOST_EVENT_GET_SCI_MASK  0x89
2339 #define EC_CMD_HOST_EVENT_GET_WAKE_MASK 0x8d
2340
2341 /* These all use ec_params_host_event_mask */
2342 #define EC_CMD_HOST_EVENT_SET_SMI_MASK  0x8a
2343 #define EC_CMD_HOST_EVENT_SET_SCI_MASK  0x8b
2344 #define EC_CMD_HOST_EVENT_CLEAR         0x8c
2345 #define EC_CMD_HOST_EVENT_SET_WAKE_MASK 0x8e
2346 #define EC_CMD_HOST_EVENT_CLEAR_B       0x8f
2347
2348 /*****************************************************************************/
2349 /* Switch commands */
2350
2351 /* Enable/disable LCD backlight */
2352 #define EC_CMD_SWITCH_ENABLE_BKLIGHT 0x90
2353
2354 struct ec_params_switch_enable_backlight {
2355         uint8_t enabled;
2356 } __ec_align1;
2357
2358 /* Enable/disable WLAN/Bluetooth */
2359 #define EC_CMD_SWITCH_ENABLE_WIRELESS 0x91
2360 #define EC_VER_SWITCH_ENABLE_WIRELESS 1
2361
2362 /* Version 0 params; no response */
2363 struct ec_params_switch_enable_wireless_v0 {
2364         uint8_t enabled;
2365 } __ec_align1;
2366
2367 /* Version 1 params */
2368 struct ec_params_switch_enable_wireless_v1 {
2369         /* Flags to enable now */
2370         uint8_t now_flags;
2371
2372         /* Which flags to copy from now_flags */
2373         uint8_t now_mask;
2374
2375         /*
2376          * Flags to leave enabled in S3, if they're on at the S0->S3
2377          * transition.  (Other flags will be disabled by the S0->S3
2378          * transition.)
2379          */
2380         uint8_t suspend_flags;
2381
2382         /* Which flags to copy from suspend_flags */
2383         uint8_t suspend_mask;
2384 } __ec_align1;
2385
2386 /* Version 1 response */
2387 struct ec_response_switch_enable_wireless_v1 {
2388         /* Flags to enable now */
2389         uint8_t now_flags;
2390
2391         /* Flags to leave enabled in S3 */
2392         uint8_t suspend_flags;
2393 } __ec_align1;
2394
2395 /*****************************************************************************/
2396 /* GPIO commands. Only available on EC if write protect has been disabled. */
2397
2398 /* Set GPIO output value */
2399 #define EC_CMD_GPIO_SET 0x92
2400
2401 struct ec_params_gpio_set {
2402         char name[32];
2403         uint8_t val;
2404 } __ec_align1;
2405
2406 /* Get GPIO value */
2407 #define EC_CMD_GPIO_GET 0x93
2408
2409 /* Version 0 of input params and response */
2410 struct ec_params_gpio_get {
2411         char name[32];
2412 } __ec_align1;
2413
2414 struct ec_response_gpio_get {
2415         uint8_t val;
2416 } __ec_align1;
2417
2418 /* Version 1 of input params and response */
2419 struct ec_params_gpio_get_v1 {
2420         uint8_t subcmd;
2421         union {
2422                 struct __ec_align1 {
2423                         char name[32];
2424                 } get_value_by_name;
2425                 struct __ec_align1 {
2426                         uint8_t index;
2427                 } get_info;
2428         };
2429 } __ec_align1;
2430
2431 struct ec_response_gpio_get_v1 {
2432         union {
2433                 struct __ec_align1 {
2434                         uint8_t val;
2435                 } get_value_by_name, get_count;
2436                 struct __ec_todo_unpacked {
2437                         uint8_t val;
2438                         char name[32];
2439                         uint32_t flags;
2440                 } get_info;
2441         };
2442 } __ec_todo_packed;
2443
2444 enum gpio_get_subcmd {
2445         EC_GPIO_GET_BY_NAME = 0,
2446         EC_GPIO_GET_COUNT = 1,
2447         EC_GPIO_GET_INFO = 2,
2448 };
2449
2450 /*****************************************************************************/
2451 /* I2C commands. Only available when flash write protect is unlocked. */
2452
2453 /*
2454  * CAUTION: These commands are deprecated, and are not supported anymore in EC
2455  * builds >= 8398.0.0 (see crosbug.com/p/23570).
2456  *
2457  * Use EC_CMD_I2C_PASSTHRU instead.
2458  */
2459
2460 /* Read I2C bus */
2461 #define EC_CMD_I2C_READ 0x94
2462
2463 struct ec_params_i2c_read {
2464         uint16_t addr; /* 8-bit address (7-bit shifted << 1) */
2465         uint8_t read_size; /* Either 8 or 16. */
2466         uint8_t port;
2467         uint8_t offset;
2468 } __ec_align_size1;
2469
2470 struct ec_response_i2c_read {
2471         uint16_t data;
2472 } __ec_align2;
2473
2474 /* Write I2C bus */
2475 #define EC_CMD_I2C_WRITE 0x95
2476
2477 struct ec_params_i2c_write {
2478         uint16_t data;
2479         uint16_t addr; /* 8-bit address (7-bit shifted << 1) */
2480         uint8_t write_size; /* Either 8 or 16. */
2481         uint8_t port;
2482         uint8_t offset;
2483 } __ec_align_size1;
2484
2485 /*****************************************************************************/
2486 /* Charge state commands. Only available when flash write protect unlocked. */
2487
2488 /* Force charge state machine to stop charging the battery or force it to
2489  * discharge the battery.
2490  */
2491 #define EC_CMD_CHARGE_CONTROL 0x96
2492 #define EC_VER_CHARGE_CONTROL 1
2493
2494 enum ec_charge_control_mode {
2495         CHARGE_CONTROL_NORMAL = 0,
2496         CHARGE_CONTROL_IDLE,
2497         CHARGE_CONTROL_DISCHARGE,
2498 };
2499
2500 struct ec_params_charge_control {
2501         uint32_t mode;  /* enum charge_control_mode */
2502 } __ec_align4;
2503
2504 /*****************************************************************************/
2505
2506 /* Snapshot console output buffer for use by EC_CMD_CONSOLE_READ. */
2507 #define EC_CMD_CONSOLE_SNAPSHOT 0x97
2508
2509 /*
2510  * Read data from the saved snapshot. If the subcmd parameter is
2511  * CONSOLE_READ_NEXT, this will return data starting from the beginning of
2512  * the latest snapshot. If it is CONSOLE_READ_RECENT, it will start from the
2513  * end of the previous snapshot.
2514  *
2515  * The params are only looked at in version >= 1 of this command. Prior
2516  * versions will just default to CONSOLE_READ_NEXT behavior.
2517  *
2518  * Response is null-terminated string.  Empty string, if there is no more
2519  * remaining output.
2520  */
2521 #define EC_CMD_CONSOLE_READ 0x98
2522
2523 enum ec_console_read_subcmd {
2524         CONSOLE_READ_NEXT = 0,
2525         CONSOLE_READ_RECENT
2526 };
2527
2528 struct ec_params_console_read_v1 {
2529         uint8_t subcmd; /* enum ec_console_read_subcmd */
2530 } __ec_align1;
2531
2532 /*****************************************************************************/
2533
2534 /*
2535  * Cut off battery power immediately or after the host has shut down.
2536  *
2537  * return EC_RES_INVALID_COMMAND if unsupported by a board/battery.
2538  *        EC_RES_SUCCESS if the command was successful.
2539  *        EC_RES_ERROR if the cut off command failed.
2540  */
2541
2542 #define EC_CMD_BATTERY_CUT_OFF 0x99
2543
2544 #define EC_BATTERY_CUTOFF_FLAG_AT_SHUTDOWN      (1 << 0)
2545
2546 struct ec_params_battery_cutoff {
2547         uint8_t flags;
2548 } __ec_align1;
2549
2550 /*****************************************************************************/
2551 /* USB port mux control. */
2552
2553 /*
2554  * Switch USB mux or return to automatic switching.
2555  */
2556 #define EC_CMD_USB_MUX 0x9a
2557
2558 struct ec_params_usb_mux {
2559         uint8_t mux;
2560 } __ec_align1;
2561
2562 /*****************************************************************************/
2563 /* LDOs / FETs control. */
2564
2565 enum ec_ldo_state {
2566         EC_LDO_STATE_OFF = 0,   /* the LDO / FET is shut down */
2567         EC_LDO_STATE_ON = 1,    /* the LDO / FET is ON / providing power */
2568 };
2569
2570 /*
2571  * Switch on/off a LDO.
2572  */
2573 #define EC_CMD_LDO_SET 0x9b
2574
2575 struct ec_params_ldo_set {
2576         uint8_t index;
2577         uint8_t state;
2578 } __ec_align1;
2579
2580 /*
2581  * Get LDO state.
2582  */
2583 #define EC_CMD_LDO_GET 0x9c
2584
2585 struct ec_params_ldo_get {
2586         uint8_t index;
2587 } __ec_align1;
2588
2589 struct ec_response_ldo_get {
2590         uint8_t state;
2591 } __ec_align1;
2592
2593 /*****************************************************************************/
2594 /* Power info. */
2595
2596 /*
2597  * Get power info.
2598  */
2599 #define EC_CMD_POWER_INFO 0x9d
2600
2601 struct ec_response_power_info {
2602         uint32_t usb_dev_type;
2603         uint16_t voltage_ac;
2604         uint16_t voltage_system;
2605         uint16_t current_system;
2606         uint16_t usb_current_limit;
2607 } __ec_align4;
2608
2609 /*****************************************************************************/
2610 /* I2C passthru command */
2611
2612 #define EC_CMD_I2C_PASSTHRU 0x9e
2613
2614 /* Read data; if not present, message is a write */
2615 #define EC_I2C_FLAG_READ        (1 << 15)
2616
2617 /* Mask for address */
2618 #define EC_I2C_ADDR_MASK        0x3ff
2619
2620 #define EC_I2C_STATUS_NAK       (1 << 0) /* Transfer was not acknowledged */
2621 #define EC_I2C_STATUS_TIMEOUT   (1 << 1) /* Timeout during transfer */
2622
2623 /* Any error */
2624 #define EC_I2C_STATUS_ERROR     (EC_I2C_STATUS_NAK | EC_I2C_STATUS_TIMEOUT)
2625
2626 struct ec_params_i2c_passthru_msg {
2627         uint16_t addr_flags;    /* I2C slave address (7 or 10 bits) and flags */
2628         uint16_t len;           /* Number of bytes to read or write */
2629 } __ec_align2;
2630
2631 struct ec_params_i2c_passthru {
2632         uint8_t port;           /* I2C port number */
2633         uint8_t num_msgs;       /* Number of messages */
2634         struct ec_params_i2c_passthru_msg msg[];
2635         /* Data to write for all messages is concatenated here */
2636 } __ec_align2;
2637
2638 struct ec_response_i2c_passthru {
2639         uint8_t i2c_status;     /* Status flags (EC_I2C_STATUS_...) */
2640         uint8_t num_msgs;       /* Number of messages processed */
2641         uint8_t data[];         /* Data read by messages concatenated here */
2642 } __ec_align1;
2643
2644 /*****************************************************************************/
2645 /* Power button hang detect */
2646
2647 #define EC_CMD_HANG_DETECT 0x9f
2648
2649 /* Reasons to start hang detection timer */
2650 /* Power button pressed */
2651 #define EC_HANG_START_ON_POWER_PRESS  (1 << 0)
2652
2653 /* Lid closed */
2654 #define EC_HANG_START_ON_LID_CLOSE    (1 << 1)
2655
2656  /* Lid opened */
2657 #define EC_HANG_START_ON_LID_OPEN     (1 << 2)
2658
2659 /* Start of AP S3->S0 transition (booting or resuming from suspend) */
2660 #define EC_HANG_START_ON_RESUME       (1 << 3)
2661
2662 /* Reasons to cancel hang detection */
2663
2664 /* Power button released */
2665 #define EC_HANG_STOP_ON_POWER_RELEASE (1 << 8)
2666
2667 /* Any host command from AP received */
2668 #define EC_HANG_STOP_ON_HOST_COMMAND  (1 << 9)
2669
2670 /* Stop on end of AP S0->S3 transition (suspending or shutting down) */
2671 #define EC_HANG_STOP_ON_SUSPEND       (1 << 10)
2672
2673 /*
2674  * If this flag is set, all the other fields are ignored, and the hang detect
2675  * timer is started.  This provides the AP a way to start the hang timer
2676  * without reconfiguring any of the other hang detect settings.  Note that
2677  * you must previously have configured the timeouts.
2678  */
2679 #define EC_HANG_START_NOW             (1 << 30)
2680
2681 /*
2682  * If this flag is set, all the other fields are ignored (including
2683  * EC_HANG_START_NOW).  This provides the AP a way to stop the hang timer
2684  * without reconfiguring any of the other hang detect settings.
2685  */
2686 #define EC_HANG_STOP_NOW              (1 << 31)
2687
2688 struct ec_params_hang_detect {
2689         /* Flags; see EC_HANG_* */
2690         uint32_t flags;
2691
2692         /* Timeout in msec before generating host event, if enabled */
2693         uint16_t host_event_timeout_msec;
2694
2695         /* Timeout in msec before generating warm reboot, if enabled */
2696         uint16_t warm_reboot_timeout_msec;
2697 } __ec_align4;
2698
2699 /*****************************************************************************/
2700 /* Commands for battery charging */
2701
2702 /*
2703  * This is the single catch-all host command to exchange data regarding the
2704  * charge state machine (v2 and up).
2705  */
2706 #define EC_CMD_CHARGE_STATE 0xa0
2707
2708 /* Subcommands for this host command */
2709 enum charge_state_command {
2710         CHARGE_STATE_CMD_GET_STATE,
2711         CHARGE_STATE_CMD_GET_PARAM,
2712         CHARGE_STATE_CMD_SET_PARAM,
2713         CHARGE_STATE_NUM_CMDS
2714 };
2715
2716 /*
2717  * Known param numbers are defined here. Ranges are reserved for board-specific
2718  * params, which are handled by the particular implementations.
2719  */
2720 enum charge_state_params {
2721         CS_PARAM_CHG_VOLTAGE,         /* charger voltage limit */
2722         CS_PARAM_CHG_CURRENT,         /* charger current limit */
2723         CS_PARAM_CHG_INPUT_CURRENT,   /* charger input current limit */
2724         CS_PARAM_CHG_STATUS,          /* charger-specific status */
2725         CS_PARAM_CHG_OPTION,          /* charger-specific options */
2726         /* How many so far? */
2727         CS_NUM_BASE_PARAMS,
2728
2729         /* Range for CONFIG_CHARGER_PROFILE_OVERRIDE params */
2730         CS_PARAM_CUSTOM_PROFILE_MIN = 0x10000,
2731         CS_PARAM_CUSTOM_PROFILE_MAX = 0x1ffff,
2732
2733         /* Other custom param ranges go here... */
2734 };
2735
2736 struct ec_params_charge_state {
2737         uint8_t cmd;                            /* enum charge_state_command */
2738         union {
2739                 struct {
2740                         /* no args */
2741                 } get_state;
2742
2743                 struct __ec_todo_unpacked {
2744                         uint32_t param;         /* enum charge_state_param */
2745                 } get_param;
2746
2747                 struct __ec_todo_unpacked {
2748                         uint32_t param;         /* param to set */
2749                         uint32_t value;         /* value to set */
2750                 } set_param;
2751         };
2752 } __ec_todo_packed;
2753
2754 struct ec_response_charge_state {
2755         union {
2756                 struct __ec_align4 {
2757                         int ac;
2758                         int chg_voltage;
2759                         int chg_current;
2760                         int chg_input_current;
2761                         int batt_state_of_charge;
2762                 } get_state;
2763
2764                 struct __ec_align4 {
2765                         uint32_t value;
2766                 } get_param;
2767                 struct {
2768                         /* no return values */
2769                 } set_param;
2770         };
2771 } __ec_align4;
2772
2773
2774 /*
2775  * Set maximum battery charging current.
2776  */
2777 #define EC_CMD_CHARGE_CURRENT_LIMIT 0xa1
2778
2779 struct ec_params_current_limit {
2780         uint32_t limit; /* in mA */
2781 } __ec_align4;
2782
2783 /*
2784  * Set maximum external voltage / current.
2785  */
2786 #define EC_CMD_EXTERNAL_POWER_LIMIT 0x00A2
2787
2788 /* Command v0 is used only on Spring and is obsolete + unsupported */
2789 struct ec_params_external_power_limit_v1 {
2790         uint16_t current_lim; /* in mA, or EC_POWER_LIMIT_NONE to clear limit */
2791         uint16_t voltage_lim; /* in mV, or EC_POWER_LIMIT_NONE to clear limit */
2792 } __ec_align2;
2793
2794 #define EC_POWER_LIMIT_NONE 0xffff
2795
2796 /* Inform the EC when entering a sleep state */
2797 #define EC_CMD_HOST_SLEEP_EVENT 0xa9
2798
2799 enum host_sleep_event {
2800         HOST_SLEEP_EVENT_S3_SUSPEND   = 1,
2801         HOST_SLEEP_EVENT_S3_RESUME    = 2,
2802         HOST_SLEEP_EVENT_S0IX_SUSPEND = 3,
2803         HOST_SLEEP_EVENT_S0IX_RESUME  = 4
2804 };
2805
2806 struct ec_params_host_sleep_event {
2807         uint8_t sleep_event;
2808 } __ec_align1;
2809
2810 /*
2811  * Use a default timeout value (CONFIG_SLEEP_TIMEOUT_MS) for detecting sleep
2812  * transition failures
2813  */
2814 #define EC_HOST_SLEEP_TIMEOUT_DEFAULT 0
2815
2816 /* Disable timeout detection for this sleep transition */
2817 #define EC_HOST_SLEEP_TIMEOUT_INFINITE 0xFFFF
2818
2819 struct ec_params_host_sleep_event_v1 {
2820         /* The type of sleep being entered or exited. */
2821         uint8_t sleep_event;
2822
2823         /* Padding */
2824         uint8_t reserved;
2825         union {
2826                 /* Parameters that apply for suspend messages. */
2827                 struct {
2828                         /*
2829                          * The timeout in milliseconds between when this message
2830                          * is received and when the EC will declare sleep
2831                          * transition failure if the sleep signal is not
2832                          * asserted.
2833                          */
2834                         uint16_t sleep_timeout_ms;
2835                 } suspend_params;
2836
2837                 /* No parameters for non-suspend messages. */
2838         };
2839 } __ec_align2;
2840
2841 /* A timeout occurred when this bit is set */
2842 #define EC_HOST_RESUME_SLEEP_TIMEOUT 0x80000000
2843
2844 /*
2845  * The mask defining which bits correspond to the number of sleep transitions,
2846  * as well as the maximum number of suspend line transitions that will be
2847  * reported back to the host.
2848  */
2849 #define EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK 0x7FFFFFFF
2850
2851 struct ec_response_host_sleep_event_v1 {
2852         union {
2853                 /* Response fields that apply for resume messages. */
2854                 struct {
2855                         /*
2856                          * The number of sleep power signal transitions that
2857                          * occurred since the suspend message. The high bit
2858                          * indicates a timeout occurred.
2859                          */
2860                         uint32_t sleep_transitions;
2861                 } resume_response;
2862
2863                 /* No response fields for non-resume messages. */
2864         };
2865 } __ec_align4;
2866
2867 /*****************************************************************************/
2868 /* Smart battery pass-through */
2869
2870 /* Get / Set 16-bit smart battery registers */
2871 #define EC_CMD_SB_READ_WORD   0xb0
2872 #define EC_CMD_SB_WRITE_WORD  0xb1
2873
2874 /* Get / Set string smart battery parameters
2875  * formatted as SMBUS "block".
2876  */
2877 #define EC_CMD_SB_READ_BLOCK  0xb2
2878 #define EC_CMD_SB_WRITE_BLOCK 0xb3
2879
2880 struct ec_params_sb_rd {
2881         uint8_t reg;
2882 } __ec_align1;
2883
2884 struct ec_response_sb_rd_word {
2885         uint16_t value;
2886 } __ec_align2;
2887
2888 struct ec_params_sb_wr_word {
2889         uint8_t reg;
2890         uint16_t value;
2891 } __ec_align1;
2892
2893 struct ec_response_sb_rd_block {
2894         uint8_t data[32];
2895 } __ec_align1;
2896
2897 struct ec_params_sb_wr_block {
2898         uint8_t reg;
2899         uint16_t data[32];
2900 } __ec_align1;
2901
2902 /*****************************************************************************/
2903 /* Battery vendor parameters
2904  *
2905  * Get or set vendor-specific parameters in the battery. Implementations may
2906  * differ between boards or batteries. On a set operation, the response
2907  * contains the actual value set, which may be rounded or clipped from the
2908  * requested value.
2909  */
2910
2911 #define EC_CMD_BATTERY_VENDOR_PARAM 0xb4
2912
2913 enum ec_battery_vendor_param_mode {
2914         BATTERY_VENDOR_PARAM_MODE_GET = 0,
2915         BATTERY_VENDOR_PARAM_MODE_SET,
2916 };
2917
2918 struct ec_params_battery_vendor_param {
2919         uint32_t param;
2920         uint32_t value;
2921         uint8_t mode;
2922 } __ec_align_size1;
2923
2924 struct ec_response_battery_vendor_param {
2925         uint32_t value;
2926 } __ec_align4;
2927
2928 /*****************************************************************************/
2929 /* Commands for I2S recording on audio codec. */
2930
2931 #define EC_CMD_CODEC_I2S 0x00BC
2932
2933 enum ec_codec_i2s_subcmd {
2934         EC_CODEC_SET_SAMPLE_DEPTH = 0x0,
2935         EC_CODEC_SET_GAIN = 0x1,
2936         EC_CODEC_GET_GAIN = 0x2,
2937         EC_CODEC_I2S_ENABLE = 0x3,
2938         EC_CODEC_I2S_SET_CONFIG = 0x4,
2939         EC_CODEC_I2S_SET_TDM_CONFIG = 0x5,
2940         EC_CODEC_I2S_SET_BCLK = 0x6,
2941 };
2942
2943 enum ec_sample_depth_value {
2944         EC_CODEC_SAMPLE_DEPTH_16 = 0,
2945         EC_CODEC_SAMPLE_DEPTH_24 = 1,
2946 };
2947
2948 enum ec_i2s_config {
2949         EC_DAI_FMT_I2S = 0,
2950         EC_DAI_FMT_RIGHT_J = 1,
2951         EC_DAI_FMT_LEFT_J = 2,
2952         EC_DAI_FMT_PCM_A = 3,
2953         EC_DAI_FMT_PCM_B = 4,
2954         EC_DAI_FMT_PCM_TDM = 5,
2955 };
2956
2957 struct __ec_todo_packed ec_param_codec_i2s {
2958         /* enum ec_codec_i2s_subcmd */
2959         uint8_t cmd;
2960         union {
2961                 /*
2962                  * EC_CODEC_SET_SAMPLE_DEPTH
2963                  * Value should be one of ec_sample_depth_value.
2964                  */
2965                 uint8_t depth;
2966
2967                 /*
2968                  * EC_CODEC_SET_GAIN
2969                  * Value should be 0~43 for both channels.
2970                  */
2971                 struct __ec_align1 ec_param_codec_i2s_set_gain {
2972                         uint8_t left;
2973                         uint8_t right;
2974                 } gain;
2975
2976                 /*
2977                  * EC_CODEC_I2S_ENABLE
2978                  * 1 to enable, 0 to disable.
2979                  */
2980                 uint8_t i2s_enable;
2981
2982                 /*
2983                  * EC_CODEC_I2S_SET_COFNIG
2984                  * Value should be one of ec_i2s_config.
2985                  */
2986                 uint8_t i2s_config;
2987
2988                 /*
2989                  * EC_CODEC_I2S_SET_TDM_CONFIG
2990                  * Value should be one of ec_i2s_config.
2991                  */
2992                 struct __ec_todo_unpacked ec_param_codec_i2s_tdm {
2993                         /*
2994                          * 0 to 496
2995                          */
2996                         int16_t ch0_delay;
2997                         /*
2998                          * -1 to 496
2999                          */
3000                         int16_t ch1_delay;
3001                         uint8_t adjacent_to_ch0;
3002                         uint8_t adjacent_to_ch1;
3003                 } tdm_param;
3004
3005                 /*
3006                  * EC_CODEC_I2S_SET_BCLK
3007                  */
3008                 uint32_t bclk;
3009         };
3010 };
3011
3012 /*
3013  * For subcommand EC_CODEC_GET_GAIN.
3014  */
3015 struct ec_response_codec_gain {
3016         uint8_t left;
3017         uint8_t right;
3018 } __ec_align1;
3019
3020 /*****************************************************************************/
3021 /* System commands */
3022
3023 /*
3024  * TODO(crosbug.com/p/23747): This is a confusing name, since it doesn't
3025  * necessarily reboot the EC.  Rename to "image" or something similar?
3026  */
3027 #define EC_CMD_REBOOT_EC 0xd2
3028
3029 /* Command */
3030 enum ec_reboot_cmd {
3031         EC_REBOOT_CANCEL = 0,        /* Cancel a pending reboot */
3032         EC_REBOOT_JUMP_RO = 1,       /* Jump to RO without rebooting */
3033         EC_REBOOT_JUMP_RW = 2,       /* Jump to active RW without rebooting */
3034         /* (command 3 was jump to RW-B) */
3035         EC_REBOOT_COLD = 4,          /* Cold-reboot */
3036         EC_REBOOT_DISABLE_JUMP = 5,  /* Disable jump until next reboot */
3037         EC_REBOOT_HIBERNATE = 6      /* Hibernate EC */
3038 };
3039
3040 /* Flags for ec_params_reboot_ec.reboot_flags */
3041 #define EC_REBOOT_FLAG_RESERVED0      (1 << 0)  /* Was recovery request */
3042 #define EC_REBOOT_FLAG_ON_AP_SHUTDOWN (1 << 1)  /* Reboot after AP shutdown */
3043
3044 struct ec_params_reboot_ec {
3045         uint8_t cmd;           /* enum ec_reboot_cmd */
3046         uint8_t flags;         /* See EC_REBOOT_FLAG_* */
3047 } __ec_align1;
3048
3049 /*
3050  * Get information on last EC panic.
3051  *
3052  * Returns variable-length platform-dependent panic information.  See panic.h
3053  * for details.
3054  */
3055 #define EC_CMD_GET_PANIC_INFO 0xd3
3056
3057 /*****************************************************************************/
3058 /*
3059  * ACPI commands
3060  *
3061  * These are valid ONLY on the ACPI command/data port.
3062  */
3063
3064 /*
3065  * ACPI Read Embedded Controller
3066  *
3067  * This reads from ACPI memory space on the EC (EC_ACPI_MEM_*).
3068  *
3069  * Use the following sequence:
3070  *
3071  *    - Write EC_CMD_ACPI_READ to EC_LPC_ADDR_ACPI_CMD
3072  *    - Wait for EC_LPC_CMDR_PENDING bit to clear
3073  *    - Write address to EC_LPC_ADDR_ACPI_DATA
3074  *    - Wait for EC_LPC_CMDR_DATA bit to set
3075  *    - Read value from EC_LPC_ADDR_ACPI_DATA
3076  */
3077 #define EC_CMD_ACPI_READ 0x80
3078
3079 /*
3080  * ACPI Write Embedded Controller
3081  *
3082  * This reads from ACPI memory space on the EC (EC_ACPI_MEM_*).
3083  *
3084  * Use the following sequence:
3085  *
3086  *    - Write EC_CMD_ACPI_WRITE to EC_LPC_ADDR_ACPI_CMD
3087  *    - Wait for EC_LPC_CMDR_PENDING bit to clear
3088  *    - Write address to EC_LPC_ADDR_ACPI_DATA
3089  *    - Wait for EC_LPC_CMDR_PENDING bit to clear
3090  *    - Write value to EC_LPC_ADDR_ACPI_DATA
3091  */
3092 #define EC_CMD_ACPI_WRITE 0x81
3093
3094 /*
3095  * ACPI Query Embedded Controller
3096  *
3097  * This clears the lowest-order bit in the currently pending host events, and
3098  * sets the result code to the 1-based index of the bit (event 0x00000001 = 1,
3099  * event 0x80000000 = 32), or 0 if no event was pending.
3100  */
3101 #define EC_CMD_ACPI_QUERY_EVENT 0x84
3102
3103 /* Valid addresses in ACPI memory space, for read/write commands */
3104
3105 /* Memory space version; set to EC_ACPI_MEM_VERSION_CURRENT */
3106 #define EC_ACPI_MEM_VERSION            0x00
3107 /*
3108  * Test location; writing value here updates test compliment byte to (0xff -
3109  * value).
3110  */
3111 #define EC_ACPI_MEM_TEST               0x01
3112 /* Test compliment; writes here are ignored. */
3113 #define EC_ACPI_MEM_TEST_COMPLIMENT    0x02
3114
3115 /* Keyboard backlight brightness percent (0 - 100) */
3116 #define EC_ACPI_MEM_KEYBOARD_BACKLIGHT 0x03
3117 /* DPTF Target Fan Duty (0-100, 0xff for auto/none) */
3118 #define EC_ACPI_MEM_FAN_DUTY           0x04
3119
3120 /*
3121  * DPTF temp thresholds. Any of the EC's temp sensors can have up to two
3122  * independent thresholds attached to them. The current value of the ID
3123  * register determines which sensor is affected by the THRESHOLD and COMMIT
3124  * registers. The THRESHOLD register uses the same EC_TEMP_SENSOR_OFFSET scheme
3125  * as the memory-mapped sensors. The COMMIT register applies those settings.
3126  *
3127  * The spec does not mandate any way to read back the threshold settings
3128  * themselves, but when a threshold is crossed the AP needs a way to determine
3129  * which sensor(s) are responsible. Each reading of the ID register clears and
3130  * returns one sensor ID that has crossed one of its threshold (in either
3131  * direction) since the last read. A value of 0xFF means "no new thresholds
3132  * have tripped". Setting or enabling the thresholds for a sensor will clear
3133  * the unread event count for that sensor.
3134  */
3135 #define EC_ACPI_MEM_TEMP_ID            0x05
3136 #define EC_ACPI_MEM_TEMP_THRESHOLD     0x06
3137 #define EC_ACPI_MEM_TEMP_COMMIT        0x07
3138 /*
3139  * Here are the bits for the COMMIT register:
3140  *   bit 0 selects the threshold index for the chosen sensor (0/1)
3141  *   bit 1 enables/disables the selected threshold (0 = off, 1 = on)
3142  * Each write to the commit register affects one threshold.
3143  */
3144 #define EC_ACPI_MEM_TEMP_COMMIT_SELECT_MASK (1 << 0)
3145 #define EC_ACPI_MEM_TEMP_COMMIT_ENABLE_MASK (1 << 1)
3146 /*
3147  * Example:
3148  *
3149  * Set the thresholds for sensor 2 to 50 C and 60 C:
3150  *   write 2 to [0x05]      --  select temp sensor 2
3151  *   write 0x7b to [0x06]   --  C_TO_K(50) - EC_TEMP_SENSOR_OFFSET
3152  *   write 0x2 to [0x07]    --  enable threshold 0 with this value
3153  *   write 0x85 to [0x06]   --  C_TO_K(60) - EC_TEMP_SENSOR_OFFSET
3154  *   write 0x3 to [0x07]    --  enable threshold 1 with this value
3155  *
3156  * Disable the 60 C threshold, leaving the 50 C threshold unchanged:
3157  *   write 2 to [0x05]      --  select temp sensor 2
3158  *   write 0x1 to [0x07]    --  disable threshold 1
3159  */
3160
3161 /* DPTF battery charging current limit */
3162 #define EC_ACPI_MEM_CHARGING_LIMIT     0x08
3163
3164 /* Charging limit is specified in 64 mA steps */
3165 #define EC_ACPI_MEM_CHARGING_LIMIT_STEP_MA   64
3166 /* Value to disable DPTF battery charging limit */
3167 #define EC_ACPI_MEM_CHARGING_LIMIT_DISABLED  0xff
3168
3169 /* Current version of ACPI memory address space */
3170 #define EC_ACPI_MEM_VERSION_CURRENT 1
3171
3172
3173 /*****************************************************************************/
3174 /*
3175  * HDMI CEC commands
3176  *
3177  * These commands are for sending and receiving message via HDMI CEC
3178  */
3179 #define EC_MAX_CEC_MSG_LEN 16
3180
3181 /* CEC message from the AP to be written on the CEC bus */
3182 #define EC_CMD_CEC_WRITE_MSG 0x00B8
3183
3184 /**
3185  * struct ec_params_cec_write - Message to write to the CEC bus
3186  * @msg: message content to write to the CEC bus
3187  */
3188 struct ec_params_cec_write {
3189         uint8_t msg[EC_MAX_CEC_MSG_LEN];
3190 } __ec_align1;
3191
3192 /* Set various CEC parameters */
3193 #define EC_CMD_CEC_SET 0x00BA
3194
3195 /**
3196  * struct ec_params_cec_set - CEC parameters set
3197  * @cmd: parameter type, can be CEC_CMD_ENABLE or CEC_CMD_LOGICAL_ADDRESS
3198  * @val: in case cmd is CEC_CMD_ENABLE, this field can be 0 to disable CEC
3199  *      or 1 to enable CEC functionality, in case cmd is CEC_CMD_LOGICAL_ADDRESS,
3200  *      this field encodes the requested logical address between 0 and 15
3201  *      or 0xff to unregister
3202  */
3203 struct ec_params_cec_set {
3204         uint8_t cmd; /* enum cec_command */
3205         uint8_t val;
3206 } __ec_align1;
3207
3208 /* Read various CEC parameters */
3209 #define EC_CMD_CEC_GET 0x00BB
3210
3211 /**
3212  * struct ec_params_cec_get - CEC parameters get
3213  * @cmd: parameter type, can be CEC_CMD_ENABLE or CEC_CMD_LOGICAL_ADDRESS
3214  */
3215 struct ec_params_cec_get {
3216         uint8_t cmd; /* enum cec_command */
3217 } __ec_align1;
3218
3219 /**
3220  * struct ec_response_cec_get - CEC parameters get response
3221  * @val: in case cmd was CEC_CMD_ENABLE, this field will 0 if CEC is
3222  *      disabled or 1 if CEC functionality is enabled,
3223  *      in case cmd was CEC_CMD_LOGICAL_ADDRESS, this will encode the
3224  *      configured logical address between 0 and 15 or 0xff if unregistered
3225  */
3226 struct ec_response_cec_get {
3227         uint8_t val;
3228 } __ec_align1;
3229
3230 /* CEC parameters command */
3231 enum ec_cec_command {
3232         /* CEC reading, writing and events enable */
3233         CEC_CMD_ENABLE,
3234         /* CEC logical address  */
3235         CEC_CMD_LOGICAL_ADDRESS,
3236 };
3237
3238 /* Events from CEC to AP */
3239 enum mkbp_cec_event {
3240         /* Outgoing message was acknowledged by a follower */
3241         EC_MKBP_CEC_SEND_OK                     = BIT(0),
3242         /* Outgoing message was not acknowledged */
3243         EC_MKBP_CEC_SEND_FAILED                 = BIT(1),
3244 };
3245
3246 /*****************************************************************************/
3247 /*
3248  * Special commands
3249  *
3250  * These do not follow the normal rules for commands.  See each command for
3251  * details.
3252  */
3253
3254 /*
3255  * Reboot NOW
3256  *
3257  * This command will work even when the EC LPC interface is busy, because the
3258  * reboot command is processed at interrupt level.  Note that when the EC
3259  * reboots, the host will reboot too, so there is no response to this command.
3260  *
3261  * Use EC_CMD_REBOOT_EC to reboot the EC more politely.
3262  */
3263 #define EC_CMD_REBOOT 0xd1  /* Think "die" */
3264
3265 /*
3266  * Resend last response (not supported on LPC).
3267  *
3268  * Returns EC_RES_UNAVAILABLE if there is no response available - for example,
3269  * there was no previous command, or the previous command's response was too
3270  * big to save.
3271  */
3272 #define EC_CMD_RESEND_RESPONSE 0xdb
3273
3274 /*
3275  * This header byte on a command indicate version 0. Any header byte less
3276  * than this means that we are talking to an old EC which doesn't support
3277  * versioning. In that case, we assume version 0.
3278  *
3279  * Header bytes greater than this indicate a later version. For example,
3280  * EC_CMD_VERSION0 + 1 means we are using version 1.
3281  *
3282  * The old EC interface must not use commands 0xdc or higher.
3283  */
3284 #define EC_CMD_VERSION0 0xdc
3285
3286 #endif  /* !__ACPI__ */
3287
3288 /*****************************************************************************/
3289 /*
3290  * PD commands
3291  *
3292  * These commands are for PD MCU communication.
3293  */
3294
3295 /* EC to PD MCU exchange status command */
3296 #define EC_CMD_PD_EXCHANGE_STATUS 0x100
3297
3298 /* Status of EC being sent to PD */
3299 struct ec_params_pd_status {
3300         int8_t batt_soc; /* battery state of charge */
3301 } __ec_align1;
3302
3303 /* Status of PD being sent back to EC */
3304 struct ec_response_pd_status {
3305         int8_t status;        /* PD MCU status */
3306         uint32_t curr_lim_ma; /* input current limit */
3307 } __ec_align_size1;
3308
3309 /* Set USB type-C port role and muxes */
3310 #define EC_CMD_USB_PD_CONTROL 0x101
3311
3312 enum usb_pd_control_role {
3313         USB_PD_CTRL_ROLE_NO_CHANGE = 0,
3314         USB_PD_CTRL_ROLE_TOGGLE_ON = 1, /* == AUTO */
3315         USB_PD_CTRL_ROLE_TOGGLE_OFF = 2,
3316         USB_PD_CTRL_ROLE_FORCE_SINK = 3,
3317         USB_PD_CTRL_ROLE_FORCE_SOURCE = 4,
3318 };
3319
3320 enum usb_pd_control_mux {
3321         USB_PD_CTRL_MUX_NO_CHANGE = 0,
3322         USB_PD_CTRL_MUX_NONE = 1,
3323         USB_PD_CTRL_MUX_USB = 2,
3324         USB_PD_CTRL_MUX_DP = 3,
3325         USB_PD_CTRL_MUX_DOCK = 4,
3326         USB_PD_CTRL_MUX_AUTO = 5,
3327 };
3328
3329 enum usb_pd_control_swap {
3330         USB_PD_CTRL_SWAP_NONE = 0,
3331         USB_PD_CTRL_SWAP_DATA = 1,
3332         USB_PD_CTRL_SWAP_POWER = 2,
3333         USB_PD_CTRL_SWAP_VCONN = 3,
3334         USB_PD_CTRL_SWAP_COUNT
3335 };
3336
3337 struct ec_params_usb_pd_control {
3338         uint8_t port;
3339         uint8_t role;
3340         uint8_t mux;
3341         uint8_t swap;
3342 } __ec_align1;
3343
3344 #define PD_CTRL_RESP_ENABLED_COMMS      (1 << 0) /* Communication enabled */
3345 #define PD_CTRL_RESP_ENABLED_CONNECTED  (1 << 1) /* Device connected */
3346 #define PD_CTRL_RESP_ENABLED_PD_CAPABLE (1 << 2) /* Partner is PD capable */
3347
3348 #define PD_CTRL_RESP_ROLE_POWER         BIT(0) /* 0=SNK/1=SRC */
3349 #define PD_CTRL_RESP_ROLE_DATA          BIT(1) /* 0=UFP/1=DFP */
3350 #define PD_CTRL_RESP_ROLE_VCONN         BIT(2) /* Vconn status */
3351 #define PD_CTRL_RESP_ROLE_DR_POWER      BIT(3) /* Partner is dualrole power */
3352 #define PD_CTRL_RESP_ROLE_DR_DATA       BIT(4) /* Partner is dualrole data */
3353 #define PD_CTRL_RESP_ROLE_USB_COMM      BIT(5) /* Partner USB comm capable */
3354 #define PD_CTRL_RESP_ROLE_EXT_POWERED   BIT(6) /* Partner externally powerd */
3355
3356 struct ec_response_usb_pd_control_v1 {
3357         uint8_t enabled;
3358         uint8_t role;
3359         uint8_t polarity;
3360         char state[32];
3361 } __ec_align1;
3362
3363 #define EC_CMD_USB_PD_PORTS 0x102
3364
3365 /* Maximum number of PD ports on a device, num_ports will be <= this */
3366 #define EC_USB_PD_MAX_PORTS 8
3367
3368 struct ec_response_usb_pd_ports {
3369         uint8_t num_ports;
3370 } __ec_align1;
3371
3372 #define EC_CMD_USB_PD_POWER_INFO 0x103
3373
3374 #define PD_POWER_CHARGING_PORT 0xff
3375 struct ec_params_usb_pd_power_info {
3376         uint8_t port;
3377 } __ec_align1;
3378
3379 enum usb_chg_type {
3380         USB_CHG_TYPE_NONE,
3381         USB_CHG_TYPE_PD,
3382         USB_CHG_TYPE_C,
3383         USB_CHG_TYPE_PROPRIETARY,
3384         USB_CHG_TYPE_BC12_DCP,
3385         USB_CHG_TYPE_BC12_CDP,
3386         USB_CHG_TYPE_BC12_SDP,
3387         USB_CHG_TYPE_OTHER,
3388         USB_CHG_TYPE_VBUS,
3389         USB_CHG_TYPE_UNKNOWN,
3390 };
3391 enum usb_power_roles {
3392         USB_PD_PORT_POWER_DISCONNECTED,
3393         USB_PD_PORT_POWER_SOURCE,
3394         USB_PD_PORT_POWER_SINK,
3395         USB_PD_PORT_POWER_SINK_NOT_CHARGING,
3396 };
3397
3398 struct usb_chg_measures {
3399         uint16_t voltage_max;
3400         uint16_t voltage_now;
3401         uint16_t current_max;
3402         uint16_t current_lim;
3403 } __ec_align2;
3404
3405 struct ec_response_usb_pd_power_info {
3406         uint8_t role;
3407         uint8_t type;
3408         uint8_t dualrole;
3409         uint8_t reserved1;
3410         struct usb_chg_measures meas;
3411         uint32_t max_power;
3412 } __ec_align4;
3413
3414 struct ec_params_usb_pd_info_request {
3415         uint8_t port;
3416 } __ec_align1;
3417
3418 /*
3419  * This command will return the number of USB PD charge port + the number
3420  * of dedicated port present.
3421  * EC_CMD_USB_PD_PORTS does NOT include the dedicated ports
3422  */
3423 #define EC_CMD_CHARGE_PORT_COUNT 0x0105
3424 struct ec_response_charge_port_count {
3425         uint8_t port_count;
3426 } __ec_align1;
3427
3428 /* Read USB-PD Device discovery info */
3429 #define EC_CMD_USB_PD_DISCOVERY 0x0113
3430 struct ec_params_usb_pd_discovery_entry {
3431         uint16_t vid;  /* USB-IF VID */
3432         uint16_t pid;  /* USB-IF PID */
3433         uint8_t ptype; /* product type (hub,periph,cable,ama) */
3434 } __ec_align_size1;
3435
3436 /* Override default charge behavior */
3437 #define EC_CMD_PD_CHARGE_PORT_OVERRIDE 0x0114
3438
3439 /* Negative port parameters have special meaning */
3440 enum usb_pd_override_ports {
3441         OVERRIDE_DONT_CHARGE = -2,
3442         OVERRIDE_OFF = -1,
3443         /* [0, CONFIG_USB_PD_PORT_COUNT): Port# */
3444 };
3445
3446 struct ec_params_charge_port_override {
3447         int16_t override_port; /* Override port# */
3448 } __ec_align2;
3449
3450 /* Read (and delete) one entry of PD event log */
3451 #define EC_CMD_PD_GET_LOG_ENTRY 0x0115
3452
3453 struct ec_response_pd_log {
3454         uint32_t timestamp; /* relative timestamp in milliseconds */
3455         uint8_t type;       /* event type : see PD_EVENT_xx below */
3456         uint8_t size_port;  /* [7:5] port number [4:0] payload size in bytes */
3457         uint16_t data;      /* type-defined data payload */
3458         uint8_t payload[0]; /* optional additional data payload: 0..16 bytes */
3459 } __ec_align4;
3460
3461 /* The timestamp is the microsecond counter shifted to get about a ms. */
3462 #define PD_LOG_TIMESTAMP_SHIFT 10 /* 1 LSB = 1024us */
3463
3464 #define PD_LOG_SIZE_MASK  0x1f
3465 #define PD_LOG_PORT_MASK  0xe0
3466 #define PD_LOG_PORT_SHIFT    5
3467 #define PD_LOG_PORT_SIZE(port, size) (((port) << PD_LOG_PORT_SHIFT) | \
3468                                       ((size) & PD_LOG_SIZE_MASK))
3469 #define PD_LOG_PORT(size_port) ((size_port) >> PD_LOG_PORT_SHIFT)
3470 #define PD_LOG_SIZE(size_port) ((size_port) & PD_LOG_SIZE_MASK)
3471
3472 /* PD event log : entry types */
3473 /* PD MCU events */
3474 #define PD_EVENT_MCU_BASE       0x00
3475 #define PD_EVENT_MCU_CHARGE             (PD_EVENT_MCU_BASE+0)
3476 #define PD_EVENT_MCU_CONNECT            (PD_EVENT_MCU_BASE+1)
3477 /* Reserved for custom board event */
3478 #define PD_EVENT_MCU_BOARD_CUSTOM       (PD_EVENT_MCU_BASE+2)
3479 /* PD generic accessory events */
3480 #define PD_EVENT_ACC_BASE       0x20
3481 #define PD_EVENT_ACC_RW_FAIL   (PD_EVENT_ACC_BASE+0)
3482 #define PD_EVENT_ACC_RW_ERASE  (PD_EVENT_ACC_BASE+1)
3483 /* PD power supply events */
3484 #define PD_EVENT_PS_BASE        0x40
3485 #define PD_EVENT_PS_FAULT      (PD_EVENT_PS_BASE+0)
3486 /* PD video dongles events */
3487 #define PD_EVENT_VIDEO_BASE     0x60
3488 #define PD_EVENT_VIDEO_DP_MODE (PD_EVENT_VIDEO_BASE+0)
3489 #define PD_EVENT_VIDEO_CODEC   (PD_EVENT_VIDEO_BASE+1)
3490 /* Returned in the "type" field, when there is no entry available */
3491 #define PD_EVENT_NO_ENTRY       0xff
3492
3493 /*
3494  * PD_EVENT_MCU_CHARGE event definition :
3495  * the payload is "struct usb_chg_measures"
3496  * the data field contains the port state flags as defined below :
3497  */
3498 /* Port partner is a dual role device */
3499 #define CHARGE_FLAGS_DUAL_ROLE         BIT(15)
3500 /* Port is the pending override port */
3501 #define CHARGE_FLAGS_DELAYED_OVERRIDE  BIT(14)
3502 /* Port is the override port */
3503 #define CHARGE_FLAGS_OVERRIDE          BIT(13)
3504 /* Charger type */
3505 #define CHARGE_FLAGS_TYPE_SHIFT               3
3506 #define CHARGE_FLAGS_TYPE_MASK       (0xf << CHARGE_FLAGS_TYPE_SHIFT)
3507 /* Power delivery role */
3508 #define CHARGE_FLAGS_ROLE_MASK         (7 <<  0)
3509
3510 /*
3511  * PD_EVENT_PS_FAULT data field flags definition :
3512  */
3513 #define PS_FAULT_OCP                          1
3514 #define PS_FAULT_FAST_OCP                     2
3515 #define PS_FAULT_OVP                          3
3516 #define PS_FAULT_DISCH                        4
3517
3518 /*
3519  * PD_EVENT_VIDEO_CODEC payload is "struct mcdp_info".
3520  */
3521 struct mcdp_version {
3522         uint8_t major;
3523         uint8_t minor;
3524         uint16_t build;
3525 } __ec_align4;
3526
3527 struct mcdp_info {
3528         uint8_t family[2];
3529         uint8_t chipid[2];
3530         struct mcdp_version irom;
3531         struct mcdp_version fw;
3532 } __ec_align4;
3533
3534 /* struct mcdp_info field decoding */
3535 #define MCDP_CHIPID(chipid) ((chipid[0] << 8) | chipid[1])
3536 #define MCDP_FAMILY(family) ((family[0] << 8) | family[1])
3537
3538 /* Get info about USB-C SS muxes */
3539 #define EC_CMD_USB_PD_MUX_INFO 0x11a
3540
3541 struct ec_params_usb_pd_mux_info {
3542         uint8_t port; /* USB-C port number */
3543 } __ec_align1;
3544
3545 /* Flags representing mux state */
3546 #define USB_PD_MUX_USB_ENABLED       (1 << 0)
3547 #define USB_PD_MUX_DP_ENABLED        (1 << 1)
3548 #define USB_PD_MUX_POLARITY_INVERTED (1 << 2)
3549 #define USB_PD_MUX_HPD_IRQ           (1 << 3)
3550
3551 struct ec_response_usb_pd_mux_info {
3552         uint8_t flags; /* USB_PD_MUX_*-encoded USB mux state */
3553 } __ec_align1;
3554
3555 /*****************************************************************************/
3556 /*
3557  * Passthru commands
3558  *
3559  * Some platforms have sub-processors chained to each other.  For example.
3560  *
3561  *     AP <--> EC <--> PD MCU
3562  *
3563  * The top 2 bits of the command number are used to indicate which device the
3564  * command is intended for.  Device 0 is always the device receiving the
3565  * command; other device mapping is board-specific.
3566  *
3567  * When a device receives a command to be passed to a sub-processor, it passes
3568  * it on with the device number set back to 0.  This allows the sub-processor
3569  * to remain blissfully unaware of whether the command originated on the next
3570  * device up the chain, or was passed through from the AP.
3571  *
3572  * In the above example, if the AP wants to send command 0x0002 to the PD MCU,
3573  *     AP sends command 0x4002 to the EC
3574  *     EC sends command 0x0002 to the PD MCU
3575  *     EC forwards PD MCU response back to the AP
3576  */
3577
3578 /* Offset and max command number for sub-device n */
3579 #define EC_CMD_PASSTHRU_OFFSET(n) (0x4000 * (n))
3580 #define EC_CMD_PASSTHRU_MAX(n) (EC_CMD_PASSTHRU_OFFSET(n) + 0x3fff)
3581
3582 /*****************************************************************************/
3583 /*
3584  * Deprecated constants. These constants have been renamed for clarity. The
3585  * meaning and size has not changed. Programs that use the old names should
3586  * switch to the new names soon, as the old names may not be carried forward
3587  * forever.
3588  */
3589 #define EC_HOST_PARAM_SIZE      EC_PROTO2_MAX_PARAM_SIZE
3590 #define EC_LPC_ADDR_OLD_PARAM   EC_HOST_CMD_REGION1
3591 #define EC_OLD_PARAM_SIZE       EC_HOST_CMD_REGION_SIZE
3592
3593 #endif  /* __CROS_EC_COMMANDS_H */