#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uba.c,v 1.83 2021/08/07 16:19:15 thorpej Exp $");
#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <sys/bus.h>
#include <machine/scb.h>
#include <sys/cpu.h>
#include <dev/qbus/ubareg.h>
#include <dev/qbus/ubavar.h>
#include "ioconf.h"
#include "locators.h"
static int ubasearch (device_t, cfdata_t,
const int *, void *);
static int ubaprint (void *, const char *);
void
uba_enqueue(struct uba_unit *uu)
{
struct uba_softc *uh;
int s;
uh = device_private(device_parent(uu->uu_dev));
s = spluba();
SIMPLEQ_INSERT_TAIL(&uh->uh_resq, uu, uu_resq);
splx(s);
}
void
uba_done(struct uba_softc *uh)
{
struct uba_unit *uu;
while ((uu = SIMPLEQ_FIRST(&uh->uh_resq))) {
SIMPLEQ_REMOVE_HEAD(&uh->uh_resq, uu_resq);
if ((*uu->uu_ready)(uu) == 0) {
SIMPLEQ_INSERT_HEAD(&uh->uh_resq, uu, uu_resq);
break;
}
}
}
void
uba_reset_establish(void (*reset)(device_t), device_t dev)
{
struct uba_softc *uh = device_private(device_parent(dev));
struct uba_reset *ur;
ur = malloc(sizeof(struct uba_reset), M_DEVBUF, M_WAITOK|M_ZERO);
ur->ur_dev = dev;
ur->ur_reset = reset;
SIMPLEQ_INSERT_TAIL(&uh->uh_resetq, ur, ur_resetq);
}
int
uballoc(struct uba_softc *uh, struct ubinfo *ui, int flags)
{
int waitok = (flags & UBA_CANTWAIT) == 0;
int error;
if ((error = bus_dmamap_create(uh->uh_dmat, ui->ui_size, 1,
ui->ui_size, 0, (waitok ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT),
&ui->ui_dmam)))
return error;
if ((error = bus_dmamap_load(uh->uh_dmat, ui->ui_dmam, ui->ui_vaddr,
ui->ui_size, NULL, (waitok ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT)))) {
bus_dmamap_destroy(uh->uh_dmat, ui->ui_dmam);
return error;
}
ui->ui_baddr = ui->ui_dmam->dm_segs[0].ds_addr;
return 0;
}
int
ubmemalloc(struct uba_softc *uh, struct ubinfo *ui, int flags)
{
int waitok = (flags & UBA_CANTWAIT ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
int error;
if ((error = bus_dmamem_alloc(uh->uh_dmat, ui->ui_size, PAGE_SIZE, 0,
&ui->ui_seg, 1, &ui->ui_rseg, waitok)))
return error;
if ((error = bus_dmamem_map(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg,
ui->ui_size, &ui->ui_vaddr, waitok|BUS_DMA_COHERENT))) {
bus_dmamem_free(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg);
return error;
}
if ((error = uballoc(uh, ui, flags))) {
bus_dmamem_unmap(uh->uh_dmat, ui->ui_vaddr, ui->ui_size);
bus_dmamem_free(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg);
}
return error;
}
void
ubfree(struct uba_softc *uh, struct ubinfo *ui)
{
bus_dmamap_unload(uh->uh_dmat, ui->ui_dmam);
bus_dmamap_destroy(uh->uh_dmat, ui->ui_dmam);
}
void
ubmemfree(struct uba_softc *uh, struct ubinfo *ui)
{
bus_dmamem_unmap(uh->uh_dmat, ui->ui_vaddr, ui->ui_size);
bus_dmamem_free(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg);
ubfree(uh, ui);
}
void
ubareset(struct uba_softc *uh)
{
struct uba_reset *ur;
int s;
s = spluba();
SIMPLEQ_INIT(&uh->uh_resq);
printf("%s: reset", device_xname(uh->uh_dev));
(*uh->uh_ubainit)(uh);
ur = SIMPLEQ_FIRST(&uh->uh_resetq);
if (ur) do {
printf(" %s", device_xname(ur->ur_dev));
(*ur->ur_reset)(ur->ur_dev);
} while ((ur = SIMPLEQ_NEXT(ur, ur_resetq)));
printf("\n");
splx(s);
}
void
uba_attach(struct uba_softc *sc, paddr_t iopagephys)
{
sc->uh_lastiv = 0x200;
SIMPLEQ_INIT(&sc->uh_resq);
SIMPLEQ_INIT(&sc->uh_resetq);
if (bus_space_map(sc->uh_iot, iopagephys, UBAIOSIZE, 0, &sc->uh_ioh))
return;
sc->uh_used = malloc(UBAIOSIZE, M_TEMP, M_WAITOK);
memset(sc->uh_used, 0, UBAIOSIZE);
if (sc->uh_beforescan)
(*sc->uh_beforescan)(sc);
config_search(sc->uh_dev, NULL,
CFARGS(.search = ubasearch));
if (sc->uh_afterscan)
(*sc->uh_afterscan)(sc);
free(sc->uh_used, M_TEMP);
}
int
ubasearch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
struct uba_softc *sc = device_private(parent);
struct uba_attach_args ua;
int i, csr, vec, br;
csr = cf->cf_loc[UBACF_CSR];
if (sc->uh_used[ubdevreg(csr)])
return 0;
ua.ua_ioh = ubdevreg(csr) + sc->uh_ioh;
ua.ua_iot = sc->uh_iot;
ua.ua_dmat = sc->uh_dmat;
if (badaddr((void *)ua.ua_ioh, 2) ||
(sc->uh_errchk ? (*sc->uh_errchk)(sc):0))
goto forgetit;
scb_vecref(0, 0);
i = config_probe(parent, cf, &ua);
if (sc->uh_errchk)
if ((*sc->uh_errchk)(sc))
goto forgetit;
if (i == 0)
goto forgetit;
i = scb_vecref(&vec, &br);
if (i == 0)
goto fail;
if (vec == 0)
goto fail;
ua.ua_br = br;
ua.ua_cvec = vec;
ua.ua_iaddr = csr;
ua.ua_evcnt = NULL;
sc->uh_used[ubdevreg(csr)] = 1;
config_attach(parent, cf, &ua, ubaprint, CFARGS_NONE);
return 0;
fail:
printf("%s%d at %s csr %o %s\n",
cf->cf_name, cf->cf_unit, device_xname(parent),
csr, (i ? "zero vector" : "didn't interrupt"));
forgetit:
return 0;
}
int
ubaprint(void *aux, const char *uba)
{
struct uba_attach_args *ua = aux;
aprint_normal(" csr %o vec %o ipl %x", ua->ua_iaddr,
ua->ua_cvec & 511, ua->ua_br);
return UNCONF;
}
void
uba_intr_establish(void *icookie, int vec, void (*ifunc)(void *iarg),
void *iarg, struct evcnt *ev)
{
scb_vecalloc(vec, ifunc, iarg, SCB_ISTACK, ev);
}