Home | History | Annotate | Line # | Download | only in isa
i82365_isa.c revision 1.3
      1 /*	$NetBSD: i82365_isa.c,v 1.3 1997/10/19 18:56:55 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 		panic("pcic_isa_attach: can't map i/o space");
    194 
    195 	/* Map mem space. */
    196 	if (bus_space_map(memt, ia->ia_maddr, ia->ia_msize, 0, &memh))
    197 		panic("pcic_isa_attach: can't map i/o space");
    198 
    199 	sc->membase = ia->ia_maddr;
    200 	sc->subregionmask = (1 << (ia->ia_msize / PCIC_MEM_PAGESIZE)) - 1;
    201 
    202 	sc->intr_est = ic;
    203 	sc->pct = (pcmcia_chipset_tag_t) & pcic_isa_functions;
    204 
    205 	sc->iot = iot;
    206 	sc->ioh = ioh;
    207 	sc->memt = memt;
    208 	sc->memh = memh;
    209 
    210 	/*
    211 	 * allocate an irq.  it will be used by both controllers.  I could
    212 	 * use two different interrupts, but interrupts are relatively
    213 	 * scarce, shareable, and for PCIC controllers, very infrequent.
    214 	 */
    215 
    216 	if ((sc->irq = ia->ia_irq) == IRQUNK) {
    217 		/* XXX CHECK RETURN VALUE */
    218 		(void) isa_intr_alloc(ic, PCIC_CSC_INTR_IRQ_VALIDMASK,
    219 		    IST_EDGE, &sc->irq);
    220 		printf(": using irq %d", sc->irq);
    221 	}
    222 	printf("\n");
    223 
    224 	pcic_attach(sc);
    225 
    226 	/*
    227 	 * figure out how wide the isa bus is.  Do this by checking if the
    228 	 * pcic controller is mirrored 0x400 above where we expect it to be.
    229 	 */
    230 
    231 	iobuswidth = 12;
    232 
    233 	/* Map i/o space. */
    234 	if (bus_space_map(iot, ia->ia_iobase + 0x400, ia->ia_iosize, 0,
    235 	    &ioh_high))
    236 		panic("pcic_isa_attach: can't map high i/o space");
    237 
    238 	for (i = 0; i < PCIC_NSLOTS; i++) {
    239 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP) {
    240 			/*
    241 			 * read the ident flags from the normal space and
    242 			 * from the mirror, and compare them
    243 			 */
    244 
    245 			bus_space_write_1(iot, ioh, PCIC_REG_INDEX,
    246 			    sc->handle[i].sock + PCIC_IDENT);
    247 			tmp1 = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
    248 
    249 			bus_space_write_1(iot, ioh_high, PCIC_REG_INDEX,
    250 			    sc->handle[i].sock + PCIC_IDENT);
    251 			tmp2 = bus_space_read_1(iot, ioh_high, PCIC_REG_DATA);
    252 
    253 			if (tmp1 == tmp2)
    254 				iobuswidth = 10;
    255 		}
    256 	}
    257 
    258 	bus_space_free(iot, ioh_high, ia->ia_iosize);
    259 
    260 	/*
    261 	 * XXX mycroft recommends I/O space range 0x400-0xfff .  I should put
    262 	 * this in a header somewhere
    263 	 */
    264 
    265 	/*
    266 	 * XXX some hardware doesn't seem to grok addresses in 0x400 range--
    267 	 * apparently missing a bit or more of address lines. (e.g.
    268 	 * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
    269 	 * TravelMate 5000--not clear which is at fault)
    270 	 *
    271 	 * Add a kludge to detect 10 bit wide buses and deal with them,
    272 	 * and also a config file option to override the probe.
    273 	 */
    274 
    275 	if (iobuswidth == 10) {
    276 		sc->iobase = 0x300;
    277 		sc->iosize = 0x0ff;
    278 	} else {
    279 		sc->iobase = 0x400;
    280 		sc->iosize = 0xbff;
    281 	}
    282 
    283 	DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
    284 	    sc->dev.dv_xname, (long) sc->iobase,
    285 	    (long) sc->iobase + sc->iosize));
    286 
    287 	if (pcic_isa_alloc_iobase && pcic_isa_alloc_iosize) {
    288 		sc->iobase = pcic_isa_alloc_iobase;
    289 		sc->iosize = pcic_isa_alloc_iosize;
    290 
    291 		DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx "
    292 		    "(config override)\n", sc->dev.dv_xname, (long) sc->iobase,
    293 		    (long) sc->iobase + sc->iosize));
    294 	}
    295 	sc->ih = isa_intr_establish(ic, sc->irq, IST_EDGE, IPL_TTY,
    296 	    pcic_intr, sc);
    297 
    298 	pcic_attach_sockets(sc);
    299 }
    300 
    301 /*
    302  * allow patching or kernel option file override of available IRQs. Useful if
    303  * order of probing would screw up other devices, or if PCIC hardware/cards
    304  * have trouble with certain interrupt lines.
    305  */
    306 
    307 #ifndef PCIC_INTR_ALLOC_MASK
    308 #define	PCIC_INTR_ALLOC_MASK	0xffff
    309 #endif
    310 
    311 int	pcic_intr_alloc_mask = PCIC_INTR_ALLOC_MASK;
    312 
    313 void *
    314 pcic_isa_chip_intr_establish(pch, pf, ipl, fct, arg)
    315 	pcmcia_chipset_handle_t pch;
    316 	struct pcmcia_function *pf;
    317 	int ipl;
    318 	int (*fct) __P((void *));
    319 	void *arg;
    320 {
    321 	struct pcic_handle *h = (struct pcic_handle *) pch;
    322 	int irq, ist;
    323 	void *ih;
    324 	int reg;
    325 
    326 	if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
    327 		ist = IST_LEVEL;
    328 	else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
    329 		ist = IST_PULSE;
    330 	else
    331 		ist = IST_LEVEL;
    332 
    333 	if (isa_intr_alloc(h->sc->intr_est,
    334 	    PCIC_INTR_IRQ_VALIDMASK & pcic_intr_alloc_mask, ist, &irq))
    335 		return (NULL);
    336 	if ((ih = isa_intr_establish(h->sc->intr_est, irq, ist, ipl,
    337 	    fct, arg)) == NULL)
    338 		return (NULL);
    339 
    340 	reg = pcic_read(h, PCIC_INTR);
    341 	reg &= ~PCIC_INTR_IRQ_MASK;
    342 	reg |= irq;
    343 	pcic_write(h, PCIC_INTR, reg);
    344 
    345 	h->ih_irq = irq;
    346 
    347 	printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq);
    348 
    349 	return (ih);
    350 }
    351 
    352 void
    353 pcic_isa_chip_intr_disestablish(pch, ih)
    354 	pcmcia_chipset_handle_t pch;
    355 	void *ih;
    356 {
    357 	struct pcic_handle *h = (struct pcic_handle *) pch;
    358 	int reg;
    359 
    360 	h->ih_irq = 0;
    361 
    362 	reg = pcic_read(h, PCIC_INTR);
    363 	reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
    364 	pcic_write(h, PCIC_INTR, reg);
    365 
    366 	isa_intr_disestablish(h->sc->intr_est, ih);
    367 }
    368