2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
41 #include <machine/bus.h>
42 #include <machine/resource.h>
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
48 #include <dev/puc/puc_bus.h>
49 #include <dev/puc/puc_cfg.h>
50 #include <dev/puc/puc_bfe.h>
55 struct puc_bar *p_bar;
56 struct resource *p_rres;
57 struct resource *p_ires;
65 serdev_intr_t *p_ihsrc[PUC_ISRCCNT];
71 devclass_t puc_devclass;
72 const char puc_driver_name[] = "puc";
74 static MALLOC_DEFINE(M_PUC, "PUC", "PUC driver");
76 SYSCTL_NODE(_hw, OID_AUTO, puc, CTLFLAG_RD, 0, "puc(9) driver configuration");
79 puc_get_bar(struct puc_softc *sc, int rid)
83 rman_res_t end, start;
86 /* Find the BAR entry with the given RID. */
88 while (i < PUC_PCI_BARS && sc->sc_bar[i].b_rid != rid)
91 return (&sc->sc_bar[i]);
93 /* Not found. If we're looking for an unused entry, return NULL. */
97 /* Get an unused entry for us to fill. */
98 bar = puc_get_bar(sc, -1);
102 bar->b_type = SYS_RES_IOPORT;
103 bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
104 &bar->b_rid, RF_ACTIVE);
105 if (bar->b_res == NULL) {
107 bar->b_type = SYS_RES_MEMORY;
108 bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
109 &bar->b_rid, RF_ACTIVE);
110 if (bar->b_res == NULL) {
116 /* Update our managed space. */
117 rm = (bar->b_type == SYS_RES_IOPORT) ? &sc->sc_ioport : &sc->sc_iomem;
118 start = rman_get_start(bar->b_res);
119 end = rman_get_end(bar->b_res);
120 error = rman_manage_region(rm, start, end);
122 bus_release_resource(sc->sc_dev, bar->b_type, bar->b_rid,
135 struct puc_port *port;
136 struct puc_softc *sc = arg;
137 u_long ds, dev, devs;
138 int i, idx, ipend, isrc, nints;
144 * Obtain the set of devices with pending interrupts.
146 devs = sc->sc_serdevs;
147 if (sc->sc_ilr == PUC_ILR_DIGI) {
149 while (devs & (0xfful << idx)) {
150 ilr = ~bus_read_1(sc->sc_port[idx].p_rres, 7);
151 devs &= ~0ul ^ ((u_long)ilr << idx);
154 } else if (sc->sc_ilr == PUC_ILR_QUATECH) {
156 * Don't trust the value if it's the same as the option
157 * register. It may mean that the ILR is not active and
158 * we're reading the option register instead. This may
159 * lead to false positives on 8-port boards.
161 ilr = bus_read_1(sc->sc_port[0].p_rres, 7);
162 if (ilr != (sc->sc_cfg_data & 0xff))
169 * Obtain the set of interrupt sources from those devices
170 * that have pending interrupts.
176 while ((ds & dev) == 0UL)
179 port = &sc->sc_port[idx];
180 port->p_ipend = SERDEV_IPEND(port->p_dev);
181 ipend |= port->p_ipend;
186 i = 0, isrc = SER_INT_OVERRUN;
188 while (i < PUC_ISRCCNT && !(ipend & isrc))
190 KASSERT(i < PUC_ISRCCNT, ("%s", __func__));
195 while ((ds & dev) == 0UL)
198 port = &sc->sc_port[idx];
199 if (!(port->p_ipend & isrc))
201 if (port->p_ihsrc[i] != NULL)
202 (*port->p_ihsrc[i])(port->p_iharg);
208 return ((nints > 0) ? FILTER_HANDLED : FILTER_STRAY);
212 puc_bfe_attach(device_t dev)
216 struct puc_port *port;
217 struct puc_softc *sc;
220 bus_addr_t ofs, start;
222 bus_space_handle_t bsh;
226 sc = device_get_softc(dev);
228 for (idx = 0; idx < PUC_PCI_BARS; idx++)
229 sc->sc_bar[idx].b_rid = -1;
232 sc->sc_ioport.rm_type = RMAN_ARRAY;
233 error = rman_init(&sc->sc_ioport);
235 sc->sc_iomem.rm_type = RMAN_ARRAY;
236 error = rman_init(&sc->sc_iomem);
238 sc->sc_irq.rm_type = RMAN_ARRAY;
239 error = rman_init(&sc->sc_irq);
242 rman_fini(&sc->sc_iomem);
244 rman_fini(&sc->sc_ioport);
249 snprintf(buffer, sizeof(buffer), "%s I/O port mapping",
250 device_get_nameunit(dev));
251 sc->sc_ioport.rm_descr = strdup(buffer, M_PUC);
252 snprintf(buffer, sizeof(buffer), "%s I/O memory mapping",
253 device_get_nameunit(dev));
254 sc->sc_iomem.rm_descr = strdup(buffer, M_PUC);
255 snprintf(buffer, sizeof(buffer), "%s port numbers",
256 device_get_nameunit(dev));
257 sc->sc_irq.rm_descr = strdup(buffer, M_PUC);
259 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
260 KASSERT(error == 0, ("%s %d", __func__, __LINE__));
261 sc->sc_nports = (int)res;
262 sc->sc_port = malloc(sc->sc_nports * sizeof(struct puc_port),
263 M_PUC, M_WAITOK|M_ZERO);
265 error = rman_manage_region(&sc->sc_irq, 1, sc->sc_nports);
269 error = puc_config(sc, PUC_CFG_SETUP, 0, &res);
273 for (idx = 0; idx < sc->sc_nports; idx++) {
274 port = &sc->sc_port[idx];
275 port->p_nr = idx + 1;
276 error = puc_config(sc, PUC_CFG_GET_TYPE, idx, &res);
280 error = puc_config(sc, PUC_CFG_GET_RID, idx, &res);
283 bar = puc_get_bar(sc, res);
289 start = rman_get_start(bar->b_res);
290 error = puc_config(sc, PUC_CFG_GET_OFS, idx, &res);
294 error = puc_config(sc, PUC_CFG_GET_LEN, idx, &res);
298 rm = (bar->b_type == SYS_RES_IOPORT)
299 ? &sc->sc_ioport: &sc->sc_iomem;
300 port->p_rres = rman_reserve_resource(rm, start + ofs,
301 start + ofs + size - 1, size, 0, NULL);
302 if (port->p_rres != NULL) {
303 bsh = rman_get_bushandle(bar->b_res);
304 bst = rman_get_bustag(bar->b_res);
305 bus_space_subregion(bst, bsh, ofs, size, &bsh);
306 rman_set_bushandle(port->p_rres, bsh);
307 rman_set_bustag(port->p_rres, bst);
309 port->p_ires = rman_reserve_resource(&sc->sc_irq, port->p_nr,
310 port->p_nr, 1, 0, NULL);
311 if (port->p_ires == NULL) {
315 error = puc_config(sc, PUC_CFG_GET_CLOCK, idx, &res);
320 port->p_dev = device_add_child(dev, NULL, -1);
321 if (port->p_dev != NULL)
322 device_set_ivars(port->p_dev, (void *)port);
325 error = puc_config(sc, PUC_CFG_GET_ILR, 0, &res);
329 if (bootverbose && sc->sc_ilr != 0)
330 device_printf(dev, "using interrupt latch register\n");
332 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
333 RF_ACTIVE|RF_SHAREABLE);
334 if (sc->sc_ires != NULL) {
335 error = bus_setup_intr(dev, sc->sc_ires,
336 INTR_TYPE_TTY, puc_intr, NULL, sc, &sc->sc_icookie);
338 error = bus_setup_intr(dev, sc->sc_ires,
339 INTR_TYPE_TTY | INTR_MPSAFE, NULL,
340 (driver_intr_t *)puc_intr, sc, &sc->sc_icookie);
345 device_printf(dev, "could not activate interrupt\n");
346 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
351 if (sc->sc_ires == NULL) {
352 /* XXX no interrupt resource. Force polled mode. */
356 /* Probe and attach our children. */
357 for (idx = 0; idx < sc->sc_nports; idx++) {
358 port = &sc->sc_port[idx];
359 if (port->p_dev == NULL)
361 error = device_probe_and_attach(port->p_dev);
363 device_delete_child(dev, port->p_dev);
369 * If there are no serdev devices, then our interrupt handler
370 * will do nothing. Tear it down.
372 if (sc->sc_serdevs == 0UL)
373 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
378 for (idx = 0; idx < sc->sc_nports; idx++) {
379 port = &sc->sc_port[idx];
380 if (port->p_dev != NULL)
381 device_delete_child(dev, port->p_dev);
382 if (port->p_rres != NULL)
383 rman_release_resource(port->p_rres);
384 if (port->p_ires != NULL)
385 rman_release_resource(port->p_ires);
387 for (idx = 0; idx < PUC_PCI_BARS; idx++) {
388 bar = &sc->sc_bar[idx];
389 if (bar->b_res != NULL)
390 bus_release_resource(sc->sc_dev, bar->b_type,
391 bar->b_rid, bar->b_res);
393 rman_fini(&sc->sc_irq);
394 free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
395 rman_fini(&sc->sc_iomem);
396 free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
397 rman_fini(&sc->sc_ioport);
398 free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
399 free(sc->sc_port, M_PUC);
404 puc_bfe_detach(device_t dev)
407 struct puc_port *port;
408 struct puc_softc *sc;
411 sc = device_get_softc(dev);
413 /* Detach our children. */
415 for (idx = 0; idx < sc->sc_nports; idx++) {
416 port = &sc->sc_port[idx];
417 if (port->p_dev == NULL)
419 if (device_delete_child(dev, port->p_dev) == 0) {
420 if (port->p_rres != NULL)
421 rman_release_resource(port->p_rres);
422 if (port->p_ires != NULL)
423 rman_release_resource(port->p_ires);
430 if (sc->sc_serdevs != 0UL)
431 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
432 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, sc->sc_ires);
434 for (idx = 0; idx < PUC_PCI_BARS; idx++) {
435 bar = &sc->sc_bar[idx];
436 if (bar->b_res != NULL)
437 bus_release_resource(sc->sc_dev, bar->b_type,
438 bar->b_rid, bar->b_res);
441 rman_fini(&sc->sc_irq);
442 free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
443 rman_fini(&sc->sc_iomem);
444 free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
445 rman_fini(&sc->sc_ioport);
446 free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
447 free(sc->sc_port, M_PUC);
452 puc_bfe_probe(device_t dev, const struct puc_cfg *cfg)
454 struct puc_softc *sc;
458 sc = device_get_softc(dev);
462 /* We don't attach to single-port serial cards. */
463 if (cfg->ports == PUC_PORT_1S || cfg->ports == PUC_PORT_1P)
465 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
468 error = puc_config(sc, PUC_CFG_GET_DESC, 0, &res);
472 device_set_desc(dev, (const char *)res);
473 return (BUS_PROBE_DEFAULT);
477 puc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
478 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
480 struct puc_port *port;
481 struct resource *res;
482 device_t assigned, originator;
485 /* Get our immediate child. */
487 while (child != NULL && device_get_parent(child) != dev)
488 child = device_get_parent(child);
492 port = device_get_ivars(child);
493 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
495 if (rid == NULL || *rid != 0)
498 /* We only support default allocations. */
499 if (!RMAN_IS_DEFAULT_RANGE(start, end))
502 if (type == port->p_bar->b_type)
504 else if (type == SYS_RES_IRQ)
512 assigned = rman_get_device(res);
513 if (assigned == NULL) /* Not allocated */
514 rman_set_device(res, originator);
515 else if (assigned != originator)
518 if (flags & RF_ACTIVE) {
519 error = rman_activate_resource(res);
521 if (assigned == NULL)
522 rman_set_device(res, NULL);
531 puc_bus_release_resource(device_t dev, device_t child, int type, int rid,
532 struct resource *res)
534 struct puc_port *port;
537 /* Get our immediate child. */
539 while (child != NULL && device_get_parent(child) != dev)
540 child = device_get_parent(child);
544 port = device_get_ivars(child);
545 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
547 if (rid != 0 || res == NULL)
550 if (type == port->p_bar->b_type) {
551 if (res != port->p_rres)
553 } else if (type == SYS_RES_IRQ) {
554 if (res != port->p_ires)
561 if (rman_get_device(res) != originator)
563 if (rman_get_flags(res) & RF_ACTIVE)
564 rman_deactivate_resource(res);
565 rman_set_device(res, NULL);
570 puc_bus_get_resource(device_t dev, device_t child, int type, int rid,
571 rman_res_t *startp, rman_res_t *countp)
573 struct puc_port *port;
574 struct resource *res;
577 /* Get our immediate child. */
578 while (child != NULL && device_get_parent(child) != dev)
579 child = device_get_parent(child);
583 port = device_get_ivars(child);
584 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
586 if (type == port->p_bar->b_type)
588 else if (type == SYS_RES_IRQ)
593 if (rid != 0 || res == NULL)
596 start = rman_get_start(res);
600 *countp = rman_get_end(res) - start + 1;
605 puc_bus_setup_intr(device_t dev, device_t child, struct resource *res,
606 int flags, driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
608 struct puc_port *port;
609 struct puc_softc *sc;
613 sc = device_get_softc(dev);
615 /* Get our immediate child. */
617 while (child != NULL && device_get_parent(child) != dev)
618 child = device_get_parent(child);
622 port = device_get_ivars(child);
623 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
625 if (cookiep == NULL || res != port->p_ires)
627 /* We demand that serdev devices use filter_only interrupts. */
628 if (port->p_type == PUC_TYPE_SERIAL && ihand != NULL)
630 if (rman_get_device(port->p_ires) != originator)
634 * Have non-serdev ports handled by the bus implementation. It
635 * supports multiple handlers for a single interrupt as it is,
636 * so we wouldn't add value if we did it ourselves.
639 if (port->p_type == PUC_TYPE_SERIAL) {
640 i = 0, isrc = SER_INT_OVERRUN;
641 while (i < PUC_ISRCCNT) {
642 port->p_ihsrc[i] = SERDEV_IHAND(originator, isrc);
643 if (port->p_ihsrc[i] != NULL)
649 return (BUS_SETUP_INTR(device_get_parent(dev), originator,
650 sc->sc_ires, flags, filt, ihand, arg, cookiep));
652 sc->sc_serdevs |= 1UL << (port->p_nr - 1);
662 puc_bus_teardown_intr(device_t dev, device_t child, struct resource *res,
665 struct puc_port *port;
666 struct puc_softc *sc;
670 sc = device_get_softc(dev);
672 /* Get our immediate child. */
674 while (child != NULL && device_get_parent(child) != dev)
675 child = device_get_parent(child);
679 port = device_get_ivars(child);
680 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
682 if (res != port->p_ires)
684 if (rman_get_device(port->p_ires) != originator)
687 if (!port->p_hasintr)
688 return (BUS_TEARDOWN_INTR(device_get_parent(dev), originator,
689 sc->sc_ires, cookie));
695 port->p_iharg = NULL;
697 for (i = 0; i < PUC_ISRCCNT; i++)
698 port->p_ihsrc[i] = NULL;
704 puc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
706 struct puc_port *port;
708 /* Get our immediate child. */
709 while (child != NULL && device_get_parent(child) != dev)
710 child = device_get_parent(child);
714 port = device_get_ivars(child);
715 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
722 *result = port->p_rclk;
725 *result = port->p_type;
734 puc_bus_print_child(device_t dev, device_t child)
736 struct puc_port *port;
739 port = device_get_ivars(child);
742 retval += bus_print_child_header(dev, child);
743 retval += printf(" at port %d", port->p_nr);
744 retval += bus_print_child_footer(dev, child);
750 puc_bus_child_location_str(device_t dev, device_t child, char *buf,
753 struct puc_port *port;
755 port = device_get_ivars(child);
756 snprintf(buf, buflen, "port=%d", port->p_nr);
761 puc_bus_child_pnpinfo_str(device_t dev, device_t child, char *buf,
764 struct puc_port *port;
766 port = device_get_ivars(child);
767 snprintf(buf, buflen, "type=%d", port->p_type);