Home | History | Annotate | Line # | Download | only in dev
if_ie_obio.c revision 1.2
      1 /*	$NetBSD: if_ie_obio.c,v 1.2 1998/02/07 17:28:18 chs Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      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 the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1995 Charles D. Cranor
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *      This product includes software developed by Charles D. Cranor.
     54  * 4. The name of the author may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 
     70 
     71 /*
     72  * Sparc OBIO front-end for the Intel 82586 Ethernet driver
     73  *
     74  * Converted to SUN ie driver by Charles D. Cranor,
     75  *		October 1994, January 1995.
     76  */
     77 
     78 /*
     79  * The i82586 is a very painful chip, found in sun3's, sun-4/100's
     80  * sun-4/200's, and VME based suns.  The byte order is all wrong for a
     81  * SUN, making life difficult.  Programming this chip is mostly the same,
     82  * but certain details differ from system to system.  This driver is
     83  * written so that different "ie" interfaces can be controled by the same
     84  * driver.
     85  */
     86 
     87 #include <sys/param.h>
     88 #include <sys/systm.h>
     89 #include <sys/errno.h>
     90 #include <sys/device.h>
     91 #include <sys/malloc.h>
     92 #include <sys/protosw.h>
     93 #include <sys/socket.h>
     94 
     95 #include <net/if.h>
     96 #include <net/if_types.h>
     97 #include <net/if_dl.h>
     98 #include <net/if_media.h>
     99 #include <net/if_ether.h>
    100 
    101 #include <vm/vm.h>
    102 #ifdef UVM
    103 #include <uvm/uvm.h>
    104 #endif
    105 
    106 #include <machine/autoconf.h>
    107 #include <machine/cpu.h>
    108 #include <machine/pmap.h>
    109 #include <machine/bus.h>
    110 
    111 #define _NEW_I82586     /* remove after all old drivers are converted */
    112 #include <dev/ic/i82586reg.h>
    113 #include <dev/ic/i82586var.h>
    114 
    115 /*
    116  * the on-board interface
    117  */
    118 struct ieob {
    119 	u_char  obctrl;
    120 };
    121 #define IEOB_NORSET 0x80	/* don't reset the board */
    122 #define IEOB_ONAIR  0x40	/* put us on the air */
    123 #define IEOB_ATTEN  0x20	/* attention! */
    124 #define IEOB_IENAB  0x10	/* interrupt enable */
    125 #define IEOB_XXXXX  0x08	/* free bit */
    126 #define IEOB_XCVRL2 0x04	/* level 2 transceiver? */
    127 #define IEOB_BUSERR 0x02	/* bus error */
    128 #define IEOB_INT    0x01	/* interrupt */
    129 
    130 #define IEOB_ADBASE 0xff000000  /* KVA base addr of 24 bit address space */
    131 
    132 
    133 static void ie_obreset __P((struct ie_softc *, int));
    134 static void ie_obattend __P((struct ie_softc *));
    135 static void ie_obrun __P((struct ie_softc *));
    136 
    137 vm_map_t ie_map; /* XXX - needs change */
    138 
    139 int ie_obio_match __P((struct device *, struct cfdata *, void *));
    140 void ie_obio_attach __P((struct device *, struct device *, void *));
    141 
    142 struct cfattach ie_obio_ca = {
    143 	sizeof(struct ie_softc), ie_obio_match, ie_obio_attach
    144 };
    145 
    146 /* Supported media */
    147 static int media[] = {
    148 	IFM_ETHER | IFM_10_2,
    149 };
    150 #define NMEDIA	(sizeof(media) / sizeof(media[0]))
    151 
    152 
    153 /*
    154  * OBIO ie support routines
    155  */
    156 void
    157 ie_obreset(sc, what)
    158 	struct ie_softc *sc;
    159 {
    160 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
    161 	ieo->obctrl = 0;
    162 	delay(100);			/* XXX could be shorter? */
    163 	ieo->obctrl = IEOB_NORSET;
    164 }
    165 void
    166 ie_obattend(sc)
    167 	struct ie_softc *sc;
    168 {
    169 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
    170 
    171 	ieo->obctrl |= IEOB_ATTEN;	/* flag! */
    172 	ieo->obctrl &= ~IEOB_ATTEN;	/* down. */
    173 }
    174 
    175 void
    176 ie_obrun(sc)
    177 	struct ie_softc *sc;
    178 {
    179 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
    180 
    181 	ieo->obctrl |= (IEOB_ONAIR|IEOB_IENAB|IEOB_NORSET);
    182 }
    183 
    184 void ie_obio_memcopyin __P((struct ie_softc *, void *, int, size_t));
    185 void ie_obio_memcopyout __P((struct ie_softc *, const void *, int, size_t));
    186 
    187 /*
    188  * Copy board memory to kernel.
    189  */
    190 void
    191 ie_obio_memcopyin(sc, p, offset, size)
    192 	struct ie_softc	*sc;
    193 	void *p;
    194 	int offset;
    195 	size_t size;
    196 {
    197 	void *addr = (void *)((u_long)sc->bh + offset);/*XXX - not MI!*/
    198 	wcopy(addr, p, size);
    199 }
    200 
    201 /*
    202  * Copy from kernel space to naord memory.
    203  */
    204 void
    205 ie_obio_memcopyout(sc, p, offset, size)
    206 	struct ie_softc	*sc;
    207 	const void *p;
    208 	int offset;
    209 	size_t size;
    210 {
    211 	void *addr = (void *)((u_long)sc->bh + offset);/*XXX - not MI!*/
    212 	wcopy(p, addr, size);
    213 }
    214 
    215 /* read a 16-bit value at BH offset */
    216 u_int16_t ie_obio_read16 __P((struct ie_softc *, int offset));
    217 /* write a 16-bit value at BH offset */
    218 void ie_obio_write16 __P((struct ie_softc *, int offset, u_int16_t value));
    219 void ie_obio_write24 __P((struct ie_softc *, int offset, int addr));
    220 
    221 u_int16_t
    222 ie_obio_read16(sc, offset)
    223 	struct ie_softc *sc;
    224 	int offset;
    225 {
    226 	u_int16_t v = bus_space_read_2(sc->bt, sc->bh, offset);
    227 	return (((v&0xff)<<8) | ((v>>8)&0xff));
    228 }
    229 
    230 void
    231 ie_obio_write16(sc, offset, v)
    232 	struct ie_softc *sc;
    233 	int offset;
    234 	u_int16_t v;
    235 {
    236 	v = (((v&0xff)<<8) | ((v>>8)&0xff));
    237 	bus_space_write_2(sc->bt, sc->bh, offset, v);
    238 }
    239 
    240 void
    241 ie_obio_write24(sc, offset, addr)
    242 	struct ie_softc *sc;
    243 	int offset;
    244 	int addr;
    245 {
    246 	u_int32_t v;
    247 	u_char *t = (u_char *)&v, *f = (u_char *)&addr;
    248 
    249 	t[0] = f[3]; t[1] = f[2]; t[2] = f[1]; t[3] = 0;
    250 	bus_space_write_4(sc->bt, sc->bh, offset, v);
    251 }
    252 
    253 int
    254 ie_obio_match(parent, cf, aux)
    255 	struct device *parent;
    256 	struct cfdata *cf;
    257 	void *aux;
    258 {
    259 	struct confargs *ca = aux;
    260 	struct romaux *ra = &ca->ca_ra;
    261 
    262 	if (strcmp(cf->cf_driver->cd_name, ra->ra_name)) /* correct name? */
    263 		return (0);
    264 
    265 	switch (ca->ca_bustype) {
    266 	default:
    267 		return (0);
    268 	case BUS_OBIO:
    269 		if (probeget(ra->ra_vaddr, 1) != -1)
    270 			return (1);
    271 		break;
    272 	}
    273 	return (0);
    274 }
    275 
    276 void
    277 ie_obio_attach(parent, self, aux)
    278 	struct device *parent;
    279 	struct device *self;
    280 	void   *aux;
    281 {
    282 	u_int8_t myaddr[ETHER_ADDR_LEN];
    283 	extern void myetheraddr(u_char *);	/* should be elsewhere */
    284 	struct ie_softc *sc = (void *) self;
    285 	struct confargs *ca = aux;
    286 	struct intrhand *ih;
    287 	register struct bootpath *bp;
    288 	volatile struct ieob *ieo;
    289 	vm_offset_t pa;
    290 
    291 	sc->bt = 0;
    292 
    293 	sc->hwreset = ie_obreset;
    294 	sc->chan_attn = ie_obattend;
    295 	sc->hwinit = ie_obrun;
    296 	sc->memcopyout = ie_obio_memcopyout;
    297 	sc->memcopyin = ie_obio_memcopyin;
    298 	sc->ie_bus_read16 = ie_obio_read16;
    299 	sc->ie_bus_write16 = ie_obio_write16;
    300 	sc->ie_bus_write24 = ie_obio_write24;
    301 	sc->sc_msize = 65536; /* XXX */
    302 
    303 	sc->sc_reg = mapiodev(ca->ca_ra.ra_reg, 0, sizeof(struct ieob));
    304 	ieo = (volatile struct ieob *) sc->sc_reg;
    305 
    306 	/*
    307 	 * the rest of the IE_OBIO case needs to be cleaned up
    308 	 * XXX-should provide bus support for 24-bit devices..
    309 	 */
    310 
    311 #ifdef UVM
    312 	ie_map = uvm_map_create(pmap_kernel(), (vm_offset_t)IEOB_ADBASE,
    313 		(vm_offset_t)IEOB_ADBASE + sc->sc_msize, TRUE);
    314 #else
    315 	ie_map = vm_map_create(pmap_kernel(), (vm_offset_t)IEOB_ADBASE,
    316 		(vm_offset_t)IEOB_ADBASE + sc->sc_msize, 1);
    317 #endif
    318 	if (ie_map == NULL)
    319 		panic("ie_map");
    320 
    321 #ifdef UVM
    322 	sc->sc_maddr = (caddr_t) uvm_km_alloc(ie_map, sc->sc_msize);
    323 #else
    324 	sc->sc_maddr = (caddr_t) kmem_alloc(ie_map, sc->sc_msize);
    325 #endif
    326 	if (sc->sc_maddr == NULL)
    327 		panic("ie kmem_alloc");
    328 
    329 	sc->bh = (bus_space_handle_t)(sc->sc_maddr);
    330 
    331 	kvm_uncache(sc->sc_maddr, sc->sc_msize >> PGSHIFT);
    332 	if (((u_long)sc->sc_maddr & ~(NBPG-1)) != (u_long)sc->sc_maddr)
    333 		panic("unaligned dvmamalloc breaks");
    334 
    335 	sc->sc_iobase = (caddr_t)IEOB_ADBASE; /* 24 bit base addr */
    336 	wzero(sc->sc_maddr, sc->sc_msize);
    337 
    338 	/* Map iscp at location zero */
    339 	sc->iscp = (int)sc->bh;
    340 
    341 	/* scb follows iscp */
    342 	sc->scb = IE_ISCP_SZ;
    343 
    344 	ie_obio_write16(sc, IE_ISCP_SCB((u_long)sc->iscp), sc->scb);
    345 	ie_obio_write24(sc, IE_ISCP_BASE((u_long)sc->iscp),
    346 			    (u_long)IEOB_ADBASE - (u_long)sc->bh);
    347 	ie_obio_write24(sc, IE_SCP_ISCP((u_long)sc->scp),
    348 			    (u_long)IEOB_ADBASE - (u_long)sc->iscp);
    349 
    350 	/*
    351 	 * SCP: the scp must appear at KVA IEOB_ADBASE.  The
    352 	 * ROM seems to have page up there, but I'm not sure all
    353 	 * ROMs will have it there.  Also, I'm not sure if that
    354 	 * page is on some free list somewhere or not.   Let's
    355 	 * map the first page of the buffer we just allocated
    356 	 * to IEOB_ADBASE to be safe.
    357 	 */
    358 
    359 	pa = pmap_extract(pmap_kernel(), (vm_offset_t)sc->sc_maddr);
    360 	if (pa == 0)
    361 		panic("ie pmap_extract");
    362 
    363 	pmap_enter(pmap_kernel(), trunc_page(IEOB_ADBASE+IE_SCP_ADDR),
    364 	    (vm_offset_t)pa | PMAP_NC /*| PMAP_IOC*/,
    365 	    VM_PROT_READ | VM_PROT_WRITE, 1);
    366 
    367 	sc->scp = IEOB_ADBASE + IE_SCP_ADDR;
    368 
    369 	/*
    370 	 * Rest of first page is unused (wasted!); rest of ram
    371 	 * for buffers.
    372 	 */
    373 	sc->buf_area = NBPG;
    374 	sc->buf_area_sz = sc->sc_msize - NBPG;
    375 
    376 	myetheraddr(myaddr);
    377 	i82586_attach(sc, "onboard", myaddr, media, NMEDIA, media[0]);
    378 
    379 	ih = malloc(sizeof *ih, M_DEVBUF, M_NOWAIT);
    380 	if (ih == NULL)
    381 		panic("ie_obio: can't malloc interrupt handler");
    382 
    383 	ih->ih_fun = i82586_intr;
    384 	ih->ih_arg = sc;
    385 	intr_establish(ca->ca_ra.ra_intr[0].int_pri, ih);
    386 
    387 	bp = ca->ca_ra.ra_bp;
    388 	if (bp != NULL && strcmp(bp->name, "ie") == 0 &&
    389 	    sc->sc_dev.dv_unit == bp->val[1])
    390 		bp->dev = &sc->sc_dev;
    391 }
    392