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