Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.83
      1 /*	$NetBSD: cardbus.c,v 1.83 2008/01/02 02:05:19 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.83 2008/01/02 02:05:19 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(struct device *, struct device *, void *);
     73 STATIC int cardbusdetach(device_t, int);
     74 STATIC int cardbusmatch(struct device *, struct cfdata *, void *);
     75 int cardbus_rescan(struct device *, const char *, const int *);
     76 void cardbus_childdetached(struct device *, struct device *);
     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(struct device *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(struct device *parent, struct device *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 		    sc->sc_dev.dv_xname));
    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 			    sc->sc_dev.dv_xname));
    215 		} else {
    216 			reg = CARDBUS_CIS_ASI_BAR(cardbus_space);
    217 			DPRINTF(("%s: reading CIS data from BAR%d\n",
    218 			    sc->sc_dev.dv_xname, 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 			printf("%s: failed to map memory\n",
    230 			    sc->sc_dev.dv_xname);
    231 			return (1);
    232 		}
    233 		aprint_debug_dev(&sc->sc_dev, "mapped %ju bytes at 0x%jx\n",
    234 		    (uintmax_t)bar_size, (uintmax_t)bar_addr);
    235 
    236 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
    237 			cardbusreg_t exrom;
    238 			int save;
    239 			struct cardbus_rom_image_head rom_image;
    240 			struct cardbus_rom_image *p;
    241 
    242 			save = splhigh();
    243 			/* enable rom address decoder */
    244 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    245 			cardbus_conf_write(cc, cf, tag, reg, exrom | 1);
    246 
    247 			command = cardbus_conf_read(cc, cf, tag,
    248 			    CARDBUS_COMMAND_STATUS_REG);
    249 			cardbus_conf_write(cc, cf, tag,
    250 			    CARDBUS_COMMAND_STATUS_REG,
    251 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    252 
    253 			if (cardbus_read_exrom(bar_tag, bar_memh, &rom_image))
    254 				goto out;
    255 
    256 			SIMPLEQ_FOREACH(p, &rom_image, next) {
    257 				if (p->rom_image ==
    258 				    CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
    259 					bus_space_read_region_1(p->romt,
    260 					    p->romh, CARDBUS_CIS_ADDR(cis_ptr),
    261 					    tuples, MIN(p->image_size, len));
    262 					found++;
    263 					break;
    264 				}
    265 			}
    266 			while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
    267 				SIMPLEQ_REMOVE_HEAD(&rom_image, next);
    268 				free(p, M_DEVBUF);
    269 			}
    270 		out:
    271 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    272 			cardbus_conf_write(cc, cf, tag, reg, exrom & ~1);
    273 			splx(save);
    274 		} else {
    275 			command = cardbus_conf_read(cc, cf, tag,
    276 			    CARDBUS_COMMAND_STATUS_REG);
    277 			cardbus_conf_write(cc, cf, tag,
    278 			    CARDBUS_COMMAND_STATUS_REG,
    279 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    280 			/* XXX byte order? */
    281 			bus_space_read_region_1(bar_tag, bar_memh,
    282 			    cis_ptr, tuples, MIN(bar_size, 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)", sc->sc_dev.dv_xname,
    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(struct device *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 	struct device *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 		if (i == 5) {
    488 			return (EIO);
    489 		}
    490 	}
    491 
    492 	bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    493 	DPRINTF(("%s bhlc 0x%08x -> ", sc->sc_dev.dv_xname, bhlc));
    494 	nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
    495 
    496 	for (function = 0; function < nfunction; function++) {
    497 		struct cardbus_attach_args ca;
    498 		int locs[CARDBUSCF_NLOCS];
    499 
    500 		if (locators[CARDBUSCF_FUNCTION] !=
    501 		    CARDBUSCF_FUNCTION_DEFAULT &&
    502 		    locators[CARDBUSCF_FUNCTION] != function)
    503 			continue;
    504 
    505 		if (sc->sc_funcs[function])
    506 			continue;
    507 
    508 		tag = cardbus_make_tag(cc, cf, sc->sc_bus, function);
    509 
    510 		id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    511 		class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
    512 		cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
    513 
    514 		/* Invalid vendor ID value? */
    515 		if (CARDBUS_VENDOR(id) == PCI_VENDOR_INVALID) {
    516 			continue;
    517 		}
    518 
    519 		DPRINTF(("cardbus_attach_card: "
    520 		    "Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
    521 		    CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
    522 
    523 		enable_function(sc, cdstatus, function);
    524 
    525 		/* clean up every BAR */
    526 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
    527 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
    528 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
    529 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
    530 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
    531 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
    532 		cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
    533 
    534 		/* set initial latency and cacheline size */
    535 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    536 		icr = cardbus_conf_read(cc, cf, tag, CARDBUS_INTERRUPT_REG);
    537 		DPRINTF(("%s func%d icr 0x%08x bhlc 0x%08x -> ",
    538 		    device_xname(&sc->sc_dev), function, icr, bhlc));
    539 		bhlc &= ~(CARDBUS_CACHELINE_MASK << CARDBUS_CACHELINE_SHIFT);
    540 		bhlc |= (sc->sc_cacheline & CARDBUS_CACHELINE_MASK) <<
    541 		    CARDBUS_CACHELINE_SHIFT;
    542 		/*
    543 		 * Set the initial value of the Latency Timer.
    544 		 *
    545 		 * While a PCI device owns the bus, its Latency
    546 		 * Timer counts down bus cycles from its initial
    547 		 * value to 0.  Minimum Grant tells for how long
    548 		 * the device wants to own the bus once it gets
    549 		 * access, in units of 250ns.
    550 		 *
    551 		 * On a 33 MHz bus, there are 8 cycles per 250ns.
    552 		 * So I multiply the Minimum Grant by 8 to find
    553 		 * out the initial value of the Latency Timer.
    554 		 *
    555 		 * Avoid setting a Latency Timer less than 0x10,
    556 		 * since the old code did not do that.
    557 		 */
    558 		lattimer =
    559 		    MIN(sc->sc_max_lattimer, MAX(0x10, 8 * PCI_MIN_GNT(icr)));
    560 		if (PCI_LATTIMER(bhlc) < lattimer) {
    561 			bhlc &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    562 			bhlc |= (lattimer << PCI_LATTIMER_SHIFT);
    563 		}
    564 
    565 		cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    566 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    567 		DPRINTF(("0x%08x\n", bhlc));
    568 
    569 		/*
    570 		 * We need to allocate the ct here, since we might
    571 		 * need it when reading the CIS
    572 		 */
    573 		if ((ct = malloc(sizeof(struct cardbus_devfunc),
    574 		    M_DEVBUF, M_NOWAIT)) == NULL) {
    575 			panic("no room for cardbus_tag");
    576 		}
    577 
    578 		ct->ct_bhlc = bhlc;
    579 		ct->ct_cc = sc->sc_cc;
    580 		ct->ct_cf = sc->sc_cf;
    581 		ct->ct_bus = sc->sc_bus;
    582 		ct->ct_func = function;
    583 		ct->ct_sc = sc;
    584 		sc->sc_funcs[function] = ct;
    585 
    586 		memset(&ca, 0, sizeof(ca));
    587 
    588 		ca.ca_ct = ct;
    589 
    590 		ca.ca_iot = sc->sc_iot;
    591 		ca.ca_memt = sc->sc_memt;
    592 		ca.ca_dmat = sc->sc_dmat;
    593 
    594 #if rbus
    595 		ca.ca_rbus_iot = sc->sc_rbus_iot;
    596 		ca.ca_rbus_memt= sc->sc_rbus_memt;
    597 #endif
    598 
    599 		ca.ca_tag = tag;
    600 		ca.ca_bus = sc->sc_bus;
    601 		ca.ca_function = function;
    602 		ca.ca_id = id;
    603 		ca.ca_class = class;
    604 
    605 		ca.ca_intrline = sc->sc_intrline;
    606 
    607 		if (cis_ptr != 0) {
    608 #define TUPLESIZE 2048
    609 			u_int8_t *tuple = malloc(TUPLESIZE, M_DEVBUF, M_WAITOK);
    610 			if (cardbus_read_tuples(&ca, cis_ptr,
    611 			    tuple, TUPLESIZE)) {
    612 				printf("cardbus_attach_card: "
    613 				    "failed to read CIS\n");
    614 			} else {
    615 #ifdef CARDBUS_DEBUG
    616 				decode_tuples(tuple, TUPLESIZE,
    617 				    print_tuple, NULL);
    618 #endif
    619 				decode_tuples(tuple, TUPLESIZE,
    620 				    parse_tuple, &ca.ca_cis);
    621 			}
    622 			free(tuple, M_DEVBUF);
    623 		}
    624 
    625 		locs[CARDBUSCF_FUNCTION] = function;
    626 
    627 		if ((csc = config_found_sm_loc((void *)sc, "cardbus", locs,
    628 		    &ca, cardbusprint, config_stdsubmatch)) == NULL) {
    629 			/* do not match */
    630 			disable_function(sc, function);
    631 			sc->sc_funcs[function] = NULL;
    632 			free(ct, M_DEVBUF);
    633 		} else {
    634 			/* found */
    635 			ct->ct_device = csc;
    636 		}
    637 	}
    638 	/*
    639 	 * XXX power down pseudo function 8 (this will power down the card
    640 	 * if no functions were attached).
    641 	 */
    642 	disable_function(sc, 8);
    643 
    644 	return (0);
    645 }
    646 
    647 static int
    648 cardbusprint(void *aux, const char *pnp)
    649 {
    650 	struct cardbus_attach_args *ca = aux;
    651 	char devinfo[256];
    652 	int i;
    653 
    654 	if (pnp) {
    655 		pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo,
    656 		    sizeof(devinfo));
    657 		for (i = 0; i < 4; i++) {
    658 			if (ca->ca_cis.cis1_info[i] == NULL)
    659 				break;
    660 			if (i)
    661 				aprint_normal(", ");
    662 			aprint_normal("%s", ca->ca_cis.cis1_info[i]);
    663 		}
    664 		aprint_verbose("%s(manufacturer 0x%x, product 0x%x)",
    665 		    i ? " " : "",
    666 		    ca->ca_cis.manufacturer, ca->ca_cis.product);
    667 		aprint_normal(" %s at %s", devinfo, pnp);
    668 	}
    669 	aprint_normal(" function %d", ca->ca_function);
    670 
    671 	return (UNCONF);
    672 }
    673 
    674 /*
    675  * void cardbus_detach_card(struct cardbus_softc *sc)
    676  *
    677  *    This function detaches the card on the slot: detach device data
    678  *    structure and turns off the power.
    679  *
    680  *    This function must not be called under interrupt context.
    681  */
    682 void
    683 cardbus_detach_card(struct cardbus_softc *sc)
    684 {
    685 	int f;
    686 	struct cardbus_devfunc *ct;
    687 
    688 	for (f = 0; f < 8; f++) {
    689 		ct = sc->sc_funcs[f];
    690 		if (!ct)
    691 			continue;
    692 
    693 		DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname,
    694 		    ct->ct_device->dv_xname));
    695 		/* call device detach function */
    696 
    697 		if (config_detach(ct->ct_device, 0) != 0) {
    698 			printf("%s: cannot detach dev %s, function %d\n",
    699 			    sc->sc_dev.dv_xname, ct->ct_device->dv_xname,
    700 			    ct->ct_func);
    701 		}
    702 	}
    703 
    704 	sc->sc_poweron_func = 0;
    705 	(*sc->sc_cf->cardbus_power)(sc->sc_cc,
    706 	    CARDBUS_VCC_0V | CARDBUS_VPP_0V);
    707 }
    708 
    709 void
    710 cardbus_childdetached(struct device *self, struct device *child)
    711 {
    712 	struct cardbus_softc *sc = device_private(self);
    713 	struct cardbus_devfunc *ct;
    714 
    715 	ct = sc->sc_funcs[device_locator(child, CARDBUSCF_FUNCTION)];
    716 	KASSERT(ct->ct_device == child);
    717 
    718 	sc->sc_poweron_func &= ~(1 << ct->ct_func);
    719 	sc->sc_funcs[ct->ct_func] = NULL;
    720 	free(ct, M_DEVBUF);
    721 }
    722 
    723 /*
    724  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    725  *   Interrupt handler of pccard.
    726  *  args:
    727  *   cardbus_chipset_tag_t *cc
    728  *   int irq:
    729  */
    730 void *
    731 cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    732     cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg)
    733 {
    734 
    735 	DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    736 	return ((*cf->cardbus_intr_establish)(cc, irq, level, func, arg));
    737 }
    738 
    739 /*
    740  * void cardbus_intr_disestablish(cc, cf, handler)
    741  *   Interrupt handler of pccard.
    742  *  args:
    743  *   cardbus_chipset_tag_t *cc
    744  */
    745 void
    746 cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    747     void *handler)
    748 {
    749 
    750 	DPRINTF(("- pccard_intr_disestablish\n"));
    751 	(*cf->cardbus_intr_disestablish)(cc, handler);
    752 }
    753 
    754 /*
    755  * XXX this should be merged with cardbus_function_{enable,disable},
    756  * but we don't have a ct when these functions are called.
    757  */
    758 static void
    759 enable_function(struct cardbus_softc *sc, int cdstatus, int function)
    760 {
    761 
    762 	if (sc->sc_poweron_func == 0) {
    763 		/* switch to 3V and/or wait for power to stabilize */
    764 		if (cdstatus & CARDBUS_3V_CARD) {
    765 			/*
    766 			 * sc_poweron_func must be substituted before
    767 			 * entering sleep, in order to avoid turn on
    768 			 * power twice.
    769 			 */
    770 			sc->sc_poweron_func |= (1 << function);
    771 			(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_3V);
    772 		} else {
    773 			/* No cards other than 3.3V cards. */
    774 			return;
    775 		}
    776 		(*sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
    777 	}
    778 	sc->sc_poweron_func |= (1 << function);
    779 }
    780 
    781 static void
    782 disable_function(struct cardbus_softc *sc, int function)
    783 {
    784 
    785 	sc->sc_poweron_func &= ~(1 << function);
    786 	if (sc->sc_poweron_func == 0) {
    787 		/* power-off because no functions are enabled */
    788 		(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_0V);
    789 	}
    790 }
    791 
    792 /*
    793  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
    794  *
    795  *   This function enables a function on a card.  When no power is
    796  *  applied on the card, power will be applied on it.
    797  */
    798 int
    799 cardbus_function_enable(struct cardbus_softc *sc, int func)
    800 {
    801 	cardbus_chipset_tag_t cc = sc->sc_cc;
    802 	cardbus_function_tag_t cf = sc->sc_cf;
    803 	cardbus_devfunc_t ct;
    804 	cardbusreg_t command;
    805 	cardbustag_t tag;
    806 
    807 	DPRINTF(("entering cardbus_function_enable...  "));
    808 
    809 	/* entering critical area */
    810 
    811 	/* XXX: sc_vold should be used */
    812 	enable_function(sc, CARDBUS_3V_CARD, func);
    813 
    814 	/* exiting critical area */
    815 
    816 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
    817 
    818 	command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    819 	command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE |
    820 	    CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    821 
    822 	cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    823 
    824 	if ((ct = sc->sc_funcs[func]) != NULL)
    825 		Cardbus_conf_write(ct, tag, CARDBUS_BHLC_REG, ct->ct_bhlc);
    826 
    827 	cardbus_free_tag(cc, cf, tag);
    828 
    829 	DPRINTF(("%x\n", sc->sc_poweron_func));
    830 
    831 	return (0);
    832 }
    833 
    834 /*
    835  * int cardbus_function_disable(struct cardbus_softc *, int func)
    836  *
    837  *   This function disable a function on a card.  When no functions are
    838  *  enabled, it turns off the power.
    839  */
    840 int
    841 cardbus_function_disable(struct cardbus_softc *sc, int func)
    842 {
    843 
    844 	DPRINTF(("entering cardbus_function_disable...  "));
    845 
    846 	disable_function(sc, func);
    847 
    848 	return (0);
    849 }
    850 
    851 /*
    852  * int cardbus_get_capability(cardbus_chipset_tag_t cc,
    853  *	cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
    854  *	cardbusreg_t *value)
    855  *
    856  *	Find the specified PCI capability.
    857  */
    858 int
    859 cardbus_get_capability(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    860     cardbustag_t tag, int capid, int *offset, cardbusreg_t *value)
    861 {
    862 	cardbusreg_t reg;
    863 	unsigned int ofs;
    864 
    865 	reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
    866 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
    867 		return (0);
    868 
    869 	ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
    870 	    PCI_CAPLISTPTR_REG));
    871 	while (ofs != 0) {
    872 #ifdef DIAGNOSTIC
    873 		if ((ofs & 3) || (ofs < 0x40))
    874 			panic("cardbus_get_capability");
    875 #endif
    876 		reg = cardbus_conf_read(cc, cf, tag, ofs);
    877 		if (PCI_CAPLIST_CAP(reg) == capid) {
    878 			if (offset)
    879 				*offset = ofs;
    880 			if (value)
    881 				*value = reg;
    882 			return (1);
    883 		}
    884 		ofs = PCI_CAPLIST_NEXT(reg);
    885 	}
    886 
    887 	return (0);
    888 }
    889 
    890 /*
    891  * below this line, there are some functions for decoding tuples.
    892  * They should go out from this file.
    893  */
    894 
    895 static u_int8_t *
    896 decode_tuple(u_int8_t *, u_int8_t *, tuple_decode_func, void *);
    897 
    898 static int
    899 decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
    900 {
    901 	u_int8_t *tp = tuple;
    902 
    903 	if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
    904 		DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    905 		return (0);
    906 	}
    907 
    908 	while ((tp = decode_tuple(tp, tuple + buflen, func, data)) != NULL)
    909 		;
    910 
    911 	return (1);
    912 }
    913 
    914 static u_int8_t *
    915 decode_tuple(u_int8_t *tuple, u_int8_t *end,
    916     tuple_decode_func func, void *data)
    917 {
    918 	u_int8_t type;
    919 	u_int8_t len;
    920 
    921 	type = tuple[0];
    922 	switch (type) {
    923 	case PCMCIA_CISTPL_NULL:
    924 	case PCMCIA_CISTPL_END:
    925 		len = 1;
    926 		break;
    927 	default:
    928 		if (tuple + 2 > end)
    929 			return (NULL);
    930 		len = tuple[1] + 2;
    931 		break;
    932 	}
    933 
    934 	if (tuple + len > end)
    935 		return (NULL);
    936 
    937 	(*func)(tuple, len, data);
    938 
    939 	if (type == PCMCIA_CISTPL_END || tuple + len == end)
    940 		return (NULL);
    941 
    942 	return (tuple + len);
    943 }
    944 
    945 /*
    946  * XXX: this is another reason why this code should be shared with PCI.
    947  */
    948 static int
    949 cardbus_get_powerstate_int(cardbus_devfunc_t ct, cardbustag_t tag,
    950     cardbusreg_t *state, int offset)
    951 {
    952 	cardbusreg_t value, now;
    953 	cardbus_chipset_tag_t cc = ct->ct_cc;
    954 	cardbus_function_tag_t cf = ct->ct_cf;
    955 
    956 	value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR);
    957 	now = value & PCI_PMCSR_STATE_MASK;
    958 	switch (now) {
    959 	case PCI_PMCSR_STATE_D0:
    960 	case PCI_PMCSR_STATE_D1:
    961 	case PCI_PMCSR_STATE_D2:
    962 	case PCI_PMCSR_STATE_D3:
    963 		*state = now;
    964 		return 0;
    965 	default:
    966 		return EINVAL;
    967 	}
    968 }
    969 
    970 int
    971 cardbus_get_powerstate(cardbus_devfunc_t ct, cardbustag_t tag,
    972     cardbusreg_t *state)
    973 {
    974 	cardbus_chipset_tag_t cc = ct->ct_cc;
    975 	cardbus_function_tag_t cf = ct->ct_cf;
    976 	int offset;
    977 	cardbusreg_t value;
    978 
    979 	if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset, &value))
    980 		return EOPNOTSUPP;
    981 
    982 	return cardbus_get_powerstate_int(ct, tag, state, offset);
    983 }
    984 
    985 static int
    986 cardbus_set_powerstate_int(cardbus_devfunc_t ct, cardbustag_t tag,
    987     cardbusreg_t state, int offset, cardbusreg_t cap_reg)
    988 {
    989 	cardbus_chipset_tag_t cc = ct->ct_cc;
    990 	cardbus_function_tag_t cf = ct->ct_cf;
    991 
    992 	cardbusreg_t value, cap, now;
    993 
    994 	cap = cap_reg >> PCI_PMCR_SHIFT;
    995 	value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR);
    996 	now = value & PCI_PMCSR_STATE_MASK;
    997 	value &= ~PCI_PMCSR_STATE_MASK;
    998 
    999 	if (now == state)
   1000 		return 0;
   1001 	switch (state) {
   1002 	case PCI_PMCSR_STATE_D0:
   1003 		value |= PCI_PMCSR_STATE_D0;
   1004 		break;
   1005 	case PCI_PMCSR_STATE_D1:
   1006 		if (now == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D3) {
   1007 			printf("invalid transition from %d to D1\n", (int)now);
   1008 			return EINVAL;
   1009 		}
   1010 		if (!(cap & PCI_PMCR_D1SUPP)) {
   1011 			printf("D1 not supported\n");
   1012 			return EOPNOTSUPP;
   1013 		}
   1014 		value |= PCI_PMCSR_STATE_D1;
   1015 		break;
   1016 	case PCI_PMCSR_STATE_D2:
   1017 		if (now == PCI_PMCSR_STATE_D3) {
   1018 			printf("invalid transition from %d to D2\n", (int)now);
   1019 			return EINVAL;
   1020 		}
   1021 		if (!(cap & PCI_PMCR_D2SUPP)) {
   1022 			printf("D2 not supported\n");
   1023 			return EOPNOTSUPP;
   1024 		}
   1025 		value |= PCI_PMCSR_STATE_D2;
   1026 		break;
   1027 	case PCI_PMCSR_STATE_D3:
   1028 		value |= PCI_PMCSR_STATE_D3;
   1029 		break;
   1030 	default:
   1031 		return EINVAL;
   1032 	}
   1033 
   1034 	cardbus_conf_write(cc, cf, tag, offset + PCI_PMCSR, value);
   1035 	DELAY(1000);
   1036 
   1037 	return 0;
   1038 }
   1039 
   1040 int
   1041 cardbus_set_powerstate(cardbus_devfunc_t ct, cardbustag_t tag, cardbusreg_t state)
   1042 {
   1043 	cardbus_chipset_tag_t cc = ct->ct_cc;
   1044 	cardbus_function_tag_t cf = ct->ct_cf;
   1045 	int offset;
   1046 	cardbusreg_t value;
   1047 
   1048 	if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset,
   1049 	    &value))
   1050 		return EOPNOTSUPP;
   1051 
   1052 	return cardbus_set_powerstate_int(ct, tag, state, offset, value);
   1053 }
   1054 
   1055 #ifdef CARDBUS_DEBUG
   1056 static const char *tuple_name(int);
   1057 static const char *tuple_names[] = {
   1058 	"TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
   1059 	"CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
   1060 	"Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
   1061 	"Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
   1062 	"CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
   1063 	"NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
   1064 	"JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
   1065 	"DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
   1066 	"MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
   1067 	"Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
   1068 	"Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
   1069 	"Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
   1070 	"Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
   1071 	"Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
   1072 	"Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
   1073 	"Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
   1074 	"VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
   1075 	"DATE", "BATTERY", "ORG"
   1076 };
   1077 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
   1078 
   1079 static const char *
   1080 tuple_name(int type)
   1081 {
   1082 
   1083 	if (0 <= type && type < NAME_LEN(tuple_names)) {
   1084 		return (tuple_names[type]);
   1085 	} else if (type == 0xff) {
   1086 		return ("END");
   1087 	} else {
   1088 		return ("Reserved");
   1089 	}
   1090 }
   1091 
   1092 static void
   1093 print_tuple(u_int8_t *tuple, int len, void *data)
   1094 {
   1095 	int i;
   1096 
   1097 	printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
   1098 
   1099 	for (i = 0; i < len; ++i) {
   1100 		if (i % 16 == 0) {
   1101 			printf("  0x%2x:", i);
   1102 		}
   1103 		printf(" %x", tuple[i]);
   1104 		if (i % 16 == 15) {
   1105 			printf("\n");
   1106 		}
   1107 	}
   1108 	if (i % 16 != 0) {
   1109 		printf("\n");
   1110 	}
   1111 }
   1112 #endif
   1113 
   1114 void
   1115 cardbus_conf_capture(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
   1116     cardbustag_t tag, struct cardbus_conf_state *pcs)
   1117 {
   1118 	int off;
   1119 
   1120 	for (off = 0; off < 16; off++)
   1121 		pcs->reg[off] = cardbus_conf_read(cc, cf, tag, (off * 4));
   1122 }
   1123 
   1124 void
   1125 cardbus_conf_restore(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
   1126     cardbustag_t tag, struct cardbus_conf_state *pcs)
   1127 {
   1128 	int off;
   1129 	cardbusreg_t val;
   1130 
   1131 	for (off = 15; off >= 0; off--) {
   1132 		val = cardbus_conf_read(cc, cf, tag, (off * 4));
   1133 		if (val != pcs->reg[off])
   1134 			cardbus_conf_write(cc, cf,tag, (off * 4), pcs->reg[off]);
   1135 	}
   1136 }
   1137 
   1138 void
   1139 cardbus_disable_retry(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
   1140     cardbustag_t tag)
   1141 {
   1142 	cardbusreg_t retry;
   1143 
   1144 	/*
   1145 	 * Disable retry timeout to keep PCI Tx retries from
   1146 	 * interfering with ACPI C3 CPU state.
   1147 	 */
   1148 	retry = cardbus_conf_read(cc, cf, tag, PCI_RETRY_TIMEOUT_REG);
   1149 	retry &= ~PCI_RETRY_TIMEOUT_REG_MASK;
   1150 	cardbus_conf_write(cc, cf, tag, PCI_RETRY_TIMEOUT_REG, retry);
   1151 }
   1152 
   1153 struct cardbus_child_power {
   1154 	struct cardbus_conf_state p_cardbusconf;
   1155 	cardbus_devfunc_t p_ct;
   1156 	cardbustag_t p_tag;
   1157 	cardbus_chipset_tag_t p_cc;
   1158 	cardbus_function_tag_t p_cf;
   1159 	cardbusreg_t p_pm_cap;
   1160 	bool p_has_pm;
   1161 	int p_pm_offset;
   1162 };
   1163 
   1164 static bool
   1165 cardbus_child_suspend(device_t dv)
   1166 {
   1167 	struct cardbus_child_power *priv = device_pmf_bus_private(dv);
   1168 
   1169 	cardbus_conf_capture(priv->p_cc, priv->p_cf, priv->p_tag,
   1170 	    &priv->p_cardbusconf);
   1171 
   1172 	if (priv->p_has_pm &&
   1173 	    cardbus_set_powerstate_int(priv->p_ct, priv->p_tag,
   1174 	    PCI_PMCSR_STATE_D3, priv->p_pm_offset, priv->p_pm_cap)) {
   1175 		aprint_error_dev(dv, "unsupported state, continuing.\n");
   1176 		return false;
   1177 	}
   1178 
   1179 	Cardbus_function_disable(priv->p_ct);
   1180 
   1181 	return true;
   1182 }
   1183 
   1184 static bool
   1185 cardbus_child_resume(device_t dv)
   1186 {
   1187 	struct cardbus_child_power *priv = device_pmf_bus_private(dv);
   1188 
   1189 	Cardbus_function_enable(priv->p_ct);
   1190 
   1191 	if (priv->p_has_pm &&
   1192 	    cardbus_set_powerstate_int(priv->p_ct, priv->p_tag,
   1193 	    PCI_PMCSR_STATE_D0, priv->p_pm_offset, priv->p_pm_cap)) {
   1194 		aprint_error_dev(dv, "unsupported state, continuing.\n");
   1195 		return false;
   1196 	}
   1197 
   1198 	cardbus_conf_restore(priv->p_cc, priv->p_cf, priv->p_tag,
   1199 	    &priv->p_cardbusconf);
   1200 
   1201 	return true;
   1202 }
   1203 
   1204 static void
   1205 cardbus_child_deregister(device_t dv)
   1206 {
   1207 	struct cardbus_child_power *priv = device_pmf_bus_private(dv);
   1208 
   1209 	free(priv, M_DEVBUF);
   1210 }
   1211 
   1212 static bool
   1213 cardbus_child_register(device_t child)
   1214 {
   1215 	device_t self = device_parent(child);
   1216 	struct cardbus_softc *sc = device_private(self);
   1217 	struct cardbus_devfunc *ct;
   1218 	struct cardbus_child_power *priv;
   1219 	int off;
   1220 	cardbusreg_t reg;
   1221 
   1222 	ct = sc->sc_funcs[device_locator(child, CARDBUSCF_FUNCTION)];
   1223 
   1224 	priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK);
   1225 
   1226 	priv->p_ct = ct;
   1227 	priv->p_cc = ct->ct_cf;
   1228 	priv->p_cf = ct->ct_cf;
   1229 	priv->p_tag = cardbus_make_tag(priv->p_cc, priv->p_cf, ct->ct_bus,
   1230 	    ct->ct_func);
   1231 
   1232 	if (cardbus_get_capability(priv->p_cc, priv->p_cf, priv->p_tag,
   1233 				   PCI_CAP_PWRMGMT, &off, &reg)) {
   1234 		priv->p_has_pm = true;
   1235 		priv->p_pm_offset = off;
   1236 		priv->p_pm_cap = reg;
   1237 	} else {
   1238 		priv->p_has_pm = false;
   1239 		priv->p_pm_offset = -1;
   1240 	}
   1241 
   1242 	device_pmf_bus_register(child, priv, cardbus_child_suspend,
   1243 	    cardbus_child_resume, cardbus_child_deregister);
   1244 
   1245 	return true;
   1246 }
   1247