Home | History | Annotate | Line # | Download | only in ic
i82365.c revision 1.83
      1  1.83   mycroft /*	$NetBSD: i82365.c,v 1.83 2004/08/12 13:37:40 mycroft Exp $	*/
      2   1.2   thorpej 
      3   1.2   thorpej /*
      4  1.33    chopps  * Copyright (c) 2000 Christian E. Hopps.  All rights reserved.
      5   1.2   thorpej  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
      6   1.2   thorpej  *
      7   1.2   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.2   thorpej  * modification, are permitted provided that the following conditions
      9   1.2   thorpej  * are met:
     10   1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     11   1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     12   1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14   1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     15   1.2   thorpej  * 3. All advertising materials mentioning features or use of this software
     16   1.2   thorpej  *    must display the following acknowledgement:
     17   1.2   thorpej  *	This product includes software developed by Marc Horowitz.
     18   1.2   thorpej  * 4. The name of the author may not be used to endorse or promote products
     19   1.2   thorpej  *    derived from this software without specific prior written permission.
     20   1.2   thorpej  *
     21   1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.2   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.2   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.2   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.2   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.2   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.2   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.2   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.2   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.2   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.2   thorpej  */
     32  1.63     lukem 
     33  1.63     lukem #include <sys/cdefs.h>
     34  1.83   mycroft __KERNEL_RCSID(0, "$NetBSD: i82365.c,v 1.83 2004/08/12 13:37:40 mycroft Exp $");
     35  1.63     lukem 
     36  1.63     lukem #define	PCICDEBUG
     37   1.2   thorpej 
     38   1.2   thorpej #include <sys/param.h>
     39   1.2   thorpej #include <sys/systm.h>
     40   1.2   thorpej #include <sys/device.h>
     41   1.2   thorpej #include <sys/extent.h>
     42  1.20   msaitoh #include <sys/kernel.h>
     43   1.2   thorpej #include <sys/malloc.h>
     44  1.14   thorpej #include <sys/kthread.h>
     45   1.2   thorpej 
     46   1.2   thorpej #include <machine/bus.h>
     47   1.2   thorpej #include <machine/intr.h>
     48   1.2   thorpej 
     49   1.2   thorpej #include <dev/pcmcia/pcmciareg.h>
     50   1.2   thorpej #include <dev/pcmcia/pcmciavar.h>
     51   1.2   thorpej 
     52   1.2   thorpej #include <dev/ic/i82365reg.h>
     53   1.2   thorpej #include <dev/ic/i82365var.h>
     54   1.2   thorpej 
     55   1.2   thorpej #ifdef PCICDEBUG
     56   1.2   thorpej int	pcic_debug = 0;
     57   1.2   thorpej #define	DPRINTF(arg) if (pcic_debug) printf arg;
     58   1.2   thorpej #else
     59   1.2   thorpej #define	DPRINTF(arg)
     60   1.2   thorpej #endif
     61   1.2   thorpej 
     62   1.2   thorpej /*
     63   1.2   thorpej  * Individual drivers will allocate their own memory and io regions. Memory
     64   1.2   thorpej  * regions must be a multiple of 4k, aligned on a 4k boundary.
     65   1.2   thorpej  */
     66   1.2   thorpej 
     67   1.2   thorpej #define	PCIC_MEM_ALIGN	PCIC_MEM_PAGESIZE
     68   1.2   thorpej 
     69   1.2   thorpej void	pcic_attach_socket __P((struct pcic_handle *));
     70  1.33    chopps void	pcic_attach_socket_finish __P((struct pcic_handle *));
     71   1.2   thorpej 
     72   1.2   thorpej int	pcic_submatch __P((struct device *, struct cfdata *, void *));
     73   1.2   thorpej int	pcic_print  __P((void *arg, const char *pnp));
     74   1.2   thorpej int	pcic_intr_socket __P((struct pcic_handle *));
     75  1.33    chopps void	pcic_poll_intr __P((void *));
     76   1.2   thorpej 
     77   1.2   thorpej void	pcic_attach_card __P((struct pcic_handle *));
     78  1.15   thorpej void	pcic_detach_card __P((struct pcic_handle *, int));
     79  1.15   thorpej void	pcic_deactivate_card __P((struct pcic_handle *));
     80   1.2   thorpej 
     81   1.2   thorpej void	pcic_chip_do_mem_map __P((struct pcic_handle *, int));
     82   1.2   thorpej void	pcic_chip_do_io_map __P((struct pcic_handle *, int));
     83   1.2   thorpej 
     84  1.14   thorpej void	pcic_create_event_thread __P((void *));
     85  1.14   thorpej void	pcic_event_thread __P((void *));
     86  1.14   thorpej 
     87  1.14   thorpej void	pcic_queue_event __P((struct pcic_handle *, int));
     88  1.26  sommerfe void	pcic_power __P((int, void *));
     89  1.14   thorpej 
     90  1.83   mycroft static int	pcic_wait_ready __P((struct pcic_handle *));
     91  1.30     enami static void	pcic_delay __P((struct pcic_handle *, int, const char *));
     92   1.8      marc 
     93  1.25      haya static u_int8_t st_pcic_read __P((struct pcic_handle *, int));
     94  1.25      haya static void st_pcic_write __P((struct pcic_handle *, int, u_int8_t));
     95  1.25      haya 
     96   1.2   thorpej int
     97   1.2   thorpej pcic_ident_ok(ident)
     98   1.2   thorpej 	int ident;
     99   1.2   thorpej {
    100   1.2   thorpej 	/* this is very empirical and heuristic */
    101   1.2   thorpej 
    102   1.2   thorpej 	if ((ident == 0) || (ident == 0xff) || (ident & PCIC_IDENT_ZERO))
    103   1.2   thorpej 		return (0);
    104   1.2   thorpej 
    105  1.75   mycroft 	if ((ident & PCIC_IDENT_REV_MASK) == 0)
    106  1.75   mycroft 		return (0);
    107  1.75   mycroft 
    108   1.2   thorpej 	if ((ident & PCIC_IDENT_IFTYPE_MASK) != PCIC_IDENT_IFTYPE_MEM_AND_IO) {
    109   1.2   thorpej #ifdef DIAGNOSTIC
    110   1.2   thorpej 		printf("pcic: does not support memory and I/O cards, "
    111   1.2   thorpej 		    "ignored (ident=%0x)\n", ident);
    112   1.2   thorpej #endif
    113   1.2   thorpej 		return (0);
    114   1.2   thorpej 	}
    115  1.75   mycroft 
    116   1.2   thorpej 	return (1);
    117   1.2   thorpej }
    118   1.2   thorpej 
    119   1.2   thorpej int
    120   1.2   thorpej pcic_vendor(h)
    121   1.2   thorpej 	struct pcic_handle *h;
    122   1.2   thorpej {
    123   1.2   thorpej 	int reg;
    124  1.69  takemura 	int vendor;
    125   1.2   thorpej 
    126  1.75   mycroft 	reg = pcic_read(h, PCIC_IDENT);
    127   1.2   thorpej 
    128  1.75   mycroft 	if ((reg & PCIC_IDENT_REV_MASK) == 0)
    129  1.75   mycroft 		return (PCIC_VENDOR_NONE);
    130   1.2   thorpej 
    131  1.69  takemura 	switch (reg) {
    132  1.75   mycroft 	case 0x00:
    133  1.75   mycroft 	case 0xff:
    134  1.75   mycroft 		return (PCIC_VENDOR_NONE);
    135  1.69  takemura 	case PCIC_IDENT_ID_INTEL0:
    136  1.69  takemura 		vendor = PCIC_VENDOR_I82365SLR0;
    137  1.69  takemura 		break;
    138  1.69  takemura 	case PCIC_IDENT_ID_INTEL1:
    139  1.69  takemura 		vendor = PCIC_VENDOR_I82365SLR1;
    140  1.69  takemura 		break;
    141  1.69  takemura 	case PCIC_IDENT_ID_INTEL2:
    142  1.69  takemura 		vendor = PCIC_VENDOR_I82365SL_DF;
    143  1.69  takemura 		break;
    144  1.69  takemura 	case PCIC_IDENT_ID_IBM1:
    145  1.69  takemura 	case PCIC_IDENT_ID_IBM2:
    146  1.69  takemura 		vendor = PCIC_VENDOR_IBM;
    147  1.69  takemura 		break;
    148  1.69  takemura 	case PCIC_IDENT_ID_IBM3:
    149  1.69  takemura 		vendor = PCIC_VENDOR_IBM_KING;
    150  1.69  takemura 		break;
    151  1.69  takemura 	default:
    152  1.69  takemura 		vendor = PCIC_VENDOR_UNKNOWN;
    153  1.69  takemura 		break;
    154  1.69  takemura 	}
    155  1.69  takemura 
    156  1.69  takemura 	if (vendor == PCIC_VENDOR_I82365SLR0 ||
    157  1.69  takemura 	    vendor == PCIC_VENDOR_I82365SLR1) {
    158  1.69  takemura 		/*
    159  1.75   mycroft 		 * Check for Cirrus PD67xx.
    160  1.75   mycroft 		 * the chip_id of the cirrus toggles between 11 and 00 after a
    161  1.75   mycroft 		 * write.  weird.
    162  1.75   mycroft 		 */
    163  1.75   mycroft 		pcic_write(h, PCIC_CIRRUS_CHIP_INFO, 0);
    164  1.75   mycroft 		reg = pcic_read(h, -1);
    165  1.75   mycroft 		if ((reg & PCIC_CIRRUS_CHIP_INFO_CHIP_ID) ==
    166  1.75   mycroft 		    PCIC_CIRRUS_CHIP_INFO_CHIP_ID) {
    167  1.75   mycroft 			reg = pcic_read(h, -1);
    168  1.75   mycroft 			if ((reg & PCIC_CIRRUS_CHIP_INFO_CHIP_ID) == 0)
    169  1.75   mycroft 				return (PCIC_VENDOR_CIRRUS_PD67XX);
    170  1.75   mycroft 		}
    171  1.75   mycroft 
    172  1.75   mycroft 		/*
    173  1.69  takemura 		 * check for Ricoh RF5C[23]96
    174  1.69  takemura 		 */
    175  1.69  takemura 		reg = pcic_read(h, PCIC_RICOH_REG_CHIP_ID);
    176  1.69  takemura 		switch (reg) {
    177  1.69  takemura 		case PCIC_RICOH_CHIP_ID_5C296:
    178  1.75   mycroft 			return (PCIC_VENDOR_RICOH_5C296);
    179  1.69  takemura 		case PCIC_RICOH_CHIP_ID_5C396:
    180  1.75   mycroft 			return (PCIC_VENDOR_RICOH_5C396);
    181  1.69  takemura 		}
    182  1.69  takemura 	}
    183  1.69  takemura 
    184  1.75   mycroft 	return (vendor);
    185   1.2   thorpej }
    186   1.2   thorpej 
    187   1.2   thorpej char *
    188   1.2   thorpej pcic_vendor_to_string(vendor)
    189   1.2   thorpej 	int vendor;
    190   1.2   thorpej {
    191   1.2   thorpej 	switch (vendor) {
    192   1.2   thorpej 	case PCIC_VENDOR_I82365SLR0:
    193   1.2   thorpej 		return ("Intel 82365SL Revision 0");
    194   1.2   thorpej 	case PCIC_VENDOR_I82365SLR1:
    195   1.2   thorpej 		return ("Intel 82365SL Revision 1");
    196  1.75   mycroft 	case PCIC_VENDOR_CIRRUS_PD67XX:
    197  1.75   mycroft 		return ("Cirrus PD6710/2X");
    198  1.69  takemura 	case PCIC_VENDOR_I82365SL_DF:
    199  1.69  takemura 		return ("Intel 82365SL-DF");
    200  1.69  takemura 	case PCIC_VENDOR_RICOH_5C296:
    201  1.69  takemura 		return ("Ricoh RF5C296");
    202  1.69  takemura 	case PCIC_VENDOR_RICOH_5C396:
    203  1.69  takemura 		return ("Ricoh RF5C396");
    204  1.69  takemura 	case PCIC_VENDOR_IBM:
    205  1.69  takemura 		return ("IBM PCIC");
    206  1.69  takemura 	case PCIC_VENDOR_IBM_KING:
    207  1.69  takemura 		return ("IBM KING");
    208   1.2   thorpej 	}
    209   1.2   thorpej 
    210   1.2   thorpej 	return ("Unknown controller");
    211   1.2   thorpej }
    212   1.2   thorpej 
    213   1.2   thorpej void
    214   1.2   thorpej pcic_attach(sc)
    215   1.2   thorpej 	struct pcic_softc *sc;
    216   1.2   thorpej {
    217  1.75   mycroft 	int i, reg, chip, socket;
    218  1.54   mycroft 	struct pcic_handle *h;
    219   1.2   thorpej 
    220  1.33    chopps 	DPRINTF(("pcic ident regs:"));
    221   1.2   thorpej 
    222  1.53   thorpej 	lockinit(&sc->sc_pcic_lock, PWAIT, "pciclk", 0, 0);
    223  1.53   thorpej 
    224  1.33    chopps 	/* find and configure for the available sockets */
    225  1.33    chopps 	for (i = 0; i < PCIC_NSLOTS; i++) {
    226  1.54   mycroft 		h = &sc->handle[i];
    227  1.33    chopps 		chip = i / 2;
    228  1.33    chopps 		socket = i % 2;
    229  1.54   mycroft 
    230  1.54   mycroft 		h->ph_parent = (struct device *)sc;
    231  1.54   mycroft 		h->chip = chip;
    232  1.54   mycroft 		h->sock = chip * PCIC_CHIP_OFFSET + socket * PCIC_SOCKET_OFFSET;
    233  1.54   mycroft 		h->laststate = PCIC_LASTSTATE_EMPTY;
    234  1.35     enami 		/* initialize pcic_read and pcic_write functions */
    235  1.54   mycroft 		h->ph_read = st_pcic_read;
    236  1.54   mycroft 		h->ph_write = st_pcic_write;
    237  1.54   mycroft 		h->ph_bus_t = sc->iot;
    238  1.54   mycroft 		h->ph_bus_h = sc->ioh;
    239  1.75   mycroft 		h->flags = 0;
    240  1.54   mycroft 
    241  1.33    chopps 		/* need to read vendor -- for cirrus to report no xtra chip */
    242  1.33    chopps 		if (socket == 0)
    243  1.54   mycroft 			h->vendor = (h+1)->vendor = pcic_vendor(h);
    244  1.54   mycroft 
    245  1.75   mycroft 		switch (h->vendor) {
    246  1.75   mycroft 		case PCIC_VENDOR_NONE:
    247  1.75   mycroft 			/* no chip */
    248  1.75   mycroft 			continue;
    249  1.75   mycroft 		case PCIC_VENDOR_CIRRUS_PD67XX:
    250  1.75   mycroft 			reg = pcic_read(h, PCIC_CIRRUS_CHIP_INFO);
    251  1.75   mycroft 			if (socket == 0 ||
    252  1.75   mycroft 			    (reg & PCIC_CIRRUS_CHIP_INFO_SLOTS))
    253  1.75   mycroft 				h->flags = PCIC_FLAG_SOCKETP;
    254  1.75   mycroft 			break;
    255  1.75   mycroft 		default:
    256  1.75   mycroft 			/*
    257  1.75   mycroft 			 * During the socket probe, read the ident register
    258  1.75   mycroft 			 * twice.  I don't understand why, but sometimes the
    259  1.75   mycroft 			 * clone chips in hpcmips boxes read all-0s the first
    260  1.75   mycroft 			 * time. -- mycroft
    261  1.75   mycroft 			 */
    262  1.75   mycroft 			reg = pcic_read(h, PCIC_IDENT);
    263  1.75   mycroft 			DPRINTF(("socket %d ident reg 0x%02x\n", i, reg));
    264  1.75   mycroft 			reg = pcic_read(h, PCIC_IDENT);
    265  1.75   mycroft 			DPRINTF(("socket %d ident reg 0x%02x\n", i, reg));
    266  1.75   mycroft 			if (pcic_ident_ok(reg))
    267  1.75   mycroft 				h->flags = PCIC_FLAG_SOCKETP;
    268  1.75   mycroft 			break;
    269  1.75   mycroft 		}
    270   1.2   thorpej 	}
    271   1.2   thorpej 
    272   1.2   thorpej 	for (i = 0; i < PCIC_NSLOTS; i++) {
    273  1.54   mycroft 		h = &sc->handle[i];
    274  1.54   mycroft 
    275  1.54   mycroft 		if (h->flags & PCIC_FLAG_SOCKETP) {
    276  1.54   mycroft 			SIMPLEQ_INIT(&h->events);
    277  1.33    chopps 
    278  1.75   mycroft 			/* disable interrupts and leave socket in reset */
    279  1.83   mycroft 			pcic_write(h, PCIC_INTR, 0);
    280  1.83   mycroft 
    281  1.83   mycroft 			/* zero out the address windows */
    282  1.83   mycroft 			pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
    283  1.83   mycroft 
    284  1.83   mycroft 			/* power down the socket */
    285  1.83   mycroft 			pcic_write(h, PCIC_PWRCTL, 0);
    286  1.83   mycroft 
    287  1.54   mycroft 			pcic_write(h, PCIC_CSC_INTR, 0);
    288  1.54   mycroft 			(void) pcic_read(h, PCIC_CSC);
    289   1.2   thorpej 		}
    290   1.2   thorpej 	}
    291   1.2   thorpej 
    292  1.33    chopps 	/* print detected info */
    293  1.33    chopps 	for (i = 0; i < PCIC_NSLOTS; i += 2) {
    294  1.54   mycroft 		h = &sc->handle[i];
    295  1.33    chopps 		chip = i / 2;
    296   1.2   thorpej 
    297  1.75   mycroft 		if (h->vendor == PCIC_VENDOR_NONE)
    298  1.75   mycroft 			continue;
    299  1.75   mycroft 
    300  1.72   thorpej 		aprint_normal("%s: controller %d (%s) has ", sc->dev.dv_xname,
    301  1.72   thorpej 		    chip, pcic_vendor_to_string(sc->handle[i].vendor));
    302   1.2   thorpej 
    303  1.54   mycroft 		if ((h->flags & PCIC_FLAG_SOCKETP) &&
    304  1.54   mycroft 		    ((h+1)->flags & PCIC_FLAG_SOCKETP))
    305  1.72   thorpej 			aprint_normal("sockets A and B\n");
    306  1.54   mycroft 		else if (h->flags & PCIC_FLAG_SOCKETP)
    307  1.72   thorpej 			aprint_normal("socket A only\n");
    308  1.54   mycroft 		else if ((h+1)->flags & PCIC_FLAG_SOCKETP)
    309  1.72   thorpej 			aprint_normal("socket B only\n");
    310   1.2   thorpej 		else
    311  1.72   thorpej 			aprint_normal("no sockets\n");
    312   1.2   thorpej 	}
    313   1.2   thorpej }
    314   1.2   thorpej 
    315  1.33    chopps /*
    316  1.33    chopps  * attach the sockets before we know what interrupts we have
    317  1.33    chopps  */
    318   1.2   thorpej void
    319   1.2   thorpej pcic_attach_sockets(sc)
    320   1.2   thorpej 	struct pcic_softc *sc;
    321   1.2   thorpej {
    322   1.2   thorpej 	int i;
    323   1.2   thorpej 
    324   1.2   thorpej 	for (i = 0; i < PCIC_NSLOTS; i++)
    325   1.2   thorpej 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
    326   1.2   thorpej 			pcic_attach_socket(&sc->handle[i]);
    327   1.2   thorpej }
    328   1.2   thorpej 
    329   1.2   thorpej void
    330  1.49     enami pcic_power(why, arg)
    331  1.26  sommerfe 	int why;
    332  1.26  sommerfe 	void *arg;
    333  1.26  sommerfe {
    334  1.26  sommerfe 	struct pcic_handle *h = (struct pcic_handle *)arg;
    335  1.35     enami 	struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
    336  1.33    chopps 	int reg;
    337  1.33    chopps 
    338  1.33    chopps 	DPRINTF(("%s: power: why %d\n", h->ph_parent->dv_xname, why));
    339  1.26  sommerfe 
    340  1.26  sommerfe 	if (h->flags & PCIC_FLAG_SOCKETP) {
    341  1.26  sommerfe 		if ((why == PWR_RESUME) &&
    342  1.26  sommerfe 		    (pcic_read(h, PCIC_CSC_INTR) == 0)) {
    343  1.26  sommerfe #ifdef PCICDEBUG
    344  1.26  sommerfe 			char bitbuf[64];
    345  1.26  sommerfe #endif
    346  1.33    chopps 			reg = PCIC_CSC_INTR_CD_ENABLE;
    347  1.33    chopps 			if (sc->irq != -1)
    348  1.33    chopps 			    reg |= sc->irq << PCIC_CSC_INTR_IRQ_SHIFT;
    349  1.33    chopps 			pcic_write(h, PCIC_CSC_INTR, reg);
    350  1.26  sommerfe 			DPRINTF(("%s: CSC_INTR was zero; reset to %s\n",
    351  1.26  sommerfe 			    sc->dev.dv_xname,
    352  1.26  sommerfe 			    bitmask_snprintf(pcic_read(h, PCIC_CSC_INTR),
    353  1.26  sommerfe 				PCIC_CSC_INTR_FORMAT,
    354  1.26  sommerfe 				bitbuf, sizeof(bitbuf))));
    355  1.26  sommerfe 		}
    356  1.42    itojun 
    357  1.42    itojun 		/*
    358  1.42    itojun 		 * check for card insertion or removal during suspend period.
    359  1.42    itojun 		 * XXX: the code can't cope with card swap (remove then insert).
    360  1.42    itojun 		 * how can we detect such situation?
    361  1.42    itojun 		 */
    362  1.42    itojun 		if (why == PWR_RESUME)
    363  1.42    itojun 			(void)pcic_intr_socket(h);
    364  1.26  sommerfe 	}
    365  1.26  sommerfe }
    366  1.26  sommerfe 
    367  1.26  sommerfe 
    368  1.33    chopps /*
    369  1.33    chopps  * attach a socket -- we don't know about irqs yet
    370  1.33    chopps  */
    371  1.26  sommerfe void
    372   1.2   thorpej pcic_attach_socket(h)
    373   1.2   thorpej 	struct pcic_handle *h;
    374   1.2   thorpej {
    375   1.2   thorpej 	struct pcmciabus_attach_args paa;
    376  1.35     enami 	struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
    377   1.2   thorpej 
    378   1.2   thorpej 	/* initialize the rest of the handle */
    379   1.2   thorpej 
    380  1.14   thorpej 	h->shutdown = 0;
    381   1.2   thorpej 	h->memalloc = 0;
    382   1.2   thorpej 	h->ioalloc = 0;
    383   1.2   thorpej 	h->ih_irq = 0;
    384   1.2   thorpej 
    385   1.2   thorpej 	/* now, config one pcmcia device per socket */
    386   1.2   thorpej 
    387  1.25      haya 	paa.paa_busname = "pcmcia";
    388  1.25      haya 	paa.pct = (pcmcia_chipset_tag_t) sc->pct;
    389   1.2   thorpej 	paa.pch = (pcmcia_chipset_handle_t) h;
    390  1.25      haya 	paa.iobase = sc->iobase;
    391  1.25      haya 	paa.iosize = sc->iosize;
    392   1.2   thorpej 
    393  1.33    chopps 	h->pcmcia = config_found_sm(&sc->dev, &paa, pcic_print, pcic_submatch);
    394  1.50   mycroft 	if (h->pcmcia == NULL) {
    395  1.50   mycroft 		h->flags &= ~PCIC_FLAG_SOCKETP;
    396  1.33    chopps 		return;
    397  1.50   mycroft 	}
    398   1.2   thorpej 
    399  1.33    chopps 	/*
    400  1.33    chopps 	 * queue creation of a kernel thread to handle insert/removal events.
    401  1.33    chopps 	 */
    402  1.33    chopps #ifdef DIAGNOSTIC
    403  1.33    chopps 	if (h->event_thread != NULL)
    404  1.33    chopps 		panic("pcic_attach_socket: event thread");
    405  1.33    chopps #endif
    406  1.33    chopps 	config_pending_incr();
    407  1.33    chopps 	kthread_create(pcic_create_event_thread, h);
    408  1.33    chopps }
    409   1.2   thorpej 
    410  1.33    chopps /*
    411  1.33    chopps  * now finish attaching the sockets, we are ready to allocate
    412  1.33    chopps  * interrupts
    413  1.33    chopps  */
    414  1.33    chopps void
    415  1.33    chopps pcic_attach_sockets_finish(sc)
    416  1.33    chopps 	struct pcic_softc *sc;
    417  1.33    chopps {
    418  1.33    chopps 	int i;
    419  1.33    chopps 
    420  1.33    chopps 	for (i = 0; i < PCIC_NSLOTS; i++)
    421  1.51   mycroft 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
    422  1.33    chopps 			pcic_attach_socket_finish(&sc->handle[i]);
    423  1.33    chopps }
    424  1.33    chopps 
    425  1.33    chopps /*
    426  1.33    chopps  * finishing attaching the socket.  Interrupts may now be on
    427  1.33    chopps  * if so expects the pcic interrupt to be blocked
    428  1.33    chopps  */
    429  1.33    chopps void
    430  1.33    chopps pcic_attach_socket_finish(h)
    431  1.33    chopps 	struct pcic_handle *h;
    432  1.33    chopps {
    433  1.35     enami 	struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
    434  1.83   mycroft 	int reg;
    435  1.33    chopps 
    436  1.46   nathanw 	DPRINTF(("%s: attach finish socket %ld\n", h->ph_parent->dv_xname,
    437  1.46   nathanw 	    (long) (h - &sc->handle[0])));
    438  1.51   mycroft 
    439  1.33    chopps 	/*
    440  1.33    chopps 	 * Set up a powerhook to ensure it continues to interrupt on
    441  1.33    chopps 	 * card detect even after suspend.
    442  1.33    chopps 	 * (this works around a bug seen in suspend-to-disk on the
    443  1.33    chopps 	 * Sony VAIO Z505; on resume, the CSC_INTR state is not preserved).
    444  1.33    chopps 	 */
    445  1.33    chopps 	powerhook_establish(pcic_power, h);
    446  1.33    chopps 
    447  1.33    chopps 	/* enable interrupts on card detect, poll for them if no irq avail */
    448  1.33    chopps 	reg = PCIC_CSC_INTR_CD_ENABLE;
    449  1.57   thorpej 	if (sc->irq == -1) {
    450  1.57   thorpej 		if (sc->poll_established == 0) {
    451  1.57   thorpej 			callout_init(&sc->poll_ch);
    452  1.57   thorpej 			callout_reset(&sc->poll_ch, hz / 2, pcic_poll_intr, sc);
    453  1.57   thorpej 			sc->poll_established = 1;
    454  1.57   thorpej 		}
    455  1.57   thorpej 	} else
    456  1.33    chopps 		reg |= sc->irq << PCIC_CSC_INTR_IRQ_SHIFT;
    457  1.33    chopps 	pcic_write(h, PCIC_CSC_INTR, reg);
    458  1.33    chopps 
    459  1.33    chopps 	/* steer above mgmt interrupt to configured place */
    460  1.73   mycroft 	if (sc->irq == 0)
    461  1.83   mycroft 		pcic_write(h, PCIC_INTR, PCIC_INTR_ENABLE);
    462  1.33    chopps 
    463  1.33    chopps 	/* clear possible card detect interrupt */
    464  1.83   mycroft 	(void) pcic_read(h, PCIC_CSC);
    465  1.33    chopps 
    466  1.33    chopps 	DPRINTF(("%s: attach finish vendor 0x%02x\n", h->ph_parent->dv_xname,
    467  1.33    chopps 	    h->vendor));
    468  1.33    chopps 
    469  1.33    chopps 	/* unsleep the cirrus controller */
    470  1.75   mycroft 	if (h->vendor == PCIC_VENDOR_CIRRUS_PD67XX) {
    471  1.33    chopps 		reg = pcic_read(h, PCIC_CIRRUS_MISC_CTL_2);
    472  1.33    chopps 		if (reg & PCIC_CIRRUS_MISC_CTL_2_SUSPEND) {
    473  1.33    chopps 			DPRINTF(("%s: socket %02x was suspended\n",
    474  1.35     enami 			    h->ph_parent->dv_xname, h->sock));
    475  1.33    chopps 			reg &= ~PCIC_CIRRUS_MISC_CTL_2_SUSPEND;
    476  1.33    chopps 			pcic_write(h, PCIC_CIRRUS_MISC_CTL_2, reg);
    477  1.33    chopps 		}
    478  1.33    chopps 	}
    479  1.33    chopps 
    480  1.33    chopps 	/* if there's a card there, then attach it. */
    481  1.33    chopps 	reg = pcic_read(h, PCIC_IF_STATUS);
    482  1.33    chopps 	if ((reg & PCIC_IF_STATUS_CARDDETECT_MASK) ==
    483  1.33    chopps 	    PCIC_IF_STATUS_CARDDETECT_PRESENT) {
    484  1.33    chopps 		pcic_queue_event(h, PCIC_EVENT_INSERTION);
    485  1.33    chopps 		h->laststate = PCIC_LASTSTATE_PRESENT;
    486  1.33    chopps 	} else {
    487  1.33    chopps 		h->laststate = PCIC_LASTSTATE_EMPTY;
    488  1.33    chopps 	}
    489   1.2   thorpej }
    490   1.2   thorpej 
    491   1.2   thorpej void
    492  1.14   thorpej pcic_create_event_thread(arg)
    493  1.14   thorpej 	void *arg;
    494  1.14   thorpej {
    495  1.14   thorpej 	struct pcic_handle *h = arg;
    496  1.14   thorpej 	const char *cs;
    497  1.14   thorpej 
    498  1.14   thorpej 	switch (h->sock) {
    499  1.14   thorpej 	case C0SA:
    500  1.14   thorpej 		cs = "0,0";
    501  1.14   thorpej 		break;
    502  1.14   thorpej 	case C0SB:
    503  1.14   thorpej 		cs = "0,1";
    504  1.14   thorpej 		break;
    505  1.14   thorpej 	case C1SA:
    506  1.14   thorpej 		cs = "1,0";
    507  1.14   thorpej 		break;
    508  1.14   thorpej 	case C1SB:
    509  1.14   thorpej 		cs = "1,1";
    510  1.14   thorpej 		break;
    511  1.14   thorpej 	default:
    512  1.14   thorpej 		panic("pcic_create_event_thread: unknown pcic socket");
    513  1.14   thorpej 	}
    514  1.14   thorpej 
    515  1.24   thorpej 	if (kthread_create1(pcic_event_thread, h, &h->event_thread,
    516  1.25      haya 	    "%s,%s", h->ph_parent->dv_xname, cs)) {
    517  1.14   thorpej 		printf("%s: unable to create event thread for sock 0x%02x\n",
    518  1.25      haya 		    h->ph_parent->dv_xname, h->sock);
    519  1.14   thorpej 		panic("pcic_create_event_thread");
    520  1.14   thorpej 	}
    521  1.14   thorpej }
    522  1.14   thorpej 
    523  1.14   thorpej void
    524  1.14   thorpej pcic_event_thread(arg)
    525  1.14   thorpej 	void *arg;
    526  1.14   thorpej {
    527  1.14   thorpej 	struct pcic_handle *h = arg;
    528  1.14   thorpej 	struct pcic_event *pe;
    529  1.29     enami 	int s, first = 1;
    530  1.35     enami 	struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
    531  1.14   thorpej 
    532  1.14   thorpej 	while (h->shutdown == 0) {
    533  1.53   thorpej 		/*
    534  1.53   thorpej 		 * Serialize event processing on the PCIC.  We may
    535  1.53   thorpej 		 * sleep while we hold this lock.
    536  1.53   thorpej 		 */
    537  1.53   thorpej 		(void) lockmgr(&sc->sc_pcic_lock, LK_EXCLUSIVE, NULL);
    538  1.53   thorpej 
    539  1.14   thorpej 		s = splhigh();
    540  1.14   thorpej 		if ((pe = SIMPLEQ_FIRST(&h->events)) == NULL) {
    541  1.14   thorpej 			splx(s);
    542  1.29     enami 			if (first) {
    543  1.29     enami 				first = 0;
    544  1.29     enami 				config_pending_decr();
    545  1.29     enami 			}
    546  1.53   thorpej 			/*
    547  1.53   thorpej 			 * No events to process; release the PCIC lock.
    548  1.53   thorpej 			 */
    549  1.53   thorpej 			(void) lockmgr(&sc->sc_pcic_lock, LK_RELEASE, NULL);
    550  1.14   thorpej 			(void) tsleep(&h->events, PWAIT, "pcicev", 0);
    551  1.14   thorpej 			continue;
    552  1.20   msaitoh 		} else {
    553  1.20   msaitoh 			splx(s);
    554  1.20   msaitoh 			/* sleep .25s to be enqueued chatterling interrupts */
    555  1.35     enami 			(void) tsleep((caddr_t)pcic_event_thread, PWAIT,
    556  1.35     enami 			    "pcicss", hz/4);
    557  1.14   thorpej 		}
    558  1.20   msaitoh 		s = splhigh();
    559  1.66     lukem 		SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
    560  1.14   thorpej 		splx(s);
    561  1.14   thorpej 
    562  1.14   thorpej 		switch (pe->pe_type) {
    563  1.14   thorpej 		case PCIC_EVENT_INSERTION:
    564  1.20   msaitoh 			s = splhigh();
    565  1.20   msaitoh 			while (1) {
    566  1.20   msaitoh 				struct pcic_event *pe1, *pe2;
    567  1.20   msaitoh 
    568  1.20   msaitoh 				if ((pe1 = SIMPLEQ_FIRST(&h->events)) == NULL)
    569  1.20   msaitoh 					break;
    570  1.20   msaitoh 				if (pe1->pe_type != PCIC_EVENT_REMOVAL)
    571  1.20   msaitoh 					break;
    572  1.20   msaitoh 				if ((pe2 = SIMPLEQ_NEXT(pe1, pe_q)) == NULL)
    573  1.20   msaitoh 					break;
    574  1.20   msaitoh 				if (pe2->pe_type == PCIC_EVENT_INSERTION) {
    575  1.66     lukem 					SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
    576  1.20   msaitoh 					free(pe1, M_TEMP);
    577  1.66     lukem 					SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
    578  1.20   msaitoh 					free(pe2, M_TEMP);
    579  1.20   msaitoh 				}
    580  1.20   msaitoh 			}
    581  1.20   msaitoh 			splx(s);
    582  1.20   msaitoh 
    583  1.35     enami 			DPRINTF(("%s: insertion event\n",
    584  1.35     enami 			    h->ph_parent->dv_xname));
    585  1.14   thorpej 			pcic_attach_card(h);
    586  1.14   thorpej 			break;
    587  1.14   thorpej 
    588  1.14   thorpej 		case PCIC_EVENT_REMOVAL:
    589  1.20   msaitoh 			s = splhigh();
    590  1.20   msaitoh 			while (1) {
    591  1.20   msaitoh 				struct pcic_event *pe1, *pe2;
    592  1.20   msaitoh 
    593  1.20   msaitoh 				if ((pe1 = SIMPLEQ_FIRST(&h->events)) == NULL)
    594  1.20   msaitoh 					break;
    595  1.20   msaitoh 				if (pe1->pe_type != PCIC_EVENT_INSERTION)
    596  1.20   msaitoh 					break;
    597  1.20   msaitoh 				if ((pe2 = SIMPLEQ_NEXT(pe1, pe_q)) == NULL)
    598  1.20   msaitoh 					break;
    599  1.20   msaitoh 				if (pe2->pe_type == PCIC_EVENT_REMOVAL) {
    600  1.66     lukem 					SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
    601  1.20   msaitoh 					free(pe1, M_TEMP);
    602  1.66     lukem 					SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
    603  1.20   msaitoh 					free(pe2, M_TEMP);
    604  1.20   msaitoh 				}
    605  1.20   msaitoh 			}
    606  1.20   msaitoh 			splx(s);
    607  1.20   msaitoh 
    608  1.35     enami 			DPRINTF(("%s: removal event\n",
    609  1.35     enami 			    h->ph_parent->dv_xname));
    610  1.15   thorpej 			pcic_detach_card(h, DETACH_FORCE);
    611  1.14   thorpej 			break;
    612  1.14   thorpej 
    613  1.14   thorpej 		default:
    614  1.14   thorpej 			panic("pcic_event_thread: unknown event %d",
    615  1.14   thorpej 			    pe->pe_type);
    616  1.14   thorpej 		}
    617  1.14   thorpej 		free(pe, M_TEMP);
    618  1.53   thorpej 
    619  1.53   thorpej 		(void) lockmgr(&sc->sc_pcic_lock, LK_RELEASE, NULL);
    620  1.14   thorpej 	}
    621  1.14   thorpej 
    622  1.14   thorpej 	h->event_thread = NULL;
    623  1.14   thorpej 
    624  1.14   thorpej 	/* In case parent is waiting for us to exit. */
    625  1.25      haya 	wakeup(sc);
    626  1.14   thorpej 
    627  1.14   thorpej 	kthread_exit(0);
    628  1.14   thorpej }
    629  1.14   thorpej 
    630   1.2   thorpej int
    631   1.2   thorpej pcic_submatch(parent, cf, aux)
    632   1.2   thorpej 	struct device *parent;
    633   1.2   thorpej 	struct cfdata *cf;
    634   1.2   thorpej 	void *aux;
    635   1.2   thorpej {
    636   1.2   thorpej 
    637   1.3     enami 	struct pcmciabus_attach_args *paa = aux;
    638   1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) paa->pch;
    639   1.2   thorpej 
    640   1.2   thorpej 	switch (h->sock) {
    641   1.2   thorpej 	case C0SA:
    642  1.78   thorpej 		if (cf->pcmciabuscf_controller !=
    643  1.16   thorpej 		    PCMCIABUSCF_CONTROLLER_DEFAULT &&
    644  1.78   thorpej 		    cf->pcmciabuscf_controller != 0)
    645   1.2   thorpej 			return 0;
    646  1.78   thorpej 		if (cf->pcmciabuscf_socket !=
    647  1.16   thorpej 		    PCMCIABUSCF_SOCKET_DEFAULT &&
    648  1.78   thorpej 		    cf->pcmciabuscf_socket != 0)
    649   1.2   thorpej 			return 0;
    650   1.2   thorpej 
    651   1.2   thorpej 		break;
    652   1.2   thorpej 	case C0SB:
    653  1.78   thorpej 		if (cf->pcmciabuscf_controller !=
    654  1.16   thorpej 		    PCMCIABUSCF_CONTROLLER_DEFAULT &&
    655  1.78   thorpej 		    cf->pcmciabuscf_controller != 0)
    656   1.2   thorpej 			return 0;
    657  1.78   thorpej 		if (cf->pcmciabuscf_socket !=
    658  1.16   thorpej 		    PCMCIABUSCF_SOCKET_DEFAULT &&
    659  1.78   thorpej 		    cf->pcmciabuscf_socket != 1)
    660   1.2   thorpej 			return 0;
    661   1.2   thorpej 
    662   1.2   thorpej 		break;
    663   1.2   thorpej 	case C1SA:
    664  1.78   thorpej 		if (cf->pcmciabuscf_controller !=
    665  1.16   thorpej 		    PCMCIABUSCF_CONTROLLER_DEFAULT &&
    666  1.78   thorpej 		    cf->pcmciabuscf_controller != 1)
    667   1.2   thorpej 			return 0;
    668  1.78   thorpej 		if (cf->pcmciabuscf_socket !=
    669  1.16   thorpej 		    PCMCIABUSCF_SOCKET_DEFAULT &&
    670  1.78   thorpej 		    cf->pcmciabuscf_socket != 0)
    671   1.2   thorpej 			return 0;
    672   1.2   thorpej 
    673   1.2   thorpej 		break;
    674   1.2   thorpej 	case C1SB:
    675  1.78   thorpej 		if (cf->pcmciabuscf_controller !=
    676  1.16   thorpej 		    PCMCIABUSCF_CONTROLLER_DEFAULT &&
    677  1.78   thorpej 		    cf->pcmciabuscf_controller != 1)
    678   1.2   thorpej 			return 0;
    679  1.78   thorpej 		if (cf->pcmciabuscf_socket !=
    680  1.16   thorpej 		    PCMCIABUSCF_SOCKET_DEFAULT &&
    681  1.78   thorpej 		    cf->pcmciabuscf_socket != 1)
    682   1.2   thorpej 			return 0;
    683   1.2   thorpej 
    684   1.2   thorpej 		break;
    685   1.2   thorpej 	default:
    686   1.2   thorpej 		panic("unknown pcic socket");
    687   1.2   thorpej 	}
    688   1.2   thorpej 
    689  1.67   thorpej 	return (config_match(parent, cf, aux));
    690   1.2   thorpej }
    691   1.2   thorpej 
    692   1.2   thorpej int
    693   1.2   thorpej pcic_print(arg, pnp)
    694   1.2   thorpej 	void *arg;
    695   1.2   thorpej 	const char *pnp;
    696   1.2   thorpej {
    697   1.3     enami 	struct pcmciabus_attach_args *paa = arg;
    698   1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) paa->pch;
    699   1.2   thorpej 
    700   1.2   thorpej 	/* Only "pcmcia"s can attach to "pcic"s... easy. */
    701   1.2   thorpej 	if (pnp)
    702  1.70   thorpej 		aprint_normal("pcmcia at %s", pnp);
    703   1.2   thorpej 
    704   1.2   thorpej 	switch (h->sock) {
    705   1.2   thorpej 	case C0SA:
    706  1.70   thorpej 		aprint_normal(" controller 0 socket 0");
    707   1.2   thorpej 		break;
    708   1.2   thorpej 	case C0SB:
    709  1.70   thorpej 		aprint_normal(" controller 0 socket 1");
    710   1.2   thorpej 		break;
    711   1.2   thorpej 	case C1SA:
    712  1.70   thorpej 		aprint_normal(" controller 1 socket 0");
    713   1.2   thorpej 		break;
    714   1.2   thorpej 	case C1SB:
    715  1.70   thorpej 		aprint_normal(" controller 1 socket 1");
    716   1.2   thorpej 		break;
    717   1.2   thorpej 	default:
    718   1.2   thorpej 		panic("unknown pcic socket");
    719   1.2   thorpej 	}
    720   1.2   thorpej 
    721   1.2   thorpej 	return (UNCONF);
    722   1.2   thorpej }
    723   1.2   thorpej 
    724  1.33    chopps void
    725  1.33    chopps pcic_poll_intr(arg)
    726  1.33    chopps 	void *arg;
    727  1.33    chopps {
    728  1.33    chopps 	struct pcic_softc *sc;
    729  1.33    chopps 	int i, s;
    730  1.33    chopps 
    731  1.33    chopps 	s = spltty();
    732  1.33    chopps 	sc = arg;
    733  1.33    chopps 	for (i = 0; i < PCIC_NSLOTS; i++)
    734  1.33    chopps 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
    735  1.33    chopps 			(void)pcic_intr_socket(&sc->handle[i]);
    736  1.57   thorpej 	callout_reset(&sc->poll_ch, hz / 2, pcic_poll_intr, sc);
    737  1.33    chopps 	splx(s);
    738  1.33    chopps }
    739  1.33    chopps 
    740   1.2   thorpej int
    741   1.2   thorpej pcic_intr(arg)
    742   1.2   thorpej 	void *arg;
    743   1.2   thorpej {
    744   1.3     enami 	struct pcic_softc *sc = arg;
    745   1.2   thorpej 	int i, ret = 0;
    746   1.2   thorpej 
    747   1.2   thorpej 	DPRINTF(("%s: intr\n", sc->dev.dv_xname));
    748   1.2   thorpej 
    749   1.2   thorpej 	for (i = 0; i < PCIC_NSLOTS; i++)
    750   1.2   thorpej 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
    751   1.2   thorpej 			ret += pcic_intr_socket(&sc->handle[i]);
    752   1.2   thorpej 
    753   1.2   thorpej 	return (ret ? 1 : 0);
    754   1.2   thorpej }
    755   1.2   thorpej 
    756   1.2   thorpej int
    757   1.2   thorpej pcic_intr_socket(h)
    758   1.2   thorpej 	struct pcic_handle *h;
    759   1.2   thorpej {
    760   1.2   thorpej 	int cscreg;
    761   1.2   thorpej 
    762   1.2   thorpej 	cscreg = pcic_read(h, PCIC_CSC);
    763   1.2   thorpej 
    764   1.2   thorpej 	cscreg &= (PCIC_CSC_GPI |
    765   1.2   thorpej 		   PCIC_CSC_CD |
    766   1.2   thorpej 		   PCIC_CSC_READY |
    767   1.2   thorpej 		   PCIC_CSC_BATTWARN |
    768   1.2   thorpej 		   PCIC_CSC_BATTDEAD);
    769   1.2   thorpej 
    770   1.2   thorpej 	if (cscreg & PCIC_CSC_GPI) {
    771  1.25      haya 		DPRINTF(("%s: %02x GPI\n", h->ph_parent->dv_xname, h->sock));
    772   1.2   thorpej 	}
    773   1.2   thorpej 	if (cscreg & PCIC_CSC_CD) {
    774   1.2   thorpej 		int statreg;
    775   1.2   thorpej 
    776   1.2   thorpej 		statreg = pcic_read(h, PCIC_IF_STATUS);
    777   1.2   thorpej 
    778  1.25      haya 		DPRINTF(("%s: %02x CD %x\n", h->ph_parent->dv_xname, h->sock,
    779   1.2   thorpej 		    statreg));
    780   1.2   thorpej 
    781   1.2   thorpej 		if ((statreg & PCIC_IF_STATUS_CARDDETECT_MASK) ==
    782   1.2   thorpej 		    PCIC_IF_STATUS_CARDDETECT_PRESENT) {
    783  1.20   msaitoh 			if (h->laststate != PCIC_LASTSTATE_PRESENT) {
    784  1.14   thorpej 				DPRINTF(("%s: enqueing INSERTION event\n",
    785  1.25      haya 					 h->ph_parent->dv_xname));
    786  1.14   thorpej 				pcic_queue_event(h, PCIC_EVENT_INSERTION);
    787  1.14   thorpej 			}
    788  1.20   msaitoh 			h->laststate = PCIC_LASTSTATE_PRESENT;
    789   1.2   thorpej 		} else {
    790  1.20   msaitoh 			if (h->laststate == PCIC_LASTSTATE_PRESENT) {
    791  1.15   thorpej 				/* Deactivate the card now. */
    792  1.15   thorpej 				DPRINTF(("%s: deactivating card\n",
    793  1.25      haya 					 h->ph_parent->dv_xname));
    794  1.15   thorpej 				pcic_deactivate_card(h);
    795  1.15   thorpej 
    796  1.14   thorpej 				DPRINTF(("%s: enqueing REMOVAL event\n",
    797  1.25      haya 					 h->ph_parent->dv_xname));
    798  1.14   thorpej 				pcic_queue_event(h, PCIC_EVENT_REMOVAL);
    799  1.14   thorpej 			}
    800  1.83   mycroft 			h->laststate = PCIC_LASTSTATE_EMPTY;
    801   1.2   thorpej 		}
    802   1.2   thorpej 	}
    803   1.2   thorpej 	if (cscreg & PCIC_CSC_READY) {
    804  1.25      haya 		DPRINTF(("%s: %02x READY\n", h->ph_parent->dv_xname, h->sock));
    805   1.2   thorpej 		/* shouldn't happen */
    806   1.2   thorpej 	}
    807   1.2   thorpej 	if (cscreg & PCIC_CSC_BATTWARN) {
    808  1.35     enami 		DPRINTF(("%s: %02x BATTWARN\n", h->ph_parent->dv_xname,
    809  1.35     enami 		    h->sock));
    810   1.2   thorpej 	}
    811   1.2   thorpej 	if (cscreg & PCIC_CSC_BATTDEAD) {
    812  1.35     enami 		DPRINTF(("%s: %02x BATTDEAD\n", h->ph_parent->dv_xname,
    813  1.35     enami 		    h->sock));
    814   1.2   thorpej 	}
    815   1.2   thorpej 	return (cscreg ? 1 : 0);
    816  1.14   thorpej }
    817  1.14   thorpej 
    818  1.14   thorpej void
    819  1.14   thorpej pcic_queue_event(h, event)
    820  1.14   thorpej 	struct pcic_handle *h;
    821  1.14   thorpej 	int event;
    822  1.14   thorpej {
    823  1.14   thorpej 	struct pcic_event *pe;
    824  1.14   thorpej 	int s;
    825  1.14   thorpej 
    826  1.14   thorpej 	pe = malloc(sizeof(*pe), M_TEMP, M_NOWAIT);
    827  1.14   thorpej 	if (pe == NULL)
    828  1.14   thorpej 		panic("pcic_queue_event: can't allocate event");
    829  1.14   thorpej 
    830  1.14   thorpej 	pe->pe_type = event;
    831  1.14   thorpej 	s = splhigh();
    832  1.14   thorpej 	SIMPLEQ_INSERT_TAIL(&h->events, pe, pe_q);
    833  1.14   thorpej 	splx(s);
    834  1.14   thorpej 	wakeup(&h->events);
    835   1.2   thorpej }
    836   1.2   thorpej 
    837   1.2   thorpej void
    838   1.2   thorpej pcic_attach_card(h)
    839   1.2   thorpej 	struct pcic_handle *h;
    840   1.2   thorpej {
    841  1.15   thorpej 
    842  1.20   msaitoh 	if (!(h->flags & PCIC_FLAG_CARDP)) {
    843  1.20   msaitoh 		/* call the MI attach function */
    844  1.20   msaitoh 		pcmcia_card_attach(h->pcmcia);
    845   1.2   thorpej 
    846  1.20   msaitoh 		h->flags |= PCIC_FLAG_CARDP;
    847  1.20   msaitoh 	} else {
    848  1.20   msaitoh 		DPRINTF(("pcic_attach_card: already attached"));
    849  1.20   msaitoh 	}
    850   1.2   thorpej }
    851   1.2   thorpej 
    852   1.2   thorpej void
    853  1.15   thorpej pcic_detach_card(h, flags)
    854   1.2   thorpej 	struct pcic_handle *h;
    855  1.15   thorpej 	int flags;		/* DETACH_* */
    856   1.2   thorpej {
    857  1.15   thorpej 
    858  1.20   msaitoh 	if (h->flags & PCIC_FLAG_CARDP) {
    859  1.20   msaitoh 		h->flags &= ~PCIC_FLAG_CARDP;
    860   1.2   thorpej 
    861  1.20   msaitoh 		/* call the MI detach function */
    862  1.20   msaitoh 		pcmcia_card_detach(h->pcmcia, flags);
    863  1.20   msaitoh 	} else {
    864  1.20   msaitoh 		DPRINTF(("pcic_detach_card: already detached"));
    865  1.20   msaitoh 	}
    866  1.15   thorpej }
    867  1.15   thorpej 
    868  1.15   thorpej void
    869  1.15   thorpej pcic_deactivate_card(h)
    870  1.15   thorpej 	struct pcic_handle *h;
    871  1.15   thorpej {
    872  1.74   mycroft 	int intr;
    873   1.2   thorpej 
    874  1.15   thorpej 	/* call the MI deactivate function */
    875  1.15   thorpej 	pcmcia_card_deactivate(h->pcmcia);
    876   1.2   thorpej 
    877   1.2   thorpej 	/* power down the socket */
    878   1.2   thorpej 	pcic_write(h, PCIC_PWRCTL, 0);
    879   1.2   thorpej 
    880  1.15   thorpej 	/* reset the socket */
    881  1.74   mycroft 	intr = pcic_read(h, PCIC_INTR);
    882  1.74   mycroft 	intr &= PCIC_INTR_ENABLE;
    883  1.74   mycroft 	pcic_write(h, PCIC_INTR, intr);
    884   1.2   thorpej }
    885   1.2   thorpej 
    886   1.2   thorpej int
    887   1.2   thorpej pcic_chip_mem_alloc(pch, size, pcmhp)
    888   1.2   thorpej 	pcmcia_chipset_handle_t pch;
    889   1.2   thorpej 	bus_size_t size;
    890   1.2   thorpej 	struct pcmcia_mem_handle *pcmhp;
    891   1.2   thorpej {
    892   1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
    893   1.2   thorpej 	bus_space_handle_t memh;
    894   1.2   thorpej 	bus_addr_t addr;
    895   1.2   thorpej 	bus_size_t sizepg;
    896   1.2   thorpej 	int i, mask, mhandle;
    897  1.35     enami 	struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
    898   1.2   thorpej 
    899   1.2   thorpej 	/* out of sc->memh, allocate as many pages as necessary */
    900   1.2   thorpej 
    901   1.2   thorpej 	/* convert size to PCIC pages */
    902   1.2   thorpej 	sizepg = (size + (PCIC_MEM_ALIGN - 1)) / PCIC_MEM_ALIGN;
    903  1.19  christos 	if (sizepg > PCIC_MAX_MEM_PAGES)
    904  1.19  christos 		return (1);
    905   1.2   thorpej 
    906   1.2   thorpej 	mask = (1 << sizepg) - 1;
    907   1.2   thorpej 
    908   1.2   thorpej 	addr = 0;		/* XXX gcc -Wuninitialized */
    909   1.2   thorpej 	mhandle = 0;		/* XXX gcc -Wuninitialized */
    910   1.2   thorpej 
    911  1.19  christos 	for (i = 0; i <= PCIC_MAX_MEM_PAGES - sizepg; i++) {
    912  1.25      haya 		if ((sc->subregionmask & (mask << i)) == (mask << i)) {
    913  1.25      haya 			if (bus_space_subregion(sc->memt, sc->memh,
    914   1.2   thorpej 			    i * PCIC_MEM_PAGESIZE,
    915   1.2   thorpej 			    sizepg * PCIC_MEM_PAGESIZE, &memh))
    916   1.2   thorpej 				return (1);
    917   1.2   thorpej 			mhandle = mask << i;
    918  1.25      haya 			addr = sc->membase + (i * PCIC_MEM_PAGESIZE);
    919  1.25      haya 			sc->subregionmask &= ~(mhandle);
    920  1.25      haya 			pcmhp->memt = sc->memt;
    921  1.19  christos 			pcmhp->memh = memh;
    922  1.19  christos 			pcmhp->addr = addr;
    923  1.19  christos 			pcmhp->size = size;
    924  1.19  christos 			pcmhp->mhandle = mhandle;
    925  1.19  christos 			pcmhp->realsize = sizepg * PCIC_MEM_PAGESIZE;
    926  1.19  christos 			return (0);
    927   1.2   thorpej 		}
    928   1.2   thorpej 	}
    929   1.2   thorpej 
    930  1.19  christos 	return (1);
    931   1.2   thorpej }
    932   1.2   thorpej 
    933   1.2   thorpej void
    934   1.2   thorpej pcic_chip_mem_free(pch, pcmhp)
    935   1.2   thorpej 	pcmcia_chipset_handle_t pch;
    936   1.2   thorpej 	struct pcmcia_mem_handle *pcmhp;
    937   1.2   thorpej {
    938   1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
    939  1.35     enami 	struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
    940   1.2   thorpej 
    941  1.25      haya 	sc->subregionmask |= pcmhp->mhandle;
    942   1.2   thorpej }
    943   1.2   thorpej 
    944  1.62  jdolecek static const struct mem_map_index_st {
    945   1.2   thorpej 	int	sysmem_start_lsb;
    946   1.2   thorpej 	int	sysmem_start_msb;
    947   1.2   thorpej 	int	sysmem_stop_lsb;
    948   1.2   thorpej 	int	sysmem_stop_msb;
    949   1.2   thorpej 	int	cardmem_lsb;
    950   1.2   thorpej 	int	cardmem_msb;
    951   1.2   thorpej 	int	memenable;
    952   1.2   thorpej } mem_map_index[] = {
    953   1.2   thorpej 	{
    954   1.2   thorpej 		PCIC_SYSMEM_ADDR0_START_LSB,
    955   1.2   thorpej 		PCIC_SYSMEM_ADDR0_START_MSB,
    956   1.2   thorpej 		PCIC_SYSMEM_ADDR0_STOP_LSB,
    957   1.2   thorpej 		PCIC_SYSMEM_ADDR0_STOP_MSB,
    958   1.2   thorpej 		PCIC_CARDMEM_ADDR0_LSB,
    959   1.2   thorpej 		PCIC_CARDMEM_ADDR0_MSB,
    960   1.2   thorpej 		PCIC_ADDRWIN_ENABLE_MEM0,
    961   1.2   thorpej 	},
    962   1.2   thorpej 	{
    963   1.2   thorpej 		PCIC_SYSMEM_ADDR1_START_LSB,
    964   1.2   thorpej 		PCIC_SYSMEM_ADDR1_START_MSB,
    965   1.2   thorpej 		PCIC_SYSMEM_ADDR1_STOP_LSB,
    966   1.2   thorpej 		PCIC_SYSMEM_ADDR1_STOP_MSB,
    967   1.2   thorpej 		PCIC_CARDMEM_ADDR1_LSB,
    968   1.2   thorpej 		PCIC_CARDMEM_ADDR1_MSB,
    969   1.2   thorpej 		PCIC_ADDRWIN_ENABLE_MEM1,
    970   1.2   thorpej 	},
    971   1.2   thorpej 	{
    972   1.2   thorpej 		PCIC_SYSMEM_ADDR2_START_LSB,
    973   1.2   thorpej 		PCIC_SYSMEM_ADDR2_START_MSB,
    974   1.2   thorpej 		PCIC_SYSMEM_ADDR2_STOP_LSB,
    975   1.2   thorpej 		PCIC_SYSMEM_ADDR2_STOP_MSB,
    976   1.2   thorpej 		PCIC_CARDMEM_ADDR2_LSB,
    977   1.2   thorpej 		PCIC_CARDMEM_ADDR2_MSB,
    978   1.2   thorpej 		PCIC_ADDRWIN_ENABLE_MEM2,
    979   1.2   thorpej 	},
    980   1.2   thorpej 	{
    981   1.2   thorpej 		PCIC_SYSMEM_ADDR3_START_LSB,
    982   1.2   thorpej 		PCIC_SYSMEM_ADDR3_START_MSB,
    983   1.2   thorpej 		PCIC_SYSMEM_ADDR3_STOP_LSB,
    984   1.2   thorpej 		PCIC_SYSMEM_ADDR3_STOP_MSB,
    985   1.2   thorpej 		PCIC_CARDMEM_ADDR3_LSB,
    986   1.2   thorpej 		PCIC_CARDMEM_ADDR3_MSB,
    987   1.2   thorpej 		PCIC_ADDRWIN_ENABLE_MEM3,
    988   1.2   thorpej 	},
    989   1.2   thorpej 	{
    990   1.2   thorpej 		PCIC_SYSMEM_ADDR4_START_LSB,
    991   1.2   thorpej 		PCIC_SYSMEM_ADDR4_START_MSB,
    992   1.2   thorpej 		PCIC_SYSMEM_ADDR4_STOP_LSB,
    993   1.2   thorpej 		PCIC_SYSMEM_ADDR4_STOP_MSB,
    994   1.2   thorpej 		PCIC_CARDMEM_ADDR4_LSB,
    995   1.2   thorpej 		PCIC_CARDMEM_ADDR4_MSB,
    996   1.2   thorpej 		PCIC_ADDRWIN_ENABLE_MEM4,
    997   1.2   thorpej 	},
    998   1.2   thorpej };
    999   1.2   thorpej 
   1000   1.2   thorpej void
   1001   1.2   thorpej pcic_chip_do_mem_map(h, win)
   1002   1.2   thorpej 	struct pcic_handle *h;
   1003   1.2   thorpej 	int win;
   1004   1.2   thorpej {
   1005   1.2   thorpej 	int reg;
   1006  1.28      joda 	int kind = h->mem[win].kind & ~PCMCIA_WIDTH_MEM_MASK;
   1007  1.35     enami 	int mem8 =
   1008  1.47    chopps 	    (h->mem[win].kind & PCMCIA_WIDTH_MEM_MASK) == PCMCIA_WIDTH_MEM8
   1009  1.47    chopps 	    || (kind == PCMCIA_MEM_ATTR);
   1010  1.28      joda 
   1011  1.33    chopps 	DPRINTF(("mem8 %d\n", mem8));
   1012  1.33    chopps 	/* mem8 = 1; */
   1013  1.33    chopps 
   1014   1.2   thorpej 	pcic_write(h, mem_map_index[win].sysmem_start_lsb,
   1015   1.2   thorpej 	    (h->mem[win].addr >> PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
   1016   1.2   thorpej 	pcic_write(h, mem_map_index[win].sysmem_start_msb,
   1017   1.2   thorpej 	    ((h->mem[win].addr >> (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
   1018  1.43      joda 	    PCIC_SYSMEM_ADDRX_START_MSB_ADDR_MASK) |
   1019  1.44     enami 	    (mem8 ? 0 : PCIC_SYSMEM_ADDRX_START_MSB_DATASIZE_16BIT));
   1020   1.2   thorpej 
   1021   1.2   thorpej 	pcic_write(h, mem_map_index[win].sysmem_stop_lsb,
   1022   1.2   thorpej 	    ((h->mem[win].addr + h->mem[win].size) >>
   1023   1.2   thorpej 	    PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
   1024   1.2   thorpej 	pcic_write(h, mem_map_index[win].sysmem_stop_msb,
   1025   1.2   thorpej 	    (((h->mem[win].addr + h->mem[win].size) >>
   1026   1.2   thorpej 	    (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
   1027   1.2   thorpej 	    PCIC_SYSMEM_ADDRX_STOP_MSB_ADDR_MASK) |
   1028   1.2   thorpej 	    PCIC_SYSMEM_ADDRX_STOP_MSB_WAIT2);
   1029   1.2   thorpej 
   1030   1.2   thorpej 	pcic_write(h, mem_map_index[win].cardmem_lsb,
   1031   1.2   thorpej 	    (h->mem[win].offset >> PCIC_CARDMEM_ADDRX_SHIFT) & 0xff);
   1032   1.2   thorpej 	pcic_write(h, mem_map_index[win].cardmem_msb,
   1033   1.2   thorpej 	    ((h->mem[win].offset >> (PCIC_CARDMEM_ADDRX_SHIFT + 8)) &
   1034   1.2   thorpej 	    PCIC_CARDMEM_ADDRX_MSB_ADDR_MASK) |
   1035  1.28      joda 	    ((kind == PCMCIA_MEM_ATTR) ?
   1036   1.2   thorpej 	    PCIC_CARDMEM_ADDRX_MSB_REGACTIVE_ATTR : 0));
   1037   1.2   thorpej 
   1038   1.2   thorpej 	reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
   1039  1.43      joda 	reg |= (mem_map_index[win].memenable | PCIC_ADDRWIN_ENABLE_MEMCS16);
   1040   1.2   thorpej 	pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
   1041  1.21      marc 
   1042  1.21      marc 	delay(100);
   1043   1.2   thorpej 
   1044   1.2   thorpej #ifdef PCICDEBUG
   1045   1.2   thorpej 	{
   1046   1.2   thorpej 		int r1, r2, r3, r4, r5, r6;
   1047   1.2   thorpej 
   1048   1.2   thorpej 		r1 = pcic_read(h, mem_map_index[win].sysmem_start_msb);
   1049   1.2   thorpej 		r2 = pcic_read(h, mem_map_index[win].sysmem_start_lsb);
   1050   1.2   thorpej 		r3 = pcic_read(h, mem_map_index[win].sysmem_stop_msb);
   1051   1.2   thorpej 		r4 = pcic_read(h, mem_map_index[win].sysmem_stop_lsb);
   1052   1.2   thorpej 		r5 = pcic_read(h, mem_map_index[win].cardmem_msb);
   1053   1.2   thorpej 		r6 = pcic_read(h, mem_map_index[win].cardmem_lsb);
   1054   1.2   thorpej 
   1055   1.2   thorpej 		DPRINTF(("pcic_chip_do_mem_map window %d: %02x%02x %02x%02x "
   1056   1.2   thorpej 		    "%02x%02x\n", win, r1, r2, r3, r4, r5, r6));
   1057   1.2   thorpej 	}
   1058   1.2   thorpej #endif
   1059   1.2   thorpej }
   1060   1.2   thorpej 
   1061   1.2   thorpej int
   1062   1.2   thorpej pcic_chip_mem_map(pch, kind, card_addr, size, pcmhp, offsetp, windowp)
   1063   1.2   thorpej 	pcmcia_chipset_handle_t pch;
   1064   1.2   thorpej 	int kind;
   1065   1.2   thorpej 	bus_addr_t card_addr;
   1066   1.2   thorpej 	bus_size_t size;
   1067   1.2   thorpej 	struct pcmcia_mem_handle *pcmhp;
   1068  1.65     soren 	bus_size_t *offsetp;
   1069   1.2   thorpej 	int *windowp;
   1070   1.2   thorpej {
   1071   1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1072   1.2   thorpej 	bus_addr_t busaddr;
   1073   1.2   thorpej 	long card_offset;
   1074   1.2   thorpej 	int i, win;
   1075  1.35     enami 	struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
   1076   1.2   thorpej 
   1077   1.2   thorpej 	win = -1;
   1078   1.2   thorpej 	for (i = 0; i < (sizeof(mem_map_index) / sizeof(mem_map_index[0]));
   1079   1.2   thorpej 	    i++) {
   1080   1.2   thorpej 		if ((h->memalloc & (1 << i)) == 0) {
   1081   1.2   thorpej 			win = i;
   1082   1.2   thorpej 			h->memalloc |= (1 << i);
   1083   1.2   thorpej 			break;
   1084   1.2   thorpej 		}
   1085   1.2   thorpej 	}
   1086   1.2   thorpej 
   1087   1.2   thorpej 	if (win == -1)
   1088   1.2   thorpej 		return (1);
   1089   1.2   thorpej 
   1090   1.2   thorpej 	*windowp = win;
   1091   1.2   thorpej 
   1092   1.2   thorpej 	/* XXX this is pretty gross */
   1093   1.2   thorpej 
   1094  1.25      haya 	if (sc->memt != pcmhp->memt)
   1095   1.2   thorpej 		panic("pcic_chip_mem_map memt is bogus");
   1096   1.2   thorpej 
   1097   1.2   thorpej 	busaddr = pcmhp->addr;
   1098   1.2   thorpej 
   1099   1.2   thorpej 	/*
   1100   1.2   thorpej 	 * compute the address offset to the pcmcia address space for the
   1101   1.2   thorpej 	 * pcic.  this is intentionally signed.  The masks and shifts below
   1102   1.2   thorpej 	 * will cause TRT to happen in the pcic registers.  Deal with making
   1103   1.2   thorpej 	 * sure the address is aligned, and return the alignment offset.
   1104   1.2   thorpej 	 */
   1105   1.2   thorpej 
   1106   1.2   thorpej 	*offsetp = card_addr % PCIC_MEM_ALIGN;
   1107   1.2   thorpej 	card_addr -= *offsetp;
   1108   1.2   thorpej 
   1109   1.2   thorpej 	DPRINTF(("pcic_chip_mem_map window %d bus %lx+%lx+%lx at card addr "
   1110   1.2   thorpej 	    "%lx\n", win, (u_long) busaddr, (u_long) * offsetp, (u_long) size,
   1111   1.2   thorpej 	    (u_long) card_addr));
   1112   1.2   thorpej 
   1113   1.2   thorpej 	/*
   1114   1.2   thorpej 	 * include the offset in the size, and decrement size by one, since
   1115   1.2   thorpej 	 * the hw wants start/stop
   1116   1.2   thorpej 	 */
   1117   1.2   thorpej 	size += *offsetp - 1;
   1118   1.2   thorpej 
   1119   1.2   thorpej 	card_offset = (((long) card_addr) - ((long) busaddr));
   1120   1.2   thorpej 
   1121   1.2   thorpej 	h->mem[win].addr = busaddr;
   1122   1.2   thorpej 	h->mem[win].size = size;
   1123   1.2   thorpej 	h->mem[win].offset = card_offset;
   1124   1.2   thorpej 	h->mem[win].kind = kind;
   1125   1.2   thorpej 
   1126   1.2   thorpej 	pcic_chip_do_mem_map(h, win);
   1127   1.2   thorpej 
   1128   1.2   thorpej 	return (0);
   1129   1.2   thorpej }
   1130   1.2   thorpej 
   1131   1.2   thorpej void
   1132   1.2   thorpej pcic_chip_mem_unmap(pch, window)
   1133   1.2   thorpej 	pcmcia_chipset_handle_t pch;
   1134   1.2   thorpej 	int window;
   1135   1.2   thorpej {
   1136   1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1137   1.2   thorpej 	int reg;
   1138   1.2   thorpej 
   1139   1.2   thorpej 	if (window >= (sizeof(mem_map_index) / sizeof(mem_map_index[0])))
   1140   1.2   thorpej 		panic("pcic_chip_mem_unmap: window out of range");
   1141   1.2   thorpej 
   1142   1.2   thorpej 	reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
   1143   1.2   thorpej 	reg &= ~mem_map_index[window].memenable;
   1144   1.2   thorpej 	pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
   1145   1.2   thorpej 
   1146   1.2   thorpej 	h->memalloc &= ~(1 << window);
   1147   1.2   thorpej }
   1148   1.2   thorpej 
   1149   1.2   thorpej int
   1150   1.2   thorpej pcic_chip_io_alloc(pch, start, size, align, pcihp)
   1151   1.2   thorpej 	pcmcia_chipset_handle_t pch;
   1152   1.2   thorpej 	bus_addr_t start;
   1153   1.2   thorpej 	bus_size_t size;
   1154   1.2   thorpej 	bus_size_t align;
   1155   1.2   thorpej 	struct pcmcia_io_handle *pcihp;
   1156   1.2   thorpej {
   1157   1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1158   1.2   thorpej 	bus_space_tag_t iot;
   1159   1.2   thorpej 	bus_space_handle_t ioh;
   1160   1.2   thorpej 	bus_addr_t ioaddr;
   1161   1.2   thorpej 	int flags = 0;
   1162  1.35     enami 	struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
   1163   1.2   thorpej 
   1164   1.2   thorpej 	/*
   1165   1.2   thorpej 	 * Allocate some arbitrary I/O space.
   1166   1.2   thorpej 	 */
   1167   1.2   thorpej 
   1168  1.25      haya 	iot = sc->iot;
   1169   1.2   thorpej 
   1170   1.2   thorpej 	if (start) {
   1171   1.2   thorpej 		ioaddr = start;
   1172   1.2   thorpej 		if (bus_space_map(iot, start, size, 0, &ioh))
   1173   1.2   thorpej 			return (1);
   1174   1.2   thorpej 		DPRINTF(("pcic_chip_io_alloc map port %lx+%lx\n",
   1175   1.2   thorpej 		    (u_long) ioaddr, (u_long) size));
   1176   1.2   thorpej 	} else {
   1177   1.2   thorpej 		flags |= PCMCIA_IO_ALLOCATED;
   1178  1.25      haya 		if (bus_space_alloc(iot, sc->iobase,
   1179  1.25      haya 		    sc->iobase + sc->iosize, size, align, 0, 0,
   1180   1.2   thorpej 		    &ioaddr, &ioh))
   1181   1.2   thorpej 			return (1);
   1182   1.2   thorpej 		DPRINTF(("pcic_chip_io_alloc alloc port %lx+%lx\n",
   1183   1.2   thorpej 		    (u_long) ioaddr, (u_long) size));
   1184   1.2   thorpej 	}
   1185   1.2   thorpej 
   1186   1.2   thorpej 	pcihp->iot = iot;
   1187   1.2   thorpej 	pcihp->ioh = ioh;
   1188   1.2   thorpej 	pcihp->addr = ioaddr;
   1189   1.2   thorpej 	pcihp->size = size;
   1190   1.2   thorpej 	pcihp->flags = flags;
   1191   1.2   thorpej 
   1192   1.2   thorpej 	return (0);
   1193   1.2   thorpej }
   1194   1.2   thorpej 
   1195   1.2   thorpej void
   1196   1.2   thorpej pcic_chip_io_free(pch, pcihp)
   1197   1.2   thorpej 	pcmcia_chipset_handle_t pch;
   1198   1.2   thorpej 	struct pcmcia_io_handle *pcihp;
   1199   1.2   thorpej {
   1200   1.2   thorpej 	bus_space_tag_t iot = pcihp->iot;
   1201   1.2   thorpej 	bus_space_handle_t ioh = pcihp->ioh;
   1202   1.2   thorpej 	bus_size_t size = pcihp->size;
   1203   1.2   thorpej 
   1204   1.2   thorpej 	if (pcihp->flags & PCMCIA_IO_ALLOCATED)
   1205   1.2   thorpej 		bus_space_free(iot, ioh, size);
   1206   1.2   thorpej 	else
   1207   1.2   thorpej 		bus_space_unmap(iot, ioh, size);
   1208   1.2   thorpej }
   1209   1.2   thorpej 
   1210   1.2   thorpej 
   1211  1.62  jdolecek static const struct io_map_index_st {
   1212   1.2   thorpej 	int	start_lsb;
   1213   1.2   thorpej 	int	start_msb;
   1214   1.2   thorpej 	int	stop_lsb;
   1215   1.2   thorpej 	int	stop_msb;
   1216   1.2   thorpej 	int	ioenable;
   1217   1.2   thorpej 	int	ioctlmask;
   1218   1.2   thorpej 	int	ioctlbits[3];		/* indexed by PCMCIA_WIDTH_* */
   1219   1.2   thorpej }               io_map_index[] = {
   1220   1.2   thorpej 	{
   1221   1.2   thorpej 		PCIC_IOADDR0_START_LSB,
   1222   1.2   thorpej 		PCIC_IOADDR0_START_MSB,
   1223   1.2   thorpej 		PCIC_IOADDR0_STOP_LSB,
   1224   1.2   thorpej 		PCIC_IOADDR0_STOP_MSB,
   1225   1.2   thorpej 		PCIC_ADDRWIN_ENABLE_IO0,
   1226   1.2   thorpej 		PCIC_IOCTL_IO0_WAITSTATE | PCIC_IOCTL_IO0_ZEROWAIT |
   1227   1.2   thorpej 		PCIC_IOCTL_IO0_IOCS16SRC_MASK | PCIC_IOCTL_IO0_DATASIZE_MASK,
   1228   1.2   thorpej 		{
   1229   1.2   thorpej 			PCIC_IOCTL_IO0_IOCS16SRC_CARD,
   1230   1.6     enami 			PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
   1231   1.6     enami 			    PCIC_IOCTL_IO0_DATASIZE_8BIT,
   1232   1.6     enami 			PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
   1233   1.6     enami 			    PCIC_IOCTL_IO0_DATASIZE_16BIT,
   1234   1.2   thorpej 		},
   1235   1.2   thorpej 	},
   1236   1.2   thorpej 	{
   1237   1.2   thorpej 		PCIC_IOADDR1_START_LSB,
   1238   1.2   thorpej 		PCIC_IOADDR1_START_MSB,
   1239   1.2   thorpej 		PCIC_IOADDR1_STOP_LSB,
   1240   1.2   thorpej 		PCIC_IOADDR1_STOP_MSB,
   1241   1.2   thorpej 		PCIC_ADDRWIN_ENABLE_IO1,
   1242   1.2   thorpej 		PCIC_IOCTL_IO1_WAITSTATE | PCIC_IOCTL_IO1_ZEROWAIT |
   1243   1.2   thorpej 		PCIC_IOCTL_IO1_IOCS16SRC_MASK | PCIC_IOCTL_IO1_DATASIZE_MASK,
   1244   1.2   thorpej 		{
   1245   1.2   thorpej 			PCIC_IOCTL_IO1_IOCS16SRC_CARD,
   1246   1.2   thorpej 			PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
   1247   1.2   thorpej 			    PCIC_IOCTL_IO1_DATASIZE_8BIT,
   1248   1.2   thorpej 			PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
   1249   1.2   thorpej 			    PCIC_IOCTL_IO1_DATASIZE_16BIT,
   1250   1.2   thorpej 		},
   1251   1.2   thorpej 	},
   1252   1.2   thorpej };
   1253   1.2   thorpej 
   1254   1.2   thorpej void
   1255   1.2   thorpej pcic_chip_do_io_map(h, win)
   1256   1.2   thorpej 	struct pcic_handle *h;
   1257   1.2   thorpej 	int win;
   1258   1.2   thorpej {
   1259   1.2   thorpej 	int reg;
   1260   1.2   thorpej 
   1261   1.2   thorpej 	DPRINTF(("pcic_chip_do_io_map win %d addr %lx size %lx width %d\n",
   1262   1.2   thorpej 	    win, (long) h->io[win].addr, (long) h->io[win].size,
   1263   1.2   thorpej 	    h->io[win].width * 8));
   1264   1.2   thorpej 
   1265   1.2   thorpej 	pcic_write(h, io_map_index[win].start_lsb, h->io[win].addr & 0xff);
   1266   1.2   thorpej 	pcic_write(h, io_map_index[win].start_msb,
   1267   1.2   thorpej 	    (h->io[win].addr >> 8) & 0xff);
   1268   1.2   thorpej 
   1269   1.2   thorpej 	pcic_write(h, io_map_index[win].stop_lsb,
   1270   1.2   thorpej 	    (h->io[win].addr + h->io[win].size - 1) & 0xff);
   1271   1.2   thorpej 	pcic_write(h, io_map_index[win].stop_msb,
   1272   1.2   thorpej 	    ((h->io[win].addr + h->io[win].size - 1) >> 8) & 0xff);
   1273   1.2   thorpej 
   1274   1.2   thorpej 	reg = pcic_read(h, PCIC_IOCTL);
   1275   1.2   thorpej 	reg &= ~io_map_index[win].ioctlmask;
   1276   1.2   thorpej 	reg |= io_map_index[win].ioctlbits[h->io[win].width];
   1277   1.2   thorpej 	pcic_write(h, PCIC_IOCTL, reg);
   1278   1.2   thorpej 
   1279   1.2   thorpej 	reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
   1280   1.2   thorpej 	reg |= io_map_index[win].ioenable;
   1281   1.2   thorpej 	pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
   1282   1.2   thorpej }
   1283   1.2   thorpej 
   1284   1.2   thorpej int
   1285   1.2   thorpej pcic_chip_io_map(pch, width, offset, size, pcihp, windowp)
   1286   1.2   thorpej 	pcmcia_chipset_handle_t pch;
   1287   1.2   thorpej 	int width;
   1288   1.2   thorpej 	bus_addr_t offset;
   1289   1.2   thorpej 	bus_size_t size;
   1290   1.2   thorpej 	struct pcmcia_io_handle *pcihp;
   1291   1.2   thorpej 	int *windowp;
   1292   1.2   thorpej {
   1293   1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1294   1.2   thorpej 	bus_addr_t ioaddr = pcihp->addr + offset;
   1295   1.4     enami 	int i, win;
   1296   1.4     enami #ifdef PCICDEBUG
   1297   1.2   thorpej 	static char *width_names[] = { "auto", "io8", "io16" };
   1298   1.4     enami #endif
   1299  1.35     enami 	struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
   1300   1.2   thorpej 
   1301   1.2   thorpej 	/* XXX Sanity check offset/size. */
   1302   1.2   thorpej 
   1303   1.2   thorpej 	win = -1;
   1304   1.2   thorpej 	for (i = 0; i < (sizeof(io_map_index) / sizeof(io_map_index[0])); i++) {
   1305   1.2   thorpej 		if ((h->ioalloc & (1 << i)) == 0) {
   1306   1.2   thorpej 			win = i;
   1307   1.2   thorpej 			h->ioalloc |= (1 << i);
   1308   1.2   thorpej 			break;
   1309   1.2   thorpej 		}
   1310   1.2   thorpej 	}
   1311   1.2   thorpej 
   1312   1.2   thorpej 	if (win == -1)
   1313   1.2   thorpej 		return (1);
   1314   1.2   thorpej 
   1315   1.2   thorpej 	*windowp = win;
   1316   1.2   thorpej 
   1317   1.2   thorpej 	/* XXX this is pretty gross */
   1318   1.2   thorpej 
   1319  1.25      haya 	if (sc->iot != pcihp->iot)
   1320   1.2   thorpej 		panic("pcic_chip_io_map iot is bogus");
   1321   1.2   thorpej 
   1322   1.2   thorpej 	DPRINTF(("pcic_chip_io_map window %d %s port %lx+%lx\n",
   1323   1.2   thorpej 		 win, width_names[width], (u_long) ioaddr, (u_long) size));
   1324   1.2   thorpej 
   1325   1.2   thorpej 	/* XXX wtf is this doing here? */
   1326   1.2   thorpej 
   1327  1.77  christos 	printf("%s: port 0x%lx", sc->dev.dv_xname, (u_long) ioaddr);
   1328   1.2   thorpej 	if (size > 1)
   1329   1.2   thorpej 		printf("-0x%lx", (u_long) ioaddr + (u_long) size - 1);
   1330  1.77  christos 	printf("\n");
   1331   1.2   thorpej 
   1332   1.2   thorpej 	h->io[win].addr = ioaddr;
   1333   1.2   thorpej 	h->io[win].size = size;
   1334   1.2   thorpej 	h->io[win].width = width;
   1335   1.2   thorpej 
   1336   1.2   thorpej 	pcic_chip_do_io_map(h, win);
   1337   1.2   thorpej 
   1338   1.2   thorpej 	return (0);
   1339   1.2   thorpej }
   1340   1.2   thorpej 
   1341   1.2   thorpej void
   1342   1.2   thorpej pcic_chip_io_unmap(pch, window)
   1343   1.2   thorpej 	pcmcia_chipset_handle_t pch;
   1344   1.2   thorpej 	int window;
   1345   1.2   thorpej {
   1346   1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1347   1.2   thorpej 	int reg;
   1348   1.2   thorpej 
   1349   1.2   thorpej 	if (window >= (sizeof(io_map_index) / sizeof(io_map_index[0])))
   1350   1.2   thorpej 		panic("pcic_chip_io_unmap: window out of range");
   1351   1.2   thorpej 
   1352   1.2   thorpej 	reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
   1353   1.2   thorpej 	reg &= ~io_map_index[window].ioenable;
   1354   1.2   thorpej 	pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
   1355   1.2   thorpej 
   1356   1.2   thorpej 	h->ioalloc &= ~(1 << window);
   1357   1.8      marc }
   1358   1.8      marc 
   1359  1.83   mycroft static int
   1360   1.8      marc pcic_wait_ready(h)
   1361   1.8      marc 	struct pcic_handle *h;
   1362   1.8      marc {
   1363  1.83   mycroft 	u_int8_t stat;
   1364   1.8      marc 	int i;
   1365   1.8      marc 
   1366  1.31    chopps 	/* wait an initial 10ms for quick cards */
   1367  1.83   mycroft 	stat = pcic_read(h, PCIC_IF_STATUS);
   1368  1.83   mycroft 	if (stat & PCIC_IF_STATUS_READY)
   1369  1.83   mycroft 		return (0);
   1370  1.36     enami 	pcic_delay(h, 10, "pccwr0");
   1371  1.31    chopps 	for (i = 0; i < 50; i++) {
   1372  1.83   mycroft 		stat = pcic_read(h, PCIC_IF_STATUS);
   1373  1.83   mycroft 		if (stat & PCIC_IF_STATUS_READY)
   1374  1.83   mycroft 			return (0);
   1375  1.83   mycroft 		if ((stat & PCIC_IF_STATUS_CARDDETECT_MASK) !=
   1376  1.83   mycroft 		    PCIC_IF_STATUS_CARDDETECT_PRESENT)
   1377  1.83   mycroft 			return (ENXIO);
   1378  1.31    chopps 		/* wait .1s (100ms) each iteration now */
   1379  1.36     enami 		pcic_delay(h, 100, "pccwr1");
   1380   1.8      marc 	}
   1381   1.8      marc 
   1382  1.83   mycroft 	printf("pcic_wait_ready: ready never happened, status=%02x\n", stat);
   1383  1.83   mycroft 	return (EWOULDBLOCK);
   1384   1.2   thorpej }
   1385   1.2   thorpej 
   1386  1.30     enami /*
   1387  1.30     enami  * Perform long (msec order) delay.
   1388  1.30     enami  */
   1389  1.30     enami static void
   1390  1.36     enami pcic_delay(h, timo, wmesg)
   1391  1.30     enami 	struct pcic_handle *h;
   1392  1.30     enami 	int timo;			/* in ms.  must not be zero */
   1393  1.36     enami 	const char *wmesg;
   1394  1.30     enami {
   1395  1.30     enami 
   1396  1.30     enami #ifdef DIAGNOSTIC
   1397  1.83   mycroft 	if (timo <= 0)
   1398  1.83   mycroft 		panic("pcic_delay: called with timeout %d", timo);
   1399  1.83   mycroft 	if (!curlwp)
   1400  1.83   mycroft 		panic("pcic_delay: called in interrupt context");
   1401  1.83   mycroft 	if (!h->event_thread)
   1402  1.83   mycroft 		panic("pcic_delay: no event thread");
   1403  1.30     enami #endif
   1404  1.48       dbj 	DPRINTF(("pcic_delay: \"%s\" %p, sleep %d ms\n",
   1405  1.49     enami 	    wmesg, h->event_thread, timo));
   1406  1.40     enami 	tsleep(pcic_delay, PWAIT, wmesg, roundup(timo * hz, 1000) / 1000);
   1407  1.30     enami }
   1408  1.30     enami 
   1409   1.2   thorpej void
   1410   1.2   thorpej pcic_chip_socket_enable(pch)
   1411   1.2   thorpej 	pcmcia_chipset_handle_t pch;
   1412   1.2   thorpej {
   1413   1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1414  1.83   mycroft 	int win;
   1415  1.83   mycroft 	u_int8_t power, intr;
   1416  1.83   mycroft #ifdef DIAGNOSTIC
   1417  1.34    chopps 	int reg;
   1418  1.34    chopps #endif
   1419   1.2   thorpej 
   1420  1.41    chopps #ifdef DIAGNOSTIC
   1421  1.41    chopps 	if (h->flags & PCIC_FLAG_ENABLED)
   1422  1.61   mycroft 		printf("pcic_chip_socket_enable: enabling twice\n");
   1423  1.41    chopps #endif
   1424  1.41    chopps 
   1425  1.38    chopps 	/* disable interrupts */
   1426  1.39     enami 	intr = pcic_read(h, PCIC_INTR);
   1427  1.79   mycroft 	intr &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_CARDTYPE_MASK);
   1428  1.34    chopps 	pcic_write(h, PCIC_INTR, intr);
   1429   1.2   thorpej 
   1430  1.82   mycroft 	/* zero out the address windows */
   1431  1.82   mycroft 	pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
   1432  1.82   mycroft 
   1433  1.83   mycroft 	/* disable socket: negate output enable bit and power off */
   1434  1.83   mycroft 	power = 0;
   1435  1.83   mycroft 	pcic_write(h, PCIC_PWRCTL, power);
   1436  1.83   mycroft 
   1437  1.83   mycroft 	/* now make sure we have reset# active */
   1438  1.83   mycroft 	intr &= ~PCIC_INTR_RESET;
   1439  1.83   mycroft 	pcic_write(h, PCIC_INTR, intr);
   1440  1.69  takemura 
   1441  1.69  takemura 	/*
   1442  1.69  takemura 	 * power hack for RICOH RF5C[23]96
   1443  1.69  takemura 	 */
   1444  1.69  takemura 	switch( h->vendor ) {
   1445  1.69  takemura 	case PCIC_VENDOR_RICOH_5C296:
   1446  1.69  takemura 	case PCIC_VENDOR_RICOH_5C396:
   1447  1.76   mycroft 	{
   1448  1.76   mycroft 		int regtmp;
   1449  1.69  takemura 		regtmp = pcic_read(h, PCIC_RICOH_REG_MCR2);
   1450  1.76   mycroft #ifdef RICOH_POWER_HACK
   1451  1.76   mycroft 		regtmp |= PCIC_RICOH_MCR2_VCC_DIRECT;
   1452  1.76   mycroft #else
   1453  1.76   mycroft 		regtmp &= ~(PCIC_RICOH_MCR2_VCC_DIRECT|PCIC_RICOH_MCR2_VCC_SEL_3V);
   1454  1.76   mycroft #endif
   1455  1.69  takemura 		pcic_write(h, PCIC_RICOH_REG_MCR2, regtmp);
   1456  1.76   mycroft 	}
   1457  1.69  takemura 		break;
   1458  1.69  takemura 	default:
   1459  1.69  takemura 		break;
   1460  1.69  takemura 	}
   1461   1.9     enami 
   1462  1.22   mycroft #ifdef VADEM_POWER_HACK
   1463  1.25      haya 	bus_space_write_1(sc->iot, sc->ioh, PCIC_REG_INDEX, 0x0e);
   1464  1.25      haya 	bus_space_write_1(sc->iot, sc->ioh, PCIC_REG_INDEX, 0x37);
   1465  1.22   mycroft 	printf("prcr = %02x\n", pcic_read(h, 0x02));
   1466  1.22   mycroft 	printf("cvsr = %02x\n", pcic_read(h, 0x2f));
   1467  1.22   mycroft 	printf("DANGER WILL ROBINSON!  Changing voltage select!\n");
   1468  1.22   mycroft 	pcic_write(h, 0x2f, pcic_read(h, 0x2f) & ~0x03);
   1469  1.22   mycroft 	printf("cvsr = %02x\n", pcic_read(h, 0x2f));
   1470  1.22   mycroft #endif
   1471  1.83   mycroft 
   1472   1.2   thorpej 	/* power up the socket */
   1473  1.83   mycroft 	power |= PCIC_PWRCTL_PWR_ENABLE | PCIC_PWRCTL_VPP1_VCC;
   1474  1.83   mycroft 	pcic_write(h, PCIC_PWRCTL, power);
   1475   1.9     enami 
   1476   1.9     enami 	/*
   1477   1.9     enami 	 * wait 100ms until power raise (Tpr) and 20ms to become
   1478   1.9     enami 	 * stable (Tsu(Vcc)).
   1479  1.12   msaitoh 	 *
   1480  1.12   msaitoh 	 * some machines require some more time to be settled
   1481  1.20   msaitoh 	 * (300ms is added here).
   1482   1.9     enami 	 */
   1483  1.30     enami 	pcic_delay(h, 100 + 20 + 300, "pccen1");
   1484  1.38    chopps 
   1485  1.83   mycroft 	power |= PCIC_PWRCTL_OE;
   1486  1.83   mycroft 	pcic_write(h, PCIC_PWRCTL, power);
   1487   1.9     enami 
   1488   1.9     enami 	/*
   1489  1.38    chopps 	 * hold RESET at least 10us, this is a min allow for slop in
   1490  1.38    chopps 	 * delay routine.
   1491   1.9     enami 	 */
   1492  1.38    chopps 	delay(20);
   1493   1.9     enami 
   1494   1.2   thorpej 	/* clear the reset flag */
   1495  1.34    chopps 	intr |= PCIC_INTR_RESET;
   1496  1.34    chopps 	pcic_write(h, PCIC_INTR, intr);
   1497   1.2   thorpej 
   1498   1.2   thorpej 	/* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
   1499  1.30     enami 	pcic_delay(h, 20, "pccen2");
   1500   1.2   thorpej 
   1501  1.83   mycroft #ifdef DIAGNOSTIC
   1502  1.68    simonb 	reg = pcic_read(h, PCIC_IF_STATUS);
   1503  1.83   mycroft 	if ((reg & PCIC_IF_STATUS_POWERACTIVE) == 0)
   1504  1.83   mycroft 		printf("pcic_chip_socket_enable: no power, status=%x\n", reg);
   1505  1.68    simonb #endif
   1506  1.83   mycroft 
   1507  1.83   mycroft 	/* wait for the chip to finish initializing */
   1508  1.83   mycroft 	if (pcic_wait_ready(h)) {
   1509  1.83   mycroft 		/* XXX return a failure status?? */
   1510  1.83   mycroft 		pcic_write(h, PCIC_PWRCTL, 0);
   1511  1.83   mycroft 		return;
   1512  1.20   msaitoh 	}
   1513   1.2   thorpej 
   1514   1.2   thorpej 	/* reinstall all the memory and io mappings */
   1515   1.2   thorpej 	for (win = 0; win < PCIC_MEM_WINS; win++)
   1516   1.2   thorpej 		if (h->memalloc & (1 << win))
   1517   1.2   thorpej 			pcic_chip_do_mem_map(h, win);
   1518   1.2   thorpej 	for (win = 0; win < PCIC_IO_WINS; win++)
   1519   1.2   thorpej 		if (h->ioalloc & (1 << win))
   1520   1.2   thorpej 			pcic_chip_do_io_map(h, win);
   1521  1.34    chopps 
   1522  1.41    chopps 	h->flags |= PCIC_FLAG_ENABLED;
   1523   1.2   thorpej }
   1524   1.2   thorpej 
   1525   1.2   thorpej void
   1526   1.2   thorpej pcic_chip_socket_disable(pch)
   1527   1.2   thorpej 	pcmcia_chipset_handle_t pch;
   1528   1.2   thorpej {
   1529   1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1530  1.83   mycroft 	u_int8_t intr;
   1531   1.2   thorpej 
   1532   1.2   thorpej 	DPRINTF(("pcic_chip_socket_disable\n"));
   1533  1.38    chopps 
   1534  1.38    chopps 	/* disable interrupts */
   1535  1.39     enami 	intr = pcic_read(h, PCIC_INTR);
   1536  1.81   mycroft 	intr &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_CARDTYPE_MASK);
   1537  1.38    chopps 	pcic_write(h, PCIC_INTR, intr);
   1538   1.2   thorpej 
   1539  1.81   mycroft 	/* zero out the address windows */
   1540  1.81   mycroft 	pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
   1541  1.81   mycroft 
   1542  1.83   mycroft 	/* disable socket: negate output enable bit and power off */
   1543   1.2   thorpej 	pcic_write(h, PCIC_PWRCTL, 0);
   1544  1.52   mycroft 
   1545  1.83   mycroft 	/* wait 300ms for power to fall */
   1546  1.83   mycroft 	pcic_delay(h, 300, "pccwr1");
   1547  1.83   mycroft 
   1548  1.41    chopps 	h->flags &= ~PCIC_FLAG_ENABLED;
   1549  1.25      haya }
   1550  1.25      haya 
   1551  1.80   mycroft void
   1552  1.80   mycroft pcic_chip_socket_settype(pch, type)
   1553  1.80   mycroft 	pcmcia_chipset_handle_t pch;
   1554  1.80   mycroft 	int type;
   1555  1.80   mycroft {
   1556  1.80   mycroft 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1557  1.80   mycroft 	int intr;
   1558  1.80   mycroft 
   1559  1.80   mycroft 	intr = pcic_read(h, PCIC_INTR);
   1560  1.81   mycroft 	intr &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_CARDTYPE_MASK);
   1561  1.80   mycroft 	if (type == PCMCIA_IFTYPE_IO) {
   1562  1.80   mycroft 		intr |= PCIC_INTR_CARDTYPE_IO;
   1563  1.80   mycroft 		intr |= h->ih_irq << PCIC_INTR_IRQ_SHIFT;
   1564  1.80   mycroft 	} else
   1565  1.80   mycroft 		intr |= PCIC_INTR_CARDTYPE_MEM;
   1566  1.80   mycroft 	pcic_write(h, PCIC_INTR, intr);
   1567  1.80   mycroft 
   1568  1.80   mycroft 	DPRINTF(("%s: pcic_chip_socket_settype %02x type %s %02x\n",
   1569  1.80   mycroft 	    h->ph_parent->dv_xname, h->sock,
   1570  1.80   mycroft 	    ((type == PCMCIA_IFTYPE_IO) ? "io" : "mem"), intr));
   1571  1.80   mycroft }
   1572  1.80   mycroft 
   1573  1.25      haya static u_int8_t
   1574  1.25      haya st_pcic_read(h, idx)
   1575  1.27  sommerfe 	struct pcic_handle *h;
   1576  1.27  sommerfe 	int idx;
   1577  1.25      haya {
   1578  1.35     enami 
   1579  1.27  sommerfe 	if (idx != -1)
   1580  1.27  sommerfe 		bus_space_write_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_INDEX,
   1581  1.27  sommerfe 		    h->sock + idx);
   1582  1.35     enami 	return (bus_space_read_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_DATA));
   1583  1.25      haya }
   1584  1.25      haya 
   1585  1.25      haya static void
   1586  1.25      haya st_pcic_write(h, idx, data)
   1587  1.27  sommerfe 	struct pcic_handle *h;
   1588  1.27  sommerfe 	int idx;
   1589  1.27  sommerfe 	u_int8_t data;
   1590  1.27  sommerfe {
   1591  1.35     enami 
   1592  1.27  sommerfe 	if (idx != -1)
   1593  1.27  sommerfe 		bus_space_write_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_INDEX,
   1594  1.27  sommerfe 		    h->sock + idx);
   1595  1.27  sommerfe 	bus_space_write_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_DATA, data);
   1596   1.2   thorpej }
   1597