pccard.c revision 82378
1/*	$NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $	*/
2/* $FreeBSD: head/sys/dev/pccard/pccard.c 82378 2001-08-27 00:09:42Z jon $ */
3
4/*
5 * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Marc Horowitz.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/malloc.h>
36#include <sys/module.h>
37#include <sys/kernel.h>
38#include <sys/queue.h>
39#include <sys/types.h>
40
41#include <sys/bus.h>
42#include <machine/bus.h>
43#include <sys/rman.h>
44#include <machine/resource.h>
45
46#include <dev/pccard/pccardreg.h>
47#include <dev/pccard/pccardvar.h>
48
49#include "power_if.h"
50#include "card_if.h"
51
52#define PCCARDDEBUG
53
54#ifdef PCCARDDEBUG
55int	pccard_debug = 1;
56#define	DPRINTF(arg) if (pccard_debug) printf arg
57#define	DEVPRINTF(arg) if (pccard_debug) device_printf arg
58#define PRVERBOSE(arg) printf arg
59#define DEVPRVERBOSE(arg) device_printf arg
60#else
61#define	DPRINTF(arg)
62#define	DEVPRINTF(arg)
63#define PRVERBOSE(arg) if (bootverbose) printf arg
64#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg
65#endif
66
67#ifdef PCCARDVERBOSE
68int	pccard_verbose = 1;
69#else
70int	pccard_verbose = 0;
71#endif
72
73static int	pccard_ccr_read(struct pccard_function *pf, int ccr);
74static void	pccard_ccr_write(struct pccard_function *pf, int ccr, int val);
75static int	pccard_attach_card(device_t dev);
76static int	pccard_detach_card(device_t dev, int flags);
77static const struct pccard_product *pccard_product_lookup(device_t dev,
78		    const struct pccard_product *tab, size_t ent_size,
79		    pccard_product_match_fn matchfn);
80static int	pccard_card_gettype(device_t dev, int *type);
81static void	pccard_function_init(struct pccard_function *pf);
82static void	pccard_function_free(struct pccard_function *pf);
83static int	pccard_function_enable(struct pccard_function *pf);
84static void	pccard_function_disable(struct pccard_function *pf);
85static int	pccard_compat_do_probe(device_t bus, device_t dev);
86static int	pccard_compat_do_attach(device_t bus, device_t dev);
87static int	pccard_add_children(device_t dev, int busno);
88static int	pccard_probe(device_t dev);
89static int	pccard_attach(device_t dev);
90static int	pccard_detach(device_t dev);
91static void	pccard_print_resources(struct resource_list *rl,
92		    const char *name, int type, int count, const char *format);
93static int	pccard_print_child(device_t dev, device_t child);
94static int	pccard_set_resource(device_t dev, device_t child, int type,
95		    int rid, u_long start, u_long count);
96static int	pccard_get_resource(device_t dev, device_t child, int type,
97		    int rid, u_long *startp, u_long *countp);
98static void	pccard_delete_resource(device_t dev, device_t child, int type,
99		    int rid);
100static int	pccard_set_res_flags(device_t dev, device_t child, int type,
101		    int rid, u_int32_t flags);
102static int	pccard_set_memory_offset(device_t dev, device_t child, int rid,
103		    u_int32_t offset, u_int32_t *deltap);
104static int	pccard_read_ivar(device_t bus, device_t child, int which,
105		    u_char *result);
106static void	pccard_driver_added(device_t dev, driver_t *driver);
107static struct resource *pccard_alloc_resource(device_t dev,
108		    device_t child, int type, int *rid, u_long start,
109		    u_long end, u_long count, u_int flags);
110static int	pccard_release_resource(device_t dev, device_t child, int type,
111		    int rid, struct resource *r);
112static void	pccard_child_detached(device_t parent, device_t dev);
113static void	pccard_intr(void *arg);
114static int	pccard_setup_intr(device_t dev, device_t child,
115		    struct resource *irq, int flags, driver_intr_t *intr,
116		    void *arg, void **cookiep);
117static int	pccard_teardown_intr(device_t dev, device_t child,
118		    struct resource *r, void *cookie);
119
120static int
121pccard_ccr_read(struct pccard_function *pf, int ccr)
122{
123	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
124	    pf->pf_ccr_offset + ccr));
125}
126
127static void
128pccard_ccr_write(struct pccard_function *pf, int ccr, int val)
129{
130	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
131		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
132		    pf->pf_ccr_offset + ccr, val);
133	}
134}
135
136static int
137pccard_attach_card(device_t dev)
138{
139	struct pccard_softc *sc = PCCARD_SOFTC(dev);
140	struct pccard_function *pf;
141	struct pccard_ivar *ivar;
142	device_t child;
143
144	/*
145	 * this is here so that when socket_enable calls gettype, trt happens
146	 */
147	STAILQ_INIT(&sc->card.pf_head);
148
149	DEVPRINTF((dev, "chip_socket_enable\n"));
150	POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
151
152	DEVPRINTF((dev, "read_cis\n"));
153	pccard_read_cis(sc);
154
155	DEVPRINTF((dev, "check_cis_quirks\n"));
156	pccard_check_cis_quirks(dev);
157
158	/*
159	 * bail now if the card has no functions, or if there was an error in
160	 * the cis.
161	 */
162
163	if (sc->card.error) {
164		device_printf (dev, "CARD ERROR!\n");
165		return (1);
166	}
167	if (STAILQ_EMPTY(&sc->card.pf_head)) {
168		device_printf (dev, "Card has no functions!\n");
169		return (1);
170	}
171
172	if (1)
173		pccard_print_cis(dev);
174
175	DEVPRINTF((dev, "functions scanning\n"));
176	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
177		if (STAILQ_EMPTY(&pf->cfe_head))
178			continue;
179
180		pf->sc = sc;
181		pf->cfe = NULL;
182		pf->dev = NULL;
183	}
184
185	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
186		if (STAILQ_EMPTY(&pf->cfe_head))
187			continue;
188		/*
189		 * In NetBSD, the drivers are responsible for activating
190		 * each function of a card.  I think that in FreeBSD we
191		 * want to activate them enough for the usual bus_*_resource
192		 * routines will do the right thing.  This many mean a
193		 * departure from the current NetBSD model.
194		 *
195		 * This could get really ugly for multifunction cards.  But
196		 * it might also just fall out of the FreeBSD resource model.
197		 *
198		 */
199		ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF,
200		    M_WAITOK | M_ZERO);
201		child = device_add_child(dev, NULL, -1);
202		device_set_ivars(child, ivar);
203		ivar->fcn = pf;
204		pf->dev = child;
205		/*
206		 * XXX We might want to move the next two lines into
207		 * XXX the pccard interface layer.  For the moment, this
208		 * XXX is OK, but some drivers want to pick the config
209		 * XXX entry to use as well as some address tweaks (mostly
210		 * XXX due to bugs in decode logic that makes some
211		 * XXX addresses illegal or broken).
212		 */
213		pccard_function_init(pf);
214		if (sc->sc_enabled_count == 0)
215			POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
216		if (pccard_function_enable(pf) == 0 &&
217		    device_probe_and_attach(child) == 0) {
218			DEVPRINTF((sc->dev, "function %d CCR at %d "
219			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
220			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
221			    pccard_ccr_read(pf, 0x00),
222			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
223			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
224			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
225			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
226		} else {
227			pccard_function_disable(pf);
228		}
229	}
230	if (sc->sc_enabled_count == 0)
231		pccard_detach_card(dev, 0);
232	return (0);
233}
234
235static int
236pccard_detach_card(device_t dev, int flags)
237{
238	struct pccard_softc *sc = PCCARD_SOFTC(dev);
239	struct pccard_function *pf;
240	struct pccard_config_entry *cfe;
241
242	/*
243	 * We are running on either the PCCARD socket's event thread
244	 * or in user context detaching a device by user request.
245	 */
246	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
247		int state = device_get_state(pf->dev);
248
249		if (state == DS_ATTACHED || state == DS_BUSY)
250			device_detach(pf->dev);
251		pccard_function_disable(pf);
252		pccard_function_free(pf);
253		if (pf->dev != NULL)
254			device_delete_child(dev, pf->dev);
255	}
256	if (sc->sc_enabled_count == 0)
257		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
258
259	while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
260		while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
261			STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
262			free(cfe, M_DEVBUF);
263		}
264		STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
265		free(pf, M_DEVBUF);
266	}
267	return (0);
268}
269
270static const struct pccard_product *
271pccard_product_lookup(device_t dev, const struct pccard_product *tab,
272    size_t ent_size, pccard_product_match_fn matchfn)
273{
274	const struct pccard_product *ent;
275	int matches;
276	u_int32_t fcn;
277	u_int32_t vendor;
278	u_int32_t prod;
279	char *vendorstr;
280	char *prodstr;
281
282#ifdef DIAGNOSTIC
283	if (sizeof *ent > ent_size)
284		panic("pccard_product_lookup: bogus ent_size %ld",
285		    (long) ent_size);
286#endif
287	if (pccard_get_vendor(dev, &vendor))
288		return (NULL);
289	if (pccard_get_product(dev, &prod))
290		return (NULL);
291	if (pccard_get_function_number(dev, &fcn))
292		return (NULL);
293	if (pccard_get_vendor_str(dev, &vendorstr))
294		return (NULL);
295	if (pccard_get_product_str(dev, &prodstr))
296		return (NULL);
297	for (ent = tab; ent->pp_name != NULL; ent =
298	    (const struct pccard_product *) ((const char *) ent + ent_size)) {
299		matches = 1;
300		if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
301		    vendor != ent->pp_vendor)
302			matches = 0;
303		if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
304		    prod != ent->pp_product)
305			matches = 0;
306		if (matches && fcn != ent->pp_expfunc)
307			matches = 0;
308		if (matches && ent->pp_cis[0] &&
309		    strcmp(ent->pp_cis[0], vendorstr) != 0)
310			matches = 0;
311		if (matches && ent->pp_cis[1] &&
312		    strcmp(ent->pp_cis[1], prodstr) != 0)
313			matches = 0;
314		/* XXX need to match cis[2] and cis[3] also XXX */
315		if (matchfn != NULL)
316			matches = (*matchfn)(dev, ent, matches);
317		if (matches)
318			return (ent);
319	}
320	return (NULL);
321}
322
323static int
324pccard_card_gettype(device_t dev, int *type)
325{
326	struct pccard_softc *sc = PCCARD_SOFTC(dev);
327	struct pccard_function *pf;
328
329	/*
330	 * set the iftype to memory if this card has no functions (not yet
331	 * probed), or only one function, and that is not initialized yet or
332	 * that is memory.
333	 */
334	pf = STAILQ_FIRST(&sc->card.pf_head);
335	if (pf == NULL ||
336	    (STAILQ_NEXT(pf, pf_list) == NULL &&
337	    (pf->cfe == NULL || pf->cfe->iftype == PCCARD_IFTYPE_MEMORY)))
338		*type = PCCARD_IFTYPE_MEMORY;
339	else
340		*type = PCCARD_IFTYPE_IO;
341	return (0);
342}
343
344/*
345 * Initialize a PCCARD function.  May be called as long as the function is
346 * disabled.
347 */
348static void
349pccard_function_init(struct pccard_function *pf)
350{
351	struct pccard_config_entry *cfe;
352	int i;
353	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
354	struct resource_list *rl = &devi->resources;
355	struct resource_list_entry *rle;
356	struct resource *r = 0;
357	device_t bus;
358	int start;
359	int end;
360
361	if (pf->pf_flags & PFF_ENABLED) {
362		printf("pccard_function_init: function is enabled");
363		return;
364	}
365	bus = device_get_parent(pf->dev);
366	/* Remember which configuration entry we are using. */
367	STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
368		for (i = 0; i < cfe->num_iospace; i++)
369			cfe->iores[i] = NULL;
370		cfe->irqres = NULL;
371		for (i = 0; i < cfe->num_iospace; i++) {
372			start = cfe->iospace[i].start;
373			if (start)
374				end = start + cfe->iospace[i].length - 1;
375			else
376				end = ~0;
377			cfe->iorid[i] = i;
378			r = cfe->iores[i] = bus_alloc_resource(bus,
379			    SYS_RES_IOPORT, &cfe->iorid[i], start, end,
380			    cfe->iospace[i].length,
381			    rman_make_alignment_flags(cfe->iospace[i].length));
382			if (cfe->iores[i] == NULL)
383				goto not_this_one;
384			resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i],
385			    rman_get_start(r), rman_get_end(r),
386			    cfe->iospace[i].length);
387			rle = resource_list_find(rl, SYS_RES_IOPORT,
388			    cfe->iorid[i]);
389			rle->res = r;
390		}
391		if (cfe->num_memspace > 0) {
392			goto not_this_one;
393		}
394		if (cfe->irqmask) {
395			cfe->irqrid = 0;
396			r = cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ,
397			    &cfe->irqrid, 0, ~0, 1, 0);
398			if (cfe->irqres == NULL)
399				goto not_this_one;
400			resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid,
401			    rman_get_start(r), rman_get_end(r), 1);
402			rle = resource_list_find(rl, SYS_RES_IRQ,
403			    cfe->irqrid);
404			rle->res = r;
405		}
406		/* If we get to here, we've allocated all we need */
407		pf->cfe = cfe;
408		break;
409	    not_this_one:;
410		DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
411		    cfe->number));
412		/*
413		 * Release resources that we partially allocated
414		 * from this config entry.
415		 */
416		for (i = 0; i < cfe->num_iospace; i++) {
417			if (cfe->iores[i] != NULL) {
418				bus_release_resource(bus, SYS_RES_IOPORT,
419				    cfe->iorid[i], cfe->iores[i]);
420				rle = resource_list_find(rl, SYS_RES_IOPORT,
421				    cfe->iorid[i]);
422				rle->res = NULL;
423				resource_list_delete(rl, SYS_RES_IOPORT,
424				    cfe->iorid[i]);
425			}
426			cfe->iores[i] = NULL;
427		}
428		if (cfe->irqmask && cfe->irqres != NULL) {
429			bus_release_resource(bus, SYS_RES_IRQ,
430			    cfe->irqrid, cfe->irqres);
431			rle = resource_list_find(rl, SYS_RES_IRQ,
432			    cfe->irqrid);
433			rle->res = NULL;
434			resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid);
435			cfe->irqres = NULL;
436		}
437	}
438}
439
440/*
441 * Free resources allocated by pccard_function_init(), May be called as long
442 * as the function is disabled.
443 */
444static void
445pccard_function_free(struct pccard_function *pf)
446{
447	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
448	struct resource_list_entry *rle;
449
450	if (pf->pf_flags & PFF_ENABLED) {
451		printf("pccard_function_init: function is enabled");
452		return;
453	}
454
455	SLIST_FOREACH(rle, &devi->resources, link) {
456		if (rle->res) {
457			if (rle->res->r_dev != pf->sc->dev)
458				device_printf(pf->sc->dev,
459				    "function_free: Resource still owned by "
460				    "child, oops. "
461				    "(type=%d, rid=%d, addr=%lx)\n",
462				    rle->type, rle->rid,
463				    rman_get_start(rle->res));
464			BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
465			    rle->res->r_dev, rle->type, rle->rid, rle->res);
466			rle->res = NULL;
467		}
468	}
469	resource_list_free(&devi->resources);
470}
471
472/* Enable a PCCARD function */
473static int
474pccard_function_enable(struct pccard_function *pf)
475{
476	struct pccard_function *tmp;
477	int reg;
478	device_t dev = pf->sc->dev;
479
480	if (pf->cfe == NULL) {
481		DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
482		return (ENOMEM);
483	}
484
485	/*
486	 * Increase the reference count on the socket, enabling power, if
487	 * necessary.
488	 */
489	pf->sc->sc_enabled_count++;
490
491	if (pf->pf_flags & PFF_ENABLED) {
492		/*
493		 * Don't do anything if we're already enabled.
494		 */
495		return (0);
496	}
497
498	/*
499	 * it's possible for different functions' CCRs to be in the same
500	 * underlying page.  Check for that.
501	 */
502	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
503		if ((tmp->pf_flags & PFF_ENABLED) &&
504		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
505		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
506		    (tmp->ccr_base - tmp->pf_ccr_offset +
507		    tmp->pf_ccr_realsize))) {
508			pf->pf_ccrt = tmp->pf_ccrt;
509			pf->pf_ccrh = tmp->pf_ccrh;
510			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
511
512			/*
513			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
514			 * tmp->ccr_base) + pf->ccr_base;
515			 */
516			/* pf->pf_ccr_offset =
517			    (tmp->pf_ccr_offset + pf->ccr_base) -
518			    tmp->ccr_base; */
519			pf->pf_ccr_window = tmp->pf_ccr_window;
520			break;
521		}
522	}
523	if (tmp == NULL) {
524		pf->ccr_rid = 0;
525		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
526		    &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE);
527		if (!pf->ccr_res)
528			goto bad;
529		DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%lx\n",
530		    rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
531		    pf->ccr_base));
532		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
533		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
534		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
535		    pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
536		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
537		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
538		pf->pf_ccr_realsize = 1;
539	}
540
541	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
542	reg |= PCCARD_CCR_OPTION_LEVIREQ;
543	if (pccard_mfc(pf->sc)) {
544		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
545			PCCARD_CCR_OPTION_ADDR_DECODE);
546		/*
547		 * XXX Need to enable PCCARD_CCR_OPTION_IRQ_ENABLE if
548		 * XXX we have an interrupt handler, but we don't know that
549		 * XXX at this point.
550		 */
551/*		reg |= PCCARD_CCR_OPTION_IREQ_ENABLE;*/
552	}
553	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
554
555	reg = 0;
556	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
557		reg |= PCCARD_CCR_STATUS_IOIS8;
558	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
559		reg |= PCCARD_CCR_STATUS_AUDIO;
560	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
561
562	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
563
564	if (pccard_mfc(pf->sc)) {
565		long tmp, iosize;
566
567		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
568		/* round up to nearest (2^n)-1 */
569		for (iosize = 1; iosize < tmp; iosize <<= 1)
570			;
571		iosize--;
572
573		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
574				 pf->pf_mfc_iobase & 0xff);
575		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
576				 (pf->pf_mfc_iobase >> 8) & 0xff);
577		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
578		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
579
580		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
581	}
582
583#ifdef PCCARDDEBUG
584	if (pccard_debug) {
585		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
586			device_printf(tmp->sc->dev,
587			    "function %d CCR at %d offset %x: "
588			    "%x %x %x %x, %x %x %x %x, %x\n",
589			    tmp->number, tmp->pf_ccr_window,
590			    tmp->pf_ccr_offset,
591			    pccard_ccr_read(tmp, 0x00),
592			    pccard_ccr_read(tmp, 0x02),
593			    pccard_ccr_read(tmp, 0x04),
594			    pccard_ccr_read(tmp, 0x06),
595			    pccard_ccr_read(tmp, 0x0A),
596			    pccard_ccr_read(tmp, 0x0C),
597			    pccard_ccr_read(tmp, 0x0E),
598			    pccard_ccr_read(tmp, 0x10),
599			    pccard_ccr_read(tmp, 0x12));
600		}
601	}
602#endif
603	pf->pf_flags |= PFF_ENABLED;
604	return (0);
605
606 bad:
607	/*
608	 * Decrement the reference count, and power down the socket, if
609	 * necessary.
610	 */
611	pf->sc->sc_enabled_count--;
612	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
613
614	return (1);
615}
616
617/* Disable PCCARD function. */
618static void
619pccard_function_disable(struct pccard_function *pf)
620{
621	struct pccard_function *tmp;
622	device_t dev = pf->sc->dev;
623
624	if (pf->cfe == NULL)
625		panic("pccard_function_disable: function not initialized");
626
627	if ((pf->pf_flags & PFF_ENABLED) == 0) {
628		/*
629		 * Don't do anything if we're already disabled.
630		 */
631		return;
632	}
633
634	if (pf->intr_handler != NULL) {
635		struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
636		struct resource_list_entry *rle =
637		    resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
638		BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
639		    pf->intr_handler_cookie);
640	}
641
642	/*
643	 * it's possible for different functions' CCRs to be in the same
644	 * underlying page.  Check for that.  Note we mark us as disabled
645	 * first to avoid matching ourself.
646	 */
647
648	pf->pf_flags &= ~PFF_ENABLED;
649	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
650		if ((tmp->pf_flags & PFF_ENABLED) &&
651		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
652		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
653		    (tmp->ccr_base - tmp->pf_ccr_offset +
654		    tmp->pf_ccr_realsize)))
655			break;
656	}
657
658	/* Not used by anyone else; unmap the CCR. */
659	if (tmp == NULL) {
660		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
661		    pf->ccr_res);
662		pf->ccr_res = NULL;
663	}
664
665	/*
666	 * Decrement the reference count, and power down the socket, if
667	 * necessary.
668	 */
669	pf->sc->sc_enabled_count--;
670}
671
672#if 0
673/* XXX These functions are needed, but not like this XXX */
674int
675pccard_io_map(struct pccard_function *pf, int width, bus_addr_t offset,
676    bus_size_t size, struct pccard_io_handle *pcihp, int *windowp)
677{
678	int reg;
679
680	if (pccard_chip_io_map(pf->sc->pct, pf->sc->pch, width, offset, size,
681	    pcihp, windowp))
682		return (1);
683
684	/*
685	 * XXX in the multifunction multi-iospace-per-function case, this
686	 * needs to cooperate with io_alloc to make sure that the spaces
687	 * don't overlap, and that the ccr's are set correctly
688	 */
689
690	if (pccard_mfc(pf->sc)) {
691		long tmp, iosize;
692
693		if (pf->pf_mfc_iomax == 0) {
694			pf->pf_mfc_iobase = pcihp->addr + offset;
695			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
696		} else {
697			/* this makes the assumption that nothing overlaps */
698			if (pf->pf_mfc_iobase > pcihp->addr + offset)
699				pf->pf_mfc_iobase = pcihp->addr + offset;
700			if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
701				pf->pf_mfc_iomax = pcihp->addr + offset + size;
702		}
703
704		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
705		/* round up to nearest (2^n)-1 */
706		for (iosize = 1; iosize >= tmp; iosize <<= 1)
707			;
708		iosize--;
709
710		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
711		    pf->pf_mfc_iobase & 0xff);
712		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
713		    (pf->pf_mfc_iobase >> 8) & 0xff);
714		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
715		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
716
717		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
718
719		reg = pccard_ccr_read(pf, PCCARD_CCR_OPTION);
720		reg |= PCCARD_CCR_OPTION_ADDR_DECODE;
721		pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
722	}
723	return (0);
724}
725
726void
727pccard_io_unmap(struct pccard_function *pf, int window)
728{
729
730	pccard_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
731
732	/* XXX Anything for multi-function cards? */
733}
734#endif
735
736/*
737 * simulate the old "probe" routine.  In the new world order, the driver
738 * needs to grab devices while in the old they were assigned to the device by
739 * the pccardd process.  These symbols are exported to the upper layers.
740 */
741static int
742pccard_compat_do_probe(device_t bus, device_t dev)
743{
744	return (CARD_COMPAT_MATCH(dev));
745}
746
747static int
748pccard_compat_do_attach(device_t bus, device_t dev)
749{
750	int err;
751
752	err = CARD_COMPAT_PROBE(dev);
753	if (err == 0)
754		err = CARD_COMPAT_ATTACH(dev);
755	return (err);
756}
757
758#define PCCARD_NPORT	2
759#define PCCARD_NMEM	5
760#define PCCARD_NIRQ	1
761#define PCCARD_NDRQ	0
762
763static int
764pccard_add_children(device_t dev, int busno)
765{
766	/* Call parent to scan for any current children */
767	return (0);
768}
769
770static int
771pccard_probe(device_t dev)
772{
773	device_set_desc(dev, "16-bit PCCard bus");
774	return (pccard_add_children(dev, device_get_unit(dev)));
775}
776
777static int
778pccard_attach(device_t dev)
779{
780	struct pccard_softc *sc = PCCARD_SOFTC(dev);
781
782	sc->dev = dev;
783	sc->sc_enabled_count = 0;
784	return (bus_generic_attach(dev));
785}
786
787static int
788pccard_detach(device_t dev)
789{
790	pccard_detach_card(dev, 0);
791	return 0;
792}
793
794static void
795pccard_print_resources(struct resource_list *rl, const char *name, int type,
796    int count, const char *format)
797{
798	struct resource_list_entry *rle;
799	int printed;
800	int i;
801
802	printed = 0;
803	for (i = 0; i < count; i++) {
804		rle = resource_list_find(rl, type, i);
805		if (rle != NULL) {
806			if (printed == 0)
807				printf(" %s ", name);
808			else if (printed > 0)
809				printf(",");
810			printed++;
811			printf(format, rle->start);
812			if (rle->count > 1) {
813				printf("-");
814				printf(format, rle->start + rle->count - 1);
815			}
816		} else if (i > 3) {
817			/* check the first few regardless */
818			break;
819		}
820	}
821}
822
823static int
824pccard_print_child(device_t dev, device_t child)
825{
826	struct pccard_ivar *devi = PCCARD_IVAR(child);
827	struct resource_list *rl = &devi->resources;
828	int retval = 0;
829
830	retval += bus_print_child_header(dev, child);
831	retval += printf(" at");
832
833	if (devi != NULL) {
834		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
835		    PCCARD_NPORT, "%#lx");
836		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
837		    PCCARD_NMEM, "%#lx");
838		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
839		    "%ld");
840		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
841		    "%ld");
842		retval += printf(" function %d config %d", devi->fcn->number,
843		    devi->fcn->cfe->number);
844	}
845
846	retval += bus_print_child_footer(dev, child);
847
848	return (retval);
849}
850
851static int
852pccard_set_resource(device_t dev, device_t child, int type, int rid,
853		 u_long start, u_long count)
854{
855	struct pccard_ivar *devi = PCCARD_IVAR(child);
856	struct resource_list *rl = &devi->resources;
857
858	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
859	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
860		return (EINVAL);
861	if (rid < 0)
862		return (EINVAL);
863	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
864		return (EINVAL);
865	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
866		return (EINVAL);
867	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
868		return (EINVAL);
869	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
870		return (EINVAL);
871
872	resource_list_add(rl, type, rid, start, start + count - 1, count);
873	if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
874	    type, &rid, start, start + count - 1, count, 0))
875		return 0;
876	else
877		return ENOMEM;
878}
879
880static int
881pccard_get_resource(device_t dev, device_t child, int type, int rid,
882    u_long *startp, u_long *countp)
883{
884	struct pccard_ivar *devi = PCCARD_IVAR(child);
885	struct resource_list *rl = &devi->resources;
886	struct resource_list_entry *rle;
887
888	rle = resource_list_find(rl, type, rid);
889	if (rle == NULL)
890		return (ENOENT);
891
892	if (startp != NULL)
893		*startp = rle->start;
894	if (countp != NULL)
895		*countp = rle->count;
896
897	return (0);
898}
899
900static void
901pccard_delete_resource(device_t dev, device_t child, int type, int rid)
902{
903	struct pccard_ivar *devi = PCCARD_IVAR(child);
904	struct resource_list *rl = &devi->resources;
905	resource_list_delete(rl, type, rid);
906}
907
908static int
909pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
910    u_int32_t flags)
911{
912	return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
913	    rid, flags));
914}
915
916static int
917pccard_set_memory_offset(device_t dev, device_t child, int rid,
918    u_int32_t offset, u_int32_t *deltap)
919
920{
921	return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
922	    offset, deltap));
923}
924
925static int
926pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
927{
928	struct pccard_ivar *devi = PCCARD_IVAR(child);
929	struct pccard_function *func = devi->fcn;
930	struct pccard_softc *sc = PCCARD_SOFTC(bus);
931
932	/* PCCARD_IVAR_ETHADDR unhandled from oldcard */
933	switch (which) {
934	default:
935	case PCCARD_IVAR_ETHADDR:
936		return (ENOENT);
937		break;
938	case PCCARD_IVAR_VENDOR:
939		*(u_int32_t *) result = sc->card.manufacturer;
940		break;
941	case PCCARD_IVAR_PRODUCT:
942		*(u_int32_t *) result = sc->card.product;
943		break;
944	case PCCARD_IVAR_FUNCTION:
945		*(u_int32_t *) result = func->function;
946		break;
947	case PCCARD_IVAR_FUNCTION_NUMBER:
948		if (!func) {
949			device_printf(bus, "No function number, bug!\n");
950			return (ENOENT);
951		}
952		*(u_int32_t *) result = func->number;
953		break;
954	case PCCARD_IVAR_VENDOR_STR:
955		*(char **) result = sc->card.cis1_info[0];
956		break;
957	case PCCARD_IVAR_PRODUCT_STR:
958		*(char **) result = sc->card.cis1_info[1];
959		break;
960	case PCCARD_IVAR_CIS3_STR:
961		*(char **) result = sc->card.cis1_info[2];
962		break;
963	case PCCARD_IVAR_CIS4_STR:
964		*(char **) result = sc->card.cis1_info[2];
965		break;
966	}
967	return (0);
968}
969
970static void
971pccard_driver_added(device_t dev, driver_t *driver)
972{
973	struct pccard_softc *sc = PCCARD_SOFTC(dev);
974	struct pccard_function *pf;
975	device_t child;
976
977	if (sc->sc_enabled_count == 0) {
978		CARD_REPROBE_CARD(device_get_parent(dev), dev);
979		return;
980	}
981
982	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
983		if (STAILQ_EMPTY(&pf->cfe_head))
984			continue;
985		child = pf->dev;
986		if (device_get_state(child) != DS_NOTPRESENT)
987			continue;
988		if (pccard_function_enable(pf) == 0 &&
989		    device_probe_and_attach(child) == 0) {
990			DEVPRINTF((sc->dev, "function %d CCR at %d "
991			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
992			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
993			    pccard_ccr_read(pf, 0x00),
994			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
995			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
996			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
997			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
998		} else {
999			pccard_function_disable(pf);
1000		}
1001	}
1002	return;
1003}
1004
1005static struct resource *
1006pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
1007    u_long start, u_long end, u_long count, u_int flags)
1008{
1009	struct pccard_ivar *dinfo;
1010	struct resource_list_entry *rle = 0;
1011	int passthrough = (device_get_parent(child) != dev);
1012
1013	if (passthrough) {
1014		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
1015		    type, rid, start, end, count, flags));
1016	}
1017
1018	dinfo = device_get_ivars(child);
1019	rle = resource_list_find(&dinfo->resources, type, *rid);
1020
1021	if (!rle)
1022		return NULL;		/* no resource of that type/rid */
1023
1024	if (!rle->res) {
1025		device_printf(dev, "WARNING: Resource not reserved by pccard bus\n");
1026		return NULL;
1027	} else {
1028		if (rle->res->r_dev != dev) return NULL;
1029		bus_release_resource(dev, type, *rid, rle->res);
1030		rle->res = NULL;
1031		switch(type) {
1032		case SYS_RES_IOPORT:
1033		case SYS_RES_MEMORY:
1034			if (!(flags & RF_ALIGNMENT_MASK))
1035				flags |= rman_make_alignment_flags(rle->count);
1036			break;
1037		case SYS_RES_IRQ:
1038			flags |= RF_SHAREABLE;
1039			break;
1040		}
1041		return resource_list_alloc(&dinfo->resources, dev, child, type,
1042		    rid, rle->start, rle->end, rle->count, flags);
1043	}
1044}
1045
1046static int
1047pccard_release_resource(device_t dev, device_t child, int type, int rid,
1048    struct resource *r)
1049{
1050	struct pccard_ivar *dinfo;
1051	int passthrough = (device_get_parent(child) != dev);
1052	struct resource_list_entry *rle = 0;
1053	int ret;
1054	int flags;
1055
1056	if (passthrough)
1057		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1058		    type, rid, r);
1059
1060	dinfo = device_get_ivars(child);
1061
1062	rle = resource_list_find(&dinfo->resources, type, rid);
1063
1064	if (!rle) {
1065		device_printf(dev, "Allocated resource not found, "
1066		    "%d %x %lx %lx\n",
1067		    type, rid, rman_get_start(r), rman_get_size(r));
1068		return ENOENT;
1069	}
1070	if (!rle->res) {
1071		device_printf(dev, "Allocated resource not recorded\n");
1072		return ENOENT;
1073	}
1074
1075	ret = BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1076	    type, rid, r);
1077	switch(type) {
1078	case SYS_RES_IOPORT:
1079	case SYS_RES_MEMORY:
1080		flags = rman_make_alignment_flags(rle->count);
1081		break;
1082	case SYS_RES_IRQ:
1083		flags = RF_SHAREABLE;
1084		break;
1085	default:
1086		flags = 0;
1087	}
1088	rle->res = bus_alloc_resource(dev, type, &rid,
1089	    rle->start, rle->end, rle->count, flags);
1090	if (rle->res == NULL)
1091		device_printf(dev, "release_resource: "
1092		    "unable to reaquire resource\n");
1093	return ret;
1094}
1095
1096static void
1097pccard_child_detached(device_t parent, device_t dev)
1098{
1099	struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1100	struct pccard_function *pf = ivar->fcn;
1101
1102	pccard_function_disable(pf);
1103}
1104
1105static void
1106pccard_intr(void *arg)
1107{
1108	struct pccard_function *pf = (struct pccard_function*) arg;
1109	int reg;
1110
1111	if (pf->intr_handler == NULL)
1112		panic("Null interrupt handler?\n");
1113
1114	reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1115	if (reg & PCCARD_CCR_STATUS_INTR) {
1116		pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1117				 reg & ~PCCARD_CCR_STATUS_INTR);
1118		pf->intr_handler(pf->intr_handler_arg);
1119	}
1120}
1121
1122static int
1123pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1124    int flags, driver_intr_t *intr, void *arg, void **cookiep)
1125{
1126	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1127	struct pccard_function *func = ivar->fcn;
1128
1129	if (func->intr_handler != NULL)
1130		panic("Only one interrupt handler per function allowed\n");
1131
1132	func->intr_handler = intr;
1133	func->intr_handler_arg = arg;
1134	func->intr_handler_cookie = *cookiep;
1135	pccard_ccr_write(func, PCCARD_CCR_OPTION,
1136	    pccard_ccr_read(func, PCCARD_CCR_OPTION) |
1137	    PCCARD_CCR_OPTION_IREQ_ENABLE);
1138
1139	bus_setup_intr(dev, irq, INTR_TYPE_TTY/* | INTR_FAST*/,
1140	    pccard_intr, func, cookiep);
1141	return (0);
1142}
1143
1144static int
1145pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
1146    void *cookie)
1147{
1148	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1149	struct pccard_function *func = ivar->fcn;
1150	int ret;
1151
1152	pccard_ccr_write(func, PCCARD_CCR_OPTION,
1153	    pccard_ccr_read(func, PCCARD_CCR_OPTION) &
1154	    ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1155
1156	ret = bus_teardown_intr(dev, r, cookie);
1157	if (ret == 0) {
1158		func->intr_handler = NULL;
1159		func->intr_handler_arg = NULL;
1160		func->intr_handler_cookie = NULL;
1161	}
1162
1163	return (ret);
1164}
1165
1166static device_method_t pccard_methods[] = {
1167	/* Device interface */
1168	DEVMETHOD(device_probe,		pccard_probe),
1169	DEVMETHOD(device_attach,	pccard_attach),
1170	DEVMETHOD(device_detach,	pccard_detach),
1171	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1172	DEVMETHOD(device_suspend,	bus_generic_suspend),
1173	DEVMETHOD(device_resume,	bus_generic_resume),
1174
1175	/* Bus interface */
1176	DEVMETHOD(bus_print_child,	pccard_print_child),
1177	DEVMETHOD(bus_driver_added,	pccard_driver_added),
1178	DEVMETHOD(bus_child_detached,	pccard_child_detached),
1179	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
1180	DEVMETHOD(bus_release_resource,	pccard_release_resource),
1181	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1182	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1183	DEVMETHOD(bus_setup_intr,	pccard_setup_intr),
1184	DEVMETHOD(bus_teardown_intr,	pccard_teardown_intr),
1185	DEVMETHOD(bus_set_resource,	pccard_set_resource),
1186	DEVMETHOD(bus_get_resource,	pccard_get_resource),
1187	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
1188	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
1189
1190	/* Card Interface */
1191	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
1192	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
1193	DEVMETHOD(card_get_type,	pccard_card_gettype),
1194	DEVMETHOD(card_attach_card,	pccard_attach_card),
1195	DEVMETHOD(card_detach_card,	pccard_detach_card),
1196	DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe),
1197	DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach),
1198
1199	{ 0, 0 }
1200};
1201
1202static driver_t pccard_driver = {
1203	"pccard",
1204	pccard_methods,
1205	sizeof(struct pccard_softc)
1206};
1207
1208devclass_t	pccard_devclass;
1209
1210DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1211DRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0);
1212DRIVER_MODULE(pccard, pccbb, pccard_driver, pccard_devclass, 0, 0);
1213DRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0);
1214MODULE_VERSION(pccard, 1);
1215/*MODULE_DEPEND(pccard, pcic, 1, 1, 1);*/
1216