Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*	$NetBSD: bcm2835_bsc.c,v 1.15 2020/03/31 12:23:17 jmcneill Exp $	*/

/*
 * Copyright (c) 2019 Jason R. Thorpe
 * Copyright (c) 2012 Jonathan A. Kollasch
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: bcm2835_bsc.c,v 1.15 2020/03/31 12:23:17 jmcneill Exp $");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/kernhist.h>
#include <sys/intr.h>
#include <sys/mutex.h>
#include <sys/systm.h>

#include <dev/i2c/i2cvar.h>

#include <arm/broadcom/bcm2835reg.h>
#include <arm/broadcom/bcm2835_bscreg.h>
#include <arm/broadcom/bcm2835_bscvar.h>

static void	bsciic_exec_func_idle(struct bsciic_softc * const);
static void	bsciic_exec_func_send_addr(struct bsciic_softc * const);
static void	bsciic_exec_func_send_cmd(struct bsciic_softc * const);
static void	bsciic_exec_func_send_data(struct bsciic_softc * const);
static void	bsciic_exec_func_recv_data(struct bsciic_softc * const);
static void	bsciic_exec_func_done(struct bsciic_softc * const);
static void	bsciic_exec_func_error(struct bsciic_softc * const);

const struct {
	void			(*func)(struct bsciic_softc * const);
	uint32_t		c_bits;
	uint32_t		s_bits;
} bsciic_exec_state_data[] = {
	[BSC_EXEC_STATE_IDLE] = {
		.func = bsciic_exec_func_idle,
	},
	[BSC_EXEC_STATE_SEND_ADDR] = {
		.func = bsciic_exec_func_send_addr,
	},
	[BSC_EXEC_STATE_SEND_CMD] = {
		.func = bsciic_exec_func_send_cmd,
		.c_bits = BSC_C_INTT,
		.s_bits = BSC_S_TXW,
	},
	[BSC_EXEC_STATE_SEND_DATA] = {
		.func = bsciic_exec_func_send_data,
		.c_bits = BSC_C_INTT,
		.s_bits = BSC_S_TXW,
	},
	[BSC_EXEC_STATE_RECV_DATA] = {
		.func = bsciic_exec_func_recv_data,
		.c_bits = BSC_C_READ | BSC_C_INTR,
		.s_bits = BSC_S_RXR,
	},
	[BSC_EXEC_STATE_DONE] = {
		.func = bsciic_exec_func_done,
	},
	[BSC_EXEC_STATE_ERROR] = {
		.func = bsciic_exec_func_error,
	},
};

int	bsciic_debug = 0;

void
bsciic_attach(struct bsciic_softc *sc)
{
	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
	cv_init(&sc->sc_intr_wait, device_xname(sc->sc_dev));

	/* clear FIFO, disable controller */
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
	    BSC_S_ERR | BSC_S_DONE);
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, 0);

	u_int divider = howmany(sc->sc_frequency, sc->sc_clkrate);
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DIV,
	   __SHIFTIN(divider, BSC_DIV_CDIV));
}

int
bsciic_acquire_bus(void *v, int flags)
{
	struct bsciic_softc * const sc = v;
	uint32_t s __diagused;

	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
	    BSC_S_ERR | BSC_S_DONE);
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_I2CEN |
	    BSC_C_CLEAR_CLEAR);
	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
	KASSERT((s & BSC_S_TA) == 0);

	return 0;
}

void
bsciic_release_bus(void *v, int flags)
{
	struct bsciic_softc * const sc = v;

	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
}

static void
bsciic_exec_lock(struct bsciic_softc * const sc)
{
	if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
		mutex_enter(&sc->sc_intr_lock);
	}
}

static void
bsciic_exec_unlock(struct bsciic_softc * const sc)
{
	if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
		mutex_exit(&sc->sc_intr_lock);
	}
}

static void
bsciic_txfill(struct bsciic_softc * const sc)
{
	uint32_t s;

	while (sc->sc_bufpos != sc->sc_buflen) {
		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
		if ((s & BSC_S_TXD) == 0)
			break;
		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO,
		    sc->sc_buf[sc->sc_bufpos++]);
	}
}

static void
bsciic_rxdrain(struct bsciic_softc * const sc)
{
	uint32_t s;

	while (sc->sc_bufpos != sc->sc_buflen) {
		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
		if ((s & BSC_S_RXD) == 0)
			break;
		sc->sc_buf[sc->sc_bufpos++] =
		    (uint8_t)bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO);
	}
}

