(fpga) Start adding support for new control of FW and appo mode.

Signed-off-by: Joachim Strömbergson <joachim@assured.se>
This commit is contained in:
Joachim Strömbergson 2024-08-28 13:40:44 +02:00
parent f5f57ffdef
commit 2464e8a505
No known key found for this signature in database
GPG Key ID: 865B8A548EA61679

View File

@ -110,9 +110,7 @@ module tk1(
localparam FW_RAM_FIRST = 32'hd0000000; localparam FW_RAM_FIRST = 32'hd0000000;
localparam FW_RAM_LAST = 32'hd00007ff; localparam FW_RAM_LAST = 32'hd00007ff;
`ifdef INCLUDE_SPI_MASTER localparam ROM_PREFIX = 2'h0;
localparam RAM_PREFIX = 2'h1;
`endif // INCLUDE_SPI_MASTER
//---------------------------------------------------------------- //----------------------------------------------------------------
@ -121,8 +119,10 @@ module tk1(
reg [31 : 0] cdi_mem [0 : 7]; reg [31 : 0] cdi_mem [0 : 7];
reg cdi_mem_we; reg cdi_mem_we;
reg switch_app_reg; reg fw_app_mode_reg;
reg switch_app_we; reg fw_app_mode_new;
reg fw_app_mode_we;
reg fw_app_mode_rst;
reg [2 : 0] led_reg; reg [2 : 0] led_reg;
reg led_we; reg led_we;
@ -167,13 +167,14 @@ module tk1(
reg force_trap_reg; reg force_trap_reg;
reg force_trap_set; reg force_trap_set;
`ifdef INCLUDE_SPI_MASTER `ifdef INCLUDE_SPI_MASTER
reg spi_access_ctrl_reg;
reg spi_access_ctrl_new;
reg spi_access_ctrl_we;
reg [31 : 0] spi_cmd_addr_reg; reg [31 : 0] spi_cmd_addr_reg;
reg spi_cmd_addr_we; reg spi_cmd_addr_we;
reg spi_access_ctrl_reg;
reg spi_access_ctrl_new;
reg spi_access_ctrl_we;
`endif // INCLUDE_SPI_MASTER `endif // INCLUDE_SPI_MASTER
@ -206,7 +207,7 @@ module tk1(
assign read_data = tmp_read_data; assign read_data = tmp_read_data;
assign ready = tmp_ready; assign ready = tmp_ready;
assign fw_app_mode = switch_app_reg; assign fw_app_mode = fw_app_mode_reg;
assign force_trap = force_trap_reg; assign force_trap = force_trap_reg;
@ -273,7 +274,7 @@ module tk1(
always @ (posedge clk) always @ (posedge clk)
begin : reg_update begin : reg_update
if (!reset_n) begin if (!reset_n) begin
switch_app_reg <= 1'h0; fw_app_mode_reg <= 1'h1;
led_reg <= 3'h6; led_reg <= 3'h6;
gpio1_reg <= 2'h0; gpio1_reg <= 2'h0;
gpio2_reg <= 2'h0; gpio2_reg <= 2'h0;
@ -317,8 +318,16 @@ module tk1(
gpio2_reg[0] <= gpio2; gpio2_reg[0] <= gpio2;
gpio2_reg[1] <= gpio2_reg[0]; gpio2_reg[1] <= gpio2_reg[0];
if (switch_app_we) begin // When not in FW mode, Disable things that are
switch_app_reg <= 1'h1; // explitly enabled.
if (!fw_app_mode) begin
`ifdef INCLUDE_SPI_MASTER
spi_access_ctrl_reg <= 1'h0;
`endif // INCLUDE_SPI_MASTER
end
if (fw_app_mode_we) begin
fw_app_mode_reg <= fw_app_mode_new;
end end
if (led_we) begin if (led_we) begin
@ -383,7 +392,7 @@ module tk1(
end end
if (spi_access_ctrl_we) begin if (spi_access_ctrl_we) begin
spi_access_ctrl_reg <= spi_access_ctrl_new; spi_access_ctrl_reg <= write_data[0];
end end
`endif // INCLUDE_SPI_MASTER `endif // INCLUDE_SPI_MASTER
@ -454,37 +463,36 @@ module tk1(
end end
`ifdef INCLUDE_SPI_MASTER
//---------------------------------------------------------------- //----------------------------------------------------------------
// spi_access_ctrl // fw_app_mode_ctrl
// //
// Logic that implements the detection of a SPI command trampoline // Logic that implements the switch between FW mode and App
// event, when the CPU reads an instruction from the specified // mode. The FW mode can explicitly be enabled via API, but only if
// SPI command handler FW entry point. When that happens SPI // the instruction writing to the API comes from the ROM.
// access is enabled.
// //
// The logic also handles the event when the SPI access control // As soon as an instruction is executed from RAM, the mode is
// API is written to. WHen that happens SPI access is // switched to App mode. This means that after reset the device is
// disabled. // in App mode, and will be set to App mode when the FW starts
//---------------------------------------------------------------- // the loaded app.
// ----------------------------------------------------------------
always @* always @*
begin : spi_access_ctrl begin : fw_app_mode_ctrl
spi_access_ctrl_new = 1'h0; fw_app_mode_new = 1'h0;
spi_access_ctrl_we = 1'h0; fw_app_mode_we = 1'h0;
if (cpu_valid & cpu_instr) begin if (cpu_valid & cpu_instr) begin
if (cpu_addr == spi_cmd_addr_reg) begin if (cpu_addr[31 : 30] == ROM_PREFIX) begin
spi_access_ctrl_new = 1'h1; if (fw_app_mode_rst) begin
spi_access_ctrl_we = 1'h1; fw_app_mode_new = 1'h0;
fw_app_mode_we = 1'h1;
end
end end
else begin
if (cpu_addr[31 : 30] == RAM_PREFIX) begin fw_app_mode_new = 1'h1;
spi_access_ctrl_new = 1'h0; fw_app_mode_new = 1'h1;
spi_access_ctrl_we = 1'h1;
end end
end end
end end
`endif // INCLUDE_SPI_MASTER
//---------------------------------------------------------------- //----------------------------------------------------------------
@ -492,7 +500,6 @@ module tk1(
//---------------------------------------------------------------- //----------------------------------------------------------------
always @* always @*
begin : api begin : api
switch_app_we = 1'h0;
led_we = 1'h0; led_we = 1'h0;
gpio3_we = 1'h0; gpio3_we = 1'h0;
gpio4_we = 1'h0; gpio4_we = 1'h0;
@ -508,6 +515,7 @@ module tk1(
cpu_mon_first_we = 1'h0; cpu_mon_first_we = 1'h0;
cpu_mon_last_we = 1'h0; cpu_mon_last_we = 1'h0;
cpu_mon_en_we = 1'h0; cpu_mon_en_we = 1'h0;
fw_app_mode_rst = 1'h0;
tmp_read_data = 32'h0; tmp_read_data = 32'h0;
tmp_ready = 1'h0; tmp_ready = 1'h0;
@ -526,7 +534,7 @@ module tk1(
tmp_ready = 1'h1; tmp_ready = 1'h1;
if (we) begin if (we) begin
if (address == ADDR_SWITCH_APP) begin if (address == ADDR_SWITCH_APP) begin
switch_app_we = 1'h1; fw_app_mode_rst = 1'h1;
end end
if (address == ADDR_LED) begin if (address == ADDR_LED) begin
@ -539,13 +547,13 @@ module tk1(
end end
if (address == ADDR_APP_START) begin if (address == ADDR_APP_START) begin
if (!switch_app_reg) begin if (!fw_app_mode_reg) begin
app_start_we = 1'h1; app_start_we = 1'h1;
end end
end end
if (address == ADDR_APP_SIZE) begin if (address == ADDR_APP_SIZE) begin
if (!switch_app_reg) begin if (!fw_app_mode_reg) begin
app_size_we = 1'h1; app_size_we = 1'h1;
end end
end end
@ -555,25 +563,25 @@ module tk1(
end end
if (address == ADDR_BLAKE2S) begin if (address == ADDR_BLAKE2S) begin
if (!switch_app_reg) begin if (!fw_app_mode_reg) begin
blake2s_addr_we = 1'h1; blake2s_addr_we = 1'h1;
end end
end end
if ((address >= ADDR_CDI_FIRST) && (address <= ADDR_CDI_LAST)) begin if ((address >= ADDR_CDI_FIRST) && (address <= ADDR_CDI_LAST)) begin
if (!switch_app_reg) begin if (!fw_app_mode_reg) begin
cdi_mem_we = 1'h1; cdi_mem_we = 1'h1;
end end
end end
if (address == ADDR_RAM_ADDR_RAND) begin if (address == ADDR_RAM_ADDR_RAND) begin
if (!switch_app_reg) begin if (!fw_app_mode_reg) begin
ram_addr_rand_we = 1'h1; ram_addr_rand_we = 1'h1;
end end
end end
if (address == ADDR_RAM_DATA_RAND) begin if (address == ADDR_RAM_DATA_RAND) begin
if (!switch_app_reg) begin if (!fw_app_mode_reg) begin
ram_data_rand_we = 1'h1; ram_data_rand_we = 1'h1;
end end
end end
@ -596,7 +604,9 @@ module tk1(
`ifdef INCLUDE_SPI_MASTER `ifdef INCLUDE_SPI_MASTER
if (address == ADDR_SPI_EN) begin if (address == ADDR_SPI_EN) begin
spi_enable_vld = spi_access_ctrl_reg; if (!fw_app_mode_reg) begin
spi_access_ctrl_we = 1'h1;
end
end end
if (address == ADDR_SPI_XFER) begin if (address == ADDR_SPI_XFER) begin
@ -608,7 +618,7 @@ module tk1(
end end
if (address == ADDR_SPI_CMD) begin if (address == ADDR_SPI_CMD) begin
if (!switch_app_reg) begin if (!fw_app_mode_reg) begin
spi_cmd_addr_we = 1'h1; spi_cmd_addr_we = 1'h1;
end end
end end
@ -629,7 +639,7 @@ module tk1(
end end
if (address == ADDR_SWITCH_APP) begin if (address == ADDR_SWITCH_APP) begin
tmp_read_data = {32{switch_app_reg}}; tmp_read_data[0] = app_mode_reg;
end end
if (address == ADDR_LED) begin if (address == ADDR_LED) begin
@ -658,7 +668,7 @@ module tk1(
end end
if ((address >= ADDR_UDI_FIRST) && (address <= ADDR_UDI_LAST)) begin if ((address >= ADDR_UDI_FIRST) && (address <= ADDR_UDI_LAST)) begin
if (!switch_app_reg) begin if (!fw_app_mode_reg) begin
tmp_read_data = udi_rdata; tmp_read_data = udi_rdata;
end end
end end