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