static bsc_exec_state_t
bsciic_next_state(struct bsciic_softc * const sc)
{
	switch (sc->sc_exec_state) {
	case BSC_EXEC_STATE_IDLE:
		if (sc->sc_exec.addr > 0x7f) {
			return BSC_EXEC_STATE_SEND_ADDR;
		}
		/* FALLTHROUGH */

	case BSC_EXEC_STATE_SEND_ADDR:
		if (sc->sc_exec.cmdlen) {
			return BSC_EXEC_STATE_SEND_CMD;
		}
		/* FALLTHROUGH */

	case BSC_EXEC_STATE_SEND_CMD:
		if (sc->sc_exec.datalen == 0) {
			return BSC_EXEC_STATE_DONE;
		}

		if (I2C_OP_READ_P(sc->sc_exec.op)) {
			return BSC_EXEC_STATE_RECV_DATA;
		}

		return BSC_EXEC_STATE_SEND_DATA;

	case BSC_EXEC_STATE_SEND_DATA:
	case BSC_EXEC_STATE_RECV_DATA:
		return BSC_EXEC_STATE_DONE;
	
	case BSC_EXEC_STATE_DONE:
	case BSC_EXEC_STATE_ERROR:
		return sc->sc_exec_state;
	}

	panic("bsciic_next_state: invalid state: %d", sc->sc_exec_state);
}

#define	BSC_EXEC_PHASE_COMPLETE(sc)				\
	((sc)->sc_exec_state == BSC_EXEC_STATE_ERROR ||		\
	 (sc)->sc_bufpos == (sc)->sc_buflen)

static void
bsciic_signal(struct bsciic_softc * const sc)
{
	if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
		cv_signal(&sc->sc_intr_wait);
	}
}

static void
bsciic_phase_done(struct bsciic_softc * const sc)
{
	sc->sc_exec_state = bsciic_next_state(sc);
	(*bsciic_exec_state_data[sc->sc_exec_state].func)(sc);
}

static void
bsciic_abort(struct bsciic_softc * const sc)
{
	sc->sc_exec_state = BSC_EXEC_STATE_ERROR;
	bsciic_phase_done(sc);
}

int
bsciic_intr(void *v)
{
	struct bsciic_softc * const sc = v;
	uint32_t s;

	bsciic_exec_lock(sc);

	if ((sc->sc_exec.flags & I2C_F_POLL) == 0 &&
	    sc->sc_expecting_interrupt == false) {
		bsciic_exec_unlock(sc);
		return 0;
	}

	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S,
	    BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);

	if (s & (BSC_S_CLKT | BSC_S_ERR)) {
		/*
		 * ERR might be a normal "probing for device" sort
		 * of thing, so don't complain about that one.
		 * Do complain about CLKT, though.
		 */
		if ((s & BSC_S_CLKT) ||
		    (bsciic_debug && (s & BSC_S_ERR))) {
			device_printf(sc->sc_dev,
			    "error s=0x%08x, aborting transfer\n", s);
		}
		bsciic_abort(sc);
		goto out;
	}

	if (BSC_EXEC_STATE_SENDING(sc)) {
		/*
		 * When transmitting, we need to wait for one final
		 * interrupt after pushing out the last of our data.
		 * Catch that case here and go to the next state.
		 */
		if (BSC_EXEC_PHASE_COMPLETE(sc)) {
			bsciic_phase_done(sc);
		} else {
			bsciic_txfill(sc);
		}
	} else if (BSC_EXEC_STATE_RECEIVING(sc)) {
		bsciic_rxdrain(sc);
		/*
		 * If we've received all of the data, go to the next
		 * state now; we might not get another interrupt.
		 */
		if (BSC_EXEC_PHASE_COMPLETE(sc)) {
			bsciic_phase_done(sc);
		}
	} else {
		device_printf(sc->sc_dev,
		    "unexpected interrupt: state=%d s=0x%08x\n",
		    sc->sc_exec_state, s);
		bsciic_abort(sc);
	}

 out:
	bsciic_exec_unlock(sc);
	return (1);
}

static void
bsciic_wait(struct bsciic_softc * const sc, const uint32_t events)
{
	if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
		return;
	}

	const uint32_t s_bits =
	    events | BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE;
	uint32_t s;

	/* sc_intr_lock is not held in this case. */

	for (;;) {
		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
		if (s & s_bits) {
			(void) bsciic_intr(sc);
		}
		if (BSC_EXEC_PHASE_COMPLETE(sc)) {
			bsciic_phase_done(sc);
		}
		if (sc->sc_exec_state >= BSC_EXEC_STATE_DONE) {
			return;
		}
		delay(1);
	}
}

static void
bsciic_start(struct bsciic_softc * const sc)
{

	sc->sc_c_bits = BSC_C_I2CEN | BSC_C_INTD |
	    bsciic_exec_state_data[sc->sc_exec_state].c_bits;

	/* Clear the interrupt-enable bits if we're polling. */
	if (sc->sc_exec.flags & I2C_F_POLL) {
		sc->sc_c_bits &= ~(BSC_C_INTD | BSC_C_INTT | BSC_C_INTR);
	}

	sc->sc_expecting_interrupt =
	    (sc->sc_c_bits & (BSC_C_INTD | BSC_C_INTT | BSC_C_INTR)) ? true
								     : false;

	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C,
	    sc->sc_c_bits | BSC_C_ST);
	bsciic_wait(sc, bsciic_exec_state_data[sc->sc_exec_state].s_bits);
}

