Home | History | Annotate | Line # | Download | only in cardbus
if_fxp_cardbus.c revision 1.21.4.1
      1 /*	$NetBSD: if_fxp_cardbus.c,v 1.21.4.1 2006/09/09 02:49:44 rpaulo 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.21.4.1 2006/09/09 02:49:44 rpaulo Exp $");
     45 
     46 #include "opt_inet.h"
     47 #include "bpfilter.h"
     48 #include "rnd.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/malloc.h>
     54 #include <sys/kernel.h>
     55 #include <sys/socket.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/errno.h>
     58 #include <sys/device.h>
     59 
     60 #if NRND > 0
     61 #include <sys/rnd.h>
     62 #endif
     63 
     64 #include <net/if.h>
     65 #include <net/if_dl.h>
     66 #include <net/if_media.h>
     67 #include <net/if_ether.h>
     68 
     69 #include <machine/endian.h>
     70 
     71 #if NBPFILTER > 0
     72 #include <net/bpf.h>
     73 #endif
     74 
     75 #ifdef INET
     76 #include <netinet/in.h>
     77 #include <netinet/if_inarp.h>
     78 #endif
     79 
     80 
     81 #include <machine/bus.h>
     82 #include <machine/intr.h>
     83 
     84 #include <dev/mii/miivar.h>
     85 
     86 #include <dev/ic/i82557reg.h>
     87 #include <dev/ic/i82557var.h>
     88 
     89 #include <dev/pci/pcivar.h>
     90 #include <dev/pci/pcireg.h>
     91 #include <dev/pci/pcidevs.h>
     92 
     93 #include <dev/cardbus/cardbusvar.h>
     94 #include <dev/pci/pcidevs.h>
     95 
     96 static int fxp_cardbus_match(struct device *, struct cfdata *, void *);
     97 static void fxp_cardbus_attach(struct device *, struct device *, void *);
     98 static int fxp_cardbus_detach(struct device * self, int flags);
     99 static void fxp_cardbus_setup(struct fxp_softc * sc);
    100 static int fxp_cardbus_enable(struct fxp_softc * sc);
    101 static void fxp_cardbus_disable(struct fxp_softc * sc);
    102 
    103 struct fxp_cardbus_softc {
    104 	struct fxp_softc sc;
    105 	cardbus_devfunc_t ct;
    106 	pcireg_t base0_reg;
    107 	pcireg_t base1_reg;
    108 	bus_size_t size;
    109 };
    110 
    111 CFATTACH_DECL(fxp_cardbus, sizeof(struct fxp_cardbus_softc),
    112     fxp_cardbus_match, fxp_cardbus_attach, fxp_cardbus_detach, fxp_activate);
    113 
    114 #ifdef CBB_DEBUG
    115 #define DPRINTF(X) printf X
    116 #else
    117 #define DPRINTF(X)
    118 #endif
    119 
    120 static int
    121 fxp_cardbus_match(parent, match, aux)
    122 	struct device *parent;
    123 	struct cfdata *match;
    124 	void   *aux;
    125 {
    126 	struct cardbus_attach_args *ca = aux;
    127 
    128 	if (CARDBUS_VENDOR(ca->ca_id) == PCI_VENDOR_INTEL &&
    129 	    CARDBUS_PRODUCT(ca->ca_id) == PCI_PRODUCT_INTEL_82557)
    130 		return (1);
    131 
    132 	return (0);
    133 }
    134 
    135 static void
    136 fxp_cardbus_attach(parent, self, aux)
    137 	struct device *parent, *self;
    138 	void *aux;
    139 {
    140 	static const char thisfunc[] = "fxp_cardbus_attach";
    141 
    142 	struct fxp_softc *sc = device_private(self);
    143 	struct fxp_cardbus_softc *csc = device_private(self);
    144 	struct cardbus_attach_args *ca = aux;
    145 	bus_space_tag_t iot, memt;
    146 	bus_space_handle_t ioh, memh;
    147 
    148 	bus_addr_t adr;
    149 	bus_size_t size;
    150 
    151 	csc->ct = ca->ca_ct;
    152 
    153 	/*
    154          * Map control/status registers.
    155          */
    156 	if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
    157 	    PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &size) == 0) {
    158 		csc->base1_reg = adr | 1;
    159 		sc->sc_st = iot;
    160 		sc->sc_sh = ioh;
    161 		csc->size = size;
    162 	} else if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
    163 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    164 	    0, &memt, &memh, &adr, &size) == 0) {
    165 		csc->base0_reg = adr;
    166 		sc->sc_st = memt;
    167 		sc->sc_sh = memh;
    168 		csc->size = size;
    169 	} else
    170 		panic("%s: failed to allocate mem and io space", thisfunc);
    171 
    172 
    173 	if (ca->ca_cis.cis1_info[0] && ca->ca_cis.cis1_info[1])
    174 		printf(": %s %s\n", ca->ca_cis.cis1_info[0],
    175 		    ca->ca_cis.cis1_info[1]);
    176 	else
    177 		printf("\n");
    178 
    179 	sc->sc_dmat = ca->ca_dmat;
    180 	sc->sc_enable = fxp_cardbus_enable;
    181 	sc->sc_disable = fxp_cardbus_disable;
    182 	sc->sc_enabled = 0;
    183 
    184 	fxp_enable(sc);
    185 	fxp_attach(sc);
    186 	fxp_disable(sc);
    187 }
    188 
    189 static void
    190 fxp_cardbus_setup(struct fxp_softc * sc)
    191 {
    192 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
    193 	struct cardbus_softc *psc =
    194 	    (struct cardbus_softc *) device_parent(&sc->sc_dev);
    195 	cardbus_chipset_tag_t cc = psc->sc_cc;
    196 	cardbus_function_tag_t cf = psc->sc_cf;
    197 	pcireg_t command;
    198 
    199 	cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
    200 	    csc->ct->ct_func);
    201 
    202 	command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
    203 	if (csc->base0_reg) {
    204 		Cardbus_conf_write(csc->ct, tag,
    205 		    CARDBUS_BASE0_REG, csc->base0_reg);
    206 		(cf->cardbus_ctrl) (cc, CARDBUS_MEM_ENABLE);
    207 		command |= CARDBUS_COMMAND_MEM_ENABLE |
    208 		    CARDBUS_COMMAND_MASTER_ENABLE;
    209 	} else if (csc->base1_reg) {
    210 		Cardbus_conf_write(csc->ct, tag,
    211 		    CARDBUS_BASE1_REG, csc->base1_reg);
    212 		(cf->cardbus_ctrl) (cc, CARDBUS_IO_ENABLE);
    213 		command |= (CARDBUS_COMMAND_IO_ENABLE |
    214 		    CARDBUS_COMMAND_MASTER_ENABLE);
    215 	}
    216 
    217 	(cf->cardbus_ctrl) (cc, CARDBUS_BM_ENABLE);
    218 
    219 	/* enable the card */
    220 	Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
    221 }
    222 
    223 static int
    224 fxp_cardbus_enable(struct fxp_softc * sc)
    225 {
    226 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
    227 	struct cardbus_softc *psc =
    228 	    (struct cardbus_softc *) device_parent(&sc->sc_dev);
    229 	cardbus_chipset_tag_t cc = psc->sc_cc;
    230 	cardbus_function_tag_t cf = psc->sc_cf;
    231 
    232 	Cardbus_function_enable(csc->ct);
    233 
    234 	fxp_cardbus_setup(sc);
    235 
    236 	/* Map and establish the interrupt. */
    237 
    238 	sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
    239 	    fxp_intr, sc);
    240 	if (NULL == sc->sc_ih) {
    241 		printf("%s: couldn't establish interrupt\n",
    242 		    sc->sc_dev.dv_xname);
    243 		return 1;
    244 	}
    245 
    246 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    247 	    psc->sc_intrline);
    248 
    249 	return 0;
    250 }
    251 
    252 static void
    253 fxp_cardbus_disable(struct fxp_softc * sc)
    254 {
    255 	struct cardbus_softc *psc =
    256 	    (struct cardbus_softc *) device_parent(&sc->sc_dev);
    257 	cardbus_chipset_tag_t cc = psc->sc_cc;
    258 	cardbus_function_tag_t cf = psc->sc_cf;
    259 
    260 	/* Remove interrupt handler. */
    261 	cardbus_intr_disestablish(cc, cf, sc->sc_ih);
    262 
    263 	Cardbus_function_disable(((struct fxp_cardbus_softc *) sc)->ct);
    264 }
    265 
    266 static int
    267 fxp_cardbus_detach(self, flags)
    268 	struct device *self;
    269 	int flags;
    270 {
    271 	struct fxp_softc *sc = device_private(self);
    272 	struct fxp_cardbus_softc *csc = device_private(self);
    273 	struct cardbus_devfunc *ct = csc->ct;
    274 	int rv, reg;
    275 
    276 #ifdef DIAGNOSTIC
    277 	if (ct == NULL)
    278 		panic("%s: data structure lacks", sc->sc_dev.dv_xname);
    279 #endif
    280 
    281 	rv = fxp_detach(sc);
    282 	if (rv == 0) {
    283 		/*
    284 		 * Unhook the interrupt handler.
    285 		 */
    286 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
    287 
    288 		/*
    289 		 * release bus space and close window
    290 		 */
    291 		if (csc->base0_reg)
    292 			reg = CARDBUS_BASE0_REG;
    293 		else
    294 			reg = CARDBUS_BASE1_REG;
    295 		Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, csc->size);
    296 	}
    297 	return (rv);
    298 }
    299