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