1 /* 2 * Copyright (c) 2000,2001 Jonathan Chen. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions, and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 /* 32 * Driver for PCI to Cardbus Bridge chips 33 * 34 * References: 35 * TI Datasheets: 36 * http://www-s.ti.com/cgi-bin/sc/generic2.cgi?family=PCI+CARDBUS+CONTROLLERS 37 * Much of the 16-bit PC Card compatibility code stolen from dev/pcic/i82365.c 38 * XXX and should be cleaned up to share as much as possible. 39 * 40 * Written by Jonathan Chen <jon@freebsd.org> 41 * The author would like to acknowledge: 42 * * HAYAKAWA Koichi: Author of the NetBSD code for the same thing 43 * * Warner Losh: Newbus/newcard guru and author of the pccard side of things 44 * * YAMAMOTO Shigeru: Author of another FreeBSD cardbus driver 45 * * David Cross: Author of the initial ugly hack for a specific cardbus card 46 */ 47 48 #define CBB_DEBUG 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/errno.h> 53 #include <sys/kernel.h> 54 #include <sys/kthread.h> 55 #include <sys/lock.h> 56 #include <sys/malloc.h> 57 #include <sys/mutex.h> 58 59 #include <sys/bus.h> 60 #include <machine/bus.h> 61 #include <sys/rman.h> 62 #include <machine/resource.h> 63 64 #include <pci/pcireg.h> 65 #include <pci/pcivar.h> 66 #include <machine/clock.h> 67 68 #include <dev/pccard/pccardreg.h> 69 #include <dev/pccard/pccardvar.h> 70 #include <dev/pcic/i82365reg.h> 71 72 #include <dev/pccbb/pccbbreg.h> 73 #include <dev/pccbb/pccbbvar.h> 74 75 #include "power_if.h" 76 #include "card_if.h" 77 #include "pcib_if.h" 78 79 #if defined CBB_DEBUG 80 #define DPRINTF(x) printf x 81 #define DEVPRINTF(x) device_printf x 82 #else 83 #define DPRINTF(x) 84 #define DEVPRINTF(x) 85 #endif 86 87 #define PCI_MASK_CONFIG(DEV,REG,MASK,SIZE) \ 88 pci_write_config(DEV, REG, pci_read_config(DEV, REG, SIZE) MASK, SIZE) 89 #define PCI_MASK2_CONFIG(DEV,REG,MASK1,MASK2,SIZE) \ 90 pci_write_config(DEV, REG, ( \ 91 pci_read_config(DEV, REG, SIZE) MASK1) MASK2, SIZE) 92 93 /* 94 * XXX all the pcic code really doesn't belong here and needs to be 95 * XXX migrated to its own file, shared with the 16-bit code 96 */ 97 #define PCIC_READ(SC,REG) \ 98 (((u_int8_t*)((SC)->sc_socketreg))[0x800+(REG)]) 99 #define PCIC_WRITE(SC,REG,val) \ 100 (((u_int8_t*)((SC)->sc_socketreg))[0x800+(REG)]) = (val) 101 #define PCIC_MASK(SC,REG,MASK) \ 102 PCIC_WRITE(SC,REG,PCIC_READ(SC,REG) MASK) 103 #define PCIC_MASK2(SC,REG,MASK,MASK2) \ 104 PCIC_WRITE(SC,REG,(PCIC_READ(SC,REG) MASK) MASK2) 105 106 struct pccbb_sclist { 107 struct pccbb_softc *sc; 108 STAILQ_ENTRY(pccbb_sclist) entries; 109 }; 110 111 static STAILQ_HEAD(, pccbb_sclist) softcs; 112 static int softcs_init = 0; 113 114 115 struct yenta_chipinfo { 116 u_int32_t yc_id; 117 const char *yc_name; 118 int yc_chiptype; 119 int yc_flags; 120 } yc_chipsets[] = { 121 /* Texas Instruments chips */ 122 {PCI_DEVICE_ID_PCIC_TI1130, "TI1130 PCI-CardBus Bridge", CB_TI113X, 123 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 124 {PCI_DEVICE_ID_PCIC_TI1131, "TI1131 PCI-CardBus Bridge", CB_TI113X, 125 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 126 127 {PCI_DEVICE_ID_PCIC_TI1211, "TI1211 PCI-CardBus Bridge", CB_TI12XX, 128 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 129 {PCI_DEVICE_ID_PCIC_TI1220, "TI1220 PCI-CardBus Bridge", CB_TI12XX, 130 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 131 {PCI_DEVICE_ID_PCIC_TI1221, "TI1221 PCI-CardBus Bridge", CB_TI12XX, 132 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 133 {PCI_DEVICE_ID_PCIC_TI1225, "TI1225 PCI-CardBus Bridge", CB_TI12XX, 134 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 135 {PCI_DEVICE_ID_PCIC_TI1250, "TI1250 PCI-CardBus Bridge", CB_TI12XX, 136 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 137 {PCI_DEVICE_ID_PCIC_TI1251, "TI1251 PCI-CardBus Bridge", CB_TI12XX, 138 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 139 {PCI_DEVICE_ID_PCIC_TI1251B,"TI1251B PCI-CardBus Bridge",CB_TI12XX, 140 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 141 {PCI_DEVICE_ID_PCIC_TI1410, "TI1410 PCI-CardBus Bridge", CB_TI12XX, 142 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 143 {PCI_DEVICE_ID_PCIC_TI1420, "TI1420 PCI-CardBus Bridge", CB_TI12XX, 144 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 145 {PCI_DEVICE_ID_PCIC_TI1450, "TI1450 PCI-CardBus Bridge", CB_TI12XX, 146 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 147 {PCI_DEVICE_ID_PCIC_TI1451, "TI1451 PCI-CardBus Bridge", CB_TI12XX, 148 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 149 {PCI_DEVICE_ID_PCIC_TI4451, "TI4451 PCI-CardBus Bridge", CB_TI12XX, 150 PCCBB_PCIC_IO_RELOC | PCCBB_PCIC_MEM_32}, 151 152 /* Ricoh chips */ 153 {PCI_DEVICE_ID_RICOH_RL5C465, "RF5C465 PCI-CardBus Bridge", 154 CB_RF5C46X, PCCBB_PCIC_MEM_32}, 155 {PCI_DEVICE_ID_RICOH_RL5C466, "RF5C466 PCI-CardBus Bridge", 156 CB_RF5C46X, PCCBB_PCIC_MEM_32}, 157 {PCI_DEVICE_ID_RICOH_RL5C475, "RF5C475 PCI-CardBus Bridge", 158 CB_RF5C47X, PCCBB_PCIC_MEM_32}, 159 {PCI_DEVICE_ID_RICOH_RL5C476, "RF5C476 PCI-CardBus Bridge", 160 CB_RF5C47X, PCCBB_PCIC_MEM_32}, 161 {PCI_DEVICE_ID_RICOH_RL5C478, "RF5C478 PCI-CardBus Bridge", 162 CB_RF5C47X, PCCBB_PCIC_MEM_32}, 163 164 /* Toshiba products */ 165 {PCI_DEVICE_ID_TOSHIBA_TOPIC95, "ToPIC95 PCI-CardBus Bridge", 166 CB_TOPIC95, PCCBB_PCIC_MEM_32}, 167 {PCI_DEVICE_ID_TOSHIBA_TOPIC95B, "ToPIC95B PCI-CardBus Bridge", 168 CB_TOPIC95B, PCCBB_PCIC_MEM_32}, 169 {PCI_DEVICE_ID_TOSHIBA_TOPIC97, "ToPIC97 PCI-CardBus Bridge", 170 CB_TOPIC97, PCCBB_PCIC_MEM_32}, 171 {PCI_DEVICE_ID_TOSHIBA_TOPIC100, "ToPIC100 PCI-CardBus Bridge", 172 CB_TOPIC97, PCCBB_PCIC_MEM_32}, 173 174 /* Cirrus Logic */ 175 {PCI_DEVICE_ID_PCIC_CLPD6832, "CLPD6832 PCI-CardBus Bridge", 176 CB_CIRRUS, PCCBB_PCIC_MEM_32}, 177 {PCI_DEVICE_ID_PCIC_CLPD6833, "CLPD6833 PCI-CardBus Bridge", 178 CB_CIRRUS, PCCBB_PCIC_MEM_32}, 179 180 /* 02Micro */ 181 {PCI_DEVICE_ID_PCIC_OZ6832, "O2Mirco OZ6832/6833 PCI-CardBus Bridge", 182 CB_CIRRUS, PCCBB_PCIC_MEM_32}, 183 {PCI_DEVICE_ID_PCIC_OZ6860, "O2Mirco OZ6836/6860 PCI-CardBus Bridge", 184 CB_CIRRUS, PCCBB_PCIC_MEM_32}, 185 {PCI_DEVICE_ID_PCIC_OZ6872, "O2Mirco OZ6812/6872 PCI-CardBus Bridge", 186 CB_CIRRUS, PCCBB_PCIC_MEM_32}, 187 188 /* sentinel */ 189 {0 /* null id */, "unknown", 190 CB_UNKNOWN, 0}, 191 }; 192 193 static int cb_chipset(u_int32_t pci_id, const char **namep, int *flagp); 194 static int pccbb_probe(device_t brdev); 195 static void pccbb_chipinit(struct pccbb_softc *sc); 196 static int pccbb_attach(device_t brdev); 197 static int pccbb_detach(device_t brdev); 198 static int pccbb_shutdown(device_t brdev); 199 static void pccbb_driver_added(device_t brdev, driver_t *driver); 200 static void pccbb_child_detached(device_t brdev, device_t child); 201 static int pccbb_card_reprobe(device_t brdev, device_t busdev); 202 static void pccbb_event_thread(void *arg); 203 static void pccbb_create_event_thread(struct pccbb_softc *sc); 204 static void pccbb_start_threads(void *arg); 205 static void pccbb_insert(struct pccbb_softc *sc); 206 static void pccbb_removal(struct pccbb_softc *sc); 207 static void pccbb_intr(void *arg); 208 static int pccbb_detect_voltage(device_t brdev); 209 static int pccbb_power(device_t brdev, int volts); 210 static void pccbb_cardbus_reset(device_t brdev); 211 static int pccbb_cardbus_power_enable_socket(device_t brdev, 212 device_t child); 213 static void pccbb_cardbus_power_disable_socket(device_t brdev, 214 device_t child); 215 static int pccbb_cardbus_io_open(device_t brdev, int win, u_int32_t start, 216 u_int32_t end); 217 static int pccbb_cardbus_mem_open(device_t brdev, int win, 218 u_int32_t start, u_int32_t end); 219 static void pccbb_cardbus_auto_open(struct pccbb_softc *sc, int type); 220 static int pccbb_cardbus_activate_resource(device_t brdev, device_t child, 221 int type, int rid, struct resource *res); 222 static int pccbb_cardbus_deactivate_resource(device_t brdev, 223 device_t child, int type, int rid, struct resource *res); 224 static struct resource *pccbb_cardbus_alloc_resource(device_t brdev, 225 device_t child, int type, int *rid, u_long start, 226 u_long end, u_long count, u_int flags); 227 static int pccbb_cardbus_release_resource(device_t brdev, device_t child, 228 int type, int rid, struct resource *res); 229 static int pccbb_pcic_power_enable_socket(device_t brdev, device_t child); 230 static void pccbb_pcic_power_disable_socket(device_t brdev, device_t child); 231 static void pccbb_pcic_wait_ready(struct pccbb_softc *sc); 232 static void pccbb_pcic_do_mem_map(struct pccbb_softc *sc, int win); 233 static int pccbb_pcic_mem_map(struct pccbb_softc *sc, int kind, 234 struct resource *res); 235 static void pccbb_pcic_mem_unmap(struct pccbb_softc *sc, int window); 236 static int pccbb_pcic_mem_findmap(struct pccbb_softc *sc, 237 struct resource *res); 238 static void pccbb_pcic_do_io_map(struct pccbb_softc *sc, int win); 239 static int pccbb_pcic_io_map(struct pccbb_softc *sc, int width, 240 struct resource *r); 241 static void pccbb_pcic_io_unmap(struct pccbb_softc *sc, int window); 242 static int pccbb_pcic_io_findmap(struct pccbb_softc *sc, 243 struct resource *res); 244 static int pccbb_pcic_activate_resource(device_t brdev, device_t child, 245 int type, int rid, struct resource *res); 246 static int pccbb_pcic_deactivate_resource(device_t brdev, device_t child, 247 int type, int rid, struct resource *res); 248 static struct resource *pccbb_pcic_alloc_resource(device_t brdev, 249 device_t child, int type, int *rid, u_long start, 250 u_long end, u_long count, u_int flags); 251 static int pccbb_pcic_release_resource(device_t brdev, device_t child, 252 int type, int rid, struct resource *res); 253 static int pccbb_pcic_set_res_flags(device_t brdev, device_t child, 254 int type, int rid, u_int32_t flags); 255 static int pccbb_pcic_set_memory_offset(device_t brdev, device_t child, 256 int rid, u_int32_t cardaddr, u_int32_t *deltap); 257 static int pccbb_power_enable_socket(device_t brdev, device_t child); 258 static void pccbb_power_disable_socket(device_t brdev, device_t child); 259 static int pccbb_activate_resource(device_t brdev, device_t child, 260 int type, int rid, struct resource *r); 261 static int pccbb_deactivate_resource(device_t brdev, device_t child, 262 int type, int rid, struct resource *r); 263 static struct resource *pccbb_alloc_resource(device_t brdev, device_t child, 264 int type, int *rid, u_long start, u_long end, u_long count, 265 u_int flags); 266 static int pccbb_release_resource(device_t brdev, device_t child, 267 int type, int rid, struct resource *r); 268 static int pccbb_read_ivar(device_t brdev, device_t child, int which, 269 uintptr_t *result); 270 static int pccbb_write_ivar(device_t brdev, device_t child, int which, 271 uintptr_t value); 272 static int pccbb_maxslots(device_t brdev); 273 static u_int32_t pccbb_read_config(device_t brdev, int b, int s, int f, 274 int reg, int width); 275 static void pccbb_write_config(device_t brdev, int b, int s, int f, 276 int reg, u_int32_t val, int width); 277 278 /************************************************************************/ 279 /* Probe/Attach */ 280 /************************************************************************/ 281 282 static int 283 cb_chipset(u_int32_t pci_id, const char **namep, int *flagp) 284 { 285 int loopend = sizeof(yc_chipsets)/sizeof(yc_chipsets[0]); 286 struct yenta_chipinfo *ycp, *ycend; 287 ycend = yc_chipsets + loopend; 288 289 for (ycp = yc_chipsets; ycp < ycend && pci_id != ycp->yc_id; ++ycp); 290 if (ycp == ycend) { 291 /* not found */ 292 ycp = yc_chipsets + loopend - 1; /* to point the sentinel */ 293 } 294 if (namep != NULL) { 295 *namep = ycp->yc_name; 296 } 297 if (flagp != NULL) { 298 *flagp = ycp->yc_flags; 299 } 300 return ycp->yc_chiptype; 301 } 302 303 static int 304 pccbb_probe(device_t brdev) 305 { 306 const char *name; 307 308 if (cb_chipset(pci_get_devid(brdev), &name, NULL) == CB_UNKNOWN) 309 return ENXIO; 310 device_set_desc(brdev, name); 311 return 0; 312 } 313 314 static void 315 pccbb_chipinit(struct pccbb_softc *sc) 316 { 317 /* Set CardBus latency timer */ 318 if (pci_read_config(sc->sc_dev, PCIR_SECLAT_1, 1) < 0x20) 319 pci_write_config(sc->sc_dev, PCIR_SECLAT_1, 0x20, 1); 320 321 /* Set PCI latency timer */ 322 if (pci_read_config(sc->sc_dev, PCIR_LATTIMER, 1) < 0x20) 323 pci_write_config(sc->sc_dev, PCIR_LATTIMER, 0x20, 1); 324 325 /* Enable memory access */ 326 PCI_MASK_CONFIG(sc->sc_dev, PCIR_COMMAND, 327 | PCIM_CMD_MEMEN 328 | PCIM_CMD_PORTEN 329 | PCIM_CMD_BUSMASTEREN, 2); 330 331 /* disable Legacy IO */ 332 switch (sc->sc_chipset) { 333 case CB_RF5C46X: 334 PCI_MASK_CONFIG(sc->sc_dev, PCCBBR_BRIDGECTRL, 335 & ~(PCCBBM_BRIDGECTRL_RL_3E0_EN | 336 PCCBBM_BRIDGECTRL_RL_3E2_EN), 2); 337 break; 338 default: 339 pci_write_config(sc->sc_dev, PCCBBR_LEGACY, 0x0, 4); 340 break; 341 } 342 343 /* Use PCI interrupt for interrupt routing */ 344 PCI_MASK2_CONFIG(sc->sc_dev, PCCBBR_BRIDGECTRL, 345 & ~(PCCBBM_BRIDGECTRL_MASTER_ABORT | 346 PCCBBM_BRIDGECTRL_INTR_IREQ_EN), 347 | PCCBBM_BRIDGECTRL_WRITE_POST_EN, 348 2); 349 350 /* XXX this should be a function table, ala OLDCARD. */ 351 switch (sc->sc_chipset) { 352 case CB_TI113X: 353 /* 354 * The TI 1030, TI 1130 and TI 1131 all require another bit 355 * be set to enable PCI routing of interrupts, and then 356 * a bit for each of the CSC and Function interrupts we 357 * want routed. 358 */ 359 PCI_MASK_CONFIG(sc->sc_dev, PCCBBR_CBCTRL, 360 | PCCBBM_CBCTRL_113X_PCI_INTR | 361 PCCBBM_CBCTRL_113X_PCI_CSC | PCCBBM_CBCTRL_113X_PCI_IRQ_EN, 362 1); 363 PCI_MASK_CONFIG(sc->sc_dev, PCCBBR_DEVCTRL, 364 & ~(PCCBBM_DEVCTRL_INT_SERIAL | 365 PCCBBM_DEVCTRL_INT_PCI), 1); 366 PCIC_WRITE(sc, PCIC_INTR, PCIC_INTR_ENABLE); 367 PCIC_WRITE(sc, PCIC_CSC_INTR, 0); 368 break; 369 case CB_TI12XX: 370 PCIC_WRITE(sc, PCIC_INTR, PCIC_INTR_ENABLE); 371 PCIC_WRITE(sc, PCIC_CSC_INTR, 0); 372 break; 373 case CB_TOPIC95B: 374 PCI_MASK_CONFIG(sc->sc_dev, PCCBBR_TOPIC_SOCKETCTRL, 375 | PCCBBM_TOPIC_SOCKETCTRL_SCR_IRQSEL, 4); 376 PCI_MASK2_CONFIG(sc->sc_dev, PCCBBR_TOPIC_SLOTCTRL, 377 | PCCBBM_TOPIC_SLOTCTRL_SLOTON | 378 PCCBBM_TOPIC_SLOTCTRL_SLOTEN | 379 PCCBBM_TOPIC_SLOTCTRL_ID_LOCK | 380 PCCBBM_TOPIC_SLOTCTRL_CARDBUS, 381 & ~PCCBBM_TOPIC_SLOTCTRL_SWDETECT, 4); 382 break; 383 } 384 385 /* close all memory and io windows */ 386 pci_write_config(sc->sc_dev, PCCBBR_MEMBASE0, 0xffffffff, 4); 387 pci_write_config(sc->sc_dev, PCCBBR_MEMLIMIT0, 0, 4); 388 pci_write_config(sc->sc_dev, PCCBBR_MEMBASE1, 0xffffffff, 4); 389 pci_write_config(sc->sc_dev, PCCBBR_MEMLIMIT1, 0, 4); 390 pci_write_config(sc->sc_dev, PCCBBR_IOBASE0, 0xffffffff, 4); 391 pci_write_config(sc->sc_dev, PCCBBR_IOLIMIT0, 0, 4); 392 pci_write_config(sc->sc_dev, PCCBBR_IOBASE1, 0xffffffff, 4); 393 pci_write_config(sc->sc_dev, PCCBBR_IOLIMIT1, 0, 4); 394 } 395 396 static int 397 pccbb_attach(device_t brdev) 398 { 399 struct pccbb_softc *sc = (struct pccbb_softc *)device_get_softc(brdev); 400 int rid; 401 u_int32_t tmp; 402 403 if (!softcs_init) { 404 softcs_init = 1; 405 STAILQ_INIT(&softcs); 406 } 407 mtx_init(&sc->sc_mtx, device_get_nameunit(brdev), MTX_DEF); 408 sc->sc_chipset = cb_chipset(pci_get_devid(brdev), NULL, &sc->sc_flags); 409 sc->sc_dev = brdev; 410 sc->sc_cbdev = NULL; 411 sc->sc_pccarddev = NULL; 412 sc->sc_secbus = pci_read_config(brdev, PCIR_SECBUS_2, 1); 413 sc->sc_subbus = pci_read_config(brdev, PCIR_SUBBUS_2, 1); 414 sc->memalloc = 0; 415 sc->ioalloc = 0; 416 SLIST_INIT(&sc->rl); 417 418 /* Ths PCI bus should have given me memory... right? */ 419 rid=PCCBBR_SOCKBASE; 420 sc->sc_base_res=bus_alloc_resource(brdev, SYS_RES_MEMORY, &rid, 421 0,~0,1, RF_ACTIVE); 422 if (!sc->sc_base_res) { 423 /* 424 * XXX EVILE HACK BAD THING! XXX 425 * The pci bus device should do this for us. 426 * Some BIOSes doesn't assign a memory space properly. 427 * So we try to manually put one in... 428 */ 429 u_int32_t sockbase; 430 431 sockbase = pci_read_config(brdev, rid, 4); 432 if (sockbase < 0x100000 || sockbase >= 0xfffffff0) { 433 pci_write_config(brdev, rid, 0xffffffff, 4); 434 sockbase = pci_read_config(brdev, rid, 4); 435 sockbase = (sockbase & 0xfffffff0) & 436 -(sockbase & 0xfffffff0); 437 sc->sc_base_res = bus_generic_alloc_resource( 438 device_get_parent(brdev), brdev, SYS_RES_MEMORY, 439 &rid, 0x10000000, ~0, sockbase, 440 RF_ACTIVE|rman_make_alignment_flags(sockbase)); 441 if (!sc->sc_base_res){ 442 device_printf(brdev, 443 "Could not grab register memory\n"); 444 mtx_destroy(&sc->sc_mtx); 445 return ENOMEM; 446 } 447 pci_write_config(brdev, PCCBBR_SOCKBASE, 448 rman_get_start(sc->sc_base_res), 4); 449 DEVPRINTF((brdev, "PCI Memory allocated: %08lx\n", 450 rman_get_start(sc->sc_base_res))); 451 } else { 452 device_printf(brdev, "Could not map register memory\n"); 453 mtx_destroy(&sc->sc_mtx); 454 return ENOMEM; 455 } 456 } 457 458 sc->sc_socketreg = 459 (struct pccbb_socketreg *)rman_get_virtual(sc->sc_base_res); 460 pccbb_chipinit(sc); 461 462 /* CSC Interrupt: Card detect interrupt on */ 463 sc->sc_socketreg->socket_mask |= PCCBB_SOCKET_MASK_CD; 464 465 /* reset interrupt */ 466 tmp = sc->sc_socketreg->socket_event; 467 sc->sc_socketreg->socket_event = tmp; 468 469 /* Map and establish the interrupt. */ 470 rid=0; 471 sc->sc_irq_res=bus_alloc_resource(brdev, SYS_RES_IRQ, &rid, 0, ~0, 1, 472 RF_SHAREABLE | RF_ACTIVE); 473 if (sc->sc_irq_res == NULL) { 474 printf("pccbb: Unable to map IRQ...\n"); 475 bus_release_resource(brdev, SYS_RES_MEMORY, PCCBBR_SOCKBASE, 476 sc->sc_base_res); 477 mtx_destroy(&sc->sc_mtx); 478 return ENOMEM; 479 } 480 481 if (bus_setup_intr(brdev, sc->sc_irq_res, INTR_TYPE_BIO, pccbb_intr, sc, 482 &(sc->sc_intrhand))) { 483 device_printf(brdev, "couldn't establish interrupt"); 484 bus_release_resource(brdev, SYS_RES_IRQ, 0, sc->sc_irq_res); 485 bus_release_resource(brdev, SYS_RES_MEMORY, PCCBBR_SOCKBASE, 486 sc->sc_base_res); 487 mtx_destroy(&sc->sc_mtx); 488 return ENOMEM; 489 } 490 491 /* attach children */ 492 sc->sc_cbdev = device_add_child(brdev, "cardbus", -1); 493 if (sc->sc_cbdev == NULL) 494 DEVPRINTF((brdev, "WARNING: cannot add cardbus bus.\n")); 495 else if (device_probe_and_attach(sc->sc_cbdev) != 0) { 496 DEVPRINTF((brdev, "WARNING: cannot attach cardbus bus!\n")); 497 sc->sc_cbdev = NULL; 498 } 499 500 sc->sc_pccarddev = device_add_child(brdev, "pccard", -1); 501 if (sc->sc_pccarddev == NULL) 502 DEVPRINTF((brdev, "WARNING: cannot add pccard bus.\n")); 503 else if (device_probe_and_attach(sc->sc_pccarddev) != 0) { 504 DEVPRINTF((brdev, "WARNING: cannot attach pccard bus.\n")); 505 sc->sc_pccarddev = NULL; 506 } 507 508 #ifndef KLD_MODULE 509 if (sc->sc_cbdev == NULL && sc->sc_pccarddev == NULL) { 510 device_printf(brdev, 511 "ERROR: Failed to attach cardbus/pccard bus!\n"); 512 bus_teardown_intr(brdev, sc->sc_irq_res, sc->sc_intrhand); 513 bus_release_resource(brdev, SYS_RES_IRQ, 0, sc->sc_irq_res); 514 bus_release_resource(brdev, SYS_RES_MEMORY, PCCBBR_SOCKBASE, 515 sc->sc_base_res); 516 mtx_destroy(&sc->sc_mtx); 517 return ENOMEM; 518 } 519 #endif 520 521 { 522 struct pccbb_sclist *sclist; 523 sclist = malloc(sizeof(struct pccbb_sclist), M_DEVBUF, 524 M_WAITOK); 525 sclist->sc = sc; 526 STAILQ_INSERT_TAIL(&softcs, sclist, entries); 527 } 528 return 0; 529 } 530 531 static int 532 pccbb_detach(device_t brdev) 533 { 534 struct pccbb_softc *sc = device_get_softc(brdev); 535 int numdevs; 536 device_t *devlist; 537 int tmp; 538 int error; 539 540 device_get_children(brdev, &devlist, &numdevs); 541 542 error = 0; 543 for (tmp = 0; tmp < numdevs; tmp++) { 544 if (device_detach(devlist[tmp]) == 0) 545 device_delete_child(brdev, devlist[tmp]); 546 else 547 error++; 548 } 549 free(devlist, M_TEMP); 550 if (error > 0) 551 return ENXIO; 552 553 mtx_lock(&sc->sc_mtx); 554 bus_teardown_intr(brdev, sc->sc_irq_res, sc->sc_intrhand); 555 556 sc->sc_flags |= PCCBB_KTHREAD_DONE; 557 if (sc->sc_flags & PCCBB_KTHREAD_RUNNING) { 558 wakeup(sc); 559 mtx_unlock(&sc->sc_mtx); 560 DEVPRINTF((brdev, "waiting for kthread exit...")); 561 error = tsleep(sc, PWAIT, "pccbb-detach-wait", 60 * hz); 562 if (error) 563 DPRINTF(("timeout\n")); 564 else 565 DPRINTF(("done\n")); 566 } else 567 mtx_unlock(&sc->sc_mtx); 568 569 bus_release_resource(brdev, SYS_RES_IRQ, 0, sc->sc_irq_res); 570 bus_release_resource(brdev, SYS_RES_MEMORY, PCCBBR_SOCKBASE, 571 sc->sc_base_res); 572 mtx_destroy(&sc->sc_mtx); 573 return 0; 574 } 575 576 static int 577 pccbb_shutdown(device_t brdev) 578 { 579 struct pccbb_softc *sc = (struct pccbb_softc *)device_get_softc(brdev); 580 /* properly reset everything at shutdown */ 581 582 PCI_MASK_CONFIG(brdev, PCCBBR_BRIDGECTRL, |PCCBBM_BRIDGECTRL_RESET, 2); 583 PCIC_MASK(sc, PCIC_INTR, & ~PCIC_INTR_RESET); 584 585 sc->sc_socketreg->socket_mask = 0; 586 587 pccbb_power(brdev, CARD_VCC_0V | CARD_VPP_0V); 588 589 PCIC_WRITE(sc, PCIC_ADDRWIN_ENABLE, 0); 590 pci_write_config(brdev, PCCBBR_MEMBASE0, 0, 4); 591 pci_write_config(brdev, PCCBBR_MEMLIMIT0, 0, 4); 592 pci_write_config(brdev, PCCBBR_MEMBASE1, 0, 4); 593 pci_write_config(brdev, PCCBBR_MEMLIMIT1, 0, 4); 594 pci_write_config(brdev, PCCBBR_IOBASE0, 0, 4); 595 pci_write_config(brdev, PCCBBR_IOLIMIT0, 0, 4); 596 pci_write_config(brdev, PCCBBR_IOBASE1, 0, 4); 597 pci_write_config(brdev, PCCBBR_IOLIMIT1, 0, 4); 598 pci_write_config(brdev, PCIR_COMMAND, 0, 2); 599 return 0; 600 } 601 602 static void 603 pccbb_driver_added(device_t brdev, driver_t *driver) 604 { 605 struct pccbb_softc *sc = device_get_softc(brdev); 606 device_t *devlist; 607 int tmp; 608 int numdevs; 609 int wake; 610 u_int32_t sockstate; 611 612 DEVICE_IDENTIFY(driver, brdev); 613 device_get_children(brdev, &devlist, &numdevs); 614 wake = 0; 615 sockstate = sc->sc_socketreg->socket_state; 616 for (tmp = 0; tmp < numdevs; tmp++) { 617 if (device_get_state(devlist[tmp]) == DS_NOTPRESENT && 618 device_probe_and_attach(devlist[tmp]) == 0) { 619 if (devlist[tmp] == NULL) 620 /* NOTHING */; 621 else if (strcmp(driver->name, "cardbus") == 0) { 622 sc->sc_cbdev = devlist[tmp]; 623 if (((sockstate & PCCBB_SOCKET_STAT_CD) == 0) && 624 (sockstate & PCCBB_SOCKET_STAT_CB)) 625 wake++; 626 } else if (strcmp(driver->name, "pccard") == 0) { 627 sc->sc_pccarddev = devlist[tmp]; 628 if (((sockstate & PCCBB_SOCKET_STAT_CD) == 0) && 629 (sockstate & PCCBB_SOCKET_STAT_16BIT)) 630 wake++; 631 } else 632 device_printf(brdev, 633 "Unsupported child bus: %s\n", 634 driver->name); 635 } 636 } 637 free(devlist, M_TEMP); 638 639 if (wake > 0) { 640 if ((sc->sc_socketreg->socket_state & PCCBB_SOCKET_STAT_CD) == 641 0) { 642 mtx_lock(&sc->sc_mtx); 643 wakeup(sc); 644 mtx_unlock(&sc->sc_mtx); 645 } 646 } 647 } 648 649 static void 650 pccbb_child_detached(device_t brdev, device_t child) 651 { 652 struct pccbb_softc *sc = device_get_softc(brdev); 653 if (child == sc->sc_cbdev) 654 sc->sc_cbdev = NULL; 655 else if (child == sc->sc_pccarddev) 656 sc->sc_pccarddev = NULL; 657 else 658 device_printf(brdev, "Unknown child detached: %s %p/%p\n", 659 device_get_nameunit(child), sc->sc_cbdev, sc->sc_pccarddev); 660 } 661 662 static int 663 pccbb_card_reprobe(device_t brdev, device_t busdev) { 664 struct pccbb_softc *sc = device_get_softc(brdev); 665 int wake = 0; 666 u_int32_t sockstate; 667 668 sockstate = sc->sc_socketreg->socket_state; 669 670 if ((sockstate & PCCBB_SOCKET_STAT_CD) == 0) { 671 if (busdev == sc->sc_cbdev && 672 (sockstate & PCCBB_SOCKET_STAT_CB)) 673 wake++; 674 else if (busdev == sc->sc_pccarddev && 675 (sockstate & PCCBB_SOCKET_STAT_16BIT)) 676 wake++; 677 678 if (wake > 0) { 679 mtx_lock(&sc->sc_mtx); 680 wakeup(sc); 681 mtx_unlock(&sc->sc_mtx); 682 return 0; 683 } 684 return EBUSY; 685 } 686 return ENOENT; 687 } 688 689 /************************************************************************/ 690 /* Kthreads */ 691 /************************************************************************/ 692 693 static void 694 pccbb_event_thread(void *arg) 695 { 696 struct pccbb_softc *sc = arg; 697 u_int32_t status; 698 699 mtx_lock(&Giant); 700 for(;;) { 701 if (!(sc->sc_flags & PCCBB_KTHREAD_RUNNING)) 702 sc->sc_flags |= PCCBB_KTHREAD_RUNNING; 703 else { 704 tsleep (sc, PWAIT, "pccbbev", 0); 705 /* 706 * Delay 1 second, make sure the user is done with 707 * whatever he is doing. We tsleep on sc->sc_flags, 708 * which should never be woken up. 709 * 710 * XXX Note: This can cause problems on card 711 * removal. See OLDCARD's ISR for how you may 712 * have to deal with the debouncing problem. The 713 * crux of the issue is interrupts delivered to 714 * the card after eject are unstable. 715 */ 716 tsleep (&sc->sc_flags, PWAIT, "pccbbev", 1*hz); 717 } 718 mtx_lock(&sc->sc_mtx); 719 if (sc->sc_flags & PCCBB_KTHREAD_DONE) 720 break; 721 722 status = sc->sc_socketreg->socket_state; 723 if ((status & PCCBB_SOCKET_STAT_CD) == 0) { 724 pccbb_insert(sc); 725 } else { 726 pccbb_removal(sc); 727 } 728 mtx_unlock(&sc->sc_mtx); 729 } 730 mtx_unlock(&sc->sc_mtx); 731 sc->sc_flags &= ~PCCBB_KTHREAD_RUNNING; 732 wakeup(sc); 733 kthread_exit(0); 734 } 735 736 static void 737 pccbb_create_event_thread(struct pccbb_softc *sc) 738 { 739 if (kthread_create(pccbb_event_thread, sc, &sc->event_thread, 740 0, "%s%d", device_get_name(sc->sc_dev), 741 device_get_unit(sc->sc_dev))) { 742 device_printf (sc->sc_dev, "unable to create event thread.\n"); 743 panic ("pccbb_create_event_thread"); 744 } 745 } 746 747 static void 748 pccbb_start_threads(void *arg) 749 { 750 struct pccbb_sclist *sclist; 751 752 STAILQ_FOREACH(sclist, &softcs, entries) { 753 pccbb_create_event_thread(sclist->sc); 754 } 755 } 756 757 /************************************************************************/ 758 /* Insert/removal */ 759 /************************************************************************/ 760 761 static void 762 pccbb_insert(struct pccbb_softc *sc) 763 { 764 u_int32_t sockevent, sockstate; 765 int timeout = 30; 766 767 do { 768 sockevent = sc->sc_socketreg->socket_event; 769 sockstate = sc->sc_socketreg->socket_state; 770 } while (sockstate & PCCBB_SOCKET_STAT_CD && --timeout > 0); 771 772 if (timeout < 0) { 773 device_printf (sc->sc_dev, "insert timeout"); 774 return; 775 } 776 777 DEVPRINTF((sc->sc_dev, "card inserted: event=0x%08x, state=%08x\n", 778 sockevent, sockstate)); 779 780 if (sockstate & PCCBB_SOCKET_STAT_16BIT && sc->sc_pccarddev != NULL) { 781 sc->sc_flags |= PCCBB_16BIT_CARD; 782 if (CARD_ATTACH_CARD(sc->sc_pccarddev) != 0) 783 device_printf(sc->sc_dev, "card activation failed\n"); 784 } else if (sockstate & PCCBB_SOCKET_STAT_CB && sc->sc_cbdev != NULL) { 785 sc->sc_flags &= ~PCCBB_16BIT_CARD; 786 if (CARD_ATTACH_CARD(sc->sc_cbdev) != 0) 787 device_printf(sc->sc_dev, "card activation failed\n"); 788 } else { 789 device_printf (sc->sc_dev, "Unsupported card type detected\n"); 790 } 791 } 792 793 static void 794 pccbb_removal(struct pccbb_softc *sc) 795 { 796 struct pccbb_reslist *rle; 797 798 if (sc->sc_flags & PCCBB_16BIT_CARD && sc->sc_pccarddev != NULL) 799 CARD_DETACH_CARD(sc->sc_pccarddev, DETACH_FORCE); 800 else if ((!(sc->sc_flags & PCCBB_16BIT_CARD)) && sc->sc_cbdev != NULL) 801 CARD_DETACH_CARD(sc->sc_cbdev, DETACH_FORCE); 802 803 while ((rle = SLIST_FIRST(&sc->rl)) != NULL) { 804 device_printf(sc->sc_dev, "Danger Will Robinson: Resource " 805 "left allocated! This is a bug... " 806 "(rid=%x, type=%d, addr=%lx)\n", rle->rid, rle->type, 807 rman_get_start(rle->res)); 808 SLIST_REMOVE_HEAD(&sc->rl, link); 809 free(rle, M_DEVBUF); 810 } 811 } 812 813 /************************************************************************/ 814 /* Interrupt Handler */ 815 /************************************************************************/ 816 817 static void 818 pccbb_intr(void *arg) 819 { 820 struct pccbb_softc *sc = arg; 821 u_int32_t sockevent; 822 823 if (!(sockevent = sc->sc_socketreg->socket_event)) { 824 /* not for me. */ 825 return; 826 } 827 828 /* reset bit */ 829 sc->sc_socketreg->socket_event = sockevent | 0x01; 830 831 if (sockevent & PCCBB_SOCKET_EVENT_CD) { 832 mtx_lock(&sc->sc_mtx); 833 wakeup(sc); 834 mtx_unlock(&sc->sc_mtx); 835 } else { 836 if (sockevent & PCCBB_SOCKET_EVENT_CSTS) { 837 DPRINTF((" cstsevent occures, 0x%08x\n", 838 sc->sc_socketreg->socket_state)); 839 } 840 if (sockevent & PCCBB_SOCKET_EVENT_POWER) { 841 DPRINTF((" pwrevent occures, 0x%08x\n", 842 sc->sc_socketreg->socket_state)); 843 } 844 } 845 846 return; 847 } 848 849 /************************************************************************/ 850 /* Generic Power functions */ 851 /************************************************************************/ 852 853 static int 854 pccbb_detect_voltage(device_t brdev) 855 { 856 struct pccbb_softc *sc = device_get_softc(brdev); 857 u_int32_t psr; 858 int vol = CARD_UKN_CARD; 859 860 psr = sc->sc_socketreg->socket_state; 861 862 if (psr & PCCBB_SOCKET_STAT_5VCARD) { 863 vol |= CARD_5V_CARD; 864 } 865 if (psr & PCCBB_SOCKET_STAT_3VCARD) { 866 vol |= CARD_3V_CARD; 867 } 868 if (psr & PCCBB_SOCKET_STAT_XVCARD) { 869 vol |= CARD_XV_CARD; 870 } 871 if (psr & PCCBB_SOCKET_STAT_YVCARD) { 872 vol |= CARD_YV_CARD; 873 } 874 875 return vol; 876 } 877 878 static int 879 pccbb_power(device_t brdev, int volts) 880 { 881 u_int32_t status, sock_ctrl; 882 struct pccbb_softc *sc = device_get_softc(brdev); 883 884 DEVPRINTF((sc->sc_dev, "pccbb_power: %s and %s [%x]\n", 885 (volts & CARD_VCCMASK) == CARD_VCC_UC ? "CARD_VCC_UC" : 886 (volts & CARD_VCCMASK) == CARD_VCC_5V ? "CARD_VCC_5V" : 887 (volts & CARD_VCCMASK) == CARD_VCC_3V ? "CARD_VCC_3V" : 888 (volts & CARD_VCCMASK) == CARD_VCC_XV ? "CARD_VCC_XV" : 889 (volts & CARD_VCCMASK) == CARD_VCC_YV ? "CARD_VCC_YV" : 890 (volts & CARD_VCCMASK) == CARD_VCC_0V ? "CARD_VCC_0V" : 891 "VCC-UNKNOWN", 892 (volts & CARD_VPPMASK) == CARD_VPP_UC ? "CARD_VPP_UC" : 893 (volts & CARD_VPPMASK) == CARD_VPP_12V ? "CARD_VPP_12V" : 894 (volts & CARD_VPPMASK) == CARD_VPP_VCC ? "CARD_VPP_VCC" : 895 (volts & CARD_VPPMASK) == CARD_VPP_0V ? "CARD_VPP_0V" : 896 "VPP-UNKNOWN", 897 volts)); 898 899 status = sc->sc_socketreg->socket_state; 900 sock_ctrl = sc->sc_socketreg->socket_control; 901 902 switch (volts & CARD_VCCMASK) { 903 case CARD_VCC_UC: 904 break; 905 case CARD_VCC_5V: 906 if (PCCBB_SOCKET_STAT_5VCARD & status) { /* check 5 V card */ 907 sock_ctrl &= ~PCCBB_SOCKET_CTRL_VCCMASK; 908 sock_ctrl |= PCCBB_SOCKET_CTRL_VCC_5V; 909 } else { 910 device_printf(sc->sc_dev, 911 "BAD voltage request: no 5 V card\n"); 912 } 913 break; 914 case CARD_VCC_3V: 915 if (PCCBB_SOCKET_STAT_3VCARD & status) { 916 sock_ctrl &= ~PCCBB_SOCKET_CTRL_VCCMASK; 917 sock_ctrl |= PCCBB_SOCKET_CTRL_VCC_3V; 918 } else { 919 device_printf(sc->sc_dev, 920 "BAD voltage request: no 3.3 V card\n"); 921 } 922 break; 923 case CARD_VCC_0V: 924 sock_ctrl &= ~PCCBB_SOCKET_CTRL_VCCMASK; 925 break; 926 default: 927 return 0; /* power NEVER changed */ 928 break; 929 } 930 931 switch (volts & CARD_VPPMASK) { 932 case CARD_VPP_UC: 933 break; 934 case CARD_VPP_0V: 935 sock_ctrl &= ~PCCBB_SOCKET_CTRL_VPPMASK; 936 break; 937 case CARD_VPP_VCC: 938 sock_ctrl &= ~PCCBB_SOCKET_CTRL_VPPMASK; 939 sock_ctrl |= ((sock_ctrl >> 4) & 0x07); 940 break; 941 case CARD_VPP_12V: 942 sock_ctrl &= ~PCCBB_SOCKET_CTRL_VPPMASK; 943 sock_ctrl |= PCCBB_SOCKET_CTRL_VPP_12V; 944 break; 945 } 946 947 if (sc->sc_socketreg->socket_control == sock_ctrl) 948 return 1; /* no change necessary */ 949 950 sc->sc_socketreg->socket_control = sock_ctrl; 951 status = sc->sc_socketreg->socket_state; 952 953 { 954 int timeout = 20; 955 u_int32_t sockevent; 956 do { 957 DELAY(20*1000); 958 sockevent = sc->sc_socketreg->socket_event; 959 } while (!(sockevent & PCCBB_SOCKET_EVENT_POWER) && 960 --timeout > 0); 961 /* reset event status */ 962 sc->sc_socketreg->socket_event = sockevent; 963 if (timeout < 0) { 964 printf ("VCC supply failed.\n"); 965 return 0; 966 } 967 } 968 /* XXX 969 * delay 400 ms: thgough the standard defines that the Vcc set-up time 970 * is 20 ms, some PC-Card bridge requires longer duration. 971 */ 972 DELAY(400*1000); 973 974 if (status & PCCBB_SOCKET_STAT_BADVCC) { 975 device_printf(sc->sc_dev, 976 "bad Vcc request. ctrl=0x%x, status=0x%x\n", 977 sock_ctrl ,status); 978 printf("pccbb_power: %s and %s [%x]\n", 979 (volts & CARD_VCCMASK) == CARD_VCC_UC ? "CARD_VCC_UC" : 980 (volts & CARD_VCCMASK) == CARD_VCC_5V ? "CARD_VCC_5V" : 981 (volts & CARD_VCCMASK) == CARD_VCC_3V ? "CARD_VCC_3V" : 982 (volts & CARD_VCCMASK) == CARD_VCC_XV ? "CARD_VCC_XV" : 983 (volts & CARD_VCCMASK) == CARD_VCC_YV ? "CARD_VCC_YV" : 984 (volts & CARD_VCCMASK) == CARD_VCC_0V ? "CARD_VCC_0V" : 985 "VCC-UNKNOWN", 986 (volts & CARD_VPPMASK) == CARD_VPP_UC ? "CARD_VPP_UC" : 987 (volts & CARD_VPPMASK) == CARD_VPP_12V ? "CARD_VPP_12V": 988 (volts & CARD_VPPMASK) == CARD_VPP_VCC ? "CARD_VPP_VCC": 989 (volts & CARD_VPPMASK) == CARD_VPP_0V ? "CARD_VPP_0V" : 990 "VPP-UNKNOWN", 991 volts); 992 return 0; 993 } 994 return 1; /* power changed correctly */ 995 } 996 997 /************************************************************************/ 998 /* Cardbus power functions */ 999 /************************************************************************/ 1000 1001 static void 1002 pccbb_cardbus_reset(device_t brdev) 1003 { 1004 struct pccbb_softc *sc = device_get_softc(brdev); 1005 int delay_us; 1006 1007 delay_us = sc->sc_chipset == CB_RF5C47X ? 400*1000 : 20*1000; 1008 1009 PCI_MASK_CONFIG(brdev, PCCBBR_BRIDGECTRL, |PCCBBM_BRIDGECTRL_RESET, 2); 1010 1011 DELAY(delay_us); 1012 1013 /* If a card exists, unreset it! */ 1014 if ((sc->sc_socketreg->socket_state & PCCBB_SOCKET_STAT_CD) == 0) { 1015 PCI_MASK_CONFIG(brdev, PCCBBR_BRIDGECTRL, 1016 &~PCCBBM_BRIDGECTRL_RESET, 2); 1017 DELAY(delay_us); 1018 } 1019 } 1020 1021 static int 1022 pccbb_cardbus_power_enable_socket(device_t brdev, device_t child) 1023 { 1024 struct pccbb_softc *sc = device_get_softc(brdev); 1025 int voltage; 1026 1027 if ((sc->sc_socketreg->socket_state & PCCBB_SOCKET_STAT_CD) == 1028 PCCBB_SOCKET_STAT_CD) 1029 return ENODEV; 1030 1031 voltage = pccbb_detect_voltage(brdev); 1032 1033 pccbb_power(brdev, CARD_VCC_0V | CARD_VPP_0V); 1034 if (voltage & CARD_5V_CARD) 1035 pccbb_power(brdev, CARD_VCC_5V | CARD_VPP_VCC); 1036 else if (voltage & CARD_3V_CARD) 1037 pccbb_power(brdev, CARD_VCC_3V | CARD_VPP_VCC); 1038 else { 1039 device_printf(brdev, "Unknown card voltage\n"); 1040 return ENXIO; 1041 } 1042 1043 pccbb_cardbus_reset(brdev); 1044 return 0; 1045 } 1046 1047 static void 1048 pccbb_cardbus_power_disable_socket(device_t brdev, device_t child) 1049 { 1050 pccbb_power(brdev, CARD_VCC_0V | CARD_VPP_0V); 1051 pccbb_cardbus_reset(brdev); 1052 } 1053 1054 /************************************************************************/ 1055 /* Cardbus Resource */ 1056 /************************************************************************/ 1057 1058 static int 1059 pccbb_cardbus_io_open(device_t brdev, int win, u_int32_t start, u_int32_t end) 1060 { 1061 int basereg; 1062 int limitreg; 1063 1064 if ((win < 0) || (win > 1)) { 1065 DEVPRINTF((brdev, 1066 "pccbb_cardbus_io_open: window out of range %d\n", win)); 1067 return EINVAL; 1068 } 1069 1070 basereg = win*8 + PCCBBR_IOBASE0; 1071 limitreg = win*8 + PCCBBR_IOLIMIT0; 1072 1073 pci_write_config(brdev, basereg, start, 4); 1074 pci_write_config(brdev, limitreg, end, 4); 1075 return 0; 1076 } 1077 1078 static int 1079 pccbb_cardbus_mem_open(device_t brdev, int win, u_int32_t start, u_int32_t end) 1080 { 1081 int basereg; 1082 int limitreg; 1083 1084 if ((win < 0) || (win > 1)) { 1085 DEVPRINTF((brdev, 1086 "pccbb_cardbus_mem_open: window out of range %d\n", win)); 1087 return EINVAL; 1088 } 1089 1090 basereg = win*8 + PCCBBR_MEMBASE0; 1091 limitreg = win*8 + PCCBBR_MEMLIMIT0; 1092 1093 pci_write_config(brdev, basereg, start, 4); 1094 pci_write_config(brdev, limitreg, end, 4); 1095 return 0; 1096 } 1097 1098 static void 1099 pccbb_cardbus_auto_open(struct pccbb_softc *sc, int type) 1100 { 1101 u_int32_t starts[2]; 1102 u_int32_t ends[2]; 1103 struct pccbb_reslist *rle; 1104 int align; 1105 int prefetchable[2]; 1106 1107 starts[0] = starts[1] = 0xffffffff; 1108 ends[0] = ends[1] = 0; 1109 1110 if (type == SYS_RES_MEMORY) 1111 align = PCCBB_MEMALIGN; 1112 else if (type == SYS_RES_IOPORT) 1113 align = PCCBB_IOALIGN; 1114 else 1115 align = 1; 1116 1117 SLIST_FOREACH(rle, &sc->rl, link) { 1118 if (rle->type != type) 1119 ; 1120 else if (rle->res == NULL) { 1121 device_printf(sc->sc_dev, "WARNING: Resource not reserved? " 1122 "(type=%d, addr=%lx)\n", 1123 rle->type, rman_get_start(rle->res)); 1124 } else if (!(rman_get_flags(rle->res) & RF_ACTIVE)) { 1125 } else if (starts[0] == 0xffffffff) { 1126 starts[0] = rman_get_start(rle->res); 1127 ends[0] = rman_get_end(rle->res); 1128 prefetchable[0] = 1129 rman_get_flags(rle->res) & RF_PREFETCHABLE; 1130 } else if (rman_get_end(rle->res) > ends[0] && 1131 rman_get_start(rle->res) - ends[0] < 1132 PCCBB_AUTO_OPEN_SMALLHOLE && prefetchable[0] == 1133 (rman_get_flags(rle->res) & RF_PREFETCHABLE)) { 1134 ends[0] = rman_get_end(rle->res); 1135 } else if (rman_get_start(rle->res) < starts[0] && 1136 starts[0] - rman_get_end(rle->res) < 1137 PCCBB_AUTO_OPEN_SMALLHOLE && prefetchable[0] == 1138 (rman_get_flags(rle->res) & RF_PREFETCHABLE)) { 1139 starts[0] = rman_get_start(rle->res); 1140 } else if (starts[1] == 0xffffffff) { 1141 starts[1] = rman_get_start(rle->res); 1142 ends[1] = rman_get_end(rle->res); 1143 prefetchable[1] = 1144 rman_get_flags(rle->res) & RF_PREFETCHABLE; 1145 } else if (rman_get_end(rle->res) > ends[1] && 1146 rman_get_start(rle->res) - ends[1] < 1147 PCCBB_AUTO_OPEN_SMALLHOLE && prefetchable[1] == 1148 (rman_get_flags(rle->res) & RF_PREFETCHABLE)) { 1149 ends[1] = rman_get_end(rle->res); 1150 } else if (rman_get_start(rle->res) < starts[1] && 1151 starts[1] - rman_get_end(rle->res) < 1152 PCCBB_AUTO_OPEN_SMALLHOLE && prefetchable[1] == 1153 (rman_get_flags(rle->res) & RF_PREFETCHABLE)) { 1154 starts[1] = rman_get_start(rle->res); 1155 } else { 1156 u_int32_t diffs[2]; 1157 int win; 1158 1159 diffs[0] = diffs[1] = 0xffffffff; 1160 if (rman_get_start(rle->res) > ends[0]) 1161 diffs[0] = rman_get_start(rle->res) - ends[0]; 1162 else if (rman_get_end(rle->res) < starts[0]) 1163 diffs[0] = starts[0] - rman_get_end(rle->res); 1164 if (rman_get_start(rle->res) > ends[1]) 1165 diffs[1] = rman_get_start(rle->res) - ends[1]; 1166 else if (rman_get_end(rle->res) < starts[1]) 1167 diffs[1] = starts[1] - rman_get_end(rle->res); 1168 1169 win = (diffs[0] <= diffs[1])?0:1; 1170 if (rman_get_start(rle->res) > ends[win]) 1171 ends[win] = rman_get_end(rle->res); 1172 else if (rman_get_end(rle->res) < starts[win]) 1173 starts[win] = rman_get_start(rle->res); 1174 if (!(rman_get_flags(rle->res) & RF_PREFETCHABLE)) 1175 prefetchable[win] = 0; 1176 } 1177 1178 if (starts[0] != 0xffffffff) 1179 starts[0] -= starts[0] % align; 1180 if (starts[1] != 0xffffffff) 1181 starts[1] -= starts[1] % align; 1182 if (ends[0] % align != 0) 1183 ends[0] += align - ends[0]%align - 1; 1184 if (ends[1] % align != 0) 1185 ends[1] += align - ends[1]%align - 1; 1186 } 1187 1188 if (type == SYS_RES_MEMORY) { 1189 u_int32_t reg; 1190 1191 pccbb_cardbus_mem_open(sc->sc_dev, 0, starts[0], ends[0]); 1192 pccbb_cardbus_mem_open(sc->sc_dev, 1, starts[1], ends[1]); 1193 reg = pci_read_config(sc->sc_dev, PCCBBR_BRIDGECTRL, 2); 1194 reg &= ~(PCCBBM_BRIDGECTRL_PREFETCH_0| 1195 PCCBBM_BRIDGECTRL_PREFETCH_1); 1196 reg |= (prefetchable[0]?PCCBBM_BRIDGECTRL_PREFETCH_0:0)| 1197 (prefetchable[1]?PCCBBM_BRIDGECTRL_PREFETCH_1:0); 1198 pci_write_config(sc->sc_dev, PCCBBR_BRIDGECTRL, reg, 2); 1199 } else if (type == SYS_RES_IOPORT) { 1200 pccbb_cardbus_io_open(sc->sc_dev, 0, starts[0], ends[0]); 1201 pccbb_cardbus_io_open(sc->sc_dev, 1, starts[1], ends[1]); 1202 } 1203 } 1204 1205 static int 1206 pccbb_cardbus_activate_resource(device_t brdev, device_t child, int type, 1207 int rid, struct resource *res) 1208 { 1209 int ret; 1210 1211 ret = BUS_ACTIVATE_RESOURCE(device_get_parent(brdev), child, 1212 type, rid, res); 1213 if (ret != 0) return ret; 1214 pccbb_cardbus_auto_open(device_get_softc(brdev), type); 1215 return 0; 1216 } 1217 1218 static int 1219 pccbb_cardbus_deactivate_resource(device_t brdev, device_t child, int type, 1220 int rid, struct resource *res) 1221 { 1222 int ret; 1223 1224 ret = BUS_DEACTIVATE_RESOURCE(device_get_parent(brdev), child, 1225 type, rid, res); 1226 if (ret != 0) return ret; 1227 pccbb_cardbus_auto_open(device_get_softc(brdev), type); 1228 return 0; 1229 } 1230 1231 static struct resource * 1232 pccbb_cardbus_alloc_resource(device_t brdev, device_t child, int type, int *rid, 1233 u_long start, u_long end, u_long count, u_int flags) 1234 { 1235 struct pccbb_softc *sc = device_get_softc(brdev); 1236 struct pccbb_reslist *rle; 1237 int tmp; 1238 struct resource *res; 1239 1240 switch (type) { 1241 case SYS_RES_IRQ: 1242 tmp = rman_get_start(sc->sc_irq_res); 1243 if (start > tmp || end < tmp || count != 1) { 1244 device_printf(child, "requested interrupt %ld-%ld," 1245 "count = %ld not supported by pccbb\n", 1246 start, end, count); 1247 return NULL; 1248 } 1249 start = end = tmp; 1250 break; 1251 case SYS_RES_IOPORT: 1252 if (start <= 0x1000) 1253 start = 0x1000; 1254 if (end < start) 1255 end = start; 1256 break; 1257 case SYS_RES_MEMORY: 1258 if (start <= 0x44000000) 1259 start = 0x44000000; 1260 if (end < start) 1261 end = start; 1262 break; 1263 } 1264 1265 res = BUS_ALLOC_RESOURCE(device_get_parent(brdev), child, type, rid, 1266 start, end, count, flags & ~RF_ACTIVE); 1267 if (res == NULL) { 1268 printf("pccbb alloc res fail\n"); 1269 return NULL; 1270 } 1271 1272 /* 1273 * Need to record allocated resource so we can iterate through 1274 * it later. 1275 */ 1276 rle = malloc(sizeof(struct pccbb_reslist), M_DEVBUF, M_NOWAIT); 1277 if (!res) 1278 panic("pccbb_cardbus_alloc_resource: can't record entry!"); 1279 rle->res = res; 1280 rle->type = type; 1281 rle->rid = *rid; 1282 rle->cardaddr = 0; 1283 SLIST_INSERT_HEAD(&sc->rl, rle, link); 1284 1285 if (flags & RF_ACTIVE) 1286 if (bus_activate_resource(child, type, *rid, res) != 0) { 1287 bus_release_resource(child, type, *rid, res); 1288 return NULL; 1289 } 1290 1291 return res; 1292 } 1293 1294 static int 1295 pccbb_cardbus_release_resource(device_t brdev, device_t child, int type, 1296 int rid, struct resource *res) 1297 { 1298 struct pccbb_softc *sc = device_get_softc(brdev); 1299 struct pccbb_reslist *rle; 1300 1301 if (rman_get_flags(res) & RF_ACTIVE) { 1302 int error; 1303 error = bus_deactivate_resource(child, type, rid, res); 1304 if (error != 0) 1305 return error; 1306 } 1307 1308 SLIST_FOREACH(rle, &sc->rl, link) { 1309 if (rle->res == res) { 1310 SLIST_REMOVE(&sc->rl, rle, pccbb_reslist, link); 1311 free(rle, M_DEVBUF); 1312 break; 1313 } 1314 } 1315 1316 return BUS_RELEASE_RESOURCE(device_get_parent(brdev), child, 1317 type, rid, res); 1318 } 1319 1320 /************************************************************************/ 1321 /* PC Card Power Functions */ 1322 /************************************************************************/ 1323 1324 static int 1325 pccbb_pcic_power_enable_socket(device_t brdev, device_t child) 1326 { 1327 struct pccbb_softc *sc = device_get_softc(brdev); 1328 1329 DPRINTF(("pccbb_pcic_socket_enable:\n")); 1330 1331 /* power down/up the socket to reset */ 1332 { 1333 int voltage = pccbb_detect_voltage(brdev); 1334 1335 pccbb_power(brdev, CARD_VCC_0V | CARD_VPP_0V); 1336 if (voltage & CARD_5V_CARD) 1337 pccbb_power(brdev, CARD_VCC_5V | CARD_VPP_VCC); 1338 else if (voltage & CARD_3V_CARD) 1339 pccbb_power(brdev, CARD_VCC_3V | CARD_VPP_VCC); 1340 else { 1341 device_printf(brdev, "Unknown card voltage\n"); 1342 return ENXIO; 1343 } 1344 } 1345 1346 /* enable socket i/o */ 1347 PCIC_MASK(sc, PCIC_PWRCTL, | PCIC_PWRCTL_OE); 1348 1349 PCIC_WRITE(sc, PCIC_INTR, PCIC_INTR_ENABLE); 1350 /* hold reset for 30ms */ 1351 DELAY(30*1000); 1352 /* clear the reset flag */ 1353 PCIC_MASK(sc, PCIC_INTR, | PCIC_INTR_RESET); 1354 /* wait 20ms as per pc card standard (r2.01) section 4.3.6 */ 1355 DELAY(20*1000); 1356 1357 pccbb_pcic_wait_ready(sc); 1358 1359 /* disable all address windows */ 1360 PCIC_WRITE(sc, PCIC_ADDRWIN_ENABLE, 0); 1361 1362 { 1363 int cardtype; 1364 CARD_GET_TYPE(child, &cardtype); 1365 PCIC_MASK(sc, PCIC_INTR, | ((cardtype == PCCARD_IFTYPE_IO) ? 1366 PCIC_INTR_CARDTYPE_IO : PCIC_INTR_CARDTYPE_MEM)); 1367 DEVPRINTF((sc->sc_dev, "card type is %s\n", 1368 (cardtype == PCCARD_IFTYPE_IO) ? "io" : "mem")); 1369 } 1370 1371 /* reinstall all the memory and io mappings */ 1372 { 1373 int win; 1374 1375 for (win = 0; win < PCIC_MEM_WINS; ++win) { 1376 if (sc->memalloc & (1 << win)) { 1377 pccbb_pcic_do_mem_map(sc, win); 1378 } 1379 } 1380 for (win = 0; win < PCIC_IO_WINS; ++win) { 1381 if (sc->ioalloc & (1 << win)) { 1382 pccbb_pcic_do_io_map(sc, win); 1383 } 1384 } 1385 } 1386 return 0; 1387 } 1388 1389 static void 1390 pccbb_pcic_power_disable_socket(device_t brdev, device_t child) 1391 { 1392 struct pccbb_softc *sc = device_get_softc(brdev); 1393 1394 DPRINTF(("pccbb_pcic_socket_disable\n")); 1395 1396 /* reset signal asserting... */ 1397 PCIC_MASK(sc, PCIC_INTR, & ~PCIC_INTR_RESET); 1398 DELAY(2*1000); 1399 1400 /* power down the socket */ 1401 pccbb_power(brdev, CARD_VCC_0V | CARD_VPP_0V); 1402 PCIC_MASK(sc, PCIC_PWRCTL, &~PCIC_PWRCTL_OE); 1403 1404 /* wait 300ms until power fails (Tpf). */ 1405 DELAY(300 * 1000); 1406 } 1407 1408 /************************************************************************/ 1409 /* PC Card Resource Functions */ 1410 /************************************************************************/ 1411 1412 static void 1413 pccbb_pcic_wait_ready(struct pccbb_softc *sc) 1414 { 1415 int i; 1416 DEVPRINTF((sc->sc_dev, "pccbb_pcic_wait_ready: status 0x%02x\n", 1417 PCIC_READ(sc, PCIC_IF_STATUS))); 1418 for (i = 0; i < 10000; i++) { 1419 if (PCIC_READ(sc, PCIC_IF_STATUS) & PCIC_IF_STATUS_READY) { 1420 return; 1421 } 1422 DELAY(500); 1423 } 1424 device_printf(sc->sc_dev, "ready never happened, status = %02x\n", 1425 PCIC_READ(sc, PCIC_IF_STATUS)); 1426 } 1427 1428 #define PCIC_MEMINFO(NUM) { \ 1429 PCIC_SYSMEM_ADDR ## NUM ## _START_LSB, \ 1430 PCIC_SYSMEM_ADDR ## NUM ## _START_MSB, \ 1431 PCIC_SYSMEM_ADDR ## NUM ## _STOP_LSB, \ 1432 PCIC_SYSMEM_ADDR ## NUM ## _STOP_MSB, \ 1433 PCIC_SYSMEM_ADDR ## NUM ## _WIN, \ 1434 PCIC_CARDMEM_ADDR ## NUM ## _LSB, \ 1435 PCIC_CARDMEM_ADDR ## NUM ## _MSB, \ 1436 PCIC_ADDRWIN_ENABLE_MEM ## NUM ##, \ 1437 } 1438 1439 static struct mem_map_index_st { 1440 int sysmem_start_lsb; 1441 int sysmem_start_msb; 1442 int sysmem_stop_lsb; 1443 int sysmem_stop_msb; 1444 int sysmem_win; 1445 int cardmem_lsb; 1446 int cardmem_msb; 1447 int memenable; 1448 } mem_map_index[] = { 1449 PCIC_MEMINFO(0), 1450 PCIC_MEMINFO(1), 1451 PCIC_MEMINFO(2), 1452 PCIC_MEMINFO(3), 1453 PCIC_MEMINFO(4), 1454 }; 1455 #undef PCIC_MEMINFO 1456 1457 static void 1458 pccbb_pcic_do_mem_map(struct pccbb_softc *sc, int win) 1459 { 1460 PCIC_WRITE(sc, mem_map_index[win].sysmem_start_lsb, 1461 (sc->mem[win].addr >> PCIC_SYSMEM_ADDRX_SHIFT) & 0xff); 1462 PCIC_WRITE(sc, mem_map_index[win].sysmem_start_msb, 1463 ((sc->mem[win].addr >> (PCIC_SYSMEM_ADDRX_SHIFT + 8)) & 1464 PCIC_SYSMEM_ADDRX_START_MSB_ADDR_MASK) | 0x80); 1465 1466 PCIC_WRITE(sc, mem_map_index[win].sysmem_stop_lsb, 1467 ((sc->mem[win].addr + sc->mem[win].realsize - 1) >> 1468 PCIC_SYSMEM_ADDRX_SHIFT) & 0xff); 1469 PCIC_WRITE(sc, mem_map_index[win].sysmem_stop_msb, 1470 (((sc->mem[win].addr + sc->mem[win].realsize - 1) >> 1471 (PCIC_SYSMEM_ADDRX_SHIFT + 8)) & 1472 PCIC_SYSMEM_ADDRX_STOP_MSB_ADDR_MASK) | 1473 PCIC_SYSMEM_ADDRX_STOP_MSB_WAIT2); 1474 1475 PCIC_WRITE(sc, mem_map_index[win].sysmem_win, 1476 (sc->mem[win].addr >> PCIC_MEMREG_WIN_SHIFT) & 0xff); 1477 1478 PCIC_WRITE(sc, mem_map_index[win].cardmem_lsb, 1479 (sc->mem[win].offset >> PCIC_CARDMEM_ADDRX_SHIFT) & 0xff); 1480 PCIC_WRITE(sc, mem_map_index[win].cardmem_msb, 1481 ((sc->mem[win].offset >> (PCIC_CARDMEM_ADDRX_SHIFT + 8)) & 1482 PCIC_CARDMEM_ADDRX_MSB_ADDR_MASK) | 1483 ((sc->mem[win].kind == PCCARD_MEM_ATTR) ? 1484 PCIC_CARDMEM_ADDRX_MSB_REGACTIVE_ATTR : 0)); 1485 1486 PCIC_MASK(sc, PCIC_ADDRWIN_ENABLE, | PCIC_ADDRWIN_ENABLE_MEMCS16 | 1487 mem_map_index[win].memenable); 1488 1489 DELAY(100); 1490 1491 #ifdef CBB_DEBUG 1492 { 1493 int r1, r2, r3, r4, r5, r6, r7; 1494 r1 = PCIC_READ(sc, mem_map_index[win].sysmem_start_msb); 1495 r2 = PCIC_READ(sc, mem_map_index[win].sysmem_start_lsb); 1496 r3 = PCIC_READ(sc, mem_map_index[win].sysmem_stop_msb); 1497 r4 = PCIC_READ(sc, mem_map_index[win].sysmem_stop_lsb); 1498 r5 = PCIC_READ(sc, mem_map_index[win].cardmem_msb); 1499 r6 = PCIC_READ(sc, mem_map_index[win].cardmem_lsb); 1500 r7 = PCIC_READ(sc, mem_map_index[win].sysmem_win); 1501 DPRINTF(("pccbb_pcic_do_mem_map window %d: %02x%02x %02x%02x " 1502 "%02x%02x %02x (%08x+%08x.%08x*%08lx)\n", 1503 win, r1, r2, r3, r4, r5, r6, r7, 1504 sc->mem[win].addr, sc->mem[win].size, sc->mem[win].realsize, 1505 sc->mem[win].offset)); 1506 } 1507 #endif 1508 } 1509 1510 static int 1511 pccbb_pcic_mem_map(struct pccbb_softc *sc, int kind, struct resource *res) 1512 { 1513 int win; 1514 struct pccbb_reslist *rle; 1515 bus_addr_t card_addr; 1516 1517 for (win = 0; win < PCIC_MEM_WINS; win++) { 1518 if ((sc->memalloc & (1 << win)) == 0) { 1519 sc->memalloc |= (1 << win); 1520 break; 1521 } 1522 } 1523 if (win >= PCIC_MEM_WINS) 1524 return (1); 1525 1526 SLIST_FOREACH(rle, &sc->rl, link) { 1527 if (rle->res == res) 1528 break; 1529 } 1530 if (!rle) { 1531 device_printf(sc->sc_dev, 1532 "pcic_map_mem: Memory resource not found\n"); 1533 return ENXIO; 1534 } 1535 card_addr = rle->cardaddr - rle->cardaddr % PCIC_MEM_PAGESIZE; 1536 sc->mem[win].memt = rman_get_bustag(res); 1537 sc->mem[win].memh = rman_get_bushandle(res); 1538 sc->mem[win].addr = rman_get_start(res); 1539 sc->mem[win].size = rman_get_end(res) - sc->mem[win].addr + 1; 1540 sc->mem[win].realsize = sc->mem[win].size + PCIC_MEM_PAGESIZE - 1; 1541 sc->mem[win].realsize = sc->mem[win].realsize - 1542 (sc->mem[win].realsize % PCIC_MEM_PAGESIZE); 1543 sc->mem[win].offset = ((long)card_addr) - 1544 ((long)(sc->mem[win].addr)); 1545 sc->mem[win].kind = kind; 1546 1547 DPRINTF(("pccbb_pcic_mem_map window %d bus %x+%x+%lx card addr %x\n", 1548 win, sc->mem[win].addr, sc->mem[win].size, 1549 sc->mem[win].offset, card_addr)); 1550 1551 pccbb_pcic_do_mem_map(sc, win); 1552 1553 return (0); 1554 } 1555 1556 static void 1557 pccbb_pcic_mem_unmap(struct pccbb_softc *sc, int window) 1558 { 1559 if (window >= PCIC_MEM_WINS) 1560 panic("pccbb_pcic_mem_unmap: window out of range"); 1561 1562 PCIC_MASK(sc, PCIC_ADDRWIN_ENABLE, & ~mem_map_index[window].memenable); 1563 sc->memalloc &= ~(1 << window); 1564 } 1565 1566 static int 1567 pccbb_pcic_mem_findmap(struct pccbb_softc *sc, struct resource *res) 1568 { 1569 int win; 1570 1571 for (win = 0; win < PCIC_MEM_WINS; win++) { 1572 if (sc->mem[win].memt == rman_get_bustag(res) && 1573 sc->mem[win].addr == rman_get_start(res) && 1574 sc->mem[win].size == rman_get_size(res)) 1575 return win; 1576 } 1577 device_printf(sc->sc_dev, "Memory map not found!\n"); 1578 return -1; 1579 } 1580 1581 #define PCIC_IOINFO(NUM) { \ 1582 PCIC_IOADDR ## NUM ## _START_LSB, \ 1583 PCIC_IOADDR ## NUM ## _START_MSB, \ 1584 PCIC_IOADDR ## NUM ## _STOP_LSB, \ 1585 PCIC_IOADDR ## NUM ## _STOP_MSB, \ 1586 PCIC_ADDRWIN_ENABLE_IO ## NUM ##, \ 1587 PCIC_IOCTL_IO ## NUM ## _WAITSTATE \ 1588 | PCIC_IOCTL_IO ## NUM ## _ZEROWAIT \ 1589 | PCIC_IOCTL_IO ## NUM ## _IOCS16SRC_MASK \ 1590 | PCIC_IOCTL_IO ## NUM ## _DATASIZE_MASK, \ 1591 { \ 1592 PCIC_IOCTL_IO ## NUM ## _IOCS16SRC_CARD, \ 1593 PCIC_IOCTL_IO ## NUM ## _IOCS16SRC_DATASIZE \ 1594 | PCIC_IOCTL_IO ## NUM ## _DATASIZE_8BIT, \ 1595 PCIC_IOCTL_IO ## NUM ## _IOCS16SRC_DATASIZE \ 1596 | PCIC_IOCTL_IO ## NUM ## _DATASIZE_16BIT, \ 1597 } \ 1598 } 1599 1600 static struct io_map_index_st { 1601 int start_lsb; 1602 int start_msb; 1603 int stop_lsb; 1604 int stop_msb; 1605 int ioenable; 1606 int ioctlmask; 1607 int ioctlbits[3]; /* indexed by PCCARD_WIDTH_* */ 1608 } io_map_index[] = { 1609 PCIC_IOINFO(0), 1610 PCIC_IOINFO(1), 1611 }; 1612 #undef PCIC_IOINFO 1613 1614 static void 1615 pccbb_pcic_do_io_map(struct pccbb_softc *sc, int win) 1616 { 1617 PCIC_WRITE(sc, io_map_index[win].start_lsb, sc->io[win].addr & 0xff); 1618 PCIC_WRITE(sc, io_map_index[win].start_msb, 1619 (sc->io[win].addr >> 8) & 0xff); 1620 1621 PCIC_WRITE(sc, io_map_index[win].stop_lsb, 1622 (sc->io[win].addr + sc->io[win].size - 1) & 0xff); 1623 PCIC_WRITE(sc, io_map_index[win].stop_msb, 1624 ((sc->io[win].addr + sc->io[win].size - 1) >> 8) & 0xff); 1625 1626 PCIC_MASK2(sc, PCIC_IOCTL, 1627 & ~io_map_index[win].ioctlmask, 1628 | io_map_index[win].ioctlbits[sc->io[win].width]); 1629 1630 PCIC_MASK(sc, PCIC_ADDRWIN_ENABLE, | io_map_index[win].ioenable); 1631 #ifdef CBB_DEBUG 1632 { 1633 int r1, r2, r3, r4; 1634 r1 = PCIC_READ(sc, io_map_index[win].start_msb); 1635 r2 = PCIC_READ(sc, io_map_index[win].start_lsb); 1636 r3 = PCIC_READ(sc, io_map_index[win].stop_msb); 1637 r4 = PCIC_READ(sc, io_map_index[win].stop_lsb); 1638 DPRINTF(("pccbb_pcic_do_io_map window %d: %02x%02x %02x%02x " 1639 "(%08x+%08x)\n", win, r1, r2, r3, r4, 1640 sc->io[win].addr, sc->io[win].size)); 1641 } 1642 #endif 1643 } 1644 1645 static int 1646 pccbb_pcic_io_map(struct pccbb_softc *sc, int width, struct resource *r) 1647 { 1648 int win; 1649 #ifdef CBB_DEBUG 1650 static char *width_names[] = { "auto", "io8", "io16"}; 1651 #endif 1652 1653 for (win=0; win < PCIC_IO_WINS; win++) { 1654 if ((sc->ioalloc & (1 << win)) == 0) { 1655 sc->ioalloc |= (1 << win); 1656 break; 1657 } 1658 } 1659 if (win >= PCIC_IO_WINS) 1660 return (1); 1661 1662 sc->io[win].iot = rman_get_bustag(r); 1663 sc->io[win].ioh = rman_get_bushandle(r); 1664 sc->io[win].addr = rman_get_start(r); 1665 sc->io[win].size = rman_get_end(r) - sc->io[win].addr + 1; 1666 sc->io[win].flags = 0; 1667 sc->io[win].width = width; 1668 1669 DPRINTF(("pccbb_pcic_io_map window %d %s port %x+%x\n", 1670 win, width_names[width], sc->io[win].addr, 1671 sc->io[win].size)); 1672 1673 pccbb_pcic_do_io_map(sc, win); 1674 1675 return (0); 1676 } 1677 1678 static void 1679 pccbb_pcic_io_unmap(struct pccbb_softc *sc, int window) 1680 { 1681 if (window >= PCIC_IO_WINS) 1682 panic("pccbb_pcic_io_unmap: window out of range"); 1683 1684 PCIC_MASK(sc, PCIC_ADDRWIN_ENABLE, & ~io_map_index[window].ioenable); 1685 1686 sc->ioalloc &= ~(1 << window); 1687 1688 sc->io[window].iot = 0; 1689 sc->io[window].ioh = 0; 1690 sc->io[window].addr = 0; 1691 sc->io[window].size = 0; 1692 sc->io[window].flags = 0; 1693 sc->io[window].width = 0; 1694 } 1695 1696 static int 1697 pccbb_pcic_io_findmap(struct pccbb_softc *sc, struct resource *res) 1698 { 1699 int win; 1700 1701 for (win = 0; win < PCIC_IO_WINS; win++) { 1702 if (sc->io[win].iot == rman_get_bustag(res) && 1703 sc->io[win].addr == rman_get_start(res) && 1704 sc->io[win].size == rman_get_size(res)) 1705 return win; 1706 } 1707 device_printf(sc->sc_dev, "IO map not found!\n"); 1708 return -1; 1709 } 1710 1711 static int 1712 pccbb_pcic_activate_resource(device_t brdev, device_t child, int type, int rid, 1713 struct resource *res) 1714 { 1715 int err; 1716 struct pccbb_softc *sc = device_get_softc(brdev); 1717 if (!(rman_get_flags(res) & RF_ACTIVE)) { /* not already activated */ 1718 switch (type) { 1719 case SYS_RES_IOPORT: 1720 err = pccbb_pcic_io_map(sc, 0, res); 1721 break; 1722 case SYS_RES_MEMORY: 1723 err = pccbb_pcic_mem_map(sc, 0, res); 1724 break; 1725 default: 1726 err = 0; 1727 break; 1728 } 1729 if (err) 1730 return err; 1731 1732 } 1733 return BUS_ACTIVATE_RESOURCE(device_get_parent(brdev), child, 1734 type, rid, res); 1735 } 1736 1737 static int 1738 pccbb_pcic_deactivate_resource(device_t brdev, device_t child, int type, 1739 int rid, struct resource *res) 1740 { 1741 struct pccbb_softc *sc = device_get_softc(brdev); 1742 int win; 1743 1744 if (rman_get_flags(res) & RF_ACTIVE) { /* if activated */ 1745 switch (type) { 1746 case SYS_RES_IOPORT: 1747 win = pccbb_pcic_io_findmap(sc, res); 1748 if (win >= 0) 1749 pccbb_pcic_io_unmap(sc, win); 1750 else 1751 return ENOENT; 1752 break; 1753 case SYS_RES_MEMORY: 1754 win = pccbb_pcic_mem_findmap(sc, res); 1755 if (win >= 0) 1756 pccbb_pcic_mem_unmap(sc, win); 1757 else 1758 return ENOENT; 1759 break; 1760 } 1761 } 1762 return BUS_DEACTIVATE_RESOURCE(device_get_parent(brdev), child, 1763 type, rid, res); 1764 } 1765 1766 static struct resource * 1767 pccbb_pcic_alloc_resource(device_t brdev, device_t child, int type, int *rid, 1768 u_long start, u_long end, u_long count, u_int flags) 1769 { 1770 struct resource *res = NULL; 1771 struct pccbb_softc *sc = device_get_softc(brdev); 1772 struct pccbb_reslist *rle; 1773 int tmp; 1774 1775 if ((sc->sc_flags & PCCBB_PCIC_MEM_32) == 0) { 1776 /* XXX: how do we do this? */ 1777 panic("PCCBB bridge cannot handle non MEM_32 bridges\n"); 1778 } 1779 1780 switch (type) { 1781 case SYS_RES_MEMORY: 1782 if (start < 0x10000000) 1783 start = 0x10000000; /* XXX tweakable? */ 1784 if (end < start) 1785 end = start; 1786 flags = (flags & ~RF_ALIGNMENT_MASK) | 1787 rman_make_alignment_flags(PCCBB_MEMALIGN); 1788 break; 1789 case SYS_RES_IOPORT: 1790 if (start < 0x100) 1791 start = 0x100; /* XXX tweakable? */ 1792 if (end < start) 1793 end = start; 1794 break; 1795 case SYS_RES_IRQ: 1796 tmp = rman_get_start(sc->sc_irq_res); 1797 if (start > tmp || end < tmp || count != 1) { 1798 device_printf(child, "requested interrupt %ld-%ld," 1799 "count = %ld not supported by pccbb\n", 1800 start, end, count); 1801 return NULL; 1802 } 1803 flags |= RF_SHAREABLE; 1804 start = end = rman_get_start(sc->sc_irq_res); 1805 break; 1806 } 1807 res = BUS_ALLOC_RESOURCE(device_get_parent(brdev), child, type, rid, 1808 start, end, count, flags & ~RF_ACTIVE); 1809 if (res == NULL) 1810 return NULL; 1811 1812 rle = malloc(sizeof(struct pccbb_reslist), M_DEVBUF, M_NOWAIT); 1813 if (!rle) 1814 panic("pccbb_pcic_alloc_resource: can't record entry!"); 1815 rle->res = res; 1816 rle->type = type; 1817 rle->rid = *rid; 1818 rle->cardaddr = 0; 1819 SLIST_INSERT_HEAD(&sc->rl, rle, link); 1820 1821 if (flags & RF_ACTIVE) { 1822 if (bus_activate_resource(child, type, *rid, res) != 0) { 1823 bus_release_resource(child, type, *rid, res); 1824 return NULL; 1825 } 1826 } 1827 1828 return res; 1829 } 1830 1831 static int 1832 pccbb_pcic_release_resource(device_t brdev, device_t child, int type, 1833 int rid, struct resource *res) 1834 { 1835 struct pccbb_softc *sc = device_get_softc(brdev); 1836 struct pccbb_reslist *rle; 1837 1838 if (rman_get_flags(res) & RF_ACTIVE) { 1839 int error; 1840 error = bus_deactivate_resource(child, type, rid, res); 1841 if (error != 0) 1842 return error; 1843 } 1844 1845 SLIST_FOREACH(rle, &sc->rl, link) { 1846 if (rle->res == res) { 1847 SLIST_REMOVE(&sc->rl, rle, pccbb_reslist, link); 1848 free(rle, M_DEVBUF); 1849 break; 1850 } 1851 } 1852 1853 return BUS_RELEASE_RESOURCE(device_get_parent(brdev), child, 1854 type, rid, res); 1855 } 1856 1857 /************************************************************************/ 1858 /* PC Card methods */ 1859 /************************************************************************/ 1860 1861 static int 1862 pccbb_pcic_set_res_flags(device_t brdev, device_t child, int type, int rid, 1863 u_int32_t flags) 1864 { 1865 struct pccbb_softc *sc = device_get_softc(brdev); 1866 struct resource *res; 1867 struct pccbb_reslist *rle; 1868 int win; 1869 1870 res = NULL; 1871 if (type != SYS_RES_MEMORY) 1872 return (EINVAL); 1873 SLIST_FOREACH(rle, &sc->rl, link) { 1874 if (SYS_RES_MEMORY == rle->type && rid == rle->rid && 1875 child == rle->res->r_dev) { 1876 res = rle->res; 1877 break; 1878 } 1879 } 1880 1881 if (res == NULL) { 1882 device_printf(brdev, 1883 "set_res_flags: specified rid not found\n"); 1884 return ENOENT; 1885 } 1886 win = pccbb_pcic_mem_findmap(sc, res); 1887 if (win < 0) { 1888 device_printf(brdev, 1889 "set_res_flags: specified resource not active\n"); 1890 return ENOENT; 1891 } 1892 1893 sc->mem[win].kind = flags; 1894 pccbb_pcic_do_mem_map(sc, win); 1895 return 0; 1896 } 1897 1898 static int 1899 pccbb_pcic_set_memory_offset(device_t brdev, device_t child, int rid, 1900 u_int32_t cardaddr, u_int32_t *deltap) 1901 { 1902 struct pccbb_softc *sc = device_get_softc(brdev); 1903 int win; 1904 struct pccbb_reslist *rle; 1905 struct resource *res; 1906 u_int32_t delta; 1907 1908 win = -1; 1909 1910 res = NULL; 1911 SLIST_FOREACH(rle, &sc->rl, link) { 1912 if (SYS_RES_MEMORY == rle->type && rid == rle->rid && 1913 child == rle->res->r_dev) { 1914 res = rle->res; 1915 rle->cardaddr = cardaddr; 1916 break; 1917 } 1918 } 1919 1920 if (res == NULL) { 1921 device_printf(brdev, 1922 "set_memory_offset: specified rid not found\n"); 1923 return ENOENT; 1924 } 1925 win = pccbb_pcic_mem_findmap(sc, res); 1926 if (win < 0) { 1927 device_printf(brdev, 1928 "set_memory_offset: specified resource not active\n"); 1929 return ENOENT; 1930 } 1931 1932 delta = cardaddr % PCIC_MEM_PAGESIZE; 1933 if (deltap) 1934 *deltap = delta; 1935 cardaddr -= delta; 1936 sc->mem[win].realsize = sc->mem[win].size + delta + 1937 PCIC_MEM_PAGESIZE - 1; 1938 sc->mem[win].realsize = sc->mem[win].realsize - 1939 (sc->mem[win].realsize % PCIC_MEM_PAGESIZE); 1940 sc->mem[win].offset = cardaddr - sc->mem[win].addr; 1941 pccbb_pcic_do_mem_map(sc, win); 1942 1943 return 0; 1944 } 1945 1946 /************************************************************************/ 1947 /* POWER methods */ 1948 /************************************************************************/ 1949 1950 static int 1951 pccbb_power_enable_socket(device_t brdev, device_t child) 1952 { 1953 struct pccbb_softc *sc = device_get_softc(brdev); 1954 1955 if (sc->sc_flags & PCCBB_16BIT_CARD) 1956 return pccbb_pcic_power_enable_socket(brdev, child); 1957 else 1958 return pccbb_cardbus_power_enable_socket(brdev, child); 1959 } 1960 1961 static void 1962 pccbb_power_disable_socket(device_t brdev, device_t child) 1963 { 1964 struct pccbb_softc *sc = device_get_softc(brdev); 1965 if (sc->sc_flags & PCCBB_16BIT_CARD) 1966 pccbb_pcic_power_disable_socket(brdev, child); 1967 else 1968 pccbb_cardbus_power_disable_socket(brdev, child); 1969 } 1970 /************************************************************************/ 1971 /* BUS Methods */ 1972 /************************************************************************/ 1973 1974 1975 static int 1976 pccbb_activate_resource(device_t brdev, device_t child, int type, int rid, 1977 struct resource *r) 1978 { 1979 struct pccbb_softc *sc = device_get_softc(brdev); 1980 1981 if (sc->sc_flags & PCCBB_16BIT_CARD) 1982 return pccbb_pcic_activate_resource(brdev, child, type, rid, r); 1983 else 1984 return pccbb_cardbus_activate_resource(brdev, child, type, rid, 1985 r); 1986 } 1987 1988 static int 1989 pccbb_deactivate_resource(device_t brdev, device_t child, int type, 1990 int rid, struct resource *r) 1991 { 1992 struct pccbb_softc *sc = device_get_softc(brdev); 1993 1994 if (sc->sc_flags & PCCBB_16BIT_CARD) 1995 return pccbb_pcic_deactivate_resource(brdev, child, type, 1996 rid, r); 1997 else 1998 return pccbb_cardbus_deactivate_resource(brdev, child, type, 1999 rid, r); 2000 } 2001 2002 static struct resource * 2003 pccbb_alloc_resource(device_t brdev, device_t child, int type, int *rid, 2004 u_long start, u_long end, u_long count, u_int flags) 2005 { 2006 struct pccbb_softc *sc = device_get_softc(brdev); 2007 2008 if (sc->sc_flags & PCCBB_16BIT_CARD) 2009 return pccbb_pcic_alloc_resource(brdev, child, type, rid, 2010 start, end, count, flags); 2011 else 2012 return pccbb_cardbus_alloc_resource(brdev, child, type, rid, 2013 start, end, count, flags); 2014 } 2015 2016 static int 2017 pccbb_release_resource(device_t brdev, device_t child, int type, int rid, 2018 struct resource *r) 2019 { 2020 struct pccbb_softc *sc = device_get_softc(brdev); 2021 2022 if (sc->sc_flags & PCCBB_16BIT_CARD) 2023 return pccbb_pcic_release_resource(brdev, child, type, 2024 rid, r); 2025 else 2026 return pccbb_cardbus_release_resource(brdev, child, type, 2027 rid, r); 2028 } 2029 2030 static int 2031 pccbb_read_ivar(device_t brdev, device_t child, int which, uintptr_t *result) 2032 { 2033 struct pccbb_softc *sc = device_get_softc(brdev); 2034 2035 switch (which) { 2036 case PCIB_IVAR_BUS: 2037 *result = sc->sc_secbus; 2038 return(0); 2039 } 2040 return(ENOENT); 2041 } 2042 2043 static int 2044 pccbb_write_ivar(device_t brdev, device_t child, int which, uintptr_t value) 2045 { 2046 struct pccbb_softc *sc = device_get_softc(brdev); 2047 2048 switch (which) { 2049 case PCIB_IVAR_BUS: 2050 sc->sc_secbus = value; 2051 break; 2052 } 2053 return(ENOENT); 2054 } 2055 2056 /************************************************************************/ 2057 /* PCI compat methods */ 2058 /************************************************************************/ 2059 2060 static int 2061 pccbb_maxslots(device_t brdev) 2062 { 2063 return 0; 2064 } 2065 2066 static u_int32_t 2067 pccbb_read_config(device_t brdev, int b, int s, int f, int reg, int width) 2068 { 2069 /* 2070 * Pass through to the next ppb up the chain (i.e. our grandparent). 2071 */ 2072 return PCIB_READ_CONFIG(device_get_parent(device_get_parent(brdev)), 2073 b, s, f, reg, width); 2074 } 2075 2076 static void 2077 pccbb_write_config(device_t brdev, int b, int s, int f, int reg, u_int32_t val, 2078 int width) 2079 { 2080 /* 2081 * Pass through to the next ppb up the chain (i.e. our grandparent). 2082 */ 2083 PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(brdev)), 2084 b, s, f, reg, val, width); 2085 } 2086 2087 static device_method_t pccbb_methods[] = { 2088 /* Device interface */ 2089 DEVMETHOD(device_probe, pccbb_probe), 2090 DEVMETHOD(device_attach, pccbb_attach), 2091 DEVMETHOD(device_detach, pccbb_detach), 2092 DEVMETHOD(device_shutdown, pccbb_shutdown), 2093 DEVMETHOD(device_suspend, bus_generic_suspend), 2094 DEVMETHOD(device_resume, bus_generic_resume), 2095 2096 /* bus methods */ 2097 DEVMETHOD(bus_print_child, bus_generic_print_child), 2098 DEVMETHOD(bus_read_ivar, pccbb_read_ivar), 2099 DEVMETHOD(bus_write_ivar, pccbb_write_ivar), 2100 DEVMETHOD(bus_alloc_resource, pccbb_alloc_resource), 2101 DEVMETHOD(bus_release_resource, pccbb_release_resource), 2102 DEVMETHOD(bus_activate_resource, pccbb_activate_resource), 2103 DEVMETHOD(bus_deactivate_resource, pccbb_deactivate_resource), 2104 DEVMETHOD(bus_driver_added, pccbb_driver_added), 2105 DEVMETHOD(bus_child_detached, pccbb_child_detached), 2106 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 2107 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 2108 2109 /* 16-bit card interface */ 2110 DEVMETHOD(card_set_res_flags, pccbb_pcic_set_res_flags), 2111 DEVMETHOD(card_set_memory_offset, pccbb_pcic_set_memory_offset), 2112 DEVMETHOD(card_reprobe_card, pccbb_card_reprobe), 2113 2114 /* power interface */ 2115 DEVMETHOD(power_enable_socket, pccbb_power_enable_socket), 2116 DEVMETHOD(power_disable_socket, pccbb_power_disable_socket), 2117 2118 /* pcib compatibility interface */ 2119 DEVMETHOD(pcib_maxslots, pccbb_maxslots), 2120 DEVMETHOD(pcib_read_config, pccbb_read_config), 2121 DEVMETHOD(pcib_write_config, pccbb_write_config), 2122 {0,0} 2123 }; 2124 2125 static driver_t pccbb_driver = { 2126 "pccbb", 2127 pccbb_methods, 2128 sizeof(struct pccbb_softc) 2129 }; 2130 2131 static devclass_t pccbb_devclass; 2132 2133 DRIVER_MODULE(pccbb, pci, pccbb_driver, pccbb_devclass, 0, 0); 2134 2135 SYSINIT(pccbb, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, pccbb_start_threads, 0); 2136