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