xref: /netbsd-src/sys/dev/isa/if_lc_isa.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: if_lc_isa.c,v 1.28 2007/10/19 12:00:18 ad 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.28 2007/10/19 12:00:18 ad 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 <sys/cpu.h>
55 #include <sys/bus.h>
56 #include <sys/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(lemac_softc_t *, struct isa_attach_args *, int);
66 static int lemac_isa_probe(struct device *, struct cfdata *, void *);
67 static void lemac_isa_attach(struct device *, struct device *, void *);
68 
69 CFATTACH_DECL(lc_isa, sizeof(lemac_softc_t),
70     lemac_isa_probe, lemac_isa_attach, NULL, NULL);
71 
72 static int
73 lemac_isa_find(sc, ia, attach)
74 	lemac_softc_t *sc;
75 	struct isa_attach_args *ia;
76 	int attach;
77 {
78 	bus_addr_t maddr;
79 	bus_addr_t msiz;
80 	int rv = 0, irq;
81 
82 	if (ia->ia_nio < 1)
83 		return (0);
84 	if (ia->ia_niomem < 1)
85 		return (0);
86 	if (ia->ia_nirq < 1)
87 		return (0);
88 
89 	if (ISA_DIRECT_CONFIG(ia))
90 		return (0);
91 
92 	/*
93 	 * Disallow wildcarded i/o addresses.
94 	 */
95 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
96 		return 0;
97 
98 	/*
99 	 * Make sure this is a valid LEMAC address.
100 	 */
101 	if (ia->ia_io[0].ir_addr & (LEMAC_IOSIZE - 1))
102 		return 0;
103 
104 	sc->sc_iot = ia->ia_iot;
105 
106 	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr, LEMAC_IOSIZE, 0,
107 	    &sc->sc_ioh)) {
108 		if (attach)
109 			printf(": can't map i/o space\n");
110 		return 0;
111 	}
112 
113 	/*
114 	 * Read the Ethernet address from the EEPROM.
115 	 * It must start with one of the DEC OUIs and pass the
116 	 * DEC ethernet checksum test.
117 	 */
118 	if (lemac_port_check(sc->sc_iot, sc->sc_ioh) == 0)
119 		goto outio;
120 
121 	/*
122 	 * Get information about memory space and attempt to map it.
123 	 */
124 	lemac_info_get(sc->sc_iot, sc->sc_ioh, &maddr, &msiz, &irq);
125 
126 	if (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM &&
127 	    ia->ia_iomem[0].ir_addr != maddr)
128 		goto outio;
129 
130 	if (attach) {
131 		if (msiz == 0) {
132 			printf(": memory configuration is invalid\n");
133 			goto outio;
134 		}
135 
136 		sc->sc_memt = ia->ia_memt;
137 		if (bus_space_map(ia->ia_memt, maddr, msiz, 0, &sc->sc_memh)) {
138 			printf(": can't map mem space\n");
139 			goto outio;
140 		}
141 	}
142 
143 	/*
144 	 * Double-check IRQ configuration.
145 	 */
146 	if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
147 	    ia->ia_irq[0].ir_irq != irq)
148 		printf("%s: overriding IRQ %d to %d\n", sc->sc_dv.dv_xname,
149 		       ia->ia_irq[0].ir_irq, irq);
150 
151 	if (attach) {
152 		sc->sc_ats = shutdownhook_establish(lemac_shutdown, sc);
153 		if (sc->sc_ats == NULL)
154 			printf("\n%s: warning: can't establish shutdown hook\n",
155 			    sc->sc_dv.dv_xname);
156 
157 		lemac_ifattach(sc);
158 
159 		sc->sc_ih = isa_intr_establish(ia->ia_ic, irq, IST_EDGE,
160 		    IPL_NET, lemac_intr, sc);
161 	}
162 
163 	/*
164 	 * I guess we've found one.
165 	 */
166 	rv = 1;
167 
168 	ia->ia_nio = 1;
169 	ia->ia_io[0].ir_size = LEMAC_IOSIZE;
170 
171 	ia->ia_niomem = 1;
172 	ia->ia_iomem[0].ir_addr = maddr;
173 	ia->ia_iomem[0].ir_size = msiz;
174 
175 	ia->ia_nirq = 1;
176 	ia->ia_irq[0].ir_irq = irq;
177 
178 	ia->ia_ndrq = 0;
179 
180 outio:
181 	if (rv == 0 || !attach)
182 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, LEMAC_IOSIZE);
183 	return rv;
184 }
185 
186 static int
187 lemac_isa_probe(struct device *parent, struct cfdata *match, void *aux)
188 {
189 	struct isa_attach_args *ia = aux;
190 	struct cfdata *cf = match;
191 	lemac_softc_t sc;
192 	snprintf(sc.sc_dv.dv_xname, sizeof(sc.sc_dv.dv_xname), "%s%d",
193 	    lc_cd.cd_name, cf->cf_unit);
194 
195 	return lemac_isa_find(&sc, ia, 0);
196 }
197 
198 static void
199 lemac_isa_attach(struct device *parent, struct device *self, void *aux)
200 {
201 	lemac_softc_t *sc = (void *)self;
202 	struct isa_attach_args *ia = aux;
203 
204 	(void) lemac_isa_find(sc, ia, 1);
205 }
206