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