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: tx39io.c,v 1.23 2012/10/27 17:17:54 chs Exp $ */

/*-
 * Copyright (c) 1999-2001 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by UCHIYAMA Yasushi.
 *
 * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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: tx39io.c,v 1.23 2012/10/27 17:17:54 chs Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <machine/bus.h>

#include <hpcmips/tx/tx39var.h>
#include <hpcmips/tx/tx39icureg.h>
#define	__TX39IO_PRIVATE
#include <hpcmips/tx/tx39iovar.h>
#include <hpcmips/tx/tx39ioreg.h>

#ifdef	TX39IO_DEBUG
#define	DPRINTF_ENABLE
#define	DPRINTF_DEBUG	tx39io_debug
#endif
#include <machine/debug.h>

#define	ISBITSET(x, s)	((x) & (1 << (s)))

int	tx39io_match(device_t, cfdata_t, void *);
void	tx39io_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(tx39io, sizeof(struct tx39io_softc),
    tx39io_match, tx39io_attach, NULL, NULL);

/* IO/MFIO common */
static void port_intr_disestablish(hpcio_chip_t, hpcio_intr_handle_t);
static void port_intr_clear(hpcio_chip_t, hpcio_intr_handle_t);
/* MFIO */
static void *mfio_intr_establish(hpcio_chip_t, int, int, int (*)(void *),
    void *);
static int mfio_in(hpcio_chip_t, int);
static void mfio_out(hpcio_chip_t, int, int);
static int mfio_intr_map(int *, int, int);
static void mfio_dump(hpcio_chip_t);
static void mfio_update(hpcio_chip_t);
/* IO */
static void *io_intr_establish(hpcio_chip_t, int, int, int (*)(void *),
    void *);
#ifdef TX391X
static int tx391x_io_in(hpcio_chip_t, int);
static void tx391x_io_out(hpcio_chip_t, int, int);
static void tx391x_io_update(hpcio_chip_t);
static int tx391x_io_intr_map(int *, int, int);
#endif
#ifdef TX392X
static int tx392x_io_in(hpcio_chip_t, int);
static void tx392x_io_out(hpcio_chip_t, int, int);
static void tx392x_io_update(hpcio_chip_t);
static int tx392x_io_intr_map(int *, int, int);
#endif
#if defined TX391X && defined TX392X
#define	tx39_io_intr_map(t, s, p, m)					\
	(IS_TX391X(t)							\
	    ? tx391x_io_intr_map(s, p, m) : tx392x_io_intr_map(s, p, m))
#elif defined TX391X
#define	tx39io_intr_map(t, s, p, m)	tx391x_io_intr_map(s, p, m)
#elif defined TX392X
#define	tx39io_intr_map(t, s, p, m)	tx392x_io_intr_map(s, p, m)
#endif
static void io_dump(hpcio_chip_t);

static void __print_port_status(struct tx39io_port_status *, int);

int
tx39io_match(device_t parent, cfdata_t cf, void *aux)
{
	return (ATTACH_FIRST); /* 1st attach group of txsim */
}

