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