Skip to content

Commit b333927

Browse files
committed
[ot] hw/opentitan: ot_spi_host: update register definitions
Signed-off-by: Emmanuel Blot <eblot@rivosinc.com>
1 parent 90b632e commit b333927

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

hw/opentitan/ot_spi_host.c

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* Copyright (C) 2022 Western Digital
55
* Copyright (c) 2022-2024 Rivos, Inc.
6+
* Copyright (c) 2025 lowRISC contributors.
67
*
78
* Author(s):
89
* Wilfred Mallawa <wilfred.mallawa@wdc.com>
@@ -101,20 +102,20 @@ REG32(STATUS, 0x14u)
101102
FIELD(STATUS, ACTIVE, 30u, 1u)
102103
FIELD(STATUS, READY, 31u, 1u)
103104
REG32(CONFIGOPTS, 0x18u)
104-
FIELD(CONFIGOPTS, CLKDIV_0, 0u, 16u)
105-
FIELD(CONFIGOPTS, CSNIDLE_0, 16u, 4u)
106-
FIELD(CONFIGOPTS, CSNTRAIL_0, 20u, 4u)
107-
FIELD(CONFIGOPTS, CSNLEAD_0, 24u, 4u)
108-
FIELD(CONFIGOPTS, FULLCYC_0, 29u, 1u)
109-
FIELD(CONFIGOPTS, CPHA_0, 30u, 1u)
110-
FIELD(CONFIGOPTS, CPOL_0, 31u, 1u)
105+
FIELD(CONFIGOPTS, CLKDIV, 0u, 16u)
106+
FIELD(CONFIGOPTS, CSNIDLE, 16u, 4u)
107+
FIELD(CONFIGOPTS, CSNTRAIL, 20u, 4u)
108+
FIELD(CONFIGOPTS, CSNLEAD, 24u, 4u)
109+
FIELD(CONFIGOPTS, FULLCYC, 29u, 1u)
110+
FIELD(CONFIGOPTS, CPHA, 30u, 1u)
111+
FIELD(CONFIGOPTS, CPOL, 31u, 1u)
111112
REG32(CSID, 0x1cu)
112113
FIELD(CSID, CSID, 0u, 32u)
113114
REG32(COMMAND, 0x20u)
114-
FIELD(COMMAND, LEN, 0u, 9u)
115-
FIELD(COMMAND, CSAAT, 9u, 1u)
116-
FIELD(COMMAND, SPEED, 10u, 2u)
117-
FIELD(COMMAND, DIRECTION, 12u, 2u)
115+
FIELD(COMMAND, CSAAT, 0u, 1u)
116+
FIELD(COMMAND, SPEED, 1u, 2u)
117+
FIELD(COMMAND, DIRECTION, 3u, 2u)
118+
FIELD(COMMAND, LEN, 5u, 20u)
118119
REG32(RXDATA, 0x24u)
119120
REG32(TXDATA, 0x28u)
120121
REG32(ERROR_ENABLE, 0x2cu)
@@ -170,13 +171,13 @@ REG32(EVENT_ENABLE, 0x34u)
170171
R_ERROR_STATUS_ACCESSINVAL_MASK)
171172

172173
#define R_CONFIGOPTS_MASK \
173-
(R_CONFIGOPTS_CLKDIV_0_MASK | \
174-
R_CONFIGOPTS_CSNIDLE_0_MASK | \
175-
R_CONFIGOPTS_CSNTRAIL_0_MASK | \
176-
R_CONFIGOPTS_CSNLEAD_0_MASK | \
177-
R_CONFIGOPTS_FULLCYC_0_MASK | \
178-
R_CONFIGOPTS_CPHA_0_MASK | \
179-
R_CONFIGOPTS_CPOL_0_MASK)
174+
(R_CONFIGOPTS_CLKDIV_MASK | \
175+
R_CONFIGOPTS_CSNIDLE_MASK | \
176+
R_CONFIGOPTS_CSNTRAIL_MASK | \
177+
R_CONFIGOPTS_CSNLEAD_MASK | \
178+
R_CONFIGOPTS_FULLCYC_MASK | \
179+
R_CONFIGOPTS_CPHA_MASK | \
180+
R_CONFIGOPTS_CPOL_MASK)
180181

