aboutsummaryrefslogtreecommitdiff
path: root/src/rtl/keywrap_mkmif.v
blob: 88af6f8f2a8c67d9b2548372463731e045e4f9e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//======================================================================
//
// keywrap_mkmif.v
// ---------------
// Wrapper for the mkmif_core. This allow us to simplify the
// integration of the mkmif in keywrap_core.
//
//
// Author: Joachim Strombergson
// Copyright (c) 2018, NORDUnet A/S
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// - Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
//
// - Neither the name of the NORDUnet nor the names of its contributors may
//   be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//======================================================================

module keywrap_mkmif (
                      input wire            clk,
                      input wire            reset_n,

                      output wire           mkm_spi_sclk,
                      output wire           mkm_spi_cs_n,
                      input wire            mkm_spi_do,
                      output wire           mkm_spi_di,

                      input wire            init,
                      input wire            read,
                      input wire            write,
                      input wire            key_status,
                      output wire           ready,

                      input wire [31 : 0]   wr_status,
                      output wire [31 : 0]  rd_status,
                      input wire [255 : 0]  wr_key,
                      output wire [255 : 0] rd_key
                     );


  //----------------------------------------------------------------
  // Paramenters and local defines.
  //----------------------------------------------------------------
  localparam CTRL_IDLE             = 3'h0;
  localparam CTRL_WAIT             = 3'h1;
  localparam CTRL_READ_STATUS_WAIT = 3'h2;
  localparam CTRL_READ_KEY         = 3'h3;
  localparam CTRL_READ_KEY_WAIT    = 3'h4;
  localparam CTRL_WRITE_KEY        = 3'h5;
  localparam CTRL_WRITE_KEY_WAIT   = 3'h6;

  // Addresses for storage in the mkm
  localparam MKM_STATUS_WORD     = 16'h00;
  localparam MKM_KEY_BASE_WORD   = 16'h04;

  localparam DEFAULT_SCLK_DIV    = 16'h0020;


  //----------------------------------------------------------------
  // Registers and memories including control signals.
  //----------------------------------------------------------------
  reg [31 : 0] key_reg [0 : 7];
  reg          key_we;

  reg [2 : 0]  key_word_ctr_reg;
  reg [2 : 0]  key_word_ctr_new;
  reg          key_word_ctr_we;
  reg          key_word_ctr_rst;
  reg          key_word_ctr_inc;

  reg [15 : 0] mkm_addr_reg;
  reg [15 : 0] mkm_addr_new;
  reg          mkm_addr_we;

  reg [31 : 0] mkm_write_data_reg;
  reg [31 : 0] mkm_write_data_new;
  reg          mkm_write_data_we;

  reg [31 : 0] status_reg;
  reg [31 : 0] status_new;
  reg          status_we;

  reg          ready_reg;
  reg          ready_new;
  reg          ready_we;

  reg [2 : 0]  keywrap_mkmif_ctrl_reg;
  reg [2 : 0]  keywrap_mkmif_ctrl_new;
  reg          keywrap_mkmif_ctrl_we;


  //----------------------------------------------------------------
  // Wires.
  //----------------------------------------------------------------
  reg           mkm_init_op;
  reg           mkm_read_op;
  reg           mkm_write_op;
  wire          mkm_ready;
  wire          mkm_valid;
  reg [15 : 0]  mkm_addr;
  reg [31 : 0]  mkm_write_data;
  wire [31 : 0] mkm_read_data;


  //----------------------------------------------------------------
  // Instantiations.
  //----------------------------------------------------------------
  mkmif_core mkm(
                 .clk(clk),
                 .reset_n(reset_n),

                 .spi_sclk(mkm_spi_sclk),
                 .spi_cs_n(mkm_spi_cs_n),
                 .spi_do(mkm_spi_do),
                 .spi_di(mkm_spi_di),

                 .init_op(mkm_init_op),
                 .read_op(mkm_read_op),
                 .write_op(mkm_write_op),

                 .ready(mkm_ready),
                 .valid(mkm_valid),

                 .sclk_div(DEFAULT_SCLK_DIV),
                 .addr(mkm_addr_reg),
                 .write_data(mkm_write_data_reg),
                 .read_data(mkm_read_data)
                );


  //----------------------------------------------------------------
  // Assignments for ports.
  //----------------------------------------------------------------
  assign ready = ready_reg;

  assign rd_status = status_reg;

  assign rd_key = {key_reg[7], key_reg[6], key_reg[5], key_reg[4],
                   key_reg[3], key_reg[2], key_reg[1], key_reg[0]};


  //----------------------------------------------------------------
  // reg_update
  //----------------------------------------------------------------
  always @ (posedge clk or negedge reset_n)
    begin: reg_update
      integer i;

      if (!reset_n)
        begin
          for (i = 0 ; i < 8 ; i = i + 1)
            key_reg[i] <= 32'h0;

          ready_reg              <= 1'h1;
          status_reg             <= 32'h0;
          mkm_addr_reg           <= 16'h0;
          mkm_write_data_reg     <= 32'h0;
          key_word_ctr_reg       <= 3'h0;
          keywrap_mkmif_ctrl_reg <= CTRL_IDLE;
       end

      else
        begin
          if (ready_we)
            ready_reg <= ready_new;

          if (mkm_addr_we)
            mkm_addr_reg <= mkm_addr_new;

          if (mkm_write_data_we)
            mkm_write_data_reg <= mkm_write_data_new;

          if (key_we)
            key_reg[key_word_ctr_reg] <= mkm_read_data;

          if (status_we)
            status_reg <= mkm_read_data;

          if (key_word_ctr_we)
            key_word_ctr_reg <= key_word_ctr_new;

          if (keywrap_mkmif_ctrl_we)
            keywrap_mkmif_ctrl_reg <= keywrap_mkmif_ctrl_new;
        end
    end // reg_update


  //----------------------------------------------------------------
  // key_word_ctr
  //----------------------------------------------------------------
  always @*
    begin : key_word_ctr
      key_word_ctr_new = 3'h0;
      key_word_ctr_we  = 1'h0;

      if (key_word_ctr_rst)
        begin
          key_word_ctr_new = 3'h0;
          key_word_ctr_we  = 1'h1;
        end

      if (key_word_ctr_inc)
        begin
          key_word_ctr_new = key_word_ctr_reg + 1'h1;
          key_word_ctr_we  = 1'h1;
        end

    end


  //----------------------------------------------------------------
  // keywrap_mkmif_ctrl
  //----------------------------------------------------------------
  always @*
    begin : keywrap_mkmif_ctrl
      ready_new              = 1'h0;
      ready_we               = 1'h0;
      key_we                 = 1'h0;
      key_word_ctr_rst       = 1'h0;
      key_word_ctr_inc       = 1'h0;
      status_we              = 1'h0;
      mkm_init_op            = 1'h0;
      mkm_read_op            = 1'h0;
      mkm_write_op           = 1'h0;
      mkm_addr_new           = 16'h0;
      mkm_addr_we            = 1'h0;
      mkm_write_data_new     = 32'h0;
      mkm_write_data_we      = 1'h0;
      keywrap_mkmif_ctrl_new = CTRL_IDLE;
      keywrap_mkmif_ctrl_we  = 1'h0;

      case (keywrap_mkmif_ctrl_reg)
        CTRL_IDLE:
          begin
            if (init)
              begin
                mkm_init_op            = 1'h1;
                ready_new              = 1'h0;
                ready_we               = 1'h1;
                keywrap_mkmif_ctrl_new = CTRL_WAIT;
                keywrap_mkmif_ctrl_we  = 1'h1;
              end

            if (read)
              begin
                ready_new = 1'h0;
                ready_we  = 1'h1;

                if (key_status)
                  begin
                    key_word_ctr_rst       = 1'h1;
                    keywrap_mkmif_ctrl_new = CTRL_READ_KEY;
                    keywrap_mkmif_ctrl_we  = 1'h1;
                  end
                else
                  begin
                    mkm_read_op            = 1'h1;
                    mkm_addr_new           = MKM_STATUS_WORD;
                    mkm_addr_we            = 1'h1;
                    keywrap_mkmif_ctrl_new = CTRL_READ_STATUS_WAIT;
                    keywrap_mkmif_ctrl_we  = 1'h1;
                  end
              end

            if (write)
              begin
                ready_new = 1'h0;
                ready_we  = 1'h1;

                if (key_status)
                  begin
                    key_word_ctr_rst       = 1'h1;
                    keywrap_mkmif_ctrl_new = CTRL_WRITE_KEY;
                    keywrap_mkmif_ctrl_we  = 1'h1;
                  end
                else
                  begin
                    mkm_write_op           = 1'h1;
                    mkm_addr_new           = MKM_STATUS_WORD;
                    mkm_addr_we            = 1'h1;
                    mkm_write_data_new     = wr_status;
                    mkm_write_data_we      = 1'h1;
                    keywrap_mkmif_ctrl_new = CTRL_WAIT;
                    keywrap_mkmif_ctrl_we  = 1'h1;
                  end
              end
          end


        CTRL_WAIT:
          begin
            if (mkm_ready)
              begin
                ready_new              = 1'h1;
                ready_we               = 1'h1;
                keywrap_mkmif_ctrl_new = CTRL_IDLE;
                keywrap_mkmif_ctrl_we  = 1'h1;
              end
          end

        CTRL_READ_STATUS_WAIT:
          begin
            if (mkm_ready)
              begin
                status_we              = 1'h1;
                ready_new              = 1'h1;
                ready_we               = 1'h1;
                keywrap_mkmif_ctrl_new = CTRL_IDLE;
                keywrap_mkmif_ctrl_we  = 1'h1;
              end
          end

        CTRL_READ_KEY:
          begin
            if (key_word_ctr_reg < 8)
              begin
                mkm_read_op            = 1'h1;
                mkm_addr_new           = MKM_KEY_BASE_WORD + {key_word_ctr_reg, 2'h0};
                mkm_addr_we            = 1'h1;
                keywrap_mkmif_ctrl_new = CTRL_WRITE_KEY_WAIT;
                keywrap_mkmif_ctrl_we  = 1'h1;
              end
            else
              begin
                ready_new              = 1'h1;
                ready_we               = 1'h1;
                keywrap_mkmif_ctrl_new = CTRL_IDLE;
                keywrap_mkmif_ctrl_we  = 1'h1;
              end
          end

        CTRL_READ_KEY_WAIT:
          begin
            if (mkm_ready)
              begin
                key_we                 = 1'h1;
                key_word_ctr_inc       = 1'h1;
                keywrap_mkmif_ctrl_new = CTRL_READ_KEY;
                keywrap_mkmif_ctrl_we  = 1'h1;
              end
          end

        CTRL_WRITE_KEY:
          begin
            if (key_word_ctr_reg < 8)
              begin
                mkm_write_op           = 1'h1;
                mkm_addr_new           = MKM_KEY_BASE_WORD + {key_word_ctr_reg, 2'h0};
                mkm_addr_we            = 1'h1;
                mkm_write_data_new     = wr_key[key_word_ctr_reg * 32 +: 32];
                mkm_write_data_we      = 1'h1;
                keywrap_mkmif_ctrl_new = CTRL_WRITE_KEY_WAIT;
                keywrap_mkmif_ctrl_we  = 1'h1;
              end
            else
              begin
                ready_new              = 1'h1;
                ready_we               = 1'h1;
                keywrap_mkmif_ctrl_new = CTRL_IDLE;
                keywrap_mkmif_ctrl_we  = 1'h1;
              end
          end

        CTRL_WRITE_KEY_WAIT:
          begin
            if (mkm_ready)
              begin
                key_word_ctr_inc       = 1'h1;
                keywrap_mkmif_ctrl_new = CTRL_WRITE_KEY;
                keywrap_mkmif_ctrl_we  = 1'h1;
              end
          end

        default:
          begin

          end
      endcase // case (keywrap_mkmif_ctrl_reg)
    end // keywrap_mkmif_ctrl

endmodule // keywrap_mkmif

//======================================================================
// EOF keywrap_mkmif.v
//======================================================================