pccard.c revision 139749
166200Simp/* $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $ */ 252506Simp 3139749Simp/*- 452506Simp * Copyright (c) 1997 Marc Horowitz. All rights reserved. 552506Simp * 652506Simp * Redistribution and use in source and binary forms, with or without 752506Simp * modification, are permitted provided that the following conditions 852506Simp * are met: 952506Simp * 1. Redistributions of source code must retain the above copyright 1052506Simp * notice, this list of conditions and the following disclaimer. 1152506Simp * 2. Redistributions in binary form must reproduce the above copyright 1252506Simp * notice, this list of conditions and the following disclaimer in the 1352506Simp * documentation and/or other materials provided with the distribution. 1452506Simp * 3. All advertising materials mentioning features or use of this software 1552506Simp * must display the following acknowledgement: 1652506Simp * This product includes software developed by Marc Horowitz. 1752506Simp * 4. The name of the author may not be used to endorse or promote products 1852506Simp * derived from this software without specific prior written permission. 1952506Simp * 2052506Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2152506Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2252506Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2352506Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2452506Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2552506Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2652506Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2752506Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2852506Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2952506Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3052506Simp */ 3152506Simp 32119418Sobrien#include <sys/cdefs.h> 33119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/pccard/pccard.c 139749 2005-01-06 01:43:34Z imp $"); 34119418Sobrien 3552506Simp#include <sys/param.h> 3652506Simp#include <sys/systm.h> 3752506Simp#include <sys/malloc.h> 3852506Simp#include <sys/module.h> 3952506Simp#include <sys/kernel.h> 4052506Simp#include <sys/queue.h> 4191786Simp#include <sys/sysctl.h> 4252506Simp#include <sys/types.h> 4352506Simp 4452506Simp#include <sys/bus.h> 4552506Simp#include <machine/bus.h> 4652506Simp#include <sys/rman.h> 4752506Simp#include <machine/resource.h> 4852506Simp 4982781Sshiba#include <net/ethernet.h> 5082781Sshiba 5152506Simp#include <dev/pccard/pccardreg.h> 5252506Simp#include <dev/pccard/pccardvar.h> 5352506Simp 5455500Simp#include "power_if.h" 5559193Simp#include "card_if.h" 5655500Simp 5755500Simp#define PCCARDDEBUG 5855500Simp 5991786Simp/* sysctl vars */ 6091786SimpSYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters"); 6191786Simp 6291786Simpint pccard_debug = 0; 6391786SimpTUNABLE_INT("hw.pccard.debug", &pccard_debug); 6491786SimpSYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW, 6591786Simp &pccard_debug, 0, 6691786Simp "pccard debug"); 6791786Simp 6891786Simpint pccard_cis_debug = 0; 6991786SimpTUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug); 7091786SimpSYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW, 7191786Simp &pccard_cis_debug, 0, "pccard CIS debug"); 7291786Simp 7352506Simp#ifdef PCCARDDEBUG 7452506Simp#define DPRINTF(arg) if (pccard_debug) printf arg 7555500Simp#define DEVPRINTF(arg) if (pccard_debug) device_printf arg 7667333Simp#define PRVERBOSE(arg) printf arg 7767333Simp#define DEVPRVERBOSE(arg) device_printf arg 7852506Simp#else 7952506Simp#define DPRINTF(arg) 8055500Simp#define DEVPRINTF(arg) 8167333Simp#define PRVERBOSE(arg) if (bootverbose) printf arg 8267333Simp#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg 8352506Simp#endif 8452506Simp 8582378Sjonstatic int pccard_ccr_read(struct pccard_function *pf, int ccr); 8682378Sjonstatic void pccard_ccr_write(struct pccard_function *pf, int ccr, int val); 8782378Sjonstatic int pccard_attach_card(device_t dev); 88106362Simpstatic int pccard_detach_card(device_t dev); 8982378Sjonstatic void pccard_function_init(struct pccard_function *pf); 9082378Sjonstatic void pccard_function_free(struct pccard_function *pf); 9182378Sjonstatic int pccard_function_enable(struct pccard_function *pf); 9282378Sjonstatic void pccard_function_disable(struct pccard_function *pf); 9382378Sjonstatic int pccard_compat_do_probe(device_t bus, device_t dev); 9482378Sjonstatic int pccard_compat_do_attach(device_t bus, device_t dev); 9582378Sjonstatic int pccard_add_children(device_t dev, int busno); 9682378Sjonstatic int pccard_probe(device_t dev); 9782378Sjonstatic int pccard_attach(device_t dev); 9882378Sjonstatic int pccard_detach(device_t dev); 9982378Sjonstatic void pccard_print_resources(struct resource_list *rl, 10082378Sjon const char *name, int type, int count, const char *format); 10182378Sjonstatic int pccard_print_child(device_t dev, device_t child); 10282378Sjonstatic int pccard_set_resource(device_t dev, device_t child, int type, 10382378Sjon int rid, u_long start, u_long count); 10482378Sjonstatic int pccard_get_resource(device_t dev, device_t child, int type, 10582378Sjon int rid, u_long *startp, u_long *countp); 10682378Sjonstatic void pccard_delete_resource(device_t dev, device_t child, int type, 10782378Sjon int rid); 10882378Sjonstatic int pccard_set_res_flags(device_t dev, device_t child, int type, 10982378Sjon int rid, u_int32_t flags); 11082378Sjonstatic int pccard_set_memory_offset(device_t dev, device_t child, int rid, 11182378Sjon u_int32_t offset, u_int32_t *deltap); 112104641Simpstatic void pccard_probe_nomatch(device_t cbdev, device_t child); 11382378Sjonstatic int pccard_read_ivar(device_t bus, device_t child, int which, 11482378Sjon u_char *result); 11582378Sjonstatic void pccard_driver_added(device_t dev, driver_t *driver); 11682378Sjonstatic struct resource *pccard_alloc_resource(device_t dev, 11782378Sjon device_t child, int type, int *rid, u_long start, 11882378Sjon u_long end, u_long count, u_int flags); 11982378Sjonstatic int pccard_release_resource(device_t dev, device_t child, int type, 12082378Sjon int rid, struct resource *r); 12182378Sjonstatic void pccard_child_detached(device_t parent, device_t dev); 12282378Sjonstatic void pccard_intr(void *arg); 12382378Sjonstatic int pccard_setup_intr(device_t dev, device_t child, 12482378Sjon struct resource *irq, int flags, driver_intr_t *intr, 12582378Sjon void *arg, void **cookiep); 12682378Sjonstatic int pccard_teardown_intr(device_t dev, device_t child, 12782378Sjon struct resource *r, void *cookie); 12852506Simp 12997613Stakawatastatic const struct pccard_product * 13097613Stakawatapccard_do_product_lookup(device_t bus, device_t dev, 13197613Stakawata const struct pccard_product *tab, size_t ent_size, 13297613Stakawata pccard_product_match_fn matchfn); 13397613Stakawata 13497613Stakawata 13582378Sjonstatic int 13674632Simppccard_ccr_read(struct pccard_function *pf, int ccr) 13752506Simp{ 13852506Simp return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 13952506Simp pf->pf_ccr_offset + ccr)); 14052506Simp} 14152506Simp 14282378Sjonstatic void 14374632Simppccard_ccr_write(struct pccard_function *pf, int ccr, int val) 14452506Simp{ 14552506Simp if ((pf->ccr_mask) & (1 << (ccr / 2))) { 14652506Simp bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 14752506Simp pf->pf_ccr_offset + ccr, val); 14852506Simp } 14952506Simp} 15052506Simp 15159193Simpstatic int 152113242Simppccard_set_default_descr(device_t dev) 153113242Simp{ 154121521Simp const char *vendorstr, *prodstr; 155133865Simp uint32_t vendor, prod; 156121521Simp char *str; 157113242Simp 158113242Simp if (pccard_get_vendor_str(dev, &vendorstr)) 159113242Simp return (0); 160113242Simp if (pccard_get_product_str(dev, &prodstr)) 161113242Simp return (0); 162133865Simp if (vendorstr != NULL && prodstr != NULL) { 163133865Simp str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF, 164133865Simp M_WAITOK); 165133865Simp sprintf(str, "%s %s", vendorstr, prodstr); 166133865Simp device_set_desc_copy(dev, str); 167133865Simp free(str, M_DEVBUF); 168133865Simp } else { 169133865Simp if (pccard_get_vendor(dev, &vendor)) 170133865Simp return (0); 171133865Simp if (pccard_get_product(dev, &prod)) 172133865Simp return (0); 173133865Simp str = malloc(100, M_DEVBUF, M_WAITOK); 174133865Simp snprintf(str, 100, "vendor=0x%x product=0x%x", vendor, prod); 175133865Simp device_set_desc_copy(dev, str); 176133865Simp free(str, M_DEVBUF); 177133865Simp } 178113242Simp return (0); 179113242Simp} 180113242Simp 181113242Simpstatic int 18259193Simppccard_attach_card(device_t dev) 18352506Simp{ 18464850Simp struct pccard_softc *sc = PCCARD_SOFTC(dev); 18552506Simp struct pccard_function *pf; 18665917Simp struct pccard_ivar *ivar; 18761788Simp device_t child; 188102713Simp int i; 18952506Simp 19052506Simp /* 19152506Simp * this is here so that when socket_enable calls gettype, trt happens 19252506Simp */ 19352506Simp STAILQ_INIT(&sc->card.pf_head); 19452506Simp 19555500Simp DEVPRINTF((dev, "chip_socket_enable\n")); 19655500Simp POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 19752506Simp 19855500Simp DEVPRINTF((dev, "read_cis\n")); 19952506Simp pccard_read_cis(sc); 20052506Simp 20155500Simp DEVPRINTF((dev, "check_cis_quirks\n")); 20252506Simp pccard_check_cis_quirks(dev); 20352506Simp 20452506Simp /* 20552506Simp * bail now if the card has no functions, or if there was an error in 20652506Simp * the cis. 20752506Simp */ 20852506Simp 20970715Sjon if (sc->card.error) { 210102713Simp device_printf(dev, "CARD ERROR!\n"); 21152506Simp return (1); 21270715Sjon } 21370715Sjon if (STAILQ_EMPTY(&sc->card.pf_head)) { 214102713Simp device_printf(dev, "Card has no functions!\n"); 21552506Simp return (1); 21670715Sjon } 21752506Simp 21890436Simp if (bootverbose || pccard_debug) 21952506Simp pccard_print_cis(dev); 22052506Simp 22155500Simp DEVPRINTF((dev, "functions scanning\n")); 222102713Simp i = -1; 22352506Simp STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 224102713Simp i++; 225102713Simp if (STAILQ_EMPTY(&pf->cfe_head)) { 226102713Simp device_printf(dev, 227102713Simp "Function %d has no config entries.!\n", i); 22852506Simp continue; 229102713Simp } 23052506Simp pf->sc = sc; 23152506Simp pf->cfe = NULL; 23264927Simp pf->dev = NULL; 23352506Simp } 234104641Simp DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1, 235102713Simp pccard_mfc(sc))); 23682378Sjon 23752506Simp STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 23852506Simp if (STAILQ_EMPTY(&pf->cfe_head)) 23952506Simp continue; 24061788Simp /* 24161788Simp * In NetBSD, the drivers are responsible for activating 24261788Simp * each function of a card. I think that in FreeBSD we 24361788Simp * want to activate them enough for the usual bus_*_resource 24461788Simp * routines will do the right thing. This many mean a 24561788Simp * departure from the current NetBSD model. 24661788Simp * 247104641Simp * This seems to work well in practice for most cards. 248104641Simp * However, there are two cases that are problematic. 249104641Simp * If a driver wishes to pick and chose which config 250104641Simp * entry to use, then this method falls down. These 251104641Simp * are usually older cards. In addition, there are 252104641Simp * some cards that have multiple hardware units on the 253104641Simp * cards, but presents only one CIS chain. These cards 254104641Simp * are combination cards, but only one of these units 255104641Simp * can be on at a time. 25661788Simp */ 25767897Sdwmalone ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, 258111119Simp M_WAITOK | M_ZERO); 25961788Simp child = device_add_child(dev, NULL, -1); 26065917Simp device_set_ivars(child, ivar); 26166847Simp ivar->fcn = pf; 26267187Simp pf->dev = child; 26367167Simp /* 26467167Simp * XXX We might want to move the next two lines into 26567167Simp * XXX the pccard interface layer. For the moment, this 26667167Simp * XXX is OK, but some drivers want to pick the config 26767167Simp * XXX entry to use as well as some address tweaks (mostly 26867167Simp * XXX due to bugs in decode logic that makes some 26967167Simp * XXX addresses illegal or broken). 27067167Simp */ 27165917Simp pccard_function_init(pf); 27282378Sjon if (sc->sc_enabled_count == 0) 27382378Sjon POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 27467333Simp if (pccard_function_enable(pf) == 0 && 275113242Simp pccard_set_default_descr(child) == 0 && 27667333Simp device_probe_and_attach(child) == 0) { 27755500Simp DEVPRINTF((sc->dev, "function %d CCR at %d " 278121905Simp "offset %x mask %x: " 279121905Simp "%x %x %x %x, %x %x %x %x, %x\n", 28067167Simp pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 281121905Simp pf->ccr_mask, pccard_ccr_read(pf, 0x00), 28252506Simp pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 28352506Simp pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 28452506Simp pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 28552506Simp pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 28667167Simp } else { 28786907Simp if (pf->cfe != NULL) 28886907Simp pccard_function_disable(pf); 28952506Simp } 29052506Simp } 29174632Simp return (0); 29252506Simp} 29352506Simp 29459193Simpstatic int 295106362Simppccard_detach_card(device_t dev) 29652506Simp{ 29764850Simp struct pccard_softc *sc = PCCARD_SOFTC(dev); 29852506Simp struct pccard_function *pf; 29982378Sjon struct pccard_config_entry *cfe; 300119755Simp int state; 30152506Simp 30252506Simp /* 30352506Simp * We are running on either the PCCARD socket's event thread 30452506Simp * or in user context detaching a device by user request. 30552506Simp */ 30652506Simp STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 307119755Simp if (pf->dev == NULL) 308119755Simp continue; 309119755Simp state = device_get_state(pf->dev); 31082378Sjon if (state == DS_ATTACHED || state == DS_BUSY) 31182378Sjon device_detach(pf->dev); 31286907Simp if (pf->cfe != NULL) 31386907Simp pccard_function_disable(pf); 31482378Sjon pccard_function_free(pf); 315106896Simp device_delete_child(dev, pf->dev); 31652506Simp } 31782378Sjon if (sc->sc_enabled_count == 0) 31882378Sjon POWER_DISABLE_SOCKET(device_get_parent(dev), dev); 31982378Sjon 32082378Sjon while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) { 32182378Sjon while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) { 32282378Sjon STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list); 32382378Sjon free(cfe, M_DEVBUF); 32482378Sjon } 32582378Sjon STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list); 32682378Sjon free(pf, M_DEVBUF); 32782378Sjon } 32874632Simp return (0); 32952506Simp} 33052506Simp 33197613Stakawatastatic const struct pccard_product * 33297613Stakawatapccard_do_product_lookup(device_t bus, device_t dev, 333112359Simp const struct pccard_product *tab, size_t ent_size, 334112359Simp pccard_product_match_fn matchfn) 33566200Simp{ 33666200Simp const struct pccard_product *ent; 33766200Simp int matches; 33866200Simp u_int32_t fcn; 33966200Simp u_int32_t vendor; 34066200Simp u_int32_t prod; 341121521Simp const char *vendorstr; 342121521Simp const char *prodstr; 34366200Simp 34466200Simp#ifdef DIAGNOSTIC 34566200Simp if (sizeof *ent > ent_size) 346112359Simp panic("pccard_product_lookup: bogus ent_size %jd", 347112359Simp (intmax_t) ent_size); 34866200Simp#endif 34966200Simp if (pccard_get_vendor(dev, &vendor)) 35066200Simp return (NULL); 35166200Simp if (pccard_get_product(dev, &prod)) 35266200Simp return (NULL); 35366200Simp if (pccard_get_function_number(dev, &fcn)) 35466200Simp return (NULL); 35566200Simp if (pccard_get_vendor_str(dev, &vendorstr)) 35666200Simp return (NULL); 35766200Simp if (pccard_get_product_str(dev, &prodstr)) 35866200Simp return (NULL); 359113313Simp for (ent = tab; ent->pp_vendor != 0; ent = 36082378Sjon (const struct pccard_product *) ((const char *) ent + ent_size)) { 36166200Simp matches = 1; 36286642Simp if (ent->pp_vendor == PCCARD_VENDOR_ANY && 363113078Ssanpei ent->pp_product == PCCARD_PRODUCT_ANY && 36486642Simp ent->pp_cis[0] == NULL && 36586642Simp ent->pp_cis[1] == NULL) { 366113300Simp if (ent->pp_name) 367113300Simp device_printf(dev, 368113300Simp "Total wildcard entry ignored for %s\n", 369113300Simp ent->pp_name); 37086642Simp continue; 37186642Simp } 37266200Simp if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY && 37366200Simp vendor != ent->pp_vendor) 37466200Simp matches = 0; 37566200Simp if (matches && ent->pp_product != PCCARD_PRODUCT_ANY && 37666200Simp prod != ent->pp_product) 37766200Simp matches = 0; 37866200Simp if (matches && fcn != ent->pp_expfunc) 37966200Simp matches = 0; 38071322Simp if (matches && ent->pp_cis[0] && 381133865Simp (vendorstr == NULL || 382133865Simp strcmp(ent->pp_cis[0], vendorstr) != 0)) 38366200Simp matches = 0; 38471322Simp if (matches && ent->pp_cis[1] && 385133865Simp (prodstr == NULL || 386133865Simp strcmp(ent->pp_cis[1], prodstr) != 0)) 38766200Simp matches = 0; 38871322Simp /* XXX need to match cis[2] and cis[3] also XXX */ 38966200Simp if (matchfn != NULL) 39066200Simp matches = (*matchfn)(dev, ent, matches); 39166200Simp if (matches) 39266200Simp return (ent); 39366200Simp } 39466200Simp return (NULL); 39566200Simp} 39666200Simp 39752506Simp/* 39852506Simp * Initialize a PCCARD function. May be called as long as the function is 39952506Simp * disabled. 40082382Simp * 40182382Simp * Note: pccard_function_init should not keep resources allocated. It should 40282382Simp * only set them up ala isa pnp, set the values in the rl lists, and return. 40382382Simp * Any resource held after pccard_function_init is called is a bug. However, 40482382Simp * the bus routines to get the resources also assume that pccard_function_init 40582382Simp * does this, so they need to be fixed too. 40652506Simp */ 40782378Sjonstatic void 40870715Sjonpccard_function_init(struct pccard_function *pf) 40952506Simp{ 41065917Simp struct pccard_config_entry *cfe; 41167187Simp int i; 41267242Simp struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 41367242Simp struct resource_list *rl = &devi->resources; 41470762Simp struct resource_list_entry *rle; 41567242Simp struct resource *r = 0; 41667242Simp device_t bus; 41767424Simp int start; 41867424Simp int end; 41990897Simp int spaces; 42065917Simp 42170715Sjon if (pf->pf_flags & PFF_ENABLED) { 42270715Sjon printf("pccard_function_init: function is enabled"); 42370715Sjon return; 42470715Sjon } 42567242Simp bus = device_get_parent(pf->dev); 42652506Simp /* Remember which configuration entry we are using. */ 42772012Sphk STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) { 42867187Simp for (i = 0; i < cfe->num_iospace; i++) 42967187Simp cfe->iores[i] = NULL; 43067187Simp cfe->irqres = NULL; 43190897Simp spaces = 0; 43267187Simp for (i = 0; i < cfe->num_iospace; i++) { 43367424Simp start = cfe->iospace[i].start; 43467424Simp if (start) 43567424Simp end = start + cfe->iospace[i].length - 1; 43667424Simp else 43767424Simp end = ~0; 43867187Simp cfe->iorid[i] = i; 43990897Simp DEVPRINTF((bus, "I/O rid %d start %x end %x\n", 44090897Simp i, start, end)); 44167242Simp r = cfe->iores[i] = bus_alloc_resource(bus, 44267424Simp SYS_RES_IOPORT, &cfe->iorid[i], start, end, 44370715Sjon cfe->iospace[i].length, 44467424Simp rman_make_alignment_flags(cfe->iospace[i].length)); 44576424Simp if (cfe->iores[i] == NULL) 44667187Simp goto not_this_one; 44767242Simp resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i], 44867242Simp rman_get_start(r), rman_get_end(r), 44967242Simp cfe->iospace[i].length); 45076424Simp rle = resource_list_find(rl, SYS_RES_IOPORT, 45176424Simp cfe->iorid[i]); 45276424Simp rle->res = r; 45390897Simp spaces++; 45467187Simp } 45567187Simp if (cfe->num_memspace > 0) { 45682781Sshiba /* 45782781Sshiba * Not implement yet, Fix me. 45882781Sshiba */ 45990897Simp DEVPRINTF((bus, "Memory space not yet implemented.\n")); 46067187Simp } 46190897Simp if (spaces == 0) { 46290897Simp DEVPRINTF((bus, "Neither memory nor I/O mampped\n")); 46390897Simp goto not_this_one; 46490897Simp } 46567187Simp if (cfe->irqmask) { 46667187Simp cfe->irqrid = 0; 467127135Snjl r = cfe->irqres = bus_alloc_resource_any(bus, 468127135Snjl SYS_RES_IRQ, &cfe->irqrid, 0); 46976424Simp if (cfe->irqres == NULL) 47067187Simp goto not_this_one; 47167242Simp resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid, 47267242Simp rman_get_start(r), rman_get_end(r), 1); 47376424Simp rle = resource_list_find(rl, SYS_RES_IRQ, 47476424Simp cfe->irqrid); 47576424Simp rle->res = r; 47667187Simp } 47767187Simp /* If we get to here, we've allocated all we need */ 47867167Simp pf->cfe = cfe; 47967187Simp break; 48067187Simp not_this_one:; 48167424Simp DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n", 48267424Simp cfe->number)); 48367333Simp /* 48467333Simp * Release resources that we partially allocated 48567333Simp * from this config entry. 48667333Simp */ 48767187Simp for (i = 0; i < cfe->num_iospace; i++) { 48876424Simp if (cfe->iores[i] != NULL) { 48970715Sjon bus_release_resource(bus, SYS_RES_IOPORT, 49067187Simp cfe->iorid[i], cfe->iores[i]); 49182378Sjon rle = resource_list_find(rl, SYS_RES_IOPORT, 49276424Simp cfe->iorid[i]); 49376424Simp rle->res = NULL; 49476424Simp resource_list_delete(rl, SYS_RES_IOPORT, 49576424Simp cfe->iorid[i]); 49675756Simp } 49767187Simp cfe->iores[i] = NULL; 49867187Simp } 49976424Simp if (cfe->irqmask && cfe->irqres != NULL) { 50067242Simp bus_release_resource(bus, SYS_RES_IRQ, 50167187Simp cfe->irqrid, cfe->irqres); 50276424Simp rle = resource_list_find(rl, SYS_RES_IRQ, 50376424Simp cfe->irqrid); 50476424Simp rle->res = NULL; 50576424Simp resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid); 50667187Simp cfe->irqres = NULL; 50767187Simp } 50867167Simp } 50952506Simp} 51052506Simp 51182378Sjon/* 51282378Sjon * Free resources allocated by pccard_function_init(), May be called as long 51382378Sjon * as the function is disabled. 51482382Simp * 51582382Simp * NOTE: This function should be unnecessary. pccard_function_init should 51682382Simp * never keep resources initialized. 51782378Sjon */ 51882378Sjonstatic void 51982378Sjonpccard_function_free(struct pccard_function *pf) 52082378Sjon{ 52182378Sjon struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 52282378Sjon struct resource_list_entry *rle; 52382378Sjon 52482378Sjon if (pf->pf_flags & PFF_ENABLED) { 52582378Sjon printf("pccard_function_init: function is enabled"); 52682378Sjon return; 52782378Sjon } 52882378Sjon 52982378Sjon SLIST_FOREACH(rle, &devi->resources, link) { 53082378Sjon if (rle->res) { 531113242Simp if (rman_get_device(rle->res) != pf->sc->dev) 53282378Sjon device_printf(pf->sc->dev, 53382378Sjon "function_free: Resource still owned by " 53482378Sjon "child, oops. " 53582378Sjon "(type=%d, rid=%d, addr=%lx)\n", 53682378Sjon rle->type, rle->rid, 53782378Sjon rman_get_start(rle->res)); 53882378Sjon BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev), 539113242Simp pf->sc->dev, rle->type, rle->rid, rle->res); 54082378Sjon rle->res = NULL; 54182378Sjon } 54282378Sjon } 54382378Sjon resource_list_free(&devi->resources); 54482378Sjon} 54582378Sjon 546121905Simpstatic void 547121905Simppccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr, 548121905Simp bus_addr_t offset, bus_size_t size) 549121905Simp{ 550121958Simp bus_size_t iosize, tmp; 551121905Simp 552121905Simp if (addr != 0) { 553121905Simp if (pf->pf_mfc_iomax == 0) { 554121905Simp pf->pf_mfc_iobase = addr + offset; 555121905Simp pf->pf_mfc_iomax = pf->pf_mfc_iobase + size; 556121905Simp } else { 557121905Simp /* this makes the assumption that nothing overlaps */ 558121905Simp if (pf->pf_mfc_iobase > addr + offset) 559121905Simp pf->pf_mfc_iobase = addr + offset; 560121905Simp if (pf->pf_mfc_iomax < addr + offset + size) 561121905Simp pf->pf_mfc_iomax = addr + offset + size; 562121905Simp } 563121905Simp } 564121905Simp 565121905Simp tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase; 566121905Simp /* round up to nearest (2^n)-1 */ 567121905Simp for (iosize = 1; iosize < tmp; iosize <<= 1) 568121905Simp ; 569121905Simp iosize--; 570121905Simp 571122032Simp DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n", 572122032Simp (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1))); 573121905Simp pccard_ccr_write(pf, PCCARD_CCR_IOBASE0, 574121905Simp pf->pf_mfc_iobase & 0xff); 575121905Simp pccard_ccr_write(pf, PCCARD_CCR_IOBASE1, 576121905Simp (pf->pf_mfc_iobase >> 8) & 0xff); 577121905Simp pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0); 578121905Simp pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0); 579121905Simp pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize); 580121905Simp} 581121905Simp 58252506Simp/* Enable a PCCARD function */ 58382378Sjonstatic int 58455720Simppccard_function_enable(struct pccard_function *pf) 58552506Simp{ 58652506Simp struct pccard_function *tmp; 58752506Simp int reg; 58855720Simp device_t dev = pf->sc->dev; 58970746Simp 59067333Simp if (pf->cfe == NULL) { 59167333Simp DEVPRVERBOSE((dev, "No config entry could be allocated.\n")); 59274632Simp return (ENOMEM); 59367333Simp } 59452506Simp 59552506Simp /* 59652506Simp * Increase the reference count on the socket, enabling power, if 59752506Simp * necessary. 59852506Simp */ 59982378Sjon pf->sc->sc_enabled_count++; 60052506Simp 60152506Simp if (pf->pf_flags & PFF_ENABLED) { 60252506Simp /* 60352506Simp * Don't do anything if we're already enabled. 60452506Simp */ 60552506Simp return (0); 60652506Simp } 60752506Simp 60852506Simp /* 60952506Simp * it's possible for different functions' CCRs to be in the same 61052506Simp * underlying page. Check for that. 61152506Simp */ 61252506Simp STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 61352506Simp if ((tmp->pf_flags & PFF_ENABLED) && 61452506Simp (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 61552506Simp ((pf->ccr_base + PCCARD_CCR_SIZE) <= 61682378Sjon (tmp->ccr_base - tmp->pf_ccr_offset + 61782378Sjon tmp->pf_ccr_realsize))) { 61852506Simp pf->pf_ccrt = tmp->pf_ccrt; 61952506Simp pf->pf_ccrh = tmp->pf_ccrh; 62052506Simp pf->pf_ccr_realsize = tmp->pf_ccr_realsize; 62152506Simp 62252506Simp /* 62352506Simp * pf->pf_ccr_offset = (tmp->pf_ccr_offset - 62452506Simp * tmp->ccr_base) + pf->ccr_base; 62552506Simp */ 62670715Sjon /* pf->pf_ccr_offset = 62752506Simp (tmp->pf_ccr_offset + pf->ccr_base) - 62870715Sjon tmp->ccr_base; */ 62952506Simp pf->pf_ccr_window = tmp->pf_ccr_window; 63052506Simp break; 63152506Simp } 63252506Simp } 63352506Simp if (tmp == NULL) { 63455720Simp pf->ccr_rid = 0; 63555720Simp pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 63670715Sjon &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE); 63770715Sjon if (!pf->ccr_res) 63852506Simp goto bad; 639106914Smux DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n", 64070746Simp rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res), 64170746Simp pf->ccr_base)); 64261788Simp CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, 64361788Simp pf->ccr_rid, PCCARD_A_MEM_ATTR); 64470715Sjon CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev, 64570748Simp pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset); 64655720Simp pf->pf_ccrt = rman_get_bustag(pf->ccr_res); 64755720Simp pf->pf_ccrh = rman_get_bushandle(pf->ccr_res); 64855720Simp pf->pf_ccr_realsize = 1; 64952506Simp } 65052506Simp 65152506Simp reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX); 65252506Simp reg |= PCCARD_CCR_OPTION_LEVIREQ; 65352506Simp if (pccard_mfc(pf->sc)) { 65452506Simp reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE | 65552506Simp PCCARD_CCR_OPTION_ADDR_DECODE); 65682383Simp /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */ 65752506Simp } 65852506Simp pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg); 65952506Simp 66052506Simp reg = 0; 66152506Simp if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0) 66252506Simp reg |= PCCARD_CCR_STATUS_IOIS8; 66352506Simp if (pf->cfe->flags & PCCARD_CFE_AUDIO) 66452506Simp reg |= PCCARD_CCR_STATUS_AUDIO; 66552506Simp pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg); 66652506Simp 66752506Simp pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0); 66852506Simp 669121905Simp if (pccard_mfc(pf->sc)) 670121905Simp pccard_mfc_adjust_iobase(pf, 0, 0, 0); 67152506Simp 67252506Simp#ifdef PCCARDDEBUG 67352506Simp if (pccard_debug) { 67452506Simp STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 67570715Sjon device_printf(tmp->sc->dev, 67655500Simp "function %d CCR at %d offset %x: " 67755500Simp "%x %x %x %x, %x %x %x %x, %x\n", 67870715Sjon tmp->number, tmp->pf_ccr_window, 67955500Simp tmp->pf_ccr_offset, 68055500Simp pccard_ccr_read(tmp, 0x00), 68155500Simp pccard_ccr_read(tmp, 0x02), 68255500Simp pccard_ccr_read(tmp, 0x04), 68355500Simp pccard_ccr_read(tmp, 0x06), 68455500Simp pccard_ccr_read(tmp, 0x0A), 68570715Sjon pccard_ccr_read(tmp, 0x0C), 68655500Simp pccard_ccr_read(tmp, 0x0E), 68755500Simp pccard_ccr_read(tmp, 0x10), 68855500Simp pccard_ccr_read(tmp, 0x12)); 68952506Simp } 69052506Simp } 69152506Simp#endif 69252506Simp pf->pf_flags |= PFF_ENABLED; 69352506Simp return (0); 69452506Simp 69552506Simp bad: 69652506Simp /* 69752506Simp * Decrement the reference count, and power down the socket, if 69852506Simp * necessary. 69952506Simp */ 70082378Sjon pf->sc->sc_enabled_count--; 70165098Simp DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count)); 70252506Simp 70352506Simp return (1); 70452506Simp} 70552506Simp 70652506Simp/* Disable PCCARD function. */ 70782378Sjonstatic void 70855720Simppccard_function_disable(struct pccard_function *pf) 70952506Simp{ 71052506Simp struct pccard_function *tmp; 71155720Simp device_t dev = pf->sc->dev; 71252506Simp 71352506Simp if (pf->cfe == NULL) 71461788Simp panic("pccard_function_disable: function not initialized"); 71552506Simp 71652506Simp if ((pf->pf_flags & PFF_ENABLED) == 0) { 71752506Simp /* 71852506Simp * Don't do anything if we're already disabled. 71952506Simp */ 72052506Simp return; 72152506Simp } 72252506Simp 72370715Sjon if (pf->intr_handler != NULL) { 72482378Sjon struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 72582378Sjon struct resource_list_entry *rle = 72682378Sjon resource_list_find(&devi->resources, SYS_RES_IRQ, 0); 72782378Sjon BUS_TEARDOWN_INTR(dev, pf->dev, rle->res, 72882378Sjon pf->intr_handler_cookie); 72970715Sjon } 73070715Sjon 73152506Simp /* 73252506Simp * it's possible for different functions' CCRs to be in the same 73352506Simp * underlying page. Check for that. Note we mark us as disabled 73452506Simp * first to avoid matching ourself. 73552506Simp */ 73652506Simp 73752506Simp pf->pf_flags &= ~PFF_ENABLED; 73852506Simp STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 73952506Simp if ((tmp->pf_flags & PFF_ENABLED) && 74052506Simp (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 74152506Simp ((pf->ccr_base + PCCARD_CCR_SIZE) <= 74282378Sjon (tmp->ccr_base - tmp->pf_ccr_offset + 74382378Sjon tmp->pf_ccr_realsize))) 74452506Simp break; 74552506Simp } 74652506Simp 74752506Simp /* Not used by anyone else; unmap the CCR. */ 74852506Simp if (tmp == NULL) { 74970715Sjon bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid, 75055720Simp pf->ccr_res); 75155720Simp pf->ccr_res = NULL; 75252506Simp } 75352506Simp 75452506Simp /* 75552506Simp * Decrement the reference count, and power down the socket, if 75652506Simp * necessary. 75752506Simp */ 75882378Sjon pf->sc->sc_enabled_count--; 75952506Simp} 76052506Simp 76166058Simp/* 76266058Simp * simulate the old "probe" routine. In the new world order, the driver 76366058Simp * needs to grab devices while in the old they were assigned to the device by 76466058Simp * the pccardd process. These symbols are exported to the upper layers. 76566058Simp */ 76674636Simpstatic int 76774636Simppccard_compat_do_probe(device_t bus, device_t dev) 76866058Simp{ 76966058Simp return (CARD_COMPAT_MATCH(dev)); 77066058Simp} 77166058Simp 77274636Simpstatic int 77374636Simppccard_compat_do_attach(device_t bus, device_t dev) 77466058Simp{ 77566058Simp int err; 77666058Simp 77766058Simp err = CARD_COMPAT_PROBE(dev); 778119462Simp if (err <= 0) 77966058Simp err = CARD_COMPAT_ATTACH(dev); 78066058Simp return (err); 78166058Simp} 78266058Simp 78353873Simp#define PCCARD_NPORT 2 78453873Simp#define PCCARD_NMEM 5 78553873Simp#define PCCARD_NIRQ 1 78653873Simp#define PCCARD_NDRQ 0 78753873Simp 78852506Simpstatic int 78952506Simppccard_add_children(device_t dev, int busno) 79052506Simp{ 79159193Simp /* Call parent to scan for any current children */ 79274632Simp return (0); 79352506Simp} 79452506Simp 79552506Simpstatic int 79652506Simppccard_probe(device_t dev) 79752506Simp{ 79867333Simp device_set_desc(dev, "16-bit PCCard bus"); 79974632Simp return (pccard_add_children(dev, device_get_unit(dev))); 80052506Simp} 80152506Simp 80259193Simpstatic int 80359193Simppccard_attach(device_t dev) 80459193Simp{ 80564850Simp struct pccard_softc *sc = PCCARD_SOFTC(dev); 80661788Simp 80759193Simp sc->dev = dev; 80861788Simp sc->sc_enabled_count = 0; 80974632Simp return (bus_generic_attach(dev)); 81059193Simp} 81159193Simp 81282378Sjonstatic int 81382378Sjonpccard_detach(device_t dev) 81482378Sjon{ 815106362Simp pccard_detach_card(dev); 81682378Sjon return 0; 81782378Sjon} 81882378Sjon 81987975Simpstatic int 82087975Simppccard_suspend(device_t self) 82187975Simp{ 822106362Simp pccard_detach_card(self); 82387975Simp return (0); 82487975Simp} 82587975Simp 82687975Simpstatic 82787975Simpint 82887975Simppccard_resume(device_t self) 82987975Simp{ 83087975Simp return (0); 83187975Simp} 83287975Simp 83353873Simpstatic void 83453873Simppccard_print_resources(struct resource_list *rl, const char *name, int type, 83553873Simp int count, const char *format) 83653873Simp{ 83753873Simp struct resource_list_entry *rle; 83853873Simp int printed; 83953873Simp int i; 84053873Simp 84153873Simp printed = 0; 84253873Simp for (i = 0; i < count; i++) { 84353873Simp rle = resource_list_find(rl, type, i); 84476424Simp if (rle != NULL) { 84553873Simp if (printed == 0) 84653873Simp printf(" %s ", name); 84753873Simp else if (printed > 0) 84853873Simp printf(","); 84953873Simp printed++; 85053873Simp printf(format, rle->start); 85153873Simp if (rle->count > 1) { 85253873Simp printf("-"); 85353873Simp printf(format, rle->start + rle->count - 1); 85453873Simp } 85553873Simp } else if (i > 3) { 85653873Simp /* check the first few regardless */ 85753873Simp break; 85853873Simp } 85953873Simp } 86053873Simp} 86153873Simp 86253873Simpstatic int 86353873Simppccard_print_child(device_t dev, device_t child) 86453873Simp{ 86566847Simp struct pccard_ivar *devi = PCCARD_IVAR(child); 86653873Simp struct resource_list *rl = &devi->resources; 86753873Simp int retval = 0; 86853873Simp 86953873Simp retval += bus_print_child_header(dev, child); 87053873Simp retval += printf(" at"); 87153873Simp 87276424Simp if (devi != NULL) { 87353873Simp pccard_print_resources(rl, "port", SYS_RES_IOPORT, 87453873Simp PCCARD_NPORT, "%#lx"); 87553873Simp pccard_print_resources(rl, "iomem", SYS_RES_MEMORY, 87653873Simp PCCARD_NMEM, "%#lx"); 87753873Simp pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ, 87853873Simp "%ld"); 87970715Sjon pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ, 88053873Simp "%ld"); 88167269Simp retval += printf(" function %d config %d", devi->fcn->number, 88267269Simp devi->fcn->cfe->number); 88353873Simp } 88453873Simp 88553873Simp retval += bus_print_child_footer(dev, child); 88653873Simp 88753873Simp return (retval); 88853873Simp} 88953873Simp 89053873Simpstatic int 89153873Simppccard_set_resource(device_t dev, device_t child, int type, int rid, 89253873Simp u_long start, u_long count) 89353873Simp{ 89466847Simp struct pccard_ivar *devi = PCCARD_IVAR(child); 89553873Simp struct resource_list *rl = &devi->resources; 89653873Simp 89753873Simp if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 89853873Simp && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 89974632Simp return (EINVAL); 90053873Simp if (rid < 0) 90174632Simp return (EINVAL); 90253873Simp if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT) 90374632Simp return (EINVAL); 90453873Simp if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM) 90574632Simp return (EINVAL); 90653873Simp if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ) 90774632Simp return (EINVAL); 90853873Simp if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ) 90974632Simp return (EINVAL); 91053873Simp 91153873Simp resource_list_add(rl, type, rid, start, start + count - 1, count); 91282378Sjon if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev, 91382378Sjon type, &rid, start, start + count - 1, count, 0)) 91482378Sjon return 0; 91582378Sjon else 91682378Sjon return ENOMEM; 91753873Simp} 91853873Simp 91953873Simpstatic int 92053873Simppccard_get_resource(device_t dev, device_t child, int type, int rid, 92153873Simp u_long *startp, u_long *countp) 92253873Simp{ 92366847Simp struct pccard_ivar *devi = PCCARD_IVAR(child); 92453873Simp struct resource_list *rl = &devi->resources; 92553873Simp struct resource_list_entry *rle; 92653873Simp 92753873Simp rle = resource_list_find(rl, type, rid); 92876424Simp if (rle == NULL) 92974632Simp return (ENOENT); 93070715Sjon 93176424Simp if (startp != NULL) 93253873Simp *startp = rle->start; 93376424Simp if (countp != NULL) 93453873Simp *countp = rle->count; 93553873Simp 93674632Simp return (0); 93753873Simp} 93853873Simp 93953873Simpstatic void 94053873Simppccard_delete_resource(device_t dev, device_t child, int type, int rid) 94153873Simp{ 94266847Simp struct pccard_ivar *devi = PCCARD_IVAR(child); 94353873Simp struct resource_list *rl = &devi->resources; 94453873Simp resource_list_delete(rl, type, rid); 94553873Simp} 94653873Simp 94759193Simpstatic int 94859193Simppccard_set_res_flags(device_t dev, device_t child, int type, int rid, 94959193Simp u_int32_t flags) 95059193Simp{ 95174632Simp return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type, 95274632Simp rid, flags)); 95359193Simp} 95459193Simp 95559193Simpstatic int 95659193Simppccard_set_memory_offset(device_t dev, device_t child, int rid, 95782378Sjon u_int32_t offset, u_int32_t *deltap) 95870715Sjon 95959193Simp{ 96074632Simp return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid, 96174632Simp offset, deltap)); 96259193Simp} 96359193Simp 964104641Simpstatic void 965104641Simppccard_probe_nomatch(device_t bus, device_t child) 966104641Simp{ 967104641Simp struct pccard_ivar *devi = PCCARD_IVAR(child); 968104641Simp struct pccard_function *func = devi->fcn; 969104641Simp struct pccard_softc *sc = PCCARD_SOFTC(bus); 970104641Simp 971104641Simp device_printf(bus, "<unknown card>"); 972104641Simp printf(" (manufacturer=0x%04x, product=0x%04x) at function %d\n", 973104641Simp sc->card.manufacturer, sc->card.product, func->number); 974104641Simp device_printf(bus, " CIS info: %s, %s, %s\n", sc->card.cis1_info[0], 975104641Simp sc->card.cis1_info[1], sc->card.cis1_info[2]); 976104641Simp return; 977104641Simp} 978104641Simp 97966058Simpstatic int 980104641Simppccard_child_location_str(device_t bus, device_t child, char *buf, 981104641Simp size_t buflen) 982104641Simp{ 983104641Simp struct pccard_ivar *devi = PCCARD_IVAR(child); 984104641Simp struct pccard_function *func = devi->fcn; 985104641Simp 986104641Simp snprintf(buf, buflen, "function=%d", func->number); 987104641Simp return (0); 988104641Simp} 989104641Simp 990104641Simpstatic int 991104641Simppccard_child_pnpinfo_str(device_t bus, device_t child, char *buf, 992104641Simp size_t buflen) 993104641Simp{ 994104641Simp struct pccard_ivar *devi = PCCARD_IVAR(child); 995104641Simp struct pccard_function *func = devi->fcn; 996104641Simp struct pccard_softc *sc = PCCARD_SOFTC(bus); 997104641Simp 998104641Simp snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x " 999104641Simp "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d", 1000104641Simp sc->card.manufacturer, sc->card.product, sc->card.cis1_info[0], 1001104641Simp sc->card.cis1_info[1], func->function); 1002104641Simp return (0); 1003104641Simp} 1004104641Simp 1005104641Simpstatic int 100666058Simppccard_read_ivar(device_t bus, device_t child, int which, u_char *result) 100766058Simp{ 100866847Simp struct pccard_ivar *devi = PCCARD_IVAR(child); 100966779Simp struct pccard_function *func = devi->fcn; 101066779Simp struct pccard_softc *sc = PCCARD_SOFTC(bus); 101166779Simp 101266779Simp switch (which) { 101366779Simp default: 101466779Simp case PCCARD_IVAR_ETHADDR: 101582781Sshiba bcopy(func->pf_funce_lan_nid, result, ETHER_ADDR_LEN); 101666779Simp break; 101766779Simp case PCCARD_IVAR_VENDOR: 101866779Simp *(u_int32_t *) result = sc->card.manufacturer; 101966779Simp break; 102066779Simp case PCCARD_IVAR_PRODUCT: 102166779Simp *(u_int32_t *) result = sc->card.product; 102266779Simp break; 102390964Sshiba case PCCARD_IVAR_PRODEXT: 102490964Sshiba *(u_int16_t *) result = sc->card.prodext; 102590964Sshiba break; 102675761Simp case PCCARD_IVAR_FUNCTION: 102775761Simp *(u_int32_t *) result = func->function; 102875761Simp break; 102966779Simp case PCCARD_IVAR_FUNCTION_NUMBER: 103066847Simp if (!func) { 103166847Simp device_printf(bus, "No function number, bug!\n"); 103266847Simp return (ENOENT); 103366847Simp } 103466847Simp *(u_int32_t *) result = func->number; 103566779Simp break; 103666779Simp case PCCARD_IVAR_VENDOR_STR: 103766779Simp *(char **) result = sc->card.cis1_info[0]; 103866779Simp break; 103966779Simp case PCCARD_IVAR_PRODUCT_STR: 104066779Simp *(char **) result = sc->card.cis1_info[1]; 104166779Simp break; 104266779Simp case PCCARD_IVAR_CIS3_STR: 104366779Simp *(char **) result = sc->card.cis1_info[2]; 104466779Simp break; 104567167Simp case PCCARD_IVAR_CIS4_STR: 1046104610Simp *(char **) result = sc->card.cis1_info[3]; 104767167Simp break; 104866779Simp } 104966779Simp return (0); 105066058Simp} 105166058Simp 105266779Simpstatic void 105366779Simppccard_driver_added(device_t dev, driver_t *driver) 105466779Simp{ 105582378Sjon struct pccard_softc *sc = PCCARD_SOFTC(dev); 105682378Sjon struct pccard_function *pf; 105782378Sjon device_t child; 105882378Sjon 105982378Sjon STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 106082378Sjon if (STAILQ_EMPTY(&pf->cfe_head)) 106182378Sjon continue; 106282378Sjon child = pf->dev; 106382378Sjon if (device_get_state(child) != DS_NOTPRESENT) 106482378Sjon continue; 106582378Sjon if (pccard_function_enable(pf) == 0 && 106682378Sjon device_probe_and_attach(child) == 0) { 106782378Sjon DEVPRINTF((sc->dev, "function %d CCR at %d " 106882378Sjon "offset %x: %x %x %x %x, %x %x %x %x, %x\n", 106982378Sjon pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 107082378Sjon pccard_ccr_read(pf, 0x00), 107182378Sjon pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 107282378Sjon pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 107382378Sjon pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 107482378Sjon pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 107582378Sjon } else { 107686907Simp if (pf->cfe != NULL) 107786907Simp pccard_function_disable(pf); 107882378Sjon } 107982378Sjon } 108082378Sjon return; 108166779Simp} 108266058Simp 108367242Simpstatic struct resource * 108467242Simppccard_alloc_resource(device_t dev, device_t child, int type, int *rid, 108567242Simp u_long start, u_long end, u_long count, u_int flags) 108667242Simp{ 108782378Sjon struct pccard_ivar *dinfo; 108882378Sjon struct resource_list_entry *rle = 0; 108982378Sjon int passthrough = (device_get_parent(child) != dev); 1090121905Simp int isdefault = (start == 0 && end == ~0UL && count == 1); 1091104641Simp struct resource *r = NULL; 109267242Simp 1093121905Simp /* XXX I'm no longer sure this is right */ 109482378Sjon if (passthrough) { 109582378Sjon return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 109682378Sjon type, rid, start, end, count, flags)); 109782378Sjon } 109870715Sjon 109982378Sjon dinfo = device_get_ivars(child); 110082378Sjon rle = resource_list_find(&dinfo->resources, type, *rid); 110170715Sjon 1102121905Simp if (rle == NULL && isdefault) 1103121905Simp return (NULL); /* no resource of that type/rid */ 1104121905Simp if (rle == NULL || rle->res == NULL) { 1105121905Simp /* Do we want this device to own it? */ 1106121905Simp /* XXX I think so, but that might be lame XXX */ 1107121905Simp r = bus_alloc_resource(dev, type, rid, start, end, 1108121905Simp count, flags /* XXX aligment? */); 1109121905Simp if (r == NULL) 1110121905Simp goto bad; 1111121905Simp resource_list_add(&dinfo->resources, type, *rid, 1112121905Simp rman_get_start(r), rman_get_end(r), count); 1113121905Simp rle = resource_list_find(&dinfo->resources, type, *rid); 1114121905Simp if (!rle) 1115121905Simp goto bad; 1116121905Simp rle->res = r; 111767269Simp } 1118121905Simp /* 1119121905Simp * XXX the following looks wrong, in theory, but likely it is 1120121905Simp * XXX needed because of how the CIS code allocates resources 1121121905Simp * XXX for this device. 1122121905Simp */ 1123113242Simp if (rman_get_device(rle->res) != dev) 1124104641Simp return (NULL); 1125104641Simp bus_release_resource(dev, type, *rid, rle->res); 1126104641Simp rle->res = NULL; 1127104641Simp switch(type) { 1128104641Simp case SYS_RES_IOPORT: 1129104641Simp case SYS_RES_MEMORY: 1130104641Simp if (!(flags & RF_ALIGNMENT_MASK)) 1131104641Simp flags |= rman_make_alignment_flags(rle->count); 1132104641Simp break; 1133104641Simp case SYS_RES_IRQ: 1134104641Simp flags |= RF_SHAREABLE; 1135104641Simp break; 1136104641Simp } 1137104641Simp rle->res = resource_list_alloc(&dinfo->resources, dev, child, 1138104641Simp type, rid, rle->start, rle->end, rle->count, flags); 1139104641Simp return (rle->res); 1140104641Simpbad:; 1141104641Simp device_printf(dev, "WARNING: Resource not reserved by pccard\n"); 1142104641Simp return (NULL); 114367242Simp} 114467242Simp 114567242Simpstatic int 114667242Simppccard_release_resource(device_t dev, device_t child, int type, int rid, 114767242Simp struct resource *r) 114867242Simp{ 114982378Sjon struct pccard_ivar *dinfo; 115082378Sjon int passthrough = (device_get_parent(child) != dev); 115182378Sjon struct resource_list_entry *rle = 0; 115282378Sjon int ret; 115382378Sjon int flags; 115470715Sjon 115582378Sjon if (passthrough) 115682378Sjon return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 115782378Sjon type, rid, r); 115870715Sjon 115982378Sjon dinfo = device_get_ivars(child); 116070715Sjon 116182378Sjon rle = resource_list_find(&dinfo->resources, type, rid); 116270715Sjon 116382378Sjon if (!rle) { 116482378Sjon device_printf(dev, "Allocated resource not found, " 116582378Sjon "%d %x %lx %lx\n", 116682378Sjon type, rid, rman_get_start(r), rman_get_size(r)); 116782378Sjon return ENOENT; 116870715Sjon } 116982378Sjon if (!rle->res) { 117082378Sjon device_printf(dev, "Allocated resource not recorded\n"); 117182378Sjon return ENOENT; 117270715Sjon } 117370715Sjon 117482378Sjon ret = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 117582378Sjon type, rid, r); 117682378Sjon switch(type) { 117782378Sjon case SYS_RES_IOPORT: 117882378Sjon case SYS_RES_MEMORY: 117982378Sjon flags = rman_make_alignment_flags(rle->count); 118082378Sjon break; 118182378Sjon case SYS_RES_IRQ: 118282378Sjon flags = RF_SHAREABLE; 118382378Sjon break; 118482378Sjon default: 118582378Sjon flags = 0; 118670715Sjon } 118782378Sjon rle->res = bus_alloc_resource(dev, type, &rid, 118882378Sjon rle->start, rle->end, rle->count, flags); 118982378Sjon if (rle->res == NULL) 119082378Sjon device_printf(dev, "release_resource: " 119182378Sjon "unable to reaquire resource\n"); 119282378Sjon return ret; 119367242Simp} 119467242Simp 119567333Simpstatic void 119667333Simppccard_child_detached(device_t parent, device_t dev) 119767333Simp{ 119867333Simp struct pccard_ivar *ivar = PCCARD_IVAR(dev); 119982378Sjon struct pccard_function *pf = ivar->fcn; 120067333Simp 120182378Sjon pccard_function_disable(pf); 120267333Simp} 120367333Simp 120470715Sjonstatic void 120570762Simppccard_intr(void *arg) 120670762Simp{ 120782378Sjon struct pccard_function *pf = (struct pccard_function*) arg; 120882378Sjon int reg; 1209102923Simp int doisr = 1; 121082378Sjon 121182383Simp /* 1212102923Simp * MFC cards know if they interrupted, so we have to ack the 1213102923Simp * interrupt and call the ISR. Non-MFC cards don't have these 1214102923Simp * bits, so they always get called. Many non-MFC cards have 1215102923Simp * this bit set always upon read, but some do not. 1216102923Simp * 1217102923Simp * We always ack the interrupt, even if there's no ISR 1218102923Simp * for the card. This is done on the theory that acking 1219102923Simp * the interrupt will pacify the card enough to keep an 1220102923Simp * interrupt storm from happening. Of course this won't 1221102923Simp * help in the non-MFC case. 1222116311Simp * 1223116311Simp * This has no impact for MPSAFEness of the client drivers. 1224116311Simp * We register this with whatever flags the intr_handler 1225116311Simp * was registered with. All these functions are MPSAFE. 122682383Simp */ 1227102923Simp if (pccard_mfc(pf->sc)) { 1228102923Simp reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS); 1229102923Simp if (reg & PCCARD_CCR_STATUS_INTR) 1230102923Simp pccard_ccr_write(pf, PCCARD_CCR_STATUS, 1231102923Simp reg & ~PCCARD_CCR_STATUS_INTR); 1232102923Simp else 1233102923Simp doisr = 0; 1234102923Simp } 1235102923Simp if (pf->intr_handler != NULL && doisr) 123682378Sjon pf->intr_handler(pf->intr_handler_arg); 123770715Sjon} 123870715Sjon 123970715Sjonstatic int 124070762Simppccard_setup_intr(device_t dev, device_t child, struct resource *irq, 124170762Simp int flags, driver_intr_t *intr, void *arg, void **cookiep) 124270715Sjon{ 1243102713Simp struct pccard_softc *sc = PCCARD_SOFTC(dev); 124470715Sjon struct pccard_ivar *ivar = PCCARD_IVAR(child); 124570715Sjon struct pccard_function *func = ivar->fcn; 124690445Simp int err; 124770715Sjon 124870715Sjon if (func->intr_handler != NULL) 1249101762Simp panic("Only one interrupt handler per function allowed"); 125090445Simp err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr, 125190445Simp func, cookiep); 125290445Simp if (err != 0) 125390445Simp return (err); 125470715Sjon func->intr_handler = intr; 125570715Sjon func->intr_handler_arg = arg; 125682378Sjon func->intr_handler_cookie = *cookiep; 1257102713Simp if (pccard_mfc(sc)) { 1258102713Simp pccard_ccr_write(func, PCCARD_CCR_OPTION, 1259102713Simp pccard_ccr_read(func, PCCARD_CCR_OPTION) | 1260102713Simp PCCARD_CCR_OPTION_IREQ_ENABLE); 1261102713Simp } 126274632Simp return (0); 126370715Sjon} 126470715Sjon 126570715Sjonstatic int 126670762Simppccard_teardown_intr(device_t dev, device_t child, struct resource *r, 126770762Simp void *cookie) 126870715Sjon{ 1269102713Simp struct pccard_softc *sc = PCCARD_SOFTC(dev); 127070715Sjon struct pccard_ivar *ivar = PCCARD_IVAR(child); 127170715Sjon struct pccard_function *func = ivar->fcn; 127282378Sjon int ret; 127370715Sjon 1274102713Simp if (pccard_mfc(sc)) { 1275102713Simp pccard_ccr_write(func, PCCARD_CCR_OPTION, 1276102713Simp pccard_ccr_read(func, PCCARD_CCR_OPTION) & 1277102713Simp ~PCCARD_CCR_OPTION_IREQ_ENABLE); 1278102713Simp } 127990445Simp ret = bus_generic_teardown_intr(dev, child, r, cookie); 128082378Sjon if (ret == 0) { 128182378Sjon func->intr_handler = NULL; 128282378Sjon func->intr_handler_arg = NULL; 128382378Sjon func->intr_handler_cookie = NULL; 128482378Sjon } 128570715Sjon 128682378Sjon return (ret); 128770715Sjon} 128870715Sjon 1289121905Simpstatic int 1290121905Simppccard_activate_resource(device_t brdev, device_t child, int type, int rid, 1291121905Simp struct resource *r) 1292121905Simp{ 1293121905Simp struct pccard_ivar *ivar = PCCARD_IVAR(child); 1294121905Simp struct pccard_function *pf = ivar->fcn; 1295121905Simp 1296121905Simp switch(type) { 1297121905Simp case SYS_RES_IOPORT: 1298121905Simp /* 1299121905Simp * We need to adjust IOBASE[01] and IOSIZE if we're an MFC 1300121905Simp * card. 1301121905Simp */ 1302121905Simp if (pccard_mfc(pf->sc)) 1303121905Simp pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0, 1304121905Simp rman_get_size(r)); 1305121905Simp break; 1306121905Simp default: 1307121905Simp break; 1308121905Simp } 1309121905Simp return (bus_generic_activate_resource(brdev, child, type, rid, r)); 1310121905Simp} 1311121905Simp 1312121905Simpstatic int 1313121905Simppccard_deactivate_resource(device_t brdev, device_t child, int type, 1314121905Simp int rid, struct resource *r) 1315121905Simp{ 1316121905Simp /* XXX undo pccard_activate_resource? XXX */ 1317121905Simp return (bus_generic_deactivate_resource(brdev, child, type, rid, r)); 1318121905Simp} 1319121905Simp 132052506Simpstatic device_method_t pccard_methods[] = { 132152506Simp /* Device interface */ 132252506Simp DEVMETHOD(device_probe, pccard_probe), 132359193Simp DEVMETHOD(device_attach, pccard_attach), 132482378Sjon DEVMETHOD(device_detach, pccard_detach), 132552506Simp DEVMETHOD(device_shutdown, bus_generic_shutdown), 132687975Simp DEVMETHOD(device_suspend, pccard_suspend), 132787975Simp DEVMETHOD(device_resume, pccard_resume), 132852506Simp 132952506Simp /* Bus interface */ 133052506Simp DEVMETHOD(bus_print_child, pccard_print_child), 133166779Simp DEVMETHOD(bus_driver_added, pccard_driver_added), 133267333Simp DEVMETHOD(bus_child_detached, pccard_child_detached), 133367242Simp DEVMETHOD(bus_alloc_resource, pccard_alloc_resource), 133467242Simp DEVMETHOD(bus_release_resource, pccard_release_resource), 1335121905Simp DEVMETHOD(bus_activate_resource, pccard_activate_resource), 1336121905Simp DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource), 133770715Sjon DEVMETHOD(bus_setup_intr, pccard_setup_intr), 133870715Sjon DEVMETHOD(bus_teardown_intr, pccard_teardown_intr), 133952506Simp DEVMETHOD(bus_set_resource, pccard_set_resource), 134052506Simp DEVMETHOD(bus_get_resource, pccard_get_resource), 134152506Simp DEVMETHOD(bus_delete_resource, pccard_delete_resource), 1342104641Simp DEVMETHOD(bus_probe_nomatch, pccard_probe_nomatch), 134366058Simp DEVMETHOD(bus_read_ivar, pccard_read_ivar), 1344104641Simp DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str), 1345104641Simp DEVMETHOD(bus_child_location_str, pccard_child_location_str), 134652506Simp 134759193Simp /* Card Interface */ 134859193Simp DEVMETHOD(card_set_res_flags, pccard_set_res_flags), 134959193Simp DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset), 135059193Simp DEVMETHOD(card_attach_card, pccard_attach_card), 135159193Simp DEVMETHOD(card_detach_card, pccard_detach_card), 135274636Simp DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe), 135374636Simp DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach), 135497613Stakawata DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup), 135559193Simp 135652506Simp { 0, 0 } 135752506Simp}; 135852506Simp 135952506Simpstatic driver_t pccard_driver = { 136052506Simp "pccard", 136152506Simp pccard_methods, 136264850Simp sizeof(struct pccard_softc) 136352506Simp}; 136452506Simp 136552506Simpdevclass_t pccard_devclass; 136652506Simp 1367101905Simp/* Maybe we need to have a slot device? */ 136853873SimpDRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0); 1369101905SimpDRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0); 137064927SimpMODULE_VERSION(pccard, 1); 1371