xref: /openbmc/u-boot/drivers/misc/cros_ec_spi.c (revision 527265d8)
1f3424c55SHung-ying Tyan /*
2f3424c55SHung-ying Tyan  * Chromium OS cros_ec driver - SPI interface
3f3424c55SHung-ying Tyan  *
4f3424c55SHung-ying Tyan  * Copyright (c) 2012 The Chromium OS Authors.
5f3424c55SHung-ying Tyan  *
61a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
7f3424c55SHung-ying Tyan  */
8f3424c55SHung-ying Tyan 
9f3424c55SHung-ying Tyan /*
10f3424c55SHung-ying Tyan  * The Matrix Keyboard Protocol driver handles talking to the keyboard
11f3424c55SHung-ying Tyan  * controller chip. Mostly this is for keyboard functions, but some other
12f3424c55SHung-ying Tyan  * things have slipped in, so we provide generic services to talk to the
13f3424c55SHung-ying Tyan  * KBC.
14f3424c55SHung-ying Tyan  */
15f3424c55SHung-ying Tyan 
16f3424c55SHung-ying Tyan #include <common.h>
17f3424c55SHung-ying Tyan #include <cros_ec.h>
18ea0ebc86SSimon Glass #include <dm.h>
19ea0ebc86SSimon Glass #include <errno.h>
20f3424c55SHung-ying Tyan #include <spi.h>
21f3424c55SHung-ying Tyan 
22ea0ebc86SSimon Glass DECLARE_GLOBAL_DATA_PTR;
23ea0ebc86SSimon Glass 
24ea0ebc86SSimon Glass int cros_ec_spi_packet(struct udevice *udev, int out_bytes, int in_bytes)
25ea0ebc86SSimon Glass {
26e564f054SSimon Glass 	struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
27ea0ebc86SSimon Glass 	struct spi_slave *slave = dev_get_parentdata(dev->dev);
28*527265d8SSimon Glass 	ulong start;
29*527265d8SSimon Glass 	uint8_t byte;
30a6070283SRandall Spangler 	int rv;
31a6070283SRandall Spangler 
32a6070283SRandall Spangler 	/* Do the transfer */
33ea0ebc86SSimon Glass 	if (spi_claim_bus(slave)) {
34a6070283SRandall Spangler 		debug("%s: Cannot claim SPI bus\n", __func__);
35a6070283SRandall Spangler 		return -1;
36a6070283SRandall Spangler 	}
37a6070283SRandall Spangler 
38*527265d8SSimon Glass 	rv = spi_xfer(slave, out_bytes * 8, dev->dout, NULL, SPI_XFER_BEGIN);
39*527265d8SSimon Glass 	if (rv)
40*527265d8SSimon Glass 		goto done;
41*527265d8SSimon Glass 	start = get_timer(0);
42*527265d8SSimon Glass 	while (1) {
43*527265d8SSimon Glass 		rv = spi_xfer(slave, 8, NULL, &byte, 0);
44*527265d8SSimon Glass 		if (byte == SPI_PREAMBLE_END_BYTE)
45*527265d8SSimon Glass 			break;
46*527265d8SSimon Glass 		if (rv)
47*527265d8SSimon Glass 			goto done;
48*527265d8SSimon Glass 		if (get_timer(start) > 100) {
49*527265d8SSimon Glass 			rv = -ETIMEDOUT;
50*527265d8SSimon Glass 			goto done;
51*527265d8SSimon Glass 		}
52*527265d8SSimon Glass 	}
53a6070283SRandall Spangler 
54*527265d8SSimon Glass 	rv = spi_xfer(slave, in_bytes * 8, NULL, dev->din, 0);
55*527265d8SSimon Glass done:
56*527265d8SSimon Glass 	spi_xfer(slave, 0, NULL, NULL, SPI_XFER_END);
57ea0ebc86SSimon Glass 	spi_release_bus(slave);
58a6070283SRandall Spangler 
59a6070283SRandall Spangler 	if (rv) {
60a6070283SRandall Spangler 		debug("%s: Cannot complete SPI transfer\n", __func__);
61a6070283SRandall Spangler 		return -1;
62a6070283SRandall Spangler 	}
63a6070283SRandall Spangler 
64a6070283SRandall Spangler 	return in_bytes;
65a6070283SRandall Spangler }
66a6070283SRandall Spangler 
67f3424c55SHung-ying Tyan /**
68f3424c55SHung-ying Tyan  * Send a command to a LPC CROS_EC device and return the reply.
69f3424c55SHung-ying Tyan  *
70f3424c55SHung-ying Tyan  * The device's internal input/output buffers are used.
71f3424c55SHung-ying Tyan  *
72f3424c55SHung-ying Tyan  * @param dev		CROS_EC device
73f3424c55SHung-ying Tyan  * @param cmd		Command to send (EC_CMD_...)
74f3424c55SHung-ying Tyan  * @param cmd_version	Version of command to send (EC_VER_...)
75f3424c55SHung-ying Tyan  * @param dout		Output data (may be NULL If dout_len=0)
76f3424c55SHung-ying Tyan  * @param dout_len      Size of output data in bytes
77f3424c55SHung-ying Tyan  * @param dinp		Returns pointer to response data. This will be
78f3424c55SHung-ying Tyan  *			untouched unless we return a value > 0.
79f3424c55SHung-ying Tyan  * @param din_len	Maximum size of response in bytes
80f3424c55SHung-ying Tyan  * @return number of bytes in response, or -1 on error
81f3424c55SHung-ying Tyan  */
82ea0ebc86SSimon Glass int cros_ec_spi_command(struct udevice *udev, uint8_t cmd, int cmd_version,
83ea0ebc86SSimon Glass 		     const uint8_t *dout, int dout_len,
84ea0ebc86SSimon Glass 		     uint8_t **dinp, int din_len)
85ea0ebc86SSimon Glass {
86e564f054SSimon Glass 	struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
87ea0ebc86SSimon Glass 	struct spi_slave *slave = dev_get_parentdata(dev->dev);
88f3424c55SHung-ying Tyan 	int in_bytes = din_len + 4;	/* status, length, checksum, trailer */
89f3424c55SHung-ying Tyan 	uint8_t *out;
90f3424c55SHung-ying Tyan 	uint8_t *p;
91f3424c55SHung-ying Tyan 	int csum, len;
92f3424c55SHung-ying Tyan 	int rv;
93f3424c55SHung-ying Tyan 
94e8c12662SRandall Spangler 	if (dev->protocol_version != 2) {
95e8c12662SRandall Spangler 		debug("%s: Unsupported EC protcol version %d\n",
96e8c12662SRandall Spangler 		      __func__, dev->protocol_version);
97e8c12662SRandall Spangler 		return -1;
98e8c12662SRandall Spangler 	}
99e8c12662SRandall Spangler 
100f3424c55SHung-ying Tyan 	/*
101f3424c55SHung-ying Tyan 	 * Sanity-check input size to make sure it plus transaction overhead
102f3424c55SHung-ying Tyan 	 * fits in the internal device buffer.
103f3424c55SHung-ying Tyan 	 */
104f3424c55SHung-ying Tyan 	if (in_bytes > sizeof(dev->din)) {
105f3424c55SHung-ying Tyan 		debug("%s: Cannot receive %d bytes\n", __func__, din_len);
106f3424c55SHung-ying Tyan 		return -1;
107f3424c55SHung-ying Tyan 	}
108f3424c55SHung-ying Tyan 
109f3424c55SHung-ying Tyan 	/* We represent message length as a byte */
110f3424c55SHung-ying Tyan 	if (dout_len > 0xff) {
111f3424c55SHung-ying Tyan 		debug("%s: Cannot send %d bytes\n", __func__, dout_len);
112f3424c55SHung-ying Tyan 		return -1;
113f3424c55SHung-ying Tyan 	}
114f3424c55SHung-ying Tyan 
115f3424c55SHung-ying Tyan 	/*
116f3424c55SHung-ying Tyan 	 * Clear input buffer so we don't get false hits for MSG_HEADER
117f3424c55SHung-ying Tyan 	 */
118f3424c55SHung-ying Tyan 	memset(dev->din, '\0', in_bytes);
119f3424c55SHung-ying Tyan 
120ea0ebc86SSimon Glass 	if (spi_claim_bus(slave)) {
121f3424c55SHung-ying Tyan 		debug("%s: Cannot claim SPI bus\n", __func__);
122f3424c55SHung-ying Tyan 		return -1;
123f3424c55SHung-ying Tyan 	}
124f3424c55SHung-ying Tyan 
125f3424c55SHung-ying Tyan 	out = dev->dout;
1262001b9a6SSimon Glass 	out[0] = EC_CMD_VERSION0 + cmd_version;
127f3424c55SHung-ying Tyan 	out[1] = cmd;
128f3424c55SHung-ying Tyan 	out[2] = (uint8_t)dout_len;
129f3424c55SHung-ying Tyan 	memcpy(out + 3, dout, dout_len);
130f3424c55SHung-ying Tyan 	csum = cros_ec_calc_checksum(out, 3)
131f3424c55SHung-ying Tyan 	       + cros_ec_calc_checksum(dout, dout_len);
132f3424c55SHung-ying Tyan 	out[3 + dout_len] = (uint8_t)csum;
133f3424c55SHung-ying Tyan 
134f3424c55SHung-ying Tyan 	/*
135f3424c55SHung-ying Tyan 	 * Send output data and receive input data starting such that the
136f3424c55SHung-ying Tyan 	 * message body will be dword aligned.
137f3424c55SHung-ying Tyan 	 */
138f3424c55SHung-ying Tyan 	p = dev->din + sizeof(int64_t) - 2;
139f3424c55SHung-ying Tyan 	len = dout_len + 4;
140f3424c55SHung-ying Tyan 	cros_ec_dump_data("out", cmd, out, len);
141ea0ebc86SSimon Glass 	rv = spi_xfer(slave, max(len, in_bytes) * 8, out, p,
142f3424c55SHung-ying Tyan 		      SPI_XFER_BEGIN | SPI_XFER_END);
143f3424c55SHung-ying Tyan 
144ea0ebc86SSimon Glass 	spi_release_bus(slave);
145f3424c55SHung-ying Tyan 
146f3424c55SHung-ying Tyan 	if (rv) {
147f3424c55SHung-ying Tyan 		debug("%s: Cannot complete SPI transfer\n", __func__);
148f3424c55SHung-ying Tyan 		return -1;
149f3424c55SHung-ying Tyan 	}
150f3424c55SHung-ying Tyan 
151b4141195SMasahiro Yamada 	len = min((int)p[1], din_len);
152f3424c55SHung-ying Tyan 	cros_ec_dump_data("in", -1, p, len + 3);
153f3424c55SHung-ying Tyan 
154f3424c55SHung-ying Tyan 	/* Response code is first byte of message */
155f3424c55SHung-ying Tyan 	if (p[0] != EC_RES_SUCCESS) {
156f3424c55SHung-ying Tyan 		printf("%s: Returned status %d\n", __func__, p[0]);
157f3424c55SHung-ying Tyan 		return -(int)(p[0]);
158f3424c55SHung-ying Tyan 	}
159f3424c55SHung-ying Tyan 
160f3424c55SHung-ying Tyan 	/* Check checksum */
161f3424c55SHung-ying Tyan 	csum = cros_ec_calc_checksum(p, len + 2);
162f3424c55SHung-ying Tyan 	if (csum != p[len + 2]) {
163f3424c55SHung-ying Tyan 		debug("%s: Invalid checksum rx %#02x, calced %#02x\n", __func__,
164f3424c55SHung-ying Tyan 		      p[2 + len], csum);
165f3424c55SHung-ying Tyan 		return -1;
166f3424c55SHung-ying Tyan 	}
167f3424c55SHung-ying Tyan 
168f3424c55SHung-ying Tyan 	/* Anything else is the response data */
169f3424c55SHung-ying Tyan 	*dinp = p + 2;
170f3424c55SHung-ying Tyan 
171f3424c55SHung-ying Tyan 	return len;
172f3424c55SHung-ying Tyan }
173f3424c55SHung-ying Tyan 
174b2568f0dSSimon Glass static int cros_ec_probe(struct udevice *dev)
175ea0ebc86SSimon Glass {
176ea0ebc86SSimon Glass 	return cros_ec_register(dev);
177ea0ebc86SSimon Glass }
178ea0ebc86SSimon Glass 
179b2568f0dSSimon Glass static struct dm_cros_ec_ops cros_ec_ops = {
180ea0ebc86SSimon Glass 	.packet = cros_ec_spi_packet,
181ea0ebc86SSimon Glass 	.command = cros_ec_spi_command,
182ea0ebc86SSimon Glass };
183ea0ebc86SSimon Glass 
184ea0ebc86SSimon Glass static const struct udevice_id cros_ec_ids[] = {
1853fbb7871SSimon Glass 	{ .compatible = "google,cros-ec-spi" },
186ea0ebc86SSimon Glass 	{ }
187ea0ebc86SSimon Glass };
188ea0ebc86SSimon Glass 
189ea0ebc86SSimon Glass U_BOOT_DRIVER(cros_ec_spi) = {
1903fbb7871SSimon Glass 	.name		= "cros_ec_spi",
191ea0ebc86SSimon Glass 	.id		= UCLASS_CROS_EC,
192ea0ebc86SSimon Glass 	.of_match	= cros_ec_ids,
193ea0ebc86SSimon Glass 	.probe		= cros_ec_probe,
194ea0ebc86SSimon Glass 	.ops		= &cros_ec_ops,
195ea0ebc86SSimon Glass };
196