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