puc.c revision 142502
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
11140040Simp *    notice, this list of conditions and the following disclaimer.
1290731Sjhay * 2. Redistributions in binary form must reproduce the above copyright
1390731Sjhay *    notice, this list of conditions and the following disclaimer in the
1490731Sjhay *    documentation and/or other materials provided with the distribution.
1590731Sjhay *
16140040Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17140040Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18140040Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19140040Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20140040Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21140040Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22140040Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23140040Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24140040Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25140040Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26140040Simp * SUCH DAMAGE.
2790731Sjhay */
2890731Sjhay
29139749Simp/*-
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 142502 2005-02-25 19:47:18Z sam $");
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
84140046Simp#define __RMAN_RESOURCE_VISIBLE	/* Shouldn't be there */
85102751Sjmallett#include "opt_puc.h"
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;
105120451Smarcel	int	port;
106120451Smarcel	int	regshft;
107119814Smarcel	u_int	serialfreq;
108119814Smarcel	u_int	subtype;
10990731Sjhay};
11090731Sjhay
11190731Sjhaystatic void puc_intr(void *arg);
11290731Sjhay
11390731Sjhaystatic int puc_find_free_unit(char *);
11490731Sjhay#ifdef PUC_DEBUG
11590731Sjhaystatic void puc_print_resource_list(struct resource_list *);
11690731Sjhay#endif
11790731Sjhay
118102893Sphkdevclass_t puc_devclass;
119102893Sphk
120102734Sphkstatic int
121102734Sphkpuc_port_bar_index(struct puc_softc *sc, int bar)
122102734Sphk{
123102734Sphk	int i;
124102734Sphk
125102734Sphk	for (i = 0; i < PUC_MAX_BAR; i += 1) {
126102734Sphk		if (!sc->sc_bar_mappings[i].used)
127102734Sphk			break;
128102734Sphk		if (sc->sc_bar_mappings[i].bar == bar)
129102734Sphk			return (i);
130102734Sphk	}
131142502Ssam	if (i == PUC_MAX_BAR) {
132142502Ssam		printf("%s: out of bars!\n", __func__);
133142502Ssam		return (-1);
134142502Ssam	}
135102734Sphk	sc->sc_bar_mappings[i].bar = bar;
136102734Sphk	sc->sc_bar_mappings[i].used = 1;
137102734Sphk	return (i);
138102734Sphk}
139102734Sphk
140112270Ssobomaxstatic int
141112270Ssobomaxpuc_probe_ilr(struct puc_softc *sc, struct resource *res)
142112270Ssobomax{
143112270Ssobomax	u_char t1, t2;
144112270Ssobomax	int i;
145112270Ssobomax
146119814Smarcel	switch (sc->sc_desc.ilr_type) {
147112270Ssobomax	case PUC_ILR_TYPE_DIGI:
148112270Ssobomax		sc->ilr_st = rman_get_bustag(res);
149112270Ssobomax		sc->ilr_sh = rman_get_bushandle(res);
150119814Smarcel		for (i = 0; i < 2 && sc->sc_desc.ilr_offset[i] != 0; i++) {
151112270Ssobomax			t1 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
152119814Smarcel			    sc->sc_desc.ilr_offset[i]);
153112270Ssobomax			t1 = ~t1;
154112270Ssobomax			bus_space_write_1(sc->ilr_st, sc->ilr_sh,
155119814Smarcel			    sc->sc_desc.ilr_offset[i], t1);
156112270Ssobomax			t2 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
157119814Smarcel			    sc->sc_desc.ilr_offset[i]);
158112270Ssobomax			if (t2 == t1)
159112270Ssobomax				return (0);
160112270Ssobomax		}
161112270Ssobomax		return (1);
162112270Ssobomax
163112270Ssobomax	default:
164112270Ssobomax		break;
165112270Ssobomax	}
166112270Ssobomax	return (0);
167112270Ssobomax}
168112270Ssobomax
169102714Sphkint
170102714Sphkpuc_attach(device_t dev, const struct puc_device_description *desc)
17190731Sjhay{
17290731Sjhay	char *typestr;
173119814Smarcel	int bidx, childunit, i, irq_setup, ressz, rid, type;
17490731Sjhay	struct puc_softc *sc;
17590731Sjhay	struct puc_device *pdev;
17690731Sjhay	struct resource *res;
17790731Sjhay	struct resource_list_entry *rle;
178131418Simp	bus_space_handle_t bh;
17990731Sjhay
180119814Smarcel	if (desc == NULL)
181119814Smarcel		return (ENXIO);
182119814Smarcel
18390731Sjhay	sc = (struct puc_softc *)device_get_softc(dev);
18490731Sjhay	bzero(sc, sizeof(*sc));
185119814Smarcel	sc->sc_desc = *desc;
18690731Sjhay
18790731Sjhay#ifdef PUC_DEBUG
18890731Sjhay	bootverbose = 1;
18990731Sjhay
190119814Smarcel	printf("puc: name: %s\n", sc->sc_desc.name);
19190731Sjhay#endif
19290731Sjhay	rid = 0;
193127135Snjl	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
19490731Sjhay	    RF_ACTIVE | RF_SHAREABLE);
19590731Sjhay	if (!res)
19690731Sjhay		return (ENXIO);
19790731Sjhay
19890731Sjhay	sc->irqres = res;
19990731Sjhay	sc->irqrid = rid;
200102929Sphk#ifdef PUC_FASTINTR
20190731Sjhay	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
202102929Sphk	    INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
203102929Sphk	if (irq_setup == 0)
204102931Sphk		sc->fastintr = INTR_FAST;
205102929Sphk	else
206102929Sphk		irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
207102929Sphk		    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
208102929Sphk#else
209102929Sphk	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
210102895Sphk	    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
211102929Sphk#endif
21290731Sjhay	if (irq_setup != 0)
21390731Sjhay		return (ENXIO);
21490731Sjhay
21590731Sjhay	rid = 0;
21690731Sjhay	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
217119814Smarcel		if (i > 0 && rid == sc->sc_desc.ports[i].bar)
21890731Sjhay			sc->barmuxed = 1;
219119814Smarcel		rid = sc->sc_desc.ports[i].bar;
220102734Sphk		bidx = puc_port_bar_index(sc, rid);
22190731Sjhay
222142502Ssam		if (bidx < 0 || sc->sc_bar_mappings[bidx].res != NULL)
22390731Sjhay			continue;
224109458Smarcel
225119814Smarcel		type = (sc->sc_desc.ports[i].flags & PUC_FLAGS_MEMORY)
226109458Smarcel		    ? SYS_RES_MEMORY : SYS_RES_IOPORT;
227109458Smarcel
228127135Snjl		res = bus_alloc_resource_any(dev, type, &rid,
229109458Smarcel		    RF_ACTIVE);
230119814Smarcel		if (res == NULL &&
231119814Smarcel		    sc->sc_desc.ports[i].flags & PUC_FLAGS_ALTRES) {
232119814Smarcel			type = (type == SYS_RES_IOPORT)
233119814Smarcel			    ? SYS_RES_MEMORY : SYS_RES_IOPORT;
234127135Snjl			res = bus_alloc_resource_any(dev, type, &rid,
235119814Smarcel			    RF_ACTIVE);
236119814Smarcel		}
23790731Sjhay		if (res == NULL) {
238119814Smarcel			device_printf(dev, "could not get resource\n");
23990731Sjhay			continue;
24090731Sjhay		}
241109458Smarcel		sc->sc_bar_mappings[bidx].type = type;
24290731Sjhay		sc->sc_bar_mappings[bidx].res = res;
243112270Ssobomax
244119814Smarcel		if (sc->sc_desc.ilr_type != PUC_ILR_TYPE_NONE) {
245112270Ssobomax			sc->ilr_enabled = puc_probe_ilr(sc, res);
246112270Ssobomax			if (sc->ilr_enabled)
247112270Ssobomax				device_printf(dev, "ILR enabled\n");
248112270Ssobomax			else
249112270Ssobomax				device_printf(dev, "ILR disabled\n");
250112270Ssobomax		}
25190731Sjhay#ifdef PUC_DEBUG
252119814Smarcel		printf("%s rid %d bst %lx, start %lx, end %lx\n",
253109458Smarcel		    (type == SYS_RES_MEMORY) ? "memory" : "port", rid,
254119814Smarcel		    (u_long)rman_get_bustag(res), (u_long)rman_get_start(res),
255119814Smarcel		    (u_long)rman_get_end(res));
25690731Sjhay#endif
25790731Sjhay	}
25890731Sjhay
259102734Sphk	if (desc->init != NULL) {
260102734Sphk		i = desc->init(sc);
261102734Sphk		if (i != 0)
262102734Sphk			return (i);
263102734Sphk	}
26490731Sjhay
26590731Sjhay	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
266119814Smarcel		rid = sc->sc_desc.ports[i].bar;
267102734Sphk		bidx = puc_port_bar_index(sc, rid);
268142502Ssam		if (bidx < 0 || sc->sc_bar_mappings[bidx].res == NULL)
26990731Sjhay			continue;
27090731Sjhay
271119814Smarcel		switch (sc->sc_desc.ports[i].type & ~PUC_PORT_SUBTYPE_MASK) {
27290731Sjhay		case PUC_PORT_TYPE_COM:
27390731Sjhay			typestr = "sio";
27490731Sjhay			break;
275118292Sambrisko		case PUC_PORT_TYPE_LPT:
276118292Sambrisko			typestr = "ppc";
277118292Sambrisko			break;
278119814Smarcel		case PUC_PORT_TYPE_UART:
279119814Smarcel			typestr = "uart";
280119814Smarcel			break;
28190731Sjhay		default:
28290731Sjhay			continue;
28390731Sjhay		}
284119814Smarcel		switch (sc->sc_desc.ports[i].type & PUC_PORT_SUBTYPE_MASK) {
285119814Smarcel		case PUC_PORT_UART_SAB82532:
286119814Smarcel			ressz = 64;
287119814Smarcel			break;
288119814Smarcel		case PUC_PORT_UART_Z8530:
289119814Smarcel			ressz = 2;
290119814Smarcel			break;
291119814Smarcel		default:
292119814Smarcel			ressz = 8;
293119814Smarcel			break;
294119814Smarcel		}
29590731Sjhay		pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
29690731Sjhay		    M_NOWAIT | M_ZERO);
29790731Sjhay		if (!pdev)
29890731Sjhay			continue;
29990731Sjhay		resource_list_init(&pdev->resources);
30090731Sjhay
30190731Sjhay		/* First fake up an IRQ resource. */
30290731Sjhay		resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
30390731Sjhay		    rman_get_start(sc->irqres), rman_get_end(sc->irqres),
30490731Sjhay		    rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
30590731Sjhay		rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
30690731Sjhay		rle->res = sc->irqres;
30790731Sjhay
308109458Smarcel		/* Now fake an IOPORT or MEMORY resource */
30990731Sjhay		res = sc->sc_bar_mappings[bidx].res;
310109458Smarcel		type = sc->sc_bar_mappings[bidx].type;
311109458Smarcel		resource_list_add(&pdev->resources, type, 0,
312119814Smarcel		    rman_get_start(res) + sc->sc_desc.ports[i].offset,
313119814Smarcel		    rman_get_start(res) + sc->sc_desc.ports[i].offset
314119814Smarcel		    + ressz - 1, ressz);
315109458Smarcel		rle = resource_list_find(&pdev->resources, type, 0);
31690731Sjhay
31790731Sjhay		if (sc->barmuxed == 0) {
31890731Sjhay			rle->res = sc->sc_bar_mappings[bidx].res;
31990731Sjhay		} else {
32090731Sjhay			rle->res = malloc(sizeof(struct resource), M_DEVBUF,
321111119Simp			    M_WAITOK | M_ZERO);
32290925Snyan			if (rle->res == NULL) {
32390925Snyan				free(pdev, M_DEVBUF);
32490731Sjhay				return (ENOMEM);
32590925Snyan			}
32690731Sjhay
327131418Simp			rman_set_start(rle->res, rman_get_start(res) +
328131418Simp			    sc->sc_desc.ports[i].offset);
329131418Simp			rman_set_end(rle->res, rman_get_start(rle->res) +
330131418Simp			    ressz - 1);
331131418Simp			rman_set_bustag(rle->res, rman_get_bustag(res));
332131418Simp			bus_space_subregion(rman_get_bustag(rle->res),
33390731Sjhay			    rman_get_bushandle(res),
334119814Smarcel			    sc->sc_desc.ports[i].offset, ressz,
335131418Simp			    &bh);
336131418Simp			rman_set_bushandle(rle->res, bh);
33790731Sjhay		}
33890731Sjhay
339120451Smarcel		pdev->port = i + 1;
340119814Smarcel		pdev->serialfreq = sc->sc_desc.ports[i].serialfreq;
341119814Smarcel		pdev->subtype = sc->sc_desc.ports[i].type &
342119814Smarcel		    PUC_PORT_SUBTYPE_MASK;
343119814Smarcel		pdev->regshft = sc->sc_desc.ports[i].regshft;
34490731Sjhay
34590731Sjhay		childunit = puc_find_free_unit(typestr);
346119814Smarcel		if (childunit < 0 && strcmp(typestr, "uart") != 0) {
347119814Smarcel			typestr = "uart";
348119814Smarcel			childunit = puc_find_free_unit(typestr);
349119814Smarcel		}
350119814Smarcel		sc->sc_ports[i].dev = device_add_child(dev, typestr,
351119814Smarcel		    childunit);
35290925Snyan		if (sc->sc_ports[i].dev == NULL) {
35390925Snyan			if (sc->barmuxed) {
35490925Snyan				bus_space_unmap(rman_get_bustag(rle->res),
355119814Smarcel				    rman_get_bushandle(rle->res), ressz);
35690925Snyan				free(rle->res, M_DEVBUF);
35790925Snyan				free(pdev, M_DEVBUF);
35890925Snyan			}
35990731Sjhay			continue;
36090925Snyan		}
36190731Sjhay		device_set_ivars(sc->sc_ports[i].dev, pdev);
362119814Smarcel		device_set_desc(sc->sc_ports[i].dev, sc->sc_desc.name);
36390731Sjhay#ifdef PUC_DEBUG
36490731Sjhay		printf("puc: type %d, bar %x, offset %x\n",
365119814Smarcel		    sc->sc_desc.ports[i].type,
366119814Smarcel		    sc->sc_desc.ports[i].bar,
367119814Smarcel		    sc->sc_desc.ports[i].offset);
368102714Sphk		puc_print_resource_list(&pdev->resources);
36990731Sjhay#endif
370104068Sphk		device_set_flags(sc->sc_ports[i].dev,
371119814Smarcel		    sc->sc_desc.ports[i].flags);
37290925Snyan		if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
37390925Snyan			if (sc->barmuxed) {
37490925Snyan				bus_space_unmap(rman_get_bustag(rle->res),
375119814Smarcel				    rman_get_bushandle(rle->res), ressz);
37690925Snyan				free(rle->res, M_DEVBUF);
37790925Snyan				free(pdev, M_DEVBUF);
37890925Snyan			}
37990925Snyan		}
38090731Sjhay	}
38190731Sjhay
38290731Sjhay#ifdef PUC_DEBUG
38390731Sjhay	bootverbose = 0;
38490731Sjhay#endif
38590731Sjhay	return (0);
38690731Sjhay}
38790731Sjhay
388112270Ssobomaxstatic u_int32_t
389112270Ssobomaxpuc_ilr_read(struct puc_softc *sc)
390112270Ssobomax{
391112270Ssobomax	u_int32_t mask;
392112270Ssobomax	int i;
393112270Ssobomax
394112270Ssobomax	mask = 0;
395119814Smarcel	switch (sc->sc_desc.ilr_type) {
396112270Ssobomax	case PUC_ILR_TYPE_DIGI:
397119814Smarcel		for (i = 1; i >= 0 && sc->sc_desc.ilr_offset[i] != 0; i--) {
398112270Ssobomax			mask = (mask << 8) | (bus_space_read_1(sc->ilr_st,
399119814Smarcel			    sc->ilr_sh, sc->sc_desc.ilr_offset[i]) & 0xff);
400112270Ssobomax		}
401112270Ssobomax		break;
402112270Ssobomax
403112270Ssobomax	default:
404112270Ssobomax		mask = 0xffffffff;
405112270Ssobomax		break;
406112270Ssobomax	}
407112270Ssobomax	return (mask);
408112270Ssobomax}
409112270Ssobomax
41090731Sjhay/*
411112270Ssobomax * This is an interrupt handler. For boards that can't tell us which
412112270Ssobomax * device generated the interrupt it just calls all the registered
413112270Ssobomax * handlers sequencially, but for boards that can tell us which
414112270Ssobomax * device(s) generated the interrupt it calls only handlers for devices
415112270Ssobomax * that actually generated the interrupt.
41690731Sjhay */
41790731Sjhaystatic void
41890731Sjhaypuc_intr(void *arg)
41990731Sjhay{
42090731Sjhay	int i;
421112270Ssobomax	u_int32_t ilr_mask;
42290731Sjhay	struct puc_softc *sc;
42390731Sjhay
42490731Sjhay	sc = (struct puc_softc *)arg;
425112270Ssobomax	ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff;
42690731Sjhay	for (i = 0; i < PUC_MAX_PORTS; i++)
427112270Ssobomax		if (sc->sc_ports[i].ihand != NULL &&
428112270Ssobomax		    ((ilr_mask >> i) & 0x00000001))
42990731Sjhay			(sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
43090731Sjhay}
43190731Sjhay
432102894Sphkstatic int
433102894Sphkpuc_find_free_unit(char *name)
43490731Sjhay{
43590731Sjhay	devclass_t dc;
43690731Sjhay	int start;
43790731Sjhay	int unit;
43890731Sjhay
43990731Sjhay	unit = 0;
44090731Sjhay	start = 0;
44190731Sjhay	while (resource_int_value(name, unit, "port", &start) == 0 &&
44290731Sjhay	    start > 0)
44390731Sjhay		unit++;
44490731Sjhay	dc = devclass_find(name);
44590731Sjhay	if (dc == NULL)
44690731Sjhay		return (-1);
44790731Sjhay	while (devclass_get_device(dc, unit))
44890731Sjhay		unit++;
44990731Sjhay#ifdef PUC_DEBUG
45090731Sjhay	printf("puc: Using %s%d\n", name, unit);
45190731Sjhay#endif
45290731Sjhay	return (unit);
45390731Sjhay}
45490731Sjhay
45590731Sjhay#ifdef PUC_DEBUG
45690731Sjhaystatic void
45790731Sjhaypuc_print_resource_list(struct resource_list *rl)
45890731Sjhay{
459102734Sphk#if 0
46090731Sjhay	struct resource_list_entry *rle;
46190731Sjhay
46290731Sjhay	printf("print_resource_list: rl %p\n", rl);
46390731Sjhay	SLIST_FOREACH(rle, rl, link)
464119814Smarcel		printf("  type %x, rid %x start %lx end %lx count %lx\n",
465102734Sphk		    rle->type, rle->rid, rle->start, rle->end, rle->count);
46690731Sjhay	printf("print_resource_list: end.\n");
467102734Sphk#endif
46890731Sjhay}
46990731Sjhay#endif
47090731Sjhay
471102714Sphkstruct resource *
47290731Sjhaypuc_alloc_resource(device_t dev, device_t child, int type, int *rid,
47390731Sjhay    u_long start, u_long end, u_long count, u_int flags)
47490731Sjhay{
47590731Sjhay	struct puc_device *pdev;
47690731Sjhay	struct resource *retval;
47790731Sjhay	struct resource_list *rl;
47890731Sjhay	struct resource_list_entry *rle;
479118292Sambrisko	device_t my_child;
48090731Sjhay
481118292Sambrisko	/*
482118292Sambrisko	 * in the case of a child of child we need to find our immediate child
483118292Sambrisko	 */
484118292Sambrisko	for (my_child = child; device_get_parent(my_child) != dev;
485118292Sambrisko	     my_child = device_get_parent(my_child));
486118292Sambrisko
487118292Sambrisko	pdev = device_get_ivars(my_child);
48890731Sjhay	rl = &pdev->resources;
48990731Sjhay
49090731Sjhay#ifdef PUC_DEBUG
49190731Sjhay	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
49290731Sjhay	    pdev, type, *rid);
49390731Sjhay	puc_print_resource_list(rl);
49490731Sjhay#endif
49590731Sjhay	retval = NULL;
49690731Sjhay	rle = resource_list_find(rl, type, *rid);
49790731Sjhay	if (rle) {
49890731Sjhay#ifdef PUC_DEBUG
499119814Smarcel		printf("found rle, %lx, %lx, %lx\n", rle->start, rle->end,
500119814Smarcel		    rle->count);
50190731Sjhay#endif
50290731Sjhay		retval = rle->res;
503118292Sambrisko	}
504118292Sambrisko#ifdef PUC_DEBUG
505118292Sambrisko	else
50690731Sjhay		printf("oops rle is gone\n");
507118292Sambrisko#endif
50890731Sjhay
50990731Sjhay	return (retval);
51090731Sjhay}
51190731Sjhay
512102714Sphkint
51390731Sjhaypuc_release_resource(device_t dev, device_t child, int type, int rid,
51490731Sjhay    struct resource *res)
51590731Sjhay{
51690731Sjhay	return (0);
51790731Sjhay}
51890731Sjhay
519102714Sphkint
52090731Sjhaypuc_get_resource(device_t dev, device_t child, int type, int rid,
52190731Sjhay    u_long *startp, u_long *countp)
52290731Sjhay{
52390731Sjhay	struct puc_device *pdev;
52490731Sjhay	struct resource_list *rl;
52590731Sjhay	struct resource_list_entry *rle;
52690731Sjhay
52790731Sjhay	pdev = device_get_ivars(child);
52890731Sjhay	rl = &pdev->resources;
52990731Sjhay
53090731Sjhay#ifdef PUC_DEBUG
53190731Sjhay	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
53290731Sjhay	    type, rid);
53390731Sjhay	puc_print_resource_list(rl);
53490731Sjhay#endif
53590731Sjhay	rle = resource_list_find(rl, type, rid);
53690731Sjhay	if (rle) {
53790731Sjhay#ifdef PUC_DEBUG
53890731Sjhay		printf("found rle %p,", rle);
53990731Sjhay#endif
54090731Sjhay		if (startp != NULL)
54190731Sjhay			*startp = rle->start;
54290731Sjhay		if (countp != NULL)
54390731Sjhay			*countp = rle->count;
54490731Sjhay#ifdef PUC_DEBUG
54590731Sjhay		printf(" %lx, %lx\n", rle->start, rle->count);
54690731Sjhay#endif
54790731Sjhay		return (0);
54890731Sjhay	} else
54990731Sjhay		printf("oops rle is gone\n");
55090731Sjhay	return (ENXIO);
55190731Sjhay}
55290731Sjhay
553102714Sphkint
55490731Sjhaypuc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
55590731Sjhay	       void (*ihand)(void *), void *arg, void **cookiep)
55690731Sjhay{
55790731Sjhay	int i;
55890731Sjhay	struct puc_softc *sc;
55990731Sjhay
560102929Sphk	sc = (struct puc_softc *)device_get_softc(dev);
561102931Sphk	if ((flags & INTR_FAST) != sc->fastintr)
562102895Sphk		return (ENXIO);
56390731Sjhay	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
56490731Sjhay		if (sc->sc_ports[i].dev == child) {
56590731Sjhay			if (sc->sc_ports[i].ihand != 0)
56690731Sjhay				return (ENXIO);
56790731Sjhay			sc->sc_ports[i].ihand = ihand;
56890731Sjhay			sc->sc_ports[i].ihandarg = arg;
56990731Sjhay			*cookiep = arg;
57090731Sjhay			return (0);
57190731Sjhay		}
57290731Sjhay	}
57390731Sjhay	return (ENXIO);
57490731Sjhay}
57590731Sjhay
576102714Sphkint
57790731Sjhaypuc_teardown_intr(device_t dev, device_t child, struct resource *r,
57890731Sjhay		  void *cookie)
57990731Sjhay{
58090731Sjhay	int i;
58190731Sjhay	struct puc_softc *sc;
58290731Sjhay
58390731Sjhay	sc = (struct puc_softc *)device_get_softc(dev);
58490731Sjhay	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
58590731Sjhay		if (sc->sc_ports[i].dev == child) {
58690731Sjhay			sc->sc_ports[i].ihand = NULL;
58790731Sjhay			sc->sc_ports[i].ihandarg = NULL;
58890731Sjhay			return (0);
58990731Sjhay		}
59090731Sjhay	}
59190731Sjhay	return (ENXIO);
59290731Sjhay}
59390731Sjhay
594102714Sphkint
59590731Sjhaypuc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
59690731Sjhay{
59790731Sjhay	struct puc_device *pdev;
59890731Sjhay
59990731Sjhay	pdev = device_get_ivars(child);
60090731Sjhay	if (pdev == NULL)
60190731Sjhay		return (ENOENT);
60290731Sjhay
60390731Sjhay	switch(index) {
60490731Sjhay	case PUC_IVAR_FREQ:
60590731Sjhay		*result = pdev->serialfreq;
60690731Sjhay		break;
607120451Smarcel	case PUC_IVAR_PORT:
608120451Smarcel		*result = pdev->port;
609119814Smarcel		break;
610119814Smarcel	case PUC_IVAR_REGSHFT:
611119814Smarcel		*result = pdev->regshft;
612119814Smarcel		break;
613120451Smarcel	case PUC_IVAR_SUBTYPE:
614120451Smarcel		*result = pdev->subtype;
615120451Smarcel		break;
61690731Sjhay	default:
61790731Sjhay		return (ENOENT);
61890731Sjhay	}
61990731Sjhay	return (0);
62090731Sjhay}
621