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