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