xref: /openbmc/u-boot/drivers/misc/cros_ec_i2c.c (revision ebce73f0)
1 /*
2  * Chromium OS cros_ec driver - I2C interface
3  *
4  * Copyright (c) 2012 The Chromium OS Authors.
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 /*
10  * The Matrix Keyboard Protocol driver handles talking to the keyboard
11  * controller chip. Mostly this is for keyboard functions, but some other
12  * things have slipped in, so we provide generic services to talk to the
13  * KBC.
14  */
15 
16 #include <common.h>
17 #include <dm.h>
18 #include <i2c.h>
19 #include <cros_ec.h>
20 
21 #ifdef DEBUG_TRACE
22 #define debug_trace(fmt, b...)	debug(fmt, #b)
23 #else
24 #define debug_trace(fmt, b...)
25 #endif
26 
27 /**
28  * Request format for protocol v3
29  * byte 0	0xda (EC_COMMAND_PROTOCOL_3)
30  * byte 1-8	struct ec_host_request
31  * byte 10-	response data
32  */
33 struct ec_host_request_i2c {
34 	/* Always 0xda to backward compatible with v2 struct */
35 	uint8_t  command_protocol;
36 	struct ec_host_request ec_request;
37 } __packed;
38 
39 /*
40  * Response format for protocol v3
41  * byte 0	result code
42  * byte 1	packet_length
43  * byte 2-9	struct ec_host_response
44  * byte 10-	response data
45  */
46 struct ec_host_response_i2c {
47 	uint8_t result;
48 	uint8_t packet_length;
49 	struct ec_host_response ec_response;
50 } __packed;
51 
52 static int cros_ec_i2c_packet(struct udevice *udev, int out_bytes, int in_bytes)
53 {
54 	struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
55 	struct dm_i2c_chip *chip = dev_get_parent_platdata(udev);
56 	struct ec_host_request_i2c *ec_request_i2c =
57 		(struct ec_host_request_i2c *)dev->dout;
58 	struct ec_host_response_i2c *ec_response_i2c =
59 		(struct ec_host_response_i2c *)dev->din;
60 	struct i2c_msg i2c_msg[2];
61 	int ret;
62 
63 	i2c_msg[0].addr = chip->chip_addr;
64 	i2c_msg[0].flags = 0;
65 	i2c_msg[1].addr = chip->chip_addr;
66 	i2c_msg[1].flags = I2C_M_RD;
67 
68 	/* one extra byte, to indicate v3 */
69 	i2c_msg[0].len = out_bytes + 1;
70 	i2c_msg[0].buf = dev->dout;
71 
72 	/* stitch on EC_COMMAND_PROTOCOL_3 */
73 	memmove(&ec_request_i2c->ec_request, dev->dout, out_bytes);
74 	ec_request_i2c->command_protocol = EC_COMMAND_PROTOCOL_3;
75 
76 	/* two extra bytes for v3 */
77 	i2c_msg[1].len = in_bytes + 2;
78 	i2c_msg[1].buf = dev->din;
79 
80 	ret = dm_i2c_xfer(udev, &i2c_msg[0], 2);
81 	if (ret) {
82 		printf("%s: Could not execute transfer: %d\n", __func__, ret);
83 		return ret;
84 	}
85 
86 	/* When we send a v3 request to v2 ec, ec won't recognize the 0xda
87 	 * (EC_COMMAND_PROTOCOL_3) and will return with status
88 	 * EC_RES_INVALID_COMMAND with zero data length
89 	 *
90 	 * In case of invalid command for v3 protocol the data length
91 	 * will be at least sizeof(struct ec_host_response)
92 	 */
93 	if (ec_response_i2c->result == EC_RES_INVALID_COMMAND &&
94 	    ec_response_i2c->packet_length == 0)
95 		return -EPROTONOSUPPORT;
96 
97 	if (ec_response_i2c->packet_length < sizeof(struct ec_host_response)) {
98 		printf("%s: response of %u bytes too short; not a full hdr\n",
99 		       __func__, ec_response_i2c->packet_length);
100 		return -EBADMSG;
101 	}
102 
103 
104 	/* drop result and packet_len */
105 	memmove(dev->din, &ec_response_i2c->ec_response, in_bytes);
106 
107 	return in_bytes;
108 }
109 
110 static int cros_ec_i2c_command(struct udevice *udev, uint8_t cmd,
111 			       int cmd_version, const uint8_t *dout,
112 			       int dout_len, uint8_t **dinp, int din_len)
113 {
114 	struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
115 	struct dm_i2c_chip *chip = dev_get_parent_platdata(udev);
116 	struct i2c_msg i2c_msg[2];
117 	/* version8, cmd8, arglen8, out8[dout_len], csum8 */
118 	int out_bytes = dout_len + 4;
119 	/* response8, arglen8, in8[din_len], checksum8 */
120 	int in_bytes = din_len + 3;
121 	uint8_t *ptr;
122 	/* Receive input data, so that args will be dword aligned */
123 	uint8_t *in_ptr;
124 	int len, csum, ret;
125 
126 	/*
127 	 * Sanity-check I/O sizes given transaction overhead in internal
128 	 * buffers.
129 	 */
130 	if (out_bytes > sizeof(dev->dout)) {
131 		debug("%s: Cannot send %d bytes\n", __func__, dout_len);
132 		return -1;
133 	}
134 	if (in_bytes > sizeof(dev->din)) {
135 		debug("%s: Cannot receive %d bytes\n", __func__, din_len);
136 		return -1;
137 	}
138 	assert(dout_len >= 0);
139 	assert(dinp);
140 
141 	i2c_msg[0].addr = chip->chip_addr;
142 	i2c_msg[0].len = out_bytes;
143 	i2c_msg[0].buf = dev->dout;
144 	i2c_msg[0].flags = 0;
145 
146 	/*
147 	 * Copy command and data into output buffer so we can do a single I2C
148 	 * burst transaction.
149 	 */
150 	ptr = dev->dout;
151 
152 	/*
153 	 * in_ptr starts of pointing to a dword-aligned input data buffer.
154 	 * We decrement it back by the number of header bytes we expect to
155 	 * receive, so that the first parameter of the resulting input data
156 	 * will be dword aligned.
157 	 */
158 	in_ptr = dev->din + sizeof(int64_t);
159 
160 	if (dev->protocol_version != 2) {
161 		/* Something we don't support */
162 		debug("%s: Protocol version %d unsupported\n",
163 		      __func__, dev->protocol_version);
164 		return -1;
165 	}
166 
167 	*ptr++ = EC_CMD_VERSION0 + cmd_version;
168 	*ptr++ = cmd;
169 	*ptr++ = dout_len;
170 	in_ptr -= 2;	/* Expect status, length bytes */
171 
172 	memcpy(ptr, dout, dout_len);
173 	ptr += dout_len;
174 
175 	*ptr++ = (uint8_t)
176 		cros_ec_calc_checksum(dev->dout, dout_len + 3);
177 
178 	i2c_msg[1].addr = chip->chip_addr;
179 	i2c_msg[1].len = in_bytes;
180 	i2c_msg[1].buf = in_ptr;
181 	i2c_msg[1].flags = I2C_M_RD;
182 
183 	/* Send output data */
184 	cros_ec_dump_data("out", -1, dev->dout, out_bytes);
185 
186 	ret = dm_i2c_xfer(udev, &i2c_msg[0], 2);
187 	if (ret) {
188 		debug("%s: Could not execute transfer to %s\n", __func__,
189 		      udev->name);
190 		ret = -1;
191 	}
192 
193 	if (*in_ptr != EC_RES_SUCCESS) {
194 		debug("%s: Received bad result code %d\n", __func__, *in_ptr);
195 		return -(int)*in_ptr;
196 	}
197 
198 	len = in_ptr[1];
199 	if (len + 3 > sizeof(dev->din)) {
200 		debug("%s: Received length %#02x too large\n",
201 		      __func__, len);
202 		return -1;
203 	}
204 	csum = cros_ec_calc_checksum(in_ptr, 2 + len);
205 	if (csum != in_ptr[2 + len]) {
206 		debug("%s: Invalid checksum rx %#02x, calced %#02x\n",
207 		      __func__, in_ptr[2 + din_len], csum);
208 		return -1;
209 	}
210 	din_len = min(din_len, len);
211 	cros_ec_dump_data("in", -1, in_ptr, din_len + 3);
212 
213 	/* Return pointer to dword-aligned input data, if any */
214 	*dinp = dev->din + sizeof(int64_t);
215 
216 	return din_len;
217 }
218 
219 static int cros_ec_probe(struct udevice *dev)
220 {
221 	return cros_ec_register(dev);
222 }
223 
224 static struct dm_cros_ec_ops cros_ec_ops = {
225 	.command = cros_ec_i2c_command,
226 	.packet = cros_ec_i2c_packet,
227 };
228 
229 static const struct udevice_id cros_ec_ids[] = {
230 	{ .compatible = "google,cros-ec-i2c" },
231 	{ }
232 };
233 
234 U_BOOT_DRIVER(cros_ec_i2c) = {
235 	.name		= "cros_ec_i2c",
236 	.id		= UCLASS_CROS_EC,
237 	.of_match	= cros_ec_ids,
238 	.probe		= cros_ec_probe,
239 	.ops		= &cros_ec_ops,
240 };
241