2 * Copyright (c) 2002 M. Warner Losh.
3 * Copyright (c) 2000,2001 Jonathan Chen.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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
33 * Copyright (c) 1998, 1999 and 2000
34 * HAYAKAWA Koichi. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by HAYAKAWA Koichi.
47 * 4. The name of the author may not be used to endorse or promote products
48 * derived from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 * Driver for PCI to Cardbus Bridge chips
67 * http://www-s.ti.com/cgi-bin/sc/generic2.cgi?family=PCI+CARDBUS+CONTROLLERS
69 * Written by Jonathan Chen <jon@freebsd.org>
70 * The author would like to acknowledge:
71 * * HAYAKAWA Koichi: Author of the NetBSD code for the same thing
72 * * Warner Losh: Newbus/newcard guru and author of the pccard side of things
73 * * YAMAMOTO Shigeru: Author of another FreeBSD cardbus driver
74 * * David Cross: Author of the initial ugly hack for a specific cardbus card
77 #include <sys/param.h>
78 #include <sys/systm.h>
80 #include <sys/condvar.h>
81 #include <sys/errno.h>
82 #include <sys/kernel.h>
84 #include <sys/malloc.h>
85 #include <sys/mutex.h>
86 #include <sys/sysctl.h>
87 #include <sys/kthread.h>
89 #include <machine/bus.h>
91 #include <machine/resource.h>
93 #include <pci/pcireg.h>
94 #include <pci/pcivar.h>
95 #include <machine/clock.h>
97 #include <dev/pccard/pccardreg.h>
98 #include <dev/pccard/pccardvar.h>
100 #include <dev/exca/excareg.h>
101 #include <dev/exca/excavar.h>
103 #include <dev/pccbb/pccbbreg.h>
104 #include <dev/pccbb/pccbbvar.h>
106 #include "power_if.h"
110 #define DPRINTF(x) do { if (cbb_debug) printf x; } while (0)
111 #define DEVPRINTF(x) do { if (cbb_debug) device_printf x; } while (0)
113 #define PCI_MASK_CONFIG(DEV,REG,MASK,SIZE) \
114 pci_write_config(DEV, REG, pci_read_config(DEV, REG, SIZE) MASK, SIZE)
115 #define PCI_MASK2_CONFIG(DEV,REG,MASK1,MASK2,SIZE) \
116 pci_write_config(DEV, REG, ( \
117 pci_read_config(DEV, REG, SIZE) MASK1) MASK2, SIZE)
119 #define PCCBB_START_MEM 0x88000000
120 #define PCCBB_START_32_IO 0x1000
121 #define PCCBB_START_16_IO 0x100
123 struct yenta_chipinfo {
128 /* Texas Instruments chips */
129 {PCIC_ID_TI1031, "TI1031 PCI-PC Card Bridge", CB_TI113X},
130 {PCIC_ID_TI1130, "TI1130 PCI-CardBus Bridge", CB_TI113X},
131 {PCIC_ID_TI1131, "TI1131 PCI-CardBus Bridge", CB_TI113X},
133 {PCIC_ID_TI1210, "TI1210 PCI-CardBus Bridge", CB_TI12XX},
134 {PCIC_ID_TI1211, "TI1211 PCI-CardBus Bridge", CB_TI12XX},
135 {PCIC_ID_TI1220, "TI1220 PCI-CardBus Bridge", CB_TI12XX},
136 {PCIC_ID_TI1221, "TI1221 PCI-CardBus Bridge", CB_TI12XX},
137 {PCIC_ID_TI1225, "TI1225 PCI-CardBus Bridge", CB_TI12XX},
138 {PCIC_ID_TI1250, "TI1250 PCI-CardBus Bridge", CB_TI125X},
139 {PCIC_ID_TI1251, "TI1251 PCI-CardBus Bridge", CB_TI125X},
140 {PCIC_ID_TI1251B,"TI1251B PCI-CardBus Bridge",CB_TI125X},
141 {PCIC_ID_TI1260, "TI1260 PCI-CardBus Bridge", CB_TI12XX},
142 {PCIC_ID_TI1260B,"TI1260B PCI-CardBus Bridge",CB_TI12XX},
143 {PCIC_ID_TI1410, "TI1410 PCI-CardBus Bridge", CB_TI12XX},
144 {PCIC_ID_TI1420, "TI1420 PCI-CardBus Bridge", CB_TI12XX},
145 {PCIC_ID_TI1421, "TI1421 PCI-CardBus Bridge", CB_TI12XX},
146 {PCIC_ID_TI1450, "TI1450 PCI-CardBus Bridge", CB_TI125X},
147 {PCIC_ID_TI1451, "TI1451 PCI-CardBus Bridge", CB_TI12XX},
148 {PCIC_ID_TI1510, "TI1510 PCI-CardBus Bridge", CB_TI12XX},
149 {PCIC_ID_TI1520, "TI1520 PCI-CardBus Bridge", CB_TI12XX},
150 {PCIC_ID_TI4410, "TI4410 PCI-CardBus Bridge", CB_TI12XX},
151 {PCIC_ID_TI4450, "TI4450 PCI-CardBus Bridge", CB_TI12XX},
152 {PCIC_ID_TI4451, "TI4451 PCI-CardBus Bridge", CB_TI12XX},
153 {PCIC_ID_TI4510, "TI4510 PCI-CardBus Bridge", CB_TI12XX},
156 {PCIC_ID_RICOH_RL5C465, "RF5C465 PCI-CardBus Bridge", CB_RF5C46X},
157 {PCIC_ID_RICOH_RL5C466, "RF5C466 PCI-CardBus Bridge", CB_RF5C46X},
158 {PCIC_ID_RICOH_RL5C475, "RF5C475 PCI-CardBus Bridge", CB_RF5C47X},
159 {PCIC_ID_RICOH_RL5C476, "RF5C476 PCI-CardBus Bridge", CB_RF5C47X},
160 {PCIC_ID_RICOH_RL5C477, "RF5C477 PCI-CardBus Bridge", CB_RF5C47X},
161 {PCIC_ID_RICOH_RL5C478, "RF5C478 PCI-CardBus Bridge", CB_RF5C47X},
163 /* Toshiba products */
164 {PCIC_ID_TOPIC95, "ToPIC95 PCI-CardBus Bridge", CB_TOPIC95},
165 {PCIC_ID_TOPIC95B, "ToPIC95B PCI-CardBus Bridge", CB_TOPIC95},
166 {PCIC_ID_TOPIC97, "ToPIC97 PCI-CardBus Bridge", CB_TOPIC97},
167 {PCIC_ID_TOPIC100, "ToPIC100 PCI-CardBus Bridge", CB_TOPIC97},
170 {PCIC_ID_CLPD6832, "CLPD6832 PCI-CardBus Bridge", CB_CIRRUS},
171 {PCIC_ID_CLPD6833, "CLPD6833 PCI-CardBus Bridge", CB_CIRRUS},
172 {PCIC_ID_CLPD6834, "CLPD6834 PCI-CardBus Bridge", CB_CIRRUS},
175 {PCIC_ID_OZ6832, "O2Mirco OZ6832/6833 PCI-CardBus Bridge", CB_CIRRUS},
176 {PCIC_ID_OZ6860, "O2Mirco OZ6836/6860 PCI-CardBus Bridge", CB_CIRRUS},
177 {PCIC_ID_OZ6872, "O2Mirco OZ6812/6872 PCI-CardBus Bridge", CB_CIRRUS},
178 {PCIC_ID_OZ6912, "O2Mirco OZ6912/6972 PCI-CardBus Bridge", CB_CIRRUS},
179 {PCIC_ID_OZ6922, "O2Mirco OZ6822 PCI-CardBus Bridge", CB_CIRRUS},
180 {PCIC_ID_OZ6933, "O2Mirco OZ6833 PCI-CardBus Bridge", CB_CIRRUS},
183 {0 /* null id */, "unknown", CB_UNKNOWN},
187 SYSCTL_NODE(_hw, OID_AUTO, cbb, CTLFLAG_RD, 0, "CBB parameters");
189 /* There's no way to say TUNEABLE_LONG to get the right types */
190 u_long pccbb_start_mem = PCCBB_START_MEM;
191 TUNABLE_INT("hw.cbb.start_memory", (int *)&pccbb_start_mem);
192 SYSCTL_ULONG(_hw_cbb, OID_AUTO, start_memory, CTLFLAG_RW,
193 &pccbb_start_mem, PCCBB_START_MEM,
194 "Starting address for memory allocations");
196 u_long pccbb_start_16_io = PCCBB_START_16_IO;
197 TUNABLE_INT("hw.cbb.start_16_io", (int *)&pccbb_start_16_io);
198 SYSCTL_ULONG(_hw_cbb, OID_AUTO, start_16_io, CTLFLAG_RW,
199 &pccbb_start_16_io, PCCBB_START_16_IO,
200 "Starting ioport for 16-bit cards");
202 u_long pccbb_start_32_io = PCCBB_START_32_IO;
203 TUNABLE_INT("hw.cbb.start_32_io", (int *)&pccbb_start_32_io);
204 SYSCTL_ULONG(_hw_cbb, OID_AUTO, start_32_io, CTLFLAG_RW,
205 &pccbb_start_32_io, PCCBB_START_32_IO,
206 "Starting ioport for 32-bit cards");
209 TUNABLE_INT("hw.cbb.debug", &cbb_debug);
210 SYSCTL_ULONG(_hw_cbb, OID_AUTO, debug, CTLFLAG_RW, &cbb_debug, 0,
211 "Verbose cardbus bridge debugging");
213 static int pccbb_chipset(uint32_t pci_id, const char **namep);
214 static int pccbb_probe(device_t brdev);
215 static void pccbb_chipinit(struct pccbb_softc *sc);
216 static int pccbb_attach(device_t brdev);
217 static int pccbb_detach(device_t brdev);
218 static int pccbb_shutdown(device_t brdev);
219 static void pccbb_driver_added(device_t brdev, driver_t *driver);
220 static void pccbb_child_detached(device_t brdev, device_t child);
221 static int pccbb_card_reprobe(device_t brdev, device_t busdev);
222 static void pccbb_event_thread(void *arg);
223 static void pccbb_insert(struct pccbb_softc *sc);
224 static void pccbb_removal(struct pccbb_softc *sc);
225 static void pccbb_intr(void *arg);
226 static int pccbb_detect_voltage(device_t brdev);
227 static int pccbb_power(device_t brdev, int volts);
228 static void pccbb_cardbus_reset(device_t brdev);
229 static int pccbb_cardbus_power_enable_socket(device_t brdev,
231 static void pccbb_cardbus_power_disable_socket(device_t brdev,
233 static int pccbb_cardbus_io_open(device_t brdev, int win, uint32_t start,
235 static int pccbb_cardbus_mem_open(device_t brdev, int win,
236 uint32_t start, uint32_t end);
237 static void pccbb_cardbus_auto_open(struct pccbb_softc *sc, int type);
238 static int pccbb_cardbus_activate_resource(device_t brdev, device_t child,
239 int type, int rid, struct resource *res);
240 static int pccbb_cardbus_deactivate_resource(device_t brdev,
241 device_t child, int type, int rid, struct resource *res);
242 static struct resource *pccbb_cardbus_alloc_resource(device_t brdev,
243 device_t child, int type, int *rid, u_long start,
244 u_long end, u_long count, uint flags);
245 static int pccbb_cardbus_release_resource(device_t brdev, device_t child,
246 int type, int rid, struct resource *res);
247 static int pccbb_power_enable_socket(device_t brdev, device_t child);
248 static void pccbb_power_disable_socket(device_t brdev, device_t child);
249 static int pccbb_activate_resource(device_t brdev, device_t child,
250 int type, int rid, struct resource *r);
251 static int pccbb_deactivate_resource(device_t brdev, device_t child,
252 int type, int rid, struct resource *r);
253 static struct resource *pccbb_alloc_resource(device_t brdev, device_t child,
254 int type, int *rid, u_long start, u_long end, u_long count,
256 static int pccbb_release_resource(device_t brdev, device_t child,
257 int type, int rid, struct resource *r);
258 static int pccbb_read_ivar(device_t brdev, device_t child, int which,
260 static int pccbb_write_ivar(device_t brdev, device_t child, int which,
262 static int pccbb_maxslots(device_t brdev);
263 static uint32_t pccbb_read_config(device_t brdev, int b, int s, int f,
265 static void pccbb_write_config(device_t brdev, int b, int s, int f,
266 int reg, uint32_t val, int width);
271 pccbb_set(struct pccbb_softc *sc, uint32_t reg, uint32_t val)
273 bus_space_write_4(sc->bst, sc->bsh, reg, val);
276 static __inline uint32_t
277 pccbb_get(struct pccbb_softc *sc, uint32_t reg)
279 return (bus_space_read_4(sc->bst, sc->bsh, reg));
283 pccbb_setb(struct pccbb_softc *sc, uint32_t reg, uint32_t bits)
285 pccbb_set(sc, reg, pccbb_get(sc, reg) | bits);
289 pccbb_clrb(struct pccbb_softc *sc, uint32_t reg, uint32_t bits)
291 pccbb_set(sc, reg, pccbb_get(sc, reg) & ~bits);
295 pccbb_remove_res(struct pccbb_softc *sc, struct resource *res)
297 struct pccbb_reslist *rle;
299 SLIST_FOREACH(rle, &sc->rl, link) {
300 if (rle->res == res) {
301 SLIST_REMOVE(&sc->rl, rle, pccbb_reslist, link);
308 static struct resource *
309 pccbb_find_res(struct pccbb_softc *sc, int type, int rid)
311 struct pccbb_reslist *rle;
313 SLIST_FOREACH(rle, &sc->rl, link)
314 if (SYS_RES_MEMORY == rle->type && rid == rle->rid)
320 pccbb_insert_res(struct pccbb_softc *sc, struct resource *res, int type,
323 struct pccbb_reslist *rle;
326 * Need to record allocated resource so we can iterate through
329 rle = malloc(sizeof(struct pccbb_reslist), M_DEVBUF, M_NOWAIT);
331 panic("pccbb_cardbus_alloc_resource: can't record entry!");
335 SLIST_INSERT_HEAD(&sc->rl, rle, link);
339 pccbb_destroy_res(struct pccbb_softc *sc)
341 struct pccbb_reslist *rle;
343 while ((rle = SLIST_FIRST(&sc->rl)) != NULL) {
344 device_printf(sc->dev, "Danger Will Robinson: Resource "
345 "left allocated! This is a bug... "
346 "(rid=%x, type=%d, addr=%lx)\n", rle->rid, rle->type,
347 rman_get_start(rle->res));
348 SLIST_REMOVE_HEAD(&sc->rl, link);
353 /************************************************************************/
355 /************************************************************************/
358 pccbb_chipset(uint32_t pci_id, const char **namep)
360 struct yenta_chipinfo *ycp;
362 for (ycp = yc_chipsets; ycp->yc_id != 0 && pci_id != ycp->yc_id; ++ycp)
365 *namep = ycp->yc_name;
366 return (ycp->yc_chiptype);
370 pccbb_probe(device_t brdev)
377 * Do we know that we support the chipset? If so, then we
380 if (pccbb_chipset(pci_get_devid(brdev), &name) != CB_UNKNOWN) {
381 device_set_desc(brdev, name);
386 * We do support generic CardBus bridges. All that we've seen
387 * to date have progif 0 (the Yenta spec, and successors mandate
388 * this). We do not support PCI PCMCIA bridges (with one exception)
389 * with this driver since they generally are I/O mapped. Those
390 * are supported by the pcic driver. This should help us be more
393 subclass = pci_get_subclass(brdev);
394 progif = pci_get_progif(brdev);
395 if (subclass == PCIS_BRIDGE_CARDBUS && progif == 0) {
396 device_set_desc(brdev, "PCI-CardBus Bridge");
404 pccbb_chipinit(struct pccbb_softc *sc)
406 uint32_t mux, sysctrl;
408 /* Set CardBus latency timer */
409 if (pci_read_config(sc->dev, PCIR_SECLAT_1, 1) < 0x20)
410 pci_write_config(sc->dev, PCIR_SECLAT_1, 0x20, 1);
412 /* Set PCI latency timer */
413 if (pci_read_config(sc->dev, PCIR_LATTIMER, 1) < 0x20)
414 pci_write_config(sc->dev, PCIR_LATTIMER, 0x20, 1);
416 /* Enable memory access */
417 PCI_MASK_CONFIG(sc->dev, PCIR_COMMAND,
420 | PCIM_CMD_BUSMASTEREN, 2);
422 /* disable Legacy IO */
423 switch (sc->chipset) {
425 PCI_MASK_CONFIG(sc->dev, CBBR_BRIDGECTRL,
426 & ~(CBBM_BRIDGECTRL_RL_3E0_EN |
427 CBBM_BRIDGECTRL_RL_3E2_EN), 2);
430 pci_write_config(sc->dev, CBBR_LEGACY, 0x0, 4);
434 /* Use PCI interrupt for interrupt routing */
435 PCI_MASK2_CONFIG(sc->dev, CBBR_BRIDGECTRL,
436 & ~(CBBM_BRIDGECTRL_MASTER_ABORT |
437 CBBM_BRIDGECTRL_INTR_IREQ_EN),
438 | CBBM_BRIDGECTRL_WRITE_POST_EN,
442 * XXX this should be a function table, ala OLDCARD. This means
443 * that we could more easily support ISA interrupts for pccard
444 * cards if we had to.
446 switch (sc->chipset) {
449 * The TI 1031, TI 1130 and TI 1131 all require another bit
450 * be set to enable PCI routing of interrupts, and then
451 * a bit for each of the CSC and Function interrupts we
454 PCI_MASK_CONFIG(sc->dev, CBBR_CBCTRL,
455 | CBBM_CBCTRL_113X_PCI_INTR |
456 CBBM_CBCTRL_113X_PCI_CSC | CBBM_CBCTRL_113X_PCI_IRQ_EN,
458 PCI_MASK_CONFIG(sc->dev, CBBR_DEVCTRL,
459 & ~(CBBM_DEVCTRL_INT_SERIAL |
460 CBBM_DEVCTRL_INT_PCI), 1);
464 * Some TI 12xx (and [14][45]xx) based pci cards
465 * sometimes have issues with the MFUNC register not
466 * being initialized due to a bad EEPROM on board.
467 * Laptops that this matters on have this register
468 * properly initialized.
470 * The TI125X parts have a different register.
472 mux = pci_read_config(sc->dev, CBBR_MFUNC, 4);
473 sysctrl = pci_read_config(sc->dev, CBBR_SYSCTRL, 4);
475 mux = (mux & ~CBBM_MFUNC_PIN0) |
476 CBBM_MFUNC_PIN0_INTA;
477 if ((sysctrl & CBBM_SYSCTRL_INTRTIE) == 0)
478 mux = (mux & ~CBBM_MFUNC_PIN1) |
479 CBBM_MFUNC_PIN1_INTB;
480 pci_write_config(sc->dev, CBBR_MFUNC, mux, 4);
485 * Disable zoom video. Some machines initialize this
486 * improperly and exerpience has shown that this helps
489 pci_write_config(sc->dev, CBBR_MMCTRL, 0, 4);
493 * Disable Zoom Video, ToPIC 97, 100.
495 pci_write_config(sc->dev, CBBR_TOPIC_ZV_CONTROL, 0, 1);
498 * At offset 0xa1: INTERRUPT CONTROL register
499 * 0x1: Turn on INT interrupts.
501 PCI_MASK_CONFIG(sc->dev, CBBR_TOPIC_INTCTRL,
502 | CBBM_TOPIC_INTCTRL_INTIRQSEL, 1);
506 * SOCKETCTRL appears to be TOPIC 95/B specific
508 PCI_MASK_CONFIG(sc->dev, CBBR_TOPIC_SOCKETCTRL,
509 | CBBM_TOPIC_SOCKETCTRL_SCR_IRQSEL, 4);
513 * At offset 0xa0: SLOT CONTROL
514 * 0x80 Enable Cardbus Functionality
515 * 0x40 Enable Cardbus and PC Card registers
516 * 0x20 Lock ID in exca regs
517 * 0x10 Write protect ID in config regs
518 * Clear the rest of the bits, which defaults the slot
519 * in legacy mode to 0x3e0 and offset 0. (legacy
520 * mode is determined elsewhere)
522 pci_write_config(sc->dev, CBBR_TOPIC_SLOTCTRL,
523 CBBM_TOPIC_SLOTCTRL_SLOTON |
524 CBBM_TOPIC_SLOTCTRL_SLOTEN |
525 CBBM_TOPIC_SLOTCTRL_ID_LOCK |
526 CBBM_TOPIC_SLOTCTRL_ID_WP, 1);
529 * At offset 0xa3 Card Detect Control Register
530 * 0x80 CARDBUS enbale
531 * 0x01 Cleared for hardware change detect
533 PCI_MASK2_CONFIG(sc->dev, CBBR_TOPIC_CDC,
534 | CBBM_TOPIC_CDC_CARDBUS,
535 & ~CBBM_TOPIC_CDC_SWDETECT, 4);
540 * Need to tell ExCA registers to route via PCI interrupts. There
541 * are two ways to do this. Once is to set INTR_ENABLE and the
542 * other is to set CSC to 0. Since both methods are mutually
543 * compatible, we do both.
545 exca_write(&sc->exca, EXCA_INTR, EXCA_INTR_ENABLE);
546 exca_write(&sc->exca, EXCA_CSC_INTR, 0);
548 /* close all memory and io windows */
549 pci_write_config(sc->dev, CBBR_MEMBASE0, 0xffffffff, 4);
550 pci_write_config(sc->dev, CBBR_MEMLIMIT0, 0, 4);
551 pci_write_config(sc->dev, CBBR_MEMBASE1, 0xffffffff, 4);
552 pci_write_config(sc->dev, CBBR_MEMLIMIT1, 0, 4);
553 pci_write_config(sc->dev, CBBR_IOBASE0, 0xffffffff, 4);
554 pci_write_config(sc->dev, CBBR_IOLIMIT0, 0, 4);
555 pci_write_config(sc->dev, CBBR_IOBASE1, 0xffffffff, 4);
556 pci_write_config(sc->dev, CBBR_IOLIMIT1, 0, 4);
560 pccbb_attach(device_t brdev)
562 struct pccbb_softc *sc = (struct pccbb_softc *)device_get_softc(brdev);
565 mtx_init(&sc->mtx, device_get_nameunit(brdev), "pccbb", MTX_DEF);
566 cv_init(&sc->cv, "pccbb cv");
567 sc->chipset = pccbb_chipset(pci_get_devid(brdev), NULL);
570 sc->pccarddev = NULL;
571 sc->secbus = pci_read_config(brdev, PCIR_SECBUS_2, 1);
572 sc->subbus = pci_read_config(brdev, PCIR_SUBBUS_2, 1);
576 * The PCI bus code should assign us memory in the absense
577 * of the BIOS doing so. However, 'should' isn't 'is,' so we kludge
578 * up something here until the PCI/acpi code properly assigns the
582 sc->base_res = bus_alloc_resource(brdev, SYS_RES_MEMORY, &rid,
583 0, ~0, 1, RF_ACTIVE);
586 * Generally, the BIOS will assign this memory for us.
587 * However, newer BIOSes do not because the MS design
588 * documents have mandated that this is for the OS
589 * to assign rather than the BIOS. This driver shouldn't
590 * be doing this, but until the pci bus code (or acpi)
591 * does this, we allow CardBus bridges to work on more
594 sockbase = pci_read_config(brdev, rid, 4);
595 if (sockbase < 0x100000 || sockbase >= 0xfffffff0) {
596 pci_write_config(brdev, rid, 0xffffffff, 4);
597 sockbase = pci_read_config(brdev, rid, 4);
598 sockbase = (sockbase & 0xfffffff0) &
599 -(sockbase & 0xfffffff0);
600 sc->base_res = bus_generic_alloc_resource(
601 device_get_parent(brdev), brdev, SYS_RES_MEMORY,
602 &rid, pccbb_start_mem, ~0, sockbase,
603 RF_ACTIVE|rman_make_alignment_flags(sockbase));
606 "Could not grab register memory\n");
607 mtx_destroy(&sc->mtx);
611 pci_write_config(brdev, CBBR_SOCKBASE,
612 rman_get_start(sc->base_res), 4);
613 DEVPRINTF((brdev, "PCI Memory allocated: %08lx\n",
614 rman_get_start(sc->base_res)));
616 device_printf(brdev, "Could not map register memory\n");
617 mtx_destroy(&sc->mtx);
623 sc->bst = rman_get_bustag(sc->base_res);
624 sc->bsh = rman_get_bushandle(sc->base_res);
625 exca_init(&sc->exca, brdev, sc->bst, sc->bsh, CBB_EXCA_OFFSET);
626 sc->exca.flags |= EXCA_HAS_MEMREG_WIN;
629 /* attach children */
630 sc->cbdev = device_add_child(brdev, "cardbus", -1);
631 if (sc->cbdev == NULL)
632 DEVPRINTF((brdev, "WARNING: cannot add cardbus bus.\n"));
633 else if (device_probe_and_attach(sc->cbdev) != 0) {
634 DEVPRINTF((brdev, "WARNING: cannot attach cardbus bus!\n"));
638 sc->pccarddev = device_add_child(brdev, "pccard", -1);
639 if (sc->pccarddev == NULL)
640 DEVPRINTF((brdev, "WARNING: cannot add pccard bus.\n"));
641 else if (device_probe_and_attach(sc->pccarddev) != 0) {
642 DEVPRINTF((brdev, "WARNING: cannot attach pccard bus.\n"));
643 sc->pccarddev = NULL;
646 /* Map and establish the interrupt. */
648 sc->irq_res = bus_alloc_resource(brdev, SYS_RES_IRQ, &rid, 0, ~0, 1,
649 RF_SHAREABLE | RF_ACTIVE);
650 if (sc->irq_res == NULL) {
651 printf("pccbb: Unable to map IRQ...\n");
652 bus_release_resource(brdev, SYS_RES_MEMORY, CBBR_SOCKBASE,
654 mtx_destroy(&sc->mtx);
659 if (bus_setup_intr(brdev, sc->irq_res, INTR_TYPE_AV, pccbb_intr, sc,
661 device_printf(brdev, "couldn't establish interrupt");
662 bus_release_resource(brdev, SYS_RES_IRQ, 0, sc->irq_res);
663 bus_release_resource(brdev, SYS_RES_MEMORY, CBBR_SOCKBASE,
665 mtx_destroy(&sc->mtx);
670 /* reset 16-bit pcmcia bus */
671 exca_clrb(&sc->exca, EXCA_INTR, EXCA_INTR_RESET);
674 pccbb_power(brdev, CARD_VCC_0V | CARD_VPP_0V);
676 /* CSC Interrupt: Card detect interrupt on */
677 pccbb_setb(sc, CBB_SOCKET_MASK, CBB_SOCKET_MASK_CD);
679 /* reset interrupt */
680 pccbb_set(sc, CBB_SOCKET_EVENT, pccbb_get(sc, CBB_SOCKET_EVENT));
682 /* Start the thread */
683 if (kthread_create(pccbb_event_thread, sc, &sc->event_thread, 0,
684 "%s%d", device_get_name(sc->dev), device_get_unit(sc->dev))) {
685 device_printf (sc->dev, "unable to create event thread.\n");
686 panic ("pccbb_create_event_thread");
693 pccbb_detach(device_t brdev)
695 struct pccbb_softc *sc = device_get_softc(brdev);
701 device_get_children(brdev, &devlist, &numdevs);
704 for (tmp = 0; tmp < numdevs; tmp++) {
705 if (device_detach(devlist[tmp]) == 0)
706 device_delete_child(brdev, devlist[tmp]);
710 free(devlist, M_TEMP);
715 bus_teardown_intr(brdev, sc->irq_res, sc->intrhand);
716 sc->flags |= PCCBB_KTHREAD_DONE;
717 if (sc->flags & PCCBB_KTHREAD_RUNNING) {
719 mtx_unlock(&sc->mtx);
720 DEVPRINTF((brdev, "waiting for kthread exit..."));
721 error = tsleep(sc, PWAIT, "pccbb-detach-wait", 60 * hz);
723 DPRINTF(("timeout\n"));
727 mtx_unlock(&sc->mtx);
730 bus_release_resource(brdev, SYS_RES_IRQ, 0, sc->irq_res);
731 bus_release_resource(brdev, SYS_RES_MEMORY, CBBR_SOCKBASE,
733 mtx_destroy(&sc->mtx);
739 pccbb_shutdown(device_t brdev)
741 struct pccbb_softc *sc = (struct pccbb_softc *)device_get_softc(brdev);
742 /* properly reset everything at shutdown */
744 PCI_MASK_CONFIG(brdev, CBBR_BRIDGECTRL, |CBBM_BRIDGECTRL_RESET, 2);
745 exca_clrb(&sc->exca, EXCA_INTR, EXCA_INTR_RESET);
747 pccbb_set(sc, CBB_SOCKET_MASK, 0);
749 pccbb_power(brdev, CARD_VCC_0V | CARD_VPP_0V);
751 exca_write(&sc->exca, EXCA_ADDRWIN_ENABLE, 0);
752 pci_write_config(brdev, CBBR_MEMBASE0, 0, 4);
753 pci_write_config(brdev, CBBR_MEMLIMIT0, 0, 4);
754 pci_write_config(brdev, CBBR_MEMBASE1, 0, 4);
755 pci_write_config(brdev, CBBR_MEMLIMIT1, 0, 4);
756 pci_write_config(brdev, CBBR_IOBASE0, 0, 4);
757 pci_write_config(brdev, CBBR_IOLIMIT0, 0, 4);
758 pci_write_config(brdev, CBBR_IOBASE1, 0, 4);
759 pci_write_config(brdev, CBBR_IOLIMIT1, 0, 4);
760 pci_write_config(brdev, PCIR_COMMAND, 0, 2);
765 pccbb_setup_intr(device_t dev, device_t child, struct resource *irq,
766 int flags, driver_intr_t *intr, void *arg, void **cookiep)
771 * You aren't allowed to have fast interrupts for pccard/cardbus
772 * things since those interrupts are PCI and shared. Since we use
773 * the PCI interrupt for the status change interrupts, it can't be
774 * free for use by the driver. Fast interrupts must not be shared.
776 if ((flags & INTR_FAST) != 0)
778 err = bus_generic_setup_intr(dev, child, irq, flags, intr, arg,
781 * XXX need to turn on ISA interrupts, if we ever support them, but
782 * XXX for now that's all we need to do.
788 pccbb_teardown_intr(device_t dev, device_t child, struct resource *irq,
791 /* XXX Need to do different things for ISA interrupts. */
792 return (bus_generic_teardown_intr(dev, child, irq, cookie));
797 pccbb_driver_added(device_t brdev, driver_t *driver)
799 struct pccbb_softc *sc = device_get_softc(brdev);
806 DEVICE_IDENTIFY(driver, brdev);
807 device_get_children(brdev, &devlist, &numdevs);
809 sockstate = pccbb_get(sc, CBB_SOCKET_STATE);
810 for (tmp = 0; tmp < numdevs; tmp++) {
811 if (device_get_state(devlist[tmp]) == DS_NOTPRESENT &&
812 device_probe_and_attach(devlist[tmp]) == 0) {
813 if (devlist[tmp] == NULL)
815 else if (strcmp(driver->name, "cardbus") == 0) {
816 sc->cbdev = devlist[tmp];
817 if (((sockstate & CBB_SOCKET_STAT_CD) == 0) &&
818 (sockstate & CBB_SOCKET_STAT_CB))
820 } else if (strcmp(driver->name, "pccard") == 0) {
821 sc->pccarddev = devlist[tmp];
822 if (((sockstate & CBB_SOCKET_STAT_CD) == 0) &&
823 (sockstate & CBB_SOCKET_STAT_16BIT))
827 "Unsupported child bus: %s\n",
831 free(devlist, M_TEMP);
834 if ((pccbb_get(sc, CBB_SOCKET_STATE) & CBB_SOCKET_STAT_CD)
838 mtx_unlock(&sc->mtx);
844 pccbb_child_detached(device_t brdev, device_t child)
846 struct pccbb_softc *sc = device_get_softc(brdev);
848 if (child == sc->cbdev)
850 else if (child == sc->pccarddev)
851 sc->pccarddev = NULL;
853 device_printf(brdev, "Unknown child detached: %s %p/%p\n",
854 device_get_nameunit(child), sc->cbdev, sc->pccarddev);
858 pccbb_card_reprobe(device_t brdev, device_t busdev)
860 struct pccbb_softc *sc = device_get_softc(brdev);
864 sockstate = pccbb_get(sc, CBB_SOCKET_STATE);
866 if ((sockstate & CBB_SOCKET_STAT_CD) == 0) {
867 if (busdev == sc->cbdev &&
868 (sockstate & CBB_SOCKET_STAT_CB))
870 else if (busdev == sc->pccarddev &&
871 (sockstate & CBB_SOCKET_STAT_16BIT))
877 mtx_unlock(&sc->mtx);
885 /************************************************************************/
887 /************************************************************************/
890 pccbb_event_thread(void *arg)
892 struct pccbb_softc *sc = arg;
897 * We take out Giant here because we drop it in tsleep
898 * and need it for kthread_exit, which drops it.
901 sc->flags |= PCCBB_KTHREAD_RUNNING;
904 * Check to see if we have anything first so that
905 * if there's a card already inserted, we do the
908 if (sc->flags & PCCBB_KTHREAD_DONE)
911 status = pccbb_get(sc, CBB_SOCKET_STATE);
912 if ((status & CBB_SOCKET_STAT_CD) == 0)
917 * Wait until it has been 1s since the last time we
918 * get an interrupt. We handle the rest of the interrupt
919 * at the top of the loop.
922 cv_wait(&sc->cv, &sc->mtx);
924 err = cv_timedwait(&sc->cv, &sc->mtx, 1 * hz);
925 } while (err != EWOULDBLOCK &&
926 (sc->flags & PCCBB_KTHREAD_DONE) == 0);
927 mtx_unlock(&sc->mtx);
929 sc->flags &= ~PCCBB_KTHREAD_RUNNING;
931 * XXX I think there's a race here. If we wakeup in the other
932 * thread before kthread_exit is called and this routine returns,
933 * and that thread causes us to be unmapped, then we are setting
934 * ourselves up for a panic. Make sure that I check out
935 * jhb's crash.c for a fix.
941 /************************************************************************/
943 /************************************************************************/
946 pccbb_insert(struct pccbb_softc *sc)
948 uint32_t sockevent, sockstate;
952 * Debounce interrupt. However, most of the debounce
953 * is done in the thread's timeout routines.
956 sockevent = pccbb_get(sc, CBB_SOCKET_EVENT);
957 sockstate = pccbb_get(sc, CBB_SOCKET_STATE);
958 } while (sockstate & CBB_SOCKET_STAT_CD && --timeout > 0);
961 device_printf (sc->dev, "insert timeout");
965 DEVPRINTF((sc->dev, "card inserted: event=0x%08x, state=%08x\n",
966 sockevent, sockstate));
968 if (sockstate & CBB_SOCKET_STAT_16BIT) {
969 if (sc->pccarddev != NULL) {
970 sc->flags |= PCCBB_16BIT_CARD;
971 if (CARD_ATTACH_CARD(sc->pccarddev) != 0)
972 device_printf(sc->dev,
973 "PC Card card activation failed\n");
975 device_printf(sc->dev,
976 "PC Card inserted, but no pccard bus.\n");
978 } else if (sockstate & CBB_SOCKET_STAT_CB) {
979 if (sc->cbdev != NULL) {
980 sc->flags &= ~PCCBB_16BIT_CARD;
981 if (CARD_ATTACH_CARD(sc->cbdev) != 0)
982 device_printf(sc->dev,
983 "CardBus card activation failed\n");
985 device_printf(sc->dev,
986 "CardBUS card inserted, but no cardbus bus.\n");
990 * We should power the card down, and try again a couple of
991 * times if this happens. XXX
993 device_printf (sc->dev, "Unsupported card type detected\n");
998 pccbb_removal(struct pccbb_softc *sc)
1000 if (sc->flags & PCCBB_16BIT_CARD && sc->pccarddev != NULL)
1001 CARD_DETACH_CARD(sc->pccarddev, DETACH_FORCE);
1002 else if ((!(sc->flags & PCCBB_16BIT_CARD)) && sc->cbdev != NULL)
1003 CARD_DETACH_CARD(sc->cbdev, DETACH_FORCE);
1004 pccbb_destroy_res(sc);
1007 /************************************************************************/
1008 /* Interrupt Handler */
1009 /************************************************************************/
1012 pccbb_intr(void *arg)
1014 struct pccbb_softc *sc = arg;
1018 * This ISR needs work XXX
1020 sockevent = pccbb_get(sc, CBB_SOCKET_EVENT);
1022 /* ack the interrupt */
1023 pccbb_setb(sc, CBB_SOCKET_EVENT, sockevent);
1025 if (sockevent & CBB_SOCKET_EVENT_CD) {
1028 mtx_unlock(&sc->mtx);
1030 if (sockevent & CBB_SOCKET_EVENT_CSTS) {
1031 DPRINTF((" cstsevent occured: 0x%08x\n",
1032 pccbb_get(sc, CBB_SOCKET_STATE)));
1034 if (sockevent & CBB_SOCKET_EVENT_POWER) {
1035 DPRINTF((" pwrevent occured: 0x%08x\n",
1036 pccbb_get(sc, CBB_SOCKET_STATE)));
1041 /* Call the interrupt if we still have the card */
1044 /************************************************************************/
1045 /* Generic Power functions */
1046 /************************************************************************/
1049 pccbb_detect_voltage(device_t brdev)
1051 struct pccbb_softc *sc = device_get_softc(brdev);
1053 int vol = CARD_UKN_CARD;
1055 psr = pccbb_get(sc, CBB_SOCKET_STATE);
1057 if (psr & CBB_SOCKET_STAT_5VCARD)
1058 vol |= CARD_5V_CARD;
1059 if (psr & CBB_SOCKET_STAT_3VCARD)
1060 vol |= CARD_3V_CARD;
1061 if (psr & CBB_SOCKET_STAT_XVCARD)
1062 vol |= CARD_XV_CARD;
1063 if (psr & CBB_SOCKET_STAT_YVCARD)
1064 vol |= CARD_YV_CARD;
1070 pccbb_power(device_t brdev, int volts)
1072 uint32_t status, sock_ctrl;
1073 struct pccbb_softc *sc = device_get_softc(brdev);
1077 DEVPRINTF((sc->dev, "pccbb_power: %s and %s [%x]\n",
1078 (volts & CARD_VCCMASK) == CARD_VCC_UC ? "CARD_VCC_UC" :
1079 (volts & CARD_VCCMASK) == CARD_VCC_5V ? "CARD_VCC_5V" :
1080 (volts & CARD_VCCMASK) == CARD_VCC_3V ? "CARD_VCC_3V" :
1081 (volts & CARD_VCCMASK) == CARD_VCC_XV ? "CARD_VCC_XV" :
1082 (volts & CARD_VCCMASK) == CARD_VCC_YV ? "CARD_VCC_YV" :
1083 (volts & CARD_VCCMASK) == CARD_VCC_0V ? "CARD_VCC_0V" :
1085 (volts & CARD_VPPMASK) == CARD_VPP_UC ? "CARD_VPP_UC" :
1086 (volts & CARD_VPPMASK) == CARD_VPP_12V ? "CARD_VPP_12V" :
1087 (volts & CARD_VPPMASK) == CARD_VPP_VCC ? "CARD_VPP_VCC" :
1088 (volts & CARD_VPPMASK) == CARD_VPP_0V ? "CARD_VPP_0V" :
1092 status = pccbb_get(sc, CBB_SOCKET_STATE);
1093 sock_ctrl = pccbb_get(sc, CBB_SOCKET_CONTROL);
1095 switch (volts & CARD_VCCMASK) {
1099 if (CBB_SOCKET_STAT_5VCARD & status) { /* check 5 V card */
1100 sock_ctrl &= ~CBB_SOCKET_CTRL_VCCMASK;
1101 sock_ctrl |= CBB_SOCKET_CTRL_VCC_5V;
1103 device_printf(sc->dev,
1104 "BAD voltage request: no 5 V card\n");
1108 if (CBB_SOCKET_STAT_3VCARD & status) {
1109 sock_ctrl &= ~CBB_SOCKET_CTRL_VCCMASK;
1110 sock_ctrl |= CBB_SOCKET_CTRL_VCC_3V;
1112 device_printf(sc->dev,
1113 "BAD voltage request: no 3.3 V card\n");
1117 sock_ctrl &= ~CBB_SOCKET_CTRL_VCCMASK;
1120 return (0); /* power NEVER changed */
1124 switch (volts & CARD_VPPMASK) {
1128 sock_ctrl &= ~CBB_SOCKET_CTRL_VPPMASK;
1131 sock_ctrl &= ~CBB_SOCKET_CTRL_VPPMASK;
1132 sock_ctrl |= ((sock_ctrl >> 4) & 0x07);
1135 sock_ctrl &= ~CBB_SOCKET_CTRL_VPPMASK;
1136 sock_ctrl |= CBB_SOCKET_CTRL_VPP_12V;
1140 if (pccbb_get(sc, CBB_SOCKET_CONTROL) == sock_ctrl)
1141 return (1); /* no change necessary */
1143 pccbb_set(sc, CBB_SOCKET_CONTROL, sock_ctrl);
1144 status = pccbb_get(sc, CBB_SOCKET_STATE);
1147 * XXX This busy wait is bogus. We should wait for a power
1148 * interrupt and then whine if the status is bad. If we're
1149 * worried about the card not coming up, then we should also
1150 * schedule a timeout which we can cacel in the power interrupt.
1155 sockevent = pccbb_get(sc, CBB_SOCKET_EVENT);
1156 } while (!(sockevent & CBB_SOCKET_EVENT_POWER) && --timeout > 0);
1157 /* reset event status */
1158 /* XXX should only reset EVENT_POWER */
1159 pccbb_set(sc, CBB_SOCKET_EVENT, sockevent);
1161 printf ("VCC supply failed.\n");
1166 * delay 400 ms: thgough the standard defines that the Vcc set-up time
1167 * is 20 ms, some PC-Card bridge requires longer duration.
1168 * XXX Note: We should check the stutus AFTER the delay to give time
1169 * for things to stabilize.
1173 if (status & CBB_SOCKET_STAT_BADVCC) {
1174 device_printf(sc->dev,
1175 "bad Vcc request. ctrl=0x%x, status=0x%x\n",
1177 printf("pccbb_power: %s and %s [%x]\n",
1178 (volts & CARD_VCCMASK) == CARD_VCC_UC ? "CARD_VCC_UC" :
1179 (volts & CARD_VCCMASK) == CARD_VCC_5V ? "CARD_VCC_5V" :
1180 (volts & CARD_VCCMASK) == CARD_VCC_3V ? "CARD_VCC_3V" :
1181 (volts & CARD_VCCMASK) == CARD_VCC_XV ? "CARD_VCC_XV" :
1182 (volts & CARD_VCCMASK) == CARD_VCC_YV ? "CARD_VCC_YV" :
1183 (volts & CARD_VCCMASK) == CARD_VCC_0V ? "CARD_VCC_0V" :
1185 (volts & CARD_VPPMASK) == CARD_VPP_UC ? "CARD_VPP_UC" :
1186 (volts & CARD_VPPMASK) == CARD_VPP_12V ? "CARD_VPP_12V":
1187 (volts & CARD_VPPMASK) == CARD_VPP_VCC ? "CARD_VPP_VCC":
1188 (volts & CARD_VPPMASK) == CARD_VPP_0V ? "CARD_VPP_0V" :
1193 return (1); /* power changed correctly */
1197 * detect the voltage for the card, and set it. Since the power
1198 * used is the square of the voltage, lower voltages is a big win
1199 * and what Windows does (and what Microsoft prefers). The MS paper
1200 * also talks about preferring the CIS entry as well.
1203 pccbb_do_power(device_t brdev)
1207 /* Prefer lowest voltage supported */
1208 voltage = pccbb_detect_voltage(brdev);
1209 pccbb_power(brdev, CARD_VCC_0V | CARD_VPP_0V);
1210 if (voltage & CARD_YV_CARD)
1211 pccbb_power(brdev, CARD_VCC_YV | CARD_VPP_VCC);
1212 else if (voltage & CARD_XV_CARD)
1213 pccbb_power(brdev, CARD_VCC_XV | CARD_VPP_VCC);
1214 else if (voltage & CARD_3V_CARD)
1215 pccbb_power(brdev, CARD_VCC_3V | CARD_VPP_VCC);
1216 else if (voltage & CARD_5V_CARD)
1217 pccbb_power(brdev, CARD_VCC_5V | CARD_VPP_VCC);
1219 device_printf(brdev, "Unknown card voltage\n");
1225 /************************************************************************/
1226 /* Cardbus power functions */
1227 /************************************************************************/
1230 pccbb_cardbus_reset(device_t brdev)
1232 struct pccbb_softc *sc = device_get_softc(brdev);
1235 delay_us = sc->chipset == CB_RF5C47X ? 400*1000 : 20*1000;
1237 PCI_MASK_CONFIG(brdev, CBBR_BRIDGECTRL, |CBBM_BRIDGECTRL_RESET, 2);
1241 /* If a card exists, unreset it! */
1242 if ((pccbb_get(sc, CBB_SOCKET_STATE) & CBB_SOCKET_STAT_CD) == 0) {
1243 PCI_MASK_CONFIG(brdev, CBBR_BRIDGECTRL,
1244 &~CBBM_BRIDGECTRL_RESET, 2);
1250 pccbb_cardbus_power_enable_socket(device_t brdev, device_t child)
1252 struct pccbb_softc *sc = device_get_softc(brdev);
1255 if ((pccbb_get(sc, CBB_SOCKET_STATE) & CBB_SOCKET_STAT_CD) ==
1259 err = pccbb_do_power(brdev);
1262 pccbb_cardbus_reset(brdev);
1267 pccbb_cardbus_power_disable_socket(device_t brdev, device_t child)
1269 pccbb_power(brdev, CARD_VCC_0V | CARD_VPP_0V);
1270 pccbb_cardbus_reset(brdev);
1273 /************************************************************************/
1274 /* Cardbus Resource */
1275 /************************************************************************/
1278 pccbb_cardbus_io_open(device_t brdev, int win, uint32_t start, uint32_t end)
1283 if ((win < 0) || (win > 1)) {
1285 "pccbb_cardbus_io_open: window out of range %d\n", win));
1289 basereg = win * 8 + CBBR_IOBASE0;
1290 limitreg = win * 8 + CBBR_IOLIMIT0;
1292 pci_write_config(brdev, basereg, start, 4);
1293 pci_write_config(brdev, limitreg, end, 4);
1298 pccbb_cardbus_mem_open(device_t brdev, int win, uint32_t start, uint32_t end)
1303 if ((win < 0) || (win > 1)) {
1305 "pccbb_cardbus_mem_open: window out of range %d\n", win));
1309 basereg = win*8 + CBBR_MEMBASE0;
1310 limitreg = win*8 + CBBR_MEMLIMIT0;
1312 pci_write_config(brdev, basereg, start, 4);
1313 pci_write_config(brdev, limitreg, end, 4);
1318 * XXX The following function belongs in the pci bus layer.
1321 pccbb_cardbus_auto_open(struct pccbb_softc *sc, int type)
1325 struct pccbb_reslist *rle;
1327 int prefetchable[2];
1330 starts[0] = starts[1] = 0xffffffff;
1331 ends[0] = ends[1] = 0;
1333 if (type == SYS_RES_MEMORY)
1334 align = CBB_MEMALIGN;
1335 else if (type == SYS_RES_IOPORT)
1336 align = CBB_IOALIGN;
1340 SLIST_FOREACH(rle, &sc->rl, link) {
1341 if (rle->type != type)
1343 else if (rle->res == NULL) {
1344 device_printf(sc->dev, "WARNING: Resource not reserved? "
1345 "(type=%d, addr=%lx)\n",
1346 rle->type, rman_get_start(rle->res));
1347 } else if (!(rman_get_flags(rle->res) & RF_ACTIVE)) {
1349 } else if (starts[0] == 0xffffffff) {
1350 starts[0] = rman_get_start(rle->res);
1351 ends[0] = rman_get_end(rle->res);
1353 rman_get_flags(rle->res) & RF_PREFETCHABLE;
1354 } else if (rman_get_end(rle->res) > ends[0] &&
1355 rman_get_start(rle->res) - ends[0] <
1356 PCCBB_AUTO_OPEN_SMALLHOLE && prefetchable[0] ==
1357 (rman_get_flags(rle->res) & RF_PREFETCHABLE)) {
1358 ends[0] = rman_get_end(rle->res);
1359 } else if (rman_get_start(rle->res) < starts[0] &&
1360 starts[0] - rman_get_end(rle->res) <
1361 PCCBB_AUTO_OPEN_SMALLHOLE && prefetchable[0] ==
1362 (rman_get_flags(rle->res) & RF_PREFETCHABLE)) {
1363 starts[0] = rman_get_start(rle->res);
1364 } else if (starts[1] == 0xffffffff) {
1365 starts[1] = rman_get_start(rle->res);
1366 ends[1] = rman_get_end(rle->res);
1368 rman_get_flags(rle->res) & RF_PREFETCHABLE;
1369 } else if (rman_get_end(rle->res) > ends[1] &&
1370 rman_get_start(rle->res) - ends[1] <
1371 PCCBB_AUTO_OPEN_SMALLHOLE && prefetchable[1] ==
1372 (rman_get_flags(rle->res) & RF_PREFETCHABLE)) {
1373 ends[1] = rman_get_end(rle->res);
1374 } else if (rman_get_start(rle->res) < starts[1] &&
1375 starts[1] - rman_get_end(rle->res) <
1376 PCCBB_AUTO_OPEN_SMALLHOLE && prefetchable[1] ==
1377 (rman_get_flags(rle->res) & RF_PREFETCHABLE)) {
1378 starts[1] = rman_get_start(rle->res);
1383 diffs[0] = diffs[1] = 0xffffffff;
1384 if (rman_get_start(rle->res) > ends[0])
1385 diffs[0] = rman_get_start(rle->res) - ends[0];
1386 else if (rman_get_end(rle->res) < starts[0])
1387 diffs[0] = starts[0] - rman_get_end(rle->res);
1388 if (rman_get_start(rle->res) > ends[1])
1389 diffs[1] = rman_get_start(rle->res) - ends[1];
1390 else if (rman_get_end(rle->res) < starts[1])
1391 diffs[1] = starts[1] - rman_get_end(rle->res);
1393 win = (diffs[0] <= diffs[1])?0:1;
1394 if (rman_get_start(rle->res) > ends[win])
1395 ends[win] = rman_get_end(rle->res);
1396 else if (rman_get_end(rle->res) < starts[win])
1397 starts[win] = rman_get_start(rle->res);
1398 if (!(rman_get_flags(rle->res) & RF_PREFETCHABLE))
1399 prefetchable[win] = 0;
1402 if (starts[0] != 0xffffffff)
1403 starts[0] -= starts[0] % align;
1404 if (starts[1] != 0xffffffff)
1405 starts[1] -= starts[1] % align;
1406 if (ends[0] % align != 0)
1407 ends[0] += align - ends[0]%align - 1;
1408 if (ends[1] % align != 0)
1409 ends[1] += align - ends[1]%align - 1;
1412 if (type == SYS_RES_MEMORY) {
1413 pccbb_cardbus_mem_open(sc->dev, 0, starts[0], ends[0]);
1414 pccbb_cardbus_mem_open(sc->dev, 1, starts[1], ends[1]);
1415 reg = pci_read_config(sc->dev, CBBR_BRIDGECTRL, 2);
1416 reg &= ~(CBBM_BRIDGECTRL_PREFETCH_0|
1417 CBBM_BRIDGECTRL_PREFETCH_1);
1418 reg |= (prefetchable[0]?CBBM_BRIDGECTRL_PREFETCH_0:0)|
1419 (prefetchable[1]?CBBM_BRIDGECTRL_PREFETCH_1:0);
1420 pci_write_config(sc->dev, CBBR_BRIDGECTRL, reg, 2);
1421 } else if (type == SYS_RES_IOPORT) {
1422 pccbb_cardbus_io_open(sc->dev, 0, starts[0], ends[0]);
1423 pccbb_cardbus_io_open(sc->dev, 1, starts[1], ends[1]);
1428 pccbb_cardbus_activate_resource(device_t brdev, device_t child, int type,
1429 int rid, struct resource *res)
1433 ret = BUS_ACTIVATE_RESOURCE(device_get_parent(brdev), child,
1437 pccbb_cardbus_auto_open(device_get_softc(brdev), type);
1442 pccbb_cardbus_deactivate_resource(device_t brdev, device_t child, int type,
1443 int rid, struct resource *res)
1447 ret = BUS_DEACTIVATE_RESOURCE(device_get_parent(brdev), child,
1451 pccbb_cardbus_auto_open(device_get_softc(brdev), type);
1455 static struct resource *
1456 pccbb_cardbus_alloc_resource(device_t brdev, device_t child, int type,
1457 int *rid, u_long start, u_long end, u_long count, uint flags)
1459 struct pccbb_softc *sc = device_get_softc(brdev);
1461 struct resource *res;
1465 tmp = rman_get_start(sc->irq_res);
1466 if (start > tmp || end < tmp || count != 1) {
1467 device_printf(child, "requested interrupt %ld-%ld,"
1468 "count = %ld not supported by pccbb\n",
1474 case SYS_RES_IOPORT:
1475 if (start <= pccbb_start_32_io)
1476 start = pccbb_start_32_io;
1480 case SYS_RES_MEMORY:
1481 if (start <= pccbb_start_mem)
1482 start = pccbb_start_mem;
1488 res = BUS_ALLOC_RESOURCE(device_get_parent(brdev), child, type, rid,
1489 start, end, count, flags & ~RF_ACTIVE);
1491 printf("pccbb alloc res fail\n");
1494 pccbb_insert_res(sc, res, type, *rid);
1495 if (flags & RF_ACTIVE)
1496 if (bus_activate_resource(child, type, *rid, res) != 0) {
1497 bus_release_resource(child, type, *rid, res);
1505 pccbb_cardbus_release_resource(device_t brdev, device_t child, int type,
1506 int rid, struct resource *res)
1508 struct pccbb_softc *sc = device_get_softc(brdev);
1511 if (rman_get_flags(res) & RF_ACTIVE) {
1512 error = bus_deactivate_resource(child, type, rid, res);
1516 pccbb_remove_res(sc, res);
1517 return (BUS_RELEASE_RESOURCE(device_get_parent(brdev), child,
1521 /************************************************************************/
1522 /* PC Card Power Functions */
1523 /************************************************************************/
1526 pccbb_pcic_power_enable_socket(device_t brdev, device_t child)
1528 struct pccbb_softc *sc = device_get_softc(brdev);
1531 DPRINTF(("pccbb_pcic_socket_enable:\n"));
1533 /* power down/up the socket to reset */
1534 err = pccbb_do_power(brdev);
1537 exca_reset(&sc->exca, child);
1543 pccbb_pcic_power_disable_socket(device_t brdev, device_t child)
1545 struct pccbb_softc *sc = device_get_softc(brdev);
1547 DPRINTF(("pccbb_pcic_socket_disable\n"));
1549 /* reset signal asserting... */
1550 exca_clrb(&sc->exca, EXCA_INTR, EXCA_INTR_RESET);
1553 /* power down the socket */
1554 pccbb_power(brdev, CARD_VCC_0V | CARD_VPP_0V);
1555 exca_clrb(&sc->exca, EXCA_PWRCTL, EXCA_PWRCTL_OE);
1557 /* wait 300ms until power fails (Tpf). */
1561 /************************************************************************/
1563 /************************************************************************/
1566 pccbb_power_enable_socket(device_t brdev, device_t child)
1568 struct pccbb_softc *sc = device_get_softc(brdev);
1570 if (sc->flags & PCCBB_16BIT_CARD)
1571 return (pccbb_pcic_power_enable_socket(brdev, child));
1573 return (pccbb_cardbus_power_enable_socket(brdev, child));
1577 pccbb_power_disable_socket(device_t brdev, device_t child)
1579 struct pccbb_softc *sc = device_get_softc(brdev);
1580 if (sc->flags & PCCBB_16BIT_CARD)
1581 pccbb_pcic_power_disable_socket(brdev, child);
1583 pccbb_cardbus_power_disable_socket(brdev, child);
1586 pccbb_pcic_activate_resource(device_t brdev, device_t child, int type, int rid,
1587 struct resource *res)
1590 struct pccbb_softc *sc = device_get_softc(brdev);
1591 if (!(rman_get_flags(res) & RF_ACTIVE)) { /* not already activated */
1593 case SYS_RES_IOPORT:
1594 err = exca_io_map(&sc->exca, 0, res);
1596 case SYS_RES_MEMORY:
1597 err = exca_mem_map(&sc->exca, 0, res);
1607 return (BUS_ACTIVATE_RESOURCE(device_get_parent(brdev), child,
1612 pccbb_pcic_deactivate_resource(device_t brdev, device_t child, int type,
1613 int rid, struct resource *res)
1615 struct pccbb_softc *sc = device_get_softc(brdev);
1617 if (rman_get_flags(res) & RF_ACTIVE) { /* if activated */
1619 case SYS_RES_IOPORT:
1620 if (exca_io_unmap_res(&sc->exca, res))
1623 case SYS_RES_MEMORY:
1624 if (exca_mem_unmap_res(&sc->exca, res))
1629 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(brdev), child,
1633 static struct resource *
1634 pccbb_pcic_alloc_resource(device_t brdev, device_t child, int type, int *rid,
1635 u_long start, u_long end, u_long count, uint flags)
1637 struct resource *res = NULL;
1638 struct pccbb_softc *sc = device_get_softc(brdev);
1642 case SYS_RES_MEMORY:
1643 if (start < pccbb_start_mem)
1644 start = pccbb_start_mem;
1647 flags = (flags & ~RF_ALIGNMENT_MASK) |
1648 rman_make_alignment_flags(CBB_MEMALIGN);
1650 case SYS_RES_IOPORT:
1651 if (start < pccbb_start_16_io)
1652 start = pccbb_start_16_io;
1657 tmp = rman_get_start(sc->irq_res);
1658 if (start > tmp || end < tmp || count != 1) {
1659 device_printf(child, "requested interrupt %ld-%ld,"
1660 "count = %ld not supported by pccbb\n",
1664 flags |= RF_SHAREABLE;
1665 start = end = rman_get_start(sc->irq_res);
1668 res = BUS_ALLOC_RESOURCE(device_get_parent(brdev), child, type, rid,
1669 start, end, count, flags & ~RF_ACTIVE);
1672 pccbb_insert_res(sc, res, type, *rid);
1673 if (flags & RF_ACTIVE) {
1674 if (bus_activate_resource(child, type, *rid, res) != 0) {
1675 bus_release_resource(child, type, *rid, res);
1684 pccbb_pcic_release_resource(device_t brdev, device_t child, int type,
1685 int rid, struct resource *res)
1687 struct pccbb_softc *sc = device_get_softc(brdev);
1690 if (rman_get_flags(res) & RF_ACTIVE) {
1691 error = bus_deactivate_resource(child, type, rid, res);
1695 pccbb_remove_res(sc, res);
1696 return (BUS_RELEASE_RESOURCE(device_get_parent(brdev), child,
1700 /************************************************************************/
1701 /* PC Card methods */
1702 /************************************************************************/
1705 pccbb_pcic_set_res_flags(device_t brdev, device_t child, int type, int rid,
1708 struct pccbb_softc *sc = device_get_softc(brdev);
1709 struct resource *res;
1711 if (type != SYS_RES_MEMORY)
1713 res = pccbb_find_res(sc, type, rid);
1715 device_printf(brdev,
1716 "set_res_flags: specified rid not found\n");
1719 return (exca_mem_set_flags(&sc->exca, res, flags));
1723 pccbb_pcic_set_memory_offset(device_t brdev, device_t child, int rid,
1724 uint32_t cardaddr, uint32_t *deltap)
1726 struct pccbb_softc *sc = device_get_softc(brdev);
1727 struct resource *res;
1729 res = pccbb_find_res(sc, SYS_RES_MEMORY, rid);
1731 device_printf(brdev,
1732 "set_memory_offset: specified rid not found\n");
1735 return (exca_mem_set_offset(&sc->exca, res, cardaddr, deltap));
1738 /************************************************************************/
1740 /************************************************************************/
1744 pccbb_activate_resource(device_t brdev, device_t child, int type, int rid,
1747 struct pccbb_softc *sc = device_get_softc(brdev);
1749 if (sc->flags & PCCBB_16BIT_CARD)
1750 return (pccbb_pcic_activate_resource(brdev, child, type, rid, r));
1752 return (pccbb_cardbus_activate_resource(brdev, child, type, rid,
1757 pccbb_deactivate_resource(device_t brdev, device_t child, int type,
1758 int rid, struct resource *r)
1760 struct pccbb_softc *sc = device_get_softc(brdev);
1762 if (sc->flags & PCCBB_16BIT_CARD)
1763 return (pccbb_pcic_deactivate_resource(brdev, child, type,
1766 return (pccbb_cardbus_deactivate_resource(brdev, child, type,
1770 static struct resource *
1771 pccbb_alloc_resource(device_t brdev, device_t child, int type, int *rid,
1772 u_long start, u_long end, u_long count, uint flags)
1774 struct pccbb_softc *sc = device_get_softc(brdev);
1776 if (sc->flags & PCCBB_16BIT_CARD)
1777 return (pccbb_pcic_alloc_resource(brdev, child, type, rid,
1778 start, end, count, flags));
1780 return (pccbb_cardbus_alloc_resource(brdev, child, type, rid,
1781 start, end, count, flags));
1785 pccbb_release_resource(device_t brdev, device_t child, int type, int rid,
1788 struct pccbb_softc *sc = device_get_softc(brdev);
1790 if (sc->flags & PCCBB_16BIT_CARD)
1791 return (pccbb_pcic_release_resource(brdev, child, type,
1794 return (pccbb_cardbus_release_resource(brdev, child, type,
1799 pccbb_read_ivar(device_t brdev, device_t child, int which, uintptr_t *result)
1801 struct pccbb_softc *sc = device_get_softc(brdev);
1805 *result = sc->secbus;
1812 pccbb_write_ivar(device_t brdev, device_t child, int which, uintptr_t value)
1814 struct pccbb_softc *sc = device_get_softc(brdev);
1824 /************************************************************************/
1825 /* PCI compat methods */
1826 /************************************************************************/
1829 pccbb_maxslots(device_t brdev)
1835 pccbb_read_config(device_t brdev, int b, int s, int f, int reg, int width)
1838 * Pass through to the next ppb up the chain (i.e. our grandparent).
1840 return (PCIB_READ_CONFIG(device_get_parent(device_get_parent(brdev)),
1841 b, s, f, reg, width));
1845 pccbb_write_config(device_t brdev, int b, int s, int f, int reg, uint32_t val,
1849 * Pass through to the next ppb up the chain (i.e. our grandparent).
1851 PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(brdev)),
1852 b, s, f, reg, val, width);
1856 pccbb_suspend(device_t self)
1859 struct pccbb_softc* sc = device_get_softc(self);
1861 bus_teardown_intr(self, sc->irq_res, sc->intrhand);
1862 error = bus_generic_suspend(self);
1867 pccbb_resume(device_t self)
1870 struct pccbb_softc *sc = (struct pccbb_softc *)device_get_softc(self);
1873 pci_write_config(self, CBBR_SOCKBASE, rman_get_start(sc->base_res), 4);
1874 DEVPRINTF((self, "PCI Memory allocated: %08lx\n",
1875 rman_get_start(sc->base_res)));
1879 /* re-establish the interrupt. */
1880 if (bus_setup_intr(self, sc->irq_res, INTR_TYPE_AV, pccbb_intr, sc,
1882 device_printf(self, "couldn't re-establish interrupt");
1883 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
1884 bus_release_resource(self, SYS_RES_MEMORY, CBBR_SOCKBASE,
1887 sc->base_res = NULL;
1891 /* CSC Interrupt: Card detect interrupt on */
1892 pccbb_setb(sc, CBB_SOCKET_MASK, CBB_SOCKET_MASK_CD);
1894 /* reset interrupt */
1895 tmp = pccbb_get(sc, CBB_SOCKET_EVENT);
1896 pccbb_set(sc, CBB_SOCKET_EVENT, tmp);
1899 * Some BIOSes will not save the BARs for the pci chips, so we
1900 * must do it ourselves. If the BAR is reset to 0 for an I/O
1901 * device, it will read back as 0x1, so no explicit test for
1902 * memory devices are needed.
1904 * Note: The PCI bus code should do this automatically for us on
1905 * suspend/resume, but until it does, we have to cope.
1907 if (pci_read_config(self, CBBR_SOCKBASE, 4) == 0)
1908 pci_write_config(self, CBBR_SOCKBASE,
1909 rman_get_start(sc->base_res), 4);
1911 error = bus_generic_resume(self);
1916 static device_method_t pccbb_methods[] = {
1917 /* Device interface */
1918 DEVMETHOD(device_probe, pccbb_probe),
1919 DEVMETHOD(device_attach, pccbb_attach),
1920 DEVMETHOD(device_detach, pccbb_detach),
1921 DEVMETHOD(device_shutdown, pccbb_shutdown),
1922 DEVMETHOD(device_suspend, pccbb_suspend),
1923 DEVMETHOD(device_resume, pccbb_resume),
1926 DEVMETHOD(bus_print_child, bus_generic_print_child),
1927 DEVMETHOD(bus_read_ivar, pccbb_read_ivar),
1928 DEVMETHOD(bus_write_ivar, pccbb_write_ivar),
1929 DEVMETHOD(bus_alloc_resource, pccbb_alloc_resource),
1930 DEVMETHOD(bus_release_resource, pccbb_release_resource),
1931 DEVMETHOD(bus_activate_resource, pccbb_activate_resource),
1932 DEVMETHOD(bus_deactivate_resource, pccbb_deactivate_resource),
1933 DEVMETHOD(bus_driver_added, pccbb_driver_added),
1934 DEVMETHOD(bus_child_detached, pccbb_child_detached),
1935 DEVMETHOD(bus_setup_intr, pccbb_setup_intr),
1936 DEVMETHOD(bus_teardown_intr, pccbb_teardown_intr),
1938 /* 16-bit card interface */
1939 DEVMETHOD(card_set_res_flags, pccbb_pcic_set_res_flags),
1940 DEVMETHOD(card_set_memory_offset, pccbb_pcic_set_memory_offset),
1941 DEVMETHOD(card_reprobe_card, pccbb_card_reprobe),
1943 /* power interface */
1944 DEVMETHOD(power_enable_socket, pccbb_power_enable_socket),
1945 DEVMETHOD(power_disable_socket, pccbb_power_disable_socket),
1947 /* pcib compatibility interface */
1948 DEVMETHOD(pcib_maxslots, pccbb_maxslots),
1949 DEVMETHOD(pcib_read_config, pccbb_read_config),
1950 DEVMETHOD(pcib_write_config, pccbb_write_config),
1954 static driver_t pccbb_driver = {
1957 sizeof(struct pccbb_softc)
1960 static devclass_t pccbb_devclass;
1962 DRIVER_MODULE(pccbb, pci, pccbb_driver, pccbb_devclass, 0, 0);
1963 MODULE_VERSION(pccbb, 1);
1964 MODULE_DEPEND(pccbb, exca, 1, 1, 1);
1965 MODULE_DEPEND(pccbb, pccard, 1, 1, 1);
1966 MODULE_DEPEND(pccbb, cardbus, 1, 1, 1);