• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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];
19 	uint8_t data[PROTO3_MAX_PACKET_SIZE];
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 
crosec_get_buffer(size_t size,int req)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 
crosec_i2c_io(size_t req_size,size_t resp_size,void * context)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 
google_chromeec_command(struct chromeec_command * cec_command)109 int google_chromeec_command(struct chromeec_command *cec_command)
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 {
131 	uint8_t version;
132 	uint8_t command;
133 	uint8_t length;
134 	uint8_t data[MAX_I2C_DATA_SIZE + 1];
135 } EcCommandI2c;
136 
137 typedef struct {
138 	uint8_t response;
139 	uint8_t length;
140 	uint8_t data[MAX_I2C_DATA_SIZE + 1];
141 } EcResponseI2c;
142 
i2c_dump(int bus,int chip,const uint8_t * data,size_t size)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 
ec_verify_checksum(const EcResponseI2c * resp)155 static int ec_verify_checksum(const EcResponseI2c *resp)
156 {
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);
164 		return 0;
165 	}
166 	return 1;
167 }
168 
ec_fill_checksum(EcCommandI2c * cmd)169 static void ec_fill_checksum(EcCommandI2c *cmd)
170 {
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);
174 }
175 
google_chromeec_command(struct chromeec_command * cec_command)176 int google_chromeec_command(struct chromeec_command *cec_command)
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);
228 		cec_command->cmd_code = EC_RES_INVALID_RESPONSE;
229 		return 1;
230 	}
231 	if (!ec_verify_checksum(&resp)) {
232 		cec_command->cmd_code = EC_RES_INVALID_CHECKSUM;
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 
google_chromeec_get_event(void)242 enum host_event_code google_chromeec_get_event(void)
243 {
244 	printk(BIOS_ERR, "%s: Not supported.\n", __func__);
245 	return EC_HOST_EVENT_NONE;
246 }
247