puc.c revision 90731
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 90731 2002-02-16 15:12:14Z jhay $");
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#if __FreeBSD_version < 500000
96#include <pci/pcireg.h>
97#include <pci/pcivar.h>
98#else
99#include <dev/pci/pcireg.h>
100#include <dev/pci/pcivar.h>
101#endif
102#include <dev/puc/pucvar.h>
103
104#include <opt_puc.h>
105
106struct puc_softc {
107	const struct puc_device_description *sc_desc;
108
109	/* card-global dynamic data */
110	int			barmuxed;
111	int			irqrid;
112	struct resource		*irqres;
113	void			*intr_cookie;
114
115	struct {
116		struct resource	*res;
117	} sc_bar_mappings[PUC_MAX_BAR];
118
119	/* per-port dynamic data */
120        struct {
121		struct device	*dev;
122		/* filled in by bus_setup_intr() */
123		void		(*ihand) __P((void *));
124		void		*ihandarg;
125        } sc_ports[PUC_MAX_PORTS];
126};
127
128struct puc_device {
129	struct resource_list resources;
130	u_int serialfreq;
131};
132
133static int puc_pci_probe(device_t dev);
134static int puc_pci_attach(device_t dev);
135static void puc_intr(void *arg);
136
137static struct resource *puc_alloc_resource(device_t, device_t, int, int *,
138    u_long, u_long, u_long, u_int);
139static int puc_release_resource(device_t, device_t, int, int,
140    struct resource *);
141static int puc_get_resource(device_t, device_t, int, int, u_long *, u_long *);
142static int puc_setup_intr(device_t, device_t, struct resource *, int,
143    void (*)(void *), void *, void **);
144static int puc_teardown_intr(device_t, device_t, struct resource *,
145    void *);
146static int puc_read_ivar(device_t, device_t, int, uintptr_t *);
147
148static const struct puc_device_description *puc_find_description(uint32_t,
149    uint32_t, uint32_t, uint32_t);
150static void puc_config_superio(device_t);
151static void puc_config_win877(struct resource *);
152static int puc_find_free_unit(char *);
153#ifdef PUC_DEBUG
154static void puc_print_win877(bus_space_tag_t, bus_space_handle_t, u_int,
155    u_int);
156static void puc_print_resource_list(struct resource_list *);
157#endif
158
159static int
160puc_pci_probe(device_t dev)
161{
162	uint32_t v1, v2, d1, d2;
163	const struct puc_device_description *desc;
164
165	if (pci_read_config(dev, PCIR_HEADERTYPE, 1) != 0)
166		return (ENXIO);
167
168	v1 = pci_read_config(dev, PCIR_VENDOR, 2);
169	d1 = pci_read_config(dev, PCIR_DEVICE, 2);
170	v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
171	d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
172
173	desc = puc_find_description(v1, d1, v2, d2);
174	if (desc == NULL)
175		return (ENXIO);
176	device_set_desc(dev, desc->name);
177	return (0);
178}
179
180static int
181puc_pci_attach(device_t dev)
182{
183	char *typestr;
184	int bidx, childunit, i, irq_setup, rid;
185	uint32_t v1, v2, d1, d2;
186	struct puc_softc *sc;
187	struct puc_device *pdev;
188	struct resource *res;
189	struct resource_list_entry *rle;
190
191	sc = (struct puc_softc *)device_get_softc(dev);
192	bzero(sc, sizeof(*sc));
193	v1 = pci_read_config(dev, PCIR_VENDOR, 2);
194	d1 = pci_read_config(dev, PCIR_DEVICE, 2);
195	v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
196	d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
197	sc->sc_desc = puc_find_description(v1, d1, v2, d2);
198	if (sc->sc_desc == NULL)
199		return (ENXIO);
200
201#ifdef PUC_DEBUG
202	bootverbose = 1;
203
204	printf("puc: name: %s\n", sc->sc_desc->name);
205#endif
206	rid = 0;
207	res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
208	    RF_ACTIVE | RF_SHAREABLE);
209	if (!res)
210		return (ENXIO);
211
212	sc->irqres = res;
213	sc->irqrid = rid;
214#ifdef PUC_FASTINTR
215	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
216	    INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
217#else
218	irq_setup = ENXIO;
219#endif
220	if (irq_setup != 0)
221		irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
222		    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
223	if (irq_setup != 0)
224		return (ENXIO);
225
226	rid = 0;
227	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
228		if (rid == sc->sc_desc->ports[i].bar)
229			sc->barmuxed = 1;
230		rid = sc->sc_desc->ports[i].bar;
231		bidx = PUC_PORT_BAR_INDEX(rid);
232
233		if (sc->sc_bar_mappings[bidx].res != NULL)
234			continue;
235		res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
236		    0ul, ~0ul, 1, RF_ACTIVE);
237		if (res == NULL) {
238			printf("could not get resource\n");
239			continue;
240		}
241		sc->sc_bar_mappings[bidx].res = res;
242#ifdef PUC_DEBUG
243		printf("port bst %x, start %x, end %x\n",
244		    (u_int)rman_get_bustag(res), (u_int)rman_get_start(res),
245		    (u_int)rman_get_end(res));
246#endif
247	}
248
249	puc_config_superio(dev);
250
251	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
252		rid = sc->sc_desc->ports[i].bar;
253		bidx = PUC_PORT_BAR_INDEX(rid);
254		if (sc->sc_bar_mappings[bidx].res == NULL)
255			continue;
256
257		switch (sc->sc_desc->ports[i].type) {
258		case PUC_PORT_TYPE_COM:
259			typestr = "sio";
260			break;
261		default:
262			continue;
263		}
264		pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
265		    M_NOWAIT | M_ZERO);
266		if (!pdev)
267			continue;
268		resource_list_init(&pdev->resources);
269
270		/* First fake up an IRQ resource. */
271		resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
272		    rman_get_start(sc->irqres), rman_get_end(sc->irqres),
273		    rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
274		rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
275		rle->res = sc->irqres;
276
277		/* Now fake an IOPORT resource */
278		res = sc->sc_bar_mappings[bidx].res;
279		resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0,
280		    rman_get_start(res) + sc->sc_desc->ports[i].offset,
281		    rman_get_end(res) + sc->sc_desc->ports[i].offset + 8 - 1,
282		    8);
283		rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0);
284
285		if (sc->barmuxed == 0) {
286			rle->res = sc->sc_bar_mappings[bidx].res;
287		} else {
288			rle->res = malloc(sizeof(struct resource), M_DEVBUF,
289			    M_WAITOK | M_ZERO);
290			if (rle->res == NULL)
291				return (ENOMEM);
292
293			rle->res->r_start = rman_get_start(res) +
294			    sc->sc_desc->ports[i].offset;
295			rle->res->r_end = rle->res->r_start + 8 - 1;
296			rle->res->r_bustag = rman_get_bustag(res);
297			bus_space_subregion(rle->res->r_bustag,
298			    rman_get_bushandle(res),
299			    sc->sc_desc->ports[i].offset, 8,
300			    &rle->res->r_bushandle);
301		}
302
303		pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
304
305		childunit = puc_find_free_unit(typestr);
306		sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
307		if (sc->sc_ports[i].dev == NULL)
308			continue;
309		device_set_ivars(sc->sc_ports[i].dev, pdev);
310		device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
311		if (!bootverbose)
312			device_quiet(sc->sc_ports[i].dev);
313#ifdef PUC_DEBUG
314		printf("puc: type %d, bar %x, offset %x\n",
315		    sc->sc_desc->ports[i].type,
316		    sc->sc_desc->ports[i].bar,
317		    sc->sc_desc->ports[i].offset);
318		print_resource_list(&pdev->resources);
319#endif
320		device_probe_and_attach(sc->sc_ports[i].dev);
321	}
322
323#ifdef PUC_DEBUG
324	bootverbose = 0;
325#endif
326	return (0);
327}
328
329/*
330 * This is just an brute force interrupt handler. It just calls all the
331 * registered handlers sequencially.
332 *
333 * Later on we should maybe have a different handler for boards that can
334 * tell us which device generated the interrupt.
335 */
336static void
337puc_intr(void *arg)
338{
339	int i;
340	struct puc_softc *sc;
341
342	sc = (struct puc_softc *)arg;
343	for (i = 0; i < PUC_MAX_PORTS; i++)
344		if (sc->sc_ports[i].ihand != NULL)
345			(sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
346}
347
348static const struct puc_device_description *
349puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
350    uint32_t sprod)
351{
352	int i;
353
354#define checkreg(val, index) \
355    (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
356
357	for (i = 0; puc_devices[i].name != NULL; i++) {
358		if (checkreg(vend, PUC_REG_VEND) &&
359		    checkreg(prod, PUC_REG_PROD) &&
360		    checkreg(svend, PUC_REG_SVEND) &&
361		    checkreg(sprod, PUC_REG_SPROD))
362			return (&puc_devices[i]);
363	}
364
365#undef checkreg
366
367	return (NULL);
368}
369
370/*
371 * It might be possible to make these more generic if we can detect patterns.
372 * For instance maybe if the size of a bar is 0x400 (the old isa space) it
373 * might contain one or more superio chips.
374 */
375static void
376puc_config_superio(device_t dev)
377{
378	struct puc_softc *sc = (struct puc_softc *)device_get_softc(dev);
379
380	if (sc->sc_desc->rval[PUC_REG_VEND] == 0x1592 &&
381	    sc->sc_desc->rval[PUC_REG_PROD] == 0x0781)
382		puc_config_win877(sc->sc_bar_mappings[0].res);
383}
384
385#define rdspio(indx)		(bus_space_write_1(bst, bsh, efir, indx), \
386				bus_space_read_1(bst, bsh, efdr))
387#define wrspio(indx,data)	(bus_space_write_1(bst, bsh, efir, indx), \
388				bus_space_write_1(bst, bsh, efdr, data))
389
390#ifdef PUC_DEBUG
391static void
392puc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir,
393	u_int efdr)
394{
395	u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17;
396	u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32;
397
398	cr00 = rdspio(0x00);
399	cr01 = rdspio(0x01);
400	cr04 = rdspio(0x04);
401	cr09 = rdspio(0x09);
402	cr0d = rdspio(0x0d);
403	cr14 = rdspio(0x14);
404	cr15 = rdspio(0x15);
405	cr16 = rdspio(0x16);
406	cr17 = rdspio(0x17);
407	cr18 = rdspio(0x18);
408	cr19 = rdspio(0x19);
409	cr24 = rdspio(0x24);
410	cr25 = rdspio(0x25);
411	cr28 = rdspio(0x28);
412	cr2c = rdspio(0x2c);
413	cr31 = rdspio(0x31);
414	cr32 = rdspio(0x32);
415	printf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, "
416	    "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, "
417	    "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09,
418	    cr0d, cr14, cr15, cr16, cr17,
419	    cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32);
420}
421#endif
422
423static void
424puc_config_win877(struct resource *res)
425{
426	u_char val;
427	u_int efir, efdr;
428	bus_space_tag_t bst;
429	bus_space_handle_t bsh;
430
431	bst = rman_get_bustag(res);
432	bsh = rman_get_bushandle(res);
433
434	/* configure the first W83877TF */
435	bus_space_write_1(bst, bsh, 0x250, 0x89);
436	efir = 0x251;
437	efdr = 0x252;
438	val = rdspio(0x09) & 0x0f;
439	if (val != 0x0c) {
440		printf("conf_win877: Oops not a W83877TF\n");
441		return;
442	}
443
444#ifdef PUC_DEBUG
445	printf("before: ");
446	puc_print_win877(bst, bsh, efir, efdr);
447#endif
448
449	val = rdspio(0x16);
450	val |= 0x04;
451	wrspio(0x16, val);
452	val &= ~0x04;
453	wrspio(0x16, val);
454
455	wrspio(0x24, 0x2e8 >> 2);
456	wrspio(0x25, 0x2f8 >> 2);
457	wrspio(0x17, 0x03);
458	wrspio(0x28, 0x43);
459
460#ifdef PUC_DEBUG
461	printf("after: ");
462	puc_print_win877(bst, bsh, efir, efdr);
463#endif
464
465	bus_space_write_1(bst, bsh, 0x250, 0xaa);
466
467	/* configure the second W83877TF */
468	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
469	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
470	efir = 0x3f0;
471	efdr = 0x3f1;
472	val = rdspio(0x09) & 0x0f;
473	if (val != 0x0c) {
474		printf("conf_win877: Oops not a W83877TF\n");
475		return;
476	}
477
478#ifdef PUC_DEBUG
479	printf("before: ");
480	puc_print_win877(bst, bsh, efir, efdr);
481#endif
482
483	val = rdspio(0x16);
484	val |= 0x04;
485	wrspio(0x16, val);
486	val &= ~0x04;
487	wrspio(0x16, val);
488
489	wrspio(0x24, 0x3e8 >> 2);
490	wrspio(0x25, 0x3f8 >> 2);
491	wrspio(0x17, 0x03);
492	wrspio(0x28, 0x43);
493
494#ifdef PUC_DEBUG
495	printf("after: ");
496	puc_print_win877(bst, bsh, efir, efdr);
497#endif
498
499	bus_space_write_1(bst, bsh, 0x3f0, 0xaa);
500}
501
502#undef rdspio
503#undef wrspio
504
505static int puc_find_free_unit(char *name)
506{
507	devclass_t dc;
508	int start;
509	int unit;
510
511	unit = 0;
512	start = 0;
513	while (resource_int_value(name, unit, "port", &start) == 0 &&
514	    start > 0)
515		unit++;
516	dc = devclass_find(name);
517	if (dc == NULL)
518		return (-1);
519	while (devclass_get_device(dc, unit))
520		unit++;
521#ifdef PUC_DEBUG
522	printf("puc: Using %s%d\n", name, unit);
523#endif
524	return (unit);
525}
526
527#ifdef PUC_DEBUG
528static void
529puc_print_resource_list(struct resource_list *rl)
530{
531	struct resource_list_entry *rle;
532
533	printf("print_resource_list: rl %p\n", rl);
534	SLIST_FOREACH(rle, rl, link)
535		printf("type %x, rid %x\n", rle->type, rle->rid);
536	printf("print_resource_list: end.\n");
537}
538#endif
539
540/* XXX
541 * This function should be part of the general bus_space_* functions. It
542 * should also handle the PC98 case where bus_space_handle_t is a structure.
543 */
544int
545bus_space_subregion(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset,
546    bus_size_t s, bus_space_handle_t *newhandle)
547{
548	*newhandle = h + offset;
549	return (0);
550}
551
552static struct resource *
553puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
554    u_long start, u_long end, u_long count, u_int flags)
555{
556	struct puc_device *pdev;
557	struct resource *retval;
558	struct resource_list *rl;
559	struct resource_list_entry *rle;
560
561	pdev = device_get_ivars(child);
562	rl = &pdev->resources;
563
564#ifdef PUC_DEBUG
565	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
566	    pdev, type, *rid);
567	puc_print_resource_list(rl);
568#endif
569	retval = NULL;
570	rle = resource_list_find(rl, type, *rid);
571	if (rle) {
572		start = rle->start;
573		end = rle->end;
574		count = rle->count;
575#ifdef PUC_DEBUG
576		printf("found rle, %lx, %lx, %lx\n", start, end, count);
577#endif
578		retval = rle->res;
579	} else
580		printf("oops rle is gone\n");
581
582	return (retval);
583}
584
585static int
586puc_release_resource(device_t dev, device_t child, int type, int rid,
587    struct resource *res)
588{
589	return (0);
590}
591
592static int
593puc_get_resource(device_t dev, device_t child, int type, int rid,
594    u_long *startp, u_long *countp)
595{
596	struct puc_device *pdev;
597	struct resource_list *rl;
598	struct resource_list_entry *rle;
599
600	pdev = device_get_ivars(child);
601	rl = &pdev->resources;
602
603#ifdef PUC_DEBUG
604	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
605	    type, rid);
606	puc_print_resource_list(rl);
607#endif
608	rle = resource_list_find(rl, type, rid);
609	if (rle) {
610#ifdef PUC_DEBUG
611		printf("found rle %p,", rle);
612#endif
613		if (startp != NULL)
614			*startp = rle->start;
615		if (countp != NULL)
616			*countp = rle->count;
617#ifdef PUC_DEBUG
618		printf(" %lx, %lx\n", rle->start, rle->count);
619#endif
620		return (0);
621	} else
622		printf("oops rle is gone\n");
623	return (ENXIO);
624}
625
626static int
627puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
628	       void (*ihand)(void *), void *arg, void **cookiep)
629{
630	int i;
631	struct puc_softc *sc;
632
633	sc = (struct puc_softc *)device_get_softc(dev);
634	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
635		if (sc->sc_ports[i].dev == child) {
636			if (sc->sc_ports[i].ihand != 0)
637				return (ENXIO);
638			sc->sc_ports[i].ihand = ihand;
639			sc->sc_ports[i].ihandarg = arg;
640			*cookiep = arg;
641			return (0);
642		}
643	}
644	return (ENXIO);
645}
646
647static int
648puc_teardown_intr(device_t dev, device_t child, struct resource *r,
649		  void *cookie)
650{
651	int i;
652	struct puc_softc *sc;
653
654	sc = (struct puc_softc *)device_get_softc(dev);
655	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
656		if (sc->sc_ports[i].dev == child) {
657			sc->sc_ports[i].ihand = NULL;
658			sc->sc_ports[i].ihandarg = NULL;
659			return (0);
660		}
661	}
662	return (ENXIO);
663}
664
665static int
666puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
667{
668	struct puc_device *pdev;
669
670	pdev = device_get_ivars(child);
671	if (pdev == NULL)
672		return (ENOENT);
673
674	switch(index) {
675	case PUC_IVAR_FREQ:
676		*result = pdev->serialfreq;
677		break;
678	default:
679		return (ENOENT);
680	}
681	return (0);
682}
683
684static device_method_t puc_pci_methods[] = {
685    /* Device interface */
686    DEVMETHOD(device_probe,		puc_pci_probe),
687    DEVMETHOD(device_attach,		puc_pci_attach),
688
689    DEVMETHOD(bus_alloc_resource,	puc_alloc_resource),
690    DEVMETHOD(bus_release_resource,	puc_release_resource),
691    DEVMETHOD(bus_get_resource,		puc_get_resource),
692    DEVMETHOD(bus_read_ivar,		puc_read_ivar),
693    DEVMETHOD(bus_setup_intr,		puc_setup_intr),
694    DEVMETHOD(bus_teardown_intr,	puc_teardown_intr),
695    DEVMETHOD(bus_print_child,		bus_generic_print_child),
696    DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
697    { 0, 0 }
698};
699
700static driver_t puc_pci_driver = {
701	"puc",
702	puc_pci_methods,
703	sizeof(struct puc_softc),
704};
705
706static devclass_t puc_devclass;
707
708DRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, 0, 0);
709