xref: /openbmc/u-boot/drivers/misc/cros_ec_lpc.c (revision e246b728)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Chromium OS cros_ec driver - LPC interface
4  *
5  * Copyright (c) 2012 The Chromium OS Authors.
6  */
7 
8 /*
9  * The Matrix Keyboard Protocol driver handles talking to the keyboard
10  * controller chip. Mostly this is for keyboard functions, but some other
11  * things have slipped in, so we provide generic services to talk to the
12  * KBC.
13  */
14 
15 #include <common.h>
16 #include <dm.h>
17 #include <command.h>
18 #include <cros_ec.h>
19 #include <asm/io.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 static int wait_for_sync(struct cros_ec_dev *dev)
28 {
29 	unsigned long start;
30 
31 	start = get_timer(0);
32 	while (inb(EC_LPC_ADDR_HOST_CMD) & EC_LPC_STATUS_BUSY_MASK) {
33 		if (get_timer(start) > 1000) {
34 			debug("%s: Timeout waiting for CROS_EC sync\n",
35 			      __func__);
36 			return -1;
37 		}
38 	}
39 
40 	return 0;
41 }
42 
43 int cros_ec_lpc_packet(struct udevice *udev, int out_bytes, int in_bytes)
44 {
45 	struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
46 	uint8_t *d;
47 	int i;
48 
49 	if (out_bytes > EC_LPC_HOST_PACKET_SIZE)
50 		return log_msg_ret("Cannot send that many bytes\n", -E2BIG);
51 
52 	if (in_bytes > EC_LPC_HOST_PACKET_SIZE)
53 		return log_msg_ret("Cannot receive that many bytes\n", -E2BIG);
54 
55 	if (wait_for_sync(dev))
56 		return log_msg_ret("Timeout waiting ready\n", -ETIMEDOUT);
57 
58 	/* Write data */
59 	for (i = 0, d = (uint8_t *)dev->dout; i < out_bytes; i++, d++)
60 		outb(*d, EC_LPC_ADDR_HOST_PACKET + i);
61 
62 	/* Start the command */
63 	outb(EC_COMMAND_PROTOCOL_3, EC_LPC_ADDR_HOST_CMD);
64 
65 	if (wait_for_sync(dev))
66 		return log_msg_ret("Timeout waiting ready\n", -ETIMEDOUT);
67 
68 	/* Read back args */
69 	for (i = 0, d = dev->din; i < in_bytes; i++, d++)
70 		*d = inb(EC_LPC_ADDR_HOST_PACKET + i);
71 
72 	return in_bytes;
73 }
74 
75 int cros_ec_lpc_command(struct udevice *udev, uint8_t cmd, int cmd_version,
76 		     const uint8_t *dout, int dout_len,
77 		     uint8_t **dinp, int din_len)
78 {
79 	struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
80 	const int cmd_addr = EC_LPC_ADDR_HOST_CMD;
81 	const int data_addr = EC_LPC_ADDR_HOST_DATA;
82 	const int args_addr = EC_LPC_ADDR_HOST_ARGS;
83 	const int param_addr = EC_LPC_ADDR_HOST_PARAM;
84 
85 	struct ec_lpc_host_args args;
86 	uint8_t *d;
87 	int csum;
88 	int i;
89 
90 	if (dout_len > EC_PROTO2_MAX_PARAM_SIZE) {
91 		debug("%s: Cannot send %d bytes\n", __func__, dout_len);
92 		return -1;
93 	}
94 
95 	/* Fill in args */
96 	args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
97 	args.command_version = cmd_version;
98 	args.data_size = dout_len;
99 
100 	/* Calculate checksum */
101 	csum = cmd + args.flags + args.command_version + args.data_size;
102 	for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++)
103 		csum += *d;
104 
105 	args.checksum = (uint8_t)csum;
106 
107 	if (wait_for_sync(dev)) {
108 		debug("%s: Timeout waiting ready\n", __func__);
109 		return -1;
110 	}
111 
112 	/* Write args */
113 	for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
114 		outb(*d, args_addr + i);
115 
116 	/* Write data, if any */
117 	debug_trace("cmd: %02x, ver: %02x", cmd, cmd_version);
118 	for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++) {
119 		outb(*d, param_addr + i);
120 		debug_trace("%02x ", *d);
121 	}
122 
123 	outb(cmd, cmd_addr);
124 	debug_trace("\n");
125 
126 	if (wait_for_sync(dev)) {
127 		debug("%s: Timeout waiting for response\n", __func__);
128 		return -1;
129 	}
130 
131 	/* Check result */
132 	i = inb(data_addr);
133 	if (i) {
134 		debug("%s: CROS_EC result code %d\n", __func__, i);
135 		return -i;
136 	}
137 
138 	/* Read back args */
139 	for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
140 		*d = inb(args_addr + i);
141 
142 	/*
143 	 * If EC didn't modify args flags, then somehow we sent a new-style
144 	 * command to an old EC, which means it would have read its params
145 	 * from the wrong place.
146 	 */
147 	if (!(args.flags & EC_HOST_ARGS_FLAG_TO_HOST)) {
148 		debug("%s: CROS_EC protocol mismatch\n", __func__);
149 		return -EC_RES_INVALID_RESPONSE;
150 	}
151 
152 	if (args.data_size > din_len) {
153 		debug("%s: CROS_EC returned too much data %d > %d\n",
154 		      __func__, args.data_size, din_len);
155 		return -EC_RES_INVALID_RESPONSE;
156 	}
157 
158 	/* Read data, if any */
159 	for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++) {
160 		*d = inb(param_addr + i);
161 		debug_trace("%02x ", *d);
162 	}
163 	debug_trace("\n");
164 
165 	/* Verify checksum */
166 	csum = cmd + args.flags + args.command_version + args.data_size;
167 	for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++)
168 		csum += *d;
169 
170 	if (args.checksum != (uint8_t)csum) {
171 		debug("%s: CROS_EC response has invalid checksum\n", __func__);
172 		return -EC_RES_INVALID_CHECKSUM;
173 	}
174 	*dinp = dev->din;
175 
176 	/* Return actual amount of data received */
177 	return args.data_size;
178 }
179 
180 /**
181  * Initialize LPC protocol.
182  *
183  * @param dev		CROS_EC device
184  * @param blob		Device tree blob
185  * @return 0 if ok, -1 on error
186  */
187 int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)
188 {
189 	int byte, i;
190 
191 	/* See if we can find an EC at the other end */
192 	byte = 0xff;
193 	byte &= inb(EC_LPC_ADDR_HOST_CMD);
194 	byte &= inb(EC_LPC_ADDR_HOST_DATA);
195 	for (i = 0; i < EC_PROTO2_MAX_PARAM_SIZE && (byte == 0xff); i++)
196 		byte &= inb(EC_LPC_ADDR_HOST_PARAM + i);
197 	if (byte == 0xff) {
198 		debug("%s: CROS_EC device not found on LPC bus\n",
199 			__func__);
200 		return -1;
201 	}
202 
203 	return 0;
204 }
205 
206 /*
207  * Test if LPC command args are supported.
208  *
209  * The cheapest way to do this is by looking for the memory-mapped
210  * flag.  This is faster than sending a new-style 'hello' command and
211  * seeing whether the EC sets the EC_HOST_ARGS_FLAG_FROM_HOST flag
212  * in args when it responds.
213  */
214 static int cros_ec_lpc_check_version(struct udevice *dev)
215 {
216 	if (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) == 'E' &&
217 			inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1)
218 				== 'C' &&
219 			(inb(EC_LPC_ADDR_MEMMAP +
220 				EC_MEMMAP_HOST_CMD_FLAGS) &
221 				EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED)) {
222 		return 0;
223 	}
224 
225 	printf("%s: ERROR: old EC interface not supported\n", __func__);
226 	return -1;
227 }
228 
229 static int cros_ec_probe(struct udevice *dev)
230 {
231 	return cros_ec_register(dev);
232 }
233 
234 static struct dm_cros_ec_ops cros_ec_ops = {
235 	.packet = cros_ec_lpc_packet,
236 	.command = cros_ec_lpc_command,
237 	.check_version = cros_ec_lpc_check_version,
238 };
239 
240 static const struct udevice_id cros_ec_ids[] = {
241 	{ .compatible = "google,cros-ec-lpc" },
242 	{ }
243 };
244 
245 U_BOOT_DRIVER(cros_ec_lpc) = {
246 	.name		= "cros_ec_lpc",
247 	.id		= UCLASS_CROS_EC,
248 	.of_match	= cros_ec_ids,
249 	.probe		= cros_ec_probe,
250 	.ops		= &cros_ec_ops,
251 };
252