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