pccard.c revision 67242
1/*	$NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $	*/
2/* $FreeBSD: head/sys/dev/pccard/pccard.c 67242 2000-10-17 06:29: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#else
59#define	DPRINTF(arg)
60#define	DEVPRINTF(arg)
61#endif
62
63#ifdef PCCARDVERBOSE
64int	pccard_verbose = 1;
65#else
66int	pccard_verbose = 0;
67#endif
68
69int	pccard_print(void *, const char *);
70
71int
72pccard_ccr_read(pf, ccr)
73	struct pccard_function *pf;
74	int ccr;
75{
76	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
77	    pf->pf_ccr_offset + ccr));
78}
79
80void
81pccard_ccr_write(pf, ccr, val)
82	struct pccard_function *pf;
83	int ccr;
84	int val;
85{
86
87	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
88		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
89		    pf->pf_ccr_offset + ccr, val);
90	}
91}
92
93static int
94pccard_attach_card(device_t dev)
95{
96	struct pccard_softc *sc = PCCARD_SOFTC(dev);
97	struct pccard_function *pf;
98	struct pccard_ivar *ivar;
99	device_t child;
100	int attached;
101
102	DEVPRINTF((dev, "pccard_card_attach\n"));
103	/*
104	 * this is here so that when socket_enable calls gettype, trt happens
105	 */
106	STAILQ_INIT(&sc->card.pf_head);
107
108	DEVPRINTF((dev, "chip_socket_enable\n"));
109	POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
110
111	DEVPRINTF((dev, "read_cis\n"));
112	pccard_read_cis(sc);
113
114	DEVPRINTF((dev, "check_cis_quirks\n"));
115	pccard_check_cis_quirks(dev);
116
117	/*
118	 * bail now if the card has no functions, or if there was an error in
119	 * the cis.
120	 */
121
122	if (sc->card.error)
123		return (1);
124	if (STAILQ_EMPTY(&sc->card.pf_head))
125		return (1);
126
127	if (1)
128		pccard_print_cis(dev);
129
130	attached = 0;
131
132	DEVPRINTF((dev, "functions scanning\n"));
133	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
134		if (STAILQ_EMPTY(&pf->cfe_head))
135			continue;
136
137		pf->sc = sc;
138		pf->cfe = NULL;
139		pf->dev = NULL;
140	}
141#if 0
142	DEVPRINTF((dev, "chip_socket_disable\n"));
143	POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
144#endif
145
146	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
147		if (STAILQ_EMPTY(&pf->cfe_head))
148			continue;
149		/*
150		 * In NetBSD, the drivers are responsible for activating
151		 * each function of a card.  I think that in FreeBSD we
152		 * want to activate them enough for the usual bus_*_resource
153		 * routines will do the right thing.  This many mean a
154		 * departure from the current NetBSD model.
155		 *
156		 * This could get really ugly for multifunction cards.  But
157		 * it might also just fall out of the FreeBSD resource model.
158		 *
159		 */
160		device_printf(dev, "Starting to attach....\n");
161		ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, M_WAITOK);
162		bzero(ivar, sizeof *ivar);
163		child = device_add_child(dev, NULL, -1);
164		device_set_ivars(child, ivar);
165		ivar->fcn = pf;
166		pf->dev = child;
167		/*
168		 * XXX We might want to move the next two lines into
169		 * XXX the pccard interface layer.  For the moment, this
170		 * XXX is OK, but some drivers want to pick the config
171		 * XXX entry to use as well as some address tweaks (mostly
172		 * XXX due to bugs in decode logic that makes some
173		 * XXX addresses illegal or broken).
174		 */
175		pccard_function_init(pf);
176		pccard_function_enable(pf);
177		device_printf(dev, "pf %p pf->sc %p\n", pf, pf->sc);
178		if (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		for (i = 0; i < cfe->num_iospace; i++) {
351			resource_list_delete(rl, SYS_RES_IOPORT, i);
352			if (cfe->iores[i])
353				bus_release_resource(bus, SYS_RES_IOPORT,
354				    cfe->iorid[i], cfe->iores[i]);
355			cfe->iores[i] = NULL;
356		}
357		if (cfe->irqmask && cfe->irqres) {
358			resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid);
359			bus_release_resource(bus, SYS_RES_IRQ,
360			    cfe->irqrid, cfe->irqres);
361			cfe->irqres = NULL;
362		}
363	}
364}
365
366/* Enable a PCCARD function */
367int
368pccard_function_enable(struct pccard_function *pf)
369{
370	struct pccard_function *tmp;
371	int reg;
372	device_t dev = pf->sc->dev;
373
374	if (pf->cfe == NULL)
375		panic("pccard_function_enable: function not initialized");
376
377	/*
378	 * Increase the reference count on the socket, enabling power, if
379	 * necessary.
380	 */
381	if (pf->sc->sc_enabled_count++ == 0)
382		POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
383	DEVPRINTF((dev, "++enabled_count = %d\n", pf->sc->sc_enabled_count));
384
385	if (pf->pf_flags & PFF_ENABLED) {
386		/*
387		 * Don't do anything if we're already enabled.
388		 */
389		return (0);
390	}
391
392	/*
393	 * it's possible for different functions' CCRs to be in the same
394	 * underlying page.  Check for that.
395	 */
396	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
397		if ((tmp->pf_flags & PFF_ENABLED) &&
398		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
399		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
400		     (tmp->ccr_base - tmp->pf_ccr_offset +
401		      tmp->pf_ccr_realsize))) {
402			pf->pf_ccrt = tmp->pf_ccrt;
403			pf->pf_ccrh = tmp->pf_ccrh;
404			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
405
406			/*
407			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
408			 * tmp->ccr_base) + pf->ccr_base;
409			 */
410			pf->pf_ccr_offset =
411			    (tmp->pf_ccr_offset + pf->ccr_base) -
412			    tmp->ccr_base;
413			pf->pf_ccr_window = tmp->pf_ccr_window;
414			break;
415		}
416	}
417
418	if (tmp == NULL) {
419		pf->ccr_rid = 0;
420		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
421		    &pf->ccr_rid, 0xa0000, 0xdffff, 1 << 10, RF_ACTIVE);
422		if (!pf->ccr_res) {
423			DEVPRINTF((dev, "ccr_res == 0\n"));
424			goto bad;
425		}
426		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
427		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
428		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
429		    pf->ccr_rid, (pf->ccr_rid >> 10) << 10);
430		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
431		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
432		pf->pf_ccr_offset = rman_get_start(pf->ccr_res);
433		pf->pf_ccr_realsize = 1;
434	}
435
436	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
437	reg |= PCCARD_CCR_OPTION_LEVIREQ;
438	if (pccard_mfc(pf->sc)) {
439		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
440			PCCARD_CCR_OPTION_ADDR_DECODE);
441		/*
442		 * XXX Need to enable PCCARD_CCR_OPTION_IRQ_ENABLE if
443		 * XXX we have an interrupt handler, but we don't know that
444		 * XXX at this point.
445		 */
446#if 0
447		if (pf->ih_fct)
448			reg |= PCCARD_CCR_OPTION_IREQ_ENABLE;
449#endif
450	}
451	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
452
453	reg = 0;
454	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
455		reg |= PCCARD_CCR_STATUS_IOIS8;
456	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
457		reg |= PCCARD_CCR_STATUS_AUDIO;
458	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
459
460	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
461
462	if (pccard_mfc(pf->sc)) {
463		long tmp, iosize;
464
465		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
466		/* round up to nearest (2^n)-1 */
467		for (iosize = 1; iosize < tmp; iosize <<= 1)
468			;
469		iosize--;
470
471		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
472				 pf->pf_mfc_iobase & 0xff);
473		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
474				 (pf->pf_mfc_iobase >> 8) & 0xff);
475		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
476		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
477
478		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
479	}
480
481#ifdef PCCARDDEBUG
482	if (pccard_debug) {
483		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
484			device_printf(tmp->sc->dev,
485			    "function %d CCR at %d offset %x: "
486			    "%x %x %x %x, %x %x %x %x, %x\n",
487			    tmp->number, tmp->pf_ccr_window,
488			    tmp->pf_ccr_offset,
489			    pccard_ccr_read(tmp, 0x00),
490			    pccard_ccr_read(tmp, 0x02),
491			    pccard_ccr_read(tmp, 0x04),
492			    pccard_ccr_read(tmp, 0x06),
493			    pccard_ccr_read(tmp, 0x0A),
494			    pccard_ccr_read(tmp, 0x0C),
495			    pccard_ccr_read(tmp, 0x0E),
496			    pccard_ccr_read(tmp, 0x10),
497			    pccard_ccr_read(tmp, 0x12));
498		}
499	}
500#endif
501	pf->pf_flags |= PFF_ENABLED;
502	return (0);
503
504 bad:
505	/*
506	 * Decrement the reference count, and power down the socket, if
507	 * necessary.
508	 */
509	if (--pf->sc->sc_enabled_count == 0)
510		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
511	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
512
513	return (1);
514}
515
516/* Disable PCCARD function. */
517void
518pccard_function_disable(struct pccard_function *pf)
519{
520	struct pccard_function *tmp;
521	device_t dev = pf->sc->dev;
522
523	if (pf->cfe == NULL)
524		panic("pccard_function_disable: function not initialized");
525
526	if ((pf->pf_flags & PFF_ENABLED) == 0) {
527		/*
528		 * Don't do anything if we're already disabled.
529		 */
530		return;
531	}
532
533	/*
534	 * it's possible for different functions' CCRs to be in the same
535	 * underlying page.  Check for that.  Note we mark us as disabled
536	 * first to avoid matching ourself.
537	 */
538
539	pf->pf_flags &= ~PFF_ENABLED;
540	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
541		if ((tmp->pf_flags & PFF_ENABLED) &&
542		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
543		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
544		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
545			break;
546	}
547
548	/* Not used by anyone else; unmap the CCR. */
549	if (tmp == NULL) {
550		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
551		    pf->ccr_res);
552		pf->ccr_res = NULL;
553	}
554
555	/*
556	 * Decrement the reference count, and power down the socket, if
557	 * necessary.
558	 */
559	if (--pf->sc->sc_enabled_count == 0)
560		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
561	DEVPRINTF((dev, "--enabled_count = %d\n", pf->sc->sc_enabled_count));
562}
563
564#if 0
565/* XXX These functions are needed, but not like this XXX */
566int
567pccard_io_map(struct pccard_function *pf, int width, bus_addr_t offset,
568    bus_size_t size, struct pccard_io_handle *pcihp, int *windowp)
569{
570	int reg;
571
572	if (pccard_chip_io_map(pf->sc->pct, pf->sc->pch, width, offset, size,
573	    pcihp, windowp))
574		return (1);
575
576	/*
577	 * XXX in the multifunction multi-iospace-per-function case, this
578	 * needs to cooperate with io_alloc to make sure that the spaces
579	 * don't overlap, and that the ccr's are set correctly
580	 */
581
582	if (pccard_mfc(pf->sc)) {
583		long tmp, iosize;
584
585		if (pf->pf_mfc_iomax == 0) {
586			pf->pf_mfc_iobase = pcihp->addr + offset;
587			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
588		} else {
589			/* this makes the assumption that nothing overlaps */
590			if (pf->pf_mfc_iobase > pcihp->addr + offset)
591				pf->pf_mfc_iobase = pcihp->addr + offset;
592			if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
593				pf->pf_mfc_iomax = pcihp->addr + offset + size;
594		}
595
596		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
597		/* round up to nearest (2^n)-1 */
598		for (iosize = 1; iosize >= tmp; iosize <<= 1)
599			;
600		iosize--;
601
602		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
603		    pf->pf_mfc_iobase & 0xff);
604		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
605		    (pf->pf_mfc_iobase >> 8) & 0xff);
606		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
607		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
608
609		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
610
611		reg = pccard_ccr_read(pf, PCCARD_CCR_OPTION);
612		reg |= PCCARD_CCR_OPTION_ADDR_DECODE;
613		pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
614	}
615	return (0);
616}
617
618void
619pccard_io_unmap(struct pccard_function *pf, int window)
620{
621
622	pccard_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
623
624	/* XXX Anything for multi-function cards? */
625}
626#endif
627
628/*
629 * simulate the old "probe" routine.  In the new world order, the driver
630 * needs to grab devices while in the old they were assigned to the device by
631 * the pccardd process.  These symbols are exported to the upper layers.
632 */
633int
634pccard_compat_probe(device_t dev)
635{
636	return (CARD_COMPAT_MATCH(dev));
637}
638
639int
640pccard_compat_attach(device_t dev)
641{
642	int err;
643
644	err = CARD_COMPAT_PROBE(dev);
645	if (err == 0)
646		err = CARD_COMPAT_ATTACH(dev);
647	return (err);
648}
649
650#define PCCARD_NPORT	2
651#define PCCARD_NMEM	5
652#define PCCARD_NIRQ	1
653#define PCCARD_NDRQ	0
654
655static int
656pccard_add_children(device_t dev, int busno)
657{
658	/* Call parent to scan for any current children */
659	return 0;
660}
661
662static int
663pccard_probe(device_t dev)
664{
665	device_set_desc(dev, "PC Card bus -- newconfig version");
666	return pccard_add_children(dev, device_get_unit(dev));
667}
668
669static int
670pccard_attach(device_t dev)
671{
672	struct pccard_softc *sc = PCCARD_SOFTC(dev);
673
674	sc->dev = dev;
675	sc->sc_enabled_count = 0;
676	DEVPRINTF((dev, "pccard_attach %p\n", dev));
677	return bus_generic_attach(dev);
678}
679
680static void
681pccard_print_resources(struct resource_list *rl, const char *name, int type,
682    int count, const char *format)
683{
684	struct resource_list_entry *rle;
685	int printed;
686	int i;
687
688	printed = 0;
689	for (i = 0; i < count; i++) {
690		rle = resource_list_find(rl, type, i);
691		if (rle) {
692			if (printed == 0)
693				printf(" %s ", name);
694			else if (printed > 0)
695				printf(",");
696			printed++;
697			printf(format, rle->start);
698			if (rle->count > 1) {
699				printf("-");
700				printf(format, rle->start + rle->count - 1);
701			}
702		} else if (i > 3) {
703			/* check the first few regardless */
704			break;
705		}
706	}
707}
708
709static int
710pccard_print_child(device_t dev, device_t child)
711{
712	struct pccard_ivar *devi = PCCARD_IVAR(child);
713	struct resource_list *rl = &devi->resources;
714	int retval = 0;
715
716	retval += bus_print_child_header(dev, child);
717	retval += printf(" at");
718
719	if (devi) {
720		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
721		    PCCARD_NPORT, "%#lx");
722		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
723		    PCCARD_NMEM, "%#lx");
724		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
725		    "%ld");
726		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
727		    "%ld");
728	}
729
730	retval += bus_print_child_footer(dev, child);
731
732	return (retval);
733}
734
735static int
736pccard_set_resource(device_t dev, device_t child, int type, int rid,
737		 u_long start, u_long count)
738{
739	struct pccard_ivar *devi = PCCARD_IVAR(child);
740	struct resource_list *rl = &devi->resources;
741
742	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
743	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
744		return EINVAL;
745	if (rid < 0)
746		return EINVAL;
747	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
748		return EINVAL;
749	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
750		return EINVAL;
751	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
752		return EINVAL;
753	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
754		return EINVAL;
755
756	resource_list_add(rl, type, rid, start, start + count - 1, count);
757
758	return 0;
759}
760
761static int
762pccard_get_resource(device_t dev, device_t child, int type, int rid,
763    u_long *startp, u_long *countp)
764{
765	struct pccard_ivar *devi = PCCARD_IVAR(child);
766	struct resource_list *rl = &devi->resources;
767	struct resource_list_entry *rle;
768
769	rle = resource_list_find(rl, type, rid);
770	if (!rle)
771		return ENOENT;
772
773	if (startp)
774		*startp = rle->start;
775	if (countp)
776		*countp = rle->count;
777
778	return 0;
779}
780
781static void
782pccard_delete_resource(device_t dev, device_t child, int type, int rid)
783{
784	struct pccard_ivar *devi = PCCARD_IVAR(child);
785	struct resource_list *rl = &devi->resources;
786	resource_list_delete(rl, type, rid);
787}
788
789static int
790pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
791    u_int32_t flags)
792{
793	return CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
794	    rid, flags);
795}
796
797static int
798pccard_set_memory_offset(device_t dev, device_t child, int rid,
799     u_int32_t offset)
800{
801	return CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
802	    offset);
803}
804
805static int
806pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
807{
808	struct pccard_ivar *devi = PCCARD_IVAR(child);
809	struct pccard_function *func = devi->fcn;
810	struct pccard_softc *sc = PCCARD_SOFTC(bus);
811
812	/* PCCARD_IVAR_ETHADDR unhandled from oldcard */
813	switch (which) {
814	default:
815	case PCCARD_IVAR_ETHADDR:
816		return (ENOENT);
817		break;
818	case PCCARD_IVAR_VENDOR:
819		*(u_int32_t *) result = sc->card.manufacturer;
820		break;
821	case PCCARD_IVAR_PRODUCT:
822		*(u_int32_t *) result = sc->card.product;
823		break;
824	case PCCARD_IVAR_FUNCTION_NUMBER:
825		if (!func) {
826			device_printf(bus, "No function number, bug!\n");
827			return (ENOENT);
828		}
829		*(u_int32_t *) result = func->number;
830		break;
831	case PCCARD_IVAR_VENDOR_STR:
832		*(char **) result = sc->card.cis1_info[0];
833		break;
834	case PCCARD_IVAR_PRODUCT_STR:
835		*(char **) result = sc->card.cis1_info[1];
836		break;
837	case PCCARD_IVAR_CIS3_STR:
838		*(char **) result = sc->card.cis1_info[2];
839		break;
840	case PCCARD_IVAR_CIS4_STR:
841		*(char **) result = sc->card.cis1_info[2];
842		break;
843	}
844	return (0);
845}
846
847static void
848pccard_driver_added(device_t dev, driver_t *driver)
849{
850	/*
851	 * XXX eventually we need to attach stuff when we know we
852	 * XXX have kids.  For now we do nothing because we normally
853	 * XXX add children ourselves.  We don't want to necessarily
854	 * XXX force a reprobe.
855	 */
856}
857
858static struct resource *
859pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
860    u_long start, u_long end, u_long count, u_int flags)
861{
862	struct pccard_ivar *ivar;
863	struct pccard_function *pf;
864
865	if (device_get_parent(child) == dev) {
866		ivar = PCCARD_IVAR(child);
867		pf = ivar->fcn;
868		switch (type) {
869		case SYS_RES_IRQ:
870			if (*rid > 0)
871				return NULL;
872			return (pf->cfe->irqres);
873		case SYS_RES_IOPORT:
874			if (*rid > 3)	/* XXX */
875				return NULL;
876			return (pf->cfe->iores[*rid]);
877		}
878	}
879	return (bus_generic_alloc_resource(dev, child, type, rid, start,
880	    end, count, flags));
881}
882
883static int
884pccard_release_resource(device_t dev, device_t child, int type, int rid,
885    struct resource *r)
886{
887	return bus_generic_release_resource(dev, child, type, rid, r);
888}
889
890static int
891pccard_activate_resource(device_t dev, device_t child, int type, int rid,
892    struct resource *r)
893{
894	/* XXX need to write to the COR to activate this */
895	return (bus_generic_activate_resource(dev, child, type, rid, r));
896}
897
898static int
899pccard_deactivate_resource(device_t dev, device_t child, int type, int rid,
900    struct resource *r)
901{
902	/* XXX need to write to the COR to deactivate this */
903	return (bus_generic_deactivate_resource(dev, child, type, rid, r));
904}
905
906static device_method_t pccard_methods[] = {
907	/* Device interface */
908	DEVMETHOD(device_probe,		pccard_probe),
909	DEVMETHOD(device_attach,	pccard_attach),
910	DEVMETHOD(device_detach,	bus_generic_detach),
911	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
912	DEVMETHOD(device_suspend,	bus_generic_suspend),
913	DEVMETHOD(device_resume,	bus_generic_resume),
914
915	/* Bus interface */
916	DEVMETHOD(bus_print_child,	pccard_print_child),
917	DEVMETHOD(bus_driver_added,	pccard_driver_added),
918	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
919	DEVMETHOD(bus_release_resource,	pccard_release_resource),
920	DEVMETHOD(bus_activate_resource, pccard_activate_resource),
921	DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
922	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
923	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
924	DEVMETHOD(bus_set_resource,	pccard_set_resource),
925	DEVMETHOD(bus_get_resource,	pccard_get_resource),
926	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
927	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
928
929	/* Card Interface */
930	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
931	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
932	DEVMETHOD(card_get_type,	pccard_card_gettype),
933	DEVMETHOD(card_attach_card,	pccard_attach_card),
934	DEVMETHOD(card_detach_card,	pccard_detach_card),
935
936	{ 0, 0 }
937};
938
939static driver_t pccard_driver = {
940	"pccard",
941	pccard_methods,
942	sizeof(struct pccard_softc)
943};
944
945devclass_t	pccard_devclass;
946
947DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
948DRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0);
949DRIVER_MODULE(pccard, pccbb, pccard_driver, pccard_devclass, 0, 0);
950DRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0);
951MODULE_VERSION(pccard, 1);
952MODULE_DEPEND(pccard, pcic, 1, 1, 1);
953