Home | History | Annotate | Line # | Download | only in isa
tcic2_isa.c revision 1.15
      1 /*	$NetBSD: tcic2_isa.c,v 1.15 2006/09/03 06:46:22 christos Exp $	*/
      2 
      3 /*
      4  *
      5  * Copyright (c) 1998, 1999 Christoph Badura. All rights reserved.
      6  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Marc Horowitz.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: tcic2_isa.c,v 1.15 2006/09/03 06:46:22 christos Exp $");
     36 
     37 #undef	TCICISADEBUG
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/device.h>
     42 #include <sys/extent.h>
     43 #include <sys/malloc.h>
     44 
     45 #include <machine/bus.h>
     46 #include <machine/intr.h>
     47 
     48 #include <dev/isa/isareg.h>
     49 #include <dev/isa/isavar.h>
     50 
     51 #include <dev/pcmcia/pcmciareg.h>
     52 #include <dev/pcmcia/pcmciavar.h>
     53 #include <dev/pcmcia/pcmciachip.h>
     54 
     55 #include <dev/ic/tcic2reg.h>
     56 #include <dev/ic/tcic2var.h>
     57 
     58 /*****************************************************************************
     59  * Configurable parameters.
     60  *****************************************************************************/
     61 
     62 #include "opt_tcic_isa_alloc_iobase.h"
     63 #include "opt_tcic_isa_alloc_iosize.h"
     64 #include "opt_tcic_isa_intr_alloc_mask.h"
     65 
     66 /*
     67  * Default I/O allocation range.  If both are set to non-zero, these
     68  * values will be used instead.  Otherwise, the code attempts to probe
     69  * the bus width.  Systems with 10 address bits should use 0x300 and 0xff.
     70  * Systems with 12 address bits (most) should use 0x400 and 0xbff.
     71  */
     72 
     73 #ifndef TCIC_ISA_ALLOC_IOBASE
     74 #define	TCIC_ISA_ALLOC_IOBASE		0
     75 #endif
     76 
     77 #ifndef TCIC_ISA_ALLOC_IOSIZE
     78 #define	TCIC_ISA_ALLOC_IOSIZE		0
     79 #endif
     80 
     81 int	tcic_isa_alloc_iobase = TCIC_ISA_ALLOC_IOBASE;
     82 int	tcic_isa_alloc_iosize = TCIC_ISA_ALLOC_IOSIZE;
     83 
     84 /*
     85  * Default IRQ allocation bitmask.  This defines the range of allowable
     86  * IRQs for PCMCIA slots.  Useful if order of probing would screw up other
     87  * devices, or if TCIC hardware/cards have trouble with certain interrupt
     88  * lines.
     89  *
     90  * We disable IRQ 10 by default, since some common laptops (namely, the
     91  * NEC Versa series) reserve IRQ 10 for the docking station SCSI interface.
     92  *
     93  * XXX Do we care about this?  the Versa doesn't use a tcic. -chb
     94  */
     95 
     96 #ifndef TCIC_ISA_INTR_ALLOC_MASK
     97 #define	TCIC_ISA_INTR_ALLOC_MASK	0xffff
     98 #endif
     99 
    100 int	tcic_isa_intr_alloc_mask = TCIC_ISA_INTR_ALLOC_MASK;
    101 
    102 /*****************************************************************************
    103  * End of configurable parameters.
    104  *****************************************************************************/
    105 
    106 #ifdef TCICISADEBUG
    107 int	tcic_isa_debug = 1;
    108 #define	DPRINTF(arg) if (tcic_isa_debug) printf arg;
    109 #else
    110 #define	DPRINTF(arg)
    111 #endif
    112 
    113 int	tcic_isa_probe(struct device *, struct cfdata *, void *);
    114 void	tcic_isa_attach(struct device *, struct device *, void *);
    115 
    116 void	*tcic_isa_chip_intr_establish(pcmcia_chipset_handle_t,
    117 	    struct pcmcia_function *, int, int (*) (void *), void *);
    118 void	tcic_isa_chip_intr_disestablish(pcmcia_chipset_handle_t, void *);
    119 
    120 CFATTACH_DECL(tcic_isa, sizeof(struct tcic_softc),
    121     tcic_isa_probe, tcic_isa_attach, NULL, NULL);
    122 
    123 static struct pcmcia_chip_functions tcic_isa_functions = {
    124 	tcic_chip_mem_alloc,
    125 	tcic_chip_mem_free,
    126 	tcic_chip_mem_map,
    127 	tcic_chip_mem_unmap,
    128 
    129 	tcic_chip_io_alloc,
    130 	tcic_chip_io_free,
    131 	tcic_chip_io_map,
    132 	tcic_chip_io_unmap,
    133 
    134 	tcic_isa_chip_intr_establish,
    135 	tcic_isa_chip_intr_disestablish,
    136 
    137 	tcic_chip_socket_enable,
    138 	tcic_chip_socket_disable,
    139 	tcic_chip_socket_settype,
    140 
    141 	NULL,	/* card_detect */
    142 };
    143 
    144 int
    145 tcic_isa_probe(parent, match, aux)
    146 	struct device *parent;
    147 	struct cfdata *match;
    148 	void *aux;
    149 {
    150 	struct isa_attach_args *ia = aux;
    151 	bus_space_tag_t iot = ia->ia_iot;
    152 	bus_space_handle_t ioh, memh;
    153 	int val, found, msize;
    154 
    155 	if (ia->ia_nio < 1)
    156 		return (0);
    157 	if (ia->ia_niomem < 1)
    158 		return (0);
    159 
    160 	if (ISA_DIRECT_CONFIG(ia))
    161 		return (0);
    162 
    163 	/* Disallow wildcarded i/o address. */
    164 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    165 		return (0);
    166 	if (ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM)
    167 		return (0);
    168 
    169 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, TCIC_IOSIZE, 0, &ioh))
    170 		return (0);
    171 
    172 	if (ia->ia_iomem[0].ir_size == ISA_UNKNOWN_IOSIZ)
    173 		msize = TCIC_MEMSIZE;
    174 	else
    175 		msize = ia->ia_iomem[0].ir_size;
    176 
    177 	if (bus_space_map(ia->ia_memt, ia->ia_iomem[0].ir_addr,
    178 	    msize, 0, &memh)) {
    179 		bus_space_unmap(iot, ioh, TCIC_IOSIZE);
    180 		return (0);
    181 	}
    182 
    183 	DPRINTF(("tcic probing 0x%03x\n", ia->ia_iobase));
    184 	found = 0;
    185 
    186 	/*
    187 	 * First, check for the reserved bits to be zero.
    188 	 */
    189 	if (tcic_check_reserved_bits(iot, ioh)) {
    190 		DPRINTF(("tcic: reserved bits checked OK\n"));
    191 		/* Second, check whether the we know how to handle the chip. */
    192 		if ((val = tcic_chipid(iot, ioh))) {
    193 			DPRINTF(("tcic id: 0x%02x\n", val));
    194 			if (tcic_chipid_known(val))
    195 				found++;
    196 		}
    197 	}
    198 	else {
    199 		DPRINTF(("tcic: reserved bits didn't check OK\n"));
    200 	}
    201 
    202 	bus_space_unmap(iot, ioh, TCIC_IOSIZE);
    203 	bus_space_unmap(ia->ia_memt, memh, msize);
    204 
    205 	if (!found)
    206 		return (0);
    207 
    208 	ia->ia_nio = 1;
    209 	ia->ia_io[0].ir_size = TCIC_IOSIZE;
    210 
    211 	ia->ia_niomem = 1;
    212 	ia->ia_iomem[0].ir_size = msize;
    213 
    214 	/* IRQ is special. */
    215 
    216 	ia->ia_ndrq = 0;
    217 
    218 	return (1);
    219 }
    220 
    221 void
    222 tcic_isa_attach(parent, self, aux)
    223 	struct device *parent, *self;
    224 	void *aux;
    225 {
    226 	struct tcic_softc *sc = (void *) self;
    227 	struct isa_attach_args *ia = aux;
    228 	isa_chipset_tag_t ic = ia->ia_ic;
    229 	bus_space_tag_t iot = ia->ia_iot;
    230 	bus_space_tag_t memt = ia->ia_memt;
    231 	bus_space_handle_t ioh;
    232 	bus_space_handle_t memh;
    233 
    234 	/* Map i/o space. */
    235 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, TCIC_IOSIZE, 0, &ioh)) {
    236 		printf(": can't map i/o space\n");
    237 		return;
    238 	}
    239 
    240 	/* Map mem space. */
    241 	if (bus_space_map(memt, ia->ia_iomem[0].ir_addr,
    242 	    ia->ia_iomem[0].ir_size, 0, &memh)) {
    243 		printf(": can't map mem space\n");
    244 		return;
    245 	}
    246 
    247 	sc->membase = ia->ia_iomem[0].ir_addr;
    248 	sc->subregionmask =
    249 	    (1 << (ia->ia_iomem[0].ir_size / TCIC_MEM_PAGESIZE)) - 1;
    250 	sc->memsize2 = tcic_log2((u_int)ia->ia_iomem[0].ir_size);
    251 
    252 	sc->intr_est = ic;
    253 	sc->pct = (pcmcia_chipset_tag_t) & tcic_isa_functions;
    254 
    255 	sc->iot = iot;
    256 	sc->ioh = ioh;
    257 	sc->memt = memt;
    258 	sc->memh = memh;
    259 
    260 	/*
    261 	 * determine chip type and initialise some chip type dependend
    262 	 * parameters in softc.
    263 	 */
    264 	sc->chipid = tcic_chipid(iot, ioh);
    265 	sc->validirqs = tcic_validirqs(sc->chipid);
    266 
    267 	/*
    268 	 * allocate an irq.  interrupts are relatively
    269 	 * scarce but for TCIC controllers very infrequent.
    270 	 */
    271 
    272 	if (ia->ia_nirq < 1)
    273 		sc->irq = ISA_UNKNOWN_IRQ;
    274 	else
    275 		sc->irq = ia->ia_irq[0].ir_irq;
    276 	if (sc->irq == ISA_UNKNOWN_IRQ) {
    277 		if (isa_intr_alloc(ic,
    278 		    sc->validirqs & (tcic_isa_intr_alloc_mask & 0xff00),
    279 		    IST_EDGE, &sc->irq)) {
    280 			printf("\n%s: can't allocate interrupt\n",
    281 			    sc->dev.dv_xname);
    282 			return;
    283 		}
    284 		printf(": using irq %d", sc->irq);
    285 	}
    286 	printf("\n");
    287 
    288 	tcic_attach(sc);
    289 
    290 
    291 	/*
    292 	 * XXX mycroft recommends I/O space range 0x400-0xfff.
    293 	 */
    294 
    295 	/*
    296 	 * XXX some hardware doesn't seem to grok addresses in 0x400 range--
    297 	 * apparently missing a bit or more of address lines. (e.g.
    298 	 * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
    299 	 * TravelMate 5000--not clear which is at fault)
    300 	 *
    301 	 * Add a kludge to detect 10 bit wide buses and deal with them,
    302 	 * and also a config file option to override the probe.
    303 	 */
    304 
    305 #if 0
    306 	/*
    307 	 * This is what we'd like to use, but...
    308 	 */
    309 	sc->iobase = 0x400;
    310 	sc->iosize = 0xbff;
    311 #else
    312 	/*
    313 	 * ...the above bus width probe doesn't always work.
    314 	 * So, experimentation has shown the following range
    315 	 * to not lose on systems that 0x300-0x3ff loses on
    316 	 * (e.g. the NEC Versa 6030X).
    317 	 */
    318 	sc->iobase = 0x330;
    319 	sc->iosize = 0x0cf;
    320 #endif
    321 
    322 	DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx)\n",
    323 	    sc->dev.dv_xname, (long) sc->iobase,
    324 	    (long) sc->iobase + sc->iosize));
    325 
    326 	if (tcic_isa_alloc_iobase && tcic_isa_alloc_iosize) {
    327 		sc->iobase = tcic_isa_alloc_iobase;
    328 		sc->iosize = tcic_isa_alloc_iosize;
    329 
    330 		DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx "
    331 		    "(config override)\n", sc->dev.dv_xname, (long) sc->iobase,
    332 		    (long) sc->iobase + sc->iosize));
    333 	}
    334 	sc->ih = isa_intr_establish(ic, sc->irq, IST_EDGE, IPL_TTY,
    335 	    tcic_intr, sc);
    336 	if (sc->ih == NULL) {
    337 		printf("%s: can't establish interrupt\n", sc->dev.dv_xname);
    338 		return;
    339 	}
    340 
    341 	tcic_attach_sockets(sc);
    342 }
    343 
    344 void *
    345 tcic_isa_chip_intr_establish(pch, pf, ipl, fct, arg)
    346 	pcmcia_chipset_handle_t pch;
    347 	struct pcmcia_function *pf;
    348 	int ipl;
    349 	int (*fct)(void *);
    350 	void *arg;
    351 {
    352 	struct tcic_handle *h = (struct tcic_handle *) pch;
    353 	int irq, ist;
    354 	void *ih;
    355 
    356 	DPRINTF(("%s: tcic_isa_chip_intr_establish\n", h->sc->dev.dv_xname));
    357 
    358 	/* XXX should we convert level to pulse? -chb  */
    359 	if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
    360 		ist = IST_LEVEL;
    361 	else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
    362 		ist = IST_PULSE;
    363 	else
    364 		ist = IST_LEVEL;
    365 
    366 	if (isa_intr_alloc(h->sc->intr_est,
    367 	    h->sc->validirqs & tcic_isa_intr_alloc_mask, ist, &irq))
    368 		return (NULL);
    369 	if ((ih = isa_intr_establish(h->sc->intr_est, irq, ist, ipl,
    370 	    fct, arg)) == NULL)
    371 		return (NULL);
    372 
    373 	DPRINTF(("%s: intr estrablished\n", h->sc->dev.dv_xname));
    374 
    375 	h->ih_irq = irq;
    376 
    377 	printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq);
    378 
    379 	return (ih);
    380 }
    381 
    382 void
    383 tcic_isa_chip_intr_disestablish(pch, ih)
    384 	pcmcia_chipset_handle_t pch;
    385 	void *ih;
    386 {
    387 	struct tcic_handle *h = (struct tcic_handle *) pch;
    388 	int val, reg;
    389 
    390 	DPRINTF(("%s: tcic_isa_chip_intr_disestablish\n", h->sc->dev.dv_xname));
    391 
    392 	h->ih_irq = 0;
    393 
    394 	reg = TCIC_IR_SCF1_N(h->sock);
    395 	val = tcic_read_ind_2(h, reg);
    396 	val &= ~TCIC_SCF1_IRQ_MASK;
    397 	tcic_write_ind_2(h, reg, val);
    398 
    399 	isa_intr_disestablish(h->sc->intr_est, ih);
    400 }
    401