1 /* $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-4-Clause 5 * 6 * Copyright (c) 1997 Marc Horowitz. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Marc Horowitz. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/malloc.h> 40 #include <sys/module.h> 41 #include <sys/kernel.h> 42 #include <sys/queue.h> 43 #include <sys/sbuf.h> 44 #include <sys/sysctl.h> 45 #include <sys/types.h> 46 47 #include <sys/bus.h> 48 #include <machine/bus.h> 49 #include <sys/rman.h> 50 #include <machine/resource.h> 51 52 #include <net/ethernet.h> 53 54 #include <dev/pccard/pccardreg.h> 55 #include <dev/pccard/pccardvar.h> 56 #include <dev/pccard/pccardvarp.h> 57 #include <dev/pccard/pccard_cis.h> 58 59 #include "power_if.h" 60 #include "card_if.h" 61 62 #define PCCARDDEBUG 63 64 /* sysctl vars */ 65 static SYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters"); 66 67 int pccard_debug = 0; 68 SYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RWTUN, 69 &pccard_debug, 0, 70 "pccard debug"); 71 72 int pccard_cis_debug = 0; 73 SYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RWTUN, 74 &pccard_cis_debug, 0, "pccard CIS debug"); 75 76 #ifdef PCCARDDEBUG 77 #define DPRINTF(arg) if (pccard_debug) printf arg 78 #define DEVPRINTF(arg) if (pccard_debug) device_printf arg 79 #define PRVERBOSE(arg) printf arg 80 #define DEVPRVERBOSE(arg) device_printf arg 81 #else 82 #define DPRINTF(arg) 83 #define DEVPRINTF(arg) 84 #define PRVERBOSE(arg) if (bootverbose) printf arg 85 #define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg 86 #endif 87 88 static int pccard_ccr_read(struct pccard_function *pf, int ccr); 89 static void pccard_ccr_write(struct pccard_function *pf, int ccr, int val); 90 static int pccard_attach_card(device_t dev); 91 static int pccard_detach_card(device_t dev); 92 static void pccard_function_init(struct pccard_function *pf, int entry); 93 static void pccard_function_free(struct pccard_function *pf); 94 static int pccard_function_enable(struct pccard_function *pf); 95 static void pccard_function_disable(struct pccard_function *pf); 96 static int pccard_probe(device_t dev); 97 static int pccard_attach(device_t dev); 98 static int pccard_detach(device_t dev); 99 static void pccard_print_resources(struct resource_list *rl, 100 const char *name, int type, int count, const char *format); 101 static int pccard_print_child(device_t dev, device_t child); 102 static int pccard_set_resource(device_t dev, device_t child, int type, 103 int rid, rman_res_t start, rman_res_t count); 104 static int pccard_get_resource(device_t dev, device_t child, int type, 105 int rid, rman_res_t *startp, rman_res_t *countp); 106 static void pccard_delete_resource(device_t dev, device_t child, int type, 107 int rid); 108 static int pccard_set_res_flags(device_t dev, device_t child, int type, 109 int rid, u_long flags); 110 static int pccard_set_memory_offset(device_t dev, device_t child, int rid, 111 uint32_t offset, uint32_t *deltap); 112 static int pccard_probe_and_attach_child(device_t dev, device_t child, 113 struct pccard_function *pf); 114 static void pccard_probe_nomatch(device_t cbdev, device_t child); 115 static int pccard_read_ivar(device_t bus, device_t child, int which, 116 uintptr_t *result); 117 static void pccard_driver_added(device_t dev, driver_t *driver); 118 static struct resource *pccard_alloc_resource(device_t dev, 119 device_t child, int type, int *rid, rman_res_t start, 120 rman_res_t end, rman_res_t count, u_int flags); 121 static int pccard_release_resource(device_t dev, device_t child, int type, 122 int rid, struct resource *r); 123 static void pccard_child_detached(device_t parent, device_t dev); 124 static int pccard_filter(void *arg); 125 static void pccard_intr(void *arg); 126 static int pccard_setup_intr(device_t dev, device_t child, 127 struct resource *irq, int flags, driver_filter_t *filt, 128 driver_intr_t *intr, void *arg, void **cookiep); 129 static int pccard_teardown_intr(device_t dev, device_t child, 130 struct resource *r, void *cookie); 131 132 static const struct pccard_product * 133 pccard_do_product_lookup(device_t bus, device_t dev, 134 const struct pccard_product *tab, size_t ent_size, 135 pccard_product_match_fn matchfn); 136 137 138 static int 139 pccard_ccr_read(struct pccard_function *pf, int ccr) 140 { 141 return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 142 pf->pf_ccr_offset + ccr)); 143 } 144 145 static void 146 pccard_ccr_write(struct pccard_function *pf, int ccr, int val) 147 { 148 if ((pf->ccr_mask) & (1 << (ccr / 2))) { 149 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 150 pf->pf_ccr_offset + ccr, val); 151 } 152 } 153 154 static int 155 pccard_set_default_descr(device_t dev) 156 { 157 const char *vendorstr, *prodstr; 158 uint32_t vendor, prod; 159 char *str; 160 161 if (pccard_get_vendor_str(dev, &vendorstr)) 162 return (0); 163 if (pccard_get_product_str(dev, &prodstr)) 164 return (0); 165 if (vendorstr != NULL && prodstr != NULL) { 166 str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF, 167 M_WAITOK); 168 sprintf(str, "%s %s", vendorstr, prodstr); 169 device_set_desc_copy(dev, str); 170 free(str, M_DEVBUF); 171 } else { 172 if (pccard_get_vendor(dev, &vendor)) 173 return (0); 174 if (pccard_get_product(dev, &prod)) 175 return (0); 176 str = malloc(100, M_DEVBUF, M_WAITOK); 177 snprintf(str, 100, "vendor=%#x product=%#x", vendor, prod); 178 device_set_desc_copy(dev, str); 179 free(str, M_DEVBUF); 180 } 181 return (0); 182 } 183 184 static int 185 pccard_attach_card(device_t dev) 186 { 187 struct pccard_softc *sc = PCCARD_SOFTC(dev); 188 struct pccard_function *pf; 189 struct pccard_ivar *ivar; 190 device_t child; 191 int i; 192 193 if (!STAILQ_EMPTY(&sc->card.pf_head)) { 194 if (bootverbose || pccard_debug) 195 device_printf(dev, "Card already inserted.\n"); 196 } 197 198 DEVPRINTF((dev, "chip_socket_enable\n")); 199 POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 200 201 DEVPRINTF((dev, "read_cis\n")); 202 pccard_read_cis(sc); 203 204 DEVPRINTF((dev, "check_cis_quirks\n")); 205 pccard_check_cis_quirks(dev); 206 207 /* 208 * bail now if the card has no functions, or if there was an error in 209 * the cis. 210 */ 211 212 if (sc->card.error) { 213 device_printf(dev, "CARD ERROR!\n"); 214 return (1); 215 } 216 if (STAILQ_EMPTY(&sc->card.pf_head)) { 217 device_printf(dev, "Card has no functions!\n"); 218 return (1); 219 } 220 221 if (bootverbose || pccard_debug) 222 pccard_print_cis(dev); 223 224 DEVPRINTF((dev, "functions scanning\n")); 225 i = -1; 226 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 227 i++; 228 if (STAILQ_EMPTY(&pf->cfe_head)) { 229 device_printf(dev, 230 "Function %d has no config entries.!\n", i); 231 continue; 232 } 233 pf->sc = sc; 234 pf->cfe = NULL; 235 pf->dev = NULL; 236 } 237 DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1, 238 pccard_mfc(sc))); 239 240 mtx_lock(&Giant); 241 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 242 if (STAILQ_EMPTY(&pf->cfe_head)) 243 continue; 244 ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, 245 M_WAITOK | M_ZERO); 246 resource_list_init(&ivar->resources); 247 child = device_add_child(dev, NULL, -1); 248 device_set_ivars(child, ivar); 249 ivar->pf = pf; 250 pf->dev = child; 251 pccard_probe_and_attach_child(dev, child, pf); 252 } 253 mtx_unlock(&Giant); 254 return (0); 255 } 256 257 static int 258 pccard_probe_and_attach_child(device_t dev, device_t child, 259 struct pccard_function *pf) 260 { 261 struct pccard_softc *sc = PCCARD_SOFTC(dev); 262 int error; 263 264 /* 265 * In NetBSD, the drivers are responsible for activating each 266 * function of a card and selecting the config to use. In 267 * FreeBSD, all that's done automatically in the typical lazy 268 * way we do device resoruce allocation (except we pick the 269 * cfe up front). This is the biggest depature from the 270 * inherited NetBSD model, apart from the FreeBSD resource code. 271 * 272 * This seems to work well in practice for most cards. 273 * However, there are two cases that are problematic. If a 274 * driver wishes to pick and chose which config entry to use, 275 * then this method falls down. These are usually older 276 * cards. In addition, there are some cards that have 277 * multiple hardware units on the cards, but presents only one 278 * CIS chain. These cards are combination cards, but only one 279 * of these units can be on at a time. 280 * 281 * To overcome this limitation, while preserving the basic 282 * model, the probe routine can select a cfe and try to 283 * activate it. If that succeeds, then we'll keep track of 284 * and let that information persist until we attach the card. 285 * Probe routines that do this MUST return 0, and cannot 286 * participate in the bidding process for a device. This 287 * seems harsh until you realize that if a probe routine knows 288 * enough to override the cfe we pick, then chances are very 289 * very good that it is the only driver that could hope to 290 * cope with the card. Bidding is for generic drivers, and 291 * while some of them may also match, none of them will do 292 * configuration override. 293 */ 294 error = device_probe(child); 295 if (error != 0) 296 goto out; 297 pccard_function_init(pf, -1); 298 if (sc->sc_enabled_count == 0) 299 POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 300 if (pccard_function_enable(pf) == 0 && 301 pccard_set_default_descr(child) == 0 && 302 device_attach(child) == 0) { 303 DEVPRINTF((sc->dev, "function %d CCR at %d offset %#x " 304 "mask %#x: %#x %#x %#x %#x, %#x %#x %#x %#x, %#x\n", 305 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 306 pf->ccr_mask, pccard_ccr_read(pf, 0x00), 307 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 308 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 309 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 310 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 311 return (0); 312 } 313 error = ENXIO; 314 out:; 315 /* 316 * Probe may fail AND also try to select a cfe, if so, free 317 * it. This is how we do cfe override. Or the attach fails. 318 * Either way, we have to clean up. 319 */ 320 if (pf->cfe != NULL) 321 pccard_function_disable(pf); 322 pf->cfe = NULL; 323 pccard_function_free(pf); 324 return error; 325 } 326 327 static int 328 pccard_detach_card(device_t dev) 329 { 330 struct pccard_softc *sc = PCCARD_SOFTC(dev); 331 struct pccard_function *pf; 332 struct pccard_config_entry *cfe; 333 struct pccard_ivar *devi; 334 int state; 335 336 /* 337 * We are running on either the PCCARD socket's event thread 338 * or in user context detaching a device by user request. 339 */ 340 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 341 if (pf->dev == NULL) 342 continue; 343 state = device_get_state(pf->dev); 344 if (state == DS_ATTACHED || state == DS_BUSY) 345 device_detach(pf->dev); 346 if (pf->cfe != NULL) 347 pccard_function_disable(pf); 348 pccard_function_free(pf); 349 devi = PCCARD_IVAR(pf->dev); 350 device_delete_child(dev, pf->dev); 351 free(devi, M_DEVBUF); 352 } 353 if (sc->sc_enabled_count == 0) 354 POWER_DISABLE_SOCKET(device_get_parent(dev), dev); 355 356 while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) { 357 while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) { 358 STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list); 359 free(cfe, M_DEVBUF); 360 } 361 STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list); 362 free(pf, M_DEVBUF); 363 } 364 STAILQ_INIT(&sc->card.pf_head); 365 return (0); 366 } 367 368 static const struct pccard_product * 369 pccard_do_product_lookup(device_t bus, device_t dev, 370 const struct pccard_product *tab, size_t ent_size, 371 pccard_product_match_fn matchfn) 372 { 373 const struct pccard_product *ent; 374 int matches; 375 uint32_t vendor; 376 uint32_t prod; 377 const char *vendorstr; 378 const char *prodstr; 379 const char *cis3str; 380 const char *cis4str; 381 382 #ifdef DIAGNOSTIC 383 if (sizeof *ent > ent_size) 384 panic("pccard_product_lookup: bogus ent_size %jd", 385 (intmax_t) ent_size); 386 #endif 387 if (pccard_get_vendor(dev, &vendor)) 388 return (NULL); 389 if (pccard_get_product(dev, &prod)) 390 return (NULL); 391 if (pccard_get_vendor_str(dev, &vendorstr)) 392 return (NULL); 393 if (pccard_get_product_str(dev, &prodstr)) 394 return (NULL); 395 if (pccard_get_cis3_str(dev, &cis3str)) 396 return (NULL); 397 if (pccard_get_cis4_str(dev, &cis4str)) 398 return (NULL); 399 for (ent = tab; ent->pp_vendor != 0; ent = 400 (const struct pccard_product *) ((const char *) ent + ent_size)) { 401 matches = 1; 402 if (ent->pp_vendor == PCCARD_VENDOR_ANY && 403 ent->pp_product == PCCARD_PRODUCT_ANY && 404 ent->pp_cis[0] == NULL && 405 ent->pp_cis[1] == NULL) { 406 if (ent->pp_name) 407 device_printf(dev, 408 "Total wildcard entry ignored for %s\n", 409 ent->pp_name); 410 continue; 411 } 412 if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY && 413 vendor != ent->pp_vendor) 414 matches = 0; 415 if (matches && ent->pp_product != PCCARD_PRODUCT_ANY && 416 prod != ent->pp_product) 417 matches = 0; 418 if (matches && ent->pp_cis[0] && 419 (vendorstr == NULL || 420 strcmp(ent->pp_cis[0], vendorstr) != 0)) 421 matches = 0; 422 if (matches && ent->pp_cis[1] && 423 (prodstr == NULL || 424 strcmp(ent->pp_cis[1], prodstr) != 0)) 425 matches = 0; 426 if (matches && ent->pp_cis[2] && 427 (cis3str == NULL || 428 strcmp(ent->pp_cis[2], cis3str) != 0)) 429 matches = 0; 430 if (matches && ent->pp_cis[3] && 431 (cis4str == NULL || 432 strcmp(ent->pp_cis[3], cis4str) != 0)) 433 matches = 0; 434 if (matchfn != NULL) 435 matches = (*matchfn)(dev, ent, matches); 436 if (matches) 437 return (ent); 438 } 439 return (NULL); 440 } 441 442 /** 443 * @brief pccard_select_cfe 444 * 445 * Select a cfe entry to use. Should be called from the pccard's probe 446 * routine after it knows for sure that it wants this card. 447 * 448 * XXX I think we need to make this symbol be static, ala the kobj stuff 449 * we do for everything else. This is a quick hack. 450 */ 451 int 452 pccard_select_cfe(device_t dev, int entry) 453 { 454 struct pccard_ivar *devi = PCCARD_IVAR(dev); 455 struct pccard_function *pf = devi->pf; 456 457 pccard_function_init(pf, entry); 458 return (pf->cfe ? 0 : ENOMEM); 459 } 460 461 /* 462 * Initialize a PCCARD function. May be called as long as the function is 463 * disabled. 464 * 465 * Note: pccard_function_init should not keep resources allocated. It should 466 * only set them up ala isa pnp, set the values in the rl lists, and return. 467 * Any resource held after pccard_function_init is called is a bug. However, 468 * the bus routines to get the resources also assume that pccard_function_init 469 * does this, so they need to be fixed too. 470 */ 471 static void 472 pccard_function_init(struct pccard_function *pf, int entry) 473 { 474 struct pccard_config_entry *cfe; 475 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 476 struct resource_list *rl = &devi->resources; 477 struct resource_list_entry *rle; 478 struct resource *r = NULL; 479 struct pccard_ce_iospace *ios; 480 struct pccard_ce_memspace *mems; 481 device_t bus; 482 rman_res_t start, end, len; 483 int i, rid, spaces; 484 485 if (pf->pf_flags & PFF_ENABLED) { 486 printf("pccard_function_init: function is enabled"); 487 return; 488 } 489 490 /* 491 * Driver probe routine requested a specific entry already 492 * that succeeded. 493 */ 494 if (pf->cfe != NULL) 495 return; 496 497 /* 498 * walk the list of configuration entries until we find one that 499 * we can allocate all the resources to. 500 */ 501 bus = device_get_parent(pf->dev); 502 STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) { 503 if (cfe->iftype != PCCARD_IFTYPE_IO) 504 continue; 505 if (entry != -1 && cfe->number != entry) 506 continue; 507 spaces = 0; 508 for (i = 0; i < cfe->num_iospace; i++) { 509 ios = cfe->iospace + i; 510 start = ios->start; 511 if (start) 512 end = start + ios->length - 1; 513 else 514 end = ~0; 515 DEVPRINTF((bus, "I/O rid %d start %#jx end %#jx\n", 516 i, start, end)); 517 rid = i; 518 len = ios->length; 519 r = bus_alloc_resource(bus, SYS_RES_IOPORT, &rid, 520 start, end, len, rman_make_alignment_flags(len)); 521 if (r == NULL) { 522 DEVPRINTF((bus, "I/O rid %d failed\n", i)); 523 goto not_this_one; 524 } 525 rle = resource_list_add(rl, SYS_RES_IOPORT, 526 rid, rman_get_start(r), rman_get_end(r), len); 527 if (rle == NULL) 528 panic("Cannot add resource rid %d IOPORT", rid); 529 rle->res = r; 530 spaces++; 531 } 532 for (i = 0; i < cfe->num_memspace; i++) { 533 mems = cfe->memspace + i; 534 start = mems->cardaddr + mems->hostaddr; 535 if (start) 536 end = start + mems->length - 1; 537 else 538 end = ~0; 539 DEVPRINTF((bus, "Memory rid %d start %#jx end %#jx\ncardaddr %#jx hostaddr %#jx length %#jx\n", 540 i, start, end, mems->cardaddr, mems->hostaddr, 541 mems->length)); 542 rid = i; 543 len = mems->length; 544 r = bus_alloc_resource(bus, SYS_RES_MEMORY, &rid, 545 start, end, len, rman_make_alignment_flags(len)); 546 if (r == NULL) { 547 DEVPRINTF((bus, "Memory rid %d failed\n", i)); 548 // goto not_this_one; 549 continue; 550 } 551 rle = resource_list_add(rl, SYS_RES_MEMORY, 552 rid, rman_get_start(r), rman_get_end(r), len); 553 if (rle == NULL) 554 panic("Cannot add resource rid %d MEM", rid); 555 rle->res = r; 556 spaces++; 557 } 558 if (spaces == 0) { 559 DEVPRINTF((bus, "Neither memory nor I/O mapped\n")); 560 goto not_this_one; 561 } 562 if (cfe->irqmask) { 563 rid = 0; 564 r = bus_alloc_resource_any(bus, SYS_RES_IRQ, &rid, 565 RF_SHAREABLE); 566 if (r == NULL) { 567 DEVPRINTF((bus, "IRQ rid %d failed\n", rid)); 568 goto not_this_one; 569 } 570 rle = resource_list_add(rl, SYS_RES_IRQ, rid, 571 rman_get_start(r), rman_get_end(r), 1); 572 if (rle == NULL) 573 panic("Cannot add resource rid %d IRQ", rid); 574 rle->res = r; 575 } 576 /* If we get to here, we've allocated all we need */ 577 pf->cfe = cfe; 578 break; 579 not_this_one:; 580 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n", 581 cfe->number)); 582 resource_list_purge(rl); 583 } 584 } 585 586 /* 587 * Free resources allocated by pccard_function_init(), May be called as long 588 * as the function is disabled. 589 * 590 * NOTE: This function should be unnecessary. pccard_function_init should 591 * never keep resources initialized. 592 */ 593 static void 594 pccard_function_free(struct pccard_function *pf) 595 { 596 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 597 struct resource_list_entry *rle; 598 599 if (pf->pf_flags & PFF_ENABLED) { 600 printf("pccard_function_free: function is enabled"); 601 return; 602 } 603 604 STAILQ_FOREACH(rle, &devi->resources, link) { 605 if (rle->res) { 606 if (rman_get_device(rle->res) != pf->sc->dev) 607 device_printf(pf->sc->dev, 608 "function_free: Resource still owned by " 609 "child, oops. " 610 "(type=%d, rid=%d, addr=%#jx)\n", 611 rle->type, rle->rid, 612 rman_get_start(rle->res)); 613 BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev), 614 pf->sc->dev, rle->type, rle->rid, rle->res); 615 rle->res = NULL; 616 } 617 } 618 resource_list_free(&devi->resources); 619 } 620 621 static void 622 pccard_mfc_adjust_iobase(struct pccard_function *pf, rman_res_t addr, 623 rman_res_t offset, rman_res_t size) 624 { 625 bus_size_t iosize, tmp; 626 627 if (addr != 0) { 628 if (pf->pf_mfc_iomax == 0) { 629 pf->pf_mfc_iobase = addr + offset; 630 pf->pf_mfc_iomax = pf->pf_mfc_iobase + size; 631 } else { 632 /* this makes the assumption that nothing overlaps */ 633 if (pf->pf_mfc_iobase > addr + offset) 634 pf->pf_mfc_iobase = addr + offset; 635 if (pf->pf_mfc_iomax < addr + offset + size) 636 pf->pf_mfc_iomax = addr + offset + size; 637 } 638 } 639 640 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase; 641 /* round up to nearest (2^n)-1 */ 642 for (iosize = 1; iosize < tmp; iosize <<= 1) 643 ; 644 iosize--; 645 646 DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n", 647 (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1))); 648 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0, 649 pf->pf_mfc_iobase & 0xff); 650 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1, 651 (pf->pf_mfc_iobase >> 8) & 0xff); 652 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0); 653 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0); 654 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize); 655 } 656 657 /* Enable a PCCARD function */ 658 static int 659 pccard_function_enable(struct pccard_function *pf) 660 { 661 struct pccard_function *tmp; 662 int reg; 663 device_t dev = pf->sc->dev; 664 665 if (pf->cfe == NULL) { 666 DEVPRVERBOSE((dev, "No config entry could be allocated.\n")); 667 return (ENOMEM); 668 } 669 670 if (pf->pf_flags & PFF_ENABLED) 671 return (0); 672 pf->sc->sc_enabled_count++; 673 674 /* 675 * it's possible for different functions' CCRs to be in the same 676 * underlying page. Check for that. 677 */ 678 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 679 if ((tmp->pf_flags & PFF_ENABLED) && 680 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 681 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 682 (tmp->ccr_base - tmp->pf_ccr_offset + 683 tmp->pf_ccr_realsize))) { 684 pf->pf_ccrt = tmp->pf_ccrt; 685 pf->pf_ccrh = tmp->pf_ccrh; 686 pf->pf_ccr_realsize = tmp->pf_ccr_realsize; 687 688 /* 689 * pf->pf_ccr_offset = (tmp->pf_ccr_offset - 690 * tmp->ccr_base) + pf->ccr_base; 691 */ 692 /* pf->pf_ccr_offset = 693 (tmp->pf_ccr_offset + pf->ccr_base) - 694 tmp->ccr_base; */ 695 pf->pf_ccr_window = tmp->pf_ccr_window; 696 break; 697 } 698 } 699 if (tmp == NULL) { 700 pf->ccr_rid = 0; 701 pf->ccr_res = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY, 702 &pf->ccr_rid, PCCARD_MEM_PAGE_SIZE, RF_ACTIVE); 703 if (!pf->ccr_res) 704 goto bad; 705 DEVPRINTF((dev, "ccr_res == %#jx-%#jx, base=%#x\n", 706 rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res), 707 pf->ccr_base)); 708 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, 709 pf->ccr_rid, PCCARD_A_MEM_ATTR); 710 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev, 711 pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset); 712 pf->pf_ccrt = rman_get_bustag(pf->ccr_res); 713 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res); 714 pf->pf_ccr_realsize = 1; 715 } 716 717 reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX); 718 reg |= PCCARD_CCR_OPTION_LEVIREQ; 719 if (pccard_mfc(pf->sc)) { 720 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE | 721 PCCARD_CCR_OPTION_ADDR_DECODE); 722 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */ 723 } 724 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg); 725 726 reg = 0; 727 if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0) 728 reg |= PCCARD_CCR_STATUS_IOIS8; 729 if (pf->cfe->flags & PCCARD_CFE_AUDIO) 730 reg |= PCCARD_CCR_STATUS_AUDIO; 731 pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg); 732 733 pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0); 734 735 if (pccard_mfc(pf->sc)) 736 pccard_mfc_adjust_iobase(pf, 0, 0, 0); 737 738 #ifdef PCCARDDEBUG 739 if (pccard_debug) { 740 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 741 device_printf(tmp->sc->dev, 742 "function %d CCR at %d offset %#x: " 743 "%#x %#x %#x %#x, %#x %#x %#x %#x, %#x\n", 744 tmp->number, tmp->pf_ccr_window, 745 tmp->pf_ccr_offset, 746 pccard_ccr_read(tmp, 0x00), 747 pccard_ccr_read(tmp, 0x02), 748 pccard_ccr_read(tmp, 0x04), 749 pccard_ccr_read(tmp, 0x06), 750 pccard_ccr_read(tmp, 0x0A), 751 pccard_ccr_read(tmp, 0x0C), 752 pccard_ccr_read(tmp, 0x0E), 753 pccard_ccr_read(tmp, 0x10), 754 pccard_ccr_read(tmp, 0x12)); 755 } 756 } 757 #endif 758 pf->pf_flags |= PFF_ENABLED; 759 return (0); 760 761 bad: 762 /* 763 * Decrement the reference count, and power down the socket, if 764 * necessary. 765 */ 766 pf->sc->sc_enabled_count--; 767 DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count)); 768 769 return (1); 770 } 771 772 /* Disable PCCARD function. */ 773 static void 774 pccard_function_disable(struct pccard_function *pf) 775 { 776 struct pccard_function *tmp; 777 device_t dev = pf->sc->dev; 778 779 if (pf->cfe == NULL) 780 panic("pccard_function_disable: function not initialized"); 781 782 if ((pf->pf_flags & PFF_ENABLED) == 0) 783 return; 784 if (pf->intr_handler != NULL) { 785 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 786 struct resource_list_entry *rle = 787 resource_list_find(&devi->resources, SYS_RES_IRQ, 0); 788 if (rle == NULL) 789 panic("Can't disable an interrupt with no IRQ res\n"); 790 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res, 791 pf->intr_handler_cookie); 792 } 793 794 /* 795 * it's possible for different functions' CCRs to be in the same 796 * underlying page. Check for that. Note we mark us as disabled 797 * first to avoid matching ourself. 798 */ 799 800 pf->pf_flags &= ~PFF_ENABLED; 801 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 802 if ((tmp->pf_flags & PFF_ENABLED) && 803 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 804 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 805 (tmp->ccr_base - tmp->pf_ccr_offset + 806 tmp->pf_ccr_realsize))) 807 break; 808 } 809 810 /* Not used by anyone else; unmap the CCR. */ 811 if (tmp == NULL) { 812 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid, 813 pf->ccr_res); 814 pf->ccr_res = NULL; 815 } 816 817 /* 818 * Decrement the reference count, and power down the socket, if 819 * necessary. 820 */ 821 pf->sc->sc_enabled_count--; 822 } 823 824 #define PCCARD_NPORT 2 825 #define PCCARD_NMEM 5 826 #define PCCARD_NIRQ 1 827 #define PCCARD_NDRQ 0 828 829 static int 830 pccard_probe(device_t dev) 831 { 832 device_set_desc(dev, "16-bit PCCard bus"); 833 return (0); 834 } 835 836 static int 837 pccard_attach(device_t dev) 838 { 839 struct pccard_softc *sc = PCCARD_SOFTC(dev); 840 int err; 841 842 sc->dev = dev; 843 sc->sc_enabled_count = 0; 844 if ((err = pccard_device_create(sc)) != 0) 845 return (err); 846 STAILQ_INIT(&sc->card.pf_head); 847 return (bus_generic_attach(dev)); 848 } 849 850 static int 851 pccard_detach(device_t dev) 852 { 853 pccard_detach_card(dev); 854 pccard_device_destroy(device_get_softc(dev)); 855 return (0); 856 } 857 858 static int 859 pccard_suspend(device_t self) 860 { 861 pccard_detach_card(self); 862 return (0); 863 } 864 865 static 866 int 867 pccard_resume(device_t self) 868 { 869 return (0); 870 } 871 872 static void 873 pccard_print_resources(struct resource_list *rl, const char *name, int type, 874 int count, const char *format) 875 { 876 struct resource_list_entry *rle; 877 int printed; 878 int i; 879 880 printed = 0; 881 for (i = 0; i < count; i++) { 882 rle = resource_list_find(rl, type, i); 883 if (rle != NULL) { 884 if (printed == 0) 885 printf(" %s ", name); 886 else if (printed > 0) 887 printf(","); 888 printed++; 889 printf(format, rle->start); 890 if (rle->count > 1) { 891 printf("-"); 892 printf(format, rle->start + rle->count - 1); 893 } 894 } else if (i > 3) { 895 /* check the first few regardless */ 896 break; 897 } 898 } 899 } 900 901 static int 902 pccard_print_child(device_t dev, device_t child) 903 { 904 struct pccard_ivar *devi = PCCARD_IVAR(child); 905 struct resource_list *rl = &devi->resources; 906 int retval = 0; 907 908 retval += bus_print_child_header(dev, child); 909 retval += printf(" at"); 910 911 if (devi != NULL) { 912 pccard_print_resources(rl, "port", SYS_RES_IOPORT, 913 PCCARD_NPORT, "%#lx"); 914 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY, 915 PCCARD_NMEM, "%#lx"); 916 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ, 917 "%ld"); 918 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ, 919 "%ld"); 920 retval += printf(" function %d config %d", devi->pf->number, 921 devi->pf->cfe->number); 922 } 923 924 retval += bus_print_child_footer(dev, child); 925 926 return (retval); 927 } 928 929 static int 930 pccard_set_resource(device_t dev, device_t child, int type, int rid, 931 rman_res_t start, rman_res_t count) 932 { 933 struct pccard_ivar *devi = PCCARD_IVAR(child); 934 struct resource_list *rl = &devi->resources; 935 936 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 937 && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 938 return (EINVAL); 939 if (rid < 0) 940 return (EINVAL); 941 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT) 942 return (EINVAL); 943 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM) 944 return (EINVAL); 945 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ) 946 return (EINVAL); 947 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ) 948 return (EINVAL); 949 950 resource_list_add(rl, type, rid, start, start + count - 1, count); 951 if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev, 952 type, &rid, start, start + count - 1, count, 0)) 953 return 0; 954 else 955 return ENOMEM; 956 } 957 958 static int 959 pccard_get_resource(device_t dev, device_t child, int type, int rid, 960 rman_res_t *startp, rman_res_t *countp) 961 { 962 struct pccard_ivar *devi = PCCARD_IVAR(child); 963 struct resource_list *rl = &devi->resources; 964 struct resource_list_entry *rle; 965 966 rle = resource_list_find(rl, type, rid); 967 if (rle == NULL) 968 return (ENOENT); 969 970 if (startp != NULL) 971 *startp = rle->start; 972 if (countp != NULL) 973 *countp = rle->count; 974 975 return (0); 976 } 977 978 static void 979 pccard_delete_resource(device_t dev, device_t child, int type, int rid) 980 { 981 struct pccard_ivar *devi = PCCARD_IVAR(child); 982 struct resource_list *rl = &devi->resources; 983 resource_list_delete(rl, type, rid); 984 } 985 986 static int 987 pccard_set_res_flags(device_t dev, device_t child, int type, int rid, 988 u_long flags) 989 { 990 return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type, 991 rid, flags)); 992 } 993 994 static int 995 pccard_set_memory_offset(device_t dev, device_t child, int rid, 996 uint32_t offset, uint32_t *deltap) 997 998 { 999 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid, 1000 offset, deltap)); 1001 } 1002 1003 static void 1004 pccard_probe_nomatch(device_t bus, device_t child) 1005 { 1006 struct pccard_ivar *devi = PCCARD_IVAR(child); 1007 struct pccard_function *pf = devi->pf; 1008 struct pccard_softc *sc = PCCARD_SOFTC(bus); 1009 int i; 1010 1011 device_printf(bus, "<unknown card>"); 1012 printf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) " 1013 "at function %d\n", sc->card.manufacturer, sc->card.product, 1014 pf->function, pf->number); 1015 device_printf(bus, " CIS info: "); 1016 for (i = 0; sc->card.cis1_info[i] != NULL && i < 4; i++) 1017 printf("%s%s", i > 0 ? ", " : "", sc->card.cis1_info[i]); 1018 printf("\n"); 1019 return; 1020 } 1021 1022 static int 1023 pccard_child_location_str(device_t bus, device_t child, char *buf, 1024 size_t buflen) 1025 { 1026 struct pccard_ivar *devi = PCCARD_IVAR(child); 1027 struct pccard_function *pf = devi->pf; 1028 1029 snprintf(buf, buflen, "function=%d", pf->number); 1030 return (0); 1031 } 1032 1033 static int 1034 pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf, 1035 size_t buflen) 1036 { 1037 struct pccard_ivar *devi = PCCARD_IVAR(child); 1038 struct pccard_function *pf = devi->pf; 1039 struct pccard_softc *sc = PCCARD_SOFTC(bus); 1040 struct sbuf sb; 1041 1042 sbuf_new(&sb, buf, buflen, SBUF_FIXEDLEN | SBUF_INCLUDENUL); 1043 sbuf_printf(&sb, "manufacturer=0x%04x product=0x%04x " 1044 "cisvendor=\"", sc->card.manufacturer, sc->card.product); 1045 devctl_safe_quote_sb(&sb, sc->card.cis1_info[0]); 1046 sbuf_printf(&sb, "\" cisproduct=\""); 1047 devctl_safe_quote_sb(&sb, sc->card.cis1_info[1]); 1048 sbuf_printf(&sb, "\" function_type=%d", pf->function); 1049 sbuf_finish(&sb); 1050 sbuf_delete(&sb); 1051 1052 return (0); 1053 } 1054 1055 static int 1056 pccard_read_ivar(device_t bus, device_t child, int which, uintptr_t *result) 1057 { 1058 struct pccard_ivar *devi = PCCARD_IVAR(child); 1059 struct pccard_function *pf = devi->pf; 1060 struct pccard_softc *sc = PCCARD_SOFTC(bus); 1061 1062 if (!pf) 1063 panic("No pccard function pointer"); 1064 switch (which) { 1065 default: 1066 return (EINVAL); 1067 case PCCARD_IVAR_FUNCE_DISK: 1068 *(uint16_t *)result = pf->pf_funce_disk_interface | 1069 (pf->pf_funce_disk_power << 8); 1070 break; 1071 case PCCARD_IVAR_ETHADDR: 1072 bcopy(pf->pf_funce_lan_nid, result, ETHER_ADDR_LEN); 1073 break; 1074 case PCCARD_IVAR_VENDOR: 1075 *(uint32_t *)result = sc->card.manufacturer; 1076 break; 1077 case PCCARD_IVAR_PRODUCT: 1078 *(uint32_t *)result = sc->card.product; 1079 break; 1080 case PCCARD_IVAR_PRODEXT: 1081 *(uint16_t *)result = sc->card.prodext; 1082 break; 1083 case PCCARD_IVAR_FUNCTION: 1084 *(uint32_t *)result = pf->function; 1085 break; 1086 case PCCARD_IVAR_FUNCTION_NUMBER: 1087 *(uint32_t *)result = pf->number; 1088 break; 1089 case PCCARD_IVAR_VENDOR_STR: 1090 *(const char **)result = sc->card.cis1_info[0]; 1091 break; 1092 case PCCARD_IVAR_PRODUCT_STR: 1093 *(const char **)result = sc->card.cis1_info[1]; 1094 break; 1095 case PCCARD_IVAR_CIS3_STR: 1096 *(const char **)result = sc->card.cis1_info[2]; 1097 break; 1098 case PCCARD_IVAR_CIS4_STR: 1099 *(const char **)result = sc->card.cis1_info[3]; 1100 break; 1101 } 1102 return (0); 1103 } 1104 1105 static void 1106 pccard_driver_added(device_t dev, driver_t *driver) 1107 { 1108 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1109 struct pccard_function *pf; 1110 device_t child; 1111 1112 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 1113 if (STAILQ_EMPTY(&pf->cfe_head)) 1114 continue; 1115 child = pf->dev; 1116 if (device_get_state(child) != DS_NOTPRESENT) 1117 continue; 1118 pccard_probe_and_attach_child(dev, child, pf); 1119 } 1120 return; 1121 } 1122 1123 static struct resource * 1124 pccard_alloc_resource(device_t dev, device_t child, int type, int *rid, 1125 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 1126 { 1127 struct pccard_ivar *dinfo; 1128 struct resource_list_entry *rle = NULL; 1129 int passthrough = (device_get_parent(child) != dev); 1130 int isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1); 1131 struct resource *r = NULL; 1132 1133 /* XXX I'm no longer sure this is right */ 1134 if (passthrough) { 1135 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 1136 type, rid, start, end, count, flags)); 1137 } 1138 1139 dinfo = device_get_ivars(child); 1140 rle = resource_list_find(&dinfo->resources, type, *rid); 1141 1142 if (rle == NULL && isdefault) 1143 return (NULL); /* no resource of that type/rid */ 1144 if (rle == NULL || rle->res == NULL) { 1145 /* XXX Need to adjust flags */ 1146 r = bus_alloc_resource(dev, type, rid, start, end, 1147 count, flags); 1148 if (r == NULL) 1149 goto bad; 1150 resource_list_add(&dinfo->resources, type, *rid, 1151 rman_get_start(r), rman_get_end(r), count); 1152 rle = resource_list_find(&dinfo->resources, type, *rid); 1153 if (!rle) 1154 goto bad; 1155 rle->res = r; 1156 } 1157 /* 1158 * If dev doesn't own the device, then we can't give this device 1159 * out. 1160 */ 1161 if (rman_get_device(rle->res) != dev) 1162 return (NULL); 1163 rman_set_device(rle->res, child); 1164 if (flags & RF_ACTIVE) 1165 BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, rle->res); 1166 return (rle->res); 1167 bad:; 1168 device_printf(dev, "WARNING: Resource not reserved by pccard\n"); 1169 return (NULL); 1170 } 1171 1172 static int 1173 pccard_release_resource(device_t dev, device_t child, int type, int rid, 1174 struct resource *r) 1175 { 1176 struct pccard_ivar *dinfo; 1177 int passthrough = (device_get_parent(child) != dev); 1178 struct resource_list_entry *rle = NULL; 1179 1180 if (passthrough) 1181 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 1182 type, rid, r); 1183 1184 dinfo = device_get_ivars(child); 1185 1186 rle = resource_list_find(&dinfo->resources, type, rid); 1187 1188 if (!rle) { 1189 device_printf(dev, "Allocated resource not found, " 1190 "%d %#x %#jx %#jx\n", 1191 type, rid, rman_get_start(r), rman_get_size(r)); 1192 return ENOENT; 1193 } 1194 if (!rle->res) { 1195 device_printf(dev, "Allocated resource not recorded\n"); 1196 return ENOENT; 1197 } 1198 /* 1199 * Deactivate the resource (since it is being released), and 1200 * assign it to the bus. 1201 */ 1202 BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, rle->res); 1203 rman_set_device(rle->res, dev); 1204 return (0); 1205 } 1206 1207 static void 1208 pccard_child_detached(device_t parent, device_t dev) 1209 { 1210 struct pccard_ivar *ivar = PCCARD_IVAR(dev); 1211 struct pccard_function *pf = ivar->pf; 1212 1213 pccard_function_disable(pf); 1214 } 1215 1216 static int 1217 pccard_filter(void *arg) 1218 { 1219 struct pccard_function *pf = (struct pccard_function*) arg; 1220 int reg; 1221 int doisr = 1; 1222 1223 /* 1224 * MFC cards know if they interrupted, so we have to ack the 1225 * interrupt and call the ISR. Non-MFC cards don't have these 1226 * bits, so they always get called. Many non-MFC cards have 1227 * this bit set always upon read, but some do not. 1228 * 1229 * We always ack the interrupt, even if there's no ISR 1230 * for the card. This is done on the theory that acking 1231 * the interrupt will pacify the card enough to keep an 1232 * interrupt storm from happening. Of course this won't 1233 * help in the non-MFC case. 1234 * 1235 * This has no impact for MPSAFEness of the client drivers. 1236 * We register this with whatever flags the intr_handler 1237 * was registered with. All these functions are MPSAFE. 1238 */ 1239 if (pccard_mfc(pf->sc)) { 1240 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS); 1241 if (reg & PCCARD_CCR_STATUS_INTR) 1242 pccard_ccr_write(pf, PCCARD_CCR_STATUS, 1243 reg & ~PCCARD_CCR_STATUS_INTR); 1244 else 1245 doisr = 0; 1246 } 1247 if (doisr) { 1248 if (pf->intr_filter != NULL) 1249 return (pf->intr_filter(pf->intr_handler_arg)); 1250 return (FILTER_SCHEDULE_THREAD); 1251 } 1252 return (FILTER_STRAY); 1253 } 1254 1255 static void 1256 pccard_intr(void *arg) 1257 { 1258 struct pccard_function *pf = (struct pccard_function*) arg; 1259 1260 pf->intr_handler(pf->intr_handler_arg); 1261 } 1262 1263 static int 1264 pccard_setup_intr(device_t dev, device_t child, struct resource *irq, 1265 int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg, 1266 void **cookiep) 1267 { 1268 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1269 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1270 struct pccard_function *pf = ivar->pf; 1271 int err; 1272 1273 if (pf->intr_filter != NULL || pf->intr_handler != NULL) 1274 panic("Only one interrupt handler per function allowed"); 1275 err = bus_generic_setup_intr(dev, child, irq, flags, pccard_filter, 1276 intr ? pccard_intr : NULL, pf, cookiep); 1277 if (err != 0) 1278 return (err); 1279 pf->intr_filter = filt; 1280 pf->intr_handler = intr; 1281 pf->intr_handler_arg = arg; 1282 pf->intr_handler_cookie = *cookiep; 1283 if (pccard_mfc(sc)) { 1284 pccard_ccr_write(pf, PCCARD_CCR_OPTION, 1285 pccard_ccr_read(pf, PCCARD_CCR_OPTION) | 1286 PCCARD_CCR_OPTION_IREQ_ENABLE); 1287 } 1288 return (0); 1289 } 1290 1291 static int 1292 pccard_teardown_intr(device_t dev, device_t child, struct resource *r, 1293 void *cookie) 1294 { 1295 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1296 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1297 struct pccard_function *pf = ivar->pf; 1298 int ret; 1299 1300 if (pccard_mfc(sc)) { 1301 pccard_ccr_write(pf, PCCARD_CCR_OPTION, 1302 pccard_ccr_read(pf, PCCARD_CCR_OPTION) & 1303 ~PCCARD_CCR_OPTION_IREQ_ENABLE); 1304 } 1305 ret = bus_generic_teardown_intr(dev, child, r, cookie); 1306 if (ret == 0) { 1307 pf->intr_handler = NULL; 1308 pf->intr_handler_arg = NULL; 1309 pf->intr_handler_cookie = NULL; 1310 } 1311 1312 return (ret); 1313 } 1314 1315 static int 1316 pccard_activate_resource(device_t brdev, device_t child, int type, int rid, 1317 struct resource *r) 1318 { 1319 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1320 struct pccard_function *pf = ivar->pf; 1321 1322 switch(type) { 1323 case SYS_RES_IOPORT: 1324 /* 1325 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC 1326 * card. 1327 */ 1328 if (pccard_mfc(pf->sc)) 1329 pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0, 1330 rman_get_size(r)); 1331 break; 1332 default: 1333 break; 1334 } 1335 return (bus_generic_activate_resource(brdev, child, type, rid, r)); 1336 } 1337 1338 static int 1339 pccard_deactivate_resource(device_t brdev, device_t child, int type, 1340 int rid, struct resource *r) 1341 { 1342 /* XXX undo pccard_activate_resource? XXX */ 1343 return (bus_generic_deactivate_resource(brdev, child, type, rid, r)); 1344 } 1345 1346 static int 1347 pccard_attr_read_impl(device_t brdev, device_t child, uint32_t offset, 1348 uint8_t *val) 1349 { 1350 struct pccard_ivar *devi = PCCARD_IVAR(child); 1351 struct pccard_function *pf = devi->pf; 1352 1353 /* 1354 * Optimization. Most of the time, devices want to access 1355 * the same page of the attribute memory that the CCR is in. 1356 * We take advantage of this fact here. 1357 */ 1358 if (offset / PCCARD_MEM_PAGE_SIZE == 1359 pf->ccr_base / PCCARD_MEM_PAGE_SIZE) 1360 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 1361 offset % PCCARD_MEM_PAGE_SIZE); 1362 else { 1363 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset, 1364 &offset); 1365 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, offset); 1366 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base, 1367 &offset); 1368 } 1369 return 0; 1370 } 1371 1372 static int 1373 pccard_attr_write_impl(device_t brdev, device_t child, uint32_t offset, 1374 uint8_t val) 1375 { 1376 struct pccard_ivar *devi = PCCARD_IVAR(child); 1377 struct pccard_function *pf = devi->pf; 1378 1379 /* 1380 * Optimization. Most of the time, devices want to access 1381 * the same page of the attribute memory that the CCR is in. 1382 * We take advantage of this fact here. 1383 */ 1384 if (offset / PCCARD_MEM_PAGE_SIZE == 1385 pf->ccr_base / PCCARD_MEM_PAGE_SIZE) 1386 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 1387 offset % PCCARD_MEM_PAGE_SIZE, val); 1388 else { 1389 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset, 1390 &offset); 1391 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, offset, val); 1392 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base, 1393 &offset); 1394 } 1395 1396 return 0; 1397 } 1398 1399 static int 1400 pccard_ccr_read_impl(device_t brdev, device_t child, uint32_t offset, 1401 uint8_t *val) 1402 { 1403 struct pccard_ivar *devi = PCCARD_IVAR(child); 1404 1405 *val = pccard_ccr_read(devi->pf, offset); 1406 DEVPRINTF((child, "ccr_read of %#x (%#x) is %#x\n", offset, 1407 devi->pf->pf_ccr_offset, *val)); 1408 return 0; 1409 } 1410 1411 static int 1412 pccard_ccr_write_impl(device_t brdev, device_t child, uint32_t offset, 1413 uint8_t val) 1414 { 1415 struct pccard_ivar *devi = PCCARD_IVAR(child); 1416 struct pccard_function *pf = devi->pf; 1417 1418 /* 1419 * Can't use pccard_ccr_write since client drivers may access 1420 * registers not contained in the 'mask' if they are non-standard. 1421 */ 1422 DEVPRINTF((child, "ccr_write of %#x to %#x (%#x)\n", val, offset, 1423 devi->pf->pf_ccr_offset)); 1424 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, pf->pf_ccr_offset + offset, 1425 val); 1426 return 0; 1427 } 1428 1429 1430 static device_method_t pccard_methods[] = { 1431 /* Device interface */ 1432 DEVMETHOD(device_probe, pccard_probe), 1433 DEVMETHOD(device_attach, pccard_attach), 1434 DEVMETHOD(device_detach, pccard_detach), 1435 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1436 DEVMETHOD(device_suspend, pccard_suspend), 1437 DEVMETHOD(device_resume, pccard_resume), 1438 1439 /* Bus interface */ 1440 DEVMETHOD(bus_print_child, pccard_print_child), 1441 DEVMETHOD(bus_driver_added, pccard_driver_added), 1442 DEVMETHOD(bus_child_detached, pccard_child_detached), 1443 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource), 1444 DEVMETHOD(bus_release_resource, pccard_release_resource), 1445 DEVMETHOD(bus_activate_resource, pccard_activate_resource), 1446 DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource), 1447 DEVMETHOD(bus_setup_intr, pccard_setup_intr), 1448 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr), 1449 DEVMETHOD(bus_set_resource, pccard_set_resource), 1450 DEVMETHOD(bus_get_resource, pccard_get_resource), 1451 DEVMETHOD(bus_delete_resource, pccard_delete_resource), 1452 DEVMETHOD(bus_probe_nomatch, pccard_probe_nomatch), 1453 DEVMETHOD(bus_read_ivar, pccard_read_ivar), 1454 DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str), 1455 DEVMETHOD(bus_child_location_str, pccard_child_location_str), 1456 1457 /* Card Interface */ 1458 DEVMETHOD(card_set_res_flags, pccard_set_res_flags), 1459 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset), 1460 DEVMETHOD(card_attach_card, pccard_attach_card), 1461 DEVMETHOD(card_detach_card, pccard_detach_card), 1462 DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup), 1463 DEVMETHOD(card_cis_scan, pccard_scan_cis), 1464 DEVMETHOD(card_attr_read, pccard_attr_read_impl), 1465 DEVMETHOD(card_attr_write, pccard_attr_write_impl), 1466 DEVMETHOD(card_ccr_read, pccard_ccr_read_impl), 1467 DEVMETHOD(card_ccr_write, pccard_ccr_write_impl), 1468 1469 { 0, 0 } 1470 }; 1471 1472 static driver_t pccard_driver = { 1473 "pccard", 1474 pccard_methods, 1475 sizeof(struct pccard_softc) 1476 }; 1477 1478 devclass_t pccard_devclass; 1479 1480 /* Maybe we need to have a slot device? */ 1481 DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0); 1482 DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0); 1483 MODULE_VERSION(pccard, 1); 1484