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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*	$NetBSD: nct.c,v 1.5 2021/08/07 16:19:12 thorpej Exp $	*/

/*-
 * Copyright (c) 2019, 2020 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Andrew Doran.
 *
 * 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.
 */

/*
 * Nuvoton NCT5104D
 *
 * - GPIO: full support.
 * - Watchdog: no support.  Watchdog uses GPIO pins.
 * - UARTS: handled by com driver.  3rd & 4th UARTs use GPIO pins.
 *
 * If asked to probe with a wildcard address, we'll only do so if known to
 * be running on a PC Engines system.  Probe is invasive.
 *
 * Register access on Super I/O chips typically involves one or two levels
 * of indirection, so we try hard to avoid needless register access.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nct.c,v 1.5 2021/08/07 16:19:12 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/device.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/gpio.h>

#include <machine/autoconf.h>

#include <dev/isa/isavar.h>

#include <dev/gpio/gpiovar.h>

/*
 * Hardware interface definition (enough for GPIO only).
 */

/* I/O basics */
#define	NCT_IOBASE_A		0x2e
#define	NCT_IOBASE_B		0x4e
#define	NCT_IOSIZE		2
#define	NCT_CHIP_ID_1		0x1061
#define	NCT_CHIP_ID_2		0xc452	/* PC Engines APU */
#define	NCT_NUM_PINS		17

/* Enable/disable keys */
#define NCT_KEY_UNLOCK		0x87
#define NCT_KEY_LOCK		0xaa

/* I/O ports */
#define	NCT_PORT_SELECT		0
#define	NCT_PORT_DATA		1

/* Global registers */
#define	GD_DEVSEL		0x0007	/* logical device select */
#define	GD_MULTIFUN		0x001c	/* multi function selection */
#define		GD_MULTIFUN_GPIO1	0x04	/* clr: gpio1 available */
#define		GD_MULTIFUN_GPIO0	0x08	/* clr: gpio0 available */
#define		GD_MULTIFUN_GPIO67	0x10	/* set: gpio67 available */
#define	GD_GLOBOPT		0x0027	/* global option */
#define 	GD_GLOBOPT_GPIO67	0x04	/* clr: gpio67 available */
#define	GD_ID_HIGH		0x0020	/* ID high byte */
#define	GD_ID_LOW		0x0021	/* ID low byte */

/* Logical device 7 */
#define	LD7_ENABLE		0x0730	/* GPIO function enable */
#define		LD7_ENABLE_GPIO0	0x01
#define		LD7_ENABLE_GPIO1	0x02
#define		LD7_ENABLE_GPIO67	0x40
#define	LD7_GPIO0_DIRECTION	0x07e0	/* clr for output, set for input */
#define	LD7_GPIO0_DATA		0x07e1	/* current status */
#define	LD7_GPIO0_INVERSION	0x07e2	/* set to invert i/o */
#define	LD7_GPIO0_STATUS	0x07e3	/* edge detect, reading clears */
#define	LD7_GPIO1_DIRECTION	0x07e4	/* clr for output, set for input */
#define	LD7_GPIO1_DATA		0x07e5	/* current status */
#define	LD7_GPIO1_INVERSION	0x07e6	/* set to invert i/o */
#define	LD7_GPIO1_STATUS	0x07e7	/* edge detect, reading clears */
#define	LD7_GPIO67_DIRECTION	0x07f8	/* clr for output, set for input */
#define	LD7_GPIO67_DATA		0x07f9	/* current status */
#define	LD7_GPIO67_INVERSION	0x07fa	/* set to invert i/o */
#define	LD7_GPIO67_STATUS	0x07fb	/* edge detect, reading clears */

/* Logical device 8 */
#define	LD8_DEVCFG		0x0830	/* WDT/GPIO device config */
#define	LD8_GPIO0_MULTIFUNC	0x08e0	/* clr: gpio, set: pin unusable */
#define	LD8_GPIO1_MULTIFUNC	0x08e1	/* clr: gpio, set: pin unusable */
#define	LD8_GPIO67_MULTIFUNC	0x08e7	/* clr: gpio, set: pin unusable */

