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