Home | History | Annotate | Line # | Download | only in cardbus
if_fxp_cardbus.c revision 1.17
      1 /*	$NetBSD: if_fxp_cardbus.c,v 1.17 2002/10/02 16:33:42 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 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.17 2002/10/02 16:33:42 thorpej 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 CFATTACH_DECL(fxp_cardbus, sizeof(struct fxp_cardbus_softc),
    117     fxp_cardbus_match, fxp_cardbus_attach, fxp_cardbus_detach, fxp_activate);
    118 
    119 #ifdef CBB_DEBUG
    120 #define DPRINTF(X) printf X
    121 #else
    122 #define DPRINTF(X)
    123 #endif
    124 
    125 static int
    126 fxp_cardbus_match(parent, match, aux)
    127 	struct device *parent;
    128 	struct cfdata *match;
    129 	void   *aux;
    130 {
    131 	struct cardbus_attach_args *ca = aux;
    132 
    133 	if (CARDBUS_VENDOR(ca->ca_id) == CARDBUS_VENDOR_INTEL &&
    134 	    CARDBUS_PRODUCT(ca->ca_id) == CARDBUS_PRODUCT_INTEL_82557)
    135 		return (1);
    136 
    137 	return (0);
    138 }
    139 
    140 static void
    141 fxp_cardbus_attach(parent, self, aux)
    142 	struct device *parent, *self;
    143 	void *aux;
    144 {
    145 	static const char thisfunc[] = "fxp_cardbus_attach";
    146 
    147 	struct fxp_softc *sc = (struct fxp_softc *) self;
    148 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self;
    149 	struct cardbus_attach_args *ca = aux;
    150 	bus_space_tag_t iot, memt;
    151 	bus_space_handle_t ioh, memh;
    152 
    153 	bus_addr_t adr;
    154 	bus_size_t size;
    155 
    156 	csc->ct = ca->ca_ct;
    157 
    158 	/*
    159          * Map control/status registers.
    160          */
    161 	if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
    162 	    PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &size) == 0) {
    163 		csc->base1_reg = adr | 1;
    164 		sc->sc_st = iot;
    165 		sc->sc_sh = ioh;
    166 		csc->size = size;
    167 	} else if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
    168 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    169 	    0, &memt, &memh, &adr, &size) == 0) {
    170 		csc->base0_reg = adr;
    171 		sc->sc_st = memt;
    172 		sc->sc_sh = memh;
    173 		csc->size = size;
    174 	} else
    175 		panic("%s: failed to allocate mem and io space", thisfunc);
    176 
    177 
    178 	if (ca->ca_cis.cis1_info[0] && ca->ca_cis.cis1_info[1])
    179 		printf(": %s %s\n", ca->ca_cis.cis1_info[0],
    180 		    ca->ca_cis.cis1_info[1]);
    181 	else
    182 		printf("\n");
    183 
    184 	sc->sc_dmat = ca->ca_dmat;
    185 	sc->sc_enable = fxp_cardbus_enable;
    186 	sc->sc_disable = fxp_cardbus_disable;
    187 	sc->sc_enabled = 0;
    188 
    189 	fxp_enable(sc);
    190 	fxp_attach(sc);
    191 	fxp_disable(sc);
    192 }
    193 
    194 static void
    195 fxp_cardbus_setup(struct fxp_softc * sc)
    196 {
    197 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
    198 	struct cardbus_softc *psc =
    199 	    (struct cardbus_softc *) sc->sc_dev.dv_parent;
    200 	cardbus_chipset_tag_t cc = psc->sc_cc;
    201 	cardbus_function_tag_t cf = psc->sc_cf;
    202 	pcireg_t command;
    203 
    204 	cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
    205 	    csc->ct->ct_dev, csc->ct->ct_func);
    206 
    207 	command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
    208 	if (csc->base0_reg) {
    209 		Cardbus_conf_write(csc->ct, tag,
    210 		    CARDBUS_BASE0_REG, csc->base0_reg);
    211 		(cf->cardbus_ctrl) (cc, CARDBUS_MEM_ENABLE);
    212 		command |= CARDBUS_COMMAND_MEM_ENABLE |
    213 		    CARDBUS_COMMAND_MASTER_ENABLE;
    214 	} else if (csc->base1_reg) {
    215 		Cardbus_conf_write(csc->ct, tag,
    216 		    CARDBUS_BASE1_REG, csc->base1_reg);
    217 		(cf->cardbus_ctrl) (cc, CARDBUS_IO_ENABLE);
    218 		command |= (CARDBUS_COMMAND_IO_ENABLE |
    219 		    CARDBUS_COMMAND_MASTER_ENABLE);
    220 	}
    221 
    222 	(cf->cardbus_ctrl) (cc, CARDBUS_BM_ENABLE);
    223 
    224 	/* enable the card */
    225 	Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
    226 }
    227 
    228 static int
    229 fxp_cardbus_enable(struct fxp_softc * sc)
    230 {
    231 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
    232 	struct cardbus_softc *psc =
    233 	    (struct cardbus_softc *) sc->sc_dev.dv_parent;
    234 	cardbus_chipset_tag_t cc = psc->sc_cc;
    235 	cardbus_function_tag_t cf = psc->sc_cf;
    236 
    237 	Cardbus_function_enable(csc->ct);
    238 
    239 	fxp_cardbus_setup(sc);
    240 
    241 	/* Map and establish the interrupt. */
    242 
    243 	sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
    244 	    fxp_intr, sc);
    245 	if (NULL == sc->sc_ih) {
    246 		printf("%s: couldn't establish interrupt\n",
    247 		    sc->sc_dev.dv_xname);
    248 		return 1;
    249 	}
    250 
    251 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    252 	    psc->sc_intrline);
    253 
    254 	return 0;
    255 }
    256 
    257 static void
    258 fxp_cardbus_disable(struct fxp_softc * sc)
    259 {
    260 	struct cardbus_softc *psc =
    261 	    (struct cardbus_softc *) sc->sc_dev.dv_parent;
    262 	cardbus_chipset_tag_t cc = psc->sc_cc;
    263 	cardbus_function_tag_t cf = psc->sc_cf;
    264 
    265 	/* Remove interrupt handler. */
    266 	cardbus_intr_disestablish(cc, cf, sc->sc_ih);
    267 
    268 	Cardbus_function_disable(((struct fxp_cardbus_softc *) sc)->ct);
    269 }
    270 
    271 static int
    272 fxp_cardbus_detach(self, flags)
    273 	struct device *self;
    274 	int flags;
    275 {
    276 	struct fxp_softc *sc = (struct fxp_softc *) self;
    277 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self;
    278 	struct cardbus_devfunc *ct = csc->ct;
    279 	int rv, reg;
    280 
    281 #ifdef DIAGNOSTIC
    282 	if (ct == NULL)
    283 		panic("%s: data structure lacks", sc->sc_dev.dv_xname);
    284 #endif
    285 
    286 	rv = fxp_detach(sc);
    287 	if (rv == 0) {
    288 		/*
    289 		 * Unhook the interrupt handler.
    290 		 */
    291 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
    292 
    293 		/*
    294 		 * release bus space and close window
    295 		 */
    296 		if (csc->base0_reg)
    297 			reg = CARDBUS_BASE0_REG;
    298 		else
    299 			reg = CARDBUS_BASE1_REG;
    300 		Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, csc->size);
    301 	}
    302 	return (rv);
    303 }
    304