181182
#define R_EVENT_ENABLE_MASK \
182183
(R_EVENT_ENABLE_RXFULL_MASK | \
@@ -377,19 +378,14 @@ enum CmdState {
377378

378379
/**
379380
* Command FIFO stores commands alongs with SPI device configuration.
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.
382381
*/
383382
typedef struct {
384383
uint32_t opts; /* configopts */
385-
uint16_t command; /* command[15:0] */
384+
uint32_t command; /* command */
386385
uint8_t csid; /* csid[7:0] */
387386
int8_t state; /* enum CmdState */
388387
} CmdFifoSlot;
389388

390-
static_assert(sizeof(TxFifoSlot) == sizeof(uint64_t),
391-
"Invalid CmdFifoSlot size");
392-
393389
struct CmdFifo {
394390
CmdFifoSlot *data;
395391
uint32_t capacity;
@@ -473,20 +469,22 @@ static void cmdfifo_create(CmdFifo *fifo, uint32_t capacity)
473469
fifo->num = 0u;
474470
}
475471

476-
static void cmdfifo_push(CmdFifo *fifo, CmdFifoSlot cmd)
472+
static void cmdfifo_push(CmdFifo *fifo, const CmdFifoSlot *cmd)
477473
{
478474
g_assert(fifo->num < fifo->capacity);
479-
fifo->data[(fifo->head + fifo->num) % fifo->capacity] = cmd;
475+
memcpy(&fifo->data[(fifo->head + fifo->num) % fifo->capacity], cmd,
476+
sizeof(*cmd));
480477
fifo->num++;
481478
}
482479

483-
static CmdFifoSlot cmdfifo_pop(CmdFifo *fifo)
480+
static void cmdfifo_pop(CmdFifo *fifo, CmdFifoSlot *cmd)
484481
{
485482
g_assert(fifo->num > 0u);
486-
CmdFifoSlot ret = fifo->data[fifo->head++];
483+
if (cmd) {
484+
memcpy(cmd, &fifo->data[fifo->head++], sizeof(*cmd));
485+
}
487486
fifo->head %= fifo->capacity;
488487
fifo->num--;
489-
return ret;
490488
}
491489

492490
static CmdFifoSlot *cmdfifo_peek(CmdFifo *fifo)
@@ -762,7 +760,7 @@ static void ot_spi_host_step_fsm(OtSPIHostState *s, const char *cause)
762760
goto post;
763761
}
764762

765-
uint32_t command = (uint32_t)headcmd->command;
763+
uint32_t command = headcmd->command;
766764
bool read = ot_spi_host_is_rx(command);
767765
bool write = ot_spi_host_is_tx(command);
768766
unsigned speed = FIELD_EX32(command, COMMAND, SPEED);
@@ -842,7 +840,7 @@ static void ot_spi_host_step_fsm(OtSPIHostState *s, const char *cause)
842840
cmd_state = CMD_EXECUTED;
843841
}
844842

845-
headcmd->command = (uint16_t)command;
843+
headcmd->command = command;
846844
headcmd->state = cmd_state;
847845

848846
post:
@@ -874,7 +872,7 @@ static void ot_spi_host_post_fsm(void *opaque)
874872
trace_ot_spi_host_fsm(s->ot_id, "post");
875873

876874
CmdFifoSlot *headcmd = cmdfifo_peek(s->cmd_fifo);
877-
uint32_t command = (uint32_t)headcmd->command;
875+
uint32_t command = headcmd->command;
878876
bool retire = headcmd->state == CMD_EXECUTED;
879877

880878
ot_spi_host_trace_status(s->ot_id, "P>", ot_spi_host_get_status(s));
@@ -899,7 +897,7 @@ static void ot_spi_host_post_fsm(void *opaque)
899897
}
900898

901899
/* retire command */
902-
cmdfifo_pop(s->cmd_fifo);
900+
cmdfifo_pop(s->cmd_fifo, NULL);
903901

904902
/* "the command is complete when STATUS.ACTIVE goes low." */
905903
s->fsm.active = false;
@@ -1159,17 +1157,17 @@ static void ot_spi_host_io_write(void *opaque, hwaddr addr, uint64_t val64,
11591157
error = true;
11601158
}
11611159

1162-
uint16_t csid = (uint16_t)s->regs[R_CSID];
1160+
uint8_t csid = (uint8_t)s->regs[R_CSID];
11631161
CmdFifoSlot slot = {
11641162
.opts = s->config_opts[csid],
1165-
.command = (uint16_t)val32, /* only b15..b0 are meaningful */
1163+
.command = val32,
11661164
.csid = csid,
11671165
.state = CMD_SCHEDULED,
11681166
};
11691167

11701168
bool activate = cmdfifo_is_empty(s->cmd_fifo) && !s->fsm.rx_stall &&
11711169
!s->fsm.tx_stall && !error;
1172-
cmdfifo_push(s->cmd_fifo, slot);
1170+
cmdfifo_push(s->cmd_fifo, &slot);
11731171
ot_spi_host_update_event(s); /* track ready */
11741172
if (activate) {
11751173
ot_spi_host_step_fsm(s, "cmd");

0 commit comments

Comments
 (0)