1 /* $NetBSD: if_le.c,v 1.32 2008/04/04 12:25:07 tsutsui Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace 9 * Simulation Facility, NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /*- 41 * Copyright (c) 1992, 1993 42 * The Regents of the University of California. All rights reserved. 43 * 44 * This code is derived from software contributed to Berkeley by 45 * Ralph Campbell and Rick Macklem. 46 * 47 * Redistribution and use in source and binary forms, with or without 48 * modification, are permitted provided that the following conditions 49 * are met: 50 * 1. Redistributions of source code must retain the above copyright 51 * notice, this list of conditions and the following disclaimer. 52 * 2. Redistributions in binary form must reproduce the above copyright 53 * notice, this list of conditions and the following disclaimer in the 54 * documentation and/or other materials provided with the distribution. 55 * 3. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * @(#)if_le.c 8.2 (Berkeley) 11/16/93 72 */ 73 74 #include <sys/cdefs.h> 75 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.32 2008/04/04 12:25:07 tsutsui Exp $"); 76 77 #include "opt_inet.h" 78 #include "bpfilter.h" 79 80 #include <sys/param.h> 81 #include <sys/syslog.h> 82 #include <sys/socket.h> 83 #include <sys/device.h> 84 #include <sys/reboot.h> 85 86 #include <uvm/uvm_extern.h> 87 88 #include <net/if.h> 89 #include <net/if_ether.h> 90 #include <net/if_media.h> 91 92 #ifdef INET 93 #include <netinet/in.h> 94 #include <netinet/if_inarp.h> 95 #endif 96 97 #include <machine/cpu.h> 98 #include <machine/nexus.h> 99 #include <machine/scb.h> 100 #include <machine/mainbus.h> 101 102 #include <dev/ic/lancereg.h> 103 #include <dev/ic/lancevar.h> 104 #include <dev/ic/am7990reg.h> 105 #include <dev/ic/am7990var.h> 106 107 #include "ioconf.h" 108 109 #define LE_VEC 0xd4 /* Interrupt vector on 3300/3400 */ 110 111 #define LE_CSR 0x20084400 112 #define LE_ROM 0x20084200 113 #define LE_RAM 0x20120000 114 115 struct le_softc { 116 struct am7990_softc sc_am7990; /* Must be first */ 117 struct evcnt sc_intrcnt; 118 volatile uint16_t *sc_rap; 119 volatile uint16_t *sc_rdp; 120 }; 121 122 static int le_mainbus_match(device_t, cfdata_t, void *); 123 static void le_mainbus_attach(device_t, device_t, void *); 124 static void lewrcsr(struct lance_softc *, uint16_t, uint16_t); 125 static uint16_t lerdcsr(struct lance_softc *, uint16_t); 126 static void lance_copytobuf_gap2(struct lance_softc *, void *, int, int); 127 static void lance_copyfrombuf_gap2(struct lance_softc *, void *, int, int); 128 static void lance_zerobuf_gap2(struct lance_softc *, int, int); 129 130 CFATTACH_DECL_NEW(le_mainbus, sizeof(struct le_softc), 131 le_mainbus_match, le_mainbus_attach, NULL, NULL); 132 133 void 134 lewrcsr(struct lance_softc *ls, uint16_t port, uint16_t val) 135 { 136 struct le_softc * const sc = (void *)ls; 137 138 *sc->sc_rap = port; 139 *sc->sc_rdp = val; 140 } 141 142 uint16_t 143 lerdcsr(struct lance_softc *ls, uint16_t port) 144 { 145 struct le_softc * const sc = (void *)ls; 146 147 *sc->sc_rap = port; 148 return *sc->sc_rdp; 149 } 150 151 int 152 le_mainbus_match(device_t parent, cfdata_t cf, void *aux) 153 { 154 struct mainbus_attach_args * const ma = aux; 155 int found; 156 vaddr_t va; 157 158 if (strcmp("lance", ma->ma_type)) 159 return 0; 160 161 va = vax_map_physmem(LE_CSR, 1); 162 found = (badaddr((void *)va, 2) == 0); 163 vax_unmap_physmem(va, 1); 164 165 return found; 166 } 167 168 void 169 le_mainbus_attach(device_t parent, device_t self, void *aux) 170 { 171 struct le_softc * const sc = device_private(self); 172 int *lance_addr; 173 int i, vec, br; 174 175 sc->sc_am7990.lsc.sc_dev = self; 176 sc->sc_rdp = (uint16_t *)vax_map_physmem(LE_CSR, 1); 177 sc->sc_rap = sc->sc_rdp + 2; 178 179 /* 180 * Set interrupt vector, by forcing an interrupt. 181 */ 182 scb_vecref(0, 0); /* Clear vector ref */ 183 *sc->sc_rap = LE_CSR0; 184 *sc->sc_rdp = LE_C0_STOP; 185 DELAY(100); 186 *sc->sc_rdp = LE_C0_INIT|LE_C0_INEA; 187 DELAY(100000); 188 i = scb_vecref(&vec, &br); 189 if (i == 0 || vec == 0) 190 return; 191 192 scb_vecalloc(vec, (void (*)(void *))am7990_intr, sc, 193 SCB_ISTACK, &sc->sc_intrcnt); 194 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL, 195 device_xname(self), "intr"); 196 197 aprint_normal(": vec %o ipl %x\n%s", vec, br, device_xname(self)); 198 /* 199 * MD functions. 200 */ 201 sc->sc_am7990.lsc.sc_rdcsr = lerdcsr; 202 sc->sc_am7990.lsc.sc_wrcsr = lewrcsr; 203 sc->sc_am7990.lsc.sc_nocarrier = NULL; 204 205 sc->sc_am7990.lsc.sc_mem = 206 (void *)uvm_km_alloc(kernel_map, (128 * 1024), 0, UVM_KMF_VAONLY); 207 if (sc->sc_am7990.lsc.sc_mem == 0) 208 return; 209 210 ioaccess((vaddr_t)sc->sc_am7990.lsc.sc_mem, LE_RAM, 211 (128 * 1024) >> VAX_PGSHIFT); 212 213 214 sc->sc_am7990.lsc.sc_addr = 0; 215 sc->sc_am7990.lsc.sc_memsize = (64 * 1024); 216 217 sc->sc_am7990.lsc.sc_copytodesc = lance_copytobuf_gap2; 218 sc->sc_am7990.lsc.sc_copyfromdesc = lance_copyfrombuf_gap2; 219 sc->sc_am7990.lsc.sc_copytobuf = lance_copytobuf_gap2; 220 sc->sc_am7990.lsc.sc_copyfrombuf = lance_copyfrombuf_gap2; 221 sc->sc_am7990.lsc.sc_zerobuf = lance_zerobuf_gap2; 222 223 /* 224 * Get the ethernet address out of rom 225 */ 226 lance_addr = (int *)vax_map_physmem(LE_ROM, 1); 227 for (i = 0; i < 6; i++) 228 sc->sc_am7990.lsc.sc_enaddr[i] = (u_char)lance_addr[i]; 229 vax_unmap_physmem((vaddr_t)lance_addr, 1); 230 231 bcopy(device_xname(self), sc->sc_am7990.lsc.sc_ethercom.ec_if.if_xname, 232 IFNAMSIZ); 233 am7990_config(&sc->sc_am7990); 234 } 235 236 /* 237 * gap2: two bytes of data followed by two bytes of pad. 238 * 239 * Buffers must be 4-byte aligned. The code doesn't worry about 240 * doing an extra byte. 241 */ 242 243 void 244 lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len) 245 { 246 volatile void *buf = sc->sc_mem; 247 char *from = fromv; 248 volatile uint16_t *bptr; 249 250 if (boff & 0x1) { 251 /* handle unaligned first byte */ 252 bptr = ((volatile uint16_t *)buf) + (boff - 1); 253 *bptr = (*from++ << 8) | (*bptr & 0xff); 254 bptr += 2; 255 len--; 256 } else 257 bptr = ((volatile uint16_t *)buf) + boff; 258 while (len > 1) { 259 *bptr = (from[1] << 8) | (from[0] & 0xff); 260 bptr += 2; 261 from += 2; 262 len -= 2; 263 } 264 if (len == 1) 265 *bptr = (uint16_t)*from; 266 } 267 268 void 269 lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len) 270 { 271 volatile void *buf = sc->sc_mem; 272 char *to = tov; 273 volatile uint16_t *bptr; 274 uint16_t tmp; 275 276 if (boff & 0x1) { 277 /* handle unaligned first byte */ 278 bptr = ((volatile uint16_t *)buf) + (boff - 1); 279 *to++ = (*bptr >> 8) & 0xff; 280 bptr += 2; 281 len--; 282 } else 283 bptr = ((volatile uint16_t *)buf) + boff; 284 while (len > 1) { 285 tmp = *bptr; 286 *to++ = tmp & 0xff; 287 *to++ = (tmp >> 8) & 0xff; 288 bptr += 2; 289 len -= 2; 290 } 291 if (len == 1) 292 *to = *bptr & 0xff; 293 } 294 295 void 296 lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len) 297 { 298 volatile void *buf = sc->sc_mem; 299 volatile uint16_t *bptr; 300 301 if ((unsigned int)boff & 0x1) { 302 bptr = ((volatile uint16_t *)buf) + (boff - 1); 303 *bptr &= 0xff; 304 bptr += 2; 305 len--; 306 } else 307 bptr = ((volatile uint16_t *)buf) + boff; 308 while (len > 0) { 309 *bptr = 0; 310 bptr += 2; 311 len -= 2; 312 } 313 } 314