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