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