Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 1 | // Copyright lowRISC contributors. |
| 2 | // Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Flash Controller for life cycle / key management handling |
| 6 | // |
| 7 | |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 8 | module flash_ctrl_lcmgr import flash_ctrl_pkg::*; #( |
| 9 | parameter flash_key_t RndCnstAddrKey = RndCnstAddrKeyDefault, |
| 10 | parameter flash_key_t RndCnstDataKey = RndCnstDataKeyDefault |
| 11 | ) ( |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 12 | input clk_i, |
| 13 | input rst_ni, |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 14 | input clk_otp_i, |
| 15 | input rst_otp_ni, |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 16 | |
Timothy Chen | 04834fd | 2020-09-11 14:58:32 -0700 | [diff] [blame] | 17 | // initialization command |
| 18 | input init_i, |
| 19 | |
| 20 | // only access seeds when provisioned |
| 21 | input provision_en_i, |
| 22 | |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 23 | // interface to ctrl arb control ports |
| 24 | output flash_ctrl_reg_pkg::flash_ctrl_reg2hw_control_reg_t ctrl_o, |
| 25 | output logic req_o, |
Timothy Chen | 2df9561 | 2020-08-31 15:29:12 -0700 | [diff] [blame] | 26 | output logic [top_pkg::TL_AW-1:0] addr_o, |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 27 | input done_i, |
| 28 | input err_i, |
| 29 | |
| 30 | // interface to ctrl_arb data ports |
| 31 | output logic rready_o, |
| 32 | input rvalid_i, |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 33 | output logic wvalid_o, |
| 34 | input wready_i, |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 35 | |
| 36 | // direct form rd_fifo |
| 37 | input [BusWidth-1:0] rdata_i, |
| 38 | |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 39 | // direct to wr_fifo |
| 40 | output logic [BusWidth-1:0] wdata_o, |
| 41 | |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 42 | // external rma request |
Timothy Chen | 04834fd | 2020-09-11 14:58:32 -0700 | [diff] [blame] | 43 | // This should be simplified to just multi-bit request and multi-bit response |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 44 | input lc_ctrl_pkg::lc_tx_t rma_req_i, |
| 45 | output lc_ctrl_pkg::lc_tx_t rma_ack_o, |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 46 | |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 47 | // seeds to the outside world, |
| 48 | output logic [NumSeeds-1:0][SeedWidth-1:0] seeds_o, |
| 49 | |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 50 | // indicate to memory protection what phase the hw interface is in |
| 51 | output flash_lcmgr_phase_e phase_o, |
| 52 | |
| 53 | // error status to registers |
| 54 | output logic seed_err_o, |
| 55 | |
Timothy Chen | 6b241b3 | 2020-11-13 15:15:45 -0800 | [diff] [blame] | 56 | // enable read buffer in flash_phy |
| 57 | output logic rd_buf_en_o, |
| 58 | |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 59 | // request otp keys |
| 60 | output otp_ctrl_pkg::flash_otp_key_req_t otp_key_req_o, |
| 61 | input otp_ctrl_pkg::flash_otp_key_rsp_t otp_key_rsp_i, |
| 62 | output flash_key_t addr_key_o, |
| 63 | output flash_key_t data_key_o, |
Timothy Chen | e1e0fd6 | 2021-04-07 16:13:28 -0700 | [diff] [blame] | 64 | output flash_key_t rand_addr_key_o, |
| 65 | output flash_key_t rand_data_key_o, |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 66 | |
Timothy Chen | aeffadc | 2020-12-11 15:03:07 -0800 | [diff] [blame] | 67 | // entropy interface |
| 68 | output logic edn_req_o, |
| 69 | input edn_ack_i, |
| 70 | output logic lfsr_en_o, |
| 71 | input [BusWidth-1:0] rand_i, |
| 72 | |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 73 | // init ongoing |
| 74 | output logic init_busy_o |
| 75 | ); |
| 76 | |
| 77 | // total number of pages to be wiped during RMA entry |
Rupert Swarbrick | 723c05e | 2021-03-30 09:26:17 +0100 | [diff] [blame] | 78 | localparam int unsigned WipeIdxWidth = prim_util_pkg::vbits(WipeEntries); |
| 79 | localparam int unsigned MaxWipeEntry = WipeEntries - 1; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 80 | |
| 81 | // seed related local params |
Rupert Swarbrick | 723c05e | 2021-03-30 09:26:17 +0100 | [diff] [blame] | 82 | localparam int unsigned SeedReads = SeedWidth / BusWidth; |
| 83 | localparam int unsigned SeedRdsWidth = $clog2(SeedReads); |
| 84 | localparam int unsigned SeedCntWidth = $clog2(NumSeeds+1); |
| 85 | localparam int unsigned NumSeedWidth = $clog2(NumSeeds); |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 86 | |
| 87 | // the various seed outputs |
| 88 | logic [NumSeeds-1:0][SeedReads-1:0][BusWidth-1:0] seeds_q; |
| 89 | |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 90 | // progress through and read out the various pieces of content |
| 91 | // This FSM should become sparse, especially for StRmaRsp |
| 92 | typedef enum logic [3:0] { |
Timothy Chen | 04834fd | 2020-09-11 14:58:32 -0700 | [diff] [blame] | 93 | StIdle, |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 94 | StReqAddrKey, |
| 95 | StReqDataKey, |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 96 | StReadSeeds, |
| 97 | StWait, |
Timothy Chen | aeffadc | 2020-12-11 15:03:07 -0800 | [diff] [blame] | 98 | StEntropyReseed, |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 99 | StRmaWipe, |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 100 | StRmaRsp, |
| 101 | StInvalid |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 102 | } state_e; |
| 103 | |
| 104 | state_e state_q, state_d; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 105 | lc_ctrl_pkg::lc_tx_t err_sts; |
| 106 | logic err_sts_set; |
| 107 | lc_ctrl_pkg::lc_tx_t rma_ack_d, rma_ack_q; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 108 | logic validate_q, validate_d; |
| 109 | logic [SeedCntWidth-1:0] seed_cnt_q; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 110 | logic [SeedRdsWidth-1:0] addr_cnt_q; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 111 | logic seed_cnt_en, seed_cnt_clr; |
| 112 | logic addr_cnt_en, addr_cnt_clr; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 113 | logic rma_wipe_req, rma_wipe_done; |
| 114 | logic [WipeIdxWidth-1:0] rma_wipe_idx; |
| 115 | logic rma_wipe_idx_incr; |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 116 | flash_lcmgr_phase_e phase; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 117 | logic seed_phase; |
| 118 | logic rma_phase; |
| 119 | |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 120 | assign seed_phase = phase == PhaseSeed; |
| 121 | assign rma_phase = phase == PhaseRma; |
| 122 | |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 123 | always_ff @(posedge clk_i or negedge rst_ni) begin |
| 124 | if (!rst_ni) begin |
Timothy Chen | 04834fd | 2020-09-11 14:58:32 -0700 | [diff] [blame] | 125 | state_q <= StIdle; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 126 | rma_ack_q <= lc_ctrl_pkg::Off; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 127 | validate_q <= 1'b0; |
| 128 | end else begin |
| 129 | state_q <= state_d; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 130 | rma_ack_q <= rma_ack_d; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 131 | validate_q <= validate_d; |
| 132 | end |
| 133 | end |
| 134 | |
| 135 | // seed cnt tracks which seed round we are handling at the moment |
| 136 | always_ff @(posedge clk_i or negedge rst_ni) begin |
| 137 | if (!rst_ni) begin |
| 138 | seed_cnt_q <= '0; |
| 139 | end else if (seed_cnt_clr) begin |
| 140 | seed_cnt_q <= '0; |
| 141 | end else if (seed_cnt_en) begin |
| 142 | seed_cnt_q <= seed_cnt_q + 1'b1; |
| 143 | end |
| 144 | end |
| 145 | |
| 146 | // addr cnt tracks how far we are in an address looop |
| 147 | always_ff @(posedge clk_i or negedge rst_ni) begin |
| 148 | if (!rst_ni) begin |
| 149 | addr_cnt_q <= '0; |
| 150 | end else if (addr_cnt_clr) begin |
| 151 | addr_cnt_q <= '0; |
| 152 | end else if (addr_cnt_en) begin |
| 153 | addr_cnt_q <= addr_cnt_q + 1'b1; |
| 154 | end |
| 155 | end |
| 156 | |
| 157 | // capture the seed values |
| 158 | logic [SeedRdsWidth-1:0] rd_idx; |
| 159 | logic [NumSeedWidth-1:0] seed_idx; |
| 160 | assign rd_idx = addr_cnt_q[SeedRdsWidth-1:0]; |
| 161 | assign seed_idx = seed_cnt_q[NumSeedWidth-1:0]; |
| 162 | always_ff @(posedge clk_i) begin |
| 163 | // validate current value |
| 164 | if (seed_phase && validate_q && rvalid_i) begin |
| 165 | seeds_q[seed_idx][rd_idx] <= seeds_q[seed_idx][rd_idx] & |
| 166 | rdata_i; |
| 167 | end else if (seed_phase && rvalid_i) begin |
| 168 | seeds_q[seed_idx][rd_idx] <= rdata_i; |
| 169 | end |
| 170 | end |
| 171 | |
Timothy Chen | 782f27a | 2020-09-17 22:57:57 -0700 | [diff] [blame] | 172 | page_addr_t seed_page; |
| 173 | logic [InfoTypesWidth-1:0] seed_info_sel; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 174 | logic [BusAddrW-1:0] seed_page_addr; |
Timothy Chen | 782f27a | 2020-09-17 22:57:57 -0700 | [diff] [blame] | 175 | assign seed_page = SeedInfoPageSel[seed_idx]; |
| 176 | assign seed_info_sel = seed_page.sel; |
| 177 | assign seed_page_addr = BusAddrW'({seed_page.addr, BusWordW'(0)}); |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 178 | |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 179 | logic start; |
| 180 | flash_op_e op; |
Timothy Chen | 103b4cb | 2020-09-12 15:31:37 -0700 | [diff] [blame] | 181 | flash_prog_e prog_type; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 182 | flash_erase_e erase_type; |
| 183 | flash_part_e part_sel; |
Timothy Chen | 782f27a | 2020-09-17 22:57:57 -0700 | [diff] [blame] | 184 | logic [InfoTypesWidth-1:0] info_sel; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 185 | logic [11:0] num_words; |
| 186 | logic [BusAddrW-1:0] addr; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 187 | |
Timothy Chen | 103b4cb | 2020-09-12 15:31:37 -0700 | [diff] [blame] | 188 | assign prog_type = FlashProgNormal; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 189 | assign erase_type = FlashErasePage; |
| 190 | // seed phase is always read |
| 191 | // rma phase is erase unless we are validating |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 192 | assign op = FlashOpRead; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 193 | |
Timothy Chen | a49dc7b | 2020-09-15 17:02:03 -0700 | [diff] [blame] | 194 | // synchronize inputs |
| 195 | logic init_q; |
Cindy Chen | 39b1434 | 2021-01-29 16:25:24 -0800 | [diff] [blame] | 196 | lc_ctrl_pkg::lc_tx_t [0:0] rma_req; |
Timothy Chen | a49dc7b | 2020-09-15 17:02:03 -0700 | [diff] [blame] | 197 | |
| 198 | prim_flop_2sync #( |
| 199 | .Width(1), |
| 200 | .ResetValue(0) |
| 201 | ) u_sync_flash_init ( |
| 202 | .clk_i, |
| 203 | .rst_ni, |
| 204 | .d_i(init_i), |
| 205 | .q_o(init_q) |
| 206 | ); |
| 207 | |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 208 | prim_lc_sync #( |
| 209 | .NumCopies(1) |
| 210 | ) u_sync_rma_req ( |
| 211 | .clk_i, |
| 212 | .rst_ni, |
| 213 | .lc_en_i(rma_req_i), |
| 214 | .lc_en_o(rma_req) |
| 215 | ); |
| 216 | |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 217 | logic addr_key_req_d; |
| 218 | logic addr_key_ack_q; |
| 219 | logic data_key_req_d; |
| 220 | logic data_key_ack_q; |
| 221 | |
| 222 | // req/ack to otp |
| 223 | prim_sync_reqack u_addr_sync_reqack ( |
| 224 | .clk_src_i(clk_i), |
| 225 | .rst_src_ni(rst_ni), |
| 226 | .clk_dst_i(clk_otp_i), |
| 227 | .rst_dst_ni(rst_otp_ni), |
| 228 | .src_req_i(addr_key_req_d), |
| 229 | .src_ack_o(addr_key_ack_q), |
| 230 | .dst_req_o(otp_key_req_o.addr_req), |
| 231 | .dst_ack_i(otp_key_rsp_i.addr_ack) |
| 232 | ); |
| 233 | |
| 234 | // req/ack to otp |
| 235 | prim_sync_reqack u_data_sync_reqack ( |
| 236 | .clk_src_i(clk_i), |
| 237 | .rst_src_ni(rst_ni), |
| 238 | .clk_dst_i(clk_otp_i), |
| 239 | .rst_dst_ni(rst_otp_ni), |
| 240 | .src_req_i(data_key_req_d), |
| 241 | .src_ack_o(data_key_ack_q), |
| 242 | .dst_req_o(otp_key_req_o.data_req), |
| 243 | .dst_ack_i(otp_key_rsp_i.data_ack) |
| 244 | ); |
| 245 | |
| 246 | always_ff @(posedge clk_i or negedge rst_ni) begin |
| 247 | if (!rst_ni) begin |
| 248 | addr_key_o <= RndCnstAddrKey; |
| 249 | data_key_o <= RndCnstDataKey; |
| 250 | end else begin |
| 251 | if (addr_key_req_d && addr_key_ack_q) begin |
| 252 | addr_key_o <= flash_key_t'(otp_key_rsp_i.key); |
Timothy Chen | e1e0fd6 | 2021-04-07 16:13:28 -0700 | [diff] [blame] | 253 | rand_addr_key_o <= flash_key_t'(otp_key_rsp_i.rand_key); |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 254 | end |
| 255 | |
| 256 | if (data_key_req_d && data_key_ack_q) begin |
| 257 | data_key_o <= flash_key_t'(otp_key_rsp_i.key); |
Timothy Chen | e1e0fd6 | 2021-04-07 16:13:28 -0700 | [diff] [blame] | 258 | rand_data_key_o <= flash_key_t'(otp_key_rsp_i.rand_key); |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 259 | end |
| 260 | end |
| 261 | end |
| 262 | |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 263 | |
| 264 | /////////////////////////////// |
| 265 | // Hardware Interface FSM |
| 266 | // TODO: Merge the read/verify mechanism with RMA later |
| 267 | /////////////////////////////// |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 268 | always_comb begin |
| 269 | |
| 270 | // phases of the hardware interface |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 271 | phase = PhaseNone; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 272 | |
| 273 | // timer controls |
| 274 | seed_cnt_en = 1'b0; |
| 275 | seed_cnt_clr = 1'b0; |
| 276 | addr_cnt_en = 1'b0; |
| 277 | addr_cnt_clr = 1'b0; |
| 278 | |
| 279 | // flash ctrrl arb controls |
| 280 | start = 1'b0; |
| 281 | addr = '0; |
| 282 | part_sel = FlashPartInfo; |
Timothy Chen | 782f27a | 2020-09-17 22:57:57 -0700 | [diff] [blame] | 283 | info_sel = 0; |
Rupert Swarbrick | 723c05e | 2021-03-30 09:26:17 +0100 | [diff] [blame] | 284 | num_words = SeedReads[11:0] - 12'd1; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 285 | |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 286 | // seed status |
| 287 | seed_err_o = 1'b0; |
| 288 | |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 289 | state_d = state_q; |
Timothy Chen | 5e85607 | 2020-12-17 14:48:06 -0800 | [diff] [blame] | 290 | rma_ack_d = lc_ctrl_pkg::Off; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 291 | validate_d = validate_q; |
| 292 | |
Timothy Chen | 6b241b3 | 2020-11-13 15:15:45 -0800 | [diff] [blame] | 293 | // read buffer enable |
| 294 | rd_buf_en_o = 1'b0; |
| 295 | |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 296 | addr_key_req_d = 1'b0; |
| 297 | data_key_req_d = 1'b0; |
| 298 | |
Timothy Chen | aeffadc | 2020-12-11 15:03:07 -0800 | [diff] [blame] | 299 | // entropy handling |
| 300 | edn_req_o = 1'b0; |
| 301 | lfsr_en_o = 1'b0; |
| 302 | |
Timothy Chen | 5e85607 | 2020-12-17 14:48:06 -0800 | [diff] [blame] | 303 | // rma related |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 304 | rma_wipe_req = 1'b0; |
| 305 | rma_wipe_idx_incr = 1'b0; |
| 306 | |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 307 | unique case (state_q) |
| 308 | |
Timothy Chen | 04834fd | 2020-09-11 14:58:32 -0700 | [diff] [blame] | 309 | StIdle: begin |
Timothy Chen | a49dc7b | 2020-09-15 17:02:03 -0700 | [diff] [blame] | 310 | if (init_q) begin |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 311 | state_d = StReqAddrKey; |
| 312 | end |
| 313 | end |
| 314 | |
| 315 | StReqAddrKey: begin |
Timothy Chen | 6efde1e | 2021-04-16 15:39:23 -0700 | [diff] [blame] | 316 | phase = PhaseSeed; |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 317 | addr_key_req_d = 1'b1; |
| 318 | if (addr_key_ack_q) begin |
| 319 | state_d = StReqDataKey; |
| 320 | end |
| 321 | end |
| 322 | |
| 323 | StReqDataKey: begin |
Timothy Chen | 6efde1e | 2021-04-16 15:39:23 -0700 | [diff] [blame] | 324 | phase = PhaseSeed; |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 325 | data_key_req_d = 1'b1; |
| 326 | if (data_key_ack_q) begin |
| 327 | // provision_en is only a "good" value after otp/lc initialization |
Timothy Chen | 04834fd | 2020-09-11 14:58:32 -0700 | [diff] [blame] | 328 | state_d = provision_en_i ? StReadSeeds : StWait; |
| 329 | end |
| 330 | end |
| 331 | |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 332 | // read seeds |
| 333 | StReadSeeds: begin |
| 334 | // seeds can be updated in this state |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 335 | phase = PhaseSeed; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 336 | |
| 337 | // kick off flash transaction |
| 338 | start = 1'b1; |
| 339 | addr = BusAddrW'(seed_page_addr); |
Timothy Chen | 782f27a | 2020-09-17 22:57:57 -0700 | [diff] [blame] | 340 | info_sel = seed_info_sel; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 341 | |
| 342 | // we have checked all seeds, proceed |
Timothy Chen | 6efde1e | 2021-04-16 15:39:23 -0700 | [diff] [blame] | 343 | if (seed_cnt_q == NumSeeds) begin |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 344 | start = 1'b0; |
| 345 | state_d = StWait; |
| 346 | |
| 347 | // still reading curent seed, increment whenever data returns |
| 348 | end else if (!done_i) begin |
| 349 | addr_cnt_en = rvalid_i; |
| 350 | |
| 351 | // current seed reading is complete |
| 352 | // error is intentionally not used here, as we do not want read seed |
| 353 | // failures to stop the software from using flash |
Timothy Chen | c0b2874 | 2020-09-14 12:18:08 -0700 | [diff] [blame] | 354 | // When there are upstream failures, the data returned is simply all 1's. |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 355 | // So instead of doing anything explicit, a status is indicated for software. |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 356 | end else if (done_i) begin |
| 357 | addr_cnt_clr = 1'b1; |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 358 | seed_err_o = 1'b1; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 359 | |
| 360 | // we move to the next seed only if current seed is read and validated |
| 361 | // if not, flip to validate phase and read seed again |
| 362 | if (validate_q) begin |
| 363 | seed_cnt_en = 1'b1; |
| 364 | validate_d = 1'b0; |
| 365 | end else begin |
| 366 | validate_d = 1'b1; |
| 367 | end |
| 368 | end |
| 369 | end |
| 370 | |
| 371 | // Waiting for an rma entry command |
| 372 | StWait: begin |
Timothy Chen | 6b241b3 | 2020-11-13 15:15:45 -0800 | [diff] [blame] | 373 | rd_buf_en_o = 1'b1; |
Cindy Chen | 39b1434 | 2021-01-29 16:25:24 -0800 | [diff] [blame] | 374 | if (rma_req[0] == lc_ctrl_pkg::On) begin |
Timothy Chen | aeffadc | 2020-12-11 15:03:07 -0800 | [diff] [blame] | 375 | state_d = StEntropyReseed; |
| 376 | end |
| 377 | end |
| 378 | |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 379 | // Reseed entropy |
Timothy Chen | aeffadc | 2020-12-11 15:03:07 -0800 | [diff] [blame] | 380 | StEntropyReseed: begin |
| 381 | edn_req_o = 1'b1; |
| 382 | if(edn_ack_i) begin |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 383 | state_d = StRmaWipe; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 384 | end |
| 385 | end |
| 386 | |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 387 | StRmaWipe: begin |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 388 | phase = PhaseRma; |
Timothy Chen | aeffadc | 2020-12-11 15:03:07 -0800 | [diff] [blame] | 389 | lfsr_en_o = 1'b1; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 390 | rma_wipe_req = 1'b1; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 391 | |
Rupert Swarbrick | 723c05e | 2021-03-30 09:26:17 +0100 | [diff] [blame] | 392 | if (rma_wipe_idx == MaxWipeEntry[WipeIdxWidth-1:0] && rma_wipe_done) begin |
Timothy Chen | 5e85607 | 2020-12-17 14:48:06 -0800 | [diff] [blame] | 393 | // first check for error status |
| 394 | // If error status is set, go directly to invalid terminal state |
| 395 | // If error status is good, go to second check |
| 396 | state_d = (err_sts != lc_ctrl_pkg::On) ? StInvalid : StRmaRsp; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 397 | end else if (rma_wipe_done) begin |
| 398 | rma_wipe_idx_incr = 1; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 399 | end |
| 400 | end |
| 401 | |
| 402 | // response to rma request |
Timothy Chen | 5e85607 | 2020-12-17 14:48:06 -0800 | [diff] [blame] | 403 | // Second check for error status: |
| 404 | // If error status indicates error, jump to invalid terminal state |
| 405 | // Otherwise assign output to error status; |
| 406 | // TODO: consider lengthening the check |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 407 | StRmaRsp: begin |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 408 | phase = PhaseRma; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 409 | if (err_sts != lc_ctrl_pkg::On) begin |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 410 | state_d = StInvalid; |
Timothy Chen | 5e85607 | 2020-12-17 14:48:06 -0800 | [diff] [blame] | 411 | end else begin |
| 412 | rma_ack_d = err_sts; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 413 | end |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 414 | end |
| 415 | |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 416 | // Invalid catch-all state |
| 417 | default: begin |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 418 | phase = PhaseInvalid; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 419 | rma_ack_d = lc_ctrl_pkg::Off; |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 420 | state_d = StInvalid; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 421 | end |
| 422 | |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 423 | endcase // unique case (state_q) |
| 424 | |
| 425 | end // always_comb |
| 426 | |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 427 | /////////////////////////////// |
| 428 | // RMA wiping Mechanism |
| 429 | /////////////////////////////// |
| 430 | |
Rupert Swarbrick | 723c05e | 2021-03-30 09:26:17 +0100 | [diff] [blame] | 431 | localparam int unsigned PageCntWidth = prim_util_pkg::vbits(PagesPerBank + 1); |
| 432 | localparam int unsigned WordCntWidth = prim_util_pkg::vbits(BusWordsPerPage + 1); |
| 433 | localparam int unsigned BeatCntWidth = prim_util_pkg::vbits(WidthMultiple); |
| 434 | localparam int unsigned MaxBeatCnt = WidthMultiple - 1; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 435 | |
| 436 | logic page_cnt_ld; |
| 437 | logic page_cnt_incr; |
| 438 | logic page_cnt_clr; |
| 439 | logic word_cnt_incr; |
| 440 | logic word_cnt_clr; |
| 441 | logic prog_cnt_en; |
| 442 | logic rd_cnt_en; |
| 443 | logic beat_cnt_clr; |
| 444 | logic [PageCntWidth-1:0] page_cnt, end_page; |
| 445 | logic [WordCntWidth-1:0] word_cnt; |
| 446 | logic [BeatCntWidth-1:0] beat_cnt; |
| 447 | logic [WidthMultiple-1:0][BusWidth-1:0] prog_data; |
| 448 | |
| 449 | assign end_page = RmaWipeEntries[rma_wipe_idx].start_page + |
| 450 | RmaWipeEntries[rma_wipe_idx].num_pages; |
| 451 | |
| 452 | typedef enum logic [2:0] { |
| 453 | StRmaIdle, |
| 454 | StRmaPageSel, |
| 455 | StRmaErase, |
| 456 | StRmaWordSel, |
| 457 | StRmaProgram, |
| 458 | StRmaProgramWait, |
| 459 | StRmaRdVerify |
| 460 | } rma_state_e; |
| 461 | |
| 462 | rma_state_e rma_state_d, rma_state_q; |
| 463 | |
| 464 | always_ff @(posedge clk_i or negedge rst_ni) begin |
| 465 | if (!rst_ni) begin |
| 466 | rma_state_q <= StRmaIdle; |
| 467 | end else begin |
| 468 | rma_state_q <= rma_state_d; |
| 469 | end |
| 470 | end |
| 471 | |
| 472 | always_ff @(posedge clk_i or negedge rst_ni) begin |
| 473 | if (!rst_ni) begin |
| 474 | page_cnt <= '0; |
| 475 | end else if (page_cnt_clr) begin |
| 476 | page_cnt <= '0; |
| 477 | end else if (page_cnt_ld) begin |
| 478 | page_cnt <= RmaWipeEntries[rma_wipe_idx].start_page; |
| 479 | end else if (page_cnt_incr) begin |
| 480 | page_cnt <= page_cnt + 1'b1; |
| 481 | end |
| 482 | end |
| 483 | |
| 484 | always_ff @(posedge clk_i or negedge rst_ni) begin |
| 485 | if (!rst_ni) begin |
| 486 | word_cnt <= '0; |
| 487 | end else if (word_cnt_clr) begin |
| 488 | word_cnt <= '0; |
| 489 | end else if (word_cnt_incr) begin |
Timothy Chen | 6efde1e | 2021-04-16 15:39:23 -0700 | [diff] [blame] | 490 | word_cnt <= word_cnt + WidthMultiple; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 491 | end |
| 492 | end |
| 493 | |
| 494 | always_ff @(posedge clk_i or negedge rst_ni) begin |
| 495 | if (!rst_ni) begin |
| 496 | rma_wipe_idx <= '0; |
| 497 | end else if (rma_wipe_idx_incr) begin |
| 498 | rma_wipe_idx <= rma_wipe_idx + 1'b1; |
| 499 | end |
| 500 | end |
| 501 | |
| 502 | always_ff @(posedge clk_i or negedge rst_ni) begin |
| 503 | if (!rst_ni) begin |
| 504 | beat_cnt <= '0; |
| 505 | end else if (beat_cnt_clr) begin |
| 506 | beat_cnt <= '0; |
| 507 | end else if (prog_cnt_en) begin |
| 508 | if (wvalid_o && wready_i) begin |
| 509 | beat_cnt <= beat_cnt + 1'b1; |
| 510 | end |
| 511 | end else if (rd_cnt_en) begin |
| 512 | if (rvalid_i && rready_o) begin |
| 513 | beat_cnt <= beat_cnt + 1'b1; |
| 514 | end |
| 515 | end |
| 516 | end |
| 517 | |
| 518 | // latch data programmed |
| 519 | always_ff @(posedge clk_i) begin |
| 520 | if (prog_cnt_en && wvalid_o && wready_i) begin |
| 521 | prog_data[beat_cnt] <= rand_i; |
| 522 | end |
| 523 | end |
| 524 | |
| 525 | // once error is set to off, it cannot be unset without a reboot |
Timothy Chen | 5e85607 | 2020-12-17 14:48:06 -0800 | [diff] [blame] | 526 | // On - no errors |
| 527 | // Off - errors were observed |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 528 | always_ff @(posedge clk_i or negedge rst_ni) begin |
| 529 | if (!rst_ni) begin |
| 530 | err_sts <= lc_ctrl_pkg::On; |
Timothy Chen | 5e85607 | 2020-12-17 14:48:06 -0800 | [diff] [blame] | 531 | end else if (err_sts_set && (err_sts != lc_ctrl_pkg::Off)) begin |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 532 | err_sts <= lc_ctrl_pkg::Off; |
| 533 | end |
| 534 | end |
| 535 | |
| 536 | logic rma_start; |
| 537 | logic [BusAddrW-1:0] rma_addr; |
| 538 | flash_op_e rma_op; |
| 539 | flash_part_e rma_part_sel; |
| 540 | logic [InfoTypesWidth-1:0] rma_info_sel; |
| 541 | logic [11:0] rma_num_words; |
| 542 | |
| 543 | assign rma_addr = {RmaWipeEntries[rma_wipe_idx].bank, |
| 544 | page_cnt[PageW-1:0], |
| 545 | word_cnt[BusWordW-1:0]}; |
| 546 | |
| 547 | assign rma_part_sel = RmaWipeEntries[rma_wipe_idx].part; |
| 548 | assign rma_info_sel = RmaWipeEntries[rma_wipe_idx].info_sel; |
Timothy Chen | 6efde1e | 2021-04-16 15:39:23 -0700 | [diff] [blame] | 549 | assign rma_num_words = WidthMultiple - 1; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 550 | |
| 551 | |
| 552 | //fsm for handling the actual wipe |
| 553 | always_comb begin |
| 554 | rma_state_d = rma_state_q; |
| 555 | rma_wipe_done = 1'b0; |
| 556 | rma_start = 1'b0; |
| 557 | rma_op = FlashOpInvalid; |
| 558 | err_sts_set = 1'b0; |
| 559 | page_cnt_ld = 1'b0; |
| 560 | page_cnt_incr = 1'b0; |
| 561 | page_cnt_clr = 1'b0; |
| 562 | word_cnt_incr = 1'b0; |
| 563 | word_cnt_clr = 1'b0; |
| 564 | prog_cnt_en = 1'b0; |
| 565 | rd_cnt_en = 1'b0; |
| 566 | beat_cnt_clr = 1'b0; |
| 567 | |
| 568 | unique case (rma_state_q) |
| 569 | |
| 570 | StRmaIdle: begin |
| 571 | if (rma_wipe_req) begin |
| 572 | rma_state_d = StRmaPageSel; |
| 573 | page_cnt_ld = 1'b1; |
| 574 | end |
| 575 | end |
| 576 | |
| 577 | StRmaPageSel: begin |
| 578 | if (page_cnt < end_page) begin |
| 579 | rma_state_d = StRmaErase; |
| 580 | end else begin |
| 581 | rma_wipe_done = 1'b1; |
| 582 | page_cnt_clr = 1'b1; |
| 583 | rma_state_d = StRmaIdle; |
| 584 | end |
| 585 | end |
| 586 | |
| 587 | StRmaErase: begin |
| 588 | rma_start = 1'b1; |
| 589 | rma_op = FlashOpErase; |
| 590 | if (done_i) begin |
| 591 | err_sts_set = err_i; |
| 592 | rma_state_d = StRmaWordSel; |
| 593 | end |
| 594 | end |
| 595 | |
| 596 | StRmaWordSel: begin |
Timothy Chen | 6efde1e | 2021-04-16 15:39:23 -0700 | [diff] [blame] | 597 | if (word_cnt < BusWordsPerPage) begin |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 598 | rma_state_d = StRmaProgram; |
| 599 | end else begin |
| 600 | word_cnt_clr = 1'b1; |
| 601 | page_cnt_incr = 1'b1; |
| 602 | rma_state_d = StRmaPageSel; |
| 603 | end |
| 604 | end |
| 605 | |
| 606 | StRmaProgram: begin |
| 607 | rma_start = 1'b1; |
| 608 | rma_op = FlashOpProgram; |
| 609 | prog_cnt_en = 1'b1; |
| 610 | |
Rupert Swarbrick | 723c05e | 2021-03-30 09:26:17 +0100 | [diff] [blame] | 611 | if ((beat_cnt == MaxBeatCnt[BeatCntWidth-1:0]) && wready_i) begin |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 612 | rma_state_d = StRmaProgramWait; |
| 613 | end |
| 614 | end |
| 615 | |
| 616 | StRmaProgramWait: begin |
| 617 | rma_start = 1'b1; |
| 618 | rma_op = FlashOpProgram; |
| 619 | |
| 620 | if (done_i) begin |
| 621 | beat_cnt_clr = 1'b1; |
| 622 | err_sts_set = err_i; |
| 623 | rma_state_d = StRmaRdVerify; |
| 624 | end |
| 625 | end |
| 626 | |
| 627 | StRmaRdVerify: begin |
| 628 | rma_start = 1'b1; |
| 629 | rma_op = FlashOpRead; |
| 630 | rd_cnt_en = 1'b1; |
| 631 | |
Rupert Swarbrick | 723c05e | 2021-03-30 09:26:17 +0100 | [diff] [blame] | 632 | if ((beat_cnt == MaxBeatCnt[BeatCntWidth-1:0]) && done_i) begin |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 633 | beat_cnt_clr = 1'b1; |
| 634 | word_cnt_incr = 1'b1; |
| 635 | rma_state_d = StRmaWordSel; |
| 636 | end |
| 637 | |
| 638 | if (rvalid_i && rready_o) begin |
| 639 | err_sts_set = prog_data[beat_cnt] != rdata_i; |
| 640 | end |
| 641 | end |
| 642 | |
| 643 | default: begin |
| 644 | err_sts_set = 1'b1; |
| 645 | end |
| 646 | |
| 647 | endcase // unique case (rma_state_q) |
| 648 | end // always_comb |
| 649 | |
| 650 | assign wdata_o = rand_i; |
| 651 | assign wvalid_o = prog_cnt_en; |
| 652 | assign ctrl_o.start.q = seed_phase ? start : rma_start; |
| 653 | assign ctrl_o.op.q = seed_phase ? op : rma_op; |
Timothy Chen | 103b4cb | 2020-09-12 15:31:37 -0700 | [diff] [blame] | 654 | assign ctrl_o.prog_sel.q = prog_type; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 655 | assign ctrl_o.erase_sel.q = erase_type; |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 656 | assign ctrl_o.partition_sel.q = seed_phase ? part_sel : rma_part_sel; |
| 657 | assign ctrl_o.info_sel.q = seed_phase ? info_sel : rma_info_sel; |
| 658 | assign ctrl_o.num = seed_phase ? num_words : rma_num_words; |
Timothy Chen | 2df9561 | 2020-08-31 15:29:12 -0700 | [diff] [blame] | 659 | // address is consistent with software width format (full bus) |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 660 | assign addr_o = seed_phase ? top_pkg::TL_AW'({addr, {BusByteWidth{1'b0}}}) : |
| 661 | top_pkg::TL_AW'({rma_addr, {BusByteWidth{1'b0}}}); |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 662 | assign init_busy_o = seed_phase; |
| 663 | assign req_o = seed_phase | rma_phase; |
| 664 | assign rready_o = 1'b1; |
| 665 | assign seeds_o = seeds_q; |
Timothy Chen | 8204f1d | 2020-09-02 19:05:39 -0700 | [diff] [blame] | 666 | assign phase_o = phase; |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 667 | |
Timothy Chen | d39402a | 2020-12-15 20:34:09 -0800 | [diff] [blame] | 668 | assign rma_ack_o = rma_ack_q; |
| 669 | |
Timothy Chen | f52a461 | 2020-12-04 20:37:49 -0800 | [diff] [blame] | 670 | logic unused_seed_valid; |
| 671 | assign unused_seed_valid = otp_key_rsp_i.seed_valid; |
| 672 | |
Timothy Chen | 5cdde15 | 2020-08-14 20:46:21 -0700 | [diff] [blame] | 673 | endmodule // flash_ctrl_lcmgr |