xref: /openbmc/u-boot/drivers/misc/cros_ec_i2c.c (revision 147f785f)
178764a4eSHung-ying Tyan /*
278764a4eSHung-ying Tyan  * Chromium OS cros_ec driver - I2C interface
378764a4eSHung-ying Tyan  *
478764a4eSHung-ying Tyan  * Copyright (c) 2012 The Chromium OS Authors.
578764a4eSHung-ying Tyan  *
61a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
778764a4eSHung-ying Tyan  */
878764a4eSHung-ying Tyan 
978764a4eSHung-ying Tyan /*
1078764a4eSHung-ying Tyan  * The Matrix Keyboard Protocol driver handles talking to the keyboard
1178764a4eSHung-ying Tyan  * controller chip. Mostly this is for keyboard functions, but some other
1278764a4eSHung-ying Tyan  * things have slipped in, so we provide generic services to talk to the
1378764a4eSHung-ying Tyan  * KBC.
1478764a4eSHung-ying Tyan  */
1578764a4eSHung-ying Tyan 
1678764a4eSHung-ying Tyan #include <common.h>
1785df958cSSimon Glass #include <dm.h>
1878764a4eSHung-ying Tyan #include <i2c.h>
1978764a4eSHung-ying Tyan #include <cros_ec.h>
2078764a4eSHung-ying Tyan 
2178764a4eSHung-ying Tyan #ifdef DEBUG_TRACE
2278764a4eSHung-ying Tyan #define debug_trace(fmt, b...)	debug(fmt, #b)
2378764a4eSHung-ying Tyan #else
2478764a4eSHung-ying Tyan #define debug_trace(fmt, b...)
2578764a4eSHung-ying Tyan #endif
2678764a4eSHung-ying Tyan 
27*147f785fSMoritz Fischer /**
28*147f785fSMoritz Fischer  * Request format for protocol v3
29*147f785fSMoritz Fischer  * byte 0	0xda (EC_COMMAND_PROTOCOL_3)
30*147f785fSMoritz Fischer  * byte 1-8	struct ec_host_request
31*147f785fSMoritz Fischer  * byte 10-	response data
32*147f785fSMoritz Fischer  */
33*147f785fSMoritz Fischer struct ec_host_request_i2c {
34*147f785fSMoritz Fischer 	/* Always 0xda to backward compatible with v2 struct */
35*147f785fSMoritz Fischer 	uint8_t  command_protocol;
36*147f785fSMoritz Fischer 	struct ec_host_request ec_request;
37*147f785fSMoritz Fischer } __packed;
38*147f785fSMoritz Fischer 
39*147f785fSMoritz Fischer /*
40*147f785fSMoritz Fischer  * Response format for protocol v3
41*147f785fSMoritz Fischer  * byte 0	result code
42*147f785fSMoritz Fischer  * byte 1	packet_length
43*147f785fSMoritz Fischer  * byte 2-9	struct ec_host_response
44*147f785fSMoritz Fischer  * byte 10-	response data
45*147f785fSMoritz Fischer  */
46*147f785fSMoritz Fischer struct ec_host_response_i2c {
47*147f785fSMoritz Fischer 	uint8_t result;
48*147f785fSMoritz Fischer 	uint8_t packet_length;
49*147f785fSMoritz Fischer 	struct ec_host_response ec_response;
50*147f785fSMoritz Fischer } __packed;
51*147f785fSMoritz Fischer 
52*147f785fSMoritz Fischer static int cros_ec_i2c_packet(struct udevice *udev, int out_bytes, int in_bytes)
53*147f785fSMoritz Fischer {
54*147f785fSMoritz Fischer 	struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
55*147f785fSMoritz Fischer 	struct dm_i2c_chip *chip = dev_get_parent_platdata(udev);
56*147f785fSMoritz Fischer 	struct ec_host_request_i2c *ec_request_i2c =
57*147f785fSMoritz Fischer 		(struct ec_host_request_i2c *)dev->dout;
58*147f785fSMoritz Fischer 	struct ec_host_response_i2c *ec_response_i2c =
59*147f785fSMoritz Fischer 		(struct ec_host_response_i2c *)dev->din;
60*147f785fSMoritz Fischer 	struct i2c_msg i2c_msg[2];
61*147f785fSMoritz Fischer 	int ret;
62*147f785fSMoritz Fischer 
63*147f785fSMoritz Fischer 	i2c_msg[0].addr = chip->chip_addr;
64*147f785fSMoritz Fischer 	i2c_msg[0].flags = 0;
65*147f785fSMoritz Fischer 	i2c_msg[1].addr = chip->chip_addr;
66*147f785fSMoritz Fischer 	i2c_msg[1].flags = I2C_M_RD;
67*147f785fSMoritz Fischer 
68*147f785fSMoritz Fischer 	/* one extra byte, to indicate v3 */
69*147f785fSMoritz Fischer 	i2c_msg[0].len = out_bytes + 1;
70*147f785fSMoritz Fischer 	i2c_msg[0].buf = dev->dout;
71*147f785fSMoritz Fischer 
72*147f785fSMoritz Fischer 	/* stitch on EC_COMMAND_PROTOCOL_3 */
73*147f785fSMoritz Fischer 	memmove(&ec_request_i2c->ec_request, dev->dout, out_bytes);
74*147f785fSMoritz Fischer 	ec_request_i2c->command_protocol = EC_COMMAND_PROTOCOL_3;
75*147f785fSMoritz Fischer 
76*147f785fSMoritz Fischer 	/* two extra bytes for v3 */
77*147f785fSMoritz Fischer 	i2c_msg[1].len = in_bytes + 2;
78*147f785fSMoritz Fischer 	i2c_msg[1].buf = dev->din;
79*147f785fSMoritz Fischer 
80*147f785fSMoritz Fischer 	ret = dm_i2c_xfer(udev, &i2c_msg[0], 2);
81*147f785fSMoritz Fischer 	if (ret) {
82*147f785fSMoritz Fischer 		printf("%s: Could not execute transfer: %d\n", __func__, ret);
83*147f785fSMoritz Fischer 		return ret;
84*147f785fSMoritz Fischer 	}
85*147f785fSMoritz Fischer 
86*147f785fSMoritz Fischer 	/* When we send a v3 request to v2 ec, ec won't recognize the 0xda
87*147f785fSMoritz Fischer 	 * (EC_COMMAND_PROTOCOL_3) and will return with status
88*147f785fSMoritz Fischer 	 * EC_RES_INVALID_COMMAND with zero data length
89*147f785fSMoritz Fischer 	 *
90*147f785fSMoritz Fischer 	 * In case of invalid command for v3 protocol the data length
91*147f785fSMoritz Fischer 	 * will be at least sizeof(struct ec_host_response)
92*147f785fSMoritz Fischer 	 */
93*147f785fSMoritz Fischer 	if (ec_response_i2c->result == EC_RES_INVALID_COMMAND &&
94*147f785fSMoritz Fischer 	    ec_response_i2c->packet_length == 0)
95*147f785fSMoritz Fischer 		return -EPROTONOSUPPORT;
96*147f785fSMoritz Fischer 
97*147f785fSMoritz Fischer 	if (ec_response_i2c->packet_length < sizeof(struct ec_host_response)) {
98*147f785fSMoritz Fischer 		printf("%s: response of %u bytes too short; not a full hdr\n",
99*147f785fSMoritz Fischer 		       __func__, ec_response_i2c->packet_length);
100*147f785fSMoritz Fischer 		return -EBADMSG;
101*147f785fSMoritz Fischer 	}
102*147f785fSMoritz Fischer 
103*147f785fSMoritz Fischer 
104*147f785fSMoritz Fischer 	/* drop result and packet_len */
105*147f785fSMoritz Fischer 	memmove(dev->din, &ec_response_i2c->ec_response, in_bytes);
106*147f785fSMoritz Fischer 
107*147f785fSMoritz Fischer 	return in_bytes;
108*147f785fSMoritz Fischer }
109*147f785fSMoritz Fischer 
11085df958cSSimon Glass static int cros_ec_i2c_command(struct udevice *udev, uint8_t cmd,
11185df958cSSimon Glass 			       int cmd_version, const uint8_t *dout,
11285df958cSSimon Glass 			       int dout_len, uint8_t **dinp, int din_len)
11378764a4eSHung-ying Tyan {
114e564f054SSimon Glass 	struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
115e9b25f2eSMoritz Fischer 	struct dm_i2c_chip *chip = dev_get_parent_platdata(udev);
116e9b25f2eSMoritz Fischer 	struct i2c_msg i2c_msg[2];
11778764a4eSHung-ying Tyan 	/* version8, cmd8, arglen8, out8[dout_len], csum8 */
11878764a4eSHung-ying Tyan 	int out_bytes = dout_len + 4;
11978764a4eSHung-ying Tyan 	/* response8, arglen8, in8[din_len], checksum8 */
12078764a4eSHung-ying Tyan 	int in_bytes = din_len + 3;
12178764a4eSHung-ying Tyan 	uint8_t *ptr;
12278764a4eSHung-ying Tyan 	/* Receive input data, so that args will be dword aligned */
12378764a4eSHung-ying Tyan 	uint8_t *in_ptr;
124e8c12662SRandall Spangler 	int len, csum, ret;
12578764a4eSHung-ying Tyan 
12678764a4eSHung-ying Tyan 	/*
12778764a4eSHung-ying Tyan 	 * Sanity-check I/O sizes given transaction overhead in internal
12878764a4eSHung-ying Tyan 	 * buffers.
12978764a4eSHung-ying Tyan 	 */
13078764a4eSHung-ying Tyan 	if (out_bytes > sizeof(dev->dout)) {
13178764a4eSHung-ying Tyan 		debug("%s: Cannot send %d bytes\n", __func__, dout_len);
13278764a4eSHung-ying Tyan 		return -1;
13378764a4eSHung-ying Tyan 	}
13478764a4eSHung-ying Tyan 	if (in_bytes > sizeof(dev->din)) {
13578764a4eSHung-ying Tyan 		debug("%s: Cannot receive %d bytes\n", __func__, din_len);
13678764a4eSHung-ying Tyan 		return -1;
13778764a4eSHung-ying Tyan 	}
13878764a4eSHung-ying Tyan 	assert(dout_len >= 0);
13978764a4eSHung-ying Tyan 	assert(dinp);
14078764a4eSHung-ying Tyan 
141e9b25f2eSMoritz Fischer 	i2c_msg[0].addr = chip->chip_addr;
142e9b25f2eSMoritz Fischer 	i2c_msg[0].len = out_bytes;
143e9b25f2eSMoritz Fischer 	i2c_msg[0].buf = dev->dout;
144e9b25f2eSMoritz Fischer 	i2c_msg[0].flags = 0;
145e9b25f2eSMoritz Fischer 
14678764a4eSHung-ying Tyan 	/*
14778764a4eSHung-ying Tyan 	 * Copy command and data into output buffer so we can do a single I2C
14878764a4eSHung-ying Tyan 	 * burst transaction.
14978764a4eSHung-ying Tyan 	 */
15078764a4eSHung-ying Tyan 	ptr = dev->dout;
15178764a4eSHung-ying Tyan 
15278764a4eSHung-ying Tyan 	/*
15378764a4eSHung-ying Tyan 	 * in_ptr starts of pointing to a dword-aligned input data buffer.
15478764a4eSHung-ying Tyan 	 * We decrement it back by the number of header bytes we expect to
15578764a4eSHung-ying Tyan 	 * receive, so that the first parameter of the resulting input data
15678764a4eSHung-ying Tyan 	 * will be dword aligned.
15778764a4eSHung-ying Tyan 	 */
15878764a4eSHung-ying Tyan 	in_ptr = dev->din + sizeof(int64_t);
159e8c12662SRandall Spangler 
160e8c12662SRandall Spangler 	if (dev->protocol_version != 2) {
161e8c12662SRandall Spangler 		/* Something we don't support */
162e8c12662SRandall Spangler 		debug("%s: Protocol version %d unsupported\n",
163e8c12662SRandall Spangler 		      __func__, dev->protocol_version);
164e8c12662SRandall Spangler 		return -1;
165e8c12662SRandall Spangler 	}
166e8c12662SRandall Spangler 
16778764a4eSHung-ying Tyan 	*ptr++ = EC_CMD_VERSION0 + cmd_version;
16878764a4eSHung-ying Tyan 	*ptr++ = cmd;
16978764a4eSHung-ying Tyan 	*ptr++ = dout_len;
17078764a4eSHung-ying Tyan 	in_ptr -= 2;	/* Expect status, length bytes */
171e8c12662SRandall Spangler 
17278764a4eSHung-ying Tyan 	memcpy(ptr, dout, dout_len);
17378764a4eSHung-ying Tyan 	ptr += dout_len;
17478764a4eSHung-ying Tyan 
17578764a4eSHung-ying Tyan 	*ptr++ = (uint8_t)
17678764a4eSHung-ying Tyan 		cros_ec_calc_checksum(dev->dout, dout_len + 3);
17778764a4eSHung-ying Tyan 
178e9b25f2eSMoritz Fischer 	i2c_msg[1].addr = chip->chip_addr;
179e9b25f2eSMoritz Fischer 	i2c_msg[1].len = in_bytes;
180e9b25f2eSMoritz Fischer 	i2c_msg[1].buf = in_ptr;
181e9b25f2eSMoritz Fischer 	i2c_msg[1].flags = I2C_M_RD;
182e9b25f2eSMoritz Fischer 
18378764a4eSHung-ying Tyan 	/* Send output data */
18478764a4eSHung-ying Tyan 	cros_ec_dump_data("out", -1, dev->dout, out_bytes);
185e9b25f2eSMoritz Fischer 
186e9b25f2eSMoritz Fischer 	ret = dm_i2c_xfer(udev, &i2c_msg[0], 2);
18778764a4eSHung-ying Tyan 	if (ret) {
188e9b25f2eSMoritz Fischer 		debug("%s: Could not execute transfer to %s\n", __func__,
18985df958cSSimon Glass 		      udev->name);
19078764a4eSHung-ying Tyan 		ret = -1;
19178764a4eSHung-ying Tyan 	}
19278764a4eSHung-ying Tyan 
19378764a4eSHung-ying Tyan 	if (*in_ptr != EC_RES_SUCCESS) {
19478764a4eSHung-ying Tyan 		debug("%s: Received bad result code %d\n", __func__, *in_ptr);
19578764a4eSHung-ying Tyan 		return -(int)*in_ptr;
19678764a4eSHung-ying Tyan 	}
19778764a4eSHung-ying Tyan 
19878764a4eSHung-ying Tyan 	len = in_ptr[1];
19978764a4eSHung-ying Tyan 	if (len + 3 > sizeof(dev->din)) {
20078764a4eSHung-ying Tyan 		debug("%s: Received length %#02x too large\n",
20178764a4eSHung-ying Tyan 		      __func__, len);
20278764a4eSHung-ying Tyan 		return -1;
20378764a4eSHung-ying Tyan 	}
20478764a4eSHung-ying Tyan 	csum = cros_ec_calc_checksum(in_ptr, 2 + len);
20578764a4eSHung-ying Tyan 	if (csum != in_ptr[2 + len]) {
20678764a4eSHung-ying Tyan 		debug("%s: Invalid checksum rx %#02x, calced %#02x\n",
20778764a4eSHung-ying Tyan 		      __func__, in_ptr[2 + din_len], csum);
20878764a4eSHung-ying Tyan 		return -1;
20978764a4eSHung-ying Tyan 	}
21078764a4eSHung-ying Tyan 	din_len = min(din_len, len);
21178764a4eSHung-ying Tyan 	cros_ec_dump_data("in", -1, in_ptr, din_len + 3);
21278764a4eSHung-ying Tyan 
21378764a4eSHung-ying Tyan 	/* Return pointer to dword-aligned input data, if any */
21478764a4eSHung-ying Tyan 	*dinp = dev->din + sizeof(int64_t);
21578764a4eSHung-ying Tyan 
21678764a4eSHung-ying Tyan 	return din_len;
21778764a4eSHung-ying Tyan }
21878764a4eSHung-ying Tyan 
21985df958cSSimon Glass static int cros_ec_probe(struct udevice *dev)
22078764a4eSHung-ying Tyan {
22185df958cSSimon Glass 	return cros_ec_register(dev);
22278764a4eSHung-ying Tyan }
22378764a4eSHung-ying Tyan 
22485df958cSSimon Glass static struct dm_cros_ec_ops cros_ec_ops = {
22585df958cSSimon Glass 	.command = cros_ec_i2c_command,
226*147f785fSMoritz Fischer 	.packet = cros_ec_i2c_packet,
22785df958cSSimon Glass };
22878764a4eSHung-ying Tyan 
22985df958cSSimon Glass static const struct udevice_id cros_ec_ids[] = {
2303fbb7871SSimon Glass 	{ .compatible = "google,cros-ec-i2c" },
23185df958cSSimon Glass 	{ }
23285df958cSSimon Glass };
23378764a4eSHung-ying Tyan 
23485df958cSSimon Glass U_BOOT_DRIVER(cros_ec_i2c) = {
2353fbb7871SSimon Glass 	.name		= "cros_ec_i2c",
23685df958cSSimon Glass 	.id		= UCLASS_CROS_EC,
23785df958cSSimon Glass 	.of_match	= cros_ec_ids,
23885df958cSSimon Glass 	.probe		= cros_ec_probe,
23985df958cSSimon Glass 	.ops		= &cros_ec_ops,
24085df958cSSimon Glass };
241