1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "mock/tcpci_i2c_mock.h"
#include "task.h"
#include "tcpci.h"
#include "test_util.h"
#include "timer.h"
#define BUFFER_SIZE 100
struct tcpci_reg {
uint8_t offset;
uint8_t size;
uint16_t value;
const char *name;
};
#define TCPCI_REG(reg_name, reg_size) \
[reg_name] = { .offset = (reg_name), .size = (reg_size), \
.value = 0, .name = #reg_name, }
static struct tcpci_reg tcpci_regs[] = {
TCPCI_REG(TCPC_REG_VENDOR_ID, 2),
TCPCI_REG(TCPC_REG_PRODUCT_ID, 2),
TCPCI_REG(TCPC_REG_BCD_DEV, 2),
TCPCI_REG(TCPC_REG_TC_REV, 2),
TCPCI_REG(TCPC_REG_PD_REV, 2),
TCPCI_REG(TCPC_REG_PD_INT_REV, 2),
TCPCI_REG(TCPC_REG_ALERT, 2),
TCPCI_REG(TCPC_REG_ALERT_MASK, 2),
TCPCI_REG(TCPC_REG_POWER_STATUS_MASK, 1),
TCPCI_REG(TCPC_REG_FAULT_STATUS_MASK, 1),
TCPCI_REG(TCPC_REG_EXT_STATUS_MASK, 1),
TCPCI_REG(TCPC_REG_ALERT_EXTENDED_MASK, 1),
TCPCI_REG(TCPC_REG_CONFIG_STD_OUTPUT, 1),
TCPCI_REG(TCPC_REG_TCPC_CTRL, 1),
TCPCI_REG(TCPC_REG_ROLE_CTRL, 1),
TCPCI_REG(TCPC_REG_FAULT_CTRL, 1),
TCPCI_REG(TCPC_REG_POWER_CTRL, 1),
TCPCI_REG(TCPC_REG_CC_STATUS, 1),
TCPCI_REG(TCPC_REG_POWER_STATUS, 1),
TCPCI_REG(TCPC_REG_FAULT_STATUS, 1),
TCPCI_REG(TCPC_REG_EXT_STATUS, 1),
TCPCI_REG(TCPC_REG_ALERT_EXT, 1),
TCPCI_REG(TCPC_REG_DEV_CAP_1, 2),
TCPCI_REG(TCPC_REG_DEV_CAP_2, 2),
TCPCI_REG(TCPC_REG_STD_INPUT_CAP, 1),
TCPCI_REG(TCPC_REG_STD_OUTPUT_CAP, 1),
TCPCI_REG(TCPC_REG_CONFIG_EXT_1, 1),
TCPCI_REG(TCPC_REG_MSG_HDR_INFO, 1),
TCPCI_REG(TCPC_REG_RX_DETECT, 1),
TCPCI_REG(TCPC_REG_RX_BUFFER, BUFFER_SIZE),
TCPCI_REG(TCPC_REG_TRANSMIT, 1),
TCPCI_REG(TCPC_REG_TX_BUFFER, BUFFER_SIZE),
TCPCI_REG(TCPC_REG_VBUS_VOLTAGE, 2),
TCPCI_REG(TCPC_REG_VBUS_SINK_DISCONNECT_THRESH, 2),
TCPCI_REG(TCPC_REG_VBUS_STOP_DISCHARGE_THRESH, 2),
TCPCI_REG(TCPC_REG_VBUS_VOLTAGE_ALARM_HI_CFG, 2),
TCPCI_REG(TCPC_REG_VBUS_VOLTAGE_ALARM_LO_CFG, 2),
TCPCI_REG(TCPC_REG_COMMAND, 1),
};
static uint8_t tx_buffer[BUFFER_SIZE];
static int tx_pos = -1;
static uint8_t rx_buffer[BUFFER_SIZE];
static int rx_pos = -1;
static const char * const ctrl_msg_name[] = {
[0] = "RSVD-C0",
[PD_CTRL_GOOD_CRC] = "GOODCRC",
[PD_CTRL_GOTO_MIN] = "GOTOMIN",
[PD_CTRL_ACCEPT] = "ACCEPT",
[PD_CTRL_REJECT] = "REJECT",
[PD_CTRL_PING] = "PING",
[PD_CTRL_PS_RDY] = "PSRDY",
[PD_CTRL_GET_SOURCE_CAP] = "GSRCCAP",
[PD_CTRL_GET_SINK_CAP] = "GSNKCAP",
[PD_CTRL_DR_SWAP] = "DRSWAP",
[PD_CTRL_PR_SWAP] = "PRSWAP",
[PD_CTRL_VCONN_SWAP] = "VCONNSW",
[PD_CTRL_WAIT] = "WAIT",
[PD_CTRL_SOFT_RESET] = "SFT-RST",
[14] = "RSVD-C14",
[15] = "RSVD-C15",
[PD_CTRL_NOT_SUPPORTED] = "NOT-SUPPORTED",
[PD_CTRL_GET_SOURCE_CAP_EXT] = "GSRCCAP-EXT",
[PD_CTRL_GET_STATUS] = "GET-STATUS",
[PD_CTRL_FR_SWAP] = "FRSWAP",
[PD_CTRL_GET_PPS_STATUS] = "GET-PPS-STATUS",
[PD_CTRL_GET_COUNTRY_CODES] = "GET-COUNTRY-CODES",
};
static const char * const data_msg_name[] = {
[0] = "RSVD-D0",
[PD_DATA_SOURCE_CAP] = "SRCCAP",
[PD_DATA_REQUEST] = "REQUEST",
[PD_DATA_BIST] = "BIST",
[PD_DATA_SINK_CAP] = "SNKCAP",
/* 5-14 Reserved */
[PD_DATA_VENDOR_DEF] = "VDM",
};
static const char * const rev_name[] = {
[PD_REV10] = "1.0",
[PD_REV20] = "2.0",
[PD_REV30] = "3.0",
[3] = "RSVD",
};
static const char * const drole_name[] = {
[PD_ROLE_UFP] = "UFP",
[PD_ROLE_DFP] = "DFP",
};
static const char * const prole_name[] = {
[PD_ROLE_SINK] = "SNK",
[PD_ROLE_SOURCE] = "SRC",
};
static void print_header(const char *prefix, uint16_t header)
{
int type = PD_HEADER_TYPE(header);
int drole = PD_HEADER_DROLE(header);
int rev = PD_HEADER_REV(header);
int prole = PD_HEADER_PROLE(header);
int id = PD_HEADER_ID(header);
int cnt = PD_HEADER_CNT(header);
int ext = PD_HEADER_EXT(header);
const char *name = cnt ? data_msg_name[type] : ctrl_msg_name[type];
ccprints("%s header=0x%x [%s %s %s %s id=%d cnt=%d ext=%d]",
prefix, header,
name, drole_name[drole], rev_name[rev], prole_name[prole],
id, cnt, ext);
}
int mock_tcpci_wait_for_transmit(enum tcpm_transmit_type tx_type,
enum pd_ctrl_msg_type ctrl_msg,
enum pd_data_msg_type data_msg)
{
int want_tx_reg = (tx_type == TCPC_TX_SOP_PRIME) ?
TCPC_REG_TRANSMIT_SET_WITHOUT_RETRY(tx_type) :
TCPC_REG_TRANSMIT_SET_WITH_RETRY(tx_type);
uint64_t timeout = get_time().val + 5 * SECOND;
TEST_EQ(tcpci_regs[TCPC_REG_TRANSMIT].value, 0, "%d");
while (get_time().val < timeout) {
if (tcpci_regs[TCPC_REG_TRANSMIT].value != 0) {
uint16_t header = UINT16_FROM_BYTE_ARRAY_LE(
tx_buffer, 1);
int type = PD_HEADER_TYPE(header);
int cnt = PD_HEADER_CNT(header);
TEST_EQ(tcpci_regs[TCPC_REG_TRANSMIT].value,
want_tx_reg, "%d");
if (ctrl_msg != 0) {
TEST_EQ(ctrl_msg, type, "%d");
TEST_EQ(cnt, 0, "%d");
} else {
TEST_EQ(data_msg, type, "%d");
TEST_GE(cnt, 1, "%d");
}
tcpci_regs[TCPC_REG_TRANSMIT].value = 0;
return EC_SUCCESS;
}
task_wait_event(5 * MSEC);
}
TEST_ASSERT(0);
return EC_ERROR_UNKNOWN;
}
void mock_tcpci_receive(enum pd_msg_type sop, uint16_t header,
uint32_t *payload)
{
int i;
rx_buffer[0] = 3 + (PD_HEADER_CNT(header) * 4);
rx_buffer[1] = sop;
rx_buffer[2] = header & 0xFF;
rx_buffer[3] = (header >> 8) & 0xFF;
if (rx_buffer[0] >= BUFFER_SIZE) {
ccprints("ERROR: rx too large");
return;
}
for (i = 4; i < rx_buffer[0]; i += 4) {
rx_buffer[i] = *payload & 0xFF;
rx_buffer[i+1] = (*payload >> 8) & 0xFF;
rx_buffer[i+2] = (*payload >> 16) & 0xFF;
rx_buffer[i+3] = (*payload >> 24) & 0xFF;
payload++;
}
rx_pos = 0;
}
void mock_tcpci_reset(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(tcpci_regs); i++)
tcpci_regs[i].value = 0;
}
void mock_tcpci_set_reg(int reg_offset, uint16_t value)
{
struct tcpci_reg *reg = tcpci_regs + reg_offset;
reg->value = value;
ccprints("TCPCI mock set %s = 0x%x", reg->name, reg->value);
}
uint16_t mock_tcpci_get_reg(int reg_offset)
{
return tcpci_regs[reg_offset].value;
}
int tcpci_i2c_xfer(int port, uint16_t slave_addr_flags,
const uint8_t *out, int out_size,
uint8_t *in, int in_size, int flags)
{
struct tcpci_reg *reg;
if (port != I2C_PORT_HOST_TCPC) {
ccprints("ERROR: wrong I2C port %d", port);
return EC_ERROR_UNKNOWN;
}
if (slave_addr_flags != MOCK_TCPCI_I2C_ADDR_FLAGS) {
ccprints("ERROR: wrong I2C address 0x%x", slave_addr_flags);
return EC_ERROR_UNKNOWN;
}
if (rx_pos > 0) {
if (rx_pos + in_size > rx_buffer[0] + 1) {
ccprints("ERROR: rx in_size");
return EC_ERROR_UNKNOWN;
}
memcpy(in, rx_buffer + rx_pos, in_size);
rx_pos += in_size;
if (rx_pos == rx_buffer[0] + 1) {
print_header("RX", UINT16_FROM_BYTE_ARRAY_LE(
rx_buffer, 2));
rx_pos = -1;
}
return EC_SUCCESS;
}
if (out_size == 0) {
ccprints("ERROR: out_size == 0");
return EC_ERROR_UNKNOWN;
}
if (tx_pos != -1) {
if (tx_pos + out_size > BUFFER_SIZE) {
ccprints("ERROR: tx out_size");
return EC_ERROR_UNKNOWN;
}
memcpy(tx_buffer + tx_pos, out, out_size);
tx_pos += out_size;
if (tx_pos > 0 && tx_pos == tx_buffer[0] + 1) {
print_header("TX", UINT16_FROM_BYTE_ARRAY_LE(
tx_buffer, 1));
tx_pos = -1;
}
return EC_SUCCESS;
}
reg = tcpci_regs + *out;
if (*out >= ARRAY_SIZE(tcpci_regs) || reg->size == 0) {
ccprints("ERROR: unknown reg 0x%x", *out);
return EC_ERROR_UNKNOWN;
}
if (reg->offset == TCPC_REG_TX_BUFFER) {
if (tx_pos != -1) {
ccprints("ERROR: TCPC_REG_TX_BUFFER not ready");
return EC_ERROR_UNKNOWN;
}
tx_pos = 0;
if (out_size != 1) {
ccprints("ERROR: TCPC_REG_TX_BUFFER out_size != 1");
return EC_ERROR_UNKNOWN;
}
} else if (reg->offset == TCPC_REG_RX_BUFFER) {
if (rx_pos != 0) {
ccprints("ERROR: TCPC_REG_RX_BUFFER not ready");
return EC_ERROR_UNKNOWN;
}
if (in_size > BUFFER_SIZE || in_size > rx_buffer[0]) {
ccprints("ERROR: TCPC_REG_RX_BUFFER in_size");
return EC_ERROR_UNKNOWN;
}
memcpy(in, rx_buffer, in_size);
rx_pos += in_size;
} else if (out_size == 1) {
if (in_size != reg->size) {
ccprints("ERROR: %s in_size %d != %d", reg->name,
in_size, reg->size);
return EC_ERROR_UNKNOWN;
}
if (reg->size == 1)
in[0] = reg->value;
else if (reg->size == 2) {
in[0] = reg->value;
in[1] = reg->value >> 8;
}
} else {
uint16_t value = 0;
if (in_size != 0) {
ccprints("ERROR: in_size != 0");
return EC_ERROR_UNKNOWN;
}
if (out_size != reg->size + 1) {
ccprints("ERROR: out_size != %d", reg->size + 1);
return EC_ERROR_UNKNOWN;
}
if (reg->size == 1)
value = out[1];
else if (reg->size == 2)
value = out[1] + (out[2] << 8);
ccprints("%s TCPCI write %s = 0x%x",
task_get_name(task_get_current()),
reg->name, value);
if (reg->offset == TCPC_REG_ALERT)
reg->value &= ~value;
else
reg->value = value;
}
return EC_SUCCESS;
}
DECLARE_TEST_I2C_XFER(tcpci_i2c_xfer);
|