xref: /freebsd/sys/dev/puc/puc.c (revision fd8e4ebc8c18caec3eefac6527831f9ee6a92959)
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$");
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 
106 struct 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 
128 struct puc_device {
129 	struct resource_list resources;
130 	u_int serialfreq;
131 };
132 
133 static int puc_pci_probe(device_t dev);
134 static int puc_pci_attach(device_t dev);
135 static void puc_intr(void *arg);
136 
137 static struct resource *puc_alloc_resource(device_t, device_t, int, int *,
138     u_long, u_long, u_long, u_int);
139 static int puc_release_resource(device_t, device_t, int, int,
140     struct resource *);
141 static int puc_get_resource(device_t, device_t, int, int, u_long *, u_long *);
142 static int puc_setup_intr(device_t, device_t, struct resource *, int,
143     void (*)(void *), void *, void **);
144 static int puc_teardown_intr(device_t, device_t, struct resource *,
145     void *);
146 static int puc_read_ivar(device_t, device_t, int, uintptr_t *);
147 
148 static const struct puc_device_description *puc_find_description(uint32_t,
149     uint32_t, uint32_t, uint32_t);
150 static void puc_config_superio(device_t);
151 static void puc_config_win877(struct resource *);
152 static int puc_find_free_unit(char *);
153 #ifdef PUC_DEBUG
154 static void puc_print_win877(bus_space_tag_t, bus_space_handle_t, u_int,
155     u_int);
156 static void puc_print_resource_list(struct resource_list *);
157 #endif
158 
159 static int
160 puc_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 
180 static int
181 puc_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  */
336 static void
337 puc_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 
348 static const struct puc_device_description *
349 puc_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  */
375 static void
376 puc_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
391 static void
392 puc_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 
423 static void
424 puc_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 
505 static 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
528 static void
529 puc_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 static struct resource *
541 puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
542     u_long start, u_long end, u_long count, u_int flags)
543 {
544 	struct puc_device *pdev;
545 	struct resource *retval;
546 	struct resource_list *rl;
547 	struct resource_list_entry *rle;
548 
549 	pdev = device_get_ivars(child);
550 	rl = &pdev->resources;
551 
552 #ifdef PUC_DEBUG
553 	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
554 	    pdev, type, *rid);
555 	puc_print_resource_list(rl);
556 #endif
557 	retval = NULL;
558 	rle = resource_list_find(rl, type, *rid);
559 	if (rle) {
560 		start = rle->start;
561 		end = rle->end;
562 		count = rle->count;
563 #ifdef PUC_DEBUG
564 		printf("found rle, %lx, %lx, %lx\n", start, end, count);
565 #endif
566 		retval = rle->res;
567 	} else
568 		printf("oops rle is gone\n");
569 
570 	return (retval);
571 }
572 
573 static int
574 puc_release_resource(device_t dev, device_t child, int type, int rid,
575     struct resource *res)
576 {
577 	return (0);
578 }
579 
580 static int
581 puc_get_resource(device_t dev, device_t child, int type, int rid,
582     u_long *startp, u_long *countp)
583 {
584 	struct puc_device *pdev;
585 	struct resource_list *rl;
586 	struct resource_list_entry *rle;
587 
588 	pdev = device_get_ivars(child);
589 	rl = &pdev->resources;
590 
591 #ifdef PUC_DEBUG
592 	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
593 	    type, rid);
594 	puc_print_resource_list(rl);
595 #endif
596 	rle = resource_list_find(rl, type, rid);
597 	if (rle) {
598 #ifdef PUC_DEBUG
599 		printf("found rle %p,", rle);
600 #endif
601 		if (startp != NULL)
602 			*startp = rle->start;
603 		if (countp != NULL)
604 			*countp = rle->count;
605 #ifdef PUC_DEBUG
606 		printf(" %lx, %lx\n", rle->start, rle->count);
607 #endif
608 		return (0);
609 	} else
610 		printf("oops rle is gone\n");
611 	return (ENXIO);
612 }
613 
614 static int
615 puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
616 	       void (*ihand)(void *), void *arg, void **cookiep)
617 {
618 	int i;
619 	struct puc_softc *sc;
620 
621 	sc = (struct puc_softc *)device_get_softc(dev);
622 	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
623 		if (sc->sc_ports[i].dev == child) {
624 			if (sc->sc_ports[i].ihand != 0)
625 				return (ENXIO);
626 			sc->sc_ports[i].ihand = ihand;
627 			sc->sc_ports[i].ihandarg = arg;
628 			*cookiep = arg;
629 			return (0);
630 		}
631 	}
632 	return (ENXIO);
633 }
634 
635 static int
636 puc_teardown_intr(device_t dev, device_t child, struct resource *r,
637 		  void *cookie)
638 {
639 	int i;
640 	struct puc_softc *sc;
641 
642 	sc = (struct puc_softc *)device_get_softc(dev);
643 	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
644 		if (sc->sc_ports[i].dev == child) {
645 			sc->sc_ports[i].ihand = NULL;
646 			sc->sc_ports[i].ihandarg = NULL;
647 			return (0);
648 		}
649 	}
650 	return (ENXIO);
651 }
652 
653 static int
654 puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
655 {
656 	struct puc_device *pdev;
657 
658 	pdev = device_get_ivars(child);
659 	if (pdev == NULL)
660 		return (ENOENT);
661 
662 	switch(index) {
663 	case PUC_IVAR_FREQ:
664 		*result = pdev->serialfreq;
665 		break;
666 	default:
667 		return (ENOENT);
668 	}
669 	return (0);
670 }
671 
672 static device_method_t puc_pci_methods[] = {
673     /* Device interface */
674     DEVMETHOD(device_probe,		puc_pci_probe),
675     DEVMETHOD(device_attach,		puc_pci_attach),
676 
677     DEVMETHOD(bus_alloc_resource,	puc_alloc_resource),
678     DEVMETHOD(bus_release_resource,	puc_release_resource),
679     DEVMETHOD(bus_get_resource,		puc_get_resource),
680     DEVMETHOD(bus_read_ivar,		puc_read_ivar),
681     DEVMETHOD(bus_setup_intr,		puc_setup_intr),
682     DEVMETHOD(bus_teardown_intr,	puc_teardown_intr),
683     DEVMETHOD(bus_print_child,		bus_generic_print_child),
684     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
685     { 0, 0 }
686 };
687 
688 static driver_t puc_pci_driver = {
689 	"puc",
690 	puc_pci_methods,
691 	sizeof(struct puc_softc),
692 };
693 
694 static devclass_t puc_devclass;
695 
696 DRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, 0, 0);
697