if_sm_pcmcia.c revision 1.38 1 /* $NetBSD: if_sm_pcmcia.c,v 1.38 2004/08/09 20:30:08 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.38 2004/08/09 20:30:08 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 psc->sc_pf = pa->pf;
147
148 SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
149 if (cfe->num_iospace != 1)
150 continue;
151
152 /* Allocate I/O space for the card. */
153 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
154 cfe->iospace[0].length, cfe->iospace[0].length,
155 &psc->sc_pcioh) == 0)
156 break;
157 }
158 if (!cfe) {
159 aprint_error("%s: can't allocate i/o space\n", self->dv_xname);
160 goto ioalloc_failed;
161 }
162
163 sc->sc_bst = psc->sc_pcioh.iot;
164 sc->sc_bsh = psc->sc_pcioh.ioh;
165
166 /* Enable the card. */
167 pcmcia_function_init(pa->pf, cfe);
168
169 if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, &psc->sc_pcioh,
170 &psc->sc_io_window)) {
171 aprint_error("%s: can't map i/o space\n", self->dv_xname);
172 goto iomap_failed;
173 }
174
175 if (sm_pcmcia_enable(sc)) {
176 aprint_error("%s: function enable failed\n", self->dv_xname);
177 goto enable_failed;
178 }
179
180 sc->sc_enable = sm_pcmcia_enable;
181 sc->sc_disable = sm_pcmcia_disable;
182
183 pp = pcmcia_product_lookup(pa, sm_pcmcia_products,
184 sizeof sm_pcmcia_products[0], NULL);
185 if (pp == NULL)
186 panic("sm_pcmcia_attach: impossible");
187
188 /*
189 * First try to get the Ethernet address from FUNCE/LANNID tuple.
190 * If that fails, try one of the CIS info strings.
191 */
192 if (pa->pf->pf_funce_lan_nidlen == ETHER_ADDR_LEN) {
193 memcpy(enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
194 } else {
195 if (!sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[3], enaddr) &&
196 !sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[2], enaddr))
197 aprint_error("%s: unable to get Ethernet address\n",
198 self->dv_xname);
199 }
200
201 /* Perform generic intialization. */
202 smc91cxx_attach(sc, enaddr);
203
204 sm_pcmcia_disable(sc);
205 return;
206
207 enable_failed:
208 pcmcia_io_unmap(pa->pf, psc->sc_io_window);
209 iomap_failed:
210 pcmcia_io_free(pa->pf, &psc->sc_pcioh);
211 ioalloc_failed:
212 psc->sc_io_window = -1;
213 }
214
215 int
216 sm_pcmcia_detach(self, flags)
217 struct device *self;
218 int flags;
219 {
220 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
221 int rv;
222
223 if (psc->sc_io_window == -1)
224 /* Nothing to detach. */
225 return (0);
226
227 rv = smc91cxx_detach((struct device *)&psc->sc_smc, flags);
228 if (rv != 0)
229 return (rv);
230
231 /* Unmap our i/o window. */
232 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
233
234 /* Free our i/o space. */
235 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
236 return (0);
237 }
238
239 int
240 sm_pcmcia_ascii_enaddr(cisstr, myla)
241 const char *cisstr;
242 u_int8_t *myla;
243 {
244 u_int8_t digit;
245 int i;
246
247 /* No CIS string. */
248 if (cisstr == 0)
249 return (0);
250
251 memset(myla, 0, ETHER_ADDR_LEN);
252
253 for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
254 if (cisstr[i] >= '0' && cisstr[i] <= '9')
255 digit |= cisstr[i] - '0';
256 else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
257 digit |= (cisstr[i] - 'a') + 10;
258 else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
259 digit |= (cisstr[i] - 'A') + 10;
260 else {
261 /* Bogus digit!! */
262 return (0);
263 }
264
265 /* Compensate for ordering of digits. */
266 if (i & 1) {
267 myla[i >> 1] = digit;
268 digit = 0;
269 } else
270 digit <<= 4;
271 }
272
273 return (1);
274 }
275
276 int
277 sm_pcmcia_enable(sc)
278 struct smc91cxx_softc *sc;
279 {
280 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
281 int error;
282
283 /* Establish the interrupt handler. */
284 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
285 sc);
286 if (psc->sc_ih == NULL) {
287 printf("%s: couldn't establish interrupt handler\n",
288 sc->sc_dev.dv_xname);
289 return (1);
290 }
291
292 error = pcmcia_function_enable(psc->sc_pf);
293 if (error)
294 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
295
296 return (error);
297 }
298
299 void
300 sm_pcmcia_disable(sc)
301 struct smc91cxx_softc *sc;
302 {
303 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
304
305 pcmcia_function_disable(psc->sc_pf);
306 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
307 }
308