/* Logical device 10 */
#define	LDA_UARTC_ENABLE	0x0a30	/* bit 0: UARTC active */

/* Logical device 11 */
#define	LDB_UARTD_ENABLE	0x0b30	/* bit 0: UARTD active */

/* Logical device 15 */
#define	LDF_GPIO0_OUTMODE	0x0fe0	/* clr: push/pull, set: open drain */
#define	LDF_GPIO1_OUTMODE	0x0fe1	/* clr: push/pull, set: open drain */
#define	LDF_GPIO67_OUTMODE	0x0fe6	/* clr: push/pull, set: open drain */

/*
 * Internal GPIO bank description, including register addresses and cached
 * register content.
 */
struct nct_bank {
	/* Pin descriptions */
	u_int8_t	nb_firstpin;
	u_int8_t	nb_numpins;
	u_int8_t	nb_enabled;

	/* Cached values */
	u_int8_t	nb_val_dir;
	u_int8_t	nb_val_inv;
	u_int8_t	nb_val_mode;

	/* Register addresses */
	u_int16_t	nb_reg_dir;
	u_int16_t	nb_reg_data;
	u_int16_t	nb_reg_inv;
	u_int16_t	nb_reg_stat;
	u_int16_t	nb_reg_mode;
};

/*
 * Driver instance.
 */
struct nct_softc {
	device_t		sc_dev;			/* MI device */
	bus_space_tag_t		sc_iot;			/* I/O tag */
	bus_space_handle_t	sc_ioh;			/* I/O handle */
	struct gpio_chipset_tag	 sc_gc;			/* GPIO tag */
	gpio_pin_t		sc_pins[NCT_NUM_PINS];	/* GPIO pin descr. */

	/* Access to the remaining members is covered by sc_lock. */
	kmutex_t		sc_lock;		/* Serialization */
	int			sc_curdev;		/* Cur. logical dev */
	int			sc_curreg;		/* Cur. register */
	struct nct_bank		sc_bank[3];		/* Bank descriptions */
};

static void	nct_attach(device_t, device_t, void *);
static int	nct_detach(device_t, int);
static void	nct_gpio_ctl(void *, int, int);
static int	nct_gpio_read(void *, int);
static void	nct_gpio_write(void *, int, int);
static int	nct_match(device_t, cfdata_t , void *);
static u_int8_t	nct_rd(struct nct_softc *, int);
static struct	nct_bank *nct_sel(struct nct_softc *, int, u_int8_t *);
static void	nct_wr(struct nct_softc *, int, u_int8_t);

static inline void
nct_outb(struct nct_softc *sc, int reg, u_int8_t data)
{

	bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, data);
}

static inline u_int8_t
nct_inb(struct nct_softc *sc, int reg)
{

	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, reg);
}

CFATTACH_DECL_NEW(nct,
		  sizeof(struct nct_softc),
		  nct_match,
		  nct_attach,
		  nct_detach,
		  NULL);

MODULE(MODULE_CLASS_DRIVER, nct, "gpio");

/*
 * Module linkage.
 */
#ifdef _MODULE
#include "ioconf.c"
#endif

static int
nct_modcmd(modcmd_t cmd, void *priv)
{
	int error = 0;

	switch (cmd) {
	case MODULE_CMD_INIT:
#ifdef _MODULE
		error = config_init_component(cfdriver_ioconf_nct,
		    cfattach_ioconf_nct, cfdata_ioconf_nct);
#endif
		return error;
	case MODULE_CMD_FINI:
#ifdef _MODULE
		error = config_fini_component(cfdriver_ioconf_nct,
		    cfattach_ioconf_nct, cfdata_ioconf_nct);
#endif
		return error;
	default:
		return ENOTTY;
	}
}

/*
 * Probe for device.
 */
