Home | History | Annotate | Line # | Download | only in isa
i82365_isasubr.c revision 1.6
      1 /*	$NetBSD: i82365_isasubr.c,v 1.6 2000/02/01 22:56:17 chopps Exp $	*/
      2 
      3 #define	PCICISADEBUG
      4 
      5 /*
      6  * Copyright (c) 2000 Christian E. Hopps.  All rights reserved.
      7  * Copyright (c) 1998 Bill Sommerfeld.  All rights reserved.
      8  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by Marc Horowitz.
     21  * 4. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 
     37 #include <sys/types.h>
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/device.h>
     41 #include <sys/extent.h>
     42 #include <sys/malloc.h>
     43 
     44 #include <vm/vm.h>
     45 
     46 #include <machine/bus.h>
     47 #include <machine/intr.h>
     48 
     49 #include <dev/isa/isareg.h>
     50 #include <dev/isa/isavar.h>
     51 
     52 #include <dev/pcmcia/pcmciareg.h>
     53 #include <dev/pcmcia/pcmciavar.h>
     54 #include <dev/pcmcia/pcmciachip.h>
     55 
     56 #include <dev/ic/i82365reg.h>
     57 #include <dev/ic/i82365var.h>
     58 #include <dev/isa/i82365_isavar.h>
     59 
     60 /*****************************************************************************
     61  * Configurable parameters.
     62  *****************************************************************************/
     63 
     64 #include "opt_pcic_isa_alloc_iobase.h"
     65 #include "opt_pcic_isa_alloc_iosize.h"
     66 #include "opt_pcic_isa_intr_alloc_mask.h"
     67 
     68 /*
     69  * Default I/O allocation range.  If both are set to non-zero, these
     70  * values will be used instead.  Otherwise, the code attempts to probe
     71  * the bus width.  Systems with 10 address bits should use 0x300 and 0xff.
     72  * Systems with 12 address bits (most) should use 0x400 and 0xbff.
     73  */
     74 
     75 #ifndef PCIC_ISA_ALLOC_IOBASE
     76 #define	PCIC_ISA_ALLOC_IOBASE		0
     77 #endif
     78 
     79 #ifndef PCIC_ISA_ALLOC_IOSIZE
     80 #define	PCIC_ISA_ALLOC_IOSIZE		0
     81 #endif
     82 
     83 int	pcic_isa_alloc_iobase = PCIC_ISA_ALLOC_IOBASE;
     84 int	pcic_isa_alloc_iosize = PCIC_ISA_ALLOC_IOSIZE;
     85 
     86 
     87 /*
     88  * Default IRQ allocation bitmask.  This defines the range of allowable
     89  * IRQs for PCMCIA slots.  Useful if order of probing would screw up other
     90  * devices, or if PCIC hardware/cards have trouble with certain interrupt
     91  * lines.
     92  *
     93  * We disable IRQ 10 by default, since some common laptops (namely, the
     94  * NEC Versa series) reserve IRQ 10 for the docking station SCSI interface.
     95  */
     96 
     97 #ifndef PCIC_ISA_INTR_ALLOC_MASK
     98 #define	PCIC_ISA_INTR_ALLOC_MASK	0xffff
     99 #endif
    100 
    101 int	pcic_isa_intr_alloc_mask = PCIC_ISA_INTR_ALLOC_MASK;
    102 
    103 /*****************************************************************************
    104  * End of configurable parameters.
    105  *****************************************************************************/
    106 
    107 #ifdef PCICISADEBUG
    108 int	pcicsubr_debug = 0;
    109 #define	DPRINTF(arg) do { if (pcicsubr_debug) printf arg ; } while (0)
    110 #else
    111 #define	DPRINTF(arg)
    112 #endif
    113 
    114 #ifndef	PCIC_NO_IRQ_PROBE
    115 /*
    116  * count the interrupt if we have a status set
    117  * just use socket 0
    118  */
    119 
    120 void pcic_isa_probe_interrupts __P((struct pcic_softc *, struct pcic_handle *));
    121 static int pcic_isa_count_intr __P((void *));
    122 
    123 static int
    124 pcic_isa_count_intr(arg)
    125 	void *arg;
    126 {
    127 	struct pcic_softc *sc;
    128 	struct pcic_handle *h;
    129 	int cscreg;
    130 
    131 	h = arg;
    132 	sc = (struct pcic_softc *)h->ph_parent;
    133 
    134 	cscreg = pcic_read(h, PCIC_CSC);
    135 	if (cscreg & PCIC_CSC_CD) {
    136 		if ((++sc->intr_detect % 20) == 0)
    137 			printf(".");
    138 		else
    139 			DPRINTF(("."));
    140 		return (1);
    141 	}
    142 
    143 #ifdef PCICISADEBUG
    144 	if (cscreg)
    145 		DPRINTF(("o"));
    146 	else
    147 		DPRINTF(("X"));
    148 #endif
    149 	return (cscreg ? 1 : 0);
    150 }
    151 
    152 /*
    153  * use soft interrupt card detect to find out which irqs are available
    154  * for this controller
    155  */
    156 void
    157 pcic_isa_probe_interrupts(sc, h)
    158 	struct pcic_softc *sc;
    159 	struct pcic_handle *h;
    160 {
    161 	isa_chipset_tag_t ic;
    162 	int i, j, mask, irq;
    163 	int cd, cscintr, intr, csc;
    164 	void *ih;
    165 
    166 	ic = sc->intr_est;
    167 
    168 	printf("%s: controller %d detecting irqs with mask 0x%04x:",
    169 	    sc->dev.dv_xname, h->chip, sc->intr_mask[h->chip]);
    170 	DPRINTF(("\n"));
    171 
    172 	/* clear any current interrupt */
    173 	pcic_read(h, PCIC_CSC);
    174 
    175 	/* first disable the status irq, then enable card detect */
    176 	pcic_write(h, PCIC_CSC_INTR, 0);
    177 	cscintr = PCIC_CSC_INTR_CD_ENABLE;
    178 	pcic_write(h, PCIC_CSC_INTR, cscintr);
    179 
    180 	/* steer the interrupt to isa and disable ring and interrupt */
    181 	intr = pcic_read(h, PCIC_INTR);
    182 	intr &= ~(PCIC_INTR_RI_ENABLE | PCIC_INTR_ENABLE | PCIC_INTR_IRQ_MASK);
    183 	pcic_write(h, PCIC_INTR, intr);
    184 
    185 	/* clear any current interrupt */
    186 	pcic_read(h, PCIC_CSC);
    187 
    188 	cd = pcic_read(h, PCIC_CARD_DETECT);
    189 	cd |= PCIC_CARD_DETECT_SW_INTR;
    190 	mask = 0;
    191 	for (i = 0; i < 16; i++) {
    192 		/* honor configured limitations */
    193 		if ((sc->intr_mask[h->chip] & (1 << i)) == 0)
    194 			continue;
    195 
    196 		DPRINTF(("probing irq %d: ", i));
    197 
    198 		/* ask for a pulse interrupt so we don't share */
    199 		if (isa_intr_alloc(ic, (1 << i), IST_PULSE, &irq)) {
    200 			DPRINTF(("currently allocated\n"));
    201 			continue;
    202 		}
    203 
    204 		if ((ih = isa_intr_establish(ic, irq, IST_LEVEL, IPL_TTY,
    205 		    pcic_isa_count_intr, h)) == NULL)
    206 			panic("cant get interrupt");
    207 
    208 		cscintr &= ~PCIC_CSC_INTR_IRQ_MASK;
    209 		cscintr |= (irq << PCIC_CSC_INTR_IRQ_SHIFT);
    210 		pcic_write(h, PCIC_CSC_INTR, cscintr);
    211 
    212 		/* interrupt 40 times */
    213 		sc->intr_detect = 0;
    214 		for (j = 0; j < 40; j++) {
    215 			pcic_write(h, PCIC_CARD_DETECT, cd);
    216 			delay(100);
    217 			csc = pcic_read(h, PCIC_CSC);
    218 			DPRINTF(("%s", csc ? "-" : ""));
    219 		}
    220 		DPRINTF((" total %d\n", sc->intr_detect));
    221 		/* allow for misses */
    222 		if (sc->intr_detect > 37 && sc->intr_detect <= 40) {
    223 			printf("%d", i);
    224 			DPRINTF((" succeded\n"));
    225 			mask |= (1 << i);
    226 		}
    227 		isa_intr_disestablish(ic, ih);
    228 	}
    229 	mask |= 0x8000;
    230 
    231 	sc->intr_mask[h->chip] = mask;
    232 	printf("%s\n", sc->intr_mask ? "" : " none");
    233 
    234 	/* disable all status interrupts */
    235 	pcic_write(h, PCIC_CSC_INTR, 0);
    236 
    237 	/* clear any current interrupt */
    238 	pcic_read(h, PCIC_CSC);
    239 }
    240 #endif	/* PCIC_NO_IRQ_PROBE */
    241 
    242 /*
    243  * called with interrupts enabled, light up the irqs to find out
    244  * which irq lines are actually hooked up to our pcic
    245  */
    246 void
    247 pcic_isa_config_interrupts(self)
    248 	struct device *self;
    249 {
    250 	struct pcic_softc *sc;
    251 	struct pcic_handle *h;
    252 	isa_chipset_tag_t ic;
    253 	int s, i, chipmask, chipuniq;
    254 
    255 	sc = (struct pcic_softc *)self;
    256 	ic = sc->intr_est;
    257 
    258 	/* probe each controller */
    259 	chipmask = 0xffff;
    260 	for (i = 0; i < PCIC_NSLOTS; i += 2) {
    261 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
    262 			h = &sc->handle[i];
    263 		else if (sc->handle[i + 1].flags & PCIC_FLAG_SOCKETP)
    264 			h = &sc->handle[i + 1];
    265 		else
    266 			continue;
    267 
    268 		sc->intr_mask[h->chip] =
    269 		    PCIC_INTR_IRQ_VALIDMASK & pcic_isa_intr_alloc_mask;
    270 
    271 #ifndef	PCIC_NO_IRQ_PROBE
    272 		/* the cirrus chips lack support for the soft interrupt */
    273 		if (h->vendor != PCIC_VENDOR_CIRRUS_PD6710 &&
    274 		    h->vendor != PCIC_VENDOR_CIRRUS_PD672X)
    275 			pcic_isa_probe_interrupts(sc, h);
    276 #endif
    277 
    278 		chipmask &= sc->intr_mask[h->chip];
    279 	}
    280 	/* now see if there is at least one irq per chip not shared by all */
    281 	chipuniq = 1;
    282 	for (i = 0; i < PCIC_NSLOTS; i += 2) {
    283 		if ((sc->handle[i].flags & PCIC_FLAG_SOCKETP) == 0 &&
    284 		    (sc->handle[i + 1].flags & PCIC_FLAG_SOCKETP) == 0)
    285 			continue;
    286 		if ((sc->intr_mask[i / 2] & ~chipmask) == 0) {
    287 			chipuniq = 0;
    288 			break;
    289 		}
    290 	}
    291 	/*
    292 	 * the rest of the following code used to run at config time with
    293 	 * no interrupts and gets unhappy if this is violated so...
    294 	 */
    295 	s = splhigh();
    296 
    297 	/*
    298 	 * allocate our irq.  it will be used by both controllers.  I could
    299 	 * use two different interrupts, but interrupts are relatively
    300 	 * scarce, shareable, and for PCIC controllers, very infrequent.
    301 	 */
    302 	if (sc->irq != IRQUNK) {
    303 		if ((chipmask & (1 << sc->irq)) == 0)
    304 			printf("%s: warning: configured irq %d not detected as"
    305 			    " available\n", sc->dev.dv_xname, sc->irq);
    306 	} else if (chipmask == 0 ||
    307 	    isa_intr_alloc(ic, chipmask, IST_EDGE, &sc->irq)) {
    308 		printf("%s: no available irq", sc->dev.dv_xname);
    309 		sc->irq = -1;
    310 	} else if ((chipmask & ~(1 << sc->irq)) == 0 && chipuniq == 0) {
    311 		printf("%s: can\'t share irq with cards", sc->dev.dv_xname);
    312 		sc->irq = -1;
    313 	}
    314 	if (sc->irq != -1) {
    315 		printf("%s: using irq %d\n", sc->dev.dv_xname, sc->irq);
    316 		sc->ih = isa_intr_establish(ic, sc->irq, IST_EDGE, IPL_TTY,
    317 		    pcic_intr, sc);
    318 		if (sc->ih == NULL) {
    319 			printf("%s: can't establish interrupt",
    320 			    sc->dev.dv_xname);
    321 			sc->irq = -1;
    322 		}
    323 	}
    324 	if (sc->irq == -1)
    325 		printf(", will poll for card insertion and removal\n");
    326 
    327 	pcic_attach_sockets_finish(sc);
    328 
    329 	splx(s);
    330 }
    331 
    332 /*
    333  * XXX This routine does not deal with the aliasing issue that its
    334  * trying to.
    335  *
    336  * Any isa device may be decoding only 10 bits of address including
    337  * the pcic.  This routine only detects if the pcic is doing 10 bits.
    338  *
    339  * What should be done is detect the pcic's idea of the bus width,
    340  * and then within those limits allocate a sparse map, where the
    341  * each sub region is offset by 0x400.
    342  */
    343 void pcic_isa_bus_width_probe (sc, iot, ioh, base, length)
    344 	struct pcic_softc *sc;
    345 	bus_space_tag_t iot;
    346 	bus_space_handle_t ioh;
    347 	bus_addr_t base;
    348 	u_int32_t length;
    349 {
    350 	bus_space_handle_t ioh_high;
    351 	int i, iobuswidth, tmp1, tmp2;
    352 
    353 	/*
    354 	 * figure out how wide the isa bus is.  Do this by checking if the
    355 	 * pcic controller is mirrored 0x400 above where we expect it to be.
    356 	 */
    357 
    358 	iobuswidth = 12;
    359 
    360 	/* Map i/o space. */
    361 	if (bus_space_map(iot, base + 0x400, length, 0, &ioh_high)) {
    362 		printf("%s: can't map high i/o space\n", sc->dev.dv_xname);
    363 		return;
    364 	}
    365 
    366 	for (i = 0; i < PCIC_NSLOTS; i++) {
    367 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP) {
    368 			/*
    369 			 * read the ident flags from the normal space and
    370 			 * from the mirror, and compare them
    371 			 */
    372 
    373 			bus_space_write_1(iot, ioh, PCIC_REG_INDEX,
    374 			    sc->handle[i].sock + PCIC_IDENT);
    375 			tmp1 = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
    376 
    377 			bus_space_write_1(iot, ioh_high, PCIC_REG_INDEX,
    378 			    sc->handle[i].sock + PCIC_IDENT);
    379 			tmp2 = bus_space_read_1(iot, ioh_high, PCIC_REG_DATA);
    380 
    381 			if (tmp1 == tmp2)
    382 				iobuswidth = 10;
    383 		}
    384 	}
    385 
    386 	bus_space_free(iot, ioh_high, length);
    387 
    388 	/*
    389 	 * XXX mycroft recommends I/O space range 0x400-0xfff .  I should put
    390 	 * this in a header somewhere
    391 	 */
    392 
    393 	/*
    394 	 * XXX some hardware doesn't seem to grok addresses in 0x400 range--
    395 	 * apparently missing a bit or more of address lines. (e.g.
    396 	 * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
    397 	 * TravelMate 5000--not clear which is at fault)
    398 	 *
    399 	 * Add a kludge to detect 10 bit wide buses and deal with them,
    400 	 * and also a config file option to override the probe.
    401 	 */
    402 
    403 	if (iobuswidth == 10) {
    404 		sc->iobase = 0x300;
    405 		sc->iosize = 0x0ff;
    406 	} else {
    407 #if 0
    408 		/*
    409 		 * This is what we'd like to use, but...
    410 		 */
    411 		sc->iobase = 0x400;
    412 		sc->iosize = 0xbff;
    413 #else
    414 		/*
    415 		 * ...the above bus width probe doesn't always work.
    416 		 * So, experimentation has shown the following range
    417 		 * to not lose on systems that 0x300-0x3ff loses on
    418 		 * (e.g. the NEC Versa 6030X).
    419 		 */
    420 		sc->iobase = 0x330;
    421 		sc->iosize = 0x0cf;
    422 #endif
    423 	}
    424 
    425 	DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
    426 	    sc->dev.dv_xname, (long) sc->iobase,
    427 
    428 	    (long) sc->iobase + sc->iosize));
    429 
    430 	if (pcic_isa_alloc_iobase && pcic_isa_alloc_iosize) {
    431 		sc->iobase = pcic_isa_alloc_iobase;
    432 		sc->iosize = pcic_isa_alloc_iosize;
    433 
    434 		DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx "
    435 		    "(config override)\n", sc->dev.dv_xname, (long) sc->iobase,
    436 		    (long) sc->iobase + sc->iosize));
    437 	}
    438 }
    439 
    440 void *
    441 pcic_isa_chip_intr_establish(pch, pf, ipl, fct, arg)
    442 	pcmcia_chipset_handle_t pch;
    443 	struct pcmcia_function *pf;
    444 	int ipl;
    445 	int (*fct) __P((void *));
    446 	void *arg;
    447 {
    448 	struct pcic_handle *h = (struct pcic_handle *) pch;
    449 	struct pcic_softc *sc = (struct pcic_softc *)(h->ph_parent);
    450 	isa_chipset_tag_t ic = sc->intr_est;
    451 	int irq, ist;
    452 	void *ih;
    453 	int reg;
    454 
    455 	if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
    456 		ist = IST_LEVEL;
    457 	else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
    458 		ist = IST_PULSE;
    459 	else
    460 		ist = IST_EDGE;
    461 
    462 	if (isa_intr_alloc(ic, sc->intr_mask[h->chip], ist, &irq))
    463 		return (NULL);
    464 	if ((ih = isa_intr_establish(ic, irq, ist, ipl, fct, arg)) == NULL)
    465 		return (NULL);
    466 
    467 	reg = pcic_read(h, PCIC_INTR);
    468 	reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
    469 	reg |= irq;
    470 	pcic_write(h, PCIC_INTR, reg);
    471 
    472 	h->ih_irq = irq;
    473 
    474 	printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq);
    475 
    476 	return (ih);
    477 }
    478 
    479 void
    480 pcic_isa_chip_intr_disestablish(pch, ih)
    481 	pcmcia_chipset_handle_t pch;
    482 	void *ih;
    483 {
    484 	struct pcic_handle *h = (struct pcic_handle *) pch;
    485 	struct pcic_softc *sc = (struct pcic_softc *)(h->ph_parent);
    486 	isa_chipset_tag_t ic = sc->intr_est;
    487 	int reg;
    488 
    489 	h->ih_irq = 0;
    490 
    491 	reg = pcic_read(h, PCIC_INTR);
    492 	reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
    493 	pcic_write(h, PCIC_INTR, reg);
    494 
    495 	isa_intr_disestablish(ic, ih);
    496 }
    497