pccard.c revision 67333
1/*	$NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $	*/
2/* $FreeBSD: head/sys/dev/pccard/pccard.c 67333 2000-10-19 22:36:41Z 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
73int	pccard_print(void *, const char *);
74
75int
76pccard_ccr_read(pf, ccr)
77	struct pccard_function *pf;
78	int ccr;
79{
80	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
81	    pf->pf_ccr_offset + ccr));
82}
83
84void
85pccard_ccr_write(pf, ccr, val)
86	struct pccard_function *pf;
87	int ccr;
88	int val;
89{
90
91	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
92		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
93		    pf->pf_ccr_offset + ccr, val);
94	}
95}
96
97static int
98pccard_attach_card(device_t dev)
99{
100	struct pccard_softc *sc = PCCARD_SOFTC(dev);
101	struct pccard_function *pf;
102	struct pccard_ivar *ivar;
103	device_t child;
104	int attached;
105
106	/*
107	 * this is here so that when socket_enable calls gettype, trt happens
108	 */
109	STAILQ_INIT(&sc->card.pf_head);
110
111	DEVPRINTF((dev, "chip_socket_enable\n"));
112	POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
113
114	DEVPRINTF((dev, "read_cis\n"));
115	pccard_read_cis(sc);
116
117	DEVPRINTF((dev, "check_cis_quirks\n"));
118	pccard_check_cis_quirks(dev);
119
120	/*
121	 * bail now if the card has no functions, or if there was an error in
122	 * the cis.
123	 */
124
125	if (sc->card.error)
126		return (1);
127	if (STAILQ_EMPTY(&sc->card.pf_head))
128		return (1);
129
130	if (1)
131		pccard_print_cis(dev);
132
133	attached = 0;
134
135	DEVPRINTF((dev, "functions scanning\n"));
136	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
137		if (STAILQ_EMPTY(&pf->cfe_head))
138			continue;
139
140		pf->sc = sc;
141		pf->cfe = NULL;
142		pf->dev = NULL;
143	}
144#if 0
145	DEVPRINTF((dev, "chip_socket_disable\n"));
146	POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
147#endif
148	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
149		if (STAILQ_EMPTY(&pf->cfe_head))
150			continue;
151		/*
152		 * In NetBSD, the drivers are responsible for activating
153		 * each function of a card.  I think that in FreeBSD we
154		 * want to activate them enough for the usual bus_*_resource
155		 * routines will do the right thing.  This many mean a
156		 * departure from the current NetBSD model.
157		 *
158		 * This could get really ugly for multifunction cards.  But
159		 * it might also just fall out of the FreeBSD resource model.
160		 *
161		 */
162		ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, M_WAITOK);
163		bzero(ivar, sizeof *ivar);
164		child = device_add_child(dev, NULL, -1);
165		device_set_ivars(child, ivar);
166		ivar->fcn = pf;
167		pf->dev = child;
168		/*
169		 * XXX We might want to move the next two lines into
170		 * XXX the pccard interface layer.  For the moment, this
171		 * XXX is OK, but some drivers want to pick the config
172		 * XXX entry to use as well as some address tweaks (mostly
173		 * XXX due to bugs in decode logic that makes some
174		 * XXX addresses illegal or broken).
175		 */
176		pccard_function_init(pf);
177		if (pccard_function_enable(pf) == 0 &&
178		    device_probe_and_attach(child) == 0) {
179			attached++;
180
181			DEVPRINTF((sc->dev, "function %d CCR at %d "
182			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
183			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
184			    pccard_ccr_read(pf, 0x00),
185			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
186			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
187			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
188			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
189		} else {
190			device_delete_child(dev, child);
191		}
192	}
193	return 0;
194}
195
196static int
197pccard_detach_card(device_t dev, int flags)
198{
199	struct pccard_softc *sc = PCCARD_SOFTC(dev);
200	struct pccard_function *pf;
201
202	/*
203	 * We are running on either the PCCARD socket's event thread
204	 * or in user context detaching a device by user request.
205	 */
206	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
207		if (STAILQ_FIRST(&pf->cfe_head) == NULL)
208			continue;
209
210		pccard_function_disable(pf);
211		if (pf->dev)
212			device_delete_child(dev, pf->dev);
213	}
214	return 0;
215}
216
217const struct pccard_product *
218pccard_product_lookup(device_t dev, const struct pccard_product *tab,
219    size_t ent_size, pccard_product_match_fn matchfn)
220{
221	const struct pccard_product *ent;
222	int matches;
223	u_int32_t fcn;
224	u_int32_t vendor;
225	u_int32_t prod;
226	char *vendorstr;
227	char *prodstr;
228
229#ifdef DIAGNOSTIC
230	if (sizeof *ent > ent_size)
231		panic("pccard_product_lookup: bogus ent_size %ld",
232		    (long) ent_size);
233#endif
234	if (pccard_get_vendor(dev, &vendor))
235		return (NULL);
236	if (pccard_get_product(dev, &prod))
237		return (NULL);
238	if (pccard_get_function_number(dev, &fcn))
239		return (NULL);
240	if (pccard_get_vendor_str(dev, &vendorstr))
241		return (NULL);
242	if (pccard_get_product_str(dev, &prodstr))
243		return (NULL);
244        for (ent = tab; ent->pp_name != NULL;
245	     ent = (const struct pccard_product *)
246		 ((const char *) ent + ent_size)) {
247		matches = 1;
248		if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
249		    vendor != ent->pp_vendor)
250			matches = 0;
251		if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
252		    prod != ent->pp_product)
253			matches = 0;
254		if (matches && fcn != ent->pp_expfunc)
255			matches = 0;
256		if (matches && ent->pp_vendor_str &&
257		    strcmp(ent->pp_vendor_str, vendorstr) != 0)
258			matches = 0;
259		if (matches && ent->pp_product_str &&
260		    strcmp(ent->pp_product_str, prodstr) != 0)
261			matches = 0;
262		if (matchfn != NULL)
263			matches = (*matchfn)(dev, ent, matches);
264		if (matches)
265			return (ent);
266	}
267	return (NULL);
268}
269
270static int
271pccard_card_gettype(device_t dev, int *type)
272{
273	struct pccard_softc *sc = PCCARD_SOFTC(dev);
274	struct pccard_function *pf;
275
276	/*
277	 * set the iftype to memory if this card has no functions (not yet
278	 * probed), or only one function, and that is not initialized yet or
279	 * that is memory.
280	 */
281	pf = STAILQ_FIRST(&sc->card.pf_head);
282	if (pf == NULL ||
283	    (STAILQ_NEXT(pf, pf_list) == NULL &&
284	    (pf->cfe == NULL || pf->cfe->iftype == PCCARD_IFTYPE_MEMORY)))
285		*type = PCCARD_IFTYPE_MEMORY;
286	else
287		*type = PCCARD_IFTYPE_IO;
288	return 0;
289}
290
291/*
292 * Initialize a PCCARD function.  May be called as long as the function is
293 * disabled.
294 */
295void
296pccard_function_init(struct pccard_function *pf)
297{
298	struct pccard_config_entry *cfe;
299	int i;
300	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
301	struct resource_list *rl = &devi->resources;
302	struct resource *r = 0;
303	device_t bus;
304
305	if (pf->pf_flags & PFF_ENABLED)
306		panic("pccard_function_init: function is enabled");
307
308	bus = device_get_parent(pf->dev);
309	/* Remember which configuration entry we are using. */
310	for (cfe = STAILQ_FIRST(&pf->cfe_head); cfe != NULL;
311	    cfe = STAILQ_NEXT(cfe, cfe_list)) {
312		for (i = 0; i < cfe->num_iospace; i++)
313			cfe->iores[i] = NULL;
314		cfe->irqres = NULL;
315		for (i = 0; i < cfe->num_iospace; i++) {
316			/* XXX kludge, need to not ignore start */
317			/* XXX start is a hint here, so this would break */
318			/* XXX modems */
319			/* XXX ALSO: should just ask for the range 0 to */
320			/* XXX 1 << decode bits - 1, so we have a layering */
321			/* XXX violation now */
322			cfe->iorid[i] = i;
323			r = cfe->iores[i] = bus_alloc_resource(bus,
324			    SYS_RES_IOPORT, &cfe->iorid[i], 0x100, 0x3ff,
325			    cfe->iospace[i].length, 0);
326			if (cfe->iores[i] == 0)
327				goto not_this_one;
328			resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i],
329			    rman_get_start(r), rman_get_end(r),
330			    cfe->iospace[i].length);
331
332		}
333		if (cfe->num_memspace > 0) {
334			goto not_this_one;
335		}
336		if (cfe->irqmask) {
337			cfe->irqrid = 0;
338			cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ,
339			    &cfe->irqrid, 10, 12, 1, 0);
340			if (cfe->irqres == 0)
341				goto not_this_one;
342			resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid,
343			    rman_get_start(r), rman_get_end(r), 1);
344		}
345		/* XXX Don't know how to deal with maxtwins */
346		/* If we get to here, we've allocated all we need */
347		pf->cfe = cfe;
348		break;
349	    not_this_one:;
350		/*
351		 * Release resources that we partially allocated
352		 * from this config entry.
353		 */
354		for (i = 0; i < cfe->num_iospace; i++) {
355			resource_list_delete(rl, SYS_RES_IOPORT, i);
356			if (cfe->iores[i])
357				bus_release_resource(bus, SYS_RES_IOPORT,
358				    cfe->iorid[i], cfe->iores[i]);
359			cfe->iores[i] = NULL;
360		}
361		if (cfe->irqmask && cfe->irqres) {
362			resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid);
363			bus_release_resource(bus, SYS_RES_IRQ,
364			    cfe->irqrid, cfe->irqres);
365			cfe->irqres = NULL;
366		}
367	}
368}
369
370/* Enable a PCCARD function */
371int
372pccard_function_enable(struct pccard_function *pf)
373{
374	struct pccard_function *tmp;
375	int reg;
376	device_t dev = pf->sc->dev;
377
378	if (pf->cfe == NULL) {
379		DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
380		return ENOMEM;
381	}
382
383	/*
384	 * Increase the reference count on the socket, enabling power, if
385	 * necessary.
386	 */
387	if (pf->sc->sc_enabled_count++ == 0)
388		POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
389	DEVPRINTF((dev, "++enabled_count = %d\n", pf->sc->sc_enabled_count));
390
391	if (pf->pf_flags & PFF_ENABLED) {
392		/*
393		 * Don't do anything if we're already enabled.
394		 */
395		return (0);
396	}
397
398	/*
399	 * it's possible for different functions' CCRs to be in the same
400	 * underlying page.  Check for that.
401	 */
402	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
403		if ((tmp->pf_flags & PFF_ENABLED) &&
404		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
405		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
406		     (tmp->ccr_base - tmp->pf_ccr_offset +
407		      tmp->pf_ccr_realsize))) {
408			pf->pf_ccrt = tmp->pf_ccrt;
409			pf->pf_ccrh = tmp->pf_ccrh;
410			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
411
412			/*
413			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
414			 * tmp->ccr_base) + pf->ccr_base;
415			 */
416			pf->pf_ccr_offset =
417			    (tmp->pf_ccr_offset + pf->ccr_base) -
418			    tmp->ccr_base;
419			pf->pf_ccr_window = tmp->pf_ccr_window;
420			break;
421		}
422	}
423
424	if (tmp == NULL) {
425		pf->ccr_rid = 0;
426		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
427		    &pf->ccr_rid, 0xa0000, 0xdffff, 1 << 10, RF_ACTIVE);
428		if (!pf->ccr_res) {
429			DEVPRINTF((dev, "ccr_res == 0\n"));
430			goto bad;
431		}
432		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
433		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
434		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
435		    pf->ccr_rid, (pf->ccr_rid >> 10) << 10);
436		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
437		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
438		pf->pf_ccr_offset = rman_get_start(pf->ccr_res);
439		pf->pf_ccr_realsize = 1;
440	}
441
442	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
443	reg |= PCCARD_CCR_OPTION_LEVIREQ;
444	if (pccard_mfc(pf->sc)) {
445		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
446			PCCARD_CCR_OPTION_ADDR_DECODE);
447		/*
448		 * XXX Need to enable PCCARD_CCR_OPTION_IRQ_ENABLE if
449		 * XXX we have an interrupt handler, but we don't know that
450		 * XXX at this point.
451		 */
452		reg |= PCCARD_CCR_OPTION_IREQ_ENABLE;
453	}
454	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
455
456	reg = 0;
457	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
458		reg |= PCCARD_CCR_STATUS_IOIS8;
459	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
460		reg |= PCCARD_CCR_STATUS_AUDIO;
461	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
462
463	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
464
465	if (pccard_mfc(pf->sc)) {
466		long tmp, iosize;
467
468		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
469		/* round up to nearest (2^n)-1 */
470		for (iosize = 1; iosize < tmp; iosize <<= 1)
471			;
472		iosize--;
473
474		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
475				 pf->pf_mfc_iobase & 0xff);
476		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
477				 (pf->pf_mfc_iobase >> 8) & 0xff);
478		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
479		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
480
481		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
482	}
483
484#ifdef PCCARDDEBUG
485	if (pccard_debug) {
486		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
487			device_printf(tmp->sc->dev,
488			    "function %d CCR at %d offset %x: "
489			    "%x %x %x %x, %x %x %x %x, %x\n",
490			    tmp->number, tmp->pf_ccr_window,
491			    tmp->pf_ccr_offset,
492			    pccard_ccr_read(tmp, 0x00),
493			    pccard_ccr_read(tmp, 0x02),
494			    pccard_ccr_read(tmp, 0x04),
495			    pccard_ccr_read(tmp, 0x06),
496			    pccard_ccr_read(tmp, 0x0A),
497			    pccard_ccr_read(tmp, 0x0C),
498			    pccard_ccr_read(tmp, 0x0E),
499			    pccard_ccr_read(tmp, 0x10),
500			    pccard_ccr_read(tmp, 0x12));
501		}
502	}
503#endif
504	pf->pf_flags |= PFF_ENABLED;
505	return (0);
506
507 bad:
508	/*
509	 * Decrement the reference count, and power down the socket, if
510	 * necessary.
511	 */
512	if (--pf->sc->sc_enabled_count == 0)
513		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
514	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
515
516	return (1);
517}
518
519/* Disable PCCARD function. */
520void
521pccard_function_disable(struct pccard_function *pf)
522{
523	struct pccard_function *tmp;
524	device_t dev = pf->sc->dev;
525
526	if (pf->cfe == NULL)
527		panic("pccard_function_disable: function not initialized");
528
529	if ((pf->pf_flags & PFF_ENABLED) == 0) {
530		/*
531		 * Don't do anything if we're already disabled.
532		 */
533		return;
534	}
535
536	/*
537	 * it's possible for different functions' CCRs to be in the same
538	 * underlying page.  Check for that.  Note we mark us as disabled
539	 * first to avoid matching ourself.
540	 */
541
542	pf->pf_flags &= ~PFF_ENABLED;
543	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
544		if ((tmp->pf_flags & PFF_ENABLED) &&
545		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
546		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
547		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
548			break;
549	}
550
551	/* Not used by anyone else; unmap the CCR. */
552	if (tmp == NULL) {
553		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
554		    pf->ccr_res);
555		pf->ccr_res = NULL;
556	}
557
558	/*
559	 * Decrement the reference count, and power down the socket, if
560	 * necessary.
561	 */
562	if (--pf->sc->sc_enabled_count == 0)
563		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
564	DEVPRINTF((dev, "--enabled_count = %d\n", pf->sc->sc_enabled_count));
565}
566
567#if 0
568/* XXX These functions are needed, but not like this XXX */
569int
570pccard_io_map(struct pccard_function *pf, int width, bus_addr_t offset,
571    bus_size_t size, struct pccard_io_handle *pcihp, int *windowp)
572{
573	int reg;
574
575	if (pccard_chip_io_map(pf->sc->pct, pf->sc->pch, width, offset, size,
576	    pcihp, windowp))
577		return (1);
578
579	/*
580	 * XXX in the multifunction multi-iospace-per-function case, this
581	 * needs to cooperate with io_alloc to make sure that the spaces
582	 * don't overlap, and that the ccr's are set correctly
583	 */
584
585	if (pccard_mfc(pf->sc)) {
586		long tmp, iosize;
587
588		if (pf->pf_mfc_iomax == 0) {
589			pf->pf_mfc_iobase = pcihp->addr + offset;
590			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
591		} else {
592			/* this makes the assumption that nothing overlaps */
593			if (pf->pf_mfc_iobase > pcihp->addr + offset)
594				pf->pf_mfc_iobase = pcihp->addr + offset;
595			if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
596				pf->pf_mfc_iomax = pcihp->addr + offset + size;
597		}
598
599		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
600		/* round up to nearest (2^n)-1 */
601		for (iosize = 1; iosize >= tmp; iosize <<= 1)
602			;
603		iosize--;
604
605		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
606		    pf->pf_mfc_iobase & 0xff);
607		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
608		    (pf->pf_mfc_iobase >> 8) & 0xff);
609		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
610		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
611
612		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
613
614		reg = pccard_ccr_read(pf, PCCARD_CCR_OPTION);
615		reg |= PCCARD_CCR_OPTION_ADDR_DECODE;
616		pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
617	}
618	return (0);
619}
620
621void
622pccard_io_unmap(struct pccard_function *pf, int window)
623{
624
625	pccard_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
626
627	/* XXX Anything for multi-function cards? */
628}
629#endif
630
631/*
632 * simulate the old "probe" routine.  In the new world order, the driver
633 * needs to grab devices while in the old they were assigned to the device by
634 * the pccardd process.  These symbols are exported to the upper layers.
635 */
636int
637pccard_compat_probe(device_t dev)
638{
639	return (CARD_COMPAT_MATCH(dev));
640}
641
642int
643pccard_compat_attach(device_t dev)
644{
645	int err;
646
647	err = CARD_COMPAT_PROBE(dev);
648	if (err == 0)
649		err = CARD_COMPAT_ATTACH(dev);
650	return (err);
651}
652
653#define PCCARD_NPORT	2
654#define PCCARD_NMEM	5
655#define PCCARD_NIRQ	1
656#define PCCARD_NDRQ	0
657
658static int
659pccard_add_children(device_t dev, int busno)
660{
661	/* Call parent to scan for any current children */
662	return 0;
663}
664
665static int
666pccard_probe(device_t dev)
667{
668	device_set_desc(dev, "16-bit PCCard bus");
669	return pccard_add_children(dev, device_get_unit(dev));
670}
671
672static int
673pccard_attach(device_t dev)
674{
675	struct pccard_softc *sc = PCCARD_SOFTC(dev);
676
677	sc->dev = dev;
678	sc->sc_enabled_count = 0;
679	return bus_generic_attach(dev);
680}
681
682static void
683pccard_print_resources(struct resource_list *rl, const char *name, int type,
684    int count, const char *format)
685{
686	struct resource_list_entry *rle;
687	int printed;
688	int i;
689
690	printed = 0;
691	for (i = 0; i < count; i++) {
692		rle = resource_list_find(rl, type, i);
693		if (rle) {
694			if (printed == 0)
695				printf(" %s ", name);
696			else if (printed > 0)
697				printf(",");
698			printed++;
699			printf(format, rle->start);
700			if (rle->count > 1) {
701				printf("-");
702				printf(format, rle->start + rle->count - 1);
703			}
704		} else if (i > 3) {
705			/* check the first few regardless */
706			break;
707		}
708	}
709}
710
711static int
712pccard_print_child(device_t dev, device_t child)
713{
714	struct pccard_ivar *devi = PCCARD_IVAR(child);
715	struct resource_list *rl = &devi->resources;
716	int retval = 0;
717
718	retval += bus_print_child_header(dev, child);
719	retval += printf(" at");
720
721	if (devi) {
722		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
723		    PCCARD_NPORT, "%#lx");
724		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
725		    PCCARD_NMEM, "%#lx");
726		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
727		    "%ld");
728		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
729		    "%ld");
730		retval += printf(" function %d config %d", devi->fcn->number,
731		    devi->fcn->cfe->number);
732	}
733
734	retval += bus_print_child_footer(dev, child);
735
736	return (retval);
737}
738
739static int
740pccard_set_resource(device_t dev, device_t child, int type, int rid,
741		 u_long start, u_long count)
742{
743	struct pccard_ivar *devi = PCCARD_IVAR(child);
744	struct resource_list *rl = &devi->resources;
745
746	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
747	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
748		return EINVAL;
749	if (rid < 0)
750		return EINVAL;
751	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
752		return EINVAL;
753	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
754		return EINVAL;
755	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
756		return EINVAL;
757	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
758		return EINVAL;
759
760	resource_list_add(rl, type, rid, start, start + count - 1, count);
761
762	return 0;
763}
764
765static int
766pccard_get_resource(device_t dev, device_t child, int type, int rid,
767    u_long *startp, u_long *countp)
768{
769	struct pccard_ivar *devi = PCCARD_IVAR(child);
770	struct resource_list *rl = &devi->resources;
771	struct resource_list_entry *rle;
772
773	rle = resource_list_find(rl, type, rid);
774	if (!rle)
775		return ENOENT;
776
777	if (startp)
778		*startp = rle->start;
779	if (countp)
780		*countp = rle->count;
781
782	return 0;
783}
784
785static void
786pccard_delete_resource(device_t dev, device_t child, int type, int rid)
787{
788	struct pccard_ivar *devi = PCCARD_IVAR(child);
789	struct resource_list *rl = &devi->resources;
790	resource_list_delete(rl, type, rid);
791}
792
793static int
794pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
795    u_int32_t flags)
796{
797	return CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
798	    rid, flags);
799}
800
801static int
802pccard_set_memory_offset(device_t dev, device_t child, int rid,
803     u_int32_t offset)
804{
805	return CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
806	    offset);
807}
808
809static int
810pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
811{
812	struct pccard_ivar *devi = PCCARD_IVAR(child);
813	struct pccard_function *func = devi->fcn;
814	struct pccard_softc *sc = PCCARD_SOFTC(bus);
815
816	/* PCCARD_IVAR_ETHADDR unhandled from oldcard */
817	switch (which) {
818	default:
819	case PCCARD_IVAR_ETHADDR:
820		return (ENOENT);
821		break;
822	case PCCARD_IVAR_VENDOR:
823		*(u_int32_t *) result = sc->card.manufacturer;
824		break;
825	case PCCARD_IVAR_PRODUCT:
826		*(u_int32_t *) result = sc->card.product;
827		break;
828	case PCCARD_IVAR_FUNCTION_NUMBER:
829		if (!func) {
830			device_printf(bus, "No function number, bug!\n");
831			return (ENOENT);
832		}
833		*(u_int32_t *) result = func->number;
834		break;
835	case PCCARD_IVAR_VENDOR_STR:
836		*(char **) result = sc->card.cis1_info[0];
837		break;
838	case PCCARD_IVAR_PRODUCT_STR:
839		*(char **) result = sc->card.cis1_info[1];
840		break;
841	case PCCARD_IVAR_CIS3_STR:
842		*(char **) result = sc->card.cis1_info[2];
843		break;
844	case PCCARD_IVAR_CIS4_STR:
845		*(char **) result = sc->card.cis1_info[2];
846		break;
847	}
848	return (0);
849}
850
851static void
852pccard_driver_added(device_t dev, driver_t *driver)
853{
854	/*
855	 * XXX eventually we need to attach stuff when we know we
856	 * XXX have kids.  For now we do nothing because we normally
857	 * XXX add children ourselves.  We don't want to necessarily
858	 * XXX force a reprobe.
859	 */
860}
861
862static struct resource *
863pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
864    u_long start, u_long end, u_long count, u_int flags)
865{
866	struct pccard_ivar *ivar;
867	struct pccard_function *pf;
868	struct resource *r = 0;
869
870	if (device_get_parent(child) == dev) {
871		ivar = PCCARD_IVAR(child);
872		pf = ivar->fcn;
873		switch (type) {
874		case SYS_RES_IRQ:
875			if (*rid > 0)
876				return NULL;
877			r = pf->cfe->irqres;
878			break;
879		case SYS_RES_IOPORT:
880			if (*rid > 3)	/* XXX */
881				return NULL;
882			r = pf->cfe->iores[*rid];
883			break;
884		default:
885			break;
886		}
887	}
888	if (r != NULL) {
889		if (flags & RF_ACTIVE)
890			bus_generic_activate_resource(dev, child, type,
891			    *rid, r);
892		return (r);
893	}
894	return (bus_generic_alloc_resource(dev, child, type, rid, start,
895	    end, count, flags));
896}
897
898static int
899pccard_release_resource(device_t dev, device_t child, int type, int rid,
900    struct resource *r)
901{
902	return bus_generic_release_resource(dev, child, type, rid, r);
903}
904
905static int
906pccard_activate_resource(device_t dev, device_t child, int type, int rid,
907    struct resource *r)
908{
909	/* XXX need to write to the COR to activate this for mf cards */
910	return (bus_generic_activate_resource(dev, child, type, rid, r));
911}
912
913static int
914pccard_deactivate_resource(device_t dev, device_t child, int type, int rid,
915    struct resource *r)
916{
917	/* XXX need to write to the COR to deactivate this for mf cards */
918	return (bus_generic_deactivate_resource(dev, child, type, rid, r));
919}
920
921static void
922pccard_child_detached(device_t parent, device_t dev)
923{
924	struct pccard_ivar *ivar = PCCARD_IVAR(dev);
925
926	if (parent == device_get_parent(dev))
927		free(ivar, M_DEVBUF);
928}
929
930static device_method_t pccard_methods[] = {
931	/* Device interface */
932	DEVMETHOD(device_probe,		pccard_probe),
933	DEVMETHOD(device_attach,	pccard_attach),
934	DEVMETHOD(device_detach,	bus_generic_detach),
935	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
936	DEVMETHOD(device_suspend,	bus_generic_suspend),
937	DEVMETHOD(device_resume,	bus_generic_resume),
938
939	/* Bus interface */
940	DEVMETHOD(bus_print_child,	pccard_print_child),
941	DEVMETHOD(bus_driver_added,	pccard_driver_added),
942	DEVMETHOD(bus_child_detached,	pccard_child_detached),
943	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
944	DEVMETHOD(bus_release_resource,	pccard_release_resource),
945	DEVMETHOD(bus_activate_resource, pccard_activate_resource),
946	DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
947	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
948	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
949	DEVMETHOD(bus_set_resource,	pccard_set_resource),
950	DEVMETHOD(bus_get_resource,	pccard_get_resource),
951	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
952	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
953
954	/* Card Interface */
955	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
956	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
957	DEVMETHOD(card_get_type,	pccard_card_gettype),
958	DEVMETHOD(card_attach_card,	pccard_attach_card),
959	DEVMETHOD(card_detach_card,	pccard_detach_card),
960
961	{ 0, 0 }
962};
963
964static driver_t pccard_driver = {
965	"pccard",
966	pccard_methods,
967	sizeof(struct pccard_softc)
968};
969
970devclass_t	pccard_devclass;
971
972DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
973DRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0);
974DRIVER_MODULE(pccard, pccbb, pccard_driver, pccard_devclass, 0, 0);
975DRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0);
976MODULE_VERSION(pccard, 1);
977MODULE_DEPEND(pccard, pcic, 1, 1, 1);
978