pccard.c revision 82382
1/*	$NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $	*/
2/* $FreeBSD: head/sys/dev/pccard/pccard.c 82382 2001-08-27 01:18:21Z imp $ */
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 *
348 * Note: pccard_function_init should not keep resources allocated.  It should
349 * only set them up ala isa pnp, set the values in the rl lists, and return.
350 * Any resource held after pccard_function_init is called is a bug.  However,
351 * the bus routines to get the resources also assume that pccard_function_init
352 * does this, so they need to be fixed too.
353 */
354static void
355pccard_function_init(struct pccard_function *pf)
356{
357	struct pccard_config_entry *cfe;
358	int i;
359	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
360	struct resource_list *rl = &devi->resources;
361	struct resource_list_entry *rle;
362	struct resource *r = 0;
363	device_t bus;
364	int start;
365	int end;
366
367	if (pf->pf_flags & PFF_ENABLED) {
368		printf("pccard_function_init: function is enabled");
369		return;
370	}
371	bus = device_get_parent(pf->dev);
372	/* Remember which configuration entry we are using. */
373	STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
374		for (i = 0; i < cfe->num_iospace; i++)
375			cfe->iores[i] = NULL;
376		cfe->irqres = NULL;
377		for (i = 0; i < cfe->num_iospace; i++) {
378			start = cfe->iospace[i].start;
379			if (start)
380				end = start + cfe->iospace[i].length - 1;
381			else
382				end = ~0;
383			cfe->iorid[i] = i;
384			r = cfe->iores[i] = bus_alloc_resource(bus,
385			    SYS_RES_IOPORT, &cfe->iorid[i], start, end,
386			    cfe->iospace[i].length,
387			    rman_make_alignment_flags(cfe->iospace[i].length));
388			if (cfe->iores[i] == NULL)
389				goto not_this_one;
390			resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i],
391			    rman_get_start(r), rman_get_end(r),
392			    cfe->iospace[i].length);
393			rle = resource_list_find(rl, SYS_RES_IOPORT,
394			    cfe->iorid[i]);
395			rle->res = r;
396		}
397		if (cfe->num_memspace > 0) {
398			goto not_this_one;
399		}
400		if (cfe->irqmask) {
401			cfe->irqrid = 0;
402			r = cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ,
403			    &cfe->irqrid, 0, ~0, 1, 0);
404			if (cfe->irqres == NULL)
405				goto not_this_one;
406			resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid,
407			    rman_get_start(r), rman_get_end(r), 1);
408			rle = resource_list_find(rl, SYS_RES_IRQ,
409			    cfe->irqrid);
410			rle->res = r;
411		}
412		/* If we get to here, we've allocated all we need */
413		pf->cfe = cfe;
414		break;
415	    not_this_one:;
416		DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
417		    cfe->number));
418		/*
419		 * Release resources that we partially allocated
420		 * from this config entry.
421		 */
422		for (i = 0; i < cfe->num_iospace; i++) {
423			if (cfe->iores[i] != NULL) {
424				bus_release_resource(bus, SYS_RES_IOPORT,
425				    cfe->iorid[i], cfe->iores[i]);
426				rle = resource_list_find(rl, SYS_RES_IOPORT,
427				    cfe->iorid[i]);
428				rle->res = NULL;
429				resource_list_delete(rl, SYS_RES_IOPORT,
430				    cfe->iorid[i]);
431			}
432			cfe->iores[i] = NULL;
433		}
434		if (cfe->irqmask && cfe->irqres != NULL) {
435			bus_release_resource(bus, SYS_RES_IRQ,
436			    cfe->irqrid, cfe->irqres);
437			rle = resource_list_find(rl, SYS_RES_IRQ,
438			    cfe->irqrid);
439			rle->res = NULL;
440			resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid);
441			cfe->irqres = NULL;
442		}
443	}
444}
445
446/*
447 * Free resources allocated by pccard_function_init(), May be called as long
448 * as the function is disabled.
449 *
450 * NOTE: This function should be unnecessary.  pccard_function_init should
451 * never keep resources initialized.
452 */
453static void
454pccard_function_free(struct pccard_function *pf)
455{
456	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
457	struct resource_list_entry *rle;
458
459	if (pf->pf_flags & PFF_ENABLED) {
460		printf("pccard_function_init: function is enabled");
461		return;
462	}
463
464	SLIST_FOREACH(rle, &devi->resources, link) {
465		if (rle->res) {
466			if (rle->res->r_dev != pf->sc->dev)
467				device_printf(pf->sc->dev,
468				    "function_free: Resource still owned by "
469				    "child, oops. "
470				    "(type=%d, rid=%d, addr=%lx)\n",
471				    rle->type, rle->rid,
472				    rman_get_start(rle->res));
473			BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
474			    rle->res->r_dev, rle->type, rle->rid, rle->res);
475			rle->res = NULL;
476		}
477	}
478	resource_list_free(&devi->resources);
479}
480
481/* Enable a PCCARD function */
482static int
483pccard_function_enable(struct pccard_function *pf)
484{
485	struct pccard_function *tmp;
486	int reg;
487	device_t dev = pf->sc->dev;
488
489	if (pf->cfe == NULL) {
490		DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
491		return (ENOMEM);
492	}
493
494	/*
495	 * Increase the reference count on the socket, enabling power, if
496	 * necessary.
497	 */
498	pf->sc->sc_enabled_count++;
499
500	if (pf->pf_flags & PFF_ENABLED) {
501		/*
502		 * Don't do anything if we're already enabled.
503		 */
504		return (0);
505	}
506
507	/*
508	 * it's possible for different functions' CCRs to be in the same
509	 * underlying page.  Check for that.
510	 */
511	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
512		if ((tmp->pf_flags & PFF_ENABLED) &&
513		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
514		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
515		    (tmp->ccr_base - tmp->pf_ccr_offset +
516		    tmp->pf_ccr_realsize))) {
517			pf->pf_ccrt = tmp->pf_ccrt;
518			pf->pf_ccrh = tmp->pf_ccrh;
519			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
520
521			/*
522			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
523			 * tmp->ccr_base) + pf->ccr_base;
524			 */
525			/* pf->pf_ccr_offset =
526			    (tmp->pf_ccr_offset + pf->ccr_base) -
527			    tmp->ccr_base; */
528			pf->pf_ccr_window = tmp->pf_ccr_window;
529			break;
530		}
531	}
532	if (tmp == NULL) {
533		pf->ccr_rid = 0;
534		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
535		    &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE);
536		if (!pf->ccr_res)
537			goto bad;
538		DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%lx\n",
539		    rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
540		    pf->ccr_base));
541		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
542		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
543		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
544		    pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
545		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
546		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
547		pf->pf_ccr_realsize = 1;
548	}
549
550	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
551	reg |= PCCARD_CCR_OPTION_LEVIREQ;
552	if (pccard_mfc(pf->sc)) {
553		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
554			PCCARD_CCR_OPTION_ADDR_DECODE);
555		/*
556		 * XXX Need to enable PCCARD_CCR_OPTION_IRQ_ENABLE if
557		 * XXX we have an interrupt handler, but we don't know that
558		 * XXX at this point.
559		 */
560/*		reg |= PCCARD_CCR_OPTION_IREQ_ENABLE;*/
561	}
562	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
563
564	reg = 0;
565	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
566		reg |= PCCARD_CCR_STATUS_IOIS8;
567	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
568		reg |= PCCARD_CCR_STATUS_AUDIO;
569	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
570
571	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
572
573	if (pccard_mfc(pf->sc)) {
574		long tmp, iosize;
575
576		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
577		/* round up to nearest (2^n)-1 */
578		for (iosize = 1; iosize < tmp; iosize <<= 1)
579			;
580		iosize--;
581
582		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
583				 pf->pf_mfc_iobase & 0xff);
584		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
585				 (pf->pf_mfc_iobase >> 8) & 0xff);
586		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
587		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
588
589		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
590	}
591
592#ifdef PCCARDDEBUG
593	if (pccard_debug) {
594		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
595			device_printf(tmp->sc->dev,
596			    "function %d CCR at %d offset %x: "
597			    "%x %x %x %x, %x %x %x %x, %x\n",
598			    tmp->number, tmp->pf_ccr_window,
599			    tmp->pf_ccr_offset,
600			    pccard_ccr_read(tmp, 0x00),
601			    pccard_ccr_read(tmp, 0x02),
602			    pccard_ccr_read(tmp, 0x04),
603			    pccard_ccr_read(tmp, 0x06),
604			    pccard_ccr_read(tmp, 0x0A),
605			    pccard_ccr_read(tmp, 0x0C),
606			    pccard_ccr_read(tmp, 0x0E),
607			    pccard_ccr_read(tmp, 0x10),
608			    pccard_ccr_read(tmp, 0x12));
609		}
610	}
611#endif
612	pf->pf_flags |= PFF_ENABLED;
613	return (0);
614
615 bad:
616	/*
617	 * Decrement the reference count, and power down the socket, if
618	 * necessary.
619	 */
620	pf->sc->sc_enabled_count--;
621	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
622
623	return (1);
624}
625
626/* Disable PCCARD function. */
627static void
628pccard_function_disable(struct pccard_function *pf)
629{
630	struct pccard_function *tmp;
631	device_t dev = pf->sc->dev;
632
633	if (pf->cfe == NULL)
634		panic("pccard_function_disable: function not initialized");
635
636	if ((pf->pf_flags & PFF_ENABLED) == 0) {
637		/*
638		 * Don't do anything if we're already disabled.
639		 */
640		return;
641	}
642
643	if (pf->intr_handler != NULL) {
644		struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
645		struct resource_list_entry *rle =
646		    resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
647		BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
648		    pf->intr_handler_cookie);
649	}
650
651	/*
652	 * it's possible for different functions' CCRs to be in the same
653	 * underlying page.  Check for that.  Note we mark us as disabled
654	 * first to avoid matching ourself.
655	 */
656
657	pf->pf_flags &= ~PFF_ENABLED;
658	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
659		if ((tmp->pf_flags & PFF_ENABLED) &&
660		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
661		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
662		    (tmp->ccr_base - tmp->pf_ccr_offset +
663		    tmp->pf_ccr_realsize)))
664			break;
665	}
666
667	/* Not used by anyone else; unmap the CCR. */
668	if (tmp == NULL) {
669		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
670		    pf->ccr_res);
671		pf->ccr_res = NULL;
672	}
673
674	/*
675	 * Decrement the reference count, and power down the socket, if
676	 * necessary.
677	 */
678	pf->sc->sc_enabled_count--;
679}
680
681#if 0
682/* XXX These functions are needed, but not like this XXX */
683int
684pccard_io_map(struct pccard_function *pf, int width, bus_addr_t offset,
685    bus_size_t size, struct pccard_io_handle *pcihp, int *windowp)
686{
687	int reg;
688
689	if (pccard_chip_io_map(pf->sc->pct, pf->sc->pch, width, offset, size,
690	    pcihp, windowp))
691		return (1);
692
693	/*
694	 * XXX in the multifunction multi-iospace-per-function case, this
695	 * needs to cooperate with io_alloc to make sure that the spaces
696	 * don't overlap, and that the ccr's are set correctly
697	 */
698
699	if (pccard_mfc(pf->sc)) {
700		long tmp, iosize;
701
702		if (pf->pf_mfc_iomax == 0) {
703			pf->pf_mfc_iobase = pcihp->addr + offset;
704			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
705		} else {
706			/* this makes the assumption that nothing overlaps */
707			if (pf->pf_mfc_iobase > pcihp->addr + offset)
708				pf->pf_mfc_iobase = pcihp->addr + offset;
709			if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
710				pf->pf_mfc_iomax = pcihp->addr + offset + size;
711		}
712
713		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
714		/* round up to nearest (2^n)-1 */
715		for (iosize = 1; iosize >= tmp; iosize <<= 1)
716			;
717		iosize--;
718
719		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
720		    pf->pf_mfc_iobase & 0xff);
721		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
722		    (pf->pf_mfc_iobase >> 8) & 0xff);
723		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
724		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
725
726		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
727
728		reg = pccard_ccr_read(pf, PCCARD_CCR_OPTION);
729		reg |= PCCARD_CCR_OPTION_ADDR_DECODE;
730		pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
731	}
732	return (0);
733}
734
735void
736pccard_io_unmap(struct pccard_function *pf, int window)
737{
738
739	pccard_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
740
741	/* XXX Anything for multi-function cards? */
742}
743#endif
744
745/*
746 * simulate the old "probe" routine.  In the new world order, the driver
747 * needs to grab devices while in the old they were assigned to the device by
748 * the pccardd process.  These symbols are exported to the upper layers.
749 */
750static int
751pccard_compat_do_probe(device_t bus, device_t dev)
752{
753	return (CARD_COMPAT_MATCH(dev));
754}
755
756static int
757pccard_compat_do_attach(device_t bus, device_t dev)
758{
759	int err;
760
761	err = CARD_COMPAT_PROBE(dev);
762	if (err == 0)
763		err = CARD_COMPAT_ATTACH(dev);
764	return (err);
765}
766
767#define PCCARD_NPORT	2
768#define PCCARD_NMEM	5
769#define PCCARD_NIRQ	1
770#define PCCARD_NDRQ	0
771
772static int
773pccard_add_children(device_t dev, int busno)
774{
775	/* Call parent to scan for any current children */
776	return (0);
777}
778
779static int
780pccard_probe(device_t dev)
781{
782	device_set_desc(dev, "16-bit PCCard bus");
783	return (pccard_add_children(dev, device_get_unit(dev)));
784}
785
786static int
787pccard_attach(device_t dev)
788{
789	struct pccard_softc *sc = PCCARD_SOFTC(dev);
790
791	sc->dev = dev;
792	sc->sc_enabled_count = 0;
793	return (bus_generic_attach(dev));
794}
795
796static int
797pccard_detach(device_t dev)
798{
799	pccard_detach_card(dev, 0);
800	return 0;
801}
802
803static void
804pccard_print_resources(struct resource_list *rl, const char *name, int type,
805    int count, const char *format)
806{
807	struct resource_list_entry *rle;
808	int printed;
809	int i;
810
811	printed = 0;
812	for (i = 0; i < count; i++) {
813		rle = resource_list_find(rl, type, i);
814		if (rle != NULL) {
815			if (printed == 0)
816				printf(" %s ", name);
817			else if (printed > 0)
818				printf(",");
819			printed++;
820			printf(format, rle->start);
821			if (rle->count > 1) {
822				printf("-");
823				printf(format, rle->start + rle->count - 1);
824			}
825		} else if (i > 3) {
826			/* check the first few regardless */
827			break;
828		}
829	}
830}
831
832static int
833pccard_print_child(device_t dev, device_t child)
834{
835	struct pccard_ivar *devi = PCCARD_IVAR(child);
836	struct resource_list *rl = &devi->resources;
837	int retval = 0;
838
839	retval += bus_print_child_header(dev, child);
840	retval += printf(" at");
841
842	if (devi != NULL) {
843		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
844		    PCCARD_NPORT, "%#lx");
845		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
846		    PCCARD_NMEM, "%#lx");
847		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
848		    "%ld");
849		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
850		    "%ld");
851		retval += printf(" function %d config %d", devi->fcn->number,
852		    devi->fcn->cfe->number);
853	}
854
855	retval += bus_print_child_footer(dev, child);
856
857	return (retval);
858}
859
860static int
861pccard_set_resource(device_t dev, device_t child, int type, int rid,
862		 u_long start, u_long count)
863{
864	struct pccard_ivar *devi = PCCARD_IVAR(child);
865	struct resource_list *rl = &devi->resources;
866
867	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
868	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
869		return (EINVAL);
870	if (rid < 0)
871		return (EINVAL);
872	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
873		return (EINVAL);
874	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
875		return (EINVAL);
876	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
877		return (EINVAL);
878	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
879		return (EINVAL);
880
881	resource_list_add(rl, type, rid, start, start + count - 1, count);
882	if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
883	    type, &rid, start, start + count - 1, count, 0))
884		return 0;
885	else
886		return ENOMEM;
887}
888
889static int
890pccard_get_resource(device_t dev, device_t child, int type, int rid,
891    u_long *startp, u_long *countp)
892{
893	struct pccard_ivar *devi = PCCARD_IVAR(child);
894	struct resource_list *rl = &devi->resources;
895	struct resource_list_entry *rle;
896
897	rle = resource_list_find(rl, type, rid);
898	if (rle == NULL)
899		return (ENOENT);
900
901	if (startp != NULL)
902		*startp = rle->start;
903	if (countp != NULL)
904		*countp = rle->count;
905
906	return (0);
907}
908
909static void
910pccard_delete_resource(device_t dev, device_t child, int type, int rid)
911{
912	struct pccard_ivar *devi = PCCARD_IVAR(child);
913	struct resource_list *rl = &devi->resources;
914	resource_list_delete(rl, type, rid);
915}
916
917static int
918pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
919    u_int32_t flags)
920{
921	return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
922	    rid, flags));
923}
924
925static int
926pccard_set_memory_offset(device_t dev, device_t child, int rid,
927    u_int32_t offset, u_int32_t *deltap)
928
929{
930	return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
931	    offset, deltap));
932}
933
934static int
935pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
936{
937	struct pccard_ivar *devi = PCCARD_IVAR(child);
938	struct pccard_function *func = devi->fcn;
939	struct pccard_softc *sc = PCCARD_SOFTC(bus);
940
941	/* PCCARD_IVAR_ETHADDR unhandled from oldcard */
942	switch (which) {
943	default:
944	case PCCARD_IVAR_ETHADDR:
945		return (ENOENT);
946		break;
947	case PCCARD_IVAR_VENDOR:
948		*(u_int32_t *) result = sc->card.manufacturer;
949		break;
950	case PCCARD_IVAR_PRODUCT:
951		*(u_int32_t *) result = sc->card.product;
952		break;
953	case PCCARD_IVAR_FUNCTION:
954		*(u_int32_t *) result = func->function;
955		break;
956	case PCCARD_IVAR_FUNCTION_NUMBER:
957		if (!func) {
958			device_printf(bus, "No function number, bug!\n");
959			return (ENOENT);
960		}
961		*(u_int32_t *) result = func->number;
962		break;
963	case PCCARD_IVAR_VENDOR_STR:
964		*(char **) result = sc->card.cis1_info[0];
965		break;
966	case PCCARD_IVAR_PRODUCT_STR:
967		*(char **) result = sc->card.cis1_info[1];
968		break;
969	case PCCARD_IVAR_CIS3_STR:
970		*(char **) result = sc->card.cis1_info[2];
971		break;
972	case PCCARD_IVAR_CIS4_STR:
973		*(char **) result = sc->card.cis1_info[2];
974		break;
975	}
976	return (0);
977}
978
979static void
980pccard_driver_added(device_t dev, driver_t *driver)
981{
982	struct pccard_softc *sc = PCCARD_SOFTC(dev);
983	struct pccard_function *pf;
984	device_t child;
985
986	if (sc->sc_enabled_count == 0) {
987		CARD_REPROBE_CARD(device_get_parent(dev), dev);
988		return;
989	}
990
991	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
992		if (STAILQ_EMPTY(&pf->cfe_head))
993			continue;
994		child = pf->dev;
995		if (device_get_state(child) != DS_NOTPRESENT)
996			continue;
997		if (pccard_function_enable(pf) == 0 &&
998		    device_probe_and_attach(child) == 0) {
999			DEVPRINTF((sc->dev, "function %d CCR at %d "
1000			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
1001			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
1002			    pccard_ccr_read(pf, 0x00),
1003			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
1004			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
1005			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
1006			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
1007		} else {
1008			pccard_function_disable(pf);
1009		}
1010	}
1011	return;
1012}
1013
1014static struct resource *
1015pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
1016    u_long start, u_long end, u_long count, u_int flags)
1017{
1018	struct pccard_ivar *dinfo;
1019	struct resource_list_entry *rle = 0;
1020	int passthrough = (device_get_parent(child) != dev);
1021
1022	if (passthrough) {
1023		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
1024		    type, rid, start, end, count, flags));
1025	}
1026
1027	dinfo = device_get_ivars(child);
1028	rle = resource_list_find(&dinfo->resources, type, *rid);
1029
1030	if (!rle)
1031		return NULL;		/* no resource of that type/rid */
1032
1033	if (!rle->res) {
1034		device_printf(dev, "WARNING: Resource not reserved by pccard bus\n");
1035		return NULL;
1036	} else {
1037		if (rle->res->r_dev != dev) return NULL;
1038		bus_release_resource(dev, type, *rid, rle->res);
1039		rle->res = NULL;
1040		switch(type) {
1041		case SYS_RES_IOPORT:
1042		case SYS_RES_MEMORY:
1043			if (!(flags & RF_ALIGNMENT_MASK))
1044				flags |= rman_make_alignment_flags(rle->count);
1045			break;
1046		case SYS_RES_IRQ:
1047			flags |= RF_SHAREABLE;
1048			break;
1049		}
1050		return resource_list_alloc(&dinfo->resources, dev, child, type,
1051		    rid, rle->start, rle->end, rle->count, flags);
1052	}
1053}
1054
1055static int
1056pccard_release_resource(device_t dev, device_t child, int type, int rid,
1057    struct resource *r)
1058{
1059	struct pccard_ivar *dinfo;
1060	int passthrough = (device_get_parent(child) != dev);
1061	struct resource_list_entry *rle = 0;
1062	int ret;
1063	int flags;
1064
1065	if (passthrough)
1066		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1067		    type, rid, r);
1068
1069	dinfo = device_get_ivars(child);
1070
1071	rle = resource_list_find(&dinfo->resources, type, rid);
1072
1073	if (!rle) {
1074		device_printf(dev, "Allocated resource not found, "
1075		    "%d %x %lx %lx\n",
1076		    type, rid, rman_get_start(r), rman_get_size(r));
1077		return ENOENT;
1078	}
1079	if (!rle->res) {
1080		device_printf(dev, "Allocated resource not recorded\n");
1081		return ENOENT;
1082	}
1083
1084	ret = BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1085	    type, rid, r);
1086	switch(type) {
1087	case SYS_RES_IOPORT:
1088	case SYS_RES_MEMORY:
1089		flags = rman_make_alignment_flags(rle->count);
1090		break;
1091	case SYS_RES_IRQ:
1092		flags = RF_SHAREABLE;
1093		break;
1094	default:
1095		flags = 0;
1096	}
1097	rle->res = bus_alloc_resource(dev, type, &rid,
1098	    rle->start, rle->end, rle->count, flags);
1099	if (rle->res == NULL)
1100		device_printf(dev, "release_resource: "
1101		    "unable to reaquire resource\n");
1102	return ret;
1103}
1104
1105static void
1106pccard_child_detached(device_t parent, device_t dev)
1107{
1108	struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1109	struct pccard_function *pf = ivar->fcn;
1110
1111	pccard_function_disable(pf);
1112}
1113
1114static void
1115pccard_intr(void *arg)
1116{
1117	struct pccard_function *pf = (struct pccard_function*) arg;
1118	int reg;
1119
1120	if (pf->intr_handler == NULL)
1121		panic("Null interrupt handler?\n");
1122
1123	reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1124	if (reg & PCCARD_CCR_STATUS_INTR) {
1125		pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1126				 reg & ~PCCARD_CCR_STATUS_INTR);
1127		pf->intr_handler(pf->intr_handler_arg);
1128	}
1129}
1130
1131static int
1132pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1133    int flags, driver_intr_t *intr, void *arg, void **cookiep)
1134{
1135	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1136	struct pccard_function *func = ivar->fcn;
1137
1138	if (func->intr_handler != NULL)
1139		panic("Only one interrupt handler per function allowed\n");
1140
1141	func->intr_handler = intr;
1142	func->intr_handler_arg = arg;
1143	func->intr_handler_cookie = *cookiep;
1144	pccard_ccr_write(func, PCCARD_CCR_OPTION,
1145	    pccard_ccr_read(func, PCCARD_CCR_OPTION) |
1146	    PCCARD_CCR_OPTION_IREQ_ENABLE);
1147
1148	bus_setup_intr(dev, irq, INTR_TYPE_TTY/* | INTR_FAST*/,
1149	    pccard_intr, func, cookiep);
1150	return (0);
1151}
1152
1153static int
1154pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
1155    void *cookie)
1156{
1157	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1158	struct pccard_function *func = ivar->fcn;
1159	int ret;
1160
1161	pccard_ccr_write(func, PCCARD_CCR_OPTION,
1162	    pccard_ccr_read(func, PCCARD_CCR_OPTION) &
1163	    ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1164
1165	ret = bus_teardown_intr(dev, r, cookie);
1166	if (ret == 0) {
1167		func->intr_handler = NULL;
1168		func->intr_handler_arg = NULL;
1169		func->intr_handler_cookie = NULL;
1170	}
1171
1172	return (ret);
1173}
1174
1175static device_method_t pccard_methods[] = {
1176	/* Device interface */
1177	DEVMETHOD(device_probe,		pccard_probe),
1178	DEVMETHOD(device_attach,	pccard_attach),
1179	DEVMETHOD(device_detach,	pccard_detach),
1180	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1181	DEVMETHOD(device_suspend,	bus_generic_suspend),
1182	DEVMETHOD(device_resume,	bus_generic_resume),
1183
1184	/* Bus interface */
1185	DEVMETHOD(bus_print_child,	pccard_print_child),
1186	DEVMETHOD(bus_driver_added,	pccard_driver_added),
1187	DEVMETHOD(bus_child_detached,	pccard_child_detached),
1188	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
1189	DEVMETHOD(bus_release_resource,	pccard_release_resource),
1190	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1191	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1192	DEVMETHOD(bus_setup_intr,	pccard_setup_intr),
1193	DEVMETHOD(bus_teardown_intr,	pccard_teardown_intr),
1194	DEVMETHOD(bus_set_resource,	pccard_set_resource),
1195	DEVMETHOD(bus_get_resource,	pccard_get_resource),
1196	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
1197	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
1198
1199	/* Card Interface */
1200	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
1201	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
1202	DEVMETHOD(card_get_type,	pccard_card_gettype),
1203	DEVMETHOD(card_attach_card,	pccard_attach_card),
1204	DEVMETHOD(card_detach_card,	pccard_detach_card),
1205	DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe),
1206	DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach),
1207
1208	{ 0, 0 }
1209};
1210
1211static driver_t pccard_driver = {
1212	"pccard",
1213	pccard_methods,
1214	sizeof(struct pccard_softc)
1215};
1216
1217devclass_t	pccard_devclass;
1218
1219DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1220DRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0);
1221DRIVER_MODULE(pccard, pccbb, pccard_driver, pccard_devclass, 0, 0);
1222DRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0);
1223MODULE_VERSION(pccard, 1);
1224/*MODULE_DEPEND(pccard, pcic, 1, 1, 1);*/
1225