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