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