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