Home | History | Annotate | Line # | Download | only in cardbus
if_fxp_cardbus.c revision 1.28
      1 /*	$NetBSD: if_fxp_cardbus.c,v 1.28 2007/12/09 23:47:53 jmcneill 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.28 2007/12/09 23:47:53 jmcneill 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 <sys/bus.h>
     82 #include <sys/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(struct device *parent, struct cfdata *match,
    122     void *aux)
    123 {
    124 	struct cardbus_attach_args *ca = aux;
    125 
    126 	if (CARDBUS_VENDOR(ca->ca_id) == PCI_VENDOR_INTEL &&
    127 	    CARDBUS_PRODUCT(ca->ca_id) == PCI_PRODUCT_INTEL_82557)
    128 		return (1);
    129 
    130 	return (0);
    131 }
    132 
    133 static void
    134 fxp_cardbus_attach(struct device *parent, struct device *self,
    135     void *aux)
    136 {
    137 	static const char thisfunc[] = "fxp_cardbus_attach";
    138 
    139 	struct fxp_softc *sc = device_private(self);
    140 	struct fxp_cardbus_softc *csc = device_private(self);
    141 	struct cardbus_attach_args *ca = aux;
    142 	bus_space_tag_t iot, memt;
    143 	bus_space_handle_t ioh, memh;
    144 
    145 	bus_addr_t adr;
    146 	bus_size_t size;
    147 
    148 	csc->ct = ca->ca_ct;
    149 
    150 	/*
    151          * Map control/status registers.
    152          */
    153 	if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
    154 	    PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &size) == 0) {
    155 		csc->base1_reg = adr | 1;
    156 		sc->sc_st = iot;
    157 		sc->sc_sh = ioh;
    158 		csc->size = size;
    159 	} else if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
    160 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    161 	    0, &memt, &memh, &adr, &size) == 0) {
    162 		csc->base0_reg = adr;
    163 		sc->sc_st = memt;
    164 		sc->sc_sh = memh;
    165 		csc->size = size;
    166 	} else
    167 		panic("%s: failed to allocate mem and io space", thisfunc);
    168 
    169 
    170 	if (ca->ca_cis.cis1_info[0] && ca->ca_cis.cis1_info[1])
    171 		printf(": %s %s\n", ca->ca_cis.cis1_info[0],
    172 		    ca->ca_cis.cis1_info[1]);
    173 	else
    174 		printf("\n");
    175 
    176 	sc->sc_dmat = ca->ca_dmat;
    177 	sc->sc_enable = fxp_cardbus_enable;
    178 	sc->sc_disable = fxp_cardbus_disable;
    179 	sc->sc_enabled = 0;
    180 
    181 	fxp_enable(sc);
    182 	fxp_attach(sc);
    183 	fxp_disable(sc);
    184 
    185 	if (!pmf_driver_register(self, NULL, NULL))
    186 		aprint_error_dev(self, "couldn't establish power handler\n");
    187 	else
    188 		pmf_class_network_register(self, &sc->sc_ethercom.ec_if);
    189 }
    190 
    191 static void
    192 fxp_cardbus_setup(struct fxp_softc * sc)
    193 {
    194 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
    195 	struct cardbus_softc *psc =
    196 	    (struct cardbus_softc *) device_parent(&sc->sc_dev);
    197 	cardbus_chipset_tag_t cc = psc->sc_cc;
    198 	cardbus_function_tag_t cf = psc->sc_cf;
    199 	pcireg_t command;
    200 
    201 	cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
    202 	    csc->ct->ct_func);
    203 
    204 	command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
    205 	if (csc->base0_reg) {
    206 		Cardbus_conf_write(csc->ct, tag,
    207 		    CARDBUS_BASE0_REG, csc->base0_reg);
    208 		(cf->cardbus_ctrl) (cc, CARDBUS_MEM_ENABLE);
    209 		command |= CARDBUS_COMMAND_MEM_ENABLE |
    210 		    CARDBUS_COMMAND_MASTER_ENABLE;
    211 	} else if (csc->base1_reg) {
    212 		Cardbus_conf_write(csc->ct, tag,
    213 		    CARDBUS_BASE1_REG, csc->base1_reg);
    214 		(cf->cardbus_ctrl) (cc, CARDBUS_IO_ENABLE);
    215 		command |= (CARDBUS_COMMAND_IO_ENABLE |
    216 		    CARDBUS_COMMAND_MASTER_ENABLE);
    217 	}
    218 
    219 	(cf->cardbus_ctrl) (cc, CARDBUS_BM_ENABLE);
    220 
    221 	/* enable the card */
    222 	Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
    223 }
    224 
    225 static int
    226 fxp_cardbus_enable(struct fxp_softc * sc)
    227 {
    228 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
    229 	struct cardbus_softc *psc =
    230 	    (struct cardbus_softc *) device_parent(&sc->sc_dev);
    231 	cardbus_chipset_tag_t cc = psc->sc_cc;
    232 	cardbus_function_tag_t cf = psc->sc_cf;
    233 
    234 	Cardbus_function_enable(csc->ct);
    235 
    236 	fxp_cardbus_setup(sc);
    237 
    238 	/* Map and establish the interrupt. */
    239 
    240 	sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
    241 	    fxp_intr, sc);
    242 	if (NULL == sc->sc_ih) {
    243 		printf("%s: couldn't establish interrupt\n",
    244 		    sc->sc_dev.dv_xname);
    245 		return 1;
    246 	}
    247 
    248 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    249 	    psc->sc_intrline);
    250 
    251 	return 0;
    252 }
    253 
    254 static void
    255 fxp_cardbus_disable(struct fxp_softc * sc)
    256 {
    257 	struct cardbus_softc *psc =
    258 	    (struct cardbus_softc *) device_parent(&sc->sc_dev);
    259 	cardbus_chipset_tag_t cc = psc->sc_cc;
    260 	cardbus_function_tag_t cf = psc->sc_cf;
    261 
    262 	/* Remove interrupt handler. */
    263 	cardbus_intr_disestablish(cc, cf, sc->sc_ih);
    264 
    265 	Cardbus_function_disable(((struct fxp_cardbus_softc *) sc)->ct);
    266 }
    267 
    268 static int
    269 fxp_cardbus_detach(struct device *self, 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