Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.55
      1  1.55  drochner /*	$NetBSD: cardbus.c,v 1.55 2004/08/23 18:21:51 drochner Exp $	*/
      2   1.1      haya 
      3   1.1      haya /*
      4  1.18      haya  * Copyright (c) 1997, 1998, 1999 and 2000
      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.37     lukem 
     35  1.37     lukem #include <sys/cdefs.h>
     36  1.55  drochner __KERNEL_RCSID(0, "$NetBSD: cardbus.c,v 1.55 2004/08/23 18:21:51 drochner Exp $");
     37   1.1      haya 
     38   1.4      haya #include "opt_cardbus.h"
     39   1.1      haya 
     40   1.1      haya #include <sys/param.h>
     41   1.1      haya #include <sys/systm.h>
     42   1.1      haya #include <sys/device.h>
     43   1.1      haya #include <sys/malloc.h>
     44   1.1      haya #include <sys/kernel.h>
     45   1.1      haya #include <sys/syslog.h>
     46  1.19      haya #include <sys/proc.h>
     47  1.36  augustss #include <sys/reboot.h>		/* for AB_* needed by bootverbose */
     48   1.1      haya 
     49   1.1      haya #include <machine/bus.h>
     50   1.1      haya 
     51   1.1      haya #include <dev/cardbus/cardbusvar.h>
     52  1.51   mycroft #include <dev/pci/pcidevs.h>
     53   1.1      haya 
     54   1.7      joda #include <dev/cardbus/cardbus_exrom.h>
     55   1.7      joda 
     56   1.1      haya #include <dev/pci/pcivar.h>	/* XXX */
     57   1.1      haya #include <dev/pci/pcireg.h>	/* XXX */
     58   1.1      haya 
     59  1.20     soren #include <dev/pcmcia/pcmciareg.h>
     60  1.10      joda 
     61  1.55  drochner #include "locators.h"
     62  1.55  drochner 
     63   1.1      haya #if defined CARDBUS_DEBUG
     64   1.1      haya #define STATIC
     65   1.1      haya #define DPRINTF(a) printf a
     66   1.1      haya #else
     67   1.1      haya #define STATIC static
     68   1.1      haya #define DPRINTF(a)
     69   1.1      haya #endif
     70   1.1      haya 
     71   1.1      haya 
     72  1.29     enami STATIC void cardbusattach(struct device *, struct device *, void *);
     73  1.29     enami STATIC int cardbusmatch(struct device *, struct cfdata *, void *);
     74  1.52  drochner int cardbus_rescan(struct device *, const char *, const int *);
     75  1.52  drochner void cardbus_childdetached(struct device *, struct device *);
     76  1.52  drochner static int cardbussubmatch(struct device *, struct cfdata *,
     77  1.52  drochner 			   const locdesc_t *, void *);
     78  1.29     enami static int cardbusprint(void *, const char *);
     79   1.1      haya 
     80  1.10      joda typedef void (*tuple_decode_func)(u_int8_t*, int, void*);
     81  1.10      joda 
     82  1.29     enami static int decode_tuples(u_int8_t *, int, tuple_decode_func, void*);
     83  1.10      joda #ifdef CARDBUS_DEBUG
     84  1.29     enami static void print_tuple(u_int8_t*, int, void*);
     85  1.10      joda #endif
     86   1.1      haya 
     87  1.29     enami static int cardbus_read_tuples(struct cardbus_attach_args *,
     88  1.29     enami     cardbusreg_t, u_int8_t *, size_t);
     89   1.8      joda 
     90  1.29     enami static void enable_function(struct cardbus_softc *, int, int);
     91  1.29     enami static void disable_function(struct cardbus_softc *, int);
     92   1.1      haya 
     93  1.52  drochner CFATTACH_DECL2(cardbus, sizeof(struct cardbus_softc),
     94  1.52  drochner     cardbusmatch, cardbusattach, NULL, NULL,
     95  1.52  drochner     cardbus_rescan, cardbus_childdetached);
     96   1.1      haya 
     97   1.1      haya #ifndef __NetBSD_Version__
     98   1.1      haya struct cfdriver cardbus_cd = {
     99   1.1      haya 	NULL, "cardbus", DV_DULL
    100   1.1      haya };
    101   1.1      haya #endif
    102   1.1      haya 
    103   1.1      haya 
    104   1.1      haya STATIC int
    105  1.29     enami cardbusmatch(struct device *parent, struct cfdata *cf, void *aux)
    106  1.29     enami {
    107  1.29     enami 	struct cbslot_attach_args *cba = aux;
    108   1.1      haya 
    109  1.42   thorpej 	if (strcmp(cba->cba_busname, cf->cf_name)) {
    110  1.29     enami 		DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
    111  1.42   thorpej 		    cba->cba_busname, cf->cf_name));
    112  1.29     enami 		return (0);
    113  1.29     enami 	}
    114  1.29     enami 
    115  1.29     enami 	return (1);
    116   1.1      haya }
    117   1.1      haya 
    118  1.29     enami STATIC void
    119  1.29     enami cardbusattach(struct device *parent, struct device *self, void *aux)
    120  1.29     enami {
    121  1.29     enami 	struct cardbus_softc *sc = (void *)self;
    122  1.29     enami 	struct cbslot_attach_args *cba = aux;
    123   1.1      haya 
    124  1.29     enami 	sc->sc_bus = cba->cba_bus;
    125  1.29     enami 	sc->sc_device = 0;
    126  1.29     enami 	sc->sc_intrline = cba->cba_intrline;
    127  1.29     enami 	sc->sc_cacheline = cba->cba_cacheline;
    128  1.29     enami 	sc->sc_lattimer = cba->cba_lattimer;
    129  1.29     enami 
    130  1.29     enami 	printf(": bus %d device %d", sc->sc_bus, sc->sc_device);
    131  1.36  augustss 	if (bootverbose)
    132  1.36  augustss 		printf(" cacheline 0x%x, lattimer 0x%x", sc->sc_cacheline,
    133  1.36  augustss 		       sc->sc_lattimer);
    134  1.36  augustss 	printf("\n");
    135  1.29     enami 
    136  1.29     enami 	sc->sc_iot = cba->cba_iot;	/* CardBus I/O space tag */
    137  1.29     enami 	sc->sc_memt = cba->cba_memt;	/* CardBus MEM space tag */
    138  1.29     enami 	sc->sc_dmat = cba->cba_dmat;	/* DMA tag */
    139  1.29     enami 	sc->sc_cc = cba->cba_cc;
    140  1.29     enami 	sc->sc_cf = cba->cba_cf;
    141   1.1      haya 
    142   1.1      haya #if rbus
    143  1.29     enami 	sc->sc_rbus_iot = cba->cba_rbus_iot;
    144  1.29     enami 	sc->sc_rbus_memt = cba->cba_rbus_memt;
    145   1.1      haya #endif
    146   1.1      haya }
    147   1.1      haya 
    148   1.7      joda static int
    149  1.29     enami cardbus_read_tuples(struct cardbus_attach_args *ca, cardbusreg_t cis_ptr,
    150  1.29     enami     u_int8_t *tuples, size_t len)
    151  1.29     enami {
    152  1.29     enami 	struct cardbus_softc *sc = ca->ca_ct->ct_sc;
    153  1.29     enami 	cardbus_chipset_tag_t cc = ca->ca_ct->ct_cc;
    154  1.29     enami 	cardbus_function_tag_t cf = ca->ca_ct->ct_cf;
    155  1.29     enami 	cardbustag_t tag = ca->ca_tag;
    156  1.29     enami 	cardbusreg_t command;
    157  1.30     enami 	bus_space_tag_t bar_tag;
    158  1.29     enami 	bus_space_handle_t bar_memh;
    159  1.29     enami 	bus_size_t bar_size;
    160  1.29     enami 	bus_addr_t bar_addr;
    161  1.29     enami 	cardbusreg_t reg;
    162  1.29     enami 	int found = 0;
    163  1.29     enami 	int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK;
    164  1.29     enami 	int i, j;
    165  1.29     enami 
    166  1.29     enami 	memset(tuples, 0, len);
    167  1.29     enami 
    168  1.29     enami 	cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK;
    169  1.29     enami 
    170  1.29     enami 	switch (cardbus_space) {
    171  1.29     enami 	case CARDBUS_CIS_ASI_TUPLE:
    172  1.29     enami 		DPRINTF(("%s: reading CIS data from configuration space\n",
    173  1.29     enami 		    sc->sc_dev.dv_xname));
    174  1.29     enami 		for (i = cis_ptr, j = 0; i < 0xff; i += 4) {
    175  1.29     enami 			u_int32_t e = (*cf->cardbus_conf_read)(cc, tag, i);
    176  1.29     enami 			tuples[j] = 0xff & e;
    177  1.29     enami 			e >>= 8;
    178  1.29     enami 			tuples[j + 1] = 0xff & e;
    179  1.29     enami 			e >>= 8;
    180  1.29     enami 			tuples[j + 2] = 0xff & e;
    181  1.29     enami 			e >>= 8;
    182  1.29     enami 			tuples[j + 3] = 0xff & e;
    183  1.29     enami 			j += 4;
    184  1.29     enami 		}
    185  1.29     enami 		found++;
    186  1.29     enami 		break;
    187  1.29     enami 
    188  1.29     enami 	case CARDBUS_CIS_ASI_BAR0:
    189  1.29     enami 	case CARDBUS_CIS_ASI_BAR1:
    190  1.29     enami 	case CARDBUS_CIS_ASI_BAR2:
    191  1.29     enami 	case CARDBUS_CIS_ASI_BAR3:
    192  1.29     enami 	case CARDBUS_CIS_ASI_BAR4:
    193  1.29     enami 	case CARDBUS_CIS_ASI_BAR5:
    194  1.29     enami 	case CARDBUS_CIS_ASI_ROM:
    195  1.29     enami 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
    196  1.29     enami 			reg = CARDBUS_ROM_REG;
    197  1.29     enami 			DPRINTF(("%s: reading CIS data from ROM\n",
    198  1.29     enami 			    sc->sc_dev.dv_xname));
    199  1.29     enami 		} else {
    200  1.29     enami 			reg = CARDBUS_BASE0_REG + (cardbus_space - 1) * 4;
    201  1.29     enami 			DPRINTF(("%s: reading CIS data from BAR%d\n",
    202  1.29     enami 			    sc->sc_dev.dv_xname, cardbus_space - 1));
    203  1.29     enami 		}
    204   1.7      joda 
    205  1.29     enami 		/*
    206  1.29     enami 		 * XXX zero register so mapreg_map doesn't get confused by old
    207  1.29     enami 		 * contents.
    208  1.29     enami 		 */
    209  1.29     enami 		cardbus_conf_write(cc, cf, tag, reg, 0);
    210  1.29     enami 		if (Cardbus_mapreg_map(ca->ca_ct, reg,
    211  1.29     enami 		    CARDBUS_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    212  1.30     enami 		    0, &bar_tag, &bar_memh, &bar_addr, &bar_size)) {
    213  1.29     enami 			printf("%s: failed to map memory\n",
    214  1.29     enami 			    sc->sc_dev.dv_xname);
    215  1.29     enami 			return (1);
    216  1.29     enami 		}
    217   1.7      joda 
    218  1.29     enami 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
    219  1.29     enami 			cardbusreg_t exrom;
    220  1.29     enami 			int save;
    221  1.29     enami 			struct cardbus_rom_image_head rom_image;
    222  1.29     enami 			struct cardbus_rom_image *p;
    223  1.29     enami 
    224  1.29     enami 			save = splhigh();
    225  1.29     enami 			/* enable rom address decoder */
    226  1.29     enami 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    227  1.29     enami 			cardbus_conf_write(cc, cf, tag, reg, exrom | 1);
    228  1.29     enami 
    229  1.29     enami 			command = cardbus_conf_read(cc, cf, tag,
    230  1.29     enami 			    CARDBUS_COMMAND_STATUS_REG);
    231  1.29     enami 			cardbus_conf_write(cc, cf, tag,
    232  1.29     enami 			    CARDBUS_COMMAND_STATUS_REG,
    233  1.29     enami 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    234  1.29     enami 
    235  1.29     enami 			if (cardbus_read_exrom(ca->ca_memt, bar_memh,
    236  1.29     enami 			    &rom_image))
    237  1.29     enami 				goto out;
    238  1.29     enami 
    239  1.41     lukem 			SIMPLEQ_FOREACH(p, &rom_image, next) {
    240  1.29     enami 				if (p->rom_image ==
    241  1.29     enami 				    CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
    242  1.29     enami 					bus_space_read_region_1(p->romt,
    243  1.29     enami 					    p->romh, CARDBUS_CIS_ADDR(cis_ptr),
    244   1.7      joda 					    tuples, 256);
    245  1.29     enami 					found++;
    246  1.29     enami 				}
    247  1.29     enami 				break;
    248  1.29     enami 			}
    249  1.29     enami 			while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
    250  1.41     lukem 				SIMPLEQ_REMOVE_HEAD(&rom_image, next);
    251  1.29     enami 				free(p, M_DEVBUF);
    252  1.29     enami 			}
    253  1.29     enami 		out:
    254  1.29     enami 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    255  1.29     enami 			cardbus_conf_write(cc, cf, tag, reg, exrom & ~1);
    256  1.29     enami 			splx(save);
    257  1.29     enami 		} else {
    258  1.29     enami 			command = cardbus_conf_read(cc, cf, tag,
    259  1.29     enami 			    CARDBUS_COMMAND_STATUS_REG);
    260  1.29     enami 			cardbus_conf_write(cc, cf, tag,
    261  1.29     enami 			    CARDBUS_COMMAND_STATUS_REG,
    262  1.29     enami 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    263  1.29     enami 			/* XXX byte order? */
    264  1.29     enami 			bus_space_read_region_1(ca->ca_memt, bar_memh,
    265  1.29     enami 			    cis_ptr, tuples, 256);
    266  1.29     enami 			found++;
    267   1.7      joda 		}
    268  1.29     enami 		command = cardbus_conf_read(cc, cf, tag,
    269  1.29     enami 		    CARDBUS_COMMAND_STATUS_REG);
    270  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
    271  1.29     enami 		    command & ~CARDBUS_COMMAND_MEM_ENABLE);
    272  1.29     enami 		cardbus_conf_write(cc, cf, tag, reg, 0);
    273  1.30     enami 
    274  1.30     enami 		Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh,
    275  1.30     enami 		    bar_size);
    276  1.29     enami 		break;
    277   1.1      haya 
    278   1.7      joda #ifdef DIAGNOSTIC
    279  1.29     enami 	default:
    280  1.29     enami 		panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname,
    281  1.29     enami 		    cardbus_space);
    282   1.7      joda #endif
    283  1.29     enami 	}
    284  1.29     enami 	return (!found);
    285   1.7      joda }
    286   1.1      haya 
    287  1.10      joda static void
    288  1.10      joda parse_tuple(u_int8_t *tuple, int len, void *data)
    289  1.10      joda {
    290  1.29     enami 	struct cardbus_cis_info *cis = data;
    291  1.29     enami 	char *p;
    292  1.29     enami 	int i, bar_index;
    293  1.29     enami 
    294  1.29     enami 	switch (tuple[0]) {
    295  1.29     enami 	case PCMCIA_CISTPL_MANFID:
    296  1.29     enami 		if (tuple[1] != 5) {
    297  1.29     enami 			DPRINTF(("%s: wrong length manufacturer id (%d)\n",
    298  1.40     enami 			    __func__, tuple[1]));
    299  1.29     enami 			break;
    300  1.29     enami 		}
    301  1.29     enami 		cis->manufacturer = tuple[2] | (tuple[3] << 8);
    302  1.29     enami 		cis->product = tuple[4] | (tuple[5] << 8);
    303  1.29     enami 		break;
    304  1.29     enami 
    305  1.29     enami 	case PCMCIA_CISTPL_VERS_1:
    306  1.29     enami 		memcpy(cis->cis1_info_buf, tuple + 2, tuple[1]);
    307  1.29     enami 		i = 0;
    308  1.29     enami 		p = cis->cis1_info_buf + 2;
    309  1.29     enami 		while (i <
    310  1.29     enami 		    sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
    311  1.29     enami 			cis->cis1_info[i++] = p;
    312  1.29     enami 			while (*p != '\0' && *p != '\xff')
    313  1.29     enami 				p++;
    314  1.29     enami 			if (*p == '\xff')
    315  1.29     enami 				break;
    316  1.29     enami 			p++;
    317  1.29     enami 		}
    318  1.10      joda 		break;
    319  1.29     enami 
    320  1.29     enami 	case PCMCIA_CISTPL_BAR:
    321  1.29     enami 		if (tuple[1] != 6) {
    322  1.29     enami 			DPRINTF(("%s: BAR with short length (%d)\n",
    323  1.40     enami 			    __func__, tuple[1]));
    324  1.29     enami 			break;
    325  1.29     enami 		}
    326  1.29     enami 		bar_index = tuple[2] & 7;
    327  1.29     enami 		if (bar_index == 0) {
    328  1.40     enami 			DPRINTF(("%s: invalid ASI in BAR tuple\n", __func__));
    329  1.29     enami 			break;
    330  1.29     enami 		}
    331  1.29     enami 		bar_index--;
    332  1.29     enami 		cis->bar[bar_index].flags = tuple[2];
    333  1.29     enami 		cis->bar[bar_index].size =
    334  1.29     enami 		    (tuple[4] << 0) |
    335  1.29     enami 		    (tuple[5] << 8) |
    336  1.29     enami 		    (tuple[6] << 16) |
    337  1.29     enami 		    (tuple[7] << 24);
    338  1.29     enami 		break;
    339  1.29     enami 
    340  1.29     enami 	case PCMCIA_CISTPL_FUNCID:
    341  1.29     enami 		cis->funcid = tuple[2];
    342  1.29     enami 		break;
    343  1.29     enami 
    344  1.29     enami 	case PCMCIA_CISTPL_FUNCE:
    345  1.29     enami 		switch (cis->funcid) {
    346  1.29     enami 		case PCMCIA_FUNCTION_SERIAL:
    347  1.29     enami 			if (tuple[1] >= 2 &&
    348  1.29     enami 			    /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */
    349  1.29     enami 			    tuple[2] == 0) {
    350  1.29     enami 				cis->funce.serial.uart_type = tuple[3] & 0x1f;
    351  1.29     enami 				cis->funce.serial.uart_present = 1;
    352  1.29     enami 			}
    353  1.29     enami 			break;
    354  1.29     enami 
    355  1.29     enami 		case PCMCIA_FUNCTION_NETWORK:
    356  1.29     enami 			if (tuple[1] >= 8 &&
    357  1.29     enami 			    tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
    358  1.29     enami 				if (tuple[3] >
    359  1.29     enami 				    sizeof(cis->funce.network.netid)) {
    360  1.29     enami 					DPRINTF(("%s: unknown network id type "
    361  1.29     enami 					    "(len = %d)\n",
    362  1.40     enami 					    __func__, tuple[3]));
    363  1.29     enami 				} else {
    364  1.29     enami 					cis->funce.network.netid_present = 1;
    365  1.29     enami 					memcpy(cis->funce.network.netid,
    366  1.29     enami 					    tuple + 4, tuple[3]);
    367  1.29     enami 				}
    368  1.29     enami 			}
    369  1.29     enami 			break;
    370  1.10      joda 		}
    371  1.29     enami 		break;
    372  1.10      joda 	}
    373  1.10      joda }
    374  1.10      joda 
    375   1.1      haya /*
    376   1.1      haya  * int cardbus_attach_card(struct cardbus_softc *sc)
    377   1.1      haya  *
    378   1.1      haya  *    This function attaches the card on the slot: turns on power,
    379  1.28       wiz  *    reads and analyses tuple, sets configuration index.
    380   1.1      haya  *
    381   1.1      haya  *    This function returns the number of recognised device functions.
    382   1.1      haya  *    If no functions are recognised, return 0.
    383   1.1      haya  */
    384   1.1      haya int
    385  1.29     enami cardbus_attach_card(struct cardbus_softc *sc)
    386   1.1      haya {
    387  1.29     enami 	cardbus_chipset_tag_t cc;
    388  1.29     enami 	cardbus_function_tag_t cf;
    389  1.52  drochner 	int cdstatus;
    390  1.52  drochner 	static int wildcard[] = {
    391  1.53  drochner 		CARDBUSCF_DEV_DEFAULT, CARDBUSCF_FUNCTION_DEFAULT
    392  1.52  drochner 	};
    393  1.52  drochner 
    394  1.52  drochner 	cc = sc->sc_cc;
    395  1.52  drochner 	cf = sc->sc_cf;
    396  1.52  drochner 
    397  1.52  drochner 	DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
    398  1.52  drochner 
    399  1.52  drochner 	/* inspect initial voltage */
    400  1.52  drochner 	if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
    401  1.52  drochner 		DPRINTF(("cardbusattach: no CardBus card on cb%d\n",
    402  1.52  drochner 		    sc->sc_dev.dv_unit));
    403  1.52  drochner 		return (0);
    404  1.52  drochner 	}
    405  1.52  drochner 
    406  1.52  drochner 	cardbus_rescan(&sc->sc_dev, "cardbus", wildcard);
    407  1.52  drochner 	return (1); /* XXX */
    408  1.52  drochner }
    409  1.52  drochner 
    410  1.52  drochner int
    411  1.52  drochner cardbus_rescan(struct device *self, const char *ifattr, const int *locators)
    412  1.52  drochner {
    413  1.52  drochner 	struct cardbus_softc *sc = (struct cardbus_softc *)self;
    414  1.52  drochner 	cardbus_chipset_tag_t cc;
    415  1.52  drochner 	cardbus_function_tag_t cf;
    416  1.29     enami 	cardbustag_t tag;
    417  1.29     enami 	cardbusreg_t id, class, cis_ptr;
    418  1.29     enami 	cardbusreg_t bhlc;
    419  1.29     enami 	u_int8_t tuple[2048];
    420  1.29     enami 	int cdstatus;
    421  1.29     enami 	int function, nfunction;
    422  1.29     enami 	struct device *csc;
    423  1.29     enami 	cardbus_devfunc_t ct;
    424  1.29     enami 
    425  1.29     enami 	cc = sc->sc_cc;
    426  1.29     enami 	cf = sc->sc_cf;
    427  1.29     enami 
    428  1.53  drochner 	/* XXX what a nonsense */
    429  1.53  drochner 	if ((locators[CARDBUSCF_DEV] != CARDBUSCF_DEV_DEFAULT) &&
    430  1.53  drochner 	    (locators[CARDBUSCF_DEV] != sc->sc_device))
    431  1.53  drochner 		return (0);
    432  1.53  drochner 
    433  1.29     enami 	/* inspect initial voltage */
    434  1.29     enami 	if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
    435  1.29     enami 		DPRINTF(("cardbusattach: no CardBus card on cb%d\n",
    436  1.29     enami 		    sc->sc_dev.dv_unit));
    437  1.29     enami 		return (0);
    438  1.29     enami 	}
    439  1.29     enami 
    440  1.29     enami 	/*
    441  1.29     enami 	 * XXX use fake function 8 to keep power on during whole
    442  1.29     enami 	 * configuration.
    443  1.29     enami 	 */
    444  1.29     enami 	enable_function(sc, cdstatus, 8);
    445  1.29     enami 	function = 0;
    446  1.29     enami 
    447  1.29     enami 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
    448  1.29     enami 
    449  1.29     enami 	/*
    450  1.29     enami 	 * Wait until power comes up.  Maxmum 500 ms.
    451  1.29     enami 	 */
    452  1.29     enami 	{
    453  1.29     enami 		int i;
    454  1.29     enami 
    455  1.29     enami 		for (i = 0; i < 5; ++i) {
    456  1.29     enami 			id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    457  1.29     enami 			if (id != 0xffffffff && id != 0) {
    458  1.29     enami 				break;
    459  1.29     enami 			}
    460  1.29     enami 			if (cold) {	/* before kernel thread invoked */
    461  1.29     enami 				delay(100 * 1000);
    462  1.29     enami 			} else {	/* thread context */
    463  1.29     enami 				if (tsleep((void *)sc, PCATCH, "cardbus",
    464  1.29     enami 				    hz / 10) != EWOULDBLOCK) {
    465  1.29     enami 					break;
    466  1.29     enami 				}
    467  1.29     enami 			}
    468  1.29     enami 		}
    469  1.29     enami 		if (i == 5) {
    470  1.52  drochner 			return (EIO);
    471  1.29     enami 		}
    472  1.29     enami 	}
    473  1.29     enami 
    474  1.34   thorpej 	bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    475  1.34   thorpej 	DPRINTF(("%s bhlc 0x%08x -> ", sc->sc_dev.dv_xname, bhlc));
    476  1.29     enami 	nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
    477  1.29     enami 
    478  1.29     enami 	for (function = 0; function < nfunction; function++) {
    479  1.29     enami 		struct cardbus_attach_args ca;
    480  1.52  drochner 		int help[3];
    481  1.52  drochner 		locdesc_t *ldesc = (void *)&help; /* XXX */
    482  1.52  drochner 
    483  1.53  drochner 		if ((locators[CARDBUSCF_FUNCTION] != CARDBUSCF_FUNCTION_DEFAULT)
    484  1.53  drochner 		    && (locators[CARDBUSCF_FUNCTION] != function))
    485  1.53  drochner 			continue;
    486  1.53  drochner 
    487  1.54  drochner 		if (sc->sc_funcs[function])
    488  1.52  drochner 			continue;
    489  1.13      haya 
    490  1.29     enami 		tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device,
    491  1.29     enami 		    function);
    492   1.1      haya 
    493  1.29     enami 		id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    494  1.29     enami 		class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
    495  1.29     enami 		cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
    496   1.1      haya 
    497  1.29     enami 		/* Invalid vendor ID value? */
    498  1.51   mycroft 		if (CARDBUS_VENDOR(id) == PCI_VENDOR_INVALID) {
    499  1.29     enami 			continue;
    500  1.29     enami 		}
    501   1.1      haya 
    502  1.29     enami 		DPRINTF(("cardbus_attach_card: "
    503  1.29     enami 		    "Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
    504  1.29     enami 		    CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
    505  1.29     enami 
    506  1.29     enami 		enable_function(sc, cdstatus, function);
    507  1.29     enami 
    508  1.29     enami 		/* clean up every BAR */
    509  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
    510  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
    511  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
    512  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
    513  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
    514  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
    515  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
    516  1.32      haya 
    517  1.32      haya 		/* set initial latency and cacheline size */
    518  1.32      haya 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    519  1.32      haya 		DPRINTF(("%s func%d bhlc 0x%08x -> ", sc->sc_dev.dv_xname,
    520  1.32      haya 		    function, bhlc));
    521  1.32      haya 		bhlc &= ~((CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT) |
    522  1.32      haya 		    (CARDBUS_CACHELINE_MASK << CARDBUS_CACHELINE_SHIFT));
    523  1.32      haya 		bhlc |= ((sc->sc_cacheline & CARDBUS_CACHELINE_MASK) << CARDBUS_CACHELINE_SHIFT);
    524  1.32      haya 		bhlc |= ((sc->sc_lattimer & CARDBUS_LATTIMER_MASK) << CARDBUS_LATTIMER_SHIFT);
    525  1.32      haya 
    526  1.32      haya 		cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    527  1.32      haya 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    528  1.32      haya 		DPRINTF(("0x%08x\n", bhlc));
    529  1.32      haya 
    530  1.32      haya 		if (CARDBUS_LATTIMER(bhlc) < 0x10) {
    531  1.32      haya 			bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    532  1.32      haya 			bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
    533  1.32      haya 			cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    534  1.32      haya 		}
    535  1.29     enami 
    536  1.29     enami 		/*
    537  1.29     enami 		 * We need to allocate the ct here, since we might
    538  1.29     enami 		 * need it when reading the CIS
    539  1.29     enami 		 */
    540  1.29     enami 		if ((ct = malloc(sizeof(struct cardbus_devfunc),
    541  1.29     enami 		    M_DEVBUF, M_NOWAIT)) == NULL) {
    542  1.29     enami 			panic("no room for cardbus_tag");
    543  1.29     enami 		}
    544  1.29     enami 
    545  1.29     enami 		ct->ct_cc = sc->sc_cc;
    546  1.29     enami 		ct->ct_cf = sc->sc_cf;
    547  1.29     enami 		ct->ct_bus = sc->sc_bus;
    548  1.29     enami 		ct->ct_dev = sc->sc_device;
    549  1.29     enami 		ct->ct_func = function;
    550  1.29     enami 		ct->ct_sc = sc;
    551  1.54  drochner 		sc->sc_funcs[function] = ct;
    552  1.29     enami 
    553  1.29     enami 		memset(&ca, 0, sizeof(ca));
    554  1.29     enami 
    555  1.29     enami 		ca.ca_ct = ct;
    556  1.29     enami 
    557  1.29     enami 		ca.ca_iot = sc->sc_iot;
    558  1.29     enami 		ca.ca_memt = sc->sc_memt;
    559  1.29     enami 		ca.ca_dmat = sc->sc_dmat;
    560  1.35       mcr 
    561  1.35       mcr #if rbus
    562  1.35       mcr 		ca.ca_rbus_iot = sc->sc_rbus_iot;
    563  1.35       mcr 		ca.ca_rbus_memt= sc->sc_rbus_memt;
    564  1.35       mcr #endif
    565  1.29     enami 
    566  1.29     enami 		ca.ca_tag = tag;
    567  1.36  augustss 		ca.ca_bus = sc->sc_bus;
    568  1.52  drochner 		ca.ca_device = sc->sc_device; /* always 0 */
    569  1.29     enami 		ca.ca_function = function;
    570  1.29     enami 		ca.ca_id = id;
    571  1.29     enami 		ca.ca_class = class;
    572   1.1      haya 
    573  1.29     enami 		ca.ca_intrline = sc->sc_intrline;
    574   1.1      haya 
    575  1.50   mycroft 		if (cis_ptr != 0) {
    576  1.50   mycroft 			if (cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
    577  1.50   mycroft 				printf("cardbus_attach_card: failed to read CIS\n");
    578  1.50   mycroft 			} else {
    579  1.29     enami #ifdef CARDBUS_DEBUG
    580  1.50   mycroft 				decode_tuples(tuple, 2048, print_tuple, NULL);
    581  1.29     enami #endif
    582  1.50   mycroft 				decode_tuples(tuple, 2048, parse_tuple, &ca.ca_cis);
    583  1.50   mycroft 			}
    584  1.29     enami 		}
    585   1.1      haya 
    586  1.52  drochner 		ldesc->len = 2;
    587  1.52  drochner 		ldesc->locs[CARDBUSCF_DEV] = sc->sc_device; /* always 0 */
    588  1.52  drochner 		ldesc->locs[CARDBUSCF_FUNCTION] = function;
    589  1.52  drochner 
    590  1.52  drochner 		if ((csc = config_found_sm_loc((void *)sc, "cardbus", ldesc,
    591  1.52  drochner 			&ca, cardbusprint, cardbussubmatch)) == NULL) {
    592  1.29     enami 			/* do not match */
    593  1.29     enami 			disable_function(sc, function);
    594  1.54  drochner 			sc->sc_funcs[function] = NULL;
    595  1.29     enami 			free(ct, M_DEVBUF);
    596  1.29     enami 		} else {
    597  1.29     enami 			/* found */
    598  1.29     enami 			ct->ct_device = csc;
    599  1.29     enami 		}
    600  1.29     enami 	}
    601  1.29     enami 	/*
    602  1.29     enami 	 * XXX power down pseudo function 8 (this will power down the card
    603  1.29     enami 	 * if no functions were attached).
    604  1.29     enami 	 */
    605  1.29     enami 	disable_function(sc, 8);
    606   1.1      haya 
    607  1.52  drochner 	return (0);
    608   1.1      haya }
    609   1.1      haya 
    610  1.29     enami static int
    611  1.52  drochner cardbussubmatch(struct device *parent, struct cfdata *cf,
    612  1.52  drochner 		const locdesc_t *ldesc, void *aux)
    613  1.29     enami {
    614   1.1      haya 
    615  1.52  drochner 	/* ldesc->locs[CARDBUSCF_DEV] is always 0 */
    616  1.53  drochner 	if (cf->cf_loc[CARDBUSCF_DEV] != CARDBUSCF_DEV_DEFAULT &&
    617  1.53  drochner 	    cf->cf_loc[CARDBUSCF_DEV] != ldesc->locs[CARDBUSCF_DEV]) {
    618  1.29     enami 		return (0);
    619  1.29     enami 	}
    620  1.53  drochner 	if (cf->cf_loc[CARDBUSCF_FUNCTION] != CARDBUSCF_FUNCTION_DEFAULT &&
    621  1.53  drochner 	    cf->cf_loc[CARDBUSCF_FUNCTION] != ldesc->locs[CARDBUSCF_FUNCTION]) {
    622  1.29     enami 		return (0);
    623  1.29     enami 	}
    624   1.1      haya 
    625  1.43   thorpej 	return (config_match(parent, cf, aux));
    626  1.29     enami }
    627   1.1      haya 
    628  1.29     enami static int
    629  1.29     enami cardbusprint(void *aux, const char *pnp)
    630  1.29     enami {
    631  1.29     enami 	struct cardbus_attach_args *ca = aux;
    632  1.29     enami 	char devinfo[256];
    633  1.29     enami 	int i;
    634  1.29     enami 
    635  1.29     enami 	if (pnp) {
    636  1.48    itojun 		pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo,
    637  1.48    itojun 		    sizeof(devinfo));
    638  1.29     enami 		for (i = 0; i < 4; i++) {
    639  1.29     enami 			if (ca->ca_cis.cis1_info[i] == NULL)
    640  1.29     enami 				break;
    641  1.29     enami 			if (i)
    642  1.47   thorpej 				aprint_normal(", ");
    643  1.47   thorpej 			aprint_normal("%s", ca->ca_cis.cis1_info[i]);
    644  1.29     enami 		}
    645  1.47   thorpej 		aprint_verbose("%s(manufacturer 0x%x, product 0x%x)",
    646  1.47   thorpej 		       i ? " " : "",
    647  1.47   thorpej 		       ca->ca_cis.manufacturer, ca->ca_cis.product);
    648  1.47   thorpej 		aprint_normal(" %s at %s", devinfo, pnp);
    649  1.29     enami 	}
    650  1.47   thorpej 	aprint_normal(" dev %d function %d", ca->ca_device, ca->ca_function);
    651   1.1      haya 
    652  1.29     enami 	return (UNCONF);
    653  1.29     enami }
    654   1.1      haya 
    655   1.1      haya /*
    656  1.18      haya  * void cardbus_detach_card(struct cardbus_softc *sc)
    657  1.18      haya  *
    658  1.18      haya  *    This function detaches the card on the slot: detach device data
    659  1.18      haya  *    structure and turns off the power.
    660  1.18      haya  *
    661  1.18      haya  *    This function must not be called under interrupt context.
    662  1.18      haya  */
    663  1.18      haya void
    664  1.29     enami cardbus_detach_card(struct cardbus_softc *sc)
    665  1.18      haya {
    666  1.54  drochner 	int f;
    667  1.54  drochner 	struct cardbus_devfunc *ct;
    668  1.18      haya 
    669  1.54  drochner 	for (f = 0; f < 8; f++) {
    670  1.54  drochner 		ct = sc->sc_funcs[f];
    671  1.54  drochner 		if (!ct)
    672  1.54  drochner 			continue;
    673  1.18      haya 
    674  1.29     enami 		DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname,
    675  1.54  drochner 		    ct->ct_device->dv_xname));
    676  1.29     enami 		/* call device detach function */
    677  1.18      haya 
    678  1.54  drochner 		if (0 != config_detach(ct->ct_device, 0)) {
    679  1.33  augustss 			printf("%s: cannot detach dev %s, function %d\n",
    680  1.54  drochner 			    sc->sc_dev.dv_xname, ct->ct_device->dv_xname,
    681  1.54  drochner 			    ct->ct_func);
    682  1.52  drochner 		}
    683  1.52  drochner 	}
    684  1.52  drochner 
    685  1.52  drochner 	sc->sc_poweron_func = 0;
    686  1.52  drochner 	(*sc->sc_cf->cardbus_power)(sc->sc_cc,
    687  1.52  drochner 	    CARDBUS_VCC_0V | CARDBUS_VPP_0V);
    688  1.52  drochner }
    689  1.52  drochner 
    690  1.52  drochner void
    691  1.52  drochner cardbus_childdetached(struct device *self, struct device *child)
    692  1.52  drochner {
    693  1.52  drochner 	struct cardbus_softc *sc = (struct cardbus_softc *)self;
    694  1.54  drochner 	struct cardbus_devfunc *ct;
    695  1.52  drochner 
    696  1.54  drochner 	ct = sc->sc_funcs[child->dv_locators[CARDBUSCF_FUNCTION]];
    697  1.54  drochner 	KASSERT(ct->ct_device == child);
    698  1.19      haya 
    699  1.54  drochner 	sc->sc_poweron_func &= ~(1 << ct->ct_func);
    700  1.54  drochner 	sc->sc_funcs[ct->ct_func] = NULL;
    701  1.54  drochner 	free(ct, M_DEVBUF);
    702  1.18      haya }
    703  1.18      haya 
    704  1.18      haya /*
    705   1.1      haya  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    706   1.1      haya  *   Interrupt handler of pccard.
    707   1.1      haya  *  args:
    708   1.1      haya  *   cardbus_chipset_tag_t *cc
    709  1.29     enami  *   int irq:
    710   1.1      haya  */
    711   1.1      haya void *
    712  1.29     enami cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    713  1.29     enami     cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg)
    714   1.1      haya {
    715   1.1      haya 
    716  1.29     enami 	DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    717  1.29     enami 	return ((*cf->cardbus_intr_establish)(cc, irq, level, func, arg));
    718   1.1      haya }
    719   1.1      haya 
    720   1.1      haya /*
    721   1.1      haya  * void cardbus_intr_disestablish(cc, cf, handler)
    722   1.1      haya  *   Interrupt handler of pccard.
    723   1.1      haya  *  args:
    724   1.1      haya  *   cardbus_chipset_tag_t *cc
    725   1.1      haya  */
    726   1.1      haya void
    727  1.29     enami cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    728  1.29     enami     void *handler)
    729   1.1      haya {
    730   1.1      haya 
    731  1.29     enami 	DPRINTF(("- pccard_intr_disestablish\n"));
    732  1.29     enami 	(*cf->cardbus_intr_disestablish)(cc, handler);
    733   1.1      haya }
    734   1.1      haya 
    735  1.29     enami /*
    736  1.29     enami  * XXX this should be merged with cardbus_function_{enable,disable},
    737  1.29     enami  * but we don't have a ct when these functions are called.
    738  1.29     enami  */
    739   1.8      joda static void
    740  1.29     enami enable_function(struct cardbus_softc *sc, int cdstatus, int function)
    741   1.8      joda {
    742  1.18      haya 
    743  1.29     enami 	if (sc->sc_poweron_func == 0) {
    744  1.29     enami 		/* switch to 3V and/or wait for power to stabilize */
    745  1.29     enami 		if (cdstatus & CARDBUS_3V_CARD) {
    746  1.31      haya 			/*
    747  1.31      haya 			 * sc_poweron_func must be substituted before
    748  1.31      haya 			 * entering sleep, in order to avoid turn on
    749  1.31      haya 			 * power twice.
    750  1.31      haya 			 */
    751  1.31      haya 			sc->sc_poweron_func |= (1 << function);
    752  1.29     enami 			(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_3V);
    753  1.29     enami 		} else {
    754  1.29     enami 			/* No cards other than 3.3V cards. */
    755  1.29     enami 			return;
    756  1.29     enami 		}
    757  1.29     enami 		(*sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
    758  1.23      haya 	}
    759  1.29     enami 	sc->sc_poweron_func |= (1 << function);
    760   1.8      joda }
    761   1.8      joda 
    762   1.8      joda static void
    763  1.29     enami disable_function(struct cardbus_softc *sc, int function)
    764   1.8      joda {
    765  1.18      haya 
    766  1.29     enami 	sc->sc_poweron_func &= ~(1 << function);
    767  1.29     enami 	if (sc->sc_poweron_func == 0) {
    768  1.29     enami 		/* power-off because no functions are enabled */
    769  1.29     enami 		(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_0V);
    770  1.29     enami 	}
    771   1.8      joda }
    772   1.8      joda 
    773   1.1      haya /*
    774   1.9      haya  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
    775   1.1      haya  *
    776   1.1      haya  *   This function enables a function on a card.  When no power is
    777   1.1      haya  *  applied on the card, power will be applied on it.
    778   1.1      haya  */
    779   1.1      haya int
    780  1.29     enami cardbus_function_enable(struct cardbus_softc *sc, int func)
    781   1.1      haya {
    782  1.29     enami 	cardbus_chipset_tag_t cc = sc->sc_cc;
    783  1.29     enami 	cardbus_function_tag_t cf = sc->sc_cf;
    784  1.29     enami 	cardbusreg_t command;
    785  1.29     enami 	cardbustag_t tag;
    786   1.1      haya 
    787  1.29     enami 	DPRINTF(("entering cardbus_function_enable...  "));
    788   1.1      haya 
    789  1.29     enami 	/* entering critical area */
    790   1.1      haya 
    791  1.29     enami 	/* XXX: sc_vold should be used */
    792  1.29     enami 	enable_function(sc, CARDBUS_3V_CARD, func);
    793   1.1      haya 
    794  1.29     enami 	/* exiting critical area */
    795   1.1      haya 
    796  1.29     enami 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
    797   1.9      haya 
    798  1.29     enami 	command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    799  1.29     enami 	command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE |
    800  1.29     enami 	    CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    801   1.9      haya 
    802  1.29     enami 	cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    803   1.1      haya 
    804  1.29     enami 	cardbus_free_tag(cc, cf, tag);
    805   1.1      haya 
    806  1.29     enami 	DPRINTF(("%x\n", sc->sc_poweron_func));
    807   1.1      haya 
    808  1.29     enami 	return (0);
    809   1.1      haya }
    810   1.1      haya 
    811   1.1      haya /*
    812   1.9      haya  * int cardbus_function_disable(struct cardbus_softc *, int func)
    813   1.1      haya  *
    814   1.1      haya  *   This function disable a function on a card.  When no functions are
    815   1.1      haya  *  enabled, it turns off the power.
    816   1.1      haya  */
    817   1.1      haya int
    818  1.29     enami cardbus_function_disable(struct cardbus_softc *sc, int func)
    819   1.1      haya {
    820   1.1      haya 
    821  1.29     enami 	DPRINTF(("entering cardbus_function_disable...  "));
    822   1.1      haya 
    823  1.29     enami 	disable_function(sc, func);
    824   1.1      haya 
    825  1.29     enami 	return (0);
    826   1.1      haya }
    827   1.1      haya 
    828  1.16   thorpej /*
    829  1.16   thorpej  * int cardbus_get_capability(cardbus_chipset_tag_t cc,
    830  1.16   thorpej  *	cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
    831  1.16   thorpej  *	cardbusreg_t *value)
    832  1.16   thorpej  *
    833  1.16   thorpej  *	Find the specified PCI capability.
    834  1.16   thorpej  */
    835  1.16   thorpej int
    836  1.29     enami cardbus_get_capability(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    837  1.29     enami     cardbustag_t tag, int capid, int *offset, cardbusreg_t *value)
    838  1.16   thorpej {
    839  1.16   thorpej 	cardbusreg_t reg;
    840  1.16   thorpej 	unsigned int ofs;
    841  1.16   thorpej 
    842  1.16   thorpej 	reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
    843  1.16   thorpej 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
    844  1.16   thorpej 		return (0);
    845  1.16   thorpej 
    846  1.16   thorpej 	ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
    847  1.16   thorpej 	    PCI_CAPLISTPTR_REG));
    848  1.16   thorpej 	while (ofs != 0) {
    849  1.16   thorpej #ifdef DIAGNOSTIC
    850  1.16   thorpej 		if ((ofs & 3) || (ofs < 0x40))
    851  1.16   thorpej 			panic("cardbus_get_capability");
    852  1.16   thorpej #endif
    853  1.16   thorpej 		reg = cardbus_conf_read(cc, cf, tag, ofs);
    854  1.16   thorpej 		if (PCI_CAPLIST_CAP(reg) == capid) {
    855  1.16   thorpej 			if (offset)
    856  1.16   thorpej 				*offset = ofs;
    857  1.16   thorpej 			if (value)
    858  1.16   thorpej 				*value = reg;
    859  1.16   thorpej 			return (1);
    860  1.16   thorpej 		}
    861  1.16   thorpej 		ofs = PCI_CAPLIST_NEXT(reg);
    862  1.16   thorpej 	}
    863   1.1      haya 
    864  1.16   thorpej 	return (0);
    865  1.16   thorpej }
    866   1.1      haya 
    867   1.1      haya /*
    868   1.1      haya  * below this line, there are some functions for decoding tuples.
    869   1.1      haya  * They should go out from this file.
    870   1.1      haya  */
    871   1.1      haya 
    872  1.10      joda static u_int8_t *
    873  1.29     enami decode_tuple(u_int8_t *tuple, tuple_decode_func func, void *data);
    874   1.1      haya 
    875   1.1      haya static int
    876  1.29     enami decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
    877  1.29     enami {
    878  1.29     enami 	u_int8_t *tp = tuple;
    879  1.29     enami 
    880  1.29     enami 	if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
    881  1.29     enami 		DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    882  1.29     enami 		return (0);
    883  1.29     enami 	}
    884  1.29     enami 
    885  1.29     enami 	while (NULL != (tp = decode_tuple(tp, func, data))) {
    886  1.29     enami 		if (tuple + buflen < tp) {
    887  1.29     enami 			break;
    888  1.29     enami 		}
    889  1.29     enami 	}
    890  1.29     enami 
    891  1.29     enami 	return (1);
    892   1.1      haya }
    893   1.1      haya 
    894   1.1      haya static u_int8_t *
    895  1.29     enami decode_tuple(u_int8_t *tuple, tuple_decode_func func, void *data)
    896   1.1      haya {
    897  1.29     enami 	u_int8_t type;
    898  1.29     enami 	u_int8_t len;
    899   1.1      haya 
    900  1.29     enami 	type = tuple[0];
    901  1.29     enami 	len = tuple[1] + 2;
    902   1.1      haya 
    903  1.29     enami 	(*func)(tuple, len, data);
    904   1.1      haya 
    905  1.29     enami 	if (type == PCMCIA_CISTPL_END) {
    906  1.29     enami 		return (NULL);
    907  1.29     enami 	}
    908   1.1      haya 
    909  1.29     enami 	return (tuple + len);
    910   1.1      haya }
    911   1.1      haya 
    912  1.49  christos /*
    913  1.49  christos  * XXX: this is another reason why this code should be shared with PCI.
    914  1.49  christos  */
    915  1.49  christos int
    916  1.49  christos cardbus_powerstate(cardbus_devfunc_t ct, pcitag_t tag, const int *newstate,
    917  1.49  christos     int *oldstate)
    918  1.49  christos {
    919  1.49  christos 	cardbus_chipset_tag_t cc = ct->ct_cc;
    920  1.49  christos 	cardbus_function_tag_t cf = ct->ct_cf;
    921  1.49  christos 
    922  1.49  christos 	int offset;
    923  1.49  christos 	pcireg_t value, cap, now;
    924  1.49  christos 
    925  1.49  christos 	if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset,
    926  1.49  christos 	    &value))
    927  1.49  christos 		return EOPNOTSUPP;
    928  1.49  christos 
    929  1.49  christos 	cap = value >> 16;
    930  1.49  christos 	value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR);
    931  1.49  christos 	now = value & PCI_PMCSR_STATE_MASK;
    932  1.49  christos 	value &= ~PCI_PMCSR_STATE_MASK;
    933  1.49  christos 	if (oldstate) {
    934  1.49  christos 		switch (now) {
    935  1.49  christos 		case PCI_PMCSR_STATE_D0:
    936  1.49  christos 			*oldstate = PCI_PWR_D0;
    937  1.49  christos 			break;
    938  1.49  christos 		case PCI_PMCSR_STATE_D1:
    939  1.49  christos 			*oldstate = PCI_PWR_D1;
    940  1.49  christos 			break;
    941  1.49  christos 		case PCI_PMCSR_STATE_D2:
    942  1.49  christos 			*oldstate = PCI_PWR_D2;
    943  1.49  christos 			break;
    944  1.49  christos 		case PCI_PMCSR_STATE_D3:
    945  1.49  christos 			*oldstate = PCI_PWR_D3;
    946  1.49  christos 			break;
    947  1.49  christos 		default:
    948  1.49  christos 			return EINVAL;
    949  1.49  christos 		}
    950  1.49  christos 	}
    951  1.49  christos 	if (newstate == NULL)
    952  1.49  christos 		return 0;
    953  1.49  christos 	switch (*newstate) {
    954  1.49  christos 	case PCI_PWR_D0:
    955  1.49  christos 		if (now == PCI_PMCSR_STATE_D0)
    956  1.49  christos 			return 0;
    957  1.49  christos 		value |= PCI_PMCSR_STATE_D0;
    958  1.49  christos 		break;
    959  1.49  christos 	case PCI_PWR_D1:
    960  1.49  christos 		if (now == PCI_PMCSR_STATE_D1)
    961  1.49  christos 			return 0;
    962  1.49  christos 		if (now == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D3)
    963  1.49  christos 			return EINVAL;
    964  1.49  christos 		if (!(cap & PCI_PMCR_D1SUPP))
    965  1.49  christos 			return EOPNOTSUPP;
    966  1.49  christos 		value |= PCI_PMCSR_STATE_D1;
    967  1.49  christos 		break;
    968  1.49  christos 	case PCI_PWR_D2:
    969  1.49  christos 		if (now == PCI_PMCSR_STATE_D2)
    970  1.49  christos 			return 0;
    971  1.49  christos 		if (now == PCI_PMCSR_STATE_D3)
    972  1.49  christos 			return EINVAL;
    973  1.49  christos 		if (!(cap & PCI_PMCR_D2SUPP))
    974  1.49  christos 			return EOPNOTSUPP;
    975  1.49  christos 		value |= PCI_PMCSR_STATE_D2;
    976  1.49  christos 		break;
    977  1.49  christos 	case PCI_PWR_D3:
    978  1.49  christos 		if (now == PCI_PMCSR_STATE_D3)
    979  1.49  christos 			return 0;
    980  1.49  christos 		value |= PCI_PMCSR_STATE_D3;
    981  1.49  christos 		break;
    982  1.49  christos 	default:
    983  1.49  christos 		return EINVAL;
    984  1.49  christos 	}
    985  1.49  christos 	cardbus_conf_write(cc, cf, tag, offset + PCI_PMCSR, value);
    986  1.49  christos 	DELAY(1000);
    987  1.49  christos 
    988  1.49  christos 	return 0;
    989  1.49  christos }
    990  1.49  christos 
    991  1.49  christos int
    992  1.49  christos cardbus_setpowerstate(const char *dvname, cardbus_devfunc_t ct, pcitag_t tag,
    993  1.49  christos     int newpwr)
    994  1.49  christos {
    995  1.49  christos 	int oldpwr, error;
    996  1.49  christos 
    997  1.49  christos 	if ((error = cardbus_powerstate(ct, tag, &newpwr, &oldpwr)) != 0)
    998  1.49  christos 		return error;
    999  1.49  christos 
   1000  1.49  christos 	if (oldpwr == newpwr)
   1001  1.49  christos 		return 0;
   1002  1.49  christos 
   1003  1.49  christos 	if (oldpwr > newpwr) {
   1004  1.49  christos 		printf("%s: sleeping to power state D%d\n", dvname, oldpwr);
   1005  1.49  christos 		return 0;
   1006  1.49  christos 	}
   1007  1.49  christos 
   1008  1.49  christos 	/* oldpwr < newpwr */
   1009  1.49  christos 	switch (oldpwr) {
   1010  1.49  christos 	case PCI_PWR_D3:
   1011  1.49  christos 		/*
   1012  1.49  christos 		 * XXX: This is because none of the devices do
   1013  1.49  christos 		 * the necessary song and dance for now to wakeup
   1014  1.49  christos 		 * Once this
   1015  1.49  christos 		 */
   1016  1.49  christos 		printf("%s: cannot wake up from power state D%d\n",
   1017  1.49  christos 		    dvname, oldpwr);
   1018  1.49  christos 		return EINVAL;
   1019  1.49  christos 	default:
   1020  1.49  christos 		printf("%s: waking up from power state D%d\n",
   1021  1.49  christos 		    dvname, oldpwr);
   1022  1.49  christos 		return 0;
   1023  1.49  christos 	}
   1024  1.49  christos }
   1025  1.49  christos 
   1026  1.49  christos 
   1027   1.3  augustss #ifdef CARDBUS_DEBUG
   1028  1.29     enami static const char *tuple_name(int);
   1029  1.29     enami static const char *tuple_names[] = {
   1030  1.29     enami 	"TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
   1031  1.29     enami 	"CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
   1032  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
   1033  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
   1034  1.29     enami 	"CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
   1035  1.29     enami 	"NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
   1036  1.29     enami 	"JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
   1037  1.29     enami 	"DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
   1038  1.29     enami 	"MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
   1039  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
   1040  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
   1041  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
   1042  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
   1043  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
   1044  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
   1045  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
   1046  1.29     enami 	"VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
   1047  1.29     enami 	"DATE", "BATTERY", "ORG"
   1048  1.29     enami };
   1049  1.29     enami #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
   1050  1.10      joda 
   1051  1.29     enami static const char *
   1052  1.29     enami tuple_name(int type)
   1053  1.29     enami {
   1054   1.1      haya 
   1055  1.29     enami 	if (0 <= type && type < NAME_LEN(tuple_names)) {
   1056  1.29     enami 		return (tuple_names[type]);
   1057  1.29     enami 	} else if (type == 0xff) {
   1058  1.29     enami 		return ("END");
   1059  1.29     enami 	} else {
   1060  1.29     enami 		return ("Reserved");
   1061  1.29     enami 	}
   1062   1.1      haya }
   1063  1.10      joda 
   1064  1.10      joda static void
   1065  1.29     enami print_tuple(u_int8_t *tuple, int len, void *data)
   1066  1.29     enami {
   1067  1.29     enami 	int i;
   1068  1.29     enami 
   1069  1.29     enami 	printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
   1070  1.29     enami 
   1071  1.29     enami 	for (i = 0; i < len; ++i) {
   1072  1.29     enami 		if (i % 16 == 0) {
   1073  1.29     enami 			printf("  0x%2x:", i);
   1074  1.29     enami 		}
   1075  1.29     enami 		printf(" %x", tuple[i]);
   1076  1.29     enami 		if (i % 16 == 15) {
   1077  1.29     enami 			printf("\n");
   1078  1.29     enami 		}
   1079  1.29     enami 	}
   1080  1.29     enami 	if (i % 16 != 0) {
   1081  1.29     enami 		printf("\n");
   1082  1.29     enami 	}
   1083  1.10      joda }
   1084   1.3  augustss #endif
   1085