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