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