Home | History | Annotate | Line # | Download | only in cardbus
if_fxp_cardbus.c revision 1.25
      1 /*	$NetBSD: if_fxp_cardbus.c,v 1.25 2006/10/12 01:30:55 christos 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.25 2006/10/12 01:30:55 christos 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 <machine/bus.h>
     82 #include <machine/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 __unused, struct cfdata *match __unused,
    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 __unused, 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 
    186 static void
    187 fxp_cardbus_setup(struct fxp_softc * sc)
    188 {
    189 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
    190 	struct cardbus_softc *psc =
    191 	    (struct cardbus_softc *) device_parent(&sc->sc_dev);
    192 	cardbus_chipset_tag_t cc = psc->sc_cc;
    193 	cardbus_function_tag_t cf = psc->sc_cf;
    194 	pcireg_t command;
    195 
    196 	cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
    197 	    csc->ct->ct_func);
    198 
    199 	command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
    200 	if (csc->base0_reg) {
    201 		Cardbus_conf_write(csc->ct, tag,
    202 		    CARDBUS_BASE0_REG, csc->base0_reg);
    203 		(cf->cardbus_ctrl) (cc, CARDBUS_MEM_ENABLE);
    204 		command |= CARDBUS_COMMAND_MEM_ENABLE |
    205 		    CARDBUS_COMMAND_MASTER_ENABLE;
    206 	} else if (csc->base1_reg) {
    207 		Cardbus_conf_write(csc->ct, tag,
    208 		    CARDBUS_BASE1_REG, csc->base1_reg);
    209 		(cf->cardbus_ctrl) (cc, CARDBUS_IO_ENABLE);
    210 		command |= (CARDBUS_COMMAND_IO_ENABLE |
    211 		    CARDBUS_COMMAND_MASTER_ENABLE);
    212 	}
    213 
    214 	(cf->cardbus_ctrl) (cc, CARDBUS_BM_ENABLE);
    215 
    216 	/* enable the card */
    217 	Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
    218 }
    219 
    220 static int
    221 fxp_cardbus_enable(struct fxp_softc * sc)
    222 {
    223 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
    224 	struct cardbus_softc *psc =
    225 	    (struct cardbus_softc *) device_parent(&sc->sc_dev);
    226 	cardbus_chipset_tag_t cc = psc->sc_cc;
    227 	cardbus_function_tag_t cf = psc->sc_cf;
    228 
    229 	Cardbus_function_enable(csc->ct);
    230 
    231 	fxp_cardbus_setup(sc);
    232 
    233 	/* Map and establish the interrupt. */
    234 
    235 	sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
    236 	    fxp_intr, sc);
    237 	if (NULL == sc->sc_ih) {
    238 		printf("%s: couldn't establish interrupt\n",
    239 		    sc->sc_dev.dv_xname);
    240 		return 1;
    241 	}
    242 
    243 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    244 	    psc->sc_intrline);
    245 
    246 	return 0;
    247 }
    248 
    249 static void
    250 fxp_cardbus_disable(struct fxp_softc * sc)
    251 {
    252 	struct cardbus_softc *psc =
    253 	    (struct cardbus_softc *) device_parent(&sc->sc_dev);
    254 	cardbus_chipset_tag_t cc = psc->sc_cc;
    255 	cardbus_function_tag_t cf = psc->sc_cf;
    256 
    257 	/* Remove interrupt handler. */
    258 	cardbus_intr_disestablish(cc, cf, sc->sc_ih);
    259 
    260 	Cardbus_function_disable(((struct fxp_cardbus_softc *) sc)->ct);
    261 }
    262 
    263 static int
    264 fxp_cardbus_detach(struct device *self, int flags __unused)
    265 {
    266 	struct fxp_softc *sc = device_private(self);
    267 	struct fxp_cardbus_softc *csc = device_private(self);
    268 	struct cardbus_devfunc *ct = csc->ct;
    269 	int rv, reg;
    270 
    271 #ifdef DIAGNOSTIC
    272 	if (ct == NULL)
    273 		panic("%s: data structure lacks", sc->sc_dev.dv_xname);
    274 #endif
    275 
    276 	rv = fxp_detach(sc);
    277 	if (rv == 0) {
    278 		/*
    279 		 * Unhook the interrupt handler.
    280 		 */
    281 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
    282 
    283 		/*
    284 		 * release bus space and close window
    285 		 */
    286 		if (csc->base0_reg)
    287 			reg = CARDBUS_BASE0_REG;
    288 		else
    289 			reg = CARDBUS_BASE1_REG;
    290 		Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, csc->size);
    291 	}
    292 	return (rv);
    293 }
    294