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