xref: /aosp_15_r20/external/gsc-utils/docs/ap-ec-comm.md (revision 4f2df630800bdcf1d4f0decf95d8a1cb87344f5f)
1*4f2df630SAndroid Build Coastguard Worker# Application Processor to EC communication
2*4f2df630SAndroid Build Coastguard Worker
3*4f2df630SAndroid Build Coastguard Worker[TOC]
4*4f2df630SAndroid Build Coastguard Worker
5*4f2df630SAndroid Build Coastguard Worker## Overview
6*4f2df630SAndroid Build Coastguard Worker
7*4f2df630SAndroid Build Coastguard WorkerThe Application Processor (sometimes called the host) communicates with the EC
8*4f2df630SAndroid Build Coastguard Workerby issuing *host commands*, which are identified by a command ID and version
9*4f2df630SAndroid Build Coastguard Workernumber, and then reading a response. When a host command is issued through
10*4f2df630SAndroid Build Coastguard Worker`ectool`, two or three software components are involved:
11*4f2df630SAndroid Build Coastguard Worker
12*4f2df630SAndroid Build Coastguard Worker* `ectool`, the user-space binary,
13*4f2df630SAndroid Build Coastguard Worker* normally the `cros-ec` Kernel driver, and
14*4f2df630SAndroid Build Coastguard Worker* the code on the EC itself. This can be thought of as two parts:
15*4f2df630SAndroid Build Coastguard Worker  * a chip-specific driver for the appropriate transport, and
16*4f2df630SAndroid Build Coastguard Worker  * the generic host command handling code (mostly in the [host command task]).
17*4f2df630SAndroid Build Coastguard Worker
18*4f2df630SAndroid Build Coastguard WorkerWe'll go into detail of each of these, as well as the traffic on the wire, in
19*4f2df630SAndroid Build Coastguard Workerthe following sections.
20*4f2df630SAndroid Build Coastguard Worker
21*4f2df630SAndroid Build Coastguard Worker### `ectool`
22*4f2df630SAndroid Build Coastguard Worker
23*4f2df630SAndroid Build Coastguard Worker`ectool` contains wrapper functions for the host commands exposed by the EC,
24*4f2df630SAndroid Build Coastguard Workerproviding a CLI. They call one of the transport-specific `ec_command`
25*4f2df630SAndroid Build Coastguard Workerimplementations in the `util/comm-*.c` files to send and receive from the EC.
26*4f2df630SAndroid Build Coastguard Worker
27*4f2df630SAndroid Build Coastguard Worker### EC kernel driver
28*4f2df630SAndroid Build Coastguard Worker
29*4f2df630SAndroid Build Coastguard WorkerIn most cases, `ectool` communicates via the [`cros-ec` Kernel driver], rather
30*4f2df630SAndroid Build Coastguard Workerthan directly from userspace. It sends raw commands to the Kernel driver, which
31*4f2df630SAndroid Build Coastguard Workersends them on to the EC, bypassing a lot of the other Kernel driver
32*4f2df630SAndroid Build Coastguard Workerfunctionality.
33*4f2df630SAndroid Build Coastguard Worker
34*4f2df630SAndroid Build Coastguard WorkerThere are other CrOS EC-related Kernel drivers, which use host commands to act
35*4f2df630SAndroid Build Coastguard Workeras adapters to existing Linux APIs. For example, sensors from the EC are mapped
36*4f2df630SAndroid Build Coastguard Workerto the Linux [Industrial I/O] system.
37*4f2df630SAndroid Build Coastguard Worker
38*4f2df630SAndroid Build Coastguard Worker### On the wire
39*4f2df630SAndroid Build Coastguard Worker
40*4f2df630SAndroid Build Coastguard WorkerNow we come to the protocol itself. All transactions take this general form:
41*4f2df630SAndroid Build Coastguard Worker
42*4f2df630SAndroid Build Coastguard Worker* Host writes the request packet, consisting of:
43*4f2df630SAndroid Build Coastguard Worker  * a transport-specific header;
44*4f2df630SAndroid Build Coastguard Worker  * a `struct ec_host_request` containing the command ID, data length, and a
45*4f2df630SAndroid Build Coastguard Worker    checksum; and
46*4f2df630SAndroid Build Coastguard Worker  * zero or more bytes of parameters for the command, the format of which
47*4f2df630SAndroid Build Coastguard Worker    depends on the command.
48*4f2df630SAndroid Build Coastguard Worker* Host reads the response to its request, consisting of:
49*4f2df630SAndroid Build Coastguard Worker  * a transport-specific header;
50*4f2df630SAndroid Build Coastguard Worker  * a `struct ec_host_response` containing the result code, data length, and a
51*4f2df630SAndroid Build Coastguard Worker    checksum; and
52*4f2df630SAndroid Build Coastguard Worker  * zero or more bytes of response from the command, again with a
53*4f2df630SAndroid Build Coastguard Worker    command-specific format.
54*4f2df630SAndroid Build Coastguard Worker
55*4f2df630SAndroid Build Coastguard Worker### On the EC
56*4f2df630SAndroid Build Coastguard Worker
57*4f2df630SAndroid Build Coastguard WorkerThe host packet is received on the EC by some chip-specific code which checks
58*4f2df630SAndroid Build Coastguard Workerits transport-specific header, then passes it on to the common host command code,
59*4f2df630SAndroid Build Coastguard Workerstarting at `host_packet_receive`. The common code validates the packet and
60*4f2df630SAndroid Build Coastguard Workerthen sends it on to the handler function (annotated with the
61*4f2df630SAndroid Build Coastguard Worker`DECLARE_HOST_COMMAND` macro), which runs in the `HOSTCMD` task. The handler can
62*4f2df630SAndroid Build Coastguard Workerset a response by modifying its arguments struct, which is sent back to the host
63*4f2df630SAndroid Build Coastguard Workervia the chip-specific code.
64*4f2df630SAndroid Build Coastguard Worker
65*4f2df630SAndroid Build Coastguard WorkerWhile this is happening, the EC needs to indicate to the host that it is busy
66*4f2df630SAndroid Build Coastguard Workerprocessing and not yet ready to give a response. How it does this depends on the
67*4f2df630SAndroid Build Coastguard Workertransport method used (see [Transport-specific details] below).
68*4f2df630SAndroid Build Coastguard Worker
69*4f2df630SAndroid Build Coastguard Worker## Versions
70*4f2df630SAndroid Build Coastguard Worker
71*4f2df630SAndroid Build Coastguard WorkerThere are two different concepts of "version" involved in host commands: version
72*4f2df630SAndroid Build Coastguard Workerof the overarching protocol, and versions of individual commands.
73*4f2df630SAndroid Build Coastguard Worker
74*4f2df630SAndroid Build Coastguard Worker### Protocol versions
75*4f2df630SAndroid Build Coastguard Worker
76*4f2df630SAndroid Build Coastguard WorkerThere have been three protocol versions so far, and this document describes
77*4f2df630SAndroid Build Coastguard Workerversion 3. Version 1 was superseded by 2 before it shipped, so no devices use
78*4f2df630SAndroid Build Coastguard Workerit anymore. Version 2 is generally deprecated, but you might still encounter it
79*4f2df630SAndroid Build Coastguard Workeroccasionally.
80*4f2df630SAndroid Build Coastguard Worker
81*4f2df630SAndroid Build Coastguard WorkerWhich version is in use can be determined using the `EC_CMD_GET_PROTOCOL_INFO`
82*4f2df630SAndroid Build Coastguard Workercommand. This was only introduced in version 3, however, so if errors,
83*4f2df630SAndroid Build Coastguard Worker`EC_CMD_HELLO` should be sent in version 2. If the hello command succeeds, the
84*4f2df630SAndroid Build Coastguard WorkerEC speaks version 2.
85*4f2df630SAndroid Build Coastguard Worker
86*4f2df630SAndroid Build Coastguard Worker### Command versions
87*4f2df630SAndroid Build Coastguard Worker
88*4f2df630SAndroid Build Coastguard WorkerIndividual commands also have versions, independent of the protocol version
89*4f2df630SAndroid Build Coastguard Workerthey're being called with. Different versions of a command may have different
90*4f2df630SAndroid Build Coastguard Workerparameter or response formats. `EC_CMD_GET_CMD_VERSIONS` returns the versions of
91*4f2df630SAndroid Build Coastguard Workerthe given command supported by the EC. These version numbers start at 0.
92*4f2df630SAndroid Build Coastguard Worker
93*4f2df630SAndroid Build Coastguard Worker## Transport-specific details
94*4f2df630SAndroid Build Coastguard Worker
95*4f2df630SAndroid Build Coastguard WorkerAlthough the command and response formats are the same across all transports,
96*4f2df630SAndroid Build Coastguard Workersome details of how they are transmitted differ, which may be of interest when
97*4f2df630SAndroid Build Coastguard Workerimplementing the EC side of the protocol on a new chip.
98*4f2df630SAndroid Build Coastguard Worker
99*4f2df630SAndroid Build Coastguard Worker### I<sup>2</sup>C
100*4f2df630SAndroid Build Coastguard Worker
101*4f2df630SAndroid Build Coastguard WorkerI<sup>2</sup>C is very flexible with its timing, so when the EC receives a
102*4f2df630SAndroid Build Coastguard Workerpacket from the host, it should stretch the clock, holding it low until it is
103*4f2df630SAndroid Build Coastguard Workerready for the host to read the response.
104*4f2df630SAndroid Build Coastguard Worker
105*4f2df630SAndroid Build Coastguard WorkerIf the host tries to read more bytes than were in the response, the EC should
106*4f2df630SAndroid Build Coastguard Workerrespond with an obvious filler byte (such as 0xEC). For example, if a command
107*4f2df630SAndroid Build Coastguard Workerthat normally returns 50 bytes errors, its response will only be 8 bytes (the
108*4f2df630SAndroid Build Coastguard Workersize of the response struct). The host will probably try to read 50 bytes
109*4f2df630SAndroid Build Coastguard Workeranyway, so the EC should send the 8 bytes of the struct followed by 42 copies of
110*4f2df630SAndroid Build Coastguard Workerthe filler byte.
111*4f2df630SAndroid Build Coastguard Worker
112*4f2df630SAndroid Build Coastguard Worker### SPI
113*4f2df630SAndroid Build Coastguard Worker
114*4f2df630SAndroid Build Coastguard WorkerThe SPI bus is similar to I<sup>2</sup>C, but with two major exceptions. First,
115*4f2df630SAndroid Build Coastguard Workerthere's a minimum speed on the SPI bus. If peripheral devices don't respond
116*4f2df630SAndroid Build Coastguard Workerquickly enough, the controller will assume they're broken and give up. Second,
117*4f2df630SAndroid Build Coastguard Workerevery transaction is bidirectional. When bits are being clocked from controller
118*4f2df630SAndroid Build Coastguard Workerto peripheral on the MOSI line, the controller will simultaneously read bits in
119*4f2df630SAndroid Build Coastguard Workerthe other direction on the MISO line.
120*4f2df630SAndroid Build Coastguard Worker
121*4f2df630SAndroid Build Coastguard WorkerHardware devices can usually handle this, and often some hardware-based flow
122*4f2df630SAndroid Build Coastguard Workercontrol used to "stretch" the transaction by a bit or byte if the peripheral
123*4f2df630SAndroid Build Coastguard Workerdevice needs a little extra time to respond to the controller's demands.
124*4f2df630SAndroid Build Coastguard Worker
125*4f2df630SAndroid Build Coastguard WorkerWhen exchanging messages with the EC on the SPI bus, the EC's host commands are
126*4f2df630SAndroid Build Coastguard Workercommunicated using our own software flow-control scheme, because most of the
127*4f2df630SAndroid Build Coastguard Workerembedded controllers either aren't fast enough or don't have any support for
128*4f2df630SAndroid Build Coastguard Workerhardware flow-control.
129*4f2df630SAndroid Build Coastguard Worker
130*4f2df630SAndroid Build Coastguard WorkerIt works like this: When the AP sends a byte to the EC, if the EC doesn't have a
131*4f2df630SAndroid Build Coastguard Workerresponse queued up in advance, a default "placeholder" byte is returned. The EC
132*4f2df630SAndroid Build Coastguard Workerpreconfigures that default response byte to indicate its status (ready, busy,
133*4f2df630SAndroid Build Coastguard Workerwaiting for more input, etc.). Once the AP has sent a complete command message,
134*4f2df630SAndroid Build Coastguard Workerit continues clocking bytes to the EC (which the EC ignores) and just looks at
135*4f2df630SAndroid Build Coastguard Workerthe response byte that comes back. Once the EC has parsed the AP's command and
136*4f2df630SAndroid Build Coastguard Workeris ready to reply, it sends a "start of frame" byte, followed by the actual
137*4f2df630SAndroid Build Coastguard Workerresponse. The AP continues to read and ignore bytes from the EC until it sees
138*4f2df630SAndroid Build Coastguard Workerthe start of frame byte, and then it knows that the EC's response is starting
139*4f2df630SAndroid Build Coastguard Workerwith the next byte.
140*4f2df630SAndroid Build Coastguard Worker
141*4f2df630SAndroid Build Coastguard WorkerOnce the response packet has been read, any additional reads should return
142*4f2df630SAndroid Build Coastguard Worker`EC_SPI_PAST_END`.
143*4f2df630SAndroid Build Coastguard Worker
144*4f2df630SAndroid Build Coastguard Worker### LPC or eSPI
145*4f2df630SAndroid Build Coastguard Worker
146*4f2df630SAndroid Build Coastguard WorkerThe EC should set `EC_LPC_STATUS_PROCESSING` in its command status register
147*4f2df630SAndroid Build Coastguard Workerafter receiving a host packet and before it has a response ready.
148*4f2df630SAndroid Build Coastguard Worker
149*4f2df630SAndroid Build Coastguard Worker
150*4f2df630SAndroid Build Coastguard Worker[`cros-ec` Kernel driver]: https://chromium.googlesource.com/chromiumos/third_party/kernel/+/refs/heads/chromeos-4.19/drivers/mfd/cros_ec_dev.c
151*4f2df630SAndroid Build Coastguard Worker[Industrial I/O]: https://www.kernel.org/doc/html/v4.14/driver-api/iio/index.html
152*4f2df630SAndroid Build Coastguard Worker[host command task]: https://chromium.googlesource.com/chromiumos/platform/ec/+/refs/heads/main/common/host_command.c
153*4f2df630SAndroid Build Coastguard Worker[Transport-specific details]: #Transport_specific-details
154