pccard.c revision 166453
139136Snsouch/* $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $ */ 239136Snsouch 339136Snsouch/*- 439136Snsouch * Copyright (c) 1997 Marc Horowitz. All rights reserved. 539136Snsouch * 639136Snsouch * Redistribution and use in source and binary forms, with or without 739136Snsouch * modification, are permitted provided that the following conditions 839136Snsouch * are met: 939136Snsouch * 1. Redistributions of source code must retain the above copyright 1039136Snsouch * notice, this list of conditions and the following disclaimer. 1139136Snsouch * 2. Redistributions in binary form must reproduce the above copyright 1239136Snsouch * notice, this list of conditions and the following disclaimer in the 1339136Snsouch * documentation and/or other materials provided with the distribution. 1439136Snsouch * 3. All advertising materials mentioning features or use of this software 1539136Snsouch * must display the following acknowledgement: 1639136Snsouch * This product includes software developed by Marc Horowitz. 1739136Snsouch * 4. The name of the author may not be used to endorse or promote products 1839136Snsouch * derived from this software without specific prior written permission. 1939136Snsouch * 2039136Snsouch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2139136Snsouch * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2239136Snsouch * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2339136Snsouch * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2439136Snsouch * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2539136Snsouch * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2640783Snsouch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2739136Snsouch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2839136Snsouch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2939136Snsouch * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3039136Snsouch */ 3139136Snsouch 3239136Snsouch#include <sys/cdefs.h> 3339136Snsouch__FBSDID("$FreeBSD: head/sys/dev/pccard/pccard.c 166453 2007-02-03 07:09:36Z imp $"); 3439136Snsouch 3539136Snsouch#include <sys/param.h> 3639136Snsouch#include <sys/systm.h> 3739136Snsouch#include <sys/malloc.h> 3839136Snsouch#include <sys/module.h> 3939136Snsouch#include <sys/kernel.h> 4039136Snsouch#include <sys/queue.h> 4139136Snsouch#include <sys/sysctl.h> 4239136Snsouch#include <sys/types.h> 4339136Snsouch 4439136Snsouch#include <sys/bus.h> 4539136Snsouch#include <machine/bus.h> 4639136Snsouch#include <sys/rman.h> 4739136Snsouch#include <machine/resource.h> 4839136Snsouch 4939136Snsouch#include <net/ethernet.h> 5039136Snsouch 5139136Snsouch#include <dev/pccard/pccardreg.h> 5239136Snsouch#include <dev/pccard/pccardvar.h> 5339136Snsouch#include <dev/pccard/pccardvarp.h> 5439136Snsouch#include <dev/pccard/pccard_cis.h> 5539136Snsouch 5639136Snsouch#include "power_if.h" 5739136Snsouch#include "card_if.h" 5839136Snsouch 5939136Snsouch#define PCCARDDEBUG 6039136Snsouch 6139136Snsouch/* sysctl vars */ 6239136SnsouchSYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters"); 6339136Snsouch 6439136Snsouchint pccard_debug = 0; 6539136SnsouchTUNABLE_INT("hw.pccard.debug", &pccard_debug); 6639136SnsouchSYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW, 6739136Snsouch &pccard_debug, 0, 6839136Snsouch "pccard debug"); 6939136Snsouch 7039136Snsouchint pccard_cis_debug = 0; 7139136SnsouchTUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug); 7239136SnsouchSYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW, 7339136Snsouch &pccard_cis_debug, 0, "pccard CIS debug"); 7439136Snsouch 7539136Snsouch#ifdef PCCARDDEBUG 7639136Snsouch#define DPRINTF(arg) if (pccard_debug) printf arg 7739136Snsouch#define DEVPRINTF(arg) if (pccard_debug) device_printf arg 7839136Snsouch#define PRVERBOSE(arg) printf arg 7939136Snsouch#define DEVPRVERBOSE(arg) device_printf arg 8039136Snsouch#else 8139136Snsouch#define DPRINTF(arg) 8239136Snsouch#define DEVPRINTF(arg) 8339136Snsouch#define PRVERBOSE(arg) if (bootverbose) printf arg 8439136Snsouch#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg 8539136Snsouch#endif 8639136Snsouch 8739136Snsouchstatic int pccard_ccr_read(struct pccard_function *pf, int ccr); 8839136Snsouchstatic void pccard_ccr_write(struct pccard_function *pf, int ccr, int val); 8939136Snsouchstatic int pccard_attach_card(device_t dev); 9039136Snsouchstatic int pccard_detach_card(device_t dev); 9139136Snsouchstatic void pccard_function_init(struct pccard_function *pf); 9239136Snsouchstatic void pccard_function_free(struct pccard_function *pf); 9339136Snsouchstatic int pccard_function_enable(struct pccard_function *pf); 9439136Snsouchstatic void pccard_function_disable(struct pccard_function *pf); 9539136Snsouchstatic int pccard_probe(device_t dev); 9639136Snsouchstatic int pccard_attach(device_t dev); 9739136Snsouchstatic int pccard_detach(device_t dev); 9839136Snsouchstatic void pccard_print_resources(struct resource_list *rl, 9939136Snsouch const char *name, int type, int count, const char *format); 10039136Snsouchstatic int pccard_print_child(device_t dev, device_t child); 10139136Snsouchstatic int pccard_set_resource(device_t dev, device_t child, int type, 10239136Snsouch int rid, u_long start, u_long count); 10339136Snsouchstatic int pccard_get_resource(device_t dev, device_t child, int type, 10439136Snsouch int rid, u_long *startp, u_long *countp); 10539136Snsouchstatic void pccard_delete_resource(device_t dev, device_t child, int type, 10639136Snsouch int rid); 10739136Snsouchstatic int pccard_set_res_flags(device_t dev, device_t child, int type, 10839136Snsouch int rid, uint32_t flags); 10939136Snsouchstatic int pccard_set_memory_offset(device_t dev, device_t child, int rid, 11039136Snsouch uint32_t offset, uint32_t *deltap); 11139136Snsouchstatic void pccard_probe_nomatch(device_t cbdev, device_t child); 11239136Snsouchstatic int pccard_read_ivar(device_t bus, device_t child, int which, 11339136Snsouch u_char *result); 11439136Snsouchstatic void pccard_driver_added(device_t dev, driver_t *driver); 11539136Snsouchstatic struct resource *pccard_alloc_resource(device_t dev, 11639136Snsouch device_t child, int type, int *rid, u_long start, 11739136Snsouch u_long end, u_long count, u_int flags); 11839136Snsouchstatic int pccard_release_resource(device_t dev, device_t child, int type, 11939136Snsouch int rid, struct resource *r); 12039136Snsouchstatic void pccard_child_detached(device_t parent, device_t dev); 12139136Snsouchstatic void pccard_intr(void *arg); 12239136Snsouchstatic int pccard_setup_intr(device_t dev, device_t child, 12339136Snsouch struct resource *irq, int flags, driver_intr_t *intr, 12439136Snsouch void *arg, void **cookiep); 12539136Snsouchstatic int pccard_teardown_intr(device_t dev, device_t child, 12639136Snsouch struct resource *r, void *cookie); 12739136Snsouch 12839136Snsouchstatic const struct pccard_product * 12939136Snsouchpccard_do_product_lookup(device_t bus, device_t dev, 13039136Snsouch const struct pccard_product *tab, size_t ent_size, 13139136Snsouch pccard_product_match_fn matchfn); 13239136Snsouch 13339136Snsouch 13439136Snsouchstatic int 13539136Snsouchpccard_ccr_read(struct pccard_function *pf, int ccr) 13639136Snsouch{ 13739136Snsouch return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 13839136Snsouch pf->pf_ccr_offset + ccr)); 13939136Snsouch} 14039136Snsouch 14139136Snsouchstatic void 14239136Snsouchpccard_ccr_write(struct pccard_function *pf, int ccr, int val) 14339136Snsouch{ 14439136Snsouch if ((pf->ccr_mask) & (1 << (ccr / 2))) { 14539136Snsouch bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 14639136Snsouch pf->pf_ccr_offset + ccr, val); 14739136Snsouch } 14839136Snsouch} 14939136Snsouch 15039136Snsouchstatic int 15139136Snsouchpccard_set_default_descr(device_t dev) 15239136Snsouch{ 15339136Snsouch const char *vendorstr, *prodstr; 15439136Snsouch uint32_t vendor, prod; 15539136Snsouch char *str; 15639136Snsouch 15739136Snsouch if (pccard_get_vendor_str(dev, &vendorstr)) 15839136Snsouch return (0); 15939136Snsouch if (pccard_get_product_str(dev, &prodstr)) 16039136Snsouch return (0); 16139136Snsouch if (vendorstr != NULL && prodstr != NULL) { 16239136Snsouch str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF, 16339136Snsouch M_WAITOK); 16439136Snsouch sprintf(str, "%s %s", vendorstr, prodstr); 16539136Snsouch device_set_desc_copy(dev, str); 16639136Snsouch free(str, M_DEVBUF); 16739136Snsouch } else { 16839136Snsouch if (pccard_get_vendor(dev, &vendor)) 16939136Snsouch return (0); 17039136Snsouch if (pccard_get_product(dev, &prod)) 17139136Snsouch return (0); 17239136Snsouch str = malloc(100, M_DEVBUF, M_WAITOK); 17339136Snsouch snprintf(str, 100, "vendor=0x%x product=0x%x", vendor, prod); 17439136Snsouch device_set_desc_copy(dev, str); 17539136Snsouch free(str, M_DEVBUF); 17639136Snsouch } 17739136Snsouch return (0); 17839136Snsouch} 17939136Snsouch 18039136Snsouchstatic int 18139136Snsouchpccard_attach_card(device_t dev) 18239136Snsouch{ 18339136Snsouch struct pccard_softc *sc = PCCARD_SOFTC(dev); 18439136Snsouch struct pccard_function *pf; 18539136Snsouch struct pccard_ivar *ivar; 18639136Snsouch device_t child; 18739136Snsouch int i; 18839136Snsouch 18939136Snsouch if (!STAILQ_EMPTY(&sc->card.pf_head)) { 19039136Snsouch if (bootverbose || pccard_debug) 19139136Snsouch device_printf(dev, "Card already inserted.\n"); 19239136Snsouch } 19339136Snsouch 19439136Snsouch DEVPRINTF((dev, "chip_socket_enable\n")); 19539136Snsouch POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 19639136Snsouch 19739136Snsouch DEVPRINTF((dev, "read_cis\n")); 19839136Snsouch pccard_read_cis(sc); 19939136Snsouch 20039136Snsouch DEVPRINTF((dev, "check_cis_quirks\n")); 20139136Snsouch pccard_check_cis_quirks(dev); 20239136Snsouch 20339136Snsouch /* 20439136Snsouch * bail now if the card has no functions, or if there was an error in 20539136Snsouch * the cis. 20639136Snsouch */ 20739136Snsouch 20839136Snsouch if (sc->card.error) { 20939136Snsouch device_printf(dev, "CARD ERROR!\n"); 21039136Snsouch return (1); 21139136Snsouch } 21239136Snsouch if (STAILQ_EMPTY(&sc->card.pf_head)) { 21339136Snsouch device_printf(dev, "Card has no functions!\n"); 21439136Snsouch return (1); 21539136Snsouch } 21639136Snsouch 21739136Snsouch if (bootverbose || pccard_debug) 21839136Snsouch pccard_print_cis(dev); 21939136Snsouch 22039136Snsouch DEVPRINTF((dev, "functions scanning\n")); 22139136Snsouch i = -1; 22239136Snsouch STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 22339136Snsouch i++; 22439136Snsouch if (STAILQ_EMPTY(&pf->cfe_head)) { 22539136Snsouch device_printf(dev, 22639136Snsouch "Function %d has no config entries.!\n", i); 22739136Snsouch continue; 22839136Snsouch } 22939136Snsouch pf->sc = sc; 23039136Snsouch pf->cfe = NULL; 23139136Snsouch pf->dev = NULL; 23239136Snsouch } 23339136Snsouch DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1, 23439136Snsouch pccard_mfc(sc))); 23539136Snsouch 23639136Snsouch STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 23739136Snsouch if (STAILQ_EMPTY(&pf->cfe_head)) 23839136Snsouch continue; 23939136Snsouch /* 24039136Snsouch * In NetBSD, the drivers are responsible for activating 24139136Snsouch * each function of a card. I think that in FreeBSD we 24239136Snsouch * want to activate them enough for the usual bus_*_resource 24339136Snsouch * routines will do the right thing. This many mean a 24439136Snsouch * departure from the current NetBSD model. 24539136Snsouch * 24639136Snsouch * This seems to work well in practice for most cards. 24739136Snsouch * However, there are two cases that are problematic. 24839136Snsouch * If a driver wishes to pick and chose which config 24939136Snsouch * entry to use, then this method falls down. These 25039136Snsouch * are usually older cards. In addition, there are 25139136Snsouch * some cards that have multiple hardware units on the 25239136Snsouch * cards, but presents only one CIS chain. These cards 25339136Snsouch * are combination cards, but only one of these units 25439136Snsouch * can be on at a time. 25539136Snsouch */ 25639136Snsouch ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, 25739136Snsouch M_WAITOK | M_ZERO); 25839136Snsouch resource_list_init(&ivar->resources); 25939136Snsouch child = device_add_child(dev, NULL, -1); 26039136Snsouch device_set_ivars(child, ivar); 26139136Snsouch ivar->pf = pf; 26239136Snsouch pf->dev = child; 26339136Snsouch /* 26439136Snsouch * XXX We might want to move the next three lines into 26539136Snsouch * XXX the pccard interface layer. For the moment, this 26639136Snsouch * XXX is OK, but some drivers want to pick the config 26739136Snsouch * XXX entry to use as well as some address tweaks (mostly 26839136Snsouch * XXX due to bugs in decode logic that makes some 26939136Snsouch * XXX addresses illegal or broken). 27039136Snsouch */ 27139136Snsouch pccard_function_init(pf); 27239520Snsouch if (sc->sc_enabled_count == 0) 27339136Snsouch POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 27439136Snsouch if (pccard_function_enable(pf) == 0 && 27539136Snsouch pccard_set_default_descr(child) == 0 && 27639136Snsouch device_probe_and_attach(child) == 0) { 27739136Snsouch DEVPRINTF((sc->dev, "function %d CCR at %d " 27839136Snsouch "offset %x mask %x: " 27939136Snsouch "%x %x %x %x, %x %x %x %x, %x\n", 28039520Snsouch pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 28139520Snsouch pf->ccr_mask, pccard_ccr_read(pf, 0x00), 28239136Snsouch pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 28339136Snsouch pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 28439136Snsouch pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 28539136Snsouch pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 28639136Snsouch } else { 28739136Snsouch if (pf->cfe != NULL) 28839136Snsouch pccard_function_disable(pf); 28940783Snsouch } 29040783Snsouch } 29140783Snsouch return (0); 29240783Snsouch} 29340783Snsouch 29440783Snsouchstatic int 29540783Snsouchpccard_detach_card(device_t dev) 29639136Snsouch{ 29739520Snsouch struct pccard_softc *sc = PCCARD_SOFTC(dev); 29839520Snsouch struct pccard_function *pf; 29939520Snsouch struct pccard_config_entry *cfe; 30039520Snsouch struct pccard_ivar *devi; 30139136Snsouch int state; 30239136Snsouch 30339136Snsouch /* 30439136Snsouch * We are running on either the PCCARD socket's event thread 30539136Snsouch * or in user context detaching a device by user request. 30639136Snsouch */ 30739520Snsouch STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 30839136Snsouch if (pf->dev == NULL) 30939136Snsouch continue; 31039136Snsouch state = device_get_state(pf->dev); 31139136Snsouch if (state == DS_ATTACHED || state == DS_BUSY) 31239136Snsouch device_detach(pf->dev); 31339136Snsouch if (pf->cfe != NULL) 31439136Snsouch pccard_function_disable(pf); 31539136Snsouch pccard_function_free(pf); 31639520Snsouch devi = PCCARD_IVAR(pf->dev); 31739520Snsouch device_delete_child(dev, pf->dev); 31839136Snsouch free(devi, M_DEVBUF); 31939520Snsouch } 32039520Snsouch if (sc->sc_enabled_count == 0) 32139520Snsouch POWER_DISABLE_SOCKET(device_get_parent(dev), dev); 32239136Snsouch 32339136Snsouch while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) { 32439136Snsouch while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) { 32539136Snsouch STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list); 32639136Snsouch free(cfe, M_DEVBUF); 32739136Snsouch } 32839136Snsouch STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list); 32939136Snsouch free(pf, M_DEVBUF); 33039136Snsouch } 33139136Snsouch STAILQ_INIT(&sc->card.pf_head); 33239136Snsouch return (0); 33339136Snsouch} 33439136Snsouch 33539136Snsouchstatic const struct pccard_product * 33639136Snsouchpccard_do_product_lookup(device_t bus, device_t dev, 33739136Snsouch const struct pccard_product *tab, size_t ent_size, 33839136Snsouch pccard_product_match_fn matchfn) 33939136Snsouch{ 34039136Snsouch const struct pccard_product *ent; 34140783Snsouch int matches; 34240783Snsouch uint32_t vendor; 34340783Snsouch uint32_t prod; 34440783Snsouch const char *vendorstr; 34540783Snsouch const char *prodstr; 34640783Snsouch const char *cis3str; 34740783Snsouch const char *cis4str; 34839136Snsouch 34939136Snsouch#ifdef DIAGNOSTIC 35039136Snsouch if (sizeof *ent > ent_size) 35139136Snsouch panic("pccard_product_lookup: bogus ent_size %jd", 35239136Snsouch (intmax_t) ent_size); 35339136Snsouch#endif 35439136Snsouch if (pccard_get_vendor(dev, &vendor)) 35539136Snsouch return (NULL); 35639136Snsouch if (pccard_get_product(dev, &prod)) 35739136Snsouch return (NULL); 35839136Snsouch if (pccard_get_vendor_str(dev, &vendorstr)) 35939136Snsouch return (NULL); 36039136Snsouch if (pccard_get_product_str(dev, &prodstr)) 36139136Snsouch return (NULL); 36239520Snsouch if (pccard_get_cis3_str(dev, &cis3str)) 36339520Snsouch return (NULL); 36439136Snsouch if (pccard_get_cis4_str(dev, &cis4str)) 36539520Snsouch return (NULL); 36639520Snsouch for (ent = tab; ent->pp_vendor != 0; ent = 36739136Snsouch (const struct pccard_product *) ((const char *) ent + ent_size)) { 36839520Snsouch matches = 1; 36939520Snsouch if (ent->pp_vendor == PCCARD_VENDOR_ANY && 37039520Snsouch ent->pp_product == PCCARD_PRODUCT_ANY && 37139520Snsouch ent->pp_cis[0] == NULL && 37239520Snsouch ent->pp_cis[1] == NULL) { 37339520Snsouch if (ent->pp_name) 37439520Snsouch device_printf(dev, 37539520Snsouch "Total wildcard entry ignored for %s\n", 37639520Snsouch ent->pp_name); 37739520Snsouch continue; 37839136Snsouch } 37939136Snsouch if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY && 38039136Snsouch vendor != ent->pp_vendor) 38139520Snsouch matches = 0; 38239520Snsouch if (matches && ent->pp_product != PCCARD_PRODUCT_ANY && 38339136Snsouch prod != ent->pp_product) 38439136Snsouch matches = 0; 38539136Snsouch if (matches && ent->pp_cis[0] && 38639136Snsouch (vendorstr == NULL || 38739520Snsouch strcmp(ent->pp_cis[0], vendorstr) != 0)) 38839520Snsouch matches = 0; 38939520Snsouch if (matches && ent->pp_cis[1] && 39039520Snsouch (prodstr == NULL || 39139900Snsouch strcmp(ent->pp_cis[1], prodstr) != 0)) 39239520Snsouch matches = 0; 39339136Snsouch if (matches && ent->pp_cis[2] && 39439136Snsouch (cis3str == NULL || 39539520Snsouch strcmp(ent->pp_cis[2], cis3str) != 0)) 39639520Snsouch matches = 0; 39739520Snsouch if (matches && ent->pp_cis[3] && 39839520Snsouch (cis4str == NULL || 39939136Snsouch strcmp(ent->pp_cis[3], cis4str) != 0)) 40039136Snsouch matches = 0; 40139136Snsouch if (matchfn != NULL) 40239136Snsouch matches = (*matchfn)(dev, ent, matches); 40339136Snsouch if (matches) 40439136Snsouch return (ent); 40539136Snsouch } 40639136Snsouch return (NULL); 40739136Snsouch} 40839136Snsouch 40939136Snsouch/* 41039136Snsouch * Initialize a PCCARD function. May be called as long as the function is 41139136Snsouch * disabled. 41239136Snsouch * 41339136Snsouch * Note: pccard_function_init should not keep resources allocated. It should 41439136Snsouch * only set them up ala isa pnp, set the values in the rl lists, and return. 41539136Snsouch * Any resource held after pccard_function_init is called is a bug. However, 41639136Snsouch * the bus routines to get the resources also assume that pccard_function_init 41739136Snsouch * does this, so they need to be fixed too. 41839136Snsouch */ 41939136Snsouchstatic void 42039136Snsouchpccard_function_init(struct pccard_function *pf) 42139136Snsouch{ 42239136Snsouch struct pccard_config_entry *cfe; 42339136Snsouch struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 42439136Snsouch struct resource_list *rl = &devi->resources; 42539136Snsouch struct resource_list_entry *rle; 42639136Snsouch struct resource *r = 0; 42739136Snsouch device_t bus; 42839136Snsouch u_long start, end, len; 42939136Snsouch int i, rid, spaces; 43039136Snsouch 43139136Snsouch if (pf->pf_flags & PFF_ENABLED) { 43239136Snsouch printf("pccard_function_init: function is enabled"); 43339136Snsouch return; 43439136Snsouch } 43539136Snsouch bus = device_get_parent(pf->dev); 43639136Snsouch /* Remember which configuration entry we are using. */ 43739136Snsouch STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) { 43839136Snsouch if (cfe->iftype != PCCARD_IFTYPE_IO) 43939136Snsouch continue; 44039136Snsouch spaces = 0; 44139136Snsouch for (i = 0; i < cfe->num_iospace; i++) { 44239136Snsouch start = cfe->iospace[i].start; 44339136Snsouch if (start) 44439136Snsouch end = start + cfe->iospace[i].length - 1; 44539136Snsouch else 44639136Snsouch end = ~0UL; 44739136Snsouch DEVPRINTF((bus, "I/O rid %d start %lx end %lx\n", 44839136Snsouch i, start, end)); 44939136Snsouch rid = i; 45039136Snsouch len = cfe->iospace[i].length; 45139136Snsouch r = bus_alloc_resource(bus, SYS_RES_IOPORT, &rid, 45239136Snsouch start, end, len, rman_make_alignment_flags(len)); 45339136Snsouch if (r == NULL) 45439136Snsouch goto not_this_one; 45539136Snsouch rle = resource_list_add(rl, SYS_RES_IOPORT, 45639136Snsouch rid, rman_get_start(r), rman_get_end(r), 45739136Snsouch cfe->iospace[i].length); 45839136Snsouch if (rle == NULL) 45939136Snsouch panic("Cannot add resource rid %d IOPORT", rid); 46039136Snsouch rle->res = r; 46139136Snsouch spaces++; 46239136Snsouch } 46339136Snsouch for (i = 0; i < cfe->num_memspace; i++) { 46439136Snsouch start = cfe->memspace[i].hostaddr; 46539136Snsouch if (start) 46639136Snsouch end = start + cfe->memspace[i].length - 1; 46739136Snsouch else 46839136Snsouch end = ~0UL; 46939136Snsouch DEVPRINTF((bus, "Memory rid %d start %lx end %lx\n", 47039136Snsouch i, start, end)); 47139136Snsouch rid = i; 47239136Snsouch len = cfe->memspace[i].length; 47339136Snsouch r = bus_alloc_resource(bus, SYS_RES_MEMORY, &rid, 47439136Snsouch start, end, len, rman_make_alignment_flags(len)); 47539136Snsouch if (r == NULL) 47639136Snsouch goto not_this_one; 47739136Snsouch rle = resource_list_add(rl, SYS_RES_MEMORY, 47839136Snsouch rid, rman_get_start(r), rman_get_end(r), 47939136Snsouch cfe->memspace[i].length); 48039136Snsouch if (rle == NULL) 48139136Snsouch panic("Cannot add resource rid %d MEM", rid); 48239136Snsouch rle->res = r; 48339136Snsouch spaces++; 48439136Snsouch } 48539136Snsouch if (spaces == 0) { 48639136Snsouch DEVPRINTF((bus, "Neither memory nor I/O mapped\n")); 48739136Snsouch goto not_this_one; 48839136Snsouch } 48939136Snsouch if (cfe->irqmask) { 49039136Snsouch rid = 0; 49139136Snsouch r = bus_alloc_resource_any(bus, SYS_RES_IRQ, &rid, 49239136Snsouch RF_SHAREABLE); 49339136Snsouch if (r == NULL) 49439136Snsouch goto not_this_one; 49539136Snsouch rle = resource_list_add(rl, SYS_RES_IRQ, rid, 49639136Snsouch rman_get_start(r), rman_get_end(r), 1); 49739136Snsouch if (rle == NULL) 49839136Snsouch panic("Cannot add resource rid %d IRQ", rid); 49939136Snsouch rle->res = r; 50039136Snsouch } 50139136Snsouch /* If we get to here, we've allocated all we need */ 50239136Snsouch pf->cfe = cfe; 50339136Snsouch break; 50439136Snsouch not_this_one:; 50539136Snsouch DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n", 50639136Snsouch cfe->number)); 50739136Snsouch resource_list_purge(rl); 50839136Snsouch } 50939136Snsouch} 51039136Snsouch 51139136Snsouch/* 51239136Snsouch * Free resources allocated by pccard_function_init(), May be called as long 51339136Snsouch * as the function is disabled. 51439136Snsouch * 51539136Snsouch * NOTE: This function should be unnecessary. pccard_function_init should 51639136Snsouch * never keep resources initialized. 51739136Snsouch */ 51839136Snsouchstatic void 51939136Snsouchpccard_function_free(struct pccard_function *pf) 52039136Snsouch{ 52139136Snsouch struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 52239136Snsouch struct resource_list_entry *rle; 52339136Snsouch 52439136Snsouch if (pf->pf_flags & PFF_ENABLED) { 52539136Snsouch printf("pccard_function_init: function is enabled"); 52639136Snsouch return; 52739136Snsouch } 52839136Snsouch 52939136Snsouch STAILQ_FOREACH(rle, &devi->resources, link) { 53039136Snsouch if (rle->res) { 53139136Snsouch if (rman_get_device(rle->res) != pf->sc->dev) 53239136Snsouch device_printf(pf->sc->dev, 53339136Snsouch "function_free: Resource still owned by " 53439136Snsouch "child, oops. " 53539136Snsouch "(type=%d, rid=%d, addr=%lx)\n", 53639136Snsouch rle->type, rle->rid, 53739136Snsouch rman_get_start(rle->res)); 53839136Snsouch BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev), 53939136Snsouch pf->sc->dev, rle->type, rle->rid, rle->res); 54039136Snsouch rle->res = NULL; 54139136Snsouch } 54239136Snsouch } 54339136Snsouch resource_list_free(&devi->resources); 54439136Snsouch} 54539136Snsouch 54639136Snsouchstatic void 54739136Snsouchpccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr, 54839136Snsouch bus_addr_t offset, bus_size_t size) 54939136Snsouch{ 55039136Snsouch bus_size_t iosize, tmp; 55139136Snsouch 55239136Snsouch if (addr != 0) { 55339136Snsouch if (pf->pf_mfc_iomax == 0) { 55439136Snsouch pf->pf_mfc_iobase = addr + offset; 55539136Snsouch pf->pf_mfc_iomax = pf->pf_mfc_iobase + size; 55639136Snsouch } else { 55739136Snsouch /* this makes the assumption that nothing overlaps */ 55839136Snsouch if (pf->pf_mfc_iobase > addr + offset) 55939136Snsouch pf->pf_mfc_iobase = addr + offset; 56039136Snsouch if (pf->pf_mfc_iomax < addr + offset + size) 56139136Snsouch pf->pf_mfc_iomax = addr + offset + size; 56239136Snsouch } 56339136Snsouch } 56439136Snsouch 56539136Snsouch tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase; 56639136Snsouch /* round up to nearest (2^n)-1 */ 56739136Snsouch for (iosize = 1; iosize < tmp; iosize <<= 1) 56839136Snsouch ; 56939136Snsouch iosize--; 57039136Snsouch 57139136Snsouch DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n", 57239136Snsouch (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1))); 57339136Snsouch pccard_ccr_write(pf, PCCARD_CCR_IOBASE0, 57439136Snsouch pf->pf_mfc_iobase & 0xff); 57539136Snsouch pccard_ccr_write(pf, PCCARD_CCR_IOBASE1, 57639136Snsouch (pf->pf_mfc_iobase >> 8) & 0xff); 57739136Snsouch pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0); 57839136Snsouch pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0); 57939136Snsouch pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize); 58039136Snsouch} 58139136Snsouch 58239136Snsouch/* Enable a PCCARD function */ 58339136Snsouchstatic int 58439136Snsouchpccard_function_enable(struct pccard_function *pf) 58539136Snsouch{ 58639136Snsouch struct pccard_function *tmp; 58739136Snsouch int reg; 58839136Snsouch device_t dev = pf->sc->dev; 58939136Snsouch 59039136Snsouch if (pf->cfe == NULL) { 59139136Snsouch DEVPRVERBOSE((dev, "No config entry could be allocated.\n")); 59239136Snsouch return (ENOMEM); 59339136Snsouch } 59439136Snsouch 59539136Snsouch /* 59639136Snsouch * Increase the reference count on the socket, enabling power, if 59739136Snsouch * necessary. 59839136Snsouch */ 59939136Snsouch pf->sc->sc_enabled_count++; 60039136Snsouch 60139136Snsouch if (pf->pf_flags & PFF_ENABLED) { 60239136Snsouch /* 60339136Snsouch * Don't do anything if we're already enabled. 60439136Snsouch */ 60539136Snsouch return (0); 60639136Snsouch } 60739136Snsouch 60839136Snsouch /* 60939136Snsouch * it's possible for different functions' CCRs to be in the same 61039136Snsouch * underlying page. Check for that. 61139136Snsouch */ 61239136Snsouch STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 61339136Snsouch if ((tmp->pf_flags & PFF_ENABLED) && 61439136Snsouch (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 61539136Snsouch ((pf->ccr_base + PCCARD_CCR_SIZE) <= 61639136Snsouch (tmp->ccr_base - tmp->pf_ccr_offset + 61739136Snsouch tmp->pf_ccr_realsize))) { 61839136Snsouch pf->pf_ccrt = tmp->pf_ccrt; 61939136Snsouch pf->pf_ccrh = tmp->pf_ccrh; 62039136Snsouch pf->pf_ccr_realsize = tmp->pf_ccr_realsize; 62139136Snsouch 62239136Snsouch /* 62339136Snsouch * pf->pf_ccr_offset = (tmp->pf_ccr_offset - 62439136Snsouch * tmp->ccr_base) + pf->ccr_base; 62539136Snsouch */ 62639136Snsouch /* pf->pf_ccr_offset = 62739136Snsouch (tmp->pf_ccr_offset + pf->ccr_base) - 62839136Snsouch tmp->ccr_base; */ 62939136Snsouch pf->pf_ccr_window = tmp->pf_ccr_window; 63039136Snsouch break; 63139136Snsouch } 63239136Snsouch } 63339136Snsouch if (tmp == NULL) { 63439136Snsouch pf->ccr_rid = 0; 63539136Snsouch pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 63639136Snsouch &pf->ccr_rid, 0, ~0, PCCARD_MEM_PAGE_SIZE, RF_ACTIVE); 63739136Snsouch if (!pf->ccr_res) 63839136Snsouch goto bad; 63939136Snsouch DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n", 64039136Snsouch rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res), 64139136Snsouch pf->ccr_base)); 64239136Snsouch CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, 64339520Snsouch pf->ccr_rid, PCCARD_A_MEM_ATTR); 64439136Snsouch CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev, 64539520Snsouch pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset); 64639520Snsouch pf->pf_ccrt = rman_get_bustag(pf->ccr_res); 64739136Snsouch pf->pf_ccrh = rman_get_bushandle(pf->ccr_res); 64839520Snsouch pf->pf_ccr_realsize = 1; 64939136Snsouch } 65039136Snsouch 65139136Snsouch reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX); 65239136Snsouch reg |= PCCARD_CCR_OPTION_LEVIREQ; 65339136Snsouch if (pccard_mfc(pf->sc)) { 65439520Snsouch reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE | 65539136Snsouch PCCARD_CCR_OPTION_ADDR_DECODE); 65639136Snsouch /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */ 65739136Snsouch } 65839136Snsouch pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg); 65939136Snsouch 66039136Snsouch reg = 0; 66139136Snsouch if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0) 66239136Snsouch reg |= PCCARD_CCR_STATUS_IOIS8; 66339136Snsouch if (pf->cfe->flags & PCCARD_CFE_AUDIO) 66439136Snsouch reg |= PCCARD_CCR_STATUS_AUDIO; 66539136Snsouch pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg); 66639136Snsouch 66739136Snsouch pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0); 66839136Snsouch 66939136Snsouch if (pccard_mfc(pf->sc)) 67039136Snsouch pccard_mfc_adjust_iobase(pf, 0, 0, 0); 67139136Snsouch 67239136Snsouch#ifdef PCCARDDEBUG 67339136Snsouch if (pccard_debug) { 67439136Snsouch STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 67539136Snsouch device_printf(tmp->sc->dev, 67639136Snsouch "function %d CCR at %d offset %x: " 67739136Snsouch "%x %x %x %x, %x %x %x %x, %x\n", 67839136Snsouch tmp->number, tmp->pf_ccr_window, 67939136Snsouch tmp->pf_ccr_offset, 68039136Snsouch pccard_ccr_read(tmp, 0x00), 68139136Snsouch pccard_ccr_read(tmp, 0x02), 68239136Snsouch pccard_ccr_read(tmp, 0x04), 68339136Snsouch pccard_ccr_read(tmp, 0x06), 68439136Snsouch pccard_ccr_read(tmp, 0x0A), 68539136Snsouch pccard_ccr_read(tmp, 0x0C), 68639520Snsouch pccard_ccr_read(tmp, 0x0E), 68739136Snsouch pccard_ccr_read(tmp, 0x10), 68839136Snsouch pccard_ccr_read(tmp, 0x12)); 68939136Snsouch } 69039136Snsouch } 69139136Snsouch#endif 69239136Snsouch pf->pf_flags |= PFF_ENABLED; 69339136Snsouch return (0); 69439136Snsouch 69539136Snsouch bad: 69639136Snsouch /* 69739136Snsouch * Decrement the reference count, and power down the socket, if 69839136Snsouch * necessary. 69939136Snsouch */ 70039136Snsouch pf->sc->sc_enabled_count--; 70139136Snsouch DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count)); 70239136Snsouch 70339136Snsouch return (1); 70439136Snsouch} 70539136Snsouch 70639136Snsouch/* Disable PCCARD function. */ 70739136Snsouchstatic void 70839136Snsouchpccard_function_disable(struct pccard_function *pf) 70939136Snsouch{ 71039136Snsouch struct pccard_function *tmp; 71139136Snsouch device_t dev = pf->sc->dev; 71239136Snsouch 71339136Snsouch if (pf->cfe == NULL) 71439136Snsouch panic("pccard_function_disable: function not initialized"); 71539136Snsouch 71639136Snsouch if ((pf->pf_flags & PFF_ENABLED) == 0) { 71739136Snsouch /* 71839136Snsouch * Don't do anything if we're already disabled. 71939136Snsouch */ 72039136Snsouch return; 72139136Snsouch } 72239136Snsouch 72339136Snsouch if (pf->intr_handler != NULL) { 72439136Snsouch struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 72539136Snsouch struct resource_list_entry *rle = 72639136Snsouch resource_list_find(&devi->resources, SYS_RES_IRQ, 0); 72739136Snsouch if (rle == NULL) 72839136Snsouch panic("Can't disable an interrupt with no IRQ res\n"); 72939136Snsouch BUS_TEARDOWN_INTR(dev, pf->dev, rle->res, 73039136Snsouch pf->intr_handler_cookie); 73139136Snsouch } 73239136Snsouch 73339136Snsouch /* 73439136Snsouch * it's possible for different functions' CCRs to be in the same 73539136Snsouch * underlying page. Check for that. Note we mark us as disabled 73639136Snsouch * first to avoid matching ourself. 73739136Snsouch */ 73839136Snsouch 73939136Snsouch pf->pf_flags &= ~PFF_ENABLED; 74039136Snsouch STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 74139136Snsouch if ((tmp->pf_flags & PFF_ENABLED) && 74239136Snsouch (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 74339136Snsouch ((pf->ccr_base + PCCARD_CCR_SIZE) <= 74439136Snsouch (tmp->ccr_base - tmp->pf_ccr_offset + 74539136Snsouch tmp->pf_ccr_realsize))) 74639136Snsouch break; 74739136Snsouch } 74839136Snsouch 74939136Snsouch /* Not used by anyone else; unmap the CCR. */ 75039136Snsouch if (tmp == NULL) { 75139136Snsouch bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid, 75239136Snsouch pf->ccr_res); 75339136Snsouch pf->ccr_res = NULL; 75439136Snsouch } 75539136Snsouch 75639136Snsouch /* 75739136Snsouch * Decrement the reference count, and power down the socket, if 75839136Snsouch * necessary. 75939136Snsouch */ 76039136Snsouch pf->sc->sc_enabled_count--; 76139136Snsouch} 76239136Snsouch 76339136Snsouch#define PCCARD_NPORT 2 76439136Snsouch#define PCCARD_NMEM 5 76539136Snsouch#define PCCARD_NIRQ 1 76639136Snsouch#define PCCARD_NDRQ 0 76739136Snsouch 76839136Snsouchstatic int 76939136Snsouchpccard_probe(device_t dev) 77039136Snsouch{ 77139136Snsouch device_set_desc(dev, "16-bit PCCard bus"); 77239136Snsouch return (0); 77339136Snsouch} 77439136Snsouch 77539136Snsouchstatic int 77639136Snsouchpccard_attach(device_t dev) 77739136Snsouch{ 77839136Snsouch struct pccard_softc *sc = PCCARD_SOFTC(dev); 77939136Snsouch int err; 78039136Snsouch 78139136Snsouch sc->dev = dev; 78239136Snsouch sc->sc_enabled_count = 0; 78339136Snsouch if ((err = pccard_device_create(sc)) != 0) 78439136Snsouch return (err); 78539136Snsouch STAILQ_INIT(&sc->card.pf_head); 78639136Snsouch return (bus_generic_attach(dev)); 78739136Snsouch} 78839136Snsouch 78939136Snsouchstatic int 79039136Snsouchpccard_detach(device_t dev) 79139136Snsouch{ 79239136Snsouch pccard_detach_card(dev); 79339136Snsouch pccard_device_destroy(device_get_softc(dev)); 79439136Snsouch return (0); 79539136Snsouch} 79639136Snsouch 79739136Snsouchstatic int 79839136Snsouchpccard_suspend(device_t self) 79939520Snsouch{ 80039136Snsouch pccard_detach_card(self); 80139136Snsouch return (0); 80239136Snsouch} 803 804static 805int 806pccard_resume(device_t self) 807{ 808 return (0); 809} 810 811static void 812pccard_print_resources(struct resource_list *rl, const char *name, int type, 813 int count, const char *format) 814{ 815 struct resource_list_entry *rle; 816 int printed; 817 int i; 818 819 printed = 0; 820 for (i = 0; i < count; i++) { 821 rle = resource_list_find(rl, type, i); 822 if (rle != NULL) { 823 if (printed == 0) 824 printf(" %s ", name); 825 else if (printed > 0) 826 printf(","); 827 printed++; 828 printf(format, rle->start); 829 if (rle->count > 1) { 830 printf("-"); 831 printf(format, rle->start + rle->count - 1); 832 } 833 } else if (i > 3) { 834 /* check the first few regardless */ 835 break; 836 } 837 } 838} 839 840static int 841pccard_print_child(device_t dev, device_t child) 842{ 843 struct pccard_ivar *devi = PCCARD_IVAR(child); 844 struct resource_list *rl = &devi->resources; 845 int retval = 0; 846 847 retval += bus_print_child_header(dev, child); 848 retval += printf(" at"); 849 850 if (devi != NULL) { 851 pccard_print_resources(rl, "port", SYS_RES_IOPORT, 852 PCCARD_NPORT, "%#lx"); 853 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY, 854 PCCARD_NMEM, "%#lx"); 855 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ, 856 "%ld"); 857 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ, 858 "%ld"); 859 retval += printf(" function %d config %d", devi->pf->number, 860 devi->pf->cfe->number); 861 } 862 863 retval += bus_print_child_footer(dev, child); 864 865 return (retval); 866} 867 868static int 869pccard_set_resource(device_t dev, device_t child, int type, int rid, 870 u_long start, u_long count) 871{ 872 struct pccard_ivar *devi = PCCARD_IVAR(child); 873 struct resource_list *rl = &devi->resources; 874 875 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 876 && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 877 return (EINVAL); 878 if (rid < 0) 879 return (EINVAL); 880 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT) 881 return (EINVAL); 882 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM) 883 return (EINVAL); 884 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ) 885 return (EINVAL); 886 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ) 887 return (EINVAL); 888 889 resource_list_add(rl, type, rid, start, start + count - 1, count); 890 if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev, 891 type, &rid, start, start + count - 1, count, 0)) 892 return 0; 893 else 894 return ENOMEM; 895} 896 897static int 898pccard_get_resource(device_t dev, device_t child, int type, int rid, 899 u_long *startp, u_long *countp) 900{ 901 struct pccard_ivar *devi = PCCARD_IVAR(child); 902 struct resource_list *rl = &devi->resources; 903 struct resource_list_entry *rle; 904 905 rle = resource_list_find(rl, type, rid); 906 if (rle == NULL) 907 return (ENOENT); 908 909 if (startp != NULL) 910 *startp = rle->start; 911 if (countp != NULL) 912 *countp = rle->count; 913 914 return (0); 915} 916 917static void 918pccard_delete_resource(device_t dev, device_t child, int type, int rid) 919{ 920 struct pccard_ivar *devi = PCCARD_IVAR(child); 921 struct resource_list *rl = &devi->resources; 922 resource_list_delete(rl, type, rid); 923} 924 925static int 926pccard_set_res_flags(device_t dev, device_t child, int type, int rid, 927 uint32_t flags) 928{ 929 return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type, 930 rid, flags)); 931} 932 933static int 934pccard_set_memory_offset(device_t dev, device_t child, int rid, 935 uint32_t offset, uint32_t *deltap) 936 937{ 938 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid, 939 offset, deltap)); 940} 941 942static void 943pccard_probe_nomatch(device_t bus, device_t child) 944{ 945 struct pccard_ivar *devi = PCCARD_IVAR(child); 946 struct pccard_function *pf = devi->pf; 947 struct pccard_softc *sc = PCCARD_SOFTC(bus); 948 int i; 949 950 device_printf(bus, "<unknown card>"); 951 printf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) " 952 "at function %d\n", sc->card.manufacturer, sc->card.product, 953 pf->function, pf->number); 954 device_printf(bus, " CIS info: "); 955 for (i = 0; sc->card.cis1_info[i] != NULL && i < 4; i++) 956 printf("%s%s", i > 0 ? ", " : "", sc->card.cis1_info[i]); 957 printf("\n"); 958 return; 959} 960 961static int 962pccard_child_location_str(device_t bus, device_t child, char *buf, 963 size_t buflen) 964{ 965 struct pccard_ivar *devi = PCCARD_IVAR(child); 966 struct pccard_function *pf = devi->pf; 967 968 snprintf(buf, buflen, "function=%d", pf->number); 969 return (0); 970} 971 972/* XXX Maybe this should be in subr_bus? */ 973static void 974pccard_safe_quote(char *dst, const char *src, size_t len) 975{ 976 char *walker = dst, *ep = dst + len - 1; 977 978 if (len == 0) 979 return; 980 while (src != NULL && walker < ep) 981 { 982 if (*src == '"') { 983 if (ep - walker < 2) 984 break; 985 *walker++ = '\\'; 986 } 987 *walker++ = *src++; 988 } 989 *walker = '\0'; 990} 991 992static int 993pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf, 994 size_t buflen) 995{ 996 struct pccard_ivar *devi = PCCARD_IVAR(child); 997 struct pccard_function *pf = devi->pf; 998 struct pccard_softc *sc = PCCARD_SOFTC(bus); 999 char cis0[128], cis1[128]; 1000 1001 pccard_safe_quote(cis0, sc->card.cis1_info[0], sizeof(cis0)); 1002 pccard_safe_quote(cis1, sc->card.cis1_info[1], sizeof(cis1)); 1003 snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x " 1004 "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d", 1005 sc->card.manufacturer, sc->card.product, cis0, cis1, pf->function); 1006 return (0); 1007} 1008 1009static int 1010pccard_read_ivar(device_t bus, device_t child, int which, u_char *result) 1011{ 1012 struct pccard_ivar *devi = PCCARD_IVAR(child); 1013 struct pccard_function *pf = devi->pf; 1014 struct pccard_softc *sc = PCCARD_SOFTC(bus); 1015 1016 if (!pf) 1017 panic("No pccard function pointer"); 1018 switch (which) { 1019 default: 1020 return (EINVAL); 1021 case PCCARD_IVAR_ETHADDR: 1022 bcopy(pf->pf_funce_lan_nid, result, ETHER_ADDR_LEN); 1023 break; 1024 case PCCARD_IVAR_VENDOR: 1025 *(uint32_t *)result = sc->card.manufacturer; 1026 break; 1027 case PCCARD_IVAR_PRODUCT: 1028 *(uint32_t *)result = sc->card.product; 1029 break; 1030 case PCCARD_IVAR_PRODEXT: 1031 *(uint16_t *)result = sc->card.prodext; 1032 break; 1033 case PCCARD_IVAR_FUNCTION: 1034 *(uint32_t *)result = pf->function; 1035 break; 1036 case PCCARD_IVAR_FUNCTION_NUMBER: 1037 *(uint32_t *)result = pf->number; 1038 break; 1039 case PCCARD_IVAR_VENDOR_STR: 1040 *(const char **)result = sc->card.cis1_info[0]; 1041 break; 1042 case PCCARD_IVAR_PRODUCT_STR: 1043 *(const char **)result = sc->card.cis1_info[1]; 1044 break; 1045 case PCCARD_IVAR_CIS3_STR: 1046 *(const char **)result = sc->card.cis1_info[2]; 1047 break; 1048 case PCCARD_IVAR_CIS4_STR: 1049 *(const char **)result = sc->card.cis1_info[3]; 1050 break; 1051 } 1052 return (0); 1053} 1054 1055static void 1056pccard_driver_added(device_t dev, driver_t *driver) 1057{ 1058 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1059 struct pccard_function *pf; 1060 device_t child; 1061 1062 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 1063 if (STAILQ_EMPTY(&pf->cfe_head)) 1064 continue; 1065 child = pf->dev; 1066 if (device_get_state(child) != DS_NOTPRESENT) 1067 continue; 1068 if (pccard_function_enable(pf) == 0 && 1069 device_probe_and_attach(child) == 0) { 1070 DEVPRINTF((sc->dev, "function %d CCR at %d " 1071 "offset %x: %x %x %x %x, %x %x %x %x, %x\n", 1072 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 1073 pccard_ccr_read(pf, 0x00), 1074 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 1075 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 1076 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 1077 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 1078 } else { 1079 if (pf->cfe != NULL) 1080 pccard_function_disable(pf); 1081 } 1082 } 1083 return; 1084} 1085 1086static struct resource * 1087pccard_alloc_resource(device_t dev, device_t child, int type, int *rid, 1088 u_long start, u_long end, u_long count, u_int flags) 1089{ 1090 struct pccard_ivar *dinfo; 1091 struct resource_list_entry *rle = 0; 1092 int passthrough = (device_get_parent(child) != dev); 1093 int isdefault = (start == 0 && end == ~0UL && count == 1); 1094 struct resource *r = NULL; 1095 1096 /* XXX I'm no longer sure this is right */ 1097 if (passthrough) { 1098 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 1099 type, rid, start, end, count, flags)); 1100 } 1101 1102 dinfo = device_get_ivars(child); 1103 rle = resource_list_find(&dinfo->resources, type, *rid); 1104 1105 if (rle == NULL && isdefault) 1106 return (NULL); /* no resource of that type/rid */ 1107 if (rle == NULL || rle->res == NULL) { 1108 /* XXX Need to adjust flags */ 1109 r = bus_alloc_resource(dev, type, rid, start, end, 1110 count, flags); 1111 if (r == NULL) 1112 goto bad; 1113 resource_list_add(&dinfo->resources, type, *rid, 1114 rman_get_start(r), rman_get_end(r), count); 1115 rle = resource_list_find(&dinfo->resources, type, *rid); 1116 if (!rle) 1117 goto bad; 1118 rle->res = r; 1119 } 1120 /* 1121 * If dev doesn't own the device, then we can't give this device 1122 * out. 1123 */ 1124 if (rman_get_device(rle->res) != dev) 1125 return (NULL); 1126 rman_set_device(rle->res, child); 1127 if (flags & RF_ACTIVE) 1128 BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, rle->res); 1129 return (rle->res); 1130bad:; 1131 device_printf(dev, "WARNING: Resource not reserved by pccard\n"); 1132 return (NULL); 1133} 1134 1135static int 1136pccard_release_resource(device_t dev, device_t child, int type, int rid, 1137 struct resource *r) 1138{ 1139 struct pccard_ivar *dinfo; 1140 int passthrough = (device_get_parent(child) != dev); 1141 struct resource_list_entry *rle = 0; 1142 1143 if (passthrough) 1144 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 1145 type, rid, r); 1146 1147 dinfo = device_get_ivars(child); 1148 1149 rle = resource_list_find(&dinfo->resources, type, rid); 1150 1151 if (!rle) { 1152 device_printf(dev, "Allocated resource not found, " 1153 "%d %x %lx %lx\n", 1154 type, rid, rman_get_start(r), rman_get_size(r)); 1155 return ENOENT; 1156 } 1157 if (!rle->res) { 1158 device_printf(dev, "Allocated resource not recorded\n"); 1159 return ENOENT; 1160 } 1161 /* 1162 * Deactivate the resource (since it is being released), and 1163 * assign it to the bus. 1164 */ 1165 BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, rle->res); 1166 rman_set_device(rle->res, dev); 1167 return (0); 1168} 1169 1170static void 1171pccard_child_detached(device_t parent, device_t dev) 1172{ 1173 struct pccard_ivar *ivar = PCCARD_IVAR(dev); 1174 struct pccard_function *pf = ivar->pf; 1175 1176 pccard_function_disable(pf); 1177} 1178 1179static void 1180pccard_intr(void *arg) 1181{ 1182 struct pccard_function *pf = (struct pccard_function*) arg; 1183 int reg; 1184 int doisr = 1; 1185 1186 /* 1187 * MFC cards know if they interrupted, so we have to ack the 1188 * interrupt and call the ISR. Non-MFC cards don't have these 1189 * bits, so they always get called. Many non-MFC cards have 1190 * this bit set always upon read, but some do not. 1191 * 1192 * We always ack the interrupt, even if there's no ISR 1193 * for the card. This is done on the theory that acking 1194 * the interrupt will pacify the card enough to keep an 1195 * interrupt storm from happening. Of course this won't 1196 * help in the non-MFC case. 1197 * 1198 * This has no impact for MPSAFEness of the client drivers. 1199 * We register this with whatever flags the intr_handler 1200 * was registered with. All these functions are MPSAFE. 1201 */ 1202 if (pccard_mfc(pf->sc)) { 1203 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS); 1204 if (reg & PCCARD_CCR_STATUS_INTR) 1205 pccard_ccr_write(pf, PCCARD_CCR_STATUS, 1206 reg & ~PCCARD_CCR_STATUS_INTR); 1207 else 1208 doisr = 0; 1209 } 1210 if (pf->intr_handler != NULL && doisr) 1211 pf->intr_handler(pf->intr_handler_arg); 1212} 1213 1214static int 1215pccard_setup_intr(device_t dev, device_t child, struct resource *irq, 1216 int flags, driver_intr_t *intr, void *arg, void **cookiep) 1217{ 1218 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1219 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1220 struct pccard_function *pf = ivar->pf; 1221 int err; 1222 1223 if (pf->intr_handler != NULL) 1224 panic("Only one interrupt handler per function allowed"); 1225 err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr, 1226 pf, cookiep); 1227 if (err != 0) 1228 return (err); 1229 pf->intr_handler = intr; 1230 pf->intr_handler_arg = arg; 1231 pf->intr_handler_cookie = *cookiep; 1232 if (pccard_mfc(sc)) { 1233 pccard_ccr_write(pf, PCCARD_CCR_OPTION, 1234 pccard_ccr_read(pf, PCCARD_CCR_OPTION) | 1235 PCCARD_CCR_OPTION_IREQ_ENABLE); 1236 } 1237 return (0); 1238} 1239 1240static int 1241pccard_teardown_intr(device_t dev, device_t child, struct resource *r, 1242 void *cookie) 1243{ 1244 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1245 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1246 struct pccard_function *pf = ivar->pf; 1247 int ret; 1248 1249 if (pccard_mfc(sc)) { 1250 pccard_ccr_write(pf, PCCARD_CCR_OPTION, 1251 pccard_ccr_read(pf, PCCARD_CCR_OPTION) & 1252 ~PCCARD_CCR_OPTION_IREQ_ENABLE); 1253 } 1254 ret = bus_generic_teardown_intr(dev, child, r, cookie); 1255 if (ret == 0) { 1256 pf->intr_handler = NULL; 1257 pf->intr_handler_arg = NULL; 1258 pf->intr_handler_cookie = NULL; 1259 } 1260 1261 return (ret); 1262} 1263 1264static int 1265pccard_activate_resource(device_t brdev, device_t child, int type, int rid, 1266 struct resource *r) 1267{ 1268 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1269 struct pccard_function *pf = ivar->pf; 1270 1271 switch(type) { 1272 case SYS_RES_IOPORT: 1273 /* 1274 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC 1275 * card. 1276 */ 1277 if (pccard_mfc(pf->sc)) 1278 pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0, 1279 rman_get_size(r)); 1280 break; 1281 default: 1282 break; 1283 } 1284 return (bus_generic_activate_resource(brdev, child, type, rid, r)); 1285} 1286 1287static int 1288pccard_deactivate_resource(device_t brdev, device_t child, int type, 1289 int rid, struct resource *r) 1290{ 1291 /* XXX undo pccard_activate_resource? XXX */ 1292 return (bus_generic_deactivate_resource(brdev, child, type, rid, r)); 1293} 1294 1295static int 1296pccard_attr_read_impl(device_t brdev, device_t child, uint32_t offset, 1297 uint8_t *val) 1298{ 1299 struct pccard_ivar *devi = PCCARD_IVAR(child); 1300 struct pccard_function *pf = devi->pf; 1301 1302 /* 1303 * Optimization. Most of the time, devices want to access 1304 * the same page of the attribute memory that the CCR is in. 1305 * We take advantage of this fact here. 1306 */ 1307 if (offset / PCCARD_MEM_PAGE_SIZE == 1308 pf->ccr_base / PCCARD_MEM_PAGE_SIZE) 1309 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 1310 offset % PCCARD_MEM_PAGE_SIZE); 1311 else { 1312 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset, 1313 &offset); 1314 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, offset); 1315 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base, 1316 &offset); 1317 } 1318 return 0; 1319} 1320 1321static int 1322pccard_attr_write_impl(device_t brdev, device_t child, uint32_t offset, 1323 uint8_t val) 1324{ 1325 struct pccard_ivar *devi = PCCARD_IVAR(child); 1326 struct pccard_function *pf = devi->pf; 1327 1328 /* 1329 * Optimization. Most of the time, devices want to access 1330 * the same page of the attribute memory that the CCR is in. 1331 * We take advantage of this fact here. 1332 */ 1333 if (offset / PCCARD_MEM_PAGE_SIZE == 1334 pf->ccr_base / PCCARD_MEM_PAGE_SIZE) 1335 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 1336 offset % PCCARD_MEM_PAGE_SIZE, val); 1337 else { 1338 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset, 1339 &offset); 1340 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, offset, val); 1341 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base, 1342 &offset); 1343 } 1344 1345 return 0; 1346} 1347 1348static int 1349pccard_ccr_read_impl(device_t brdev, device_t child, uint32_t offset, 1350 uint8_t *val) 1351{ 1352 struct pccard_ivar *devi = PCCARD_IVAR(child); 1353 1354 *val = pccard_ccr_read(devi->pf, offset); 1355 device_printf(child, "ccr_read of %#x (%#x) is %#x\n", offset, 1356 devi->pf->pf_ccr_offset, *val); 1357 return 0; 1358} 1359 1360static int 1361pccard_ccr_write_impl(device_t brdev, device_t child, uint32_t offset, 1362 uint8_t val) 1363{ 1364 struct pccard_ivar *devi = PCCARD_IVAR(child); 1365 struct pccard_function *pf = devi->pf; 1366 1367 /* 1368 * Can't use pccard_ccr_write since client drivers may access 1369 * registers not contained in the 'mask' if they are non-standard. 1370 */ 1371 device_printf(child, "ccr_write of %#x to %#x (%#x)\n", val, offset, 1372 devi->pf->pf_ccr_offset); 1373 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, pf->pf_ccr_offset + offset, 1374 val); 1375 return 0; 1376} 1377 1378 1379static device_method_t pccard_methods[] = { 1380 /* Device interface */ 1381 DEVMETHOD(device_probe, pccard_probe), 1382 DEVMETHOD(device_attach, pccard_attach), 1383 DEVMETHOD(device_detach, pccard_detach), 1384 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1385 DEVMETHOD(device_suspend, pccard_suspend), 1386 DEVMETHOD(device_resume, pccard_resume), 1387 1388 /* Bus interface */ 1389 DEVMETHOD(bus_print_child, pccard_print_child), 1390 DEVMETHOD(bus_driver_added, pccard_driver_added), 1391 DEVMETHOD(bus_child_detached, pccard_child_detached), 1392 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource), 1393 DEVMETHOD(bus_release_resource, pccard_release_resource), 1394 DEVMETHOD(bus_activate_resource, pccard_activate_resource), 1395 DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource), 1396 DEVMETHOD(bus_setup_intr, pccard_setup_intr), 1397 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr), 1398 DEVMETHOD(bus_set_resource, pccard_set_resource), 1399 DEVMETHOD(bus_get_resource, pccard_get_resource), 1400 DEVMETHOD(bus_delete_resource, pccard_delete_resource), 1401 DEVMETHOD(bus_probe_nomatch, pccard_probe_nomatch), 1402 DEVMETHOD(bus_read_ivar, pccard_read_ivar), 1403 DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str), 1404 DEVMETHOD(bus_child_location_str, pccard_child_location_str), 1405 1406 /* Card Interface */ 1407 DEVMETHOD(card_set_res_flags, pccard_set_res_flags), 1408 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset), 1409 DEVMETHOD(card_attach_card, pccard_attach_card), 1410 DEVMETHOD(card_detach_card, pccard_detach_card), 1411 DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup), 1412 DEVMETHOD(card_cis_scan, pccard_scan_cis), 1413 DEVMETHOD(card_attr_read, pccard_attr_read_impl), 1414 DEVMETHOD(card_attr_write, pccard_attr_write_impl), 1415 DEVMETHOD(card_ccr_read, pccard_ccr_read_impl), 1416 DEVMETHOD(card_ccr_write, pccard_ccr_write_impl), 1417 1418 { 0, 0 } 1419}; 1420 1421static driver_t pccard_driver = { 1422 "pccard", 1423 pccard_methods, 1424 sizeof(struct pccard_softc) 1425}; 1426 1427devclass_t pccard_devclass; 1428 1429/* Maybe we need to have a slot device? */ 1430DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0); 1431DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0); 1432MODULE_VERSION(pccard, 1); 1433