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