xref: /netbsd/sys/dev/isa/if_lc_isa.c (revision bf9ec67e)
1 /*	$NetBSD: if_lc_isa.c,v 1.13 2002/01/07 21:47:08 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 1994, 1995, 1997 Matt Thomas <matt@3am-software.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  * DEC EtherWORKS 3 Ethernet Controllers
29  *
30  * Written by Matt Thomas
31  *
32  *   This driver supports the LEMAC (DE203, DE204, and DE205) cards.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: if_lc_isa.c,v 1.13 2002/01/07 21:47:08 thorpej Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
43 #include <sys/errno.h>
44 #include <sys/syslog.h>
45 #include <sys/select.h>
46 #include <sys/device.h>
47 #include <sys/queue.h>
48 
49 #include <net/if.h>
50 #include <net/if_dl.h>
51 #include <net/if_ether.h>
52 #include <net/if_media.h>
53 
54 #include <machine/cpu.h>
55 #include <machine/bus.h>
56 #include <machine/intr.h>
57 
58 #include <dev/ic/lemacreg.h>
59 #include <dev/ic/lemacvar.h>
60 
61 #include <dev/isa/isavar.h>
62 
63 extern struct cfdriver lc_cd;
64 
65 static int lemac_isa_find __P((lemac_softc_t *, struct isa_attach_args *,
66     int));
67 static int lemac_isa_probe __P((struct device *, struct cfdata *, void *));
68 static void lemac_isa_attach __P((struct device *, struct device *, void *));
69 
70 struct cfattach lc_isa_ca = {
71 	sizeof(lemac_softc_t), lemac_isa_probe, lemac_isa_attach
72 };
73 
74 static int
75 lemac_isa_find(sc, ia, attach)
76 	lemac_softc_t *sc;
77 	struct isa_attach_args *ia;
78 	int attach;
79 {
80 	bus_addr_t maddr;
81 	bus_addr_t msize;
82 	int rv = 0, irq;
83 
84 	if (ia->ia_nio < 1)
85 		return (0);
86 	if (ia->ia_niomem < 1)
87 		return (0);
88 	if (ia->ia_nirq < 1)
89 		return (0);
90 
91 	if (ISA_DIRECT_CONFIG(ia))
92 		return (0);
93 
94 	/*
95 	 * Disallow wildcarded i/o addresses.
96 	 */
97 	if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
98 		return 0;
99 
100 	/*
101 	 * Make sure this is a valid LEMAC address.
102 	 */
103 	if (ia->ia_io[0].ir_addr & (LEMAC_IOSIZE - 1))
104 		return 0;
105 
106 	sc->sc_iot = ia->ia_iot;
107 
108 	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr, LEMAC_IOSIZE, 0,
109 	    &sc->sc_ioh)) {
110 		if (attach)
111 			printf(": can't map i/o space\n");
112 		return 0;
113 	}
114 
115 	/*
116 	 * Read the Ethernet address from the EEPROM.
117 	 * It must start with one of the DEC OUIs and pass the
118 	 * DEC ethernet checksum test.
119 	 */
120 	if (lemac_port_check(sc->sc_iot, sc->sc_ioh) == 0)
121 		goto outio;
122 
123 	/*
124 	 * Get information about memory space and attempt to map it.
125 	 */
126 	lemac_info_get(sc->sc_iot, sc->sc_ioh, &maddr, &msize, &irq);
127 
128 	if (ia->ia_iomem[0].ir_addr != ISACF_IOMEM_DEFAULT &&
129 	    ia->ia_iomem[0].ir_addr != maddr)
130 		goto outio;
131 
132 	sc->sc_memt = ia->ia_memt;
133 	if (bus_space_map(ia->ia_memt, maddr, msize, 0, &sc->sc_memh)) {
134 		if (attach)
135 			printf(": can't map mem space\n");
136 		goto outio;
137 	}
138 
139 
140 	/*
141 	 * Double-check IRQ configuration.
142 	 */
143 	if (ia->ia_irq[0].ir_irq != ISACF_IRQ_DEFAULT &&
144 	    ia->ia_irq[0].ir_irq != irq)
145 		printf("%s: overriding IRQ %d to %d\n", sc->sc_dv.dv_xname,
146 		       ia->ia_irq[0].ir_irq, irq);
147 
148 	if (attach) {
149 		sc->sc_ats = shutdownhook_establish(lemac_shutdown, sc);
150 		if (sc->sc_ats == NULL)
151 			printf("\n%s: warning: can't establish shutdown hook\n",
152 			    sc->sc_dv.dv_xname);
153 
154 		lemac_ifattach(sc);
155 
156 		sc->sc_ih = isa_intr_establish(ia->ia_ic, irq, IST_EDGE,
157 		    IPL_NET, lemac_intr, sc);
158 	}
159 
160 	/*
161 	 * I guess we've found one.
162 	 */
163 	rv = 1;
164 
165 	ia->ia_nio = 1;
166 	ia->ia_io[0].ir_size = LEMAC_IOSIZE;
167 
168 	ia->ia_niomem = 1;
169 	ia->ia_iomem[0].ir_addr = maddr;
170 	ia->ia_iomem[0].ir_size = msize;
171 
172 	ia->ia_nirq = 1;
173 	ia->ia_irq[0].ir_irq = irq;
174 
175 	ia->ia_ndrq = 0;
176 
177 	if (rv == 0 || !attach)
178 		bus_space_unmap(sc->sc_memt, sc->sc_memh, msize);
179 outio:
180 	if (rv == 0 || !attach)
181 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, LEMAC_IOSIZE);
182 	return rv;
183 }
184 
185 static int
186 lemac_isa_probe(parent, match, aux)
187 	struct device *parent;
188 	struct cfdata *match;
189 	void *aux;
190 {
191 	struct isa_attach_args *ia = aux;
192 	struct cfdata *cf = match;
193 	lemac_softc_t sc;
194 	(void)sprintf(sc.sc_dv.dv_xname, "%s%d", lc_cd.cd_name, cf->cf_unit);
195 
196 	return lemac_isa_find(&sc, ia, 0);
197 }
198 
199 static void
200 lemac_isa_attach(parent, self, aux)
201 	struct device *parent;
202 	struct device *self;
203 	void *aux;
204 {
205 	lemac_softc_t *sc = (void *)self;
206 	struct isa_attach_args *ia = aux;
207 
208 	(void) lemac_isa_find(sc, ia, 1);
209 }
210