if_le_lebuffer.c revision 1.6 1 /* $NetBSD: if_le_lebuffer.c,v 1.6 2000/07/09 20:57:42 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
41 #include "opt_inet.h"
42 #include "bpfilter.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/mbuf.h>
47 #include <sys/syslog.h>
48 #include <sys/socket.h>
49 #include <sys/device.h>
50 #include <sys/malloc.h>
51
52 #include <net/if.h>
53 #include <net/if_ether.h>
54 #include <net/if_media.h>
55
56 #ifdef INET
57 #include <netinet/in.h>
58 #include <netinet/if_inarp.h>
59 #endif
60
61 #include <machine/bus.h>
62 #include <machine/intr.h>
63 #include <machine/autoconf.h>
64
65 #include <dev/sbus/sbusvar.h>
66 #include <dev/sbus/lebuffervar.h>
67
68 #include <dev/ic/lancereg.h>
69 #include <dev/ic/lancevar.h>
70 #include <dev/ic/am7990reg.h>
71 #include <dev/ic/am7990var.h>
72
73 /*
74 * LANCE registers.
75 */
76 #define LEREG1_RDP 0 /* Register Data port */
77 #define LEREG1_RAP 2 /* Register Address port */
78
79 struct le_softc {
80 struct am7990_softc sc_am7990; /* glue to MI code */
81 struct sbusdev sc_sd; /* sbus device */
82 bus_space_tag_t sc_bustag;
83 bus_dma_tag_t sc_dmatag;
84 bus_space_handle_t sc_reg; /* LANCE registers */
85 };
86
87
88 int lematch_lebuffer __P((struct device *, struct cfdata *, void *));
89 void leattach_lebuffer __P((struct device *, struct device *, void *));
90
91 /*
92 * Media types supported.
93 */
94 static int lemedia[] = {
95 IFM_ETHER|IFM_10_T,
96 };
97 #define NLEMEDIA (sizeof(lemedia) / sizeof(lemedia[0]))
98
99 struct cfattach le_lebuffer_ca = {
100 sizeof(struct le_softc), lematch_lebuffer, leattach_lebuffer
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 int
156 lematch_lebuffer(parent, cf, aux)
157 struct device *parent;
158 struct cfdata *cf;
159 void *aux;
160 {
161 struct sbus_attach_args *sa = aux;
162
163 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
164 }
165
166
167 void
168 leattach_lebuffer(parent, self, aux)
169 struct device *parent, *self;
170 void *aux;
171 {
172 struct sbus_attach_args *sa = aux;
173 struct le_softc *lesc = (struct le_softc *)self;
174 struct lance_softc *sc = &lesc->sc_am7990.lsc;
175 struct lebuf_softc *lebuf = (struct lebuf_softc *)parent;
176 /* XXX the following declarations should be elsewhere */
177 extern void myetheraddr __P((u_char *));
178
179 lesc->sc_bustag = sa->sa_bustag;
180 lesc->sc_dmatag = sa->sa_dmatag;
181
182 if (bus_space_map2(sa->sa_bustag,
183 sa->sa_slot,
184 sa->sa_offset,
185 sa->sa_size,
186 BUS_SPACE_MAP_LINEAR,
187 0, &lesc->sc_reg)) {
188 printf("%s @ lebuffer: cannot map registers\n", self->dv_xname);
189 return;
190 }
191
192 sc->sc_mem = lebuf->sc_buffer;
193 sc->sc_memsize = lebuf->sc_bufsiz;
194 sc->sc_addr = 0; /* Lance view is offset by buffer location */
195 lebuf->attached = 1;
196
197 /* That old black magic... */
198 sc->sc_conf3 = getpropint(sa->sa_node, "busmaster-regval",
199 LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
200
201 /* Assume SBus is grandparent */
202 lesc->sc_sd.sd_reset = (void *)lance_reset;
203 sbus_establish(&lesc->sc_sd, parent);
204
205 sc->sc_supmedia = lemedia;
206 sc->sc_nsupmedia = NLEMEDIA;
207 sc->sc_defaultmedia = lemedia[0];
208
209 myetheraddr(sc->sc_enaddr);
210
211 sc->sc_copytodesc = lance_copytobuf_contig;
212 sc->sc_copyfromdesc = lance_copyfrombuf_contig;
213 sc->sc_copytobuf = lance_copytobuf_contig;
214 sc->sc_copyfrombuf = lance_copyfrombuf_contig;
215 sc->sc_zerobuf = lance_zerobuf_contig;
216
217 sc->sc_rdcsr = lerdcsr;
218 sc->sc_wrcsr = lewrcsr;
219
220 am7990_config(&lesc->sc_am7990);
221
222 /* Establish interrupt handler */
223 if (sa->sa_nintr != 0)
224 (void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri,
225 IPL_NET, 0, am7990_intr, sc);
226 }
227