xref: /netbsd/sys/arch/hp300/dev/if_le.c (revision c4a72b64)
1 /*	$NetBSD: if_le.c,v 1.52 2002/10/20 05:54:29 gmcgarry 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 <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.52 2002/10/20 05:54:29 gmcgarry Exp $");
79 
80 #include "opt_inet.h"
81 #include "bpfilter.h"
82 
83 #include <sys/param.h>
84 #include <sys/systm.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 #include <dev/ic/lancereg.h>
92 #include <dev/ic/lancevar.h>
93 #include <dev/ic/am7990var.h>
94 
95 #include <hp300/dev/dioreg.h>
96 #include <hp300/dev/diovar.h>
97 #include <hp300/dev/diodevs.h>
98 #include <hp300/dev/if_lereg.h>
99 
100 #include "opt_useleds.h"
101 
102 #ifdef USELEDS
103 #include <hp300/hp300/leds.h>
104 #endif
105 
106 struct  le_softc {
107 	struct  am7990_softc sc_am7990; /* glue to MI code */
108 
109 	bus_space_tag_t sc_bst;
110 
111 	bus_space_handle_t sc_bsh0;	/* DIO registers */
112 	bus_space_handle_t sc_bsh1;	/* LANCE registers */
113 	bus_space_handle_t sc_bsh2;	/* buffer area */
114 };
115 
116 int	lematch(struct device *, struct cfdata *, void *);
117 void	leattach(struct device *, struct device *, void *);
118 
119 CFATTACH_DECL(le, sizeof(struct le_softc),
120     lematch, leattach, NULL, NULL);
121 
122 int	leintr(void *);
123 
124 #if defined(_KERNEL_OPT)
125 #include "opt_ddb.h"
126 #endif
127 
128 #ifdef DDB
129 #define	integrate
130 #define hide
131 #else
132 #define	integrate	static __inline
133 #define hide		static
134 #endif
135 
136 hide void le_copytobuf(struct lance_softc *, void *, int, int);
137 hide void le_copyfrombuf(struct lance_softc *, void *, int, int);
138 hide void le_zerobuf(struct lance_softc *, int, int);
139 
140 /* offsets for:	   ID,   REGS,    MEM,  NVRAM */
141 int	lestd[] = { 0, 0x4000, 0x8000, 0xC008 };
142 
143 hide void lewrcsr(struct lance_softc *, u_int16_t, u_int16_t);
144 hide u_int16_t lerdcsr(struct lance_softc *, u_int16_t);
145 
146 hide void
147 lewrcsr(struct lance_softc *sc, u_int16_t port, u_int16_t val)
148 {
149 	struct le_softc *lesc = (struct le_softc *)sc;
150 	bus_space_tag_t bst = lesc->sc_bst;
151 	bus_space_handle_t bsh0 = lesc->sc_bsh0;
152 	bus_space_handle_t bsh1 = lesc->sc_bsh1;
153 
154 	do {
155 		bus_space_write_2(bst, bsh1, LER1_RAP, port);
156 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
157 	do {
158 		bus_space_write_2(bst, bsh1, LER1_RDP, val);
159 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
160 }
161 
162 hide u_int16_t
163 lerdcsr(struct lance_softc *sc, u_int16_t port)
164 {
165 	struct le_softc *lesc = (struct le_softc *)sc;
166 	bus_space_tag_t bst = lesc->sc_bst;
167 	bus_space_handle_t bsh0 = lesc->sc_bsh0;
168 	bus_space_handle_t bsh1 = lesc->sc_bsh1;
169 	u_int16_t val;
170 
171 	do {
172 		bus_space_write_2(bst, bsh1, LER1_RAP, port);
173 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
174 	do {
175 		val = bus_space_read_2(bst, bsh1, LER1_RDP);
176 	} while ((bus_space_read_1(bst, bsh0, LER0_STATUS) & LE_ACK) == 0);
177 
178 	return (val);
179 }
180 
181 int
182 lematch(struct device *parent, struct cfdata *match, void *aux)
183 {
184 	struct dio_attach_args *da = aux;
185 
186 	if (da->da_id == DIO_DEVICE_ID_LAN)
187 		return (1);
188 	return (0);
189 }
190 
191 /*
192  * Interface exists: make available by filling in network interface
193  * record.  System will initialize the interface when it is ready
194  * to accept packets.
195  */
196 void
197 leattach(struct device *parent, struct device *self, void *aux)
198 {
199 	struct dio_attach_args *da = aux;
200 	struct le_softc *lesc = (struct le_softc *)self;
201 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
202 	bus_space_tag_t bst = da->da_bst;
203 	bus_space_handle_t bsh0, bsh1, bsh2;
204 	bus_size_t offset;
205 	int i, ipl;
206 
207 	if (bus_space_map(bst, (bus_addr_t)dio_scodetopa(da->da_scode),
208 	    da->da_size, 0, &bsh0)) {
209 		printf("\n%s: can't map LANCE registers\n",
210 		    sc->sc_dev.dv_xname);
211 		return;
212 	}
213 
214 	if (bus_space_subregion(bst, bsh0, lestd[1], LER1_SIZE, &bsh1)) {
215 		printf("\n%s: can't subregion LANCE space\n",
216 		    sc->sc_dev.dv_xname);
217 		return;
218 	}
219 
220 	if (bus_space_subregion(bst, bsh0, lestd[2], LE_BUFSIZE, &bsh2)) {
221 		printf("\n%s: can't subregion buffer space\n",
222 		    sc->sc_dev.dv_xname);
223 		return;
224 	}
225 
226 	lesc->sc_bst = bst;
227 	lesc->sc_bsh0 = bsh0;
228 	lesc->sc_bsh1 = bsh1;
229 	lesc->sc_bsh2 = bsh2;
230 
231 	bus_space_write_1(bst, bsh0, LER0_ID, 0xff);
232 	DELAY(100);
233 
234 	ipl = DIO_IPL((caddr_t)bsh0);			/* XXX */
235 	printf(" ipl %d", ipl);
236 
237 	sc->sc_conf3 = LE_C3_BSWP;
238 	sc->sc_addr = 0;
239 	sc->sc_memsize = LE_BUFSIZE;
240 
241 	/*
242 	 * Read the ethernet address off the board, one nibble at a time.
243 	 */
244 	offset = lestd[3];
245 	for (i = 0; i < sizeof(sc->sc_enaddr); i++) {
246 		sc->sc_enaddr[i] =
247 		    (bus_space_read_1(bst, bsh0, ++offset) & 0xf) << 4;
248 		offset++;
249 		sc->sc_enaddr[i] |=
250 		    (bus_space_read_1(bst, bsh0, ++offset) & 0xf);
251 		offset++;
252 	}
253 
254 	sc->sc_copytodesc = le_copytobuf;
255 	sc->sc_copyfromdesc = le_copyfrombuf;
256 	sc->sc_copytobuf = le_copytobuf;
257 	sc->sc_copyfrombuf = le_copyfrombuf;
258 	sc->sc_zerobuf = le_zerobuf;
259 
260 	sc->sc_rdcsr = lerdcsr;
261 	sc->sc_wrcsr = lewrcsr;
262 	sc->sc_hwinit = NULL;
263 
264 	am7990_config(&lesc->sc_am7990);
265 
266 	/* Establish the interrupt handler. */
267 	(void) dio_intr_establish(leintr, sc, ipl, IPL_NET);
268 	bus_space_write_1(bst, bsh0, LER0_STATUS, LE_IE);
269 }
270 
271 int
272 leintr(void *arg)
273 {
274 	struct lance_softc *sc = arg;
275 #ifdef USELEDS
276 	u_int16_t isr;
277 
278 	isr = lerdcsr(sc, LE_CSR0);
279 
280 	if ((isr & LE_C0_INTR) == 0)
281 		return (0);
282 
283 	if (isr & LE_C0_RINT)
284 		ledcontrol(0, 0, LED_LANRCV);
285 
286 	if (isr & LE_C0_TINT)
287 		ledcontrol(0, 0, LED_LANXMT);
288 #endif /* USELEDS */
289 
290 	return (am7990_intr(sc));
291 }
292 
293 hide void
294 le_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
295 {
296 	struct le_softc *lesc = (struct le_softc *)sc;
297 
298 	bus_space_write_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, from, len);
299 }
300 
301 hide void
302 le_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
303 {
304 	struct le_softc *lesc = (struct le_softc *)sc;
305 
306 	bus_space_read_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, to, len);
307 }
308 
309 hide void
310 le_zerobuf(struct lance_softc *sc, int boff, int len)
311 {
312 	struct le_softc *lesc = (struct le_softc *)sc;
313 
314 	bus_space_set_region_1(lesc->sc_bst, lesc->sc_bsh2, boff, 0, len);
315 }
316