if_sm_pcmcia.c revision 1.34 1 /* $NetBSD: if_sm_pcmcia.c,v 1.34 2004/08/08 15:12:20 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * 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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: if_sm_pcmcia.c,v 1.34 2004/08/08 15:12:20 mycroft Exp $");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/select.h>
51 #include <sys/device.h>
52
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_ether.h>
56 #include <net/if_media.h>
57
58 #include <machine/intr.h>
59 #include <machine/bus.h>
60
61 #include <dev/mii/mii.h>
62 #include <dev/mii/miivar.h>
63
64 #include <dev/ic/smc91cxxreg.h>
65 #include <dev/ic/smc91cxxvar.h>
66
67 #include <dev/pcmcia/pcmciareg.h>
68 #include <dev/pcmcia/pcmciavar.h>
69 #include <dev/pcmcia/pcmciadevs.h>
70
71 int sm_pcmcia_match __P((struct device *, struct cfdata *, void *));
72 void sm_pcmcia_attach __P((struct device *, struct device *, void *));
73 int sm_pcmcia_detach __P((struct device *, int));
74
75 struct sm_pcmcia_softc {
76 struct smc91cxx_softc sc_smc; /* real "smc" softc */
77
78 /* PCMCIA-specific goo. */
79 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
80 int sc_io_window; /* our i/o window */
81 void *sc_ih; /* interrupt cookie */
82 struct pcmcia_function *sc_pf; /* our PCMCIA function */
83 };
84
85 CFATTACH_DECL(sm_pcmcia, sizeof(struct sm_pcmcia_softc),
86 sm_pcmcia_match, sm_pcmcia_attach, sm_pcmcia_detach, smc91cxx_activate);
87
88 int sm_pcmcia_enable __P((struct smc91cxx_softc *));
89 void sm_pcmcia_disable __P((struct smc91cxx_softc *));
90
91 int sm_pcmcia_ascii_enaddr __P((const char *, u_int8_t *));
92
93 const struct pcmcia_product sm_pcmcia_products[] = {
94 { "", PCMCIA_VENDOR_MEGAHERTZ2,
95 PCMCIA_PRODUCT_MEGAHERTZ2_EM1144, 0, },
96 { "", PCMCIA_VENDOR_MEGAHERTZ2,
97 PCMCIA_PRODUCT_MEGAHERTZ2_EM1144, 1, },
98
99 { "", PCMCIA_VENDOR_MEGAHERTZ2,
100 PCMCIA_PRODUCT_MEGAHERTZ2_XJACK, 0, },
101
102 { "", PCMCIA_VENDOR_NEWMEDIA,
103 PCMCIA_PRODUCT_NEWMEDIA_BASICS, 0, },
104
105 #if 0
106 { "", PCMCIA_VENDOR_SMC,
107 PCMCIA_PRODUCT_SMC_8020BT, 0, },
108 #endif
109 { "", PCMCIA_VENDOR_PSION,
110 PCMCIA_PRODUCT_PSION_GOLDCARD, 0, },
111
112 { NULL }
113 };
114
115 int
116 sm_pcmcia_match(parent, match, aux)
117 struct device *parent;
118 struct cfdata *match;
119 void *aux;
120 {
121 struct pcmcia_attach_args *pa = aux;
122
123 /* This is to differentiate the serial function of Megahertz cards. */
124 if (pa->pf->function != PCMCIA_FUNCTION_NETWORK)
125 return (0);
126
127 if (pcmcia_product_lookup(pa, sm_pcmcia_products,
128 sizeof sm_pcmcia_products[0], NULL) != NULL)
129 return (1);
130 return (0);
131 }
132
133 void
134 sm_pcmcia_attach(parent, self, aux)
135 struct device *parent, *self;
136 void *aux;
137 {
138 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
139 struct smc91cxx_softc *sc = &psc->sc_smc;
140 struct pcmcia_attach_args *pa = aux;
141 struct pcmcia_config_entry *cfe;
142 u_int8_t enaddr[ETHER_ADDR_LEN];
143 const struct pcmcia_product *pp;
144
145 aprint_normal("\n");
146
147 psc->sc_pf = pa->pf;
148 cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head);
149
150 /* Enable the card. */
151 pcmcia_function_init(pa->pf, cfe);
152 if (pcmcia_function_enable(pa->pf)) {
153 aprint_error("%s: function enable failed\n", self->dv_xname);
154 goto enable_failed;
155 }
156
157 /* XXX sanity check number of mem and i/o spaces */
158
159 /* Allocate and map i/o space for the card. */
160 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
161 cfe->iospace[0].length, &psc->sc_pcioh)) {
162 aprint_error("%s: can't allocate i/o space\n", self->dv_xname);
163 goto ioalloc_failed;
164 }
165
166 sc->sc_bst = psc->sc_pcioh.iot;
167 sc->sc_bsh = psc->sc_pcioh.ioh;
168
169 sc->sc_enable = sm_pcmcia_enable;
170 sc->sc_disable = sm_pcmcia_disable;
171
172 if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0, cfe->iospace[0].length,
173 &psc->sc_pcioh, &psc->sc_io_window)) {
174 aprint_error("%s: can't map i/o space\n", self->dv_xname);
175 goto iomap_failed;
176 }
177
178 pp = pcmcia_product_lookup(pa, sm_pcmcia_products,
179 sizeof sm_pcmcia_products[0], NULL);
180 if (pp == NULL)
181 panic("sm_pcmcia_attach: impossible");
182
183 /*
184 * First try to get the Ethernet address from FUNCE/LANNID tuple.
185 * If that fails, try one of the CIS info strings.
186 */
187 if (pa->pf->pf_funce_lan_nidlen == ETHER_ADDR_LEN) {
188 memcpy(enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
189 } else {
190 if (!sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[3], enaddr) &&
191 !sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[2], enaddr))
192 aprint_error("%s: unable to get Ethernet address\n",
193 self->dv_xname);
194 }
195
196 /* Perform generic intialization. */
197 smc91cxx_attach(sc, enaddr);
198
199 pcmcia_function_disable(pa->pf);
200 return;
201
202 iomap_failed:
203 /* Free our i/o space. */
204 pcmcia_io_free(pa->pf, &psc->sc_pcioh);
205
206 ioalloc_failed:
207 /* Disable the device */
208 pcmcia_function_disable(pa->pf);
209
210 enable_failed:
211 psc->sc_io_window = -1;
212 }
213
214 int
215 sm_pcmcia_detach(self, flags)
216 struct device *self;
217 int flags;
218 {
219 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
220 int rv;
221
222 if (psc->sc_io_window == -1)
223 /* Nothing to detach. */
224 return (0);
225
226 rv = smc91cxx_detach((struct device *)&psc->sc_smc, flags);
227 if (rv != 0)
228 return (rv);
229
230 /* Unmap our i/o window. */
231 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
232
233 /* Free our i/o space. */
234 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
235 return (0);
236 }
237
238 int
239 sm_pcmcia_ascii_enaddr(cisstr, myla)
240 const char *cisstr;
241 u_int8_t *myla;
242 {
243 u_int8_t digit;
244 int i;
245
246 /* No CIS string. */
247 if (cisstr == 0)
248 return (0);
249
250 memset(myla, 0, ETHER_ADDR_LEN);
251
252 for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
253 if (cisstr[i] >= '0' && cisstr[i] <= '9')
254 digit |= cisstr[i] - '0';
255 else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
256 digit |= (cisstr[i] - 'a') + 10;
257 else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
258 digit |= (cisstr[i] - 'A') + 10;
259 else {
260 /* Bogus digit!! */
261 return (0);
262 }
263
264 /* Compensate for ordering of digits. */
265 if (i & 1) {
266 myla[i >> 1] = digit;
267 digit = 0;
268 } else
269 digit <<= 4;
270 }
271
272 return (1);
273 }
274
275 int
276 sm_pcmcia_enable(sc)
277 struct smc91cxx_softc *sc;
278 {
279 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
280 int rv;
281
282 rv = pcmcia_function_enable(psc->sc_pf);
283 if (rv != 0)
284 return (rv);
285
286 /* Establish the interrupt handler. */
287 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
288 sc);
289 if (psc->sc_ih == NULL) {
290 printf("%s: couldn't establish interrupt handler\n",
291 sc->sc_dev.dv_xname);
292 return (1);
293 }
294 return (0);
295 }
296
297 void
298 sm_pcmcia_disable(sc)
299 struct smc91cxx_softc *sc;
300 {
301 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
302
303 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
304 pcmcia_function_disable(psc->sc_pf);
305 }
306