Home | History | Annotate | Line # | Download | only in cardbus
if_fxp_cardbus.c revision 1.45
      1 /*	$NetBSD: if_fxp_cardbus.c,v 1.45 2010/02/25 23:40:39 dyoung 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * CardBus front-end for the Intel i82557 family of Ethernet chips.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: if_fxp_cardbus.c,v 1.45 2010/02/25 23:40:39 dyoung Exp $");
     38 
     39 #include "opt_inet.h"
     40 #include "rnd.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/malloc.h>
     46 #include <sys/kernel.h>
     47 #include <sys/socket.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/errno.h>
     50 #include <sys/device.h>
     51 
     52 #if NRND > 0
     53 #include <sys/rnd.h>
     54 #endif
     55 
     56 #include <net/if.h>
     57 #include <net/if_dl.h>
     58 #include <net/if_media.h>
     59 #include <net/if_ether.h>
     60 
     61 #include <machine/endian.h>
     62 
     63 #ifdef INET
     64 #include <netinet/in.h>
     65 #include <netinet/if_inarp.h>
     66 #endif
     67 
     68 
     69 #include <sys/bus.h>
     70 #include <sys/intr.h>
     71 
     72 #include <dev/mii/miivar.h>
     73 
     74 #include <dev/ic/i82557reg.h>
     75 #include <dev/ic/i82557var.h>
     76 
     77 #include <dev/pci/pcivar.h>
     78 #include <dev/pci/pcireg.h>
     79 #include <dev/pci/pcidevs.h>
     80 
     81 #include <dev/cardbus/cardbusvar.h>
     82 #include <dev/pci/pcidevs.h>
     83 
     84 static int fxp_cardbus_match(device_t, cfdata_t, void *);
     85 static void fxp_cardbus_attach(device_t, device_t, void *);
     86 static int fxp_cardbus_detach(device_t, int);
     87 static void fxp_cardbus_setup(struct fxp_softc *);
     88 static int fxp_cardbus_enable(struct fxp_softc *);
     89 static void fxp_cardbus_disable(struct fxp_softc *);
     90 
     91 struct fxp_cardbus_softc {
     92 	struct fxp_softc sc;
     93 	cardbus_devfunc_t ct;
     94 	cardbus_intr_line_t intrline;
     95 	pcitag_t tag;
     96 	pcireg_t base0_reg;
     97 	pcireg_t base1_reg;
     98 };
     99 
    100 CFATTACH_DECL3_NEW(fxp_cardbus, sizeof(struct fxp_cardbus_softc),
    101     fxp_cardbus_match, fxp_cardbus_attach, fxp_cardbus_detach, fxp_activate,
    102     NULL, null_childdetached, DVF_DETACH_SHUTDOWN);
    103 
    104 #ifdef CBB_DEBUG
    105 #define DPRINTF(X) printf X
    106 #else
    107 #define DPRINTF(X)
    108 #endif
    109 
    110 static int
    111 fxp_cardbus_match(device_t parent, cfdata_t match,
    112     void *aux)
    113 {
    114 	struct cardbus_attach_args *ca = aux;
    115 
    116 	if (CARDBUS_VENDOR(ca->ca_id) == PCI_VENDOR_INTEL &&
    117 	    CARDBUS_PRODUCT(ca->ca_id) == PCI_PRODUCT_INTEL_82557)
    118 		return (1);
    119 
    120 	return (0);
    121 }
    122 
    123 static void
    124 fxp_cardbus_attach(device_t parent, device_t self,
    125     void *aux)
    126 {
    127 	struct fxp_cardbus_softc *csc = device_private(self);
    128 	struct fxp_softc *sc = &csc->sc;
    129 	struct cardbus_attach_args *ca = aux;
    130 	bus_space_tag_t iot, memt;
    131 	bus_space_handle_t ioh, memh;
    132 
    133 	bus_addr_t adr;
    134 
    135 	sc->sc_dev = self;
    136 	csc->intrline = ca->ca_intrline;
    137 	csc->ct = ca->ca_ct;
    138 	csc->tag = ca->ca_tag;
    139 
    140 	/*
    141          * Map control/status registers.
    142          */
    143 	if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
    144 	    PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &sc->sc_size) == 0) {
    145 		csc->base1_reg = adr | 1;
    146 		sc->sc_st = iot;
    147 		sc->sc_sh = ioh;
    148 	} else if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
    149 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    150 	    0, &memt, &memh, &adr, &sc->sc_size) == 0) {
    151 		csc->base0_reg = adr;
    152 		sc->sc_st = memt;
    153 		sc->sc_sh = memh;
    154 	} else
    155 		panic("%s: failed to allocate mem and io space", __func__);
    156 
    157 	if (ca->ca_cis.cis1_info[0] && ca->ca_cis.cis1_info[1])
    158 		printf(": %s %s\n", ca->ca_cis.cis1_info[0],
    159 		    ca->ca_cis.cis1_info[1]);
    160 	else
    161 		printf("\n");
    162 
    163 	sc->sc_rev = CARDBUS_REVISION(ca->ca_class);
    164 	if (sc->sc_rev >= FXP_REV_82558_A4)
    165 		sc->sc_flags |= FXPF_FC|FXPF_EXT_TXCB;
    166 	if (sc->sc_rev >= FXP_REV_82559_A0)
    167 		sc->sc_flags |= FXPF_82559_RXCSUM;
    168 	if (sc->sc_rev >= FXP_REV_82550) {
    169 		sc->sc_flags &= ~FXPF_82559_RXCSUM;
    170 		sc->sc_flags |= FXPF_EXT_RFA;
    171 	}
    172 
    173 	sc->sc_dmat = ca->ca_dmat;
    174 	sc->sc_enable = fxp_cardbus_enable;
    175 	sc->sc_disable = fxp_cardbus_disable;
    176 	sc->sc_enabled = 0;
    177 
    178 	fxp_enable(sc);
    179 	fxp_attach(sc);
    180 	fxp_disable(sc);
    181 
    182 	if (pmf_device_register(self, NULL, NULL))
    183 		pmf_class_network_register(self, &sc->sc_ethercom.ec_if);
    184 	else
    185 		aprint_error_dev(self, "couldn't establish power handler\n");
    186 }
    187 
    188 static void
    189 fxp_cardbus_setup(struct fxp_softc * sc)
    190 {
    191 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *)sc;
    192 	pcireg_t command;
    193 
    194 	pcitag_t tag = csc->tag;
    195 
    196 	command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
    197 	if (csc->base0_reg) {
    198 		Cardbus_conf_write(csc->ct, tag,
    199 		    CARDBUS_BASE0_REG, csc->base0_reg);
    200 		command |= CARDBUS_COMMAND_MEM_ENABLE |
    201 		    CARDBUS_COMMAND_MASTER_ENABLE;
    202 	} else if (csc->base1_reg) {
    203 		Cardbus_conf_write(csc->ct, tag,
    204 		    CARDBUS_BASE1_REG, csc->base1_reg);
    205 		command |= (CARDBUS_COMMAND_IO_ENABLE |
    206 		    CARDBUS_COMMAND_MASTER_ENABLE);
    207 	}
    208 
    209 	/* enable the card */
    210 	Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
    211 }
    212 
    213 static int
    214 fxp_cardbus_enable(struct fxp_softc * sc)
    215 {
    216 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *)sc;
    217 	cardbus_chipset_tag_t cc = csc->ct->ct_cc;
    218 	cardbus_function_tag_t cf = csc->ct->ct_cf;
    219 
    220 	Cardbus_function_enable(csc->ct);
    221 
    222 	fxp_cardbus_setup(sc);
    223 
    224 	/* Map and establish the interrupt. */
    225 
    226 	sc->sc_ih = cardbus_intr_establish(cc, cf, csc->intrline, IPL_NET,
    227 	    fxp_intr, sc);
    228 	if (NULL == sc->sc_ih) {
    229 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt\n");
    230 		return 1;
    231 	}
    232 
    233 	return 0;
    234 }
    235 
    236 static void
    237 fxp_cardbus_disable(struct fxp_softc * sc)
    238 {
    239 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *)sc;
    240 	struct cardbus_devfunc *ct = csc->ct;
    241 	cardbus_chipset_tag_t cc = ct->ct_cc;
    242 	cardbus_function_tag_t cf = ct->ct_cf;
    243 
    244 	/* Remove interrupt handler. */
    245 	cardbus_intr_disestablish(cc, cf, sc->sc_ih);
    246 
    247 	Cardbus_function_disable(csc->ct);
    248 }
    249 
    250 static int
    251 fxp_cardbus_detach(device_t self, int flags)
    252 {
    253 	struct fxp_cardbus_softc *csc = device_private(self);
    254 	struct fxp_softc *sc = &csc->sc;
    255 	struct cardbus_devfunc *ct = csc->ct;
    256 	int rv, reg;
    257 
    258 #ifdef DIAGNOSTIC
    259 	if (ct == NULL)
    260 		panic("%s: data structure lacks", device_xname(self));
    261 #endif
    262 
    263 	if ((rv = fxp_detach(sc, flags)) != 0)
    264 		return rv;
    265 	/*
    266 	 * Unhook the interrupt handler.
    267 	 */
    268 	cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
    269 
    270 	/*
    271 	 * release bus space and close window
    272 	 */
    273 	if (csc->base0_reg)
    274 		reg = CARDBUS_BASE0_REG;
    275 	else
    276 		reg = CARDBUS_BASE1_REG;
    277 	Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, sc->sc_size);
    278 	return 0;
    279 }
    280