if_sm_pcmcia.c revision 1.32 1 /* $NetBSD: if_sm_pcmcia.c,v 1.32 2004/08/07 01:07:31 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.32 2004/08/07 01:07:31 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 psc->sc_pf = pa->pf;
146 cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head);
147
148 /* Enable the card. */
149 pcmcia_function_init(pa->pf, cfe);
150 if (pcmcia_function_enable(pa->pf)) {
151 printf(": function enable failed\n");
152 goto enable_failed;
153 }
154
155 /* XXX sanity check number of mem and i/o spaces */
156
157 /* Allocate and map i/o space for the card. */
158 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
159 cfe->iospace[0].length, &psc->sc_pcioh)) {
160 printf(": can't allocate i/o space\n");
161 goto ioalloc_failed;
162 }
163
164 sc->sc_bst = psc->sc_pcioh.iot;
165 sc->sc_bsh = psc->sc_pcioh.ioh;
166
167 sc->sc_enable = sm_pcmcia_enable;
168 sc->sc_disable = sm_pcmcia_disable;
169
170 if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
171 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
172 &psc->sc_pcioh, &psc->sc_io_window)) {
173 printf(": can't map i/o space\n");
174 goto iomap_failed;
175 }
176
177 printf("\n");
178
179 pp = pcmcia_product_lookup(pa, sm_pcmcia_products,
180 sizeof sm_pcmcia_products[0], NULL);
181 if (pp == NULL)
182 panic("sm_pcmcia_attach: impossible");
183
184 /*
185 * First try to get the Ethernet address from FUNCE/LANNID tuple.
186 * If that fails, try one of the CIS info strings.
187 */
188 if (pa->pf->pf_funce_lan_nidlen == ETHER_ADDR_LEN) {
189 memcpy(enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
190 } else {
191 if (!sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[3], enaddr) &&
192 !sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[2], enaddr))
193 printf("%s: unable to get Ethernet address\n",
194 sc->sc_dev.dv_xname);
195 }
196
197 /* Perform generic intialization. */
198 smc91cxx_attach(sc, enaddr);
199
200 pcmcia_function_disable(pa->pf);
201 return;
202
203 iomap_failed:
204 /* Free our i/o space. */
205 pcmcia_io_free(pa->pf, &psc->sc_pcioh);
206
207 ioalloc_failed:
208 /* Disable the device */
209 pcmcia_function_disable(pa->pf);
210
211 enable_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 rv;
282
283 rv = pcmcia_function_enable(psc->sc_pf);
284 if (rv != 0)
285 return (rv);
286
287 /* Establish the interrupt handler. */
288 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
289 sc);
290 if (psc->sc_ih == NULL) {
291 printf("%s: couldn't establish interrupt handler\n",
292 sc->sc_dev.dv_xname);
293 return (1);
294 }
295 return (0);
296 }
297
298 void
299 sm_pcmcia_disable(sc)
300 struct smc91cxx_softc *sc;
301 {
302 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
303
304 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
305 pcmcia_function_disable(psc->sc_pf);
306 }
307