xref: /netbsd/sys/dev/isa/if_le_isa.c (revision c4a72b64)
1 /*	$NetBSD: if_le_isa.c,v 1.34 2002/10/02 03:10:48 thorpej 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 <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: if_le_isa.c,v 1.34 2002/10/02 03:10:48 thorpej Exp $");
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 <uvm/uvm_extern.h>
89 
90 #include <net/if.h>
91 #include <net/if_ether.h>
92 #include <net/if_media.h>
93 
94 #include <machine/cpu.h>
95 #include <machine/intr.h>
96 #include <machine/bus.h>
97 
98 #include <dev/isa/isareg.h>
99 #include <dev/isa/isavar.h>
100 #include <dev/isa/isadmavar.h>
101 
102 #include <dev/ic/lancereg.h>
103 #include <dev/ic/lancevar.h>
104 #include <dev/ic/am7990reg.h>
105 #include <dev/ic/am7990var.h>
106 
107 #include <dev/isa/if_levar.h>
108 
109 int ne2100_isa_probe __P((struct device *, struct cfdata *, void *));
110 int bicc_isa_probe __P((struct device *, struct cfdata *, void *));
111 void le_dummyattach __P((struct device *, struct device *, void *));
112 int le_dummyprobe __P((struct device *, struct cfdata *, void *));
113 void le_ne2100_attach __P((struct device *, struct device *, void *));
114 void le_bicc_attach __P((struct device *, struct device *, void *));
115 
116 CFATTACH_DECL(nele, sizeof(struct device),
117     ne2100_isa_probe, le_dummyattach, NULL, NULL);
118 
119 CFATTACH_DECL(le_nele, sizeof(struct le_softc),
120     le_dummyprobe, le_ne2100_attach, NULL, NULL);
121 
122 CFATTACH_DECL(bicc, sizeof(struct device),
123     bicc_isa_probe, le_dummyattach, NULL, NULL);
124 
125 CFATTACH_DECL(le_bicc, sizeof(struct le_softc),
126     le_dummyprobe, le_bicc_attach, NULL, NULL);
127 
128 struct le_isa_params {
129 	char *name;
130 	int iosize, rap, rdp;
131 	int macstart, macstride;
132 } ne2100_params = {
133 	"NE2100",
134 	24, NE2100_RAP, NE2100_RDP,
135 	0, 1
136 }, bicc_params = {
137 	"BICC Isolan",
138 	16, BICC_RAP, BICC_RDP,
139 	0, 2
140 };
141 
142 int lance_isa_probe __P((struct isa_attach_args *,
143 			 struct le_isa_params *, int));
144 void le_isa_attach __P((struct device *, struct le_softc *,
145 			struct isa_attach_args *, struct le_isa_params *));
146 
147 int le_isa_intredge __P((void *));
148 
149 #if defined(_KERNEL_OPT)
150 #include "opt_ddb.h"
151 #endif
152 
153 #ifdef DDB
154 #define	integrate
155 #define hide
156 #else
157 #define	integrate	static __inline
158 #define hide		static
159 #endif
160 
161 hide void le_isa_wrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
162 hide u_int16_t le_isa_rdcsr __P((struct lance_softc *, u_int16_t));
163 
164 #define	LE_ISA_MEMSIZE	16384
165 
166 hide void
167 le_isa_wrcsr(sc, port, val)
168 	struct lance_softc *sc;
169 	u_int16_t port, val;
170 {
171 	struct le_softc *lesc = (struct le_softc *)sc;
172 	bus_space_tag_t iot = lesc->sc_iot;
173 	bus_space_handle_t ioh = lesc->sc_ioh;
174 
175 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
176 	bus_space_write_2(iot, ioh, lesc->sc_rdp, val);
177 }
178 
179 hide u_int16_t
180 le_isa_rdcsr(sc, port)
181 	struct lance_softc *sc;
182 	u_int16_t port;
183 {
184 	struct le_softc *lesc = (struct le_softc *)sc;
185 	bus_space_tag_t iot = lesc->sc_iot;
186 	bus_space_handle_t ioh = lesc->sc_ioh;
187 	u_int16_t val;
188 
189 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
190 	val = bus_space_read_2(iot, ioh, lesc->sc_rdp);
191 	return (val);
192 }
193 
194 int
195 ne2100_isa_probe(parent, match, aux)
196 	struct device *parent;
197 	struct cfdata *match;
198 	void *aux;
199 {
200 	return (lance_isa_probe(aux, &ne2100_params, match->cf_flags));
201 }
202 
203 int
204 bicc_isa_probe(parent, match, aux)
205 	struct device *parent;
206 	struct cfdata *match;
207 	void *aux;
208 {
209 	return (lance_isa_probe(aux, &bicc_params, match->cf_flags));
210 }
211 
212 /*
213  * Determine which chip is present on the card.
214  */
215 int
216 lance_isa_probe(ia, p, flags)
217 	struct isa_attach_args *ia;
218 	struct le_isa_params *p;
219 	int flags;
220 {
221 	bus_space_tag_t iot = ia->ia_iot;
222 	bus_space_handle_t ioh;
223 	int rap, rdp;
224 	int rv = 0;
225 
226 	if (ia->ia_nio < 1)
227 		return (0);
228 	if (ia->ia_nirq < 1)
229 		return (0);
230 	if (ia->ia_ndrq < 1)
231 		return (0);
232 
233 	if (ISA_DIRECT_CONFIG(ia))
234 		return (0);
235 
236 	/* Disallow wildcarded i/o address. */
237 	if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
238 		return (0);
239 	if (ia->ia_irq[0].ir_irq == ISACF_IRQ_DEFAULT)
240 		return (0);
241 	if ((flags & LANCEISA_FLAG_LOCALBUS) == 0 &&
242 	    ia->ia_drq[0].ir_drq == ISACF_DRQ_DEFAULT)
243 		return (0);
244 
245 	/* Map i/o space. */
246 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, p->iosize, 0, &ioh))
247 		return (0);
248 
249 	rap = p->rap;
250 	rdp = p->rdp;
251 
252 	/* Stop the LANCE chip and put it in a known state. */
253 	bus_space_write_2(iot, ioh, rap, LE_CSR0);
254 	bus_space_write_2(iot, ioh, rdp, LE_C0_STOP);
255 	delay(100);
256 
257 	bus_space_write_2(iot, ioh, rap, LE_CSR0);
258 	if (bus_space_read_2(iot, ioh, rdp) != LE_C0_STOP)
259 		goto bad;
260 
261 	bus_space_write_2(iot, ioh, rap, LE_CSR3);
262 	bus_space_write_2(iot, ioh, rdp, 0);
263 
264 	ia->ia_nio = 1;
265 	ia->ia_io[0].ir_size = p->iosize;
266 
267 	ia->ia_nirq = 1;
268 
269 	if ((flags & LANCEISA_FLAG_LOCALBUS) != 0 &&
270 	    ia->ia_drq[0].ir_drq == ISACF_DRQ_DEFAULT)
271 	    ia->ia_ndrq = 0;
272 	else
273 	    ia->ia_ndrq = 1;
274 
275 	ia->ia_niomem = 0;
276 
277 	rv = 1;
278 
279 bad:
280 	bus_space_unmap(iot, ioh, p->iosize);
281 	return (rv);
282 }
283 
284 void
285 le_dummyattach(parent, self, aux)
286 	struct device *parent, *self;
287 	void *aux;
288 {
289 	printf("\n");
290 
291 	config_found(self, aux, 0);
292 }
293 
294 int
295 le_dummyprobe(parent, match, aux)
296 	struct device *parent;
297 	struct cfdata *match;
298 	void *aux;
299 {
300 	return (1);
301 }
302 
303 void
304 le_ne2100_attach(parent, self, aux)
305 	struct device *parent, *self;
306 	void *aux;
307 {
308 	le_isa_attach(parent, (void *)self, aux, &ne2100_params);
309 }
310 
311 void
312 le_bicc_attach(parent, self, aux)
313 	struct device *parent, *self;
314 	void *aux;
315 {
316 	le_isa_attach(parent, (void *)self, aux, &bicc_params);
317 }
318 
319 void
320 le_isa_attach(parent, lesc, ia, p)
321 	struct device *parent;
322 	struct le_softc *lesc;
323 	struct isa_attach_args *ia;
324 	struct le_isa_params *p;
325 {
326 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
327 	bus_space_tag_t iot = ia->ia_iot;
328 	bus_space_handle_t ioh;
329 	bus_dma_tag_t dmat = ia->ia_dmat;
330 	bus_dma_segment_t seg;
331 	int i, rseg, error;
332 
333 	printf(": %s Ethernet\n", p->name);
334 
335 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, p->iosize, 0, &ioh))
336 		panic("%s: can't map io", sc->sc_dev.dv_xname);
337 
338 	/*
339 	 * Extract the physical MAC address from the ROM.
340 	 */
341 	for (i = 0; i < sizeof(sc->sc_enaddr); i++)
342 		sc->sc_enaddr[i] =
343 		    bus_space_read_1(iot, ioh, p->macstart + i * p->macstride);
344 
345 	lesc->sc_iot = iot;
346 	lesc->sc_ioh = ioh;
347 	lesc->sc_dmat = dmat;
348 	lesc->sc_rap = p->rap;
349 	lesc->sc_rdp = p->rdp;
350 
351 	/*
352 	 * Allocate a DMA area for the card.
353 	 */
354 	if (bus_dmamem_alloc(dmat, LE_ISA_MEMSIZE, PAGE_SIZE, 0, &seg, 1,
355 			     &rseg, BUS_DMA_NOWAIT)) {
356 		printf("%s: couldn't allocate memory for card\n",
357 		       sc->sc_dev.dv_xname);
358 		return;
359 	}
360 	if (bus_dmamem_map(dmat, &seg, rseg, LE_ISA_MEMSIZE,
361 			   (caddr_t *)&sc->sc_mem,
362 			   BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
363 		printf("%s: couldn't map memory for card\n",
364 		       sc->sc_dev.dv_xname);
365 		return;
366 	}
367 
368 	/*
369 	 * Create and load the DMA map for the DMA area.
370 	 */
371 	if (bus_dmamap_create(dmat, LE_ISA_MEMSIZE, 1,
372 			LE_ISA_MEMSIZE, 0, BUS_DMA_NOWAIT, &lesc->sc_dmam)) {
373 		printf("%s: couldn't create DMA map\n",
374 		       sc->sc_dev.dv_xname);
375 		bus_dmamem_free(dmat, &seg, rseg);
376 		return;
377 	}
378 	if (bus_dmamap_load(dmat, lesc->sc_dmam,
379 			sc->sc_mem, LE_ISA_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
380 		printf("%s: coundn't load DMA map\n",
381 		       sc->sc_dev.dv_xname);
382 		bus_dmamem_free(dmat, &seg, rseg);
383 		return;
384 	}
385 
386 	sc->sc_conf3 = 0;
387 	sc->sc_addr = lesc->sc_dmam->dm_segs[0].ds_addr;
388 	sc->sc_memsize = LE_ISA_MEMSIZE;
389 
390 	sc->sc_copytodesc = lance_copytobuf_contig;
391 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
392 	sc->sc_copytobuf = lance_copytobuf_contig;
393 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
394 	sc->sc_zerobuf = lance_zerobuf_contig;
395 
396 	sc->sc_rdcsr = le_isa_rdcsr;
397 	sc->sc_wrcsr = le_isa_wrcsr;
398 	sc->sc_hwinit = NULL;
399 
400 	if (ia->ia_ndrq > 0) {
401 		if ((error = isa_dmacascade(ia->ia_ic,
402 					    ia->ia_drq[0].ir_drq)) != 0) {
403 			printf("%s: unable to cascade DRQ, error = %d\n",
404 				    sc->sc_dev.dv_xname, error);
405 			return;
406 		}
407 	}
408 
409 	lesc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
410 	    IST_EDGE, IPL_NET, le_isa_intredge, sc);
411 
412 	printf("%s", sc->sc_dev.dv_xname);
413 	am7990_config(&lesc->sc_am7990);
414 }
415 
416 /*
417  * Controller interrupt.
418  */
419 int
420 le_isa_intredge(arg)
421 	void *arg;
422 {
423 
424 	if (am7990_intr(arg) == 0)
425 		return (0);
426 	for (;;)
427 		if (am7990_intr(arg) == 0)
428 			return (1);
429 }
430