xref: /netbsd-src/sys/arch/hp300/dev/if_le.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: if_le.c,v 1.47 1998/08/15 10:51:16 mycroft Exp $	*/
2 
3 /*-
4  * Copyright (c) 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 and by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*-
40  * Copyright (c) 1992, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * This code is derived from software contributed to Berkeley by
44  * Ralph Campbell and Rick Macklem.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *	This product includes software developed by the University of
57  *	California, Berkeley and its contributors.
58  * 4. Neither the name of the University nor the names of its contributors
59  *    may be used to endorse or promote products derived from this software
60  *    without specific prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72  * SUCH DAMAGE.
73  *
74  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
75  */
76 
77 #include "opt_inet.h"
78 #include "bpfilter.h"
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/mbuf.h>
83 #include <sys/syslog.h>
84 #include <sys/socket.h>
85 #include <sys/device.h>
86 
87 #include <net/if.h>
88 #include <net/if_ether.h>
89 #include <net/if_media.h>
90 
91 #ifdef INET
92 #include <netinet/in.h>
93 #include <netinet/if_inarp.h>
94 #endif
95 
96 #include <machine/autoconf.h>
97 #include <machine/cpu.h>
98 #include <machine/intr.h>
99 
100 #include <dev/ic/lancereg.h>
101 #include <dev/ic/lancevar.h>
102 #include <dev/ic/am7990reg.h>
103 #include <dev/ic/am7990var.h>
104 
105 #include <hp300/dev/dioreg.h>
106 #include <hp300/dev/diovar.h>
107 #include <hp300/dev/diodevs.h>
108 #include <hp300/dev/if_lereg.h>
109 #include <hp300/dev/if_levar.h>
110 
111 #include "opt_useleds.h"
112 
113 #ifdef USELEDS
114 #include <hp300/hp300/leds.h>
115 #endif
116 
117 int	lematch __P((struct device *, struct cfdata *, void *));
118 void	leattach __P((struct device *, struct device *, void *));
119 
120 struct cfattach le_ca = {
121 	sizeof(struct le_softc), lematch, leattach
122 };
123 
124 int	leintr __P((void *));
125 
126 #if defined(_KERNEL) && !defined(_LKM)
127 #include "opt_ddb.h"
128 #endif
129 
130 #ifdef DDB
131 #define	integrate
132 #define hide
133 #else
134 #define	integrate	static __inline
135 #define hide		static
136 #endif
137 
138 hide void le_copytobuf __P((struct lance_softc *, void *, int, int));
139 hide void le_copyfrombuf __P((struct lance_softc *, void *, int, int));
140 hide void le_zerobuf __P((struct lance_softc *, int, int));
141 
142 /* offsets for:	   ID,   REGS,    MEM,  NVRAM */
143 int	lestd[] = { 0, 0x4000, 0x8000, 0xC008 };
144 
145 hide void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
146 hide u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
147 
148 hide void
149 lewrcsr(sc, port, val)
150 	struct lance_softc *sc;
151 	u_int16_t port, val;
152 {
153 	struct le_softc *lesc = (struct le_softc *)sc;
154 	bus_space_tag_t bst = lesc->sc_bst;
155 	bus_space_handle_t bsh0 = lesc->sc_bsh0;
156 	bus_space_handle_t bsh1 = lesc->sc_bsh1;
157 
158 	do {
159 		bus_space_write_2(bst, bsh1, LER1_RAP, port);
160 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
161 	do {
162 		bus_space_write_2(bst, bsh1, LER1_RDP, val);
163 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
164 }
165 
166 hide u_int16_t
167 lerdcsr(sc, port)
168 	struct lance_softc *sc;
169 	u_int16_t port;
170 {
171 	struct le_softc *lesc = (struct le_softc *)sc;
172 	bus_space_tag_t bst = lesc->sc_bst;
173 	bus_space_handle_t bsh0 = lesc->sc_bsh0;
174 	bus_space_handle_t bsh1 = lesc->sc_bsh1;
175 	u_int16_t val;
176 
177 	do {
178 		bus_space_write_2(bst, bsh1, LER1_RAP, port);
179 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
180 	do {
181 		val = bus_space_read_2(bst, bsh1, LER1_RDP);
182 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
183 
184 	return (val);
185 }
186 
187 int
188 lematch(parent, match, aux)
189 	struct device *parent;
190 	struct cfdata *match;
191 	void *aux;
192 {
193 	struct dio_attach_args *da = aux;
194 
195 	if (da->da_id == DIO_DEVICE_ID_LAN)
196 		return (1);
197 	return (0);
198 }
199 
200 /*
201  * Interface exists: make available by filling in network interface
202  * record.  System will initialize the interface when it is ready
203  * to accept packets.
204  */
205 void
206 leattach(parent, self, aux)
207 	struct device *parent, *self;
208 	void *aux;
209 {
210 	struct dio_attach_args *da = aux;
211 	struct le_softc *lesc = (struct le_softc *)self;
212 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
213 	bus_space_tag_t bst = da->da_bst;
214 	bus_space_handle_t bsh0, bsh1, bsh2;
215 	bus_size_t offset;
216 	int i, ipl;
217 
218 	if (bus_space_map(bst, (bus_addr_t)dio_scodetopa(da->da_scode),
219 	    da->da_size, 0, &bsh0)) {
220 		printf("\n%s: can't map LANCE registers\n",
221 		    sc->sc_dev.dv_xname);
222 		return;
223 	}
224 
225 	if (bus_space_subregion(bst, bsh0, lestd[1], LER1_SIZE, &bsh1)) {
226 		printf("\n%s: can't subregion LANCE space\n",
227 		    sc->sc_dev.dv_xname);
228 		return;
229 	}
230 
231 	if (bus_space_subregion(bst, bsh0, lestd[2], LE_BUFSIZE, &bsh2)) {
232 		printf("\n%s: can't subregion buffer space\n",
233 		    sc->sc_dev.dv_xname);
234 		return;
235 	}
236 
237 	lesc->sc_bst = bst;
238 	lesc->sc_bsh0 = bsh0;
239 	lesc->sc_bsh1 = bsh1;
240 	lesc->sc_bsh2 = bsh2;
241 
242 	bus_space_write_1(bst, bsh0, LER0_ID, 0xff);
243 	DELAY(100);
244 
245 	ipl = DIO_IPL((caddr_t)bsh0);			/* XXX */
246 	printf(" ipl %d", ipl);
247 
248 	sc->sc_conf3 = LE_C3_BSWP;
249 	sc->sc_addr = 0;
250 	sc->sc_memsize = LE_BUFSIZE;
251 
252 	/*
253 	 * Read the ethernet address off the board, one nibble at a time.
254 	 */
255 	offset = lestd[3];
256 	for (i = 0; i < sizeof(sc->sc_enaddr); i++) {
257 		sc->sc_enaddr[i] =
258 		    (bus_space_read_1(bst, bsh0, ++offset) & 0xf) << 4;
259 		offset++;
260 		sc->sc_enaddr[i] |=
261 		    (bus_space_read_1(bst, bsh0, ++offset) & 0xf);
262 		offset++;
263 	}
264 
265 	sc->sc_copytodesc = le_copytobuf;
266 	sc->sc_copyfromdesc = le_copyfrombuf;
267 	sc->sc_copytobuf = le_copytobuf;
268 	sc->sc_copyfrombuf = le_copyfrombuf;
269 	sc->sc_zerobuf = le_zerobuf;
270 
271 	sc->sc_rdcsr = lerdcsr;
272 	sc->sc_wrcsr = lewrcsr;
273 	sc->sc_hwinit = NULL;
274 
275 	am7990_config(&lesc->sc_am7990);
276 
277 	/* Establish the interrupt handler. */
278 	(void) dio_intr_establish(leintr, sc, ipl, IPL_NET);
279 	bus_space_write_1(bst, bsh0, LER0_STATUS, LE_IE);
280 }
281 
282 int
283 leintr(arg)
284 	void *arg;
285 {
286 	struct lance_softc *sc = arg;
287 #ifdef USELEDS
288 	u_int16_t isr;
289 
290 	isr = lerdcsr(sc, LE_CSR0);
291 
292 	if ((isr & LE_C0_INTR) == 0)
293 		return (0);
294 
295 	if (isr & LE_C0_RINT)
296 		ledcontrol(0, 0, LED_LANRCV);
297 
298 	if (isr & LE_C0_TINT)
299 		ledcontrol(0, 0, LED_LANXMT);
300 #endif /* USELEDS */
301 
302 	return (am7990_intr(sc));
303 }
304 
305 hide void
306 le_copytobuf(sc, from, boff, len)
307 	struct lance_softc *sc;
308 	void *from;
309 	int boff, len;
310 {
311 	struct le_softc *lesc = (struct le_softc *)sc;
312 
313 	bus_space_write_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, from, len);
314 }
315 
316 hide void
317 le_copyfrombuf(sc, to, boff, len)
318 	struct lance_softc *sc;
319 	void *to;
320 	int boff, len;
321 {
322 	struct le_softc *lesc = (struct le_softc *)sc;
323 
324 	bus_space_read_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, to, len);
325 }
326 
327 hide void
328 le_zerobuf(sc, boff, len)
329 	struct lance_softc *sc;
330 	int boff, len;
331 {
332 	struct le_softc *lesc = (struct le_softc *)sc;
333 
334 	bus_space_set_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, 0, len);
335 }
336