1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <common.h>
17#include <i2c.h>
18#include <cros_ec.h>
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
26int cros_ec_i2c_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
27 const uint8_t *dout, int dout_len,
28 uint8_t **dinp, int din_len)
29{
30 int old_bus = 0;
31
32 int out_bytes = dout_len + 4;
33
34 int in_bytes = din_len + 3;
35 uint8_t *ptr;
36
37 uint8_t *in_ptr;
38 int ret;
39
40 old_bus = i2c_get_bus_num();
41
42
43
44
45
46 if (out_bytes > sizeof(dev->dout)) {
47 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
48 return -1;
49 }
50 if (in_bytes > sizeof(dev->din)) {
51 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
52 return -1;
53 }
54 assert(dout_len >= 0);
55 assert(dinp);
56
57
58
59
60
61 ptr = dev->dout;
62
63
64
65
66
67
68
69 in_ptr = dev->din + sizeof(int64_t);
70 if (!dev->cmd_version_is_supported) {
71
72 *ptr++ = cmd;
73 out_bytes = dout_len + 1;
74 in_bytes = din_len + 2;
75 in_ptr--;
76 } else {
77 *ptr++ = EC_CMD_VERSION0 + cmd_version;
78 *ptr++ = cmd;
79 *ptr++ = dout_len;
80 in_ptr -= 2;
81 }
82 memcpy(ptr, dout, dout_len);
83 ptr += dout_len;
84
85 if (dev->cmd_version_is_supported)
86 *ptr++ = (uint8_t)
87 cros_ec_calc_checksum(dev->dout, dout_len + 3);
88
89
90 if (i2c_set_bus_num(dev->bus_num)) {
91 debug("%s: Cannot change to I2C bus %d\n", __func__,
92 dev->bus_num);
93 return -1;
94 }
95
96
97 cros_ec_dump_data("out", -1, dev->dout, out_bytes);
98 ret = i2c_write(dev->addr, 0, 0, dev->dout, out_bytes);
99 if (ret) {
100 debug("%s: Cannot complete I2C write to 0x%x\n",
101 __func__, dev->addr);
102 ret = -1;
103 }
104
105 if (!ret) {
106 ret = i2c_read(dev->addr, 0, 0, in_ptr, in_bytes);
107 if (ret) {
108 debug("%s: Cannot complete I2C read from 0x%x\n",
109 __func__, dev->addr);
110 ret = -1;
111 }
112 }
113
114
115 i2c_set_bus_num(old_bus);
116 if (ret)
117 return ret;
118
119 if (*in_ptr != EC_RES_SUCCESS) {
120 debug("%s: Received bad result code %d\n", __func__, *in_ptr);
121 return -(int)*in_ptr;
122 }
123
124 if (dev->cmd_version_is_supported) {
125 int len, csum;
126
127 len = in_ptr[1];
128 if (len + 3 > sizeof(dev->din)) {
129 debug("%s: Received length %#02x too large\n",
130 __func__, len);
131 return -1;
132 }
133 csum = cros_ec_calc_checksum(in_ptr, 2 + len);
134 if (csum != in_ptr[2 + len]) {
135 debug("%s: Invalid checksum rx %#02x, calced %#02x\n",
136 __func__, in_ptr[2 + din_len], csum);
137 return -1;
138 }
139 din_len = min(din_len, len);
140 cros_ec_dump_data("in", -1, in_ptr, din_len + 3);
141 } else {
142 cros_ec_dump_data("in (old)", -1, in_ptr, in_bytes);
143 }
144
145
146 *dinp = dev->din + sizeof(int64_t);
147
148 return din_len;
149}
150
151int cros_ec_i2c_decode_fdt(struct cros_ec_dev *dev, const void *blob)
152{
153
154 dev->max_frequency = fdtdec_get_int(blob, dev->node,
155 "i2c-max-frequency", 100000);
156 dev->bus_num = i2c_get_bus_num_fdt(dev->parent_node);
157 if (dev->bus_num == -1) {
158 debug("%s: Failed to read bus number\n", __func__);
159 return -1;
160 }
161 dev->addr = fdtdec_get_int(blob, dev->node, "reg", -1);
162 if (dev->addr == -1) {
163 debug("%s: Failed to read device address\n", __func__);
164 return -1;
165 }
166
167 return 0;
168}
169
170
171
172
173
174
175
176
177int cros_ec_i2c_init(struct cros_ec_dev *dev, const void *blob)
178{
179 i2c_init(dev->max_frequency, dev->addr);
180
181 dev->cmd_version_is_supported = 0;
182
183 return 0;
184}
185