pccard.c revision 67242
1139804Simp/*	$NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $	*/
272376Sjake/* $FreeBSD: head/sys/dev/pccard/pccard.c 67242 2000-10-17 06:29:21Z imp $ */
372376Sjake
450027Speter/*
550027Speter * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
650027Speter *
750027Speter * Redistribution and use in source and binary forms, with or without
850027Speter * modification, are permitted provided that the following conditions
950027Speter * are met:
1050027Speter * 1. Redistributions of source code must retain the above copyright
1150027Speter *    notice, this list of conditions and the following disclaimer.
1250027Speter * 2. Redistributions in binary form must reproduce the above copyright
1350027Speter *    notice, this list of conditions and the following disclaimer in the
1450027Speter *    documentation and/or other materials provided with the distribution.
1550027Speter * 3. All advertising materials mentioning features or use of this software
1650027Speter *    must display the following acknowledgement:
1750027Speter *	This product includes software developed by Marc Horowitz.
1850027Speter * 4. The name of the author may not be used to endorse or promote products
1950027Speter *    derived from this software without specific prior written permission.
2050027Speter *
2150027Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2250027Speter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2350027Speter * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2450027Speter * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2550027Speter * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2650027Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2799072Sjulian * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28116182Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29116182Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30116182Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31134591Sjulian */
32131481Sjhb
3350027Speter#include <sys/param.h>
3450027Speter#include <sys/systm.h>
35131927Smarcel#include <sys/malloc.h>
3650027Speter#include <sys/module.h>
3765557Sjasone#include <sys/kernel.h>
3874914Sjhb#include <sys/queue.h>
3967365Sjhb#include <sys/types.h>
4050027Speter
4150027Speter#include <sys/bus.h>
42104964Sjeff#include <machine/bus.h>
43112993Speter#include <sys/rman.h>
44177435Sjeff#include <machine/resource.h>
4550027Speter
46170293Sjeff#include <dev/pccard/pccardreg.h>
47170293Sjeff#include <dev/pccard/pccardvar.h>
48153510Snjl
49153510Snjl#include "power_if.h"
50153510Snjl#include "card_if.h"
51153510Snjl
52153510Snjl#define PCCARDDEBUG
53153510Snjl
54153510Snjl#ifdef PCCARDDEBUG
55134649Sscottlint	pccard_debug = 1;
56134649Sscottl#define	DPRINTF(arg) if (pccard_debug) printf arg
57134649Sscottl#define	DEVPRINTF(arg) if (pccard_debug) device_printf arg
58134649Sscottl#else
59134649Sscottl#define	DPRINTF(arg)
60134591Sjulian#define	DEVPRINTF(arg)
6197261Sjake#endif
6297261Sjake
63143884Srwatson#ifdef PCCARDVERBOSE
64143884Srwatsonint	pccard_verbose = 1;
65143884Srwatson#else
66143884Srwatsonint	pccard_verbose = 0;
67143884Srwatson#endif
68143884Srwatson
69143884Srwatsonint	pccard_print(void *, const char *);
70143884Srwatson
71143884Srwatsonint
72143884Srwatsonpccard_ccr_read(pf, ccr)
73143884Srwatson	struct pccard_function *pf;
74143884Srwatson	int ccr;
75143884Srwatson{
76178272Sjeff	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
77178272Sjeff	    pf->pf_ccr_offset + ccr));
78178272Sjeff}
79178272Sjeff
80178272Sjeffvoid
81170293Sjeffpccard_ccr_write(pf, ccr, val)
82178272Sjeff	struct pccard_function *pf;
83178272Sjeff	int ccr;
84194936Sjeff	int val;
85194936Sjeff{
86194936Sjeff
87194936Sjeff	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
88194936Sjeff		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
89194936Sjeff		    pf->pf_ccr_offset + ccr, val);
90194936Sjeff	}
91194936Sjeff}
92194936Sjeff
93194936Sjeffstatic int
94194936Sjeffpccard_attach_card(device_t dev)
95194936Sjeff{
96194936Sjeff	struct pccard_softc *sc = PCCARD_SOFTC(dev);
97194936Sjeff	struct pccard_function *pf;
98194936Sjeff	struct pccard_ivar *ivar;
99194936Sjeff	device_t child;
100194936Sjeff	int attached;
101194936Sjeff
102194936Sjeff	DEVPRINTF((dev, "pccard_card_attach\n"));
103194936Sjeff	/*
104194936Sjeff	 * this is here so that when socket_enable calls gettype, trt happens
105194936Sjeff	 */
106194936Sjeff	STAILQ_INIT(&sc->card.pf_head);
107194936Sjeff
108194936Sjeff	DEVPRINTF((dev, "chip_socket_enable\n"));
109194936Sjeff	POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
110194936Sjeff
111194936Sjeff	DEVPRINTF((dev, "read_cis\n"));
112194936Sjeff	pccard_read_cis(sc);
113170293Sjeff
114170293Sjeff	DEVPRINTF((dev, "check_cis_quirks\n"));
115170293Sjeff	pccard_check_cis_quirks(dev);
116178272Sjeff
117194936Sjeff	/*
118170293Sjeff	 * bail now if the card has no functions, or if there was an error in
119170293Sjeff	 * the cis.
120194936Sjeff	 */
121170293Sjeff
122170293Sjeff	if (sc->card.error)
123170293Sjeff		return (1);
124170293Sjeff	if (STAILQ_EMPTY(&sc->card.pf_head))
125170293Sjeff		return (1);
126170293Sjeff
127170293Sjeff	if (1)
128178272Sjeff		pccard_print_cis(dev);
129178272Sjeff
130178272Sjeff	attached = 0;
131178272Sjeff
132178272Sjeff	DEVPRINTF((dev, "functions scanning\n"));
133178272Sjeff	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
134178272Sjeff		if (STAILQ_EMPTY(&pf->cfe_head))
135194936Sjeff			continue;
136209059Sjhb
137194936Sjeff		pf->sc = sc;
138194936Sjeff		pf->cfe = NULL;
139178272Sjeff		pf->dev = NULL;
140170293Sjeff	}
141170293Sjeff#if 0
142170293Sjeff	DEVPRINTF((dev, "chip_socket_disable\n"));
143170293Sjeff	POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
144170293Sjeff#endif
145170293Sjeff
146170293Sjeff	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
14799072Sjulian		if (STAILQ_EMPTY(&pf->cfe_head))
14899072Sjulian			continue;
14999072Sjulian		/*
15050027Speter		 * In NetBSD, the drivers are responsible for activating
151163709Sjb		 * each function of a card.  I think that in FreeBSD we
152163709Sjb		 * want to activate them enough for the usual bus_*_resource
15383366Sjulian		 * routines will do the right thing.  This many mean a
15483366Sjulian		 * departure from the current NetBSD model.
15550027Speter		 *
15699072Sjulian		 * This could get really ugly for multifunction cards.  But
15799072Sjulian		 * it might also just fall out of the FreeBSD resource model.
158100209Sgallatin		 *
159166188Sjeff		 */
160108338Sjulian		device_printf(dev, "Starting to attach....\n");
161108338Sjulian		ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, M_WAITOK);
162115215Sjulian		bzero(ivar, sizeof *ivar);
163115215Sjulian		child = device_add_child(dev, NULL, -1);
164108338Sjulian		device_set_ivars(child, ivar);
165100209Sgallatin		ivar->fcn = pf;
166115215Sjulian		pf->dev = child;
167115215Sjulian		/*
168115215Sjulian		 * XXX We might want to move the next two lines into
169100209Sgallatin		 * XXX the pccard interface layer.  For the moment, this
170115215Sjulian		 * XXX is OK, but some drivers want to pick the config
171108338Sjulian		 * XXX entry to use as well as some address tweaks (mostly
172103216Sjulian		 * XXX due to bugs in decode logic that makes some
17399072Sjulian		 * XXX addresses illegal or broken).
17472376Sjake		 */
17550027Speter		pccard_function_init(pf);
17699072Sjulian		pccard_function_enable(pf);
177131481Sjhb		device_printf(dev, "pf %p pf->sc %p\n", pf, pf->sc);
178131481Sjhb		if (device_probe_and_attach(child) == 0) {
179131481Sjhb			attached++;
18088088Sjhb
18188088Sjhb			DEVPRINTF((sc->dev, "function %d CCR at %d "
18288088Sjhb			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
18388088Sjhb			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
18488088Sjhb			    pccard_ccr_read(pf, 0x00),
18588088Sjhb			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
18688088Sjhb			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
187153510Snjl			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
188173600Sjulian			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
18988088Sjhb		} else {
19088088Sjhb			device_delete_child(dev, child);
19188088Sjhb		}
19288088Sjhb	}
19388088Sjhb	return 0;
19488088Sjhb}
195178272Sjeff
19688088Sjhbstatic int
19788088Sjhbpccard_detach_card(device_t dev, int flags)
198125315Sjeff{
199125315Sjeff	struct pccard_softc *sc = PCCARD_SOFTC(dev);
200172481Sjeff	struct pccard_function *pf;
20188088Sjhb
202144777Sups	/*
203230175Savg	 * We are running on either the PCCARD socket's event thread
204144777Sups	 * or in user context detaching a device by user request.
205170293Sjeff	 */
206144777Sups	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
207178272Sjeff		if (STAILQ_FIRST(&pf->cfe_head) == NULL)
208178272Sjeff			continue;
209178272Sjeff
210178272Sjeff		pccard_function_disable(pf);
211178272Sjeff		if (pf->dev)
212178272Sjeff			device_delete_child(dev, pf->dev);
213170293Sjeff	}
214131481Sjhb	return 0;
215153797Skan}
21688088Sjhb
217153797Skanconst struct pccard_product *
218153510Snjlpccard_product_lookup(device_t dev, const struct pccard_product *tab,
219173600Sjulian    size_t ent_size, pccard_product_match_fn matchfn)
22088088Sjhb{
22188088Sjhb	const struct pccard_product *ent;
22299072Sjulian	int matches;
22399072Sjulian	u_int32_t fcn;
22499072Sjulian	u_int32_t vendor;
22572376Sjake	u_int32_t prod;
22699072Sjulian	char *vendorstr;
22799072Sjulian	char *prodstr;
22899072Sjulian
22999072Sjulian#ifdef DIAGNOSTIC
23099072Sjulian	if (sizeof *ent > ent_size)
23199072Sjulian		panic("pccard_product_lookup: bogus ent_size %ld",
23299072Sjulian		    (long) ent_size);
23399072Sjulian#endif
23499072Sjulian	if (pccard_get_vendor(dev, &vendor))
23599072Sjulian		return (NULL);
23699072Sjulian	if (pccard_get_product(dev, &prod))
23799072Sjulian		return (NULL);
23899072Sjulian	if (pccard_get_function_number(dev, &fcn))
23972376Sjake		return (NULL);
24072376Sjake	if (pccard_get_vendor_str(dev, &vendorstr))
24172376Sjake		return (NULL);
24272376Sjake	if (pccard_get_product_str(dev, &prodstr))
24372376Sjake		return (NULL);
24472376Sjake        for (ent = tab; ent->pp_name != NULL;
24572376Sjake	     ent = (const struct pccard_product *)
24665557Sjasone		 ((const char *) ent + ent_size)) {
24772376Sjake		matches = 1;
24872376Sjake		if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
24972376Sjake		    vendor != ent->pp_vendor)
25072376Sjake			matches = 0;
25172376Sjake		if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
25272376Sjake		    prod != ent->pp_product)
25350027Speter			matches = 0;
25450027Speter		if (matches && fcn != ent->pp_expfunc)
25550027Speter			matches = 0;
25672376Sjake		if (matches && ent->pp_vendor_str &&
25772376Sjake		    strcmp(ent->pp_vendor_str, vendorstr) != 0)
25850027Speter			matches = 0;
25972376Sjake		if (matches && ent->pp_product_str &&
26072376Sjake		    strcmp(ent->pp_product_str, prodstr) != 0)
26172376Sjake			matches = 0;
26272376Sjake		if (matchfn != NULL)
26372376Sjake			matches = (*matchfn)(dev, ent, matches);
26472376Sjake		if (matches)
26572376Sjake			return (ent);
26672376Sjake	}
26772376Sjake	return (NULL);
26872376Sjake}
26998469Speter
27072376Sjakestatic int
27172376Sjakepccard_card_gettype(device_t dev, int *type)
27272376Sjake{
27372376Sjake	struct pccard_softc *sc = PCCARD_SOFTC(dev);
27472376Sjake	struct pccard_function *pf;
27572376Sjake
27672376Sjake	/*
27772376Sjake	 * set the iftype to memory if this card has no functions (not yet
278165761Sjeff	 * probed), or only one function, and that is not initialized yet or
279171900Sjeff	 * that is memory.
280165761Sjeff	 */
281165761Sjeff	pf = STAILQ_FIRST(&sc->card.pf_head);
282171900Sjeff	if (pf == NULL ||
283165761Sjeff	    (STAILQ_NEXT(pf, pf_list) == NULL &&
284165761Sjeff	    (pf->cfe == NULL || pf->cfe->iftype == PCCARD_IFTYPE_MEMORY)))
285171900Sjeff		*type = PCCARD_IFTYPE_MEMORY;
286171900Sjeff	else
287171900Sjeff		*type = PCCARD_IFTYPE_IO;
288171900Sjeff	return 0;
289165761Sjeff}
290165761Sjeff
291171900Sjeff/*
292171900Sjeff * Initialize a PCCARD function.  May be called as long as the function is
293171900Sjeff * disabled.
294171900Sjeff */
295171900Sjeffvoid
296171900Sjeffpccard_function_init(struct pccard_function *pf)
297171900Sjeff{
298171900Sjeff	struct pccard_config_entry *cfe;
299165761Sjeff	int i;
300171900Sjeff	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
301171900Sjeff	struct resource_list *rl = &devi->resources;
302171900Sjeff	struct resource *r = 0;
303171900Sjeff	device_t bus;
304171900Sjeff
305171900Sjeff	if (pf->pf_flags & PFF_ENABLED)
306171900Sjeff		panic("pccard_function_init: function is enabled");
307171900Sjeff
308165761Sjeff	bus = device_get_parent(pf->dev);
309165761Sjeff	/* Remember which configuration entry we are using. */
31072376Sjake	for (cfe = STAILQ_FIRST(&pf->cfe_head); cfe != NULL;
31172376Sjake	    cfe = STAILQ_NEXT(cfe, cfe_list)) {
31272376Sjake		for (i = 0; i < cfe->num_iospace; i++)
31372376Sjake			cfe->iores[i] = NULL;
31472376Sjake		cfe->irqres = NULL;
31572376Sjake		for (i = 0; i < cfe->num_iospace; i++) {
31672376Sjake			/* XXX kludge, need to not ignore start */
31772376Sjake			/* XXX start is a hint here, so this would break */
31872376Sjake			/* XXX modems */
31972376Sjake			/* XXX ALSO: should just ask for the range 0 to */
32072376Sjake			/* XXX 1 << decode bits - 1, so we have a layering */
32172376Sjake			/* XXX violation now */
32272376Sjake			cfe->iorid[i] = i;
32372376Sjake			r = cfe->iores[i] = bus_alloc_resource(bus,
32472376Sjake			    SYS_RES_IOPORT, &cfe->iorid[i], 0x100, 0x3ff,
32572376Sjake			    cfe->iospace[i].length, 0);
32672376Sjake			if (cfe->iores[i] == 0)
32772376Sjake				goto not_this_one;
328164936Sjulian			resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i],
32972376Sjake			    rman_get_start(r), rman_get_end(r),
33072376Sjake			    cfe->iospace[i].length);
33150027Speter
332177435Sjeff		}
33350027Speter		if (cfe->num_memspace > 0) {
33472376Sjake			goto not_this_one;
33572376Sjake		}
33650027Speter		if (cfe->irqmask) {
337177435Sjeff			cfe->irqrid = 0;
338177435Sjeff			cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ,
33972376Sjake			    &cfe->irqrid, 10, 12, 1, 0);
34072376Sjake			if (cfe->irqres == 0)
341177435Sjeff				goto not_this_one;
342177435Sjeff			resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid,
343136170Sjulian			    rman_get_start(r), rman_get_end(r), 1);
344177435Sjeff		}
345136170Sjulian		/* XXX Don't know how to deal with maxtwins */
346177435Sjeff		/* If we get to here, we've allocated all we need */
347136170Sjulian		pf->cfe = cfe;
34850027Speter		break;
34950027Speter	    not_this_one:;
350165761Sjeff		for (i = 0; i < cfe->num_iospace; i++) {
351177435Sjeff			resource_list_delete(rl, SYS_RES_IOPORT, i);
352165761Sjeff			if (cfe->iores[i])
353165761Sjeff				bus_release_resource(bus, SYS_RES_IOPORT,
354165761Sjeff				    cfe->iorid[i], cfe->iores[i]);
355165761Sjeff			cfe->iores[i] = NULL;
356177435Sjeff		}
357165761Sjeff		if (cfe->irqmask && cfe->irqres) {
358165761Sjeff			resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid);
359177435Sjeff			bus_release_resource(bus, SYS_RES_IRQ,
360177435Sjeff			    cfe->irqrid, cfe->irqres);
361165761Sjeff			cfe->irqres = NULL;
362177435Sjeff		}
363165761Sjeff	}
364177435Sjeff}
365165761Sjeff
366165761Sjeff/* Enable a PCCARD function */
36750027Speterint
36872376Sjakepccard_function_enable(struct pccard_function *pf)
36972376Sjake{
37072376Sjake	struct pccard_function *tmp;
37150027Speter	int reg;
37272376Sjake	device_t dev = pf->sc->dev;
37372376Sjake
37450027Speter	if (pf->cfe == NULL)
37572376Sjake		panic("pccard_function_enable: function not initialized");
37672376Sjake
37772376Sjake	/*
37872376Sjake	 * Increase the reference count on the socket, enabling power, if
37972376Sjake	 * necessary.
38072376Sjake	 */
38172376Sjake	if (pf->sc->sc_enabled_count++ == 0)
38272376Sjake		POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
38372376Sjake	DEVPRINTF((dev, "++enabled_count = %d\n", pf->sc->sc_enabled_count));
38472376Sjake
38572376Sjake	if (pf->pf_flags & PFF_ENABLED) {
38672376Sjake		/*
38772376Sjake		 * Don't do anything if we're already enabled.
38850027Speter		 */
38950027Speter		return (0);
39050027Speter	}
391104964Sjeff
39250027Speter	/*
393177435Sjeff	 * it's possible for different functions' CCRs to be in the same
394177419Sjeff	 * underlying page.  Check for that.
39550027Speter	 */
39672376Sjake	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
397177435Sjeff		if ((tmp->pf_flags & PFF_ENABLED) &&
39872376Sjake		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
39950027Speter		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
40099072Sjulian		     (tmp->ccr_base - tmp->pf_ccr_offset +
40172376Sjake		      tmp->pf_ccr_realsize))) {
402134591Sjulian			pf->pf_ccrt = tmp->pf_ccrt;
403177419Sjeff			pf->pf_ccrh = tmp->pf_ccrh;
404134591Sjulian			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
405134591Sjulian
406134591Sjulian			/*
407134591Sjulian			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
408177419Sjeff			 * tmp->ccr_base) + pf->ccr_base;
409134591Sjulian			 */
410177435Sjeff			pf->pf_ccr_offset =
411177435Sjeff			    (tmp->pf_ccr_offset + pf->ccr_base) -
412134591Sjulian			    tmp->ccr_base;
413177435Sjeff			pf->pf_ccr_window = tmp->pf_ccr_window;
414178961Sjulian			break;
415177435Sjeff		}
416134591Sjulian	}
417134591Sjulian
418177435Sjeff	if (tmp == NULL) {
419134591Sjulian		pf->ccr_rid = 0;
420153797Skan		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
421177435Sjeff		    &pf->ccr_rid, 0xa0000, 0xdffff, 1 << 10, RF_ACTIVE);
422177435Sjeff		if (!pf->ccr_res) {
423177419Sjeff			DEVPRINTF((dev, "ccr_res == 0\n"));
424177435Sjeff			goto bad;
425177435Sjeff		}
426177419Sjeff		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
427177419Sjeff		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
428177419Sjeff		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
429177419Sjeff		    pf->ccr_rid, (pf->ccr_rid >> 10) << 10);
430177419Sjeff		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
431177419Sjeff		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
432177419Sjeff		pf->pf_ccr_offset = rman_get_start(pf->ccr_res);
433177419Sjeff		pf->pf_ccr_realsize = 1;
434177419Sjeff	}
435177435Sjeff
436177419Sjeff	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
437177419Sjeff	reg |= PCCARD_CCR_OPTION_LEVIREQ;
438177419Sjeff	if (pccard_mfc(pf->sc)) {
439177435Sjeff		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
440177419Sjeff			PCCARD_CCR_OPTION_ADDR_DECODE);
441177419Sjeff		/*
442177419Sjeff		 * XXX Need to enable PCCARD_CCR_OPTION_IRQ_ENABLE if
443177419Sjeff		 * XXX we have an interrupt handler, but we don't know that
444177435Sjeff		 * XXX at this point.
445177435Sjeff		 */
44699072Sjulian#if 0
447177435Sjeff		if (pf->ih_fct)
448177435Sjeff			reg |= PCCARD_CCR_OPTION_IREQ_ENABLE;
44950027Speter#endif
450177435Sjeff	}
45172376Sjake	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
45299072Sjulian
45350027Speter	reg = 0;
45472376Sjake	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
455177435Sjeff		reg |= PCCARD_CCR_STATUS_IOIS8;
456166557Sjeff	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
457165761Sjeff		reg |= PCCARD_CCR_STATUS_AUDIO;
458165761Sjeff	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
459177435Sjeff
460165761Sjeff	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
461165761Sjeff
462165765Sjeff	if (pccard_mfc(pf->sc)) {
463165761Sjeff		long tmp, iosize;
464177435Sjeff
465177435Sjeff		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
466165761Sjeff		/* round up to nearest (2^n)-1 */
467177435Sjeff		for (iosize = 1; iosize < tmp; iosize <<= 1)
468177435Sjeff			;
469177435Sjeff		iosize--;
470165761Sjeff
471177435Sjeff		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
472165761Sjeff				 pf->pf_mfc_iobase & 0xff);
473165761Sjeff		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
474165761Sjeff				 (pf->pf_mfc_iobase >> 8) & 0xff);
47572376Sjake		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
476164936Sjulian		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
47772376Sjake
478166188Sjeff		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
47972376Sjake	}
48072376Sjake
481177435Sjeff#ifdef PCCARDDEBUG
48272376Sjake	if (pccard_debug) {
483165761Sjeff		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
484177435Sjeff			device_printf(tmp->sc->dev,
485165761Sjeff			    "function %d CCR at %d offset %x: "
486165761Sjeff			    "%x %x %x %x, %x %x %x %x, %x\n",
487165761Sjeff			    tmp->number, tmp->pf_ccr_window,
488177435Sjeff			    tmp->pf_ccr_offset,
489165761Sjeff			    pccard_ccr_read(tmp, 0x00),
49072376Sjake			    pccard_ccr_read(tmp, 0x02),
491166557Sjeff			    pccard_ccr_read(tmp, 0x04),
49272376Sjake			    pccard_ccr_read(tmp, 0x06),
493177435Sjeff			    pccard_ccr_read(tmp, 0x0A),
494172207Sjeff			    pccard_ccr_read(tmp, 0x0C),
495177435Sjeff			    pccard_ccr_read(tmp, 0x0E),
496170293Sjeff			    pccard_ccr_read(tmp, 0x10),
49772376Sjake			    pccard_ccr_read(tmp, 0x12));
498177435Sjeff		}
499177435Sjeff	}
500177435Sjeff#endif
50172376Sjake	pf->pf_flags |= PFF_ENABLED;
502165761Sjeff	return (0);
50372376Sjake
504165761Sjeff bad:
505165761Sjeff	/*
50672376Sjake	 * Decrement the reference count, and power down the socket, if
50772376Sjake	 * necessary.
508	 */
509	if (--pf->sc->sc_enabled_count == 0)
510		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
511	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
512
513	return (1);
514}
515
516/* Disable PCCARD function. */
517void
518pccard_function_disable(struct pccard_function *pf)
519{
520	struct pccard_function *tmp;
521	device_t dev = pf->sc->dev;
522
523	if (pf->cfe == NULL)
524		panic("pccard_function_disable: function not initialized");
525
526	if ((pf->pf_flags & PFF_ENABLED) == 0) {
527		/*
528		 * Don't do anything if we're already disabled.
529		 */
530		return;
531	}
532
533	/*
534	 * it's possible for different functions' CCRs to be in the same
535	 * underlying page.  Check for that.  Note we mark us as disabled
536	 * first to avoid matching ourself.
537	 */
538
539	pf->pf_flags &= ~PFF_ENABLED;
540	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
541		if ((tmp->pf_flags & PFF_ENABLED) &&
542		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
543		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
544		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
545			break;
546	}
547
548	/* Not used by anyone else; unmap the CCR. */
549	if (tmp == NULL) {
550		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
551		    pf->ccr_res);
552		pf->ccr_res = NULL;
553	}
554
555	/*
556	 * Decrement the reference count, and power down the socket, if
557	 * necessary.
558	 */
559	if (--pf->sc->sc_enabled_count == 0)
560		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
561	DEVPRINTF((dev, "--enabled_count = %d\n", pf->sc->sc_enabled_count));
562}
563
564#if 0
565/* XXX These functions are needed, but not like this XXX */
566int
567pccard_io_map(struct pccard_function *pf, int width, bus_addr_t offset,
568    bus_size_t size, struct pccard_io_handle *pcihp, int *windowp)
569{
570	int reg;
571
572	if (pccard_chip_io_map(pf->sc->pct, pf->sc->pch, width, offset, size,
573	    pcihp, windowp))
574		return (1);
575
576	/*
577	 * XXX in the multifunction multi-iospace-per-function case, this
578	 * needs to cooperate with io_alloc to make sure that the spaces
579	 * don't overlap, and that the ccr's are set correctly
580	 */
581
582	if (pccard_mfc(pf->sc)) {
583		long tmp, iosize;
584
585		if (pf->pf_mfc_iomax == 0) {
586			pf->pf_mfc_iobase = pcihp->addr + offset;
587			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
588		} else {
589			/* this makes the assumption that nothing overlaps */
590			if (pf->pf_mfc_iobase > pcihp->addr + offset)
591				pf->pf_mfc_iobase = pcihp->addr + offset;
592			if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
593				pf->pf_mfc_iomax = pcihp->addr + offset + size;
594		}
595
596		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
597		/* round up to nearest (2^n)-1 */
598		for (iosize = 1; iosize >= tmp; iosize <<= 1)
599			;
600		iosize--;
601
602		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
603		    pf->pf_mfc_iobase & 0xff);
604		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
605		    (pf->pf_mfc_iobase >> 8) & 0xff);
606		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
607		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
608
609		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
610
611		reg = pccard_ccr_read(pf, PCCARD_CCR_OPTION);
612		reg |= PCCARD_CCR_OPTION_ADDR_DECODE;
613		pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
614	}
615	return (0);
616}
617
618void
619pccard_io_unmap(struct pccard_function *pf, int window)
620{
621
622	pccard_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
623
624	/* XXX Anything for multi-function cards? */
625}
626#endif
627
628/*
629 * simulate the old "probe" routine.  In the new world order, the driver
630 * needs to grab devices while in the old they were assigned to the device by
631 * the pccardd process.  These symbols are exported to the upper layers.
632 */
633int
634pccard_compat_probe(device_t dev)
635{
636	return (CARD_COMPAT_MATCH(dev));
637}
638
639int
640pccard_compat_attach(device_t dev)
641{
642	int err;
643
644	err = CARD_COMPAT_PROBE(dev);
645	if (err == 0)
646		err = CARD_COMPAT_ATTACH(dev);
647	return (err);
648}
649
650#define PCCARD_NPORT	2
651#define PCCARD_NMEM	5
652#define PCCARD_NIRQ	1
653#define PCCARD_NDRQ	0
654
655static int
656pccard_add_children(device_t dev, int busno)
657{
658	/* Call parent to scan for any current children */
659	return 0;
660}
661
662static int
663pccard_probe(device_t dev)
664{
665	device_set_desc(dev, "PC Card bus -- newconfig version");
666	return pccard_add_children(dev, device_get_unit(dev));
667}
668
669static int
670pccard_attach(device_t dev)
671{
672	struct pccard_softc *sc = PCCARD_SOFTC(dev);
673
674	sc->dev = dev;
675	sc->sc_enabled_count = 0;
676	DEVPRINTF((dev, "pccard_attach %p\n", dev));
677	return bus_generic_attach(dev);
678}
679
680static void
681pccard_print_resources(struct resource_list *rl, const char *name, int type,
682    int count, const char *format)
683{
684	struct resource_list_entry *rle;
685	int printed;
686	int i;
687
688	printed = 0;
689	for (i = 0; i < count; i++) {
690		rle = resource_list_find(rl, type, i);
691		if (rle) {
692			if (printed == 0)
693				printf(" %s ", name);
694			else if (printed > 0)
695				printf(",");
696			printed++;
697			printf(format, rle->start);
698			if (rle->count > 1) {
699				printf("-");
700				printf(format, rle->start + rle->count - 1);
701			}
702		} else if (i > 3) {
703			/* check the first few regardless */
704			break;
705		}
706	}
707}
708
709static int
710pccard_print_child(device_t dev, device_t child)
711{
712	struct pccard_ivar *devi = PCCARD_IVAR(child);
713	struct resource_list *rl = &devi->resources;
714	int retval = 0;
715
716	retval += bus_print_child_header(dev, child);
717	retval += printf(" at");
718
719	if (devi) {
720		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
721		    PCCARD_NPORT, "%#lx");
722		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
723		    PCCARD_NMEM, "%#lx");
724		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
725		    "%ld");
726		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
727		    "%ld");
728	}
729
730	retval += bus_print_child_footer(dev, child);
731
732	return (retval);
733}
734
735static int
736pccard_set_resource(device_t dev, device_t child, int type, int rid,
737		 u_long start, u_long count)
738{
739	struct pccard_ivar *devi = PCCARD_IVAR(child);
740	struct resource_list *rl = &devi->resources;
741
742	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
743	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
744		return EINVAL;
745	if (rid < 0)
746		return EINVAL;
747	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
748		return EINVAL;
749	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
750		return EINVAL;
751	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
752		return EINVAL;
753	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
754		return EINVAL;
755
756	resource_list_add(rl, type, rid, start, start + count - 1, count);
757
758	return 0;
759}
760
761static int
762pccard_get_resource(device_t dev, device_t child, int type, int rid,
763    u_long *startp, u_long *countp)
764{
765	struct pccard_ivar *devi = PCCARD_IVAR(child);
766	struct resource_list *rl = &devi->resources;
767	struct resource_list_entry *rle;
768
769	rle = resource_list_find(rl, type, rid);
770	if (!rle)
771		return ENOENT;
772
773	if (startp)
774		*startp = rle->start;
775	if (countp)
776		*countp = rle->count;
777
778	return 0;
779}
780
781static void
782pccard_delete_resource(device_t dev, device_t child, int type, int rid)
783{
784	struct pccard_ivar *devi = PCCARD_IVAR(child);
785	struct resource_list *rl = &devi->resources;
786	resource_list_delete(rl, type, rid);
787}
788
789static int
790pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
791    u_int32_t flags)
792{
793	return CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
794	    rid, flags);
795}
796
797static int
798pccard_set_memory_offset(device_t dev, device_t child, int rid,
799     u_int32_t offset)
800{
801	return CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
802	    offset);
803}
804
805static int
806pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
807{
808	struct pccard_ivar *devi = PCCARD_IVAR(child);
809	struct pccard_function *func = devi->fcn;
810	struct pccard_softc *sc = PCCARD_SOFTC(bus);
811
812	/* PCCARD_IVAR_ETHADDR unhandled from oldcard */
813	switch (which) {
814	default:
815	case PCCARD_IVAR_ETHADDR:
816		return (ENOENT);
817		break;
818	case PCCARD_IVAR_VENDOR:
819		*(u_int32_t *) result = sc->card.manufacturer;
820		break;
821	case PCCARD_IVAR_PRODUCT:
822		*(u_int32_t *) result = sc->card.product;
823		break;
824	case PCCARD_IVAR_FUNCTION_NUMBER:
825		if (!func) {
826			device_printf(bus, "No function number, bug!\n");
827			return (ENOENT);
828		}
829		*(u_int32_t *) result = func->number;
830		break;
831	case PCCARD_IVAR_VENDOR_STR:
832		*(char **) result = sc->card.cis1_info[0];
833		break;
834	case PCCARD_IVAR_PRODUCT_STR:
835		*(char **) result = sc->card.cis1_info[1];
836		break;
837	case PCCARD_IVAR_CIS3_STR:
838		*(char **) result = sc->card.cis1_info[2];
839		break;
840	case PCCARD_IVAR_CIS4_STR:
841		*(char **) result = sc->card.cis1_info[2];
842		break;
843	}
844	return (0);
845}
846
847static void
848pccard_driver_added(device_t dev, driver_t *driver)
849{
850	/*
851	 * XXX eventually we need to attach stuff when we know we
852	 * XXX have kids.  For now we do nothing because we normally
853	 * XXX add children ourselves.  We don't want to necessarily
854	 * XXX force a reprobe.
855	 */
856}
857
858static struct resource *
859pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
860    u_long start, u_long end, u_long count, u_int flags)
861{
862	struct pccard_ivar *ivar;
863	struct pccard_function *pf;
864
865	if (device_get_parent(child) == dev) {
866		ivar = PCCARD_IVAR(child);
867		pf = ivar->fcn;
868		switch (type) {
869		case SYS_RES_IRQ:
870			if (*rid > 0)
871				return NULL;
872			return (pf->cfe->irqres);
873		case SYS_RES_IOPORT:
874			if (*rid > 3)	/* XXX */
875				return NULL;
876			return (pf->cfe->iores[*rid]);
877		}
878	}
879	return (bus_generic_alloc_resource(dev, child, type, rid, start,
880	    end, count, flags));
881}
882
883static int
884pccard_release_resource(device_t dev, device_t child, int type, int rid,
885    struct resource *r)
886{
887	return bus_generic_release_resource(dev, child, type, rid, r);
888}
889
890static int
891pccard_activate_resource(device_t dev, device_t child, int type, int rid,
892    struct resource *r)
893{
894	/* XXX need to write to the COR to activate this */
895	return (bus_generic_activate_resource(dev, child, type, rid, r));
896}
897
898static int
899pccard_deactivate_resource(device_t dev, device_t child, int type, int rid,
900    struct resource *r)
901{
902	/* XXX need to write to the COR to deactivate this */
903	return (bus_generic_deactivate_resource(dev, child, type, rid, r));
904}
905
906static device_method_t pccard_methods[] = {
907	/* Device interface */
908	DEVMETHOD(device_probe,		pccard_probe),
909	DEVMETHOD(device_attach,	pccard_attach),
910	DEVMETHOD(device_detach,	bus_generic_detach),
911	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
912	DEVMETHOD(device_suspend,	bus_generic_suspend),
913	DEVMETHOD(device_resume,	bus_generic_resume),
914
915	/* Bus interface */
916	DEVMETHOD(bus_print_child,	pccard_print_child),
917	DEVMETHOD(bus_driver_added,	pccard_driver_added),
918	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
919	DEVMETHOD(bus_release_resource,	pccard_release_resource),
920	DEVMETHOD(bus_activate_resource, pccard_activate_resource),
921	DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
922	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
923	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
924	DEVMETHOD(bus_set_resource,	pccard_set_resource),
925	DEVMETHOD(bus_get_resource,	pccard_get_resource),
926	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
927	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
928
929	/* Card Interface */
930	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
931	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
932	DEVMETHOD(card_get_type,	pccard_card_gettype),
933	DEVMETHOD(card_attach_card,	pccard_attach_card),
934	DEVMETHOD(card_detach_card,	pccard_detach_card),
935
936	{ 0, 0 }
937};
938
939static driver_t pccard_driver = {
940	"pccard",
941	pccard_methods,
942	sizeof(struct pccard_softc)
943};
944
945devclass_t	pccard_devclass;
946
947DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
948DRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0);
949DRIVER_MODULE(pccard, pccbb, pccard_driver, pccard_devclass, 0, 0);
950DRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0);
951MODULE_VERSION(pccard, 1);
952MODULE_DEPEND(pccard, pcic, 1, 1, 1);
953