xref: /openbmc/u-boot/drivers/misc/cros_ec_i2c.c (revision e8f80a5a)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
278764a4eSHung-ying Tyan /*
378764a4eSHung-ying Tyan  * Chromium OS cros_ec driver - I2C interface
478764a4eSHung-ying Tyan  *
578764a4eSHung-ying Tyan  * Copyright (c) 2012 The Chromium OS Authors.
678764a4eSHung-ying Tyan  */
778764a4eSHung-ying Tyan 
878764a4eSHung-ying Tyan /*
978764a4eSHung-ying Tyan  * The Matrix Keyboard Protocol driver handles talking to the keyboard
1078764a4eSHung-ying Tyan  * controller chip. Mostly this is for keyboard functions, but some other
1178764a4eSHung-ying Tyan  * things have slipped in, so we provide generic services to talk to the
1278764a4eSHung-ying Tyan  * KBC.
1378764a4eSHung-ying Tyan  */
1478764a4eSHung-ying Tyan 
1578764a4eSHung-ying Tyan #include <common.h>
1685df958cSSimon Glass #include <dm.h>
1778764a4eSHung-ying Tyan #include <i2c.h>
1878764a4eSHung-ying Tyan #include <cros_ec.h>
1978764a4eSHung-ying Tyan 
2078764a4eSHung-ying Tyan #ifdef DEBUG_TRACE
2178764a4eSHung-ying Tyan #define debug_trace(fmt, b...)	debug(fmt, #b)
2278764a4eSHung-ying Tyan #else
2378764a4eSHung-ying Tyan #define debug_trace(fmt, b...)
2478764a4eSHung-ying Tyan #endif
2578764a4eSHung-ying Tyan 
26147f785fSMoritz Fischer /**
27147f785fSMoritz Fischer  * Request format for protocol v3
28147f785fSMoritz Fischer  * byte 0	0xda (EC_COMMAND_PROTOCOL_3)
29147f785fSMoritz Fischer  * byte 1-8	struct ec_host_request
30147f785fSMoritz Fischer  * byte 10-	response data
31147f785fSMoritz Fischer  */
32147f785fSMoritz Fischer struct ec_host_request_i2c {
33147f785fSMoritz Fischer 	/* Always 0xda to backward compatible with v2 struct */
34147f785fSMoritz Fischer 	uint8_t  command_protocol;
35147f785fSMoritz Fischer 	struct ec_host_request ec_request;
36147f785fSMoritz Fischer } __packed;
37147f785fSMoritz Fischer 
38147f785fSMoritz Fischer /*
39147f785fSMoritz Fischer  * Response format for protocol v3
40147f785fSMoritz Fischer  * byte 0	result code
41147f785fSMoritz Fischer  * byte 1	packet_length
42147f785fSMoritz Fischer  * byte 2-9	struct ec_host_response
43147f785fSMoritz Fischer  * byte 10-	response data
44147f785fSMoritz Fischer  */
45147f785fSMoritz Fischer struct ec_host_response_i2c {
46147f785fSMoritz Fischer 	uint8_t result;
47147f785fSMoritz Fischer 	uint8_t packet_length;
48147f785fSMoritz Fischer 	struct ec_host_response ec_response;
49147f785fSMoritz Fischer } __packed;
50147f785fSMoritz Fischer 
cros_ec_i2c_packet(struct udevice * udev,int out_bytes,int in_bytes)51147f785fSMoritz Fischer static int cros_ec_i2c_packet(struct udevice *udev, int out_bytes, int in_bytes)
52147f785fSMoritz Fischer {
53147f785fSMoritz Fischer 	struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
54147f785fSMoritz Fischer 	struct dm_i2c_chip *chip = dev_get_parent_platdata(udev);
55147f785fSMoritz Fischer 	struct ec_host_request_i2c *ec_request_i2c =
56147f785fSMoritz Fischer 		(struct ec_host_request_i2c *)dev->dout;
57147f785fSMoritz Fischer 	struct ec_host_response_i2c *ec_response_i2c =
58147f785fSMoritz Fischer 		(struct ec_host_response_i2c *)dev->din;
59147f785fSMoritz Fischer 	struct i2c_msg i2c_msg[2];
60147f785fSMoritz Fischer 	int ret;
61147f785fSMoritz Fischer 
62147f785fSMoritz Fischer 	i2c_msg[0].addr = chip->chip_addr;
63147f785fSMoritz Fischer 	i2c_msg[0].flags = 0;
64147f785fSMoritz Fischer 	i2c_msg[1].addr = chip->chip_addr;
65147f785fSMoritz Fischer 	i2c_msg[1].flags = I2C_M_RD;
66147f785fSMoritz Fischer 
67147f785fSMoritz Fischer 	/* one extra byte, to indicate v3 */
68147f785fSMoritz Fischer 	i2c_msg[0].len = out_bytes + 1;
69147f785fSMoritz Fischer 	i2c_msg[0].buf = dev->dout;
70147f785fSMoritz Fischer 
71147f785fSMoritz Fischer 	/* stitch on EC_COMMAND_PROTOCOL_3 */
72147f785fSMoritz Fischer 	memmove(&ec_request_i2c->ec_request, dev->dout, out_bytes);
73147f785fSMoritz Fischer 	ec_request_i2c->command_protocol = EC_COMMAND_PROTOCOL_3;
74147f785fSMoritz Fischer 
75147f785fSMoritz Fischer 	/* two extra bytes for v3 */
76147f785fSMoritz Fischer 	i2c_msg[1].len = in_bytes + 2;
77147f785fSMoritz Fischer 	i2c_msg[1].buf = dev->din;
78147f785fSMoritz Fischer 
79147f785fSMoritz Fischer 	ret = dm_i2c_xfer(udev, &i2c_msg[0], 2);
80147f785fSMoritz Fischer 	if (ret) {
81147f785fSMoritz Fischer 		printf("%s: Could not execute transfer: %d\n", __func__, ret);
82147f785fSMoritz Fischer 		return ret;
83147f785fSMoritz Fischer 	}
84147f785fSMoritz Fischer 
85147f785fSMoritz Fischer 	/* When we send a v3 request to v2 ec, ec won't recognize the 0xda
86147f785fSMoritz Fischer 	 * (EC_COMMAND_PROTOCOL_3) and will return with status
87147f785fSMoritz Fischer 	 * EC_RES_INVALID_COMMAND with zero data length
88147f785fSMoritz Fischer 	 *
89147f785fSMoritz Fischer 	 * In case of invalid command for v3 protocol the data length
90147f785fSMoritz Fischer 	 * will be at least sizeof(struct ec_host_response)
91147f785fSMoritz Fischer 	 */
92147f785fSMoritz Fischer 	if (ec_response_i2c->result == EC_RES_INVALID_COMMAND &&
93147f785fSMoritz Fischer 	    ec_response_i2c->packet_length == 0)
94147f785fSMoritz Fischer 		return -EPROTONOSUPPORT;
95147f785fSMoritz Fischer 
96147f785fSMoritz Fischer 	if (ec_response_i2c->packet_length < sizeof(struct ec_host_response)) {
97147f785fSMoritz Fischer 		printf("%s: response of %u bytes too short; not a full hdr\n",
98147f785fSMoritz Fischer 		       __func__, ec_response_i2c->packet_length);
99147f785fSMoritz Fischer 		return -EBADMSG;
100147f785fSMoritz Fischer 	}
101147f785fSMoritz Fischer 
102147f785fSMoritz Fischer 
103147f785fSMoritz Fischer 	/* drop result and packet_len */
104147f785fSMoritz Fischer 	memmove(dev->din, &ec_response_i2c->ec_response, in_bytes);
105147f785fSMoritz Fischer 
106147f785fSMoritz Fischer 	return in_bytes;
107147f785fSMoritz Fischer }
108147f785fSMoritz Fischer 
cros_ec_i2c_command(struct udevice * udev,uint8_t cmd,int cmd_version,const uint8_t * dout,int dout_len,uint8_t ** dinp,int din_len)10985df958cSSimon Glass static int cros_ec_i2c_command(struct udevice *udev, uint8_t cmd,
11085df958cSSimon Glass 			       int cmd_version, const uint8_t *dout,
11185df958cSSimon Glass 			       int dout_len, uint8_t **dinp, int din_len)
11278764a4eSHung-ying Tyan {
113e564f054SSimon Glass 	struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
114e9b25f2eSMoritz Fischer 	struct dm_i2c_chip *chip = dev_get_parent_platdata(udev);
115e9b25f2eSMoritz Fischer 	struct i2c_msg i2c_msg[2];
11678764a4eSHung-ying Tyan 	/* version8, cmd8, arglen8, out8[dout_len], csum8 */
11778764a4eSHung-ying Tyan 	int out_bytes = dout_len + 4;
11878764a4eSHung-ying Tyan 	/* response8, arglen8, in8[din_len], checksum8 */
11978764a4eSHung-ying Tyan 	int in_bytes = din_len + 3;
12078764a4eSHung-ying Tyan 	uint8_t *ptr;
12178764a4eSHung-ying Tyan 	/* Receive input data, so that args will be dword aligned */
12278764a4eSHung-ying Tyan 	uint8_t *in_ptr;
123e8c12662SRandall Spangler 	int len, csum, ret;
12478764a4eSHung-ying Tyan 
12578764a4eSHung-ying Tyan 	/*
12678764a4eSHung-ying Tyan 	 * Sanity-check I/O sizes given transaction overhead in internal
12778764a4eSHung-ying Tyan 	 * buffers.
12878764a4eSHung-ying Tyan 	 */
12978764a4eSHung-ying Tyan 	if (out_bytes > sizeof(dev->dout)) {
13078764a4eSHung-ying Tyan 		debug("%s: Cannot send %d bytes\n", __func__, dout_len);
13178764a4eSHung-ying Tyan 		return -1;
13278764a4eSHung-ying Tyan 	}
13378764a4eSHung-ying Tyan 	if (in_bytes > sizeof(dev->din)) {
13478764a4eSHung-ying Tyan 		debug("%s: Cannot receive %d bytes\n", __func__, din_len);
13578764a4eSHung-ying Tyan 		return -1;
13678764a4eSHung-ying Tyan 	}
13778764a4eSHung-ying Tyan 	assert(dout_len >= 0);
13878764a4eSHung-ying Tyan 	assert(dinp);
13978764a4eSHung-ying Tyan 
140e9b25f2eSMoritz Fischer 	i2c_msg[0].addr = chip->chip_addr;
141e9b25f2eSMoritz Fischer 	i2c_msg[0].len = out_bytes;
142e9b25f2eSMoritz Fischer 	i2c_msg[0].buf = dev->dout;
143e9b25f2eSMoritz Fischer 	i2c_msg[0].flags = 0;
144e9b25f2eSMoritz Fischer 
14578764a4eSHung-ying Tyan 	/*
14678764a4eSHung-ying Tyan 	 * Copy command and data into output buffer so we can do a single I2C
14778764a4eSHung-ying Tyan 	 * burst transaction.
14878764a4eSHung-ying Tyan 	 */
14978764a4eSHung-ying Tyan 	ptr = dev->dout;
15078764a4eSHung-ying Tyan 
15178764a4eSHung-ying Tyan 	/*
15278764a4eSHung-ying Tyan 	 * in_ptr starts of pointing to a dword-aligned input data buffer.
15378764a4eSHung-ying Tyan 	 * We decrement it back by the number of header bytes we expect to
15478764a4eSHung-ying Tyan 	 * receive, so that the first parameter of the resulting input data
15578764a4eSHung-ying Tyan 	 * will be dword aligned.
15678764a4eSHung-ying Tyan 	 */
15778764a4eSHung-ying Tyan 	in_ptr = dev->din + sizeof(int64_t);
158e8c12662SRandall Spangler 
159e8c12662SRandall Spangler 	if (dev->protocol_version != 2) {
160e8c12662SRandall Spangler 		/* Something we don't support */
161e8c12662SRandall Spangler 		debug("%s: Protocol version %d unsupported\n",
162e8c12662SRandall Spangler 		      __func__, dev->protocol_version);
163e8c12662SRandall Spangler 		return -1;
164e8c12662SRandall Spangler 	}
165e8c12662SRandall Spangler 
16678764a4eSHung-ying Tyan 	*ptr++ = EC_CMD_VERSION0 + cmd_version;
16778764a4eSHung-ying Tyan 	*ptr++ = cmd;
16878764a4eSHung-ying Tyan 	*ptr++ = dout_len;
16978764a4eSHung-ying Tyan 	in_ptr -= 2;	/* Expect status, length bytes */
170e8c12662SRandall Spangler 
17178764a4eSHung-ying Tyan 	memcpy(ptr, dout, dout_len);
17278764a4eSHung-ying Tyan 	ptr += dout_len;
17378764a4eSHung-ying Tyan 
17478764a4eSHung-ying Tyan 	*ptr++ = (uint8_t)
17578764a4eSHung-ying Tyan 		cros_ec_calc_checksum(dev->dout, dout_len + 3);
17678764a4eSHung-ying Tyan 
177e9b25f2eSMoritz Fischer 	i2c_msg[1].addr = chip->chip_addr;
178e9b25f2eSMoritz Fischer 	i2c_msg[1].len = in_bytes;
179e9b25f2eSMoritz Fischer 	i2c_msg[1].buf = in_ptr;
180e9b25f2eSMoritz Fischer 	i2c_msg[1].flags = I2C_M_RD;
181e9b25f2eSMoritz Fischer 
18278764a4eSHung-ying Tyan 	/* Send output data */
18378764a4eSHung-ying Tyan 	cros_ec_dump_data("out", -1, dev->dout, out_bytes);
184e9b25f2eSMoritz Fischer 
185e9b25f2eSMoritz Fischer 	ret = dm_i2c_xfer(udev, &i2c_msg[0], 2);
18678764a4eSHung-ying Tyan 	if (ret) {
187e9b25f2eSMoritz Fischer 		debug("%s: Could not execute transfer to %s\n", __func__,
18885df958cSSimon Glass 		      udev->name);
18978764a4eSHung-ying Tyan 		ret = -1;
19078764a4eSHung-ying Tyan 	}
19178764a4eSHung-ying Tyan 
19278764a4eSHung-ying Tyan 	if (*in_ptr != EC_RES_SUCCESS) {
19378764a4eSHung-ying Tyan 		debug("%s: Received bad result code %d\n", __func__, *in_ptr);
19478764a4eSHung-ying Tyan 		return -(int)*in_ptr;
19578764a4eSHung-ying Tyan 	}
19678764a4eSHung-ying Tyan 
19778764a4eSHung-ying Tyan 	len = in_ptr[1];
19878764a4eSHung-ying Tyan 	if (len + 3 > sizeof(dev->din)) {
19978764a4eSHung-ying Tyan 		debug("%s: Received length %#02x too large\n",
20078764a4eSHung-ying Tyan 		      __func__, len);
20178764a4eSHung-ying Tyan 		return -1;
20278764a4eSHung-ying Tyan 	}
20378764a4eSHung-ying Tyan 	csum = cros_ec_calc_checksum(in_ptr, 2 + len);
20478764a4eSHung-ying Tyan 	if (csum != in_ptr[2 + len]) {
20578764a4eSHung-ying Tyan 		debug("%s: Invalid checksum rx %#02x, calced %#02x\n",
20678764a4eSHung-ying Tyan 		      __func__, in_ptr[2 + din_len], csum);
20778764a4eSHung-ying Tyan 		return -1;
20878764a4eSHung-ying Tyan 	}
20978764a4eSHung-ying Tyan 	din_len = min(din_len, len);
21078764a4eSHung-ying Tyan 	cros_ec_dump_data("in", -1, in_ptr, din_len + 3);
21178764a4eSHung-ying Tyan 
21278764a4eSHung-ying Tyan 	/* Return pointer to dword-aligned input data, if any */
21378764a4eSHung-ying Tyan 	*dinp = dev->din + sizeof(int64_t);
21478764a4eSHung-ying Tyan 
21578764a4eSHung-ying Tyan 	return din_len;
21678764a4eSHung-ying Tyan }
21778764a4eSHung-ying Tyan 
cros_ec_probe(struct udevice * dev)21885df958cSSimon Glass static int cros_ec_probe(struct udevice *dev)
21978764a4eSHung-ying Tyan {
22085df958cSSimon Glass 	return cros_ec_register(dev);
22178764a4eSHung-ying Tyan }
22278764a4eSHung-ying Tyan 
22385df958cSSimon Glass static struct dm_cros_ec_ops cros_ec_ops = {
22485df958cSSimon Glass 	.command = cros_ec_i2c_command,
225147f785fSMoritz Fischer 	.packet = cros_ec_i2c_packet,
22685df958cSSimon Glass };
22778764a4eSHung-ying Tyan 
22885df958cSSimon Glass static const struct udevice_id cros_ec_ids[] = {
2293fbb7871SSimon Glass 	{ .compatible = "google,cros-ec-i2c" },
23085df958cSSimon Glass 	{ }
23185df958cSSimon Glass };
23278764a4eSHung-ying Tyan 
23385df958cSSimon Glass U_BOOT_DRIVER(cros_ec_i2c) = {
2343fbb7871SSimon Glass 	.name		= "cros_ec_i2c",
23585df958cSSimon Glass 	.id		= UCLASS_CROS_EC,
23685df958cSSimon Glass 	.of_match	= cros_ec_ids,
23785df958cSSimon Glass 	.probe		= cros_ec_probe,
23885df958cSSimon Glass 	.ops		= &cros_ec_ops,
23985df958cSSimon Glass };
240