1 /* $NetBSD: pcmcia.c,v 1.84 2007/12/09 20:28:14 jmcneill Exp $ */ 2 3 /* 4 * Copyright (c) 2004 Charles M. Hannum. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Charles M. Hannum. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 */ 20 21 /* 22 * Copyright (c) 1997 Marc Horowitz. All rights reserved. 23 * 24 * Redistribution and use in source and binary forms, with or without 25 * modification, are permitted provided that the following conditions 26 * are met: 27 * 1. Redistributions of source code must retain the above copyright 28 * notice, this list of conditions and the following disclaimer. 29 * 2. Redistributions in binary form must reproduce the above copyright 30 * notice, this list of conditions and the following disclaimer in the 31 * documentation and/or other materials provided with the distribution. 32 * 3. All advertising materials mentioning features or use of this software 33 * must display the following acknowledgement: 34 * This product includes software developed by Marc Horowitz. 35 * 4. The name of the author may not be used to endorse or promote products 36 * derived from this software without specific prior written permission. 37 * 38 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 39 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 40 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 41 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 42 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 43 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 47 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 48 */ 49 50 #include <sys/cdefs.h> 51 __KERNEL_RCSID(0, "$NetBSD: pcmcia.c,v 1.84 2007/12/09 20:28:14 jmcneill Exp $"); 52 53 #include "opt_pcmciaverbose.h" 54 55 #include <sys/param.h> 56 #include <sys/systm.h> 57 #include <sys/device.h> 58 59 #include <net/if.h> 60 61 #include <dev/pcmcia/pcmciareg.h> 62 #include <dev/pcmcia/pcmciachip.h> 63 #include <dev/pcmcia/pcmciavar.h> 64 #ifdef IT8368E_LEGACY_MODE /* XXX -uch */ 65 #include <arch/hpcmips/dev/it8368var.h> 66 #endif 67 68 #include "locators.h" 69 70 #ifdef PCMCIADEBUG 71 int pcmcia_debug = 0; 72 #define DPRINTF(arg) if (pcmcia_debug) printf arg 73 #else 74 #define DPRINTF(arg) 75 #endif 76 77 #ifdef PCMCIAVERBOSE 78 int pcmcia_verbose = 1; 79 #else 80 int pcmcia_verbose = 0; 81 #endif 82 83 int pcmcia_match(struct device *, struct cfdata *, void *); 84 void pcmcia_attach(struct device *, struct device *, void *); 85 int pcmcia_rescan(struct device *, const char *, const int *); 86 void pcmcia_childdetached(struct device *, struct device *); 87 int pcmcia_print(void *, const char *); 88 89 CFATTACH_DECL2(pcmcia, sizeof(struct pcmcia_softc), 90 pcmcia_match, pcmcia_attach, NULL, NULL, 91 pcmcia_rescan, pcmcia_childdetached); 92 93 int 94 pcmcia_ccr_read(pf, ccr) 95 struct pcmcia_function *pf; 96 int ccr; 97 { 98 99 return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 100 pf->pf_ccr_offset + ccr * 2)); 101 } 102 103 void 104 pcmcia_ccr_write(pf, ccr, val) 105 struct pcmcia_function *pf; 106 int ccr; 107 int val; 108 { 109 110 if (pf->ccr_mask & (1 << ccr)) { 111 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 112 pf->pf_ccr_offset + ccr * 2, val); 113 } 114 } 115 116 int 117 pcmcia_match(struct device *parent, struct cfdata *match, void *aux) 118 { 119 struct pcmciabus_attach_args *paa = aux; 120 121 if (strcmp(paa->paa_busname, match->cf_name)) { 122 return 0; 123 } 124 /* if the autoconfiguration got this far, there's a socket here */ 125 return (1); 126 } 127 128 void 129 pcmcia_attach(struct device *parent, struct device *self, void *aux) 130 { 131 struct pcmciabus_attach_args *paa = aux; 132 struct pcmcia_softc *sc = (struct pcmcia_softc *) self; 133 134 aprint_naive("\n"); 135 aprint_normal("\n"); 136 137 sc->pct = paa->pct; 138 sc->pch = paa->pch; 139 sc->iobase = paa->iobase; 140 sc->iosize = paa->iosize; 141 142 sc->ih = NULL; 143 144 if (!pmf_device_register(self, NULL, NULL)) 145 aprint_error_dev(self, "couldn't establish power handler\n"); 146 } 147 148 int 149 pcmcia_card_attach(dev) 150 struct device *dev; 151 { 152 struct pcmcia_softc *sc = (struct pcmcia_softc *) dev; 153 struct pcmcia_function *pf; 154 int error; 155 static const int wildcard[PCMCIACF_NLOCS] = { 156 PCMCIACF_FUNCTION_DEFAULT 157 }; 158 159 /* 160 * this is here so that when socket_enable calls gettype, trt happens 161 */ 162 SIMPLEQ_FIRST(&sc->card.pf_head) = NULL; 163 164 pcmcia_socket_enable(dev); 165 166 pcmcia_read_cis(sc); 167 pcmcia_check_cis_quirks(sc); 168 169 #if 1 /* XXX remove this, done below ??? */ 170 /* 171 * bail now if the card has no functions, or if there was an error in 172 * the cis. 173 */ 174 if (sc->card.error || 175 SIMPLEQ_EMPTY(&sc->card.pf_head)) { 176 printf("%s: card appears to have bogus CIS\n", 177 sc->dev.dv_xname); 178 error = EIO; 179 goto done; 180 } 181 #endif 182 183 if (pcmcia_verbose) 184 pcmcia_print_cis(sc); 185 186 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 187 if (SIMPLEQ_EMPTY(&pf->cfe_head)) 188 continue; 189 190 #ifdef DIAGNOSTIC 191 if (pf->child != NULL) { 192 printf("%s: %s still attached to function %d!\n", 193 sc->dev.dv_xname, pf->child->dv_xname, 194 pf->number); 195 panic("pcmcia_card_attach"); 196 } 197 #endif 198 pf->sc = sc; 199 pf->child = NULL; 200 pf->cfe = NULL; 201 pf->pf_ih = NULL; 202 } 203 204 error = pcmcia_rescan(dev, "pcmcia", wildcard); 205 done: 206 pcmcia_socket_disable(dev); 207 return (error); 208 } 209 210 int 211 pcmcia_rescan(struct device *self, const char *ifattr, 212 const int *locators) 213 { 214 struct pcmcia_softc *sc = (struct pcmcia_softc *)self; 215 struct pcmcia_function *pf; 216 struct pcmcia_attach_args paa; 217 int locs[PCMCIACF_NLOCS]; 218 219 if (sc->card.error || 220 SIMPLEQ_EMPTY(&sc->card.pf_head)) { 221 /* XXX silently ignore if no card present? */ 222 return (EIO); 223 } 224 225 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 226 if (SIMPLEQ_EMPTY(&pf->cfe_head)) 227 continue; 228 229 if ((locators[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT) 230 && (locators[PCMCIACF_FUNCTION] != pf->number)) 231 continue; 232 233 if (pf->child) 234 continue; 235 236 locs[PCMCIACF_FUNCTION] = pf->number; 237 238 paa.manufacturer = sc->card.manufacturer; 239 paa.product = sc->card.product; 240 paa.card = &sc->card; 241 paa.pf = pf; 242 243 pf->child = config_found_sm_loc(self, "pcmcia", locs, &paa, 244 pcmcia_print, 245 config_stdsubmatch); 246 } 247 248 return (0); 249 } 250 251 void 252 pcmcia_card_detach(dev, flags) 253 struct device *dev; 254 int flags; /* DETACH_* flags */ 255 { 256 struct pcmcia_softc *sc = (struct pcmcia_softc *) dev; 257 struct pcmcia_function *pf; 258 int error; 259 260 /* 261 * We are running on either the PCMCIA socket's event thread 262 * or in user context detaching a device by user request. 263 */ 264 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 265 pf->pf_flags |= PFF_DETACHED; 266 if (SIMPLEQ_EMPTY(&pf->cfe_head)) 267 continue; 268 if (pf->child == NULL) 269 continue; 270 DPRINTF(("%s: detaching %s (function %d)\n", 271 sc->dev.dv_xname, pf->child->dv_xname, pf->number)); 272 if ((error = config_detach(pf->child, flags)) != 0) { 273 printf("%s: error %d detaching %s (function %d)\n", 274 sc->dev.dv_xname, error, pf->child->dv_xname, 275 pf->number); 276 } 277 } 278 279 if (sc->sc_enabled_count != 0) { 280 #ifdef DIAGNOSTIC 281 printf("pcmcia_card_detach: enabled_count should be 0 here??\n"); 282 #endif 283 pcmcia_chip_socket_disable(sc->pct, sc->pch); 284 sc->sc_enabled_count = 0; 285 } 286 } 287 288 void 289 pcmcia_childdetached(struct device *self, struct device *child) 290 { 291 struct pcmcia_softc *sc = (struct pcmcia_softc *)self; 292 struct pcmcia_function *pf; 293 294 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 295 if (SIMPLEQ_EMPTY(&pf->cfe_head)) 296 continue; 297 if (pf->child == child) { 298 KASSERT(device_locator(child, PCMCIACF_FUNCTION) 299 == pf->number); 300 pf->child = NULL; 301 return; 302 } 303 } 304 305 printf("%s: pcmcia_childdetached: %s not found\n", 306 self->dv_xname, child->dv_xname); 307 } 308 309 void 310 pcmcia_card_deactivate(dev) 311 struct device *dev; 312 { 313 struct pcmcia_softc *sc = (struct pcmcia_softc *) dev; 314 struct pcmcia_function *pf; 315 316 /* 317 * We're in the chip's card removal interrupt handler. 318 * Deactivate the child driver. The PCMCIA socket's 319 * event thread will run later to finish the detach. 320 */ 321 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 322 if (SIMPLEQ_EMPTY(&pf->cfe_head)) 323 continue; 324 if (pf->child == NULL) 325 continue; 326 DPRINTF(("%s: deactivating %s (function %d)\n", 327 sc->dev.dv_xname, pf->child->dv_xname, pf->number)); 328 config_deactivate(pf->child); 329 } 330 } 331 332 int 333 pcmcia_print(arg, pnp) 334 void *arg; 335 const char *pnp; 336 { 337 struct pcmcia_attach_args *pa = arg; 338 struct pcmcia_softc *sc = pa->pf->sc; 339 struct pcmcia_card *card = &sc->card; 340 char devinfo[256]; 341 342 if (pnp) 343 aprint_normal("%s", pnp); 344 345 pcmcia_devinfo(card, !!pnp, devinfo, sizeof(devinfo)); 346 347 aprint_normal(" function %d: %s\n", pa->pf->number, devinfo); 348 349 return (UNCONF); 350 } 351 352 void 353 pcmcia_devinfo(card, showhex, cp, cplen) 354 struct pcmcia_card *card; 355 int showhex; 356 char *cp; 357 size_t cplen; 358 { 359 int i, n; 360 361 if (cplen > 1) { 362 *cp++ = '<'; 363 *cp = '\0'; 364 cplen--; 365 } 366 367 for (i = 0; i < 4 && card->cis1_info[i] != NULL && cplen > 1; i++) { 368 n = snprintf(cp, cplen, "%s%s", i ? ", " : "", 369 card->cis1_info[i]); 370 cp += n; 371 if (cplen < n) 372 return; 373 cplen -= n; 374 } 375 376 if (cplen > 1) { 377 *cp++ = '>'; 378 *cp = '\0'; 379 cplen--; 380 } 381 382 if (showhex && cplen > 1) 383 snprintf(cp, cplen, " (manufacturer 0x%04x, product 0x%04x)", 384 card->manufacturer, card->product); 385 } 386 387 const void * 388 pcmcia_product_lookup(pa, tab, nent, ent_size, matchfn) 389 struct pcmcia_attach_args *pa; 390 const void *tab; 391 size_t nent; 392 size_t ent_size; 393 pcmcia_product_match_fn matchfn; 394 { 395 const struct pcmcia_product *pp; 396 int n; 397 int matches; 398 399 #ifdef DIAGNOSTIC 400 if (sizeof *pp > ent_size) 401 panic("pcmcia_product_lookup: bogus ent_size %ld", 402 (long) ent_size); 403 #endif 404 405 for (pp = tab, n = nent; n; pp = (const struct pcmcia_product *) 406 ((const char *)pp + ent_size), n--) { 407 /* see if it matches vendor/product */ 408 matches = 0; 409 if ((pp->pp_vendor != PCMCIA_VENDOR_INVALID && 410 pp->pp_vendor == pa->manufacturer) && 411 (pp->pp_product != PCMCIA_PRODUCT_INVALID && 412 pp->pp_product == pa->product)) 413 matches = 1; 414 if ((pp->pp_cisinfo[0] && pa->card->cis1_info[0] && 415 !strcmp(pp->pp_cisinfo[0], pa->card->cis1_info[0])) && 416 (pp->pp_cisinfo[1] && pa->card->cis1_info[1] && 417 !strcmp(pp->pp_cisinfo[1], pa->card->cis1_info[1])) && 418 (!pp->pp_cisinfo[2] || (pa->card->cis1_info[2] && 419 !strcmp(pp->pp_cisinfo[2], pa->card->cis1_info[2]))) && 420 (!pp->pp_cisinfo[3] || (pa->card->cis1_info[3] && 421 !strcmp(pp->pp_cisinfo[3], pa->card->cis1_info[3])))) 422 matches = 1; 423 424 /* if a separate match function is given, let it override */ 425 if (matchfn) 426 matches = (*matchfn)(pa, pp, matches); 427 428 if (matches) 429 return (pp); 430 } 431 return (0); 432 } 433 434 void 435 pcmcia_socket_settype(dev, type) 436 struct device *dev; 437 int type; 438 { 439 struct pcmcia_softc *sc = (void *)dev; 440 441 pcmcia_chip_socket_settype(sc->pct, sc->pch, type); 442 } 443 444 /* 445 * Initialize a PCMCIA function. May be called as long as the function is 446 * disabled. 447 */ 448 void 449 pcmcia_function_init(pf, cfe) 450 struct pcmcia_function *pf; 451 struct pcmcia_config_entry *cfe; 452 { 453 if (pf->pf_flags & PFF_ENABLED) 454 panic("pcmcia_function_init: function is enabled"); 455 456 /* Remember which configuration entry we are using. */ 457 pf->cfe = cfe; 458 } 459 460 void 461 pcmcia_socket_enable(dev) 462 struct device *dev; 463 { 464 struct pcmcia_softc *sc = (void *)dev; 465 466 if (sc->sc_enabled_count++ == 0) 467 pcmcia_chip_socket_enable(sc->pct, sc->pch); 468 DPRINTF(("%s: ++enabled_count = %d\n", sc->dev.dv_xname, 469 sc->sc_enabled_count)); 470 } 471 472 void 473 pcmcia_socket_disable(dev) 474 struct device *dev; 475 { 476 struct pcmcia_softc *sc = (void *)dev; 477 478 if (--sc->sc_enabled_count == 0) 479 pcmcia_chip_socket_disable(sc->pct, sc->pch); 480 DPRINTF(("%s: --enabled_count = %d\n", sc->dev.dv_xname, 481 sc->sc_enabled_count)); 482 } 483 484 /* Enable a PCMCIA function */ 485 int 486 pcmcia_function_enable(pf) 487 struct pcmcia_function *pf; 488 { 489 struct pcmcia_softc *sc = pf->sc; 490 struct pcmcia_function *tmp; 491 int reg; 492 int error; 493 494 if (pf->cfe == NULL) 495 panic("pcmcia_function_enable: function not initialized"); 496 497 /* 498 * Increase the reference count on the socket, enabling power, if 499 * necessary. 500 */ 501 pcmcia_socket_enable(&sc->dev); 502 pcmcia_socket_settype(&sc->dev, pf->cfe->iftype); 503 504 if (pf->pf_flags & PFF_ENABLED) { 505 /* 506 * Don't do anything if we're already enabled. 507 */ 508 return (0); 509 } 510 511 /* 512 * it's possible for different functions' CCRs to be in the same 513 * underlying page. Check for that. 514 */ 515 516 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) { 517 if ((tmp->pf_flags & PFF_ENABLED) && 518 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 519 ((pf->ccr_base + PCMCIA_CCR_SIZE) <= 520 (tmp->ccr_base - tmp->pf_ccr_offset + 521 tmp->pf_ccr_realsize))) { 522 pf->pf_ccrt = tmp->pf_ccrt; 523 pf->pf_ccrh = tmp->pf_ccrh; 524 pf->pf_ccr_realsize = tmp->pf_ccr_realsize; 525 526 /* 527 * pf->pf_ccr_offset = (tmp->pf_ccr_offset - 528 * tmp->ccr_base) + pf->ccr_base; 529 */ 530 pf->pf_ccr_offset = 531 (tmp->pf_ccr_offset + pf->ccr_base) - 532 tmp->ccr_base; 533 pf->pf_ccr_window = tmp->pf_ccr_window; 534 break; 535 } 536 } 537 538 if (tmp == NULL) { 539 error = pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh); 540 if (error) 541 goto bad; 542 543 error = pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base, 544 PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset, 545 &pf->pf_ccr_window); 546 if (error) { 547 pcmcia_mem_free(pf, &pf->pf_pcmh); 548 goto bad; 549 } 550 } 551 552 if (pcmcia_mfc(sc) || 1) { 553 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0, 554 (pf->pf_mfc_iobase >> 0) & 0xff); 555 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1, 556 (pf->pf_mfc_iobase >> 8) & 0xff); 557 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2, 558 (pf->pf_mfc_iobase >> 16) & 0xff); 559 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3, 560 (pf->pf_mfc_iobase >> 24) & 0xff); 561 pcmcia_ccr_write(pf, PCMCIA_CCR_IOLIMIT, 562 pf->pf_mfc_iomax - pf->pf_mfc_iobase); 563 } 564 565 reg = 0; 566 if (pf->cfe->flags & PCMCIA_CFE_AUDIO) 567 reg |= PCMCIA_CCR_STATUS_AUDIO; 568 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg); 569 570 pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0); 571 572 reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX); 573 reg |= PCMCIA_CCR_OPTION_LEVIREQ; 574 if (pcmcia_mfc(sc)) { 575 reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE | 576 PCMCIA_CCR_OPTION_ADDR_DECODE); 577 if (pf->pf_ih) 578 reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE; 579 580 } 581 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg); 582 583 #ifdef PCMCIADEBUG 584 if (pcmcia_debug) { 585 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) { 586 printf("%s: function %d CCR at %d offset %lx: " 587 "%x %x %x %x, %x %x %x %x, %x\n", 588 tmp->sc->dev.dv_xname, tmp->number, 589 tmp->pf_ccr_window, 590 (unsigned long) tmp->pf_ccr_offset, 591 pcmcia_ccr_read(tmp, 0), 592 pcmcia_ccr_read(tmp, 1), 593 pcmcia_ccr_read(tmp, 2), 594 pcmcia_ccr_read(tmp, 3), 595 596 pcmcia_ccr_read(tmp, 5), 597 pcmcia_ccr_read(tmp, 6), 598 pcmcia_ccr_read(tmp, 7), 599 pcmcia_ccr_read(tmp, 8), 600 601 pcmcia_ccr_read(tmp, 9)); 602 } 603 } 604 #endif 605 606 #ifdef IT8368E_LEGACY_MODE 607 /* return to I/O mode */ 608 it8368_mode(pf, IT8368_IO_MODE, IT8368_WIDTH_16); 609 #endif 610 611 pf->pf_flags |= PFF_ENABLED; 612 return (0); 613 614 bad: 615 /* 616 * Decrement the reference count, and power down the socket, if 617 * necessary. 618 */ 619 printf("%s: couldn't map the CCR\n", pf->child->dv_xname); 620 pcmcia_socket_disable(&sc->dev); 621 622 return (error); 623 } 624 625 /* Disable PCMCIA function. */ 626 void 627 pcmcia_function_disable(pf) 628 struct pcmcia_function *pf; 629 { 630 struct pcmcia_softc *sc = pf->sc; 631 struct pcmcia_function *tmp; 632 int reg; 633 634 if (pf->cfe == NULL) 635 panic("pcmcia_function_enable: function not initialized"); 636 637 if ((pf->pf_flags & PFF_ENABLED) == 0) { 638 /* 639 * Don't do anything but decrement if we're already disabled. 640 */ 641 goto out; 642 } 643 644 if (pcmcia_mfc(sc) && 645 (pf->pf_flags & PFF_DETACHED) == 0) { 646 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION); 647 reg &= ~(PCMCIA_CCR_OPTION_FUNC_ENABLE| 648 PCMCIA_CCR_OPTION_ADDR_DECODE| 649 PCMCIA_CCR_OPTION_IREQ_ENABLE); 650 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg); 651 } 652 653 /* 654 * it's possible for different functions' CCRs to be in the same 655 * underlying page. Check for that. Note we mark us as disabled 656 * first to avoid matching ourself. 657 */ 658 659 pf->pf_flags &= ~PFF_ENABLED; 660 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) { 661 if ((tmp->pf_flags & PFF_ENABLED) && 662 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 663 ((pf->ccr_base + PCMCIA_CCR_SIZE) <= 664 (tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize))) 665 break; 666 } 667 668 /* Not used by anyone else; unmap the CCR. */ 669 if (tmp == NULL) { 670 pcmcia_mem_unmap(pf, pf->pf_ccr_window); 671 pcmcia_mem_free(pf, &pf->pf_pcmh); 672 } 673 674 out: 675 /* 676 * Decrement the reference count, and power down the socket, if 677 * necessary. 678 */ 679 pcmcia_socket_disable(&sc->dev); 680 } 681 682 int 683 pcmcia_io_map(pf, width, pcihp, windowp) 684 struct pcmcia_function *pf; 685 int width; 686 struct pcmcia_io_handle *pcihp; 687 int *windowp; 688 { 689 struct pcmcia_softc *sc = pf->sc; 690 int error; 691 692 if (pf->pf_flags & PFF_ENABLED) 693 printf("pcmcia_io_map: function is enabled!\n"); 694 695 error = pcmcia_chip_io_map(sc->pct, sc->pch, 696 width, 0, pcihp->size, pcihp, windowp); 697 if (error) 698 return (error); 699 700 /* 701 * XXX in the multifunction multi-iospace-per-function case, this 702 * needs to cooperate with io_alloc to make sure that the spaces 703 * don't overlap, and that the ccr's are set correctly 704 */ 705 706 if (pcmcia_mfc(sc) || 1) { 707 bus_addr_t iobase = pcihp->addr; 708 bus_addr_t iomax = pcihp->addr + pcihp->size - 1; 709 710 DPRINTF(("window iobase %lx iomax %lx\n", (long)iobase, 711 (long)iomax)); 712 if (pf->pf_mfc_iobase == 0) { 713 pf->pf_mfc_iobase = iobase; 714 pf->pf_mfc_iomax = iomax; 715 } else { 716 if (iobase < pf->pf_mfc_iobase) 717 pf->pf_mfc_iobase = iobase; 718 if (iomax > pf->pf_mfc_iomax) 719 pf->pf_mfc_iomax = iomax; 720 } 721 DPRINTF(("function iobase %lx iomax %lx\n", 722 (long)pf->pf_mfc_iobase, (long)pf->pf_mfc_iomax)); 723 } 724 725 return (0); 726 } 727 728 void 729 pcmcia_io_unmap(pf, window) 730 struct pcmcia_function *pf; 731 int window; 732 { 733 struct pcmcia_softc *sc = pf->sc; 734 735 if (pf->pf_flags & PFF_ENABLED) 736 printf("pcmcia_io_unmap: function is enabled!\n"); 737 738 pcmcia_chip_io_unmap(sc->pct, sc->pch, window); 739 } 740 741 void * 742 pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg) 743 struct pcmcia_function *pf; 744 int ipl; 745 int (*ih_fct)(void *); 746 void *ih_arg; 747 { 748 749 if (pf->pf_flags & PFF_ENABLED) 750 printf("pcmcia_intr_establish: function is enabled!\n"); 751 if (pf->pf_ih) 752 panic("pcmcia_intr_establish: already done\n"); 753 754 pf->pf_ih = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch, 755 pf, ipl, ih_fct, ih_arg); 756 if (!pf->pf_ih) 757 printf("%s: interrupt establish failed\n", pf->child->dv_xname); 758 return (pf->pf_ih); 759 } 760 761 void 762 pcmcia_intr_disestablish(pf, ih) 763 struct pcmcia_function *pf; 764 void *ih; 765 { 766 767 if (pf->pf_flags & PFF_ENABLED) 768 printf("pcmcia_intr_disestablish: function is enabled!\n"); 769 if (!pf->pf_ih) 770 panic("pcmcia_intr_distestablish: already done\n"); 771 772 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih); 773 pf->pf_ih = 0; 774 } 775 776 int 777 pcmcia_config_alloc(pf, cfe) 778 struct pcmcia_function *pf; 779 struct pcmcia_config_entry *cfe; 780 { 781 int error = 0; 782 int n, m; 783 784 for (n = 0; n < cfe->num_iospace; n++) { 785 bus_addr_t start = cfe->iospace[n].start; 786 bus_size_t length = cfe->iospace[n].length; 787 bus_size_t align = cfe->iomask ? (1 << cfe->iomask) : 788 length; 789 bus_size_t skew = start & (align - 1); 790 791 if ((start - skew) == 0 && align < 0x400) { 792 if (skew) 793 printf("Drats! I need a skew!\n"); 794 start = 0; 795 } 796 797 DPRINTF(("pcmcia_config_alloc: io %d start=%lx length=%lx align=%lx skew=%lx\n", 798 n, (long)start, (long)length, (long)align, (long)skew)); 799 800 error = pcmcia_io_alloc(pf, start, length, align, 801 &cfe->iospace[n].handle); 802 if (error) 803 break; 804 } 805 if (n < cfe->num_iospace) { 806 for (m = 0; m < n; m++) 807 pcmcia_io_free(pf, &cfe->iospace[m].handle); 808 return (error); 809 } 810 811 for (n = 0; n < cfe->num_memspace; n++) { 812 bus_size_t length = cfe->memspace[n].length; 813 814 DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n, 815 (long)length)); 816 817 error = pcmcia_mem_alloc(pf, length, &cfe->memspace[n].handle); 818 if (error) 819 break; 820 } 821 if (n < cfe->num_memspace) { 822 for (m = 0; m < cfe->num_iospace; m++) 823 pcmcia_io_free(pf, &cfe->iospace[m].handle); 824 for (m = 0; m < n; m++) 825 pcmcia_mem_free(pf, &cfe->memspace[m].handle); 826 return (error); 827 } 828 829 /* This one's good! */ 830 return (error); 831 } 832 833 void 834 pcmcia_config_free(pf) 835 struct pcmcia_function *pf; 836 { 837 struct pcmcia_config_entry *cfe = pf->cfe; 838 int m; 839 840 for (m = 0; m < cfe->num_iospace; m++) 841 pcmcia_io_free(pf, &cfe->iospace[m].handle); 842 for (m = 0; m < cfe->num_memspace; m++) 843 pcmcia_mem_free(pf, &cfe->memspace[m].handle); 844 } 845 846 int 847 pcmcia_config_map(pf) 848 struct pcmcia_function *pf; 849 { 850 struct pcmcia_config_entry *cfe = pf->cfe; 851 int error = 0; 852 int n, m; 853 854 for (n = 0; n < cfe->num_iospace; n++) { 855 int width; 856 857 if (cfe->flags & PCMCIA_CFE_IO16) 858 width = PCMCIA_WIDTH_AUTO; 859 else 860 width = PCMCIA_WIDTH_IO8; 861 error = pcmcia_io_map(pf, width, &cfe->iospace[n].handle, 862 &cfe->iospace[n].window); 863 if (error) 864 break; 865 } 866 if (n < cfe->num_iospace) { 867 for (m = 0; m < n; m++) 868 pcmcia_io_unmap(pf, cfe->iospace[m].window); 869 return (error); 870 } 871 872 for (n = 0; n < cfe->num_memspace; n++) { 873 bus_size_t length = cfe->memspace[n].length; 874 int width; 875 876 DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n, 877 (long)length)); 878 879 /*XXX*/ 880 width = PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON; 881 error = pcmcia_mem_map(pf, width, 0, length, 882 &cfe->memspace[n].handle, &cfe->memspace[n].offset, 883 &cfe->memspace[n].window); 884 if (error) 885 break; 886 } 887 if (n < cfe->num_memspace) { 888 for (m = 0; m < cfe->num_iospace; m++) 889 pcmcia_io_unmap(pf, cfe->iospace[m].window); 890 for (m = 0; m < n; m++) 891 pcmcia_mem_unmap(pf, cfe->memspace[m].window); 892 return (error); 893 } 894 895 /* This one's good! */ 896 return (error); 897 } 898 899 void 900 pcmcia_config_unmap(pf) 901 struct pcmcia_function *pf; 902 { 903 struct pcmcia_config_entry *cfe = pf->cfe; 904 int m; 905 906 for (m = 0; m < cfe->num_iospace; m++) 907 pcmcia_io_unmap(pf, cfe->iospace[m].window); 908 for (m = 0; m < cfe->num_memspace; m++) 909 pcmcia_mem_unmap(pf, cfe->memspace[m].window); 910 } 911 912 int 913 pcmcia_function_configure(pf, validator) 914 struct pcmcia_function *pf; 915 int (*validator)(struct pcmcia_config_entry *); 916 { 917 struct pcmcia_config_entry *cfe; 918 int error = ENOENT; 919 920 SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) { 921 error = validator(cfe); 922 if (error) 923 continue; 924 error = pcmcia_config_alloc(pf, cfe); 925 if (!error) 926 break; 927 } 928 if (!cfe) { 929 DPRINTF(("pcmcia_function_configure: no config entry found, error=%d\n", 930 error)); 931 return (error); 932 } 933 934 /* Remember which configuration entry we are using. */ 935 pf->cfe = cfe; 936 937 error = pcmcia_config_map(pf); 938 if (error) { 939 DPRINTF(("pcmcia_function_configure: map failed, error=%d\n", 940 error)); 941 return (error); 942 } 943 944 return (0); 945 } 946 947 void 948 pcmcia_function_unconfigure(pf) 949 struct pcmcia_function *pf; 950 { 951 952 pcmcia_config_unmap(pf); 953 pcmcia_config_free(pf); 954 } 955