]>
Commit | Line | Data |
---|---|---|
88364387 HT |
1 | /* |
2 | * Chromium OS cros_ec driver | |
3 | * | |
4 | * Copyright (c) 2012 The Chromium OS Authors. | |
88364387 | 5 | * |
1a459660 | 6 | * SPDX-License-Identifier: GPL-2.0+ |
88364387 HT |
7 | */ |
8 | ||
9 | /* | |
836bb6e8 SG |
10 | * This is the interface to the Chrome OS EC. It provides keyboard functions, |
11 | * power control and battery management. Quite a few other functions are | |
12 | * provided to enable the EC software to be updated, talk to the EC's I2C bus | |
13 | * and store a small amount of data in a memory which persists while the EC | |
14 | * is not reset. | |
88364387 HT |
15 | */ |
16 | ||
17 | #include <common.h> | |
18 | #include <command.h> | |
84d6cbd3 | 19 | #include <dm.h> |
88364387 HT |
20 | #include <i2c.h> |
21 | #include <cros_ec.h> | |
22 | #include <fdtdec.h> | |
23 | #include <malloc.h> | |
24 | #include <spi.h> | |
2ab83f0d | 25 | #include <asm/errno.h> |
88364387 HT |
26 | #include <asm/io.h> |
27 | #include <asm-generic/gpio.h> | |
84d6cbd3 SG |
28 | #include <dm/device-internal.h> |
29 | #include <dm/uclass-internal.h> | |
88364387 HT |
30 | |
31 | #ifdef DEBUG_TRACE | |
32 | #define debug_trace(fmt, b...) debug(fmt, #b) | |
33 | #else | |
34 | #define debug_trace(fmt, b...) | |
35 | #endif | |
36 | ||
37 | enum { | |
38 | /* Timeout waiting for a flash erase command to complete */ | |
39 | CROS_EC_CMD_TIMEOUT_MS = 5000, | |
40 | /* Timeout waiting for a synchronous hash to be recomputed */ | |
41 | CROS_EC_CMD_HASH_TIMEOUT_MS = 2000, | |
42 | }; | |
43 | ||
84d6cbd3 | 44 | #ifndef CONFIG_DM_CROS_EC |
88364387 | 45 | static struct cros_ec_dev static_dev, *last_dev; |
84d6cbd3 | 46 | #endif |
88364387 HT |
47 | |
48 | DECLARE_GLOBAL_DATA_PTR; | |
49 | ||
50 | /* Note: depends on enum ec_current_image */ | |
51 | static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"}; | |
52 | ||
53 | void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len) | |
54 | { | |
55 | #ifdef DEBUG | |
56 | int i; | |
57 | ||
58 | printf("%s: ", name); | |
59 | if (cmd != -1) | |
60 | printf("cmd=%#x: ", cmd); | |
61 | for (i = 0; i < len; i++) | |
62 | printf("%02x ", data[i]); | |
63 | printf("\n"); | |
64 | #endif | |
65 | } | |
66 | ||
67 | /* | |
68 | * Calculate a simple 8-bit checksum of a data block | |
69 | * | |
70 | * @param data Data block to checksum | |
71 | * @param size Size of data block in bytes | |
72 | * @return checksum value (0 to 255) | |
73 | */ | |
74 | int cros_ec_calc_checksum(const uint8_t *data, int size) | |
75 | { | |
76 | int csum, i; | |
77 | ||
78 | for (i = csum = 0; i < size; i++) | |
79 | csum += data[i]; | |
80 | return csum & 0xff; | |
81 | } | |
82 | ||
2d8ede58 SG |
83 | /** |
84 | * Create a request packet for protocol version 3. | |
85 | * | |
86 | * The packet is stored in the device's internal output buffer. | |
87 | * | |
88 | * @param dev CROS-EC device | |
89 | * @param cmd Command to send (EC_CMD_...) | |
90 | * @param cmd_version Version of command to send (EC_VER_...) | |
91 | * @param dout Output data (may be NULL If dout_len=0) | |
92 | * @param dout_len Size of output data in bytes | |
93 | * @return packet size in bytes, or <0 if error. | |
94 | */ | |
95 | static int create_proto3_request(struct cros_ec_dev *dev, | |
96 | int cmd, int cmd_version, | |
97 | const void *dout, int dout_len) | |
98 | { | |
99 | struct ec_host_request *rq = (struct ec_host_request *)dev->dout; | |
100 | int out_bytes = dout_len + sizeof(*rq); | |
101 | ||
102 | /* Fail if output size is too big */ | |
103 | if (out_bytes > (int)sizeof(dev->dout)) { | |
104 | debug("%s: Cannot send %d bytes\n", __func__, dout_len); | |
105 | return -EC_RES_REQUEST_TRUNCATED; | |
106 | } | |
107 | ||
108 | /* Fill in request packet */ | |
109 | rq->struct_version = EC_HOST_REQUEST_VERSION; | |
110 | rq->checksum = 0; | |
111 | rq->command = cmd; | |
112 | rq->command_version = cmd_version; | |
113 | rq->reserved = 0; | |
114 | rq->data_len = dout_len; | |
115 | ||
116 | /* Copy data after header */ | |
117 | memcpy(rq + 1, dout, dout_len); | |
118 | ||
119 | /* Write checksum field so the entire packet sums to 0 */ | |
120 | rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes)); | |
121 | ||
122 | cros_ec_dump_data("out", cmd, dev->dout, out_bytes); | |
123 | ||
124 | /* Return size of request packet */ | |
125 | return out_bytes; | |
126 | } | |
127 | ||
128 | /** | |
129 | * Prepare the device to receive a protocol version 3 response. | |
130 | * | |
131 | * @param dev CROS-EC device | |
132 | * @param din_len Maximum size of response in bytes | |
133 | * @return maximum expected number of bytes in response, or <0 if error. | |
134 | */ | |
135 | static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len) | |
136 | { | |
137 | int in_bytes = din_len + sizeof(struct ec_host_response); | |
138 | ||
139 | /* Fail if input size is too big */ | |
140 | if (in_bytes > (int)sizeof(dev->din)) { | |
141 | debug("%s: Cannot receive %d bytes\n", __func__, din_len); | |
142 | return -EC_RES_RESPONSE_TOO_BIG; | |
143 | } | |
144 | ||
145 | /* Return expected size of response packet */ | |
146 | return in_bytes; | |
147 | } | |
148 | ||
149 | /** | |
150 | * Handle a protocol version 3 response packet. | |
151 | * | |
152 | * The packet must already be stored in the device's internal input buffer. | |
153 | * | |
154 | * @param dev CROS-EC device | |
155 | * @param dinp Returns pointer to response data | |
156 | * @param din_len Maximum size of response in bytes | |
157 | * @return number of bytes of response data, or <0 if error | |
158 | */ | |
159 | static int handle_proto3_response(struct cros_ec_dev *dev, | |
160 | uint8_t **dinp, int din_len) | |
161 | { | |
162 | struct ec_host_response *rs = (struct ec_host_response *)dev->din; | |
163 | int in_bytes; | |
164 | int csum; | |
165 | ||
166 | cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs)); | |
167 | ||
168 | /* Check input data */ | |
169 | if (rs->struct_version != EC_HOST_RESPONSE_VERSION) { | |
170 | debug("%s: EC response version mismatch\n", __func__); | |
171 | return -EC_RES_INVALID_RESPONSE; | |
172 | } | |
173 | ||
174 | if (rs->reserved) { | |
175 | debug("%s: EC response reserved != 0\n", __func__); | |
176 | return -EC_RES_INVALID_RESPONSE; | |
177 | } | |
178 | ||
179 | if (rs->data_len > din_len) { | |
180 | debug("%s: EC returned too much data\n", __func__); | |
181 | return -EC_RES_RESPONSE_TOO_BIG; | |
182 | } | |
183 | ||
184 | cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len); | |
185 | ||
186 | /* Update in_bytes to actual data size */ | |
187 | in_bytes = sizeof(*rs) + rs->data_len; | |
188 | ||
189 | /* Verify checksum */ | |
190 | csum = cros_ec_calc_checksum(dev->din, in_bytes); | |
191 | if (csum) { | |
192 | debug("%s: EC response checksum invalid: 0x%02x\n", __func__, | |
193 | csum); | |
194 | return -EC_RES_INVALID_CHECKSUM; | |
195 | } | |
196 | ||
197 | /* Return error result, if any */ | |
198 | if (rs->result) | |
199 | return -(int)rs->result; | |
200 | ||
201 | /* If we're still here, set response data pointer and return length */ | |
202 | *dinp = (uint8_t *)(rs + 1); | |
203 | ||
204 | return rs->data_len; | |
205 | } | |
206 | ||
207 | static int send_command_proto3(struct cros_ec_dev *dev, | |
208 | int cmd, int cmd_version, | |
209 | const void *dout, int dout_len, | |
210 | uint8_t **dinp, int din_len) | |
211 | { | |
84d6cbd3 SG |
212 | #ifdef CONFIG_DM_CROS_EC |
213 | struct dm_cros_ec_ops *ops; | |
214 | #endif | |
2d8ede58 SG |
215 | int out_bytes, in_bytes; |
216 | int rv; | |
217 | ||
218 | /* Create request packet */ | |
219 | out_bytes = create_proto3_request(dev, cmd, cmd_version, | |
220 | dout, dout_len); | |
221 | if (out_bytes < 0) | |
222 | return out_bytes; | |
223 | ||
224 | /* Prepare response buffer */ | |
225 | in_bytes = prepare_proto3_response_buffer(dev, din_len); | |
226 | if (in_bytes < 0) | |
227 | return in_bytes; | |
228 | ||
84d6cbd3 SG |
229 | #ifdef CONFIG_DM_CROS_EC |
230 | ops = dm_cros_ec_get_ops(dev->dev); | |
231 | rv = ops->packet(dev->dev, out_bytes, in_bytes); | |
232 | #else | |
2d8ede58 | 233 | switch (dev->interface) { |
a6070283 RS |
234 | #ifdef CONFIG_CROS_EC_SPI |
235 | case CROS_EC_IF_SPI: | |
236 | rv = cros_ec_spi_packet(dev, out_bytes, in_bytes); | |
237 | break; | |
df93d90a SG |
238 | #endif |
239 | #ifdef CONFIG_CROS_EC_SANDBOX | |
240 | case CROS_EC_IF_SANDBOX: | |
241 | rv = cros_ec_sandbox_packet(dev, out_bytes, in_bytes); | |
242 | break; | |
a6070283 | 243 | #endif |
2d8ede58 SG |
244 | case CROS_EC_IF_NONE: |
245 | /* TODO: support protocol 3 for LPC, I2C; for now fall through */ | |
246 | default: | |
247 | debug("%s: Unsupported interface\n", __func__); | |
248 | rv = -1; | |
249 | } | |
84d6cbd3 | 250 | #endif |
2d8ede58 SG |
251 | if (rv < 0) |
252 | return rv; | |
253 | ||
254 | /* Process the response */ | |
255 | return handle_proto3_response(dev, dinp, din_len); | |
256 | } | |
257 | ||
88364387 HT |
258 | static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version, |
259 | const void *dout, int dout_len, | |
260 | uint8_t **dinp, int din_len) | |
261 | { | |
84d6cbd3 SG |
262 | #ifdef CONFIG_DM_CROS_EC |
263 | struct dm_cros_ec_ops *ops; | |
264 | #endif | |
2d8ede58 SG |
265 | int ret = -1; |
266 | ||
267 | /* Handle protocol version 3 support */ | |
268 | if (dev->protocol_version == 3) { | |
269 | return send_command_proto3(dev, cmd, cmd_version, | |
270 | dout, dout_len, dinp, din_len); | |
271 | } | |
88364387 | 272 | |
84d6cbd3 SG |
273 | #ifdef CONFIG_DM_CROS_EC |
274 | ops = dm_cros_ec_get_ops(dev->dev); | |
275 | ret = ops->command(dev->dev, cmd, cmd_version, | |
276 | (const uint8_t *)dout, dout_len, dinp, din_len); | |
277 | #else | |
88364387 HT |
278 | switch (dev->interface) { |
279 | #ifdef CONFIG_CROS_EC_SPI | |
280 | case CROS_EC_IF_SPI: | |
281 | ret = cros_ec_spi_command(dev, cmd, cmd_version, | |
282 | (const uint8_t *)dout, dout_len, | |
283 | dinp, din_len); | |
284 | break; | |
285 | #endif | |
286 | #ifdef CONFIG_CROS_EC_I2C | |
287 | case CROS_EC_IF_I2C: | |
288 | ret = cros_ec_i2c_command(dev, cmd, cmd_version, | |
289 | (const uint8_t *)dout, dout_len, | |
290 | dinp, din_len); | |
291 | break; | |
292 | #endif | |
293 | #ifdef CONFIG_CROS_EC_LPC | |
294 | case CROS_EC_IF_LPC: | |
295 | ret = cros_ec_lpc_command(dev, cmd, cmd_version, | |
296 | (const uint8_t *)dout, dout_len, | |
297 | dinp, din_len); | |
298 | break; | |
299 | #endif | |
300 | case CROS_EC_IF_NONE: | |
301 | default: | |
302 | ret = -1; | |
303 | } | |
84d6cbd3 | 304 | #endif |
88364387 HT |
305 | |
306 | return ret; | |
307 | } | |
308 | ||
309 | /** | |
310 | * Send a command to the CROS-EC device and return the reply. | |
311 | * | |
312 | * The device's internal input/output buffers are used. | |
313 | * | |
314 | * @param dev CROS-EC device | |
315 | * @param cmd Command to send (EC_CMD_...) | |
316 | * @param cmd_version Version of command to send (EC_VER_...) | |
317 | * @param dout Output data (may be NULL If dout_len=0) | |
318 | * @param dout_len Size of output data in bytes | |
319 | * @param dinp Response data (may be NULL If din_len=0). | |
320 | * If not NULL, it will be updated to point to the data | |
321 | * and will always be double word aligned (64-bits) | |
322 | * @param din_len Maximum size of response in bytes | |
323 | * @return number of bytes in response, or -1 on error | |
324 | */ | |
325 | static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd, | |
326 | int cmd_version, const void *dout, int dout_len, uint8_t **dinp, | |
327 | int din_len) | |
328 | { | |
2ab83f0d | 329 | uint8_t *din = NULL; |
88364387 HT |
330 | int len; |
331 | ||
88364387 HT |
332 | len = send_command(dev, cmd, cmd_version, dout, dout_len, |
333 | &din, din_len); | |
334 | ||
335 | /* If the command doesn't complete, wait a while */ | |
336 | if (len == -EC_RES_IN_PROGRESS) { | |
2ab83f0d | 337 | struct ec_response_get_comms_status *resp = NULL; |
88364387 HT |
338 | ulong start; |
339 | ||
340 | /* Wait for command to complete */ | |
341 | start = get_timer(0); | |
342 | do { | |
343 | int ret; | |
344 | ||
345 | mdelay(50); /* Insert some reasonable delay */ | |
346 | ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0, | |
347 | NULL, 0, | |
348 | (uint8_t **)&resp, sizeof(*resp)); | |
349 | if (ret < 0) | |
350 | return ret; | |
351 | ||
352 | if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) { | |
353 | debug("%s: Command %#02x timeout\n", | |
354 | __func__, cmd); | |
355 | return -EC_RES_TIMEOUT; | |
356 | } | |
357 | } while (resp->flags & EC_COMMS_STATUS_PROCESSING); | |
358 | ||
359 | /* OK it completed, so read the status response */ | |
360 | /* not sure why it was 0 for the last argument */ | |
361 | len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0, | |
362 | NULL, 0, &din, din_len); | |
363 | } | |
364 | ||
2ab83f0d SG |
365 | debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp, |
366 | dinp ? *dinp : NULL); | |
88364387 HT |
367 | if (dinp) { |
368 | /* If we have any data to return, it must be 64bit-aligned */ | |
369 | assert(len <= 0 || !((uintptr_t)din & 7)); | |
370 | *dinp = din; | |
371 | } | |
372 | ||
373 | return len; | |
374 | } | |
375 | ||
376 | /** | |
377 | * Send a command to the CROS-EC device and return the reply. | |
378 | * | |
379 | * The device's internal input/output buffers are used. | |
380 | * | |
381 | * @param dev CROS-EC device | |
382 | * @param cmd Command to send (EC_CMD_...) | |
383 | * @param cmd_version Version of command to send (EC_VER_...) | |
384 | * @param dout Output data (may be NULL If dout_len=0) | |
385 | * @param dout_len Size of output data in bytes | |
386 | * @param din Response data (may be NULL If din_len=0). | |
387 | * It not NULL, it is a place for ec_command() to copy the | |
388 | * data to. | |
389 | * @param din_len Maximum size of response in bytes | |
390 | * @return number of bytes in response, or -1 on error | |
391 | */ | |
392 | static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version, | |
393 | const void *dout, int dout_len, | |
394 | void *din, int din_len) | |
395 | { | |
396 | uint8_t *in_buffer; | |
397 | int len; | |
398 | ||
399 | assert((din_len == 0) || din); | |
400 | len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len, | |
401 | &in_buffer, din_len); | |
402 | if (len > 0) { | |
403 | /* | |
404 | * If we were asked to put it somewhere, do so, otherwise just | |
405 | * disregard the result. | |
406 | */ | |
407 | if (din && in_buffer) { | |
408 | assert(len <= din_len); | |
409 | memmove(din, in_buffer, len); | |
410 | } | |
411 | } | |
412 | return len; | |
413 | } | |
414 | ||
415 | int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan) | |
416 | { | |
836bb6e8 | 417 | if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan, |
2ab83f0d | 418 | sizeof(scan->data)) != sizeof(scan->data)) |
88364387 HT |
419 | return -1; |
420 | ||
421 | return 0; | |
422 | } | |
423 | ||
424 | int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen) | |
425 | { | |
426 | struct ec_response_get_version *r; | |
427 | ||
428 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, | |
2ab83f0d | 429 | (uint8_t **)&r, sizeof(*r)) != sizeof(*r)) |
88364387 HT |
430 | return -1; |
431 | ||
2ab83f0d | 432 | if (maxlen > (int)sizeof(r->version_string_ro)) |
88364387 HT |
433 | maxlen = sizeof(r->version_string_ro); |
434 | ||
435 | switch (r->current_image) { | |
436 | case EC_IMAGE_RO: | |
437 | memcpy(id, r->version_string_ro, maxlen); | |
438 | break; | |
439 | case EC_IMAGE_RW: | |
440 | memcpy(id, r->version_string_rw, maxlen); | |
441 | break; | |
442 | default: | |
443 | return -1; | |
444 | } | |
445 | ||
446 | id[maxlen - 1] = '\0'; | |
447 | return 0; | |
448 | } | |
449 | ||
450 | int cros_ec_read_version(struct cros_ec_dev *dev, | |
451 | struct ec_response_get_version **versionp) | |
452 | { | |
453 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, | |
454 | (uint8_t **)versionp, sizeof(**versionp)) | |
2ab83f0d | 455 | != sizeof(**versionp)) |
88364387 HT |
456 | return -1; |
457 | ||
458 | return 0; | |
459 | } | |
460 | ||
461 | int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp) | |
462 | { | |
463 | if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0, | |
836bb6e8 | 464 | (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0) |
88364387 HT |
465 | return -1; |
466 | ||
467 | return 0; | |
468 | } | |
469 | ||
470 | int cros_ec_read_current_image(struct cros_ec_dev *dev, | |
471 | enum ec_current_image *image) | |
472 | { | |
473 | struct ec_response_get_version *r; | |
474 | ||
475 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, | |
2ab83f0d | 476 | (uint8_t **)&r, sizeof(*r)) != sizeof(*r)) |
88364387 HT |
477 | return -1; |
478 | ||
479 | *image = r->current_image; | |
480 | return 0; | |
481 | } | |
482 | ||
483 | static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev, | |
484 | struct ec_response_vboot_hash *hash) | |
485 | { | |
486 | struct ec_params_vboot_hash p; | |
487 | ulong start; | |
488 | ||
489 | start = get_timer(0); | |
490 | while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) { | |
491 | mdelay(50); /* Insert some reasonable delay */ | |
492 | ||
493 | p.cmd = EC_VBOOT_HASH_GET; | |
494 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), | |
495 | hash, sizeof(*hash)) < 0) | |
496 | return -1; | |
497 | ||
498 | if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) { | |
499 | debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__); | |
500 | return -EC_RES_TIMEOUT; | |
501 | } | |
502 | } | |
503 | return 0; | |
504 | } | |
505 | ||
506 | ||
507 | int cros_ec_read_hash(struct cros_ec_dev *dev, | |
508 | struct ec_response_vboot_hash *hash) | |
509 | { | |
510 | struct ec_params_vboot_hash p; | |
511 | int rv; | |
512 | ||
513 | p.cmd = EC_VBOOT_HASH_GET; | |
514 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), | |
515 | hash, sizeof(*hash)) < 0) | |
516 | return -1; | |
517 | ||
518 | /* If the EC is busy calculating the hash, fidget until it's done. */ | |
519 | rv = cros_ec_wait_on_hash_done(dev, hash); | |
520 | if (rv) | |
521 | return rv; | |
522 | ||
523 | /* If the hash is valid, we're done. Otherwise, we have to kick it off | |
524 | * again and wait for it to complete. Note that we explicitly assume | |
525 | * that hashing zero bytes is always wrong, even though that would | |
526 | * produce a valid hash value. */ | |
527 | if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size) | |
528 | return 0; | |
529 | ||
530 | debug("%s: No valid hash (status=%d size=%d). Compute one...\n", | |
531 | __func__, hash->status, hash->size); | |
532 | ||
836bb6e8 | 533 | p.cmd = EC_VBOOT_HASH_START; |
88364387 HT |
534 | p.hash_type = EC_VBOOT_HASH_TYPE_SHA256; |
535 | p.nonce_size = 0; | |
536 | p.offset = EC_VBOOT_HASH_OFFSET_RW; | |
537 | ||
538 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), | |
539 | hash, sizeof(*hash)) < 0) | |
540 | return -1; | |
541 | ||
542 | rv = cros_ec_wait_on_hash_done(dev, hash); | |
543 | if (rv) | |
544 | return rv; | |
545 | ||
546 | debug("%s: hash done\n", __func__); | |
547 | ||
548 | return 0; | |
549 | } | |
550 | ||
551 | static int cros_ec_invalidate_hash(struct cros_ec_dev *dev) | |
552 | { | |
553 | struct ec_params_vboot_hash p; | |
554 | struct ec_response_vboot_hash *hash; | |
555 | ||
556 | /* We don't have an explict command for the EC to discard its current | |
557 | * hash value, so we'll just tell it to calculate one that we know is | |
558 | * wrong (we claim that hashing zero bytes is always invalid). | |
559 | */ | |
560 | p.cmd = EC_VBOOT_HASH_RECALC; | |
561 | p.hash_type = EC_VBOOT_HASH_TYPE_SHA256; | |
562 | p.nonce_size = 0; | |
563 | p.offset = 0; | |
564 | p.size = 0; | |
565 | ||
566 | debug("%s:\n", __func__); | |
567 | ||
568 | if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), | |
569 | (uint8_t **)&hash, sizeof(*hash)) < 0) | |
570 | return -1; | |
571 | ||
572 | /* No need to wait for it to finish */ | |
573 | return 0; | |
574 | } | |
575 | ||
576 | int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd, | |
577 | uint8_t flags) | |
578 | { | |
579 | struct ec_params_reboot_ec p; | |
580 | ||
581 | p.cmd = cmd; | |
582 | p.flags = flags; | |
583 | ||
584 | if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0) | |
585 | < 0) | |
586 | return -1; | |
587 | ||
588 | if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) { | |
589 | /* | |
590 | * EC reboot will take place immediately so delay to allow it | |
591 | * to complete. Note that some reboot types (EC_REBOOT_COLD) | |
592 | * will reboot the AP as well, in which case we won't actually | |
593 | * get to this point. | |
594 | */ | |
595 | /* | |
596 | * TODO([email protected]): Would be nice if we had a | |
597 | * better way to determine when the reboot is complete. Could | |
598 | * we poll a memory-mapped LPC value? | |
599 | */ | |
600 | udelay(50000); | |
601 | } | |
602 | ||
603 | return 0; | |
604 | } | |
605 | ||
606 | int cros_ec_interrupt_pending(struct cros_ec_dev *dev) | |
607 | { | |
608 | /* no interrupt support : always poll */ | |
609 | if (!fdt_gpio_isvalid(&dev->ec_int)) | |
2ab83f0d | 610 | return -ENOENT; |
88364387 HT |
611 | |
612 | return !gpio_get_value(dev->ec_int.gpio); | |
613 | } | |
614 | ||
836bb6e8 | 615 | int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info) |
88364387 | 616 | { |
836bb6e8 | 617 | if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info, |
2ab83f0d | 618 | sizeof(*info)) != sizeof(*info)) |
88364387 HT |
619 | return -1; |
620 | ||
621 | return 0; | |
622 | } | |
623 | ||
624 | int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr) | |
625 | { | |
626 | struct ec_response_host_event_mask *resp; | |
627 | ||
628 | /* | |
629 | * Use the B copy of the event flags, because the main copy is already | |
630 | * used by ACPI/SMI. | |
631 | */ | |
632 | if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0, | |
2ab83f0d | 633 | (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp)) |
88364387 HT |
634 | return -1; |
635 | ||
636 | if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID)) | |
637 | return -1; | |
638 | ||
639 | *events_ptr = resp->mask; | |
640 | return 0; | |
641 | } | |
642 | ||
643 | int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events) | |
644 | { | |
645 | struct ec_params_host_event_mask params; | |
646 | ||
647 | params.mask = events; | |
648 | ||
649 | /* | |
650 | * Use the B copy of the event flags, so it affects the data returned | |
651 | * by cros_ec_get_host_events(). | |
652 | */ | |
653 | if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0, | |
654 | ¶ms, sizeof(params), NULL, 0) < 0) | |
655 | return -1; | |
656 | ||
657 | return 0; | |
658 | } | |
659 | ||
660 | int cros_ec_flash_protect(struct cros_ec_dev *dev, | |
661 | uint32_t set_mask, uint32_t set_flags, | |
662 | struct ec_response_flash_protect *resp) | |
663 | { | |
664 | struct ec_params_flash_protect params; | |
665 | ||
666 | params.mask = set_mask; | |
667 | params.flags = set_flags; | |
668 | ||
669 | if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT, | |
670 | ¶ms, sizeof(params), | |
2ab83f0d | 671 | resp, sizeof(*resp)) != sizeof(*resp)) |
88364387 HT |
672 | return -1; |
673 | ||
674 | return 0; | |
675 | } | |
676 | ||
677 | static int cros_ec_check_version(struct cros_ec_dev *dev) | |
678 | { | |
679 | struct ec_params_hello req; | |
680 | struct ec_response_hello *resp; | |
681 | ||
682 | #ifdef CONFIG_CROS_EC_LPC | |
683 | /* LPC has its own way of doing this */ | |
684 | if (dev->interface == CROS_EC_IF_LPC) | |
685 | return cros_ec_lpc_check_version(dev); | |
686 | #endif | |
687 | ||
688 | /* | |
689 | * TODO([email protected]). | |
690 | * There is a strange oddity here with the EC. We could just ignore | |
691 | * the response, i.e. pass the last two parameters as NULL and 0. | |
692 | * In this case we won't read back very many bytes from the EC. | |
693 | * On the I2C bus the EC gets upset about this and will try to send | |
694 | * the bytes anyway. This means that we will have to wait for that | |
695 | * to complete before continuing with a new EC command. | |
696 | * | |
697 | * This problem is probably unique to the I2C bus. | |
698 | * | |
699 | * So for now, just read all the data anyway. | |
700 | */ | |
e8c12662 | 701 | |
a6070283 RS |
702 | /* Try sending a version 3 packet */ |
703 | dev->protocol_version = 3; | |
d11e8fd8 | 704 | req.in_data = 0; |
a6070283 RS |
705 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
706 | (uint8_t **)&resp, sizeof(*resp)) > 0) { | |
707 | return 0; | |
708 | } | |
709 | ||
e8c12662 RS |
710 | /* Try sending a version 2 packet */ |
711 | dev->protocol_version = 2; | |
88364387 HT |
712 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
713 | (uint8_t **)&resp, sizeof(*resp)) > 0) { | |
e8c12662 | 714 | return 0; |
88364387 HT |
715 | } |
716 | ||
e8c12662 RS |
717 | /* |
718 | * Fail if we're still here, since the EC doesn't understand any | |
719 | * protcol version we speak. Version 1 interface without command | |
720 | * version is no longer supported, and we don't know about any new | |
721 | * protocol versions. | |
722 | */ | |
723 | dev->protocol_version = 0; | |
724 | printf("%s: ERROR: old EC interface not supported\n", __func__); | |
725 | return -1; | |
88364387 HT |
726 | } |
727 | ||
728 | int cros_ec_test(struct cros_ec_dev *dev) | |
729 | { | |
730 | struct ec_params_hello req; | |
731 | struct ec_response_hello *resp; | |
732 | ||
733 | req.in_data = 0x12345678; | |
734 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), | |
735 | (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) { | |
736 | printf("ec_command_inptr() returned error\n"); | |
737 | return -1; | |
738 | } | |
739 | if (resp->out_data != req.in_data + 0x01020304) { | |
740 | printf("Received invalid handshake %x\n", resp->out_data); | |
741 | return -1; | |
742 | } | |
743 | ||
744 | return 0; | |
745 | } | |
746 | ||
747 | int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region, | |
748 | uint32_t *offset, uint32_t *size) | |
749 | { | |
750 | struct ec_params_flash_region_info p; | |
751 | struct ec_response_flash_region_info *r; | |
752 | int ret; | |
753 | ||
754 | p.region = region; | |
755 | ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO, | |
756 | EC_VER_FLASH_REGION_INFO, | |
757 | &p, sizeof(p), (uint8_t **)&r, sizeof(*r)); | |
758 | if (ret != sizeof(*r)) | |
759 | return -1; | |
760 | ||
761 | if (offset) | |
762 | *offset = r->offset; | |
763 | if (size) | |
764 | *size = r->size; | |
765 | ||
766 | return 0; | |
767 | } | |
768 | ||
769 | int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size) | |
770 | { | |
771 | struct ec_params_flash_erase p; | |
772 | ||
773 | p.offset = offset; | |
774 | p.size = size; | |
775 | return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p), | |
776 | NULL, 0); | |
777 | } | |
778 | ||
779 | /** | |
780 | * Write a single block to the flash | |
781 | * | |
782 | * Write a block of data to the EC flash. The size must not exceed the flash | |
783 | * write block size which you can obtain from cros_ec_flash_write_burst_size(). | |
784 | * | |
785 | * The offset starts at 0. You can obtain the region information from | |
786 | * cros_ec_flash_offset() to find out where to write for a particular region. | |
787 | * | |
788 | * Attempting to write to the region where the EC is currently running from | |
789 | * will result in an error. | |
790 | * | |
791 | * @param dev CROS-EC device | |
792 | * @param data Pointer to data buffer to write | |
793 | * @param offset Offset within flash to write to. | |
794 | * @param size Number of bytes to write | |
795 | * @return 0 if ok, -1 on error | |
796 | */ | |
797 | static int cros_ec_flash_write_block(struct cros_ec_dev *dev, | |
798 | const uint8_t *data, uint32_t offset, uint32_t size) | |
799 | { | |
800 | struct ec_params_flash_write p; | |
801 | ||
802 | p.offset = offset; | |
803 | p.size = size; | |
836bb6e8 SG |
804 | assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE); |
805 | memcpy(&p + 1, data, p.size); | |
88364387 HT |
806 | |
807 | return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0, | |
808 | &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1; | |
809 | } | |
810 | ||
811 | /** | |
812 | * Return optimal flash write burst size | |
813 | */ | |
814 | static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev) | |
815 | { | |
836bb6e8 | 816 | return EC_FLASH_WRITE_VER0_SIZE; |
88364387 HT |
817 | } |
818 | ||
819 | /** | |
820 | * Check if a block of data is erased (all 0xff) | |
821 | * | |
822 | * This function is useful when dealing with flash, for checking whether a | |
823 | * data block is erased and thus does not need to be programmed. | |
824 | * | |
825 | * @param data Pointer to data to check (must be word-aligned) | |
826 | * @param size Number of bytes to check (must be word-aligned) | |
827 | * @return 0 if erased, non-zero if any word is not erased | |
828 | */ | |
829 | static int cros_ec_data_is_erased(const uint32_t *data, int size) | |
830 | { | |
831 | assert(!(size & 3)); | |
832 | size /= sizeof(uint32_t); | |
833 | for (; size > 0; size -= 4, data++) | |
834 | if (*data != -1U) | |
835 | return 0; | |
836 | ||
837 | return 1; | |
838 | } | |
839 | ||
840 | int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data, | |
841 | uint32_t offset, uint32_t size) | |
842 | { | |
843 | uint32_t burst = cros_ec_flash_write_burst_size(dev); | |
844 | uint32_t end, off; | |
845 | int ret; | |
846 | ||
847 | /* | |
848 | * TODO: round up to the nearest multiple of write size. Can get away | |
849 | * without that on link right now because its write size is 4 bytes. | |
850 | */ | |
851 | end = offset + size; | |
852 | for (off = offset; off < end; off += burst, data += burst) { | |
853 | uint32_t todo; | |
854 | ||
855 | /* If the data is empty, there is no point in programming it */ | |
856 | todo = min(end - off, burst); | |
857 | if (dev->optimise_flash_write && | |
858 | cros_ec_data_is_erased((uint32_t *)data, todo)) | |
859 | continue; | |
860 | ||
861 | ret = cros_ec_flash_write_block(dev, data, off, todo); | |
862 | if (ret) | |
863 | return ret; | |
864 | } | |
865 | ||
866 | return 0; | |
867 | } | |
868 | ||
869 | /** | |
870 | * Read a single block from the flash | |
871 | * | |
872 | * Read a block of data from the EC flash. The size must not exceed the flash | |
873 | * write block size which you can obtain from cros_ec_flash_write_burst_size(). | |
874 | * | |
875 | * The offset starts at 0. You can obtain the region information from | |
876 | * cros_ec_flash_offset() to find out where to read for a particular region. | |
877 | * | |
878 | * @param dev CROS-EC device | |
879 | * @param data Pointer to data buffer to read into | |
880 | * @param offset Offset within flash to read from | |
881 | * @param size Number of bytes to read | |
882 | * @return 0 if ok, -1 on error | |
883 | */ | |
884 | static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data, | |
885 | uint32_t offset, uint32_t size) | |
886 | { | |
887 | struct ec_params_flash_read p; | |
888 | ||
889 | p.offset = offset; | |
890 | p.size = size; | |
891 | ||
892 | return ec_command(dev, EC_CMD_FLASH_READ, 0, | |
893 | &p, sizeof(p), data, size) >= 0 ? 0 : -1; | |
894 | } | |
895 | ||
896 | int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset, | |
897 | uint32_t size) | |
898 | { | |
899 | uint32_t burst = cros_ec_flash_write_burst_size(dev); | |
900 | uint32_t end, off; | |
901 | int ret; | |
902 | ||
903 | end = offset + size; | |
904 | for (off = offset; off < end; off += burst, data += burst) { | |
905 | ret = cros_ec_flash_read_block(dev, data, off, | |
906 | min(end - off, burst)); | |
907 | if (ret) | |
908 | return ret; | |
909 | } | |
910 | ||
911 | return 0; | |
912 | } | |
913 | ||
914 | int cros_ec_flash_update_rw(struct cros_ec_dev *dev, | |
915 | const uint8_t *image, int image_size) | |
916 | { | |
917 | uint32_t rw_offset, rw_size; | |
918 | int ret; | |
919 | ||
920 | if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size)) | |
921 | return -1; | |
2ab83f0d | 922 | if (image_size > (int)rw_size) |
88364387 HT |
923 | return -1; |
924 | ||
925 | /* Invalidate the existing hash, just in case the AP reboots | |
926 | * unexpectedly during the update. If that happened, the EC RW firmware | |
927 | * would be invalid, but the EC would still have the original hash. | |
928 | */ | |
929 | ret = cros_ec_invalidate_hash(dev); | |
930 | if (ret) | |
931 | return ret; | |
932 | ||
933 | /* | |
934 | * Erase the entire RW section, so that the EC doesn't see any garbage | |
935 | * past the new image if it's smaller than the current image. | |
936 | * | |
937 | * TODO: could optimize this to erase just the current image, since | |
938 | * presumably everything past that is 0xff's. But would still need to | |
939 | * round up to the nearest multiple of erase size. | |
940 | */ | |
941 | ret = cros_ec_flash_erase(dev, rw_offset, rw_size); | |
942 | if (ret) | |
943 | return ret; | |
944 | ||
945 | /* Write the image */ | |
946 | ret = cros_ec_flash_write(dev, image, rw_offset, image_size); | |
947 | if (ret) | |
948 | return ret; | |
949 | ||
950 | return 0; | |
951 | } | |
952 | ||
953 | int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block) | |
954 | { | |
955 | struct ec_params_vbnvcontext p; | |
956 | int len; | |
957 | ||
958 | p.op = EC_VBNV_CONTEXT_OP_READ; | |
959 | ||
960 | len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT, | |
961 | &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE); | |
962 | if (len < EC_VBNV_BLOCK_SIZE) | |
963 | return -1; | |
964 | ||
965 | return 0; | |
966 | } | |
967 | ||
968 | int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block) | |
969 | { | |
970 | struct ec_params_vbnvcontext p; | |
971 | int len; | |
972 | ||
973 | p.op = EC_VBNV_CONTEXT_OP_WRITE; | |
974 | memcpy(p.block, block, sizeof(p.block)); | |
975 | ||
976 | len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT, | |
977 | &p, sizeof(p), NULL, 0); | |
978 | if (len < 0) | |
979 | return -1; | |
980 | ||
981 | return 0; | |
982 | } | |
983 | ||
984 | int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state) | |
985 | { | |
986 | struct ec_params_ldo_set params; | |
987 | ||
988 | params.index = index; | |
989 | params.state = state; | |
990 | ||
991 | if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, | |
992 | ¶ms, sizeof(params), | |
993 | NULL, 0)) | |
994 | return -1; | |
995 | ||
996 | return 0; | |
997 | } | |
998 | ||
999 | int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state) | |
1000 | { | |
1001 | struct ec_params_ldo_get params; | |
1002 | struct ec_response_ldo_get *resp; | |
1003 | ||
1004 | params.index = index; | |
1005 | ||
1006 | if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, | |
1007 | ¶ms, sizeof(params), | |
2ab83f0d | 1008 | (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp)) |
88364387 HT |
1009 | return -1; |
1010 | ||
1011 | *state = resp->state; | |
1012 | ||
1013 | return 0; | |
1014 | } | |
1015 | ||
84d6cbd3 | 1016 | #ifndef CONFIG_DM_CROS_EC |
88364387 | 1017 | /** |
836bb6e8 SG |
1018 | * Decode EC interface details from the device tree and allocate a suitable |
1019 | * device. | |
88364387 HT |
1020 | * |
1021 | * @param blob Device tree blob | |
1022 | * @param node Node to decode from | |
1023 | * @param devp Returns a pointer to the new allocated device | |
1024 | * @return 0 if ok, -1 on error | |
1025 | */ | |
1026 | static int cros_ec_decode_fdt(const void *blob, int node, | |
1027 | struct cros_ec_dev **devp) | |
1028 | { | |
1029 | enum fdt_compat_id compat; | |
1030 | struct cros_ec_dev *dev; | |
1031 | int parent; | |
1032 | ||
1033 | /* See what type of parent we are inside (this is expensive) */ | |
1034 | parent = fdt_parent_offset(blob, node); | |
1035 | if (parent < 0) { | |
1036 | debug("%s: Cannot find node parent\n", __func__); | |
1037 | return -1; | |
1038 | } | |
1039 | ||
1040 | dev = &static_dev; | |
1041 | dev->node = node; | |
1042 | dev->parent_node = parent; | |
1043 | ||
1044 | compat = fdtdec_lookup(blob, parent); | |
1045 | switch (compat) { | |
1046 | #ifdef CONFIG_CROS_EC_SPI | |
1047 | case COMPAT_SAMSUNG_EXYNOS_SPI: | |
1048 | dev->interface = CROS_EC_IF_SPI; | |
1049 | if (cros_ec_spi_decode_fdt(dev, blob)) | |
1050 | return -1; | |
1051 | break; | |
1052 | #endif | |
1053 | #ifdef CONFIG_CROS_EC_I2C | |
1054 | case COMPAT_SAMSUNG_S3C2440_I2C: | |
1055 | dev->interface = CROS_EC_IF_I2C; | |
1056 | if (cros_ec_i2c_decode_fdt(dev, blob)) | |
1057 | return -1; | |
1058 | break; | |
1059 | #endif | |
1060 | #ifdef CONFIG_CROS_EC_LPC | |
1061 | case COMPAT_INTEL_LPC: | |
1062 | dev->interface = CROS_EC_IF_LPC; | |
1063 | break; | |
df93d90a SG |
1064 | #endif |
1065 | #ifdef CONFIG_CROS_EC_SANDBOX | |
1066 | case COMPAT_SANDBOX_HOST_EMULATION: | |
1067 | dev->interface = CROS_EC_IF_SANDBOX; | |
1068 | break; | |
88364387 HT |
1069 | #endif |
1070 | default: | |
1071 | debug("%s: Unknown compat id %d\n", __func__, compat); | |
1072 | return -1; | |
1073 | } | |
1074 | ||
1075 | fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int); | |
1076 | dev->optimise_flash_write = fdtdec_get_bool(blob, node, | |
1077 | "optimise-flash-write"); | |
1078 | *devp = dev; | |
1079 | ||
1080 | return 0; | |
1081 | } | |
84d6cbd3 | 1082 | #endif |
88364387 | 1083 | |
84d6cbd3 SG |
1084 | #ifdef CONFIG_DM_CROS_EC |
1085 | int cros_ec_register(struct udevice *dev) | |
88364387 | 1086 | { |
84d6cbd3 SG |
1087 | struct cros_ec_dev *cdev = dev->uclass_priv; |
1088 | const void *blob = gd->fdt_blob; | |
1089 | int node = dev->of_offset; | |
88364387 | 1090 | char id[MSG_BYTES]; |
84d6cbd3 SG |
1091 | |
1092 | cdev->dev = dev; | |
1093 | fdtdec_decode_gpio(blob, node, "ec-interrupt", &cdev->ec_int); | |
1094 | cdev->optimise_flash_write = fdtdec_get_bool(blob, node, | |
1095 | "optimise-flash-write"); | |
1096 | ||
1097 | /* we will poll the EC interrupt line */ | |
1098 | fdtdec_setup_gpio(&cdev->ec_int); | |
1099 | if (fdt_gpio_isvalid(&cdev->ec_int)) { | |
1100 | gpio_request(cdev->ec_int.gpio, "cros-ec-irq"); | |
1101 | gpio_direction_input(cdev->ec_int.gpio); | |
1102 | } | |
1103 | ||
1104 | if (cros_ec_check_version(cdev)) { | |
1105 | debug("%s: Could not detect CROS-EC version\n", __func__); | |
1106 | return -CROS_EC_ERR_CHECK_VERSION; | |
1107 | } | |
1108 | ||
1109 | if (cros_ec_read_id(cdev, id, sizeof(id))) { | |
1110 | debug("%s: Could not read KBC ID\n", __func__); | |
1111 | return -CROS_EC_ERR_READ_ID; | |
1112 | } | |
1113 | ||
1114 | /* Remember this device for use by the cros_ec command */ | |
1115 | debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id); | |
1116 | ||
1117 | return 0; | |
1118 | } | |
1119 | #else | |
1120 | int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp) | |
1121 | { | |
88364387 | 1122 | struct cros_ec_dev *dev; |
84d6cbd3 SG |
1123 | char id[MSG_BYTES]; |
1124 | #ifdef CONFIG_DM_CROS_EC | |
1125 | struct udevice *udev; | |
1126 | int ret; | |
1127 | ||
1128 | ret = uclass_find_device(UCLASS_CROS_EC, 0, &udev); | |
1129 | if (!ret) | |
1130 | device_remove(udev); | |
1131 | ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev); | |
1132 | if (ret) | |
1133 | return ret; | |
1134 | dev = udev->uclass_priv; | |
1135 | return 0; | |
1136 | #else | |
88364387 HT |
1137 | int node = 0; |
1138 | ||
1139 | *cros_ecp = NULL; | |
1140 | do { | |
1141 | node = fdtdec_next_compatible(blob, node, | |
1142 | COMPAT_GOOGLE_CROS_EC); | |
1143 | if (node < 0) { | |
1144 | debug("%s: Node not found\n", __func__); | |
1145 | return 0; | |
1146 | } | |
1147 | } while (!fdtdec_get_is_enabled(blob, node)); | |
1148 | ||
1149 | if (cros_ec_decode_fdt(blob, node, &dev)) { | |
1150 | debug("%s: Failed to decode device.\n", __func__); | |
1151 | return -CROS_EC_ERR_FDT_DECODE; | |
1152 | } | |
1153 | ||
1154 | switch (dev->interface) { | |
1155 | #ifdef CONFIG_CROS_EC_SPI | |
1156 | case CROS_EC_IF_SPI: | |
1157 | if (cros_ec_spi_init(dev, blob)) { | |
1158 | debug("%s: Could not setup SPI interface\n", __func__); | |
1159 | return -CROS_EC_ERR_DEV_INIT; | |
1160 | } | |
1161 | break; | |
1162 | #endif | |
1163 | #ifdef CONFIG_CROS_EC_I2C | |
1164 | case CROS_EC_IF_I2C: | |
1165 | if (cros_ec_i2c_init(dev, blob)) | |
1166 | return -CROS_EC_ERR_DEV_INIT; | |
1167 | break; | |
1168 | #endif | |
1169 | #ifdef CONFIG_CROS_EC_LPC | |
1170 | case CROS_EC_IF_LPC: | |
1171 | if (cros_ec_lpc_init(dev, blob)) | |
1172 | return -CROS_EC_ERR_DEV_INIT; | |
1173 | break; | |
df93d90a SG |
1174 | #endif |
1175 | #ifdef CONFIG_CROS_EC_SANDBOX | |
1176 | case CROS_EC_IF_SANDBOX: | |
1177 | if (cros_ec_sandbox_init(dev, blob)) | |
1178 | return -CROS_EC_ERR_DEV_INIT; | |
1179 | break; | |
88364387 HT |
1180 | #endif |
1181 | case CROS_EC_IF_NONE: | |
1182 | default: | |
1183 | return 0; | |
1184 | } | |
84d6cbd3 | 1185 | #endif |
88364387 HT |
1186 | |
1187 | /* we will poll the EC interrupt line */ | |
1188 | fdtdec_setup_gpio(&dev->ec_int); | |
84d6cbd3 SG |
1189 | if (fdt_gpio_isvalid(&dev->ec_int)) { |
1190 | gpio_request(dev->ec_int.gpio, "cros-ec-irq"); | |
88364387 | 1191 | gpio_direction_input(dev->ec_int.gpio); |
84d6cbd3 | 1192 | } |
88364387 HT |
1193 | |
1194 | if (cros_ec_check_version(dev)) { | |
1195 | debug("%s: Could not detect CROS-EC version\n", __func__); | |
1196 | return -CROS_EC_ERR_CHECK_VERSION; | |
1197 | } | |
1198 | ||
1199 | if (cros_ec_read_id(dev, id, sizeof(id))) { | |
1200 | debug("%s: Could not read KBC ID\n", __func__); | |
1201 | return -CROS_EC_ERR_READ_ID; | |
1202 | } | |
1203 | ||
1204 | /* Remember this device for use by the cros_ec command */ | |
84d6cbd3 SG |
1205 | *cros_ecp = dev; |
1206 | #ifndef CONFIG_DM_CROS_EC | |
1207 | last_dev = dev; | |
1208 | #endif | |
88364387 HT |
1209 | debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id); |
1210 | ||
1211 | return 0; | |
1212 | } | |
84d6cbd3 | 1213 | #endif |
88364387 | 1214 | |
88364387 HT |
1215 | int cros_ec_decode_region(int argc, char * const argv[]) |
1216 | { | |
1217 | if (argc > 0) { | |
1218 | if (0 == strcmp(*argv, "rw")) | |
1219 | return EC_FLASH_REGION_RW; | |
1220 | else if (0 == strcmp(*argv, "ro")) | |
1221 | return EC_FLASH_REGION_RO; | |
1222 | ||
1223 | debug("%s: Invalid region '%s'\n", __func__, *argv); | |
1224 | } else { | |
1225 | debug("%s: Missing region parameter\n", __func__); | |
1226 | } | |
1227 | ||
1228 | return -1; | |
1229 | } | |
1230 | ||
84d6cbd3 SG |
1231 | int cros_ec_decode_ec_flash(const void *blob, int node, |
1232 | struct fdt_cros_ec *config) | |
d7f25f35 | 1233 | { |
84d6cbd3 | 1234 | int flash_node; |
d7f25f35 SG |
1235 | |
1236 | flash_node = fdt_subnode_offset(blob, node, "flash"); | |
1237 | if (flash_node < 0) { | |
1238 | debug("Failed to find flash node\n"); | |
1239 | return -1; | |
1240 | } | |
1241 | ||
1242 | if (fdtdec_read_fmap_entry(blob, flash_node, "flash", | |
1243 | &config->flash)) { | |
1244 | debug("Failed to decode flash node in chrome-ec'\n"); | |
1245 | return -1; | |
1246 | } | |
1247 | ||
1248 | config->flash_erase_value = fdtdec_get_int(blob, flash_node, | |
1249 | "erase-value", -1); | |
1250 | for (node = fdt_first_subnode(blob, flash_node); node >= 0; | |
1251 | node = fdt_next_subnode(blob, node)) { | |
1252 | const char *name = fdt_get_name(blob, node, NULL); | |
1253 | enum ec_flash_region region; | |
1254 | ||
1255 | if (0 == strcmp(name, "ro")) { | |
1256 | region = EC_FLASH_REGION_RO; | |
1257 | } else if (0 == strcmp(name, "rw")) { | |
1258 | region = EC_FLASH_REGION_RW; | |
1259 | } else if (0 == strcmp(name, "wp-ro")) { | |
1260 | region = EC_FLASH_REGION_WP_RO; | |
1261 | } else { | |
1262 | debug("Unknown EC flash region name '%s'\n", name); | |
1263 | return -1; | |
1264 | } | |
1265 | ||
1266 | if (fdtdec_read_fmap_entry(blob, node, "reg", | |
1267 | &config->region[region])) { | |
1268 | debug("Failed to decode flash region in chrome-ec'\n"); | |
1269 | return -1; | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | return 0; | |
1274 | } | |
1275 | ||
b2a668b5 SG |
1276 | int cros_ec_i2c_xfer(struct cros_ec_dev *dev, uchar chip, uint addr, |
1277 | int alen, uchar *buffer, int len, int is_read) | |
1278 | { | |
1279 | union { | |
1280 | struct ec_params_i2c_passthru p; | |
1281 | uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE]; | |
1282 | } params; | |
1283 | union { | |
1284 | struct ec_response_i2c_passthru r; | |
1285 | uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE]; | |
1286 | } response; | |
1287 | struct ec_params_i2c_passthru *p = ¶ms.p; | |
1288 | struct ec_response_i2c_passthru *r = &response.r; | |
1289 | struct ec_params_i2c_passthru_msg *msg = p->msg; | |
1290 | uint8_t *pdata; | |
1291 | int read_len, write_len; | |
1292 | int size; | |
1293 | int rv; | |
1294 | ||
1295 | p->port = 0; | |
1296 | ||
1297 | if (alen != 1) { | |
1298 | printf("Unsupported address length %d\n", alen); | |
1299 | return -1; | |
1300 | } | |
1301 | if (is_read) { | |
1302 | read_len = len; | |
1303 | write_len = alen; | |
1304 | p->num_msgs = 2; | |
1305 | } else { | |
1306 | read_len = 0; | |
1307 | write_len = alen + len; | |
1308 | p->num_msgs = 1; | |
1309 | } | |
1310 | ||
1311 | size = sizeof(*p) + p->num_msgs * sizeof(*msg); | |
1312 | if (size + write_len > sizeof(params)) { | |
1313 | puts("Params too large for buffer\n"); | |
1314 | return -1; | |
1315 | } | |
1316 | if (sizeof(*r) + read_len > sizeof(response)) { | |
1317 | puts("Read length too big for buffer\n"); | |
1318 | return -1; | |
1319 | } | |
1320 | ||
1321 | /* Create a message to write the register address and optional data */ | |
1322 | pdata = (uint8_t *)p + size; | |
1323 | msg->addr_flags = chip; | |
1324 | msg->len = write_len; | |
1325 | pdata[0] = addr; | |
1326 | if (!is_read) | |
1327 | memcpy(pdata + 1, buffer, len); | |
1328 | msg++; | |
1329 | ||
1330 | if (read_len) { | |
1331 | msg->addr_flags = chip | EC_I2C_FLAG_READ; | |
1332 | msg->len = read_len; | |
1333 | } | |
1334 | ||
1335 | rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, size + write_len, | |
1336 | r, sizeof(*r) + read_len); | |
1337 | if (rv < 0) | |
1338 | return rv; | |
1339 | ||
1340 | /* Parse response */ | |
1341 | if (r->i2c_status & EC_I2C_STATUS_ERROR) { | |
1342 | printf("Transfer failed with status=0x%x\n", r->i2c_status); | |
1343 | return -1; | |
1344 | } | |
1345 | ||
1346 | if (rv < sizeof(*r) + read_len) { | |
1347 | puts("Truncated read response\n"); | |
1348 | return -1; | |
1349 | } | |
1350 | ||
1351 | if (read_len) | |
1352 | memcpy(buffer, r->data, read_len); | |
1353 | ||
1354 | return 0; | |
1355 | } | |
1356 | ||
1c266b92 SG |
1357 | #ifdef CONFIG_CMD_CROS_EC |
1358 | ||
88364387 HT |
1359 | /** |
1360 | * Perform a flash read or write command | |
1361 | * | |
1362 | * @param dev CROS-EC device to read/write | |
1363 | * @param is_write 1 do to a write, 0 to do a read | |
1364 | * @param argc Number of arguments | |
1365 | * @param argv Arguments (2 is region, 3 is address) | |
1366 | * @return 0 for ok, 1 for a usage error or -ve for ec command error | |
1367 | * (negative EC_RES_...) | |
1368 | */ | |
1369 | static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc, | |
1370 | char * const argv[]) | |
1371 | { | |
1372 | uint32_t offset, size = -1U, region_size; | |
1373 | unsigned long addr; | |
1374 | char *endp; | |
1375 | int region; | |
1376 | int ret; | |
1377 | ||
1378 | region = cros_ec_decode_region(argc - 2, argv + 2); | |
1379 | if (region == -1) | |
1380 | return 1; | |
1381 | if (argc < 4) | |
1382 | return 1; | |
1383 | addr = simple_strtoul(argv[3], &endp, 16); | |
1384 | if (*argv[3] == 0 || *endp != 0) | |
1385 | return 1; | |
1386 | if (argc > 4) { | |
1387 | size = simple_strtoul(argv[4], &endp, 16); | |
1388 | if (*argv[4] == 0 || *endp != 0) | |
1389 | return 1; | |
1390 | } | |
1391 | ||
1392 | ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size); | |
1393 | if (ret) { | |
1394 | debug("%s: Could not read region info\n", __func__); | |
1395 | return ret; | |
1396 | } | |
1397 | if (size == -1U) | |
1398 | size = region_size; | |
1399 | ||
1400 | ret = is_write ? | |
1401 | cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) : | |
1402 | cros_ec_flash_read(dev, (uint8_t *)addr, offset, size); | |
1403 | if (ret) { | |
1404 | debug("%s: Could not %s region\n", __func__, | |
1405 | is_write ? "write" : "read"); | |
1406 | return ret; | |
1407 | } | |
1408 | ||
1409 | return 0; | |
1410 | } | |
1411 | ||
b2a668b5 SG |
1412 | /** |
1413 | * get_alen() - Small parser helper function to get address length | |
1414 | * | |
1415 | * Returns the address length. | |
1416 | */ | |
1417 | static uint get_alen(char *arg) | |
1418 | { | |
1419 | int j; | |
1420 | int alen; | |
1421 | ||
1422 | alen = 1; | |
1423 | for (j = 0; j < 8; j++) { | |
1424 | if (arg[j] == '.') { | |
1425 | alen = arg[j+1] - '0'; | |
1426 | break; | |
1427 | } else if (arg[j] == '\0') { | |
1428 | break; | |
1429 | } | |
1430 | } | |
1431 | return alen; | |
1432 | } | |
1433 | ||
1434 | #define DISP_LINE_LEN 16 | |
1435 | ||
1436 | /* | |
1437 | * TODO([email protected]): This code copied almost verbatim from cmd_i2c.c | |
1438 | * so we can remove it later. | |
1439 | */ | |
1440 | static int cros_ec_i2c_md(struct cros_ec_dev *dev, int flag, int argc, | |
1441 | char * const argv[]) | |
1442 | { | |
1443 | u_char chip; | |
1444 | uint addr, alen, length = 0x10; | |
1445 | int j, nbytes, linebytes; | |
1446 | ||
1447 | if (argc < 2) | |
1448 | return CMD_RET_USAGE; | |
1449 | ||
1450 | if (1 || (flag & CMD_FLAG_REPEAT) == 0) { | |
1451 | /* | |
1452 | * New command specified. | |
1453 | */ | |
1454 | ||
1455 | /* | |
1456 | * I2C chip address | |
1457 | */ | |
1458 | chip = simple_strtoul(argv[0], NULL, 16); | |
1459 | ||
1460 | /* | |
1461 | * I2C data address within the chip. This can be 1 or | |
1462 | * 2 bytes long. Some day it might be 3 bytes long :-). | |
1463 | */ | |
1464 | addr = simple_strtoul(argv[1], NULL, 16); | |
1465 | alen = get_alen(argv[1]); | |
1466 | if (alen > 3) | |
1467 | return CMD_RET_USAGE; | |
1468 | ||
1469 | /* | |
1470 | * If another parameter, it is the length to display. | |
1471 | * Length is the number of objects, not number of bytes. | |
1472 | */ | |
1473 | if (argc > 2) | |
1474 | length = simple_strtoul(argv[2], NULL, 16); | |
1475 | } | |
1476 | ||
1477 | /* | |
1478 | * Print the lines. | |
1479 | * | |
1480 | * We buffer all read data, so we can make sure data is read only | |
1481 | * once. | |
1482 | */ | |
1483 | nbytes = length; | |
1484 | do { | |
1485 | unsigned char linebuf[DISP_LINE_LEN]; | |
1486 | unsigned char *cp; | |
1487 | ||
1488 | linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes; | |
1489 | ||
1490 | if (cros_ec_i2c_xfer(dev, chip, addr, alen, linebuf, linebytes, | |
1491 | 1)) | |
1492 | puts("Error reading the chip.\n"); | |
1493 | else { | |
1494 | printf("%04x:", addr); | |
1495 | cp = linebuf; | |
1496 | for (j = 0; j < linebytes; j++) { | |
1497 | printf(" %02x", *cp++); | |
1498 | addr++; | |
1499 | } | |
1500 | puts(" "); | |
1501 | cp = linebuf; | |
1502 | for (j = 0; j < linebytes; j++) { | |
1503 | if ((*cp < 0x20) || (*cp > 0x7e)) | |
1504 | puts("."); | |
1505 | else | |
1506 | printf("%c", *cp); | |
1507 | cp++; | |
1508 | } | |
1509 | putc('\n'); | |
1510 | } | |
1511 | nbytes -= linebytes; | |
1512 | } while (nbytes > 0); | |
1513 | ||
1514 | return 0; | |
1515 | } | |
1516 | ||
1517 | static int cros_ec_i2c_mw(struct cros_ec_dev *dev, int flag, int argc, | |
1518 | char * const argv[]) | |
1519 | { | |
1520 | uchar chip; | |
1521 | ulong addr; | |
1522 | uint alen; | |
1523 | uchar byte; | |
1524 | int count; | |
1525 | ||
1526 | if ((argc < 3) || (argc > 4)) | |
1527 | return CMD_RET_USAGE; | |
1528 | ||
1529 | /* | |
1530 | * Chip is always specified. | |
1531 | */ | |
1532 | chip = simple_strtoul(argv[0], NULL, 16); | |
1533 | ||
1534 | /* | |
1535 | * Address is always specified. | |
1536 | */ | |
1537 | addr = simple_strtoul(argv[1], NULL, 16); | |
1538 | alen = get_alen(argv[1]); | |
1539 | if (alen > 3) | |
1540 | return CMD_RET_USAGE; | |
1541 | ||
1542 | /* | |
1543 | * Value to write is always specified. | |
1544 | */ | |
1545 | byte = simple_strtoul(argv[2], NULL, 16); | |
1546 | ||
1547 | /* | |
1548 | * Optional count | |
1549 | */ | |
1550 | if (argc == 4) | |
1551 | count = simple_strtoul(argv[3], NULL, 16); | |
1552 | else | |
1553 | count = 1; | |
1554 | ||
1555 | while (count-- > 0) { | |
1556 | if (cros_ec_i2c_xfer(dev, chip, addr++, alen, &byte, 1, 0)) | |
1557 | puts("Error writing the chip.\n"); | |
1558 | /* | |
1559 | * Wait for the write to complete. The write can take | |
1560 | * up to 10mSec (we allow a little more time). | |
1561 | */ | |
1562 | /* | |
1563 | * No write delay with FRAM devices. | |
1564 | */ | |
1565 | #if !defined(CONFIG_SYS_I2C_FRAM) | |
1566 | udelay(11000); | |
1567 | #endif | |
1568 | } | |
1569 | ||
1570 | return 0; | |
1571 | } | |
1572 | ||
1573 | /* Temporary code until we have driver model and can use the i2c command */ | |
1574 | static int cros_ec_i2c_passthrough(struct cros_ec_dev *dev, int flag, | |
1575 | int argc, char * const argv[]) | |
1576 | { | |
1577 | const char *cmd; | |
1578 | ||
1579 | if (argc < 1) | |
1580 | return CMD_RET_USAGE; | |
1581 | cmd = *argv++; | |
1582 | argc--; | |
1583 | if (0 == strcmp("md", cmd)) | |
1584 | cros_ec_i2c_md(dev, flag, argc, argv); | |
1585 | else if (0 == strcmp("mw", cmd)) | |
1586 | cros_ec_i2c_mw(dev, flag, argc, argv); | |
1587 | else | |
1588 | return CMD_RET_USAGE; | |
1589 | ||
1590 | return 0; | |
1591 | } | |
1592 | ||
88364387 HT |
1593 | static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
1594 | { | |
84d6cbd3 SG |
1595 | struct cros_ec_dev *dev; |
1596 | #ifdef CONFIG_DM_CROS_EC | |
1597 | struct udevice *udev; | |
1598 | #endif | |
88364387 HT |
1599 | const char *cmd; |
1600 | int ret = 0; | |
1601 | ||
1602 | if (argc < 2) | |
1603 | return CMD_RET_USAGE; | |
1604 | ||
1605 | cmd = argv[1]; | |
1606 | if (0 == strcmp("init", cmd)) { | |
84d6cbd3 | 1607 | #ifndef CONFIG_DM_CROS_EC |
88364387 HT |
1608 | ret = cros_ec_init(gd->fdt_blob, &dev); |
1609 | if (ret) { | |
1610 | printf("Could not init cros_ec device (err %d)\n", ret); | |
1611 | return 1; | |
1612 | } | |
84d6cbd3 | 1613 | #endif |
88364387 HT |
1614 | return 0; |
1615 | } | |
1616 | ||
84d6cbd3 SG |
1617 | #ifdef CONFIG_DM_CROS_EC |
1618 | ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev); | |
1619 | if (ret) { | |
1620 | printf("Cannot get cros-ec device (err=%d)\n", ret); | |
1621 | return 1; | |
1622 | } | |
1623 | dev = udev->uclass_priv; | |
1624 | #else | |
88364387 HT |
1625 | /* Just use the last allocated device; there should be only one */ |
1626 | if (!last_dev) { | |
1627 | printf("No CROS-EC device available\n"); | |
1628 | return 1; | |
1629 | } | |
84d6cbd3 SG |
1630 | dev = last_dev; |
1631 | #endif | |
88364387 HT |
1632 | if (0 == strcmp("id", cmd)) { |
1633 | char id[MSG_BYTES]; | |
1634 | ||
1635 | if (cros_ec_read_id(dev, id, sizeof(id))) { | |
1636 | debug("%s: Could not read KBC ID\n", __func__); | |
1637 | return 1; | |
1638 | } | |
1639 | printf("%s\n", id); | |
1640 | } else if (0 == strcmp("info", cmd)) { | |
836bb6e8 | 1641 | struct ec_response_mkbp_info info; |
88364387 HT |
1642 | |
1643 | if (cros_ec_info(dev, &info)) { | |
1644 | debug("%s: Could not read KBC info\n", __func__); | |
1645 | return 1; | |
1646 | } | |
1647 | printf("rows = %u\n", info.rows); | |
1648 | printf("cols = %u\n", info.cols); | |
1649 | printf("switches = %#x\n", info.switches); | |
1650 | } else if (0 == strcmp("curimage", cmd)) { | |
1651 | enum ec_current_image image; | |
1652 | ||
1653 | if (cros_ec_read_current_image(dev, &image)) { | |
1654 | debug("%s: Could not read KBC image\n", __func__); | |
1655 | return 1; | |
1656 | } | |
1657 | printf("%d\n", image); | |
1658 | } else if (0 == strcmp("hash", cmd)) { | |
1659 | struct ec_response_vboot_hash hash; | |
1660 | int i; | |
1661 | ||
1662 | if (cros_ec_read_hash(dev, &hash)) { | |
1663 | debug("%s: Could not read KBC hash\n", __func__); | |
1664 | return 1; | |
1665 | } | |
1666 | ||
1667 | if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256) | |
1668 | printf("type: SHA-256\n"); | |
1669 | else | |
1670 | printf("type: %d\n", hash.hash_type); | |
1671 | ||
1672 | printf("offset: 0x%08x\n", hash.offset); | |
1673 | printf("size: 0x%08x\n", hash.size); | |
1674 | ||
1675 | printf("digest: "); | |
1676 | for (i = 0; i < hash.digest_size; i++) | |
1677 | printf("%02x", hash.hash_digest[i]); | |
1678 | printf("\n"); | |
1679 | } else if (0 == strcmp("reboot", cmd)) { | |
1680 | int region; | |
1681 | enum ec_reboot_cmd cmd; | |
1682 | ||
1683 | if (argc >= 3 && !strcmp(argv[2], "cold")) | |
1684 | cmd = EC_REBOOT_COLD; | |
1685 | else { | |
1686 | region = cros_ec_decode_region(argc - 2, argv + 2); | |
1687 | if (region == EC_FLASH_REGION_RO) | |
1688 | cmd = EC_REBOOT_JUMP_RO; | |
1689 | else if (region == EC_FLASH_REGION_RW) | |
1690 | cmd = EC_REBOOT_JUMP_RW; | |
1691 | else | |
1692 | return CMD_RET_USAGE; | |
1693 | } | |
1694 | ||
1695 | if (cros_ec_reboot(dev, cmd, 0)) { | |
1696 | debug("%s: Could not reboot KBC\n", __func__); | |
1697 | return 1; | |
1698 | } | |
1699 | } else if (0 == strcmp("events", cmd)) { | |
1700 | uint32_t events; | |
1701 | ||
1702 | if (cros_ec_get_host_events(dev, &events)) { | |
1703 | debug("%s: Could not read host events\n", __func__); | |
1704 | return 1; | |
1705 | } | |
1706 | printf("0x%08x\n", events); | |
1707 | } else if (0 == strcmp("clrevents", cmd)) { | |
1708 | uint32_t events = 0x7fffffff; | |
1709 | ||
1710 | if (argc >= 3) | |
1711 | events = simple_strtol(argv[2], NULL, 0); | |
1712 | ||
1713 | if (cros_ec_clear_host_events(dev, events)) { | |
1714 | debug("%s: Could not clear host events\n", __func__); | |
1715 | return 1; | |
1716 | } | |
1717 | } else if (0 == strcmp("read", cmd)) { | |
1718 | ret = do_read_write(dev, 0, argc, argv); | |
1719 | if (ret > 0) | |
1720 | return CMD_RET_USAGE; | |
1721 | } else if (0 == strcmp("write", cmd)) { | |
1722 | ret = do_read_write(dev, 1, argc, argv); | |
1723 | if (ret > 0) | |
1724 | return CMD_RET_USAGE; | |
1725 | } else if (0 == strcmp("erase", cmd)) { | |
1726 | int region = cros_ec_decode_region(argc - 2, argv + 2); | |
1727 | uint32_t offset, size; | |
1728 | ||
1729 | if (region == -1) | |
1730 | return CMD_RET_USAGE; | |
1731 | if (cros_ec_flash_offset(dev, region, &offset, &size)) { | |
1732 | debug("%s: Could not read region info\n", __func__); | |
1733 | ret = -1; | |
1734 | } else { | |
1735 | ret = cros_ec_flash_erase(dev, offset, size); | |
1736 | if (ret) { | |
1737 | debug("%s: Could not erase region\n", | |
1738 | __func__); | |
1739 | } | |
1740 | } | |
1741 | } else if (0 == strcmp("regioninfo", cmd)) { | |
1742 | int region = cros_ec_decode_region(argc - 2, argv + 2); | |
1743 | uint32_t offset, size; | |
1744 | ||
1745 | if (region == -1) | |
1746 | return CMD_RET_USAGE; | |
1747 | ret = cros_ec_flash_offset(dev, region, &offset, &size); | |
1748 | if (ret) { | |
1749 | debug("%s: Could not read region info\n", __func__); | |
1750 | } else { | |
1751 | printf("Region: %s\n", region == EC_FLASH_REGION_RO ? | |
1752 | "RO" : "RW"); | |
1753 | printf("Offset: %x\n", offset); | |
1754 | printf("Size: %x\n", size); | |
1755 | } | |
1756 | } else if (0 == strcmp("vbnvcontext", cmd)) { | |
1757 | uint8_t block[EC_VBNV_BLOCK_SIZE]; | |
1758 | char buf[3]; | |
1759 | int i, len; | |
1760 | unsigned long result; | |
1761 | ||
1762 | if (argc <= 2) { | |
1763 | ret = cros_ec_read_vbnvcontext(dev, block); | |
1764 | if (!ret) { | |
1765 | printf("vbnv_block: "); | |
1766 | for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) | |
1767 | printf("%02x", block[i]); | |
1768 | putc('\n'); | |
1769 | } | |
1770 | } else { | |
1771 | /* | |
1772 | * TODO(clchiou): Move this to a utility function as | |
1773 | * cmd_spi might want to call it. | |
1774 | */ | |
1775 | memset(block, 0, EC_VBNV_BLOCK_SIZE); | |
1776 | len = strlen(argv[2]); | |
1777 | buf[2] = '\0'; | |
1778 | for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) { | |
1779 | if (i * 2 >= len) | |
1780 | break; | |
1781 | buf[0] = argv[2][i * 2]; | |
1782 | if (i * 2 + 1 >= len) | |
1783 | buf[1] = '0'; | |
1784 | else | |
1785 | buf[1] = argv[2][i * 2 + 1]; | |
1786 | strict_strtoul(buf, 16, &result); | |
1787 | block[i] = result; | |
1788 | } | |
1789 | ret = cros_ec_write_vbnvcontext(dev, block); | |
1790 | } | |
1791 | if (ret) { | |
1792 | debug("%s: Could not %s VbNvContext\n", __func__, | |
1793 | argc <= 2 ? "read" : "write"); | |
1794 | } | |
1795 | } else if (0 == strcmp("test", cmd)) { | |
1796 | int result = cros_ec_test(dev); | |
1797 | ||
1798 | if (result) | |
1799 | printf("Test failed with error %d\n", result); | |
1800 | else | |
1801 | puts("Test passed\n"); | |
1802 | } else if (0 == strcmp("version", cmd)) { | |
1803 | struct ec_response_get_version *p; | |
1804 | char *build_string; | |
1805 | ||
1806 | ret = cros_ec_read_version(dev, &p); | |
1807 | if (!ret) { | |
1808 | /* Print versions */ | |
1809 | printf("RO version: %1.*s\n", | |
2ab83f0d | 1810 | (int)sizeof(p->version_string_ro), |
88364387 HT |
1811 | p->version_string_ro); |
1812 | printf("RW version: %1.*s\n", | |
2ab83f0d | 1813 | (int)sizeof(p->version_string_rw), |
88364387 HT |
1814 | p->version_string_rw); |
1815 | printf("Firmware copy: %s\n", | |
1816 | (p->current_image < | |
1817 | ARRAY_SIZE(ec_current_image_name) ? | |
1818 | ec_current_image_name[p->current_image] : | |
1819 | "?")); | |
1820 | ret = cros_ec_read_build_info(dev, &build_string); | |
1821 | if (!ret) | |
1822 | printf("Build info: %s\n", build_string); | |
1823 | } | |
1824 | } else if (0 == strcmp("ldo", cmd)) { | |
1825 | uint8_t index, state; | |
1826 | char *endp; | |
1827 | ||
1828 | if (argc < 3) | |
1829 | return CMD_RET_USAGE; | |
1830 | index = simple_strtoul(argv[2], &endp, 10); | |
1831 | if (*argv[2] == 0 || *endp != 0) | |
1832 | return CMD_RET_USAGE; | |
1833 | if (argc > 3) { | |
1834 | state = simple_strtoul(argv[3], &endp, 10); | |
1835 | if (*argv[3] == 0 || *endp != 0) | |
1836 | return CMD_RET_USAGE; | |
1837 | ret = cros_ec_set_ldo(dev, index, state); | |
1838 | } else { | |
1839 | ret = cros_ec_get_ldo(dev, index, &state); | |
1840 | if (!ret) { | |
1841 | printf("LDO%d: %s\n", index, | |
1842 | state == EC_LDO_STATE_ON ? | |
1843 | "on" : "off"); | |
1844 | } | |
1845 | } | |
1846 | ||
1847 | if (ret) { | |
1848 | debug("%s: Could not access LDO%d\n", __func__, index); | |
1849 | return ret; | |
1850 | } | |
b2a668b5 SG |
1851 | } else if (0 == strcmp("i2c", cmd)) { |
1852 | ret = cros_ec_i2c_passthrough(dev, flag, argc - 2, argv + 2); | |
88364387 HT |
1853 | } else { |
1854 | return CMD_RET_USAGE; | |
1855 | } | |
1856 | ||
1857 | if (ret < 0) { | |
1858 | printf("Error: CROS-EC command failed (error %d)\n", ret); | |
1859 | ret = 1; | |
1860 | } | |
1861 | ||
1862 | return ret; | |
1863 | } | |
1864 | ||
1865 | U_BOOT_CMD( | |
b2a668b5 | 1866 | crosec, 6, 1, do_cros_ec, |
88364387 HT |
1867 | "CROS-EC utility command", |
1868 | "init Re-init CROS-EC (done on startup automatically)\n" | |
1869 | "crosec id Read CROS-EC ID\n" | |
1870 | "crosec info Read CROS-EC info\n" | |
1871 | "crosec curimage Read CROS-EC current image\n" | |
1872 | "crosec hash Read CROS-EC hash\n" | |
1873 | "crosec reboot [rw | ro | cold] Reboot CROS-EC\n" | |
1874 | "crosec events Read CROS-EC host events\n" | |
1875 | "crosec clrevents [mask] Clear CROS-EC host events\n" | |
1876 | "crosec regioninfo <ro|rw> Read image info\n" | |
1877 | "crosec erase <ro|rw> Erase EC image\n" | |
1878 | "crosec read <ro|rw> <addr> [<size>] Read EC image\n" | |
1879 | "crosec write <ro|rw> <addr> [<size>] Write EC image\n" | |
1880 | "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n" | |
1881 | "crosec ldo <idx> [<state>] Switch/Read LDO state\n" | |
1882 | "crosec test run tests on cros_ec\n" | |
b2a668b5 SG |
1883 | "crosec version Read CROS-EC version\n" |
1884 | "crosec i2c md chip address[.0, .1, .2] [# of objects] - read from I2C passthru\n" | |
1885 | "crosec i2c mw chip address[.0, .1, .2] value [count] - write to I2C passthru (fill)" | |
88364387 HT |
1886 | ); |
1887 | #endif | |
84d6cbd3 SG |
1888 | |
1889 | #ifdef CONFIG_DM_CROS_EC | |
1890 | UCLASS_DRIVER(cros_ec) = { | |
1891 | .id = UCLASS_CROS_EC, | |
1892 | .name = "cros_ec", | |
1893 | .per_device_auto_alloc_size = sizeof(struct cros_ec_dev), | |
1894 | }; | |
1895 | #endif |