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