mhzc.c revision 1.4 1 1.4 cgd /* $NetBSD: mhzc.c,v 1.4 2000/02/04 01:27:13 cgd Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.4 cgd * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 thorpej * NASA Ames Research Center.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.1 thorpej * must display the following acknowledgement:
21 1.1 thorpej * This product includes software developed by the NetBSD
22 1.1 thorpej * Foundation, Inc. and its contributors.
23 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 thorpej * contributors may be used to endorse or promote products derived
25 1.1 thorpej * from this software without specific prior written permission.
26 1.1 thorpej *
27 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.1 thorpej */
39 1.1 thorpej
40 1.1 thorpej /*
41 1.1 thorpej * Device driver for the Megaherz X-JACK Ethernet/Modem combo cards.
42 1.1 thorpej *
43 1.1 thorpej * Many thanks to Chuck Cranor for having the patience to sift through
44 1.1 thorpej * the Linux smc91c92_cs.c driver to find the magic details to get this
45 1.1 thorpej * working!
46 1.1 thorpej */
47 1.1 thorpej
48 1.1 thorpej #include "opt_inet.h"
49 1.1 thorpej #include "opt_ns.h"
50 1.1 thorpej #include "bpfilter.h"
51 1.1 thorpej
52 1.1 thorpej #include <sys/param.h>
53 1.1 thorpej #include <sys/systm.h>
54 1.1 thorpej #include <sys/mbuf.h>
55 1.1 thorpej #include <sys/socket.h>
56 1.1 thorpej #include <sys/ioctl.h>
57 1.1 thorpej #include <sys/errno.h>
58 1.1 thorpej #include <sys/syslog.h>
59 1.1 thorpej #include <sys/select.h>
60 1.1 thorpej #include <sys/tty.h>
61 1.1 thorpej #include <sys/device.h>
62 1.1 thorpej
63 1.1 thorpej #include <net/if.h>
64 1.1 thorpej #include <net/if_dl.h>
65 1.1 thorpej #include <net/if_ether.h>
66 1.1 thorpej #include <net/if_media.h>
67 1.1 thorpej
68 1.1 thorpej #ifdef INET
69 1.1 thorpej #include <netinet/in.h>
70 1.1 thorpej #include <netinet/in_systm.h>
71 1.1 thorpej #include <netinet/in_var.h>
72 1.1 thorpej #include <netinet/ip.h>
73 1.1 thorpej #include <netinet/if_inarp.h>
74 1.1 thorpej #endif
75 1.1 thorpej
76 1.1 thorpej #ifdef NS
77 1.1 thorpej #include <netns/ns.h>
78 1.1 thorpej #include <netns/ns_if.h>
79 1.1 thorpej #endif
80 1.1 thorpej
81 1.1 thorpej #if NBPFILTER > 0
82 1.1 thorpej #include <net/bpf.h>
83 1.1 thorpej #include <net/bpfdesc.h>
84 1.1 thorpej #endif
85 1.1 thorpej
86 1.1 thorpej #include <machine/intr.h>
87 1.1 thorpej #include <machine/bus.h>
88 1.1 thorpej
89 1.1 thorpej #include <dev/ic/comreg.h>
90 1.1 thorpej #include <dev/ic/comvar.h>
91 1.1 thorpej
92 1.1 thorpej #include <dev/ic/smc91cxxreg.h>
93 1.1 thorpej #include <dev/ic/smc91cxxvar.h>
94 1.1 thorpej
95 1.1 thorpej #include <dev/pcmcia/pcmciareg.h>
96 1.1 thorpej #include <dev/pcmcia/pcmciavar.h>
97 1.1 thorpej #include <dev/pcmcia/pcmciadevs.h>
98 1.1 thorpej
99 1.1 thorpej #include "mhzc.h"
100 1.1 thorpej
101 1.1 thorpej struct mhzc_softc {
102 1.1 thorpej struct device sc_dev; /* generic device glue */
103 1.1 thorpej
104 1.1 thorpej struct pcmcia_function *sc_pf; /* our PCMCIA function */
105 1.1 thorpej void *sc_ih; /* interrupt handle */
106 1.1 thorpej
107 1.1 thorpej const struct mhzc_product *sc_product;
108 1.1 thorpej
109 1.1 thorpej /*
110 1.1 thorpej * Data for the Modem portion.
111 1.1 thorpej */
112 1.1 thorpej struct device *sc_modem;
113 1.1 thorpej struct pcmcia_io_handle sc_modem_pcioh;
114 1.1 thorpej int sc_modem_io_window;
115 1.1 thorpej
116 1.1 thorpej /*
117 1.1 thorpej * Data for the Ethernet portion.
118 1.1 thorpej */
119 1.1 thorpej struct device *sc_ethernet;
120 1.1 thorpej struct pcmcia_io_handle sc_ethernet_pcioh;
121 1.1 thorpej int sc_ethernet_io_window;
122 1.1 thorpej
123 1.1 thorpej int sc_flags;
124 1.1 thorpej };
125 1.1 thorpej
126 1.1 thorpej /* sc_flags */
127 1.1 thorpej #define MHZC_MODEM_MAPPED 0x01
128 1.1 thorpej #define MHZC_ETHERNET_MAPPED 0x02
129 1.1 thorpej #define MHZC_MODEM_ENABLED 0x04
130 1.1 thorpej #define MHZC_ETHERNET_ENABLED 0x08
131 1.1 thorpej
132 1.1 thorpej int mhzc_match __P((struct device *, struct cfdata *, void *));
133 1.1 thorpej void mhzc_attach __P((struct device *, struct device *, void *));
134 1.1 thorpej int mhzc_detach __P((struct device *, int));
135 1.1 thorpej int mhzc_activate __P((struct device *, enum devact));
136 1.1 thorpej
137 1.1 thorpej struct cfattach mhzc_ca = {
138 1.1 thorpej sizeof(struct mhzc_softc), mhzc_match, mhzc_attach,
139 1.1 thorpej mhzc_detach, mhzc_activate
140 1.1 thorpej };
141 1.1 thorpej
142 1.1 thorpej int mhzc_em3336_enaddr __P((struct mhzc_softc *, u_int8_t *));
143 1.1 thorpej int mhzc_em3336_enable __P((struct mhzc_softc *));
144 1.1 thorpej
145 1.4 cgd const struct mhzc_product {
146 1.4 cgd struct pcmcia_product mp_product;
147 1.1 thorpej
148 1.1 thorpej /* Get the Ethernet address for this card. */
149 1.1 thorpej int (*mp_enaddr) __P((struct mhzc_softc *, u_int8_t *));
150 1.1 thorpej
151 1.1 thorpej /* Perform any special `enable' magic. */
152 1.1 thorpej int (*mp_enable) __P((struct mhzc_softc *));
153 1.1 thorpej } mhzc_products[] = {
154 1.4 cgd { { PCMCIA_STR_MEGAHERTZ_XJEM3336, PCMCIA_VENDOR_MEGAHERTZ,
155 1.4 cgd PCMCIA_PRODUCT_MEGAHERTZ_XJEM3336, 0 },
156 1.1 thorpej mhzc_em3336_enaddr, mhzc_em3336_enable },
157 1.1 thorpej
158 1.1 thorpej /*
159 1.1 thorpej * Eventually we could add support for other Ethernet/Modem
160 1.1 thorpej * combo cards, even if they're aren't Megahertz, because
161 1.1 thorpej * most of them work more or less the same way.
162 1.1 thorpej */
163 1.1 thorpej
164 1.4 cgd { { NULL } }
165 1.1 thorpej };
166 1.1 thorpej
167 1.1 thorpej int mhzc_print __P((void *, const char *));
168 1.1 thorpej
169 1.1 thorpej int mhzc_check_cfe __P((struct mhzc_softc *, struct pcmcia_config_entry *));
170 1.1 thorpej int mhzc_alloc_ethernet __P((struct mhzc_softc *));
171 1.1 thorpej
172 1.1 thorpej int mhzc_enable __P((struct mhzc_softc *, int));
173 1.1 thorpej void mhzc_disable __P((struct mhzc_softc *, int));
174 1.1 thorpej
175 1.1 thorpej int mhzc_intr __P((void *));
176 1.1 thorpej
177 1.1 thorpej int
178 1.1 thorpej mhzc_match(parent, match, aux)
179 1.1 thorpej struct device *parent;
180 1.1 thorpej struct cfdata *match;
181 1.1 thorpej void *aux;
182 1.1 thorpej {
183 1.1 thorpej struct pcmcia_attach_args *pa = aux;
184 1.1 thorpej
185 1.4 cgd if (pcmcia_product_lookup(pa,
186 1.4 cgd (const struct pcmcia_product *)mhzc_products,
187 1.4 cgd sizeof mhzc_products[0], NULL) != NULL)
188 1.1 thorpej return (10); /* beat `com' */
189 1.1 thorpej
190 1.1 thorpej return (0);
191 1.1 thorpej }
192 1.1 thorpej
193 1.1 thorpej void
194 1.1 thorpej mhzc_attach(parent, self, aux)
195 1.1 thorpej struct device *parent, *self;
196 1.1 thorpej void *aux;
197 1.1 thorpej {
198 1.1 thorpej struct mhzc_softc *sc = (void *)self;
199 1.1 thorpej struct pcmcia_attach_args *pa = aux;
200 1.1 thorpej struct pcmcia_config_entry *cfe;
201 1.1 thorpej
202 1.1 thorpej sc->sc_pf = pa->pf;
203 1.1 thorpej
204 1.4 cgd sc->sc_product = (const struct mhzc_product *)pcmcia_product_lookup(pa,
205 1.4 cgd (const struct pcmcia_product *)mhzc_products,
206 1.4 cgd sizeof mhzc_products[0], NULL);
207 1.1 thorpej if (sc->sc_product == NULL) {
208 1.1 thorpej printf("\n");
209 1.1 thorpej panic("mhzc_attach: impossible");
210 1.1 thorpej }
211 1.1 thorpej
212 1.4 cgd printf(": %s\n", sc->sc_product->mp_product.pp_name);
213 1.1 thorpej
214 1.1 thorpej /*
215 1.1 thorpej * The address decoders on these cards are wacky. The configuration
216 1.1 thorpej * entries are set up to look like serial ports, and have no
217 1.1 thorpej * information about the Ethernet portion. In order to talk to
218 1.1 thorpej * the Modem portion, the I/O address must have bit 0x80 set.
219 1.1 thorpej * In order to talk to the Ethernet portion, the I/O address must
220 1.1 thorpej * have the 0x80 bit clear.
221 1.1 thorpej *
222 1.1 thorpej * The standard configuration entries conveniently have 0x80 set
223 1.1 thorpej * in them, and have a length of 8 (a 16550's size, convenient!),
224 1.1 thorpej * so we use those to set up the Modem portion.
225 1.1 thorpej *
226 1.1 thorpej * Once we have the Modem's address established, we search for
227 1.1 thorpej * an address suitable for the Ethernet portion. We do this by
228 1.1 thorpej * rounding up to the next 16-byte aligned address where 0x80
229 1.1 thorpej * isn't set (the SMC Ethernet chip has a 16-byte address size)
230 1.1 thorpej * and attemping to allocate a 16-byte region until we succeed.
231 1.1 thorpej *
232 1.1 thorpej * Sure would have been nice if Megahertz had made the card a
233 1.1 thorpej * proper multi-function device.
234 1.1 thorpej */
235 1.1 thorpej for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
236 1.1 thorpej cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
237 1.1 thorpej if (mhzc_check_cfe(sc, cfe)) {
238 1.1 thorpej /* Found one! */
239 1.1 thorpej break;
240 1.1 thorpej }
241 1.1 thorpej }
242 1.1 thorpej if (cfe == NULL) {
243 1.1 thorpej printf("%s: unable to find suitable config table entry\n",
244 1.1 thorpej sc->sc_dev.dv_xname);
245 1.1 thorpej return;
246 1.1 thorpej }
247 1.1 thorpej
248 1.1 thorpej if (mhzc_alloc_ethernet(sc) == 0) {
249 1.1 thorpej printf("%s: unable to allocate space for Ethernet portion\n",
250 1.1 thorpej sc->sc_dev.dv_xname);
251 1.1 thorpej return;
252 1.1 thorpej }
253 1.1 thorpej
254 1.1 thorpej /* Enable the card. */
255 1.1 thorpej pcmcia_function_init(pa->pf, cfe);
256 1.1 thorpej if (pcmcia_function_enable(pa->pf)) {
257 1.1 thorpej printf(": function enable failed\n");
258 1.1 thorpej return;
259 1.1 thorpej }
260 1.1 thorpej
261 1.1 thorpej if (sc->sc_product->mp_enable != NULL)
262 1.1 thorpej (*sc->sc_product->mp_enable)(sc);
263 1.1 thorpej
264 1.1 thorpej sc->sc_modem = config_found(&sc->sc_dev, "com", mhzc_print);
265 1.1 thorpej sc->sc_ethernet = config_found(&sc->sc_dev, "sm", mhzc_print);
266 1.1 thorpej
267 1.1 thorpej pcmcia_function_disable(pa->pf);
268 1.1 thorpej }
269 1.1 thorpej
270 1.1 thorpej int
271 1.1 thorpej mhzc_check_cfe(sc, cfe)
272 1.1 thorpej struct mhzc_softc *sc;
273 1.1 thorpej struct pcmcia_config_entry *cfe;
274 1.1 thorpej {
275 1.1 thorpej
276 1.1 thorpej if (cfe->num_memspace != 0)
277 1.1 thorpej return (0);
278 1.1 thorpej
279 1.1 thorpej if (cfe->num_iospace != 1)
280 1.1 thorpej return (0);
281 1.1 thorpej
282 1.1 thorpej if (pcmcia_io_alloc(sc->sc_pf,
283 1.1 thorpej cfe->iospace[0].start,
284 1.1 thorpej cfe->iospace[0].length,
285 1.1 thorpej cfe->iospace[0].length,
286 1.1 thorpej &sc->sc_modem_pcioh) == 0) {
287 1.1 thorpej /* Found one for the modem! */
288 1.1 thorpej return (1);
289 1.1 thorpej }
290 1.1 thorpej
291 1.1 thorpej return (0);
292 1.1 thorpej }
293 1.1 thorpej
294 1.1 thorpej int
295 1.1 thorpej mhzc_alloc_ethernet(sc)
296 1.1 thorpej struct mhzc_softc *sc;
297 1.1 thorpej {
298 1.1 thorpej bus_addr_t addr, maxaddr;
299 1.1 thorpej
300 1.1 thorpej maxaddr = sc->sc_pf->sc->iobase + sc->sc_pf->sc->iosize;
301 1.3 enami addr = sc->sc_pf->sc->iobase;
302 1.1 thorpej
303 1.1 thorpej /*
304 1.1 thorpej * Now round it up so that it starts on a 16-byte boundary.
305 1.1 thorpej */
306 1.1 thorpej addr = roundup(addr, 0x10);
307 1.1 thorpej
308 1.1 thorpej for (; (addr + 0x10) < maxaddr; addr += 0x10) {
309 1.1 thorpej if (addr & 0x80)
310 1.1 thorpej continue;
311 1.1 thorpej if (pcmcia_io_alloc(sc->sc_pf, addr, 0x10, 0x10,
312 1.1 thorpej &sc->sc_ethernet_pcioh) == 0) {
313 1.1 thorpej /* Found one for the ethernet! */
314 1.1 thorpej return (1);
315 1.1 thorpej }
316 1.1 thorpej }
317 1.1 thorpej
318 1.1 thorpej /*
319 1.1 thorpej * Weren't able to allocate space for the Ethernet, so we
320 1.1 thorpej * free the Modem's I/O space.
321 1.1 thorpej */
322 1.1 thorpej pcmcia_io_free(sc->sc_pf, &sc->sc_modem_pcioh);
323 1.1 thorpej return (0);
324 1.1 thorpej }
325 1.1 thorpej
326 1.1 thorpej int
327 1.1 thorpej mhzc_print(aux, pnp)
328 1.1 thorpej void *aux;
329 1.1 thorpej const char *pnp;
330 1.1 thorpej {
331 1.1 thorpej const char *name = aux;
332 1.1 thorpej
333 1.1 thorpej if (pnp)
334 1.1 thorpej printf("%s at %s", name, pnp);
335 1.1 thorpej
336 1.1 thorpej return (UNCONF);
337 1.1 thorpej }
338 1.1 thorpej
339 1.1 thorpej int
340 1.1 thorpej mhzc_detach(self, flags)
341 1.1 thorpej struct device *self;
342 1.1 thorpej int flags;
343 1.1 thorpej {
344 1.1 thorpej struct mhzc_softc *sc = (void *)self;
345 1.1 thorpej int rv;
346 1.1 thorpej
347 1.1 thorpej if (sc->sc_ethernet != NULL) {
348 1.1 thorpej rv = config_detach(sc->sc_ethernet, flags);
349 1.1 thorpej if (rv != 0)
350 1.1 thorpej return (rv);
351 1.1 thorpej }
352 1.1 thorpej
353 1.1 thorpej if (sc->sc_modem != NULL) {
354 1.1 thorpej rv = config_detach(sc->sc_modem, flags);
355 1.1 thorpej if (rv != 0)
356 1.1 thorpej return (rv);
357 1.1 thorpej }
358 1.1 thorpej
359 1.1 thorpej /* Unmap our i/o windows. */
360 1.1 thorpej if (sc->sc_flags & MHZC_MODEM_MAPPED)
361 1.1 thorpej pcmcia_io_unmap(sc->sc_pf, sc->sc_modem_io_window);
362 1.1 thorpej if (sc->sc_flags & MHZC_ETHERNET_MAPPED)
363 1.1 thorpej pcmcia_io_unmap(sc->sc_pf, sc->sc_ethernet_io_window);
364 1.1 thorpej
365 1.1 thorpej /* Free our i/o spaces. */
366 1.1 thorpej pcmcia_io_free(sc->sc_pf, &sc->sc_modem_pcioh);
367 1.1 thorpej pcmcia_io_free(sc->sc_pf, &sc->sc_ethernet_pcioh);
368 1.1 thorpej
369 1.1 thorpej return (0);
370 1.1 thorpej }
371 1.1 thorpej
372 1.1 thorpej int
373 1.1 thorpej mhzc_activate(self, act)
374 1.1 thorpej struct device *self;
375 1.1 thorpej enum devact act;
376 1.1 thorpej {
377 1.1 thorpej struct mhzc_softc *sc = (void *)self;
378 1.1 thorpej int s, rv = 0;
379 1.1 thorpej
380 1.1 thorpej s = splhigh();
381 1.1 thorpej switch (act) {
382 1.1 thorpej case DVACT_ACTIVATE:
383 1.1 thorpej rv = EOPNOTSUPP;
384 1.1 thorpej break;
385 1.1 thorpej
386 1.1 thorpej case DVACT_DEACTIVATE:
387 1.1 thorpej if (sc->sc_ethernet != NULL) {
388 1.1 thorpej rv = config_deactivate(sc->sc_ethernet);
389 1.1 thorpej if (rv != 0)
390 1.1 thorpej goto out;
391 1.1 thorpej }
392 1.1 thorpej
393 1.1 thorpej if (sc->sc_modem != NULL) {
394 1.1 thorpej rv = config_deactivate(sc->sc_modem);
395 1.1 thorpej if (rv != 0)
396 1.1 thorpej goto out;
397 1.1 thorpej }
398 1.1 thorpej break;
399 1.1 thorpej }
400 1.1 thorpej out:
401 1.1 thorpej splx(s);
402 1.1 thorpej return (rv);
403 1.1 thorpej }
404 1.1 thorpej
405 1.1 thorpej int
406 1.1 thorpej mhzc_intr(arg)
407 1.1 thorpej void *arg;
408 1.1 thorpej {
409 1.1 thorpej struct mhzc_softc *sc = arg;
410 1.1 thorpej int rval = 0;
411 1.1 thorpej
412 1.1 thorpej #if NCOM_MHZC > 0
413 1.1 thorpej if (sc->sc_modem != NULL &&
414 1.1 thorpej (sc->sc_flags & MHZC_MODEM_ENABLED) != 0)
415 1.1 thorpej rval |= comintr(sc->sc_modem);
416 1.1 thorpej #endif
417 1.1 thorpej
418 1.1 thorpej #if NSM_MHZC > 0
419 1.1 thorpej if (sc->sc_ethernet != NULL &&
420 1.1 thorpej (sc->sc_flags & MHZC_ETHERNET_ENABLED) != 0)
421 1.1 thorpej rval |= smc91cxx_intr(sc->sc_ethernet);
422 1.1 thorpej #endif
423 1.1 thorpej
424 1.1 thorpej return (rval);
425 1.1 thorpej }
426 1.1 thorpej
427 1.1 thorpej int
428 1.1 thorpej mhzc_enable(sc, flag)
429 1.1 thorpej struct mhzc_softc *sc;
430 1.1 thorpej int flag;
431 1.1 thorpej {
432 1.1 thorpej
433 1.1 thorpej if (sc->sc_flags & flag) {
434 1.1 thorpej printf("%s: %s already enabled\n", sc->sc_dev.dv_xname,
435 1.1 thorpej (flag & MHZC_MODEM_ENABLED) ? "modem" : "ethernet");
436 1.1 thorpej panic("mhzc_enable");
437 1.1 thorpej }
438 1.1 thorpej
439 1.1 thorpej if ((sc->sc_flags & (MHZC_MODEM_ENABLED|MHZC_ETHERNET_ENABLED)) != 0) {
440 1.1 thorpej sc->sc_flags |= flag;
441 1.1 thorpej return (0);
442 1.1 thorpej }
443 1.1 thorpej
444 1.1 thorpej /*
445 1.1 thorpej * Establish our interrupt handler.
446 1.1 thorpej *
447 1.1 thorpej * XXX Note, we establish this at IPL_NET. This is suboptimal
448 1.1 thorpej * XXX the Modem portion, but is necessary to make the Ethernet
449 1.1 thorpej * XXX portion have the correct interrupt level semantics.
450 1.1 thorpej *
451 1.1 thorpej * XXX Eventually we should use the `enabled' bits in the
452 1.1 thorpej * XXX flags word to determine which level we should be at.
453 1.1 thorpej */
454 1.1 thorpej sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET,
455 1.1 thorpej mhzc_intr, sc);
456 1.1 thorpej if (sc->sc_ih == NULL) {
457 1.1 thorpej printf("%s: unable to establish interrupt\n",
458 1.1 thorpej sc->sc_dev.dv_xname);
459 1.1 thorpej return (1);
460 1.1 thorpej }
461 1.1 thorpej
462 1.1 thorpej if (pcmcia_function_enable(sc->sc_pf)) {
463 1.2 thorpej pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
464 1.2 thorpej return (1);
465 1.2 thorpej }
466 1.2 thorpej
467 1.2 thorpej /*
468 1.2 thorpej * Perform any special enable magic necessary.
469 1.2 thorpej */
470 1.2 thorpej if (sc->sc_product->mp_enable != NULL &&
471 1.2 thorpej (*sc->sc_product->mp_enable)(sc) != 0) {
472 1.2 thorpej pcmcia_function_disable(sc->sc_pf);
473 1.1 thorpej pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
474 1.1 thorpej return (1);
475 1.1 thorpej }
476 1.1 thorpej
477 1.1 thorpej sc->sc_flags |= flag;
478 1.1 thorpej return (0);
479 1.1 thorpej }
480 1.1 thorpej
481 1.1 thorpej void
482 1.1 thorpej mhzc_disable(sc, flag)
483 1.1 thorpej struct mhzc_softc *sc;
484 1.1 thorpej int flag;
485 1.1 thorpej {
486 1.1 thorpej
487 1.1 thorpej if ((sc->sc_flags & flag) == 0) {
488 1.1 thorpej printf("%s: %s already disabled\n", sc->sc_dev.dv_xname,
489 1.1 thorpej (flag & MHZC_MODEM_ENABLED) ? "modem" : "ethernet");
490 1.1 thorpej panic("mhzc_disable");
491 1.1 thorpej }
492 1.1 thorpej
493 1.1 thorpej sc->sc_flags &= ~flag;
494 1.1 thorpej if ((sc->sc_flags & (MHZC_MODEM_ENABLED|MHZC_ETHERNET_ENABLED)) != 0)
495 1.1 thorpej return;
496 1.1 thorpej
497 1.1 thorpej pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
498 1.1 thorpej pcmcia_function_disable(sc->sc_pf);
499 1.1 thorpej }
500 1.1 thorpej
501 1.1 thorpej /*****************************************************************************
502 1.1 thorpej * Megahertz EM3336 (and compatibles) support
503 1.1 thorpej *****************************************************************************/
504 1.1 thorpej
505 1.1 thorpej int mhzc_em3336_lannid_ciscallback __P((struct pcmcia_tuple *, void *));
506 1.1 thorpej int mhzc_em3336_ascii_enaddr __P((const char *cisstr, u_int8_t *));
507 1.1 thorpej
508 1.1 thorpej int
509 1.1 thorpej mhzc_em3336_enaddr(sc, myla)
510 1.1 thorpej struct mhzc_softc *sc;
511 1.1 thorpej u_int8_t *myla;
512 1.1 thorpej {
513 1.1 thorpej
514 1.1 thorpej /* Get the station address from CIS tuple 0x81. */
515 1.1 thorpej if (pcmcia_scan_cis(sc->sc_dev.dv_parent,
516 1.1 thorpej mhzc_em3336_lannid_ciscallback, myla) != 1) {
517 1.1 thorpej printf("%s: unable to get Ethernet address from CIS\n",
518 1.1 thorpej sc->sc_dev.dv_xname);
519 1.1 thorpej return (0);
520 1.1 thorpej }
521 1.1 thorpej
522 1.1 thorpej return (1);
523 1.1 thorpej }
524 1.1 thorpej
525 1.1 thorpej int
526 1.1 thorpej mhzc_em3336_enable(sc)
527 1.1 thorpej struct mhzc_softc *sc;
528 1.1 thorpej {
529 1.1 thorpej struct pcmcia_mem_handle memh;
530 1.1 thorpej bus_addr_t memoff;
531 1.1 thorpej int memwin, reg;
532 1.1 thorpej
533 1.1 thorpej /*
534 1.1 thorpej * Bring the chip to live by touching its registers in the correct
535 1.1 thorpej * way (as per my reference... the Linux smc91c92_cs.c driver by
536 1.1 thorpej * David A. Hinds).
537 1.1 thorpej */
538 1.1 thorpej
539 1.1 thorpej /* Map the ISRPOWEREG. */
540 1.1 thorpej if (pcmcia_mem_alloc(sc->sc_pf, 0x1000, &memh) != 0) {
541 1.1 thorpej printf("%s: unable to allocate memory space\n",
542 1.1 thorpej sc->sc_dev.dv_xname);
543 1.1 thorpej return (1);
544 1.1 thorpej }
545 1.1 thorpej
546 1.1 thorpej if (pcmcia_mem_map(sc->sc_pf, PCMCIA_MEM_ATTR, 0, 0x1000,
547 1.1 thorpej &memh, &memoff, &memwin)) {
548 1.1 thorpej printf("%s: unable to map memory space\n",
549 1.1 thorpej sc->sc_dev.dv_xname);
550 1.1 thorpej pcmcia_mem_free(sc->sc_pf, &memh);
551 1.1 thorpej return (1);
552 1.1 thorpej }
553 1.1 thorpej
554 1.1 thorpej /*
555 1.1 thorpej * The magic sequence:
556 1.1 thorpej *
557 1.1 thorpej * - read/write the CCR option register.
558 1.1 thorpej * - read the ISRPOWEREG 2 times.
559 1.1 thorpej * - read/write the CCR option register again.
560 1.1 thorpej */
561 1.1 thorpej
562 1.1 thorpej reg = pcmcia_ccr_read(sc->sc_pf, PCMCIA_CCR_OPTION);
563 1.1 thorpej pcmcia_ccr_write(sc->sc_pf, PCMCIA_CCR_OPTION, reg);
564 1.1 thorpej
565 1.1 thorpej reg = bus_space_read_1(memh.memt, memh.memh, 0x380);
566 1.1 thorpej delay(5);
567 1.1 thorpej reg = bus_space_read_1(memh.memt, memh.memh, 0x380);
568 1.1 thorpej
569 1.1 thorpej delay(200000);
570 1.1 thorpej
571 1.1 thorpej reg = pcmcia_ccr_read(sc->sc_pf, PCMCIA_CCR_OPTION);
572 1.1 thorpej delay(5);
573 1.1 thorpej pcmcia_ccr_write(sc->sc_pf, PCMCIA_CCR_OPTION, reg);
574 1.1 thorpej
575 1.1 thorpej pcmcia_mem_unmap(sc->sc_pf, memwin);
576 1.1 thorpej pcmcia_mem_free(sc->sc_pf, &memh);
577 1.1 thorpej
578 1.1 thorpej return (0);
579 1.1 thorpej }
580 1.1 thorpej
581 1.1 thorpej int
582 1.1 thorpej mhzc_em3336_lannid_ciscallback(tuple, arg)
583 1.1 thorpej struct pcmcia_tuple *tuple;
584 1.1 thorpej void *arg;
585 1.1 thorpej {
586 1.1 thorpej u_int8_t *myla = arg, addr_str[ETHER_ADDR_LEN * 2];
587 1.1 thorpej int i;
588 1.1 thorpej
589 1.1 thorpej if (tuple->code == 0x81) {
590 1.1 thorpej /*
591 1.1 thorpej * We have a string-encoded address. Length includes
592 1.1 thorpej * terminating 0xff.
593 1.1 thorpej */
594 1.1 thorpej if (tuple->length != (ETHER_ADDR_LEN * 2) + 1)
595 1.1 thorpej return (0);
596 1.1 thorpej
597 1.1 thorpej for (i = 0; i < tuple->length - 1; i++)
598 1.1 thorpej addr_str[i] = pcmcia_tuple_read_1(tuple, i);
599 1.1 thorpej
600 1.1 thorpej /*
601 1.1 thorpej * Decode the string into `myla'.
602 1.1 thorpej */
603 1.1 thorpej return (mhzc_em3336_ascii_enaddr(addr_str, myla));
604 1.1 thorpej }
605 1.1 thorpej return (0);
606 1.1 thorpej }
607 1.1 thorpej
608 1.1 thorpej /* XXX This should be shared w/ if_sm_pcmcia.c */
609 1.1 thorpej int
610 1.1 thorpej mhzc_em3336_ascii_enaddr(cisstr, myla)
611 1.1 thorpej const char *cisstr;
612 1.1 thorpej u_int8_t *myla;
613 1.1 thorpej {
614 1.1 thorpej u_int8_t digit;
615 1.1 thorpej int i;
616 1.1 thorpej
617 1.1 thorpej memset(myla, 0, ETHER_ADDR_LEN);
618 1.1 thorpej
619 1.1 thorpej for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
620 1.1 thorpej if (cisstr[i] >= '0' && cisstr[i] <= '9')
621 1.1 thorpej digit |= cisstr[i] - '0';
622 1.1 thorpej else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
623 1.1 thorpej digit |= (cisstr[i] - 'a') + 10;
624 1.1 thorpej else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
625 1.1 thorpej digit |= (cisstr[i] - 'A') + 10;
626 1.1 thorpej else {
627 1.1 thorpej /* Bogus digit!! */
628 1.1 thorpej return (0);
629 1.1 thorpej }
630 1.1 thorpej
631 1.1 thorpej /* Compensate for ordering of digits. */
632 1.1 thorpej if (i & 1) {
633 1.1 thorpej myla[i >> 1] = digit;
634 1.1 thorpej digit = 0;
635 1.1 thorpej } else
636 1.1 thorpej digit <<= 4;
637 1.1 thorpej }
638 1.1 thorpej
639 1.1 thorpej return (1);
640 1.1 thorpej }
641 1.1 thorpej
642 1.1 thorpej /****** Here begins the com attachment code. ******/
643 1.1 thorpej
644 1.1 thorpej #if NCOM_MHZC > 0
645 1.1 thorpej int com_mhzc_match __P((struct device *, struct cfdata *, void *));
646 1.1 thorpej void com_mhzc_attach __P((struct device *, struct device *, void *));
647 1.1 thorpej
648 1.1 thorpej /* No mhzc-specific goo in the softc; it's all in the parent. */
649 1.1 thorpej struct cfattach com_mhzc_ca = {
650 1.1 thorpej sizeof(struct com_softc), com_mhzc_match, com_mhzc_attach,
651 1.1 thorpej com_detach, com_activate
652 1.1 thorpej };
653 1.1 thorpej
654 1.1 thorpej int com_mhzc_enable __P((struct com_softc *));
655 1.1 thorpej void com_mhzc_disable __P((struct com_softc *));
656 1.1 thorpej
657 1.1 thorpej int
658 1.1 thorpej com_mhzc_match(parent, match, aux)
659 1.1 thorpej struct device *parent;
660 1.1 thorpej struct cfdata *match;
661 1.1 thorpej void *aux;
662 1.1 thorpej {
663 1.1 thorpej extern struct cfdriver com_cd;
664 1.1 thorpej const char *name = aux;
665 1.1 thorpej
666 1.1 thorpej /* Device is always present. */
667 1.1 thorpej if (strcmp(name, com_cd.cd_name) == 0)
668 1.1 thorpej return (1);
669 1.1 thorpej
670 1.1 thorpej return (0);
671 1.1 thorpej }
672 1.1 thorpej
673 1.1 thorpej void
674 1.1 thorpej com_mhzc_attach(parent, self, aux)
675 1.1 thorpej struct device *parent, *self;
676 1.1 thorpej void *aux;
677 1.1 thorpej {
678 1.1 thorpej struct com_softc *sc = (void *)self;
679 1.1 thorpej struct mhzc_softc *msc = (void *)parent;
680 1.1 thorpej
681 1.1 thorpej printf(":");
682 1.1 thorpej if (pcmcia_io_map(msc->sc_pf, PCMCIA_WIDTH_IO8, 0,
683 1.1 thorpej msc->sc_modem_pcioh.size, &msc->sc_modem_pcioh,
684 1.1 thorpej &msc->sc_modem_io_window)) {
685 1.1 thorpej printf("unable to map I/O space\n");
686 1.1 thorpej return;
687 1.1 thorpej }
688 1.1 thorpej
689 1.1 thorpej msc->sc_flags |= MHZC_MODEM_MAPPED;
690 1.1 thorpej
691 1.1 thorpej sc->sc_iot = msc->sc_modem_pcioh.iot;
692 1.1 thorpej sc->sc_ioh = msc->sc_modem_pcioh.ioh;
693 1.1 thorpej
694 1.1 thorpej sc->enabled = 1;
695 1.1 thorpej
696 1.1 thorpej sc->sc_iobase = -1;
697 1.1 thorpej sc->sc_frequency = COM_FREQ;
698 1.1 thorpej
699 1.1 thorpej sc->enable = com_mhzc_enable;
700 1.1 thorpej sc->disable = com_mhzc_disable;
701 1.1 thorpej
702 1.1 thorpej com_attach_subr(sc);
703 1.1 thorpej
704 1.1 thorpej sc->enabled = 0;
705 1.1 thorpej }
706 1.1 thorpej
707 1.1 thorpej int
708 1.1 thorpej com_mhzc_enable(sc)
709 1.1 thorpej struct com_softc *sc;
710 1.1 thorpej {
711 1.1 thorpej
712 1.1 thorpej return (mhzc_enable((struct mhzc_softc *)sc->sc_dev.dv_parent,
713 1.1 thorpej MHZC_MODEM_ENABLED));
714 1.1 thorpej }
715 1.1 thorpej
716 1.1 thorpej void
717 1.1 thorpej com_mhzc_disable(sc)
718 1.1 thorpej struct com_softc *sc;
719 1.1 thorpej {
720 1.1 thorpej
721 1.1 thorpej mhzc_disable((struct mhzc_softc *)sc->sc_dev.dv_parent,
722 1.1 thorpej MHZC_MODEM_ENABLED);
723 1.1 thorpej }
724 1.1 thorpej
725 1.1 thorpej #endif /* NCOM_MHZC > 0 */
726 1.1 thorpej
727 1.1 thorpej /****** Here begins the sm attachment code. ******/
728 1.1 thorpej
729 1.1 thorpej #if NSM_MHZC > 0
730 1.1 thorpej int sm_mhzc_match __P((struct device *, struct cfdata *, void *));
731 1.1 thorpej void sm_mhzc_attach __P((struct device *, struct device *, void *));
732 1.1 thorpej int sm_mhzc_detach __P((struct device *, int));
733 1.1 thorpej
734 1.1 thorpej /* No mhzc-specific goo in the softc; it's all in the parent. */
735 1.1 thorpej struct cfattach sm_mhzc_ca = {
736 1.1 thorpej sizeof(struct smc91cxx_softc), sm_mhzc_match, sm_mhzc_attach,
737 1.1 thorpej sm_mhzc_detach, smc91cxx_activate
738 1.1 thorpej };
739 1.1 thorpej
740 1.1 thorpej int sm_mhzc_enable __P((struct smc91cxx_softc *));
741 1.1 thorpej void sm_mhzc_disable __P((struct smc91cxx_softc *));
742 1.1 thorpej
743 1.1 thorpej int
744 1.1 thorpej sm_mhzc_match(parent, match, aux)
745 1.1 thorpej struct device *parent;
746 1.1 thorpej struct cfdata *match;
747 1.1 thorpej void *aux;
748 1.1 thorpej {
749 1.1 thorpej extern struct cfdriver sm_cd;
750 1.1 thorpej const char *name = aux;
751 1.1 thorpej
752 1.1 thorpej /* Device is always present. */
753 1.1 thorpej if (strcmp(name, sm_cd.cd_name) == 0)
754 1.1 thorpej return (1);
755 1.1 thorpej
756 1.1 thorpej return (0);
757 1.1 thorpej }
758 1.1 thorpej
759 1.1 thorpej void
760 1.1 thorpej sm_mhzc_attach(parent, self, aux)
761 1.1 thorpej struct device *parent, *self;
762 1.1 thorpej void *aux;
763 1.1 thorpej {
764 1.1 thorpej struct smc91cxx_softc *sc = (void *)self;
765 1.1 thorpej struct mhzc_softc *msc = (void *)parent;
766 1.1 thorpej u_int8_t myla[ETHER_ADDR_LEN];
767 1.1 thorpej
768 1.1 thorpej printf(":");
769 1.1 thorpej if (pcmcia_io_map(msc->sc_pf, PCMCIA_WIDTH_IO16, 0,
770 1.1 thorpej msc->sc_ethernet_pcioh.size, &msc->sc_ethernet_pcioh,
771 1.1 thorpej &msc->sc_ethernet_io_window)) {
772 1.1 thorpej printf("unable to map I/O space\n");
773 1.1 thorpej return;
774 1.1 thorpej }
775 1.1 thorpej printf("\n");
776 1.1 thorpej
777 1.1 thorpej msc->sc_flags |= MHZC_ETHERNET_MAPPED;
778 1.1 thorpej
779 1.1 thorpej sc->sc_bst = msc->sc_ethernet_pcioh.iot;
780 1.1 thorpej sc->sc_bsh = msc->sc_ethernet_pcioh.ioh;
781 1.1 thorpej
782 1.1 thorpej sc->sc_enable = sm_mhzc_enable;
783 1.1 thorpej sc->sc_disable = sm_mhzc_disable;
784 1.1 thorpej
785 1.1 thorpej if ((*msc->sc_product->mp_enaddr)(msc, myla) != 1)
786 1.1 thorpej return;
787 1.1 thorpej
788 1.1 thorpej /* Perform generic initialization. */
789 1.1 thorpej smc91cxx_attach(sc, myla);
790 1.1 thorpej }
791 1.1 thorpej
792 1.1 thorpej int
793 1.1 thorpej sm_mhzc_detach(self, flags)
794 1.1 thorpej struct device *self;
795 1.1 thorpej int flags;
796 1.1 thorpej {
797 1.1 thorpej #ifdef notyet
798 1.1 thorpej struct smc91cxx_softc *sc = (void *)self;
799 1.1 thorpej
800 1.1 thorpej /*
801 1.1 thorpej * Our softc is about to go away, so drop our reference
802 1.1 thorpej * to the ifnet.
803 1.1 thorpej */
804 1.1 thorpej if_delref(sc->sc_ec.ec_if);
805 1.1 thorpej return (0);
806 1.1 thorpej #else
807 1.1 thorpej return (EBUSY);
808 1.1 thorpej #endif
809 1.1 thorpej }
810 1.1 thorpej
811 1.1 thorpej int
812 1.1 thorpej sm_mhzc_enable(sc)
813 1.1 thorpej struct smc91cxx_softc *sc;
814 1.1 thorpej {
815 1.1 thorpej
816 1.1 thorpej return (mhzc_enable((struct mhzc_softc *)sc->sc_dev.dv_parent,
817 1.1 thorpej MHZC_ETHERNET_ENABLED));
818 1.1 thorpej }
819 1.1 thorpej
820 1.1 thorpej void
821 1.1 thorpej sm_mhzc_disable(sc)
822 1.1 thorpej struct smc91cxx_softc *sc;
823 1.1 thorpej {
824 1.1 thorpej
825 1.1 thorpej mhzc_disable((struct mhzc_softc *)sc->sc_dev.dv_parent,
826 1.1 thorpej MHZC_ETHERNET_ENABLED);
827 1.1 thorpej }
828 1.1 thorpej
829 1.1 thorpej #endif /* NSM_MHZC > 0 */
830