Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Chromium OS cros_ec driver |
| 4 | * |
| 5 | * Copyright (c) 2012 The Chromium OS Authors. |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
Simon Glass | ece2886 | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 9 | * This is the interface to the Chrome OS EC. It provides keyboard functions, |
| 10 | * power control and battery management. Quite a few other functions are |
| 11 | * provided to enable the EC software to be updated, talk to the EC's I2C bus |
| 12 | * and store a small amount of data in a memory which persists while the EC |
| 13 | * is not reset. |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <common.h> |
| 17 | #include <command.h> |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 18 | #include <dm.h> |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 19 | #include <i2c.h> |
| 20 | #include <cros_ec.h> |
| 21 | #include <fdtdec.h> |
| 22 | #include <malloc.h> |
| 23 | #include <spi.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 24 | #include <linux/errno.h> |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 25 | #include <asm/io.h> |
| 26 | #include <asm-generic/gpio.h> |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 27 | #include <dm/device-internal.h> |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 28 | #include <dm/of_extra.h> |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 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 | void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len) |
| 45 | { |
| 46 | #ifdef DEBUG |
| 47 | int i; |
| 48 | |
| 49 | printf("%s: ", name); |
| 50 | if (cmd != -1) |
| 51 | printf("cmd=%#x: ", cmd); |
| 52 | for (i = 0; i < len; i++) |
| 53 | printf("%02x ", data[i]); |
| 54 | printf("\n"); |
| 55 | #endif |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Calculate a simple 8-bit checksum of a data block |
| 60 | * |
| 61 | * @param data Data block to checksum |
| 62 | * @param size Size of data block in bytes |
| 63 | * @return checksum value (0 to 255) |
| 64 | */ |
| 65 | int cros_ec_calc_checksum(const uint8_t *data, int size) |
| 66 | { |
| 67 | int csum, i; |
| 68 | |
| 69 | for (i = csum = 0; i < size; i++) |
| 70 | csum += data[i]; |
| 71 | return csum & 0xff; |
| 72 | } |
| 73 | |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Create a request packet for protocol version 3. |
| 76 | * |
| 77 | * The packet is stored in the device's internal output buffer. |
| 78 | * |
| 79 | * @param dev CROS-EC device |
| 80 | * @param cmd Command to send (EC_CMD_...) |
| 81 | * @param cmd_version Version of command to send (EC_VER_...) |
| 82 | * @param dout Output data (may be NULL If dout_len=0) |
| 83 | * @param dout_len Size of output data in bytes |
| 84 | * @return packet size in bytes, or <0 if error. |
| 85 | */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 86 | static int create_proto3_request(struct cros_ec_dev *cdev, |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 87 | int cmd, int cmd_version, |
| 88 | const void *dout, int dout_len) |
| 89 | { |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 90 | struct ec_host_request *rq = (struct ec_host_request *)cdev->dout; |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 91 | int out_bytes = dout_len + sizeof(*rq); |
| 92 | |
| 93 | /* Fail if output size is too big */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 94 | if (out_bytes > (int)sizeof(cdev->dout)) { |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 95 | debug("%s: Cannot send %d bytes\n", __func__, dout_len); |
| 96 | return -EC_RES_REQUEST_TRUNCATED; |
| 97 | } |
| 98 | |
| 99 | /* Fill in request packet */ |
| 100 | rq->struct_version = EC_HOST_REQUEST_VERSION; |
| 101 | rq->checksum = 0; |
| 102 | rq->command = cmd; |
| 103 | rq->command_version = cmd_version; |
| 104 | rq->reserved = 0; |
| 105 | rq->data_len = dout_len; |
| 106 | |
| 107 | /* Copy data after header */ |
| 108 | memcpy(rq + 1, dout, dout_len); |
| 109 | |
| 110 | /* Write checksum field so the entire packet sums to 0 */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 111 | rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes)); |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 112 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 113 | cros_ec_dump_data("out", cmd, cdev->dout, out_bytes); |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 114 | |
| 115 | /* Return size of request packet */ |
| 116 | return out_bytes; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Prepare the device to receive a protocol version 3 response. |
| 121 | * |
| 122 | * @param dev CROS-EC device |
| 123 | * @param din_len Maximum size of response in bytes |
| 124 | * @return maximum expected number of bytes in response, or <0 if error. |
| 125 | */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 126 | static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len) |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 127 | { |
| 128 | int in_bytes = din_len + sizeof(struct ec_host_response); |
| 129 | |
| 130 | /* Fail if input size is too big */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 131 | if (in_bytes > (int)sizeof(cdev->din)) { |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 132 | debug("%s: Cannot receive %d bytes\n", __func__, din_len); |
| 133 | return -EC_RES_RESPONSE_TOO_BIG; |
| 134 | } |
| 135 | |
| 136 | /* Return expected size of response packet */ |
| 137 | return in_bytes; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Handle a protocol version 3 response packet. |
| 142 | * |
| 143 | * The packet must already be stored in the device's internal input buffer. |
| 144 | * |
| 145 | * @param dev CROS-EC device |
| 146 | * @param dinp Returns pointer to response data |
| 147 | * @param din_len Maximum size of response in bytes |
Simon Glass | d3d9c06 | 2015-01-25 08:27:17 -0700 | [diff] [blame] | 148 | * @return number of bytes of response data, or <0 if error. Note that error |
| 149 | * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they |
| 150 | * overlap!) |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 151 | */ |
| 152 | static int handle_proto3_response(struct cros_ec_dev *dev, |
| 153 | uint8_t **dinp, int din_len) |
| 154 | { |
| 155 | struct ec_host_response *rs = (struct ec_host_response *)dev->din; |
| 156 | int in_bytes; |
| 157 | int csum; |
| 158 | |
| 159 | cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs)); |
| 160 | |
| 161 | /* Check input data */ |
| 162 | if (rs->struct_version != EC_HOST_RESPONSE_VERSION) { |
| 163 | debug("%s: EC response version mismatch\n", __func__); |
| 164 | return -EC_RES_INVALID_RESPONSE; |
| 165 | } |
| 166 | |
| 167 | if (rs->reserved) { |
| 168 | debug("%s: EC response reserved != 0\n", __func__); |
| 169 | return -EC_RES_INVALID_RESPONSE; |
| 170 | } |
| 171 | |
| 172 | if (rs->data_len > din_len) { |
| 173 | debug("%s: EC returned too much data\n", __func__); |
| 174 | return -EC_RES_RESPONSE_TOO_BIG; |
| 175 | } |
| 176 | |
| 177 | cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len); |
| 178 | |
| 179 | /* Update in_bytes to actual data size */ |
| 180 | in_bytes = sizeof(*rs) + rs->data_len; |
| 181 | |
| 182 | /* Verify checksum */ |
| 183 | csum = cros_ec_calc_checksum(dev->din, in_bytes); |
| 184 | if (csum) { |
| 185 | debug("%s: EC response checksum invalid: 0x%02x\n", __func__, |
| 186 | csum); |
| 187 | return -EC_RES_INVALID_CHECKSUM; |
| 188 | } |
| 189 | |
| 190 | /* Return error result, if any */ |
| 191 | if (rs->result) |
| 192 | return -(int)rs->result; |
| 193 | |
| 194 | /* If we're still here, set response data pointer and return length */ |
| 195 | *dinp = (uint8_t *)(rs + 1); |
| 196 | |
| 197 | return rs->data_len; |
| 198 | } |
| 199 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 200 | static int send_command_proto3(struct cros_ec_dev *cdev, |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 201 | int cmd, int cmd_version, |
| 202 | const void *dout, int dout_len, |
| 203 | uint8_t **dinp, int din_len) |
| 204 | { |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 205 | struct dm_cros_ec_ops *ops; |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 206 | int out_bytes, in_bytes; |
| 207 | int rv; |
| 208 | |
| 209 | /* Create request packet */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 210 | out_bytes = create_proto3_request(cdev, cmd, cmd_version, |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 211 | dout, dout_len); |
| 212 | if (out_bytes < 0) |
| 213 | return out_bytes; |
| 214 | |
| 215 | /* Prepare response buffer */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 216 | in_bytes = prepare_proto3_response_buffer(cdev, din_len); |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 217 | if (in_bytes < 0) |
| 218 | return in_bytes; |
| 219 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 220 | ops = dm_cros_ec_get_ops(cdev->dev); |
| 221 | rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) : |
| 222 | -ENOSYS; |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 223 | if (rv < 0) |
| 224 | return rv; |
| 225 | |
| 226 | /* Process the response */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 227 | return handle_proto3_response(cdev, dinp, din_len); |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 230 | static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version, |
| 231 | const void *dout, int dout_len, |
| 232 | uint8_t **dinp, int din_len) |
| 233 | { |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 234 | struct dm_cros_ec_ops *ops; |
Simon Glass | 13c7c5b | 2014-02-27 13:26:09 -0700 | [diff] [blame] | 235 | int ret = -1; |
| 236 | |
| 237 | /* Handle protocol version 3 support */ |
| 238 | if (dev->protocol_version == 3) { |
| 239 | return send_command_proto3(dev, cmd, cmd_version, |
| 240 | dout, dout_len, dinp, din_len); |
| 241 | } |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 242 | |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 243 | ops = dm_cros_ec_get_ops(dev->dev); |
| 244 | ret = ops->command(dev->dev, cmd, cmd_version, |
| 245 | (const uint8_t *)dout, dout_len, dinp, din_len); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Send a command to the CROS-EC device and return the reply. |
| 252 | * |
| 253 | * The device's internal input/output buffers are used. |
| 254 | * |
| 255 | * @param dev CROS-EC device |
| 256 | * @param cmd Command to send (EC_CMD_...) |
| 257 | * @param cmd_version Version of command to send (EC_VER_...) |
| 258 | * @param dout Output data (may be NULL If dout_len=0) |
| 259 | * @param dout_len Size of output data in bytes |
| 260 | * @param dinp Response data (may be NULL If din_len=0). |
| 261 | * If not NULL, it will be updated to point to the data |
| 262 | * and will always be double word aligned (64-bits) |
| 263 | * @param din_len Maximum size of response in bytes |
Simon Glass | d3d9c06 | 2015-01-25 08:27:17 -0700 | [diff] [blame] | 264 | * @return number of bytes in response, or -ve on error |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 265 | */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 266 | static int ec_command_inptr(struct udevice *dev, uint8_t cmd, |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 267 | int cmd_version, const void *dout, int dout_len, |
| 268 | uint8_t **dinp, int din_len) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 269 | { |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 270 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 271 | uint8_t *din = NULL; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 272 | int len; |
| 273 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 274 | len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din, |
| 275 | din_len); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 276 | |
| 277 | /* If the command doesn't complete, wait a while */ |
| 278 | if (len == -EC_RES_IN_PROGRESS) { |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 279 | struct ec_response_get_comms_status *resp = NULL; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 280 | ulong start; |
| 281 | |
| 282 | /* Wait for command to complete */ |
| 283 | start = get_timer(0); |
| 284 | do { |
| 285 | int ret; |
| 286 | |
| 287 | mdelay(50); /* Insert some reasonable delay */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 288 | ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0, |
| 289 | NULL, 0, |
| 290 | (uint8_t **)&resp, sizeof(*resp)); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 291 | if (ret < 0) |
| 292 | return ret; |
| 293 | |
| 294 | if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) { |
| 295 | debug("%s: Command %#02x timeout\n", |
| 296 | __func__, cmd); |
| 297 | return -EC_RES_TIMEOUT; |
| 298 | } |
| 299 | } while (resp->flags & EC_COMMS_STATUS_PROCESSING); |
| 300 | |
| 301 | /* OK it completed, so read the status response */ |
| 302 | /* not sure why it was 0 for the last argument */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 303 | len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0, |
| 304 | &din, din_len); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 305 | } |
| 306 | |
Simon Glass | 0b8e8ce | 2017-05-18 20:09:22 -0600 | [diff] [blame] | 307 | debug("%s: len=%d, din=%p\n", __func__, len, din); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 308 | if (dinp) { |
| 309 | /* If we have any data to return, it must be 64bit-aligned */ |
| 310 | assert(len <= 0 || !((uintptr_t)din & 7)); |
| 311 | *dinp = din; |
| 312 | } |
| 313 | |
| 314 | return len; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Send a command to the CROS-EC device and return the reply. |
| 319 | * |
| 320 | * The device's internal input/output buffers are used. |
| 321 | * |
| 322 | * @param dev CROS-EC device |
| 323 | * @param cmd Command to send (EC_CMD_...) |
| 324 | * @param cmd_version Version of command to send (EC_VER_...) |
| 325 | * @param dout Output data (may be NULL If dout_len=0) |
| 326 | * @param dout_len Size of output data in bytes |
| 327 | * @param din Response data (may be NULL If din_len=0). |
| 328 | * It not NULL, it is a place for ec_command() to copy the |
| 329 | * data to. |
| 330 | * @param din_len Maximum size of response in bytes |
Simon Glass | d3d9c06 | 2015-01-25 08:27:17 -0700 | [diff] [blame] | 331 | * @return number of bytes in response, or -ve on error |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 332 | */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 333 | static int ec_command(struct udevice *dev, uint8_t cmd, int cmd_version, |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 334 | const void *dout, int dout_len, |
| 335 | void *din, int din_len) |
| 336 | { |
| 337 | uint8_t *in_buffer; |
| 338 | int len; |
| 339 | |
| 340 | assert((din_len == 0) || din); |
| 341 | len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len, |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 342 | &in_buffer, din_len); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 343 | if (len > 0) { |
| 344 | /* |
| 345 | * If we were asked to put it somewhere, do so, otherwise just |
| 346 | * disregard the result. |
| 347 | */ |
| 348 | if (din && in_buffer) { |
| 349 | assert(len <= din_len); |
| 350 | memmove(din, in_buffer, len); |
| 351 | } |
| 352 | } |
| 353 | return len; |
| 354 | } |
| 355 | |
Simon Glass | fb72640 | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 356 | 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] | 357 | { |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 358 | if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan, |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 359 | sizeof(scan->data)) != sizeof(scan->data)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 360 | return -1; |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 365 | int cros_ec_read_id(struct udevice *dev, char *id, int maxlen) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 366 | { |
| 367 | struct ec_response_get_version *r; |
| 368 | |
| 369 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 370 | (uint8_t **)&r, sizeof(*r)) != sizeof(*r)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 371 | return -1; |
| 372 | |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 373 | if (maxlen > (int)sizeof(r->version_string_ro)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 374 | maxlen = sizeof(r->version_string_ro); |
| 375 | |
| 376 | switch (r->current_image) { |
| 377 | case EC_IMAGE_RO: |
| 378 | memcpy(id, r->version_string_ro, maxlen); |
| 379 | break; |
| 380 | case EC_IMAGE_RW: |
| 381 | memcpy(id, r->version_string_rw, maxlen); |
| 382 | break; |
| 383 | default: |
| 384 | return -1; |
| 385 | } |
| 386 | |
| 387 | id[maxlen - 1] = '\0'; |
| 388 | return 0; |
| 389 | } |
| 390 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 391 | int cros_ec_read_version(struct udevice *dev, |
| 392 | struct ec_response_get_version **versionp) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 393 | { |
| 394 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, |
| 395 | (uint8_t **)versionp, sizeof(**versionp)) |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 396 | != sizeof(**versionp)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 397 | return -1; |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 402 | int cros_ec_read_build_info(struct udevice *dev, char **strp) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 403 | { |
| 404 | 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] | 405 | (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 406 | return -1; |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 411 | int cros_ec_read_current_image(struct udevice *dev, |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 412 | enum ec_current_image *image) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 413 | { |
| 414 | struct ec_response_get_version *r; |
| 415 | |
| 416 | if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 417 | (uint8_t **)&r, sizeof(*r)) != sizeof(*r)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 418 | return -1; |
| 419 | |
| 420 | *image = r->current_image; |
| 421 | return 0; |
| 422 | } |
| 423 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 424 | static int cros_ec_wait_on_hash_done(struct udevice *dev, |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 425 | struct ec_response_vboot_hash *hash) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 426 | { |
| 427 | struct ec_params_vboot_hash p; |
| 428 | ulong start; |
| 429 | |
| 430 | start = get_timer(0); |
| 431 | while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) { |
| 432 | mdelay(50); /* Insert some reasonable delay */ |
| 433 | |
| 434 | p.cmd = EC_VBOOT_HASH_GET; |
| 435 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
| 436 | hash, sizeof(*hash)) < 0) |
| 437 | return -1; |
| 438 | |
| 439 | if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) { |
| 440 | debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__); |
| 441 | return -EC_RES_TIMEOUT; |
| 442 | } |
| 443 | } |
| 444 | return 0; |
| 445 | } |
| 446 | |
Simon Glass | 9456490 | 2018-10-01 12:22:38 -0600 | [diff] [blame] | 447 | int cros_ec_read_hash(struct udevice *dev, uint hash_offset, |
| 448 | struct ec_response_vboot_hash *hash) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 449 | { |
| 450 | struct ec_params_vboot_hash p; |
| 451 | int rv; |
| 452 | |
| 453 | p.cmd = EC_VBOOT_HASH_GET; |
Simon Glass | 9456490 | 2018-10-01 12:22:38 -0600 | [diff] [blame] | 454 | p.offset = hash_offset; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 455 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
| 456 | hash, sizeof(*hash)) < 0) |
| 457 | return -1; |
| 458 | |
| 459 | /* If the EC is busy calculating the hash, fidget until it's done. */ |
| 460 | rv = cros_ec_wait_on_hash_done(dev, hash); |
| 461 | if (rv) |
| 462 | return rv; |
| 463 | |
| 464 | /* If the hash is valid, we're done. Otherwise, we have to kick it off |
| 465 | * again and wait for it to complete. Note that we explicitly assume |
| 466 | * that hashing zero bytes is always wrong, even though that would |
| 467 | * produce a valid hash value. */ |
| 468 | if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size) |
| 469 | return 0; |
| 470 | |
| 471 | debug("%s: No valid hash (status=%d size=%d). Compute one...\n", |
| 472 | __func__, hash->status, hash->size); |
| 473 | |
Simon Glass | ece2886 | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 474 | p.cmd = EC_VBOOT_HASH_START; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 475 | p.hash_type = EC_VBOOT_HASH_TYPE_SHA256; |
| 476 | p.nonce_size = 0; |
Simon Glass | 9456490 | 2018-10-01 12:22:38 -0600 | [diff] [blame] | 477 | p.offset = hash_offset; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 478 | |
| 479 | if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
| 480 | hash, sizeof(*hash)) < 0) |
| 481 | return -1; |
| 482 | |
| 483 | rv = cros_ec_wait_on_hash_done(dev, hash); |
| 484 | if (rv) |
| 485 | return rv; |
| 486 | |
| 487 | debug("%s: hash done\n", __func__); |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 492 | static int cros_ec_invalidate_hash(struct udevice *dev) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 493 | { |
| 494 | struct ec_params_vboot_hash p; |
| 495 | struct ec_response_vboot_hash *hash; |
| 496 | |
| 497 | /* We don't have an explict command for the EC to discard its current |
| 498 | * hash value, so we'll just tell it to calculate one that we know is |
| 499 | * wrong (we claim that hashing zero bytes is always invalid). |
| 500 | */ |
| 501 | p.cmd = EC_VBOOT_HASH_RECALC; |
| 502 | p.hash_type = EC_VBOOT_HASH_TYPE_SHA256; |
| 503 | p.nonce_size = 0; |
| 504 | p.offset = 0; |
| 505 | p.size = 0; |
| 506 | |
| 507 | debug("%s:\n", __func__); |
| 508 | |
| 509 | if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), |
| 510 | (uint8_t **)&hash, sizeof(*hash)) < 0) |
| 511 | return -1; |
| 512 | |
| 513 | /* No need to wait for it to finish */ |
| 514 | return 0; |
| 515 | } |
| 516 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 517 | int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 518 | { |
| 519 | struct ec_params_reboot_ec p; |
| 520 | |
| 521 | p.cmd = cmd; |
| 522 | p.flags = flags; |
| 523 | |
| 524 | if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0) |
| 525 | < 0) |
| 526 | return -1; |
| 527 | |
| 528 | if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) { |
| 529 | /* |
| 530 | * EC reboot will take place immediately so delay to allow it |
| 531 | * to complete. Note that some reboot types (EC_REBOOT_COLD) |
| 532 | * will reboot the AP as well, in which case we won't actually |
| 533 | * get to this point. |
| 534 | */ |
| 535 | /* |
| 536 | * TODO(rspangler@chromium.org): Would be nice if we had a |
| 537 | * better way to determine when the reboot is complete. Could |
| 538 | * we poll a memory-mapped LPC value? |
| 539 | */ |
| 540 | udelay(50000); |
| 541 | } |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
Simon Glass | fb72640 | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 546 | int cros_ec_interrupt_pending(struct udevice *dev) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 547 | { |
Simon Glass | fb72640 | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 548 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
| 549 | |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 550 | /* no interrupt support : always poll */ |
Simon Glass | fb72640 | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 551 | if (!dm_gpio_is_valid(&cdev->ec_int)) |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 552 | return -ENOENT; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 553 | |
Simon Glass | fb72640 | 2015-10-18 21:17:14 -0600 | [diff] [blame] | 554 | return dm_gpio_get_value(&cdev->ec_int); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 555 | } |
| 556 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 557 | int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 558 | { |
Simon Glass | ece2886 | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 559 | if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info, |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 560 | sizeof(*info)) != sizeof(*info)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 561 | return -1; |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 566 | int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 567 | { |
| 568 | struct ec_response_host_event_mask *resp; |
| 569 | |
| 570 | /* |
| 571 | * Use the B copy of the event flags, because the main copy is already |
| 572 | * used by ACPI/SMI. |
| 573 | */ |
| 574 | 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] | 575 | (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 576 | return -1; |
| 577 | |
| 578 | if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID)) |
| 579 | return -1; |
| 580 | |
| 581 | *events_ptr = resp->mask; |
| 582 | return 0; |
| 583 | } |
| 584 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 585 | int cros_ec_clear_host_events(struct udevice *dev, uint32_t events) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 586 | { |
| 587 | struct ec_params_host_event_mask params; |
| 588 | |
| 589 | params.mask = events; |
| 590 | |
| 591 | /* |
| 592 | * Use the B copy of the event flags, so it affects the data returned |
| 593 | * by cros_ec_get_host_events(). |
| 594 | */ |
| 595 | if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0, |
| 596 | ¶ms, sizeof(params), NULL, 0) < 0) |
| 597 | return -1; |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 602 | int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask, |
| 603 | uint32_t set_flags, |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 604 | struct ec_response_flash_protect *resp) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 605 | { |
| 606 | struct ec_params_flash_protect params; |
| 607 | |
| 608 | params.mask = set_mask; |
| 609 | params.flags = set_flags; |
| 610 | |
| 611 | if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT, |
| 612 | ¶ms, sizeof(params), |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 613 | resp, sizeof(*resp)) != sizeof(*resp)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 614 | return -1; |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 619 | static int cros_ec_check_version(struct udevice *dev) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 620 | { |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 621 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 622 | struct ec_params_hello req; |
| 623 | struct ec_response_hello *resp; |
| 624 | |
Simon Glass | 23fb156 | 2015-03-26 09:29:30 -0600 | [diff] [blame] | 625 | struct dm_cros_ec_ops *ops; |
| 626 | int ret; |
| 627 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 628 | ops = dm_cros_ec_get_ops(dev); |
Simon Glass | 23fb156 | 2015-03-26 09:29:30 -0600 | [diff] [blame] | 629 | if (ops->check_version) { |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 630 | ret = ops->check_version(dev); |
Simon Glass | 23fb156 | 2015-03-26 09:29:30 -0600 | [diff] [blame] | 631 | if (ret) |
| 632 | return ret; |
| 633 | } |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 634 | |
| 635 | /* |
| 636 | * TODO(sjg@chromium.org). |
| 637 | * There is a strange oddity here with the EC. We could just ignore |
| 638 | * the response, i.e. pass the last two parameters as NULL and 0. |
| 639 | * In this case we won't read back very many bytes from the EC. |
| 640 | * On the I2C bus the EC gets upset about this and will try to send |
| 641 | * the bytes anyway. This means that we will have to wait for that |
| 642 | * to complete before continuing with a new EC command. |
| 643 | * |
| 644 | * This problem is probably unique to the I2C bus. |
| 645 | * |
| 646 | * So for now, just read all the data anyway. |
| 647 | */ |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 648 | |
Randall Spangler | ce3ebb9 | 2014-02-27 13:26:10 -0700 | [diff] [blame] | 649 | /* Try sending a version 3 packet */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 650 | cdev->protocol_version = 3; |
Simon Glass | 73c1ecf | 2014-11-11 18:06:30 -0700 | [diff] [blame] | 651 | req.in_data = 0; |
Randall Spangler | ce3ebb9 | 2014-02-27 13:26:10 -0700 | [diff] [blame] | 652 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
| 653 | (uint8_t **)&resp, sizeof(*resp)) > 0) { |
| 654 | return 0; |
| 655 | } |
| 656 | |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 657 | /* Try sending a version 2 packet */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 658 | cdev->protocol_version = 2; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 659 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 660 | (uint8_t **)&resp, sizeof(*resp)) > 0) { |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 661 | return 0; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 662 | } |
| 663 | |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 664 | /* |
| 665 | * Fail if we're still here, since the EC doesn't understand any |
| 666 | * protcol version we speak. Version 1 interface without command |
| 667 | * version is no longer supported, and we don't know about any new |
| 668 | * protocol versions. |
| 669 | */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 670 | cdev->protocol_version = 0; |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 671 | printf("%s: ERROR: old EC interface not supported\n", __func__); |
| 672 | return -1; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 673 | } |
| 674 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 675 | int cros_ec_test(struct udevice *dev) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 676 | { |
| 677 | struct ec_params_hello req; |
| 678 | struct ec_response_hello *resp; |
| 679 | |
| 680 | req.in_data = 0x12345678; |
| 681 | if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), |
| 682 | (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) { |
| 683 | printf("ec_command_inptr() returned error\n"); |
| 684 | return -1; |
| 685 | } |
| 686 | if (resp->out_data != req.in_data + 0x01020304) { |
| 687 | printf("Received invalid handshake %x\n", resp->out_data); |
| 688 | return -1; |
| 689 | } |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 694 | int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region, |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 695 | uint32_t *offset, uint32_t *size) |
| 696 | { |
| 697 | struct ec_params_flash_region_info p; |
| 698 | struct ec_response_flash_region_info *r; |
| 699 | int ret; |
| 700 | |
| 701 | p.region = region; |
| 702 | ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO, |
| 703 | EC_VER_FLASH_REGION_INFO, |
| 704 | &p, sizeof(p), (uint8_t **)&r, sizeof(*r)); |
| 705 | if (ret != sizeof(*r)) |
| 706 | return -1; |
| 707 | |
| 708 | if (offset) |
| 709 | *offset = r->offset; |
| 710 | if (size) |
| 711 | *size = r->size; |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 716 | int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 717 | { |
| 718 | struct ec_params_flash_erase p; |
| 719 | |
| 720 | p.offset = offset; |
| 721 | p.size = size; |
| 722 | return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p), |
| 723 | NULL, 0); |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Write a single block to the flash |
| 728 | * |
| 729 | * Write a block of data to the EC flash. The size must not exceed the flash |
| 730 | * write block size which you can obtain from cros_ec_flash_write_burst_size(). |
| 731 | * |
| 732 | * The offset starts at 0. You can obtain the region information from |
| 733 | * cros_ec_flash_offset() to find out where to write for a particular region. |
| 734 | * |
| 735 | * Attempting to write to the region where the EC is currently running from |
| 736 | * will result in an error. |
| 737 | * |
| 738 | * @param dev CROS-EC device |
| 739 | * @param data Pointer to data buffer to write |
| 740 | * @param offset Offset within flash to write to. |
| 741 | * @param size Number of bytes to write |
| 742 | * @return 0 if ok, -1 on error |
| 743 | */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 744 | static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data, |
| 745 | uint32_t offset, uint32_t size) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 746 | { |
Moritz Fischer | e597bc7 | 2016-09-12 12:57:52 -0700 | [diff] [blame] | 747 | struct ec_params_flash_write *p; |
| 748 | int ret; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 749 | |
Moritz Fischer | e597bc7 | 2016-09-12 12:57:52 -0700 | [diff] [blame] | 750 | p = malloc(sizeof(*p) + size); |
| 751 | if (!p) |
| 752 | return -ENOMEM; |
| 753 | |
| 754 | p->offset = offset; |
| 755 | p->size = size; |
| 756 | assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE); |
| 757 | memcpy(p + 1, data, p->size); |
| 758 | |
| 759 | ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0, |
| 760 | p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1; |
| 761 | |
| 762 | free(p); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 763 | |
Moritz Fischer | e597bc7 | 2016-09-12 12:57:52 -0700 | [diff] [blame] | 764 | return ret; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Return optimal flash write burst size |
| 769 | */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 770 | static int cros_ec_flash_write_burst_size(struct udevice *dev) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 771 | { |
Simon Glass | ece2886 | 2014-02-27 13:26:07 -0700 | [diff] [blame] | 772 | return EC_FLASH_WRITE_VER0_SIZE; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /** |
| 776 | * Check if a block of data is erased (all 0xff) |
| 777 | * |
| 778 | * This function is useful when dealing with flash, for checking whether a |
| 779 | * data block is erased and thus does not need to be programmed. |
| 780 | * |
| 781 | * @param data Pointer to data to check (must be word-aligned) |
| 782 | * @param size Number of bytes to check (must be word-aligned) |
| 783 | * @return 0 if erased, non-zero if any word is not erased |
| 784 | */ |
| 785 | static int cros_ec_data_is_erased(const uint32_t *data, int size) |
| 786 | { |
| 787 | assert(!(size & 3)); |
| 788 | size /= sizeof(uint32_t); |
| 789 | for (; size > 0; size -= 4, data++) |
| 790 | if (*data != -1U) |
| 791 | return 0; |
| 792 | |
| 793 | return 1; |
| 794 | } |
| 795 | |
Moritz Fischer | de5bc89 | 2016-09-13 14:44:48 -0700 | [diff] [blame] | 796 | /** |
| 797 | * Read back flash parameters |
| 798 | * |
| 799 | * This function reads back parameters of the flash as reported by the EC |
| 800 | * |
| 801 | * @param dev Pointer to device |
| 802 | * @param info Pointer to output flash info struct |
| 803 | */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 804 | int cros_ec_read_flashinfo(struct udevice *dev, |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 805 | struct ec_response_flash_info *info) |
Moritz Fischer | de5bc89 | 2016-09-13 14:44:48 -0700 | [diff] [blame] | 806 | { |
| 807 | int ret; |
| 808 | |
| 809 | ret = ec_command(dev, EC_CMD_FLASH_INFO, 0, |
| 810 | NULL, 0, info, sizeof(*info)); |
| 811 | if (ret < 0) |
| 812 | return ret; |
| 813 | |
| 814 | return ret < sizeof(*info) ? -1 : 0; |
| 815 | } |
| 816 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 817 | int cros_ec_flash_write(struct udevice *dev, const uint8_t *data, |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 818 | uint32_t offset, uint32_t size) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 819 | { |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 820 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 821 | uint32_t burst = cros_ec_flash_write_burst_size(dev); |
| 822 | uint32_t end, off; |
| 823 | int ret; |
| 824 | |
| 825 | /* |
| 826 | * TODO: round up to the nearest multiple of write size. Can get away |
| 827 | * without that on link right now because its write size is 4 bytes. |
| 828 | */ |
| 829 | end = offset + size; |
| 830 | for (off = offset; off < end; off += burst, data += burst) { |
| 831 | uint32_t todo; |
| 832 | |
| 833 | /* If the data is empty, there is no point in programming it */ |
| 834 | todo = min(end - off, burst); |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 835 | if (cdev->optimise_flash_write && |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 836 | cros_ec_data_is_erased((uint32_t *)data, todo)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 837 | continue; |
| 838 | |
| 839 | ret = cros_ec_flash_write_block(dev, data, off, todo); |
| 840 | if (ret) |
| 841 | return ret; |
| 842 | } |
| 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * Read a single block from the flash |
| 849 | * |
| 850 | * Read a block of data from the EC flash. The size must not exceed the flash |
| 851 | * write block size which you can obtain from cros_ec_flash_write_burst_size(). |
| 852 | * |
| 853 | * The offset starts at 0. You can obtain the region information from |
| 854 | * cros_ec_flash_offset() to find out where to read for a particular region. |
| 855 | * |
| 856 | * @param dev CROS-EC device |
| 857 | * @param data Pointer to data buffer to read into |
| 858 | * @param offset Offset within flash to read from |
| 859 | * @param size Number of bytes to read |
| 860 | * @return 0 if ok, -1 on error |
| 861 | */ |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 862 | static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data, |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 863 | uint32_t offset, uint32_t size) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 864 | { |
| 865 | struct ec_params_flash_read p; |
| 866 | |
| 867 | p.offset = offset; |
| 868 | p.size = size; |
| 869 | |
| 870 | return ec_command(dev, EC_CMD_FLASH_READ, 0, |
| 871 | &p, sizeof(p), data, size) >= 0 ? 0 : -1; |
| 872 | } |
| 873 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 874 | int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset, |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 875 | uint32_t size) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 876 | { |
| 877 | uint32_t burst = cros_ec_flash_write_burst_size(dev); |
| 878 | uint32_t end, off; |
| 879 | int ret; |
| 880 | |
| 881 | end = offset + size; |
| 882 | for (off = offset; off < end; off += burst, data += burst) { |
| 883 | ret = cros_ec_flash_read_block(dev, data, off, |
| 884 | min(end - off, burst)); |
| 885 | if (ret) |
| 886 | return ret; |
| 887 | } |
| 888 | |
| 889 | return 0; |
| 890 | } |
| 891 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 892 | int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image, |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 893 | int image_size) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 894 | { |
| 895 | uint32_t rw_offset, rw_size; |
| 896 | int ret; |
| 897 | |
Simon Glass | 2c1e550 | 2018-10-01 12:22:36 -0600 | [diff] [blame] | 898 | if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset, |
| 899 | &rw_size)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 900 | return -1; |
Simon Glass | 19cc295 | 2014-02-27 13:26:11 -0700 | [diff] [blame] | 901 | if (image_size > (int)rw_size) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 902 | return -1; |
| 903 | |
| 904 | /* Invalidate the existing hash, just in case the AP reboots |
| 905 | * unexpectedly during the update. If that happened, the EC RW firmware |
| 906 | * would be invalid, but the EC would still have the original hash. |
| 907 | */ |
| 908 | ret = cros_ec_invalidate_hash(dev); |
| 909 | if (ret) |
| 910 | return ret; |
| 911 | |
| 912 | /* |
| 913 | * Erase the entire RW section, so that the EC doesn't see any garbage |
| 914 | * past the new image if it's smaller than the current image. |
| 915 | * |
| 916 | * TODO: could optimize this to erase just the current image, since |
| 917 | * presumably everything past that is 0xff's. But would still need to |
| 918 | * round up to the nearest multiple of erase size. |
| 919 | */ |
| 920 | ret = cros_ec_flash_erase(dev, rw_offset, rw_size); |
| 921 | if (ret) |
| 922 | return ret; |
| 923 | |
| 924 | /* Write the image */ |
| 925 | ret = cros_ec_flash_write(dev, image, rw_offset, image_size); |
| 926 | if (ret) |
| 927 | return ret; |
| 928 | |
| 929 | return 0; |
| 930 | } |
| 931 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 932 | int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 933 | { |
| 934 | struct ec_params_vbnvcontext p; |
| 935 | int len; |
| 936 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 937 | if (size != EC_VBNV_BLOCK_SIZE) |
| 938 | return -EINVAL; |
| 939 | |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 940 | p.op = EC_VBNV_CONTEXT_OP_READ; |
| 941 | |
| 942 | len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT, |
| 943 | &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE); |
| 944 | if (len < EC_VBNV_BLOCK_SIZE) |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 945 | return -EIO; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 946 | |
| 947 | return 0; |
| 948 | } |
| 949 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 950 | int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 951 | { |
| 952 | struct ec_params_vbnvcontext p; |
| 953 | int len; |
| 954 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 955 | if (size != EC_VBNV_BLOCK_SIZE) |
| 956 | return -EINVAL; |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 957 | p.op = EC_VBNV_CONTEXT_OP_WRITE; |
| 958 | memcpy(p.block, block, sizeof(p.block)); |
| 959 | |
| 960 | len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT, |
| 961 | &p, sizeof(p), NULL, 0); |
| 962 | if (len < 0) |
| 963 | return -1; |
| 964 | |
| 965 | return 0; |
| 966 | } |
| 967 | |
Simon Glass | eb2cc51 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 968 | 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] | 969 | { |
| 970 | struct ec_params_ldo_set params; |
| 971 | |
| 972 | params.index = index; |
| 973 | params.state = state; |
| 974 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 975 | if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params), |
Simon Glass | eb2cc51 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 976 | NULL, 0)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 977 | return -1; |
| 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
Simon Glass | eb2cc51 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 982 | 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] | 983 | { |
| 984 | struct ec_params_ldo_get params; |
| 985 | struct ec_response_ldo_get *resp; |
| 986 | |
| 987 | params.index = index; |
| 988 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 989 | if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params), |
Simon Glass | eb2cc51 | 2015-08-03 08:19:24 -0600 | [diff] [blame] | 990 | (uint8_t **)&resp, sizeof(*resp)) != |
| 991 | sizeof(*resp)) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 992 | return -1; |
| 993 | |
| 994 | *state = resp->state; |
| 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 999 | int cros_ec_register(struct udevice *dev) |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1000 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 1001 | struct cros_ec_dev *cdev = dev_get_uclass_priv(dev); |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1002 | char id[MSG_BYTES]; |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1003 | |
| 1004 | cdev->dev = dev; |
Simon Glass | 0d7cc47 | 2015-01-05 20:05:32 -0700 | [diff] [blame] | 1005 | gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int, |
| 1006 | GPIOD_IS_IN); |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 1007 | cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write"); |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1008 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 1009 | if (cros_ec_check_version(dev)) { |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1010 | debug("%s: Could not detect CROS-EC version\n", __func__); |
| 1011 | return -CROS_EC_ERR_CHECK_VERSION; |
| 1012 | } |
| 1013 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 1014 | if (cros_ec_read_id(dev, id, sizeof(id))) { |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1015 | debug("%s: Could not read KBC ID\n", __func__); |
| 1016 | return -CROS_EC_ERR_READ_ID; |
| 1017 | } |
| 1018 | |
| 1019 | /* Remember this device for use by the cros_ec command */ |
Simon Glass | 46e1733 | 2015-02-17 15:29:36 -0700 | [diff] [blame] | 1020 | debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n", |
| 1021 | cdev->protocol_version, id); |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1022 | |
| 1023 | return 0; |
| 1024 | } |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 1025 | |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 1026 | int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config) |
Simon Glass | 3c59abc | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 1027 | { |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 1028 | ofnode flash_node, node; |
Simon Glass | 3c59abc | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 1029 | |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 1030 | flash_node = dev_read_subnode(dev, "flash"); |
| 1031 | if (!ofnode_valid(flash_node)) { |
Simon Glass | 3c59abc | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 1032 | debug("Failed to find flash node\n"); |
| 1033 | return -1; |
| 1034 | } |
| 1035 | |
Simon Glass | 4b58093 | 2018-06-11 13:07:17 -0600 | [diff] [blame] | 1036 | if (ofnode_read_fmap_entry(flash_node, &config->flash)) { |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 1037 | debug("Failed to decode flash node in chrome-ec\n"); |
Simon Glass | 3c59abc | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 1038 | return -1; |
| 1039 | } |
| 1040 | |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 1041 | config->flash_erase_value = ofnode_read_s32_default(flash_node, |
| 1042 | "erase-value", -1); |
Simon Glass | 2852976 | 2017-08-05 15:45:54 -0600 | [diff] [blame] | 1043 | ofnode_for_each_subnode(node, flash_node) { |
Simon Glass | b35e6d6 | 2017-05-18 20:09:23 -0600 | [diff] [blame] | 1044 | const char *name = ofnode_get_name(node); |
Simon Glass | 3c59abc | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 1045 | enum ec_flash_region region; |
| 1046 | |
| 1047 | if (0 == strcmp(name, "ro")) { |
| 1048 | region = EC_FLASH_REGION_RO; |
| 1049 | } else if (0 == strcmp(name, "rw")) { |
Simon Glass | 2c1e550 | 2018-10-01 12:22:36 -0600 | [diff] [blame] | 1050 | region = EC_FLASH_REGION_ACTIVE; |
Simon Glass | 3c59abc | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 1051 | } else if (0 == strcmp(name, "wp-ro")) { |
| 1052 | region = EC_FLASH_REGION_WP_RO; |
| 1053 | } else { |
| 1054 | debug("Unknown EC flash region name '%s'\n", name); |
| 1055 | return -1; |
| 1056 | } |
| 1057 | |
Simon Glass | 4b58093 | 2018-06-11 13:07:17 -0600 | [diff] [blame] | 1058 | if (ofnode_read_fmap_entry(node, &config->region[region])) { |
Simon Glass | 3c59abc | 2014-02-27 13:26:03 -0700 | [diff] [blame] | 1059 | debug("Failed to decode flash region in chrome-ec'\n"); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
Moritz Fischer | 6cddc60 | 2016-09-27 15:42:07 -0700 | [diff] [blame] | 1067 | int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in, |
| 1068 | int nmsgs) |
Simon Glass | 9ad07af | 2015-08-03 08:19:23 -0600 | [diff] [blame] | 1069 | { |
Simon Glass | 9ad07af | 2015-08-03 08:19:23 -0600 | [diff] [blame] | 1070 | union { |
| 1071 | struct ec_params_i2c_passthru p; |
| 1072 | uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE]; |
| 1073 | } params; |
| 1074 | union { |
| 1075 | struct ec_response_i2c_passthru r; |
| 1076 | uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE]; |
| 1077 | } response; |
| 1078 | struct ec_params_i2c_passthru *p = ¶ms.p; |
| 1079 | struct ec_response_i2c_passthru *r = &response.r; |
| 1080 | struct ec_params_i2c_passthru_msg *msg; |
| 1081 | uint8_t *pdata, *read_ptr = NULL; |
| 1082 | int read_len; |
| 1083 | int size; |
| 1084 | int rv; |
| 1085 | int i; |
| 1086 | |
Moritz Fischer | 6cddc60 | 2016-09-27 15:42:07 -0700 | [diff] [blame] | 1087 | p->port = port; |
Simon Glass | 9ad07af | 2015-08-03 08:19:23 -0600 | [diff] [blame] | 1088 | |
| 1089 | p->num_msgs = nmsgs; |
| 1090 | size = sizeof(*p) + p->num_msgs * sizeof(*msg); |
| 1091 | |
| 1092 | /* Create a message to write the register address and optional data */ |
| 1093 | pdata = (uint8_t *)p + size; |
| 1094 | |
| 1095 | read_len = 0; |
| 1096 | for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) { |
| 1097 | bool is_read = in->flags & I2C_M_RD; |
| 1098 | |
| 1099 | msg->addr_flags = in->addr; |
| 1100 | msg->len = in->len; |
| 1101 | if (is_read) { |
| 1102 | msg->addr_flags |= EC_I2C_FLAG_READ; |
| 1103 | read_len += in->len; |
| 1104 | read_ptr = in->buf; |
| 1105 | if (sizeof(*r) + read_len > sizeof(response)) { |
| 1106 | puts("Read length too big for buffer\n"); |
| 1107 | return -1; |
| 1108 | } |
| 1109 | } else { |
| 1110 | if (pdata - (uint8_t *)p + in->len > sizeof(params)) { |
| 1111 | puts("Params too large for buffer\n"); |
| 1112 | return -1; |
| 1113 | } |
| 1114 | memcpy(pdata, in->buf, in->len); |
| 1115 | pdata += in->len; |
| 1116 | } |
| 1117 | } |
| 1118 | |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 1119 | rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p, |
Simon Glass | 9ad07af | 2015-08-03 08:19:23 -0600 | [diff] [blame] | 1120 | r, sizeof(*r) + read_len); |
| 1121 | if (rv < 0) |
| 1122 | return rv; |
| 1123 | |
| 1124 | /* Parse response */ |
| 1125 | if (r->i2c_status & EC_I2C_STATUS_ERROR) { |
| 1126 | printf("Transfer failed with status=0x%x\n", r->i2c_status); |
| 1127 | return -1; |
| 1128 | } |
| 1129 | |
| 1130 | if (rv < sizeof(*r) + read_len) { |
| 1131 | puts("Truncated read response\n"); |
| 1132 | return -1; |
| 1133 | } |
| 1134 | |
| 1135 | /* We only support a single read message for each transfer */ |
| 1136 | if (read_len) |
| 1137 | memcpy(read_ptr, r->data, read_len); |
| 1138 | |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1142 | UCLASS_DRIVER(cros_ec) = { |
| 1143 | .id = UCLASS_CROS_EC, |
| 1144 | .name = "cros_ec", |
| 1145 | .per_device_auto_alloc_size = sizeof(struct cros_ec_dev), |
Simon Glass | 1823034 | 2016-07-05 17:10:10 -0600 | [diff] [blame] | 1146 | .post_bind = dm_scan_fdt_dev, |
Simon Glass | 44569b5 | 2014-10-13 23:42:14 -0600 | [diff] [blame] | 1147 | }; |