Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Chromium OS cros_ec driver - I2C interface |
| 4 | * |
| 5 | * Copyright (c) 2012 The Chromium OS Authors. |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * The Matrix Keyboard Protocol driver handles talking to the keyboard |
| 10 | * controller chip. Mostly this is for keyboard functions, but some other |
| 11 | * things have slipped in, so we provide generic services to talk to the |
| 12 | * KBC. |
| 13 | */ |
| 14 | |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 15 | #include <dm.h> |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 16 | #include <i2c.h> |
| 17 | #include <cros_ec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 18 | #include <log.h> |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 19 | |
| 20 | #ifdef DEBUG_TRACE |
| 21 | #define debug_trace(fmt, b...) debug(fmt, #b) |
| 22 | #else |
| 23 | #define debug_trace(fmt, b...) |
| 24 | #endif |
| 25 | |
Moritz Fischer | 874d6b4 | 2017-01-20 12:35:03 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Request format for protocol v3 |
| 28 | * byte 0 0xda (EC_COMMAND_PROTOCOL_3) |
| 29 | * byte 1-8 struct ec_host_request |
| 30 | * byte 10- response data |
| 31 | */ |
| 32 | struct ec_host_request_i2c { |
| 33 | /* Always 0xda to backward compatible with v2 struct */ |
| 34 | uint8_t command_protocol; |
| 35 | struct ec_host_request ec_request; |
| 36 | } __packed; |
| 37 | |
| 38 | /* |
| 39 | * Response format for protocol v3 |
| 40 | * byte 0 result code |
| 41 | * byte 1 packet_length |
| 42 | * byte 2-9 struct ec_host_response |
| 43 | * byte 10- response data |
| 44 | */ |
| 45 | struct ec_host_response_i2c { |
| 46 | uint8_t result; |
| 47 | uint8_t packet_length; |
| 48 | struct ec_host_response ec_response; |
| 49 | } __packed; |
| 50 | |
| 51 | static int cros_ec_i2c_packet(struct udevice *udev, int out_bytes, int in_bytes) |
| 52 | { |
| 53 | struct cros_ec_dev *dev = dev_get_uclass_priv(udev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 54 | struct dm_i2c_chip *chip = dev_get_parent_plat(udev); |
Moritz Fischer | 874d6b4 | 2017-01-20 12:35:03 -0800 | [diff] [blame] | 55 | struct ec_host_request_i2c *ec_request_i2c = |
| 56 | (struct ec_host_request_i2c *)dev->dout; |
| 57 | struct ec_host_response_i2c *ec_response_i2c = |
| 58 | (struct ec_host_response_i2c *)dev->din; |
| 59 | struct i2c_msg i2c_msg[2]; |
| 60 | int ret; |
| 61 | |
| 62 | i2c_msg[0].addr = chip->chip_addr; |
| 63 | i2c_msg[0].flags = 0; |
| 64 | i2c_msg[1].addr = chip->chip_addr; |
| 65 | i2c_msg[1].flags = I2C_M_RD; |
| 66 | |
| 67 | /* one extra byte, to indicate v3 */ |
| 68 | i2c_msg[0].len = out_bytes + 1; |
| 69 | i2c_msg[0].buf = dev->dout; |
| 70 | |
| 71 | /* stitch on EC_COMMAND_PROTOCOL_3 */ |
| 72 | memmove(&ec_request_i2c->ec_request, dev->dout, out_bytes); |
| 73 | ec_request_i2c->command_protocol = EC_COMMAND_PROTOCOL_3; |
| 74 | |
| 75 | /* two extra bytes for v3 */ |
| 76 | i2c_msg[1].len = in_bytes + 2; |
| 77 | i2c_msg[1].buf = dev->din; |
| 78 | |
| 79 | ret = dm_i2c_xfer(udev, &i2c_msg[0], 2); |
| 80 | if (ret) { |
| 81 | printf("%s: Could not execute transfer: %d\n", __func__, ret); |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | /* When we send a v3 request to v2 ec, ec won't recognize the 0xda |
| 86 | * (EC_COMMAND_PROTOCOL_3) and will return with status |
| 87 | * EC_RES_INVALID_COMMAND with zero data length |
| 88 | * |
| 89 | * In case of invalid command for v3 protocol the data length |
| 90 | * will be at least sizeof(struct ec_host_response) |
| 91 | */ |
| 92 | if (ec_response_i2c->result == EC_RES_INVALID_COMMAND && |
| 93 | ec_response_i2c->packet_length == 0) |
| 94 | return -EPROTONOSUPPORT; |
| 95 | |
| 96 | if (ec_response_i2c->packet_length < sizeof(struct ec_host_response)) { |
| 97 | printf("%s: response of %u bytes too short; not a full hdr\n", |
| 98 | __func__, ec_response_i2c->packet_length); |
| 99 | return -EBADMSG; |
| 100 | } |
| 101 | |
Moritz Fischer | 874d6b4 | 2017-01-20 12:35:03 -0800 | [diff] [blame] | 102 | /* drop result and packet_len */ |
| 103 | memmove(dev->din, &ec_response_i2c->ec_response, in_bytes); |
| 104 | |
| 105 | return in_bytes; |
| 106 | } |
| 107 | |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 108 | static int cros_ec_i2c_command(struct udevice *udev, uint8_t cmd, |
| 109 | int cmd_version, const uint8_t *dout, |
| 110 | int dout_len, uint8_t **dinp, int din_len) |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 111 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 112 | struct cros_ec_dev *dev = dev_get_uclass_priv(udev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 113 | struct dm_i2c_chip *chip = dev_get_parent_plat(udev); |
Moritz Fischer | 9b606d6 | 2017-01-12 09:47:30 -0800 | [diff] [blame] | 114 | struct i2c_msg i2c_msg[2]; |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 115 | /* version8, cmd8, arglen8, out8[dout_len], csum8 */ |
| 116 | int out_bytes = dout_len + 4; |
| 117 | /* response8, arglen8, in8[din_len], checksum8 */ |
| 118 | int in_bytes = din_len + 3; |
| 119 | uint8_t *ptr; |
| 120 | /* Receive input data, so that args will be dword aligned */ |
| 121 | uint8_t *in_ptr; |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 122 | int len, csum, ret; |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 123 | |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 124 | /* |
| 125 | * Sanity-check I/O sizes given transaction overhead in internal |
| 126 | * buffers. |
| 127 | */ |
| 128 | if (out_bytes > sizeof(dev->dout)) { |
| 129 | debug("%s: Cannot send %d bytes\n", __func__, dout_len); |
| 130 | return -1; |
| 131 | } |
| 132 | if (in_bytes > sizeof(dev->din)) { |
| 133 | debug("%s: Cannot receive %d bytes\n", __func__, din_len); |
| 134 | return -1; |
| 135 | } |
| 136 | assert(dout_len >= 0); |
| 137 | assert(dinp); |
| 138 | |
Moritz Fischer | 9b606d6 | 2017-01-12 09:47:30 -0800 | [diff] [blame] | 139 | i2c_msg[0].addr = chip->chip_addr; |
| 140 | i2c_msg[0].len = out_bytes; |
| 141 | i2c_msg[0].buf = dev->dout; |
| 142 | i2c_msg[0].flags = 0; |
| 143 | |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 144 | /* |
| 145 | * Copy command and data into output buffer so we can do a single I2C |
| 146 | * burst transaction. |
| 147 | */ |
| 148 | ptr = dev->dout; |
| 149 | |
| 150 | /* |
| 151 | * in_ptr starts of pointing to a dword-aligned input data buffer. |
| 152 | * We decrement it back by the number of header bytes we expect to |
| 153 | * receive, so that the first parameter of the resulting input data |
| 154 | * will be dword aligned. |
| 155 | */ |
| 156 | in_ptr = dev->din + sizeof(int64_t); |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 157 | |
| 158 | if (dev->protocol_version != 2) { |
| 159 | /* Something we don't support */ |
| 160 | debug("%s: Protocol version %d unsupported\n", |
| 161 | __func__, dev->protocol_version); |
| 162 | return -1; |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 163 | } |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 164 | |
| 165 | *ptr++ = EC_CMD_VERSION0 + cmd_version; |
| 166 | *ptr++ = cmd; |
| 167 | *ptr++ = dout_len; |
| 168 | in_ptr -= 2; /* Expect status, length bytes */ |
| 169 | |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 170 | memcpy(ptr, dout, dout_len); |
| 171 | ptr += dout_len; |
| 172 | |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 173 | *ptr++ = (uint8_t) |
| 174 | cros_ec_calc_checksum(dev->dout, dout_len + 3); |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 175 | |
Moritz Fischer | 9b606d6 | 2017-01-12 09:47:30 -0800 | [diff] [blame] | 176 | i2c_msg[1].addr = chip->chip_addr; |
| 177 | i2c_msg[1].len = in_bytes; |
| 178 | i2c_msg[1].buf = in_ptr; |
| 179 | i2c_msg[1].flags = I2C_M_RD; |
| 180 | |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 181 | /* Send output data */ |
| 182 | cros_ec_dump_data("out", -1, dev->dout, out_bytes); |
Moritz Fischer | 9b606d6 | 2017-01-12 09:47:30 -0800 | [diff] [blame] | 183 | |
| 184 | ret = dm_i2c_xfer(udev, &i2c_msg[0], 2); |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 185 | if (ret) { |
Moritz Fischer | 9b606d6 | 2017-01-12 09:47:30 -0800 | [diff] [blame] | 186 | debug("%s: Could not execute transfer to %s\n", __func__, |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 187 | udev->name); |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 188 | ret = -1; |
| 189 | } |
| 190 | |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 191 | if (*in_ptr != EC_RES_SUCCESS) { |
| 192 | debug("%s: Received bad result code %d\n", __func__, *in_ptr); |
| 193 | return -(int)*in_ptr; |
| 194 | } |
| 195 | |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 196 | len = in_ptr[1]; |
| 197 | if (len + 3 > sizeof(dev->din)) { |
| 198 | debug("%s: Received length %#02x too large\n", |
| 199 | __func__, len); |
| 200 | return -1; |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 201 | } |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 202 | csum = cros_ec_calc_checksum(in_ptr, 2 + len); |
| 203 | if (csum != in_ptr[2 + len]) { |
| 204 | debug("%s: Invalid checksum rx %#02x, calced %#02x\n", |
| 205 | __func__, in_ptr[2 + din_len], csum); |
| 206 | return -1; |
| 207 | } |
| 208 | din_len = min(din_len, len); |
| 209 | cros_ec_dump_data("in", -1, in_ptr, din_len + 3); |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 210 | |
| 211 | /* Return pointer to dword-aligned input data, if any */ |
| 212 | *dinp = dev->din + sizeof(int64_t); |
| 213 | |
| 214 | return din_len; |
| 215 | } |
| 216 | |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 217 | static int cros_ec_probe(struct udevice *dev) |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 218 | { |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 219 | return cros_ec_register(dev); |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 220 | } |
| 221 | |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 222 | static struct dm_cros_ec_ops cros_ec_ops = { |
| 223 | .command = cros_ec_i2c_command, |
Moritz Fischer | 874d6b4 | 2017-01-20 12:35:03 -0800 | [diff] [blame] | 224 | .packet = cros_ec_i2c_packet, |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 225 | }; |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 226 | |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 227 | static const struct udevice_id cros_ec_ids[] = { |
Simon Glass | 73f220c | 2015-03-26 09:29:39 -0600 | [diff] [blame] | 228 | { .compatible = "google,cros-ec-i2c" }, |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 229 | { } |
| 230 | }; |
| 231 | |
Simon Glass | df2e939 | 2020-10-05 05:27:00 -0600 | [diff] [blame] | 232 | U_BOOT_DRIVER(google_cros_ec_i2c) = { |
| 233 | .name = "google_cros_ec_i2c", |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 234 | .id = UCLASS_CROS_EC, |
| 235 | .of_match = cros_ec_ids, |
| 236 | .probe = cros_ec_probe, |
| 237 | .ops = &cros_ec_ops, |
| 238 | }; |