summaryrefslogtreecommitdiff
path: root/common/i2c_wedge.c
blob: 48bcac090caa3e2fc68ac62b64c2f3e8f6ea8300 (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 2013 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.
 */

/*
 * Define CONFIG_CMD_I2CWEDGE and I2C_PORT_HOST to enable the 'i2cwedge'
 * console command to allow us to bang the bus into a wedged state. For
 * example, include the following lines in board/pit/board.h to enable it on
 * pit:
 *
 * #define CONFIG_CMD_I2CWEDGE
 * #define I2C_PORT_HOST I2C_PORT_CONTROLLER
 *
 */

#include "console.h"
#include "gpio.h"
#include "i2c.h"
#include "system.h"
#include "timer.h"
#include "util.h"

/*
 * The implementation is based on Wikipedia.
 */

int i2c_bang_started;

static void i2c_bang_delay(void)
{
	udelay(5);
}

static void i2c_bang_start_cond(void)
{
	/* Restart if needed */
	if (i2c_bang_started) {
		/* set SDA to 1 */
		i2c_raw_set_sda(I2C_PORT_HOST, 1);
		i2c_bang_delay();

		/* Clock stretching */
		i2c_raw_set_scl(I2C_PORT_HOST, 1);
		while (i2c_raw_get_scl(I2C_PORT_HOST) == 0)
			; /* TODO(crosbug.com/p/26487): TIMEOUT */

		/* Repeated start setup time, minimum 4.7us */
		i2c_bang_delay();
	}

	i2c_raw_set_sda(I2C_PORT_HOST, 1);
	if (i2c_raw_get_sda(I2C_PORT_HOST) == 0)
		; /* TODO(crosbug.com/p/26487): arbitration_lost */

	/* SCL is high, set SDA from 1 to 0. */
	i2c_raw_set_sda(I2C_PORT_HOST, 0);
	i2c_bang_delay();
	i2c_raw_set_scl(I2C_PORT_HOST, 0);
	i2c_bang_started = 1;

	ccputs("BITBANG: send start\n");
}

static void i2c_bang_stop_cond(void)
{
	/* set SDA to 0 */
	i2c_raw_set_sda(I2C_PORT_HOST, 0);
	i2c_bang_delay();

	/* Clock stretching */
	i2c_raw_set_scl(I2C_PORT_HOST, 1);
	while (i2c_raw_get_scl(I2C_PORT_HOST) == 0)
		; /* TODO(crosbug.com/p/26487): TIMEOUT */

	/* Stop bit setup time, minimum 4us */
	i2c_bang_delay();

	/* SCL is high, set SDA from 0 to 1 */
	i2c_raw_set_sda(I2C_PORT_HOST, 1);
	if (i2c_raw_get_sda(I2C_PORT_HOST) == 0)
		; /* TODO(crosbug.com/p/26487): arbitration_lost */

	i2c_bang_delay();

	i2c_bang_started = 0;
	ccputs("BITBANG: send stop\n");
}

static void i2c_bang_out_bit(int bit)
{
	if (bit)
		i2c_raw_set_sda(I2C_PORT_HOST, 1);
	else
		i2c_raw_set_sda(I2C_PORT_HOST, 0);

	i2c_bang_delay();

	/* Clock stretching */
	i2c_raw_set_scl(I2C_PORT_HOST, 1);
	while (i2c_raw_get_scl(I2C_PORT_HOST) == 0)
		; /* TODO(crosbug.com/p/26487): TIMEOUT */

	/*
	 * SCL is high, now data is valid
	 * If SDA is high, check that nobody else is driving SDA
	 */
	i2c_raw_set_sda(I2C_PORT_HOST, 1);
	if (bit && i2c_raw_get_sda(I2C_PORT_HOST) == 0)
		; /* TODO(crosbug.com/p/26487): arbitration_lost */

	i2c_bang_delay();
	i2c_raw_set_scl(I2C_PORT_HOST, 0);
}

static int i2c_bang_in_bit(void)
{
	int bit;

	/* Let the peripheral drive data */
	i2c_raw_set_sda(I2C_PORT_HOST, 1);
	i2c_bang_delay();

	/* Clock stretching */
	i2c_raw_set_scl(I2C_PORT_HOST, 1);
	while (i2c_raw_get_scl(I2C_PORT_HOST) == 0)
		; /* TODO(crosbug.com/p/26487): TIMEOUT */

	/* SCL is high, now data is valid */
	bit = i2c_raw_get_sda(I2C_PORT_HOST);
	i2c_bang_delay();
	i2c_raw_set_scl(I2C_PORT_HOST, 0);

	return bit;
}

/* Write a byte to I2C bus. Return 0 if ack by the peripheral. */
static int i2c_bang_out_byte(int send_start, int send_stop, unsigned char byte)
{
	unsigned bit;
	int nack;
	int tmp = byte;

	if (send_start)
		i2c_bang_start_cond();

	for (bit = 0; bit < 8; bit++) {
		i2c_bang_out_bit((byte & 0x80) != 0);
		byte <<= 1;
	}

	nack = i2c_bang_in_bit();

	ccprintf("  write byte: %d     ack/nack=%d\n", tmp, nack);

	if (send_stop)
		i2c_bang_stop_cond();

	return nack;
}

static unsigned char i2c_bang_in_byte(int ack, int send_stop)
{
	unsigned char byte = 0;
	int i;
	for (i = 0; i < 8; ++i)
		byte = (byte << 1) | i2c_bang_in_bit();
	i2c_bang_out_bit(ack != 0);
	if (send_stop)
		i2c_bang_stop_cond();
	return byte;
}

static void i2c_bang_init(void)
{
	i2c_bang_started = 0;

	i2c_raw_mode(I2C_PORT_HOST, 1);
}

static void i2c_bang_xfer(int addr, int reg)
{
	int byte;

	i2c_bang_init();

	/* State a write command to 'addr' */
	i2c_bang_out_byte(1 /*start*/, 0 /*stop*/, addr);
	/* Write 'reg' */
	i2c_bang_out_byte(0 /*start*/, 0 /*stop*/, reg);

	/* Start a read command */
	i2c_bang_out_byte(1 /*start*/, 0 /*stop*/, addr | 1);

	/* Read two bytes */
	byte = i2c_bang_in_byte(0, 0); /* ack and no stop */
	ccprintf("  read byte: %d\n", byte);
	byte = i2c_bang_in_byte(1, 1); /* nack and stop */
	ccprintf("  read byte: %d\n", byte);
}

static void i2c_bang_wedge_write(int addr, int byte, int bit_count,
	int reboot)
{
	int i;

	i2c_bang_init();

	/* State a write command to 'addr' */
	i2c_bang_out_byte(1 /*start*/, 0 /*stop*/, addr);
	/* Send a few bits and stop */
	for (i = 0; i < bit_count; ++i) {
		i2c_bang_out_bit((byte & 0x80) != 0);
		byte <<= 1;
	}
	ccprintf("  wedged write after %d bits\n", bit_count);

	if (reboot)
		system_reset(0);
}

static void i2c_bang_wedge_read(int addr, int reg, int bit_count,
	int reboot)
{
	int i;

	i2c_bang_init();

	/* State a write command to 'addr' */
	i2c_bang_out_byte(1 /*start*/, 0 /*stop*/, addr);
	/* Write 'reg' */
	i2c_bang_out_byte(0 /*start*/, 0 /*stop*/, reg);

	/* Start a read command */
	i2c_bang_out_byte(1 /*start*/, 0 /*stop*/, addr | 1);

	/* Read bit_count bits and stop */
	for (i = 0; i < bit_count; ++i)
		i2c_bang_in_bit();

	ccprintf("  wedged read after %d bits\n", bit_count);

	if (reboot)
		system_reset(0);
}

#define WEDGE_WRITE	1
#define WEDGE_READ	2
#define WEDGE_REBOOT	4

static int command_i2c_wedge(int argc, char **argv)
{
	int addr, reg, wedge_flag = 0, wedge_bit_count = -1;
	char *e;
	enum gpio_signal tmp;

	/* Verify that the I2C_PORT_HOST has SDA and SCL pins defined. */
	if (get_sda_from_i2c_port(I2C_PORT_HOST, &tmp) != EC_SUCCESS ||
		get_scl_from_i2c_port(I2C_PORT_HOST, &tmp) != EC_SUCCESS) {
		ccprintf("Cannot wedge bus because no SCL and SDA pins are"
			"defined for this port. Check i2c_ports[].\n");
		return EC_SUCCESS;
	}

	if (argc < 3) {
		ccputs("Usage: i2cwedge addr out_byte "
			"[wedge_flag [wedge_bit_count]]\n");
		ccputs("  wedge_flag - (1: wedge out; 2: wedge in;"
			" 5: wedge out+reboot; 6: wedge in+reboot)]\n");
		ccputs("  wedge_bit_count - 0 to 8\n");
		return EC_ERROR_UNKNOWN;
	}

	addr = strtoi(argv[1], &e, 0);
	if (*e) {
		ccprintf("Invalid addr %s\n", argv[1]);
		return EC_ERROR_INVAL;
	}
	reg = strtoi(argv[2], &e, 0);
	if (*e) {
		ccprintf("Invalid out_byte %s\n", argv[2]);
		return EC_ERROR_INVAL;
	}
	if (argc > 3) {
		wedge_flag = strtoi(argv[3], &e, 0);
		if (*e) {
			ccprintf("Invalid wedge_flag %s\n", argv[3]);
			return EC_ERROR_INVAL;
		}
	}
	if (argc > 4) {
		wedge_bit_count = strtoi(argv[4], &e, 0);
		if (*e || wedge_bit_count < 0 || wedge_bit_count > 8) {
			ccprintf("Invalid wedge_bit_count %s.\n", argv[4]);
			return EC_ERROR_INVAL;
		}
	}

	i2c_lock(I2C_PORT_HOST, 1);

	if (wedge_flag & WEDGE_WRITE) {
		if (wedge_bit_count < 0)
			wedge_bit_count = 8;
		i2c_bang_wedge_write(addr, reg, wedge_bit_count,
			(wedge_flag & WEDGE_REBOOT));
	} else if (wedge_flag & WEDGE_READ) {
		if (wedge_bit_count < 0)
			wedge_bit_count = 2;
		i2c_bang_wedge_read(addr, reg, wedge_bit_count,
			(wedge_flag & WEDGE_REBOOT));
	} else {
		i2c_bang_xfer(addr, reg);
	}

	/* Put it back into normal mode */
	i2c_raw_mode(I2C_PORT_HOST, 0);

	i2c_lock(I2C_PORT_HOST, 0);

	if (wedge_flag & (WEDGE_WRITE | WEDGE_READ))
		ccprintf("I2C bus %d is now wedged. Enjoy.\n", I2C_PORT_HOST);
	else
		ccprintf("Bit bang xfer complete.\n");

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(i2cwedge, command_i2c_wedge,
			"i2cwedge addr out_byte "
				"[wedge_flag [wedge_bit_count]]",
			"Wedge host I2C bus");

static int command_i2c_unwedge(int argc, char **argv)
{
	i2c_unwedge(I2C_PORT_HOST);

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(i2cunwedge, command_i2c_unwedge,
	"",
	"Unwedge host I2C bus");