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