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