static int
nct_match(device_t parent, cfdata_t match, void *aux)
{
	int ioaddrs[2] = { 0x2e, 0x4e };
	struct isa_attach_args *ia = aux;
	bus_space_handle_t ioh;
	int nioaddr, i;
	u_int8_t low, high;
	u_int16_t id;
	const char *vendor;

	/*
	 * Allow override of I/O base address.  If no I/O base address is
	 * provided, proceed to probe if running on a PC Engines system.
	 */
	if (ia->ia_nio > 0 && ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) {
		ioaddrs[0] = ia->ia_io[0].ir_addr;
		nioaddr = 1;
	} else {
		vendor = pmf_get_platform("system-vendor");
		if (vendor != NULL && strstr(vendor, "PC Engines") != NULL) {
			nioaddr = __arraycount(ioaddrs);
		} else {
			nioaddr = 0;
		}
	}

	/*
	 * Probe at the selected addresses, if any.
	 */
	for (i = 0; i < nioaddr; i++) {
		if (bus_space_map(ia->ia_iot, ioaddrs[i], NCT_IOSIZE, 0,
		    &ioh) != 0) {
		    continue;
		}
                /* Unlock chip */
		bus_space_write_1(ia->ia_iot, ioh, NCT_PORT_SELECT,
		    NCT_KEY_UNLOCK);
		bus_space_write_1(ia->ia_iot, ioh, NCT_PORT_SELECT,
		    NCT_KEY_UNLOCK);
		/* Read ID */
		bus_space_write_1(ia->ia_iot, ioh, NCT_PORT_SELECT, GD_ID_LOW);
		low = bus_space_read_1(ia->ia_iot, ioh, NCT_PORT_DATA);
		bus_space_write_1(ia->ia_iot, ioh, NCT_PORT_SELECT, GD_ID_HIGH);
		high = bus_space_read_1(ia->ia_iot, ioh, NCT_PORT_DATA);
		id = (u_int16_t)low | ((u_int16_t)high << 8);
		bus_space_unmap(ia->ia_iot, ioh, NCT_IOSIZE);
		if (id == NCT_CHIP_ID_1 || id == NCT_CHIP_ID_2) {
			ia->ia_nirq = 0;
			ia->ia_ndrq = 0;
			ia->ia_niomem = 0;
			ia->ia_nio = 1;
			ia->ia_io[0].ir_size = NCT_IOSIZE;
			ia->ia_io[0].ir_addr = ioaddrs[i];
			return 1;
		}
	}
	return 0;
}

/*
 * Attach device instance.
 */
