Home | History | Annotate | Line # | Download | only in pci
elan520.c revision 1.16.42.3
      1  1.16.42.1    bouyer /*	$NetBSD: elan520.c,v 1.16.42.3 2008/01/23 19:27:19 bouyer 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.16.42.1    bouyer __KERNEL_RCSID(0, "$NetBSD: elan520.c,v 1.16.42.3 2008/01/23 19:27:19 bouyer Exp $");
     51        1.1   thorpej 
     52        1.1   thorpej #include <sys/param.h>
     53        1.1   thorpej #include <sys/systm.h>
     54  1.16.42.3    bouyer #include <sys/time.h>
     55        1.1   thorpej #include <sys/device.h>
     56        1.9       riz #include <sys/gpio.h>
     57  1.16.42.1    bouyer #include <sys/mutex.h>
     58  1.16.42.1    bouyer #include <sys/wdog.h>
     59        1.1   thorpej 
     60        1.5   thorpej #include <uvm/uvm_extern.h>
     61        1.5   thorpej 
     62        1.1   thorpej #include <machine/bus.h>
     63        1.1   thorpej 
     64        1.1   thorpej #include <dev/pci/pcivar.h>
     65        1.1   thorpej 
     66        1.1   thorpej #include <dev/pci/pcidevs.h>
     67        1.1   thorpej 
     68       1.10  drochner #include "gpio.h"
     69       1.10  drochner #if NGPIO > 0
     70        1.9       riz #include <dev/gpio/gpiovar.h>
     71       1.10  drochner #endif
     72        1.9       riz 
     73        1.1   thorpej #include <arch/i386/pci/elan520reg.h>
     74        1.1   thorpej 
     75        1.1   thorpej #include <dev/sysmon/sysmonvar.h>
     76        1.1   thorpej 
     77  1.16.42.3    bouyer #define	ELAN_IRQ	1
     78  1.16.42.3    bouyer #define	IDT_PROT_SIZE	PAGE_SIZE
     79  1.16.42.3    bouyer 
     80        1.1   thorpej struct elansc_softc {
     81        1.1   thorpej 	struct device sc_dev;
     82  1.16.42.3    bouyer 	device_t sc_par;
     83  1.16.42.3    bouyer 	device_t sc_pex;
     84  1.16.42.3    bouyer 
     85  1.16.42.3    bouyer 	pci_chipset_tag_t sc_pc;
     86  1.16.42.3    bouyer 	pcitag_t sc_tag;
     87        1.1   thorpej 	bus_space_tag_t sc_memt;
     88        1.1   thorpej 	bus_space_handle_t sc_memh;
     89        1.1   thorpej 	int sc_echobug;
     90        1.1   thorpej 
     91  1.16.42.1    bouyer 	kmutex_t sc_mtx;
     92  1.16.42.1    bouyer 
     93        1.1   thorpej 	struct sysmon_wdog sc_smw;
     94  1.16.42.3    bouyer 	void		*sc_eih;
     95  1.16.42.3    bouyer 	void		*sc_pih;
     96  1.16.42.3    bouyer 	void		*sc_sh;
     97  1.16.42.3    bouyer 	uint8_t		sc_mpicmode;
     98  1.16.42.3    bouyer 	uint8_t		sc_picicr;
     99  1.16.42.3    bouyer 	int		sc_idtpar;
    100  1.16.42.3    bouyer 	int		sc_textpar;
    101       1.11       riz #if NGPIO > 0
    102        1.9       riz 	/* GPIO interface */
    103        1.9       riz 	struct gpio_chipset_tag sc_gpio_gc;
    104        1.9       riz 	gpio_pin_t sc_gpio_pins[ELANSC_PIO_NPINS];
    105       1.11       riz #endif
    106        1.1   thorpej };
    107        1.1   thorpej 
    108  1.16.42.3    bouyer int elansc_wpvnmi = 1;
    109  1.16.42.3    bouyer int elansc_pcinmi = 1;
    110  1.16.42.3    bouyer int elansc_do_protect_idt = 0;
    111  1.16.42.3    bouyer 
    112       1.10  drochner #if NGPIO > 0
    113        1.9       riz static int	elansc_gpio_pin_read(void *, int);
    114        1.9       riz static void	elansc_gpio_pin_write(void *, int, int);
    115        1.9       riz static void	elansc_gpio_pin_ctl(void *, int, int);
    116       1.10  drochner #endif
    117        1.9       riz 
    118  1.16.42.3    bouyer static void elansc_print_par(device_t, int, uint32_t);
    119  1.16.42.3    bouyer static void elanpex_intr_establish(device_t, struct elansc_softc *);
    120  1.16.42.3    bouyer static void elanpar_intr_establish(device_t, struct elansc_softc *);
    121  1.16.42.3    bouyer static void elanpex_intr_disestablish(struct elansc_softc *);
    122  1.16.42.3    bouyer static void elanpar_intr_disestablish(struct elansc_softc *);
    123  1.16.42.3    bouyer 
    124        1.1   thorpej static void
    125  1.16.42.1    bouyer elansc_childdetached(device_t self, device_t child)
    126  1.16.42.1    bouyer {
    127  1.16.42.3    bouyer 	struct elansc_softc *sc = device_private(self);
    128  1.16.42.3    bouyer 
    129  1.16.42.3    bouyer 	if (child == sc->sc_par)
    130  1.16.42.3    bouyer 		sc->sc_par = NULL;
    131  1.16.42.3    bouyer 	if (child == sc->sc_pex)
    132  1.16.42.3    bouyer 		sc->sc_pex = NULL;
    133  1.16.42.3    bouyer 	/* elansc does not presently keep a pointer to
    134  1.16.42.3    bouyer 	 * the gpio, so there is nothing to do if it is detached.
    135  1.16.42.1    bouyer 	 */
    136  1.16.42.1    bouyer }
    137  1.16.42.1    bouyer 
    138  1.16.42.1    bouyer static void
    139        1.1   thorpej elansc_wdogctl_write(struct elansc_softc *sc, uint16_t val)
    140        1.1   thorpej {
    141        1.6  christos 	uint8_t echo_mode = 0; /* XXX: gcc */
    142        1.1   thorpej 
    143  1.16.42.1    bouyer 	KASSERT(mutex_owned(&sc->sc_mtx));
    144        1.1   thorpej 
    145        1.1   thorpej 	/* Switch off GP bus echo mode if we need to. */
    146        1.1   thorpej 	if (sc->sc_echobug) {
    147        1.1   thorpej 		echo_mode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    148        1.1   thorpej 		    MMCR_GPECHO);
    149        1.1   thorpej 		bus_space_write_1(sc->sc_memt, sc->sc_memh,
    150        1.1   thorpej 		    MMCR_GPECHO, echo_mode & ~GPECHO_GP_ECHO_ENB);
    151        1.1   thorpej 	}
    152        1.1   thorpej 
    153        1.1   thorpej 	/* Unlock the register. */
    154        1.1   thorpej 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    155        1.1   thorpej 	    WDTMRCTL_UNLOCK1);
    156        1.1   thorpej 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    157        1.1   thorpej 	    WDTMRCTL_UNLOCK2);
    158        1.1   thorpej 
    159        1.1   thorpej 	/* Write the value. */
    160        1.1   thorpej 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL, val);
    161        1.1   thorpej 
    162        1.1   thorpej 	/* Switch GP bus echo mode back. */
    163        1.1   thorpej 	if (sc->sc_echobug)
    164        1.1   thorpej 		bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_GPECHO,
    165        1.1   thorpej 		    echo_mode);
    166        1.1   thorpej }
    167        1.1   thorpej 
    168        1.1   thorpej static void
    169        1.1   thorpej elansc_wdogctl_reset(struct elansc_softc *sc)
    170        1.1   thorpej {
    171        1.7  christos 	uint8_t echo_mode = 0/* XXX: gcc */;
    172        1.1   thorpej 
    173  1.16.42.1    bouyer 	KASSERT(mutex_owned(&sc->sc_mtx));
    174        1.1   thorpej 
    175        1.1   thorpej 	/* Switch off GP bus echo mode if we need to. */
    176        1.1   thorpej 	if (sc->sc_echobug) {
    177        1.1   thorpej 		echo_mode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    178        1.1   thorpej 		    MMCR_GPECHO);
    179        1.1   thorpej 		bus_space_write_1(sc->sc_memt, sc->sc_memh,
    180        1.1   thorpej 		    MMCR_GPECHO, echo_mode & ~GPECHO_GP_ECHO_ENB);
    181        1.1   thorpej 	}
    182        1.1   thorpej 
    183        1.1   thorpej 	/* Reset the watchdog. */
    184        1.1   thorpej 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    185        1.1   thorpej 	    WDTMRCTL_RESET1);
    186        1.1   thorpej 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    187        1.1   thorpej 	    WDTMRCTL_RESET2);
    188        1.1   thorpej 
    189        1.1   thorpej 	/* Switch GP bus echo mode back. */
    190        1.1   thorpej 	if (sc->sc_echobug)
    191        1.1   thorpej 		bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_GPECHO,
    192        1.1   thorpej 		    echo_mode);
    193        1.1   thorpej }
    194        1.1   thorpej 
    195        1.1   thorpej static const struct {
    196        1.1   thorpej 	int	period;		/* whole seconds */
    197        1.1   thorpej 	uint16_t exp;		/* exponent select */
    198        1.1   thorpej } elansc_wdog_periods[] = {
    199        1.1   thorpej 	{ 1,	WDTMRCTL_EXP_SEL25 },
    200        1.1   thorpej 	{ 2,	WDTMRCTL_EXP_SEL26 },
    201        1.1   thorpej 	{ 4,	WDTMRCTL_EXP_SEL27 },
    202        1.1   thorpej 	{ 8,	WDTMRCTL_EXP_SEL28 },
    203        1.1   thorpej 	{ 16,	WDTMRCTL_EXP_SEL29 },
    204        1.1   thorpej 	{ 32,	WDTMRCTL_EXP_SEL30 },
    205        1.1   thorpej 	{ 0,	0 },
    206        1.1   thorpej };
    207        1.1   thorpej 
    208        1.1   thorpej static int
    209  1.16.42.1    bouyer elansc_wdog_arm(struct elansc_softc *sc)
    210        1.1   thorpej {
    211  1.16.42.1    bouyer 	struct sysmon_wdog *smw = &sc->sc_smw;
    212        1.1   thorpej 	int i;
    213        1.7  christos 	uint16_t exp_sel = 0; /* XXX: gcc */
    214        1.1   thorpej 
    215  1.16.42.1    bouyer 	KASSERT(mutex_owned(&sc->sc_mtx));
    216  1.16.42.1    bouyer 
    217  1.16.42.1    bouyer 	if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
    218  1.16.42.1    bouyer 		smw->smw_period = 32;
    219  1.16.42.1    bouyer 		exp_sel = WDTMRCTL_EXP_SEL30;
    220        1.1   thorpej 	} else {
    221  1.16.42.1    bouyer 		for (i = 0; elansc_wdog_periods[i].period != 0; i++) {
    222  1.16.42.1    bouyer 			if (elansc_wdog_periods[i].period ==
    223  1.16.42.1    bouyer 			    smw->smw_period) {
    224  1.16.42.1    bouyer 				exp_sel = elansc_wdog_periods[i].exp;
    225  1.16.42.1    bouyer 				break;
    226        1.1   thorpej 			}
    227        1.1   thorpej 		}
    228  1.16.42.1    bouyer 		if (elansc_wdog_periods[i].period == 0)
    229  1.16.42.1    bouyer 			return EINVAL;
    230        1.1   thorpej 	}
    231  1.16.42.1    bouyer 	elansc_wdogctl_write(sc, WDTMRCTL_ENB |
    232  1.16.42.1    bouyer 	    WDTMRCTL_WRST_ENB | exp_sel);
    233  1.16.42.1    bouyer 	elansc_wdogctl_reset(sc);
    234  1.16.42.1    bouyer 	return 0;
    235  1.16.42.1    bouyer }
    236  1.16.42.1    bouyer 
    237  1.16.42.1    bouyer static int
    238  1.16.42.1    bouyer elansc_wdog_setmode(struct sysmon_wdog *smw)
    239  1.16.42.1    bouyer {
    240  1.16.42.1    bouyer 	struct elansc_softc *sc = smw->smw_cookie;
    241  1.16.42.1    bouyer 	int rc = 0;
    242  1.16.42.1    bouyer 
    243  1.16.42.1    bouyer 	mutex_enter(&sc->sc_mtx);
    244  1.16.42.1    bouyer 
    245  1.16.42.1    bouyer 	if (!device_is_active(&sc->sc_dev))
    246  1.16.42.1    bouyer 		rc = EBUSY;
    247  1.16.42.1    bouyer 	else if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    248  1.16.42.1    bouyer 		elansc_wdogctl_write(sc,
    249  1.16.42.1    bouyer 		    WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
    250  1.16.42.1    bouyer 	} else
    251  1.16.42.1    bouyer 		rc = elansc_wdog_arm(sc);
    252  1.16.42.1    bouyer 
    253  1.16.42.1    bouyer 	mutex_exit(&sc->sc_mtx);
    254  1.16.42.1    bouyer 	return rc;
    255        1.1   thorpej }
    256        1.1   thorpej 
    257        1.1   thorpej static int
    258        1.1   thorpej elansc_wdog_tickle(struct sysmon_wdog *smw)
    259        1.1   thorpej {
    260        1.1   thorpej 	struct elansc_softc *sc = smw->smw_cookie;
    261        1.1   thorpej 
    262  1.16.42.1    bouyer 	mutex_enter(&sc->sc_mtx);
    263        1.1   thorpej 	elansc_wdogctl_reset(sc);
    264  1.16.42.1    bouyer 	mutex_exit(&sc->sc_mtx);
    265  1.16.42.1    bouyer 	return 0;
    266        1.1   thorpej }
    267        1.1   thorpej 
    268        1.1   thorpej static int
    269  1.16.42.2    bouyer elansc_match(device_t parent, struct cfdata *match, void *aux)
    270        1.1   thorpej {
    271        1.1   thorpej 	struct pci_attach_args *pa = aux;
    272        1.1   thorpej 
    273        1.1   thorpej 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
    274        1.1   thorpej 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_SC520_SC)
    275        1.1   thorpej 		return (10);	/* beat pchb */
    276        1.1   thorpej 
    277        1.1   thorpej 	return (0);
    278        1.1   thorpej }
    279        1.1   thorpej 
    280        1.1   thorpej static const char *elansc_speeds[] = {
    281        1.1   thorpej 	"(reserved 00)",
    282        1.1   thorpej 	"100MHz",
    283        1.1   thorpej 	"133MHz",
    284        1.1   thorpej 	"(reserved 11)",
    285        1.1   thorpej };
    286        1.1   thorpej 
    287  1.16.42.3    bouyer static int
    288  1.16.42.3    bouyer elanpar_intr(void *arg)
    289  1.16.42.3    bouyer {
    290  1.16.42.3    bouyer 	struct elansc_softc *sc = arg;
    291  1.16.42.3    bouyer 	uint16_t wpvsta;
    292  1.16.42.3    bouyer 	unsigned win;
    293  1.16.42.3    bouyer 	uint32_t par;
    294  1.16.42.3    bouyer 	const char *wpvstr;
    295  1.16.42.3    bouyer 
    296  1.16.42.3    bouyer 	wpvsta = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_WPVSTA);
    297  1.16.42.3    bouyer 
    298  1.16.42.3    bouyer 	if ((wpvsta & MMCR_WPVSTA_WPV_STA) == 0)
    299  1.16.42.3    bouyer 		return 0;
    300  1.16.42.3    bouyer 
    301  1.16.42.3    bouyer 	win = __SHIFTOUT(wpvsta, MMCR_WPVSTA_WPV_WINDOW);
    302  1.16.42.3    bouyer 
    303  1.16.42.3    bouyer 	par = bus_space_read_4(sc->sc_memt, sc->sc_memh, MMCR_PAR(win));
    304  1.16.42.3    bouyer 
    305  1.16.42.3    bouyer 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WPVSTA,
    306  1.16.42.3    bouyer 	    MMCR_WPVSTA_WPV_STA);
    307  1.16.42.3    bouyer 
    308  1.16.42.3    bouyer 	switch (wpvsta & MMCR_WPVSTA_WPV_MSTR) {
    309  1.16.42.3    bouyer 	case MMCR_WPVSTA_WPV_MSTR_CPU:
    310  1.16.42.3    bouyer 		wpvstr = "cpu";
    311  1.16.42.3    bouyer 		break;
    312  1.16.42.3    bouyer 	case MMCR_WPVSTA_WPV_MSTR_PCI:
    313  1.16.42.3    bouyer 		wpvstr = "pci";
    314  1.16.42.3    bouyer 		break;
    315  1.16.42.3    bouyer 	case MMCR_WPVSTA_WPV_MSTR_GP:
    316  1.16.42.3    bouyer 		wpvstr = "gp";
    317  1.16.42.3    bouyer 		break;
    318  1.16.42.3    bouyer 	default:
    319  1.16.42.3    bouyer 		wpvstr = "unknown";
    320  1.16.42.3    bouyer 		break;
    321  1.16.42.3    bouyer 	}
    322  1.16.42.3    bouyer 	aprint_error_dev(sc->sc_par,
    323  1.16.42.3    bouyer 	    "%s violated write-protect window %u\n", wpvstr, win);
    324  1.16.42.3    bouyer 	elansc_print_par(sc->sc_par, win, par);
    325  1.16.42.3    bouyer 	return 0;
    326  1.16.42.3    bouyer }
    327  1.16.42.3    bouyer 
    328  1.16.42.3    bouyer static int
    329  1.16.42.3    bouyer elanpex_intr(void *arg)
    330  1.16.42.3    bouyer {
    331  1.16.42.3    bouyer 	static struct {
    332  1.16.42.3    bouyer 		const char *string;
    333  1.16.42.3    bouyer 		bool nonfatal;
    334  1.16.42.3    bouyer 	} cmd[16] = {
    335  1.16.42.3    bouyer 		  [0] =	{.string = "not latched"}
    336  1.16.42.3    bouyer 		, [1] =	{.string = "special cycle"}
    337  1.16.42.3    bouyer 		, [2] =	{.string = "i/o read"}
    338  1.16.42.3    bouyer 		, [3] =	{.string = "i/o write"}
    339  1.16.42.3    bouyer 		, [4] =	{.string = "4"}
    340  1.16.42.3    bouyer 		, [5] =	{.string = "5"}
    341  1.16.42.3    bouyer 		, [6] =	{.string = "memory rd"}
    342  1.16.42.3    bouyer 		, [7] =	{.string = "memory wr"}
    343  1.16.42.3    bouyer 		, [8] =	{.string = "8"}
    344  1.16.42.3    bouyer 		, [9] =	{.string = "9"}
    345  1.16.42.3    bouyer 		, [10] = {.string = "cfg rd", .nonfatal = true}
    346  1.16.42.3    bouyer 		, [11] = {.string = "cfg wr"}
    347  1.16.42.3    bouyer 		, [12] = {.string = "memory rd mul"}
    348  1.16.42.3    bouyer 		, [13] = {.string = "dual-address cycle"}
    349  1.16.42.3    bouyer 		, [14] = {.string = "memory rd line"}
    350  1.16.42.3    bouyer 		, [15] = {.string = "memory wr & inv"}
    351  1.16.42.3    bouyer 	};
    352  1.16.42.3    bouyer 
    353  1.16.42.3    bouyer 	static const struct {
    354  1.16.42.3    bouyer 		uint16_t bit;
    355  1.16.42.3    bouyer 		const char *msg;
    356  1.16.42.3    bouyer 	} mmsg[] = {
    357  1.16.42.3    bouyer 		  {MMCR_HBMSTIRQSTA_M_RTRTO_IRQ_STA, "retry timeout"}
    358  1.16.42.3    bouyer 		, {MMCR_HBMSTIRQSTA_M_TABRT_IRQ_STA, "target abort"}
    359  1.16.42.3    bouyer 		, {MMCR_HBMSTIRQSTA_M_MABRT_IRQ_STA, "abort"}
    360  1.16.42.3    bouyer 		, {MMCR_HBMSTIRQSTA_M_SERR_IRQ_STA, "system error"}
    361  1.16.42.3    bouyer 		, {MMCR_HBMSTIRQSTA_M_RPER_IRQ_STA, "received parity error"}
    362  1.16.42.3    bouyer 		, {MMCR_HBMSTIRQSTA_M_DPER_IRQ_STA, "detected parity error"}
    363  1.16.42.3    bouyer 	}, tmsg[] = {
    364  1.16.42.3    bouyer 		  {MMCR_HBTGTIRQSTA_T_DLYTO_IRQ_STA, "delayed txn timeout"}
    365  1.16.42.3    bouyer 		, {MMCR_HBTGTIRQSTA_T_APER_IRQ_STA, "address parity"}
    366  1.16.42.3    bouyer 		, {MMCR_HBTGTIRQSTA_T_DPER_IRQ_STA, "data parity"}
    367  1.16.42.3    bouyer 	};
    368  1.16.42.3    bouyer 	uint8_t pciarbsta;
    369  1.16.42.3    bouyer 	uint16_t mstcmd, mstirq, tgtid, tgtirq;
    370  1.16.42.3    bouyer 	uint32_t mstaddr;
    371  1.16.42.3    bouyer 	uint16_t mstack = 0, tgtack = 0;
    372  1.16.42.3    bouyer 	int fatal = 0, i, handled = 0;
    373  1.16.42.3    bouyer 	struct elansc_softc *sc = arg;
    374  1.16.42.3    bouyer 
    375  1.16.42.3    bouyer 	pciarbsta = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_PCIARBSTA);
    376  1.16.42.3    bouyer 	mstirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQSTA);
    377  1.16.42.3    bouyer 	mstaddr = bus_space_read_4(sc->sc_memt, sc->sc_memh, MMCR_MSTINTADD);
    378  1.16.42.3    bouyer 	tgtirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQSTA);
    379  1.16.42.3    bouyer 
    380  1.16.42.3    bouyer 	if ((pciarbsta & MMCR_PCIARBSTA_GNT_TO_STA) != 0) {
    381  1.16.42.3    bouyer 		aprint_error_dev(sc->sc_pex,
    382  1.16.42.3    bouyer 		    "grant time-out, GNT%" __PRIuBITS "# asserted\n",
    383  1.16.42.3    bouyer 		    __SHIFTOUT(pciarbsta, MMCR_PCIARBSTA_GNT_TO_ID));
    384  1.16.42.3    bouyer 		bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_PCIARBSTA,
    385  1.16.42.3    bouyer 		    MMCR_PCIARBSTA_GNT_TO_STA);
    386  1.16.42.3    bouyer 		handled = true;
    387  1.16.42.3    bouyer 	}
    388  1.16.42.3    bouyer 
    389  1.16.42.3    bouyer 	mstcmd = __SHIFTOUT(mstirq, MMCR_HBMSTIRQSTA_M_CMD_IRQ_ID);
    390  1.16.42.3    bouyer 
    391  1.16.42.3    bouyer 	for (i = 0; i < __arraycount(mmsg); i++) {
    392  1.16.42.3    bouyer 		if ((mstirq & mmsg[i].bit) == 0)
    393  1.16.42.3    bouyer 			continue;
    394  1.16.42.3    bouyer 		aprint_error_dev(sc->sc_pex,
    395  1.16.42.3    bouyer 		    "%s %08" PRIx32 " master %s\n",
    396  1.16.42.3    bouyer 		    cmd[mstcmd].string, mstaddr, mmsg[i].msg);
    397  1.16.42.3    bouyer 
    398  1.16.42.3    bouyer 		mstack |= mmsg[i].bit;
    399  1.16.42.3    bouyer 		if (!cmd[mstcmd].nonfatal)
    400  1.16.42.3    bouyer 			fatal = true;
    401  1.16.42.3    bouyer 	}
    402  1.16.42.3    bouyer 
    403  1.16.42.3    bouyer 	tgtid = __SHIFTOUT(tgtirq, MMCR_HBTGTIRQSTA_T_IRQ_ID);
    404  1.16.42.3    bouyer 
    405  1.16.42.3    bouyer 	for (i = 0; i < __arraycount(tmsg); i++) {
    406  1.16.42.3    bouyer 		if ((tgtirq & tmsg[i].bit) == 0)
    407  1.16.42.3    bouyer 			continue;
    408  1.16.42.3    bouyer 		aprint_error_dev(sc->sc_pex, "%1x target %s\n", tgtid,
    409  1.16.42.3    bouyer 		    tmsg[i].msg);
    410  1.16.42.3    bouyer 		tgtack |= tmsg[i].bit;
    411  1.16.42.3    bouyer 	}
    412  1.16.42.3    bouyer 
    413  1.16.42.3    bouyer 	/* acknowledge interrupts */
    414  1.16.42.3    bouyer 	if (tgtack != 0) {
    415  1.16.42.3    bouyer 		handled = true;
    416  1.16.42.3    bouyer 		bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQSTA,
    417  1.16.42.3    bouyer 		    tgtack);
    418  1.16.42.3    bouyer 	}
    419  1.16.42.3    bouyer 	if (mstack != 0) {
    420  1.16.42.3    bouyer 		handled = true;
    421  1.16.42.3    bouyer 		bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQSTA,
    422  1.16.42.3    bouyer 		    mstack);
    423  1.16.42.3    bouyer 	}
    424  1.16.42.3    bouyer 	return fatal ? 0 : (handled ? 1 : 0);
    425  1.16.42.3    bouyer }
    426  1.16.42.3    bouyer 
    427  1.16.42.3    bouyer #define	elansc_print_1(__dev, __sc, __reg)				\
    428  1.16.42.3    bouyer do {									\
    429  1.16.42.3    bouyer 	aprint_debug_dev(__dev,						\
    430  1.16.42.3    bouyer 	    "%s: %s %02" PRIx8 "\n", __func__, #__reg,			\
    431  1.16.42.3    bouyer 	    bus_space_read_1((__sc)->sc_memt, (__sc)->sc_memh, __reg));	\
    432  1.16.42.3    bouyer } while (/*CONSTCOND*/0)
    433  1.16.42.3    bouyer 
    434  1.16.42.3    bouyer static void
    435  1.16.42.3    bouyer elansc_print_par(device_t dev, int i, uint32_t par)
    436  1.16.42.3    bouyer {
    437  1.16.42.3    bouyer 	uint32_t addr, sz, unit;
    438  1.16.42.3    bouyer 	const char *tgtstr;
    439  1.16.42.3    bouyer 
    440  1.16.42.3    bouyer 	switch (par & MMCR_PAR_TARGET) {
    441  1.16.42.3    bouyer 	default:
    442  1.16.42.3    bouyer 	case MMCR_PAR_TARGET_OFF:
    443  1.16.42.3    bouyer 		tgtstr = "off";
    444  1.16.42.3    bouyer 		break;
    445  1.16.42.3    bouyer 	case MMCR_PAR_TARGET_GPIO:
    446  1.16.42.3    bouyer 		tgtstr = "gpio";
    447  1.16.42.3    bouyer 		break;
    448  1.16.42.3    bouyer 	case MMCR_PAR_TARGET_GPMEM:
    449  1.16.42.3    bouyer 		tgtstr = "gpmem";
    450  1.16.42.3    bouyer 		break;
    451  1.16.42.3    bouyer 	case MMCR_PAR_TARGET_PCI:
    452  1.16.42.3    bouyer 		tgtstr = "pci";
    453  1.16.42.3    bouyer 		break;
    454  1.16.42.3    bouyer 	case MMCR_PAR_TARGET_BOOTCS:
    455  1.16.42.3    bouyer 		tgtstr = "bootcs";
    456  1.16.42.3    bouyer 		break;
    457  1.16.42.3    bouyer 	case MMCR_PAR_TARGET_ROMCS1:
    458  1.16.42.3    bouyer 		tgtstr = "romcs1";
    459  1.16.42.3    bouyer 		break;
    460  1.16.42.3    bouyer 	case MMCR_PAR_TARGET_ROMCS2:
    461  1.16.42.3    bouyer 		tgtstr = "romcs2";
    462  1.16.42.3    bouyer 		break;
    463  1.16.42.3    bouyer 	case MMCR_PAR_TARGET_SDRAM:
    464  1.16.42.3    bouyer 		tgtstr = "sdram";
    465  1.16.42.3    bouyer 		break;
    466  1.16.42.3    bouyer 	}
    467  1.16.42.3    bouyer 	if ((par & MMCR_PAR_TARGET) == MMCR_PAR_TARGET_GPIO) {
    468  1.16.42.3    bouyer 		unit = 1;
    469  1.16.42.3    bouyer 		sz = __SHIFTOUT(par, MMCR_PAR_IO_SZ);
    470  1.16.42.3    bouyer 		addr = __SHIFTOUT(par, MMCR_PAR_IO_ST_ADR);
    471  1.16.42.3    bouyer 	} else if ((par & MMCR_PAR_PG_SZ) != 0) {
    472  1.16.42.3    bouyer 		unit = 64 * 1024;
    473  1.16.42.3    bouyer 		sz = __SHIFTOUT(par, MMCR_PAR_64KB_SZ);
    474  1.16.42.3    bouyer 		addr = __SHIFTOUT(par, MMCR_PAR_64KB_ST_ADR);
    475  1.16.42.3    bouyer 	} else {
    476  1.16.42.3    bouyer 		unit = 4 * 1024;
    477  1.16.42.3    bouyer 		sz = __SHIFTOUT(par, MMCR_PAR_4KB_SZ);
    478  1.16.42.3    bouyer 		addr = __SHIFTOUT(par, MMCR_PAR_4KB_ST_ADR);
    479  1.16.42.3    bouyer 	}
    480  1.16.42.3    bouyer 
    481  1.16.42.3    bouyer 	aprint_debug_dev(dev,
    482  1.16.42.3    bouyer 	    "PAR[%d] %08" PRIx32 " tgt %s attr %1" __PRIxBITS
    483  1.16.42.3    bouyer 	    " start %08" PRIx32 " size %" PRIu32 "\n",
    484  1.16.42.3    bouyer 	    i, par, tgtstr, __SHIFTOUT(par, MMCR_PAR_ATTR),
    485  1.16.42.3    bouyer 	    addr * unit, (sz + 1) * unit);
    486  1.16.42.3    bouyer }
    487  1.16.42.3    bouyer 
    488  1.16.42.3    bouyer static void
    489  1.16.42.3    bouyer elansc_print_all_par(device_t dev,
    490  1.16.42.3    bouyer     bus_space_tag_t memt, bus_space_handle_t memh)
    491  1.16.42.3    bouyer {
    492  1.16.42.3    bouyer 	int i;
    493  1.16.42.3    bouyer 	uint32_t par;
    494  1.16.42.3    bouyer 
    495  1.16.42.3    bouyer 	for (i = 0; i < 16; i++) {
    496  1.16.42.3    bouyer 		par = bus_space_read_4(memt, memh, MMCR_PAR(i));
    497  1.16.42.3    bouyer 		elansc_print_par(dev, i, par);
    498  1.16.42.3    bouyer 	}
    499  1.16.42.3    bouyer }
    500  1.16.42.3    bouyer 
    501  1.16.42.3    bouyer static int
    502  1.16.42.3    bouyer elansc_alloc_par(bus_space_tag_t memt, bus_space_handle_t memh)
    503  1.16.42.3    bouyer {
    504  1.16.42.3    bouyer 	int i;
    505  1.16.42.3    bouyer 	uint32_t par;
    506  1.16.42.3    bouyer 
    507  1.16.42.3    bouyer 	for (i = 0; i < 16; i++) {
    508  1.16.42.3    bouyer 
    509  1.16.42.3    bouyer 		par = bus_space_read_4(memt, memh, MMCR_PAR(i));
    510  1.16.42.3    bouyer 
    511  1.16.42.3    bouyer 		if ((par & MMCR_PAR_TARGET) == MMCR_PAR_TARGET_OFF)
    512  1.16.42.3    bouyer 			break;
    513  1.16.42.3    bouyer 	}
    514  1.16.42.3    bouyer 	if (i == 16)
    515  1.16.42.3    bouyer 		return -1;
    516  1.16.42.3    bouyer 	return i;
    517  1.16.42.3    bouyer }
    518  1.16.42.3    bouyer 
    519  1.16.42.3    bouyer static void
    520  1.16.42.3    bouyer elansc_disable_par(bus_space_tag_t memt, bus_space_handle_t memh, int idx)
    521  1.16.42.3    bouyer {
    522  1.16.42.3    bouyer 	uint32_t par;
    523  1.16.42.3    bouyer 	par = bus_space_read_4(memt, memh, MMCR_PAR(idx));
    524  1.16.42.3    bouyer 	par &= ~MMCR_PAR_TARGET;
    525  1.16.42.3    bouyer 	par |= MMCR_PAR_TARGET_OFF;
    526  1.16.42.3    bouyer 	bus_space_write_4(memt, memh, MMCR_PAR(idx), par);
    527  1.16.42.3    bouyer }
    528  1.16.42.3    bouyer 
    529  1.16.42.3    bouyer static int
    530  1.16.42.3    bouyer elansc_protect_text(device_t self, struct elansc_softc *sc)
    531  1.16.42.3    bouyer {
    532  1.16.42.3    bouyer 	int i;
    533  1.16.42.3    bouyer 	uint32_t par;
    534  1.16.42.3    bouyer 	uint32_t protsize, unprotsize;
    535  1.16.42.3    bouyer 	const uint32_t sfkb = 64 * 1024;
    536  1.16.42.3    bouyer 	paddr_t start_pa, end_pa;
    537  1.16.42.3    bouyer 	extern char kernel_text, etext;
    538  1.16.42.3    bouyer 	bus_space_tag_t memt;
    539  1.16.42.3    bouyer 	bus_space_handle_t memh;
    540  1.16.42.3    bouyer 
    541  1.16.42.3    bouyer 	memt = sc->sc_memt;
    542  1.16.42.3    bouyer 	memh = sc->sc_memh;
    543  1.16.42.3    bouyer 
    544  1.16.42.3    bouyer 	if (!pmap_extract(pmap_kernel(), (vaddr_t)&kernel_text, &start_pa) ||
    545  1.16.42.3    bouyer 	    !pmap_extract(pmap_kernel(), (vaddr_t)&etext, &end_pa))
    546  1.16.42.3    bouyer 		return -1;
    547  1.16.42.3    bouyer 
    548  1.16.42.3    bouyer 	if (&etext - &kernel_text != end_pa - start_pa) {
    549  1.16.42.3    bouyer 		aprint_error_dev(self, "kernel text may not be contiguous\n");
    550  1.16.42.3    bouyer 		return -1;
    551  1.16.42.3    bouyer 	}
    552  1.16.42.3    bouyer 
    553  1.16.42.3    bouyer 	if ((i = elansc_alloc_par(memt, memh)) == -1) {
    554  1.16.42.3    bouyer 		aprint_error_dev(self, "cannot allocate PAR\n");
    555  1.16.42.3    bouyer 		return -1;
    556  1.16.42.3    bouyer 	}
    557  1.16.42.3    bouyer 
    558  1.16.42.3    bouyer 	par = bus_space_read_4(memt, memh, MMCR_PAR(i));
    559  1.16.42.3    bouyer 
    560  1.16.42.3    bouyer 	aprint_debug_dev(self,
    561  1.16.42.3    bouyer 	    "protect kernel text at physical addresses %p - %p\n",
    562  1.16.42.3    bouyer 	    (void *)start_pa, (void *)end_pa);
    563  1.16.42.3    bouyer 
    564  1.16.42.3    bouyer 	unprotsize = sfkb - start_pa % sfkb;
    565  1.16.42.3    bouyer 	start_pa += unprotsize;
    566  1.16.42.3    bouyer 	unprotsize += end_pa % sfkb;
    567  1.16.42.3    bouyer 	end_pa -= end_pa % sfkb;
    568  1.16.42.3    bouyer 
    569  1.16.42.3    bouyer 	aprint_debug_dev(self,
    570  1.16.42.3    bouyer 	    "actually protect kernel text at physical addresses %p - %p\n",
    571  1.16.42.3    bouyer 	    (void *)start_pa, (void *)end_pa);
    572  1.16.42.3    bouyer 
    573  1.16.42.3    bouyer 	aprint_verbose_dev(self,
    574  1.16.42.3    bouyer 	    "%" PRIu32 " bytes of kernel text are unprotected\n", unprotsize);
    575  1.16.42.3    bouyer 
    576  1.16.42.3    bouyer 	protsize = end_pa - start_pa;
    577  1.16.42.3    bouyer 
    578  1.16.42.3    bouyer 	/* clear PG_SZ, attribute, target, size, address. */
    579  1.16.42.3    bouyer 	par = MMCR_PAR_TARGET_SDRAM | MMCR_PAR_ATTR_NOWRITE | MMCR_PAR_PG_SZ;
    580  1.16.42.3    bouyer 	par |= __SHIFTIN(protsize / sfkb - 1, MMCR_PAR_64KB_SZ);
    581  1.16.42.3    bouyer 	par |= __SHIFTIN(start_pa / sfkb, MMCR_PAR_64KB_ST_ADR);
    582  1.16.42.3    bouyer 	bus_space_write_4(memt, memh, MMCR_PAR(i), par);
    583  1.16.42.3    bouyer 	return i;
    584  1.16.42.3    bouyer }
    585  1.16.42.3    bouyer 
    586  1.16.42.3    bouyer static int
    587  1.16.42.3    bouyer elansc_protect_idt(struct elansc_softc *sc)
    588  1.16.42.3    bouyer {
    589  1.16.42.3    bouyer 	int i;
    590  1.16.42.3    bouyer 	uint32_t par;
    591  1.16.42.3    bouyer 	extern paddr_t idt_paddr;
    592  1.16.42.3    bouyer 	bus_space_tag_t memt;
    593  1.16.42.3    bouyer 	bus_space_handle_t memh;
    594  1.16.42.3    bouyer 
    595  1.16.42.3    bouyer 	memt = sc->sc_memt;
    596  1.16.42.3    bouyer 	memh = sc->sc_memh;
    597  1.16.42.3    bouyer 
    598  1.16.42.3    bouyer 	if (elansc_do_protect_idt == 0)
    599  1.16.42.3    bouyer 		return -1;
    600  1.16.42.3    bouyer 
    601  1.16.42.3    bouyer 	if ((i = elansc_alloc_par(memt, memh)) == -1)
    602  1.16.42.3    bouyer 		return -1;
    603  1.16.42.3    bouyer 
    604  1.16.42.3    bouyer 	par = bus_space_read_4(memt, memh, MMCR_PAR(i));
    605  1.16.42.3    bouyer 
    606  1.16.42.3    bouyer 	aprint_debug_dev(sc->sc_par, "protect IDT at paddr %p\n",
    607  1.16.42.3    bouyer 	    (const void *)idt_paddr);
    608  1.16.42.3    bouyer 
    609  1.16.42.3    bouyer 	/* clear PG_SZ, attribute, target, size, address. */
    610  1.16.42.3    bouyer 	par = MMCR_PAR_TARGET_SDRAM | MMCR_PAR_ATTR_NOWRITE;
    611  1.16.42.3    bouyer 	par |= __SHIFTIN(IDT_PROT_SIZE / PAGE_SIZE - 1, MMCR_PAR_4KB_SZ);
    612  1.16.42.3    bouyer 	par |= __SHIFTIN(idt_paddr / PAGE_SIZE, MMCR_PAR_4KB_ST_ADR);
    613  1.16.42.3    bouyer 	bus_space_write_4(memt, memh, MMCR_PAR(i), par);
    614  1.16.42.3    bouyer 	return i;
    615  1.16.42.3    bouyer }
    616  1.16.42.3    bouyer 
    617  1.16.42.3    bouyer static void
    618  1.16.42.3    bouyer elanpex_intr_ack(bus_space_tag_t memt, bus_space_handle_t memh)
    619  1.16.42.3    bouyer {
    620  1.16.42.3    bouyer 	bus_space_write_1(memt, memh, MMCR_PCIARBSTA,
    621  1.16.42.3    bouyer 	    MMCR_PCIARBSTA_GNT_TO_STA);
    622  1.16.42.3    bouyer 	bus_space_write_2(memt, memh, MMCR_HBTGTIRQSTA, MMCR_TGTIRQ_ACT);
    623  1.16.42.3    bouyer 	bus_space_write_2(memt, memh, MMCR_HBMSTIRQSTA, MMCR_MSTIRQ_ACT);
    624  1.16.42.3    bouyer }
    625  1.16.42.3    bouyer 
    626  1.16.42.1    bouyer static bool
    627  1.16.42.1    bouyer elansc_suspend(device_t dev)
    628  1.16.42.1    bouyer {
    629  1.16.42.1    bouyer 	bool rc;
    630  1.16.42.1    bouyer 	struct elansc_softc *sc = device_private(dev);
    631  1.16.42.1    bouyer 
    632  1.16.42.1    bouyer 	mutex_enter(&sc->sc_mtx);
    633  1.16.42.1    bouyer 	rc = ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED);
    634  1.16.42.1    bouyer 	mutex_exit(&sc->sc_mtx);
    635  1.16.42.1    bouyer 	if (!rc)
    636  1.16.42.1    bouyer 		aprint_debug_dev(dev, "watchdog enabled, suspend forbidden");
    637  1.16.42.1    bouyer 	return rc;
    638  1.16.42.1    bouyer }
    639  1.16.42.1    bouyer 
    640  1.16.42.1    bouyer static bool
    641  1.16.42.1    bouyer elansc_resume(device_t dev)
    642  1.16.42.1    bouyer {
    643  1.16.42.1    bouyer 	struct elansc_softc *sc = device_private(dev);
    644  1.16.42.1    bouyer 
    645  1.16.42.1    bouyer 	mutex_enter(&sc->sc_mtx);
    646  1.16.42.1    bouyer 	/* Set up the watchdog registers with some defaults. */
    647  1.16.42.1    bouyer 	elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
    648  1.16.42.1    bouyer 
    649  1.16.42.1    bouyer 	/* ...and clear it. */
    650  1.16.42.1    bouyer 	elansc_wdogctl_reset(sc);
    651  1.16.42.1    bouyer 	mutex_exit(&sc->sc_mtx);
    652  1.16.42.1    bouyer 
    653  1.16.42.1    bouyer 	return true;
    654  1.16.42.1    bouyer }
    655  1.16.42.1    bouyer 
    656  1.16.42.1    bouyer static int
    657  1.16.42.1    bouyer elansc_detach(device_t self, int flags)
    658  1.16.42.1    bouyer {
    659  1.16.42.1    bouyer 	int rc;
    660  1.16.42.1    bouyer 	struct elansc_softc *sc = device_private(self);
    661  1.16.42.1    bouyer 
    662  1.16.42.1    bouyer 	if ((rc = config_detach_children(self, flags)) != 0)
    663  1.16.42.1    bouyer 		return rc;
    664  1.16.42.1    bouyer 
    665  1.16.42.1    bouyer 	pmf_device_deregister(self);
    666  1.16.42.1    bouyer 
    667  1.16.42.1    bouyer 	if ((rc = sysmon_wdog_unregister(&sc->sc_smw)) != 0) {
    668  1.16.42.1    bouyer 		if (rc == ERESTART)
    669  1.16.42.1    bouyer 			rc = EINTR;
    670  1.16.42.1    bouyer 		return rc;
    671  1.16.42.1    bouyer 	}
    672  1.16.42.1    bouyer 
    673  1.16.42.1    bouyer 	mutex_enter(&sc->sc_mtx);
    674  1.16.42.1    bouyer 
    675  1.16.42.1    bouyer 	/* Set up the watchdog registers with some defaults. */
    676  1.16.42.1    bouyer 	elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
    677  1.16.42.1    bouyer 
    678  1.16.42.1    bouyer 	/* ...and clear it. */
    679  1.16.42.1    bouyer 	elansc_wdogctl_reset(sc);
    680  1.16.42.1    bouyer 
    681  1.16.42.1    bouyer 	bus_space_unmap(sc->sc_memt, sc->sc_memh, PAGE_SIZE);
    682  1.16.42.1    bouyer 
    683  1.16.42.1    bouyer 	mutex_exit(&sc->sc_mtx);
    684  1.16.42.1    bouyer 	mutex_destroy(&sc->sc_mtx);
    685  1.16.42.1    bouyer 	return 0;
    686  1.16.42.1    bouyer }
    687  1.16.42.1    bouyer 
    688  1.16.42.3    bouyer static void *
    689  1.16.42.3    bouyer elansc_intr_establish(device_t dev, int (*handler)(void *), void *arg)
    690  1.16.42.3    bouyer {
    691  1.16.42.3    bouyer 	struct pic *pic;
    692  1.16.42.3    bouyer 	void *ih;
    693  1.16.42.3    bouyer 
    694  1.16.42.3    bouyer 	if ((pic = intr_findpic(ELAN_IRQ)) == NULL) {
    695  1.16.42.3    bouyer 		aprint_error_dev(dev, "PIC for irq %d not found\n",
    696  1.16.42.3    bouyer 		    ELAN_IRQ);
    697  1.16.42.3    bouyer 		return NULL;
    698  1.16.42.3    bouyer 	} else if ((ih = intr_establish(ELAN_IRQ, pic, ELAN_IRQ,
    699  1.16.42.3    bouyer 	    IST_LEVEL, IPL_HIGH, handler, arg)) == NULL) {
    700  1.16.42.3    bouyer 		aprint_error_dev(dev,
    701  1.16.42.3    bouyer 		    "could not establish interrupt\n");
    702  1.16.42.3    bouyer 		return NULL;
    703  1.16.42.3    bouyer 	}
    704  1.16.42.3    bouyer 	aprint_verbose_dev(dev, "interrupting at irq %d\n", ELAN_IRQ);
    705  1.16.42.3    bouyer 	return ih;
    706  1.16.42.3    bouyer }
    707  1.16.42.3    bouyer 
    708  1.16.42.3    bouyer static bool
    709  1.16.42.3    bouyer elanpex_resume(device_t self)
    710  1.16.42.3    bouyer {
    711  1.16.42.3    bouyer 	struct elansc_softc *sc = device_private(device_parent(self));
    712  1.16.42.3    bouyer 
    713  1.16.42.3    bouyer 	elanpex_intr_establish(self, sc);
    714  1.16.42.3    bouyer 	return sc->sc_eih != NULL;
    715  1.16.42.3    bouyer }
    716  1.16.42.3    bouyer 
    717  1.16.42.3    bouyer static bool
    718  1.16.42.3    bouyer elanpex_suspend(device_t self)
    719  1.16.42.3    bouyer {
    720  1.16.42.3    bouyer 	struct elansc_softc *sc = device_private(device_parent(self));
    721  1.16.42.3    bouyer 
    722  1.16.42.3    bouyer 	elanpex_intr_disestablish(sc);
    723  1.16.42.3    bouyer 
    724  1.16.42.3    bouyer 	return true;
    725  1.16.42.3    bouyer }
    726  1.16.42.3    bouyer 
    727  1.16.42.3    bouyer static bool
    728  1.16.42.3    bouyer elanpar_resume(device_t self)
    729  1.16.42.3    bouyer {
    730  1.16.42.3    bouyer 	struct elansc_softc *sc = device_private(device_parent(self));
    731  1.16.42.3    bouyer 
    732  1.16.42.3    bouyer 	elanpar_intr_establish(self, sc);
    733  1.16.42.3    bouyer 	return sc->sc_pih != NULL;
    734  1.16.42.3    bouyer }
    735  1.16.42.3    bouyer 
    736  1.16.42.3    bouyer static bool
    737  1.16.42.3    bouyer elanpar_suspend(device_t self)
    738  1.16.42.3    bouyer {
    739  1.16.42.3    bouyer 	struct elansc_softc *sc = device_private(device_parent(self));
    740  1.16.42.3    bouyer 
    741  1.16.42.3    bouyer 	elanpar_intr_disestablish(sc->sc_pih);
    742  1.16.42.3    bouyer 
    743  1.16.42.3    bouyer 	return true;
    744  1.16.42.3    bouyer }
    745  1.16.42.3    bouyer 
    746  1.16.42.3    bouyer static void
    747  1.16.42.3    bouyer elanpex_intr_establish(device_t self, struct elansc_softc *sc)
    748  1.16.42.3    bouyer {
    749  1.16.42.3    bouyer 	uint8_t sysarbctl;
    750  1.16.42.3    bouyer 	uint16_t pcihostmap, mstirq, tgtirq;
    751  1.16.42.3    bouyer 
    752  1.16.42.3    bouyer 	pcihostmap = bus_space_read_2(sc->sc_memt, sc->sc_memh,
    753  1.16.42.3    bouyer 	    MMCR_PCIHOSTMAP);
    754  1.16.42.3    bouyer 	/* Priority P2 (Master PIC IR1) */
    755  1.16.42.3    bouyer 	pcihostmap &= ~MMCR_PCIHOSTMAP_PCI_IRQ_MAP;
    756  1.16.42.3    bouyer 	pcihostmap |= __SHIFTIN(__BIT(ELAN_IRQ), MMCR_PCIHOSTMAP_PCI_IRQ_MAP);
    757  1.16.42.3    bouyer 	if (elansc_pcinmi)
    758  1.16.42.3    bouyer 		pcihostmap |= MMCR_PCIHOSTMAP_PCI_NMI_ENB;
    759  1.16.42.3    bouyer 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_PCIHOSTMAP,
    760  1.16.42.3    bouyer 	    pcihostmap);
    761  1.16.42.3    bouyer 
    762  1.16.42.3    bouyer 	elanpex_intr_ack(sc->sc_memt, sc->sc_memh);
    763  1.16.42.3    bouyer 
    764  1.16.42.3    bouyer 	sysarbctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_SYSARBCTL);
    765  1.16.42.3    bouyer 	mstirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL);
    766  1.16.42.3    bouyer 	tgtirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL);
    767  1.16.42.3    bouyer 
    768  1.16.42.3    bouyer 	sysarbctl |= MMCR_SYSARBCTL_GNT_TO_INT_ENB;
    769  1.16.42.3    bouyer 
    770  1.16.42.3    bouyer 	mstirq |= MMCR_HBMSTIRQCTL_M_RTRTO_IRQ_ENB;
    771  1.16.42.3    bouyer 	mstirq |= MMCR_HBMSTIRQCTL_M_TABRT_IRQ_ENB;
    772  1.16.42.3    bouyer 	mstirq |= MMCR_HBMSTIRQCTL_M_MABRT_IRQ_ENB;
    773  1.16.42.3    bouyer 	mstirq |= MMCR_HBMSTIRQCTL_M_SERR_IRQ_ENB;
    774  1.16.42.3    bouyer 	mstirq |= MMCR_HBMSTIRQCTL_M_RPER_IRQ_ENB;
    775  1.16.42.3    bouyer 	mstirq |= MMCR_HBMSTIRQCTL_M_DPER_IRQ_ENB;
    776  1.16.42.3    bouyer 
    777  1.16.42.3    bouyer 	tgtirq |= MMCR_HBTGTIRQCTL_T_DLYTO_IRQ_ENB;
    778  1.16.42.3    bouyer 	tgtirq |= MMCR_HBTGTIRQCTL_T_APER_IRQ_ENB;
    779  1.16.42.3    bouyer 	tgtirq |= MMCR_HBTGTIRQCTL_T_DPER_IRQ_ENB;
    780  1.16.42.3    bouyer 
    781  1.16.42.3    bouyer 	if (elansc_pcinmi) {
    782  1.16.42.3    bouyer 		sc->sc_eih = nmi_establish(elanpex_intr, sc);
    783  1.16.42.3    bouyer 
    784  1.16.42.3    bouyer 		mstirq |= MMCR_HBMSTIRQCTL_M_RTRTO_IRQ_SEL;
    785  1.16.42.3    bouyer 		mstirq |= MMCR_HBMSTIRQCTL_M_TABRT_IRQ_SEL;
    786  1.16.42.3    bouyer 		mstirq |= MMCR_HBMSTIRQCTL_M_MABRT_IRQ_SEL;
    787  1.16.42.3    bouyer 		mstirq |= MMCR_HBMSTIRQCTL_M_SERR_IRQ_SEL;
    788  1.16.42.3    bouyer 		mstirq |= MMCR_HBMSTIRQCTL_M_RPER_IRQ_SEL;
    789  1.16.42.3    bouyer 		mstirq |= MMCR_HBMSTIRQCTL_M_DPER_IRQ_SEL;
    790  1.16.42.3    bouyer 
    791  1.16.42.3    bouyer 		tgtirq |= MMCR_HBTGTIRQCTL_T_DLYTO_IRQ_SEL;
    792  1.16.42.3    bouyer 		tgtirq |= MMCR_HBTGTIRQCTL_T_APER_IRQ_SEL;
    793  1.16.42.3    bouyer 		tgtirq |= MMCR_HBTGTIRQCTL_T_DPER_IRQ_SEL;
    794  1.16.42.3    bouyer 	} else
    795  1.16.42.3    bouyer 		sc->sc_eih = elansc_intr_establish(self, elanpex_intr, sc);
    796  1.16.42.3    bouyer 
    797  1.16.42.3    bouyer 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_SYSARBCTL, sysarbctl);
    798  1.16.42.3    bouyer 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL, mstirq);
    799  1.16.42.3    bouyer 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL, tgtirq);
    800  1.16.42.3    bouyer }
    801  1.16.42.3    bouyer 
    802  1.16.42.3    bouyer static void
    803  1.16.42.3    bouyer elanpex_attach(device_t parent, device_t self, void *aux)
    804  1.16.42.3    bouyer {
    805  1.16.42.3    bouyer 	struct elansc_softc *sc = device_private(parent);
    806  1.16.42.3    bouyer 
    807  1.16.42.3    bouyer 	aprint_naive(": PCI Exceptions\n");
    808  1.16.42.3    bouyer 	aprint_normal(": AMD Elan SC520 PCI Exceptions\n");
    809  1.16.42.3    bouyer 
    810  1.16.42.3    bouyer 	elanpex_intr_establish(self, sc);
    811  1.16.42.3    bouyer 
    812  1.16.42.3    bouyer 	aprint_debug_dev(self, "HBMSTIRQCTL %04x\n",
    813  1.16.42.3    bouyer 	    bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL));
    814  1.16.42.3    bouyer 
    815  1.16.42.3    bouyer 	aprint_debug_dev(self, "HBTGTIRQCTL %04x\n",
    816  1.16.42.3    bouyer 	    bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL));
    817  1.16.42.3    bouyer 
    818  1.16.42.3    bouyer 	aprint_debug_dev(self, "PCIHOSTMAP %04x\n",
    819  1.16.42.3    bouyer 	    bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_PCIHOSTMAP));
    820  1.16.42.3    bouyer 
    821  1.16.42.3    bouyer 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
    822  1.16.42.3    bouyer 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG) |
    823  1.16.42.3    bouyer 	    PCI_COMMAND_PARITY_ENABLE|PCI_COMMAND_SERR_ENABLE);
    824  1.16.42.3    bouyer 
    825  1.16.42.3    bouyer 	if (!pmf_device_register(self, elanpex_suspend, elanpex_resume))
    826  1.16.42.3    bouyer 		aprint_error_dev(self, "could not establish power hooks\n");
    827  1.16.42.3    bouyer }
    828  1.16.42.3    bouyer 
    829  1.16.42.3    bouyer static void
    830  1.16.42.3    bouyer elanpex_intr_disestablish(struct elansc_softc *sc)
    831  1.16.42.3    bouyer {
    832  1.16.42.3    bouyer 	uint8_t sysarbctl;
    833  1.16.42.3    bouyer 	uint16_t pcihostmap, mstirq, tgtirq;
    834  1.16.42.3    bouyer 
    835  1.16.42.3    bouyer 	sysarbctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_SYSARBCTL);
    836  1.16.42.3    bouyer 	sysarbctl &= ~MMCR_SYSARBCTL_GNT_TO_INT_ENB;
    837  1.16.42.3    bouyer 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_SYSARBCTL, sysarbctl);
    838  1.16.42.3    bouyer 
    839  1.16.42.3    bouyer 	mstirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL);
    840  1.16.42.3    bouyer 	mstirq &= ~MMCR_HBMSTIRQCTL_M_RTRTO_IRQ_ENB;
    841  1.16.42.3    bouyer 	mstirq &= ~MMCR_HBMSTIRQCTL_M_TABRT_IRQ_ENB;
    842  1.16.42.3    bouyer 	mstirq &= ~MMCR_HBMSTIRQCTL_M_MABRT_IRQ_ENB;
    843  1.16.42.3    bouyer 	mstirq &= ~MMCR_HBMSTIRQCTL_M_SERR_IRQ_ENB;
    844  1.16.42.3    bouyer 	mstirq &= ~MMCR_HBMSTIRQCTL_M_RPER_IRQ_ENB;
    845  1.16.42.3    bouyer 	mstirq &= ~MMCR_HBMSTIRQCTL_M_DPER_IRQ_ENB;
    846  1.16.42.3    bouyer 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL, mstirq);
    847  1.16.42.3    bouyer 
    848  1.16.42.3    bouyer 	tgtirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL);
    849  1.16.42.3    bouyer 	tgtirq &= ~MMCR_HBTGTIRQCTL_T_DLYTO_IRQ_ENB;
    850  1.16.42.3    bouyer 	tgtirq &= ~MMCR_HBTGTIRQCTL_T_APER_IRQ_ENB;
    851  1.16.42.3    bouyer 	tgtirq &= ~MMCR_HBTGTIRQCTL_T_DPER_IRQ_ENB;
    852  1.16.42.3    bouyer 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL, tgtirq);
    853  1.16.42.3    bouyer 
    854  1.16.42.3    bouyer 	pcihostmap = bus_space_read_2(sc->sc_memt, sc->sc_memh,
    855  1.16.42.3    bouyer 	    MMCR_PCIHOSTMAP);
    856  1.16.42.3    bouyer 	/* Priority P2 (Master PIC IR1) */
    857  1.16.42.3    bouyer 	pcihostmap &= ~MMCR_PCIHOSTMAP_PCI_IRQ_MAP;
    858  1.16.42.3    bouyer 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_PCIHOSTMAP,
    859  1.16.42.3    bouyer 	    pcihostmap);
    860  1.16.42.3    bouyer 
    861  1.16.42.3    bouyer 	if (elansc_pcinmi)
    862  1.16.42.3    bouyer 		nmi_disestablish(sc->sc_eih);
    863  1.16.42.3    bouyer 	else
    864  1.16.42.3    bouyer 		intr_disestablish(sc->sc_eih);
    865  1.16.42.3    bouyer 	sc->sc_eih = NULL;
    866  1.16.42.3    bouyer 
    867  1.16.42.3    bouyer }
    868  1.16.42.3    bouyer 
    869  1.16.42.3    bouyer static int
    870  1.16.42.3    bouyer elanpex_detach(device_t self, int flags)
    871  1.16.42.3    bouyer {
    872  1.16.42.3    bouyer 	struct elansc_softc *sc = device_private(device_parent(self));
    873  1.16.42.3    bouyer 
    874  1.16.42.3    bouyer 	pmf_device_deregister(self);
    875  1.16.42.3    bouyer 	elanpex_intr_disestablish(sc);
    876  1.16.42.3    bouyer 
    877  1.16.42.3    bouyer 	return 0;
    878  1.16.42.3    bouyer }
    879  1.16.42.3    bouyer 
    880  1.16.42.3    bouyer static void
    881  1.16.42.3    bouyer elanpar_intr_establish(device_t self, struct elansc_softc *sc)
    882  1.16.42.3    bouyer {
    883  1.16.42.3    bouyer 	uint8_t adddecctl, wpvmap;
    884  1.16.42.3    bouyer 
    885  1.16.42.3    bouyer 	wpvmap = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_WPVMAP);
    886  1.16.42.3    bouyer 	wpvmap &= ~MMCR_WPVMAP_INT_MAP;
    887  1.16.42.3    bouyer 	if (elansc_wpvnmi)
    888  1.16.42.3    bouyer 		wpvmap |= MMCR_WPVMAP_INT_NMI;
    889  1.16.42.3    bouyer 	else
    890  1.16.42.3    bouyer 		wpvmap |= __SHIFTIN(__BIT(ELAN_IRQ), MMCR_WPVMAP_INT_MAP);
    891  1.16.42.3    bouyer 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_WPVMAP, wpvmap);
    892  1.16.42.3    bouyer 
    893  1.16.42.3    bouyer 	/* clear interrupt status */
    894  1.16.42.3    bouyer 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WPVSTA,
    895  1.16.42.3    bouyer 	    MMCR_WPVSTA_WPV_STA);
    896  1.16.42.3    bouyer 
    897  1.16.42.3    bouyer 	/* establish interrupt */
    898  1.16.42.3    bouyer 	if (elansc_wpvnmi)
    899  1.16.42.3    bouyer 		sc->sc_pih = nmi_establish(elanpar_intr, sc);
    900  1.16.42.3    bouyer 	else
    901  1.16.42.3    bouyer 		sc->sc_pih = elansc_intr_establish(self, elanpar_intr, sc);
    902  1.16.42.3    bouyer 
    903  1.16.42.3    bouyer 	adddecctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_ADDDECCTL);
    904  1.16.42.3    bouyer 	adddecctl |= MMCR_ADDDECCTL_WPV_INT_ENB;
    905  1.16.42.3    bouyer 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_ADDDECCTL, adddecctl);
    906  1.16.42.3    bouyer }
    907  1.16.42.3    bouyer 
    908  1.16.42.3    bouyer static void
    909  1.16.42.3    bouyer elanpar_attach(device_t parent, device_t self, void *aux)
    910  1.16.42.3    bouyer {
    911  1.16.42.3    bouyer 	struct elansc_softc *sc = device_private(parent);
    912  1.16.42.3    bouyer 
    913  1.16.42.3    bouyer 	aprint_naive(": Programmable Address Regions\n");
    914  1.16.42.3    bouyer 	aprint_normal(": AMD Elan SC520 Programmable Address Regions\n");
    915  1.16.42.3    bouyer 
    916  1.16.42.3    bouyer 	elansc_print_1(self, sc, MMCR_WPVMAP);
    917  1.16.42.3    bouyer 	elansc_print_all_par(self, sc->sc_memt, sc->sc_memh);
    918  1.16.42.3    bouyer 
    919  1.16.42.3    bouyer 	sc->sc_idtpar = elansc_protect_idt(sc);
    920  1.16.42.3    bouyer 	sc->sc_textpar = elansc_protect_text(self, sc);
    921  1.16.42.3    bouyer 
    922  1.16.42.3    bouyer 	elanpar_intr_establish(self, sc);
    923  1.16.42.3    bouyer 
    924  1.16.42.3    bouyer 	elansc_print_1(self, sc, MMCR_ADDDECCTL);
    925  1.16.42.3    bouyer 
    926  1.16.42.3    bouyer 	if (!pmf_device_register(self, elanpar_suspend, elanpar_resume))
    927  1.16.42.3    bouyer 		aprint_error_dev(self, "could not establish power hooks\n");
    928  1.16.42.3    bouyer }
    929  1.16.42.3    bouyer 
    930  1.16.42.3    bouyer static void
    931  1.16.42.3    bouyer elanpar_intr_disestablish(struct elansc_softc *sc)
    932  1.16.42.3    bouyer {
    933  1.16.42.3    bouyer 	uint8_t adddecctl, wpvmap;
    934  1.16.42.3    bouyer 
    935  1.16.42.3    bouyer 	/* disable interrupt, acknowledge it, disestablish our
    936  1.16.42.3    bouyer 	 * handler, unmap it
    937  1.16.42.3    bouyer 	 */
    938  1.16.42.3    bouyer 	adddecctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_ADDDECCTL);
    939  1.16.42.3    bouyer 	adddecctl &= ~MMCR_ADDDECCTL_WPV_INT_ENB;
    940  1.16.42.3    bouyer 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_ADDDECCTL, adddecctl);
    941  1.16.42.3    bouyer 
    942  1.16.42.3    bouyer 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WPVSTA,
    943  1.16.42.3    bouyer 	    MMCR_WPVSTA_WPV_STA);
    944  1.16.42.3    bouyer 
    945  1.16.42.3    bouyer 	if (elansc_wpvnmi)
    946  1.16.42.3    bouyer 		nmi_disestablish(sc->sc_pih);
    947  1.16.42.3    bouyer 	else
    948  1.16.42.3    bouyer 		intr_disestablish(sc->sc_pih);
    949  1.16.42.3    bouyer 	sc->sc_pih = NULL;
    950  1.16.42.3    bouyer 
    951  1.16.42.3    bouyer 	wpvmap = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_WPVMAP);
    952  1.16.42.3    bouyer 	wpvmap &= ~MMCR_WPVMAP_INT_MAP;
    953  1.16.42.3    bouyer 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_WPVMAP, wpvmap);
    954  1.16.42.3    bouyer }
    955  1.16.42.3    bouyer 
    956  1.16.42.3    bouyer static int
    957  1.16.42.3    bouyer elanpar_detach(device_t self, int flags)
    958  1.16.42.3    bouyer {
    959  1.16.42.3    bouyer 	struct elansc_softc *sc = device_private(device_parent(self));
    960  1.16.42.3    bouyer 
    961  1.16.42.3    bouyer 	pmf_device_deregister(self);
    962  1.16.42.3    bouyer 
    963  1.16.42.3    bouyer 	if (sc->sc_textpar != -1) {
    964  1.16.42.3    bouyer 		elansc_disable_par(sc->sc_memt, sc->sc_memh, sc->sc_textpar);
    965  1.16.42.3    bouyer 		sc->sc_textpar = -1;
    966  1.16.42.3    bouyer 	}
    967  1.16.42.3    bouyer 	if (sc->sc_idtpar != -1) {
    968  1.16.42.3    bouyer 		elansc_disable_par(sc->sc_memt, sc->sc_memh, sc->sc_idtpar);
    969  1.16.42.3    bouyer 		sc->sc_idtpar = -1;
    970  1.16.42.3    bouyer 	}
    971  1.16.42.3    bouyer 
    972  1.16.42.3    bouyer 	elanpar_intr_disestablish(sc);
    973  1.16.42.3    bouyer 
    974  1.16.42.3    bouyer 	return 0;
    975  1.16.42.3    bouyer }
    976  1.16.42.3    bouyer 
    977        1.1   thorpej static void
    978  1.16.42.2    bouyer elansc_attach(device_t parent, device_t self, void *aux)
    979        1.1   thorpej {
    980  1.16.42.1    bouyer 	struct elansc_softc *sc = device_private(self);
    981        1.1   thorpej 	struct pci_attach_args *pa = aux;
    982        1.1   thorpej 	uint16_t rev;
    983  1.16.42.3    bouyer 	uint8_t cpuctl, picicr, ressta;
    984  1.16.42.3    bouyer #if 0
    985  1.16.42.3    bouyer 	struct pci_conf_state pcf;
    986  1.16.42.3    bouyer #endif
    987       1.10  drochner #if NGPIO > 0
    988       1.10  drochner 	struct gpiobus_attach_args gba;
    989  1.16.42.3    bouyer 	int pin, reg, shift;
    990        1.9       riz 	uint16_t data;
    991       1.10  drochner #endif
    992  1.16.42.3    bouyer 	sc->sc_pc = pa->pa_pc;
    993  1.16.42.3    bouyer 	sc->sc_tag = pa->pa_tag;
    994        1.1   thorpej 
    995       1.14   thorpej 	aprint_naive(": System Controller\n");
    996       1.14   thorpej 	aprint_normal(": AMD Elan SC520 System Controller\n");
    997        1.1   thorpej 
    998        1.1   thorpej 	sc->sc_memt = pa->pa_memt;
    999        1.5   thorpej 	if (bus_space_map(sc->sc_memt, MMCR_BASE_ADDR, PAGE_SIZE, 0,
   1000        1.1   thorpej 	    &sc->sc_memh) != 0) {
   1001  1.16.42.2    bouyer 		aprint_error_dev(&sc->sc_dev, "unable to map registers\n");
   1002        1.1   thorpej 		return;
   1003        1.1   thorpej 	}
   1004        1.1   thorpej 
   1005  1.16.42.1    bouyer 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_HIGH);
   1006  1.16.42.1    bouyer 
   1007        1.1   thorpej 	rev = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_REVID);
   1008        1.1   thorpej 	cpuctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_CPUCTL);
   1009        1.1   thorpej 
   1010  1.16.42.2    bouyer 	aprint_normal_dev(&sc->sc_dev,
   1011  1.16.42.2    bouyer 	    "product %d stepping %d.%d, CPU clock %s\n",
   1012        1.1   thorpej 	    (rev & REVID_PRODID) >> REVID_PRODID_SHIFT,
   1013        1.1   thorpej 	    (rev & REVID_MAJSTEP) >> REVID_MAJSTEP_SHIFT,
   1014        1.1   thorpej 	    (rev & REVID_MINSTEP),
   1015        1.1   thorpej 	    elansc_speeds[cpuctl & CPUCTL_CPU_CLK_SPD_MASK]);
   1016        1.1   thorpej 
   1017        1.1   thorpej 	/*
   1018        1.1   thorpej 	 * SC520 rev A1 has a bug that affects the watchdog timer.  If
   1019        1.1   thorpej 	 * the GP bus echo mode is enabled, writing to the watchdog control
   1020        1.1   thorpej 	 * register is blocked.
   1021        1.1   thorpej 	 *
   1022        1.1   thorpej 	 * The BIOS in some systems (e.g. the Soekris net4501) enables
   1023        1.1   thorpej 	 * GP bus echo for various reasons, so we need to switch it off
   1024        1.1   thorpej 	 * when we talk to the watchdog timer.
   1025        1.1   thorpej 	 *
   1026        1.1   thorpej 	 * XXX The step 1.1 (B1?) in my Soekris net4501 also has this
   1027        1.1   thorpej 	 * XXX problem, so we'll just enable it for all Elan SC520s
   1028        1.8    keihan 	 * XXX for now.  --thorpej (at) NetBSD.org
   1029        1.1   thorpej 	 */
   1030        1.1   thorpej 	if (1 || rev == ((PRODID_ELAN_SC520 << REVID_PRODID_SHIFT) |
   1031        1.1   thorpej 		    (0 << REVID_MAJSTEP_SHIFT) | (1)))
   1032        1.1   thorpej 		sc->sc_echobug = 1;
   1033        1.1   thorpej 
   1034        1.1   thorpej 	/*
   1035        1.1   thorpej 	 * Determine cause of the last reset, and issue a warning if it
   1036        1.1   thorpej 	 * was due to watchdog expiry.
   1037        1.1   thorpej 	 */
   1038        1.1   thorpej 	ressta = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_RESSTA);
   1039        1.1   thorpej 	if (ressta & RESSTA_WDT_RST_DET)
   1040  1.16.42.2    bouyer 		aprint_error_dev(&sc->sc_dev,
   1041  1.16.42.2    bouyer 		    "WARNING: LAST RESET DUE TO WATCHDOG EXPIRATION!\n");
   1042        1.1   thorpej 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_RESSTA, ressta);
   1043        1.1   thorpej 
   1044  1.16.42.3    bouyer 	elansc_print_1(self, sc, MMCR_MPICMODE);
   1045  1.16.42.3    bouyer 	elansc_print_1(self, sc, MMCR_SL1PICMODE);
   1046  1.16.42.3    bouyer 	elansc_print_1(self, sc, MMCR_SL2PICMODE);
   1047  1.16.42.3    bouyer 	elansc_print_1(self, sc, MMCR_PICICR);
   1048  1.16.42.3    bouyer 
   1049  1.16.42.3    bouyer 	sc->sc_mpicmode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
   1050  1.16.42.3    bouyer 	    MMCR_MPICMODE);
   1051  1.16.42.3    bouyer 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_MPICMODE,
   1052  1.16.42.3    bouyer 	    sc->sc_mpicmode | __BIT(ELAN_IRQ));
   1053  1.16.42.3    bouyer 
   1054  1.16.42.3    bouyer 	sc->sc_picicr = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_PICICR);
   1055  1.16.42.3    bouyer 	picicr = sc->sc_picicr;
   1056  1.16.42.3    bouyer 	if (elansc_pcinmi || elansc_wpvnmi)
   1057  1.16.42.3    bouyer 		picicr |= MMCR_PICICR_NMI_ENB;
   1058  1.16.42.3    bouyer #if 0
   1059  1.16.42.3    bouyer 	/* PC/AT compatibility */
   1060  1.16.42.3    bouyer 	picicr |= MMCR_PICICR_S1_GINT_MODE|MMCR_PICICR_M_GINT_MODE;
   1061  1.16.42.3    bouyer #endif
   1062  1.16.42.3    bouyer 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_PICICR, picicr);
   1063  1.16.42.3    bouyer 
   1064  1.16.42.3    bouyer 	elansc_print_1(self, sc, MMCR_PICICR);
   1065  1.16.42.3    bouyer 	elansc_print_1(self, sc, MMCR_MPICMODE);
   1066  1.16.42.3    bouyer 
   1067  1.16.42.3    bouyer 	mutex_enter(&sc->sc_mtx);
   1068        1.1   thorpej 	/* Set up the watchdog registers with some defaults. */
   1069        1.1   thorpej 	elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
   1070        1.1   thorpej 
   1071        1.1   thorpej 	/* ...and clear it. */
   1072        1.1   thorpej 	elansc_wdogctl_reset(sc);
   1073  1.16.42.3    bouyer 	mutex_exit(&sc->sc_mtx);
   1074        1.9       riz 
   1075  1.16.42.3    bouyer 	if (!pmf_device_register(self, elansc_suspend, elansc_resume))
   1076  1.16.42.3    bouyer 		aprint_error_dev(self, "could not establish power hooks\n");
   1077  1.16.42.1    bouyer 
   1078       1.10  drochner #if NGPIO > 0
   1079        1.9       riz 	/* Initialize GPIO pins array */
   1080        1.9       riz 	for (pin = 0; pin < ELANSC_PIO_NPINS; pin++) {
   1081        1.9       riz 		sc->sc_gpio_pins[pin].pin_num = pin;
   1082        1.9       riz 		sc->sc_gpio_pins[pin].pin_caps = GPIO_PIN_INPUT |
   1083        1.9       riz 		    GPIO_PIN_OUTPUT;
   1084        1.9       riz 
   1085        1.9       riz 		/* Read initial state */
   1086        1.9       riz 		reg = (pin < 16 ? MMCR_PIODIR15_0 : MMCR_PIODIR31_16);
   1087        1.9       riz 		shift = pin % 16;
   1088        1.9       riz 		data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
   1089        1.9       riz 		if ((data & (1 << shift)) == 0)
   1090        1.9       riz 			sc->sc_gpio_pins[pin].pin_flags = GPIO_PIN_INPUT;
   1091        1.9       riz 		else
   1092        1.9       riz 			sc->sc_gpio_pins[pin].pin_flags = GPIO_PIN_OUTPUT;
   1093        1.9       riz 		if (elansc_gpio_pin_read(sc, pin) == 0)
   1094        1.9       riz 			sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_LOW;
   1095        1.9       riz 		else
   1096        1.9       riz 			sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_HIGH;
   1097        1.9       riz 	}
   1098        1.9       riz 
   1099        1.9       riz 	/* Create controller tag */
   1100        1.9       riz 	sc->sc_gpio_gc.gp_cookie = sc;
   1101        1.9       riz 	sc->sc_gpio_gc.gp_pin_read = elansc_gpio_pin_read;
   1102        1.9       riz 	sc->sc_gpio_gc.gp_pin_write = elansc_gpio_pin_write;
   1103        1.9       riz 	sc->sc_gpio_gc.gp_pin_ctl = elansc_gpio_pin_ctl;
   1104        1.9       riz 
   1105        1.9       riz 	gba.gba_gc = &sc->sc_gpio_gc;
   1106        1.9       riz 	gba.gba_pins = sc->sc_gpio_pins;
   1107        1.9       riz 	gba.gba_npins = ELANSC_PIO_NPINS;
   1108        1.9       riz 
   1109  1.16.42.3    bouyer 	sc->sc_par = config_found_ia(&sc->sc_dev, "elanparbus", NULL, NULL);
   1110  1.16.42.3    bouyer 	sc->sc_pex = config_found_ia(&sc->sc_dev, "elanpexbus", NULL, NULL);
   1111        1.9       riz 	/* Attach GPIO framework */
   1112       1.10  drochner 	config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);
   1113       1.10  drochner #endif /* NGPIO */
   1114  1.16.42.1    bouyer 
   1115  1.16.42.1    bouyer 	/*
   1116  1.16.42.1    bouyer 	 * Hook up the watchdog timer.
   1117  1.16.42.1    bouyer 	 */
   1118  1.16.42.2    bouyer 	sc->sc_smw.smw_name = device_xname(&sc->sc_dev);
   1119  1.16.42.1    bouyer 	sc->sc_smw.smw_cookie = sc;
   1120  1.16.42.1    bouyer 	sc->sc_smw.smw_setmode = elansc_wdog_setmode;
   1121  1.16.42.1    bouyer 	sc->sc_smw.smw_tickle = elansc_wdog_tickle;
   1122  1.16.42.1    bouyer 	sc->sc_smw.smw_period = 32;	/* actually 32.54 */
   1123  1.16.42.2    bouyer 	if (sysmon_wdog_register(&sc->sc_smw) != 0) {
   1124  1.16.42.2    bouyer 		aprint_error_dev(&sc->sc_dev,
   1125  1.16.42.2    bouyer 		    "unable to register watchdog with sysmon\n");
   1126  1.16.42.2    bouyer 	}
   1127        1.1   thorpej }
   1128        1.1   thorpej 
   1129  1.16.42.3    bouyer static int
   1130  1.16.42.3    bouyer elanpex_match(device_t parent, struct cfdata *match, void *aux)
   1131  1.16.42.3    bouyer {
   1132  1.16.42.3    bouyer 	struct elansc_softc *sc = device_private(parent);
   1133  1.16.42.3    bouyer 
   1134  1.16.42.3    bouyer 	return sc->sc_pex == NULL;
   1135  1.16.42.3    bouyer }
   1136  1.16.42.3    bouyer 
   1137  1.16.42.3    bouyer static int
   1138  1.16.42.3    bouyer elanpar_match(device_t parent, struct cfdata *match, void *aux)
   1139  1.16.42.3    bouyer {
   1140  1.16.42.3    bouyer 	struct elansc_softc *sc = device_private(parent);
   1141  1.16.42.3    bouyer 
   1142  1.16.42.3    bouyer 	return sc->sc_par == NULL;
   1143  1.16.42.3    bouyer }
   1144  1.16.42.3    bouyer 
   1145  1.16.42.3    bouyer CFATTACH_DECL_NEW(elanpar, sizeof(struct device),
   1146  1.16.42.3    bouyer     elanpar_match, elanpar_attach, elanpar_detach, NULL);
   1147  1.16.42.3    bouyer 
   1148  1.16.42.3    bouyer CFATTACH_DECL_NEW(elanpex, sizeof(struct device),
   1149  1.16.42.3    bouyer     elanpex_match, elanpex_attach, elanpex_detach, NULL);
   1150  1.16.42.3    bouyer 
   1151  1.16.42.1    bouyer CFATTACH_DECL2(elansc, sizeof(struct elansc_softc),
   1152  1.16.42.1    bouyer     elansc_match, elansc_attach, elansc_detach, NULL, NULL,
   1153  1.16.42.1    bouyer     elansc_childdetached);
   1154        1.9       riz 
   1155       1.10  drochner #if NGPIO > 0
   1156        1.9       riz static int
   1157        1.9       riz elansc_gpio_pin_read(void *arg, int pin)
   1158        1.9       riz {
   1159        1.9       riz 	struct elansc_softc *sc = arg;
   1160        1.9       riz 	int reg, shift;
   1161       1.13     perry 	uint16_t data;
   1162        1.9       riz 
   1163        1.9       riz 	reg = (pin < 16 ? MMCR_PIODATA15_0 : MMCR_PIODATA31_16);
   1164        1.9       riz 	shift = pin % 16;
   1165  1.16.42.1    bouyer 
   1166  1.16.42.1    bouyer 	mutex_enter(&sc->sc_mtx);
   1167        1.9       riz 	data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
   1168  1.16.42.1    bouyer 	mutex_exit(&sc->sc_mtx);
   1169        1.9       riz 
   1170        1.9       riz 	return ((data >> shift) & 0x1);
   1171        1.9       riz }
   1172        1.9       riz 
   1173        1.9       riz static void
   1174        1.9       riz elansc_gpio_pin_write(void *arg, int pin, int value)
   1175        1.9       riz {
   1176        1.9       riz 	struct elansc_softc *sc = arg;
   1177        1.9       riz 	int reg, shift;
   1178       1.13     perry 	uint16_t data;
   1179        1.9       riz 
   1180        1.9       riz 	reg = (pin < 16 ? MMCR_PIODATA15_0 : MMCR_PIODATA31_16);
   1181        1.9       riz 	shift = pin % 16;
   1182  1.16.42.1    bouyer 
   1183  1.16.42.1    bouyer 	mutex_enter(&sc->sc_mtx);
   1184        1.9       riz 	data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
   1185        1.9       riz 	if (value == 0)
   1186        1.9       riz 		data &= ~(1 << shift);
   1187        1.9       riz 	else if (value == 1)
   1188        1.9       riz 		data |= (1 << shift);
   1189        1.9       riz 
   1190        1.9       riz 	bus_space_write_2(sc->sc_memt, sc->sc_memh, reg, data);
   1191  1.16.42.1    bouyer 	mutex_exit(&sc->sc_mtx);
   1192        1.9       riz }
   1193        1.9       riz 
   1194        1.9       riz static void
   1195        1.9       riz elansc_gpio_pin_ctl(void *arg, int pin, int flags)
   1196        1.9       riz {
   1197        1.9       riz 	struct elansc_softc *sc = arg;
   1198        1.9       riz 	int reg, shift;
   1199       1.13     perry 	uint16_t data;
   1200        1.9       riz 
   1201        1.9       riz 	reg = (pin < 16 ? MMCR_PIODIR15_0 : MMCR_PIODIR31_16);
   1202        1.9       riz 	shift = pin % 16;
   1203  1.16.42.1    bouyer 	mutex_enter(&sc->sc_mtx);
   1204        1.9       riz 	data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
   1205        1.9       riz 	if (flags & GPIO_PIN_INPUT)
   1206        1.9       riz 		data &= ~(1 << shift);
   1207        1.9       riz 	if (flags & GPIO_PIN_OUTPUT)
   1208        1.9       riz 		data |= (1 << shift);
   1209        1.9       riz 
   1210        1.9       riz 	bus_space_write_2(sc->sc_memt, sc->sc_memh, reg, data);
   1211  1.16.42.1    bouyer 	mutex_exit(&sc->sc_mtx);
   1212        1.9       riz }
   1213       1.10  drochner #endif /* NGPIO */
   1214