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 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;
133 int i, idx, ipend, isrc;
136 devs = sc->sc_serdevs;
137 if (sc->sc_ilr == PUC_ILR_DIGI) {
139 while (devs & (0xfful << idx)) {
140 ilr = ~bus_read_1(sc->sc_port[idx].p_rres, 7);
141 devs &= ~0ul ^ ((u_long)ilr << idx);
144 } else if (sc->sc_ilr == PUC_ILR_QUATECH) {
146 * Don't trust the value if it's the same as the option
147 * register. It may mean that the ILR is not active and
148 * we're reading the option register instead. This may
149 * lead to false positives on 8-port boards.
151 ilr = bus_read_1(sc->sc_port[0].p_rres, 7);
152 if (ilr != (sc->sc_cfg_data & 0xff))
158 while (devs != 0UL) {
159 while ((devs & dev) == 0UL)
162 port = &sc->sc_port[idx];
163 port->p_ipend = SERDEV_IPEND(port->p_dev);
164 ipend |= port->p_ipend;
167 i = 0, isrc = SER_INT_OVERRUN;
169 while (i < PUC_ISRCCNT && !(ipend & isrc))
171 KASSERT(i < PUC_ISRCCNT, ("%s", __func__));
174 devs = sc->sc_serdevs;
175 while (devs != 0UL) {
176 while ((devs & dev) == 0UL)
179 port = &sc->sc_port[idx];
180 if (!(port->p_ipend & isrc))
182 if (port->p_ihsrc[i] != NULL)
183 (*port->p_ihsrc[i])(port->p_iharg);
185 return (FILTER_HANDLED);
187 return (FILTER_STRAY);
191 puc_bfe_attach(device_t dev)
195 struct puc_port *port;
196 struct puc_softc *sc;
199 bus_addr_t ofs, start;
201 bus_space_handle_t bsh;
205 sc = device_get_softc(dev);
207 for (idx = 0; idx < PUC_PCI_BARS; idx++)
208 sc->sc_bar[idx].b_rid = -1;
211 sc->sc_ioport.rm_type = RMAN_ARRAY;
212 error = rman_init(&sc->sc_ioport);
214 sc->sc_iomem.rm_type = RMAN_ARRAY;
215 error = rman_init(&sc->sc_iomem);
217 sc->sc_irq.rm_type = RMAN_ARRAY;
218 error = rman_init(&sc->sc_irq);
221 rman_fini(&sc->sc_iomem);
223 rman_fini(&sc->sc_ioport);
228 snprintf(buffer, sizeof(buffer), "%s I/O port mapping",
229 device_get_nameunit(dev));
230 sc->sc_ioport.rm_descr = strdup(buffer, M_PUC);
231 snprintf(buffer, sizeof(buffer), "%s I/O memory mapping",
232 device_get_nameunit(dev));
233 sc->sc_iomem.rm_descr = strdup(buffer, M_PUC);
234 snprintf(buffer, sizeof(buffer), "%s port numbers",
235 device_get_nameunit(dev));
236 sc->sc_irq.rm_descr = strdup(buffer, M_PUC);
238 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
239 KASSERT(error == 0, ("%s %d", __func__, __LINE__));
240 sc->sc_nports = (int)res;
241 sc->sc_port = malloc(sc->sc_nports * sizeof(struct puc_port),
242 M_PUC, M_WAITOK|M_ZERO);
244 error = rman_manage_region(&sc->sc_irq, 1, sc->sc_nports);
248 error = puc_config(sc, PUC_CFG_SETUP, 0, &res);
252 for (idx = 0; idx < sc->sc_nports; idx++) {
253 port = &sc->sc_port[idx];
254 port->p_nr = idx + 1;
255 error = puc_config(sc, PUC_CFG_GET_TYPE, idx, &res);
259 error = puc_config(sc, PUC_CFG_GET_RID, idx, &res);
262 bar = puc_get_bar(sc, res);
268 start = rman_get_start(bar->b_res);
269 error = puc_config(sc, PUC_CFG_GET_OFS, idx, &res);
273 error = puc_config(sc, PUC_CFG_GET_LEN, idx, &res);
277 rm = (bar->b_type == SYS_RES_IOPORT)
278 ? &sc->sc_ioport: &sc->sc_iomem;
279 port->p_rres = rman_reserve_resource(rm, start + ofs,
280 start + ofs + size - 1, size, 0, NULL);
281 if (port->p_rres != NULL) {
282 bsh = rman_get_bushandle(bar->b_res);
283 bst = rman_get_bustag(bar->b_res);
284 bus_space_subregion(bst, bsh, ofs, size, &bsh);
285 rman_set_bushandle(port->p_rres, bsh);
286 rman_set_bustag(port->p_rres, bst);
288 port->p_ires = rman_reserve_resource(&sc->sc_irq, port->p_nr,
289 port->p_nr, 1, 0, NULL);
290 if (port->p_ires == NULL) {
294 error = puc_config(sc, PUC_CFG_GET_CLOCK, idx, &res);
299 port->p_dev = device_add_child(dev, NULL, -1);
300 if (port->p_dev != NULL)
301 device_set_ivars(port->p_dev, (void *)port);
304 error = puc_config(sc, PUC_CFG_GET_ILR, 0, &res);
308 if (bootverbose && sc->sc_ilr != 0)
309 device_printf(dev, "using interrupt latch register\n");
312 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
313 RF_ACTIVE|RF_SHAREABLE);
314 if (sc->sc_ires != NULL) {
315 error = bus_setup_intr(dev, sc->sc_ires,
316 INTR_TYPE_TTY, puc_intr, NULL, sc, &sc->sc_icookie);
318 error = bus_setup_intr(dev, sc->sc_ires,
319 INTR_TYPE_TTY | INTR_MPSAFE, NULL,
320 (driver_intr_t *)puc_intr, sc, &sc->sc_icookie);
325 device_printf(dev, "could not activate interrupt\n");
326 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
331 if (sc->sc_ires == NULL) {
332 /* XXX no interrupt resource. Force polled mode. */
336 /* Probe and attach our children. */
337 for (idx = 0; idx < sc->sc_nports; idx++) {
338 port = &sc->sc_port[idx];
339 if (port->p_dev == NULL)
341 error = device_probe_and_attach(port->p_dev);
343 device_delete_child(dev, port->p_dev);
349 * If there are no serdev devices, then our interrupt handler
350 * will do nothing. Tear it down.
352 if (sc->sc_serdevs == 0UL)
353 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
358 for (idx = 0; idx < sc->sc_nports; idx++) {
359 port = &sc->sc_port[idx];
360 if (port->p_dev != NULL)
361 device_delete_child(dev, port->p_dev);
362 if (port->p_rres != NULL)
363 rman_release_resource(port->p_rres);
364 if (port->p_ires != NULL)
365 rman_release_resource(port->p_ires);
367 for (idx = 0; idx < PUC_PCI_BARS; idx++) {
368 bar = &sc->sc_bar[idx];
369 if (bar->b_res != NULL)
370 bus_release_resource(sc->sc_dev, bar->b_type,
371 bar->b_rid, bar->b_res);
373 rman_fini(&sc->sc_irq);
374 free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
375 rman_fini(&sc->sc_iomem);
376 free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
377 rman_fini(&sc->sc_ioport);
378 free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
379 free(sc->sc_port, M_PUC);
384 puc_bfe_detach(device_t dev)
387 struct puc_port *port;
388 struct puc_softc *sc;
391 sc = device_get_softc(dev);
393 /* Detach our children. */
395 for (idx = 0; idx < sc->sc_nports; idx++) {
396 port = &sc->sc_port[idx];
397 if (port->p_dev == NULL)
399 if (device_detach(port->p_dev) == 0) {
400 device_delete_child(dev, port->p_dev);
401 if (port->p_rres != NULL)
402 rman_release_resource(port->p_rres);
403 if (port->p_ires != NULL)
404 rman_release_resource(port->p_ires);
411 if (sc->sc_serdevs != 0UL)
412 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
413 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, sc->sc_ires);
415 for (idx = 0; idx < PUC_PCI_BARS; idx++) {
416 bar = &sc->sc_bar[idx];
417 if (bar->b_res != NULL)
418 bus_release_resource(sc->sc_dev, bar->b_type,
419 bar->b_rid, bar->b_res);
422 rman_fini(&sc->sc_irq);
423 free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
424 rman_fini(&sc->sc_iomem);
425 free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
426 rman_fini(&sc->sc_ioport);
427 free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
428 free(sc->sc_port, M_PUC);
433 puc_bfe_probe(device_t dev, const struct puc_cfg *cfg)
435 struct puc_softc *sc;
439 sc = device_get_softc(dev);
443 /* We don't attach to single-port serial cards. */
444 if (cfg->ports == PUC_PORT_1S || cfg->ports == PUC_PORT_1P)
446 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
449 error = puc_config(sc, PUC_CFG_GET_DESC, 0, &res);
453 device_set_desc(dev, (const char *)res);
454 return (BUS_PROBE_DEFAULT);
458 puc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
459 u_long start, u_long end, u_long count, u_int flags)
461 struct puc_port *port;
462 struct resource *res;
463 device_t assigned, originator;
466 /* Get our immediate child. */
468 while (child != NULL && device_get_parent(child) != dev)
469 child = device_get_parent(child);
473 port = device_get_ivars(child);
474 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
476 if (rid == NULL || *rid != 0)
479 /* We only support default allocations. */
480 if (start != 0UL || end != ~0UL)
483 if (type == port->p_bar->b_type)
485 else if (type == SYS_RES_IRQ)
493 assigned = rman_get_device(res);
494 if (assigned == NULL) /* Not allocated */
495 rman_set_device(res, originator);
496 else if (assigned != originator)
499 if (flags & RF_ACTIVE) {
500 error = rman_activate_resource(res);
502 if (assigned == NULL)
503 rman_set_device(res, NULL);
512 puc_bus_release_resource(device_t dev, device_t child, int type, int rid,
513 struct resource *res)
515 struct puc_port *port;
518 /* Get our immediate child. */
520 while (child != NULL && device_get_parent(child) != dev)
521 child = device_get_parent(child);
525 port = device_get_ivars(child);
526 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
528 if (rid != 0 || res == NULL)
531 if (type == port->p_bar->b_type) {
532 if (res != port->p_rres)
534 } else if (type == SYS_RES_IRQ) {
535 if (res != port->p_ires)
542 if (rman_get_device(res) != originator)
544 if (rman_get_flags(res) & RF_ACTIVE)
545 rman_deactivate_resource(res);
546 rman_set_device(res, NULL);
551 puc_bus_get_resource(device_t dev, device_t child, int type, int rid,
552 u_long *startp, u_long *countp)
554 struct puc_port *port;
555 struct resource *res;
558 /* Get our immediate child. */
559 while (child != NULL && device_get_parent(child) != dev)
560 child = device_get_parent(child);
564 port = device_get_ivars(child);
565 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
567 if (type == port->p_bar->b_type)
569 else if (type == SYS_RES_IRQ)
574 if (rid != 0 || res == NULL)
577 start = rman_get_start(res);
581 *countp = rman_get_end(res) - start + 1;
586 puc_bus_setup_intr(device_t dev, device_t child, struct resource *res,
587 int flags, driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
589 struct puc_port *port;
590 struct puc_softc *sc;
594 sc = device_get_softc(dev);
596 /* Get our immediate child. */
598 while (child != NULL && device_get_parent(child) != dev)
599 child = device_get_parent(child);
603 port = device_get_ivars(child);
604 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
606 if (cookiep == NULL || res != port->p_ires)
608 /* We demand that serdev devices use filter_only interrupts. */
611 if (rman_get_device(port->p_ires) != originator)
615 * Have non-serdev ports handled by the bus implementation. It
616 * supports multiple handlers for a single interrupt as it is,
617 * so we wouldn't add value if we did it ourselves.
620 if (port->p_type == PUC_TYPE_SERIAL) {
621 i = 0, isrc = SER_INT_OVERRUN;
622 while (i < PUC_ISRCCNT) {
623 port->p_ihsrc[i] = SERDEV_IHAND(originator, isrc);
624 if (port->p_ihsrc[i] != NULL)
630 return (BUS_SETUP_INTR(device_get_parent(dev), originator,
631 sc->sc_ires, flags, filt, ihand, arg, cookiep));
633 sc->sc_serdevs |= 1UL << (port->p_nr - 1);
643 puc_bus_teardown_intr(device_t dev, device_t child, struct resource *res,
646 struct puc_port *port;
647 struct puc_softc *sc;
651 sc = device_get_softc(dev);
653 /* Get our immediate child. */
655 while (child != NULL && device_get_parent(child) != dev)
656 child = device_get_parent(child);
660 port = device_get_ivars(child);
661 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
663 if (res != port->p_ires)
665 if (rman_get_device(port->p_ires) != originator)
668 if (!port->p_hasintr)
669 return (BUS_TEARDOWN_INTR(device_get_parent(dev), originator,
670 sc->sc_ires, cookie));
676 port->p_iharg = NULL;
678 for (i = 0; i < PUC_ISRCCNT; i++)
679 port->p_ihsrc[i] = NULL;
685 puc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
687 struct puc_port *port;
689 /* Get our immediate child. */
690 while (child != NULL && device_get_parent(child) != dev)
691 child = device_get_parent(child);
695 port = device_get_ivars(child);
696 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
703 *result = port->p_rclk;
706 *result = port->p_type;