Home | History | Annotate | Line # | Download | only in isa
i82365_isa.c revision 1.4
      1 /*	$NetBSD: i82365_isa.c,v 1.4 1997/10/20 18:43:10 thorpej Exp $	*/
      2 
      3 #define	PCICISADEBUG
      4 
      5 /*
      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/types.h>
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/extent.h>
     39 #include <sys/malloc.h>
     40 
     41 #include <vm/vm.h>
     42 
     43 #include <machine/bus.h>
     44 #include <machine/intr.h>
     45 
     46 #include <dev/isa/isareg.h>
     47 #include <dev/isa/isavar.h>
     48 
     49 #include <dev/pcmcia/pcmciareg.h>
     50 #include <dev/pcmcia/pcmciavar.h>
     51 #include <dev/pcmcia/pcmciachip.h>
     52 
     53 #include <dev/ic/i82365reg.h>
     54 #include <dev/ic/i82365var.h>
     55 
     56 #ifdef PCICISADEBUG
     57 int	pcicisa_debug = 0 /* XXX */ ;
     58 #define	DPRINTF(arg) if (pcicisa_debug) printf arg;
     59 #else
     60 #define	DPRINTF(arg)
     61 #endif
     62 
     63 int	pcic_isa_probe __P((struct device *, void *, void *));
     64 void	pcic_isa_attach __P((struct device *, struct device *, void *));
     65 
     66 void	*pcic_isa_chip_intr_establish __P((pcmcia_chipset_handle_t,
     67 	    struct pcmcia_function *, int, int (*) (void *), void *));
     68 void	pcic_isa_chip_intr_disestablish __P((pcmcia_chipset_handle_t, void *));
     69 
     70 struct cfattach pcic_isa_ca = {
     71 	sizeof(struct pcic_softc), pcic_isa_probe, pcic_isa_attach
     72 };
     73 
     74 static struct pcmcia_chip_functions pcic_isa_functions = {
     75 	pcic_chip_mem_alloc,
     76 	pcic_chip_mem_free,
     77 	pcic_chip_mem_map,
     78 	pcic_chip_mem_unmap,
     79 
     80 	pcic_chip_io_alloc,
     81 	pcic_chip_io_free,
     82 	pcic_chip_io_map,
     83 	pcic_chip_io_unmap,
     84 
     85 	pcic_isa_chip_intr_establish,
     86 	pcic_isa_chip_intr_disestablish,
     87 
     88 	pcic_chip_socket_enable,
     89 	pcic_chip_socket_disable,
     90 };
     91 
     92 int
     93 pcic_isa_probe(parent, match, aux)
     94 	struct device *parent;
     95 	void *match, *aux;
     96 {
     97 	struct isa_attach_args *ia = aux;
     98 	bus_space_tag_t iot = ia->ia_iot;
     99 	bus_space_handle_t ioh, memh;
    100 	int val, found;
    101 
    102 	/* Disallow wildcarded i/o address. */
    103 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
    104 		return (0);
    105 
    106 	if (bus_space_map(iot, ia->ia_iobase, PCIC_IOSIZE, 0, &ioh))
    107 		return (0);
    108 
    109 	if (ia->ia_msize == -1)
    110 		ia->ia_msize = PCIC_MEMSIZE;
    111 
    112 	if (bus_space_map(ia->ia_memt, ia->ia_maddr, ia->ia_msize, 0, &memh))
    113 		return (0);
    114 
    115 	found = 0;
    116 
    117 	/*
    118 	 * this could be done with a loop, but it would violate the
    119 	 * abstraction
    120 	 */
    121 
    122 	bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SA + PCIC_IDENT);
    123 
    124 	val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
    125 
    126 	if (pcic_ident_ok(val))
    127 		found++;
    128 
    129 
    130 	bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SB + PCIC_IDENT);
    131 
    132 	val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
    133 
    134 	if (pcic_ident_ok(val))
    135 		found++;
    136 
    137 
    138 	bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SA + PCIC_IDENT);
    139 
    140 	val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
    141 
    142 	if (pcic_ident_ok(val))
    143 		found++;
    144 
    145 
    146 	bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SB + PCIC_IDENT);
    147 
    148 	val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
    149 
    150 	if (pcic_ident_ok(val))
    151 		found++;
    152 
    153 
    154 	bus_space_unmap(iot, ioh, PCIC_IOSIZE);
    155 	bus_space_unmap(ia->ia_memt, memh, ia->ia_msize);
    156 
    157 	if (!found)
    158 		return (0);
    159 
    160 	ia->ia_iosize = PCIC_IOSIZE;
    161 
    162 	return (1);
    163 }
    164 
    165 #ifndef PCIC_ISA_ALLOC_IOBASE
    166 #define	PCIC_ISA_ALLOC_IOBASE 0
    167 #endif
    168 
    169 #ifndef PCIC_ISA_ALLOC_IOSIZE
    170 #define	PCIC_ISA_ALLOC_IOSIZE 0
    171 #endif
    172 
    173 int	pcic_isa_alloc_iobase = PCIC_ISA_ALLOC_IOBASE;
    174 int	pcic_isa_alloc_iosize = PCIC_ISA_ALLOC_IOSIZE;
    175 
    176 void
    177 pcic_isa_attach(parent, self, aux)
    178 	struct device *parent, *self;
    179 	void *aux;
    180 {
    181 	struct pcic_softc *sc = (void *) self;
    182 	struct isa_attach_args *ia = aux;
    183 	isa_chipset_tag_t ic = ia->ia_ic;
    184 	bus_space_tag_t iot = ia->ia_iot;
    185 	bus_space_tag_t memt = ia->ia_memt;
    186 	bus_space_handle_t ioh;
    187 	bus_space_handle_t memh;
    188 	bus_space_handle_t ioh_high;
    189 	int i, iobuswidth, tmp1, tmp2;
    190 
    191 	/* Map i/o space. */
    192 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
    193 		printf(": can't map i/o space\n");
    194 		return;
    195 	}
    196 
    197 	/* Map mem space. */
    198 	if (bus_space_map(memt, ia->ia_maddr, ia->ia_msize, 0, &memh)) {
    199 		printf(": can't map mem space\n");
    200 		return;
    201 	}
    202 
    203 	sc->membase = ia->ia_maddr;
    204 	sc->subregionmask = (1 << (ia->ia_msize / PCIC_MEM_PAGESIZE)) - 1;
    205 
    206 	sc->intr_est = ic;
    207 	sc->pct = (pcmcia_chipset_tag_t) & pcic_isa_functions;
    208 
    209 	sc->iot = iot;
    210 	sc->ioh = ioh;
    211 	sc->memt = memt;
    212 	sc->memh = memh;
    213 
    214 	/*
    215 	 * allocate an irq.  it will be used by both controllers.  I could
    216 	 * use two different interrupts, but interrupts are relatively
    217 	 * scarce, shareable, and for PCIC controllers, very infrequent.
    218 	 */
    219 
    220 	if ((sc->irq = ia->ia_irq) == IRQUNK) {
    221 		/* XXX CHECK RETURN VALUE */
    222 		(void) isa_intr_alloc(ic, PCIC_CSC_INTR_IRQ_VALIDMASK,
    223 		    IST_EDGE, &sc->irq);
    224 		printf(": using irq %d", sc->irq);
    225 	}
    226 	printf("\n");
    227 
    228 	pcic_attach(sc);
    229 
    230 	/*
    231 	 * figure out how wide the isa bus is.  Do this by checking if the
    232 	 * pcic controller is mirrored 0x400 above where we expect it to be.
    233 	 */
    234 
    235 	iobuswidth = 12;
    236 
    237 	/* Map i/o space. */
    238 	if (bus_space_map(iot, ia->ia_iobase + 0x400, ia->ia_iosize, 0,
    239 	    &ioh_high)) {
    240 		printf("%s: can't map high i/o space\n", sc->dev.dv_xname);
    241 		return;
    242 	}
    243 
    244 	for (i = 0; i < PCIC_NSLOTS; i++) {
    245 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP) {
    246 			/*
    247 			 * read the ident flags from the normal space and
    248 			 * from the mirror, and compare them
    249 			 */
    250 
    251 			bus_space_write_1(iot, ioh, PCIC_REG_INDEX,
    252 			    sc->handle[i].sock + PCIC_IDENT);
    253 			tmp1 = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
    254 
    255 			bus_space_write_1(iot, ioh_high, PCIC_REG_INDEX,
    256 			    sc->handle[i].sock + PCIC_IDENT);
    257 			tmp2 = bus_space_read_1(iot, ioh_high, PCIC_REG_DATA);
    258 
    259 			if (tmp1 == tmp2)
    260 				iobuswidth = 10;
    261 		}
    262 	}
    263 
    264 	bus_space_free(iot, ioh_high, ia->ia_iosize);
    265 
    266 	/*
    267 	 * XXX mycroft recommends I/O space range 0x400-0xfff .  I should put
    268 	 * this in a header somewhere
    269 	 */
    270 
    271 	/*
    272 	 * XXX some hardware doesn't seem to grok addresses in 0x400 range--
    273 	 * apparently missing a bit or more of address lines. (e.g.
    274 	 * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
    275 	 * TravelMate 5000--not clear which is at fault)
    276 	 *
    277 	 * Add a kludge to detect 10 bit wide buses and deal with them,
    278 	 * and also a config file option to override the probe.
    279 	 */
    280 
    281 	if (iobuswidth == 10) {
    282 		sc->iobase = 0x300;
    283 		sc->iosize = 0x0ff;
    284 	} else {
    285 		sc->iobase = 0x400;
    286 		sc->iosize = 0xbff;
    287 	}
    288 
    289 	DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
    290 	    sc->dev.dv_xname, (long) sc->iobase,
    291 	    (long) sc->iobase + sc->iosize));
    292 
    293 	if (pcic_isa_alloc_iobase && pcic_isa_alloc_iosize) {
    294 		sc->iobase = pcic_isa_alloc_iobase;
    295 		sc->iosize = pcic_isa_alloc_iosize;
    296 
    297 		DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx "
    298 		    "(config override)\n", sc->dev.dv_xname, (long) sc->iobase,
    299 		    (long) sc->iobase + sc->iosize));
    300 	}
    301 	sc->ih = isa_intr_establish(ic, sc->irq, IST_EDGE, IPL_TTY,
    302 	    pcic_intr, sc);
    303 
    304 	pcic_attach_sockets(sc);
    305 }
    306 
    307 /*
    308  * allow patching or kernel option file override of available IRQs. Useful if
    309  * order of probing would screw up other devices, or if PCIC hardware/cards
    310  * have trouble with certain interrupt lines.
    311  */
    312 
    313 #ifndef PCIC_INTR_ALLOC_MASK
    314 #define	PCIC_INTR_ALLOC_MASK	0xffff
    315 #endif
    316 
    317 int	pcic_intr_alloc_mask = PCIC_INTR_ALLOC_MASK;
    318 
    319 void *
    320 pcic_isa_chip_intr_establish(pch, pf, ipl, fct, arg)
    321 	pcmcia_chipset_handle_t pch;
    322 	struct pcmcia_function *pf;
    323 	int ipl;
    324 	int (*fct) __P((void *));
    325 	void *arg;
    326 {
    327 	struct pcic_handle *h = (struct pcic_handle *) pch;
    328 	int irq, ist;
    329 	void *ih;
    330 	int reg;
    331 
    332 	if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
    333 		ist = IST_LEVEL;
    334 	else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
    335 		ist = IST_PULSE;
    336 	else
    337 		ist = IST_LEVEL;
    338 
    339 	if (isa_intr_alloc(h->sc->intr_est,
    340 	    PCIC_INTR_IRQ_VALIDMASK & pcic_intr_alloc_mask, ist, &irq))
    341 		return (NULL);
    342 	if ((ih = isa_intr_establish(h->sc->intr_est, irq, ist, ipl,
    343 	    fct, arg)) == NULL)
    344 		return (NULL);
    345 
    346 	reg = pcic_read(h, PCIC_INTR);
    347 	reg &= ~PCIC_INTR_IRQ_MASK;
    348 	reg |= irq;
    349 	pcic_write(h, PCIC_INTR, reg);
    350 
    351 	h->ih_irq = irq;
    352 
    353 	printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq);
    354 
    355 	return (ih);
    356 }
    357 
    358 void
    359 pcic_isa_chip_intr_disestablish(pch, ih)
    360 	pcmcia_chipset_handle_t pch;
    361 	void *ih;
    362 {
    363 	struct pcic_handle *h = (struct pcic_handle *) pch;
    364 	int reg;
    365 
    366 	h->ih_irq = 0;
    367 
    368 	reg = pcic_read(h, PCIC_INTR);
    369 	reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
    370 	pcic_write(h, PCIC_INTR, reg);
    371 
    372 	isa_intr_disestablish(h->sc->intr_est, ih);
    373 }
    374