Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.86
      1  1.86    dyoung /*	$NetBSD: cardbus.c,v 1.86 2008/01/03 23:15:43 dyoung Exp $	*/
      2   1.1      haya 
      3   1.1      haya /*
      4  1.18      haya  * Copyright (c) 1997, 1998, 1999 and 2000
      5   1.4      haya  *     HAYAKAWA Koichi.  All rights reserved.
      6   1.1      haya  *
      7   1.1      haya  * Redistribution and use in source and binary forms, with or without
      8   1.1      haya  * modification, are permitted provided that the following conditions
      9   1.1      haya  * are met:
     10   1.1      haya  * 1. Redistributions of source code must retain the above copyright
     11   1.1      haya  *    notice, this list of conditions and the following disclaimer.
     12   1.1      haya  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      haya  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      haya  *    documentation and/or other materials provided with the distribution.
     15   1.1      haya  * 3. All advertising materials mentioning features or use of this software
     16   1.1      haya  *    must display the following acknowledgement:
     17   1.1      haya  *	This product includes software developed by HAYAKAWA Koichi.
     18   1.1      haya  * 4. The name of the author may not be used to endorse or promote products
     19   1.1      haya  *    derived from this software without specific prior written permission.
     20   1.1      haya  *
     21   1.1      haya  *
     22   1.1      haya  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1      haya  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24   1.1      haya  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25   1.1      haya  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     26   1.1      haya  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     27   1.1      haya  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28   1.1      haya  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1      haya  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     30   1.1      haya  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     31   1.1      haya  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32   1.1      haya  * POSSIBILITY OF SUCH DAMAGE.
     33   1.1      haya  */
     34  1.37     lukem 
     35  1.37     lukem #include <sys/cdefs.h>
     36  1.86    dyoung __KERNEL_RCSID(0, "$NetBSD: cardbus.c,v 1.86 2008/01/03 23:15:43 dyoung Exp $");
     37   1.1      haya 
     38   1.4      haya #include "opt_cardbus.h"
     39   1.1      haya 
     40   1.1      haya #include <sys/param.h>
     41   1.1      haya #include <sys/systm.h>
     42   1.1      haya #include <sys/device.h>
     43   1.1      haya #include <sys/malloc.h>
     44   1.1      haya #include <sys/kernel.h>
     45   1.1      haya #include <sys/syslog.h>
     46  1.19      haya #include <sys/proc.h>
     47  1.36  augustss #include <sys/reboot.h>		/* for AB_* needed by bootverbose */
     48   1.1      haya 
     49  1.76        ad #include <sys/bus.h>
     50   1.1      haya 
     51   1.1      haya #include <dev/cardbus/cardbusvar.h>
     52  1.51   mycroft #include <dev/pci/pcidevs.h>
     53   1.1      haya 
     54   1.7      joda #include <dev/cardbus/cardbus_exrom.h>
     55   1.7      joda 
     56   1.1      haya #include <dev/pci/pcivar.h>	/* XXX */
     57   1.1      haya #include <dev/pci/pcireg.h>	/* XXX */
     58   1.1      haya 
     59  1.20     soren #include <dev/pcmcia/pcmciareg.h>
     60  1.10      joda 
     61  1.55  drochner #include "locators.h"
     62  1.55  drochner 
     63   1.1      haya #if defined CARDBUS_DEBUG
     64   1.1      haya #define STATIC
     65   1.1      haya #define DPRINTF(a) printf a
     66   1.1      haya #else
     67   1.1      haya #define STATIC static
     68   1.1      haya #define DPRINTF(a)
     69   1.1      haya #endif
     70   1.1      haya 
     71   1.1      haya 
     72  1.29     enami STATIC void cardbusattach(struct device *, struct device *, void *);
     73  1.82    dyoung STATIC int cardbusdetach(device_t, int);
     74  1.29     enami STATIC int cardbusmatch(struct device *, struct cfdata *, void *);
     75  1.52  drochner int cardbus_rescan(struct device *, const char *, const int *);
     76  1.52  drochner void cardbus_childdetached(struct device *, struct device *);
     77  1.29     enami static int cardbusprint(void *, const char *);
     78   1.1      haya 
     79  1.10      joda typedef void (*tuple_decode_func)(u_int8_t*, int, void*);
     80  1.10      joda 
     81  1.29     enami static int decode_tuples(u_int8_t *, int, tuple_decode_func, void*);
     82  1.10      joda #ifdef CARDBUS_DEBUG
     83  1.29     enami static void print_tuple(u_int8_t*, int, void*);
     84  1.10      joda #endif
     85   1.1      haya 
     86  1.29     enami static int cardbus_read_tuples(struct cardbus_attach_args *,
     87  1.29     enami     cardbusreg_t, u_int8_t *, size_t);
     88   1.8      joda 
     89  1.29     enami static void enable_function(struct cardbus_softc *, int, int);
     90  1.29     enami static void disable_function(struct cardbus_softc *, int);
     91   1.1      haya 
     92  1.81  jmcneill static bool cardbus_child_register(device_t);
     93  1.81  jmcneill 
     94  1.52  drochner CFATTACH_DECL2(cardbus, sizeof(struct cardbus_softc),
     95  1.82    dyoung     cardbusmatch, cardbusattach, cardbusdetach, NULL,
     96  1.52  drochner     cardbus_rescan, cardbus_childdetached);
     97   1.1      haya 
     98   1.1      haya #ifndef __NetBSD_Version__
     99   1.1      haya struct cfdriver cardbus_cd = {
    100   1.1      haya 	NULL, "cardbus", DV_DULL
    101   1.1      haya };
    102   1.1      haya #endif
    103   1.1      haya 
    104   1.1      haya 
    105   1.1      haya STATIC int
    106  1.74  christos cardbusmatch(struct device *parent, struct cfdata *cf, void *aux)
    107  1.29     enami {
    108  1.29     enami 	struct cbslot_attach_args *cba = aux;
    109   1.1      haya 
    110  1.42   thorpej 	if (strcmp(cba->cba_busname, cf->cf_name)) {
    111  1.29     enami 		DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
    112  1.42   thorpej 		    cba->cba_busname, cf->cf_name));
    113  1.29     enami 		return (0);
    114  1.29     enami 	}
    115  1.29     enami 
    116  1.29     enami 	return (1);
    117   1.1      haya }
    118   1.1      haya 
    119  1.29     enami STATIC void
    120  1.74  christos cardbusattach(struct device *parent, struct device *self, void *aux)
    121  1.29     enami {
    122  1.71   thorpej 	struct cardbus_softc *sc = device_private(self);
    123  1.29     enami 	struct cbslot_attach_args *cba = aux;
    124   1.1      haya 
    125  1.29     enami 	sc->sc_bus = cba->cba_bus;
    126  1.29     enami 	sc->sc_intrline = cba->cba_intrline;
    127  1.29     enami 	sc->sc_cacheline = cba->cba_cacheline;
    128  1.77    dyoung 	sc->sc_max_lattimer = MIN(0xf8, cba->cba_max_lattimer);
    129  1.29     enami 
    130  1.80  jmcneill 	aprint_naive("\n");
    131  1.79  jmcneill 	aprint_normal(": bus %d", sc->sc_bus);
    132  1.36  augustss 	if (bootverbose)
    133  1.79  jmcneill 		aprint_normal(" cacheline 0x%x, lattimer 0x%x",
    134  1.79  jmcneill 		    sc->sc_cacheline, sc->sc_max_lattimer);
    135  1.79  jmcneill 	aprint_normal("\n");
    136  1.29     enami 
    137  1.29     enami 	sc->sc_iot = cba->cba_iot;	/* CardBus I/O space tag */
    138  1.29     enami 	sc->sc_memt = cba->cba_memt;	/* CardBus MEM space tag */
    139  1.29     enami 	sc->sc_dmat = cba->cba_dmat;	/* DMA tag */
    140  1.29     enami 	sc->sc_cc = cba->cba_cc;
    141  1.29     enami 	sc->sc_cf = cba->cba_cf;
    142   1.1      haya 
    143   1.1      haya #if rbus
    144  1.29     enami 	sc->sc_rbus_iot = cba->cba_rbus_iot;
    145  1.29     enami 	sc->sc_rbus_memt = cba->cba_rbus_memt;
    146   1.1      haya #endif
    147  1.81  jmcneill 
    148  1.81  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    149  1.81  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    150   1.1      haya }
    151   1.1      haya 
    152  1.82    dyoung STATIC int
    153  1.82    dyoung cardbusdetach(device_t self, int flags)
    154  1.82    dyoung {
    155  1.82    dyoung 	int rc;
    156  1.82    dyoung 
    157  1.82    dyoung 	if ((rc = config_detach_children(self, flags)) != 0)
    158  1.82    dyoung 		return rc;
    159  1.82    dyoung 
    160  1.82    dyoung 	pmf_device_deregister(self);
    161  1.82    dyoung 	return 0;
    162  1.82    dyoung }
    163  1.82    dyoung 
    164   1.7      joda static int
    165  1.29     enami cardbus_read_tuples(struct cardbus_attach_args *ca, cardbusreg_t cis_ptr,
    166  1.29     enami     u_int8_t *tuples, size_t len)
    167  1.29     enami {
    168  1.29     enami 	struct cardbus_softc *sc = ca->ca_ct->ct_sc;
    169  1.29     enami 	cardbus_chipset_tag_t cc = ca->ca_ct->ct_cc;
    170  1.29     enami 	cardbus_function_tag_t cf = ca->ca_ct->ct_cf;
    171  1.29     enami 	cardbustag_t tag = ca->ca_tag;
    172  1.29     enami 	cardbusreg_t command;
    173  1.30     enami 	bus_space_tag_t bar_tag;
    174  1.29     enami 	bus_space_handle_t bar_memh;
    175  1.29     enami 	bus_size_t bar_size;
    176  1.29     enami 	bus_addr_t bar_addr;
    177  1.29     enami 	cardbusreg_t reg;
    178  1.29     enami 	int found = 0;
    179  1.29     enami 	int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK;
    180  1.29     enami 	int i, j;
    181  1.29     enami 
    182  1.29     enami 	memset(tuples, 0, len);
    183  1.29     enami 
    184  1.29     enami 	cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK;
    185  1.29     enami 
    186  1.29     enami 	switch (cardbus_space) {
    187  1.29     enami 	case CARDBUS_CIS_ASI_TUPLE:
    188  1.29     enami 		DPRINTF(("%s: reading CIS data from configuration space\n",
    189  1.29     enami 		    sc->sc_dev.dv_xname));
    190  1.29     enami 		for (i = cis_ptr, j = 0; i < 0xff; i += 4) {
    191  1.29     enami 			u_int32_t e = (*cf->cardbus_conf_read)(cc, tag, i);
    192  1.29     enami 			tuples[j] = 0xff & e;
    193  1.29     enami 			e >>= 8;
    194  1.29     enami 			tuples[j + 1] = 0xff & e;
    195  1.29     enami 			e >>= 8;
    196  1.29     enami 			tuples[j + 2] = 0xff & e;
    197  1.29     enami 			e >>= 8;
    198  1.29     enami 			tuples[j + 3] = 0xff & e;
    199  1.29     enami 			j += 4;
    200  1.29     enami 		}
    201  1.29     enami 		found++;
    202  1.29     enami 		break;
    203  1.29     enami 
    204  1.29     enami 	case CARDBUS_CIS_ASI_BAR0:
    205  1.29     enami 	case CARDBUS_CIS_ASI_BAR1:
    206  1.29     enami 	case CARDBUS_CIS_ASI_BAR2:
    207  1.29     enami 	case CARDBUS_CIS_ASI_BAR3:
    208  1.29     enami 	case CARDBUS_CIS_ASI_BAR4:
    209  1.29     enami 	case CARDBUS_CIS_ASI_BAR5:
    210  1.29     enami 	case CARDBUS_CIS_ASI_ROM:
    211  1.29     enami 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
    212  1.29     enami 			reg = CARDBUS_ROM_REG;
    213  1.29     enami 			DPRINTF(("%s: reading CIS data from ROM\n",
    214  1.29     enami 			    sc->sc_dev.dv_xname));
    215  1.29     enami 		} else {
    216  1.77    dyoung 			reg = CARDBUS_CIS_ASI_BAR(cardbus_space);
    217  1.29     enami 			DPRINTF(("%s: reading CIS data from BAR%d\n",
    218  1.29     enami 			    sc->sc_dev.dv_xname, cardbus_space - 1));
    219  1.29     enami 		}
    220   1.7      joda 
    221  1.29     enami 		/*
    222  1.29     enami 		 * XXX zero register so mapreg_map doesn't get confused by old
    223  1.29     enami 		 * contents.
    224  1.29     enami 		 */
    225  1.29     enami 		cardbus_conf_write(cc, cf, tag, reg, 0);
    226  1.29     enami 		if (Cardbus_mapreg_map(ca->ca_ct, reg,
    227  1.29     enami 		    CARDBUS_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    228  1.30     enami 		    0, &bar_tag, &bar_memh, &bar_addr, &bar_size)) {
    229  1.29     enami 			printf("%s: failed to map memory\n",
    230  1.29     enami 			    sc->sc_dev.dv_xname);
    231  1.29     enami 			return (1);
    232  1.29     enami 		}
    233  1.83    dyoung 		aprint_debug_dev(&sc->sc_dev, "mapped %ju bytes at 0x%jx\n",
    234  1.83    dyoung 		    (uintmax_t)bar_size, (uintmax_t)bar_addr);
    235   1.7      joda 
    236  1.29     enami 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
    237  1.29     enami 			cardbusreg_t exrom;
    238  1.29     enami 			int save;
    239  1.29     enami 			struct cardbus_rom_image_head rom_image;
    240  1.29     enami 			struct cardbus_rom_image *p;
    241  1.29     enami 
    242  1.29     enami 			save = splhigh();
    243  1.29     enami 			/* enable rom address decoder */
    244  1.29     enami 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    245  1.29     enami 			cardbus_conf_write(cc, cf, tag, reg, exrom | 1);
    246  1.29     enami 
    247  1.29     enami 			command = cardbus_conf_read(cc, cf, tag,
    248  1.29     enami 			    CARDBUS_COMMAND_STATUS_REG);
    249  1.29     enami 			cardbus_conf_write(cc, cf, tag,
    250  1.29     enami 			    CARDBUS_COMMAND_STATUS_REG,
    251  1.29     enami 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    252  1.29     enami 
    253  1.83    dyoung 			if (cardbus_read_exrom(bar_tag, bar_memh, &rom_image))
    254  1.29     enami 				goto out;
    255  1.29     enami 
    256  1.41     lukem 			SIMPLEQ_FOREACH(p, &rom_image, next) {
    257  1.29     enami 				if (p->rom_image ==
    258  1.29     enami 				    CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
    259  1.29     enami 					bus_space_read_region_1(p->romt,
    260  1.29     enami 					    p->romh, CARDBUS_CIS_ADDR(cis_ptr),
    261  1.60     enami 					    tuples, MIN(p->image_size, len));
    262  1.29     enami 					found++;
    263  1.59     enami 					break;
    264  1.29     enami 				}
    265  1.29     enami 			}
    266  1.29     enami 			while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
    267  1.41     lukem 				SIMPLEQ_REMOVE_HEAD(&rom_image, next);
    268  1.29     enami 				free(p, M_DEVBUF);
    269  1.29     enami 			}
    270  1.29     enami 		out:
    271  1.29     enami 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    272  1.29     enami 			cardbus_conf_write(cc, cf, tag, reg, exrom & ~1);
    273  1.29     enami 			splx(save);
    274  1.29     enami 		} else {
    275  1.29     enami 			command = cardbus_conf_read(cc, cf, tag,
    276  1.29     enami 			    CARDBUS_COMMAND_STATUS_REG);
    277  1.29     enami 			cardbus_conf_write(cc, cf, tag,
    278  1.29     enami 			    CARDBUS_COMMAND_STATUS_REG,
    279  1.29     enami 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    280  1.29     enami 			/* XXX byte order? */
    281  1.83    dyoung 			bus_space_read_region_1(bar_tag, bar_memh,
    282  1.84    dyoung 			    cis_ptr, tuples,
    283  1.84    dyoung 			    MIN(bar_size - MIN(bar_size, cis_ptr), len));
    284  1.29     enami 			found++;
    285   1.7      joda 		}
    286  1.29     enami 		command = cardbus_conf_read(cc, cf, tag,
    287  1.29     enami 		    CARDBUS_COMMAND_STATUS_REG);
    288  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
    289  1.29     enami 		    command & ~CARDBUS_COMMAND_MEM_ENABLE);
    290  1.29     enami 		cardbus_conf_write(cc, cf, tag, reg, 0);
    291  1.30     enami 
    292  1.30     enami 		Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh,
    293  1.30     enami 		    bar_size);
    294  1.29     enami 		break;
    295   1.1      haya 
    296   1.7      joda #ifdef DIAGNOSTIC
    297  1.29     enami 	default:
    298  1.29     enami 		panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname,
    299  1.29     enami 		    cardbus_space);
    300   1.7      joda #endif
    301  1.29     enami 	}
    302  1.29     enami 	return (!found);
    303   1.7      joda }
    304   1.1      haya 
    305  1.10      joda static void
    306  1.74  christos parse_tuple(u_int8_t *tuple, int len, void *data)
    307  1.10      joda {
    308  1.29     enami 	struct cardbus_cis_info *cis = data;
    309  1.29     enami 	char *p;
    310  1.29     enami 	int i, bar_index;
    311  1.29     enami 
    312  1.29     enami 	switch (tuple[0]) {
    313  1.29     enami 	case PCMCIA_CISTPL_MANFID:
    314  1.65  drochner 		if (tuple[1] != 4) {
    315  1.29     enami 			DPRINTF(("%s: wrong length manufacturer id (%d)\n",
    316  1.40     enami 			    __func__, tuple[1]));
    317  1.29     enami 			break;
    318  1.29     enami 		}
    319  1.29     enami 		cis->manufacturer = tuple[2] | (tuple[3] << 8);
    320  1.29     enami 		cis->product = tuple[4] | (tuple[5] << 8);
    321  1.29     enami 		break;
    322  1.29     enami 
    323  1.29     enami 	case PCMCIA_CISTPL_VERS_1:
    324  1.29     enami 		memcpy(cis->cis1_info_buf, tuple + 2, tuple[1]);
    325  1.29     enami 		i = 0;
    326  1.29     enami 		p = cis->cis1_info_buf + 2;
    327  1.29     enami 		while (i <
    328  1.29     enami 		    sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
    329  1.65  drochner 			if (p >= cis->cis1_info_buf + tuple[1] || *p == '\xff')
    330  1.65  drochner 				break;
    331  1.29     enami 			cis->cis1_info[i++] = p;
    332  1.29     enami 			while (*p != '\0' && *p != '\xff')
    333  1.29     enami 				p++;
    334  1.65  drochner 			if (*p == '\0')
    335  1.65  drochner 				p++;
    336  1.29     enami 		}
    337  1.10      joda 		break;
    338  1.29     enami 
    339  1.29     enami 	case PCMCIA_CISTPL_BAR:
    340  1.29     enami 		if (tuple[1] != 6) {
    341  1.29     enami 			DPRINTF(("%s: BAR with short length (%d)\n",
    342  1.40     enami 			    __func__, tuple[1]));
    343  1.29     enami 			break;
    344  1.29     enami 		}
    345  1.29     enami 		bar_index = tuple[2] & 7;
    346  1.29     enami 		if (bar_index == 0) {
    347  1.40     enami 			DPRINTF(("%s: invalid ASI in BAR tuple\n", __func__));
    348  1.29     enami 			break;
    349  1.29     enami 		}
    350  1.29     enami 		bar_index--;
    351  1.29     enami 		cis->bar[bar_index].flags = tuple[2];
    352  1.29     enami 		cis->bar[bar_index].size =
    353  1.29     enami 		    (tuple[4] << 0) |
    354  1.29     enami 		    (tuple[5] << 8) |
    355  1.29     enami 		    (tuple[6] << 16) |
    356  1.29     enami 		    (tuple[7] << 24);
    357  1.29     enami 		break;
    358  1.29     enami 
    359  1.29     enami 	case PCMCIA_CISTPL_FUNCID:
    360  1.29     enami 		cis->funcid = tuple[2];
    361  1.29     enami 		break;
    362  1.29     enami 
    363  1.29     enami 	case PCMCIA_CISTPL_FUNCE:
    364  1.29     enami 		switch (cis->funcid) {
    365  1.29     enami 		case PCMCIA_FUNCTION_SERIAL:
    366  1.29     enami 			if (tuple[1] >= 2 &&
    367  1.29     enami 			    /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */
    368  1.29     enami 			    tuple[2] == 0) {
    369  1.29     enami 				cis->funce.serial.uart_type = tuple[3] & 0x1f;
    370  1.29     enami 				cis->funce.serial.uart_present = 1;
    371  1.29     enami 			}
    372  1.29     enami 			break;
    373  1.29     enami 
    374  1.29     enami 		case PCMCIA_FUNCTION_NETWORK:
    375  1.29     enami 			if (tuple[1] >= 8 &&
    376  1.29     enami 			    tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
    377  1.29     enami 				if (tuple[3] >
    378  1.29     enami 				    sizeof(cis->funce.network.netid)) {
    379  1.29     enami 					DPRINTF(("%s: unknown network id type "
    380  1.29     enami 					    "(len = %d)\n",
    381  1.40     enami 					    __func__, tuple[3]));
    382  1.29     enami 				} else {
    383  1.29     enami 					cis->funce.network.netid_present = 1;
    384  1.29     enami 					memcpy(cis->funce.network.netid,
    385  1.29     enami 					    tuple + 4, tuple[3]);
    386  1.29     enami 				}
    387  1.29     enami 			}
    388  1.29     enami 			break;
    389  1.10      joda 		}
    390  1.29     enami 		break;
    391  1.10      joda 	}
    392  1.10      joda }
    393  1.10      joda 
    394   1.1      haya /*
    395   1.1      haya  * int cardbus_attach_card(struct cardbus_softc *sc)
    396   1.1      haya  *
    397   1.1      haya  *    This function attaches the card on the slot: turns on power,
    398  1.28       wiz  *    reads and analyses tuple, sets configuration index.
    399   1.1      haya  *
    400   1.1      haya  *    This function returns the number of recognised device functions.
    401   1.1      haya  *    If no functions are recognised, return 0.
    402   1.1      haya  */
    403   1.1      haya int
    404  1.29     enami cardbus_attach_card(struct cardbus_softc *sc)
    405   1.1      haya {
    406  1.29     enami 	cardbus_chipset_tag_t cc;
    407  1.29     enami 	cardbus_function_tag_t cf;
    408  1.52  drochner 	int cdstatus;
    409  1.64  drochner 	static int wildcard[CARDBUSCF_NLOCS] = {
    410  1.67  drochner 		CARDBUSCF_FUNCTION_DEFAULT
    411  1.52  drochner 	};
    412  1.52  drochner 
    413  1.52  drochner 	cc = sc->sc_cc;
    414  1.52  drochner 	cf = sc->sc_cf;
    415  1.52  drochner 
    416  1.69   thorpej 	DPRINTF(("cardbus_attach_card: cb%d start\n",
    417  1.69   thorpej 		 device_unit(&sc->sc_dev)));
    418  1.52  drochner 
    419  1.52  drochner 	/* inspect initial voltage */
    420  1.52  drochner 	if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
    421  1.75    dyoung 		DPRINTF(("%s: no CardBus card on cb%d\n", __func__,
    422  1.69   thorpej 		    device_unit(&sc->sc_dev)));
    423  1.52  drochner 		return (0);
    424  1.52  drochner 	}
    425  1.52  drochner 
    426  1.81  jmcneill 	device_pmf_driver_set_child_register(&sc->sc_dev, cardbus_child_register);
    427  1.52  drochner 	cardbus_rescan(&sc->sc_dev, "cardbus", wildcard);
    428  1.52  drochner 	return (1); /* XXX */
    429  1.52  drochner }
    430  1.52  drochner 
    431  1.52  drochner int
    432  1.74  christos cardbus_rescan(struct device *self, const char *ifattr,
    433  1.73  christos     const int *locators)
    434  1.52  drochner {
    435  1.71   thorpej 	struct cardbus_softc *sc = device_private(self);
    436  1.52  drochner 	cardbus_chipset_tag_t cc;
    437  1.52  drochner 	cardbus_function_tag_t cf;
    438  1.29     enami 	cardbustag_t tag;
    439  1.29     enami 	cardbusreg_t id, class, cis_ptr;
    440  1.77    dyoung 	cardbusreg_t bhlc, icr, lattimer;
    441  1.29     enami 	int cdstatus;
    442  1.29     enami 	int function, nfunction;
    443  1.29     enami 	struct device *csc;
    444  1.29     enami 	cardbus_devfunc_t ct;
    445  1.29     enami 
    446  1.29     enami 	cc = sc->sc_cc;
    447  1.29     enami 	cf = sc->sc_cf;
    448  1.29     enami 
    449  1.29     enami 	/* inspect initial voltage */
    450  1.29     enami 	if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
    451  1.75    dyoung 		DPRINTF(("%s: no CardBus card on cb%d\n", __func__,
    452  1.69   thorpej 		    device_unit(&sc->sc_dev)));
    453  1.29     enami 		return (0);
    454  1.29     enami 	}
    455  1.29     enami 
    456  1.29     enami 	/*
    457  1.29     enami 	 * XXX use fake function 8 to keep power on during whole
    458  1.29     enami 	 * configuration.
    459  1.29     enami 	 */
    460  1.29     enami 	enable_function(sc, cdstatus, 8);
    461  1.29     enami 	function = 0;
    462  1.29     enami 
    463  1.66  drochner 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, function);
    464  1.29     enami 
    465  1.29     enami 	/*
    466  1.29     enami 	 * Wait until power comes up.  Maxmum 500 ms.
    467  1.77    dyoung 	 *
    468  1.77    dyoung 	 * XXX What is this for?  The bridge driver ought to have waited
    469  1.77    dyoung 	 * XXX already.
    470  1.29     enami 	 */
    471  1.29     enami 	{
    472  1.29     enami 		int i;
    473  1.29     enami 
    474  1.29     enami 		for (i = 0; i < 5; ++i) {
    475  1.29     enami 			id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    476  1.29     enami 			if (id != 0xffffffff && id != 0) {
    477  1.29     enami 				break;
    478  1.29     enami 			}
    479  1.29     enami 			if (cold) {	/* before kernel thread invoked */
    480  1.29     enami 				delay(100 * 1000);
    481  1.29     enami 			} else {	/* thread context */
    482  1.29     enami 				if (tsleep((void *)sc, PCATCH, "cardbus",
    483  1.29     enami 				    hz / 10) != EWOULDBLOCK) {
    484  1.29     enami 					break;
    485  1.29     enami 				}
    486  1.29     enami 			}
    487  1.29     enami 		}
    488  1.29     enami 		if (i == 5) {
    489  1.52  drochner 			return (EIO);
    490  1.29     enami 		}
    491  1.29     enami 	}
    492  1.29     enami 
    493  1.34   thorpej 	bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    494  1.34   thorpej 	DPRINTF(("%s bhlc 0x%08x -> ", sc->sc_dev.dv_xname, bhlc));
    495  1.29     enami 	nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
    496  1.29     enami 
    497  1.29     enami 	for (function = 0; function < nfunction; function++) {
    498  1.29     enami 		struct cardbus_attach_args ca;
    499  1.62  drochner 		int locs[CARDBUSCF_NLOCS];
    500  1.52  drochner 
    501  1.56     enami 		if (locators[CARDBUSCF_FUNCTION] !=
    502  1.56     enami 		    CARDBUSCF_FUNCTION_DEFAULT &&
    503  1.56     enami 		    locators[CARDBUSCF_FUNCTION] != function)
    504  1.53  drochner 			continue;
    505  1.53  drochner 
    506  1.54  drochner 		if (sc->sc_funcs[function])
    507  1.52  drochner 			continue;
    508  1.13      haya 
    509  1.66  drochner 		tag = cardbus_make_tag(cc, cf, sc->sc_bus, function);
    510   1.1      haya 
    511  1.29     enami 		id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    512  1.29     enami 		class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
    513  1.29     enami 		cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
    514   1.1      haya 
    515  1.29     enami 		/* Invalid vendor ID value? */
    516  1.51   mycroft 		if (CARDBUS_VENDOR(id) == PCI_VENDOR_INVALID) {
    517  1.29     enami 			continue;
    518  1.29     enami 		}
    519   1.1      haya 
    520  1.29     enami 		DPRINTF(("cardbus_attach_card: "
    521  1.29     enami 		    "Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
    522  1.29     enami 		    CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
    523  1.29     enami 
    524  1.29     enami 		enable_function(sc, cdstatus, function);
    525  1.29     enami 
    526  1.29     enami 		/* clean up every BAR */
    527  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
    528  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
    529  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
    530  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
    531  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
    532  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
    533  1.29     enami 		cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
    534  1.32      haya 
    535  1.32      haya 		/* set initial latency and cacheline size */
    536  1.32      haya 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    537  1.77    dyoung 		icr = cardbus_conf_read(cc, cf, tag, CARDBUS_INTERRUPT_REG);
    538  1.77    dyoung 		DPRINTF(("%s func%d icr 0x%08x bhlc 0x%08x -> ",
    539  1.77    dyoung 		    device_xname(&sc->sc_dev), function, icr, bhlc));
    540  1.77    dyoung 		bhlc &= ~(CARDBUS_CACHELINE_MASK << CARDBUS_CACHELINE_SHIFT);
    541  1.56     enami 		bhlc |= (sc->sc_cacheline & CARDBUS_CACHELINE_MASK) <<
    542  1.56     enami 		    CARDBUS_CACHELINE_SHIFT;
    543  1.77    dyoung 		/*
    544  1.77    dyoung 		 * Set the initial value of the Latency Timer.
    545  1.77    dyoung 		 *
    546  1.77    dyoung 		 * While a PCI device owns the bus, its Latency
    547  1.77    dyoung 		 * Timer counts down bus cycles from its initial
    548  1.77    dyoung 		 * value to 0.  Minimum Grant tells for how long
    549  1.77    dyoung 		 * the device wants to own the bus once it gets
    550  1.77    dyoung 		 * access, in units of 250ns.
    551  1.77    dyoung 		 *
    552  1.77    dyoung 		 * On a 33 MHz bus, there are 8 cycles per 250ns.
    553  1.77    dyoung 		 * So I multiply the Minimum Grant by 8 to find
    554  1.77    dyoung 		 * out the initial value of the Latency Timer.
    555  1.77    dyoung 		 *
    556  1.77    dyoung 		 * Avoid setting a Latency Timer less than 0x10,
    557  1.77    dyoung 		 * since the old code did not do that.
    558  1.77    dyoung 		 */
    559  1.77    dyoung 		lattimer =
    560  1.77    dyoung 		    MIN(sc->sc_max_lattimer, MAX(0x10, 8 * PCI_MIN_GNT(icr)));
    561  1.77    dyoung 		if (PCI_LATTIMER(bhlc) < lattimer) {
    562  1.77    dyoung 			bhlc &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    563  1.77    dyoung 			bhlc |= (lattimer << PCI_LATTIMER_SHIFT);
    564  1.77    dyoung 		}
    565  1.32      haya 
    566  1.32      haya 		cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    567  1.32      haya 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    568  1.32      haya 		DPRINTF(("0x%08x\n", bhlc));
    569  1.61     perry 
    570  1.29     enami 		/*
    571  1.29     enami 		 * We need to allocate the ct here, since we might
    572  1.29     enami 		 * need it when reading the CIS
    573  1.29     enami 		 */
    574  1.29     enami 		if ((ct = malloc(sizeof(struct cardbus_devfunc),
    575  1.29     enami 		    M_DEVBUF, M_NOWAIT)) == NULL) {
    576  1.29     enami 			panic("no room for cardbus_tag");
    577  1.29     enami 		}
    578  1.29     enami 
    579  1.77    dyoung 		ct->ct_bhlc = bhlc;
    580  1.29     enami 		ct->ct_cc = sc->sc_cc;
    581  1.29     enami 		ct->ct_cf = sc->sc_cf;
    582  1.29     enami 		ct->ct_bus = sc->sc_bus;
    583  1.29     enami 		ct->ct_func = function;
    584  1.29     enami 		ct->ct_sc = sc;
    585  1.54  drochner 		sc->sc_funcs[function] = ct;
    586  1.29     enami 
    587  1.29     enami 		memset(&ca, 0, sizeof(ca));
    588  1.29     enami 
    589  1.29     enami 		ca.ca_ct = ct;
    590  1.29     enami 
    591  1.29     enami 		ca.ca_iot = sc->sc_iot;
    592  1.29     enami 		ca.ca_memt = sc->sc_memt;
    593  1.29     enami 		ca.ca_dmat = sc->sc_dmat;
    594  1.35       mcr 
    595  1.35       mcr #if rbus
    596  1.35       mcr 		ca.ca_rbus_iot = sc->sc_rbus_iot;
    597  1.35       mcr 		ca.ca_rbus_memt= sc->sc_rbus_memt;
    598  1.35       mcr #endif
    599  1.29     enami 
    600  1.29     enami 		ca.ca_tag = tag;
    601  1.36  augustss 		ca.ca_bus = sc->sc_bus;
    602  1.29     enami 		ca.ca_function = function;
    603  1.29     enami 		ca.ca_id = id;
    604  1.29     enami 		ca.ca_class = class;
    605   1.1      haya 
    606  1.29     enami 		ca.ca_intrline = sc->sc_intrline;
    607   1.1      haya 
    608  1.50   mycroft 		if (cis_ptr != 0) {
    609  1.72  christos #define TUPLESIZE 2048
    610  1.72  christos 			u_int8_t *tuple = malloc(TUPLESIZE, M_DEVBUF, M_WAITOK);
    611  1.57     enami 			if (cardbus_read_tuples(&ca, cis_ptr,
    612  1.72  christos 			    tuple, TUPLESIZE)) {
    613  1.57     enami 				printf("cardbus_attach_card: "
    614  1.57     enami 				    "failed to read CIS\n");
    615  1.50   mycroft 			} else {
    616  1.29     enami #ifdef CARDBUS_DEBUG
    617  1.72  christos 				decode_tuples(tuple, TUPLESIZE,
    618  1.57     enami 				    print_tuple, NULL);
    619  1.29     enami #endif
    620  1.72  christos 				decode_tuples(tuple, TUPLESIZE,
    621  1.57     enami 				    parse_tuple, &ca.ca_cis);
    622  1.50   mycroft 			}
    623  1.72  christos 			free(tuple, M_DEVBUF);
    624  1.29     enami 		}
    625   1.1      haya 
    626  1.62  drochner 		locs[CARDBUSCF_FUNCTION] = function;
    627  1.52  drochner 
    628  1.62  drochner 		if ((csc = config_found_sm_loc((void *)sc, "cardbus", locs,
    629  1.63  drochner 		    &ca, cardbusprint, config_stdsubmatch)) == NULL) {
    630  1.29     enami 			/* do not match */
    631  1.29     enami 			disable_function(sc, function);
    632  1.54  drochner 			sc->sc_funcs[function] = NULL;
    633  1.29     enami 			free(ct, M_DEVBUF);
    634  1.29     enami 		} else {
    635  1.29     enami 			/* found */
    636  1.29     enami 			ct->ct_device = csc;
    637  1.29     enami 		}
    638  1.29     enami 	}
    639  1.29     enami 	/*
    640  1.29     enami 	 * XXX power down pseudo function 8 (this will power down the card
    641  1.29     enami 	 * if no functions were attached).
    642  1.29     enami 	 */
    643  1.29     enami 	disable_function(sc, 8);
    644   1.1      haya 
    645  1.52  drochner 	return (0);
    646   1.1      haya }
    647   1.1      haya 
    648  1.29     enami static int
    649  1.29     enami cardbusprint(void *aux, const char *pnp)
    650  1.29     enami {
    651  1.29     enami 	struct cardbus_attach_args *ca = aux;
    652  1.29     enami 	char devinfo[256];
    653  1.29     enami 	int i;
    654  1.29     enami 
    655  1.29     enami 	if (pnp) {
    656  1.48    itojun 		pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo,
    657  1.48    itojun 		    sizeof(devinfo));
    658  1.29     enami 		for (i = 0; i < 4; i++) {
    659  1.29     enami 			if (ca->ca_cis.cis1_info[i] == NULL)
    660  1.29     enami 				break;
    661  1.29     enami 			if (i)
    662  1.47   thorpej 				aprint_normal(", ");
    663  1.47   thorpej 			aprint_normal("%s", ca->ca_cis.cis1_info[i]);
    664  1.29     enami 		}
    665  1.47   thorpej 		aprint_verbose("%s(manufacturer 0x%x, product 0x%x)",
    666  1.56     enami 		    i ? " " : "",
    667  1.56     enami 		    ca->ca_cis.manufacturer, ca->ca_cis.product);
    668  1.47   thorpej 		aprint_normal(" %s at %s", devinfo, pnp);
    669  1.29     enami 	}
    670  1.66  drochner 	aprint_normal(" function %d", ca->ca_function);
    671   1.1      haya 
    672  1.29     enami 	return (UNCONF);
    673  1.29     enami }
    674   1.1      haya 
    675   1.1      haya /*
    676  1.18      haya  * void cardbus_detach_card(struct cardbus_softc *sc)
    677  1.18      haya  *
    678  1.18      haya  *    This function detaches the card on the slot: detach device data
    679  1.18      haya  *    structure and turns off the power.
    680  1.18      haya  *
    681  1.18      haya  *    This function must not be called under interrupt context.
    682  1.18      haya  */
    683  1.18      haya void
    684  1.29     enami cardbus_detach_card(struct cardbus_softc *sc)
    685  1.18      haya {
    686  1.54  drochner 	int f;
    687  1.54  drochner 	struct cardbus_devfunc *ct;
    688  1.18      haya 
    689  1.54  drochner 	for (f = 0; f < 8; f++) {
    690  1.54  drochner 		ct = sc->sc_funcs[f];
    691  1.54  drochner 		if (!ct)
    692  1.54  drochner 			continue;
    693  1.18      haya 
    694  1.29     enami 		DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname,
    695  1.54  drochner 		    ct->ct_device->dv_xname));
    696  1.29     enami 		/* call device detach function */
    697  1.18      haya 
    698  1.56     enami 		if (config_detach(ct->ct_device, 0) != 0) {
    699  1.33  augustss 			printf("%s: cannot detach dev %s, function %d\n",
    700  1.54  drochner 			    sc->sc_dev.dv_xname, ct->ct_device->dv_xname,
    701  1.54  drochner 			    ct->ct_func);
    702  1.52  drochner 		}
    703  1.52  drochner 	}
    704  1.52  drochner 
    705  1.52  drochner 	sc->sc_poweron_func = 0;
    706  1.52  drochner 	(*sc->sc_cf->cardbus_power)(sc->sc_cc,
    707  1.52  drochner 	    CARDBUS_VCC_0V | CARDBUS_VPP_0V);
    708  1.52  drochner }
    709  1.52  drochner 
    710  1.52  drochner void
    711  1.52  drochner cardbus_childdetached(struct device *self, struct device *child)
    712  1.52  drochner {
    713  1.71   thorpej 	struct cardbus_softc *sc = device_private(self);
    714  1.54  drochner 	struct cardbus_devfunc *ct;
    715  1.52  drochner 
    716  1.70   thorpej 	ct = sc->sc_funcs[device_locator(child, CARDBUSCF_FUNCTION)];
    717  1.54  drochner 	KASSERT(ct->ct_device == child);
    718  1.19      haya 
    719  1.54  drochner 	sc->sc_poweron_func &= ~(1 << ct->ct_func);
    720  1.54  drochner 	sc->sc_funcs[ct->ct_func] = NULL;
    721  1.54  drochner 	free(ct, M_DEVBUF);
    722  1.18      haya }
    723  1.18      haya 
    724  1.18      haya /*
    725   1.1      haya  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    726   1.1      haya  *   Interrupt handler of pccard.
    727   1.1      haya  *  args:
    728   1.1      haya  *   cardbus_chipset_tag_t *cc
    729  1.29     enami  *   int irq:
    730   1.1      haya  */
    731   1.1      haya void *
    732  1.29     enami cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    733  1.29     enami     cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg)
    734   1.1      haya {
    735   1.1      haya 
    736  1.29     enami 	DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    737  1.29     enami 	return ((*cf->cardbus_intr_establish)(cc, irq, level, func, arg));
    738   1.1      haya }
    739   1.1      haya 
    740   1.1      haya /*
    741   1.1      haya  * void cardbus_intr_disestablish(cc, cf, handler)
    742   1.1      haya  *   Interrupt handler of pccard.
    743   1.1      haya  *  args:
    744   1.1      haya  *   cardbus_chipset_tag_t *cc
    745   1.1      haya  */
    746   1.1      haya void
    747  1.29     enami cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    748  1.29     enami     void *handler)
    749   1.1      haya {
    750   1.1      haya 
    751  1.29     enami 	DPRINTF(("- pccard_intr_disestablish\n"));
    752  1.29     enami 	(*cf->cardbus_intr_disestablish)(cc, handler);
    753   1.1      haya }
    754   1.1      haya 
    755  1.29     enami /*
    756  1.29     enami  * XXX this should be merged with cardbus_function_{enable,disable},
    757  1.29     enami  * but we don't have a ct when these functions are called.
    758  1.29     enami  */
    759   1.8      joda static void
    760  1.29     enami enable_function(struct cardbus_softc *sc, int cdstatus, int function)
    761   1.8      joda {
    762  1.18      haya 
    763  1.29     enami 	if (sc->sc_poweron_func == 0) {
    764  1.29     enami 		/* switch to 3V and/or wait for power to stabilize */
    765  1.29     enami 		if (cdstatus & CARDBUS_3V_CARD) {
    766  1.31      haya 			/*
    767  1.31      haya 			 * sc_poweron_func must be substituted before
    768  1.31      haya 			 * entering sleep, in order to avoid turn on
    769  1.31      haya 			 * power twice.
    770  1.31      haya 			 */
    771  1.31      haya 			sc->sc_poweron_func |= (1 << function);
    772  1.29     enami 			(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_3V);
    773  1.29     enami 		} else {
    774  1.29     enami 			/* No cards other than 3.3V cards. */
    775  1.29     enami 			return;
    776  1.29     enami 		}
    777  1.29     enami 		(*sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
    778  1.23      haya 	}
    779  1.29     enami 	sc->sc_poweron_func |= (1 << function);
    780   1.8      joda }
    781   1.8      joda 
    782   1.8      joda static void
    783  1.29     enami disable_function(struct cardbus_softc *sc, int function)
    784   1.8      joda {
    785  1.18      haya 
    786  1.29     enami 	sc->sc_poweron_func &= ~(1 << function);
    787  1.29     enami 	if (sc->sc_poweron_func == 0) {
    788  1.29     enami 		/* power-off because no functions are enabled */
    789  1.29     enami 		(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_0V);
    790  1.29     enami 	}
    791   1.8      joda }
    792   1.8      joda 
    793   1.1      haya /*
    794   1.9      haya  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
    795   1.1      haya  *
    796   1.1      haya  *   This function enables a function on a card.  When no power is
    797   1.1      haya  *  applied on the card, power will be applied on it.
    798   1.1      haya  */
    799   1.1      haya int
    800  1.29     enami cardbus_function_enable(struct cardbus_softc *sc, int func)
    801   1.1      haya {
    802  1.29     enami 	cardbus_chipset_tag_t cc = sc->sc_cc;
    803  1.29     enami 	cardbus_function_tag_t cf = sc->sc_cf;
    804  1.77    dyoung 	cardbus_devfunc_t ct;
    805  1.29     enami 	cardbusreg_t command;
    806  1.29     enami 	cardbustag_t tag;
    807   1.1      haya 
    808  1.29     enami 	DPRINTF(("entering cardbus_function_enable...  "));
    809   1.1      haya 
    810  1.29     enami 	/* entering critical area */
    811   1.1      haya 
    812  1.29     enami 	/* XXX: sc_vold should be used */
    813  1.29     enami 	enable_function(sc, CARDBUS_3V_CARD, func);
    814   1.1      haya 
    815  1.29     enami 	/* exiting critical area */
    816   1.1      haya 
    817  1.66  drochner 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
    818   1.9      haya 
    819  1.29     enami 	command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    820  1.29     enami 	command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE |
    821  1.29     enami 	    CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    822   1.9      haya 
    823  1.29     enami 	cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    824   1.1      haya 
    825  1.77    dyoung 	if ((ct = sc->sc_funcs[func]) != NULL)
    826  1.77    dyoung 		Cardbus_conf_write(ct, tag, CARDBUS_BHLC_REG, ct->ct_bhlc);
    827  1.77    dyoung 
    828  1.29     enami 	cardbus_free_tag(cc, cf, tag);
    829   1.1      haya 
    830  1.29     enami 	DPRINTF(("%x\n", sc->sc_poweron_func));
    831   1.1      haya 
    832  1.29     enami 	return (0);
    833   1.1      haya }
    834   1.1      haya 
    835   1.1      haya /*
    836   1.9      haya  * int cardbus_function_disable(struct cardbus_softc *, int func)
    837   1.1      haya  *
    838   1.1      haya  *   This function disable a function on a card.  When no functions are
    839   1.1      haya  *  enabled, it turns off the power.
    840   1.1      haya  */
    841   1.1      haya int
    842  1.29     enami cardbus_function_disable(struct cardbus_softc *sc, int func)
    843   1.1      haya {
    844   1.1      haya 
    845  1.29     enami 	DPRINTF(("entering cardbus_function_disable...  "));
    846   1.1      haya 
    847  1.29     enami 	disable_function(sc, func);
    848   1.1      haya 
    849  1.29     enami 	return (0);
    850   1.1      haya }
    851   1.1      haya 
    852  1.16   thorpej /*
    853  1.16   thorpej  * int cardbus_get_capability(cardbus_chipset_tag_t cc,
    854  1.16   thorpej  *	cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
    855  1.16   thorpej  *	cardbusreg_t *value)
    856  1.16   thorpej  *
    857  1.16   thorpej  *	Find the specified PCI capability.
    858  1.16   thorpej  */
    859  1.16   thorpej int
    860  1.29     enami cardbus_get_capability(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    861  1.29     enami     cardbustag_t tag, int capid, int *offset, cardbusreg_t *value)
    862  1.16   thorpej {
    863  1.16   thorpej 	cardbusreg_t reg;
    864  1.16   thorpej 	unsigned int ofs;
    865  1.16   thorpej 
    866  1.16   thorpej 	reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
    867  1.16   thorpej 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
    868  1.16   thorpej 		return (0);
    869  1.16   thorpej 
    870  1.16   thorpej 	ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
    871  1.16   thorpej 	    PCI_CAPLISTPTR_REG));
    872  1.16   thorpej 	while (ofs != 0) {
    873  1.16   thorpej #ifdef DIAGNOSTIC
    874  1.16   thorpej 		if ((ofs & 3) || (ofs < 0x40))
    875  1.16   thorpej 			panic("cardbus_get_capability");
    876  1.16   thorpej #endif
    877  1.16   thorpej 		reg = cardbus_conf_read(cc, cf, tag, ofs);
    878  1.16   thorpej 		if (PCI_CAPLIST_CAP(reg) == capid) {
    879  1.16   thorpej 			if (offset)
    880  1.16   thorpej 				*offset = ofs;
    881  1.16   thorpej 			if (value)
    882  1.16   thorpej 				*value = reg;
    883  1.16   thorpej 			return (1);
    884  1.16   thorpej 		}
    885  1.16   thorpej 		ofs = PCI_CAPLIST_NEXT(reg);
    886  1.16   thorpej 	}
    887   1.1      haya 
    888  1.16   thorpej 	return (0);
    889  1.16   thorpej }
    890   1.1      haya 
    891   1.1      haya /*
    892   1.1      haya  * below this line, there are some functions for decoding tuples.
    893   1.1      haya  * They should go out from this file.
    894   1.1      haya  */
    895   1.1      haya 
    896  1.10      joda static u_int8_t *
    897  1.57     enami decode_tuple(u_int8_t *, u_int8_t *, tuple_decode_func, void *);
    898   1.1      haya 
    899   1.1      haya static int
    900  1.29     enami decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
    901  1.29     enami {
    902  1.29     enami 	u_int8_t *tp = tuple;
    903  1.29     enami 
    904  1.29     enami 	if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
    905  1.29     enami 		DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    906  1.29     enami 		return (0);
    907  1.29     enami 	}
    908  1.29     enami 
    909  1.57     enami 	while ((tp = decode_tuple(tp, tuple + buflen, func, data)) != NULL)
    910  1.57     enami 		;
    911  1.29     enami 
    912  1.29     enami 	return (1);
    913   1.1      haya }
    914   1.1      haya 
    915   1.1      haya static u_int8_t *
    916  1.57     enami decode_tuple(u_int8_t *tuple, u_int8_t *end,
    917  1.57     enami     tuple_decode_func func, void *data)
    918   1.1      haya {
    919  1.29     enami 	u_int8_t type;
    920  1.29     enami 	u_int8_t len;
    921   1.1      haya 
    922  1.29     enami 	type = tuple[0];
    923  1.60     enami 	switch (type) {
    924  1.60     enami 	case PCMCIA_CISTPL_NULL:
    925  1.60     enami 	case PCMCIA_CISTPL_END:
    926  1.60     enami 		len = 1;
    927  1.60     enami 		break;
    928  1.60     enami 	default:
    929  1.60     enami 		if (tuple + 2 > end)
    930  1.60     enami 			return (NULL);
    931  1.60     enami 		len = tuple[1] + 2;
    932  1.60     enami 		break;
    933  1.60     enami 	}
    934   1.1      haya 
    935  1.57     enami 	if (tuple + len > end)
    936  1.57     enami 		return (NULL);
    937  1.57     enami 
    938  1.29     enami 	(*func)(tuple, len, data);
    939   1.1      haya 
    940  1.60     enami 	if (type == PCMCIA_CISTPL_END || tuple + len == end)
    941  1.29     enami 		return (NULL);
    942   1.1      haya 
    943  1.29     enami 	return (tuple + len);
    944   1.1      haya }
    945   1.1      haya 
    946  1.49  christos /*
    947  1.49  christos  * XXX: this is another reason why this code should be shared with PCI.
    948  1.49  christos  */
    949  1.81  jmcneill static int
    950  1.81  jmcneill cardbus_get_powerstate_int(cardbus_devfunc_t ct, cardbustag_t tag,
    951  1.81  jmcneill     cardbusreg_t *state, int offset)
    952  1.81  jmcneill {
    953  1.81  jmcneill 	cardbusreg_t value, now;
    954  1.81  jmcneill 	cardbus_chipset_tag_t cc = ct->ct_cc;
    955  1.81  jmcneill 	cardbus_function_tag_t cf = ct->ct_cf;
    956  1.81  jmcneill 
    957  1.81  jmcneill 	value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR);
    958  1.81  jmcneill 	now = value & PCI_PMCSR_STATE_MASK;
    959  1.81  jmcneill 	switch (now) {
    960  1.81  jmcneill 	case PCI_PMCSR_STATE_D0:
    961  1.81  jmcneill 	case PCI_PMCSR_STATE_D1:
    962  1.81  jmcneill 	case PCI_PMCSR_STATE_D2:
    963  1.81  jmcneill 	case PCI_PMCSR_STATE_D3:
    964  1.81  jmcneill 		*state = now;
    965  1.81  jmcneill 		return 0;
    966  1.81  jmcneill 	default:
    967  1.81  jmcneill 		return EINVAL;
    968  1.81  jmcneill 	}
    969  1.81  jmcneill }
    970  1.81  jmcneill 
    971  1.49  christos int
    972  1.81  jmcneill cardbus_get_powerstate(cardbus_devfunc_t ct, cardbustag_t tag,
    973  1.81  jmcneill     cardbusreg_t *state)
    974  1.49  christos {
    975  1.49  christos 	cardbus_chipset_tag_t cc = ct->ct_cc;
    976  1.49  christos 	cardbus_function_tag_t cf = ct->ct_cf;
    977  1.49  christos 	int offset;
    978  1.81  jmcneill 	cardbusreg_t value;
    979  1.49  christos 
    980  1.81  jmcneill 	if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset, &value))
    981  1.49  christos 		return EOPNOTSUPP;
    982  1.49  christos 
    983  1.81  jmcneill 	return cardbus_get_powerstate_int(ct, tag, state, offset);
    984  1.81  jmcneill }
    985  1.81  jmcneill 
    986  1.81  jmcneill static int
    987  1.81  jmcneill cardbus_set_powerstate_int(cardbus_devfunc_t ct, cardbustag_t tag,
    988  1.81  jmcneill     cardbusreg_t state, int offset, cardbusreg_t cap_reg)
    989  1.81  jmcneill {
    990  1.81  jmcneill 	cardbus_chipset_tag_t cc = ct->ct_cc;
    991  1.81  jmcneill 	cardbus_function_tag_t cf = ct->ct_cf;
    992  1.81  jmcneill 
    993  1.81  jmcneill 	cardbusreg_t value, cap, now;
    994  1.81  jmcneill 
    995  1.81  jmcneill 	cap = cap_reg >> PCI_PMCR_SHIFT;
    996  1.49  christos 	value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR);
    997  1.49  christos 	now = value & PCI_PMCSR_STATE_MASK;
    998  1.81  jmcneill 	value &= ~PCI_PMCSR_STATE_MASK;
    999  1.81  jmcneill 
   1000  1.81  jmcneill 	if (now == state)
   1001  1.49  christos 		return 0;
   1002  1.81  jmcneill 	switch (state) {
   1003  1.81  jmcneill 	case PCI_PMCSR_STATE_D0:
   1004  1.49  christos 		value |= PCI_PMCSR_STATE_D0;
   1005  1.49  christos 		break;
   1006  1.81  jmcneill 	case PCI_PMCSR_STATE_D1:
   1007  1.81  jmcneill 		if (now == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D3) {
   1008  1.81  jmcneill 			printf("invalid transition from %d to D1\n", (int)now);
   1009  1.49  christos 			return EINVAL;
   1010  1.81  jmcneill 		}
   1011  1.81  jmcneill 		if (!(cap & PCI_PMCR_D1SUPP)) {
   1012  1.81  jmcneill 			printf("D1 not supported\n");
   1013  1.49  christos 			return EOPNOTSUPP;
   1014  1.81  jmcneill 		}
   1015  1.49  christos 		value |= PCI_PMCSR_STATE_D1;
   1016  1.49  christos 		break;
   1017  1.81  jmcneill 	case PCI_PMCSR_STATE_D2:
   1018  1.81  jmcneill 		if (now == PCI_PMCSR_STATE_D3) {
   1019  1.81  jmcneill 			printf("invalid transition from %d to D2\n", (int)now);
   1020  1.49  christos 			return EINVAL;
   1021  1.81  jmcneill 		}
   1022  1.81  jmcneill 		if (!(cap & PCI_PMCR_D2SUPP)) {
   1023  1.81  jmcneill 			printf("D2 not supported\n");
   1024  1.49  christos 			return EOPNOTSUPP;
   1025  1.81  jmcneill 		}
   1026  1.49  christos 		value |= PCI_PMCSR_STATE_D2;
   1027  1.49  christos 		break;
   1028  1.81  jmcneill 	case PCI_PMCSR_STATE_D3:
   1029  1.49  christos 		value |= PCI_PMCSR_STATE_D3;
   1030  1.49  christos 		break;
   1031  1.49  christos 	default:
   1032  1.49  christos 		return EINVAL;
   1033  1.49  christos 	}
   1034  1.81  jmcneill 
   1035  1.49  christos 	cardbus_conf_write(cc, cf, tag, offset + PCI_PMCSR, value);
   1036  1.49  christos 	DELAY(1000);
   1037  1.49  christos 
   1038  1.49  christos 	return 0;
   1039  1.49  christos }
   1040  1.49  christos 
   1041  1.49  christos int
   1042  1.81  jmcneill cardbus_set_powerstate(cardbus_devfunc_t ct, cardbustag_t tag, cardbusreg_t state)
   1043  1.49  christos {
   1044  1.81  jmcneill 	cardbus_chipset_tag_t cc = ct->ct_cc;
   1045  1.81  jmcneill 	cardbus_function_tag_t cf = ct->ct_cf;
   1046  1.81  jmcneill 	int offset;
   1047  1.81  jmcneill 	cardbusreg_t value;
   1048  1.49  christos 
   1049  1.81  jmcneill 	if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset,
   1050  1.81  jmcneill 	    &value))
   1051  1.81  jmcneill 		return EOPNOTSUPP;
   1052  1.49  christos 
   1053  1.81  jmcneill 	return cardbus_set_powerstate_int(ct, tag, state, offset, value);
   1054  1.49  christos }
   1055  1.49  christos 
   1056   1.3  augustss #ifdef CARDBUS_DEBUG
   1057  1.29     enami static const char *tuple_name(int);
   1058  1.29     enami static const char *tuple_names[] = {
   1059  1.29     enami 	"TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
   1060  1.29     enami 	"CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
   1061  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
   1062  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
   1063  1.29     enami 	"CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
   1064  1.29     enami 	"NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
   1065  1.29     enami 	"JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
   1066  1.29     enami 	"DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
   1067  1.29     enami 	"MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
   1068  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
   1069  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
   1070  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
   1071  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
   1072  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
   1073  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
   1074  1.29     enami 	"Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
   1075  1.29     enami 	"VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
   1076  1.29     enami 	"DATE", "BATTERY", "ORG"
   1077  1.29     enami };
   1078  1.29     enami #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
   1079  1.10      joda 
   1080  1.29     enami static const char *
   1081  1.29     enami tuple_name(int type)
   1082  1.29     enami {
   1083   1.1      haya 
   1084  1.29     enami 	if (0 <= type && type < NAME_LEN(tuple_names)) {
   1085  1.29     enami 		return (tuple_names[type]);
   1086  1.29     enami 	} else if (type == 0xff) {
   1087  1.29     enami 		return ("END");
   1088  1.29     enami 	} else {
   1089  1.29     enami 		return ("Reserved");
   1090  1.29     enami 	}
   1091   1.1      haya }
   1092  1.10      joda 
   1093  1.10      joda static void
   1094  1.74  christos print_tuple(u_int8_t *tuple, int len, void *data)
   1095  1.29     enami {
   1096  1.29     enami 	int i;
   1097  1.29     enami 
   1098  1.29     enami 	printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
   1099  1.29     enami 
   1100  1.29     enami 	for (i = 0; i < len; ++i) {
   1101  1.29     enami 		if (i % 16 == 0) {
   1102  1.29     enami 			printf("  0x%2x:", i);
   1103  1.29     enami 		}
   1104  1.29     enami 		printf(" %x", tuple[i]);
   1105  1.29     enami 		if (i % 16 == 15) {
   1106  1.29     enami 			printf("\n");
   1107  1.29     enami 		}
   1108  1.29     enami 	}
   1109  1.29     enami 	if (i % 16 != 0) {
   1110  1.29     enami 		printf("\n");
   1111  1.29     enami 	}
   1112  1.10      joda }
   1113   1.3  augustss #endif
   1114  1.81  jmcneill 
   1115  1.81  jmcneill void
   1116  1.81  jmcneill cardbus_conf_capture(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
   1117  1.81  jmcneill     cardbustag_t tag, struct cardbus_conf_state *pcs)
   1118  1.81  jmcneill {
   1119  1.81  jmcneill 	int off;
   1120  1.81  jmcneill 
   1121  1.81  jmcneill 	for (off = 0; off < 16; off++)
   1122  1.81  jmcneill 		pcs->reg[off] = cardbus_conf_read(cc, cf, tag, (off * 4));
   1123  1.81  jmcneill }
   1124  1.81  jmcneill 
   1125  1.81  jmcneill void
   1126  1.81  jmcneill cardbus_conf_restore(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
   1127  1.81  jmcneill     cardbustag_t tag, struct cardbus_conf_state *pcs)
   1128  1.81  jmcneill {
   1129  1.81  jmcneill 	int off;
   1130  1.81  jmcneill 	cardbusreg_t val;
   1131  1.81  jmcneill 
   1132  1.81  jmcneill 	for (off = 15; off >= 0; off--) {
   1133  1.81  jmcneill 		val = cardbus_conf_read(cc, cf, tag, (off * 4));
   1134  1.81  jmcneill 		if (val != pcs->reg[off])
   1135  1.81  jmcneill 			cardbus_conf_write(cc, cf,tag, (off * 4), pcs->reg[off]);
   1136  1.81  jmcneill 	}
   1137  1.81  jmcneill }
   1138  1.81  jmcneill 
   1139  1.81  jmcneill void
   1140  1.81  jmcneill cardbus_disable_retry(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
   1141  1.81  jmcneill     cardbustag_t tag)
   1142  1.81  jmcneill {
   1143  1.86    dyoung 	/* See comment on sys/dev/pci/pci.c:pci_disable_retry() for
   1144  1.86    dyoung 	 * the reason I comment-out this code.
   1145  1.86    dyoung 	 */
   1146  1.86    dyoung #if 0
   1147  1.81  jmcneill 	cardbusreg_t retry;
   1148  1.81  jmcneill 
   1149  1.81  jmcneill 	retry = cardbus_conf_read(cc, cf, tag, PCI_RETRY_TIMEOUT_REG);
   1150  1.81  jmcneill 	retry &= ~PCI_RETRY_TIMEOUT_REG_MASK;
   1151  1.81  jmcneill 	cardbus_conf_write(cc, cf, tag, PCI_RETRY_TIMEOUT_REG, retry);
   1152  1.86    dyoung #endif
   1153  1.81  jmcneill }
   1154  1.81  jmcneill 
   1155  1.81  jmcneill struct cardbus_child_power {
   1156  1.81  jmcneill 	struct cardbus_conf_state p_cardbusconf;
   1157  1.81  jmcneill 	cardbus_devfunc_t p_ct;
   1158  1.81  jmcneill 	cardbustag_t p_tag;
   1159  1.81  jmcneill 	cardbus_chipset_tag_t p_cc;
   1160  1.81  jmcneill 	cardbus_function_tag_t p_cf;
   1161  1.81  jmcneill 	cardbusreg_t p_pm_cap;
   1162  1.81  jmcneill 	bool p_has_pm;
   1163  1.81  jmcneill 	int p_pm_offset;
   1164  1.81  jmcneill };
   1165  1.81  jmcneill 
   1166  1.81  jmcneill static bool
   1167  1.81  jmcneill cardbus_child_suspend(device_t dv)
   1168  1.81  jmcneill {
   1169  1.81  jmcneill 	struct cardbus_child_power *priv = device_pmf_bus_private(dv);
   1170  1.81  jmcneill 
   1171  1.81  jmcneill 	cardbus_conf_capture(priv->p_cc, priv->p_cf, priv->p_tag,
   1172  1.81  jmcneill 	    &priv->p_cardbusconf);
   1173  1.81  jmcneill 
   1174  1.81  jmcneill 	if (priv->p_has_pm &&
   1175  1.81  jmcneill 	    cardbus_set_powerstate_int(priv->p_ct, priv->p_tag,
   1176  1.81  jmcneill 	    PCI_PMCSR_STATE_D3, priv->p_pm_offset, priv->p_pm_cap)) {
   1177  1.81  jmcneill 		aprint_error_dev(dv, "unsupported state, continuing.\n");
   1178  1.81  jmcneill 		return false;
   1179  1.81  jmcneill 	}
   1180  1.81  jmcneill 
   1181  1.81  jmcneill 	Cardbus_function_disable(priv->p_ct);
   1182  1.81  jmcneill 
   1183  1.81  jmcneill 	return true;
   1184  1.81  jmcneill }
   1185  1.81  jmcneill 
   1186  1.81  jmcneill static bool
   1187  1.81  jmcneill cardbus_child_resume(device_t dv)
   1188  1.81  jmcneill {
   1189  1.81  jmcneill 	struct cardbus_child_power *priv = device_pmf_bus_private(dv);
   1190  1.81  jmcneill 
   1191  1.81  jmcneill 	Cardbus_function_enable(priv->p_ct);
   1192  1.81  jmcneill 
   1193  1.81  jmcneill 	if (priv->p_has_pm &&
   1194  1.81  jmcneill 	    cardbus_set_powerstate_int(priv->p_ct, priv->p_tag,
   1195  1.81  jmcneill 	    PCI_PMCSR_STATE_D0, priv->p_pm_offset, priv->p_pm_cap)) {
   1196  1.81  jmcneill 		aprint_error_dev(dv, "unsupported state, continuing.\n");
   1197  1.81  jmcneill 		return false;
   1198  1.81  jmcneill 	}
   1199  1.81  jmcneill 
   1200  1.81  jmcneill 	cardbus_conf_restore(priv->p_cc, priv->p_cf, priv->p_tag,
   1201  1.81  jmcneill 	    &priv->p_cardbusconf);
   1202  1.81  jmcneill 
   1203  1.81  jmcneill 	return true;
   1204  1.81  jmcneill }
   1205  1.81  jmcneill 
   1206  1.81  jmcneill static void
   1207  1.81  jmcneill cardbus_child_deregister(device_t dv)
   1208  1.81  jmcneill {
   1209  1.81  jmcneill 	struct cardbus_child_power *priv = device_pmf_bus_private(dv);
   1210  1.81  jmcneill 
   1211  1.81  jmcneill 	free(priv, M_DEVBUF);
   1212  1.81  jmcneill }
   1213  1.81  jmcneill 
   1214  1.81  jmcneill static bool
   1215  1.81  jmcneill cardbus_child_register(device_t child)
   1216  1.81  jmcneill {
   1217  1.81  jmcneill 	device_t self = device_parent(child);
   1218  1.81  jmcneill 	struct cardbus_softc *sc = device_private(self);
   1219  1.81  jmcneill 	struct cardbus_devfunc *ct;
   1220  1.81  jmcneill 	struct cardbus_child_power *priv;
   1221  1.81  jmcneill 	int off;
   1222  1.81  jmcneill 	cardbusreg_t reg;
   1223  1.81  jmcneill 
   1224  1.81  jmcneill 	ct = sc->sc_funcs[device_locator(child, CARDBUSCF_FUNCTION)];
   1225  1.81  jmcneill 
   1226  1.81  jmcneill 	priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK);
   1227  1.81  jmcneill 
   1228  1.81  jmcneill 	priv->p_ct = ct;
   1229  1.85    dyoung 	priv->p_cc = ct->ct_cc;
   1230  1.81  jmcneill 	priv->p_cf = ct->ct_cf;
   1231  1.81  jmcneill 	priv->p_tag = cardbus_make_tag(priv->p_cc, priv->p_cf, ct->ct_bus,
   1232  1.81  jmcneill 	    ct->ct_func);
   1233  1.81  jmcneill 
   1234  1.81  jmcneill 	if (cardbus_get_capability(priv->p_cc, priv->p_cf, priv->p_tag,
   1235  1.81  jmcneill 				   PCI_CAP_PWRMGMT, &off, &reg)) {
   1236  1.81  jmcneill 		priv->p_has_pm = true;
   1237  1.81  jmcneill 		priv->p_pm_offset = off;
   1238  1.81  jmcneill 		priv->p_pm_cap = reg;
   1239  1.81  jmcneill 	} else {
   1240  1.81  jmcneill 		priv->p_has_pm = false;
   1241  1.81  jmcneill 		priv->p_pm_offset = -1;
   1242  1.81  jmcneill 	}
   1243  1.81  jmcneill 
   1244  1.81  jmcneill 	device_pmf_bus_register(child, priv, cardbus_child_suspend,
   1245  1.81  jmcneill 	    cardbus_child_resume, cardbus_child_deregister);
   1246  1.81  jmcneill 
   1247  1.81  jmcneill 	return true;
   1248  1.81  jmcneill }
   1249