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