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 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29 /*- 30 * Copyright (c) 1998, 1999 and 2000 31 * HAYAKAWA Koichi. All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. All advertising materials mentioning features or use of this software 42 * must display the following acknowledgement: 43 * This product includes software developed by HAYAKAWA Koichi. 44 * 4. The name of the author may not be used to endorse or promote products 45 * derived from this software without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 57 */ 58 59 /* 60 * Driver for PCI to CardBus Bridge chips 61 * 62 * References: 63 * TI Datasheets: 64 * http://www-s.ti.com/cgi-bin/sc/generic2.cgi?family=PCI+CARDBUS+CONTROLLERS 65 * 66 * Written by Jonathan Chen <jon@freebsd.org> 67 * The author would like to acknowledge: 68 * * HAYAKAWA Koichi: Author of the NetBSD code for the same thing 69 * * Warner Losh: Newbus/newcard guru and author of the pccard side of things 70 * * YAMAMOTO Shigeru: Author of another FreeBSD cardbus driver 71 * * David Cross: Author of the initial ugly hack for a specific cardbus card 72 */ 73 74 #include <sys/cdefs.h> 75 __FBSDID("$FreeBSD$"); 76 77 #include <sys/param.h> 78 #include <sys/systm.h> 79 #include <sys/proc.h> 80 #include <sys/condvar.h> 81 #include <sys/errno.h> 82 #include <sys/kernel.h> 83 #include <sys/lock.h> 84 #include <sys/malloc.h> 85 #include <sys/mutex.h> 86 #include <sys/sysctl.h> 87 #include <sys/kthread.h> 88 #include <sys/bus.h> 89 #include <machine/bus.h> 90 #include <sys/rman.h> 91 #include <machine/resource.h> 92 #include <sys/module.h> 93 94 #include <dev/pci/pcireg.h> 95 #include <dev/pci/pcivar.h> 96 #include <dev/pci/pcib_private.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 static void cbb_chipinit(struct cbb_softc *sc); 121 static int cbb_pci_filt(void *arg); 122 123 static struct yenta_chipinfo { 124 uint32_t yc_id; 125 const char *yc_name; 126 int yc_chiptype; 127 } yc_chipsets[] = { 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}, 132 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}, /*SIC!*/ 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}, 154 {PCIC_ID_TI6411, "TI6411 PCI-CardBus Bridge", CB_TI12XX}, 155 {PCIC_ID_TI6420, "TI6420 PCI-CardBus Bridge", CB_TI12XX}, 156 {PCIC_ID_TI6420SC, "TI6420 PCI-CardBus Bridge", CB_TI12XX}, 157 {PCIC_ID_TI7410, "TI7410 PCI-CardBus Bridge", CB_TI12XX}, 158 {PCIC_ID_TI7510, "TI7510 PCI-CardBus Bridge", CB_TI12XX}, 159 {PCIC_ID_TI7610, "TI7610 PCI-CardBus Bridge", CB_TI12XX}, 160 {PCIC_ID_TI7610M, "TI7610 PCI-CardBus Bridge", CB_TI12XX}, 161 {PCIC_ID_TI7610SD, "TI7610 PCI-CardBus Bridge", CB_TI12XX}, 162 {PCIC_ID_TI7610MS, "TI7610 PCI-CardBus Bridge", CB_TI12XX}, 163 164 /* ENE */ 165 {PCIC_ID_ENE_CB710, "ENE CB710 PCI-CardBus Bridge", CB_TI12XX}, 166 {PCIC_ID_ENE_CB720, "ENE CB720 PCI-CardBus Bridge", CB_TI12XX}, 167 {PCIC_ID_ENE_CB1211, "ENE CB1211 PCI-CardBus Bridge", CB_TI12XX}, 168 {PCIC_ID_ENE_CB1225, "ENE CB1225 PCI-CardBus Bridge", CB_TI12XX}, 169 {PCIC_ID_ENE_CB1410, "ENE CB1410 PCI-CardBus Bridge", CB_TI12XX}, 170 {PCIC_ID_ENE_CB1420, "ENE CB1420 PCI-CardBus Bridge", CB_TI12XX}, 171 172 /* Ricoh chips */ 173 {PCIC_ID_RICOH_RL5C465, "RF5C465 PCI-CardBus Bridge", CB_RF5C46X}, 174 {PCIC_ID_RICOH_RL5C466, "RF5C466 PCI-CardBus Bridge", CB_RF5C46X}, 175 {PCIC_ID_RICOH_RL5C475, "RF5C475 PCI-CardBus Bridge", CB_RF5C47X}, 176 {PCIC_ID_RICOH_RL5C476, "RF5C476 PCI-CardBus Bridge", CB_RF5C47X}, 177 {PCIC_ID_RICOH_RL5C477, "RF5C477 PCI-CardBus Bridge", CB_RF5C47X}, 178 {PCIC_ID_RICOH_RL5C478, "RF5C478 PCI-CardBus Bridge", CB_RF5C47X}, 179 180 /* Toshiba products */ 181 {PCIC_ID_TOPIC95, "ToPIC95 PCI-CardBus Bridge", CB_TOPIC95}, 182 {PCIC_ID_TOPIC95B, "ToPIC95B PCI-CardBus Bridge", CB_TOPIC95}, 183 {PCIC_ID_TOPIC97, "ToPIC97 PCI-CardBus Bridge", CB_TOPIC97}, 184 {PCIC_ID_TOPIC100, "ToPIC100 PCI-CardBus Bridge", CB_TOPIC97}, 185 186 /* Cirrus Logic */ 187 {PCIC_ID_CLPD6832, "CLPD6832 PCI-CardBus Bridge", CB_CIRRUS}, 188 {PCIC_ID_CLPD6833, "CLPD6833 PCI-CardBus Bridge", CB_CIRRUS}, 189 {PCIC_ID_CLPD6834, "CLPD6834 PCI-CardBus Bridge", CB_CIRRUS}, 190 191 /* 02Micro */ 192 {PCIC_ID_OZ6832, "O2Micro OZ6832/6833 PCI-CardBus Bridge", CB_O2MICRO}, 193 {PCIC_ID_OZ6860, "O2Micro OZ6836/6860 PCI-CardBus Bridge", CB_O2MICRO}, 194 {PCIC_ID_OZ6872, "O2Micro OZ6812/6872 PCI-CardBus Bridge", CB_O2MICRO}, 195 {PCIC_ID_OZ6912, "O2Micro OZ6912/6972 PCI-CardBus Bridge", CB_O2MICRO}, 196 {PCIC_ID_OZ6922, "O2Micro OZ6922 PCI-CardBus Bridge", CB_O2MICRO}, 197 {PCIC_ID_OZ6933, "O2Micro OZ6933 PCI-CardBus Bridge", CB_O2MICRO}, 198 {PCIC_ID_OZ711E1, "O2Micro OZ711E1 PCI-CardBus Bridge", CB_O2MICRO}, 199 {PCIC_ID_OZ711EC1, "O2Micro OZ711EC1/M1 PCI-CardBus Bridge", CB_O2MICRO}, 200 {PCIC_ID_OZ711E2, "O2Micro OZ711E2 PCI-CardBus Bridge", CB_O2MICRO}, 201 {PCIC_ID_OZ711M1, "O2Micro OZ711M1 PCI-CardBus Bridge", CB_O2MICRO}, 202 {PCIC_ID_OZ711M2, "O2Micro OZ711M2 PCI-CardBus Bridge", CB_O2MICRO}, 203 {PCIC_ID_OZ711M3, "O2Micro OZ711M3 PCI-CardBus Bridge", CB_O2MICRO}, 204 205 /* SMC */ 206 {PCIC_ID_SMC_34C90, "SMC 34C90 PCI-CardBus Bridge", CB_CIRRUS}, 207 208 /* sentinel */ 209 {0 /* null id */, "unknown", CB_UNKNOWN}, 210 }; 211 212 /************************************************************************/ 213 /* Probe/Attach */ 214 /************************************************************************/ 215 216 static int 217 cbb_chipset(uint32_t pci_id, const char **namep) 218 { 219 struct yenta_chipinfo *ycp; 220 221 for (ycp = yc_chipsets; ycp->yc_id != 0 && pci_id != ycp->yc_id; ++ycp) 222 continue; 223 if (namep != NULL) 224 *namep = ycp->yc_name; 225 return (ycp->yc_chiptype); 226 } 227 228 static int 229 cbb_pci_probe(device_t brdev) 230 { 231 const char *name; 232 uint32_t progif; 233 uint32_t baseclass; 234 uint32_t subclass; 235 236 /* 237 * Do we know that we support the chipset? If so, then we 238 * accept the device. 239 */ 240 if (cbb_chipset(pci_get_devid(brdev), &name) != CB_UNKNOWN) { 241 device_set_desc(brdev, name); 242 return (BUS_PROBE_DEFAULT); 243 } 244 245 /* 246 * We do support generic CardBus bridges. All that we've seen 247 * to date have progif 0 (the Yenta spec, and successors mandate 248 * this). 249 */ 250 baseclass = pci_get_class(brdev); 251 subclass = pci_get_subclass(brdev); 252 progif = pci_get_progif(brdev); 253 if (baseclass == PCIC_BRIDGE && 254 subclass == PCIS_BRIDGE_CARDBUS && progif == 0) { 255 device_set_desc(brdev, "PCI-CardBus Bridge"); 256 return (BUS_PROBE_GENERIC); 257 } 258 return (ENXIO); 259 } 260 261 /* 262 * Print out the config space 263 */ 264 static void 265 cbb_print_config(device_t dev) 266 { 267 int i; 268 269 device_printf(dev, "PCI Configuration space:"); 270 for (i = 0; i < 256; i += 4) { 271 if (i % 16 == 0) 272 printf("\n 0x%02x: ", i); 273 printf("0x%08x ", pci_read_config(dev, i, 4)); 274 } 275 printf("\n"); 276 } 277 278 static int 279 cbb_pci_attach(device_t brdev) 280 { 281 #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 282 static int curr_bus_number = 2; /* XXX EVILE BAD (see below) */ 283 uint32_t pribus; 284 #endif 285 struct cbb_softc *sc = (struct cbb_softc *)device_get_softc(brdev); 286 struct sysctl_ctx_list *sctx; 287 struct sysctl_oid *soid; 288 int rid; 289 device_t parent; 290 291 parent = device_get_parent(brdev); 292 mtx_init(&sc->mtx, device_get_nameunit(brdev), "cbb", MTX_DEF); 293 sc->chipset = cbb_chipset(pci_get_devid(brdev), NULL); 294 sc->dev = brdev; 295 sc->cbdev = NULL; 296 sc->exca[0].pccarddev = NULL; 297 sc->domain = pci_get_domain(brdev); 298 sc->pribus = pcib_get_bus(parent); 299 #if defined(NEW_PCIB) && defined(PCI_RES_BUS) 300 pci_write_config(brdev, PCIR_PRIBUS_2, sc->pribus, 1); 301 pcib_setup_secbus(brdev, &sc->bus, 1); 302 #else 303 sc->bus.sec = pci_read_config(brdev, PCIR_SECBUS_2, 1); 304 sc->bus.sub = pci_read_config(brdev, PCIR_SUBBUS_2, 1); 305 #endif 306 SLIST_INIT(&sc->rl); 307 308 rid = CBBR_SOCKBASE; 309 sc->base_res = bus_alloc_resource_any(brdev, SYS_RES_MEMORY, &rid, 310 RF_ACTIVE); 311 if (!sc->base_res) { 312 device_printf(brdev, "Could not map register memory\n"); 313 mtx_destroy(&sc->mtx); 314 return (ENOMEM); 315 } else { 316 DEVPRINTF((brdev, "Found memory at %jx\n", 317 rman_get_start(sc->base_res))); 318 } 319 320 sc->bst = rman_get_bustag(sc->base_res); 321 sc->bsh = rman_get_bushandle(sc->base_res); 322 exca_init(&sc->exca[0], brdev, sc->bst, sc->bsh, CBB_EXCA_OFFSET); 323 sc->exca[0].flags |= EXCA_HAS_MEMREG_WIN; 324 sc->exca[0].chipset = EXCA_CARDBUS; 325 sc->chipinit = cbb_chipinit; 326 sc->chipinit(sc); 327 328 /*Sysctls*/ 329 sctx = device_get_sysctl_ctx(brdev); 330 soid = device_get_sysctl_tree(brdev); 331 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 332 CTLFLAG_RD, &sc->domain, 0, "Domain number"); 333 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 334 CTLFLAG_RD, &sc->pribus, 0, "Primary bus number"); 335 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 336 CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number"); 337 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 338 CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number"); 339 #if 0 340 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "memory", 341 CTLFLAG_RD, &sc->subbus, 0, "Memory window open"); 342 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "premem", 343 CTLFLAG_RD, &sc->subbus, 0, "Prefetch memory window open"); 344 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "io1", 345 CTLFLAG_RD, &sc->subbus, 0, "io range 1 open"); 346 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "io2", 347 CTLFLAG_RD, &sc->subbus, 0, "io range 2 open"); 348 #endif 349 350 #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 351 /* 352 * This is a gross hack. We should be scanning the entire pci 353 * tree, assigning bus numbers in a way such that we (1) can 354 * reserve 1 extra bus just in case and (2) all sub busses 355 * are in an appropriate range. 356 */ 357 DEVPRINTF((brdev, "Secondary bus is %d\n", sc->bus.sec)); 358 pribus = pci_read_config(brdev, PCIR_PRIBUS_2, 1); 359 if (sc->bus.sec == 0 || sc->pribus != pribus) { 360 if (curr_bus_number <= sc->pribus) 361 curr_bus_number = sc->pribus + 1; 362 if (pribus != sc->pribus) { 363 DEVPRINTF((brdev, "Setting primary bus to %d\n", 364 sc->pribus)); 365 pci_write_config(brdev, PCIR_PRIBUS_2, sc->pribus, 1); 366 } 367 sc->bus.sec = curr_bus_number++; 368 sc->bus.sub = curr_bus_number++; 369 DEVPRINTF((brdev, "Secondary bus set to %d subbus %d\n", 370 sc->bus.sec, sc->bus.sub)); 371 pci_write_config(brdev, PCIR_SECBUS_2, sc->bus.sec, 1); 372 pci_write_config(brdev, PCIR_SUBBUS_2, sc->bus.sub, 1); 373 } 374 #endif 375 376 /* attach children */ 377 sc->cbdev = device_add_child(brdev, "cardbus", -1); 378 if (sc->cbdev == NULL) 379 DEVPRINTF((brdev, "WARNING: cannot add cardbus bus.\n")); 380 else if (device_probe_and_attach(sc->cbdev) != 0) 381 DEVPRINTF((brdev, "WARNING: cannot attach cardbus bus!\n")); 382 383 sc->exca[0].pccarddev = device_add_child(brdev, "pccard", -1); 384 if (sc->exca[0].pccarddev == NULL) 385 DEVPRINTF((brdev, "WARNING: cannot add pccard bus.\n")); 386 else if (device_probe_and_attach(sc->exca[0].pccarddev) != 0) 387 DEVPRINTF((brdev, "WARNING: cannot attach pccard bus.\n")); 388 389 /* Map and establish the interrupt. */ 390 rid = 0; 391 sc->irq_res = bus_alloc_resource_any(brdev, SYS_RES_IRQ, &rid, 392 RF_SHAREABLE | RF_ACTIVE); 393 if (sc->irq_res == NULL) { 394 device_printf(brdev, "Unable to map IRQ...\n"); 395 goto err; 396 } 397 398 if (bus_setup_intr(brdev, sc->irq_res, INTR_TYPE_AV | INTR_MPSAFE, 399 cbb_pci_filt, NULL, sc, &sc->intrhand)) { 400 device_printf(brdev, "couldn't establish interrupt\n"); 401 goto err; 402 } 403 404 /* reset 16-bit pcmcia bus */ 405 exca_clrb(&sc->exca[0], EXCA_INTR, EXCA_INTR_RESET); 406 407 /* turn off power */ 408 cbb_power(brdev, CARD_OFF); 409 410 /* CSC Interrupt: Card detect interrupt on */ 411 cbb_setb(sc, CBB_SOCKET_MASK, CBB_SOCKET_MASK_CD); 412 413 /* reset interrupt */ 414 cbb_set(sc, CBB_SOCKET_EVENT, cbb_get(sc, CBB_SOCKET_EVENT)); 415 416 if (bootverbose) 417 cbb_print_config(brdev); 418 419 /* Start the thread */ 420 if (kproc_create(cbb_event_thread, sc, &sc->event_thread, 0, 0, 421 "%s event thread", device_get_nameunit(brdev))) { 422 device_printf(brdev, "unable to create event thread.\n"); 423 panic("cbb_create_event_thread"); 424 } 425 sc->sc_root_token = root_mount_hold(device_get_nameunit(sc->dev)); 426 return (0); 427 err: 428 if (sc->irq_res) 429 bus_release_resource(brdev, SYS_RES_IRQ, 0, sc->irq_res); 430 if (sc->base_res) { 431 bus_release_resource(brdev, SYS_RES_MEMORY, CBBR_SOCKBASE, 432 sc->base_res); 433 } 434 mtx_destroy(&sc->mtx); 435 return (ENOMEM); 436 } 437 438 static int 439 cbb_pci_detach(device_t brdev) 440 { 441 #if defined(NEW_PCIB) && defined(PCI_RES_BUS) 442 struct cbb_softc *sc = device_get_softc(brdev); 443 #endif 444 int error; 445 446 error = cbb_detach(brdev); 447 #if defined(NEW_PCIB) && defined(PCI_RES_BUS) 448 if (error == 0) 449 pcib_free_secbus(brdev, &sc->bus); 450 #endif 451 return (error); 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_2, 1) < 0x20) 461 pci_write_config(sc->dev, PCIR_SECLAT_2, 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 DMA, memory access for this card and I/O access for children */ 468 pci_enable_busmaster(sc->dev); 469 pci_enable_io(sc->dev, SYS_RES_IOPORT); 470 pci_enable_io(sc->dev, SYS_RES_MEMORY); 471 472 /* disable Legacy IO */ 473 switch (sc->chipset) { 474 case CB_RF5C46X: 475 PCI_MASK_CONFIG(sc->dev, CBBR_BRIDGECTRL, 476 & ~(CBBM_BRIDGECTRL_RL_3E0_EN | 477 CBBM_BRIDGECTRL_RL_3E2_EN), 2); 478 break; 479 default: 480 pci_write_config(sc->dev, CBBR_LEGACY, 0x0, 4); 481 break; 482 } 483 484 /* Use PCI interrupt for interrupt routing */ 485 PCI_MASK2_CONFIG(sc->dev, CBBR_BRIDGECTRL, 486 & ~(CBBM_BRIDGECTRL_MASTER_ABORT | 487 CBBM_BRIDGECTRL_INTR_IREQ_ISA_EN), 488 | CBBM_BRIDGECTRL_WRITE_POST_EN, 489 2); 490 491 /* 492 * XXX this should be a function table, ala OLDCARD. This means 493 * that we could more easily support ISA interrupts for pccard 494 * cards if we had to. 495 */ 496 switch (sc->chipset) { 497 case CB_TI113X: 498 /* 499 * The TI 1031, TI 1130 and TI 1131 all require another bit 500 * be set to enable PCI routing of interrupts, and then 501 * a bit for each of the CSC and Function interrupts we 502 * want routed. 503 */ 504 PCI_MASK_CONFIG(sc->dev, CBBR_CBCTRL, 505 | CBBM_CBCTRL_113X_PCI_INTR | 506 CBBM_CBCTRL_113X_PCI_CSC | CBBM_CBCTRL_113X_PCI_IRQ_EN, 507 1); 508 PCI_MASK_CONFIG(sc->dev, CBBR_DEVCTRL, 509 & ~(CBBM_DEVCTRL_INT_SERIAL | 510 CBBM_DEVCTRL_INT_PCI), 1); 511 break; 512 case CB_TI12XX: 513 /* 514 * Some TI 12xx (and [14][45]xx) based pci cards 515 * sometimes have issues with the MFUNC register not 516 * being initialized due to a bad EEPROM on board. 517 * Laptops that this matters on have this register 518 * properly initialized. 519 * 520 * The TI125X parts have a different register. 521 * 522 * Note: Only the lower two nibbles matter. When set 523 * to 0, the MFUNC{0,1} pins are GPIO, which isn't 524 * going to work out too well because we specifically 525 * program these parts to parallel interrupt signalling 526 * elsewhere. We preserve the upper bits of this 527 * register since changing them have subtle side effects 528 * for different variants of the card and are 529 * extremely difficult to exaustively test. 530 * 531 * Also, the TI 1510/1520 changed the default for the MFUNC 532 * register from 0x0 to 0x1000 to enable IRQSER by default. 533 * We want to be careful to avoid overriding that, and the 534 * below test will do that. Should this check prove to be 535 * too permissive, we should just check against 0 and 0x1000 536 * and not touch it otherwise. 537 */ 538 mux = pci_read_config(sc->dev, CBBR_MFUNC, 4); 539 sysctrl = pci_read_config(sc->dev, CBBR_SYSCTRL, 4); 540 if ((mux & (CBBM_MFUNC_PIN0 | CBBM_MFUNC_PIN1)) == 0) { 541 mux = (mux & ~CBBM_MFUNC_PIN0) | 542 CBBM_MFUNC_PIN0_INTA; 543 if ((sysctrl & CBBM_SYSCTRL_INTRTIE) == 0) 544 mux = (mux & ~CBBM_MFUNC_PIN1) | 545 CBBM_MFUNC_PIN1_INTB; 546 pci_write_config(sc->dev, CBBR_MFUNC, mux, 4); 547 } 548 /*FALLTHROUGH*/ 549 case CB_TI125X: 550 /* 551 * Disable zoom video. Some machines initialize this 552 * improperly and exerpience has shown that this helps 553 * prevent strange behavior. We don't support zoom 554 * video anyway, so no harm can come from this. 555 */ 556 pci_write_config(sc->dev, CBBR_MMCTRL, 0, 4); 557 break; 558 case CB_O2MICRO: 559 /* 560 * Issue #1: INT# generated at the same time as 561 * selected ISA IRQ. When IREQ# or STSCHG# is active, 562 * in addition to the ISA IRQ being generated, INT# 563 * will also be generated at the same time. 564 * 565 * Some of the older controllers have an issue in 566 * which the slot's PCI INT# will be asserted whenever 567 * IREQ# or STSCGH# is asserted even if ExCA registers 568 * 03h or 05h have an ISA IRQ selected. 569 * 570 * The fix for this issue, which will work for any 571 * controller (old or new), is to set ExCA registers 572 * 3Ah (slot 0) & 7Ah (slot 1) bits 7:4 = 1010b. 573 * These bits are undocumented. By setting this 574 * register (of each slot) to '1010xxxxb' a routing of 575 * IREQ# to INTC# and STSCHG# to INTC# is selected. 576 * Since INTC# isn't connected there will be no 577 * unexpected PCI INT when IREQ# or STSCHG# is active. 578 * However, INTA# (slot 0) or INTB# (slot 1) will 579 * still be correctly generated if NO ISA IRQ is 580 * selected (ExCA regs 03h or 05h are cleared). 581 */ 582 reg = exca_getb(&sc->exca[0], EXCA_O2MICRO_CTRL_C); 583 reg = (reg & 0x0f) | 584 EXCA_O2CC_IREQ_INTC | EXCA_O2CC_STSCHG_INTC; 585 exca_putb(&sc->exca[0], EXCA_O2MICRO_CTRL_C, reg); 586 break; 587 case CB_TOPIC97: 588 /* 589 * Disable Zoom Video, ToPIC 97, 100. 590 */ 591 pci_write_config(sc->dev, TOPIC97_ZV_CONTROL, 0, 1); 592 /* 593 * ToPIC 97, 100 594 * At offset 0xa1: INTERRUPT CONTROL register 595 * 0x1: Turn on INT interrupts. 596 */ 597 PCI_MASK_CONFIG(sc->dev, TOPIC_INTCTRL, 598 | TOPIC97_INTCTRL_INTIRQSEL, 1); 599 /* 600 * ToPIC97, 100 601 * Need to assert support for low voltage cards 602 */ 603 exca_setb(&sc->exca[0], EXCA_TOPIC97_CTRL, 604 EXCA_TOPIC97_CTRL_LV_MASK); 605 goto topic_common; 606 case CB_TOPIC95: 607 /* 608 * SOCKETCTRL appears to be TOPIC 95/B specific 609 */ 610 PCI_MASK_CONFIG(sc->dev, TOPIC95_SOCKETCTRL, 611 | TOPIC95_SOCKETCTRL_SCR_IRQSEL, 4); 612 613 topic_common:; 614 /* 615 * At offset 0xa0: SLOT CONTROL 616 * 0x80 Enable CardBus Functionality 617 * 0x40 Enable CardBus and PC Card registers 618 * 0x20 Lock ID in exca regs 619 * 0x10 Write protect ID in config regs 620 * Clear the rest of the bits, which defaults the slot 621 * in legacy mode to 0x3e0 and offset 0. (legacy 622 * mode is determined elsewhere) 623 */ 624 pci_write_config(sc->dev, TOPIC_SLOTCTRL, 625 TOPIC_SLOTCTRL_SLOTON | 626 TOPIC_SLOTCTRL_SLOTEN | 627 TOPIC_SLOTCTRL_ID_LOCK | 628 TOPIC_SLOTCTRL_ID_WP, 1); 629 630 /* 631 * At offset 0xa3 Card Detect Control Register 632 * 0x80 CARDBUS enbale 633 * 0x01 Cleared for hardware change detect 634 */ 635 PCI_MASK2_CONFIG(sc->dev, TOPIC_CDC, 636 | TOPIC_CDC_CARDBUS, & ~TOPIC_CDC_SWDETECT, 4); 637 break; 638 } 639 640 /* 641 * Need to tell ExCA registers to CSC interrupts route via PCI 642 * interrupts. There are two ways to do this. One is to set 643 * INTR_ENABLE and the other is to set CSC to 0. Since both 644 * methods are mutually compatible, we do both. 645 */ 646 exca_putb(&sc->exca[0], EXCA_INTR, EXCA_INTR_ENABLE); 647 exca_putb(&sc->exca[0], EXCA_CSC_INTR, 0); 648 649 cbb_disable_func_intr(sc); 650 651 /* close all memory and io windows */ 652 pci_write_config(sc->dev, CBBR_MEMBASE0, 0xffffffff, 4); 653 pci_write_config(sc->dev, CBBR_MEMLIMIT0, 0, 4); 654 pci_write_config(sc->dev, CBBR_MEMBASE1, 0xffffffff, 4); 655 pci_write_config(sc->dev, CBBR_MEMLIMIT1, 0, 4); 656 pci_write_config(sc->dev, CBBR_IOBASE0, 0xffffffff, 4); 657 pci_write_config(sc->dev, CBBR_IOLIMIT0, 0, 4); 658 pci_write_config(sc->dev, CBBR_IOBASE1, 0xffffffff, 4); 659 pci_write_config(sc->dev, CBBR_IOLIMIT1, 0, 4); 660 } 661 662 static int 663 cbb_route_interrupt(device_t pcib, device_t dev, int pin) 664 { 665 struct cbb_softc *sc = (struct cbb_softc *)device_get_softc(pcib); 666 667 return (rman_get_start(sc->irq_res)); 668 } 669 670 static int 671 cbb_pci_shutdown(device_t brdev) 672 { 673 struct cbb_softc *sc = (struct cbb_softc *)device_get_softc(brdev); 674 675 /* 676 * We're about to pull the rug out from the card, so mark it as 677 * gone to prevent harm. 678 */ 679 sc->cardok = 0; 680 681 /* 682 * Place the cards in reset, turn off the interrupts and power 683 * down the socket. 684 */ 685 PCI_MASK_CONFIG(brdev, CBBR_BRIDGECTRL, |CBBM_BRIDGECTRL_RESET, 2); 686 exca_clrb(&sc->exca[0], EXCA_INTR, EXCA_INTR_RESET); 687 cbb_set(sc, CBB_SOCKET_MASK, 0); 688 cbb_set(sc, CBB_SOCKET_EVENT, 0xffffffff); 689 cbb_power(brdev, CARD_OFF); 690 691 /* 692 * For paranoia, turn off all address decoding. Really not needed, 693 * it seems, but it can't hurt 694 */ 695 exca_putb(&sc->exca[0], EXCA_ADDRWIN_ENABLE, 0); 696 pci_write_config(brdev, CBBR_MEMBASE0, 0, 4); 697 pci_write_config(brdev, CBBR_MEMLIMIT0, 0, 4); 698 pci_write_config(brdev, CBBR_MEMBASE1, 0, 4); 699 pci_write_config(brdev, CBBR_MEMLIMIT1, 0, 4); 700 pci_write_config(brdev, CBBR_IOBASE0, 0, 4); 701 pci_write_config(brdev, CBBR_IOLIMIT0, 0, 4); 702 pci_write_config(brdev, CBBR_IOBASE1, 0, 4); 703 pci_write_config(brdev, CBBR_IOLIMIT1, 0, 4); 704 return (0); 705 } 706 707 static int 708 cbb_pci_filt(void *arg) 709 { 710 struct cbb_softc *sc = arg; 711 uint32_t sockevent; 712 uint8_t csc; 713 int retval = FILTER_STRAY; 714 715 /* 716 * Some chips also require us to read the old ExCA registe for card 717 * status change when we route CSC vis PCI. This isn't supposed to be 718 * required, but it clears the interrupt state on some chipsets. 719 * Maybe there's a setting that would obviate its need. Maybe we 720 * should test the status bits and deal with them, but so far we've 721 * not found any machines that don't also give us the socket status 722 * indication above. 723 * 724 * This call used to be unconditional. However, further research 725 * suggests that we hit this condition when the card READY interrupt 726 * fired. So now we only read it for 16-bit cards, and we only claim 727 * the interrupt if READY is set. If this still causes problems, then 728 * the next step would be to read this if we have a 16-bit card *OR* 729 * we have no card. We treat the READY signal as if it were the power 730 * completion signal. Some bridges may double signal things here, bit 731 * signalling twice should be OK since we only sleep on the powerintr 732 * in one place and a double wakeup would be benign there. 733 */ 734 if (sc->flags & CBB_16BIT_CARD) { 735 csc = exca_getb(&sc->exca[0], EXCA_CSC); 736 if (csc & EXCA_CSC_READY) { 737 atomic_add_int(&sc->powerintr, 1); 738 wakeup((void *)&sc->powerintr); 739 retval = FILTER_HANDLED; 740 } 741 } 742 743 /* 744 * Read the socket event. Sometimes, the theory goes, the PCI bus is 745 * so loaded that it cannot satisfy the read request, so we get 746 * garbage back from the following read. We have to filter out the 747 * garbage so that we don't spontaneously reset the card under high 748 * load. PCI isn't supposed to act like this. No doubt this is a bug 749 * in the PCI bridge chipset (or cbb brige) that's being used in 750 * certain amd64 laptops today. Work around the issue by assuming 751 * that any bits we don't know about being set means that we got 752 * garbage. 753 */ 754 sockevent = cbb_get(sc, CBB_SOCKET_EVENT); 755 if (sockevent != 0 && (sockevent & ~CBB_SOCKET_EVENT_VALID_MASK) == 0) { 756 /* 757 * If anything has happened to the socket, we assume that the 758 * card is no longer OK, and we shouldn't call its ISR. We 759 * set cardok as soon as we've attached the card. This helps 760 * in a noisy eject, which happens all too often when users 761 * are ejecting their PC Cards. 762 * 763 * We use this method in preference to checking to see if the 764 * card is still there because the check suffers from a race 765 * condition in the bouncing case. 766 */ 767 #define DELTA (CBB_SOCKET_MASK_CD) 768 if (sockevent & DELTA) { 769 cbb_clrb(sc, CBB_SOCKET_MASK, DELTA); 770 cbb_set(sc, CBB_SOCKET_EVENT, DELTA); 771 sc->cardok = 0; 772 cbb_disable_func_intr(sc); 773 wakeup(&sc->intrhand); 774 } 775 #undef DELTA 776 777 /* 778 * Wakeup anybody waiting for a power interrupt. We have to 779 * use atomic_add_int for wakups on other cores. 780 */ 781 if (sockevent & CBB_SOCKET_EVENT_POWER) { 782 cbb_clrb(sc, CBB_SOCKET_MASK, CBB_SOCKET_EVENT_POWER); 783 cbb_set(sc, CBB_SOCKET_EVENT, CBB_SOCKET_EVENT_POWER); 784 atomic_add_int(&sc->powerintr, 1); 785 wakeup((void *)&sc->powerintr); 786 } 787 788 /* 789 * Status change interrupts aren't presently used in the 790 * rest of the driver. For now, just ACK them. 791 */ 792 if (sockevent & CBB_SOCKET_EVENT_CSTS) 793 cbb_set(sc, CBB_SOCKET_EVENT, CBB_SOCKET_EVENT_CSTS); 794 retval = FILTER_HANDLED; 795 } 796 return retval; 797 } 798 799 #if defined(NEW_PCIB) && defined(PCI_RES_BUS) 800 static struct resource * 801 cbb_pci_alloc_resource(device_t bus, device_t child, int type, int *rid, 802 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 803 { 804 struct cbb_softc *sc; 805 806 sc = device_get_softc(bus); 807 if (type == PCI_RES_BUS) 808 return (pcib_alloc_subbus(&sc->bus, child, rid, start, end, 809 count, flags)); 810 return (cbb_alloc_resource(bus, child, type, rid, start, end, count, 811 flags)); 812 } 813 814 static int 815 cbb_pci_adjust_resource(device_t bus, device_t child, int type, 816 struct resource *r, rman_res_t start, rman_res_t end) 817 { 818 struct cbb_softc *sc; 819 820 sc = device_get_softc(bus); 821 if (type == PCI_RES_BUS) { 822 if (!rman_is_region_manager(r, &sc->bus.rman)) 823 return (EINVAL); 824 return (rman_adjust_resource(r, start, end)); 825 } 826 return (bus_generic_adjust_resource(bus, child, type, r, start, end)); 827 } 828 829 static int 830 cbb_pci_release_resource(device_t bus, device_t child, int type, int rid, 831 struct resource *r) 832 { 833 struct cbb_softc *sc; 834 int error; 835 836 sc = device_get_softc(bus); 837 if (type == PCI_RES_BUS) { 838 if (!rman_is_region_manager(r, &sc->bus.rman)) 839 return (EINVAL); 840 if (rman_get_flags(r) & RF_ACTIVE) { 841 error = bus_deactivate_resource(child, type, rid, r); 842 if (error) 843 return (error); 844 } 845 return (rman_release_resource(r)); 846 } 847 return (cbb_release_resource(bus, child, type, rid, r)); 848 } 849 #endif 850 851 /************************************************************************/ 852 /* PCI compat methods */ 853 /************************************************************************/ 854 855 static int 856 cbb_maxslots(device_t brdev) 857 { 858 return (0); 859 } 860 861 static uint32_t 862 cbb_read_config(device_t brdev, u_int b, u_int s, u_int f, u_int reg, int width) 863 { 864 /* 865 * Pass through to the next ppb up the chain (i.e. our grandparent). 866 */ 867 return (PCIB_READ_CONFIG(device_get_parent(device_get_parent(brdev)), 868 b, s, f, reg, width)); 869 } 870 871 static void 872 cbb_write_config(device_t brdev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, 873 int width) 874 { 875 /* 876 * Pass through to the next ppb up the chain (i.e. our grandparent). 877 */ 878 PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(brdev)), 879 b, s, f, reg, val, width); 880 } 881 882 static int 883 cbb_pci_suspend(device_t brdev) 884 { 885 int error = 0; 886 struct cbb_softc *sc = device_get_softc(brdev); 887 888 error = bus_generic_suspend(brdev); 889 if (error != 0) 890 return (error); 891 cbb_set(sc, CBB_SOCKET_MASK, 0); /* Quiet hardware */ 892 sc->cardok = 0; /* Card is bogus now */ 893 return (0); 894 } 895 896 static int 897 cbb_pci_resume(device_t brdev) 898 { 899 int error = 0; 900 struct cbb_softc *sc = (struct cbb_softc *)device_get_softc(brdev); 901 uint32_t tmp; 902 903 /* 904 * In the APM and early ACPI era, BIOSes saved the PCI config 905 * registers. As chips became more complicated, that functionality moved 906 * into the ACPI code / tables. We must therefore, restore the settings 907 * we made here to make sure the device come back. Transitions to Dx 908 * from D0 and back to D0 cause the bridge to lose its config space, so 909 * all the bus mappings and such are preserved. 910 * 911 * The PCI layer handles standard PCI registers like the 912 * command register and BARs, but cbb-specific registers are 913 * handled here. 914 */ 915 sc->chipinit(sc); 916 917 /* reset interrupt -- Do we really need to do this? */ 918 tmp = cbb_get(sc, CBB_SOCKET_EVENT); 919 cbb_set(sc, CBB_SOCKET_EVENT, tmp); 920 921 /* CSC Interrupt: Card detect interrupt on */ 922 cbb_setb(sc, CBB_SOCKET_MASK, CBB_SOCKET_MASK_CD); 923 924 /* Signal the thread to wakeup. */ 925 wakeup(&sc->intrhand); 926 927 error = bus_generic_resume(brdev); 928 929 return (error); 930 } 931 932 static device_method_t cbb_methods[] = { 933 /* Device interface */ 934 DEVMETHOD(device_probe, cbb_pci_probe), 935 DEVMETHOD(device_attach, cbb_pci_attach), 936 DEVMETHOD(device_detach, cbb_pci_detach), 937 DEVMETHOD(device_shutdown, cbb_pci_shutdown), 938 DEVMETHOD(device_suspend, cbb_pci_suspend), 939 DEVMETHOD(device_resume, cbb_pci_resume), 940 941 /* bus methods */ 942 DEVMETHOD(bus_read_ivar, cbb_read_ivar), 943 DEVMETHOD(bus_write_ivar, cbb_write_ivar), 944 #if defined(NEW_PCIB) && defined(PCI_RES_BUS) 945 DEVMETHOD(bus_alloc_resource, cbb_pci_alloc_resource), 946 DEVMETHOD(bus_adjust_resource, cbb_pci_adjust_resource), 947 DEVMETHOD(bus_release_resource, cbb_pci_release_resource), 948 #else 949 DEVMETHOD(bus_alloc_resource, cbb_alloc_resource), 950 DEVMETHOD(bus_release_resource, cbb_release_resource), 951 #endif 952 DEVMETHOD(bus_activate_resource, cbb_activate_resource), 953 DEVMETHOD(bus_deactivate_resource, cbb_deactivate_resource), 954 DEVMETHOD(bus_driver_added, cbb_driver_added), 955 DEVMETHOD(bus_child_detached, cbb_child_detached), 956 DEVMETHOD(bus_setup_intr, cbb_setup_intr), 957 DEVMETHOD(bus_teardown_intr, cbb_teardown_intr), 958 DEVMETHOD(bus_child_present, cbb_child_present), 959 960 /* 16-bit card interface */ 961 DEVMETHOD(card_set_res_flags, cbb_pcic_set_res_flags), 962 DEVMETHOD(card_set_memory_offset, cbb_pcic_set_memory_offset), 963 964 /* power interface */ 965 DEVMETHOD(power_enable_socket, cbb_power_enable_socket), 966 DEVMETHOD(power_disable_socket, cbb_power_disable_socket), 967 968 /* pcib compatibility interface */ 969 DEVMETHOD(pcib_maxslots, cbb_maxslots), 970 DEVMETHOD(pcib_read_config, cbb_read_config), 971 DEVMETHOD(pcib_write_config, cbb_write_config), 972 DEVMETHOD(pcib_route_interrupt, cbb_route_interrupt), 973 974 DEVMETHOD_END 975 }; 976 977 static driver_t cbb_driver = { 978 "cbb", 979 cbb_methods, 980 sizeof(struct cbb_softc) 981 }; 982 983 DRIVER_MODULE(cbb, pci, cbb_driver, cbb_devclass, 0, 0); 984 MODULE_DEPEND(cbb, exca, 1, 1, 1); 985