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