void
tx39io_attach(device_t parent, device_t self, void *aux)
{
	struct txsim_attach_args *ta = aux;
	struct tx39io_softc *sc = device_private(self);
	tx_chipset_tag_t tc;
	struct hpcio_chip *io_hc = &sc->sc_io_ops;
	struct hpcio_chip *mfio_hc = &sc->sc_mfio_ops;

	sc->sc_dev = self;
	tc = sc->sc_tc = ta->ta_tc;

	printf("\n");
	sc->sc_stat_io_mask = ~(1 << 5); /* exclude Plum2 INT */
	sc->sc_stat_mfio_mask = ~(0x3|(0x3 << 23));

	/* IO */
	io_hc->hc_chipid		= IO;
	io_hc->hc_name			= "IO";
	io_hc->hc_sc			= sc;
	io_hc->hc_intr_establish	= io_intr_establish;
	io_hc->hc_intr_disestablish	= port_intr_disestablish;
	io_hc->hc_intr_clear		= port_intr_clear;
	io_hc->hc_dump			= io_dump;
	if (IS_TX391X(tc)) {
#ifdef TX391X
		io_hc->hc_portread	= tx391x_io_in;
		io_hc->hc_portwrite	= tx391x_io_out;
		io_hc->hc_update	= tx391x_io_update;
#endif
	} else if (IS_TX392X(tc)) {
#ifdef TX392X
		io_hc->hc_portread	= tx392x_io_in;
		io_hc->hc_portwrite	= tx392x_io_out;
		io_hc->hc_update	= tx392x_io_update;
#endif
	}
	tx_conf_register_ioman(tc, io_hc);

	/* MFIO */
	mfio_hc->hc_chipid		= MFIO;
	mfio_hc->hc_name		= "MFIO";
	mfio_hc->hc_sc			= sc;
	mfio_hc->hc_portread		= mfio_in;
	mfio_hc->hc_portwrite		= mfio_out;
	mfio_hc->hc_intr_establish	= mfio_intr_establish;
	mfio_hc->hc_intr_disestablish	= port_intr_disestablish;
	mfio_hc->hc_update		= mfio_update;
	mfio_hc->hc_dump		= mfio_dump;

	tx_conf_register_ioman(tc, mfio_hc);

	hpcio_update(io_hc);
	hpcio_update(mfio_hc);

#ifdef TX39IO_DEBUG
	hpcio_dump(io_hc);
	hpcio_dump(mfio_hc);
	printf("IO i0x%08x o0x%08x MFIO i0x%08x o0x%08x\n",
	       sc->sc_stat_io.in, sc->sc_stat_io.out,
	       sc->sc_stat_mfio.in, sc->sc_stat_mfio.out);
#endif /* TX39IO_DEBUG */
}

/*
 * TX391X, TX392X common
 */
static void *
io_intr_establish(hpcio_chip_t arg, int port, int mode, int (*func)(void *),
    void *func_arg)
{
	struct tx39io_softc *sc = arg->hc_sc;
	int src;

	if (tx39io_intr_map(sc->sc_tc, &src, port, mode) != 0)
		return (0);

	return (tx_intr_establish(sc->sc_tc, src, IST_EDGE, IPL_CLOCK, func,
	    func_arg));
}

static void *
mfio_intr_establish(hpcio_chip_t arg, int port, int mode, int (*func)(void *),
    void *func_arg)
{
	struct tx39io_softc *sc = arg->hc_sc;
	int src;

	if (mfio_intr_map(&src, port, mode) != 0)
		return (0);

	return (tx_intr_establish(sc->sc_tc, src, IST_EDGE, IPL_CLOCK, func,
	    func_arg));
}

static void
port_intr_disestablish(hpcio_chip_t arg, void *ih)
{
	struct tx39io_softc *sc = arg->hc_sc;
	tx_intr_disestablish(sc->sc_tc, ih);
}

static void
port_intr_clear(hpcio_chip_t arg, void *ih)
{
}

static void
mfio_out(hpcio_chip_t arg, int port, int onoff)
{
	struct tx39io_softc *sc = arg->hc_sc;
	tx_chipset_tag_t tc;
	txreg_t reg, pos;

	DPRINTF("port #%d\n", port);
	tc = sc->sc_tc;
	/* MFIO */
	pos = 1 << port;
#ifdef DIAGNOSTIC
	if (!(sc->sc_stat_mfio.dir & pos)) {
		panic("%s: MFIO%d is not output port.",
		      device_xname(sc->sc_dev), port);
	}
#endif
	reg = tx_conf_read(tc, TX39_IOMFIODATAOUT_REG);
	if (onoff)
		reg |= pos;
	else
		reg &= ~pos;
	tx_conf_write(tc, TX39_IOMFIODATAOUT_REG, reg);
}

static int
mfio_in(hpcio_chip_t arg, int port)
{
	struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc ;

	DPRINTF("port #%d\n", port);
	return (tx_conf_read(sc->sc_tc, TX39_IOMFIODATAIN_REG) & (1 << port));
}

static int
mfio_intr_map(int *src, int port, int mode)
{

	if (mode & HPCIO_INTR_POSEDGE) {
		*src = MAKEINTR(3, (1 << port));
		return (0);
	} else if (mode & HPCIO_INTR_NEGEDGE) {
		*src = MAKEINTR(4, (1 << port));
		return (0);
	}

	DPRINTF("invalid interrupt mode.\n");

	return (1);
}

