coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
ec_i2c.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
4 #include <device/i2c_simple.h>
5 #include <stdint.h>
6 #include <string.h>
7 
8 #include "ec.h"
9 #include "ec_commands.h"
10 
11 #if CONFIG(EC_GOOGLE_CHROMEEC_I2C_PROTO3)
12 
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
16 
17 struct proto3_i2c_buf {
18  uint8_t framing_bytes[PROTO3_FRAMING_BYTES];
20 } __attribute__((aligned(sizeof(uint32_t))));
21 
22 static struct proto3_i2c_buf req_buf;
23 static struct proto3_i2c_buf resp_buf;
24 
25 enum {
26  CMD_INDEX,
27  RESP_INDEX,
28  SEGS_PER_CMD,
29 };
30 
31 struct i2c_ec {
32  int bus;
33  struct i2c_msg segs[SEGS_PER_CMD];
34 };
35 
36 static struct i2c_ec ec_dev = {
37  .bus = CONFIG_EC_GOOGLE_CHROMEEC_I2C_BUS,
38  .segs[CMD_INDEX] = {
39  .flags = 0,
40  .slave = CONFIG_EC_GOOGLE_CHROMEEC_I2C_CHIP,
41  /* Framing byte to be transferred prior to request. */
42  .buf = &req_buf.framing_bytes[3],
43  },
44  .segs[RESP_INDEX] = {
45  .flags = I2C_M_RD,
46  .slave = CONFIG_EC_GOOGLE_CHROMEEC_I2C_CHIP,
47  /* return code and total length before full response. */
48  .buf = &resp_buf.framing_bytes[2],
49  },
50 };
51 
52 void *crosec_get_buffer(size_t size, int req)
53 {
54  struct proto3_i2c_buf *ib;
55 
56  if (size > PROTO3_MAX_PACKET_SIZE) {
57  printk(BIOS_DEBUG, "Proto v3 buffer request too large: %zu!\n",
58  size);
59  return NULL;
60  }
61 
62  if (req)
63  ib = &req_buf;
64  else
65  ib = &resp_buf;
66 
67  return &ib->data[0];
68 }
69 
70 static int crosec_i2c_io(size_t req_size, size_t resp_size, void *context)
71 {
72  struct i2c_ec *ec = context;
73  uint8_t ret_code;
74  size_t resp_len;
75 
76  if (req_size > PROTO3_MAX_PACKET_SIZE ||
77  resp_size > PROTO3_MAX_PACKET_SIZE)
78  return -1;
79 
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;
85 
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);
89  return -1;
90  }
91 
92  ret_code = ec->segs[RESP_INDEX].buf[0];
93  resp_len = ec->segs[RESP_INDEX].buf[1];
94 
95  if (ret_code != 0) {
96  printk(BIOS_ERR, "EC command returned 0x%x\n", ret_code);
97  return -1;
98  }
99 
100  if (resp_len > resp_size) {
101  printk(BIOS_ERR, "Response length mismatch %zu vs %zu\n",
102  resp_len, resp_size);
103  return -1;
104  }
105 
106  return 0;
107 }
108 
110 {
111  return crosec_command_proto(cec_command, crosec_i2c_io, &ec_dev);
112 }
113 
114 #else /* CONFIG_EC_GOOGLE_CHROMEEC_I2C_PROTO3 */
115 
116 /* Command (host->device) format for I2C:
117  * uint8_t version, cmd, len, data[len], checksum;
118  *
119  * Response (device->host) format for I2C:
120  * uint8_t response, len, data[len], checksum;
121  *
122  * Note the location of checksum is different from LPC protocol.
123  *
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
126  * is 0xff + 3 bytes.
127  */
128 #define MAX_I2C_DATA_SIZE (0xff)
129 
130 typedef struct {
135 } EcCommandI2c;
136 
137 typedef struct {
141 } EcResponseI2c;
142 
143 static inline void i2c_dump(int bus, int chip, const uint8_t *data, size_t size)
144 {
145 #ifdef TRACE_CHROMEEC
146  printk(BIOS_INFO, "i2c: bus=%d, chip=%#x, size=%d, data: ", bus, chip,
147  size);
148  while (size-- > 0) {
149  printk(BIOS_INFO, "%02X ", *data++);
150  }
151  printk(BIOS_INFO, "\n");
152 #endif
153 }
154 
155 static int ec_verify_checksum(const EcResponseI2c *resp)
156 {
157  size_t size = sizeof(*resp) - sizeof(resp->data) + resp->length;
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);
164  return 0;
165  }
166  return 1;
167 }
168 
170 {
171  size_t size = sizeof(*cmd) - sizeof(cmd->data) + cmd->length;
173  (const uint8_t *)cmd, size);
174 }
175 
177 {
178  EcCommandI2c cmd;
179  EcResponseI2c resp;
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);
186 
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;
193  return 1;
194  }
195 
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);
202 
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;
209  return 1;
210  }
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;
215  return 1;
216  }
217 
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);
223  return 1;
224  }
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);
229  return 1;
230  }
231  if (!ec_verify_checksum(&resp)) {
233  return 1;
234  }
235  cec_command->cmd_size_out = resp.length;
236  memcpy(cec_command->cmd_data_out, resp.data, resp.length);
237  return 0;
238 }
239 
240 #endif /* CONFIG_EC_GOOGLE_CHROMEEC_I2C_PROTO3 */
241 
243 {
244  printk(BIOS_ERR, "%s: Not supported.\n", __func__);
245  return EC_HOST_EVENT_NONE;
246 }
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define printk(level,...)
Definition: stdlib.h:16
int crosec_command_proto(struct chromeec_command *cec_command, crosec_io_t crosec_io, void *context)
Definition: crosec_proto.c:240
void *__weak crosec_get_buffer(size_t size, int req)
Definition: crosec_proto.c:12
uint8_t google_chromeec_calc_checksum(const uint8_t *data, int size)
Definition: ec.c:66
#define EC_CMD_VERSION0
Definition: ec_commands.h:5435
host_event_code
Definition: ec_commands.h:653
@ EC_HOST_EVENT_NONE
Definition: ec_commands.h:654
cec_command
Definition: ec_commands.h:5071
@ EC_RES_INVALID_CHECKSUM
Definition: ec_commands.h:628
@ EC_RES_INVALID_PARAM
Definition: ec_commands.h:624
@ EC_RES_ERROR
Definition: ec_commands.h:623
@ EC_RES_SUCCESS
Definition: ec_commands.h:621
@ EC_RES_INVALID_RESPONSE
Definition: ec_commands.h:626
#define EC_COMMAND_PROTOCOL_3
Definition: ec_commands.h:901
int google_chromeec_command(struct chromeec_command *cec_command)
Send a command to a CrOS EC.
Definition: ec_i2c.c:176
static void ec_fill_checksum(EcCommandI2c *cmd)
Definition: ec_i2c.c:169
static int ec_verify_checksum(const EcResponseI2c *resp)
Definition: ec_i2c.c:155
enum host_event_code google_chromeec_get_event(void)
Definition: ec_i2c.c:242
static void i2c_dump(int bus, int chip, const uint8_t *data, size_t size)
Definition: ec_i2c.c:143
#define MAX_I2C_DATA_SIZE
Definition: ec_i2c.c:128
static uint8_t req_buf[PROTO3_MAX_PACKET_SIZE]
Definition: ec_spi.c:17
static uint8_t resp_buf[PROTO3_MAX_PACKET_SIZE]
Definition: ec_spi.c:18
#define PROTO3_MAX_PACKET_SIZE
Definition: ec_spi.c:15
static struct tpm_chip chip
Definition: tis.c:17
static int i2c_transfer(unsigned int bus, struct i2c_msg *segments, int count)
Definition: i2c_simple.h:42
static int i2c_write_raw(unsigned int bus, uint8_t slave, uint8_t *data, int len)
Definition: i2c_simple.h:72
static int i2c_read_raw(unsigned int bus, uint8_t slave, uint8_t *data, int len)
Definition: i2c_simple.h:57
#define I2C_M_RD
Definition: i2c.h:34
#define BIOS_INFO
BIOS_INFO - Expected events.
Definition: loglevel.h:113
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
#define CMD_INDEX(x)
Definition: mmc.c:19
#define NULL
Definition: stddef.h:19
unsigned int uint32_t
Definition: stdint.h:14
unsigned char uint8_t
Definition: stdint.h:8
uint8_t command
Definition: ec_i2c.c:132
uint8_t version
Definition: ec_i2c.c:131
uint8_t length
Definition: ec_i2c.c:133
uint8_t data[MAX_I2C_DATA_SIZE+1]
Definition: ec_i2c.c:134
uint8_t data[MAX_I2C_DATA_SIZE+1]
Definition: ec_i2c.c:140
uint8_t response
Definition: ec_i2c.c:138
uint8_t length
Definition: ec_i2c.c:139
Definition: device.h:76
struct i2c_msg - an I2C transaction segment beginning with START @addr: Slave address,...
Definition: i2c.h:32
uint16_t flags
Definition: i2c.h:33