1 /* 2 * Chromium OS cros_ec driver - I2C interface 3 * 4 * Copyright (c) 2012 The Chromium OS Authors. 5 * 6 * SPDX-License-Identifier: GPL-2.0+ 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> 17 #include <dm.h> 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 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) 30 { 31 struct cros_ec_dev *dev = dev_get_uclass_priv(udev); 32 struct dm_i2c_chip *chip = dev_get_parent_platdata(udev); 33 struct i2c_msg i2c_msg[2]; 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; 41 int len, csum, ret; 42 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 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 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); 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; 82 } 83 84 *ptr++ = EC_CMD_VERSION0 + cmd_version; 85 *ptr++ = cmd; 86 *ptr++ = dout_len; 87 in_ptr -= 2; /* Expect status, length bytes */ 88 89 memcpy(ptr, dout, dout_len); 90 ptr += dout_len; 91 92 *ptr++ = (uint8_t) 93 cros_ec_calc_checksum(dev->dout, dout_len + 3); 94 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 100 /* Send output data */ 101 cros_ec_dump_data("out", -1, dev->dout, out_bytes); 102 103 ret = dm_i2c_xfer(udev, &i2c_msg[0], 2); 104 if (ret) { 105 debug("%s: Could not execute transfer to %s\n", __func__, 106 udev->name); 107 ret = -1; 108 } 109 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 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; 120 } 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); 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 136 static int cros_ec_probe(struct udevice *dev) 137 { 138 return cros_ec_register(dev); 139 } 140 141 static struct dm_cros_ec_ops cros_ec_ops = { 142 .command = cros_ec_i2c_command, 143 }; 144 145 static const struct udevice_id cros_ec_ids[] = { 146 { .compatible = "google,cros-ec-i2c" }, 147 { } 148 }; 149 150 U_BOOT_DRIVER(cros_ec_i2c) = { 151 .name = "cros_ec_i2c", 152 .id = UCLASS_CROS_EC, 153 .of_match = cros_ec_ids, 154 .probe = cros_ec_probe, 155 .ops = &cros_ec_ops, 156 }; 157