xref: /netbsd/sys/dev/sbus/if_le_lebuffer.c (revision bf9ec67e)
1 /*	$NetBSD: if_le_lebuffer.c,v 1.11 2002/03/20 20:39:15 eeh 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 <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: if_le_lebuffer.c,v 1.11 2002/03/20 20:39:15 eeh Exp $");
42 
43 #include "opt_inet.h"
44 #include "bpfilter.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/syslog.h>
50 #include <sys/socket.h>
51 #include <sys/device.h>
52 #include <sys/malloc.h>
53 
54 #include <net/if.h>
55 #include <net/if_ether.h>
56 #include <net/if_media.h>
57 
58 #ifdef INET
59 #include <netinet/in.h>
60 #include <netinet/if_inarp.h>
61 #endif
62 
63 #include <machine/bus.h>
64 #include <machine/intr.h>
65 #include <machine/autoconf.h>
66 
67 #include <dev/sbus/sbusvar.h>
68 #include <dev/sbus/lebuffervar.h>
69 
70 #include <dev/ic/lancereg.h>
71 #include <dev/ic/lancevar.h>
72 #include <dev/ic/am7990reg.h>
73 #include <dev/ic/am7990var.h>
74 
75 /*
76  * LANCE registers.
77  */
78 #define LEREG1_RDP	0	/* Register Data port */
79 #define LEREG1_RAP	2	/* Register Address port */
80 
81 struct	le_softc {
82 	struct	am7990_softc	sc_am7990;	/* glue to MI code */
83 	struct	sbusdev		sc_sd;		/* sbus device */
84 	bus_space_tag_t		sc_bustag;
85 	bus_dma_tag_t		sc_dmatag;
86 	bus_space_handle_t	sc_reg;		/* LANCE registers */
87 };
88 
89 
90 int	lematch_lebuffer __P((struct device *, struct cfdata *, void *));
91 void	leattach_lebuffer __P((struct device *, struct device *, void *));
92 
93 /*
94  * Media types supported.
95  */
96 static int lemedia[] = {
97 	IFM_ETHER|IFM_10_T,
98 };
99 #define NLEMEDIA	(sizeof(lemedia) / sizeof(lemedia[0]))
100 
101 struct cfattach le_lebuffer_ca = {
102 	sizeof(struct le_softc), lematch_lebuffer, leattach_lebuffer
103 };
104 
105 extern struct cfdriver le_cd;
106 
107 #if defined(_KERNEL_OPT)
108 #include "opt_ddb.h"
109 #endif
110 
111 #ifdef DDB
112 #define	integrate
113 #define hide
114 #else
115 #define	integrate	static __inline
116 #define hide		static
117 #endif
118 
119 static void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
120 static u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
121 
122 static void
123 lewrcsr(sc, port, val)
124 	struct lance_softc *sc;
125 	u_int16_t port, val;
126 {
127 	struct le_softc *lesc = (struct le_softc *)sc;
128 
129 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
130 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
131 
132 #if defined(SUN4M)
133 	/*
134 	 * We need to flush the Sbus->Mbus write buffers. This can most
135 	 * easily be accomplished by reading back the register that we
136 	 * just wrote (thanks to Chris Torek for this solution).
137 	 */
138 	if (CPU_ISSUN4M) {
139 		volatile u_int16_t discard;
140 		discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg,
141 					   LEREG1_RDP);
142 	}
143 #endif
144 }
145 
146 static u_int16_t
147 lerdcsr(sc, port)
148 	struct lance_softc *sc;
149 	u_int16_t port;
150 {
151 	struct le_softc *lesc = (struct le_softc *)sc;
152 
153 	bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
154 	return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
155 }
156 
157 int
158 lematch_lebuffer(parent, cf, aux)
159 	struct device *parent;
160 	struct cfdata *cf;
161 	void *aux;
162 {
163 	struct sbus_attach_args *sa = aux;
164 
165 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
166 }
167 
168 
169 void
170 leattach_lebuffer(parent, self, aux)
171 	struct device *parent, *self;
172 	void *aux;
173 {
174 	struct sbus_attach_args *sa = aux;
175 	struct le_softc *lesc = (struct le_softc *)self;
176 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
177 	struct lebuf_softc *lebuf = (struct lebuf_softc *)parent;
178 	/* XXX the following declarations should be elsewhere */
179 	extern void myetheraddr __P((u_char *));
180 
181 	lesc->sc_bustag = sa->sa_bustag;
182 	lesc->sc_dmatag = sa->sa_dmatag;
183 
184 	if (sbus_bus_map(sa->sa_bustag,
185 			 sa->sa_slot,
186 			 sa->sa_offset,
187 			 sa->sa_size,
188 			 0, &lesc->sc_reg)) {
189 		printf("%s @ lebuffer: cannot map registers\n", self->dv_xname);
190 		return;
191 	}
192 
193 	sc->sc_mem = lebuf->sc_buffer;
194 	sc->sc_memsize = lebuf->sc_bufsiz;
195 	sc->sc_addr = 0; /* Lance view is offset by buffer location */
196 	lebuf->attached = 1;
197 
198 	/* That old black magic... */
199 	sc->sc_conf3 = PROM_getpropint(sa->sa_node, "busmaster-regval",
200 				  LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON);
201 
202 	/* Assume SBus is grandparent */
203 	lesc->sc_sd.sd_reset = (void *)lance_reset;
204 	sbus_establish(&lesc->sc_sd, parent);
205 
206 	sc->sc_supmedia = lemedia;
207 	sc->sc_nsupmedia = NLEMEDIA;
208 	sc->sc_defaultmedia = lemedia[0];
209 
210 	myetheraddr(sc->sc_enaddr);
211 
212 	sc->sc_copytodesc = lance_copytobuf_contig;
213 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
214 	sc->sc_copytobuf = lance_copytobuf_contig;
215 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
216 	sc->sc_zerobuf = lance_zerobuf_contig;
217 
218 	sc->sc_rdcsr = lerdcsr;
219 	sc->sc_wrcsr = lewrcsr;
220 
221 	am7990_config(&lesc->sc_am7990);
222 
223 	/* Establish interrupt handler */
224 	if (sa->sa_nintr != 0)
225 		(void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri,
226 					 IPL_NET, 0, am7990_intr, sc);
227 }
228