1 // SPDX-License-Identifier: GPL-2.0+
3 * Chromium OS cros_ec driver - LPC interface
5 * Copyright (c) 2012 The Chromium OS Authors.
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
23 #define debug_trace(fmt, b...) debug(fmt, ##b)
25 #define debug_trace(fmt, b...)
28 /* Timeout waiting for a flash erase command to complete */
29 static const int CROS_EC_CMD_TIMEOUT_MS = 5000;
31 static int wait_for_sync(struct cros_ec_dev *dev)
36 while (inb(EC_LPC_ADDR_HOST_CMD) & EC_LPC_STATUS_BUSY_MASK) {
37 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
38 debug("%s: Timeout waiting for CROS_EC sync\n",
47 int cros_ec_lpc_packet(struct udevice *udev, int out_bytes, int in_bytes)
49 struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
53 if (out_bytes > EC_LPC_HOST_PACKET_SIZE)
54 return log_msg_ret("Cannot send that many bytes\n", -E2BIG);
56 if (in_bytes > EC_LPC_HOST_PACKET_SIZE)
57 return log_msg_ret("Cannot receive that many bytes\n", -E2BIG);
59 if (wait_for_sync(dev))
60 return log_msg_ret("Timeout waiting ready\n", -ETIMEDOUT);
63 for (i = 0, d = (uint8_t *)dev->dout; i < out_bytes; i++, d++)
64 outb(*d, EC_LPC_ADDR_HOST_PACKET + i);
66 /* Start the command */
67 outb(EC_COMMAND_PROTOCOL_3, EC_LPC_ADDR_HOST_CMD);
69 if (wait_for_sync(dev))
70 return log_msg_ret("Timeout waiting ready\n", -ETIMEDOUT);
73 for (i = 0, d = dev->din; i < in_bytes; i++, d++)
74 *d = inb(EC_LPC_ADDR_HOST_PACKET + i);
79 int cros_ec_lpc_command(struct udevice *udev, uint8_t cmd, int cmd_version,
80 const uint8_t *dout, int dout_len,
81 uint8_t **dinp, int din_len)
83 struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
84 const int cmd_addr = EC_LPC_ADDR_HOST_CMD;
85 const int data_addr = EC_LPC_ADDR_HOST_DATA;
86 const int args_addr = EC_LPC_ADDR_HOST_ARGS;
87 const int param_addr = EC_LPC_ADDR_HOST_PARAM;
89 struct ec_lpc_host_args args;
94 if (dout_len > EC_PROTO2_MAX_PARAM_SIZE) {
95 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
100 args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
101 args.command_version = cmd_version;
102 args.data_size = dout_len;
104 /* Calculate checksum */
105 csum = cmd + args.flags + args.command_version + args.data_size;
106 for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++)
109 args.checksum = (uint8_t)csum;
111 if (wait_for_sync(dev)) {
112 debug("%s: Timeout waiting ready\n", __func__);
117 for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
118 outb(*d, args_addr + i);
120 /* Write data, if any */
121 debug_trace("cmd: %02x, ver: %02x", cmd, cmd_version);
122 for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++) {
123 outb(*d, param_addr + i);
124 debug_trace("%02x ", *d);
130 if (wait_for_sync(dev)) {
131 debug("%s: Timeout waiting for response\n", __func__);
138 debug("%s: CROS_EC result code %d\n", __func__, i);
143 for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
144 *d = inb(args_addr + i);
147 * If EC didn't modify args flags, then somehow we sent a new-style
148 * command to an old EC, which means it would have read its params
149 * from the wrong place.
151 if (!(args.flags & EC_HOST_ARGS_FLAG_TO_HOST)) {
152 debug("%s: CROS_EC protocol mismatch\n", __func__);
153 return -EC_RES_INVALID_RESPONSE;
156 if (args.data_size > din_len) {
157 debug("%s: CROS_EC returned too much data %d > %d\n",
158 __func__, args.data_size, din_len);
159 return -EC_RES_INVALID_RESPONSE;
162 /* Read data, if any */
163 for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++) {
164 *d = inb(param_addr + i);
165 debug_trace("%02x ", *d);
169 /* Verify checksum */
170 csum = cmd + args.flags + args.command_version + args.data_size;
171 for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++)
174 if (args.checksum != (uint8_t)csum) {
175 debug("%s: CROS_EC response has invalid checksum\n", __func__);
176 return -EC_RES_INVALID_CHECKSUM;
180 /* Return actual amount of data received */
181 return args.data_size;
185 * Initialize LPC protocol.
187 * @param dev CROS_EC device
188 * @param blob Device tree blob
189 * Return: 0 if ok, -1 on error
191 int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)
195 /* See if we can find an EC at the other end */
197 byte &= inb(EC_LPC_ADDR_HOST_CMD);
198 byte &= inb(EC_LPC_ADDR_HOST_DATA);
199 for (i = 0; i < EC_PROTO2_MAX_PARAM_SIZE && (byte == 0xff); i++)
200 byte &= inb(EC_LPC_ADDR_HOST_PARAM + i);
202 debug("%s: CROS_EC device not found on LPC bus\n",
210 /* Return the byte of EC switch states */
211 static int cros_ec_lpc_get_switches(struct udevice *dev)
213 return inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SWITCHES);
217 * Test if LPC command args are supported.
219 * The cheapest way to do this is by looking for the memory-mapped
220 * flag. This is faster than sending a new-style 'hello' command and
221 * seeing whether the EC sets the EC_HOST_ARGS_FLAG_FROM_HOST flag
222 * in args when it responds.
224 static int cros_ec_lpc_check_version(struct udevice *dev)
226 if (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) == 'E' &&
227 inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1)
229 (inb(EC_LPC_ADDR_MEMMAP +
230 EC_MEMMAP_HOST_CMD_FLAGS) &
231 EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED)) {
235 printf("%s: ERROR: old EC interface not supported\n", __func__);
239 static int cros_ec_probe(struct udevice *dev)
241 return cros_ec_register(dev);
244 static struct dm_cros_ec_ops cros_ec_ops = {
245 .packet = cros_ec_lpc_packet,
246 .command = cros_ec_lpc_command,
247 .check_version = cros_ec_lpc_check_version,
248 .get_switches = cros_ec_lpc_get_switches,
251 static const struct udevice_id cros_ec_ids[] = {
252 { .compatible = "google,cros-ec-lpc" },
256 U_BOOT_DRIVER(google_cros_ec_lpc) = {
257 .name = "google_cros_ec_lpc",
258 .id = UCLASS_CROS_EC,
259 .of_match = cros_ec_ids,
260 .probe = cros_ec_probe,