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