Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Chromium OS cros_ec driver |
| 3 | * |
| 4 | * Copyright (c) 2012 The Chromium OS Authors. |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
Simon Glass | 836bb6e | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 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. |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <common.h> |
| 18 | #include <command.h> |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 19 | #include <dm.h> |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 20 | #include <i2c.h> |
| 21 | #include <cros_ec.h> |
| 22 | #include <fdtdec.h> |
| 23 | #include <malloc.h> |
| 24 | #include <spi.h> |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 25 | #include <asm/errno.h> |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 26 | #include <asm/io.h> |
| 27 | #include <asm-generic/gpio.h> |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 28 | #include <dm/device-internal.h> |
Simon Glass | cc456bd | 2015-08-03 08:19:23 -0600 | [diff] [blame] | 29 | #include <dm/root.h> |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 30 | #include <dm/uclass-internal.h> |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 31 | |
| 32 | #ifdef DEBUG_TRACE |
| 33 | #define debug_trace(fmt, b...) debug(fmt, #b) |
| 34 | #else |
| 35 | #define debug_trace(fmt, b...) |
| 36 | #endif |
| 37 | |
| 38 | enum { |
| 39 | /* Timeout waiting for a flash erase command to complete */ |
| 40 | CROS_EC_CMD_TIMEOUT_MS = 5000, |
| 41 | /* Timeout waiting for a synchronous hash to be recomputed */ |
| 42 | CROS_EC_CMD_HASH_TIMEOUT_MS = 2000, |
| 43 | }; |
| 44 | |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 45 | DECLARE_GLOBAL_DATA_PTR; |
| 46 | |
| 47 | /* Note: depends on enum ec_current_image */ |
| 48 | static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"}; |
| 49 | |
| 50 | void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len) |
| 51 | { |
| 52 | #ifdef DEBUG |
| 53 | int i; |
| 54 | |
| 55 | printf("%s: ", name); |
| 56 | if (cmd != -1) |
| 57 | printf("cmd=%#x: ", cmd); |
| 58 | for (i = 0; i < len; i++) |
| 59 | printf("%02x ", data[i]); |
| 60 | printf("\n"); |
| 61 | #endif |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Calculate a simple 8-bit checksum of a data block |
| 66 | * |
| 67 | * @param data Data block to checksum |
| 68 | * @param size Size of data block in bytes |
| 69 | * @return checksum value (0 to 255) |
| 70 | */ |
| 71 | int cros_ec_calc_checksum(const uint8_t *data, int size) |
| 72 | { |
| 73 | int csum, i; |
| 74 | |
| 75 | for (i = csum = 0; i < size; i++) |
| 76 | csum += data[i]; |
| 77 | return csum & 0xff; |
| 78 | } |
| 79 | |
Simon Glass | 2d8ede5 | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Create a request packet for protocol version 3. |
| 82 | * |
| 83 | * The packet is stored in the device's internal output buffer. |
| 84 | * |
| 85 | * @param dev CROS-EC device |
| 86 | * @param cmd Command to send (EC_CMD_...) |
| 87 | * @param cmd_version Version of command to send (EC_VER_...) |
| 88 | * @param dout Output data (may be NULL If dout_len=0) |
| 89 | * @param dout_len Size of output data in bytes |
| 90 | * @return packet size in bytes, or <0 if error. |
| 91 | */ |
| 92 | static int create_proto3_request(struct cros_ec_dev *dev, |
| 93 | int cmd, int cmd_version, |
| 94 | const void *dout, int dout_len) |
| 95 | { |
| 96 | struct ec_host_request *rq = (struct ec_host_request *)dev->dout; |
| 97 | int out_bytes = dout_len + sizeof(*rq); |
| 98 | |
| 99 | /* Fail if output size is too big */ |
| 100 | if (out_bytes > (int)sizeof(dev->dout)) { |
| 101 | debug("%s: Cannot send %d bytes\n", __func__, dout_len); |
| 102 | return -EC_RES_REQUEST_TRUNCATED; |
| 103 | } |
| 104 | |
| 105 | /* Fill in request packet */ |
| 106 | rq->struct_version = EC_HOST_REQUEST_VERSION; |
| 107 | rq->checksum = 0; |
| 108 | rq->command = cmd; |
| 109 | rq->command_version = cmd_version; |
| 110 | rq->reserved = 0; |
| 111 | rq->data_len = dout_len; |
| 112 | |
| 113 | /* Copy data after header */ |
| 114 | memcpy(rq + 1, dout, dout_len); |
| 115 | |
| 116 | /* Write checksum field so the entire packet sums to 0 */ |
| 117 | rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes)); |
| 118 | |
| 119 | cros_ec_dump_data("out", cmd, dev->dout, out_bytes); |
| 120 | |
| 121 | /* Return size of request packet */ |
| 122 | return out_bytes; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Prepare the device to receive a protocol version 3 response. |
| 127 | * |
| 128 | * @param dev CROS-EC device |
| 129 | * @param din_len Maximum size of response in bytes |
| 130 | * @return maximum expected number of bytes in response, or <0 if error. |
| 131 | */ |
| 132 | static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len) |
| 133 | { |
| 134 | int in_bytes = din_len + sizeof(struct ec_host_response); |
| 135 | |
| 136 | /* Fail if input size is too big */ |
| 137 | if (in_bytes > (int)sizeof(dev->din)) { |
| 138 | debug("%s: Cannot receive %d bytes\n", __func__, din_len); |
| 139 | return -EC_RES_RESPONSE_TOO_BIG; |
| 140 | } |
| 141 | |
| 142 | /* Return expected size of response packet */ |
| 143 | return in_bytes; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Handle a protocol version 3 response packet. |
| 148 | * |
| 149 | * The packet must already be stored in the device's internal input buffer. |
| 150 | * |
| 151 | * @param dev CROS-EC device |
| 152 | * @param dinp Returns pointer to response data |
| 153 | * @param din_len Maximum size of response in bytes |
Simon Glass | 8bbb38b | 2015-01-25 08:27:17 -0700 | [diff] [blame] | 154 | * @return number of bytes of response data, or <0 if error. Note that error |
| 155 | * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they |
| 156 | * overlap!) |
Simon Glass | 2d8ede5 | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 157 | */ |
| 158 | static int handle_proto3_response(struct cros_ec_dev *dev, |
| 159 | uint8_t **dinp, int din_len) |
| 160 | { |
| 161 | struct ec_host_response *rs = (struct ec_host_response *)dev->din; |
| 162 | int in_bytes; |
| 163 | int csum; |
| 164 | |
| 165 | cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs)); |
| 166 | |
| 167 | /* Check input data */ |
| 168 | if (rs->struct_version != EC_HOST_RESPONSE_VERSION) { |
| 169 | debug("%s: EC response version mismatch\n", __func__); |
| 170 | return -EC_RES_INVALID_RESPONSE; |
| 171 | } |
| 172 | |
| 173 | if (rs->reserved) { |
| 174 | debug("%s: EC response reserved != 0\n", __func__); |
| 175 | return -EC_RES_INVALID_RESPONSE; |
| 176 | } |
| 177 | |
| 178 | if (rs->data_len > din_len) { |
| 179 | debug("%s: EC returned too much data\n", __func__); |
| 180 | return -EC_RES_RESPONSE_TOO_BIG; |
| 181 | } |
| 182 | |
| 183 | cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len); |
| 184 | |
| 185 | /* Update in_bytes to actual data size */ |
| 186 | in_bytes = sizeof(*rs) + rs->data_len; |
| 187 | |
| 188 | /* Verify checksum */ |
| 189 | csum = cros_ec_calc_checksum(dev->din, in_bytes); |
| 190 | if (csum) { |
| 191 | debug("%s: EC response checksum invalid: 0x%02x\n", __func__, |
| 192 | csum); |
| 193 | return -EC_RES_INVALID_CHECKSUM; |
| 194 | } |
| 195 | |
| 196 | /* Return error result, if any */ |
| 197 | if (rs->result) |
| 198 | return -(int)rs->result; |
| 199 | |
| 200 | /* If we're still here, set response data pointer and return length */ |
| 201 | *dinp = (uint8_t *)(rs + 1); |
| 202 | |
| 203 | return rs->data_len; |
| 204 | } |
| 205 | |
| 206 | static int send_command_proto3(struct cros_ec_dev *dev, |
| 207 | int cmd, int cmd_version, |
| 208 | const void *dout, int dout_len, |
| 209 | uint8_t **dinp, int din_len) |
| 210 | { |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 211 | struct dm_cros_ec_ops *ops; |
Simon Glass | 2d8ede5 | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 212 | int out_bytes, in_bytes; |
| 213 | int rv; |
| 214 | |
| 215 | /* Create request packet */ |
| 216 | out_bytes = create_proto3_request(dev, cmd, cmd_version, |
| 217 | dout, dout_len); |
| 218 | if (out_bytes < 0) |
| 219 | return out_bytes; |
| 220 | |
| 221 | /* Prepare response buffer */ |
| 222 | in_bytes = prepare_proto3_response_buffer(dev, din_len); |
| 223 | if (in_bytes < 0) |
| 224 | return in_bytes; |
| 225 | |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 226 | ops = dm_cros_ec_get_ops(dev->dev); |
Simon Glass | 8bbb38b | 2015-01-25 08:27:17 -0700 | [diff] [blame] | 227 | rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS; |
Simon Glass | 2d8ede5 | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 228 | if (rv < 0) |
| 229 | return rv; |
| 230 | |
| 231 | /* Process the response */ |
| 232 | return handle_proto3_response(dev, dinp, din_len); |
| 233 | } |
| 234 | |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 235 | static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version, |
| 236 | const void *dout, int dout_len, |
| 237 | uint8_t **dinp, int din_len) |
| 238 | { |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 239 | struct dm_cros_ec_ops *ops; |
Simon Glass | 2d8ede5 | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 240 | int ret = -1; |
| 241 | |
| 242 | /* Handle protocol version 3 support */ |
| 243 | if (dev->protocol_version == 3) { |
| 244 | return send_command_proto3(dev, cmd, cmd_version, |
| 245 | dout, dout_len, dinp, din_len); |
| 246 | } |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 247 | |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 248 | ops = dm_cros_ec_get_ops(dev->dev); |
| 249 | ret = ops->command(dev->dev, cmd, cmd_version, |
| 250 | (const uint8_t *)dout, dout_len, dinp, din_len); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Send a command to the CROS-EC device and return the reply. |
| 257 | * |
| 258 | * The device's internal input/output buffers are used. |
| 259 | * |
| 260 | * @param dev CROS-EC device |
| 261 | * @param cmd Command to send (EC_CMD_...) |
| 262 | * @param cmd_version Version of command to send (EC_VER_...) |
| 263 | * @param dout Output data (may be NULL If dout_len=0) |
| 264 | * @param dout_len Size of output data in bytes |
| 265 | * @param dinp Response data (may be NULL If din_len=0). |
| 266 | * If not NULL, it will be updated to point to the data |
| 267 | * and will always be double word aligned (64-bits) |
| 268 | * @param din_len Maximum size of response in bytes |
Simon Glass | 8bbb38b | 2015-01-25 08:27:17 -0700 | [diff] [blame] | 269 | * @return number of bytes in response, or -ve on error |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 270 | */ |
| 271 | static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd, |
| 272 | int cmd_version, const void *dout, int dout_len, uint8_t **dinp, |
| 273 | int din_len) |
| 274 | { |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 275 | uint8_t *din = NULL; |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 276 | int len; |
| 277 | |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 278 | len = send_command(dev, cmd, cmd_version, dout, dout_len, |
| 279 | &din, din_len); |
| 280 | |
| 281 | /* If the command doesn't complete, wait a while */ |
| 282 | if (len == -EC_RES_IN_PROGRESS) { |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 283 | struct ec_response_get_comms_status *resp = NULL; |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 284 | ulong start; |
| 285 | |
| 286 | /* Wait for command to complete */ |
| 287 | start = get_timer(0); |
| 288 | do { |
| 289 | int ret; |
| 290 | |
| 291 | mdelay(50); /* Insert some reasonable delay */ |
| 292 | ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0, |
| 293 | NULL, 0, |
| 294 | (uint8_t **)&resp, sizeof(*resp)); |
| 295 | if (ret < 0) |
| 296 | return ret; |
| 297 | |
| 298 | if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) { |
| 299 | debug("%s: Command %#02x timeout\n", |
| 300 | __func__, cmd); |
| 301 | return -EC_RES_TIMEOUT; |
| 302 | } |
| 303 | } while (resp->flags & EC_COMMS_STATUS_PROCESSING); |
| 304 | |
| 305 | /* OK it completed, so read the status response */ |
| 306 | /* not sure why it was 0 for the last argument */ |
| 307 | len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0, |
| 308 | NULL, 0, &din, din_len); |
| 309 | } |
| 310 | |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 311 | debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp, |
| 312 | dinp ? *dinp : NULL); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 313 | if (dinp) { |
| 314 | /* If we have any data to return, it must be 64bit-aligned */ |
| 315 | assert(len <= 0 || !((uintptr_t)din & 7)); |
| 316 | *dinp = din; |
| 317 | } |
| 318 | |
| 319 | return len; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Send a command to the CROS-EC device and return the reply. |
| 324 | * |
| 325 | * The device's internal input/output buffers are used. |
| 326 | * |
| 327 | * @param dev CROS-EC device |
| 328 | * @param cmd Command to send (EC_CMD_...) |
| 329 | * @param cmd_version Version of command to send (EC_VER_...) |
| 330 | * @param dout Output data (may be NULL If dout_len=0) |
| 331 | * @param dout_len Size of output data in bytes |
| 332 | * @param din Response data (may be NULL If din_len=0). |
| 333 | * It not NULL, it is a place for ec_command() to copy the |
| 334 | * data to. |
| 335 | * @param din_len Maximum size of response in bytes |
Simon Glass | 8bbb38b | 2015-01-25 08:27:17 -0700 | [diff] [blame] | 336 | * @return number of bytes in response, or -ve on error |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 337 | */ |
| 338 | static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version, |
| 339 | const void *dout, int dout_len, |
| 340 | void *din, int din_len) |
| 341 | { |
| 342 | uint8_t *in_buffer; |
| 343 | int len; |
| 344 | |
| 345 | assert((din_len == 0) || din); |
| 346 | len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len, |
| 347 | &in_buffer, din_len); |
| 348 | if (len > 0) { |
| 349 | /* |
| 350 | * If we were asked to put it somewhere, do so, otherwise just |
| 351 | * disregard the result. |
| 352 | */ |
| 353 | if (din && in_buffer) { |
| 354 | assert(len <= din_len); |
| 355 | memmove(din, in_buffer, len); |
| 356 | } |
| 357 | } |
| 358 | return len; |
| 359 | } |
| 360 | |
| 361 | int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan) |
| 362 | { |
Simon Glass | 836bb6e | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 363 | if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan, |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 364 | sizeof(scan->data)) != sizeof(scan->data)) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 365 | return -1; |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen) |
| 371 | { |
| 372 | struct ec_response_get_version *r; |
| 373 | |
| 374 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 375 | (uint8_t **)&r, sizeof(*r)) != sizeof(*r)) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 376 | return -1; |
| 377 | |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 378 | if (maxlen > (int)sizeof(r->version_string_ro)) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 379 | maxlen = sizeof(r->version_string_ro); |
| 380 | |
| 381 | switch (r->current_image) { |
| 382 | case EC_IMAGE_RO: |
| 383 | memcpy(id, r->version_string_ro, maxlen); |
| 384 | break; |
| 385 | case EC_IMAGE_RW: |
| 386 | memcpy(id, r->version_string_rw, maxlen); |
| 387 | break; |
| 388 | default: |
| 389 | return -1; |
| 390 | } |
| 391 | |
| 392 | id[maxlen - 1] = '\0'; |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | int cros_ec_read_version(struct cros_ec_dev *dev, |
| 397 | struct ec_response_get_version **versionp) |
| 398 | { |
| 399 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, |
| 400 | (uint8_t **)versionp, sizeof(**versionp)) |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 401 | != sizeof(**versionp)) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 402 | return -1; |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp) |
| 408 | { |
| 409 | if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0, |
Simon Glass | 836bb6e | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 410 | (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 411 | return -1; |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | int cros_ec_read_current_image(struct cros_ec_dev *dev, |
| 417 | enum ec_current_image *image) |
| 418 | { |
| 419 | struct ec_response_get_version *r; |
| 420 | |
| 421 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 422 | (uint8_t **)&r, sizeof(*r)) != sizeof(*r)) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 423 | return -1; |
| 424 | |
| 425 | *image = r->current_image; |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev, |
| 430 | struct ec_response_vboot_hash *hash) |
| 431 | { |
| 432 | struct ec_params_vboot_hash p; |
| 433 | ulong start; |
| 434 | |
| 435 | start = get_timer(0); |
| 436 | while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) { |
| 437 | mdelay(50); /* Insert some reasonable delay */ |
| 438 | |
| 439 | p.cmd = EC_VBOOT_HASH_GET; |
| 440 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
| 441 | hash, sizeof(*hash)) < 0) |
| 442 | return -1; |
| 443 | |
| 444 | if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) { |
| 445 | debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__); |
| 446 | return -EC_RES_TIMEOUT; |
| 447 | } |
| 448 | } |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | int cros_ec_read_hash(struct cros_ec_dev *dev, |
| 454 | struct ec_response_vboot_hash *hash) |
| 455 | { |
| 456 | struct ec_params_vboot_hash p; |
| 457 | int rv; |
| 458 | |
| 459 | p.cmd = EC_VBOOT_HASH_GET; |
| 460 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
| 461 | hash, sizeof(*hash)) < 0) |
| 462 | return -1; |
| 463 | |
| 464 | /* If the EC is busy calculating the hash, fidget until it's done. */ |
| 465 | rv = cros_ec_wait_on_hash_done(dev, hash); |
| 466 | if (rv) |
| 467 | return rv; |
| 468 | |
| 469 | /* If the hash is valid, we're done. Otherwise, we have to kick it off |
| 470 | * again and wait for it to complete. Note that we explicitly assume |
| 471 | * that hashing zero bytes is always wrong, even though that would |
| 472 | * produce a valid hash value. */ |
| 473 | if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size) |
| 474 | return 0; |
| 475 | |
| 476 | debug("%s: No valid hash (status=%d size=%d). Compute one...\n", |
| 477 | __func__, hash->status, hash->size); |
| 478 | |
Simon Glass | 836bb6e | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 479 | p.cmd = EC_VBOOT_HASH_START; |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 480 | p.hash_type = EC_VBOOT_HASH_TYPE_SHA256; |
| 481 | p.nonce_size = 0; |
| 482 | p.offset = EC_VBOOT_HASH_OFFSET_RW; |
| 483 | |
| 484 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
| 485 | hash, sizeof(*hash)) < 0) |
| 486 | return -1; |
| 487 | |
| 488 | rv = cros_ec_wait_on_hash_done(dev, hash); |
| 489 | if (rv) |
| 490 | return rv; |
| 491 | |
| 492 | debug("%s: hash done\n", __func__); |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | static int cros_ec_invalidate_hash(struct cros_ec_dev *dev) |
| 498 | { |
| 499 | struct ec_params_vboot_hash p; |
| 500 | struct ec_response_vboot_hash *hash; |
| 501 | |
| 502 | /* We don't have an explict command for the EC to discard its current |
| 503 | * hash value, so we'll just tell it to calculate one that we know is |
| 504 | * wrong (we claim that hashing zero bytes is always invalid). |
| 505 | */ |
| 506 | p.cmd = EC_VBOOT_HASH_RECALC; |
| 507 | p.hash_type = EC_VBOOT_HASH_TYPE_SHA256; |
| 508 | p.nonce_size = 0; |
| 509 | p.offset = 0; |
| 510 | p.size = 0; |
| 511 | |
| 512 | debug("%s:\n", __func__); |
| 513 | |
| 514 | if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
| 515 | (uint8_t **)&hash, sizeof(*hash)) < 0) |
| 516 | return -1; |
| 517 | |
| 518 | /* No need to wait for it to finish */ |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd, |
| 523 | uint8_t flags) |
| 524 | { |
| 525 | struct ec_params_reboot_ec p; |
| 526 | |
| 527 | p.cmd = cmd; |
| 528 | p.flags = flags; |
| 529 | |
| 530 | if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0) |
| 531 | < 0) |
| 532 | return -1; |
| 533 | |
| 534 | if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) { |
| 535 | /* |
| 536 | * EC reboot will take place immediately so delay to allow it |
| 537 | * to complete. Note that some reboot types (EC_REBOOT_COLD) |
| 538 | * will reboot the AP as well, in which case we won't actually |
| 539 | * get to this point. |
| 540 | */ |
| 541 | /* |
| 542 | * TODO(rspangler@chromium.org): Would be nice if we had a |
| 543 | * better way to determine when the reboot is complete. Could |
| 544 | * we poll a memory-mapped LPC value? |
| 545 | */ |
| 546 | udelay(50000); |
| 547 | } |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | int cros_ec_interrupt_pending(struct cros_ec_dev *dev) |
| 553 | { |
| 554 | /* no interrupt support : always poll */ |
Simon Glass | 32f8a19 | 2015-01-05 20:05:32 -0700 | [diff] [blame] | 555 | if (!dm_gpio_is_valid(&dev->ec_int)) |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 556 | return -ENOENT; |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 557 | |
Simon Glass | 32f8a19 | 2015-01-05 20:05:32 -0700 | [diff] [blame] | 558 | return dm_gpio_get_value(&dev->ec_int); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 559 | } |
| 560 | |
Simon Glass | 836bb6e | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 561 | int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 562 | { |
Simon Glass | 836bb6e | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 563 | if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info, |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 564 | sizeof(*info)) != sizeof(*info)) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 565 | return -1; |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr) |
| 571 | { |
| 572 | struct ec_response_host_event_mask *resp; |
| 573 | |
| 574 | /* |
| 575 | * Use the B copy of the event flags, because the main copy is already |
| 576 | * used by ACPI/SMI. |
| 577 | */ |
| 578 | if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0, |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 579 | (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp)) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 580 | return -1; |
| 581 | |
| 582 | if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID)) |
| 583 | return -1; |
| 584 | |
| 585 | *events_ptr = resp->mask; |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events) |
| 590 | { |
| 591 | struct ec_params_host_event_mask params; |
| 592 | |
| 593 | params.mask = events; |
| 594 | |
| 595 | /* |
| 596 | * Use the B copy of the event flags, so it affects the data returned |
| 597 | * by cros_ec_get_host_events(). |
| 598 | */ |
| 599 | if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0, |
| 600 | ¶ms, sizeof(params), NULL, 0) < 0) |
| 601 | return -1; |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | int cros_ec_flash_protect(struct cros_ec_dev *dev, |
| 607 | uint32_t set_mask, uint32_t set_flags, |
| 608 | struct ec_response_flash_protect *resp) |
| 609 | { |
| 610 | struct ec_params_flash_protect params; |
| 611 | |
| 612 | params.mask = set_mask; |
| 613 | params.flags = set_flags; |
| 614 | |
| 615 | if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT, |
| 616 | ¶ms, sizeof(params), |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 617 | resp, sizeof(*resp)) != sizeof(*resp)) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 618 | return -1; |
| 619 | |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | static int cros_ec_check_version(struct cros_ec_dev *dev) |
| 624 | { |
| 625 | struct ec_params_hello req; |
| 626 | struct ec_response_hello *resp; |
| 627 | |
Simon Glass | 72a38e0 | 2015-03-26 09:29:30 -0600 | [diff] [blame] | 628 | struct dm_cros_ec_ops *ops; |
| 629 | int ret; |
| 630 | |
| 631 | ops = dm_cros_ec_get_ops(dev->dev); |
| 632 | if (ops->check_version) { |
| 633 | ret = ops->check_version(dev->dev); |
| 634 | if (ret) |
| 635 | return ret; |
| 636 | } |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * TODO(sjg@chromium.org). |
| 640 | * There is a strange oddity here with the EC. We could just ignore |
| 641 | * the response, i.e. pass the last two parameters as NULL and 0. |
| 642 | * In this case we won't read back very many bytes from the EC. |
| 643 | * On the I2C bus the EC gets upset about this and will try to send |
| 644 | * the bytes anyway. This means that we will have to wait for that |
| 645 | * to complete before continuing with a new EC command. |
| 646 | * |
| 647 | * This problem is probably unique to the I2C bus. |
| 648 | * |
| 649 | * So for now, just read all the data anyway. |
| 650 | */ |
Randall Spangler | e8c1266 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 651 | |
Randall Spangler | a607028 | 2014-02-27 13:26:10 -0700 | [diff] [blame] | 652 | /* Try sending a version 3 packet */ |
| 653 | dev->protocol_version = 3; |
Simon Glass | d11e8fd | 2014-11-11 18:06:30 -0700 | [diff] [blame] | 654 | req.in_data = 0; |
Randall Spangler | a607028 | 2014-02-27 13:26:10 -0700 | [diff] [blame] | 655 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
| 656 | (uint8_t **)&resp, sizeof(*resp)) > 0) { |
| 657 | return 0; |
| 658 | } |
| 659 | |
Randall Spangler | e8c1266 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 660 | /* Try sending a version 2 packet */ |
| 661 | dev->protocol_version = 2; |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 662 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
| 663 | (uint8_t **)&resp, sizeof(*resp)) > 0) { |
Randall Spangler | e8c1266 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 664 | return 0; |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 665 | } |
| 666 | |
Randall Spangler | e8c1266 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 667 | /* |
| 668 | * Fail if we're still here, since the EC doesn't understand any |
| 669 | * protcol version we speak. Version 1 interface without command |
| 670 | * version is no longer supported, and we don't know about any new |
| 671 | * protocol versions. |
| 672 | */ |
| 673 | dev->protocol_version = 0; |
| 674 | printf("%s: ERROR: old EC interface not supported\n", __func__); |
| 675 | return -1; |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | int cros_ec_test(struct cros_ec_dev *dev) |
| 679 | { |
| 680 | struct ec_params_hello req; |
| 681 | struct ec_response_hello *resp; |
| 682 | |
| 683 | req.in_data = 0x12345678; |
| 684 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
| 685 | (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) { |
| 686 | printf("ec_command_inptr() returned error\n"); |
| 687 | return -1; |
| 688 | } |
| 689 | if (resp->out_data != req.in_data + 0x01020304) { |
| 690 | printf("Received invalid handshake %x\n", resp->out_data); |
| 691 | return -1; |
| 692 | } |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region, |
| 698 | uint32_t *offset, uint32_t *size) |
| 699 | { |
| 700 | struct ec_params_flash_region_info p; |
| 701 | struct ec_response_flash_region_info *r; |
| 702 | int ret; |
| 703 | |
| 704 | p.region = region; |
| 705 | ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO, |
| 706 | EC_VER_FLASH_REGION_INFO, |
| 707 | &p, sizeof(p), (uint8_t **)&r, sizeof(*r)); |
| 708 | if (ret != sizeof(*r)) |
| 709 | return -1; |
| 710 | |
| 711 | if (offset) |
| 712 | *offset = r->offset; |
| 713 | if (size) |
| 714 | *size = r->size; |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size) |
| 720 | { |
| 721 | struct ec_params_flash_erase p; |
| 722 | |
| 723 | p.offset = offset; |
| 724 | p.size = size; |
| 725 | return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p), |
| 726 | NULL, 0); |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * Write a single block to the flash |
| 731 | * |
| 732 | * Write a block of data to the EC flash. The size must not exceed the flash |
| 733 | * write block size which you can obtain from cros_ec_flash_write_burst_size(). |
| 734 | * |
| 735 | * The offset starts at 0. You can obtain the region information from |
| 736 | * cros_ec_flash_offset() to find out where to write for a particular region. |
| 737 | * |
| 738 | * Attempting to write to the region where the EC is currently running from |
| 739 | * will result in an error. |
| 740 | * |
| 741 | * @param dev CROS-EC device |
| 742 | * @param data Pointer to data buffer to write |
| 743 | * @param offset Offset within flash to write to. |
| 744 | * @param size Number of bytes to write |
| 745 | * @return 0 if ok, -1 on error |
| 746 | */ |
| 747 | static int cros_ec_flash_write_block(struct cros_ec_dev *dev, |
| 748 | const uint8_t *data, uint32_t offset, uint32_t size) |
| 749 | { |
| 750 | struct ec_params_flash_write p; |
| 751 | |
| 752 | p.offset = offset; |
| 753 | p.size = size; |
Simon Glass | 836bb6e | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 754 | assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE); |
| 755 | memcpy(&p + 1, data, p.size); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 756 | |
| 757 | return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0, |
| 758 | &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1; |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Return optimal flash write burst size |
| 763 | */ |
| 764 | static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev) |
| 765 | { |
Simon Glass | 836bb6e | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 766 | return EC_FLASH_WRITE_VER0_SIZE; |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /** |
| 770 | * Check if a block of data is erased (all 0xff) |
| 771 | * |
| 772 | * This function is useful when dealing with flash, for checking whether a |
| 773 | * data block is erased and thus does not need to be programmed. |
| 774 | * |
| 775 | * @param data Pointer to data to check (must be word-aligned) |
| 776 | * @param size Number of bytes to check (must be word-aligned) |
| 777 | * @return 0 if erased, non-zero if any word is not erased |
| 778 | */ |
| 779 | static int cros_ec_data_is_erased(const uint32_t *data, int size) |
| 780 | { |
| 781 | assert(!(size & 3)); |
| 782 | size /= sizeof(uint32_t); |
| 783 | for (; size > 0; size -= 4, data++) |
| 784 | if (*data != -1U) |
| 785 | return 0; |
| 786 | |
| 787 | return 1; |
| 788 | } |
| 789 | |
| 790 | int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data, |
| 791 | uint32_t offset, uint32_t size) |
| 792 | { |
| 793 | uint32_t burst = cros_ec_flash_write_burst_size(dev); |
| 794 | uint32_t end, off; |
| 795 | int ret; |
| 796 | |
| 797 | /* |
| 798 | * TODO: round up to the nearest multiple of write size. Can get away |
| 799 | * without that on link right now because its write size is 4 bytes. |
| 800 | */ |
| 801 | end = offset + size; |
| 802 | for (off = offset; off < end; off += burst, data += burst) { |
| 803 | uint32_t todo; |
| 804 | |
| 805 | /* If the data is empty, there is no point in programming it */ |
| 806 | todo = min(end - off, burst); |
| 807 | if (dev->optimise_flash_write && |
| 808 | cros_ec_data_is_erased((uint32_t *)data, todo)) |
| 809 | continue; |
| 810 | |
| 811 | ret = cros_ec_flash_write_block(dev, data, off, todo); |
| 812 | if (ret) |
| 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * Read a single block from the flash |
| 821 | * |
| 822 | * Read a block of data from the EC flash. The size must not exceed the flash |
| 823 | * write block size which you can obtain from cros_ec_flash_write_burst_size(). |
| 824 | * |
| 825 | * The offset starts at 0. You can obtain the region information from |
| 826 | * cros_ec_flash_offset() to find out where to read for a particular region. |
| 827 | * |
| 828 | * @param dev CROS-EC device |
| 829 | * @param data Pointer to data buffer to read into |
| 830 | * @param offset Offset within flash to read from |
| 831 | * @param size Number of bytes to read |
| 832 | * @return 0 if ok, -1 on error |
| 833 | */ |
| 834 | static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data, |
| 835 | uint32_t offset, uint32_t size) |
| 836 | { |
| 837 | struct ec_params_flash_read p; |
| 838 | |
| 839 | p.offset = offset; |
| 840 | p.size = size; |
| 841 | |
| 842 | return ec_command(dev, EC_CMD_FLASH_READ, 0, |
| 843 | &p, sizeof(p), data, size) >= 0 ? 0 : -1; |
| 844 | } |
| 845 | |
| 846 | int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset, |
| 847 | uint32_t size) |
| 848 | { |
| 849 | uint32_t burst = cros_ec_flash_write_burst_size(dev); |
| 850 | uint32_t end, off; |
| 851 | int ret; |
| 852 | |
| 853 | end = offset + size; |
| 854 | for (off = offset; off < end; off += burst, data += burst) { |
| 855 | ret = cros_ec_flash_read_block(dev, data, off, |
| 856 | min(end - off, burst)); |
| 857 | if (ret) |
| 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | int cros_ec_flash_update_rw(struct cros_ec_dev *dev, |
| 865 | const uint8_t *image, int image_size) |
| 866 | { |
| 867 | uint32_t rw_offset, rw_size; |
| 868 | int ret; |
| 869 | |
| 870 | if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size)) |
| 871 | return -1; |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 872 | if (image_size > (int)rw_size) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 873 | return -1; |
| 874 | |
| 875 | /* Invalidate the existing hash, just in case the AP reboots |
| 876 | * unexpectedly during the update. If that happened, the EC RW firmware |
| 877 | * would be invalid, but the EC would still have the original hash. |
| 878 | */ |
| 879 | ret = cros_ec_invalidate_hash(dev); |
| 880 | if (ret) |
| 881 | return ret; |
| 882 | |
| 883 | /* |
| 884 | * Erase the entire RW section, so that the EC doesn't see any garbage |
| 885 | * past the new image if it's smaller than the current image. |
| 886 | * |
| 887 | * TODO: could optimize this to erase just the current image, since |
| 888 | * presumably everything past that is 0xff's. But would still need to |
| 889 | * round up to the nearest multiple of erase size. |
| 890 | */ |
| 891 | ret = cros_ec_flash_erase(dev, rw_offset, rw_size); |
| 892 | if (ret) |
| 893 | return ret; |
| 894 | |
| 895 | /* Write the image */ |
| 896 | ret = cros_ec_flash_write(dev, image, rw_offset, image_size); |
| 897 | if (ret) |
| 898 | return ret; |
| 899 | |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block) |
| 904 | { |
| 905 | struct ec_params_vbnvcontext p; |
| 906 | int len; |
| 907 | |
| 908 | p.op = EC_VBNV_CONTEXT_OP_READ; |
| 909 | |
| 910 | len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT, |
| 911 | &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE); |
| 912 | if (len < EC_VBNV_BLOCK_SIZE) |
| 913 | return -1; |
| 914 | |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block) |
| 919 | { |
| 920 | struct ec_params_vbnvcontext p; |
| 921 | int len; |
| 922 | |
| 923 | p.op = EC_VBNV_CONTEXT_OP_WRITE; |
| 924 | memcpy(p.block, block, sizeof(p.block)); |
| 925 | |
| 926 | len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT, |
| 927 | &p, sizeof(p), NULL, 0); |
| 928 | if (len < 0) |
| 929 | return -1; |
| 930 | |
| 931 | return 0; |
| 932 | } |
| 933 | |
Simon Glass | f48eaf0 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 934 | int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 935 | { |
Simon Glass | f48eaf0 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 936 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 937 | struct ec_params_ldo_set params; |
| 938 | |
| 939 | params.index = index; |
| 940 | params.state = state; |
| 941 | |
Simon Glass | f48eaf0 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 942 | if (ec_command_inptr(cdev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params), |
| 943 | NULL, 0)) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 944 | return -1; |
| 945 | |
| 946 | return 0; |
| 947 | } |
| 948 | |
Simon Glass | f48eaf0 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 949 | int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 950 | { |
Simon Glass | f48eaf0 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 951 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 952 | struct ec_params_ldo_get params; |
| 953 | struct ec_response_ldo_get *resp; |
| 954 | |
| 955 | params.index = index; |
| 956 | |
Simon Glass | f48eaf0 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 957 | if (ec_command_inptr(cdev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params), |
| 958 | (uint8_t **)&resp, sizeof(*resp)) != |
| 959 | sizeof(*resp)) |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 960 | return -1; |
| 961 | |
| 962 | *state = resp->state; |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 967 | int cros_ec_register(struct udevice *dev) |
| 968 | { |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 969 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 970 | const void *blob = gd->fdt_blob; |
| 971 | int node = dev->of_offset; |
| 972 | char id[MSG_BYTES]; |
| 973 | |
| 974 | cdev->dev = dev; |
Simon Glass | 32f8a19 | 2015-01-05 20:05:32 -0700 | [diff] [blame] | 975 | gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int, |
| 976 | GPIOD_IS_IN); |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 977 | cdev->optimise_flash_write = fdtdec_get_bool(blob, node, |
| 978 | "optimise-flash-write"); |
| 979 | |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 980 | if (cros_ec_check_version(cdev)) { |
| 981 | debug("%s: Could not detect CROS-EC version\n", __func__); |
| 982 | return -CROS_EC_ERR_CHECK_VERSION; |
| 983 | } |
| 984 | |
| 985 | if (cros_ec_read_id(cdev, id, sizeof(id))) { |
| 986 | debug("%s: Could not read KBC ID\n", __func__); |
| 987 | return -CROS_EC_ERR_READ_ID; |
| 988 | } |
| 989 | |
| 990 | /* Remember this device for use by the cros_ec command */ |
Simon Glass | c4b206d | 2015-02-17 15:29:36 -0700 | [diff] [blame] | 991 | debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n", |
| 992 | cdev->protocol_version, id); |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 993 | |
| 994 | return 0; |
| 995 | } |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 996 | |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 997 | int cros_ec_decode_region(int argc, char * const argv[]) |
| 998 | { |
| 999 | if (argc > 0) { |
| 1000 | if (0 == strcmp(*argv, "rw")) |
| 1001 | return EC_FLASH_REGION_RW; |
| 1002 | else if (0 == strcmp(*argv, "ro")) |
| 1003 | return EC_FLASH_REGION_RO; |
| 1004 | |
| 1005 | debug("%s: Invalid region '%s'\n", __func__, *argv); |
| 1006 | } else { |
| 1007 | debug("%s: Missing region parameter\n", __func__); |
| 1008 | } |
| 1009 | |
| 1010 | return -1; |
| 1011 | } |
| 1012 | |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1013 | int cros_ec_decode_ec_flash(const void *blob, int node, |
| 1014 | struct fdt_cros_ec *config) |
Simon Glass | d7f25f3 | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 1015 | { |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1016 | int flash_node; |
Simon Glass | d7f25f3 | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 1017 | |
| 1018 | flash_node = fdt_subnode_offset(blob, node, "flash"); |
| 1019 | if (flash_node < 0) { |
| 1020 | debug("Failed to find flash node\n"); |
| 1021 | return -1; |
| 1022 | } |
| 1023 | |
| 1024 | if (fdtdec_read_fmap_entry(blob, flash_node, "flash", |
| 1025 | &config->flash)) { |
| 1026 | debug("Failed to decode flash node in chrome-ec'\n"); |
| 1027 | return -1; |
| 1028 | } |
| 1029 | |
| 1030 | config->flash_erase_value = fdtdec_get_int(blob, flash_node, |
| 1031 | "erase-value", -1); |
| 1032 | for (node = fdt_first_subnode(blob, flash_node); node >= 0; |
| 1033 | node = fdt_next_subnode(blob, node)) { |
| 1034 | const char *name = fdt_get_name(blob, node, NULL); |
| 1035 | enum ec_flash_region region; |
| 1036 | |
| 1037 | if (0 == strcmp(name, "ro")) { |
| 1038 | region = EC_FLASH_REGION_RO; |
| 1039 | } else if (0 == strcmp(name, "rw")) { |
| 1040 | region = EC_FLASH_REGION_RW; |
| 1041 | } else if (0 == strcmp(name, "wp-ro")) { |
| 1042 | region = EC_FLASH_REGION_WP_RO; |
| 1043 | } else { |
| 1044 | debug("Unknown EC flash region name '%s'\n", name); |
| 1045 | return -1; |
| 1046 | } |
| 1047 | |
| 1048 | if (fdtdec_read_fmap_entry(blob, node, "reg", |
| 1049 | &config->region[region])) { |
| 1050 | debug("Failed to decode flash region in chrome-ec'\n"); |
| 1051 | return -1; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | return 0; |
| 1056 | } |
| 1057 | |
Simon Glass | cc456bd | 2015-08-03 08:19:23 -0600 | [diff] [blame] | 1058 | int cros_ec_i2c_tunnel(struct udevice *dev, struct i2c_msg *in, int nmsgs) |
| 1059 | { |
| 1060 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
| 1061 | union { |
| 1062 | struct ec_params_i2c_passthru p; |
| 1063 | uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE]; |
| 1064 | } params; |
| 1065 | union { |
| 1066 | struct ec_response_i2c_passthru r; |
| 1067 | uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE]; |
| 1068 | } response; |
| 1069 | struct ec_params_i2c_passthru *p = ¶ms.p; |
| 1070 | struct ec_response_i2c_passthru *r = &response.r; |
| 1071 | struct ec_params_i2c_passthru_msg *msg; |
| 1072 | uint8_t *pdata, *read_ptr = NULL; |
| 1073 | int read_len; |
| 1074 | int size; |
| 1075 | int rv; |
| 1076 | int i; |
| 1077 | |
| 1078 | p->port = 0; |
| 1079 | |
| 1080 | p->num_msgs = nmsgs; |
| 1081 | size = sizeof(*p) + p->num_msgs * sizeof(*msg); |
| 1082 | |
| 1083 | /* Create a message to write the register address and optional data */ |
| 1084 | pdata = (uint8_t *)p + size; |
| 1085 | |
| 1086 | read_len = 0; |
| 1087 | for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) { |
| 1088 | bool is_read = in->flags & I2C_M_RD; |
| 1089 | |
| 1090 | msg->addr_flags = in->addr; |
| 1091 | msg->len = in->len; |
| 1092 | if (is_read) { |
| 1093 | msg->addr_flags |= EC_I2C_FLAG_READ; |
| 1094 | read_len += in->len; |
| 1095 | read_ptr = in->buf; |
| 1096 | if (sizeof(*r) + read_len > sizeof(response)) { |
| 1097 | puts("Read length too big for buffer\n"); |
| 1098 | return -1; |
| 1099 | } |
| 1100 | } else { |
| 1101 | if (pdata - (uint8_t *)p + in->len > sizeof(params)) { |
| 1102 | puts("Params too large for buffer\n"); |
| 1103 | return -1; |
| 1104 | } |
| 1105 | memcpy(pdata, in->buf, in->len); |
| 1106 | pdata += in->len; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | rv = ec_command(cdev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p, |
| 1111 | r, sizeof(*r) + read_len); |
| 1112 | if (rv < 0) |
| 1113 | return rv; |
| 1114 | |
| 1115 | /* Parse response */ |
| 1116 | if (r->i2c_status & EC_I2C_STATUS_ERROR) { |
| 1117 | printf("Transfer failed with status=0x%x\n", r->i2c_status); |
| 1118 | return -1; |
| 1119 | } |
| 1120 | |
| 1121 | if (rv < sizeof(*r) + read_len) { |
| 1122 | puts("Truncated read response\n"); |
| 1123 | return -1; |
| 1124 | } |
| 1125 | |
| 1126 | /* We only support a single read message for each transfer */ |
| 1127 | if (read_len) |
| 1128 | memcpy(read_ptr, r->data, read_len); |
| 1129 | |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
Simon Glass | 1c266b9 | 2014-02-27 13:26:06 -0700 | [diff] [blame] | 1133 | #ifdef CONFIG_CMD_CROS_EC |
| 1134 | |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1135 | /** |
| 1136 | * Perform a flash read or write command |
| 1137 | * |
| 1138 | * @param dev CROS-EC device to read/write |
| 1139 | * @param is_write 1 do to a write, 0 to do a read |
| 1140 | * @param argc Number of arguments |
| 1141 | * @param argv Arguments (2 is region, 3 is address) |
| 1142 | * @return 0 for ok, 1 for a usage error or -ve for ec command error |
| 1143 | * (negative EC_RES_...) |
| 1144 | */ |
| 1145 | static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc, |
| 1146 | char * const argv[]) |
| 1147 | { |
| 1148 | uint32_t offset, size = -1U, region_size; |
| 1149 | unsigned long addr; |
| 1150 | char *endp; |
| 1151 | int region; |
| 1152 | int ret; |
| 1153 | |
| 1154 | region = cros_ec_decode_region(argc - 2, argv + 2); |
| 1155 | if (region == -1) |
| 1156 | return 1; |
| 1157 | if (argc < 4) |
| 1158 | return 1; |
| 1159 | addr = simple_strtoul(argv[3], &endp, 16); |
| 1160 | if (*argv[3] == 0 || *endp != 0) |
| 1161 | return 1; |
| 1162 | if (argc > 4) { |
| 1163 | size = simple_strtoul(argv[4], &endp, 16); |
| 1164 | if (*argv[4] == 0 || *endp != 0) |
| 1165 | return 1; |
| 1166 | } |
| 1167 | |
| 1168 | ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size); |
| 1169 | if (ret) { |
| 1170 | debug("%s: Could not read region info\n", __func__); |
| 1171 | return ret; |
| 1172 | } |
| 1173 | if (size == -1U) |
| 1174 | size = region_size; |
| 1175 | |
| 1176 | ret = is_write ? |
| 1177 | cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) : |
| 1178 | cros_ec_flash_read(dev, (uint8_t *)addr, offset, size); |
| 1179 | if (ret) { |
| 1180 | debug("%s: Could not %s region\n", __func__, |
| 1181 | is_write ? "write" : "read"); |
| 1182 | return ret; |
| 1183 | } |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 1189 | { |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1190 | struct cros_ec_dev *dev; |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1191 | struct udevice *udev; |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1192 | const char *cmd; |
| 1193 | int ret = 0; |
| 1194 | |
| 1195 | if (argc < 2) |
| 1196 | return CMD_RET_USAGE; |
| 1197 | |
| 1198 | cmd = argv[1]; |
| 1199 | if (0 == strcmp("init", cmd)) { |
Simon Glass | e96fc7d | 2015-03-26 09:29:31 -0600 | [diff] [blame] | 1200 | /* Remove any existing device */ |
| 1201 | ret = uclass_find_device(UCLASS_CROS_EC, 0, &udev); |
| 1202 | if (!ret) |
| 1203 | device_remove(udev); |
| 1204 | ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1205 | if (ret) { |
| 1206 | printf("Could not init cros_ec device (err %d)\n", ret); |
| 1207 | return 1; |
| 1208 | } |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1212 | ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev); |
| 1213 | if (ret) { |
| 1214 | printf("Cannot get cros-ec device (err=%d)\n", ret); |
| 1215 | return 1; |
| 1216 | } |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 1217 | dev = dev_get_uclass_priv(udev); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1218 | if (0 == strcmp("id", cmd)) { |
| 1219 | char id[MSG_BYTES]; |
| 1220 | |
| 1221 | if (cros_ec_read_id(dev, id, sizeof(id))) { |
| 1222 | debug("%s: Could not read KBC ID\n", __func__); |
| 1223 | return 1; |
| 1224 | } |
| 1225 | printf("%s\n", id); |
| 1226 | } else if (0 == strcmp("info", cmd)) { |
Simon Glass | 836bb6e | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 1227 | struct ec_response_mkbp_info info; |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1228 | |
| 1229 | if (cros_ec_info(dev, &info)) { |
| 1230 | debug("%s: Could not read KBC info\n", __func__); |
| 1231 | return 1; |
| 1232 | } |
| 1233 | printf("rows = %u\n", info.rows); |
| 1234 | printf("cols = %u\n", info.cols); |
| 1235 | printf("switches = %#x\n", info.switches); |
| 1236 | } else if (0 == strcmp("curimage", cmd)) { |
| 1237 | enum ec_current_image image; |
| 1238 | |
| 1239 | if (cros_ec_read_current_image(dev, &image)) { |
| 1240 | debug("%s: Could not read KBC image\n", __func__); |
| 1241 | return 1; |
| 1242 | } |
| 1243 | printf("%d\n", image); |
| 1244 | } else if (0 == strcmp("hash", cmd)) { |
| 1245 | struct ec_response_vboot_hash hash; |
| 1246 | int i; |
| 1247 | |
| 1248 | if (cros_ec_read_hash(dev, &hash)) { |
| 1249 | debug("%s: Could not read KBC hash\n", __func__); |
| 1250 | return 1; |
| 1251 | } |
| 1252 | |
| 1253 | if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256) |
| 1254 | printf("type: SHA-256\n"); |
| 1255 | else |
| 1256 | printf("type: %d\n", hash.hash_type); |
| 1257 | |
| 1258 | printf("offset: 0x%08x\n", hash.offset); |
| 1259 | printf("size: 0x%08x\n", hash.size); |
| 1260 | |
| 1261 | printf("digest: "); |
| 1262 | for (i = 0; i < hash.digest_size; i++) |
| 1263 | printf("%02x", hash.hash_digest[i]); |
| 1264 | printf("\n"); |
| 1265 | } else if (0 == strcmp("reboot", cmd)) { |
| 1266 | int region; |
| 1267 | enum ec_reboot_cmd cmd; |
| 1268 | |
| 1269 | if (argc >= 3 && !strcmp(argv[2], "cold")) |
| 1270 | cmd = EC_REBOOT_COLD; |
| 1271 | else { |
| 1272 | region = cros_ec_decode_region(argc - 2, argv + 2); |
| 1273 | if (region == EC_FLASH_REGION_RO) |
| 1274 | cmd = EC_REBOOT_JUMP_RO; |
| 1275 | else if (region == EC_FLASH_REGION_RW) |
| 1276 | cmd = EC_REBOOT_JUMP_RW; |
| 1277 | else |
| 1278 | return CMD_RET_USAGE; |
| 1279 | } |
| 1280 | |
| 1281 | if (cros_ec_reboot(dev, cmd, 0)) { |
| 1282 | debug("%s: Could not reboot KBC\n", __func__); |
| 1283 | return 1; |
| 1284 | } |
| 1285 | } else if (0 == strcmp("events", cmd)) { |
| 1286 | uint32_t events; |
| 1287 | |
| 1288 | if (cros_ec_get_host_events(dev, &events)) { |
| 1289 | debug("%s: Could not read host events\n", __func__); |
| 1290 | return 1; |
| 1291 | } |
| 1292 | printf("0x%08x\n", events); |
| 1293 | } else if (0 == strcmp("clrevents", cmd)) { |
| 1294 | uint32_t events = 0x7fffffff; |
| 1295 | |
| 1296 | if (argc >= 3) |
| 1297 | events = simple_strtol(argv[2], NULL, 0); |
| 1298 | |
| 1299 | if (cros_ec_clear_host_events(dev, events)) { |
| 1300 | debug("%s: Could not clear host events\n", __func__); |
| 1301 | return 1; |
| 1302 | } |
| 1303 | } else if (0 == strcmp("read", cmd)) { |
| 1304 | ret = do_read_write(dev, 0, argc, argv); |
| 1305 | if (ret > 0) |
| 1306 | return CMD_RET_USAGE; |
| 1307 | } else if (0 == strcmp("write", cmd)) { |
| 1308 | ret = do_read_write(dev, 1, argc, argv); |
| 1309 | if (ret > 0) |
| 1310 | return CMD_RET_USAGE; |
| 1311 | } else if (0 == strcmp("erase", cmd)) { |
| 1312 | int region = cros_ec_decode_region(argc - 2, argv + 2); |
| 1313 | uint32_t offset, size; |
| 1314 | |
| 1315 | if (region == -1) |
| 1316 | return CMD_RET_USAGE; |
| 1317 | if (cros_ec_flash_offset(dev, region, &offset, &size)) { |
| 1318 | debug("%s: Could not read region info\n", __func__); |
| 1319 | ret = -1; |
| 1320 | } else { |
| 1321 | ret = cros_ec_flash_erase(dev, offset, size); |
| 1322 | if (ret) { |
| 1323 | debug("%s: Could not erase region\n", |
| 1324 | __func__); |
| 1325 | } |
| 1326 | } |
| 1327 | } else if (0 == strcmp("regioninfo", cmd)) { |
| 1328 | int region = cros_ec_decode_region(argc - 2, argv + 2); |
| 1329 | uint32_t offset, size; |
| 1330 | |
| 1331 | if (region == -1) |
| 1332 | return CMD_RET_USAGE; |
| 1333 | ret = cros_ec_flash_offset(dev, region, &offset, &size); |
| 1334 | if (ret) { |
| 1335 | debug("%s: Could not read region info\n", __func__); |
| 1336 | } else { |
| 1337 | printf("Region: %s\n", region == EC_FLASH_REGION_RO ? |
| 1338 | "RO" : "RW"); |
| 1339 | printf("Offset: %x\n", offset); |
| 1340 | printf("Size: %x\n", size); |
| 1341 | } |
| 1342 | } else if (0 == strcmp("vbnvcontext", cmd)) { |
| 1343 | uint8_t block[EC_VBNV_BLOCK_SIZE]; |
| 1344 | char buf[3]; |
| 1345 | int i, len; |
| 1346 | unsigned long result; |
| 1347 | |
| 1348 | if (argc <= 2) { |
| 1349 | ret = cros_ec_read_vbnvcontext(dev, block); |
| 1350 | if (!ret) { |
| 1351 | printf("vbnv_block: "); |
| 1352 | for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) |
| 1353 | printf("%02x", block[i]); |
| 1354 | putc('\n'); |
| 1355 | } |
| 1356 | } else { |
| 1357 | /* |
| 1358 | * TODO(clchiou): Move this to a utility function as |
| 1359 | * cmd_spi might want to call it. |
| 1360 | */ |
| 1361 | memset(block, 0, EC_VBNV_BLOCK_SIZE); |
| 1362 | len = strlen(argv[2]); |
| 1363 | buf[2] = '\0'; |
| 1364 | for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) { |
| 1365 | if (i * 2 >= len) |
| 1366 | break; |
| 1367 | buf[0] = argv[2][i * 2]; |
| 1368 | if (i * 2 + 1 >= len) |
| 1369 | buf[1] = '0'; |
| 1370 | else |
| 1371 | buf[1] = argv[2][i * 2 + 1]; |
| 1372 | strict_strtoul(buf, 16, &result); |
| 1373 | block[i] = result; |
| 1374 | } |
| 1375 | ret = cros_ec_write_vbnvcontext(dev, block); |
| 1376 | } |
| 1377 | if (ret) { |
| 1378 | debug("%s: Could not %s VbNvContext\n", __func__, |
| 1379 | argc <= 2 ? "read" : "write"); |
| 1380 | } |
| 1381 | } else if (0 == strcmp("test", cmd)) { |
| 1382 | int result = cros_ec_test(dev); |
| 1383 | |
| 1384 | if (result) |
| 1385 | printf("Test failed with error %d\n", result); |
| 1386 | else |
| 1387 | puts("Test passed\n"); |
| 1388 | } else if (0 == strcmp("version", cmd)) { |
| 1389 | struct ec_response_get_version *p; |
| 1390 | char *build_string; |
| 1391 | |
| 1392 | ret = cros_ec_read_version(dev, &p); |
| 1393 | if (!ret) { |
| 1394 | /* Print versions */ |
| 1395 | printf("RO version: %1.*s\n", |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 1396 | (int)sizeof(p->version_string_ro), |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1397 | p->version_string_ro); |
| 1398 | printf("RW version: %1.*s\n", |
Simon Glass | 2ab83f0 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 1399 | (int)sizeof(p->version_string_rw), |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1400 | p->version_string_rw); |
| 1401 | printf("Firmware copy: %s\n", |
| 1402 | (p->current_image < |
| 1403 | ARRAY_SIZE(ec_current_image_name) ? |
| 1404 | ec_current_image_name[p->current_image] : |
| 1405 | "?")); |
| 1406 | ret = cros_ec_read_build_info(dev, &build_string); |
| 1407 | if (!ret) |
| 1408 | printf("Build info: %s\n", build_string); |
| 1409 | } |
| 1410 | } else if (0 == strcmp("ldo", cmd)) { |
| 1411 | uint8_t index, state; |
| 1412 | char *endp; |
| 1413 | |
| 1414 | if (argc < 3) |
| 1415 | return CMD_RET_USAGE; |
| 1416 | index = simple_strtoul(argv[2], &endp, 10); |
| 1417 | if (*argv[2] == 0 || *endp != 0) |
| 1418 | return CMD_RET_USAGE; |
| 1419 | if (argc > 3) { |
| 1420 | state = simple_strtoul(argv[3], &endp, 10); |
| 1421 | if (*argv[3] == 0 || *endp != 0) |
| 1422 | return CMD_RET_USAGE; |
Simon Glass | f48eaf0 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 1423 | ret = cros_ec_set_ldo(udev, index, state); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1424 | } else { |
Simon Glass | f48eaf0 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 1425 | ret = cros_ec_get_ldo(udev, index, &state); |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1426 | if (!ret) { |
| 1427 | printf("LDO%d: %s\n", index, |
| 1428 | state == EC_LDO_STATE_ON ? |
| 1429 | "on" : "off"); |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | if (ret) { |
| 1434 | debug("%s: Could not access LDO%d\n", __func__, index); |
| 1435 | return ret; |
| 1436 | } |
| 1437 | } else { |
| 1438 | return CMD_RET_USAGE; |
| 1439 | } |
| 1440 | |
| 1441 | if (ret < 0) { |
| 1442 | printf("Error: CROS-EC command failed (error %d)\n", ret); |
| 1443 | ret = 1; |
| 1444 | } |
| 1445 | |
| 1446 | return ret; |
| 1447 | } |
| 1448 | |
Simon Glass | cc456bd | 2015-08-03 08:19:23 -0600 | [diff] [blame] | 1449 | int cros_ec_post_bind(struct udevice *dev) |
| 1450 | { |
| 1451 | /* Scan for available EC devices (e.g. I2C tunnel) */ |
| 1452 | return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false); |
| 1453 | } |
| 1454 | |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1455 | U_BOOT_CMD( |
Simon Glass | b2a668b | 2014-02-27 13:26:14 -0700 | [diff] [blame] | 1456 | crosec, 6, 1, do_cros_ec, |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1457 | "CROS-EC utility command", |
| 1458 | "init Re-init CROS-EC (done on startup automatically)\n" |
| 1459 | "crosec id Read CROS-EC ID\n" |
| 1460 | "crosec info Read CROS-EC info\n" |
| 1461 | "crosec curimage Read CROS-EC current image\n" |
| 1462 | "crosec hash Read CROS-EC hash\n" |
| 1463 | "crosec reboot [rw | ro | cold] Reboot CROS-EC\n" |
| 1464 | "crosec events Read CROS-EC host events\n" |
| 1465 | "crosec clrevents [mask] Clear CROS-EC host events\n" |
| 1466 | "crosec regioninfo <ro|rw> Read image info\n" |
| 1467 | "crosec erase <ro|rw> Erase EC image\n" |
| 1468 | "crosec read <ro|rw> <addr> [<size>] Read EC image\n" |
| 1469 | "crosec write <ro|rw> <addr> [<size>] Write EC image\n" |
| 1470 | "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n" |
| 1471 | "crosec ldo <idx> [<state>] Switch/Read LDO state\n" |
| 1472 | "crosec test run tests on cros_ec\n" |
Simon Glass | 24696e2 | 2015-08-03 08:19:33 -0600 | [diff] [blame^] | 1473 | "crosec version Read CROS-EC version" |
Hung-ying Tyan | 8836438 | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1474 | ); |
| 1475 | #endif |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1476 | |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1477 | UCLASS_DRIVER(cros_ec) = { |
| 1478 | .id = UCLASS_CROS_EC, |
| 1479 | .name = "cros_ec", |
| 1480 | .per_device_auto_alloc_size = sizeof(struct cros_ec_dev), |
Simon Glass | cc456bd | 2015-08-03 08:19:23 -0600 | [diff] [blame] | 1481 | .post_bind = cros_ec_post_bind, |
Simon Glass | 84d6cbd | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1482 | }; |