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