puc.c revision 150549
1245450Sganbold/*	$NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $	*/
2245450Sganbold
3245450Sganbold/*-
4245450Sganbold * Copyright (c) 2002 JF Hay.  All rights reserved.
5245450Sganbold * Copyright (c) 2000 M. Warner Losh.  All rights reserved.
6245450Sganbold *
7245450Sganbold * Redistribution and use in source and binary forms, with or without
8245450Sganbold * modification, are permitted provided that the following conditions
9245450Sganbold * are met:
10245450Sganbold * 1. Redistributions of source code must retain the above copyright
11245450Sganbold *    notice, this list of conditions and the following disclaimer.
12245450Sganbold * 2. Redistributions in binary form must reproduce the above copyright
13245450Sganbold *    notice, this list of conditions and the following disclaimer in the
14245450Sganbold *    documentation and/or other materials provided with the distribution.
15245450Sganbold *
16245450Sganbold * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17245450Sganbold * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18245450Sganbold * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19245450Sganbold * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20245450Sganbold * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21245450Sganbold * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22245450Sganbold * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23245450Sganbold * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24245450Sganbold * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25245450Sganbold * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26245450Sganbold * SUCH DAMAGE.
27245450Sganbold */
28245450Sganbold
29245450Sganbold/*-
30245450Sganbold * Copyright (c) 1996, 1998, 1999
31245450Sganbold *	Christopher G. Demetriou.  All rights reserved.
32245450Sganbold *
33245450Sganbold * Redistribution and use in source and binary forms, with or without
34245450Sganbold * modification, are permitted provided that the following conditions
35245450Sganbold * are met:
36245450Sganbold * 1. Redistributions of source code must retain the above copyright
37245450Sganbold *    notice, this list of conditions and the following disclaimer.
38245450Sganbold * 2. Redistributions in binary form must reproduce the above copyright
39245450Sganbold *    notice, this list of conditions and the following disclaimer in the
40245450Sganbold *    documentation and/or other materials provided with the distribution.
41245450Sganbold * 3. All advertising materials mentioning features or use of this software
42245450Sganbold *    must display the following acknowledgement:
43245450Sganbold *      This product includes software developed by Christopher G. Demetriou
44245450Sganbold *	for the NetBSD Project.
45245450Sganbold * 4. The name of the author may not be used to endorse or promote products
46245450Sganbold *    derived from this software without specific prior written permission
47245450Sganbold *
48245450Sganbold * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49245450Sganbold * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50245450Sganbold * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51245450Sganbold * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52245450Sganbold * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53245450Sganbold * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54245450Sganbold * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55245450Sganbold * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56245450Sganbold * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57245450Sganbold * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58245450Sganbold */
59245450Sganbold
60245450Sganbold#include <sys/cdefs.h>
61245450Sganbold__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 150549 2005-09-25 20:21:14Z phk $");
62245450Sganbold
63245450Sganbold/*
64245450Sganbold * PCI "universal" communication card device driver, glues com, lpt,
65245450Sganbold * and similar ports to PCI via bridge chip often much larger than
66245450Sganbold * the devices being glued.
67245450Sganbold *
68245450Sganbold * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
69245450Sganbold * sys/dev/pci/pciide.c, revision 1.6).
70245450Sganbold *
71245450Sganbold * These devices could be (and some times are) described as
72245450Sganbold * communications/{serial,parallel}, etc. devices with known
73245450Sganbold * programming interfaces, but those programming interfaces (in
74245450Sganbold * particular the BAR assignments for devices, etc.) in fact are not
75245450Sganbold * particularly well defined.
76245450Sganbold *
77245450Sganbold * After I/we have seen more of these devices, it may be possible
78245450Sganbold * to generalize some of these bits.  In particular, devices which
79245450Sganbold * describe themselves as communications/serial/16[45]50, and
80245450Sganbold * communications/parallel/??? might be attached via direct
81245450Sganbold * 'com' and 'lpt' attachments to pci.
82245450Sganbold */
83245450Sganbold
84245450Sganbold#include "opt_puc.h"
85245450Sganbold#include <sys/param.h>
86245450Sganbold#include <sys/systm.h>
87245450Sganbold#include <sys/kernel.h>
88245450Sganbold#include <sys/bus.h>
89245450Sganbold#include <sys/conf.h>
90245450Sganbold#include <sys/malloc.h>
91245450Sganbold
92245450Sganbold#include <machine/bus.h>
93245450Sganbold#include <machine/resource.h>
94245450Sganbold#include <sys/rman.h>
95245450Sganbold
96245450Sganbold#include <dev/pci/pcireg.h>
97245450Sganbold#include <dev/pci/pcivar.h>
98245450Sganbold
99245450Sganbold#define PUC_ENTRAILS	1
100245450Sganbold#include <dev/puc/pucvar.h>
101245450Sganbold
102245450Sganboldstruct puc_device {
103245450Sganbold	struct resource_list resources;
104245450Sganbold	int	port;
105245450Sganbold	int	regshft;
106245450Sganbold	u_int	serialfreq;
107245450Sganbold	u_int	subtype;
108245450Sganbold};
109245450Sganbold
110245450Sganboldstatic void puc_intr(void *arg);
111245450Sganbold
112245450Sganboldstatic int puc_find_free_unit(char *);
113245450Sganbold#ifdef PUC_DEBUG
114245450Sganboldstatic void puc_print_resource_list(struct resource_list *);
115245450Sganbold#endif
116245450Sganbold
117245450Sganbolddevclass_t puc_devclass;
118245450Sganbold
119245450Sganboldstatic int
120245450Sganboldpuc_port_bar_index(struct puc_softc *sc, int bar)
121245450Sganbold{
122245450Sganbold	int i;
123245450Sganbold
124245450Sganbold	for (i = 0; i < PUC_MAX_BAR; i += 1) {
125245450Sganbold		if (!sc->sc_bar_mappings[i].used)
126245450Sganbold			break;
127245450Sganbold		if (sc->sc_bar_mappings[i].bar == bar)
128245450Sganbold			return (i);
129245450Sganbold	}
130245450Sganbold	if (i == PUC_MAX_BAR) {
131245450Sganbold		printf("%s: out of bars!\n", __func__);
132245450Sganbold		return (-1);
133245450Sganbold	}
134245450Sganbold	sc->sc_bar_mappings[i].bar = bar;
135245450Sganbold	sc->sc_bar_mappings[i].used = 1;
136245450Sganbold	return (i);
137245450Sganbold}
138245450Sganbold
139245450Sganboldstatic int
140245450Sganboldpuc_probe_ilr(struct puc_softc *sc, struct resource *res)
141245450Sganbold{
142245450Sganbold	u_char t1, t2;
143245450Sganbold	int i;
144245450Sganbold
145245450Sganbold	switch (sc->sc_desc.ilr_type) {
146245450Sganbold	case PUC_ILR_TYPE_DIGI:
147245450Sganbold		sc->ilr_st = rman_get_bustag(res);
148245450Sganbold		sc->ilr_sh = rman_get_bushandle(res);
149245450Sganbold		for (i = 0; i < 2 && sc->sc_desc.ilr_offset[i] != 0; i++) {
150245450Sganbold			t1 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
151245450Sganbold			    sc->sc_desc.ilr_offset[i]);
152245450Sganbold			t1 = ~t1;
153245450Sganbold			bus_space_write_1(sc->ilr_st, sc->ilr_sh,
154245450Sganbold			    sc->sc_desc.ilr_offset[i], t1);
155245450Sganbold			t2 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
156245450Sganbold			    sc->sc_desc.ilr_offset[i]);
157245450Sganbold			if (t2 == t1)
158245450Sganbold				return (0);
159245450Sganbold		}
160245450Sganbold		return (1);
161245450Sganbold
162245450Sganbold	default:
163245450Sganbold		break;
164245450Sganbold	}
165245450Sganbold	return (0);
166245450Sganbold}
167245450Sganbold
168245450Sganboldint
169245450Sganboldpuc_attach(device_t dev, const struct puc_device_description *desc)
170245450Sganbold{
171245450Sganbold	char *typestr;
172245450Sganbold	int bidx, childunit, i, irq_setup, ressz, rid, type;
173245450Sganbold	struct puc_softc *sc;
174245450Sganbold	struct puc_device *pdev;
175245450Sganbold	struct resource *res;
176245450Sganbold	struct resource_list_entry *rle;
177245450Sganbold	bus_space_handle_t bh;
178245450Sganbold
179245450Sganbold	if (desc == NULL)
180245450Sganbold		return (ENXIO);
181245450Sganbold
182245450Sganbold	sc = (struct puc_softc *)device_get_softc(dev);
183245450Sganbold	bzero(sc, sizeof(*sc));
184245450Sganbold	sc->sc_desc = *desc;
185245450Sganbold
186245450Sganbold#ifdef PUC_DEBUG
187245450Sganbold	bootverbose = 1;
188245450Sganbold
189245450Sganbold	printf("puc: name: %s\n", sc->sc_desc.name);
190245450Sganbold#endif
191245450Sganbold	rid = 0;
192245450Sganbold	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
193245450Sganbold	    RF_ACTIVE | RF_SHAREABLE);
194245450Sganbold	if (!res)
195245450Sganbold		return (ENXIO);
196245450Sganbold
197245450Sganbold	sc->irqres = res;
198245450Sganbold	sc->irqrid = rid;
199245450Sganbold#ifdef PUC_FASTINTR
200245450Sganbold	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
201245450Sganbold	    INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
202245450Sganbold	if (irq_setup == 0)
203245450Sganbold		sc->fastintr = INTR_FAST;
204245450Sganbold	else
205245450Sganbold		irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
206245450Sganbold		    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
207245450Sganbold#else
208245450Sganbold	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
209245450Sganbold	    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
210245450Sganbold#endif
211245450Sganbold	if (irq_setup != 0)
212245450Sganbold		return (ENXIO);
213245450Sganbold
214245450Sganbold	rid = 0;
215245450Sganbold	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
216245450Sganbold		if (i > 0 && rid == sc->sc_desc.ports[i].bar)
217245450Sganbold			sc->barmuxed = 1;
218245450Sganbold		rid = sc->sc_desc.ports[i].bar;
219245450Sganbold		bidx = puc_port_bar_index(sc, rid);
220245450Sganbold
221245450Sganbold		if (bidx < 0 || sc->sc_bar_mappings[bidx].res != NULL)
222245450Sganbold			continue;
223245450Sganbold
224245450Sganbold		type = (sc->sc_desc.ports[i].flags & PUC_FLAGS_MEMORY)
225245450Sganbold		    ? SYS_RES_MEMORY : SYS_RES_IOPORT;
226245450Sganbold
227245450Sganbold		res = bus_alloc_resource_any(dev, type, &rid,
228245450Sganbold		    RF_ACTIVE);
229245450Sganbold		if (res == NULL &&
230245450Sganbold		    sc->sc_desc.ports[i].flags & PUC_FLAGS_ALTRES) {
231245450Sganbold			type = (type == SYS_RES_IOPORT)
232245450Sganbold			    ? SYS_RES_MEMORY : SYS_RES_IOPORT;
233245450Sganbold			res = bus_alloc_resource_any(dev, type, &rid,
234245450Sganbold			    RF_ACTIVE);
235245450Sganbold		}
236245450Sganbold		if (res == NULL) {
237245450Sganbold			device_printf(dev, "could not get resource\n");
238245450Sganbold			continue;
239245450Sganbold		}
240245450Sganbold		sc->sc_bar_mappings[bidx].type = type;
241245450Sganbold		sc->sc_bar_mappings[bidx].res = res;
242245450Sganbold
243245450Sganbold		if (sc->sc_desc.ilr_type != PUC_ILR_TYPE_NONE) {
244245450Sganbold			sc->ilr_enabled = puc_probe_ilr(sc, res);
245245450Sganbold			if (sc->ilr_enabled)
246245450Sganbold				device_printf(dev, "ILR enabled\n");
247245450Sganbold			else
248245450Sganbold				device_printf(dev, "ILR disabled\n");
249245450Sganbold		}
250245450Sganbold#ifdef PUC_DEBUG
251245450Sganbold		printf("%s rid %d bst %lx, start %lx, end %lx\n",
252245450Sganbold		    (type == SYS_RES_MEMORY) ? "memory" : "port", rid,
253245450Sganbold		    (u_long)rman_get_bustag(res), (u_long)rman_get_start(res),
254245450Sganbold		    (u_long)rman_get_end(res));
255245450Sganbold#endif
256245450Sganbold	}
257245450Sganbold
258245450Sganbold	if (desc->init != NULL) {
259245450Sganbold		i = desc->init(sc);
260245450Sganbold		if (i != 0)
261245450Sganbold			return (i);
262245450Sganbold	}
263245450Sganbold
264245450Sganbold	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
265245450Sganbold		rid = sc->sc_desc.ports[i].bar;
266245450Sganbold		bidx = puc_port_bar_index(sc, rid);
267245450Sganbold		if (bidx < 0 || sc->sc_bar_mappings[bidx].res == NULL)
268245450Sganbold			continue;
269245450Sganbold
270245450Sganbold		switch (sc->sc_desc.ports[i].type & ~PUC_PORT_SUBTYPE_MASK) {
271245450Sganbold		case PUC_PORT_TYPE_COM:
272245450Sganbold			typestr = "sio";
273245450Sganbold			break;
274245450Sganbold		case PUC_PORT_TYPE_LPT:
275245450Sganbold			typestr = "ppc";
276245450Sganbold			break;
277245450Sganbold		case PUC_PORT_TYPE_UART:
278245450Sganbold			typestr = "uart";
279245450Sganbold			break;
280245450Sganbold		default:
281245450Sganbold			continue;
282245450Sganbold		}
283245450Sganbold		switch (sc->sc_desc.ports[i].type & PUC_PORT_SUBTYPE_MASK) {
284245450Sganbold		case PUC_PORT_UART_SAB82532:
285245450Sganbold			ressz = 64;
286245450Sganbold			break;
287245450Sganbold		case PUC_PORT_UART_Z8530:
288245450Sganbold			ressz = 2;
289245450Sganbold			break;
290245450Sganbold		default:
291245450Sganbold			ressz = 8;
292245450Sganbold			break;
293245450Sganbold		}
294245450Sganbold		pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
295245450Sganbold		    M_NOWAIT | M_ZERO);
296245450Sganbold		if (!pdev)
297245450Sganbold			continue;
298245450Sganbold		resource_list_init(&pdev->resources);
299245450Sganbold
300245450Sganbold		/* First fake up an IRQ resource. */
301245450Sganbold		resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
302245450Sganbold		    rman_get_start(sc->irqres), rman_get_end(sc->irqres),
303245450Sganbold		    rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
304245450Sganbold		rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
305245450Sganbold		rle->res = sc->irqres;
306245450Sganbold
307245450Sganbold		/* Now fake an IOPORT or MEMORY resource */
308245450Sganbold		res = sc->sc_bar_mappings[bidx].res;
309245450Sganbold		type = sc->sc_bar_mappings[bidx].type;
310245450Sganbold		resource_list_add(&pdev->resources, type, 0,
311245450Sganbold		    rman_get_start(res) + sc->sc_desc.ports[i].offset,
312245450Sganbold		    rman_get_start(res) + sc->sc_desc.ports[i].offset
313245450Sganbold		    + ressz - 1, ressz);
314245450Sganbold		rle = resource_list_find(&pdev->resources, type, 0);
315245450Sganbold
316245450Sganbold		if (sc->barmuxed == 0) {
317245450Sganbold			rle->res = sc->sc_bar_mappings[bidx].res;
318245450Sganbold		} else {
319245450Sganbold			rle->res = malloc(sizeof(struct resource), M_DEVBUF,
320245450Sganbold			    M_WAITOK | M_ZERO);
321245450Sganbold			if (rle->res == NULL) {
322245450Sganbold				free(pdev, M_DEVBUF);
323245450Sganbold				return (ENOMEM);
324245450Sganbold			}
325245450Sganbold
326245450Sganbold			rman_set_start(rle->res, rman_get_start(res) +
327245450Sganbold			    sc->sc_desc.ports[i].offset);
328245450Sganbold			rman_set_end(rle->res, rman_get_start(rle->res) +
329245450Sganbold			    ressz - 1);
330245450Sganbold			rman_set_bustag(rle->res, rman_get_bustag(res));
331245450Sganbold			bus_space_subregion(rman_get_bustag(rle->res),
332245450Sganbold			    rman_get_bushandle(res),
333245450Sganbold			    sc->sc_desc.ports[i].offset, ressz,
334245450Sganbold			    &bh);
335245450Sganbold			rman_set_bushandle(rle->res, bh);
336245450Sganbold		}
337245450Sganbold
338245450Sganbold		pdev->port = i + 1;
339245450Sganbold		pdev->serialfreq = sc->sc_desc.ports[i].serialfreq;
340245450Sganbold		pdev->subtype = sc->sc_desc.ports[i].type &
341245450Sganbold		    PUC_PORT_SUBTYPE_MASK;
342		pdev->regshft = sc->sc_desc.ports[i].regshft;
343
344		childunit = puc_find_free_unit(typestr);
345		if (childunit < 0 && strcmp(typestr, "uart") != 0) {
346			typestr = "uart";
347			childunit = puc_find_free_unit(typestr);
348		}
349		sc->sc_ports[i].dev = device_add_child(dev, typestr,
350		    childunit);
351		if (sc->sc_ports[i].dev == NULL) {
352			if (sc->barmuxed) {
353				bus_space_unmap(rman_get_bustag(rle->res),
354				    rman_get_bushandle(rle->res), ressz);
355				free(rle->res, M_DEVBUF);
356				free(pdev, M_DEVBUF);
357			}
358			continue;
359		}
360		device_set_ivars(sc->sc_ports[i].dev, pdev);
361		device_set_desc(sc->sc_ports[i].dev, sc->sc_desc.name);
362#ifdef PUC_DEBUG
363		printf("puc: type %d, bar %x, offset %x\n",
364		    sc->sc_desc.ports[i].type,
365		    sc->sc_desc.ports[i].bar,
366		    sc->sc_desc.ports[i].offset);
367		puc_print_resource_list(&pdev->resources);
368#endif
369		device_set_flags(sc->sc_ports[i].dev,
370		    sc->sc_desc.ports[i].flags);
371		if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
372			if (sc->barmuxed) {
373				bus_space_unmap(rman_get_bustag(rle->res),
374				    rman_get_bushandle(rle->res), ressz);
375				free(rle->res, M_DEVBUF);
376				free(pdev, M_DEVBUF);
377			}
378		}
379	}
380
381#ifdef PUC_DEBUG
382	bootverbose = 0;
383#endif
384	return (0);
385}
386
387static u_int32_t
388puc_ilr_read(struct puc_softc *sc)
389{
390	u_int32_t mask;
391	int i;
392
393	mask = 0;
394	switch (sc->sc_desc.ilr_type) {
395	case PUC_ILR_TYPE_DIGI:
396		for (i = 1; i >= 0 && sc->sc_desc.ilr_offset[i] != 0; i--) {
397			mask = (mask << 8) | (bus_space_read_1(sc->ilr_st,
398			    sc->ilr_sh, sc->sc_desc.ilr_offset[i]) & 0xff);
399		}
400		break;
401
402	default:
403		mask = 0xffffffff;
404		break;
405	}
406	return (mask);
407}
408
409/*
410 * This is an interrupt handler. For boards that can't tell us which
411 * device generated the interrupt it just calls all the registered
412 * handlers sequencially, but for boards that can tell us which
413 * device(s) generated the interrupt it calls only handlers for devices
414 * that actually generated the interrupt.
415 */
416static void
417puc_intr(void *arg)
418{
419	int i;
420	u_int32_t ilr_mask;
421	struct puc_softc *sc;
422
423	sc = (struct puc_softc *)arg;
424	ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff;
425	for (i = 0; i < PUC_MAX_PORTS; i++)
426		if (sc->sc_ports[i].ihand != NULL &&
427		    ((ilr_mask >> i) & 0x00000001))
428			(sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
429}
430
431static int
432puc_find_free_unit(char *name)
433{
434	devclass_t dc;
435	int start;
436	int unit;
437
438	unit = 0;
439	start = 0;
440	while (resource_int_value(name, unit, "port", &start) == 0 &&
441	    start > 0)
442		unit++;
443	dc = devclass_find(name);
444	if (dc == NULL)
445		return (-1);
446	while (devclass_get_device(dc, unit))
447		unit++;
448#ifdef PUC_DEBUG
449	printf("puc: Using %s%d\n", name, unit);
450#endif
451	return (unit);
452}
453
454#ifdef PUC_DEBUG
455static void
456puc_print_resource_list(struct resource_list *rl)
457{
458#if 0
459	struct resource_list_entry *rle;
460
461	printf("print_resource_list: rl %p\n", rl);
462	SLIST_FOREACH(rle, rl, link)
463		printf("  type %x, rid %x start %lx end %lx count %lx\n",
464		    rle->type, rle->rid, rle->start, rle->end, rle->count);
465	printf("print_resource_list: end.\n");
466#endif
467}
468#endif
469
470struct resource *
471puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
472    u_long start, u_long end, u_long count, u_int flags)
473{
474	struct puc_device *pdev;
475	struct resource *retval;
476	struct resource_list *rl;
477	struct resource_list_entry *rle;
478	device_t my_child;
479
480	/*
481	 * in the case of a child of child we need to find our immediate child
482	 */
483	for (my_child = child; device_get_parent(my_child) != dev;
484	     my_child = device_get_parent(my_child));
485
486	pdev = device_get_ivars(my_child);
487	rl = &pdev->resources;
488
489#ifdef PUC_DEBUG
490	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
491	    pdev, type, *rid);
492	puc_print_resource_list(rl);
493#endif
494	retval = NULL;
495	rle = resource_list_find(rl, type, *rid);
496	if (rle) {
497#ifdef PUC_DEBUG
498		printf("found rle, %lx, %lx, %lx\n", rle->start, rle->end,
499		    rle->count);
500#endif
501		retval = rle->res;
502	}
503#ifdef PUC_DEBUG
504	else
505		printf("oops rle is gone\n");
506#endif
507
508	return (retval);
509}
510
511int
512puc_release_resource(device_t dev, device_t child, int type, int rid,
513    struct resource *res)
514{
515	return (0);
516}
517
518int
519puc_get_resource(device_t dev, device_t child, int type, int rid,
520    u_long *startp, u_long *countp)
521{
522	struct puc_device *pdev;
523	struct resource_list *rl;
524	struct resource_list_entry *rle;
525
526	pdev = device_get_ivars(child);
527	rl = &pdev->resources;
528
529#ifdef PUC_DEBUG
530	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
531	    type, rid);
532	puc_print_resource_list(rl);
533#endif
534	rle = resource_list_find(rl, type, rid);
535	if (rle) {
536#ifdef PUC_DEBUG
537		printf("found rle %p,", rle);
538#endif
539		if (startp != NULL)
540			*startp = rle->start;
541		if (countp != NULL)
542			*countp = rle->count;
543#ifdef PUC_DEBUG
544		printf(" %lx, %lx\n", rle->start, rle->count);
545#endif
546		return (0);
547	} else
548		printf("oops rle is gone\n");
549	return (ENXIO);
550}
551
552int
553puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
554	       void (*ihand)(void *), void *arg, void **cookiep)
555{
556	int i;
557	struct puc_softc *sc;
558
559	sc = (struct puc_softc *)device_get_softc(dev);
560	if ((flags & INTR_FAST) != sc->fastintr)
561		return (ENXIO);
562	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
563		if (sc->sc_ports[i].dev == child) {
564			if (sc->sc_ports[i].ihand != 0)
565				return (ENXIO);
566			sc->sc_ports[i].ihand = ihand;
567			sc->sc_ports[i].ihandarg = arg;
568			*cookiep = arg;
569			return (0);
570		}
571	}
572	return (ENXIO);
573}
574
575int
576puc_teardown_intr(device_t dev, device_t child, struct resource *r,
577		  void *cookie)
578{
579	int i;
580	struct puc_softc *sc;
581
582	sc = (struct puc_softc *)device_get_softc(dev);
583	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
584		if (sc->sc_ports[i].dev == child) {
585			sc->sc_ports[i].ihand = NULL;
586			sc->sc_ports[i].ihandarg = NULL;
587			return (0);
588		}
589	}
590	return (ENXIO);
591}
592
593int
594puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
595{
596	struct puc_device *pdev;
597
598	pdev = device_get_ivars(child);
599	if (pdev == NULL)
600		return (ENOENT);
601
602	switch(index) {
603	case PUC_IVAR_FREQ:
604		*result = pdev->serialfreq;
605		break;
606	case PUC_IVAR_PORT:
607		*result = pdev->port;
608		break;
609	case PUC_IVAR_REGSHFT:
610		*result = pdev->regshft;
611		break;
612	case PUC_IVAR_SUBTYPE:
613		*result = pdev->subtype;
614		break;
615	default:
616		return (ENOENT);
617	}
618	return (0);
619}
620