Home | History | Annotate | Line # | Download | only in cardbus
if_ex_cardbus.c revision 1.9
      1 /*	$NetBSD: if_ex_cardbus.c,v 1.9 1999/11/17 08:49:16 thorpej Exp $	*/
      2 
      3 /*
      4  * CardBus specific routines for 3Com 3C575-family CardBus ethernet adapter
      5  *
      6  * Copyright (c) 1998 and 1999
      7  *       HAYAKAWA Koichi.  All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed by the author.
     20  * 4. Neither the name of the author nor the names of any co-contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY HAYAKAWA KOICHI ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL TAKESHI OHASHI OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *
     37  */
     38 
     39 /* #define EX_DEBUG 4 */	/* define to report infomation for debugging */
     40 
     41 #define EX_POWER_STATIC		/* do not use enable/disable functions */
     42 				/* I'm waiting elinkxl.c uses
     43                                    sc->enable and sc->disable
     44                                    functions. */
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/socket.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/errno.h>
     52 #include <sys/syslog.h>
     53 #include <sys/select.h>
     54 #include <sys/device.h>
     55 
     56 #include <net/if.h>
     57 #include <net/if_dl.h>
     58 #include <net/if_ether.h>
     59 #include <net/if_media.h>
     60 
     61 #include <machine/cpu.h>
     62 #include <machine/bus.h>
     63 
     64 #include <dev/cardbus/cardbusvar.h>
     65 #include <dev/cardbus/cardbusdevs.h>
     66 
     67 #include <dev/mii/miivar.h>
     68 
     69 #include <dev/ic/elink3var.h>
     70 #include <dev/ic/elink3reg.h>
     71 #include <dev/ic/elinkxlreg.h>
     72 #include <dev/ic/elinkxlvar.h>
     73 
     74 #if defined DEBUG && !defined EX_DEBUG
     75 #define EX_DEBUG
     76 #endif
     77 
     78 #if defined EX_DEBUG
     79 #define DPRINTF(a) printf a
     80 #else
     81 #define DPRINTF(a)
     82 #endif
     83 
     84 #define CARDBUS_3C575BTX_FUNCSTAT_PCIREG  CARDBUS_BASE2_REG  /* means 0x18 */
     85 #define EX_CB_INTR 4		/* intr acknowledge reg. CardBus only */
     86 #define EX_CB_INTR_ACK 0x8000 /* intr acknowledge bit */
     87 
     88 int ex_cardbus_match __P((struct device *, struct cfdata *, void *));
     89 void ex_cardbus_attach __P((struct device *, struct device *,void *));
     90 int ex_cardbus_detach __P((struct device *, int));
     91 void ex_cardbus_intr_ack __P((struct ex_softc *));
     92 
     93 #if !defined EX_POWER_STATIC
     94 int ex_cardbus_enable __P((struct ex_softc *sc));
     95 void ex_cardbus_disable __P((struct ex_softc *sc));
     96 #endif /* !defined EX_POWER_STATIC */
     97 
     98 struct ex_cardbus_softc {
     99 	struct ex_softc sc_softc;
    100 
    101 	cardbus_devfunc_t sc_ct;
    102 	int sc_intrline;
    103 	u_int8_t sc_cardbus_flags;
    104 #define EX_REATTACH		0x01
    105 #define EX_ABSENT		0x02
    106 	u_int8_t sc_cardtype;
    107 #define EX_3C575		1
    108 #define EX_3C575B		2
    109 
    110 	/* CardBus function status space.  575B requests it. */
    111 	bus_space_tag_t sc_funct;
    112 	bus_space_handle_t sc_funch;
    113 };
    114 
    115 struct cfattach ex_cardbus_ca = {
    116 	sizeof(struct ex_cardbus_softc), ex_cardbus_match,
    117 	    ex_cardbus_attach, ex_cardbus_detach
    118 };
    119 
    120 const struct ex_cardbus_product {
    121 	u_int32_t	ecp_prodid;	/* CardBus product ID */
    122 	int		ecp_flags;	/* initial softc flags */
    123 	pcireg_t	ecp_csr;	/* PCI CSR flags */
    124 	int		ecp_cardtype;	/* card type */
    125 	const char	*ecp_name;	/* device name */
    126 } ex_cardbus_products[] = {
    127 	{ CARDBUS_PRODUCT_3COM_3C575TX,
    128 	  EX_CONF_MII,
    129 	  CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE,
    130 	  EX_3C575,
    131 	  "3c575-TX Ethernet" },
    132 
    133 	{ CARDBUS_PRODUCT_3COM_3C575BTX,
    134 	  EX_CONF_90XB|EX_CONF_MII,
    135 	  CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MEM_ENABLE |
    136 	      CARDBUS_COMMAND_MASTER_ENABLE,
    137 	  EX_3C575B,
    138 	  "3c575B-TX Ethernet" },
    139 
    140 	{ 0,
    141 	  0,
    142 	  0,
    143 	  NULL },
    144 };
    145 
    146 const struct ex_cardbus_product *ex_cardbus_lookup
    147     __P((const struct cardbus_attach_args *));
    148 
    149 const struct ex_cardbus_product *
    150 ex_cardbus_lookup(ca)
    151 	const struct cardbus_attach_args *ca;
    152 {
    153 	const struct ex_cardbus_product *ecp;
    154 
    155 	if (CARDBUS_VENDOR(ca->ca_id) != CARDBUS_VENDOR_3COM)
    156 		return (NULL);
    157 
    158 	for (ecp = ex_cardbus_products; ecp->ecp_name != NULL; ecp++)
    159 		if (CARDBUS_PRODUCT(ca->ca_id) == ecp->ecp_prodid)
    160 			return (ecp);
    161 	return (NULL);
    162 }
    163 
    164 int
    165 ex_cardbus_match(parent, cf, aux)
    166 	struct device *parent;
    167 	struct cfdata *cf;
    168 	void *aux;
    169 {
    170 	struct cardbus_attach_args *ca = aux;
    171 
    172 	if (ex_cardbus_lookup(ca) != NULL)
    173 		return (1);
    174 
    175 	return (0);
    176 }
    177 
    178 void
    179 ex_cardbus_attach(parent, self, aux)
    180 	struct device *parent;
    181 	struct device *self;
    182 	void *aux;
    183 {
    184 	struct ex_cardbus_softc *psc = (void *)self;
    185 	struct ex_softc *sc = &psc->sc_softc;
    186 	struct cardbus_attach_args *ca = aux;
    187 	cardbus_devfunc_t ct = ca->ca_ct;
    188 	cardbus_chipset_tag_t cc = ct->ct_cc;
    189 	cardbus_function_tag_t cf = ct->ct_cf;
    190 	cardbusreg_t iob, command, bhlc;
    191 	const struct ex_cardbus_product *ecp;
    192 	bus_space_handle_t ioh;
    193 	bus_addr_t adr;
    194 
    195 	if (Cardbus_mapreg_map(ct, CARDBUS_BASE0_REG, CARDBUS_MAPREG_TYPE_IO, 0,
    196 	    &(sc->sc_iot), &ioh, &adr, NULL)) {
    197 		printf(": can't map i/o space\n");
    198 		return;
    199 	}
    200 
    201 	ecp = ex_cardbus_lookup(ca);
    202 	if (ecp == NULL) {
    203 		printf("\n");
    204 		panic("ex_cardbus_attach: impossible");
    205 	}
    206 
    207 	printf(": 3Com %s\n", ecp->ecp_name);
    208 
    209 #if !defined EX_POWER_STATIC
    210 	sc->enable = ex_cardbus_enable;
    211 	sc->disable = ex_cardbus_disable;
    212 #else
    213 	sc->enable = NULL;
    214 	sc->disable = NULL;
    215 #endif
    216 	sc->enabled = 1;
    217 
    218 	sc->sc_dmat = ca->ca_dmat;
    219 
    220 	sc->ex_bustype = EX_BUS_CARDBUS;
    221 	sc->ex_conf = ecp->ecp_flags;
    222 
    223 	iob = adr;
    224 	sc->sc_ioh = ioh;
    225 
    226 #if rbus
    227 #else
    228 	(ct->ct_cf->cardbus_io_open)(cc, 0, iob, iob + 0x40);
    229 #endif
    230 	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
    231 
    232 	command = cardbus_conf_read(cc, cf, ca->ca_tag,
    233 	    CARDBUS_COMMAND_STATUS_REG);
    234 	command |= ecp->ecp_csr;
    235 	psc->sc_cardtype = ecp->ecp_cardtype;
    236 
    237 	if (psc->sc_cardtype == EX_3C575B) {
    238 		/* Map CardBus function status window. */
    239 		if (Cardbus_mapreg_map(ct, CARDBUS_3C575BTX_FUNCSTAT_PCIREG,
    240 		    CARDBUS_MAPREG_TYPE_MEM, 0, &psc->sc_funct,
    241 		    &psc->sc_funch, 0, NULL)) {
    242 			printf("%s: unable to map function status window\n",
    243 			    self->dv_xname);
    244 			return;
    245 		}
    246 
    247 		/*
    248 		 * Make sure CardBus brigde can access memory space.  Usually
    249 		 * memory access is enabled by BIOS, but some BIOSes do not
    250 		 * enable it.
    251 		 */
    252 		(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    253 
    254 		/* Setup interrupt acknowledge hook */
    255 		sc->intr_ack = ex_cardbus_intr_ack;
    256 	}
    257 
    258 	cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG,
    259 	    command);
    260 
    261  	/*
    262 	 * set latency timmer
    263 	 */
    264 	bhlc = cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG);
    265 	if (CARDBUS_LATTIMER(bhlc) < 0x20) {
    266 		/* at least the value of latency timer should 0x20. */
    267 		DPRINTF(("if_ex_cardbus: lattimer 0x%x -> 0x20\n",
    268 		    CARDBUS_LATTIMER(bhlc)));
    269 		bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    270 		bhlc |= (0x20 << CARDBUS_LATTIMER_SHIFT);
    271 		cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG, bhlc);
    272 	}
    273 
    274 	psc->sc_ct = ca->ca_ct;
    275 	psc->sc_intrline = ca->ca_intrline;
    276 
    277 #if defined EX_POWER_STATIC
    278 	/* Map and establish the interrupt. */
    279 
    280 	sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_NET,
    281 	    ex_intr, psc);
    282 	if (sc->sc_ih == NULL) {
    283 		printf("%s: couldn't establish interrupt",
    284 		    sc->sc_dev.dv_xname);
    285 		printf(" at %d", ca->ca_intrline);
    286 		printf("\n");
    287 		return;
    288 	}
    289 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    290 	    ca->ca_intrline);
    291 #endif
    292 
    293 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND, GLOBAL_RESET);
    294 	delay(400);
    295 	{
    296 		int i = 0;
    297 		while (bus_space_read_2(sc->sc_iot, sc->sc_ioh, ELINK_STATUS) &
    298 		    S_COMMAND_IN_PROGRESS) {
    299 			if (++i > 10000) {
    300 				printf("ex: timeout %x\n",
    301 				    bus_space_read_2(sc->sc_iot, sc->sc_ioh,
    302 				        ELINK_STATUS));
    303 				printf("ex: addr %x\n",
    304 				    cardbus_conf_read(cc, cf, ca->ca_tag,
    305 				    CARDBUS_BASE0_REG));
    306 				return;		/* emergency exit */
    307 			}
    308 		}
    309 	}
    310 
    311 	ex_config(sc);
    312 
    313 	if (psc->sc_cardtype == EX_3C575B)
    314 		bus_space_write_4(psc->sc_funct, psc->sc_funch,
    315 		    EX_CB_INTR, EX_CB_INTR_ACK);
    316 
    317 #if !defined EX_POWER_STATIC
    318 	cardbus_function_disable(psc->sc_ct);
    319 	sc->enabled = 0;
    320 #endif
    321 }
    322 
    323 void
    324 ex_cardbus_intr_ack(sc)
    325 	struct ex_softc *sc;
    326 {
    327 	struct ex_cardbus_softc *psc = (struct ex_cardbus_softc *)sc;
    328 
    329 	bus_space_write_4(psc->sc_funct, psc->sc_funch, EX_CB_INTR,
    330 	    EX_CB_INTR_ACK);
    331 }
    332 
    333 int
    334 ex_cardbus_detach(self, arg)
    335 	struct device *self;
    336 	int arg;
    337 {
    338 	struct ex_cardbus_softc *psc = (void *)self;
    339 	struct ex_softc *sc = &psc->sc_softc;
    340 	cardbus_function_tag_t cf = psc->sc_ct->ct_cf;
    341 	cardbus_chipset_tag_t cc = psc->sc_ct->ct_cc;
    342 
    343 	/*
    344 	 * XXX Currently, no detach.
    345 	 */
    346 	printf("- ex_cardbus_detach\n");
    347 
    348 	cardbus_intr_disestablish(cc, cf, sc->sc_ih);
    349 
    350 	sc->enabled = 0;
    351 
    352 	return (EBUSY);
    353 }
    354 
    355 #if !defined EX_POWER_STATIC
    356 int
    357 ex_cardbus_enable(sc)
    358 	struct ex_softc *sc;
    359 {
    360 	struct ex_cardbus_softc *csc = (struct ex_cardbus_softc *)sc;
    361 	cardbus_function_tag_t cf = csc->sc_ct->ct_cf;
    362 	cardbus_chipset_tag_t cc = csc->sc_ct->ct_cc;
    363 
    364 	Cardbus_function_enable(csc->sc_ct);
    365 	cardbus_restore_bar(csc->sc_ct);
    366 
    367 	sc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline,
    368 	    IPL_NET, ex_intr, sc);
    369 	if (NULL == sc->sc_ih) {
    370 		printf("%s: couldn't establish interrupt\n",
    371 		    sc->sc_dev.dv_xname);
    372 		return (1);
    373 	}
    374 
    375 	return (0);
    376 }
    377 
    378 void
    379 ex_cardbus_disable(sc)
    380 	struct ex_softc *sc;
    381 {
    382 	struct ex_cardbus_softc *csc = (struct ex_cardbus_softc *)sc;
    383 	cardbus_function_tag_t cf = csc->sc_ct->ct_cf;
    384 	cardbus_chipset_tag_t cc = csc->sc_ct->ct_cc;
    385 
    386 	cardbus_save_bar(csc->sc_ct);
    387 
    388  	Cardbus_function_disable(csc->sc_ct);
    389 
    390 	cardbus_intr_disestablish(cc, cf, sc->sc_ih);
    391 }
    392 #endif /* EX_POWER_STATIC */
    393