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