puc.c revision 100425
1/*	$NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $	*/
2
3/*-
4 * Copyright (c) 2002 JF Hay.  All rights reserved.
5 * Copyright (c) 2000 M. Warner Losh.  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 unmodified, this list of conditions, and the following
12 *    disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * Copyright (c) 1996, 1998, 1999
31 *	Christopher G. Demetriou.  All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 *    notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 *    notice, this list of conditions and the following disclaimer in the
40 *    documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 *    must display the following acknowledgement:
43 *      This product includes software developed by Christopher G. Demetriou
44 *	for the NetBSD Project.
45 * 4. The name of the author may not be used to endorse or promote products
46 *    derived from this software without specific prior written permission
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 */
59
60#include <sys/cdefs.h>
61__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 100425 2002-07-21 04:23:40Z imp $");
62
63/*
64 * PCI "universal" communication card device driver, glues com, lpt,
65 * and similar ports to PCI via bridge chip often much larger than
66 * the devices being glued.
67 *
68 * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
69 * sys/dev/pci/pciide.c, revision 1.6).
70 *
71 * These devices could be (and some times are) described as
72 * communications/{serial,parallel}, etc. devices with known
73 * programming interfaces, but those programming interfaces (in
74 * particular the BAR assignments for devices, etc.) in fact are not
75 * particularly well defined.
76 *
77 * After I/we have seen more of these devices, it may be possible
78 * to generalize some of these bits.  In particular, devices which
79 * describe themselves as communications/serial/16[45]50, and
80 * communications/parallel/??? might be attached via direct
81 * 'com' and 'lpt' attachments to pci.
82 */
83
84#include <sys/param.h>
85#include <sys/systm.h>
86#include <sys/kernel.h>
87#include <sys/bus.h>
88#include <sys/conf.h>
89#include <sys/malloc.h>
90
91#include <machine/bus.h>
92#include <machine/resource.h>
93#include <sys/rman.h>
94
95#include <dev/pci/pcireg.h>
96#include <dev/pci/pcivar.h>
97#include <dev/puc/pucvar.h>
98
99#include <opt_puc.h>
100
101struct puc_softc {
102	const struct puc_device_description *sc_desc;
103
104	/* card-global dynamic data */
105	int			barmuxed;
106	int			irqrid;
107	struct resource		*irqres;
108	void			*intr_cookie;
109
110	struct {
111		struct resource	*res;
112	} sc_bar_mappings[PUC_MAX_BAR];
113
114	/* per-port dynamic data */
115        struct {
116		struct device	*dev;
117		/* filled in by bus_setup_intr() */
118		void		(*ihand)(void *);
119		void		*ihandarg;
120        } sc_ports[PUC_MAX_PORTS];
121};
122
123struct puc_device {
124	struct resource_list resources;
125	u_int serialfreq;
126};
127
128static int puc_pci_probe(device_t dev);
129static int puc_pci_attach(device_t dev);
130static void puc_intr(void *arg);
131
132static struct resource *puc_alloc_resource(device_t, device_t, int, int *,
133    u_long, u_long, u_long, u_int);
134static int puc_release_resource(device_t, device_t, int, int,
135    struct resource *);
136static int puc_get_resource(device_t, device_t, int, int, u_long *, u_long *);
137static int puc_setup_intr(device_t, device_t, struct resource *, int,
138    void (*)(void *), void *, void **);
139static int puc_teardown_intr(device_t, device_t, struct resource *,
140    void *);
141static int puc_read_ivar(device_t, device_t, int, uintptr_t *);
142
143static const struct puc_device_description *puc_find_description(uint32_t,
144    uint32_t, uint32_t, uint32_t);
145static void puc_config_superio(device_t);
146static void puc_config_win877(struct resource *);
147static int puc_find_free_unit(char *);
148#ifdef PUC_DEBUG
149static void puc_print_win877(bus_space_tag_t, bus_space_handle_t, u_int,
150    u_int);
151static void puc_print_resource_list(struct resource_list *);
152#endif
153
154static int
155puc_pci_probe(device_t dev)
156{
157	uint32_t v1, v2, d1, d2;
158	const struct puc_device_description *desc;
159
160	if ((pci_read_config(dev, PCIR_HEADERTYPE, 1) & 0x7f) != 0)
161		return (ENXIO);
162
163	v1 = pci_read_config(dev, PCIR_VENDOR, 2);
164	d1 = pci_read_config(dev, PCIR_DEVICE, 2);
165	v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
166	d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
167
168	desc = puc_find_description(v1, d1, v2, d2);
169	if (desc == NULL)
170		return (ENXIO);
171	device_set_desc(dev, desc->name);
172	return (0);
173}
174
175static int
176puc_pci_attach(device_t dev)
177{
178	char *typestr;
179	int bidx, childunit, i, irq_setup, rid;
180	uint32_t v1, v2, d1, d2;
181	struct puc_softc *sc;
182	struct puc_device *pdev;
183	struct resource *res;
184	struct resource_list_entry *rle;
185
186	sc = (struct puc_softc *)device_get_softc(dev);
187	bzero(sc, sizeof(*sc));
188	v1 = pci_read_config(dev, PCIR_VENDOR, 2);
189	d1 = pci_read_config(dev, PCIR_DEVICE, 2);
190	v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
191	d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
192	sc->sc_desc = puc_find_description(v1, d1, v2, d2);
193	if (sc->sc_desc == NULL)
194		return (ENXIO);
195
196#ifdef PUC_DEBUG
197	bootverbose = 1;
198
199	printf("puc: name: %s\n", sc->sc_desc->name);
200#endif
201	rid = 0;
202	res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
203	    RF_ACTIVE | RF_SHAREABLE);
204	if (!res)
205		return (ENXIO);
206
207	sc->irqres = res;
208	sc->irqrid = rid;
209#ifdef PUC_FASTINTR
210	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
211	    INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
212#else
213	irq_setup = ENXIO;
214#endif
215	if (irq_setup != 0)
216		irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
217		    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
218	if (irq_setup != 0)
219		return (ENXIO);
220
221	rid = 0;
222	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
223		if (rid == sc->sc_desc->ports[i].bar)
224			sc->barmuxed = 1;
225		rid = sc->sc_desc->ports[i].bar;
226		bidx = PUC_PORT_BAR_INDEX(rid);
227
228		if (sc->sc_bar_mappings[bidx].res != NULL)
229			continue;
230		res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
231		    0ul, ~0ul, 1, RF_ACTIVE);
232		if (res == NULL) {
233			printf("could not get resource\n");
234			continue;
235		}
236		sc->sc_bar_mappings[bidx].res = res;
237#ifdef PUC_DEBUG
238		printf("port bst %x, start %x, end %x\n",
239		    (u_int)rman_get_bustag(res), (u_int)rman_get_start(res),
240		    (u_int)rman_get_end(res));
241#endif
242	}
243
244	puc_config_superio(dev);
245
246	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
247		rid = sc->sc_desc->ports[i].bar;
248		bidx = PUC_PORT_BAR_INDEX(rid);
249		if (sc->sc_bar_mappings[bidx].res == NULL)
250			continue;
251
252		switch (sc->sc_desc->ports[i].type) {
253		case PUC_PORT_TYPE_COM:
254			typestr = "sio";
255			break;
256		default:
257			continue;
258		}
259		pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
260		    M_NOWAIT | M_ZERO);
261		if (!pdev)
262			continue;
263		resource_list_init(&pdev->resources);
264
265		/* First fake up an IRQ resource. */
266		resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
267		    rman_get_start(sc->irqres), rman_get_end(sc->irqres),
268		    rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
269		rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
270		rle->res = sc->irqres;
271
272		/* Now fake an IOPORT resource */
273		res = sc->sc_bar_mappings[bidx].res;
274		resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0,
275		    rman_get_start(res) + sc->sc_desc->ports[i].offset,
276		    rman_get_end(res) + sc->sc_desc->ports[i].offset + 8 - 1,
277		    8);
278		rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0);
279
280		if (sc->barmuxed == 0) {
281			rle->res = sc->sc_bar_mappings[bidx].res;
282		} else {
283			rle->res = malloc(sizeof(struct resource), M_DEVBUF,
284			    M_WAITOK | M_ZERO);
285			if (rle->res == NULL) {
286				free(pdev, M_DEVBUF);
287				return (ENOMEM);
288			}
289
290			rle->res->r_start = rman_get_start(res) +
291			    sc->sc_desc->ports[i].offset;
292			rle->res->r_end = rle->res->r_start + 8 - 1;
293			rle->res->r_bustag = rman_get_bustag(res);
294			bus_space_subregion(rle->res->r_bustag,
295			    rman_get_bushandle(res),
296			    sc->sc_desc->ports[i].offset, 8,
297			    &rle->res->r_bushandle);
298		}
299
300		pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
301
302		childunit = puc_find_free_unit(typestr);
303		sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
304		if (sc->sc_ports[i].dev == NULL) {
305			if (sc->barmuxed) {
306				bus_space_unmap(rman_get_bustag(rle->res),
307						rman_get_bushandle(rle->res),
308						8);
309				free(rle->res, M_DEVBUF);
310				free(pdev, M_DEVBUF);
311			}
312			continue;
313		}
314		device_set_ivars(sc->sc_ports[i].dev, pdev);
315		device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
316		if (!bootverbose)
317			device_quiet(sc->sc_ports[i].dev);
318#ifdef PUC_DEBUG
319		printf("puc: type %d, bar %x, offset %x\n",
320		    sc->sc_desc->ports[i].type,
321		    sc->sc_desc->ports[i].bar,
322		    sc->sc_desc->ports[i].offset);
323		print_resource_list(&pdev->resources);
324#endif
325		if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
326			if (sc->barmuxed) {
327				bus_space_unmap(rman_get_bustag(rle->res),
328						rman_get_bushandle(rle->res),
329						8);
330				free(rle->res, M_DEVBUF);
331				free(pdev, M_DEVBUF);
332			}
333		}
334	}
335
336#ifdef PUC_DEBUG
337	bootverbose = 0;
338#endif
339	return (0);
340}
341
342/*
343 * This is just an brute force interrupt handler. It just calls all the
344 * registered handlers sequencially.
345 *
346 * Later on we should maybe have a different handler for boards that can
347 * tell us which device generated the interrupt.
348 */
349static void
350puc_intr(void *arg)
351{
352	int i;
353	struct puc_softc *sc;
354
355	sc = (struct puc_softc *)arg;
356	for (i = 0; i < PUC_MAX_PORTS; i++)
357		if (sc->sc_ports[i].ihand != NULL)
358			(sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
359}
360
361static const struct puc_device_description *
362puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
363    uint32_t sprod)
364{
365	int i;
366
367#define checkreg(val, index) \
368    (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
369
370	for (i = 0; puc_devices[i].name != NULL; i++) {
371		if (checkreg(vend, PUC_REG_VEND) &&
372		    checkreg(prod, PUC_REG_PROD) &&
373		    checkreg(svend, PUC_REG_SVEND) &&
374		    checkreg(sprod, PUC_REG_SPROD))
375			return (&puc_devices[i]);
376	}
377
378#undef checkreg
379
380	return (NULL);
381}
382
383/*
384 * It might be possible to make these more generic if we can detect patterns.
385 * For instance maybe if the size of a bar is 0x400 (the old isa space) it
386 * might contain one or more superio chips.
387 */
388static void
389puc_config_superio(device_t dev)
390{
391	struct puc_softc *sc = (struct puc_softc *)device_get_softc(dev);
392
393	if (sc->sc_desc->rval[PUC_REG_VEND] == 0x1592 &&
394	    sc->sc_desc->rval[PUC_REG_PROD] == 0x0781)
395		puc_config_win877(sc->sc_bar_mappings[0].res);
396}
397
398#define rdspio(indx)		(bus_space_write_1(bst, bsh, efir, indx), \
399				bus_space_read_1(bst, bsh, efdr))
400#define wrspio(indx,data)	(bus_space_write_1(bst, bsh, efir, indx), \
401				bus_space_write_1(bst, bsh, efdr, data))
402
403#ifdef PUC_DEBUG
404static void
405puc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir,
406	u_int efdr)
407{
408	u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17;
409	u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32;
410
411	cr00 = rdspio(0x00);
412	cr01 = rdspio(0x01);
413	cr04 = rdspio(0x04);
414	cr09 = rdspio(0x09);
415	cr0d = rdspio(0x0d);
416	cr14 = rdspio(0x14);
417	cr15 = rdspio(0x15);
418	cr16 = rdspio(0x16);
419	cr17 = rdspio(0x17);
420	cr18 = rdspio(0x18);
421	cr19 = rdspio(0x19);
422	cr24 = rdspio(0x24);
423	cr25 = rdspio(0x25);
424	cr28 = rdspio(0x28);
425	cr2c = rdspio(0x2c);
426	cr31 = rdspio(0x31);
427	cr32 = rdspio(0x32);
428	printf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, "
429	    "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, "
430	    "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09,
431	    cr0d, cr14, cr15, cr16, cr17,
432	    cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32);
433}
434#endif
435
436static void
437puc_config_win877(struct resource *res)
438{
439	u_char val;
440	u_int efir, efdr;
441	bus_space_tag_t bst;
442	bus_space_handle_t bsh;
443
444	bst = rman_get_bustag(res);
445	bsh = rman_get_bushandle(res);
446
447	/* configure the first W83877TF */
448	bus_space_write_1(bst, bsh, 0x250, 0x89);
449	efir = 0x251;
450	efdr = 0x252;
451	val = rdspio(0x09) & 0x0f;
452	if (val != 0x0c) {
453		printf("conf_win877: Oops not a W83877TF\n");
454		return;
455	}
456
457#ifdef PUC_DEBUG
458	printf("before: ");
459	puc_print_win877(bst, bsh, efir, efdr);
460#endif
461
462	val = rdspio(0x16);
463	val |= 0x04;
464	wrspio(0x16, val);
465	val &= ~0x04;
466	wrspio(0x16, val);
467
468	wrspio(0x24, 0x2e8 >> 2);
469	wrspio(0x25, 0x2f8 >> 2);
470	wrspio(0x17, 0x03);
471	wrspio(0x28, 0x43);
472
473#ifdef PUC_DEBUG
474	printf("after: ");
475	puc_print_win877(bst, bsh, efir, efdr);
476#endif
477
478	bus_space_write_1(bst, bsh, 0x250, 0xaa);
479
480	/* configure the second W83877TF */
481	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
482	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
483	efir = 0x3f0;
484	efdr = 0x3f1;
485	val = rdspio(0x09) & 0x0f;
486	if (val != 0x0c) {
487		printf("conf_win877: Oops not a W83877TF\n");
488		return;
489	}
490
491#ifdef PUC_DEBUG
492	printf("before: ");
493	puc_print_win877(bst, bsh, efir, efdr);
494#endif
495
496	val = rdspio(0x16);
497	val |= 0x04;
498	wrspio(0x16, val);
499	val &= ~0x04;
500	wrspio(0x16, val);
501
502	wrspio(0x24, 0x3e8 >> 2);
503	wrspio(0x25, 0x3f8 >> 2);
504	wrspio(0x17, 0x03);
505	wrspio(0x28, 0x43);
506
507#ifdef PUC_DEBUG
508	printf("after: ");
509	puc_print_win877(bst, bsh, efir, efdr);
510#endif
511
512	bus_space_write_1(bst, bsh, 0x3f0, 0xaa);
513}
514
515#undef rdspio
516#undef wrspio
517
518static int puc_find_free_unit(char *name)
519{
520	devclass_t dc;
521	int start;
522	int unit;
523
524	unit = 0;
525	start = 0;
526	while (resource_int_value(name, unit, "port", &start) == 0 &&
527	    start > 0)
528		unit++;
529	dc = devclass_find(name);
530	if (dc == NULL)
531		return (-1);
532	while (devclass_get_device(dc, unit))
533		unit++;
534#ifdef PUC_DEBUG
535	printf("puc: Using %s%d\n", name, unit);
536#endif
537	return (unit);
538}
539
540#ifdef PUC_DEBUG
541static void
542puc_print_resource_list(struct resource_list *rl)
543{
544	struct resource_list_entry *rle;
545
546	printf("print_resource_list: rl %p\n", rl);
547	SLIST_FOREACH(rle, rl, link)
548		printf("type %x, rid %x\n", rle->type, rle->rid);
549	printf("print_resource_list: end.\n");
550}
551#endif
552
553static struct resource *
554puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
555    u_long start, u_long end, u_long count, u_int flags)
556{
557	struct puc_device *pdev;
558	struct resource *retval;
559	struct resource_list *rl;
560	struct resource_list_entry *rle;
561
562	pdev = device_get_ivars(child);
563	rl = &pdev->resources;
564
565#ifdef PUC_DEBUG
566	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
567	    pdev, type, *rid);
568	puc_print_resource_list(rl);
569#endif
570	retval = NULL;
571	rle = resource_list_find(rl, type, *rid);
572	if (rle) {
573		start = rle->start;
574		end = rle->end;
575		count = rle->count;
576#ifdef PUC_DEBUG
577		printf("found rle, %lx, %lx, %lx\n", start, end, count);
578#endif
579		retval = rle->res;
580	} else
581		printf("oops rle is gone\n");
582
583	return (retval);
584}
585
586static int
587puc_release_resource(device_t dev, device_t child, int type, int rid,
588    struct resource *res)
589{
590	return (0);
591}
592
593static int
594puc_get_resource(device_t dev, device_t child, int type, int rid,
595    u_long *startp, u_long *countp)
596{
597	struct puc_device *pdev;
598	struct resource_list *rl;
599	struct resource_list_entry *rle;
600
601	pdev = device_get_ivars(child);
602	rl = &pdev->resources;
603
604#ifdef PUC_DEBUG
605	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
606	    type, rid);
607	puc_print_resource_list(rl);
608#endif
609	rle = resource_list_find(rl, type, rid);
610	if (rle) {
611#ifdef PUC_DEBUG
612		printf("found rle %p,", rle);
613#endif
614		if (startp != NULL)
615			*startp = rle->start;
616		if (countp != NULL)
617			*countp = rle->count;
618#ifdef PUC_DEBUG
619		printf(" %lx, %lx\n", rle->start, rle->count);
620#endif
621		return (0);
622	} else
623		printf("oops rle is gone\n");
624	return (ENXIO);
625}
626
627static int
628puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
629	       void (*ihand)(void *), void *arg, void **cookiep)
630{
631	int i;
632	struct puc_softc *sc;
633
634	sc = (struct puc_softc *)device_get_softc(dev);
635	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
636		if (sc->sc_ports[i].dev == child) {
637			if (sc->sc_ports[i].ihand != 0)
638				return (ENXIO);
639			sc->sc_ports[i].ihand = ihand;
640			sc->sc_ports[i].ihandarg = arg;
641			*cookiep = arg;
642			return (0);
643		}
644	}
645	return (ENXIO);
646}
647
648static int
649puc_teardown_intr(device_t dev, device_t child, struct resource *r,
650		  void *cookie)
651{
652	int i;
653	struct puc_softc *sc;
654
655	sc = (struct puc_softc *)device_get_softc(dev);
656	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
657		if (sc->sc_ports[i].dev == child) {
658			sc->sc_ports[i].ihand = NULL;
659			sc->sc_ports[i].ihandarg = NULL;
660			return (0);
661		}
662	}
663	return (ENXIO);
664}
665
666static int
667puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
668{
669	struct puc_device *pdev;
670
671	pdev = device_get_ivars(child);
672	if (pdev == NULL)
673		return (ENOENT);
674
675	switch(index) {
676	case PUC_IVAR_FREQ:
677		*result = pdev->serialfreq;
678		break;
679	default:
680		return (ENOENT);
681	}
682	return (0);
683}
684
685static device_method_t puc_pci_methods[] = {
686    /* Device interface */
687    DEVMETHOD(device_probe,		puc_pci_probe),
688    DEVMETHOD(device_attach,		puc_pci_attach),
689
690    DEVMETHOD(bus_alloc_resource,	puc_alloc_resource),
691    DEVMETHOD(bus_release_resource,	puc_release_resource),
692    DEVMETHOD(bus_get_resource,		puc_get_resource),
693    DEVMETHOD(bus_read_ivar,		puc_read_ivar),
694    DEVMETHOD(bus_setup_intr,		puc_setup_intr),
695    DEVMETHOD(bus_teardown_intr,	puc_teardown_intr),
696    DEVMETHOD(bus_print_child,		bus_generic_print_child),
697    DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
698    { 0, 0 }
699};
700
701static driver_t puc_pci_driver = {
702	"puc",
703	puc_pci_methods,
704	sizeof(struct puc_softc),
705};
706
707static devclass_t puc_devclass;
708
709DRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, 0, 0);
710DRIVER_MODULE(puc, cardbus, puc_pci_driver, puc_devclass, 0, 0);
711