forked from sutajiokousagi/betrusted-soc
-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
betrusted_soc.py
executable file
·2217 lines (1998 loc) · 123 KB
/
betrusted_soc.py
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# This variable defines all the external programs that this module
# relies on. lxbuildenv reads this variable in order to ensure
# the build will finish without exiting due to missing third-party
# programs.
LX_DEPENDENCIES = ["riscv", "vivado"]
# Import lxbuildenv to integrate the deps/ directory
from re import S
import lxbuildenv
import litex.soc.doc as lxsocdoc
from pathlib import Path
import subprocess
import sys
from random import SystemRandom
import argparse
from migen import *
from migen.genlib.cdc import MultiReg, BlindTransfer, BusSynchronizer
from migen.genlib.resetsync import AsyncResetSynchronizer
from litex.build.generic_platform import *
from litex.build.xilinx import XilinxPlatform, VivadoProgrammer
from litex.soc.interconnect.csr import *
from litex.soc.interconnect.csr_eventmanager import *
from litex.soc.integration.soc_core import *
from litex.soc.integration.builder import *
from litex.soc.integration.doc import AutoDoc, ModuleDoc
from litex.soc.cores.clock import S7MMCM, S7IDELAYCTRL
from litex.soc.cores.i2s import S7I2S
from litex.soc.cores.spi_opi import S7SPIOPI
from litex.soc.integration.soc import SoCRegion
from litex.soc.interconnect import wishbone
from litex.soc.integration.soc import SoCRegion
from gateware.rom_block import BlockRom
from gateware import info
from gateware import sram_32_cached
from gateware import memlcd
from gateware import spi_7series as spi
from gateware import messible
from gateware import i2c
from gateware import ticktimer
from gateware.wdt import WDT
from gateware import spinor
from gateware import keyboard
from gateware import jtag_phy
from gateware.trng.ring_osc_v2 import TrngRingOscV2
from gateware import aes_opentitan as aes
from gateware import sha2_opentitan as sha2
from gateware import sha512_opentitan as sha512
from gateware.curve25519.engine import Engine
from gateware.timer_alwayson import TimerAlwaysOn
from gateware.keyrom import KeyRom
from gateware import perfcounter
from valentyusb.usbcore.cpu.eptri import TriEndpointInterface
from valentyusb.usbcore.io import IoBuf
# IOs ----------------------------------------------------------------------------------------------
_io_pvt = [ # PVT-generation I/Os
("clk12", 0, Pins("R3"), IOStandard("LVCMOS18")),
("jtag", 0,
Subsignal("tck", Pins("U11"), IOStandard("LVCMOS18")), # DVT
Subsignal("tms", Pins("P6"), IOStandard("LVCMOS18")), # DVT
Subsignal("tdi", Pins("P7"), IOStandard("LVCMOS18")), # DVT
Subsignal("tdo", Pins("R6"), IOStandard("LVCMOS18")), # DVT
),
("usb", 0,
Subsignal("d_p", Pins("C1"), IOStandard("LVCMOS33"), Misc("DRIVE=12")), # DVT
Subsignal("d_n", Pins("B1"), IOStandard("LVCMOS33"), Misc("DRIVE=12")), # DVT
Subsignal("pullup_p", Pins("D1"), IOStandard("LVCMOS33"), Misc("DRIVE=4")), # DVT
Misc("SLEW=SLOW"),
),
# USB PU/PD options are also available, but not wired up
# ("usb_alt", 0,
# Subsignal("pulldn_p", Pins("C2"), IOStandard("LVCMOS33")), # DVT
# Subsignal("pullup_n", Pins("B2"), IOStandard("LVCMOS33")), # DVT
# Subsignal("pulldn_n", Pins("A4"), IOStandard("LVCMOS33")), # DVT
# Misc("DRIVE=4"), Misc("SLEW=SLOW"),
# ),
("lpclk", 0, Pins("N15"), IOStandard("LVCMOS18")), # wifi_lpclk
# Audio interface
("i2s", 0,
Subsignal("clk", Pins("D12")),
Subsignal("tx", Pins("E13")), # au_sdi1
Subsignal("rx", Pins("C13")), # au_sdo1
Subsignal("sync", Pins("D14")),
IOStandard("LVCMOS33"),
Misc("SLEW=SLOW"), Misc("DRIVE=4"),
),
("au_mclk", 0, Pins("E12"), IOStandard("LVCMOS33"), Misc("SLEW=SLOW"), Misc("DRIVE=8")),
# I2C1 bus -- to RTC and audio CODEC
("i2c", 0,
Subsignal("scl", Pins("G2"), IOStandard("LVCMOS33")), # DVT
Subsignal("sda", Pins("F2"), IOStandard("LVCMOS33")), # DVT
Misc("SLEW=SLOW"), Misc("DRIVE=4"),
),
# RTC interrupt
("rtc_irq", 0, Pins("N5"), IOStandard("LVCMOS18")),
# COM interface to UP5K
("com", 0,
Subsignal("csn", Pins("T15"), IOStandard("LVCMOS18"), Misc("SLEW=SLOW"), Misc("DRIVE=4")),
Subsignal("cipo", Pins("P16"), IOStandard("LVCMOS18")),
Subsignal("copi", Pins("N18"), IOStandard("LVCMOS18"), Misc("SLEW=SLOW"), Misc("DRIVE=4")),
Subsignal("sclk", Pins("R16"), IOStandard("LVCMOS18"), Misc("SLEW=FAST"), Misc("DRIVE=8")),
Subsignal("hold", Pins("L13"), IOStandard("LVCMOS18")),
),
("com_irq", 0, Pins("M16"), IOStandard("LVCMOS18")),
# Keyboard scan matrix
("kbd", 0,
# "key" 0-8 are rows, 9-18 are columns
# column scan with 1's, so PD to default 0
Subsignal("row", Pins("A15 A17 A16 A14 C17 B16 B17 C14 B15"), Misc("PULLDOWN True")), # DVT
Subsignal("col", Pins("B13 C18 E14 D15 B18 D16 D17 F13 E15 A13")), # DVT
IOStandard("LVCMOS33"),
Misc("SLEW=SLOW"),
Misc("DRIVE=4"),
),
# LCD interface
("lcd", 0,
Subsignal("sclk", Pins("H17")), # DVT
Subsignal("scs", Pins("G17")), # DVT
Subsignal("si", Pins("H18")), # DVT
IOStandard("LVCMOS33"),
Misc("SLEW=SLOW"),
Misc("DRIVE=4"),
),
# SPI Flash
("spiflash_1x", 0, # clock needs to be accessed through STARTUPE2
Subsignal("cs_n", Pins("M13")),
Subsignal("copi", Pins("K17")),
Subsignal("cipo", Pins("K18")),
Subsignal("wp", Pins("L14")), # provisional
Subsignal("hold", Pins("M15")), # provisional
IOStandard("LVCMOS18")
),
("spiflash_8x", 0, # clock needs a separate override to meet timing
Subsignal("cs_n", Pins("M13"), Misc("SLEW=SLOW")),
Subsignal("dq", Pins("K17 K18 L14 M15 L17 L18 M14 N14"), Misc("SLEW=SLOW")),
Subsignal("dqs", Pins("R14"), Misc("SLEW=SLOW")),
Subsignal("ecs_n", Pins("L16"), Misc("SLEW=SLOW")),
Subsignal("sclk", Pins("C12"), Misc("SLEW=FAST")), # DVT
IOStandard("LVCMOS18"),
),
# SRAM
("sram", 0,
Subsignal("adr", Pins(
"V12 M5 P5 N4 V14 M3 R17 U15",
"M4 L6 K3 R18 U16 K1 R5 T2",
"U1 N1 L5 K2 M18 T6"),
IOStandard("LVCMOS18"),
Misc("SLEW=SLOW"),
),
Subsignal("ce_n", Pins("V5"), IOStandard("LVCMOS18"), Misc("PULLUP True")),
Subsignal("oe_n", Pins("U12"), IOStandard("LVCMOS18"), Misc("PULLUP True")),
Subsignal("we_n", Pins("K4"), IOStandard("LVCMOS18"), Misc("PULLUP True")),
Subsignal("zz_n", Pins("V17"), IOStandard("LVCMOS18"), Misc("PULLUP True"), Misc("SLEW=SLOW")),
Subsignal("d", Pins(
"M2 R4 P2 L4 L1 M1 R1 P1",
"U3 V2 V4 U2 N2 T1 K6 J6",
"V16 V15 U17 U18 P17 T18 P18 M17",
"N3 T4 V13 P15 T14 R15 T3 R7"),
IOStandard("LVCMOS18"),
Misc("SLEW=SLOW"),
),
Subsignal("dm_n", Pins("V3 R2 T5 T13"), IOStandard("LVCMOS18")),
),
]
_io_xous = [
("analog", 0,
Subsignal("usbdet_p", Pins("C3"), IOStandard("LVCMOS33")), # DVT
Subsignal("usbdet_n", Pins("A3"), IOStandard("LVCMOS33")), # DVT
Subsignal("vbus_div", Pins("C4"), IOStandard("LVCMOS33")), # DVT
Subsignal("noise0", Pins("C5"), IOStandard("LVCMOS33")), # DVT
Subsignal("noise1", Pins("A8"), IOStandard("LVCMOS33")), # DVT
# diff grounds
Subsignal("usbdet_p_n", Pins("B3"), IOStandard("LVCMOS33")), # DVT
Subsignal("usbdet_n_n", Pins("A2"), IOStandard("LVCMOS33")), # DVT
Subsignal("vbus_div_n", Pins("B4"), IOStandard("LVCMOS33")), # DVT
Subsignal("noise0_n", Pins("B5"), IOStandard("LVCMOS33")), # DVT
Subsignal("noise1_n", Pins("A7"), IOStandard("LVCMOS33")), # DVT
# dedicated pins (no I/O standard applicable)
Subsignal("ana_vn", Pins("K9")),
Subsignal("ana_vp", Pins("J10")),
),
("noise", 0,
Subsignal("noisebias_on", Pins("E17"), IOStandard("LVCMOS33")), # DVT
# Noise generator
Subsignal("noise_on", Pins("P14 R13"), IOStandard("LVCMOS18")),
),
# Power control signals
("power", 0,
Subsignal("audio_on", Pins("B7"), IOStandard("LVCMOS33")), # DVT
Subsignal("fpga_sys_on", Pins("A5"), IOStandard("LVCMOS33")), # DVT
Subsignal("allow_up5k_n", Pins("B14"), IOStandard("LVCMOS33")),
Subsignal("pwr_s0", Pins("U6"), IOStandard("LVCMOS18")),
# Subsignal("pwr_s1", Pins("L13"), IOStandard("LVCMOS18")), # DVT # PVT convert to "com hold"
# vibe motor
Subsignal("vibe_on", Pins("G13"), IOStandard("LVCMOS33")), # PVT
# reset EC
Subsignal("reset_ec", Pins("M6"), IOStandard("LVCMOS18")),
# PVT -- allow FPGA to recover crashed EC (invert polarity)
# USB_CC DFP attach
Subsignal("cc_id", Pins("D18"), IOStandard("LVCMOS33")), # DVT
# turn on the UP5K in case we are woken up by RTC
Subsignal("up5k_on", Pins("G18"), IOStandard("LVCMOS33")), # DVT -- T_TO_U_ON
Subsignal("boostmode", Pins("H16"), IOStandard("LVCMOS33"), Misc("PULLDOWN True"), Misc("DRIVE=4")), # PVT - for sourcing power in USB host mode
Subsignal("selfdestruct", Pins("J14"), IOStandard("LVCMOS33"), Misc("PULLDOWN True"), Misc("DRIVE=16"),),
# PVT - cut power to BBRAM key and unit in an annoying-to-reset fashion
Misc("SLEW=SLOW"),
Misc("DRIVE=4"),
),
# Top-side internal FPC header
("gpio", 0, Pins("F14 F15 E16 G15 H15 G16 F18 E18"), IOStandard("LVCMOS33"), Misc("SLEW=SLOW")), # PVT
]
_io_xous_pvt2 = [
("analog", 0,
Subsignal("usbdet_p", Pins("C3"), IOStandard("LVCMOS33")), # DVT
Subsignal("usbdet_n", Pins("A3"), IOStandard("LVCMOS33")), # DVT
Subsignal("vbus_div", Pins("C4"), IOStandard("LVCMOS33")), # DVT
Subsignal("noise0", Pins("C5"), IOStandard("LVCMOS33")), # DVT
Subsignal("noise1", Pins("A8"), IOStandard("LVCMOS33")), # DVT
Subsignal("gpio2", Pins("E16"), IOStandard("LVCMOS33")), # PVT2
Subsignal("gpio5", Pins("D7"), IOStandard("LVCMOS33")), # PVT2
# diff grounds
Subsignal("usbdet_p_n", Pins("B3"), IOStandard("LVCMOS33")), # DVT
Subsignal("usbdet_n_n", Pins("A2"), IOStandard("LVCMOS33")), # DVT
Subsignal("vbus_div_n", Pins("B4"), IOStandard("LVCMOS33")), # DVT
Subsignal("noise0_n", Pins("B5"), IOStandard("LVCMOS33")), # DVT
Subsignal("noise1_n", Pins("A7"), IOStandard("LVCMOS33")), # DVT
Subsignal("gpio2_n", Pins("E17"), IOStandard("LVCMOS33")), # PVT2
Subsignal("gpio5_n", Pins("C7"), IOStandard("LVCMOS33")), # PVT2
# dedicated pins (no I/O standard applicable)
Subsignal("ana_vn", Pins("K9")),
Subsignal("ana_vp", Pins("J10")),
),
("noise", 0,
Subsignal("noisebias_on", Pins("H14"), IOStandard("LVCMOS33")), # PVT2
# Noise generator
Subsignal("noise_on", Pins("P14 R13"), IOStandard("LVCMOS18")),
),
# Power control signals
("power", 0,
Subsignal("audio_on", Pins("B7"), IOStandard("LVCMOS33")), # DVT
Subsignal("fpga_sys_on", Pins("A5"), IOStandard("LVCMOS33")), # DVT
Subsignal("allow_up5k_n", Pins("B14"), IOStandard("LVCMOS33")),
Subsignal("pwr_s0", Pins("U6"), IOStandard("LVCMOS18")),
Subsignal("pwr_s0_replica", Pins("U7"), IOStandard("LVCMOS18")), # PVT2
# Subsignal("pwr_s1", Pins("L13"), IOStandard("LVCMOS18")), # DVT # PVT convert to "com hold"
# vibe motor
Subsignal("vibe_on", Pins("G13"), IOStandard("LVCMOS33")), # PVT
# reset EC
Subsignal("reset_ec", Pins("M6"), IOStandard("LVCMOS18")),
# PVT -- allow FPGA to recover crashed EC (invert polarity)
# USB_CC DFP attach
Subsignal("cc_id", Pins("D18"), IOStandard("LVCMOS33")), # DVT
# turn on the UP5K in case we are woken up by RTC
Subsignal("up5k_on", Pins("G18"), IOStandard("LVCMOS33")), # DVT -- T_TO_U_ON
Subsignal("boostmode", Pins("H16"), IOStandard("LVCMOS33"), Misc("PULLDOWN True"), Misc("DRIVE=4")), # PVT - for sourcing power in USB host mode
Subsignal("selfdestruct", Pins("J14"), IOStandard("LVCMOS33"), Misc("PULLDOWN True"), Misc("DRIVE=16"),),
# PVT - cut power to BBRAM key and unit in an annoying-to-reset fashion
Misc("SLEW=SLOW"),
Misc("DRIVE=4"),
),
# mixed digital/analog: gpio2 and gpio5 are mapped to bogus, non-connected pins (E6/D6)
("gpio", 0, Pins("F14 F15 E6 G15 H15 D6 F18 E18"), IOStandard("LVCMOS33"), Misc("SLEW=SLOW")), # PVT2
]
_io_gpio_digital_only = [ # todo - add a command line config option + mod the analog_pads record to accommodate digital-only mode
# Top-side internal FPC header
# digital-only pinout for GPIOs
("gpio", 0, Pins("F14 F15 E16 G15 H15 D7 F18 E18"), IOStandard("LVCMOS33"), Misc("SLEW=SLOW")), # PVT2
]
_io_fw = [
("analog", 0,
Subsignal("usbdet_p", Pins("C3"), IOStandard("LVCMOS33")), # DVT
Subsignal("usbdet_n", Pins("A3"), IOStandard("LVCMOS33")), # DVT
Subsignal("vbus_div", Pins("C4"), IOStandard("LVCMOS33")), # DVT
Subsignal("noise0", Pins("C5"), IOStandard("LVCMOS33")), # DVT
Subsignal("noise1", Pins("A8"), IOStandard("LVCMOS33")), # DVT
# diff grounds
Subsignal("usbdet_p_n", Pins("B3"), IOStandard("LVCMOS33")), # DVT
Subsignal("usbdet_n_n", Pins("A2"), IOStandard("LVCMOS33")), # DVT
Subsignal("vbus_div_n", Pins("B4"), IOStandard("LVCMOS33")), # DVT
Subsignal("noise0_n", Pins("B5"), IOStandard("LVCMOS33")), # DVT
Subsignal("noise1_n", Pins("A7"), IOStandard("LVCMOS33")), # DVT
# dedicated pins (no I/O standard applicable)
Subsignal("ana_vn", Pins("K9")),
Subsignal("ana_vp", Pins("J10")),
),
# Power control signals
("power", 0,
Subsignal("noisebias_on", Pins("E17"), IOStandard("LVCMOS33")), # DVT
# Noise generator
Subsignal("noise_on", Pins("P14 R13"), IOStandard("LVCMOS18")),
Subsignal("audio_on", Pins("B7"), IOStandard("LVCMOS33")), # DVT
Subsignal("fpga_sys_on", Pins("A5"), IOStandard("LVCMOS33")), # DVT
Subsignal("allow_up5k_n", Pins("B14"), IOStandard("LVCMOS33")),
Subsignal("pwr_s0", Pins("U6"), IOStandard("LVCMOS18")),
# Subsignal("pwr_s1", Pins("L13"), IOStandard("LVCMOS18")), # DVT # PVT convert to "com hold"
# vibe motor
Subsignal("vibe_on", Pins("G13"), IOStandard("LVCMOS33")), # PVT
# reset EC
Subsignal("reset_ec", Pins("M6"), IOStandard("LVCMOS18")),
# PVT -- allow FPGA to recover crashed EC (invert polarity)
# USB_CC DFP attach
Subsignal("cc_id", Pins("D18"), IOStandard("LVCMOS33")), # DVT
# turn on the UP5K in case we are woken up by RTC
Subsignal("up5k_on", Pins("G18"), IOStandard("LVCMOS33")), # DVT -- T_TO_U_ON
Subsignal("boostmode", Pins("H16"), IOStandard("LVCMOS33")), # PVT - for sourcing power in USB host mode
Subsignal("selfdestruct", Pins("J14"), IOStandard("LVCMOS33"), Misc("PULLDOWN True")),
# PVT - cut power to BBRAM key and unit in an annoying-to-reset fashion
Misc("SLEW=SLOW"),
Misc("DRIVE=4"),
),
# Top-side internal FPC header
("gpio", 0, Pins("F14 F15 E16 G15 H15 G16 F18 E18"), IOStandard("LVCMOS33"), Misc("SLEW=SLOW")), # PVT
]
_io_xous_modnoise = [
("analog", 0,
Subsignal("usbdet_p", Pins("C3"), IOStandard("LVCMOS33")), # DVT
Subsignal("usbdet_n", Pins("A3"), IOStandard("LVCMOS33")), # DVT
Subsignal("vbus_div", Pins("C4"), IOStandard("LVCMOS33")), # DVT
#Subsignal("noise0", Pins("C5"), IOStandard("LVCMOS33")), # DVT
#Subsignal("noise1", Pins("A8"), IOStandard("LVCMOS33")), # DVT
# diff grounds
Subsignal("usbdet_p_n", Pins("B3"), IOStandard("LVCMOS33")), # DVT
Subsignal("usbdet_n_n", Pins("A2"), IOStandard("LVCMOS33")), # DVT
Subsignal("vbus_div_n", Pins("B4"), IOStandard("LVCMOS33")), # DVT
#Subsignal("noise0_n", Pins("B5"), IOStandard("LVCMOS33")), # DVT
#Subsignal("noise1_n", Pins("A7"), IOStandard("LVCMOS33")), # DVT
# dedicated pins (no I/O standard applicable)
Subsignal("ana_vn", Pins("K9")),
Subsignal("ana_vp", Pins("J10")),
),
# Modular noise generator
("noise", 0,
Subsignal("noise_on", Pins("R13"), IOStandard("LVCMOS18"), Misc("SLEW=SLOW"), Misc("DRIVE=12")),
Subsignal("noise_in", Pins("P14"), IOStandard("LVCMOS18")),
Subsignal("phase0", Pins("C5"), IOStandard("LVCMOS33"), Misc("SLEW=SLOW")),
Subsignal("phase1", Pins("A8"), IOStandard("LVCMOS33"), Misc("SLEW=SLOW")),
),
# Power control signals
("power", 0,
Subsignal("audio_on", Pins("B7"), IOStandard("LVCMOS33")), # DVT
Subsignal("fpga_sys_on", Pins("A5"), IOStandard("LVCMOS33")), # DVT
Subsignal("allow_up5k_n", Pins("B14"), IOStandard("LVCMOS33")),
Subsignal("pwr_s0", Pins("U6"), IOStandard("LVCMOS18")),
# Subsignal("pwr_s1", Pins("L13"), IOStandard("LVCMOS18")), # DVT # PVT convert to "com hold"
# vibe motor
Subsignal("vibe_on", Pins("G13"), IOStandard("LVCMOS33")), # PVT
# reset EC
Subsignal("reset_ec", Pins("M6"), IOStandard("LVCMOS18")),
# PVT -- allow FPGA to recover crashed EC (invert polarity)
# USB_CC DFP attach
Subsignal("cc_id", Pins("D18"), IOStandard("LVCMOS33")), # DVT
# turn on the UP5K in case we are woken up by RTC
Subsignal("up5k_on", Pins("G18"), IOStandard("LVCMOS33")), # DVT -- T_TO_U_ON
Subsignal("boostmode", Pins("H16"), IOStandard("LVCMOS33")), # PVT - for sourcing power in USB host mode
Subsignal("selfdestruct", Pins("J14"), IOStandard("LVCMOS33"), Misc("PULLDOWN True")),
# PVT - cut power to BBRAM key and unit in an annoying-to-reset fashion
Misc("SLEW=SLOW"),
Misc("DRIVE=4"),
),
# Top-side internal FPC header
("gpio", 0, Pins("F14 F15 E16 G15 H15 G16 F18 E18"), IOStandard("LVCMOS33"), Misc("SLEW=SLOW")), # PVT
]
# use this config to wire the debug bridge UART to the Rpi
_io_uart_debug = [
("debug", 0, # wired to the Rpi
Subsignal("tx", Pins("V6")),
Subsignal("rx", Pins("V7"), Misc("PULLUP True")),
IOStandard("LVCMOS18"),
Misc("SLEW=SLOW"),
),
("serial", 0, # wired to the internal flex
Subsignal("tx", Pins("B18")), # debug0 breakout
Subsignal("rx", Pins("D15"), Misc("PULLUP True")), # debug1
IOStandard("LVCMOS33"),
Misc("SLEW=SLOW"),
),
]
# use this config to wire the console UART to the Rpi, and debug bridge to GPIOs
_io_uart_debug_swapped = [
("serial", 0, # wired to the RPi
Subsignal("tx", Pins("V6")),
Subsignal("rx", Pins("V7"), Misc("PULLUP True")),
IOStandard("LVCMOS18"),
Misc("SLEW=SLOW"),
),
("debug", 0, # wired to the internal flex
Subsignal("tx", Pins("B18")), # debug0 breakout
Subsignal("rx", Pins("D15"), Misc("PULLUP True")), # debug1
IOStandard("LVCMOS33"),
Misc("SLEW=SLOW"),
),
]
# Platform -----------------------------------------------------------------------------------------
class Platform(XilinxPlatform):
def __init__(self, io, toolchain="vivado", programmer="vivado", part="50", encrypt=False, make_mod=False, bbram=False, strategy='default'):
part = "xc7s" + part + "-csga324-1il"
XilinxPlatform.__init__(self, part, io, toolchain=toolchain)
if strategy != 'default':
self.toolchain.vivado_synth_directive = "PerformanceOptimized"
self.toolchain.opt_directive = "ExploreWithRemap"
self.toolchain.vivado_place_directive = "ExtraNetDelay_high" # ExtraTimingOpt
self.toolchain.vivado_post_place_phys_opt_directive = "Explore"
self.toolchain.vivado_route_directive = "AggressiveExplore"
self.toolchain.vivado_post_route_phys_opt_directive = "Explore"
# NOTE: to do quad-SPI mode, the QE bit has to be set in the SPINOR status register. OpenOCD
# won't do this natively, have to find a work-around (like using iMPACT to set it once)
self.add_platform_command(
"set_property CONFIG_VOLTAGE 1.8 [current_design]")
self.add_platform_command(
"set_property CFGBVS GND [current_design]")
self.add_platform_command(
"set_property BITSTREAM.CONFIG.CONFIGRATE 66 [current_design]")
self.add_platform_command(
"set_property BITSTREAM.CONFIG.SPI_BUSWIDTH 1 [current_design]")
self.toolchain.bitstream_commands = [
"set_property CONFIG_VOLTAGE 1.8 [current_design]",
"set_property CFGBVS GND [current_design]",
"set_property BITSTREAM.CONFIG.CONFIGRATE 66 [current_design]",
"set_property BITSTREAM.CONFIG.SPI_BUSWIDTH 1 [current_design]",
]
if encrypt:
type = 'eFUSE'
if bbram:
type = 'BBRAM'
self.toolchain.bitstream_commands += [
"set_property BITSTREAM.ENCRYPTION.ENCRYPT YES [current_design]",
"set_property BITSTREAM.ENCRYPTION.ENCRYPTKEYSELECT {} [current_design]".format(type),
"set_property BITSTREAM.ENCRYPTION.KEYFILE ../../dummy.nky [current_design]"
]
self.toolchain.additional_commands += \
["write_cfgmem -verbose -force -format bin -interface spix1 -size 64 "
"-loadbit \"up 0x0 {build_name}.bit\" -file {build_name}.bin"]
self.programmer = programmer
self.toolchain.additional_commands += [
"create_slack_histogram -delay_type max -num_bins 100 -to [get_clocks -of_objects [get_pins MMCME2_ADV/CLKOUT6]] -significant_digits 3 -file histo_max.txt",
"create_slack_histogram -delay_type min -num_bins 100 -to [get_clocks -of_objects [get_pins MMCME2_ADV/CLKOUT6]] -significant_digits 3 -file histo_min.txt",
"report_timing -delay_type min_max -max_paths 100 -slack_less_than 0 -sort_by group -input_pins -routable_nets -name failures -file timing-failures.txt",
"report_timing_summary -delay_type min_max -max_paths 30000 -routable_nets -datasheet -path_type end -file timing-detail.txt",
]
# this routine retained in case we have to re-explore the bitstream to find the location of the ROM LUTs
if make_mod:
# build a version of the bitstream with a different INIT value for the ROM lut, so the offset frame can
# be discovered by diffing
for bit in range(0, 32):
for lut in range(4):
if lut == 0:
lutname = 'A'
elif lut == 1:
lutname = 'B'
elif lut == 2:
lutname = 'C'
else:
lutname = 'D'
self.toolchain.additional_commands += ["set_property INIT 64'hA6C355555555A6C3 [get_cells KEYROM" + str(bit) + lutname + "]"]
self.toolchain.additional_commands += ["write_bitstream -bin_file -force top-mod.bit"]
def create_programmer(self):
if self.programmer == "vivado":
return VivadoProgrammer(flash_part="n25q128-1.8v-spi-x1_x2_x4")
else:
raise ValueError("{} programmer is not supported".format(self.programmer))
def do_finalize(self, fragment):
XilinxPlatform.do_finalize(self, fragment)
# CRG ----------------------------------------------------------------------------------------------
class CRG(Module, AutoCSR):
def __init__(self, platform, sys_clk_freq, spinor_edge_delay_ns=2.5):
self.warm_reset = Signal()
self.power_down = Signal()
self.crypto_on = Signal()
self.clock_domains.cd_sys = ClockDomain()
self.clock_domains.cd_spi = ClockDomain()
self.clock_domains.cd_lpclk = ClockDomain()
self.clock_domains.cd_spinor = ClockDomain()
self.clock_domains.cd_clk200 = ClockDomain()
self.clock_domains.cd_clk50 = ClockDomain()
self.clock_domains.cd_usb_48 = ClockDomain()
self.clock_domains.cd_usb_12 = ClockDomain()
self.clock_domains.cd_raw_12 = ClockDomain()
self.clock_domains.cd_clk200_crypto = ClockDomain()
self.clock_domains.cd_sys_crypto = ClockDomain()
self.clock_domains.cd_sys_always_on = ClockDomain()
self.clock_domains.cd_clk50_always_on = ClockDomain()
# # #
sysclk_ns = 1e9 / sys_clk_freq
# convert delay request in ns to degrees, where 360 degrees is one whole clock period
phase_f = (spinor_edge_delay_ns / sysclk_ns) * 360
# round phase to the nearest multiple of 7.5 (needs to be a multiple of 45 / CLKOUT2_DIVIDE = 45 / 6 = 7.5
# note that CLKOUT2_DIVIDE is automatically calculated by mmcm.create_clkout() below
phase = round(phase_f / 7.5) * 7.5
clk32khz = platform.request("lpclk")
self.specials += Instance("BUFG", i_I=clk32khz, o_O=self.cd_lpclk.clk)
platform.add_platform_command("create_clock -name lpclk -period {:0.3f} [get_nets lpclk]".format(1e9 / 32.768e3))
clk12 = platform.request("clk12")
# Note: below feature cannot be used because Litex appends this *after* platform commands! This causes the generated
# clock derived constraints immediately below to fail, because .xdc file is parsed in-order, and the main clock needs
# to be created before the derived clocks. Instead, we use the line afterwards.
# platform.add_period_constraint(clk12, 1e9 / 12e6)
platform.add_platform_command("create_clock -name clk12 -period {:0.3f} [get_nets clk12]".format(1e9 / 12e6))
# The above constraint must strictly proceed the below create_generated_clock constraints in the .XDC file
# This allows PLLs/MMCMEs to be placed anywhere and reference the input clock
self.clk12_bufg = Signal()
self.specials += Instance("BUFG", i_I=clk12, o_O=self.clk12_bufg)
self.comb += self.cd_raw_12.clk.eq(self.clk12_bufg)
self.submodules.mmcm = mmcm = S7MMCM(speedgrade=-1)
mmcm.register_clkin(self.clk12_bufg, 12e6)
# we count on clocks being assigned to the MMCME2_ADV in order. If we make more MMCME2 or shift ordering, these constraints must change.
mmcm.create_clkout(self.cd_usb_48, 48e6, with_reset=False, buf="bufgce", ce=mmcm.locked) # 48 MHz for USB; always-on
platform.add_platform_command("create_generated_clock -name usb_48 [get_pins MMCME2_ADV/CLKOUT0]")
mmcm.create_clkout(self.cd_spi, 20e6, with_reset=False, buf="bufgce", ce=mmcm.locked & ~self.power_down)
platform.add_platform_command("create_generated_clock -name spi_clk [get_pins MMCME2_ADV/CLKOUT1]")
mmcm.create_clkout(self.cd_spinor, sys_clk_freq, phase=phase, with_reset=False, buf="bufgce", ce=mmcm.locked & ~self.power_down) # delayed version for SPINOR cclk (different from COM SPI above)
platform.add_platform_command("create_generated_clock -name spinor [get_pins MMCME2_ADV/CLKOUT2]")
# clk200 does not gate off because we want to keep the IDELAYCTRL block "warm"
mmcm.create_clkout(self.cd_clk200, 200e6, with_reset=False, buf="bufg",
gated_replicas={self.cd_clk200_crypto : (mmcm.locked & (~self.power_down | self.crypto_on))}) # 200MHz always-on required for IDELAYCTL
platform.add_platform_command("create_generated_clock -name clk200 [get_pins MMCME2_ADV/CLKOUT3]")
# clk50 is explicitly for the crypto unit, so it doesn't have the _crypto suffix, consfusingly...
mmcm.create_clkout(self.cd_clk50, 50e6, with_reset=False, buf="bufgce", ce=(mmcm.locked & (~self.power_down | self.crypto_on)),
gated_replicas={self.cd_clk50_always_on: mmcm.locked}) # 50MHz for ChaCha conditioner, attached to the always-on TRNG
platform.add_platform_command("create_generated_clock -name clk50 [get_pins MMCME2_ADV/CLKOUT4]")
mmcm.create_clkout(self.cd_usb_12, 12e6, with_reset=False, buf="bufgce", ce=mmcm.locked) # 12 MHz for USB; always-on
platform.add_platform_command("create_generated_clock -name usb_12 [get_pins MMCME2_ADV/CLKOUT5]")
# needs to be exactly 100MHz hence margin=0
mmcm.create_clkout(self.cd_sys, sys_clk_freq, margin=0, with_reset=False, buf="bufgce", ce=(~self.power_down & mmcm.locked),
gated_replicas={self.cd_sys_crypto : (mmcm.locked & (~self.power_down | self.crypto_on)), self.cd_sys_always_on : mmcm.locked})
platform.add_platform_command("create_generated_clock -name sys_clk [get_pins MMCME2_ADV/CLKOUT6]")
# mmcm.expose_drp() # the DRP isn't used, so don't expose it
# timing to the "S" pins is not sensitive because we don't care if there is an extra clock pulse relative
# to the gating. Glitch-free operation is guaranteed regardless!
platform.add_platform_command('set_false_path -through [get_pins BUFGCTRL*/S*]')
platform.add_platform_command('set_false_path -through [get_nets vns_rst_meta*]') # fixes for a later version of vivado
self.ignore_locked = Signal()
reset_combo = Signal()
self.comb += reset_combo.eq(self.warm_reset | (~mmcm.locked & ~self.ignore_locked))
# See https://forums.xilinx.com/t5/Other-FPGA-Architecture/MMCM-Behavior-After-Its-PWRDWN-Port-Is-Asserted-and-Then/td-p/792324
# "The DRP functional logic itself does not behave differently for PWRDWN or RST.
# The "registers" programmed previously through the DRP (or any other once) are not affected either
# way because they are configuration cells and are only overwritten if you re-program the part or
# by another DRP operation. Typically, from an application perspective, PWRDWN and RST are identical.
# The difference is obviously that in the PWRDWN case the MMCM completely shuts down for an extended period
# of time even if asserted only briefly. Takes a while to bring back the regulators vs simply reset.
# In addition, since power is turned of, it takes longer to reacquire LOCK vs RST because the VCO starts from scratch."
#self.comb += mmcm.reset.eq(self.power_down)
#self.comb += mmcm.power_down.eq(self.power_down)
self.specials += [
AsyncResetSynchronizer(self.cd_usb_48, reset_combo),
AsyncResetSynchronizer(self.cd_spi, reset_combo),
AsyncResetSynchronizer(self.cd_spinor, reset_combo),
AsyncResetSynchronizer(self.cd_clk200, reset_combo),
AsyncResetSynchronizer(self.cd_clk50, reset_combo),
AsyncResetSynchronizer(self.cd_usb_12, reset_combo),
AsyncResetSynchronizer(self.cd_sys, reset_combo),
AsyncResetSynchronizer(self.cd_clk200_crypto, reset_combo),
AsyncResetSynchronizer(self.cd_sys_crypto, reset_combo),
AsyncResetSynchronizer(self.cd_sys_always_on, reset_combo),
AsyncResetSynchronizer(self.cd_clk50_always_on, reset_combo),
]
# Add an IDELAYCTRL primitive for the SpiOpi block
self.submodules += S7IDELAYCTRL(self.cd_clk200, reset_cycles=32) # 155ns @ 200MHz, min 59.28ns
# WarmBoot -----------------------------------------------------------------------------------------
class WarmBoot(Module, AutoCSR):
def __init__(self, parent, reset_vector=0):
self.soc_reset = CSRStorage(size=8, description="Writing 0xAC to this register will do a full SoC reset, including CRGs and peripherals")
self.addr = CSRStorage(size=32, reset=reset_vector, description="The address written here will be used as the next reset vector")
self.cpu_reset = CSRStorage(size=1, description="Writing anything to this register resets the CPU, and the CPU only; does not affect CRG or peripherals")
self.cpu_hold_reset = CSRStorage(size = 1, description="This bit is wired directly to the CPU's reset line. Thus, when set, the CPU stays in reset until it is cleared. This is intended to be used by external bus masters via USB to hold the CPU in reset.")
self.do_reset = Signal()
# "Reset Key" is 0xac (0b101011xx)
self.comb += self.do_reset.eq((self.soc_reset.storage & 0xfc) == 0xac)
# BtEvents -----------------------------------------------------------------------------------------
class BtEvents(Module, AutoCSR, AutoDoc):
def __init__(self, com, rtc):
self.submodules.ev = EventManager()
self.ev.com_int = EventSourceProcess(edge="rising") # rising edge triggered
self.ev.rtc_int = EventSourceProcess() # falling edge triggered
self.ev.finalize()
com_int = Signal()
rtc_int = Signal()
self.specials += MultiReg(com, com_int)
self.specials += MultiReg(rtc, rtc_int)
self.comb += self.ev.com_int.trigger.eq(com_int)
self.comb += self.ev.rtc_int.trigger.eq(rtc_int)
self.com_pad = Signal()
self.rtc_pad = Signal()
self.comb += [
self.com_pad.eq(com),
self.rtc_pad.eq(rtc),
]
# BtPower ------------------------------------------------------------------------------------------
class BtPower(Module, AutoCSR, AutoDoc):
def __init__(self, pads, revision='pvt', xous=True):
self.intro = ModuleDoc("""BtPower - power control pins
""")
if xous == True:
self.power = CSRStorage(fields=[
CSRField("audio", size=1, description="Write `1` to power on the audio subsystem"),
CSRField("self", size=1, description="Writing `1` forces self power-on (overrides the EC's ability to power me down)", reset=1),
CSRField("ec_snoop", size=1, description="Writing `1` allows the insecure EC to snoop a couple keyboard pads for wakeup key sequence recognition"),
CSRField("state", size=2, description="Current SoC power state. 0x=off or not ready, 10=on and safe to shutdown, 11=on and not safe to shut down, resets to 01 to allow extSRAM access immediately during init", reset=1),
CSRField("reset_ec", size=1, description="Writing a `1` forces EC into reset. Requires write of `0` to release reset."),
CSRField("up5k_on", size=1, description="Writing a `1` pulses the UP5K domain to turn on", pulse=True),
CSRField("boostmode", size=1, description="Writing a `1` causes the USB port to source 5V. To be active only when playing the host role."),
CSRField("selfdestruct", size=1, description="Set this bit to clear BBRAM AES key (if used) and cut power in an annoying-to-reset fashion"),
CSRField("crypto_on", size=1, description="Writing a `1` to this bit turns the clock on to the crypto accelerators. Configured to currently override power to `on` at boot to ease system setup.", reset=1),
CSRField("ignore_locked", size=1, description="Writing a `1` to this bit causes the reset to ignore the PLL lock status"),
CSRField("disable_wfi", size=1, description="Writing a `1` to this bit causes WFI throttling to be disabled")
])
else:
self.power = CSRStorage(8, fields=[
CSRField("audio", size=1, description="Write `1` to power on the audio subsystem"),
CSRField("self", size=1, description="Writing `1` forces self power-on (overrides the EC's ability to power me down)", reset=1),
CSRField("ec_snoop", size=1, description="Writing `1` allows the insecure EC to snoop a couple keyboard pads for wakeup key sequence recognition"),
CSRField("state", size=2, description="Current SoC power state. 0=off or not ready, 1=on, resets to 1 to allow extSRAM access immediately during init", reset=1),
CSRField("noisebias", size=1, description="Writing `1` enables the primary bias supply for the noise generator"),
CSRField("noise", size=2, description="Controls which of two noise channels are active; all combos valid. noisebias must be on first."),
CSRField("reset_ec", size=1, description="Writing a `1` forces EC into reset. Requires write of `0` to release reset."),
CSRField("up5k_on", size=1, description="Writing a `1` pulses the UP5K domain to turn on", pulse=True),
CSRField("boostmode", size=1, description="Writing a `1` causes the USB port to source 5V. To be active only when playing the host role."),
CSRField("selfdestruct", size=1, description="Set this bit to clear BBRAM AES key (if used) and cut power in an annoying-to-reset fashion")
])
self.clk_status = CSRStatus(fields=[
CSRField("crypto_on", size=1, description="The actual crypto clock power on signal, after OR'ing between three separate sources (power, sha512, and engine25519)"),
CSRField("sha_on", size=1, description="Readback of SHA block power setting"),
CSRField("engine_on", size=1, description="Readback of Engine25519 block power setting"),
CSRField("btpower_on", size=1, description="Readback of this block's override-on power setting"),
])
self.wakeup_source = CSRStorage(size=8, fields=[
CSRField("kbd", size=1, reset=1, description="Use the keyboard as wakeup source"),
CSRField("ticktimer", size=1, reset=1, description="Use the ticktimer as wakeup source"),
CSRField("timer0", size=1, reset=1, description="Use timer0 (os timer) as wakeup source"),
CSRField("usb", size=1, reset=1, description="Use USB k-transition as wakeup source"),
CSRField("audio", size=1, reset=1, description="Use audio FIFO empty as wakeup source"),
CSRField("com", size=1, reset=1, description="Use COM hold falling edge plus COM irq rising edge as wakeup source"),
CSRField("rtc", size=1, reset=1, description="Use RTC external interrupt as wakeup source"),
CSRField("console", size=1, reset=1, description="Use the console UART RX line dropping as wakeup source"),
])
self.kbd_wakeup = Signal()
self.ticktimer_wakeup = Signal()
self.timer0_wakeup = Signal()
self.usb_wakeup = Signal()
self.audio_wakeup = Signal()
self.com_wakeup = Signal()
self.rtc_wakeup = Signal()
self.console_wakeup = Signal()
self.specials += MultiReg(self.wakeup_source.fields.kbd, self.kbd_wakeup, "raw_12")
self.specials += MultiReg(self.wakeup_source.fields.ticktimer, self.ticktimer_wakeup, "raw_12")
self.specials += MultiReg(self.wakeup_source.fields.timer0, self.timer0_wakeup, "raw_12")
self.specials += MultiReg(self.wakeup_source.fields.usb, self.usb_wakeup, "raw_12")
self.specials += MultiReg(self.wakeup_source.fields.audio, self.audio_wakeup, "raw_12")
self.specials += MultiReg(self.wakeup_source.fields.com, self.com_wakeup, "raw_12")
self.specials += MultiReg(self.wakeup_source.fields.rtc, self.rtc_wakeup, "raw_12")
self.specials += MultiReg(self.wakeup_source.fields.console, self.console_wakeup, "raw_12")
count_bits=31
count=Signal(count_bits)
active=Signal(count_bits)
self.power_down = Signal()
self.activity_rate = CSRStatus(fields=[
CSRField("counts_awake", size=count_bits, description="number of counts in the last sample interval that we were active")
])
self.sampling_period = CSRStorage(fields=[
CSRField("sample_period", size=count_bits, description="Sampling period for counts awake, in 12MHz cycles"),
CSRField("kill_sampler", size=1, description="When set, permanently disable reporting (in case you don't want the high-resolutionn timing sidechannel)")
])
self.submodules.period_sync = BusSynchronizer(count_bits, "sys", "raw_12")
self.submodules.period_update = BlindTransfer("sys", "raw_12")
self.submodules.activity_update_sys = BlindTransfer("raw_12", "sys")
self.submodules.activity_sync = BusSynchronizer(count_bits, "raw_12", "sys")
sampler_killed=Signal(reset=0)
self.sync += [ # one-way door for kill_sampler
If(self.sampling_period.fields.kill_sampler,
sampler_killed.eq(1)
).Else(
sampler_killed.eq(sampler_killed)
)
]
self.comb += [
If(~sampler_killed,
self.activity_rate.fields.counts_awake.eq(self.activity_sync.o)
).Else(
self.activity_rate.fields.counts_awake.eq(0xdead)
)
]
activity_update = Signal()
self.comb += [
self.period_sync.i.eq(self.sampling_period.fields.sample_period),
self.period_update.i.eq(self.sampling_period.re),
activity_update.eq(self.activity_update_sys.o),
]
self.sync.raw_12 += [
If((count == 0) | self.period_update.o,
count.eq(self.period_sync.o),
).Else(
count.eq(count - 1)
),
If(count == 0,
self.activity_sync.i.eq(active),
self.activity_update_sys.i.eq(1),
active.eq(0),
).Else(
self.activity_update_sys.i.eq(0),
self.activity_sync.i.eq(self.activity_sync.i),
If(~self.power_down,
active.eq(active + 1)
).Else(
active.eq(active)
)
)
]
# future-proofing this: we might want to add e.g. PWM levels and so forth, so give it its own register
self.vibe = CSRStorage(1, description="Vibration motor configuration register", fields=[
CSRField("vibe", size=1, description="Turn on vibration motor"),
])
self.comb += [
pads.audio_on.eq(self.power.fields.audio),
# This signal automatically enables snoop when SoC is powered down
pads.allow_up5k_n.eq(~self.power.fields.ec_snoop),
]
if (revision != 'modnoise') and (xous == False):
self.comb += pads.noise_on.eq(self.power.fields.noise),
if xous == False:
self.comb += pads.noisebias_on.eq(self.power.fields.noisebias)
self.comb += [
pads.vibe_on.eq(self.vibe.fields.vibe),
pads.reset_ec.eq(self.power.fields.reset_ec),
]
self.submodules.ev = EventManager()
self.ev.usb_attach = EventSourcePulse(description="USB attach event")
self.ev.activity_update = EventSourcePulse(description="update available to activity register")
self.ev.finalize()
self.comb += self.ev.activity_update.trigger.eq(activity_update) # this is actually about 9 cycles wide, not strictly a single-cycle pulse, but interrupt latency should be >>9 cycles
usb_attach = Signal()
usb_attach_r = Signal()
self.specials += MultiReg(pads.cc_id, usb_attach)
self.sync += [
usb_attach_r.eq(usb_attach),
self.ev.usb_attach.trigger.eq(~usb_attach & usb_attach_r), # falling edge trigger
]
up5k_on_pulse = 0.20 # pulse up5k for 200ms to turn it on and have it keep itself on
up5k_on_count = Signal(26, reset=int(up5k_on_pulse * 100e6))
self.sync += [
If(up5k_on_count > 0,
pads.up5k_on.eq(1),
).Else(
pads.up5k_on.eq(0)
),
If(self.power.fields.up5k_on,
up5k_on_count.eq(int(up5k_on_pulse * 100e6))
).Elif( up5k_on_count > 0,
up5k_on_count.eq(up5k_on_count - 1),
).Else(
up5k_on_count.eq(0)
)
]
# 4 cycles (40ns) delay between power-down, and CE & ZZ tri-state. This is to ensure that
# the CE & ZZ pins are definitely off before allowing the power to drop, or else we'll have RAM corruption
self.powerdown_override = Signal()
self.l2_idle = Signal()
pdo_delay = Signal(4, reset=0)
self.sync.sys_always_on += [
pdo_delay[3].eq(self.powerdown_override & self.l2_idle),
pdo_delay[2].eq(pdo_delay[3]),
pdo_delay[1].eq(pdo_delay[2]),
pdo_delay[0].eq(pdo_delay[1]),
]
# Hi-Z driver is less glitchy during power transients
self.sys_on_ts = TSTriple(1)
self.specials += self.sys_on_ts.get_tristate(pads.fpga_sys_on)
self.comb += [
self.sys_on_ts.oe.eq(self.power.fields.self & ~pdo_delay[0]),
self.sys_on_ts.o.eq(self.power.fields.self & ~pdo_delay[1]),
]
# Ensure SRAM isolation during reset (CE & ZZ = 1 by pull-ups). Use Hi-Z driver for less glitches.
s0 = Signal()
self.pwr_s0_ts = TSTriple(1)
self.specials += self.pwr_s0_ts.get_tristate(pads.pwr_s0)
self.comb += [
s0.eq(self.power.fields.state[0] & ~ResetSignal()),
self.pwr_s0_ts.oe.eq(s0 & ~pdo_delay[0]),
self.pwr_s0_ts.o.eq(s0 & ~self.powerdown_override)
]
if (revision == 'pvt2'):
# replica version to firewall off u-domain manipulation of SRAM CE lines
self.comb += pads.pwr_s0_replica.eq(s0 & ~self.powerdown_override)
# Hi-Z driver is less glitchy, prevents boost mode power from re-glitching the power on during power off transitions
self.boost_ts = TSTriple(1)
self.specials += self.boost_ts.get_tristate(pads.boostmode)
self.comb += [
self.boost_ts.oe.eq(self.power.fields.boostmode),
self.boost_ts.o.eq(self.power.fields.boostmode)
]
# Hi-Z driver is less glitchy and less likely to trigger the self destruct mechanism on power glitches
self.sd_ts = TSTriple(1)
self.specials += self.sd_ts.get_tristate(pads.selfdestruct)
self.comb += [
self.sd_ts.oe.eq(self.power.fields.selfdestruct),
self.sd_ts.o.eq(self.power.fields.selfdestruct),
]
# BtGpio -------------------------------------------------------------------------------------------
class BtGpio(Module, AutoDoc, AutoCSR):
def __init__(self, pads, usb_type="debug"):
self.intro = ModuleDoc("""BtGpio - GPIO interface for betrusted""")
gpio_in = Signal(pads.nbits)
gpio_out = Signal(pads.nbits)
gpio_oe = Signal(pads.nbits)
for g in range(0, pads.nbits):
gpio_ts = TSTriple(1)
self.specials += gpio_ts.get_tristate(pads[g])
self.comb += [
gpio_ts.oe.eq(gpio_oe[g]),
gpio_ts.o.eq(gpio_out[g]),
gpio_in[g].eq(gpio_ts.i),
]
self.output = CSRStorage(pads.nbits, name="output", description="Values to appear on GPIO when respective `drive` bit is asserted")
self.input = CSRStatus(pads.nbits, name="input", description="Value measured on the respective GPIO pin")
self.drive = CSRStorage(pads.nbits, name="drive", description="When a bit is set to `1`, the respective pad drives its value out")
self.intena = CSRStatus(pads.nbits, name="intena", description="Enable interrupts when a respective bit is set")
self.intpol = CSRStatus(pads.nbits, name="intpol", description="When a bit is `1`, falling-edges cause interrupts. Otherwise, rising edges cause interrupts.")
self.uartsel = CSRStorage(2, name="uartsel", description="Used to select which UART is routed to physical pins, 00 = kernel debug, 01 = console, others reserved based on build")
self.debug = CSRStorage(description="Various debugging configurations", fields = [
CSRField(name="wfi", size=1,
description="Whet set, patches CRG powerdown into GPIO0 instead of the usual data line. Must configure as output for the value to appear on the pin"),
CSRField(name="wakeup", size=1,
description="Whet set, patches wakeup signal into GPIO1 instead of the usual data line. Must configure as output for the value to appear on the pin"),
])
if usb_type != 'spinal':
self.usbdisable = CSRStorage(1, name="usbdisable", description="When set to ``1``, USB debug is limited by remapping all wishbone request addresses to 0x8000_0000")
self.debug_wakeup = Signal()
self.debug_wfi = Signal()
self.specials += MultiReg(gpio_in, self.input.status)
self.comb += [
gpio_out[2:].eq(self.output.storage[2:]),
gpio_oe.eq(self.drive.storage),
If(self.debug.fields.wfi,
gpio_out[0].eq(self.debug_wfi)
).Else(
gpio_out[0].eq(self.output.storage[0])
),
If(self.debug.fields.wakeup,
gpio_out[1].eq(self.debug_wakeup)
).Else(
gpio_out[1].eq(self.output.storage[1])
)
]
self.submodules.ev = EventManager()
for i in range(0, pads.nbits):
setattr(self.ev, "gpioint" + str(i), EventSourcePulse() ) # pulse => rising edge
self.ev.finalize()
for i in range(0, pads.nbits):
# pull from input.status because it's after the MultiReg synchronizer
self.comb += getattr(self.ev, "gpioint" + str(i)).trigger.eq(self.input.status[i] ^ self.intpol.status[i])
# note that if you change the polarity on the interrupt it could trigger an interrupt
# BtSeed -------------------------------------------------------------------------------------------
class BtSeed(Module, AutoDoc, AutoCSR):
def __init__(self, reproduceable=False):
self.intro = ModuleDoc("""Place and route seed. Set to a fixed number for reproduceable builds.
Use a random number or your own number if you are paranoid about hardware implants that target
fixed locations within the FPGA.""")
if reproduceable:
seed_reset = int(4) # chosen by fair dice roll. guaranteed to be random.
else:
rng = SystemRandom()
seed_reset = rng.getrandbits(64)
self.seed = CSRStatus(64, name="seed", description="Seed used for the build", reset=seed_reset)
# Keyboard Injector -------------------------------------------------------------------------------
class KeyInject(Module, AutoDoc, AutoCSR):
def __init__(self):
self.intro = ModuleDoc("""Used by developers to pass a key from the UART to the keyboard block.
This is necessary because memory space partitioning plus Rust dependency architecture does
not allow the logger block to access either the keyboard memory space or any of the facilities
that would normally allow a message to be passed to the keyboard interface. In particular,
every package depends upon the logger block; in order for the logger block to talk to the
keyboard interface, it would need to add the keyboard to its dependencies, which in turn
depends upon the logger, which creates a circular, unresolvable dependency in Rust. This block
allows us to break this dependency by creating a separate memory page for a CSR that we can
map into the logger's memory space, which is capable of raising an interrupt in the Keyboard's
memory space.""")
# this is used to permanently disable this backdoor, should a user desire to
disable = Signal(reset=0)
self.uart_char = CSRStorage(8, fields = [
CSRField("char", size=8, description="character value to inject. Automatically raises an interrupt upon write. There is no interlock or FIFO buffering on this, so you can lose characters if you inject too fast."),
])
self.disable = CSRStorage(1, fields = [
CSRField("disable", size=1, description="writing a 1 permanently disables the block, until the next cold boot", reset=0),
])
self.char = Signal(8)
self.stb = Signal()
self.sync += [
If(self.disable.fields.disable,
disable.eq(1)
).Else(
disable.eq(disable)
),
If(disable == 0,
self.stb.eq(self.uart_char.re),