summaryrefslogtreecommitdiff
path: root/chip/nrf51/i2c.c
blob: ff23152c897e4e5aeedb01e4d794ea99d64d1a66 (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
/* Copyright 2014 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 "clock.h"
#include "common.h"
#include "console.h"
#include "gpio.h"
#include "hooks.h"
#include "i2c.h"
#include "ppi.h"
#include "registers.h"
#include "task.h"
#include "timer.h"
#include "util.h"

/* Console output macros */
#define CPUTS(outstr) cputs(CC_I2C, outstr)
#define CPRINTF(format, args...) cprintf(CC_I2C, format, ## args)
#define CPRINTS(format, args...) cprints(CC_I2C, format, ## args)

#define I2C_TIMEOUT 20000

/* Keep track of the PPI channel used for each port */
static int i2c_ppi_chan[] = {-1, -1};

static void i2c_init_port(unsigned int port);

/* board-specific setup for post-I2C module init */
void __board_i2c_post_init(int port)
{
}

void board_i2c_post_init(int port)
		__attribute__((weak, alias("__board_i2c_post_init")));

static void i2c_init_port(unsigned int port)
{
	NRF51_TWI_RXDRDY(port) = 0;
	NRF51_TWI_TXDSENT(port) = 0;

	NRF51_TWI_PSELSCL(port) = NRF51_TWI_SCL_PIN(port);
	NRF51_TWI_PSELSDA(port) = NRF51_TWI_SDA_PIN(port);
	NRF51_TWI_FREQUENCY(port) = NRF51_TWI_FREQ(port);

	NRF51_PPI_CHENCLR = 1 << i2c_ppi_chan[port];

	NRF51_PPI_EEP(i2c_ppi_chan[port]) = (uint32_t)&NRF51_TWI_BB(port);
	NRF51_PPI_TEP(i2c_ppi_chan[port]) =
		(uint32_t)&NRF51_TWI_SUSPEND(port);

	/* Master enable */
	NRF51_TWI_ENABLE(port) = NRF51_TWI_ENABLE_VAL;

	if (!(i2c_raw_get_scl(port) && (i2c_raw_get_sda(port))))
		CPRINTF("port %d could be wedged\n", port);
}

void i2c_init(void)
{
	int i, rv;

	gpio_config_module(MODULE_I2C, 1);

	for (i = 0; i < i2c_ports_used; i++) {
		if (i2c_ppi_chan[i] == -1) {
			rv = ppi_request_channel(&i2c_ppi_chan[i]);
			ASSERT(rv == EC_SUCCESS);

			i2c_init_port(i);
		}
	}
}

static void dump_i2c_reg(int port)
{
#ifdef CONFIG_I2C_DEBUG
	CPRINTF("port      : %01d\n", port);
	CPRINTF("Regs :\n");
	CPRINTF(" 1: INTEN     : %08x\n", NRF51_TWI_INTEN(port));
	CPRINTF(" 2: ERRORSRC  : %08x\n", NRF51_TWI_ERRORSRC(port));
	CPRINTF(" 3: ENABLE    : %08x\n", NRF51_TWI_ENABLE(port));
	CPRINTF(" 4: PSELSCL   : %08x\n", NRF51_TWI_PSELSCL(port));
	CPRINTF(" 5: PSELSDA   : %08x\n", NRF51_TWI_PSELSDA(port));
	CPRINTF(" 6: RXD       : %08x\n", NRF51_TWI_RXD(port));
	CPRINTF(" 7: TXD       : %08x\n", NRF51_TWI_TXD(port));
	CPRINTF(" 8: FREQUENCY : %08x\n", NRF51_TWI_FREQUENCY(port));
	CPRINTF(" 9: ADDRESS   : %08x\n", NRF51_TWI_ADDRESS(port));
	CPRINTF("Events :\n");
	CPRINTF(" STOPPED   : %08x\n", NRF51_TWI_STOPPED(port));
	CPRINTF(" RXDRDY    : %08x\n", NRF51_TWI_RXDRDY(port));
	CPRINTF(" TXDSENT   : %08x\n", NRF51_TWI_TXDSENT(port));
	CPRINTF(" ERROR     : %08x\n", NRF51_TWI_ERROR(port));
	CPRINTF(" BB        : %08x\n", NRF51_TWI_BB(port));
#endif /* CONFIG_I2C_DEBUG */
}

static void i2c_recover(int port)
{
	/*
	 * Recovery of the TWI peripheral:
	 * To recover a TWI peripheral that has been locked up you must use
	 * the following code.
	 * After the recover function it is important to reconfigure all
	 * relevant TWI registers explicitly to ensure that it operates
	 * correctly.
	 *    TWI0:
	 *      NRF_TWI0->ENABLE =
	 *         TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
	 *      *(uint32_t *)(NRF_TWI0_BASE + 0xFFC) = 0;
	 *      nrf_delay_us(5);
	 *      *(uint32_t *)(NRF_TWI0_BASE + 0xFFC) = 1;
	 *      NRF_TWI0->ENABLE =
	 *         TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
	 */
	NRF51_TWI_ENABLE(port) = NRF51_TWI_DISABLE_VAL;
	NRF51_TWI_POWER(port) = 0;
	udelay(5);
	NRF51_TWI_POWER(port) = 1;

	i2c_init_port(port);
}

static void handle_i2c_error(int port, int rv)
{
	if (rv == EC_SUCCESS)
		return;

#ifdef CONFIG_I2C_DEBUG
	if (rv != EC_ERROR_TIMEOUT)
		CPRINTF("handle_i2c_error %d\n", rv);
	else
		CPRINTF("handle_i2c_error: Timeout\n");

	dump_i2c_reg(port);
#endif

	/* This may be a little too heavy handed. */
	i2c_recover(port);
}

static int i2c_master_write(const int port, const uint16_t slave_addr_flags,
			    const uint8_t *data, int size, int stop)
{
	int bytes_sent;
	int timeout = I2C_TIMEOUT;

	NRF51_TWI_ADDRESS(port) = I2C_STRIP_FLAGS(slave_addr_flags);

	/* Clear the sent bit */
	NRF51_TWI_TXDSENT(port) = 0;

	for (bytes_sent = 0; bytes_sent < size; bytes_sent++) {
		/*Send a byte */
		NRF51_TWI_TXD(port) = data[bytes_sent];

		/* Only send a start for the first byte */
		if (bytes_sent == 0)
			NRF51_TWI_STARTTX(port) = 1;

		/* Wait for ACK/NACK */
		timeout = I2C_TIMEOUT;
		while (timeout > 0 && NRF51_TWI_TXDSENT(port) == 0 &&
				NRF51_TWI_ERROR(port) == 0)
			timeout--;

		if (timeout == 0)
			return EC_ERROR_TIMEOUT;

		if (NRF51_TWI_ERROR(port))
			return EC_ERROR_UNKNOWN;

		/* Clear the sent bit */
		NRF51_TWI_TXDSENT(port) = 0;
	}

	if (stop) {
		NRF51_TWI_STOPPED(port) = 0;
		NRF51_TWI_STOP(port) = 1;
		timeout = 10;
		while (NRF51_TWI_STOPPED(port) == 0 && timeout > 0)
			timeout--;
	}

	return EC_SUCCESS;
}

static int i2c_master_read(const int port, const uint16_t slave_addr_flags,
			   uint8_t *data, int size)
{
	int curr_byte;
	int timeout = I2C_TIMEOUT;

	NRF51_TWI_ADDRESS(port) = I2C_STRIP_FLAGS(slave_addr_flags);

	if (size == 1) /* Last byte: stop after this one. */
		NRF51_PPI_TEP(i2c_ppi_chan[port]) =
			(uint32_t)&NRF51_TWI_STOP(port);
	else
		NRF51_PPI_TEP(i2c_ppi_chan[port]) =
			(uint32_t)&NRF51_TWI_SUSPEND(port);
	NRF51_PPI_CHENSET = 1 << i2c_ppi_chan[port];

	NRF51_TWI_RXDRDY(port) = 0;
	NRF51_TWI_STARTRX(port) = 1;

	for (curr_byte = 0; curr_byte < size; curr_byte++) {

		/* Wait for data */
		while (timeout > 0 && NRF51_TWI_RXDRDY(port) == 0 &&
				NRF51_TWI_ERROR(port) == 0)
			timeout--;

		if (timeout == 0)
			return EC_ERROR_TIMEOUT;

		if (NRF51_TWI_ERROR(port))
			return EC_ERROR_UNKNOWN;

		data[curr_byte] = NRF51_TWI_RXD(port);
		NRF51_TWI_RXDRDY(port) = 0;

		/* Second to the last byte: stop next time. */
		if (curr_byte == size-2)
			NRF51_PPI_TEP(i2c_ppi_chan[port]) =
				(uint32_t)&NRF51_TWI_STOP(port);

		/*
		 * According to nRF51822-PAN v2.4 (Product Anomaly Notice),
		 * the I2C locks up when RESUME is triggered too soon.
		 * "the firmware should ensure that the time between receiving
		 * the RXDRDY event and trigging the RESUME task is at least
		 * two times the TWI clock period (i.e. 20 μs at 100 kbps).
		 * Provided the TWI slave doesn’t do clock stretching during
		 * the ACK bit, this will be enough to avoid the RESUME task
		 * hit the end of the ACK bit. If this fails, a recovery of
		 * the peripheral will be necessary, see i2c_recover.
		 */
		udelay(20);
		NRF51_TWI_RESUME(port) = 1;
	}

	timeout = I2C_TIMEOUT;
	while (NRF51_TWI_STOPPED(port) == 0 && timeout > 0)
		timeout--;

	NRF51_TWI_STOP(port) = 0;

	NRF51_PPI_CHENCLR = 1 << i2c_ppi_chan[port];

	return EC_SUCCESS;
}

int chip_i2c_xfer(const int port, const uint16_t slave_addr_flags,
		  const uint8_t *out, int out_bytes,
		  uint8_t *in, int in_bytes, int flags)
{
	int rv = EC_SUCCESS;

	ASSERT(out || !out_bytes);
	ASSERT(in || !in_bytes);

	if (out_bytes)
		rv = i2c_master_write(port, slave_addr_flags,
				      out, out_bytes,
				      in_bytes ? 0 : 1);
	if (rv == EC_SUCCESS && in_bytes)
		rv = i2c_master_read(port, slave_addr_flags,
				     in, in_bytes);

	handle_i2c_error(port, rv);

	return rv;
}

int i2c_raw_get_scl(int port)
{
	enum gpio_signal g;

	if (get_scl_from_i2c_port(port, &g) == EC_SUCCESS)
		return gpio_get_level(g);

	/* If no SCL pin defined for this port, then return 1 to appear idle. */
	return 1;
}

int i2c_raw_get_sda(int port)
{
	enum gpio_signal g;

	if (get_sda_from_i2c_port(port, &g) == EC_SUCCESS)
		return gpio_get_level(g);

	/* If no SDA pin defined for this port, then return 1 to appear idle. */
	return 1;
}

int i2c_get_line_levels(int port)
{
	return (i2c_raw_get_sda(port) ? I2C_LINE_SDA_HIGH : 0) |
		(i2c_raw_get_scl(port) ? I2C_LINE_SCL_HIGH : 0);
}