Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Chromium OS cros_ec driver - I2C interface |
| 3 | * |
| 4 | * Copyright (c) 2012 The Chromium OS Authors. |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * The Matrix Keyboard Protocol driver handles talking to the keyboard |
| 11 | * controller chip. Mostly this is for keyboard functions, but some other |
| 12 | * things have slipped in, so we provide generic services to talk to the |
| 13 | * KBC. |
| 14 | */ |
| 15 | |
| 16 | #include <common.h> |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 17 | #include <dm.h> |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 18 | #include <i2c.h> |
| 19 | #include <cros_ec.h> |
| 20 | |
| 21 | #ifdef DEBUG_TRACE |
| 22 | #define debug_trace(fmt, b...) debug(fmt, #b) |
| 23 | #else |
| 24 | #define debug_trace(fmt, b...) |
| 25 | #endif |
| 26 | |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 27 | static int cros_ec_i2c_command(struct udevice *udev, uint8_t cmd, |
| 28 | int cmd_version, const uint8_t *dout, |
| 29 | int dout_len, uint8_t **dinp, int din_len) |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 30 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 31 | struct cros_ec_dev *dev = dev_get_uclass_priv(udev); |
Moritz Fischer | 9b606d6 | 2017-01-12 09:47:30 -0800 | [diff] [blame] | 32 | struct dm_i2c_chip *chip = dev_get_parent_platdata(udev); |
| 33 | struct i2c_msg i2c_msg[2]; |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 34 | /* version8, cmd8, arglen8, out8[dout_len], csum8 */ |
| 35 | int out_bytes = dout_len + 4; |
| 36 | /* response8, arglen8, in8[din_len], checksum8 */ |
| 37 | int in_bytes = din_len + 3; |
| 38 | uint8_t *ptr; |
| 39 | /* Receive input data, so that args will be dword aligned */ |
| 40 | uint8_t *in_ptr; |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 41 | int len, csum, ret; |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 42 | |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 43 | /* |
| 44 | * Sanity-check I/O sizes given transaction overhead in internal |
| 45 | * buffers. |
| 46 | */ |
| 47 | if (out_bytes > sizeof(dev->dout)) { |
| 48 | debug("%s: Cannot send %d bytes\n", __func__, dout_len); |
| 49 | return -1; |
| 50 | } |
| 51 | if (in_bytes > sizeof(dev->din)) { |
| 52 | debug("%s: Cannot receive %d bytes\n", __func__, din_len); |
| 53 | return -1; |
| 54 | } |
| 55 | assert(dout_len >= 0); |
| 56 | assert(dinp); |
| 57 | |
Moritz Fischer | 9b606d6 | 2017-01-12 09:47:30 -0800 | [diff] [blame] | 58 | i2c_msg[0].addr = chip->chip_addr; |
| 59 | i2c_msg[0].len = out_bytes; |
| 60 | i2c_msg[0].buf = dev->dout; |
| 61 | i2c_msg[0].flags = 0; |
| 62 | |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 63 | /* |
| 64 | * Copy command and data into output buffer so we can do a single I2C |
| 65 | * burst transaction. |
| 66 | */ |
| 67 | ptr = dev->dout; |
| 68 | |
| 69 | /* |
| 70 | * in_ptr starts of pointing to a dword-aligned input data buffer. |
| 71 | * We decrement it back by the number of header bytes we expect to |
| 72 | * receive, so that the first parameter of the resulting input data |
| 73 | * will be dword aligned. |
| 74 | */ |
| 75 | in_ptr = dev->din + sizeof(int64_t); |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 76 | |
| 77 | if (dev->protocol_version != 2) { |
| 78 | /* Something we don't support */ |
| 79 | debug("%s: Protocol version %d unsupported\n", |
| 80 | __func__, dev->protocol_version); |
| 81 | return -1; |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 82 | } |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 83 | |
| 84 | *ptr++ = EC_CMD_VERSION0 + cmd_version; |
| 85 | *ptr++ = cmd; |
| 86 | *ptr++ = dout_len; |
| 87 | in_ptr -= 2; /* Expect status, length bytes */ |
| 88 | |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 89 | memcpy(ptr, dout, dout_len); |
| 90 | ptr += dout_len; |
| 91 | |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 92 | *ptr++ = (uint8_t) |
| 93 | cros_ec_calc_checksum(dev->dout, dout_len + 3); |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 94 | |
Moritz Fischer | 9b606d6 | 2017-01-12 09:47:30 -0800 | [diff] [blame] | 95 | i2c_msg[1].addr = chip->chip_addr; |
| 96 | i2c_msg[1].len = in_bytes; |
| 97 | i2c_msg[1].buf = in_ptr; |
| 98 | i2c_msg[1].flags = I2C_M_RD; |
| 99 | |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 100 | /* Send output data */ |
| 101 | cros_ec_dump_data("out", -1, dev->dout, out_bytes); |
Moritz Fischer | 9b606d6 | 2017-01-12 09:47:30 -0800 | [diff] [blame] | 102 | |
| 103 | ret = dm_i2c_xfer(udev, &i2c_msg[0], 2); |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 104 | if (ret) { |
Moritz Fischer | 9b606d6 | 2017-01-12 09:47:30 -0800 | [diff] [blame] | 105 | debug("%s: Could not execute transfer to %s\n", __func__, |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 106 | udev->name); |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 107 | ret = -1; |
| 108 | } |
| 109 | |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 110 | if (*in_ptr != EC_RES_SUCCESS) { |
| 111 | debug("%s: Received bad result code %d\n", __func__, *in_ptr); |
| 112 | return -(int)*in_ptr; |
| 113 | } |
| 114 | |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 115 | len = in_ptr[1]; |
| 116 | if (len + 3 > sizeof(dev->din)) { |
| 117 | debug("%s: Received length %#02x too large\n", |
| 118 | __func__, len); |
| 119 | return -1; |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 120 | } |
Randall Spangler | f03e970 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 121 | csum = cros_ec_calc_checksum(in_ptr, 2 + len); |
| 122 | if (csum != in_ptr[2 + len]) { |
| 123 | debug("%s: Invalid checksum rx %#02x, calced %#02x\n", |
| 124 | __func__, in_ptr[2 + din_len], csum); |
| 125 | return -1; |
| 126 | } |
| 127 | din_len = min(din_len, len); |
| 128 | cros_ec_dump_data("in", -1, in_ptr, din_len + 3); |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 129 | |
| 130 | /* Return pointer to dword-aligned input data, if any */ |
| 131 | *dinp = dev->din + sizeof(int64_t); |
| 132 | |
| 133 | return din_len; |
| 134 | } |
| 135 | |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 136 | static int cros_ec_probe(struct udevice *dev) |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 137 | { |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 138 | return cros_ec_register(dev); |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 139 | } |
| 140 | |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 141 | static struct dm_cros_ec_ops cros_ec_ops = { |
| 142 | .command = cros_ec_i2c_command, |
| 143 | }; |
Hung-ying Tyan | 6830408 | 2013-05-15 18:27:29 +0800 | [diff] [blame] | 144 | |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 145 | static const struct udevice_id cros_ec_ids[] = { |
Simon Glass | 73f220c | 2015-03-26 09:29:39 -0600 | [diff] [blame] | 146 | { .compatible = "google,cros-ec-i2c" }, |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 147 | { } |
| 148 | }; |
| 149 | |
| 150 | U_BOOT_DRIVER(cros_ec_i2c) = { |
Simon Glass | 73f220c | 2015-03-26 09:29:39 -0600 | [diff] [blame] | 151 | .name = "cros_ec_i2c", |
Simon Glass | a1f2603 | 2015-01-26 20:29:40 -0700 | [diff] [blame] | 152 | .id = UCLASS_CROS_EC, |
| 153 | .of_match = cros_ec_ids, |
| 154 | .probe = cros_ec_probe, |
| 155 | .ops = &cros_ec_ops, |
| 156 | }; |