Home | History | Annotate | Line # | Download | only in cardbus
if_fxp_cardbus.c revision 1.50
      1 /*	$NetBSD: if_fxp_cardbus.c,v 1.50 2012/02/02 19:43:02 tls 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.50 2012/02/02 19:43:02 tls Exp $");
     38 
     39 #include "opt_inet.h"
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/mbuf.h>
     44 #include <sys/malloc.h>
     45 #include <sys/kernel.h>
     46 #include <sys/socket.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/errno.h>
     49 #include <sys/device.h>
     50 
     51 #include <sys/rnd.h>
     52 
     53 #include <net/if.h>
     54 #include <net/if_dl.h>
     55 #include <net/if_media.h>
     56 #include <net/if_ether.h>
     57 
     58 #include <machine/endian.h>
     59 
     60 #ifdef INET
     61 #include <netinet/in.h>
     62 #include <netinet/if_inarp.h>
     63 #endif
     64 
     65 
     66 #include <sys/bus.h>
     67 #include <sys/intr.h>
     68 
     69 #include <dev/mii/miivar.h>
     70 
     71 #include <dev/ic/i82557reg.h>
     72 #include <dev/ic/i82557var.h>
     73 
     74 #include <dev/pci/pcivar.h>
     75 #include <dev/pci/pcireg.h>
     76 #include <dev/pci/pcidevs.h>
     77 
     78 #include <dev/cardbus/cardbusvar.h>
     79 #include <dev/pci/pcidevs.h>
     80 
     81 static int fxp_cardbus_match(device_t, cfdata_t, void *);
     82 static void fxp_cardbus_attach(device_t, device_t, void *);
     83 static int fxp_cardbus_detach(device_t, int);
     84 static void fxp_cardbus_setup(struct fxp_softc *);
     85 static int fxp_cardbus_enable(struct fxp_softc *);
     86 static void fxp_cardbus_disable(struct fxp_softc *);
     87 
     88 struct fxp_cardbus_softc {
     89 	struct fxp_softc sc;
     90 	cardbus_devfunc_t ct;
     91 	pcitag_t tag;
     92 	pcireg_t base0_reg;
     93 	pcireg_t base1_reg;
     94 };
     95 
     96 CFATTACH_DECL3_NEW(fxp_cardbus, sizeof(struct fxp_cardbus_softc),
     97     fxp_cardbus_match, fxp_cardbus_attach, fxp_cardbus_detach, fxp_activate,
     98     NULL, null_childdetached, DVF_DETACH_SHUTDOWN);
     99 
    100 #ifdef CBB_DEBUG
    101 #define DPRINTF(X) printf X
    102 #else
    103 #define DPRINTF(X)
    104 #endif
    105 
    106 static int
    107 fxp_cardbus_match(device_t parent, cfdata_t match,
    108     void *aux)
    109 {
    110 	struct cardbus_attach_args *ca = aux;
    111 
    112 	if (PCI_VENDOR(ca->ca_id) == PCI_VENDOR_INTEL &&
    113 	    PCI_PRODUCT(ca->ca_id) == PCI_PRODUCT_INTEL_8255X)
    114 		return (1);
    115 
    116 	return (0);
    117 }
    118 
    119 static void
    120 fxp_cardbus_attach(device_t parent, device_t self,
    121     void *aux)
    122 {
    123 	struct fxp_cardbus_softc *csc = device_private(self);
    124 	struct fxp_softc *sc = &csc->sc;
    125 	struct cardbus_attach_args *ca = aux;
    126 	bus_space_tag_t iot, memt;
    127 	bus_space_handle_t ioh, memh;
    128 
    129 	bus_addr_t adr;
    130 
    131 	sc->sc_dev = self;
    132 	csc->ct = ca->ca_ct;
    133 	csc->tag = ca->ca_tag;
    134 
    135 	/*
    136          * Map control/status registers.
    137          */
    138 	if (Cardbus_mapreg_map(csc->ct, PCI_BAR1,
    139 	    PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &sc->sc_size) == 0) {
    140 		csc->base1_reg = adr | 1;
    141 		sc->sc_st = iot;
    142 		sc->sc_sh = ioh;
    143 	} else if (Cardbus_mapreg_map(csc->ct, PCI_BAR0,
    144 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    145 	    0, &memt, &memh, &adr, &sc->sc_size) == 0) {
    146 		csc->base0_reg = adr;
    147 		sc->sc_st = memt;
    148 		sc->sc_sh = memh;
    149 	} else
    150 		panic("%s: failed to allocate mem and io space", __func__);
    151 
    152 	if (ca->ca_cis.cis1_info[0] && ca->ca_cis.cis1_info[1])
    153 		printf(": %s %s\n", ca->ca_cis.cis1_info[0],
    154 		    ca->ca_cis.cis1_info[1]);
    155 	else
    156 		printf("\n");
    157 
    158 	sc->sc_rev = PCI_REVISION(ca->ca_class);
    159 	if (sc->sc_rev >= FXP_REV_82558_A4)
    160 		sc->sc_flags |= FXPF_FC|FXPF_EXT_TXCB;
    161 	if (sc->sc_rev >= FXP_REV_82559_A0)
    162 		sc->sc_flags |= FXPF_82559_RXCSUM;
    163 	if (sc->sc_rev >= FXP_REV_82550) {
    164 		sc->sc_flags &= ~FXPF_82559_RXCSUM;
    165 		sc->sc_flags |= FXPF_EXT_RFA;
    166 	}
    167 
    168 	sc->sc_dmat = ca->ca_dmat;
    169 	sc->sc_enable = fxp_cardbus_enable;
    170 	sc->sc_disable = fxp_cardbus_disable;
    171 	sc->sc_enabled = 0;
    172 
    173 	fxp_enable(sc);
    174 	fxp_attach(sc);
    175 	fxp_disable(sc);
    176 
    177 	if (pmf_device_register(self, NULL, NULL))
    178 		pmf_class_network_register(self, &sc->sc_ethercom.ec_if);
    179 	else
    180 		aprint_error_dev(self, "couldn't establish power handler\n");
    181 }
    182 
    183 static void
    184 fxp_cardbus_setup(struct fxp_softc * sc)
    185 {
    186 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *)sc;
    187 	pcireg_t command;
    188 
    189 	pcitag_t tag = csc->tag;
    190 
    191 	command = Cardbus_conf_read(csc->ct, tag, PCI_COMMAND_STATUS_REG);
    192 	if (csc->base0_reg) {
    193 		Cardbus_conf_write(csc->ct, tag,
    194 		    PCI_BAR0, csc->base0_reg);
    195 		command |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    196 	} else if (csc->base1_reg) {
    197 		Cardbus_conf_write(csc->ct, tag,
    198 		    PCI_BAR1, csc->base1_reg);
    199 		command |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    200 	}
    201 
    202 	/* enable the card */
    203 	Cardbus_conf_write(csc->ct, tag, PCI_COMMAND_STATUS_REG, command);
    204 }
    205 
    206 static int
    207 fxp_cardbus_enable(struct fxp_softc * sc)
    208 {
    209 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *)sc;
    210 	cardbus_devfunc_t ct = csc->ct;
    211 
    212 	Cardbus_function_enable(csc->ct);
    213 
    214 	fxp_cardbus_setup(sc);
    215 
    216 	/* Map and establish the interrupt. */
    217 
    218 	sc->sc_ih = Cardbus_intr_establish(ct, IPL_NET, fxp_intr, sc);
    219 	if (NULL == sc->sc_ih) {
    220 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt\n");
    221 		return 1;
    222 	}
    223 
    224 	return 0;
    225 }
    226 
    227 static void
    228 fxp_cardbus_disable(struct fxp_softc * sc)
    229 {
    230 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *)sc;
    231 	cardbus_devfunc_t ct = csc->ct;
    232 
    233 	/* Remove interrupt handler. */
    234 	Cardbus_intr_disestablish(ct, sc->sc_ih);
    235 
    236 	Cardbus_function_disable(csc->ct);
    237 }
    238 
    239 static int
    240 fxp_cardbus_detach(device_t self, int flags)
    241 {
    242 	struct fxp_cardbus_softc *csc = device_private(self);
    243 	struct fxp_softc *sc = &csc->sc;
    244 	struct cardbus_devfunc *ct = csc->ct;
    245 	int rv, reg;
    246 
    247 #ifdef DIAGNOSTIC
    248 	if (ct == NULL)
    249 		panic("%s: data structure lacks", device_xname(self));
    250 #endif
    251 
    252 	if ((rv = fxp_detach(sc, flags)) != 0)
    253 		return rv;
    254 	/*
    255 	 * Unhook the interrupt handler.
    256 	 */
    257 	Cardbus_intr_disestablish(ct, sc->sc_ih);
    258 
    259 	/*
    260 	 * release bus space and close window
    261 	 */
    262 	if (csc->base0_reg)
    263 		reg = PCI_BAR0;
    264 	else
    265 		reg = PCI_BAR1;
    266 	Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, sc->sc_size);
    267 	return 0;
    268 }
    269