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