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