Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.37
      1 /*	$NetBSD: cardbus.c,v 1.37 2001/11/13 12:51:12 lukem 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.37 2001/11/13 12:51:12 lukem Exp $");
     37 
     38 #include "opt_cardbus.h"
     39 
     40 #include <sys/types.h>
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/device.h>
     44 #include <sys/malloc.h>
     45 #include <sys/kernel.h>
     46 #include <sys/syslog.h>
     47 #include <sys/proc.h>
     48 #include <sys/reboot.h>		/* for AB_* needed by bootverbose */
     49 
     50 #include <machine/bus.h>
     51 
     52 #include <dev/cardbus/cardbusvar.h>
     53 #include <dev/cardbus/cardbusdevs.h>
     54 
     55 #include <dev/cardbus/cardbus_exrom.h>
     56 
     57 #include <dev/pci/pcivar.h>	/* XXX */
     58 #include <dev/pci/pcireg.h>	/* XXX */
     59 
     60 #include <dev/pcmcia/pcmciareg.h>
     61 
     62 #if defined CARDBUS_DEBUG
     63 #define STATIC
     64 #define DPRINTF(a) printf a
     65 #else
     66 #define STATIC static
     67 #define DPRINTF(a)
     68 #endif
     69 
     70 
     71 STATIC void cardbusattach(struct device *, struct device *, void *);
     72 int cardbus_attach_card(struct cardbus_softc *);
     73 
     74 STATIC int cardbusmatch(struct device *, struct cfdata *, void *);
     75 static int cardbussubmatch(struct device *, struct cfdata *, 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 struct cfattach cardbus_ca = {
     92 	sizeof(struct cardbus_softc), cardbusmatch, cardbusattach
     93 };
     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_driver->cd_name)) {
    108 		DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
    109 		    cba->cba_busname, cf->cf_driver->cd_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 	sc->sc_funcs = NULL;
    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 			for (p = SIMPLEQ_FIRST(&rom_image); p != NULL;
    240 			    p = SIMPLEQ_NEXT(p, next)) {
    241 				if (p->rom_image ==
    242 				    CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
    243 					bus_space_read_region_1(p->romt,
    244 					    p->romh, CARDBUS_CIS_ADDR(cis_ptr),
    245 					    tuples, 256);
    246 					found++;
    247 				}
    248 				break;
    249 			}
    250 			while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
    251 				SIMPLEQ_REMOVE_HEAD(&rom_image, p, next);
    252 				free(p, M_DEVBUF);
    253 			}
    254 		out:
    255 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    256 			cardbus_conf_write(cc, cf, tag, reg, exrom & ~1);
    257 			splx(save);
    258 		} else {
    259 			command = cardbus_conf_read(cc, cf, tag,
    260 			    CARDBUS_COMMAND_STATUS_REG);
    261 			cardbus_conf_write(cc, cf, tag,
    262 			    CARDBUS_COMMAND_STATUS_REG,
    263 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    264 			/* XXX byte order? */
    265 			bus_space_read_region_1(ca->ca_memt, bar_memh,
    266 			    cis_ptr, tuples, 256);
    267 			found++;
    268 		}
    269 		command = cardbus_conf_read(cc, cf, tag,
    270 		    CARDBUS_COMMAND_STATUS_REG);
    271 		cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
    272 		    command & ~CARDBUS_COMMAND_MEM_ENABLE);
    273 		cardbus_conf_write(cc, cf, tag, reg, 0);
    274 
    275 		Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh,
    276 		    bar_size);
    277 		break;
    278 
    279 #ifdef DIAGNOSTIC
    280 	default:
    281 		panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname,
    282 		    cardbus_space);
    283 #endif
    284 	}
    285 	return (!found);
    286 }
    287 
    288 static void
    289 parse_tuple(u_int8_t *tuple, int len, void *data)
    290 {
    291 #ifdef CARDBUS_DEBUG
    292 	static const char __func__[] = "parse_tuple";
    293 #endif
    294 	struct cardbus_cis_info *cis = data;
    295 	char *p;
    296 	int i, bar_index;
    297 
    298 	switch (tuple[0]) {
    299 	case PCMCIA_CISTPL_MANFID:
    300 		if (tuple[1] != 5) {
    301 			DPRINTF(("%s: wrong length manufacturer id (%d)\n",
    302 			    __func__, tuple[1]));
    303 			break;
    304 		}
    305 		cis->manufacturer = tuple[2] | (tuple[3] << 8);
    306 		cis->product = tuple[4] | (tuple[5] << 8);
    307 		break;
    308 
    309 	case PCMCIA_CISTPL_VERS_1:
    310 		memcpy(cis->cis1_info_buf, tuple + 2, tuple[1]);
    311 		i = 0;
    312 		p = cis->cis1_info_buf + 2;
    313 		while (i <
    314 		    sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
    315 			cis->cis1_info[i++] = p;
    316 			while (*p != '\0' && *p != '\xff')
    317 				p++;
    318 			if (*p == '\xff')
    319 				break;
    320 			p++;
    321 		}
    322 		break;
    323 
    324 	case PCMCIA_CISTPL_BAR:
    325 		if (tuple[1] != 6) {
    326 			DPRINTF(("%s: BAR with short length (%d)\n",
    327 			    __func__, tuple[1]));
    328 			break;
    329 		}
    330 		bar_index = tuple[2] & 7;
    331 		if (bar_index == 0) {
    332 			DPRINTF(("%s: invalid ASI in BAR tuple\n", __func__));
    333 			break;
    334 		}
    335 		bar_index--;
    336 		cis->bar[bar_index].flags = tuple[2];
    337 		cis->bar[bar_index].size =
    338 		    (tuple[4] << 0) |
    339 		    (tuple[5] << 8) |
    340 		    (tuple[6] << 16) |
    341 		    (tuple[7] << 24);
    342 		break;
    343 
    344 	case PCMCIA_CISTPL_FUNCID:
    345 		cis->funcid = tuple[2];
    346 		break;
    347 
    348 	case PCMCIA_CISTPL_FUNCE:
    349 		switch (cis->funcid) {
    350 		case PCMCIA_FUNCTION_SERIAL:
    351 			if (tuple[1] >= 2 &&
    352 			    /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */
    353 			    tuple[2] == 0) {
    354 				cis->funce.serial.uart_type = tuple[3] & 0x1f;
    355 				cis->funce.serial.uart_present = 1;
    356 			}
    357 			break;
    358 
    359 		case PCMCIA_FUNCTION_NETWORK:
    360 			if (tuple[1] >= 8 &&
    361 			    tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
    362 				if (tuple[3] >
    363 				    sizeof(cis->funce.network.netid)) {
    364 					DPRINTF(("%s: unknown network id type "
    365 					    "(len = %d)\n",
    366 					    __func__, tuple[3]));
    367 				} else {
    368 					cis->funce.network.netid_present = 1;
    369 					memcpy(cis->funce.network.netid,
    370 					    tuple + 4, tuple[3]);
    371 				}
    372 			}
    373 			break;
    374 		}
    375 		break;
    376 	}
    377 }
    378 
    379 /*
    380  * int cardbus_attach_card(struct cardbus_softc *sc)
    381  *
    382  *    This function attaches the card on the slot: turns on power,
    383  *    reads and analyses tuple, sets configuration index.
    384  *
    385  *    This function returns the number of recognised device functions.
    386  *    If no functions are recognised, return 0.
    387  */
    388 int
    389 cardbus_attach_card(struct cardbus_softc *sc)
    390 {
    391 	cardbus_chipset_tag_t cc;
    392 	cardbus_function_tag_t cf;
    393 	cardbustag_t tag;
    394 	cardbusreg_t id, class, cis_ptr;
    395 	cardbusreg_t bhlc;
    396 	u_int8_t tuple[2048];
    397 	int cdstatus;
    398 	int function, nfunction;
    399 	struct cardbus_devfunc **previous_next = &(sc->sc_funcs);
    400 	struct device *csc;
    401 	int no_work_funcs = 0;
    402 	cardbus_devfunc_t ct;
    403 
    404 	cc = sc->sc_cc;
    405 	cf = sc->sc_cf;
    406 
    407 	DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
    408 
    409 	/* inspect initial voltage */
    410 	if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
    411 		DPRINTF(("cardbusattach: no CardBus card on cb%d\n",
    412 		    sc->sc_dev.dv_unit));
    413 		return (0);
    414 	}
    415 
    416 	/*
    417 	 * XXX use fake function 8 to keep power on during whole
    418 	 * configuration.
    419 	 */
    420 	enable_function(sc, cdstatus, 8);
    421 	function = 0;
    422 
    423 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
    424 
    425 	/*
    426 	 * Wait until power comes up.  Maxmum 500 ms.
    427 	 */
    428 	{
    429 		int i;
    430 
    431 		for (i = 0; i < 5; ++i) {
    432 			id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    433 			if (id != 0xffffffff && id != 0) {
    434 				break;
    435 			}
    436 			if (cold) {	/* before kernel thread invoked */
    437 				delay(100 * 1000);
    438 			} else {	/* thread context */
    439 				if (tsleep((void *)sc, PCATCH, "cardbus",
    440 				    hz / 10) != EWOULDBLOCK) {
    441 					break;
    442 				}
    443 			}
    444 		}
    445 		if (i == 5) {
    446 			return (0);
    447 		}
    448 	}
    449 
    450 	bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    451 	DPRINTF(("%s bhlc 0x%08x -> ", sc->sc_dev.dv_xname, bhlc));
    452 	nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
    453 
    454 	for (function = 0; function < nfunction; function++) {
    455 		struct cardbus_attach_args ca;
    456 
    457 		tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device,
    458 		    function);
    459 
    460 		id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    461 		class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
    462 		cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
    463 
    464 		/* Invalid vendor ID value? */
    465 		if (CARDBUS_VENDOR(id) == CARDBUS_VENDOR_INVALID) {
    466 			continue;
    467 		}
    468 
    469 		DPRINTF(("cardbus_attach_card: "
    470 		    "Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
    471 		    CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
    472 
    473 		enable_function(sc, cdstatus, function);
    474 
    475 		/* clean up every BAR */
    476 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
    477 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
    478 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
    479 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
    480 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
    481 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
    482 		cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
    483 
    484 		/* set initial latency and cacheline size */
    485 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    486 		DPRINTF(("%s func%d bhlc 0x%08x -> ", sc->sc_dev.dv_xname,
    487 		    function, bhlc));
    488 		bhlc &= ~((CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT) |
    489 		    (CARDBUS_CACHELINE_MASK << CARDBUS_CACHELINE_SHIFT));
    490 		bhlc |= ((sc->sc_cacheline & CARDBUS_CACHELINE_MASK) << CARDBUS_CACHELINE_SHIFT);
    491 		bhlc |= ((sc->sc_lattimer & CARDBUS_LATTIMER_MASK) << CARDBUS_LATTIMER_SHIFT);
    492 
    493 		cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    494 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    495 		DPRINTF(("0x%08x\n", bhlc));
    496 
    497 		if (CARDBUS_LATTIMER(bhlc) < 0x10) {
    498 			bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    499 			bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
    500 			cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    501 		}
    502 
    503 		/*
    504 		 * We need to allocate the ct here, since we might
    505 		 * need it when reading the CIS
    506 		 */
    507 		if ((ct = malloc(sizeof(struct cardbus_devfunc),
    508 		    M_DEVBUF, M_NOWAIT)) == NULL) {
    509 			panic("no room for cardbus_tag");
    510 		}
    511 
    512 		ct->ct_cc = sc->sc_cc;
    513 		ct->ct_cf = sc->sc_cf;
    514 		ct->ct_bus = sc->sc_bus;
    515 		ct->ct_dev = sc->sc_device;
    516 		ct->ct_func = function;
    517 		ct->ct_sc = sc;
    518 		ct->ct_next = NULL;
    519 		*previous_next = ct;
    520 
    521 		memset(&ca, 0, sizeof(ca));
    522 
    523 		ca.ca_unit = sc->sc_dev.dv_unit;
    524 		ca.ca_ct = ct;
    525 
    526 		ca.ca_iot = sc->sc_iot;
    527 		ca.ca_memt = sc->sc_memt;
    528 		ca.ca_dmat = sc->sc_dmat;
    529 
    530 #if rbus
    531 		ca.ca_rbus_iot = sc->sc_rbus_iot;
    532 		ca.ca_rbus_memt= sc->sc_rbus_memt;
    533 #endif
    534 
    535 		ca.ca_tag = tag;
    536 		ca.ca_bus = sc->sc_bus;
    537 		ca.ca_device = sc->sc_device;
    538 		ca.ca_function = function;
    539 		ca.ca_id = id;
    540 		ca.ca_class = class;
    541 
    542 		ca.ca_intrline = sc->sc_intrline;
    543 
    544 		if (cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
    545 			printf("cardbus_attach_card: failed to read CIS\n");
    546 		} else {
    547 #ifdef CARDBUS_DEBUG
    548 			decode_tuples(tuple, 2048, print_tuple, NULL);
    549 #endif
    550 			decode_tuples(tuple, 2048, parse_tuple, &ca.ca_cis);
    551 		}
    552 
    553 		if ((csc = config_found_sm((void *)sc, &ca, cardbusprint,
    554 		    cardbussubmatch)) == NULL) {
    555 			/* do not match */
    556 			disable_function(sc, function);
    557 			free(ct, M_DEVBUF);
    558 			*previous_next = NULL;
    559 		} else {
    560 			/* found */
    561 			previous_next = &(ct->ct_next);
    562 			ct->ct_device = csc;
    563 			++no_work_funcs;
    564 		}
    565 	}
    566 	/*
    567 	 * XXX power down pseudo function 8 (this will power down the card
    568 	 * if no functions were attached).
    569 	 */
    570 	disable_function(sc, 8);
    571 
    572 	return (no_work_funcs);
    573 }
    574 
    575 static int
    576 cardbussubmatch(struct device *parent, struct cfdata *cf, void *aux)
    577 {
    578 	struct cardbus_attach_args *ca = aux;
    579 
    580 	if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
    581 	    cf->cardbuscf_dev != ca->ca_unit) {
    582 		return (0);
    583 	}
    584 	if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
    585 	    cf->cardbuscf_function != ca->ca_function) {
    586 		return (0);
    587 	}
    588 
    589 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    590 }
    591 
    592 static int
    593 cardbusprint(void *aux, const char *pnp)
    594 {
    595 	struct cardbus_attach_args *ca = aux;
    596 	char devinfo[256];
    597 	int i;
    598 
    599 	if (pnp) {
    600 		pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo);
    601 		for (i = 0; i < 4; i++) {
    602 			if (ca->ca_cis.cis1_info[i] == NULL)
    603 				break;
    604 			if (i)
    605 				printf(", ");
    606 			printf("%s", ca->ca_cis.cis1_info[i]);
    607 		}
    608 		if (bootverbose) {
    609 			if (i)
    610 				printf(" ");
    611 			printf("(manufacturer 0x%x, product 0x%x)",
    612 			       ca->ca_cis.manufacturer, ca->ca_cis.product);
    613 		}
    614 		printf(" %s at %s", devinfo, pnp);
    615 	}
    616 	printf(" dev %d function %d", ca->ca_device, ca->ca_function);
    617 
    618 	return (UNCONF);
    619 }
    620 
    621 /*
    622  * void cardbus_detach_card(struct cardbus_softc *sc)
    623  *
    624  *    This function detaches the card on the slot: detach device data
    625  *    structure and turns off the power.
    626  *
    627  *    This function must not be called under interrupt context.
    628  */
    629 void
    630 cardbus_detach_card(struct cardbus_softc *sc)
    631 {
    632 	struct cardbus_devfunc *ct, *ct_next, **prev_next;
    633 
    634 	prev_next = &(sc->sc_funcs->ct_next);
    635 
    636 	for (ct = sc->sc_funcs; ct != NULL; ct = ct_next) {
    637 		struct device *fndev = ct->ct_device;
    638 		ct_next = ct->ct_next;
    639 
    640 		DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname,
    641 		    fndev->dv_xname));
    642 		/* call device detach function */
    643 
    644 		if (0 != config_detach(fndev, 0)) {
    645 			printf("%s: cannot detach dev %s, function %d\n",
    646 			    sc->sc_dev.dv_xname, fndev->dv_xname, ct->ct_func);
    647 			prev_next = &(ct->ct_next);
    648 		} else {
    649 			sc->sc_poweron_func &= ~(1 << ct->ct_func);
    650 			*prev_next = ct->ct_next;
    651 			free(ct, M_DEVBUF);
    652 		}
    653 	}
    654 
    655 	sc->sc_poweron_func = 0;
    656 	(*sc->sc_cf->cardbus_power)(sc->sc_cc,
    657 	    CARDBUS_VCC_0V | CARDBUS_VPP_0V);
    658 }
    659 
    660 /*
    661  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    662  *   Interrupt handler of pccard.
    663  *  args:
    664  *   cardbus_chipset_tag_t *cc
    665  *   int irq:
    666  */
    667 void *
    668 cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    669     cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg)
    670 {
    671 
    672 	DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    673 	return ((*cf->cardbus_intr_establish)(cc, irq, level, func, arg));
    674 }
    675 
    676 /*
    677  * void cardbus_intr_disestablish(cc, cf, handler)
    678  *   Interrupt handler of pccard.
    679  *  args:
    680  *   cardbus_chipset_tag_t *cc
    681  */
    682 void
    683 cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    684     void *handler)
    685 {
    686 
    687 	DPRINTF(("- pccard_intr_disestablish\n"));
    688 	(*cf->cardbus_intr_disestablish)(cc, handler);
    689 }
    690 
    691 /*
    692  * XXX this should be merged with cardbus_function_{enable,disable},
    693  * but we don't have a ct when these functions are called.
    694  */
    695 static void
    696 enable_function(struct cardbus_softc *sc, int cdstatus, int function)
    697 {
    698 
    699 	if (sc->sc_poweron_func == 0) {
    700 		/* switch to 3V and/or wait for power to stabilize */
    701 		if (cdstatus & CARDBUS_3V_CARD) {
    702 			/*
    703 			 * sc_poweron_func must be substituted before
    704 			 * entering sleep, in order to avoid turn on
    705 			 * power twice.
    706 			 */
    707 			sc->sc_poweron_func |= (1 << function);
    708 			(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_3V);
    709 		} else {
    710 			/* No cards other than 3.3V cards. */
    711 			return;
    712 		}
    713 		(*sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
    714 	}
    715 	sc->sc_poweron_func |= (1 << function);
    716 }
    717 
    718 static void
    719 disable_function(struct cardbus_softc *sc, int function)
    720 {
    721 
    722 	sc->sc_poweron_func &= ~(1 << function);
    723 	if (sc->sc_poweron_func == 0) {
    724 		/* power-off because no functions are enabled */
    725 		(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_0V);
    726 	}
    727 }
    728 
    729 /*
    730  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
    731  *
    732  *   This function enables a function on a card.  When no power is
    733  *  applied on the card, power will be applied on it.
    734  */
    735 int
    736 cardbus_function_enable(struct cardbus_softc *sc, int func)
    737 {
    738 	cardbus_chipset_tag_t cc = sc->sc_cc;
    739 	cardbus_function_tag_t cf = sc->sc_cf;
    740 	cardbusreg_t command;
    741 	cardbustag_t tag;
    742 
    743 	DPRINTF(("entering cardbus_function_enable...  "));
    744 
    745 	/* entering critical area */
    746 
    747 	/* XXX: sc_vold should be used */
    748 	enable_function(sc, CARDBUS_3V_CARD, func);
    749 
    750 	/* exiting critical area */
    751 
    752 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
    753 
    754 	command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    755 	command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE |
    756 	    CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    757 
    758 	cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    759 
    760 	cardbus_free_tag(cc, cf, tag);
    761 
    762 	DPRINTF(("%x\n", sc->sc_poweron_func));
    763 
    764 	return (0);
    765 }
    766 
    767 /*
    768  * int cardbus_function_disable(struct cardbus_softc *, int func)
    769  *
    770  *   This function disable a function on a card.  When no functions are
    771  *  enabled, it turns off the power.
    772  */
    773 int
    774 cardbus_function_disable(struct cardbus_softc *sc, int func)
    775 {
    776 
    777 	DPRINTF(("entering cardbus_function_disable...  "));
    778 
    779 	disable_function(sc, func);
    780 
    781 	return (0);
    782 }
    783 
    784 /*
    785  * int cardbus_get_capability(cardbus_chipset_tag_t cc,
    786  *	cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
    787  *	cardbusreg_t *value)
    788  *
    789  *	Find the specified PCI capability.
    790  */
    791 int
    792 cardbus_get_capability(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    793     cardbustag_t tag, int capid, int *offset, cardbusreg_t *value)
    794 {
    795 	cardbusreg_t reg;
    796 	unsigned int ofs;
    797 
    798 	reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
    799 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
    800 		return (0);
    801 
    802 	ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
    803 	    PCI_CAPLISTPTR_REG));
    804 	while (ofs != 0) {
    805 #ifdef DIAGNOSTIC
    806 		if ((ofs & 3) || (ofs < 0x40))
    807 			panic("cardbus_get_capability");
    808 #endif
    809 		reg = cardbus_conf_read(cc, cf, tag, ofs);
    810 		if (PCI_CAPLIST_CAP(reg) == capid) {
    811 			if (offset)
    812 				*offset = ofs;
    813 			if (value)
    814 				*value = reg;
    815 			return (1);
    816 		}
    817 		ofs = PCI_CAPLIST_NEXT(reg);
    818 	}
    819 
    820 	return (0);
    821 }
    822 
    823 /*
    824  * below this line, there are some functions for decoding tuples.
    825  * They should go out from this file.
    826  */
    827 
    828 static u_int8_t *
    829 decode_tuple(u_int8_t *tuple, tuple_decode_func func, void *data);
    830 
    831 static int
    832 decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
    833 {
    834 	u_int8_t *tp = tuple;
    835 
    836 	if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
    837 		DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    838 		return (0);
    839 	}
    840 
    841 	while (NULL != (tp = decode_tuple(tp, func, data))) {
    842 		if (tuple + buflen < tp) {
    843 			break;
    844 		}
    845 	}
    846 
    847 	return (1);
    848 }
    849 
    850 static u_int8_t *
    851 decode_tuple(u_int8_t *tuple, tuple_decode_func func, void *data)
    852 {
    853 	u_int8_t type;
    854 	u_int8_t len;
    855 
    856 	type = tuple[0];
    857 	len = tuple[1] + 2;
    858 
    859 	(*func)(tuple, len, data);
    860 
    861 	if (type == PCMCIA_CISTPL_END) {
    862 		return (NULL);
    863 	}
    864 
    865 	return (tuple + len);
    866 }
    867 
    868 #ifdef CARDBUS_DEBUG
    869 static const char *tuple_name(int);
    870 static const char *tuple_names[] = {
    871 	"TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
    872 	"CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
    873 	"Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
    874 	"Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
    875 	"CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
    876 	"NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
    877 	"JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
    878 	"DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
    879 	"MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
    880 	"Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
    881 	"Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
    882 	"Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
    883 	"Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
    884 	"Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
    885 	"Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
    886 	"Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
    887 	"VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
    888 	"DATE", "BATTERY", "ORG"
    889 };
    890 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
    891 
    892 static const char *
    893 tuple_name(int type)
    894 {
    895 
    896 	if (0 <= type && type < NAME_LEN(tuple_names)) {
    897 		return (tuple_names[type]);
    898 	} else if (type == 0xff) {
    899 		return ("END");
    900 	} else {
    901 		return ("Reserved");
    902 	}
    903 }
    904 
    905 static void
    906 print_tuple(u_int8_t *tuple, int len, void *data)
    907 {
    908 	int i;
    909 
    910 	printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
    911 
    912 	for (i = 0; i < len; ++i) {
    913 		if (i % 16 == 0) {
    914 			printf("  0x%2x:", i);
    915 		}
    916 		printf(" %x", tuple[i]);
    917 		if (i % 16 == 15) {
    918 			printf("\n");
    919 		}
    920 	}
    921 	if (i % 16 != 0) {
    922 		printf("\n");
    923 	}
    924 }
    925 #endif
    926