pccard.c revision 67399
166200Simp/*	$NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $	*/
252506Simp/* $FreeBSD: head/sys/dev/pccard/pccard.c 67399 2000-10-20 20:27:22Z imp $ */
352506Simp
452506Simp/*
552506Simp * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
652506Simp *
752506Simp * Redistribution and use in source and binary forms, with or without
852506Simp * modification, are permitted provided that the following conditions
952506Simp * are met:
1052506Simp * 1. Redistributions of source code must retain the above copyright
1152506Simp *    notice, this list of conditions and the following disclaimer.
1252506Simp * 2. Redistributions in binary form must reproduce the above copyright
1352506Simp *    notice, this list of conditions and the following disclaimer in the
1452506Simp *    documentation and/or other materials provided with the distribution.
1552506Simp * 3. All advertising materials mentioning features or use of this software
1652506Simp *    must display the following acknowledgement:
1752506Simp *	This product includes software developed by Marc Horowitz.
1852506Simp * 4. The name of the author may not be used to endorse or promote products
1952506Simp *    derived from this software without specific prior written permission.
2052506Simp *
2152506Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2252506Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2352506Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2452506Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2552506Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2652506Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2752506Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2852506Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2952506Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3052506Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3152506Simp */
3252506Simp
3352506Simp#include <sys/param.h>
3452506Simp#include <sys/systm.h>
3552506Simp#include <sys/malloc.h>
3652506Simp#include <sys/module.h>
3752506Simp#include <sys/kernel.h>
3852506Simp#include <sys/queue.h>
3952506Simp#include <sys/types.h>
4052506Simp
4152506Simp#include <sys/bus.h>
4252506Simp#include <machine/bus.h>
4352506Simp#include <sys/rman.h>
4452506Simp#include <machine/resource.h>
4552506Simp
4652506Simp#include <dev/pccard/pccardreg.h>
4752506Simp#include <dev/pccard/pccardvar.h>
4852506Simp
4955500Simp#include "power_if.h"
5059193Simp#include "card_if.h"
5155500Simp
5255500Simp#define PCCARDDEBUG
5355500Simp
5452506Simp#ifdef PCCARDDEBUG
5555500Simpint	pccard_debug = 1;
5652506Simp#define	DPRINTF(arg) if (pccard_debug) printf arg
5755500Simp#define	DEVPRINTF(arg) if (pccard_debug) device_printf arg
5867333Simp#define PRVERBOSE(arg) printf arg
5967333Simp#define DEVPRVERBOSE(arg) device_printf arg
6052506Simp#else
6152506Simp#define	DPRINTF(arg)
6255500Simp#define	DEVPRINTF(arg)
6367333Simp#define PRVERBOSE(arg) if (bootverbose) printf arg
6467333Simp#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg
6552506Simp#endif
6652506Simp
6752506Simp#ifdef PCCARDVERBOSE
6852506Simpint	pccard_verbose = 1;
6952506Simp#else
7052506Simpint	pccard_verbose = 0;
7152506Simp#endif
7252506Simp
7354250Simpint	pccard_print(void *, const char *);
7452506Simp
7552506Simpint
7652506Simppccard_ccr_read(pf, ccr)
7752506Simp	struct pccard_function *pf;
7852506Simp	int ccr;
7952506Simp{
8052506Simp	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
8152506Simp	    pf->pf_ccr_offset + ccr));
8252506Simp}
8352506Simp
8452506Simpvoid
8552506Simppccard_ccr_write(pf, ccr, val)
8652506Simp	struct pccard_function *pf;
8752506Simp	int ccr;
8852506Simp	int val;
8952506Simp{
9052506Simp
9152506Simp	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
9252506Simp		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
9352506Simp		    pf->pf_ccr_offset + ccr, val);
9452506Simp	}
9552506Simp}
9652506Simp
9759193Simpstatic int
9859193Simppccard_attach_card(device_t dev)
9952506Simp{
10064850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
10152506Simp	struct pccard_function *pf;
10265917Simp	struct pccard_ivar *ivar;
10361788Simp	device_t child;
10452506Simp	int attached;
10552506Simp
10652506Simp	/*
10752506Simp	 * this is here so that when socket_enable calls gettype, trt happens
10852506Simp	 */
10952506Simp	STAILQ_INIT(&sc->card.pf_head);
11052506Simp
11155500Simp	DEVPRINTF((dev, "chip_socket_enable\n"));
11255500Simp	POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
11352506Simp
11455500Simp	DEVPRINTF((dev, "read_cis\n"));
11552506Simp	pccard_read_cis(sc);
11652506Simp
11755500Simp	DEVPRINTF((dev, "check_cis_quirks\n"));
11852506Simp	pccard_check_cis_quirks(dev);
11952506Simp
12052506Simp	/*
12152506Simp	 * bail now if the card has no functions, or if there was an error in
12252506Simp	 * the cis.
12352506Simp	 */
12452506Simp
12552506Simp	if (sc->card.error)
12652506Simp		return (1);
12752506Simp	if (STAILQ_EMPTY(&sc->card.pf_head))
12852506Simp		return (1);
12952506Simp
13061788Simp	if (1)
13152506Simp		pccard_print_cis(dev);
13252506Simp
13352506Simp	attached = 0;
13452506Simp
13555500Simp	DEVPRINTF((dev, "functions scanning\n"));
13652506Simp	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
13752506Simp		if (STAILQ_EMPTY(&pf->cfe_head))
13852506Simp			continue;
13952506Simp
14052506Simp		pf->sc = sc;
14152506Simp		pf->cfe = NULL;
14264927Simp		pf->dev = NULL;
14352506Simp	}
14465098Simp#if 0
14564850Simp	DEVPRINTF((dev, "chip_socket_disable\n"));
14664850Simp	POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
14765098Simp#endif
14852506Simp	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
14952506Simp		if (STAILQ_EMPTY(&pf->cfe_head))
15052506Simp			continue;
15161788Simp		/*
15261788Simp		 * In NetBSD, the drivers are responsible for activating
15361788Simp		 * each function of a card.  I think that in FreeBSD we
15461788Simp		 * want to activate them enough for the usual bus_*_resource
15561788Simp		 * routines will do the right thing.  This many mean a
15661788Simp		 * departure from the current NetBSD model.
15761788Simp		 *
15861788Simp		 * This could get really ugly for multifunction cards.  But
15961788Simp		 * it might also just fall out of the FreeBSD resource model.
16061788Simp		 *
16161788Simp		 */
16265917Simp		ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, M_WAITOK);
16367167Simp		bzero(ivar, sizeof *ivar);
16461788Simp		child = device_add_child(dev, NULL, -1);
16565917Simp		device_set_ivars(child, ivar);
16666847Simp		ivar->fcn = pf;
16767187Simp		pf->dev = child;
16867167Simp		/*
16967167Simp		 * XXX We might want to move the next two lines into
17067167Simp		 * XXX the pccard interface layer.  For the moment, this
17167167Simp		 * XXX is OK, but some drivers want to pick the config
17267167Simp		 * XXX entry to use as well as some address tweaks (mostly
17367167Simp		 * XXX due to bugs in decode logic that makes some
17467167Simp		 * XXX addresses illegal or broken).
17567167Simp		 */
17665917Simp		pccard_function_init(pf);
17767333Simp		if (pccard_function_enable(pf) == 0 &&
17867333Simp		    device_probe_and_attach(child) == 0) {
17952506Simp			attached++;
18052506Simp
18155500Simp			DEVPRINTF((sc->dev, "function %d CCR at %d "
18267167Simp			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
18367167Simp			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
18467167Simp			    pccard_ccr_read(pf, 0x00),
18552506Simp			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
18652506Simp			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
18752506Simp			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
18852506Simp			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
18967167Simp		} else {
19067167Simp			device_delete_child(dev, child);
19152506Simp		}
19252506Simp	}
19361788Simp	return 0;
19452506Simp}
19552506Simp
19659193Simpstatic int
19759193Simppccard_detach_card(device_t dev, int flags)
19852506Simp{
19964850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
20052506Simp	struct pccard_function *pf;
20152506Simp
20252506Simp	/*
20352506Simp	 * We are running on either the PCCARD socket's event thread
20452506Simp	 * or in user context detaching a device by user request.
20552506Simp	 */
20652506Simp	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
20752506Simp		if (STAILQ_FIRST(&pf->cfe_head) == NULL)
20852506Simp			continue;
20961788Simp
21061788Simp		pccard_function_disable(pf);
21164927Simp		if (pf->dev)
21264927Simp			device_delete_child(dev, pf->dev);
21352506Simp	}
21459193Simp	return 0;
21552506Simp}
21652506Simp
21766200Simpconst struct pccard_product *
21866200Simppccard_product_lookup(device_t dev, const struct pccard_product *tab,
21966200Simp    size_t ent_size, pccard_product_match_fn matchfn)
22066200Simp{
22166200Simp	const struct pccard_product *ent;
22266200Simp	int matches;
22366200Simp	u_int32_t fcn;
22466200Simp	u_int32_t vendor;
22566200Simp	u_int32_t prod;
22666200Simp	char *vendorstr;
22766200Simp	char *prodstr;
22866200Simp
22966200Simp#ifdef DIAGNOSTIC
23066200Simp	if (sizeof *ent > ent_size)
23166200Simp		panic("pccard_product_lookup: bogus ent_size %ld",
23266200Simp		    (long) ent_size);
23366200Simp#endif
23466200Simp	if (pccard_get_vendor(dev, &vendor))
23566200Simp		return (NULL);
23666200Simp	if (pccard_get_product(dev, &prod))
23766200Simp		return (NULL);
23866200Simp	if (pccard_get_function_number(dev, &fcn))
23966200Simp		return (NULL);
24066200Simp	if (pccard_get_vendor_str(dev, &vendorstr))
24166200Simp		return (NULL);
24266200Simp	if (pccard_get_product_str(dev, &prodstr))
24366200Simp		return (NULL);
24466200Simp        for (ent = tab; ent->pp_name != NULL;
24566200Simp	     ent = (const struct pccard_product *)
24666200Simp		 ((const char *) ent + ent_size)) {
24766200Simp		matches = 1;
24866200Simp		if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
24966200Simp		    vendor != ent->pp_vendor)
25066200Simp			matches = 0;
25166200Simp		if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
25266200Simp		    prod != ent->pp_product)
25366200Simp			matches = 0;
25466200Simp		if (matches && fcn != ent->pp_expfunc)
25566200Simp			matches = 0;
25666200Simp		if (matches && ent->pp_vendor_str &&
25766200Simp		    strcmp(ent->pp_vendor_str, vendorstr) != 0)
25866200Simp			matches = 0;
25966200Simp		if (matches && ent->pp_product_str &&
26066200Simp		    strcmp(ent->pp_product_str, prodstr) != 0)
26166200Simp			matches = 0;
26266200Simp		if (matchfn != NULL)
26366200Simp			matches = (*matchfn)(dev, ent, matches);
26466200Simp		if (matches)
26566200Simp			return (ent);
26666200Simp	}
26766200Simp	return (NULL);
26866200Simp}
26966200Simp
27059193Simpstatic int
27159193Simppccard_card_gettype(device_t dev, int *type)
27252506Simp{
27364850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
27452506Simp	struct pccard_function *pf;
27552506Simp
27652506Simp	/*
27752506Simp	 * set the iftype to memory if this card has no functions (not yet
27852506Simp	 * probed), or only one function, and that is not initialized yet or
27952506Simp	 * that is memory.
28052506Simp	 */
28152506Simp	pf = STAILQ_FIRST(&sc->card.pf_head);
28252506Simp	if (pf == NULL ||
28352506Simp	    (STAILQ_NEXT(pf, pf_list) == NULL &&
28452506Simp	    (pf->cfe == NULL || pf->cfe->iftype == PCCARD_IFTYPE_MEMORY)))
28559193Simp		*type = PCCARD_IFTYPE_MEMORY;
28652506Simp	else
28759193Simp		*type = PCCARD_IFTYPE_IO;
28859193Simp	return 0;
28952506Simp}
29052506Simp
29152506Simp/*
29252506Simp * Initialize a PCCARD function.  May be called as long as the function is
29352506Simp * disabled.
29452506Simp */
29552506Simpvoid
29665917Simppccard_function_init(struct pccard_function *pf)
29752506Simp{
29865917Simp	struct pccard_config_entry *cfe;
29967187Simp	int i;
30067242Simp	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
30167242Simp	struct resource_list *rl = &devi->resources;
30267242Simp	struct resource *r = 0;
30367242Simp	device_t bus;
30465917Simp
30552506Simp	if (pf->pf_flags & PFF_ENABLED)
30652506Simp		panic("pccard_function_init: function is enabled");
30752506Simp
30867242Simp	bus = device_get_parent(pf->dev);
30952506Simp	/* Remember which configuration entry we are using. */
31067167Simp	for (cfe = STAILQ_FIRST(&pf->cfe_head); cfe != NULL;
31167167Simp	    cfe = STAILQ_NEXT(cfe, cfe_list)) {
31267187Simp		for (i = 0; i < cfe->num_iospace; i++)
31367187Simp			cfe->iores[i] = NULL;
31467187Simp		cfe->irqres = NULL;
31567187Simp		for (i = 0; i < cfe->num_iospace; i++) {
31667187Simp			/* XXX kludge, need to not ignore start */
31767187Simp			/* XXX start is a hint here, so this would break */
31867187Simp			/* XXX modems */
31967242Simp			/* XXX ALSO: should just ask for the range 0 to */
32067242Simp			/* XXX 1 << decode bits - 1, so we have a layering */
32167242Simp			/* XXX violation now */
32267187Simp			cfe->iorid[i] = i;
32367242Simp			r = cfe->iores[i] = bus_alloc_resource(bus,
32467242Simp			    SYS_RES_IOPORT, &cfe->iorid[i], 0x100, 0x3ff,
32567187Simp			    cfe->iospace[i].length, 0);
32667187Simp			if (cfe->iores[i] == 0)
32767187Simp				goto not_this_one;
32867242Simp			resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i],
32967242Simp			    rman_get_start(r), rman_get_end(r),
33067242Simp			    cfe->iospace[i].length);
33167187Simp
33267187Simp		}
33367187Simp		if (cfe->num_memspace > 0) {
33467187Simp			goto not_this_one;
33567187Simp		}
33667187Simp		if (cfe->irqmask) {
33767187Simp			cfe->irqrid = 0;
33867399Simp			r = cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ,
33967187Simp			    &cfe->irqrid, 10, 12, 1, 0);
34067187Simp			if (cfe->irqres == 0)
34167187Simp				goto not_this_one;
34267242Simp			resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid,
34367242Simp			    rman_get_start(r), rman_get_end(r), 1);
34467187Simp		}
34567187Simp		/* XXX Don't know how to deal with maxtwins */
34667187Simp		/* If we get to here, we've allocated all we need */
34767167Simp		pf->cfe = cfe;
34867187Simp		break;
34967187Simp	    not_this_one:;
35067333Simp		/*
35167333Simp		 * Release resources that we partially allocated
35267333Simp		 * from this config entry.
35367333Simp		 */
35467187Simp		for (i = 0; i < cfe->num_iospace; i++) {
35567242Simp			resource_list_delete(rl, SYS_RES_IOPORT, i);
35667187Simp			if (cfe->iores[i])
35767242Simp				bus_release_resource(bus, SYS_RES_IOPORT,
35867187Simp				    cfe->iorid[i], cfe->iores[i]);
35967187Simp			cfe->iores[i] = NULL;
36067187Simp		}
36167187Simp		if (cfe->irqmask && cfe->irqres) {
36267242Simp			resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid);
36367242Simp			bus_release_resource(bus, SYS_RES_IRQ,
36467187Simp			    cfe->irqrid, cfe->irqres);
36567187Simp			cfe->irqres = NULL;
36667187Simp		}
36767167Simp	}
36852506Simp}
36952506Simp
37052506Simp/* Enable a PCCARD function */
37152506Simpint
37255720Simppccard_function_enable(struct pccard_function *pf)
37352506Simp{
37452506Simp	struct pccard_function *tmp;
37552506Simp	int reg;
37655720Simp	device_t dev = pf->sc->dev;
37752506Simp
37867333Simp	if (pf->cfe == NULL) {
37967333Simp		DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
38067333Simp		return ENOMEM;
38167333Simp	}
38252506Simp
38352506Simp	/*
38452506Simp	 * Increase the reference count on the socket, enabling power, if
38552506Simp	 * necessary.
38652506Simp	 */
38752506Simp	if (pf->sc->sc_enabled_count++ == 0)
38855720Simp		POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
38955720Simp	DEVPRINTF((dev, "++enabled_count = %d\n", pf->sc->sc_enabled_count));
39052506Simp
39152506Simp	if (pf->pf_flags & PFF_ENABLED) {
39252506Simp		/*
39352506Simp		 * Don't do anything if we're already enabled.
39452506Simp		 */
39552506Simp		return (0);
39652506Simp	}
39752506Simp
39852506Simp	/*
39952506Simp	 * it's possible for different functions' CCRs to be in the same
40052506Simp	 * underlying page.  Check for that.
40152506Simp	 */
40252506Simp	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
40352506Simp		if ((tmp->pf_flags & PFF_ENABLED) &&
40452506Simp		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
40552506Simp		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
40652506Simp		     (tmp->ccr_base - tmp->pf_ccr_offset +
40752506Simp		      tmp->pf_ccr_realsize))) {
40852506Simp			pf->pf_ccrt = tmp->pf_ccrt;
40952506Simp			pf->pf_ccrh = tmp->pf_ccrh;
41052506Simp			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
41152506Simp
41252506Simp			/*
41352506Simp			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
41452506Simp			 * tmp->ccr_base) + pf->ccr_base;
41552506Simp			 */
41652506Simp			pf->pf_ccr_offset =
41752506Simp			    (tmp->pf_ccr_offset + pf->ccr_base) -
41852506Simp			    tmp->ccr_base;
41952506Simp			pf->pf_ccr_window = tmp->pf_ccr_window;
42052506Simp			break;
42152506Simp		}
42252506Simp	}
42352506Simp
42452506Simp	if (tmp == NULL) {
42555720Simp		pf->ccr_rid = 0;
42655720Simp		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
42765098Simp		    &pf->ccr_rid, 0xa0000, 0xdffff, 1 << 10, RF_ACTIVE);
42865098Simp		if (!pf->ccr_res) {
42965098Simp			DEVPRINTF((dev, "ccr_res == 0\n"));
43052506Simp			goto bad;
43165098Simp		}
43261788Simp		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
43361788Simp		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
43465098Simp		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
43565098Simp		    pf->ccr_rid, (pf->ccr_rid >> 10) << 10);
43655720Simp		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
43755720Simp		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
43855720Simp		pf->pf_ccr_offset = rman_get_start(pf->ccr_res);
43955720Simp		pf->pf_ccr_realsize = 1;
44052506Simp	}
44152506Simp
44252506Simp	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
44352506Simp	reg |= PCCARD_CCR_OPTION_LEVIREQ;
44452506Simp	if (pccard_mfc(pf->sc)) {
44552506Simp		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
44652506Simp			PCCARD_CCR_OPTION_ADDR_DECODE);
44767167Simp		/*
44867167Simp		 * XXX Need to enable PCCARD_CCR_OPTION_IRQ_ENABLE if
44967167Simp		 * XXX we have an interrupt handler, but we don't know that
45067167Simp		 * XXX at this point.
45167167Simp		 */
45267269Simp		reg |= PCCARD_CCR_OPTION_IREQ_ENABLE;
45352506Simp	}
45452506Simp	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
45552506Simp
45652506Simp	reg = 0;
45752506Simp	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
45852506Simp		reg |= PCCARD_CCR_STATUS_IOIS8;
45952506Simp	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
46052506Simp		reg |= PCCARD_CCR_STATUS_AUDIO;
46152506Simp	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
46252506Simp
46352506Simp	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
46452506Simp
46552506Simp	if (pccard_mfc(pf->sc)) {
46652506Simp		long tmp, iosize;
46752506Simp
46852506Simp		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
46952506Simp		/* round up to nearest (2^n)-1 */
47052506Simp		for (iosize = 1; iosize < tmp; iosize <<= 1)
47152506Simp			;
47252506Simp		iosize--;
47352506Simp
47452506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
47552506Simp				 pf->pf_mfc_iobase & 0xff);
47652506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
47752506Simp				 (pf->pf_mfc_iobase >> 8) & 0xff);
47852506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
47952506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
48052506Simp
48152506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
48252506Simp	}
48352506Simp
48452506Simp#ifdef PCCARDDEBUG
48552506Simp	if (pccard_debug) {
48652506Simp		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
48755500Simp			device_printf(tmp->sc->dev,
48855500Simp			    "function %d CCR at %d offset %x: "
48955500Simp			    "%x %x %x %x, %x %x %x %x, %x\n",
49055500Simp			    tmp->number, tmp->pf_ccr_window,
49155500Simp			    tmp->pf_ccr_offset,
49255500Simp			    pccard_ccr_read(tmp, 0x00),
49355500Simp			    pccard_ccr_read(tmp, 0x02),
49455500Simp			    pccard_ccr_read(tmp, 0x04),
49555500Simp			    pccard_ccr_read(tmp, 0x06),
49655500Simp			    pccard_ccr_read(tmp, 0x0A),
49755500Simp			    pccard_ccr_read(tmp, 0x0C),
49855500Simp			    pccard_ccr_read(tmp, 0x0E),
49955500Simp			    pccard_ccr_read(tmp, 0x10),
50055500Simp			    pccard_ccr_read(tmp, 0x12));
50152506Simp		}
50252506Simp	}
50352506Simp#endif
50452506Simp	pf->pf_flags |= PFF_ENABLED;
50552506Simp	return (0);
50652506Simp
50752506Simp bad:
50852506Simp	/*
50952506Simp	 * Decrement the reference count, and power down the socket, if
51052506Simp	 * necessary.
51152506Simp	 */
51252506Simp	if (--pf->sc->sc_enabled_count == 0)
51355720Simp		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
51465098Simp	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
51552506Simp
51652506Simp	return (1);
51752506Simp}
51852506Simp
51952506Simp/* Disable PCCARD function. */
52052506Simpvoid
52155720Simppccard_function_disable(struct pccard_function *pf)
52252506Simp{
52352506Simp	struct pccard_function *tmp;
52455720Simp	device_t dev = pf->sc->dev;
52552506Simp
52652506Simp	if (pf->cfe == NULL)
52761788Simp		panic("pccard_function_disable: function not initialized");
52852506Simp
52952506Simp	if ((pf->pf_flags & PFF_ENABLED) == 0) {
53052506Simp		/*
53152506Simp		 * Don't do anything if we're already disabled.
53252506Simp		 */
53352506Simp		return;
53452506Simp	}
53552506Simp
53652506Simp	/*
53752506Simp	 * it's possible for different functions' CCRs to be in the same
53852506Simp	 * underlying page.  Check for that.  Note we mark us as disabled
53952506Simp	 * first to avoid matching ourself.
54052506Simp	 */
54152506Simp
54252506Simp	pf->pf_flags &= ~PFF_ENABLED;
54352506Simp	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
54452506Simp		if ((tmp->pf_flags & PFF_ENABLED) &&
54552506Simp		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
54652506Simp		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
54752506Simp		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
54852506Simp			break;
54952506Simp	}
55052506Simp
55152506Simp	/* Not used by anyone else; unmap the CCR. */
55252506Simp	if (tmp == NULL) {
55355720Simp		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
55455720Simp		    pf->ccr_res);
55555720Simp		pf->ccr_res = NULL;
55652506Simp	}
55752506Simp
55852506Simp	/*
55952506Simp	 * Decrement the reference count, and power down the socket, if
56052506Simp	 * necessary.
56152506Simp	 */
56252506Simp	if (--pf->sc->sc_enabled_count == 0)
56355720Simp		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
56455720Simp	DEVPRINTF((dev, "--enabled_count = %d\n", pf->sc->sc_enabled_count));
56552506Simp}
56652506Simp
56755720Simp#if 0
56855720Simp/* XXX These functions are needed, but not like this XXX */
56952506Simpint
57055720Simppccard_io_map(struct pccard_function *pf, int width, bus_addr_t offset,
57155720Simp    bus_size_t size, struct pccard_io_handle *pcihp, int *windowp)
57252506Simp{
57352506Simp	int reg;
57452506Simp
57567167Simp	if (pccard_chip_io_map(pf->sc->pct, pf->sc->pch, width, offset, size,
57667167Simp	    pcihp, windowp))
57752506Simp		return (1);
57852506Simp
57952506Simp	/*
58052506Simp	 * XXX in the multifunction multi-iospace-per-function case, this
58152506Simp	 * needs to cooperate with io_alloc to make sure that the spaces
58252506Simp	 * don't overlap, and that the ccr's are set correctly
58352506Simp	 */
58452506Simp
58552506Simp	if (pccard_mfc(pf->sc)) {
58652506Simp		long tmp, iosize;
58752506Simp
58852506Simp		if (pf->pf_mfc_iomax == 0) {
58952506Simp			pf->pf_mfc_iobase = pcihp->addr + offset;
59052506Simp			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
59152506Simp		} else {
59252506Simp			/* this makes the assumption that nothing overlaps */
59352506Simp			if (pf->pf_mfc_iobase > pcihp->addr + offset)
59452506Simp				pf->pf_mfc_iobase = pcihp->addr + offset;
59552506Simp			if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
59652506Simp				pf->pf_mfc_iomax = pcihp->addr + offset + size;
59752506Simp		}
59852506Simp
59952506Simp		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
60052506Simp		/* round up to nearest (2^n)-1 */
60152506Simp		for (iosize = 1; iosize >= tmp; iosize <<= 1)
60252506Simp			;
60352506Simp		iosize--;
60452506Simp
60555720Simp		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
60655720Simp		    pf->pf_mfc_iobase & 0xff);
60752506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
60855720Simp		    (pf->pf_mfc_iobase >> 8) & 0xff);
60952506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
61052506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
61152506Simp
61252506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
61352506Simp
61452506Simp		reg = pccard_ccr_read(pf, PCCARD_CCR_OPTION);
61552506Simp		reg |= PCCARD_CCR_OPTION_ADDR_DECODE;
61652506Simp		pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
61752506Simp	}
61852506Simp	return (0);
61952506Simp}
62052506Simp
62152506Simpvoid
62255720Simppccard_io_unmap(struct pccard_function *pf, int window)
62352506Simp{
62452506Simp
62552506Simp	pccard_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
62652506Simp
62752506Simp	/* XXX Anything for multi-function cards? */
62852506Simp}
62952506Simp#endif
63052506Simp
63166058Simp/*
63266058Simp * simulate the old "probe" routine.  In the new world order, the driver
63366058Simp * needs to grab devices while in the old they were assigned to the device by
63466058Simp * the pccardd process.  These symbols are exported to the upper layers.
63566058Simp */
63666058Simpint
63766058Simppccard_compat_probe(device_t dev)
63866058Simp{
63966058Simp	return (CARD_COMPAT_MATCH(dev));
64066058Simp}
64166058Simp
64266058Simpint
64366058Simppccard_compat_attach(device_t dev)
64466058Simp{
64566058Simp	int err;
64666058Simp
64766058Simp	err = CARD_COMPAT_PROBE(dev);
64866058Simp	if (err == 0)
64966058Simp		err = CARD_COMPAT_ATTACH(dev);
65066058Simp	return (err);
65166058Simp}
65266058Simp
65353873Simp#define PCCARD_NPORT	2
65453873Simp#define PCCARD_NMEM	5
65553873Simp#define PCCARD_NIRQ	1
65653873Simp#define PCCARD_NDRQ	0
65753873Simp
65852506Simpstatic int
65952506Simppccard_add_children(device_t dev, int busno)
66052506Simp{
66159193Simp	/* Call parent to scan for any current children */
66252506Simp	return 0;
66352506Simp}
66452506Simp
66552506Simpstatic int
66652506Simppccard_probe(device_t dev)
66752506Simp{
66867333Simp	device_set_desc(dev, "16-bit PCCard bus");
66952506Simp	return pccard_add_children(dev, device_get_unit(dev));
67052506Simp}
67152506Simp
67259193Simpstatic int
67359193Simppccard_attach(device_t dev)
67459193Simp{
67564850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
67661788Simp
67759193Simp	sc->dev = dev;
67861788Simp	sc->sc_enabled_count = 0;
67959193Simp	return bus_generic_attach(dev);
68059193Simp}
68159193Simp
68253873Simpstatic void
68353873Simppccard_print_resources(struct resource_list *rl, const char *name, int type,
68453873Simp    int count, const char *format)
68553873Simp{
68653873Simp	struct resource_list_entry *rle;
68753873Simp	int printed;
68853873Simp	int i;
68953873Simp
69053873Simp	printed = 0;
69153873Simp	for (i = 0; i < count; i++) {
69253873Simp		rle = resource_list_find(rl, type, i);
69353873Simp		if (rle) {
69453873Simp			if (printed == 0)
69553873Simp				printf(" %s ", name);
69653873Simp			else if (printed > 0)
69753873Simp				printf(",");
69853873Simp			printed++;
69953873Simp			printf(format, rle->start);
70053873Simp			if (rle->count > 1) {
70153873Simp				printf("-");
70253873Simp				printf(format, rle->start + rle->count - 1);
70353873Simp			}
70453873Simp		} else if (i > 3) {
70553873Simp			/* check the first few regardless */
70653873Simp			break;
70753873Simp		}
70853873Simp	}
70953873Simp}
71053873Simp
71153873Simpstatic int
71253873Simppccard_print_child(device_t dev, device_t child)
71353873Simp{
71466847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
71553873Simp	struct resource_list *rl = &devi->resources;
71653873Simp	int retval = 0;
71753873Simp
71853873Simp	retval += bus_print_child_header(dev, child);
71953873Simp	retval += printf(" at");
72053873Simp
72153873Simp	if (devi) {
72253873Simp		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
72353873Simp		    PCCARD_NPORT, "%#lx");
72453873Simp		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
72553873Simp		    PCCARD_NMEM, "%#lx");
72653873Simp		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
72753873Simp		    "%ld");
72853873Simp		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
72953873Simp		    "%ld");
73067269Simp		retval += printf(" function %d config %d", devi->fcn->number,
73167269Simp		    devi->fcn->cfe->number);
73253873Simp	}
73353873Simp
73453873Simp	retval += bus_print_child_footer(dev, child);
73553873Simp
73653873Simp	return (retval);
73753873Simp}
73853873Simp
73953873Simpstatic int
74053873Simppccard_set_resource(device_t dev, device_t child, int type, int rid,
74153873Simp		 u_long start, u_long count)
74253873Simp{
74366847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
74453873Simp	struct resource_list *rl = &devi->resources;
74553873Simp
74653873Simp	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
74753873Simp	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
74853873Simp		return EINVAL;
74953873Simp	if (rid < 0)
75053873Simp		return EINVAL;
75153873Simp	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
75253873Simp		return EINVAL;
75353873Simp	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
75453873Simp		return EINVAL;
75553873Simp	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
75653873Simp		return EINVAL;
75753873Simp	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
75853873Simp		return EINVAL;
75953873Simp
76053873Simp	resource_list_add(rl, type, rid, start, start + count - 1, count);
76153873Simp
76253873Simp	return 0;
76353873Simp}
76453873Simp
76553873Simpstatic int
76653873Simppccard_get_resource(device_t dev, device_t child, int type, int rid,
76753873Simp    u_long *startp, u_long *countp)
76853873Simp{
76966847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
77053873Simp	struct resource_list *rl = &devi->resources;
77153873Simp	struct resource_list_entry *rle;
77253873Simp
77353873Simp	rle = resource_list_find(rl, type, rid);
77453873Simp	if (!rle)
77553873Simp		return ENOENT;
77653873Simp
77753873Simp	if (startp)
77853873Simp		*startp = rle->start;
77953873Simp	if (countp)
78053873Simp		*countp = rle->count;
78153873Simp
78253873Simp	return 0;
78353873Simp}
78453873Simp
78553873Simpstatic void
78653873Simppccard_delete_resource(device_t dev, device_t child, int type, int rid)
78753873Simp{
78866847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
78953873Simp	struct resource_list *rl = &devi->resources;
79053873Simp	resource_list_delete(rl, type, rid);
79153873Simp}
79253873Simp
79359193Simpstatic int
79459193Simppccard_set_res_flags(device_t dev, device_t child, int type, int rid,
79559193Simp    u_int32_t flags)
79659193Simp{
79759193Simp	return CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
79859193Simp	    rid, flags);
79959193Simp}
80059193Simp
80159193Simpstatic int
80259193Simppccard_set_memory_offset(device_t dev, device_t child, int rid,
80359193Simp     u_int32_t offset)
80459193Simp{
80559193Simp	return CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
80659193Simp	    offset);
80759193Simp}
80859193Simp
80966058Simpstatic int
81066058Simppccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
81166058Simp{
81266847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
81366779Simp	struct pccard_function *func = devi->fcn;
81466779Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
81566779Simp
81666058Simp	/* PCCARD_IVAR_ETHADDR unhandled from oldcard */
81766779Simp	switch (which) {
81866779Simp	default:
81966779Simp	case PCCARD_IVAR_ETHADDR:
82066779Simp		return (ENOENT);
82166779Simp		break;
82266779Simp	case PCCARD_IVAR_VENDOR:
82366779Simp		*(u_int32_t *) result = sc->card.manufacturer;
82466779Simp		break;
82566779Simp	case PCCARD_IVAR_PRODUCT:
82666779Simp		*(u_int32_t *) result = sc->card.product;
82766779Simp		break;
82866779Simp	case PCCARD_IVAR_FUNCTION_NUMBER:
82966847Simp		if (!func) {
83066847Simp			device_printf(bus, "No function number, bug!\n");
83166847Simp			return (ENOENT);
83266847Simp		}
83366847Simp		*(u_int32_t *) result = func->number;
83466779Simp		break;
83566779Simp	case PCCARD_IVAR_VENDOR_STR:
83666779Simp		*(char **) result = sc->card.cis1_info[0];
83766779Simp		break;
83866779Simp	case PCCARD_IVAR_PRODUCT_STR:
83966779Simp		*(char **) result = sc->card.cis1_info[1];
84066779Simp		break;
84166779Simp	case PCCARD_IVAR_CIS3_STR:
84266779Simp		*(char **) result = sc->card.cis1_info[2];
84366779Simp		break;
84467167Simp	case PCCARD_IVAR_CIS4_STR:
84567167Simp		*(char **) result = sc->card.cis1_info[2];
84667167Simp		break;
84766779Simp	}
84866779Simp	return (0);
84966058Simp}
85066058Simp
85166779Simpstatic void
85266779Simppccard_driver_added(device_t dev, driver_t *driver)
85366779Simp{
85467167Simp	/*
85567167Simp	 * XXX eventually we need to attach stuff when we know we
85667167Simp	 * XXX have kids.  For now we do nothing because we normally
85767167Simp	 * XXX add children ourselves.  We don't want to necessarily
85867167Simp	 * XXX force a reprobe.
85967167Simp	 */
86066779Simp}
86166058Simp
86267242Simpstatic struct resource *
86367242Simppccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
86467242Simp    u_long start, u_long end, u_long count, u_int flags)
86567242Simp{
86667242Simp	struct pccard_ivar *ivar;
86767242Simp	struct pccard_function *pf;
86867269Simp	struct resource *r = 0;
86967242Simp
87067242Simp	if (device_get_parent(child) == dev) {
87167242Simp		ivar = PCCARD_IVAR(child);
87267242Simp		pf = ivar->fcn;
87367242Simp		switch (type) {
87467242Simp		case SYS_RES_IRQ:
87567242Simp			if (*rid > 0)
87667242Simp				return NULL;
87767269Simp			r = pf->cfe->irqres;
87867269Simp			break;
87967242Simp		case SYS_RES_IOPORT:
88067242Simp			if (*rid > 3)	/* XXX */
88167242Simp				return NULL;
88267269Simp			r = pf->cfe->iores[*rid];
88367269Simp			break;
88467269Simp		default:
88567269Simp			break;
88667242Simp		}
88767242Simp	}
88867269Simp	if (r != NULL) {
88967269Simp		if (flags & RF_ACTIVE)
89067269Simp			bus_generic_activate_resource(dev, child, type,
89167269Simp			    *rid, r);
89267269Simp		return (r);
89367269Simp	}
89467242Simp	return (bus_generic_alloc_resource(dev, child, type, rid, start,
89567242Simp	    end, count, flags));
89667242Simp}
89767242Simp
89867242Simpstatic int
89967242Simppccard_release_resource(device_t dev, device_t child, int type, int rid,
90067242Simp    struct resource *r)
90167242Simp{
90267242Simp	return bus_generic_release_resource(dev, child, type, rid, r);
90367242Simp}
90467242Simp
90567242Simpstatic int
90667242Simppccard_activate_resource(device_t dev, device_t child, int type, int rid,
90767242Simp    struct resource *r)
90867242Simp{
90967269Simp	/* XXX need to write to the COR to activate this for mf cards */
91067242Simp	return (bus_generic_activate_resource(dev, child, type, rid, r));
91167242Simp}
91267242Simp
91367242Simpstatic int
91467242Simppccard_deactivate_resource(device_t dev, device_t child, int type, int rid,
91567242Simp    struct resource *r)
91667242Simp{
91767269Simp	/* XXX need to write to the COR to deactivate this for mf cards */
91867242Simp	return (bus_generic_deactivate_resource(dev, child, type, rid, r));
91967242Simp}
92067242Simp
92167333Simpstatic void
92267333Simppccard_child_detached(device_t parent, device_t dev)
92367333Simp{
92467333Simp	struct pccard_ivar *ivar = PCCARD_IVAR(dev);
92567333Simp
92667333Simp	if (parent == device_get_parent(dev))
92767333Simp		free(ivar, M_DEVBUF);
92867333Simp}
92967333Simp
93052506Simpstatic device_method_t pccard_methods[] = {
93152506Simp	/* Device interface */
93252506Simp	DEVMETHOD(device_probe,		pccard_probe),
93359193Simp	DEVMETHOD(device_attach,	pccard_attach),
93461788Simp	DEVMETHOD(device_detach,	bus_generic_detach),
93552506Simp	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
93652506Simp	DEVMETHOD(device_suspend,	bus_generic_suspend),
93752506Simp	DEVMETHOD(device_resume,	bus_generic_resume),
93852506Simp
93952506Simp	/* Bus interface */
94052506Simp	DEVMETHOD(bus_print_child,	pccard_print_child),
94166779Simp	DEVMETHOD(bus_driver_added,	pccard_driver_added),
94267333Simp	DEVMETHOD(bus_child_detached,	pccard_child_detached),
94367242Simp	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
94467242Simp	DEVMETHOD(bus_release_resource,	pccard_release_resource),
94567242Simp	DEVMETHOD(bus_activate_resource, pccard_activate_resource),
94667242Simp	DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
94752506Simp	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
94852506Simp	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
94952506Simp	DEVMETHOD(bus_set_resource,	pccard_set_resource),
95052506Simp	DEVMETHOD(bus_get_resource,	pccard_get_resource),
95152506Simp	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
95266058Simp	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
95352506Simp
95459193Simp	/* Card Interface */
95559193Simp	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
95659193Simp	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
95759193Simp	DEVMETHOD(card_get_type,	pccard_card_gettype),
95859193Simp	DEVMETHOD(card_attach_card,	pccard_attach_card),
95959193Simp	DEVMETHOD(card_detach_card,	pccard_detach_card),
96059193Simp
96152506Simp	{ 0, 0 }
96252506Simp};
96352506Simp
96452506Simpstatic driver_t pccard_driver = {
96552506Simp	"pccard",
96652506Simp	pccard_methods,
96764850Simp	sizeof(struct pccard_softc)
96852506Simp};
96952506Simp
97052506Simpdevclass_t	pccard_devclass;
97152506Simp
97253873SimpDRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
97352506SimpDRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0);
97452506SimpDRIVER_MODULE(pccard, pccbb, pccard_driver, pccard_devclass, 0, 0);
97553873SimpDRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0);
97664927SimpMODULE_VERSION(pccard, 1);
97764927SimpMODULE_DEPEND(pccard, pcic, 1, 1, 1);
978