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
/* $NetBSD: tegra_cec.c,v 1.11 2021/08/07 16:18:44 thorpej Exp $ */

/*-
 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
 * 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 AUTHOR ``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 AUTHOR 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: tegra_cec.c,v 1.11 2021/08/07 16:18:44 thorpej Exp $");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/intr.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/poll.h>
#include <sys/select.h>

#include <dev/hdmicec/hdmicecio.h>
#include <dev/hdmicec/hdmicec_if.h>

#include <arm/nvidia/tegra_var.h>
#include <arm/nvidia/tegra_pmcreg.h>
#include <arm/nvidia/tegra_cecreg.h>

#include <dev/fdt/fdtvar.h>

#define CEC_VENDORID_NVIDIA	0x00044b

static int	tegra_cec_match(device_t, cfdata_t, void *);
static void	tegra_cec_attach(device_t, device_t, void *);

static int	tegra_cec_intr(void *);

struct tegra_cec_softc {
	device_t		sc_dev;
	bus_space_tag_t		sc_bst;
	bus_space_handle_t	sc_bsh;
	void			*sc_ih;
	struct clk		*sc_clk;
	struct fdtbus_reset	*sc_rst;

	kmutex_t		sc_lock;
	kcondvar_t		sc_cv;

	const char		*sc_hdmidevname;
	device_t		sc_cecdev;

	struct selinfo		sc_selinfo;

	uint8_t			sc_rxbuf[16];
	int			sc_rxlen;
	bool			sc_rxdone;

	uint8_t			sc_txbuf[16];
	int			sc_txlen;
	int			sc_txcur;
	int			sc_txerr;
	bool			sc_txdone;
};

static void	tegra_cec_reset(struct tegra_cec_softc *);

static int	tegra_cec_open(void *, int);
static void	tegra_cec_close(void *);
static int	tegra_cec_ioctl(void *, u_long, void *, int, lwp_t *);
static int	tegra_cec_send(void *, const uint8_t *, size_t);
static ssize_t	tegra_cec_recv(void *, uint8_t *, size_t);
static int	tegra_cec_poll(void *, int, lwp_t *);

static const struct hdmicec_hw_if tegra_cec_hw_if = {
	.open = tegra_cec_open,
	.close = tegra_cec_close,
	.ioctl = tegra_cec_ioctl,
	.send = tegra_cec_send,
	.recv = tegra_cec_recv,
	.poll = tegra_cec_poll,
};

CFATTACH_DECL_NEW(tegra_cec, sizeof(struct tegra_cec_softc),
	tegra_cec_match, tegra_cec_attach, NULL, NULL);

#define CEC_READ(sc, reg)			\
    bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
#define CEC_WRITE(sc, reg, val)			\
    bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
#define CEC_SET_CLEAR(sc, reg, set, clr)	\
    tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (set), (clr))

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "nvidia,tegra124-cec" },
	DEVICE_COMPAT_EOL
};

static int
tegra_cec_match(device_t parent, cfdata_t cf, void *aux)
{
	struct fdt_attach_args * const faa = aux;

	return of_compatible_match(faa->faa_phandle, compat_data);
}

static void
tegra_cec_attach(device_t parent, device_t self, void *aux)
{
	struct tegra_cec_softc * const sc = device_private(self);
	struct fdt_attach_args * const faa = aux;
	prop_dictionary_t prop = device_properties(self);
	struct hdmicec_attach_args caa;
	char intrstr[128];
	bus_addr_t addr;
	bus_size_t size;
	int error;

	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
		aprint_error(": couldn't get registers\n");
		return;
	}
	sc->sc_clk = fdtbus_clock_get(faa->faa_phandle, "cec");
	if (sc->sc_clk == NULL) {
		aprint_error(": couldn't get clock cec\n");
		return;
	}
	sc->sc_rst = fdtbus_reset_get(faa->faa_phandle, "cec");
	if (sc->sc_rst == NULL) {
		aprint_error(": couldn't get reset cec\n");
		return;
	}

	sc->sc_dev = self;
	sc->sc_bst = faa->faa_bst;
	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
	if (error) {
		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error);
		return;
	}
	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
	cv_init(&sc->sc_cv, "tegracec");
	selinit(&sc->sc_selinfo);

	aprint_naive("\n");
	aprint_normal(": HDMI CEC\n");

	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
		aprint_error_dev(self, "failed to decode interrupt\n");
		return;
	}

	sc->sc_ih = fdtbus_intr_establish_xname(faa->faa_phandle, 0, IPL_VM,
	    FDT_INTR_MPSAFE, tegra_cec_intr, sc, device_xname(self));
	if (sc->sc_ih == NULL) {
		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
		    intrstr);
		return;
	}
	aprint_normal_dev(self, "interrupting on %s\n", intrstr);

	prop_dictionary_get_string(prop, "hdmi-device",
	    &sc->sc_hdmidevname);

	fdtbus_reset_assert(sc->sc_rst);
	error = clk_enable(sc->sc_clk);
	if (error) {
		aprint_error_dev(self, "couldn't enable cec: %d\n", error);
		return;
	}
	fdtbus_reset_deassert(sc->sc_rst);

	CEC_WRITE(sc, CEC_SW_CONTROL_REG, 0);
	CEC_WRITE(sc, CEC_INPUT_FILTER_REG, 0);
	CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
	CEC_WRITE(sc, CEC_INT_MASK_REG, 0);
	CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);

	memset(&caa, 0, sizeof(caa));
	caa.priv = sc;
	caa.hwif = &tegra_cec_hw_if;
	sc->sc_cecdev = config_found(self, &caa, NULL, CFARGS_NONE);
}

static int
tegra_cec_intr(void *priv)
{
	struct tegra_cec_softc * const sc = priv;
	uint32_t val;
	int handled = 0;

	mutex_enter(&sc->sc_lock);
	const uint32_t int_stat = CEC_READ(sc, CEC_INT_STAT_REG);

	if (int_stat & CEC_INT_RX_REGISTER_FULL) {
		val = CEC_READ(sc, CEC_RX_REGISTER_REG);
		sc->sc_rxbuf[sc->sc_rxlen++] =
		    __SHIFTOUT(val, CEC_RX_REGISTER_DATA);
		if ((val & CEC_RX_REGISTER_EOM) != 0 ||
		    sc->sc_rxlen == 16) {
			CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
			    CEC_INT_RX_REGISTER_FULL);
			sc->sc_rxdone = true;
			cv_broadcast(&sc->sc_cv);
			selnotify(&sc->sc_selinfo, POLLIN|POLLRDNORM,
			    NOTE_SUBMIT);
		}
		CEC_WRITE(sc, CEC_INT_STAT_REG, CEC_INT_RX_REGISTER_FULL);
		++handled;
	}

	if (int_stat & CEC_INT_TX_REGISTER_EMPTY) {
		if (sc->sc_txcur < sc->sc_txlen) {
			const uint8_t destination = sc->sc_txbuf[0] & 0xf;
			val = __SHIFTIN(sc->sc_txbuf[sc->sc_txcur],
			    CEC_TX_REGISTER_DATA);
			if (sc->sc_txcur == 0)
				val |= CEC_TX_REGISTER_GENERATE_START_BIT;
			if (sc->sc_txcur == sc->sc_txlen - 1)
				val |= CEC_TX_REGISTER_EOM;
			if (destination == 0xf)
				val |= CEC_TX_REGISTER_ADDRESS_MODE;

			CEC_WRITE(sc, CEC_TX_REGISTER_REG, val);
			CEC_WRITE(sc, CEC_INT_STAT_REG,
			    CEC_INT_TX_REGISTER_EMPTY);
			++sc->sc_txcur;
		} else {
			CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
			    CEC_INT_TX_REGISTER_EMPTY);
		}
		++handled;
	}

	if (int_stat & CEC_INT_TX_FRAME_TRANSMITTED) {
		CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
		    CEC_INT_TX_FRAME_TRANSMITTED |
		    CEC_INT_TX_FRAME_OR_BLOCK_NAKD);
		CEC_WRITE(sc, CEC_INT_STAT_REG, CEC_INT_TX_FRAME_TRANSMITTED);
		if (int_stat & CEC_INT_TX_FRAME_OR_BLOCK_NAKD) {
			CEC_WRITE(sc, CEC_INT_STAT_REG,
			    CEC_INT_TX_FRAME_OR_BLOCK_NAKD);
			sc->sc_txerr = ECONNREFUSED;
			tegra_cec_reset(sc);
		}
		sc->sc_txdone = true;
		cv_broadcast(&sc->sc_cv);
		++handled;
	}

	if (int_stat & CEC_INT_TX_REGISTER_UNDERRUN) {
		tegra_cec_reset(sc);
		cv_broadcast(&sc->sc_cv);
		++handled;
	}

	mutex_exit(&sc->sc_lock);

	return handled;
}

static void
tegra_cec_reset(struct tegra_cec_softc *sc)
{
	uint32_t val;

	KASSERT(mutex_owned(&sc->sc_lock));

	val = CEC_READ(sc, CEC_HW_CONTROL_REG);
	CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
	CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);
	CEC_WRITE(sc, CEC_HW_CONTROL_REG, val);
}

static int
tegra_cec_open(void *priv, int flag)
{
	struct tegra_cec_softc * const sc = priv;

	mutex_enter(&sc->sc_lock);
	sc->sc_rxlen = 0;
	sc->sc_rxdone = false;
	CEC_WRITE(sc, CEC_INT_MASK_REG, CEC_INT_RX_REGISTER_FULL);
	CEC_WRITE(sc, CEC_HW_CONTROL_REG, CEC_HW_CONTROL_TX_RX_MODE);
	mutex_exit(&sc->sc_lock);

	return 0;
}

static void
tegra_cec_close(void *priv)
{
	struct tegra_cec_softc * const sc = priv;

	mutex_enter(&sc->sc_lock);
	CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
	CEC_WRITE(sc, CEC_INT_MASK_REG, 0);
	CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);
	mutex_exit(&sc->sc_lock);
}

static int
tegra_cec_get_phys_addr(struct tegra_cec_softc *sc, uint16_t *phys_addr)
{
	device_t hdmidev;

	if (sc->sc_hdmidevname == NULL)
		return EIO;
	hdmidev = device_find_by_xname(sc->sc_hdmidevname);
	if (hdmidev == NULL)
		return ENXIO;

	const prop_dictionary_t prop = device_properties(hdmidev);
	if (!prop_dictionary_get_uint16(prop, "physical-address", phys_addr))
		return ENOTCONN;

	return 0;
}

static int
tegra_cec_ioctl(void *priv, u_long cmd, void *data, int flag, lwp_t *l)
{
	struct tegra_cec_softc * const sc = priv;
	uint32_t val;

	switch (cmd) {
	case CEC_GET_PHYS_ADDR:
		return tegra_cec_get_phys_addr(sc, data);
	case CEC_GET_LOG_ADDRS:
		val = CEC_READ(sc, CEC_HW_CONTROL_REG);
		*(uint16_t *)data =
		    __SHIFTOUT(val, CEC_HW_CONTROL_RX_LOGICAL_ADDRS);
		return 0;
	case CEC_SET_LOG_ADDRS:
		val = *(uint16_t *)data & 0x7fff;
		CEC_SET_CLEAR(sc, CEC_HW_CONTROL_REG,
		    __SHIFTIN(val, CEC_HW_CONTROL_RX_LOGICAL_ADDRS),
		    CEC_HW_CONTROL_RX_LOGICAL_ADDRS);
		return 0;
	case CEC_GET_VENDOR_ID:
		*(uint32_t *)data = CEC_VENDORID_NVIDIA;
		return 0;
	default:
		return EINVAL;
	}
}

static int
tegra_cec_send(void *priv, const uint8_t *data, size_t len)
{
	struct tegra_cec_softc * const sc = priv;
	int error = 0;

	mutex_enter(&sc->sc_lock);

	sc->sc_txdone = false;
	sc->sc_txcur = 0;
	sc->sc_txerr = 0;
	memcpy(sc->sc_txbuf, data, len);
	sc->sc_txlen = len;

	CEC_SET_CLEAR(sc, CEC_INT_MASK_REG,
	    CEC_INT_TX_REGISTER_EMPTY |
	    CEC_INT_TX_FRAME_TRANSMITTED |
	    CEC_INT_TX_FRAME_OR_BLOCK_NAKD, 0);

	while (sc->sc_txdone == false) {
		error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock, hz);
		if (error)
			break;
	}

	if (sc->sc_txdone)
		error = sc->sc_txerr;

	mutex_exit(&sc->sc_lock);

	return error;
}

static ssize_t
tegra_cec_recv(void *priv, uint8_t *data, size_t len)
{
	struct tegra_cec_softc * const sc = priv;
	ssize_t alen = -1;
	int error = 0;

	mutex_enter(&sc->sc_lock);

	while (sc->sc_rxdone == false) {
		error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock, hz);
		if (error)
			break;
	}

	if (sc->sc_rxdone) {
		memcpy(data, sc->sc_rxbuf, sc->sc_rxlen);
		alen = sc->sc_rxlen;
		sc->sc_rxlen = 0;
		sc->sc_rxdone = false;
	}

	mutex_exit(&sc->sc_lock);

	return alen;
}

static int
tegra_cec_poll(void *priv, int events, lwp_t *l)
{
	struct tegra_cec_softc * const sc = priv;
	int revents;

	revents = events & (POLLOUT | POLLWRNORM);

	if ((events & (POLLIN | POLLRDNORM)) == 0)
		return revents;

	mutex_enter(&sc->sc_lock);
	if (sc->sc_rxdone) {
		revents = (events & (POLLIN | POLLRDNORM));
	} else {
		selrecord(l, &sc->sc_selinfo);
		revents = 0;
	}
	mutex_exit(&sc->sc_lock);

	return revents;
}