Home | History | Annotate | Line # | Download | only in dev
if_ie_obio.c revision 1.37
      1 /*	$NetBSD: if_ie_obio.c,v 1.37 2009/09/20 16:18:21 tsutsui 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1995 Charles D. Cranor
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *      This product includes software developed by Charles D. Cranor.
     47  * 4. The name of the author may not be used to endorse or promote products
     48  *    derived from this software without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     51  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     52  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     53  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     54  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     55  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     59  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     60  */
     61 
     62 
     63 
     64 /*
     65  * Sparc OBIO front-end for the Intel 82586 Ethernet driver
     66  *
     67  * Converted to SUN ie driver by Charles D. Cranor,
     68  *		October 1994, January 1995.
     69  */
     70 
     71 /*
     72  * The i82586 is a very painful chip, found in sun3's, sun-4/100's
     73  * sun-4/200's, and VME based suns.  The byte order is all wrong for a
     74  * SUN, making life difficult.  Programming this chip is mostly the same,
     75  * but certain details differ from system to system.  This driver is
     76  * written so that different "ie" interfaces can be controled by the same
     77  * driver.
     78  */
     79 
     80 #include <sys/cdefs.h>
     81 __KERNEL_RCSID(0, "$NetBSD: if_ie_obio.c,v 1.37 2009/09/20 16:18:21 tsutsui Exp $");
     82 
     83 #include <sys/param.h>
     84 #include <sys/systm.h>
     85 #include <sys/errno.h>
     86 #include <sys/device.h>
     87 #include <sys/malloc.h>
     88 #include <sys/protosw.h>
     89 #include <sys/socket.h>
     90 
     91 #include <net/if.h>
     92 #include <net/if_types.h>
     93 #include <net/if_dl.h>
     94 #include <net/if_media.h>
     95 #include <net/if_ether.h>
     96 
     97 #include <uvm/uvm_extern.h>
     98 
     99 #include <machine/bus.h>
    100 #include <machine/intr.h>
    101 #include <machine/autoconf.h>
    102 
    103 #include <dev/ic/i82586reg.h>
    104 #include <dev/ic/i82586var.h>
    105 
    106 /*
    107  * the on-board interface
    108  */
    109 struct ieob {
    110 	u_char  obctrl;
    111 };
    112 #define IEOB_NORSET 0x80	/* don't reset the board */
    113 #define IEOB_ONAIR  0x40	/* put us on the air */
    114 #define IEOB_ATTEN  0x20	/* attention! */
    115 #define IEOB_IENAB  0x10	/* interrupt enable */
    116 #define IEOB_XXXXX  0x08	/* free bit */
    117 #define IEOB_XCVRL2 0x04	/* level 2 transceiver? */
    118 #define IEOB_BUSERR 0x02	/* bus error */
    119 #define IEOB_INT    0x01	/* interrupt */
    120 
    121 #define IEOB_ADBASE 0xff000000  /* KVA base addr of 24 bit address space */
    122 
    123 
    124 static void ie_obreset(struct ie_softc *, int);
    125 static void ie_obattend(struct ie_softc *, int);
    126 static void ie_obrun(struct ie_softc *);
    127 
    128 int ie_obio_match(device_t, cfdata_t, void *);
    129 void ie_obio_attach(device_t, device_t, void *);
    130 
    131 CFATTACH_DECL(ie_obio, sizeof(struct ie_softc),
    132     ie_obio_match, ie_obio_attach, NULL, NULL);
    133 
    134 /* Supported media */
    135 static int media[] = {
    136 	IFM_ETHER | IFM_10_2,
    137 };
    138 #define NMEDIA	(sizeof(media) / sizeof(media[0]))
    139 
    140 
    141 /*
    142  * OBIO ie support routines
    143  */
    144 static void
    145 ie_obreset(struct ie_softc *sc, int what)
    146 {
    147 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
    148 
    149 	ieo->obctrl = 0;
    150 	delay(100);			/* XXX could be shorter? */
    151 	ieo->obctrl = IEOB_NORSET;
    152 }
    153 
    154 static void
    155 ie_obattend(struct ie_softc *sc, int why)
    156 {
    157 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
    158 
    159 	ieo->obctrl |= IEOB_ATTEN;	/* flag! */
    160 	ieo->obctrl &= ~IEOB_ATTEN;	/* down. */
    161 }
    162 
    163 static void
    164 ie_obrun(struct ie_softc *sc)
    165 {
    166 	volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
    167 
    168 	ieo->obctrl |= (IEOB_ONAIR|IEOB_IENAB|IEOB_NORSET);
    169 }
    170 
    171 void ie_obio_memcopyin(struct ie_softc *, void *, int, size_t);
    172 void ie_obio_memcopyout(struct ie_softc *, const void *, int, size_t);
    173 
    174 /*
    175  * Copy board memory to kernel.
    176  */
    177 void
    178 ie_obio_memcopyin(struct ie_softc *sc, void *p, int offset, size_t size)
    179 {
    180 	void *addr = (void *)((u_long)sc->bh + offset);/*XXX - not MI!*/
    181 
    182 	wcopy(addr, p, size);
    183 }
    184 
    185 /*
    186  * Copy from kernel space to naord memory.
    187  */
    188 void
    189 ie_obio_memcopyout(struct ie_softc *sc, const void *p, int offset, size_t size)
    190 {
    191 	void *addr = (void *)((u_long)sc->bh + offset);/*XXX - not MI!*/
    192 
    193 	wcopy(p, addr, size);
    194 }
    195 
    196 /* read a 16-bit value at BH offset */
    197 uint16_t ie_obio_read16(struct ie_softc *, int);
    198 /* write a 16-bit value at BH offset */
    199 void ie_obio_write16(struct ie_softc *, int, uint16_t);
    200 void ie_obio_write24(struct ie_softc *, int, int);
    201 
    202 uint16_t
    203 ie_obio_read16(struct ie_softc *sc, int offset)
    204 {
    205 	uint16_t v = bus_space_read_2(sc->bt, sc->bh, offset);
    206 
    207 	return (((v&0xff)<<8) | ((v>>8)&0xff));
    208 }
    209 
    210 void
    211 ie_obio_write16(struct ie_softc *sc, int offset, uint16_t v)
    212 {
    213 	v = (((v&0xff)<<8) | ((v>>8)&0xff));
    214 	bus_space_write_2(sc->bt, sc->bh, offset, v);
    215 }
    216 
    217 void
    218 ie_obio_write24(struct ie_softc *sc, int offset, int addr)
    219 {
    220 	u_char *f = (u_char *)&addr;
    221 	uint16_t v0, v1;
    222 	u_char *t;
    223 
    224 	t = (u_char *)&v0;
    225 	t[0] = f[3]; t[1] = f[2];
    226 	bus_space_write_2(sc->bt, sc->bh, offset, v0);
    227 
    228 	t = (u_char *)&v1;
    229 	t[0] = f[1]; t[1] = 0;
    230 	bus_space_write_2(sc->bt, sc->bh, offset+2, v1);
    231 }
    232 
    233 int
    234 ie_obio_match(device_t parent, cfdata_t cf, void *aux)
    235 {
    236 	union obio_attach_args *uoba = aux;
    237 	struct obio4_attach_args *oba;
    238 
    239 	if (uoba->uoba_isobio4 == 0)
    240 		return (0);
    241 
    242 	oba = &uoba->uoba_oba4;
    243 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
    244 				1,	/* probe size */
    245 				0,	/* offset */
    246 				0,	/* flags */
    247 				NULL, NULL));
    248 }
    249 
    250 void
    251 ie_obio_attach(device_t parent, device_t self, void *aux)
    252 {
    253 	union obio_attach_args *uoba = aux;
    254 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
    255 	struct ie_softc *sc = device_private(self);
    256 	bus_dma_tag_t dmatag = oba->oba_dmatag;
    257 	bus_space_handle_t bh;
    258 	bus_dma_segment_t seg;
    259 	int rseg;
    260 	int error;
    261 	paddr_t pa;
    262 	struct intrhand *ih;
    263 	bus_size_t memsize;
    264 	u_long iebase;
    265 	uint8_t myaddr[ETHER_ADDR_LEN];
    266 
    267 	sc->bt = oba->oba_bustag;
    268 
    269 	sc->hwreset = ie_obreset;
    270 	sc->chan_attn = ie_obattend;
    271 	sc->hwinit = ie_obrun;
    272 	sc->memcopyout = ie_obio_memcopyout;
    273 	sc->memcopyin = ie_obio_memcopyin;
    274 
    275 	sc->ie_bus_barrier = NULL;
    276 	sc->ie_bus_read16 = ie_obio_read16;
    277 	sc->ie_bus_write16 = ie_obio_write16;
    278 	sc->ie_bus_write24 = ie_obio_write24;
    279 	sc->sc_msize = memsize = 65536; /* XXX */
    280 
    281 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
    282 			  sizeof(struct ieob),
    283 			  BUS_SPACE_MAP_LINEAR,
    284 			  &bh) != 0) {
    285 		printf("%s: cannot map registers\n", device_xname(self));
    286 		return;
    287 	}
    288 	sc->sc_reg = (void *)bh;
    289 
    290 	/*
    291 	 * Allocate control & buffer memory.
    292 	 */
    293 	if ((error = bus_dmamap_create(dmatag, memsize, 1, memsize, 0,
    294 					BUS_DMA_NOWAIT|BUS_DMA_24BIT,
    295 					&sc->sc_dmamap)) != 0) {
    296 		printf("%s: DMA map create error %d\n",
    297 		    device_xname(self), error);
    298 		return;
    299 	}
    300 	if ((error = bus_dmamem_alloc(dmatag, memsize, 64*1024, 0,
    301 			     &seg, 1, &rseg,
    302 			     BUS_DMA_NOWAIT | BUS_DMA_24BIT)) != 0) {
    303 		printf("%s: DMA memory allocation error %d\n",
    304 		    device_xname(self), error);
    305 		return;
    306 	}
    307 
    308 	/* Map DMA buffer in CPU addressable space */
    309 	if ((error = bus_dmamem_map(dmatag, &seg, rseg, memsize,
    310 				    (void **)&sc->sc_maddr,
    311 				    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    312 		printf("%s: DMA buffer map error %d\n",
    313 		    device_xname(self), error);
    314 		bus_dmamem_free(dmatag, &seg, rseg);
    315 		return;
    316 	}
    317 
    318 	/* Load the segment */
    319 	if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
    320 				     sc->sc_maddr, memsize, NULL,
    321 				     BUS_DMA_NOWAIT)) != 0) {
    322 		printf("%s: DMA buffer map load error %d\n",
    323 		    device_xname(self), error);
    324 		bus_dmamem_unmap(dmatag, sc->sc_maddr, memsize);
    325 		bus_dmamem_free(dmatag, &seg, rseg);
    326 		return;
    327 	}
    328 
    329 	wzero(sc->sc_maddr, memsize);
    330 	sc->bh = (bus_space_handle_t)(sc->sc_maddr);
    331 
    332 	/*
    333 	 * The i82586's 24-bit address space maps to the last 16MB of
    334 	 * KVA space ().  In addition, the SCP must appear
    335 	 * at IE_SCP_ADDR within the 24-bit address space,
    336 	 * i.e. at KVA  IEOB_ADBASE+IE_SCP_ADDR, at the very top of
    337 	 * kernel space.  We double-map this last page to the first
    338 	 * page (starting at `maddr') of the memory we allocate to the chip.
    339 	 * (a side-effect of this double-map is that the ISCP and SCB
    340 	 * structures also get aliased there, but we ignore this). The
    341 	 * first page at `maddr' is only used for ISCP, SCB and the aliased
    342 	 * SCP; the actual buffers start at maddr+PAGE_SIZE.
    343 	 *
    344 	 * In a picture:
    345 
    346 	|---//--- ISCP-SCB-----scp-|--//- buffers -//-|... |iscp-scb-----SCP-|
    347 	|         |                |                  |    |             |   |
    348 	|         |<---PAGE_SIZE-->|                  |    |<--PAGE_SIZE-+-->|
    349 	|         |<------------ memsize ------------>|    |       ^     |
    350 	|         |                                                |     |
    351 	|         \@maddr                                 (last page dbl mapped)
    352 	|                                                                |
    353 	\@IEOB_ADBASE                           @IEOB_ADBASE+IE_SCP_ADDR-+
    354 
    355 	 *
    356 	 */
    357 
    358 	/* Double map the SCP */
    359 	if (pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_maddr, &pa) == false)
    360 		panic("ie pmap_extract");
    361 
    362 	pmap_enter(pmap_kernel(), trunc_page(IEOB_ADBASE+IE_SCP_ADDR),
    363 	    pa | PMAP_NC /*| PMAP_IOC*/,
    364 	    VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED);
    365 	pmap_update(pmap_kernel());
    366 
    367 	/* Map iscp at location 0 (relative to `maddr') */
    368 	sc->iscp = 0;
    369 
    370 	/* scb follows iscp */
    371 	sc->scb = IE_ISCP_SZ;
    372 
    373 	/* scp is at the fixed location IE_SCP_ADDR (modulo the page size) */
    374 	sc->scp = IE_SCP_ADDR & PGOFSET;
    375 
    376 	/* Calculate the 24-bit base of i82586 operations */
    377 	iebase = (u_long)sc->sc_dmamap->dm_segs[0].ds_addr -
    378 			(u_long)IEOB_ADBASE;
    379 	ie_obio_write16(sc, IE_ISCP_SCB(sc->iscp), sc->scb);
    380 	ie_obio_write24(sc, IE_ISCP_BASE(sc->iscp), iebase);
    381 	ie_obio_write24(sc, IE_SCP_ISCP(sc->scp), iebase + sc->iscp);
    382 
    383 	/*
    384 	 * Rest of first page is unused (wasted!); the other pages
    385 	 * are used for buffers.
    386 	 */
    387 	sc->buf_area = PAGE_SIZE;
    388 	sc->buf_area_sz = memsize - PAGE_SIZE;
    389 
    390 	if (i82586_proberam(sc) == 0) {
    391 		printf(": memory probe failed\n");
    392 		return;
    393 	}
    394 
    395 	prom_getether(0, myaddr);
    396 	i82586_attach(sc, "onboard", myaddr, media, NMEDIA, media[0]);
    397 
    398 	/* Establish interrupt channel */
    399 	ih = bus_intr_establish(oba->oba_bustag,
    400 				oba->oba_pri, IPL_NET,
    401 				i82586_intr, sc);
    402 }
    403