static void
mfio_update(hpcio_chip_t arg)
{
	struct tx39io_softc *sc = arg->hc_sc;
	tx_chipset_tag_t tc = sc->sc_tc;
	struct tx39io_port_status *stat_mfio = &sc->sc_stat_mfio;

	sc->sc_ostat_mfio = *stat_mfio; /* save old status */
	stat_mfio->dir		= tx_conf_read(tc, TX39_IOMFIODATADIR_REG);
	stat_mfio->in		= tx_conf_read(tc, TX39_IOMFIODATAIN_REG);
	stat_mfio->out		= tx_conf_read(tc, TX39_IOMFIODATAOUT_REG);
	stat_mfio->power	= tx_conf_read(tc, TX39_IOMFIOPOWERDWN_REG);
	stat_mfio->u.select	= tx_conf_read(tc, TX39_IOMFIODATASEL_REG);
}

#ifdef TX391X
/*
 * TMPR3912 specific
 */
int
tx391x_io_in(hpcio_chip_t arg, int port)
{
	struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc;
	txreg_t reg = tx_conf_read(sc->sc_tc, TX39_IOCTRL_REG);

	DPRINTF("port #%d\n", port);
	return  (TX391X_IOCTRL_IODIN(reg) & (1 << port));
}

void
tx391x_io_out(hpcio_chip_t arg, int port, int onoff)
{
	struct tx39io_softc *sc = arg->hc_sc;
	tx_chipset_tag_t tc;
	txreg_t reg, pos, iostat;

	KASSERT(sc);
	DPRINTF("port #%d\n", port);

	tc = sc->sc_tc;

	/* IO [0:6] */
	pos = 1 << port;
#ifdef DIAGNOSTIC
	if (!(sc->sc_stat_io.dir & pos))
		panic("%s: IO%d is not output port.", device_xname(sc->sc_dev),
		      port);
#endif
	reg = tx_conf_read(tc, TX39_IOCTRL_REG);
	iostat = TX391X_IOCTRL_IODOUT(reg);
	if (onoff)
		iostat |= pos;
	else
		iostat &= ~pos;
	TX391X_IOCTRL_IODOUT_CLR(reg);
	reg = TX391X_IOCTRL_IODOUT_SET(reg, iostat);
	tx_conf_write(tc, TX39_IOCTRL_REG, reg);
}

void
tx391x_io_update(hpcio_chip_t arg)
{
	struct tx39io_softc *sc = arg->hc_sc;
	struct tx39io_port_status *stat_io = &sc->sc_stat_io;
	tx_chipset_tag_t tc = sc->sc_tc;
	txreg_t reg;

	/* IO [0:6] */
	sc->sc_ostat_io = *stat_io; /* save old status */
	reg = tx_conf_read(tc, TX39_IOCTRL_REG);
	stat_io->dir		= TX391X_IOCTRL_IODIREC(reg);
	stat_io->in		= TX391X_IOCTRL_IODIN(reg);
	stat_io->out		= TX391X_IOCTRL_IODOUT(reg);
	stat_io->u.debounce	= TX391X_IOCTRL_IODEBSEL(reg);
	reg = tx_conf_read(tc, TX39_IOIOPOWERDWN_REG);
	stat_io->power = TX391X_IOIOPOWERDWN_IOPD(reg);
}

int
tx391x_io_intr_map(int *src, int port, int mode)
{

	if (mode & HPCIO_INTR_POSEDGE) {
		*src = MAKEINTR(5, (1 << (port + 7)));
		return (0);
	} else if (mode & HPCIO_INTR_NEGEDGE) {
		*src = MAKEINTR(5, (1 << port));
		return (0);
	}

	DPRINTF("invalid interrupt mode.\n");

	return (1);
}
#endif /* TX391X */

#ifdef TX392X
/*
 * TMPR3922 specific
 */
int
tx392x_io_in(hpcio_chip_t arg, int port)
{
	struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc;
	txreg_t reg = tx_conf_read(sc->sc_tc, TX392X_IODATAINOUT_REG);

	DPRINTF("port #%d\n", port);

	return (TX392X_IODATAINOUT_DIN(reg) & (1 << port));
}

