Home | History | Annotate | Line # | Download | only in pci
elan520.c revision 1.15
      1 /*	$NetBSD: elan520.c,v 1.15 2006/10/12 01:30:43 christos 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.15 2006/10/12 01:30:43 christos Exp $");
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/device.h>
     55 #include <sys/wdog.h>
     56 #include <sys/gpio.h>
     57 
     58 #include <uvm/uvm_extern.h>
     59 
     60 #include <machine/bus.h>
     61 
     62 #include <dev/pci/pcivar.h>
     63 
     64 #include <dev/pci/pcidevs.h>
     65 
     66 #include "gpio.h"
     67 #if NGPIO > 0
     68 #include <dev/gpio/gpiovar.h>
     69 #endif
     70 
     71 #include <arch/i386/pci/elan520reg.h>
     72 
     73 #include <dev/sysmon/sysmonvar.h>
     74 
     75 struct elansc_softc {
     76 	struct device sc_dev;
     77 	bus_space_tag_t sc_memt;
     78 	bus_space_handle_t sc_memh;
     79 	int sc_echobug;
     80 
     81 	struct sysmon_wdog sc_smw;
     82 #if NGPIO > 0
     83 	/* GPIO interface */
     84 	struct gpio_chipset_tag sc_gpio_gc;
     85 	gpio_pin_t sc_gpio_pins[ELANSC_PIO_NPINS];
     86 #endif
     87 };
     88 
     89 #if NGPIO > 0
     90 static int	elansc_gpio_pin_read(void *, int);
     91 static void	elansc_gpio_pin_write(void *, int, int);
     92 static void	elansc_gpio_pin_ctl(void *, int, int);
     93 #endif
     94 
     95 static void
     96 elansc_wdogctl_write(struct elansc_softc *sc, uint16_t val)
     97 {
     98 	int s;
     99 	uint8_t echo_mode = 0; /* XXX: gcc */
    100 
    101 	s = splhigh();
    102 
    103 	/* Switch off GP bus echo mode if we need to. */
    104 	if (sc->sc_echobug) {
    105 		echo_mode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    106 		    MMCR_GPECHO);
    107 		bus_space_write_1(sc->sc_memt, sc->sc_memh,
    108 		    MMCR_GPECHO, echo_mode & ~GPECHO_GP_ECHO_ENB);
    109 	}
    110 
    111 	/* Unlock the register. */
    112 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    113 	    WDTMRCTL_UNLOCK1);
    114 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    115 	    WDTMRCTL_UNLOCK2);
    116 
    117 	/* Write the value. */
    118 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL, val);
    119 
    120 	/* Switch GP bus echo mode back. */
    121 	if (sc->sc_echobug)
    122 		bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_GPECHO,
    123 		    echo_mode);
    124 
    125 	splx(s);
    126 }
    127 
    128 static void
    129 elansc_wdogctl_reset(struct elansc_softc *sc)
    130 {
    131 	int s;
    132 	uint8_t echo_mode = 0/* XXX: gcc */;
    133 
    134 	s = splhigh();
    135 
    136 	/* Switch off GP bus echo mode if we need to. */
    137 	if (sc->sc_echobug) {
    138 		echo_mode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    139 		    MMCR_GPECHO);
    140 		bus_space_write_1(sc->sc_memt, sc->sc_memh,
    141 		    MMCR_GPECHO, echo_mode & ~GPECHO_GP_ECHO_ENB);
    142 	}
    143 
    144 	/* Reset the watchdog. */
    145 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    146 	    WDTMRCTL_RESET1);
    147 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    148 	    WDTMRCTL_RESET2);
    149 
    150 	/* Switch GP bus echo mode back. */
    151 	if (sc->sc_echobug)
    152 		bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_GPECHO,
    153 		    echo_mode);
    154 
    155 	splx(s);
    156 }
    157 
    158 static const struct {
    159 	int	period;		/* whole seconds */
    160 	uint16_t exp;		/* exponent select */
    161 } elansc_wdog_periods[] = {
    162 	{ 1,	WDTMRCTL_EXP_SEL25 },
    163 	{ 2,	WDTMRCTL_EXP_SEL26 },
    164 	{ 4,	WDTMRCTL_EXP_SEL27 },
    165 	{ 8,	WDTMRCTL_EXP_SEL28 },
    166 	{ 16,	WDTMRCTL_EXP_SEL29 },
    167 	{ 32,	WDTMRCTL_EXP_SEL30 },
    168 	{ 0,	0 },
    169 };
    170 
    171 static int
    172 elansc_wdog_setmode(struct sysmon_wdog *smw)
    173 {
    174 	struct elansc_softc *sc = smw->smw_cookie;
    175 	int i;
    176 	uint16_t exp_sel = 0; /* XXX: gcc */
    177 
    178 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    179 		elansc_wdogctl_write(sc,
    180 		    WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
    181 	} else {
    182 		if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
    183 			smw->smw_period = 32;
    184 			exp_sel = WDTMRCTL_EXP_SEL30;
    185 		} else {
    186 			for (i = 0; elansc_wdog_periods[i].period != 0; i++) {
    187 				if (elansc_wdog_periods[i].period ==
    188 				    smw->smw_period) {
    189 					exp_sel = elansc_wdog_periods[i].exp;
    190 					break;
    191 				}
    192 			}
    193 			if (elansc_wdog_periods[i].period == 0)
    194 				return (EINVAL);
    195 		}
    196 		elansc_wdogctl_write(sc, WDTMRCTL_ENB |
    197 		    WDTMRCTL_WRST_ENB | exp_sel);
    198 		elansc_wdogctl_reset(sc);
    199 	}
    200 	return (0);
    201 }
    202 
    203 static int
    204 elansc_wdog_tickle(struct sysmon_wdog *smw)
    205 {
    206 	struct elansc_softc *sc = smw->smw_cookie;
    207 
    208 	elansc_wdogctl_reset(sc);
    209 	return (0);
    210 }
    211 
    212 static int
    213 elansc_match(struct device *parent __unused, struct cfdata *match __unused,
    214     void *aux)
    215 {
    216 	struct pci_attach_args *pa = aux;
    217 
    218 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
    219 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_SC520_SC)
    220 		return (10);	/* beat pchb */
    221 
    222 	return (0);
    223 }
    224 
    225 static const char *elansc_speeds[] = {
    226 	"(reserved 00)",
    227 	"100MHz",
    228 	"133MHz",
    229 	"(reserved 11)",
    230 };
    231 
    232 static void
    233 elansc_attach(struct device *parent __unused, struct device *self, void *aux)
    234 {
    235 	struct elansc_softc *sc = (void *) self;
    236 	struct pci_attach_args *pa = aux;
    237 	uint16_t rev;
    238 	uint8_t ressta, cpuctl;
    239 #if NGPIO > 0
    240 	struct gpiobus_attach_args gba;
    241 	int pin;
    242 	int reg, shift;
    243 	uint16_t data;
    244 #endif
    245 
    246 	aprint_naive(": System Controller\n");
    247 	aprint_normal(": AMD Elan SC520 System Controller\n");
    248 
    249 	sc->sc_memt = pa->pa_memt;
    250 	if (bus_space_map(sc->sc_memt, MMCR_BASE_ADDR, PAGE_SIZE, 0,
    251 	    &sc->sc_memh) != 0) {
    252 		aprint_error("%s: unable to map registers\n",
    253 		    sc->sc_dev.dv_xname);
    254 		return;
    255 	}
    256 
    257 	rev = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_REVID);
    258 	cpuctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_CPUCTL);
    259 
    260 	aprint_normal("%s: product %d stepping %d.%d, CPU clock %s\n",
    261 	    sc->sc_dev.dv_xname,
    262 	    (rev & REVID_PRODID) >> REVID_PRODID_SHIFT,
    263 	    (rev & REVID_MAJSTEP) >> REVID_MAJSTEP_SHIFT,
    264 	    (rev & REVID_MINSTEP),
    265 	    elansc_speeds[cpuctl & CPUCTL_CPU_CLK_SPD_MASK]);
    266 
    267 	/*
    268 	 * SC520 rev A1 has a bug that affects the watchdog timer.  If
    269 	 * the GP bus echo mode is enabled, writing to the watchdog control
    270 	 * register is blocked.
    271 	 *
    272 	 * The BIOS in some systems (e.g. the Soekris net4501) enables
    273 	 * GP bus echo for various reasons, so we need to switch it off
    274 	 * when we talk to the watchdog timer.
    275 	 *
    276 	 * XXX The step 1.1 (B1?) in my Soekris net4501 also has this
    277 	 * XXX problem, so we'll just enable it for all Elan SC520s
    278 	 * XXX for now.  --thorpej (at) NetBSD.org
    279 	 */
    280 	if (1 || rev == ((PRODID_ELAN_SC520 << REVID_PRODID_SHIFT) |
    281 		    (0 << REVID_MAJSTEP_SHIFT) | (1)))
    282 		sc->sc_echobug = 1;
    283 
    284 	/*
    285 	 * Determine cause of the last reset, and issue a warning if it
    286 	 * was due to watchdog expiry.
    287 	 */
    288 	ressta = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_RESSTA);
    289 	if (ressta & RESSTA_WDT_RST_DET)
    290 		aprint_error(
    291 		    "%s: WARNING: LAST RESET DUE TO WATCHDOG EXPIRATION!\n",
    292 		    sc->sc_dev.dv_xname);
    293 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_RESSTA, ressta);
    294 
    295 	/*
    296 	 * Hook up the watchdog timer.
    297 	 */
    298 	sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
    299 	sc->sc_smw.smw_cookie = sc;
    300 	sc->sc_smw.smw_setmode = elansc_wdog_setmode;
    301 	sc->sc_smw.smw_tickle = elansc_wdog_tickle;
    302 	sc->sc_smw.smw_period = 32;	/* actually 32.54 */
    303 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    304 		aprint_error("%s: unable to register watchdog with sysmon\n",
    305 		    sc->sc_dev.dv_xname);
    306 
    307 	/* Set up the watchdog registers with some defaults. */
    308 	elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
    309 
    310 	/* ...and clear it. */
    311 	elansc_wdogctl_reset(sc);
    312 
    313 #if NGPIO > 0
    314 	/* Initialize GPIO pins array */
    315 	for (pin = 0; pin < ELANSC_PIO_NPINS; pin++) {
    316 		sc->sc_gpio_pins[pin].pin_num = pin;
    317 		sc->sc_gpio_pins[pin].pin_caps = GPIO_PIN_INPUT |
    318 		    GPIO_PIN_OUTPUT;
    319 
    320 		/* Read initial state */
    321 		reg = (pin < 16 ? MMCR_PIODIR15_0 : MMCR_PIODIR31_16);
    322 		shift = pin % 16;
    323 		data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
    324 		if ((data & (1 << shift)) == 0)
    325 			sc->sc_gpio_pins[pin].pin_flags = GPIO_PIN_INPUT;
    326 		else
    327 			sc->sc_gpio_pins[pin].pin_flags = GPIO_PIN_OUTPUT;
    328 		if (elansc_gpio_pin_read(sc, pin) == 0)
    329 			sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_LOW;
    330 		else
    331 			sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_HIGH;
    332 	}
    333 
    334 	/* Create controller tag */
    335 	sc->sc_gpio_gc.gp_cookie = sc;
    336 	sc->sc_gpio_gc.gp_pin_read = elansc_gpio_pin_read;
    337 	sc->sc_gpio_gc.gp_pin_write = elansc_gpio_pin_write;
    338 	sc->sc_gpio_gc.gp_pin_ctl = elansc_gpio_pin_ctl;
    339 
    340 	gba.gba_gc = &sc->sc_gpio_gc;
    341 	gba.gba_pins = sc->sc_gpio_pins;
    342 	gba.gba_npins = ELANSC_PIO_NPINS;
    343 
    344 	/* Attach GPIO framework */
    345 	config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);
    346 #endif /* NGPIO */
    347 }
    348 
    349 CFATTACH_DECL(elansc, sizeof(struct elansc_softc),
    350     elansc_match, elansc_attach, NULL, NULL);
    351 
    352 #if NGPIO > 0
    353 static int
    354 elansc_gpio_pin_read(void *arg, int pin)
    355 {
    356 	struct elansc_softc *sc = arg;
    357 	int reg, shift;
    358 	uint16_t data;
    359 
    360 	reg = (pin < 16 ? MMCR_PIODATA15_0 : MMCR_PIODATA31_16);
    361 	shift = pin % 16;
    362 	data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
    363 
    364 	return ((data >> shift) & 0x1);
    365 }
    366 
    367 static void
    368 elansc_gpio_pin_write(void *arg, int pin, int value)
    369 {
    370 	struct elansc_softc *sc = arg;
    371 	int reg, shift;
    372 	uint16_t data;
    373 
    374 	reg = (pin < 16 ? MMCR_PIODATA15_0 : MMCR_PIODATA31_16);
    375 	shift = pin % 16;
    376 	data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
    377 	if (value == 0)
    378 		data &= ~(1 << shift);
    379 	else if (value == 1)
    380 		data |= (1 << shift);
    381 
    382 	bus_space_write_2(sc->sc_memt, sc->sc_memh, reg, data);
    383 }
    384 
    385 static void
    386 elansc_gpio_pin_ctl(void *arg, int pin, int flags)
    387 {
    388 	struct elansc_softc *sc = arg;
    389 	int reg, shift;
    390 	uint16_t data;
    391 
    392 	reg = (pin < 16 ? MMCR_PIODIR15_0 : MMCR_PIODIR31_16);
    393 	shift = pin % 16;
    394 	data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
    395 	if (flags & GPIO_PIN_INPUT)
    396 		data &= ~(1 << shift);
    397 	if (flags & GPIO_PIN_OUTPUT)
    398 		data |= (1 << shift);
    399 
    400 	bus_space_write_2(sc->sc_memt, sc->sc_memh, reg, data);
    401 }
    402 #endif /* NGPIO */
    403