Home | History | Annotate | Line # | Download | only in vme
if_le_vme.c revision 1.5
      1 /*	$NetBSD: if_le_vme.c,v 1.5 1998/07/05 00:51:08 jonathan Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 Leo Weppelman.  All rights reserved.
      5  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
      6  * Copyright (c) 1992, 1993
      7  *	The Regents of the University of California.  All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * Ralph Campbell and Rick Macklem.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
     41  */
     42 
     43 #include "opt_inet.h"
     44 #include "bpfilter.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/syslog.h>
     50 #include <sys/socket.h>
     51 #include <sys/device.h>
     52 
     53 #include <net/if.h>
     54 #include <net/if_media.h>
     55 #include <net/if_ether.h>
     56 
     57 #ifdef INET
     58 #include <netinet/in.h>
     59 #include <netinet/if_inarp.h>
     60 #endif
     61 
     62 #include <machine/cpu.h>
     63 #include <machine/bus.h>
     64 #include <machine/iomap.h>
     65 #include <machine/scu.h>
     66 
     67 #include <atari/atari/device.h>
     68 #include <atari/atari/intr.h>
     69 
     70 #include <dev/ic/am7990reg.h>
     71 #include <dev/ic/am7990var.h>
     72 
     73 #include <atari/vme/vmevar.h>
     74 #include <atari/vme/if_levar.h>
     75 
     76 struct le_addresses {
     77 	u_long	reg_addr;
     78 	u_long	mem_addr;
     79 	int	irq;
     80 } lestd[] = {
     81 	{ 0xfe00fff0, 0xfe010000, IRQUNK },	/* Riebl VME	*/
     82 	{ 0xffcffff0, 0xffcf0000,      5 },	/* PAM VME	*/
     83 	{ 0xfecffff0, 0xfecf0000,      5 }	/* Rhotron VME	*/
     84 };
     85 
     86 #define	NLESTD	(sizeof(lestd) / sizeof(lestd[0]))
     87 
     88 /*
     89  * All cards have 64KB RAM. However.... On the Riebl cards the area
     90  * between the offsets 0xee70-0xeec0 is used to store config data.
     91  */
     92 #define	MEMSIZE	(64*1024)
     93 
     94 /*
     95  * Default mac for RIEBL cards without a (working) battery. The first 4 bytes
     96  * are the manufacturer id.
     97  */
     98 static u_char riebl_def_mac[] = {
     99 	0x00, 0x00, 0x36, 0x04, 0x00, 0x00
    100 };
    101 
    102 static int le_intr __P((struct le_softc *, int));
    103 static void lepseudointr __P((struct le_softc *, void *));
    104 static int le_vme_match __P((struct device *, struct cfdata *, void *));
    105 static void le_vme_attach __P((struct device *, struct device *, void *));
    106 static int probe_addresses __P((bus_space_tag_t *, bus_space_tag_t *,
    107 				bus_space_handle_t *, bus_space_handle_t *));
    108 static void riebl_skip_reserved_area __P((struct am7990_softc *));
    109 
    110 struct cfattach le_vme_ca = {
    111 	sizeof(struct le_softc), le_vme_match, le_vme_attach
    112 };
    113 
    114 hide void lewrcsr __P((struct am7990_softc *, u_int16_t, u_int16_t));
    115 hide u_int16_t lerdcsr __P((struct am7990_softc *, u_int16_t));
    116 
    117 hide void
    118 lewrcsr(sc, port, val)
    119 	struct am7990_softc	*sc;
    120 	u_int16_t		port, val;
    121 {
    122 	struct le_softc		*lesc = (struct le_softc *)sc;
    123 	int			s;
    124 
    125 	s = splhigh();
    126 	bus_space_write_2(lesc->sc_iot, lesc->sc_ioh, LER_RAP, port);
    127 	bus_space_write_2(lesc->sc_iot, lesc->sc_ioh, LER_RDP, val);
    128 	splx(s);
    129 }
    130 
    131 hide u_int16_t
    132 lerdcsr(sc, port)
    133 	struct am7990_softc	*sc;
    134 	u_int16_t		port;
    135 {
    136 	struct le_softc		*lesc = (struct le_softc *)sc;
    137 	u_int16_t		val;
    138 	int			s;
    139 
    140 	s = splhigh();
    141 	bus_space_write_2(lesc->sc_iot, lesc->sc_ioh, LER_RAP, port);
    142 	val = bus_space_read_2(lesc->sc_iot, lesc->sc_ioh, LER_RDP);
    143 	splx(s);
    144 
    145 	return (val);
    146 }
    147 
    148 static int
    149 le_vme_match(parent, cfp, aux)
    150 	struct device	*parent;
    151 	struct cfdata	*cfp;
    152 	void		*aux;
    153 {
    154 	struct vme_attach_args	*va = aux;
    155 	int			i;
    156 	bus_space_tag_t		iot;
    157 	bus_space_tag_t		memt;
    158 	bus_space_handle_t	ioh;
    159 	bus_space_handle_t	memh;
    160 
    161 	iot  = va->va_iot;
    162 	memt = va->va_memt;
    163 
    164 	for (i = 0; i < NLESTD; i++) {
    165 		struct le_addresses	*le_ap = &lestd[i];
    166 		int			found  = 0;
    167 
    168 		if ((va->va_iobase != IOBASEUNK)
    169 		     && (va->va_iobase != le_ap->reg_addr))
    170 			continue;
    171 
    172 		if ((va->va_maddr != MADDRUNK)
    173 		     && (va->va_maddr != le_ap->mem_addr))
    174 			continue;
    175 
    176 		if ((le_ap->irq != IRQUNK) && (va->va_irq != le_ap->irq))
    177 			continue;
    178 
    179 		if (bus_space_map(iot, le_ap->reg_addr, 16, 0, &ioh)) {
    180 			printf("leprobe: cannot map io-area\n");
    181 			return (0);
    182 		}
    183 		if (bus_space_map(memt, le_ap->mem_addr, MEMSIZE, 0, &memh)) {
    184 			bus_space_unmap(iot, (caddr_t)le_ap->reg_addr, 16);
    185 			printf("leprobe: cannot map memory-area\n");
    186 			return (0);
    187 		}
    188 		found = probe_addresses(&iot, &memt, &ioh, &memh);
    189 		bus_space_unmap(iot, (caddr_t)le_ap->reg_addr, 16);
    190 		bus_space_unmap(memt, (caddr_t)le_ap->mem_addr, 8*NBPG);
    191 
    192 		if (found) {
    193 			va->va_iobase = le_ap->reg_addr;
    194 			va->va_iosize = 16;
    195 			va->va_maddr  = le_ap->mem_addr;
    196 			va->va_msize  = MEMSIZE;
    197 			if (va->va_irq == IRQUNK)
    198 				va->va_irq = le_ap->irq;
    199 			return 1;
    200 		}
    201     }
    202     return (0);
    203 }
    204 
    205 static int
    206 probe_addresses(iot, memt, ioh, memh)
    207 bus_space_tag_t		*iot;
    208 bus_space_tag_t		*memt;
    209 bus_space_handle_t	*ioh;
    210 bus_space_handle_t	*memh;
    211 {
    212 	/*
    213 	 * Test accesibility of register and memory area
    214 	 */
    215 	if(!bus_space_peek_2(*iot, *ioh, LER_RDP))
    216 		return 0;
    217 	if(!bus_space_peek_1(*memt, *memh, 0))
    218 		return 0;
    219 
    220 	/*
    221 	 * Test for writable memory
    222 	 */
    223 	bus_space_write_2(*memt, *memh, 0, 0xa5a5);
    224 	if (bus_space_read_2(*memt, *memh, 0) != 0xa5a5)
    225 		return 0;
    226 
    227 	/*
    228 	 * Test writability of selector port.
    229 	 */
    230 	bus_space_write_2(*iot, *ioh, LER_RAP, LE_CSR1);
    231 	if (bus_space_read_2(*iot, *ioh, LER_RAP) != LE_CSR1)
    232 		return 0;
    233 
    234 	/*
    235 	 * Do a small register test
    236 	 */
    237 	bus_space_write_2(*iot, *ioh, LER_RAP, LE_CSR0);
    238 	bus_space_write_2(*iot, *ioh, LER_RDP, LE_C0_INIT | LE_C0_STOP);
    239 	if (bus_space_read_2(*iot, *ioh, LER_RDP) != LE_C0_STOP)
    240 		return 0;
    241 
    242 	bus_space_write_2(*iot, *ioh, LER_RDP, LE_C0_STOP);
    243 	if (bus_space_read_2(*iot, *ioh, LER_RDP) != LE_C0_STOP)
    244 		return 0;
    245 
    246 	return 1;
    247 }
    248 
    249 /*
    250  * Interrupt mess. Because the card's interrupt is hardwired to either
    251  * ipl5 or ipl3 (mostly on ipl5) and raising splnet to spl5() just won't do
    252  * (it kills the serial at the least), we use a 2-level interrupt sceme. The
    253  * card interrupt is routed to 'le_intr'. If the previous ipl was below
    254  * splnet, just call the mi-function. If not, save the interrupt status,
    255  * turn off card interrupts (the card is *very* persistent) and arrange
    256  * for a softint 'callback' through 'lepseudointr'.
    257  */
    258 static int
    259 le_intr(lesc, sr)
    260 	struct le_softc	*lesc;
    261 	int		 sr;
    262 {
    263 	struct am7990_softc	*sc = &lesc->sc_am7990;
    264 	u_int16_t		csr0;
    265 
    266 	if ((sr & PSL_IPL) < IPL_NET)
    267 		am7990_intr(sc);
    268 	else {
    269 		sc->sc_saved_csr0 = csr0 = lerdcsr(sc, LE_CSR0);
    270 		lewrcsr(sc, LE_CSR0, csr0 & ~LE_C0_INEA);
    271 		add_sicallback((si_farg)lepseudointr, lesc, sc);
    272 	}
    273 	return 1;
    274 }
    275 
    276 
    277 static void
    278 lepseudointr(lesc, sc)
    279 struct le_softc	*lesc;
    280 void		*sc;
    281 {
    282 	int	s;
    283 
    284 	s = splx(lesc->sc_splval);
    285 	am7990_intr(sc);
    286 	splx(s);
    287 }
    288 
    289 static void
    290 le_vme_attach(parent, self, aux)
    291 	struct device *parent, *self;
    292 	void *aux;
    293 {
    294 	struct le_softc		*lesc = (struct le_softc *)self;
    295 	struct am7990_softc	*sc = &lesc->sc_am7990;
    296 	struct vme_attach_args	*va = aux;
    297 	bus_space_handle_t	ioh;
    298 	bus_space_handle_t	memh;
    299 	int			i;
    300 
    301 	printf("\n%s: ", sc->sc_dev.dv_xname);
    302 
    303 	if (bus_space_map(va->va_iot, va->va_iobase, va->va_iosize, 0, &ioh))
    304 		panic("leattach: cannot map io-area\n");
    305 	if (bus_space_map(va->va_memt, va->va_maddr, va->va_msize, 0, &memh))
    306 		panic("leattach: cannot map mem-area\n");
    307 
    308 	lesc->sc_iot    = va->va_iot;
    309 	lesc->sc_ioh    = ioh;
    310 	lesc->sc_memt   = va->va_memt;
    311 	lesc->sc_memh   = memh;
    312 	lesc->sc_splval = (va->va_irq << 8) | PSL_S; /* XXX */
    313 
    314 	/*
    315 	 * Go on to find board type
    316 	 */
    317 	if (bus_space_peek_1(va->va_iot, ioh, LER_EEPROM)) {
    318 		printf("PAM card");
    319 		lesc->sc_type = LE_PAM;
    320 		bus_space_read_1(va->va_iot, ioh, LER_MEME);
    321 	}
    322 	else {
    323 		printf("Riebl card");
    324 		if(bus_space_read_4(va->va_memt, memh, RIEBL_MAGIC_ADDR)
    325 								== RIEBL_MAGIC)
    326 			lesc->sc_type = LE_NEW_RIEBL;
    327 		else {
    328 			printf("(without battery) ");
    329 			lesc->sc_type = LE_OLD_RIEBL;
    330 		}
    331 	}
    332 
    333 	sc->sc_copytodesc   = am7990_copytobuf_contig;
    334 	sc->sc_copyfromdesc = am7990_copyfrombuf_contig;
    335 	sc->sc_copytobuf    = am7990_copytobuf_contig;
    336 	sc->sc_copyfrombuf  = am7990_copyfrombuf_contig;
    337 	sc->sc_zerobuf      = am7990_zerobuf_contig;
    338 
    339 	sc->sc_rdcsr   = lerdcsr;
    340 	sc->sc_wrcsr   = lewrcsr;
    341 	sc->sc_hwinit  = NULL;
    342 	sc->sc_conf3   = LE_C3_BSWP;
    343 	sc->sc_addr    = 0;
    344 	sc->sc_memsize = va->va_msize;
    345 	sc->sc_mem     = (void *)memh; /* XXX */
    346 
    347 	/*
    348 	 * Get MAC address
    349 	 */
    350 	switch (lesc->sc_type) {
    351 	    case LE_OLD_RIEBL:
    352 		bcopy(riebl_def_mac, sc->sc_enaddr,
    353 					sizeof(sc->sc_enaddr));
    354 		break;
    355 	    case LE_NEW_RIEBL:
    356 		for (i = 0; i < sizeof(sc->sc_enaddr); i++)
    357 		    sc->sc_enaddr[i] =
    358 			bus_space_read_1(va->va_memt, memh, i + RIEBL_MAC_ADDR);
    359 			break;
    360 	    case LE_PAM:
    361 		i = bus_space_read_1(va->va_iot, ioh, LER_EEPROM);
    362 		for (i = 0; i < sizeof(sc->sc_enaddr); i++) {
    363 		    sc->sc_enaddr[i] =
    364 			(bus_space_read_2(va->va_memt, memh, 2 * i) << 4) |
    365 			(bus_space_read_2(va->va_memt, memh, 2 * i + 1) & 0xf);
    366 		}
    367 		i = bus_space_read_1(va->va_iot, ioh, LER_MEME);
    368 		break;
    369 	}
    370 
    371 	am7990_config(sc);
    372 
    373 	if ((lesc->sc_type == LE_OLD_RIEBL) || (lesc->sc_type == LE_NEW_RIEBL))
    374 		riebl_skip_reserved_area(sc);
    375 
    376 	/*
    377 	 * XXX: We always use uservector 64....
    378 	 */
    379 	if ((lesc->sc_intr = intr_establish(64, USER_VEC, 0,
    380 				(hw_ifun_t)le_intr, lesc)) == NULL) {
    381 		printf("le_vme_attach: Can't establish interrupt\n");
    382 		return;
    383 	}
    384 
    385 	/*
    386 	 * Notify the card of the vector
    387 	 */
    388 	switch (lesc->sc_type) {
    389 		case LE_OLD_RIEBL:
    390 		case LE_NEW_RIEBL:
    391 			bus_space_write_2(va->va_memt, memh, RIEBL_IVEC_ADDR,
    392 								64 + 64);
    393 			break;
    394 		case LE_PAM:
    395 			bus_space_write_1(va->va_iot, ioh, LER_IVEC, 64 + 64);
    396 			break;
    397 	}
    398 
    399 	/*
    400 	 * Unmask the VME-interrupt we're on
    401 	 */
    402 	if (machineid & ATARI_TT)
    403 		SCU->vme_mask |= 1 << va->va_irq;
    404 }
    405 
    406 /*
    407  * True if 'addr' containe within [start,len]
    408  */
    409 #define WITHIN(start, len, addr)	\
    410 		((addr >= start) && ((addr) <= ((start) + (len))))
    411 static void
    412 riebl_skip_reserved_area(sc)
    413 	struct am7990_softc	*sc;
    414 {
    415 	int	offset = 0;
    416 	int	i;
    417 
    418 	for(i = 0; i < sc->sc_nrbuf; i++) {
    419 		if (WITHIN(sc->sc_rbufaddr[i], LEBLEN, RIEBL_RES_START)
    420 		    || WITHIN(sc->sc_rbufaddr[i], LEBLEN, RIEBL_RES_END)) {
    421 			offset = RIEBL_RES_END - sc->sc_rbufaddr[i];
    422 		}
    423 		sc->sc_rbufaddr[i] += offset;
    424 	}
    425 
    426 	for(i = 0; i < sc->sc_ntbuf; i++) {
    427 		if (WITHIN(sc->sc_tbufaddr[i], LEBLEN, RIEBL_RES_START)
    428 		    || WITHIN(sc->sc_tbufaddr[i], LEBLEN, RIEBL_RES_END)) {
    429 			offset = RIEBL_RES_END - sc->sc_tbufaddr[i];
    430 		}
    431 		sc->sc_tbufaddr[i] += offset;
    432 	}
    433 }
    434