puc.c revision 90925
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 90925 2002-02-19 14:58:11Z nyan $");
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				free(pdev, M_DEVBUF);
292				return (ENOMEM);
293			}
294
295			rle->res->r_start = rman_get_start(res) +
296			    sc->sc_desc->ports[i].offset;
297			rle->res->r_end = rle->res->r_start + 8 - 1;
298			rle->res->r_bustag = rman_get_bustag(res);
299			bus_space_subregion(rle->res->r_bustag,
300			    rman_get_bushandle(res),
301			    sc->sc_desc->ports[i].offset, 8,
302			    &rle->res->r_bushandle);
303		}
304
305		pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
306
307		childunit = puc_find_free_unit(typestr);
308		sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
309		if (sc->sc_ports[i].dev == NULL) {
310			if (sc->barmuxed) {
311				bus_space_unmap(rman_get_bustag(rle->res),
312						rman_get_bushandle(rle->res),
313						8);
314				free(rle->res, M_DEVBUF);
315				free(pdev, M_DEVBUF);
316			}
317			continue;
318		}
319		device_set_ivars(sc->sc_ports[i].dev, pdev);
320		device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
321		if (!bootverbose)
322			device_quiet(sc->sc_ports[i].dev);
323#ifdef PUC_DEBUG
324		printf("puc: type %d, bar %x, offset %x\n",
325		    sc->sc_desc->ports[i].type,
326		    sc->sc_desc->ports[i].bar,
327		    sc->sc_desc->ports[i].offset);
328		print_resource_list(&pdev->resources);
329#endif
330		if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
331			if (sc->barmuxed) {
332				bus_space_unmap(rman_get_bustag(rle->res),
333						rman_get_bushandle(rle->res),
334						8);
335				free(rle->res, M_DEVBUF);
336				free(pdev, M_DEVBUF);
337			}
338		}
339	}
340
341#ifdef PUC_DEBUG
342	bootverbose = 0;
343#endif
344	return (0);
345}
346
347/*
348 * This is just an brute force interrupt handler. It just calls all the
349 * registered handlers sequencially.
350 *
351 * Later on we should maybe have a different handler for boards that can
352 * tell us which device generated the interrupt.
353 */
354static void
355puc_intr(void *arg)
356{
357	int i;
358	struct puc_softc *sc;
359
360	sc = (struct puc_softc *)arg;
361	for (i = 0; i < PUC_MAX_PORTS; i++)
362		if (sc->sc_ports[i].ihand != NULL)
363			(sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
364}
365
366static const struct puc_device_description *
367puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
368    uint32_t sprod)
369{
370	int i;
371
372#define checkreg(val, index) \
373    (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
374
375	for (i = 0; puc_devices[i].name != NULL; i++) {
376		if (checkreg(vend, PUC_REG_VEND) &&
377		    checkreg(prod, PUC_REG_PROD) &&
378		    checkreg(svend, PUC_REG_SVEND) &&
379		    checkreg(sprod, PUC_REG_SPROD))
380			return (&puc_devices[i]);
381	}
382
383#undef checkreg
384
385	return (NULL);
386}
387
388/*
389 * It might be possible to make these more generic if we can detect patterns.
390 * For instance maybe if the size of a bar is 0x400 (the old isa space) it
391 * might contain one or more superio chips.
392 */
393static void
394puc_config_superio(device_t dev)
395{
396	struct puc_softc *sc = (struct puc_softc *)device_get_softc(dev);
397
398	if (sc->sc_desc->rval[PUC_REG_VEND] == 0x1592 &&
399	    sc->sc_desc->rval[PUC_REG_PROD] == 0x0781)
400		puc_config_win877(sc->sc_bar_mappings[0].res);
401}
402
403#define rdspio(indx)		(bus_space_write_1(bst, bsh, efir, indx), \
404				bus_space_read_1(bst, bsh, efdr))
405#define wrspio(indx,data)	(bus_space_write_1(bst, bsh, efir, indx), \
406				bus_space_write_1(bst, bsh, efdr, data))
407
408#ifdef PUC_DEBUG
409static void
410puc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir,
411	u_int efdr)
412{
413	u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17;
414	u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32;
415
416	cr00 = rdspio(0x00);
417	cr01 = rdspio(0x01);
418	cr04 = rdspio(0x04);
419	cr09 = rdspio(0x09);
420	cr0d = rdspio(0x0d);
421	cr14 = rdspio(0x14);
422	cr15 = rdspio(0x15);
423	cr16 = rdspio(0x16);
424	cr17 = rdspio(0x17);
425	cr18 = rdspio(0x18);
426	cr19 = rdspio(0x19);
427	cr24 = rdspio(0x24);
428	cr25 = rdspio(0x25);
429	cr28 = rdspio(0x28);
430	cr2c = rdspio(0x2c);
431	cr31 = rdspio(0x31);
432	cr32 = rdspio(0x32);
433	printf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, "
434	    "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, "
435	    "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09,
436	    cr0d, cr14, cr15, cr16, cr17,
437	    cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32);
438}
439#endif
440
441static void
442puc_config_win877(struct resource *res)
443{
444	u_char val;
445	u_int efir, efdr;
446	bus_space_tag_t bst;
447	bus_space_handle_t bsh;
448
449	bst = rman_get_bustag(res);
450	bsh = rman_get_bushandle(res);
451
452	/* configure the first W83877TF */
453	bus_space_write_1(bst, bsh, 0x250, 0x89);
454	efir = 0x251;
455	efdr = 0x252;
456	val = rdspio(0x09) & 0x0f;
457	if (val != 0x0c) {
458		printf("conf_win877: Oops not a W83877TF\n");
459		return;
460	}
461
462#ifdef PUC_DEBUG
463	printf("before: ");
464	puc_print_win877(bst, bsh, efir, efdr);
465#endif
466
467	val = rdspio(0x16);
468	val |= 0x04;
469	wrspio(0x16, val);
470	val &= ~0x04;
471	wrspio(0x16, val);
472
473	wrspio(0x24, 0x2e8 >> 2);
474	wrspio(0x25, 0x2f8 >> 2);
475	wrspio(0x17, 0x03);
476	wrspio(0x28, 0x43);
477
478#ifdef PUC_DEBUG
479	printf("after: ");
480	puc_print_win877(bst, bsh, efir, efdr);
481#endif
482
483	bus_space_write_1(bst, bsh, 0x250, 0xaa);
484
485	/* configure the second W83877TF */
486	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
487	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
488	efir = 0x3f0;
489	efdr = 0x3f1;
490	val = rdspio(0x09) & 0x0f;
491	if (val != 0x0c) {
492		printf("conf_win877: Oops not a W83877TF\n");
493		return;
494	}
495
496#ifdef PUC_DEBUG
497	printf("before: ");
498	puc_print_win877(bst, bsh, efir, efdr);
499#endif
500
501	val = rdspio(0x16);
502	val |= 0x04;
503	wrspio(0x16, val);
504	val &= ~0x04;
505	wrspio(0x16, val);
506
507	wrspio(0x24, 0x3e8 >> 2);
508	wrspio(0x25, 0x3f8 >> 2);
509	wrspio(0x17, 0x03);
510	wrspio(0x28, 0x43);
511
512#ifdef PUC_DEBUG
513	printf("after: ");
514	puc_print_win877(bst, bsh, efir, efdr);
515#endif
516
517	bus_space_write_1(bst, bsh, 0x3f0, 0xaa);
518}
519
520#undef rdspio
521#undef wrspio
522
523static int puc_find_free_unit(char *name)
524{
525	devclass_t dc;
526	int start;
527	int unit;
528
529	unit = 0;
530	start = 0;
531	while (resource_int_value(name, unit, "port", &start) == 0 &&
532	    start > 0)
533		unit++;
534	dc = devclass_find(name);
535	if (dc == NULL)
536		return (-1);
537	while (devclass_get_device(dc, unit))
538		unit++;
539#ifdef PUC_DEBUG
540	printf("puc: Using %s%d\n", name, unit);
541#endif
542	return (unit);
543}
544
545#ifdef PUC_DEBUG
546static void
547puc_print_resource_list(struct resource_list *rl)
548{
549	struct resource_list_entry *rle;
550
551	printf("print_resource_list: rl %p\n", rl);
552	SLIST_FOREACH(rle, rl, link)
553		printf("type %x, rid %x\n", rle->type, rle->rid);
554	printf("print_resource_list: end.\n");
555}
556#endif
557
558static struct resource *
559puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
560    u_long start, u_long end, u_long count, u_int flags)
561{
562	struct puc_device *pdev;
563	struct resource *retval;
564	struct resource_list *rl;
565	struct resource_list_entry *rle;
566
567	pdev = device_get_ivars(child);
568	rl = &pdev->resources;
569
570#ifdef PUC_DEBUG
571	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
572	    pdev, type, *rid);
573	puc_print_resource_list(rl);
574#endif
575	retval = NULL;
576	rle = resource_list_find(rl, type, *rid);
577	if (rle) {
578		start = rle->start;
579		end = rle->end;
580		count = rle->count;
581#ifdef PUC_DEBUG
582		printf("found rle, %lx, %lx, %lx\n", start, end, count);
583#endif
584		retval = rle->res;
585	} else
586		printf("oops rle is gone\n");
587
588	return (retval);
589}
590
591static int
592puc_release_resource(device_t dev, device_t child, int type, int rid,
593    struct resource *res)
594{
595	return (0);
596}
597
598static int
599puc_get_resource(device_t dev, device_t child, int type, int rid,
600    u_long *startp, u_long *countp)
601{
602	struct puc_device *pdev;
603	struct resource_list *rl;
604	struct resource_list_entry *rle;
605
606	pdev = device_get_ivars(child);
607	rl = &pdev->resources;
608
609#ifdef PUC_DEBUG
610	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
611	    type, rid);
612	puc_print_resource_list(rl);
613#endif
614	rle = resource_list_find(rl, type, rid);
615	if (rle) {
616#ifdef PUC_DEBUG
617		printf("found rle %p,", rle);
618#endif
619		if (startp != NULL)
620			*startp = rle->start;
621		if (countp != NULL)
622			*countp = rle->count;
623#ifdef PUC_DEBUG
624		printf(" %lx, %lx\n", rle->start, rle->count);
625#endif
626		return (0);
627	} else
628		printf("oops rle is gone\n");
629	return (ENXIO);
630}
631
632static int
633puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
634	       void (*ihand)(void *), void *arg, void **cookiep)
635{
636	int i;
637	struct puc_softc *sc;
638
639	sc = (struct puc_softc *)device_get_softc(dev);
640	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
641		if (sc->sc_ports[i].dev == child) {
642			if (sc->sc_ports[i].ihand != 0)
643				return (ENXIO);
644			sc->sc_ports[i].ihand = ihand;
645			sc->sc_ports[i].ihandarg = arg;
646			*cookiep = arg;
647			return (0);
648		}
649	}
650	return (ENXIO);
651}
652
653static int
654puc_teardown_intr(device_t dev, device_t child, struct resource *r,
655		  void *cookie)
656{
657	int i;
658	struct puc_softc *sc;
659
660	sc = (struct puc_softc *)device_get_softc(dev);
661	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
662		if (sc->sc_ports[i].dev == child) {
663			sc->sc_ports[i].ihand = NULL;
664			sc->sc_ports[i].ihandarg = NULL;
665			return (0);
666		}
667	}
668	return (ENXIO);
669}
670
671static int
672puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
673{
674	struct puc_device *pdev;
675
676	pdev = device_get_ivars(child);
677	if (pdev == NULL)
678		return (ENOENT);
679
680	switch(index) {
681	case PUC_IVAR_FREQ:
682		*result = pdev->serialfreq;
683		break;
684	default:
685		return (ENOENT);
686	}
687	return (0);
688}
689
690static device_method_t puc_pci_methods[] = {
691    /* Device interface */
692    DEVMETHOD(device_probe,		puc_pci_probe),
693    DEVMETHOD(device_attach,		puc_pci_attach),
694
695    DEVMETHOD(bus_alloc_resource,	puc_alloc_resource),
696    DEVMETHOD(bus_release_resource,	puc_release_resource),
697    DEVMETHOD(bus_get_resource,		puc_get_resource),
698    DEVMETHOD(bus_read_ivar,		puc_read_ivar),
699    DEVMETHOD(bus_setup_intr,		puc_setup_intr),
700    DEVMETHOD(bus_teardown_intr,	puc_teardown_intr),
701    DEVMETHOD(bus_print_child,		bus_generic_print_child),
702    DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
703    { 0, 0 }
704};
705
706static driver_t puc_pci_driver = {
707	"puc",
708	puc_pci_methods,
709	sizeof(struct puc_softc),
710};
711
712static devclass_t puc_devclass;
713
714DRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, 0, 0);
715