Home | History | Annotate | Line # | Download | only in isa
i82365_isasubr.c revision 1.12
      1 /*	$NetBSD: i82365_isasubr.c,v 1.12 2000/02/08 17:14:29 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 #ifndef	PCIC_NO_IRQ_PROBE
    104 #ifdef __hpcmips__
    105 #define	PCIC_NO_IRQ_PROBE	0
    106 #else
    107 #define	PCIC_NO_IRQ_PROBE	1
    108 #endif
    109 #endif
    110 
    111 int	pcic_no_irq_probe = PCIC_NO_IRQ_PROBE;
    112 
    113 /*****************************************************************************
    114  * End of configurable parameters.
    115  *****************************************************************************/
    116 
    117 #ifdef PCICISADEBUG
    118 int	pcicsubr_debug = 0;
    119 #define	DPRINTF(arg) do { if (pcicsubr_debug) printf arg ; } while (0)
    120 #else
    121 #define	DPRINTF(arg)
    122 #endif
    123 
    124 /*
    125  * count the interrupt if we have a status set
    126  * just use socket 0
    127  */
    128 
    129 void pcic_isa_probe_interrupts __P((struct pcic_softc *, struct pcic_handle *));
    130 static int pcic_isa_count_intr __P((void *));
    131 
    132 static int
    133 pcic_isa_count_intr(arg)
    134 	void *arg;
    135 {
    136 	struct pcic_softc *sc;
    137 	struct pcic_handle *h;
    138 	int cscreg;
    139 
    140 	h = arg;
    141 	sc = (struct pcic_softc *)h->ph_parent;
    142 
    143 	cscreg = pcic_read(h, PCIC_CSC);
    144 	if (cscreg & PCIC_CSC_CD) {
    145 		if ((++sc->intr_detect % 20) == 0)
    146 			printf(".");
    147 		else
    148 			DPRINTF(("."));
    149 		return (1);
    150 	}
    151 
    152 	/*
    153 	 * make sure we don't get stuck in a loop due to
    154 	 * unhandled level interupts
    155 	 */
    156 	if (++sc->intr_false > 40) {
    157 		isa_intr_disestablish(sc->intr_est, sc->ih);
    158 		sc->ih = 0;
    159 	}
    160 #ifdef PCICISADEBUG
    161 	if (cscreg)
    162 		DPRINTF(("o"));
    163 	else
    164 		DPRINTF(("X"));
    165 #endif
    166 	return (cscreg ? 1 : 0);
    167 }
    168 
    169 /*
    170  * use soft interrupt card detect to find out which irqs are available
    171  * for this controller
    172  */
    173 void
    174 pcic_isa_probe_interrupts(sc, h)
    175 	struct pcic_softc *sc;
    176 	struct pcic_handle *h;
    177 {
    178 	isa_chipset_tag_t ic;
    179 	int i, j, mask, irq;
    180 	int cd, cscintr, intr, csc;
    181 
    182 	ic = sc->intr_est;
    183 
    184 	printf("%s: controller %d detecting irqs with mask 0x%04x:",
    185 	    sc->dev.dv_xname, h->chip, sc->intr_mask[h->chip]);
    186 	DPRINTF(("\n"));
    187 
    188 	/* clear any current interrupt */
    189 	pcic_read(h, PCIC_CSC);
    190 
    191 	/* first disable the status irq, card detect is enabled later */
    192 	pcic_write(h, PCIC_CSC_INTR, 0);
    193 
    194 	/* steer the interrupt to isa and disable ring and interrupt */
    195 	intr = pcic_read(h, PCIC_INTR);
    196 	DPRINTF(("pcic: old intr 0x%x\n", intr));
    197 	intr &= ~(PCIC_INTR_RI_ENABLE | PCIC_INTR_ENABLE | PCIC_INTR_IRQ_MASK);
    198 	pcic_write(h, PCIC_INTR, intr);
    199 
    200 
    201 	/* clear any current interrupt */
    202 	pcic_read(h, PCIC_CSC);
    203 
    204 	cd = pcic_read(h, PCIC_CARD_DETECT);
    205 	cd |= PCIC_CARD_DETECT_SW_INTR;
    206 
    207 	mask = 0;
    208 	for (i = 0; i < 16; i++) {
    209 		/* honor configured limitations */
    210 		if ((sc->intr_mask[h->chip] & (1 << i)) == 0)
    211 			continue;
    212 
    213 		DPRINTF(("probing irq %d: ", i));
    214 
    215 		/* ask for a pulse interrupt so we don't share */
    216 		if (isa_intr_alloc(ic, (1 << i), IST_PULSE, &irq)) {
    217 			DPRINTF(("currently allocated\n"));
    218 			continue;
    219 		}
    220 
    221 		if ((sc->ih = isa_intr_establish(ic, irq, IST_LEVEL, IPL_TTY,
    222 		    pcic_isa_count_intr, h)) == NULL)
    223 			panic("cant get interrupt");
    224 
    225 		cscintr = PCIC_CSC_INTR_CD_ENABLE;
    226 		cscintr |= (irq << PCIC_CSC_INTR_IRQ_SHIFT);
    227 		pcic_write(h, PCIC_CSC_INTR, cscintr);
    228 
    229 		/* interrupt 40 times */
    230 		sc->intr_detect = 0;
    231 		for (j = 0; j < 40 && sc->ih; j++) {
    232 			sc->intr_false = 0;
    233 			pcic_write(h, PCIC_CARD_DETECT, cd);
    234 			delay(100);
    235 			csc = pcic_read(h, PCIC_CSC);
    236 			DPRINTF(("%s", csc ? "-" : ""));
    237 		}
    238 		DPRINTF((" total %d\n", sc->intr_detect));
    239 		/* allow for misses */
    240 		if (sc->intr_detect > 37 && sc->intr_detect <= 40) {
    241 			printf("%d", i);
    242 			DPRINTF((" succeded\n"));
    243 			mask |= (1 << i);
    244 		}
    245 		pcic_write(h, PCIC_CSC_INTR, 0);
    246 		if (sc->ih)
    247 			isa_intr_disestablish(ic, sc->ih);
    248 	}
    249 	sc->intr_mask[h->chip] = mask;
    250 
    251 	printf("%s\n", sc->intr_mask ? "" : " none");
    252 
    253 	/* clear any current interrupt */
    254 	pcic_read(h, PCIC_CSC);
    255 }
    256 
    257 /*
    258  * called with interrupts enabled, light up the irqs to find out
    259  * which irq lines are actually hooked up to our pcic
    260  */
    261 void
    262 pcic_isa_config_interrupts(self)
    263 	struct device *self;
    264 {
    265 	struct pcic_softc *sc;
    266 	struct pcic_handle *h;
    267 	isa_chipset_tag_t ic;
    268 	int s, i, chipmask, chipuniq;
    269 
    270 	sc = (struct pcic_softc *)self;
    271 	ic = sc->intr_est;
    272 
    273 	/* probe each controller */
    274 	chipmask = 0xffff;
    275 	for (i = 0; i < PCIC_NSLOTS; i += 2) {
    276 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
    277 			h = &sc->handle[i];
    278 		else if (sc->handle[i + 1].flags & PCIC_FLAG_SOCKETP)
    279 			h = &sc->handle[i + 1];
    280 		else
    281 			continue;
    282 
    283 		sc->intr_mask[h->chip] =
    284 		    PCIC_INTR_IRQ_VALIDMASK & pcic_isa_intr_alloc_mask;
    285 
    286 		/* the cirrus chips lack support for the soft interrupt */
    287 		if (pcic_no_irq_probe != 0 &&
    288 		    h->vendor != PCIC_VENDOR_CIRRUS_PD6710 &&
    289 		    h->vendor != PCIC_VENDOR_CIRRUS_PD672X)
    290 			pcic_isa_probe_interrupts(sc, h);
    291 
    292 		chipmask &= sc->intr_mask[h->chip];
    293 	}
    294 	/* now see if there is at least one irq per chip not shared by all */
    295 	chipuniq = 1;
    296 	for (i = 0; i < PCIC_NSLOTS; i += 2) {
    297 		if ((sc->handle[i].flags & PCIC_FLAG_SOCKETP) == 0 &&
    298 		    (sc->handle[i + 1].flags & PCIC_FLAG_SOCKETP) == 0)
    299 			continue;
    300 		if ((sc->intr_mask[i / 2] & ~chipmask) == 0) {
    301 			chipuniq = 0;
    302 			break;
    303 		}
    304 	}
    305 	/*
    306 	 * the rest of the following code used to run at config time with
    307 	 * no interrupts and gets unhappy if this is violated so...
    308 	 */
    309 	s = splhigh();
    310 
    311 	/*
    312 	 * allocate our irq.  it will be used by both controllers.  I could
    313 	 * use two different interrupts, but interrupts are relatively
    314 	 * scarce, shareable, and for PCIC controllers, very infrequent.
    315 	 */
    316 	if (sc->irq != IRQUNK) {
    317 		if ((chipmask & (1 << sc->irq)) == 0)
    318 			printf("%s: warning: configured irq %d not detected as"
    319 			    " available\n", sc->dev.dv_xname, sc->irq);
    320 	} else if (chipmask == 0 ||
    321 	    isa_intr_alloc(ic, chipmask, IST_EDGE, &sc->irq)) {
    322 		printf("%s: no available irq", sc->dev.dv_xname);
    323 		sc->irq = IRQUNK;
    324 	} else if ((chipmask & ~(1 << sc->irq)) == 0 && chipuniq == 0) {
    325 		printf("%s: can't share irq with cards", sc->dev.dv_xname);
    326 		sc->irq = IRQUNK;
    327 	}
    328 	if (sc->irq != IRQUNK) {
    329 		sc->ih = isa_intr_establish(ic, sc->irq, IST_EDGE, IPL_TTY,
    330 		    pcic_intr, sc);
    331 		if (sc->ih == NULL) {
    332 			printf("%s: can't establish interrupt",
    333 			    sc->dev.dv_xname);
    334 			sc->irq = IRQUNK;
    335 		}
    336 	}
    337 	if (sc->irq == IRQUNK)
    338 		printf("; polling for socket events\n");
    339 	else
    340 		printf("%s: using irq %d for socket events\n", sc->dev.dv_xname,
    341 		    sc->irq);
    342 
    343 	pcic_attach_sockets_finish(sc);
    344 
    345 	splx(s);
    346 }
    347 
    348 /*
    349  * XXX This routine does not deal with the aliasing issue that its
    350  * trying to.
    351  *
    352  * Any isa device may be decoding only 10 bits of address including
    353  * the pcic.  This routine only detects if the pcic is doing 10 bits.
    354  *
    355  * What should be done is detect the pcic's idea of the bus width,
    356  * and then within those limits allocate a sparse map, where the
    357  * each sub region is offset by 0x400.
    358  */
    359 void pcic_isa_bus_width_probe (sc, iot, ioh, base, length)
    360 	struct pcic_softc *sc;
    361 	bus_space_tag_t iot;
    362 	bus_space_handle_t ioh;
    363 	bus_addr_t base;
    364 	u_int32_t length;
    365 {
    366 	bus_space_handle_t ioh_high;
    367 	int i, iobuswidth, tmp1, tmp2;
    368 
    369 	/*
    370 	 * figure out how wide the isa bus is.  Do this by checking if the
    371 	 * pcic controller is mirrored 0x400 above where we expect it to be.
    372 	 */
    373 
    374 	iobuswidth = 12;
    375 
    376 	/* Map i/o space. */
    377 	if (bus_space_map(iot, base + 0x400, length, 0, &ioh_high)) {
    378 		printf("%s: can't map high i/o space\n", sc->dev.dv_xname);
    379 		return;
    380 	}
    381 
    382 	for (i = 0; i < PCIC_NSLOTS; i++) {
    383 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP) {
    384 			/*
    385 			 * read the ident flags from the normal space and
    386 			 * from the mirror, and compare them
    387 			 */
    388 
    389 			bus_space_write_1(iot, ioh, PCIC_REG_INDEX,
    390 			    sc->handle[i].sock + PCIC_IDENT);
    391 			tmp1 = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
    392 
    393 			bus_space_write_1(iot, ioh_high, PCIC_REG_INDEX,
    394 			    sc->handle[i].sock + PCIC_IDENT);
    395 			tmp2 = bus_space_read_1(iot, ioh_high, PCIC_REG_DATA);
    396 
    397 			if (tmp1 == tmp2)
    398 				iobuswidth = 10;
    399 		}
    400 	}
    401 
    402 	bus_space_free(iot, ioh_high, length);
    403 
    404 	/*
    405 	 * XXX mycroft recommends I/O space range 0x400-0xfff .  I should put
    406 	 * this in a header somewhere
    407 	 */
    408 
    409 	/*
    410 	 * XXX some hardware doesn't seem to grok addresses in 0x400 range--
    411 	 * apparently missing a bit or more of address lines. (e.g.
    412 	 * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
    413 	 * TravelMate 5000--not clear which is at fault)
    414 	 *
    415 	 * Add a kludge to detect 10 bit wide buses and deal with them,
    416 	 * and also a config file option to override the probe.
    417 	 */
    418 
    419 	if (iobuswidth == 10) {
    420 		sc->iobase = 0x300;
    421 		sc->iosize = 0x0ff;
    422 	} else {
    423 #if 0
    424 		/*
    425 		 * This is what we'd like to use, but...
    426 		 */
    427 		sc->iobase = 0x400;
    428 		sc->iosize = 0xbff;
    429 #else
    430 		/*
    431 		 * ...the above bus width probe doesn't always work.
    432 		 * So, experimentation has shown the following range
    433 		 * to not lose on systems that 0x300-0x3ff loses on
    434 		 * (e.g. the NEC Versa 6030X).
    435 		 */
    436 		sc->iobase = 0x330;
    437 		sc->iosize = 0x0cf;
    438 #endif
    439 	}
    440 
    441 	DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
    442 	    sc->dev.dv_xname, (long) sc->iobase,
    443 
    444 	    (long) sc->iobase + sc->iosize));
    445 
    446 	if (pcic_isa_alloc_iobase && pcic_isa_alloc_iosize) {
    447 		sc->iobase = pcic_isa_alloc_iobase;
    448 		sc->iosize = pcic_isa_alloc_iosize;
    449 
    450 		DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx "
    451 		    "(config override)\n", sc->dev.dv_xname, (long) sc->iobase,
    452 		    (long) sc->iobase + sc->iosize));
    453 	}
    454 }
    455 
    456 void *
    457 pcic_isa_chip_intr_establish(pch, pf, ipl, fct, arg)
    458 	pcmcia_chipset_handle_t pch;
    459 	struct pcmcia_function *pf;
    460 	int ipl;
    461 	int (*fct) __P((void *));
    462 	void *arg;
    463 {
    464 	struct pcic_handle *h = (struct pcic_handle *) pch;
    465 	struct pcic_softc *sc = (struct pcic_softc *)(h->ph_parent);
    466 	isa_chipset_tag_t ic = sc->intr_est;
    467 	int irq, ist;
    468 	void *ih;
    469 	int reg;
    470 
    471 	if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
    472 		ist = IST_LEVEL;
    473 	else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
    474 		ist = IST_PULSE;
    475 	else
    476 		ist = IST_EDGE;
    477 
    478 	if (isa_intr_alloc(ic, sc->intr_mask[h->chip], ist, &irq))
    479 		return (NULL);
    480 	if ((ih = isa_intr_establish(ic, irq, ist, ipl, fct, arg)) == NULL)
    481 		return (NULL);
    482 
    483 	h->ih_irq = irq;
    484 	if (h->flags & PCIC_FLAG_ENABLED) {
    485 		reg = pcic_read(h, PCIC_INTR);
    486 		reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
    487 		reg |= irq;
    488 		pcic_write(h, PCIC_INTR, reg);
    489 	}
    490 
    491 	printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq);
    492 
    493 	return (ih);
    494 }
    495 
    496 void
    497 pcic_isa_chip_intr_disestablish(pch, ih)
    498 	pcmcia_chipset_handle_t pch;
    499 	void *ih;
    500 {
    501 	struct pcic_handle *h = (struct pcic_handle *) pch;
    502 	struct pcic_softc *sc = (struct pcic_softc *)(h->ph_parent);
    503 	isa_chipset_tag_t ic = sc->intr_est;
    504 	int reg;
    505 
    506 	h->ih_irq = 0;
    507 	if (h->flags & PCIC_FLAG_ENABLED) {
    508 		reg = pcic_read(h, PCIC_INTR);
    509 		reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
    510 		pcic_write(h, PCIC_INTR, reg);
    511 	}
    512 	isa_intr_disestablish(ic, ih);
    513 }
    514