blob: 1a8a81349c3a520489ff17ce3695cd2b2165ee4c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hung-ying Tyan62899fc2013-05-15 18:27:31 +08002/*
3 * Chromium OS cros_ec driver - LPC interface
4 *
5 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyan62899fc2013-05-15 18:27:31 +08006 */
7
8/*
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
12 * KBC.
13 */
14
Tom Riniabb9a042024-05-18 20:20:43 -060015#include <common.h>
Simon Glass23fb1562015-03-26 09:29:30 -060016#include <dm.h>
Hung-ying Tyan62899fc2013-05-15 18:27:31 +080017#include <command.h>
18#include <cros_ec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
Hung-ying Tyan62899fc2013-05-15 18:27:31 +080020#include <asm/io.h>
21
22#ifdef DEBUG_TRACE
23#define debug_trace(fmt, b...) debug(fmt, ##b)
24#else
25#define debug_trace(fmt, b...)
26#endif
27
Simon Glassea5def62020-11-09 07:14:44 -070028/* Timeout waiting for a flash erase command to complete */
29static const int CROS_EC_CMD_TIMEOUT_MS = 5000;
30
Hung-ying Tyan62899fc2013-05-15 18:27:31 +080031static int wait_for_sync(struct cros_ec_dev *dev)
32{
33 unsigned long start;
34
35 start = get_timer(0);
36 while (inb(EC_LPC_ADDR_HOST_CMD) & EC_LPC_STATUS_BUSY_MASK) {
Simon Glassea5def62020-11-09 07:14:44 -070037 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
Hung-ying Tyan62899fc2013-05-15 18:27:31 +080038 debug("%s: Timeout waiting for CROS_EC sync\n",
39 __func__);
40 return -1;
41 }
42 }
43
44 return 0;
45}
46
Simon Glass4ed88a52018-10-01 12:22:39 -060047int cros_ec_lpc_packet(struct udevice *udev, int out_bytes, int in_bytes)
48{
49 struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
50 uint8_t *d;
51 int i;
52
53 if (out_bytes > EC_LPC_HOST_PACKET_SIZE)
54 return log_msg_ret("Cannot send that many bytes\n", -E2BIG);
55
56 if (in_bytes > EC_LPC_HOST_PACKET_SIZE)
57 return log_msg_ret("Cannot receive that many bytes\n", -E2BIG);
58
59 if (wait_for_sync(dev))
60 return log_msg_ret("Timeout waiting ready\n", -ETIMEDOUT);
61
62 /* Write data */
63 for (i = 0, d = (uint8_t *)dev->dout; i < out_bytes; i++, d++)
64 outb(*d, EC_LPC_ADDR_HOST_PACKET + i);
65
66 /* Start the command */
67 outb(EC_COMMAND_PROTOCOL_3, EC_LPC_ADDR_HOST_CMD);
68
69 if (wait_for_sync(dev))
70 return log_msg_ret("Timeout waiting ready\n", -ETIMEDOUT);
71
72 /* Read back args */
73 for (i = 0, d = dev->din; i < in_bytes; i++, d++)
74 *d = inb(EC_LPC_ADDR_HOST_PACKET + i);
75
76 return in_bytes;
77}
78
Simon Glass23fb1562015-03-26 09:29:30 -060079int 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)
82{
83 struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
Hung-ying Tyan62899fc2013-05-15 18:27:31 +080084 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;
88
89 struct ec_lpc_host_args args;
90 uint8_t *d;
91 int csum;
92 int i;
93
Simon Glassa1a34302014-10-10 07:30:15 -060094 if (dout_len > EC_PROTO2_MAX_PARAM_SIZE) {
Hung-ying Tyan62899fc2013-05-15 18:27:31 +080095 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
96 return -1;
97 }
98
99 /* Fill in args */
100 args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
101 args.command_version = cmd_version;
102 args.data_size = dout_len;
103
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++)
107 csum += *d;
108
109 args.checksum = (uint8_t)csum;
110
111 if (wait_for_sync(dev)) {
112 debug("%s: Timeout waiting ready\n", __func__);
113 return -1;
114 }
115
116 /* Write args */
117 for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
118 outb(*d, args_addr + i);
119
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);
125 }
126
127 outb(cmd, cmd_addr);
128 debug_trace("\n");
129
130 if (wait_for_sync(dev)) {
131 debug("%s: Timeout waiting for response\n", __func__);
132 return -1;
133 }
134
135 /* Check result */
136 i = inb(data_addr);
137 if (i) {
138 debug("%s: CROS_EC result code %d\n", __func__, i);
139 return -i;
140 }
141
142 /* Read back args */
143 for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
144 *d = inb(args_addr + i);
145
146 /*
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.
150 */
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;
154 }
155
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;
160 }
161
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);
166 }
167 debug_trace("\n");
168
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++)
172 csum += *d;
173
174 if (args.checksum != (uint8_t)csum) {
175 debug("%s: CROS_EC response has invalid checksum\n", __func__);
176 return -EC_RES_INVALID_CHECKSUM;
177 }
178 *dinp = dev->din;
179
180 /* Return actual amount of data received */
181 return args.data_size;
182}
183
184/**
185 * Initialize LPC protocol.
186 *
187 * @param dev CROS_EC device
188 * @param blob Device tree blob
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100189 * Return: 0 if ok, -1 on error
Hung-ying Tyan62899fc2013-05-15 18:27:31 +0800190 */
191int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)
192{
193 int byte, i;
194
195 /* See if we can find an EC at the other end */
196 byte = 0xff;
197 byte &= inb(EC_LPC_ADDR_HOST_CMD);
198 byte &= inb(EC_LPC_ADDR_HOST_DATA);
Simon Glassa1a34302014-10-10 07:30:15 -0600199 for (i = 0; i < EC_PROTO2_MAX_PARAM_SIZE && (byte == 0xff); i++)
Hung-ying Tyan62899fc2013-05-15 18:27:31 +0800200 byte &= inb(EC_LPC_ADDR_HOST_PARAM + i);
201 if (byte == 0xff) {
202 debug("%s: CROS_EC device not found on LPC bus\n",
203 __func__);
204 return -1;
205 }
206
207 return 0;
208}
209
Simon Glass9d702522021-01-16 14:52:28 -0700210/* Return the byte of EC switch states */
211static int cros_ec_lpc_get_switches(struct udevice *dev)
212{
213 return inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SWITCHES);
214}
215
Hung-ying Tyan62899fc2013-05-15 18:27:31 +0800216/*
217 * Test if LPC command args are supported.
218 *
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.
223 */
Simon Glass23fb1562015-03-26 09:29:30 -0600224static int cros_ec_lpc_check_version(struct udevice *dev)
Hung-ying Tyan62899fc2013-05-15 18:27:31 +0800225{
226 if (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) == 'E' &&
227 inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1)
228 == 'C' &&
229 (inb(EC_LPC_ADDR_MEMMAP +
230 EC_MEMMAP_HOST_CMD_FLAGS) &
231 EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED)) {
Vadim Bendebury2f548812014-02-27 13:26:04 -0700232 return 0;
Hung-ying Tyan62899fc2013-05-15 18:27:31 +0800233 }
Hung-ying Tyan62899fc2013-05-15 18:27:31 +0800234
Vadim Bendebury2f548812014-02-27 13:26:04 -0700235 printf("%s: ERROR: old EC interface not supported\n", __func__);
236 return -1;
Hung-ying Tyan62899fc2013-05-15 18:27:31 +0800237}
Simon Glass23fb1562015-03-26 09:29:30 -0600238
Simon Glass23fb1562015-03-26 09:29:30 -0600239static int cros_ec_probe(struct udevice *dev)
240{
241 return cros_ec_register(dev);
242}
243
244static struct dm_cros_ec_ops cros_ec_ops = {
Simon Glass4ed88a52018-10-01 12:22:39 -0600245 .packet = cros_ec_lpc_packet,
Simon Glass23fb1562015-03-26 09:29:30 -0600246 .command = cros_ec_lpc_command,
247 .check_version = cros_ec_lpc_check_version,
Simon Glass9d702522021-01-16 14:52:28 -0700248 .get_switches = cros_ec_lpc_get_switches,
Simon Glass23fb1562015-03-26 09:29:30 -0600249};
250
251static const struct udevice_id cros_ec_ids[] = {
Simon Glass73f220c2015-03-26 09:29:39 -0600252 { .compatible = "google,cros-ec-lpc" },
Simon Glass23fb1562015-03-26 09:29:30 -0600253 { }
254};
255
Simon Glassdf2e9392020-10-05 05:27:00 -0600256U_BOOT_DRIVER(google_cros_ec_lpc) = {
257 .name = "google_cros_ec_lpc",
Simon Glass23fb1562015-03-26 09:29:30 -0600258 .id = UCLASS_CROS_EC,
259 .of_match = cros_ec_ids,
260 .probe = cros_ec_probe,
261 .ops = &cros_ec_ops,
262};