summaryrefslogtreecommitdiff
path: root/common/i2c_bitbang.c
blob: 86d76a8b478d2d5e37f0f11189fb535694030408 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* 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 "console.h"
#include "gpio.h"
#include "i2c_bitbang.h"
#include "task.h"
#include "timer.h"
#include "util.h"

#define CPUTS(str) cputs(CC_I2C, str)

static int started;

/* TODO: respect i2c_port->kbps setting */
static void i2c_delay(void)
{
	udelay(5);
}

/* Number of attempts to unwedge each pin. */
#define UNWEDGE_SCL_ATTEMPTS  10
#define UNWEDGE_SDA_ATTEMPTS  3

static void i2c_bitbang_unwedge(const struct i2c_port_t *i2c_port)
{
	int i, j;

	gpio_set_level(i2c_port->scl, 1);
	/*
	 * If clock is low, wait for a while in case of clock stretched
	 * by a peripheral.
	 */
	if (!gpio_get_level(i2c_port->scl)) {
		for (i = 0;; i++) {
			if (i >= UNWEDGE_SCL_ATTEMPTS) {
				/*
				 * If we get here, a peripheral is holding the
				 * clock low and there is nothing we can do.
				 */
				CPUTS("I2C unwedge failed, SCL is held low\n");
				return;
			}
			i2c_delay();
			if (gpio_get_level(i2c_port->scl))
				break;
		}
	}

	if (gpio_get_level(i2c_port->sda))
		return;

	CPUTS("I2C unwedge called with SDA held low\n");

	/* Keep trying to unwedge the SDA line until we run out of attempts. */
	for (i = 0; i < UNWEDGE_SDA_ATTEMPTS; i++) {
		/* Drive the clock high. */
		gpio_set_level(i2c_port->scl, 0);
		i2c_delay();

		/*
		 * Clock through the problem by clocking out 9 bits. If
		 * peripheral releases the SDA line, then we can stop clocking
		 * bits and send a STOP.
		 */
		for (j = 0; j < 9; j++) {
			if (gpio_get_level(i2c_port->sda))
				break;

			gpio_set_level(i2c_port->scl, 0);
			i2c_delay();
			gpio_set_level(i2c_port->scl, 1);
			i2c_delay();
		}

		/* Take control of SDA line and issue a STOP command. */
		gpio_set_level(i2c_port->sda, 0);
		i2c_delay();
		gpio_set_level(i2c_port->sda, 1);
		i2c_delay();

		/* Check if the bus is unwedged. */
		if (gpio_get_level(i2c_port->sda) &&
				gpio_get_level(i2c_port->scl))
			break;
	}

	if (!gpio_get_level(i2c_port->sda))
		CPUTS("I2C unwedge failed, SDA still low\n");
	if (!gpio_get_level(i2c_port->scl))
		CPUTS("I2C unwedge failed, SCL still low\n");
}

static void i2c_stop_cond(const struct i2c_port_t *i2c_port)
{
	int i;

	if (!started)
		return;

	gpio_set_level(i2c_port->sda, 0);
	i2c_delay();

	gpio_set_level(i2c_port->scl, 1);

	/*
	 * SMBus 3.0, 4.2.5
	 *
	 *  the recommendation is that if SMBDAT is still low tTIMEOUT,MAX after
	 *  SMBCLK has gone high at the end of a transaction the controller
	 *  should hold SMBCLK low for at least tTIMEOUT,MAX in an attempt to
	 *  reset the SMBus interface of all of the devices on the bus.
	 */
	for (i = 0; i < 7000; i++) {
		if (gpio_get_level(i2c_port->scl))
			break;
		i2c_delay();
	}
	i2c_delay();

	/* SCL is high, set SDA from 0 to 1 */
	gpio_set_level(i2c_port->sda, 1);
	i2c_delay();

	started = 0;
}

static int clock_stretching(const struct i2c_port_t *i2c_port)
{
	int i;

	i2c_delay();
	/* 5us * 7000 iterations ~= 35ms */
	for (i = 0; i < 7000; i++) {
		if (gpio_get_level(i2c_port->scl))
			return 0;
		i2c_delay();
	}

	/*
	 * SMBus 3.0, Note 3
	 * Devices participating in a transfer can abort the transfer in
	 * progress and release the bus when any single clock low interval
	 * exceeds the value of tTIMEOUT,MIN(=25ms).
	 * After the controller in a transaction detects this condition, it must
	 * generate a stop condition within or after the current data byte in
	 * the transfer process.
	 */
	i2c_stop_cond(i2c_port);
	CPUTS("clock low timeout\n");

	return EC_ERROR_TIMEOUT;
}

