Skip to content

Commit

Permalink
twsi: another big update
Browse files Browse the repository at this point in the history
- bulk of twsi_transfer and twsi_intr are protected with the mutex to
  ensure a consistent view of the state;
- use msleep_sbt instead of pause_sbt, so that wakeup has any effect;
- call wakeup in a single place;
- remove most of redundant writes of the control register;
  the hardware executes a next stage of I2C protocol when the interrupt
  flag is cleared, so it should be easier to reason about the hardware
  state and behavior when the flag is cleared in a single place;
- detect more unexpected conditions;
- support more message combinations including:
  - transfers with no data bytes (S + addr + P)
  - read message with NO_STOP flag;
- better structure the code to handle protocol transitions;
- improve diagnostic messages and code formatting.

Tested with: icee, ds1307, htu21, pcf8591, max44009
  • Loading branch information
avg-I committed Sep 23, 2021
1 parent 4fc6103 commit 0075e71
Showing 1 changed file with 131 additions and 101 deletions.
232 changes: 131 additions & 101 deletions sys/dev/iicbus/twsi/twsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,18 +493,19 @@ twsi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
{
struct twsi_softc *sc;
int i;
int error;

sc = device_get_softc(dev);

if (!sc->have_intr)
return (iicbus_transfer_gen(dev, msgs, nmsgs));

sc->control_val = TWSI_CONTROL_TWSIEN |
TWSI_CONTROL_INTEN | TWSI_CONTROL_ACK;
TWSI_WRITE(sc, sc->reg_control, sc->control_val);
debugf(dev, "transmitting %d messages\n", nmsgs);
debugf(sc->dev, "status=%x\n", TWSI_READ(sc, sc->reg_status));

mtx_lock(&sc->mutex);
KASSERT(sc->transfer == 0,
("starting a transfer while another is active"));
sc->nmsgs = nmsgs;
sc->msgs = msgs;
sc->msg_idx = 0;
Expand All @@ -515,12 +516,12 @@ twsi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
for (int i = 0; i < nmsgs; i++)
debugf(sc->dev, "msg %d is %d bytes long\n", i, msgs[i].len);
#endif

/* Send start and re-enable interrupts */
sc->control_val = TWSI_CONTROL_TWSIEN |
TWSI_CONTROL_INTEN | TWSI_CONTROL_ACK;
sc->control_val = TWSI_CONTROL_TWSIEN | TWSI_CONTROL_INTEN;
TWSI_WRITE(sc, sc->reg_control, sc->control_val | TWSI_CONTROL_START);
for (i = 0; i < 100 && sc->error == 0 && sc->transfer != 0; i++) {
pause_sbt("twsi", SBT_1MS * 30, SBT_1MS, 0);
msleep_sbt(sc, &sc->mutex, 0, "twsi", SBT_1MS * 30, SBT_1MS, 0);
}
if (sc->error == 0 && sc->transfer != 0) {
device_printf(sc->dev, "transfer timeout\n");
Expand All @@ -529,200 +530,200 @@ twsi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
}
debugf(sc->dev, "pause finish\n");

if (sc->error) {
debugf(sc->dev, "Error, aborting (%d)\n", sc->error);
TWSI_WRITE(sc, sc->reg_control, 0);
}
if (sc->error != 0)
debugf(sc->dev, "Error: %d\n", sc->error);

/* Disable module and interrupts */
debugf(sc->dev, "status=%x\n", TWSI_READ(sc, sc->reg_status));
TWSI_WRITE(sc, sc->reg_control, 0);
debugf(sc->dev, "status=%x\n", TWSI_READ(sc, sc->reg_status));
error = sc->error;
mtx_unlock(&sc->mutex);

return (sc->error);
return (error);
}

static void
twsi_error(struct twsi_softc *sc, int err)
{
/*
* Must send stop condition to abort the current transfer and
* signal the caller.
* Must send stop condition to abort the current transfer.
*/
debugf(sc->dev, "Sending STOP condition for error %d\n", err);
sc->transfer = 0;
sc->error = err;
sc->control_val = 0;
TWSI_WRITE(sc, sc->reg_control, sc->control_val | TWSI_CONTROL_STOP);
wakeup(sc);
}

