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