xref: /openbmc/u-boot/drivers/misc/cros_ec_spi.c (revision 713cb680)
1 /*
2  * Chromium OS cros_ec driver - SPI interface
3  *
4  * Copyright (c) 2012 The Chromium OS Authors.
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 /*
25  * The Matrix Keyboard Protocol driver handles talking to the keyboard
26  * controller chip. Mostly this is for keyboard functions, but some other
27  * things have slipped in, so we provide generic services to talk to the
28  * KBC.
29  */
30 
31 #include <common.h>
32 #include <cros_ec.h>
33 #include <spi.h>
34 
35 /**
36  * Send a command to a LPC CROS_EC device and return the reply.
37  *
38  * The device's internal input/output buffers are used.
39  *
40  * @param dev		CROS_EC device
41  * @param cmd		Command to send (EC_CMD_...)
42  * @param cmd_version	Version of command to send (EC_VER_...)
43  * @param dout		Output data (may be NULL If dout_len=0)
44  * @param dout_len      Size of output data in bytes
45  * @param dinp		Returns pointer to response data. This will be
46  *			untouched unless we return a value > 0.
47  * @param din_len	Maximum size of response in bytes
48  * @return number of bytes in response, or -1 on error
49  */
50 int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
51 		     const uint8_t *dout, int dout_len,
52 		     uint8_t **dinp, int din_len)
53 {
54 	int in_bytes = din_len + 4;	/* status, length, checksum, trailer */
55 	uint8_t *out;
56 	uint8_t *p;
57 	int csum, len;
58 	int rv;
59 
60 	/*
61 	 * Sanity-check input size to make sure it plus transaction overhead
62 	 * fits in the internal device buffer.
63 	 */
64 	if (in_bytes > sizeof(dev->din)) {
65 		debug("%s: Cannot receive %d bytes\n", __func__, din_len);
66 		return -1;
67 	}
68 
69 	/* We represent message length as a byte */
70 	if (dout_len > 0xff) {
71 		debug("%s: Cannot send %d bytes\n", __func__, dout_len);
72 		return -1;
73 	}
74 
75 	/*
76 	 * Clear input buffer so we don't get false hits for MSG_HEADER
77 	 */
78 	memset(dev->din, '\0', in_bytes);
79 
80 	if (spi_claim_bus(dev->spi)) {
81 		debug("%s: Cannot claim SPI bus\n", __func__);
82 		return -1;
83 	}
84 
85 	out = dev->dout;
86 	out[0] = cmd_version;
87 	out[1] = cmd;
88 	out[2] = (uint8_t)dout_len;
89 	memcpy(out + 3, dout, dout_len);
90 	csum = cros_ec_calc_checksum(out, 3)
91 	       + cros_ec_calc_checksum(dout, dout_len);
92 	out[3 + dout_len] = (uint8_t)csum;
93 
94 	/*
95 	 * Send output data and receive input data starting such that the
96 	 * message body will be dword aligned.
97 	 */
98 	p = dev->din + sizeof(int64_t) - 2;
99 	len = dout_len + 4;
100 	cros_ec_dump_data("out", cmd, out, len);
101 	rv = spi_xfer(dev->spi, max(len, in_bytes) * 8, out, p,
102 		      SPI_XFER_BEGIN | SPI_XFER_END);
103 
104 	spi_release_bus(dev->spi);
105 
106 	if (rv) {
107 		debug("%s: Cannot complete SPI transfer\n", __func__);
108 		return -1;
109 	}
110 
111 	len = min(p[1], din_len);
112 	cros_ec_dump_data("in", -1, p, len + 3);
113 
114 	/* Response code is first byte of message */
115 	if (p[0] != EC_RES_SUCCESS) {
116 		printf("%s: Returned status %d\n", __func__, p[0]);
117 		return -(int)(p[0]);
118 	}
119 
120 	/* Check checksum */
121 	csum = cros_ec_calc_checksum(p, len + 2);
122 	if (csum != p[len + 2]) {
123 		debug("%s: Invalid checksum rx %#02x, calced %#02x\n", __func__,
124 		      p[2 + len], csum);
125 		return -1;
126 	}
127 
128 	/* Anything else is the response data */
129 	*dinp = p + 2;
130 
131 	return len;
132 }
133 
134 int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob)
135 {
136 	/* Decode interface-specific FDT params */
137 	dev->max_frequency = fdtdec_get_int(blob, dev->node,
138 					    "spi-max-frequency", 500000);
139 	dev->cs = fdtdec_get_int(blob, dev->node, "reg", 0);
140 
141 	return 0;
142 }
143 
144 /**
145  * Initialize SPI protocol.
146  *
147  * @param dev		CROS_EC device
148  * @param blob		Device tree blob
149  * @return 0 if ok, -1 on error
150  */
151 int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob)
152 {
153 	dev->spi = spi_setup_slave_fdt(blob, dev->parent_node,
154 				       dev->cs, dev->max_frequency, 0);
155 	if (!dev->spi) {
156 		debug("%s: Could not setup SPI slave\n", __func__);
157 		return -1;
158 	}
159 
160 	return 0;
161 }
162