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 int cros_ec_lpc_command(struct udevice
*udev
, uint8_t cmd
, int cmd_version
,
45 const uint8_t *dout
, int dout_len
,
46 uint8_t **dinp
, int din_len
)
48 struct cros_ec_dev
*dev
= dev_get_uclass_priv(udev
);
49 const int cmd_addr
= EC_LPC_ADDR_HOST_CMD
;
50 const int data_addr
= EC_LPC_ADDR_HOST_DATA
;
51 const int args_addr
= EC_LPC_ADDR_HOST_ARGS
;
52 const int param_addr
= EC_LPC_ADDR_HOST_PARAM
;
54 struct ec_lpc_host_args args
;
59 if (dout_len
> EC_PROTO2_MAX_PARAM_SIZE
) {
60 debug("%s: Cannot send %d bytes\n", __func__
, dout_len
);
65 args
.flags
= EC_HOST_ARGS_FLAG_FROM_HOST
;
66 args
.command_version
= cmd_version
;
67 args
.data_size
= dout_len
;
69 /* Calculate checksum */
70 csum
= cmd
+ args
.flags
+ args
.command_version
+ args
.data_size
;
71 for (i
= 0, d
= (uint8_t *)dout
; i
< dout_len
; i
++, d
++)
74 args
.checksum
= (uint8_t)csum
;
76 if (wait_for_sync(dev
)) {
77 debug("%s: Timeout waiting ready\n", __func__
);
82 for (i
= 0, d
= (uint8_t *)&args
; i
< sizeof(args
); i
++, d
++)
83 outb(*d
, args_addr
+ i
);
85 /* Write data, if any */
86 debug_trace("cmd: %02x, ver: %02x", cmd
, cmd_version
);
87 for (i
= 0, d
= (uint8_t *)dout
; i
< dout_len
; i
++, d
++) {
88 outb(*d
, param_addr
+ i
);
89 debug_trace("%02x ", *d
);
95 if (wait_for_sync(dev
)) {
96 debug("%s: Timeout waiting for response\n", __func__
);
103 debug("%s: CROS_EC result code %d\n", __func__
, i
);
108 for (i
= 0, d
= (uint8_t *)&args
; i
< sizeof(args
); i
++, d
++)
109 *d
= inb(args_addr
+ i
);
112 * If EC didn't modify args flags, then somehow we sent a new-style
113 * command to an old EC, which means it would have read its params
114 * from the wrong place.
116 if (!(args
.flags
& EC_HOST_ARGS_FLAG_TO_HOST
)) {
117 debug("%s: CROS_EC protocol mismatch\n", __func__
);
118 return -EC_RES_INVALID_RESPONSE
;
121 if (args
.data_size
> din_len
) {
122 debug("%s: CROS_EC returned too much data %d > %d\n",
123 __func__
, args
.data_size
, din_len
);
124 return -EC_RES_INVALID_RESPONSE
;
127 /* Read data, if any */
128 for (i
= 0, d
= (uint8_t *)dev
->din
; i
< args
.data_size
; i
++, d
++) {
129 *d
= inb(param_addr
+ i
);
130 debug_trace("%02x ", *d
);
134 /* Verify checksum */
135 csum
= cmd
+ args
.flags
+ args
.command_version
+ args
.data_size
;
136 for (i
= 0, d
= (uint8_t *)dev
->din
; i
< args
.data_size
; i
++, d
++)
139 if (args
.checksum
!= (uint8_t)csum
) {
140 debug("%s: CROS_EC response has invalid checksum\n", __func__
);
141 return -EC_RES_INVALID_CHECKSUM
;
145 /* Return actual amount of data received */
146 return args
.data_size
;
150 * Initialize LPC protocol.
152 * @param dev CROS_EC device
153 * @param blob Device tree blob
154 * @return 0 if ok, -1 on error
156 int cros_ec_lpc_init(struct cros_ec_dev
*dev
, const void *blob
)
160 /* See if we can find an EC at the other end */
162 byte
&= inb(EC_LPC_ADDR_HOST_CMD
);
163 byte
&= inb(EC_LPC_ADDR_HOST_DATA
);
164 for (i
= 0; i
< EC_PROTO2_MAX_PARAM_SIZE
&& (byte
== 0xff); i
++)
165 byte
&= inb(EC_LPC_ADDR_HOST_PARAM
+ i
);
167 debug("%s: CROS_EC device not found on LPC bus\n",
176 * Test if LPC command args are supported.
178 * The cheapest way to do this is by looking for the memory-mapped
179 * flag. This is faster than sending a new-style 'hello' command and
180 * seeing whether the EC sets the EC_HOST_ARGS_FLAG_FROM_HOST flag
181 * in args when it responds.
183 static int cros_ec_lpc_check_version(struct udevice
*dev
)
185 if (inb(EC_LPC_ADDR_MEMMAP
+ EC_MEMMAP_ID
) == 'E' &&
186 inb(EC_LPC_ADDR_MEMMAP
+ EC_MEMMAP_ID
+ 1)
188 (inb(EC_LPC_ADDR_MEMMAP
+
189 EC_MEMMAP_HOST_CMD_FLAGS
) &
190 EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED
)) {
194 printf("%s: ERROR: old EC interface not supported\n", __func__
);
198 static int cros_ec_probe(struct udevice
*dev
)
200 return cros_ec_register(dev
);
203 static struct dm_cros_ec_ops cros_ec_ops
= {
204 .command
= cros_ec_lpc_command
,
205 .check_version
= cros_ec_lpc_check_version
,
208 static const struct udevice_id cros_ec_ids
[] = {
209 { .compatible
= "google,cros-ec-lpc" },
213 U_BOOT_DRIVER(cros_ec_lpc
) = {
214 .name
= "cros_ec_lpc",
215 .id
= UCLASS_CROS_EC
,
216 .of_match
= cros_ec_ids
,
217 .probe
= cros_ec_probe
,