Home | History | Annotate | Line # | Download | only in vme
if_ie_vme.c revision 1.2
      1 /*	$NetBSD: if_ie_vme.c,v 1.2 1998/01/25 19:57:27 pk Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995 Charles D. Cranor
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Charles D. Cranor.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Converted to SUN ie driver by Charles D. Cranor,
     35  *		October 1994, January 1995.
     36  */
     37 
     38 /*
     39  * The i82586 is a very painful chip, found in sun3's, sun-4/100's
     40  * sun-4/200's, and VME based suns.  The byte order is all wrong for a
     41  * SUN, making life difficult.  Programming this chip is mostly the same,
     42  * but certain details differ from system to system.  This driver is
     43  * written so that different "ie" interfaces can be controled by the same
     44  * driver.
     45  */
     46 
     47 /*
     48  * programming notes:
     49  *
     50  * the ie chip operates in a 24 bit address space.
     51  *
     52  * most ie interfaces appear to be divided into two parts:
     53  *	 - generic 586 stuff
     54  *	 - board specific
     55  *
     56  * generic:
     57  *	the generic stuff of the ie chip is all done with data structures
     58  * 	that live in the chip's memory address space.   the chip expects
     59  * 	its main data structure (the sys conf ptr -- SCP) to be at a fixed
     60  * 	address in its 24 bit space: 0xfffff4
     61  *
     62  *      the SCP points to another structure called the ISCP.
     63  *      the ISCP points to another structure called the SCB.
     64  * 	the SCB has a status field, a linked list of "commands", and
     65  * 	a linked list of "receive buffers".   these are data structures that
     66  * 	live in memory, not registers.
     67  *
     68  * board:
     69  * 	to get the chip to do anything, you first put a command in the
     70  * 	command data structure list.   then you have to signal "attention"
     71  * 	to the chip to get it to look at the command.   how you
     72  * 	signal attention depends on what board you have... on PC's
     73  * 	there is an i/o port number to do this, on sun's there is a
     74  * 	register bit you toggle.
     75  *
     76  * 	to get data from the chip you program it to interrupt...
     77  *
     78  *
     79  * sun issues:
     80  *
     81  *      there are 3 kinds of sun "ie" interfaces:
     82  *        1 - a VME/multibus card
     83  *        2 - an on-board interface (sun3's, sun-4/100's, and sun-4/200's)
     84  *        3 - another VME board called the 3E
     85  *
     86  * 	the VME boards lives in vme16 space.   only 16 and 8 bit accesses
     87  * 	are allowed, so functions that copy data must be aware of this.
     88  *
     89  * 	the chip is an intel chip.  this means that the byte order
     90  * 	on all the "short"s in the chip's data structures is wrong.
     91  * 	so, constants described in the intel docs are swapped for the sun.
     92  * 	that means that any buffer pointers you give the chip must be
     93  * 	swapped to intel format.   yuck.
     94  *
     95  *   VME/multibus interface:
     96  * 	for the multibus interface the board ignores the top 4 bits
     97  * 	of the chip address.   the multibus interface has its own
     98  * 	MMU like page map (without protections or valid bits, etc).
     99  * 	there are 256 pages of physical memory on the board (each page
    100  * 	is 1024 bytes).   There are 1024 slots in the page map.  so,
    101  * 	a 1024 byte page takes up 10 bits of address for the offset,
    102  * 	and if there are 1024 slots in the page that is another 10 bits
    103  * 	of the address.   That makes a 20 bit address, and as stated
    104  * 	earlier the board ignores the top 4 bits, so that accounts
    105  * 	for all 24 bits of address.
    106  *
    107  * 	Note that the last entry of the page map maps the top of the
    108  * 	24 bit address space and that the SCP is supposed to be at
    109  * 	0xfffff4 (taking into account allignment).   so,
    110  *	for multibus, that entry in the page map has to be used for the SCP.
    111  *
    112  * 	The page map effects BOTH how the ie chip sees the
    113  * 	memory, and how the host sees it.
    114  *
    115  * 	The page map is part of the "register" area of the board
    116  *
    117  *	The page map to control where ram appears in the address space.
    118  *	We choose to have RAM start at 0 in the 24 bit address space.
    119  *
    120  *	to get the phyiscal address of the board's RAM you must take the
    121  *	top 12 bits of the physical address of the register address and
    122  *	or in the 4 bits from the status word as bits 17-20 (remember that
    123  *	the board ignores the chip's top 4 address lines). For example:
    124  *	if the register is @ 0xffe88000, then the top 12 bits are 0xffe00000.
    125  *	to get the 4 bits from the the status word just do status & IEVME_HADDR.
    126  *	suppose the value is "4".   Then just shift it left 16 bits to get
    127  *	it into bits 17-20 (e.g. 0x40000).    Then or it to get the
    128  *	address of RAM (in our example: 0xffe40000).   see the attach routine!
    129  *
    130  *
    131  *   on-board interface:
    132  *
    133  *	on the onboard ie interface the 24 bit address space is hardwired
    134  *	to be 0xff000000 -> 0xffffffff of KVA.   this means that sc_iobase
    135  *	will be 0xff000000.   sc_maddr will be where ever we allocate RAM
    136  *	in KVA.    note that since the SCP is at a fixed address it means
    137  *	that we have to allocate a fixed KVA for the SCP.
    138  *	<fill in useful info later>
    139  *
    140  *
    141  *   VME3E interface:
    142  *
    143  *	<fill in useful info later>
    144  *
    145  */
    146 
    147 #include <sys/param.h>
    148 #include <sys/systm.h>
    149 #include <sys/errno.h>
    150 #include <sys/device.h>
    151 #include <sys/protosw.h>
    152 #include <sys/socket.h>
    153 
    154 #include <net/if.h>
    155 #include <net/if_types.h>
    156 #include <net/if_dl.h>
    157 #include <net/if_media.h>
    158 #include <net/if_ether.h>
    159 
    160 #include <vm/vm.h>
    161 
    162 #include <machine/bus.h>
    163 #include <dev/vme/vmevar.h>
    164 
    165 #define _NEW_I82586	/* remove after all old drivers are converted */
    166 #include <dev/ic/i82586reg.h>
    167 #include <dev/ic/i82586var.h>
    168 
    169 
    170 /*
    171  * VME/multibus definitions
    172  */
    173 #define IEVME_PAGESIZE 1024	/* bytes */
    174 #define IEVME_PAGSHIFT 10	/* bits */
    175 #define IEVME_NPAGES   256	/* number of pages on chip */
    176 #define IEVME_MAPSZ    1024	/* number of entries in the map */
    177 
    178 /*
    179  * PTE for the page map
    180  */
    181 #define IEVME_SBORDR 0x8000	/* sun byte order */
    182 #define IEVME_IBORDR 0x0000	/* intel byte ordr */
    183 
    184 #define IEVME_P2MEM  0x2000	/* memory is on P2 */
    185 #define IEVME_OBMEM  0x0000	/* memory is on board */
    186 
    187 #define IEVME_PGMASK 0x0fff	/* gives the physical page frame number */
    188 
    189 struct ievme {
    190 	u_int16_t	pgmap[IEVME_MAPSZ];
    191 	u_int16_t	xxx[32];	/* prom */
    192 	u_int16_t	status;		/* see below for bits */
    193 	u_int16_t	xxx2;		/* filler */
    194 	u_int16_t	pectrl;		/* parity control (see below) */
    195 	u_int16_t	peaddr;		/* low 16 bits of address */
    196 };
    197 
    198 /*
    199  * status bits
    200  */
    201 #define IEVME_RESET 0x8000	/* reset board */
    202 #define IEVME_ONAIR 0x4000	/* go out of loopback 'on-air' */
    203 #define IEVME_ATTEN 0x2000	/* attention */
    204 #define IEVME_IENAB 0x1000	/* interrupt enable */
    205 #define IEVME_PEINT 0x0800	/* parity error interrupt enable */
    206 #define IEVME_PERR  0x0200	/* parity error flag */
    207 #define IEVME_INT   0x0100	/* interrupt flag */
    208 #define IEVME_P2EN  0x0020	/* enable p2 bus */
    209 #define IEVME_256K  0x0010	/* 256kb rams */
    210 #define IEVME_HADDR 0x000f	/* mask for bits 17-20 of address */
    211 
    212 /*
    213  * parity control
    214  */
    215 #define IEVME_PARACK 0x0100	/* parity error ack */
    216 #define IEVME_PARSRC 0x0080	/* parity error source */
    217 #define IEVME_PAREND 0x0040	/* which end of the data got the error */
    218 #define IEVME_PARADR 0x000f	/* mask to get bits 17-20 of parity address */
    219 
    220 /* Supported media */
    221 static int media[] = {
    222 	IFM_ETHER | IFM_10_2,
    223 };
    224 #define NMEDIA	(sizeof(media) / sizeof(media[0]))
    225 
    226 /*
    227  * the 3E board not supported (yet?)
    228  */
    229 
    230 
    231 static void ie_vmereset __P((struct ie_softc *, int));
    232 static void ie_vmeattend __P((struct ie_softc *));
    233 static void ie_vmerun __P((struct ie_softc *));
    234 static int  ie_vmeintr __P((struct ie_softc *, int));
    235 
    236 int ie_vme_match __P((struct device *, struct cfdata *, void *));
    237 void ie_vme_attach __P((struct device *, struct device *, void *));
    238 
    239 struct cfattach ie_vme_ca = {
    240 	sizeof(struct ie_softc), ie_vme_match, ie_vme_attach
    241 };
    242 
    243 
    244 /*
    245  * MULTIBUS/VME support routines
    246  */
    247 void
    248 ie_vmereset(sc, what)
    249 	struct ie_softc *sc;
    250 	int what;
    251 {
    252 	volatile struct ievme *iev = (struct ievme *) sc->sc_reg;
    253 	iev->status = IEVME_RESET;
    254 	delay(100);		/* XXX could be shorter? */
    255 	iev->status = 0;
    256 }
    257 
    258 void
    259 ie_vmeattend(sc)
    260 	struct ie_softc *sc;
    261 {
    262 	volatile struct ievme *iev = (struct ievme *) sc->sc_reg;
    263 
    264 	iev->status |= IEVME_ATTEN;	/* flag! */
    265 	iev->status &= ~IEVME_ATTEN;	/* down. */
    266 }
    267 
    268 void
    269 ie_vmerun(sc)
    270 	struct ie_softc *sc;
    271 {
    272 	volatile struct ievme *iev = (struct ievme *) sc->sc_reg;
    273 
    274 	iev->status |= (IEVME_ONAIR | IEVME_IENAB | IEVME_PEINT);
    275 }
    276 
    277 int
    278 ie_vmeintr(sc, where)
    279 	struct ie_softc *sc;
    280 	int where;
    281 {
    282 	volatile struct ievme *iev = (volatile struct ievme *)sc->sc_reg;
    283 
    284 	if (where != INTR_ENTER)
    285 		return (0);
    286 
    287         /*
    288          * check for parity error
    289          */
    290 	if (iev->status & IEVME_PERR) {
    291 		printf("%s: parity error (ctrl 0x%x @ 0x%02x%04x)\n",
    292 			sc->sc_dev.dv_xname, iev->pectrl,
    293 			iev->pectrl & IEVME_HADDR, iev->peaddr);
    294 		iev->pectrl = iev->pectrl | IEVME_PARACK;
    295 	}
    296 	return (0);
    297 }
    298 
    299 void ie_memcopyin __P((struct ie_softc *, void *, int, size_t));
    300 void ie_memcopyout __P((struct ie_softc *, const void *, int, size_t));
    301 
    302 /*
    303  * Copy board memory to kernel.
    304  */
    305 void
    306 ie_memcopyin(sc, p, offset, size)
    307 	struct ie_softc	*sc;
    308 	void *p;
    309 	int offset;
    310 	size_t size;
    311 {
    312 	void *addr = (void *)((u_long)sc->bh + offset);/*XXX - not MI!*/
    313 	wcopy(addr, p, size);
    314 }
    315 
    316 /*
    317  * Copy from kernel space to naord memory.
    318  */
    319 void
    320 ie_memcopyout(sc, p, offset, size)
    321 	struct ie_softc	*sc;
    322 	const void *p;
    323 	int offset;
    324 	size_t size;
    325 {
    326 	void *addr = (void *)((u_long)sc->bh + offset);/*XXX - not MI!*/
    327 	wcopy(p, addr, size);
    328 }
    329 
    330 /* read a 16-bit value at BH offset */
    331 u_int16_t ie_vme_read16 __P((struct ie_softc *, int offset));
    332 /* write a 16-bit value at BH offset */
    333 void ie_vme_write16 __P((struct ie_softc *, int offset, u_int16_t value));
    334 void ie_vme_write24 __P((struct ie_softc *, int offset, int addr));
    335 
    336 u_int16_t
    337 ie_vme_read16(sc, offset)
    338 	struct ie_softc *sc;
    339 	int offset;
    340 {
    341 	u_int16_t v;
    342 
    343 	bus_space_barrier(sc->bt, sc->bh, offset, 2, BUS_SPACE_BARRIER_READ);
    344 	v = bus_space_read_2(sc->bt, sc->bh, offset);
    345 	return (((v&0xff)<<8) | ((v>>8)&0xff));
    346 }
    347 
    348 void
    349 ie_vme_write16(sc, offset, v)
    350 	struct ie_softc *sc;
    351 	int offset;
    352 	u_int16_t v;
    353 {
    354 	int v0 = ((((v)&0xff)<<8) | (((v)>>8)&0xff));
    355 	bus_space_write_2(sc->bt, sc->bh, offset, v0);
    356 	bus_space_barrier(sc->bt, sc->bh, offset, 2, BUS_SPACE_BARRIER_WRITE);
    357 }
    358 
    359 void
    360 ie_vme_write24(sc, offset, addr)
    361 	struct ie_softc *sc;
    362 	int offset;
    363 	int addr;
    364 {
    365 	u_char *f = (u_char *)&addr;
    366 	u_char *t = (u_char *)(sc->bh + offset);
    367 
    368 	t[0] = f[3]; t[1] = f[2]; t[2] = f[1]; /*_t[3] = _f[0];*/
    369 	bus_space_barrier(sc->bt, sc->bh, offset, 4, BUS_SPACE_BARRIER_WRITE);
    370 }
    371 
    372 int
    373 ie_vme_match(parent, cf, aux)
    374 	struct device *parent;
    375 	struct cfdata *cf;
    376 	void *aux;
    377 {
    378 	struct vme_attach_args *va = aux;
    379 	vme_chipset_tag_t ct = va->vma_chipset_tag;
    380 	bus_space_tag_t bt = va->vma_bustag;
    381 	int mod;
    382 
    383 	mod = VMEMOD_A24 | VMEMOD_S | VMEMOD_D;
    384 	if (vme_bus_probe(ct, bt, va->vma_reg[0], 2, mod))
    385 		return (1);
    386 
    387 	return (0);
    388 }
    389 
    390 void
    391 ie_vme_attach(parent, self, aux)
    392 	struct device *parent;
    393 	struct device *self;
    394 	void   *aux;
    395 {
    396 	u_int8_t myaddr[ETHER_ADDR_LEN];
    397 	extern void myetheraddr(u_char *);	/* should be elsewhere */
    398 	struct ie_softc *sc = (void *) self;
    399 	struct vme_attach_args *va = aux;
    400 	vme_chipset_tag_t ct = va->vma_chipset_tag;
    401 	bus_space_tag_t bt = va->vma_bustag;
    402 	bus_space_handle_t bh;
    403 	vme_intr_handle_t ih;
    404 	volatile struct ievme *iev;
    405 	u_long  rampaddr;
    406 	int     lcv;
    407 	vme_size_t sz;
    408 
    409 	int mod;
    410 
    411 	/*
    412 	 * *note*: we don't detect the difference between a VME3E and
    413 	 * a multibus/vme card.   if you want to use a 3E you'll have
    414 	 * to fix this.
    415 	 */
    416 	mod = VMEMOD_A24 | VMEMOD_S | VMEMOD_D;
    417 
    418 	sc->bt = bt;
    419 
    420 	sc->hwreset = ie_vmereset;
    421 	sc->hwinit = ie_vmerun;
    422 	sc->chan_attn = ie_vmeattend;
    423 	sc->intrhook = ie_vmeintr;
    424 	sc->memcopyout = ie_memcopyout;
    425 	sc->memcopyin = ie_memcopyin;
    426 	sc->ie_bus_read16 = ie_vme_read16;
    427 	sc->ie_bus_write16 = ie_vme_write16;
    428 	sc->ie_bus_write24 = ie_vme_write24;
    429 	sc->sc_msize = 4*65536;	/* XXX */
    430 
    431 	sz = sizeof(struct ievme);
    432 	if (vme_bus_map(ct, va->vma_reg[0], sz, mod, bt, &bh) != 0)
    433 		panic("if_ie: vme_map");
    434 	sc->sc_reg = (caddr_t)bh;
    435 
    436 	iev = (volatile struct ievme *) sc->sc_reg;
    437 	/* top 12 bits */
    438 	rampaddr = (u_long)va->vma_reg[0] & 0xfff00000;
    439 
    440 	/* 4 more */
    441 	rampaddr = rampaddr | ((iev->status & IEVME_HADDR) << 16);
    442 	sz = sc->sc_msize;
    443 	if (vme_bus_map(ct, rampaddr, sz, mod, bt, &bh) != 0)
    444 		panic("if_ie: vme_map");
    445 
    446 	sc->bh = bh;
    447 
    448 	iev->pectrl = iev->pectrl | IEVME_PARACK; /* clear to start */
    449 
    450 	/*
    451 	 * Set up mappings, direct map except for last page
    452 	 * which is mapped at zero and at high address (for scp)
    453 	 */
    454 	for (lcv = 0; lcv < IEVME_MAPSZ - 1; lcv++)
    455 		iev->pgmap[lcv] = IEVME_SBORDR | IEVME_OBMEM | lcv;
    456 	iev->pgmap[IEVME_MAPSZ - 1] = IEVME_SBORDR | IEVME_OBMEM | 0;
    457 
    458 	/* Clear all ram */
    459 	bus_space_set_multi_2(sc->bt, sc->bh, 0, 0, sc->sc_msize/2);
    460 
    461 	/*
    462 	 * We use the first page to set up SCP, ICSP and SCB data
    463 	 * structures. The remaining pages become the buffer area
    464 	 * (managed in i82586.c).
    465 	 * SCP is in double-mapped page, so the 586 can see it at
    466 	 * the mandatory magic address (IE_SCP_ADDR).
    467 	 */
    468 	sc->scp = (IE_SCP_ADDR & (IEVME_PAGESIZE - 1));
    469 
    470 	/* iscp at location zero */
    471 	sc->iscp = 0;
    472 
    473 	/* scb follows iscp */
    474 	sc->scb = IE_ISCP_SZ;
    475 
    476 	ie_vme_write16(sc, IE_ISCP_SCB((long)sc->iscp), sc->scb);
    477 	ie_vme_write16(sc, IE_ISCP_BASE((u_long)sc->iscp), 0);
    478 	ie_vme_write24(sc, IE_SCP_ISCP((u_long)sc->scp), 0);
    479 
    480 	if (i82586_proberam(sc) == 0) {
    481 		printf(": memory probe failed\n");
    482 		return;
    483 	}
    484 
    485 	/*
    486 	 * Rest of first page is unused; rest of ram for buffers.
    487 	 */
    488 	sc->buf_area = IEVME_PAGESIZE;
    489 	sc->buf_area_sz = sc->sc_msize - IEVME_PAGESIZE;
    490 
    491 	sc->do_xmitnopchain = 0;
    492 
    493 	myetheraddr(myaddr);
    494 	i82586_attach(sc, "multibus/vme", myaddr, media, NMEDIA, media[0]);
    495 
    496 	vme_intr_map(ct, va->vma_vec, va->vma_pri, &ih);
    497 	vme_intr_establish(ct, ih, i82586_intr, sc);
    498 
    499 	vme_bus_establish(ct, &sc->sc_dev);
    500 }
    501