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