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