Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.9
      1 /*	$NetBSD: cardbus.c,v 1.9 1999/11/01 09:59:23 haya Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998 and 1999
      5  *     HAYAKAWA Koichi.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by HAYAKAWA Koichi.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     26  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include "opt_cardbus.h"
     36 
     37 #include <sys/types.h>
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/device.h>
     41 #include <sys/malloc.h>
     42 #include <sys/kernel.h>
     43 #include <sys/syslog.h>
     44 
     45 #include <machine/bus.h>
     46 
     47 #include <dev/cardbus/cardbusvar.h>
     48 #include <dev/cardbus/pccardcis.h>
     49 
     50 #include <dev/cardbus/cardbus_exrom.h>
     51 
     52 #include <dev/pci/pcivar.h>	/* XXX */
     53 #include <dev/pci/pcireg.h>	/* XXX */
     54 
     55 #if defined CARDBUS_DEBUG
     56 #define STATIC
     57 #define DPRINTF(a) printf a
     58 #define DDELAY(x) delay((x)*1000*1000)
     59 #else
     60 #define STATIC static
     61 #define DPRINTF(a)
     62 #endif
     63 
     64 
     65 
     66 STATIC void cardbusattach __P((struct device *, struct device *, void *));
     67 /* STATIC int cardbusprint __P((void *, const char *)); */
     68 int cardbus_attach_card __P((struct cardbus_softc *));
     69 
     70 #if !defined __BROKEN_INDIRECT_CONFIG
     71 STATIC int cardbusmatch __P((struct device *, struct cfdata *, void *));
     72 static int cardbussubmatch __P((struct device *, struct cfdata *, void *));
     73 #else
     74 STATIC int cardbusmatch __P((struct device *, void *, void *));
     75 static int cardbussubmatch __P((struct device *, void *, void *));
     76 #endif
     77 static int cardbusprint __P((void *, const char *));
     78 
     79 static int decode_tuples __P((u_int8_t *, int));
     80 
     81 static int cardbus_read_tuples __P((struct cardbus_attach_args *,
     82 				    cardbusreg_t, u_int8_t *, size_t));
     83 
     84 static void enable_function __P((struct cardbus_softc *, int, int));
     85 static void disable_function __P((struct cardbus_softc *, int));
     86 
     87 
     88 struct cfattach cardbus_ca = {
     89 	sizeof(struct cardbus_softc), cardbusmatch, cardbusattach
     90 };
     91 
     92 #ifndef __NetBSD_Version__
     93 struct cfdriver cardbus_cd = {
     94 	NULL, "cardbus", DV_DULL
     95 };
     96 #endif
     97 
     98 
     99 STATIC int
    100 #if defined __BROKEN_INDIRECT_CONFIG
    101 cardbusmatch(parent, match, aux)
    102      struct device *parent;
    103      void *match;
    104      void *aux;
    105 #else
    106 cardbusmatch(parent, cf, aux)
    107      struct device *parent;
    108      struct cfdata *cf;
    109      void *aux;
    110 #endif
    111 {
    112 #if defined __BROKEN_INDIRECT_CONFIG
    113   struct cfdata *cf = match;
    114 #endif
    115   struct cbslot_attach_args *cba = aux;
    116 
    117   if (strcmp(cba->cba_busname, cf->cf_driver->cd_name)) {
    118     DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
    119 	     cba->cba_busname, cf->cf_driver->cd_name));
    120     return 0;
    121   }
    122 
    123 #if 0
    124   /* which function? */
    125   if (cf->cbslotcf_func != CBSLOT_UNK_FUNC &&
    126       cf->cbslotcf_func != cba->cba_function) {
    127     DPRINTF(("cardbusmatch: function differs %d <=> %d\n",
    128 	     cf->cbslotcf_func, cba->cba_function));
    129     return 0;
    130   }
    131 #endif
    132 
    133   if (cba->cba_function < 0 || cba->cba_function > 255) {
    134     return 0;
    135   }
    136 
    137   return 1;
    138 }
    139 
    140 
    141 
    142 STATIC void
    143 cardbusattach(parent, self, aux)
    144      struct device *parent;
    145      struct device *self;
    146      void *aux;
    147 {
    148   struct cardbus_softc *sc = (void *)self;
    149   struct cbslot_attach_args *cba = aux;
    150   int cdstatus;
    151 
    152   sc->sc_bus = cba->cba_bus;
    153   sc->sc_device = cba->cba_function;
    154   sc->sc_intrline = cba->cba_intrline;
    155   sc->sc_cacheline = cba->cba_cacheline;
    156   sc->sc_lattimer = cba->cba_lattimer;
    157 
    158   printf(": bus %d device %d", sc->sc_bus, sc->sc_device);
    159   printf(" cacheline 0x%x, lattimer 0x%x\n", sc->sc_cacheline,sc->sc_lattimer);
    160 
    161   sc->sc_iot = cba->cba_iot;	/* CardBus I/O space tag */
    162   sc->sc_memt = cba->cba_memt;	/* CardBus MEM space tag */
    163   sc->sc_dmat = cba->cba_dmat;	/* DMA tag */
    164   sc->sc_cc = cba->cba_cc;
    165   sc->sc_cf = cba->cba_cf;
    166 
    167 #if rbus
    168   sc->sc_rbus_iot = cba->cba_rbus_iot;
    169   sc->sc_rbus_memt = cba->cba_rbus_memt;
    170 #endif
    171 
    172   sc->sc_funcs = NULL;
    173 
    174   cdstatus = 0;
    175 }
    176 
    177 static int
    178 cardbus_read_tuples(ca, cis_ptr, tuples, len)
    179      struct cardbus_attach_args *ca;
    180      cardbusreg_t cis_ptr;
    181      u_int8_t *tuples;
    182      size_t len;
    183 {
    184     struct cardbus_softc *sc = ca->ca_ct->ct_sc;
    185     cardbus_chipset_tag_t cc = ca->ca_ct->ct_cc;
    186     cardbus_function_tag_t cf = ca->ca_ct->ct_cf;
    187     cardbustag_t tag = ca->ca_tag;
    188     cardbusreg_t command;
    189     int found = 0;
    190 
    191     int i, j;
    192     int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK;
    193     bus_space_handle_t bar_memh;
    194     bus_size_t bar_size;
    195     bus_addr_t bar_addr;
    196 
    197     int reg;
    198 
    199     memset(tuples, 0, len);
    200 
    201     cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK;
    202 
    203     switch(cardbus_space) {
    204     case CARDBUS_CIS_ASI_TUPLE:
    205 	DPRINTF(("%s: reading CIS data from configuration space\n",
    206 		 sc->sc_dev.dv_xname));
    207 	for (i = cis_ptr, j = 0; i < 0xff; i += 4) {
    208 	    u_int32_t e = (cf->cardbus_conf_read)(cc, tag, i);
    209 	    tuples[j] = 0xff & e;
    210 	    e >>= 8;
    211 	    tuples[j + 1] = 0xff & e;
    212 	    e >>= 8;
    213 	    tuples[j + 2] = 0xff & e;
    214 	    e >>= 8;
    215 	    tuples[j + 3] = 0xff & e;
    216 	    j += 4;
    217 	}
    218 	found++;
    219 	break;
    220 
    221     case CARDBUS_CIS_ASI_BAR0:
    222     case CARDBUS_CIS_ASI_BAR1:
    223     case CARDBUS_CIS_ASI_BAR2:
    224     case CARDBUS_CIS_ASI_BAR3:
    225     case CARDBUS_CIS_ASI_BAR4:
    226     case CARDBUS_CIS_ASI_BAR5:
    227     case CARDBUS_CIS_ASI_ROM:
    228 	if(cardbus_space == CARDBUS_CIS_ASI_ROM) {
    229 	    reg = CARDBUS_ROM_REG;
    230 	    DPRINTF(("%s: reading CIS data from ROM\n",
    231 		     sc->sc_dev.dv_xname));
    232 	} else {
    233 	    reg = CARDBUS_BASE0_REG + (cardbus_space - 1) * 4;
    234 	    DPRINTF(("%s: reading CIS data from BAR%d\n",
    235 		     sc->sc_dev.dv_xname, cardbus_space - 1));
    236 	}
    237 
    238 	/* XXX zero register so mapreg_map doesn't get confused by old
    239            contents */
    240 	cardbus_conf_write(cc, cf, tag, reg, 0);
    241 	if(Cardbus_mapreg_map(ca->ca_ct, reg,
    242 			      CARDBUS_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    243 			      0,
    244 			      NULL, &bar_memh, &bar_addr, &bar_size)) {
    245 	    printf("%s: failed to map memory\n", sc->sc_dev.dv_xname);
    246 	    return 1;
    247 	}
    248 
    249 
    250 	if(cardbus_space == CARDBUS_CIS_ASI_ROM) {
    251 	    cardbusreg_t exrom;
    252 	    int save;
    253 	    struct cardbus_rom_image_head rom_image;
    254 	    struct cardbus_rom_image *p;
    255 
    256 	    save = splhigh();
    257 	    /* enable rom address decoder */
    258 	    exrom = cardbus_conf_read(cc, cf, tag, reg);
    259 	    cardbus_conf_write(cc, cf, tag, reg, exrom | 1);
    260 
    261 	    command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    262 	    cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
    263 			       command | CARDBUS_COMMAND_MEM_ENABLE);
    264 
    265 	    printf("reg = %x\n", reg);
    266 	    printf("bar_addr = %lx, exrom = %x\n", bar_addr, exrom);
    267 	    printf("exrom = %x\n", cardbus_conf_read(cc, cf, tag, reg));
    268 
    269 	    if(cardbus_read_exrom(ca->ca_memt, bar_memh, &rom_image))
    270 		goto out;
    271 
    272 	    for(p = SIMPLEQ_FIRST(&rom_image);
    273 		p;
    274 		p = SIMPLEQ_NEXT(p, next)) {
    275 		if(p->rom_image == CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
    276 		    bus_space_read_region_1(p->romt, p->romh,
    277 					    CARDBUS_CIS_ADDR(cis_ptr),
    278 					    tuples, 256);
    279 		    found++;
    280 		}
    281 		break;
    282 	    }
    283 	    while((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
    284 		SIMPLEQ_REMOVE_HEAD(&rom_image, p, next);
    285 		free(p, M_DEVBUF);
    286 	    }
    287 	out:
    288 	    exrom = cardbus_conf_read(cc, cf, tag, reg);
    289 	    cardbus_conf_write(cc, cf, tag, reg, exrom & ~1);
    290 	    splx(save);
    291 	} else {
    292 	    command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    293 	    cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
    294 			       command | CARDBUS_COMMAND_MEM_ENABLE);
    295 	    /* XXX byte order? */
    296 	    bus_space_read_region_1(ca->ca_memt, bar_memh,
    297 				    cis_ptr, tuples, 256);
    298 	    found++;
    299 	}
    300 	command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    301 	cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
    302 			   command & ~CARDBUS_COMMAND_MEM_ENABLE);
    303 	cardbus_conf_write(cc, cf, tag, reg, 0);
    304 #if 0
    305 	/* XXX unmap memory */
    306 	(*ca->ca_ct->ct_cf->cardbus_space_free)(ca->ca_ct,
    307 						ca->ca_ct->ct_sc->sc_rbus_memt,
    308 						bar_memh, bar_size);
    309 #endif
    310 	break;
    311 
    312 #ifdef DIAGNOSTIC
    313     default:
    314 	panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname, cardbus_space);
    315 #endif
    316     }
    317     return !found;
    318 }
    319 
    320 /*
    321  * int cardbus_attach_card(struct cardbus_softc *sc)
    322  *
    323  *    This function attaches the card on the slot: turns on power,
    324  *    reads and analyses tuple, sets consifuration index.
    325  *
    326  *    This function returns the number of recognised device functions.
    327  *    If no functions are recognised, return 0.
    328  */
    329 int
    330 cardbus_attach_card(sc)
    331      struct cardbus_softc *sc;
    332 {
    333   cardbus_chipset_tag_t cc;
    334   cardbus_function_tag_t cf;
    335   int cdstatus;
    336   cardbustag_t tag;
    337   cardbusreg_t id, class, cis_ptr;
    338   cardbusreg_t bhlc;
    339   u_int8_t tuple[2048];
    340   int function, nfunction;
    341   struct cardbus_devfunc **previous_next = &(sc->sc_funcs);
    342   struct device *csc;
    343   int no_work_funcs = 0;
    344   cardbus_devfunc_t ct;
    345 
    346   cc = sc->sc_cc;
    347   cf = sc->sc_cf;
    348 
    349   DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
    350 
    351   /* inspect initial voltage */
    352   if (0 == (cdstatus = (cf->cardbus_ctrl)(cc, CARDBUS_CD))) {
    353     DPRINTF(("cardbusattach: no CardBus card on cb%d\n", sc->sc_dev.dv_unit));
    354     return 0;
    355   }
    356 
    357   enable_function(sc, cdstatus, 8); /* XXX use fake function 8 to
    358 				       keep power on during whole
    359 				       configuration */
    360   function = 0;
    361 
    362   tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
    363 
    364   /*
    365    * Wait until power comes up.  Maxmum 500 ms.
    366    */
    367   {
    368     int i;
    369     for (i = 0; i < 5; ++i) {
    370       id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    371       if (id != 0xffffffff && id != 0) {
    372 	break;
    373       }
    374       delay(100*1000);		/* or tsleep */
    375     }
    376     if (i == 5) {
    377       return 0;
    378     }
    379   }
    380 
    381   bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    382   if (CARDBUS_LATTIMER(bhlc) < 0x10) {
    383     bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    384     bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
    385     cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    386   }
    387 
    388   nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
    389 
    390   for(function = 0; function < nfunction; function++) {
    391       struct cardbus_attach_args ca;
    392 
    393       tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
    394 
    395       id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    396       class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
    397       cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
    398 
    399       /* Invalid vendor ID value? */
    400       if (CARDBUS_VENDOR(id) == 0xffff)
    401 	  continue;
    402 
    403       DPRINTF(("cardbus_attach_card: Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
    404 	       CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
    405 
    406       enable_function(sc, cdstatus, function);
    407 
    408       /* we need to allocate the ct here, since we might
    409 	 need it when reading the CIS */
    410       if (NULL == (ct = (cardbus_devfunc_t)malloc(sizeof(struct cardbus_devfunc),
    411 						  M_DEVBUF, M_NOWAIT))) {
    412 	  panic("no room for cardbus_tag");
    413       }
    414 
    415       ct->ct_cc = sc->sc_cc;
    416       ct->ct_cf = sc->sc_cf;
    417       ct->ct_bus = sc->sc_bus;
    418       ct->ct_dev = sc->sc_device;
    419       ct->ct_func = function;
    420       ct->ct_sc = sc;
    421       ct->ct_next = NULL;
    422       *previous_next = ct;
    423 
    424       ca.ca_unit = sc->sc_dev.dv_unit;
    425       ca.ca_ct = ct;
    426 
    427       ca.ca_iot = sc->sc_iot;
    428       ca.ca_memt = sc->sc_memt;
    429       ca.ca_dmat = sc->sc_dmat;
    430 
    431       ca.ca_tag = tag;
    432       ca.ca_device = sc->sc_device;
    433       ca.ca_function = function;
    434       ca.ca_id = id;
    435       ca.ca_class = class;
    436 
    437       ca.ca_intrline = sc->sc_intrline;
    438 
    439       bzero(tuple, 2048);
    440 
    441       if(cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
    442 	  printf("cardbus_attach_card: failed to read CIS\n");
    443 	  free(ct, M_DEVBUF);
    444 	  disable_function(sc, function);
    445 	  continue;
    446       }
    447 
    448       decode_tuples(tuple, 2048);
    449 
    450 
    451       if (NULL == (csc = config_found_sm((void *)sc, &ca, cardbusprint, cardbussubmatch))) {
    452 	  /* do not match */
    453 	  disable_function(sc, function);
    454 	  free(ct, M_DEVBUF);
    455 	  *previous_next = NULL;
    456       } else {
    457 	  /* found */
    458 	  previous_next = &(ct->ct_next);
    459 	  ct->ct_device = csc;
    460 	  ++no_work_funcs;
    461       }
    462   }
    463   /* XXX power down pseudo function 8 (this will power down the card
    464      if no functions were attached) */
    465   disable_function(sc, 8);
    466 
    467   if(no_work_funcs == 0)
    468       free(cc, M_DEVBUF);
    469   return no_work_funcs;
    470 }
    471 
    472 
    473 static int
    474 #ifdef __BROKEN_INDIRECT_CONFIG
    475 cardbussubmatch(parent, match, aux)
    476 #else
    477 cardbussubmatch(parent, cf, aux)
    478 #endif
    479      struct device *parent;
    480 #ifdef __BROKEN_INDIRECT_CONFIG
    481      void *match;
    482 #else
    483      struct cfdata *cf;
    484 #endif
    485      void *aux;
    486 {
    487 #ifdef __BROKEN_INDIRECT_CONFIG
    488   struct cfdata *cf = match;
    489 #endif
    490   struct cardbus_attach_args *ca = aux;
    491 
    492   if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
    493       cf->cardbuscf_dev != ca->ca_unit) {
    494     return 0;
    495   }
    496   if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
    497       cf->cardbuscf_function != ca->ca_function) {
    498     return 0;
    499   }
    500 
    501   return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    502 }
    503 
    504 
    505 
    506 static int
    507 cardbusprint(aux, pnp)
    508      void *aux;
    509      const char *pnp;
    510 {
    511   register struct cardbus_attach_args *ca = aux;
    512   char devinfo[256];
    513 
    514   if (pnp) {
    515     pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo);
    516     printf("%s at %s", devinfo, pnp);
    517   }
    518   printf(" dev %d function %d", ca->ca_device, ca->ca_function);
    519 
    520   return UNCONF;
    521 }
    522 
    523 
    524 
    525 
    526 
    527 
    528 /*
    529  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    530  *   Interrupt handler of pccard.
    531  *  args:
    532  *   cardbus_chipset_tag_t *cc
    533  *   int irq:
    534  */
    535 void *
    536 cardbus_intr_establish(cc, cf, irq, level, func, arg)
    537      cardbus_chipset_tag_t cc;
    538      cardbus_function_tag_t cf;
    539      cardbus_intr_handle_t irq;
    540      int level;
    541      int (*func) __P((void *));
    542      void *arg;
    543 {
    544   DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    545 
    546   return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg);
    547 }
    548 
    549 
    550 
    551 /*
    552  * void cardbus_intr_disestablish(cc, cf, handler)
    553  *   Interrupt handler of pccard.
    554  *  args:
    555  *   cardbus_chipset_tag_t *cc
    556  */
    557 void
    558 cardbus_intr_disestablish(cc, cf, handler)
    559      cardbus_chipset_tag_t cc;
    560      cardbus_function_tag_t cf;
    561      void *handler;
    562 {
    563   DPRINTF(("- pccard_intr_disestablish\n"));
    564 
    565  (*cf->cardbus_intr_disestablish)(cc, handler);
    566   return;
    567 }
    568 
    569 
    570 
    571 /* XXX this should be merged with cardbus_function_{enable,disable},
    572    but we don't have a ct when these functions are called */
    573 
    574 static void
    575 enable_function(sc, cdstatus, function)
    576      struct cardbus_softc *sc;
    577      int cdstatus;
    578      int function;
    579 {
    580     if(sc->sc_poweron_func == 0) {
    581 	if (cdstatus & CARDBUS_3V_CARD) {
    582 	    sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V);
    583 	}
    584 	(sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
    585     }
    586     sc->sc_poweron_func |= (1 << function);
    587 }
    588 
    589 static void
    590 disable_function(sc, function)
    591      struct cardbus_softc *sc;
    592      int function;
    593 {
    594     sc->sc_poweron_func &= ~(1 << function);
    595     if(sc->sc_poweron_func == 0) {
    596 	/* power-off because no functions are enabled */
    597 	sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V);
    598     }
    599 }
    600 
    601 /*
    602  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
    603  *
    604  *   This function enables a function on a card.  When no power is
    605  *  applied on the card, power will be applied on it.
    606  */
    607 int
    608 cardbus_function_enable(sc, func)
    609      struct cardbus_softc *sc;
    610      int func;
    611 {
    612   cardbus_chipset_tag_t cc = sc->sc_cc;
    613   cardbus_function_tag_t cf = sc->sc_cf;
    614   cardbusreg_t command;
    615   cardbustag_t tag;
    616 
    617   DPRINTF(("entering cardbus_function_enable...  "));
    618 
    619   /* entering critical area */
    620 
    621   enable_function(sc, CARDBUS_3V_CARD, func); /* XXX: sc_vold should be used */
    622 
    623   /* exiting critical area */
    624 
    625   tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
    626 
    627   command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    628   command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    629 
    630   cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    631 
    632   cardbus_free_tag(cc, cf, tag);
    633 
    634   DPRINTF(("%x\n", sc->sc_poweron_func));
    635 
    636   return 0;
    637 }
    638 
    639 
    640 /*
    641  * int cardbus_function_disable(struct cardbus_softc *, int func)
    642  *
    643  *   This function disable a function on a card.  When no functions are
    644  *  enabled, it turns off the power.
    645  */
    646 int
    647 cardbus_function_disable(sc, func)
    648      struct cardbus_softc *sc;
    649      int func;
    650 {
    651 
    652   DPRINTF(("entering cardbus_function_disable...  "));
    653 
    654   disable_function(sc, func);
    655 
    656   return 0;
    657 }
    658 
    659 
    660 
    661 
    662 
    663 
    664 
    665 /*
    666  * below this line, there are some functions for decoding tuples.
    667  * They should go out from this file.
    668  */
    669 
    670 static u_int8_t *decode_tuple __P((u_int8_t *));
    671 #ifdef CARDBUS_DEBUG
    672 static char *tuple_name __P((int));
    673 #endif
    674 
    675 static int
    676 decode_tuples(tuple, buflen)
    677      u_int8_t *tuple;
    678      int buflen;
    679 {
    680   u_int8_t *tp = tuple;
    681 
    682   if (CISTPL_LINKTARGET != *tuple) {
    683     DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    684     return 0;
    685   }
    686 
    687   while (NULL != (tp = decode_tuple(tp))) {
    688     if (tuple + buflen < tp) {
    689       break;
    690     }
    691   }
    692 
    693   return 1;
    694 }
    695 
    696 
    697 static u_int8_t *
    698 decode_tuple(tuple)
    699      u_int8_t *tuple;
    700 {
    701   u_int8_t type;
    702   u_int8_t len;
    703 #ifdef CARDBUS_DEBUG
    704   int i;
    705 #endif
    706 
    707   type = tuple[0];
    708   len = tuple[1] + 2;
    709 
    710 #ifdef CARDBUS_DEBUG
    711   printf("tuple: %s len %d\n", tuple_name(type), len);
    712 #endif
    713   if (CISTPL_END == type) {
    714     return NULL;
    715   }
    716 
    717 #ifdef CARDBUS_DEBUG
    718   for (i = 0; i < len; ++i) {
    719     if (i % 16 == 0) {
    720       printf("  0x%2x:", i);
    721     }
    722     printf(" %x",tuple[i]);
    723     if (i % 16 == 15) {
    724       printf("\n");
    725     }
    726   }
    727   if (i % 16 != 0) {
    728     printf("\n");
    729   }
    730 #endif
    731 
    732   return tuple + len;
    733 }
    734 
    735 
    736 #ifdef CARDBUS_DEBUG
    737 static char *
    738 tuple_name(type)
    739      int type;
    740 {
    741   static char *tuple_name_s [] = {
    742     "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
    743     "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
    744     "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
    745     "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
    746     "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET",	/* 10-13 */
    747     "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
    748     "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
    749     "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
    750     "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
    751     "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
    752     "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
    753     "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
    754     "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
    755     "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
    756     "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
    757     "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
    758     "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
    759     "DATE", "BATTERY", "ORG"
    760   };
    761 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
    762 
    763   if (type > 0 && type < NAME_LEN(tuple_name_s)) {
    764     return tuple_name_s[type];
    765   } else if (0xff == type) {
    766     return "END";
    767   } else {
    768     return "Reserved";
    769   }
    770 }
    771 #endif
    772