pccard.c revision 121958
166200Simp/*	$NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $	*/
252506Simp
352506Simp/*
452506Simp * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
552506Simp *
652506Simp * Redistribution and use in source and binary forms, with or without
752506Simp * modification, are permitted provided that the following conditions
852506Simp * are met:
952506Simp * 1. Redistributions of source code must retain the above copyright
1052506Simp *    notice, this list of conditions and the following disclaimer.
1152506Simp * 2. Redistributions in binary form must reproduce the above copyright
1252506Simp *    notice, this list of conditions and the following disclaimer in the
1352506Simp *    documentation and/or other materials provided with the distribution.
1452506Simp * 3. All advertising materials mentioning features or use of this software
1552506Simp *    must display the following acknowledgement:
1652506Simp *	This product includes software developed by Marc Horowitz.
1752506Simp * 4. The name of the author may not be used to endorse or promote products
1852506Simp *    derived from this software without specific prior written permission.
1952506Simp *
2052506Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2152506Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2252506Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2352506Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2452506Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2552506Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2652506Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2752506Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2852506Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2952506Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3052506Simp */
3152506Simp
32119418Sobrien#include <sys/cdefs.h>
33119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/pccard/pccard.c 121958 2003-11-03 16:04:31Z imp $");
34119418Sobrien
3552506Simp#include <sys/param.h>
3652506Simp#include <sys/systm.h>
3752506Simp#include <sys/malloc.h>
3852506Simp#include <sys/module.h>
3952506Simp#include <sys/kernel.h>
4052506Simp#include <sys/queue.h>
4191786Simp#include <sys/sysctl.h>
4252506Simp#include <sys/types.h>
4352506Simp
4452506Simp#include <sys/bus.h>
4552506Simp#include <machine/bus.h>
4652506Simp#include <sys/rman.h>
4752506Simp#include <machine/resource.h>
4852506Simp
4982781Sshiba#include <net/ethernet.h>
5082781Sshiba
5152506Simp#include <dev/pccard/pccardreg.h>
5252506Simp#include <dev/pccard/pccardvar.h>
5352506Simp
5455500Simp#include "power_if.h"
5559193Simp#include "card_if.h"
5655500Simp
5755500Simp#define PCCARDDEBUG
5855500Simp
5991786Simp/* sysctl vars */
6091786SimpSYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters");
6191786Simp
6291786Simpint	pccard_debug = 0;
6391786SimpTUNABLE_INT("hw.pccard.debug", &pccard_debug);
6491786SimpSYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW,
6591786Simp    &pccard_debug, 0,
6691786Simp  "pccard debug");
6791786Simp
6891786Simpint	pccard_cis_debug = 0;
6991786SimpTUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug);
7091786SimpSYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW,
7191786Simp    &pccard_cis_debug, 0, "pccard CIS debug");
7291786Simp
7352506Simp#ifdef PCCARDDEBUG
7452506Simp#define	DPRINTF(arg) if (pccard_debug) printf arg
7555500Simp#define	DEVPRINTF(arg) if (pccard_debug) device_printf arg
7667333Simp#define PRVERBOSE(arg) printf arg
7767333Simp#define DEVPRVERBOSE(arg) device_printf arg
7852506Simp#else
7952506Simp#define	DPRINTF(arg)
8055500Simp#define	DEVPRINTF(arg)
8167333Simp#define PRVERBOSE(arg) if (bootverbose) printf arg
8267333Simp#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg
8352506Simp#endif
8452506Simp
8582378Sjonstatic int	pccard_ccr_read(struct pccard_function *pf, int ccr);
8682378Sjonstatic void	pccard_ccr_write(struct pccard_function *pf, int ccr, int val);
8782378Sjonstatic int	pccard_attach_card(device_t dev);
88106362Simpstatic int	pccard_detach_card(device_t dev);
8982378Sjonstatic void	pccard_function_init(struct pccard_function *pf);
9082378Sjonstatic void	pccard_function_free(struct pccard_function *pf);
9182378Sjonstatic int	pccard_function_enable(struct pccard_function *pf);
9282378Sjonstatic void	pccard_function_disable(struct pccard_function *pf);
9382378Sjonstatic int	pccard_compat_do_probe(device_t bus, device_t dev);
9482378Sjonstatic int	pccard_compat_do_attach(device_t bus, device_t dev);
9582378Sjonstatic int	pccard_add_children(device_t dev, int busno);
9682378Sjonstatic int	pccard_probe(device_t dev);
9782378Sjonstatic int	pccard_attach(device_t dev);
9882378Sjonstatic int	pccard_detach(device_t dev);
9982378Sjonstatic void	pccard_print_resources(struct resource_list *rl,
10082378Sjon		    const char *name, int type, int count, const char *format);
10182378Sjonstatic int	pccard_print_child(device_t dev, device_t child);
10282378Sjonstatic int	pccard_set_resource(device_t dev, device_t child, int type,
10382378Sjon		    int rid, u_long start, u_long count);
10482378Sjonstatic int	pccard_get_resource(device_t dev, device_t child, int type,
10582378Sjon		    int rid, u_long *startp, u_long *countp);
10682378Sjonstatic void	pccard_delete_resource(device_t dev, device_t child, int type,
10782378Sjon		    int rid);
10882378Sjonstatic int	pccard_set_res_flags(device_t dev, device_t child, int type,
10982378Sjon		    int rid, u_int32_t flags);
11082378Sjonstatic int	pccard_set_memory_offset(device_t dev, device_t child, int rid,
11182378Sjon		    u_int32_t offset, u_int32_t *deltap);
112104641Simpstatic void	pccard_probe_nomatch(device_t cbdev, device_t child);
11382378Sjonstatic int	pccard_read_ivar(device_t bus, device_t child, int which,
11482378Sjon		    u_char *result);
11582378Sjonstatic void	pccard_driver_added(device_t dev, driver_t *driver);
11682378Sjonstatic struct resource *pccard_alloc_resource(device_t dev,
11782378Sjon		    device_t child, int type, int *rid, u_long start,
11882378Sjon		    u_long end, u_long count, u_int flags);
11982378Sjonstatic int	pccard_release_resource(device_t dev, device_t child, int type,
12082378Sjon		    int rid, struct resource *r);
12182378Sjonstatic void	pccard_child_detached(device_t parent, device_t dev);
12282378Sjonstatic void	pccard_intr(void *arg);
12382378Sjonstatic int	pccard_setup_intr(device_t dev, device_t child,
12482378Sjon		    struct resource *irq, int flags, driver_intr_t *intr,
12582378Sjon		    void *arg, void **cookiep);
12682378Sjonstatic int	pccard_teardown_intr(device_t dev, device_t child,
12782378Sjon		    struct resource *r, void *cookie);
12852506Simp
12997613Stakawatastatic const struct pccard_product *
13097613Stakawatapccard_do_product_lookup(device_t bus, device_t dev,
13197613Stakawata			 const struct pccard_product *tab, size_t ent_size,
13297613Stakawata			 pccard_product_match_fn matchfn);
13397613Stakawata
13497613Stakawata
13582378Sjonstatic int
13674632Simppccard_ccr_read(struct pccard_function *pf, int ccr)
13752506Simp{
13852506Simp	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
13952506Simp	    pf->pf_ccr_offset + ccr));
14052506Simp}
14152506Simp
14282378Sjonstatic void
14374632Simppccard_ccr_write(struct pccard_function *pf, int ccr, int val)
14452506Simp{
14552506Simp	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
14652506Simp		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
14752506Simp		    pf->pf_ccr_offset + ccr, val);
14852506Simp	}
14952506Simp}
15052506Simp
15159193Simpstatic int
152113242Simppccard_set_default_descr(device_t dev)
153113242Simp{
154121521Simp	const char *vendorstr, *prodstr;
155121521Simp	char *str;
156113242Simp
157113242Simp	if (pccard_get_vendor_str(dev, &vendorstr))
158113242Simp		return (0);
159113242Simp	if (pccard_get_product_str(dev, &prodstr))
160113242Simp		return (0);
161113242Simp	str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF,
162113242Simp	    M_WAITOK);
163113242Simp	sprintf(str, "%s %s", vendorstr, prodstr);
164113242Simp	device_set_desc_copy(dev, str);
165113242Simp	free(str, M_DEVBUF);
166113242Simp	return (0);
167113242Simp}
168113242Simp
169113242Simpstatic int
17059193Simppccard_attach_card(device_t dev)
17152506Simp{
17264850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
17352506Simp	struct pccard_function *pf;
17465917Simp	struct pccard_ivar *ivar;
17561788Simp	device_t child;
176102713Simp	int i;
17752506Simp
17852506Simp	/*
17952506Simp	 * this is here so that when socket_enable calls gettype, trt happens
18052506Simp	 */
18152506Simp	STAILQ_INIT(&sc->card.pf_head);
18252506Simp
18355500Simp	DEVPRINTF((dev, "chip_socket_enable\n"));
18455500Simp	POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
18552506Simp
18655500Simp	DEVPRINTF((dev, "read_cis\n"));
18752506Simp	pccard_read_cis(sc);
18852506Simp
18955500Simp	DEVPRINTF((dev, "check_cis_quirks\n"));
19052506Simp	pccard_check_cis_quirks(dev);
19152506Simp
19252506Simp	/*
19352506Simp	 * bail now if the card has no functions, or if there was an error in
19452506Simp	 * the cis.
19552506Simp	 */
19652506Simp
19770715Sjon	if (sc->card.error) {
198102713Simp		device_printf(dev, "CARD ERROR!\n");
19952506Simp		return (1);
20070715Sjon	}
20170715Sjon	if (STAILQ_EMPTY(&sc->card.pf_head)) {
202102713Simp		device_printf(dev, "Card has no functions!\n");
20352506Simp		return (1);
20470715Sjon	}
20552506Simp
20690436Simp	if (bootverbose || pccard_debug)
20752506Simp		pccard_print_cis(dev);
20852506Simp
20955500Simp	DEVPRINTF((dev, "functions scanning\n"));
210102713Simp	i = -1;
21152506Simp	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
212102713Simp		i++;
213102713Simp		if (STAILQ_EMPTY(&pf->cfe_head)) {
214102713Simp			device_printf(dev,
215102713Simp			    "Function %d has no config entries.!\n", i);
21652506Simp			continue;
217102713Simp		}
21852506Simp		pf->sc = sc;
21952506Simp		pf->cfe = NULL;
22064927Simp		pf->dev = NULL;
22152506Simp	}
222104641Simp	DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1,
223102713Simp	    pccard_mfc(sc)));
22482378Sjon
22552506Simp	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
22652506Simp		if (STAILQ_EMPTY(&pf->cfe_head))
22752506Simp			continue;
22861788Simp		/*
22961788Simp		 * In NetBSD, the drivers are responsible for activating
23061788Simp		 * each function of a card.  I think that in FreeBSD we
23161788Simp		 * want to activate them enough for the usual bus_*_resource
23261788Simp		 * routines will do the right thing.  This many mean a
23361788Simp		 * departure from the current NetBSD model.
23461788Simp		 *
235104641Simp		 * This seems to work well in practice for most cards.
236104641Simp		 * However, there are two cases that are problematic.
237104641Simp		 * If a driver wishes to pick and chose which config
238104641Simp		 * entry to use, then this method falls down.  These
239104641Simp		 * are usually older cards.  In addition, there are
240104641Simp		 * some cards that have multiple hardware units on the
241104641Simp		 * cards, but presents only one CIS chain.  These cards
242104641Simp		 * are combination cards, but only one of these units
243104641Simp		 * can be on at a time.
24461788Simp		 */
24567897Sdwmalone		ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF,
246111119Simp		    M_WAITOK | M_ZERO);
24761788Simp		child = device_add_child(dev, NULL, -1);
24865917Simp		device_set_ivars(child, ivar);
24966847Simp		ivar->fcn = pf;
25067187Simp		pf->dev = child;
25167167Simp		/*
25267167Simp		 * XXX We might want to move the next two lines into
25367167Simp		 * XXX the pccard interface layer.  For the moment, this
25467167Simp		 * XXX is OK, but some drivers want to pick the config
25567167Simp		 * XXX entry to use as well as some address tweaks (mostly
25667167Simp		 * XXX due to bugs in decode logic that makes some
25767167Simp		 * XXX addresses illegal or broken).
25867167Simp		 */
25965917Simp		pccard_function_init(pf);
26082378Sjon		if (sc->sc_enabled_count == 0)
26182378Sjon			POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
26267333Simp		if (pccard_function_enable(pf) == 0 &&
263113242Simp		    pccard_set_default_descr(child) == 0 &&
26467333Simp		    device_probe_and_attach(child) == 0) {
26555500Simp			DEVPRINTF((sc->dev, "function %d CCR at %d "
266121905Simp			    "offset %x mask %x: "
267121905Simp			    "%x %x %x %x, %x %x %x %x, %x\n",
26867167Simp			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
269121905Simp			    pf->ccr_mask, pccard_ccr_read(pf, 0x00),
27052506Simp			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
27152506Simp			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
27252506Simp			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
27352506Simp			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
27467167Simp		} else {
27586907Simp			if (pf->cfe != NULL)
27686907Simp				pccard_function_disable(pf);
27752506Simp		}
27852506Simp	}
27974632Simp	return (0);
28052506Simp}
28152506Simp
28259193Simpstatic int
283106362Simppccard_detach_card(device_t dev)
28452506Simp{
28564850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
28652506Simp	struct pccard_function *pf;
28782378Sjon	struct pccard_config_entry *cfe;
288119755Simp	int state;
28952506Simp
29052506Simp	/*
29152506Simp	 * We are running on either the PCCARD socket's event thread
29252506Simp	 * or in user context detaching a device by user request.
29352506Simp	 */
29452506Simp	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
295119755Simp		if (pf->dev == NULL)
296119755Simp			continue;
297119755Simp		state = device_get_state(pf->dev);
29882378Sjon		if (state == DS_ATTACHED || state == DS_BUSY)
29982378Sjon			device_detach(pf->dev);
30086907Simp		if (pf->cfe != NULL)
30186907Simp			pccard_function_disable(pf);
30282378Sjon		pccard_function_free(pf);
303106896Simp		device_delete_child(dev, pf->dev);
30452506Simp	}
30582378Sjon	if (sc->sc_enabled_count == 0)
30682378Sjon		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
30782378Sjon
30882378Sjon	while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
30982378Sjon		while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
31082378Sjon			STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
31182378Sjon			free(cfe, M_DEVBUF);
31282378Sjon		}
31382378Sjon		STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
31482378Sjon		free(pf, M_DEVBUF);
31582378Sjon	}
31674632Simp	return (0);
31752506Simp}
31852506Simp
31997613Stakawatastatic const struct pccard_product *
32097613Stakawatapccard_do_product_lookup(device_t bus, device_t dev,
321112359Simp    const struct pccard_product *tab, size_t ent_size,
322112359Simp    pccard_product_match_fn matchfn)
32366200Simp{
32466200Simp	const struct pccard_product *ent;
32566200Simp	int matches;
32666200Simp	u_int32_t fcn;
32766200Simp	u_int32_t vendor;
32866200Simp	u_int32_t prod;
329121521Simp	const char *vendorstr;
330121521Simp	const char *prodstr;
33166200Simp
33266200Simp#ifdef DIAGNOSTIC
33366200Simp	if (sizeof *ent > ent_size)
334112359Simp		panic("pccard_product_lookup: bogus ent_size %jd",
335112359Simp		    (intmax_t) ent_size);
33666200Simp#endif
33766200Simp	if (pccard_get_vendor(dev, &vendor))
33866200Simp		return (NULL);
33966200Simp	if (pccard_get_product(dev, &prod))
34066200Simp		return (NULL);
34166200Simp	if (pccard_get_function_number(dev, &fcn))
34266200Simp		return (NULL);
34366200Simp	if (pccard_get_vendor_str(dev, &vendorstr))
34466200Simp		return (NULL);
34566200Simp	if (pccard_get_product_str(dev, &prodstr))
34666200Simp		return (NULL);
347113313Simp	for (ent = tab; ent->pp_vendor != 0; ent =
34882378Sjon	    (const struct pccard_product *) ((const char *) ent + ent_size)) {
34966200Simp		matches = 1;
35086642Simp		if (ent->pp_vendor == PCCARD_VENDOR_ANY &&
351113078Ssanpei		    ent->pp_product == PCCARD_PRODUCT_ANY &&
35286642Simp		    ent->pp_cis[0] == NULL &&
35386642Simp		    ent->pp_cis[1] == NULL) {
354113300Simp			if (ent->pp_name)
355113300Simp				device_printf(dev,
356113300Simp				    "Total wildcard entry ignored for %s\n",
357113300Simp				    ent->pp_name);
35886642Simp			continue;
35986642Simp		}
36066200Simp		if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
36166200Simp		    vendor != ent->pp_vendor)
36266200Simp			matches = 0;
36366200Simp		if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
36466200Simp		    prod != ent->pp_product)
36566200Simp			matches = 0;
36666200Simp		if (matches && fcn != ent->pp_expfunc)
36766200Simp			matches = 0;
36871322Simp		if (matches && ent->pp_cis[0] &&
36971322Simp		    strcmp(ent->pp_cis[0], vendorstr) != 0)
37066200Simp			matches = 0;
37171322Simp		if (matches && ent->pp_cis[1] &&
37271322Simp		    strcmp(ent->pp_cis[1], prodstr) != 0)
37366200Simp			matches = 0;
37471322Simp		/* XXX need to match cis[2] and cis[3] also XXX */
37566200Simp		if (matchfn != NULL)
37666200Simp			matches = (*matchfn)(dev, ent, matches);
37766200Simp		if (matches)
37866200Simp			return (ent);
37966200Simp	}
38066200Simp	return (NULL);
38166200Simp}
38266200Simp
38352506Simp/*
38452506Simp * Initialize a PCCARD function.  May be called as long as the function is
38552506Simp * disabled.
38682382Simp *
38782382Simp * Note: pccard_function_init should not keep resources allocated.  It should
38882382Simp * only set them up ala isa pnp, set the values in the rl lists, and return.
38982382Simp * Any resource held after pccard_function_init is called is a bug.  However,
39082382Simp * the bus routines to get the resources also assume that pccard_function_init
39182382Simp * does this, so they need to be fixed too.
39252506Simp */
39382378Sjonstatic void
39470715Sjonpccard_function_init(struct pccard_function *pf)
39552506Simp{
39665917Simp	struct pccard_config_entry *cfe;
39767187Simp	int i;
39867242Simp	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
39967242Simp	struct resource_list *rl = &devi->resources;
40070762Simp	struct resource_list_entry *rle;
40167242Simp	struct resource *r = 0;
40267242Simp	device_t bus;
40367424Simp	int start;
40467424Simp	int end;
40590897Simp	int spaces;
40665917Simp
40770715Sjon	if (pf->pf_flags & PFF_ENABLED) {
40870715Sjon		printf("pccard_function_init: function is enabled");
40970715Sjon		return;
41070715Sjon	}
41167242Simp	bus = device_get_parent(pf->dev);
41252506Simp	/* Remember which configuration entry we are using. */
41372012Sphk	STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
41467187Simp		for (i = 0; i < cfe->num_iospace; i++)
41567187Simp			cfe->iores[i] = NULL;
41667187Simp		cfe->irqres = NULL;
41790897Simp		spaces = 0;
41867187Simp		for (i = 0; i < cfe->num_iospace; i++) {
41967424Simp			start = cfe->iospace[i].start;
42067424Simp			if (start)
42167424Simp				end = start + cfe->iospace[i].length - 1;
42267424Simp			else
42367424Simp				end = ~0;
42467187Simp			cfe->iorid[i] = i;
42590897Simp			DEVPRINTF((bus, "I/O rid %d start %x end %x\n",
42690897Simp			    i, start, end));
42767242Simp			r = cfe->iores[i] = bus_alloc_resource(bus,
42867424Simp			    SYS_RES_IOPORT, &cfe->iorid[i], start, end,
42970715Sjon			    cfe->iospace[i].length,
43067424Simp			    rman_make_alignment_flags(cfe->iospace[i].length));
43176424Simp			if (cfe->iores[i] == NULL)
43267187Simp				goto not_this_one;
43367242Simp			resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i],
43467242Simp			    rman_get_start(r), rman_get_end(r),
43567242Simp			    cfe->iospace[i].length);
43676424Simp			rle = resource_list_find(rl, SYS_RES_IOPORT,
43776424Simp			    cfe->iorid[i]);
43876424Simp			rle->res = r;
43990897Simp			spaces++;
44067187Simp		}
44167187Simp		if (cfe->num_memspace > 0) {
44282781Sshiba			/*
44382781Sshiba			 * Not implement yet, Fix me.
44482781Sshiba			 */
44590897Simp			DEVPRINTF((bus, "Memory space not yet implemented.\n"));
44667187Simp		}
44790897Simp		if (spaces == 0) {
44890897Simp			DEVPRINTF((bus, "Neither memory nor I/O mampped\n"));
44990897Simp			goto not_this_one;
45090897Simp		}
45167187Simp		if (cfe->irqmask) {
45267187Simp			cfe->irqrid = 0;
45367399Simp			r = cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ,
45467424Simp			    &cfe->irqrid, 0, ~0, 1, 0);
45576424Simp			if (cfe->irqres == NULL)
45667187Simp				goto not_this_one;
45767242Simp			resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid,
45867242Simp			    rman_get_start(r), rman_get_end(r), 1);
45976424Simp			rle = resource_list_find(rl, SYS_RES_IRQ,
46076424Simp			    cfe->irqrid);
46176424Simp			rle->res = r;
46267187Simp		}
46367187Simp		/* If we get to here, we've allocated all we need */
46467167Simp		pf->cfe = cfe;
46567187Simp		break;
46667187Simp	    not_this_one:;
46767424Simp		DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
46867424Simp		    cfe->number));
46967333Simp		/*
47067333Simp		 * Release resources that we partially allocated
47167333Simp		 * from this config entry.
47267333Simp		 */
47367187Simp		for (i = 0; i < cfe->num_iospace; i++) {
47476424Simp			if (cfe->iores[i] != NULL) {
47570715Sjon				bus_release_resource(bus, SYS_RES_IOPORT,
47667187Simp				    cfe->iorid[i], cfe->iores[i]);
47782378Sjon				rle = resource_list_find(rl, SYS_RES_IOPORT,
47876424Simp				    cfe->iorid[i]);
47976424Simp				rle->res = NULL;
48076424Simp				resource_list_delete(rl, SYS_RES_IOPORT,
48176424Simp				    cfe->iorid[i]);
48275756Simp			}
48367187Simp			cfe->iores[i] = NULL;
48467187Simp		}
48576424Simp		if (cfe->irqmask && cfe->irqres != NULL) {
48667242Simp			bus_release_resource(bus, SYS_RES_IRQ,
48767187Simp			    cfe->irqrid, cfe->irqres);
48876424Simp			rle = resource_list_find(rl, SYS_RES_IRQ,
48976424Simp			    cfe->irqrid);
49076424Simp			rle->res = NULL;
49176424Simp			resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid);
49267187Simp			cfe->irqres = NULL;
49367187Simp		}
49467167Simp	}
49552506Simp}
49652506Simp
49782378Sjon/*
49882378Sjon * Free resources allocated by pccard_function_init(), May be called as long
49982378Sjon * as the function is disabled.
50082382Simp *
50182382Simp * NOTE: This function should be unnecessary.  pccard_function_init should
50282382Simp * never keep resources initialized.
50382378Sjon */
50482378Sjonstatic void
50582378Sjonpccard_function_free(struct pccard_function *pf)
50682378Sjon{
50782378Sjon	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
50882378Sjon	struct resource_list_entry *rle;
50982378Sjon
51082378Sjon	if (pf->pf_flags & PFF_ENABLED) {
51182378Sjon		printf("pccard_function_init: function is enabled");
51282378Sjon		return;
51382378Sjon	}
51482378Sjon
51582378Sjon	SLIST_FOREACH(rle, &devi->resources, link) {
51682378Sjon		if (rle->res) {
517113242Simp			if (rman_get_device(rle->res) != pf->sc->dev)
51882378Sjon				device_printf(pf->sc->dev,
51982378Sjon				    "function_free: Resource still owned by "
52082378Sjon				    "child, oops. "
52182378Sjon				    "(type=%d, rid=%d, addr=%lx)\n",
52282378Sjon				    rle->type, rle->rid,
52382378Sjon				    rman_get_start(rle->res));
52482378Sjon			BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
525113242Simp			    pf->sc->dev, rle->type, rle->rid, rle->res);
52682378Sjon			rle->res = NULL;
52782378Sjon		}
52882378Sjon	}
52982378Sjon	resource_list_free(&devi->resources);
53082378Sjon}
53182378Sjon
532121905Simpstatic void
533121905Simppccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr,
534121905Simp    bus_addr_t offset, bus_size_t size)
535121905Simp{
536121958Simp	bus_size_t iosize, tmp;
537121905Simp
538121905Simp	if (addr != 0) {
539121905Simp		if (pf->pf_mfc_iomax == 0) {
540121905Simp			pf->pf_mfc_iobase = addr + offset;
541121905Simp			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
542121905Simp		} else {
543121905Simp			/* this makes the assumption that nothing overlaps */
544121905Simp			if (pf->pf_mfc_iobase > addr + offset)
545121905Simp				pf->pf_mfc_iobase = addr + offset;
546121905Simp			if (pf->pf_mfc_iomax < addr + offset + size)
547121905Simp				pf->pf_mfc_iomax = addr + offset + size;
548121905Simp		}
549121905Simp	}
550121905Simp
551121905Simp	tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
552121905Simp	/* round up to nearest (2^n)-1 */
553121905Simp	for (iosize = 1; iosize < tmp; iosize <<= 1)
554121905Simp		;
555121905Simp	iosize--;
556121905Simp
557121958Simp	DEVPRINTF((pf->dev, "MFC: I/O base 0x%jx IOSIZE %jd\n",
558121958Simp	  (uintmax_t) pf->pf_mfc_iobase, (uintmax_t) iosize));
559121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
560121905Simp	    pf->pf_mfc_iobase & 0xff);
561121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
562121905Simp	    (pf->pf_mfc_iobase >> 8) & 0xff);
563121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
564121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
565121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
566121905Simp}
567121905Simp
56852506Simp/* Enable a PCCARD function */
56982378Sjonstatic int
57055720Simppccard_function_enable(struct pccard_function *pf)
57152506Simp{
57252506Simp	struct pccard_function *tmp;
57352506Simp	int reg;
57455720Simp	device_t dev = pf->sc->dev;
57570746Simp
57667333Simp	if (pf->cfe == NULL) {
57767333Simp		DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
57874632Simp		return (ENOMEM);
57967333Simp	}
58052506Simp
58152506Simp	/*
58252506Simp	 * Increase the reference count on the socket, enabling power, if
58352506Simp	 * necessary.
58452506Simp	 */
58582378Sjon	pf->sc->sc_enabled_count++;
58652506Simp
58752506Simp	if (pf->pf_flags & PFF_ENABLED) {
58852506Simp		/*
58952506Simp		 * Don't do anything if we're already enabled.
59052506Simp		 */
59152506Simp		return (0);
59252506Simp	}
59352506Simp
59452506Simp	/*
59552506Simp	 * it's possible for different functions' CCRs to be in the same
59652506Simp	 * underlying page.  Check for that.
59752506Simp	 */
59852506Simp	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
59952506Simp		if ((tmp->pf_flags & PFF_ENABLED) &&
60052506Simp		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
60152506Simp		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
60282378Sjon		    (tmp->ccr_base - tmp->pf_ccr_offset +
60382378Sjon		    tmp->pf_ccr_realsize))) {
60452506Simp			pf->pf_ccrt = tmp->pf_ccrt;
60552506Simp			pf->pf_ccrh = tmp->pf_ccrh;
60652506Simp			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
60752506Simp
60852506Simp			/*
60952506Simp			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
61052506Simp			 * tmp->ccr_base) + pf->ccr_base;
61152506Simp			 */
61270715Sjon			/* pf->pf_ccr_offset =
61352506Simp			    (tmp->pf_ccr_offset + pf->ccr_base) -
61470715Sjon			    tmp->ccr_base; */
61552506Simp			pf->pf_ccr_window = tmp->pf_ccr_window;
61652506Simp			break;
61752506Simp		}
61852506Simp	}
61952506Simp	if (tmp == NULL) {
62055720Simp		pf->ccr_rid = 0;
62155720Simp		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
62270715Sjon		    &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE);
62370715Sjon		if (!pf->ccr_res)
62452506Simp			goto bad;
625106914Smux		DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n",
62670746Simp		    rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
62770746Simp		    pf->ccr_base));
62861788Simp		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
62961788Simp		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
63070715Sjon		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
63170748Simp		    pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
63255720Simp		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
63355720Simp		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
63455720Simp		pf->pf_ccr_realsize = 1;
63552506Simp	}
63652506Simp
63752506Simp	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
63852506Simp	reg |= PCCARD_CCR_OPTION_LEVIREQ;
63952506Simp	if (pccard_mfc(pf->sc)) {
64052506Simp		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
64152506Simp			PCCARD_CCR_OPTION_ADDR_DECODE);
64282383Simp		/* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
64352506Simp	}
64452506Simp	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
64552506Simp
64652506Simp	reg = 0;
64752506Simp	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
64852506Simp		reg |= PCCARD_CCR_STATUS_IOIS8;
64952506Simp	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
65052506Simp		reg |= PCCARD_CCR_STATUS_AUDIO;
65152506Simp	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
65252506Simp
65352506Simp	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
65452506Simp
655121905Simp	if (pccard_mfc(pf->sc))
656121905Simp		pccard_mfc_adjust_iobase(pf, 0, 0, 0);
65752506Simp
65852506Simp#ifdef PCCARDDEBUG
65952506Simp	if (pccard_debug) {
66052506Simp		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
66170715Sjon			device_printf(tmp->sc->dev,
66255500Simp			    "function %d CCR at %d offset %x: "
66355500Simp			    "%x %x %x %x, %x %x %x %x, %x\n",
66470715Sjon			    tmp->number, tmp->pf_ccr_window,
66555500Simp			    tmp->pf_ccr_offset,
66655500Simp			    pccard_ccr_read(tmp, 0x00),
66755500Simp			    pccard_ccr_read(tmp, 0x02),
66855500Simp			    pccard_ccr_read(tmp, 0x04),
66955500Simp			    pccard_ccr_read(tmp, 0x06),
67055500Simp			    pccard_ccr_read(tmp, 0x0A),
67170715Sjon			    pccard_ccr_read(tmp, 0x0C),
67255500Simp			    pccard_ccr_read(tmp, 0x0E),
67355500Simp			    pccard_ccr_read(tmp, 0x10),
67455500Simp			    pccard_ccr_read(tmp, 0x12));
67552506Simp		}
67652506Simp	}
67752506Simp#endif
67852506Simp	pf->pf_flags |= PFF_ENABLED;
67952506Simp	return (0);
68052506Simp
68152506Simp bad:
68252506Simp	/*
68352506Simp	 * Decrement the reference count, and power down the socket, if
68452506Simp	 * necessary.
68552506Simp	 */
68682378Sjon	pf->sc->sc_enabled_count--;
68765098Simp	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
68852506Simp
68952506Simp	return (1);
69052506Simp}
69152506Simp
69252506Simp/* Disable PCCARD function. */
69382378Sjonstatic void
69455720Simppccard_function_disable(struct pccard_function *pf)
69552506Simp{
69652506Simp	struct pccard_function *tmp;
69755720Simp	device_t dev = pf->sc->dev;
69852506Simp
69952506Simp	if (pf->cfe == NULL)
70061788Simp		panic("pccard_function_disable: function not initialized");
70152506Simp
70252506Simp	if ((pf->pf_flags & PFF_ENABLED) == 0) {
70352506Simp		/*
70452506Simp		 * Don't do anything if we're already disabled.
70552506Simp		 */
70652506Simp		return;
70752506Simp	}
70852506Simp
70970715Sjon	if (pf->intr_handler != NULL) {
71082378Sjon		struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
71182378Sjon		struct resource_list_entry *rle =
71282378Sjon		    resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
71382378Sjon		BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
71482378Sjon		    pf->intr_handler_cookie);
71570715Sjon	}
71670715Sjon
71752506Simp	/*
71852506Simp	 * it's possible for different functions' CCRs to be in the same
71952506Simp	 * underlying page.  Check for that.  Note we mark us as disabled
72052506Simp	 * first to avoid matching ourself.
72152506Simp	 */
72252506Simp
72352506Simp	pf->pf_flags &= ~PFF_ENABLED;
72452506Simp	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
72552506Simp		if ((tmp->pf_flags & PFF_ENABLED) &&
72652506Simp		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
72752506Simp		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
72882378Sjon		    (tmp->ccr_base - tmp->pf_ccr_offset +
72982378Sjon		    tmp->pf_ccr_realsize)))
73052506Simp			break;
73152506Simp	}
73252506Simp
73352506Simp	/* Not used by anyone else; unmap the CCR. */
73452506Simp	if (tmp == NULL) {
73570715Sjon		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
73655720Simp		    pf->ccr_res);
73755720Simp		pf->ccr_res = NULL;
73852506Simp	}
73952506Simp
74052506Simp	/*
74152506Simp	 * Decrement the reference count, and power down the socket, if
74252506Simp	 * necessary.
74352506Simp	 */
74482378Sjon	pf->sc->sc_enabled_count--;
74552506Simp}
74652506Simp
74766058Simp/*
74866058Simp * simulate the old "probe" routine.  In the new world order, the driver
74966058Simp * needs to grab devices while in the old they were assigned to the device by
75066058Simp * the pccardd process.  These symbols are exported to the upper layers.
75166058Simp */
75274636Simpstatic int
75374636Simppccard_compat_do_probe(device_t bus, device_t dev)
75466058Simp{
75566058Simp	return (CARD_COMPAT_MATCH(dev));
75666058Simp}
75766058Simp
75874636Simpstatic int
75974636Simppccard_compat_do_attach(device_t bus, device_t dev)
76066058Simp{
76166058Simp	int err;
76266058Simp
76366058Simp	err = CARD_COMPAT_PROBE(dev);
764119462Simp	if (err <= 0)
76566058Simp		err = CARD_COMPAT_ATTACH(dev);
76666058Simp	return (err);
76766058Simp}
76866058Simp
76953873Simp#define PCCARD_NPORT	2
77053873Simp#define PCCARD_NMEM	5
77153873Simp#define PCCARD_NIRQ	1
77253873Simp#define PCCARD_NDRQ	0
77353873Simp
77452506Simpstatic int
77552506Simppccard_add_children(device_t dev, int busno)
77652506Simp{
77759193Simp	/* Call parent to scan for any current children */
77874632Simp	return (0);
77952506Simp}
78052506Simp
78152506Simpstatic int
78252506Simppccard_probe(device_t dev)
78352506Simp{
78467333Simp	device_set_desc(dev, "16-bit PCCard bus");
78574632Simp	return (pccard_add_children(dev, device_get_unit(dev)));
78652506Simp}
78752506Simp
78859193Simpstatic int
78959193Simppccard_attach(device_t dev)
79059193Simp{
79164850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
79261788Simp
79359193Simp	sc->dev = dev;
79461788Simp	sc->sc_enabled_count = 0;
79574632Simp	return (bus_generic_attach(dev));
79659193Simp}
79759193Simp
79882378Sjonstatic int
79982378Sjonpccard_detach(device_t dev)
80082378Sjon{
801106362Simp	pccard_detach_card(dev);
80282378Sjon	return 0;
80382378Sjon}
80482378Sjon
80587975Simpstatic int
80687975Simppccard_suspend(device_t self)
80787975Simp{
808106362Simp	pccard_detach_card(self);
80987975Simp	return (0);
81087975Simp}
81187975Simp
81287975Simpstatic
81387975Simpint
81487975Simppccard_resume(device_t self)
81587975Simp{
81687975Simp	return (0);
81787975Simp}
81887975Simp
81953873Simpstatic void
82053873Simppccard_print_resources(struct resource_list *rl, const char *name, int type,
82153873Simp    int count, const char *format)
82253873Simp{
82353873Simp	struct resource_list_entry *rle;
82453873Simp	int printed;
82553873Simp	int i;
82653873Simp
82753873Simp	printed = 0;
82853873Simp	for (i = 0; i < count; i++) {
82953873Simp		rle = resource_list_find(rl, type, i);
83076424Simp		if (rle != NULL) {
83153873Simp			if (printed == 0)
83253873Simp				printf(" %s ", name);
83353873Simp			else if (printed > 0)
83453873Simp				printf(",");
83553873Simp			printed++;
83653873Simp			printf(format, rle->start);
83753873Simp			if (rle->count > 1) {
83853873Simp				printf("-");
83953873Simp				printf(format, rle->start + rle->count - 1);
84053873Simp			}
84153873Simp		} else if (i > 3) {
84253873Simp			/* check the first few regardless */
84353873Simp			break;
84453873Simp		}
84553873Simp	}
84653873Simp}
84753873Simp
84853873Simpstatic int
84953873Simppccard_print_child(device_t dev, device_t child)
85053873Simp{
85166847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
85253873Simp	struct resource_list *rl = &devi->resources;
85353873Simp	int retval = 0;
85453873Simp
85553873Simp	retval += bus_print_child_header(dev, child);
85653873Simp	retval += printf(" at");
85753873Simp
85876424Simp	if (devi != NULL) {
85953873Simp		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
86053873Simp		    PCCARD_NPORT, "%#lx");
86153873Simp		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
86253873Simp		    PCCARD_NMEM, "%#lx");
86353873Simp		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
86453873Simp		    "%ld");
86570715Sjon		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
86653873Simp		    "%ld");
86767269Simp		retval += printf(" function %d config %d", devi->fcn->number,
86867269Simp		    devi->fcn->cfe->number);
86953873Simp	}
87053873Simp
87153873Simp	retval += bus_print_child_footer(dev, child);
87253873Simp
87353873Simp	return (retval);
87453873Simp}
87553873Simp
87653873Simpstatic int
87753873Simppccard_set_resource(device_t dev, device_t child, int type, int rid,
87853873Simp		 u_long start, u_long count)
87953873Simp{
88066847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
88153873Simp	struct resource_list *rl = &devi->resources;
88253873Simp
88353873Simp	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
88453873Simp	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
88574632Simp		return (EINVAL);
88653873Simp	if (rid < 0)
88774632Simp		return (EINVAL);
88853873Simp	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
88974632Simp		return (EINVAL);
89053873Simp	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
89174632Simp		return (EINVAL);
89253873Simp	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
89374632Simp		return (EINVAL);
89453873Simp	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
89574632Simp		return (EINVAL);
89653873Simp
89753873Simp	resource_list_add(rl, type, rid, start, start + count - 1, count);
89882378Sjon	if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
89982378Sjon	    type, &rid, start, start + count - 1, count, 0))
90082378Sjon		return 0;
90182378Sjon	else
90282378Sjon		return ENOMEM;
90353873Simp}
90453873Simp
90553873Simpstatic int
90653873Simppccard_get_resource(device_t dev, device_t child, int type, int rid,
90753873Simp    u_long *startp, u_long *countp)
90853873Simp{
90966847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
91053873Simp	struct resource_list *rl = &devi->resources;
91153873Simp	struct resource_list_entry *rle;
91253873Simp
91353873Simp	rle = resource_list_find(rl, type, rid);
91476424Simp	if (rle == NULL)
91574632Simp		return (ENOENT);
91670715Sjon
91776424Simp	if (startp != NULL)
91853873Simp		*startp = rle->start;
91976424Simp	if (countp != NULL)
92053873Simp		*countp = rle->count;
92153873Simp
92274632Simp	return (0);
92353873Simp}
92453873Simp
92553873Simpstatic void
92653873Simppccard_delete_resource(device_t dev, device_t child, int type, int rid)
92753873Simp{
92866847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
92953873Simp	struct resource_list *rl = &devi->resources;
93053873Simp	resource_list_delete(rl, type, rid);
93153873Simp}
93253873Simp
93359193Simpstatic int
93459193Simppccard_set_res_flags(device_t dev, device_t child, int type, int rid,
93559193Simp    u_int32_t flags)
93659193Simp{
93774632Simp	return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
93874632Simp	    rid, flags));
93959193Simp}
94059193Simp
94159193Simpstatic int
94259193Simppccard_set_memory_offset(device_t dev, device_t child, int rid,
94382378Sjon    u_int32_t offset, u_int32_t *deltap)
94470715Sjon
94559193Simp{
94674632Simp	return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
94774632Simp	    offset, deltap));
94859193Simp}
94959193Simp
950104641Simpstatic void
951104641Simppccard_probe_nomatch(device_t bus, device_t child)
952104641Simp{
953104641Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
954104641Simp	struct pccard_function *func = devi->fcn;
955104641Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
956104641Simp
957104641Simp	device_printf(bus, "<unknown card>");
958104641Simp	printf(" (manufacturer=0x%04x, product=0x%04x) at function %d\n",
959104641Simp	  sc->card.manufacturer, sc->card.product, func->number);
960104641Simp	device_printf(bus, "   CIS info: %s, %s, %s\n", sc->card.cis1_info[0],
961104641Simp	  sc->card.cis1_info[1], sc->card.cis1_info[2]);
962104641Simp	return;
963104641Simp}
964104641Simp
96566058Simpstatic int
966104641Simppccard_child_location_str(device_t bus, device_t child, char *buf,
967104641Simp    size_t buflen)
968104641Simp{
969104641Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
970104641Simp	struct pccard_function *func = devi->fcn;
971104641Simp
972104641Simp	snprintf(buf, buflen, "function=%d", func->number);
973104641Simp	return (0);
974104641Simp}
975104641Simp
976104641Simpstatic int
977104641Simppccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
978104641Simp    size_t buflen)
979104641Simp{
980104641Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
981104641Simp	struct pccard_function *func = devi->fcn;
982104641Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
983104641Simp
984104641Simp	snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
985104641Simp	    "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
986104641Simp	    sc->card.manufacturer, sc->card.product, sc->card.cis1_info[0],
987104641Simp	    sc->card.cis1_info[1], func->function);
988104641Simp	return (0);
989104641Simp}
990104641Simp
991104641Simpstatic int
99266058Simppccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
99366058Simp{
99466847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
99566779Simp	struct pccard_function *func = devi->fcn;
99666779Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
99766779Simp
99866779Simp	switch (which) {
99966779Simp	default:
100066779Simp	case PCCARD_IVAR_ETHADDR:
100182781Sshiba		bcopy(func->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
100266779Simp		break;
100366779Simp	case PCCARD_IVAR_VENDOR:
100466779Simp		*(u_int32_t *) result = sc->card.manufacturer;
100566779Simp		break;
100666779Simp	case PCCARD_IVAR_PRODUCT:
100766779Simp		*(u_int32_t *) result = sc->card.product;
100866779Simp		break;
100990964Sshiba	case PCCARD_IVAR_PRODEXT:
101090964Sshiba		*(u_int16_t *) result = sc->card.prodext;
101190964Sshiba		break;
101275761Simp	case PCCARD_IVAR_FUNCTION:
101375761Simp		*(u_int32_t *) result = func->function;
101475761Simp		break;
101566779Simp	case PCCARD_IVAR_FUNCTION_NUMBER:
101666847Simp		if (!func) {
101766847Simp			device_printf(bus, "No function number, bug!\n");
101866847Simp			return (ENOENT);
101966847Simp		}
102066847Simp		*(u_int32_t *) result = func->number;
102166779Simp		break;
102266779Simp	case PCCARD_IVAR_VENDOR_STR:
102366779Simp		*(char **) result = sc->card.cis1_info[0];
102466779Simp		break;
102566779Simp	case PCCARD_IVAR_PRODUCT_STR:
102666779Simp		*(char **) result = sc->card.cis1_info[1];
102766779Simp		break;
102866779Simp	case PCCARD_IVAR_CIS3_STR:
102966779Simp		*(char **) result = sc->card.cis1_info[2];
103066779Simp		break;
103167167Simp	case PCCARD_IVAR_CIS4_STR:
1032104610Simp		*(char **) result = sc->card.cis1_info[3];
103367167Simp		break;
103466779Simp	}
103566779Simp	return (0);
103666058Simp}
103766058Simp
103866779Simpstatic void
103966779Simppccard_driver_added(device_t dev, driver_t *driver)
104066779Simp{
104182378Sjon	struct pccard_softc *sc = PCCARD_SOFTC(dev);
104282378Sjon	struct pccard_function *pf;
104382378Sjon	device_t child;
104482378Sjon
104582378Sjon	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
104682378Sjon		if (STAILQ_EMPTY(&pf->cfe_head))
104782378Sjon			continue;
104882378Sjon		child = pf->dev;
104982378Sjon		if (device_get_state(child) != DS_NOTPRESENT)
105082378Sjon			continue;
105182378Sjon		if (pccard_function_enable(pf) == 0 &&
105282378Sjon		    device_probe_and_attach(child) == 0) {
105382378Sjon			DEVPRINTF((sc->dev, "function %d CCR at %d "
105482378Sjon			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
105582378Sjon			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
105682378Sjon			    pccard_ccr_read(pf, 0x00),
105782378Sjon			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
105882378Sjon			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
105982378Sjon			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
106082378Sjon			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
106182378Sjon		} else {
106286907Simp			if (pf->cfe != NULL)
106386907Simp				pccard_function_disable(pf);
106482378Sjon		}
106582378Sjon	}
106682378Sjon	return;
106766779Simp}
106866058Simp
106967242Simpstatic struct resource *
107067242Simppccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
107167242Simp    u_long start, u_long end, u_long count, u_int flags)
107267242Simp{
107382378Sjon	struct pccard_ivar *dinfo;
107482378Sjon	struct resource_list_entry *rle = 0;
107582378Sjon	int passthrough = (device_get_parent(child) != dev);
1076121905Simp	int isdefault = (start == 0 && end == ~0UL && count == 1);
1077104641Simp	struct resource *r = NULL;
107867242Simp
1079121905Simp	/* XXX I'm no longer sure this is right */
108082378Sjon	if (passthrough) {
108182378Sjon		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
108282378Sjon		    type, rid, start, end, count, flags));
108382378Sjon	}
108470715Sjon
108582378Sjon	dinfo = device_get_ivars(child);
108682378Sjon	rle = resource_list_find(&dinfo->resources, type, *rid);
108770715Sjon
1088121905Simp	if (rle == NULL && isdefault)
1089121905Simp		return (NULL);	/* no resource of that type/rid */
1090121905Simp	if (rle == NULL || rle->res == NULL) {
1091121905Simp		/* Do we want this device to own it? */
1092121905Simp		/* XXX I think so, but that might be lame XXX */
1093121905Simp		r = bus_alloc_resource(dev, type, rid, start, end,
1094121905Simp		  count, flags /* XXX aligment? */);
1095121905Simp		if (r == NULL)
1096121905Simp		    goto bad;
1097121905Simp		resource_list_add(&dinfo->resources, type, *rid,
1098121905Simp		  rman_get_start(r), rman_get_end(r), count);
1099121905Simp		rle = resource_list_find(&dinfo->resources, type, *rid);
1100121905Simp		if (!rle)
1101121905Simp		    goto bad;
1102121905Simp		rle->res = r;
110367269Simp	}
1104121905Simp	/*
1105121905Simp	 * XXX the following looks wrong, in theory, but likely it is
1106121905Simp	 * XXX needed because of how the CIS code allocates resources
1107121905Simp	 * XXX for this device.
1108121905Simp	 */
1109113242Simp	if (rman_get_device(rle->res) != dev)
1110104641Simp		return (NULL);
1111104641Simp	bus_release_resource(dev, type, *rid, rle->res);
1112104641Simp	rle->res = NULL;
1113104641Simp	switch(type) {
1114104641Simp	case SYS_RES_IOPORT:
1115104641Simp	case SYS_RES_MEMORY:
1116104641Simp		if (!(flags & RF_ALIGNMENT_MASK))
1117104641Simp			flags |= rman_make_alignment_flags(rle->count);
1118104641Simp		break;
1119104641Simp	case SYS_RES_IRQ:
1120104641Simp		flags |= RF_SHAREABLE;
1121104641Simp		break;
1122104641Simp	}
1123104641Simp	rle->res = resource_list_alloc(&dinfo->resources, dev, child,
1124104641Simp	    type, rid, rle->start, rle->end, rle->count, flags);
1125104641Simp	return (rle->res);
1126104641Simpbad:;
1127104641Simp	device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1128104641Simp	return (NULL);
112967242Simp}
113067242Simp
113167242Simpstatic int
113267242Simppccard_release_resource(device_t dev, device_t child, int type, int rid,
113367242Simp    struct resource *r)
113467242Simp{
113582378Sjon	struct pccard_ivar *dinfo;
113682378Sjon	int passthrough = (device_get_parent(child) != dev);
113782378Sjon	struct resource_list_entry *rle = 0;
113882378Sjon	int ret;
113982378Sjon	int flags;
114070715Sjon
114182378Sjon	if (passthrough)
114282378Sjon		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
114382378Sjon		    type, rid, r);
114470715Sjon
114582378Sjon	dinfo = device_get_ivars(child);
114670715Sjon
114782378Sjon	rle = resource_list_find(&dinfo->resources, type, rid);
114870715Sjon
114982378Sjon	if (!rle) {
115082378Sjon		device_printf(dev, "Allocated resource not found, "
115182378Sjon		    "%d %x %lx %lx\n",
115282378Sjon		    type, rid, rman_get_start(r), rman_get_size(r));
115382378Sjon		return ENOENT;
115470715Sjon	}
115582378Sjon	if (!rle->res) {
115682378Sjon		device_printf(dev, "Allocated resource not recorded\n");
115782378Sjon		return ENOENT;
115870715Sjon	}
115970715Sjon
116082378Sjon	ret = BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
116182378Sjon	    type, rid, r);
116282378Sjon	switch(type) {
116382378Sjon	case SYS_RES_IOPORT:
116482378Sjon	case SYS_RES_MEMORY:
116582378Sjon		flags = rman_make_alignment_flags(rle->count);
116682378Sjon		break;
116782378Sjon	case SYS_RES_IRQ:
116882378Sjon		flags = RF_SHAREABLE;
116982378Sjon		break;
117082378Sjon	default:
117182378Sjon		flags = 0;
117270715Sjon	}
117382378Sjon	rle->res = bus_alloc_resource(dev, type, &rid,
117482378Sjon	    rle->start, rle->end, rle->count, flags);
117582378Sjon	if (rle->res == NULL)
117682378Sjon		device_printf(dev, "release_resource: "
117782378Sjon		    "unable to reaquire resource\n");
117882378Sjon	return ret;
117967242Simp}
118067242Simp
118167333Simpstatic void
118267333Simppccard_child_detached(device_t parent, device_t dev)
118367333Simp{
118467333Simp	struct pccard_ivar *ivar = PCCARD_IVAR(dev);
118582378Sjon	struct pccard_function *pf = ivar->fcn;
118667333Simp
118782378Sjon	pccard_function_disable(pf);
118867333Simp}
118967333Simp
119070715Sjonstatic void
119170762Simppccard_intr(void *arg)
119270762Simp{
119382378Sjon	struct pccard_function *pf = (struct pccard_function*) arg;
119482378Sjon	int reg;
1195102923Simp	int doisr = 1;
119682378Sjon
119782383Simp	/*
1198102923Simp	 * MFC cards know if they interrupted, so we have to ack the
1199102923Simp	 * interrupt and call the ISR.  Non-MFC cards don't have these
1200102923Simp	 * bits, so they always get called.  Many non-MFC cards have
1201102923Simp	 * this bit set always upon read, but some do not.
1202102923Simp	 *
1203102923Simp	 * We always ack the interrupt, even if there's no ISR
1204102923Simp	 * for the card.  This is done on the theory that acking
1205102923Simp	 * the interrupt will pacify the card enough to keep an
1206102923Simp	 * interrupt storm from happening.  Of course this won't
1207102923Simp	 * help in the non-MFC case.
1208116311Simp	 *
1209116311Simp	 * This has no impact for MPSAFEness of the client drivers.
1210116311Simp	 * We register this with whatever flags the intr_handler
1211116311Simp	 * was registered with.  All these functions are MPSAFE.
121282383Simp	 */
1213102923Simp	if (pccard_mfc(pf->sc)) {
1214102923Simp		reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1215102923Simp		if (reg & PCCARD_CCR_STATUS_INTR)
1216102923Simp			pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1217102923Simp			    reg & ~PCCARD_CCR_STATUS_INTR);
1218102923Simp		else
1219102923Simp			doisr = 0;
1220102923Simp	}
1221102923Simp	if (pf->intr_handler != NULL && doisr)
122282378Sjon		pf->intr_handler(pf->intr_handler_arg);
122370715Sjon}
122470715Sjon
122570715Sjonstatic int
122670762Simppccard_setup_intr(device_t dev, device_t child, struct resource *irq,
122770762Simp    int flags, driver_intr_t *intr, void *arg, void **cookiep)
122870715Sjon{
1229102713Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
123070715Sjon	struct pccard_ivar *ivar = PCCARD_IVAR(child);
123170715Sjon	struct pccard_function *func = ivar->fcn;
123290445Simp	int err;
123370715Sjon
123470715Sjon	if (func->intr_handler != NULL)
1235101762Simp		panic("Only one interrupt handler per function allowed");
123690445Simp	err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr,
123790445Simp	    func, cookiep);
123890445Simp	if (err != 0)
123990445Simp		return (err);
124070715Sjon	func->intr_handler = intr;
124170715Sjon	func->intr_handler_arg = arg;
124282378Sjon	func->intr_handler_cookie = *cookiep;
1243102713Simp	if (pccard_mfc(sc)) {
1244102713Simp		pccard_ccr_write(func, PCCARD_CCR_OPTION,
1245102713Simp		    pccard_ccr_read(func, PCCARD_CCR_OPTION) |
1246102713Simp		    PCCARD_CCR_OPTION_IREQ_ENABLE);
1247102713Simp	}
124874632Simp	return (0);
124970715Sjon}
125070715Sjon
125170715Sjonstatic int
125270762Simppccard_teardown_intr(device_t dev, device_t child, struct resource *r,
125370762Simp    void *cookie)
125470715Sjon{
1255102713Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
125670715Sjon	struct pccard_ivar *ivar = PCCARD_IVAR(child);
125770715Sjon	struct pccard_function *func = ivar->fcn;
125882378Sjon	int ret;
125970715Sjon
1260102713Simp	if (pccard_mfc(sc)) {
1261102713Simp		pccard_ccr_write(func, PCCARD_CCR_OPTION,
1262102713Simp		    pccard_ccr_read(func, PCCARD_CCR_OPTION) &
1263102713Simp		    ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1264102713Simp	}
126590445Simp	ret = bus_generic_teardown_intr(dev, child, r, cookie);
126682378Sjon	if (ret == 0) {
126782378Sjon		func->intr_handler = NULL;
126882378Sjon		func->intr_handler_arg = NULL;
126982378Sjon		func->intr_handler_cookie = NULL;
127082378Sjon	}
127170715Sjon
127282378Sjon	return (ret);
127370715Sjon}
127470715Sjon
1275121905Simpstatic int
1276121905Simppccard_activate_resource(device_t brdev, device_t child, int type, int rid,
1277121905Simp    struct resource *r)
1278121905Simp{
1279121905Simp	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1280121905Simp	struct pccard_function *pf = ivar->fcn;
1281121905Simp
1282121905Simp	switch(type) {
1283121905Simp	case SYS_RES_IOPORT:
1284121905Simp		/*
1285121905Simp		 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC
1286121905Simp		 * card.
1287121905Simp		 */
1288121905Simp		if (pccard_mfc(pf->sc))
1289121905Simp			pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0,
1290121905Simp			    rman_get_size(r));
1291121905Simp		break;
1292121905Simp	default:
1293121905Simp		break;
1294121905Simp	}
1295121905Simp	return (bus_generic_activate_resource(brdev, child, type, rid, r));
1296121905Simp}
1297121905Simp
1298121905Simpstatic int
1299121905Simppccard_deactivate_resource(device_t brdev, device_t child, int type,
1300121905Simp    int rid, struct resource *r)
1301121905Simp{
1302121905Simp	/* XXX undo pccard_activate_resource? XXX */
1303121905Simp	return (bus_generic_deactivate_resource(brdev, child, type, rid, r));
1304121905Simp}
1305121905Simp
130652506Simpstatic device_method_t pccard_methods[] = {
130752506Simp	/* Device interface */
130852506Simp	DEVMETHOD(device_probe,		pccard_probe),
130959193Simp	DEVMETHOD(device_attach,	pccard_attach),
131082378Sjon	DEVMETHOD(device_detach,	pccard_detach),
131152506Simp	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
131287975Simp	DEVMETHOD(device_suspend,	pccard_suspend),
131387975Simp	DEVMETHOD(device_resume,	pccard_resume),
131452506Simp
131552506Simp	/* Bus interface */
131652506Simp	DEVMETHOD(bus_print_child,	pccard_print_child),
131766779Simp	DEVMETHOD(bus_driver_added,	pccard_driver_added),
131867333Simp	DEVMETHOD(bus_child_detached,	pccard_child_detached),
131967242Simp	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
132067242Simp	DEVMETHOD(bus_release_resource,	pccard_release_resource),
1321121905Simp	DEVMETHOD(bus_activate_resource, pccard_activate_resource),
1322121905Simp	DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
132370715Sjon	DEVMETHOD(bus_setup_intr,	pccard_setup_intr),
132470715Sjon	DEVMETHOD(bus_teardown_intr,	pccard_teardown_intr),
132552506Simp	DEVMETHOD(bus_set_resource,	pccard_set_resource),
132652506Simp	DEVMETHOD(bus_get_resource,	pccard_get_resource),
132752506Simp	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
1328104641Simp	DEVMETHOD(bus_probe_nomatch,	pccard_probe_nomatch),
132966058Simp	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
1330104641Simp	DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1331104641Simp	DEVMETHOD(bus_child_location_str, pccard_child_location_str),
133252506Simp
133359193Simp	/* Card Interface */
133459193Simp	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
133559193Simp	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
133659193Simp	DEVMETHOD(card_attach_card,	pccard_attach_card),
133759193Simp	DEVMETHOD(card_detach_card,	pccard_detach_card),
133874636Simp	DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe),
133974636Simp	DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach),
134097613Stakawata	DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
134159193Simp
134252506Simp	{ 0, 0 }
134352506Simp};
134452506Simp
134552506Simpstatic driver_t pccard_driver = {
134652506Simp	"pccard",
134752506Simp	pccard_methods,
134864850Simp	sizeof(struct pccard_softc)
134952506Simp};
135052506Simp
135152506Simpdevclass_t	pccard_devclass;
135252506Simp
1353101905Simp/* Maybe we need to have a slot device? */
135453873SimpDRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1355101905SimpDRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
135664927SimpMODULE_VERSION(pccard, 1);
1357