if_le.c revision 1.6 1 /* $NetBSD: if_le.c,v 1.6 1998/08/29 20:32:10 pk 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; Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
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 #include "opt_inet.h"
41 #include "bpfilter.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/syslog.h>
47 #include <sys/socket.h>
48 #include <sys/device.h>
49 #include <sys/malloc.h>
50
51 #include <net/if.h>
52 #include <net/if_ether.h>
53 #include <net/if_media.h>
54
55 #ifdef INET
56 #include <netinet/in.h>
57 #include <netinet/if_inarp.h>
58 #endif
59
60 #include <machine/autoconf.h>
61 #include <machine/cpu.h>
62
63 #include <dev/sbus/sbusvar.h>
64 #include <dev/sbus/lebuffervar.h> /*XXX*/
65
66 #include <dev/ic/lancereg.h>
67 #include <dev/ic/lancevar.h>
68 #include <dev/ic/am7990reg.h>
69 #include <dev/ic/am7990var.h>
70
71 /*
72 * LANCE registers.
73 */
74 #define LEREG1_RDP 0 /* Register Data port */
75 #define LEREG1_RAP 2 /* Register Address port */
76
77 struct le_softc {
78 struct am7990_softc sc_am7990; /* glue to MI code */
79 struct sbusdev sc_sd; /* sbus device */
80 bus_space_tag_t sc_bustag;
81 bus_dma_tag_t sc_dmatag;
82 bus_space_handle_t sc_reg;
83 };
84
85 #define MEMSIZE 0x4000 /* LANCE memory size */
86
87 int lematch_sbus __P((struct device *, struct cfdata *, void *));
88 void leattach_sbus __P((struct device *, struct device *, void *));
89
90 /*
91 * Media types supported.
92 */
93 static int lemedia[] = {
94 IFM_ETHER|IFM_10_5,
95 };
96 #define NLEMEDIA (sizeof(lemedia) / sizeof(lemedia[0]))
97
98
99 struct cfattach le_sbus_ca = {
100 sizeof(struct le_softc), lematch_sbus, leattach_sbus
101 };
102
103 extern struct cfdriver le_cd;
104
105 #if defined(_KERNEL) && !defined(_LKM)
106 #include "opt_ddb.h"
107 #endif
108
109 #ifdef DDB
110 #define integrate
111 #define hide
112 #else
113 #define integrate static __inline
114 #define hide static
115 #endif
116
117 static void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
118 static u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
119
120 static void
121 lewrcsr(sc, port, val)
122 struct lance_softc *sc;
123 u_int16_t port, val;
124 {
125 struct le_softc *lesc = (struct le_softc *)sc;
126
127 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
128 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
129
130 #if defined(SUN4M)
131 /*
132 * We need to flush the Sbus->Mbus write buffers. This can most
133 * easily be accomplished by reading back the register that we
134 * just wrote (thanks to Chris Torek for this solution).
135 */
136 if (CPU_ISSUN4M) {
137 volatile u_int16_t discard;
138 discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg,
139 LEREG1_RDP);
140 }
141 #endif
142 }
143
144 static u_int16_t
145 lerdcsr(sc, port)
146 struct lance_softc *sc;
147 u_int16_t port;
148 {
149 struct le_softc *lesc = (struct le_softc *)sc;
150
151 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
152 return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
153 }
154
155
156 int
157 lematch_sbus(parent, cf, aux)
158 struct device *parent;
159 struct cfdata *cf;
160 void *aux;
161 {
162 struct sbus_attach_args *sa = aux;
163
164 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
165 }
166
167 #define SAME_LANCE(bp, sa) \
168 ((bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset) || \
169 (bp->val[0] == -1 && bp->val[1] == sc->sc_dev.dv_unit))
170
171 void
172 leattach_sbus(parent, self, aux)
173 struct device *parent, *self;
174 void *aux;
175 {
176 struct sbus_attach_args *sa = aux;
177 struct le_softc *lesc = (struct le_softc *)self;
178 struct lance_softc *sc = &lesc->sc_am7990.lsc;
179 struct sbusdev *sd;
180 /* XXX the following declarations should be elsewhere */
181 extern void myetheraddr __P((u_char *));
182
183
184 lesc->sc_bustag = sa->sa_bustag;
185 lesc->sc_dmatag = sa->sa_dmatag;
186
187 if (sbus_bus_map(sa->sa_bustag,
188 sa->sa_slot,
189 sa->sa_offset,
190 sa->sa_size,
191 BUS_SPACE_MAP_LINEAR,
192 0, &lesc->sc_reg) != 0) {
193 printf("%s @ sbus: cannot map registers\n", self->dv_xname);
194 return;
195 }
196
197 /*
198 * Look for an "unallocated" lebuffer and pair it with
199 * this `le' device on the assumption that we're on
200 * a pre-historic ROM that doesn't establish le<=>lebuffer
201 * parent-child relationships.
202 */
203 for (sd = ((struct sbus_softc *)parent)->sc_sbdev; sd != NULL;
204 sd = sd->sd_bchain) {
205
206 struct lebuf_softc *lebuf = (struct lebuf_softc *)sd->sd_dev;
207
208 if (strncmp("lebuffer", sd->sd_dev->dv_xname, 8) != 0)
209 continue;
210
211 if (lebuf->attached != 0)
212 continue;
213
214 sc->sc_mem = lebuf->sc_buffer;
215 sc->sc_memsize = lebuf->sc_bufsiz;
216 sc->sc_addr = 0; /* Lance view is offset by buffer location */
217 lebuf->attached = 1;
218
219 /* That old black magic... */
220 sc->sc_conf3 = getpropint(sa->sa_node,
221 "busmaster-regval",
222 LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
223 break;
224 }
225
226 lesc->sc_sd.sd_reset = (void *)lance_reset;
227 sbus_establish(&lesc->sc_sd, &sc->sc_dev);
228
229 if (sa->sa_bp != NULL && strcmp(sa->sa_bp->name, le_cd.cd_name) == 0 &&
230 SAME_LANCE(sa->sa_bp, sa))
231 sa->sa_bp->dev = &sc->sc_dev;
232
233 if (sc->sc_mem == 0) {
234 bus_dma_segment_t seg;
235 int rseg, error;
236
237 #ifndef BUS_DMA_24BIT
238 /* XXX - This flag is not defined on all archs */
239 #define BUS_DMA_24BIT 0
240 #endif
241 error = bus_dmamem_alloc(lesc->sc_dmatag, MEMSIZE, NBPG, 0,
242 &seg, 1, &rseg,
243 BUS_DMA_NOWAIT | BUS_DMA_24BIT);
244 if (error) {
245 printf("%s: DMA buffer allocation error %d\n",
246 self->dv_xname, error);
247 return;
248 }
249 error = bus_dmamem_map(lesc->sc_dmatag, &seg, rseg, MEMSIZE,
250 (caddr_t *)&sc->sc_mem,
251 BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
252 if (error) {
253 printf("%s: DMA buffer map error %d\n",
254 self->dv_xname, error);
255 bus_dmamem_free(lesc->sc_dmatag, &seg, rseg);
256 return;
257 }
258
259 sc->sc_addr = seg.ds_addr & 0xffffff;
260 sc->sc_memsize = MEMSIZE;
261 sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
262 }
263
264 myetheraddr(sc->sc_enaddr);
265
266 sc->sc_supmedia = lemedia;
267 sc->sc_nsupmedia = NLEMEDIA;
268 sc->sc_defaultmedia = lemedia[0];
269
270 sc->sc_copytodesc = lance_copytobuf_contig;
271 sc->sc_copyfromdesc = lance_copyfrombuf_contig;
272 sc->sc_copytobuf = lance_copytobuf_contig;
273 sc->sc_copyfrombuf = lance_copyfrombuf_contig;
274 sc->sc_zerobuf = lance_zerobuf_contig;
275
276 sc->sc_rdcsr = lerdcsr;
277 sc->sc_wrcsr = lewrcsr;
278
279 am7990_config(&lesc->sc_am7990);
280
281 (void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri, 0,
282 am7990_intr, sc);
283 }
284