Home | History | Annotate | Line # | Download | only in pci
pccbb.c revision 1.173
      1 /*	$NetBSD: pccbb.c,v 1.173 2008/06/25 15:29:23 drochner Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 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  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: pccbb.c,v 1.173 2008/06/25 15:29:23 drochner Exp $");
     35 
     36 /*
     37 #define CBB_DEBUG
     38 #define SHOW_REGS
     39 */
     40 
     41 /*
     42  * BROKEN!
     43 #define PCCBB_PCMCIA_POLL
     44 #define CB_PCMCIA_POLL
     45 #define CB_PCMCIA_POLL_ONLY
     46 #define LEVEL2
     47 */
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/kernel.h>
     52 #include <sys/errno.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/reboot.h>		/* for bootverbose */
     55 #include <sys/syslog.h>
     56 #include <sys/device.h>
     57 #include <sys/malloc.h>
     58 #include <sys/proc.h>
     59 
     60 #include <sys/intr.h>
     61 #include <sys/bus.h>
     62 
     63 #include <dev/pci/pcivar.h>
     64 #include <dev/pci/pcireg.h>
     65 #include <dev/pci/pcidevs.h>
     66 
     67 #include <dev/pci/pccbbreg.h>
     68 
     69 #include <dev/cardbus/cardslotvar.h>
     70 
     71 #include <dev/cardbus/cardbusvar.h>
     72 
     73 #include <dev/pcmcia/pcmciareg.h>
     74 #include <dev/pcmcia/pcmciavar.h>
     75 
     76 #include <dev/ic/i82365reg.h>
     77 #include <dev/pci/pccbbvar.h>
     78 
     79 #ifndef __NetBSD_Version__
     80 struct cfdriver cbb_cd = {
     81 	NULL, "cbb", DV_DULL
     82 };
     83 #endif
     84 
     85 #ifdef CBB_DEBUG
     86 #define DPRINTF(x) printf x
     87 #define STATIC
     88 #else
     89 #define DPRINTF(x)
     90 #define STATIC static
     91 #endif
     92 
     93 int pccbb_burstup = 1;
     94 
     95 /*
     96  * delay_ms() is wait in milliseconds.  It should be used instead
     97  * of delay() if you want to wait more than 1 ms.
     98  */
     99 static inline void
    100 delay_ms(int millis, void *param)
    101 {
    102 	if (cold)
    103 		delay(millis * 1000);
    104 	else
    105 		tsleep(param, PWAIT, "pccbb", MAX(2, hz * millis / 1000));
    106 }
    107 
    108 int pcicbbmatch(device_t, struct cfdata *, void *);
    109 void pccbbattach(device_t, device_t, void *);
    110 int pccbbdetach(device_t, int);
    111 int pccbbintr(void *);
    112 static void pci113x_insert(void *);
    113 static int pccbbintr_function(struct pccbb_softc *);
    114 
    115 static int pccbb_detect_card(struct pccbb_softc *);
    116 
    117 static void pccbb_pcmcia_write(struct pccbb_softc *, int, u_int8_t);
    118 static u_int8_t pccbb_pcmcia_read(struct pccbb_softc *, int);
    119 #define Pcic_read(ph, reg) pccbb_pcmcia_read((ph)->ph_parent, (reg))
    120 #define Pcic_write(ph, reg, val) pccbb_pcmcia_write((ph)->ph_parent, \
    121 						    (reg), (val))
    122 
    123 STATIC int cb_reset(struct pccbb_softc *);
    124 STATIC int cb_detect_voltage(struct pccbb_softc *);
    125 STATIC int cbbprint(void *, const char *);
    126 
    127 static int cb_chipset(u_int32_t, int *);
    128 STATIC void pccbb_pcmcia_attach_setup(struct pccbb_softc *,
    129     struct pcmciabus_attach_args *);
    130 #if 0
    131 STATIC void pccbb_pcmcia_attach_card(struct pcic_handle *);
    132 STATIC void pccbb_pcmcia_detach_card(struct pcic_handle *, int);
    133 STATIC void pccbb_pcmcia_deactivate_card(struct pcic_handle *);
    134 #endif
    135 
    136 STATIC int pccbb_ctrl(cardbus_chipset_tag_t, int);
    137 STATIC int pccbb_power(struct pccbb_softc *sc, int);
    138 STATIC int pccbb_power_ct(cardbus_chipset_tag_t, int);
    139 STATIC int pccbb_cardenable(struct pccbb_softc * sc, int function);
    140 #if !rbus
    141 static int pccbb_io_open(cardbus_chipset_tag_t, int, u_int32_t, u_int32_t);
    142 static int pccbb_io_close(cardbus_chipset_tag_t, int);
    143 static int pccbb_mem_open(cardbus_chipset_tag_t, int, u_int32_t, u_int32_t);
    144 static int pccbb_mem_close(cardbus_chipset_tag_t, int);
    145 #endif /* !rbus */
    146 static void *pccbb_intr_establish(struct pccbb_softc *,
    147     cardbus_intr_line_t irq, int level, int (*ih) (void *), void *sc);
    148 static void pccbb_intr_disestablish(struct pccbb_softc *, void *ih);
    149 
    150 static void *pccbb_cb_intr_establish(cardbus_chipset_tag_t,
    151     cardbus_intr_line_t irq, int level, int (*ih) (void *), void *sc);
    152 static void pccbb_cb_intr_disestablish(cardbus_chipset_tag_t ct, void *ih);
    153 
    154 static cardbustag_t pccbb_make_tag(cardbus_chipset_tag_t, int, int);
    155 static void pccbb_free_tag(cardbus_chipset_tag_t, cardbustag_t);
    156 static cardbusreg_t pccbb_conf_read(cardbus_chipset_tag_t, cardbustag_t, int);
    157 static void pccbb_conf_write(cardbus_chipset_tag_t, cardbustag_t, int,
    158     cardbusreg_t);
    159 static void pccbb_chipinit(struct pccbb_softc *);
    160 static void pccbb_intrinit(struct pccbb_softc *);
    161 
    162 STATIC int pccbb_pcmcia_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
    163     struct pcmcia_mem_handle *);
    164 STATIC void pccbb_pcmcia_mem_free(pcmcia_chipset_handle_t,
    165     struct pcmcia_mem_handle *);
    166 STATIC int pccbb_pcmcia_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t,
    167     bus_size_t, struct pcmcia_mem_handle *, bus_addr_t *, int *);
    168 STATIC void pccbb_pcmcia_mem_unmap(pcmcia_chipset_handle_t, int);
    169 STATIC int pccbb_pcmcia_io_alloc(pcmcia_chipset_handle_t, bus_addr_t,
    170     bus_size_t, bus_size_t, struct pcmcia_io_handle *);
    171 STATIC void pccbb_pcmcia_io_free(pcmcia_chipset_handle_t,
    172     struct pcmcia_io_handle *);
    173 STATIC int pccbb_pcmcia_io_map(pcmcia_chipset_handle_t, int, bus_addr_t,
    174     bus_size_t, struct pcmcia_io_handle *, int *);
    175 STATIC void pccbb_pcmcia_io_unmap(pcmcia_chipset_handle_t, int);
    176 STATIC void *pccbb_pcmcia_intr_establish(pcmcia_chipset_handle_t,
    177     struct pcmcia_function *, int, int (*)(void *), void *);
    178 STATIC void pccbb_pcmcia_intr_disestablish(pcmcia_chipset_handle_t, void *);
    179 STATIC void pccbb_pcmcia_socket_enable(pcmcia_chipset_handle_t);
    180 STATIC void pccbb_pcmcia_socket_disable(pcmcia_chipset_handle_t);
    181 STATIC void pccbb_pcmcia_socket_settype(pcmcia_chipset_handle_t, int);
    182 STATIC int pccbb_pcmcia_card_detect(pcmcia_chipset_handle_t pch);
    183 
    184 static int pccbb_pcmcia_wait_ready(struct pcic_handle *);
    185 static void pccbb_pcmcia_delay(struct pcic_handle *, int, const char *);
    186 
    187 static void pccbb_pcmcia_do_io_map(struct pcic_handle *, int);
    188 static void pccbb_pcmcia_do_mem_map(struct pcic_handle *, int);
    189 
    190 /* bus-space allocation and deallocation functions */
    191 #if rbus
    192 
    193 static int pccbb_rbus_cb_space_alloc(cardbus_chipset_tag_t, rbus_tag_t,
    194     bus_addr_t addr, bus_size_t size, bus_addr_t mask, bus_size_t align,
    195     int flags, bus_addr_t * addrp, bus_space_handle_t * bshp);
    196 static int pccbb_rbus_cb_space_free(cardbus_chipset_tag_t, rbus_tag_t,
    197     bus_space_handle_t, bus_size_t);
    198 
    199 #endif /* rbus */
    200 
    201 #if rbus
    202 
    203 static int pccbb_open_win(struct pccbb_softc *, bus_space_tag_t,
    204     bus_addr_t, bus_size_t, bus_space_handle_t, int flags);
    205 static int pccbb_close_win(struct pccbb_softc *, bus_space_tag_t,
    206     bus_space_handle_t, bus_size_t);
    207 static int pccbb_winlist_insert(struct pccbb_win_chain_head *, bus_addr_t,
    208     bus_size_t, bus_space_handle_t, int);
    209 static int pccbb_winlist_delete(struct pccbb_win_chain_head *,
    210     bus_space_handle_t, bus_size_t);
    211 static void pccbb_winset(bus_addr_t align, struct pccbb_softc *,
    212     bus_space_tag_t);
    213 void pccbb_winlist_show(struct pccbb_win_chain *);
    214 
    215 #endif /* rbus */
    216 
    217 /* for config_defer */
    218 static void pccbb_pci_callback(device_t);
    219 
    220 static bool pccbb_suspend(device_t PMF_FN_PROTO);
    221 static bool pccbb_resume(device_t PMF_FN_PROTO);
    222 
    223 #if defined SHOW_REGS
    224 static void cb_show_regs(pci_chipset_tag_t pc, pcitag_t tag,
    225     bus_space_tag_t memt, bus_space_handle_t memh);
    226 #endif
    227 
    228 CFATTACH_DECL_NEW(cbb_pci, sizeof(struct pccbb_softc),
    229     pcicbbmatch, pccbbattach, pccbbdetach, NULL);
    230 
    231 static struct pcmcia_chip_functions pccbb_pcmcia_funcs = {
    232 	pccbb_pcmcia_mem_alloc,
    233 	pccbb_pcmcia_mem_free,
    234 	pccbb_pcmcia_mem_map,
    235 	pccbb_pcmcia_mem_unmap,
    236 	pccbb_pcmcia_io_alloc,
    237 	pccbb_pcmcia_io_free,
    238 	pccbb_pcmcia_io_map,
    239 	pccbb_pcmcia_io_unmap,
    240 	pccbb_pcmcia_intr_establish,
    241 	pccbb_pcmcia_intr_disestablish,
    242 	pccbb_pcmcia_socket_enable,
    243 	pccbb_pcmcia_socket_disable,
    244 	pccbb_pcmcia_socket_settype,
    245 	pccbb_pcmcia_card_detect
    246 };
    247 
    248 #if rbus
    249 static struct cardbus_functions pccbb_funcs = {
    250 	pccbb_rbus_cb_space_alloc,
    251 	pccbb_rbus_cb_space_free,
    252 	pccbb_cb_intr_establish,
    253 	pccbb_cb_intr_disestablish,
    254 	pccbb_ctrl,
    255 	pccbb_power_ct,
    256 	pccbb_make_tag,
    257 	pccbb_free_tag,
    258 	pccbb_conf_read,
    259 	pccbb_conf_write,
    260 };
    261 #else
    262 static struct cardbus_functions pccbb_funcs = {
    263 	pccbb_ctrl,
    264 	pccbb_power_ct,
    265 	pccbb_mem_open,
    266 	pccbb_mem_close,
    267 	pccbb_io_open,
    268 	pccbb_io_close,
    269 	pccbb_cb_intr_establish,
    270 	pccbb_cb_intr_disestablish,
    271 	pccbb_make_tag,
    272 	pccbb_conf_read,
    273 	pccbb_conf_write,
    274 };
    275 #endif
    276 
    277 int
    278 pcicbbmatch(device_t parent, struct cfdata *match, void *aux)
    279 {
    280 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    281 
    282 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
    283 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_CARDBUS &&
    284 	    PCI_INTERFACE(pa->pa_class) == 0) {
    285 		return 1;
    286 	}
    287 
    288 	return 0;
    289 }
    290 
    291 #define MAKEID(vendor, prod) (((vendor) << PCI_VENDOR_SHIFT) \
    292                               | ((prod) << PCI_PRODUCT_SHIFT))
    293 
    294 const struct yenta_chipinfo {
    295 	pcireg_t yc_id;		       /* vendor tag | product tag */
    296 	int yc_chiptype;
    297 	int yc_flags;
    298 } yc_chipsets[] = {
    299 	/* Texas Instruments chips */
    300 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1130), CB_TI113X,
    301 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    302 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1131), CB_TI113X,
    303 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    304 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1250), CB_TI125X,
    305 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    306 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1220), CB_TI12XX,
    307 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    308 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1221), CB_TI12XX,
    309 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    310 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1225), CB_TI12XX,
    311 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    312 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1251), CB_TI125X,
    313 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    314 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1251B), CB_TI125X,
    315 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    316 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1211), CB_TI12XX,
    317 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    318 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1410), CB_TI12XX,
    319 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    320 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1420), CB_TI1420,
    321 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    322 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1450), CB_TI125X,
    323 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    324 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1451), CB_TI12XX,
    325 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    326 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI1520), CB_TI12XX,
    327 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    328 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI4410YENTA), CB_TI12XX,
    329 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    330 	{ MAKEID(PCI_VENDOR_TI, PCI_PRODUCT_TI_PCI4520YENTA), CB_TI12XX,
    331 	    PCCBB_PCMCIA_IO_RELOC | PCCBB_PCMCIA_MEM_32},
    332 
    333 	/* Ricoh chips */
    334 	{ MAKEID(PCI_VENDOR_RICOH, PCI_PRODUCT_RICOH_Rx5C475), CB_RX5C47X,
    335 	    PCCBB_PCMCIA_MEM_32},
    336 	{ MAKEID(PCI_VENDOR_RICOH, PCI_PRODUCT_RICOH_RL5C476), CB_RX5C47X,
    337 	    PCCBB_PCMCIA_MEM_32},
    338 	{ MAKEID(PCI_VENDOR_RICOH, PCI_PRODUCT_RICOH_Rx5C477), CB_RX5C47X,
    339 	    PCCBB_PCMCIA_MEM_32},
    340 	{ MAKEID(PCI_VENDOR_RICOH, PCI_PRODUCT_RICOH_Rx5C478), CB_RX5C47X,
    341 	    PCCBB_PCMCIA_MEM_32},
    342 	{ MAKEID(PCI_VENDOR_RICOH, PCI_PRODUCT_RICOH_Rx5C465), CB_RX5C46X,
    343 	    PCCBB_PCMCIA_MEM_32},
    344 	{ MAKEID(PCI_VENDOR_RICOH, PCI_PRODUCT_RICOH_Rx5C466), CB_RX5C46X,
    345 	    PCCBB_PCMCIA_MEM_32},
    346 
    347 	/* Toshiba products */
    348 	{ MAKEID(PCI_VENDOR_TOSHIBA2, PCI_PRODUCT_TOSHIBA2_ToPIC95),
    349 	    CB_TOPIC95, PCCBB_PCMCIA_MEM_32},
    350 	{ MAKEID(PCI_VENDOR_TOSHIBA2, PCI_PRODUCT_TOSHIBA2_ToPIC95B),
    351 	    CB_TOPIC95B, PCCBB_PCMCIA_MEM_32},
    352 	{ MAKEID(PCI_VENDOR_TOSHIBA2, PCI_PRODUCT_TOSHIBA2_ToPIC97),
    353 	    CB_TOPIC97, PCCBB_PCMCIA_MEM_32},
    354 	{ MAKEID(PCI_VENDOR_TOSHIBA2, PCI_PRODUCT_TOSHIBA2_ToPIC100),
    355 	    CB_TOPIC97, PCCBB_PCMCIA_MEM_32},
    356 
    357 	/* Cirrus Logic products */
    358 	{ MAKEID(PCI_VENDOR_CIRRUS, PCI_PRODUCT_CIRRUS_CL_PD6832),
    359 	    CB_CIRRUS, PCCBB_PCMCIA_MEM_32},
    360 	{ MAKEID(PCI_VENDOR_CIRRUS, PCI_PRODUCT_CIRRUS_CL_PD6833),
    361 	    CB_CIRRUS, PCCBB_PCMCIA_MEM_32},
    362 
    363 	/* O2 Micro products */
    364 	{ MAKEID(PCI_VENDOR_O2MICRO, PCI_PRODUCT_O2MICRO_OZ6729),
    365 	  CB_O2MICRO, PCCBB_PCMCIA_MEM_32},
    366 	{ MAKEID(PCI_VENDOR_O2MICRO, PCI_PRODUCT_O2MICRO_OZ6730),
    367 	  CB_O2MICRO, PCCBB_PCMCIA_MEM_32},
    368 	{ MAKEID(PCI_VENDOR_O2MICRO, PCI_PRODUCT_O2MICRO_OZ6832),
    369 	  CB_O2MICRO, PCCBB_PCMCIA_MEM_32},
    370 	{ MAKEID(PCI_VENDOR_O2MICRO, PCI_PRODUCT_O2MICRO_OZ6836),
    371 	  CB_O2MICRO, PCCBB_PCMCIA_MEM_32},
    372 	{ MAKEID(PCI_VENDOR_O2MICRO, PCI_PRODUCT_O2MICRO_OZ6872),
    373 	  CB_O2MICRO, PCCBB_PCMCIA_MEM_32},
    374 	{ MAKEID(PCI_VENDOR_O2MICRO, PCI_PRODUCT_O2MICRO_OZ6922),
    375 	  CB_O2MICRO, PCCBB_PCMCIA_MEM_32},
    376 	{ MAKEID(PCI_VENDOR_O2MICRO, PCI_PRODUCT_O2MICRO_OZ6933),
    377 	  CB_O2MICRO, PCCBB_PCMCIA_MEM_32},
    378 	{ MAKEID(PCI_VENDOR_O2MICRO, PCI_PRODUCT_O2MICRO_OZ6972),
    379 	  CB_O2MICRO, PCCBB_PCMCIA_MEM_32},
    380 
    381 	/* sentinel, or Generic chip */
    382 	{ 0 /* null id */ , CB_UNKNOWN, PCCBB_PCMCIA_MEM_32},
    383 };
    384 
    385 static int
    386 cb_chipset(u_int32_t pci_id, int *flagp)
    387 {
    388 	const struct yenta_chipinfo *yc;
    389 
    390 	/* Loop over except the last default entry. */
    391 	for (yc = yc_chipsets; yc < yc_chipsets +
    392 	    __arraycount(yc_chipsets) - 1; yc++)
    393 		if (pci_id == yc->yc_id)
    394 			break;
    395 
    396 	if (flagp != NULL)
    397 		*flagp = yc->yc_flags;
    398 
    399 	return (yc->yc_chiptype);
    400 }
    401 
    402 void
    403 pccbbattach(device_t parent, device_t self, void *aux)
    404 {
    405 	struct pccbb_softc *sc = device_private(self);
    406 	struct pci_attach_args *pa = aux;
    407 	pci_chipset_tag_t pc = pa->pa_pc;
    408 	pcireg_t busreg, reg, sock_base;
    409 	bus_addr_t sockbase;
    410 	char devinfo[256];
    411 	int flags;
    412 
    413 #ifdef __HAVE_PCCBB_ATTACH_HOOK
    414 	pccbb_attach_hook(parent, self, pa);
    415 #endif
    416 
    417 	sc->sc_dev = self;
    418 
    419 	callout_init(&sc->sc_insert_ch, 0);
    420 	callout_setfunc(&sc->sc_insert_ch, pci113x_insert, sc);
    421 
    422 	sc->sc_chipset = cb_chipset(pa->pa_id, &flags);
    423 
    424 	aprint_naive("\n");
    425 
    426 	pci_devinfo(pa->pa_id, 0, 0, devinfo, sizeof(devinfo));
    427 	aprint_normal(": %s (rev. 0x%02x)", devinfo,
    428 	    PCI_REVISION(pa->pa_class));
    429 	DPRINTF((" (chipflags %x)", flags));
    430 	aprint_normal("\n");
    431 
    432 	TAILQ_INIT(&sc->sc_memwindow);
    433 	TAILQ_INIT(&sc->sc_iowindow);
    434 
    435 #if rbus
    436 	sc->sc_rbus_iot = rbus_pccbb_parent_io(pa);
    437 	sc->sc_rbus_memt = rbus_pccbb_parent_mem(pa);
    438 
    439 #if 0
    440 	printf("pa->pa_memt: %08x vs rbus_mem->rb_bt: %08x\n",
    441 	       pa->pa_memt, sc->sc_rbus_memt->rb_bt);
    442 #endif
    443 #endif /* rbus */
    444 
    445 	sc->sc_flags &= ~CBB_MEMHMAPPED;
    446 
    447 	/*
    448 	 * MAP socket registers and ExCA registers on memory-space
    449 	 * When no valid address is set on socket base registers (on pci
    450 	 * config space), get it not polite way.
    451 	 */
    452 	sock_base = pci_conf_read(pc, pa->pa_tag, PCI_SOCKBASE);
    453 
    454 	if (PCI_MAPREG_MEM_ADDR(sock_base) >= 0x100000 &&
    455 	    PCI_MAPREG_MEM_ADDR(sock_base) != 0xfffffff0) {
    456 		/* The address must be valid. */
    457 		if (pci_mapreg_map(pa, PCI_SOCKBASE, PCI_MAPREG_TYPE_MEM, 0,
    458 		    &sc->sc_base_memt, &sc->sc_base_memh, &sockbase, &sc->sc_base_size)) {
    459 			aprint_error_dev(self,
    460 			    "can't map socket base address 0x%lx\n",
    461 			    (unsigned long)sock_base);
    462 			/*
    463 			 * I think it's funny: socket base registers must be
    464 			 * mapped on memory space, but ...
    465 			 */
    466 			if (pci_mapreg_map(pa, PCI_SOCKBASE, PCI_MAPREG_TYPE_IO,
    467 			    0, &sc->sc_base_memt, &sc->sc_base_memh, &sockbase,
    468 			    &sc->sc_base_size)) {
    469 				aprint_error_dev(self,
    470 				    "can't map socket base address"
    471 				    " 0x%lx: io mode\n",
    472 				    (unsigned long)sockbase);
    473 				/* give up... allocate reg space via rbus. */
    474 				pci_conf_write(pc, pa->pa_tag, PCI_SOCKBASE, 0);
    475 			} else
    476 				sc->sc_flags |= CBB_MEMHMAPPED;
    477 		} else {
    478 			DPRINTF(("%s: socket base address 0x%lx\n",
    479 			    device_xname(self),
    480 			    (unsigned long)sockbase));
    481 			sc->sc_flags |= CBB_MEMHMAPPED;
    482 		}
    483 	}
    484 
    485 	sc->sc_mem_start = 0;	       /* XXX */
    486 	sc->sc_mem_end = 0xffffffff;   /* XXX */
    487 
    488 	busreg = pci_conf_read(pc, pa->pa_tag, PCI_BUSNUM);
    489 
    490 	/* pccbb_machdep.c end */
    491 
    492 #if defined CBB_DEBUG
    493 	{
    494 		static const char *intrname[] = { "NON", "A", "B", "C", "D" };
    495 		aprint_debug_dev(self, "intrpin %s, intrtag %d\n",
    496 		    intrname[pa->pa_intrpin], pa->pa_intrline);
    497 	}
    498 #endif
    499 
    500 	/* setup softc */
    501 	sc->sc_pc = pc;
    502 	sc->sc_iot = pa->pa_iot;
    503 	sc->sc_memt = pa->pa_memt;
    504 	sc->sc_dmat = pa->pa_dmat;
    505 	sc->sc_tag = pa->pa_tag;
    506 	sc->sc_function = pa->pa_function;
    507 
    508 	memcpy(&sc->sc_pa, pa, sizeof(*pa));
    509 
    510 	sc->sc_pcmcia_flags = flags;   /* set PCMCIA facility */
    511 
    512 	/* Disable legacy register mapping. */
    513 	switch (sc->sc_chipset) {
    514 	case CB_RX5C46X:	       /* fallthrough */
    515 #if 0
    516 	/* The RX5C47X-series requires writes to the PCI_LEGACY register. */
    517 	case CB_RX5C47X:
    518 #endif
    519 		/*
    520 		 * The legacy pcic io-port on Ricoh RX5C46X CardBus bridges
    521 		 * cannot be disabled by substituting 0 into PCI_LEGACY
    522 		 * register.  Ricoh CardBus bridges have special bits on Bridge
    523 		 * control reg (addr 0x3e on PCI config space).
    524 		 */
    525 		reg = pci_conf_read(pc, pa->pa_tag, PCI_BRIDGE_CONTROL_REG);
    526 		reg &= ~(CB_BCRI_RL_3E0_ENA | CB_BCRI_RL_3E2_ENA);
    527 		pci_conf_write(pc, pa->pa_tag, PCI_BRIDGE_CONTROL_REG, reg);
    528 		break;
    529 
    530 	default:
    531 		/* XXX I don't know proper way to kill legacy I/O. */
    532 		pci_conf_write(pc, pa->pa_tag, PCI_LEGACY, 0x0);
    533 		break;
    534 	}
    535 
    536 	if (!pmf_device_register(self, pccbb_suspend, pccbb_resume))
    537 		aprint_error_dev(self, "couldn't establish power handler\n");
    538 
    539 	config_defer(self, pccbb_pci_callback);
    540 }
    541 
    542 int
    543 pccbbdetach(device_t self, int flags)
    544 {
    545 	struct pccbb_softc *sc = device_private(self);
    546 	pci_chipset_tag_t pc = sc->sc_pa.pa_pc;
    547 	bus_space_tag_t bmt = sc->sc_base_memt;
    548 	bus_space_handle_t bmh = sc->sc_base_memh;
    549 	uint32_t sockmask;
    550 	int rc;
    551 
    552 	if ((rc = config_detach_children(self, flags)) != 0)
    553 		return rc;
    554 
    555 	if (!LIST_EMPTY(&sc->sc_pil)) {
    556 		panic("%s: interrupt handlers still registered",
    557 		    device_xname(self));
    558 		return EBUSY;
    559 	}
    560 
    561 	if (sc->sc_ih != NULL) {
    562 		pci_intr_disestablish(pc, sc->sc_ih);
    563 		sc->sc_ih = NULL;
    564 	}
    565 
    566 	/* CSC Interrupt: turn off card detect and power cycle interrupts */
    567 	sockmask = bus_space_read_4(bmt, bmh, CB_SOCKET_MASK);
    568 	sockmask &= ~(CB_SOCKET_MASK_CSTS | CB_SOCKET_MASK_CD |
    569 		      CB_SOCKET_MASK_POWER);
    570 	bus_space_write_4(bmt, bmh, CB_SOCKET_MASK, sockmask);
    571 	/* reset interrupt */
    572 	bus_space_write_4(bmt, bmh, CB_SOCKET_EVENT,
    573 	    bus_space_read_4(bmt, bmh, CB_SOCKET_EVENT));
    574 
    575 	switch (sc->sc_flags & (CBB_MEMHMAPPED|CBB_SPECMAPPED)) {
    576 	case CBB_MEMHMAPPED:
    577 		bus_space_unmap(bmt, bmh, sc->sc_base_size);
    578 		break;
    579 	case CBB_MEMHMAPPED|CBB_SPECMAPPED:
    580 #if rbus
    581 	{
    582 		pcireg_t sockbase;
    583 
    584 		sockbase = pci_conf_read(pc, sc->sc_tag, PCI_SOCKBASE);
    585 		rbus_space_free(sc->sc_rbus_memt, bmh, 0x1000,
    586 		    NULL);
    587 	}
    588 #else
    589 		bus_space_free(bmt, bmh, 0x1000);
    590 #endif
    591 	}
    592 	sc->sc_flags &= ~(CBB_MEMHMAPPED|CBB_SPECMAPPED);
    593 
    594 	if (!TAILQ_EMPTY(&sc->sc_iowindow))
    595 		aprint_error_dev(self, "i/o windows not empty");
    596 	if (!TAILQ_EMPTY(&sc->sc_memwindow))
    597 		aprint_error_dev(self, "memory windows not empty");
    598 
    599 	callout_stop(&sc->sc_insert_ch);
    600 	callout_destroy(&sc->sc_insert_ch);
    601 	return 0;
    602 }
    603 
    604 /*
    605  * static void pccbb_pci_callback(device_t self)
    606  *
    607  *   The actual attach routine: get memory space for YENTA register
    608  *   space, setup YENTA register and route interrupt.
    609  *
    610  *   This function should be deferred because this device may obtain
    611  *   memory space dynamically.  This function must avoid obtaining
    612  *   memory area which has already kept for another device.
    613  */
    614 static void
    615 pccbb_pci_callback(device_t self)
    616 {
    617 	struct pccbb_softc *sc = device_private(self);
    618 	pci_chipset_tag_t pc = sc->sc_pc;
    619 	bus_addr_t sockbase;
    620 	struct cbslot_attach_args cba;
    621 	struct pcmciabus_attach_args paa;
    622 	struct cardslot_attach_args caa;
    623 	device_t csc;
    624 
    625 	if (!(sc->sc_flags & CBB_MEMHMAPPED)) {
    626 		/* The socket registers aren't mapped correctly. */
    627 #if rbus
    628 		if (rbus_space_alloc(sc->sc_rbus_memt, 0, 0x1000, 0x0fff,
    629 		    (sc->sc_chipset == CB_RX5C47X
    630 		    || sc->sc_chipset == CB_TI113X) ? 0x10000 : 0x1000,
    631 		    0, &sockbase, &sc->sc_base_memh)) {
    632 			return;
    633 		}
    634 		sc->sc_base_memt = sc->sc_memt;
    635 		pci_conf_write(pc, sc->sc_tag, PCI_SOCKBASE, sockbase);
    636 		DPRINTF(("%s: CardBus register address 0x%lx -> 0x%lx\n",
    637 		    device_xname(self), (unsigned long)sockbase,
    638 		    (unsigned long)pci_conf_read(pc, sc->sc_tag,
    639 		    PCI_SOCKBASE)));
    640 #else
    641 		sc->sc_base_memt = sc->sc_memt;
    642 #if !defined CBB_PCI_BASE
    643 #define CBB_PCI_BASE 0x20000000
    644 #endif
    645 		if (bus_space_alloc(sc->sc_base_memt, CBB_PCI_BASE, 0xffffffff,
    646 		    0x1000, 0x1000, 0, 0, &sockbase, &sc->sc_base_memh)) {
    647 			/* cannot allocate memory space */
    648 			return;
    649 		}
    650 		pci_conf_write(pc, sc->sc_tag, PCI_SOCKBASE, sockbase);
    651 		DPRINTF(("%s: CardBus register address 0x%lx -> 0x%lx\n",
    652 		    device_xname(self), (unsigned long)sock_base,
    653 		    (unsigned long)pci_conf_read(pc,
    654 		    sc->sc_tag, PCI_SOCKBASE)));
    655 #endif
    656 		sc->sc_flags |= CBB_MEMHMAPPED;
    657 	}
    658 
    659 	/* clear data structure for child device interrupt handlers */
    660 	LIST_INIT(&sc->sc_pil);
    661 
    662 	/* bus bridge initialization */
    663 	pccbb_chipinit(sc);
    664 
    665 	sc->sc_pil_intr_enable = 1;
    666 
    667 	{
    668 		u_int32_t sockstat;
    669 
    670 		sockstat = bus_space_read_4(sc->sc_base_memt,
    671 		    sc->sc_base_memh, CB_SOCKET_STAT);
    672 		if (0 == (sockstat & CB_SOCKET_STAT_CD)) {
    673 			sc->sc_flags |= CBB_CARDEXIST;
    674 		}
    675 	}
    676 
    677 	/*
    678 	 * attach cardbus
    679 	 */
    680 	{
    681 		pcireg_t busreg = pci_conf_read(pc, sc->sc_tag, PCI_BUSNUM);
    682 		pcireg_t bhlc = pci_conf_read(pc, sc->sc_tag, PCI_BHLC_REG);
    683 
    684 		/* initialize cbslot_attach */
    685 		cba.cba_iot = sc->sc_iot;
    686 		cba.cba_memt = sc->sc_memt;
    687 		cba.cba_dmat = sc->sc_dmat;
    688 		cba.cba_bus = (busreg >> 8) & 0x0ff;
    689 		cba.cba_cc = (void *)sc;
    690 		cba.cba_cf = &pccbb_funcs;
    691 		cba.cba_intrline = 0; /* XXX dummy */
    692 
    693 #if rbus
    694 		cba.cba_rbus_iot = sc->sc_rbus_iot;
    695 		cba.cba_rbus_memt = sc->sc_rbus_memt;
    696 #endif
    697 
    698 		cba.cba_cacheline = PCI_CACHELINE(bhlc);
    699 		cba.cba_max_lattimer = PCI_LATTIMER(bhlc);
    700 
    701 		aprint_verbose_dev(self,
    702 		    "cacheline 0x%x lattimer 0x%x\n",
    703 		    cba.cba_cacheline,
    704 		    cba.cba_max_lattimer);
    705 		aprint_verbose_dev(self, "bhlc 0x%x\n", bhlc);
    706 #if defined SHOW_REGS
    707 		cb_show_regs(sc->sc_pc, sc->sc_tag, sc->sc_base_memt,
    708 		    sc->sc_base_memh);
    709 #endif
    710 	}
    711 
    712 	pccbb_pcmcia_attach_setup(sc, &paa);
    713 	caa.caa_cb_attach = NULL;
    714 	if (cba.cba_bus == 0)
    715 		aprint_error_dev(self,
    716 		    "secondary bus number uninitialized; try PCI_BUS_FIXUP\n");
    717 	else
    718 		caa.caa_cb_attach = &cba;
    719 	caa.caa_16_attach = &paa;
    720 
    721 	pccbb_intrinit(sc);
    722 
    723 	if (NULL != (csc = config_found_ia(self, "pcmciaslot", &caa,
    724 					   cbbprint))) {
    725 		DPRINTF(("%s: found cardslot\n", __func__));
    726 		sc->sc_csc = device_private(csc);
    727 	}
    728 
    729 	return;
    730 }
    731 
    732 
    733 
    734 
    735 
    736 /*
    737  * static void pccbb_chipinit(struct pccbb_softc *sc)
    738  *
    739  *   This function initialize YENTA chip registers listed below:
    740  *     1) PCI command reg,
    741  *     2) PCI and CardBus latency timer,
    742  *     3) route PCI interrupt,
    743  *     4) close all memory and io windows.
    744  *     5) turn off bus power.
    745  *     6) card detect and power cycle interrupts on.
    746  *     7) clear interrupt
    747  */
    748 static void
    749 pccbb_chipinit(struct pccbb_softc *sc)
    750 {
    751 	pci_chipset_tag_t pc = sc->sc_pc;
    752 	pcitag_t tag = sc->sc_tag;
    753 	bus_space_tag_t bmt = sc->sc_base_memt;
    754 	bus_space_handle_t bmh = sc->sc_base_memh;
    755 	pcireg_t bcr, bhlc, cbctl, csr, lscp, mfunc, mrburst, slotctl, sockctl,
    756 	    sysctrl;
    757 
    758 	/*
    759 	 * Set PCI command reg.
    760 	 * Some laptop's BIOSes (i.e. TICO) do not enable CardBus chip.
    761 	 */
    762 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    763 	/* I believe it is harmless. */
    764 	csr |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
    765 	    PCI_COMMAND_MASTER_ENABLE);
    766 
    767 	/* All O2 Micro chips have broken parity-error reporting
    768 	 * until proven otherwise.  The OZ6933 PCI-CardBus Bridge
    769 	 * is known to have the defect---see PR kern/38698.
    770 	 */
    771 	if (sc->sc_chipset != CB_O2MICRO)
    772 		csr |= PCI_COMMAND_PARITY_ENABLE;
    773 
    774 	csr |= PCI_COMMAND_SERR_ENABLE;
    775 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    776 
    777 	/*
    778 	 * Set CardBus latency timer.
    779 	 */
    780 	lscp = pci_conf_read(pc, tag, PCI_CB_LSCP_REG);
    781 	if (PCI_CB_LATENCY(lscp) < 0x20) {
    782 		lscp &= ~(PCI_CB_LATENCY_MASK << PCI_CB_LATENCY_SHIFT);
    783 		lscp |= (0x20 << PCI_CB_LATENCY_SHIFT);
    784 		pci_conf_write(pc, tag, PCI_CB_LSCP_REG, lscp);
    785 	}
    786 	DPRINTF(("CardBus latency timer 0x%x (%x)\n",
    787 	    PCI_CB_LATENCY(lscp), pci_conf_read(pc, tag, PCI_CB_LSCP_REG)));
    788 
    789 	/*
    790 	 * Set PCI latency timer.
    791 	 */
    792 	bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG);
    793 	if (PCI_LATTIMER(bhlc) < 0x10) {
    794 		bhlc &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    795 		bhlc |= (0x10 << PCI_LATTIMER_SHIFT);
    796 		pci_conf_write(pc, tag, PCI_BHLC_REG, bhlc);
    797 	}
    798 	DPRINTF(("PCI latency timer 0x%x (%x)\n",
    799 	    PCI_LATTIMER(bhlc), pci_conf_read(pc, tag, PCI_BHLC_REG)));
    800 
    801 
    802 	/* Route functional interrupts to PCI. */
    803 	bcr = pci_conf_read(pc, tag, PCI_BRIDGE_CONTROL_REG);
    804 	bcr |= CB_BCR_INTR_IREQ_ENABLE;		/* disable PCI Intr */
    805 	bcr |= CB_BCR_WRITE_POST_ENABLE;	/* enable write post */
    806 	/* assert reset */
    807 	bcr |= PCI_BRIDGE_CONTROL_SECBR	<< PCI_BRIDGE_CONTROL_SHIFT;
    808         /* Set master abort mode to 1, forward SERR# from secondary
    809          * to primary, and detect parity errors on secondary.
    810 	 */
    811 	bcr |= PCI_BRIDGE_CONTROL_MABRT	<< PCI_BRIDGE_CONTROL_SHIFT;
    812 	bcr |= PCI_BRIDGE_CONTROL_SERR << PCI_BRIDGE_CONTROL_SHIFT;
    813 	bcr |= PCI_BRIDGE_CONTROL_PERE << PCI_BRIDGE_CONTROL_SHIFT;
    814 	pci_conf_write(pc, tag, PCI_BRIDGE_CONTROL_REG, bcr);
    815 
    816 	switch (sc->sc_chipset) {
    817 	case CB_TI113X:
    818 		cbctl = pci_conf_read(pc, tag, PCI_CBCTRL);
    819 		/* This bit is shared, but may read as 0 on some chips, so set
    820 		   it explicitly on both functions. */
    821 		cbctl |= PCI113X_CBCTRL_PCI_IRQ_ENA;
    822 		/* CSC intr enable */
    823 		cbctl |= PCI113X_CBCTRL_PCI_CSC;
    824 		/* functional intr prohibit | prohibit ISA routing */
    825 		cbctl &= ~(PCI113X_CBCTRL_PCI_INTR | PCI113X_CBCTRL_INT_MASK);
    826 		pci_conf_write(pc, tag, PCI_CBCTRL, cbctl);
    827 		break;
    828 
    829 	case CB_TI1420:
    830 		sysctrl = pci_conf_read(pc, tag, PCI_SYSCTRL);
    831 		mrburst = pccbb_burstup
    832 		    ? PCI1420_SYSCTRL_MRBURST : PCI1420_SYSCTRL_MRBURSTDN;
    833 		if ((sysctrl & PCI1420_SYSCTRL_MRBURST) == mrburst) {
    834 			printf("%s: %swrite bursts enabled\n",
    835 			    device_xname(sc->sc_dev),
    836 			    pccbb_burstup ? "read/" : "");
    837 		} else if (pccbb_burstup) {
    838 			printf("%s: enabling read/write bursts\n",
    839 			    device_xname(sc->sc_dev));
    840 			sysctrl |= PCI1420_SYSCTRL_MRBURST;
    841 			pci_conf_write(pc, tag, PCI_SYSCTRL, sysctrl);
    842 		} else {
    843 			printf("%s: disabling read bursts, "
    844 			    "enabling write bursts\n",
    845 			    device_xname(sc->sc_dev));
    846 			sysctrl |= PCI1420_SYSCTRL_MRBURSTDN;
    847 			sysctrl &= ~PCI1420_SYSCTRL_MRBURSTUP;
    848 			pci_conf_write(pc, tag, PCI_SYSCTRL, sysctrl);
    849 		}
    850 		/*FALLTHROUGH*/
    851 	case CB_TI12XX:
    852 		/*
    853 		 * Some TI 12xx (and [14][45]xx) based pci cards
    854 		 * sometimes have issues with the MFUNC register not
    855 		 * being initialized due to a bad EEPROM on board.
    856 		 * Laptops that this matters on have this register
    857 		 * properly initialized.
    858 		 *
    859 		 * The TI125X parts have a different register.
    860 		 */
    861 		mfunc = pci_conf_read(pc, tag, PCI12XX_MFUNC);
    862 		if (mfunc == 0) {
    863 			mfunc &= ~PCI12XX_MFUNC_PIN0;
    864 			mfunc |= PCI12XX_MFUNC_PIN0_INTA;
    865 			if ((pci_conf_read(pc, tag, PCI_SYSCTRL) &
    866 			     PCI12XX_SYSCTRL_INTRTIE) == 0) {
    867 				mfunc &= ~PCI12XX_MFUNC_PIN1;
    868 				mfunc |= PCI12XX_MFUNC_PIN1_INTB;
    869 			}
    870 			pci_conf_write(pc, tag, PCI12XX_MFUNC, mfunc);
    871 		}
    872 		/* fallthrough */
    873 
    874 	case CB_TI125X:
    875 		/*
    876 		 * Disable zoom video.  Some machines initialize this
    877 		 * improperly and experience has shown that this helps
    878 		 * prevent strange behavior.
    879 		 */
    880 		pci_conf_write(pc, tag, PCI12XX_MMCTRL, 0);
    881 
    882 		sysctrl = pci_conf_read(pc, tag, PCI_SYSCTRL);
    883 		sysctrl |= PCI12XX_SYSCTRL_VCCPROT;
    884 		pci_conf_write(pc, tag, PCI_SYSCTRL, sysctrl);
    885 		cbctl = pci_conf_read(pc, tag, PCI_CBCTRL);
    886 		cbctl |= PCI12XX_CBCTRL_CSC;
    887 		pci_conf_write(pc, tag, PCI_CBCTRL, cbctl);
    888 		break;
    889 
    890 	case CB_TOPIC95B:
    891 		sockctl = pci_conf_read(pc, tag, TOPIC_SOCKET_CTRL);
    892 		sockctl |= TOPIC_SOCKET_CTRL_SCR_IRQSEL;
    893 		pci_conf_write(pc, tag, TOPIC_SOCKET_CTRL, sockctl);
    894 		slotctl = pci_conf_read(pc, tag, TOPIC_SLOT_CTRL);
    895 		DPRINTF(("%s: topic slot ctrl reg 0x%x -> ",
    896 		    device_xname(sc->sc_dev), slotctl));
    897 		slotctl |= (TOPIC_SLOT_CTRL_SLOTON | TOPIC_SLOT_CTRL_SLOTEN |
    898 		    TOPIC_SLOT_CTRL_ID_LOCK | TOPIC_SLOT_CTRL_CARDBUS);
    899 		slotctl &= ~TOPIC_SLOT_CTRL_SWDETECT;
    900 		DPRINTF(("0x%x\n", slotctl));
    901 		pci_conf_write(pc, tag, TOPIC_SLOT_CTRL, slotctl);
    902 		break;
    903 
    904 	case CB_TOPIC97:
    905 		slotctl = pci_conf_read(pc, tag, TOPIC_SLOT_CTRL);
    906 		DPRINTF(("%s: topic slot ctrl reg 0x%x -> ",
    907 		    device_xname(sc->sc_dev), slotctl));
    908 		slotctl |= (TOPIC_SLOT_CTRL_SLOTON | TOPIC_SLOT_CTRL_SLOTEN |
    909 		    TOPIC_SLOT_CTRL_ID_LOCK | TOPIC_SLOT_CTRL_CARDBUS);
    910 		slotctl &= ~TOPIC_SLOT_CTRL_SWDETECT;
    911 		slotctl |= TOPIC97_SLOT_CTRL_PCIINT;
    912 		slotctl &= ~(TOPIC97_SLOT_CTRL_STSIRQP | TOPIC97_SLOT_CTRL_IRQP);
    913 		DPRINTF(("0x%x\n", slotctl));
    914 		pci_conf_write(pc, tag, TOPIC_SLOT_CTRL, slotctl);
    915 		/* make sure to assert LV card support bits */
    916 		bus_space_write_1(sc->sc_base_memt, sc->sc_base_memh,
    917 		    0x800 + 0x3e,
    918 		    bus_space_read_1(sc->sc_base_memt, sc->sc_base_memh,
    919 			0x800 + 0x3e) | 0x03);
    920 		break;
    921 	}
    922 
    923 	/* Close all memory and I/O windows. */
    924 	pci_conf_write(pc, tag, PCI_CB_MEMBASE0, 0xffffffff);
    925 	pci_conf_write(pc, tag, PCI_CB_MEMLIMIT0, 0);
    926 	pci_conf_write(pc, tag, PCI_CB_MEMBASE1, 0xffffffff);
    927 	pci_conf_write(pc, tag, PCI_CB_MEMLIMIT1, 0);
    928 	pci_conf_write(pc, tag, PCI_CB_IOBASE0, 0xffffffff);
    929 	pci_conf_write(pc, tag, PCI_CB_IOLIMIT0, 0);
    930 	pci_conf_write(pc, tag, PCI_CB_IOBASE1, 0xffffffff);
    931 	pci_conf_write(pc, tag, PCI_CB_IOLIMIT1, 0);
    932 
    933 	/* reset 16-bit pcmcia bus */
    934 	bus_space_write_1(bmt, bmh, 0x800 + PCIC_INTR,
    935 	    bus_space_read_1(bmt, bmh, 0x800 + PCIC_INTR) & ~PCIC_INTR_RESET);
    936 
    937 	/* turn off power */
    938 	pccbb_power(sc, CARDBUS_VCC_0V | CARDBUS_VPP_0V);
    939 }
    940 
    941 static void
    942 pccbb_intrinit(struct pccbb_softc *sc)
    943 {
    944 	pcireg_t sockmask;
    945 	const char *intrstr = NULL;
    946 	pci_intr_handle_t ih;
    947 	pci_chipset_tag_t pc = sc->sc_pc;
    948 	bus_space_tag_t bmt = sc->sc_base_memt;
    949 	bus_space_handle_t bmh = sc->sc_base_memh;
    950 
    951 	/* Map and establish the interrupt. */
    952 	if (pci_intr_map(&sc->sc_pa, &ih)) {
    953 		aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
    954 		return;
    955 	}
    956 	intrstr = pci_intr_string(pc, ih);
    957 
    958 	/*
    959 	 * XXX pccbbintr should be called under the priority lower
    960 	 * than any other hard interupts.
    961 	 */
    962 	KASSERT(sc->sc_ih == NULL);
    963 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, pccbbintr, sc);
    964 
    965 	if (sc->sc_ih == NULL) {
    966 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
    967 		if (intrstr != NULL)
    968 			aprint_error(" at %s\n", intrstr);
    969 		else
    970 			aprint_error("\n");
    971 		return;
    972 	}
    973 
    974 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
    975 
    976 	/* CSC Interrupt: Card detect and power cycle interrupts on */
    977 	sockmask = bus_space_read_4(bmt, bmh, CB_SOCKET_MASK);
    978 	sockmask |= CB_SOCKET_MASK_CSTS | CB_SOCKET_MASK_CD |
    979 	    CB_SOCKET_MASK_POWER;
    980 	bus_space_write_4(bmt, bmh, CB_SOCKET_MASK, sockmask);
    981 	/* reset interrupt */
    982 	bus_space_write_4(bmt, bmh, CB_SOCKET_EVENT,
    983 	    bus_space_read_4(bmt, bmh, CB_SOCKET_EVENT));
    984 }
    985 
    986 /*
    987  * STATIC void pccbb_pcmcia_attach_setup(struct pccbb_softc *sc,
    988  *					 struct pcmciabus_attach_args *paa)
    989  *
    990  *   This function attaches 16-bit PCcard bus.
    991  */
    992 STATIC void
    993 pccbb_pcmcia_attach_setup(struct pccbb_softc *sc,
    994     struct pcmciabus_attach_args *paa)
    995 {
    996 	struct pcic_handle *ph = &sc->sc_pcmcia_h;
    997 #if rbus
    998 	rbus_tag_t rb;
    999 #endif
   1000 
   1001 	/* initialize pcmcia part in pccbb_softc */
   1002 	ph->ph_parent = sc;
   1003 	/* rest of ph is zero-initialized */
   1004 	sc->sc_pct = &pccbb_pcmcia_funcs;
   1005 
   1006 	/*
   1007 	 * We need to do a few things here:
   1008 	 * 1) Disable routing of CSC and functional interrupts to ISA IRQs by
   1009 	 *    setting the IRQ numbers to 0.
   1010 	 * 2) Set bit 4 of PCIC_INTR, which is needed on some chips to enable
   1011 	 *    routing of CSC interrupts (e.g. card removal) to PCI while in
   1012 	 *    PCMCIA mode.  We just leave this set all the time.
   1013 	 * 3) Enable card insertion/removal interrupts in case the chip also
   1014 	 *    needs that while in PCMCIA mode.
   1015 	 * 4) Clear any pending CSC interrupt.
   1016 	 */
   1017 	Pcic_write(ph, PCIC_INTR, PCIC_INTR_ENABLE);
   1018 	if (sc->sc_chipset == CB_TI113X) {
   1019 		Pcic_write(ph, PCIC_CSC_INTR, 0);
   1020 	} else {
   1021 		Pcic_write(ph, PCIC_CSC_INTR, PCIC_CSC_INTR_CD_ENABLE);
   1022 		Pcic_read(ph, PCIC_CSC);
   1023 	}
   1024 
   1025 	/* initialize pcmcia bus attachment */
   1026 	paa->paa_busname = "pcmcia";
   1027 	paa->pct = sc->sc_pct;
   1028 	paa->pch = ph;
   1029 	paa->iobase = 0;	       /* I don't use them */
   1030 	paa->iosize = 0;
   1031 #if rbus
   1032 	rb = sc->sc_rbus_iot;
   1033 	paa->iobase = rb->rb_start + rb->rb_offset;
   1034 	paa->iosize = rb->rb_end - rb->rb_start;
   1035 #endif
   1036 
   1037 	return;
   1038 }
   1039 
   1040 #if 0
   1041 STATIC void
   1042 pccbb_pcmcia_attach_card(struct pcic_handle *ph)
   1043 {
   1044 	if (ph->flags & PCIC_FLAG_CARDP) {
   1045 		panic("pccbb_pcmcia_attach_card: already attached");
   1046 	}
   1047 
   1048 	/* call the MI attach function */
   1049 	pcmcia_card_attach(ph->pcmcia);
   1050 
   1051 	ph->flags |= PCIC_FLAG_CARDP;
   1052 }
   1053 
   1054 STATIC void
   1055 pccbb_pcmcia_detach_card(struct pcic_handle *ph, int flags)
   1056 {
   1057 	if (!(ph->flags & PCIC_FLAG_CARDP)) {
   1058 		panic("pccbb_pcmcia_detach_card: already detached");
   1059 	}
   1060 
   1061 	ph->flags &= ~PCIC_FLAG_CARDP;
   1062 
   1063 	/* call the MI detach function */
   1064 	pcmcia_card_detach(ph->pcmcia, flags);
   1065 }
   1066 #endif
   1067 
   1068 /*
   1069  * int pccbbintr(arg)
   1070  *    void *arg;
   1071  *   This routine handles the interrupt from Yenta PCI-CardBus bridge
   1072  *   itself.
   1073  */
   1074 int
   1075 pccbbintr(void *arg)
   1076 {
   1077 	struct pccbb_softc *sc = (struct pccbb_softc *)arg;
   1078 	u_int32_t sockevent, sockstate;
   1079 	bus_space_tag_t memt = sc->sc_base_memt;
   1080 	bus_space_handle_t memh = sc->sc_base_memh;
   1081 	struct pcic_handle *ph = &sc->sc_pcmcia_h;
   1082 
   1083 	if (!device_has_power(sc->sc_dev))
   1084 		return 0;
   1085 
   1086 	sockevent = bus_space_read_4(memt, memh, CB_SOCKET_EVENT);
   1087 	bus_space_write_4(memt, memh, CB_SOCKET_EVENT, sockevent);
   1088 	Pcic_read(ph, PCIC_CSC);
   1089 
   1090 	if (sockevent != 0) {
   1091 		aprint_debug("%s: enter sockevent %" PRIx32 "\n", __func__,
   1092 		    sockevent);
   1093 	}
   1094 
   1095 	/* Sometimes a change of CSTSCHG# accompanies the first
   1096 	 * interrupt from an Atheros WLAN.  That generates a
   1097 	 * CB_SOCKET_EVENT_CSTS event on the bridge.  The event
   1098 	 * isn't interesting to pccbb(4), so we used to ignore the
   1099 	 * interrupt.  Now, let the child devices try to handle
   1100 	 * the interrupt, instead.  The Atheros NIC produces
   1101 	 * interrupts more reliably, now: used to be that it would
   1102 	 * only interrupt if the driver avoided powering down the
   1103 	 * NIC's cardslot, and then the NIC would only work after
   1104 	 * it was reset a second time.
   1105 	 */
   1106 	if (sockevent == 0 ||
   1107 	    (sockevent & ~(CB_SOCKET_EVENT_POWER|CB_SOCKET_EVENT_CD)) != 0) {
   1108 		/* This intr is not for me: it may be for my child devices. */
   1109 		if (sc->sc_pil_intr_enable) {
   1110 			return pccbbintr_function(sc);
   1111 		} else {
   1112 			return 0;
   1113 		}
   1114 	}
   1115 
   1116 	if (sockevent & CB_SOCKET_EVENT_CD) {
   1117 		sockstate = bus_space_read_4(memt, memh, CB_SOCKET_STAT);
   1118 		if (0x00 != (sockstate & CB_SOCKET_STAT_CD)) {
   1119 			/* A card should be removed. */
   1120 			if (sc->sc_flags & CBB_CARDEXIST) {
   1121 				DPRINTF(("%s: 0x%08x",
   1122 				    device_xname(sc->sc_dev), sockevent));
   1123 				DPRINTF((" card removed, 0x%08x\n", sockstate));
   1124 				sc->sc_flags &= ~CBB_CARDEXIST;
   1125 				if (sc->sc_csc->sc_status &
   1126 				    CARDSLOT_STATUS_CARD_16) {
   1127 #if 0
   1128 					struct pcic_handle *ph =
   1129 					    &sc->sc_pcmcia_h;
   1130 
   1131 					pcmcia_card_deactivate(ph->pcmcia);
   1132 					pccbb_pcmcia_socket_disable(ph);
   1133 					pccbb_pcmcia_detach_card(ph,
   1134 					    DETACH_FORCE);
   1135 #endif
   1136 					cardslot_event_throw(sc->sc_csc,
   1137 					    CARDSLOT_EVENT_REMOVAL_16);
   1138 				} else if (sc->sc_csc->sc_status &
   1139 				    CARDSLOT_STATUS_CARD_CB) {
   1140 					/* Cardbus intr removed */
   1141 					cardslot_event_throw(sc->sc_csc,
   1142 					    CARDSLOT_EVENT_REMOVAL_CB);
   1143 				}
   1144 			} else if (sc->sc_flags & CBB_INSERTING) {
   1145 				sc->sc_flags &= ~CBB_INSERTING;
   1146 				callout_stop(&sc->sc_insert_ch);
   1147 			}
   1148 		} else if (0x00 == (sockstate & CB_SOCKET_STAT_CD) &&
   1149 		    /*
   1150 		     * The pccbbintr may called from powerdown hook when
   1151 		     * the system resumed, to detect the card
   1152 		     * insertion/removal during suspension.
   1153 		     */
   1154 		    (sc->sc_flags & CBB_CARDEXIST) == 0) {
   1155 			if (sc->sc_flags & CBB_INSERTING) {
   1156 				callout_stop(&sc->sc_insert_ch);
   1157 			}
   1158 			callout_schedule(&sc->sc_insert_ch, hz / 5);
   1159 			sc->sc_flags |= CBB_INSERTING;
   1160 		}
   1161 	}
   1162 
   1163 	/* XXX sockevent == 9 does occur in the wild.  handle it. */
   1164 	if (sockevent & CB_SOCKET_EVENT_POWER) {
   1165 		DPRINTF(("Powercycling because of socket event\n"));
   1166 		/* XXX: Does not happen when attaching a 16-bit card */
   1167 		sc->sc_pwrcycle++;
   1168 		wakeup(&sc->sc_pwrcycle);
   1169 	}
   1170 
   1171 	return (1);
   1172 }
   1173 
   1174 /*
   1175  * static int pccbbintr_function(struct pccbb_softc *sc)
   1176  *
   1177  *    This function calls each interrupt handler registered at the
   1178  *    bridge.  The interrupt handlers are called in registered order.
   1179  */
   1180 static int
   1181 pccbbintr_function(struct pccbb_softc *sc)
   1182 {
   1183 	int retval = 0, val;
   1184 	struct pccbb_intrhand_list *pil;
   1185 	int s;
   1186 
   1187 	LIST_FOREACH(pil, &sc->sc_pil, pil_next) {
   1188 		s = splraiseipl(pil->pil_icookie);
   1189 		val = (*pil->pil_func)(pil->pil_arg);
   1190 		splx(s);
   1191 
   1192 		retval = retval == 1 ? 1 :
   1193 		    retval == 0 ? val : val != 0 ? val : retval;
   1194 	}
   1195 
   1196 	return retval;
   1197 }
   1198 
   1199 static void
   1200 pci113x_insert(void *arg)
   1201 {
   1202 	struct pccbb_softc *sc = arg;
   1203 	u_int32_t sockevent, sockstate;
   1204 
   1205 	if (!(sc->sc_flags & CBB_INSERTING)) {
   1206 		/* We add a card only under inserting state. */
   1207 		return;
   1208 	}
   1209 	sc->sc_flags &= ~CBB_INSERTING;
   1210 
   1211 	sockevent = bus_space_read_4(sc->sc_base_memt, sc->sc_base_memh,
   1212 	    CB_SOCKET_EVENT);
   1213 	sockstate = bus_space_read_4(sc->sc_base_memt, sc->sc_base_memh,
   1214 	    CB_SOCKET_STAT);
   1215 
   1216 	if (0 == (sockstate & CB_SOCKET_STAT_CD)) {	/* card exist */
   1217 		DPRINTF(("%s: 0x%08x", device_xname(sc->sc_dev), sockevent));
   1218 		DPRINTF((" card inserted, 0x%08x\n", sockstate));
   1219 		sc->sc_flags |= CBB_CARDEXIST;
   1220 		/* call pccard interrupt handler here */
   1221 		if (sockstate & CB_SOCKET_STAT_16BIT) {
   1222 			/* 16-bit card found */
   1223 /*      pccbb_pcmcia_attach_card(&sc->sc_pcmcia_h); */
   1224 			cardslot_event_throw(sc->sc_csc,
   1225 			    CARDSLOT_EVENT_INSERTION_16);
   1226 		} else if (sockstate & CB_SOCKET_STAT_CB) {
   1227 			/* cardbus card found */
   1228 /*      cardbus_attach_card(sc->sc_csc); */
   1229 			cardslot_event_throw(sc->sc_csc,
   1230 			    CARDSLOT_EVENT_INSERTION_CB);
   1231 		} else {
   1232 			/* who are you? */
   1233 		}
   1234 	} else {
   1235 		callout_schedule(&sc->sc_insert_ch, hz / 10);
   1236 	}
   1237 }
   1238 
   1239 #define PCCBB_PCMCIA_OFFSET 0x800
   1240 static u_int8_t
   1241 pccbb_pcmcia_read(struct pccbb_softc *sc, int reg)
   1242 {
   1243 	bus_space_barrier(sc->sc_base_memt, sc->sc_base_memh,
   1244 	    PCCBB_PCMCIA_OFFSET + reg, 1, BUS_SPACE_BARRIER_READ);
   1245 
   1246 	return bus_space_read_1(sc->sc_base_memt, sc->sc_base_memh,
   1247 	    PCCBB_PCMCIA_OFFSET + reg);
   1248 }
   1249 
   1250 static void
   1251 pccbb_pcmcia_write(struct pccbb_softc *sc, int reg, u_int8_t val)
   1252 {
   1253 	bus_space_write_1(sc->sc_base_memt, sc->sc_base_memh,
   1254 			  PCCBB_PCMCIA_OFFSET + reg, val);
   1255 
   1256 	bus_space_barrier(sc->sc_base_memt, sc->sc_base_memh,
   1257 	    PCCBB_PCMCIA_OFFSET + reg, 1, BUS_SPACE_BARRIER_WRITE);
   1258 }
   1259 
   1260 /*
   1261  * STATIC int pccbb_ctrl(cardbus_chipset_tag_t, int)
   1262  */
   1263 STATIC int
   1264 pccbb_ctrl(cardbus_chipset_tag_t ct, int command)
   1265 {
   1266 	struct pccbb_softc *sc = (struct pccbb_softc *)ct;
   1267 
   1268 	switch (command) {
   1269 	case CARDBUS_CD:
   1270 		if (2 == pccbb_detect_card(sc)) {
   1271 			int retval = 0;
   1272 			int status = cb_detect_voltage(sc);
   1273 			if (PCCARD_VCC_5V & status) {
   1274 				retval |= CARDBUS_5V_CARD;
   1275 			}
   1276 			if (PCCARD_VCC_3V & status) {
   1277 				retval |= CARDBUS_3V_CARD;
   1278 			}
   1279 			if (PCCARD_VCC_XV & status) {
   1280 				retval |= CARDBUS_XV_CARD;
   1281 			}
   1282 			if (PCCARD_VCC_YV & status) {
   1283 				retval |= CARDBUS_YV_CARD;
   1284 			}
   1285 			return retval;
   1286 		} else {
   1287 			return 0;
   1288 		}
   1289 	case CARDBUS_RESET:
   1290 		return cb_reset(sc);
   1291 	case CARDBUS_IO_ENABLE:       /* fallthrough */
   1292 	case CARDBUS_IO_DISABLE:      /* fallthrough */
   1293 	case CARDBUS_MEM_ENABLE:      /* fallthrough */
   1294 	case CARDBUS_MEM_DISABLE:     /* fallthrough */
   1295 	case CARDBUS_BM_ENABLE:       /* fallthrough */
   1296 	case CARDBUS_BM_DISABLE:      /* fallthrough */
   1297 		/* XXX: I think we don't need to call this function below. */
   1298 		return pccbb_cardenable(sc, command);
   1299 	}
   1300 
   1301 	return 0;
   1302 }
   1303 
   1304 STATIC int
   1305 pccbb_power_ct(cardbus_chipset_tag_t ct, int command)
   1306 {
   1307 	struct pccbb_softc *sc = (struct pccbb_softc *)ct;
   1308 
   1309 	return pccbb_power(sc, command);
   1310 }
   1311 
   1312 /*
   1313  * STATIC int pccbb_power(cardbus_chipset_tag_t, int)
   1314  *   This function returns true when it succeeds and returns false when
   1315  *   it fails.
   1316  */
   1317 STATIC int
   1318 pccbb_power(struct pccbb_softc *sc, int command)
   1319 {
   1320 	u_int32_t status, osock_ctrl, sock_ctrl, reg_ctrl;
   1321 	bus_space_tag_t memt = sc->sc_base_memt;
   1322 	bus_space_handle_t memh = sc->sc_base_memh;
   1323 	int on = 0, pwrcycle, s, times;
   1324 	struct timeval before, after, diff;
   1325 
   1326 	DPRINTF(("pccbb_power: %s and %s [0x%x]\n",
   1327 	    (command & CARDBUS_VCCMASK) == CARDBUS_VCC_UC ? "CARDBUS_VCC_UC" :
   1328 	    (command & CARDBUS_VCCMASK) == CARDBUS_VCC_5V ? "CARDBUS_VCC_5V" :
   1329 	    (command & CARDBUS_VCCMASK) == CARDBUS_VCC_3V ? "CARDBUS_VCC_3V" :
   1330 	    (command & CARDBUS_VCCMASK) == CARDBUS_VCC_XV ? "CARDBUS_VCC_XV" :
   1331 	    (command & CARDBUS_VCCMASK) == CARDBUS_VCC_YV ? "CARDBUS_VCC_YV" :
   1332 	    (command & CARDBUS_VCCMASK) == CARDBUS_VCC_0V ? "CARDBUS_VCC_0V" :
   1333 	    "UNKNOWN",
   1334 	    (command & CARDBUS_VPPMASK) == CARDBUS_VPP_UC ? "CARDBUS_VPP_UC" :
   1335 	    (command & CARDBUS_VPPMASK) == CARDBUS_VPP_12V ? "CARDBUS_VPP_12V" :
   1336 	    (command & CARDBUS_VPPMASK) == CARDBUS_VPP_VCC ? "CARDBUS_VPP_VCC" :
   1337 	    (command & CARDBUS_VPPMASK) == CARDBUS_VPP_0V ? "CARDBUS_VPP_0V" :
   1338 	    "UNKNOWN", command));
   1339 
   1340 	status = bus_space_read_4(memt, memh, CB_SOCKET_STAT);
   1341 	osock_ctrl = sock_ctrl = bus_space_read_4(memt, memh, CB_SOCKET_CTRL);
   1342 
   1343 	switch (command & CARDBUS_VCCMASK) {
   1344 	case CARDBUS_VCC_UC:
   1345 		break;
   1346 	case CARDBUS_VCC_5V:
   1347 		on++;
   1348 		if (CB_SOCKET_STAT_5VCARD & status) {	/* check 5 V card */
   1349 			sock_ctrl &= ~CB_SOCKET_CTRL_VCCMASK;
   1350 			sock_ctrl |= CB_SOCKET_CTRL_VCC_5V;
   1351 		} else {
   1352 			aprint_error_dev(sc->sc_dev,
   1353 			    "BAD voltage request: no 5 V card\n");
   1354 			return 0;
   1355 		}
   1356 		break;
   1357 	case CARDBUS_VCC_3V:
   1358 		on++;
   1359 		if (CB_SOCKET_STAT_3VCARD & status) {
   1360 			sock_ctrl &= ~CB_SOCKET_CTRL_VCCMASK;
   1361 			sock_ctrl |= CB_SOCKET_CTRL_VCC_3V;
   1362 		} else {
   1363 			aprint_error_dev(sc->sc_dev,
   1364 			    "BAD voltage request: no 3.3 V card\n");
   1365 			return 0;
   1366 		}
   1367 		break;
   1368 	case CARDBUS_VCC_0V:
   1369 		sock_ctrl &= ~CB_SOCKET_CTRL_VCCMASK;
   1370 		break;
   1371 	default:
   1372 		return 0;	       /* power NEVER changed */
   1373 	}
   1374 
   1375 	switch (command & CARDBUS_VPPMASK) {
   1376 	case CARDBUS_VPP_UC:
   1377 		break;
   1378 	case CARDBUS_VPP_0V:
   1379 		sock_ctrl &= ~CB_SOCKET_CTRL_VPPMASK;
   1380 		break;
   1381 	case CARDBUS_VPP_VCC:
   1382 		sock_ctrl &= ~CB_SOCKET_CTRL_VPPMASK;
   1383 		sock_ctrl |= ((sock_ctrl >> 4) & 0x07);
   1384 		break;
   1385 	case CARDBUS_VPP_12V:
   1386 		sock_ctrl &= ~CB_SOCKET_CTRL_VPPMASK;
   1387 		sock_ctrl |= CB_SOCKET_CTRL_VPP_12V;
   1388 		break;
   1389 	}
   1390 
   1391 	pwrcycle = sc->sc_pwrcycle;
   1392 	aprint_debug_dev(sc->sc_dev, "osock_ctrl %#" PRIx32
   1393 	    " sock_ctrl %#" PRIx32 "\n", osock_ctrl, sock_ctrl);
   1394 
   1395 	microtime(&before);
   1396 	s = splbio();
   1397 	bus_space_write_4(memt, memh, CB_SOCKET_CTRL, sock_ctrl);
   1398 
   1399 	/*
   1400 	 * Wait as long as 200ms for a power-cycle interrupt.  If
   1401 	 * interrupts are enabled, but the socket has already
   1402 	 * changed to the desired status, keep waiting for the
   1403 	 * interrupt.  "Consuming" the interrupt in this way keeps
   1404 	 * the interrupt from prematurely waking some subsequent
   1405 	 * pccbb_power call.
   1406 	 *
   1407 	 * XXX Not every bridge interrupts on the ->OFF transition.
   1408 	 * XXX That's ok, we will time-out after 200ms.
   1409 	 *
   1410 	 * XXX The power cycle event will never happen when attaching
   1411 	 * XXX a 16-bit card.  That's ok, we will time-out after
   1412 	 * XXX 200ms.
   1413 	 */
   1414 	for (times = 5; --times >= 0; ) {
   1415 		if (cold)
   1416 			DELAY(40 * 1000);
   1417 		else {
   1418 			(void)tsleep(&sc->sc_pwrcycle, PWAIT, "pccpwr",
   1419 			    hz / 25);
   1420 			if (pwrcycle == sc->sc_pwrcycle)
   1421 				continue;
   1422 		}
   1423 		status = bus_space_read_4(memt, memh, CB_SOCKET_STAT);
   1424 		if ((status & CB_SOCKET_STAT_PWRCYCLE) != 0 && on)
   1425 			break;
   1426 		if ((status & CB_SOCKET_STAT_PWRCYCLE) == 0 && !on)
   1427 			break;
   1428 	}
   1429 	splx(s);
   1430 	microtime(&after);
   1431 	timersub(&after, &before, &diff);
   1432 	aprint_debug_dev(sc->sc_dev, "wait took%s %ld.%06lds\n",
   1433 	    (on && times < 0) ? " too long" : "", diff.tv_sec, diff.tv_usec);
   1434 
   1435 	/*
   1436 	 * Ok, wait a bit longer for things to settle.
   1437 	 */
   1438 	if (on && sc->sc_chipset == CB_TOPIC95B)
   1439 		delay_ms(100, sc);
   1440 
   1441 	status = bus_space_read_4(memt, memh, CB_SOCKET_STAT);
   1442 
   1443 	if (on && sc->sc_chipset != CB_TOPIC95B) {
   1444 		if ((status & CB_SOCKET_STAT_PWRCYCLE) == 0)
   1445 			aprint_error_dev(sc->sc_dev, "power on failed?\n");
   1446 	}
   1447 
   1448 	if (status & CB_SOCKET_STAT_BADVCC) {	/* bad Vcc request */
   1449 		aprint_error_dev(sc->sc_dev,
   1450 		    "bad Vcc request. sock_ctrl 0x%x, sock_status 0x%x\n",
   1451 		    sock_ctrl, status);
   1452 		aprint_error_dev(sc->sc_dev, "disabling socket\n");
   1453 		sock_ctrl &= ~CB_SOCKET_CTRL_VCCMASK;
   1454 		sock_ctrl &= ~CB_SOCKET_CTRL_VPPMASK;
   1455 		bus_space_write_4(memt, memh, CB_SOCKET_CTRL, sock_ctrl);
   1456 		status &= ~CB_SOCKET_STAT_BADVCC;
   1457 		bus_space_write_4(memt, memh, CB_SOCKET_FORCE, status);
   1458 		printf("new status 0x%x\n", bus_space_read_4(memt, memh,
   1459 		    CB_SOCKET_STAT));
   1460 		return 0;
   1461 	}
   1462 
   1463 	if (sc->sc_chipset == CB_TOPIC97) {
   1464 		reg_ctrl = pci_conf_read(sc->sc_pc, sc->sc_tag, TOPIC_REG_CTRL);
   1465 		reg_ctrl &= ~TOPIC97_REG_CTRL_TESTMODE;
   1466 		if ((command & CARDBUS_VCCMASK) == CARDBUS_VCC_0V)
   1467 			reg_ctrl &= ~TOPIC97_REG_CTRL_CLKRUN_ENA;
   1468 		else
   1469 			reg_ctrl |= TOPIC97_REG_CTRL_CLKRUN_ENA;
   1470 		pci_conf_write(sc->sc_pc, sc->sc_tag, TOPIC_REG_CTRL, reg_ctrl);
   1471 	}
   1472 
   1473 	return 1;		       /* power changed correctly */
   1474 }
   1475 
   1476 #if defined CB_PCMCIA_POLL
   1477 struct cb_poll_str {
   1478 	void *arg;
   1479 	int (*func)(void *);
   1480 	int level;
   1481 	pccard_chipset_tag_t ct;
   1482 	int count;
   1483 	struct callout poll_ch;
   1484 };
   1485 
   1486 static struct cb_poll_str cb_poll[10];
   1487 static int cb_poll_n = 0;
   1488 
   1489 static void cb_pcmcia_poll(void *arg);
   1490 
   1491 static void
   1492 cb_pcmcia_poll(void *arg)
   1493 {
   1494 	struct cb_poll_str *poll = arg;
   1495 	struct cbb_pcmcia_softc *psc = (void *)poll->ct->v;
   1496 	struct pccbb_softc *sc = psc->cpc_parent;
   1497 	int s;
   1498 	u_int32_t spsr;		       /* socket present-state reg */
   1499 
   1500 	callout_reset(&poll->poll_ch, hz / 10, cb_pcmcia_poll, poll);
   1501 	switch (poll->level) {
   1502 	case IPL_NET:
   1503 		s = splnet();
   1504 		break;
   1505 	case IPL_BIO:
   1506 		s = splbio();
   1507 		break;
   1508 	case IPL_TTY:		       /* fallthrough */
   1509 	default:
   1510 		s = spltty();
   1511 		break;
   1512 	}
   1513 
   1514 	spsr =
   1515 	    bus_space_read_4(sc->sc_base_memt, sc->sc_base_memh,
   1516 	    CB_SOCKET_STAT);
   1517 
   1518 #if defined CB_PCMCIA_POLL_ONLY && defined LEVEL2
   1519 	if (!(spsr & 0x40)) {	       /* CINT low */
   1520 #else
   1521 	if (1) {
   1522 #endif
   1523 		if ((*poll->func) (poll->arg) == 1) {
   1524 			++poll->count;
   1525 			printf("intr: reported from poller, 0x%x\n", spsr);
   1526 #if defined LEVEL2
   1527 		} else {
   1528 			printf("intr: miss! 0x%x\n", spsr);
   1529 #endif
   1530 		}
   1531 	}
   1532 	splx(s);
   1533 }
   1534 #endif /* defined CB_PCMCIA_POLL */
   1535 
   1536 /*
   1537  * static int pccbb_detect_card(struct pccbb_softc *sc)
   1538  *   return value:  0 if no card exists.
   1539  *                  1 if 16-bit card exists.
   1540  *                  2 if cardbus card exists.
   1541  */
   1542 static int
   1543 pccbb_detect_card(struct pccbb_softc *sc)
   1544 {
   1545 	bus_space_handle_t base_memh = sc->sc_base_memh;
   1546 	bus_space_tag_t base_memt = sc->sc_base_memt;
   1547 	u_int32_t sockstat =
   1548 	    bus_space_read_4(base_memt, base_memh, CB_SOCKET_STAT);
   1549 	int retval = 0;
   1550 
   1551 	/* CD1 and CD2 asserted */
   1552 	if (0x00 == (sockstat & CB_SOCKET_STAT_CD)) {
   1553 		/* card must be present */
   1554 		if (!(CB_SOCKET_STAT_NOTCARD & sockstat)) {
   1555 			/* NOTACARD DEASSERTED */
   1556 			if (CB_SOCKET_STAT_CB & sockstat) {
   1557 				/* CardBus mode */
   1558 				retval = 2;
   1559 			} else if (CB_SOCKET_STAT_16BIT & sockstat) {
   1560 				/* 16-bit mode */
   1561 				retval = 1;
   1562 			}
   1563 		}
   1564 	}
   1565 	return retval;
   1566 }
   1567 
   1568 /*
   1569  * STATIC int cb_reset(struct pccbb_softc *sc)
   1570  *   This function resets CardBus card.
   1571  */
   1572 STATIC int
   1573 cb_reset(struct pccbb_softc *sc)
   1574 {
   1575 	/*
   1576 	 * Reset Assert at least 20 ms
   1577 	 * Some machines request longer duration.
   1578 	 */
   1579 	int reset_duration =
   1580 	    (sc->sc_chipset == CB_RX5C47X ? 400 : 50);
   1581 	u_int32_t bcr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_BRIDGE_CONTROL_REG);
   1582 	aprint_debug("%s: enter bcr %" PRIx32 "\n", __func__, bcr);
   1583 
   1584 	/* Reset bit Assert (bit 6 at 0x3E) */
   1585 	bcr |= PCI_BRIDGE_CONTROL_SECBR << PCI_BRIDGE_CONTROL_SHIFT;
   1586 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_BRIDGE_CONTROL_REG, bcr);
   1587 	aprint_debug("%s: wrote bcr %" PRIx32 "\n", __func__, bcr);
   1588 	delay_ms(reset_duration, sc);
   1589 
   1590 	if (CBB_CARDEXIST & sc->sc_flags) {	/* A card exists.  Reset it! */
   1591 		/* Reset bit Deassert (bit 6 at 0x3E) */
   1592 		bcr &= ~(PCI_BRIDGE_CONTROL_SECBR << PCI_BRIDGE_CONTROL_SHIFT);
   1593 		pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_BRIDGE_CONTROL_REG,
   1594 		    bcr);
   1595 		aprint_debug("%s: wrote bcr %" PRIx32 "\n", __func__, bcr);
   1596 		delay_ms(reset_duration, sc);
   1597 		aprint_debug("%s: end of delay\n", __func__);
   1598 	}
   1599 	/* No card found on the slot. Keep Reset. */
   1600 	return 1;
   1601 }
   1602 
   1603 /*
   1604  * STATIC int cb_detect_voltage(struct pccbb_softc *sc)
   1605  *  This function detect card Voltage.
   1606  */
   1607 STATIC int
   1608 cb_detect_voltage(struct pccbb_softc *sc)
   1609 {
   1610 	u_int32_t psr;		       /* socket present-state reg */
   1611 	bus_space_tag_t iot = sc->sc_base_memt;
   1612 	bus_space_handle_t ioh = sc->sc_base_memh;
   1613 	int vol = PCCARD_VCC_UKN;      /* set 0 */
   1614 
   1615 	psr = bus_space_read_4(iot, ioh, CB_SOCKET_STAT);
   1616 
   1617 	if (0x400u & psr) {
   1618 		vol |= PCCARD_VCC_5V;
   1619 	}
   1620 	if (0x800u & psr) {
   1621 		vol |= PCCARD_VCC_3V;
   1622 	}
   1623 
   1624 	return vol;
   1625 }
   1626 
   1627 STATIC int
   1628 cbbprint(void *aux, const char *pcic)
   1629 {
   1630 #if 0
   1631 	struct cbslot_attach_args *cba = aux;
   1632 
   1633 	if (cba->cba_slot >= 0) {
   1634 		aprint_normal(" slot %d", cba->cba_slot);
   1635 	}
   1636 #endif
   1637 	return UNCONF;
   1638 }
   1639 
   1640 /*
   1641  * STATIC int pccbb_cardenable(struct pccbb_softc *sc, int function)
   1642  *   This function enables and disables the card
   1643  */
   1644 STATIC int
   1645 pccbb_cardenable(struct pccbb_softc *sc, int function)
   1646 {
   1647 	u_int32_t command =
   1648 	    pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
   1649 
   1650 	DPRINTF(("pccbb_cardenable:"));
   1651 	switch (function) {
   1652 	case CARDBUS_IO_ENABLE:
   1653 		command |= PCI_COMMAND_IO_ENABLE;
   1654 		break;
   1655 	case CARDBUS_IO_DISABLE:
   1656 		command &= ~PCI_COMMAND_IO_ENABLE;
   1657 		break;
   1658 	case CARDBUS_MEM_ENABLE:
   1659 		command |= PCI_COMMAND_MEM_ENABLE;
   1660 		break;
   1661 	case CARDBUS_MEM_DISABLE:
   1662 		command &= ~PCI_COMMAND_MEM_ENABLE;
   1663 		break;
   1664 	case CARDBUS_BM_ENABLE:
   1665 		command |= PCI_COMMAND_MASTER_ENABLE;
   1666 		break;
   1667 	case CARDBUS_BM_DISABLE:
   1668 		command &= ~PCI_COMMAND_MASTER_ENABLE;
   1669 		break;
   1670 	default:
   1671 		return 0;
   1672 	}
   1673 
   1674 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, command);
   1675 	DPRINTF((" command reg 0x%x\n", command));
   1676 	return 1;
   1677 }
   1678 
   1679 #if !rbus
   1680 static int
   1681 pccbb_io_open(cardbus_chipset_tag_t ct, int win, uint32_t start, uint32_t end)
   1682 {
   1683 	struct pccbb_softc *sc = (struct pccbb_softc *)ct;
   1684 	int basereg;
   1685 	int limitreg;
   1686 
   1687 	if ((win < 0) || (win > 2)) {
   1688 #if defined DIAGNOSTIC
   1689 		printf("cardbus_io_open: window out of range %d\n", win);
   1690 #endif
   1691 		return 0;
   1692 	}
   1693 
   1694 	basereg = win * 8 + PCI_CB_IOBASE0;
   1695 	limitreg = win * 8 + PCI_CB_IOLIMIT0;
   1696 
   1697 	DPRINTF(("pccbb_io_open: 0x%x[0x%x] - 0x%x[0x%x]\n",
   1698 	    start, basereg, end, limitreg));
   1699 
   1700 	pci_conf_write(sc->sc_pc, sc->sc_tag, basereg, start);
   1701 	pci_conf_write(sc->sc_pc, sc->sc_tag, limitreg, end);
   1702 	return 1;
   1703 }
   1704 
   1705 /*
   1706  * int pccbb_io_close(cardbus_chipset_tag_t, int)
   1707  */
   1708 static int
   1709 pccbb_io_close(cardbus_chipset_tag_t ct, int win)
   1710 {
   1711 	struct pccbb_softc *sc = (struct pccbb_softc *)ct;
   1712 	int basereg;
   1713 	int limitreg;
   1714 
   1715 	if ((win < 0) || (win > 2)) {
   1716 #if defined DIAGNOSTIC
   1717 		printf("cardbus_io_close: window out of range %d\n", win);
   1718 #endif
   1719 		return 0;
   1720 	}
   1721 
   1722 	basereg = win * 8 + PCI_CB_IOBASE0;
   1723 	limitreg = win * 8 + PCI_CB_IOLIMIT0;
   1724 
   1725 	pci_conf_write(sc->sc_pc, sc->sc_tag, basereg, 0);
   1726 	pci_conf_write(sc->sc_pc, sc->sc_tag, limitreg, 0);
   1727 	return 1;
   1728 }
   1729 
   1730 static int
   1731 pccbb_mem_open(cardbus_chipset_tag_t ct, int win, uint32_t start, uint32_t end)
   1732 {
   1733 	struct pccbb_softc *sc = (struct pccbb_softc *)ct;
   1734 	int basereg;
   1735 	int limitreg;
   1736 
   1737 	if ((win < 0) || (win > 2)) {
   1738 #if defined DIAGNOSTIC
   1739 		printf("cardbus_mem_open: window out of range %d\n", win);
   1740 #endif
   1741 		return 0;
   1742 	}
   1743 
   1744 	basereg = win * 8 + PCI_CB_MEMBASE0;
   1745 	limitreg = win * 8 + PCI_CB_MEMLIMIT0;
   1746 
   1747 	pci_conf_write(sc->sc_pc, sc->sc_tag, basereg, start);
   1748 	pci_conf_write(sc->sc_pc, sc->sc_tag, limitreg, end);
   1749 	return 1;
   1750 }
   1751 
   1752 static int
   1753 pccbb_mem_close(cardbus_chipset_tag_t ct, int win)
   1754 {
   1755 	struct pccbb_softc *sc = (struct pccbb_softc *)ct;
   1756 	int basereg;
   1757 	int limitreg;
   1758 
   1759 	if ((win < 0) || (win > 2)) {
   1760 #if defined DIAGNOSTIC
   1761 		printf("cardbus_mem_close: window out of range %d\n", win);
   1762 #endif
   1763 		return 0;
   1764 	}
   1765 
   1766 	basereg = win * 8 + PCI_CB_MEMBASE0;
   1767 	limitreg = win * 8 + PCI_CB_MEMLIMIT0;
   1768 
   1769 	pci_conf_write(sc->sc_pc, sc->sc_tag, basereg, 0);
   1770 	pci_conf_write(sc->sc_pc, sc->sc_tag, limitreg, 0);
   1771 	return 1;
   1772 }
   1773 #endif
   1774 
   1775 /*
   1776  * static void *pccbb_cb_intr_establish(cardbus_chipset_tag_t ct,
   1777  *					int irq,
   1778  *					int level,
   1779  *					int (* func)(void *),
   1780  *					void *arg)
   1781  *
   1782  *   This function registers an interrupt handler at the bridge, in
   1783  *   order not to call the interrupt handlers of child devices when
   1784  *   a card-deletion interrupt occurs.
   1785  *
   1786  *   The arguments irq and level are not used.
   1787  */
   1788 static void *
   1789 pccbb_cb_intr_establish(cardbus_chipset_tag_t ct, cardbus_intr_line_t irq,
   1790     int level, int (*func)(void *), void *arg)
   1791 {
   1792 	struct pccbb_softc *sc = (struct pccbb_softc *)ct;
   1793 
   1794 	return pccbb_intr_establish(sc, irq, level, func, arg);
   1795 }
   1796 
   1797 
   1798 /*
   1799  * static void *pccbb_cb_intr_disestablish(cardbus_chipset_tag_t ct,
   1800  *					   void *ih)
   1801  *
   1802  *   This function removes an interrupt handler pointed by ih.
   1803  */
   1804 static void
   1805 pccbb_cb_intr_disestablish(cardbus_chipset_tag_t ct, void *ih)
   1806 {
   1807 	struct pccbb_softc *sc = (struct pccbb_softc *)ct;
   1808 
   1809 	pccbb_intr_disestablish(sc, ih);
   1810 }
   1811 
   1812 
   1813 void
   1814 pccbb_intr_route(struct pccbb_softc *sc)
   1815 {
   1816 	pcireg_t bcr, cbctrl;
   1817 
   1818 	/* initialize bridge intr routing */
   1819 	bcr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_BRIDGE_CONTROL_REG);
   1820 	bcr &= ~CB_BCR_INTR_IREQ_ENABLE;
   1821 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_BRIDGE_CONTROL_REG, bcr);
   1822 
   1823 	switch (sc->sc_chipset) {
   1824 	case CB_TI113X:
   1825 		cbctrl = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CBCTRL);
   1826 		/* functional intr enabled */
   1827 		cbctrl |= PCI113X_CBCTRL_PCI_INTR;
   1828 		pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CBCTRL, cbctrl);
   1829 		break;
   1830 	default:
   1831 		break;
   1832 	}
   1833 }
   1834 
   1835 /*
   1836  * static void *pccbb_intr_establish(struct pccbb_softc *sc,
   1837  *				     int irq,
   1838  *				     int level,
   1839  *				     int (* func)(void *),
   1840  *				     void *arg)
   1841  *
   1842  *   This function registers an interrupt handler at the bridge, in
   1843  *   order not to call the interrupt handlers of child devices when
   1844  *   a card-deletion interrupt occurs.
   1845  *
   1846  *   The arguments irq is not used because pccbb selects intr vector.
   1847  */
   1848 static void *
   1849 pccbb_intr_establish(struct pccbb_softc *sc, cardbus_intr_line_t irq,
   1850     int level, int (*func)(void *), void *arg)
   1851 {
   1852 	struct pccbb_intrhand_list *pil, *newpil;
   1853 
   1854 	DPRINTF(("pccbb_intr_establish start. %p\n", LIST_FIRST(&sc->sc_pil)));
   1855 
   1856 	if (LIST_EMPTY(&sc->sc_pil)) {
   1857 		pccbb_intr_route(sc);
   1858 	}
   1859 
   1860 	/*
   1861 	 * Allocate a room for interrupt handler structure.
   1862 	 */
   1863 	if (NULL == (newpil =
   1864 	    (struct pccbb_intrhand_list *)malloc(sizeof(struct
   1865 	    pccbb_intrhand_list), M_DEVBUF, M_WAITOK))) {
   1866 		return NULL;
   1867 	}
   1868 
   1869 	newpil->pil_func = func;
   1870 	newpil->pil_arg = arg;
   1871 	newpil->pil_icookie = makeiplcookie(level);
   1872 
   1873 	if (LIST_EMPTY(&sc->sc_pil)) {
   1874 		LIST_INSERT_HEAD(&sc->sc_pil, newpil, pil_next);
   1875 	} else {
   1876 		for (pil = LIST_FIRST(&sc->sc_pil);
   1877 		     LIST_NEXT(pil, pil_next) != NULL;
   1878 		     pil = LIST_NEXT(pil, pil_next));
   1879 		LIST_INSERT_AFTER(pil, newpil, pil_next);
   1880 	}
   1881 
   1882 	DPRINTF(("pccbb_intr_establish add pil. %p\n",
   1883 	    LIST_FIRST(&sc->sc_pil)));
   1884 
   1885 	return newpil;
   1886 }
   1887 
   1888 /*
   1889  * static void *pccbb_intr_disestablish(struct pccbb_softc *sc,
   1890  *					void *ih)
   1891  *
   1892  *	This function removes an interrupt handler pointed by ih.  ih
   1893  *	should be the value returned by cardbus_intr_establish() or
   1894  *	NULL.
   1895  *
   1896  *	When ih is NULL, this function will do nothing.
   1897  */
   1898 static void
   1899 pccbb_intr_disestablish(struct pccbb_softc *sc, void *ih)
   1900 {
   1901 	struct pccbb_intrhand_list *pil;
   1902 	pcireg_t reg;
   1903 
   1904 	DPRINTF(("pccbb_intr_disestablish start. %p\n",
   1905 	    LIST_FIRST(&sc->sc_pil)));
   1906 
   1907 	if (ih == NULL) {
   1908 		/* intr handler is not set */
   1909 		DPRINTF(("pccbb_intr_disestablish: no ih\n"));
   1910 		return;
   1911 	}
   1912 
   1913 #ifdef DIAGNOSTIC
   1914 	LIST_FOREACH(pil, &sc->sc_pil, pil_next) {
   1915 		DPRINTF(("pccbb_intr_disestablish: pil %p\n", pil));
   1916 		if (pil == ih) {
   1917 			DPRINTF(("pccbb_intr_disestablish frees one pil\n"));
   1918 			break;
   1919 		}
   1920 	}
   1921 	if (pil == NULL) {
   1922 		panic("pccbb_intr_disestablish: %s cannot find pil %p",
   1923 		    device_xname(sc->sc_dev), ih);
   1924 	}
   1925 #endif
   1926 
   1927 	pil = (struct pccbb_intrhand_list *)ih;
   1928 	LIST_REMOVE(pil, pil_next);
   1929 	free(pil, M_DEVBUF);
   1930 	DPRINTF(("pccbb_intr_disestablish frees one pil\n"));
   1931 
   1932 	if (LIST_EMPTY(&sc->sc_pil)) {
   1933 		/* No interrupt handlers */
   1934 
   1935 		DPRINTF(("pccbb_intr_disestablish: no interrupt handler\n"));
   1936 
   1937 		/* stop routing PCI intr */
   1938 		reg = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_BRIDGE_CONTROL_REG);
   1939 		reg |= CB_BCR_INTR_IREQ_ENABLE;
   1940 		pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_BRIDGE_CONTROL_REG, reg);
   1941 
   1942 		switch (sc->sc_chipset) {
   1943 		case CB_TI113X:
   1944 			reg = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CBCTRL);
   1945 			/* functional intr disabled */
   1946 			reg &= ~PCI113X_CBCTRL_PCI_INTR;
   1947 			pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CBCTRL, reg);
   1948 			break;
   1949 		default:
   1950 			break;
   1951 		}
   1952 	}
   1953 }
   1954 
   1955 #if defined SHOW_REGS
   1956 static void
   1957 cb_show_regs(pci_chipset_tag_t pc, pcitag_t tag, bus_space_tag_t memt,
   1958     bus_space_handle_t memh)
   1959 {
   1960 	int i;
   1961 	printf("PCI config regs:");
   1962 	for (i = 0; i < 0x50; i += 4) {
   1963 		if (i % 16 == 0)
   1964 			printf("\n 0x%02x:", i);
   1965 		printf(" %08x", pci_conf_read(pc, tag, i));
   1966 	}
   1967 	for (i = 0x80; i < 0xb0; i += 4) {
   1968 		if (i % 16 == 0)
   1969 			printf("\n 0x%02x:", i);
   1970 		printf(" %08x", pci_conf_read(pc, tag, i));
   1971 	}
   1972 
   1973 	if (memh == 0) {
   1974 		printf("\n");
   1975 		return;
   1976 	}
   1977 
   1978 	printf("\nsocket regs:");
   1979 	for (i = 0; i <= 0x10; i += 0x04)
   1980 		printf(" %08x", bus_space_read_4(memt, memh, i));
   1981 	printf("\nExCA regs:");
   1982 	for (i = 0; i < 0x08; ++i)
   1983 		printf(" %02x", bus_space_read_1(memt, memh, 0x800 + i));
   1984 	printf("\n");
   1985 	return;
   1986 }
   1987 #endif
   1988 
   1989 /*
   1990  * static cardbustag_t pccbb_make_tag(cardbus_chipset_tag_t cc,
   1991  *                                    int busno, int function)
   1992  *   This is the function to make a tag to access config space of
   1993  *  a CardBus Card.  It works same as pci_conf_read.
   1994  */
   1995 static cardbustag_t
   1996 pccbb_make_tag(cardbus_chipset_tag_t cc, int busno, int function)
   1997 {
   1998 	struct pccbb_softc *sc = (struct pccbb_softc *)cc;
   1999 
   2000 	return pci_make_tag(sc->sc_pc, busno, 0, function);
   2001 }
   2002 
   2003 static void
   2004 pccbb_free_tag(cardbus_chipset_tag_t cc, cardbustag_t tag)
   2005 {
   2006 }
   2007 
   2008 /*
   2009  * pccbb_conf_read
   2010  *
   2011  * This is the function to read the config space of a CardBus card.
   2012  * It works the same as pci_conf_read(9).
   2013  */
   2014 static cardbusreg_t
   2015 pccbb_conf_read(cardbus_chipset_tag_t cc, cardbustag_t tag, int offset)
   2016 {
   2017 	struct pccbb_softc *sc = (struct pccbb_softc *)cc;
   2018 
   2019 	return pci_conf_read(sc->sc_pc, tag, offset);
   2020 }
   2021 
   2022 /*
   2023  * pccbb_conf_write
   2024  *
   2025  * This is the function to write the config space of a CardBus
   2026  * card.  It works the same as pci_conf_write(9).
   2027  */
   2028 static void
   2029 pccbb_conf_write(cardbus_chipset_tag_t cc, cardbustag_t tag, int reg,
   2030     cardbusreg_t val)
   2031 {
   2032 	struct pccbb_softc *sc = (struct pccbb_softc *)cc;
   2033 
   2034 	pci_conf_write(sc->sc_pc, tag, reg, val);
   2035 }
   2036 
   2037 #if 0
   2038 STATIC int
   2039 pccbb_new_pcmcia_io_alloc(pcmcia_chipset_handle_t pch,
   2040     bus_addr_t start, bus_size_t size, bus_size_t align, bus_addr_t mask,
   2041     int speed, int flags,
   2042     bus_space_handle_t * iohp)
   2043 #endif
   2044 /*
   2045  * STATIC int pccbb_pcmcia_io_alloc(pcmcia_chipset_handle_t pch,
   2046  *                                  bus_addr_t start, bus_size_t size,
   2047  *                                  bus_size_t align,
   2048  *                                  struct pcmcia_io_handle *pcihp
   2049  *
   2050  * This function only allocates I/O region for pccard. This function
   2051  * never maps the allocated region to pccard I/O area.
   2052  *
   2053  * XXX: The interface of this function is not very good, I believe.
   2054  */
   2055 STATIC int
   2056 pccbb_pcmcia_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start,
   2057     bus_size_t size, bus_size_t align, struct pcmcia_io_handle *pcihp)
   2058 {
   2059 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2060 	bus_addr_t ioaddr;
   2061 	int flags = 0;
   2062 	bus_space_tag_t iot;
   2063 	bus_space_handle_t ioh;
   2064 	bus_addr_t mask;
   2065 #if rbus
   2066 	rbus_tag_t rb;
   2067 #endif
   2068 	if (align == 0) {
   2069 		align = size;	       /* XXX: funny??? */
   2070 	}
   2071 
   2072 	if (start != 0) {
   2073 		/* XXX: assume all card decode lower 10 bits by its hardware */
   2074 		mask = 0x3ff;
   2075 		/* enforce to use only masked address */
   2076 		start &= mask;
   2077 	} else {
   2078 		/*
   2079 		 * calculate mask:
   2080 		 *  1. get the most significant bit of size (call it msb).
   2081 		 *  2. compare msb with the value of size.
   2082 		 *  3. if size is larger, shift msb left once.
   2083 		 *  4. obtain mask value to decrement msb.
   2084 		 */
   2085 		bus_size_t size_tmp = size;
   2086 		int shifts = 0;
   2087 
   2088 		mask = 1;
   2089 		while (size_tmp) {
   2090 			++shifts;
   2091 			size_tmp >>= 1;
   2092 		}
   2093 		mask = (1 << shifts);
   2094 		if (mask < size) {
   2095 			mask <<= 1;
   2096 		}
   2097 		--mask;
   2098 	}
   2099 
   2100 	/*
   2101 	 * Allocate some arbitrary I/O space.
   2102 	 */
   2103 
   2104 	iot = ph->ph_parent->sc_iot;
   2105 
   2106 #if rbus
   2107 	rb = ph->ph_parent->sc_rbus_iot;
   2108 	if (rbus_space_alloc(rb, start, size, mask, align, 0, &ioaddr, &ioh)) {
   2109 		return 1;
   2110 	}
   2111 	DPRINTF(("pccbb_pcmcia_io_alloc alloc port 0x%lx+0x%lx\n",
   2112 	    (u_long) ioaddr, (u_long) size));
   2113 #else
   2114 	if (start) {
   2115 		ioaddr = start;
   2116 		if (bus_space_map(iot, start, size, 0, &ioh)) {
   2117 			return 1;
   2118 		}
   2119 		DPRINTF(("pccbb_pcmcia_io_alloc map port 0x%lx+0x%lx\n",
   2120 		    (u_long) ioaddr, (u_long) size));
   2121 	} else {
   2122 		flags |= PCMCIA_IO_ALLOCATED;
   2123 		if (bus_space_alloc(iot, 0x700 /* ph->sc->sc_iobase */ ,
   2124 		    0x800,	/* ph->sc->sc_iobase + ph->sc->sc_iosize */
   2125 		    size, align, 0, 0, &ioaddr, &ioh)) {
   2126 			/* No room be able to be get. */
   2127 			return 1;
   2128 		}
   2129 		DPRINTF(("pccbb_pcmmcia_io_alloc alloc port 0x%lx+0x%lx\n",
   2130 		    (u_long) ioaddr, (u_long) size));
   2131 	}
   2132 #endif
   2133 
   2134 	pcihp->iot = iot;
   2135 	pcihp->ioh = ioh;
   2136 	pcihp->addr = ioaddr;
   2137 	pcihp->size = size;
   2138 	pcihp->flags = flags;
   2139 
   2140 	return 0;
   2141 }
   2142 
   2143 /*
   2144  * STATIC int pccbb_pcmcia_io_free(pcmcia_chipset_handle_t pch,
   2145  *                                 struct pcmcia_io_handle *pcihp)
   2146  *
   2147  * This function only frees I/O region for pccard.
   2148  *
   2149  * XXX: The interface of this function is not very good, I believe.
   2150  */
   2151 void
   2152 pccbb_pcmcia_io_free(pcmcia_chipset_handle_t pch,
   2153     struct pcmcia_io_handle *pcihp)
   2154 {
   2155 #if !rbus
   2156 	bus_space_tag_t iot = pcihp->iot;
   2157 #endif
   2158 	bus_space_handle_t ioh = pcihp->ioh;
   2159 	bus_size_t size = pcihp->size;
   2160 
   2161 #if rbus
   2162 	struct pccbb_softc *sc =
   2163 	    ((struct pcic_handle *)pch)->ph_parent;
   2164 	rbus_tag_t rb = sc->sc_rbus_iot;
   2165 
   2166 	rbus_space_free(rb, ioh, size, NULL);
   2167 #else
   2168 	if (pcihp->flags & PCMCIA_IO_ALLOCATED)
   2169 		bus_space_free(iot, ioh, size);
   2170 	else
   2171 		bus_space_unmap(iot, ioh, size);
   2172 #endif
   2173 }
   2174 
   2175 /*
   2176  * STATIC int pccbb_pcmcia_io_map(pcmcia_chipset_handle_t pch, int width,
   2177  *                                bus_addr_t offset, bus_size_t size,
   2178  *                                struct pcmcia_io_handle *pcihp,
   2179  *                                int *windowp)
   2180  *
   2181  * This function maps the allocated I/O region to pccard. This function
   2182  * never allocates any I/O region for pccard I/O area.  I don't
   2183  * understand why the original authors of pcmciabus separated alloc and
   2184  * map.  I believe the two must be unite.
   2185  *
   2186  * XXX: no wait timing control?
   2187  */
   2188 int
   2189 pccbb_pcmcia_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
   2190     bus_size_t size, struct pcmcia_io_handle *pcihp, int *windowp)
   2191 {
   2192 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2193 	bus_addr_t ioaddr = pcihp->addr + offset;
   2194 	int i, win;
   2195 #if defined CBB_DEBUG
   2196 	static const char *width_names[] = { "dynamic", "io8", "io16" };
   2197 #endif
   2198 
   2199 	/* Sanity check I/O handle. */
   2200 
   2201 	if (ph->ph_parent->sc_iot != pcihp->iot) {
   2202 		panic("pccbb_pcmcia_io_map iot is bogus");
   2203 	}
   2204 
   2205 	/* XXX Sanity check offset/size. */
   2206 
   2207 	win = -1;
   2208 	for (i = 0; i < PCIC_IO_WINS; i++) {
   2209 		if ((ph->ioalloc & (1 << i)) == 0) {
   2210 			win = i;
   2211 			ph->ioalloc |= (1 << i);
   2212 			break;
   2213 		}
   2214 	}
   2215 
   2216 	if (win == -1) {
   2217 		return 1;
   2218 	}
   2219 
   2220 	*windowp = win;
   2221 
   2222 	/* XXX this is pretty gross */
   2223 
   2224 	DPRINTF(("pccbb_pcmcia_io_map window %d %s port %lx+%lx\n",
   2225 	    win, width_names[width], (u_long) ioaddr, (u_long) size));
   2226 
   2227 	/* XXX wtf is this doing here? */
   2228 
   2229 #if 0
   2230 	printf(" port 0x%lx", (u_long) ioaddr);
   2231 	if (size > 1) {
   2232 		printf("-0x%lx", (u_long) ioaddr + (u_long) size - 1);
   2233 	}
   2234 #endif
   2235 
   2236 	ph->io[win].addr = ioaddr;
   2237 	ph->io[win].size = size;
   2238 	ph->io[win].width = width;
   2239 
   2240 	/* actual dirty register-value changing in the function below. */
   2241 	pccbb_pcmcia_do_io_map(ph, win);
   2242 
   2243 	return 0;
   2244 }
   2245 
   2246 /*
   2247  * STATIC void pccbb_pcmcia_do_io_map(struct pcic_handle *h, int win)
   2248  *
   2249  * This function changes register-value to map I/O region for pccard.
   2250  */
   2251 static void
   2252 pccbb_pcmcia_do_io_map(struct pcic_handle *ph, int win)
   2253 {
   2254 	static u_int8_t pcic_iowidth[3] = {
   2255 		PCIC_IOCTL_IO0_IOCS16SRC_CARD,
   2256 		PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
   2257 		    PCIC_IOCTL_IO0_DATASIZE_8BIT,
   2258 		PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
   2259 		    PCIC_IOCTL_IO0_DATASIZE_16BIT,
   2260 	};
   2261 
   2262 #define PCIC_SIA_START_LOW 0
   2263 #define PCIC_SIA_START_HIGH 1
   2264 #define PCIC_SIA_STOP_LOW 2
   2265 #define PCIC_SIA_STOP_HIGH 3
   2266 
   2267 	int regbase_win = 0x8 + win * 0x04;
   2268 	u_int8_t ioctl, enable;
   2269 
   2270 	DPRINTF(("pccbb_pcmcia_do_io_map win %d addr 0x%lx size 0x%lx "
   2271 	    "width %d\n", win, (unsigned long)ph->io[win].addr,
   2272 	    (unsigned long)ph->io[win].size, ph->io[win].width * 8));
   2273 
   2274 	Pcic_write(ph, regbase_win + PCIC_SIA_START_LOW,
   2275 	    ph->io[win].addr & 0xff);
   2276 	Pcic_write(ph, regbase_win + PCIC_SIA_START_HIGH,
   2277 	    (ph->io[win].addr >> 8) & 0xff);
   2278 
   2279 	Pcic_write(ph, regbase_win + PCIC_SIA_STOP_LOW,
   2280 	    (ph->io[win].addr + ph->io[win].size - 1) & 0xff);
   2281 	Pcic_write(ph, regbase_win + PCIC_SIA_STOP_HIGH,
   2282 	    ((ph->io[win].addr + ph->io[win].size - 1) >> 8) & 0xff);
   2283 
   2284 	ioctl = Pcic_read(ph, PCIC_IOCTL);
   2285 	enable = Pcic_read(ph, PCIC_ADDRWIN_ENABLE);
   2286 	switch (win) {
   2287 	case 0:
   2288 		ioctl &= ~(PCIC_IOCTL_IO0_WAITSTATE | PCIC_IOCTL_IO0_ZEROWAIT |
   2289 		    PCIC_IOCTL_IO0_IOCS16SRC_MASK |
   2290 		    PCIC_IOCTL_IO0_DATASIZE_MASK);
   2291 		ioctl |= pcic_iowidth[ph->io[win].width];
   2292 		enable |= PCIC_ADDRWIN_ENABLE_IO0;
   2293 		break;
   2294 	case 1:
   2295 		ioctl &= ~(PCIC_IOCTL_IO1_WAITSTATE | PCIC_IOCTL_IO1_ZEROWAIT |
   2296 		    PCIC_IOCTL_IO1_IOCS16SRC_MASK |
   2297 		    PCIC_IOCTL_IO1_DATASIZE_MASK);
   2298 		ioctl |= (pcic_iowidth[ph->io[win].width] << 4);
   2299 		enable |= PCIC_ADDRWIN_ENABLE_IO1;
   2300 		break;
   2301 	}
   2302 	Pcic_write(ph, PCIC_IOCTL, ioctl);
   2303 	Pcic_write(ph, PCIC_ADDRWIN_ENABLE, enable);
   2304 #if defined(CBB_DEBUG)
   2305 	{
   2306 		u_int8_t start_low =
   2307 		    Pcic_read(ph, regbase_win + PCIC_SIA_START_LOW);
   2308 		u_int8_t start_high =
   2309 		    Pcic_read(ph, regbase_win + PCIC_SIA_START_HIGH);
   2310 		u_int8_t stop_low =
   2311 		    Pcic_read(ph, regbase_win + PCIC_SIA_STOP_LOW);
   2312 		u_int8_t stop_high =
   2313 		    Pcic_read(ph, regbase_win + PCIC_SIA_STOP_HIGH);
   2314 		printf("pccbb_pcmcia_do_io_map start %02x %02x, "
   2315 		    "stop %02x %02x, ioctl %02x enable %02x\n",
   2316 		    start_low, start_high, stop_low, stop_high, ioctl, enable);
   2317 	}
   2318 #endif
   2319 }
   2320 
   2321 /*
   2322  * STATIC void pccbb_pcmcia_io_unmap(pcmcia_chipset_handle_t *h, int win)
   2323  *
   2324  * This function unmaps I/O region.  No return value.
   2325  */
   2326 STATIC void
   2327 pccbb_pcmcia_io_unmap(pcmcia_chipset_handle_t pch, int win)
   2328 {
   2329 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2330 	int reg;
   2331 
   2332 	if (win >= PCIC_IO_WINS || win < 0) {
   2333 		panic("pccbb_pcmcia_io_unmap: window out of range");
   2334 	}
   2335 
   2336 	reg = Pcic_read(ph, PCIC_ADDRWIN_ENABLE);
   2337 	switch (win) {
   2338 	case 0:
   2339 		reg &= ~PCIC_ADDRWIN_ENABLE_IO0;
   2340 		break;
   2341 	case 1:
   2342 		reg &= ~PCIC_ADDRWIN_ENABLE_IO1;
   2343 		break;
   2344 	}
   2345 	Pcic_write(ph, PCIC_ADDRWIN_ENABLE, reg);
   2346 
   2347 	ph->ioalloc &= ~(1 << win);
   2348 }
   2349 
   2350 static int
   2351 pccbb_pcmcia_wait_ready(struct pcic_handle *ph)
   2352 {
   2353 	u_int8_t stat;
   2354 	int i;
   2355 
   2356 	/* wait an initial 10ms for quick cards */
   2357 	stat = Pcic_read(ph, PCIC_IF_STATUS);
   2358 	if (stat & PCIC_IF_STATUS_READY)
   2359 		return (0);
   2360 	pccbb_pcmcia_delay(ph, 10, "pccwr0");
   2361 	for (i = 0; i < 50; i++) {
   2362 		stat = Pcic_read(ph, PCIC_IF_STATUS);
   2363 		if (stat & PCIC_IF_STATUS_READY)
   2364 			return (0);
   2365 		if ((stat & PCIC_IF_STATUS_CARDDETECT_MASK) !=
   2366 		    PCIC_IF_STATUS_CARDDETECT_PRESENT)
   2367 			return (ENXIO);
   2368 		/* wait .1s (100ms) each iteration now */
   2369 		pccbb_pcmcia_delay(ph, 100, "pccwr1");
   2370 	}
   2371 
   2372 	printf("pccbb_pcmcia_wait_ready: ready never happened, status=%02x\n", stat);
   2373 	return (EWOULDBLOCK);
   2374 }
   2375 
   2376 /*
   2377  * Perform long (msec order) delay.  timo is in milliseconds.
   2378  */
   2379 static void
   2380 pccbb_pcmcia_delay(struct pcic_handle *ph, int timo, const char *wmesg)
   2381 {
   2382 #ifdef DIAGNOSTIC
   2383 	if (timo <= 0)
   2384 		panic("pccbb_pcmcia_delay: called with timeout %d", timo);
   2385 	if (!curlwp)
   2386 		panic("pccbb_pcmcia_delay: called in interrupt context");
   2387 #if 0
   2388 	if (!ph->event_thread)
   2389 		panic("pccbb_pcmcia_delay: no event thread");
   2390 #endif
   2391 #endif
   2392 	DPRINTF(("pccbb_pcmcia_delay: \"%s\" %p, sleep %d ms\n",
   2393 	    wmesg, ph->event_thread, timo));
   2394 	tsleep(pccbb_pcmcia_delay, PWAIT, wmesg, roundup(timo * hz, 1000) / 1000);
   2395 }
   2396 
   2397 /*
   2398  * STATIC void pccbb_pcmcia_socket_enable(pcmcia_chipset_handle_t pch)
   2399  *
   2400  * This function enables the card.  All information is stored in
   2401  * the first argument, pcmcia_chipset_handle_t.
   2402  */
   2403 STATIC void
   2404 pccbb_pcmcia_socket_enable(pcmcia_chipset_handle_t pch)
   2405 {
   2406 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2407 	struct pccbb_softc *sc = ph->ph_parent;
   2408 	pcireg_t spsr;
   2409 	int voltage;
   2410 	int win;
   2411 	u_int8_t power, intr;
   2412 #ifdef DIAGNOSTIC
   2413 	int reg;
   2414 #endif
   2415 
   2416 	/* this bit is mostly stolen from pcic_attach_card */
   2417 
   2418 	DPRINTF(("pccbb_pcmcia_socket_enable: "));
   2419 
   2420 	/* get card Vcc info */
   2421 	spsr =
   2422 	    bus_space_read_4(sc->sc_base_memt, sc->sc_base_memh,
   2423 	    CB_SOCKET_STAT);
   2424 	if (spsr & CB_SOCKET_STAT_5VCARD) {
   2425 		DPRINTF(("5V card\n"));
   2426 		voltage = CARDBUS_VCC_5V | CARDBUS_VPP_VCC;
   2427 	} else if (spsr & CB_SOCKET_STAT_3VCARD) {
   2428 		DPRINTF(("3V card\n"));
   2429 		voltage = CARDBUS_VCC_3V | CARDBUS_VPP_VCC;
   2430 	} else {
   2431 		DPRINTF(("?V card, 0x%x\n", spsr));	/* XXX */
   2432 		return;
   2433 	}
   2434 
   2435 	/* disable interrupts; assert RESET */
   2436 	intr = Pcic_read(ph, PCIC_INTR);
   2437 	intr &= PCIC_INTR_ENABLE;
   2438 	Pcic_write(ph, PCIC_INTR, intr);
   2439 
   2440 	/* zero out the address windows */
   2441 	Pcic_write(ph, PCIC_ADDRWIN_ENABLE, 0);
   2442 
   2443 	/* power down the socket to reset it, clear the card reset pin */
   2444 	pccbb_power(sc, CARDBUS_VCC_0V | CARDBUS_VPP_0V);
   2445 
   2446 	/* power off; assert output enable bit */
   2447 	power = PCIC_PWRCTL_OE;
   2448 	Pcic_write(ph, PCIC_PWRCTL, power);
   2449 
   2450 	/* power up the socket */
   2451 	if (pccbb_power(sc, voltage) == 0)
   2452 		return;
   2453 
   2454 	/*
   2455 	 * Table 4-18 and figure 4-6 of the PC Card specifiction say:
   2456 	 * Vcc Rising Time (Tpr) = 100ms (handled in pccbb_power() above)
   2457 	 * RESET Width (Th (Hi-z RESET)) = 1ms
   2458 	 * RESET Width (Tw (RESET)) = 10us
   2459 	 *
   2460 	 * some machines require some more time to be settled
   2461 	 * for example old toshiba topic bridges!
   2462 	 * (100ms is added here).
   2463 	 */
   2464 	pccbb_pcmcia_delay(ph, 200 + 1, "pccen1");
   2465 
   2466 	/* negate RESET */
   2467 	intr |= PCIC_INTR_RESET;
   2468 	Pcic_write(ph, PCIC_INTR, intr);
   2469 
   2470 	/*
   2471 	 * RESET Setup Time (Tsu (RESET)) = 20ms
   2472 	 */
   2473 	pccbb_pcmcia_delay(ph, 20, "pccen2");
   2474 
   2475 #ifdef DIAGNOSTIC
   2476 	reg = Pcic_read(ph, PCIC_IF_STATUS);
   2477 	if ((reg & PCIC_IF_STATUS_POWERACTIVE) == 0)
   2478 		printf("pccbb_pcmcia_socket_enable: no power, status=%x\n", reg);
   2479 #endif
   2480 
   2481 	/* wait for the chip to finish initializing */
   2482 	if (pccbb_pcmcia_wait_ready(ph)) {
   2483 #ifdef DIAGNOSTIC
   2484 		printf("pccbb_pcmcia_socket_enable: never became ready\n");
   2485 #endif
   2486 		/* XXX return a failure status?? */
   2487 		pccbb_power(sc, CARDBUS_VCC_0V | CARDBUS_VPP_0V);
   2488 		Pcic_write(ph, PCIC_PWRCTL, 0);
   2489 		return;
   2490 	}
   2491 
   2492 	/* reinstall all the memory and io mappings */
   2493 	for (win = 0; win < PCIC_MEM_WINS; ++win)
   2494 		if (ph->memalloc & (1 << win))
   2495 			pccbb_pcmcia_do_mem_map(ph, win);
   2496 	for (win = 0; win < PCIC_IO_WINS; ++win)
   2497 		if (ph->ioalloc & (1 << win))
   2498 			pccbb_pcmcia_do_io_map(ph, win);
   2499 }
   2500 
   2501 /*
   2502  * STATIC void pccbb_pcmcia_socket_disable(pcmcia_chipset_handle_t *ph)
   2503  *
   2504  * This function disables the card.  All information is stored in
   2505  * the first argument, pcmcia_chipset_handle_t.
   2506  */
   2507 STATIC void
   2508 pccbb_pcmcia_socket_disable(pcmcia_chipset_handle_t pch)
   2509 {
   2510 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2511 	struct pccbb_softc *sc = ph->ph_parent;
   2512 	u_int8_t intr;
   2513 
   2514 	DPRINTF(("pccbb_pcmcia_socket_disable\n"));
   2515 
   2516 	/* disable interrupts; assert RESET */
   2517 	intr = Pcic_read(ph, PCIC_INTR);
   2518 	intr &= PCIC_INTR_ENABLE;
   2519 	Pcic_write(ph, PCIC_INTR, intr);
   2520 
   2521 	/* zero out the address windows */
   2522 	Pcic_write(ph, PCIC_ADDRWIN_ENABLE, 0);
   2523 
   2524 	/* power down the socket to reset it, clear the card reset pin */
   2525 	pccbb_power(sc, CARDBUS_VCC_0V | CARDBUS_VPP_0V);
   2526 
   2527 	/* disable socket: negate output enable bit and power off */
   2528 	Pcic_write(ph, PCIC_PWRCTL, 0);
   2529 
   2530 	/*
   2531 	 * Vcc Falling Time (Tpf) = 300ms
   2532 	 */
   2533 	pccbb_pcmcia_delay(ph, 300, "pccwr1");
   2534 }
   2535 
   2536 STATIC void
   2537 pccbb_pcmcia_socket_settype(pcmcia_chipset_handle_t pch, int type)
   2538 {
   2539 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2540 	u_int8_t intr;
   2541 
   2542 	/* set the card type */
   2543 
   2544 	intr = Pcic_read(ph, PCIC_INTR);
   2545 	intr &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_CARDTYPE_MASK);
   2546 	if (type == PCMCIA_IFTYPE_IO)
   2547 		intr |= PCIC_INTR_CARDTYPE_IO;
   2548 	else
   2549 		intr |= PCIC_INTR_CARDTYPE_MEM;
   2550 	Pcic_write(ph, PCIC_INTR, intr);
   2551 
   2552 	DPRINTF(("%s: pccbb_pcmcia_socket_settype %02x type %s %02x\n",
   2553 	    device_xname(ph->ph_parent->sc_dev),
   2554 	    ph->sock, ((type == PCMCIA_IFTYPE_IO) ? "io" : "mem"), intr));
   2555 }
   2556 
   2557 /*
   2558  * STATIC int pccbb_pcmcia_card_detect(pcmcia_chipset_handle_t *ph)
   2559  *
   2560  * This function detects whether a card is in the slot or not.
   2561  * If a card is inserted, return 1.  Otherwise, return 0.
   2562  */
   2563 STATIC int
   2564 pccbb_pcmcia_card_detect(pcmcia_chipset_handle_t pch)
   2565 {
   2566 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2567 	struct pccbb_softc *sc = ph->ph_parent;
   2568 
   2569 	DPRINTF(("pccbb_pcmcia_card_detect\n"));
   2570 	return pccbb_detect_card(sc) == 1 ? 1 : 0;
   2571 }
   2572 
   2573 #if 0
   2574 STATIC int
   2575 pccbb_new_pcmcia_mem_alloc(pcmcia_chipset_handle_t pch,
   2576     bus_addr_t start, bus_size_t size, bus_size_t align, int speed, int flags,
   2577     bus_space_tag_t * memtp bus_space_handle_t * memhp)
   2578 #endif
   2579 /*
   2580  * STATIC int pccbb_pcmcia_mem_alloc(pcmcia_chipset_handle_t pch,
   2581  *                                   bus_size_t size,
   2582  *                                   struct pcmcia_mem_handle *pcmhp)
   2583  *
   2584  * This function only allocates memory region for pccard. This
   2585  * function never maps the allocated region to pccard memory area.
   2586  *
   2587  * XXX: Why the argument of start address is not in?
   2588  */
   2589 STATIC int
   2590 pccbb_pcmcia_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
   2591     struct pcmcia_mem_handle *pcmhp)
   2592 {
   2593 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2594 	bus_space_handle_t memh;
   2595 	bus_addr_t addr;
   2596 	bus_size_t sizepg;
   2597 	struct pccbb_softc *sc = ph->ph_parent;
   2598 #if rbus
   2599 	rbus_tag_t rb;
   2600 #endif
   2601 
   2602 	/* Check that the card is still there. */
   2603 	if ((Pcic_read(ph, PCIC_IF_STATUS) & PCIC_IF_STATUS_CARDDETECT_MASK) !=
   2604 		    PCIC_IF_STATUS_CARDDETECT_PRESENT)
   2605 		return 1;
   2606 
   2607 	/* out of sc->memh, allocate as many pages as necessary */
   2608 
   2609 	/* convert size to PCIC pages */
   2610 	/*
   2611 	 * This is not enough; when the requested region is on the page
   2612 	 * boundaries, this may calculate wrong result.
   2613 	 */
   2614 	sizepg = (size + (PCIC_MEM_PAGESIZE - 1)) / PCIC_MEM_PAGESIZE;
   2615 #if 0
   2616 	if (sizepg > PCIC_MAX_MEM_PAGES) {
   2617 		return 1;
   2618 	}
   2619 #endif
   2620 
   2621 	if (!(sc->sc_pcmcia_flags & PCCBB_PCMCIA_MEM_32)) {
   2622 		return 1;
   2623 	}
   2624 
   2625 	addr = 0;		       /* XXX gcc -Wuninitialized */
   2626 
   2627 #if rbus
   2628 	rb = sc->sc_rbus_memt;
   2629 	if (rbus_space_alloc(rb, 0, sizepg * PCIC_MEM_PAGESIZE,
   2630 	    sizepg * PCIC_MEM_PAGESIZE - 1, PCIC_MEM_PAGESIZE, 0,
   2631 	    &addr, &memh)) {
   2632 		return 1;
   2633 	}
   2634 #else
   2635 	if (bus_space_alloc(sc->sc_memt, sc->sc_mem_start, sc->sc_mem_end,
   2636 	    sizepg * PCIC_MEM_PAGESIZE, PCIC_MEM_PAGESIZE,
   2637 	    0, /* boundary */
   2638 	    0,	/* flags */
   2639 	    &addr, &memh)) {
   2640 		return 1;
   2641 	}
   2642 #endif
   2643 
   2644 	DPRINTF(("pccbb_pcmcia_alloc_mem: addr 0x%lx size 0x%lx, "
   2645 	    "realsize 0x%lx\n", (unsigned long)addr, (unsigned long)size,
   2646 	    (unsigned long)sizepg * PCIC_MEM_PAGESIZE));
   2647 
   2648 	pcmhp->memt = sc->sc_memt;
   2649 	pcmhp->memh = memh;
   2650 	pcmhp->addr = addr;
   2651 	pcmhp->size = size;
   2652 	pcmhp->realsize = sizepg * PCIC_MEM_PAGESIZE;
   2653 	/* What is mhandle?  I feel it is very dirty and it must go trush. */
   2654 	pcmhp->mhandle = 0;
   2655 	/* No offset???  Funny. */
   2656 
   2657 	return 0;
   2658 }
   2659 
   2660 /*
   2661  * STATIC void pccbb_pcmcia_mem_free(pcmcia_chipset_handle_t pch,
   2662  *                                   struct pcmcia_mem_handle *pcmhp)
   2663  *
   2664  * This function release the memory space allocated by the function
   2665  * pccbb_pcmcia_mem_alloc().
   2666  */
   2667 STATIC void
   2668 pccbb_pcmcia_mem_free(pcmcia_chipset_handle_t pch,
   2669     struct pcmcia_mem_handle *pcmhp)
   2670 {
   2671 #if rbus
   2672 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2673 	struct pccbb_softc *sc = ph->ph_parent;
   2674 
   2675 	rbus_space_free(sc->sc_rbus_memt, pcmhp->memh, pcmhp->realsize, NULL);
   2676 #else
   2677 	bus_space_free(pcmhp->memt, pcmhp->memh, pcmhp->realsize);
   2678 #endif
   2679 }
   2680 
   2681 /*
   2682  * STATIC void pccbb_pcmcia_do_mem_map(struct pcic_handle *ph, int win)
   2683  *
   2684  * This function release the memory space allocated by the function
   2685  * pccbb_pcmcia_mem_alloc().
   2686  */
   2687 STATIC void
   2688 pccbb_pcmcia_do_mem_map(struct pcic_handle *ph, int win)
   2689 {
   2690 	int regbase_win;
   2691 	bus_addr_t phys_addr;
   2692 	bus_addr_t phys_end;
   2693 
   2694 #define PCIC_SMM_START_LOW 0
   2695 #define PCIC_SMM_START_HIGH 1
   2696 #define PCIC_SMM_STOP_LOW 2
   2697 #define PCIC_SMM_STOP_HIGH 3
   2698 #define PCIC_CMA_LOW 4
   2699 #define PCIC_CMA_HIGH 5
   2700 
   2701 	u_int8_t start_low, start_high = 0;
   2702 	u_int8_t stop_low, stop_high;
   2703 	u_int8_t off_low, off_high;
   2704 	u_int8_t mem_window;
   2705 	int reg;
   2706 
   2707 	int kind = ph->mem[win].kind & ~PCMCIA_WIDTH_MEM_MASK;
   2708 	int mem8 =
   2709 	    (ph->mem[win].kind & PCMCIA_WIDTH_MEM_MASK) == PCMCIA_WIDTH_MEM8
   2710 	    || (kind == PCMCIA_MEM_ATTR);
   2711 
   2712 	regbase_win = 0x10 + win * 0x08;
   2713 
   2714 	phys_addr = ph->mem[win].addr;
   2715 	phys_end = phys_addr + ph->mem[win].size;
   2716 
   2717 	DPRINTF(("pccbb_pcmcia_do_mem_map: start 0x%lx end 0x%lx off 0x%lx\n",
   2718 	    (unsigned long)phys_addr, (unsigned long)phys_end,
   2719 	    (unsigned long)ph->mem[win].offset));
   2720 
   2721 #define PCIC_MEMREG_LSB_SHIFT PCIC_SYSMEM_ADDRX_SHIFT
   2722 #define PCIC_MEMREG_MSB_SHIFT (PCIC_SYSMEM_ADDRX_SHIFT + 8)
   2723 #define PCIC_MEMREG_WIN_SHIFT (PCIC_SYSMEM_ADDRX_SHIFT + 12)
   2724 
   2725 	/* bit 19:12 */
   2726 	start_low = (phys_addr >> PCIC_MEMREG_LSB_SHIFT) & 0xff;
   2727 	/* bit 23:20 and bit 7 on */
   2728 	start_high = ((phys_addr >> PCIC_MEMREG_MSB_SHIFT) & 0x0f)
   2729 	    |(mem8 ? 0 : PCIC_SYSMEM_ADDRX_START_MSB_DATASIZE_16BIT);
   2730 	/* bit 31:24, for 32-bit address */
   2731 	mem_window = (phys_addr >> PCIC_MEMREG_WIN_SHIFT) & 0xff;
   2732 
   2733 	Pcic_write(ph, regbase_win + PCIC_SMM_START_LOW, start_low);
   2734 	Pcic_write(ph, regbase_win + PCIC_SMM_START_HIGH, start_high);
   2735 
   2736 	if (ph->ph_parent->sc_pcmcia_flags & PCCBB_PCMCIA_MEM_32) {
   2737 		Pcic_write(ph, 0x40 + win, mem_window);
   2738 	}
   2739 
   2740 	stop_low = (phys_end >> PCIC_MEMREG_LSB_SHIFT) & 0xff;
   2741 	stop_high = ((phys_end >> PCIC_MEMREG_MSB_SHIFT) & 0x0f)
   2742 	    | PCIC_SYSMEM_ADDRX_STOP_MSB_WAIT2;	/* wait 2 cycles */
   2743 	/* XXX Geee, WAIT2!! Crazy!!  I must rewrite this routine. */
   2744 
   2745 	Pcic_write(ph, regbase_win + PCIC_SMM_STOP_LOW, stop_low);
   2746 	Pcic_write(ph, regbase_win + PCIC_SMM_STOP_HIGH, stop_high);
   2747 
   2748 	off_low = (ph->mem[win].offset >> PCIC_CARDMEM_ADDRX_SHIFT) & 0xff;
   2749 	off_high = ((ph->mem[win].offset >> (PCIC_CARDMEM_ADDRX_SHIFT + 8))
   2750 	    & PCIC_CARDMEM_ADDRX_MSB_ADDR_MASK)
   2751 	    | ((kind == PCMCIA_MEM_ATTR) ?
   2752 	    PCIC_CARDMEM_ADDRX_MSB_REGACTIVE_ATTR : 0);
   2753 
   2754 	Pcic_write(ph, regbase_win + PCIC_CMA_LOW, off_low);
   2755 	Pcic_write(ph, regbase_win + PCIC_CMA_HIGH, off_high);
   2756 
   2757 	reg = Pcic_read(ph, PCIC_ADDRWIN_ENABLE);
   2758 	reg |= ((1 << win) | PCIC_ADDRWIN_ENABLE_MEMCS16);
   2759 	Pcic_write(ph, PCIC_ADDRWIN_ENABLE, reg);
   2760 
   2761 #if defined(CBB_DEBUG)
   2762 	{
   2763 		int r1, r2, r3, r4, r5, r6, r7 = 0;
   2764 
   2765 		r1 = Pcic_read(ph, regbase_win + PCIC_SMM_START_LOW);
   2766 		r2 = Pcic_read(ph, regbase_win + PCIC_SMM_START_HIGH);
   2767 		r3 = Pcic_read(ph, regbase_win + PCIC_SMM_STOP_LOW);
   2768 		r4 = Pcic_read(ph, regbase_win + PCIC_SMM_STOP_HIGH);
   2769 		r5 = Pcic_read(ph, regbase_win + PCIC_CMA_LOW);
   2770 		r6 = Pcic_read(ph, regbase_win + PCIC_CMA_HIGH);
   2771 		if (ph->ph_parent->sc_pcmcia_flags & PCCBB_PCMCIA_MEM_32) {
   2772 			r7 = Pcic_read(ph, 0x40 + win);
   2773 		}
   2774 
   2775 		printf("pccbb_pcmcia_do_mem_map window %d: %02x%02x %02x%02x "
   2776 		    "%02x%02x", win, r1, r2, r3, r4, r5, r6);
   2777 		if (ph->ph_parent->sc_pcmcia_flags & PCCBB_PCMCIA_MEM_32) {
   2778 			printf(" %02x", r7);
   2779 		}
   2780 		printf("\n");
   2781 	}
   2782 #endif
   2783 }
   2784 
   2785 /*
   2786  * STATIC int pccbb_pcmcia_mem_map(pcmcia_chipset_handle_t pch, int kind,
   2787  *                                 bus_addr_t card_addr, bus_size_t size,
   2788  *                                 struct pcmcia_mem_handle *pcmhp,
   2789  *                                 bus_addr_t *offsetp, int *windowp)
   2790  *
   2791  * This function maps memory space allocated by the function
   2792  * pccbb_pcmcia_mem_alloc().
   2793  */
   2794 STATIC int
   2795 pccbb_pcmcia_mem_map(pcmcia_chipset_handle_t pch, int kind,
   2796     bus_addr_t card_addr, bus_size_t size, struct pcmcia_mem_handle *pcmhp,
   2797     bus_addr_t *offsetp, int *windowp)
   2798 {
   2799 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2800 	bus_addr_t busaddr;
   2801 	long card_offset;
   2802 	int win;
   2803 
   2804 	/* Check that the card is still there. */
   2805 	if ((Pcic_read(ph, PCIC_IF_STATUS) & PCIC_IF_STATUS_CARDDETECT_MASK) !=
   2806 		    PCIC_IF_STATUS_CARDDETECT_PRESENT)
   2807 		return 1;
   2808 
   2809 	for (win = 0; win < PCIC_MEM_WINS; ++win) {
   2810 		if ((ph->memalloc & (1 << win)) == 0) {
   2811 			ph->memalloc |= (1 << win);
   2812 			break;
   2813 		}
   2814 	}
   2815 
   2816 	if (win == PCIC_MEM_WINS) {
   2817 		return 1;
   2818 	}
   2819 
   2820 	*windowp = win;
   2821 
   2822 	/* XXX this is pretty gross */
   2823 
   2824 	if (ph->ph_parent->sc_memt != pcmhp->memt) {
   2825 		panic("pccbb_pcmcia_mem_map memt is bogus");
   2826 	}
   2827 
   2828 	busaddr = pcmhp->addr;
   2829 
   2830 	/*
   2831 	 * compute the address offset to the pcmcia address space for the
   2832 	 * pcic.  this is intentionally signed.  The masks and shifts below
   2833 	 * will cause TRT to happen in the pcic registers.  Deal with making
   2834 	 * sure the address is aligned, and return the alignment offset.
   2835 	 */
   2836 
   2837 	*offsetp = card_addr % PCIC_MEM_PAGESIZE;
   2838 	card_addr -= *offsetp;
   2839 
   2840 	DPRINTF(("pccbb_pcmcia_mem_map window %d bus %lx+%lx+%lx at card addr "
   2841 	    "%lx\n", win, (u_long) busaddr, (u_long) * offsetp, (u_long) size,
   2842 	    (u_long) card_addr));
   2843 
   2844 	/*
   2845 	 * include the offset in the size, and decrement size by one, since
   2846 	 * the hw wants start/stop
   2847 	 */
   2848 	size += *offsetp - 1;
   2849 
   2850 	card_offset = (((long)card_addr) - ((long)busaddr));
   2851 
   2852 	ph->mem[win].addr = busaddr;
   2853 	ph->mem[win].size = size;
   2854 	ph->mem[win].offset = card_offset;
   2855 	ph->mem[win].kind = kind;
   2856 
   2857 	pccbb_pcmcia_do_mem_map(ph, win);
   2858 
   2859 	return 0;
   2860 }
   2861 
   2862 /*
   2863  * STATIC int pccbb_pcmcia_mem_unmap(pcmcia_chipset_handle_t pch,
   2864  *                                   int window)
   2865  *
   2866  * This function unmaps memory space which mapped by the function
   2867  * pccbb_pcmcia_mem_map().
   2868  */
   2869 STATIC void
   2870 pccbb_pcmcia_mem_unmap(pcmcia_chipset_handle_t pch, int window)
   2871 {
   2872 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2873 	int reg;
   2874 
   2875 	if (window >= PCIC_MEM_WINS) {
   2876 		panic("pccbb_pcmcia_mem_unmap: window out of range");
   2877 	}
   2878 
   2879 	reg = Pcic_read(ph, PCIC_ADDRWIN_ENABLE);
   2880 	reg &= ~(1 << window);
   2881 	Pcic_write(ph, PCIC_ADDRWIN_ENABLE, reg);
   2882 
   2883 	ph->memalloc &= ~(1 << window);
   2884 }
   2885 
   2886 #if defined PCCBB_PCMCIA_POLL
   2887 struct pccbb_poll_str {
   2888 	void *arg;
   2889 	int (*func)(void *);
   2890 	int level;
   2891 	struct pcic_handle *ph;
   2892 	int count;
   2893 	int num;
   2894 	struct callout poll_ch;
   2895 };
   2896 
   2897 static struct pccbb_poll_str pccbb_poll[10];
   2898 static int pccbb_poll_n = 0;
   2899 
   2900 static void pccbb_pcmcia_poll(void *arg);
   2901 
   2902 static void
   2903 pccbb_pcmcia_poll(void *arg)
   2904 {
   2905 	struct pccbb_poll_str *poll = arg;
   2906 	struct pcic_handle *ph = poll->ph;
   2907 	struct pccbb_softc *sc = ph->sc;
   2908 	int s;
   2909 	u_int32_t spsr;		       /* socket present-state reg */
   2910 
   2911 	callout_reset(&poll->poll_ch, hz * 2, pccbb_pcmcia_poll, arg);
   2912 	switch (poll->level) {
   2913 	case IPL_NET:
   2914 		s = splnet();
   2915 		break;
   2916 	case IPL_BIO:
   2917 		s = splbio();
   2918 		break;
   2919 	case IPL_TTY:		       /* fallthrough */
   2920 	default:
   2921 		s = spltty();
   2922 		break;
   2923 	}
   2924 
   2925 	spsr = bus_space_read_4(sc->sc_base_memt, sc->sc_base_memh,
   2926 	    CB_SOCKET_STAT);
   2927 
   2928 #if defined PCCBB_PCMCIA_POLL_ONLY && defined LEVEL2
   2929 	if (!(spsr & 0x40))	       /* CINT low */
   2930 #else
   2931 	if (1)
   2932 #endif
   2933 	{
   2934 		if ((*poll->func) (poll->arg) > 0) {
   2935 			++poll->count;
   2936 /*      printf("intr: reported from poller, 0x%x\n", spsr); */
   2937 #if defined LEVEL2
   2938 		} else {
   2939 			printf("intr: miss! 0x%x\n", spsr);
   2940 #endif
   2941 		}
   2942 	}
   2943 	splx(s);
   2944 }
   2945 #endif /* defined CB_PCMCIA_POLL */
   2946 
   2947 /*
   2948  * STATIC void *pccbb_pcmcia_intr_establish(pcmcia_chipset_handle_t pch,
   2949  *                                          struct pcmcia_function *pf,
   2950  *                                          int ipl,
   2951  *                                          int (*func)(void *),
   2952  *                                          void *arg);
   2953  *
   2954  * This function enables PC-Card interrupt.  PCCBB uses PCI interrupt line.
   2955  */
   2956 STATIC void *
   2957 pccbb_pcmcia_intr_establish(pcmcia_chipset_handle_t pch,
   2958     struct pcmcia_function *pf, int ipl, int (*func)(void *), void *arg)
   2959 {
   2960 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2961 	struct pccbb_softc *sc = ph->ph_parent;
   2962 
   2963 	if (!(pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)) {
   2964 		/* what should I do? */
   2965 		if ((pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)) {
   2966 			DPRINTF(("%s does not provide edge nor pulse "
   2967 			    "interrupt\n", device_xname(sc->sc_dev)));
   2968 			return NULL;
   2969 		}
   2970 		/*
   2971 		 * XXX Noooooo!  The interrupt flag must set properly!!
   2972 		 * dumb pcmcia driver!!
   2973 		 */
   2974 	}
   2975 
   2976 	return pccbb_intr_establish(sc, 0, ipl, func, arg);
   2977 }
   2978 
   2979 /*
   2980  * STATIC void pccbb_pcmcia_intr_disestablish(pcmcia_chipset_handle_t pch,
   2981  *                                            void *ih)
   2982  *
   2983  * This function disables PC-Card interrupt.
   2984  */
   2985 STATIC void
   2986 pccbb_pcmcia_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
   2987 {
   2988 	struct pcic_handle *ph = (struct pcic_handle *)pch;
   2989 	struct pccbb_softc *sc = ph->ph_parent;
   2990 
   2991 	pccbb_intr_disestablish(sc, ih);
   2992 }
   2993 
   2994 #if rbus
   2995 /*
   2996  * static int
   2997  * pccbb_rbus_cb_space_alloc(cardbus_chipset_tag_t ct, rbus_tag_t rb,
   2998  *			    bus_addr_t addr, bus_size_t size,
   2999  *			    bus_addr_t mask, bus_size_t align,
   3000  *			    int flags, bus_addr_t *addrp;
   3001  *			    bus_space_handle_t *bshp)
   3002  *
   3003  *   This function allocates a portion of memory or io space for
   3004  *   clients.  This function is called from CardBus card drivers.
   3005  */
   3006 static int
   3007 pccbb_rbus_cb_space_alloc(cardbus_chipset_tag_t ct, rbus_tag_t rb,
   3008     bus_addr_t addr, bus_size_t size, bus_addr_t mask, bus_size_t align,
   3009     int flags, bus_addr_t *addrp, bus_space_handle_t *bshp)
   3010 {
   3011 	struct pccbb_softc *sc = (struct pccbb_softc *)ct;
   3012 
   3013 	DPRINTF(("pccbb_rbus_cb_space_alloc: addr 0x%lx, size 0x%lx, "
   3014 	    "mask 0x%lx, align 0x%lx\n", (unsigned long)addr,
   3015 	    (unsigned long)size, (unsigned long)mask, (unsigned long)align));
   3016 
   3017 	if (align == 0) {
   3018 		align = size;
   3019 	}
   3020 
   3021 	if (rb->rb_bt == sc->sc_memt) {
   3022 		if (align < 16) {
   3023 			return 1;
   3024 		}
   3025 		/*
   3026 		 * XXX: align more than 0x1000 to avoid overwrapping
   3027 		 * memory windows for two or more devices.  0x1000
   3028 		 * means memory window's granularity.
   3029 		 *
   3030 		 * Two or more devices should be able to share same
   3031 		 * memory window region.  However, overrapping memory
   3032 		 * window is not good because some devices, such as
   3033 		 * 3Com 3C575[BC], have a broken address decoder and
   3034 		 * intrude other's memory region.
   3035 		 */
   3036 		if (align < 0x1000) {
   3037 			align = 0x1000;
   3038 		}
   3039 	} else if (rb->rb_bt == sc->sc_iot) {
   3040 		if (align < 4) {
   3041 			return 1;
   3042 		}
   3043 		/* XXX: hack for avoiding ISA image */
   3044 		if (mask < 0x0100) {
   3045 			mask = 0x3ff;
   3046 			addr = 0x300;
   3047 		}
   3048 
   3049 	} else {
   3050 		DPRINTF(("pccbb_rbus_cb_space_alloc: Bus space tag 0x%lx is "
   3051 		    "NOT used. io: 0x%lx, mem: 0x%lx\n",
   3052 		    (unsigned long)rb->rb_bt, (unsigned long)sc->sc_iot,
   3053 		    (unsigned long)sc->sc_memt));
   3054 		return 1;
   3055 		/* XXX: panic here? */
   3056 	}
   3057 
   3058 	if (rbus_space_alloc(rb, addr, size, mask, align, flags, addrp, bshp)) {
   3059 		aprint_normal_dev(sc->sc_dev, "<rbus> no bus space\n");
   3060 		return 1;
   3061 	}
   3062 
   3063 	pccbb_open_win(sc, rb->rb_bt, *addrp, size, *bshp, 0);
   3064 
   3065 	return 0;
   3066 }
   3067 
   3068 /*
   3069  * static int
   3070  * pccbb_rbus_cb_space_free(cardbus_chipset_tag_t *ct, rbus_tag_t rb,
   3071  *			   bus_space_handle_t *bshp, bus_size_t size);
   3072  *
   3073  *   This function is called from CardBus card drivers.
   3074  */
   3075 static int
   3076 pccbb_rbus_cb_space_free(cardbus_chipset_tag_t ct, rbus_tag_t rb,
   3077     bus_space_handle_t bsh, bus_size_t size)
   3078 {
   3079 	struct pccbb_softc *sc = (struct pccbb_softc *)ct;
   3080 	bus_space_tag_t bt = rb->rb_bt;
   3081 
   3082 	pccbb_close_win(sc, bt, bsh, size);
   3083 
   3084 	if (bt == sc->sc_memt) {
   3085 	} else if (bt == sc->sc_iot) {
   3086 	} else {
   3087 		return 1;
   3088 		/* XXX: panic here? */
   3089 	}
   3090 
   3091 	return rbus_space_free(rb, bsh, size, NULL);
   3092 }
   3093 #endif /* rbus */
   3094 
   3095 #if rbus
   3096 
   3097 static int
   3098 pccbb_open_win(struct pccbb_softc *sc, bus_space_tag_t bst, bus_addr_t addr,
   3099     bus_size_t size, bus_space_handle_t bsh, int flags)
   3100 {
   3101 	struct pccbb_win_chain_head *head;
   3102 	bus_addr_t align;
   3103 
   3104 	head = &sc->sc_iowindow;
   3105 	align = 0x04;
   3106 	if (sc->sc_memt == bst) {
   3107 		head = &sc->sc_memwindow;
   3108 		align = 0x1000;
   3109 		DPRINTF(("using memory window, 0x%lx 0x%lx 0x%lx\n\n",
   3110 		    (unsigned long)sc->sc_iot, (unsigned long)sc->sc_memt,
   3111 		    (unsigned long)bst));
   3112 	}
   3113 
   3114 	if (pccbb_winlist_insert(head, addr, size, bsh, flags)) {
   3115 		aprint_error_dev(sc->sc_dev,
   3116 		    "pccbb_open_win: %s winlist insert failed\n",
   3117 		    (head == &sc->sc_memwindow) ? "mem" : "io");
   3118 	}
   3119 	pccbb_winset(align, sc, bst);
   3120 
   3121 	return 0;
   3122 }
   3123 
   3124 static int
   3125 pccbb_close_win(struct pccbb_softc *sc, bus_space_tag_t bst,
   3126     bus_space_handle_t bsh, bus_size_t size)
   3127 {
   3128 	struct pccbb_win_chain_head *head;
   3129 	bus_addr_t align;
   3130 
   3131 	head = &sc->sc_iowindow;
   3132 	align = 0x04;
   3133 	if (sc->sc_memt == bst) {
   3134 		head = &sc->sc_memwindow;
   3135 		align = 0x1000;
   3136 	}
   3137 
   3138 	if (pccbb_winlist_delete(head, bsh, size)) {
   3139 		aprint_error_dev(sc->sc_dev,
   3140 		    "pccbb_close_win: %s winlist delete failed\n",
   3141 		    (head == &sc->sc_memwindow) ? "mem" : "io");
   3142 	}
   3143 	pccbb_winset(align, sc, bst);
   3144 
   3145 	return 0;
   3146 }
   3147 
   3148 static int
   3149 pccbb_winlist_insert(struct pccbb_win_chain_head *head, bus_addr_t start,
   3150     bus_size_t size, bus_space_handle_t bsh, int flags)
   3151 {
   3152 	struct pccbb_win_chain *chainp, *elem;
   3153 
   3154 	if ((elem = malloc(sizeof(struct pccbb_win_chain), M_DEVBUF,
   3155 	    M_NOWAIT)) == NULL)
   3156 		return (1);		/* fail */
   3157 
   3158 	elem->wc_start = start;
   3159 	elem->wc_end = start + (size - 1);
   3160 	elem->wc_handle = bsh;
   3161 	elem->wc_flags = flags;
   3162 
   3163 	TAILQ_FOREACH(chainp, head, wc_list) {
   3164 		if (chainp->wc_end >= start)
   3165 			break;
   3166 	}
   3167 	if (chainp != NULL)
   3168 		TAILQ_INSERT_AFTER(head, chainp, elem, wc_list);
   3169 	else
   3170 		TAILQ_INSERT_TAIL(head, elem, wc_list);
   3171 	return (0);
   3172 }
   3173 
   3174 static int
   3175 pccbb_winlist_delete(struct pccbb_win_chain_head *head, bus_space_handle_t bsh,
   3176     bus_size_t size)
   3177 {
   3178 	struct pccbb_win_chain *chainp;
   3179 
   3180 	TAILQ_FOREACH(chainp, head, wc_list) {
   3181 		if (memcmp(&chainp->wc_handle, &bsh, sizeof(bsh)) == 0)
   3182 			break;
   3183 	}
   3184 	if (chainp == NULL)
   3185 		return 1;	       /* fail: no candidate to remove */
   3186 
   3187 	if ((chainp->wc_end - chainp->wc_start) != (size - 1)) {
   3188 		printf("pccbb_winlist_delete: window 0x%lx size "
   3189 		    "inconsistent: 0x%lx, 0x%lx\n",
   3190 		    (unsigned long)chainp->wc_start,
   3191 		    (unsigned long)(chainp->wc_end - chainp->wc_start),
   3192 		    (unsigned long)(size - 1));
   3193 		return 1;
   3194 	}
   3195 
   3196 	TAILQ_REMOVE(head, chainp, wc_list);
   3197 	free(chainp, M_DEVBUF);
   3198 
   3199 	return 0;
   3200 }
   3201 
   3202 static void
   3203 pccbb_winset(bus_addr_t align, struct pccbb_softc *sc, bus_space_tag_t bst)
   3204 {
   3205 	pci_chipset_tag_t pc;
   3206 	pcitag_t tag;
   3207 	bus_addr_t mask = ~(align - 1);
   3208 	struct {
   3209 		cardbusreg_t win_start;
   3210 		cardbusreg_t win_limit;
   3211 		int win_flags;
   3212 	} win[2];
   3213 	struct pccbb_win_chain *chainp;
   3214 	int offs;
   3215 
   3216 	win[0].win_start = win[1].win_start = 0xffffffff;
   3217 	win[0].win_limit = win[1].win_limit = 0;
   3218 	win[0].win_flags = win[1].win_flags = 0;
   3219 
   3220 	chainp = TAILQ_FIRST(&sc->sc_iowindow);
   3221 	offs = PCI_CB_IOBASE0;
   3222 	if (sc->sc_memt == bst) {
   3223 		chainp = TAILQ_FIRST(&sc->sc_memwindow);
   3224 		offs = PCI_CB_MEMBASE0;
   3225 	}
   3226 
   3227 	if (chainp != NULL) {
   3228 		win[0].win_start = chainp->wc_start & mask;
   3229 		win[0].win_limit = chainp->wc_end & mask;
   3230 		win[0].win_flags = chainp->wc_flags;
   3231 		chainp = TAILQ_NEXT(chainp, wc_list);
   3232 	}
   3233 
   3234 	for (; chainp != NULL; chainp = TAILQ_NEXT(chainp, wc_list)) {
   3235 		if (win[1].win_start == 0xffffffff) {
   3236 			/* window 1 is not used */
   3237 			if ((win[0].win_flags == chainp->wc_flags) &&
   3238 			    (win[0].win_limit + align >=
   3239 			    (chainp->wc_start & mask))) {
   3240 				/* concatenate */
   3241 				win[0].win_limit = chainp->wc_end & mask;
   3242 			} else {
   3243 				/* make new window */
   3244 				win[1].win_start = chainp->wc_start & mask;
   3245 				win[1].win_limit = chainp->wc_end & mask;
   3246 				win[1].win_flags = chainp->wc_flags;
   3247 			}
   3248 			continue;
   3249 		}
   3250 
   3251 		/* Both windows are engaged. */
   3252 		if (win[0].win_flags == win[1].win_flags) {
   3253 			/* same flags */
   3254 			if (win[0].win_flags == chainp->wc_flags) {
   3255 				if (win[1].win_start - (win[0].win_limit +
   3256 				    align) <
   3257 				    (chainp->wc_start & mask) -
   3258 				    ((chainp->wc_end & mask) + align)) {
   3259 					/*
   3260 					 * merge window 0 and 1, and set win1
   3261 					 * to chainp
   3262 					 */
   3263 					win[0].win_limit = win[1].win_limit;
   3264 					win[1].win_start =
   3265 					    chainp->wc_start & mask;
   3266 					win[1].win_limit =
   3267 					    chainp->wc_end & mask;
   3268 				} else {
   3269 					win[1].win_limit =
   3270 					    chainp->wc_end & mask;
   3271 				}
   3272 			} else {
   3273 				/* different flags */
   3274 
   3275 				/* concatenate win0 and win1 */
   3276 				win[0].win_limit = win[1].win_limit;
   3277 				/* allocate win[1] to new space */
   3278 				win[1].win_start = chainp->wc_start & mask;
   3279 				win[1].win_limit = chainp->wc_end & mask;
   3280 				win[1].win_flags = chainp->wc_flags;
   3281 			}
   3282 		} else {
   3283 			/* the flags of win[0] and win[1] is different */
   3284 			if (win[0].win_flags == chainp->wc_flags) {
   3285 				win[0].win_limit = chainp->wc_end & mask;
   3286 				/*
   3287 				 * XXX this creates overlapping windows, so
   3288 				 * what should the poor bridge do if one is
   3289 				 * cachable, and the other is not?
   3290 				 */
   3291 				aprint_error_dev(sc->sc_dev,
   3292 				    "overlapping windows\n");
   3293 			} else {
   3294 				win[1].win_limit = chainp->wc_end & mask;
   3295 			}
   3296 		}
   3297 	}
   3298 
   3299 	pc = sc->sc_pc;
   3300 	tag = sc->sc_tag;
   3301 	pci_conf_write(pc, tag, offs, win[0].win_start);
   3302 	pci_conf_write(pc, tag, offs + 4, win[0].win_limit);
   3303 	pci_conf_write(pc, tag, offs + 8, win[1].win_start);
   3304 	pci_conf_write(pc, tag, offs + 12, win[1].win_limit);
   3305 	DPRINTF(("--pccbb_winset: win0 [0x%lx, 0x%lx), win1 [0x%lx, 0x%lx)\n",
   3306 	    (unsigned long)pci_conf_read(pc, tag, offs),
   3307 	    (unsigned long)pci_conf_read(pc, tag, offs + 4) + align,
   3308 	    (unsigned long)pci_conf_read(pc, tag, offs + 8),
   3309 	    (unsigned long)pci_conf_read(pc, tag, offs + 12) + align));
   3310 
   3311 	if (bst == sc->sc_memt) {
   3312 		pcireg_t bcr = pci_conf_read(pc, tag, PCI_BRIDGE_CONTROL_REG);
   3313 
   3314 		bcr &= ~(CB_BCR_PREFETCH_MEMWIN0 | CB_BCR_PREFETCH_MEMWIN1);
   3315 		if (win[0].win_flags & PCCBB_MEM_CACHABLE)
   3316 			bcr |= CB_BCR_PREFETCH_MEMWIN0;
   3317 		if (win[1].win_flags & PCCBB_MEM_CACHABLE)
   3318 			bcr |= CB_BCR_PREFETCH_MEMWIN1;
   3319 		pci_conf_write(pc, tag, PCI_BRIDGE_CONTROL_REG, bcr);
   3320 	}
   3321 }
   3322 
   3323 #endif /* rbus */
   3324 
   3325 static bool
   3326 pccbb_suspend(device_t dv PMF_FN_ARGS)
   3327 {
   3328 	struct pccbb_softc *sc = device_private(dv);
   3329 	bus_space_tag_t base_memt = sc->sc_base_memt;	/* socket regs memory */
   3330 	bus_space_handle_t base_memh = sc->sc_base_memh;
   3331 	pcireg_t reg;
   3332 
   3333 	if (sc->sc_pil_intr_enable)
   3334 		(void)pccbbintr_function(sc);
   3335 	sc->sc_pil_intr_enable = 0;
   3336 
   3337 	reg = bus_space_read_4(base_memt, base_memh, CB_SOCKET_MASK);
   3338 	/* Disable interrupts. */
   3339 	reg &= ~(CB_SOCKET_MASK_CSTS | CB_SOCKET_MASK_CD | CB_SOCKET_MASK_POWER);
   3340 	bus_space_write_4(base_memt, base_memh, CB_SOCKET_MASK, reg);
   3341 	/* XXX joerg Disable power to the socket? */
   3342 
   3343 	/* XXX flush PCI write */
   3344 	bus_space_read_4(base_memt, base_memh, CB_SOCKET_EVENT);
   3345 
   3346 	/* reset interrupt */
   3347 	bus_space_write_4(base_memt, base_memh, CB_SOCKET_EVENT,
   3348 	    bus_space_read_4(base_memt, base_memh, CB_SOCKET_EVENT));
   3349 	/* XXX flush PCI write */
   3350 	bus_space_read_4(base_memt, base_memh, CB_SOCKET_EVENT);
   3351 
   3352 	if (sc->sc_ih != NULL) {
   3353 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
   3354 		sc->sc_ih = NULL;
   3355 	}
   3356 
   3357 	return true;
   3358 }
   3359 
   3360 static bool
   3361 pccbb_resume(device_t dv PMF_FN_ARGS)
   3362 {
   3363 	struct pccbb_softc *sc = device_private(dv);
   3364 	bus_space_tag_t base_memt = sc->sc_base_memt;	/* socket regs memory */
   3365 	bus_space_handle_t base_memh = sc->sc_base_memh;
   3366 	pcireg_t reg;
   3367 
   3368 	pccbb_chipinit(sc);
   3369 	pccbb_intrinit(sc);
   3370 	/* setup memory and io space window for CB */
   3371 	pccbb_winset(0x1000, sc, sc->sc_memt);
   3372 	pccbb_winset(0x04, sc, sc->sc_iot);
   3373 
   3374 	/* CSC Interrupt: Card detect interrupt on */
   3375 	reg = bus_space_read_4(base_memt, base_memh, CB_SOCKET_MASK);
   3376 	/* Card detect intr is turned on. */
   3377 	reg |= CB_SOCKET_MASK_CSTS | CB_SOCKET_MASK_CD | CB_SOCKET_MASK_POWER;
   3378 	bus_space_write_4(base_memt, base_memh, CB_SOCKET_MASK, reg);
   3379 	/* reset interrupt */
   3380 	reg = bus_space_read_4(base_memt, base_memh, CB_SOCKET_EVENT);
   3381 	bus_space_write_4(base_memt, base_memh, CB_SOCKET_EVENT, reg);
   3382 
   3383 	/*
   3384 	 * check for card insertion or removal during suspend period.
   3385 	 * XXX: the code can't cope with card swap (remove then
   3386 	 * insert).  how can we detect such situation?
   3387 	 */
   3388 	(void)pccbbintr(sc);
   3389 
   3390 	sc->sc_pil_intr_enable = 1;
   3391 
   3392 	return true;
   3393 }
   3394