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