190731Sjhay/*-
2158124Smarcel * Copyright (c) 2006 Marcel Moolenaar
3158124Smarcel * All rights reserved.
490731Sjhay *
590731Sjhay * Redistribution and use in source and binary forms, with or without
690731Sjhay * modification, are permitted provided that the following conditions
790731Sjhay * are met:
890731Sjhay *
990731Sjhay * 1. Redistributions of source code must retain the above copyright
1090731Sjhay *    notice, this list of conditions and the following disclaimer.
1190731Sjhay * 2. Redistributions in binary form must reproduce the above copyright
1290731Sjhay *    notice, this list of conditions and the following disclaimer in the
1390731Sjhay *    documentation and/or other materials provided with the distribution.
1490731Sjhay *
1590731Sjhay * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1690731Sjhay * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1790731Sjhay * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1890731Sjhay * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1990731Sjhay * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2090731Sjhay * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2190731Sjhay * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2290731Sjhay * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2390731Sjhay * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2490731Sjhay * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2590731Sjhay */
2690731Sjhay
2790731Sjhay#include <sys/cdefs.h>
2890731Sjhay__FBSDID("$FreeBSD$");
2990731Sjhay
3090731Sjhay#include <sys/param.h>
3190731Sjhay#include <sys/systm.h>
3290731Sjhay#include <sys/kernel.h>
3390731Sjhay#include <sys/bus.h>
3490731Sjhay#include <sys/conf.h>
3590731Sjhay#include <sys/malloc.h>
36158124Smarcel#include <sys/mutex.h>
3790731Sjhay
3890731Sjhay#include <machine/bus.h>
3990731Sjhay#include <machine/resource.h>
4090731Sjhay#include <sys/rman.h>
4190731Sjhay
4290731Sjhay#include <dev/pci/pcireg.h>
4390731Sjhay#include <dev/pci/pcivar.h>
44102714Sphk
45158124Smarcel#include <dev/puc/puc_bus.h>
46158124Smarcel#include <dev/puc/puc_cfg.h>
47160030Sobrien#include <dev/puc/puc_bfe.h>
4890731Sjhay
49158124Smarcel#define	PUC_ISRCCNT	5
5090731Sjhay
51158124Smarcelstruct puc_port {
52158124Smarcel	struct puc_bar	*p_bar;
53158124Smarcel	struct resource *p_rres;
54158124Smarcel	struct resource *p_ires;
55158124Smarcel	device_t	p_dev;
56158124Smarcel	int		p_nr;
57158124Smarcel	int		p_type;
58158124Smarcel	int		p_rclk;
5990731Sjhay
60158124Smarcel	int		p_hasintr:1;
6190731Sjhay
62158124Smarcel	serdev_intr_t	*p_ihsrc[PUC_ISRCCNT];
63158124Smarcel	void		*p_iharg;
64158124Smarcel
65158124Smarcel	int		p_ipend;
66158124Smarcel};
67158124Smarcel
68102893Sphkdevclass_t puc_devclass;
69158124Smarcelconst char puc_driver_name[] = "puc";
70102893Sphk
71249132Smavstatic MALLOC_DEFINE(M_PUC, "PUC", "PUC driver");
72158124Smarcel
73158124Smarcelstruct puc_bar *
74158124Smarcelpuc_get_bar(struct puc_softc *sc, int rid)
75102734Sphk{
76158124Smarcel	struct puc_bar *bar;
77158124Smarcel	struct rman *rm;
78158124Smarcel	u_long end, start;
79158124Smarcel	int error, i;
80102734Sphk
81158124Smarcel	/* Find the BAR entry with the given RID. */
82158124Smarcel	i = 0;
83158124Smarcel	while (i < PUC_PCI_BARS && sc->sc_bar[i].b_rid != rid)
84158124Smarcel		i++;
85158124Smarcel	if (i < PUC_PCI_BARS)
86158124Smarcel		return (&sc->sc_bar[i]);
87158124Smarcel
88158124Smarcel	/* Not found. If we're looking for an unused entry, return NULL. */
89158124Smarcel	if (rid == -1)
90158124Smarcel		return (NULL);
91158124Smarcel
92158124Smarcel	/* Get an unused entry for us to fill.  */
93158124Smarcel	bar = puc_get_bar(sc, -1);
94158124Smarcel	if (bar == NULL)
95158124Smarcel		return (NULL);
96158124Smarcel	bar->b_rid = rid;
97158124Smarcel	bar->b_type = SYS_RES_IOPORT;
98158124Smarcel	bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
99158124Smarcel	    &bar->b_rid, RF_ACTIVE);
100158124Smarcel	if (bar->b_res == NULL) {
101158124Smarcel		bar->b_rid = rid;
102158124Smarcel		bar->b_type = SYS_RES_MEMORY;
103158124Smarcel		bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
104158124Smarcel		    &bar->b_rid, RF_ACTIVE);
105158124Smarcel		if (bar->b_res == NULL) {
106158124Smarcel			bar->b_rid = -1;
107158124Smarcel			return (NULL);
108158124Smarcel		}
109102734Sphk	}
110158124Smarcel
111158124Smarcel	/* Update our managed space. */
112158124Smarcel	rm = (bar->b_type == SYS_RES_IOPORT) ? &sc->sc_ioport : &sc->sc_iomem;
113158124Smarcel	start = rman_get_start(bar->b_res);
114158124Smarcel	end = rman_get_end(bar->b_res);
115158124Smarcel	error = rman_manage_region(rm, start, end);
116158124Smarcel	if (error) {
117158124Smarcel		bus_release_resource(sc->sc_dev, bar->b_type, bar->b_rid,
118158124Smarcel		    bar->b_res);
119158124Smarcel		bar->b_res = NULL;
120158124Smarcel		bar->b_rid = -1;
121158124Smarcel		bar = NULL;
122142502Ssam	}
123158124Smarcel
124158124Smarcel	return (bar);
125102734Sphk}
126102734Sphk
127166901Spisostatic int
128158124Smarcelpuc_intr(void *arg)
129112270Ssobomax{
130158124Smarcel	struct puc_port *port;
131158124Smarcel	struct puc_softc *sc = arg;
132200397Smarcel	u_long ds, dev, devs;
133200397Smarcel	int i, idx, ipend, isrc, nints;
134158124Smarcel	uint8_t ilr;
135112270Ssobomax
136200397Smarcel	nints = 0;
137200397Smarcel	while (1) {
138200397Smarcel		/*
139200397Smarcel		 * Obtain the set of devices with pending interrupts.
140200397Smarcel		 */
141200397Smarcel		devs = sc->sc_serdevs;
142200397Smarcel		if (sc->sc_ilr == PUC_ILR_DIGI) {
143200397Smarcel			idx = 0;
144200397Smarcel			while (devs & (0xfful << idx)) {
145200397Smarcel				ilr = ~bus_read_1(sc->sc_port[idx].p_rres, 7);
146200397Smarcel				devs &= ~0ul ^ ((u_long)ilr << idx);
147200397Smarcel				idx += 8;
148200397Smarcel			}
149200397Smarcel		} else if (sc->sc_ilr == PUC_ILR_QUATECH) {
150200397Smarcel			/*
151200397Smarcel			 * Don't trust the value if it's the same as the option
152200397Smarcel			 * register. It may mean that the ILR is not active and
153200397Smarcel			 * we're reading the option register instead. This may
154200397Smarcel			 * lead to false positives on 8-port boards.
155200397Smarcel			 */
156200397Smarcel			ilr = bus_read_1(sc->sc_port[0].p_rres, 7);
157200397Smarcel			if (ilr != (sc->sc_cfg_data & 0xff))
158200397Smarcel				devs &= (u_long)ilr;
159112270Ssobomax		}
160200397Smarcel		if (devs == 0UL)
161200397Smarcel			break;
162200397Smarcel
163158124Smarcel		/*
164200397Smarcel		 * Obtain the set of interrupt sources from those devices
165200397Smarcel		 * that have pending interrupts.
166158124Smarcel		 */
167200397Smarcel		ipend = 0;
168158124Smarcel		idx = 0, dev = 1UL;
169200397Smarcel		ds = devs;
170200397Smarcel		while (ds != 0UL) {
171200397Smarcel			while ((ds & dev) == 0UL)
172158124Smarcel				idx++, dev <<= 1;
173200397Smarcel			ds &= ~dev;
174158124Smarcel			port = &sc->sc_port[idx];
175200397Smarcel			port->p_ipend = SERDEV_IPEND(port->p_dev);
176200397Smarcel			ipend |= port->p_ipend;
177158124Smarcel		}
178200397Smarcel		if (ipend == 0)
179200397Smarcel			break;
180200397Smarcel
181200397Smarcel		i = 0, isrc = SER_INT_OVERRUN;
182200397Smarcel		while (ipend) {
183200397Smarcel			while (i < PUC_ISRCCNT && !(ipend & isrc))
184200397Smarcel				i++, isrc <<= 1;
185200397Smarcel			KASSERT(i < PUC_ISRCCNT, ("%s", __func__));
186200397Smarcel			ipend &= ~isrc;
187200397Smarcel			idx = 0, dev = 1UL;
188200397Smarcel			ds = devs;
189200397Smarcel			while (ds != 0UL) {
190200397Smarcel				while ((ds & dev) == 0UL)
191200397Smarcel					idx++, dev <<= 1;
192200397Smarcel				ds &= ~dev;
193200397Smarcel				port = &sc->sc_port[idx];
194200397Smarcel				if (!(port->p_ipend & isrc))
195200397Smarcel					continue;
196200397Smarcel				if (port->p_ihsrc[i] != NULL)
197200397Smarcel					(*port->p_ihsrc[i])(port->p_iharg);
198200397Smarcel				nints++;
199200397Smarcel			}
200200397Smarcel		}
201158124Smarcel	}
202200397Smarcel
203200397Smarcel	return ((nints > 0) ? FILTER_HANDLED : FILTER_STRAY);
204112270Ssobomax}
205112270Ssobomax
206102714Sphkint
207158124Smarcelpuc_bfe_attach(device_t dev)
20890731Sjhay{
209158124Smarcel	char buffer[64];
210158124Smarcel	struct puc_bar *bar;
211158124Smarcel	struct puc_port *port;
21290731Sjhay	struct puc_softc *sc;
213158124Smarcel	struct rman *rm;
214158124Smarcel	intptr_t res;
215158124Smarcel	bus_addr_t ofs, start;
216158124Smarcel	bus_size_t size;
217158124Smarcel	bus_space_handle_t bsh;
218158124Smarcel	bus_space_tag_t bst;
219158124Smarcel	int error, idx;
22090731Sjhay
221158124Smarcel	sc = device_get_softc(dev);
222119814Smarcel
223158124Smarcel	for (idx = 0; idx < PUC_PCI_BARS; idx++)
224158124Smarcel		sc->sc_bar[idx].b_rid = -1;
22590731Sjhay
226158124Smarcel	do {
227158124Smarcel		sc->sc_ioport.rm_type = RMAN_ARRAY;
228158124Smarcel		error = rman_init(&sc->sc_ioport);
229158124Smarcel		if (!error) {
230158124Smarcel			sc->sc_iomem.rm_type = RMAN_ARRAY;
231158124Smarcel			error = rman_init(&sc->sc_iomem);
232158124Smarcel			if (!error) {
233158124Smarcel				sc->sc_irq.rm_type = RMAN_ARRAY;
234158124Smarcel				error = rman_init(&sc->sc_irq);
235158124Smarcel				if (!error)
236158124Smarcel					break;
237158124Smarcel				rman_fini(&sc->sc_iomem);
238158124Smarcel			}
239158124Smarcel			rman_fini(&sc->sc_ioport);
240158124Smarcel		}
241158124Smarcel		return (error);
242158124Smarcel	} while (0);
24390731Sjhay
244158124Smarcel	snprintf(buffer, sizeof(buffer), "%s I/O port mapping",
245158124Smarcel	    device_get_nameunit(dev));
246158124Smarcel	sc->sc_ioport.rm_descr = strdup(buffer, M_PUC);
247158124Smarcel	snprintf(buffer, sizeof(buffer), "%s I/O memory mapping",
248158124Smarcel	    device_get_nameunit(dev));
249158124Smarcel	sc->sc_iomem.rm_descr = strdup(buffer, M_PUC);
250158124Smarcel	snprintf(buffer, sizeof(buffer), "%s port numbers",
251158124Smarcel	    device_get_nameunit(dev));
252158124Smarcel	sc->sc_irq.rm_descr = strdup(buffer, M_PUC);
25390731Sjhay
254158124Smarcel	error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
255158124Smarcel	KASSERT(error == 0, ("%s %d", __func__, __LINE__));
256158124Smarcel	sc->sc_nports = (int)res;
257158124Smarcel	sc->sc_port = malloc(sc->sc_nports * sizeof(struct puc_port),
258158124Smarcel	    M_PUC, M_WAITOK|M_ZERO);
25990731Sjhay
260158124Smarcel	error = rman_manage_region(&sc->sc_irq, 1, sc->sc_nports);
261158124Smarcel	if (error)
262158124Smarcel		goto fail;
26390731Sjhay
264158124Smarcel	error = puc_config(sc, PUC_CFG_SETUP, 0, &res);
265158124Smarcel	if (error)
266158124Smarcel		goto fail;
267109458Smarcel
268158124Smarcel	for (idx = 0; idx < sc->sc_nports; idx++) {
269158124Smarcel		port = &sc->sc_port[idx];
270158124Smarcel		port->p_nr = idx + 1;
271158124Smarcel		error = puc_config(sc, PUC_CFG_GET_TYPE, idx, &res);
272158124Smarcel		if (error)
273158124Smarcel			goto fail;
274158124Smarcel		port->p_type = res;
275158124Smarcel		error = puc_config(sc, PUC_CFG_GET_RID, idx, &res);
276158124Smarcel		if (error)
277158124Smarcel			goto fail;
278158124Smarcel		bar = puc_get_bar(sc, res);
279158124Smarcel		if (bar == NULL) {
280158124Smarcel			error = ENXIO;
281158124Smarcel			goto fail;
282119814Smarcel		}
283158124Smarcel		port->p_bar = bar;
284158124Smarcel		start = rman_get_start(bar->b_res);
285158124Smarcel		error = puc_config(sc, PUC_CFG_GET_OFS, idx, &res);
286158124Smarcel		if (error)
287158124Smarcel			goto fail;
288158124Smarcel		ofs = res;
289158124Smarcel		error = puc_config(sc, PUC_CFG_GET_LEN, idx, &res);
290158124Smarcel		if (error)
291158124Smarcel			goto fail;
292158124Smarcel		size = res;
293158124Smarcel		rm = (bar->b_type == SYS_RES_IOPORT)
294158124Smarcel		    ? &sc->sc_ioport: &sc->sc_iomem;
295158124Smarcel		port->p_rres = rman_reserve_resource(rm, start + ofs,
296158124Smarcel		    start + ofs + size - 1, size, 0, NULL);
297158124Smarcel		if (port->p_rres != NULL) {
298158124Smarcel			bsh = rman_get_bushandle(bar->b_res);
299158124Smarcel			bst = rman_get_bustag(bar->b_res);
300158124Smarcel			bus_space_subregion(bst, bsh, ofs, size, &bsh);
301158124Smarcel			rman_set_bushandle(port->p_rres, bsh);
302158124Smarcel			rman_set_bustag(port->p_rres, bst);
30390731Sjhay		}
304158124Smarcel		port->p_ires = rman_reserve_resource(&sc->sc_irq, port->p_nr,
305158124Smarcel		    port->p_nr, 1, 0, NULL);
306158124Smarcel		if (port->p_ires == NULL) {
307158124Smarcel			error = ENXIO;
308158124Smarcel			goto fail;
309112270Ssobomax		}
310158124Smarcel		error = puc_config(sc, PUC_CFG_GET_CLOCK, idx, &res);
311158124Smarcel		if (error)
312158124Smarcel			goto fail;
313158124Smarcel		port->p_rclk = res;
31490731Sjhay
315158124Smarcel		port->p_dev = device_add_child(dev, NULL, -1);
316158124Smarcel		if (port->p_dev != NULL)
317158124Smarcel			device_set_ivars(port->p_dev, (void *)port);
318102734Sphk	}
31990731Sjhay
320158124Smarcel	error = puc_config(sc, PUC_CFG_GET_ILR, 0, &res);
321158124Smarcel	if (error)
322158124Smarcel		goto fail;
323158124Smarcel	sc->sc_ilr = res;
324158124Smarcel	if (bootverbose && sc->sc_ilr != 0)
325158124Smarcel		device_printf(dev, "using interrupt latch register\n");
32690731Sjhay
327158124Smarcel	sc->sc_irid = 0;
328158124Smarcel	sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
329158124Smarcel	    RF_ACTIVE|RF_SHAREABLE);
330158124Smarcel	if (sc->sc_ires != NULL) {
331158124Smarcel		error = bus_setup_intr(dev, sc->sc_ires,
332166901Spiso		    INTR_TYPE_TTY, puc_intr, NULL, sc, &sc->sc_icookie);
333158124Smarcel		if (error)
334158124Smarcel			error = bus_setup_intr(dev, sc->sc_ires,
335166901Spiso			    INTR_TYPE_TTY | INTR_MPSAFE, NULL,
336166901Spiso			    (driver_intr_t *)puc_intr, sc, &sc->sc_icookie);
337158124Smarcel		else
338158124Smarcel			sc->sc_fastintr = 1;
33990731Sjhay
340158124Smarcel		if (error) {
341158124Smarcel			device_printf(dev, "could not activate interrupt\n");
342158124Smarcel			bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
343158124Smarcel			    sc->sc_ires);
344158124Smarcel			sc->sc_ires = NULL;
34590731Sjhay		}
346158124Smarcel	}
347158124Smarcel	if (sc->sc_ires == NULL) {
348158124Smarcel		/* XXX no interrupt resource. Force polled mode. */
349158124Smarcel		sc->sc_polled = 1;
350158124Smarcel	}
35190731Sjhay
352158124Smarcel	/* Probe and attach our children. */
353158124Smarcel	for (idx = 0; idx < sc->sc_nports; idx++) {
354158124Smarcel		port = &sc->sc_port[idx];
355158124Smarcel		if (port->p_dev == NULL)
35690731Sjhay			continue;
357158124Smarcel		error = device_probe_and_attach(port->p_dev);
358158124Smarcel		if (error) {
359158124Smarcel			device_delete_child(dev, port->p_dev);
360158124Smarcel			port->p_dev = NULL;
36190925Snyan		}
36290731Sjhay	}
36390731Sjhay
364158124Smarcel	/*
365158124Smarcel	 * If there are no serdev devices, then our interrupt handler
366158124Smarcel	 * will do nothing. Tear it down.
367158124Smarcel	 */
368158124Smarcel	if (sc->sc_serdevs == 0UL)
369158124Smarcel		bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
370158124Smarcel
37190731Sjhay	return (0);
372158124Smarcel
373158124Smarcelfail:
374158124Smarcel	for (idx = 0; idx < sc->sc_nports; idx++) {
375158124Smarcel		port = &sc->sc_port[idx];
376158124Smarcel		if (port->p_dev != NULL)
377158124Smarcel			device_delete_child(dev, port->p_dev);
378158124Smarcel		if (port->p_rres != NULL)
379158124Smarcel			rman_release_resource(port->p_rres);
380158124Smarcel		if (port->p_ires != NULL)
381158124Smarcel			rman_release_resource(port->p_ires);
382158124Smarcel	}
383158124Smarcel	for (idx = 0; idx < PUC_PCI_BARS; idx++) {
384158124Smarcel		bar = &sc->sc_bar[idx];
385158124Smarcel		if (bar->b_res != NULL)
386158124Smarcel			bus_release_resource(sc->sc_dev, bar->b_type,
387158124Smarcel			    bar->b_rid, bar->b_res);
388158124Smarcel	}
389158124Smarcel	rman_fini(&sc->sc_irq);
390158124Smarcel	free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
391158124Smarcel	rman_fini(&sc->sc_iomem);
392158124Smarcel	free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
393158124Smarcel	rman_fini(&sc->sc_ioport);
394158124Smarcel	free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
395158124Smarcel	free(sc->sc_port, M_PUC);
396158124Smarcel	return (error);
39790731Sjhay}
39890731Sjhay
399158124Smarcelint
400158124Smarcelpuc_bfe_detach(device_t dev)
401112270Ssobomax{
402158124Smarcel	struct puc_bar *bar;
403158124Smarcel	struct puc_port *port;
404158124Smarcel	struct puc_softc *sc;
405158124Smarcel	int error, idx;
406112270Ssobomax
407158124Smarcel	sc = device_get_softc(dev);
408112270Ssobomax
409158124Smarcel	/* Detach our children. */
410158124Smarcel	error = 0;
411158124Smarcel	for (idx = 0; idx < sc->sc_nports; idx++) {
412158124Smarcel		port = &sc->sc_port[idx];
413158124Smarcel		if (port->p_dev == NULL)
414158124Smarcel			continue;
415158124Smarcel		if (device_detach(port->p_dev) == 0) {
416158124Smarcel			device_delete_child(dev, port->p_dev);
417158124Smarcel			if (port->p_rres != NULL)
418158124Smarcel				rman_release_resource(port->p_rres);
419158124Smarcel			if (port->p_ires != NULL)
420158124Smarcel				rman_release_resource(port->p_ires);
421158124Smarcel		} else
422158124Smarcel			error = ENXIO;
423112270Ssobomax	}
424158124Smarcel	if (error)
425158124Smarcel		return (error);
426112270Ssobomax
427158124Smarcel	if (sc->sc_serdevs != 0UL)
428158124Smarcel		bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
429158124Smarcel	bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, sc->sc_ires);
43090731Sjhay
431158124Smarcel	for (idx = 0; idx < PUC_PCI_BARS; idx++) {
432158124Smarcel		bar = &sc->sc_bar[idx];
433158124Smarcel		if (bar->b_res != NULL)
434158124Smarcel			bus_release_resource(sc->sc_dev, bar->b_type,
435158124Smarcel			    bar->b_rid, bar->b_res);
436158124Smarcel	}
437158124Smarcel
438158124Smarcel	rman_fini(&sc->sc_irq);
439158124Smarcel	free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
440158124Smarcel	rman_fini(&sc->sc_iomem);
441158124Smarcel	free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
442158124Smarcel	rman_fini(&sc->sc_ioport);
443158124Smarcel	free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
444158124Smarcel	free(sc->sc_port, M_PUC);
445158124Smarcel	return (0);
44690731Sjhay}
44790731Sjhay
448158124Smarcelint
449158124Smarcelpuc_bfe_probe(device_t dev, const struct puc_cfg *cfg)
45090731Sjhay{
451158124Smarcel	struct puc_softc *sc;
452158124Smarcel	intptr_t res;
453158124Smarcel	int error;
45490731Sjhay
455158124Smarcel	sc = device_get_softc(dev);
456158124Smarcel	sc->sc_dev = dev;
457158124Smarcel	sc->sc_cfg = cfg;
45890731Sjhay
459158124Smarcel	/* We don't attach to single-port serial cards. */
460158124Smarcel	if (cfg->ports == PUC_PORT_1S || cfg->ports == PUC_PORT_1P)
461158124Smarcel		return (EDOOFUS);
462158124Smarcel	error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
463158124Smarcel	if (error)
464158124Smarcel		return (error);
465158124Smarcel	error = puc_config(sc, PUC_CFG_GET_DESC, 0, &res);
466158124Smarcel	if (error)
467158124Smarcel		return (error);
468158124Smarcel	if (res != 0)
469158124Smarcel		device_set_desc(dev, (const char *)res);
470158124Smarcel	return (BUS_PROBE_DEFAULT);
47190731Sjhay}
47290731Sjhay
473102714Sphkstruct resource *
474158124Smarcelpuc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
47590731Sjhay    u_long start, u_long end, u_long count, u_int flags)
47690731Sjhay{
477158124Smarcel	struct puc_port *port;
478158124Smarcel	struct resource *res;
479158124Smarcel	device_t assigned, originator;
480158124Smarcel	int error;
48190731Sjhay
482158124Smarcel	/* Get our immediate child. */
483158124Smarcel	originator = child;
484158124Smarcel	while (child != NULL && device_get_parent(child) != dev)
485158124Smarcel		child = device_get_parent(child);
486158124Smarcel	if (child == NULL)
487158124Smarcel		return (NULL);
488118292Sambrisko
489158124Smarcel	port = device_get_ivars(child);
490158124Smarcel	KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
49190731Sjhay
492158124Smarcel	if (rid == NULL || *rid != 0)
493158124Smarcel		return (NULL);
494158124Smarcel
495158124Smarcel	/* We only support default allocations. */
496158124Smarcel	if (start != 0UL || end != ~0UL)
497158124Smarcel		return (NULL);
498158124Smarcel
499158124Smarcel	if (type == port->p_bar->b_type)
500158124Smarcel		res = port->p_rres;
501158124Smarcel	else if (type == SYS_RES_IRQ)
502158124Smarcel		res = port->p_ires;
503118292Sambrisko	else
504158124Smarcel		return (NULL);
50590731Sjhay
506158124Smarcel	if (res == NULL)
507158124Smarcel		return (NULL);
508158124Smarcel
509158124Smarcel	assigned = rman_get_device(res);
510158124Smarcel	if (assigned == NULL)	/* Not allocated */
511158124Smarcel		rman_set_device(res, originator);
512158124Smarcel	else if (assigned != originator)
513158124Smarcel		return (NULL);
514158124Smarcel
515158124Smarcel	if (flags & RF_ACTIVE) {
516158124Smarcel		error = rman_activate_resource(res);
517158124Smarcel		if (error) {
518158124Smarcel			if (assigned == NULL)
519158124Smarcel				rman_set_device(res, NULL);
520158124Smarcel			return (NULL);
521158124Smarcel		}
522158124Smarcel	}
523158124Smarcel
524158124Smarcel	return (res);
52590731Sjhay}
52690731Sjhay
527102714Sphkint
528158124Smarcelpuc_bus_release_resource(device_t dev, device_t child, int type, int rid,
52990731Sjhay    struct resource *res)
53090731Sjhay{
531158124Smarcel	struct puc_port *port;
532158124Smarcel	device_t originator;
533158124Smarcel
534158124Smarcel	/* Get our immediate child. */
535158124Smarcel	originator = child;
536158124Smarcel	while (child != NULL && device_get_parent(child) != dev)
537158124Smarcel		child = device_get_parent(child);
538158124Smarcel	if (child == NULL)
539158124Smarcel		return (EINVAL);
540158124Smarcel
541158124Smarcel	port = device_get_ivars(child);
542158124Smarcel	KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
543158124Smarcel
544158124Smarcel	if (rid != 0 || res == NULL)
545158124Smarcel		return (EINVAL);
546158124Smarcel
547158124Smarcel	if (type == port->p_bar->b_type) {
548158124Smarcel		if (res != port->p_rres)
549158124Smarcel			return (EINVAL);
550158124Smarcel	} else if (type == SYS_RES_IRQ) {
551158124Smarcel		if (res != port->p_ires)
552158124Smarcel			return (EINVAL);
553158124Smarcel		if (port->p_hasintr)
554158124Smarcel			return (EBUSY);
555158124Smarcel	} else
556158124Smarcel		return (EINVAL);
557158124Smarcel
558158124Smarcel	if (rman_get_device(res) != originator)
559158124Smarcel		return (ENXIO);
560158124Smarcel	if (rman_get_flags(res) & RF_ACTIVE)
561158124Smarcel		rman_deactivate_resource(res);
562158124Smarcel	rman_set_device(res, NULL);
56390731Sjhay	return (0);
56490731Sjhay}
56590731Sjhay
566102714Sphkint
567158124Smarcelpuc_bus_get_resource(device_t dev, device_t child, int type, int rid,
56890731Sjhay    u_long *startp, u_long *countp)
56990731Sjhay{
570158124Smarcel	struct puc_port *port;
571158124Smarcel	struct resource *res;
572158124Smarcel	u_long start;
57390731Sjhay
574158124Smarcel	/* Get our immediate child. */
575158124Smarcel	while (child != NULL && device_get_parent(child) != dev)
576158124Smarcel		child = device_get_parent(child);
577158124Smarcel	if (child == NULL)
578158124Smarcel		return (EINVAL);
57990731Sjhay
580158124Smarcel	port = device_get_ivars(child);
581158124Smarcel	KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
582158124Smarcel
583158124Smarcel	if (type == port->p_bar->b_type)
584158124Smarcel		res = port->p_rres;
585158124Smarcel	else if (type == SYS_RES_IRQ)
586158124Smarcel		res = port->p_ires;
587158124Smarcel	else
588158124Smarcel		return (ENXIO);
589158124Smarcel
590158124Smarcel	if (rid != 0 || res == NULL)
591158124Smarcel		return (ENXIO);
592158124Smarcel
593158124Smarcel	start = rman_get_start(res);
594158124Smarcel	if (startp != NULL)
595158124Smarcel		*startp = start;
596158124Smarcel	if (countp != NULL)
597158124Smarcel		*countp = rman_get_end(res) - start + 1;
598158124Smarcel	return (0);
59990731Sjhay}
60090731Sjhay
601102714Sphkint
602158124Smarcelpuc_bus_setup_intr(device_t dev, device_t child, struct resource *res,
603166901Spiso    int flags, driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
60490731Sjhay{
605158124Smarcel	struct puc_port *port;
60690731Sjhay	struct puc_softc *sc;
607158124Smarcel	device_t originator;
608158124Smarcel	int i, isrc, serdev;
60990731Sjhay
610158124Smarcel	sc = device_get_softc(dev);
611158124Smarcel
612158124Smarcel	/* Get our immediate child. */
613158124Smarcel	originator = child;
614158124Smarcel	while (child != NULL && device_get_parent(child) != dev)
615158124Smarcel		child = device_get_parent(child);
616158124Smarcel	if (child == NULL)
617158124Smarcel		return (EINVAL);
618158124Smarcel
619158124Smarcel	port = device_get_ivars(child);
620158124Smarcel	KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
621158124Smarcel
622170386Spiso	if (cookiep == NULL || res != port->p_ires)
623158124Smarcel		return (EINVAL);
624170386Spiso	/* We demand that serdev devices use filter_only interrupts. */
625247489Sjhb	if (port->p_type == PUC_TYPE_SERIAL && ihand != NULL)
626170386Spiso		return (ENXIO);
627158124Smarcel	if (rman_get_device(port->p_ires) != originator)
628102895Sphk		return (ENXIO);
629158124Smarcel
630158124Smarcel	/*
631158124Smarcel	 * Have non-serdev ports handled by the bus implementation. It
632158124Smarcel	 * supports multiple handlers for a single interrupt as it is,
633158124Smarcel	 * so we wouldn't add value if we did it ourselves.
634158124Smarcel	 */
635158124Smarcel	serdev = 0;
636158124Smarcel	if (port->p_type == PUC_TYPE_SERIAL) {
637158124Smarcel		i = 0, isrc = SER_INT_OVERRUN;
638158124Smarcel		while (i < PUC_ISRCCNT) {
639158124Smarcel			port->p_ihsrc[i] = SERDEV_IHAND(originator, isrc);
640158124Smarcel			if (port->p_ihsrc[i] != NULL)
641158124Smarcel				serdev = 1;
642158124Smarcel			i++, isrc <<= 1;
64390731Sjhay		}
64490731Sjhay	}
645158124Smarcel	if (!serdev)
646158124Smarcel		return (BUS_SETUP_INTR(device_get_parent(dev), originator,
647166901Spiso		    sc->sc_ires, flags, filt, ihand, arg, cookiep));
648158124Smarcel
649158124Smarcel	sc->sc_serdevs |= 1UL << (port->p_nr - 1);
650158124Smarcel
651158124Smarcel	port->p_hasintr = 1;
652158124Smarcel	port->p_iharg = arg;
653158124Smarcel
654158124Smarcel	*cookiep = port;
655158124Smarcel	return (0);
65690731Sjhay}
65790731Sjhay
658102714Sphkint
659158124Smarcelpuc_bus_teardown_intr(device_t dev, device_t child, struct resource *res,
660158124Smarcel    void *cookie)
66190731Sjhay{
662158124Smarcel	struct puc_port *port;
663158124Smarcel	struct puc_softc *sc;
664158124Smarcel	device_t originator;
66590731Sjhay	int i;
66690731Sjhay
667158124Smarcel	sc = device_get_softc(dev);
668158124Smarcel
669158124Smarcel	/* Get our immediate child. */
670158124Smarcel	originator = child;
671158124Smarcel	while (child != NULL && device_get_parent(child) != dev)
672158124Smarcel		child = device_get_parent(child);
673158124Smarcel	if (child == NULL)
674158124Smarcel		return (EINVAL);
675158124Smarcel
676158124Smarcel	port = device_get_ivars(child);
677158124Smarcel	KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
678158124Smarcel
679158124Smarcel	if (res != port->p_ires)
680158124Smarcel		return (EINVAL);
681158124Smarcel	if (rman_get_device(port->p_ires) != originator)
682158124Smarcel		return (ENXIO);
683158124Smarcel
684158124Smarcel	if (!port->p_hasintr)
685158124Smarcel		return (BUS_TEARDOWN_INTR(device_get_parent(dev), originator,
686158124Smarcel		    sc->sc_ires, cookie));
687158124Smarcel
688158124Smarcel	if (cookie != port)
689158124Smarcel		return (EINVAL);
690158124Smarcel
691158124Smarcel	port->p_hasintr = 0;
692158124Smarcel	port->p_iharg = NULL;
693158124Smarcel
694158124Smarcel	for (i = 0; i < PUC_ISRCCNT; i++)
695158124Smarcel		port->p_ihsrc[i] = NULL;
696158124Smarcel
697158124Smarcel	return (0);
69890731Sjhay}
69990731Sjhay
700102714Sphkint
701158124Smarcelpuc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
70290731Sjhay{
703158124Smarcel	struct puc_port *port;
70490731Sjhay
705158124Smarcel	/* Get our immediate child. */
706158124Smarcel	while (child != NULL && device_get_parent(child) != dev)
707158124Smarcel		child = device_get_parent(child);
708158124Smarcel	if (child == NULL)
709158124Smarcel		return (EINVAL);
71090731Sjhay
711158124Smarcel	port = device_get_ivars(child);
712158124Smarcel	KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
713158124Smarcel
714158124Smarcel	if (result == NULL)
715158124Smarcel		return (EINVAL);
716158124Smarcel
71790731Sjhay	switch(index) {
718158124Smarcel	case PUC_IVAR_CLOCK:
719158124Smarcel		*result = port->p_rclk;
72090731Sjhay		break;
721158124Smarcel	case PUC_IVAR_TYPE:
722158124Smarcel		*result = port->p_type;
723119814Smarcel		break;
72490731Sjhay	default:
72590731Sjhay		return (ENOENT);
72690731Sjhay	}
72790731Sjhay	return (0);
72890731Sjhay}
729223091Sjhb
730223091Sjhbint
731223091Sjhbpuc_bus_print_child(device_t dev, device_t child)
732223091Sjhb{
733223091Sjhb	struct puc_port *port;
734223091Sjhb	int retval;
735223091Sjhb
736223091Sjhb	port = device_get_ivars(child);
737223091Sjhb	retval = 0;
738223091Sjhb
739223091Sjhb	retval += bus_print_child_header(dev, child);
740223091Sjhb	retval += printf(" at port %d", port->p_nr);
741223091Sjhb	retval += bus_print_child_footer(dev, child);
742223091Sjhb
743223091Sjhb	return (retval);
744223091Sjhb}
745223091Sjhb
746223091Sjhbint
747223091Sjhbpuc_bus_child_location_str(device_t dev, device_t child, char *buf,
748223091Sjhb    size_t buflen)
749223091Sjhb{
750223091Sjhb	struct puc_port *port;
751223091Sjhb
752223091Sjhb	port = device_get_ivars(child);
753223091Sjhb	snprintf(buf, buflen, "port=%d", port->p_nr);
754223091Sjhb	return (0);
755223091Sjhb}
756223091Sjhb
757223091Sjhbint
758223091Sjhbpuc_bus_child_pnpinfo_str(device_t dev, device_t child, char *buf,
759223091Sjhb    size_t buflen)
760223091Sjhb{
761223091Sjhb	struct puc_port *port;
762223091Sjhb
763223091Sjhb	port = device_get_ivars(child);
764223091Sjhb	snprintf(buf, buflen, "type=%d", port->p_type);
765223091Sjhb	return (0);
766223091Sjhb}
767