static void
twsi_intr(void *arg)
{
struct twsi_softc *sc;
uint32_t status;
int transfer_done = 0;
int send_byte = 0;
bool message_done;
bool send_start;

sc = arg;
message_done = false;
send_start = false;

debugf(sc->dev, "Got interrupt Current msg=%u\n", sc->msg_idx);
mtx_lock(&sc->mutex);
debugf(sc->dev, "Got interrupt, current msg=%u\n", sc->msg_idx);

status = TWSI_READ(sc, sc->reg_status);
debugf(sc->dev, "reg control=%x\n", TWSI_READ(sc, sc->reg_control));
debugf(sc->dev, "reg control = 0x%x, status = 0x%x\n",
TWSI_READ(sc, sc->reg_control), status);

if (sc->transfer == 0) {
device_printf(sc->dev, "interrupt without active transfer, "
"status = 0x%x\n", status);
TWSI_WRITE(sc, sc->reg_control, sc->control_val |
TWSI_CONTROL_STOP);
goto end;
}

restart:
switch (status) {
case TWSI_STATUS_START:
case TWSI_STATUS_RPTD_START:
/* Transmit the address */
debugf(sc->dev, "Send the address %x\n", sc->msgs[sc->msg_idx].slave);
debugf(sc->dev, "Send address 0x%x\n",
sc->msgs[sc->msg_idx].slave);

if (sc->msgs[sc->msg_idx].flags & IIC_M_RD)
TWSI_WRITE(sc, sc->reg_data,
sc->msgs[sc->msg_idx].slave | LSB);
else
TWSI_WRITE(sc, sc->reg_data,
sc->msgs[sc->msg_idx].slave & ~LSB);
TWSI_WRITE(sc, sc->reg_control, sc->control_val);
break;

case TWSI_STATUS_ADDR_W_ACK:
debugf(sc->dev, "ACK received after transmitting the address (write)\n");
/* Directly send the first byte */
sc->sent_bytes = 1;
debugf(sc->dev, "Sending byte 0 (of %d) = %x\n",
sc->msgs[sc->msg_idx].len,
sc->msgs[sc->msg_idx].buf[0]);
TWSI_WRITE(sc, sc->reg_data, sc->msgs[sc->msg_idx].buf[0]);
debugf(sc->dev, "Address ACK-ed (write)\n");

if (sc->msgs[sc->msg_idx].len > 0) {
/* Directly send the first byte */
sc->sent_bytes = 1;
debugf(sc->dev, "Sending byte 0 (of %d) = %x\n",
sc->msgs[sc->msg_idx].len,
sc->msgs[sc->msg_idx].buf[0]);
TWSI_WRITE(sc, sc->reg_data,
sc->msgs[sc->msg_idx].buf[0]);
} else {
debugf(sc->dev, "Zero-length write, sending STOP\n");
TWSI_WRITE(sc, sc->reg_control,
sc->control_val | TWSI_CONTROL_STOP);
}

TWSI_WRITE(sc, sc->reg_control, sc->control_val);
break;

case TWSI_STATUS_ADDR_R_ACK:
debugf(sc->dev, "ACK received after transmitting the address (read)\n");
debugf(sc->dev, "Address ACK-ed (read)\n");
sc->recv_bytes = 0;

if (sc->msgs[sc->msg_idx].len == 1)
if (sc->msgs[sc->msg_idx].len == 0) {
debugf(sc->dev, "Zero-length read, sending STOP\n");
TWSI_WRITE(sc, sc->reg_control,
sc->control_val | TWSI_CONTROL_STOP);
} else if (sc->msgs[sc->msg_idx].len == 1) {
sc->control_val &= ~TWSI_CONTROL_ACK;
else
} else {
sc->control_val |= TWSI_CONTROL_ACK;
TWSI_WRITE(sc, sc->reg_control, sc->control_val);
}
break;

case TWSI_STATUS_ADDR_W_NACK:
case TWSI_STATUS_ADDR_R_NACK:
debugf(sc->dev, "NACK received after transmitting the address\n");
debugf(sc->dev, "Address NACK-ed\n");
twsi_error(sc, IIC_ENOACK);
break;
case TWSI_STATUS_DATA_WR_NACK:
debugf(sc->dev, "NACK received after transmitting data byte\n");
debugf(sc->dev, "Data byte NACK-ed\n");
twsi_error(sc, IIC_ENOACK);
wakeup(sc);
break;
case TWSI_STATUS_DATA_WR_ACK:
KASSERT(sc->sent_bytes <= sc->msgs[sc->msg_idx].len,
("impossible sent_bytes value"));
("sent_bytes beyond message length"));
debugf(sc->dev, "ACK received after transmitting data\n");
if (sc->sent_bytes == sc->msgs[sc->msg_idx].len) {
debugf(sc->dev, "Done sending all the bytes for msg %d\n", sc->msg_idx);
send_byte = 0;
debugf(sc->dev, "Done TX data\n");

/* Send stop, no interrupts on stop */
if (!(sc->msgs[sc->msg_idx].flags & IIC_M_NOSTOP)) {
debugf(sc->dev, "Done TX data, send stop\n");
debugf(sc->dev, "Send STOP\n");
TWSI_WRITE(sc, sc->reg_control,
sc->control_val | TWSI_CONTROL_STOP);
} else {
debugf(sc->dev, "Done TX data with NO_STOP\n");
}
sc->msg_idx++;
if (sc->msg_idx == sc->nmsgs) {
debugf(sc->dev, "All messages transmitted\n");
transfer_done = 1;
sc->error = 0;
} else if (!(sc->msgs[sc->msg_idx].flags & IIC_M_NOSTART)) {
debugf(sc->dev, "Send (repeated) start\n");
TWSI_WRITE(sc, sc->reg_control, sc->control_val | TWSI_CONTROL_START);
} else {
/* Just keep sending data. */
KASSERT((sc->msgs[sc->msg_idx].flags & IIC_M_RD) == 0,
("read with nostart after write with nostop"));
debugf(sc->dev, "write followed by write\n");
sc->sent_bytes = 0;
send_byte = 1;
debugf(sc->dev, "NOSTOP flag\n");
}
} else {
send_byte = 1;
message_done = true;
break;
}

if (send_byte) {
debugf(sc->dev, "Sending byte %d (of %d) = %x\n",
sc->sent_bytes,
sc->msgs[sc->msg_idx].len,
sc->msgs[sc->msg_idx].buf[sc->sent_bytes]);
TWSI_WRITE(sc, sc->reg_data,
sc->msgs[sc->msg_idx].buf[sc->sent_bytes]);
TWSI_WRITE(sc, sc->reg_control,
sc->control_val);
sc->sent_bytes++;
}
debugf(sc->dev, "Sending byte %d (of %d) = 0x%x\n",
sc->sent_bytes,
sc->msgs[sc->msg_idx].len,
sc->msgs[sc->msg_idx].buf[sc->sent_bytes]);
TWSI_WRITE(sc, sc->reg_data,
sc->msgs[sc->msg_idx].buf[sc->sent_bytes]);
sc->sent_bytes++;
break;

case TWSI_STATUS_DATA_RD_ACK:
debugf(sc->dev, "Received and ACK-ed data\n");
KASSERT(sc->recv_bytes < sc->msgs[sc->msg_idx].len,
("receiving beyond the end of buffer"));

sc->msgs[sc->msg_idx].buf[sc->recv_bytes++] = TWSI_READ(sc, sc->reg_data);
debugf(sc->dev, "msg_len=%d recv_bytes=%d\n", sc->msgs[sc->msg_idx].len, sc->recv_bytes);
sc->msgs[sc->msg_idx].buf[sc->recv_bytes] =
TWSI_READ(sc, sc->reg_data);
debugf(sc->dev, "Received byte %d (of %d) = 0x%x\n",
sc->recv_bytes,
sc->msgs[sc->msg_idx].len,
sc->msgs[sc->msg_idx].buf[sc->recv_bytes]);
sc->recv_bytes++;

/* If we only have one byte left, disable ACK */
if (sc->msgs[sc->msg_idx].len - sc->recv_bytes == 1)
if (sc->msgs[sc->msg_idx].len - sc->recv_bytes == 1) {
sc->control_val &= ~TWSI_CONTROL_ACK;
if (sc->msgs[sc->msg_idx].len == sc->recv_bytes) {
debugf(sc->dev, "Done with msg %d but ACKed for more data\n",
sc->msg_idx);
sc->msg_idx++;
if (sc->msg_idx == sc->nmsgs - 1) {
debugf(sc->dev, "No more msgs\n");
transfer_done = 1;
sc->error = 0;
}
} else if (sc->msgs[sc->msg_idx].len == sc->recv_bytes) {
/*
* We should not have ACK-ed the last byte.
* The protocol state machine is in invalid state.
*/
debugf(sc->dev, "RX all but asked for more?\n");
twsi_error(sc, IIC_ESTATUS);
}
TWSI_WRITE(sc, sc->reg_control, sc->control_val);
break;

case TWSI_STATUS_DATA_RD_NOACK:
debugf(sc->dev, "Received and NACK-ed data\n");
KASSERT(sc->recv_bytes < sc->msgs[sc->msg_idx].len,
("receiving beyond the end of buffer"));
KASSERT(sc->recv_bytes == sc->msgs[sc->msg_idx].len - 1,
("sent NACK before receiving all requested data"));
if (sc->msgs[sc->msg_idx].len - sc->recv_bytes == 1) {
sc->msgs[sc->msg_idx].buf[sc->recv_bytes++] = TWSI_READ(sc, sc->reg_data);
sc->msgs[sc->msg_idx].buf[sc->recv_bytes] =
TWSI_READ(sc, sc->reg_data);
debugf(sc->dev, "Received byte %d (of %d) = 0x%x\n",
sc->recv_bytes,
sc->msgs[sc->msg_idx].len,
sc->msgs[sc->msg_idx].buf[sc->recv_bytes]);
sc->recv_bytes++;

if (sc->msgs[sc->msg_idx].len == sc->recv_bytes) {
debugf(sc->dev, "Done RX data\n");
if (!(sc->msgs[sc->msg_idx].flags & IIC_M_NOSTOP)) {
debugf(sc->dev, "send stop\n");
debugf(sc->dev, "Send STOP\n");
TWSI_WRITE(sc, sc->reg_control,
sc->control_val | TWSI_CONTROL_STOP);
}
message_done = true;
} else {
debugf(sc->dev, "NACK-ed too early\n");
if (!(sc->msgs[sc->msg_idx].flags & IIC_M_NOSTOP))
debugf(sc->dev, "sending stop anyway\n");
TWSI_WRITE(sc, sc->reg_control,
sc->control_val | TWSI_CONTROL_STOP);
/*
* We should not have NACK-ed yet.
* The protocol state machine is in invalid state.
*/
debugf(sc->dev, "NACK-ed before receving all bytes?\n");
twsi_error(sc, IIC_ESTATUS);
}
sc->transfer = 0;
transfer_done = 1;
sc->error = 0;
break;

case TWSI_STATUS_BUS_ERROR:
Expand All @@ -738,19 +739,48 @@ twsi_intr(void *arg)
twsi_error(sc, IIC_ESTATUS);
break;
}
debugf(sc->dev, "Refresh reg_control\n");

if (message_done) {
sc->msg_idx++;
if (sc->msg_idx == sc->nmsgs) {
debugf(sc->dev, "All messages transmitted\n");
sc->transfer = 0;
sc->error = 0;
} else if ((sc->msgs[sc->msg_idx].flags & IIC_M_NOSTART) == 0) {
debugf(sc->dev, "Send (repeated) start\n");
send_start = true;
} else {
/* Just keep transmitting data. */
KASSERT((sc->msgs[sc->msg_idx - 1].flags & IIC_M_NOSTOP) != 0,
("NOSTART message after STOP"));
KASSERT((sc->msgs[sc->msg_idx].flags & IIC_M_RD) ==
(sc->msgs[sc->msg_idx - 1].flags & IIC_M_RD),
("change of transfer direction without a START"));
debugf(sc->dev, "NOSTART message after NOSTOP\n");
sc->sent_bytes = 0;
sc->recv_bytes = 0;
if ((sc->msgs[sc->msg_idx].flags & IIC_M_RD) == 0) {
status = TWSI_STATUS_ADDR_W_ACK;
goto restart;
} else {
debugf(sc->dev, "Read+NOSTART unsupported\n");
twsi_error(sc, IIC_ESTATUS);
}
}
}
end:
/*
* Newer Allwinner chips clear IFLG after writing 1 to it.
*/
debugf(sc->dev, "Refresh reg_control\n");
TWSI_WRITE(sc, sc->reg_control, sc->control_val |
(sc->iflag_w1c ? TWSI_CONTROL_IFLG : 0));
(sc->iflag_w1c ? TWSI_CONTROL_IFLG : 0) |
(send_start ? TWSI_CONTROL_START : 0));

debugf(sc->dev, "Done with interrupts\n");
if (transfer_done == 1) {
sc->transfer = 0;
debugf(sc->dev, "Done with interrupt, transfer = %d\n", sc->transfer);
if (sc->transfer == 0)
wakeup(sc);
}
mtx_unlock(&sc->mutex);
}

static void
Expand Down

0 comments on commit 0075e71

Please sign in to comment.