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