2002-02-16 15:12:14 +00:00
|
|
|
/*-
|
2006-04-28 21:21:53 +00:00
|
|
|
* Copyright (c) 2006 Marcel Moolenaar
|
|
|
|
* All rights reserved.
|
2002-02-16 15:12:14 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/malloc.h>
|
2006-04-28 21:21:53 +00:00
|
|
|
#include <sys/mutex.h>
|
2002-02-16 15:12:14 +00:00
|
|
|
|
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <machine/resource.h>
|
|
|
|
#include <sys/rman.h>
|
|
|
|
|
|
|
|
#include <dev/pci/pcireg.h>
|
|
|
|
#include <dev/pci/pcivar.h>
|
2002-08-31 18:38:43 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
#include <dev/puc/puc_bus.h>
|
|
|
|
#include <dev/puc/puc_cfg.h>
|
2006-06-29 16:27:19 +00:00
|
|
|
#include <dev/puc/puc_bfe.h>
|
2002-02-16 15:12:14 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
#define PUC_ISRCCNT 5
|
|
|
|
|
|
|
|
struct puc_port {
|
|
|
|
struct puc_bar *p_bar;
|
|
|
|
struct resource *p_rres;
|
|
|
|
struct resource *p_ires;
|
|
|
|
device_t p_dev;
|
|
|
|
int p_nr;
|
|
|
|
int p_type;
|
|
|
|
int p_rclk;
|
2002-02-16 15:12:14 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
int p_hasintr:1;
|
2002-02-16 15:12:14 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
serdev_intr_t *p_ihsrc[PUC_ISRCCNT];
|
|
|
|
void *p_iharg;
|
|
|
|
|
|
|
|
int p_ipend;
|
|
|
|
};
|
2002-02-16 15:12:14 +00:00
|
|
|
|
2002-09-03 11:18:35 +00:00
|
|
|
devclass_t puc_devclass;
|
2006-04-28 21:21:53 +00:00
|
|
|
const char puc_driver_name[] = "puc";
|
2002-09-03 11:18:35 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
MALLOC_DEFINE(M_PUC, "PUC", "PUC driver");
|
2002-08-31 20:29:46 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
struct puc_bar *
|
|
|
|
puc_get_bar(struct puc_softc *sc, int rid)
|
|
|
|
{
|
|
|
|
struct puc_bar *bar;
|
|
|
|
struct rman *rm;
|
|
|
|
u_long end, start;
|
|
|
|
int error, i;
|
|
|
|
|
|
|
|
/* Find the BAR entry with the given RID. */
|
|
|
|
i = 0;
|
|
|
|
while (i < PUC_PCI_BARS && sc->sc_bar[i].b_rid != rid)
|
|
|
|
i++;
|
|
|
|
if (i < PUC_PCI_BARS)
|
|
|
|
return (&sc->sc_bar[i]);
|
|
|
|
|
|
|
|
/* Not found. If we're looking for an unused entry, return NULL. */
|
|
|
|
if (rid == -1)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/* Get an unused entry for us to fill. */
|
|
|
|
bar = puc_get_bar(sc, -1);
|
|
|
|
if (bar == NULL)
|
|
|
|
return (NULL);
|
|
|
|
bar->b_rid = rid;
|
|
|
|
bar->b_type = SYS_RES_IOPORT;
|
|
|
|
bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
|
|
|
|
&bar->b_rid, RF_ACTIVE);
|
|
|
|
if (bar->b_res == NULL) {
|
|
|
|
bar->b_rid = rid;
|
|
|
|
bar->b_type = SYS_RES_MEMORY;
|
|
|
|
bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
|
|
|
|
&bar->b_rid, RF_ACTIVE);
|
|
|
|
if (bar->b_res == NULL) {
|
|
|
|
bar->b_rid = -1;
|
|
|
|
return (NULL);
|
|
|
|
}
|
2002-08-31 20:29:46 +00:00
|
|
|
}
|
2006-04-28 21:21:53 +00:00
|
|
|
|
|
|
|
/* Update our managed space. */
|
|
|
|
rm = (bar->b_type == SYS_RES_IOPORT) ? &sc->sc_ioport : &sc->sc_iomem;
|
|
|
|
start = rman_get_start(bar->b_res);
|
|
|
|
end = rman_get_end(bar->b_res);
|
|
|
|
error = rman_manage_region(rm, start, end);
|
|
|
|
if (error) {
|
|
|
|
bus_release_resource(sc->sc_dev, bar->b_type, bar->b_rid,
|
|
|
|
bar->b_res);
|
|
|
|
bar->b_res = NULL;
|
|
|
|
bar->b_rid = -1;
|
|
|
|
bar = NULL;
|
2005-02-25 19:47:18 +00:00
|
|
|
}
|
2006-04-28 21:21:53 +00:00
|
|
|
|
|
|
|
return (bar);
|
2002-08-31 20:29:46 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 12:19:07 +00:00
|
|
|
static int
|
2006-04-28 21:21:53 +00:00
|
|
|
puc_intr(void *arg)
|
2003-03-15 16:25:40 +00:00
|
|
|
{
|
2006-04-28 21:21:53 +00:00
|
|
|
struct puc_port *port;
|
|
|
|
struct puc_softc *sc = arg;
|
|
|
|
u_long dev, devs;
|
|
|
|
int i, idx, ipend, isrc;
|
|
|
|
uint8_t ilr;
|
|
|
|
|
|
|
|
devs = sc->sc_serdevs;
|
|
|
|
if (sc->sc_ilr == PUC_ILR_DIGI) {
|
|
|
|
idx = 0;
|
|
|
|
while (devs & (0xfful << idx)) {
|
|
|
|
ilr = ~bus_read_1(sc->sc_port[idx].p_rres, 7);
|
|
|
|
devs &= ~0ul ^ ((u_long)ilr << idx);
|
|
|
|
idx += 8;
|
2003-03-15 16:25:40 +00:00
|
|
|
}
|
2006-04-28 21:21:53 +00:00
|
|
|
} else if (sc->sc_ilr == PUC_ILR_QUATECH) {
|
|
|
|
/*
|
|
|
|
* Don't trust the value if it's the same as the option
|
|
|
|
* register. It may mean that the ILR is not active and
|
|
|
|
* we're reading the option register instead. This may
|
|
|
|
* lead to false positives on 8-port boards.
|
|
|
|
*/
|
|
|
|
ilr = bus_read_1(sc->sc_port[0].p_rres, 7);
|
|
|
|
if (ilr != (sc->sc_cfg_data & 0xff))
|
|
|
|
devs &= (u_long)ilr;
|
|
|
|
}
|
2003-03-15 16:25:40 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
ipend = 0;
|
|
|
|
idx = 0, dev = 1UL;
|
|
|
|
while (devs != 0UL) {
|
|
|
|
while ((devs & dev) == 0UL)
|
|
|
|
idx++, dev <<= 1;
|
|
|
|
devs &= ~dev;
|
|
|
|
port = &sc->sc_port[idx];
|
|
|
|
port->p_ipend = SERDEV_IPEND(port->p_dev);
|
|
|
|
ipend |= port->p_ipend;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0, isrc = SER_INT_OVERRUN;
|
|
|
|
while (ipend) {
|
|
|
|
while (i < PUC_ISRCCNT && !(ipend & isrc))
|
|
|
|
i++, isrc <<= 1;
|
|
|
|
KASSERT(i < PUC_ISRCCNT, ("%s", __func__));
|
|
|
|
ipend &= ~isrc;
|
|
|
|
idx = 0, dev = 1UL;
|
|
|
|
devs = sc->sc_serdevs;
|
|
|
|
while (devs != 0UL) {
|
|
|
|
while ((devs & dev) == 0UL)
|
|
|
|
idx++, dev <<= 1;
|
|
|
|
devs &= ~dev;
|
|
|
|
port = &sc->sc_port[idx];
|
|
|
|
if (!(port->p_ipend & isrc))
|
|
|
|
continue;
|
|
|
|
if (port->p_ihsrc[i] != NULL)
|
|
|
|
(*port->p_ihsrc[i])(port->p_iharg);
|
|
|
|
}
|
2007-02-23 12:19:07 +00:00
|
|
|
return (FILTER_HANDLED);
|
2003-03-15 16:25:40 +00:00
|
|
|
}
|
2007-02-23 12:19:07 +00:00
|
|
|
return (FILTER_STRAY);
|
2003-03-15 16:25:40 +00:00
|
|
|
}
|
|
|
|
|
2002-08-31 18:38:43 +00:00
|
|
|
int
|
2006-04-28 21:21:53 +00:00
|
|
|
puc_bfe_attach(device_t dev)
|
2002-02-16 15:12:14 +00:00
|
|
|
{
|
2006-04-28 21:21:53 +00:00
|
|
|
char buffer[64];
|
|
|
|
struct puc_bar *bar;
|
|
|
|
struct puc_port *port;
|
2002-02-16 15:12:14 +00:00
|
|
|
struct puc_softc *sc;
|
2006-04-28 21:21:53 +00:00
|
|
|
struct rman *rm;
|
|
|
|
intptr_t res;
|
|
|
|
bus_addr_t ofs, start;
|
|
|
|
bus_size_t size;
|
|
|
|
bus_space_handle_t bsh;
|
|
|
|
bus_space_tag_t bst;
|
|
|
|
int error, idx;
|
|
|
|
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
|
|
|
|
for (idx = 0; idx < PUC_PCI_BARS; idx++)
|
|
|
|
sc->sc_bar[idx].b_rid = -1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
sc->sc_ioport.rm_type = RMAN_ARRAY;
|
|
|
|
error = rman_init(&sc->sc_ioport);
|
|
|
|
if (!error) {
|
|
|
|
sc->sc_iomem.rm_type = RMAN_ARRAY;
|
|
|
|
error = rman_init(&sc->sc_iomem);
|
|
|
|
if (!error) {
|
|
|
|
sc->sc_irq.rm_type = RMAN_ARRAY;
|
|
|
|
error = rman_init(&sc->sc_irq);
|
|
|
|
if (!error)
|
|
|
|
break;
|
|
|
|
rman_fini(&sc->sc_iomem);
|
|
|
|
}
|
|
|
|
rman_fini(&sc->sc_ioport);
|
2003-09-06 21:48:50 +00:00
|
|
|
}
|
2006-04-28 21:21:53 +00:00
|
|
|
return (error);
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s I/O port mapping",
|
|
|
|
device_get_nameunit(dev));
|
|
|
|
sc->sc_ioport.rm_descr = strdup(buffer, M_PUC);
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s I/O memory mapping",
|
|
|
|
device_get_nameunit(dev));
|
|
|
|
sc->sc_iomem.rm_descr = strdup(buffer, M_PUC);
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s port numbers",
|
|
|
|
device_get_nameunit(dev));
|
|
|
|
sc->sc_irq.rm_descr = strdup(buffer, M_PUC);
|
|
|
|
|
|
|
|
error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
|
|
|
|
KASSERT(error == 0, ("%s %d", __func__, __LINE__));
|
|
|
|
sc->sc_nports = (int)res;
|
|
|
|
sc->sc_port = malloc(sc->sc_nports * sizeof(struct puc_port),
|
|
|
|
M_PUC, M_WAITOK|M_ZERO);
|
|
|
|
|
|
|
|
error = rman_manage_region(&sc->sc_irq, 1, sc->sc_nports);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
error = puc_config(sc, PUC_CFG_SETUP, 0, &res);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
for (idx = 0; idx < sc->sc_nports; idx++) {
|
|
|
|
port = &sc->sc_port[idx];
|
|
|
|
port->p_nr = idx + 1;
|
|
|
|
error = puc_config(sc, PUC_CFG_GET_TYPE, idx, &res);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
port->p_type = res;
|
|
|
|
error = puc_config(sc, PUC_CFG_GET_RID, idx, &res);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
bar = puc_get_bar(sc, res);
|
|
|
|
if (bar == NULL) {
|
|
|
|
error = ENXIO;
|
|
|
|
goto fail;
|
2002-02-16 15:12:14 +00:00
|
|
|
}
|
2006-04-28 21:21:53 +00:00
|
|
|
port->p_bar = bar;
|
|
|
|
start = rman_get_start(bar->b_res);
|
|
|
|
error = puc_config(sc, PUC_CFG_GET_OFS, idx, &res);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
ofs = res;
|
|
|
|
error = puc_config(sc, PUC_CFG_GET_LEN, idx, &res);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
size = res;
|
|
|
|
rm = (bar->b_type == SYS_RES_IOPORT)
|
|
|
|
? &sc->sc_ioport: &sc->sc_iomem;
|
|
|
|
port->p_rres = rman_reserve_resource(rm, start + ofs,
|
|
|
|
start + ofs + size - 1, size, 0, NULL);
|
|
|
|
if (port->p_rres != NULL) {
|
|
|
|
bsh = rman_get_bushandle(bar->b_res);
|
|
|
|
bst = rman_get_bustag(bar->b_res);
|
|
|
|
bus_space_subregion(bst, bsh, ofs, size, &bsh);
|
|
|
|
rman_set_bushandle(port->p_rres, bsh);
|
|
|
|
rman_set_bustag(port->p_rres, bst);
|
2003-03-15 16:25:40 +00:00
|
|
|
}
|
2006-04-28 21:21:53 +00:00
|
|
|
port->p_ires = rman_reserve_resource(&sc->sc_irq, port->p_nr,
|
|
|
|
port->p_nr, 1, 0, NULL);
|
|
|
|
if (port->p_ires == NULL) {
|
|
|
|
error = ENXIO;
|
|
|
|
goto fail;
|
2003-09-06 21:48:50 +00:00
|
|
|
}
|
2006-04-28 21:21:53 +00:00
|
|
|
error = puc_config(sc, PUC_CFG_GET_CLOCK, idx, &res);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
port->p_rclk = res;
|
|
|
|
|
|
|
|
port->p_dev = device_add_child(dev, NULL, -1);
|
|
|
|
if (port->p_dev != NULL)
|
|
|
|
device_set_ivars(port->p_dev, (void *)port);
|
|
|
|
}
|
2002-02-16 15:12:14 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
error = puc_config(sc, PUC_CFG_GET_ILR, 0, &res);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
sc->sc_ilr = res;
|
|
|
|
if (bootverbose && sc->sc_ilr != 0)
|
|
|
|
device_printf(dev, "using interrupt latch register\n");
|
|
|
|
|
|
|
|
sc->sc_irid = 0;
|
|
|
|
sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
|
|
|
|
RF_ACTIVE|RF_SHAREABLE);
|
|
|
|
if (sc->sc_ires != NULL) {
|
|
|
|
error = bus_setup_intr(dev, sc->sc_ires,
|
2007-02-23 12:19:07 +00:00
|
|
|
INTR_TYPE_TTY, puc_intr, NULL, sc, &sc->sc_icookie);
|
2006-04-28 21:21:53 +00:00
|
|
|
if (error)
|
|
|
|
error = bus_setup_intr(dev, sc->sc_ires,
|
2007-02-23 12:19:07 +00:00
|
|
|
INTR_TYPE_TTY | INTR_MPSAFE, NULL,
|
|
|
|
(driver_intr_t *)puc_intr, sc, &sc->sc_icookie);
|
2006-04-28 21:21:53 +00:00
|
|
|
else
|
|
|
|
sc->sc_fastintr = 1;
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
device_printf(dev, "could not activate interrupt\n");
|
|
|
|
bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
|
|
|
|
sc->sc_ires);
|
|
|
|
sc->sc_ires = NULL;
|
2002-02-16 15:12:14 +00:00
|
|
|
}
|
2006-04-28 21:21:53 +00:00
|
|
|
}
|
|
|
|
if (sc->sc_ires == NULL) {
|
|
|
|
/* XXX no interrupt resource. Force polled mode. */
|
|
|
|
sc->sc_polled = 1;
|
|
|
|
}
|
2002-02-16 15:12:14 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
/* Probe and attach our children. */
|
|
|
|
for (idx = 0; idx < sc->sc_nports; idx++) {
|
|
|
|
port = &sc->sc_port[idx];
|
|
|
|
if (port->p_dev == NULL)
|
2002-02-16 15:12:14 +00:00
|
|
|
continue;
|
2006-04-28 21:21:53 +00:00
|
|
|
error = device_probe_and_attach(port->p_dev);
|
|
|
|
if (error) {
|
|
|
|
device_delete_child(dev, port->p_dev);
|
|
|
|
port->p_dev = NULL;
|
2002-02-19 14:58:11 +00:00
|
|
|
}
|
2002-02-16 15:12:14 +00:00
|
|
|
}
|
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
/*
|
|
|
|
* If there are no serdev devices, then our interrupt handler
|
|
|
|
* will do nothing. Tear it down.
|
|
|
|
*/
|
|
|
|
if (sc->sc_serdevs == 0UL)
|
|
|
|
bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
|
2003-03-15 16:25:40 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
return (0);
|
2003-03-15 16:25:40 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
fail:
|
|
|
|
for (idx = 0; idx < sc->sc_nports; idx++) {
|
|
|
|
port = &sc->sc_port[idx];
|
|
|
|
if (port->p_dev != NULL)
|
|
|
|
device_delete_child(dev, port->p_dev);
|
|
|
|
if (port->p_rres != NULL)
|
|
|
|
rman_release_resource(port->p_rres);
|
|
|
|
if (port->p_ires != NULL)
|
|
|
|
rman_release_resource(port->p_ires);
|
2003-03-15 16:25:40 +00:00
|
|
|
}
|
2006-04-28 21:21:53 +00:00
|
|
|
for (idx = 0; idx < PUC_PCI_BARS; idx++) {
|
|
|
|
bar = &sc->sc_bar[idx];
|
|
|
|
if (bar->b_res != NULL)
|
|
|
|
bus_release_resource(sc->sc_dev, bar->b_type,
|
|
|
|
bar->b_rid, bar->b_res);
|
|
|
|
}
|
|
|
|
rman_fini(&sc->sc_irq);
|
|
|
|
free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
|
|
|
|
rman_fini(&sc->sc_iomem);
|
|
|
|
free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
|
|
|
|
rman_fini(&sc->sc_ioport);
|
|
|
|
free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
|
|
|
|
free(sc->sc_port, M_PUC);
|
|
|
|
return (error);
|
2003-03-15 16:25:40 +00:00
|
|
|
}
|
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
int
|
|
|
|
puc_bfe_detach(device_t dev)
|
2002-02-16 15:12:14 +00:00
|
|
|
{
|
2006-04-28 21:21:53 +00:00
|
|
|
struct puc_bar *bar;
|
|
|
|
struct puc_port *port;
|
2002-02-16 15:12:14 +00:00
|
|
|
struct puc_softc *sc;
|
2006-04-28 21:21:53 +00:00
|
|
|
int error, idx;
|
2002-02-16 15:12:14 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
sc = device_get_softc(dev);
|
2002-02-16 15:12:14 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
/* Detach our children. */
|
|
|
|
error = 0;
|
|
|
|
for (idx = 0; idx < sc->sc_nports; idx++) {
|
|
|
|
port = &sc->sc_port[idx];
|
|
|
|
if (port->p_dev == NULL)
|
|
|
|
continue;
|
|
|
|
if (device_detach(port->p_dev) == 0) {
|
|
|
|
device_delete_child(dev, port->p_dev);
|
|
|
|
if (port->p_rres != NULL)
|
|
|
|
rman_release_resource(port->p_rres);
|
|
|
|
if (port->p_ires != NULL)
|
|
|
|
rman_release_resource(port->p_ires);
|
|
|
|
} else
|
|
|
|
error = ENXIO;
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
if (sc->sc_serdevs != 0UL)
|
|
|
|
bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
|
|
|
|
bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, sc->sc_ires);
|
|
|
|
|
|
|
|
for (idx = 0; idx < PUC_PCI_BARS; idx++) {
|
|
|
|
bar = &sc->sc_bar[idx];
|
|
|
|
if (bar->b_res != NULL)
|
|
|
|
bus_release_resource(sc->sc_dev, bar->b_type,
|
|
|
|
bar->b_rid, bar->b_res);
|
|
|
|
}
|
|
|
|
|
|
|
|
rman_fini(&sc->sc_irq);
|
|
|
|
free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
|
|
|
|
rman_fini(&sc->sc_iomem);
|
|
|
|
free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
|
|
|
|
rman_fini(&sc->sc_ioport);
|
|
|
|
free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
|
|
|
|
free(sc->sc_port, M_PUC);
|
|
|
|
return (0);
|
2002-02-16 15:12:14 +00:00
|
|
|
}
|
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
int
|
|
|
|
puc_bfe_probe(device_t dev, const struct puc_cfg *cfg)
|
2002-02-16 15:12:14 +00:00
|
|
|
{
|
2006-04-28 21:21:53 +00:00
|
|
|
struct puc_softc *sc;
|
|
|
|
intptr_t res;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
sc->sc_dev = dev;
|
|
|
|
sc->sc_cfg = cfg;
|
|
|
|
|
|
|
|
/* We don't attach to single-port serial cards. */
|
|
|
|
if (cfg->ports == PUC_PORT_1S || cfg->ports == PUC_PORT_1P)
|
|
|
|
return (EDOOFUS);
|
|
|
|
error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
error = puc_config(sc, PUC_CFG_GET_DESC, 0, &res);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
if (res != 0)
|
|
|
|
device_set_desc(dev, (const char *)res);
|
|
|
|
return (BUS_PROBE_DEFAULT);
|
2002-02-16 15:12:14 +00:00
|
|
|
}
|
|
|
|
|
2002-08-31 18:38:43 +00:00
|
|
|
struct resource *
|
2006-04-28 21:21:53 +00:00
|
|
|
puc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
|
2002-02-16 15:12:14 +00:00
|
|
|
u_long start, u_long end, u_long count, u_int flags)
|
|
|
|
{
|
2006-04-28 21:21:53 +00:00
|
|
|
struct puc_port *port;
|
|
|
|
struct resource *res;
|
|
|
|
device_t assigned, originator;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Get our immediate child. */
|
|
|
|
originator = child;
|
|
|
|
while (child != NULL && device_get_parent(child) != dev)
|
|
|
|
child = device_get_parent(child);
|
|
|
|
if (child == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
port = device_get_ivars(child);
|
|
|
|
KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
|
|
|
|
|
|
|
|
if (rid == NULL || *rid != 0)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/* We only support default allocations. */
|
|
|
|
if (start != 0UL || end != ~0UL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (type == port->p_bar->b_type)
|
|
|
|
res = port->p_rres;
|
|
|
|
else if (type == SYS_RES_IRQ)
|
|
|
|
res = port->p_ires;
|
2003-08-01 02:25:32 +00:00
|
|
|
else
|
2006-04-28 21:21:53 +00:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (res == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
assigned = rman_get_device(res);
|
|
|
|
if (assigned == NULL) /* Not allocated */
|
|
|
|
rman_set_device(res, originator);
|
|
|
|
else if (assigned != originator)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (flags & RF_ACTIVE) {
|
|
|
|
error = rman_activate_resource(res);
|
|
|
|
if (error) {
|
|
|
|
if (assigned == NULL)
|
|
|
|
rman_set_device(res, NULL);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
2002-02-16 15:12:14 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
return (res);
|
2002-02-16 15:12:14 +00:00
|
|
|
}
|
|
|
|
|
2002-08-31 18:38:43 +00:00
|
|
|
int
|
2006-04-28 21:21:53 +00:00
|
|
|
puc_bus_release_resource(device_t dev, device_t child, int type, int rid,
|
2002-02-16 15:12:14 +00:00
|
|
|
struct resource *res)
|
|
|
|
{
|
2006-04-28 21:21:53 +00:00
|
|
|
struct puc_port *port;
|
|
|
|
device_t originator;
|
|
|
|
|
|
|
|
/* Get our immediate child. */
|
|
|
|
originator = child;
|
|
|
|
while (child != NULL && device_get_parent(child) != dev)
|
|
|
|
child = device_get_parent(child);
|
|
|
|
if (child == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
port = device_get_ivars(child);
|
|
|
|
KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
|
|
|
|
|
|
|
|
if (rid != 0 || res == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (type == port->p_bar->b_type) {
|
|
|
|
if (res != port->p_rres)
|
|
|
|
return (EINVAL);
|
|
|
|
} else if (type == SYS_RES_IRQ) {
|
|
|
|
if (res != port->p_ires)
|
|
|
|
return (EINVAL);
|
|
|
|
if (port->p_hasintr)
|
|
|
|
return (EBUSY);
|
|
|
|
} else
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (rman_get_device(res) != originator)
|
|
|
|
return (ENXIO);
|
|
|
|
if (rman_get_flags(res) & RF_ACTIVE)
|
|
|
|
rman_deactivate_resource(res);
|
|
|
|
rman_set_device(res, NULL);
|
2002-02-16 15:12:14 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-08-31 18:38:43 +00:00
|
|
|
int
|
2006-04-28 21:21:53 +00:00
|
|
|
puc_bus_get_resource(device_t dev, device_t child, int type, int rid,
|
2002-02-16 15:12:14 +00:00
|
|
|
u_long *startp, u_long *countp)
|
|
|
|
{
|
2006-04-28 21:21:53 +00:00
|
|
|
struct puc_port *port;
|
|
|
|
struct resource *res;
|
|
|
|
u_long start;
|
|
|
|
|
|
|
|
/* Get our immediate child. */
|
|
|
|
while (child != NULL && device_get_parent(child) != dev)
|
|
|
|
child = device_get_parent(child);
|
|
|
|
if (child == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
port = device_get_ivars(child);
|
|
|
|
KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
|
|
|
|
|
|
|
|
if (type == port->p_bar->b_type)
|
|
|
|
res = port->p_rres;
|
|
|
|
else if (type == SYS_RES_IRQ)
|
|
|
|
res = port->p_ires;
|
|
|
|
else
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
if (rid != 0 || res == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
start = rman_get_start(res);
|
|
|
|
if (startp != NULL)
|
|
|
|
*startp = start;
|
|
|
|
if (countp != NULL)
|
|
|
|
*countp = rman_get_end(res) - start + 1;
|
|
|
|
return (0);
|
2002-02-16 15:12:14 +00:00
|
|
|
}
|
|
|
|
|
2002-08-31 18:38:43 +00:00
|
|
|
int
|
2006-04-28 21:21:53 +00:00
|
|
|
puc_bus_setup_intr(device_t dev, device_t child, struct resource *res,
|
2007-02-23 12:19:07 +00:00
|
|
|
int flags, driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
|
2002-02-16 15:12:14 +00:00
|
|
|
{
|
2006-04-28 21:21:53 +00:00
|
|
|
struct puc_port *port;
|
2002-02-16 15:12:14 +00:00
|
|
|
struct puc_softc *sc;
|
2006-04-28 21:21:53 +00:00
|
|
|
device_t originator;
|
|
|
|
int i, isrc, serdev;
|
|
|
|
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
|
|
|
|
/* Get our immediate child. */
|
|
|
|
originator = child;
|
|
|
|
while (child != NULL && device_get_parent(child) != dev)
|
|
|
|
child = device_get_parent(child);
|
|
|
|
if (child == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
port = device_get_ivars(child);
|
|
|
|
KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
|
2002-02-16 15:12:14 +00:00
|
|
|
|
2007-06-06 22:17:01 +00:00
|
|
|
if (cookiep == NULL || res != port->p_ires)
|
2006-04-28 21:21:53 +00:00
|
|
|
return (EINVAL);
|
2007-06-06 22:17:01 +00:00
|
|
|
/* We demand that serdev devices use filter_only interrupts. */
|
|
|
|
if (ihand != NULL)
|
|
|
|
return (ENXIO);
|
2006-04-28 21:21:53 +00:00
|
|
|
if (rman_get_device(port->p_ires) != originator)
|
2002-09-04 15:29:04 +00:00
|
|
|
return (ENXIO);
|
2006-04-28 21:21:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Have non-serdev ports handled by the bus implementation. It
|
|
|
|
* supports multiple handlers for a single interrupt as it is,
|
|
|
|
* so we wouldn't add value if we did it ourselves.
|
|
|
|
*/
|
|
|
|
serdev = 0;
|
|
|
|
if (port->p_type == PUC_TYPE_SERIAL) {
|
|
|
|
i = 0, isrc = SER_INT_OVERRUN;
|
|
|
|
while (i < PUC_ISRCCNT) {
|
|
|
|
port->p_ihsrc[i] = SERDEV_IHAND(originator, isrc);
|
|
|
|
if (port->p_ihsrc[i] != NULL)
|
|
|
|
serdev = 1;
|
|
|
|
i++, isrc <<= 1;
|
2002-02-16 15:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
2006-04-28 21:21:53 +00:00
|
|
|
if (!serdev)
|
|
|
|
return (BUS_SETUP_INTR(device_get_parent(dev), originator,
|
2007-02-23 12:19:07 +00:00
|
|
|
sc->sc_ires, flags, filt, ihand, arg, cookiep));
|
2006-04-28 21:21:53 +00:00
|
|
|
|
|
|
|
sc->sc_serdevs |= 1UL << (port->p_nr - 1);
|
|
|
|
|
|
|
|
port->p_hasintr = 1;
|
|
|
|
port->p_iharg = arg;
|
|
|
|
|
|
|
|
*cookiep = port;
|
|
|
|
return (0);
|
2002-02-16 15:12:14 +00:00
|
|
|
}
|
|
|
|
|
2002-08-31 18:38:43 +00:00
|
|
|
int
|
2006-04-28 21:21:53 +00:00
|
|
|
puc_bus_teardown_intr(device_t dev, device_t child, struct resource *res,
|
|
|
|
void *cookie)
|
2002-02-16 15:12:14 +00:00
|
|
|
{
|
2006-04-28 21:21:53 +00:00
|
|
|
struct puc_port *port;
|
2002-02-16 15:12:14 +00:00
|
|
|
struct puc_softc *sc;
|
2006-04-28 21:21:53 +00:00
|
|
|
device_t originator;
|
|
|
|
int i;
|
2002-02-16 15:12:14 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
sc = device_get_softc(dev);
|
|
|
|
|
|
|
|
/* Get our immediate child. */
|
|
|
|
originator = child;
|
|
|
|
while (child != NULL && device_get_parent(child) != dev)
|
|
|
|
child = device_get_parent(child);
|
|
|
|
if (child == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
port = device_get_ivars(child);
|
|
|
|
KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
|
|
|
|
|
|
|
|
if (res != port->p_ires)
|
|
|
|
return (EINVAL);
|
|
|
|
if (rman_get_device(port->p_ires) != originator)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
if (!port->p_hasintr)
|
|
|
|
return (BUS_TEARDOWN_INTR(device_get_parent(dev), originator,
|
|
|
|
sc->sc_ires, cookie));
|
|
|
|
|
|
|
|
if (cookie != port)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
port->p_hasintr = 0;
|
|
|
|
port->p_iharg = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < PUC_ISRCCNT; i++)
|
|
|
|
port->p_ihsrc[i] = NULL;
|
|
|
|
|
|
|
|
return (0);
|
2002-02-16 15:12:14 +00:00
|
|
|
}
|
|
|
|
|
2002-08-31 18:38:43 +00:00
|
|
|
int
|
2006-04-28 21:21:53 +00:00
|
|
|
puc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
|
2002-02-16 15:12:14 +00:00
|
|
|
{
|
2006-04-28 21:21:53 +00:00
|
|
|
struct puc_port *port;
|
2002-02-16 15:12:14 +00:00
|
|
|
|
2006-04-28 21:21:53 +00:00
|
|
|
/* Get our immediate child. */
|
|
|
|
while (child != NULL && device_get_parent(child) != dev)
|
|
|
|
child = device_get_parent(child);
|
|
|
|
if (child == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
port = device_get_ivars(child);
|
|
|
|
KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
|
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
return (EINVAL);
|
2002-02-16 15:12:14 +00:00
|
|
|
|
|
|
|
switch(index) {
|
2006-04-28 21:21:53 +00:00
|
|
|
case PUC_IVAR_CLOCK:
|
|
|
|
*result = port->p_rclk;
|
2003-09-06 21:48:50 +00:00
|
|
|
break;
|
2006-04-28 21:21:53 +00:00
|
|
|
case PUC_IVAR_TYPE:
|
|
|
|
*result = port->p_type;
|
2003-09-26 04:44:55 +00:00
|
|
|
break;
|
2002-02-16 15:12:14 +00:00
|
|
|
default:
|
|
|
|
return (ENOENT);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|