void
tx392x_io_out(hpcio_chip_t arg, int port, int onoff)
{
	struct tx39io_softc *sc = arg->hc_sc;
#ifdef DIAGNOSTIC
	const char *devname =  device_xname(sc->sc_dev);
#endif
	tx_chipset_tag_t tc = sc->sc_tc;
	txreg_t reg, pos, iostat;

	DPRINTF("port #%d\n", port);
	/* IO [0:15] */
	pos = 1 << port;
#ifdef DIAGNOSTIC
	if (!(sc->sc_stat_io.dir & pos))
		panic("%s: IO%d is not output port.", devname, port);
#endif
	reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
	iostat = TX392X_IODATAINOUT_DOUT(reg);
	if (onoff)
		iostat |= pos;
	else
		iostat &= ~pos;
	TX392X_IODATAINOUT_DOUT_CLR(reg);
	reg = TX392X_IODATAINOUT_DOUT_SET(reg, iostat);
	tx_conf_write(tc, TX392X_IODATAINOUT_REG, reg);
}

int
tx392x_io_intr_map(int *src, int port, int mode)
{

	if (mode & HPCIO_INTR_POSEDGE) {
		*src = MAKEINTR(8, (1 << (port + 16)));
		return (0);
	} else if (mode & HPCIO_INTR_NEGEDGE) {
		*src = MAKEINTR(8, (1 << port));
		return (0);
	}

	DPRINTF("invalid interrupt mode.\n");

	return (1);
}

void
tx392x_io_update(hpcio_chip_t arg)
{
	struct tx39io_softc *sc = arg->hc_sc;
	struct tx39io_port_status *stat_io = &sc->sc_stat_io;
	tx_chipset_tag_t tc = sc->sc_tc;
	txreg_t reg;

	sc->sc_ostat_io = *stat_io; /* save old status */
	/* IO [0:15] */
	reg = tx_conf_read(tc, TX39_IOCTRL_REG);
	stat_io->dir		= TX392X_IOCTRL_IODIREC(reg);
	stat_io->u.debounce	= TX392X_IOCTRL_IODEBSEL(reg);
	reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
	stat_io->in		= TX392X_IODATAINOUT_DIN(reg);
	stat_io->out		= TX392X_IODATAINOUT_DOUT(reg);
	reg = tx_conf_read(tc, TX39_IOIOPOWERDWN_REG);
	stat_io->power = TX392X_IOIOPOWERDWN_IOPD(reg);
}

#endif /* TX392X */

static const char *line = "--------------------------------------------------"
"------------\n";
static void
mfio_dump(hpcio_chip_t arg)
{
	struct tx39io_softc *sc = arg->hc_sc;
	const struct tx39io_mfio_map *map = tx39io_get_mfio_map(tc);
	struct tx39io_port_status *stat;
	int i;

	printf("%s", line);
	stat = &sc->sc_stat_mfio;
	for (i = TX39_IO_MFIO_MAX - 1; i >= 0 ; i--) {
		/* MFIO port has power down state */
		printf("MFIO %2d:     -       ", i);
		__print_port_status(stat, i);
		printf(ISBITSET(stat->u.select, i) ? "  MFIO(%s)\n" : "  %s\n",
		       map[i].std_pin_name);
	}
	printf("%s", line);
}

static void
io_dump(hpcio_chip_t arg)
{
	struct tx39io_softc *sc = arg->hc_sc;
	struct tx39io_port_status *stat;
	int i;

	printf("%s	 Debounce Direction DataOut DataIn PowerDown Select"
	       "\n%s", line, line);
	stat = &sc->sc_stat_io;
	for (i = tx39io_get_io_max(tc) - 1; i >= 0 ; i--) {
		/* IO port has debouncer */
		printf("IO   %2d:    %s      ", i,
		       ISBITSET(stat->u.debounce, i) ? "On " : "Off");
		__print_port_status(stat, i);
		printf("    -\n");
	}
}

static void
__print_port_status(struct tx39io_port_status *stat, int i)
{
	printf("%s       %d       %d     %s",
	       ISBITSET(stat->dir, i) ? "Out" : "In ",
	       ISBITSET(stat->out, i) ? 1 : 0,
	       ISBITSET(stat->in, i) ? 1 : 0,
	       ISBITSET(stat->power, i) ? "Down  ": "Active");
}