Home | History | Annotate | Line # | Download | only in pcmcia
mhzc.c revision 1.47
      1  1.47    dyoung /*	$NetBSD: mhzc.c,v 1.47 2009/11/13 01:14:35 dyoung Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*-
      4  1.24   mycroft  * Copyright (c) 1999, 2000, 2004 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.24   mycroft  * NASA Ames Research Center, and by Charles M. Hannum.
     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  *
     20   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1   thorpej  */
     32   1.1   thorpej 
     33   1.1   thorpej /*
     34   1.1   thorpej  * Device driver for the Megaherz X-JACK Ethernet/Modem combo cards.
     35   1.1   thorpej  *
     36   1.1   thorpej  * Many thanks to Chuck Cranor for having the patience to sift through
     37   1.1   thorpej  * the Linux smc91c92_cs.c driver to find the magic details to get this
     38   1.1   thorpej  * working!
     39   1.1   thorpej  */
     40   1.8     lukem 
     41   1.8     lukem #include <sys/cdefs.h>
     42  1.47    dyoung __KERNEL_RCSID(0, "$NetBSD: mhzc.c,v 1.47 2009/11/13 01:14:35 dyoung Exp $");
     43   1.1   thorpej 
     44  1.31     perry #include "opt_inet.h"
     45   1.1   thorpej #include "bpfilter.h"
     46   1.1   thorpej 
     47   1.1   thorpej #include <sys/param.h>
     48   1.1   thorpej #include <sys/systm.h>
     49   1.1   thorpej #include <sys/mbuf.h>
     50   1.1   thorpej #include <sys/socket.h>
     51   1.1   thorpej #include <sys/ioctl.h>
     52   1.1   thorpej #include <sys/errno.h>
     53   1.1   thorpej #include <sys/syslog.h>
     54   1.1   thorpej #include <sys/select.h>
     55   1.1   thorpej #include <sys/tty.h>
     56   1.1   thorpej #include <sys/device.h>
     57  1.29   mycroft #include <sys/kernel.h>
     58  1.29   mycroft #include <sys/proc.h>
     59   1.1   thorpej 
     60  1.31     perry #include <net/if.h>
     61  1.31     perry #include <net/if_dl.h>
     62   1.1   thorpej #include <net/if_ether.h>
     63  1.31     perry #include <net/if_media.h>
     64   1.1   thorpej 
     65   1.1   thorpej #ifdef INET
     66   1.1   thorpej #include <netinet/in.h>
     67   1.1   thorpej #include <netinet/in_systm.h>
     68   1.1   thorpej #include <netinet/in_var.h>
     69   1.1   thorpej #include <netinet/ip.h>
     70   1.1   thorpej #include <netinet/if_inarp.h>
     71   1.1   thorpej #endif
     72  1.31     perry 
     73   1.1   thorpej 
     74   1.1   thorpej #if NBPFILTER > 0
     75   1.1   thorpej #include <net/bpf.h>
     76   1.1   thorpej #include <net/bpfdesc.h>
     77   1.1   thorpej #endif
     78   1.1   thorpej 
     79  1.40        ad #include <sys/intr.h>
     80  1.40        ad #include <sys/bus.h>
     81   1.1   thorpej 
     82   1.1   thorpej #include <dev/ic/comreg.h>
     83   1.1   thorpej #include <dev/ic/comvar.h>
     84   1.6    briggs 
     85   1.6    briggs #include <dev/mii/mii.h>
     86   1.6    briggs #include <dev/mii/miivar.h>
     87   1.1   thorpej 
     88   1.1   thorpej #include <dev/ic/smc91cxxreg.h>
     89   1.1   thorpej #include <dev/ic/smc91cxxvar.h>
     90   1.1   thorpej 
     91   1.1   thorpej #include <dev/pcmcia/pcmciareg.h>
     92   1.1   thorpej #include <dev/pcmcia/pcmciavar.h>
     93   1.1   thorpej #include <dev/pcmcia/pcmciadevs.h>
     94   1.1   thorpej 
     95   1.1   thorpej #include "mhzc.h"
     96   1.1   thorpej 
     97   1.1   thorpej struct mhzc_softc {
     98   1.1   thorpej 	struct device sc_dev;		/* generic device glue */
     99   1.1   thorpej 
    100   1.1   thorpej 	struct pcmcia_function *sc_pf;	/* our PCMCIA function */
    101   1.1   thorpej 	void *sc_ih;			/* interrupt handle */
    102   1.1   thorpej 
    103   1.1   thorpej 	const struct mhzc_product *sc_product;
    104   1.1   thorpej 
    105   1.1   thorpej 	/*
    106   1.1   thorpej 	 * Data for the Modem portion.
    107   1.1   thorpej 	 */
    108  1.46    cegger 	device_t sc_modem;
    109   1.1   thorpej 	struct pcmcia_io_handle sc_modem_pcioh;
    110   1.1   thorpej 	int sc_modem_io_window;
    111   1.1   thorpej 
    112   1.1   thorpej 	/*
    113   1.1   thorpej 	 * Data for the Ethernet portion.
    114   1.1   thorpej 	 */
    115  1.46    cegger 	device_t sc_ethernet;
    116   1.1   thorpej 	struct pcmcia_io_handle sc_ethernet_pcioh;
    117   1.1   thorpej 	int sc_ethernet_io_window;
    118   1.1   thorpej 
    119   1.1   thorpej 	int sc_flags;
    120   1.1   thorpej };
    121   1.1   thorpej 
    122   1.1   thorpej /* sc_flags */
    123   1.1   thorpej #define	MHZC_MODEM_MAPPED	0x01
    124   1.1   thorpej #define	MHZC_ETHERNET_MAPPED	0x02
    125   1.1   thorpej #define	MHZC_MODEM_ENABLED	0x04
    126   1.1   thorpej #define	MHZC_ETHERNET_ENABLED	0x08
    127  1.21   mycroft #define	MHZC_MODEM_ALLOCED	0x10
    128  1.21   mycroft #define	MHZC_ETHERNET_ALLOCED	0x20
    129   1.1   thorpej 
    130  1.46    cegger int	mhzc_match(device_t, cfdata_t, void *);
    131  1.46    cegger void	mhzc_attach(device_t, device_t, void *);
    132  1.47    dyoung void	mhzc_childdet(device_t, device_t);
    133  1.46    cegger int	mhzc_detach(device_t, int);
    134   1.1   thorpej 
    135  1.12   thorpej CFATTACH_DECL(mhzc, sizeof(struct mhzc_softc),
    136  1.47    dyoung     mhzc_match, mhzc_attach, mhzc_detach, NULL);
    137   1.1   thorpej 
    138  1.30     perry int	mhzc_em3336_enaddr(struct mhzc_softc *, u_int8_t *);
    139  1.30     perry int	mhzc_em3336_enable(struct mhzc_softc *);
    140   1.1   thorpej 
    141   1.4       cgd const struct mhzc_product {
    142   1.4       cgd 	struct pcmcia_product mp_product;
    143   1.1   thorpej 
    144   1.1   thorpej 	/* Get the Ethernet address for this card. */
    145  1.30     perry 	int		(*mp_enaddr)(struct mhzc_softc *, u_int8_t *);
    146   1.1   thorpej 
    147   1.1   thorpej 	/* Perform any special `enable' magic. */
    148  1.30     perry 	int		(*mp_enable)(struct mhzc_softc *);
    149   1.1   thorpej } mhzc_products[] = {
    150  1.27   mycroft 	{ { PCMCIA_VENDOR_MEGAHERTZ, PCMCIA_PRODUCT_MEGAHERTZ_EM3336,
    151  1.27   mycroft 	    PCMCIA_CIS_INVALID },
    152   1.1   thorpej 	  mhzc_em3336_enaddr,		mhzc_em3336_enable },
    153   1.1   thorpej };
    154  1.27   mycroft static const size_t mhzc_nproducts =
    155  1.27   mycroft     sizeof(mhzc_products) / sizeof(mhzc_products[0]);
    156   1.1   thorpej 
    157  1.30     perry int	mhzc_print(void *, const char *);
    158   1.1   thorpej 
    159  1.30     perry int	mhzc_check_cfe(struct mhzc_softc *, struct pcmcia_config_entry *);
    160  1.30     perry int	mhzc_alloc_ethernet(struct mhzc_softc *, struct pcmcia_config_entry *);
    161   1.1   thorpej 
    162  1.30     perry int	mhzc_enable(struct mhzc_softc *, int);
    163  1.30     perry void	mhzc_disable(struct mhzc_softc *, int);
    164   1.1   thorpej 
    165  1.30     perry int	mhzc_intr(void *);
    166   1.1   thorpej 
    167   1.1   thorpej int
    168  1.47    dyoung mhzc_match(device_t parent, cfdata_t match, void *aux)
    169   1.1   thorpej {
    170   1.1   thorpej 	struct pcmcia_attach_args *pa = aux;
    171   1.1   thorpej 
    172  1.27   mycroft 	if (pcmcia_product_lookup(pa, mhzc_products, mhzc_nproducts,
    173  1.27   mycroft 	    sizeof(mhzc_products[0]), NULL))
    174  1.27   mycroft 		return (2);		/* beat `com' */
    175   1.1   thorpej 	return (0);
    176   1.1   thorpej }
    177   1.1   thorpej 
    178   1.1   thorpej void
    179  1.46    cegger mhzc_attach(device_t parent, device_t self, void *aux)
    180   1.1   thorpej {
    181  1.47    dyoung 	struct mhzc_softc *sc = device_private(self);
    182   1.1   thorpej 	struct pcmcia_attach_args *pa = aux;
    183   1.1   thorpej 	struct pcmcia_config_entry *cfe;
    184  1.25   mycroft 	int error;
    185   1.1   thorpej 
    186   1.1   thorpej 	sc->sc_pf = pa->pf;
    187   1.1   thorpej 
    188  1.27   mycroft 	sc->sc_product = pcmcia_product_lookup(pa, mhzc_products,
    189  1.27   mycroft 	    mhzc_nproducts, sizeof(mhzc_products[0]), NULL);
    190  1.27   mycroft 	if (!sc->sc_product)
    191   1.1   thorpej 		panic("mhzc_attach: impossible");
    192   1.1   thorpej 
    193   1.1   thorpej 	/*
    194   1.1   thorpej 	 * The address decoders on these cards are wacky.  The configuration
    195   1.1   thorpej 	 * entries are set up to look like serial ports, and have no
    196   1.1   thorpej 	 * information about the Ethernet portion.  In order to talk to
    197   1.1   thorpej 	 * the Modem portion, the I/O address must have bit 0x80 set.
    198   1.1   thorpej 	 * In order to talk to the Ethernet portion, the I/O address must
    199   1.1   thorpej 	 * have the 0x80 bit clear.
    200   1.1   thorpej 	 *
    201   1.1   thorpej 	 * The standard configuration entries conveniently have 0x80 set
    202   1.1   thorpej 	 * in them, and have a length of 8 (a 16550's size, convenient!),
    203   1.1   thorpej 	 * so we use those to set up the Modem portion.
    204   1.1   thorpej 	 *
    205   1.1   thorpej 	 * Once we have the Modem's address established, we search for
    206   1.1   thorpej 	 * an address suitable for the Ethernet portion.  We do this by
    207   1.1   thorpej 	 * rounding up to the next 16-byte aligned address where 0x80
    208   1.1   thorpej 	 * isn't set (the SMC Ethernet chip has a 16-byte address size)
    209   1.1   thorpej 	 * and attemping to allocate a 16-byte region until we succeed.
    210   1.1   thorpej 	 *
    211   1.1   thorpej 	 * Sure would have been nice if Megahertz had made the card a
    212   1.1   thorpej 	 * proper multi-function device.
    213   1.1   thorpej 	 */
    214   1.9     lukem 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    215   1.1   thorpej 		if (mhzc_check_cfe(sc, cfe)) {
    216   1.1   thorpej 			/* Found one! */
    217   1.1   thorpej 			break;
    218   1.1   thorpej 		}
    219   1.1   thorpej 	}
    220   1.1   thorpej 	if (cfe == NULL) {
    221  1.42    cegger 		aprint_error_dev(self, "unable to find suitable config table entry\n");
    222  1.21   mycroft 		goto fail;
    223   1.1   thorpej 	}
    224   1.1   thorpej 
    225   1.7       uwe 	if (mhzc_alloc_ethernet(sc, cfe) == 0) {
    226  1.42    cegger 		aprint_error_dev(self, "unable to allocate space for Ethernet portion\n");
    227  1.21   mycroft 		goto fail;
    228   1.1   thorpej 	}
    229   1.1   thorpej 
    230   1.1   thorpej 	/* Enable the card. */
    231   1.1   thorpej 	pcmcia_function_init(pa->pf, cfe);
    232  1.21   mycroft 
    233  1.23   mycroft 	if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_IO8, &sc->sc_modem_pcioh,
    234  1.23   mycroft 	    &sc->sc_modem_io_window)) {
    235  1.42    cegger 		aprint_error_dev(&sc->sc_dev, "unable to map I/O space\n");
    236  1.23   mycroft 		goto fail;
    237  1.23   mycroft 	}
    238  1.23   mycroft 	sc->sc_flags |= MHZC_MODEM_MAPPED;
    239  1.23   mycroft 
    240  1.23   mycroft 	if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_AUTO, &sc->sc_ethernet_pcioh,
    241  1.23   mycroft 	    &sc->sc_ethernet_io_window)) {
    242  1.42    cegger 		aprint_error_dev(&sc->sc_dev, "unable to map I/O space\n");
    243  1.23   mycroft 		goto fail;
    244  1.23   mycroft 	}
    245  1.23   mycroft 	sc->sc_flags |= MHZC_ETHERNET_MAPPED;
    246  1.23   mycroft 
    247  1.25   mycroft 	error = mhzc_enable(sc, MHZC_MODEM_ENABLED|MHZC_ETHERNET_ENABLED);
    248  1.25   mycroft 	if (error)
    249  1.21   mycroft 		goto fail;
    250   1.1   thorpej 
    251  1.32  christos 	/*XXXUNCONST*/
    252  1.32  christos 	sc->sc_modem = config_found(self, __UNCONST("com"), mhzc_print);
    253  1.32  christos 	/*XXXUNCONST*/
    254  1.32  christos 	sc->sc_ethernet = config_found(self, __UNCONST("sm"), mhzc_print);
    255   1.1   thorpej 
    256  1.21   mycroft 	mhzc_disable(sc, MHZC_MODEM_ENABLED|MHZC_ETHERNET_ENABLED);
    257   1.5     enami 	return;
    258   1.5     enami 
    259  1.21   mycroft fail:
    260  1.23   mycroft 	/* I/O spaces will be freed by detach. */
    261  1.23   mycroft 	;
    262   1.1   thorpej }
    263   1.1   thorpej 
    264   1.1   thorpej int
    265  1.44       dsl mhzc_check_cfe(struct mhzc_softc *sc, struct pcmcia_config_entry *cfe)
    266   1.1   thorpej {
    267   1.1   thorpej 
    268   1.1   thorpej 	if (cfe->num_memspace != 0)
    269   1.1   thorpej 		return (0);
    270   1.1   thorpej 
    271   1.1   thorpej 	if (cfe->num_iospace != 1)
    272   1.1   thorpej 		return (0);
    273   1.1   thorpej 
    274   1.1   thorpej 	if (pcmcia_io_alloc(sc->sc_pf,
    275   1.1   thorpej 	    cfe->iospace[0].start,
    276   1.1   thorpej 	    cfe->iospace[0].length,
    277   1.1   thorpej 	    cfe->iospace[0].length,
    278   1.1   thorpej 	    &sc->sc_modem_pcioh) == 0) {
    279   1.1   thorpej 		/* Found one for the modem! */
    280  1.21   mycroft 		sc->sc_flags |= MHZC_MODEM_ALLOCED;
    281   1.1   thorpej 		return (1);
    282   1.1   thorpej 	}
    283   1.1   thorpej 
    284   1.1   thorpej 	return (0);
    285   1.1   thorpej }
    286   1.1   thorpej 
    287   1.1   thorpej int
    288  1.44       dsl mhzc_alloc_ethernet(struct mhzc_softc *sc, struct pcmcia_config_entry *cfe)
    289   1.1   thorpej {
    290   1.1   thorpej 	bus_addr_t addr, maxaddr;
    291   1.1   thorpej 
    292   1.7       uwe 	addr = cfe->iospace[0].start + cfe->iospace[0].length;
    293   1.7       uwe 	maxaddr = 0x1000;
    294   1.1   thorpej 
    295   1.1   thorpej 	/*
    296   1.1   thorpej 	 * Now round it up so that it starts on a 16-byte boundary.
    297   1.1   thorpej 	 */
    298   1.1   thorpej 	addr = roundup(addr, 0x10);
    299   1.1   thorpej 
    300   1.1   thorpej 	for (; (addr + 0x10) < maxaddr; addr += 0x10) {
    301   1.1   thorpej 		if (addr & 0x80)
    302   1.1   thorpej 			continue;
    303   1.1   thorpej 		if (pcmcia_io_alloc(sc->sc_pf, addr, 0x10, 0x10,
    304   1.1   thorpej 		    &sc->sc_ethernet_pcioh) == 0) {
    305   1.1   thorpej 			/* Found one for the ethernet! */
    306  1.21   mycroft 			sc->sc_flags |= MHZC_ETHERNET_ALLOCED;
    307   1.1   thorpej 			return (1);
    308   1.1   thorpej 		}
    309   1.1   thorpej 	}
    310   1.1   thorpej 
    311   1.1   thorpej 	return (0);
    312   1.1   thorpej }
    313   1.1   thorpej 
    314   1.1   thorpej int
    315  1.44       dsl mhzc_print(void *aux, const char *pnp)
    316   1.1   thorpej {
    317   1.1   thorpej 	const char *name = aux;
    318   1.1   thorpej 
    319   1.1   thorpej 	if (pnp)
    320  1.14   thorpej 		aprint_normal("%s at %s(*)",  name, pnp);
    321   1.1   thorpej 
    322   1.1   thorpej 	return (UNCONF);
    323   1.1   thorpej }
    324   1.1   thorpej 
    325  1.47    dyoung void
    326  1.47    dyoung mhzc_childdet(device_t self, device_t child)
    327  1.47    dyoung {
    328  1.47    dyoung 	struct mhzc_softc *sc = device_private(self);
    329  1.47    dyoung 
    330  1.47    dyoung 	if (sc->sc_ethernet == child)
    331  1.47    dyoung 		sc->sc_ethernet = NULL;
    332  1.47    dyoung 	if (sc->sc_modem == child)
    333  1.47    dyoung 		sc->sc_modem = NULL;
    334  1.47    dyoung }
    335  1.47    dyoung 
    336   1.1   thorpej int
    337  1.46    cegger mhzc_detach(device_t self, int flags)
    338   1.1   thorpej {
    339  1.47    dyoung 	struct mhzc_softc *sc = device_private(self);
    340   1.1   thorpej 	int rv;
    341   1.1   thorpej 
    342   1.1   thorpej 	if (sc->sc_ethernet != NULL) {
    343  1.47    dyoung 		if ((rv = config_detach(sc->sc_ethernet, flags)) != 0)
    344  1.47    dyoung 			return rv;
    345   1.1   thorpej 	}
    346   1.1   thorpej 
    347   1.1   thorpej 	if (sc->sc_modem != NULL) {
    348  1.47    dyoung 		if ((rv = config_detach(sc->sc_modem, flags)) != 0)
    349  1.47    dyoung 			return rv;
    350   1.1   thorpej 	}
    351   1.1   thorpej 
    352   1.1   thorpej 	/* Unmap our i/o windows. */
    353   1.1   thorpej 	if (sc->sc_flags & MHZC_MODEM_MAPPED)
    354   1.1   thorpej 		pcmcia_io_unmap(sc->sc_pf, sc->sc_modem_io_window);
    355   1.1   thorpej 	if (sc->sc_flags & MHZC_ETHERNET_MAPPED)
    356   1.1   thorpej 		pcmcia_io_unmap(sc->sc_pf, sc->sc_ethernet_io_window);
    357   1.1   thorpej 
    358   1.1   thorpej 	/* Free our i/o spaces. */
    359  1.21   mycroft 	if (sc->sc_flags & MHZC_ETHERNET_ALLOCED)
    360   1.5     enami 		pcmcia_io_free(sc->sc_pf, &sc->sc_modem_pcioh);
    361  1.21   mycroft 	if (sc->sc_flags & MHZC_MODEM_ALLOCED)
    362   1.5     enami 		pcmcia_io_free(sc->sc_pf, &sc->sc_ethernet_pcioh);
    363  1.23   mycroft 
    364  1.21   mycroft 	sc->sc_flags = 0;
    365   1.1   thorpej 
    366  1.47    dyoung 	return 0;
    367   1.1   thorpej }
    368   1.1   thorpej 
    369   1.1   thorpej int
    370  1.44       dsl mhzc_intr(void *arg)
    371   1.1   thorpej {
    372   1.1   thorpej 	struct mhzc_softc *sc = arg;
    373   1.1   thorpej 	int rval = 0;
    374   1.1   thorpej 
    375   1.1   thorpej #if NCOM_MHZC > 0
    376   1.1   thorpej 	if (sc->sc_modem != NULL &&
    377   1.1   thorpej 	    (sc->sc_flags & MHZC_MODEM_ENABLED) != 0)
    378   1.1   thorpej 		rval |= comintr(sc->sc_modem);
    379   1.1   thorpej #endif
    380   1.1   thorpej 
    381   1.1   thorpej #if NSM_MHZC > 0
    382   1.1   thorpej 	if (sc->sc_ethernet != NULL &&
    383   1.1   thorpej 	    (sc->sc_flags & MHZC_ETHERNET_ENABLED) != 0)
    384   1.1   thorpej 		rval |= smc91cxx_intr(sc->sc_ethernet);
    385   1.1   thorpej #endif
    386   1.1   thorpej 
    387   1.1   thorpej 	return (rval);
    388   1.1   thorpej }
    389   1.1   thorpej 
    390   1.1   thorpej int
    391  1.44       dsl mhzc_enable(struct mhzc_softc *sc, int flag)
    392   1.1   thorpej {
    393  1.25   mycroft 	int error;
    394   1.1   thorpej 
    395  1.21   mycroft 	if ((sc->sc_flags & flag) == flag) {
    396  1.42    cegger 		printf("%s: already enabled\n", device_xname(&sc->sc_dev));
    397  1.21   mycroft 		return (0);
    398   1.1   thorpej 	}
    399   1.1   thorpej 
    400   1.1   thorpej 	if ((sc->sc_flags & (MHZC_MODEM_ENABLED|MHZC_ETHERNET_ENABLED)) != 0) {
    401   1.1   thorpej 		sc->sc_flags |= flag;
    402   1.1   thorpej 		return (0);
    403   1.1   thorpej 	}
    404   1.1   thorpej 
    405   1.1   thorpej 	/*
    406   1.1   thorpej 	 * Establish our interrupt handler.
    407   1.1   thorpej 	 *
    408   1.1   thorpej 	 * XXX Note, we establish this at IPL_NET.  This is suboptimal
    409   1.1   thorpej 	 * XXX the Modem portion, but is necessary to make the Ethernet
    410   1.1   thorpej 	 * XXX portion have the correct interrupt level semantics.
    411   1.1   thorpej 	 *
    412   1.1   thorpej 	 * XXX Eventually we should use the `enabled' bits in the
    413   1.1   thorpej 	 * XXX flags word to determine which level we should be at.
    414   1.1   thorpej 	 */
    415   1.1   thorpej 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET,
    416   1.1   thorpej 	    mhzc_intr, sc);
    417  1.25   mycroft 	if (!sc->sc_ih)
    418  1.25   mycroft 		return (EIO);
    419   1.1   thorpej 
    420  1.25   mycroft 	error = pcmcia_function_enable(sc->sc_pf);
    421  1.25   mycroft 	if (error) {
    422   1.2   thorpej 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    423  1.26   mycroft 		sc->sc_ih = 0;
    424  1.25   mycroft 		return (error);
    425   1.2   thorpej 	}
    426   1.2   thorpej 
    427   1.2   thorpej 	/*
    428   1.2   thorpej 	 * Perform any special enable magic necessary.
    429   1.2   thorpej 	 */
    430   1.2   thorpej 	if (sc->sc_product->mp_enable != NULL &&
    431   1.2   thorpej 	    (*sc->sc_product->mp_enable)(sc) != 0) {
    432   1.2   thorpej 		pcmcia_function_disable(sc->sc_pf);
    433   1.1   thorpej 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    434   1.1   thorpej 		return (1);
    435   1.1   thorpej 	}
    436   1.1   thorpej 
    437   1.1   thorpej 	sc->sc_flags |= flag;
    438   1.1   thorpej 	return (0);
    439   1.1   thorpej }
    440   1.1   thorpej 
    441   1.1   thorpej void
    442  1.44       dsl mhzc_disable(struct mhzc_softc *sc, int flag)
    443   1.1   thorpej {
    444   1.1   thorpej 
    445   1.1   thorpej 	if ((sc->sc_flags & flag) == 0) {
    446  1.42    cegger 		printf("%s: already disabled\n", device_xname(&sc->sc_dev));
    447  1.21   mycroft 		return;
    448   1.1   thorpej 	}
    449   1.1   thorpej 
    450   1.1   thorpej 	sc->sc_flags &= ~flag;
    451   1.1   thorpej 	if ((sc->sc_flags & (MHZC_MODEM_ENABLED|MHZC_ETHERNET_ENABLED)) != 0)
    452   1.1   thorpej 		return;
    453   1.1   thorpej 
    454  1.22   mycroft 	pcmcia_function_disable(sc->sc_pf);
    455   1.1   thorpej 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    456  1.25   mycroft 	sc->sc_ih = 0;
    457   1.1   thorpej }
    458   1.1   thorpej 
    459   1.1   thorpej /*****************************************************************************
    460   1.1   thorpej  * Megahertz EM3336 (and compatibles) support
    461   1.1   thorpej  *****************************************************************************/
    462   1.1   thorpej 
    463  1.30     perry int	mhzc_em3336_lannid_ciscallback(struct pcmcia_tuple *, void *);
    464  1.30     perry int	mhzc_em3336_ascii_enaddr(const char *cisstr, u_int8_t *);
    465   1.1   thorpej 
    466   1.1   thorpej int
    467  1.44       dsl mhzc_em3336_enaddr(struct mhzc_softc *sc, u_int8_t *myla)
    468   1.1   thorpej {
    469   1.1   thorpej 
    470   1.1   thorpej 	/* Get the station address from CIS tuple 0x81. */
    471  1.35   thorpej 	if (pcmcia_scan_cis(device_parent(&sc->sc_dev),
    472   1.1   thorpej 	    mhzc_em3336_lannid_ciscallback, myla) != 1) {
    473   1.1   thorpej 		printf("%s: unable to get Ethernet address from CIS\n",
    474  1.42    cegger 		    device_xname(&sc->sc_dev));
    475   1.1   thorpej 		return (0);
    476   1.1   thorpej 	}
    477   1.1   thorpej 
    478   1.1   thorpej 	return (1);
    479   1.1   thorpej }
    480   1.1   thorpej 
    481   1.1   thorpej int
    482  1.44       dsl mhzc_em3336_enable(struct mhzc_softc *sc)
    483   1.1   thorpej {
    484   1.1   thorpej 	struct pcmcia_mem_handle memh;
    485  1.33       jdc 	bus_size_t memoff;
    486   1.1   thorpej 	int memwin, reg;
    487   1.1   thorpej 
    488   1.1   thorpej 	/*
    489   1.1   thorpej 	 * Bring the chip to live by touching its registers in the correct
    490   1.1   thorpej 	 * way (as per my reference... the Linux smc91c92_cs.c driver by
    491   1.1   thorpej 	 * David A. Hinds).
    492   1.1   thorpej 	 */
    493  1.31     perry 
    494   1.1   thorpej 	/* Map the ISRPOWEREG. */
    495   1.1   thorpej 	if (pcmcia_mem_alloc(sc->sc_pf, 0x1000, &memh) != 0) {
    496  1.42    cegger 		aprint_error_dev(&sc->sc_dev, "unable to allocate memory space\n");
    497   1.1   thorpej 		return (1);
    498   1.1   thorpej 	}
    499   1.1   thorpej 
    500   1.1   thorpej 	if (pcmcia_mem_map(sc->sc_pf, PCMCIA_MEM_ATTR, 0, 0x1000,
    501   1.1   thorpej 	    &memh, &memoff, &memwin)) {
    502  1.42    cegger 		aprint_error_dev(&sc->sc_dev, "unable to map memory space\n");
    503   1.1   thorpej 		pcmcia_mem_free(sc->sc_pf, &memh);
    504   1.1   thorpej 		return (1);
    505   1.1   thorpej 	}
    506   1.1   thorpej 
    507   1.1   thorpej 	/*
    508   1.1   thorpej 	 * The magic sequence:
    509   1.1   thorpej 	 *
    510   1.1   thorpej 	 *	- read/write the CCR option register.
    511   1.1   thorpej 	 *	- read the ISRPOWEREG 2 times.
    512   1.1   thorpej 	 *	- read/write the CCR option register again.
    513   1.1   thorpej 	 */
    514   1.1   thorpej 
    515   1.1   thorpej 	reg = pcmcia_ccr_read(sc->sc_pf, PCMCIA_CCR_OPTION);
    516   1.1   thorpej 	pcmcia_ccr_write(sc->sc_pf, PCMCIA_CCR_OPTION, reg);
    517   1.1   thorpej 
    518   1.1   thorpej 	reg = bus_space_read_1(memh.memt, memh.memh, 0x380);
    519   1.1   thorpej 	delay(5);
    520   1.1   thorpej 	reg = bus_space_read_1(memh.memt, memh.memh, 0x380);
    521   1.1   thorpej 
    522  1.29   mycroft 	tsleep(&mhzc_em3336_enable, PWAIT, "mhz3en", hz * 200 / 1000);
    523   1.1   thorpej 
    524   1.1   thorpej 	reg = pcmcia_ccr_read(sc->sc_pf, PCMCIA_CCR_OPTION);
    525   1.1   thorpej 	delay(5);
    526   1.1   thorpej 	pcmcia_ccr_write(sc->sc_pf, PCMCIA_CCR_OPTION, reg);
    527   1.1   thorpej 
    528   1.1   thorpej 	pcmcia_mem_unmap(sc->sc_pf, memwin);
    529   1.1   thorpej 	pcmcia_mem_free(sc->sc_pf, &memh);
    530   1.1   thorpej 
    531   1.1   thorpej 	return (0);
    532   1.1   thorpej }
    533   1.1   thorpej 
    534   1.1   thorpej int
    535  1.44       dsl mhzc_em3336_lannid_ciscallback(struct pcmcia_tuple *tuple, void *arg)
    536   1.1   thorpej {
    537   1.1   thorpej 	u_int8_t *myla = arg, addr_str[ETHER_ADDR_LEN * 2];
    538   1.1   thorpej 	int i;
    539   1.1   thorpej 
    540   1.1   thorpej 	if (tuple->code == 0x81) {
    541   1.1   thorpej 		/*
    542   1.1   thorpej 		 * We have a string-encoded address.  Length includes
    543   1.1   thorpej 		 * terminating 0xff.
    544   1.1   thorpej 		 */
    545   1.1   thorpej 		if (tuple->length != (ETHER_ADDR_LEN * 2) + 1)
    546   1.1   thorpej 			return (0);
    547   1.1   thorpej 
    548   1.1   thorpej 		for (i = 0; i < tuple->length - 1; i++)
    549   1.1   thorpej 			addr_str[i] = pcmcia_tuple_read_1(tuple, i);
    550   1.1   thorpej 
    551   1.1   thorpej 		/*
    552   1.1   thorpej 		 * Decode the string into `myla'.
    553   1.1   thorpej 		 */
    554   1.1   thorpej 		return (mhzc_em3336_ascii_enaddr(addr_str, myla));
    555   1.1   thorpej 	}
    556   1.1   thorpej 	return (0);
    557   1.1   thorpej }
    558   1.1   thorpej 
    559   1.1   thorpej /* XXX This should be shared w/ if_sm_pcmcia.c */
    560   1.1   thorpej int
    561  1.44       dsl mhzc_em3336_ascii_enaddr(const char *cisstr, u_int8_t *myla)
    562   1.1   thorpej {
    563   1.1   thorpej 	u_int8_t digit;
    564   1.1   thorpej 	int i;
    565   1.1   thorpej 
    566   1.1   thorpej 	memset(myla, 0, ETHER_ADDR_LEN);
    567   1.1   thorpej 
    568   1.1   thorpej 	for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
    569   1.1   thorpej 		if (cisstr[i] >= '0' && cisstr[i] <= '9')
    570   1.1   thorpej 			digit |= cisstr[i] - '0';
    571   1.1   thorpej 		else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
    572   1.1   thorpej 			digit |= (cisstr[i] - 'a') + 10;
    573   1.1   thorpej 		else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
    574   1.1   thorpej 			digit |= (cisstr[i] - 'A') + 10;
    575   1.1   thorpej 		else {
    576   1.1   thorpej 			/* Bogus digit!! */
    577   1.1   thorpej 			return (0);
    578   1.1   thorpej 		}
    579   1.1   thorpej 
    580   1.1   thorpej 		/* Compensate for ordering of digits. */
    581   1.1   thorpej 		if (i & 1) {
    582   1.1   thorpej 			myla[i >> 1] = digit;
    583   1.1   thorpej 			digit = 0;
    584   1.1   thorpej 		} else
    585   1.1   thorpej 			digit <<= 4;
    586   1.1   thorpej 	}
    587   1.1   thorpej 
    588   1.1   thorpej 	return (1);
    589   1.1   thorpej }
    590   1.1   thorpej 
    591   1.1   thorpej /****** Here begins the com attachment code. ******/
    592   1.1   thorpej 
    593   1.1   thorpej #if NCOM_MHZC > 0
    594  1.41      cube int	com_mhzc_match(device_t, cfdata_t , void *);
    595  1.41      cube void	com_mhzc_attach(device_t, device_t, void *);
    596  1.41      cube int	com_mhzc_detach(device_t, int);
    597   1.1   thorpej 
    598   1.1   thorpej /* No mhzc-specific goo in the softc; it's all in the parent. */
    599  1.41      cube CFATTACH_DECL_NEW(com_mhzc, sizeof(struct com_softc),
    600  1.47    dyoung     com_mhzc_match, com_mhzc_attach, com_detach, NULL);
    601   1.1   thorpej 
    602  1.30     perry int	com_mhzc_enable(struct com_softc *);
    603  1.30     perry void	com_mhzc_disable(struct com_softc *);
    604   1.1   thorpej 
    605   1.1   thorpej int
    606  1.41      cube com_mhzc_match(device_t parent, cfdata_t match, void *aux)
    607   1.1   thorpej {
    608   1.1   thorpej 	extern struct cfdriver com_cd;
    609   1.1   thorpej 	const char *name = aux;
    610   1.1   thorpej 
    611   1.1   thorpej 	/* Device is always present. */
    612   1.1   thorpej 	if (strcmp(name, com_cd.cd_name) == 0)
    613   1.1   thorpej 		return (1);
    614   1.1   thorpej 
    615   1.1   thorpej 	return (0);
    616   1.1   thorpej }
    617   1.1   thorpej 
    618   1.1   thorpej void
    619  1.41      cube com_mhzc_attach(device_t parent, device_t self, void *aux)
    620   1.1   thorpej {
    621  1.41      cube 	struct com_softc *sc = device_private(self);
    622  1.41      cube 	struct mhzc_softc *msc = device_private(parent);
    623   1.1   thorpej 
    624  1.41      cube 	sc->sc_dev = self;
    625  1.16   mycroft 	aprint_normal("\n");
    626  1.16   mycroft 
    627  1.36   gdamore 	COM_INIT_REGS(sc->sc_regs,
    628  1.36   gdamore 	    msc->sc_modem_pcioh.iot,
    629  1.36   gdamore 	    msc->sc_modem_pcioh.ioh,
    630  1.36   gdamore 	    -1);
    631   1.1   thorpej 
    632   1.1   thorpej 	sc->enabled = 1;
    633   1.1   thorpej 
    634   1.1   thorpej 	sc->sc_frequency = COM_FREQ;
    635   1.1   thorpej 
    636   1.1   thorpej 	sc->enable = com_mhzc_enable;
    637   1.1   thorpej 	sc->disable = com_mhzc_disable;
    638   1.1   thorpej 
    639  1.41      cube 	aprint_normal("%s", device_xname(self));
    640  1.16   mycroft 
    641   1.1   thorpej 	com_attach_subr(sc);
    642   1.1   thorpej 
    643   1.1   thorpej 	sc->enabled = 0;
    644   1.1   thorpej }
    645   1.1   thorpej 
    646   1.1   thorpej int
    647  1.41      cube com_mhzc_enable(struct com_softc *sc)
    648   1.1   thorpej {
    649   1.1   thorpej 
    650  1.41      cube 	return (mhzc_enable(device_private(device_parent(sc->sc_dev)),
    651   1.1   thorpej 	    MHZC_MODEM_ENABLED));
    652   1.1   thorpej }
    653   1.1   thorpej 
    654   1.1   thorpej void
    655  1.41      cube com_mhzc_disable(struct com_softc *sc)
    656   1.1   thorpej {
    657   1.1   thorpej 
    658  1.41      cube 	mhzc_disable(device_private(device_parent(sc->sc_dev)),
    659   1.1   thorpej 	    MHZC_MODEM_ENABLED);
    660   1.1   thorpej }
    661   1.1   thorpej 
    662   1.1   thorpej #endif /* NCOM_MHZC > 0 */
    663   1.1   thorpej 
    664   1.1   thorpej /****** Here begins the sm attachment code. ******/
    665   1.1   thorpej 
    666   1.1   thorpej #if NSM_MHZC > 0
    667  1.46    cegger int	sm_mhzc_match(device_t, cfdata_t, void *);
    668  1.46    cegger void	sm_mhzc_attach(device_t, device_t, void *);
    669   1.1   thorpej 
    670   1.1   thorpej /* No mhzc-specific goo in the softc; it's all in the parent. */
    671  1.12   thorpej CFATTACH_DECL(sm_mhzc, sizeof(struct smc91cxx_softc),
    672  1.13   thorpej     sm_mhzc_match, sm_mhzc_attach, smc91cxx_detach, smc91cxx_activate);
    673   1.1   thorpej 
    674  1.30     perry int	sm_mhzc_enable(struct smc91cxx_softc *);
    675  1.30     perry void	sm_mhzc_disable(struct smc91cxx_softc *);
    676   1.1   thorpej 
    677   1.1   thorpej int
    678  1.47    dyoung sm_mhzc_match(device_t parent, cfdata_t match, void *aux)
    679   1.1   thorpej {
    680   1.1   thorpej 	extern struct cfdriver sm_cd;
    681   1.1   thorpej 	const char *name = aux;
    682   1.1   thorpej 
    683   1.1   thorpej 	/* Device is always present. */
    684   1.1   thorpej 	if (strcmp(name, sm_cd.cd_name) == 0)
    685   1.1   thorpej 		return (1);
    686   1.1   thorpej 
    687   1.1   thorpej 	return (0);
    688   1.1   thorpej }
    689   1.1   thorpej 
    690   1.1   thorpej void
    691  1.46    cegger sm_mhzc_attach(device_t parent, device_t self, void *aux)
    692   1.1   thorpej {
    693  1.47    dyoung 	struct smc91cxx_softc *sc = device_private(self);
    694  1.47    dyoung 	struct mhzc_softc *msc = device_private(parent);
    695   1.1   thorpej 	u_int8_t myla[ETHER_ADDR_LEN];
    696   1.1   thorpej 
    697  1.17   mycroft 	aprint_normal("\n");
    698  1.16   mycroft 
    699   1.1   thorpej 	sc->sc_bst = msc->sc_ethernet_pcioh.iot;
    700   1.1   thorpej 	sc->sc_bsh = msc->sc_ethernet_pcioh.ioh;
    701   1.1   thorpej 
    702   1.1   thorpej 	sc->sc_enable = sm_mhzc_enable;
    703   1.1   thorpej 	sc->sc_disable = sm_mhzc_disable;
    704   1.1   thorpej 
    705   1.1   thorpej 	if ((*msc->sc_product->mp_enaddr)(msc, myla) != 1)
    706   1.1   thorpej 		return;
    707   1.1   thorpej 
    708   1.1   thorpej 	/* Perform generic initialization. */
    709   1.1   thorpej 	smc91cxx_attach(sc, myla);
    710   1.1   thorpej }
    711   1.1   thorpej 
    712   1.1   thorpej int
    713  1.44       dsl sm_mhzc_enable(struct smc91cxx_softc *sc)
    714   1.1   thorpej {
    715  1.47    dyoung 	struct mhzc_softc *xsc = device_private(device_parent(&sc->sc_dev));
    716   1.1   thorpej 
    717  1.47    dyoung 	return mhzc_enable(xsc, MHZC_ETHERNET_ENABLED);
    718   1.1   thorpej }
    719   1.1   thorpej 
    720   1.1   thorpej void
    721  1.44       dsl sm_mhzc_disable(struct smc91cxx_softc *sc)
    722   1.1   thorpej {
    723  1.47    dyoung 	struct mhzc_softc *xsc = device_private(device_parent(&sc->sc_dev));
    724   1.1   thorpej 
    725  1.47    dyoung 	mhzc_disable(xsc, MHZC_ETHERNET_ENABLED);
    726   1.1   thorpej }
    727   1.1   thorpej 
    728   1.1   thorpej #endif /* NSM_MHZC > 0 */
    729