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