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