summaryrefslogtreecommitdiff
path: root/common/i2c_trace.c
blob: 06990cfa4f27a6fe0961102a7ec831fbb89195a1 (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
/* Copyright 2019 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 "common.h"
#include "console.h"
#include "i2c.h"
#include "stddef.h"
#include "stdbool.h"
#include "util.h"

#define CPUTS(outstr) cputs(CC_I2C, outstr)
#define CPRINTS(format, args...) cprints(CC_I2C, format, ## args)
#define CPRINTF(format, args...) cprintf(CC_I2C, format, ## args)

struct i2c_trace_range {
	bool enabled;
	int port;
	int slave_addr_lo; /* Inclusive */
	int slave_addr_hi; /* Inclusive */
};

static struct i2c_trace_range trace_entries[8];

void i2c_trace_notify(int port, uint16_t slave_addr_flags,
		      int direction, const uint8_t *data, size_t size)
{
	size_t i;
	uint16_t addr = I2C_GET_ADDR(slave_addr_flags);

	for (i = 0; i < ARRAY_SIZE(trace_entries); i++)
		if (trace_entries[i].enabled
		    && trace_entries[i].port == port
		    && trace_entries[i].slave_addr_lo <= addr
		    && trace_entries[i].slave_addr_hi >= addr)
			goto trace_enabled;
	return;

trace_enabled:
	CPRINTF("i2c: %s %d:0x%X ",
		direction ? "read" : "write",
		port,
		addr);
	for (i = 0; i < size; i++)
		CPRINTF("%02X ", data[i]);
	CPRINTF("\n");
}

static int command_i2ctrace_list(void)
{
	size_t i;

	ccprintf("id port address\n");
	ccprintf("-- ---- -------\n");

	for (i = 0; i < ARRAY_SIZE(trace_entries); i++) {
		if (trace_entries[i].enabled) {
			ccprintf("%2d %4d 0x%X",
				 i,
				 trace_entries[i].port,
				 trace_entries[i].slave_addr_lo);
			if (trace_entries[i].slave_addr_hi
			    != trace_entries[i].slave_addr_lo)
				ccprintf(" to 0x%X",
					 trace_entries[i].slave_addr_hi);
			ccprintf("\n");
		}
	}

	return EC_SUCCESS;
}

static int command_i2ctrace_disable(size_t id)
{
	if (id >= ARRAY_SIZE(trace_entries))
		return EC_ERROR_PARAM2;

	trace_entries[id].enabled = 0;
	return EC_SUCCESS;
}

static int command_i2ctrace_enable(int port, int slave_addr_lo,
				   int slave_addr_hi)
{
	struct i2c_trace_range *t;
	struct i2c_trace_range *new_entry = NULL;

	if (port >= i2c_ports_used)
		return EC_ERROR_PARAM2;

	if (slave_addr_lo > slave_addr_hi)
		return EC_ERROR_PARAM3;

	/*
	 * Scan thru existing entries to see if there is one we can
	 * extend instead of making a new entry
	 */
	for (t = trace_entries;
	     t < trace_entries + ARRAY_SIZE(trace_entries);
	     t++) {
		if (t->enabled && t->port == port) {
			/* Subset of existing range, do nothing */
			if (t->slave_addr_lo <= slave_addr_lo &&
			    t->slave_addr_hi >= slave_addr_hi)
				return EC_SUCCESS;

			/* Extends exising range on both directions, replace */
			if (t->slave_addr_lo >= slave_addr_lo &&
			    t->slave_addr_hi <= slave_addr_hi) {
				t->enabled = 0;
				return command_i2ctrace_enable(
					port, slave_addr_lo, slave_addr_hi);
			}

			/* Extends existing range below */
			if (t->slave_addr_lo - 1 <= slave_addr_hi &&
			    t->slave_addr_hi >= slave_addr_hi) {
				t->enabled = 0;
				return command_i2ctrace_enable(
					port,
					slave_addr_lo,
					t->slave_addr_hi);
			}

			/* Extends existing range above */
			if (t->slave_addr_lo <= slave_addr_lo &&
			    t->slave_addr_hi + 1 >= slave_addr_lo) {
				t->enabled = 0;
				return command_i2ctrace_enable(
					port,
					t->slave_addr_lo,
					slave_addr_hi);
			}
		} else if (!t->enabled && !new_entry) {
			new_entry = t;
		}
	}

	/* We need to allocate a new entry */
	if (new_entry) {
		new_entry->enabled = 1;
		new_entry->port = port;
		new_entry->slave_addr_lo = slave_addr_lo;
		new_entry->slave_addr_hi = slave_addr_hi;
		return EC_SUCCESS;
	}

	ccprintf("No space to allocate new trace entry. Delete some first.\n");
	return EC_ERROR_MEMORY_ALLOCATION;
}


static int command_i2ctrace(int argc, char **argv)
{
	int id_or_port;
	int address_low;
	int address_high;
	char *end;

	if (argc < 2)
		return EC_ERROR_PARAM_COUNT;

	if (!strcasecmp(argv[1], "list") && argc == 2)
		return command_i2ctrace_list();

	if (argc < 3)
		return EC_ERROR_PARAM_COUNT;

	id_or_port = strtoi(argv[2], &end, 0);
	if (*end || id_or_port < 0)
		return EC_ERROR_PARAM2;

	if (!strcasecmp(argv[1], "disable") && argc == 3)
		return command_i2ctrace_disable(id_or_port);

	if (!strcasecmp(argv[1], "enable")) {
		address_low = strtoi(argv[3], &end, 0);
		if (*end || address_low < 0)
			return EC_ERROR_PARAM3;

		if (argc == 4) {
			address_high = address_low;
		} else if (argc == 5) {
			address_high = strtoi(argv[4], &end, 0);
			if (*end || address_high < 0)
				return EC_ERROR_PARAM4;
		} else {
			return EC_ERROR_PARAM_COUNT;
		}

		return command_i2ctrace_enable(
			id_or_port, address_low, address_high);
	}

	return EC_ERROR_PARAM1;
}
DECLARE_CONSOLE_COMMAND(i2ctrace,
			command_i2ctrace,
			"[list | disable <id> | enable <port> <address> | "
			"enable <port> <address-low> <address-high>]",
			"Trace I2C transactions");