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