pccard.c revision 150468
1/* $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $ */ 2 3/*- 4 * Copyright (c) 1997 Marc Horowitz. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Marc Horowitz. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: head/sys/dev/pccard/pccard.c 150468 2005-09-22 14:51:11Z imp $"); 34 35#include <sys/param.h> 36#include <sys/systm.h> 37#include <sys/malloc.h> 38#include <sys/module.h> 39#include <sys/kernel.h> 40#include <sys/queue.h> 41#include <sys/sysctl.h> 42#include <sys/types.h> 43 44#include <sys/bus.h> 45#include <machine/bus.h> 46#include <sys/rman.h> 47#include <machine/resource.h> 48 49#include <net/ethernet.h> 50 51#include <dev/pccard/pccardreg.h> 52#include <dev/pccard/pccardvar.h> 53#include <dev/pccard/pccardvarp.h> 54#include <dev/pccard/pccard_cis.h> 55 56#include "power_if.h" 57#include "card_if.h" 58 59#define PCCARDDEBUG 60 61/* sysctl vars */ 62SYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters"); 63 64int pccard_debug = 0; 65TUNABLE_INT("hw.pccard.debug", &pccard_debug); 66SYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW, 67 &pccard_debug, 0, 68 "pccard debug"); 69 70int pccard_cis_debug = 0; 71TUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug); 72SYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW, 73 &pccard_cis_debug, 0, "pccard CIS debug"); 74 75#ifdef PCCARDDEBUG 76#define DPRINTF(arg) if (pccard_debug) printf arg 77#define DEVPRINTF(arg) if (pccard_debug) device_printf arg 78#define PRVERBOSE(arg) printf arg 79#define DEVPRVERBOSE(arg) device_printf arg 80#else 81#define DPRINTF(arg) 82#define DEVPRINTF(arg) 83#define PRVERBOSE(arg) if (bootverbose) printf arg 84#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg 85#endif 86 87static int pccard_ccr_read(struct pccard_function *pf, int ccr); 88static void pccard_ccr_write(struct pccard_function *pf, int ccr, int val); 89static int pccard_attach_card(device_t dev); 90static int pccard_detach_card(device_t dev); 91static void pccard_function_init(struct pccard_function *pf); 92static void pccard_function_free(struct pccard_function *pf); 93static int pccard_function_enable(struct pccard_function *pf); 94static void pccard_function_disable(struct pccard_function *pf); 95static int pccard_probe(device_t dev); 96static int pccard_attach(device_t dev); 97static int pccard_detach(device_t dev); 98static void pccard_print_resources(struct resource_list *rl, 99 const char *name, int type, int count, const char *format); 100static int pccard_print_child(device_t dev, device_t child); 101static int pccard_set_resource(device_t dev, device_t child, int type, 102 int rid, u_long start, u_long count); 103static int pccard_get_resource(device_t dev, device_t child, int type, 104 int rid, u_long *startp, u_long *countp); 105static void pccard_delete_resource(device_t dev, device_t child, int type, 106 int rid); 107static int pccard_set_res_flags(device_t dev, device_t child, int type, 108 int rid, uint32_t flags); 109static int pccard_set_memory_offset(device_t dev, device_t child, int rid, 110 uint32_t offset, uint32_t *deltap); 111static void pccard_probe_nomatch(device_t cbdev, device_t child); 112static int pccard_read_ivar(device_t bus, device_t child, int which, 113 u_char *result); 114static void pccard_driver_added(device_t dev, driver_t *driver); 115static struct resource *pccard_alloc_resource(device_t dev, 116 device_t child, int type, int *rid, u_long start, 117 u_long end, u_long count, u_int flags); 118static int pccard_release_resource(device_t dev, device_t child, int type, 119 int rid, struct resource *r); 120static void pccard_child_detached(device_t parent, device_t dev); 121static void pccard_intr(void *arg); 122static int pccard_setup_intr(device_t dev, device_t child, 123 struct resource *irq, int flags, driver_intr_t *intr, 124 void *arg, void **cookiep); 125static int pccard_teardown_intr(device_t dev, device_t child, 126 struct resource *r, void *cookie); 127 128static const struct pccard_product * 129pccard_do_product_lookup(device_t bus, device_t dev, 130 const struct pccard_product *tab, size_t ent_size, 131 pccard_product_match_fn matchfn); 132 133 134static int 135pccard_ccr_read(struct pccard_function *pf, int ccr) 136{ 137 return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 138 pf->pf_ccr_offset + ccr)); 139} 140 141static void 142pccard_ccr_write(struct pccard_function *pf, int ccr, int val) 143{ 144 if ((pf->ccr_mask) & (1 << (ccr / 2))) { 145 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 146 pf->pf_ccr_offset + ccr, val); 147 } 148} 149 150static int 151pccard_set_default_descr(device_t dev) 152{ 153 const char *vendorstr, *prodstr; 154 uint32_t vendor, prod; 155 char *str; 156 157 if (pccard_get_vendor_str(dev, &vendorstr)) 158 return (0); 159 if (pccard_get_product_str(dev, &prodstr)) 160 return (0); 161 if (vendorstr != NULL && prodstr != NULL) { 162 str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF, 163 M_WAITOK); 164 sprintf(str, "%s %s", vendorstr, prodstr); 165 device_set_desc_copy(dev, str); 166 free(str, M_DEVBUF); 167 } else { 168 if (pccard_get_vendor(dev, &vendor)) 169 return (0); 170 if (pccard_get_product(dev, &prod)) 171 return (0); 172 str = malloc(100, M_DEVBUF, M_WAITOK); 173 snprintf(str, 100, "vendor=0x%x product=0x%x", vendor, prod); 174 device_set_desc_copy(dev, str); 175 free(str, M_DEVBUF); 176 } 177 return (0); 178} 179 180static int 181pccard_attach_card(device_t dev) 182{ 183 struct pccard_softc *sc = PCCARD_SOFTC(dev); 184 struct pccard_function *pf; 185 struct pccard_ivar *ivar; 186 device_t child; 187 int i; 188 189 /* 190 * this is here so that when socket_enable calls gettype, trt happens 191 */ 192 STAILQ_INIT(&sc->card.pf_head); 193 194 DEVPRINTF((dev, "chip_socket_enable\n")); 195 POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 196 197 DEVPRINTF((dev, "read_cis\n")); 198 pccard_read_cis(sc); 199 200 DEVPRINTF((dev, "check_cis_quirks\n")); 201 pccard_check_cis_quirks(dev); 202 203 /* 204 * bail now if the card has no functions, or if there was an error in 205 * the cis. 206 */ 207 208 if (sc->card.error) { 209 device_printf(dev, "CARD ERROR!\n"); 210 return (1); 211 } 212 if (STAILQ_EMPTY(&sc->card.pf_head)) { 213 device_printf(dev, "Card has no functions!\n"); 214 return (1); 215 } 216 217 if (bootverbose || pccard_debug) 218 pccard_print_cis(dev); 219 220 DEVPRINTF((dev, "functions scanning\n")); 221 i = -1; 222 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 223 i++; 224 if (STAILQ_EMPTY(&pf->cfe_head)) { 225 device_printf(dev, 226 "Function %d has no config entries.!\n", i); 227 continue; 228 } 229 pf->sc = sc; 230 pf->cfe = NULL; 231 pf->dev = NULL; 232 } 233 DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1, 234 pccard_mfc(sc))); 235 236 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 237 if (STAILQ_EMPTY(&pf->cfe_head)) 238 continue; 239 /* 240 * In NetBSD, the drivers are responsible for activating 241 * each function of a card. I think that in FreeBSD we 242 * want to activate them enough for the usual bus_*_resource 243 * routines will do the right thing. This many mean a 244 * departure from the current NetBSD model. 245 * 246 * This seems to work well in practice for most cards. 247 * However, there are two cases that are problematic. 248 * If a driver wishes to pick and chose which config 249 * entry to use, then this method falls down. These 250 * are usually older cards. In addition, there are 251 * some cards that have multiple hardware units on the 252 * cards, but presents only one CIS chain. These cards 253 * are combination cards, but only one of these units 254 * can be on at a time. 255 */ 256 ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, 257 M_WAITOK | M_ZERO); 258 resource_list_init(&ivar->resources); 259 child = device_add_child(dev, NULL, -1); 260 device_set_ivars(child, ivar); 261 ivar->pf = pf; 262 pf->dev = child; 263 /* 264 * XXX We might want to move the next three lines into 265 * XXX the pccard interface layer. For the moment, this 266 * XXX is OK, but some drivers want to pick the config 267 * XXX entry to use as well as some address tweaks (mostly 268 * XXX due to bugs in decode logic that makes some 269 * XXX addresses illegal or broken). 270 */ 271 pccard_function_init(pf); 272 if (sc->sc_enabled_count == 0) 273 POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 274 if (pccard_function_enable(pf) == 0 && 275 pccard_set_default_descr(child) == 0 && 276 device_probe_and_attach(child) == 0) { 277 DEVPRINTF((sc->dev, "function %d CCR at %d " 278 "offset %x mask %x: " 279 "%x %x %x %x, %x %x %x %x, %x\n", 280 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 281 pf->ccr_mask, pccard_ccr_read(pf, 0x00), 282 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 283 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 284 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 285 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 286 } else { 287 if (pf->cfe != NULL) 288 pccard_function_disable(pf); 289 } 290 } 291 return (0); 292} 293 294static int 295pccard_detach_card(device_t dev) 296{ 297 struct pccard_softc *sc = PCCARD_SOFTC(dev); 298 struct pccard_function *pf; 299 struct pccard_config_entry *cfe; 300 int state; 301 302 /* 303 * We are running on either the PCCARD socket's event thread 304 * or in user context detaching a device by user request. 305 */ 306 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 307 if (pf->dev == NULL) 308 continue; 309 state = device_get_state(pf->dev); 310 if (state == DS_ATTACHED || state == DS_BUSY) 311 device_detach(pf->dev); 312 if (pf->cfe != NULL) 313 pccard_function_disable(pf); 314 pccard_function_free(pf); 315 device_delete_child(dev, pf->dev); 316 } 317 if (sc->sc_enabled_count == 0) 318 POWER_DISABLE_SOCKET(device_get_parent(dev), dev); 319 320 while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) { 321 while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) { 322 STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list); 323 free(cfe, M_DEVBUF); 324 } 325 STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list); 326 free(pf, M_DEVBUF); 327 } 328 return (0); 329} 330 331static const struct pccard_product * 332pccard_do_product_lookup(device_t bus, device_t dev, 333 const struct pccard_product *tab, size_t ent_size, 334 pccard_product_match_fn matchfn) 335{ 336 const struct pccard_product *ent; 337 int matches; 338 uint32_t vendor; 339 uint32_t prod; 340 const char *vendorstr; 341 const char *prodstr; 342 const char *cis3str; 343 const char *cis4str; 344 345#ifdef DIAGNOSTIC 346 if (sizeof *ent > ent_size) 347 panic("pccard_product_lookup: bogus ent_size %jd", 348 (intmax_t) ent_size); 349#endif 350 if (pccard_get_vendor(dev, &vendor)) 351 return (NULL); 352 if (pccard_get_product(dev, &prod)) 353 return (NULL); 354 if (pccard_get_vendor_str(dev, &vendorstr)) 355 return (NULL); 356 if (pccard_get_product_str(dev, &prodstr)) 357 return (NULL); 358 if (pccard_get_cis3_str(dev, &cis3str)) 359 return (NULL); 360 if (pccard_get_cis4_str(dev, &cis4str)) 361 return (NULL); 362 for (ent = tab; ent->pp_vendor != 0; ent = 363 (const struct pccard_product *) ((const char *) ent + ent_size)) { 364 matches = 1; 365 if (ent->pp_vendor == PCCARD_VENDOR_ANY && 366 ent->pp_product == PCCARD_PRODUCT_ANY && 367 ent->pp_cis[0] == NULL && 368 ent->pp_cis[1] == NULL) { 369 if (ent->pp_name) 370 device_printf(dev, 371 "Total wildcard entry ignored for %s\n", 372 ent->pp_name); 373 continue; 374 } 375 if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY && 376 vendor != ent->pp_vendor) 377 matches = 0; 378 if (matches && ent->pp_product != PCCARD_PRODUCT_ANY && 379 prod != ent->pp_product) 380 matches = 0; 381 if (matches && ent->pp_cis[0] && 382 (vendorstr == NULL || 383 strcmp(ent->pp_cis[0], vendorstr) != 0)) 384 matches = 0; 385 if (matches && ent->pp_cis[1] && 386 (prodstr == NULL || 387 strcmp(ent->pp_cis[1], prodstr) != 0)) 388 matches = 0; 389 if (matches && ent->pp_cis[2] && 390 (cis3str == NULL || 391 strcmp(ent->pp_cis[2], cis3str) != 0)) 392 matches = 0; 393 if (matches && ent->pp_cis[3] && 394 (cis4str == NULL || 395 strcmp(ent->pp_cis[3], cis4str) != 0)) 396 matches = 0; 397 if (matchfn != NULL) 398 matches = (*matchfn)(dev, ent, matches); 399 if (matches) 400 return (ent); 401 } 402 return (NULL); 403} 404 405/* 406 * Initialize a PCCARD function. May be called as long as the function is 407 * disabled. 408 * 409 * Note: pccard_function_init should not keep resources allocated. It should 410 * only set them up ala isa pnp, set the values in the rl lists, and return. 411 * Any resource held after pccard_function_init is called is a bug. However, 412 * the bus routines to get the resources also assume that pccard_function_init 413 * does this, so they need to be fixed too. 414 */ 415static void 416pccard_function_init(struct pccard_function *pf) 417{ 418 struct pccard_config_entry *cfe; 419 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 420 struct resource_list *rl = &devi->resources; 421 struct resource_list_entry *rle; 422 struct resource *r = 0; 423 device_t bus; 424 u_long start, end, len; 425 int i, rid, spaces; 426 427 if (pf->pf_flags & PFF_ENABLED) { 428 printf("pccard_function_init: function is enabled"); 429 return; 430 } 431 bus = device_get_parent(pf->dev); 432 /* Remember which configuration entry we are using. */ 433 STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) { 434 if (cfe->iftype != PCCARD_IFTYPE_IO) 435 continue; 436 spaces = 0; 437 for (i = 0; i < cfe->num_iospace; i++) { 438 start = cfe->iospace[i].start; 439 if (start) 440 end = start + cfe->iospace[i].length - 1; 441 else 442 end = ~0UL; 443 DEVPRINTF((bus, "I/O rid %d start %lx end %lx\n", 444 i, start, end)); 445 rid = i; 446 len = cfe->iospace[i].length; 447 r = bus_alloc_resource(bus, SYS_RES_IOPORT, &rid, 448 start, end, len, rman_make_alignment_flags(len)); 449 if (r == NULL) 450 goto not_this_one; 451 rle = resource_list_add(rl, SYS_RES_IOPORT, 452 rid, rman_get_start(r), rman_get_end(r), 453 cfe->iospace[i].length); 454 if (rle == NULL) 455 panic("Cannot add resource rid %d IOPORT", rid); 456 rle->res = r; 457 spaces++; 458 } 459 for (i = 0; i < cfe->num_memspace; i++) { 460 start = cfe->memspace[i].hostaddr; 461 if (start) 462 end = start + cfe->memspace[i].length - 1; 463 else 464 end = ~0UL; 465 DEVPRINTF((bus, "Memory rid %d start %lx end %lx\n", 466 i, start, end)); 467 rid = i; 468 len = cfe->memspace[i].length; 469 r = bus_alloc_resource(bus, SYS_RES_MEMORY, &rid, 470 start, end, len, rman_make_alignment_flags(len)); 471 if (r == NULL) 472 goto not_this_one; 473 rle = resource_list_add(rl, SYS_RES_MEMORY, 474 rid, rman_get_start(r), rman_get_end(r), 475 cfe->memspace[i].length); 476 if (rle == NULL) 477 panic("Cannot add resource rid %d MEM", rid); 478 rle->res = r; 479 spaces++; 480 } 481 if (spaces == 0) { 482 DEVPRINTF((bus, "Neither memory nor I/O mapped\n")); 483 goto not_this_one; 484 } 485 if (cfe->irqmask) { 486 rid = 0; 487 r = bus_alloc_resource_any(bus, SYS_RES_IRQ, &rid, 488 RF_SHAREABLE); 489 if (r == NULL) 490 goto not_this_one; 491 rle = resource_list_add(rl, SYS_RES_IRQ, rid, 492 rman_get_start(r), rman_get_end(r), 1); 493 if (rle == NULL) 494 panic("Cannot add resource rid %d IRQ", rid); 495 rle->res = r; 496 } 497 /* If we get to here, we've allocated all we need */ 498 pf->cfe = cfe; 499 break; 500 not_this_one:; 501 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n", 502 cfe->number)); 503 resource_list_purge(rl); 504 } 505} 506 507/* 508 * Free resources allocated by pccard_function_init(), May be called as long 509 * as the function is disabled. 510 * 511 * NOTE: This function should be unnecessary. pccard_function_init should 512 * never keep resources initialized. 513 */ 514static void 515pccard_function_free(struct pccard_function *pf) 516{ 517 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 518 struct resource_list_entry *rle; 519 520 if (pf->pf_flags & PFF_ENABLED) { 521 printf("pccard_function_init: function is enabled"); 522 return; 523 } 524 525 STAILQ_FOREACH(rle, &devi->resources, link) { 526 if (rle->res) { 527 if (rman_get_device(rle->res) != pf->sc->dev) 528 device_printf(pf->sc->dev, 529 "function_free: Resource still owned by " 530 "child, oops. " 531 "(type=%d, rid=%d, addr=%lx)\n", 532 rle->type, rle->rid, 533 rman_get_start(rle->res)); 534 BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev), 535 pf->sc->dev, rle->type, rle->rid, rle->res); 536 rle->res = NULL; 537 } 538 } 539 resource_list_free(&devi->resources); 540} 541 542static void 543pccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr, 544 bus_addr_t offset, bus_size_t size) 545{ 546 bus_size_t iosize, tmp; 547 548 if (addr != 0) { 549 if (pf->pf_mfc_iomax == 0) { 550 pf->pf_mfc_iobase = addr + offset; 551 pf->pf_mfc_iomax = pf->pf_mfc_iobase + size; 552 } else { 553 /* this makes the assumption that nothing overlaps */ 554 if (pf->pf_mfc_iobase > addr + offset) 555 pf->pf_mfc_iobase = addr + offset; 556 if (pf->pf_mfc_iomax < addr + offset + size) 557 pf->pf_mfc_iomax = addr + offset + size; 558 } 559 } 560 561 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase; 562 /* round up to nearest (2^n)-1 */ 563 for (iosize = 1; iosize < tmp; iosize <<= 1) 564 ; 565 iosize--; 566 567 DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n", 568 (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1))); 569 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0, 570 pf->pf_mfc_iobase & 0xff); 571 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1, 572 (pf->pf_mfc_iobase >> 8) & 0xff); 573 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0); 574 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0); 575 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize); 576} 577 578/* Enable a PCCARD function */ 579static int 580pccard_function_enable(struct pccard_function *pf) 581{ 582 struct pccard_function *tmp; 583 int reg; 584 device_t dev = pf->sc->dev; 585 586 if (pf->cfe == NULL) { 587 DEVPRVERBOSE((dev, "No config entry could be allocated.\n")); 588 return (ENOMEM); 589 } 590 591 /* 592 * Increase the reference count on the socket, enabling power, if 593 * necessary. 594 */ 595 pf->sc->sc_enabled_count++; 596 597 if (pf->pf_flags & PFF_ENABLED) { 598 /* 599 * Don't do anything if we're already enabled. 600 */ 601 return (0); 602 } 603 604 /* 605 * it's possible for different functions' CCRs to be in the same 606 * underlying page. Check for that. 607 */ 608 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 609 if ((tmp->pf_flags & PFF_ENABLED) && 610 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 611 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 612 (tmp->ccr_base - tmp->pf_ccr_offset + 613 tmp->pf_ccr_realsize))) { 614 pf->pf_ccrt = tmp->pf_ccrt; 615 pf->pf_ccrh = tmp->pf_ccrh; 616 pf->pf_ccr_realsize = tmp->pf_ccr_realsize; 617 618 /* 619 * pf->pf_ccr_offset = (tmp->pf_ccr_offset - 620 * tmp->ccr_base) + pf->ccr_base; 621 */ 622 /* pf->pf_ccr_offset = 623 (tmp->pf_ccr_offset + pf->ccr_base) - 624 tmp->ccr_base; */ 625 pf->pf_ccr_window = tmp->pf_ccr_window; 626 break; 627 } 628 } 629 if (tmp == NULL) { 630 pf->ccr_rid = 0; 631 pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 632 &pf->ccr_rid, 0, ~0, PCCARD_MEM_PAGE_SIZE, RF_ACTIVE); 633 if (!pf->ccr_res) 634 goto bad; 635 DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n", 636 rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res), 637 pf->ccr_base)); 638 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, 639 pf->ccr_rid, PCCARD_A_MEM_ATTR); 640 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev, 641 pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset); 642 pf->pf_ccrt = rman_get_bustag(pf->ccr_res); 643 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res); 644 pf->pf_ccr_realsize = 1; 645 } 646 647 reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX); 648 reg |= PCCARD_CCR_OPTION_LEVIREQ; 649 if (pccard_mfc(pf->sc)) { 650 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE | 651 PCCARD_CCR_OPTION_ADDR_DECODE); 652 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */ 653 } 654 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg); 655 656 reg = 0; 657 if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0) 658 reg |= PCCARD_CCR_STATUS_IOIS8; 659 if (pf->cfe->flags & PCCARD_CFE_AUDIO) 660 reg |= PCCARD_CCR_STATUS_AUDIO; 661 pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg); 662 663 pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0); 664 665 if (pccard_mfc(pf->sc)) 666 pccard_mfc_adjust_iobase(pf, 0, 0, 0); 667 668#ifdef PCCARDDEBUG 669 if (pccard_debug) { 670 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 671 device_printf(tmp->sc->dev, 672 "function %d CCR at %d offset %x: " 673 "%x %x %x %x, %x %x %x %x, %x\n", 674 tmp->number, tmp->pf_ccr_window, 675 tmp->pf_ccr_offset, 676 pccard_ccr_read(tmp, 0x00), 677 pccard_ccr_read(tmp, 0x02), 678 pccard_ccr_read(tmp, 0x04), 679 pccard_ccr_read(tmp, 0x06), 680 pccard_ccr_read(tmp, 0x0A), 681 pccard_ccr_read(tmp, 0x0C), 682 pccard_ccr_read(tmp, 0x0E), 683 pccard_ccr_read(tmp, 0x10), 684 pccard_ccr_read(tmp, 0x12)); 685 } 686 } 687#endif 688 pf->pf_flags |= PFF_ENABLED; 689 return (0); 690 691 bad: 692 /* 693 * Decrement the reference count, and power down the socket, if 694 * necessary. 695 */ 696 pf->sc->sc_enabled_count--; 697 DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count)); 698 699 return (1); 700} 701 702/* Disable PCCARD function. */ 703static void 704pccard_function_disable(struct pccard_function *pf) 705{ 706 struct pccard_function *tmp; 707 device_t dev = pf->sc->dev; 708 709 if (pf->cfe == NULL) 710 panic("pccard_function_disable: function not initialized"); 711 712 if ((pf->pf_flags & PFF_ENABLED) == 0) { 713 /* 714 * Don't do anything if we're already disabled. 715 */ 716 return; 717 } 718 719 if (pf->intr_handler != NULL) { 720 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 721 struct resource_list_entry *rle = 722 resource_list_find(&devi->resources, SYS_RES_IRQ, 0); 723 if (rle == NULL) 724 panic("Can't disable an interrupt with no IRQ res\n"); 725 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res, 726 pf->intr_handler_cookie); 727 } 728 729 /* 730 * it's possible for different functions' CCRs to be in the same 731 * underlying page. Check for that. Note we mark us as disabled 732 * first to avoid matching ourself. 733 */ 734 735 pf->pf_flags &= ~PFF_ENABLED; 736 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 737 if ((tmp->pf_flags & PFF_ENABLED) && 738 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 739 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 740 (tmp->ccr_base - tmp->pf_ccr_offset + 741 tmp->pf_ccr_realsize))) 742 break; 743 } 744 745 /* Not used by anyone else; unmap the CCR. */ 746 if (tmp == NULL) { 747 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid, 748 pf->ccr_res); 749 pf->ccr_res = NULL; 750 } 751 752 /* 753 * Decrement the reference count, and power down the socket, if 754 * necessary. 755 */ 756 pf->sc->sc_enabled_count--; 757} 758 759#define PCCARD_NPORT 2 760#define PCCARD_NMEM 5 761#define PCCARD_NIRQ 1 762#define PCCARD_NDRQ 0 763 764static int 765pccard_probe(device_t dev) 766{ 767 device_set_desc(dev, "16-bit PCCard bus"); 768 return (0); 769} 770 771static int 772pccard_attach(device_t dev) 773{ 774 struct pccard_softc *sc = PCCARD_SOFTC(dev); 775 int err; 776 777 sc->dev = dev; 778 sc->sc_enabled_count = 0; 779 if ((err = pccard_device_create(sc)) != 0) 780 return (err); 781 return (bus_generic_attach(dev)); 782} 783 784static int 785pccard_detach(device_t dev) 786{ 787 pccard_detach_card(dev); 788 pccard_device_destroy(device_get_softc(dev)); 789 return (0); 790} 791 792static int 793pccard_suspend(device_t self) 794{ 795 pccard_detach_card(self); 796 return (0); 797} 798 799static 800int 801pccard_resume(device_t self) 802{ 803 return (0); 804} 805 806static void 807pccard_print_resources(struct resource_list *rl, const char *name, int type, 808 int count, const char *format) 809{ 810 struct resource_list_entry *rle; 811 int printed; 812 int i; 813 814 printed = 0; 815 for (i = 0; i < count; i++) { 816 rle = resource_list_find(rl, type, i); 817 if (rle != NULL) { 818 if (printed == 0) 819 printf(" %s ", name); 820 else if (printed > 0) 821 printf(","); 822 printed++; 823 printf(format, rle->start); 824 if (rle->count > 1) { 825 printf("-"); 826 printf(format, rle->start + rle->count - 1); 827 } 828 } else if (i > 3) { 829 /* check the first few regardless */ 830 break; 831 } 832 } 833} 834 835static int 836pccard_print_child(device_t dev, device_t child) 837{ 838 struct pccard_ivar *devi = PCCARD_IVAR(child); 839 struct resource_list *rl = &devi->resources; 840 int retval = 0; 841 842 retval += bus_print_child_header(dev, child); 843 retval += printf(" at"); 844 845 if (devi != NULL) { 846 pccard_print_resources(rl, "port", SYS_RES_IOPORT, 847 PCCARD_NPORT, "%#lx"); 848 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY, 849 PCCARD_NMEM, "%#lx"); 850 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ, 851 "%ld"); 852 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ, 853 "%ld"); 854 retval += printf(" function %d config %d", devi->pf->number, 855 devi->pf->cfe->number); 856 } 857 858 retval += bus_print_child_footer(dev, child); 859 860 return (retval); 861} 862 863static int 864pccard_set_resource(device_t dev, device_t child, int type, int rid, 865 u_long start, u_long count) 866{ 867 struct pccard_ivar *devi = PCCARD_IVAR(child); 868 struct resource_list *rl = &devi->resources; 869 870 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 871 && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 872 return (EINVAL); 873 if (rid < 0) 874 return (EINVAL); 875 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT) 876 return (EINVAL); 877 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM) 878 return (EINVAL); 879 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ) 880 return (EINVAL); 881 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ) 882 return (EINVAL); 883 884 resource_list_add(rl, type, rid, start, start + count - 1, count); 885 if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev, 886 type, &rid, start, start + count - 1, count, 0)) 887 return 0; 888 else 889 return ENOMEM; 890} 891 892static int 893pccard_get_resource(device_t dev, device_t child, int type, int rid, 894 u_long *startp, u_long *countp) 895{ 896 struct pccard_ivar *devi = PCCARD_IVAR(child); 897 struct resource_list *rl = &devi->resources; 898 struct resource_list_entry *rle; 899 900 rle = resource_list_find(rl, type, rid); 901 if (rle == NULL) 902 return (ENOENT); 903 904 if (startp != NULL) 905 *startp = rle->start; 906 if (countp != NULL) 907 *countp = rle->count; 908 909 return (0); 910} 911 912static void 913pccard_delete_resource(device_t dev, device_t child, int type, int rid) 914{ 915 struct pccard_ivar *devi = PCCARD_IVAR(child); 916 struct resource_list *rl = &devi->resources; 917 resource_list_delete(rl, type, rid); 918} 919 920static int 921pccard_set_res_flags(device_t dev, device_t child, int type, int rid, 922 uint32_t flags) 923{ 924 return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type, 925 rid, flags)); 926} 927 928static int 929pccard_set_memory_offset(device_t dev, device_t child, int rid, 930 uint32_t offset, uint32_t *deltap) 931 932{ 933 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid, 934 offset, deltap)); 935} 936 937static void 938pccard_probe_nomatch(device_t bus, device_t child) 939{ 940 struct pccard_ivar *devi = PCCARD_IVAR(child); 941 struct pccard_function *pf = devi->pf; 942 struct pccard_softc *sc = PCCARD_SOFTC(bus); 943 int i; 944 945 device_printf(bus, "<unknown card>"); 946 printf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) " 947 "at function %d\n", sc->card.manufacturer, sc->card.product, 948 pf->function, pf->number); 949 device_printf(bus, " CIS info: "); 950 for (i = 0; sc->card.cis1_info[i] != NULL && i < 4; i++) 951 printf("%s%s", i > 0 ? ", " : "", sc->card.cis1_info[i]); 952 printf("\n"); 953 return; 954} 955 956static int 957pccard_child_location_str(device_t bus, device_t child, char *buf, 958 size_t buflen) 959{ 960 struct pccard_ivar *devi = PCCARD_IVAR(child); 961 struct pccard_function *pf = devi->pf; 962 963 snprintf(buf, buflen, "function=%d", pf->number); 964 return (0); 965} 966 967/* XXX Maybe this should be in subr_bus? */ 968static void 969pccard_safe_quote(char *dst, const char *src, size_t len) 970{ 971 char *walker = dst, *ep = dst + len - 1; 972 973 if (len == 0) 974 return; 975 while (walker < ep) 976 { 977 if (*src == '"') { 978 if (ep - walker < 2) 979 break; 980 *walker++ = '\\'; 981 } 982 *walker++ = *src++; 983 } 984 *walker = '\0'; 985} 986 987static int 988pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf, 989 size_t buflen) 990{ 991 struct pccard_ivar *devi = PCCARD_IVAR(child); 992 struct pccard_function *pf = devi->pf; 993 struct pccard_softc *sc = PCCARD_SOFTC(bus); 994 char cis0[128], cis1[128]; 995 996 pccard_safe_quote(cis0, sc->card.cis1_info[0], sizeof(cis0)); 997 pccard_safe_quote(cis1, sc->card.cis1_info[1], sizeof(cis1)); 998 snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x " 999 "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d", 1000 sc->card.manufacturer, sc->card.product, cis0, cis1, pf->function); 1001 return (0); 1002} 1003 1004static int 1005pccard_read_ivar(device_t bus, device_t child, int which, u_char *result) 1006{ 1007 struct pccard_ivar *devi = PCCARD_IVAR(child); 1008 struct pccard_function *pf = devi->pf; 1009 struct pccard_softc *sc = PCCARD_SOFTC(bus); 1010 1011 if (!pf) 1012 panic("No pccard function pointer"); 1013 switch (which) { 1014 default: 1015 return (EINVAL); 1016 case PCCARD_IVAR_ETHADDR: 1017 bcopy(pf->pf_funce_lan_nid, result, ETHER_ADDR_LEN); 1018 break; 1019 case PCCARD_IVAR_VENDOR: 1020 *(uint32_t *)result = sc->card.manufacturer; 1021 break; 1022 case PCCARD_IVAR_PRODUCT: 1023 *(uint32_t *)result = sc->card.product; 1024 break; 1025 case PCCARD_IVAR_PRODEXT: 1026 *(uint16_t *)result = sc->card.prodext; 1027 break; 1028 case PCCARD_IVAR_FUNCTION: 1029 *(uint32_t *)result = pf->function; 1030 break; 1031 case PCCARD_IVAR_FUNCTION_NUMBER: 1032 *(uint32_t *)result = pf->number; 1033 break; 1034 case PCCARD_IVAR_VENDOR_STR: 1035 *(const char **)result = sc->card.cis1_info[0]; 1036 break; 1037 case PCCARD_IVAR_PRODUCT_STR: 1038 *(const char **)result = sc->card.cis1_info[1]; 1039 break; 1040 case PCCARD_IVAR_CIS3_STR: 1041 *(const char **)result = sc->card.cis1_info[2]; 1042 break; 1043 case PCCARD_IVAR_CIS4_STR: 1044 *(const char **)result = sc->card.cis1_info[3]; 1045 break; 1046 } 1047 return (0); 1048} 1049 1050static void 1051pccard_driver_added(device_t dev, driver_t *driver) 1052{ 1053 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1054 struct pccard_function *pf; 1055 device_t child; 1056 1057 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 1058 if (STAILQ_EMPTY(&pf->cfe_head)) 1059 continue; 1060 child = pf->dev; 1061 if (device_get_state(child) != DS_NOTPRESENT) 1062 continue; 1063 if (pccard_function_enable(pf) == 0 && 1064 device_probe_and_attach(child) == 0) { 1065 DEVPRINTF((sc->dev, "function %d CCR at %d " 1066 "offset %x: %x %x %x %x, %x %x %x %x, %x\n", 1067 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 1068 pccard_ccr_read(pf, 0x00), 1069 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 1070 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 1071 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 1072 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 1073 } else { 1074 if (pf->cfe != NULL) 1075 pccard_function_disable(pf); 1076 } 1077 } 1078 return; 1079} 1080 1081static struct resource * 1082pccard_alloc_resource(device_t dev, device_t child, int type, int *rid, 1083 u_long start, u_long end, u_long count, u_int flags) 1084{ 1085 struct pccard_ivar *dinfo; 1086 struct resource_list_entry *rle = 0; 1087 int passthrough = (device_get_parent(child) != dev); 1088 int isdefault = (start == 0 && end == ~0UL && count == 1); 1089 struct resource *r = NULL; 1090 1091 /* XXX I'm no longer sure this is right */ 1092 if (passthrough) { 1093 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 1094 type, rid, start, end, count, flags)); 1095 } 1096 1097 dinfo = device_get_ivars(child); 1098 rle = resource_list_find(&dinfo->resources, type, *rid); 1099 1100 if (rle == NULL && isdefault) 1101 return (NULL); /* no resource of that type/rid */ 1102 if (rle == NULL || rle->res == NULL) { 1103 /* XXX Need to adjust flags */ 1104 r = bus_alloc_resource(dev, type, rid, start, end, 1105 count, flags); 1106 if (r == NULL) 1107 goto bad; 1108 resource_list_add(&dinfo->resources, type, *rid, 1109 rman_get_start(r), rman_get_end(r), count); 1110 rle = resource_list_find(&dinfo->resources, type, *rid); 1111 if (!rle) 1112 goto bad; 1113 rle->res = r; 1114 } 1115 /* 1116 * If dev doesn't own the device, then we can't give this device 1117 * out. 1118 */ 1119 if (rman_get_device(rle->res) != dev) 1120 return (NULL); 1121 rman_set_device(rle->res, child); 1122 if (flags & RF_ACTIVE) 1123 BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, rle->res); 1124 return (rle->res); 1125bad:; 1126 device_printf(dev, "WARNING: Resource not reserved by pccard\n"); 1127 return (NULL); 1128} 1129 1130static int 1131pccard_release_resource(device_t dev, device_t child, int type, int rid, 1132 struct resource *r) 1133{ 1134 struct pccard_ivar *dinfo; 1135 int passthrough = (device_get_parent(child) != dev); 1136 struct resource_list_entry *rle = 0; 1137 1138 if (passthrough) 1139 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 1140 type, rid, r); 1141 1142 dinfo = device_get_ivars(child); 1143 1144 rle = resource_list_find(&dinfo->resources, type, rid); 1145 1146 if (!rle) { 1147 device_printf(dev, "Allocated resource not found, " 1148 "%d %x %lx %lx\n", 1149 type, rid, rman_get_start(r), rman_get_size(r)); 1150 return ENOENT; 1151 } 1152 if (!rle->res) { 1153 device_printf(dev, "Allocated resource not recorded\n"); 1154 return ENOENT; 1155 } 1156 /* 1157 * Deactivate the resource (since it is being released), and 1158 * assign it to the bus. 1159 */ 1160 BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, rle->res); 1161 rman_set_device(rle->res, dev); 1162 return (0); 1163} 1164 1165static void 1166pccard_child_detached(device_t parent, device_t dev) 1167{ 1168 struct pccard_ivar *ivar = PCCARD_IVAR(dev); 1169 struct pccard_function *pf = ivar->pf; 1170 1171 pccard_function_disable(pf); 1172} 1173 1174static void 1175pccard_intr(void *arg) 1176{ 1177 struct pccard_function *pf = (struct pccard_function*) arg; 1178 int reg; 1179 int doisr = 1; 1180 1181 /* 1182 * MFC cards know if they interrupted, so we have to ack the 1183 * interrupt and call the ISR. Non-MFC cards don't have these 1184 * bits, so they always get called. Many non-MFC cards have 1185 * this bit set always upon read, but some do not. 1186 * 1187 * We always ack the interrupt, even if there's no ISR 1188 * for the card. This is done on the theory that acking 1189 * the interrupt will pacify the card enough to keep an 1190 * interrupt storm from happening. Of course this won't 1191 * help in the non-MFC case. 1192 * 1193 * This has no impact for MPSAFEness of the client drivers. 1194 * We register this with whatever flags the intr_handler 1195 * was registered with. All these functions are MPSAFE. 1196 */ 1197 if (pccard_mfc(pf->sc)) { 1198 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS); 1199 if (reg & PCCARD_CCR_STATUS_INTR) 1200 pccard_ccr_write(pf, PCCARD_CCR_STATUS, 1201 reg & ~PCCARD_CCR_STATUS_INTR); 1202 else 1203 doisr = 0; 1204 } 1205 if (pf->intr_handler != NULL && doisr) 1206 pf->intr_handler(pf->intr_handler_arg); 1207} 1208 1209static int 1210pccard_setup_intr(device_t dev, device_t child, struct resource *irq, 1211 int flags, driver_intr_t *intr, void *arg, void **cookiep) 1212{ 1213 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1214 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1215 struct pccard_function *pf = ivar->pf; 1216 int err; 1217 1218 if (pf->intr_handler != NULL) 1219 panic("Only one interrupt handler per function allowed"); 1220 err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr, 1221 pf, cookiep); 1222 if (err != 0) 1223 return (err); 1224 pf->intr_handler = intr; 1225 pf->intr_handler_arg = arg; 1226 pf->intr_handler_cookie = *cookiep; 1227 if (pccard_mfc(sc)) { 1228 pccard_ccr_write(pf, PCCARD_CCR_OPTION, 1229 pccard_ccr_read(pf, PCCARD_CCR_OPTION) | 1230 PCCARD_CCR_OPTION_IREQ_ENABLE); 1231 } 1232 return (0); 1233} 1234 1235static int 1236pccard_teardown_intr(device_t dev, device_t child, struct resource *r, 1237 void *cookie) 1238{ 1239 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1240 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1241 struct pccard_function *pf = ivar->pf; 1242 int ret; 1243 1244 if (pccard_mfc(sc)) { 1245 pccard_ccr_write(pf, PCCARD_CCR_OPTION, 1246 pccard_ccr_read(pf, PCCARD_CCR_OPTION) & 1247 ~PCCARD_CCR_OPTION_IREQ_ENABLE); 1248 } 1249 ret = bus_generic_teardown_intr(dev, child, r, cookie); 1250 if (ret == 0) { 1251 pf->intr_handler = NULL; 1252 pf->intr_handler_arg = NULL; 1253 pf->intr_handler_cookie = NULL; 1254 } 1255 1256 return (ret); 1257} 1258 1259static int 1260pccard_activate_resource(device_t brdev, device_t child, int type, int rid, 1261 struct resource *r) 1262{ 1263 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1264 struct pccard_function *pf = ivar->pf; 1265 1266 switch(type) { 1267 case SYS_RES_IOPORT: 1268 /* 1269 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC 1270 * card. 1271 */ 1272 if (pccard_mfc(pf->sc)) 1273 pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0, 1274 rman_get_size(r)); 1275 break; 1276 default: 1277 break; 1278 } 1279 return (bus_generic_activate_resource(brdev, child, type, rid, r)); 1280} 1281 1282static int 1283pccard_deactivate_resource(device_t brdev, device_t child, int type, 1284 int rid, struct resource *r) 1285{ 1286 /* XXX undo pccard_activate_resource? XXX */ 1287 return (bus_generic_deactivate_resource(brdev, child, type, rid, r)); 1288} 1289 1290static int 1291pccard_attr_read_impl(device_t brdev, device_t child, uint32_t offset, 1292 uint8_t *val) 1293{ 1294 struct pccard_ivar *devi = PCCARD_IVAR(child); 1295 struct pccard_function *pf = devi->pf; 1296 1297 /* 1298 * Optimization. Most of the time, devices want to access 1299 * the same page of the attribute memory that the CCR is in. 1300 * We take advantage of this fact here. 1301 */ 1302 if (offset / PCCARD_MEM_PAGE_SIZE == 1303 pf->ccr_base / PCCARD_MEM_PAGE_SIZE) 1304 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 1305 offset % PCCARD_MEM_PAGE_SIZE); 1306 else { 1307 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset, 1308 &offset); 1309 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, offset); 1310 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base, 1311 &offset); 1312 } 1313 return 0; 1314} 1315 1316static int 1317pccard_attr_write_impl(device_t brdev, device_t child, uint32_t offset, 1318 uint8_t val) 1319{ 1320 struct pccard_ivar *devi = PCCARD_IVAR(child); 1321 struct pccard_function *pf = devi->pf; 1322 1323 /* 1324 * Optimization. Most of the time, devices want to access 1325 * the same page of the attribute memory that the CCR is in. 1326 * We take advantage of this fact here. 1327 */ 1328 if (offset / PCCARD_MEM_PAGE_SIZE == 1329 pf->ccr_base / PCCARD_MEM_PAGE_SIZE) 1330 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 1331 offset % PCCARD_MEM_PAGE_SIZE, val); 1332 else { 1333 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset, 1334 &offset); 1335 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, offset, val); 1336 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base, 1337 &offset); 1338 } 1339 1340 return 0; 1341} 1342 1343static int 1344pccard_ccr_read_impl(device_t brdev, device_t child, uint32_t offset, 1345 uint8_t *val) 1346{ 1347 struct pccard_ivar *devi = PCCARD_IVAR(child); 1348 1349 *val = pccard_ccr_read(devi->pf, offset); 1350 device_printf(child, "ccr_read of %#x (%#x) is %#x\n", offset, 1351 devi->pf->pf_ccr_offset, *val); 1352 return 0; 1353} 1354 1355static int 1356pccard_ccr_write_impl(device_t brdev, device_t child, uint32_t offset, 1357 uint8_t val) 1358{ 1359 struct pccard_ivar *devi = PCCARD_IVAR(child); 1360 struct pccard_function *pf = devi->pf; 1361 1362 /* 1363 * Can't use pccard_ccr_write since client drivers may access 1364 * registers not contained in the 'mask' if they are non-standard. 1365 */ 1366 device_printf(child, "ccr_write of %#x to %#x (%#x)\n", val, offset, 1367 devi->pf->pf_ccr_offset); 1368 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, pf->pf_ccr_offset + offset, 1369 val); 1370 return 0; 1371} 1372 1373 1374static device_method_t pccard_methods[] = { 1375 /* Device interface */ 1376 DEVMETHOD(device_probe, pccard_probe), 1377 DEVMETHOD(device_attach, pccard_attach), 1378 DEVMETHOD(device_detach, pccard_detach), 1379 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1380 DEVMETHOD(device_suspend, pccard_suspend), 1381 DEVMETHOD(device_resume, pccard_resume), 1382 1383 /* Bus interface */ 1384 DEVMETHOD(bus_print_child, pccard_print_child), 1385 DEVMETHOD(bus_driver_added, pccard_driver_added), 1386 DEVMETHOD(bus_child_detached, pccard_child_detached), 1387 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource), 1388 DEVMETHOD(bus_release_resource, pccard_release_resource), 1389 DEVMETHOD(bus_activate_resource, pccard_activate_resource), 1390 DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource), 1391 DEVMETHOD(bus_setup_intr, pccard_setup_intr), 1392 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr), 1393 DEVMETHOD(bus_set_resource, pccard_set_resource), 1394 DEVMETHOD(bus_get_resource, pccard_get_resource), 1395 DEVMETHOD(bus_delete_resource, pccard_delete_resource), 1396 DEVMETHOD(bus_probe_nomatch, pccard_probe_nomatch), 1397 DEVMETHOD(bus_read_ivar, pccard_read_ivar), 1398 DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str), 1399 DEVMETHOD(bus_child_location_str, pccard_child_location_str), 1400 1401 /* Card Interface */ 1402 DEVMETHOD(card_set_res_flags, pccard_set_res_flags), 1403 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset), 1404 DEVMETHOD(card_attach_card, pccard_attach_card), 1405 DEVMETHOD(card_detach_card, pccard_detach_card), 1406 DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup), 1407 DEVMETHOD(card_cis_scan, pccard_scan_cis), 1408 DEVMETHOD(card_attr_read, pccard_attr_read_impl), 1409 DEVMETHOD(card_attr_write, pccard_attr_write_impl), 1410 DEVMETHOD(card_ccr_read, pccard_ccr_read_impl), 1411 DEVMETHOD(card_ccr_write, pccard_ccr_write_impl), 1412 1413 { 0, 0 } 1414}; 1415 1416static driver_t pccard_driver = { 1417 "pccard", 1418 pccard_methods, 1419 sizeof(struct pccard_softc) 1420}; 1421 1422devclass_t pccard_devclass; 1423 1424/* Maybe we need to have a slot device? */ 1425DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0); 1426DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0); 1427MODULE_VERSION(pccard, 1); 1428