xref: /freebsd/sys/dev/le/if_le_isa.c (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
1 /*	$NetBSD: if_le_isa.c,v 1.41 2005/12/24 20:27:41 perry Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause AND BSD-3-Clause
5  *
6  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
11  * Simulation Facility, NASA Ames Research Center.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*-
36  * Copyright (c) 1992, 1993
37  *	The Regents of the University of California.  All rights reserved.
38  *
39  * This code is derived from software contributed to Berkeley by
40  * Ralph Campbell and Rick Macklem.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  */
66 
67 #include <sys/cdefs.h>
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/bus.h>
71 #include <sys/endian.h>
72 #include <sys/kernel.h>
73 #include <sys/lock.h>
74 #include <sys/module.h>
75 #include <sys/mutex.h>
76 #include <sys/resource.h>
77 #include <sys/rman.h>
78 #include <sys/socket.h>
79 
80 #include <net/ethernet.h>
81 #include <net/if.h>
82 #include <net/if_media.h>
83 
84 #include <machine/bus.h>
85 #include <machine/resource.h>
86 
87 #include <isa/isavar.h>
88 
89 #include <dev/le/lancereg.h>
90 #include <dev/le/lancevar.h>
91 #include <dev/le/am7990var.h>
92 
93 #define	LE_ISA_MEMSIZE	(16*1024)
94 #define	PCNET_RDP	0x10
95 #define	PCNET_RAP	0x12
96 
97 struct le_isa_softc {
98 	struct am7990_softc	sc_am7990;	/* glue to MI code */
99 
100 	bus_size_t		sc_rap;		/* offsets to LANCE... */
101 	bus_size_t		sc_rdp;		/* ...registers */
102 
103 	struct resource		*sc_rres;
104 
105 	struct resource		*sc_dres;
106 
107 	struct resource		*sc_ires;
108 	void			*sc_ih;
109 
110 	bus_dma_tag_t		sc_pdmat;
111 	bus_dma_tag_t		sc_dmat;
112 	bus_dmamap_t		sc_dmam;
113 };
114 
115 static device_probe_t le_isa_probe;
116 static device_attach_t le_isa_attach;
117 static device_detach_t le_isa_detach;
118 static device_resume_t le_isa_resume;
119 static device_suspend_t le_isa_suspend;
120 
121 static device_method_t le_isa_methods[] = {
122 	/* Device interface */
123 	DEVMETHOD(device_probe,		le_isa_probe),
124 	DEVMETHOD(device_attach,	le_isa_attach),
125 	DEVMETHOD(device_detach,	le_isa_detach),
126 	/* We can just use the suspend method here. */
127 	DEVMETHOD(device_shutdown,	le_isa_suspend),
128 	DEVMETHOD(device_suspend,	le_isa_suspend),
129 	DEVMETHOD(device_resume,	le_isa_resume),
130 
131 	{ 0, 0 }
132 };
133 
134 struct le_isa_param {
135 	const char	*name;
136 	u_long		iosize;
137 	bus_size_t	rap;
138 	bus_size_t	rdp;
139 	bus_size_t	macstart;
140 	int		macstride;
141 } static const le_isa_params[] = {
142 	{ "BICC Isolan", 24, 0xe, 0xc, 0, 2 },
143 	{ "Novell NE2100", 16, 0x12, 0x10, 0, 1 }
144 };
145 
146 static struct isa_pnp_id le_isa_ids[] = {
147 	{ 0x0322690e, "Cabletron E2200 Single Chip" },	/* CSI2203 */
148 	{ 0x0110490a, "Boca LANCard Combo" },		/* BRI1001 */
149 	{ 0x0100a60a, "Melco Inc. LGY-IV" },		/* BUF0001 */
150 	{ 0xd880d041, "Novell NE2100" },		/* PNP80D8 */
151 	{ 0x0082d041, "Cabletron E2100 Series DNI" },	/* PNP8200 */
152 	{ 0x3182d041, "AMD AM1500T/AM2100" },		/* PNP8231 */
153 	{ 0x8c82d041, "AMD PCnet-ISA" },		/* PNP828C */
154 	{ 0x8d82d041, "AMD PCnet-32" },			/* PNP828D */
155 	{ 0xcefaedfe, "Racal InterLan EtherBlaster" },	/* _WMFACE */
156 	{ 0, NULL }
157 };
158 
159 static void le_isa_wrcsr(struct lance_softc *, uint16_t, uint16_t);
160 static uint16_t le_isa_rdcsr(struct lance_softc *, uint16_t);
161 static bus_dmamap_callback_t le_isa_dma_callback;
162 static int le_isa_probe_legacy(device_t, const struct le_isa_param *);
163 
164 static void
165 le_isa_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
166 {
167 	struct le_isa_softc *lesc = (struct le_isa_softc *)sc;
168 
169 	bus_write_2(lesc->sc_rres, lesc->sc_rap, port);
170 	bus_barrier(lesc->sc_rres, lesc->sc_rap, 2, BUS_SPACE_BARRIER_WRITE);
171 	bus_write_2(lesc->sc_rres, lesc->sc_rdp, val);
172 }
173 
174 static uint16_t
175 le_isa_rdcsr(struct lance_softc *sc, uint16_t port)
176 {
177 	struct le_isa_softc *lesc = (struct le_isa_softc *)sc;
178 
179 	bus_write_2(lesc->sc_rres, lesc->sc_rap, port);
180 	bus_barrier(lesc->sc_rres, lesc->sc_rap, 2, BUS_SPACE_BARRIER_WRITE);
181 	return (bus_read_2(lesc->sc_rres, lesc->sc_rdp));
182 }
183 
184 static void
185 le_isa_dma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
186 {
187 	struct lance_softc *sc = (struct lance_softc *)xsc;
188 
189 	if (error != 0)
190 		return;
191 	KASSERT(nsegs == 1, ("%s: bad DMA segment count", __func__));
192 	sc->sc_addr = segs[0].ds_addr;
193 }
194 
195 static int
196 le_isa_probe_legacy(device_t dev, const struct le_isa_param *leip)
197 {
198 	struct le_isa_softc *lesc;
199 	struct lance_softc *sc;
200 	int error, i;
201 
202 	lesc = device_get_softc(dev);
203 	sc = &lesc->sc_am7990.lsc;
204 
205 	i = 0;
206 	lesc->sc_rres = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &i,
207 	    leip->iosize, RF_ACTIVE);
208 	if (lesc->sc_rres == NULL)
209 		return (ENXIO);
210 	lesc->sc_rap = leip->rap;
211 	lesc->sc_rdp = leip->rdp;
212 
213 	/* Stop the chip and put it in a known state. */
214 	le_isa_wrcsr(sc, LE_CSR0, LE_C0_STOP);
215 	DELAY(100);
216 	if (le_isa_rdcsr(sc, LE_CSR0) != LE_C0_STOP) {
217 		error = ENXIO;
218 		goto fail;
219 	}
220 	le_isa_wrcsr(sc, LE_CSR3, 0);
221 	error = 0;
222 
223  fail:
224 	bus_release_resource(dev, SYS_RES_IOPORT,
225 	    rman_get_rid(lesc->sc_rres), lesc->sc_rres);
226 	return (error);
227 }
228 
229 static int
230 le_isa_probe(device_t dev)
231 {
232 	int i;
233 
234 	switch (ISA_PNP_PROBE(device_get_parent(dev), dev, le_isa_ids)) {
235 	case 0:
236 		return (BUS_PROBE_DEFAULT);
237 	case ENOENT:
238 		for (i = 0; i < nitems(le_isa_params); i++) {
239 			if (le_isa_probe_legacy(dev, &le_isa_params[i]) == 0) {
240 				device_set_desc(dev, le_isa_params[i].name);
241 				return (BUS_PROBE_DEFAULT);
242 			}
243 		}
244 		/* FALLTHROUGH */
245 	case ENXIO:
246 	default:
247 		return (ENXIO);
248 	}
249 }
250 
251 static int
252 le_isa_attach(device_t dev)
253 {
254 	struct le_isa_softc *lesc;
255 	struct lance_softc *sc;
256 	bus_size_t macstart, rap, rdp;
257 	int error, i, j, macstride;
258 
259 	lesc = device_get_softc(dev);
260 	sc = &lesc->sc_am7990.lsc;
261 
262 	LE_LOCK_INIT(sc, device_get_nameunit(dev));
263 
264 	j = 0;
265 	switch (ISA_PNP_PROBE(device_get_parent(dev), dev, le_isa_ids)) {
266 	case 0:
267 		lesc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
268 		    &j, RF_ACTIVE);
269 		rap = PCNET_RAP;
270 		rdp = PCNET_RDP;
271 		macstart = 0;
272 		macstride = 1;
273 		break;
274 	case ENOENT:
275 		for (i = 0; i < nitems(le_isa_params); i++) {
276 			if (le_isa_probe_legacy(dev, &le_isa_params[i]) == 0) {
277 				lesc->sc_rres = bus_alloc_resource_anywhere(dev,
278 				    SYS_RES_IOPORT, &j,
279 				    le_isa_params[i].iosize, RF_ACTIVE);
280 				rap = le_isa_params[i].rap;
281 				rdp = le_isa_params[i].rdp;
282 				macstart = le_isa_params[i].macstart;
283 				macstride = le_isa_params[i].macstride;
284 				goto found;
285 			}
286 		}
287 		/* FALLTHROUGH */
288 	case ENXIO:
289 	default:
290 		device_printf(dev, "cannot determine chip\n");
291 		error = ENXIO;
292 		goto fail_mtx;
293 	}
294 
295  found:
296 	if (lesc->sc_rres == NULL) {
297 		device_printf(dev, "cannot allocate registers\n");
298 		error = ENXIO;
299 		goto fail_mtx;
300 	}
301 	lesc->sc_rap = rap;
302 	lesc->sc_rdp = rdp;
303 
304 	i = 0;
305 	if ((lesc->sc_dres = bus_alloc_resource_any(dev, SYS_RES_DRQ,
306 	    &i, RF_ACTIVE)) == NULL) {
307 		device_printf(dev, "cannot allocate DMA channel\n");
308 		error = ENXIO;
309 		goto fail_rres;
310 	}
311 
312 	i = 0;
313 	if ((lesc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
314 	    &i, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
315 		device_printf(dev, "cannot allocate interrupt\n");
316 		error = ENXIO;
317 		goto fail_dres;
318 	}
319 
320 	error = bus_dma_tag_create(
321 	    bus_get_dma_tag(dev),	/* parent */
322 	    1, 0,			/* alignment, boundary */
323 	    BUS_SPACE_MAXADDR_24BIT,	/* lowaddr */
324 	    BUS_SPACE_MAXADDR,		/* highaddr */
325 	    NULL, NULL,			/* filter, filterarg */
326 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
327 	    0,				/* nsegments */
328 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
329 	    0,				/* flags */
330 	    NULL, NULL,			/* lockfunc, lockarg */
331 	    &lesc->sc_pdmat);
332 	if (error != 0) {
333 		device_printf(dev, "cannot allocate parent DMA tag\n");
334 		goto fail_ires;
335 	}
336 
337 	sc->sc_memsize = LE_ISA_MEMSIZE;
338 	/*
339 	 * For Am79C90, Am79C961 and Am79C961A the init block must be 2-byte
340 	 * aligned and the ring descriptors must be 8-byte aligned.
341 	 */
342 	error = bus_dma_tag_create(
343 	    lesc->sc_pdmat,		/* parent */
344 	    8, 0,			/* alignment, boundary */
345 	    BUS_SPACE_MAXADDR_24BIT,	/* lowaddr */
346 	    BUS_SPACE_MAXADDR,		/* highaddr */
347 	    NULL, NULL,			/* filter, filterarg */
348 	    sc->sc_memsize,		/* maxsize */
349 	    1,				/* nsegments */
350 	    sc->sc_memsize,		/* maxsegsize */
351 	    0,				/* flags */
352 	    NULL, NULL,			/* lockfunc, lockarg */
353 	    &lesc->sc_dmat);
354 	if (error != 0) {
355 		device_printf(dev, "cannot allocate buffer DMA tag\n");
356 		goto fail_pdtag;
357 	}
358 
359 	error = bus_dmamem_alloc(lesc->sc_dmat, (void **)&sc->sc_mem,
360 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT, &lesc->sc_dmam);
361 	if (error != 0) {
362 		device_printf(dev, "cannot allocate DMA buffer memory\n");
363 		goto fail_dtag;
364 	}
365 
366 	sc->sc_addr = 0;
367 	error = bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmam, sc->sc_mem,
368 	    sc->sc_memsize, le_isa_dma_callback, sc, 0);
369 	if (error != 0 || sc->sc_addr == 0) {
370 		device_printf(dev, "cannot load DMA buffer map\n");
371 		goto fail_dmem;
372 	}
373 
374 	isa_dmacascade(rman_get_start(lesc->sc_dres));
375 
376 	sc->sc_flags = 0;
377 	sc->sc_conf3 = 0;
378 
379 	/*
380 	 * Extract the physical MAC address from the ROM.
381 	 */
382 	for (i = 0; i < sizeof(sc->sc_enaddr); i++)
383 		sc->sc_enaddr[i] = bus_read_1(lesc->sc_rres,
384 		    macstart + i * macstride);
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_hwreset = NULL;
395 	sc->sc_hwinit = NULL;
396 	sc->sc_hwintr = NULL;
397 	sc->sc_nocarrier = NULL;
398 	sc->sc_mediachange = NULL;
399 	sc->sc_mediastatus = NULL;
400 	sc->sc_supmedia = NULL;
401 
402 	error = am7990_config(&lesc->sc_am7990, device_get_name(dev),
403 	    device_get_unit(dev));
404 	if (error != 0) {
405 		device_printf(dev, "cannot attach Am7990\n");
406 		goto fail_dmap;
407 	}
408 
409 	error = bus_setup_intr(dev, lesc->sc_ires, INTR_TYPE_NET | INTR_MPSAFE,
410 	    NULL, am7990_intr, sc, &lesc->sc_ih);
411 	if (error != 0) {
412 		device_printf(dev, "cannot set up interrupt\n");
413 		goto fail_am7990;
414 	}
415 
416 	return (0);
417 
418  fail_am7990:
419 	am7990_detach(&lesc->sc_am7990);
420  fail_dmap:
421 	bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
422  fail_dmem:
423 	bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
424  fail_dtag:
425 	bus_dma_tag_destroy(lesc->sc_dmat);
426  fail_pdtag:
427 	bus_dma_tag_destroy(lesc->sc_pdmat);
428  fail_ires:
429 	bus_release_resource(dev, SYS_RES_IRQ,
430 	    rman_get_rid(lesc->sc_ires), lesc->sc_ires);
431  fail_dres:
432 	bus_release_resource(dev, SYS_RES_DRQ,
433 	    rman_get_rid(lesc->sc_dres), lesc->sc_dres);
434  fail_rres:
435 	bus_release_resource(dev, SYS_RES_IOPORT,
436 	    rman_get_rid(lesc->sc_rres), lesc->sc_rres);
437  fail_mtx:
438 	LE_LOCK_DESTROY(sc);
439 	return (error);
440 }
441 
442 static int
443 le_isa_detach(device_t dev)
444 {
445 	struct le_isa_softc *lesc;
446 	struct lance_softc *sc;
447 
448 	lesc = device_get_softc(dev);
449 	sc = &lesc->sc_am7990.lsc;
450 
451 	bus_teardown_intr(dev, lesc->sc_ires, lesc->sc_ih);
452 	am7990_detach(&lesc->sc_am7990);
453 	bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
454 	bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
455 	bus_dma_tag_destroy(lesc->sc_dmat);
456 	bus_dma_tag_destroy(lesc->sc_pdmat);
457 	bus_release_resource(dev, SYS_RES_IRQ,
458 	    rman_get_rid(lesc->sc_ires), lesc->sc_ires);
459 	bus_release_resource(dev, SYS_RES_DRQ,
460 	    rman_get_rid(lesc->sc_dres), lesc->sc_dres);
461 	bus_release_resource(dev, SYS_RES_IOPORT,
462 	    rman_get_rid(lesc->sc_rres), lesc->sc_rres);
463 	LE_LOCK_DESTROY(sc);
464 
465 	return (0);
466 }
467 
468 static int
469 le_isa_suspend(device_t dev)
470 {
471 	struct le_isa_softc *lesc;
472 
473 	lesc = device_get_softc(dev);
474 
475 	lance_suspend(&lesc->sc_am7990.lsc);
476 
477 	return (0);
478 }
479 
480 static int
481 le_isa_resume(device_t dev)
482 {
483 	struct le_isa_softc *lesc;
484 
485 	lesc = device_get_softc(dev);
486 
487 	lance_resume(&lesc->sc_am7990.lsc);
488 
489 	return (0);
490 }
491 
492 DEFINE_CLASS_0(le, le_isa_driver, le_isa_methods, sizeof(struct le_isa_softc));
493 DRIVER_MODULE(le, isa, le_isa_driver, 0, 0);
494 MODULE_DEPEND(le, ether, 1, 1, 1);
495 ISA_PNP_INFO(le_isa_ids);
496