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