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