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 | CTLFLAG_MPSAFE, 0,
77 "puc(9) driver configuration");
80 puc_get_bar(struct puc_softc *sc, int rid)
84 rman_res_t end, start;
87 /* Find the BAR entry with the given RID. */
89 while (i < PUC_PCI_BARS && sc->sc_bar[i].b_rid != rid)
92 return (&sc->sc_bar[i]);
94 /* Not found. If we're looking for an unused entry, return NULL. */
98 /* Get an unused entry for us to fill. */
99 bar = puc_get_bar(sc, -1);
103 bar->b_type = SYS_RES_IOPORT;
104 bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
105 &bar->b_rid, RF_ACTIVE);
106 if (bar->b_res == NULL) {
108 bar->b_type = SYS_RES_MEMORY;
109 bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
110 &bar->b_rid, RF_ACTIVE);
111 if (bar->b_res == NULL) {
117 /* Update our managed space. */
118 rm = (bar->b_type == SYS_RES_IOPORT) ? &sc->sc_ioport : &sc->sc_iomem;
119 start = rman_get_start(bar->b_res);
120 end = rman_get_end(bar->b_res);
121 error = rman_manage_region(rm, start, end);
123 bus_release_resource(sc->sc_dev, bar->b_type, bar->b_rid,
136 struct puc_port *port;
137 struct puc_softc *sc = arg;
138 u_long ds, dev, devs;
139 int i, idx, ipend, isrc, nints;
145 * Obtain the set of devices with pending interrupts.
147 devs = sc->sc_serdevs;
148 if (sc->sc_ilr == PUC_ILR_DIGI) {
150 while (devs & (0xfful << idx)) {
151 ilr = ~bus_read_1(sc->sc_port[idx].p_rres, 7);
152 devs &= ~0ul ^ ((u_long)ilr << idx);
155 } else if (sc->sc_ilr == PUC_ILR_QUATECH) {
157 * Don't trust the value if it's the same as the option
158 * register. It may mean that the ILR is not active and
159 * we're reading the option register instead. This may
160 * lead to false positives on 8-port boards.
162 ilr = bus_read_1(sc->sc_port[0].p_rres, 7);
163 if (ilr != (sc->sc_cfg_data & 0xff))
170 * Obtain the set of interrupt sources from those devices
171 * that have pending interrupts.
177 while ((ds & dev) == 0UL)
180 port = &sc->sc_port[idx];
181 port->p_ipend = SERDEV_IPEND(port->p_dev);
182 ipend |= port->p_ipend;
187 i = 0, isrc = SER_INT_OVERRUN;
189 while (i < PUC_ISRCCNT && !(ipend & isrc))
191 KASSERT(i < PUC_ISRCCNT, ("%s", __func__));
196 while ((ds & dev) == 0UL)
199 port = &sc->sc_port[idx];
200 if (!(port->p_ipend & isrc))
202 if (port->p_ihsrc[i] != NULL)
203 (*port->p_ihsrc[i])(port->p_iharg);
209 return ((nints > 0) ? FILTER_HANDLED : FILTER_STRAY);
213 puc_bfe_attach(device_t dev)
217 struct puc_port *port;
218 struct puc_softc *sc;
221 bus_addr_t ofs, start;
223 bus_space_handle_t bsh;
227 sc = device_get_softc(dev);
229 for (idx = 0; idx < PUC_PCI_BARS; idx++)
230 sc->sc_bar[idx].b_rid = -1;
233 sc->sc_ioport.rm_type = RMAN_ARRAY;
234 error = rman_init(&sc->sc_ioport);
236 sc->sc_iomem.rm_type = RMAN_ARRAY;
237 error = rman_init(&sc->sc_iomem);
239 sc->sc_irq.rm_type = RMAN_ARRAY;
240 error = rman_init(&sc->sc_irq);
243 rman_fini(&sc->sc_iomem);
245 rman_fini(&sc->sc_ioport);
250 snprintf(buffer, sizeof(buffer), "%s I/O port mapping",
251 device_get_nameunit(dev));
252 sc->sc_ioport.rm_descr = strdup(buffer, M_PUC);
253 snprintf(buffer, sizeof(buffer), "%s I/O memory mapping",
254 device_get_nameunit(dev));
255 sc->sc_iomem.rm_descr = strdup(buffer, M_PUC);
256 snprintf(buffer, sizeof(buffer), "%s port numbers",
257 device_get_nameunit(dev));
258 sc->sc_irq.rm_descr = strdup(buffer, M_PUC);
260 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
261 KASSERT(error == 0, ("%s %d", __func__, __LINE__));
262 sc->sc_nports = (int)res;
263 sc->sc_port = malloc(sc->sc_nports * sizeof(struct puc_port),
264 M_PUC, M_WAITOK|M_ZERO);
266 error = rman_manage_region(&sc->sc_irq, 1, sc->sc_nports);
270 error = puc_config(sc, PUC_CFG_SETUP, 0, &res);
274 for (idx = 0; idx < sc->sc_nports; idx++) {
275 port = &sc->sc_port[idx];
276 port->p_nr = idx + 1;
277 error = puc_config(sc, PUC_CFG_GET_TYPE, idx, &res);
281 error = puc_config(sc, PUC_CFG_GET_RID, idx, &res);
284 bar = puc_get_bar(sc, res);
290 start = rman_get_start(bar->b_res);
291 error = puc_config(sc, PUC_CFG_GET_OFS, idx, &res);
295 error = puc_config(sc, PUC_CFG_GET_LEN, idx, &res);
299 rm = (bar->b_type == SYS_RES_IOPORT)
300 ? &sc->sc_ioport: &sc->sc_iomem;
301 port->p_rres = rman_reserve_resource(rm, start + ofs,
302 start + ofs + size - 1, size, 0, NULL);
303 if (port->p_rres != NULL) {
304 bsh = rman_get_bushandle(bar->b_res);
305 bst = rman_get_bustag(bar->b_res);
306 bus_space_subregion(bst, bsh, ofs, size, &bsh);
307 rman_set_bushandle(port->p_rres, bsh);
308 rman_set_bustag(port->p_rres, bst);
310 port->p_ires = rman_reserve_resource(&sc->sc_irq, port->p_nr,
311 port->p_nr, 1, 0, NULL);
312 if (port->p_ires == NULL) {
316 error = puc_config(sc, PUC_CFG_GET_CLOCK, idx, &res);
321 port->p_dev = device_add_child(dev, NULL, -1);
322 if (port->p_dev != NULL)
323 device_set_ivars(port->p_dev, (void *)port);
326 error = puc_config(sc, PUC_CFG_GET_ILR, 0, &res);
330 if (bootverbose && sc->sc_ilr != 0)
331 device_printf(dev, "using interrupt latch register\n");
333 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
334 RF_ACTIVE|RF_SHAREABLE);
335 if (sc->sc_ires != NULL) {
336 error = bus_setup_intr(dev, sc->sc_ires,
337 INTR_TYPE_TTY, puc_intr, NULL, sc, &sc->sc_icookie);
339 error = bus_setup_intr(dev, sc->sc_ires,
340 INTR_TYPE_TTY | INTR_MPSAFE, NULL,
341 (driver_intr_t *)puc_intr, sc, &sc->sc_icookie);
346 device_printf(dev, "could not activate interrupt\n");
347 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
352 if (sc->sc_ires == NULL) {
353 /* XXX no interrupt resource. Force polled mode. */
357 /* Probe and attach our children. */
358 for (idx = 0; idx < sc->sc_nports; idx++) {
359 port = &sc->sc_port[idx];
360 if (port->p_dev == NULL)
362 error = device_probe_and_attach(port->p_dev);
364 device_delete_child(dev, port->p_dev);
370 * If there are no serdev devices, then our interrupt handler
371 * will do nothing. Tear it down.
373 if (sc->sc_serdevs == 0UL)
374 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
379 for (idx = 0; idx < sc->sc_nports; idx++) {
380 port = &sc->sc_port[idx];
381 if (port->p_dev != NULL)
382 device_delete_child(dev, port->p_dev);
383 if (port->p_rres != NULL)
384 rman_release_resource(port->p_rres);
385 if (port->p_ires != NULL)
386 rman_release_resource(port->p_ires);
388 for (idx = 0; idx < PUC_PCI_BARS; idx++) {
389 bar = &sc->sc_bar[idx];
390 if (bar->b_res != NULL)
391 bus_release_resource(sc->sc_dev, bar->b_type,
392 bar->b_rid, bar->b_res);
394 rman_fini(&sc->sc_irq);
395 free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
396 rman_fini(&sc->sc_iomem);
397 free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
398 rman_fini(&sc->sc_ioport);
399 free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
400 free(sc->sc_port, M_PUC);
405 puc_bfe_detach(device_t dev)
408 struct puc_port *port;
409 struct puc_softc *sc;
412 sc = device_get_softc(dev);
414 /* Detach our children. */
416 for (idx = 0; idx < sc->sc_nports; idx++) {
417 port = &sc->sc_port[idx];
418 if (port->p_dev == NULL)
420 if (device_delete_child(dev, port->p_dev) == 0) {
421 if (port->p_rres != NULL)
422 rman_release_resource(port->p_rres);
423 if (port->p_ires != NULL)
424 rman_release_resource(port->p_ires);
431 if (sc->sc_serdevs != 0UL)
432 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
433 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, sc->sc_ires);
435 for (idx = 0; idx < PUC_PCI_BARS; idx++) {
436 bar = &sc->sc_bar[idx];
437 if (bar->b_res != NULL)
438 bus_release_resource(sc->sc_dev, bar->b_type,
439 bar->b_rid, bar->b_res);
442 rman_fini(&sc->sc_irq);
443 free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
444 rman_fini(&sc->sc_iomem);
445 free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
446 rman_fini(&sc->sc_ioport);
447 free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
448 free(sc->sc_port, M_PUC);
453 puc_bfe_probe(device_t dev, const struct puc_cfg *cfg)
455 struct puc_softc *sc;
459 sc = device_get_softc(dev);
463 /* We don't attach to single-port serial cards. */
464 if (cfg->ports == PUC_PORT_1S || cfg->ports == PUC_PORT_1P)
466 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
469 error = puc_config(sc, PUC_CFG_GET_DESC, 0, &res);
473 device_set_desc(dev, (const char *)res);
474 return (BUS_PROBE_DEFAULT);
478 puc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
479 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
481 struct puc_port *port;
482 struct resource *res;
483 device_t assigned, originator;
486 /* Get our immediate child. */
488 while (child != NULL && device_get_parent(child) != dev)
489 child = device_get_parent(child);
493 port = device_get_ivars(child);
494 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
496 if (rid == NULL || *rid != 0)
499 /* We only support default allocations. */
500 if (!RMAN_IS_DEFAULT_RANGE(start, end))
503 if (type == port->p_bar->b_type)
505 else if (type == SYS_RES_IRQ)
513 assigned = rman_get_device(res);
514 if (assigned == NULL) /* Not allocated */
515 rman_set_device(res, originator);
516 else if (assigned != originator)
519 if (flags & RF_ACTIVE) {
520 error = rman_activate_resource(res);
522 if (assigned == NULL)
523 rman_set_device(res, NULL);
532 puc_bus_release_resource(device_t dev, device_t child, int type, int rid,
533 struct resource *res)
535 struct puc_port *port;
538 /* Get our immediate child. */
540 while (child != NULL && device_get_parent(child) != dev)
541 child = device_get_parent(child);
545 port = device_get_ivars(child);
546 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
548 if (rid != 0 || res == NULL)
551 if (type == port->p_bar->b_type) {
552 if (res != port->p_rres)
554 } else if (type == SYS_RES_IRQ) {
555 if (res != port->p_ires)
562 if (rman_get_device(res) != originator)
564 if (rman_get_flags(res) & RF_ACTIVE)
565 rman_deactivate_resource(res);
566 rman_set_device(res, NULL);
571 puc_bus_get_resource(device_t dev, device_t child, int type, int rid,
572 rman_res_t *startp, rman_res_t *countp)
574 struct puc_port *port;
575 struct resource *res;
578 /* Get our immediate child. */
579 while (child != NULL && device_get_parent(child) != dev)
580 child = device_get_parent(child);
584 port = device_get_ivars(child);
585 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
587 if (type == port->p_bar->b_type)
589 else if (type == SYS_RES_IRQ)
594 if (rid != 0 || res == NULL)
597 start = rman_get_start(res);
601 *countp = rman_get_end(res) - start + 1;
606 puc_bus_setup_intr(device_t dev, device_t child, struct resource *res,
607 int flags, driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
609 struct puc_port *port;
610 struct puc_softc *sc;
614 sc = device_get_softc(dev);
616 /* Get our immediate child. */
618 while (child != NULL && device_get_parent(child) != dev)
619 child = device_get_parent(child);
623 port = device_get_ivars(child);
624 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
626 if (cookiep == NULL || res != port->p_ires)
628 /* We demand that serdev devices use filter_only interrupts. */
629 if (port->p_type == PUC_TYPE_SERIAL && ihand != NULL)
631 if (rman_get_device(port->p_ires) != originator)
635 * Have non-serdev ports handled by the bus implementation. It
636 * supports multiple handlers for a single interrupt as it is,
637 * so we wouldn't add value if we did it ourselves.
640 if (port->p_type == PUC_TYPE_SERIAL) {
641 i = 0, isrc = SER_INT_OVERRUN;
642 while (i < PUC_ISRCCNT) {
643 port->p_ihsrc[i] = SERDEV_IHAND(originator, isrc);
644 if (port->p_ihsrc[i] != NULL)
650 return (BUS_SETUP_INTR(device_get_parent(dev), originator,
651 sc->sc_ires, flags, filt, ihand, arg, cookiep));
653 sc->sc_serdevs |= 1UL << (port->p_nr - 1);
663 puc_bus_teardown_intr(device_t dev, device_t child, struct resource *res,
666 struct puc_port *port;
667 struct puc_softc *sc;
671 sc = device_get_softc(dev);
673 /* Get our immediate child. */
675 while (child != NULL && device_get_parent(child) != dev)
676 child = device_get_parent(child);
680 port = device_get_ivars(child);
681 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
683 if (res != port->p_ires)
685 if (rman_get_device(port->p_ires) != originator)
688 if (!port->p_hasintr)
689 return (BUS_TEARDOWN_INTR(device_get_parent(dev), originator,
690 sc->sc_ires, cookie));
696 port->p_iharg = NULL;
698 for (i = 0; i < PUC_ISRCCNT; i++)
699 port->p_ihsrc[i] = NULL;
705 puc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
707 struct puc_port *port;
709 /* Get our immediate child. */
710 while (child != NULL && device_get_parent(child) != dev)
711 child = device_get_parent(child);
715 port = device_get_ivars(child);
716 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
723 *result = port->p_rclk;
726 *result = port->p_type;
735 puc_bus_print_child(device_t dev, device_t child)
737 struct puc_port *port;
740 port = device_get_ivars(child);
743 retval += bus_print_child_header(dev, child);
744 retval += printf(" at port %d", port->p_nr);
745 retval += bus_print_child_footer(dev, child);
751 puc_bus_child_location_str(device_t dev, device_t child, char *buf,
754 struct puc_port *port;
756 port = device_get_ivars(child);
757 snprintf(buf, buflen, "port=%d", port->p_nr);
762 puc_bus_child_pnpinfo_str(device_t dev, device_t child, char *buf,
765 struct puc_port *port;
767 port = device_get_ivars(child);
768 snprintf(buf, buflen, "type=%d", port->p_type);