#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: bcm2835_bsc.c,v 1.16 2024/02/16 15:11:38 skrll Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/kernhist.h>
#include <sys/intr.h>
#include <sys/mutex.h>
#include <sys/systm.h>
#include <dev/i2c/i2cvar.h>
#include <arm/broadcom/bcm2835reg.h>
#include <arm/broadcom/bcm2835_bscreg.h>
#include <arm/broadcom/bcm2835_bscvar.h>
static void bsciic_exec_func_idle(struct bsciic_softc * const);
static void bsciic_exec_func_send_addr(struct bsciic_softc * const);
static void bsciic_exec_func_send_cmd(struct bsciic_softc * const);
static void bsciic_exec_func_send_data(struct bsciic_softc * const);
static void bsciic_exec_func_recv_data(struct bsciic_softc * const);
static void bsciic_exec_func_done(struct bsciic_softc * const);
static void bsciic_exec_func_error(struct bsciic_softc * const);
const struct {
void (*func)(struct bsciic_softc * const);
uint32_t c_bits;
uint32_t s_bits;
} bsciic_exec_state_data[] = {
[BSC_EXEC_STATE_IDLE] = {
.func = bsciic_exec_func_idle,
},
[BSC_EXEC_STATE_SEND_ADDR] = {
.func = bsciic_exec_func_send_addr,
},
[BSC_EXEC_STATE_SEND_CMD] = {
.func = bsciic_exec_func_send_cmd,
.c_bits = BSC_C_INTT,
.s_bits = BSC_S_TXW,
},
[BSC_EXEC_STATE_SEND_DATA] = {
.func = bsciic_exec_func_send_data,
.c_bits = BSC_C_INTT,
.s_bits = BSC_S_TXW,
},
[BSC_EXEC_STATE_RECV_DATA] = {
.func = bsciic_exec_func_recv_data,
.c_bits = BSC_C_READ | BSC_C_INTR,
.s_bits = BSC_S_RXR,
},
[BSC_EXEC_STATE_DONE] = {
.func = bsciic_exec_func_done,
},
[BSC_EXEC_STATE_ERROR] = {
.func = bsciic_exec_func_error,
},
};
int bsciic_debug = 0;
void
bsciic_attach(struct bsciic_softc *sc)
{
mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
cv_init(&sc->sc_intr_wait, device_xname(sc->sc_dev));
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
BSC_S_ERR | BSC_S_DONE);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, 0);
u_int divider = howmany(sc->sc_frequency, sc->sc_clkrate);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DIV,
__SHIFTIN(divider, BSC_DIV_CDIV));
}
int
bsciic_acquire_bus(void *v, int flags)
{
struct bsciic_softc * const sc = v;
uint32_t s __diagused;
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
BSC_S_ERR | BSC_S_DONE);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_I2CEN |
BSC_C_CLEAR_CLEAR);
s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
KASSERT((s & BSC_S_TA) == 0);
return 0;
}
void
bsciic_release_bus(void *v, int flags)
{
struct bsciic_softc * const sc = v;
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
}
static void
bsciic_exec_lock(struct bsciic_softc * const sc)
{
if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
mutex_enter(&sc->sc_intr_lock);
}
}
static void
bsciic_exec_unlock(struct bsciic_softc * const sc)
{
if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
mutex_exit(&sc->sc_intr_lock);
}
}
static void
bsciic_txfill(struct bsciic_softc * const sc)
{
uint32_t s;
while (sc->sc_bufpos != sc->sc_buflen) {
s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
if ((s & BSC_S_TXD) == 0)
break;
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO,
sc->sc_buf[sc->sc_bufpos++]);
}
}
static void
bsciic_rxdrain(struct bsciic_softc * const sc)
{
uint32_t s;
while (sc->sc_bufpos != sc->sc_buflen) {
s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
if ((s & BSC_S_RXD) == 0)
break;
sc->sc_buf[sc->sc_bufpos++] =
(uint8_t)bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO);
}
}
static bsc_exec_state_t
bsciic_next_state(struct bsciic_softc * const sc)
{
switch (sc->sc_exec_state) {
case BSC_EXEC_STATE_IDLE:
if (sc->sc_exec.addr > 0x7f) {
return BSC_EXEC_STATE_SEND_ADDR;
}
case BSC_EXEC_STATE_SEND_ADDR:
if (sc->sc_exec.cmdlen) {
return BSC_EXEC_STATE_SEND_CMD;
}
case BSC_EXEC_STATE_SEND_CMD:
if (sc->sc_exec.datalen == 0) {
return BSC_EXEC_STATE_DONE;
}
if (I2C_OP_READ_P(sc->sc_exec.op)) {
return BSC_EXEC_STATE_RECV_DATA;
}
return BSC_EXEC_STATE_SEND_DATA;
case BSC_EXEC_STATE_SEND_DATA:
case BSC_EXEC_STATE_RECV_DATA:
return BSC_EXEC_STATE_DONE;
case BSC_EXEC_STATE_DONE:
case BSC_EXEC_STATE_ERROR:
return sc->sc_exec_state;
}
panic("bsciic_next_state: invalid state: %d", sc->sc_exec_state);
}
#define BSC_EXEC_PHASE_COMPLETE(sc) \
((sc)->sc_exec_state == BSC_EXEC_STATE_ERROR || \
(sc)->sc_bufpos == (sc)->sc_buflen)
static void
bsciic_signal(struct bsciic_softc * const sc)
{
if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
cv_signal(&sc->sc_intr_wait);
}
}
static void
bsciic_phase_done(struct bsciic_softc * const sc)
{
sc->sc_exec_state = bsciic_next_state(sc);
(*bsciic_exec_state_data[sc->sc_exec_state].func)(sc);
}
static void
bsciic_abort(struct bsciic_softc * const sc)
{
sc->sc_exec_state = BSC_EXEC_STATE_ERROR;
bsciic_phase_done(sc);
}
int
bsciic_intr(void *v)
{
struct bsciic_softc * const sc = v;
uint32_t s;
bsciic_exec_lock(sc);
if ((sc->sc_exec.flags & I2C_F_POLL) == 0 &&
sc->sc_expecting_interrupt == false) {
bsciic_exec_unlock(sc);
return 0;
}
s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S,
BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
if (s & (BSC_S_CLKT | BSC_S_ERR)) {
if ((s & BSC_S_CLKT) ||
(bsciic_debug && (s & BSC_S_ERR))) {
device_printf(sc->sc_dev,
"error s=0x%08x, aborting transfer\n", s);
}
bsciic_abort(sc);
goto out;
}
if (BSC_EXEC_STATE_SENDING(sc)) {
if (BSC_EXEC_PHASE_COMPLETE(sc)) {
bsciic_phase_done(sc);
} else {
bsciic_txfill(sc);
}
} else if (BSC_EXEC_STATE_RECEIVING(sc)) {
bsciic_rxdrain(sc);
if (BSC_EXEC_PHASE_COMPLETE(sc)) {
bsciic_phase_done(sc);
}
} else {
device_printf(sc->sc_dev,
"unexpected interrupt: state=%d s=0x%08x\n",
sc->sc_exec_state, s);
bsciic_abort(sc);
}
out:
bsciic_exec_unlock(sc);
return (1);
}
static void
bsciic_wait(struct bsciic_softc * const sc, const uint32_t events)
{
if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
return;
}
const uint32_t s_bits =
events | BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE;
uint32_t s;
for (;;) {
s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
if (s & s_bits) {
(void) bsciic_intr(sc);
}
if (BSC_EXEC_PHASE_COMPLETE(sc)) {
bsciic_phase_done(sc);
}
if (sc->sc_exec_state >= BSC_EXEC_STATE_DONE) {
return;
}
delay(1);
}
}
static void
bsciic_start(struct bsciic_softc * const sc)
{
sc->sc_c_bits = BSC_C_I2CEN | BSC_C_INTD |
bsciic_exec_state_data[sc->sc_exec_state].c_bits;
if (sc->sc_exec.flags & I2C_F_POLL) {
sc->sc_c_bits &= ~(BSC_C_INTD | BSC_C_INTT | BSC_C_INTR);
}
sc->sc_expecting_interrupt =
(sc->sc_c_bits & (BSC_C_INTD | BSC_C_INTT | BSC_C_INTR)) ? true
: false;
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C,
sc->sc_c_bits | BSC_C_ST);
bsciic_wait(sc, bsciic_exec_state_data[sc->sc_exec_state].s_bits);
}
static void
bsciic_exec_func_idle(struct bsciic_softc * const sc)
{
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_A, sc->sc_exec.addr);
bsciic_phase_done(sc);
}
static void
bsciic_exec_func_send_addr(struct bsciic_softc * const sc)
{
panic("bsciic_exec_func_send_addr is not supposed to be called");
}
static void
bsciic_exec_func_send_cmd(struct bsciic_softc * const sc)
{
sc->sc_buf = __UNCONST(sc->sc_exec.cmdbuf);
sc->sc_bufpos = 0;
sc->sc_buflen = sc->sc_exec.cmdlen;
uint32_t dlen = sc->sc_exec.cmdlen;
if (! I2C_OP_READ_P(sc->sc_exec.op)) {
dlen += sc->sc_exec.datalen;
}
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
bsciic_start(sc);
}
static void
bsciic_exec_func_send_data(struct bsciic_softc * const sc)
{
sc->sc_buf = sc->sc_exec.databuf;
sc->sc_bufpos = 0;
sc->sc_buflen = sc->sc_exec.datalen;
if (sc->sc_exec.cmdlen) {
bsciic_wait(sc, BSC_S_TXW);
} else {
uint32_t dlen = sc->sc_exec.datalen;
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
bsciic_start(sc);
}
}
static void
bsciic_exec_func_recv_data(struct bsciic_softc * const sc)
{
sc->sc_buf = sc->sc_exec.databuf;
sc->sc_bufpos = 0;
sc->sc_buflen = sc->sc_exec.datalen;
uint32_t dlen = sc->sc_exec.datalen;
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
bsciic_start(sc);
}
static void
bsciic_exec_func_done(struct bsciic_softc * const sc)
{
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_I2CEN);
sc->sc_expecting_interrupt = false;
bsciic_signal(sc);
}
static void
bsciic_exec_func_error(struct bsciic_softc * const sc)
{
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C,
BSC_C_I2CEN | BSC_C_CLEAR_CLEAR);
sc->sc_expecting_interrupt = false;
bsciic_signal(sc);
}
int
bsciic_exec(void *v, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
size_t cmdlen, void *databuf, size_t datalen, int flags)
{
struct bsciic_softc * const sc = v;
if (addr > 0x7f)
return (ENOTSUP);
KASSERT(sc->sc_exec_state == BSC_EXEC_STATE_IDLE);
sc->sc_exec.op = op;
sc->sc_exec.addr = addr;
sc->sc_exec.cmdbuf = cmdbuf;
sc->sc_exec.cmdlen = cmdlen;
sc->sc_exec.databuf = databuf;
sc->sc_exec.datalen = datalen;
sc->sc_exec.flags = flags;
bsciic_exec_lock(sc);
(*bsciic_exec_state_data[sc->sc_exec_state].func)(sc);
while (sc->sc_exec_state < BSC_EXEC_STATE_DONE) {
KASSERT((flags & I2C_F_POLL) == 0);
cv_wait(&sc->sc_intr_wait, &sc->sc_intr_lock);
}
int error = sc->sc_exec_state == BSC_EXEC_STATE_ERROR ? EIO : 0;
uint32_t s;
do {
s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
} while ((s & BSC_S_TA) != 0);
if (s & (BSC_S_CLKT | BSC_S_ERR)) {
error = EIO;
}
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C,
BSC_C_I2CEN | BSC_C_CLEAR_CLEAR);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S,
BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, 0);
bsciic_exec_unlock(sc);
sc->sc_exec.flags = 0;
sc->sc_exec_state = BSC_EXEC_STATE_IDLE;
memset(&sc->sc_exec, 0, sizeof(sc->sc_exec));
return error;
}