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_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)
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;
53 struct ec_lpc_host_args args;
58 if (dout_len > EC_PROTO2_MAX_PARAM_SIZE) {
59 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
64 args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
65 args.command_version = cmd_version;
66 args.data_size = dout_len;
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++)
73 args.checksum = (uint8_t)csum;
75 if (wait_for_sync(dev)) {
76 debug("%s: Timeout waiting ready\n", __func__);
81 for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
82 outb(*d, args_addr + i);
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);
94 if (wait_for_sync(dev)) {
95 debug("%s: Timeout waiting for response\n", __func__);
102 debug("%s: CROS_EC result code %d\n", __func__, i);
107 for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
108 *d = inb(args_addr + i);
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.
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;
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;
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);
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++)
138 if (args.checksum != (uint8_t)csum) {
139 debug("%s: CROS_EC response has invalid checksum\n", __func__);
140 return -EC_RES_INVALID_CHECKSUM;
144 /* Return actual amount of data received */
145 return args.data_size;
149 * Initialize LPC protocol.
151 * @param dev CROS_EC device
152 * @param blob Device tree blob
153 * @return 0 if ok, -1 on error
155 int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)
159 /* See if we can find an EC at the other end */
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);
166 debug("%s: CROS_EC device not found on LPC bus\n",
175 * Test if LPC command args are supported.
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.
182 static int cros_ec_lpc_check_version(struct udevice *dev)
184 if (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) == 'E' &&
185 inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1)
187 (inb(EC_LPC_ADDR_MEMMAP +
188 EC_MEMMAP_HOST_CMD_FLAGS) &
189 EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED)) {
193 printf("%s: ERROR: old EC interface not supported\n", __func__);
197 static int cros_ec_probe(struct udevice *dev)
199 return cros_ec_register(dev);
202 static struct dm_cros_ec_ops cros_ec_ops = {
203 .command = cros_ec_lpc_command,
204 .check_version = cros_ec_lpc_check_version,
207 static const struct udevice_id cros_ec_ids[] = {
208 { .compatible = "google,cros-ec-lpc" },
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,