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 driver_filter_t *p_ih;
63 serdev_intr_t *p_ihsrc[PUC_ISRCCNT];
69 devclass_t puc_devclass;
70 const char puc_driver_name[] = "puc";
72 MALLOC_DEFINE(M_PUC, "PUC", "PUC driver");
75 puc_get_bar(struct puc_softc *sc, int rid)
82 /* Find the BAR entry with the given RID. */
84 while (i < PUC_PCI_BARS && sc->sc_bar[i].b_rid != rid)
87 return (&sc->sc_bar[i]);
89 /* Not found. If we're looking for an unused entry, return NULL. */
93 /* Get an unused entry for us to fill. */
94 bar = puc_get_bar(sc, -1);
98 bar->b_type = SYS_RES_IOPORT;
99 bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
100 &bar->b_rid, RF_ACTIVE);
101 if (bar->b_res == NULL) {
103 bar->b_type = SYS_RES_MEMORY;
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) {
112 /* Update our managed space. */
113 rm = (bar->b_type == SYS_RES_IOPORT) ? &sc->sc_ioport : &sc->sc_iomem;
114 start = rman_get_start(bar->b_res);
115 end = rman_get_end(bar->b_res);
116 error = rman_manage_region(rm, start, end);
118 bus_release_resource(sc->sc_dev, bar->b_type, bar->b_rid,
131 struct puc_port *port;
132 struct puc_softc *sc = arg;
134 int i, idx, ipend, isrc;
137 devs = sc->sc_serdevs;
138 if (sc->sc_ilr == PUC_ILR_DIGI) {
140 while (devs & (0xfful << idx)) {
141 ilr = ~bus_read_1(sc->sc_port[idx].p_rres, 7);
142 devs &= ~0ul ^ ((u_long)ilr << idx);
145 } else if (sc->sc_ilr == PUC_ILR_QUATECH) {
147 * Don't trust the value if it's the same as the option
148 * register. It may mean that the ILR is not active and
149 * we're reading the option register instead. This may
150 * lead to false positives on 8-port boards.
152 ilr = bus_read_1(sc->sc_port[0].p_rres, 7);
153 if (ilr != (sc->sc_cfg_data & 0xff))
159 while (devs != 0UL) {
160 while ((devs & dev) == 0UL)
163 port = &sc->sc_port[idx];
164 port->p_ipend = SERDEV_IPEND(port->p_dev);
165 ipend |= port->p_ipend;
168 i = 0, isrc = SER_INT_OVERRUN;
170 while (i < PUC_ISRCCNT && !(ipend & isrc))
172 KASSERT(i < PUC_ISRCCNT, ("%s", __func__));
175 devs = sc->sc_serdevs;
176 while (devs != 0UL) {
177 while ((devs & dev) == 0UL)
180 port = &sc->sc_port[idx];
181 if (!(port->p_ipend & isrc))
183 if (port->p_ihsrc[i] != NULL)
184 (*port->p_ihsrc[i])(port->p_iharg);
186 return (FILTER_HANDLED);
188 return (FILTER_STRAY);
192 puc_bfe_attach(device_t dev)
196 struct puc_port *port;
197 struct puc_softc *sc;
200 bus_addr_t ofs, start;
202 bus_space_handle_t bsh;
206 sc = device_get_softc(dev);
208 for (idx = 0; idx < PUC_PCI_BARS; idx++)
209 sc->sc_bar[idx].b_rid = -1;
212 sc->sc_ioport.rm_type = RMAN_ARRAY;
213 error = rman_init(&sc->sc_ioport);
215 sc->sc_iomem.rm_type = RMAN_ARRAY;
216 error = rman_init(&sc->sc_iomem);
218 sc->sc_irq.rm_type = RMAN_ARRAY;
219 error = rman_init(&sc->sc_irq);
222 rman_fini(&sc->sc_iomem);
224 rman_fini(&sc->sc_ioport);
229 snprintf(buffer, sizeof(buffer), "%s I/O port mapping",
230 device_get_nameunit(dev));
231 sc->sc_ioport.rm_descr = strdup(buffer, M_PUC);
232 snprintf(buffer, sizeof(buffer), "%s I/O memory mapping",
233 device_get_nameunit(dev));
234 sc->sc_iomem.rm_descr = strdup(buffer, M_PUC);
235 snprintf(buffer, sizeof(buffer), "%s port numbers",
236 device_get_nameunit(dev));
237 sc->sc_irq.rm_descr = strdup(buffer, M_PUC);
239 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
240 KASSERT(error == 0, ("%s %d", __func__, __LINE__));
241 sc->sc_nports = (int)res;
242 sc->sc_port = malloc(sc->sc_nports * sizeof(struct puc_port),
243 M_PUC, M_WAITOK|M_ZERO);
245 error = rman_manage_region(&sc->sc_irq, 1, sc->sc_nports);
249 error = puc_config(sc, PUC_CFG_SETUP, 0, &res);
253 for (idx = 0; idx < sc->sc_nports; idx++) {
254 port = &sc->sc_port[idx];
255 port->p_nr = idx + 1;
256 error = puc_config(sc, PUC_CFG_GET_TYPE, idx, &res);
260 error = puc_config(sc, PUC_CFG_GET_RID, idx, &res);
263 bar = puc_get_bar(sc, res);
269 start = rman_get_start(bar->b_res);
270 error = puc_config(sc, PUC_CFG_GET_OFS, idx, &res);
274 error = puc_config(sc, PUC_CFG_GET_LEN, idx, &res);
278 rm = (bar->b_type == SYS_RES_IOPORT)
279 ? &sc->sc_ioport: &sc->sc_iomem;
280 port->p_rres = rman_reserve_resource(rm, start + ofs,
281 start + ofs + size - 1, size, 0, NULL);
282 if (port->p_rres != NULL) {
283 bsh = rman_get_bushandle(bar->b_res);
284 bst = rman_get_bustag(bar->b_res);
285 bus_space_subregion(bst, bsh, ofs, size, &bsh);
286 rman_set_bushandle(port->p_rres, bsh);
287 rman_set_bustag(port->p_rres, bst);
289 port->p_ires = rman_reserve_resource(&sc->sc_irq, port->p_nr,
290 port->p_nr, 1, 0, NULL);
291 if (port->p_ires == NULL) {
295 error = puc_config(sc, PUC_CFG_GET_CLOCK, idx, &res);
300 port->p_dev = device_add_child(dev, NULL, -1);
301 if (port->p_dev != NULL)
302 device_set_ivars(port->p_dev, (void *)port);
305 error = puc_config(sc, PUC_CFG_GET_ILR, 0, &res);
309 if (bootverbose && sc->sc_ilr != 0)
310 device_printf(dev, "using interrupt latch register\n");
313 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
314 RF_ACTIVE|RF_SHAREABLE);
315 if (sc->sc_ires != NULL) {
316 error = bus_setup_intr(dev, sc->sc_ires,
317 INTR_TYPE_TTY, puc_intr, NULL, sc, &sc->sc_icookie);
319 error = bus_setup_intr(dev, sc->sc_ires,
320 INTR_TYPE_TTY | INTR_MPSAFE, NULL,
321 (driver_intr_t *)puc_intr, sc, &sc->sc_icookie);
326 device_printf(dev, "could not activate interrupt\n");
327 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
332 if (sc->sc_ires == NULL) {
333 /* XXX no interrupt resource. Force polled mode. */
337 /* Probe and attach our children. */
338 for (idx = 0; idx < sc->sc_nports; idx++) {
339 port = &sc->sc_port[idx];
340 if (port->p_dev == NULL)
342 error = device_probe_and_attach(port->p_dev);
344 device_delete_child(dev, port->p_dev);
350 * If there are no serdev devices, then our interrupt handler
351 * will do nothing. Tear it down.
353 if (sc->sc_serdevs == 0UL)
354 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
359 for (idx = 0; idx < sc->sc_nports; idx++) {
360 port = &sc->sc_port[idx];
361 if (port->p_dev != NULL)
362 device_delete_child(dev, port->p_dev);
363 if (port->p_rres != NULL)
364 rman_release_resource(port->p_rres);
365 if (port->p_ires != NULL)
366 rman_release_resource(port->p_ires);
368 for (idx = 0; idx < PUC_PCI_BARS; idx++) {
369 bar = &sc->sc_bar[idx];
370 if (bar->b_res != NULL)
371 bus_release_resource(sc->sc_dev, bar->b_type,
372 bar->b_rid, bar->b_res);
374 rman_fini(&sc->sc_irq);
375 free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
376 rman_fini(&sc->sc_iomem);
377 free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
378 rman_fini(&sc->sc_ioport);
379 free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
380 free(sc->sc_port, M_PUC);
385 puc_bfe_detach(device_t dev)
388 struct puc_port *port;
389 struct puc_softc *sc;
392 sc = device_get_softc(dev);
394 /* Detach our children. */
396 for (idx = 0; idx < sc->sc_nports; idx++) {
397 port = &sc->sc_port[idx];
398 if (port->p_dev == NULL)
400 if (device_detach(port->p_dev) == 0) {
401 device_delete_child(dev, port->p_dev);
402 if (port->p_rres != NULL)
403 rman_release_resource(port->p_rres);
404 if (port->p_ires != NULL)
405 rman_release_resource(port->p_ires);
412 if (sc->sc_serdevs != 0UL)
413 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
414 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, sc->sc_ires);
416 for (idx = 0; idx < PUC_PCI_BARS; idx++) {
417 bar = &sc->sc_bar[idx];
418 if (bar->b_res != NULL)
419 bus_release_resource(sc->sc_dev, bar->b_type,
420 bar->b_rid, bar->b_res);
423 rman_fini(&sc->sc_irq);
424 free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
425 rman_fini(&sc->sc_iomem);
426 free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
427 rman_fini(&sc->sc_ioport);
428 free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
429 free(sc->sc_port, M_PUC);
434 puc_bfe_probe(device_t dev, const struct puc_cfg *cfg)
436 struct puc_softc *sc;
440 sc = device_get_softc(dev);
444 /* We don't attach to single-port serial cards. */
445 if (cfg->ports == PUC_PORT_1S || cfg->ports == PUC_PORT_1P)
447 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
450 error = puc_config(sc, PUC_CFG_GET_DESC, 0, &res);
454 device_set_desc(dev, (const char *)res);
455 return (BUS_PROBE_DEFAULT);
459 puc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
460 u_long start, u_long end, u_long count, u_int flags)
462 struct puc_port *port;
463 struct resource *res;
464 device_t assigned, originator;
467 /* Get our immediate child. */
469 while (child != NULL && device_get_parent(child) != dev)
470 child = device_get_parent(child);
474 port = device_get_ivars(child);
475 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
477 if (rid == NULL || *rid != 0)
480 /* We only support default allocations. */
481 if (start != 0UL || end != ~0UL)
484 if (type == port->p_bar->b_type)
486 else if (type == SYS_RES_IRQ)
494 assigned = rman_get_device(res);
495 if (assigned == NULL) /* Not allocated */
496 rman_set_device(res, originator);
497 else if (assigned != originator)
500 if (flags & RF_ACTIVE) {
501 error = rman_activate_resource(res);
503 if (assigned == NULL)
504 rman_set_device(res, NULL);
513 puc_bus_release_resource(device_t dev, device_t child, int type, int rid,
514 struct resource *res)
516 struct puc_port *port;
519 /* Get our immediate child. */
521 while (child != NULL && device_get_parent(child) != dev)
522 child = device_get_parent(child);
526 port = device_get_ivars(child);
527 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
529 if (rid != 0 || res == NULL)
532 if (type == port->p_bar->b_type) {
533 if (res != port->p_rres)
535 } else if (type == SYS_RES_IRQ) {
536 if (res != port->p_ires)
543 if (rman_get_device(res) != originator)
545 if (rman_get_flags(res) & RF_ACTIVE)
546 rman_deactivate_resource(res);
547 rman_set_device(res, NULL);
552 puc_bus_get_resource(device_t dev, device_t child, int type, int rid,
553 u_long *startp, u_long *countp)
555 struct puc_port *port;
556 struct resource *res;
559 /* Get our immediate child. */
560 while (child != NULL && device_get_parent(child) != dev)
561 child = device_get_parent(child);
565 port = device_get_ivars(child);
566 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
568 if (type == port->p_bar->b_type)
570 else if (type == SYS_RES_IRQ)
575 if (rid != 0 || res == NULL)
578 start = rman_get_start(res);
582 *countp = rman_get_end(res) - start + 1;
587 puc_bus_setup_intr(device_t dev, device_t child, struct resource *res,
588 int flags, driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
590 struct puc_port *port;
591 struct puc_softc *sc;
595 sc = device_get_softc(dev);
597 /* Get our immediate child. */
599 while (child != NULL && device_get_parent(child) != dev)
600 child = device_get_parent(child);
604 port = device_get_ivars(child);
605 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
607 if (filt == NULL || cookiep == NULL || res != port->p_ires)
609 if (rman_get_device(port->p_ires) != originator)
613 * Have non-serdev ports handled by the bus implementation. It
614 * supports multiple handlers for a single interrupt as it is,
615 * so we wouldn't add value if we did it ourselves.
618 if (port->p_type == PUC_TYPE_SERIAL) {
619 i = 0, isrc = SER_INT_OVERRUN;
620 while (i < PUC_ISRCCNT) {
621 port->p_ihsrc[i] = SERDEV_IHAND(originator, isrc);
622 if (port->p_ihsrc[i] != NULL)
628 return (BUS_SETUP_INTR(device_get_parent(dev), originator,
629 sc->sc_ires, flags, filt, ihand, arg, cookiep));
631 /* We demand that serdev devices use fast interrupts. */
635 sc->sc_serdevs |= 1UL << (port->p_nr - 1);
646 puc_bus_teardown_intr(device_t dev, device_t child, struct resource *res,
649 struct puc_port *port;
650 struct puc_softc *sc;
654 sc = device_get_softc(dev);
656 /* Get our immediate child. */
658 while (child != NULL && device_get_parent(child) != dev)
659 child = device_get_parent(child);
663 port = device_get_ivars(child);
664 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
666 if (res != port->p_ires)
668 if (rman_get_device(port->p_ires) != originator)
671 if (!port->p_hasintr)
672 return (BUS_TEARDOWN_INTR(device_get_parent(dev), originator,
673 sc->sc_ires, cookie));
680 port->p_iharg = NULL;
682 for (i = 0; i < PUC_ISRCCNT; i++)
683 port->p_ihsrc[i] = NULL;
689 puc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
691 struct puc_port *port;
693 /* Get our immediate child. */
694 while (child != NULL && device_get_parent(child) != dev)
695 child = device_get_parent(child);
699 port = device_get_ivars(child);
700 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
707 *result = port->p_rclk;
710 *result = port->p_type;