xref: /freebsd/sys/dev/pccbb/pccbb.c (revision a2c4a178abf68aac1bd0f385824622b1a53e632b)
1 /*
2  * Copyright (c) 2002-2004 M. Warner Losh.
3  * Copyright (c) 2000-2001 Jonathan Chen.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * Copyright (c) 1998, 1999 and 2000
32  *      HAYAKAWA Koichi.  All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 3. All advertising materials mentioning features or use of this software
43  *    must display the following acknowledgement:
44  *	This product includes software developed by HAYAKAWA Koichi.
45  * 4. The name of the author may not be used to endorse or promote products
46  *    derived from this software without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  */
59 
60 /*
61  * Driver for PCI to CardBus Bridge chips
62  *
63  * References:
64  *  TI Datasheets:
65  *   http://www-s.ti.com/cgi-bin/sc/generic2.cgi?family=PCI+CARDBUS+CONTROLLERS
66  *
67  * Written by Jonathan Chen <jon@freebsd.org>
68  * The author would like to acknowledge:
69  *  * HAYAKAWA Koichi: Author of the NetBSD code for the same thing
70  *  * Warner Losh: Newbus/newcard guru and author of the pccard side of things
71  *  * YAMAMOTO Shigeru: Author of another FreeBSD cardbus driver
72  *  * David Cross: Author of the initial ugly hack for a specific cardbus card
73  */
74 
75 #include <sys/cdefs.h>
76 __FBSDID("$FreeBSD$");
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/proc.h>
81 #include <sys/condvar.h>
82 #include <sys/errno.h>
83 #include <sys/kernel.h>
84 #include <sys/lock.h>
85 #include <sys/malloc.h>
86 #include <sys/mutex.h>
87 #include <sys/sysctl.h>
88 #include <sys/kthread.h>
89 #include <sys/bus.h>
90 #include <machine/bus.h>
91 #include <sys/rman.h>
92 #include <machine/resource.h>
93 
94 #include <dev/pci/pcireg.h>
95 #include <dev/pci/pcivar.h>
96 #include <machine/clock.h>
97 
98 #include <dev/pccard/pccardreg.h>
99 #include <dev/pccard/pccardvar.h>
100 
101 #include <dev/exca/excareg.h>
102 #include <dev/exca/excavar.h>
103 
104 #include <dev/pccbb/pccbbreg.h>
105 #include <dev/pccbb/pccbbvar.h>
106 
107 #include "power_if.h"
108 #include "card_if.h"
109 #include "pcib_if.h"
110 
111 #define	DPRINTF(x) do { if (cbb_debug) printf x; } while (0)
112 #define	DEVPRINTF(x) do { if (cbb_debug) device_printf x; } while (0)
113 
114 #define	PCI_MASK_CONFIG(DEV,REG,MASK,SIZE)				\
115 	pci_write_config(DEV, REG, pci_read_config(DEV, REG, SIZE) MASK, SIZE)
116 #define	PCI_MASK2_CONFIG(DEV,REG,MASK1,MASK2,SIZE)			\
117 	pci_write_config(DEV, REG, (					\
118 		pci_read_config(DEV, REG, SIZE) MASK1) MASK2, SIZE)
119 
120 #define CBB_CARD_PRESENT(s) ((s & CBB_STATE_CD) == 0)
121 
122 #define CBB_START_MEM	0x88000000
123 #define CBB_START_32_IO 0x1000
124 #define CBB_START_16_IO 0x100
125 
126 struct yenta_chipinfo {
127 	uint32_t yc_id;
128 	const	char *yc_name;
129 	int	yc_chiptype;
130 } yc_chipsets[] = {
131 	/* Texas Instruments chips */
132 	{PCIC_ID_TI1031, "TI1031 PCI-PC Card Bridge", CB_TI113X},
133 	{PCIC_ID_TI1130, "TI1130 PCI-CardBus Bridge", CB_TI113X},
134 	{PCIC_ID_TI1131, "TI1131 PCI-CardBus Bridge", CB_TI113X},
135 
136 	{PCIC_ID_TI1210, "TI1210 PCI-CardBus Bridge", CB_TI12XX},
137 	{PCIC_ID_TI1211, "TI1211 PCI-CardBus Bridge", CB_TI12XX},
138 	{PCIC_ID_TI1220, "TI1220 PCI-CardBus Bridge", CB_TI12XX},
139 	{PCIC_ID_TI1221, "TI1221 PCI-CardBus Bridge", CB_TI12XX},
140 	{PCIC_ID_TI1225, "TI1225 PCI-CardBus Bridge", CB_TI12XX},
141 	{PCIC_ID_TI1250, "TI1250 PCI-CardBus Bridge", CB_TI125X},
142 	{PCIC_ID_TI1251, "TI1251 PCI-CardBus Bridge", CB_TI125X},
143 	{PCIC_ID_TI1251B,"TI1251B PCI-CardBus Bridge",CB_TI125X},
144 	{PCIC_ID_TI1260, "TI1260 PCI-CardBus Bridge", CB_TI12XX},
145 	{PCIC_ID_TI1260B,"TI1260B PCI-CardBus Bridge",CB_TI12XX},
146 	{PCIC_ID_TI1410, "TI1410 PCI-CardBus Bridge", CB_TI12XX},
147 	{PCIC_ID_TI1420, "TI1420 PCI-CardBus Bridge", CB_TI12XX},
148 	{PCIC_ID_TI1421, "TI1421 PCI-CardBus Bridge", CB_TI12XX},
149 	{PCIC_ID_TI1450, "TI1450 PCI-CardBus Bridge", CB_TI125X}, /*SIC!*/
150 	{PCIC_ID_TI1451, "TI1451 PCI-CardBus Bridge", CB_TI12XX},
151 	{PCIC_ID_TI1510, "TI1510 PCI-CardBus Bridge", CB_TI12XX},
152 	{PCIC_ID_TI1520, "TI1520 PCI-CardBus Bridge", CB_TI12XX},
153 	{PCIC_ID_TI4410, "TI4410 PCI-CardBus Bridge", CB_TI12XX},
154 	{PCIC_ID_TI4450, "TI4450 PCI-CardBus Bridge", CB_TI12XX},
155 	{PCIC_ID_TI4451, "TI4451 PCI-CardBus Bridge", CB_TI12XX},
156 	{PCIC_ID_TI4510, "TI4510 PCI-CardBus Bridge", CB_TI12XX},
157 
158 	/* ENE */
159 	{PCIC_ID_ENE_CB710, "ENE CB710 PCI-CardBus Bridge", CB_TI12XX},
160 	{PCIC_ID_ENE_CB720, "ENE CB720 PCI-CardBus Bridge", CB_TI12XX},
161 	{PCIC_ID_ENE_CB1211, "ENE CB1211 PCI-CardBus Bridge", CB_TI12XX},
162 	{PCIC_ID_ENE_CB1225, "ENE CB1225 PCI-CardBus Bridge", CB_TI12XX},
163 	{PCIC_ID_ENE_CB1410, "ENE CB1410 PCI-CardBus Bridge", CB_TI12XX},
164 	{PCIC_ID_ENE_CB1420, "ENE CB1420 PCI-CardBus Bridge", CB_TI12XX},
165 
166 	/* Ricoh chips */
167 	{PCIC_ID_RICOH_RL5C465, "RF5C465 PCI-CardBus Bridge", CB_RF5C46X},
168 	{PCIC_ID_RICOH_RL5C466, "RF5C466 PCI-CardBus Bridge", CB_RF5C46X},
169 	{PCIC_ID_RICOH_RL5C475, "RF5C475 PCI-CardBus Bridge", CB_RF5C47X},
170 	{PCIC_ID_RICOH_RL5C476, "RF5C476 PCI-CardBus Bridge", CB_RF5C47X},
171 	{PCIC_ID_RICOH_RL5C477, "RF5C477 PCI-CardBus Bridge", CB_RF5C47X},
172 	{PCIC_ID_RICOH_RL5C478, "RF5C478 PCI-CardBus Bridge", CB_RF5C47X},
173 
174 	/* Toshiba products */
175 	{PCIC_ID_TOPIC95, "ToPIC95 PCI-CardBus Bridge", CB_TOPIC95},
176 	{PCIC_ID_TOPIC95B, "ToPIC95B PCI-CardBus Bridge", CB_TOPIC95},
177 	{PCIC_ID_TOPIC97, "ToPIC97 PCI-CardBus Bridge", CB_TOPIC97},
178 	{PCIC_ID_TOPIC100, "ToPIC100 PCI-CardBus Bridge", CB_TOPIC97},
179 
180 	/* Cirrus Logic */
181 	{PCIC_ID_CLPD6832, "CLPD6832 PCI-CardBus Bridge", CB_CIRRUS},
182 	{PCIC_ID_CLPD6833, "CLPD6833 PCI-CardBus Bridge", CB_CIRRUS},
183 	{PCIC_ID_CLPD6834, "CLPD6834 PCI-CardBus Bridge", CB_CIRRUS},
184 
185 	/* 02Micro */
186 	{PCIC_ID_OZ6832, "O2Micro OZ6832/6833 PCI-CardBus Bridge", CB_O2MICRO},
187 	{PCIC_ID_OZ6860, "O2Micro OZ6836/6860 PCI-CardBus Bridge", CB_O2MICRO},
188 	{PCIC_ID_OZ6872, "O2Micro OZ6812/6872 PCI-CardBus Bridge", CB_O2MICRO},
189 	{PCIC_ID_OZ6912, "O2Micro OZ6912/6972 PCI-CardBus Bridge", CB_O2MICRO},
190 	{PCIC_ID_OZ6922, "O2Micro OZ6922 PCI-CardBus Bridge", CB_O2MICRO},
191 	{PCIC_ID_OZ6933, "O2Micro OZ6933 PCI-CardBus Bridge", CB_O2MICRO},
192 	{PCIC_ID_OZ711E1, "O2Micro OZ711E1 PCI-CardBus Bridge", CB_O2MICRO},
193 
194 	/* sentinel */
195 	{0 /* null id */, "unknown", CB_UNKNOWN},
196 };
197 
198 /* sysctl vars */
199 SYSCTL_NODE(_hw, OID_AUTO, cbb, CTLFLAG_RD, 0, "CBB parameters");
200 
201 /* There's no way to say TUNEABLE_LONG to get the right types */
202 u_long cbb_start_mem = CBB_START_MEM;
203 TUNABLE_INT("hw.cbb.start_memory", (int *)&cbb_start_mem);
204 SYSCTL_ULONG(_hw_cbb, OID_AUTO, start_memory, CTLFLAG_RW,
205     &cbb_start_mem, CBB_START_MEM,
206     "Starting address for memory allocations");
207 
208 u_long cbb_start_16_io = CBB_START_16_IO;
209 TUNABLE_INT("hw.cbb.start_16_io", (int *)&cbb_start_16_io);
210 SYSCTL_ULONG(_hw_cbb, OID_AUTO, start_16_io, CTLFLAG_RW,
211     &cbb_start_16_io, CBB_START_16_IO,
212     "Starting ioport for 16-bit cards");
213 
214 u_long cbb_start_32_io = CBB_START_32_IO;
215 TUNABLE_INT("hw.cbb.start_32_io", (int *)&cbb_start_32_io);
216 SYSCTL_ULONG(_hw_cbb, OID_AUTO, start_32_io, CTLFLAG_RW,
217     &cbb_start_32_io, CBB_START_32_IO,
218     "Starting ioport for 32-bit cards");
219 
220 int cbb_debug = 0;
221 TUNABLE_INT("hw.cbb.debug", &cbb_debug);
222 SYSCTL_ULONG(_hw_cbb, OID_AUTO, debug, CTLFLAG_RW, &cbb_debug, 0,
223     "Verbose cardbus bridge debugging");
224 
225 static int	cbb_chipset(uint32_t pci_id, const char **namep);
226 static int	cbb_probe(device_t brdev);
227 static void	cbb_chipinit(struct cbb_softc *sc);
228 static int	cbb_attach(device_t brdev);
229 static int	cbb_detach(device_t brdev);
230 static int	cbb_shutdown(device_t brdev);
231 static void	cbb_driver_added(device_t brdev, driver_t *driver);
232 static void	cbb_child_detached(device_t brdev, device_t child);
233 static void	cbb_event_thread(void *arg);
234 static void	cbb_insert(struct cbb_softc *sc);
235 static void	cbb_removal(struct cbb_softc *sc);
236 static void	cbb_intr(void *arg);
237 static int	cbb_detect_voltage(device_t brdev);
238 static int	cbb_power(device_t brdev, int volts);
239 static void	cbb_cardbus_reset(device_t brdev);
240 static int	cbb_cardbus_power_enable_socket(device_t brdev,
241 		    device_t child);
242 static void	cbb_cardbus_power_disable_socket(device_t brdev,
243 		    device_t child);
244 static int	cbb_cardbus_io_open(device_t brdev, int win, uint32_t start,
245 		    uint32_t end);
246 static int	cbb_cardbus_mem_open(device_t brdev, int win,
247 		    uint32_t start, uint32_t end);
248 static void	cbb_cardbus_auto_open(struct cbb_softc *sc, int type);
249 static int	cbb_cardbus_activate_resource(device_t brdev, device_t child,
250 		    int type, int rid, struct resource *res);
251 static int	cbb_cardbus_deactivate_resource(device_t brdev,
252 		    device_t child, int type, int rid, struct resource *res);
253 static struct resource	*cbb_cardbus_alloc_resource(device_t brdev,
254 		    device_t child, int type, int *rid, u_long start,
255 		    u_long end, u_long count, u_int flags);
256 static int	cbb_cardbus_release_resource(device_t brdev, device_t child,
257 		    int type, int rid, struct resource *res);
258 static int	cbb_power_enable_socket(device_t brdev, device_t child);
259 static void	cbb_power_disable_socket(device_t brdev, device_t child);
260 static int	cbb_activate_resource(device_t brdev, device_t child,
261 		    int type, int rid, struct resource *r);
262 static int	cbb_deactivate_resource(device_t brdev, device_t child,
263 		    int type, int rid, struct resource *r);
264 static struct resource	*cbb_alloc_resource(device_t brdev, device_t child,
265 		    int type, int *rid, u_long start, u_long end, u_long count,
266 		    u_int flags);
267 static int	cbb_release_resource(device_t brdev, device_t child,
268 		    int type, int rid, struct resource *r);
269 static int	cbb_read_ivar(device_t brdev, device_t child, int which,
270 		    uintptr_t *result);
271 static int	cbb_write_ivar(device_t brdev, device_t child, int which,
272 		    uintptr_t value);
273 static int	cbb_maxslots(device_t brdev);
274 static uint32_t cbb_read_config(device_t brdev, int b, int s, int f,
275 		    int reg, int width);
276 static void	cbb_write_config(device_t brdev, int b, int s, int f,
277 		    int reg, uint32_t val, int width);
278 
279 /*
280  */
281 static __inline void
282 cbb_set(struct cbb_softc *sc, uint32_t reg, uint32_t val)
283 {
284 	bus_space_write_4(sc->bst, sc->bsh, reg, val);
285 }
286 
287 static __inline uint32_t
288 cbb_get(struct cbb_softc *sc, uint32_t reg)
289 {
290 	return (bus_space_read_4(sc->bst, sc->bsh, reg));
291 }
292 
293 static __inline void
294 cbb_setb(struct cbb_softc *sc, uint32_t reg, uint32_t bits)
295 {
296 	cbb_set(sc, reg, cbb_get(sc, reg) | bits);
297 }
298 
299 static __inline void
300 cbb_clrb(struct cbb_softc *sc, uint32_t reg, uint32_t bits)
301 {
302 	cbb_set(sc, reg, cbb_get(sc, reg) & ~bits);
303 }
304 
305 static void
306 cbb_remove_res(struct cbb_softc *sc, struct resource *res)
307 {
308 	struct cbb_reslist *rle;
309 
310 	SLIST_FOREACH(rle, &sc->rl, link) {
311 		if (rle->res == res) {
312 			SLIST_REMOVE(&sc->rl, rle, cbb_reslist, link);
313 			free(rle, M_DEVBUF);
314 			return;
315 		}
316 	}
317 }
318 
319 static struct resource *
320 cbb_find_res(struct cbb_softc *sc, int type, int rid)
321 {
322 	struct cbb_reslist *rle;
323 
324 	SLIST_FOREACH(rle, &sc->rl, link)
325 		if (SYS_RES_MEMORY == rle->type && rid == rle->rid)
326 			return (rle->res);
327 	return (NULL);
328 }
329 
330 static void
331 cbb_insert_res(struct cbb_softc *sc, struct resource *res, int type,
332     int rid)
333 {
334 	struct cbb_reslist *rle;
335 
336 	/*
337 	 * Need to record allocated resource so we can iterate through
338 	 * it later.
339 	 */
340 	rle = malloc(sizeof(struct cbb_reslist), M_DEVBUF, M_NOWAIT);
341 	if (rle == NULL)
342 		panic("cbb_cardbus_alloc_resource: can't record entry!");
343 	rle->res = res;
344 	rle->type = type;
345 	rle->rid = rid;
346 	SLIST_INSERT_HEAD(&sc->rl, rle, link);
347 }
348 
349 static void
350 cbb_destroy_res(struct cbb_softc *sc)
351 {
352 	struct cbb_reslist *rle;
353 
354 	while ((rle = SLIST_FIRST(&sc->rl)) != NULL) {
355 		device_printf(sc->dev, "Danger Will Robinson: Resource "
356 		    "left allocated!  This is a bug... "
357 		    "(rid=%x, type=%d, addr=%lx)\n", rle->rid, rle->type,
358 		    rman_get_start(rle->res));
359 		SLIST_REMOVE_HEAD(&sc->rl, link);
360 		free(rle, M_DEVBUF);
361 	}
362 }
363 
364 /************************************************************************/
365 /* Probe/Attach								*/
366 /************************************************************************/
367 
368 static int
369 cbb_chipset(uint32_t pci_id, const char **namep)
370 {
371 	struct yenta_chipinfo *ycp;
372 
373 	for (ycp = yc_chipsets; ycp->yc_id != 0 && pci_id != ycp->yc_id; ++ycp)
374 	    continue;
375 	if (namep != NULL)
376 		*namep = ycp->yc_name;
377 	return (ycp->yc_chiptype);
378 }
379 
380 static int
381 cbb_probe(device_t brdev)
382 {
383 	const char *name;
384 	uint32_t progif;
385 	uint32_t subclass;
386 
387 	/*
388 	 * Do we know that we support the chipset?  If so, then we
389 	 * accept the device.
390 	 */
391 	if (cbb_chipset(pci_get_devid(brdev), &name) != CB_UNKNOWN) {
392 		device_set_desc(brdev, name);
393 		return (0);
394 	}
395 
396 	/*
397 	 * We do support generic CardBus bridges.  All that we've seen
398 	 * to date have progif 0 (the Yenta spec, and successors mandate
399 	 * this).  We do not support PCI PCMCIA bridges (with one exception)
400 	 * with this driver since they generally are I/O mapped.  Those
401 	 * are supported by the pcic driver.  This should help us be more
402 	 * future proof.
403 	 */
404 	subclass = pci_get_subclass(brdev);
405 	progif = pci_get_progif(brdev);
406 	if (subclass == PCIS_BRIDGE_CARDBUS && progif == 0) {
407 		device_set_desc(brdev, "PCI-CardBus Bridge");
408 		return (0);
409 	}
410 	return (ENXIO);
411 }
412 
413 
414 /*
415  * Disable function interrupts by telling the bridge to generate IRQ1
416  * interrupts.  These interrupts aren't really generated by the chip, since
417  * IRQ1 is reserved.  Some chipsets assert INTA# inappropriately during
418  * initialization, so this helps to work around the problem.
419  *
420  * XXX We can't do this workaround for all chipsets, because this
421  * XXX causes interference with the keyboard because somechipsets will
422  * XXX actually signal IRQ1 over their serial interrupt connections to
423  * XXX the south bridge.  Disable it it for now.
424  */
425 static void
426 cbb_disable_func_intr(struct cbb_softc *sc)
427 {
428 #if 0
429 	uint8_t reg;
430 
431 	reg = (exca_getb(&sc->exca, EXCA_INTR) & ~EXCA_INTR_IRQ_MASK) |
432 	    EXCA_INTR_IRQ_RESERVED1;
433 	exca_putb(&sc->exca, EXCA_INTR, reg);
434 #endif
435 }
436 
437 /*
438  * Enable function interrupts.  We turn on function interrupts when the card
439  * requests an interrupt.  The PCMCIA standard says that we should set
440  * the lower 4 bits to 0 to route via PCI.  Note: we call this for both
441  * CardBus and R2 (PC Card) cases, but it should have no effect on CardBus
442  * cards.
443  */
444 static void
445 cbb_enable_func_intr(struct cbb_softc *sc)
446 {
447 	uint8_t reg;
448 
449 	reg = (exca_getb(&sc->exca, EXCA_INTR) & ~EXCA_INTR_IRQ_MASK) |
450 	    EXCA_INTR_IRQ_NONE;
451 	exca_putb(&sc->exca, EXCA_INTR, reg);
452 }
453 
454 static void
455 cbb_chipinit(struct cbb_softc *sc)
456 {
457 	uint32_t mux, sysctrl, reg;
458 
459 	/* Set CardBus latency timer */
460 	if (pci_read_config(sc->dev, PCIR_SECLAT_1, 1) < 0x20)
461 		pci_write_config(sc->dev, PCIR_SECLAT_1, 0x20, 1);
462 
463 	/* Set PCI latency timer */
464 	if (pci_read_config(sc->dev, PCIR_LATTIMER, 1) < 0x20)
465 		pci_write_config(sc->dev, PCIR_LATTIMER, 0x20, 1);
466 
467 	/* Enable memory access */
468 	PCI_MASK_CONFIG(sc->dev, PCIR_COMMAND,
469 	    | PCIM_CMD_MEMEN
470 	    | PCIM_CMD_PORTEN
471 	    | PCIM_CMD_BUSMASTEREN, 2);
472 
473 	/* disable Legacy IO */
474 	switch (sc->chipset) {
475 	case CB_RF5C46X:
476 		PCI_MASK_CONFIG(sc->dev, CBBR_BRIDGECTRL,
477 		    & ~(CBBM_BRIDGECTRL_RL_3E0_EN |
478 		    CBBM_BRIDGECTRL_RL_3E2_EN), 2);
479 		break;
480 	default:
481 		pci_write_config(sc->dev, CBBR_LEGACY, 0x0, 4);
482 		break;
483 	}
484 
485 	/* Use PCI interrupt for interrupt routing */
486 	PCI_MASK2_CONFIG(sc->dev, CBBR_BRIDGECTRL,
487 	    & ~(CBBM_BRIDGECTRL_MASTER_ABORT |
488 	    CBBM_BRIDGECTRL_INTR_IREQ_EN),
489 	    | CBBM_BRIDGECTRL_WRITE_POST_EN,
490 	    2);
491 
492 	/*
493 	 * XXX this should be a function table, ala OLDCARD.  This means
494 	 * that we could more easily support ISA interrupts for pccard
495 	 * cards if we had to.
496 	 */
497 	switch (sc->chipset) {
498 	case CB_TI113X:
499 		/*
500 		 * The TI 1031, TI 1130 and TI 1131 all require another bit
501 		 * be set to enable PCI routing of interrupts, and then
502 		 * a bit for each of the CSC and Function interrupts we
503 		 * want routed.
504 		 */
505 		PCI_MASK_CONFIG(sc->dev, CBBR_CBCTRL,
506 		    | CBBM_CBCTRL_113X_PCI_INTR |
507 		    CBBM_CBCTRL_113X_PCI_CSC | CBBM_CBCTRL_113X_PCI_IRQ_EN,
508 		    1);
509 		PCI_MASK_CONFIG(sc->dev, CBBR_DEVCTRL,
510 		    & ~(CBBM_DEVCTRL_INT_SERIAL |
511 		    CBBM_DEVCTRL_INT_PCI), 1);
512 		break;
513 	case CB_TI12XX:
514 		/*
515 		 * Some TI 12xx (and [14][45]xx) based pci cards
516 		 * sometimes have issues with the MFUNC register not
517 		 * being initialized due to a bad EEPROM on board.
518 		 * Laptops that this matters on have this register
519 		 * properly initialized.
520 		 *
521 		 * The TI125X parts have a different register.
522 		 */
523 		mux = pci_read_config(sc->dev, CBBR_MFUNC, 4);
524 		sysctrl = pci_read_config(sc->dev, CBBR_SYSCTRL, 4);
525 		if (mux == 0) {
526 			mux = (mux & ~CBBM_MFUNC_PIN0) |
527 			    CBBM_MFUNC_PIN0_INTA;
528 			if ((sysctrl & CBBM_SYSCTRL_INTRTIE) == 0)
529 				mux = (mux & ~CBBM_MFUNC_PIN1) |
530 				    CBBM_MFUNC_PIN1_INTB;
531 			pci_write_config(sc->dev, CBBR_MFUNC, mux, 4);
532 		}
533 		/*FALLTHROUGH*/
534 	case CB_TI125X:
535 		/*
536 		 * Disable zoom video.  Some machines initialize this
537 		 * improperly and exerpience has shown that this helps
538 		 * prevent strange behavior.
539 		 */
540 		pci_write_config(sc->dev, CBBR_MMCTRL, 0, 4);
541 		break;
542 	case CB_O2MICRO:
543 		/*
544 		 * Issue #1: INT# generated at the same time as
545 		 * selected ISA IRQ.  When IREQ# or STSCHG# is active,
546 		 * in addition to the ISA IRQ being generated, INT#
547 		 * will also be generated at the same time.
548 		 *
549 		 * Some of the older controllers have an issue in
550 		 * which the slot's PCI INT# will be asserted whenever
551 		 * IREQ# or STSCGH# is asserted even if ExCA registers
552 		 * 03h or 05h have an ISA IRQ selected.
553 		 *
554 		 * The fix for this issue, which will work for any
555 		 * controller (old or new), is to set ExCA registers
556 		 * 3Ah (slot 0) & 7Ah (slot 1) bits 7:4 = 1010b.
557 		 * These bits are undocumented.  By setting this
558 		 * register (of each slot) to '1010xxxxb' a routing of
559 		 * IREQ# to INTC# and STSCHG# to INTC# is selected.
560 		 * Since INTC# isn't connected there will be no
561 		 * unexpected PCI INT when IREQ# or STSCHG# is active.
562 		 * However, INTA# (slot 0) or INTB# (slot 1) will
563 		 * still be correctly generated if NO ISA IRQ is
564 		 * selected (ExCA regs 03h or 05h are cleared).
565 		 */
566 		reg = exca_getb(&sc->exca, EXCA_O2MICRO_CTRL_C);
567 		reg = (reg & 0x0f) |
568 		    EXCA_O2CC_IREQ_INTC | EXCA_O2CC_STSCHG_INTC;
569 		exca_putb(&sc->exca, EXCA_O2MICRO_CTRL_C, reg);
570 
571 		break;
572 	case CB_TOPIC97:
573 		/*
574 		 * Disable Zoom Video, ToPIC 97, 100.
575 		 */
576 		pci_write_config(sc->dev, CBBR_TOPIC_ZV_CONTROL, 0, 1);
577 		/*
578 		 * ToPIC 97, 100
579 		 * At offset 0xa1: INTERRUPT CONTROL register
580 		 * 0x1: Turn on INT interrupts.
581 		 */
582 		PCI_MASK_CONFIG(sc->dev, CBBR_TOPIC_INTCTRL,
583 		    | CBBM_TOPIC_INTCTRL_INTIRQSEL, 1);
584 		goto topic_common;
585 	case CB_TOPIC95:
586 		/*
587 		 * SOCKETCTRL appears to be TOPIC 95/B specific
588 		 */
589 		PCI_MASK_CONFIG(sc->dev, CBBR_TOPIC_SOCKETCTRL,
590 		    | CBBM_TOPIC_SOCKETCTRL_SCR_IRQSEL, 4);
591 
592 	topic_common:;
593 		/*
594 		 * At offset 0xa0: SLOT CONTROL
595 		 * 0x80 Enable CardBus Functionality
596 		 * 0x40 Enable CardBus and PC Card registers
597 		 * 0x20 Lock ID in exca regs
598 		 * 0x10 Write protect ID in config regs
599 		 * Clear the rest of the bits, which defaults the slot
600 		 * in legacy mode to 0x3e0 and offset 0. (legacy
601 		 * mode is determined elsewhere)
602 		 */
603 		pci_write_config(sc->dev, CBBR_TOPIC_SLOTCTRL,
604 		    CBBM_TOPIC_SLOTCTRL_SLOTON |
605 		    CBBM_TOPIC_SLOTCTRL_SLOTEN |
606 		    CBBM_TOPIC_SLOTCTRL_ID_LOCK |
607 		    CBBM_TOPIC_SLOTCTRL_ID_WP, 1);
608 
609 		/*
610 		 * At offset 0xa3 Card Detect Control Register
611 		 * 0x80 CARDBUS enbale
612 		 * 0x01 Cleared for hardware change detect
613 		 */
614 		PCI_MASK2_CONFIG(sc->dev, CBBR_TOPIC_CDC,
615 		    | CBBM_TOPIC_CDC_CARDBUS,
616 		    & ~CBBM_TOPIC_CDC_SWDETECT, 4);
617 		break;
618 	}
619 
620 	/*
621 	 * Need to tell ExCA registers to CSC interrupts route via PCI
622 	 * interrupts.  There are two ways to do this.  Once is to set
623 	 * INTR_ENABLE and the other is to set CSC to 0.  Since both
624 	 * methods are mutually compatible, we do both.
625 	 */
626 	exca_putb(&sc->exca, EXCA_INTR, EXCA_INTR_ENABLE);
627 	exca_putb(&sc->exca, EXCA_CSC_INTR, 0);
628 
629 	cbb_disable_func_intr(sc);
630 
631 	/* close all memory and io windows */
632 	pci_write_config(sc->dev, CBBR_MEMBASE0, 0xffffffff, 4);
633 	pci_write_config(sc->dev, CBBR_MEMLIMIT0, 0, 4);
634 	pci_write_config(sc->dev, CBBR_MEMBASE1, 0xffffffff, 4);
635 	pci_write_config(sc->dev, CBBR_MEMLIMIT1, 0, 4);
636 	pci_write_config(sc->dev, CBBR_IOBASE0, 0xffffffff, 4);
637 	pci_write_config(sc->dev, CBBR_IOLIMIT0, 0, 4);
638 	pci_write_config(sc->dev, CBBR_IOBASE1, 0xffffffff, 4);
639 	pci_write_config(sc->dev, CBBR_IOLIMIT1, 0, 4);
640 }
641 
642 #ifndef BURN_BRIDGES
643 static void
644 cbb_powerstate_d0(device_t dev)
645 {
646 	u_int32_t membase, irq;
647 
648 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
649 		/* Save important PCI config data. */
650 		membase = pci_read_config(dev, CBBR_SOCKBASE, 4);
651 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
652 
653 		/* Reset the power state. */
654 		device_printf(dev, "chip is in D%d power mode "
655 		    "-- setting to D0\n", pci_get_powerstate(dev));
656 
657 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
658 
659 		/* Restore PCI config data. */
660 		pci_write_config(dev, CBBR_SOCKBASE, membase, 4);
661 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
662 	}
663 }
664 #endif
665 
666 /*
667  * Print out the config space
668  */
669 static void
670 cbb_print_config(device_t dev)
671 {
672 	int i;
673 
674 	device_printf(dev, "PCI Configuration space:");
675 	for (i = 0; i < 256; i += 4) {
676 		if (i % 16 == 0)
677 			printf("\n  0x%02x: ", i);
678 		printf("0x%08x ", pci_read_config(dev, i, 4));
679 	}
680 	printf("\n");
681 }
682 
683 static int
684 cbb_attach(device_t brdev)
685 {
686 	struct cbb_softc *sc = (struct cbb_softc *)device_get_softc(brdev);
687 	int rid;
688 
689 	mtx_init(&sc->mtx, device_get_nameunit(brdev), "cbb", MTX_DEF);
690 	cv_init(&sc->cv, "cbb cv");
691 	sc->chipset = cbb_chipset(pci_get_devid(brdev), NULL);
692 	sc->dev = brdev;
693 	sc->cbdev = NULL;
694 	sc->exca.pccarddev = NULL;
695 	sc->secbus = pci_read_config(brdev, PCIR_SECBUS_2, 1);
696 	sc->subbus = pci_read_config(brdev, PCIR_SUBBUS_2, 1);
697 	SLIST_INIT(&sc->rl);
698 	STAILQ_INIT(&sc->intr_handlers);
699 #ifndef	BURN_BRIDGES
700 	cbb_powerstate_d0(brdev);
701 
702 	/*
703 	 * The PCI bus code should assign us memory in the absense
704 	 * of the BIOS doing so.  However, 'should' isn't 'is,' so we kludge
705 	 * up something here until the PCI/acpi code properly assigns the
706 	 * resource.
707 	 */
708 #endif
709 	rid = CBBR_SOCKBASE;
710 	sc->base_res = bus_alloc_resource_any(brdev, SYS_RES_MEMORY, &rid,
711 	    RF_ACTIVE);
712 	if (!sc->base_res) {
713 #ifdef BURN_BRIDGES
714 		device_printf(brdev, "Could not map register memory\n");
715 		mtx_destroy(&sc->mtx);
716 		cv_destroy(&sc->cv);
717 		return (ENOMEM);
718 #else
719 		uint32_t sockbase;
720 		/*
721 		 * Generally, the BIOS will assign this memory for us.
722 		 * However, newer BIOSes do not because the MS design
723 		 * documents have mandated that this is for the OS
724 		 * to assign rather than the BIOS.  This driver shouldn't
725 		 * be doing this, but until the pci bus code (or acpi)
726 		 * does this, we allow CardBus bridges to work on more
727 		 * machines.
728 		 */
729 		pci_write_config(brdev, rid, 0xfffffffful, 4);
730 		sockbase = pci_read_config(brdev, rid, 4);
731 		sockbase = (sockbase & 0xfffffff0ul) &
732 		    -(sockbase & 0xfffffff0ul);
733 		sc->base_res = bus_generic_alloc_resource(
734 		    device_get_parent(brdev), brdev, SYS_RES_MEMORY,
735 		    &rid, cbb_start_mem, ~0, sockbase,
736 		    RF_ACTIVE | rman_make_alignment_flags(sockbase));
737 		if (!sc->base_res) {
738 			device_printf(brdev,
739 			    "Could not grab register memory\n");
740 			mtx_destroy(&sc->mtx);
741 			cv_destroy(&sc->cv);
742 			return (ENOMEM);
743 		}
744 		sc->flags |= CBB_KLUDGE_ALLOC;
745 		pci_write_config(brdev, CBBR_SOCKBASE,
746 		    rman_get_start(sc->base_res), 4);
747 		DEVPRINTF((brdev, "PCI Memory allocated: %08lx\n",
748 		    rman_get_start(sc->base_res)));
749 #endif
750 	} else {
751 		DEVPRINTF((brdev, "Found memory at %08lx\n",
752 		    rman_get_start(sc->base_res)));
753 	}
754 
755 	sc->bst = rman_get_bustag(sc->base_res);
756 	sc->bsh = rman_get_bushandle(sc->base_res);
757 	exca_init(&sc->exca, brdev, sc->bst, sc->bsh, CBB_EXCA_OFFSET);
758 	sc->exca.flags |= EXCA_HAS_MEMREG_WIN;
759 	sc->exca.chipset = EXCA_CARDBUS;
760 	cbb_chipinit(sc);
761 
762 	/* attach children */
763 	sc->cbdev = device_add_child(brdev, "cardbus", -1);
764 	if (sc->cbdev == NULL)
765 		DEVPRINTF((brdev, "WARNING: cannot add cardbus bus.\n"));
766 	else if (device_probe_and_attach(sc->cbdev) != 0) {
767 		DEVPRINTF((brdev, "WARNING: cannot attach cardbus bus!\n"));
768 		sc->cbdev = NULL;
769 	}
770 
771 	sc->exca.pccarddev = device_add_child(brdev, "pccard", -1);
772 	if (sc->exca.pccarddev == NULL)
773 		DEVPRINTF((brdev, "WARNING: cannot add pccard bus.\n"));
774 	else if (device_probe_and_attach(sc->exca.pccarddev) != 0) {
775 		DEVPRINTF((brdev, "WARNING: cannot attach pccard bus.\n"));
776 		sc->exca.pccarddev = NULL;
777 	}
778 
779 	/* Map and establish the interrupt. */
780 	rid = 0;
781 	sc->irq_res = bus_alloc_resource_any(brdev, SYS_RES_IRQ, &rid,
782 	    RF_SHAREABLE | RF_ACTIVE);
783 	if (sc->irq_res == NULL) {
784 		printf("cbb: Unable to map IRQ...\n");
785 		goto err;
786 	}
787 
788 	if (bus_setup_intr(brdev, sc->irq_res, INTR_TYPE_AV | INTR_MPSAFE,
789 	    cbb_intr, sc, &sc->intrhand)) {
790 		device_printf(brdev, "couldn't establish interrupt");
791 		goto err;
792 	}
793 
794 	/* reset 16-bit pcmcia bus */
795 	exca_clrb(&sc->exca, EXCA_INTR, EXCA_INTR_RESET);
796 
797 	/* turn off power */
798 	cbb_power(brdev, CARD_OFF);
799 
800 	/* CSC Interrupt: Card detect interrupt on */
801 	cbb_setb(sc, CBB_SOCKET_MASK, CBB_SOCKET_MASK_CD);
802 
803 	/* reset interrupt */
804 	cbb_set(sc, CBB_SOCKET_EVENT, cbb_get(sc, CBB_SOCKET_EVENT));
805 
806 	if (bootverbose)
807 		cbb_print_config(brdev);
808 
809 	/* Start the thread */
810 	if (kthread_create(cbb_event_thread, sc, &sc->event_thread, 0, 0,
811 	    "%s", device_get_nameunit(brdev))) {
812 		device_printf(brdev, "unable to create event thread.\n");
813 		panic("cbb_create_event_thread");
814 	}
815 
816 	return (0);
817 err:
818 	if (sc->irq_res)
819 		bus_release_resource(brdev, SYS_RES_IRQ, 0, sc->irq_res);
820 	if (sc->base_res) {
821 		if (sc->flags & CBB_KLUDGE_ALLOC)
822 			bus_generic_release_resource(device_get_parent(brdev),
823 			    brdev, SYS_RES_MEMORY, CBBR_SOCKBASE,
824 			    sc->base_res);
825 		else
826 			bus_release_resource(brdev, SYS_RES_MEMORY,
827 			    CBBR_SOCKBASE, sc->base_res);
828 	}
829 	mtx_destroy(&sc->mtx);
830 	cv_destroy(&sc->cv);
831 	return (ENOMEM);
832 }
833 
834 static int
835 cbb_detach(device_t brdev)
836 {
837 	struct cbb_softc *sc = device_get_softc(brdev);
838 	int numdevs;
839 	device_t *devlist;
840 	int tmp;
841 	int error;
842 
843 	device_get_children(brdev, &devlist, &numdevs);
844 
845 	error = 0;
846 	for (tmp = 0; tmp < numdevs; tmp++) {
847 		if (device_detach(devlist[tmp]) == 0)
848 			device_delete_child(brdev, devlist[tmp]);
849 		else
850 			error++;
851 	}
852 	free(devlist, M_TEMP);
853 	if (error > 0)
854 		return (ENXIO);
855 
856 	mtx_lock(&sc->mtx);
857 	bus_teardown_intr(brdev, sc->irq_res, sc->intrhand);
858 	sc->flags |= CBB_KTHREAD_DONE;
859 	if (sc->flags & CBB_KTHREAD_RUNNING) {
860 		cv_broadcast(&sc->cv);
861 		msleep(sc->event_thread, &sc->mtx, PWAIT, "cbbun", 0);
862 	}
863 	mtx_unlock(&sc->mtx);
864 
865 	bus_release_resource(brdev, SYS_RES_IRQ, 0, sc->irq_res);
866 	if (sc->flags & CBB_KLUDGE_ALLOC)
867 		bus_generic_release_resource(device_get_parent(brdev),
868 		    brdev, SYS_RES_MEMORY, CBBR_SOCKBASE, sc->base_res);
869 	else
870 		bus_release_resource(brdev, SYS_RES_MEMORY,
871 		    CBBR_SOCKBASE, sc->base_res);
872 	mtx_destroy(&sc->mtx);
873 	cv_destroy(&sc->cv);
874 	return (0);
875 }
876 
877 static int
878 cbb_shutdown(device_t brdev)
879 {
880 	struct cbb_softc *sc = (struct cbb_softc *)device_get_softc(brdev);
881 	/* properly reset everything at shutdown */
882 
883 	PCI_MASK_CONFIG(brdev, CBBR_BRIDGECTRL, |CBBM_BRIDGECTRL_RESET, 2);
884 	exca_clrb(&sc->exca, EXCA_INTR, EXCA_INTR_RESET);
885 
886 	cbb_set(sc, CBB_SOCKET_MASK, 0);
887 
888 	cbb_power(brdev, CARD_OFF);
889 
890 	exca_putb(&sc->exca, EXCA_ADDRWIN_ENABLE, 0);
891 	pci_write_config(brdev, CBBR_MEMBASE0, 0, 4);
892 	pci_write_config(brdev, CBBR_MEMLIMIT0, 0, 4);
893 	pci_write_config(brdev, CBBR_MEMBASE1, 0, 4);
894 	pci_write_config(brdev, CBBR_MEMLIMIT1, 0, 4);
895 	pci_write_config(brdev, CBBR_IOBASE0, 0, 4);
896 	pci_write_config(brdev, CBBR_IOLIMIT0, 0, 4);
897 	pci_write_config(brdev, CBBR_IOBASE1, 0, 4);
898 	pci_write_config(brdev, CBBR_IOLIMIT1, 0, 4);
899 	pci_write_config(brdev, PCIR_COMMAND, 0, 2);
900 	return (0);
901 }
902 
903 static int
904 cbb_setup_intr(device_t dev, device_t child, struct resource *irq,
905   int flags, driver_intr_t *intr, void *arg, void **cookiep)
906 {
907 	struct cbb_intrhand *ih;
908 	struct cbb_softc *sc = device_get_softc(dev);
909 
910 	/*
911 	 * You aren't allowed to have fast interrupts for pccard/cardbus
912 	 * things since those interrupts are PCI and shared.  Since we use
913 	 * the PCI interrupt for the status change interrupts, it can't be
914 	 * free for use by the driver.  Fast interrupts must not be shared.
915 	 */
916 	if ((flags & INTR_FAST) != 0)
917 		return (EINVAL);
918 	ih = malloc(sizeof(struct cbb_intrhand), M_DEVBUF, M_NOWAIT);
919 	if (ih == NULL)
920 		return (ENOMEM);
921 	*cookiep = ih;
922 	ih->intr = intr;
923 	ih->arg = arg;
924 	ih->flags = flags & INTR_MPSAFE;
925 	STAILQ_INSERT_TAIL(&sc->intr_handlers, ih, entries);
926 	cbb_enable_func_intr(sc);
927 	/*
928 	 * XXX need to turn on ISA interrupts, if we ever support them, but
929 	 * XXX for now that's all we need to do.
930 	 */
931 	return (0);
932 }
933 
934 static int
935 cbb_teardown_intr(device_t dev, device_t child, struct resource *irq,
936     void *cookie)
937 {
938 	struct cbb_intrhand *ih;
939 	struct cbb_softc *sc = device_get_softc(dev);
940 
941 	/* XXX Need to do different things for ISA interrupts. */
942 	ih = (struct cbb_intrhand *) cookie;
943 	STAILQ_REMOVE(&sc->intr_handlers, ih, cbb_intrhand, entries);
944 	free(ih, M_DEVBUF);
945 	return (0);
946 }
947 
948 
949 static void
950 cbb_driver_added(device_t brdev, driver_t *driver)
951 {
952 	struct cbb_softc *sc = device_get_softc(brdev);
953 	device_t *devlist;
954 	device_t dev;
955 	int tmp;
956 	int numdevs;
957 	int wake = 0;
958 
959 	DEVICE_IDENTIFY(driver, brdev);
960 	device_get_children(brdev, &devlist, &numdevs);
961 	for (tmp = 0; tmp < numdevs; tmp++) {
962 		dev = devlist[tmp];
963 		if (device_get_state(dev) == DS_NOTPRESENT &&
964 		    device_probe_and_attach(dev) == 0)
965 			wake++;
966 	}
967 	free(devlist, M_TEMP);
968 
969 	if (wake > 0) {
970 		mtx_lock(&sc->mtx);
971 		cv_signal(&sc->cv);
972 		mtx_unlock(&sc->mtx);
973 	}
974 }
975 
976 static void
977 cbb_child_detached(device_t brdev, device_t child)
978 {
979 	struct cbb_softc *sc = device_get_softc(brdev);
980 
981 	if (child != sc->cbdev && child != sc->exca.pccarddev)
982 		device_printf(brdev, "Unknown child detached: %s\n",
983 		    device_get_nameunit(child));
984 }
985 
986 /************************************************************************/
987 /* Kthreads								*/
988 /************************************************************************/
989 
990 static void
991 cbb_event_thread(void *arg)
992 {
993 	struct cbb_softc *sc = arg;
994 	uint32_t status;
995 	int err;
996 	int not_a_card = 0;
997 
998 	sc->flags |= CBB_KTHREAD_RUNNING;
999 	while ((sc->flags & CBB_KTHREAD_DONE) == 0) {
1000 		/*
1001 		 * We take out Giant here because we need it deep,
1002 		 * down in the bowels of the vm system for mapping the
1003 		 * memory we need to read the CIS.  In addition, since
1004 		 * we are adding/deleting devices from the dev tree,
1005 		 * and that code isn't MP safe, we have to hold Giant.
1006 		 */
1007 		mtx_lock(&Giant);
1008 		status = cbb_get(sc, CBB_SOCKET_STATE);
1009 		DPRINTF(("Status is 0x%x\n", status));
1010 		if (!CBB_CARD_PRESENT(status)) {
1011 			not_a_card = 0;		/* We know card type */
1012 			cbb_removal(sc);
1013 		} else if (status & CBB_STATE_NOT_A_CARD) {
1014 			/*
1015 			 * Up to 20 times, try to rescan the card when we
1016 			 * see NOT_A_CARD.
1017 			 */
1018 			if (not_a_card++ < 20) {
1019 				DEVPRINTF((sc->dev,
1020 				    "Not a card bit set, rescanning\n"));
1021 				cbb_setb(sc, CBB_SOCKET_FORCE, CBB_FORCE_CV_TEST);
1022 			} else {
1023 				device_printf(sc->dev,
1024 				    "Can't determine card type\n");
1025 			}
1026 		} else {
1027 			not_a_card = 0;		/* We know card type */
1028 			cbb_insert(sc);
1029 		}
1030 		mtx_unlock(&Giant);
1031 
1032 		/*
1033 		 * Wait until it has been 1s since the last time we
1034 		 * get an interrupt.  We handle the rest of the interrupt
1035 		 * at the top of the loop.  Although we clear the bit in the
1036 		 * ISR, we signal sc->cv from the detach path after we've
1037 		 * set the CBB_KTHREAD_DONE bit, so we can't do a simple
1038 		 * 1s sleep here.
1039 		 *
1040 		 * In our ISR, we turn off the card changed interrupt.  Turn
1041 		 * them back on here before we wait for them to happen.  We
1042 		 * turn them on/off so that we can tolerate a large latency
1043 		 * between the time we signal cbb_event_thread and it gets
1044 		 * a chance to run.
1045 		 */
1046 		mtx_lock(&sc->mtx);
1047 		cbb_setb(sc, CBB_SOCKET_MASK, CBB_SOCKET_MASK_CD);
1048 		cv_wait(&sc->cv, &sc->mtx);
1049 		err = 0;
1050 		while (err != EWOULDBLOCK &&
1051 		    (sc->flags & CBB_KTHREAD_DONE) == 0)
1052 			err = cv_timedwait(&sc->cv, &sc->mtx, 1 * hz);
1053 		mtx_unlock(&sc->mtx);
1054 	}
1055 	sc->flags &= ~CBB_KTHREAD_RUNNING;
1056 	kthread_exit(0);
1057 }
1058 
1059 /************************************************************************/
1060 /* Insert/removal							*/
1061 /************************************************************************/
1062 
1063 static void
1064 cbb_insert(struct cbb_softc *sc)
1065 {
1066 	uint32_t sockevent, sockstate;
1067 
1068 	sockevent = cbb_get(sc, CBB_SOCKET_EVENT);
1069 	sockstate = cbb_get(sc, CBB_SOCKET_STATE);
1070 
1071 	DEVPRINTF((sc->dev, "card inserted: event=0x%08x, state=%08x\n",
1072 	    sockevent, sockstate));
1073 
1074 	if (sockstate & CBB_STATE_R2_CARD) {
1075 		if (sc->exca.pccarddev)
1076 			sc->flags |= CBB_16BIT_CARD | CBB_CARD_OK;
1077 		exca_insert(&sc->exca);
1078 	} else if (sockstate & CBB_STATE_CB_CARD) {
1079 		if (sc->cbdev != NULL) {
1080 			sc->flags &= ~CBB_16BIT_CARD;
1081 			sc->flags |= CBB_CARD_OK;
1082 			if (CARD_ATTACH_CARD(sc->cbdev) != 0)
1083 				device_printf(sc->dev,
1084 				    "CardBus card activation failed\n");
1085 		} else {
1086 			device_printf(sc->dev,
1087 			    "CardBus card inserted, but no cardbus bus.\n");
1088 		}
1089 	} else {
1090 		/*
1091 		 * We should power the card down, and try again a couple of
1092 		 * times if this happens. XXX
1093 		 */
1094 		device_printf(sc->dev, "Unsupported card type detected\n");
1095 	}
1096 }
1097 
1098 static void
1099 cbb_removal(struct cbb_softc *sc)
1100 {
1101 	if (sc->flags & CBB_16BIT_CARD) {
1102 		exca_removal(&sc->exca);
1103 	} else {
1104 		if (sc->cbdev != NULL)
1105 			CARD_DETACH_CARD(sc->cbdev);
1106 	}
1107 	cbb_destroy_res(sc);
1108 }
1109 
1110 /************************************************************************/
1111 /* Interrupt Handler							*/
1112 /************************************************************************/
1113 
1114 static void
1115 cbb_intr(void *arg)
1116 {
1117 	struct cbb_softc *sc = arg;
1118 	uint32_t sockevent;
1119 	struct cbb_intrhand *ih;
1120 
1121 	/*
1122 	 * This ISR needs work XXX
1123 	 */
1124 	sockevent = cbb_get(sc, CBB_SOCKET_EVENT);
1125 	if (sockevent != 0) {
1126 		/* ack the interrupt */
1127 		cbb_setb(sc, CBB_SOCKET_EVENT, sockevent);
1128 
1129 		/*
1130 		 * If anything has happened to the socket, we assume that
1131 		 * the card is no longer OK, and we shouldn't call its
1132 		 * ISR.  We set CARD_OK as soon as we've attached the
1133 		 * card.  This helps in a noisy eject, which happens
1134 		 * all too often when users are ejecting their PC Cards.
1135 		 *
1136 		 * We use this method in preference to checking to see if
1137 		 * the card is still there because the check suffers from
1138 		 * a race condition in the bouncing case.  Prior versions
1139 		 * of the pccard software used a similar trick and achieved
1140 		 * excellent results.
1141 		 */
1142 		if (sockevent & CBB_SOCKET_EVENT_CD) {
1143 			mtx_lock(&sc->mtx);
1144 			cbb_setb(sc, CBB_SOCKET_MASK, CBB_SOCKET_MASK_CD);
1145 			sc->flags &= ~CBB_CARD_OK;
1146 			cbb_disable_func_intr(sc);
1147 			cv_signal(&sc->cv);
1148 			mtx_unlock(&sc->mtx);
1149 		}
1150 	}
1151 	/*
1152 	 * Some chips also require us to read the old ExCA registe for
1153 	 * card status change when we route CSC vis PCI.  This isn't supposed
1154 	 * to be required, but it clears the interrupt state on some chipsets.
1155 	 * Maybe there's a setting that would obviate its need.  Maybe we
1156 	 * should test the status bits and deal with them, but so far we've
1157 	 * not found any machines that don't also give us the socket status
1158 	 * indication above.
1159 	 *
1160 	 * We have to call this unconditionally because some bridges deliver
1161 	 * the even independent of the CBB_SOCKET_EVENT_CD above.
1162 	 */
1163 	exca_getb(&sc->exca, EXCA_CSC);
1164 
1165 	/*
1166 	 * If the card is OK, call all the interrupt handlers.
1167  	 */
1168 	if (sc->flags & CBB_CARD_OK) {
1169 		STAILQ_FOREACH(ih, &sc->intr_handlers, entries) {
1170 			if ((ih->flags & INTR_MPSAFE) == 0)
1171 				mtx_lock(&Giant);
1172 			(*ih->intr)(ih->arg);
1173 			if ((ih->flags & INTR_MPSAFE) == 0)
1174 				mtx_unlock(&Giant);
1175 		}
1176 	}
1177 }
1178 
1179 /************************************************************************/
1180 /* Generic Power functions						*/
1181 /************************************************************************/
1182 
1183 static int
1184 cbb_detect_voltage(device_t brdev)
1185 {
1186 	struct cbb_softc *sc = device_get_softc(brdev);
1187 	uint32_t psr;
1188 	int vol = CARD_UKN_CARD;
1189 
1190 	psr = cbb_get(sc, CBB_SOCKET_STATE);
1191 
1192 	if (psr & CBB_STATE_5VCARD)
1193 		vol |= CARD_5V_CARD;
1194 	if (psr & CBB_STATE_3VCARD)
1195 		vol |= CARD_3V_CARD;
1196 	if (psr & CBB_STATE_XVCARD)
1197 		vol |= CARD_XV_CARD;
1198 	if (psr & CBB_STATE_YVCARD)
1199 		vol |= CARD_YV_CARD;
1200 
1201 	return (vol);
1202 }
1203 
1204 static uint8_t
1205 cbb_o2micro_power_hack(struct cbb_softc *sc)
1206 {
1207 	uint8_t reg;
1208 
1209 	/*
1210 	 * Issue #2: INT# not qualified with IRQ Routing Bit.  An
1211 	 * unexpected PCI INT# may be generated during PC-Card
1212 	 * initialization even with the IRQ Routing Bit Set with some
1213 	 * PC-Cards.
1214 	 *
1215 	 * This is a two part issue.  The first part is that some of
1216 	 * our older controllers have an issue in which the slot's PCI
1217 	 * INT# is NOT qualified by the IRQ routing bit (PCI reg. 3Eh
1218 	 * bit 7).  Regardless of the IRQ routing bit, if NO ISA IRQ
1219 	 * is selected (ExCA register 03h bits 3:0, of the slot, are
1220 	 * cleared) we will generate INT# if IREQ# is asserted.  The
1221 	 * second part is because some PC-Cards prematurally assert
1222 	 * IREQ# before the ExCA registers are fully programmed.  This
1223 	 * in turn asserts INT# because ExCA register 03h bits 3:0
1224 	 * (ISA IRQ Select) are not yet programmed.
1225 	 *
1226 	 * The fix for this issue, which will work for any controller
1227 	 * (old or new), is to set ExCA register 03h bits 3:0 = 0001b
1228 	 * (select IRQ1), of the slot, before turning on slot power.
1229 	 * Selecting IRQ1 will result in INT# NOT being asserted
1230 	 * (because IRQ1 is selected), and IRQ1 won't be asserted
1231 	 * because our controllers don't generate IRQ1.
1232 	 */
1233 	reg = exca_getb(&sc->exca, EXCA_INTR);
1234 	exca_putb(&sc->exca, EXCA_INTR, (reg & 0xf0) | 1);
1235 	return (reg);
1236 }
1237 
1238 /*
1239  * Restore the damage that cbb_o2micro_power_hack does to EXCA_INTR so
1240  * we don't have an interrupt storm on power on.  This has the efect of
1241  * disabling card status change interrupts for the duration of poweron.
1242  */
1243 static void
1244 cbb_o2micro_power_hack2(struct cbb_softc *sc, uint8_t reg)
1245 {
1246 	exca_putb(&sc->exca, EXCA_INTR, reg);
1247 }
1248 
1249 static int
1250 cbb_power(device_t brdev, int volts)
1251 {
1252 	uint32_t status, sock_ctrl;
1253 	struct cbb_softc *sc = device_get_softc(brdev);
1254 	int timeout;
1255 	int retval = 0;
1256 	uint32_t sockevent;
1257 	uint8_t reg = 0;
1258 
1259 	status = cbb_get(sc, CBB_SOCKET_STATE);
1260 	sock_ctrl = cbb_get(sc, CBB_SOCKET_CONTROL);
1261 
1262 	sock_ctrl &= ~CBB_SOCKET_CTRL_VCCMASK;
1263 	switch (volts & CARD_VCCMASK) {
1264 	case 5:
1265 		sock_ctrl |= CBB_SOCKET_CTRL_VCC_5V;
1266 		break;
1267 	case 3:
1268 		sock_ctrl |= CBB_SOCKET_CTRL_VCC_3V;
1269 		break;
1270 	case XV:
1271 		sock_ctrl |= CBB_SOCKET_CTRL_VCC_XV;
1272 		break;
1273 	case YV:
1274 		sock_ctrl |= CBB_SOCKET_CTRL_VCC_YV;
1275 		break;
1276 	case 0:
1277 		break;
1278 	default:
1279 		return (0);			/* power NEVER changed */
1280 	}
1281 
1282 	/* VPP == VCC */
1283 	sock_ctrl &= ~CBB_SOCKET_CTRL_VPPMASK;
1284 	sock_ctrl |= ((sock_ctrl >> 4) & 0x07);
1285 
1286 	if (cbb_get(sc, CBB_SOCKET_CONTROL) == sock_ctrl)
1287 		return (1); /* no change necessary */
1288 	DEVPRINTF((sc->dev, "cbb_power: %dV\n", volts));
1289 	if (volts != 0 && sc->chipset == CB_O2MICRO)
1290 		reg = cbb_o2micro_power_hack(sc);
1291 
1292 	cbb_set(sc, CBB_SOCKET_CONTROL, sock_ctrl);
1293 	status = cbb_get(sc, CBB_SOCKET_STATE);
1294 
1295 	/*
1296 	 * XXX This busy wait is bogus.  We should wait for a power
1297 	 * interrupt and then whine if the status is bad.  If we're
1298 	 * worried about the card not coming up, then we should also
1299 	 * schedule a timeout which we can cancel in the power interrupt.
1300 	 */
1301 	timeout = 20;
1302 	do {
1303 		DELAY(20*1000);
1304 		sockevent = cbb_get(sc, CBB_SOCKET_EVENT);
1305 	} while (!(sockevent & CBB_SOCKET_EVENT_POWER) && --timeout > 0);
1306 	/* reset event status */
1307 	/* XXX should only reset EVENT_POWER */
1308 	cbb_set(sc, CBB_SOCKET_EVENT, sockevent);
1309 	if (timeout < 0) {
1310 		printf ("VCC supply failed.\n");
1311 		goto done;
1312 	}
1313 
1314 	/* XXX
1315 	 * delay 400 ms: thgough the standard defines that the Vcc set-up time
1316 	 * is 20 ms, some PC-Card bridge requires longer duration.
1317 	 * XXX Note: We should check the stutus AFTER the delay to give time
1318 	 * for things to stabilize.
1319 	 */
1320 	DELAY(400*1000);
1321 
1322 	if (status & CBB_STATE_BAD_VCC_REQ) {
1323 		device_printf(sc->dev,
1324 		    "bad Vcc request. ctrl=0x%x, status=0x%x\n",
1325 		    sock_ctrl ,status);
1326 		printf("cbb_power: %dV\n", volts);
1327 		goto done;
1328 	}
1329 	retval = 1;
1330 done:;
1331 	if (volts != 0 && sc->chipset == CB_O2MICRO)
1332 		cbb_o2micro_power_hack2(sc, reg);
1333 	return (retval);
1334 }
1335 
1336 /*
1337  * detect the voltage for the card, and set it.  Since the power
1338  * used is the square of the voltage, lower voltages is a big win
1339  * and what Windows does (and what Microsoft prefers).  The MS paper
1340  * also talks about preferring the CIS entry as well.  In addition,
1341  * we power up with OE disabled.  We'll set it later in the power
1342  * up sequence.
1343  */
1344 static int
1345 cbb_do_power(device_t brdev)
1346 {
1347 	struct cbb_softc *sc = device_get_softc(brdev);
1348 	int voltage;
1349 
1350 	/* Don't enable OE */
1351 	exca_clrb(&sc->exca, EXCA_PWRCTL, EXCA_PWRCTL_OE);
1352 
1353 	/* Prefer lowest voltage supported */
1354 	voltage = cbb_detect_voltage(brdev);
1355 	cbb_power(brdev, CARD_OFF);
1356 	if (voltage & CARD_YV_CARD)
1357 		cbb_power(brdev, CARD_VCC(YV));
1358 	else if (voltage & CARD_XV_CARD)
1359 		cbb_power(brdev, CARD_VCC(XV));
1360 	else if (voltage & CARD_3V_CARD)
1361 		cbb_power(brdev, CARD_VCC(3));
1362 	else if (voltage & CARD_5V_CARD)
1363 		cbb_power(brdev, CARD_VCC(5));
1364 	else {
1365 		device_printf(brdev, "Unknown card voltage\n");
1366 		return (ENXIO);
1367 	}
1368 	return (0);
1369 }
1370 
1371 /************************************************************************/
1372 /* CardBus power functions						*/
1373 /************************************************************************/
1374 
1375 static void
1376 cbb_cardbus_reset(device_t brdev)
1377 {
1378 	struct cbb_softc *sc = device_get_softc(brdev);
1379 	int delay_us;
1380 
1381 	delay_us = sc->chipset == CB_RF5C47X ? 400*1000 : 20*1000;
1382 
1383 	PCI_MASK_CONFIG(brdev, CBBR_BRIDGECTRL, |CBBM_BRIDGECTRL_RESET, 2);
1384 
1385 	DELAY(delay_us);
1386 
1387 	/* If a card exists, unreset it! */
1388 	if (CBB_CARD_PRESENT(cbb_get(sc, CBB_SOCKET_STATE))) {
1389 		PCI_MASK_CONFIG(brdev, CBBR_BRIDGECTRL,
1390 		    &~CBBM_BRIDGECTRL_RESET, 2);
1391 		DELAY(delay_us);
1392 	}
1393 }
1394 
1395 static int
1396 cbb_cardbus_power_enable_socket(device_t brdev, device_t child)
1397 {
1398 	struct cbb_softc *sc = device_get_softc(brdev);
1399 	int err;
1400 
1401 	if (!CBB_CARD_PRESENT(cbb_get(sc, CBB_SOCKET_STATE)))
1402 		return (ENODEV);
1403 
1404 	err = cbb_do_power(brdev);
1405 	if (err)
1406 		return (err);
1407 	cbb_cardbus_reset(brdev);
1408 	return (0);
1409 }
1410 
1411 static void
1412 cbb_cardbus_power_disable_socket(device_t brdev, device_t child)
1413 {
1414 	cbb_power(brdev, CARD_OFF);
1415 	cbb_cardbus_reset(brdev);
1416 }
1417 
1418 /************************************************************************/
1419 /* CardBus Resource							*/
1420 /************************************************************************/
1421 
1422 static int
1423 cbb_cardbus_io_open(device_t brdev, int win, uint32_t start, uint32_t end)
1424 {
1425 	int basereg;
1426 	int limitreg;
1427 
1428 	if ((win < 0) || (win > 1)) {
1429 		DEVPRINTF((brdev,
1430 		    "cbb_cardbus_io_open: window out of range %d\n", win));
1431 		return (EINVAL);
1432 	}
1433 
1434 	basereg = win * 8 + CBBR_IOBASE0;
1435 	limitreg = win * 8 + CBBR_IOLIMIT0;
1436 
1437 	pci_write_config(brdev, basereg, start, 4);
1438 	pci_write_config(brdev, limitreg, end, 4);
1439 	return (0);
1440 }
1441 
1442 static int
1443 cbb_cardbus_mem_open(device_t brdev, int win, uint32_t start, uint32_t end)
1444 {
1445 	int basereg;
1446 	int limitreg;
1447 
1448 	if ((win < 0) || (win > 1)) {
1449 		DEVPRINTF((brdev,
1450 		    "cbb_cardbus_mem_open: window out of range %d\n", win));
1451 		return (EINVAL);
1452 	}
1453 
1454 	basereg = win*8 + CBBR_MEMBASE0;
1455 	limitreg = win*8 + CBBR_MEMLIMIT0;
1456 
1457 	pci_write_config(brdev, basereg, start, 4);
1458 	pci_write_config(brdev, limitreg, end, 4);
1459 	return (0);
1460 }
1461 
1462 /*
1463  * XXX The following function belongs in the pci bus layer.
1464  */
1465 static void
1466 cbb_cardbus_auto_open(struct cbb_softc *sc, int type)
1467 {
1468 	uint32_t starts[2];
1469 	uint32_t ends[2];
1470 	struct cbb_reslist *rle;
1471 	int align;
1472 	int prefetchable[2];
1473 	uint32_t reg;
1474 
1475 	starts[0] = starts[1] = 0xffffffff;
1476 	ends[0] = ends[1] = 0;
1477 
1478 	if (type == SYS_RES_MEMORY)
1479 		align = CBB_MEMALIGN;
1480 	else if (type == SYS_RES_IOPORT)
1481 		align = CBB_IOALIGN;
1482 	else
1483 		align = 1;
1484 
1485 	/*
1486 	 * This looks somewhat bogus, and doesn't seem to really respect
1487 	 * alignment.  The alignment stuff is happening too late (it
1488 	 * should happen at allocation time, not activation time) and
1489 	 * this code looks generally to be too complex for the purpose
1490 	 * it surves.
1491 	 */
1492 	SLIST_FOREACH(rle, &sc->rl, link) {
1493 		if (rle->type != type)
1494 			;
1495 		else if (rle->res == NULL) {
1496 			device_printf(sc->dev, "WARNING: Resource not reserved?  "
1497 			    "(type=%d, addr=%lx)\n",
1498 			    rle->type, rman_get_start(rle->res));
1499 		} else if (!(rman_get_flags(rle->res) & RF_ACTIVE)) {
1500 			/* XXX */
1501 		} else if (starts[0] == 0xffffffff) {
1502 			starts[0] = rman_get_start(rle->res);
1503 			ends[0] = rman_get_end(rle->res);
1504 			prefetchable[0] =
1505 			    rman_get_flags(rle->res) & RF_PREFETCHABLE;
1506 		} else if (rman_get_end(rle->res) > ends[0] &&
1507 		    rman_get_start(rle->res) - ends[0] <
1508 		    CBB_AUTO_OPEN_SMALLHOLE && prefetchable[0] ==
1509 		    (rman_get_flags(rle->res) & RF_PREFETCHABLE)) {
1510 			ends[0] = rman_get_end(rle->res);
1511 		} else if (rman_get_start(rle->res) < starts[0] &&
1512 		    starts[0] - rman_get_end(rle->res) <
1513 		    CBB_AUTO_OPEN_SMALLHOLE && prefetchable[0] ==
1514 		    (rman_get_flags(rle->res) & RF_PREFETCHABLE)) {
1515 			starts[0] = rman_get_start(rle->res);
1516 		} else if (starts[1] == 0xffffffff) {
1517 			starts[1] = rman_get_start(rle->res);
1518 			ends[1] = rman_get_end(rle->res);
1519 			prefetchable[1] =
1520 			    rman_get_flags(rle->res) & RF_PREFETCHABLE;
1521 		} else if (rman_get_end(rle->res) > ends[1] &&
1522 		    rman_get_start(rle->res) - ends[1] <
1523 		    CBB_AUTO_OPEN_SMALLHOLE && prefetchable[1] ==
1524 		    (rman_get_flags(rle->res) & RF_PREFETCHABLE)) {
1525 			ends[1] = rman_get_end(rle->res);
1526 		} else if (rman_get_start(rle->res) < starts[1] &&
1527 		    starts[1] - rman_get_end(rle->res) <
1528 		    CBB_AUTO_OPEN_SMALLHOLE && prefetchable[1] ==
1529 		    (rman_get_flags(rle->res) & RF_PREFETCHABLE)) {
1530 			starts[1] = rman_get_start(rle->res);
1531 		} else {
1532 			uint32_t diffs[2];
1533 			int win;
1534 
1535 			diffs[0] = diffs[1] = 0xffffffff;
1536 			if (rman_get_start(rle->res) > ends[0])
1537 				diffs[0] = rman_get_start(rle->res) - ends[0];
1538 			else if (rman_get_end(rle->res) < starts[0])
1539 				diffs[0] = starts[0] - rman_get_end(rle->res);
1540 			if (rman_get_start(rle->res) > ends[1])
1541 				diffs[1] = rman_get_start(rle->res) - ends[1];
1542 			else if (rman_get_end(rle->res) < starts[1])
1543 				diffs[1] = starts[1] - rman_get_end(rle->res);
1544 
1545 			win = (diffs[0] <= diffs[1])?0:1;
1546 			if (rman_get_start(rle->res) > ends[win])
1547 				ends[win] = rman_get_end(rle->res);
1548 			else if (rman_get_end(rle->res) < starts[win])
1549 				starts[win] = rman_get_start(rle->res);
1550 			if (!(rman_get_flags(rle->res) & RF_PREFETCHABLE))
1551 				prefetchable[win] = 0;
1552 		}
1553 
1554 		if (starts[0] != 0xffffffff)
1555 			starts[0] -= starts[0] % align;
1556 		if (starts[1] != 0xffffffff)
1557 			starts[1] -= starts[1] % align;
1558 		if (ends[0] % align != 0)
1559 			ends[0] += align - ends[0] % align - 1;
1560 		if (ends[1] % align != 0)
1561 			ends[1] += align - ends[1] % align - 1;
1562 	}
1563 
1564 	if (type == SYS_RES_MEMORY) {
1565 		cbb_cardbus_mem_open(sc->dev, 0, starts[0], ends[0]);
1566 		cbb_cardbus_mem_open(sc->dev, 1, starts[1], ends[1]);
1567 		reg = pci_read_config(sc->dev, CBBR_BRIDGECTRL, 2);
1568 		reg &= ~(CBBM_BRIDGECTRL_PREFETCH_0|
1569 		    CBBM_BRIDGECTRL_PREFETCH_1);
1570 		reg |= (prefetchable[0]?CBBM_BRIDGECTRL_PREFETCH_0:0)|
1571 		    (prefetchable[1]?CBBM_BRIDGECTRL_PREFETCH_1:0);
1572 		pci_write_config(sc->dev, CBBR_BRIDGECTRL, reg, 2);
1573 	} else if (type == SYS_RES_IOPORT) {
1574 		cbb_cardbus_io_open(sc->dev, 0, starts[0], ends[0]);
1575 		cbb_cardbus_io_open(sc->dev, 1, starts[1], ends[1]);
1576 	}
1577 }
1578 
1579 static int
1580 cbb_cardbus_activate_resource(device_t brdev, device_t child, int type,
1581     int rid, struct resource *res)
1582 {
1583 	int ret;
1584 
1585 	ret = BUS_ACTIVATE_RESOURCE(device_get_parent(brdev), child,
1586 	    type, rid, res);
1587 	if (ret != 0)
1588 		return (ret);
1589 	cbb_cardbus_auto_open(device_get_softc(brdev), type);
1590 	return (0);
1591 }
1592 
1593 static int
1594 cbb_cardbus_deactivate_resource(device_t brdev, device_t child, int type,
1595     int rid, struct resource *res)
1596 {
1597 	int ret;
1598 
1599 	ret = BUS_DEACTIVATE_RESOURCE(device_get_parent(brdev), child,
1600 	    type, rid, res);
1601 	if (ret != 0)
1602 		return (ret);
1603 	cbb_cardbus_auto_open(device_get_softc(brdev), type);
1604 	return (0);
1605 }
1606 
1607 static struct resource *
1608 cbb_cardbus_alloc_resource(device_t brdev, device_t child, int type,
1609     int *rid, u_long start, u_long end, u_long count, u_int flags)
1610 {
1611 	struct cbb_softc *sc = device_get_softc(brdev);
1612 	int tmp;
1613 	struct resource *res;
1614 	u_long align;
1615 
1616 	switch (type) {
1617 	case SYS_RES_IRQ:
1618 		tmp = rman_get_start(sc->irq_res);
1619 		if (start > tmp || end < tmp || count != 1) {
1620 			device_printf(child, "requested interrupt %ld-%ld,"
1621 			    "count = %ld not supported by cbb\n",
1622 			    start, end, count);
1623 			return (NULL);
1624 		}
1625 		start = end = tmp;
1626 		flags |= RF_SHAREABLE;
1627 		break;
1628 	case SYS_RES_IOPORT:
1629 		if (start <= cbb_start_32_io)
1630 			start = cbb_start_32_io;
1631 		if (end < start)
1632 			end = start;
1633 		break;
1634 	case SYS_RES_MEMORY:
1635 		if (start <= cbb_start_mem)
1636 			start = cbb_start_mem;
1637 		if (end < start)
1638 			end = start;
1639 		if (count < CBB_MEMALIGN)
1640 			align = CBB_MEMALIGN;
1641 		else
1642 			align = count;
1643 		if (align > (1 << RF_ALIGNMENT(flags)))
1644 			flags = (flags & ~RF_ALIGNMENT_MASK) |
1645 			    rman_make_alignment_flags(align);
1646 		break;
1647 	}
1648 
1649 	res = BUS_ALLOC_RESOURCE(device_get_parent(brdev), child, type, rid,
1650 	    start, end, count, flags & ~RF_ACTIVE);
1651 	if (res == NULL) {
1652 		printf("cbb alloc res fail\n");
1653 		return (NULL);
1654 	}
1655 	cbb_insert_res(sc, res, type, *rid);
1656 	if (flags & RF_ACTIVE)
1657 		if (bus_activate_resource(child, type, *rid, res) != 0) {
1658 			bus_release_resource(child, type, *rid, res);
1659 			return (NULL);
1660 		}
1661 
1662 	return (res);
1663 }
1664 
1665 static int
1666 cbb_cardbus_release_resource(device_t brdev, device_t child, int type,
1667     int rid, struct resource *res)
1668 {
1669 	struct cbb_softc *sc = device_get_softc(brdev);
1670 	int error;
1671 
1672 	if (rman_get_flags(res) & RF_ACTIVE) {
1673 		error = bus_deactivate_resource(child, type, rid, res);
1674 		if (error != 0)
1675 			return (error);
1676 	}
1677 	cbb_remove_res(sc, res);
1678 	return (BUS_RELEASE_RESOURCE(device_get_parent(brdev), child,
1679 	    type, rid, res));
1680 }
1681 
1682 /************************************************************************/
1683 /* PC Card Power Functions						*/
1684 /************************************************************************/
1685 
1686 static int
1687 cbb_pcic_power_enable_socket(device_t brdev, device_t child)
1688 {
1689 	struct cbb_softc *sc = device_get_softc(brdev);
1690 	int err;
1691 
1692 	DPRINTF(("cbb_pcic_socket_enable:\n"));
1693 
1694 	/* power down/up the socket to reset */
1695 	err = cbb_do_power(brdev);
1696 	if (err)
1697 		return (err);
1698 	exca_reset(&sc->exca, child);
1699 
1700 	return (0);
1701 }
1702 
1703 static void
1704 cbb_pcic_power_disable_socket(device_t brdev, device_t child)
1705 {
1706 	struct cbb_softc *sc = device_get_softc(brdev);
1707 
1708 	DPRINTF(("cbb_pcic_socket_disable\n"));
1709 
1710 	/* reset signal asserting... */
1711 	exca_clrb(&sc->exca, EXCA_INTR, EXCA_INTR_RESET);
1712 	DELAY(2*1000);
1713 
1714 	/* power down the socket */
1715 	cbb_power(brdev, CARD_OFF);
1716 	exca_clrb(&sc->exca, EXCA_PWRCTL, EXCA_PWRCTL_OE);
1717 
1718 	/* wait 300ms until power fails (Tpf). */
1719 	DELAY(300 * 1000);
1720 }
1721 
1722 /************************************************************************/
1723 /* POWER methods							*/
1724 /************************************************************************/
1725 
1726 static int
1727 cbb_power_enable_socket(device_t brdev, device_t child)
1728 {
1729 	struct cbb_softc *sc = device_get_softc(brdev);
1730 
1731 	if (sc->flags & CBB_16BIT_CARD)
1732 		return (cbb_pcic_power_enable_socket(brdev, child));
1733 	else
1734 		return (cbb_cardbus_power_enable_socket(brdev, child));
1735 }
1736 
1737 static void
1738 cbb_power_disable_socket(device_t brdev, device_t child)
1739 {
1740 	struct cbb_softc *sc = device_get_softc(brdev);
1741 	if (sc->flags & CBB_16BIT_CARD)
1742 		cbb_pcic_power_disable_socket(brdev, child);
1743 	else
1744 		cbb_cardbus_power_disable_socket(brdev, child);
1745 }
1746 
1747 static int
1748 cbb_pcic_activate_resource(device_t brdev, device_t child, int type, int rid,
1749     struct resource *res)
1750 {
1751 	struct cbb_softc *sc = device_get_softc(brdev);
1752 	return (exca_activate_resource(&sc->exca, child, type, rid, res));
1753 }
1754 
1755 static int
1756 cbb_pcic_deactivate_resource(device_t brdev, device_t child, int type,
1757     int rid, struct resource *res)
1758 {
1759 	struct cbb_softc *sc = device_get_softc(brdev);
1760 	return (exca_deactivate_resource(&sc->exca, child, type, rid, res));
1761 }
1762 
1763 static struct resource *
1764 cbb_pcic_alloc_resource(device_t brdev, device_t child, int type, int *rid,
1765     u_long start, u_long end, u_long count, u_int flags)
1766 {
1767 	struct resource *res = NULL;
1768 	struct cbb_softc *sc = device_get_softc(brdev);
1769 	int align;
1770 	int tmp;
1771 
1772 	switch (type) {
1773 	case SYS_RES_MEMORY:
1774 		if (start < cbb_start_mem)
1775 			start = cbb_start_mem;
1776 		if (end < start)
1777 			end = start;
1778 		if (count < CBB_MEMALIGN)
1779 			align = CBB_MEMALIGN;
1780 		else
1781 			align = count;
1782 		if (align > (1 << RF_ALIGNMENT(flags)))
1783 			flags = (flags & ~RF_ALIGNMENT_MASK) |
1784 			    rman_make_alignment_flags(align);
1785 		break;
1786 	case SYS_RES_IOPORT:
1787 		if (start < cbb_start_16_io)
1788 			start = cbb_start_16_io;
1789 		if (end < start)
1790 			end = start;
1791 		break;
1792 	case SYS_RES_IRQ:
1793 		tmp = rman_get_start(sc->irq_res);
1794 		if (start > tmp || end < tmp || count != 1) {
1795 			device_printf(child, "requested interrupt %ld-%ld,"
1796 			    "count = %ld not supported by cbb\n",
1797 			    start, end, count);
1798 			return (NULL);
1799 		}
1800 		flags |= RF_SHAREABLE;
1801 		start = end = rman_get_start(sc->irq_res);
1802 		break;
1803 	}
1804 	res = BUS_ALLOC_RESOURCE(device_get_parent(brdev), child, type, rid,
1805 	    start, end, count, flags & ~RF_ACTIVE);
1806 	if (res == NULL)
1807 		return (NULL);
1808 	cbb_insert_res(sc, res, type, *rid);
1809 	if (flags & RF_ACTIVE) {
1810 		if (bus_activate_resource(child, type, *rid, res) != 0) {
1811 			bus_release_resource(child, type, *rid, res);
1812 			return (NULL);
1813 		}
1814 	}
1815 
1816 	return (res);
1817 }
1818 
1819 static int
1820 cbb_pcic_release_resource(device_t brdev, device_t child, int type,
1821     int rid, struct resource *res)
1822 {
1823 	struct cbb_softc *sc = device_get_softc(brdev);
1824 	int error;
1825 
1826 	if (rman_get_flags(res) & RF_ACTIVE) {
1827 		error = bus_deactivate_resource(child, type, rid, res);
1828 		if (error != 0)
1829 			return (error);
1830 	}
1831 	cbb_remove_res(sc, res);
1832 	return (BUS_RELEASE_RESOURCE(device_get_parent(brdev), child,
1833 	    type, rid, res));
1834 }
1835 
1836 /************************************************************************/
1837 /* PC Card methods							*/
1838 /************************************************************************/
1839 
1840 static int
1841 cbb_pcic_set_res_flags(device_t brdev, device_t child, int type, int rid,
1842     uint32_t flags)
1843 {
1844 	struct cbb_softc *sc = device_get_softc(brdev);
1845 	struct resource *res;
1846 
1847 	if (type != SYS_RES_MEMORY)
1848 		return (EINVAL);
1849 	res = cbb_find_res(sc, type, rid);
1850 	if (res == NULL) {
1851 		device_printf(brdev,
1852 		    "set_res_flags: specified rid not found\n");
1853 		return (ENOENT);
1854 	}
1855 	return (exca_mem_set_flags(&sc->exca, res, flags));
1856 }
1857 
1858 static int
1859 cbb_pcic_set_memory_offset(device_t brdev, device_t child, int rid,
1860     uint32_t cardaddr, uint32_t *deltap)
1861 {
1862 	struct cbb_softc *sc = device_get_softc(brdev);
1863 	struct resource *res;
1864 
1865 	res = cbb_find_res(sc, SYS_RES_MEMORY, rid);
1866 	if (res == NULL) {
1867 		device_printf(brdev,
1868 		    "set_memory_offset: specified rid not found\n");
1869 		return (ENOENT);
1870 	}
1871 	return (exca_mem_set_offset(&sc->exca, res, cardaddr, deltap));
1872 }
1873 
1874 /************************************************************************/
1875 /* BUS Methods								*/
1876 /************************************************************************/
1877 
1878 
1879 static int
1880 cbb_activate_resource(device_t brdev, device_t child, int type, int rid,
1881     struct resource *r)
1882 {
1883 	struct cbb_softc *sc = device_get_softc(brdev);
1884 
1885 	if (sc->flags & CBB_16BIT_CARD)
1886 		return (cbb_pcic_activate_resource(brdev, child, type, rid, r));
1887 	else
1888 		return (cbb_cardbus_activate_resource(brdev, child, type, rid,
1889 		    r));
1890 }
1891 
1892 static int
1893 cbb_deactivate_resource(device_t brdev, device_t child, int type,
1894     int rid, struct resource *r)
1895 {
1896 	struct cbb_softc *sc = device_get_softc(brdev);
1897 
1898 	if (sc->flags & CBB_16BIT_CARD)
1899 		return (cbb_pcic_deactivate_resource(brdev, child, type,
1900 		    rid, r));
1901 	else
1902 		return (cbb_cardbus_deactivate_resource(brdev, child, type,
1903 		    rid, r));
1904 }
1905 
1906 static struct resource *
1907 cbb_alloc_resource(device_t brdev, device_t child, int type, int *rid,
1908     u_long start, u_long end, u_long count, u_int flags)
1909 {
1910 	struct cbb_softc *sc = device_get_softc(brdev);
1911 
1912 	if (sc->flags & CBB_16BIT_CARD)
1913 		return (cbb_pcic_alloc_resource(brdev, child, type, rid,
1914 		    start, end, count, flags));
1915 	else
1916 		return (cbb_cardbus_alloc_resource(brdev, child, type, rid,
1917 		    start, end, count, flags));
1918 }
1919 
1920 static int
1921 cbb_release_resource(device_t brdev, device_t child, int type, int rid,
1922     struct resource *r)
1923 {
1924 	struct cbb_softc *sc = device_get_softc(brdev);
1925 
1926 	if (sc->flags & CBB_16BIT_CARD)
1927 		return (cbb_pcic_release_resource(brdev, child, type,
1928 		    rid, r));
1929 	else
1930 		return (cbb_cardbus_release_resource(brdev, child, type,
1931 		    rid, r));
1932 }
1933 
1934 static int
1935 cbb_read_ivar(device_t brdev, device_t child, int which, uintptr_t *result)
1936 {
1937 	struct cbb_softc *sc = device_get_softc(brdev);
1938 
1939 	switch (which) {
1940 	case PCIB_IVAR_BUS:
1941 		*result = sc->secbus;
1942 		return (0);
1943 	}
1944 	return (ENOENT);
1945 }
1946 
1947 static int
1948 cbb_write_ivar(device_t brdev, device_t child, int which, uintptr_t value)
1949 {
1950 	struct cbb_softc *sc = device_get_softc(brdev);
1951 
1952 	switch (which) {
1953 	case PCIB_IVAR_BUS:
1954 		sc->secbus = value;
1955 		break;
1956 	}
1957 	return (ENOENT);
1958 }
1959 
1960 /************************************************************************/
1961 /* PCI compat methods							*/
1962 /************************************************************************/
1963 
1964 static int
1965 cbb_maxslots(device_t brdev)
1966 {
1967 	return (0);
1968 }
1969 
1970 static uint32_t
1971 cbb_read_config(device_t brdev, int b, int s, int f, int reg, int width)
1972 {
1973 	/*
1974 	 * Pass through to the next ppb up the chain (i.e. our grandparent).
1975 	 */
1976 	return (PCIB_READ_CONFIG(device_get_parent(device_get_parent(brdev)),
1977 	    b, s, f, reg, width));
1978 }
1979 
1980 static void
1981 cbb_write_config(device_t brdev, int b, int s, int f, int reg, uint32_t val,
1982     int width)
1983 {
1984 	/*
1985 	 * Pass through to the next ppb up the chain (i.e. our grandparent).
1986 	 */
1987 	PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(brdev)),
1988 	    b, s, f, reg, val, width);
1989 }
1990 
1991 static int
1992 cbb_suspend(device_t self)
1993 {
1994 	int			error = 0;
1995 	struct cbb_softc	*sc = device_get_softc(self);
1996 
1997 	cbb_set(sc, CBB_SOCKET_MASK, 0);	/* Quiet hardware */
1998 	bus_teardown_intr(self, sc->irq_res, sc->intrhand);
1999 	sc->flags &= ~CBB_CARD_OK;		/* Card is bogus now */
2000 	error = bus_generic_suspend(self);
2001 	return (error);
2002 }
2003 
2004 static int
2005 cbb_resume(device_t self)
2006 {
2007 	int	error = 0;
2008 	struct cbb_softc *sc = (struct cbb_softc *)device_get_softc(self);
2009 	uint32_t tmp;
2010 
2011 	/*
2012 	 * Some BIOSes will not save the BARs for the pci chips, so we
2013 	 * must do it ourselves.  If the BAR is reset to 0 for an I/O
2014 	 * device, it will read back as 0x1, so no explicit test for
2015 	 * memory devices are needed.
2016 	 *
2017 	 * Note: The PCI bus code should do this automatically for us on
2018 	 * suspend/resume, but until it does, we have to cope.
2019 	 */
2020 	pci_write_config(self, CBBR_SOCKBASE, rman_get_start(sc->base_res), 4);
2021 	DEVPRINTF((self, "PCI Memory allocated: %08lx\n",
2022 	    rman_get_start(sc->base_res)));
2023 
2024 	cbb_chipinit(sc);
2025 
2026 	/* reset interrupt -- Do we really need to do this? */
2027 	tmp = cbb_get(sc, CBB_SOCKET_EVENT);
2028 	cbb_set(sc, CBB_SOCKET_EVENT, tmp);
2029 
2030 	/* re-establish the interrupt. */
2031 	if (bus_setup_intr(self, sc->irq_res, INTR_TYPE_AV, cbb_intr, sc,
2032 	    &sc->intrhand)) {
2033 		device_printf(self, "couldn't re-establish interrupt");
2034 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
2035 		bus_release_resource(self, SYS_RES_MEMORY, CBBR_SOCKBASE,
2036 		    sc->base_res);
2037 		sc->irq_res = NULL;
2038 		sc->base_res = NULL;
2039 		return (ENOMEM);
2040 	}
2041 
2042 	/* CSC Interrupt: Card detect interrupt on */
2043 	cbb_setb(sc, CBB_SOCKET_MASK, CBB_SOCKET_MASK_CD);
2044 
2045 	/* Signal the thread to wakeup. */
2046 	mtx_lock(&sc->mtx);
2047 	cv_signal(&sc->cv);
2048 	mtx_unlock(&sc->mtx);
2049 
2050 	error = bus_generic_resume(self);
2051 
2052 	return (error);
2053 }
2054 
2055 static int
2056 cbb_child_present(device_t self)
2057 {
2058 	struct cbb_softc *sc = (struct cbb_softc *)device_get_softc(self);
2059 	uint32_t sockstate;
2060 
2061 	sockstate = cbb_get(sc, CBB_SOCKET_STATE);
2062 	return (CBB_CARD_PRESENT(sockstate) &&
2063 	  (sc->flags & CBB_CARD_OK) == CBB_CARD_OK);
2064 }
2065 
2066 static device_method_t cbb_methods[] = {
2067 	/* Device interface */
2068 	DEVMETHOD(device_probe,			cbb_probe),
2069 	DEVMETHOD(device_attach,		cbb_attach),
2070 	DEVMETHOD(device_detach,		cbb_detach),
2071 	DEVMETHOD(device_shutdown,		cbb_shutdown),
2072 	DEVMETHOD(device_suspend,		cbb_suspend),
2073 	DEVMETHOD(device_resume,		cbb_resume),
2074 
2075 	/* bus methods */
2076 	DEVMETHOD(bus_print_child,		bus_generic_print_child),
2077 	DEVMETHOD(bus_read_ivar,		cbb_read_ivar),
2078 	DEVMETHOD(bus_write_ivar,		cbb_write_ivar),
2079 	DEVMETHOD(bus_alloc_resource,		cbb_alloc_resource),
2080 	DEVMETHOD(bus_release_resource,		cbb_release_resource),
2081 	DEVMETHOD(bus_activate_resource,	cbb_activate_resource),
2082 	DEVMETHOD(bus_deactivate_resource,	cbb_deactivate_resource),
2083 	DEVMETHOD(bus_driver_added,		cbb_driver_added),
2084 	DEVMETHOD(bus_child_detached,		cbb_child_detached),
2085 	DEVMETHOD(bus_setup_intr,		cbb_setup_intr),
2086 	DEVMETHOD(bus_teardown_intr,		cbb_teardown_intr),
2087 	DEVMETHOD(bus_child_present,		cbb_child_present),
2088 
2089 	/* 16-bit card interface */
2090 	DEVMETHOD(card_set_res_flags,		cbb_pcic_set_res_flags),
2091 	DEVMETHOD(card_set_memory_offset,	cbb_pcic_set_memory_offset),
2092 
2093 	/* power interface */
2094 	DEVMETHOD(power_enable_socket,		cbb_power_enable_socket),
2095 	DEVMETHOD(power_disable_socket,		cbb_power_disable_socket),
2096 
2097 	/* pcib compatibility interface */
2098 	DEVMETHOD(pcib_maxslots,		cbb_maxslots),
2099 	DEVMETHOD(pcib_read_config,		cbb_read_config),
2100 	DEVMETHOD(pcib_write_config,		cbb_write_config),
2101 	{0,0}
2102 };
2103 
2104 static driver_t cbb_driver = {
2105 	"cbb",
2106 	cbb_methods,
2107 	sizeof(struct cbb_softc)
2108 };
2109 
2110 static devclass_t cbb_devclass;
2111 
2112 DRIVER_MODULE(cbb, pci, cbb_driver, cbb_devclass, 0, 0);
2113 MODULE_VERSION(cbb, 1);
2114 MODULE_DEPEND(cbb, exca, 1, 1, 1);
2115