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