puc.c revision 112270
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 112270 2003-03-15 16:25:40Z sobomax $");
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);
143112270Ssobomax		for (i = 0; i < 2; 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;
25990731Sjhay		default:
26090731Sjhay			continue;
26190731Sjhay		}
26290731Sjhay		pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
26390731Sjhay		    M_NOWAIT | M_ZERO);
26490731Sjhay		if (!pdev)
26590731Sjhay			continue;
26690731Sjhay		resource_list_init(&pdev->resources);
26790731Sjhay
26890731Sjhay		/* First fake up an IRQ resource. */
26990731Sjhay		resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
27090731Sjhay		    rman_get_start(sc->irqres), rman_get_end(sc->irqres),
27190731Sjhay		    rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
27290731Sjhay		rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
27390731Sjhay		rle->res = sc->irqres;
27490731Sjhay
275109458Smarcel		/* Now fake an IOPORT or MEMORY resource */
27690731Sjhay		res = sc->sc_bar_mappings[bidx].res;
277109458Smarcel		type = sc->sc_bar_mappings[bidx].type;
278109458Smarcel		resource_list_add(&pdev->resources, type, 0,
27990731Sjhay		    rman_get_start(res) + sc->sc_desc->ports[i].offset,
280102734Sphk		    rman_get_start(res) + sc->sc_desc->ports[i].offset + 8 - 1,
28190731Sjhay		    8);
282109458Smarcel		rle = resource_list_find(&pdev->resources, type, 0);
28390731Sjhay
28490731Sjhay		if (sc->barmuxed == 0) {
28590731Sjhay			rle->res = sc->sc_bar_mappings[bidx].res;
28690731Sjhay		} else {
28790731Sjhay			rle->res = malloc(sizeof(struct resource), M_DEVBUF,
288111119Simp			    M_WAITOK | M_ZERO);
28990925Snyan			if (rle->res == NULL) {
29090925Snyan				free(pdev, M_DEVBUF);
29190731Sjhay				return (ENOMEM);
29290925Snyan			}
29390731Sjhay
29490731Sjhay			rle->res->r_start = rman_get_start(res) +
29590731Sjhay			    sc->sc_desc->ports[i].offset;
29690731Sjhay			rle->res->r_end = rle->res->r_start + 8 - 1;
29790731Sjhay			rle->res->r_bustag = rman_get_bustag(res);
29890731Sjhay			bus_space_subregion(rle->res->r_bustag,
29990731Sjhay			    rman_get_bushandle(res),
30090731Sjhay			    sc->sc_desc->ports[i].offset, 8,
30190731Sjhay			    &rle->res->r_bushandle);
30290731Sjhay		}
30390731Sjhay
30490731Sjhay		pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
30590731Sjhay
30690731Sjhay		childunit = puc_find_free_unit(typestr);
30790731Sjhay		sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
30890925Snyan		if (sc->sc_ports[i].dev == NULL) {
30990925Snyan			if (sc->barmuxed) {
31090925Snyan				bus_space_unmap(rman_get_bustag(rle->res),
311109458Smarcel				    rman_get_bushandle(rle->res), 8);
31290925Snyan				free(rle->res, M_DEVBUF);
31390925Snyan				free(pdev, M_DEVBUF);
31490925Snyan			}
31590731Sjhay			continue;
31690925Snyan		}
31790731Sjhay		device_set_ivars(sc->sc_ports[i].dev, pdev);
31890731Sjhay		device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
31990731Sjhay		if (!bootverbose)
32090731Sjhay			device_quiet(sc->sc_ports[i].dev);
32190731Sjhay#ifdef PUC_DEBUG
32290731Sjhay		printf("puc: type %d, bar %x, offset %x\n",
32390731Sjhay		    sc->sc_desc->ports[i].type,
32490731Sjhay		    sc->sc_desc->ports[i].bar,
32590731Sjhay		    sc->sc_desc->ports[i].offset);
326102714Sphk		puc_print_resource_list(&pdev->resources);
32790731Sjhay#endif
328104068Sphk		device_set_flags(sc->sc_ports[i].dev,
329104068Sphk		    sc->sc_desc->ports[i].flags);
33090925Snyan		if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
33190925Snyan			if (sc->barmuxed) {
33290925Snyan				bus_space_unmap(rman_get_bustag(rle->res),
33390925Snyan						rman_get_bushandle(rle->res),
33490925Snyan						8);
33590925Snyan				free(rle->res, M_DEVBUF);
33690925Snyan				free(pdev, M_DEVBUF);
33790925Snyan			}
33890925Snyan		}
33990731Sjhay	}
34090731Sjhay
34190731Sjhay#ifdef PUC_DEBUG
34290731Sjhay	bootverbose = 0;
34390731Sjhay#endif
34490731Sjhay	return (0);
34590731Sjhay}
34690731Sjhay
347112270Ssobomaxstatic u_int32_t
348112270Ssobomaxpuc_ilr_read(struct puc_softc *sc)
349112270Ssobomax{
350112270Ssobomax	u_int32_t mask;
351112270Ssobomax	int i;
352112270Ssobomax
353112270Ssobomax	mask = 0;
354112270Ssobomax	switch (sc->sc_desc->ilr_type) {
355112270Ssobomax	case PUC_ILR_TYPE_DIGI:
356112270Ssobomax		for (i = 1; i >= 0; i--) {
357112270Ssobomax			mask = (mask << 8) | (bus_space_read_1(sc->ilr_st,
358112270Ssobomax			    sc->ilr_sh, sc->sc_desc->ilr_offset[i]) & 0xff);
359112270Ssobomax		}
360112270Ssobomax		break;
361112270Ssobomax
362112270Ssobomax	default:
363112270Ssobomax		mask = 0xffffffff;
364112270Ssobomax		break;
365112270Ssobomax	}
366112270Ssobomax	return (mask);
367112270Ssobomax}
368112270Ssobomax
36990731Sjhay/*
370112270Ssobomax * This is an interrupt handler. For boards that can't tell us which
371112270Ssobomax * device generated the interrupt it just calls all the registered
372112270Ssobomax * handlers sequencially, but for boards that can tell us which
373112270Ssobomax * device(s) generated the interrupt it calls only handlers for devices
374112270Ssobomax * that actually generated the interrupt.
37590731Sjhay */
37690731Sjhaystatic void
37790731Sjhaypuc_intr(void *arg)
37890731Sjhay{
37990731Sjhay	int i;
380112270Ssobomax	u_int32_t ilr_mask;
38190731Sjhay	struct puc_softc *sc;
38290731Sjhay
38390731Sjhay	sc = (struct puc_softc *)arg;
384112270Ssobomax	ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff;
38590731Sjhay	for (i = 0; i < PUC_MAX_PORTS; i++)
386112270Ssobomax		if (sc->sc_ports[i].ihand != NULL &&
387112270Ssobomax		    ((ilr_mask >> i) & 0x00000001))
38890731Sjhay			(sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
38990731Sjhay}
39090731Sjhay
391102714Sphkconst struct puc_device_description *
39290731Sjhaypuc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
39390731Sjhay    uint32_t sprod)
39490731Sjhay{
39590731Sjhay	int i;
39690731Sjhay
39790731Sjhay#define checkreg(val, index) \
39890731Sjhay    (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
39990731Sjhay
40090731Sjhay	for (i = 0; puc_devices[i].name != NULL; i++) {
40190731Sjhay		if (checkreg(vend, PUC_REG_VEND) &&
40290731Sjhay		    checkreg(prod, PUC_REG_PROD) &&
40390731Sjhay		    checkreg(svend, PUC_REG_SVEND) &&
40490731Sjhay		    checkreg(sprod, PUC_REG_SPROD))
40590731Sjhay			return (&puc_devices[i]);
40690731Sjhay	}
40790731Sjhay
40890731Sjhay#undef checkreg
40990731Sjhay
41090731Sjhay	return (NULL);
41190731Sjhay}
412102894Sphk
413102894Sphkstatic int
414102894Sphkpuc_find_free_unit(char *name)
41590731Sjhay{
41690731Sjhay	devclass_t dc;
41790731Sjhay	int start;
41890731Sjhay	int unit;
41990731Sjhay
42090731Sjhay	unit = 0;
42190731Sjhay	start = 0;
42290731Sjhay	while (resource_int_value(name, unit, "port", &start) == 0 &&
42390731Sjhay	    start > 0)
42490731Sjhay		unit++;
42590731Sjhay	dc = devclass_find(name);
42690731Sjhay	if (dc == NULL)
42790731Sjhay		return (-1);
42890731Sjhay	while (devclass_get_device(dc, unit))
42990731Sjhay		unit++;
43090731Sjhay#ifdef PUC_DEBUG
43190731Sjhay	printf("puc: Using %s%d\n", name, unit);
43290731Sjhay#endif
43390731Sjhay	return (unit);
43490731Sjhay}
43590731Sjhay
43690731Sjhay#ifdef PUC_DEBUG
43790731Sjhaystatic void
43890731Sjhaypuc_print_resource_list(struct resource_list *rl)
43990731Sjhay{
440102734Sphk#if 0
44190731Sjhay	struct resource_list_entry *rle;
44290731Sjhay
44390731Sjhay	printf("print_resource_list: rl %p\n", rl);
44490731Sjhay	SLIST_FOREACH(rle, rl, link)
445102734Sphk		printf("  type %x, rid %x start %x end %x count %x\n",
446102734Sphk		    rle->type, rle->rid, rle->start, rle->end, rle->count);
44790731Sjhay	printf("print_resource_list: end.\n");
448102734Sphk#endif
44990731Sjhay}
45090731Sjhay#endif
45190731Sjhay
452102714Sphkstruct resource *
45390731Sjhaypuc_alloc_resource(device_t dev, device_t child, int type, int *rid,
45490731Sjhay    u_long start, u_long end, u_long count, u_int flags)
45590731Sjhay{
45690731Sjhay	struct puc_device *pdev;
45790731Sjhay	struct resource *retval;
45890731Sjhay	struct resource_list *rl;
45990731Sjhay	struct resource_list_entry *rle;
46090731Sjhay
46190731Sjhay	pdev = device_get_ivars(child);
46290731Sjhay	rl = &pdev->resources;
46390731Sjhay
46490731Sjhay#ifdef PUC_DEBUG
46590731Sjhay	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
46690731Sjhay	    pdev, type, *rid);
46790731Sjhay	puc_print_resource_list(rl);
46890731Sjhay#endif
46990731Sjhay	retval = NULL;
47090731Sjhay	rle = resource_list_find(rl, type, *rid);
47190731Sjhay	if (rle) {
47290731Sjhay		start = rle->start;
47390731Sjhay		end = rle->end;
47490731Sjhay		count = rle->count;
47590731Sjhay#ifdef PUC_DEBUG
47690731Sjhay		printf("found rle, %lx, %lx, %lx\n", start, end, count);
47790731Sjhay#endif
47890731Sjhay		retval = rle->res;
47990731Sjhay	} else
48090731Sjhay		printf("oops rle is gone\n");
48190731Sjhay
48290731Sjhay	return (retval);
48390731Sjhay}
48490731Sjhay
485102714Sphkint
48690731Sjhaypuc_release_resource(device_t dev, device_t child, int type, int rid,
48790731Sjhay    struct resource *res)
48890731Sjhay{
48990731Sjhay	return (0);
49090731Sjhay}
49190731Sjhay
492102714Sphkint
49390731Sjhaypuc_get_resource(device_t dev, device_t child, int type, int rid,
49490731Sjhay    u_long *startp, u_long *countp)
49590731Sjhay{
49690731Sjhay	struct puc_device *pdev;
49790731Sjhay	struct resource_list *rl;
49890731Sjhay	struct resource_list_entry *rle;
49990731Sjhay
50090731Sjhay	pdev = device_get_ivars(child);
50190731Sjhay	rl = &pdev->resources;
50290731Sjhay
50390731Sjhay#ifdef PUC_DEBUG
50490731Sjhay	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
50590731Sjhay	    type, rid);
50690731Sjhay	puc_print_resource_list(rl);
50790731Sjhay#endif
50890731Sjhay	rle = resource_list_find(rl, type, rid);
50990731Sjhay	if (rle) {
51090731Sjhay#ifdef PUC_DEBUG
51190731Sjhay		printf("found rle %p,", rle);
51290731Sjhay#endif
51390731Sjhay		if (startp != NULL)
51490731Sjhay			*startp = rle->start;
51590731Sjhay		if (countp != NULL)
51690731Sjhay			*countp = rle->count;
51790731Sjhay#ifdef PUC_DEBUG
51890731Sjhay		printf(" %lx, %lx\n", rle->start, rle->count);
51990731Sjhay#endif
52090731Sjhay		return (0);
52190731Sjhay	} else
52290731Sjhay		printf("oops rle is gone\n");
52390731Sjhay	return (ENXIO);
52490731Sjhay}
52590731Sjhay
526102714Sphkint
52790731Sjhaypuc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
52890731Sjhay	       void (*ihand)(void *), void *arg, void **cookiep)
52990731Sjhay{
53090731Sjhay	int i;
53190731Sjhay	struct puc_softc *sc;
53290731Sjhay
533102929Sphk	sc = (struct puc_softc *)device_get_softc(dev);
534102931Sphk	if ((flags & INTR_FAST) != sc->fastintr)
535102895Sphk		return (ENXIO);
53690731Sjhay	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
53790731Sjhay		if (sc->sc_ports[i].dev == child) {
53890731Sjhay			if (sc->sc_ports[i].ihand != 0)
53990731Sjhay				return (ENXIO);
54090731Sjhay			sc->sc_ports[i].ihand = ihand;
54190731Sjhay			sc->sc_ports[i].ihandarg = arg;
54290731Sjhay			*cookiep = arg;
54390731Sjhay			return (0);
54490731Sjhay		}
54590731Sjhay	}
54690731Sjhay	return (ENXIO);
54790731Sjhay}
54890731Sjhay
549102714Sphkint
55090731Sjhaypuc_teardown_intr(device_t dev, device_t child, struct resource *r,
55190731Sjhay		  void *cookie)
55290731Sjhay{
55390731Sjhay	int i;
55490731Sjhay	struct puc_softc *sc;
55590731Sjhay
55690731Sjhay	sc = (struct puc_softc *)device_get_softc(dev);
55790731Sjhay	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
55890731Sjhay		if (sc->sc_ports[i].dev == child) {
55990731Sjhay			sc->sc_ports[i].ihand = NULL;
56090731Sjhay			sc->sc_ports[i].ihandarg = NULL;
56190731Sjhay			return (0);
56290731Sjhay		}
56390731Sjhay	}
56490731Sjhay	return (ENXIO);
56590731Sjhay}
56690731Sjhay
567102714Sphkint
56890731Sjhaypuc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
56990731Sjhay{
57090731Sjhay	struct puc_device *pdev;
57190731Sjhay
57290731Sjhay	pdev = device_get_ivars(child);
57390731Sjhay	if (pdev == NULL)
57490731Sjhay		return (ENOENT);
57590731Sjhay
57690731Sjhay	switch(index) {
57790731Sjhay	case PUC_IVAR_FREQ:
57890731Sjhay		*result = pdev->serialfreq;
57990731Sjhay		break;
58090731Sjhay	default:
58190731Sjhay		return (ENOENT);
58290731Sjhay	}
58390731Sjhay	return (0);
58490731Sjhay}
585