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