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