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