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