static void
nct_attach(device_t parent, device_t self, void *aux)
{
	struct nct_softc *sc = device_private(self);
	struct isa_attach_args *ia = aux;
	struct gpiobus_attach_args gba;
	struct nct_bank *nb;
	u_int8_t multifun, enable;
	int i, j;

	/*
	 * Set up register space and basics of our state.
	 */
	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr,
	    ia->ia_io[0].ir_size, 0, &sc->sc_ioh) != 0) {
		aprint_normal(": can't map i/o space\n");
		return;
	}
	aprint_normal(": Nuvoton NCT5104D GPIO\n");
	sc->sc_dev = self;
	sc->sc_iot = ia->ia_iot;
	sc->sc_curdev = -1;
	sc->sc_curreg = -1;

        /*
         * All pin access is funneled through a common, indirect register
         * interface.  The gpio framework doesn't serialize calls to our
         * access methods, so do it internally.  This is likely such a
         * common requirement that it should be factored out as is done for
         * audio devices, allowing the driver to specify the appropriate
         * locks.  Anyhow, acquire the lock immediately to pacify locking
         * assertions.
         */
        mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
	mutex_spin_enter(&sc->sc_lock);

        /*
         * Disable watchdog timer and GPIO alternate I/O mapping.
         */
        nct_wr(sc, LD8_DEVCFG, 0);

	/*
	 * The BIOS doesn't set things up the way we want.  Pfft.
	 * Enable all GPIO0/GPIO1 pins.
	 */
	multifun = nct_rd(sc, GD_MULTIFUN);
	nct_wr(sc, GD_MULTIFUN,
	    multifun & ~(GD_MULTIFUN_GPIO0 | GD_MULTIFUN_GPIO1));
	nct_wr(sc, LDA_UARTC_ENABLE, 0);
	nct_wr(sc, LD8_GPIO0_MULTIFUNC, 0);
	nct_wr(sc, LDB_UARTD_ENABLE, 0);
	nct_wr(sc, LD8_GPIO1_MULTIFUNC, 0);
	multifun = nct_rd(sc, GD_MULTIFUN);
	enable = nct_rd(sc, LD7_ENABLE) | LD7_ENABLE_GPIO0 | LD7_ENABLE_GPIO1;

	nb = &sc->sc_bank[0];
	nb->nb_firstpin = 0;
	nb->nb_numpins = 8;
	nb->nb_enabled = 0xff;
	nb->nb_reg_dir = LD7_GPIO0_DIRECTION;
	nb->nb_reg_data = LD7_GPIO0_DATA;
	nb->nb_reg_inv = LD7_GPIO0_INVERSION;
	nb->nb_reg_stat = LD7_GPIO0_STATUS;
	nb->nb_reg_mode = LDF_GPIO0_OUTMODE;

	nb = &sc->sc_bank[1];
	nb->nb_firstpin = 8;
	nb->nb_numpins = 8;
	nb->nb_enabled = 0xff;
	nb->nb_reg_dir = LD7_GPIO1_DIRECTION;
	nb->nb_reg_data = LD7_GPIO1_DATA;
	nb->nb_reg_inv = LD7_GPIO1_INVERSION;
	nb->nb_reg_stat = LD7_GPIO1_STATUS;
	nb->nb_reg_mode = LDF_GPIO1_OUTMODE;

	nb = &sc->sc_bank[2];
	nb->nb_firstpin = 16;
	nb->nb_numpins = 1;
	nb->nb_reg_dir = LD7_GPIO67_DIRECTION;
	nb->nb_reg_data = LD7_GPIO67_DATA;
	nb->nb_reg_stat = LD7_GPIO67_STATUS;
	nb->nb_reg_mode = LDF_GPIO67_OUTMODE;
	if ((multifun & GD_MULTIFUN_GPIO67) != 0 &&
	    (nct_rd(sc, GD_GLOBOPT) & GD_GLOBOPT_GPIO67) == 0) {
		nct_wr(sc, LD8_GPIO67_MULTIFUNC, 0);
		nb->nb_enabled = 0x01;
		enable |= LD7_ENABLE_GPIO67;
	} else {
		sc->sc_bank[2].nb_enabled = 0;
	}

	/*
	 * Display enabled pins and enable GPIO devices accordingly.
	 */
	nct_wr(sc, LD7_ENABLE, enable);
	mutex_spin_exit(&sc->sc_lock);

	aprint_normal_dev(self,
	    "enabled pins: GPIO0(%02x) GPIO1(%02x) GPIO67(%01x)\n",
	    (unsigned)sc->sc_bank[0].nb_enabled,
	    (unsigned)sc->sc_bank[1].nb_enabled,
	    (unsigned)sc->sc_bank[2].nb_enabled);

	/*
	 * Fill pin descriptions and initialize registers.
	 */
	memset(sc->sc_pins, 0, sizeof(sc->sc_pins));
	for (i = 0; i < __arraycount(sc->sc_bank); i++) {
		nb = &sc->sc_bank[i];
		mutex_spin_enter(&sc->sc_lock);
		nb->nb_val_dir = nct_rd(sc, nb->nb_reg_dir);
		nb->nb_val_inv = nct_rd(sc, nb->nb_reg_inv);
		nb->nb_val_mode = nct_rd(sc, nb->nb_reg_mode);
		mutex_spin_exit(&sc->sc_lock);
		for (j = 0; j < nb->nb_numpins; j++) {
			gpio_pin_t *pin = &sc->sc_pins[nb->nb_firstpin + j];
			pin->pin_num = nb->nb_firstpin + j;
			/* Skip pin if not configured as GPIO. */
			if ((nb->nb_enabled & (1 << j)) == 0) {
				continue;
			}
			pin->pin_caps =
			    GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
			    GPIO_PIN_OPENDRAIN |
			    GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
			    GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
			pin->pin_flags =
			    GPIO_PIN_INPUT | GPIO_PIN_OPENDRAIN;
			nct_gpio_ctl(sc, pin->pin_num, pin->pin_flags);
			pin->pin_state = nct_gpio_read(sc, pin->pin_num);
		}
	}

	/*
	 * Attach to gpio framework, and attach all pins unconditionally. 
	 * If the pins are disabled, we'll ignore any access later.
	 */
	sc->sc_gc.gp_cookie = sc;
	sc->sc_gc.gp_pin_read = nct_gpio_read;
	sc->sc_gc.gp_pin_write = nct_gpio_write;
	sc->sc_gc.gp_pin_ctl = nct_gpio_ctl;

	gba.gba_gc = &sc->sc_gc;
	gba.gba_pins = sc->sc_pins;
	gba.gba_npins = NCT_NUM_PINS;

	(void)config_found(sc->sc_dev, &gba, gpiobus_print, CFARGS_NONE);
}

