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