static void
bsciic_exec_func_idle(struct bsciic_softc * const sc)
{
	/* We kick off a transfer by setting the slave address register. */
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_A, sc->sc_exec.addr);

	/* Immediately transition to the next state. */
	bsciic_phase_done(sc);
}

static void
bsciic_exec_func_send_addr(struct bsciic_softc * const sc)
{
	/* XXX For 10-bit addressing; not implemented yet. */
	panic("bsciic_exec_func_send_addr is not supposed to be called");
}

static void
bsciic_exec_func_send_cmd(struct bsciic_softc * const sc)
{
	sc->sc_buf = __UNCONST(sc->sc_exec.cmdbuf);
	sc->sc_bufpos = 0;
	sc->sc_buflen = sc->sc_exec.cmdlen;

	uint32_t dlen = sc->sc_exec.cmdlen;
	if (! I2C_OP_READ_P(sc->sc_exec.op)) {
		dlen += sc->sc_exec.datalen;
	}
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);

	bsciic_start(sc);
}

static void
bsciic_exec_func_send_data(struct bsciic_softc * const sc)
{
	sc->sc_buf = sc->sc_exec.databuf;
	sc->sc_bufpos = 0;
	sc->sc_buflen = sc->sc_exec.datalen;

	if (sc->sc_exec.cmdlen) {
		/*
		 * Output has already been started in this case; we just
		 * needed to switch buffers.
		 */
		bsciic_wait(sc, BSC_S_TXW);
	} else {
		uint32_t dlen = sc->sc_exec.datalen;
		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
		bsciic_start(sc);
	}
}

static void
bsciic_exec_func_recv_data(struct bsciic_softc * const sc)
{
	sc->sc_buf = sc->sc_exec.databuf;
	sc->sc_bufpos = 0;
	sc->sc_buflen = sc->sc_exec.datalen;

	uint32_t dlen = sc->sc_exec.datalen;
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);

	bsciic_start(sc);
}

static void
bsciic_exec_func_done(struct bsciic_softc * const sc)
{
	/* We're done!  Disable interrupts. */
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_I2CEN);
	sc->sc_expecting_interrupt = false;
	bsciic_signal(sc);
}

static void
bsciic_exec_func_error(struct bsciic_softc * const sc)
{
	/* Clear the FIFO and disable interrupts. */
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C,
	    BSC_C_I2CEN | BSC_C_CLEAR_CLEAR);
	sc->sc_expecting_interrupt = false;
	bsciic_signal(sc);
}

int
bsciic_exec(void *v, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
    size_t cmdlen, void *databuf, size_t datalen, int flags)
{
	struct bsciic_softc * const sc = v;

	/* XXX We don't do 10-bit addressing correctly yet. */
	if (addr > 0x7f)
		return (ENOTSUP);

	/*
	 * The I2C middle layer has ensured that the client device has
	 * exclusive access to the controller.  Copy the parameters
	 * and start the state machine that runs through the necessary
	 * phases of the request.
	 */
	KASSERT(sc->sc_exec_state == BSC_EXEC_STATE_IDLE);
	sc->sc_exec.op = op;
	sc->sc_exec.addr = addr;
	sc->sc_exec.cmdbuf = cmdbuf;
	sc->sc_exec.cmdlen = cmdlen;
	sc->sc_exec.databuf = databuf;
	sc->sc_exec.datalen = datalen;
	sc->sc_exec.flags = flags;

	bsciic_exec_lock(sc);
	(*bsciic_exec_state_data[sc->sc_exec_state].func)(sc);
	while (sc->sc_exec_state < BSC_EXEC_STATE_DONE) {
		KASSERT((flags & I2C_F_POLL) == 0);
		cv_wait(&sc->sc_intr_wait, &sc->sc_intr_lock);
	}
	int error = sc->sc_exec_state == BSC_EXEC_STATE_ERROR ? EIO : 0;
	uint32_t s;
	do {
		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
	} while ((s & BSC_S_TA) != 0);
	if (s & (BSC_S_CLKT | BSC_S_ERR)) {
		error = EIO;
	}
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C,
	    BSC_C_I2CEN | BSC_C_CLEAR_CLEAR);
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S,
	    BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, 0);
	bsciic_exec_unlock(sc);

	sc->sc_exec.flags = 0;
	sc->sc_exec_state = BSC_EXEC_STATE_IDLE;
	memset(&sc->sc_exec, 0, sizeof(sc->sc_exec));

	return error;
}