/*
 * Detach device instance.
 */
static int
nct_detach(device_t self, int flags)
{
	struct nct_softc *sc = device_private(self);

	bus_space_unmap(sc->sc_iot, sc->sc_ioh, NCT_IOSIZE);
	mutex_destroy(&sc->sc_lock);
	return 0;
}

/*
 * Read byte from specified register.
 */
static u_int8_t
nct_rd(struct nct_softc *sc, int reg)
{
	int dev;

	KASSERT(mutex_owned(&sc->sc_lock));
	
	dev = reg >> 8;
	reg &= 0xff;

	if (dev != sc->sc_curdev && dev != 0x00) {
		sc->sc_curdev = dev;
		sc->sc_curreg = reg;
		nct_outb(sc, NCT_PORT_SELECT, GD_DEVSEL);
		nct_outb(sc, NCT_PORT_DATA, dev);
		nct_outb(sc, NCT_PORT_SELECT, reg);
		return nct_inb(sc, NCT_PORT_DATA);
	} else if (reg != sc->sc_curreg) {
		sc->sc_curreg = reg;
		nct_outb(sc, NCT_PORT_SELECT, reg);
		return nct_inb(sc, NCT_PORT_DATA);
	} else {
		return nct_inb(sc, NCT_PORT_DATA);
	}
}

/*
 * Write byte to specified register.
 */
static void
nct_wr(struct nct_softc *sc, int reg, u_int8_t data)
{
	int dev;

	KASSERT(mutex_owned(&sc->sc_lock));
	
	dev = reg >> 8;
	reg &= 0xff;

	if (dev != sc->sc_curdev && dev != 0x00) {
		sc->sc_curdev = dev;
		sc->sc_curreg = reg;
		nct_outb(sc, NCT_PORT_SELECT, GD_DEVSEL);
		nct_outb(sc, NCT_PORT_DATA, dev);
		nct_outb(sc, NCT_PORT_SELECT, reg);
		nct_outb(sc, NCT_PORT_DATA, data);
	} else if (reg != sc->sc_curreg) {
		sc->sc_curreg = reg;
		nct_outb(sc, NCT_PORT_SELECT, reg);
		nct_outb(sc, NCT_PORT_DATA, data);
	} else {
		nct_outb(sc, NCT_PORT_DATA, data);
	}
}

/*
 * Given pin number, return bank and pin mask.  This alters no state and so
 * can safely be called without the mutex held.
 */
static struct nct_bank *
nct_sel(struct nct_softc *sc, int pin, u_int8_t *mask)
{
	struct nct_bank *nb;

	KASSERT(pin >= 0 && pin < NCT_NUM_PINS);
	nb = &sc->sc_bank[pin >> 3];
	KASSERT(pin >= nb->nb_firstpin);
	KASSERT((pin & 7) < nb->nb_numpins);
	*mask = (u_int8_t)(1 << (pin & 7)) & nb->nb_enabled;
	return nb;
}

