Home | History | Annotate | Line # | Download | only in pci
elan520.c revision 1.31.4.1
      1  1.31.4.1      yamt /*	$NetBSD: elan520.c,v 1.31.4.1 2008/05/16 02:22:37 yamt 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  *
     19       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1   thorpej  */
     31       1.1   thorpej 
     32       1.1   thorpej /*
     33       1.1   thorpej  * Device driver for the AMD Elan SC520 System Controller.  This attaches
     34       1.1   thorpej  * where the "pchb" driver might normally attach, and provides support for
     35       1.1   thorpej  * extra features on the SC520, such as the watchdog timer and GPIO.
     36       1.1   thorpej  *
     37       1.1   thorpej  * Information about the GP bus echo bug work-around is from code posted
     38       1.1   thorpej  * to the "soekris-tech" mailing list by Jasper Wallace.
     39       1.1   thorpej  */
     40       1.1   thorpej 
     41       1.1   thorpej #include <sys/cdefs.h>
     42       1.1   thorpej 
     43  1.31.4.1      yamt __KERNEL_RCSID(0, "$NetBSD: elan520.c,v 1.31.4.1 2008/05/16 02:22:37 yamt Exp $");
     44       1.1   thorpej 
     45       1.1   thorpej #include <sys/param.h>
     46       1.1   thorpej #include <sys/systm.h>
     47      1.22    dyoung #include <sys/time.h>
     48       1.1   thorpej #include <sys/device.h>
     49      1.19    dyoung #include <sys/gpio.h>
     50      1.19    dyoung #include <sys/mutex.h>
     51       1.1   thorpej #include <sys/wdog.h>
     52       1.1   thorpej 
     53       1.5   thorpej #include <uvm/uvm_extern.h>
     54       1.5   thorpej 
     55       1.1   thorpej #include <machine/bus.h>
     56       1.1   thorpej 
     57       1.1   thorpej #include <dev/pci/pcivar.h>
     58       1.1   thorpej 
     59       1.1   thorpej #include <dev/pci/pcidevs.h>
     60       1.1   thorpej 
     61      1.10  drochner #include "gpio.h"
     62      1.10  drochner #if NGPIO > 0
     63       1.9       riz #include <dev/gpio/gpiovar.h>
     64      1.10  drochner #endif
     65       1.9       riz 
     66       1.1   thorpej #include <arch/i386/pci/elan520reg.h>
     67       1.1   thorpej 
     68       1.1   thorpej #include <dev/sysmon/sysmonvar.h>
     69       1.1   thorpej 
     70      1.22    dyoung #define	ELAN_IRQ	1
     71      1.23    dyoung #define	PG0_PROT_SIZE	PAGE_SIZE
     72      1.22    dyoung 
     73       1.1   thorpej struct elansc_softc {
     74      1.29    dyoung 	device_t sc_dev;
     75      1.22    dyoung 	device_t sc_par;
     76      1.22    dyoung 	device_t sc_pex;
     77      1.31    dyoung 	device_t sc_pci;
     78      1.22    dyoung 
     79      1.22    dyoung 	pci_chipset_tag_t sc_pc;
     80      1.22    dyoung 	pcitag_t sc_tag;
     81       1.1   thorpej 	bus_space_tag_t sc_memt;
     82       1.1   thorpej 	bus_space_handle_t sc_memh;
     83       1.1   thorpej 	int sc_echobug;
     84       1.1   thorpej 
     85      1.19    dyoung 	kmutex_t sc_mtx;
     86      1.19    dyoung 
     87       1.1   thorpej 	struct sysmon_wdog sc_smw;
     88      1.22    dyoung 	void		*sc_eih;
     89      1.22    dyoung 	void		*sc_pih;
     90      1.22    dyoung 	void		*sc_sh;
     91      1.22    dyoung 	uint8_t		sc_mpicmode;
     92      1.22    dyoung 	uint8_t		sc_picicr;
     93      1.23    dyoung 	int		sc_pg0par;
     94      1.27    dyoung 	int		sc_textpar[3];
     95      1.11       riz #if NGPIO > 0
     96       1.9       riz 	/* GPIO interface */
     97       1.9       riz 	struct gpio_chipset_tag sc_gpio_gc;
     98       1.9       riz 	gpio_pin_t sc_gpio_pins[ELANSC_PIO_NPINS];
     99      1.11       riz #endif
    100       1.1   thorpej };
    101       1.1   thorpej 
    102      1.31    dyoung static bool elansc_attached = false;
    103      1.22    dyoung int elansc_wpvnmi = 1;
    104      1.22    dyoung int elansc_pcinmi = 1;
    105      1.23    dyoung int elansc_do_protect_pg0 = 1;
    106      1.22    dyoung 
    107      1.10  drochner #if NGPIO > 0
    108       1.9       riz static int	elansc_gpio_pin_read(void *, int);
    109       1.9       riz static void	elansc_gpio_pin_write(void *, int, int);
    110       1.9       riz static void	elansc_gpio_pin_ctl(void *, int, int);
    111      1.10  drochner #endif
    112       1.9       riz 
    113      1.22    dyoung static void elansc_print_par(device_t, int, uint32_t);
    114      1.31    dyoung 
    115      1.22    dyoung static void elanpar_intr_establish(device_t, struct elansc_softc *);
    116      1.22    dyoung static void elanpar_intr_disestablish(struct elansc_softc *);
    117      1.26    dyoung static bool elanpar_shutdown(device_t, int);
    118      1.31    dyoung 
    119      1.31    dyoung static void elanpex_intr_establish(device_t, struct elansc_softc *);
    120      1.31    dyoung static void elanpex_intr_disestablish(struct elansc_softc *);
    121      1.26    dyoung static bool elanpex_shutdown(device_t, int);
    122      1.22    dyoung 
    123      1.27    dyoung static void elansc_protect(struct elansc_softc *, int, paddr_t, uint32_t);
    124      1.27    dyoung 
    125      1.28    dyoung static const uint32_t sfkb = 64 * 1024, fkb = 4 * 1024;
    126      1.28    dyoung 
    127       1.1   thorpej static void
    128      1.19    dyoung elansc_childdetached(device_t self, device_t child)
    129      1.19    dyoung {
    130      1.22    dyoung 	struct elansc_softc *sc = device_private(self);
    131      1.22    dyoung 
    132      1.22    dyoung 	if (child == sc->sc_par)
    133      1.22    dyoung 		sc->sc_par = NULL;
    134      1.22    dyoung 	if (child == sc->sc_pex)
    135      1.22    dyoung 		sc->sc_pex = NULL;
    136      1.31    dyoung 	if (child == sc->sc_pci)
    137      1.31    dyoung 		sc->sc_pci = NULL;
    138      1.31    dyoung 
    139      1.22    dyoung 	/* elansc does not presently keep a pointer to
    140      1.22    dyoung 	 * the gpio, so there is nothing to do if it is detached.
    141      1.19    dyoung 	 */
    142      1.19    dyoung }
    143      1.19    dyoung 
    144      1.31    dyoung static int
    145      1.31    dyoung elansc_match(device_t parent, cfdata_t match, void *aux)
    146      1.31    dyoung {
    147      1.31    dyoung 	struct pcibus_attach_args *pba = aux;
    148      1.31    dyoung 	pcitag_t tag;
    149      1.31    dyoung 	pcireg_t id;
    150      1.31    dyoung 
    151      1.31    dyoung 	if (elansc_attached)
    152      1.31    dyoung 		return 0;
    153      1.31    dyoung 
    154      1.31    dyoung 	if (pcimatch(parent, match, aux) == 0)
    155      1.31    dyoung 		return 0;
    156      1.31    dyoung 
    157      1.31    dyoung 	if (pba->pba_bus != 0)
    158      1.31    dyoung 		return 0;
    159      1.31    dyoung 
    160      1.31    dyoung 	tag = pci_make_tag(pba->pba_pc, 0, 0, 0);
    161      1.31    dyoung 	id = pci_conf_read(pba->pba_pc, tag, PCI_ID_REG);
    162      1.31    dyoung 
    163      1.31    dyoung 	if (PCI_VENDOR(id) == PCI_VENDOR_AMD &&
    164      1.31    dyoung 	    PCI_PRODUCT(id) == PCI_PRODUCT_AMD_SC520_SC)
    165      1.31    dyoung 		return 10;
    166      1.31    dyoung 
    167      1.31    dyoung 	return 0;
    168      1.31    dyoung }
    169      1.31    dyoung 
    170      1.31    dyoung /*
    171      1.31    dyoung  * Performance tuning for Soekris net4501:
    172      1.31    dyoung  *   - enable SDRAM write buffer and read prefetching
    173      1.31    dyoung  */
    174      1.31    dyoung #if 0
    175      1.31    dyoung 	uint8_t dbctl;
    176      1.31    dyoung 
    177      1.31    dyoung 	dbctl = bus_space_read_1(memt, memh, MMCR_DBCTL);
    178      1.31    dyoung  	dbctl &= ~MMCR_DBCTL_WB_WM_MASK;
    179      1.31    dyoung 	dbctl |= MMCR_DBCTL_WB_WM_16DW;
    180      1.31    dyoung 	dbctl |= MMCR_DBCTL_WB_ENB | MMCR_DBCTL_RAB_ENB;
    181      1.31    dyoung 	bus_space_write_1(memt, memh, MMCR_DBCTL, dbctl);
    182      1.31    dyoung #endif
    183      1.31    dyoung 
    184      1.31    dyoung /*
    185      1.31    dyoung  * Performance tuning for PCI bus on the AMD Elan SC520:
    186      1.31    dyoung  *   - enable concurrent arbitration of PCI and CPU busses
    187      1.31    dyoung  *     (and PCI buffer)
    188      1.31    dyoung  *   - enable PCI automatic delayed read transactions and
    189      1.31    dyoung  *     write posting
    190      1.31    dyoung  *   - enable PCI read buffer snooping (coherency)
    191      1.31    dyoung  */
    192      1.31    dyoung static void
    193      1.31    dyoung elansc_perf_tune(device_t self, bus_space_tag_t memt, bus_space_handle_t memh)
    194      1.31    dyoung {
    195      1.31    dyoung 	uint8_t sysarbctl;
    196      1.31    dyoung 	uint16_t hbctl;
    197      1.31    dyoung 	const bool concurrency = true;	/* concurrent bus arbitration */
    198      1.31    dyoung 
    199      1.31    dyoung 	sysarbctl = bus_space_read_1(memt, memh, MMCR_SYSARBCTL);
    200      1.31    dyoung 	if ((sysarbctl & MMCR_SYSARBCTL_CNCR_MODE_ENB) != 0) {
    201      1.31    dyoung 		aprint_debug_dev(self,
    202      1.31    dyoung 		    "concurrent arbitration mode is active\n");
    203      1.31    dyoung 	} else if (concurrency) {
    204      1.31    dyoung 		aprint_verbose_dev(self, "activating concurrent "
    205      1.31    dyoung 		    "arbitration mode\n");
    206      1.31    dyoung 		/* activate concurrent bus arbitration */
    207      1.31    dyoung 		sysarbctl |= MMCR_SYSARBCTL_CNCR_MODE_ENB;
    208      1.31    dyoung 		bus_space_write_1(memt, memh, MMCR_SYSARBCTL, sysarbctl);
    209      1.31    dyoung 	}
    210      1.31    dyoung 
    211      1.31    dyoung 	hbctl = bus_space_read_2(memt, memh, MMCR_HBCTL);
    212      1.31    dyoung 
    213      1.31    dyoung 	/* target read FIFO snoop */
    214      1.31    dyoung 	if ((hbctl & MMCR_HBCTL_T_PURGE_RD_ENB) != 0)
    215      1.31    dyoung 		aprint_debug_dev(self, "read-FIFO snooping is active\n");
    216      1.31    dyoung 	else {
    217      1.31    dyoung 		aprint_verbose_dev(self, "activating read-FIFO snooping\n");
    218      1.31    dyoung 		hbctl |= MMCR_HBCTL_T_PURGE_RD_ENB;
    219      1.31    dyoung 	}
    220      1.31    dyoung 
    221      1.31    dyoung 	if ((hbctl & MMCR_HBCTL_M_WPOST_ENB) != 0)
    222      1.31    dyoung 		aprint_debug_dev(self, "CPU->PCI write-posting is active\n");
    223      1.31    dyoung 	else if (concurrency) {
    224      1.31    dyoung 		aprint_verbose_dev(self, "activating CPU->PCI write-posting\n");
    225      1.31    dyoung 		hbctl |= MMCR_HBCTL_M_WPOST_ENB;
    226      1.31    dyoung 	}
    227      1.31    dyoung 
    228      1.31    dyoung 	/* auto delay read txn: looks safe, but seems to cause
    229      1.31    dyoung 	 * net4526 w/ minipci ath fits
    230      1.31    dyoung 	 */
    231      1.31    dyoung #if 0
    232      1.31    dyoung 	if ((hbctl & MMCR_HBCTL_T_DLYTR_ENB_AUTORETRY) != 0)
    233      1.31    dyoung 		aprint_debug_dev(self,
    234      1.31    dyoung 		    "automatic read transaction delay is active\n");
    235      1.31    dyoung 	else {
    236      1.31    dyoung 		aprint_verbose_dev(self,
    237      1.31    dyoung 		    "activating automatic read transaction delay\n");
    238      1.31    dyoung 		hbctl |= MMCR_HBCTL_T_DLYTR_ENB_AUTORETRY;
    239      1.31    dyoung 	}
    240      1.31    dyoung #endif
    241      1.31    dyoung 	bus_space_write_2(memt, memh, MMCR_HBCTL, hbctl);
    242      1.31    dyoung }
    243      1.31    dyoung 
    244      1.19    dyoung static void
    245       1.1   thorpej elansc_wdogctl_write(struct elansc_softc *sc, uint16_t val)
    246       1.1   thorpej {
    247       1.6  christos 	uint8_t echo_mode = 0; /* XXX: gcc */
    248       1.1   thorpej 
    249      1.19    dyoung 	KASSERT(mutex_owned(&sc->sc_mtx));
    250       1.1   thorpej 
    251       1.1   thorpej 	/* Switch off GP bus echo mode if we need to. */
    252       1.1   thorpej 	if (sc->sc_echobug) {
    253       1.1   thorpej 		echo_mode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    254       1.1   thorpej 		    MMCR_GPECHO);
    255       1.1   thorpej 		bus_space_write_1(sc->sc_memt, sc->sc_memh,
    256       1.1   thorpej 		    MMCR_GPECHO, echo_mode & ~GPECHO_GP_ECHO_ENB);
    257       1.1   thorpej 	}
    258       1.1   thorpej 
    259       1.1   thorpej 	/* Unlock the register. */
    260       1.1   thorpej 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    261       1.1   thorpej 	    WDTMRCTL_UNLOCK1);
    262       1.1   thorpej 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    263       1.1   thorpej 	    WDTMRCTL_UNLOCK2);
    264       1.1   thorpej 
    265       1.1   thorpej 	/* Write the value. */
    266       1.1   thorpej 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL, val);
    267       1.1   thorpej 
    268       1.1   thorpej 	/* Switch GP bus echo mode back. */
    269       1.1   thorpej 	if (sc->sc_echobug)
    270       1.1   thorpej 		bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_GPECHO,
    271       1.1   thorpej 		    echo_mode);
    272       1.1   thorpej }
    273       1.1   thorpej 
    274       1.1   thorpej static void
    275       1.1   thorpej elansc_wdogctl_reset(struct elansc_softc *sc)
    276       1.1   thorpej {
    277       1.7  christos 	uint8_t echo_mode = 0/* XXX: gcc */;
    278       1.1   thorpej 
    279      1.19    dyoung 	KASSERT(mutex_owned(&sc->sc_mtx));
    280       1.1   thorpej 
    281       1.1   thorpej 	/* Switch off GP bus echo mode if we need to. */
    282       1.1   thorpej 	if (sc->sc_echobug) {
    283       1.1   thorpej 		echo_mode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    284       1.1   thorpej 		    MMCR_GPECHO);
    285       1.1   thorpej 		bus_space_write_1(sc->sc_memt, sc->sc_memh,
    286       1.1   thorpej 		    MMCR_GPECHO, echo_mode & ~GPECHO_GP_ECHO_ENB);
    287       1.1   thorpej 	}
    288       1.1   thorpej 
    289       1.1   thorpej 	/* Reset the watchdog. */
    290       1.1   thorpej 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    291       1.1   thorpej 	    WDTMRCTL_RESET1);
    292       1.1   thorpej 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
    293       1.1   thorpej 	    WDTMRCTL_RESET2);
    294       1.1   thorpej 
    295       1.1   thorpej 	/* Switch GP bus echo mode back. */
    296       1.1   thorpej 	if (sc->sc_echobug)
    297       1.1   thorpej 		bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_GPECHO,
    298       1.1   thorpej 		    echo_mode);
    299       1.1   thorpej }
    300       1.1   thorpej 
    301       1.1   thorpej static const struct {
    302       1.1   thorpej 	int	period;		/* whole seconds */
    303       1.1   thorpej 	uint16_t exp;		/* exponent select */
    304       1.1   thorpej } elansc_wdog_periods[] = {
    305       1.1   thorpej 	{ 1,	WDTMRCTL_EXP_SEL25 },
    306       1.1   thorpej 	{ 2,	WDTMRCTL_EXP_SEL26 },
    307       1.1   thorpej 	{ 4,	WDTMRCTL_EXP_SEL27 },
    308       1.1   thorpej 	{ 8,	WDTMRCTL_EXP_SEL28 },
    309       1.1   thorpej 	{ 16,	WDTMRCTL_EXP_SEL29 },
    310       1.1   thorpej 	{ 32,	WDTMRCTL_EXP_SEL30 },
    311       1.1   thorpej 	{ 0,	0 },
    312       1.1   thorpej };
    313       1.1   thorpej 
    314       1.1   thorpej static int
    315      1.19    dyoung elansc_wdog_arm(struct elansc_softc *sc)
    316       1.1   thorpej {
    317      1.19    dyoung 	struct sysmon_wdog *smw = &sc->sc_smw;
    318       1.1   thorpej 	int i;
    319       1.7  christos 	uint16_t exp_sel = 0; /* XXX: gcc */
    320       1.1   thorpej 
    321      1.19    dyoung 	KASSERT(mutex_owned(&sc->sc_mtx));
    322      1.17    dyoung 
    323      1.19    dyoung 	if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
    324      1.19    dyoung 		smw->smw_period = 32;
    325      1.19    dyoung 		exp_sel = WDTMRCTL_EXP_SEL30;
    326       1.1   thorpej 	} else {
    327      1.19    dyoung 		for (i = 0; elansc_wdog_periods[i].period != 0; i++) {
    328      1.19    dyoung 			if (elansc_wdog_periods[i].period ==
    329      1.19    dyoung 			    smw->smw_period) {
    330      1.19    dyoung 				exp_sel = elansc_wdog_periods[i].exp;
    331      1.19    dyoung 				break;
    332       1.1   thorpej 			}
    333       1.1   thorpej 		}
    334      1.19    dyoung 		if (elansc_wdog_periods[i].period == 0)
    335      1.19    dyoung 			return EINVAL;
    336       1.1   thorpej 	}
    337      1.19    dyoung 	elansc_wdogctl_write(sc, WDTMRCTL_ENB |
    338      1.19    dyoung 	    WDTMRCTL_WRST_ENB | exp_sel);
    339      1.19    dyoung 	elansc_wdogctl_reset(sc);
    340      1.19    dyoung 	return 0;
    341      1.19    dyoung }
    342      1.19    dyoung 
    343      1.19    dyoung static int
    344      1.19    dyoung elansc_wdog_setmode(struct sysmon_wdog *smw)
    345      1.19    dyoung {
    346      1.19    dyoung 	struct elansc_softc *sc = smw->smw_cookie;
    347      1.19    dyoung 	int rc = 0;
    348      1.19    dyoung 
    349      1.19    dyoung 	mutex_enter(&sc->sc_mtx);
    350      1.19    dyoung 
    351      1.29    dyoung 	if (!device_is_active(sc->sc_dev))
    352      1.19    dyoung 		rc = EBUSY;
    353      1.19    dyoung 	else if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    354      1.19    dyoung 		elansc_wdogctl_write(sc,
    355      1.19    dyoung 		    WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
    356      1.19    dyoung 	} else
    357      1.19    dyoung 		rc = elansc_wdog_arm(sc);
    358      1.19    dyoung 
    359      1.19    dyoung 	mutex_exit(&sc->sc_mtx);
    360      1.19    dyoung 	return rc;
    361       1.1   thorpej }
    362       1.1   thorpej 
    363       1.1   thorpej static int
    364       1.1   thorpej elansc_wdog_tickle(struct sysmon_wdog *smw)
    365       1.1   thorpej {
    366       1.1   thorpej 	struct elansc_softc *sc = smw->smw_cookie;
    367       1.1   thorpej 
    368      1.19    dyoung 	mutex_enter(&sc->sc_mtx);
    369       1.1   thorpej 	elansc_wdogctl_reset(sc);
    370      1.19    dyoung 	mutex_exit(&sc->sc_mtx);
    371      1.19    dyoung 	return 0;
    372       1.1   thorpej }
    373       1.1   thorpej 
    374       1.1   thorpej static const char *elansc_speeds[] = {
    375       1.1   thorpej 	"(reserved 00)",
    376       1.1   thorpej 	"100MHz",
    377       1.1   thorpej 	"133MHz",
    378       1.1   thorpej 	"(reserved 11)",
    379       1.1   thorpej };
    380       1.1   thorpej 
    381      1.22    dyoung static int
    382      1.22    dyoung elanpar_intr(void *arg)
    383      1.22    dyoung {
    384      1.22    dyoung 	struct elansc_softc *sc = arg;
    385      1.22    dyoung 	uint16_t wpvsta;
    386      1.22    dyoung 	unsigned win;
    387      1.22    dyoung 	uint32_t par;
    388      1.22    dyoung 	const char *wpvstr;
    389      1.22    dyoung 
    390      1.22    dyoung 	wpvsta = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_WPVSTA);
    391      1.22    dyoung 
    392      1.22    dyoung 	if ((wpvsta & MMCR_WPVSTA_WPV_STA) == 0)
    393      1.22    dyoung 		return 0;
    394      1.22    dyoung 
    395      1.22    dyoung 	win = __SHIFTOUT(wpvsta, MMCR_WPVSTA_WPV_WINDOW);
    396      1.22    dyoung 
    397      1.22    dyoung 	par = bus_space_read_4(sc->sc_memt, sc->sc_memh, MMCR_PAR(win));
    398      1.22    dyoung 
    399      1.22    dyoung 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WPVSTA,
    400      1.22    dyoung 	    MMCR_WPVSTA_WPV_STA);
    401      1.22    dyoung 
    402      1.22    dyoung 	switch (wpvsta & MMCR_WPVSTA_WPV_MSTR) {
    403      1.22    dyoung 	case MMCR_WPVSTA_WPV_MSTR_CPU:
    404      1.22    dyoung 		wpvstr = "cpu";
    405      1.22    dyoung 		break;
    406      1.22    dyoung 	case MMCR_WPVSTA_WPV_MSTR_PCI:
    407      1.22    dyoung 		wpvstr = "pci";
    408      1.22    dyoung 		break;
    409      1.22    dyoung 	case MMCR_WPVSTA_WPV_MSTR_GP:
    410      1.22    dyoung 		wpvstr = "gp";
    411      1.22    dyoung 		break;
    412      1.22    dyoung 	default:
    413      1.22    dyoung 		wpvstr = "unknown";
    414      1.22    dyoung 		break;
    415      1.22    dyoung 	}
    416      1.22    dyoung 	aprint_error_dev(sc->sc_par,
    417      1.22    dyoung 	    "%s violated write-protect window %u\n", wpvstr, win);
    418      1.22    dyoung 	elansc_print_par(sc->sc_par, win, par);
    419      1.22    dyoung 	return 0;
    420      1.22    dyoung }
    421      1.22    dyoung 
    422      1.22    dyoung static int
    423      1.22    dyoung elanpex_intr(void *arg)
    424      1.22    dyoung {
    425      1.22    dyoung 	static struct {
    426      1.22    dyoung 		const char *string;
    427      1.22    dyoung 		bool nonfatal;
    428      1.22    dyoung 	} cmd[16] = {
    429      1.22    dyoung 		  [0] =	{.string = "not latched"}
    430      1.22    dyoung 		, [1] =	{.string = "special cycle"}
    431      1.22    dyoung 		, [2] =	{.string = "i/o read"}
    432      1.22    dyoung 		, [3] =	{.string = "i/o write"}
    433      1.22    dyoung 		, [4] =	{.string = "4"}
    434      1.22    dyoung 		, [5] =	{.string = "5"}
    435      1.22    dyoung 		, [6] =	{.string = "memory rd"}
    436      1.22    dyoung 		, [7] =	{.string = "memory wr"}
    437      1.22    dyoung 		, [8] =	{.string = "8"}
    438      1.22    dyoung 		, [9] =	{.string = "9"}
    439      1.22    dyoung 		, [10] = {.string = "cfg rd", .nonfatal = true}
    440      1.22    dyoung 		, [11] = {.string = "cfg wr"}
    441      1.22    dyoung 		, [12] = {.string = "memory rd mul"}
    442      1.22    dyoung 		, [13] = {.string = "dual-address cycle"}
    443      1.22    dyoung 		, [14] = {.string = "memory rd line"}
    444      1.22    dyoung 		, [15] = {.string = "memory wr & inv"}
    445      1.22    dyoung 	};
    446      1.22    dyoung 
    447      1.22    dyoung 	static const struct {
    448      1.22    dyoung 		uint16_t bit;
    449      1.22    dyoung 		const char *msg;
    450      1.22    dyoung 	} mmsg[] = {
    451      1.22    dyoung 		  {MMCR_HBMSTIRQSTA_M_RTRTO_IRQ_STA, "retry timeout"}
    452      1.22    dyoung 		, {MMCR_HBMSTIRQSTA_M_TABRT_IRQ_STA, "target abort"}
    453      1.22    dyoung 		, {MMCR_HBMSTIRQSTA_M_MABRT_IRQ_STA, "abort"}
    454      1.22    dyoung 		, {MMCR_HBMSTIRQSTA_M_SERR_IRQ_STA, "system error"}
    455      1.22    dyoung 		, {MMCR_HBMSTIRQSTA_M_RPER_IRQ_STA, "received parity error"}
    456      1.22    dyoung 		, {MMCR_HBMSTIRQSTA_M_DPER_IRQ_STA, "detected parity error"}
    457      1.22    dyoung 	}, tmsg[] = {
    458      1.22    dyoung 		  {MMCR_HBTGTIRQSTA_T_DLYTO_IRQ_STA, "delayed txn timeout"}
    459      1.22    dyoung 		, {MMCR_HBTGTIRQSTA_T_APER_IRQ_STA, "address parity"}
    460      1.22    dyoung 		, {MMCR_HBTGTIRQSTA_T_DPER_IRQ_STA, "data parity"}
    461      1.22    dyoung 	};
    462      1.22    dyoung 	uint8_t pciarbsta;
    463      1.22    dyoung 	uint16_t mstcmd, mstirq, tgtid, tgtirq;
    464      1.22    dyoung 	uint32_t mstaddr;
    465      1.22    dyoung 	uint16_t mstack = 0, tgtack = 0;
    466      1.22    dyoung 	int fatal = 0, i, handled = 0;
    467      1.22    dyoung 	struct elansc_softc *sc = arg;
    468      1.22    dyoung 
    469      1.22    dyoung 	pciarbsta = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_PCIARBSTA);
    470      1.22    dyoung 	mstirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQSTA);
    471      1.22    dyoung 	mstaddr = bus_space_read_4(sc->sc_memt, sc->sc_memh, MMCR_MSTINTADD);
    472      1.22    dyoung 	tgtirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQSTA);
    473      1.22    dyoung 
    474      1.22    dyoung 	if ((pciarbsta & MMCR_PCIARBSTA_GNT_TO_STA) != 0) {
    475      1.22    dyoung 		aprint_error_dev(sc->sc_pex,
    476      1.22    dyoung 		    "grant time-out, GNT%" __PRIuBITS "# asserted\n",
    477      1.22    dyoung 		    __SHIFTOUT(pciarbsta, MMCR_PCIARBSTA_GNT_TO_ID));
    478      1.22    dyoung 		bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_PCIARBSTA,
    479      1.22    dyoung 		    MMCR_PCIARBSTA_GNT_TO_STA);
    480      1.22    dyoung 		handled = true;
    481      1.22    dyoung 	}
    482      1.22    dyoung 
    483      1.22    dyoung 	mstcmd = __SHIFTOUT(mstirq, MMCR_HBMSTIRQSTA_M_CMD_IRQ_ID);
    484      1.22    dyoung 
    485      1.22    dyoung 	for (i = 0; i < __arraycount(mmsg); i++) {
    486      1.22    dyoung 		if ((mstirq & mmsg[i].bit) == 0)
    487      1.22    dyoung 			continue;
    488      1.22    dyoung 		aprint_error_dev(sc->sc_pex,
    489      1.22    dyoung 		    "%s %08" PRIx32 " master %s\n",
    490      1.22    dyoung 		    cmd[mstcmd].string, mstaddr, mmsg[i].msg);
    491      1.22    dyoung 
    492      1.22    dyoung 		mstack |= mmsg[i].bit;
    493      1.22    dyoung 		if (!cmd[mstcmd].nonfatal)
    494      1.22    dyoung 			fatal = true;
    495      1.22    dyoung 	}
    496      1.22    dyoung 
    497      1.22    dyoung 	tgtid = __SHIFTOUT(tgtirq, MMCR_HBTGTIRQSTA_T_IRQ_ID);
    498      1.22    dyoung 
    499      1.22    dyoung 	for (i = 0; i < __arraycount(tmsg); i++) {
    500      1.22    dyoung 		if ((tgtirq & tmsg[i].bit) == 0)
    501      1.22    dyoung 			continue;
    502      1.22    dyoung 		aprint_error_dev(sc->sc_pex, "%1x target %s\n", tgtid,
    503      1.22    dyoung 		    tmsg[i].msg);
    504      1.22    dyoung 		tgtack |= tmsg[i].bit;
    505      1.22    dyoung 	}
    506      1.22    dyoung 
    507      1.22    dyoung 	/* acknowledge interrupts */
    508      1.22    dyoung 	if (tgtack != 0) {
    509      1.22    dyoung 		handled = true;
    510      1.22    dyoung 		bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQSTA,
    511      1.22    dyoung 		    tgtack);
    512      1.22    dyoung 	}
    513      1.22    dyoung 	if (mstack != 0) {
    514      1.22    dyoung 		handled = true;
    515      1.22    dyoung 		bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQSTA,
    516      1.22    dyoung 		    mstack);
    517      1.22    dyoung 	}
    518      1.22    dyoung 	return fatal ? 0 : (handled ? 1 : 0);
    519      1.22    dyoung }
    520      1.22    dyoung 
    521      1.22    dyoung #define	elansc_print_1(__dev, __sc, __reg)				\
    522      1.22    dyoung do {									\
    523      1.22    dyoung 	aprint_debug_dev(__dev,						\
    524      1.22    dyoung 	    "%s: %s %02" PRIx8 "\n", __func__, #__reg,			\
    525      1.22    dyoung 	    bus_space_read_1((__sc)->sc_memt, (__sc)->sc_memh, __reg));	\
    526      1.22    dyoung } while (/*CONSTCOND*/0)
    527      1.22    dyoung 
    528      1.22    dyoung static void
    529      1.22    dyoung elansc_print_par(device_t dev, int i, uint32_t par)
    530      1.22    dyoung {
    531      1.22    dyoung 	uint32_t addr, sz, unit;
    532      1.22    dyoung 	const char *tgtstr;
    533      1.22    dyoung 
    534      1.22    dyoung 	switch (par & MMCR_PAR_TARGET) {
    535      1.22    dyoung 	default:
    536      1.22    dyoung 	case MMCR_PAR_TARGET_OFF:
    537      1.22    dyoung 		tgtstr = "off";
    538      1.22    dyoung 		break;
    539      1.22    dyoung 	case MMCR_PAR_TARGET_GPIO:
    540      1.22    dyoung 		tgtstr = "gpio";
    541      1.22    dyoung 		break;
    542      1.22    dyoung 	case MMCR_PAR_TARGET_GPMEM:
    543      1.22    dyoung 		tgtstr = "gpmem";
    544      1.22    dyoung 		break;
    545      1.22    dyoung 	case MMCR_PAR_TARGET_PCI:
    546      1.22    dyoung 		tgtstr = "pci";
    547      1.22    dyoung 		break;
    548      1.22    dyoung 	case MMCR_PAR_TARGET_BOOTCS:
    549      1.22    dyoung 		tgtstr = "bootcs";
    550      1.22    dyoung 		break;
    551      1.22    dyoung 	case MMCR_PAR_TARGET_ROMCS1:
    552      1.22    dyoung 		tgtstr = "romcs1";
    553      1.22    dyoung 		break;
    554      1.22    dyoung 	case MMCR_PAR_TARGET_ROMCS2:
    555      1.22    dyoung 		tgtstr = "romcs2";
    556      1.22    dyoung 		break;
    557      1.22    dyoung 	case MMCR_PAR_TARGET_SDRAM:
    558      1.22    dyoung 		tgtstr = "sdram";
    559      1.22    dyoung 		break;
    560      1.22    dyoung 	}
    561      1.22    dyoung 	if ((par & MMCR_PAR_TARGET) == MMCR_PAR_TARGET_GPIO) {
    562      1.22    dyoung 		unit = 1;
    563      1.22    dyoung 		sz = __SHIFTOUT(par, MMCR_PAR_IO_SZ);
    564      1.22    dyoung 		addr = __SHIFTOUT(par, MMCR_PAR_IO_ST_ADR);
    565      1.22    dyoung 	} else if ((par & MMCR_PAR_PG_SZ) != 0) {
    566      1.22    dyoung 		unit = 64 * 1024;
    567      1.22    dyoung 		sz = __SHIFTOUT(par, MMCR_PAR_64KB_SZ);
    568      1.22    dyoung 		addr = __SHIFTOUT(par, MMCR_PAR_64KB_ST_ADR);
    569      1.22    dyoung 	} else {
    570      1.22    dyoung 		unit = 4 * 1024;
    571      1.22    dyoung 		sz = __SHIFTOUT(par, MMCR_PAR_4KB_SZ);
    572      1.22    dyoung 		addr = __SHIFTOUT(par, MMCR_PAR_4KB_ST_ADR);
    573      1.22    dyoung 	}
    574      1.22    dyoung 
    575      1.22    dyoung 	aprint_debug_dev(dev,
    576      1.22    dyoung 	    "PAR[%d] %08" PRIx32 " tgt %s attr %1" __PRIxBITS
    577      1.22    dyoung 	    " start %08" PRIx32 " size %" PRIu32 "\n",
    578      1.22    dyoung 	    i, par, tgtstr, __SHIFTOUT(par, MMCR_PAR_ATTR),
    579      1.22    dyoung 	    addr * unit, (sz + 1) * unit);
    580      1.22    dyoung }
    581      1.22    dyoung 
    582      1.22    dyoung static void
    583      1.22    dyoung elansc_print_all_par(device_t dev,
    584      1.22    dyoung     bus_space_tag_t memt, bus_space_handle_t memh)
    585      1.22    dyoung {
    586      1.22    dyoung 	int i;
    587      1.22    dyoung 	uint32_t par;
    588      1.22    dyoung 
    589      1.22    dyoung 	for (i = 0; i < 16; i++) {
    590      1.22    dyoung 		par = bus_space_read_4(memt, memh, MMCR_PAR(i));
    591      1.22    dyoung 		elansc_print_par(dev, i, par);
    592      1.22    dyoung 	}
    593      1.22    dyoung }
    594      1.22    dyoung 
    595      1.22    dyoung static int
    596      1.22    dyoung elansc_alloc_par(bus_space_tag_t memt, bus_space_handle_t memh)
    597      1.22    dyoung {
    598      1.22    dyoung 	int i;
    599      1.22    dyoung 	uint32_t par;
    600      1.22    dyoung 
    601      1.22    dyoung 	for (i = 0; i < 16; i++) {
    602      1.22    dyoung 
    603      1.22    dyoung 		par = bus_space_read_4(memt, memh, MMCR_PAR(i));
    604      1.22    dyoung 
    605      1.22    dyoung 		if ((par & MMCR_PAR_TARGET) == MMCR_PAR_TARGET_OFF)
    606      1.22    dyoung 			break;
    607      1.22    dyoung 	}
    608      1.22    dyoung 	if (i == 16)
    609      1.22    dyoung 		return -1;
    610      1.22    dyoung 	return i;
    611      1.22    dyoung }
    612      1.22    dyoung 
    613      1.22    dyoung static void
    614      1.22    dyoung elansc_disable_par(bus_space_tag_t memt, bus_space_handle_t memh, int idx)
    615      1.22    dyoung {
    616      1.22    dyoung 	uint32_t par;
    617      1.22    dyoung 	par = bus_space_read_4(memt, memh, MMCR_PAR(idx));
    618      1.22    dyoung 	par &= ~MMCR_PAR_TARGET;
    619      1.22    dyoung 	par |= MMCR_PAR_TARGET_OFF;
    620      1.22    dyoung 	bus_space_write_4(memt, memh, MMCR_PAR(idx), par);
    621      1.22    dyoung }
    622      1.22    dyoung 
    623      1.27    dyoung struct pareg {
    624      1.27    dyoung 	paddr_t start;
    625      1.27    dyoung 	paddr_t end;
    626      1.27    dyoung };
    627      1.27    dyoung 
    628      1.22    dyoung static int
    629      1.27    dyoung region_paddr_to_par(struct pareg *region0, struct pareg *regions, uint32_t unit)
    630      1.27    dyoung {
    631      1.27    dyoung 	struct pareg *residue = regions;
    632      1.27    dyoung 	paddr_t start, end;
    633      1.27    dyoung 	paddr_t start0, end0;
    634      1.27    dyoung 
    635      1.27    dyoung 	start0 = region0->start;
    636      1.27    dyoung 	end0 = region0->end;
    637      1.27    dyoung 
    638      1.27    dyoung 	if (start0 % unit != 0)
    639      1.27    dyoung 		start = start0 + unit - start0 % unit;
    640      1.27    dyoung 	else
    641      1.27    dyoung 		start = start0;
    642      1.27    dyoung 
    643      1.27    dyoung 	end = end0 - end0 % unit;
    644      1.27    dyoung 
    645      1.27    dyoung 	if (start >= end)
    646      1.27    dyoung 		return 0;
    647      1.27    dyoung 
    648      1.27    dyoung 	residue->start = start;
    649      1.27    dyoung 	residue->end = end;
    650      1.27    dyoung 	residue++;
    651      1.27    dyoung 
    652      1.27    dyoung 	if (start0 < start) {
    653      1.27    dyoung 		residue->start = start0;
    654      1.27    dyoung 		residue->end = start;
    655      1.27    dyoung 		residue++;
    656      1.27    dyoung 	}
    657      1.27    dyoung 	if (end < end0) {
    658      1.27    dyoung 		residue->start = end;
    659      1.27    dyoung 		residue->end = end0;
    660      1.27    dyoung 		residue++;
    661      1.27    dyoung 	}
    662      1.27    dyoung 	return residue - regions;
    663      1.27    dyoung }
    664      1.27    dyoung 
    665      1.27    dyoung static void
    666      1.22    dyoung elansc_protect_text(device_t self, struct elansc_softc *sc)
    667      1.22    dyoung {
    668      1.27    dyoung 	int i, j, nregion, pidx, tidx = 0, xnregion;
    669      1.22    dyoung 	uint32_t par;
    670      1.22    dyoung 	uint32_t protsize, unprotsize;
    671      1.22    dyoung 	paddr_t start_pa, end_pa;
    672      1.22    dyoung 	extern char kernel_text, etext;
    673      1.22    dyoung 	bus_space_tag_t memt;
    674      1.22    dyoung 	bus_space_handle_t memh;
    675      1.27    dyoung 	struct pareg region0, regions[3], xregions[3];
    676      1.27    dyoung 
    677      1.27    dyoung 	sc->sc_textpar[0] = sc->sc_textpar[1] = sc->sc_textpar[2] = -1;
    678      1.22    dyoung 
    679      1.22    dyoung 	memt = sc->sc_memt;
    680      1.22    dyoung 	memh = sc->sc_memh;
    681      1.22    dyoung 
    682      1.27    dyoung 	if (!pmap_extract(pmap_kernel(), (vaddr_t)&kernel_text,
    683      1.27    dyoung 	                  &region0.start) ||
    684      1.27    dyoung 	    !pmap_extract(pmap_kernel(), (vaddr_t)&etext,
    685      1.27    dyoung 	                  &region0.end))
    686      1.27    dyoung 		return;
    687      1.22    dyoung 
    688      1.27    dyoung 	if (&etext - &kernel_text != region0.end - region0.start) {
    689      1.22    dyoung 		aprint_error_dev(self, "kernel text may not be contiguous\n");
    690      1.27    dyoung 		return;
    691      1.22    dyoung 	}
    692      1.22    dyoung 
    693      1.27    dyoung 	if ((pidx = elansc_alloc_par(memt, memh)) == -1) {
    694      1.22    dyoung 		aprint_error_dev(self, "cannot allocate PAR\n");
    695      1.27    dyoung 		return;
    696      1.22    dyoung 	}
    697      1.22    dyoung 
    698      1.27    dyoung 	par = bus_space_read_4(memt, memh, MMCR_PAR(pidx));
    699      1.22    dyoung 
    700      1.22    dyoung 	aprint_debug_dev(self,
    701      1.22    dyoung 	    "protect kernel text at physical addresses %p - %p\n",
    702      1.27    dyoung 	    (void *)region0.start, (void *)region0.end);
    703      1.27    dyoung 
    704      1.27    dyoung 	nregion = region_paddr_to_par(&region0, regions, sfkb);
    705      1.27    dyoung 	if (nregion == 0) {
    706      1.27    dyoung 		aprint_error_dev(self, "kernel text is unprotected\n");
    707      1.27    dyoung 		return;
    708      1.27    dyoung 	}
    709      1.27    dyoung 
    710      1.27    dyoung 	unprotsize = 0;
    711      1.27    dyoung 	for (i = 1; i < nregion; i++)
    712      1.27    dyoung 		unprotsize += regions[i].end - regions[i].start;
    713      1.22    dyoung 
    714      1.27    dyoung 	start_pa = regions[0].start;
    715      1.27    dyoung 	end_pa = regions[0].end;
    716      1.22    dyoung 
    717      1.22    dyoung 	aprint_debug_dev(self,
    718      1.22    dyoung 	    "actually protect kernel text at physical addresses %p - %p\n",
    719      1.22    dyoung 	    (void *)start_pa, (void *)end_pa);
    720      1.22    dyoung 
    721      1.22    dyoung 	aprint_verbose_dev(self,
    722      1.22    dyoung 	    "%" PRIu32 " bytes of kernel text are unprotected\n", unprotsize);
    723      1.22    dyoung 
    724      1.22    dyoung 	protsize = end_pa - start_pa;
    725      1.22    dyoung 
    726      1.27    dyoung #if 0
    727      1.27    dyoung 	/* set PG_SZ, attribute, target, size, address. */
    728      1.22    dyoung 	par = MMCR_PAR_TARGET_SDRAM | MMCR_PAR_ATTR_NOWRITE | MMCR_PAR_PG_SZ;
    729      1.22    dyoung 	par |= __SHIFTIN(protsize / sfkb - 1, MMCR_PAR_64KB_SZ);
    730      1.22    dyoung 	par |= __SHIFTIN(start_pa / sfkb, MMCR_PAR_64KB_ST_ADR);
    731      1.27    dyoung 	bus_space_write_4(memt, memh, MMCR_PAR(pidx), par);
    732      1.27    dyoung #else
    733      1.27    dyoung 	elansc_protect(sc, pidx, start_pa, protsize);
    734      1.27    dyoung #endif
    735      1.27    dyoung 
    736      1.27    dyoung 	sc->sc_textpar[tidx++] = pidx;
    737      1.27    dyoung 
    738      1.27    dyoung 	unprotsize = 0;
    739      1.27    dyoung 	for (i = 1; i < nregion; i++) {
    740      1.27    dyoung 		xnregion = region_paddr_to_par(&regions[i], xregions, fkb);
    741      1.27    dyoung 		if (xnregion == 0) {
    742      1.27    dyoung 			aprint_verbose_dev(self, "skip region %p - %p\n",
    743      1.27    dyoung 			    (void *)regions[i].start, (void *)regions[i].end);
    744      1.27    dyoung 			continue;
    745      1.27    dyoung 		}
    746      1.27    dyoung 		if ((pidx = elansc_alloc_par(memt, memh)) == -1) {
    747      1.27    dyoung 			unprotsize += regions[i].end - regions[i].start;
    748      1.27    dyoung 			continue;
    749      1.27    dyoung 		}
    750      1.27    dyoung 		elansc_protect(sc, pidx, xregions[0].start,
    751      1.27    dyoung 		    xregions[0].end - xregions[0].start);
    752      1.27    dyoung 		sc->sc_textpar[tidx++] = pidx;
    753      1.27    dyoung 
    754      1.27    dyoung 		aprint_debug_dev(self,
    755      1.27    dyoung 		    "protect add'l kernel text at physical addresses %p - %p\n",
    756      1.27    dyoung 		    (void *)xregions[0].start, (void *)xregions[0].end);
    757      1.27    dyoung 
    758      1.27    dyoung 		for (j = 1; j < xnregion; j++)
    759      1.27    dyoung 			unprotsize += xregions[j].end - xregions[j].start;
    760      1.27    dyoung 	}
    761      1.27    dyoung 	aprint_verbose_dev(self,
    762      1.27    dyoung 	    "%" PRIu32 " bytes of kernel text still unprotected\n", unprotsize);
    763      1.27    dyoung 
    764      1.27    dyoung }
    765      1.27    dyoung 
    766      1.27    dyoung static void
    767      1.27    dyoung elansc_protect(struct elansc_softc *sc, int pidx, paddr_t addr, uint32_t sz)
    768      1.27    dyoung {
    769      1.27    dyoung 	uint32_t addr_field, blksz, par, size_field;
    770      1.27    dyoung 
    771      1.27    dyoung 	/* set attribute, target. */
    772      1.27    dyoung 	par = MMCR_PAR_TARGET_SDRAM | MMCR_PAR_ATTR_NOWRITE;
    773      1.27    dyoung 
    774      1.27    dyoung 	KASSERT(addr % fkb == 0 && sz % fkb == 0);
    775      1.27    dyoung 
    776      1.27    dyoung 	if (addr % sfkb == 0 && sz % sfkb == 0) {
    777      1.27    dyoung 		par |= MMCR_PAR_PG_SZ;
    778      1.27    dyoung 
    779      1.27    dyoung 		size_field = MMCR_PAR_64KB_SZ;
    780      1.27    dyoung 		addr_field = MMCR_PAR_64KB_ST_ADR;
    781      1.27    dyoung 		blksz = 64 * 1024;
    782      1.27    dyoung 	} else {
    783      1.27    dyoung 		size_field = MMCR_PAR_4KB_SZ;
    784      1.27    dyoung 		addr_field = MMCR_PAR_4KB_ST_ADR;
    785      1.27    dyoung 		blksz = 4 * 1024;
    786      1.27    dyoung 	}
    787      1.27    dyoung 
    788      1.27    dyoung 	KASSERT(sz / blksz - 1 <= __SHIFTOUT_MASK(size_field));
    789      1.27    dyoung 	KASSERT(addr / blksz <= __SHIFTOUT_MASK(addr_field));
    790      1.27    dyoung 
    791      1.27    dyoung 	/* set size and address. */
    792      1.27    dyoung 	par |= __SHIFTIN(sz / blksz - 1, size_field);
    793      1.27    dyoung 	par |= __SHIFTIN(addr / blksz, addr_field);
    794      1.27    dyoung 
    795      1.27    dyoung 	bus_space_write_4(sc->sc_memt, sc->sc_memh, MMCR_PAR(pidx), par);
    796      1.22    dyoung }
    797      1.22    dyoung 
    798      1.22    dyoung static int
    799      1.23    dyoung elansc_protect_pg0(device_t self, struct elansc_softc *sc)
    800      1.22    dyoung {
    801      1.27    dyoung 	int pidx;
    802      1.23    dyoung 	const paddr_t pg0_paddr = 0;
    803      1.22    dyoung 	bus_space_tag_t memt;
    804      1.22    dyoung 	bus_space_handle_t memh;
    805      1.22    dyoung 
    806      1.22    dyoung 	memt = sc->sc_memt;
    807      1.22    dyoung 	memh = sc->sc_memh;
    808      1.22    dyoung 
    809      1.23    dyoung 	if (elansc_do_protect_pg0 == 0)
    810      1.22    dyoung 		return -1;
    811      1.22    dyoung 
    812      1.27    dyoung 	if ((pidx = elansc_alloc_par(memt, memh)) == -1)
    813      1.22    dyoung 		return -1;
    814      1.22    dyoung 
    815      1.23    dyoung 	aprint_debug_dev(self, "protect page 0\n");
    816      1.22    dyoung 
    817      1.27    dyoung #if 0
    818      1.27    dyoung 	/* set PG_SZ, attribute, target, size, address. */
    819      1.22    dyoung 	par = MMCR_PAR_TARGET_SDRAM | MMCR_PAR_ATTR_NOWRITE;
    820      1.23    dyoung 	par |= __SHIFTIN(PG0_PROT_SIZE / PAGE_SIZE - 1, MMCR_PAR_4KB_SZ);
    821      1.23    dyoung 	par |= __SHIFTIN(pg0_paddr / PAGE_SIZE, MMCR_PAR_4KB_ST_ADR);
    822      1.27    dyoung 	bus_space_write_4(memt, memh, MMCR_PAR(pidx), par);
    823      1.27    dyoung #else
    824      1.27    dyoung 	elansc_protect(sc, pidx, pg0_paddr, PG0_PROT_SIZE);
    825      1.27    dyoung #endif
    826      1.27    dyoung 	return pidx;
    827      1.22    dyoung }
    828      1.22    dyoung 
    829      1.22    dyoung static void
    830      1.22    dyoung elanpex_intr_ack(bus_space_tag_t memt, bus_space_handle_t memh)
    831      1.22    dyoung {
    832      1.22    dyoung 	bus_space_write_1(memt, memh, MMCR_PCIARBSTA,
    833      1.22    dyoung 	    MMCR_PCIARBSTA_GNT_TO_STA);
    834      1.22    dyoung 	bus_space_write_2(memt, memh, MMCR_HBTGTIRQSTA, MMCR_TGTIRQ_ACT);
    835      1.22    dyoung 	bus_space_write_2(memt, memh, MMCR_HBMSTIRQSTA, MMCR_MSTIRQ_ACT);
    836      1.22    dyoung }
    837      1.22    dyoung 
    838      1.17    dyoung static bool
    839      1.24    dyoung elansc_suspend(device_t dev PMF_FN_ARGS)
    840      1.17    dyoung {
    841      1.19    dyoung 	bool rc;
    842      1.17    dyoung 	struct elansc_softc *sc = device_private(dev);
    843      1.17    dyoung 
    844      1.19    dyoung 	mutex_enter(&sc->sc_mtx);
    845      1.19    dyoung 	rc = ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED);
    846      1.19    dyoung 	mutex_exit(&sc->sc_mtx);
    847      1.19    dyoung 	if (!rc)
    848      1.17    dyoung 		aprint_debug_dev(dev, "watchdog enabled, suspend forbidden");
    849      1.19    dyoung 	return rc;
    850      1.17    dyoung }
    851      1.17    dyoung 
    852      1.17    dyoung static bool
    853      1.24    dyoung elansc_resume(device_t dev PMF_FN_ARGS)
    854      1.17    dyoung {
    855      1.17    dyoung 	struct elansc_softc *sc = device_private(dev);
    856      1.17    dyoung 
    857      1.19    dyoung 	mutex_enter(&sc->sc_mtx);
    858      1.17    dyoung 	/* Set up the watchdog registers with some defaults. */
    859      1.17    dyoung 	elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
    860      1.17    dyoung 
    861      1.17    dyoung 	/* ...and clear it. */
    862      1.17    dyoung 	elansc_wdogctl_reset(sc);
    863      1.19    dyoung 	mutex_exit(&sc->sc_mtx);
    864      1.17    dyoung 
    865      1.31    dyoung 	elansc_perf_tune(dev, sc->sc_memt, sc->sc_memh);
    866      1.31    dyoung 
    867      1.17    dyoung 	return true;
    868      1.17    dyoung }
    869      1.17    dyoung 
    870      1.18    dyoung static int
    871      1.18    dyoung elansc_detach(device_t self, int flags)
    872      1.18    dyoung {
    873      1.19    dyoung 	int rc;
    874      1.18    dyoung 	struct elansc_softc *sc = device_private(self);
    875      1.18    dyoung 
    876      1.19    dyoung 	if ((rc = config_detach_children(self, flags)) != 0)
    877      1.19    dyoung 		return rc;
    878      1.19    dyoung 
    879      1.18    dyoung 	pmf_device_deregister(self);
    880      1.18    dyoung 
    881      1.19    dyoung 	if ((rc = sysmon_wdog_unregister(&sc->sc_smw)) != 0) {
    882      1.19    dyoung 		if (rc == ERESTART)
    883      1.19    dyoung 			rc = EINTR;
    884      1.19    dyoung 		return rc;
    885      1.19    dyoung 	}
    886      1.19    dyoung 
    887      1.19    dyoung 	mutex_enter(&sc->sc_mtx);
    888      1.18    dyoung 
    889      1.18    dyoung 	/* Set up the watchdog registers with some defaults. */
    890      1.18    dyoung 	elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
    891      1.18    dyoung 
    892      1.18    dyoung 	/* ...and clear it. */
    893      1.18    dyoung 	elansc_wdogctl_reset(sc);
    894      1.18    dyoung 
    895      1.19    dyoung 	mutex_exit(&sc->sc_mtx);
    896      1.19    dyoung 	mutex_destroy(&sc->sc_mtx);
    897  1.31.4.1      yamt 
    898  1.31.4.1      yamt 	bus_space_unmap(sc->sc_memt, sc->sc_memh, PAGE_SIZE);
    899      1.31    dyoung 	elansc_attached = false;
    900      1.18    dyoung 	return 0;
    901      1.18    dyoung }
    902      1.18    dyoung 
    903      1.22    dyoung static void *
    904      1.22    dyoung elansc_intr_establish(device_t dev, int (*handler)(void *), void *arg)
    905      1.22    dyoung {
    906      1.22    dyoung 	struct pic *pic;
    907      1.22    dyoung 	void *ih;
    908      1.22    dyoung 
    909      1.22    dyoung 	if ((pic = intr_findpic(ELAN_IRQ)) == NULL) {
    910      1.22    dyoung 		aprint_error_dev(dev, "PIC for irq %d not found\n",
    911      1.22    dyoung 		    ELAN_IRQ);
    912      1.22    dyoung 		return NULL;
    913      1.22    dyoung 	} else if ((ih = intr_establish(ELAN_IRQ, pic, ELAN_IRQ,
    914      1.22    dyoung 	    IST_LEVEL, IPL_HIGH, handler, arg)) == NULL) {
    915      1.22    dyoung 		aprint_error_dev(dev,
    916      1.22    dyoung 		    "could not establish interrupt\n");
    917      1.22    dyoung 		return NULL;
    918      1.22    dyoung 	}
    919      1.22    dyoung 	aprint_verbose_dev(dev, "interrupting at irq %d\n", ELAN_IRQ);
    920      1.22    dyoung 	return ih;
    921      1.22    dyoung }
    922      1.22    dyoung 
    923      1.22    dyoung static bool
    924      1.24    dyoung elanpex_resume(device_t self PMF_FN_ARGS)
    925      1.22    dyoung {
    926      1.22    dyoung 	struct elansc_softc *sc = device_private(device_parent(self));
    927      1.22    dyoung 
    928      1.22    dyoung 	elanpex_intr_establish(self, sc);
    929      1.22    dyoung 	return sc->sc_eih != NULL;
    930      1.22    dyoung }
    931      1.22    dyoung 
    932      1.22    dyoung static bool
    933      1.24    dyoung elanpex_suspend(device_t self PMF_FN_ARGS)
    934      1.22    dyoung {
    935      1.22    dyoung 	struct elansc_softc *sc = device_private(device_parent(self));
    936      1.22    dyoung 
    937      1.22    dyoung 	elanpex_intr_disestablish(sc);
    938      1.22    dyoung 
    939      1.22    dyoung 	return true;
    940      1.22    dyoung }
    941      1.22    dyoung 
    942      1.22    dyoung static bool
    943      1.24    dyoung elanpar_resume(device_t self PMF_FN_ARGS)
    944      1.22    dyoung {
    945      1.22    dyoung 	struct elansc_softc *sc = device_private(device_parent(self));
    946      1.22    dyoung 
    947      1.22    dyoung 	elanpar_intr_establish(self, sc);
    948      1.22    dyoung 	return sc->sc_pih != NULL;
    949      1.22    dyoung }
    950      1.22    dyoung 
    951      1.22    dyoung static bool
    952      1.24    dyoung elanpar_suspend(device_t self PMF_FN_ARGS)
    953      1.22    dyoung {
    954      1.22    dyoung 	struct elansc_softc *sc = device_private(device_parent(self));
    955      1.22    dyoung 
    956      1.25    dyoung 	elanpar_intr_disestablish(sc);
    957      1.22    dyoung 
    958      1.22    dyoung 	return true;
    959      1.22    dyoung }
    960      1.22    dyoung 
    961      1.22    dyoung static void
    962      1.22    dyoung elanpex_intr_establish(device_t self, struct elansc_softc *sc)
    963      1.22    dyoung {
    964      1.22    dyoung 	uint8_t sysarbctl;
    965      1.22    dyoung 	uint16_t pcihostmap, mstirq, tgtirq;
    966      1.22    dyoung 
    967      1.22    dyoung 	pcihostmap = bus_space_read_2(sc->sc_memt, sc->sc_memh,
    968      1.22    dyoung 	    MMCR_PCIHOSTMAP);
    969      1.22    dyoung 	/* Priority P2 (Master PIC IR1) */
    970      1.22    dyoung 	pcihostmap &= ~MMCR_PCIHOSTMAP_PCI_IRQ_MAP;
    971      1.22    dyoung 	pcihostmap |= __SHIFTIN(__BIT(ELAN_IRQ), MMCR_PCIHOSTMAP_PCI_IRQ_MAP);
    972      1.22    dyoung 	if (elansc_pcinmi)
    973      1.22    dyoung 		pcihostmap |= MMCR_PCIHOSTMAP_PCI_NMI_ENB;
    974      1.22    dyoung 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_PCIHOSTMAP,
    975      1.22    dyoung 	    pcihostmap);
    976      1.22    dyoung 
    977      1.22    dyoung 	elanpex_intr_ack(sc->sc_memt, sc->sc_memh);
    978      1.22    dyoung 
    979      1.22    dyoung 	sysarbctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_SYSARBCTL);
    980      1.22    dyoung 	mstirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL);
    981      1.22    dyoung 	tgtirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL);
    982      1.22    dyoung 
    983      1.22    dyoung 	sysarbctl |= MMCR_SYSARBCTL_GNT_TO_INT_ENB;
    984      1.22    dyoung 
    985      1.22    dyoung 	mstirq |= MMCR_HBMSTIRQCTL_M_RTRTO_IRQ_ENB;
    986      1.22    dyoung 	mstirq |= MMCR_HBMSTIRQCTL_M_TABRT_IRQ_ENB;
    987      1.22    dyoung 	mstirq |= MMCR_HBMSTIRQCTL_M_MABRT_IRQ_ENB;
    988      1.22    dyoung 	mstirq |= MMCR_HBMSTIRQCTL_M_SERR_IRQ_ENB;
    989      1.22    dyoung 	mstirq |= MMCR_HBMSTIRQCTL_M_RPER_IRQ_ENB;
    990      1.22    dyoung 	mstirq |= MMCR_HBMSTIRQCTL_M_DPER_IRQ_ENB;
    991      1.22    dyoung 
    992      1.22    dyoung 	tgtirq |= MMCR_HBTGTIRQCTL_T_DLYTO_IRQ_ENB;
    993      1.22    dyoung 	tgtirq |= MMCR_HBTGTIRQCTL_T_APER_IRQ_ENB;
    994      1.22    dyoung 	tgtirq |= MMCR_HBTGTIRQCTL_T_DPER_IRQ_ENB;
    995      1.22    dyoung 
    996      1.22    dyoung 	if (elansc_pcinmi) {
    997      1.22    dyoung 		sc->sc_eih = nmi_establish(elanpex_intr, sc);
    998      1.22    dyoung 
    999      1.22    dyoung 		mstirq |= MMCR_HBMSTIRQCTL_M_RTRTO_IRQ_SEL;
   1000      1.22    dyoung 		mstirq |= MMCR_HBMSTIRQCTL_M_TABRT_IRQ_SEL;
   1001      1.22    dyoung 		mstirq |= MMCR_HBMSTIRQCTL_M_MABRT_IRQ_SEL;
   1002      1.22    dyoung 		mstirq |= MMCR_HBMSTIRQCTL_M_SERR_IRQ_SEL;
   1003      1.22    dyoung 		mstirq |= MMCR_HBMSTIRQCTL_M_RPER_IRQ_SEL;
   1004      1.22    dyoung 		mstirq |= MMCR_HBMSTIRQCTL_M_DPER_IRQ_SEL;
   1005      1.22    dyoung 
   1006      1.22    dyoung 		tgtirq |= MMCR_HBTGTIRQCTL_T_DLYTO_IRQ_SEL;
   1007      1.22    dyoung 		tgtirq |= MMCR_HBTGTIRQCTL_T_APER_IRQ_SEL;
   1008      1.22    dyoung 		tgtirq |= MMCR_HBTGTIRQCTL_T_DPER_IRQ_SEL;
   1009      1.22    dyoung 	} else
   1010      1.22    dyoung 		sc->sc_eih = elansc_intr_establish(self, elanpex_intr, sc);
   1011      1.22    dyoung 
   1012      1.22    dyoung 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_SYSARBCTL, sysarbctl);
   1013      1.22    dyoung 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL, mstirq);
   1014      1.22    dyoung 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL, tgtirq);
   1015      1.22    dyoung }
   1016      1.22    dyoung 
   1017      1.22    dyoung static void
   1018      1.22    dyoung elanpex_attach(device_t parent, device_t self, void *aux)
   1019      1.22    dyoung {
   1020      1.22    dyoung 	struct elansc_softc *sc = device_private(parent);
   1021      1.22    dyoung 
   1022      1.22    dyoung 	aprint_naive(": PCI Exceptions\n");
   1023      1.22    dyoung 	aprint_normal(": AMD Elan SC520 PCI Exceptions\n");
   1024      1.22    dyoung 
   1025      1.22    dyoung 	elanpex_intr_establish(self, sc);
   1026      1.22    dyoung 
   1027      1.22    dyoung 	aprint_debug_dev(self, "HBMSTIRQCTL %04x\n",
   1028      1.22    dyoung 	    bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL));
   1029      1.22    dyoung 
   1030      1.22    dyoung 	aprint_debug_dev(self, "HBTGTIRQCTL %04x\n",
   1031      1.22    dyoung 	    bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL));
   1032      1.22    dyoung 
   1033      1.22    dyoung 	aprint_debug_dev(self, "PCIHOSTMAP %04x\n",
   1034      1.22    dyoung 	    bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_PCIHOSTMAP));
   1035      1.22    dyoung 
   1036      1.22    dyoung 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
   1037      1.22    dyoung 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG) |
   1038      1.22    dyoung 	    PCI_COMMAND_PARITY_ENABLE|PCI_COMMAND_SERR_ENABLE);
   1039      1.22    dyoung 
   1040      1.26    dyoung 	if (!pmf_device_register1(self, elanpex_suspend, elanpex_resume,
   1041      1.26    dyoung 	                          elanpex_shutdown))
   1042      1.22    dyoung 		aprint_error_dev(self, "could not establish power hooks\n");
   1043      1.22    dyoung }
   1044      1.22    dyoung 
   1045      1.26    dyoung static bool
   1046      1.26    dyoung elanpex_shutdown(device_t self, int flags)
   1047      1.22    dyoung {
   1048      1.26    dyoung 	struct elansc_softc *sc = device_private(device_parent(self));
   1049      1.22    dyoung 	uint8_t sysarbctl;
   1050      1.22    dyoung 	uint16_t pcihostmap, mstirq, tgtirq;
   1051      1.22    dyoung 
   1052      1.22    dyoung 	sysarbctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_SYSARBCTL);
   1053      1.22    dyoung 	sysarbctl &= ~MMCR_SYSARBCTL_GNT_TO_INT_ENB;
   1054      1.22    dyoung 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_SYSARBCTL, sysarbctl);
   1055      1.22    dyoung 
   1056      1.22    dyoung 	mstirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL);
   1057      1.22    dyoung 	mstirq &= ~MMCR_HBMSTIRQCTL_M_RTRTO_IRQ_ENB;
   1058      1.22    dyoung 	mstirq &= ~MMCR_HBMSTIRQCTL_M_TABRT_IRQ_ENB;
   1059      1.22    dyoung 	mstirq &= ~MMCR_HBMSTIRQCTL_M_MABRT_IRQ_ENB;
   1060      1.22    dyoung 	mstirq &= ~MMCR_HBMSTIRQCTL_M_SERR_IRQ_ENB;
   1061      1.22    dyoung 	mstirq &= ~MMCR_HBMSTIRQCTL_M_RPER_IRQ_ENB;
   1062      1.22    dyoung 	mstirq &= ~MMCR_HBMSTIRQCTL_M_DPER_IRQ_ENB;
   1063      1.22    dyoung 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL, mstirq);
   1064      1.22    dyoung 
   1065      1.22    dyoung 	tgtirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL);
   1066      1.22    dyoung 	tgtirq &= ~MMCR_HBTGTIRQCTL_T_DLYTO_IRQ_ENB;
   1067      1.22    dyoung 	tgtirq &= ~MMCR_HBTGTIRQCTL_T_APER_IRQ_ENB;
   1068      1.22    dyoung 	tgtirq &= ~MMCR_HBTGTIRQCTL_T_DPER_IRQ_ENB;
   1069      1.22    dyoung 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL, tgtirq);
   1070      1.22    dyoung 
   1071      1.22    dyoung 	pcihostmap = bus_space_read_2(sc->sc_memt, sc->sc_memh,
   1072      1.22    dyoung 	    MMCR_PCIHOSTMAP);
   1073      1.22    dyoung 	/* Priority P2 (Master PIC IR1) */
   1074      1.22    dyoung 	pcihostmap &= ~MMCR_PCIHOSTMAP_PCI_IRQ_MAP;
   1075      1.22    dyoung 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_PCIHOSTMAP,
   1076      1.22    dyoung 	    pcihostmap);
   1077      1.22    dyoung 
   1078      1.26    dyoung 	return true;
   1079      1.26    dyoung }
   1080      1.26    dyoung 
   1081      1.26    dyoung static void
   1082      1.26    dyoung elanpex_intr_disestablish(struct elansc_softc *sc)
   1083      1.26    dyoung {
   1084      1.26    dyoung 	elanpex_shutdown(sc->sc_pex, 0);
   1085      1.26    dyoung 
   1086      1.22    dyoung 	if (elansc_pcinmi)
   1087      1.22    dyoung 		nmi_disestablish(sc->sc_eih);
   1088      1.22    dyoung 	else
   1089      1.22    dyoung 		intr_disestablish(sc->sc_eih);
   1090      1.22    dyoung 	sc->sc_eih = NULL;
   1091      1.22    dyoung 
   1092      1.22    dyoung }
   1093      1.22    dyoung 
   1094      1.22    dyoung static int
   1095      1.22    dyoung elanpex_detach(device_t self, int flags)
   1096      1.22    dyoung {
   1097      1.22    dyoung 	struct elansc_softc *sc = device_private(device_parent(self));
   1098      1.22    dyoung 
   1099      1.22    dyoung 	pmf_device_deregister(self);
   1100      1.22    dyoung 	elanpex_intr_disestablish(sc);
   1101      1.22    dyoung 
   1102      1.22    dyoung 	return 0;
   1103      1.22    dyoung }
   1104      1.22    dyoung 
   1105      1.22    dyoung static void
   1106      1.22    dyoung elanpar_intr_establish(device_t self, struct elansc_softc *sc)
   1107      1.22    dyoung {
   1108      1.22    dyoung 	uint8_t adddecctl, wpvmap;
   1109      1.22    dyoung 
   1110      1.22    dyoung 	wpvmap = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_WPVMAP);
   1111      1.22    dyoung 	wpvmap &= ~MMCR_WPVMAP_INT_MAP;
   1112      1.22    dyoung 	if (elansc_wpvnmi)
   1113      1.22    dyoung 		wpvmap |= MMCR_WPVMAP_INT_NMI;
   1114      1.22    dyoung 	else
   1115      1.22    dyoung 		wpvmap |= __SHIFTIN(__BIT(ELAN_IRQ), MMCR_WPVMAP_INT_MAP);
   1116      1.22    dyoung 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_WPVMAP, wpvmap);
   1117      1.22    dyoung 
   1118      1.22    dyoung 	/* clear interrupt status */
   1119      1.22    dyoung 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WPVSTA,
   1120      1.22    dyoung 	    MMCR_WPVSTA_WPV_STA);
   1121      1.22    dyoung 
   1122      1.22    dyoung 	/* establish interrupt */
   1123      1.22    dyoung 	if (elansc_wpvnmi)
   1124      1.22    dyoung 		sc->sc_pih = nmi_establish(elanpar_intr, sc);
   1125      1.22    dyoung 	else
   1126      1.22    dyoung 		sc->sc_pih = elansc_intr_establish(self, elanpar_intr, sc);
   1127      1.22    dyoung 
   1128      1.22    dyoung 	adddecctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_ADDDECCTL);
   1129      1.22    dyoung 	adddecctl |= MMCR_ADDDECCTL_WPV_INT_ENB;
   1130      1.22    dyoung 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_ADDDECCTL, adddecctl);
   1131      1.22    dyoung }
   1132      1.22    dyoung 
   1133      1.26    dyoung static bool
   1134      1.26    dyoung elanpar_shutdown(device_t self, int flags)
   1135      1.26    dyoung {
   1136      1.27    dyoung 	int i;
   1137      1.26    dyoung 	struct elansc_softc *sc = device_private(device_parent(self));
   1138      1.26    dyoung 
   1139      1.27    dyoung 	for (i = 0; i < __arraycount(sc->sc_textpar); i++) {
   1140      1.27    dyoung 		if (sc->sc_textpar[i] == -1)
   1141      1.27    dyoung 			continue;
   1142      1.27    dyoung 		elansc_disable_par(sc->sc_memt, sc->sc_memh, sc->sc_textpar[i]);
   1143      1.27    dyoung 		sc->sc_textpar[i] = -1;
   1144      1.26    dyoung 	}
   1145      1.26    dyoung 	if (sc->sc_pg0par != -1) {
   1146      1.26    dyoung 		elansc_disable_par(sc->sc_memt, sc->sc_memh, sc->sc_pg0par);
   1147      1.26    dyoung 		sc->sc_pg0par = -1;
   1148      1.26    dyoung 	}
   1149      1.26    dyoung 	return true;
   1150      1.26    dyoung }
   1151      1.26    dyoung 
   1152      1.22    dyoung static void
   1153      1.30    dyoung elanpar_deferred_attach(device_t self)
   1154      1.30    dyoung {
   1155      1.30    dyoung 	struct elansc_softc *sc = device_private(device_parent(self));
   1156      1.30    dyoung 
   1157      1.30    dyoung 	elansc_protect_text(self, sc);
   1158      1.30    dyoung }
   1159      1.30    dyoung 
   1160      1.30    dyoung static void
   1161      1.22    dyoung elanpar_attach(device_t parent, device_t self, void *aux)
   1162      1.22    dyoung {
   1163      1.22    dyoung 	struct elansc_softc *sc = device_private(parent);
   1164      1.22    dyoung 
   1165      1.22    dyoung 	aprint_naive(": Programmable Address Regions\n");
   1166      1.22    dyoung 	aprint_normal(": AMD Elan SC520 Programmable Address Regions\n");
   1167      1.22    dyoung 
   1168      1.22    dyoung 	elansc_print_1(self, sc, MMCR_WPVMAP);
   1169      1.22    dyoung 	elansc_print_all_par(self, sc->sc_memt, sc->sc_memh);
   1170      1.22    dyoung 
   1171      1.23    dyoung 	sc->sc_pg0par = elansc_protect_pg0(self, sc);
   1172      1.30    dyoung 	/* XXX grotty hack to avoid trapping writes by x86_patch()
   1173      1.30    dyoung 	 * to the kernel text on a MULTIPROCESSOR kernel.
   1174      1.30    dyoung 	 */
   1175      1.30    dyoung 	config_interrupts(self, elanpar_deferred_attach);
   1176      1.27    dyoung 
   1177      1.27    dyoung 	elansc_print_all_par(self, sc->sc_memt, sc->sc_memh);
   1178      1.22    dyoung 
   1179      1.22    dyoung 	elanpar_intr_establish(self, sc);
   1180      1.22    dyoung 
   1181      1.22    dyoung 	elansc_print_1(self, sc, MMCR_ADDDECCTL);
   1182      1.22    dyoung 
   1183      1.26    dyoung 	if (!pmf_device_register1(self, elanpar_suspend, elanpar_resume,
   1184      1.26    dyoung 	                          elanpar_shutdown))
   1185      1.22    dyoung 		aprint_error_dev(self, "could not establish power hooks\n");
   1186      1.22    dyoung }
   1187      1.22    dyoung 
   1188      1.22    dyoung static void
   1189      1.22    dyoung elanpar_intr_disestablish(struct elansc_softc *sc)
   1190      1.22    dyoung {
   1191      1.22    dyoung 	uint8_t adddecctl, wpvmap;
   1192      1.22    dyoung 
   1193      1.22    dyoung 	/* disable interrupt, acknowledge it, disestablish our
   1194      1.22    dyoung 	 * handler, unmap it
   1195      1.22    dyoung 	 */
   1196      1.22    dyoung 	adddecctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_ADDDECCTL);
   1197      1.22    dyoung 	adddecctl &= ~MMCR_ADDDECCTL_WPV_INT_ENB;
   1198      1.22    dyoung 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_ADDDECCTL, adddecctl);
   1199      1.22    dyoung 
   1200      1.22    dyoung 	bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WPVSTA,
   1201      1.22    dyoung 	    MMCR_WPVSTA_WPV_STA);
   1202      1.22    dyoung 
   1203      1.22    dyoung 	if (elansc_wpvnmi)
   1204      1.22    dyoung 		nmi_disestablish(sc->sc_pih);
   1205      1.22    dyoung 	else
   1206      1.22    dyoung 		intr_disestablish(sc->sc_pih);
   1207      1.22    dyoung 	sc->sc_pih = NULL;
   1208      1.22    dyoung 
   1209      1.22    dyoung 	wpvmap = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_WPVMAP);
   1210      1.22    dyoung 	wpvmap &= ~MMCR_WPVMAP_INT_MAP;
   1211      1.22    dyoung 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_WPVMAP, wpvmap);
   1212      1.22    dyoung }
   1213      1.22    dyoung 
   1214      1.22    dyoung static int
   1215      1.22    dyoung elanpar_detach(device_t self, int flags)
   1216      1.22    dyoung {
   1217      1.22    dyoung 	struct elansc_softc *sc = device_private(device_parent(self));
   1218      1.22    dyoung 
   1219      1.22    dyoung 	pmf_device_deregister(self);
   1220      1.22    dyoung 
   1221      1.26    dyoung 	elanpar_shutdown(self, 0);
   1222      1.22    dyoung 
   1223      1.22    dyoung 	elanpar_intr_disestablish(sc);
   1224      1.22    dyoung 
   1225      1.22    dyoung 	return 0;
   1226      1.22    dyoung }
   1227      1.22    dyoung 
   1228       1.1   thorpej static void
   1229      1.21    dyoung elansc_attach(device_t parent, device_t self, void *aux)
   1230       1.1   thorpej {
   1231      1.17    dyoung 	struct elansc_softc *sc = device_private(self);
   1232      1.31    dyoung 	struct pcibus_attach_args *pba = aux;
   1233       1.1   thorpej 	uint16_t rev;
   1234      1.22    dyoung 	uint8_t cpuctl, picicr, ressta;
   1235      1.10  drochner #if NGPIO > 0
   1236      1.10  drochner 	struct gpiobus_attach_args gba;
   1237      1.22    dyoung 	int pin, reg, shift;
   1238       1.9       riz 	uint16_t data;
   1239      1.10  drochner #endif
   1240      1.29    dyoung 
   1241      1.29    dyoung 	sc->sc_dev = self;
   1242      1.29    dyoung 
   1243      1.31    dyoung 	sc->sc_pc = pba->pba_pc;
   1244      1.31    dyoung 	sc->sc_tag = pci_make_tag(sc->sc_pc, 0, 0, 0);
   1245       1.1   thorpej 
   1246      1.14   thorpej 	aprint_naive(": System Controller\n");
   1247      1.14   thorpej 	aprint_normal(": AMD Elan SC520 System Controller\n");
   1248       1.1   thorpej 
   1249      1.31    dyoung 	sc->sc_memt = pba->pba_memt;
   1250       1.5   thorpej 	if (bus_space_map(sc->sc_memt, MMCR_BASE_ADDR, PAGE_SIZE, 0,
   1251       1.1   thorpej 	    &sc->sc_memh) != 0) {
   1252      1.29    dyoung 		aprint_error_dev(sc->sc_dev, "unable to map registers\n");
   1253       1.1   thorpej 		return;
   1254       1.1   thorpej 	}
   1255       1.1   thorpej 
   1256      1.19    dyoung 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_HIGH);
   1257      1.19    dyoung 
   1258       1.1   thorpej 	rev = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_REVID);
   1259       1.1   thorpej 	cpuctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_CPUCTL);
   1260       1.1   thorpej 
   1261      1.29    dyoung 	aprint_normal_dev(sc->sc_dev,
   1262      1.21    dyoung 	    "product %d stepping %d.%d, CPU clock %s\n",
   1263       1.1   thorpej 	    (rev & REVID_PRODID) >> REVID_PRODID_SHIFT,
   1264       1.1   thorpej 	    (rev & REVID_MAJSTEP) >> REVID_MAJSTEP_SHIFT,
   1265       1.1   thorpej 	    (rev & REVID_MINSTEP),
   1266       1.1   thorpej 	    elansc_speeds[cpuctl & CPUCTL_CPU_CLK_SPD_MASK]);
   1267       1.1   thorpej 
   1268       1.1   thorpej 	/*
   1269       1.1   thorpej 	 * SC520 rev A1 has a bug that affects the watchdog timer.  If
   1270       1.1   thorpej 	 * the GP bus echo mode is enabled, writing to the watchdog control
   1271       1.1   thorpej 	 * register is blocked.
   1272       1.1   thorpej 	 *
   1273       1.1   thorpej 	 * The BIOS in some systems (e.g. the Soekris net4501) enables
   1274       1.1   thorpej 	 * GP bus echo for various reasons, so we need to switch it off
   1275       1.1   thorpej 	 * when we talk to the watchdog timer.
   1276       1.1   thorpej 	 *
   1277       1.1   thorpej 	 * XXX The step 1.1 (B1?) in my Soekris net4501 also has this
   1278       1.1   thorpej 	 * XXX problem, so we'll just enable it for all Elan SC520s
   1279       1.8    keihan 	 * XXX for now.  --thorpej (at) NetBSD.org
   1280       1.1   thorpej 	 */
   1281       1.1   thorpej 	if (1 || rev == ((PRODID_ELAN_SC520 << REVID_PRODID_SHIFT) |
   1282       1.1   thorpej 		    (0 << REVID_MAJSTEP_SHIFT) | (1)))
   1283       1.1   thorpej 		sc->sc_echobug = 1;
   1284       1.1   thorpej 
   1285       1.1   thorpej 	/*
   1286       1.1   thorpej 	 * Determine cause of the last reset, and issue a warning if it
   1287       1.1   thorpej 	 * was due to watchdog expiry.
   1288       1.1   thorpej 	 */
   1289       1.1   thorpej 	ressta = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_RESSTA);
   1290       1.1   thorpej 	if (ressta & RESSTA_WDT_RST_DET)
   1291      1.29    dyoung 		aprint_error_dev(sc->sc_dev,
   1292      1.21    dyoung 		    "WARNING: LAST RESET DUE TO WATCHDOG EXPIRATION!\n");
   1293       1.1   thorpej 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_RESSTA, ressta);
   1294       1.1   thorpej 
   1295      1.22    dyoung 	elansc_print_1(self, sc, MMCR_MPICMODE);
   1296      1.22    dyoung 	elansc_print_1(self, sc, MMCR_SL1PICMODE);
   1297      1.22    dyoung 	elansc_print_1(self, sc, MMCR_SL2PICMODE);
   1298      1.22    dyoung 	elansc_print_1(self, sc, MMCR_PICICR);
   1299      1.22    dyoung 
   1300      1.22    dyoung 	sc->sc_mpicmode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
   1301      1.22    dyoung 	    MMCR_MPICMODE);
   1302      1.22    dyoung 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_MPICMODE,
   1303      1.22    dyoung 	    sc->sc_mpicmode | __BIT(ELAN_IRQ));
   1304      1.22    dyoung 
   1305      1.22    dyoung 	sc->sc_picicr = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_PICICR);
   1306      1.22    dyoung 	picicr = sc->sc_picicr;
   1307      1.22    dyoung 	if (elansc_pcinmi || elansc_wpvnmi)
   1308      1.22    dyoung 		picicr |= MMCR_PICICR_NMI_ENB;
   1309      1.22    dyoung #if 0
   1310      1.22    dyoung 	/* PC/AT compatibility */
   1311      1.22    dyoung 	picicr |= MMCR_PICICR_S1_GINT_MODE|MMCR_PICICR_M_GINT_MODE;
   1312      1.22    dyoung #endif
   1313      1.22    dyoung 	bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_PICICR, picicr);
   1314      1.22    dyoung 
   1315      1.22    dyoung 	elansc_print_1(self, sc, MMCR_PICICR);
   1316      1.22    dyoung 	elansc_print_1(self, sc, MMCR_MPICMODE);
   1317      1.22    dyoung 
   1318      1.22    dyoung 	mutex_enter(&sc->sc_mtx);
   1319       1.1   thorpej 	/* Set up the watchdog registers with some defaults. */
   1320       1.1   thorpej 	elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
   1321       1.1   thorpej 
   1322       1.1   thorpej 	/* ...and clear it. */
   1323       1.1   thorpej 	elansc_wdogctl_reset(sc);
   1324      1.22    dyoung 	mutex_exit(&sc->sc_mtx);
   1325       1.9       riz 
   1326      1.22    dyoung 	if (!pmf_device_register(self, elansc_suspend, elansc_resume))
   1327      1.22    dyoung 		aprint_error_dev(self, "could not establish power hooks\n");
   1328      1.17    dyoung 
   1329      1.10  drochner #if NGPIO > 0
   1330       1.9       riz 	/* Initialize GPIO pins array */
   1331       1.9       riz 	for (pin = 0; pin < ELANSC_PIO_NPINS; pin++) {
   1332       1.9       riz 		sc->sc_gpio_pins[pin].pin_num = pin;
   1333       1.9       riz 		sc->sc_gpio_pins[pin].pin_caps = GPIO_PIN_INPUT |
   1334       1.9       riz 		    GPIO_PIN_OUTPUT;
   1335       1.9       riz 
   1336       1.9       riz 		/* Read initial state */
   1337       1.9       riz 		reg = (pin < 16 ? MMCR_PIODIR15_0 : MMCR_PIODIR31_16);
   1338       1.9       riz 		shift = pin % 16;
   1339       1.9       riz 		data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
   1340       1.9       riz 		if ((data & (1 << shift)) == 0)
   1341       1.9       riz 			sc->sc_gpio_pins[pin].pin_flags = GPIO_PIN_INPUT;
   1342       1.9       riz 		else
   1343       1.9       riz 			sc->sc_gpio_pins[pin].pin_flags = GPIO_PIN_OUTPUT;
   1344       1.9       riz 		if (elansc_gpio_pin_read(sc, pin) == 0)
   1345       1.9       riz 			sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_LOW;
   1346       1.9       riz 		else
   1347       1.9       riz 			sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_HIGH;
   1348       1.9       riz 	}
   1349       1.9       riz 
   1350       1.9       riz 	/* Create controller tag */
   1351       1.9       riz 	sc->sc_gpio_gc.gp_cookie = sc;
   1352       1.9       riz 	sc->sc_gpio_gc.gp_pin_read = elansc_gpio_pin_read;
   1353       1.9       riz 	sc->sc_gpio_gc.gp_pin_write = elansc_gpio_pin_write;
   1354       1.9       riz 	sc->sc_gpio_gc.gp_pin_ctl = elansc_gpio_pin_ctl;
   1355       1.9       riz 
   1356       1.9       riz 	gba.gba_gc = &sc->sc_gpio_gc;
   1357       1.9       riz 	gba.gba_pins = sc->sc_gpio_pins;
   1358       1.9       riz 	gba.gba_npins = ELANSC_PIO_NPINS;
   1359       1.9       riz 
   1360      1.29    dyoung 	sc->sc_par = config_found_ia(sc->sc_dev, "elanparbus", NULL, NULL);
   1361      1.29    dyoung 	sc->sc_pex = config_found_ia(sc->sc_dev, "elanpexbus", NULL, NULL);
   1362       1.9       riz 	/* Attach GPIO framework */
   1363      1.29    dyoung 	config_found_ia(sc->sc_dev, "gpiobus", &gba, gpiobus_print);
   1364      1.10  drochner #endif /* NGPIO */
   1365      1.19    dyoung 
   1366      1.19    dyoung 	/*
   1367      1.19    dyoung 	 * Hook up the watchdog timer.
   1368      1.19    dyoung 	 */
   1369      1.29    dyoung 	sc->sc_smw.smw_name = device_xname(sc->sc_dev);
   1370      1.19    dyoung 	sc->sc_smw.smw_cookie = sc;
   1371      1.19    dyoung 	sc->sc_smw.smw_setmode = elansc_wdog_setmode;
   1372      1.19    dyoung 	sc->sc_smw.smw_tickle = elansc_wdog_tickle;
   1373      1.19    dyoung 	sc->sc_smw.smw_period = 32;	/* actually 32.54 */
   1374      1.21    dyoung 	if (sysmon_wdog_register(&sc->sc_smw) != 0) {
   1375      1.29    dyoung 		aprint_error_dev(sc->sc_dev,
   1376      1.21    dyoung 		    "unable to register watchdog with sysmon\n");
   1377      1.21    dyoung 	}
   1378      1.31    dyoung 	elansc_attached = true;
   1379      1.31    dyoung 	sc->sc_pci = config_found_ia(self, "pcibus", pba, pcibusprint);
   1380       1.1   thorpej }
   1381       1.1   thorpej 
   1382      1.22    dyoung static int
   1383      1.22    dyoung elanpex_match(device_t parent, struct cfdata *match, void *aux)
   1384      1.22    dyoung {
   1385      1.22    dyoung 	struct elansc_softc *sc = device_private(parent);
   1386      1.22    dyoung 
   1387      1.22    dyoung 	return sc->sc_pex == NULL;
   1388      1.22    dyoung }
   1389      1.22    dyoung 
   1390      1.22    dyoung static int
   1391      1.22    dyoung elanpar_match(device_t parent, struct cfdata *match, void *aux)
   1392      1.22    dyoung {
   1393      1.22    dyoung 	struct elansc_softc *sc = device_private(parent);
   1394      1.22    dyoung 
   1395      1.22    dyoung 	return sc->sc_par == NULL;
   1396      1.22    dyoung }
   1397      1.22    dyoung 
   1398      1.31    dyoung CFATTACH_DECL_NEW(elanpar, 0,
   1399      1.22    dyoung     elanpar_match, elanpar_attach, elanpar_detach, NULL);
   1400      1.22    dyoung 
   1401      1.31    dyoung CFATTACH_DECL_NEW(elanpex, 0,
   1402      1.22    dyoung     elanpex_match, elanpex_attach, elanpex_detach, NULL);
   1403      1.22    dyoung 
   1404      1.29    dyoung CFATTACH_DECL2_NEW(elansc, sizeof(struct elansc_softc),
   1405      1.19    dyoung     elansc_match, elansc_attach, elansc_detach, NULL, NULL,
   1406      1.19    dyoung     elansc_childdetached);
   1407       1.9       riz 
   1408      1.10  drochner #if NGPIO > 0
   1409       1.9       riz static int
   1410       1.9       riz elansc_gpio_pin_read(void *arg, int pin)
   1411       1.9       riz {
   1412       1.9       riz 	struct elansc_softc *sc = arg;
   1413       1.9       riz 	int reg, shift;
   1414      1.13     perry 	uint16_t data;
   1415       1.9       riz 
   1416       1.9       riz 	reg = (pin < 16 ? MMCR_PIODATA15_0 : MMCR_PIODATA31_16);
   1417       1.9       riz 	shift = pin % 16;
   1418      1.19    dyoung 
   1419      1.19    dyoung 	mutex_enter(&sc->sc_mtx);
   1420       1.9       riz 	data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
   1421      1.19    dyoung 	mutex_exit(&sc->sc_mtx);
   1422       1.9       riz 
   1423       1.9       riz 	return ((data >> shift) & 0x1);
   1424       1.9       riz }
   1425       1.9       riz 
   1426       1.9       riz static void
   1427       1.9       riz elansc_gpio_pin_write(void *arg, int pin, int value)
   1428       1.9       riz {
   1429       1.9       riz 	struct elansc_softc *sc = arg;
   1430       1.9       riz 	int reg, shift;
   1431      1.13     perry 	uint16_t data;
   1432       1.9       riz 
   1433       1.9       riz 	reg = (pin < 16 ? MMCR_PIODATA15_0 : MMCR_PIODATA31_16);
   1434       1.9       riz 	shift = pin % 16;
   1435      1.19    dyoung 
   1436      1.19    dyoung 	mutex_enter(&sc->sc_mtx);
   1437       1.9       riz 	data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
   1438       1.9       riz 	if (value == 0)
   1439       1.9       riz 		data &= ~(1 << shift);
   1440       1.9       riz 	else if (value == 1)
   1441       1.9       riz 		data |= (1 << shift);
   1442       1.9       riz 
   1443       1.9       riz 	bus_space_write_2(sc->sc_memt, sc->sc_memh, reg, data);
   1444      1.19    dyoung 	mutex_exit(&sc->sc_mtx);
   1445       1.9       riz }
   1446       1.9       riz 
   1447       1.9       riz static void
   1448       1.9       riz elansc_gpio_pin_ctl(void *arg, int pin, int flags)
   1449       1.9       riz {
   1450       1.9       riz 	struct elansc_softc *sc = arg;
   1451       1.9       riz 	int reg, shift;
   1452      1.13     perry 	uint16_t data;
   1453       1.9       riz 
   1454       1.9       riz 	reg = (pin < 16 ? MMCR_PIODIR15_0 : MMCR_PIODIR31_16);
   1455       1.9       riz 	shift = pin % 16;
   1456      1.19    dyoung 	mutex_enter(&sc->sc_mtx);
   1457       1.9       riz 	data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
   1458       1.9       riz 	if (flags & GPIO_PIN_INPUT)
   1459       1.9       riz 		data &= ~(1 << shift);
   1460       1.9       riz 	if (flags & GPIO_PIN_OUTPUT)
   1461       1.9       riz 		data |= (1 << shift);
   1462       1.9       riz 
   1463       1.9       riz 	bus_space_write_2(sc->sc_memt, sc->sc_memh, reg, data);
   1464      1.19    dyoung 	mutex_exit(&sc->sc_mtx);
   1465       1.9       riz }
   1466      1.10  drochner #endif /* NGPIO */
   1467