-
Notifications
You must be signed in to change notification settings - Fork 729
/
spi_host_fsm.sv
655 lines (587 loc) · 23.5 KB
/
spi_host_fsm.sv
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
//
// Core Implemenation module for Serial Peripheral Interface (SPI) Host IP.
//
module spi_host_fsm
import spi_host_cmd_pkg::*;
#(
parameter int NumCS = 1
) (
input clk_i,
input rst_ni,
input en_i,
input command_t command_i,
input command_valid_i,
output logic command_ready_o,
output logic sck_o,
output logic [NumCS-1:0] csb_o,
output logic [3:0] sd_en_o,
output logic last_read_o,
output logic last_write_o,
output logic wr_en_o,
input sr_wr_ready_i,
output logic rd_en_o,
input sr_rd_ready_i,
output logic sample_en_o,
output logic shift_en_o,
output logic [1:0] speed_o,
output logic full_cyc_o,
output logic rx_stall_o,
output logic tx_stall_o,
output logic active_o,
input sw_rst_i
);
logic is_idle;
logic [15:0] clkdiv, clkdiv_q;
logic [15:0] clk_cntr_q, clk_cntr_d;
logic clk_cntr_en;
logic [CSW-1:0] csid;
logic [CSW-1:0] csid_q;
logic [3:0] csnidle, csntrail, csnlead;
logic [3:0] csnidle_q, csntrail_q, csnlead_q;
logic full_cyc, cpha, cpol;
logic full_cyc_q, cpha_q, cpol_q;
// Unlike the configopts fields, the segment fields can change in back-to-back segment operation.
// (If a change in only the configopts fields is detected, the FSM transitions to idle instead).
// For that reason, when using the segment fields in back-to-back operations, we have to bear
// in mind context and determine whether it is appropriate to use the value for the previous
// segment or the following segment.
// For instance, the cmd_rd_en signal is sometimes used to push segment bytes into the shift
// register, and so in that case it is important to use cmd_rd_en_q in that sense. The same
// signal is consulted to load the bit counter at the /beginning/ of each byte or dummy cycle,
// and so in this context it is appropriate to use cmd_rd_en_d (which refers to the value from
// the immediately following segment).
logic [1:0] cmd_speed_d, cmd_speed_q;
logic cmd_wr_en_d, cmd_wr_en_q;
logic cmd_rd_en_d, cmd_rd_en_q;
logic [8:0] cmd_len_d, cmd_len_q;
logic csaat;
logic csaat_q;
logic [2:0] bit_cntr_d, bit_cntr_q;
logic [8:0] byte_cntr_cpha0_d, byte_cntr_cpha1_d, byte_cntr_cpha0_q, byte_cntr_cpha1_q;
logic [8:0] byte_cntr_early, byte_cntr_late;
logic [3:0] wait_cntr_d, wait_cntr_q;
logic last_bit, last_byte;
logic state_changing;
logic byte_starting, byte_starting_cpha0, byte_starting_cpha1;
logic bit_shifting, bit_shifting_cpha0, bit_shifting_cpha1;
logic byte_ending, byte_ending_cpha0, byte_ending_cpha1;
logic sample_en_d, sample_en_q, sample_en_q2;
logic config_changed;
logic fsm_en;
// new_command: signals a new segment input
logic new_command;
logic csb_single_d;
logic [NumCS-1:0] csb_q;
logic sck_d, sck_q;
logic wr_en_internal, rd_en_internal, sample_en_internal, shift_en_internal;
logic stall;
assign stall = rx_stall_o | tx_stall_o;
// suppress output pulses if stalled.
assign wr_en_o = wr_en_internal & ~stall;
assign rd_en_o = rd_en_internal & ~stall;
assign sample_en_o = sample_en_internal & ~stall;
assign shift_en_o = shift_en_internal & ~stall;
typedef enum logic [2:0] {
Idle,
WaitLead,
InternalClkLow,
InternalClkHigh,
WaitTrail,
WaitIdle,
CSBSwitch,
IdleCSBActive
} spi_host_st_e;
spi_host_st_e state_q, state_d;
logic command_ready_int;
assign command_ready_o = command_ready_int & ~stall;
assign new_command = command_valid_i && command_ready_int;
assign config_changed = (command_i.configopts.cpol != cpol_q) ||
(command_i.configopts.cpha != cpha_q) ||
(command_i.configopts.full_cyc != full_cyc_q) ||
(command_i.configopts.csnidle != csnidle_q) ||
(command_i.configopts.csntrail != csntrail_q) ||
(command_i.configopts.csnlead != csnlead_q) ||
(command_i.configopts.clkdiv != clkdiv_q);
always_comb begin
csid = new_command ? command_i.csid : csid_q;
cpol = new_command ? command_i.configopts.cpol : cpol_q;
cpha = new_command ? command_i.configopts.cpha : cpha_q;
full_cyc = new_command ? command_i.configopts.full_cyc : full_cyc_q;
csnidle = new_command ? command_i.configopts.csnidle : csnidle_q;
csnlead = new_command ? command_i.configopts.csnlead : csnlead_q;
csntrail = new_command ? command_i.configopts.csntrail : csntrail_q;
clkdiv = new_command ? command_i.configopts.clkdiv : clkdiv_q;
csaat = new_command ? command_i.segment.csaat : csaat_q;
cmd_len_d = new_command ? command_i.segment.len : cmd_len_q;
cmd_wr_en_d = new_command ? command_i.segment.cmd_wr_en : cmd_wr_en_q;
cmd_rd_en_d = new_command ? command_i.segment.cmd_rd_en : cmd_rd_en_q;
cmd_speed_d = new_command ? command_i.segment.speed : cmd_speed_q;
end
always_ff @(posedge clk_i or negedge rst_ni) begin
if (!rst_ni) begin
csid_q <= {CSW{1'b0}};
cpol_q <= 1'b0;
cpha_q <= 1'b0;
full_cyc_q <= 1'b0;
csnidle_q <= 4'h0;
csnlead_q <= 4'h0;
csntrail_q <= 4'h0;
clkdiv_q <= 16'h0;
csaat_q <= 1'b0;
cmd_rd_en_q <= 1'b0;
cmd_wr_en_q <= 1'b0;
cmd_speed_q <= 2'b00;
cmd_len_q <= 9'h0;
end else begin
csid_q <= (new_command && !stall) ? csid : csid_q;
cpol_q <= (new_command && !stall) ? cpol : cpol_q;
cpha_q <= (new_command && !stall) ? cpha : cpha_q;
full_cyc_q <= (new_command && !stall) ? full_cyc : full_cyc_q;
csnidle_q <= (new_command && !stall) ? csnidle : csnidle_q;
csnlead_q <= (new_command && !stall) ? csnlead : csnlead_q;
csntrail_q <= (new_command && !stall) ? csntrail : csntrail_q;
clkdiv_q <= (new_command && !stall) ? clkdiv : clkdiv_q;
csaat_q <= (new_command && !stall) ? csaat : csaat_q;
cmd_wr_en_q <= (new_command && !stall) ? cmd_wr_en_d : cmd_wr_en_q;
cmd_rd_en_q <= (new_command && !stall) ? cmd_rd_en_d : cmd_rd_en_q;
cmd_speed_q <= (new_command && !stall) ? cmd_speed_d : cmd_speed_q;
cmd_len_q <= (new_command && !stall) ? cmd_len_d : cmd_len_q;
end
end
assign is_idle = (state_q == Idle) || (state_q == IdleCSBActive);
assign active_o = ~is_idle;
// New commands must always ensure there is the full setup time available.
assign clk_cntr_d = sw_rst_i ? 16'h0 :
!clk_cntr_en ? clk_cntr_q :
new_command ? clkdiv :
is_idle ? clk_cntr_q :
(clk_cntr_q == 16'h0) ? clkdiv :
clk_cntr_q - 1;
assign tx_stall_o = wr_en_internal & ~sr_wr_ready_i;
assign rx_stall_o = rd_en_internal & ~sr_rd_ready_i;
assign clk_cntr_en = en_i;
assign fsm_en = (clk_cntr_en && ((clk_cntr_q == 0) || is_idle));
spi_host_st_e next_state_after_idle;
always_comb begin
if (command_valid_i) begin
if (config_changed) begin
next_state_after_idle = CSBSwitch;
end else begin
next_state_after_idle = WaitLead;
end
end else begin
next_state_after_idle = Idle;
end
end
spi_host_st_e next_state_after_idle_csb_active;
logic command_ready_idle_csb_active;
always_comb begin
if (command_valid_i) begin
if (command_i.csid != csid_q) begin
//
// Do not acknowledge the command now, as it will trigger
// an update of the internal command and configuration registers.
// *Silently* transition to WaitTrail. The command
// will be acknowledged later, at the end of the WaitIdle state.
//
next_state_after_idle_csb_active = WaitTrail;
// Explicitly *suppress* command_ready
command_ready_idle_csb_active = 1'b0;
end else begin
if (cpha_q) begin
next_state_after_idle_csb_active = InternalClkHigh;
end else begin
next_state_after_idle_csb_active = InternalClkLow;
end
command_ready_idle_csb_active = 1'b1;
end
end else begin
next_state_after_idle_csb_active = IdleCSBActive;
command_ready_idle_csb_active = 1'b1;
end
end
//
// FSM main body: Controls state transitions and command_ready_o signaling
//
always_comb begin
state_d = state_q;
command_ready_int = 1'b0;
if (sw_rst_i) begin
state_d = Idle;
end else if (fsm_en) begin
unique case (state_q)
Idle: begin
// Initial state, wait for commands.
command_ready_int = 1'b1;
state_d = next_state_after_idle;
end
WaitLead: begin
// Transaction lead: CSB is low, waiting to start sck pulses.
if (wait_cntr_q == 4'h0) begin
state_d = InternalClkHigh;
end
end
InternalClkLow: begin
// Launch clock and full-cycle sampling clock for CPHA=0.
// Half-cycle sampling clock for CPHA=1.
// End-of-transaction clock level for both CPHA=0 and CPHA=1.
// One of two active clock states. sck is low when CPOL=0.
if (!last_bit || !last_byte || !cpha_q) begin
state_d = InternalClkHigh;
end else if (!csaat_q) begin
state_d = WaitTrail;
end else begin
state_d = next_state_after_idle_csb_active;
command_ready_int = command_ready_idle_csb_active;
end
end
InternalClkHigh: begin
// Half-cycle sampling clock for CPHA=0.
// Launch clock for CPHA=1. Note: Full-cycle sampling is not compatible with CPHA=1.
// One of two active clock states. sck is low when CPOL=0.
// Typically often the last state in a command, and so the next state depends on CSAAT,
// and of CSAAT is asserted, the details of the subsequent command.
if (!last_bit || !last_byte || cpha_q) begin
state_d = InternalClkLow;
// Check value of csaat for the previously submitted segment
end else if (!csaat_q) begin
state_d = WaitTrail;
end else begin
state_d = next_state_after_idle_csb_active;
command_ready_int = command_ready_idle_csb_active;
end
end
WaitTrail: begin
// Prepare to enter CSB high idle state by waiting csntrail cycles.
if (wait_cntr_q == 4'h0) begin
state_d = WaitIdle;
end
end
WaitIdle: begin
// Once CSB is high, wait for the designated number of cycles before accepting commands.
if (wait_cntr_q == 4'h0) begin
// ready to accept new command
command_ready_int = 1'b1;
state_d = next_state_after_idle;
end
end
CSBSwitch: begin
// Insert extra idle cycles when swtiching between CSID, this allows time to switch CPHA,
// CPOL and clkdiv settings, as well as guarantee that the idle delay requirements have
// been observed for the new device.
if (wait_cntr_q == 4'h0) begin
state_d = WaitLead;
end else begin
state_d = CSBSwitch;
end
end
IdleCSBActive: begin
// Wait for new commands, but with CSB held active low.
state_d = next_state_after_idle_csb_active;
command_ready_int = command_ready_idle_csb_active;
end
default: begin
command_ready_int = 1'b0;
state_d = Idle;
end
endcase
end
end
// All register updates freeze when a stall is detected.
// The definition of the stall signal looks ahead to determine whether a conflict is looming.
// Thus stall depends on state_d. Making state_d depend on stall
// would create a circular logic loop, and lint errors. Therefore stall is applied here, not
// in the previous always_comb block;
always_ff @(posedge clk_i or negedge rst_ni) begin
if (!rst_ni) begin
state_q <= Idle;
clk_cntr_q <= 16'h0;
end else begin
state_q <= stall ? state_q : state_d;
clk_cntr_q <= stall ? clk_cntr_q : clk_cntr_d;
end
end
logic segment_rd_en, segment_rd_en_cpha0, segment_rd_en_cpha1;
assign state_changing = (state_q != state_d);
assign byte_starting_cpha0 = ~sw_rst_i & state_changing &
((state_d == WaitLead) |
(state_d == InternalClkLow & bit_cntr_q==0));
assign bit_shifting_cpha0 = ~sw_rst_i & state_changing &
(state_d == InternalClkLow & bit_cntr_q != 0);
assign byte_ending_cpha0 = ~sw_rst_i & state_changing &
(state_q == InternalClkHigh & bit_cntr_q == 0);
assign segment_rd_en_cpha0 = cmd_rd_en_q;
// We can calculate byte transitions for CPHA=1 by noting
// that in this implmentation, the sck edges have a 1-1
// correspondence with FSM transitions.
// New bytes are loaded exactly one state transition behind the time
// when they would be loaded if CPHA=0
always_ff @(posedge clk_i or negedge rst_ni) begin
if (!rst_ni) begin
segment_rd_en_cpha1 <= 1'b0;
end else if (state_changing && !stall) begin
segment_rd_en_cpha1 <= segment_rd_en_cpha0;
end
end
// For CPHA=1, the launch and capture states flip roles, and data output
// does not begin in the WaitIdle state, when CSB first asserts. They
// otherwise look very similar to the CPHA=0 case.
assign byte_starting_cpha1 = ~sw_rst_i & state_changing &
(state_d == InternalClkHigh & bit_cntr_q==0);
assign bit_shifting_cpha1 = ~sw_rst_i & state_changing &
(state_d == InternalClkHigh & bit_cntr_q != 0);
assign byte_ending_cpha1 = ~sw_rst_i & state_changing &
(state_q == InternalClkLow & bit_cntr_q == 0);
assign byte_starting = (cpha == 1'b0) ? byte_starting_cpha0 :
byte_starting_cpha1;
assign byte_ending = (cpha == 1'b0) ? byte_ending_cpha0 :
byte_ending_cpha1;
assign bit_shifting = (cpha == 1'b0) ? bit_shifting_cpha0 :
bit_shifting_cpha1;
assign speed_o = cmd_speed_q;
assign segment_rd_en = (cpha == 1'b0) ? segment_rd_en_cpha0:
segment_rd_en_cpha1;
assign byte_cntr_early = (cpha == 1'b0) ? byte_cntr_cpha0_d :
byte_cntr_cpha1_d;
assign byte_cntr_late = (cpha == 1'b0) ? byte_cntr_cpha0_q :
byte_cntr_cpha1_q;
logic [2:0] shift_size;
logic [2:0] start_bit;
always_comb begin
if (!cmd_rd_en_d && !cmd_wr_en_d) begin
// direction == 0, means to send out
// a fixed number of pulses, NOT bytes.
// thus "last_bit" is always asserted,
// and the number of pulses is counted
// by byte_cntr_d.
shift_size = 0;
start_bit = 3'h0;
end else begin
unique case (cmd_speed_d)
Standard: begin
shift_size = 3'h1;
start_bit = 3'h7;
end
Dual: begin
shift_size = 3'h2;
start_bit = 3'h6;
end
Quad: begin
shift_size = 3'h4;
start_bit = 3'h4;
end
default: begin
// Invalid_speed;
shift_size = 3'h1;
start_bit = 3'h1;
end
endcase
end
end
assign bit_cntr_d = sw_rst_i ? 3'h0 :
!fsm_en ? bit_cntr_q :
byte_starting ? start_bit :
bit_shifting ? bit_cntr_q - shift_size :
bit_cntr_q;
assign last_bit = (bit_cntr_q == 3'h0);
//
// The variable last_byte is only used for updating the FSM state.
// For CPHA=1 operation, either byte_cntr_cpha0_q or byte_cntr_cpha1_q
// can drive the FSM properly. However, we explicitly choose
// byte_cntr_cpha0_q to avoid a combinational logic loop.
//
always_comb begin
if (cpha_q) begin
last_byte = (byte_cntr_cpha1_q == 9'h0);
end else begin
last_byte = (byte_cntr_cpha0_q == 9'h0);
end
end
// Note: when updating the byte_cntr in CPHA=0 mode with a new command value, the length must
// be pulled in directly from the command bus, cmd_len_d;
assign byte_cntr_cpha0_d = sw_rst_i ? 9'h0 :
!fsm_en ? byte_cntr_cpha0_q :
new_command ? cmd_len_d :
byte_ending_cpha0 ? byte_cntr_cpha0_q - 1 :
byte_cntr_cpha0_q;
assign byte_cntr_cpha1_d = sw_rst_i ? 9'h0 :
!fsm_en ? byte_cntr_cpha1_q :
new_command ? cmd_len_d :
byte_ending_cpha1 ? byte_cntr_cpha1_q - 1 :
byte_cntr_cpha1_q;
always_comb begin
if(sw_rst_i) begin
wait_cntr_d = 4'b0;
end else if (!fsm_en) begin
wait_cntr_d = wait_cntr_q;
end else if (state_changing) begin
unique case (state_d)
WaitLead: begin
wait_cntr_d = csnlead;
end
WaitTrail: begin
wait_cntr_d = csntrail;
end
WaitIdle: begin
wait_cntr_d = csnidle;
end
CSBSwitch: begin
wait_cntr_d = csnidle;
end
default: begin
// Hold wait cntr to zero
// for states that don't use it
wait_cntr_d = 4'b0;
end
endcase
end else if (wait_cntr_q == 0) begin
wait_cntr_d = 4'h0;
end else begin
wait_cntr_d = wait_cntr_q - 1;
end
end
always_ff @(posedge clk_i or negedge rst_ni) begin
if (!rst_ni) begin
bit_cntr_q <= 3'h0;
byte_cntr_cpha0_q <= 9'h0;
byte_cntr_cpha1_q <= 9'h0;
wait_cntr_q <= 4'h0;
end else begin
bit_cntr_q <= stall ? bit_cntr_q : bit_cntr_d;
byte_cntr_cpha0_q <= stall ? byte_cntr_cpha0_q : byte_cntr_cpha0_d;
byte_cntr_cpha1_q <= stall ? byte_cntr_cpha1_q : byte_cntr_cpha1_d;
wait_cntr_q <= stall ? wait_cntr_q : wait_cntr_d;
end
end
assign wr_en_internal = byte_starting & cmd_wr_en_d;
assign shift_en_internal = bit_shifting;
assign rd_en_internal = byte_ending & segment_rd_en;
assign sample_en_d = byte_starting | shift_en_o;
assign full_cyc_o = full_cyc;
assign last_read_o = (byte_cntr_late == 'h0) & rd_en_o & sr_rd_ready_i;
assign last_write_o = (byte_cntr_early == 'h0) & wr_en_o & sr_wr_ready_i;
always_ff @(posedge clk_i or negedge rst_ni) begin
if (!rst_ni) begin
sample_en_q <= 1'b0;
sample_en_q2 <= 1'b0;
end else begin
sample_en_q <= (fsm_en && !stall) ? sample_en_d : sample_en_q;
sample_en_q2 <= (fsm_en && !stall) ? sample_en_q : sample_en_q2;
end
end
assign sample_en_internal = full_cyc_o ? sample_en_q2 : sample_en_q;
always_comb begin
unique case (state_d)
WaitLead, InternalClkLow, InternalClkHigh, IdleCSBActive, WaitTrail:
csb_single_d = 1'b0;
default:
csb_single_d = 1'b1;
endcase
end
assign sck_d = cpol ? (state_d != InternalClkHigh) :
(state_d == InternalClkHigh);
assign sck_o = sck_q;
prim_flop_en u_sck_flop (
.clk_i,
.rst_ni,
.en_i(~stall),
.d_i(sck_d),
.q_o(sck_q)
);
for (genvar ii = 0; ii < NumCS; ii = ii + 1) begin : gen_csb_gen
always_ff @(posedge clk_i or negedge rst_ni) begin
if (!rst_ni) begin
csb_q[ii] <= 1'b1;
end else begin
csb_q[ii] <= (csid != ii) ? 1'b1 :
!stall ? csb_single_d :
csb_q[ii];
end
end
end : gen_csb_gen
// When 'cmd_wr_en_q' deasserts, we need to stretch the enable used
// to drive sd_en_o[0] until the end of the bit period. This is because
// 'cmd_wr_en_q' can change up to 1/2 cycle before the end of the period
// (depending on cpol/cpha).
// The signal 'last_bit' is active in the final period, so use that to start
// stretching the enable, and 'byte_ending' marks the period end.
logic cmd_wr_en_last_bit;
always_ff @(posedge clk_i or negedge rst_ni) begin
if (!rst_ni) begin
cmd_wr_en_last_bit <= 1'b0;
end else if (cmd_wr_en_q & last_bit) begin
cmd_wr_en_last_bit <= 1'b1;
end else if (byte_ending) begin
cmd_wr_en_last_bit <= 1'b0;
end else begin // Stretching..
cmd_wr_en_last_bit <= cmd_wr_en_last_bit;
end
end
assign csb_o = csb_q;
logic [3:0] sd_en_ff_d, sd_en_ff_q;
assign sd_en_ff_d = sd_en_o;
always_ff @(posedge clk_i or negedge rst_ni) begin
if (!rst_ni) begin
sd_en_ff_q <= 0;
end
//TODO: do we need an enable signal?
else begin
sd_en_ff_q <= sd_en_ff_d;
end
end
logic drive_posedge;
// CPOL / CPHASE truth table
// ---------------------------------------------
// CPHA | CPOL | Drive/sample |
// ---------------------------------------------
// 0 | 0 | drive negedge, sample posedge |
// 0 | 1 | drive posedge, sample negedge |
// 1 | 0 | drive posedge, sample negedge |
// 1 | 1 | drive negedge, sample posedge |
// ---------------------------------------------
assign drive_posedge = (cpol != cpha);
always_comb begin
if (&csb_o) begin
sd_en_o[3:0] = 4'h0;
end else begin
// Only update the sd_en_o on the right clock transition. Since sck_o
// is generated from the sys clock we ensure we only drive the data on the
// right clock edge
if( (drive_posedge && sck_o) ||
(!drive_posedge && !sck_o) ) begin
unique case (speed_o)
Standard: begin
sd_en_o[0] = cmd_wr_en_q | cmd_wr_en_last_bit;
sd_en_o[1] = 1'b0;
sd_en_o[3:2] = 2'b00;
end
Dual: begin
sd_en_o[1:0] = {2{cmd_wr_en_q}};
sd_en_o[3:2] = 2'b00;
end
Quad: begin
sd_en_o[3:0] = {4{cmd_wr_en_q}};
end
default: begin
// invalid speed
sd_en_o[3:0] = 4'h0;
end
endcase
end
else begin
sd_en_o = sd_en_ff_q;
end
end // else: !if(&csb_o)
end
//
// Assertions confirming valid user input.
//
`ASSERT(BidirOnlyInStdMode_A,
cmd_speed_d == Standard || !(cmd_rd_en_d && cmd_wr_en_d),
clk_i, rst_ni)
`ASSERT(ValidSpeed_A, cmd_speed_d != RsvdSpd, clk_i, rst_ni)
`ASSERT(ValidCSID_A, csid < NumCS, clk_i, rst_ni)
endmodule