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