summaryrefslogtreecommitdiff
path: root/zephyr/emul/emul_bb_retimer.c
blob: 8028c53c0f6d0bde411f242fd97a490c4e8ad140 (plain)
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
334
335
336
337
338
339
340
341
/* Copyright 2021 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "driver/retimer/bb_retimer.h"
#include "emul/emul_bb_retimer.h"
#include "emul/emul_common_i2c.h"
#include "emul/emul_stub_device.h"

#include <zephyr/device.h>
#include <zephyr/drivers/emul.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/i2c_emul.h>
#include <zephyr/logging/log.h>

#define DT_DRV_COMPAT intel_jhl8040r

#define LOG_LEVEL CONFIG_I2C_LOG_LEVEL
LOG_MODULE_REGISTER(emul_bb_retimer);

/** Run-time data used by the emulator */
struct bb_emul_data {
	/** Common I2C data */
	struct i2c_common_emul_data common;

	/** Current state of all emulated BB retimer registers */
	uint32_t reg[BB_RETIMER_REG_COUNT];

	/** Vendor ID of emulated device */
	uint32_t vendor_id;

	/** Return error when trying to write to RO register */
	bool error_on_ro_write;
	/** Return error when trying to write 1 to reserved bit */
	bool error_on_rsvd_write;

	/** Value of data dword in ongoing i2c message */
	uint32_t data_dword;
};

/** Check description in emul_bb_retimer.h */
void bb_emul_set_reg(const struct emul *emul, int reg, uint32_t val)
{
	struct bb_emul_data *data;

	if (reg < 0 || reg > BB_RETIMER_REG_COUNT) {
		return;
	}

	data = emul->data;
	data->reg[reg] = val;
}

/** Check description in emul_bb_retimer.h */
uint32_t bb_emul_get_reg(const struct emul *emul, int reg)
{
	struct bb_emul_data *data;

	if (reg < 0 || reg > BB_RETIMER_REG_COUNT) {
		return 0;
	}

	data = emul->data;

	return data->reg[reg];
}

/** Check description in emul_bb_retimer.h */
void bb_emul_set_err_on_ro_write(const struct emul *emul, bool set)
{
	struct bb_emul_data *data;

	data = emul->data;
	data->error_on_ro_write = set;
}

/** Check description in emul_bb_retimer.h */
void bb_emul_set_err_on_rsvd_write(const struct emul *emul, bool set)
{
	struct bb_emul_data *data;

	data = emul->data;
	data->error_on_rsvd_write = set;
}

/** Mask reserved bits in each register of BB retimer */
static const uint32_t bb_emul_rsvd_mask[] = {
	[BB_RETIMER_REG_VENDOR_ID] = 0x00000000,
	[BB_RETIMER_REG_DEVICE_ID] = 0x00000000,
	[0x02] = 0xffffffff, /* Reserved */
	[0x03] = 0xffffffff, /* Reserved */
	[BB_RETIMER_REG_CONNECTION_STATE] = 0xc0201000,
	[BB_RETIMER_REG_TBT_CONTROL] = 0xffffdfff,
	[0x06] = 0xffffffff, /* Reserved */
	[BB_RETIMER_REG_EXT_CONNECTION_MODE] = 0x08007f00,
};

/**
 * @brief Reset registers to default values
 *
 * @param emul Pointer to BB retimer emulator
 */
static void bb_emul_reset(const struct emul *emul)
{
	struct bb_emul_data *data;

	data = emul->data;

	data->reg[BB_RETIMER_REG_VENDOR_ID] = data->vendor_id;
	data->reg[BB_RETIMER_REG_DEVICE_ID] = BB_RETIMER_DEVICE_ID;
	data->reg[0x02] = 0x00; /* Reserved */
	data->reg[0x03] = 0x00; /* Reserved */
	data->reg[BB_RETIMER_REG_CONNECTION_STATE] = 0x00;
	data->reg[BB_RETIMER_REG_TBT_CONTROL] = 0x00;
	data->reg[0x06] = 0x00; /* Reserved */
	data->reg[BB_RETIMER_REG_EXT_CONNECTION_MODE] = 0x00;
}

/**
 * @brief Handle I2C write message. It is checked if accessed register isn't RO
 *        and reserved bits are set to 0. Write set value of reg field of BB
 *        retimer emulator data ignoring reserved bits and write only bits.
 *
 * @param emul Pointer to BB retimer emulator
 * @param reg Register which is written
 * @param msg_len Length of handled I2C message
 *
 * @return 0 on success
 * @return -EIO on error
 */
