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