Home | History | Annotate | Line # | Download | only in pci
elan520.c revision 1.16.38.1
      1 /*	$NetBSD: elan520.c,v 1.16.38.1 2007/12/26 19:42:23 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Device driver for the AMD Elan SC520 System Controller.  This attaches
     41  * where the "pchb" driver might normally attach, and provides support for
     42  * extra features on the SC520, such as the watchdog timer and GPIO.
     43  *
     44  * Information about the GP bus echo bug work-around is from code posted
     45  * to the "soekris-tech" mailing list by Jasper Wallace.
     46  */
     47 
     48 #include <sys/cdefs.h>
     49 
     50 __KERNEL_RCSID(0, "$NetBSD: elan520.c,v 1.16.38.1 2007/12/26 19:42:23 ad Exp $");
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/device.h>
     55 #include <sys/gpio.h>
     56 #include <sys/mutex.h>
     57 #include <sys/wdog.h>
     58 
     59 #include <uvm/uvm_extern.h>
     60 
     61 #include <machine/bus.h>
     62 
     63 #include <dev/pci/pcivar.h>
     64 
     65 #include <dev/pci/pcidevs.h>
     66 
     67 #include "gpio.h"
     68 #if NGPIO > 0
     69 #include <dev/gpio/gpiovar.h>
     70 #endif
     71 
     72 #include <arch/i386/pci/elan520reg.h>
     73 
     74 #include <dev/sysmon/sysmonvar.h>
     75 
     76 struct elansc_softc {
     77 	struct device sc_dev;
     78 	bus_space_tag_t sc_memt;
     79 	bus_space_handle_t sc_memh;
     80 	int sc_echobug;
     81 
     82 	kmutex_t sc_mtx;
     83 
     84 	struct sysmon_wdog sc_smw;
     85 #if NGPIO > 0
     86 	/* GPIO interface */
     87 	struct gpio_chipset_tag sc_gpio_gc;
     88 	gpio_pin_t sc_gpio_pins[ELANSC_PIO_NPINS];
     89 #endif
     90 };
     91 
     92 #if NGPIO > 0
     93 static int	elansc_gpio_pin_read(void *, int);
     94 static void	elansc_gpio_pin_write(void *, int, int);
     95 static void	elansc_gpio_pin_ctl(void *, int, int);
     96 #endif
     97 
     98 static void
     99 elansc_childdetached(device_t self, device_t child)
    100 {
    101 	/* elansc does not presently keep a pointer to children such
    102 	 * as the gpio, so there is nothing to do.
    103 	 */
    104 }
    105 
    106 static void
    107 elansc_wdogctl_write(struct elansc_softc *sc, uint16_t val)
    108 {
    109 	uint8_t echo_mode = 0; /* XXX: gcc */
    110 
    111 	KASSERT(mutex_owned(&sc->sc_mtx));
    112 
    113 	/* Switch off GP bus echo mode if we need to. */
    114 	if (sc->sc_echobug) {
    115 		echo_mode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    116 		    MMCR_GPECHO);
    117 		bus_space_write_1(sc->sc_memt, sc->sc_memh,
    118 		    MMCR_GPECHO, echo_mode & ~GPECHO_GP_ECHO_ENB);
    119 	}
    120 
    121 	/* Unlock the register. */
    122 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    123 	    WDTMRCTL_UNLOCK1);
    124 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    125 	    WDTMRCTL_UNLOCK2);
    126 
    127 	/* Write the value. */
    128 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL, val);
    129 
    130 	/* Switch GP bus echo mode back. */
    131 	if (sc->sc_echobug)
    132 		bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_GPECHO,
    133 		    echo_mode);
    134 }
    135 
    136 static void
    137 elansc_wdogctl_reset(struct elansc_softc *sc)
    138 {
    139 	uint8_t echo_mode = 0/* XXX: gcc */;
    140 
    141 	KASSERT(mutex_owned(&sc->sc_mtx));
    142 
    143 	/* Switch off GP bus echo mode if we need to. */
    144 	if (sc->sc_echobug) {
    145 		echo_mode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    146 		    MMCR_GPECHO);
    147 		bus_space_write_1(sc->sc_memt, sc->sc_memh,
    148 		    MMCR_GPECHO, echo_mode & ~GPECHO_GP_ECHO_ENB);
    149 	}
    150 
    151 	/* Reset the watchdog. */
    152 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    153 	    WDTMRCTL_RESET1);
    154 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    155 	    WDTMRCTL_RESET2);
    156 
    157 	/* Switch GP bus echo mode back. */
    158 	if (sc->sc_echobug)
    159 		bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_GPECHO,
    160 		    echo_mode);
    161 }
    162 
    163 static const struct {
    164 	int	period;		/* whole seconds */
    165 	uint16_t exp;		/* exponent select */
    166 } elansc_wdog_periods[] = {
    167 	{ 1,	WDTMRCTL_EXP_SEL25 },
    168 	{ 2,	WDTMRCTL_EXP_SEL26 },
    169 	{ 4,	WDTMRCTL_EXP_SEL27 },
    170 	{ 8,	WDTMRCTL_EXP_SEL28 },
    171 	{ 16,	WDTMRCTL_EXP_SEL29 },
    172 	{ 32,	WDTMRCTL_EXP_SEL30 },
    173 	{ 0,	0 },
    174 };
    175 
    176 static int
    177 elansc_wdog_arm(struct elansc_softc *sc)
    178 {
    179 	struct sysmon_wdog *smw = &sc->sc_smw;
    180 	int i;
    181 	uint16_t exp_sel = 0; /* XXX: gcc */
    182 
    183 	KASSERT(mutex_owned(&sc->sc_mtx));
    184 
    185 	if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
    186 		smw->smw_period = 32;
    187 		exp_sel = WDTMRCTL_EXP_SEL30;
    188 	} else {
    189 		for (i = 0; elansc_wdog_periods[i].period != 0; i++) {
    190 			if (elansc_wdog_periods[i].period ==
    191 			    smw->smw_period) {
    192 				exp_sel = elansc_wdog_periods[i].exp;
    193 				break;
    194 			}
    195 		}
    196 		if (elansc_wdog_periods[i].period == 0)
    197 			return EINVAL;
    198 	}
    199 	elansc_wdogctl_write(sc, WDTMRCTL_ENB |
    200 	    WDTMRCTL_WRST_ENB | exp_sel);
    201 	elansc_wdogctl_reset(sc);
    202 	return 0;
    203 }
    204 
    205 static int
    206 elansc_wdog_setmode(struct sysmon_wdog *smw)
    207 {
    208 	struct elansc_softc *sc = smw->smw_cookie;
    209 	int rc = 0;
    210 
    211 	mutex_enter(&sc->sc_mtx);
    212 
    213 	if (!device_is_active(&sc->sc_dev))
    214 		rc = EBUSY;
    215 	else if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    216 		elansc_wdogctl_write(sc,
    217 		    WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
    218 	} else
    219 		rc = elansc_wdog_arm(sc);
    220 
    221 	mutex_exit(&sc->sc_mtx);
    222 	return rc;
    223 }
    224 
    225 static int
    226 elansc_wdog_tickle(struct sysmon_wdog *smw)
    227 {
    228 	struct elansc_softc *sc = smw->smw_cookie;
    229 
    230 	mutex_enter(&sc->sc_mtx);
    231 	elansc_wdogctl_reset(sc);
    232 	mutex_exit(&sc->sc_mtx);
    233 	return 0;
    234 }
    235 
    236 static int
    237 elansc_match(struct device *parent, struct cfdata *match,
    238     void *aux)
    239 {
    240 	struct pci_attach_args *pa = aux;
    241 
    242 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
    243 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_SC520_SC)
    244 		return (10);	/* beat pchb */
    245 
    246 	return (0);
    247 }
    248 
    249 static const char *elansc_speeds[] = {
    250 	"(reserved 00)",
    251 	"100MHz",
    252 	"133MHz",
    253 	"(reserved 11)",
    254 };
    255 
    256 static bool
    257 elansc_suspend(device_t dev)
    258 {
    259 	bool rc;
    260 	struct elansc_softc *sc = device_private(dev);
    261 
    262 	mutex_enter(&sc->sc_mtx);
    263 	rc = ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED);
    264 	mutex_exit(&sc->sc_mtx);
    265 	if (!rc)
    266 		aprint_debug_dev(dev, "watchdog enabled, suspend forbidden");
    267 	return rc;
    268 }
    269 
    270 static bool
    271 elansc_resume(device_t dev)
    272 {
    273 	struct elansc_softc *sc = device_private(dev);
    274 
    275 	mutex_enter(&sc->sc_mtx);
    276 	/* Set up the watchdog registers with some defaults. */
    277 	elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
    278 
    279 	/* ...and clear it. */
    280 	elansc_wdogctl_reset(sc);
    281 	mutex_exit(&sc->sc_mtx);
    282 
    283 	return true;
    284 }
    285 
    286 static int
    287 elansc_detach(device_t self, int flags)
    288 {
    289 	int rc;
    290 	struct elansc_softc *sc = device_private(self);
    291 
    292 	if ((rc = config_detach_children(self, flags)) != 0)
    293 		return rc;
    294 
    295 	pmf_device_deregister(self);
    296 
    297 	if ((rc = sysmon_wdog_unregister(&sc->sc_smw)) != 0) {
    298 		if (rc == ERESTART)
    299 			rc = EINTR;
    300 		return rc;
    301 	}
    302 
    303 	mutex_enter(&sc->sc_mtx);
    304 
    305 	/* Set up the watchdog registers with some defaults. */
    306 	elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
    307 
    308 	/* ...and clear it. */
    309 	elansc_wdogctl_reset(sc);
    310 
    311 	bus_space_unmap(sc->sc_memt, sc->sc_memh, PAGE_SIZE);
    312 
    313 	mutex_exit(&sc->sc_mtx);
    314 	mutex_destroy(&sc->sc_mtx);
    315 	return 0;
    316 }
    317 
    318 static void
    319 elansc_attach(struct device *parent, struct device *self, void *aux)
    320 {
    321 	struct elansc_softc *sc = device_private(self);
    322 	struct pci_attach_args *pa = aux;
    323 	uint16_t rev;
    324 	uint8_t ressta, cpuctl;
    325 #if NGPIO > 0
    326 	struct gpiobus_attach_args gba;
    327 	int pin;
    328 	int reg, shift;
    329 	uint16_t data;
    330 #endif
    331 
    332 	aprint_naive(": System Controller\n");
    333 	aprint_normal(": AMD Elan SC520 System Controller\n");
    334 
    335 	sc->sc_memt = pa->pa_memt;
    336 	if (bus_space_map(sc->sc_memt, MMCR_BASE_ADDR, PAGE_SIZE, 0,
    337 	    &sc->sc_memh) != 0) {
    338 		aprint_error("%s: unable to map registers\n",
    339 		    sc->sc_dev.dv_xname);
    340 		return;
    341 	}
    342 
    343 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_HIGH);
    344 
    345 	rev = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_REVID);
    346 	cpuctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_CPUCTL);
    347 
    348 	aprint_normal("%s: product %d stepping %d.%d, CPU clock %s\n",
    349 	    sc->sc_dev.dv_xname,
    350 	    (rev & REVID_PRODID) >> REVID_PRODID_SHIFT,
    351 	    (rev & REVID_MAJSTEP) >> REVID_MAJSTEP_SHIFT,
    352 	    (rev & REVID_MINSTEP),
    353 	    elansc_speeds[cpuctl & CPUCTL_CPU_CLK_SPD_MASK]);
    354 
    355 	/*
    356 	 * SC520 rev A1 has a bug that affects the watchdog timer.  If
    357 	 * the GP bus echo mode is enabled, writing to the watchdog control
    358 	 * register is blocked.
    359 	 *
    360 	 * The BIOS in some systems (e.g. the Soekris net4501) enables
    361 	 * GP bus echo for various reasons, so we need to switch it off
    362 	 * when we talk to the watchdog timer.
    363 	 *
    364 	 * XXX The step 1.1 (B1?) in my Soekris net4501 also has this
    365 	 * XXX problem, so we'll just enable it for all Elan SC520s
    366 	 * XXX for now.  --thorpej (at) NetBSD.org
    367 	 */
    368 	if (1 || rev == ((PRODID_ELAN_SC520 << REVID_PRODID_SHIFT) |
    369 		    (0 << REVID_MAJSTEP_SHIFT) | (1)))
    370 		sc->sc_echobug = 1;
    371 
    372 	/*
    373 	 * Determine cause of the last reset, and issue a warning if it
    374 	 * was due to watchdog expiry.
    375 	 */
    376 	ressta = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_RESSTA);
    377 	if (ressta & RESSTA_WDT_RST_DET)
    378 		aprint_error(
    379 		    "%s: WARNING: LAST RESET DUE TO WATCHDOG EXPIRATION!\n",
    380 		    sc->sc_dev.dv_xname);
    381 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_RESSTA, ressta);
    382 
    383 	/* Set up the watchdog registers with some defaults. */
    384 	elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
    385 
    386 	/* ...and clear it. */
    387 	elansc_wdogctl_reset(sc);
    388 
    389 	pmf_device_register(self, elansc_suspend, elansc_resume);
    390 
    391 #if NGPIO > 0
    392 	/* Initialize GPIO pins array */
    393 	for (pin = 0; pin < ELANSC_PIO_NPINS; pin++) {
    394 		sc->sc_gpio_pins[pin].pin_num = pin;
    395 		sc->sc_gpio_pins[pin].pin_caps = GPIO_PIN_INPUT |
    396 		    GPIO_PIN_OUTPUT;
    397 
    398 		/* Read initial state */
    399 		reg = (pin < 16 ? MMCR_PIODIR15_0 : MMCR_PIODIR31_16);
    400 		shift = pin % 16;
    401 		data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
    402 		if ((data & (1 << shift)) == 0)
    403 			sc->sc_gpio_pins[pin].pin_flags = GPIO_PIN_INPUT;
    404 		else
    405 			sc->sc_gpio_pins[pin].pin_flags = GPIO_PIN_OUTPUT;
    406 		if (elansc_gpio_pin_read(sc, pin) == 0)
    407 			sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_LOW;
    408 		else
    409 			sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_HIGH;
    410 	}
    411 
    412 	/* Create controller tag */
    413 	sc->sc_gpio_gc.gp_cookie = sc;
    414 	sc->sc_gpio_gc.gp_pin_read = elansc_gpio_pin_read;
    415 	sc->sc_gpio_gc.gp_pin_write = elansc_gpio_pin_write;
    416 	sc->sc_gpio_gc.gp_pin_ctl = elansc_gpio_pin_ctl;
    417 
    418 	gba.gba_gc = &sc->sc_gpio_gc;
    419 	gba.gba_pins = sc->sc_gpio_pins;
    420 	gba.gba_npins = ELANSC_PIO_NPINS;
    421 
    422 	/* Attach GPIO framework */
    423 	config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);
    424 #endif /* NGPIO */
    425 
    426 	/*
    427 	 * Hook up the watchdog timer.
    428 	 */
    429 	sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
    430 	sc->sc_smw.smw_cookie = sc;
    431 	sc->sc_smw.smw_setmode = elansc_wdog_setmode;
    432 	sc->sc_smw.smw_tickle = elansc_wdog_tickle;
    433 	sc->sc_smw.smw_period = 32;	/* actually 32.54 */
    434 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    435 		aprint_error("%s: unable to register watchdog with sysmon\n",
    436 		    sc->sc_dev.dv_xname);
    437 }
    438 
    439 CFATTACH_DECL2(elansc, sizeof(struct elansc_softc),
    440     elansc_match, elansc_attach, elansc_detach, NULL, NULL,
    441     elansc_childdetached);
    442 
    443 #if NGPIO > 0
    444 static int
    445 elansc_gpio_pin_read(void *arg, int pin)
    446 {
    447 	struct elansc_softc *sc = arg;
    448 	int reg, shift;
    449 	uint16_t data;
    450 
    451 	reg = (pin < 16 ? MMCR_PIODATA15_0 : MMCR_PIODATA31_16);
    452 	shift = pin % 16;
    453 
    454 	mutex_enter(&sc->sc_mtx);
    455 	data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
    456 	mutex_exit(&sc->sc_mtx);
    457 
    458 	return ((data >> shift) & 0x1);
    459 }
    460 
    461 static void
    462 elansc_gpio_pin_write(void *arg, int pin, int value)
    463 {
    464 	struct elansc_softc *sc = arg;
    465 	int reg, shift;
    466 	uint16_t data;
    467 
    468 	reg = (pin < 16 ? MMCR_PIODATA15_0 : MMCR_PIODATA31_16);
    469 	shift = pin % 16;
    470 
    471 	mutex_enter(&sc->sc_mtx);
    472 	data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
    473 	if (value == 0)
    474 		data &= ~(1 << shift);
    475 	else if (value == 1)
    476 		data |= (1 << shift);
    477 
    478 	bus_space_write_2(sc->sc_memt, sc->sc_memh, reg, data);
    479 	mutex_exit(&sc->sc_mtx);
    480 }
    481 
    482 static void
    483 elansc_gpio_pin_ctl(void *arg, int pin, int flags)
    484 {
    485 	struct elansc_softc *sc = arg;
    486 	int reg, shift;
    487 	uint16_t data;
    488 
    489 	reg = (pin < 16 ? MMCR_PIODIR15_0 : MMCR_PIODIR31_16);
    490 	shift = pin % 16;
    491 	mutex_enter(&sc->sc_mtx);
    492 	data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
    493 	if (flags & GPIO_PIN_INPUT)
    494 		data &= ~(1 << shift);
    495 	if (flags & GPIO_PIN_OUTPUT)
    496 		data |= (1 << shift);
    497 
    498 	bus_space_write_2(sc->sc_memt, sc->sc_memh, reg, data);
    499 	mutex_exit(&sc->sc_mtx);
    500 }
    501 #endif /* NGPIO */
    502