pccard.c revision 70762
1/* $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $ */ 2/* $FreeBSD: head/sys/dev/pccard/pccard.c 70762 2001-01-07 20:40:23Z imp $ */ 3 4/* 5 * Copyright (c) 1997 Marc Horowitz. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Marc Horowitz. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/malloc.h> 36#include <sys/module.h> 37#include <sys/kernel.h> 38#include <sys/queue.h> 39#include <sys/types.h> 40 41#include <sys/bus.h> 42#include <machine/bus.h> 43#include <sys/rman.h> 44#include <machine/resource.h> 45 46#include <dev/pccard/pccardreg.h> 47#include <dev/pccard/pccardvar.h> 48 49#include "power_if.h" 50#include "card_if.h" 51 52#define PCCARDDEBUG 53 54#ifdef PCCARDDEBUG 55int pccard_debug = 1; 56#define DPRINTF(arg) if (pccard_debug) printf arg 57#define DEVPRINTF(arg) if (pccard_debug) device_printf arg 58#define PRVERBOSE(arg) printf arg 59#define DEVPRVERBOSE(arg) device_printf arg 60#else 61#define DPRINTF(arg) 62#define DEVPRINTF(arg) 63#define PRVERBOSE(arg) if (bootverbose) printf arg 64#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg 65#endif 66 67#ifdef PCCARDVERBOSE 68int pccard_verbose = 1; 69#else 70int pccard_verbose = 0; 71#endif 72 73int pccard_print(void *, const char *); 74 75int 76pccard_ccr_read(pf, ccr) 77 struct pccard_function *pf; 78 int ccr; 79{ 80 return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 81 pf->pf_ccr_offset + ccr)); 82} 83 84void 85pccard_ccr_write(pf, ccr, val) 86 struct pccard_function *pf; 87 int ccr; 88 int val; 89{ 90 91 if ((pf->ccr_mask) & (1 << (ccr / 2))) { 92 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 93 pf->pf_ccr_offset + ccr, val); 94 } 95} 96 97static int 98pccard_attach_card(device_t dev) 99{ 100 struct pccard_softc *sc = PCCARD_SOFTC(dev); 101 struct pccard_function *pf; 102 struct pccard_ivar *ivar; 103 device_t child; 104 int attached; 105 106 sc->intr_handler_count = 0; 107 /* 108 * this is here so that when socket_enable calls gettype, trt happens 109 */ 110 STAILQ_INIT(&sc->card.pf_head); 111 112 DEVPRINTF((dev, "chip_socket_enable\n")); 113 POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 114 115 DEVPRINTF((dev, "read_cis\n")); 116 pccard_read_cis(sc); 117 118 DEVPRINTF((dev, "check_cis_quirks\n")); 119 pccard_check_cis_quirks(dev); 120 121 /* 122 * bail now if the card has no functions, or if there was an error in 123 * the cis. 124 */ 125 126 if (sc->card.error) { 127 device_printf (dev, "CARD ERROR!\n"); 128 return (1); 129 } 130 if (STAILQ_EMPTY(&sc->card.pf_head)) { 131 device_printf (dev, "Card has no functions!\n"); 132 return (1); 133 } 134 135 if (1) 136 pccard_print_cis(dev); 137 138 attached = 0; 139 140 DEVPRINTF((dev, "functions scanning\n")); 141 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 142 if (STAILQ_EMPTY(&pf->cfe_head)) 143 continue; 144 145 pf->sc = sc; 146 pf->cfe = NULL; 147 pf->dev = NULL; 148 } 149#if 0 150 DEVPRINTF((dev, "chip_socket_disable\n")); 151 POWER_DISABLE_SOCKET(device_get_parent(dev), dev); 152#endif 153 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 154 if (STAILQ_EMPTY(&pf->cfe_head)) 155 continue; 156 /* 157 * In NetBSD, the drivers are responsible for activating 158 * each function of a card. I think that in FreeBSD we 159 * want to activate them enough for the usual bus_*_resource 160 * routines will do the right thing. This many mean a 161 * departure from the current NetBSD model. 162 * 163 * This could get really ugly for multifunction cards. But 164 * it might also just fall out of the FreeBSD resource model. 165 * 166 */ 167 ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, 168 M_WAITOK | M_ZERO); 169 child = device_add_child(dev, NULL, -1); 170 device_set_ivars(child, ivar); 171 ivar->fcn = pf; 172 pf->dev = child; 173 /* 174 * XXX We might want to move the next two lines into 175 * XXX the pccard interface layer. For the moment, this 176 * XXX is OK, but some drivers want to pick the config 177 * XXX entry to use as well as some address tweaks (mostly 178 * XXX due to bugs in decode logic that makes some 179 * XXX addresses illegal or broken). 180 */ 181 pccard_function_init(pf); 182 if (pccard_function_enable(pf) == 0 && 183 device_probe_and_attach(child) == 0) { 184 attached++; 185 186 DEVPRINTF((sc->dev, "function %d CCR at %d " 187 "offset %x: %x %x %x %x, %x %x %x %x, %x\n", 188 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 189 pccard_ccr_read(pf, 0x00), 190 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 191 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 192 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 193 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 194 } else { 195 device_delete_child(dev, child); 196 } 197 } 198 return 0; 199} 200 201static int 202pccard_detach_card(device_t dev, int flags) 203{ 204 struct pccard_softc *sc = PCCARD_SOFTC(dev); 205 struct pccard_function *pf; 206 207 /* 208 * We are running on either the PCCARD socket's event thread 209 * or in user context detaching a device by user request. 210 */ 211 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 212 if (STAILQ_FIRST(&pf->cfe_head) == NULL) 213 continue; 214 215 pccard_function_disable(pf); 216 if (pf->dev) 217 device_delete_child(dev, pf->dev); 218 } 219 return 0; 220} 221 222const struct pccard_product * 223pccard_product_lookup(device_t dev, const struct pccard_product *tab, 224 size_t ent_size, pccard_product_match_fn matchfn) 225{ 226 const struct pccard_product *ent; 227 int matches; 228 u_int32_t fcn; 229 u_int32_t vendor; 230 u_int32_t prod; 231 char *vendorstr; 232 char *prodstr; 233 234#ifdef DIAGNOSTIC 235 if (sizeof *ent > ent_size) 236 panic("pccard_product_lookup: bogus ent_size %ld", 237 (long) ent_size); 238#endif 239 if (pccard_get_vendor(dev, &vendor)) 240 return (NULL); 241 if (pccard_get_product(dev, &prod)) 242 return (NULL); 243 if (pccard_get_function_number(dev, &fcn)) 244 return (NULL); 245 if (pccard_get_vendor_str(dev, &vendorstr)) 246 return (NULL); 247 if (pccard_get_product_str(dev, &prodstr)) 248 return (NULL); 249 for (ent = tab; ent->pp_name != NULL; 250 ent = (const struct pccard_product *) 251 ((const char *) ent + ent_size)) { 252 matches = 1; 253 if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY && 254 vendor != ent->pp_vendor) 255 matches = 0; 256 if (matches && ent->pp_product != PCCARD_PRODUCT_ANY && 257 prod != ent->pp_product) 258 matches = 0; 259 if (matches && fcn != ent->pp_expfunc) 260 matches = 0; 261 if (matches && ent->pp_vendor_str && 262 strcmp(ent->pp_vendor_str, vendorstr) != 0) 263 matches = 0; 264 if (matches && ent->pp_product_str && 265 strcmp(ent->pp_product_str, prodstr) != 0) 266 matches = 0; 267 if (matchfn != NULL) 268 matches = (*matchfn)(dev, ent, matches); 269 if (matches) 270 return (ent); 271 } 272 return (NULL); 273} 274 275static int 276pccard_card_gettype(device_t dev, int *type) 277{ 278 struct pccard_softc *sc = PCCARD_SOFTC(dev); 279 struct pccard_function *pf; 280 281 /* 282 * set the iftype to memory if this card has no functions (not yet 283 * probed), or only one function, and that is not initialized yet or 284 * that is memory. 285 */ 286 pf = STAILQ_FIRST(&sc->card.pf_head); 287 if (pf == NULL || 288 (STAILQ_NEXT(pf, pf_list) == NULL && 289 (pf->cfe == NULL || pf->cfe->iftype == PCCARD_IFTYPE_MEMORY))) 290 *type = PCCARD_IFTYPE_MEMORY; 291 else 292 *type = PCCARD_IFTYPE_IO; 293 return 0; 294} 295 296/* 297 * Initialize a PCCARD function. May be called as long as the function is 298 * disabled. 299 */ 300void 301pccard_function_init(struct pccard_function *pf) 302{ 303 struct pccard_config_entry *cfe; 304 int i; 305 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 306 struct resource_list *rl = &devi->resources; 307 struct resource_list_entry *rle; 308 struct resource *r = 0; 309 device_t bus; 310 int start; 311 int end; 312 313 if (pf->pf_flags & PFF_ENABLED) { 314 printf("pccard_function_init: function is enabled"); 315 return; 316 } 317 bus = device_get_parent(pf->dev); 318 /* Remember which configuration entry we are using. */ 319 for (cfe = STAILQ_FIRST(&pf->cfe_head); cfe != NULL; 320 cfe = STAILQ_NEXT(cfe, cfe_list)) { 321 for (i = 0; i < cfe->num_iospace; i++) 322 cfe->iores[i] = NULL; 323 cfe->irqres = NULL; 324 for (i = 0; i < cfe->num_iospace; i++) { 325 start = cfe->iospace[i].start; 326 if (start) 327 end = start + cfe->iospace[i].length - 1; 328 else 329 end = ~0; 330 cfe->iorid[i] = i; 331 r = cfe->iores[i] = bus_alloc_resource(bus, 332 SYS_RES_IOPORT, &cfe->iorid[i], start, end, 333 cfe->iospace[i].length, 334 rman_make_alignment_flags(cfe->iospace[i].length)); 335 if (cfe->iores[i] == 0) 336 goto not_this_one; 337 resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i], 338 rman_get_start(r), rman_get_end(r), 339 cfe->iospace[i].length); 340 { 341 rle = resource_list_find(rl, SYS_RES_IOPORT, 342 cfe->iorid[i]); 343 rle->res = r; 344 } 345 } 346 if (cfe->num_memspace > 0) { 347 goto not_this_one; 348 } 349 if (cfe->irqmask) { 350 cfe->irqrid = 0; 351 r = cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ, 352 &cfe->irqrid, 0, ~0, 1, 0); 353 if (cfe->irqres == 0) 354 goto not_this_one; 355 resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid, 356 rman_get_start(r), rman_get_end(r), 1); 357 { 358 rle = resource_list_find(rl, SYS_RES_IRQ, 359 cfe->irqrid); 360 rle->res = r; 361 } 362 } 363 /* XXX Don't know how to deal with maxtwins */ 364 /* If we get to here, we've allocated all we need */ 365 pf->cfe = cfe; 366 break; 367 not_this_one:; 368 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n", 369 cfe->number)); 370 /* 371 * Release resources that we partially allocated 372 * from this config entry. 373 */ 374 for (i = 0; i < cfe->num_iospace; i++) { 375 resource_list_delete(rl, SYS_RES_IOPORT, i); 376 if (cfe->iores[i]) 377 bus_release_resource(bus, SYS_RES_IOPORT, 378 cfe->iorid[i], cfe->iores[i]); 379 cfe->iores[i] = NULL; 380 } 381 if (cfe->irqmask && cfe->irqres) { 382 resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid); 383 bus_release_resource(bus, SYS_RES_IRQ, 384 cfe->irqrid, cfe->irqres); 385 cfe->irqres = NULL; 386 } 387 } 388} 389 390/* Enable a PCCARD function */ 391int 392pccard_function_enable(struct pccard_function *pf) 393{ 394 struct pccard_function *tmp; 395 int reg; 396 device_t dev = pf->sc->dev; 397 398 if (pf->cfe == NULL) { 399 DEVPRVERBOSE((dev, "No config entry could be allocated.\n")); 400 return ENOMEM; 401 } 402 403 /* 404 * Increase the reference count on the socket, enabling power, if 405 * necessary. 406 */ 407 if (pf->sc->sc_enabled_count++ == 0) 408 POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 409 DEVPRINTF((dev, "++enabled_count = %d\n", pf->sc->sc_enabled_count)); 410 411 if (pf->pf_flags & PFF_ENABLED) { 412 /* 413 * Don't do anything if we're already enabled. 414 */ 415 return (0); 416 } 417 418 /* 419 * it's possible for different functions' CCRs to be in the same 420 * underlying page. Check for that. 421 */ 422 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 423 if ((tmp->pf_flags & PFF_ENABLED) && 424 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 425 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 426 (tmp->ccr_base - tmp->pf_ccr_offset + 427 tmp->pf_ccr_realsize))) { 428 pf->pf_ccrt = tmp->pf_ccrt; 429 pf->pf_ccrh = tmp->pf_ccrh; 430 pf->pf_ccr_realsize = tmp->pf_ccr_realsize; 431 432 /* 433 * pf->pf_ccr_offset = (tmp->pf_ccr_offset - 434 * tmp->ccr_base) + pf->ccr_base; 435 */ 436 /* pf->pf_ccr_offset = 437 (tmp->pf_ccr_offset + pf->ccr_base) - 438 tmp->ccr_base; */ 439 pf->pf_ccr_window = tmp->pf_ccr_window; 440 break; 441 } 442 } 443 if (tmp == NULL) { 444 pf->ccr_rid = 0; 445 pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 446 &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE); 447 if (!pf->ccr_res) 448 goto bad; 449 DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%lx\n", 450 rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res), 451 pf->ccr_base)); 452 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, 453 pf->ccr_rid, PCCARD_A_MEM_ATTR); 454 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev, 455 pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset); 456 pf->pf_ccrt = rman_get_bustag(pf->ccr_res); 457 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res); 458 pf->pf_ccr_realsize = 1; 459 } 460 461 reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX); 462 reg |= PCCARD_CCR_OPTION_LEVIREQ; 463 if (pccard_mfc(pf->sc)) { 464 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE | 465 PCCARD_CCR_OPTION_ADDR_DECODE); 466 /* 467 * XXX Need to enable PCCARD_CCR_OPTION_IRQ_ENABLE if 468 * XXX we have an interrupt handler, but we don't know that 469 * XXX at this point. 470 */ 471/* reg |= PCCARD_CCR_OPTION_IREQ_ENABLE;*/ 472 } 473 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg); 474 475 reg = 0; 476 if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0) 477 reg |= PCCARD_CCR_STATUS_IOIS8; 478 if (pf->cfe->flags & PCCARD_CFE_AUDIO) 479 reg |= PCCARD_CCR_STATUS_AUDIO; 480 pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg); 481 482 pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0); 483 484 if (pccard_mfc(pf->sc)) { 485 long tmp, iosize; 486 487 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase; 488 /* round up to nearest (2^n)-1 */ 489 for (iosize = 1; iosize < tmp; iosize <<= 1) 490 ; 491 iosize--; 492 493 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0, 494 pf->pf_mfc_iobase & 0xff); 495 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1, 496 (pf->pf_mfc_iobase >> 8) & 0xff); 497 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0); 498 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0); 499 500 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize); 501 } 502 503#ifdef PCCARDDEBUG 504 if (pccard_debug) { 505 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 506 device_printf(tmp->sc->dev, 507 "function %d CCR at %d offset %x: " 508 "%x %x %x %x, %x %x %x %x, %x\n", 509 tmp->number, tmp->pf_ccr_window, 510 tmp->pf_ccr_offset, 511 pccard_ccr_read(tmp, 0x00), 512 pccard_ccr_read(tmp, 0x02), 513 pccard_ccr_read(tmp, 0x04), 514 pccard_ccr_read(tmp, 0x06), 515 pccard_ccr_read(tmp, 0x0A), 516 pccard_ccr_read(tmp, 0x0C), 517 pccard_ccr_read(tmp, 0x0E), 518 pccard_ccr_read(tmp, 0x10), 519 pccard_ccr_read(tmp, 0x12)); 520 } 521 } 522#endif 523 pf->pf_flags |= PFF_ENABLED; 524 return (0); 525 526 bad: 527 /* 528 * Decrement the reference count, and power down the socket, if 529 * necessary. 530 */ 531 if (--pf->sc->sc_enabled_count == 0) 532 POWER_DISABLE_SOCKET(device_get_parent(dev), dev); 533 DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count)); 534 535 return (1); 536} 537 538/* Disable PCCARD function. */ 539void 540pccard_function_disable(struct pccard_function *pf) 541{ 542 struct pccard_function *tmp; 543 device_t dev = pf->sc->dev; 544 545 if (pf->cfe == NULL) 546 panic("pccard_function_disable: function not initialized"); 547 548 if ((pf->pf_flags & PFF_ENABLED) == 0) { 549 /* 550 * Don't do anything if we're already disabled. 551 */ 552 return; 553 } 554 555 if (pf->intr_handler != NULL) { 556 pf->intr_handler = NULL; 557 pf->intr_handler_arg = NULL; 558 pf->intr_handler_cookie = NULL; 559 pccard_ccr_write(pf, PCCARD_CCR_OPTION, 560 pccard_ccr_read(pf, PCCARD_CCR_OPTION) & 561 ~PCCARD_CCR_OPTION_IREQ_ENABLE); 562 563 if (pf->sc->intr_handler_count == 1) { 564 struct pccard_ivar *ivar = PCCARD_IVAR(pf->dev); 565 struct resource_list_entry *rle = NULL; 566 567 pf->sc->intr_handler_count--; 568 rle = resource_list_find(&ivar->resources, SYS_RES_IRQ, 569 0); 570 if (rle == NULL) 571 panic("No IRQ for pccard?"); 572 573 bus_teardown_intr(dev, rle->res, 574 &pf->sc->intr_handler_count); 575 } 576 } 577 578 /* 579 * it's possible for different functions' CCRs to be in the same 580 * underlying page. Check for that. Note we mark us as disabled 581 * first to avoid matching ourself. 582 */ 583 584 pf->pf_flags &= ~PFF_ENABLED; 585 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 586 if ((tmp->pf_flags & PFF_ENABLED) && 587 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 588 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 589 (tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize))) 590 break; 591 } 592 593 /* Not used by anyone else; unmap the CCR. */ 594 if (tmp == NULL) { 595 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid, 596 pf->ccr_res); 597 pf->ccr_res = NULL; 598 } 599 600 /* 601 * Decrement the reference count, and power down the socket, if 602 * necessary. 603 */ 604 if (--pf->sc->sc_enabled_count == 0) 605 POWER_DISABLE_SOCKET(device_get_parent(dev), dev); 606 DEVPRINTF((dev, "--enabled_count = %d\n", pf->sc->sc_enabled_count)); 607} 608 609#if 0 610/* XXX These functions are needed, but not like this XXX */ 611int 612pccard_io_map(struct pccard_function *pf, int width, bus_addr_t offset, 613 bus_size_t size, struct pccard_io_handle *pcihp, int *windowp) 614{ 615 int reg; 616 617 if (pccard_chip_io_map(pf->sc->pct, pf->sc->pch, width, offset, size, 618 pcihp, windowp)) 619 return (1); 620 621 /* 622 * XXX in the multifunction multi-iospace-per-function case, this 623 * needs to cooperate with io_alloc to make sure that the spaces 624 * don't overlap, and that the ccr's are set correctly 625 */ 626 627 if (pccard_mfc(pf->sc)) { 628 long tmp, iosize; 629 630 if (pf->pf_mfc_iomax == 0) { 631 pf->pf_mfc_iobase = pcihp->addr + offset; 632 pf->pf_mfc_iomax = pf->pf_mfc_iobase + size; 633 } else { 634 /* this makes the assumption that nothing overlaps */ 635 if (pf->pf_mfc_iobase > pcihp->addr + offset) 636 pf->pf_mfc_iobase = pcihp->addr + offset; 637 if (pf->pf_mfc_iomax < pcihp->addr + offset + size) 638 pf->pf_mfc_iomax = pcihp->addr + offset + size; 639 } 640 641 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase; 642 /* round up to nearest (2^n)-1 */ 643 for (iosize = 1; iosize >= tmp; iosize <<= 1) 644 ; 645 iosize--; 646 647 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0, 648 pf->pf_mfc_iobase & 0xff); 649 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1, 650 (pf->pf_mfc_iobase >> 8) & 0xff); 651 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0); 652 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0); 653 654 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize); 655 656 reg = pccard_ccr_read(pf, PCCARD_CCR_OPTION); 657 reg |= PCCARD_CCR_OPTION_ADDR_DECODE; 658 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg); 659 } 660 return (0); 661} 662 663void 664pccard_io_unmap(struct pccard_function *pf, int window) 665{ 666 667 pccard_chip_io_unmap(pf->sc->pct, pf->sc->pch, window); 668 669 /* XXX Anything for multi-function cards? */ 670} 671#endif 672 673/* 674 * simulate the old "probe" routine. In the new world order, the driver 675 * needs to grab devices while in the old they were assigned to the device by 676 * the pccardd process. These symbols are exported to the upper layers. 677 */ 678int 679pccard_compat_probe(device_t dev) 680{ 681 return (CARD_COMPAT_MATCH(dev)); 682} 683 684int 685pccard_compat_attach(device_t dev) 686{ 687 int err; 688 689 err = CARD_COMPAT_PROBE(dev); 690 if (err == 0) 691 err = CARD_COMPAT_ATTACH(dev); 692 return (err); 693} 694 695#define PCCARD_NPORT 2 696#define PCCARD_NMEM 5 697#define PCCARD_NIRQ 1 698#define PCCARD_NDRQ 0 699 700static int 701pccard_add_children(device_t dev, int busno) 702{ 703 /* Call parent to scan for any current children */ 704 return 0; 705} 706 707static int 708pccard_probe(device_t dev) 709{ 710 device_set_desc(dev, "16-bit PCCard bus"); 711 return pccard_add_children(dev, device_get_unit(dev)); 712} 713 714static int 715pccard_attach(device_t dev) 716{ 717 struct pccard_softc *sc = PCCARD_SOFTC(dev); 718 719 sc->dev = dev; 720 sc->sc_enabled_count = 0; 721 return bus_generic_attach(dev); 722} 723 724static void 725pccard_print_resources(struct resource_list *rl, const char *name, int type, 726 int count, const char *format) 727{ 728 struct resource_list_entry *rle; 729 int printed; 730 int i; 731 732 printed = 0; 733 for (i = 0; i < count; i++) { 734 rle = resource_list_find(rl, type, i); 735 if (rle) { 736 if (printed == 0) 737 printf(" %s ", name); 738 else if (printed > 0) 739 printf(","); 740 printed++; 741 printf(format, rle->start); 742 if (rle->count > 1) { 743 printf("-"); 744 printf(format, rle->start + rle->count - 1); 745 } 746 } else if (i > 3) { 747 /* check the first few regardless */ 748 break; 749 } 750 } 751} 752 753static int 754pccard_print_child(device_t dev, device_t child) 755{ 756 struct pccard_ivar *devi = PCCARD_IVAR(child); 757 struct resource_list *rl = &devi->resources; 758 int retval = 0; 759 760 retval += bus_print_child_header(dev, child); 761 retval += printf(" at"); 762 763 if (devi) { 764 pccard_print_resources(rl, "port", SYS_RES_IOPORT, 765 PCCARD_NPORT, "%#lx"); 766 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY, 767 PCCARD_NMEM, "%#lx"); 768 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ, 769 "%ld"); 770 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ, 771 "%ld"); 772 retval += printf(" function %d config %d", devi->fcn->number, 773 devi->fcn->cfe->number); 774 } 775 776 retval += bus_print_child_footer(dev, child); 777 778 return (retval); 779} 780 781static int 782pccard_set_resource(device_t dev, device_t child, int type, int rid, 783 u_long start, u_long count) 784{ 785 struct pccard_ivar *devi = PCCARD_IVAR(child); 786 struct resource_list *rl = &devi->resources; 787 788 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 789 && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 790 return EINVAL; 791 if (rid < 0) 792 return EINVAL; 793 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT) 794 return EINVAL; 795 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM) 796 return EINVAL; 797 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ) 798 return EINVAL; 799 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ) 800 return EINVAL; 801 802 resource_list_add(rl, type, rid, start, start + count - 1, count); 803 804 return 0; 805} 806 807static int 808pccard_get_resource(device_t dev, device_t child, int type, int rid, 809 u_long *startp, u_long *countp) 810{ 811 struct pccard_ivar *devi = PCCARD_IVAR(child); 812 struct resource_list *rl = &devi->resources; 813 struct resource_list_entry *rle; 814 815 rle = resource_list_find(rl, type, rid); 816 if (!rle) 817 return ENOENT; 818 819 if (startp) 820 *startp = rle->start; 821 if (countp) 822 *countp = rle->count; 823 824 return 0; 825} 826 827static void 828pccard_delete_resource(device_t dev, device_t child, int type, int rid) 829{ 830 struct pccard_ivar *devi = PCCARD_IVAR(child); 831 struct resource_list *rl = &devi->resources; 832 resource_list_delete(rl, type, rid); 833} 834 835static int 836pccard_set_res_flags(device_t dev, device_t child, int type, int rid, 837 u_int32_t flags) 838{ 839 return CARD_SET_RES_FLAGS(device_get_parent(dev), child, type, 840 rid, flags); 841} 842 843static int 844pccard_set_memory_offset(device_t dev, device_t child, int rid, 845 u_int32_t offset, u_int32_t *deltap) 846 847{ 848 return CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid, 849 offset, deltap); 850} 851 852static int 853pccard_read_ivar(device_t bus, device_t child, int which, u_char *result) 854{ 855 struct pccard_ivar *devi = PCCARD_IVAR(child); 856 struct pccard_function *func = devi->fcn; 857 struct pccard_softc *sc = PCCARD_SOFTC(bus); 858 859 /* PCCARD_IVAR_ETHADDR unhandled from oldcard */ 860 switch (which) { 861 default: 862 case PCCARD_IVAR_ETHADDR: 863 return (ENOENT); 864 break; 865 case PCCARD_IVAR_VENDOR: 866 *(u_int32_t *) result = sc->card.manufacturer; 867 break; 868 case PCCARD_IVAR_PRODUCT: 869 *(u_int32_t *) result = sc->card.product; 870 break; 871 case PCCARD_IVAR_FUNCTION_NUMBER: 872 if (!func) { 873 device_printf(bus, "No function number, bug!\n"); 874 return (ENOENT); 875 } 876 *(u_int32_t *) result = func->number; 877 break; 878 case PCCARD_IVAR_VENDOR_STR: 879 *(char **) result = sc->card.cis1_info[0]; 880 break; 881 case PCCARD_IVAR_PRODUCT_STR: 882 *(char **) result = sc->card.cis1_info[1]; 883 break; 884 case PCCARD_IVAR_CIS3_STR: 885 *(char **) result = sc->card.cis1_info[2]; 886 break; 887 case PCCARD_IVAR_CIS4_STR: 888 *(char **) result = sc->card.cis1_info[2]; 889 break; 890 } 891 return (0); 892} 893 894static void 895pccard_driver_added(device_t dev, driver_t *driver) 896{ 897 /* 898 * XXX eventually we need to attach stuff when we know we 899 * XXX have kids. For now we do nothing because we normally 900 * XXX add children ourselves. We don't want to necessarily 901 * XXX force a reprobe. 902 */ 903} 904 905static struct resource * 906pccard_alloc_resource(device_t dev, device_t child, int type, int *rid, 907 u_long start, u_long end, u_long count, u_int flags) 908{ 909 struct resource_list_entry *rle = NULL; 910 911 /* XXX: This is an ugly way to fudge the resources. */ 912 913 if (device_get_parent(child) == dev) { 914 struct pccard_ivar *devi = PCCARD_IVAR(child); 915 struct resource_list *rl = &devi->resources; 916 917 rle = resource_list_find(rl, type, *rid); 918 } 919 920 if (rle != NULL) { 921 if (flags & RF_ACTIVE) 922 bus_activate_resource(dev, type, rle->rid, rle->res); 923 return (rle->res); 924 } 925 return (bus_generic_alloc_resource(dev, child, type, rid, start, 926 end, count, flags)); 927} 928 929static int 930pccard_release_resource(device_t dev, device_t child, int type, int rid, 931 struct resource *r) 932{ 933 struct resource_list_entry *rle = NULL; 934 935 if (device_get_parent(child) == dev) { 936 struct pccard_ivar *devi = PCCARD_IVAR(child); 937 struct resource_list *rl = &devi->resources; 938 939 rle = resource_list_find(rl, type, rid); 940 } 941 942 if (rle != NULL) { 943 return bus_release_resource(dev, type, rle->rid, rle->res); 944 } 945 946 return bus_generic_release_resource(dev, child, type, rid, r); 947} 948 949static int 950pccard_activate_resource(device_t dev, device_t child, int type, int rid, 951 struct resource *r) 952{ 953 /* XXX need to write to the COR to activate this for mf cards */ 954 struct resource_list_entry *rle = NULL; 955 956 if (device_get_parent(child) == dev) { 957 struct pccard_ivar *devi = PCCARD_IVAR(child); 958 struct resource_list *rl = &devi->resources; 959 960 rle = resource_list_find(rl, type, rid); 961 } 962 963 if (rle != NULL) { 964 return (bus_activate_resource(dev, type, rle->rid, rle->res)); 965 } 966 967 return (bus_generic_activate_resource(dev, child, type, rid, r)); 968} 969 970static int 971pccard_deactivate_resource(device_t dev, device_t child, int type, int rid, 972 struct resource *r) 973{ 974 /* XXX need to write to the COR to deactivate this for mf cards */ 975 struct resource_list_entry *rle = NULL; 976 977 if (device_get_parent(child) == dev) { 978 struct pccard_ivar *devi = PCCARD_IVAR(child); 979 struct resource_list *rl = &devi->resources; 980 981 rle = resource_list_find(rl, type, rid); 982 } 983 984 if (rle != NULL) { 985 return (bus_deactivate_resource(dev, type, rle->rid, rle->res)); 986 } 987 988 return (bus_generic_deactivate_resource(dev, child, type, rid, r)); 989} 990 991static void 992pccard_child_detached(device_t parent, device_t dev) 993{ 994 struct pccard_ivar *ivar = PCCARD_IVAR(dev); 995 996 if (parent == device_get_parent(dev)) 997 free(ivar, M_DEVBUF); 998} 999 1000static void 1001pccard_intr(void *arg) 1002{ 1003 struct pccard_softc *sc = (struct pccard_softc *) arg; 1004 struct pccard_function *pf; 1005 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 1006 if (pf->intr_handler != NULL) { 1007 int reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS); 1008 if (reg & PCCARD_CCR_STATUS_INTR) { 1009 pccard_ccr_write(pf, PCCARD_CCR_STATUS, 1010 reg & ~PCCARD_CCR_STATUS_INTR); 1011 pf->intr_handler(pf->intr_handler_arg); 1012 } 1013 } 1014 } 1015} 1016 1017static int 1018pccard_setup_intr(device_t dev, device_t child, struct resource *irq, 1019 int flags, driver_intr_t *intr, void *arg, void **cookiep) 1020{ 1021 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1022 struct pccard_function *func = ivar->fcn; 1023 struct resource_list_entry *rle = NULL; 1024 struct pccard_softc *sc = device_get_softc(dev); 1025 1026 if (func->intr_handler != NULL) 1027 panic("Only one interrupt handler per function allowed\n"); 1028 1029 rle = resource_list_find(&ivar->resources, SYS_RES_IRQ, 0); 1030 if (rle == NULL || rle->res != irq) 1031 panic("irq in setup_intr does not match allocated irq\n"); 1032 1033 func->intr_handler = intr; 1034 func->intr_handler_arg = arg; 1035 func->intr_handler_cookie = *cookiep = func; 1036 pccard_ccr_write(func, PCCARD_CCR_OPTION, 1037 pccard_ccr_read(func, PCCARD_CCR_OPTION) | 1038 PCCARD_CCR_OPTION_IREQ_ENABLE); 1039 1040 if (sc->intr_handler_count++ == 0) { 1041 rle = resource_list_find(&ivar->resources, SYS_RES_IRQ, 0); 1042 if (rle == NULL) 1043 panic("No IRQ for pccard?"); 1044 1045 bus_setup_intr(dev, rle->res, INTR_TYPE_TTY/* | INTR_FAST*/, 1046 pccard_intr, sc, (void*)&sc->intr_handler_count); 1047 } 1048 return 0; 1049} 1050 1051static int 1052pccard_teardown_intr(device_t dev, device_t child, struct resource *r, 1053 void *cookie) 1054{ 1055 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1056 struct pccard_function *func = ivar->fcn; 1057 struct pccard_softc *sc = device_get_softc(dev); 1058 1059 if (func->intr_handler_cookie != cookie) 1060 panic("pccard teardown of unknown interrupt handler\n"); 1061 1062 func->intr_handler = NULL; 1063 func->intr_handler_arg = NULL; 1064 func->intr_handler_cookie = NULL; 1065 pccard_ccr_write(func, PCCARD_CCR_OPTION, 1066 pccard_ccr_read(func, PCCARD_CCR_OPTION) & 1067 ~PCCARD_CCR_OPTION_IREQ_ENABLE); 1068 1069 if (--sc->intr_handler_count == 0) { 1070 struct resource_list_entry *rle = NULL; 1071 1072 rle = resource_list_find(&ivar->resources, SYS_RES_IRQ, 0); 1073 if (rle == NULL) 1074 panic("No IRQ for pccard?"); 1075 1076 bus_teardown_intr(dev, rle->res, &sc->intr_handler_count); 1077 } 1078 return 0; 1079} 1080 1081static device_method_t pccard_methods[] = { 1082 /* Device interface */ 1083 DEVMETHOD(device_probe, pccard_probe), 1084 DEVMETHOD(device_attach, pccard_attach), 1085 DEVMETHOD(device_detach, bus_generic_detach), 1086 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1087 DEVMETHOD(device_suspend, bus_generic_suspend), 1088 DEVMETHOD(device_resume, bus_generic_resume), 1089 1090 /* Bus interface */ 1091 DEVMETHOD(bus_print_child, pccard_print_child), 1092 DEVMETHOD(bus_driver_added, pccard_driver_added), 1093 DEVMETHOD(bus_child_detached, pccard_child_detached), 1094 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource), 1095 DEVMETHOD(bus_release_resource, pccard_release_resource), 1096 DEVMETHOD(bus_activate_resource, pccard_activate_resource), 1097 DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource), 1098 DEVMETHOD(bus_setup_intr, pccard_setup_intr), 1099 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr), 1100 DEVMETHOD(bus_set_resource, pccard_set_resource), 1101 DEVMETHOD(bus_get_resource, pccard_get_resource), 1102 DEVMETHOD(bus_delete_resource, pccard_delete_resource), 1103 DEVMETHOD(bus_read_ivar, pccard_read_ivar), 1104 1105 /* Card Interface */ 1106 DEVMETHOD(card_set_res_flags, pccard_set_res_flags), 1107 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset), 1108 DEVMETHOD(card_get_type, pccard_card_gettype), 1109 DEVMETHOD(card_attach_card, pccard_attach_card), 1110 DEVMETHOD(card_detach_card, pccard_detach_card), 1111 1112 { 0, 0 } 1113}; 1114 1115static driver_t pccard_driver = { 1116 "pccard", 1117 pccard_methods, 1118 sizeof(struct pccard_softc) 1119}; 1120 1121devclass_t pccard_devclass; 1122 1123DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0); 1124DRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0); 1125DRIVER_MODULE(pccard, pccbb, pccard_driver, pccard_devclass, 0, 0); 1126DRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0); 1127MODULE_VERSION(pccard, 1); 1128/*MODULE_DEPEND(pccard, pcic, 1, 1, 1);*/ 1129