if_sm_pcmcia.c revision 1.46 1 1.46 christos /* $NetBSD: if_sm_pcmcia.c,v 1.46 2005/12/11 12:23:23 christos Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.40 mycroft * Copyright (c) 1997, 1998, 2000, 2004 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.2 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.40 mycroft * NASA Ames Research Center, and by Charles M. Hannum.
10 1.2 thorpej *
11 1.2 thorpej * Redistribution and use in source and binary forms, with or without
12 1.2 thorpej * modification, are permitted provided that the following conditions
13 1.2 thorpej * are met:
14 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.2 thorpej * notice, this list of conditions and the following disclaimer.
16 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.2 thorpej * documentation and/or other materials provided with the distribution.
19 1.2 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.2 thorpej * must display the following acknowledgement:
21 1.2 thorpej * This product includes software developed by the NetBSD
22 1.2 thorpej * Foundation, Inc. and its contributors.
23 1.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.2 thorpej * contributors may be used to endorse or promote products derived
25 1.2 thorpej * from this software without specific prior written permission.
26 1.2 thorpej *
27 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.2 thorpej */
39 1.26 lukem
40 1.26 lukem #include <sys/cdefs.h>
41 1.46 christos __KERNEL_RCSID(0, "$NetBSD: if_sm_pcmcia.c,v 1.46 2005/12/11 12:23:23 christos Exp $");
42 1.2 thorpej
43 1.2 thorpej #include <sys/param.h>
44 1.2 thorpej #include <sys/systm.h>
45 1.2 thorpej #include <sys/mbuf.h>
46 1.2 thorpej #include <sys/socket.h>
47 1.2 thorpej #include <sys/ioctl.h>
48 1.2 thorpej #include <sys/errno.h>
49 1.2 thorpej #include <sys/syslog.h>
50 1.2 thorpej #include <sys/select.h>
51 1.2 thorpej #include <sys/device.h>
52 1.2 thorpej
53 1.2 thorpej #include <net/if.h>
54 1.2 thorpej #include <net/if_dl.h>
55 1.2 thorpej #include <net/if_ether.h>
56 1.2 thorpej #include <net/if_media.h>
57 1.2 thorpej
58 1.2 thorpej #include <machine/intr.h>
59 1.2 thorpej #include <machine/bus.h>
60 1.23 briggs
61 1.23 briggs #include <dev/mii/mii.h>
62 1.23 briggs #include <dev/mii/miivar.h>
63 1.2 thorpej
64 1.2 thorpej #include <dev/ic/smc91cxxreg.h>
65 1.2 thorpej #include <dev/ic/smc91cxxvar.h>
66 1.2 thorpej
67 1.2 thorpej #include <dev/pcmcia/pcmciareg.h>
68 1.2 thorpej #include <dev/pcmcia/pcmciavar.h>
69 1.6 christos #include <dev/pcmcia/pcmciadevs.h>
70 1.2 thorpej
71 1.45 perry int sm_pcmcia_match(struct device *, struct cfdata *, void *);
72 1.45 perry int sm_pcmcia_validate_config(struct pcmcia_config_entry *);
73 1.45 perry void sm_pcmcia_attach(struct device *, struct device *, void *);
74 1.45 perry int sm_pcmcia_detach(struct device *, int);
75 1.2 thorpej
76 1.2 thorpej struct sm_pcmcia_softc {
77 1.2 thorpej struct smc91cxx_softc sc_smc; /* real "smc" softc */
78 1.2 thorpej
79 1.43 mycroft struct pcmcia_function *sc_pf; /* our PCMCIA function */
80 1.2 thorpej void *sc_ih; /* interrupt cookie */
81 1.39 mycroft
82 1.39 mycroft int sc_state;
83 1.39 mycroft #define SM_PCMCIA_ATTACHED 3
84 1.2 thorpej };
85 1.2 thorpej
86 1.29 thorpej CFATTACH_DECL(sm_pcmcia, sizeof(struct sm_pcmcia_softc),
87 1.30 thorpej sm_pcmcia_match, sm_pcmcia_attach, sm_pcmcia_detach, smc91cxx_activate);
88 1.2 thorpej
89 1.45 perry int sm_pcmcia_enable(struct smc91cxx_softc *);
90 1.45 perry void sm_pcmcia_disable(struct smc91cxx_softc *);
91 1.2 thorpej
92 1.45 perry int sm_pcmcia_ascii_enaddr(const char *, u_int8_t *);
93 1.7 thorpej
94 1.20 cgd const struct pcmcia_product sm_pcmcia_products[] = {
95 1.43 mycroft { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_EM1144,
96 1.43 mycroft PCMCIA_CIS_INVALID },
97 1.31 mycroft
98 1.43 mycroft { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,
99 1.43 mycroft PCMCIA_CIS_INVALID },
100 1.16 abs
101 1.43 mycroft { PCMCIA_VENDOR_NEWMEDIA, PCMCIA_PRODUCT_NEWMEDIA_BASICS,
102 1.43 mycroft PCMCIA_CIS_INVALID },
103 1.10 thorpej
104 1.11 thorpej #if 0
105 1.43 mycroft { PCMCIA_VENDOR_SMC, PCMCIA_PRODUCT_SMC_8020BT,
106 1.43 mycroft PCMCIA_CIS_INVALID },
107 1.11 thorpej #endif
108 1.43 mycroft { PCMCIA_VENDOR_PSION, PCMCIA_PRODUCT_PSION_GOLDCARD,
109 1.43 mycroft PCMCIA_CIS_INVALID },
110 1.7 thorpej };
111 1.43 mycroft const size_t sm_pcmcia_nproducts =
112 1.43 mycroft sizeof(sm_pcmcia_products) / sizeof(sm_pcmcia_products[0]);
113 1.7 thorpej
114 1.2 thorpej int
115 1.2 thorpej sm_pcmcia_match(parent, match, aux)
116 1.2 thorpej struct device *parent;
117 1.2 thorpej struct cfdata *match;
118 1.2 thorpej void *aux;
119 1.2 thorpej {
120 1.2 thorpej struct pcmcia_attach_args *pa = aux;
121 1.2 thorpej
122 1.43 mycroft /* This is to differentiate the serial function of some cards. */
123 1.31 mycroft if (pa->pf->function != PCMCIA_FUNCTION_NETWORK)
124 1.31 mycroft return (0);
125 1.31 mycroft
126 1.43 mycroft if (pcmcia_product_lookup(pa, sm_pcmcia_products, sm_pcmcia_nproducts,
127 1.43 mycroft sizeof(sm_pcmcia_products[0]), NULL))
128 1.7 thorpej return (1);
129 1.2 thorpej return (0);
130 1.2 thorpej }
131 1.2 thorpej
132 1.39 mycroft int
133 1.39 mycroft sm_pcmcia_validate_config(cfe)
134 1.39 mycroft struct pcmcia_config_entry *cfe;
135 1.39 mycroft {
136 1.39 mycroft if (cfe->iftype != PCMCIA_IFTYPE_IO ||
137 1.39 mycroft cfe->num_memspace != 0 ||
138 1.39 mycroft cfe->num_iospace != 1 ||
139 1.39 mycroft cfe->iospace[0].length < SMC_IOSIZE)
140 1.39 mycroft return (EINVAL);
141 1.39 mycroft return (0);
142 1.39 mycroft }
143 1.39 mycroft
144 1.2 thorpej void
145 1.2 thorpej sm_pcmcia_attach(parent, self, aux)
146 1.2 thorpej struct device *parent, *self;
147 1.2 thorpej void *aux;
148 1.2 thorpej {
149 1.2 thorpej struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
150 1.2 thorpej struct smc91cxx_softc *sc = &psc->sc_smc;
151 1.2 thorpej struct pcmcia_attach_args *pa = aux;
152 1.2 thorpej struct pcmcia_config_entry *cfe;
153 1.32 mycroft u_int8_t enaddr[ETHER_ADDR_LEN];
154 1.39 mycroft int error;
155 1.2 thorpej
156 1.2 thorpej psc->sc_pf = pa->pf;
157 1.35 mycroft
158 1.39 mycroft error = pcmcia_function_configure(pa->pf, sm_pcmcia_validate_config);
159 1.39 mycroft if (error) {
160 1.39 mycroft aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
161 1.39 mycroft error);
162 1.39 mycroft return;
163 1.2 thorpej }
164 1.2 thorpej
165 1.39 mycroft cfe = pa->pf->cfe;
166 1.39 mycroft sc->sc_bst = cfe->iospace[0].handle.iot;
167 1.39 mycroft sc->sc_bsh = cfe->iospace[0].handle.ioh;
168 1.39 mycroft
169 1.39 mycroft error = sm_pcmcia_enable(sc);
170 1.41 mycroft if (error)
171 1.39 mycroft goto fail;
172 1.38 mycroft
173 1.38 mycroft sc->sc_enable = sm_pcmcia_enable;
174 1.38 mycroft sc->sc_disable = sm_pcmcia_disable;
175 1.38 mycroft
176 1.10 thorpej /*
177 1.10 thorpej * First try to get the Ethernet address from FUNCE/LANNID tuple.
178 1.32 mycroft * If that fails, try one of the CIS info strings.
179 1.10 thorpej */
180 1.32 mycroft if (pa->pf->pf_funce_lan_nidlen == ETHER_ADDR_LEN) {
181 1.32 mycroft memcpy(enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
182 1.32 mycroft } else {
183 1.32 mycroft if (!sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[3], enaddr) &&
184 1.32 mycroft !sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[2], enaddr))
185 1.33 mycroft aprint_error("%s: unable to get Ethernet address\n",
186 1.33 mycroft self->dv_xname);
187 1.10 thorpej }
188 1.10 thorpej
189 1.7 thorpej /* Perform generic intialization. */
190 1.7 thorpej smc91cxx_attach(sc, enaddr);
191 1.7 thorpej
192 1.39 mycroft psc->sc_state = SM_PCMCIA_ATTACHED;
193 1.38 mycroft sm_pcmcia_disable(sc);
194 1.21 enami return;
195 1.21 enami
196 1.39 mycroft fail:
197 1.39 mycroft pcmcia_function_unconfigure(pa->pf);
198 1.13 thorpej }
199 1.13 thorpej
200 1.13 thorpej int
201 1.13 thorpej sm_pcmcia_detach(self, flags)
202 1.13 thorpej struct device *self;
203 1.13 thorpej int flags;
204 1.13 thorpej {
205 1.14 thorpej struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
206 1.39 mycroft int error;
207 1.14 thorpej
208 1.39 mycroft if (psc->sc_state != SM_PCMCIA_ATTACHED)
209 1.21 enami return (0);
210 1.14 thorpej
211 1.39 mycroft error = smc91cxx_detach((struct device *)&psc->sc_smc, flags);
212 1.39 mycroft if (error)
213 1.39 mycroft return (error);
214 1.21 enami
215 1.39 mycroft pcmcia_function_unconfigure(psc->sc_pf);
216 1.14 thorpej
217 1.21 enami return (0);
218 1.7 thorpej }
219 1.7 thorpej
220 1.7 thorpej int
221 1.10 thorpej sm_pcmcia_ascii_enaddr(cisstr, myla)
222 1.10 thorpej const char *cisstr;
223 1.7 thorpej u_int8_t *myla;
224 1.7 thorpej {
225 1.18 thorpej u_int8_t digit;
226 1.18 thorpej int i;
227 1.7 thorpej
228 1.31 mycroft /* No CIS string. */
229 1.31 mycroft if (cisstr == 0)
230 1.31 mycroft return (0);
231 1.31 mycroft
232 1.18 thorpej memset(myla, 0, ETHER_ADDR_LEN);
233 1.18 thorpej
234 1.18 thorpej for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
235 1.18 thorpej if (cisstr[i] >= '0' && cisstr[i] <= '9')
236 1.18 thorpej digit |= cisstr[i] - '0';
237 1.18 thorpej else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
238 1.18 thorpej digit |= (cisstr[i] - 'a') + 10;
239 1.18 thorpej else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
240 1.18 thorpej digit |= (cisstr[i] - 'A') + 10;
241 1.18 thorpej else {
242 1.18 thorpej /* Bogus digit!! */
243 1.18 thorpej return (0);
244 1.2 thorpej }
245 1.18 thorpej
246 1.18 thorpej /* Compensate for ordering of digits. */
247 1.18 thorpej if (i & 1) {
248 1.18 thorpej myla[i >> 1] = digit;
249 1.18 thorpej digit = 0;
250 1.18 thorpej } else
251 1.18 thorpej digit <<= 4;
252 1.2 thorpej }
253 1.2 thorpej
254 1.7 thorpej return (1);
255 1.7 thorpej }
256 1.7 thorpej
257 1.7 thorpej int
258 1.2 thorpej sm_pcmcia_enable(sc)
259 1.2 thorpej struct smc91cxx_softc *sc;
260 1.2 thorpej {
261 1.2 thorpej struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
262 1.38 mycroft int error;
263 1.19 itojun
264 1.2 thorpej /* Establish the interrupt handler. */
265 1.2 thorpej psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
266 1.2 thorpej sc);
267 1.41 mycroft if (!psc->sc_ih)
268 1.41 mycroft return (EIO);
269 1.37 mycroft
270 1.38 mycroft error = pcmcia_function_enable(psc->sc_pf);
271 1.42 mycroft if (error) {
272 1.37 mycroft pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
273 1.42 mycroft psc->sc_ih = 0;
274 1.42 mycroft }
275 1.37 mycroft
276 1.38 mycroft return (error);
277 1.2 thorpej }
278 1.2 thorpej
279 1.2 thorpej void
280 1.2 thorpej sm_pcmcia_disable(sc)
281 1.2 thorpej struct smc91cxx_softc *sc;
282 1.2 thorpej {
283 1.2 thorpej struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
284 1.2 thorpej
285 1.37 mycroft pcmcia_function_disable(psc->sc_pf);
286 1.25 pooka pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
287 1.39 mycroft psc->sc_ih = 0;
288 1.2 thorpej }
289