pccard.c revision 74636
1/* $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $ */ 2/* $FreeBSD: head/sys/dev/pccard/pccard.c 74636 2001-03-22 06:00:07Z 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 resource_list_delete(rl, SYS_RES_IOPORT, i); 374 if (cfe->iores[i]) 375 bus_release_resource(bus, SYS_RES_IOPORT, 376 cfe->iorid[i], cfe->iores[i]); 377 cfe->iores[i] = NULL; 378 } 379 if (cfe->irqmask && cfe->irqres) { 380 resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid); 381 bus_release_resource(bus, SYS_RES_IRQ, 382 cfe->irqrid, cfe->irqres); 383 cfe->irqres = NULL; 384 } 385 } 386} 387 388/* Enable a PCCARD function */ 389int 390pccard_function_enable(struct pccard_function *pf) 391{ 392 struct pccard_function *tmp; 393 int reg; 394 device_t dev = pf->sc->dev; 395 396 if (pf->cfe == NULL) { 397 DEVPRVERBOSE((dev, "No config entry could be allocated.\n")); 398 return (ENOMEM); 399 } 400 401 /* 402 * Increase the reference count on the socket, enabling power, if 403 * necessary. 404 */ 405 if (pf->sc->sc_enabled_count++ == 0) 406 POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 407 DEVPRINTF((dev, "++enabled_count = %d\n", pf->sc->sc_enabled_count)); 408 409 if (pf->pf_flags & PFF_ENABLED) { 410 /* 411 * Don't do anything if we're already enabled. 412 */ 413 return (0); 414 } 415 416 /* 417 * it's possible for different functions' CCRs to be in the same 418 * underlying page. Check for that. 419 */ 420 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 421 if ((tmp->pf_flags & PFF_ENABLED) && 422 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 423 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 424 (tmp->ccr_base - tmp->pf_ccr_offset + 425 tmp->pf_ccr_realsize))) { 426 pf->pf_ccrt = tmp->pf_ccrt; 427 pf->pf_ccrh = tmp->pf_ccrh; 428 pf->pf_ccr_realsize = tmp->pf_ccr_realsize; 429 430 /* 431 * pf->pf_ccr_offset = (tmp->pf_ccr_offset - 432 * tmp->ccr_base) + pf->ccr_base; 433 */ 434 /* pf->pf_ccr_offset = 435 (tmp->pf_ccr_offset + pf->ccr_base) - 436 tmp->ccr_base; */ 437 pf->pf_ccr_window = tmp->pf_ccr_window; 438 break; 439 } 440 } 441 if (tmp == NULL) { 442 pf->ccr_rid = 0; 443 pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 444 &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE); 445 if (!pf->ccr_res) 446 goto bad; 447 DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%lx\n", 448 rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res), 449 pf->ccr_base)); 450 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, 451 pf->ccr_rid, PCCARD_A_MEM_ATTR); 452 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev, 453 pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset); 454 pf->pf_ccrt = rman_get_bustag(pf->ccr_res); 455 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res); 456 pf->pf_ccr_realsize = 1; 457 } 458 459 reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX); 460 reg |= PCCARD_CCR_OPTION_LEVIREQ; 461 if (pccard_mfc(pf->sc)) { 462 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE | 463 PCCARD_CCR_OPTION_ADDR_DECODE); 464 /* 465 * XXX Need to enable PCCARD_CCR_OPTION_IRQ_ENABLE if 466 * XXX we have an interrupt handler, but we don't know that 467 * XXX at this point. 468 */ 469/* reg |= PCCARD_CCR_OPTION_IREQ_ENABLE;*/ 470 } 471 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg); 472 473 reg = 0; 474 if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0) 475 reg |= PCCARD_CCR_STATUS_IOIS8; 476 if (pf->cfe->flags & PCCARD_CFE_AUDIO) 477 reg |= PCCARD_CCR_STATUS_AUDIO; 478 pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg); 479 480 pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0); 481 482 if (pccard_mfc(pf->sc)) { 483 long tmp, iosize; 484 485 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase; 486 /* round up to nearest (2^n)-1 */ 487 for (iosize = 1; iosize < tmp; iosize <<= 1) 488 ; 489 iosize--; 490 491 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0, 492 pf->pf_mfc_iobase & 0xff); 493 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1, 494 (pf->pf_mfc_iobase >> 8) & 0xff); 495 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0); 496 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0); 497 498 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize); 499 } 500 501#ifdef PCCARDDEBUG 502 if (pccard_debug) { 503 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 504 device_printf(tmp->sc->dev, 505 "function %d CCR at %d offset %x: " 506 "%x %x %x %x, %x %x %x %x, %x\n", 507 tmp->number, tmp->pf_ccr_window, 508 tmp->pf_ccr_offset, 509 pccard_ccr_read(tmp, 0x00), 510 pccard_ccr_read(tmp, 0x02), 511 pccard_ccr_read(tmp, 0x04), 512 pccard_ccr_read(tmp, 0x06), 513 pccard_ccr_read(tmp, 0x0A), 514 pccard_ccr_read(tmp, 0x0C), 515 pccard_ccr_read(tmp, 0x0E), 516 pccard_ccr_read(tmp, 0x10), 517 pccard_ccr_read(tmp, 0x12)); 518 } 519 } 520#endif 521 pf->pf_flags |= PFF_ENABLED; 522 return (0); 523 524 bad: 525 /* 526 * Decrement the reference count, and power down the socket, if 527 * necessary. 528 */ 529 if (--pf->sc->sc_enabled_count == 0) 530 POWER_DISABLE_SOCKET(device_get_parent(dev), dev); 531 DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count)); 532 533 return (1); 534} 535 536/* Disable PCCARD function. */ 537void 538pccard_function_disable(struct pccard_function *pf) 539{ 540 struct pccard_function *tmp; 541 device_t dev = pf->sc->dev; 542 543 if (pf->cfe == NULL) 544 panic("pccard_function_disable: function not initialized"); 545 546 if ((pf->pf_flags & PFF_ENABLED) == 0) { 547 /* 548 * Don't do anything if we're already disabled. 549 */ 550 return; 551 } 552 553 if (pf->intr_handler != NULL) { 554 pf->intr_handler = NULL; 555 pf->intr_handler_arg = NULL; 556 pf->intr_handler_cookie = NULL; 557 pccard_ccr_write(pf, PCCARD_CCR_OPTION, 558 pccard_ccr_read(pf, PCCARD_CCR_OPTION) & 559 ~PCCARD_CCR_OPTION_IREQ_ENABLE); 560 561 if (pf->sc->intr_handler_count == 1) { 562 struct pccard_ivar *ivar = PCCARD_IVAR(pf->dev); 563 struct resource_list_entry *rle = NULL; 564 565 pf->sc->intr_handler_count--; 566 rle = resource_list_find(&ivar->resources, SYS_RES_IRQ, 567 0); 568 if (rle == NULL) 569 panic("No IRQ for pccard?"); 570 571 bus_teardown_intr(dev, rle->res, 572 &pf->sc->intr_handler_count); 573 } 574 } 575 576 /* 577 * it's possible for different functions' CCRs to be in the same 578 * underlying page. Check for that. Note we mark us as disabled 579 * first to avoid matching ourself. 580 */ 581 582 pf->pf_flags &= ~PFF_ENABLED; 583 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 584 if ((tmp->pf_flags & PFF_ENABLED) && 585 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 586 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 587 (tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize))) 588 break; 589 } 590 591 /* Not used by anyone else; unmap the CCR. */ 592 if (tmp == NULL) { 593 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid, 594 pf->ccr_res); 595 pf->ccr_res = NULL; 596 } 597 598 /* 599 * Decrement the reference count, and power down the socket, if 600 * necessary. 601 */ 602 if (--pf->sc->sc_enabled_count == 0) 603 POWER_DISABLE_SOCKET(device_get_parent(dev), dev); 604 DEVPRINTF((dev, "--enabled_count = %d\n", pf->sc->sc_enabled_count)); 605} 606 607#if 0 608/* XXX These functions are needed, but not like this XXX */ 609int 610pccard_io_map(struct pccard_function *pf, int width, bus_addr_t offset, 611 bus_size_t size, struct pccard_io_handle *pcihp, int *windowp) 612{ 613 int reg; 614 615 if (pccard_chip_io_map(pf->sc->pct, pf->sc->pch, width, offset, size, 616 pcihp, windowp)) 617 return (1); 618 619 /* 620 * XXX in the multifunction multi-iospace-per-function case, this 621 * needs to cooperate with io_alloc to make sure that the spaces 622 * don't overlap, and that the ccr's are set correctly 623 */ 624 625 if (pccard_mfc(pf->sc)) { 626 long tmp, iosize; 627 628 if (pf->pf_mfc_iomax == 0) { 629 pf->pf_mfc_iobase = pcihp->addr + offset; 630 pf->pf_mfc_iomax = pf->pf_mfc_iobase + size; 631 } else { 632 /* this makes the assumption that nothing overlaps */ 633 if (pf->pf_mfc_iobase > pcihp->addr + offset) 634 pf->pf_mfc_iobase = pcihp->addr + offset; 635 if (pf->pf_mfc_iomax < pcihp->addr + offset + size) 636 pf->pf_mfc_iomax = pcihp->addr + offset + size; 637 } 638 639 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase; 640 /* round up to nearest (2^n)-1 */ 641 for (iosize = 1; iosize >= tmp; iosize <<= 1) 642 ; 643 iosize--; 644 645 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0, 646 pf->pf_mfc_iobase & 0xff); 647 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1, 648 (pf->pf_mfc_iobase >> 8) & 0xff); 649 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0); 650 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0); 651 652 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize); 653 654 reg = pccard_ccr_read(pf, PCCARD_CCR_OPTION); 655 reg |= PCCARD_CCR_OPTION_ADDR_DECODE; 656 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg); 657 } 658 return (0); 659} 660 661void 662pccard_io_unmap(struct pccard_function *pf, int window) 663{ 664 665 pccard_chip_io_unmap(pf->sc->pct, pf->sc->pch, window); 666 667 /* XXX Anything for multi-function cards? */ 668} 669#endif 670 671/* 672 * simulate the old "probe" routine. In the new world order, the driver 673 * needs to grab devices while in the old they were assigned to the device by 674 * the pccardd process. These symbols are exported to the upper layers. 675 */ 676static int 677pccard_compat_do_probe(device_t bus, device_t dev) 678{ 679 return (CARD_COMPAT_MATCH(dev)); 680} 681 682static int 683pccard_compat_do_attach(device_t bus, device_t dev) 684{ 685 int err; 686 687 err = CARD_COMPAT_PROBE(dev); 688 if (err == 0) 689 err = CARD_COMPAT_ATTACH(dev); 690 return (err); 691} 692 693#define PCCARD_NPORT 2 694#define PCCARD_NMEM 5 695#define PCCARD_NIRQ 1 696#define PCCARD_NDRQ 0 697 698static int 699pccard_add_children(device_t dev, int busno) 700{ 701 /* Call parent to scan for any current children */ 702 return (0); 703} 704 705static int 706pccard_probe(device_t dev) 707{ 708 device_set_desc(dev, "16-bit PCCard bus"); 709 return (pccard_add_children(dev, device_get_unit(dev))); 710} 711 712static int 713pccard_attach(device_t dev) 714{ 715 struct pccard_softc *sc = PCCARD_SOFTC(dev); 716 717 sc->dev = dev; 718 sc->sc_enabled_count = 0; 719 return (bus_generic_attach(dev)); 720} 721 722static void 723pccard_print_resources(struct resource_list *rl, const char *name, int type, 724 int count, const char *format) 725{ 726 struct resource_list_entry *rle; 727 int printed; 728 int i; 729 730 printed = 0; 731 for (i = 0; i < count; i++) { 732 rle = resource_list_find(rl, type, i); 733 if (rle) { 734 if (printed == 0) 735 printf(" %s ", name); 736 else if (printed > 0) 737 printf(","); 738 printed++; 739 printf(format, rle->start); 740 if (rle->count > 1) { 741 printf("-"); 742 printf(format, rle->start + rle->count - 1); 743 } 744 } else if (i > 3) { 745 /* check the first few regardless */ 746 break; 747 } 748 } 749} 750 751static int 752pccard_print_child(device_t dev, device_t child) 753{ 754 struct pccard_ivar *devi = PCCARD_IVAR(child); 755 struct resource_list *rl = &devi->resources; 756 int retval = 0; 757 758 retval += bus_print_child_header(dev, child); 759 retval += printf(" at"); 760 761 if (devi) { 762 pccard_print_resources(rl, "port", SYS_RES_IOPORT, 763 PCCARD_NPORT, "%#lx"); 764 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY, 765 PCCARD_NMEM, "%#lx"); 766 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ, 767 "%ld"); 768 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ, 769 "%ld"); 770 retval += printf(" function %d config %d", devi->fcn->number, 771 devi->fcn->cfe->number); 772 } 773 774 retval += bus_print_child_footer(dev, child); 775 776 return (retval); 777} 778 779static int 780pccard_set_resource(device_t dev, device_t child, int type, int rid, 781 u_long start, u_long count) 782{ 783 struct pccard_ivar *devi = PCCARD_IVAR(child); 784 struct resource_list *rl = &devi->resources; 785 786 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 787 && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 788 return (EINVAL); 789 if (rid < 0) 790 return (EINVAL); 791 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT) 792 return (EINVAL); 793 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM) 794 return (EINVAL); 795 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ) 796 return (EINVAL); 797 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ) 798 return (EINVAL); 799 800 resource_list_add(rl, type, rid, start, start + count - 1, count); 801 802 return (0); 803} 804 805static int 806pccard_get_resource(device_t dev, device_t child, int type, int rid, 807 u_long *startp, u_long *countp) 808{ 809 struct pccard_ivar *devi = PCCARD_IVAR(child); 810 struct resource_list *rl = &devi->resources; 811 struct resource_list_entry *rle; 812 813 rle = resource_list_find(rl, type, rid); 814 if (!rle) 815 return (ENOENT); 816 817 if (startp) 818 *startp = rle->start; 819 if (countp) 820 *countp = rle->count; 821 822 return (0); 823} 824 825static void 826pccard_delete_resource(device_t dev, device_t child, int type, int rid) 827{ 828 struct pccard_ivar *devi = PCCARD_IVAR(child); 829 struct resource_list *rl = &devi->resources; 830 resource_list_delete(rl, type, rid); 831} 832 833static int 834pccard_set_res_flags(device_t dev, device_t child, int type, int rid, 835 u_int32_t flags) 836{ 837 return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type, 838 rid, flags)); 839} 840 841static int 842pccard_set_memory_offset(device_t dev, device_t child, int rid, 843 u_int32_t offset, u_int32_t *deltap) 844 845{ 846 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid, 847 offset, deltap)); 848} 849 850static int 851pccard_read_ivar(device_t bus, device_t child, int which, u_char *result) 852{ 853 struct pccard_ivar *devi = PCCARD_IVAR(child); 854 struct pccard_function *func = devi->fcn; 855 struct pccard_softc *sc = PCCARD_SOFTC(bus); 856 857 /* PCCARD_IVAR_ETHADDR unhandled from oldcard */ 858 switch (which) { 859 default: 860 case PCCARD_IVAR_ETHADDR: 861 return (ENOENT); 862 break; 863 case PCCARD_IVAR_VENDOR: 864 *(u_int32_t *) result = sc->card.manufacturer; 865 break; 866 case PCCARD_IVAR_PRODUCT: 867 *(u_int32_t *) result = sc->card.product; 868 break; 869 case PCCARD_IVAR_FUNCTION_NUMBER: 870 if (!func) { 871 device_printf(bus, "No function number, bug!\n"); 872 return (ENOENT); 873 } 874 *(u_int32_t *) result = func->number; 875 break; 876 case PCCARD_IVAR_VENDOR_STR: 877 *(char **) result = sc->card.cis1_info[0]; 878 break; 879 case PCCARD_IVAR_PRODUCT_STR: 880 *(char **) result = sc->card.cis1_info[1]; 881 break; 882 case PCCARD_IVAR_CIS3_STR: 883 *(char **) result = sc->card.cis1_info[2]; 884 break; 885 case PCCARD_IVAR_CIS4_STR: 886 *(char **) result = sc->card.cis1_info[2]; 887 break; 888 } 889 return (0); 890} 891 892static void 893pccard_driver_added(device_t dev, driver_t *driver) 894{ 895 /* 896 * XXX eventually we need to attach stuff when we know we 897 * XXX have kids. For now we do nothing because we normally 898 * XXX add children ourselves. We don't want to necessarily 899 * XXX force a reprobe. 900 */ 901} 902 903static struct resource * 904pccard_alloc_resource(device_t dev, device_t child, int type, int *rid, 905 u_long start, u_long end, u_long count, u_int flags) 906{ 907 struct resource_list_entry *rle = NULL; 908 909 /* XXX: This is an ugly way to fudge the resources. */ 910 911 if (device_get_parent(child) == dev) { 912 struct pccard_ivar *devi = PCCARD_IVAR(child); 913 struct resource_list *rl = &devi->resources; 914 915 rle = resource_list_find(rl, type, *rid); 916 } 917 918 if (rle != NULL) { 919 if (flags & RF_ACTIVE) 920 bus_activate_resource(dev, type, rle->rid, rle->res); 921 return (rle->res); 922 } 923 return (bus_generic_alloc_resource(dev, child, type, rid, start, 924 end, count, flags)); 925} 926 927static int 928pccard_release_resource(device_t dev, device_t child, int type, int rid, 929 struct resource *r) 930{ 931 struct resource_list_entry *rle = NULL; 932 933 if (device_get_parent(child) == dev) { 934 struct pccard_ivar *devi = PCCARD_IVAR(child); 935 struct resource_list *rl = &devi->resources; 936 937 rle = resource_list_find(rl, type, rid); 938 } 939 940 if (rle != NULL) { 941 return (bus_deactivate_resource(dev, type, rle->rid, rle->res)); 942 } 943 944 return (bus_generic_release_resource(dev, child, type, rid, r)); 945} 946 947static int 948pccard_activate_resource(device_t dev, device_t child, int type, int rid, 949 struct resource *r) 950{ 951 /* XXX need to write to the COR to activate this for mf cards */ 952 struct resource_list_entry *rle = NULL; 953 954 if (device_get_parent(child) == dev) { 955 struct pccard_ivar *devi = PCCARD_IVAR(child); 956 struct resource_list *rl = &devi->resources; 957 958 rle = resource_list_find(rl, type, rid); 959 } 960 961 if (rle != NULL) { 962 return (bus_activate_resource(dev, type, rle->rid, rle->res)); 963 } 964 965 return (bus_generic_activate_resource(dev, child, type, rid, r)); 966} 967 968static int 969pccard_deactivate_resource(device_t dev, device_t child, int type, int rid, 970 struct resource *r) 971{ 972 /* XXX need to write to the COR to deactivate this for mf cards */ 973 struct resource_list_entry *rle = NULL; 974 975 if (device_get_parent(child) == dev) { 976 struct pccard_ivar *devi = PCCARD_IVAR(child); 977 struct resource_list *rl = &devi->resources; 978 979 rle = resource_list_find(rl, type, rid); 980 } 981 982 if (rle != NULL) { 983 return (bus_deactivate_resource(dev, type, rle->rid, rle->res)); 984 } 985 986 return (bus_generic_deactivate_resource(dev, child, type, rid, r)); 987} 988 989static void 990pccard_child_detached(device_t parent, device_t dev) 991{ 992 struct pccard_ivar *ivar = PCCARD_IVAR(dev); 993 994 if (parent == device_get_parent(dev)) 995 free(ivar, M_DEVBUF); 996} 997 998static void 999pccard_intr(void *arg) 1000{ 1001 struct pccard_softc *sc = (struct pccard_softc *) arg; 1002 struct pccard_function *pf; 1003 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 1004 if (pf->intr_handler != NULL) { 1005 int reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS); 1006 if (reg & PCCARD_CCR_STATUS_INTR) { 1007 pccard_ccr_write(pf, PCCARD_CCR_STATUS, 1008 reg & ~PCCARD_CCR_STATUS_INTR); 1009 pf->intr_handler(pf->intr_handler_arg); 1010 } 1011 } 1012 } 1013} 1014 1015static int 1016pccard_setup_intr(device_t dev, device_t child, struct resource *irq, 1017 int flags, driver_intr_t *intr, void *arg, void **cookiep) 1018{ 1019 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1020 struct pccard_function *func = ivar->fcn; 1021 struct resource_list_entry *rle = NULL; 1022 struct pccard_softc *sc = device_get_softc(dev); 1023 1024 if (func->intr_handler != NULL) 1025 panic("Only one interrupt handler per function allowed\n"); 1026 1027 rle = resource_list_find(&ivar->resources, SYS_RES_IRQ, 0); 1028 if (rle == NULL || rle->res != irq) 1029 panic("irq in setup_intr does not match allocated irq\n"); 1030 1031 func->intr_handler = intr; 1032 func->intr_handler_arg = arg; 1033 func->intr_handler_cookie = *cookiep = func; 1034 pccard_ccr_write(func, PCCARD_CCR_OPTION, 1035 pccard_ccr_read(func, PCCARD_CCR_OPTION) | 1036 PCCARD_CCR_OPTION_IREQ_ENABLE); 1037 1038 if (sc->intr_handler_count++ == 0) { 1039 rle = resource_list_find(&ivar->resources, SYS_RES_IRQ, 0); 1040 if (rle == NULL) 1041 panic("No IRQ for pccard?"); 1042 1043 bus_setup_intr(dev, rle->res, INTR_TYPE_TTY/* | INTR_FAST*/, 1044 pccard_intr, sc, (void*)&sc->intr_handler_count); 1045 } 1046 return (0); 1047} 1048 1049static int 1050pccard_teardown_intr(device_t dev, device_t child, struct resource *r, 1051 void *cookie) 1052{ 1053 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1054 struct pccard_function *func = ivar->fcn; 1055 struct pccard_softc *sc = device_get_softc(dev); 1056 1057 if (func->intr_handler_cookie != cookie) 1058 panic("pccard teardown of unknown interrupt handler\n"); 1059 1060 func->intr_handler = NULL; 1061 func->intr_handler_arg = NULL; 1062 func->intr_handler_cookie = NULL; 1063 pccard_ccr_write(func, PCCARD_CCR_OPTION, 1064 pccard_ccr_read(func, PCCARD_CCR_OPTION) & 1065 ~PCCARD_CCR_OPTION_IREQ_ENABLE); 1066 1067 if (--sc->intr_handler_count == 0) { 1068 struct resource_list_entry *rle = NULL; 1069 1070 rle = resource_list_find(&ivar->resources, SYS_RES_IRQ, 0); 1071 if (rle == NULL) 1072 panic("No IRQ for pccard?"); 1073 1074 bus_teardown_intr(dev, rle->res, &sc->intr_handler_count); 1075 } 1076 return (0); 1077} 1078 1079static device_method_t pccard_methods[] = { 1080 /* Device interface */ 1081 DEVMETHOD(device_probe, pccard_probe), 1082 DEVMETHOD(device_attach, pccard_attach), 1083 DEVMETHOD(device_detach, bus_generic_detach), 1084 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1085 DEVMETHOD(device_suspend, bus_generic_suspend), 1086 DEVMETHOD(device_resume, bus_generic_resume), 1087 1088 /* Bus interface */ 1089 DEVMETHOD(bus_print_child, pccard_print_child), 1090 DEVMETHOD(bus_driver_added, pccard_driver_added), 1091 DEVMETHOD(bus_child_detached, pccard_child_detached), 1092 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource), 1093 DEVMETHOD(bus_release_resource, pccard_release_resource), 1094 DEVMETHOD(bus_activate_resource, pccard_activate_resource), 1095 DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource), 1096 DEVMETHOD(bus_setup_intr, pccard_setup_intr), 1097 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr), 1098 DEVMETHOD(bus_set_resource, pccard_set_resource), 1099 DEVMETHOD(bus_get_resource, pccard_get_resource), 1100 DEVMETHOD(bus_delete_resource, pccard_delete_resource), 1101 DEVMETHOD(bus_read_ivar, pccard_read_ivar), 1102 1103 /* Card Interface */ 1104 DEVMETHOD(card_set_res_flags, pccard_set_res_flags), 1105 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset), 1106 DEVMETHOD(card_get_type, pccard_card_gettype), 1107 DEVMETHOD(card_attach_card, pccard_attach_card), 1108 DEVMETHOD(card_detach_card, pccard_detach_card), 1109 DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe), 1110 DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach), 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