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