Hung-ying Tyan | c48ca88f | 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 | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
Simon Glass | ece2886 | 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 | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <common.h> |
| 18 | #include <command.h> |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 19 | #include <dm.h> |
Hung-ying Tyan | c48ca88f | 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 | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 25 | #include <asm/errno.h> |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 26 | #include <asm/io.h> |
| 27 | #include <asm-generic/gpio.h> |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 28 | #include <dm/device-internal.h> |
| 29 | #include <dm/uclass-internal.h> |
Hung-ying Tyan | c48ca88f | 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 | c48ca88f | 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 | 13c7c5b | 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 | d3d9c06 | 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 | 13c7c5b | 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 | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 210 | struct dm_cros_ec_ops *ops; |
Simon Glass | 13c7c5b | 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 | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 225 | ops = dm_cros_ec_get_ops(dev->dev); |
Simon Glass | d3d9c06 | 2015-01-25 08:27:17 -0700 | [diff] [blame] | 226 | rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS; |
Simon Glass | 13c7c5b | 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 | c48ca88f | 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 | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 238 | struct dm_cros_ec_ops *ops; |
Simon Glass | 13c7c5b | 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 | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 246 | |
Simon Glass | 44569b5 | 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 | c48ca88f | 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 | d3d9c06 | 2015-01-25 08:27:17 -0700 | [diff] [blame] | 268 | * @return number of bytes in response, or -ve on error |
Hung-ying Tyan | c48ca88f | 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 | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 274 | uint8_t *din = NULL; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 275 | int len; |
| 276 | |
Hung-ying Tyan | c48ca88f | 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 | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 282 | struct ec_response_get_comms_status *resp = NULL; |
Hung-ying Tyan | c48ca88f | 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 | 19cc295 | 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 | c48ca88f | 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 | d3d9c06 | 2015-01-25 08:27:17 -0700 | [diff] [blame] | 335 | * @return number of bytes in response, or -ve on error |
Hung-ying Tyan | c48ca88f | 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 | |
Simon Glass | fb72640 | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 360 | int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 361 | { |
Simon Glass | fb72640 | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 362 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
| 363 | |
| 364 | if (ec_command(cdev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan, |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 365 | sizeof(scan->data)) != sizeof(scan->data)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 366 | return -1; |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen) |
| 372 | { |
| 373 | struct ec_response_get_version *r; |
| 374 | |
| 375 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 376 | (uint8_t **)&r, sizeof(*r)) != sizeof(*r)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 377 | return -1; |
| 378 | |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 379 | if (maxlen > (int)sizeof(r->version_string_ro)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 380 | maxlen = sizeof(r->version_string_ro); |
| 381 | |
| 382 | switch (r->current_image) { |
| 383 | case EC_IMAGE_RO: |
| 384 | memcpy(id, r->version_string_ro, maxlen); |
| 385 | break; |
| 386 | case EC_IMAGE_RW: |
| 387 | memcpy(id, r->version_string_rw, maxlen); |
| 388 | break; |
| 389 | default: |
| 390 | return -1; |
| 391 | } |
| 392 | |
| 393 | id[maxlen - 1] = '\0'; |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | int cros_ec_read_version(struct cros_ec_dev *dev, |
| 398 | struct ec_response_get_version **versionp) |
| 399 | { |
| 400 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, |
| 401 | (uint8_t **)versionp, sizeof(**versionp)) |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 402 | != sizeof(**versionp)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 403 | return -1; |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp) |
| 409 | { |
| 410 | if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0, |
Simon Glass | ece2886 | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 411 | (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 412 | return -1; |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | int cros_ec_read_current_image(struct cros_ec_dev *dev, |
| 418 | enum ec_current_image *image) |
| 419 | { |
| 420 | struct ec_response_get_version *r; |
| 421 | |
| 422 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 423 | (uint8_t **)&r, sizeof(*r)) != sizeof(*r)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 424 | return -1; |
| 425 | |
| 426 | *image = r->current_image; |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev, |
| 431 | struct ec_response_vboot_hash *hash) |
| 432 | { |
| 433 | struct ec_params_vboot_hash p; |
| 434 | ulong start; |
| 435 | |
| 436 | start = get_timer(0); |
| 437 | while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) { |
| 438 | mdelay(50); /* Insert some reasonable delay */ |
| 439 | |
| 440 | p.cmd = EC_VBOOT_HASH_GET; |
| 441 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
| 442 | hash, sizeof(*hash)) < 0) |
| 443 | return -1; |
| 444 | |
| 445 | if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) { |
| 446 | debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__); |
| 447 | return -EC_RES_TIMEOUT; |
| 448 | } |
| 449 | } |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | |
| 454 | int cros_ec_read_hash(struct cros_ec_dev *dev, |
| 455 | struct ec_response_vboot_hash *hash) |
| 456 | { |
| 457 | struct ec_params_vboot_hash p; |
| 458 | int rv; |
| 459 | |
| 460 | p.cmd = EC_VBOOT_HASH_GET; |
| 461 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
| 462 | hash, sizeof(*hash)) < 0) |
| 463 | return -1; |
| 464 | |
| 465 | /* If the EC is busy calculating the hash, fidget until it's done. */ |
| 466 | rv = cros_ec_wait_on_hash_done(dev, hash); |
| 467 | if (rv) |
| 468 | return rv; |
| 469 | |
| 470 | /* If the hash is valid, we're done. Otherwise, we have to kick it off |
| 471 | * again and wait for it to complete. Note that we explicitly assume |
| 472 | * that hashing zero bytes is always wrong, even though that would |
| 473 | * produce a valid hash value. */ |
| 474 | if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size) |
| 475 | return 0; |
| 476 | |
| 477 | debug("%s: No valid hash (status=%d size=%d). Compute one...\n", |
| 478 | __func__, hash->status, hash->size); |
| 479 | |
Simon Glass | ece2886 | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 480 | p.cmd = EC_VBOOT_HASH_START; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 481 | p.hash_type = EC_VBOOT_HASH_TYPE_SHA256; |
| 482 | p.nonce_size = 0; |
| 483 | p.offset = EC_VBOOT_HASH_OFFSET_RW; |
| 484 | |
| 485 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
| 486 | hash, sizeof(*hash)) < 0) |
| 487 | return -1; |
| 488 | |
| 489 | rv = cros_ec_wait_on_hash_done(dev, hash); |
| 490 | if (rv) |
| 491 | return rv; |
| 492 | |
| 493 | debug("%s: hash done\n", __func__); |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static int cros_ec_invalidate_hash(struct cros_ec_dev *dev) |
| 499 | { |
| 500 | struct ec_params_vboot_hash p; |
| 501 | struct ec_response_vboot_hash *hash; |
| 502 | |
| 503 | /* We don't have an explict command for the EC to discard its current |
| 504 | * hash value, so we'll just tell it to calculate one that we know is |
| 505 | * wrong (we claim that hashing zero bytes is always invalid). |
| 506 | */ |
| 507 | p.cmd = EC_VBOOT_HASH_RECALC; |
| 508 | p.hash_type = EC_VBOOT_HASH_TYPE_SHA256; |
| 509 | p.nonce_size = 0; |
| 510 | p.offset = 0; |
| 511 | p.size = 0; |
| 512 | |
| 513 | debug("%s:\n", __func__); |
| 514 | |
| 515 | if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
| 516 | (uint8_t **)&hash, sizeof(*hash)) < 0) |
| 517 | return -1; |
| 518 | |
| 519 | /* No need to wait for it to finish */ |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd, |
| 524 | uint8_t flags) |
| 525 | { |
| 526 | struct ec_params_reboot_ec p; |
| 527 | |
| 528 | p.cmd = cmd; |
| 529 | p.flags = flags; |
| 530 | |
| 531 | if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0) |
| 532 | < 0) |
| 533 | return -1; |
| 534 | |
| 535 | if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) { |
| 536 | /* |
| 537 | * EC reboot will take place immediately so delay to allow it |
| 538 | * to complete. Note that some reboot types (EC_REBOOT_COLD) |
| 539 | * will reboot the AP as well, in which case we won't actually |
| 540 | * get to this point. |
| 541 | */ |
| 542 | /* |
| 543 | * TODO(rspangler@chromium.org): Would be nice if we had a |
| 544 | * better way to determine when the reboot is complete. Could |
| 545 | * we poll a memory-mapped LPC value? |
| 546 | */ |
| 547 | udelay(50000); |
| 548 | } |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
Simon Glass | fb72640 | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 553 | int cros_ec_interrupt_pending(struct udevice *dev) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 554 | { |
Simon Glass | fb72640 | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 555 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
| 556 | |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 557 | /* no interrupt support : always poll */ |
Simon Glass | fb72640 | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 558 | if (!dm_gpio_is_valid(&cdev->ec_int)) |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 559 | return -ENOENT; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 560 | |
Simon Glass | fb72640 | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 561 | return dm_gpio_get_value(&cdev->ec_int); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 562 | } |
| 563 | |
Simon Glass | ece2886 | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 564 | int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 565 | { |
Simon Glass | ece2886 | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 566 | if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info, |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 567 | sizeof(*info)) != sizeof(*info)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 568 | return -1; |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr) |
| 574 | { |
| 575 | struct ec_response_host_event_mask *resp; |
| 576 | |
| 577 | /* |
| 578 | * Use the B copy of the event flags, because the main copy is already |
| 579 | * used by ACPI/SMI. |
| 580 | */ |
| 581 | if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0, |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 582 | (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 583 | return -1; |
| 584 | |
| 585 | if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID)) |
| 586 | return -1; |
| 587 | |
| 588 | *events_ptr = resp->mask; |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events) |
| 593 | { |
| 594 | struct ec_params_host_event_mask params; |
| 595 | |
| 596 | params.mask = events; |
| 597 | |
| 598 | /* |
| 599 | * Use the B copy of the event flags, so it affects the data returned |
| 600 | * by cros_ec_get_host_events(). |
| 601 | */ |
| 602 | if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0, |
| 603 | ¶ms, sizeof(params), NULL, 0) < 0) |
| 604 | return -1; |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | int cros_ec_flash_protect(struct cros_ec_dev *dev, |
| 610 | uint32_t set_mask, uint32_t set_flags, |
| 611 | struct ec_response_flash_protect *resp) |
| 612 | { |
| 613 | struct ec_params_flash_protect params; |
| 614 | |
| 615 | params.mask = set_mask; |
| 616 | params.flags = set_flags; |
| 617 | |
| 618 | if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT, |
| 619 | ¶ms, sizeof(params), |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 620 | resp, sizeof(*resp)) != sizeof(*resp)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 621 | return -1; |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static int cros_ec_check_version(struct cros_ec_dev *dev) |
| 627 | { |
| 628 | struct ec_params_hello req; |
| 629 | struct ec_response_hello *resp; |
| 630 | |
Simon Glass | 23fb156 | 2015-03-26 09:29:30 -0600 | [diff] [blame] | 631 | struct dm_cros_ec_ops *ops; |
| 632 | int ret; |
| 633 | |
| 634 | ops = dm_cros_ec_get_ops(dev->dev); |
| 635 | if (ops->check_version) { |
| 636 | ret = ops->check_version(dev->dev); |
| 637 | if (ret) |
| 638 | return ret; |
| 639 | } |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 640 | |
| 641 | /* |
| 642 | * TODO(sjg@chromium.org). |
| 643 | * There is a strange oddity here with the EC. We could just ignore |
| 644 | * the response, i.e. pass the last two parameters as NULL and 0. |
| 645 | * In this case we won't read back very many bytes from the EC. |
| 646 | * On the I2C bus the EC gets upset about this and will try to send |
| 647 | * the bytes anyway. This means that we will have to wait for that |
| 648 | * to complete before continuing with a new EC command. |
| 649 | * |
| 650 | * This problem is probably unique to the I2C bus. |
| 651 | * |
| 652 | * So for now, just read all the data anyway. |
| 653 | */ |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 654 | |
Randall Spangler | ce3ebb9 | 2014-02-27 13:26:10 -0700 | [diff] [blame] | 655 | /* Try sending a version 3 packet */ |
| 656 | dev->protocol_version = 3; |
Simon Glass | 73c1ecf | 2014-11-11 18:06:30 -0700 | [diff] [blame] | 657 | req.in_data = 0; |
Randall Spangler | ce3ebb9 | 2014-02-27 13:26:10 -0700 | [diff] [blame] | 658 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
| 659 | (uint8_t **)&resp, sizeof(*resp)) > 0) { |
| 660 | return 0; |
| 661 | } |
| 662 | |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 663 | /* Try sending a version 2 packet */ |
| 664 | dev->protocol_version = 2; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 665 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
| 666 | (uint8_t **)&resp, sizeof(*resp)) > 0) { |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 667 | return 0; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 668 | } |
| 669 | |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 670 | /* |
| 671 | * Fail if we're still here, since the EC doesn't understand any |
| 672 | * protcol version we speak. Version 1 interface without command |
| 673 | * version is no longer supported, and we don't know about any new |
| 674 | * protocol versions. |
| 675 | */ |
| 676 | dev->protocol_version = 0; |
| 677 | printf("%s: ERROR: old EC interface not supported\n", __func__); |
| 678 | return -1; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | int cros_ec_test(struct cros_ec_dev *dev) |
| 682 | { |
| 683 | struct ec_params_hello req; |
| 684 | struct ec_response_hello *resp; |
| 685 | |
| 686 | req.in_data = 0x12345678; |
| 687 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
| 688 | (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) { |
| 689 | printf("ec_command_inptr() returned error\n"); |
| 690 | return -1; |
| 691 | } |
| 692 | if (resp->out_data != req.in_data + 0x01020304) { |
| 693 | printf("Received invalid handshake %x\n", resp->out_data); |
| 694 | return -1; |
| 695 | } |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region, |
| 701 | uint32_t *offset, uint32_t *size) |
| 702 | { |
| 703 | struct ec_params_flash_region_info p; |
| 704 | struct ec_response_flash_region_info *r; |
| 705 | int ret; |
| 706 | |
| 707 | p.region = region; |
| 708 | ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO, |
| 709 | EC_VER_FLASH_REGION_INFO, |
| 710 | &p, sizeof(p), (uint8_t **)&r, sizeof(*r)); |
| 711 | if (ret != sizeof(*r)) |
| 712 | return -1; |
| 713 | |
| 714 | if (offset) |
| 715 | *offset = r->offset; |
| 716 | if (size) |
| 717 | *size = r->size; |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size) |
| 723 | { |
| 724 | struct ec_params_flash_erase p; |
| 725 | |
| 726 | p.offset = offset; |
| 727 | p.size = size; |
| 728 | return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p), |
| 729 | NULL, 0); |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * Write a single block to the flash |
| 734 | * |
| 735 | * Write a block of data to the EC flash. The size must not exceed the flash |
| 736 | * write block size which you can obtain from cros_ec_flash_write_burst_size(). |
| 737 | * |
| 738 | * The offset starts at 0. You can obtain the region information from |
| 739 | * cros_ec_flash_offset() to find out where to write for a particular region. |
| 740 | * |
| 741 | * Attempting to write to the region where the EC is currently running from |
| 742 | * will result in an error. |
| 743 | * |
| 744 | * @param dev CROS-EC device |
| 745 | * @param data Pointer to data buffer to write |
| 746 | * @param offset Offset within flash to write to. |
| 747 | * @param size Number of bytes to write |
| 748 | * @return 0 if ok, -1 on error |
| 749 | */ |
| 750 | static int cros_ec_flash_write_block(struct cros_ec_dev *dev, |
| 751 | const uint8_t *data, uint32_t offset, uint32_t size) |
| 752 | { |
| 753 | struct ec_params_flash_write p; |
| 754 | |
| 755 | p.offset = offset; |
| 756 | p.size = size; |
Simon Glass | ece2886 | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 757 | assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE); |
| 758 | memcpy(&p + 1, data, p.size); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 759 | |
| 760 | return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0, |
| 761 | &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1; |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Return optimal flash write burst size |
| 766 | */ |
| 767 | static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev) |
| 768 | { |
Simon Glass | ece2886 | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 769 | return EC_FLASH_WRITE_VER0_SIZE; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | /** |
| 773 | * Check if a block of data is erased (all 0xff) |
| 774 | * |
| 775 | * This function is useful when dealing with flash, for checking whether a |
| 776 | * data block is erased and thus does not need to be programmed. |
| 777 | * |
| 778 | * @param data Pointer to data to check (must be word-aligned) |
| 779 | * @param size Number of bytes to check (must be word-aligned) |
| 780 | * @return 0 if erased, non-zero if any word is not erased |
| 781 | */ |
| 782 | static int cros_ec_data_is_erased(const uint32_t *data, int size) |
| 783 | { |
| 784 | assert(!(size & 3)); |
| 785 | size /= sizeof(uint32_t); |
| 786 | for (; size > 0; size -= 4, data++) |
| 787 | if (*data != -1U) |
| 788 | return 0; |
| 789 | |
| 790 | return 1; |
| 791 | } |
| 792 | |
| 793 | int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data, |
| 794 | uint32_t offset, uint32_t size) |
| 795 | { |
| 796 | uint32_t burst = cros_ec_flash_write_burst_size(dev); |
| 797 | uint32_t end, off; |
| 798 | int ret; |
| 799 | |
| 800 | /* |
| 801 | * TODO: round up to the nearest multiple of write size. Can get away |
| 802 | * without that on link right now because its write size is 4 bytes. |
| 803 | */ |
| 804 | end = offset + size; |
| 805 | for (off = offset; off < end; off += burst, data += burst) { |
| 806 | uint32_t todo; |
| 807 | |
| 808 | /* If the data is empty, there is no point in programming it */ |
| 809 | todo = min(end - off, burst); |
| 810 | if (dev->optimise_flash_write && |
| 811 | cros_ec_data_is_erased((uint32_t *)data, todo)) |
| 812 | continue; |
| 813 | |
| 814 | ret = cros_ec_flash_write_block(dev, data, off, todo); |
| 815 | if (ret) |
| 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * Read a single block from the flash |
| 824 | * |
| 825 | * Read a block of data from the EC flash. The size must not exceed the flash |
| 826 | * write block size which you can obtain from cros_ec_flash_write_burst_size(). |
| 827 | * |
| 828 | * The offset starts at 0. You can obtain the region information from |
| 829 | * cros_ec_flash_offset() to find out where to read for a particular region. |
| 830 | * |
| 831 | * @param dev CROS-EC device |
| 832 | * @param data Pointer to data buffer to read into |
| 833 | * @param offset Offset within flash to read from |
| 834 | * @param size Number of bytes to read |
| 835 | * @return 0 if ok, -1 on error |
| 836 | */ |
| 837 | static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data, |
| 838 | uint32_t offset, uint32_t size) |
| 839 | { |
| 840 | struct ec_params_flash_read p; |
| 841 | |
| 842 | p.offset = offset; |
| 843 | p.size = size; |
| 844 | |
| 845 | return ec_command(dev, EC_CMD_FLASH_READ, 0, |
| 846 | &p, sizeof(p), data, size) >= 0 ? 0 : -1; |
| 847 | } |
| 848 | |
| 849 | int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset, |
| 850 | uint32_t size) |
| 851 | { |
| 852 | uint32_t burst = cros_ec_flash_write_burst_size(dev); |
| 853 | uint32_t end, off; |
| 854 | int ret; |
| 855 | |
| 856 | end = offset + size; |
| 857 | for (off = offset; off < end; off += burst, data += burst) { |
| 858 | ret = cros_ec_flash_read_block(dev, data, off, |
| 859 | min(end - off, burst)); |
| 860 | if (ret) |
| 861 | return ret; |
| 862 | } |
| 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | int cros_ec_flash_update_rw(struct cros_ec_dev *dev, |
| 868 | const uint8_t *image, int image_size) |
| 869 | { |
| 870 | uint32_t rw_offset, rw_size; |
| 871 | int ret; |
| 872 | |
| 873 | if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size)) |
| 874 | return -1; |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 875 | if (image_size > (int)rw_size) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 876 | return -1; |
| 877 | |
| 878 | /* Invalidate the existing hash, just in case the AP reboots |
| 879 | * unexpectedly during the update. If that happened, the EC RW firmware |
| 880 | * would be invalid, but the EC would still have the original hash. |
| 881 | */ |
| 882 | ret = cros_ec_invalidate_hash(dev); |
| 883 | if (ret) |
| 884 | return ret; |
| 885 | |
| 886 | /* |
| 887 | * Erase the entire RW section, so that the EC doesn't see any garbage |
| 888 | * past the new image if it's smaller than the current image. |
| 889 | * |
| 890 | * TODO: could optimize this to erase just the current image, since |
| 891 | * presumably everything past that is 0xff's. But would still need to |
| 892 | * round up to the nearest multiple of erase size. |
| 893 | */ |
| 894 | ret = cros_ec_flash_erase(dev, rw_offset, rw_size); |
| 895 | if (ret) |
| 896 | return ret; |
| 897 | |
| 898 | /* Write the image */ |
| 899 | ret = cros_ec_flash_write(dev, image, rw_offset, image_size); |
| 900 | if (ret) |
| 901 | return ret; |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block) |
| 907 | { |
| 908 | struct ec_params_vbnvcontext p; |
| 909 | int len; |
| 910 | |
| 911 | p.op = EC_VBNV_CONTEXT_OP_READ; |
| 912 | |
| 913 | len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT, |
| 914 | &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE); |
| 915 | if (len < EC_VBNV_BLOCK_SIZE) |
| 916 | return -1; |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block) |
| 922 | { |
| 923 | struct ec_params_vbnvcontext p; |
| 924 | int len; |
| 925 | |
| 926 | p.op = EC_VBNV_CONTEXT_OP_WRITE; |
| 927 | memcpy(p.block, block, sizeof(p.block)); |
| 928 | |
| 929 | len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT, |
| 930 | &p, sizeof(p), NULL, 0); |
| 931 | if (len < 0) |
| 932 | return -1; |
| 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
Simon Glass | eb2cc51 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 937 | int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 938 | { |
Simon Glass | eb2cc51 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 939 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 940 | struct ec_params_ldo_set params; |
| 941 | |
| 942 | params.index = index; |
| 943 | params.state = state; |
| 944 | |
Simon Glass | eb2cc51 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 945 | if (ec_command_inptr(cdev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params), |
| 946 | NULL, 0)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 947 | return -1; |
| 948 | |
| 949 | return 0; |
| 950 | } |
| 951 | |
Simon Glass | eb2cc51 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 952 | int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 953 | { |
Simon Glass | eb2cc51 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 954 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 955 | struct ec_params_ldo_get params; |
| 956 | struct ec_response_ldo_get *resp; |
| 957 | |
| 958 | params.index = index; |
| 959 | |
Simon Glass | eb2cc51 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 960 | if (ec_command_inptr(cdev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params), |
| 961 | (uint8_t **)&resp, sizeof(*resp)) != |
| 962 | sizeof(*resp)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 963 | return -1; |
| 964 | |
| 965 | *state = resp->state; |
| 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 970 | int cros_ec_register(struct udevice *dev) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 971 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 972 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 973 | const void *blob = gd->fdt_blob; |
| 974 | int node = dev->of_offset; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 975 | char id[MSG_BYTES]; |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 976 | |
| 977 | cdev->dev = dev; |
Simon Glass | 0d7cc47 | 2015-01-05 20:05:32 -0700 | [diff] [blame] | 978 | gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int, |
| 979 | GPIOD_IS_IN); |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 980 | cdev->optimise_flash_write = fdtdec_get_bool(blob, node, |
| 981 | "optimise-flash-write"); |
| 982 | |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 983 | if (cros_ec_check_version(cdev)) { |
| 984 | debug("%s: Could not detect CROS-EC version\n", __func__); |
| 985 | return -CROS_EC_ERR_CHECK_VERSION; |
| 986 | } |
| 987 | |
| 988 | if (cros_ec_read_id(cdev, id, sizeof(id))) { |
| 989 | debug("%s: Could not read KBC ID\n", __func__); |
| 990 | return -CROS_EC_ERR_READ_ID; |
| 991 | } |
| 992 | |
| 993 | /* Remember this device for use by the cros_ec command */ |
Simon Glass | 46e1733 | 2015-02-17 15:29:36 -0700 | [diff] [blame] | 994 | debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n", |
| 995 | cdev->protocol_version, id); |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 996 | |
| 997 | return 0; |
| 998 | } |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 999 | |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1000 | int cros_ec_decode_region(int argc, char * const argv[]) |
| 1001 | { |
| 1002 | if (argc > 0) { |
| 1003 | if (0 == strcmp(*argv, "rw")) |
| 1004 | return EC_FLASH_REGION_RW; |
| 1005 | else if (0 == strcmp(*argv, "ro")) |
| 1006 | return EC_FLASH_REGION_RO; |
| 1007 | |
| 1008 | debug("%s: Invalid region '%s'\n", __func__, *argv); |
| 1009 | } else { |
| 1010 | debug("%s: Missing region parameter\n", __func__); |
| 1011 | } |
| 1012 | |
| 1013 | return -1; |
| 1014 | } |
| 1015 | |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1016 | int cros_ec_decode_ec_flash(const void *blob, int node, |
| 1017 | struct fdt_cros_ec *config) |
Simon Glass | 3c59abc | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 1018 | { |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1019 | int flash_node; |
Simon Glass | 3c59abc | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 1020 | |
| 1021 | flash_node = fdt_subnode_offset(blob, node, "flash"); |
| 1022 | if (flash_node < 0) { |
| 1023 | debug("Failed to find flash node\n"); |
| 1024 | return -1; |
| 1025 | } |
| 1026 | |
| 1027 | if (fdtdec_read_fmap_entry(blob, flash_node, "flash", |
| 1028 | &config->flash)) { |
| 1029 | debug("Failed to decode flash node in chrome-ec'\n"); |
| 1030 | return -1; |
| 1031 | } |
| 1032 | |
| 1033 | config->flash_erase_value = fdtdec_get_int(blob, flash_node, |
| 1034 | "erase-value", -1); |
| 1035 | for (node = fdt_first_subnode(blob, flash_node); node >= 0; |
| 1036 | node = fdt_next_subnode(blob, node)) { |
| 1037 | const char *name = fdt_get_name(blob, node, NULL); |
| 1038 | enum ec_flash_region region; |
| 1039 | |
| 1040 | if (0 == strcmp(name, "ro")) { |
| 1041 | region = EC_FLASH_REGION_RO; |
| 1042 | } else if (0 == strcmp(name, "rw")) { |
| 1043 | region = EC_FLASH_REGION_RW; |
| 1044 | } else if (0 == strcmp(name, "wp-ro")) { |
| 1045 | region = EC_FLASH_REGION_WP_RO; |
| 1046 | } else { |
| 1047 | debug("Unknown EC flash region name '%s'\n", name); |
| 1048 | return -1; |
| 1049 | } |
| 1050 | |
| 1051 | if (fdtdec_read_fmap_entry(blob, node, "reg", |
| 1052 | &config->region[region])) { |
| 1053 | debug("Failed to decode flash region in chrome-ec'\n"); |
| 1054 | return -1; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
Simon Glass | 9ad07af | 2015-08-03 08:19:23 -0600 | [diff] [blame] | 1061 | int cros_ec_i2c_tunnel(struct udevice *dev, struct i2c_msg *in, int nmsgs) |
| 1062 | { |
| 1063 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
| 1064 | union { |
| 1065 | struct ec_params_i2c_passthru p; |
| 1066 | uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE]; |
| 1067 | } params; |
| 1068 | union { |
| 1069 | struct ec_response_i2c_passthru r; |
| 1070 | uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE]; |
| 1071 | } response; |
| 1072 | struct ec_params_i2c_passthru *p = ¶ms.p; |
| 1073 | struct ec_response_i2c_passthru *r = &response.r; |
| 1074 | struct ec_params_i2c_passthru_msg *msg; |
| 1075 | uint8_t *pdata, *read_ptr = NULL; |
| 1076 | int read_len; |
| 1077 | int size; |
| 1078 | int rv; |
| 1079 | int i; |
| 1080 | |
| 1081 | p->port = 0; |
| 1082 | |
| 1083 | p->num_msgs = nmsgs; |
| 1084 | size = sizeof(*p) + p->num_msgs * sizeof(*msg); |
| 1085 | |
| 1086 | /* Create a message to write the register address and optional data */ |
| 1087 | pdata = (uint8_t *)p + size; |
| 1088 | |
| 1089 | read_len = 0; |
| 1090 | for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) { |
| 1091 | bool is_read = in->flags & I2C_M_RD; |
| 1092 | |
| 1093 | msg->addr_flags = in->addr; |
| 1094 | msg->len = in->len; |
| 1095 | if (is_read) { |
| 1096 | msg->addr_flags |= EC_I2C_FLAG_READ; |
| 1097 | read_len += in->len; |
| 1098 | read_ptr = in->buf; |
| 1099 | if (sizeof(*r) + read_len > sizeof(response)) { |
| 1100 | puts("Read length too big for buffer\n"); |
| 1101 | return -1; |
| 1102 | } |
| 1103 | } else { |
| 1104 | if (pdata - (uint8_t *)p + in->len > sizeof(params)) { |
| 1105 | puts("Params too large for buffer\n"); |
| 1106 | return -1; |
| 1107 | } |
| 1108 | memcpy(pdata, in->buf, in->len); |
| 1109 | pdata += in->len; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | rv = ec_command(cdev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p, |
| 1114 | r, sizeof(*r) + read_len); |
| 1115 | if (rv < 0) |
| 1116 | return rv; |
| 1117 | |
| 1118 | /* Parse response */ |
| 1119 | if (r->i2c_status & EC_I2C_STATUS_ERROR) { |
| 1120 | printf("Transfer failed with status=0x%x\n", r->i2c_status); |
| 1121 | return -1; |
| 1122 | } |
| 1123 | |
| 1124 | if (rv < sizeof(*r) + read_len) { |
| 1125 | puts("Truncated read response\n"); |
| 1126 | return -1; |
| 1127 | } |
| 1128 | |
| 1129 | /* We only support a single read message for each transfer */ |
| 1130 | if (read_len) |
| 1131 | memcpy(read_ptr, r->data, read_len); |
| 1132 | |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
Simon Glass | c4af76a | 2014-02-27 13:26:06 -0700 | [diff] [blame] | 1136 | #ifdef CONFIG_CMD_CROS_EC |
| 1137 | |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1138 | /** |
| 1139 | * Perform a flash read or write command |
| 1140 | * |
| 1141 | * @param dev CROS-EC device to read/write |
| 1142 | * @param is_write 1 do to a write, 0 to do a read |
| 1143 | * @param argc Number of arguments |
| 1144 | * @param argv Arguments (2 is region, 3 is address) |
| 1145 | * @return 0 for ok, 1 for a usage error or -ve for ec command error |
| 1146 | * (negative EC_RES_...) |
| 1147 | */ |
| 1148 | static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc, |
| 1149 | char * const argv[]) |
| 1150 | { |
| 1151 | uint32_t offset, size = -1U, region_size; |
| 1152 | unsigned long addr; |
| 1153 | char *endp; |
| 1154 | int region; |
| 1155 | int ret; |
| 1156 | |
| 1157 | region = cros_ec_decode_region(argc - 2, argv + 2); |
| 1158 | if (region == -1) |
| 1159 | return 1; |
| 1160 | if (argc < 4) |
| 1161 | return 1; |
| 1162 | addr = simple_strtoul(argv[3], &endp, 16); |
| 1163 | if (*argv[3] == 0 || *endp != 0) |
| 1164 | return 1; |
| 1165 | if (argc > 4) { |
| 1166 | size = simple_strtoul(argv[4], &endp, 16); |
| 1167 | if (*argv[4] == 0 || *endp != 0) |
| 1168 | return 1; |
| 1169 | } |
| 1170 | |
| 1171 | ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size); |
| 1172 | if (ret) { |
| 1173 | debug("%s: Could not read region info\n", __func__); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | if (size == -1U) |
| 1177 | size = region_size; |
| 1178 | |
| 1179 | ret = is_write ? |
| 1180 | cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) : |
| 1181 | cros_ec_flash_read(dev, (uint8_t *)addr, offset, size); |
| 1182 | if (ret) { |
| 1183 | debug("%s: Could not %s region\n", __func__, |
| 1184 | is_write ? "write" : "read"); |
| 1185 | return ret; |
| 1186 | } |
Simon Glass | 9b0d1c9 | 2014-02-27 13:26:14 -0700 | [diff] [blame] | 1187 | |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1191 | static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 1192 | { |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1193 | struct cros_ec_dev *dev; |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1194 | struct udevice *udev; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1195 | const char *cmd; |
| 1196 | int ret = 0; |
| 1197 | |
| 1198 | if (argc < 2) |
| 1199 | return CMD_RET_USAGE; |
| 1200 | |
| 1201 | cmd = argv[1]; |
| 1202 | if (0 == strcmp("init", cmd)) { |
Simon Glass | f9a2d9a | 2015-03-26 09:29:31 -0600 | [diff] [blame] | 1203 | /* Remove any existing device */ |
| 1204 | ret = uclass_find_device(UCLASS_CROS_EC, 0, &udev); |
| 1205 | if (!ret) |
| 1206 | device_remove(udev); |
| 1207 | ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1208 | if (ret) { |
| 1209 | printf("Could not init cros_ec device (err %d)\n", ret); |
| 1210 | return 1; |
| 1211 | } |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1215 | ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev); |
| 1216 | if (ret) { |
| 1217 | printf("Cannot get cros-ec device (err=%d)\n", ret); |
| 1218 | return 1; |
| 1219 | } |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 1220 | dev = dev_get_uclass_priv(udev); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1221 | if (0 == strcmp("id", cmd)) { |
| 1222 | char id[MSG_BYTES]; |
| 1223 | |
| 1224 | if (cros_ec_read_id(dev, id, sizeof(id))) { |
| 1225 | debug("%s: Could not read KBC ID\n", __func__); |
| 1226 | return 1; |
| 1227 | } |
| 1228 | printf("%s\n", id); |
| 1229 | } else if (0 == strcmp("info", cmd)) { |
Simon Glass | ece2886 | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 1230 | struct ec_response_mkbp_info info; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1231 | |
| 1232 | if (cros_ec_info(dev, &info)) { |
| 1233 | debug("%s: Could not read KBC info\n", __func__); |
| 1234 | return 1; |
| 1235 | } |
| 1236 | printf("rows = %u\n", info.rows); |
| 1237 | printf("cols = %u\n", info.cols); |
| 1238 | printf("switches = %#x\n", info.switches); |
| 1239 | } else if (0 == strcmp("curimage", cmd)) { |
| 1240 | enum ec_current_image image; |
| 1241 | |
| 1242 | if (cros_ec_read_current_image(dev, &image)) { |
| 1243 | debug("%s: Could not read KBC image\n", __func__); |
| 1244 | return 1; |
| 1245 | } |
| 1246 | printf("%d\n", image); |
| 1247 | } else if (0 == strcmp("hash", cmd)) { |
| 1248 | struct ec_response_vboot_hash hash; |
| 1249 | int i; |
| 1250 | |
| 1251 | if (cros_ec_read_hash(dev, &hash)) { |
| 1252 | debug("%s: Could not read KBC hash\n", __func__); |
| 1253 | return 1; |
| 1254 | } |
| 1255 | |
| 1256 | if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256) |
| 1257 | printf("type: SHA-256\n"); |
| 1258 | else |
| 1259 | printf("type: %d\n", hash.hash_type); |
| 1260 | |
| 1261 | printf("offset: 0x%08x\n", hash.offset); |
| 1262 | printf("size: 0x%08x\n", hash.size); |
| 1263 | |
| 1264 | printf("digest: "); |
| 1265 | for (i = 0; i < hash.digest_size; i++) |
| 1266 | printf("%02x", hash.hash_digest[i]); |
| 1267 | printf("\n"); |
| 1268 | } else if (0 == strcmp("reboot", cmd)) { |
| 1269 | int region; |
| 1270 | enum ec_reboot_cmd cmd; |
| 1271 | |
| 1272 | if (argc >= 3 && !strcmp(argv[2], "cold")) |
| 1273 | cmd = EC_REBOOT_COLD; |
| 1274 | else { |
| 1275 | region = cros_ec_decode_region(argc - 2, argv + 2); |
| 1276 | if (region == EC_FLASH_REGION_RO) |
| 1277 | cmd = EC_REBOOT_JUMP_RO; |
| 1278 | else if (region == EC_FLASH_REGION_RW) |
| 1279 | cmd = EC_REBOOT_JUMP_RW; |
| 1280 | else |
| 1281 | return CMD_RET_USAGE; |
| 1282 | } |
| 1283 | |
| 1284 | if (cros_ec_reboot(dev, cmd, 0)) { |
| 1285 | debug("%s: Could not reboot KBC\n", __func__); |
| 1286 | return 1; |
| 1287 | } |
| 1288 | } else if (0 == strcmp("events", cmd)) { |
| 1289 | uint32_t events; |
| 1290 | |
| 1291 | if (cros_ec_get_host_events(dev, &events)) { |
| 1292 | debug("%s: Could not read host events\n", __func__); |
| 1293 | return 1; |
| 1294 | } |
| 1295 | printf("0x%08x\n", events); |
| 1296 | } else if (0 == strcmp("clrevents", cmd)) { |
| 1297 | uint32_t events = 0x7fffffff; |
| 1298 | |
| 1299 | if (argc >= 3) |
| 1300 | events = simple_strtol(argv[2], NULL, 0); |
| 1301 | |
| 1302 | if (cros_ec_clear_host_events(dev, events)) { |
| 1303 | debug("%s: Could not clear host events\n", __func__); |
| 1304 | return 1; |
| 1305 | } |
| 1306 | } else if (0 == strcmp("read", cmd)) { |
| 1307 | ret = do_read_write(dev, 0, argc, argv); |
| 1308 | if (ret > 0) |
| 1309 | return CMD_RET_USAGE; |
| 1310 | } else if (0 == strcmp("write", cmd)) { |
| 1311 | ret = do_read_write(dev, 1, argc, argv); |
| 1312 | if (ret > 0) |
| 1313 | return CMD_RET_USAGE; |
| 1314 | } else if (0 == strcmp("erase", cmd)) { |
| 1315 | int region = cros_ec_decode_region(argc - 2, argv + 2); |
| 1316 | uint32_t offset, size; |
| 1317 | |
| 1318 | if (region == -1) |
| 1319 | return CMD_RET_USAGE; |
| 1320 | if (cros_ec_flash_offset(dev, region, &offset, &size)) { |
| 1321 | debug("%s: Could not read region info\n", __func__); |
| 1322 | ret = -1; |
| 1323 | } else { |
| 1324 | ret = cros_ec_flash_erase(dev, offset, size); |
| 1325 | if (ret) { |
| 1326 | debug("%s: Could not erase region\n", |
| 1327 | __func__); |
| 1328 | } |
| 1329 | } |
| 1330 | } else if (0 == strcmp("regioninfo", cmd)) { |
| 1331 | int region = cros_ec_decode_region(argc - 2, argv + 2); |
| 1332 | uint32_t offset, size; |
| 1333 | |
| 1334 | if (region == -1) |
| 1335 | return CMD_RET_USAGE; |
| 1336 | ret = cros_ec_flash_offset(dev, region, &offset, &size); |
| 1337 | if (ret) { |
| 1338 | debug("%s: Could not read region info\n", __func__); |
| 1339 | } else { |
| 1340 | printf("Region: %s\n", region == EC_FLASH_REGION_RO ? |
| 1341 | "RO" : "RW"); |
| 1342 | printf("Offset: %x\n", offset); |
| 1343 | printf("Size: %x\n", size); |
| 1344 | } |
| 1345 | } else if (0 == strcmp("vbnvcontext", cmd)) { |
| 1346 | uint8_t block[EC_VBNV_BLOCK_SIZE]; |
| 1347 | char buf[3]; |
| 1348 | int i, len; |
| 1349 | unsigned long result; |
| 1350 | |
| 1351 | if (argc <= 2) { |
| 1352 | ret = cros_ec_read_vbnvcontext(dev, block); |
| 1353 | if (!ret) { |
| 1354 | printf("vbnv_block: "); |
| 1355 | for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) |
| 1356 | printf("%02x", block[i]); |
| 1357 | putc('\n'); |
| 1358 | } |
| 1359 | } else { |
| 1360 | /* |
| 1361 | * TODO(clchiou): Move this to a utility function as |
| 1362 | * cmd_spi might want to call it. |
| 1363 | */ |
| 1364 | memset(block, 0, EC_VBNV_BLOCK_SIZE); |
| 1365 | len = strlen(argv[2]); |
| 1366 | buf[2] = '\0'; |
| 1367 | for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) { |
| 1368 | if (i * 2 >= len) |
| 1369 | break; |
| 1370 | buf[0] = argv[2][i * 2]; |
| 1371 | if (i * 2 + 1 >= len) |
| 1372 | buf[1] = '0'; |
| 1373 | else |
| 1374 | buf[1] = argv[2][i * 2 + 1]; |
| 1375 | strict_strtoul(buf, 16, &result); |
| 1376 | block[i] = result; |
| 1377 | } |
| 1378 | ret = cros_ec_write_vbnvcontext(dev, block); |
| 1379 | } |
| 1380 | if (ret) { |
| 1381 | debug("%s: Could not %s VbNvContext\n", __func__, |
| 1382 | argc <= 2 ? "read" : "write"); |
| 1383 | } |
| 1384 | } else if (0 == strcmp("test", cmd)) { |
| 1385 | int result = cros_ec_test(dev); |
| 1386 | |
| 1387 | if (result) |
| 1388 | printf("Test failed with error %d\n", result); |
| 1389 | else |
| 1390 | puts("Test passed\n"); |
| 1391 | } else if (0 == strcmp("version", cmd)) { |
| 1392 | struct ec_response_get_version *p; |
| 1393 | char *build_string; |
| 1394 | |
| 1395 | ret = cros_ec_read_version(dev, &p); |
| 1396 | if (!ret) { |
| 1397 | /* Print versions */ |
| 1398 | printf("RO version: %1.*s\n", |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 1399 | (int)sizeof(p->version_string_ro), |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1400 | p->version_string_ro); |
| 1401 | printf("RW version: %1.*s\n", |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 1402 | (int)sizeof(p->version_string_rw), |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1403 | p->version_string_rw); |
| 1404 | printf("Firmware copy: %s\n", |
| 1405 | (p->current_image < |
| 1406 | ARRAY_SIZE(ec_current_image_name) ? |
| 1407 | ec_current_image_name[p->current_image] : |
| 1408 | "?")); |
| 1409 | ret = cros_ec_read_build_info(dev, &build_string); |
| 1410 | if (!ret) |
| 1411 | printf("Build info: %s\n", build_string); |
| 1412 | } |
| 1413 | } else if (0 == strcmp("ldo", cmd)) { |
| 1414 | uint8_t index, state; |
| 1415 | char *endp; |
| 1416 | |
| 1417 | if (argc < 3) |
| 1418 | return CMD_RET_USAGE; |
| 1419 | index = simple_strtoul(argv[2], &endp, 10); |
| 1420 | if (*argv[2] == 0 || *endp != 0) |
| 1421 | return CMD_RET_USAGE; |
| 1422 | if (argc > 3) { |
| 1423 | state = simple_strtoul(argv[3], &endp, 10); |
| 1424 | if (*argv[3] == 0 || *endp != 0) |
| 1425 | return CMD_RET_USAGE; |
Simon Glass | eb2cc51 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 1426 | ret = cros_ec_set_ldo(udev, index, state); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1427 | } else { |
Simon Glass | eb2cc51 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 1428 | ret = cros_ec_get_ldo(udev, index, &state); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1429 | if (!ret) { |
| 1430 | printf("LDO%d: %s\n", index, |
| 1431 | state == EC_LDO_STATE_ON ? |
| 1432 | "on" : "off"); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | if (ret) { |
| 1437 | debug("%s: Could not access LDO%d\n", __func__, index); |
| 1438 | return ret; |
| 1439 | } |
| 1440 | } else { |
| 1441 | return CMD_RET_USAGE; |
| 1442 | } |
| 1443 | |
| 1444 | if (ret < 0) { |
| 1445 | printf("Error: CROS-EC command failed (error %d)\n", ret); |
| 1446 | ret = 1; |
| 1447 | } |
| 1448 | |
| 1449 | return ret; |
| 1450 | } |
| 1451 | |
| 1452 | U_BOOT_CMD( |
Simon Glass | 9b0d1c9 | 2014-02-27 13:26:14 -0700 | [diff] [blame] | 1453 | crosec, 6, 1, do_cros_ec, |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1454 | "CROS-EC utility command", |
| 1455 | "init Re-init CROS-EC (done on startup automatically)\n" |
| 1456 | "crosec id Read CROS-EC ID\n" |
| 1457 | "crosec info Read CROS-EC info\n" |
| 1458 | "crosec curimage Read CROS-EC current image\n" |
| 1459 | "crosec hash Read CROS-EC hash\n" |
| 1460 | "crosec reboot [rw | ro | cold] Reboot CROS-EC\n" |
| 1461 | "crosec events Read CROS-EC host events\n" |
| 1462 | "crosec clrevents [mask] Clear CROS-EC host events\n" |
| 1463 | "crosec regioninfo <ro|rw> Read image info\n" |
| 1464 | "crosec erase <ro|rw> Erase EC image\n" |
| 1465 | "crosec read <ro|rw> <addr> [<size>] Read EC image\n" |
| 1466 | "crosec write <ro|rw> <addr> [<size>] Write EC image\n" |
| 1467 | "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n" |
| 1468 | "crosec ldo <idx> [<state>] Switch/Read LDO state\n" |
| 1469 | "crosec test run tests on cros_ec\n" |
Simon Glass | 66d14fc | 2015-08-03 08:19:33 -0600 | [diff] [blame] | 1470 | "crosec version Read CROS-EC version" |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1471 | ); |
| 1472 | #endif |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1473 | |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1474 | UCLASS_DRIVER(cros_ec) = { |
| 1475 | .id = UCLASS_CROS_EC, |
| 1476 | .name = "cros_ec", |
| 1477 | .per_device_auto_alloc_size = sizeof(struct cros_ec_dev), |
Simon Glass | 1823034 | 2016-07-05 17:10:10 -0600 | [diff] [blame] | 1478 | .post_bind = dm_scan_fdt_dev, |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1479 | }; |