Home | History | Annotate | Line # | Download | only in dev
if_ie_sebuf.c revision 1.14
      1 /*	$NetBSD: if_ie_sebuf.c,v 1.14 2007/03/04 13:59:47 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Gordon W. Ross.
      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  * Machine-dependent glue for the Intel Ethernet (ie) driver,
     41  * as found on the Sun3/E SCSI/Ethernet board.
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: if_ie_sebuf.c,v 1.14 2007/03/04 13:59:47 tsutsui Exp $");
     46 
     47 #include "opt_inet.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/device.h>
     52 #include <sys/protosw.h>
     53 #include <sys/socket.h>
     54 #include <net/if.h>
     55 #include <net/if_ether.h>
     56 
     57 #ifdef INET
     58 #include <netinet/in.h>
     59 #include <netinet/in_systm.h>
     60 #include <netinet/in_var.h>
     61 #include <netinet/ip.h>
     62 #include <netinet/if_inarp.h>
     63 #endif
     64 
     65 #include <machine/autoconf.h>
     66 #include <machine/cpu.h>
     67 #include <machine/dvma.h>
     68 #include <machine/idprom.h>
     69 #include <machine/vmparam.h>
     70 
     71 #include "i82586.h"
     72 #include "if_iereg.h"
     73 #include "if_ievar.h"
     74 #include "sereg.h"
     75 #include "sevar.h"
     76 
     77 static void ie_sebuf_reset(struct ie_softc *);
     78 static void ie_sebuf_attend(struct ie_softc *);
     79 static void ie_sebuf_run(struct ie_softc *);
     80 
     81 /*
     82  * zero/copy functions: OBIO can use the normal functions, but VME
     83  *    must do only byte or half-word (16 bit) accesses...
     84  */
     85 static void *wmemcpy(void *, const void *, size_t);
     86 static void *wmemset(void *, int, size_t);
     87 
     88 
     89 /*
     90  * New-style autoconfig attachment
     91  */
     92 
     93 static int  ie_sebuf_match(struct device *, struct cfdata *, void *);
     94 static void ie_sebuf_attach(struct device *, struct device *, void *);
     95 
     96 CFATTACH_DECL(ie_sebuf, sizeof(struct ie_softc),
     97     ie_sebuf_match, ie_sebuf_attach, NULL, NULL);
     98 
     99 static int
    100 ie_sebuf_match(struct device *parent, struct cfdata *cf, void *args)
    101 {
    102 	struct sebuf_attach_args *aa = args;
    103 
    104 	/* Match by name. */
    105 	if (strcmp(aa->name, "ie"))
    106 		return (0);
    107 
    108 	/* Anyting else to check? */
    109 
    110 	return (1);
    111 }
    112 
    113 static void
    114 ie_sebuf_attach(struct device *parent, struct device *self, void *args)
    115 {
    116 	struct ie_softc *sc = (void *) self;
    117 	struct sebuf_attach_args *aa = args;
    118 	volatile struct ie_regs *regs;
    119 	int     off;
    120 
    121 	sc->hard_type = IE_VME3E;
    122 	sc->reset_586 = ie_sebuf_reset;
    123 	sc->chan_attn = ie_sebuf_attend;
    124 	sc->run_586   = ie_sebuf_run;
    125 	sc->sc_memcpy = wmemcpy;
    126 	sc->sc_memset = wmemset;
    127 
    128 	/* Control regs mapped by parent. */
    129 	sc->sc_reg = aa->regs;
    130 	regs = (struct ie_regs *) sc->sc_reg;
    131 
    132 	/*
    133 	 * On this hardware, the i82586 address zero
    134 	 * maps to the start of the board, which we
    135 	 * happen to know is 128K below (XXX).
    136 	 */
    137 	sc->sc_iobase = aa->buf - 0x20000;
    138 
    139 	/*
    140 	 * There is 128K of memory for the i82586 on
    141 	 * the Sun3/E SCSI/Ethernet board, and the
    142 	 * "sebuf" driver has mapped it in for us.
    143 	 * (Fixed in hardware; NOT configurable!)
    144 	 */
    145 	if (aa->blen < SE_IEBUFSIZE)
    146 		panic("ie_sebuf: bad size");
    147 	sc->sc_msize = SE_IEBUFSIZE;
    148 	sc->sc_maddr = aa->buf;
    149 
    150 	/* Clear the memory. */
    151 	(sc->sc_memset)(sc->sc_maddr, 0, sc->sc_msize);
    152 
    153 	/*
    154 	 * XXX:  Unfortunately, the common driver code
    155 	 * is not ready to deal with more than 64K of
    156 	 * memory, so skip the first 64K.  Too bad.
    157 	 * Note: device needs the SCP at the end.
    158 	 */
    159 	sc->sc_msize -= 0x10000;
    160 	sc->sc_maddr = (char *)sc->sc_maddr + 0x10000;
    161 
    162 	/*
    163 	 * Set the System Configuration Pointer (SCP).
    164 	 * Its location is system-dependent because the
    165 	 * i82586 reads it from a fixed physical address.
    166 	 * On this hardware, the i82586 address is just
    167 	 * masked down to 17 bits, so the SCP is found
    168 	 * at the end of the RAM on the VME board.
    169 	 */
    170 	off = IE_SCP_ADDR & 0xFFFF;
    171 	sc->scp = (volatile void *)((char *)sc->sc_maddr + off);
    172 
    173 	/*
    174 	 * The rest of ram is used for buffers, etc.
    175 	 */
    176 	sc->buf_area = sc->sc_maddr;
    177 	sc->buf_area_sz = off;
    178 
    179 	/* Install interrupt handler. */
    180 	regs->ie_ivec = aa->ca.ca_intvec;
    181 	isr_add_vectored(ie_intr, (void *)sc,
    182 		aa->ca.ca_intpri, aa->ca.ca_intvec);
    183 
    184 	/* Set the ethernet address. */
    185 	idprom_etheraddr(sc->sc_addr);
    186 
    187 	/* Do machine-independent parts of attach. */
    188 	ie_attach(sc);
    189 
    190 }
    191 
    192 /* Whack the "channel attetion" line. */
    193 void
    194 ie_sebuf_attend(struct ie_softc *sc)
    195 {
    196 	volatile struct ie_regs *regs = (struct ie_regs *) sc->sc_reg;
    197 
    198 	regs->ie_csr |= IE_CSR_ATTEN;	/* flag! */
    199 	regs->ie_csr &= ~IE_CSR_ATTEN;	/* down. */
    200 }
    201 
    202 /*
    203  * This is called during driver attach.
    204  * Reset and initialize.
    205  */
    206 void
    207 ie_sebuf_reset(struct ie_softc *sc)
    208 {
    209 	volatile struct ie_regs *regs = (struct ie_regs *) sc->sc_reg;
    210 	regs->ie_csr = IE_CSR_RESET;
    211 	delay(20);
    212 	regs->ie_csr = (IE_CSR_NOLOOP | IE_CSR_IENAB);
    213 }
    214 
    215 /*
    216  * This is called at the end of ieinit().
    217  * optional.
    218  */
    219 void
    220 ie_sebuf_run(struct ie_softc *sc)
    221 {
    222 	/* do it all in reset */
    223 }
    224 
    225 /*
    226  * wmemcpy/wmemset - like memcpy/memset but largest access is 16-bits,
    227  * and also does byte swaps...
    228  * XXX - Would be nice to have asm versions in some library...
    229  */
    230 
    231 static void *
    232 wmemset(void *vb, int val, size_t l)
    233 {
    234 	u_char *b = vb;
    235 	u_char *be = b + l;
    236 	u_short *sp;
    237 
    238 	if (l == 0)
    239 		return (vb);
    240 
    241 	/* front, */
    242 	if ((u_long)b & 1)
    243 		*b++ = val;
    244 
    245 	/* back, */
    246 	if (b != be && ((u_long)be & 1) != 0) {
    247 		be--;
    248 		*be = val;
    249 	}
    250 
    251 	/* and middle. */
    252 	sp = (u_short *)b;
    253 	while (sp != (u_short *)be)
    254 		*sp++ = val;
    255 
    256 	return (vb);
    257 }
    258 
    259 static void *
    260 wmemcpy(void *dst, const void *src, size_t l)
    261 {
    262 	const u_char *b1e, *b1 = src;
    263 	u_char *b2 = dst;
    264 	const u_short *sp;
    265 	int bstore = 0;
    266 
    267 	if (l == 0)
    268 		return (dst);
    269 
    270 	/* front, */
    271 	if ((u_long)b1 & 1) {
    272 		*b2++ = *b1++;
    273 		l--;
    274 	}
    275 
    276 	/* middle, */
    277 	sp = (const u_short *)b1;
    278 	b1e = b1 + l;
    279 	if (l & 1)
    280 		b1e--;
    281 	bstore = (u_long)b2 & 1;
    282 
    283 	while (sp < (const u_short *)b1e) {
    284 		if (bstore) {
    285 			b2[1] = *sp & 0xff;
    286 			b2[0] = *sp >> 8;
    287 		} else
    288 			*((short *)b2) = *sp;
    289 		sp++;
    290 		b2 += 2;
    291 	}
    292 
    293 	/* and back. */
    294 	if (l & 1)
    295 		*b2 = *b1e;
    296 
    297 	return (dst);
    298 }
    299