2 * Copyright (c) 2006 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/sysctl.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
46 #include <dev/puc/puc_bus.h>
47 #include <dev/puc/puc_cfg.h>
48 #include <dev/puc/puc_bfe.h>
53 struct puc_bar *p_bar;
54 struct resource *p_rres;
55 struct resource *p_ires;
63 serdev_intr_t *p_ihsrc[PUC_ISRCCNT];
69 devclass_t puc_devclass;
70 const char puc_driver_name[] = "puc";
72 static MALLOC_DEFINE(M_PUC, "PUC", "PUC driver");
74 SYSCTL_NODE(_hw, OID_AUTO, puc, CTLFLAG_RD, 0, "puc(9) driver configuration");
77 puc_get_bar(struct puc_softc *sc, int rid)
81 rman_res_t end, start;
84 /* Find the BAR entry with the given RID. */
86 while (i < PUC_PCI_BARS && sc->sc_bar[i].b_rid != rid)
89 return (&sc->sc_bar[i]);
91 /* Not found. If we're looking for an unused entry, return NULL. */
95 /* Get an unused entry for us to fill. */
96 bar = puc_get_bar(sc, -1);
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) {
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) {
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);
120 bus_release_resource(sc->sc_dev, bar->b_type, bar->b_rid,
133 struct puc_port *port;
134 struct puc_softc *sc = arg;
135 u_long ds, dev, devs;
136 int i, idx, ipend, isrc, nints;
142 * Obtain the set of devices with pending interrupts.
144 devs = sc->sc_serdevs;
145 if (sc->sc_ilr == PUC_ILR_DIGI) {
147 while (devs & (0xfful << idx)) {
148 ilr = ~bus_read_1(sc->sc_port[idx].p_rres, 7);
149 devs &= ~0ul ^ ((u_long)ilr << idx);
152 } else if (sc->sc_ilr == PUC_ILR_QUATECH) {
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.
159 ilr = bus_read_1(sc->sc_port[0].p_rres, 7);
160 if (ilr != (sc->sc_cfg_data & 0xff))
167 * Obtain the set of interrupt sources from those devices
168 * that have pending interrupts.
174 while ((ds & dev) == 0UL)
177 port = &sc->sc_port[idx];
178 port->p_ipend = SERDEV_IPEND(port->p_dev);
179 ipend |= port->p_ipend;
184 i = 0, isrc = SER_INT_OVERRUN;
186 while (i < PUC_ISRCCNT && !(ipend & isrc))
188 KASSERT(i < PUC_ISRCCNT, ("%s", __func__));
193 while ((ds & dev) == 0UL)
196 port = &sc->sc_port[idx];
197 if (!(port->p_ipend & isrc))
199 if (port->p_ihsrc[i] != NULL)
200 (*port->p_ihsrc[i])(port->p_iharg);
206 return ((nints > 0) ? FILTER_HANDLED : FILTER_STRAY);
210 puc_bfe_attach(device_t dev)
214 struct puc_port *port;
215 struct puc_softc *sc;
218 bus_addr_t ofs, start;
220 bus_space_handle_t bsh;
224 sc = device_get_softc(dev);
226 for (idx = 0; idx < PUC_PCI_BARS; idx++)
227 sc->sc_bar[idx].b_rid = -1;
230 sc->sc_ioport.rm_type = RMAN_ARRAY;
231 error = rman_init(&sc->sc_ioport);
233 sc->sc_iomem.rm_type = RMAN_ARRAY;
234 error = rman_init(&sc->sc_iomem);
236 sc->sc_irq.rm_type = RMAN_ARRAY;
237 error = rman_init(&sc->sc_irq);
240 rman_fini(&sc->sc_iomem);
242 rman_fini(&sc->sc_ioport);
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);
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);
263 error = rman_manage_region(&sc->sc_irq, 1, sc->sc_nports);
267 error = puc_config(sc, PUC_CFG_SETUP, 0, &res);
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);
278 error = puc_config(sc, PUC_CFG_GET_RID, idx, &res);
281 bar = puc_get_bar(sc, res);
287 start = rman_get_start(bar->b_res);
288 error = puc_config(sc, PUC_CFG_GET_OFS, idx, &res);
292 error = puc_config(sc, PUC_CFG_GET_LEN, idx, &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);
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) {
313 error = puc_config(sc, PUC_CFG_GET_CLOCK, idx, &res);
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);
323 error = puc_config(sc, PUC_CFG_GET_ILR, 0, &res);
327 if (bootverbose && sc->sc_ilr != 0)
328 device_printf(dev, "using interrupt latch register\n");
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);
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);
343 device_printf(dev, "could not activate interrupt\n");
344 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
349 if (sc->sc_ires == NULL) {
350 /* XXX no interrupt resource. Force polled mode. */
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)
359 error = device_probe_and_attach(port->p_dev);
361 device_delete_child(dev, port->p_dev);
367 * If there are no serdev devices, then our interrupt handler
368 * will do nothing. Tear it down.
370 if (sc->sc_serdevs == 0UL)
371 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
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);
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);
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);
402 puc_bfe_detach(device_t dev)
405 struct puc_port *port;
406 struct puc_softc *sc;
409 sc = device_get_softc(dev);
411 /* Detach our children. */
413 for (idx = 0; idx < sc->sc_nports; idx++) {
414 port = &sc->sc_port[idx];
415 if (port->p_dev == NULL)
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);
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);
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);
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);
450 puc_bfe_probe(device_t dev, const struct puc_cfg *cfg)
452 struct puc_softc *sc;
456 sc = device_get_softc(dev);
460 /* We don't attach to single-port serial cards. */
461 if (cfg->ports == PUC_PORT_1S || cfg->ports == PUC_PORT_1P)
463 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
466 error = puc_config(sc, PUC_CFG_GET_DESC, 0, &res);
470 device_set_desc(dev, (const char *)res);
471 return (BUS_PROBE_DEFAULT);
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)
478 struct puc_port *port;
479 struct resource *res;
480 device_t assigned, originator;
483 /* Get our immediate child. */
485 while (child != NULL && device_get_parent(child) != dev)
486 child = device_get_parent(child);
490 port = device_get_ivars(child);
491 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
493 if (rid == NULL || *rid != 0)
496 /* We only support default allocations. */
497 if (!RMAN_IS_DEFAULT_RANGE(start, end))
500 if (type == port->p_bar->b_type)
502 else if (type == SYS_RES_IRQ)
510 assigned = rman_get_device(res);
511 if (assigned == NULL) /* Not allocated */
512 rman_set_device(res, originator);
513 else if (assigned != originator)
516 if (flags & RF_ACTIVE) {
517 error = rman_activate_resource(res);
519 if (assigned == NULL)
520 rman_set_device(res, NULL);
529 puc_bus_release_resource(device_t dev, device_t child, int type, int rid,
530 struct resource *res)
532 struct puc_port *port;
535 /* Get our immediate child. */
537 while (child != NULL && device_get_parent(child) != dev)
538 child = device_get_parent(child);
542 port = device_get_ivars(child);
543 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
545 if (rid != 0 || res == NULL)
548 if (type == port->p_bar->b_type) {
549 if (res != port->p_rres)
551 } else if (type == SYS_RES_IRQ) {
552 if (res != port->p_ires)
559 if (rman_get_device(res) != originator)
561 if (rman_get_flags(res) & RF_ACTIVE)
562 rman_deactivate_resource(res);
563 rman_set_device(res, NULL);
568 puc_bus_get_resource(device_t dev, device_t child, int type, int rid,
569 rman_res_t *startp, rman_res_t *countp)
571 struct puc_port *port;
572 struct resource *res;
575 /* Get our immediate child. */
576 while (child != NULL && device_get_parent(child) != dev)
577 child = device_get_parent(child);
581 port = device_get_ivars(child);
582 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
584 if (type == port->p_bar->b_type)
586 else if (type == SYS_RES_IRQ)
591 if (rid != 0 || res == NULL)
594 start = rman_get_start(res);
598 *countp = rman_get_end(res) - start + 1;
603 puc_bus_setup_intr(device_t dev, device_t child, struct resource *res,
604 int flags, driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
606 struct puc_port *port;
607 struct puc_softc *sc;
611 sc = device_get_softc(dev);
613 /* Get our immediate child. */
615 while (child != NULL && device_get_parent(child) != dev)
616 child = device_get_parent(child);
620 port = device_get_ivars(child);
621 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
623 if (cookiep == NULL || res != port->p_ires)
625 /* We demand that serdev devices use filter_only interrupts. */
626 if (port->p_type == PUC_TYPE_SERIAL && ihand != NULL)
628 if (rman_get_device(port->p_ires) != originator)
632 * Have non-serdev ports handled by the bus implementation. It
633 * supports multiple handlers for a single interrupt as it is,
634 * so we wouldn't add value if we did it ourselves.
637 if (port->p_type == PUC_TYPE_SERIAL) {
638 i = 0, isrc = SER_INT_OVERRUN;
639 while (i < PUC_ISRCCNT) {
640 port->p_ihsrc[i] = SERDEV_IHAND(originator, isrc);
641 if (port->p_ihsrc[i] != NULL)
647 return (BUS_SETUP_INTR(device_get_parent(dev), originator,
648 sc->sc_ires, flags, filt, ihand, arg, cookiep));
650 sc->sc_serdevs |= 1UL << (port->p_nr - 1);
660 puc_bus_teardown_intr(device_t dev, device_t child, struct resource *res,
663 struct puc_port *port;
664 struct puc_softc *sc;
668 sc = device_get_softc(dev);
670 /* Get our immediate child. */
672 while (child != NULL && device_get_parent(child) != dev)
673 child = device_get_parent(child);
677 port = device_get_ivars(child);
678 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
680 if (res != port->p_ires)
682 if (rman_get_device(port->p_ires) != originator)
685 if (!port->p_hasintr)
686 return (BUS_TEARDOWN_INTR(device_get_parent(dev), originator,
687 sc->sc_ires, cookie));
693 port->p_iharg = NULL;
695 for (i = 0; i < PUC_ISRCCNT; i++)
696 port->p_ihsrc[i] = NULL;
702 puc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
704 struct puc_port *port;
706 /* Get our immediate child. */
707 while (child != NULL && device_get_parent(child) != dev)
708 child = device_get_parent(child);
712 port = device_get_ivars(child);
713 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
720 *result = port->p_rclk;
723 *result = port->p_type;
732 puc_bus_print_child(device_t dev, device_t child)
734 struct puc_port *port;
737 port = device_get_ivars(child);
740 retval += bus_print_child_header(dev, child);
741 retval += printf(" at port %d", port->p_nr);
742 retval += bus_print_child_footer(dev, child);
748 puc_bus_child_location_str(device_t dev, device_t child, char *buf,
751 struct puc_port *port;
753 port = device_get_ivars(child);
754 snprintf(buf, buflen, "port=%d", port->p_nr);
759 puc_bus_child_pnpinfo_str(device_t dev, device_t child, char *buf,
762 struct puc_port *port;
764 port = device_get_ivars(child);
765 snprintf(buf, buflen, "type=%d", port->p_type);