xref: /freebsd/sys/dev/puc/puc.c (revision c57c26179033f64c2011a2d2a904ee3fa62e826a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/sbuf.h>
37 #include <sys/sysctl.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42 
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
45 
46 #include <dev/puc/puc_bus.h>
47 #include <dev/puc/puc_cfg.h>
48 #include <dev/puc/puc_bfe.h>
49 
50 #define	PUC_ISRCCNT	5
51 
52 struct puc_port {
53 	struct puc_bar	*p_bar;
54 	struct resource *p_rres;
55 	struct resource *p_ires;
56 	device_t	p_dev;
57 	int		p_nr;
58 	int		p_type;
59 	int		p_rclk;
60 
61 	bool		p_hasintr:1;
62 
63 	serdev_intr_t	*p_ihsrc[PUC_ISRCCNT];
64 	void		*p_iharg;
65 
66 	int		p_ipend;
67 };
68 
69 const char puc_driver_name[] = "puc";
70 
71 static MALLOC_DEFINE(M_PUC, "PUC", "PUC driver");
72 
73 SYSCTL_NODE(_hw, OID_AUTO, puc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
74     "puc(4) driver configuration");
75 
76 struct puc_bar *
77 puc_get_bar(struct puc_softc *sc, int rid)
78 {
79 	struct puc_bar *bar;
80 	struct rman *rm;
81 	rman_res_t end, start;
82 	int error, i;
83 
84 	/* Find the BAR entry with the given RID. */
85 	i = 0;
86 	while (i < PUC_PCI_BARS && sc->sc_bar[i].b_rid != rid)
87 		i++;
88 	if (i < PUC_PCI_BARS)
89 		return (&sc->sc_bar[i]);
90 
91 	/* Not found. If we're looking for an unused entry, return NULL. */
92 	if (rid == -1)
93 		return (NULL);
94 
95 	/* Get an unused entry for us to fill.  */
96 	bar = puc_get_bar(sc, -1);
97 	if (bar == NULL)
98 		return (NULL);
99 	bar->b_rid = rid;
100 	bar->b_type = SYS_RES_IOPORT;
101 	bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
102 	    &bar->b_rid, RF_ACTIVE);
103 	if (bar->b_res == NULL) {
104 		bar->b_rid = rid;
105 		bar->b_type = SYS_RES_MEMORY;
106 		bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
107 		    &bar->b_rid, RF_ACTIVE);
108 		if (bar->b_res == NULL) {
109 			bar->b_rid = -1;
110 			return (NULL);
111 		}
112 	}
113 
114 	/* Update our managed space. */
115 	rm = (bar->b_type == SYS_RES_IOPORT) ? &sc->sc_ioport : &sc->sc_iomem;
116 	start = rman_get_start(bar->b_res);
117 	end = rman_get_end(bar->b_res);
118 	error = rman_manage_region(rm, start, end);
119 	if (error) {
120 		bus_release_resource(sc->sc_dev, bar->b_type, bar->b_rid,
121 		    bar->b_res);
122 		bar->b_res = NULL;
123 		bar->b_rid = -1;
124 		bar = NULL;
125 	}
126 
127 	return (bar);
128 }
129 
130 static int
131 puc_intr(void *arg)
132 {
133 	struct puc_port *port;
134 	struct puc_softc *sc = arg;
135 	u_long ds, dev, devs;
136 	int i, idx, ipend, isrc, nints;
137 	uint8_t ilr;
138 
139 	nints = 0;
140 	while (1) {
141 		/*
142 		 * Obtain the set of devices with pending interrupts.
143 		 */
144 		devs = sc->sc_serdevs;
145 		if (sc->sc_ilr == PUC_ILR_DIGI) {
146 			idx = 0;
147 			while (devs & (0xfful << idx)) {
148 				ilr = ~bus_read_1(sc->sc_port[idx].p_rres, 7);
149 				devs &= ~0ul ^ ((u_long)ilr << idx);
150 				idx += 8;
151 			}
152 		} else if (sc->sc_ilr == PUC_ILR_QUATECH) {
153 			/*
154 			 * Don't trust the value if it's the same as the option
155 			 * register. It may mean that the ILR is not active and
156 			 * we're reading the option register instead. This may
157 			 * lead to false positives on 8-port boards.
158 			 */
159 			ilr = bus_read_1(sc->sc_port[0].p_rres, 7);
160 			if (ilr != (sc->sc_cfg_data & 0xff))
161 				devs &= (u_long)ilr;
162 		}
163 		if (devs == 0UL)
164 			break;
165 
166 		/*
167 		 * Obtain the set of interrupt sources from those devices
168 		 * that have pending interrupts.
169 		 */
170 		ipend = 0;
171 		idx = 0, dev = 1UL;
172 		ds = devs;
173 		while (ds != 0UL) {
174 			while ((ds & dev) == 0UL)
175 				idx++, dev <<= 1;
176 			ds &= ~dev;
177 			port = &sc->sc_port[idx];
178 			port->p_ipend = SERDEV_IPEND(port->p_dev);
179 			ipend |= port->p_ipend;
180 		}
181 		if (ipend == 0)
182 			break;
183 
184 		i = 0, isrc = SER_INT_OVERRUN;
185 		while (ipend) {
186 			while (i < PUC_ISRCCNT && !(ipend & isrc))
187 				i++, isrc <<= 1;
188 			KASSERT(i < PUC_ISRCCNT, ("%s", __func__));
189 			ipend &= ~isrc;
190 			idx = 0, dev = 1UL;
191 			ds = devs;
192 			while (ds != 0UL) {
193 				while ((ds & dev) == 0UL)
194 					idx++, dev <<= 1;
195 				ds &= ~dev;
196 				port = &sc->sc_port[idx];
197 				if (!(port->p_ipend & isrc))
198 					continue;
199 				if (port->p_ihsrc[i] != NULL)
200 					(*port->p_ihsrc[i])(port->p_iharg);
201 				nints++;
202 			}
203 		}
204 	}
205 
206 	return ((nints > 0) ? FILTER_HANDLED : FILTER_STRAY);
207 }
208 
209 int
210 puc_bfe_attach(device_t dev)
211 {
212 	char buffer[64];
213 	struct puc_bar *bar;
214 	struct puc_port *port;
215 	struct puc_softc *sc;
216 	struct rman *rm;
217 	intptr_t res;
218 	bus_addr_t ofs, start;
219 	bus_size_t size;
220 	bus_space_handle_t bsh;
221 	bus_space_tag_t bst;
222 	int error, idx;
223 
224 	sc = device_get_softc(dev);
225 
226 	for (idx = 0; idx < PUC_PCI_BARS; idx++)
227 		sc->sc_bar[idx].b_rid = -1;
228 
229 	do {
230 		sc->sc_ioport.rm_type = RMAN_ARRAY;
231 		error = rman_init(&sc->sc_ioport);
232 		if (!error) {
233 			sc->sc_iomem.rm_type = RMAN_ARRAY;
234 			error = rman_init(&sc->sc_iomem);
235 			if (!error) {
236 				sc->sc_irq.rm_type = RMAN_ARRAY;
237 				error = rman_init(&sc->sc_irq);
238 				if (!error)
239 					break;
240 				rman_fini(&sc->sc_iomem);
241 			}
242 			rman_fini(&sc->sc_ioport);
243 		}
244 		return (error);
245 	} while (0);
246 
247 	snprintf(buffer, sizeof(buffer), "%s I/O port mapping",
248 	    device_get_nameunit(dev));
249 	sc->sc_ioport.rm_descr = strdup(buffer, M_PUC);
250 	snprintf(buffer, sizeof(buffer), "%s I/O memory mapping",
251 	    device_get_nameunit(dev));
252 	sc->sc_iomem.rm_descr = strdup(buffer, M_PUC);
253 	snprintf(buffer, sizeof(buffer), "%s port numbers",
254 	    device_get_nameunit(dev));
255 	sc->sc_irq.rm_descr = strdup(buffer, M_PUC);
256 
257 	error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
258 	KASSERT(error == 0, ("%s %d", __func__, __LINE__));
259 	sc->sc_nports = (int)res;
260 	sc->sc_port = malloc(sc->sc_nports * sizeof(struct puc_port),
261 	    M_PUC, M_WAITOK|M_ZERO);
262 
263 	error = rman_manage_region(&sc->sc_irq, 1, sc->sc_nports);
264 	if (error)
265 		goto fail;
266 
267 	error = puc_config(sc, PUC_CFG_SETUP, 0, &res);
268 	if (error)
269 		goto fail;
270 
271 	for (idx = 0; idx < sc->sc_nports; idx++) {
272 		port = &sc->sc_port[idx];
273 		port->p_nr = idx + 1;
274 		error = puc_config(sc, PUC_CFG_GET_TYPE, idx, &res);
275 		if (error)
276 			goto fail;
277 		port->p_type = res;
278 		error = puc_config(sc, PUC_CFG_GET_RID, idx, &res);
279 		if (error)
280 			goto fail;
281 		bar = puc_get_bar(sc, res);
282 		if (bar == NULL) {
283 			error = ENXIO;
284 			goto fail;
285 		}
286 		port->p_bar = bar;
287 		start = rman_get_start(bar->b_res);
288 		error = puc_config(sc, PUC_CFG_GET_OFS, idx, &res);
289 		if (error)
290 			goto fail;
291 		ofs = res;
292 		error = puc_config(sc, PUC_CFG_GET_LEN, idx, &res);
293 		if (error)
294 			goto fail;
295 		size = res;
296 		rm = (bar->b_type == SYS_RES_IOPORT)
297 		    ? &sc->sc_ioport: &sc->sc_iomem;
298 		port->p_rres = rman_reserve_resource(rm, start + ofs,
299 		    start + ofs + size - 1, size, 0, NULL);
300 		if (port->p_rres != NULL) {
301 			bsh = rman_get_bushandle(bar->b_res);
302 			bst = rman_get_bustag(bar->b_res);
303 			bus_space_subregion(bst, bsh, ofs, size, &bsh);
304 			rman_set_bushandle(port->p_rres, bsh);
305 			rman_set_bustag(port->p_rres, bst);
306 		}
307 		port->p_ires = rman_reserve_resource(&sc->sc_irq, port->p_nr,
308 		    port->p_nr, 1, 0, NULL);
309 		if (port->p_ires == NULL) {
310 			error = ENXIO;
311 			goto fail;
312 		}
313 		error = puc_config(sc, PUC_CFG_GET_CLOCK, idx, &res);
314 		if (error)
315 			goto fail;
316 		port->p_rclk = res;
317 
318 		port->p_dev = device_add_child(dev, NULL, -1);
319 		if (port->p_dev != NULL)
320 			device_set_ivars(port->p_dev, (void *)port);
321 	}
322 
323 	error = puc_config(sc, PUC_CFG_GET_ILR, 0, &res);
324 	if (error)
325 		goto fail;
326 	sc->sc_ilr = res;
327 	if (bootverbose && sc->sc_ilr != 0)
328 		device_printf(dev, "using interrupt latch register\n");
329 
330 	sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
331 	    RF_ACTIVE|RF_SHAREABLE);
332 	if (sc->sc_ires != NULL) {
333 		error = bus_setup_intr(dev, sc->sc_ires,
334 		    INTR_TYPE_TTY, puc_intr, NULL, sc, &sc->sc_icookie);
335 		if (error)
336 			error = bus_setup_intr(dev, sc->sc_ires,
337 			    INTR_TYPE_TTY | INTR_MPSAFE, NULL,
338 			    (driver_intr_t *)puc_intr, sc, &sc->sc_icookie);
339 		else
340 			sc->sc_fastintr = 1;
341 
342 		if (error) {
343 			device_printf(dev, "could not activate interrupt\n");
344 			bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
345 			    sc->sc_ires);
346 			sc->sc_ires = NULL;
347 		}
348 	}
349 	if (sc->sc_ires == NULL) {
350 		/* XXX no interrupt resource. Force polled mode. */
351 		sc->sc_polled = 1;
352 	}
353 
354 	/* Probe and attach our children. */
355 	for (idx = 0; idx < sc->sc_nports; idx++) {
356 		port = &sc->sc_port[idx];
357 		if (port->p_dev == NULL)
358 			continue;
359 		error = device_probe_and_attach(port->p_dev);
360 		if (error) {
361 			device_delete_child(dev, port->p_dev);
362 			port->p_dev = NULL;
363 		}
364 	}
365 
366 	/*
367 	 * If there are no serdev devices, then our interrupt handler
368 	 * will do nothing. Tear it down.
369 	 */
370 	if (sc->sc_serdevs == 0UL)
371 		bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
372 
373 	return (0);
374 
375 fail:
376 	for (idx = 0; idx < sc->sc_nports; idx++) {
377 		port = &sc->sc_port[idx];
378 		if (port->p_dev != NULL)
379 			device_delete_child(dev, port->p_dev);
380 		if (port->p_rres != NULL)
381 			rman_release_resource(port->p_rres);
382 		if (port->p_ires != NULL)
383 			rman_release_resource(port->p_ires);
384 	}
385 	for (idx = 0; idx < PUC_PCI_BARS; idx++) {
386 		bar = &sc->sc_bar[idx];
387 		if (bar->b_res != NULL)
388 			bus_release_resource(sc->sc_dev, bar->b_type,
389 			    bar->b_rid, bar->b_res);
390 	}
391 	rman_fini(&sc->sc_irq);
392 	free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
393 	rman_fini(&sc->sc_iomem);
394 	free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
395 	rman_fini(&sc->sc_ioport);
396 	free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
397 	free(sc->sc_port, M_PUC);
398 	return (error);
399 }
400 
401 int
402 puc_bfe_detach(device_t dev)
403 {
404 	struct puc_bar *bar;
405 	struct puc_port *port;
406 	struct puc_softc *sc;
407 	int error, idx;
408 
409 	sc = device_get_softc(dev);
410 
411 	/* Detach our children. */
412 	error = 0;
413 	for (idx = 0; idx < sc->sc_nports; idx++) {
414 		port = &sc->sc_port[idx];
415 		if (port->p_dev == NULL)
416 			continue;
417 		if (device_delete_child(dev, port->p_dev) == 0) {
418 			if (port->p_rres != NULL)
419 				rman_release_resource(port->p_rres);
420 			if (port->p_ires != NULL)
421 				rman_release_resource(port->p_ires);
422 		} else
423 			error = ENXIO;
424 	}
425 	if (error)
426 		return (error);
427 
428 	if (sc->sc_serdevs != 0UL)
429 		bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
430 	bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, sc->sc_ires);
431 
432 	for (idx = 0; idx < PUC_PCI_BARS; idx++) {
433 		bar = &sc->sc_bar[idx];
434 		if (bar->b_res != NULL)
435 			bus_release_resource(sc->sc_dev, bar->b_type,
436 			    bar->b_rid, bar->b_res);
437 	}
438 
439 	rman_fini(&sc->sc_irq);
440 	free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
441 	rman_fini(&sc->sc_iomem);
442 	free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
443 	rman_fini(&sc->sc_ioport);
444 	free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
445 	free(sc->sc_port, M_PUC);
446 	return (0);
447 }
448 
449 int
450 puc_bfe_probe(device_t dev, const struct puc_cfg *cfg)
451 {
452 	struct puc_softc *sc;
453 	intptr_t res;
454 	int error;
455 
456 	sc = device_get_softc(dev);
457 	sc->sc_dev = dev;
458 	sc->sc_cfg = cfg;
459 
460 	/* We don't attach to single-port serial cards. */
461 	if (cfg->ports == PUC_PORT_1S || cfg->ports == PUC_PORT_1P)
462 		return (EDOOFUS);
463 	error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
464 	if (error)
465 		return (error);
466 	error = puc_config(sc, PUC_CFG_GET_DESC, 0, &res);
467 	if (error)
468 		return (error);
469 	if (res != 0)
470 		device_set_desc(dev, (const char *)res);
471 	return (BUS_PROBE_DEFAULT);
472 }
473 
474 struct resource *
475 puc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
476     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
477 {
478 	struct puc_port *port;
479 	struct resource *res;
480 	device_t assigned, originator;
481 	int error;
482 
483 	/* Get our immediate child. */
484 	originator = child;
485 	while (child != NULL && device_get_parent(child) != dev)
486 		child = device_get_parent(child);
487 	if (child == NULL)
488 		return (NULL);
489 
490 	port = device_get_ivars(child);
491 	KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
492 
493 	if (rid == NULL || *rid != 0)
494 		return (NULL);
495 
496 	/* We only support default allocations. */
497 	if (!RMAN_IS_DEFAULT_RANGE(start, end))
498 		return (NULL);
499 
500 	if (type == port->p_bar->b_type)
501 		res = port->p_rres;
502 	else if (type == SYS_RES_IRQ)
503 		res = port->p_ires;
504 	else
505 		return (NULL);
506 
507 	if (res == NULL)
508 		return (NULL);
509 
510 	assigned = rman_get_device(res);
511 	if (assigned == NULL)	/* Not allocated */
512 		rman_set_device(res, originator);
513 	else if (assigned != originator)
514 		return (NULL);
515 
516 	if (flags & RF_ACTIVE) {
517 		error = rman_activate_resource(res);
518 		if (error) {
519 			if (assigned == NULL)
520 				rman_set_device(res, NULL);
521 			return (NULL);
522 		}
523 	}
524 
525 	return (res);
526 }
527 
528 int
529 puc_bus_release_resource(device_t dev, device_t child, struct resource *res)
530 {
531 	struct puc_port *port;
532 	device_t originator;
533 
534 	/* Get our immediate child. */
535 	originator = child;
536 	while (child != NULL && device_get_parent(child) != dev)
537 		child = device_get_parent(child);
538 	if (child == NULL)
539 		return (EINVAL);
540 
541 	port = device_get_ivars(child);
542 	KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
543 
544 	if (res == NULL)
545 		return (EINVAL);
546 
547 	if (res == port->p_ires) {
548 		if (port->p_hasintr)
549 			return (EBUSY);
550 	} else if (res != port->p_rres)
551 		return (EINVAL);
552 
553 	if (rman_get_device(res) != originator)
554 		return (ENXIO);
555 	if (rman_get_flags(res) & RF_ACTIVE)
556 		rman_deactivate_resource(res);
557 	rman_set_device(res, NULL);
558 	return (0);
559 }
560 
561 int
562 puc_bus_get_resource(device_t dev, device_t child, int type, int rid,
563     rman_res_t *startp, rman_res_t *countp)
564 {
565 	struct puc_port *port;
566 	struct resource *res;
567 	rman_res_t start;
568 
569 	/* Get our immediate child. */
570 	while (child != NULL && device_get_parent(child) != dev)
571 		child = device_get_parent(child);
572 	if (child == NULL)
573 		return (EINVAL);
574 
575 	port = device_get_ivars(child);
576 	KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
577 
578 	if (type == port->p_bar->b_type)
579 		res = port->p_rres;
580 	else if (type == SYS_RES_IRQ)
581 		res = port->p_ires;
582 	else
583 		return (ENXIO);
584 
585 	if (rid != 0 || res == NULL)
586 		return (ENXIO);
587 
588 	start = rman_get_start(res);
589 	if (startp != NULL)
590 		*startp = start;
591 	if (countp != NULL)
592 		*countp = rman_get_end(res) - start + 1;
593 	return (0);
594 }
595 
596 int
597 puc_bus_setup_intr(device_t dev, device_t child, struct resource *res,
598     int flags, driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
599 {
600 	struct puc_port *port;
601 	struct puc_softc *sc;
602 	device_t originator;
603 	int i, isrc, serdev;
604 
605 	sc = device_get_softc(dev);
606 
607 	/* Get our immediate child. */
608 	originator = child;
609 	while (child != NULL && device_get_parent(child) != dev)
610 		child = device_get_parent(child);
611 	if (child == NULL)
612 		return (EINVAL);
613 
614 	port = device_get_ivars(child);
615 	KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
616 
617 	if (cookiep == NULL || res != port->p_ires)
618 		return (EINVAL);
619 	/* We demand that serdev devices use filter_only interrupts. */
620 	if (port->p_type == PUC_TYPE_SERIAL && ihand != NULL)
621 		return (ENXIO);
622 	if (rman_get_device(port->p_ires) != originator)
623 		return (ENXIO);
624 
625 	/*
626 	 * Have non-serdev ports handled by the bus implementation. It
627 	 * supports multiple handlers for a single interrupt as it is,
628 	 * so we wouldn't add value if we did it ourselves.
629 	 */
630 	serdev = 0;
631 	if (port->p_type == PUC_TYPE_SERIAL) {
632 		i = 0, isrc = SER_INT_OVERRUN;
633 		while (i < PUC_ISRCCNT) {
634 			port->p_ihsrc[i] = SERDEV_IHAND(originator, isrc);
635 			if (port->p_ihsrc[i] != NULL)
636 				serdev = 1;
637 			i++, isrc <<= 1;
638 		}
639 	}
640 	if (!serdev)
641 		return (BUS_SETUP_INTR(device_get_parent(dev), originator,
642 		    sc->sc_ires, flags, filt, ihand, arg, cookiep));
643 
644 	sc->sc_serdevs |= 1UL << (port->p_nr - 1);
645 
646 	port->p_hasintr = 1;
647 	port->p_iharg = arg;
648 
649 	*cookiep = port;
650 	return (0);
651 }
652 
653 int
654 puc_bus_teardown_intr(device_t dev, device_t child, struct resource *res,
655     void *cookie)
656 {
657 	struct puc_port *port;
658 	struct puc_softc *sc;
659 	device_t originator;
660 	int i;
661 
662 	sc = device_get_softc(dev);
663 
664 	/* Get our immediate child. */
665 	originator = child;
666 	while (child != NULL && device_get_parent(child) != dev)
667 		child = device_get_parent(child);
668 	if (child == NULL)
669 		return (EINVAL);
670 
671 	port = device_get_ivars(child);
672 	KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
673 
674 	if (res != port->p_ires)
675 		return (EINVAL);
676 	if (rman_get_device(port->p_ires) != originator)
677 		return (ENXIO);
678 
679 	if (!port->p_hasintr)
680 		return (BUS_TEARDOWN_INTR(device_get_parent(dev), originator,
681 		    sc->sc_ires, cookie));
682 
683 	if (cookie != port)
684 		return (EINVAL);
685 
686 	port->p_hasintr = 0;
687 	port->p_iharg = NULL;
688 
689 	for (i = 0; i < PUC_ISRCCNT; i++)
690 		port->p_ihsrc[i] = NULL;
691 
692 	return (0);
693 }
694 
695 int
696 puc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
697 {
698 	struct puc_port *port;
699 
700 	/* Get our immediate child. */
701 	while (child != NULL && device_get_parent(child) != dev)
702 		child = device_get_parent(child);
703 	if (child == NULL)
704 		return (EINVAL);
705 
706 	port = device_get_ivars(child);
707 	KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
708 
709 	if (result == NULL)
710 		return (EINVAL);
711 
712 	switch(index) {
713 	case PUC_IVAR_CLOCK:
714 		*result = port->p_rclk;
715 		break;
716 	case PUC_IVAR_TYPE:
717 		*result = port->p_type;
718 		break;
719 	default:
720 		return (ENOENT);
721 	}
722 	return (0);
723 }
724 
725 int
726 puc_bus_print_child(device_t dev, device_t child)
727 {
728 	struct puc_port *port;
729 	int retval;
730 
731 	port = device_get_ivars(child);
732 	retval = 0;
733 
734 	retval += bus_print_child_header(dev, child);
735 	retval += printf(" at port %d", port->p_nr);
736 	retval += bus_print_child_footer(dev, child);
737 
738 	return (retval);
739 }
740 
741 int
742 puc_bus_child_location(device_t dev, device_t child, struct sbuf *sb)
743 {
744 	struct puc_port *port;
745 
746 	port = device_get_ivars(child);
747 	sbuf_printf(sb, "port=%d", port->p_nr);
748 	return (0);
749 }
750 
751 int
752 puc_bus_child_pnpinfo(device_t dev, device_t child, struct sbuf *sb)
753 {
754 	struct puc_port *port;
755 
756 	port = device_get_ivars(child);
757 	sbuf_printf(sb, "type=%d", port->p_type);
758 	return (0);
759 }
760