#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: auspi.c,v 1.12 2025/09/10 01:55:07 thorpej Exp $");
#include "locators.h"
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <mips/alchemy/include/aubusvar.h>
#include <mips/alchemy/include/auvar.h>
#include <mips/alchemy/dev/aupscreg.h>
#include <mips/alchemy/dev/aupscvar.h>
#include <mips/alchemy/dev/auspireg.h>
#include <mips/alchemy/dev/auspivar.h>
#include <dev/spi/spivar.h>
struct auspi_softc {
device_t sc_dev;
struct aupsc_controller sc_psc;
struct spi_controller sc_spi;
struct auspi_machdep sc_md;
struct auspi_job *sc_job;
struct spi_chunk *sc_wchunk;
struct spi_chunk *sc_rchunk;
void *sc_ih;
struct spi_transfer *sc_transfer;
bool sc_running;
SIMPLEQ_HEAD(,spi_transfer) sc_q;
};
#define auspi_select(sc, slave) \
(sc)->sc_md.am_select((sc)->sc_md.am_cookie, (slave))
#define STATIC
STATIC int auspi_match(device_t, struct cfdata *, void *);
STATIC void auspi_attach(device_t, device_t, void *);
STATIC int auspi_intr(void *);
CFATTACH_DECL_NEW(auspi, sizeof(struct auspi_softc),
auspi_match, auspi_attach, NULL, NULL);
STATIC int auspi_configure(void *, int, int, int);
STATIC int auspi_transfer(void *, struct spi_transfer *);
STATIC void auspi_done(struct auspi_softc *, int);
STATIC void auspi_send(struct auspi_softc *);
STATIC void auspi_recv(struct auspi_softc *);
STATIC void auspi_sched(struct auspi_softc *);
#define GETREG(sc, x) \
bus_space_read_4(sc->sc_psc.psc_bust, sc->sc_psc.psc_bush, x)
#define PUTREG(sc, x, v) \
bus_space_write_4(sc->sc_psc.psc_bust, sc->sc_psc.psc_bush, x, v)
int
auspi_match(device_t parent, struct cfdata *cf, void *aux)
{
struct aupsc_attach_args *aa = aux;
if (strcmp(aa->aupsc_name, cf->cf_name) != 0)
return 0;
return 1;
}
void
auspi_attach(device_t parent, device_t self, void *aux)
{
struct auspi_softc *sc = device_private(self);
struct aupsc_attach_args *aa = aux;
const struct auspi_machdep *md;
sc->sc_dev = self;
if ((md = auspi_machdep(aa->aupsc_addr)) != NULL) {
sc->sc_md = *md;
}
aprint_normal(": Alchemy PSC SPI protocol\n");
sc->sc_psc = aa->aupsc_ctrl;
sc->sc_spi.sct_cookie = sc;
sc->sc_spi.sct_configure = auspi_configure;
sc->sc_spi.sct_transfer = auspi_transfer;
sc->sc_spi.sct_nslaves = sc->sc_md.am_nslaves;
sc->sc_psc.psc_enable(sc, AUPSC_SEL_SPI);
SIMPLEQ_INIT(&sc->sc_q);
PUTREG(sc, AUPSC_SPIMSK, SPIMSK_ALL);
sc->sc_ih = au_intr_establish(aa->aupsc_irq, 0, IPL_BIO, IST_LEVEL,
auspi_intr, sc);
spibus_attach(self, &sc->sc_spi);
}
int
auspi_configure(void *arg, int slave, int mode, int speed)
{
struct auspi_softc *sc = arg;
int brg, i;
uint32_t reg;
PUTREG(sc, AUPSC_SPIMSK, SPIMSK_NORM);
reg = GETREG(sc, AUPSC_SPICFG);
reg &= ~(SPICFG_BRG_MASK);
reg &= ~(SPICFG_DIV_MASK);
reg &= ~(SPICFG_PSE);
reg &= ~(SPICFG_BI);
reg &= ~(SPICFG_CDE);
reg &= ~(SPICFG_CGE);
reg |= SPICFG_DE;
reg |= SPICFG_DD;
reg |= SPICFG_RT_1;
reg |= SPICFG_TT_1;
reg |= ((8-1) << SPICFG_LEN_SHIFT);
#define BASECLK 24000000
for (brg = 0; brg < 64; brg++) {
if (speed >= (BASECLK / ((brg + 1) * 2))) {
break;
}
}
if (speed < (BASECLK / ((brg + 1) * 2))) {
return EINVAL;
}
reg &= ~SPICFG_BRG_MASK;
reg |= (brg << SPICFG_BRG_SHIFT);
switch (mode) {
case SPI_MODE_0:
reg |= 0;
break;
case SPI_MODE_1:
reg |= SPICFG_CDE;
break;
case SPI_MODE_2:
reg |= SPICFG_BI;
break;
case SPI_MODE_3:
reg |= SPICFG_CDE | SPICFG_BI;
break;
default:
return EINVAL;
}
PUTREG(sc, AUPSC_SPICFG, reg);
for (i = 1000000; i; i -= 10) {
if (GETREG(sc, AUPSC_SPISTAT) & SPISTAT_DR) {
return 0;
}
}
return ETIMEDOUT;
}
void
auspi_send(struct auspi_softc *sc)
{
uint32_t data;
struct spi_chunk *chunk;
while ((chunk = sc->sc_wchunk) != NULL) {
while (chunk->chunk_wresid) {
if (GETREG(sc, AUPSC_SPISTAT) & SPISTAT_TF) {
return;
}
if (chunk->chunk_wptr) {
data = *chunk->chunk_wptr++;
} else {
data = 0;
}
chunk->chunk_wresid--;
if ((chunk->chunk_wresid == 0) &&
(chunk->chunk_next == NULL)) {
data |= SPITXRX_LC;
}
PUTREG(sc, AUPSC_SPITXRX, data);
}
sc->sc_wchunk = sc->sc_wchunk->chunk_next;
}
}
void
auspi_recv(struct auspi_softc *sc)
{
uint32_t data;
struct spi_chunk *chunk;
while ((chunk = sc->sc_rchunk) != NULL) {
while (chunk->chunk_rresid) {
if ((GETREG(sc, AUPSC_SPISTAT) & SPISTAT_RE) != 0) {
return;
}
data = GETREG(sc, AUPSC_SPITXRX);
if (chunk->chunk_rptr) {
*chunk->chunk_rptr++ = data & 0xff;
}
chunk->chunk_rresid--;
}
sc->sc_rchunk = sc->sc_rchunk->chunk_next;
}
}
void
auspi_sched(struct auspi_softc *sc)
{
struct spi_transfer *st;
int err;
while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
spi_transq_dequeue(&sc->sc_q);
sc->sc_transfer = st;
if ((err = auspi_select(sc, st->st_slave)) != 0) {
spi_done(st, err);
continue;
}
PUTREG(sc, AUPSC_SPIPCR, SPIPCR_RC | SPIPCR_TC);
sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
auspi_send(sc);
PUTREG(sc, AUPSC_SPIPCR, SPIPCR_MS);
sc->sc_running = true;
return;
}
auspi_select(sc, -1);
sc->sc_running = false;
}
void
auspi_done(struct auspi_softc *sc, int err)
{
struct spi_transfer *st;
if ((st = sc->sc_transfer) != NULL) {
sc->sc_transfer = NULL;
spi_done(st, err);
}
sc->sc_wchunk = sc->sc_rchunk = NULL;
auspi_sched(sc);
}
int
auspi_intr(void *arg)
{
struct auspi_softc *sc = arg;
uint32_t ev;
int err = 0;
if ((GETREG(sc, AUPSC_SPISTAT) & SPISTAT_DI) == 0) {
return 0;
}
ev = GETREG(sc, AUPSC_SPIEVNT);
if (ev & SPIMSK_MM) {
printf("%s: multiple masters detected!\n",
device_xname(sc->sc_dev));
err = EIO;
}
if (ev & SPIMSK_RO) {
printf("%s: receive overflow\n", device_xname(sc->sc_dev));
err = EIO;
}
if (ev & SPIMSK_TU) {
printf("%s: transmit underflow\n", device_xname(sc->sc_dev));
err = EIO;
}
if (err) {
PUTREG(sc, AUPSC_SPIEVNT,
ev & (SPIMSK_MM | SPIMSK_RO | SPIMSK_TU));
PUTREG(sc, AUPSC_SPIPCR, SPIPCR_RC | SPIPCR_TC);
auspi_done(sc, err);
} else {
auspi_send(sc);
auspi_recv(sc);
if (ev & SPIMSK_MD) {
if ((sc->sc_wchunk != NULL) ||
(sc->sc_rchunk != NULL)) {
printf("%s: partial transfer?\n",
device_xname(sc->sc_dev));
err = EIO;
}
auspi_done(sc, err);
}
PUTREG(sc, AUPSC_SPIEVNT,
ev & (SPIMSK_TR | SPIMSK_RR | SPIMSK_MD));
}
return 1;
}
int
auspi_transfer(void *arg, struct spi_transfer *st)
{
struct auspi_softc *sc = arg;
int s;
s = splbio();
spi_transq_enqueue(&sc->sc_q, st);
if (sc->sc_running == 0) {
auspi_sched(sc);
}
splx(s);
return 0;
}