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