/*
 * GPIO hook: read pin.
 */
static int
nct_gpio_read(void *arg, int pin)
{
	struct nct_softc *sc = arg;
	struct nct_bank *nb;
	u_int8_t data, mask;
	int rv = GPIO_PIN_LOW;

	nb = nct_sel(sc, pin, &mask);
	if (__predict_true(mask != 0)) {
		mutex_spin_enter(&sc->sc_lock);
		data = nct_rd(sc, nb->nb_reg_data);
		if ((data & mask) != 0) {
			rv = GPIO_PIN_HIGH;
		}
		mutex_spin_exit(&sc->sc_lock);
	}
	return rv;
}

/*
 * GPIO hook: write pin.
 */
static void
nct_gpio_write(void *arg, int pin, int val)
{
	struct nct_softc *sc = arg;
	struct nct_bank *nb;
	u_int8_t data, mask;

	nb = nct_sel(sc, pin, &mask);
	if (__predict_true(mask != 0)) {
		mutex_spin_enter(&sc->sc_lock);
		data = nct_rd(sc, nb->nb_reg_data);
		if (val == GPIO_PIN_LOW) {
			data &= ~mask;
		} else if (val == GPIO_PIN_HIGH) {
			data |= mask;
		}
		nct_wr(sc, nb->nb_reg_data, data);
		mutex_spin_exit(&sc->sc_lock);
	}
}

/*
 * GPIO hook: change pin parameters.
 */
static void
nct_gpio_ctl(void *arg, int pin, int flg)
{
	struct nct_softc *sc = arg;
	struct nct_bank *nb;
	u_int8_t data, mask;

	nb = nct_sel(sc, pin, &mask);
	if (__predict_false(mask == 0)) {
		return;
	}

	/*
	 * Set input direction early to avoid perturbation.
	 */
	mutex_spin_enter(&sc->sc_lock);
	data = nb->nb_val_dir;
	if ((flg & (GPIO_PIN_INPUT | GPIO_PIN_TRISTATE)) != 0) {
		data |= mask;
	}
	if (data != nb->nb_val_dir) {
		nct_wr(sc, nb->nb_reg_dir, data);
		nb->nb_val_dir = data;
	}

	/*
	 * Set inversion.
	 */
	data = nb->nb_val_inv;
	if ((flg & (GPIO_PIN_OUTPUT | GPIO_PIN_INVOUT)) ==
	             (GPIO_PIN_OUTPUT | GPIO_PIN_INVOUT) ||
            (flg & (GPIO_PIN_INPUT | GPIO_PIN_INVIN)) ==
  	             (GPIO_PIN_INPUT | GPIO_PIN_INVIN)) {
		data |= mask;
	} else {
		data &= ~mask;
	}
	if (data != nb->nb_val_inv) {
		nct_wr(sc, nb->nb_reg_inv, data);
		nb->nb_val_inv = data;
	}

	/*
	 * Set drain mode.
	 */
	data = nb->nb_val_mode;
	if ((flg & GPIO_PIN_PUSHPULL) != 0) {
		data |= mask;
	} else /* GPIO_PIN_OPENDRAIN */ {
		data &= ~mask;
	}
	if (data != nb->nb_val_mode) {
		nct_wr(sc, nb->nb_reg_mode, data);
		nb->nb_val_mode = data;
	}

	/*
	 * Set output direction late to avoid perturbation.
	 */
	data = nb->nb_val_dir;
	if ((flg & (GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE)) == GPIO_PIN_OUTPUT) {
		data &= ~mask;
	}
	if (data != nb->nb_val_dir) {
		nct_wr(sc, nb->nb_reg_dir, data);
		nb->nb_val_dir = data;
	}
	mutex_spin_exit(&sc->sc_lock);
}