Home | History | Annotate | Line # | Download | only in isa
wbsio.c revision 1.28
      1 /*	$NetBSD: wbsio.c,v 1.28 2022/10/01 07:22:55 msaitoh Exp $	*/
      2 /*	$OpenBSD: wbsio.c,v 1.10 2015/03/14 03:38:47 jsg Exp $	*/
      3 /*
      4  * Copyright (c) 2008 Mark Kettenis <kettenis (at) openbsd.org>
      5  *
      6  * Permission to use, copy, modify, and distribute this software for any
      7  * purpose with or without fee is hereby granted, provided that the above
      8  * copyright notice and this permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  */
     18 
     19 /*
     20  * Winbond LPC Super I/O driver.
     21  */
     22 
     23 #include <sys/cdefs.h>
     24 #include <sys/param.h>
     25 #include <sys/device.h>
     26 #include <sys/kernel.h>
     27 #include <sys/module.h>
     28 #include <sys/systm.h>
     29 #include <sys/mutex.h>
     30 #include <sys/gpio.h>
     31 
     32 #include <sys/bus.h>
     33 
     34 #include <dev/isa/isareg.h>
     35 #include <dev/isa/isavar.h>
     36 #include <dev/isa/wbsioreg.h>
     37 #include <dev/sysmon/sysmonvar.h>
     38 
     39 /* Don't use gpio for now in the module */
     40 #if !defined(_MODULE) && defined(WBSIO_GPIO)
     41 #include "gpio.h"
     42 #endif
     43 
     44 #if NGPIO > 0
     45 #include <dev/gpio/gpiovar.h>
     46 #endif
     47 
     48 struct wbsio_softc {
     49 	device_t	sc_dev;
     50 	device_t	sc_lm_dev;
     51 #if NGPIO > 0
     52 	device_t	sc_gpio_dev;
     53 #endif
     54 
     55 	bus_space_tag_t		sc_iot;
     56 	bus_space_handle_t	sc_ioh;
     57 	kmutex_t		sc_conf_lock;
     58 
     59 	struct isa_attach_args	sc_ia;
     60 	struct isa_io		sc_io;
     61 
     62 #if NGPIO > 0
     63 	bus_space_handle_t	sc_gpio_ioh;
     64 	kmutex_t		sc_gpio_lock;
     65 	struct gpio_chipset_tag	sc_gpio_gc;
     66 	struct gpio_pin		sc_gpio_pins[WBSIO_GPIO_NPINS];
     67 	bool			sc_gpio_rt;
     68 #endif
     69 
     70 	struct sysmon_wdog	sc_smw;
     71 	bool			sc_smw_valid;
     72 };
     73 
     74 static const struct wbsio_product {
     75 	uint16_t id;
     76 	int idbits;
     77 	const char *str;
     78 } wbsio_products[] = {
     79 	{ WBSIO_ID_W83627HF,	8,	"W83627HF" },
     80 	{ WBSIO_ID_W83697HF,	8,	"W83697HF" },
     81 	{ WBSIO_ID_W83637HF,	8,	"W83637HF" },
     82 	{ WBSIO_ID_W83627THF,	8,	"W83627THF" },
     83 	{ WBSIO_ID_W83687THF,	8,	"W83687THF" },
     84 	{ WBSIO_ID_W83627DHG,	12,	"W83627DHG" },
     85 	{ WBSIO_ID_W83627DHGP,	12,	"W83627DHG-P" },
     86 	{ WBSIO_ID_W83627EHF,	12,	"W83627EHF" },
     87 	{ WBSIO_ID_W83627SF,	12,	"W83627SF" },
     88 	{ WBSIO_ID_W83627UHG,	12,	"W83627UHG" },
     89 	{ WBSIO_ID_W83667HG,	12,	"W83667HG" },
     90 	{ WBSIO_ID_W83667HGB,	12,	"W83667HGB" },
     91 	{ WBSIO_ID_W83697UG,	12,	"W83697UG" },
     92 	{ WBSIO_ID_NCT6775F,	12,	"NCT6775F" },
     93 	{ WBSIO_ID_NCT6776F,	12,	"NCT6776F" },
     94 	{ WBSIO_ID_NCT5104D,	12,	"NCT5104D or 610[246]D" },
     95 	{ WBSIO_ID_NCT6779D,	12,	"NCT6779D" },
     96 	{ WBSIO_ID_NCT6791D,	12,	"NCT6791D" },
     97 	{ WBSIO_ID_NCT6792D,	12,	"NCT6792D" },
     98 	{ WBSIO_ID_NCT6793D,	12,	"NCT6793D" },
     99 	{ WBSIO_ID_NCT6795D,	12,	"NCT6795D" },
    100 	{ WBSIO_ID_NCT6796D,	13,	"NCT6796D" },
    101 	{ WBSIO_ID_NCT6798D,	13,	"NCT6798D" },
    102 	{ WBSIO_ID_NCT6799D,	13,	"NCT6799D" },
    103 };
    104 
    105 static const struct wbsio_product *wbsio_lookup(uint8_t id, uint8_t rev);
    106 static int	wbsio_match(device_t, cfdata_t, void *);
    107 static void	wbsio_attach(device_t, device_t, void *);
    108 static int	wbsio_detach(device_t, int);
    109 static int	wbsio_rescan(device_t, const char *, const int *);
    110 static void	wbsio_childdet(device_t, device_t);
    111 static int	wbsio_print(void *, const char *);
    112 static int	wbsio_search(device_t, cfdata_t, const int *, void *);
    113 static bool	wbsio_suspend(device_t, const pmf_qual_t *);
    114 #if NGPIO > 0
    115 static int	wbsio_gpio_search(device_t, cfdata_t, const int *, void *);
    116 static int	wbsio_gpio_rt_init(struct wbsio_softc *);
    117 static int	wbsio_gpio_rt_read(struct wbsio_softc *, int, int);
    118 static void	wbsio_gpio_rt_write(struct wbsio_softc *, int, int, int);
    119 static int	wbsio_gpio_rt_pin_read(void *, int);
    120 static void	wbsio_gpio_rt_pin_write(void *, int, int);
    121 static void	wbsio_gpio_rt_pin_ctl(void *, int, int);
    122 static void	wbsio_gpio_enable_nct6779d(device_t);
    123 static void	wbsio_gpio_pinconfig_nct6779d(device_t);
    124 #endif
    125 static void	wbsio_wdog_attach(device_t);
    126 static int	wbsio_wdog_detach(device_t);
    127 static int	wbsio_wdog_setmode(struct sysmon_wdog *);
    128 static int	wbsio_wdog_tickle(struct sysmon_wdog *);
    129 static void	wbsio_wdog_setcounter(struct wbsio_softc *, uint8_t);
    130 static void	wbsio_wdog_clear_timeout(struct wbsio_softc *);
    131 
    132 CFATTACH_DECL2_NEW(wbsio, sizeof(struct wbsio_softc),
    133     wbsio_match, wbsio_attach, wbsio_detach, NULL,
    134     wbsio_rescan, wbsio_childdet);
    135 
    136 static __inline void
    137 wbsio_conf_enable(kmutex_t *lock, bus_space_tag_t iot, bus_space_handle_t ioh)
    138 {
    139 	if (lock)
    140 		mutex_enter(lock);
    141 
    142 	bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
    143 	bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
    144 }
    145 
    146 static __inline void
    147 wbsio_conf_disable(kmutex_t *lock, bus_space_tag_t iot, bus_space_handle_t ioh)
    148 {
    149 
    150 	bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_DS_MAGIC);
    151 
    152 	if (lock)
    153 		mutex_exit(lock);
    154 }
    155 
    156 static __inline uint8_t
    157 wbsio_conf_read(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t index)
    158 {
    159 	bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
    160 	return (bus_space_read_1(iot, ioh, WBSIO_DATA));
    161 }
    162 
    163 static __inline void
    164 wbsio_conf_write(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t index,
    165     uint8_t data)
    166 {
    167 	bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
    168 	bus_space_write_1(iot, ioh, WBSIO_DATA, data);
    169 }
    170 
    171 static const struct wbsio_product *
    172 wbsio_lookup(uint8_t id, uint8_t rev)
    173 {
    174 	int i;
    175 
    176 	for (i = 0; i < __arraycount(wbsio_products); i++) {
    177 		const struct wbsio_product *ent = &wbsio_products[i];
    178 		switch (ent->idbits) {
    179 		case 13:
    180 		case 12:
    181 		case 8:
    182 			if (ent->id == WBSIO_MAKEID(id, rev, ent->idbits))
    183 				return ent;
    184 			break;
    185 		default:
    186 			break;
    187 		}
    188 	}
    189 
    190 	/* Not found */
    191 	return NULL;
    192 }
    193 
    194 int
    195 wbsio_match(device_t parent, cfdata_t match, void *aux)
    196 {
    197 	struct isa_attach_args *ia = aux;
    198 	const struct wbsio_product *product;
    199 	bus_space_tag_t iot;
    200 	bus_space_handle_t ioh;
    201 	uint8_t id, rev;
    202 
    203 	/* Must supply an address */
    204 	if (ia->ia_nio < 1)
    205 		return 0;
    206 
    207 	if (ISA_DIRECT_CONFIG(ia))
    208 		return 0;
    209 
    210 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    211 		return 0;
    212 
    213 	/* Match by device ID */
    214 	iot = ia->ia_iot;
    215 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, WBSIO_IOSIZE, 0, &ioh))
    216 		return 0;
    217 	wbsio_conf_enable(NULL, iot, ioh);
    218 	id = wbsio_conf_read(iot, ioh, WBSIO_ID);
    219 	rev = wbsio_conf_read(iot, ioh, WBSIO_REV);
    220 	aprint_debug("wbsio_probe: id 0x%02x, rev 0x%02x\n", id, rev);
    221 	wbsio_conf_disable(NULL, iot, ioh);
    222 	bus_space_unmap(iot, ioh, WBSIO_IOSIZE);
    223 
    224 	if ((product = wbsio_lookup(id, rev)) == NULL)
    225 		return 0;
    226 
    227 	ia->ia_nio = 1;
    228 	ia->ia_io[0].ir_size = WBSIO_IOSIZE;
    229 	ia->ia_niomem = 0;
    230 	ia->ia_nirq = 0;
    231 	ia->ia_ndrq = 0;
    232 	return 1;
    233 }
    234 
    235 void
    236 wbsio_attach(device_t parent, device_t self, void *aux)
    237 {
    238 	struct wbsio_softc *sc = device_private(self);
    239 	struct isa_attach_args *ia = aux;
    240 	const struct wbsio_product *product;
    241 	const char *desc;
    242 	const char *vendor;
    243 	uint8_t id, rev;
    244 
    245 	sc->sc_dev = self;
    246 
    247 	sc->sc_ia = *ia;
    248 
    249 	/* Map ISA I/O space */
    250 	sc->sc_iot = ia->ia_iot;
    251 	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
    252 	    WBSIO_IOSIZE, 0, &sc->sc_ioh)) {
    253 		aprint_error(": can't map i/o space\n");
    254 		return;
    255 	}
    256 
    257 	mutex_init(&sc->sc_conf_lock, MUTEX_DEFAULT, IPL_NONE);
    258 
    259 	/* Enter configuration mode */
    260 	wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    261 
    262 	/* Read device ID */
    263 	id = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
    264 	/* Read device revision */
    265 	rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
    266 
    267 	/* Escape from configuration mode */
    268 	wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    269 
    270 	if ((product = wbsio_lookup(id, rev)) == NULL) {
    271 		aprint_error_dev(self, "Unknown device. Failed to attach\n");
    272 		return;
    273 	}
    274 	rev = WBSIO_MAKEREV(rev, product->idbits);
    275 
    276 	desc = product->str;
    277 	if (desc[0] == 'W')
    278 		vendor = "Winbond";
    279 	else
    280 		vendor = "Nuvoton";
    281 	aprint_naive("\n");
    282 	aprint_normal(": %s LPC Super I/O %s rev ", vendor, desc);
    283 	if (product->idbits >= 12) {
    284 		/* Revision filed is 4 or 3bits */
    285 		aprint_normal("%c\n", 'A' + rev);
    286 	} else
    287 		aprint_normal("0x%02x\n", rev);
    288 
    289 	if (!pmf_device_register(self, wbsio_suspend, NULL))
    290 		aprint_error_dev(self, "couldn't establish power handler\n");
    291 
    292 	sc->sc_smw_valid = false;
    293 	wbsio_rescan(self, "wbsio", NULL);
    294 
    295 #if NGPIO > 0
    296 
    297 	wbsio_rescan(self, "gpiobus", NULL);
    298 #endif
    299 }
    300 
    301 int
    302 wbsio_detach(device_t self, int flags)
    303 {
    304 	struct wbsio_softc *sc = device_private(self);
    305 	int rc;
    306 
    307 	if ((rc = wbsio_wdog_detach(self)) != 0)
    308 		return rc;
    309 
    310 	if ((rc = config_detach_children(self, flags)) != 0)
    311 		return rc;
    312 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, WBSIO_IOSIZE);
    313 	pmf_device_deregister(self);
    314 
    315 #if NGPIO > 0
    316 	if (sc->sc_gpio_dev) {
    317 		bus_space_unmap(sc->sc_iot, sc->sc_gpio_ioh,
    318 		    WBSIO_GPIO_IOSIZE);
    319 	}
    320 
    321 	if (sc->sc_gpio_rt) {
    322 		mutex_destroy(&sc->sc_gpio_lock);
    323 	}
    324 #endif
    325 
    326 	mutex_destroy(&sc->sc_conf_lock);
    327 	return 0;
    328 }
    329 
    330 int
    331 wbsio_rescan(device_t self, const char *ifattr, const int *locators)
    332 {
    333 
    334 #if NGPIO > 0
    335 	if (ifattr_match(ifattr, "gpiobus")) {
    336 		config_search(self, NULL,
    337 		    CFARGS(.search = wbsio_gpio_search,
    338 			   .iattr = ifattr,
    339 			   .locators = locators));
    340 		return 0;
    341 	}
    342 #endif
    343 	config_search(self, NULL,
    344 	    CFARGS(.search = wbsio_search,
    345 		   .iattr = ifattr,
    346 		   .locators = locators));
    347 
    348 	wbsio_wdog_attach(self);
    349 
    350 	return 0;
    351 }
    352 
    353 void
    354 wbsio_childdet(device_t self, device_t child)
    355 {
    356 	struct wbsio_softc *sc = device_private(self);
    357 
    358 	if (sc->sc_lm_dev == child)
    359 		sc->sc_lm_dev = NULL;
    360 }
    361 
    362 static int
    363 wbsio_search(device_t parent, cfdata_t cf, const int *slocs, void *aux)
    364 {
    365 	struct wbsio_softc *sc = device_private(parent);
    366 	const struct wbsio_product *product;
    367 	uint16_t iobase;
    368 	uint16_t devid;
    369 	uint8_t reg0, reg1, rev;
    370 
    371 	/* Enter configuration mode */
    372 	wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    373 
    374 	/* Select HM logical device */
    375 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
    376 
    377 	/*
    378 	 * The address should be 8-byte aligned, but it seems some
    379 	 * BIOSes ignore this.  They get away with it, because
    380 	 * Apparently the hardware simply ignores the lower three
    381 	 * bits.  We do the same here.
    382 	 */
    383 	reg0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_LSB);
    384 	reg1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_MSB);
    385 
    386 	/* Escape from configuration mode */
    387 	wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    388 
    389 	iobase = (reg1 << 8) | (reg0 & ~0x7);
    390 
    391 	if (iobase == 0)
    392 		return -1;
    393 
    394 	/* Enter configuration mode */
    395 	wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    396 	/* Read device ID and revision */
    397 	devid = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
    398 	rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
    399 	/* Escape from configuration mode */
    400 	wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    401 
    402 	if ((product = wbsio_lookup(devid, rev)) == NULL) {
    403 		aprint_error_dev(parent, "%s: Unknown device.\n", __func__);
    404 		return -1;
    405 	}
    406 	devid = WBSIO_MAKEID(devid, rev, product->idbits);
    407 
    408 	sc->sc_ia.ia_nio = 1;
    409 	sc->sc_ia.ia_io = &sc->sc_io;
    410 	sc->sc_ia.ia_io[0].ir_addr = iobase;
    411 	sc->sc_ia.ia_io[0].ir_size = 8;
    412 	sc->sc_ia.ia_niomem = 0;
    413 	sc->sc_ia.ia_nirq = 0;
    414 	sc->sc_ia.ia_ndrq = 0;
    415 	/* Store device-id to ia_aux */
    416 	sc->sc_ia.ia_aux = (void *)(uintptr_t)devid;
    417 	sc->sc_lm_dev = config_attach(parent, cf, &sc->sc_ia, wbsio_print,
    418 	    CFARGS_NONE);
    419 
    420 	return 0;
    421 }
    422 
    423 int
    424 wbsio_print(void *aux, const char *pnp)
    425 {
    426 	struct isa_attach_args *ia = aux;
    427 
    428 	if (pnp)
    429 		aprint_normal("%s", pnp);
    430 	if (ia->ia_io[0].ir_size)
    431 		aprint_normal(" port 0x%x", ia->ia_io[0].ir_addr);
    432 	if (ia->ia_io[0].ir_size > 1)
    433 		aprint_normal("-0x%x", ia->ia_io[0].ir_addr +
    434 		    ia->ia_io[0].ir_size - 1);
    435 	return (UNCONF);
    436 }
    437 
    438 static bool
    439 wbsio_suspend(device_t self, const pmf_qual_t *qual)
    440 {
    441 	struct wbsio_softc *sc = device_private(self);
    442 
    443 	if (sc->sc_smw_valid) {
    444 		if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK)
    445 		    != WDOG_MODE_DISARMED)
    446 			return false;
    447 	}
    448 
    449 	return true;
    450 }
    451 
    452 #if NGPIO > 0
    453 static int
    454 wbsio_gpio_search(device_t parent, cfdata_t cf, const int *slocs, void *aux)
    455 {
    456 	struct wbsio_softc *sc = device_private(parent);
    457 	const struct wbsio_product *product;
    458 	struct gpiobus_attach_args gba;
    459 	uint16_t devid;
    460 	uint8_t rev;
    461 	int i;
    462 
    463 	/* Enter configuration mode */
    464 	wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    465 	/* Read device ID and revision */
    466 	devid = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
    467 	rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
    468 	/* Escape from configuration mode */
    469 	wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    470 
    471 	if ((product = wbsio_lookup(devid, rev)) == NULL) {
    472 		aprint_error_dev(parent, "%s: Unknown device.\n", __func__);
    473 		return -1;
    474 	}
    475 
    476 	sc->sc_gpio_rt = false;
    477 
    478 	switch (product->id) {
    479 	case WBSIO_ID_NCT6779D:
    480 		wbsio_gpio_enable_nct6779d(parent);
    481 		sc->sc_gpio_rt = true;
    482 		break;
    483 	default:
    484 		aprint_error_dev(parent, "%s's GPIO is not supported yet\n",
    485 		    product->str);
    486 		return -1;
    487 	}
    488 
    489 	if (sc->sc_gpio_rt) {
    490 		if (wbsio_gpio_rt_init(sc) != 0) {
    491 			sc->sc_gpio_rt = false;
    492 			return -1;
    493 		}
    494 		sc->sc_gpio_gc.gp_cookie = sc;
    495 		sc->sc_gpio_gc.gp_pin_read = wbsio_gpio_rt_pin_read;
    496 		sc->sc_gpio_gc.gp_pin_write = wbsio_gpio_rt_pin_write;
    497 		sc->sc_gpio_gc.gp_pin_ctl = wbsio_gpio_rt_pin_ctl;
    498 	} else {
    499 		aprint_error_dev(parent,
    500 		    "GPIO indirect access is not supported\n");
    501 		return -1;
    502 	}
    503 
    504 	for (i = 0; i < WBSIO_GPIO_NPINS; i++) {
    505 		sc->sc_gpio_pins[i].pin_num = i;
    506 		sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
    507 		    GPIO_PIN_OUTPUT | GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
    508 
    509 		/* safe defaults */
    510 		sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_INPUT;
    511 		sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
    512 		sc->sc_gpio_gc.gp_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
    513 		sc->sc_gpio_gc.gp_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
    514 	}
    515 
    516 	switch (product->id) {
    517 	case WBSIO_ID_NCT6779D:
    518 		wbsio_gpio_pinconfig_nct6779d(parent);
    519 		break;
    520 	}
    521 
    522 	gba.gba_gc = &sc->sc_gpio_gc;
    523 	gba.gba_pins = sc->sc_gpio_pins;
    524 	gba.gba_npins = WBSIO_GPIO_NPINS;
    525 
    526 	sc->sc_gpio_dev = config_attach(parent, cf, &gba, gpiobus_print,
    527 	    CFARGS_NONE);
    528 
    529 	return 0;
    530 }
    531 
    532 static int
    533 wbsio_gpio_rt_init(struct wbsio_softc *sc)
    534 {
    535 	uint16_t iobase;
    536 	uint8_t reg0, reg1;
    537 
    538 	/* Enter configuration mode */
    539 	wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    540 
    541 	/* Get GPIO Register Table address */
    542 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
    543 	reg0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_GPIO_ADDR_LSB);
    544 	reg1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_GPIO_ADDR_MSB);
    545 	iobase = (reg1 << 8) | (reg0 & ~0x7);
    546 
    547 	/* Escape from configuration mode */
    548 	wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    549 
    550 	if (bus_space_map(sc->sc_iot, iobase, WBSIO_GPIO_IOSIZE,
    551 	    0, &sc->sc_gpio_ioh)) {
    552 		aprint_error_dev(sc->sc_dev,
    553 		    "can't map gpio to i/o space\n");
    554 		return -1;
    555 	}
    556 
    557 	aprint_normal_dev(sc->sc_dev, "GPIO: port 0x%x-0x%x\n",
    558 	    iobase, iobase + WBSIO_GPIO_IOSIZE);
    559 
    560 	mutex_init(&sc->sc_gpio_lock, MUTEX_DEFAULT, IPL_VM);
    561 
    562 	return 0;
    563 }
    564 
    565 static int
    566 wbsio_gpio_rt_read(struct wbsio_softc *sc, int port, int reg)
    567 {
    568 	int v;
    569 
    570 	mutex_enter(&sc->sc_gpio_lock);
    571 
    572 	bus_space_write_1(sc->sc_iot, sc->sc_gpio_ioh,
    573 	    WBSIO_GPIO_GSR, port);
    574 	v = bus_space_read_1(sc->sc_iot, sc->sc_gpio_ioh, reg);
    575 
    576 	mutex_exit(&sc->sc_gpio_lock);
    577 
    578 	return v;
    579 }
    580 
    581 static void
    582 wbsio_gpio_rt_write(struct wbsio_softc *sc, int port, int reg, int value)
    583 {
    584 
    585 	mutex_enter(&sc->sc_gpio_lock);
    586 
    587 	bus_space_write_1(sc->sc_iot, sc->sc_gpio_ioh,
    588 	    WBSIO_GPIO_GSR, port);
    589 	bus_space_write_1(sc->sc_iot, sc->sc_gpio_ioh,
    590 	    reg, value);
    591 
    592 	mutex_exit(&sc->sc_gpio_lock);
    593 }
    594 
    595 static int
    596 wbsio_gpio_rt_pin_read(void *aux, int pin)
    597 {
    598 	struct wbsio_softc *sc = (struct wbsio_softc *)aux;
    599 	int port, shift, data;
    600 
    601 	port = (pin >> 3) & 0x07;
    602 	shift = pin & 0x07;
    603 
    604 	data = wbsio_gpio_rt_read(sc, port, WBSIO_GPIO_DAT);
    605 
    606 	return ((data >> shift) & 0x01);
    607 }
    608 
    609 static void
    610 wbsio_gpio_rt_pin_write(void *aux, int pin, int v)
    611 {
    612 	struct wbsio_softc *sc = (struct wbsio_softc *)aux;
    613 	int port, shift, data;
    614 
    615 	port = (pin >> 3) & 0x07;
    616 	shift = pin & 0x07;
    617 
    618 	data = wbsio_gpio_rt_read(sc, port, WBSIO_GPIO_DAT);
    619 
    620 	if (v == 0)
    621 		data &= ~(1 << shift);
    622 	else if (v == 1)
    623 		data |= (1 << shift);
    624 
    625 	wbsio_gpio_rt_write(sc, port, WBSIO_GPIO_DAT, data);
    626 }
    627 
    628 static void
    629 wbsio_gpio_rt_pin_ctl(void *aux, int pin, int flags)
    630 {
    631 	struct wbsio_softc *sc = (struct wbsio_softc *)aux;
    632 	uint8_t ior, inv;
    633 	int port, shift;
    634 
    635 	port = (pin >> 3) & 0x07;
    636 	shift = pin & 0x07;
    637 
    638 	ior = wbsio_gpio_rt_read(sc, port, WBSIO_GPIO_IOR);
    639 	inv = wbsio_gpio_rt_read(sc, port, WBSIO_GPIO_INV);
    640 
    641 	if (flags & GPIO_PIN_INPUT) {
    642 		ior |= (1 << shift);
    643 		inv &= ~(1 << shift);
    644 	} else if (flags & GPIO_PIN_OUTPUT) {
    645 		ior &= ~(1 << shift);
    646 		inv &= ~(1 << shift);
    647 	} else if (flags & GPIO_PIN_INVIN) {
    648 		ior |= (1 << shift);
    649 		inv |= (1 << shift);
    650 	} else if (flags & GPIO_PIN_INVOUT) {
    651 		ior &= ~(1 << shift);
    652 		inv |= (1 << shift);
    653 	}
    654 
    655 	wbsio_gpio_rt_write(sc, port, WBSIO_GPIO_IOR, ior);
    656 	wbsio_gpio_rt_write(sc, port, WBSIO_GPIO_INV, inv);
    657 }
    658 
    659 static void
    660 wbsio_gpio_enable_nct6779d(device_t parent)
    661 {
    662 	struct wbsio_softc *sc = device_private(parent);
    663 	uint8_t reg, conf;
    664 
    665 	/* Enter configuration mode */
    666 	wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    667 
    668 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
    669 	reg = WBSIO_GPIO_CONF;
    670 	conf = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, reg);
    671 	/* Activate Register Table access */
    672 	conf |= WBSIO_GPIO_BASEADDR;
    673 
    674 	conf |= WBSIO_GPIO0_ENABLE;
    675 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, reg, conf);
    676 
    677 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO1);
    678 	reg = WBSIO_GPIO_CONF;
    679 	conf = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, reg);
    680 	conf |= WBSIO_GPIO1_ENABLE;
    681 	conf |= WBSIO_GPIO2_ENABLE;
    682 	conf |= WBSIO_GPIO3_ENABLE;
    683 	conf |= WBSIO_GPIO4_ENABLE;
    684 	conf |= WBSIO_GPIO5_ENABLE;
    685 	conf |= WBSIO_GPIO6_ENABLE;
    686 	conf |= WBSIO_GPIO7_ENABLE;
    687 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, reg, conf);
    688 
    689 	/* Escape from configuration mode */
    690 	wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    691 }
    692 
    693 static void
    694 wbsio_gpio_pinconfig_nct6779d(device_t parent)
    695 {
    696 	struct wbsio_softc *sc = device_private(parent);
    697 	uint8_t sfr, mfs0, mfs1, mfs2, mfs3;
    698 	uint8_t mfs4, mfs5, mfs6, gopt2, hm_conf;
    699 
    700 	/* Enter configuration mode */
    701 	wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    702 
    703 	/* Strapping Function Result */
    704 	sfr = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_SFR);
    705 	sfr &= ~(WBSIO_SFR_LPT | WBSIO_SFR_DSW | WBSIO_SFR_AMDPWR);
    706 
    707 	/* Read current configuration */
    708 	mfs0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS0);
    709 	mfs1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS1);
    710 	mfs2 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS2);
    711 	mfs3 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS3);
    712 	mfs4 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS4);
    713 	mfs5 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS5);
    714 	mfs6 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS6);
    715 	gopt2 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_GOPT2);
    716 
    717 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
    718 	hm_conf = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_CONF);
    719 
    720 	/* GPIO0 pin configs */
    721 	mfs2 |= WBSIO_NCT6779D_MFS2_GP00;
    722 	mfs2 |= WBSIO_NCT6779D_MFS2_GP01;
    723 	mfs2 |= WBSIO_NCT6779D_MFS2_GP02;
    724 	mfs2 &= ~WBSIO_NCT6779D_MFS2_GP03_MASK;
    725 	mfs2 |= WBSIO_NCT6779D_MFS2_GP04;
    726 	mfs2 |= WBSIO_NCT6779D_MFS2_GP05;
    727 	mfs2 |= WBSIO_NCT6779D_MFS2_GP06;
    728 	mfs3 &= ~WBSIO_NCT6779D_MFS3_GP07_MASK;
    729 
    730 	/* GPIO1 pin configs */
    731 	mfs4 |= WBSIO_NCT6779D_MFS4_GP10_GP17;
    732 
    733 	/* GPIO2 pin configs */
    734 	mfs4 |= WBSIO_NCT6779D_MFS4_GP20_GP21;
    735 	mfs4 |= WBSIO_NCT6779D_MFS4_GP22_GP23;
    736 	mfs1 &= ~WBSIO_NCT6779D_MFS1_GP24_MASK;
    737 	gopt2 &= ~WBSIO_NCT6779D_GOPT2_GP24_MASK;
    738 	mfs4 &= ~WBSIO_NCT6779D_MFS4_GP25_MASK;
    739 	gopt2 &= ~WBSIO_NCT6779D_GOPT2_GP25_MASK;
    740 	mfs6 |= WBSIO_NCT6779D_MFS6_GP26;
    741 	mfs6 &= ~WBSIO_NCT6779D_MFS6_GP27_MASK;
    742 
    743 	/* GPIO3 pin configs */
    744 	mfs0 &= ~WBSIO_NCT6779D_MFS0_GP30_MASK;
    745 	mfs0 |= WBSIO_NCT6779D_MFS0_GP30;
    746 	mfs1 &= ~WBSIO_NCT6779D_MFS1_GP31_MASK;
    747 	mfs0 |= WBSIO_NCT6779D_MFS0_GP31;
    748 	mfs1 &= ~WBSIO_NCT6779D_MFS1_GP32_MASK;
    749 	mfs0 |= WBSIO_NCT6779D_MFS0_GP32;
    750 	mfs6 &= ~WBSIO_NCT6779D_MFS6_GP33_MASK;
    751 	mfs6 |= WBSIO_NCT6779D_MFS6_GP33;
    752 	/* GP34, 35 and 36 are enabled by LPT_EN=0 */
    753 	/* GP37 is not existed */
    754 
    755 	/* GPIO4 pin configs */
    756 	mfs1 |= WBSIO_NCT6779D_MFS1_GP40;
    757 	/* GP41 to GP46 requires LPT_EN=0 */
    758 	mfs0 &= ~WBSIO_NCT6779D_MFS0_GP41_MASK;
    759 	mfs0 |= WBSIO_NCT6779D_MFS0_GP41;
    760 	mfs1 |= WBSIO_NCT6779D_MFS1_GP42;
    761 	mfs1 |= WBSIO_NCT6779D_MFS1_GP42;
    762 	gopt2 |= WBSIO_NCT6779D_GOPT2_GP43;
    763 	mfs1 &= ~WBSIO_NCT6779D_MFS1_GP44_GP45_MASK;
    764 	gopt2 &= ~WBSIO_NCT6779D_GOPT2_GP46_MASK;
    765 	mfs1 |= WBSIO_NCT6779D_MFS1_GP47;
    766 
    767 	/* GPIO5 pin configs */
    768 	/* GP50 to GP55 requires DSW_EN=0 */
    769 	hm_conf &= ~WBSIO_NCT6779D_HM_GP50_MASK;
    770 	/* GP51 is enabled by DSW_EN=0 */
    771 	hm_conf &= ~WBSIO_NCT6779D_HM_GP52_MASK;
    772 	/* GP53 and GP54 are enabled by DSW_EN=0 */
    773 	hm_conf &= ~WBSIO_NCT6779D_HM_GP55_MASK;
    774 	/* GP56 and GP57 are enabled by AMDPWR_EN=0 */
    775 
    776 	/* GPIO6 pin configs are shared with GP43 */
    777 
    778 	/* GPIO7 pin configs */
    779 	/* GP70 to GP73 are enabled by TEST_MODE_EN */
    780 	mfs5 |= WBSIO_NCT6779D_MFS5_GP74;
    781 	mfs5 |= WBSIO_NCT6779D_MFS5_GP75;
    782 	mfs5 |= WBSIO_NCT6779D_MFS5_GP76;
    783 	/* GP77 is not existed */
    784 
    785 	/* Write all pin configs */
    786 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_SFR, sfr);
    787 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS0, mfs0);
    788 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS1, mfs1);
    789 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS2, mfs2);
    790 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS3, mfs3);
    791 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS4, mfs4);
    792 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS5, mfs5);
    793 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS6, mfs6);
    794 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_GOPT2, gopt2);
    795 
    796 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
    797 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_HM_CONF, hm_conf);
    798 
    799 	/* Escape from configuration mode */
    800 	wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    801 }
    802 
    803 #endif /* NGPIO > 0 */
    804 
    805 static void
    806 wbsio_wdog_attach(device_t self)
    807 {
    808 	struct wbsio_softc *sc = device_private(self);
    809 	const struct wbsio_product *product;
    810 	uint8_t gpio, mode;
    811 	uint16_t devid;
    812 	uint8_t rev;
    813 
    814 	if (sc->sc_smw_valid)
    815 		return;		/* watchdog already attached */
    816 
    817 	wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    818 	devid = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
    819 	rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
    820 	wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    821 
    822 	if ((product = wbsio_lookup(devid, rev)) == NULL) {
    823 		return;
    824 	}
    825 
    826 	switch (product->id) {
    827 	case WBSIO_ID_NCT6779D:
    828 		break;
    829 	default:
    830 		/* WDT is not supoorted */
    831 		return;
    832 	}
    833 
    834 	wbsio_wdog_setcounter(sc, WBSIO_WDT_CNTR_STOP);
    835 
    836 	wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    837 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
    838 
    839 	gpio = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_GPIO_CONF);
    840 	mode = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_MODE);
    841 
    842 	gpio |= WBSIO_GPIO0_WDT1;
    843 
    844 	mode &= ~WBSIO_WDT_MODE_FASTER;
    845 	mode &= ~WBSIO_WDT_MODE_MINUTES;
    846 	mode &= ~WBSIO_WDT_MODE_KBCRST;
    847 	mode &= ~WBSIO_WDT_MODE_LEVEL;
    848 
    849 	/* initialize WDT mode */
    850 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_MODE, mode);
    851 	/* Activate WDT1 function */
    852 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_GPIO_CONF, gpio);
    853 
    854 	wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    855 
    856 	sc->sc_smw.smw_name = device_xname(self);
    857 	sc->sc_smw.smw_cookie = sc;
    858 	sc->sc_smw.smw_setmode = wbsio_wdog_setmode;
    859 	sc->sc_smw.smw_tickle = wbsio_wdog_tickle;
    860 	sc->sc_smw.smw_period = WBSIO_WDT_CNTR_MAX;
    861 
    862 	if (sysmon_wdog_register(&sc->sc_smw))
    863 		aprint_error_dev(self, "couldn't register with sysmon\n");
    864 	else
    865 		sc->sc_smw_valid = true;
    866 }
    867 
    868 static int
    869 wbsio_wdog_detach(device_t self)
    870 {
    871 	struct wbsio_softc *sc = device_private(self);
    872 	int error;
    873 
    874 	error = 0;
    875 
    876 	if (sc->sc_smw_valid) {
    877 		if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK)
    878 		    != WDOG_MODE_DISARMED)
    879 			return EBUSY;
    880 
    881 		error = sysmon_wdog_unregister(&sc->sc_smw);
    882 	}
    883 
    884 	if (!error)
    885 		sc->sc_smw_valid = false;
    886 
    887 	return error;
    888 }
    889 
    890 static int
    891 wbsio_wdog_setmode(struct sysmon_wdog *smw)
    892 {
    893 
    894 	switch(smw->smw_mode & WDOG_MODE_MASK) {
    895 	case WDOG_MODE_DISARMED:
    896 		wbsio_wdog_setcounter(smw->smw_cookie, WBSIO_WDT_CNTR_STOP);
    897 		wbsio_wdog_clear_timeout(smw->smw_cookie);
    898 		break;
    899 	default:
    900 		if (smw->smw_period > WBSIO_WDT_CNTR_MAX
    901 		    || smw->smw_period == 0)
    902 			return EINVAL;
    903 
    904 		wbsio_wdog_setcounter(smw->smw_cookie, smw->smw_period);
    905 	}
    906 
    907 	return 0;
    908 }
    909 
    910 static void
    911 wbsio_wdog_setcounter(struct wbsio_softc *sc, uint8_t period)
    912 {
    913 
    914 	KASSERT(!mutex_owned(&sc->sc_conf_lock));
    915 
    916 	wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    917 
    918 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
    919 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_CNTR, period);
    920 
    921 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
    922 
    923 
    924 	wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    925 }
    926 
    927 static void
    928 wbsio_wdog_clear_timeout(struct wbsio_softc *sc)
    929 {
    930 	uint8_t st;
    931 
    932 	KASSERT(!mutex_owned(&sc->sc_conf_lock));
    933 
    934 	wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    935 
    936 	st = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_STAT);
    937 	st &= ~WBSIO_WDT_STAT_TIMEOUT;
    938 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_STAT, st);
    939 
    940 	wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
    941 }
    942 
    943 static int
    944 wbsio_wdog_tickle(struct sysmon_wdog *smw)
    945 {
    946 
    947 	wbsio_wdog_setcounter(smw->smw_cookie, smw->smw_period);
    948 
    949 	return 0;
    950 }
    951 
    952 
    953 MODULE(MODULE_CLASS_DRIVER, wbsio, "sysmon_wdog");
    954 
    955 #ifdef _MODULE
    956 #include "ioconf.c"
    957 #endif
    958 
    959 static int
    960 wbsio_modcmd(modcmd_t cmd, void *opaque)
    961 {
    962 	switch (cmd) {
    963 	case MODULE_CMD_INIT:
    964 #ifdef _MODULE
    965 		return config_init_component(cfdriver_ioconf_wbsio,
    966 		    cfattach_ioconf_wbsio, cfdata_ioconf_wbsio);
    967 #else
    968 		return 0;
    969 #endif
    970 	case MODULE_CMD_FINI:
    971 #ifdef _MODULE
    972 		return config_fini_component(cfdriver_ioconf_wbsio,
    973 		    cfattach_ioconf_wbsio, cfdata_ioconf_wbsio);
    974 #else
    975 		return 0;
    976 #endif
    977 	default:
    978 		return ENOTTY;
    979 	}
    980 }
    981