Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.54
      1 /*	$NetBSD: cardbus.c,v 1.54 2004/08/23 17:52:45 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.54 2004/08/23 17:52:45 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 #if defined CARDBUS_DEBUG
     62 #define STATIC
     63 #define DPRINTF(a) printf a
     64 #else
     65 #define STATIC static
     66 #define DPRINTF(a)
     67 #endif
     68 
     69 
     70 STATIC void cardbusattach(struct device *, struct device *, void *);
     71 STATIC int cardbusmatch(struct device *, struct cfdata *, void *);
     72 int cardbus_rescan(struct device *, const char *, const int *);
     73 void cardbus_childdetached(struct device *, struct device *);
     74 static int cardbussubmatch(struct device *, struct cfdata *,
     75 			   const locdesc_t *, void *);
     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 = (void *)self;
    120 	struct cbslot_attach_args *cba = aux;
    121 
    122 	sc->sc_bus = cba->cba_bus;
    123 	sc->sc_device = 0;
    124 	sc->sc_intrline = cba->cba_intrline;
    125 	sc->sc_cacheline = cba->cba_cacheline;
    126 	sc->sc_lattimer = cba->cba_lattimer;
    127 
    128 	printf(": bus %d device %d", sc->sc_bus, sc->sc_device);
    129 	if (bootverbose)
    130 		printf(" cacheline 0x%x, lattimer 0x%x", sc->sc_cacheline,
    131 		       sc->sc_lattimer);
    132 	printf("\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_BASE0_REG + (cardbus_space - 1) * 4;
    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, 256);
    243 					found++;
    244 				}
    245 				break;
    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, 256);
    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] != 5) {
    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 			cis->cis1_info[i++] = p;
    310 			while (*p != '\0' && *p != '\xff')
    311 				p++;
    312 			if (*p == '\xff')
    313 				break;
    314 			p++;
    315 		}
    316 		break;
    317 
    318 	case PCMCIA_CISTPL_BAR:
    319 		if (tuple[1] != 6) {
    320 			DPRINTF(("%s: BAR with short length (%d)\n",
    321 			    __func__, tuple[1]));
    322 			break;
    323 		}
    324 		bar_index = tuple[2] & 7;
    325 		if (bar_index == 0) {
    326 			DPRINTF(("%s: invalid ASI in BAR tuple\n", __func__));
    327 			break;
    328 		}
    329 		bar_index--;
    330 		cis->bar[bar_index].flags = tuple[2];
    331 		cis->bar[bar_index].size =
    332 		    (tuple[4] << 0) |
    333 		    (tuple[5] << 8) |
    334 		    (tuple[6] << 16) |
    335 		    (tuple[7] << 24);
    336 		break;
    337 
    338 	case PCMCIA_CISTPL_FUNCID:
    339 		cis->funcid = tuple[2];
    340 		break;
    341 
    342 	case PCMCIA_CISTPL_FUNCE:
    343 		switch (cis->funcid) {
    344 		case PCMCIA_FUNCTION_SERIAL:
    345 			if (tuple[1] >= 2 &&
    346 			    /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */
    347 			    tuple[2] == 0) {
    348 				cis->funce.serial.uart_type = tuple[3] & 0x1f;
    349 				cis->funce.serial.uart_present = 1;
    350 			}
    351 			break;
    352 
    353 		case PCMCIA_FUNCTION_NETWORK:
    354 			if (tuple[1] >= 8 &&
    355 			    tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
    356 				if (tuple[3] >
    357 				    sizeof(cis->funce.network.netid)) {
    358 					DPRINTF(("%s: unknown network id type "
    359 					    "(len = %d)\n",
    360 					    __func__, tuple[3]));
    361 				} else {
    362 					cis->funce.network.netid_present = 1;
    363 					memcpy(cis->funce.network.netid,
    364 					    tuple + 4, tuple[3]);
    365 				}
    366 			}
    367 			break;
    368 		}
    369 		break;
    370 	}
    371 }
    372 
    373 /*
    374  * int cardbus_attach_card(struct cardbus_softc *sc)
    375  *
    376  *    This function attaches the card on the slot: turns on power,
    377  *    reads and analyses tuple, sets configuration index.
    378  *
    379  *    This function returns the number of recognised device functions.
    380  *    If no functions are recognised, return 0.
    381  */
    382 int
    383 cardbus_attach_card(struct cardbus_softc *sc)
    384 {
    385 	cardbus_chipset_tag_t cc;
    386 	cardbus_function_tag_t cf;
    387 	int cdstatus;
    388 	static int wildcard[] = {
    389 		CARDBUSCF_DEV_DEFAULT, CARDBUSCF_FUNCTION_DEFAULT
    390 	};
    391 
    392 	cc = sc->sc_cc;
    393 	cf = sc->sc_cf;
    394 
    395 	DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
    396 
    397 	/* inspect initial voltage */
    398 	if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
    399 		DPRINTF(("cardbusattach: no CardBus card on cb%d\n",
    400 		    sc->sc_dev.dv_unit));
    401 		return (0);
    402 	}
    403 
    404 	cardbus_rescan(&sc->sc_dev, "cardbus", wildcard);
    405 	return (1); /* XXX */
    406 }
    407 
    408 int
    409 cardbus_rescan(struct device *self, const char *ifattr, const int *locators)
    410 {
    411 	struct cardbus_softc *sc = (struct cardbus_softc *)self;
    412 	cardbus_chipset_tag_t cc;
    413 	cardbus_function_tag_t cf;
    414 	cardbustag_t tag;
    415 	cardbusreg_t id, class, cis_ptr;
    416 	cardbusreg_t bhlc;
    417 	u_int8_t tuple[2048];
    418 	int cdstatus;
    419 	int function, nfunction;
    420 	struct device *csc;
    421 	cardbus_devfunc_t ct;
    422 
    423 	cc = sc->sc_cc;
    424 	cf = sc->sc_cf;
    425 
    426 	/* XXX what a nonsense */
    427 	if ((locators[CARDBUSCF_DEV] != CARDBUSCF_DEV_DEFAULT) &&
    428 	    (locators[CARDBUSCF_DEV] != sc->sc_device))
    429 		return (0);
    430 
    431 	/* inspect initial voltage */
    432 	if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
    433 		DPRINTF(("cardbusattach: no CardBus card on cb%d\n",
    434 		    sc->sc_dev.dv_unit));
    435 		return (0);
    436 	}
    437 
    438 	/*
    439 	 * XXX use fake function 8 to keep power on during whole
    440 	 * configuration.
    441 	 */
    442 	enable_function(sc, cdstatus, 8);
    443 	function = 0;
    444 
    445 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
    446 
    447 	/*
    448 	 * Wait until power comes up.  Maxmum 500 ms.
    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 help[3];
    479 		locdesc_t *ldesc = (void *)&help; /* XXX */
    480 
    481 		if ((locators[CARDBUSCF_FUNCTION] != 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, sc->sc_device,
    489 		    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 		DPRINTF(("%s func%d bhlc 0x%08x -> ", sc->sc_dev.dv_xname,
    518 		    function, bhlc));
    519 		bhlc &= ~((CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT) |
    520 		    (CARDBUS_CACHELINE_MASK << CARDBUS_CACHELINE_SHIFT));
    521 		bhlc |= ((sc->sc_cacheline & CARDBUS_CACHELINE_MASK) << CARDBUS_CACHELINE_SHIFT);
    522 		bhlc |= ((sc->sc_lattimer & CARDBUS_LATTIMER_MASK) << CARDBUS_LATTIMER_SHIFT);
    523 
    524 		cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    525 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    526 		DPRINTF(("0x%08x\n", bhlc));
    527 
    528 		if (CARDBUS_LATTIMER(bhlc) < 0x10) {
    529 			bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    530 			bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
    531 			cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    532 		}
    533 
    534 		/*
    535 		 * We need to allocate the ct here, since we might
    536 		 * need it when reading the CIS
    537 		 */
    538 		if ((ct = malloc(sizeof(struct cardbus_devfunc),
    539 		    M_DEVBUF, M_NOWAIT)) == NULL) {
    540 			panic("no room for cardbus_tag");
    541 		}
    542 
    543 		ct->ct_cc = sc->sc_cc;
    544 		ct->ct_cf = sc->sc_cf;
    545 		ct->ct_bus = sc->sc_bus;
    546 		ct->ct_dev = sc->sc_device;
    547 		ct->ct_func = function;
    548 		ct->ct_sc = sc;
    549 		sc->sc_funcs[function] = ct;
    550 
    551 		memset(&ca, 0, sizeof(ca));
    552 
    553 		ca.ca_ct = ct;
    554 
    555 		ca.ca_iot = sc->sc_iot;
    556 		ca.ca_memt = sc->sc_memt;
    557 		ca.ca_dmat = sc->sc_dmat;
    558 
    559 #if rbus
    560 		ca.ca_rbus_iot = sc->sc_rbus_iot;
    561 		ca.ca_rbus_memt= sc->sc_rbus_memt;
    562 #endif
    563 
    564 		ca.ca_tag = tag;
    565 		ca.ca_bus = sc->sc_bus;
    566 		ca.ca_device = sc->sc_device; /* always 0 */
    567 		ca.ca_function = function;
    568 		ca.ca_id = id;
    569 		ca.ca_class = class;
    570 
    571 		ca.ca_intrline = sc->sc_intrline;
    572 
    573 		if (cis_ptr != 0) {
    574 			if (cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
    575 				printf("cardbus_attach_card: failed to read CIS\n");
    576 			} else {
    577 #ifdef CARDBUS_DEBUG
    578 				decode_tuples(tuple, 2048, print_tuple, NULL);
    579 #endif
    580 				decode_tuples(tuple, 2048, parse_tuple, &ca.ca_cis);
    581 			}
    582 		}
    583 
    584 		ldesc->len = 2;
    585 		ldesc->locs[CARDBUSCF_DEV] = sc->sc_device; /* always 0 */
    586 		ldesc->locs[CARDBUSCF_FUNCTION] = function;
    587 
    588 		if ((csc = config_found_sm_loc((void *)sc, "cardbus", ldesc,
    589 			&ca, cardbusprint, cardbussubmatch)) == NULL) {
    590 			/* do not match */
    591 			disable_function(sc, function);
    592 			sc->sc_funcs[function] = NULL;
    593 			free(ct, M_DEVBUF);
    594 		} else {
    595 			/* found */
    596 			ct->ct_device = csc;
    597 		}
    598 	}
    599 	/*
    600 	 * XXX power down pseudo function 8 (this will power down the card
    601 	 * if no functions were attached).
    602 	 */
    603 	disable_function(sc, 8);
    604 
    605 	return (0);
    606 }
    607 
    608 static int
    609 cardbussubmatch(struct device *parent, struct cfdata *cf,
    610 		const locdesc_t *ldesc, void *aux)
    611 {
    612 
    613 	/* ldesc->locs[CARDBUSCF_DEV] is always 0 */
    614 	if (cf->cf_loc[CARDBUSCF_DEV] != CARDBUSCF_DEV_DEFAULT &&
    615 	    cf->cf_loc[CARDBUSCF_DEV] != ldesc->locs[CARDBUSCF_DEV]) {
    616 		return (0);
    617 	}
    618 	if (cf->cf_loc[CARDBUSCF_FUNCTION] != CARDBUSCF_FUNCTION_DEFAULT &&
    619 	    cf->cf_loc[CARDBUSCF_FUNCTION] != ldesc->locs[CARDBUSCF_FUNCTION]) {
    620 		return (0);
    621 	}
    622 
    623 	return (config_match(parent, cf, aux));
    624 }
    625 
    626 static int
    627 cardbusprint(void *aux, const char *pnp)
    628 {
    629 	struct cardbus_attach_args *ca = aux;
    630 	char devinfo[256];
    631 	int i;
    632 
    633 	if (pnp) {
    634 		pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo,
    635 		    sizeof(devinfo));
    636 		for (i = 0; i < 4; i++) {
    637 			if (ca->ca_cis.cis1_info[i] == NULL)
    638 				break;
    639 			if (i)
    640 				aprint_normal(", ");
    641 			aprint_normal("%s", ca->ca_cis.cis1_info[i]);
    642 		}
    643 		aprint_verbose("%s(manufacturer 0x%x, product 0x%x)",
    644 		       i ? " " : "",
    645 		       ca->ca_cis.manufacturer, ca->ca_cis.product);
    646 		aprint_normal(" %s at %s", devinfo, pnp);
    647 	}
    648 	aprint_normal(" dev %d function %d", ca->ca_device, ca->ca_function);
    649 
    650 	return (UNCONF);
    651 }
    652 
    653 /*
    654  * void cardbus_detach_card(struct cardbus_softc *sc)
    655  *
    656  *    This function detaches the card on the slot: detach device data
    657  *    structure and turns off the power.
    658  *
    659  *    This function must not be called under interrupt context.
    660  */
    661 void
    662 cardbus_detach_card(struct cardbus_softc *sc)
    663 {
    664 	int f;
    665 	struct cardbus_devfunc *ct;
    666 
    667 	for (f = 0; f < 8; f++) {
    668 		ct = sc->sc_funcs[f];
    669 		if (!ct)
    670 			continue;
    671 
    672 		DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname,
    673 		    ct->ct_device->dv_xname));
    674 		/* call device detach function */
    675 
    676 		if (0 != config_detach(ct->ct_device, 0)) {
    677 			printf("%s: cannot detach dev %s, function %d\n",
    678 			    sc->sc_dev.dv_xname, ct->ct_device->dv_xname,
    679 			    ct->ct_func);
    680 		}
    681 	}
    682 
    683 	sc->sc_poweron_func = 0;
    684 	(*sc->sc_cf->cardbus_power)(sc->sc_cc,
    685 	    CARDBUS_VCC_0V | CARDBUS_VPP_0V);
    686 }
    687 
    688 void
    689 cardbus_childdetached(struct device *self, struct device *child)
    690 {
    691 	struct cardbus_softc *sc = (struct cardbus_softc *)self;
    692 	struct cardbus_devfunc *ct;
    693 
    694 	ct = sc->sc_funcs[child->dv_locators[CARDBUSCF_FUNCTION]];
    695 	KASSERT(ct->ct_device == child);
    696 
    697 	sc->sc_poweron_func &= ~(1 << ct->ct_func);
    698 	sc->sc_funcs[ct->ct_func] = NULL;
    699 	free(ct, M_DEVBUF);
    700 }
    701 
    702 /*
    703  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    704  *   Interrupt handler of pccard.
    705  *  args:
    706  *   cardbus_chipset_tag_t *cc
    707  *   int irq:
    708  */
    709 void *
    710 cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    711     cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg)
    712 {
    713 
    714 	DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    715 	return ((*cf->cardbus_intr_establish)(cc, irq, level, func, arg));
    716 }
    717 
    718 /*
    719  * void cardbus_intr_disestablish(cc, cf, handler)
    720  *   Interrupt handler of pccard.
    721  *  args:
    722  *   cardbus_chipset_tag_t *cc
    723  */
    724 void
    725 cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    726     void *handler)
    727 {
    728 
    729 	DPRINTF(("- pccard_intr_disestablish\n"));
    730 	(*cf->cardbus_intr_disestablish)(cc, handler);
    731 }
    732 
    733 /*
    734  * XXX this should be merged with cardbus_function_{enable,disable},
    735  * but we don't have a ct when these functions are called.
    736  */
    737 static void
    738 enable_function(struct cardbus_softc *sc, int cdstatus, int function)
    739 {
    740 
    741 	if (sc->sc_poweron_func == 0) {
    742 		/* switch to 3V and/or wait for power to stabilize */
    743 		if (cdstatus & CARDBUS_3V_CARD) {
    744 			/*
    745 			 * sc_poweron_func must be substituted before
    746 			 * entering sleep, in order to avoid turn on
    747 			 * power twice.
    748 			 */
    749 			sc->sc_poweron_func |= (1 << function);
    750 			(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_3V);
    751 		} else {
    752 			/* No cards other than 3.3V cards. */
    753 			return;
    754 		}
    755 		(*sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
    756 	}
    757 	sc->sc_poweron_func |= (1 << function);
    758 }
    759 
    760 static void
    761 disable_function(struct cardbus_softc *sc, int function)
    762 {
    763 
    764 	sc->sc_poweron_func &= ~(1 << function);
    765 	if (sc->sc_poweron_func == 0) {
    766 		/* power-off because no functions are enabled */
    767 		(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_0V);
    768 	}
    769 }
    770 
    771 /*
    772  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
    773  *
    774  *   This function enables a function on a card.  When no power is
    775  *  applied on the card, power will be applied on it.
    776  */
    777 int
    778 cardbus_function_enable(struct cardbus_softc *sc, int func)
    779 {
    780 	cardbus_chipset_tag_t cc = sc->sc_cc;
    781 	cardbus_function_tag_t cf = sc->sc_cf;
    782 	cardbusreg_t command;
    783 	cardbustag_t tag;
    784 
    785 	DPRINTF(("entering cardbus_function_enable...  "));
    786 
    787 	/* entering critical area */
    788 
    789 	/* XXX: sc_vold should be used */
    790 	enable_function(sc, CARDBUS_3V_CARD, func);
    791 
    792 	/* exiting critical area */
    793 
    794 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
    795 
    796 	command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    797 	command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE |
    798 	    CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    799 
    800 	cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    801 
    802 	cardbus_free_tag(cc, cf, tag);
    803 
    804 	DPRINTF(("%x\n", sc->sc_poweron_func));
    805 
    806 	return (0);
    807 }
    808 
    809 /*
    810  * int cardbus_function_disable(struct cardbus_softc *, int func)
    811  *
    812  *   This function disable a function on a card.  When no functions are
    813  *  enabled, it turns off the power.
    814  */
    815 int
    816 cardbus_function_disable(struct cardbus_softc *sc, int func)
    817 {
    818 
    819 	DPRINTF(("entering cardbus_function_disable...  "));
    820 
    821 	disable_function(sc, func);
    822 
    823 	return (0);
    824 }
    825 
    826 /*
    827  * int cardbus_get_capability(cardbus_chipset_tag_t cc,
    828  *	cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
    829  *	cardbusreg_t *value)
    830  *
    831  *	Find the specified PCI capability.
    832  */
    833 int
    834 cardbus_get_capability(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    835     cardbustag_t tag, int capid, int *offset, cardbusreg_t *value)
    836 {
    837 	cardbusreg_t reg;
    838 	unsigned int ofs;
    839 
    840 	reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
    841 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
    842 		return (0);
    843 
    844 	ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
    845 	    PCI_CAPLISTPTR_REG));
    846 	while (ofs != 0) {
    847 #ifdef DIAGNOSTIC
    848 		if ((ofs & 3) || (ofs < 0x40))
    849 			panic("cardbus_get_capability");
    850 #endif
    851 		reg = cardbus_conf_read(cc, cf, tag, ofs);
    852 		if (PCI_CAPLIST_CAP(reg) == capid) {
    853 			if (offset)
    854 				*offset = ofs;
    855 			if (value)
    856 				*value = reg;
    857 			return (1);
    858 		}
    859 		ofs = PCI_CAPLIST_NEXT(reg);
    860 	}
    861 
    862 	return (0);
    863 }
    864 
    865 /*
    866  * below this line, there are some functions for decoding tuples.
    867  * They should go out from this file.
    868  */
    869 
    870 static u_int8_t *
    871 decode_tuple(u_int8_t *tuple, tuple_decode_func func, void *data);
    872 
    873 static int
    874 decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
    875 {
    876 	u_int8_t *tp = tuple;
    877 
    878 	if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
    879 		DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    880 		return (0);
    881 	}
    882 
    883 	while (NULL != (tp = decode_tuple(tp, func, data))) {
    884 		if (tuple + buflen < tp) {
    885 			break;
    886 		}
    887 	}
    888 
    889 	return (1);
    890 }
    891 
    892 static u_int8_t *
    893 decode_tuple(u_int8_t *tuple, tuple_decode_func func, void *data)
    894 {
    895 	u_int8_t type;
    896 	u_int8_t len;
    897 
    898 	type = tuple[0];
    899 	len = tuple[1] + 2;
    900 
    901 	(*func)(tuple, len, data);
    902 
    903 	if (type == PCMCIA_CISTPL_END) {
    904 		return (NULL);
    905 	}
    906 
    907 	return (tuple + len);
    908 }
    909 
    910 /*
    911  * XXX: this is another reason why this code should be shared with PCI.
    912  */
    913 int
    914 cardbus_powerstate(cardbus_devfunc_t ct, pcitag_t tag, const int *newstate,
    915     int *oldstate)
    916 {
    917 	cardbus_chipset_tag_t cc = ct->ct_cc;
    918 	cardbus_function_tag_t cf = ct->ct_cf;
    919 
    920 	int offset;
    921 	pcireg_t value, cap, now;
    922 
    923 	if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset,
    924 	    &value))
    925 		return EOPNOTSUPP;
    926 
    927 	cap = value >> 16;
    928 	value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR);
    929 	now = value & PCI_PMCSR_STATE_MASK;
    930 	value &= ~PCI_PMCSR_STATE_MASK;
    931 	if (oldstate) {
    932 		switch (now) {
    933 		case PCI_PMCSR_STATE_D0:
    934 			*oldstate = PCI_PWR_D0;
    935 			break;
    936 		case PCI_PMCSR_STATE_D1:
    937 			*oldstate = PCI_PWR_D1;
    938 			break;
    939 		case PCI_PMCSR_STATE_D2:
    940 			*oldstate = PCI_PWR_D2;
    941 			break;
    942 		case PCI_PMCSR_STATE_D3:
    943 			*oldstate = PCI_PWR_D3;
    944 			break;
    945 		default:
    946 			return EINVAL;
    947 		}
    948 	}
    949 	if (newstate == NULL)
    950 		return 0;
    951 	switch (*newstate) {
    952 	case PCI_PWR_D0:
    953 		if (now == PCI_PMCSR_STATE_D0)
    954 			return 0;
    955 		value |= PCI_PMCSR_STATE_D0;
    956 		break;
    957 	case PCI_PWR_D1:
    958 		if (now == PCI_PMCSR_STATE_D1)
    959 			return 0;
    960 		if (now == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D3)
    961 			return EINVAL;
    962 		if (!(cap & PCI_PMCR_D1SUPP))
    963 			return EOPNOTSUPP;
    964 		value |= PCI_PMCSR_STATE_D1;
    965 		break;
    966 	case PCI_PWR_D2:
    967 		if (now == PCI_PMCSR_STATE_D2)
    968 			return 0;
    969 		if (now == PCI_PMCSR_STATE_D3)
    970 			return EINVAL;
    971 		if (!(cap & PCI_PMCR_D2SUPP))
    972 			return EOPNOTSUPP;
    973 		value |= PCI_PMCSR_STATE_D2;
    974 		break;
    975 	case PCI_PWR_D3:
    976 		if (now == PCI_PMCSR_STATE_D3)
    977 			return 0;
    978 		value |= PCI_PMCSR_STATE_D3;
    979 		break;
    980 	default:
    981 		return EINVAL;
    982 	}
    983 	cardbus_conf_write(cc, cf, tag, offset + PCI_PMCSR, value);
    984 	DELAY(1000);
    985 
    986 	return 0;
    987 }
    988 
    989 int
    990 cardbus_setpowerstate(const char *dvname, cardbus_devfunc_t ct, pcitag_t tag,
    991     int newpwr)
    992 {
    993 	int oldpwr, error;
    994 
    995 	if ((error = cardbus_powerstate(ct, tag, &newpwr, &oldpwr)) != 0)
    996 		return error;
    997 
    998 	if (oldpwr == newpwr)
    999 		return 0;
   1000 
   1001 	if (oldpwr > newpwr) {
   1002 		printf("%s: sleeping to power state D%d\n", dvname, oldpwr);
   1003 		return 0;
   1004 	}
   1005 
   1006 	/* oldpwr < newpwr */
   1007 	switch (oldpwr) {
   1008 	case PCI_PWR_D3:
   1009 		/*
   1010 		 * XXX: This is because none of the devices do
   1011 		 * the necessary song and dance for now to wakeup
   1012 		 * Once this
   1013 		 */
   1014 		printf("%s: cannot wake up from power state D%d\n",
   1015 		    dvname, oldpwr);
   1016 		return EINVAL;
   1017 	default:
   1018 		printf("%s: waking up from power state D%d\n",
   1019 		    dvname, oldpwr);
   1020 		return 0;
   1021 	}
   1022 }
   1023 
   1024 
   1025 #ifdef CARDBUS_DEBUG
   1026 static const char *tuple_name(int);
   1027 static const char *tuple_names[] = {
   1028 	"TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
   1029 	"CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
   1030 	"Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
   1031 	"Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
   1032 	"CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
   1033 	"NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
   1034 	"JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
   1035 	"DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
   1036 	"MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
   1037 	"Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
   1038 	"Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
   1039 	"Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
   1040 	"Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
   1041 	"Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
   1042 	"Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
   1043 	"Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
   1044 	"VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
   1045 	"DATE", "BATTERY", "ORG"
   1046 };
   1047 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
   1048 
   1049 static const char *
   1050 tuple_name(int type)
   1051 {
   1052 
   1053 	if (0 <= type && type < NAME_LEN(tuple_names)) {
   1054 		return (tuple_names[type]);
   1055 	} else if (type == 0xff) {
   1056 		return ("END");
   1057 	} else {
   1058 		return ("Reserved");
   1059 	}
   1060 }
   1061 
   1062 static void
   1063 print_tuple(u_int8_t *tuple, int len, void *data)
   1064 {
   1065 	int i;
   1066 
   1067 	printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
   1068 
   1069 	for (i = 0; i < len; ++i) {
   1070 		if (i % 16 == 0) {
   1071 			printf("  0x%2x:", i);
   1072 		}
   1073 		printf(" %x", tuple[i]);
   1074 		if (i % 16 == 15) {
   1075 			printf("\n");
   1076 		}
   1077 	}
   1078 	if (i % 16 != 0) {
   1079 		printf("\n");
   1080 	}
   1081 }
   1082 #endif
   1083