Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.88
      1 /*	$NetBSD: cardbus.c,v 1.88 2008/02/22 23:30:42 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999 and 2000
      5  *     HAYAKAWA Koichi.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by HAYAKAWA Koichi.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     26  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: cardbus.c,v 1.88 2008/02/22 23:30:42 dyoung Exp $");
     37 
     38 #include "opt_cardbus.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 #include <sys/malloc.h>
     44 #include <sys/kernel.h>
     45 #include <sys/syslog.h>
     46 #include <sys/proc.h>
     47 #include <sys/reboot.h>		/* for AB_* needed by bootverbose */
     48 
     49 #include <sys/bus.h>
     50 
     51 #include <dev/cardbus/cardbusvar.h>
     52 #include <dev/pci/pcidevs.h>
     53 
     54 #include <dev/cardbus/cardbus_exrom.h>
     55 
     56 #include <dev/pci/pcivar.h>	/* XXX */
     57 #include <dev/pci/pcireg.h>	/* XXX */
     58 
     59 #include <dev/pcmcia/pcmciareg.h>
     60 
     61 #include "locators.h"
     62 
     63 #if defined CARDBUS_DEBUG
     64 #define STATIC
     65 #define DPRINTF(a) printf a
     66 #else
     67 #define STATIC static
     68 #define DPRINTF(a)
     69 #endif
     70 
     71 
     72 STATIC void cardbusattach(device_t, device_t, void *);
     73 STATIC int cardbusdetach(device_t, int);
     74 STATIC int cardbusmatch(device_t, struct cfdata *, void *);
     75 int cardbus_rescan(device_t, const char *, const int *);
     76 void cardbus_childdetached(device_t, device_t);
     77 static int cardbusprint(void *, const char *);
     78 
     79 typedef void (*tuple_decode_func)(u_int8_t*, int, void*);
     80 
     81 static int decode_tuples(u_int8_t *, int, tuple_decode_func, void*);
     82 #ifdef CARDBUS_DEBUG
     83 static void print_tuple(u_int8_t*, int, void*);
     84 #endif
     85 
     86 static int cardbus_read_tuples(struct cardbus_attach_args *,
     87     cardbusreg_t, u_int8_t *, size_t);
     88 
     89 static void enable_function(struct cardbus_softc *, int, int);
     90 static void disable_function(struct cardbus_softc *, int);
     91 
     92 static bool cardbus_child_register(device_t);
     93 
     94 CFATTACH_DECL2(cardbus, sizeof(struct cardbus_softc),
     95     cardbusmatch, cardbusattach, cardbusdetach, NULL,
     96     cardbus_rescan, cardbus_childdetached);
     97 
     98 #ifndef __NetBSD_Version__
     99 struct cfdriver cardbus_cd = {
    100 	NULL, "cardbus", DV_DULL
    101 };
    102 #endif
    103 
    104 
    105 STATIC int
    106 cardbusmatch(device_t parent, struct cfdata *cf, void *aux)
    107 {
    108 	struct cbslot_attach_args *cba = aux;
    109 
    110 	if (strcmp(cba->cba_busname, cf->cf_name)) {
    111 		DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
    112 		    cba->cba_busname, cf->cf_name));
    113 		return (0);
    114 	}
    115 
    116 	return (1);
    117 }
    118 
    119 STATIC void
    120 cardbusattach(device_t parent, device_t self, void *aux)
    121 {
    122 	struct cardbus_softc *sc = device_private(self);
    123 	struct cbslot_attach_args *cba = aux;
    124 
    125 	sc->sc_bus = cba->cba_bus;
    126 	sc->sc_intrline = cba->cba_intrline;
    127 	sc->sc_cacheline = cba->cba_cacheline;
    128 	sc->sc_max_lattimer = MIN(0xf8, cba->cba_max_lattimer);
    129 
    130 	aprint_naive("\n");
    131 	aprint_normal(": bus %d", sc->sc_bus);
    132 	if (bootverbose)
    133 		aprint_normal(" cacheline 0x%x, lattimer 0x%x",
    134 		    sc->sc_cacheline, sc->sc_max_lattimer);
    135 	aprint_normal("\n");
    136 
    137 	sc->sc_iot = cba->cba_iot;	/* CardBus I/O space tag */
    138 	sc->sc_memt = cba->cba_memt;	/* CardBus MEM space tag */
    139 	sc->sc_dmat = cba->cba_dmat;	/* DMA tag */
    140 	sc->sc_cc = cba->cba_cc;
    141 	sc->sc_cf = cba->cba_cf;
    142 
    143 #if rbus
    144 	sc->sc_rbus_iot = cba->cba_rbus_iot;
    145 	sc->sc_rbus_memt = cba->cba_rbus_memt;
    146 #endif
    147 
    148 	if (!pmf_device_register(self, NULL, NULL))
    149 		aprint_error_dev(self, "couldn't establish power handler\n");
    150 }
    151 
    152 STATIC int
    153 cardbusdetach(device_t self, int flags)
    154 {
    155 	int rc;
    156 
    157 	if ((rc = config_detach_children(self, flags)) != 0)
    158 		return rc;
    159 
    160 	pmf_device_deregister(self);
    161 	return 0;
    162 }
    163 
    164 static int
    165 cardbus_read_tuples(struct cardbus_attach_args *ca, cardbusreg_t cis_ptr,
    166     u_int8_t *tuples, size_t len)
    167 {
    168 	struct cardbus_softc *sc = ca->ca_ct->ct_sc;
    169 	cardbus_chipset_tag_t cc = ca->ca_ct->ct_cc;
    170 	cardbus_function_tag_t cf = ca->ca_ct->ct_cf;
    171 	cardbustag_t tag = ca->ca_tag;
    172 	cardbusreg_t command;
    173 	bus_space_tag_t bar_tag;
    174 	bus_space_handle_t bar_memh;
    175 	bus_size_t bar_size;
    176 	bus_addr_t bar_addr;
    177 	cardbusreg_t reg;
    178 	int found = 0;
    179 	int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK;
    180 	int i, j;
    181 
    182 	memset(tuples, 0, len);
    183 
    184 	cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK;
    185 
    186 	switch (cardbus_space) {
    187 	case CARDBUS_CIS_ASI_TUPLE:
    188 		DPRINTF(("%s: reading CIS data from configuration space\n",
    189 		    device_xname(&sc->sc_dev)));
    190 		for (i = cis_ptr, j = 0; i < 0xff; i += 4) {
    191 			u_int32_t e = (*cf->cardbus_conf_read)(cc, tag, i);
    192 			tuples[j] = 0xff & e;
    193 			e >>= 8;
    194 			tuples[j + 1] = 0xff & e;
    195 			e >>= 8;
    196 			tuples[j + 2] = 0xff & e;
    197 			e >>= 8;
    198 			tuples[j + 3] = 0xff & e;
    199 			j += 4;
    200 		}
    201 		found++;
    202 		break;
    203 
    204 	case CARDBUS_CIS_ASI_BAR0:
    205 	case CARDBUS_CIS_ASI_BAR1:
    206 	case CARDBUS_CIS_ASI_BAR2:
    207 	case CARDBUS_CIS_ASI_BAR3:
    208 	case CARDBUS_CIS_ASI_BAR4:
    209 	case CARDBUS_CIS_ASI_BAR5:
    210 	case CARDBUS_CIS_ASI_ROM:
    211 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
    212 			reg = CARDBUS_ROM_REG;
    213 			DPRINTF(("%s: reading CIS data from ROM\n",
    214 			    device_xname(&sc->sc_dev)));
    215 		} else {
    216 			reg = CARDBUS_CIS_ASI_BAR(cardbus_space);
    217 			DPRINTF(("%s: reading CIS data from BAR%d\n",
    218 			    device_xname(&sc->sc_dev), cardbus_space - 1));
    219 		}
    220 
    221 		/*
    222 		 * XXX zero register so mapreg_map doesn't get confused by old
    223 		 * contents.
    224 		 */
    225 		cardbus_conf_write(cc, cf, tag, reg, 0);
    226 		if (Cardbus_mapreg_map(ca->ca_ct, reg,
    227 		    CARDBUS_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    228 		    0, &bar_tag, &bar_memh, &bar_addr, &bar_size)) {
    229 			aprint_error_dev(&sc->sc_dev, "failed to map memory\n");
    230 			return (1);
    231 		}
    232 		aprint_debug_dev(&sc->sc_dev, "mapped %ju bytes at 0x%jx\n",
    233 		    (uintmax_t)bar_size, (uintmax_t)bar_addr);
    234 
    235 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
    236 			cardbusreg_t exrom;
    237 			int save;
    238 			struct cardbus_rom_image_head rom_image;
    239 			struct cardbus_rom_image *p;
    240 
    241 			save = splhigh();
    242 			/* enable rom address decoder */
    243 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    244 			cardbus_conf_write(cc, cf, tag, reg, exrom | 1);
    245 
    246 			command = cardbus_conf_read(cc, cf, tag,
    247 			    CARDBUS_COMMAND_STATUS_REG);
    248 			cardbus_conf_write(cc, cf, tag,
    249 			    CARDBUS_COMMAND_STATUS_REG,
    250 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    251 
    252 			if (cardbus_read_exrom(bar_tag, bar_memh, &rom_image))
    253 				goto out;
    254 
    255 			SIMPLEQ_FOREACH(p, &rom_image, next) {
    256 				if (p->rom_image ==
    257 				    CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
    258 					bus_space_read_region_1(p->romt,
    259 					    p->romh, CARDBUS_CIS_ADDR(cis_ptr),
    260 					    tuples, MIN(p->image_size, len));
    261 					found++;
    262 					break;
    263 				}
    264 			}
    265 			while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
    266 				SIMPLEQ_REMOVE_HEAD(&rom_image, next);
    267 				free(p, M_DEVBUF);
    268 			}
    269 		out:
    270 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    271 			cardbus_conf_write(cc, cf, tag, reg, exrom & ~1);
    272 			splx(save);
    273 		} else {
    274 			command = cardbus_conf_read(cc, cf, tag,
    275 			    CARDBUS_COMMAND_STATUS_REG);
    276 			cardbus_conf_write(cc, cf, tag,
    277 			    CARDBUS_COMMAND_STATUS_REG,
    278 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    279 			/* XXX byte order? */
    280 			bus_space_read_region_1(bar_tag, bar_memh,
    281 			    cis_ptr, tuples,
    282 			    MIN(bar_size - MIN(bar_size, cis_ptr), len));
    283 			found++;
    284 		}
    285 		command = cardbus_conf_read(cc, cf, tag,
    286 		    CARDBUS_COMMAND_STATUS_REG);
    287 		cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
    288 		    command & ~CARDBUS_COMMAND_MEM_ENABLE);
    289 		cardbus_conf_write(cc, cf, tag, reg, 0);
    290 
    291 		Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh,
    292 		    bar_size);
    293 		break;
    294 
    295 #ifdef DIAGNOSTIC
    296 	default:
    297 		panic("%s: bad CIS space (%d)", device_xname(&sc->sc_dev),
    298 		    cardbus_space);
    299 #endif
    300 	}
    301 	return (!found);
    302 }
    303 
    304 static void
    305 parse_tuple(u_int8_t *tuple, int len, void *data)
    306 {
    307 	struct cardbus_cis_info *cis = data;
    308 	char *p;
    309 	int i, bar_index;
    310 
    311 	switch (tuple[0]) {
    312 	case PCMCIA_CISTPL_MANFID:
    313 		if (tuple[1] != 4) {
    314 			DPRINTF(("%s: wrong length manufacturer id (%d)\n",
    315 			    __func__, tuple[1]));
    316 			break;
    317 		}
    318 		cis->manufacturer = tuple[2] | (tuple[3] << 8);
    319 		cis->product = tuple[4] | (tuple[5] << 8);
    320 		break;
    321 
    322 	case PCMCIA_CISTPL_VERS_1:
    323 		memcpy(cis->cis1_info_buf, tuple + 2, tuple[1]);
    324 		i = 0;
    325 		p = cis->cis1_info_buf + 2;
    326 		while (i <
    327 		    sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
    328 			if (p >= cis->cis1_info_buf + tuple[1] || *p == '\xff')
    329 				break;
    330 			cis->cis1_info[i++] = p;
    331 			while (*p != '\0' && *p != '\xff')
    332 				p++;
    333 			if (*p == '\0')
    334 				p++;
    335 		}
    336 		break;
    337 
    338 	case PCMCIA_CISTPL_BAR:
    339 		if (tuple[1] != 6) {
    340 			DPRINTF(("%s: BAR with short length (%d)\n",
    341 			    __func__, tuple[1]));
    342 			break;
    343 		}
    344 		bar_index = tuple[2] & 7;
    345 		if (bar_index == 0) {
    346 			DPRINTF(("%s: invalid ASI in BAR tuple\n", __func__));
    347 			break;
    348 		}
    349 		bar_index--;
    350 		cis->bar[bar_index].flags = tuple[2];
    351 		cis->bar[bar_index].size =
    352 		    (tuple[4] << 0) |
    353 		    (tuple[5] << 8) |
    354 		    (tuple[6] << 16) |
    355 		    (tuple[7] << 24);
    356 		break;
    357 
    358 	case PCMCIA_CISTPL_FUNCID:
    359 		cis->funcid = tuple[2];
    360 		break;
    361 
    362 	case PCMCIA_CISTPL_FUNCE:
    363 		switch (cis->funcid) {
    364 		case PCMCIA_FUNCTION_SERIAL:
    365 			if (tuple[1] >= 2 &&
    366 			    /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */
    367 			    tuple[2] == 0) {
    368 				cis->funce.serial.uart_type = tuple[3] & 0x1f;
    369 				cis->funce.serial.uart_present = 1;
    370 			}
    371 			break;
    372 
    373 		case PCMCIA_FUNCTION_NETWORK:
    374 			if (tuple[1] >= 8 &&
    375 			    tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
    376 				if (tuple[3] >
    377 				    sizeof(cis->funce.network.netid)) {
    378 					DPRINTF(("%s: unknown network id type "
    379 					    "(len = %d)\n",
    380 					    __func__, tuple[3]));
    381 				} else {
    382 					cis->funce.network.netid_present = 1;
    383 					memcpy(cis->funce.network.netid,
    384 					    tuple + 4, tuple[3]);
    385 				}
    386 			}
    387 			break;
    388 		}
    389 		break;
    390 	}
    391 }
    392 
    393 /*
    394  * int cardbus_attach_card(struct cardbus_softc *sc)
    395  *
    396  *    This function attaches the card on the slot: turns on power,
    397  *    reads and analyses tuple, sets configuration index.
    398  *
    399  *    This function returns the number of recognised device functions.
    400  *    If no functions are recognised, return 0.
    401  */
    402 int
    403 cardbus_attach_card(struct cardbus_softc *sc)
    404 {
    405 	cardbus_chipset_tag_t cc;
    406 	cardbus_function_tag_t cf;
    407 	int cdstatus;
    408 	static int wildcard[CARDBUSCF_NLOCS] = {
    409 		CARDBUSCF_FUNCTION_DEFAULT
    410 	};
    411 
    412 	cc = sc->sc_cc;
    413 	cf = sc->sc_cf;
    414 
    415 	DPRINTF(("cardbus_attach_card: cb%d start\n",
    416 		 device_unit(&sc->sc_dev)));
    417 
    418 	/* inspect initial voltage */
    419 	if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
    420 		DPRINTF(("%s: no CardBus card on cb%d\n", __func__,
    421 		    device_unit(&sc->sc_dev)));
    422 		return (0);
    423 	}
    424 
    425 	device_pmf_driver_set_child_register(&sc->sc_dev, cardbus_child_register);
    426 	cardbus_rescan(&sc->sc_dev, "cardbus", wildcard);
    427 	return (1); /* XXX */
    428 }
    429 
    430 int
    431 cardbus_rescan(device_t self, const char *ifattr,
    432     const int *locators)
    433 {
    434 	struct cardbus_softc *sc = device_private(self);
    435 	cardbus_chipset_tag_t cc;
    436 	cardbus_function_tag_t cf;
    437 	cardbustag_t tag;
    438 	cardbusreg_t id, class, cis_ptr;
    439 	cardbusreg_t bhlc, icr, lattimer;
    440 	int cdstatus;
    441 	int function, nfunction;
    442 	device_t csc;
    443 	cardbus_devfunc_t ct;
    444 
    445 	cc = sc->sc_cc;
    446 	cf = sc->sc_cf;
    447 
    448 	/* inspect initial voltage */
    449 	if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
    450 		DPRINTF(("%s: no CardBus card on cb%d\n", __func__,
    451 		    device_unit(&sc->sc_dev)));
    452 		return (0);
    453 	}
    454 
    455 	/*
    456 	 * XXX use fake function 8 to keep power on during whole
    457 	 * configuration.
    458 	 */
    459 	enable_function(sc, cdstatus, 8);
    460 	function = 0;
    461 
    462 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, function);
    463 
    464 	/*
    465 	 * Wait until power comes up.  Maxmum 500 ms.
    466 	 *
    467 	 * XXX What is this for?  The bridge driver ought to have waited
    468 	 * XXX already.
    469 	 */
    470 	{
    471 		int i;
    472 
    473 		for (i = 0; i < 5; ++i) {
    474 			id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    475 			if (id != 0xffffffff && id != 0) {
    476 				break;
    477 			}
    478 			if (cold) {	/* before kernel thread invoked */
    479 				delay(100 * 1000);
    480 			} else {	/* thread context */
    481 				if (tsleep((void *)sc, PCATCH, "cardbus",
    482 				    hz / 10) != EWOULDBLOCK) {
    483 					break;
    484 				}
    485 			}
    486 		}
    487 		aprint_debug_dev(self, "id reg valid in %d iterations\n", i);
    488 		if (i == 5) {
    489 			return (EIO);
    490 		}
    491 	}
    492 
    493 	bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    494 	DPRINTF(("%s bhlc 0x%08x -> ", device_xname(&sc->sc_dev), bhlc));
    495 	nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
    496 
    497 	for (function = 0; function < nfunction; function++) {
    498 		struct cardbus_attach_args ca;
    499 		int locs[CARDBUSCF_NLOCS];
    500 
    501 		if (locators[CARDBUSCF_FUNCTION] !=
    502 		    CARDBUSCF_FUNCTION_DEFAULT &&
    503 		    locators[CARDBUSCF_FUNCTION] != function)
    504 			continue;
    505 
    506 		if (sc->sc_funcs[function])
    507 			continue;
    508 
    509 		tag = cardbus_make_tag(cc, cf, sc->sc_bus, function);
    510 
    511 		id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    512 		class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
    513 		cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
    514 
    515 		/* Invalid vendor ID value? */
    516 		if (CARDBUS_VENDOR(id) == PCI_VENDOR_INVALID) {
    517 			continue;
    518 		}
    519 
    520 		DPRINTF(("cardbus_attach_card: "
    521 		    "Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
    522 		    CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
    523 
    524 		enable_function(sc, cdstatus, function);
    525 
    526 		/* clean up every BAR */
    527 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
    528 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
    529 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
    530 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
    531 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
    532 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
    533 		cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
    534 
    535 		/* set initial latency and cacheline size */
    536 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    537 		icr = cardbus_conf_read(cc, cf, tag, CARDBUS_INTERRUPT_REG);
    538 		DPRINTF(("%s func%d icr 0x%08x bhlc 0x%08x -> ",
    539 		    device_xname(&sc->sc_dev), function, icr, bhlc));
    540 		bhlc &= ~(CARDBUS_CACHELINE_MASK << CARDBUS_CACHELINE_SHIFT);
    541 		bhlc |= (sc->sc_cacheline & CARDBUS_CACHELINE_MASK) <<
    542 		    CARDBUS_CACHELINE_SHIFT;
    543 		/*
    544 		 * Set the initial value of the Latency Timer.
    545 		 *
    546 		 * While a PCI device owns the bus, its Latency
    547 		 * Timer counts down bus cycles from its initial
    548 		 * value to 0.  Minimum Grant tells for how long
    549 		 * the device wants to own the bus once it gets
    550 		 * access, in units of 250ns.
    551 		 *
    552 		 * On a 33 MHz bus, there are 8 cycles per 250ns.
    553 		 * So I multiply the Minimum Grant by 8 to find
    554 		 * out the initial value of the Latency Timer.
    555 		 *
    556 		 * Avoid setting a Latency Timer less than 0x10,
    557 		 * since the old code did not do that.
    558 		 */
    559 		lattimer =
    560 		    MIN(sc->sc_max_lattimer, MAX(0x10, 8 * PCI_MIN_GNT(icr)));
    561 		if (PCI_LATTIMER(bhlc) < lattimer) {
    562 			bhlc &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    563 			bhlc |= (lattimer << PCI_LATTIMER_SHIFT);
    564 		}
    565 
    566 		cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    567 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    568 		DPRINTF(("0x%08x\n", bhlc));
    569 
    570 		/*
    571 		 * We need to allocate the ct here, since we might
    572 		 * need it when reading the CIS
    573 		 */
    574 		if ((ct = malloc(sizeof(struct cardbus_devfunc),
    575 		    M_DEVBUF, M_NOWAIT)) == NULL) {
    576 			panic("no room for cardbus_tag");
    577 		}
    578 
    579 		ct->ct_bhlc = bhlc;
    580 		ct->ct_cc = sc->sc_cc;
    581 		ct->ct_cf = sc->sc_cf;
    582 		ct->ct_bus = sc->sc_bus;
    583 		ct->ct_func = function;
    584 		ct->ct_sc = sc;
    585 		sc->sc_funcs[function] = ct;
    586 
    587 		memset(&ca, 0, sizeof(ca));
    588 
    589 		ca.ca_ct = ct;
    590 
    591 		ca.ca_iot = sc->sc_iot;
    592 		ca.ca_memt = sc->sc_memt;
    593 		ca.ca_dmat = sc->sc_dmat;
    594 
    595 #if rbus
    596 		ca.ca_rbus_iot = sc->sc_rbus_iot;
    597 		ca.ca_rbus_memt= sc->sc_rbus_memt;
    598 #endif
    599 
    600 		ca.ca_tag = tag;
    601 		ca.ca_bus = sc->sc_bus;
    602 		ca.ca_function = function;
    603 		ca.ca_id = id;
    604 		ca.ca_class = class;
    605 
    606 		ca.ca_intrline = sc->sc_intrline;
    607 
    608 		if (cis_ptr != 0) {
    609 #define TUPLESIZE 2048
    610 			u_int8_t *tuple = malloc(TUPLESIZE, M_DEVBUF, M_WAITOK);
    611 			if (cardbus_read_tuples(&ca, cis_ptr,
    612 			    tuple, TUPLESIZE)) {
    613 				printf("cardbus_attach_card: "
    614 				    "failed to read CIS\n");
    615 			} else {
    616 #ifdef CARDBUS_DEBUG
    617 				decode_tuples(tuple, TUPLESIZE,
    618 				    print_tuple, NULL);
    619 #endif
    620 				decode_tuples(tuple, TUPLESIZE,
    621 				    parse_tuple, &ca.ca_cis);
    622 			}
    623 			free(tuple, M_DEVBUF);
    624 		}
    625 
    626 		locs[CARDBUSCF_FUNCTION] = function;
    627 
    628 		if ((csc = config_found_sm_loc((void *)sc, "cardbus", locs,
    629 		    &ca, cardbusprint, config_stdsubmatch)) == NULL) {
    630 			/* do not match */
    631 			disable_function(sc, function);
    632 			sc->sc_funcs[function] = NULL;
    633 			free(ct, M_DEVBUF);
    634 		} else {
    635 			/* found */
    636 			ct->ct_device = csc;
    637 		}
    638 	}
    639 	/*
    640 	 * XXX power down pseudo function 8 (this will power down the card
    641 	 * if no functions were attached).
    642 	 */
    643 	disable_function(sc, 8);
    644 
    645 	return (0);
    646 }
    647 
    648 static int
    649 cardbusprint(void *aux, const char *pnp)
    650 {
    651 	struct cardbus_attach_args *ca = aux;
    652 	char devinfo[256];
    653 	int i;
    654 
    655 	if (pnp) {
    656 		pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo,
    657 		    sizeof(devinfo));
    658 		for (i = 0; i < 4; i++) {
    659 			if (ca->ca_cis.cis1_info[i] == NULL)
    660 				break;
    661 			if (i)
    662 				aprint_normal(", ");
    663 			aprint_normal("%s", ca->ca_cis.cis1_info[i]);
    664 		}
    665 		aprint_verbose("%s(manufacturer 0x%x, product 0x%x)",
    666 		    i ? " " : "",
    667 		    ca->ca_cis.manufacturer, ca->ca_cis.product);
    668 		aprint_normal(" %s at %s", devinfo, pnp);
    669 	}
    670 	aprint_normal(" function %d", ca->ca_function);
    671 
    672 	return (UNCONF);
    673 }
    674 
    675 /*
    676  * void cardbus_detach_card(struct cardbus_softc *sc)
    677  *
    678  *    This function detaches the card on the slot: detach device data
    679  *    structure and turns off the power.
    680  *
    681  *    This function must not be called under interrupt context.
    682  */
    683 void
    684 cardbus_detach_card(struct cardbus_softc *sc)
    685 {
    686 	int f;
    687 	struct cardbus_devfunc *ct;
    688 
    689 	for (f = 0; f < 8; f++) {
    690 		ct = sc->sc_funcs[f];
    691 		if (!ct)
    692 			continue;
    693 
    694 		DPRINTF(("%s: detaching %s\n", device_xname(&sc->sc_dev),
    695 		    device_xname(ct->ct_device)));
    696 		/* call device detach function */
    697 
    698 		if (config_detach(ct->ct_device, 0) != 0) {
    699 			aprint_error_dev(&sc->sc_dev,
    700 			    "cannot detach dev %s, function %d\n",
    701 			    device_xname(ct->ct_device), ct->ct_func);
    702 		}
    703 	}
    704 
    705 	sc->sc_poweron_func = 0;
    706 	(*sc->sc_cf->cardbus_power)(sc->sc_cc,
    707 	    CARDBUS_VCC_0V | CARDBUS_VPP_0V);
    708 }
    709 
    710 void
    711 cardbus_childdetached(device_t self, device_t child)
    712 {
    713 	struct cardbus_softc *sc = device_private(self);
    714 	struct cardbus_devfunc *ct;
    715 
    716 	ct = sc->sc_funcs[device_locator(child, CARDBUSCF_FUNCTION)];
    717 	KASSERT(ct->ct_device == child);
    718 
    719 	sc->sc_poweron_func &= ~(1 << ct->ct_func);
    720 	sc->sc_funcs[ct->ct_func] = NULL;
    721 	free(ct, M_DEVBUF);
    722 }
    723 
    724 /*
    725  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    726  *   Interrupt handler of pccard.
    727  *  args:
    728  *   cardbus_chipset_tag_t *cc
    729  *   int irq:
    730  */
    731 void *
    732 cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    733     cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg)
    734 {
    735 
    736 	DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    737 	return ((*cf->cardbus_intr_establish)(cc, irq, level, func, arg));
    738 }
    739 
    740 /*
    741  * void cardbus_intr_disestablish(cc, cf, handler)
    742  *   Interrupt handler of pccard.
    743  *  args:
    744  *   cardbus_chipset_tag_t *cc
    745  */
    746 void
    747 cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    748     void *handler)
    749 {
    750 
    751 	DPRINTF(("- pccard_intr_disestablish\n"));
    752 	(*cf->cardbus_intr_disestablish)(cc, handler);
    753 }
    754 
    755 /*
    756  * XXX this should be merged with cardbus_function_{enable,disable},
    757  * but we don't have a ct when these functions are called.
    758  */
    759 static void
    760 enable_function(struct cardbus_softc *sc, int cdstatus, int function)
    761 {
    762 
    763 	if (sc->sc_poweron_func == 0) {
    764 		/* switch to 3V and/or wait for power to stabilize */
    765 		if (cdstatus & CARDBUS_3V_CARD) {
    766 			/*
    767 			 * sc_poweron_func must be substituted before
    768 			 * entering sleep, in order to avoid turn on
    769 			 * power twice.
    770 			 */
    771 			sc->sc_poweron_func |= (1 << function);
    772 			(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_3V);
    773 		} else {
    774 			/* No cards other than 3.3V cards. */
    775 			return;
    776 		}
    777 		(*sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
    778 	}
    779 	sc->sc_poweron_func |= (1 << function);
    780 }
    781 
    782 static void
    783 disable_function(struct cardbus_softc *sc, int function)
    784 {
    785 
    786 	sc->sc_poweron_func &= ~(1 << function);
    787 	if (sc->sc_poweron_func == 0) {
    788 		/* power-off because no functions are enabled */
    789 		(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_0V);
    790 	}
    791 }
    792 
    793 /*
    794  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
    795  *
    796  *   This function enables a function on a card.  When no power is
    797  *  applied on the card, power will be applied on it.
    798  */
    799 int
    800 cardbus_function_enable(struct cardbus_softc *sc, int func)
    801 {
    802 	cardbus_chipset_tag_t cc = sc->sc_cc;
    803 	cardbus_function_tag_t cf = sc->sc_cf;
    804 	cardbus_devfunc_t ct;
    805 	cardbusreg_t command;
    806 	cardbustag_t tag;
    807 
    808 	DPRINTF(("entering cardbus_function_enable...  "));
    809 
    810 	/* entering critical area */
    811 
    812 	/* XXX: sc_vold should be used */
    813 	enable_function(sc, CARDBUS_3V_CARD, func);
    814 
    815 	/* exiting critical area */
    816 
    817 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
    818 
    819 	command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    820 	command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE |
    821 	    CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    822 
    823 	cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    824 
    825 	if ((ct = sc->sc_funcs[func]) != NULL)
    826 		Cardbus_conf_write(ct, tag, CARDBUS_BHLC_REG, ct->ct_bhlc);
    827 
    828 	cardbus_free_tag(cc, cf, tag);
    829 
    830 	DPRINTF(("%x\n", sc->sc_poweron_func));
    831 
    832 	return (0);
    833 }
    834 
    835 /*
    836  * int cardbus_function_disable(struct cardbus_softc *, int func)
    837  *
    838  *   This function disable a function on a card.  When no functions are
    839  *  enabled, it turns off the power.
    840  */
    841 int
    842 cardbus_function_disable(struct cardbus_softc *sc, int func)
    843 {
    844 
    845 	DPRINTF(("entering cardbus_function_disable...  "));
    846 
    847 	disable_function(sc, func);
    848 
    849 	return (0);
    850 }
    851 
    852 /*
    853  * int cardbus_get_capability(cardbus_chipset_tag_t cc,
    854  *	cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
    855  *	cardbusreg_t *value)
    856  *
    857  *	Find the specified PCI capability.
    858  */
    859 int
    860 cardbus_get_capability(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    861     cardbustag_t tag, int capid, int *offset, cardbusreg_t *value)
    862 {
    863 	cardbusreg_t reg;
    864 	unsigned int ofs;
    865 
    866 	reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
    867 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
    868 		return (0);
    869 
    870 	ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
    871 	    PCI_CAPLISTPTR_REG));
    872 	while (ofs != 0) {
    873 #ifdef DIAGNOSTIC
    874 		if ((ofs & 3) || (ofs < 0x40))
    875 			panic("cardbus_get_capability");
    876 #endif
    877 		reg = cardbus_conf_read(cc, cf, tag, ofs);
    878 		if (PCI_CAPLIST_CAP(reg) == capid) {
    879 			if (offset)
    880 				*offset = ofs;
    881 			if (value)
    882 				*value = reg;
    883 			return (1);
    884 		}
    885 		ofs = PCI_CAPLIST_NEXT(reg);
    886 	}
    887 
    888 	return (0);
    889 }
    890 
    891 /*
    892  * below this line, there are some functions for decoding tuples.
    893  * They should go out from this file.
    894  */
    895 
    896 static u_int8_t *
    897 decode_tuple(u_int8_t *, u_int8_t *, tuple_decode_func, void *);
    898 
    899 static int
    900 decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
    901 {
    902 	u_int8_t *tp = tuple;
    903 
    904 	if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
    905 		DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    906 		return (0);
    907 	}
    908 
    909 	while ((tp = decode_tuple(tp, tuple + buflen, func, data)) != NULL)
    910 		;
    911 
    912 	return (1);
    913 }
    914 
    915 static u_int8_t *
    916 decode_tuple(u_int8_t *tuple, u_int8_t *end,
    917     tuple_decode_func func, void *data)
    918 {
    919 	u_int8_t type;
    920 	u_int8_t len;
    921 
    922 	type = tuple[0];
    923 	switch (type) {
    924 	case PCMCIA_CISTPL_NULL:
    925 	case PCMCIA_CISTPL_END:
    926 		len = 1;
    927 		break;
    928 	default:
    929 		if (tuple + 2 > end)
    930 			return (NULL);
    931 		len = tuple[1] + 2;
    932 		break;
    933 	}
    934 
    935 	if (tuple + len > end)
    936 		return (NULL);
    937 
    938 	(*func)(tuple, len, data);
    939 
    940 	if (type == PCMCIA_CISTPL_END || tuple + len == end)
    941 		return (NULL);
    942 
    943 	return (tuple + len);
    944 }
    945 
    946 /*
    947  * XXX: this is another reason why this code should be shared with PCI.
    948  */
    949 static int
    950 cardbus_get_powerstate_int(cardbus_devfunc_t ct, cardbustag_t tag,
    951     cardbusreg_t *state, int offset)
    952 {
    953 	cardbusreg_t value, now;
    954 	cardbus_chipset_tag_t cc = ct->ct_cc;
    955 	cardbus_function_tag_t cf = ct->ct_cf;
    956 
    957 	value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR);
    958 	now = value & PCI_PMCSR_STATE_MASK;
    959 	switch (now) {
    960 	case PCI_PMCSR_STATE_D0:
    961 	case PCI_PMCSR_STATE_D1:
    962 	case PCI_PMCSR_STATE_D2:
    963 	case PCI_PMCSR_STATE_D3:
    964 		*state = now;
    965 		return 0;
    966 	default:
    967 		return EINVAL;
    968 	}
    969 }
    970 
    971 int
    972 cardbus_get_powerstate(cardbus_devfunc_t ct, cardbustag_t tag,
    973     cardbusreg_t *state)
    974 {
    975 	cardbus_chipset_tag_t cc = ct->ct_cc;
    976 	cardbus_function_tag_t cf = ct->ct_cf;
    977 	int offset;
    978 	cardbusreg_t value;
    979 
    980 	if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset, &value))
    981 		return EOPNOTSUPP;
    982 
    983 	return cardbus_get_powerstate_int(ct, tag, state, offset);
    984 }
    985 
    986 static int
    987 cardbus_set_powerstate_int(cardbus_devfunc_t ct, cardbustag_t tag,
    988     cardbusreg_t state, int offset, cardbusreg_t cap_reg)
    989 {
    990 	cardbus_chipset_tag_t cc = ct->ct_cc;
    991 	cardbus_function_tag_t cf = ct->ct_cf;
    992 
    993 	cardbusreg_t value, cap, now;
    994 
    995 	KASSERT((offset & 0x3) == 0);
    996 
    997 	cap = cap_reg >> PCI_PMCR_SHIFT;
    998 	value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR);
    999 	now = value & PCI_PMCSR_STATE_MASK;
   1000 	value &= ~PCI_PMCSR_STATE_MASK;
   1001 
   1002 	if (now == state)
   1003 		return 0;
   1004 	switch (state) {
   1005 	case PCI_PMCSR_STATE_D0:
   1006 		value |= PCI_PMCSR_STATE_D0;
   1007 		break;
   1008 	case PCI_PMCSR_STATE_D1:
   1009 		if (now == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D3) {
   1010 			printf("invalid transition from %d to D1\n", (int)now);
   1011 			return EINVAL;
   1012 		}
   1013 		if (!(cap & PCI_PMCR_D1SUPP)) {
   1014 			printf("D1 not supported\n");
   1015 			return EOPNOTSUPP;
   1016 		}
   1017 		value |= PCI_PMCSR_STATE_D1;
   1018 		break;
   1019 	case PCI_PMCSR_STATE_D2:
   1020 		if (now == PCI_PMCSR_STATE_D3) {
   1021 			printf("invalid transition from %d to D2\n", (int)now);
   1022 			return EINVAL;
   1023 		}
   1024 		if (!(cap & PCI_PMCR_D2SUPP)) {
   1025 			printf("D2 not supported\n");
   1026 			return EOPNOTSUPP;
   1027 		}
   1028 		value |= PCI_PMCSR_STATE_D2;
   1029 		break;
   1030 	case PCI_PMCSR_STATE_D3:
   1031 		value |= PCI_PMCSR_STATE_D3;
   1032 		break;
   1033 	default:
   1034 		return EINVAL;
   1035 	}
   1036 
   1037 	cardbus_conf_write(cc, cf, tag, offset + PCI_PMCSR, value);
   1038 	if (state == PCI_PMCSR_STATE_D3 || now == PCI_PMCSR_STATE_D3)
   1039 		DELAY(10000);
   1040 	else if (state == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D2)
   1041 		DELAY(200);
   1042 
   1043 	return 0;
   1044 }
   1045 
   1046 int
   1047 cardbus_set_powerstate(cardbus_devfunc_t ct, cardbustag_t tag, cardbusreg_t state)
   1048 {
   1049 	cardbus_chipset_tag_t cc = ct->ct_cc;
   1050 	cardbus_function_tag_t cf = ct->ct_cf;
   1051 	int offset;
   1052 	cardbusreg_t value;
   1053 
   1054 	if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset,
   1055 	    &value))
   1056 		return EOPNOTSUPP;
   1057 
   1058 	return cardbus_set_powerstate_int(ct, tag, state, offset, value);
   1059 }
   1060 
   1061 #ifdef CARDBUS_DEBUG
   1062 static const char *tuple_name(int);
   1063 static const char *tuple_names[] = {
   1064 	"TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
   1065 	"CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
   1066 	"Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
   1067 	"Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
   1068 	"CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
   1069 	"NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
   1070 	"JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
   1071 	"DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
   1072 	"MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
   1073 	"Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
   1074 	"Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
   1075 	"Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
   1076 	"Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
   1077 	"Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
   1078 	"Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
   1079 	"Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
   1080 	"VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
   1081 	"DATE", "BATTERY", "ORG"
   1082 };
   1083 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
   1084 
   1085 static const char *
   1086 tuple_name(int type)
   1087 {
   1088 
   1089 	if (0 <= type && type < NAME_LEN(tuple_names)) {
   1090 		return (tuple_names[type]);
   1091 	} else if (type == 0xff) {
   1092 		return ("END");
   1093 	} else {
   1094 		return ("Reserved");
   1095 	}
   1096 }
   1097 
   1098 static void
   1099 print_tuple(u_int8_t *tuple, int len, void *data)
   1100 {
   1101 	int i;
   1102 
   1103 	printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
   1104 
   1105 	for (i = 0; i < len; ++i) {
   1106 		if (i % 16 == 0) {
   1107 			printf("  0x%2x:", i);
   1108 		}
   1109 		printf(" %x", tuple[i]);
   1110 		if (i % 16 == 15) {
   1111 			printf("\n");
   1112 		}
   1113 	}
   1114 	if (i % 16 != 0) {
   1115 		printf("\n");
   1116 	}
   1117 }
   1118 #endif
   1119 
   1120 void
   1121 cardbus_conf_capture(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
   1122     cardbustag_t tag, struct cardbus_conf_state *pcs)
   1123 {
   1124 	int off;
   1125 
   1126 	for (off = 0; off < 16; off++)
   1127 		pcs->reg[off] = cardbus_conf_read(cc, cf, tag, (off * 4));
   1128 }
   1129 
   1130 void
   1131 cardbus_conf_restore(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
   1132     cardbustag_t tag, struct cardbus_conf_state *pcs)
   1133 {
   1134 	int off;
   1135 	cardbusreg_t val;
   1136 
   1137 	for (off = 15; off >= 0; off--) {
   1138 		val = cardbus_conf_read(cc, cf, tag, (off * 4));
   1139 		if (val != pcs->reg[off])
   1140 			cardbus_conf_write(cc, cf,tag, (off * 4), pcs->reg[off]);
   1141 	}
   1142 }
   1143 
   1144 void
   1145 cardbus_disable_retry(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
   1146     cardbustag_t tag)
   1147 {
   1148 	/* See comment on sys/dev/pci/pci.c:pci_disable_retry() for
   1149 	 * the reason I comment-out this code.
   1150 	 */
   1151 #if 0
   1152 	cardbusreg_t retry;
   1153 
   1154 	retry = cardbus_conf_read(cc, cf, tag, PCI_RETRY_TIMEOUT_REG);
   1155 	retry &= ~PCI_RETRY_TIMEOUT_REG_MASK;
   1156 	cardbus_conf_write(cc, cf, tag, PCI_RETRY_TIMEOUT_REG, retry);
   1157 #endif
   1158 }
   1159 
   1160 struct cardbus_child_power {
   1161 	struct cardbus_conf_state p_cardbusconf;
   1162 	cardbus_devfunc_t p_ct;
   1163 	cardbustag_t p_tag;
   1164 	cardbus_chipset_tag_t p_cc;
   1165 	cardbus_function_tag_t p_cf;
   1166 	cardbusreg_t p_pm_cap;
   1167 	bool p_has_pm;
   1168 	int p_pm_offset;
   1169 };
   1170 
   1171 static bool
   1172 cardbus_child_suspend(device_t dv PMF_FN_ARGS)
   1173 {
   1174 	struct cardbus_child_power *priv = device_pmf_bus_private(dv);
   1175 
   1176 	cardbus_conf_capture(priv->p_cc, priv->p_cf, priv->p_tag,
   1177 	    &priv->p_cardbusconf);
   1178 
   1179 	if (priv->p_has_pm &&
   1180 	    cardbus_set_powerstate_int(priv->p_ct, priv->p_tag,
   1181 	    PCI_PMCSR_STATE_D3, priv->p_pm_offset, priv->p_pm_cap)) {
   1182 		aprint_error_dev(dv, "unsupported state, continuing.\n");
   1183 		return false;
   1184 	}
   1185 
   1186 	Cardbus_function_disable(priv->p_ct);
   1187 
   1188 	return true;
   1189 }
   1190 
   1191 static bool
   1192 cardbus_child_resume(device_t dv PMF_FN_ARGS)
   1193 {
   1194 	struct cardbus_child_power *priv = device_pmf_bus_private(dv);
   1195 
   1196 	Cardbus_function_enable(priv->p_ct);
   1197 
   1198 	if (priv->p_has_pm &&
   1199 	    cardbus_set_powerstate_int(priv->p_ct, priv->p_tag,
   1200 	    PCI_PMCSR_STATE_D0, priv->p_pm_offset, priv->p_pm_cap)) {
   1201 		aprint_error_dev(dv, "unsupported state, continuing.\n");
   1202 		return false;
   1203 	}
   1204 
   1205 	cardbus_conf_restore(priv->p_cc, priv->p_cf, priv->p_tag,
   1206 	    &priv->p_cardbusconf);
   1207 
   1208 	return true;
   1209 }
   1210 
   1211 static void
   1212 cardbus_child_deregister(device_t dv)
   1213 {
   1214 	struct cardbus_child_power *priv = device_pmf_bus_private(dv);
   1215 
   1216 	free(priv, M_DEVBUF);
   1217 }
   1218 
   1219 static bool
   1220 cardbus_child_register(device_t child)
   1221 {
   1222 	device_t self = device_parent(child);
   1223 	struct cardbus_softc *sc = device_private(self);
   1224 	struct cardbus_devfunc *ct;
   1225 	struct cardbus_child_power *priv;
   1226 	int off;
   1227 	cardbusreg_t reg;
   1228 
   1229 	ct = sc->sc_funcs[device_locator(child, CARDBUSCF_FUNCTION)];
   1230 
   1231 	priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK);
   1232 
   1233 	priv->p_ct = ct;
   1234 	priv->p_cc = ct->ct_cc;
   1235 	priv->p_cf = ct->ct_cf;
   1236 	priv->p_tag = cardbus_make_tag(priv->p_cc, priv->p_cf, ct->ct_bus,
   1237 	    ct->ct_func);
   1238 
   1239 	if (cardbus_get_capability(priv->p_cc, priv->p_cf, priv->p_tag,
   1240 				   PCI_CAP_PWRMGMT, &off, &reg)) {
   1241 		priv->p_has_pm = true;
   1242 		priv->p_pm_offset = off;
   1243 		priv->p_pm_cap = reg;
   1244 	} else {
   1245 		priv->p_has_pm = false;
   1246 		priv->p_pm_offset = -1;
   1247 	}
   1248 
   1249 	device_pmf_bus_register(child, priv, cardbus_child_suspend,
   1250 	    cardbus_child_resume, cardbus_child_deregister);
   1251 
   1252 	return true;
   1253 }
   1254