if_le.c revision 1.3 1 /* $NetBSD: if_le.c,v 1.3 1998/07/30 22:44:51 pk 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 Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * 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) 1996
42 * The President and Fellows of Harvard College. All rights reserved.
43 * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
44 * Copyright (c) 1992, 1993
45 * The Regents of the University of California. All rights reserved.
46 *
47 * This code is derived from software contributed to Berkeley by
48 * Ralph Campbell and Rick Macklem.
49 *
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright
56 * notice, this list of conditions and the following disclaimer in the
57 * documentation and/or other materials provided with the distribution.
58 * 3. All advertising materials mentioning features or use of this software
59 * must display the following acknowledgement:
60 * This product includes software developed by Aaron Brown and
61 * Harvard University.
62 * This product includes software developed by the University of
63 * California, Berkeley and its contributors.
64 * 4. Neither the name of the University nor the names of its contributors
65 * may be used to endorse or promote products derived from this software
66 * without specific prior written permission.
67 *
68 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 * SUCH DAMAGE.
79 *
80 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
81 */
82
83 #include "opt_inet.h"
84 #include "bpfilter.h"
85
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/mbuf.h>
89 #include <sys/syslog.h>
90 #include <sys/socket.h>
91 #include <sys/device.h>
92 #include <sys/malloc.h>
93
94 #include <net/if.h>
95 #include <net/if_ether.h>
96 #include <net/if_media.h>
97
98 #ifdef INET
99 #include <netinet/in.h>
100 #include <netinet/if_inarp.h>
101 #endif
102
103 #include <machine/autoconf.h>
104 #include <machine/cpu.h>
105
106 #include <dev/sbus/sbusvar.h>
107 #include <dev/sbus/lebuffervar.h> /*XXX*/
108
109 #include <dev/ic/lancereg.h>
110 #include <dev/ic/lancevar.h>
111 #include <dev/ic/am7990reg.h>
112 #include <dev/ic/am7990var.h>
113
114 /*
115 * LANCE registers.
116 */
117 struct lereg1 {
118 volatile u_int16_t ler1_rdp; /* data port */
119 volatile u_int16_t ler1_rap; /* register select port */
120 };
121
122 struct le_softc {
123 struct am7990_softc sc_am7990; /* glue to MI code */
124 struct sbusdev sc_sd; /* sbus device */
125 bus_space_tag_t sc_bustag;
126 bus_dma_tag_t sc_dmatag;
127 struct lereg1 *sc_r1; /* LANCE registers */
128 };
129
130 #define MEMSIZE 0x4000 /* LANCE memory size */
131
132 int lematch_sbus __P((struct device *, struct cfdata *, void *));
133 void leattach_sbus __P((struct device *, struct device *, void *));
134
135 /*
136 * Media types supported.
137 */
138 static int lemedia[] = {
139 IFM_ETHER|IFM_10_5,
140 };
141 #define NLEMEDIA (sizeof(lemedia) / sizeof(lemedia[0]))
142
143
144 struct cfattach le_sbus_ca = {
145 sizeof(struct le_softc), lematch_sbus, leattach_sbus
146 };
147
148 extern struct cfdriver le_cd;
149
150 #if defined(_KERNEL) && !defined(_LKM)
151 #include "opt_ddb.h"
152 #endif
153
154 #ifdef DDB
155 #define integrate
156 #define hide
157 #else
158 #define integrate static __inline
159 #define hide static
160 #endif
161
162 static void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
163 static u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
164
165 static void
166 lewrcsr(sc, port, val)
167 struct lance_softc *sc;
168 u_int16_t port, val;
169 {
170 struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
171
172 ler1->ler1_rap = port;
173 ler1->ler1_rdp = val;
174
175 #if defined(SUN4M)
176 /*
177 * We need to flush the Sbus->Mbus write buffers. This can most
178 * easily be accomplished by reading back the register that we
179 * just wrote (thanks to Chris Torek for this solution).
180 */
181 if (CPU_ISSUN4M) {
182 volatile u_int16_t discard;
183 discard = ler1->ler1_rdp;
184 }
185 #endif
186 }
187
188 static u_int16_t
189 lerdcsr(sc, port)
190 struct lance_softc *sc;
191 u_int16_t port;
192 {
193 struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
194 u_int16_t val;
195
196 ler1->ler1_rap = port;
197 val = ler1->ler1_rdp;
198 return (val);
199 }
200
201
202 int
203 lematch_sbus(parent, cf, aux)
204 struct device *parent;
205 struct cfdata *cf;
206 void *aux;
207 {
208 struct sbus_attach_args *sa = aux;
209
210 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
211 }
212
213 #define SAME_LANCE(bp, sa) \
214 ((bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset) || \
215 (bp->val[0] == -1 && bp->val[1] == sc->sc_dev.dv_unit))
216
217 void
218 leattach_sbus(parent, self, aux)
219 struct device *parent, *self;
220 void *aux;
221 {
222 struct sbus_attach_args *sa = aux;
223 struct le_softc *lesc = (struct le_softc *)self;
224 struct lance_softc *sc = &lesc->sc_am7990.lsc;
225 struct sbusdev *sd;
226 bus_space_handle_t bh;
227 /* XXX the following declarations should be elsewhere */
228 extern void myetheraddr __P((u_char *));
229
230
231 lesc->sc_bustag = sa->sa_bustag;
232 lesc->sc_dmatag = sa->sa_dmatag;
233
234 if (sbus_bus_map(sa->sa_bustag,
235 sa->sa_slot,
236 sa->sa_offset,
237 sizeof(struct lereg1),
238 BUS_SPACE_MAP_LINEAR,
239 0, &bh) != 0) {
240 printf("%s @ sbus: cannot map registers\n", self->dv_xname);
241 return;
242 }
243 lesc->sc_r1 = (struct lereg1 *)bh;
244
245 /*
246 * Look for an "unallocated" lebuffer and pair it with
247 * this `le' device on the assumption that we're on
248 * a pre-historic ROM that doesn't establish le<=>lebuffer
249 * parent-child relationships.
250 */
251 for (sd = ((struct sbus_softc *)parent)->sc_sbdev; sd != NULL;
252 sd = sd->sd_bchain) {
253
254 struct lebuf_softc *lebuf = (struct lebuf_softc *)sd->sd_dev;
255
256 if (strncmp("lebuffer", sd->sd_dev->dv_xname, 8) != 0)
257 continue;
258
259 if (lebuf->attached != 0)
260 continue;
261
262 sc->sc_mem = lebuf->sc_buffer;
263 sc->sc_memsize = lebuf->sc_bufsiz;
264 sc->sc_addr = 0; /* Lance view is offset by buffer location */
265 lebuf->attached = 1;
266
267 /* That old black magic... */
268 sc->sc_conf3 = getpropint(sa->sa_node,
269 "busmaster-regval",
270 LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
271 break;
272 }
273
274 lesc->sc_sd.sd_reset = (void *)lance_reset;
275 sbus_establish(&lesc->sc_sd, &sc->sc_dev);
276
277 if (sa->sa_bp != NULL && strcmp(sa->sa_bp->name, le_cd.cd_name) == 0 &&
278 SAME_LANCE(sa->sa_bp, sa))
279 sa->sa_bp->dev = &sc->sc_dev;
280
281 if (sc->sc_mem == 0) {
282 bus_dma_segment_t seg;
283 int rseg, error;
284
285 error = bus_dmamem_alloc(lesc->sc_dmatag, MEMSIZE, NBPG, 0,
286 &seg, 1, &rseg,
287 BUS_DMA_NOWAIT | BUS_DMA_24BIT);
288 if (error) {
289 printf("if_le: DMA buffer alloc error %d\n", error);
290 return;
291 }
292 error = bus_dmamem_map(lesc->sc_dmatag, &seg, rseg, MEMSIZE,
293 (caddr_t *)&sc->sc_mem,
294 BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
295 if (error) {
296 printf("if_le: DMA buffer map error %d\n", error);
297 return;
298 }
299
300 sc->sc_addr = seg.ds_addr & 0xffffff;
301 #if defined (SUN4M)
302 if ((sc->sc_addr & 0xffffff) >=
303 (sc->sc_addr & 0xffffff) + MEMSIZE)
304 panic("if_le: Lance buffer crosses 16MB boundary");
305 #endif
306 sc->sc_memsize = MEMSIZE;
307 sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
308 }
309
310 myetheraddr(sc->sc_enaddr);
311
312 sc->sc_supmedia = lemedia;
313 sc->sc_nsupmedia = NLEMEDIA;
314 sc->sc_defaultmedia = lemedia[0];
315
316 sc->sc_copytodesc = lance_copytobuf_contig;
317 sc->sc_copyfromdesc = lance_copyfrombuf_contig;
318 sc->sc_copytobuf = lance_copytobuf_contig;
319 sc->sc_copyfrombuf = lance_copyfrombuf_contig;
320 sc->sc_zerobuf = lance_zerobuf_contig;
321
322 sc->sc_rdcsr = lerdcsr;
323 sc->sc_wrcsr = lewrcsr;
324
325 am7990_config(&lesc->sc_am7990);
326
327 (void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri, 0,
328 am7990_intr, sc);
329 }
330