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