static int i2c_start_cond(const struct i2c_port_t *i2c_port)
{
	int err;

	if (started) {
		gpio_set_level(i2c_port->sda, 1);
		i2c_delay();

		gpio_set_level(i2c_port->scl, 1);
		err = clock_stretching(i2c_port);
		if (err)
			return err;
		i2c_delay();

		if (gpio_get_level(i2c_port->sda) == 0) {
			CPUTS("start_cond: arbitration lost\n");
			started = 0;
			return EC_ERROR_UNKNOWN;
		}
	}

	/* check if bus is idle before starting */
	if (gpio_get_level(i2c_port->scl) == 0 ||
	    gpio_get_level(i2c_port->sda) == 0)
		return EC_ERROR_UNKNOWN;

	gpio_set_level(i2c_port->sda, 0);
	i2c_delay();

	gpio_set_level(i2c_port->scl, 0);
	started = 1;

	return 0;
}

static int i2c_write_bit(const struct i2c_port_t *i2c_port, int bit)
{
	int err;

	gpio_set_level(i2c_port->sda, !!bit);
	i2c_delay();

	gpio_set_level(i2c_port->scl, 1);
	err = clock_stretching(i2c_port);
	if (err)
		return err;
	i2c_delay();

	if (bit && gpio_get_level(i2c_port->sda) == 0) {
		CPUTS("write_bit: arbitration lost\n");
		started = 0;
		return EC_ERROR_UNKNOWN;
	}

	gpio_set_level(i2c_port->scl, 0);

	return 0;
}

static int i2c_read_bit(const struct i2c_port_t *i2c_port, int *bit)
{
	int err;

	gpio_set_level(i2c_port->sda, 1);
	i2c_delay();

	gpio_set_level(i2c_port->scl, 1);
	err = clock_stretching(i2c_port);
	if (err)
		return err;
	i2c_delay();
	*bit = gpio_get_level(i2c_port->sda);

	gpio_set_level(i2c_port->scl, 0);

	return 0;
}

static int i2c_write_byte(const struct i2c_port_t *i2c_port, uint8_t byte)
{
	int i, nack, err;

	for (i = 7; i >= 0; i--) {
		err = i2c_write_bit(i2c_port, byte & (1 << i));
		if (err)
			return err;
	}

	err = i2c_read_bit(i2c_port, &nack);
	if (err)
		return err;

	if (nack) {
		/*
		 * The peripheral device detects an invalid command or invalid
		 * data. In this case the peripheral device must NACK the
		 * received byte. The controller upon detection of this
		 * condition must generate a STOP condition and retry the
		 * transaction
		 */
		i2c_stop_cond(i2c_port);
		/* return EC_ERROR_BUSY to indicate i2c_xfer() to retry */
		return EC_ERROR_BUSY;
	}
	return 0;
}

static int i2c_read_byte(const struct i2c_port_t *i2c_port, uint8_t *byte,
		int nack)
{
	int i;

	*byte = 0;
	for (i = 0; i < 8; i++) {
		int bit = 0, err;

		err = i2c_read_bit(i2c_port, &bit);
		if (err)
			return err;
		*byte = (*byte << 1) | bit;
	}

	return i2c_write_bit(i2c_port, nack);
}

static int i2c_bitbang_xfer(const struct i2c_port_t *i2c_port,
		const uint16_t addr_flags,
		const uint8_t *out, int out_size,
		uint8_t *in, int in_size, int flags)
{
	uint16_t addr_8bit = addr_flags << 1, err = EC_SUCCESS;
	int i = 0;

	if (i2c_port->kbps != 100)
		CPUTS("warning: bitbang driver only supports 100kbps\n");

	if (out_size) {
		if (flags & I2C_XFER_START) {
			err = i2c_start_cond(i2c_port);
			if (err)
				goto exit;
			err = i2c_write_byte(i2c_port, addr_8bit);
			if (err)
				goto exit;
		}

		for (i = 0; i < out_size; i++) {
			err = i2c_write_byte(i2c_port, out[i]);
			if (err)
				goto exit;
		}
	}

	if (in_size) {
		if (flags & I2C_XFER_START) {
			err = i2c_start_cond(i2c_port);
			if (err)
				goto exit;
			err = i2c_write_byte(i2c_port, addr_8bit | 1);
			if (err)
				goto exit;
		}

		for (i = 0; i < in_size; i++) {
			err = i2c_read_byte(i2c_port, &in[i],
				(flags & I2C_XFER_STOP) && (i == in_size - 1));
			if (err)
				goto exit;
		}
	}

	if (flags & I2C_XFER_STOP)
		i2c_stop_cond(i2c_port);

exit:
	if (err) {
		i2c_bitbang_unwedge(i2c_port);
		started = 0;
	}
	return err;
}

const struct i2c_drv bitbang_drv = {
	.xfer = &i2c_bitbang_xfer
};

#ifdef TEST_BUILD
int bitbang_start_cond(const struct i2c_port_t *i2c_port)
{
	return i2c_start_cond(i2c_port);
}

void bitbang_stop_cond(const struct i2c_port_t *i2c_port)
{
	i2c_stop_cond(i2c_port);
}

int bitbang_write_byte(const struct i2c_port_t *i2c_port, uint8_t byte)
{
	return i2c_write_byte(i2c_port, byte);
}

void bitbang_set_started(int val)
{
	started = val;
}
#endif