2 * Chromium OS cros_ec driver - LPC interface
4 * Copyright (c) 2012 The Chromium OS Authors.
6 * SPDX-License-Identifier: GPL-2.0+
10 * The Matrix Keyboard Protocol driver handles talking to the keyboard
11 * controller chip. Mostly this is for keyboard functions, but some other
12 * 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 static int wait_for_sync(struct cros_ec_dev *dev)
33 while (inb(EC_LPC_ADDR_HOST_CMD) & EC_LPC_STATUS_BUSY_MASK) {
34 if (get_timer(start) > 1000) {
35 debug("%s: Timeout waiting for CROS_EC sync\n",
44 #ifdef CONFIG_DM_CROS_EC
45 int cros_ec_lpc_command(struct udevice *udev, uint8_t cmd, int cmd_version,
46 const uint8_t *dout, int dout_len,
47 uint8_t **dinp, int din_len)
49 struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
51 int cros_ec_lpc_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
52 const uint8_t *dout, int dout_len,
53 uint8_t **dinp, int din_len)
56 const int cmd_addr = EC_LPC_ADDR_HOST_CMD;
57 const int data_addr = EC_LPC_ADDR_HOST_DATA;
58 const int args_addr = EC_LPC_ADDR_HOST_ARGS;
59 const int param_addr = EC_LPC_ADDR_HOST_PARAM;
61 struct ec_lpc_host_args args;
66 if (dout_len > EC_PROTO2_MAX_PARAM_SIZE) {
67 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
72 args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
73 args.command_version = cmd_version;
74 args.data_size = dout_len;
76 /* Calculate checksum */
77 csum = cmd + args.flags + args.command_version + args.data_size;
78 for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++)
81 args.checksum = (uint8_t)csum;
83 if (wait_for_sync(dev)) {
84 debug("%s: Timeout waiting ready\n", __func__);
89 for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
90 outb(*d, args_addr + i);
92 /* Write data, if any */
93 debug_trace("cmd: %02x, ver: %02x", cmd, cmd_version);
94 for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++) {
95 outb(*d, param_addr + i);
96 debug_trace("%02x ", *d);
102 if (wait_for_sync(dev)) {
103 debug("%s: Timeout waiting for response\n", __func__);
110 debug("%s: CROS_EC result code %d\n", __func__, i);
115 for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
116 *d = inb(args_addr + i);
119 * If EC didn't modify args flags, then somehow we sent a new-style
120 * command to an old EC, which means it would have read its params
121 * from the wrong place.
123 if (!(args.flags & EC_HOST_ARGS_FLAG_TO_HOST)) {
124 debug("%s: CROS_EC protocol mismatch\n", __func__);
125 return -EC_RES_INVALID_RESPONSE;
128 if (args.data_size > din_len) {
129 debug("%s: CROS_EC returned too much data %d > %d\n",
130 __func__, args.data_size, din_len);
131 return -EC_RES_INVALID_RESPONSE;
134 /* Read data, if any */
135 for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++) {
136 *d = inb(param_addr + i);
137 debug_trace("%02x ", *d);
141 /* Verify checksum */
142 csum = cmd + args.flags + args.command_version + args.data_size;
143 for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++)
146 if (args.checksum != (uint8_t)csum) {
147 debug("%s: CROS_EC response has invalid checksum\n", __func__);
148 return -EC_RES_INVALID_CHECKSUM;
152 /* Return actual amount of data received */
153 return args.data_size;
157 * Initialize LPC protocol.
159 * @param dev CROS_EC device
160 * @param blob Device tree blob
161 * @return 0 if ok, -1 on error
163 int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)
167 /* See if we can find an EC at the other end */
169 byte &= inb(EC_LPC_ADDR_HOST_CMD);
170 byte &= inb(EC_LPC_ADDR_HOST_DATA);
171 for (i = 0; i < EC_PROTO2_MAX_PARAM_SIZE && (byte == 0xff); i++)
172 byte &= inb(EC_LPC_ADDR_HOST_PARAM + i);
174 debug("%s: CROS_EC device not found on LPC bus\n",
183 * Test if LPC command args are supported.
185 * The cheapest way to do this is by looking for the memory-mapped
186 * flag. This is faster than sending a new-style 'hello' command and
187 * seeing whether the EC sets the EC_HOST_ARGS_FLAG_FROM_HOST flag
188 * in args when it responds.
190 #ifdef CONFIG_DM_CROS_EC
191 static int cros_ec_lpc_check_version(struct udevice *dev)
193 int cros_ec_lpc_check_version(struct cros_ec_dev *dev)
196 if (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) == 'E' &&
197 inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1)
199 (inb(EC_LPC_ADDR_MEMMAP +
200 EC_MEMMAP_HOST_CMD_FLAGS) &
201 EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED)) {
205 printf("%s: ERROR: old EC interface not supported\n", __func__);
209 #ifdef CONFIG_DM_CROS_EC
210 static int cros_ec_probe(struct udevice *dev)
212 return cros_ec_register(dev);
215 static struct dm_cros_ec_ops cros_ec_ops = {
216 .command = cros_ec_lpc_command,
217 .check_version = cros_ec_lpc_check_version,
220 static const struct udevice_id cros_ec_ids[] = {
221 { .compatible = "google,cros-ec" },
225 U_BOOT_DRIVER(cros_ec_lpc) = {
227 .id = UCLASS_CROS_EC,
228 .of_match = cros_ec_ids,
229 .probe = cros_ec_probe,