static int bb_emul_handle_write(const struct emul *emul, int reg, int msg_len)
{
	struct bb_emul_data *data;
	uint32_t val;

	data = emul->data;

	/* This write only selected register for I2C read message */
	if (msg_len < 2) {
		return 0;
	}

	val = data->data_dword;

	/*
	 * BB retimer ignores data bytes above 4 and use zeros if there is less
	 * then 4 data bytes. Emulator prints warning in that case.
	 */
	if (msg_len != 6) {
		LOG_WRN("Got %d bytes of WR data, expected 4", msg_len - 2);
	}

	if (reg <= BB_RETIMER_REG_DEVICE_ID || reg >= BB_RETIMER_REG_COUNT ||
	    reg == BB_RETIMER_REG_TBT_CONTROL) {
		if (data->error_on_ro_write) {
			LOG_ERR("Writing to reg 0x%x which is RO", reg);
			return -EIO;
		}

		return 0;
	}

	if (data->error_on_rsvd_write && bb_emul_rsvd_mask[reg] & val) {
		LOG_ERR("Writing 0x%x to reg 0x%x with rsvd bits mask 0x%x",
			val, reg, bb_emul_rsvd_mask[reg]);
		return -EIO;
	}

	/* Ignore all reserved bits */
	val &= ~bb_emul_rsvd_mask[reg];
	val |= data->reg[reg] & bb_emul_rsvd_mask[reg];

	data->reg[reg] = val;

	return 0;
}

/**
 * @brief Handle I2C read message. Response is obtained from reg field of bb
 *        emul data.
 *
 * @param emul Pointer to BB retimer emulator
 * @param reg Register address to read
 *
 * @return 0 on success
 * @return -EIO on error
 */
static int bb_emul_handle_read(const struct emul *emul, int reg)
{
	struct bb_emul_data *data;

	data = emul->data;

	if (reg >= BB_RETIMER_REG_COUNT) {
		LOG_ERR("Read unknown register 0x%x", reg);

		return -EIO;
	}

	data->data_dword = data->reg[reg];

	return 0;
}

/**
 * @brief Function called for each byte of write message. Data are stored
 *        in data_dword field of bb_emul_data
 *
 * @param emul Pointer to BB retimer emulator
 * @param reg First byte of write message
 * @param val Received byte of write message
 * @param bytes Number of bytes already received
 *
 * @return 0 on success
 */
static int bb_emul_write_byte(const struct emul *emul, int reg, uint8_t val,
			      int bytes)
{
	struct bb_emul_data *data;

	data = emul->data;

	if (bytes == 1) {
		data->data_dword = 0;
		if (val != 4) {
			LOG_WRN("Invalid write size");
		}
	} else if (bytes < 6) {
		data->data_dword |= val << (8 * (bytes - 2));
	}

	return 0;
}

/**
 * @brief Function called for each byte of read message. data_dword is converted
 *        to read message response.
 *
 * @param emul Pointer to BB retimer emulator
 * @param reg First byte of last write message
 * @param val Pointer where byte to read should be stored
 * @param bytes Number of bytes already readed
 *
 * @return 0 on success
 */
static int bb_emul_read_byte(const struct emul *emul, int reg, uint8_t *val,
			     int bytes)
{
	struct bb_emul_data *data;

	data = emul->data;

	/* First byte of read message is read size which is always 4 */
	if (bytes == 0) {
		*val = 4;
		return 0;
	}

	*val = data->data_dword & 0xff;
	data->data_dword >>= 8;

	return 0;
}

/**
 * @brief Get currently accessed register, which always equals to selected
 *        register.
 *
 * @param emul Pointer to BB retimer emulator
 * @param reg First byte of last write message
 * @param bytes Number of bytes already handled from current message
 * @param read If currently handled is read message
 *
 * @return Currently accessed register
 */
static int bb_emul_access_reg(const struct emul *emul, int reg, int bytes,
			      bool read)
{
	return reg;
}

/* Device instantiation */

/**
 * @brief Set up a new BB retimer emulator
 *
 * This should be called for each BB retimer device that needs to be
 * emulated. It registers it with the I2C emulation controller.
 *
 * @param emul Emulation information
 * @param parent Device to emulate
 *
 * @return 0 indicating success (always)
 */
static int bb_emul_init(const struct emul *emul, const struct device *parent)
{
	struct bb_emul_data *data = emul->data;

	data->common.i2c = parent;

	i2c_common_emul_init(&data->common);

	bb_emul_reset(emul);

	return 0;
}

#define BB_RETIMER_EMUL(n)                                          \
	static struct bb_emul_data bb_emul_data_##n = {			\
		.vendor_id = BB_RETIMER_VENDOR_ID_1,	\
		.error_on_ro_write = true, \
		.error_on_rsvd_write = true,	\
		.common = {						\
			.start_write = NULL,				\
			.write_byte = bb_emul_write_byte,		\
			.finish_write = bb_emul_handle_write,		\
			.start_read = bb_emul_handle_read,		\
			.read_byte = bb_emul_read_byte,			\
			.finish_read = NULL,				\
			.access_reg = bb_emul_access_reg,		\
		},							\
	};         \
                                                                    \
	static const struct i2c_common_emul_cfg bb_emul_cfg_##n = { \
		.dev_label = DT_NODE_FULL_NAME(DT_DRV_INST(n)),     \
		.data = &bb_emul_data_##n.common,                   \
		.addr = DT_INST_REG_ADDR(n),                        \
	};                                                          \
	EMUL_DT_INST_DEFINE(n, bb_emul_init, &bb_emul_data_##n,     \
			    &bb_emul_cfg_##n, &i2c_common_emul_api)

DT_INST_FOREACH_STATUS_OKAY(BB_RETIMER_EMUL)

DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE);

struct i2c_common_emul_data *
emul_bb_retimer_get_i2c_common_data(const struct emul *emul)
{
	return emul->data;
}