blob: f2f6961499bc2ddb0f52cab1ce0cc8d5b3054e1b [file] [log] [blame]
Hung-ying Tyan68304082013-05-15 18:27:29 +08001/*
2 * Chromium OS cros_ec driver - I2C interface
3 *
4 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyan68304082013-05-15 18:27:29 +08005 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Hung-ying Tyan68304082013-05-15 18:27:29 +08007 */
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 Glassa1f26032015-01-26 20:29:40 -070017#include <dm.h>
Hung-ying Tyan68304082013-05-15 18:27:29 +080018#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 Glassa1f26032015-01-26 20:29:40 -070027static 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 Tyan68304082013-05-15 18:27:29 +080030{
Simon Glassde0977b2015-03-05 12:25:20 -070031 struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
Moritz Fischer9b606d62017-01-12 09:47:30 -080032 struct dm_i2c_chip *chip = dev_get_parent_platdata(udev);
33 struct i2c_msg i2c_msg[2];
Hung-ying Tyan68304082013-05-15 18:27:29 +080034 /* 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 Spanglerf03e9702014-02-27 13:26:08 -070041 int len, csum, ret;
Hung-ying Tyan68304082013-05-15 18:27:29 +080042
Hung-ying Tyan68304082013-05-15 18:27:29 +080043 /*
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 Fischer9b606d62017-01-12 09:47:30 -080058 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 Tyan68304082013-05-15 18:27:29 +080063 /*
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 Spanglerf03e9702014-02-27 13:26:08 -070076
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 Tyan68304082013-05-15 18:27:29 +080082 }
Randall Spanglerf03e9702014-02-27 13:26:08 -070083
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 Tyan68304082013-05-15 18:27:29 +080089 memcpy(ptr, dout, dout_len);
90 ptr += dout_len;
91
Randall Spanglerf03e9702014-02-27 13:26:08 -070092 *ptr++ = (uint8_t)
93 cros_ec_calc_checksum(dev->dout, dout_len + 3);
Hung-ying Tyan68304082013-05-15 18:27:29 +080094
Moritz Fischer9b606d62017-01-12 09:47:30 -080095 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 Tyan68304082013-05-15 18:27:29 +0800100 /* Send output data */
101 cros_ec_dump_data("out", -1, dev->dout, out_bytes);
Moritz Fischer9b606d62017-01-12 09:47:30 -0800102
103 ret = dm_i2c_xfer(udev, &i2c_msg[0], 2);
Hung-ying Tyan68304082013-05-15 18:27:29 +0800104 if (ret) {
Moritz Fischer9b606d62017-01-12 09:47:30 -0800105 debug("%s: Could not execute transfer to %s\n", __func__,
Simon Glassa1f26032015-01-26 20:29:40 -0700106 udev->name);
Hung-ying Tyan68304082013-05-15 18:27:29 +0800107 ret = -1;
108 }
109
Hung-ying Tyan68304082013-05-15 18:27:29 +0800110 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 Spanglerf03e9702014-02-27 13:26:08 -0700115 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 Tyan68304082013-05-15 18:27:29 +0800120 }
Randall Spanglerf03e9702014-02-27 13:26:08 -0700121 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 Tyan68304082013-05-15 18:27:29 +0800129
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 Glassa1f26032015-01-26 20:29:40 -0700136static int cros_ec_probe(struct udevice *dev)
Hung-ying Tyan68304082013-05-15 18:27:29 +0800137{
Simon Glassa1f26032015-01-26 20:29:40 -0700138 return cros_ec_register(dev);
Hung-ying Tyan68304082013-05-15 18:27:29 +0800139}
140
Simon Glassa1f26032015-01-26 20:29:40 -0700141static struct dm_cros_ec_ops cros_ec_ops = {
142 .command = cros_ec_i2c_command,
143};
Hung-ying Tyan68304082013-05-15 18:27:29 +0800144
Simon Glassa1f26032015-01-26 20:29:40 -0700145static const struct udevice_id cros_ec_ids[] = {
Simon Glass73f220c2015-03-26 09:29:39 -0600146 { .compatible = "google,cros-ec-i2c" },
Simon Glassa1f26032015-01-26 20:29:40 -0700147 { }
148};
149
150U_BOOT_DRIVER(cros_ec_i2c) = {
Simon Glass73f220c2015-03-26 09:29:39 -0600151 .name = "cros_ec_i2c",
Simon Glassa1f26032015-01-26 20:29:40 -0700152 .id = UCLASS_CROS_EC,
153 .of_match = cros_ec_ids,
154 .probe = cros_ec_probe,
155 .ops = &cros_ec_ops,
156};