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