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
22 #define debug_trace(fmt, b...) debug(fmt, ##b)
24 #define debug_trace(fmt, b...)
27 static int wait_for_sync(struct cros_ec_dev *dev)
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",
43 int cros_ec_lpc_packet(struct udevice *udev, int out_bytes, int in_bytes)
45 struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
49 if (out_bytes > EC_LPC_HOST_PACKET_SIZE)
50 return log_msg_ret("Cannot send that many bytes\n", -E2BIG);
52 if (in_bytes > EC_LPC_HOST_PACKET_SIZE)
53 return log_msg_ret("Cannot receive that many bytes\n", -E2BIG);
55 if (wait_for_sync(dev))
56 return log_msg_ret("Timeout waiting ready\n", -ETIMEDOUT);
59 for (i = 0, d = (uint8_t *)dev->dout; i < out_bytes; i++, d++)
60 outb(*d, EC_LPC_ADDR_HOST_PACKET + i);
62 /* Start the command */
63 outb(EC_COMMAND_PROTOCOL_3, EC_LPC_ADDR_HOST_CMD);
65 if (wait_for_sync(dev))
66 return log_msg_ret("Timeout waiting ready\n", -ETIMEDOUT);
69 for (i = 0, d = dev->din; i < in_bytes; i++, d++)
70 *d = inb(EC_LPC_ADDR_HOST_PACKET + i);
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)
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;
85 struct ec_lpc_host_args args;
90 if (dout_len > EC_PROTO2_MAX_PARAM_SIZE) {
91 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
96 args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
97 args.command_version = cmd_version;
98 args.data_size = dout_len;
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++)
105 args.checksum = (uint8_t)csum;
107 if (wait_for_sync(dev)) {
108 debug("%s: Timeout waiting ready\n", __func__);
113 for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
114 outb(*d, args_addr + i);
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);
126 if (wait_for_sync(dev)) {
127 debug("%s: Timeout waiting for response\n", __func__);
134 debug("%s: CROS_EC result code %d\n", __func__, i);
139 for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
140 *d = inb(args_addr + i);
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.
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;
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;
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);
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++)
170 if (args.checksum != (uint8_t)csum) {
171 debug("%s: CROS_EC response has invalid checksum\n", __func__);
172 return -EC_RES_INVALID_CHECKSUM;
176 /* Return actual amount of data received */
177 return args.data_size;
181 * Initialize LPC protocol.
183 * @param dev CROS_EC device
184 * @param blob Device tree blob
185 * @return 0 if ok, -1 on error
187 int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)
191 /* See if we can find an EC at the other end */
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);
198 debug("%s: CROS_EC device not found on LPC bus\n",
207 * Test if LPC command args are supported.
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.
214 static int cros_ec_lpc_check_version(struct udevice *dev)
216 if (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) == 'E' &&
217 inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1)
219 (inb(EC_LPC_ADDR_MEMMAP +
220 EC_MEMMAP_HOST_CMD_FLAGS) &
221 EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED)) {
225 printf("%s: ERROR: old EC interface not supported\n", __func__);
229 static int cros_ec_probe(struct udevice *dev)
231 return cros_ec_register(dev);
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,
240 static const struct udevice_id cros_ec_ids[] = {
241 { .compatible = "google,cros-ec-lpc" },
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,