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>
38 #include <machine/bus.h>
39 #include <machine/resource.h>
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcivar.h>
45 #include <dev/puc/puc_bus.h>
46 #include <dev/puc/puc_cfg.h>
47 #include <dev/puc/puc_bfe.h>
52 struct puc_bar *p_bar;
53 struct resource *p_rres;
54 struct resource *p_ires;
62 serdev_intr_t *p_ihsrc[PUC_ISRCCNT];
68 devclass_t puc_devclass;
69 const char puc_driver_name[] = "puc";
71 static MALLOC_DEFINE(M_PUC, "PUC", "PUC driver");
74 puc_get_bar(struct puc_softc *sc, int rid)
81 /* Find the BAR entry with the given RID. */
83 while (i < PUC_PCI_BARS && sc->sc_bar[i].b_rid != rid)
86 return (&sc->sc_bar[i]);
88 /* Not found. If we're looking for an unused entry, return NULL. */
92 /* Get an unused entry for us to fill. */
93 bar = puc_get_bar(sc, -1);
97 bar->b_type = SYS_RES_IOPORT;
98 bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
99 &bar->b_rid, RF_ACTIVE);
100 if (bar->b_res == NULL) {
102 bar->b_type = SYS_RES_MEMORY;
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) {
111 /* Update our managed space. */
112 rm = (bar->b_type == SYS_RES_IOPORT) ? &sc->sc_ioport : &sc->sc_iomem;
113 start = rman_get_start(bar->b_res);
114 end = rman_get_end(bar->b_res);
115 error = rman_manage_region(rm, start, end);
117 bus_release_resource(sc->sc_dev, bar->b_type, bar->b_rid,
130 struct puc_port *port;
131 struct puc_softc *sc = arg;
132 u_long ds, dev, devs;
133 int i, idx, ipend, isrc, nints;
139 * Obtain the set of devices with pending interrupts.
141 devs = sc->sc_serdevs;
142 if (sc->sc_ilr == PUC_ILR_DIGI) {
144 while (devs & (0xfful << idx)) {
145 ilr = ~bus_read_1(sc->sc_port[idx].p_rres, 7);
146 devs &= ~0ul ^ ((u_long)ilr << idx);
149 } else if (sc->sc_ilr == PUC_ILR_QUATECH) {
151 * Don't trust the value if it's the same as the option
152 * register. It may mean that the ILR is not active and
153 * we're reading the option register instead. This may
154 * lead to false positives on 8-port boards.
156 ilr = bus_read_1(sc->sc_port[0].p_rres, 7);
157 if (ilr != (sc->sc_cfg_data & 0xff))
164 * Obtain the set of interrupt sources from those devices
165 * that have pending interrupts.
171 while ((ds & dev) == 0UL)
174 port = &sc->sc_port[idx];
175 port->p_ipend = SERDEV_IPEND(port->p_dev);
176 ipend |= port->p_ipend;
181 i = 0, isrc = SER_INT_OVERRUN;
183 while (i < PUC_ISRCCNT && !(ipend & isrc))
185 KASSERT(i < PUC_ISRCCNT, ("%s", __func__));
190 while ((ds & dev) == 0UL)
193 port = &sc->sc_port[idx];
194 if (!(port->p_ipend & isrc))
196 if (port->p_ihsrc[i] != NULL)
197 (*port->p_ihsrc[i])(port->p_iharg);
203 return ((nints > 0) ? FILTER_HANDLED : FILTER_STRAY);
207 puc_bfe_attach(device_t dev)
211 struct puc_port *port;
212 struct puc_softc *sc;
215 bus_addr_t ofs, start;
217 bus_space_handle_t bsh;
221 sc = device_get_softc(dev);
223 for (idx = 0; idx < PUC_PCI_BARS; idx++)
224 sc->sc_bar[idx].b_rid = -1;
227 sc->sc_ioport.rm_type = RMAN_ARRAY;
228 error = rman_init(&sc->sc_ioport);
230 sc->sc_iomem.rm_type = RMAN_ARRAY;
231 error = rman_init(&sc->sc_iomem);
233 sc->sc_irq.rm_type = RMAN_ARRAY;
234 error = rman_init(&sc->sc_irq);
237 rman_fini(&sc->sc_iomem);
239 rman_fini(&sc->sc_ioport);
244 snprintf(buffer, sizeof(buffer), "%s I/O port mapping",
245 device_get_nameunit(dev));
246 sc->sc_ioport.rm_descr = strdup(buffer, M_PUC);
247 snprintf(buffer, sizeof(buffer), "%s I/O memory mapping",
248 device_get_nameunit(dev));
249 sc->sc_iomem.rm_descr = strdup(buffer, M_PUC);
250 snprintf(buffer, sizeof(buffer), "%s port numbers",
251 device_get_nameunit(dev));
252 sc->sc_irq.rm_descr = strdup(buffer, M_PUC);
254 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
255 KASSERT(error == 0, ("%s %d", __func__, __LINE__));
256 sc->sc_nports = (int)res;
257 sc->sc_port = malloc(sc->sc_nports * sizeof(struct puc_port),
258 M_PUC, M_WAITOK|M_ZERO);
260 error = rman_manage_region(&sc->sc_irq, 1, sc->sc_nports);
264 error = puc_config(sc, PUC_CFG_SETUP, 0, &res);
268 for (idx = 0; idx < sc->sc_nports; idx++) {
269 port = &sc->sc_port[idx];
270 port->p_nr = idx + 1;
271 error = puc_config(sc, PUC_CFG_GET_TYPE, idx, &res);
275 error = puc_config(sc, PUC_CFG_GET_RID, idx, &res);
278 bar = puc_get_bar(sc, res);
284 start = rman_get_start(bar->b_res);
285 error = puc_config(sc, PUC_CFG_GET_OFS, idx, &res);
289 error = puc_config(sc, PUC_CFG_GET_LEN, idx, &res);
293 rm = (bar->b_type == SYS_RES_IOPORT)
294 ? &sc->sc_ioport: &sc->sc_iomem;
295 port->p_rres = rman_reserve_resource(rm, start + ofs,
296 start + ofs + size - 1, size, 0, NULL);
297 if (port->p_rres != NULL) {
298 bsh = rman_get_bushandle(bar->b_res);
299 bst = rman_get_bustag(bar->b_res);
300 bus_space_subregion(bst, bsh, ofs, size, &bsh);
301 rman_set_bushandle(port->p_rres, bsh);
302 rman_set_bustag(port->p_rres, bst);
304 port->p_ires = rman_reserve_resource(&sc->sc_irq, port->p_nr,
305 port->p_nr, 1, 0, NULL);
306 if (port->p_ires == NULL) {
310 error = puc_config(sc, PUC_CFG_GET_CLOCK, idx, &res);
315 port->p_dev = device_add_child(dev, NULL, -1);
316 if (port->p_dev != NULL)
317 device_set_ivars(port->p_dev, (void *)port);
320 error = puc_config(sc, PUC_CFG_GET_ILR, 0, &res);
324 if (bootverbose && sc->sc_ilr != 0)
325 device_printf(dev, "using interrupt latch register\n");
328 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
329 RF_ACTIVE|RF_SHAREABLE);
330 if (sc->sc_ires != NULL) {
331 error = bus_setup_intr(dev, sc->sc_ires,
332 INTR_TYPE_TTY, puc_intr, NULL, sc, &sc->sc_icookie);
334 error = bus_setup_intr(dev, sc->sc_ires,
335 INTR_TYPE_TTY | INTR_MPSAFE, NULL,
336 (driver_intr_t *)puc_intr, sc, &sc->sc_icookie);
341 device_printf(dev, "could not activate interrupt\n");
342 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
347 if (sc->sc_ires == NULL) {
348 /* XXX no interrupt resource. Force polled mode. */
352 /* Probe and attach our children. */
353 for (idx = 0; idx < sc->sc_nports; idx++) {
354 port = &sc->sc_port[idx];
355 if (port->p_dev == NULL)
357 error = device_probe_and_attach(port->p_dev);
359 device_delete_child(dev, port->p_dev);
365 * If there are no serdev devices, then our interrupt handler
366 * will do nothing. Tear it down.
368 if (sc->sc_serdevs == 0UL)
369 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
374 for (idx = 0; idx < sc->sc_nports; idx++) {
375 port = &sc->sc_port[idx];
376 if (port->p_dev != NULL)
377 device_delete_child(dev, port->p_dev);
378 if (port->p_rres != NULL)
379 rman_release_resource(port->p_rres);
380 if (port->p_ires != NULL)
381 rman_release_resource(port->p_ires);
383 for (idx = 0; idx < PUC_PCI_BARS; idx++) {
384 bar = &sc->sc_bar[idx];
385 if (bar->b_res != NULL)
386 bus_release_resource(sc->sc_dev, bar->b_type,
387 bar->b_rid, bar->b_res);
389 rman_fini(&sc->sc_irq);
390 free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
391 rman_fini(&sc->sc_iomem);
392 free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
393 rman_fini(&sc->sc_ioport);
394 free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
395 free(sc->sc_port, M_PUC);
400 puc_bfe_detach(device_t dev)
403 struct puc_port *port;
404 struct puc_softc *sc;
407 sc = device_get_softc(dev);
409 /* Detach our children. */
411 for (idx = 0; idx < sc->sc_nports; idx++) {
412 port = &sc->sc_port[idx];
413 if (port->p_dev == NULL)
415 if (device_detach(port->p_dev) == 0) {
416 device_delete_child(dev, port->p_dev);
417 if (port->p_rres != NULL)
418 rman_release_resource(port->p_rres);
419 if (port->p_ires != NULL)
420 rman_release_resource(port->p_ires);
427 if (sc->sc_serdevs != 0UL)
428 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
429 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, sc->sc_ires);
431 for (idx = 0; idx < PUC_PCI_BARS; idx++) {
432 bar = &sc->sc_bar[idx];
433 if (bar->b_res != NULL)
434 bus_release_resource(sc->sc_dev, bar->b_type,
435 bar->b_rid, bar->b_res);
438 rman_fini(&sc->sc_irq);
439 free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
440 rman_fini(&sc->sc_iomem);
441 free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
442 rman_fini(&sc->sc_ioport);
443 free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
444 free(sc->sc_port, M_PUC);
449 puc_bfe_probe(device_t dev, const struct puc_cfg *cfg)
451 struct puc_softc *sc;
455 sc = device_get_softc(dev);
459 /* We don't attach to single-port serial cards. */
460 if (cfg->ports == PUC_PORT_1S || cfg->ports == PUC_PORT_1P)
462 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
465 error = puc_config(sc, PUC_CFG_GET_DESC, 0, &res);
469 device_set_desc(dev, (const char *)res);
470 return (BUS_PROBE_DEFAULT);
474 puc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
475 u_long start, u_long end, u_long count, u_int flags)
477 struct puc_port *port;
478 struct resource *res;
479 device_t assigned, originator;
482 /* Get our immediate child. */
484 while (child != NULL && device_get_parent(child) != dev)
485 child = device_get_parent(child);
489 port = device_get_ivars(child);
490 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
492 if (rid == NULL || *rid != 0)
495 /* We only support default allocations. */
496 if (start != 0UL || end != ~0UL)
499 if (type == port->p_bar->b_type)
501 else if (type == SYS_RES_IRQ)
509 assigned = rman_get_device(res);
510 if (assigned == NULL) /* Not allocated */
511 rman_set_device(res, originator);
512 else if (assigned != originator)
515 if (flags & RF_ACTIVE) {
516 error = rman_activate_resource(res);
518 if (assigned == NULL)
519 rman_set_device(res, NULL);
528 puc_bus_release_resource(device_t dev, device_t child, int type, int rid,
529 struct resource *res)
531 struct puc_port *port;
534 /* Get our immediate child. */
536 while (child != NULL && device_get_parent(child) != dev)
537 child = device_get_parent(child);
541 port = device_get_ivars(child);
542 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
544 if (rid != 0 || res == NULL)
547 if (type == port->p_bar->b_type) {
548 if (res != port->p_rres)
550 } else if (type == SYS_RES_IRQ) {
551 if (res != port->p_ires)
558 if (rman_get_device(res) != originator)
560 if (rman_get_flags(res) & RF_ACTIVE)
561 rman_deactivate_resource(res);
562 rman_set_device(res, NULL);
567 puc_bus_get_resource(device_t dev, device_t child, int type, int rid,
568 u_long *startp, u_long *countp)
570 struct puc_port *port;
571 struct resource *res;
574 /* Get our immediate child. */
575 while (child != NULL && device_get_parent(child) != dev)
576 child = device_get_parent(child);
580 port = device_get_ivars(child);
581 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
583 if (type == port->p_bar->b_type)
585 else if (type == SYS_RES_IRQ)
590 if (rid != 0 || res == NULL)
593 start = rman_get_start(res);
597 *countp = rman_get_end(res) - start + 1;
602 puc_bus_setup_intr(device_t dev, device_t child, struct resource *res,
603 int flags, driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
605 struct puc_port *port;
606 struct puc_softc *sc;
610 sc = device_get_softc(dev);
612 /* Get our immediate child. */
614 while (child != NULL && device_get_parent(child) != dev)
615 child = device_get_parent(child);
619 port = device_get_ivars(child);
620 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
622 if (cookiep == NULL || res != port->p_ires)
624 /* We demand that serdev devices use filter_only interrupts. */
625 if (port->p_type == PUC_TYPE_SERIAL && ihand != NULL)
627 if (rman_get_device(port->p_ires) != originator)
631 * Have non-serdev ports handled by the bus implementation. It
632 * supports multiple handlers for a single interrupt as it is,
633 * so we wouldn't add value if we did it ourselves.
636 if (port->p_type == PUC_TYPE_SERIAL) {
637 i = 0, isrc = SER_INT_OVERRUN;
638 while (i < PUC_ISRCCNT) {
639 port->p_ihsrc[i] = SERDEV_IHAND(originator, isrc);
640 if (port->p_ihsrc[i] != NULL)
646 return (BUS_SETUP_INTR(device_get_parent(dev), originator,
647 sc->sc_ires, flags, filt, ihand, arg, cookiep));
649 sc->sc_serdevs |= 1UL << (port->p_nr - 1);
659 puc_bus_teardown_intr(device_t dev, device_t child, struct resource *res,
662 struct puc_port *port;
663 struct puc_softc *sc;
667 sc = device_get_softc(dev);
669 /* Get our immediate child. */
671 while (child != NULL && device_get_parent(child) != dev)
672 child = device_get_parent(child);
676 port = device_get_ivars(child);
677 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
679 if (res != port->p_ires)
681 if (rman_get_device(port->p_ires) != originator)
684 if (!port->p_hasintr)
685 return (BUS_TEARDOWN_INTR(device_get_parent(dev), originator,
686 sc->sc_ires, cookie));
692 port->p_iharg = NULL;
694 for (i = 0; i < PUC_ISRCCNT; i++)
695 port->p_ihsrc[i] = NULL;
701 puc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
703 struct puc_port *port;
705 /* Get our immediate child. */
706 while (child != NULL && device_get_parent(child) != dev)
707 child = device_get_parent(child);
711 port = device_get_ivars(child);
712 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
719 *result = port->p_rclk;
722 *result = port->p_type;
731 puc_bus_print_child(device_t dev, device_t child)
733 struct puc_port *port;
736 port = device_get_ivars(child);
739 retval += bus_print_child_header(dev, child);
740 retval += printf(" at port %d", port->p_nr);
741 retval += bus_print_child_footer(dev, child);
747 puc_bus_child_location_str(device_t dev, device_t child, char *buf,
750 struct puc_port *port;
752 port = device_get_ivars(child);
753 snprintf(buf, buflen, "port=%d", port->p_nr);
758 puc_bus_child_pnpinfo_str(device_t dev, device_t child, char *buf,
761 struct puc_port *port;
763 port = device_get_ivars(child);
764 snprintf(buf, buflen, "type=%d", port->p_type);