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