#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: gttwsi_core.c,v 1.20 2025/09/25 13:44:31 thorpej Exp $");
#include "locators.h"
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/condvar.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/mutex.h>
#include <sys/systm.h>
#include <dev/i2c/i2cvar.h>
#include <dev/i2c/gttwsireg.h>
#include <dev/i2c/gttwsivar.h>
static int gttwsi_send_start(void *v, int flags);
static int gttwsi_send_stop(void *v, int flags);
static int gttwsi_initiate_xfer(void *v, i2c_addr_t addr, int flags);
static int gttwsi_read_byte(void *v, uint8_t *valp, int flags);
static int gttwsi_write_byte(void *v, uint8_t val, int flags);
static int gttwsi_wait(struct gttwsi_softc *, uint32_t, uint32_t,
uint32_t, int, const char *);
uint32_t
gttwsi_read_4(struct gttwsi_softc *sc, uint32_t reg)
{
const uint32_t val = bus_space_read_4(sc->sc_bust, sc->sc_bush,
sc->sc_regmap[reg]);
#ifdef TWSI_DEBUG
printf("I2C:R:[%" PRIu32 "]%02" PRIxBUSSIZE ":%02" PRIx32 "\n", reg, sc->sc_regmap[reg], val);
#else
DELAY(TWSI_READ_DELAY);
#endif
return val;
}
void
gttwsi_write_4(struct gttwsi_softc *sc, uint32_t reg, uint32_t val)
{
bus_space_write_4(sc->sc_bust, sc->sc_bush, sc->sc_regmap[reg], val);
#ifdef TWSI_DEBUG
printf("I2C:W:[%" PRIu32 "]%02" PRIxBUSSIZE ":%02" PRIx32 "\n", reg, sc->sc_regmap[reg], val);
#else
DELAY(TWSI_WRITE_DELAY);
#endif
}
void
gttwsi_attach_subr(device_t self, bus_space_tag_t iot, bus_space_handle_t ioh,
const bus_size_t *regmap)
{
struct gttwsi_softc * const sc = device_private(self);
aprint_naive("\n");
aprint_normal(": Marvell TWSI controller\n");
sc->sc_dev = self;
sc->sc_bust = iot;
sc->sc_bush = ioh;
sc->sc_regmap = regmap;
mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_BIO);
cv_init(&sc->sc_cv, device_xname(self));
sc->sc_started = false;
iic_tag_init(&sc->sc_i2c);
sc->sc_i2c.ic_cookie = sc;
sc->sc_i2c.ic_send_start = gttwsi_send_start;
sc->sc_i2c.ic_send_stop = gttwsi_send_stop;
sc->sc_i2c.ic_initiate_xfer = gttwsi_initiate_xfer;
sc->sc_i2c.ic_read_byte = gttwsi_read_byte;
sc->sc_i2c.ic_write_byte = gttwsi_write_byte;
gttwsi_write_4(sc, TWSI_SOFTRESET, SOFTRESET_VAL);
}
void
gttwsi_config_children(device_t self)
{
struct gttwsi_softc * const sc = device_private(self);
iicbus_attach(self, &sc->sc_i2c);
}
int
gttwsi_intr(void *arg)
{
struct gttwsi_softc *sc = arg;
uint32_t val;
mutex_enter(&sc->sc_mtx);
val = gttwsi_read_4(sc, TWSI_CONTROL);
if (val & CONTROL_IFLG) {
gttwsi_write_4(sc, TWSI_CONTROL, val & ~CONTROL_INTEN);
cv_broadcast(&sc->sc_cv);
mutex_exit(&sc->sc_mtx);
return 1;
}
mutex_exit(&sc->sc_mtx);
return 0;
}
static int
gttwsi_send_start(void *v, int flags)
{
struct gttwsi_softc *sc = v;
int expect;
if (sc->sc_started)
expect = STAT_RSCT;
else
expect = STAT_SCT;
sc->sc_started = true;
return gttwsi_wait(sc, CONTROL_START, expect, 0, flags, "send-start");
}
static int
gttwsi_send_stop(void *v, int flags)
{
struct gttwsi_softc *sc = v;
int retry = TWSI_RETRY_COUNT;
uint32_t control, status;
sc->sc_started = false;
control = CONTROL_STOP | CONTROL_TWSIEN;
if (sc->sc_iflg_rwc)
control |= CONTROL_IFLG;
gttwsi_write_4(sc, TWSI_CONTROL, control);
while (retry > 0) {
if ((status = gttwsi_read_4(sc, TWSI_STATUS)) == STAT_NRS)
return 0;
retry--;
DELAY(TWSI_STAT_DELAY);
}
aprint_error_dev(sc->sc_dev, "send STOP failed, status=0x%02x\n",
status);
return EWOULDBLOCK;
}
static int
gttwsi_initiate_xfer(void *v, i2c_addr_t addr, int flags)
{
struct gttwsi_softc *sc = v;
uint32_t data, expect, alt;
int error, read;
error = gttwsi_send_start(v, flags);
if (error)
return error;
read = (flags & I2C_F_READ) != 0;
if (read) {
expect = STAT_ARBT_AR;
alt = STAT_ARBT_ANR;
} else {
expect = STAT_AWBT_AR;
alt = STAT_AWBT_ANR;
}
data = read;
if (addr > 0x7f) {
data |= 0xf0 | ((addr & 0x300) >> 7);
gttwsi_write_4(sc, TWSI_DATA, data);
error = gttwsi_wait(sc, 0, expect, alt, flags, "send-addr-10");
if (error)
return error;
if (read) {
expect = STAT_SARBT_AR;
alt = STAT_SARBT_ANR;
} else {
expect = STAT_SAWBT_AR;
alt = STAT_SAWBT_ANR;
}
data = (uint8_t)addr;
} else
data |= (addr << 1);
gttwsi_write_4(sc, TWSI_DATA, data);
return gttwsi_wait(sc, 0, expect, alt, flags, "send-addr");
}
static int
gttwsi_read_byte(void *v, uint8_t *valp, int flags)
{
struct gttwsi_softc *sc = v;
int error;
if (flags & I2C_F_LAST) {
error = gttwsi_wait(sc, 0, STAT_MRRD_ANT, 0, flags,
"read-last-byte");
} else {
error = gttwsi_wait(sc, CONTROL_ACK, STAT_MRRD_AT, 0, flags,
"read-byte");
}
if (!error)
*valp = gttwsi_read_4(sc, TWSI_DATA);
if ((flags & (I2C_F_LAST | I2C_F_STOP)) == (I2C_F_LAST | I2C_F_STOP))
error = gttwsi_send_stop(sc, flags);
return error;
}
static int
gttwsi_write_byte(void *v, uint8_t val, int flags)
{
struct gttwsi_softc *sc = v;
int error;
gttwsi_write_4(sc, TWSI_DATA, val);
error = gttwsi_wait(sc, 0, STAT_MTDB_AR, 0, flags, "write-byte");
if (flags & I2C_F_STOP)
gttwsi_send_stop(sc, flags);
return error;
}
static int
gttwsi_wait(struct gttwsi_softc *sc, uint32_t control, uint32_t expect,
uint32_t alt, int flags, const char *what)
{
uint32_t status;
int timo, error = 0;
flags |= I2C_F_POLL;
DELAY(5);
if (!(flags & I2C_F_POLL))
control |= CONTROL_INTEN;
if (sc->sc_iflg_rwc)
control |= CONTROL_IFLG;
mutex_enter(&sc->sc_mtx);
gttwsi_write_4(sc, TWSI_CONTROL, control | CONTROL_TWSIEN);
timo = 0;
for (;;) {
control = gttwsi_read_4(sc, TWSI_CONTROL);
if (control & CONTROL_IFLG)
break;
if (!(flags & I2C_F_POLL)) {
error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz);
if (error) {
break;
}
} else {
DELAY(TWSI_RETRY_DELAY);
if (timo++ > 1000000)
break;
}
}
if ((control & CONTROL_IFLG) == 0) {
if (flags & I2C_F_POLL) {
error = EWOULDBLOCK;
} else {
KASSERT(error != 0);
}
aprint_error_dev(sc->sc_dev,
"gttwsi_wait(): %s timeout%s, control=0x%x, error=%d\n",
what, (flags & I2C_F_POLL) ? " (polled)" : "",
control, error);
goto end;
}
status = gttwsi_read_4(sc, TWSI_STATUS);
if (status != expect) {
if (alt != 0 && status != alt)
aprint_error_dev(sc->sc_dev,
"unexpected status 0x%x: expect 0x%x\n", status,
expect);
error = EIO;
}
end:
mutex_exit(&sc->sc_mtx);
return error;
}