Home | History | Annotate | Line # | Download | only in ic
tcic2.c revision 1.1.4.1
      1 /*	$NetBSD: tcic2.c,v 1.1.4.1 1999/08/02 21:59:06 thorpej Exp $	*/
      2 
      3 #undef	TCICDEBUG
      4 
      5 /*
      6  * Copyright (c) 1998, 1999 Christoph Badura.  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/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/tcic2reg.h>
     52 #include <dev/ic/tcic2var.h>
     53 
     54 #include "locators.h"
     55 
     56 #ifdef TCICDEBUG
     57 int	tcic_debug = 1;
     58 #define	DPRINTF(arg) if (tcic_debug) printf arg;
     59 #else
     60 #define	DPRINTF(arg)
     61 #endif
     62 
     63 /*
     64  * Individual drivers will allocate their own memory and io regions. Memory
     65  * regions must be a multiple of 4k, aligned on a 4k boundary.
     66  */
     67 
     68 #define	TCIC_MEM_ALIGN	TCIC_MEM_PAGESIZE
     69 
     70 void	tcic_attach_socket __P((struct tcic_handle *));
     71 void	tcic_init_socket __P((struct tcic_handle *));
     72 
     73 int	tcic_submatch __P((struct device *, struct cfdata *, void *));
     74 int	tcic_print  __P((void *arg, const char *pnp));
     75 int	tcic_intr_socket __P((struct tcic_handle *));
     76 
     77 void	tcic_attach_card __P((struct tcic_handle *));
     78 void	tcic_detach_card __P((struct tcic_handle *, int));
     79 void	tcic_deactivate_card __P((struct tcic_handle *));
     80 
     81 void	tcic_chip_do_mem_map __P((struct tcic_handle *, int));
     82 void	tcic_chip_do_io_map __P((struct tcic_handle *, int));
     83 
     84 void	tcic_create_event_thread __P((void *));
     85 void	tcic_event_thread __P((void *));
     86 
     87 void	tcic_queue_event __P((struct tcic_handle *, int));
     88 
     89 /* Map between irq numbers and internal representation */
     90 #if 1
     91 int tcic_irqmap[] =
     92     { 0, 0, 0, 3, 4, 5, 6, 7, 0, 0, 10, 1, 0, 0, 14, 0 };
     93 int tcic_valid_irqs = 0x4cf8;
     94 #else
     95 int tcic_irqmap[] =	/* irqs 9 and 6 switched, some ISA cards */
     96     { 0, 0, 0, 3, 4, 5, 0, 7, 0, 6, 10, 1, 0, 0, 14, 0 };
     97 int tcic_valid_irqs = 0x4eb8;
     98 #endif
     99 
    100 int tcic_mem_speed = 250;	/* memory access time in nanoseconds */
    101 int tcic_io_speed = 165;	/* io access time in nanoseconds */
    102 
    103 /*
    104  * Check various reserved and otherwise in their value restricted bits.
    105  */
    106 int
    107 tcic_check_reserved_bits(iot, ioh)
    108 	bus_space_tag_t iot;
    109 	bus_space_handle_t ioh;
    110 {
    111 	int val, auxreg;
    112 
    113 	DPRINTF(("tcic: chkrsvd 1\n"));
    114 	/* R_ADDR bit 30:28 have a restricted range. */
    115 	val = (bus_space_read_2(iot, ioh, TCIC_R_ADDR2) & TCIC_SS_MASK)
    116 	    >> TCIC_SS_SHIFT;
    117 	if (val > 1)
    118 		return 0;
    119 
    120 	DPRINTF(("tcic: chkrsvd 2\n"));
    121 	/* R_SCTRL bits 6,2,1 are reserved. */
    122 	val = bus_space_read_1(iot, ioh, TCIC_R_SCTRL);
    123 	if (val & TCIC_SCTRL_RSVD)
    124 		return 0;
    125 
    126 	DPRINTF(("tcic: chkrsvd 3\n"));
    127 	/* R_ICSR bit 2 must be same as bit 3. */
    128 	val = bus_space_read_1(iot, ioh, TCIC_R_ICSR);
    129 	if (((val >> 1) & 1) != ((val >> 2) & 1))
    130 		return 0;
    131 
    132 	DPRINTF(("tcic: chkrsvd 4\n"));
    133 	/* R_IENA bits 7,2 are reserverd. */
    134 	val = bus_space_read_1(iot, ioh, TCIC_R_IENA);
    135 	if (val & TCIC_IENA_RSVD)
    136 		return 0;
    137 
    138 	DPRINTF(("tcic: chkrsvd 5\n"));
    139 	/* Some aux registers have reserved bits. */
    140 	/* Which are we looking at? */
    141 	auxreg = bus_space_read_1(iot, ioh, TCIC_R_MODE)
    142 	    & TCIC_AR_MASK;
    143 	val = bus_space_read_2(iot, ioh, TCIC_R_AUX);
    144 	DPRINTF(("tcic: auxreg 0x%02x val 0x%04x\n", auxreg, val));
    145 	switch (auxreg) {
    146 	case TCIC_AR_SYSCFG:
    147 		if (INVALID_AR_SYSCFG(val))
    148 			return 0;
    149 		break;
    150 	case TCIC_AR_ILOCK:
    151 		if (INVALID_AR_ILOCK(val))
    152 			return 0;
    153 		break;
    154 	case TCIC_AR_TEST:
    155 		if (INVALID_AR_TEST(val))
    156 			return 0;
    157 		break;
    158 	}
    159 
    160 	DPRINTF(("tcic: chkrsvd 6\n"));
    161 	/* XXX fails if pcmcia bios is enabled. */
    162 	/* Various bits set or not depending if in RESET mode. */
    163 	val = bus_space_read_1(iot, ioh, TCIC_R_SCTRL);
    164 	if (val & TCIC_SCTRL_RESET) {
    165 		DPRINTF(("tcic: chkrsvd 7\n"));
    166 		/* Address bits must be 0 */
    167 		val = bus_space_read_2(iot, ioh, TCIC_R_ADDR);
    168 		if (val != 0)
    169 			return 0;
    170 		val = bus_space_read_2(iot, ioh, TCIC_R_ADDR2);
    171 		if (val != 0)
    172 			return 0;
    173 		DPRINTF(("tcic: chkrsvd 8\n"));
    174 		/* EDC bits must be 0 */
    175 		val = bus_space_read_2(iot, ioh, TCIC_R_EDC);
    176 		if (val != 0)
    177 			return 0;
    178 		/* We're OK, so take it out of reset. XXX -chb */
    179 		bus_space_write_1(iot, ioh, TCIC_R_SCTRL, 0);
    180 	}
    181 	else {	/* not in RESET mode */
    182 		int omode;
    183 		int val1, val2;
    184 		DPRINTF(("tcic: chkrsvd 9\n"));
    185 		/* Programming timers must have expired. */
    186 		val = bus_space_read_1(iot, ioh, TCIC_R_SSTAT);
    187 		if ((val & (TCIC_SSTAT_6US|TCIC_SSTAT_10US|TCIC_SSTAT_PROGTIME))
    188 		    != (TCIC_SSTAT_6US|TCIC_SSTAT_10US|TCIC_SSTAT_PROGTIME))
    189 			return 0;
    190 		DPRINTF(("tcic: chkrsvd 10\n"));
    191 		/*
    192 		 * EDC bits should change on read from data space
    193 		 * as long as either EDC or the data are nonzero.
    194 		 */
    195 		 if ((bus_space_read_2(iot, ioh, TCIC_R_ADDR2)
    196 		     & TCIC_ADDR2_INDREG) != 0) {
    197 			val1 = bus_space_read_2(iot, ioh, TCIC_R_EDC);
    198 			val2 = bus_space_read_2(iot, ioh, TCIC_R_DATA);
    199 			if (val1 | val2) {
    200 				val1 = bus_space_read_2(iot, ioh, TCIC_R_EDC);
    201 				if (val1 == val2)
    202 					return 0;
    203 			}
    204 		}
    205 		DPRINTF(("tcic: chkrsvd 11\n"));
    206 		/* XXX what does this check? -chb */
    207 		omode = bus_space_read_1(iot, ioh, TCIC_R_MODE);
    208 		val1 = omode ^ TCIC_AR_MASK;
    209 		bus_space_write_1(iot, ioh, TCIC_R_MODE, val1);
    210 		val2 = bus_space_read_1(iot, ioh, TCIC_R_MODE);
    211 		bus_space_write_1(iot, ioh, TCIC_R_MODE, omode);
    212 		if ( val1 != val2)
    213 			return 0;
    214 	}
    215 	/* All tests passed */
    216 	return 1;
    217 }
    218 
    219 /*
    220  * Read chip ID from AR_ILOCK in test mode.
    221  */
    222 int
    223 tcic_chipid(iot, ioh)
    224 	bus_space_tag_t iot;
    225 	bus_space_handle_t ioh;
    226 {
    227 	unsigned id, otest;
    228 
    229 	otest = tcic_read_aux_2(iot, ioh, TCIC_AR_TEST);
    230 	tcic_write_aux_2(iot, ioh, TCIC_AR_TEST, TCIC_TEST_DIAG);
    231 	id = tcic_read_aux_2(iot, ioh, TCIC_AR_ILOCK);
    232 	tcic_write_aux_2(iot, ioh, TCIC_AR_TEST, otest);
    233 	id &= TCIC_ILOCKTEST_ID_MASK;
    234 	id >>= TCIC_ILOCKTEST_ID_SHFT;
    235 
    236 	/* clear up IRQs inside tcic. XXX -chb */
    237 	while (bus_space_read_1(iot, ioh, TCIC_R_ICSR))
    238 		bus_space_write_1(iot, ioh, TCIC_R_ICSR, TCIC_ICSR_JAM);
    239 
    240 	return id;
    241 }
    242 /*
    243  * Indicate whether the driver can handle the chip.
    244  */
    245 int
    246 tcic_chipid_known(id)
    247 	int id;
    248 {
    249 	/* XXX only know how to handle DB86082 -chb */
    250 	switch (id) {
    251 	case TCIC_CHIPID_DB86082_1:
    252 	case TCIC_CHIPID_DB86082A:
    253 	case TCIC_CHIPID_DB86082B_ES:
    254 	case TCIC_CHIPID_DB86082B:
    255 	case TCIC_CHIPID_DB86084_1:
    256 	case TCIC_CHIPID_DB86084A:
    257 	case TCIC_CHIPID_DB86184_1:
    258 	case TCIC_CHIPID_DB86072_1_ES:
    259 	case TCIC_CHIPID_DB86072_1:
    260 		return 1;
    261 	}
    262 
    263 	return 0;
    264 }
    265 
    266 char *
    267 tcic_chipid_to_string(id)
    268 	int id;
    269 {
    270 	switch (id) {
    271 	case TCIC_CHIPID_DB86082_1:
    272 		return ("Databook DB86082");
    273 	case TCIC_CHIPID_DB86082A:
    274 		return ("Databook DB86082A");
    275 	case TCIC_CHIPID_DB86082B_ES:
    276 		return ("Databook DB86082B-es");
    277 	case TCIC_CHIPID_DB86082B:
    278 		return ("Databook DB86082B");
    279 	case TCIC_CHIPID_DB86084_1:
    280 		return ("Databook DB86084");
    281 	case TCIC_CHIPID_DB86084A:
    282 		return ("Databook DB86084A");
    283 	case TCIC_CHIPID_DB86184_1:
    284 		return ("Databook DB86184");
    285 	case TCIC_CHIPID_DB86072_1_ES:
    286 		return ("Databook DB86072-es");
    287 	case TCIC_CHIPID_DB86072_1:
    288 		return ("Databook DB86072");
    289 	}
    290 
    291 	return ("Unknown controller");
    292 }
    293 /*
    294  * Return bitmask of IRQs that the chip can handle.
    295  * XXX should be table driven.
    296  */
    297 int
    298 tcic_validirqs(chipid)
    299 	int chipid;
    300 {
    301 	switch (chipid) {
    302 	case TCIC_CHIPID_DB86082_1:
    303 	case TCIC_CHIPID_DB86082A:
    304 	case TCIC_CHIPID_DB86082B_ES:
    305 	case TCIC_CHIPID_DB86082B:
    306 	case TCIC_CHIPID_DB86084_1:
    307 	case TCIC_CHIPID_DB86084A:
    308 	case TCIC_CHIPID_DB86184_1:
    309 	case TCIC_CHIPID_DB86072_1_ES:
    310 	case TCIC_CHIPID_DB86072_1:
    311 		return tcic_valid_irqs;
    312 	}
    313 	return 0;
    314 }
    315 
    316 void
    317 tcic_attach(sc)
    318 	struct tcic_softc *sc;
    319 {
    320 	int i, reg;
    321 
    322 	/* set more chipset dependend parameters in the softc. */
    323 	switch (sc->chipid) {
    324 	case TCIC_CHIPID_DB86084_1:
    325 	case TCIC_CHIPID_DB86084A:
    326 	case TCIC_CHIPID_DB86184_1:
    327 		sc->pwrena = TCIC_PWR_ENA;
    328 		break;
    329 	default:
    330 		sc->pwrena = 0;
    331 		break;
    332 	}
    333 
    334 	/* set up global config registers */
    335 	reg = TCIC_WAIT_SYNC | TCIC_WAIT_CCLK | TCIC_WAIT_RISING;
    336 	reg |= (tcic_ns2wscnt(250) & TCIC_WAIT_COUNT_MASK);
    337 	tcic_write_aux_1(sc->iot, sc->ioh, TCIC_AR_WCTL, TCIC_R_WCTL_WAIT, reg);
    338 	reg = TCIC_SYSCFG_MPSEL_RI | TCIC_SYSCFG_MCSFULL;
    339 	tcic_write_aux_2(sc->iot, sc->ioh, TCIC_AR_SYSCFG, reg);
    340 	reg = tcic_read_aux_2(sc->iot, sc->ioh, TCIC_AR_ILOCK);
    341 	reg |= TCIC_ILOCK_HOLD_CCLK;
    342 	tcic_write_aux_2(sc->iot, sc->ioh, TCIC_AR_ILOCK, reg);
    343 
    344 	/* the TCIC has two sockets */
    345 	/* XXX should i check for actual presence of sockets? -chb */
    346 	for (i = 0; i < TCIC_NSLOTS; i++) {
    347 		sc->handle[i].sc = sc;
    348 		sc->handle[i].sock = i;
    349 		sc->handle[i].flags = TCIC_FLAG_SOCKETP;
    350 		sc->handle[i].memwins
    351 		    = sc->chipid == TCIC_CHIPID_DB86082_1 ?  4 : 5;
    352 	}
    353 
    354 	/* establish the interrupt */
    355 	reg = tcic_read_1(&sc->handle[0], TCIC_R_IENA);
    356 	tcic_write_1(&sc->handle[0], TCIC_R_IENA,
    357 	    (reg & ~TCIC_IENA_CFG_MASK) | TCIC_IENA_CFG_HIGH);
    358 	reg = tcic_read_aux_2(sc->iot, sc->ioh, TCIC_AR_SYSCFG);
    359 	tcic_write_aux_2(sc->iot, sc->ioh, TCIC_AR_SYSCFG,
    360 	    (reg & ~TCIC_SYSCFG_IRQ_MASK) | tcic_irqmap[sc->irq]);
    361 
    362 	/* XXX block interrupts? */
    363 
    364 	for (i = 0; i < TCIC_NSLOTS; i++) {
    365 		/* XXX make more clear what happens here -chb */
    366 		tcic_sel_sock(&sc->handle[i]);
    367 		tcic_write_ind_2(&sc->handle[i], TCIC_IR_SCF1_N(i), 0);
    368 		tcic_write_ind_2(&sc->handle[i], TCIC_IR_SCF2_N(i),
    369 		    (TCIC_SCF2_MCD|TCIC_SCF2_MWP|TCIC_SCF2_MRDY
    370 #if 1		/* XXX explain byte routing issue */
    371 		    |TCIC_SCF2_MLBAT2|TCIC_SCF2_MLBAT1|TCIC_SCF2_IDBR));
    372 #else
    373 		    |TCIC_SCF2_MLBAT2|TCIC_SCF2_MLBAT1));
    374 #endif
    375 		tcic_write_1(&sc->handle[i], TCIC_R_MODE, 0);
    376 		reg = tcic_read_aux_2(sc->iot, sc->ioh, TCIC_AR_SYSCFG);
    377 		reg &= ~TCIC_SYSCFG_AUTOBUSY;
    378 		tcic_write_aux_2(sc->iot, sc->ioh, TCIC_AR_SYSCFG, reg);
    379 		SIMPLEQ_INIT(&sc->handle[i].events);
    380 	}
    381 
    382 	if ((sc->handle[0].flags & TCIC_FLAG_SOCKETP) ||
    383 	    (sc->handle[1].flags & TCIC_FLAG_SOCKETP)) {
    384 		printf("%s: %s has ", sc->dev.dv_xname,
    385 		       tcic_chipid_to_string(sc->chipid));
    386 
    387 		if ((sc->handle[0].flags & TCIC_FLAG_SOCKETP) &&
    388 		    (sc->handle[1].flags & TCIC_FLAG_SOCKETP))
    389 			printf("sockets A and B\n");
    390 		else if (sc->handle[0].flags & TCIC_FLAG_SOCKETP)
    391 			printf("socket A only\n");
    392 		else
    393 			printf("socket B only\n");
    394 
    395 	}
    396 }
    397 
    398 void
    399 tcic_attach_sockets(sc)
    400 	struct tcic_softc *sc;
    401 {
    402 	int i;
    403 
    404 	for (i = 0; i < TCIC_NSLOTS; i++)
    405 		if (sc->handle[i].flags & TCIC_FLAG_SOCKETP)
    406 			tcic_attach_socket(&sc->handle[i]);
    407 }
    408 
    409 void
    410 tcic_attach_socket(h)
    411 	struct tcic_handle *h;
    412 {
    413 	struct pcmciabus_attach_args paa;
    414 
    415 	/* initialize the rest of the handle */
    416 
    417 	h->shutdown = 0;
    418 	h->memalloc = 0;
    419 	h->ioalloc = 0;
    420 	h->ih_irq = 0;
    421 
    422 	/* now, config one pcmcia device per socket */
    423 
    424 	paa.pct = (pcmcia_chipset_tag_t) h->sc->pct;
    425 	paa.pch = (pcmcia_chipset_handle_t) h;
    426 	paa.iobase = h->sc->iobase;
    427 	paa.iosize = h->sc->iosize;
    428 
    429 	h->pcmcia = config_found_sm(&h->sc->dev, &paa, tcic_print,
    430 	    tcic_submatch);
    431 
    432 	/* if there's actually a pcmcia device attached, initialize the slot */
    433 
    434 	if (h->pcmcia)
    435 		tcic_init_socket(h);
    436 }
    437 
    438 void
    439 tcic_create_event_thread(arg)
    440 	void *arg;
    441 {
    442 	struct tcic_handle *h = arg;
    443 	const char *cs;
    444 
    445 	switch (h->sock) {
    446 	case 0:
    447 		cs = "0";
    448 		break;
    449 	case 1:
    450 		cs = "1";
    451 		break;
    452 	default:
    453 		panic("tcic_create_event_thread: unknown tcic socket");
    454 	}
    455 
    456 	if (kthread_create1(tcic_event_thread, h, &h->event_thread,
    457 	    "%s,%s", h->sc->dev.dv_xname, cs)) {
    458 		printf("%s: unable to create event thread for sock 0x%02x\n",
    459 		    h->sc->dev.dv_xname, h->sock);
    460 		panic("tcic_create_event_thread");
    461 	}
    462 }
    463 
    464 void
    465 tcic_event_thread(arg)
    466 	void *arg;
    467 {
    468 	struct tcic_handle *h = arg;
    469 	struct tcic_event *pe;
    470 	int s;
    471 
    472 	while (h->shutdown == 0) {
    473 		s = splhigh();
    474 		if ((pe = SIMPLEQ_FIRST(&h->events)) == NULL) {
    475 			splx(s);
    476 			(void) tsleep(&h->events, PWAIT, "tcicev", 0);
    477 			continue;
    478 		}
    479 		SIMPLEQ_REMOVE_HEAD(&h->events, pe, pe_q);
    480 		splx(s);
    481 
    482 		switch (pe->pe_type) {
    483 		case TCIC_EVENT_INSERTION:
    484 			DPRINTF(("%s: insertion event\n", h->sc->dev.dv_xname));
    485 			tcic_attach_card(h);
    486 			break;
    487 
    488 		case TCIC_EVENT_REMOVAL:
    489 			DPRINTF(("%s: removal event\n", h->sc->dev.dv_xname));
    490 			tcic_detach_card(h, DETACH_FORCE);
    491 			break;
    492 
    493 		default:
    494 			panic("tcic_event_thread: unknown event %d",
    495 			    pe->pe_type);
    496 		}
    497 		free(pe, M_TEMP);
    498 	}
    499 
    500 	h->event_thread = NULL;
    501 
    502 	/* In case parent is waiting for us to exit. */
    503 	wakeup(h->sc);
    504 
    505 	kthread_exit(0);
    506 }
    507 
    508 
    509 void
    510 tcic_init_socket(h)
    511 	struct tcic_handle *h;
    512 {
    513 	int reg;
    514 
    515 	/* select this socket's config registers */
    516 	tcic_sel_sock(h);
    517 
    518 	/* set up the socket to interrupt on card detect */
    519 	reg = tcic_read_ind_2(h, TCIC_IR_SCF2_N(h->sock));
    520 	tcic_write_ind_2(h, TCIC_IR_SCF2_N(h->sock), reg & ~TCIC_SCF2_MCD);
    521 
    522 	/* enable CD irq in R_IENA */
    523 	reg = tcic_read_2(h, TCIC_R_IENA);
    524 	tcic_write_2(h, TCIC_R_IENA, reg |= TCIC_IENA_CDCHG);
    525 
    526 	/* if there's a card there, then attach it. also save sstat */
    527 	h->sstat = reg = tcic_read_1(h, TCIC_R_SSTAT) & TCIC_SSTAT_STAT_MASK;
    528 	if (reg & TCIC_SSTAT_CD)
    529 		tcic_attach_card(h);
    530 }
    531 
    532 int
    533 #ifdef __BROKEN_INDIRECT_CONFIG
    534 tcic_submatch(parent, match, aux)
    535 #else
    536 tcic_submatch(parent, cf, aux)
    537 #endif
    538 	struct device *parent;
    539 #ifdef __BROKEN_INDIRECT_CONFIG
    540 	void *match;
    541 #else
    542 	struct cfdata *cf;
    543 #endif
    544 	void *aux;
    545 {
    546 #ifdef __BROKEN_INDIRECT_CONFIG
    547 	struct cfdata *cf = match;
    548 #endif
    549 
    550 	struct pcmciabus_attach_args *paa = aux;
    551 	struct tcic_handle *h = (struct tcic_handle *) paa->pch;
    552 
    553 	switch (h->sock) {
    554 	case 0:
    555 		if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] !=
    556 		    PCMCIABUSCF_CONTROLLER_DEFAULT &&
    557 		    cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 0)
    558 			return 0;
    559 		if (cf->cf_loc[PCMCIABUSCF_SOCKET] !=
    560 		    PCMCIABUSCF_SOCKET_DEFAULT &&
    561 		    cf->cf_loc[PCMCIABUSCF_SOCKET] != 0)
    562 			return 0;
    563 
    564 		break;
    565 	case 1:
    566 		if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] !=
    567 		    PCMCIABUSCF_CONTROLLER_DEFAULT &&
    568 		    cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 0)
    569 			return 0;
    570 		if (cf->cf_loc[PCMCIABUSCF_SOCKET] !=
    571 		    PCMCIABUSCF_SOCKET_DEFAULT &&
    572 		    cf->cf_loc[PCMCIABUSCF_SOCKET] != 1)
    573 			return 0;
    574 
    575 		break;
    576 	default:
    577 		panic("unknown tcic socket");
    578 	}
    579 
    580 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    581 }
    582 
    583 int
    584 tcic_print(arg, pnp)
    585 	void *arg;
    586 	const char *pnp;
    587 {
    588 	struct pcmciabus_attach_args *paa = arg;
    589 	struct tcic_handle *h = (struct tcic_handle *) paa->pch;
    590 
    591 	/* Only "pcmcia"s can attach to "tcic"s... easy. */
    592 	if (pnp)
    593 		printf("pcmcia at %s", pnp);
    594 
    595 	switch (h->sock) {
    596 	case 0:
    597 		printf(" socket 0");
    598 		break;
    599 	case 1:
    600 		printf(" socket 1");
    601 		break;
    602 	default:
    603 		panic("unknown tcic socket");
    604 	}
    605 	return (UNCONF);
    606 }
    607 
    608 int
    609 tcic_intr(arg)
    610 	void *arg;
    611 {
    612 	struct tcic_softc *sc = arg;
    613 	int i, ret = 0;
    614 
    615 	DPRINTF(("%s: intr\n", sc->dev.dv_xname));
    616 
    617 	for (i = 0; i < TCIC_NSLOTS; i++)
    618 		if (sc->handle[i].flags & TCIC_FLAG_SOCKETP)
    619 			ret += tcic_intr_socket(&sc->handle[i]);
    620 
    621 	return (ret ? 1 : 0);
    622 }
    623 
    624 int
    625 tcic_intr_socket(h)
    626 	struct tcic_handle *h;
    627 {
    628 	int icsr, rv;
    629 
    630 	rv = 0;
    631 	tcic_sel_sock(h);
    632 	icsr = tcic_read_1(h, TCIC_R_ICSR);
    633 
    634 	DPRINTF(("%s: %d icsr: 0x%02x \n", h->sc->dev.dv_xname, h->sock, icsr));
    635 
    636 	/* XXX or should the next three be handled in tcic_intr? -chb */
    637 	if (icsr & TCIC_ICSR_PROGTIME) {
    638 		DPRINTF(("%s: %02x PROGTIME\n", h->sc->dev.dv_xname, h->sock));
    639 		rv = 1;
    640 	}
    641 	if (icsr & TCIC_ICSR_ILOCK) {
    642 		DPRINTF(("%s: %02x ILOCK\n", h->sc->dev.dv_xname, h->sock));
    643 		rv = 1;
    644 	}
    645 	if (icsr & TCIC_ICSR_ERR) {
    646 		DPRINTF(("%s: %02x ERR\n", h->sc->dev.dv_xname, h->sock));
    647 		rv = 1;
    648 	}
    649 	if (icsr & TCIC_ICSR_CDCHG) {
    650 		int sstat, delta;
    651 
    652 		/* compute what changed since last interrupt */
    653 		sstat = tcic_read_aux_1(h->sc->iot, h->sc->ioh,
    654 		    TCIC_AR_WCTL, TCIC_R_WCTL_XCSR) & TCIC_XCSR_STAT_MASK;
    655 		delta = h->sstat ^ sstat;
    656 		h->sstat = sstat;
    657 
    658 		if (delta)
    659 			rv = 1;
    660 
    661 		DPRINTF(("%s: %02x CDCHG %x\n", h->sc->dev.dv_xname, h->sock,
    662 		    delta));
    663 
    664 		/*
    665 		 * XXX This should probably schedule something to happen
    666 		 * after the interrupt handler completes
    667 		 */
    668 
    669 		if (delta & TCIC_SSTAT_CD) {
    670 			if (sstat & TCIC_SSTAT_CD) {
    671 				if (!(h->flags & TCIC_FLAG_CARDP)) {
    672 					DPRINTF(("%s: enqueing INSERTION event\n",
    673 					    h->sc->dev.dv_xname));
    674 					tcic_queue_event(h, TCIC_EVENT_INSERTION);
    675 				}
    676 			} else {
    677 				if (h->flags & TCIC_FLAG_CARDP) {
    678 					/* Deactivate the card now. */
    679 					DPRINTF(("%s: deactivating card\n",
    680 					    h->sc->dev.dv_xname));
    681 					tcic_deactivate_card(h);
    682 
    683 					DPRINTF(("%s: enqueing REMOVAL event\n",
    684 					    h->sc->dev.dv_xname));
    685 					tcic_queue_event(h, TCIC_EVENT_REMOVAL);
    686 				}
    687 			}
    688 		}
    689 		if (delta & TCIC_SSTAT_RDY) {
    690 			DPRINTF(("%s: %02x READY\n", h->sc->dev.dv_xname, h->sock));
    691 			/* shouldn't happen */
    692 		}
    693 		if (delta & TCIC_SSTAT_LBAT1) {
    694 			DPRINTF(("%s: %02x LBAT1\n", h->sc->dev.dv_xname, h->sock));
    695 		}
    696 		if (delta & TCIC_SSTAT_LBAT2) {
    697 			DPRINTF(("%s: %02x LBAT2\n", h->sc->dev.dv_xname, h->sock));
    698 		}
    699 		if (delta & TCIC_SSTAT_WP) {
    700 			DPRINTF(("%s: %02x WP\n", h->sc->dev.dv_xname, h->sock));
    701 		}
    702 	}
    703 	return rv;
    704 }
    705 
    706 void
    707 tcic_queue_event(h, event)
    708 	struct tcic_handle *h;
    709 	int event;
    710 {
    711 	struct tcic_event *pe;
    712 	int s;
    713 
    714 	pe = malloc(sizeof(*pe), M_TEMP, M_NOWAIT);
    715 	if (pe == NULL)
    716 		panic("tcic_queue_event: can't allocate event");
    717 
    718 	pe->pe_type = event;
    719 	s = splhigh();
    720 	SIMPLEQ_INSERT_TAIL(&h->events, pe, pe_q);
    721 	splx(s);
    722 	wakeup(&h->events);
    723 }
    724 void
    725 tcic_attach_card(h)
    726 	struct tcic_handle *h;
    727 {
    728 	DPRINTF(("tcic_attach_card\n"));
    729 
    730 	if (h->flags & TCIC_FLAG_CARDP)
    731 		panic("tcic_attach_card: already attached");
    732 
    733 	/* call the MI attach function */
    734 
    735 	pcmcia_card_attach(h->pcmcia);
    736 
    737 	h->flags |= TCIC_FLAG_CARDP;
    738 }
    739 
    740 void
    741 tcic_detach_card(h, flags)
    742 	struct tcic_handle *h;
    743 	int flags;		/* DETACH_* */
    744 {
    745 	DPRINTF(("tcic_detach_card\n"));
    746 
    747 	if (!(h->flags & TCIC_FLAG_CARDP))
    748 		panic("tcic_detach_card: already detached");
    749 
    750 	h->flags &= ~TCIC_FLAG_CARDP;
    751 
    752 	/* call the MI detach function */
    753 
    754 	pcmcia_card_detach(h->pcmcia, flags);
    755 
    756 }
    757 
    758 void
    759 tcic_deactivate_card(h)
    760 	struct tcic_handle *h;
    761 {
    762 	int val, reg;
    763 
    764 	if (!(h->flags & TCIC_FLAG_CARDP))
    765 		 panic("tcic_deactivate_card: already detached");
    766 
    767 	/* call the MI deactivate function */
    768 	pcmcia_card_deactivate(h->pcmcia);
    769 
    770 	tcic_sel_sock(h);
    771 
    772 	/* XXX disable card detect resume and configuration reset??? */
    773 
    774 	/* power down the socket */
    775 	tcic_write_1(h, TCIC_R_PWR, 0);
    776 
    777 	/* reset the card XXX ? -chb */
    778 
    779 	/* turn off irq's for this socket */
    780 	reg = TCIC_IR_SCF1_N(h->sock);
    781 	val = tcic_read_ind_2(h, reg);
    782 	tcic_write_ind_2(h, reg, (val & ~TCIC_SCF1_IRQ_MASK)|TCIC_SCF1_IRQOFF);
    783 	reg = TCIC_IR_SCF2_N(h->sock);
    784 	val = tcic_read_ind_2(h, reg);
    785 	tcic_write_ind_2(h, reg,
    786 	    (val | (TCIC_SCF2_MLBAT1|TCIC_SCF2_MLBAT2|TCIC_SCF2_MRDY
    787 		|TCIC_SCF2_MWP|TCIC_SCF2_MCD)));
    788 }
    789 
    790 /* XXX the following routine may need to be rewritten. -chb */
    791 int
    792 tcic_chip_mem_alloc(pch, size, pcmhp)
    793 	pcmcia_chipset_handle_t pch;
    794 	bus_size_t size;
    795 	struct pcmcia_mem_handle *pcmhp;
    796 {
    797 	struct tcic_handle *h = (struct tcic_handle *) pch;
    798 	bus_space_handle_t memh;
    799 	bus_addr_t addr;
    800 	bus_size_t sizepg;
    801 	int i, mask, mhandle;
    802 
    803 	/* out of sc->memh, allocate as many pages as necessary */
    804 
    805 	/*
    806 	 * The TCIC can map memory only in sizes that are
    807 	 * powers of two, aligned at the natural boundary for the size.
    808 	 */
    809 	i = tcic_log2((u_int)size);
    810 	if ((1<<i) < size)
    811 		i++;
    812 	sizepg = max(i, TCIC_MEM_SHIFT) - (TCIC_MEM_SHIFT-1);
    813 
    814 	DPRINTF(("tcic_chip_mem_alloc: size %ld sizepg %ld\n", size, sizepg));
    815 
    816 	/* can't allocate that much anyway */
    817 	if (sizepg > TCIC_MEM_PAGES)	/* XXX -chb */
    818 		return 1;
    819 
    820 	mask = (1 << sizepg) - 1;
    821 
    822 	addr = 0;		/* XXX gcc -Wuninitialized */
    823 	mhandle = 0;		/* XXX gcc -Wuninitialized */
    824 
    825 	/* XXX i should be initialised to always lay on boundary. -chb */
    826 	for (i = 0; i < (TCIC_MEM_PAGES + 1 - sizepg); i += sizepg) {
    827 		if ((h->sc->subregionmask & (mask << i)) == (mask << i)) {
    828 			if (bus_space_subregion(h->sc->memt, h->sc->memh,
    829 			    i * TCIC_MEM_PAGESIZE,
    830 			    sizepg * TCIC_MEM_PAGESIZE, &memh))
    831 				return (1);
    832 			mhandle = mask << i;
    833 			addr = h->sc->membase + (i * TCIC_MEM_PAGESIZE);
    834 			h->sc->subregionmask &= ~(mhandle);
    835 			break;
    836 		}
    837 	}
    838 
    839 	if (i == (TCIC_MEM_PAGES + 1 - sizepg))
    840 		return (1);
    841 
    842 	DPRINTF(("tcic_chip_mem_alloc bus addr 0x%lx+0x%lx\n", (u_long) addr,
    843 		 (u_long) size));
    844 
    845 	pcmhp->memt = h->sc->memt;
    846 	pcmhp->memh = memh;
    847 	pcmhp->addr = addr;
    848 	pcmhp->size = size;
    849 	pcmhp->mhandle = mhandle;
    850 	pcmhp->realsize = sizepg * TCIC_MEM_PAGESIZE;
    851 
    852 	return (0);
    853 }
    854 
    855 /* XXX the following routine may need to be rewritten. -chb */
    856 void
    857 tcic_chip_mem_free(pch, pcmhp)
    858 	pcmcia_chipset_handle_t pch;
    859 	struct pcmcia_mem_handle *pcmhp;
    860 {
    861 	struct tcic_handle *h = (struct tcic_handle *) pch;
    862 
    863 	h->sc->subregionmask |= pcmhp->mhandle;
    864 }
    865 
    866 void
    867 tcic_chip_do_mem_map(h, win)
    868 	struct tcic_handle *h;
    869 	int win;
    870 {
    871 	int reg, hwwin, wscnt;
    872 
    873 	DPRINTF(("tcic_chip_do_mem_map window %d: 0x%lx+0x%lx 0x%lx\n",
    874 		win, (u_long)h->mem[win].addr, (u_long)h->mem[win].size,
    875 		(u_long)h->mem[win].offset));
    876 	/*
    877 	 * the even windows are used for socket 0,
    878 	 * the odd ones for socket 1.
    879 	 */
    880 	hwwin = (win << 1) + h->sock;
    881 
    882 	/* the WR_MEXT register is MBZ */
    883 	tcic_write_ind_2(h, TCIC_WR_MEXT_N(hwwin), 0);
    884 
    885 	/* set the host base address and window size */
    886 	if (h->mem[win].size2 <= 1) {
    887 		reg = ((h->mem[win].addr >> TCIC_MEM_SHIFT) &
    888 		    TCIC_MBASE_ADDR_MASK) | TCIC_MBASE_4K;
    889 	} else {
    890 		reg = ((h->mem[win].addr >> TCIC_MEM_SHIFT) &
    891 		    TCIC_MBASE_ADDR_MASK) | (h->mem[win].size2 >> 1);
    892 	}
    893 	tcic_write_ind_2(h, TCIC_WR_MBASE_N(hwwin), reg);
    894 
    895 	/* set the card address and address space */
    896 	reg = 0;
    897 	reg = ((h->mem[win].offset >> TCIC_MEM_SHIFT) & TCIC_MMAP_ADDR_MASK);
    898 	reg |= (h->mem[win].kind & PCMCIA_MEM_ATTR) ? TCIC_MMAP_ATTR : 0;
    899 	DPRINTF(("tcic_chip_do_map_mem window %d(%d) mmap 0x%04x\n",
    900 	    win, hwwin, reg));
    901 	tcic_write_ind_2(h, TCIC_WR_MMAP_N(hwwin), reg);
    902 
    903 	/* set the MCTL register */
    904 	/* must save WSCNT field in case this is a DB86082 rev 0 */
    905 	/* XXX why can't I do the following two in one statement? */
    906 	reg = tcic_read_ind_2(h, TCIC_WR_MCTL_N(hwwin)) & TCIC_MCTL_WSCNT_MASK;
    907 	reg |= TCIC_MCTL_ENA|TCIC_MCTL_QUIET;
    908 	reg |= (h->sock << TCIC_MCTL_SS_SHIFT) & TCIC_MCTL_SS_MASK;
    909 #ifdef notyet	/* XXX must get speed from CIS somehow. -chb */
    910 	wscnt = tcic_ns2wscnt(h->mem[win].speed);
    911 #else
    912 	wscnt = tcic_ns2wscnt(tcic_mem_speed);	/*  300 is "save" default for CIS memory */
    913 #endif
    914 	if (h->sc->chipid == TCIC_CHIPID_DB86082_1) {
    915 		/*
    916 		 * this chip has the wait state count in window
    917 		 * register 7 - hwwin.
    918 		 */
    919 		int reg2;
    920 		reg2 = tcic_read_ind_2(h, TCIC_WR_MCTL_N(7-hwwin));
    921 		reg2 &= ~TCIC_MCTL_WSCNT_MASK;
    922 		reg2 |= wscnt & TCIC_MCTL_WSCNT_MASK;
    923 		tcic_write_ind_2(h, TCIC_WR_MCTL_N(7-hwwin), reg2);
    924 	} else {
    925 		reg |= wscnt & TCIC_MCTL_WSCNT_MASK;
    926 	}
    927 	tcic_write_ind_2(h, TCIC_WR_MCTL_N(hwwin), reg);
    928 
    929 #ifdef TCICDEBUG
    930 	{
    931 		int r1, r2, r3;
    932 
    933 		r1 = tcic_read_ind_2(h, TCIC_WR_MBASE_N(hwwin));
    934 		r2 = tcic_read_ind_2(h, TCIC_WR_MMAP_N(hwwin));
    935 		r3 = tcic_read_ind_2(h, TCIC_WR_MCTL_N(hwwin));
    936 
    937 		DPRINTF(("tcic_chip_do_mem_map window %d(%d): %04x %04x %04x\n",
    938 		    win, hwwin, r1, r2, r3));
    939 	}
    940 #endif
    941 }
    942 
    943 /* XXX needs work */
    944 int
    945 tcic_chip_mem_map(pch, kind, card_addr, size, pcmhp, offsetp, windowp)
    946 	pcmcia_chipset_handle_t pch;
    947 	int kind;
    948 	bus_addr_t card_addr;
    949 	bus_size_t size;
    950 	struct pcmcia_mem_handle *pcmhp;
    951 	bus_addr_t *offsetp;
    952 	int *windowp;
    953 {
    954 	struct tcic_handle *h = (struct tcic_handle *) pch;
    955 	bus_addr_t busaddr;
    956 	long card_offset;
    957 	int i, win;
    958 
    959 	win = -1;
    960 	for (i = 0; i < h->memwins; i++) {
    961 		if ((h->memalloc & (1 << i)) == 0) {
    962 			win = i;
    963 			h->memalloc |= (1 << i);
    964 			break;
    965 		}
    966 	}
    967 
    968 	if (win == -1)
    969 		return (1);
    970 
    971 	*windowp = win;
    972 
    973 	/* XXX this is pretty gross */
    974 
    975 	if (h->sc->memt != pcmhp->memt)
    976 		panic("tcic_chip_mem_map memt is bogus");
    977 
    978 	busaddr = pcmhp->addr;
    979 
    980 	/*
    981 	 * compute the address offset to the pcmcia address space for the
    982 	 * tcic.  this is intentionally signed.  The masks and shifts below
    983 	 * will cause TRT to happen in the tcic registers.  Deal with making
    984 	 * sure the address is aligned, and return the alignment offset.
    985 	 */
    986 
    987 	*offsetp = card_addr % TCIC_MEM_ALIGN;
    988 	card_addr -= *offsetp;
    989 
    990 	DPRINTF(("tcic_chip_mem_map window %d bus %lx+%lx+%lx at card addr "
    991 	    "%lx\n", win, (u_long) busaddr, (u_long) * offsetp, (u_long) size,
    992 	    (u_long) card_addr));
    993 
    994 	/* XXX we can't use size. -chb */
    995 	/*
    996 	 * include the offset in the size, and decrement size by one, since
    997 	 * the hw wants start/stop
    998 	 */
    999 	size += *offsetp - 1;
   1000 
   1001 	card_offset = (((long) card_addr) - ((long) busaddr));
   1002 
   1003 	DPRINTF(("tcic_chip_mem_map window %d card_offset 0x%lx\n",
   1004 	    win, (u_long)card_offset));
   1005 
   1006 	h->mem[win].addr = busaddr;
   1007 	h->mem[win].size = size;
   1008 	h->mem[win].size2 = tcic_log2((u_int)pcmhp->realsize) - TCIC_MEM_SHIFT;
   1009 	h->mem[win].offset = card_offset;
   1010 	h->mem[win].kind = kind;
   1011 
   1012 	tcic_chip_do_mem_map(h, win);
   1013 
   1014 	return (0);
   1015 }
   1016 
   1017 void
   1018 tcic_chip_mem_unmap(pch, window)
   1019 	pcmcia_chipset_handle_t pch;
   1020 	int window;
   1021 {
   1022 	struct tcic_handle *h = (struct tcic_handle *) pch;
   1023 	int reg, hwwin;
   1024 
   1025 	if (window >= h->memwins)
   1026 		panic("tcic_chip_mem_unmap: window out of range");
   1027 
   1028 	hwwin = (window << 1) + h->sock;
   1029 	reg = tcic_read_ind_2(h, TCIC_WR_MCTL_N(hwwin));
   1030 	reg &= ~TCIC_MCTL_ENA;
   1031 	tcic_write_ind_2(h, TCIC_WR_MCTL_N(hwwin), reg);
   1032 
   1033 	h->memalloc &= ~(1 << window);
   1034 }
   1035 
   1036 int
   1037 tcic_chip_io_alloc(pch, start, size, align, pcihp)
   1038 	pcmcia_chipset_handle_t pch;
   1039 	bus_addr_t start;
   1040 	bus_size_t size;
   1041 	bus_size_t align;
   1042 	struct pcmcia_io_handle *pcihp;
   1043 {
   1044 	struct tcic_handle *h = (struct tcic_handle *) pch;
   1045 	bus_space_tag_t iot;
   1046 	bus_space_handle_t ioh;
   1047 	bus_addr_t ioaddr;
   1048 	int size2, flags = 0;
   1049 
   1050 	/*
   1051 	 * Allocate some arbitrary I/O space.
   1052 	 */
   1053 
   1054 	DPRINTF(("tcic_chip_io_alloc req 0x%lx %ld %ld\n",
   1055 	    (u_long) start, (u_long) size, (u_long) align));
   1056 	/*
   1057 	 * The TCIC can map I/O space only in sizes that are
   1058 	 * powers of two, aligned at the natural boundary for the size.
   1059 	 */
   1060 	size2 = tcic_log2((u_int)size);
   1061 	if ((1 << size2) < size)
   1062 		size2++;
   1063 	/* can't allocate that much anyway */
   1064 	if (size2 > 16)	/* XXX 64K -chb */
   1065 		return 1;
   1066 	if (align) {
   1067 		if ((1 << size2) != align)
   1068 			return 1;	/* not suitably  aligned */
   1069 	} else {
   1070 		align = 1 << size2;	/* no alignment given, make it natural */
   1071 	}
   1072 	if (start & (align - 1))
   1073 		return 1;	/* not suitably aligned */
   1074 
   1075 	iot = h->sc->iot;
   1076 
   1077 	if (start) {
   1078 		ioaddr = start;
   1079 		if (bus_space_map(iot, start, size, 0, &ioh))
   1080 			return (1);
   1081 		DPRINTF(("tcic_chip_io_alloc map port %lx+%lx\n",
   1082 		    (u_long) ioaddr, (u_long) size));
   1083 	} else {
   1084 		flags |= PCMCIA_IO_ALLOCATED;
   1085 		if (bus_space_alloc(iot, h->sc->iobase,
   1086 		    h->sc->iobase + h->sc->iosize, size, align, 0, 0,
   1087 		    &ioaddr, &ioh))
   1088 			return (1);
   1089 		DPRINTF(("tcic_chip_io_alloc alloc port %lx+%lx\n",
   1090 		    (u_long) ioaddr, (u_long) size));
   1091 	}
   1092 
   1093 	pcihp->iot = iot;
   1094 	pcihp->ioh = ioh;
   1095 	pcihp->addr = ioaddr;
   1096 	pcihp->size = size;
   1097 	pcihp->flags = flags;
   1098 
   1099 	return (0);
   1100 }
   1101 
   1102 void
   1103 tcic_chip_io_free(pch, pcihp)
   1104 	pcmcia_chipset_handle_t pch;
   1105 	struct pcmcia_io_handle *pcihp;
   1106 {
   1107 	bus_space_tag_t iot = pcihp->iot;
   1108 	bus_space_handle_t ioh = pcihp->ioh;
   1109 	bus_size_t size = pcihp->size;
   1110 
   1111 	if (pcihp->flags & PCMCIA_IO_ALLOCATED)
   1112 		bus_space_free(iot, ioh, size);
   1113 	else
   1114 		bus_space_unmap(iot, ioh, size);
   1115 }
   1116 
   1117 static int tcic_iowidth_map[] =
   1118     { TCIC_ICTL_AUTOSZ, TCIC_ICTL_B8, TCIC_ICTL_B16 };
   1119 
   1120 void
   1121 tcic_chip_do_io_map(h, win)
   1122 	struct tcic_handle *h;
   1123 	int win;
   1124 {
   1125 	int reg, size2, iotiny, wbase, hwwin, wscnt;
   1126 
   1127 	DPRINTF(("tcic_chip_do_io_map win %d addr %lx size %lx width %d\n",
   1128 	    win, (long) h->io[win].addr, (long) h->io[win].size,
   1129 	    h->io[win].width * 8));
   1130 
   1131 	/*
   1132 	 * the even windows are used for socket 0,
   1133 	 * the odd ones for socket 1.
   1134 	 */
   1135 	hwwin = (win << 1) + h->sock;
   1136 
   1137 	/* set the WR_BASE register */
   1138 	/* XXX what if size isn't power of 2? -chb */
   1139 	size2 = tcic_log2((u_int)h->io[win].size);
   1140 	DPRINTF(("tcic_chip_do_io_map win %d size2 %d\n", win, size2));
   1141 	if (size2 < 1) {
   1142 		iotiny = TCIC_ICTL_TINY;
   1143 		wbase = h->io[win].addr;
   1144 	} else {
   1145 		iotiny = 0;
   1146 		/* XXX we should do better -chb */
   1147 		wbase = h->io[win].addr | (1 << (size2 - 1));
   1148 	}
   1149 	tcic_write_ind_2(h, TCIC_WR_IBASE_N(hwwin), wbase);
   1150 
   1151 	/* set the WR_ICTL register */
   1152 	reg = TCIC_ICTL_ENA | TCIC_ICTL_QUIET;
   1153 	reg |= (h->sock << TCIC_ICTL_SS_SHIFT) & TCIC_ICTL_SS_MASK;
   1154 	reg |= iotiny | tcic_iowidth_map[h->io[win].width];
   1155 	if (h->sc->chipid != TCIC_CHIPID_DB86082_1)
   1156 		reg |= TCIC_ICTL_PASS16;
   1157 #ifdef notyet	/* XXX must get speed from CIS somehow. -chb */
   1158 	wscnt = tcic_ns2wscnt(h->io[win].speed);
   1159 #else
   1160 	wscnt = tcic_ns2wscnt(tcic_io_speed);	/* linux uses 0 as default */
   1161 #endif
   1162 	reg |= wscnt & TCIC_ICTL_WSCNT_MASK;
   1163 	tcic_write_ind_2(h, TCIC_WR_ICTL_N(hwwin), reg);
   1164 
   1165 #ifdef TCICDEBUG
   1166 	{
   1167 		int r1, r2;
   1168 
   1169 		r1 = tcic_read_ind_2(h, TCIC_WR_IBASE_N(hwwin));
   1170 		r2 = tcic_read_ind_2(h, TCIC_WR_ICTL_N(hwwin));
   1171 
   1172 		DPRINTF(("tcic_chip_do_io_map window %d(%d): %04x %04x\n",
   1173 		    win, hwwin, r1, r2));
   1174 	}
   1175 #endif
   1176 }
   1177 
   1178 int
   1179 tcic_chip_io_map(pch, width, offset, size, pcihp, windowp)
   1180 	pcmcia_chipset_handle_t pch;
   1181 	int width;
   1182 	bus_addr_t offset;
   1183 	bus_size_t size;
   1184 	struct pcmcia_io_handle *pcihp;
   1185 	int *windowp;
   1186 {
   1187 	struct tcic_handle *h = (struct tcic_handle *) pch;
   1188 	bus_addr_t ioaddr = pcihp->addr + offset;
   1189 	int i, win;
   1190 #ifdef TCICDEBUG
   1191 	static char *width_names[] = { "auto", "io8", "io16" };
   1192 #endif
   1193 
   1194 	/* XXX Sanity check offset/size. */
   1195 
   1196 	win = -1;
   1197 	for (i = 0; i < TCIC_IO_WINS; i++) {
   1198 		if ((h->ioalloc & (1 << i)) == 0) {
   1199 			win = i;
   1200 			h->ioalloc |= (1 << i);
   1201 			break;
   1202 		}
   1203 	}
   1204 
   1205 	if (win == -1)
   1206 		return (1);
   1207 
   1208 	*windowp = win;
   1209 
   1210 	/* XXX this is pretty gross */
   1211 
   1212 	if (h->sc->iot != pcihp->iot)
   1213 		panic("tcic_chip_io_map iot is bogus");
   1214 
   1215 	DPRINTF(("tcic_chip_io_map window %d %s port %lx+%lx\n",
   1216 		 win, width_names[width], (u_long) ioaddr, (u_long) size));
   1217 
   1218 	/* XXX wtf is this doing here? */
   1219 
   1220 	printf(" port 0x%lx", (u_long) ioaddr);
   1221 	if (size > 1)
   1222 		printf("-0x%lx", (u_long) ioaddr + (u_long) size - 1);
   1223 
   1224 	h->io[win].addr = ioaddr;
   1225 	h->io[win].size = size;
   1226 	h->io[win].width = width;
   1227 
   1228 	tcic_chip_do_io_map(h, win);
   1229 
   1230 	return (0);
   1231 }
   1232 
   1233 void
   1234 tcic_chip_io_unmap(pch, window)
   1235 	pcmcia_chipset_handle_t pch;
   1236 	int window;
   1237 {
   1238 	struct tcic_handle *h = (struct tcic_handle *) pch;
   1239 	int reg, hwwin;
   1240 
   1241 	if (window >= TCIC_IO_WINS)
   1242 		panic("tcic_chip_io_unmap: window out of range");
   1243 
   1244 	hwwin = (window << 1) + h->sock;
   1245 	reg = tcic_read_ind_2(h, TCIC_WR_ICTL_N(hwwin));
   1246 	reg &= ~TCIC_ICTL_ENA;
   1247 	tcic_write_ind_2(h, TCIC_WR_ICTL_N(hwwin), reg);
   1248 
   1249 	h->ioalloc &= ~(1 << window);
   1250 }
   1251 
   1252 void
   1253 tcic_chip_socket_enable(pch)
   1254 	pcmcia_chipset_handle_t pch;
   1255 {
   1256 	struct tcic_handle *h = (struct tcic_handle *) pch;
   1257 	int cardtype, reg, win;
   1258 
   1259 	tcic_sel_sock(h);
   1260 
   1261 	/*
   1262 	 * power down the socket to reset it.
   1263 	 * put card reset into high-z, put chip outputs to card into high-z
   1264 	 */
   1265 
   1266 	tcic_write_1(h, TCIC_R_PWR, 0);
   1267 	reg = tcic_read_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK);
   1268 	reg |= TCIC_ILOCK_CWAIT;
   1269 	reg &= ~(TCIC_ILOCK_CRESET|TCIC_ILOCK_CRESENA);
   1270 	tcic_write_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK, reg);
   1271 	tcic_write_1(h, TCIC_R_SCTRL, 0);	/* clear TCIC_SCTRL_ENA */
   1272 
   1273 	/* power up the socket */
   1274 
   1275 	/* turn on VCC, turn of VPP */
   1276 	reg = TCIC_PWR_VCC_N(h->sock) | TCIC_PWR_VPP_N(h->sock) | h->sc->pwrena;
   1277 	if (h->sc->pwrena)		/* this is a '84 type chip */
   1278 		reg |= TCIC_PWR_VCC5V;
   1279 	tcic_write_1(h, TCIC_R_PWR, reg);
   1280 	delay(10000);
   1281 
   1282 	/* enable reset and wiggle it to reset the card */
   1283 	reg = tcic_read_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK);
   1284 	reg |= TCIC_ILOCK_CRESENA;
   1285 	tcic_write_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK, reg);
   1286 	/* XXX need bus_space_barrier here */
   1287 	reg |= TCIC_ILOCK_CRESET;
   1288 	tcic_write_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK, reg);
   1289 	/* enable card signals */
   1290 	tcic_write_1(h, TCIC_R_SCTRL, TCIC_SCTRL_ENA);
   1291 	delay(10);	/* wait 10 us */
   1292 
   1293 	/* clear the reset flag */
   1294 	reg = tcic_read_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK);
   1295 	reg &= ~(TCIC_ILOCK_CRESET);
   1296 	tcic_write_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK, reg);
   1297 
   1298 	/* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
   1299 	delay(20000);
   1300 
   1301 	/* wait for the chip to finish initializing */
   1302 	tcic_wait_ready(h);
   1303 
   1304 	/* WWW */
   1305 	/* zero out the address windows */
   1306 
   1307 	/* writing to WR_MBASE_N disables the window */
   1308 	for (win = 0; win < h->memwins; win++) {
   1309 		tcic_write_ind_2(h, TCIC_WR_MBASE_N((win<<1)+h->sock), 0);
   1310 	}
   1311 	/* writing to WR_IBASE_N disables the window */
   1312 	for (win = 0; win < TCIC_IO_WINS; win++) {
   1313 		tcic_write_ind_2(h, TCIC_WR_IBASE_N((win<<1)+h->sock), 0);
   1314 	}
   1315 
   1316 	/* set the card type */
   1317 
   1318 	cardtype = pcmcia_card_gettype(h->pcmcia);
   1319 
   1320 #if 0
   1321 	reg = tcic_read_ind_2(h, TCIC_IR_SCF1_N(h->sock));
   1322 	reg &= ~TCIC_SCF1_IRQ_MASK;
   1323 #else
   1324 	reg = 0;
   1325 #endif
   1326 	reg |= ((cardtype == PCMCIA_IFTYPE_IO) ?
   1327 		TCIC_SCF1_IOSTS : 0);
   1328 	reg |= tcic_irqmap[h->ih_irq];		/* enable interrupts */
   1329 	reg &= ~TCIC_SCF1_IRQOD;
   1330 	tcic_write_ind_2(h, TCIC_IR_SCF1_N(h->sock), reg);
   1331 
   1332 	DPRINTF(("%s: tcic_chip_socket_enable %d cardtype %s 0x%02x\n",
   1333 	    h->sc->dev.dv_xname, h->sock,
   1334 	    ((cardtype == PCMCIA_IFTYPE_IO) ? "io" : "mem"), reg));
   1335 
   1336 	/* reinstall all the memory and io mappings */
   1337 
   1338 	for (win = 0; win < h->memwins; win++)
   1339 		if (h->memalloc & (1 << win))
   1340 			tcic_chip_do_mem_map(h, win);
   1341 
   1342 	for (win = 0; win < TCIC_IO_WINS; win++)
   1343 		if (h->ioalloc & (1 << win))
   1344 			tcic_chip_do_io_map(h, win);
   1345 }
   1346 
   1347 void
   1348 tcic_chip_socket_disable(pch)
   1349 	pcmcia_chipset_handle_t pch;
   1350 {
   1351 	struct tcic_handle *h = (struct tcic_handle *) pch;
   1352 	int val;
   1353 
   1354 	DPRINTF(("tcic_chip_socket_disable\n"));
   1355 
   1356 	tcic_sel_sock(h);
   1357 
   1358 	/* disable interrupts */
   1359 	val = tcic_read_ind_2(h, TCIC_IR_SCF1_N(h->sock));
   1360 	val &= TCIC_SCF1_IRQ_MASK;
   1361 	tcic_write_ind_2(h, TCIC_IR_SCF1_N(h->sock), val);
   1362 
   1363 	/* disable the output signals */
   1364 	tcic_write_1(h, TCIC_R_SCTRL, 0);
   1365 	val = tcic_read_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK);
   1366 	val &= ~TCIC_ILOCK_CRESENA;
   1367 	tcic_write_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK, val);
   1368 
   1369 	/* power down the socket */
   1370 	tcic_write_1(h, TCIC_R_PWR, 0);
   1371 }
   1372 
   1373 /*
   1374  * XXX The following is Linux driver but doesn't match the table
   1375  * in the manual.
   1376  */
   1377 int
   1378 tcic_ns2wscnt(ns)
   1379 	int ns;
   1380 {
   1381 	if (ns < 14) {
   1382 		return 0;
   1383 	} else {
   1384 		return (2*(ns-14))/70;	/* XXX assumes 14.31818 MHz clock. */
   1385 	}
   1386 }
   1387 
   1388 int
   1389 tcic_log2(val)
   1390 	u_int val;
   1391 {
   1392 	int i, l2;
   1393 
   1394 	l2 = i = 0;
   1395 	while (val) {
   1396 		if (val & 1)
   1397 			l2 = i;
   1398 		i++;
   1399 		val >>= 1;
   1400 	}
   1401 	return l2;
   1402 }
   1403