1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <device/i2c_simple.h>
9 #include "ec_commands.h"
11 #if CONFIG(EC_GOOGLE_CHROMEEC_I2C_PROTO3)
13 #define PROTO3_FRAMING_BYTES sizeof(uint32_t)
14 /* Just use the LPC host packet size to size the buffer. */
15 #define PROTO3_MAX_PACKET_SIZE 268
17 struct proto3_i2c_buf
{
18 uint8_t framing_bytes
[PROTO3_FRAMING_BYTES
];
19 uint8_t data
[PROTO3_MAX_PACKET_SIZE
];
20 } __attribute__((aligned(sizeof(uint32_t))));
22 static struct proto3_i2c_buf req_buf
;
23 static struct proto3_i2c_buf resp_buf
;
33 struct i2c_msg segs
[SEGS_PER_CMD
];
36 static struct i2c_ec ec_dev
= {
37 .bus
= CONFIG_EC_GOOGLE_CHROMEEC_I2C_BUS
,
40 .slave
= CONFIG_EC_GOOGLE_CHROMEEC_I2C_CHIP
,
41 /* Framing byte to be transferred prior to request. */
42 .buf
= &req_buf
.framing_bytes
[3],
46 .slave
= CONFIG_EC_GOOGLE_CHROMEEC_I2C_CHIP
,
47 /* return code and total length before full response. */
48 .buf
= &resp_buf
.framing_bytes
[2],
52 void *crosec_get_buffer(size_t size
, int req
)
54 struct proto3_i2c_buf
*ib
;
56 if (size
> PROTO3_MAX_PACKET_SIZE
) {
57 printk(BIOS_DEBUG
, "Proto v3 buffer request too large: %zu!\n",
70 static int crosec_i2c_io(size_t req_size
, size_t resp_size
, void *context
)
72 struct i2c_ec
*ec
= context
;
76 if (req_size
> PROTO3_MAX_PACKET_SIZE
||
77 resp_size
> PROTO3_MAX_PACKET_SIZE
)
80 /* Place the framing byte and set size accordingly. */
81 ec
->segs
[CMD_INDEX
].len
= req_size
+ 1;
82 ec
->segs
[CMD_INDEX
].buf
[0] = EC_COMMAND_PROTOCOL_3
;
83 /* Return code and length returned prior to packet data. */
84 ec
->segs
[RESP_INDEX
].len
= resp_size
+ 2;
86 if (i2c_transfer(ec
->bus
, ec
->segs
, ARRAY_SIZE(ec
->segs
)) != 0) {
87 printk(BIOS_ERR
, "%s: Cannot complete read from i2c-%d:%#x\n",
88 __func__
, ec
->bus
, ec
->segs
[0].slave
);
92 ret_code
= ec
->segs
[RESP_INDEX
].buf
[0];
93 resp_len
= ec
->segs
[RESP_INDEX
].buf
[1];
96 printk(BIOS_ERR
, "EC command returned 0x%x\n", ret_code
);
100 if (resp_len
> resp_size
) {
101 printk(BIOS_ERR
, "Response length mismatch %zu vs %zu\n",
102 resp_len
, resp_size
);
109 int google_chromeec_command(struct chromeec_command
*cec_command
)
111 return crosec_command_proto(cec_command
, crosec_i2c_io
, &ec_dev
);
114 #else /* CONFIG_EC_GOOGLE_CHROMEEC_I2C_PROTO3 */
116 /* Command (host->device) format for I2C:
117 * uint8_t version, cmd, len, data[len], checksum;
119 * Response (device->host) format for I2C:
120 * uint8_t response, len, data[len], checksum;
122 * Note the location of checksum is different from LPC protocol.
124 * The length is 8 bit so maximum data size is 0xff.
125 * Any I2C command should fit in 0xff + 4 bytes, and max response length
128 #define MAX_I2C_DATA_SIZE (0xff)
134 uint8_t data
[MAX_I2C_DATA_SIZE
+ 1];
140 uint8_t data
[MAX_I2C_DATA_SIZE
+ 1];
143 static inline void i2c_dump(int bus
, int chip
, const uint8_t *data
, size_t size
)
145 #ifdef TRACE_CHROMEEC
146 printk(BIOS_INFO
, "i2c: bus=%d, chip=%#x, size=%d, data: ", bus
, chip
,
149 printk(BIOS_INFO
, "%02X ", *data
++);
151 printk(BIOS_INFO
, "\n");
155 static int ec_verify_checksum(const EcResponseI2c
*resp
)
157 size_t size
= sizeof(*resp
) - sizeof(resp
->data
) + resp
->length
;
158 uint8_t calculated
= google_chromeec_calc_checksum(
159 (const uint8_t *)resp
, size
);
160 uint8_t received
= resp
->data
[resp
->length
];
161 if (calculated
!= received
) {
162 printk(BIOS_ERR
, "%s: Unmatch (rx: %#02x, calc: %#02x)\n",
163 __func__
, received
, calculated
);
169 static void ec_fill_checksum(EcCommandI2c
*cmd
)
171 size_t size
= sizeof(*cmd
) - sizeof(cmd
->data
) + cmd
->length
;
172 cmd
->data
[cmd
->length
] = google_chromeec_calc_checksum(
173 (const uint8_t *)cmd
, size
);
176 int google_chromeec_command(struct chromeec_command
*cec_command
)
180 int bus
= CONFIG_EC_GOOGLE_CHROMEEC_I2C_BUS
;
181 int chip
= CONFIG_EC_GOOGLE_CHROMEEC_I2C_CHIP
;
182 size_t size_i2c_cmd
= (sizeof(cmd
) - sizeof(cmd
.data
) +
183 cec_command
->cmd_size_in
+ 1),
184 size_i2c_resp
= (sizeof(resp
) - sizeof(resp
.data
) +
185 cec_command
->cmd_size_out
+ 1);
187 if (cec_command
->cmd_size_in
> MAX_I2C_DATA_SIZE
||
188 cec_command
->cmd_size_out
> MAX_I2C_DATA_SIZE
) {
189 printk(BIOS_ERR
, "%s: Command data size too large (%d,%d)\n",
190 __func__
, cec_command
->cmd_size_in
,
191 cec_command
->cmd_size_out
);
192 cec_command
->cmd_code
= EC_RES_INVALID_PARAM
;
196 /* Construct command. */
197 cmd
.version
= EC_CMD_VERSION0
+ cec_command
->cmd_version
;
198 cmd
.command
= cec_command
->cmd_code
;
199 cmd
.length
= cec_command
->cmd_size_in
;
200 memcpy(cmd
.data
, cec_command
->cmd_data_in
, cmd
.length
);
201 ec_fill_checksum(&cmd
);
203 /* Start I2C communication */
204 i2c_dump(bus
, chip
, (const uint8_t *)&cmd
, size_i2c_cmd
);
205 if (i2c_write_raw(bus
, chip
, (uint8_t *)&cmd
, size_i2c_cmd
) != 0) {
206 printk(BIOS_ERR
, "%s: Cannot complete write to i2c-%d:%#x\n",
207 __func__
, bus
, chip
);
208 cec_command
->cmd_code
= EC_RES_ERROR
;
211 if (i2c_read_raw(bus
, chip
, (uint8_t *)&resp
, size_i2c_resp
) != 0) {
212 printk(BIOS_ERR
, "%s: Cannot complete read from i2c-%d:%#x\n",
213 __func__
, bus
, chip
);
214 cec_command
->cmd_code
= EC_RES_ERROR
;
218 /* Verify and return response */
219 cec_command
->cmd_code
= resp
.response
;
220 if (resp
.response
!= EC_RES_SUCCESS
) {
221 printk(BIOS_DEBUG
, "%s: Received bad result code %d\n",
222 __func__
, (int)resp
.response
);
225 if (resp
.length
> cec_command
->cmd_size_out
) {
226 printk(BIOS_ERR
, "%s: Received len %#02x too large\n",
227 __func__
, (int)resp
.length
);
228 cec_command
->cmd_code
= EC_RES_INVALID_RESPONSE
;
231 if (!ec_verify_checksum(&resp
)) {
232 cec_command
->cmd_code
= EC_RES_INVALID_CHECKSUM
;
235 cec_command
->cmd_size_out
= resp
.length
;
236 memcpy(cec_command
->cmd_data_out
, resp
.data
, resp
.length
);
240 #endif /* CONFIG_EC_GOOGLE_CHROMEEC_I2C_PROTO3 */
242 enum host_event_code
google_chromeec_get_event(void)
244 printk(BIOS_ERR
, "%s: Not supported.\n", __func__
);
245 return EC_HOST_EVENT_NONE
;