puc.c revision 118292
190731Sjhay/*	$NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $	*/
290731Sjhay
390731Sjhay/*-
490731Sjhay * Copyright (c) 2002 JF Hay.  All rights reserved.
590731Sjhay * Copyright (c) 2000 M. Warner Losh.  All rights reserved.
690731Sjhay *
790731Sjhay * Redistribution and use in source and binary forms, with or without
890731Sjhay * modification, are permitted provided that the following conditions
990731Sjhay * are met:
1090731Sjhay * 1. Redistributions of source code must retain the above copyright
1190731Sjhay *    notice unmodified, this list of conditions, and the following
1290731Sjhay *    disclaimer.
1390731Sjhay * 2. Redistributions in binary form must reproduce the above copyright
1490731Sjhay *    notice, this list of conditions and the following disclaimer in the
1590731Sjhay *    documentation and/or other materials provided with the distribution.
1690731Sjhay *
1790731Sjhay * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1890731Sjhay * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1990731Sjhay * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2090731Sjhay * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2190731Sjhay * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2290731Sjhay * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2390731Sjhay * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2490731Sjhay * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2590731Sjhay * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2690731Sjhay * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2790731Sjhay */
2890731Sjhay
2990731Sjhay/*
3090731Sjhay * Copyright (c) 1996, 1998, 1999
3190731Sjhay *	Christopher G. Demetriou.  All rights reserved.
3290731Sjhay *
3390731Sjhay * Redistribution and use in source and binary forms, with or without
3490731Sjhay * modification, are permitted provided that the following conditions
3590731Sjhay * are met:
3690731Sjhay * 1. Redistributions of source code must retain the above copyright
3790731Sjhay *    notice, this list of conditions and the following disclaimer.
3890731Sjhay * 2. Redistributions in binary form must reproduce the above copyright
3990731Sjhay *    notice, this list of conditions and the following disclaimer in the
4090731Sjhay *    documentation and/or other materials provided with the distribution.
4190731Sjhay * 3. All advertising materials mentioning features or use of this software
4290731Sjhay *    must display the following acknowledgement:
4390731Sjhay *      This product includes software developed by Christopher G. Demetriou
4490731Sjhay *	for the NetBSD Project.
4590731Sjhay * 4. The name of the author may not be used to endorse or promote products
4690731Sjhay *    derived from this software without specific prior written permission
4790731Sjhay *
4890731Sjhay * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4990731Sjhay * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5090731Sjhay * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5190731Sjhay * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
5290731Sjhay * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
5390731Sjhay * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5490731Sjhay * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5590731Sjhay * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5690731Sjhay * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
5790731Sjhay * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5890731Sjhay */
5990731Sjhay
6090731Sjhay#include <sys/cdefs.h>
6190731Sjhay__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 118292 2003-08-01 02:25:32Z ambrisko $");
6290731Sjhay
6390731Sjhay/*
6490731Sjhay * PCI "universal" communication card device driver, glues com, lpt,
6590731Sjhay * and similar ports to PCI via bridge chip often much larger than
6690731Sjhay * the devices being glued.
6790731Sjhay *
6890731Sjhay * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
6990731Sjhay * sys/dev/pci/pciide.c, revision 1.6).
7090731Sjhay *
7190731Sjhay * These devices could be (and some times are) described as
7290731Sjhay * communications/{serial,parallel}, etc. devices with known
7390731Sjhay * programming interfaces, but those programming interfaces (in
7490731Sjhay * particular the BAR assignments for devices, etc.) in fact are not
7590731Sjhay * particularly well defined.
7690731Sjhay *
7790731Sjhay * After I/we have seen more of these devices, it may be possible
7890731Sjhay * to generalize some of these bits.  In particular, devices which
7990731Sjhay * describe themselves as communications/serial/16[45]50, and
8090731Sjhay * communications/parallel/??? might be attached via direct
8190731Sjhay * 'com' and 'lpt' attachments to pci.
8290731Sjhay */
8390731Sjhay
84102751Sjmallett#include "opt_puc.h"
85102751Sjmallett
8690731Sjhay#include <sys/param.h>
8790731Sjhay#include <sys/systm.h>
8890731Sjhay#include <sys/kernel.h>
8990731Sjhay#include <sys/bus.h>
9090731Sjhay#include <sys/conf.h>
9190731Sjhay#include <sys/malloc.h>
9290731Sjhay
9390731Sjhay#include <machine/bus.h>
9490731Sjhay#include <machine/resource.h>
9590731Sjhay#include <sys/rman.h>
9690731Sjhay
9790731Sjhay#include <dev/pci/pcireg.h>
9890731Sjhay#include <dev/pci/pcivar.h>
99102714Sphk
100102714Sphk#define PUC_ENTRAILS	1
10190731Sjhay#include <dev/puc/pucvar.h>
10290731Sjhay
10390731Sjhaystruct puc_device {
10490731Sjhay	struct resource_list resources;
10590731Sjhay	u_int serialfreq;
10690731Sjhay};
10790731Sjhay
10890731Sjhaystatic void puc_intr(void *arg);
10990731Sjhay
11090731Sjhaystatic int puc_find_free_unit(char *);
11190731Sjhay#ifdef PUC_DEBUG
11290731Sjhaystatic void puc_print_resource_list(struct resource_list *);
11390731Sjhay#endif
11490731Sjhay
115102893Sphkdevclass_t puc_devclass;
116102893Sphk
117102734Sphkstatic int
118102734Sphkpuc_port_bar_index(struct puc_softc *sc, int bar)
119102734Sphk{
120102734Sphk	int i;
121102734Sphk
122102734Sphk	for (i = 0; i < PUC_MAX_BAR; i += 1) {
123102734Sphk		if (!sc->sc_bar_mappings[i].used)
124102734Sphk			break;
125102734Sphk		if (sc->sc_bar_mappings[i].bar == bar)
126102734Sphk			return (i);
127102734Sphk	}
128102734Sphk	sc->sc_bar_mappings[i].bar = bar;
129102734Sphk	sc->sc_bar_mappings[i].used = 1;
130102734Sphk	return (i);
131102734Sphk}
132102734Sphk
133112270Ssobomaxstatic int
134112270Ssobomaxpuc_probe_ilr(struct puc_softc *sc, struct resource *res)
135112270Ssobomax{
136112270Ssobomax	u_char t1, t2;
137112270Ssobomax	int i;
138112270Ssobomax
139112270Ssobomax	switch (sc->sc_desc->ilr_type) {
140112270Ssobomax	case PUC_ILR_TYPE_DIGI:
141112270Ssobomax		sc->ilr_st = rman_get_bustag(res);
142112270Ssobomax		sc->ilr_sh = rman_get_bushandle(res);
143114344Ssobomax		for (i = 0; i < 2 && sc->sc_desc->ilr_offset[i] != 0; i++) {
144112270Ssobomax			t1 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
145112270Ssobomax			    sc->sc_desc->ilr_offset[i]);
146112270Ssobomax			t1 = ~t1;
147112270Ssobomax			bus_space_write_1(sc->ilr_st, sc->ilr_sh,
148112270Ssobomax			    sc->sc_desc->ilr_offset[i], t1);
149112270Ssobomax			t2 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
150112270Ssobomax			    sc->sc_desc->ilr_offset[i]);
151112270Ssobomax			if (t2 == t1)
152112270Ssobomax				return (0);
153112270Ssobomax		}
154112270Ssobomax		return (1);
155112270Ssobomax
156112270Ssobomax	default:
157112270Ssobomax		break;
158112270Ssobomax	}
159112270Ssobomax	return (0);
160112270Ssobomax}
161112270Ssobomax
162102714Sphkint
163102714Sphkpuc_attach(device_t dev, const struct puc_device_description *desc)
16490731Sjhay{
16590731Sjhay	char *typestr;
166109458Smarcel	int bidx, childunit, i, irq_setup, rid, type;
16790731Sjhay	struct puc_softc *sc;
16890731Sjhay	struct puc_device *pdev;
16990731Sjhay	struct resource *res;
17090731Sjhay	struct resource_list_entry *rle;
17190731Sjhay
17290731Sjhay	sc = (struct puc_softc *)device_get_softc(dev);
17390731Sjhay	bzero(sc, sizeof(*sc));
174102714Sphk	sc->sc_desc = desc;
17590731Sjhay	if (sc->sc_desc == NULL)
17690731Sjhay		return (ENXIO);
17790731Sjhay
17890731Sjhay#ifdef PUC_DEBUG
17990731Sjhay	bootverbose = 1;
18090731Sjhay
18190731Sjhay	printf("puc: name: %s\n", sc->sc_desc->name);
18290731Sjhay#endif
18390731Sjhay	rid = 0;
18490731Sjhay	res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
18590731Sjhay	    RF_ACTIVE | RF_SHAREABLE);
18690731Sjhay	if (!res)
18790731Sjhay		return (ENXIO);
18890731Sjhay
18990731Sjhay	sc->irqres = res;
19090731Sjhay	sc->irqrid = rid;
191102929Sphk#ifdef PUC_FASTINTR
19290731Sjhay	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
193102929Sphk	    INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
194102929Sphk	if (irq_setup == 0)
195102931Sphk		sc->fastintr = INTR_FAST;
196102929Sphk	else
197102929Sphk		irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
198102929Sphk		    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
199102929Sphk#else
200102929Sphk	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
201102895Sphk	    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
202102929Sphk#endif
20390731Sjhay	if (irq_setup != 0)
20490731Sjhay		return (ENXIO);
20590731Sjhay
20690731Sjhay	rid = 0;
20790731Sjhay	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
208102734Sphk		if (i > 0 && rid == sc->sc_desc->ports[i].bar)
20990731Sjhay			sc->barmuxed = 1;
21090731Sjhay		rid = sc->sc_desc->ports[i].bar;
211102734Sphk		bidx = puc_port_bar_index(sc, rid);
21290731Sjhay
21390731Sjhay		if (sc->sc_bar_mappings[bidx].res != NULL)
21490731Sjhay			continue;
215109458Smarcel
216109458Smarcel		type = (sc->sc_desc->ports[i].flags & PUC_FLAGS_MEMORY)
217109458Smarcel		    ? SYS_RES_MEMORY : SYS_RES_IOPORT;
218109458Smarcel
219109458Smarcel		res = bus_alloc_resource(dev, type, &rid, 0ul, ~0ul, 1,
220109458Smarcel		    RF_ACTIVE);
22190731Sjhay		if (res == NULL) {
22290731Sjhay			printf("could not get resource\n");
22390731Sjhay			continue;
22490731Sjhay		}
225109458Smarcel		sc->sc_bar_mappings[bidx].type = type;
22690731Sjhay		sc->sc_bar_mappings[bidx].res = res;
227112270Ssobomax
228112270Ssobomax		if (sc->sc_desc->ilr_type != PUC_ILR_TYPE_NONE) {
229112270Ssobomax			sc->ilr_enabled = puc_probe_ilr(sc, res);
230112270Ssobomax			if (sc->ilr_enabled)
231112270Ssobomax				device_printf(dev, "ILR enabled\n");
232112270Ssobomax			else
233112270Ssobomax				device_printf(dev, "ILR disabled\n");
234112270Ssobomax		}
23590731Sjhay#ifdef PUC_DEBUG
236109458Smarcel		printf("%s rid %d bst %x, start %x, end %x\n",
237109458Smarcel		    (type == SYS_RES_MEMORY) ? "memory" : "port", rid,
23890731Sjhay		    (u_int)rman_get_bustag(res), (u_int)rman_get_start(res),
23990731Sjhay		    (u_int)rman_get_end(res));
24090731Sjhay#endif
24190731Sjhay	}
24290731Sjhay
243102734Sphk	if (desc->init != NULL) {
244102734Sphk		i = desc->init(sc);
245102734Sphk		if (i != 0)
246102734Sphk			return (i);
247102734Sphk	}
24890731Sjhay
24990731Sjhay	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
25090731Sjhay		rid = sc->sc_desc->ports[i].bar;
251102734Sphk		bidx = puc_port_bar_index(sc, rid);
25290731Sjhay		if (sc->sc_bar_mappings[bidx].res == NULL)
25390731Sjhay			continue;
25490731Sjhay
25590731Sjhay		switch (sc->sc_desc->ports[i].type) {
25690731Sjhay		case PUC_PORT_TYPE_COM:
25790731Sjhay			typestr = "sio";
25890731Sjhay			break;
259118292Sambrisko		case PUC_PORT_TYPE_LPT:
260118292Sambrisko			typestr = "ppc";
261118292Sambrisko			break;
26290731Sjhay		default:
26390731Sjhay			continue;
26490731Sjhay		}
26590731Sjhay		pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
26690731Sjhay		    M_NOWAIT | M_ZERO);
26790731Sjhay		if (!pdev)
26890731Sjhay			continue;
26990731Sjhay		resource_list_init(&pdev->resources);
27090731Sjhay
27190731Sjhay		/* First fake up an IRQ resource. */
27290731Sjhay		resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
27390731Sjhay		    rman_get_start(sc->irqres), rman_get_end(sc->irqres),
27490731Sjhay		    rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
27590731Sjhay		rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
27690731Sjhay		rle->res = sc->irqres;
27790731Sjhay
278109458Smarcel		/* Now fake an IOPORT or MEMORY resource */
27990731Sjhay		res = sc->sc_bar_mappings[bidx].res;
280109458Smarcel		type = sc->sc_bar_mappings[bidx].type;
281109458Smarcel		resource_list_add(&pdev->resources, type, 0,
28290731Sjhay		    rman_get_start(res) + sc->sc_desc->ports[i].offset,
283102734Sphk		    rman_get_start(res) + sc->sc_desc->ports[i].offset + 8 - 1,
28490731Sjhay		    8);
285109458Smarcel		rle = resource_list_find(&pdev->resources, type, 0);
28690731Sjhay
28790731Sjhay		if (sc->barmuxed == 0) {
28890731Sjhay			rle->res = sc->sc_bar_mappings[bidx].res;
28990731Sjhay		} else {
29090731Sjhay			rle->res = malloc(sizeof(struct resource), M_DEVBUF,
291111119Simp			    M_WAITOK | M_ZERO);
29290925Snyan			if (rle->res == NULL) {
29390925Snyan				free(pdev, M_DEVBUF);
29490731Sjhay				return (ENOMEM);
29590925Snyan			}
29690731Sjhay
29790731Sjhay			rle->res->r_start = rman_get_start(res) +
29890731Sjhay			    sc->sc_desc->ports[i].offset;
29990731Sjhay			rle->res->r_end = rle->res->r_start + 8 - 1;
30090731Sjhay			rle->res->r_bustag = rman_get_bustag(res);
30190731Sjhay			bus_space_subregion(rle->res->r_bustag,
30290731Sjhay			    rman_get_bushandle(res),
30390731Sjhay			    sc->sc_desc->ports[i].offset, 8,
30490731Sjhay			    &rle->res->r_bushandle);
30590731Sjhay		}
30690731Sjhay
30790731Sjhay		pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
30890731Sjhay
30990731Sjhay		childunit = puc_find_free_unit(typestr);
31090731Sjhay		sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
31190925Snyan		if (sc->sc_ports[i].dev == NULL) {
31290925Snyan			if (sc->barmuxed) {
31390925Snyan				bus_space_unmap(rman_get_bustag(rle->res),
314109458Smarcel				    rman_get_bushandle(rle->res), 8);
31590925Snyan				free(rle->res, M_DEVBUF);
31690925Snyan				free(pdev, M_DEVBUF);
31790925Snyan			}
31890731Sjhay			continue;
31990925Snyan		}
32090731Sjhay		device_set_ivars(sc->sc_ports[i].dev, pdev);
32190731Sjhay		device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
32290731Sjhay		if (!bootverbose)
32390731Sjhay			device_quiet(sc->sc_ports[i].dev);
32490731Sjhay#ifdef PUC_DEBUG
32590731Sjhay		printf("puc: type %d, bar %x, offset %x\n",
32690731Sjhay		    sc->sc_desc->ports[i].type,
32790731Sjhay		    sc->sc_desc->ports[i].bar,
32890731Sjhay		    sc->sc_desc->ports[i].offset);
329102714Sphk		puc_print_resource_list(&pdev->resources);
33090731Sjhay#endif
331104068Sphk		device_set_flags(sc->sc_ports[i].dev,
332104068Sphk		    sc->sc_desc->ports[i].flags);
33390925Snyan		if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
33490925Snyan			if (sc->barmuxed) {
33590925Snyan				bus_space_unmap(rman_get_bustag(rle->res),
33690925Snyan						rman_get_bushandle(rle->res),
33790925Snyan						8);
33890925Snyan				free(rle->res, M_DEVBUF);
33990925Snyan				free(pdev, M_DEVBUF);
34090925Snyan			}
34190925Snyan		}
34290731Sjhay	}
34390731Sjhay
34490731Sjhay#ifdef PUC_DEBUG
34590731Sjhay	bootverbose = 0;
34690731Sjhay#endif
34790731Sjhay	return (0);
34890731Sjhay}
34990731Sjhay
350112270Ssobomaxstatic u_int32_t
351112270Ssobomaxpuc_ilr_read(struct puc_softc *sc)
352112270Ssobomax{
353112270Ssobomax	u_int32_t mask;
354112270Ssobomax	int i;
355112270Ssobomax
356112270Ssobomax	mask = 0;
357112270Ssobomax	switch (sc->sc_desc->ilr_type) {
358112270Ssobomax	case PUC_ILR_TYPE_DIGI:
359114344Ssobomax		for (i = 1; i >= 0 && sc->sc_desc->ilr_offset[i] != 0; i--) {
360112270Ssobomax			mask = (mask << 8) | (bus_space_read_1(sc->ilr_st,
361112270Ssobomax			    sc->ilr_sh, sc->sc_desc->ilr_offset[i]) & 0xff);
362112270Ssobomax		}
363112270Ssobomax		break;
364112270Ssobomax
365112270Ssobomax	default:
366112270Ssobomax		mask = 0xffffffff;
367112270Ssobomax		break;
368112270Ssobomax	}
369112270Ssobomax	return (mask);
370112270Ssobomax}
371112270Ssobomax
37290731Sjhay/*
373112270Ssobomax * This is an interrupt handler. For boards that can't tell us which
374112270Ssobomax * device generated the interrupt it just calls all the registered
375112270Ssobomax * handlers sequencially, but for boards that can tell us which
376112270Ssobomax * device(s) generated the interrupt it calls only handlers for devices
377112270Ssobomax * that actually generated the interrupt.
37890731Sjhay */
37990731Sjhaystatic void
38090731Sjhaypuc_intr(void *arg)
38190731Sjhay{
38290731Sjhay	int i;
383112270Ssobomax	u_int32_t ilr_mask;
38490731Sjhay	struct puc_softc *sc;
38590731Sjhay
38690731Sjhay	sc = (struct puc_softc *)arg;
387112270Ssobomax	ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff;
38890731Sjhay	for (i = 0; i < PUC_MAX_PORTS; i++)
389112270Ssobomax		if (sc->sc_ports[i].ihand != NULL &&
390112270Ssobomax		    ((ilr_mask >> i) & 0x00000001))
39190731Sjhay			(sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
39290731Sjhay}
39390731Sjhay
394102714Sphkconst struct puc_device_description *
39590731Sjhaypuc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
39690731Sjhay    uint32_t sprod)
39790731Sjhay{
39890731Sjhay	int i;
39990731Sjhay
40090731Sjhay#define checkreg(val, index) \
40190731Sjhay    (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
40290731Sjhay
40390731Sjhay	for (i = 0; puc_devices[i].name != NULL; i++) {
40490731Sjhay		if (checkreg(vend, PUC_REG_VEND) &&
40590731Sjhay		    checkreg(prod, PUC_REG_PROD) &&
40690731Sjhay		    checkreg(svend, PUC_REG_SVEND) &&
40790731Sjhay		    checkreg(sprod, PUC_REG_SPROD))
40890731Sjhay			return (&puc_devices[i]);
40990731Sjhay	}
41090731Sjhay
41190731Sjhay#undef checkreg
41290731Sjhay
41390731Sjhay	return (NULL);
41490731Sjhay}
415102894Sphk
416102894Sphkstatic int
417102894Sphkpuc_find_free_unit(char *name)
41890731Sjhay{
41990731Sjhay	devclass_t dc;
42090731Sjhay	int start;
42190731Sjhay	int unit;
42290731Sjhay
42390731Sjhay	unit = 0;
42490731Sjhay	start = 0;
42590731Sjhay	while (resource_int_value(name, unit, "port", &start) == 0 &&
42690731Sjhay	    start > 0)
42790731Sjhay		unit++;
42890731Sjhay	dc = devclass_find(name);
42990731Sjhay	if (dc == NULL)
43090731Sjhay		return (-1);
43190731Sjhay	while (devclass_get_device(dc, unit))
43290731Sjhay		unit++;
43390731Sjhay#ifdef PUC_DEBUG
43490731Sjhay	printf("puc: Using %s%d\n", name, unit);
43590731Sjhay#endif
43690731Sjhay	return (unit);
43790731Sjhay}
43890731Sjhay
43990731Sjhay#ifdef PUC_DEBUG
44090731Sjhaystatic void
44190731Sjhaypuc_print_resource_list(struct resource_list *rl)
44290731Sjhay{
443102734Sphk#if 0
44490731Sjhay	struct resource_list_entry *rle;
44590731Sjhay
44690731Sjhay	printf("print_resource_list: rl %p\n", rl);
44790731Sjhay	SLIST_FOREACH(rle, rl, link)
448102734Sphk		printf("  type %x, rid %x start %x end %x count %x\n",
449102734Sphk		    rle->type, rle->rid, rle->start, rle->end, rle->count);
45090731Sjhay	printf("print_resource_list: end.\n");
451102734Sphk#endif
45290731Sjhay}
45390731Sjhay#endif
45490731Sjhay
455102714Sphkstruct resource *
45690731Sjhaypuc_alloc_resource(device_t dev, device_t child, int type, int *rid,
45790731Sjhay    u_long start, u_long end, u_long count, u_int flags)
45890731Sjhay{
45990731Sjhay	struct puc_device *pdev;
46090731Sjhay	struct resource *retval;
46190731Sjhay	struct resource_list *rl;
46290731Sjhay	struct resource_list_entry *rle;
463118292Sambrisko	device_t my_child;
46490731Sjhay
465118292Sambrisko	/*
466118292Sambrisko	 * in the case of a child of child we need to find our immediate child
467118292Sambrisko	 */
468118292Sambrisko	for (my_child = child; device_get_parent(my_child) != dev;
469118292Sambrisko	     my_child = device_get_parent(my_child));
470118292Sambrisko
471118292Sambrisko	pdev = device_get_ivars(my_child);
47290731Sjhay	rl = &pdev->resources;
47390731Sjhay
47490731Sjhay#ifdef PUC_DEBUG
47590731Sjhay	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
47690731Sjhay	    pdev, type, *rid);
47790731Sjhay	puc_print_resource_list(rl);
47890731Sjhay#endif
47990731Sjhay	retval = NULL;
48090731Sjhay	rle = resource_list_find(rl, type, *rid);
48190731Sjhay	if (rle) {
48290731Sjhay		start = rle->start;
48390731Sjhay		end = rle->end;
48490731Sjhay		count = rle->count;
48590731Sjhay#ifdef PUC_DEBUG
48690731Sjhay		printf("found rle, %lx, %lx, %lx\n", start, end, count);
48790731Sjhay#endif
48890731Sjhay		retval = rle->res;
489118292Sambrisko	}
490118292Sambrisko#ifdef PUC_DEBUG
491118292Sambrisko	else
49290731Sjhay		printf("oops rle is gone\n");
493118292Sambrisko#endif
49490731Sjhay
49590731Sjhay	return (retval);
49690731Sjhay}
49790731Sjhay
498102714Sphkint
49990731Sjhaypuc_release_resource(device_t dev, device_t child, int type, int rid,
50090731Sjhay    struct resource *res)
50190731Sjhay{
50290731Sjhay	return (0);
50390731Sjhay}
50490731Sjhay
505102714Sphkint
50690731Sjhaypuc_get_resource(device_t dev, device_t child, int type, int rid,
50790731Sjhay    u_long *startp, u_long *countp)
50890731Sjhay{
50990731Sjhay	struct puc_device *pdev;
51090731Sjhay	struct resource_list *rl;
51190731Sjhay	struct resource_list_entry *rle;
51290731Sjhay
51390731Sjhay	pdev = device_get_ivars(child);
51490731Sjhay	rl = &pdev->resources;
51590731Sjhay
51690731Sjhay#ifdef PUC_DEBUG
51790731Sjhay	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
51890731Sjhay	    type, rid);
51990731Sjhay	puc_print_resource_list(rl);
52090731Sjhay#endif
52190731Sjhay	rle = resource_list_find(rl, type, rid);
52290731Sjhay	if (rle) {
52390731Sjhay#ifdef PUC_DEBUG
52490731Sjhay		printf("found rle %p,", rle);
52590731Sjhay#endif
52690731Sjhay		if (startp != NULL)
52790731Sjhay			*startp = rle->start;
52890731Sjhay		if (countp != NULL)
52990731Sjhay			*countp = rle->count;
53090731Sjhay#ifdef PUC_DEBUG
53190731Sjhay		printf(" %lx, %lx\n", rle->start, rle->count);
53290731Sjhay#endif
53390731Sjhay		return (0);
53490731Sjhay	} else
53590731Sjhay		printf("oops rle is gone\n");
53690731Sjhay	return (ENXIO);
53790731Sjhay}
53890731Sjhay
539102714Sphkint
54090731Sjhaypuc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
54190731Sjhay	       void (*ihand)(void *), void *arg, void **cookiep)
54290731Sjhay{
54390731Sjhay	int i;
54490731Sjhay	struct puc_softc *sc;
54590731Sjhay
546102929Sphk	sc = (struct puc_softc *)device_get_softc(dev);
547102931Sphk	if ((flags & INTR_FAST) != sc->fastintr)
548102895Sphk		return (ENXIO);
54990731Sjhay	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
55090731Sjhay		if (sc->sc_ports[i].dev == child) {
55190731Sjhay			if (sc->sc_ports[i].ihand != 0)
55290731Sjhay				return (ENXIO);
55390731Sjhay			sc->sc_ports[i].ihand = ihand;
55490731Sjhay			sc->sc_ports[i].ihandarg = arg;
55590731Sjhay			*cookiep = arg;
55690731Sjhay			return (0);
55790731Sjhay		}
55890731Sjhay	}
55990731Sjhay	return (ENXIO);
56090731Sjhay}
56190731Sjhay
562102714Sphkint
56390731Sjhaypuc_teardown_intr(device_t dev, device_t child, struct resource *r,
56490731Sjhay		  void *cookie)
56590731Sjhay{
56690731Sjhay	int i;
56790731Sjhay	struct puc_softc *sc;
56890731Sjhay
56990731Sjhay	sc = (struct puc_softc *)device_get_softc(dev);
57090731Sjhay	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
57190731Sjhay		if (sc->sc_ports[i].dev == child) {
57290731Sjhay			sc->sc_ports[i].ihand = NULL;
57390731Sjhay			sc->sc_ports[i].ihandarg = NULL;
57490731Sjhay			return (0);
57590731Sjhay		}
57690731Sjhay	}
57790731Sjhay	return (ENXIO);
57890731Sjhay}
57990731Sjhay
580102714Sphkint
58190731Sjhaypuc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
58290731Sjhay{
58390731Sjhay	struct puc_device *pdev;
58490731Sjhay
58590731Sjhay	pdev = device_get_ivars(child);
58690731Sjhay	if (pdev == NULL)
58790731Sjhay		return (ENOENT);
58890731Sjhay
58990731Sjhay	switch(index) {
59090731Sjhay	case PUC_IVAR_FREQ:
59190731Sjhay		*result = pdev->serialfreq;
59290731Sjhay		break;
59390731Sjhay	default:
59490731Sjhay		return (ENOENT);
59590731Sjhay	}
59690731Sjhay	return (0);
59790731Sjhay}
598