Skip to content

Commit 90b632e

Browse files
committed
[ot] hw/opentitan: ot_spi_host: add a SPI command state.
Replace previous boolean-based state with an enumeration, as non-executing command can be either about to execute or in finalization stage. This enables differentiation of zero length command which can either be a 1-byte command to be executed, or a command whose all bytes have been processed. Signed-off-by: Emmanuel Blot <eblot@rivosinc.com> (cherry picked from commit 98a555a1449856df66c331de05d9338aac5fc096)
1 parent 5489358 commit 90b632e

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

hw/opentitan/ot_spi_host.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,22 @@ struct TxFifo {
369369
uint32_t num;
370370
};
371371

372+
enum CmdState {
373+
CMD_SCHEDULED, /* command scheduled for execution, not yet handled */
374+
CMD_ONGOING, /* command is being executed, not yet completed */
375+
CMD_EXECUTED, /* commmand has been executed, need to be popped out */
376+
};
377+
372378
/**
373379
* Command FIFO stores commands alongs with SPI device configuration.
374-
* To fit into 64-bit word, limit supported CS lines down to 64K rather than 4G.
380+
* To fit into 64-bit word, limit supported CS lines down to 256 rather than 4G,
381+
* and use "int8_t" for command state.
375382
*/
376383
typedef struct {
377384
uint32_t opts; /* configopts */
378385
uint16_t command; /* command[15:0] */
379386
uint8_t csid; /* csid[7:0] */
380-
bool ongoing; /* command is being processed */
387+
int8_t state; /* enum CmdState */
381388
} CmdFifoSlot;
382389

383390
static_assert(sizeof(TxFifoSlot) == sizeof(uint64_t),
@@ -751,6 +758,10 @@ static void ot_spi_host_step_fsm(OtSPIHostState *s, const char *cause)
751758
s->fsm.active = true;
752759
ot_spi_host_update_event(s);
753760

761+
if (headcmd->state == CMD_EXECUTED) {
762+
goto post;
763+
}
764+
754765
uint32_t command = (uint32_t)headcmd->command;
755766
bool read = ot_spi_host_is_rx(command);
756767
bool write = ot_spi_host_is_tx(command);
@@ -812,7 +823,7 @@ static void ot_spi_host_step_fsm(OtSPIHostState *s, const char *cause)
812823
length--;
813824
}
814825

815-
bool ongoing;
826+
int8_t cmd_state;
816827
if (length) {
817828
/* if the transfer early ended, a stall condition has been detected */
818829
if (write && txfifo_is_empty(s->tx_fifo)) {
@@ -825,15 +836,16 @@ static void ot_spi_host_step_fsm(OtSPIHostState *s, const char *cause)
825836
}
826837

827838
command = FIELD_DP32(command, COMMAND, LEN, length - 1);
828-
ongoing = true;
839+
cmd_state = CMD_ONGOING;
829840
} else {
830841
command = FIELD_DP32(command, COMMAND, LEN, 0);
831-
ongoing = false;
842+
cmd_state = CMD_EXECUTED;
832843
}
833844

834845
headcmd->command = (uint16_t)command;
835-
headcmd->ongoing = ongoing;
846+
headcmd->state = cmd_state;
836847

848+
post:
837849
ot_spi_host_update_regs(s);
838850

839851
timer_mod_anticipate(s->fsm_delay, qemu_clock_get_ns(OT_VIRTUAL_CLOCK) +
@@ -863,11 +875,11 @@ static void ot_spi_host_post_fsm(void *opaque)
863875

864876
CmdFifoSlot *headcmd = cmdfifo_peek(s->cmd_fifo);
865877
uint32_t command = (uint32_t)headcmd->command;
866-
bool ongoing = headcmd->ongoing;
878+
bool retire = headcmd->state == CMD_EXECUTED;
867879

868880
ot_spi_host_trace_status(s->ot_id, "P>", ot_spi_host_get_status(s));
869881

870-
if (!ongoing) {
882+
if (retire) {
871883
if (ot_spi_host_is_rx(command)) {
872884
/*
873885
* transfer has been completed, RX FIFO may need padding up to a
@@ -897,11 +909,11 @@ static void ot_spi_host_post_fsm(void *opaque)
897909

898910
ot_spi_host_trace_status(s->ot_id, "P<", ot_spi_host_get_status(s));
899911

900-
if (!ongoing) {
912+
if (retire) {
901913
/* last command has completed */
902914
if (!cmdfifo_is_empty(s->cmd_fifo)) {
903915
/* more commands have been scheduled */
904-
trace_ot_spi_host_debug(s->ot_id, "Next cmd");
916+
trace_ot_spi_host_debug(s->ot_id, "next cmd");
905917
if (!ot_spi_host_is_on_error(s)) {
906918
ot_spi_host_step_fsm(s, "post");
907919
} else {
@@ -1152,7 +1164,7 @@ static void ot_spi_host_io_write(void *opaque, hwaddr addr, uint64_t val64,
11521164
.opts = s->config_opts[csid],
11531165
.command = (uint16_t)val32, /* only b15..b0 are meaningful */
11541166
.csid = csid,
1155-
.ongoing = false,
1167+
.state = CMD_SCHEDULED,
11561168
};
11571169

11581170
bool activate = cmdfifo_is_empty(s->cmd_fifo) && !s->fsm.rx_stall &&

0 commit comments

Comments
 (0)