Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.6
      1 /*	$NetBSD: cardbus.c,v 1.6 1999/10/29 07:29:08 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/pci/pcivar.h>	/* XXX */
     51 #include <dev/pci/pcireg.h>	/* XXX */
     52 
     53 #if defined CARDBUS_DEBUG
     54 #define STATIC
     55 #define DPRINTF(a) printf a
     56 #define DDELAY(x) delay((x)*1000*1000)
     57 #else
     58 #define STATIC static
     59 #define DPRINTF(a)
     60 #endif
     61 
     62 
     63 
     64 STATIC void cardbusattach __P((struct device *, struct device *, void *));
     65 /* STATIC int cardbusprint __P((void *, const char *)); */
     66 int cardbus_attach_card __P((struct cardbus_softc *));
     67 
     68 #if !defined __BROKEN_INDIRECT_CONFIG
     69 STATIC int cardbusmatch __P((struct device *, struct cfdata *, void *));
     70 static int cardbussubmatch __P((struct device *, struct cfdata *, void *));
     71 #else
     72 STATIC int cardbusmatch __P((struct device *, void *, void *));
     73 static int cardbussubmatch __P((struct device *, void *, void *));
     74 #endif
     75 static int cardbusprint __P((void *, const char *));
     76 
     77 static int decode_tuples __P((u_int8_t *, int));
     78 
     79 
     80 struct cfattach cardbus_ca = {
     81 	sizeof(struct cardbus_softc), cardbusmatch, cardbusattach
     82 };
     83 
     84 #ifndef __NetBSD_Version__
     85 struct cfdriver cardbus_cd = {
     86 	NULL, "cardbus", DV_DULL
     87 };
     88 #endif
     89 
     90 
     91 STATIC int
     92 #if defined __BROKEN_INDIRECT_CONFIG
     93 cardbusmatch(parent, match, aux)
     94      struct device *parent;
     95      void *match;
     96      void *aux;
     97 #else
     98 cardbusmatch(parent, cf, aux)
     99      struct device *parent;
    100      struct cfdata *cf;
    101      void *aux;
    102 #endif
    103 {
    104 #if defined __BROKEN_INDIRECT_CONFIG
    105   struct cfdata *cf = match;
    106 #endif
    107   struct cbslot_attach_args *cba = aux;
    108 
    109   if (strcmp(cba->cba_busname, cf->cf_driver->cd_name)) {
    110     DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
    111 	     cba->cba_busname, cf->cf_driver->cd_name));
    112     return 0;
    113   }
    114 
    115 #if 0
    116   /* which function? */
    117   if (cf->cbslotcf_func != CBSLOT_UNK_FUNC &&
    118       cf->cbslotcf_func != cba->cba_function) {
    119     DPRINTF(("cardbusmatch: function differs %d <=> %d\n",
    120 	     cf->cbslotcf_func, cba->cba_function));
    121     return 0;
    122   }
    123 #endif
    124 
    125   if (cba->cba_function < 0 || cba->cba_function > 255) {
    126     return 0;
    127   }
    128 
    129   return 1;
    130 }
    131 
    132 
    133 
    134 STATIC void
    135 cardbusattach(parent, self, aux)
    136      struct device *parent;
    137      struct device *self;
    138      void *aux;
    139 {
    140   struct cardbus_softc *sc = (void *)self;
    141   struct cbslot_attach_args *cba = aux;
    142   int cdstatus;
    143 
    144   sc->sc_bus = cba->cba_bus;
    145   sc->sc_device = cba->cba_function;
    146   sc->sc_intrline = cba->cba_intrline;
    147   sc->sc_cacheline = cba->cba_cacheline;
    148   sc->sc_lattimer = cba->cba_lattimer;
    149 
    150   printf(": bus %d device %d", sc->sc_bus, sc->sc_device);
    151   printf(" cacheline 0x%x, lattimer 0x%x\n", sc->sc_cacheline,sc->sc_lattimer);
    152 
    153   sc->sc_iot = cba->cba_iot;	/* CardBus I/O space tag */
    154   sc->sc_memt = cba->cba_memt;	/* CardBus MEM space tag */
    155   sc->sc_dmat = cba->cba_dmat;	/* DMA tag */
    156   sc->sc_cc = cba->cba_cc;
    157   sc->sc_cf = cba->cba_cf;
    158 
    159 #if rbus
    160   sc->sc_rbus_iot = cba->cba_rbus_iot;
    161   sc->sc_rbus_memt = cba->cba_rbus_memt;
    162 #endif
    163 
    164   sc->sc_funcs = NULL;
    165 
    166   cdstatus = 0;
    167 }
    168 
    169 
    170 
    171 
    172 /*
    173  * int cardbus_attach_card(struct cardbus_softc *sc)
    174  *
    175  *    This function attaches the card on the slot: turns on power,
    176  *    reads and analyses tuple, sets consifuration index.
    177  *
    178  *    This function returns the number of recognised device functions.
    179  *    If no functions are recognised, return 0.
    180  */
    181 int
    182 cardbus_attach_card(sc)
    183      struct cardbus_softc *sc;
    184 {
    185   cardbus_chipset_tag_t cc;
    186   cardbus_function_tag_t cf;
    187   int cdstatus;
    188   cardbustag_t tag;
    189   cardbusreg_t id, class, cis_ptr;
    190   cardbusreg_t bhlc;
    191   u_int8_t tuple[2048];
    192   int function, nfunction;
    193   struct cardbus_devfunc **previous_next = &(sc->sc_funcs);
    194   struct device *csc;
    195   int no_work_funcs = 0;
    196   cardbus_devfunc_t ct;
    197 
    198   cc = sc->sc_cc;
    199   cf = sc->sc_cf;
    200 
    201   DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
    202 
    203   /* inspect initial voltage */
    204   if (0 == (cdstatus = (cf->cardbus_ctrl)(cc, CARDBUS_CD))) {
    205     DPRINTF(("cardbusattach: no CardBus card on cb%d\n", sc->sc_dev.dv_unit));
    206     return 0;
    207   }
    208 
    209   if (cdstatus & CARDBUS_3V_CARD) {
    210     cf->cardbus_power(cc, CARDBUS_VCC_3V);
    211     sc->sc_poweron_func = 1;	/* function 0 on */
    212   }
    213 
    214   (cf->cardbus_ctrl)(cc, CARDBUS_RESET);
    215 
    216   function = 0;
    217 
    218   tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
    219 
    220   /*
    221    * Wait until power comes up.  Maxmum 500 ms.
    222    */
    223   {
    224     int i;
    225     for (i = 0; i < 5; ++i) {
    226       id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    227       if (id != 0xffffffff && id != 0) {
    228 	break;
    229       }
    230       delay(100*1000);		/* or tsleep */
    231     }
    232     if (i == 5) {
    233       return 0;
    234     }
    235   }
    236 
    237   bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    238   if (CARDBUS_LATTIMER(bhlc) < 0x10) {
    239     bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    240     bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
    241     cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    242   }
    243 
    244   nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
    245 
    246   /*
    247    *           XXX multi-function card
    248    *
    249    * I don't know how to process CIS information for
    250    * multi-function cards.
    251    */
    252 
    253   id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    254   class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
    255   cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
    256 
    257   DPRINTF(("cardbus_attach_card: Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
    258 	   CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
    259 
    260   bzero(tuple, 2048);
    261 
    262   if (CARDBUS_CIS_ASI_TUPLE == (CARDBUS_CIS_ASI(cis_ptr))) {
    263 				/* Tuple is in Cardbus config space */
    264     int i = cis_ptr & CARDBUS_CIS_ADDRMASK;
    265     int j = 0;
    266 
    267     for (; i < 0xff; i += 4) {
    268       u_int32_t e = (cf->cardbus_conf_read)(cc, tag, i);
    269       tuple[j] = 0xff & e;
    270       e >>= 8;
    271       tuple[j + 1] = 0xff & e;
    272       e >>= 8;
    273       tuple[j + 2] = 0xff & e;
    274       e >>= 8;
    275       tuple[j + 3] = 0xff & e;
    276       j += 4;
    277     }
    278   } else if (CARDBUS_CIS_ASI(cis_ptr) <= CARDBUS_CIS_ASI_BAR5) {
    279     /*    int bar = CARDBUS_CIS_ASI_BAR(cis_ptr);*/
    280   }
    281 
    282 
    283   decode_tuples(tuple, 2048);
    284 
    285   {
    286     struct cardbus_attach_args ca;
    287 
    288     if (NULL == (ct = (cardbus_devfunc_t)malloc(sizeof(struct cardbus_devfunc),
    289 						M_DEVBUF, M_NOWAIT))) {
    290       panic("no room for cardbus_tag");
    291     }
    292 
    293     ct->ct_cc = sc->sc_cc;
    294     ct->ct_cf = sc->sc_cf;
    295     ct->ct_bus = sc->sc_bus;
    296     ct->ct_dev = sc->sc_device;
    297     ct->ct_func = function;
    298     ct->ct_sc = sc;
    299     ct->ct_next = NULL;
    300     *previous_next = ct;
    301 
    302     ca.ca_unit = sc->sc_dev.dv_unit;
    303     ca.ca_ct = ct;
    304 
    305     ca.ca_iot = sc->sc_iot;
    306     ca.ca_memt = sc->sc_memt;
    307     ca.ca_dmat = sc->sc_dmat;
    308 
    309     ca.ca_tag = tag;
    310     ca.ca_device = sc->sc_device;
    311     ca.ca_function = function;
    312     ca.ca_id = id;
    313     ca.ca_class = class;
    314 
    315     ca.ca_intrline = sc->sc_intrline;
    316 
    317     if (NULL == (csc = config_found_sm((void *)sc, &ca, cardbusprint, cardbussubmatch))) {
    318       /* do not match */
    319       cf->cardbus_power(cc, CARDBUS_VCC_0V);
    320       sc->sc_poweron_func = 0;	/* no functions on */
    321       free(cc, M_DEVBUF);
    322     } else {
    323       /* found */
    324       previous_next = &(ct->ct_next);
    325       ct->ct_device = csc;
    326       ++no_work_funcs;
    327     }
    328   }
    329 
    330   return no_work_funcs;
    331 }
    332 
    333 
    334 static int
    335 #ifdef __BROKEN_INDIRECT_CONFIG
    336 cardbussubmatch(parent, match, aux)
    337 #else
    338 cardbussubmatch(parent, cf, aux)
    339 #endif
    340      struct device *parent;
    341 #ifdef __BROKEN_INDIRECT_CONFIG
    342      void *match;
    343 #else
    344      struct cfdata *cf;
    345 #endif
    346      void *aux;
    347 {
    348 #ifdef __BROKEN_INDIRECT_CONFIG
    349   struct cfdata *cf = match;
    350 #endif
    351   struct cardbus_attach_args *ca = aux;
    352 
    353   if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
    354       cf->cardbuscf_dev != ca->ca_unit) {
    355     return 0;
    356   }
    357   if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
    358       cf->cardbuscf_function != ca->ca_function) {
    359     return 0;
    360   }
    361 
    362   return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    363 }
    364 
    365 
    366 
    367 static int
    368 cardbusprint(aux, pnp)
    369      void *aux;
    370      const char *pnp;
    371 {
    372   register struct cardbus_attach_args *ca = aux;
    373   char devinfo[256];
    374 
    375   if (pnp) {
    376     pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo);
    377     printf("%s at %s", devinfo, pnp);
    378   }
    379   printf(" dev %d function %d", ca->ca_device, ca->ca_function);
    380 
    381   return UNCONF;
    382 }
    383 
    384 
    385 
    386 
    387 
    388 
    389 /*
    390  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    391  *   Interrupt handler of pccard.
    392  *  args:
    393  *   cardbus_chipset_tag_t *cc
    394  *   int irq:
    395  */
    396 void *
    397 cardbus_intr_establish(cc, cf, irq, level, func, arg)
    398      cardbus_chipset_tag_t cc;
    399      cardbus_function_tag_t cf;
    400      cardbus_intr_handle_t irq;
    401      int level;
    402      int (*func) __P((void *));
    403      void *arg;
    404 {
    405   DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    406 
    407   return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg);
    408 }
    409 
    410 
    411 
    412 /*
    413  * void cardbus_intr_disestablish(cc, cf, handler)
    414  *   Interrupt handler of pccard.
    415  *  args:
    416  *   cardbus_chipset_tag_t *cc
    417  */
    418 void
    419 cardbus_intr_disestablish(cc, cf, handler)
    420      cardbus_chipset_tag_t cc;
    421      cardbus_function_tag_t cf;
    422      void *handler;
    423 {
    424   DPRINTF(("- pccard_intr_disestablish\n"));
    425 
    426  (*cf->cardbus_intr_disestablish)(cc, handler);
    427   return;
    428 }
    429 
    430 
    431 
    432 /*
    433  * int cardbus_function_enable(cardbus_devfunc_t ct)
    434  *
    435  *   This function enables a function on a card.  When no power is
    436  *  applied on the card, power will be applied on it.
    437  */
    438 int
    439 cardbus_function_enable(ct)
    440      cardbus_devfunc_t ct;
    441 {
    442   struct cardbus_softc *sc = ct->ct_sc;
    443   int func = ct->ct_func;
    444   cardbus_chipset_tag_t cc = sc->sc_cc;
    445   cardbus_function_tag_t cf = sc->sc_cf;
    446   cardbusreg_t command;
    447   cardbustag_t tag;
    448 
    449   DPRINTF(("entering cardbus_function_enable...  "));
    450 
    451   /* entering critical area */
    452 
    453   if (sc->sc_poweron_func == 0) {
    454     /* no functions are enabled */
    455     (*cf->cardbus_power)(cc, CARDBUS_VCC_3V); /* XXX: sc_vold should be used */
    456     (*cf->cardbus_ctrl)(cc, CARDBUS_RESET);
    457   }
    458 
    459   sc->sc_poweron_func |= (1 << func);
    460 
    461   /* exiting critical area */
    462 
    463   tag = Cardbus_make_tag(ct);
    464   command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    465   command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    466   cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    467   Cardbus_free_tag(ct, tag);
    468 
    469 
    470   DPRINTF(("%x\n", sc->sc_poweron_func));
    471 
    472   return 0;
    473 }
    474 
    475 
    476 /*
    477  * int cardbus_function_disable(cardbus_devfunc_t ct)
    478  *
    479  *   This function disable a function on a card.  When no functions are
    480  *  enabled, it turns off the power.
    481  */
    482 int
    483 cardbus_function_disable(ct)
    484      cardbus_devfunc_t ct;
    485 {
    486   struct cardbus_softc *sc = ct->ct_sc;
    487   int func = ct->ct_func;
    488   cardbus_chipset_tag_t cc = sc->sc_cc;
    489   cardbus_function_tag_t cf = sc->sc_cf;
    490 
    491   DPRINTF(("entering cardbus_enable_disable...  "));
    492 
    493   sc->sc_poweron_func &= ~(1 << func);
    494 
    495   DPRINTF(("%x\n", sc->sc_poweron_func));
    496 
    497   if (sc->sc_poweron_func == 0) {
    498     /* power-off because no functions are enabled */
    499     (*cf->cardbus_power)(cc, CARDBUS_VCC_0V);
    500   }
    501 
    502   return 0;
    503 }
    504 
    505 
    506 
    507 
    508 
    509 
    510 
    511 /*
    512  * below this line, there are some functions for decoding tuples.
    513  * They should go out from this file.
    514  */
    515 
    516 static u_int8_t *decode_tuple __P((u_int8_t *));
    517 #ifdef CARDBUS_DEBUG
    518 static char *tuple_name __P((int));
    519 #endif
    520 
    521 static int
    522 decode_tuples(tuple, buflen)
    523      u_int8_t *tuple;
    524      int buflen;
    525 {
    526   u_int8_t *tp = tuple;
    527 
    528   if (CISTPL_LINKTARGET != *tuple) {
    529     DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    530     return 0;
    531   }
    532 
    533   while (NULL != (tp = decode_tuple(tp))) {
    534     if (tuple + buflen < tp) {
    535       break;
    536     }
    537   }
    538 
    539   return 1;
    540 }
    541 
    542 
    543 static u_int8_t *
    544 decode_tuple(tuple)
    545      u_int8_t *tuple;
    546 {
    547   u_int8_t type;
    548   u_int8_t len;
    549 #ifdef CARDBUS_DEBUG
    550   int i;
    551 #endif
    552 
    553   type = tuple[0];
    554   len = tuple[1] + 2;
    555 
    556 #ifdef CARDBUS_DEBUG
    557   printf("tuple: %s len %d\n", tuple_name(type), len);
    558 #endif
    559   if (CISTPL_END == type) {
    560     return NULL;
    561   }
    562 
    563 #ifdef CARDBUS_DEBUG
    564   for (i = 0; i < len; ++i) {
    565     if (i % 16 == 0) {
    566       printf("  0x%2x:", i);
    567     }
    568     printf(" %x",tuple[i]);
    569     if (i % 16 == 15) {
    570       printf("\n");
    571     }
    572   }
    573   if (i % 16 != 0) {
    574     printf("\n");
    575   }
    576 #endif
    577 
    578   return tuple + len;
    579 }
    580 
    581 
    582 #ifdef CARDBUS_DEBUG
    583 static char *
    584 tuple_name(type)
    585      int type;
    586 {
    587   static char *tuple_name_s [] = {
    588     "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
    589     "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
    590     "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
    591     "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
    592     "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET",	/* 10-13 */
    593     "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
    594     "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
    595     "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
    596     "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
    597     "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
    598     "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
    599     "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
    600     "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
    601     "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
    602     "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
    603     "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
    604     "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
    605     "DATE", "BATTERY", "ORG"
    606   };
    607 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
    608 
    609   if (type > 0 && type < NAME_LEN(tuple_name_s)) {
    610     return tuple_name_s[type];
    611   } else if (0xff == type) {
    612     return "END";
    613   } else {
    614     return "Reserved";
    615   }
    616 }
    617 #endif
    618