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