blob: 4ad6c8ca66d22b0ed38590f510a6448182739f54 [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
15#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
28static int wait_for_sync(struct cros_ec_dev *dev)
29{
30 unsigned long start;
31
32 start = get_timer(0);
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",
36 __func__);
37 return -1;
38 }
39 }
40
41 return 0;
42}
43
Simon Glass4ed88a52018-10-01 12:22:39 -060044int cros_ec_lpc_packet(struct udevice *udev, int out_bytes, int in_bytes)
45{
46 struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
47 uint8_t *d;
48 int i;
49
50 if (out_bytes > EC_LPC_HOST_PACKET_SIZE)
51 return log_msg_ret("Cannot send that many bytes\n", -E2BIG);
52
53 if (in_bytes > EC_LPC_HOST_PACKET_SIZE)
54 return log_msg_ret("Cannot receive that many bytes\n", -E2BIG);
55
56 if (wait_for_sync(dev))
57 return log_msg_ret("Timeout waiting ready\n", -ETIMEDOUT);
58
59 /* Write data */
60 for (i = 0, d = (uint8_t *)dev->dout; i < out_bytes; i++, d++)
61 outb(*d, EC_LPC_ADDR_HOST_PACKET + i);
62
63 /* Start the command */
64 outb(EC_COMMAND_PROTOCOL_3, EC_LPC_ADDR_HOST_CMD);
65
66 if (wait_for_sync(dev))
67 return log_msg_ret("Timeout waiting ready\n", -ETIMEDOUT);
68
69 /* Read back args */
70 for (i = 0, d = dev->din; i < in_bytes; i++, d++)
71 *d = inb(EC_LPC_ADDR_HOST_PACKET + i);
72
73 return in_bytes;
74}
75
Simon Glass23fb1562015-03-26 09:29:30 -060076int cros_ec_lpc_command(struct udevice *udev, uint8_t cmd, int cmd_version,
77 const uint8_t *dout, int dout_len,
78 uint8_t **dinp, int din_len)
79{
80 struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
Hung-ying Tyan62899fc2013-05-15 18:27:31 +080081 const int cmd_addr = EC_LPC_ADDR_HOST_CMD;
82 const int data_addr = EC_LPC_ADDR_HOST_DATA;
83 const int args_addr = EC_LPC_ADDR_HOST_ARGS;
84 const int param_addr = EC_LPC_ADDR_HOST_PARAM;
85
86 struct ec_lpc_host_args args;
87 uint8_t *d;
88 int csum;
89 int i;
90
Simon Glassa1a34302014-10-10 07:30:15 -060091 if (dout_len > EC_PROTO2_MAX_PARAM_SIZE) {
Hung-ying Tyan62899fc2013-05-15 18:27:31 +080092 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
93 return -1;
94 }
95
96 /* Fill in args */
97 args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
98 args.command_version = cmd_version;
99 args.data_size = dout_len;
100
101 /* Calculate checksum */
102 csum = cmd + args.flags + args.command_version + args.data_size;
103 for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++)
104 csum += *d;
105
106 args.checksum = (uint8_t)csum;
107
108 if (wait_for_sync(dev)) {
109 debug("%s: Timeout waiting ready\n", __func__);
110 return -1;
111 }
112
113 /* Write args */
114 for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
115 outb(*d, args_addr + i);
116
117 /* Write data, if any */
118 debug_trace("cmd: %02x, ver: %02x", cmd, cmd_version);
119 for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++) {
120 outb(*d, param_addr + i);
121 debug_trace("%02x ", *d);
122 }
123
124 outb(cmd, cmd_addr);
125 debug_trace("\n");
126
127 if (wait_for_sync(dev)) {
128 debug("%s: Timeout waiting for response\n", __func__);
129 return -1;
130 }
131
132 /* Check result */
133 i = inb(data_addr);
134 if (i) {
135 debug("%s: CROS_EC result code %d\n", __func__, i);
136 return -i;
137 }
138
139 /* Read back args */
140 for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
141 *d = inb(args_addr + i);
142
143 /*
144 * If EC didn't modify args flags, then somehow we sent a new-style
145 * command to an old EC, which means it would have read its params
146 * from the wrong place.
147 */
148 if (!(args.flags & EC_HOST_ARGS_FLAG_TO_HOST)) {
149 debug("%s: CROS_EC protocol mismatch\n", __func__);
150 return -EC_RES_INVALID_RESPONSE;
151 }
152
153 if (args.data_size > din_len) {
154 debug("%s: CROS_EC returned too much data %d > %d\n",
155 __func__, args.data_size, din_len);
156 return -EC_RES_INVALID_RESPONSE;
157 }
158
159 /* Read data, if any */
160 for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++) {
161 *d = inb(param_addr + i);
162 debug_trace("%02x ", *d);
163 }
164 debug_trace("\n");
165
166 /* Verify checksum */
167 csum = cmd + args.flags + args.command_version + args.data_size;
168 for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++)
169 csum += *d;
170
171 if (args.checksum != (uint8_t)csum) {
172 debug("%s: CROS_EC response has invalid checksum\n", __func__);
173 return -EC_RES_INVALID_CHECKSUM;
174 }
175 *dinp = dev->din;
176
177 /* Return actual amount of data received */
178 return args.data_size;
179}
180
181/**
182 * Initialize LPC protocol.
183 *
184 * @param dev CROS_EC device
185 * @param blob Device tree blob
186 * @return 0 if ok, -1 on error
187 */
188int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)
189{
190 int byte, i;
191
192 /* See if we can find an EC at the other end */
193 byte = 0xff;
194 byte &= inb(EC_LPC_ADDR_HOST_CMD);
195 byte &= inb(EC_LPC_ADDR_HOST_DATA);
Simon Glassa1a34302014-10-10 07:30:15 -0600196 for (i = 0; i < EC_PROTO2_MAX_PARAM_SIZE && (byte == 0xff); i++)
Hung-ying Tyan62899fc2013-05-15 18:27:31 +0800197 byte &= inb(EC_LPC_ADDR_HOST_PARAM + i);
198 if (byte == 0xff) {
199 debug("%s: CROS_EC device not found on LPC bus\n",
200 __func__);
201 return -1;
202 }
203
204 return 0;
205}
206
207/*
208 * Test if LPC command args are supported.
209 *
210 * The cheapest way to do this is by looking for the memory-mapped
211 * flag. This is faster than sending a new-style 'hello' command and
212 * seeing whether the EC sets the EC_HOST_ARGS_FLAG_FROM_HOST flag
213 * in args when it responds.
214 */
Simon Glass23fb1562015-03-26 09:29:30 -0600215static int cros_ec_lpc_check_version(struct udevice *dev)
Hung-ying Tyan62899fc2013-05-15 18:27:31 +0800216{
217 if (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) == 'E' &&
218 inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1)
219 == 'C' &&
220 (inb(EC_LPC_ADDR_MEMMAP +
221 EC_MEMMAP_HOST_CMD_FLAGS) &
222 EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED)) {
Vadim Bendebury2f548812014-02-27 13:26:04 -0700223 return 0;
Hung-ying Tyan62899fc2013-05-15 18:27:31 +0800224 }
Hung-ying Tyan62899fc2013-05-15 18:27:31 +0800225
Vadim Bendebury2f548812014-02-27 13:26:04 -0700226 printf("%s: ERROR: old EC interface not supported\n", __func__);
227 return -1;
Hung-ying Tyan62899fc2013-05-15 18:27:31 +0800228}
Simon Glass23fb1562015-03-26 09:29:30 -0600229
Simon Glass23fb1562015-03-26 09:29:30 -0600230static int cros_ec_probe(struct udevice *dev)
231{
232 return cros_ec_register(dev);
233}
234
235static struct dm_cros_ec_ops cros_ec_ops = {
Simon Glass4ed88a52018-10-01 12:22:39 -0600236 .packet = cros_ec_lpc_packet,
Simon Glass23fb1562015-03-26 09:29:30 -0600237 .command = cros_ec_lpc_command,
238 .check_version = cros_ec_lpc_check_version,
239};
240
241static const struct udevice_id cros_ec_ids[] = {
Simon Glass73f220c2015-03-26 09:29:39 -0600242 { .compatible = "google,cros-ec-lpc" },
Simon Glass23fb1562015-03-26 09:29:30 -0600243 { }
244};
245
246U_BOOT_DRIVER(cros_ec_lpc) = {
Simon Glass73f220c2015-03-26 09:29:39 -0600247 .name = "cros_ec_lpc",
Simon Glass23fb1562015-03-26 09:29:30 -0600248 .id = UCLASS_CROS_EC,
249 .of_match = cros_ec_ids,
250 .probe = cros_ec_probe,
251 .ops = &cros_ec_ops,
252};