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