Home | History | Annotate | Line # | Download | only in cardbus
if_fxp_cardbus.c revision 1.50.24.1
      1  1.50.24.1     skrll /*	$NetBSD: if_fxp_cardbus.c,v 1.50.24.1 2015/06/06 14:40:06 skrll Exp $	*/
      2        1.1      haya 
      3        1.1      haya /*
      4        1.3      joda  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5        1.3      joda  * All rights reserved.
      6        1.1      haya  *
      7        1.9   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8        1.9   thorpej  * by Johan Danielsson.
      9        1.1      haya  *
     10        1.9   thorpej  * Redistribution and use in source and binary forms, with or without
     11        1.9   thorpej  * modification, are permitted provided that the following conditions
     12        1.9   thorpej  * are met:
     13        1.9   thorpej  * 1. Redistributions of source code must retain the above copyright
     14        1.9   thorpej  *    notice, this list of conditions and the following disclaimer.
     15        1.9   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.9   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17        1.9   thorpej  *    documentation and/or other materials provided with the distribution.
     18        1.3      joda  *
     19        1.3      joda  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.3      joda  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.3      joda  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.3      joda  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.3      joda  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.3      joda  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.3      joda  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.3      joda  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.3      joda  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.3      joda  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1      haya  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1      haya  */
     31        1.1      haya 
     32        1.9   thorpej /*
     33        1.9   thorpej  * CardBus front-end for the Intel i82557 family of Ethernet chips.
     34        1.9   thorpej  */
     35       1.13     lukem 
     36       1.13     lukem #include <sys/cdefs.h>
     37  1.50.24.1     skrll __KERNEL_RCSID(0, "$NetBSD: if_fxp_cardbus.c,v 1.50.24.1 2015/06/06 14:40:06 skrll Exp $");
     38        1.1      haya 
     39        1.1      haya #include "opt_inet.h"
     40        1.1      haya 
     41        1.1      haya #include <sys/param.h>
     42        1.1      haya #include <sys/systm.h>
     43        1.1      haya #include <sys/mbuf.h>
     44        1.1      haya #include <sys/malloc.h>
     45        1.1      haya #include <sys/kernel.h>
     46        1.1      haya #include <sys/socket.h>
     47        1.1      haya #include <sys/ioctl.h>
     48        1.1      haya #include <sys/errno.h>
     49        1.1      haya #include <sys/device.h>
     50        1.1      haya 
     51        1.1      haya #include <net/if.h>
     52        1.1      haya #include <net/if_dl.h>
     53        1.1      haya #include <net/if_media.h>
     54        1.1      haya #include <net/if_ether.h>
     55        1.5   thorpej 
     56        1.5   thorpej #include <machine/endian.h>
     57        1.1      haya 
     58        1.1      haya #ifdef INET
     59        1.1      haya #include <netinet/in.h>
     60        1.1      haya #include <netinet/if_inarp.h>
     61        1.1      haya #endif
     62        1.1      haya 
     63        1.1      haya 
     64       1.27        ad #include <sys/bus.h>
     65       1.27        ad #include <sys/intr.h>
     66        1.1      haya 
     67        1.1      haya #include <dev/mii/miivar.h>
     68        1.1      haya 
     69        1.3      joda #include <dev/ic/i82557reg.h>
     70        1.3      joda #include <dev/ic/i82557var.h>
     71        1.1      haya 
     72        1.1      haya #include <dev/pci/pcivar.h>
     73        1.1      haya #include <dev/pci/pcireg.h>
     74        1.1      haya #include <dev/pci/pcidevs.h>
     75        1.1      haya 
     76        1.1      haya #include <dev/cardbus/cardbusvar.h>
     77       1.18   mycroft #include <dev/pci/pcidevs.h>
     78        1.1      haya 
     79       1.34     joerg static int fxp_cardbus_match(device_t, cfdata_t, void *);
     80       1.34     joerg static void fxp_cardbus_attach(device_t, device_t, void *);
     81       1.34     joerg static int fxp_cardbus_detach(device_t, int);
     82       1.34     joerg static void fxp_cardbus_setup(struct fxp_softc *);
     83       1.34     joerg static int fxp_cardbus_enable(struct fxp_softc *);
     84       1.34     joerg static void fxp_cardbus_disable(struct fxp_softc *);
     85        1.1      haya 
     86        1.1      haya struct fxp_cardbus_softc {
     87        1.9   thorpej 	struct fxp_softc sc;
     88        1.9   thorpej 	cardbus_devfunc_t ct;
     89       1.43    dyoung 	pcitag_t tag;
     90        1.9   thorpej 	pcireg_t base0_reg;
     91        1.9   thorpej 	pcireg_t base1_reg;
     92        1.1      haya };
     93        1.1      haya 
     94       1.45    dyoung CFATTACH_DECL3_NEW(fxp_cardbus, sizeof(struct fxp_cardbus_softc),
     95       1.45    dyoung     fxp_cardbus_match, fxp_cardbus_attach, fxp_cardbus_detach, fxp_activate,
     96       1.45    dyoung     NULL, null_childdetached, DVF_DETACH_SHUTDOWN);
     97        1.1      haya 
     98        1.1      haya #ifdef CBB_DEBUG
     99        1.1      haya #define DPRINTF(X) printf X
    100        1.1      haya #else
    101        1.1      haya #define DPRINTF(X)
    102        1.1      haya #endif
    103        1.1      haya 
    104        1.1      haya static int
    105       1.39    cegger fxp_cardbus_match(device_t parent, cfdata_t match,
    106       1.25  christos     void *aux)
    107        1.1      haya {
    108        1.9   thorpej 	struct cardbus_attach_args *ca = aux;
    109        1.9   thorpej 
    110       1.46    dyoung 	if (PCI_VENDOR(ca->ca_id) == PCI_VENDOR_INTEL &&
    111       1.49   msaitoh 	    PCI_PRODUCT(ca->ca_id) == PCI_PRODUCT_INTEL_8255X)
    112       1.11   thorpej 		return (1);
    113       1.11   thorpej 
    114       1.11   thorpej 	return (0);
    115        1.1      haya }
    116        1.1      haya 
    117        1.1      haya static void
    118       1.39    cegger fxp_cardbus_attach(device_t parent, device_t self,
    119       1.25  christos     void *aux)
    120        1.1      haya {
    121       1.23   thorpej 	struct fxp_cardbus_softc *csc = device_private(self);
    122       1.34     joerg 	struct fxp_softc *sc = &csc->sc;
    123        1.9   thorpej 	struct cardbus_attach_args *ca = aux;
    124        1.9   thorpej 	bus_space_tag_t iot, memt;
    125        1.9   thorpej 	bus_space_handle_t ioh, memh;
    126        1.9   thorpej 
    127        1.9   thorpej 	bus_addr_t adr;
    128        1.9   thorpej 
    129       1.34     joerg 	sc->sc_dev = self;
    130        1.9   thorpej 	csc->ct = ca->ca_ct;
    131       1.43    dyoung 	csc->tag = ca->ca_tag;
    132        1.9   thorpej 
    133        1.9   thorpej 	/*
    134        1.9   thorpej          * Map control/status registers.
    135        1.9   thorpej          */
    136       1.46    dyoung 	if (Cardbus_mapreg_map(csc->ct, PCI_BAR1,
    137       1.45    dyoung 	    PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &sc->sc_size) == 0) {
    138       1.10     veego 		csc->base1_reg = adr | 1;
    139       1.10     veego 		sc->sc_st = iot;
    140       1.10     veego 		sc->sc_sh = ioh;
    141       1.46    dyoung 	} else if (Cardbus_mapreg_map(csc->ct, PCI_BAR0,
    142        1.9   thorpej 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    143       1.45    dyoung 	    0, &memt, &memh, &adr, &sc->sc_size) == 0) {
    144        1.9   thorpej 		csc->base0_reg = adr;
    145        1.9   thorpej 		sc->sc_st = memt;
    146        1.9   thorpej 		sc->sc_sh = memh;
    147        1.9   thorpej 	} else
    148       1.34     joerg 		panic("%s: failed to allocate mem and io space", __func__);
    149        1.9   thorpej 
    150        1.9   thorpej 	if (ca->ca_cis.cis1_info[0] && ca->ca_cis.cis1_info[1])
    151        1.9   thorpej 		printf(": %s %s\n", ca->ca_cis.cis1_info[0],
    152        1.9   thorpej 		    ca->ca_cis.cis1_info[1]);
    153        1.9   thorpej 	else
    154        1.9   thorpej 		printf("\n");
    155        1.9   thorpej 
    156       1.46    dyoung 	sc->sc_rev = PCI_REVISION(ca->ca_class);
    157       1.35       mrg 	if (sc->sc_rev >= FXP_REV_82558_A4)
    158       1.35       mrg 		sc->sc_flags |= FXPF_FC|FXPF_EXT_TXCB;
    159       1.37   tsutsui 	if (sc->sc_rev >= FXP_REV_82559_A0)
    160       1.37   tsutsui 		sc->sc_flags |= FXPF_82559_RXCSUM;
    161       1.37   tsutsui 	if (sc->sc_rev >= FXP_REV_82550) {
    162       1.37   tsutsui 		sc->sc_flags &= ~FXPF_82559_RXCSUM;
    163       1.36       mrg 		sc->sc_flags |= FXPF_EXT_RFA;
    164       1.37   tsutsui 	}
    165       1.35       mrg 
    166        1.9   thorpej 	sc->sc_dmat = ca->ca_dmat;
    167        1.9   thorpej 	sc->sc_enable = fxp_cardbus_enable;
    168        1.9   thorpej 	sc->sc_disable = fxp_cardbus_disable;
    169        1.9   thorpej 	sc->sc_enabled = 0;
    170        1.9   thorpej 
    171        1.9   thorpej 	fxp_enable(sc);
    172        1.9   thorpej 	fxp_attach(sc);
    173        1.9   thorpej 	fxp_disable(sc);
    174       1.28  jmcneill 
    175       1.40   tsutsui 	if (pmf_device_register(self, NULL, NULL))
    176       1.40   tsutsui 		pmf_class_network_register(self, &sc->sc_ethercom.ec_if);
    177       1.40   tsutsui 	else
    178       1.28  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    179        1.1      haya }
    180        1.1      haya 
    181        1.1      haya static void
    182        1.9   thorpej fxp_cardbus_setup(struct fxp_softc * sc)
    183        1.1      haya {
    184       1.34     joerg 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *)sc;
    185        1.9   thorpej 	pcireg_t command;
    186        1.9   thorpej 
    187       1.43    dyoung 	pcitag_t tag = csc->tag;
    188        1.9   thorpej 
    189       1.46    dyoung 	command = Cardbus_conf_read(csc->ct, tag, PCI_COMMAND_STATUS_REG);
    190        1.9   thorpej 	if (csc->base0_reg) {
    191        1.9   thorpej 		Cardbus_conf_write(csc->ct, tag,
    192       1.46    dyoung 		    PCI_BAR0, csc->base0_reg);
    193       1.46    dyoung 		command |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    194        1.9   thorpej 	} else if (csc->base1_reg) {
    195        1.9   thorpej 		Cardbus_conf_write(csc->ct, tag,
    196       1.46    dyoung 		    PCI_BAR1, csc->base1_reg);
    197       1.46    dyoung 		command |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    198        1.9   thorpej 	}
    199        1.9   thorpej 
    200        1.9   thorpej 	/* enable the card */
    201       1.46    dyoung 	Cardbus_conf_write(csc->ct, tag, PCI_COMMAND_STATUS_REG, command);
    202        1.1      haya }
    203        1.1      haya 
    204        1.1      haya static int
    205        1.9   thorpej fxp_cardbus_enable(struct fxp_softc * sc)
    206        1.1      haya {
    207       1.34     joerg 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *)sc;
    208       1.47    dyoung 	cardbus_devfunc_t ct = csc->ct;
    209        1.9   thorpej 
    210        1.9   thorpej 	Cardbus_function_enable(csc->ct);
    211        1.9   thorpej 
    212        1.9   thorpej 	fxp_cardbus_setup(sc);
    213        1.9   thorpej 
    214        1.9   thorpej 	/* Map and establish the interrupt. */
    215        1.9   thorpej 
    216       1.48  drochner 	sc->sc_ih = Cardbus_intr_establish(ct, IPL_NET, fxp_intr, sc);
    217        1.9   thorpej 	if (NULL == sc->sc_ih) {
    218       1.34     joerg 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt\n");
    219        1.9   thorpej 		return 1;
    220        1.9   thorpej 	}
    221        1.1      haya 
    222        1.9   thorpej 	return 0;
    223        1.1      haya }
    224        1.1      haya 
    225        1.1      haya static void
    226        1.9   thorpej fxp_cardbus_disable(struct fxp_softc * sc)
    227        1.1      haya {
    228       1.34     joerg 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *)sc;
    229       1.47    dyoung 	cardbus_devfunc_t ct = csc->ct;
    230        1.9   thorpej 
    231        1.9   thorpej 	/* Remove interrupt handler. */
    232       1.47    dyoung 	Cardbus_intr_disestablish(ct, sc->sc_ih);
    233        1.9   thorpej 
    234       1.34     joerg 	Cardbus_function_disable(csc->ct);
    235        1.6      joda }
    236        1.6      joda 
    237        1.6      joda static int
    238       1.34     joerg fxp_cardbus_detach(device_t self, int flags)
    239        1.6      joda {
    240       1.23   thorpej 	struct fxp_cardbus_softc *csc = device_private(self);
    241       1.34     joerg 	struct fxp_softc *sc = &csc->sc;
    242        1.6      joda 	struct cardbus_devfunc *ct = csc->ct;
    243        1.9   thorpej 	int rv, reg;
    244        1.6      joda 
    245        1.6      joda #ifdef DIAGNOSTIC
    246        1.9   thorpej 	if (ct == NULL)
    247       1.34     joerg 		panic("%s: data structure lacks", device_xname(self));
    248        1.6      joda #endif
    249        1.6      joda 
    250       1.45    dyoung 	if ((rv = fxp_detach(sc, flags)) != 0)
    251       1.45    dyoung 		return rv;
    252       1.45    dyoung 	/*
    253       1.45    dyoung 	 * Unhook the interrupt handler.
    254       1.45    dyoung 	 */
    255       1.47    dyoung 	Cardbus_intr_disestablish(ct, sc->sc_ih);
    256       1.45    dyoung 
    257       1.45    dyoung 	/*
    258       1.45    dyoung 	 * release bus space and close window
    259       1.45    dyoung 	 */
    260       1.45    dyoung 	if (csc->base0_reg)
    261       1.46    dyoung 		reg = PCI_BAR0;
    262       1.45    dyoung 	else
    263       1.46    dyoung 		reg = PCI_BAR1;
    264       1.45    dyoung 	Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, sc->sc_size);
    265       1.45    dyoung 	return 0;
    266        1.1      haya }
    267