Home | History | Annotate | Line # | Download | only in cardbus
if_fxp_cardbus.c revision 1.5
      1 /*	$NetBSD: if_fxp_cardbus.c,v 1.5 1999/12/12 17:46:37 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to
      8  * The NetBSD Foundation by Johan Danielsson.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  *
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  *
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * 3. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 
     39 #include "opt_inet.h"
     40 #include "opt_ns.h"
     41 #include "bpfilter.h"
     42 #include "rnd.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/malloc.h>
     48 #include <sys/kernel.h>
     49 #include <sys/socket.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/errno.h>
     52 #include <sys/device.h>
     53 
     54 #if NRND > 0
     55 #include <sys/rnd.h>
     56 #endif
     57 
     58 #include <net/if.h>
     59 #include <net/if_dl.h>
     60 #include <net/if_media.h>
     61 #include <net/if_ether.h>
     62 
     63 #include <machine/endian.h>
     64 
     65 #if NBPFILTER > 0
     66 #include <net/bpf.h>
     67 #endif
     68 
     69 #ifdef INET
     70 #include <netinet/in.h>
     71 #include <netinet/if_inarp.h>
     72 #endif
     73 
     74 #ifdef NS
     75 #include <netns/ns.h>
     76 #include <netns/ns_if.h>
     77 #endif
     78 
     79 #include <machine/bus.h>
     80 #include <machine/intr.h>
     81 
     82 #include <dev/mii/miivar.h>
     83 
     84 #include <dev/ic/i82557reg.h>
     85 #include <dev/ic/i82557var.h>
     86 
     87 #include <dev/pci/pcivar.h>
     88 #include <dev/pci/pcireg.h>
     89 #include <dev/pci/pcidevs.h>
     90 
     91 #include <dev/cardbus/cardbusvar.h>
     92 #include <dev/cardbus/cardbusdevs.h>
     93 
     94 static int fxp_cardbus_match __P((struct device *, struct cfdata *, void *));
     95 static void fxp_cardbus_attach __P((struct device *, struct device *, void *));
     96 static void fxp_cardbus_setup __P((struct fxp_softc *sc));
     97 static int fxp_cardbus_enable __P((struct fxp_softc *sc));
     98 static void fxp_cardbus_disable __P((struct fxp_softc *sc));
     99 
    100 struct fxp_cardbus_softc {
    101     struct fxp_softc sc;
    102     cardbus_devfunc_t ct;
    103     pcireg_t base0_reg;
    104     pcireg_t base1_reg;
    105     bus_size_t size;
    106 };
    107 
    108 struct cfattach fxp_cardbus_ca = {
    109     sizeof(struct fxp_cardbus_softc), fxp_cardbus_match, fxp_cardbus_attach
    110 };
    111 
    112 #ifdef CBB_DEBUG
    113 #define DPRINTF(X) printf X
    114 #else
    115 #define DPRINTF(X)
    116 #endif
    117 
    118 static int
    119 fxp_cardbus_match(parent, match, aux)
    120      struct device *parent;
    121      struct cfdata *match;
    122      void *aux;
    123 {
    124     /* should check CIS */
    125     struct cardbus_attach_args *ca = aux;
    126 
    127     if (CARDBUS_VENDOR(ca->ca_id) != CARDBUS_VENDOR_INTEL)
    128 	return (0);
    129 
    130     switch (CARDBUS_PRODUCT(ca->ca_id)) {
    131     case CARDBUS_PRODUCT_INTEL_82557:
    132 	return (1);
    133     }
    134 
    135     return (0);
    136 }
    137 
    138 static void
    139 fxp_cardbus_attach(parent, self, aux)
    140      struct device *parent, *self;
    141      void *aux;
    142 {
    143     struct fxp_softc *sc = (struct fxp_softc*)self;
    144     struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc*)self;
    145     struct cardbus_attach_args *ca = aux;
    146     bus_space_tag_t iot, memt;
    147     bus_space_handle_t ioh, memh;
    148 
    149     bus_addr_t adr;
    150     bus_size_t size;
    151 
    152     csc->ct = ca->ca_ct;
    153 
    154     /*
    155      * Map control/status registers.
    156      */
    157     if(Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
    158 			  PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    159 			  0, &memt, &memh, &adr, &size) == 0) {
    160 	csc->base0_reg = adr;
    161 	sc->sc_st = memt;
    162 	sc->sc_sh = memh;
    163 	csc->size = size;
    164     } else if(Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
    165 				 PCI_MAPREG_TYPE_IO,
    166 				 0, &iot, &ioh, &adr, &size) == 0) {
    167 	csc->base1_reg = adr | 1;
    168 	sc->sc_st = iot;
    169 	sc->sc_sh = ioh;
    170 	csc->size = size;
    171     } else
    172 	panic("%s: failed to allocate mem and io space", __FUNCTION__);
    173 
    174 
    175     sc->sc_dmat = ca->ca_dmat;
    176     sc->sc_enable = fxp_cardbus_enable;
    177     sc->sc_disable = fxp_cardbus_disable;
    178     sc->sc_enabled = 0;
    179 
    180     fxp_enable(sc);
    181     fxp_attach(sc);
    182     fxp_disable(sc);
    183 }
    184 
    185 
    186 static void
    187 fxp_cardbus_setup(struct fxp_softc *sc)
    188 {
    189     struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc*)sc;
    190     struct cardbus_softc *psc = (struct cardbus_softc *)sc->sc_dev.dv_parent;
    191     cardbus_chipset_tag_t cc = psc->sc_cc;
    192     cardbus_function_tag_t cf = psc->sc_cf;
    193     pcireg_t command;
    194 
    195     cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
    196 					csc->ct->ct_dev, csc->ct->ct_func);
    197 
    198     command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
    199     if(csc->base0_reg) {
    200 	Cardbus_conf_write(csc->ct, tag,
    201 			   CARDBUS_BASE0_REG, csc->base0_reg);
    202 	(cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    203 	command |= CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE;
    204 
    205     } else if(csc->base1_reg) {
    206 	Cardbus_conf_write(csc->ct, tag,
    207 			   CARDBUS_BASE1_REG, csc->base1_reg);
    208 	(cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
    209 	command |= (CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE);
    210     }
    211     /* enable the card */
    212     Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
    213 }
    214 
    215 static int
    216 fxp_cardbus_enable(struct fxp_softc *sc)
    217 {
    218     struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc*)sc;
    219     struct cardbus_softc *psc = (struct cardbus_softc *)sc->sc_dev.dv_parent;
    220     cardbus_chipset_tag_t cc = psc->sc_cc;
    221     cardbus_function_tag_t cf = psc->sc_cf;
    222 
    223     Cardbus_function_enable(csc->ct);
    224 
    225     fxp_cardbus_setup(sc);
    226 
    227     /* Map and establish the interrupt. */
    228 
    229     sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
    230 				       fxp_intr, sc);
    231     if (NULL == sc->sc_ih) {
    232 	printf("%s: couldn't establish interrupt\n", sc->sc_dev.dv_xname);
    233 	return 1;
    234     }
    235 
    236     printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname, psc->sc_intrline);
    237 
    238     return 0;
    239 }
    240 
    241 static void
    242 fxp_cardbus_disable(struct fxp_softc *sc)
    243 {
    244     struct cardbus_softc *psc = (struct cardbus_softc *)sc->sc_dev.dv_parent;
    245     cardbus_chipset_tag_t cc = psc->sc_cc;
    246     cardbus_function_tag_t cf = psc->sc_cf;
    247 
    248     cardbus_intr_disestablish(cc, cf, sc->sc_ih); /* remove intr handler */
    249 
    250     Cardbus_function_disable(((struct fxp_cardbus_softc*)sc)->ct);
    251 }
    252