xref: /netbsd-src/sys/dev/isa/if_le_isa.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: if_le_isa.c,v 1.23 1998/08/15 10:51:19 mycroft 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 and by Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center.
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  * Copyright (c) 1992, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * This code is derived from software contributed to Berkeley by
45  * Ralph Campbell and Rick Macklem.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. All advertising materials mentioning features or use of this software
56  *    must display the following acknowledgement:
57  *	This product includes software developed by the University of
58  *	California, Berkeley and its contributors.
59  * 4. Neither the name of the University nor the names of its contributors
60  *    may be used to endorse or promote products derived from this software
61  *    without specific prior written permission.
62  *
63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73  * SUCH DAMAGE.
74  *
75  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
76  */
77 
78 #include "opt_inet.h"
79 #include "bpfilter.h"
80 
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/mbuf.h>
84 #include <sys/syslog.h>
85 #include <sys/socket.h>
86 #include <sys/device.h>
87 
88 #include <net/if.h>
89 #include <net/if_ether.h>
90 #include <net/if_media.h>
91 
92 #ifdef INET
93 #include <netinet/in.h>
94 #include <netinet/if_inarp.h>
95 #endif
96 
97 #include <vm/vm.h>
98 
99 #include <machine/cpu.h>
100 #include <machine/intr.h>
101 #include <machine/bus.h>
102 
103 #include <dev/isa/isareg.h>
104 #include <dev/isa/isavar.h>
105 #include <dev/isa/isadmavar.h>
106 
107 #include <dev/ic/lancereg.h>
108 #include <dev/ic/lancevar.h>
109 #include <dev/ic/am7990reg.h>
110 #include <dev/ic/am7990var.h>
111 
112 #include <dev/isa/if_levar.h>
113 
114 int ne2100_isa_probe __P((struct device *, struct cfdata *, void *));
115 int bicc_isa_probe __P((struct device *, struct cfdata *, void *));
116 void le_dummyattach __P((struct device *, struct device *, void *));
117 int le_dummyprobe __P((struct device *, struct cfdata *, void *));
118 void le_ne2100_attach __P((struct device *, struct device *, void *));
119 void le_bicc_attach __P((struct device *, struct device *, void *));
120 
121 struct cfattach nele_ca = {
122 	sizeof(struct device), ne2100_isa_probe, le_dummyattach
123 }, le_nele_ca = {
124 	sizeof(struct le_softc), le_dummyprobe, le_ne2100_attach
125 }, bicc_ca = {
126 	sizeof(struct device), bicc_isa_probe, le_dummyattach
127 }, le_bicc_ca = {
128 	sizeof(struct le_softc), le_dummyprobe, le_bicc_attach
129 };
130 
131 struct le_isa_params {
132 	char *name;
133 	int iosize, rap, rdp;
134 	int macstart, macstride;
135 } ne2100_params = {
136 	"NE2100",
137 	24, NE2100_RAP, NE2100_RDP,
138 	0, 1
139 }, bicc_params = {
140 	"BICC Isolan",
141 	16, BICC_RAP, BICC_RDP,
142 	0, 2
143 };
144 
145 int lance_isa_probe __P((struct isa_attach_args *, struct le_isa_params *));
146 void le_isa_attach __P((struct device *, struct le_softc *,
147 			struct isa_attach_args *, struct le_isa_params *));
148 
149 int le_isa_intredge __P((void *));
150 
151 #if defined(_KERNEL) && !defined(_LKM)
152 #include "opt_ddb.h"
153 #endif
154 
155 #ifdef DDB
156 #define	integrate
157 #define hide
158 #else
159 #define	integrate	static __inline
160 #define hide		static
161 #endif
162 
163 hide void le_isa_wrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
164 hide u_int16_t le_isa_rdcsr __P((struct lance_softc *, u_int16_t));
165 
166 #define	LE_ISA_MEMSIZE	16384
167 
168 hide void
169 le_isa_wrcsr(sc, port, val)
170 	struct lance_softc *sc;
171 	u_int16_t port, val;
172 {
173 	struct le_softc *lesc = (struct le_softc *)sc;
174 	bus_space_tag_t iot = lesc->sc_iot;
175 	bus_space_handle_t ioh = lesc->sc_ioh;
176 
177 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
178 	bus_space_write_2(iot, ioh, lesc->sc_rdp, val);
179 }
180 
181 hide u_int16_t
182 le_isa_rdcsr(sc, port)
183 	struct lance_softc *sc;
184 	u_int16_t port;
185 {
186 	struct le_softc *lesc = (struct le_softc *)sc;
187 	bus_space_tag_t iot = lesc->sc_iot;
188 	bus_space_handle_t ioh = lesc->sc_ioh;
189 	u_int16_t val;
190 
191 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
192 	val = bus_space_read_2(iot, ioh, lesc->sc_rdp);
193 	return (val);
194 }
195 
196 int
197 ne2100_isa_probe(parent, match, aux)
198 	struct device *parent;
199 	struct cfdata *match;
200 	void *aux;
201 {
202 	return (lance_isa_probe(aux, &ne2100_params));
203 }
204 
205 int
206 bicc_isa_probe(parent, match, aux)
207 	struct device *parent;
208 	struct cfdata *match;
209 	void *aux;
210 {
211 	return (lance_isa_probe(aux, &bicc_params));
212 }
213 
214 /*
215  * Determine which chip is present on the card.
216  */
217 int
218 lance_isa_probe(ia, p)
219 	struct isa_attach_args *ia;
220 	struct le_isa_params *p;
221 {
222 	bus_space_tag_t iot = ia->ia_iot;
223 	bus_space_handle_t ioh;
224 	int rap, rdp;
225 	int rv = 0;
226 
227 	/* Disallow wildcarded i/o address. */
228 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
229 		return (0);
230 
231 	/* Map i/o space. */
232 	if (bus_space_map(iot, ia->ia_iobase, p->iosize, 0, &ioh))
233 		return (0);
234 
235 	rap = p->rap;
236 	rdp = p->rdp;
237 
238 	/* Stop the LANCE chip and put it in a known state. */
239 	bus_space_write_2(iot, ioh, rap, LE_CSR0);
240 	bus_space_write_2(iot, ioh, rdp, LE_C0_STOP);
241 	delay(100);
242 
243 	bus_space_write_2(iot, ioh, rap, LE_CSR0);
244 	if (bus_space_read_2(iot, ioh, rdp) != LE_C0_STOP)
245 		goto bad;
246 
247 	bus_space_write_2(iot, ioh, rap, LE_CSR3);
248 	bus_space_write_2(iot, ioh, rdp, 0);
249 
250 	ia->ia_iosize = p->iosize;
251 	rv = 1;
252 
253 bad:
254 	bus_space_unmap(iot, ioh, p->iosize);
255 	return (rv);
256 }
257 
258 void
259 le_dummyattach(parent, self, aux)
260 	struct device *parent, *self;
261 	void *aux;
262 {
263 	printf("\n");
264 
265 	config_found(self, aux, 0);
266 }
267 
268 int
269 le_dummyprobe(parent, match, aux)
270 	struct device *parent;
271 	struct cfdata *match;
272 	void *aux;
273 {
274 	return (1);
275 }
276 
277 void
278 le_ne2100_attach(parent, self, aux)
279 	struct device *parent, *self;
280 	void *aux;
281 {
282 	le_isa_attach(parent, (void *)self, aux, &ne2100_params);
283 }
284 
285 void
286 le_bicc_attach(parent, self, aux)
287 	struct device *parent, *self;
288 	void *aux;
289 {
290 	le_isa_attach(parent, (void *)self, aux, &bicc_params);
291 }
292 
293 void
294 le_isa_attach(parent, lesc, ia, p)
295 	struct device *parent;
296 	struct le_softc *lesc;
297 	struct isa_attach_args *ia;
298 	struct le_isa_params *p;
299 {
300 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
301 	bus_space_tag_t iot = ia->ia_iot;
302 	bus_space_handle_t ioh;
303 	bus_dma_tag_t dmat = ia->ia_dmat;
304 	bus_dma_segment_t seg;
305 	int i, rseg, error;
306 
307 	printf(": %s Ethernet\n", p->name);
308 
309 	if (bus_space_map(iot, ia->ia_iobase, p->iosize, 0, &ioh))
310 		panic("%s: can't map io", sc->sc_dev.dv_xname);
311 
312 	/*
313 	 * Extract the physical MAC address from the ROM.
314 	 */
315 	for (i = 0; i < sizeof(sc->sc_enaddr); i++)
316 		sc->sc_enaddr[i] =
317 		    bus_space_read_1(iot, ioh, p->macstart + i * p->macstride);
318 
319 	lesc->sc_iot = iot;
320 	lesc->sc_ioh = ioh;
321 	lesc->sc_dmat = dmat;
322 	lesc->sc_rap = p->rap;
323 	lesc->sc_rdp = p->rdp;
324 
325 	/*
326 	 * Allocate a DMA area for the card.
327 	 */
328 	if (bus_dmamem_alloc(dmat, LE_ISA_MEMSIZE, NBPG, 0, &seg, 1,
329 			     &rseg, BUS_DMA_NOWAIT)) {
330 		printf("%s: couldn't allocate memory for card\n",
331 		       sc->sc_dev.dv_xname);
332 		return;
333 	}
334 	if (bus_dmamem_map(dmat, &seg, rseg, LE_ISA_MEMSIZE,
335 			   (caddr_t *)&sc->sc_mem,
336 			   BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
337 		printf("%s: couldn't map memory for card\n",
338 		       sc->sc_dev.dv_xname);
339 		return;
340 	}
341 
342 	/*
343 	 * Create and load the DMA map for the DMA area.
344 	 */
345 	if (bus_dmamap_create(dmat, LE_ISA_MEMSIZE, 1,
346 			LE_ISA_MEMSIZE, 0, BUS_DMA_NOWAIT, &lesc->sc_dmam)) {
347 		printf("%s: couldn't create DMA map\n",
348 		       sc->sc_dev.dv_xname);
349 		bus_dmamem_free(dmat, &seg, rseg);
350 		return;
351 	}
352 	if (bus_dmamap_load(dmat, lesc->sc_dmam,
353 			sc->sc_mem, LE_ISA_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
354 		printf("%s: coundn't load DMA map\n",
355 		       sc->sc_dev.dv_xname);
356 		bus_dmamem_free(dmat, &seg, rseg);
357 		return;
358 	}
359 
360 	sc->sc_conf3 = 0;
361 	sc->sc_addr = lesc->sc_dmam->dm_segs[0].ds_addr;
362 	sc->sc_memsize = LE_ISA_MEMSIZE;
363 
364 	sc->sc_copytodesc = lance_copytobuf_contig;
365 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
366 	sc->sc_copytobuf = lance_copytobuf_contig;
367 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
368 	sc->sc_zerobuf = lance_zerobuf_contig;
369 
370 	sc->sc_rdcsr = le_isa_rdcsr;
371 	sc->sc_wrcsr = le_isa_wrcsr;
372 	sc->sc_hwinit = NULL;
373 
374 	if (ia->ia_drq != DRQUNK) {
375 		if ((error = isa_dmacascade(ia->ia_ic, ia->ia_drq)) != 0) {
376 			printf("%s: unable to cascade DRQ, error = %d\n",
377 			    sc->sc_dev.dv_xname, error);
378 			return;
379 		}
380 	}
381 
382 	lesc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
383 	    IPL_NET, le_isa_intredge, sc);
384 
385 	printf("%s", sc->sc_dev.dv_xname);
386 	am7990_config(&lesc->sc_am7990);
387 }
388 
389 /*
390  * Controller interrupt.
391  */
392 int
393 le_isa_intredge(arg)
394 	void *arg;
395 {
396 
397 	if (am7990_intr(arg) == 0)
398 		return (0);
399 	for (;;)
400 		if (am7990_intr(arg) == 0)
401 			return (1);
402 }
403