if_sm_pcmcia.c revision 1.24 1 /* $NetBSD: if_sm_pcmcia.c,v 1.24 2001/07/01 16:35:37 thorpej 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/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/ioctl.h>
45 #include <sys/errno.h>
46 #include <sys/syslog.h>
47 #include <sys/select.h>
48 #include <sys/device.h>
49
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_ether.h>
53 #include <net/if_media.h>
54
55 #include <machine/intr.h>
56 #include <machine/bus.h>
57
58 #include <dev/mii/mii.h>
59 #include <dev/mii/miivar.h>
60
61 #include <dev/ic/smc91cxxreg.h>
62 #include <dev/ic/smc91cxxvar.h>
63
64 #include <dev/pcmcia/pcmciareg.h>
65 #include <dev/pcmcia/pcmciavar.h>
66 #include <dev/pcmcia/pcmciadevs.h>
67
68 int sm_pcmcia_match __P((struct device *, struct cfdata *, void *));
69 void sm_pcmcia_attach __P((struct device *, struct device *, void *));
70 int sm_pcmcia_detach __P((struct device *, int));
71
72 struct sm_pcmcia_softc {
73 struct smc91cxx_softc sc_smc; /* real "smc" softc */
74
75 /* PCMCIA-specific goo. */
76 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
77 int sc_io_window; /* our i/o window */
78 void *sc_ih; /* interrupt cookie */
79 struct pcmcia_function *sc_pf; /* our PCMCIA function */
80 };
81
82 struct cfattach sm_pcmcia_ca = {
83 sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach,
84 sm_pcmcia_detach, smc91cxx_activate
85 };
86
87 int sm_pcmcia_enable __P((struct smc91cxx_softc *));
88 void sm_pcmcia_disable __P((struct smc91cxx_softc *));
89
90 int sm_pcmcia_ascii_enaddr __P((const char *, u_int8_t *));
91 int sm_pcmcia_funce_enaddr __P((struct device *, u_int8_t *));
92
93 int sm_pcmcia_lannid_ciscallback __P((struct pcmcia_tuple *, void *));
94
95 const struct pcmcia_product sm_pcmcia_products[] = {
96 { PCMCIA_STR_MEGAHERTZ2_XJACK, PCMCIA_VENDOR_MEGAHERTZ2,
97 PCMCIA_PRODUCT_MEGAHERTZ2_XJACK, 0, },
98
99 { PCMCIA_STR_NEWMEDIA_BASICS, PCMCIA_VENDOR_NEWMEDIA,
100 PCMCIA_PRODUCT_NEWMEDIA_BASICS, 0, },
101
102 #if 0
103 { PCMCIA_STR_SMC_8020BT, PCMCIA_VENDOR_SMC,
104 PCMCIA_PRODUCT_SMC_8020BT, 0, },
105 #endif
106
107 { NULL }
108 };
109
110 int
111 sm_pcmcia_match(parent, match, aux)
112 struct device *parent;
113 struct cfdata *match;
114 void *aux;
115 {
116 struct pcmcia_attach_args *pa = aux;
117
118 if (pcmcia_product_lookup(pa, sm_pcmcia_products,
119 sizeof sm_pcmcia_products[0], NULL) != NULL)
120 return (1);
121 return (0);
122 }
123
124 void
125 sm_pcmcia_attach(parent, self, aux)
126 struct device *parent, *self;
127 void *aux;
128 {
129 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
130 struct smc91cxx_softc *sc = &psc->sc_smc;
131 struct pcmcia_attach_args *pa = aux;
132 struct pcmcia_config_entry *cfe;
133 u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL;
134 const struct pcmcia_product *pp;
135
136 psc->sc_pf = pa->pf;
137 cfe = pa->pf->cfe_head.sqh_first;
138
139 /* Enable the card. */
140 pcmcia_function_init(pa->pf, cfe);
141 if (pcmcia_function_enable(pa->pf)) {
142 printf(": function enable failed\n");
143 goto enable_failed;
144 }
145
146 /* XXX sanity check number of mem and i/o spaces */
147
148 /* Allocate and map i/o space for the card. */
149 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
150 cfe->iospace[0].length, &psc->sc_pcioh)) {
151 printf(": can't allocate i/o space\n");
152 goto ioalloc_failed;
153 }
154
155 sc->sc_bst = psc->sc_pcioh.iot;
156 sc->sc_bsh = psc->sc_pcioh.ioh;
157
158 sc->sc_enable = sm_pcmcia_enable;
159 sc->sc_disable = sm_pcmcia_disable;
160
161 if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
162 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
163 &psc->sc_pcioh, &psc->sc_io_window)) {
164 printf(": can't map i/o space\n");
165 goto iomap_failed;
166 }
167
168 pp = pcmcia_product_lookup(pa, sm_pcmcia_products,
169 sizeof sm_pcmcia_products[0], NULL);
170 if (pp == NULL)
171 panic("sm_pcmcia_attach: impossible");
172
173 printf(": %s\n", pp->pp_name);
174
175 /*
176 * First try to get the Ethernet address from FUNCE/LANNID tuple.
177 */
178 if (sm_pcmcia_funce_enaddr(parent, myla))
179 enaddr = myla;
180
181 /*
182 * If that failed, try one of the CIS info strings.
183 */
184 if (enaddr == NULL) {
185 char *cisstr = NULL;
186
187 switch (pa->manufacturer) {
188 case PCMCIA_VENDOR_MEGAHERTZ:
189 case PCMCIA_VENDOR_MEGAHERTZ2:
190 cisstr = pa->pf->sc->card.cis1_info[3];
191 break;
192
193 case PCMCIA_VENDOR_SMC:
194 cisstr = pa->pf->sc->card.cis1_info[2];
195 break;
196 }
197
198 if (cisstr != NULL && sm_pcmcia_ascii_enaddr(cisstr, myla))
199 enaddr = myla;
200 }
201
202 if (enaddr == NULL)
203 printf("%s: unable to get Ethernet address\n",
204 sc->sc_dev.dv_xname);
205
206 /* Perform generic intialization. */
207 smc91cxx_attach(sc, enaddr);
208
209 pcmcia_function_disable(pa->pf);
210 return;
211
212 iomap_failed:
213 /* Free our i/o space. */
214 pcmcia_io_free(pa->pf, &psc->sc_pcioh);
215
216 ioalloc_failed:
217 /* Disable the device */
218 pcmcia_function_disable(pa->pf);
219
220 enable_failed:
221 psc->sc_io_window = -1;
222 }
223
224 int
225 sm_pcmcia_detach(self, flags)
226 struct device *self;
227 int flags;
228 {
229 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
230 int rv;
231
232 if (psc->sc_io_window == -1)
233 /* Nothing to detach. */
234 return (0);
235
236 rv = smc91cxx_detach((struct device *)&psc->sc_smc, flags);
237 if (rv != 0)
238 return (rv);
239
240 /* Unmap our i/o window. */
241 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
242
243 /* Free our i/o space. */
244 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
245 return (0);
246 }
247
248 int
249 sm_pcmcia_ascii_enaddr(cisstr, myla)
250 const char *cisstr;
251 u_int8_t *myla;
252 {
253 u_int8_t digit;
254 int i;
255
256 memset(myla, 0, ETHER_ADDR_LEN);
257
258 for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
259 if (cisstr[i] >= '0' && cisstr[i] <= '9')
260 digit |= cisstr[i] - '0';
261 else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
262 digit |= (cisstr[i] - 'a') + 10;
263 else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
264 digit |= (cisstr[i] - 'A') + 10;
265 else {
266 /* Bogus digit!! */
267 return (0);
268 }
269
270 /* Compensate for ordering of digits. */
271 if (i & 1) {
272 myla[i >> 1] = digit;
273 digit = 0;
274 } else
275 digit <<= 4;
276 }
277
278 return (1);
279 }
280
281 int
282 sm_pcmcia_funce_enaddr(parent, myla)
283 struct device *parent;
284 u_int8_t *myla;
285 {
286
287 return (pcmcia_scan_cis(parent, sm_pcmcia_lannid_ciscallback, myla));
288 }
289
290 int
291 sm_pcmcia_lannid_ciscallback(tuple, arg)
292 struct pcmcia_tuple *tuple;
293 void *arg;
294 {
295 u_int8_t *myla = arg;
296 int i;
297
298 if (tuple->code == PCMCIA_CISTPL_FUNCE) {
299 /* subcode, length */
300 if (tuple->length < 2)
301 return (0);
302
303 if ((pcmcia_tuple_read_1(tuple, 0) !=
304 PCMCIA_TPLFE_TYPE_LAN_NID) ||
305 (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
306 return (0);
307
308 for (i = 0; i < ETHER_ADDR_LEN; i++)
309 myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
310 return (1);
311 }
312 return (0);
313 }
314
315 int
316 sm_pcmcia_enable(sc)
317 struct smc91cxx_softc *sc;
318 {
319 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
320 int rv;
321
322 /* Establish the interrupt handler. */
323 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
324 sc);
325 if (psc->sc_ih == NULL) {
326 printf("%s: couldn't establish interrupt handler\n",
327 sc->sc_dev.dv_xname);
328 return (1);
329 }
330
331 rv = pcmcia_function_enable(psc->sc_pf);
332 if (rv != 0)
333 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
334 return (rv);
335 }
336
337 void
338 sm_pcmcia_disable(sc)
339 struct smc91cxx_softc *sc;
340 {
341 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
342
343 pcmcia_function_disable(psc->sc_pf);
344 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
345 }
346