xref: /openbmc/u-boot/drivers/misc/cros_ec_lpc.c (revision 282ce645)
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_command(struct udevice *udev, uint8_t cmd, int cmd_version,
44 		     const uint8_t *dout, int dout_len,
45 		     uint8_t **dinp, int din_len)
46 {
47 	struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
48 	const int cmd_addr = EC_LPC_ADDR_HOST_CMD;
49 	const int data_addr = EC_LPC_ADDR_HOST_DATA;
50 	const int args_addr = EC_LPC_ADDR_HOST_ARGS;
51 	const int param_addr = EC_LPC_ADDR_HOST_PARAM;
52 
53 	struct ec_lpc_host_args args;
54 	uint8_t *d;
55 	int csum;
56 	int i;
57 
58 	if (dout_len > EC_PROTO2_MAX_PARAM_SIZE) {
59 		debug("%s: Cannot send %d bytes\n", __func__, dout_len);
60 		return -1;
61 	}
62 
63 	/* Fill in args */
64 	args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
65 	args.command_version = cmd_version;
66 	args.data_size = dout_len;
67 
68 	/* Calculate checksum */
69 	csum = cmd + args.flags + args.command_version + args.data_size;
70 	for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++)
71 		csum += *d;
72 
73 	args.checksum = (uint8_t)csum;
74 
75 	if (wait_for_sync(dev)) {
76 		debug("%s: Timeout waiting ready\n", __func__);
77 		return -1;
78 	}
79 
80 	/* Write args */
81 	for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
82 		outb(*d, args_addr + i);
83 
84 	/* Write data, if any */
85 	debug_trace("cmd: %02x, ver: %02x", cmd, cmd_version);
86 	for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++) {
87 		outb(*d, param_addr + i);
88 		debug_trace("%02x ", *d);
89 	}
90 
91 	outb(cmd, cmd_addr);
92 	debug_trace("\n");
93 
94 	if (wait_for_sync(dev)) {
95 		debug("%s: Timeout waiting for response\n", __func__);
96 		return -1;
97 	}
98 
99 	/* Check result */
100 	i = inb(data_addr);
101 	if (i) {
102 		debug("%s: CROS_EC result code %d\n", __func__, i);
103 		return -i;
104 	}
105 
106 	/* Read back args */
107 	for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
108 		*d = inb(args_addr + i);
109 
110 	/*
111 	 * If EC didn't modify args flags, then somehow we sent a new-style
112 	 * command to an old EC, which means it would have read its params
113 	 * from the wrong place.
114 	 */
115 	if (!(args.flags & EC_HOST_ARGS_FLAG_TO_HOST)) {
116 		debug("%s: CROS_EC protocol mismatch\n", __func__);
117 		return -EC_RES_INVALID_RESPONSE;
118 	}
119 
120 	if (args.data_size > din_len) {
121 		debug("%s: CROS_EC returned too much data %d > %d\n",
122 		      __func__, args.data_size, din_len);
123 		return -EC_RES_INVALID_RESPONSE;
124 	}
125 
126 	/* Read data, if any */
127 	for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++) {
128 		*d = inb(param_addr + i);
129 		debug_trace("%02x ", *d);
130 	}
131 	debug_trace("\n");
132 
133 	/* Verify checksum */
134 	csum = cmd + args.flags + args.command_version + args.data_size;
135 	for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++)
136 		csum += *d;
137 
138 	if (args.checksum != (uint8_t)csum) {
139 		debug("%s: CROS_EC response has invalid checksum\n", __func__);
140 		return -EC_RES_INVALID_CHECKSUM;
141 	}
142 	*dinp = dev->din;
143 
144 	/* Return actual amount of data received */
145 	return args.data_size;
146 }
147 
148 /**
149  * Initialize LPC protocol.
150  *
151  * @param dev		CROS_EC device
152  * @param blob		Device tree blob
153  * @return 0 if ok, -1 on error
154  */
155 int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)
156 {
157 	int byte, i;
158 
159 	/* See if we can find an EC at the other end */
160 	byte = 0xff;
161 	byte &= inb(EC_LPC_ADDR_HOST_CMD);
162 	byte &= inb(EC_LPC_ADDR_HOST_DATA);
163 	for (i = 0; i < EC_PROTO2_MAX_PARAM_SIZE && (byte == 0xff); i++)
164 		byte &= inb(EC_LPC_ADDR_HOST_PARAM + i);
165 	if (byte == 0xff) {
166 		debug("%s: CROS_EC device not found on LPC bus\n",
167 			__func__);
168 		return -1;
169 	}
170 
171 	return 0;
172 }
173 
174 /*
175  * Test if LPC command args are supported.
176  *
177  * The cheapest way to do this is by looking for the memory-mapped
178  * flag.  This is faster than sending a new-style 'hello' command and
179  * seeing whether the EC sets the EC_HOST_ARGS_FLAG_FROM_HOST flag
180  * in args when it responds.
181  */
182 static int cros_ec_lpc_check_version(struct udevice *dev)
183 {
184 	if (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) == 'E' &&
185 			inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1)
186 				== 'C' &&
187 			(inb(EC_LPC_ADDR_MEMMAP +
188 				EC_MEMMAP_HOST_CMD_FLAGS) &
189 				EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED)) {
190 		return 0;
191 	}
192 
193 	printf("%s: ERROR: old EC interface not supported\n", __func__);
194 	return -1;
195 }
196 
197 static int cros_ec_probe(struct udevice *dev)
198 {
199 	return cros_ec_register(dev);
200 }
201 
202 static struct dm_cros_ec_ops cros_ec_ops = {
203 	.command = cros_ec_lpc_command,
204 	.check_version = cros_ec_lpc_check_version,
205 };
206 
207 static const struct udevice_id cros_ec_ids[] = {
208 	{ .compatible = "google,cros-ec-lpc" },
209 	{ }
210 };
211 
212 U_BOOT_DRIVER(cros_ec_lpc) = {
213 	.name		= "cros_ec_lpc",
214 	.id		= UCLASS_CROS_EC,
215 	.of_match	= cros_ec_ids,
216 	.probe		= cros_ec_probe,
217 	.ops		= &cros_ec_ops,
218 };
219