-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalt_fpga_manager.c
1101 lines (912 loc) · 33.6 KB
/
alt_fpga_manager.c
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
/******************************************************************************
*
* Copyright 2015 Altera Corporation. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the copyright holder 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.
*
******************************************************************************/
/*
* $Id: //acds/rel/19.1std/embedded/ip/hps/altera_hps/hwlib/src/hwmgr/soc_a10/alt_fpga_manager.c#1 $
*/
#include <stdio.h>
#include <inttypes.h>
#include "alt_fpga_manager.h"
#include "alt_printf.h"
#include "alt_fpgamgr.h"
#include "alt_fpgamgrdata.h"
#include "hps.h"
#include "socal.h"
#if DEBUG_ALT_FPGA_MANAGER
#define dprintf printf
#else
#define dprintf null_printf
#endif
/*
* This is used in the FPGA reconfiguration streaming interface. Because FPGA
* images are commonly stored on disk, the chunk size is that of the disk size.
* We cannot choose too large a chunk size because the stack size is fairly
* small.
*/
#define DISK_SECTOR_SIZE 8192
#define ISTREAM_CHUNK_SIZE DISK_SECTOR_SIZE
/*
* Structure that holds the internal global state.
*/
static struct
{
/* HPS CPU is in control of FPGA block. */
bool cpu_in_control;
/* Reset assert active. */
bool reset_asserted;
} g_fpgaState;
/*
* FPGA Data Type identifier enum
*/
typedef enum FPGA_DATA_TYPE_e
{
FPGA_DATA_FULL = 1,
FPGA_DATA_LIST = 2,
FPGA_DATA_STREAM = 3
} FPGA_DATA_TYPE_t;
/*
* FPGA Data, for Full Buffer, Buffer List, or IStream configuration
*/
typedef struct FPGA_DATA_s
{
FPGA_DATA_TYPE_t type;
union
{
/* For FPGA_DATA_FULL */
struct
{
const void * buffer;
size_t length;
} full;
/* For FPGA_DATA_LIST */
struct
{
const void ** buffer;
const size_t * length;
size_t count;
} list;
/* For FPGA_DATA_STREAM */
struct
{
alt_fpga_istream_t callback;
void * context;
} stream;
} mode;
#if ALT_FPGA_ENABLE_DMA_SUPPORT
bool use_dma;
ALT_DMA_CHANNEL_t dma_channel;
#endif
} FPGA_DATA_t;
/*****/
/*
* Helper function that polls for a certain FPGA state to become active.
*/
static ALT_STATUS_CODE wait_for_fpga_status(ALT_FPGA_STATUS_t state, bool to_be_set, size_t tmo)
{
if (to_be_set)
{
while ((alt_read_word(ALT_FPGAMGR_IMGCFG_STAT_ADDR) & state) == 0)
{
--tmo;
if (tmo == 0)
{
return ALT_E_TMO;
}
}
}
else
{
while ((alt_read_word(ALT_FPGAMGR_IMGCFG_STAT_ADDR) & state) != 0)
{
--tmo;
if (tmo == 0)
{
return ALT_E_TMO;
}
}
}
return ALT_E_SUCCESS;
}
/*****/
ALT_STATUS_CODE alt_fpga_init(void)
{
g_fpgaState.cpu_in_control = false;
g_fpgaState.reset_asserted = false;
return ALT_E_SUCCESS;
}
ALT_STATUS_CODE alt_fpga_uninit(void)
{
if (g_fpgaState.cpu_in_control)
{
alt_fpga_control_disable();
}
return ALT_E_SUCCESS;
}
ALT_STATUS_CODE alt_fpga_control_enable(ALT_FPGA_CFG_MODE_t mode)
{
if (!g_fpgaState.cpu_in_control)
{
uint32_t mask = 0;
/*
* Step 1:
* Verify MSEL is 000 or 001.
*/
dprintf("FPGA[ctrl:en]: === Step 1 ===\n");
switch (alt_read_word(ALT_FPGAMGR_IMGCFG_STAT_ADDR)
& (ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL0_SET_MSK | ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL1_SET_MSK | ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL2_SET_MSK))
{
case 0:
case ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL0_SET_MSK:
break;
default:
dprintf("FPGA[1]: MSEL not set to 000 or 001.\n");
return ALT_E_ERROR;
}
dprintf("FPGA[ctrl:en]: === Step 2 ===\n");
switch (mode)
{
case ALT_FPGA_CFG_MODE_PP16_FAST_NOAES_NODC:
mask = ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_E_PPX16)
| ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_E_X1);
break;
case ALT_FPGA_CFG_MODE_PP16_FAST_AES_NODC:
mask = ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_E_PPX16)
| ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_E_X2);
break;
case ALT_FPGA_CFG_MODE_PP16_FAST_NOAES_DC:
mask = ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_E_PPX16)
| ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_E_X4);
break;
case ALT_FPGA_CFG_MODE_PP16_FAST_AES_DC:
mask = ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_E_PPX16)
| ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_E_X4);
break;
case ALT_FPGA_CFG_MODE_PP32_FAST_NOAES_NODC:
mask = ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_E_PPX32)
| ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_E_X1);
break;
case ALT_FPGA_CFG_MODE_PP32_FAST_AES_NODC:
mask = ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_E_PPX32)
| ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_E_X4);
break;
case ALT_FPGA_CFG_MODE_PP32_FAST_NOAES_DC:
mask = ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_E_PPX32)
| ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_E_X8);
break;
case ALT_FPGA_CFG_MODE_PP32_FAST_AES_DC:
mask = ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_E_PPX32)
| ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET(ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_E_X8);
break;
}
alt_replbits_word(ALT_FPGAMGR_IMGCFG_CTL_02_ADDR,
ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET_MSK | ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK,
mask);
/*
* Step 3:
* Verify no other devices are interfering with programming.
// Verify: F2S_NCONFIG_PIN = 1
// Verify: F2S_NSTATUS_PIN = 1
*/
dprintf("FPGA[ctrl:en]: === Step 3 ===\n");
if ((alt_read_word(ALT_FPGAMGR_IMGCFG_STAT_ADDR) & (ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTAT_PIN_SET_MSK | ALT_FPGAMGR_IMGCFG_STAT_F2S_NCFG_PIN_SET_MSK))
!= (ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTAT_PIN_SET_MSK | ALT_FPGAMGR_IMGCFG_STAT_F2S_NCFG_PIN_SET_MSK))
{
dprintf("FPGA[3]: Error: F2S_NCONFIG_PIN != 1 or F2S_NSTATUS_PIN != 1.\n");
return ALT_E_ERROR;
}
/*
* Step 4:
* Deassert signal drives before taking over those overrides.
// Write: ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE = 1
// Write: ALT_FPGAMGR_IMGCFG_CTL_01_S2F_PR_REQUEST = 0
// Write: ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA = 0
// Write: ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTL = 0
// Write: ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCFG = 1
// Write: ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NSTAT_OE = 0
// Write: ALT_FPGAMGR_IMGCFG_CTL_00_S2F_CONDONE_OE = 0
*/
dprintf("FPGA[ctrl:en]: === Step 4 ===\n");
alt_replbits_word(ALT_FPGAMGR_IMGCFG_CTL_01_ADDR,
ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK | ALT_FPGAMGR_IMGCFG_CTL_01_S2F_PR_REQUEST_SET_MSK,
ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
alt_replbits_word(ALT_FPGAMGR_IMGCFG_CTL_02_ADDR,
ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK | ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTL_SET_MSK,
0);
alt_replbits_word(ALT_FPGAMGR_IMGCFG_CTL_00_ADDR,
ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCFG_SET_MSK | ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NSTAT_OE_SET_MSK | ALT_FPGAMGR_IMGCFG_CTL_00_S2F_CONDONE_OE_SET_MSK,
ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCFG_SET_MSK);
/*
* Step 5:
* Enable overrides for DATA / DCLK / NCONFIG.
// Write: ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NEN_CFG = 0
// Write: ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NEN_NCFG = 0
*
* Disable overrides for NSTATUS / CONF_DONE.
// Write: ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NEN_NSTAT = 1
// Write: ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NEN_CONDONE = 1
*/
dprintf("FPGA[ctrl:en]: === Step 5 ===\n");
alt_clrbits_word(ALT_FPGAMGR_IMGCFG_CTL_01_ADDR, ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NEN_CFG_SET_MSK);
alt_clrbits_word(ALT_FPGAMGR_IMGCFG_CTL_00_ADDR, ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NEN_NCFG_SET_MSK);
alt_setbits_word(ALT_FPGAMGR_IMGCFG_CTL_00_ADDR,
ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NEN_NSTAT_SET_MSK | ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NEN_CONDONE_SET_MSK);
/*
* Step 6:
* Drive chip select.
// Write: ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE = 0
*/
dprintf("FPGA[ctrl:en]: === Step 6 ===\n");
alt_clrbits_word(ALT_FPGAMGR_IMGCFG_CTL_01_ADDR, ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
/*
* Step 7:
* Repeat step 3, just in case.
*/
dprintf("FPGA[ctrl:en]: === Step 7 ===\n");
if ((alt_read_word(ALT_FPGAMGR_IMGCFG_STAT_ADDR) & (ALT_FPGA_STATUS_F2S_NSTATUS_PIN | ALT_FPGA_STATUS_F2S_NCONFIG_PIN))
!= (ALT_FPGA_STATUS_F2S_NSTATUS_PIN | ALT_FPGA_STATUS_F2S_NCONFIG_PIN))
{
dprintf("FPGA[7]: Error: F2S_NCONFIG_PIN != 1 or F2S_NSTATUS_PIN != 1.\n");
return ALT_E_ERROR;
}
/*
* Mark state as in control.
*/
g_fpgaState.cpu_in_control = true;
}
return ALT_E_SUCCESS;
}
ALT_STATUS_CODE alt_fpga_control_disable(void)
{
if (g_fpgaState.cpu_in_control)
{
if (g_fpgaState.reset_asserted)
{
/*
* Borrowed from Step 8b. Disable reset of FPGA.
*/
dprintf("FPGA[ctrl:dis]: === Step 8b ===\n");
alt_setbits_word(ALT_FPGAMGR_IMGCFG_CTL_00_ADDR, ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCFG_SET_MSK);
g_fpgaState.reset_asserted = false;
}
/*
* Step 15:
* Disable chip select.
// Write: ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE = 1
*/
dprintf("FPGA[ctrl:dis]: === Step 15 ===\n");
alt_setbits_word(ALT_FPGAMGR_IMGCFG_CTL_01_ADDR, ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
/*
* Step 16:
* Disable overrides for DATA / DCLK / NCONFIG
// Write: ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NEN_CFG = 1
// Write: ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NEN_NCFG = 1
*/
dprintf("FPGA[ctrl:dis]: === Step 16 ===\n");
alt_setbits_word(ALT_FPGAMGR_IMGCFG_CTL_01_ADDR, ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NEN_CFG_SET_MSK);
alt_setbits_word(ALT_FPGAMGR_IMGCFG_CTL_00_ADDR, ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NEN_NCFG_SET_MSK);
/*
* Mark state as not in control.
*/
g_fpgaState.cpu_in_control = false;
}
return ALT_E_SUCCESS;
}
bool alt_fpga_control_is_enabled(void)
{
return g_fpgaState.cpu_in_control;
}
ALT_STATUS_CODE alt_fpga_reset_assert(void)
{
if (!alt_fpga_control_is_enabled())
{
/* HPS not in control. */
return ALT_E_FPGA_NO_SOC_CTRL;
}
else if (g_fpgaState.reset_asserted)
{
/* Reset already asserted. */
return ALT_E_SUCCESS;
}
else
{
g_fpgaState.reset_asserted = true;
/*
* Borrowed from FPGA configuration, step 8a.
*/
dprintf("FPGA[rst:en]: === Step 8a ===\n");
alt_clrbits_word(ALT_FPGAMGR_IMGCFG_CTL_00_ADDR, ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCFG_SET_MSK);
return wait_for_fpga_status(ALT_FPGA_STATUS_F2S_NSTATUS_PIN, false, 1000);
}
}
ALT_STATUS_CODE alt_fpga_reset_deassert(void)
{
if (!alt_fpga_control_is_enabled())
{
/* HPS not in control. */
return ALT_E_FPGA_NO_SOC_CTRL;
}
else if (!g_fpgaState.reset_asserted)
{
/* Reset already unasserted. */
return ALT_E_SUCCESS;
}
else
{
g_fpgaState.reset_asserted = false;
/*
* Borrowed from FPGA configuration, step 8b.
*/
dprintf("FPGA[rst:dis]: === Step 8b ===\n");
alt_setbits_word(ALT_FPGAMGR_IMGCFG_CTL_00_ADDR, ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCFG_SET_MSK);
return wait_for_fpga_status(ALT_FPGA_STATUS_F2S_NSTATUS_PIN, true, 1000);
}
}
uint32_t alt_fpga_status_get(void)
{
return alt_read_word(ALT_FPGAMGR_IMGCFG_STAT_ADDR);
}
/*
* Helper function which handles writing data to the AXI bus.
*/
static ALT_STATUS_CODE alt_fpga_internal_writeaxi(const void * bufferv, uint32_t length
#if ALT_FPGA_ENABLE_DMA_SUPPORT
,
bool use_dma, ALT_DMA_CHANNEL_t dma_channel
#endif
)
{
const char * buffer = bufferv;
ALT_STATUS_CODE status = ALT_E_SUCCESS;
const uint32_t * buffer_end_32 = (const uint32_t *) (buffer + (length & ~0x3));
#if ALT_FPGA_ENABLE_DMA_SUPPORT
if (use_dma)
{
/* A10 DMA support for FPGA as a peripheral is not implemented. */
dprintf("FPGA[AXI]: DMA support not implemented.\n");
status = ALT_E_ERROR;
}
else
#endif
{
const uint32_t * buffer_32 = (const uint32_t *) buffer;
/* Write out as many complete 32-bit chunks. */
while (length >= sizeof(uint32_t))
{
alt_write_word(ALT_FPGAMGRDATA_ADDR, *buffer_32);
++buffer_32;
length -= sizeof(uint32_t);
}
}
/* Write out remaining non 32-bit aligned chunk. */
if ((status == ALT_E_SUCCESS) && (length & 0x3))
{
dprintf("FPGA[AXI]: PIO unaligned data ...\n");
switch (length & 0x3)
{
case 3:
alt_write_word(ALT_FPGAMGRDATA_ADDR, *buffer_end_32 & 0x00ffffff);
break;
case 2:
alt_write_word(ALT_FPGAMGRDATA_ADDR, *buffer_end_32 & 0x0000ffff);
break;
case 1:
alt_write_word(ALT_FPGAMGRDATA_ADDR, *buffer_end_32 & 0x000000ff);
break;
default:
/* This will never happen. */
break;
}
}
return status;
}
static ALT_STATUS_CODE alt_fpga_internal_configure_idata(FPGA_DATA_t * fpga_data)
{
ALT_STATUS_CODE status = ALT_E_SUCCESS;
/*
* Step 10:
* Send POF / SOF data
* Program in ALT_FPGAMGRDATA_ADDR
* Optionally read and confirm LT_FPGAMGR_IMGCFG_STAT_F2S_NSTAT_PIN = 1, else restart from step 1.
*/
if (status == ALT_E_SUCCESS)
{
/*
* This is the largest configuration image possible for the largest Arria 10
* SoC device with some generous padding added.
* Experimental values:
* - Compressed : 15 MiB.
* - Uncompressed : 31 MiB.
*/
uint32_t data_limit = 36 * 1024 * 1024;
dprintf("FPGA[cfg]: === Step 10 ===\n");
/* Print this here or else istream will cause this to be printed many times. */
dprintf("FPGA[AXI]: PIO aligned data ...\n");
if (fpga_data->type == FPGA_DATA_FULL)
{
if (fpga_data->mode.full.length > data_limit)
{
status = ALT_E_FPGA_CFG;
}
else
{
status = alt_fpga_internal_writeaxi(fpga_data->mode.full.buffer, fpga_data->mode.full.length
#if ALT_FPGA_ENABLE_DMA_SUPPORT
,
fpga_data->use_dma, fpga_data->dma_channel
#endif
);
}
}
else if (fpga_data->type == FPGA_DATA_LIST)
{
/* Flag if processing has encountered an uint32_t unaligned segment.
If so, that must be the last segment. */
bool unaligned_segment = false;
size_t i;
for (i = 0; i < fpga_data->mode.list.count; ++i)
{
const void * buffer = fpga_data->mode.list.buffer[i];
size_t length = fpga_data->mode.list.length[i];
if (length > data_limit)
{
dprintf("FPGA[10][list]: Data limit breached; infinite loop or invalid image likely.\n");
status = ALT_E_FPGA_CFG;
}
else if (unaligned_segment)
{
dprintf("FPGA[10][list]: Previous segment unaligned; RBF segment corruption likely.\n");
status = ALT_E_FPGA_CFG;
}
else
{
status = alt_fpga_internal_writeaxi(buffer, length
#if ALT_FPGA_ENABLE_DMA_SUPPORT
,
fpga_data->use_dma, fpga_data->dma_channel
#endif
);
data_limit -= length;
if (length & (sizeof(uint32_t) - 1))
{
unaligned_segment = true;
}
}
if (status != ALT_E_SUCCESS)
{
break;
}
}
}
else if (fpga_data->type == FPGA_DATA_STREAM)
{
/* Flag if processing has encountered an uint32_t unaligned segment.
If so, that must be the last segment. */
bool unaligned_segment = false;
uint32_t buffer[ISTREAM_CHUNK_SIZE / sizeof(uint32_t)];
int32_t cb_status = 0; /* Callback status */
do
{
cb_status = fpga_data->mode.stream.callback(buffer, sizeof(buffer), fpga_data->mode.stream.context);
if (cb_status > sizeof(buffer))
{
/* Callback data overflows buffer space. */
status = ALT_E_FPGA_CFG_STM;
}
else if (cb_status < 0)
{
/* A problem occurred when streaming data from the source. */
status = ALT_E_FPGA_CFG_STM;
}
else if (cb_status == 0)
{
/* End of IStream data. */
break;
}
else if (cb_status > data_limit)
{
/* Limit hit for the largest permissible data stream. */
status = ALT_E_FPGA_CFG_STM;
}
else if (unaligned_segment)
{
dprintf("FPGA[10][stream]: Previous segment unaligned; previous segment invalid.\n");
status = ALT_E_FPGA_CFG;
}
else
{
/* Copy in configuration data. */
status = alt_fpga_internal_writeaxi(buffer, cb_status
#if ALT_FPGA_ENABLE_DMA_SUPPORT
,
fpga_data->use_dma, fpga_data->dma_channel
#endif
);
data_limit -= cb_status;
if (cb_status & (sizeof(uint32_t) - 1))
{
unaligned_segment = true;
}
}
if (status != ALT_E_SUCCESS)
{
break;
}
} while (cb_status > 0);
}
else
{
dprintf("FPGA[10]: Invalid programming request type.\n");
status = ALT_E_ERROR;
}
}
/*
* Step 11:
* Wait for ConfigCompletion
// Wait: ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN = 1 or ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTAT_PIN = 0.
// If ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTAT_PIN = 0: goto step 1.
// If ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN = 1: configuration passed. yay!
*/
if (status == ALT_E_SUCCESS)
{
int i = 10000;
dprintf("FPGA[cfg]: === Step 11 === (starting i = %d).\n", i);
do
{
uint32_t imgcfg = alt_read_word(ALT_FPGAMGR_IMGCFG_STAT_ADDR);
if (!(imgcfg & ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTAT_PIN_SET_MSK))
{
dprintf("FPGA[11]: Error: F2S_NSTAT_PIN = 0.\n");
status = ALT_E_ERROR;
break;
}
else if (imgcfg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK)
{
/* yay! */
break;
}
} while (--i);
if (i == 0)
{
dprintf("FPGA[11]: Timeout waiting for config completion result.\n");
status = ALT_E_TMO;
}
else
{
dprintf("FPGA[11]: i = %d.\n", i);
}
}
/*
* Step 12:
* Write dclkcnt = 0xf.
// Write: ALT_FPGAMGR_DCLKCNT_ADDR = 0xf.
// Wait: ALT_FPGAMGR_DCLKSTAT_ADDR = 1.
*/
if (status == ALT_E_SUCCESS)
{
int i = 10000;
dprintf("FPGA[cfg]: === Step 12 === (starting i = %d).\n", i);
/* Clear the DCLKSTAT.dcntdone before starting. */
if (alt_read_word(ALT_FPGAMGR_DCLKSTAT_ADDR))
{
alt_write_word(ALT_FPGAMGR_DCLKSTAT_ADDR, ALT_FPGAMGR_DCLKSTAT_DCNTDONE_SET_MSK);
}
alt_write_word(ALT_FPGAMGR_DCLKCNT_ADDR, 0xf);
/* Now poll until DCLKSTAT.dcntdone = 1*/
while ((alt_read_word(ALT_FPGAMGR_DCLKSTAT_ADDR) & ALT_FPGAMGR_DCLKSTAT_DCNTDONE_SET_MSK) == 0)
{
if (!--i)
{
dprintf("FPGA[12]: Timeout waiting for DCLKSTAT.DCNTDONE.\n");
status = ALT_E_TMO;
break;
}
}
if (i != 0)
{
/* Cleanup DCLKSTAT.dcntdone status. */
alt_write_word(ALT_FPGAMGR_DCLKSTAT_ADDR, ALT_FPGAMGR_DCLKSTAT_DCNTDONE_SET_MSK);
dprintf("FPGA[12]: i = %d.\n", i);
}
}
/*
* Step 13:
* Wait for initialization sequence to complete.
// Wait: ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMOD = 1
*/
if (status == ALT_E_SUCCESS)
{
dprintf("FPGA[cfg]: === Step 13 ===\n");
status = wait_for_fpga_status(ALT_FPGA_STATUS_F2S_USERMODE, true, 10000);
if (status != ALT_E_SUCCESS)
{
dprintf("FPGA[13]: Timeout waiting for F2S_USERMOD = 1.\n");
}
}
return status;
}
static ALT_STATUS_CODE alt_fpga_internal_configure(FPGA_DATA_t * fpga_data)
{
ALT_STATUS_CODE status = ALT_E_SUCCESS;
/* HPS should be on control before configuration. */
if (g_fpgaState.cpu_in_control == false)
{
dprintf("FPGA[cfg]: precondition not met: CPU not in control.\n");
return ALT_E_FPGA_NO_SOC_CTRL;
}
/* FPGA should not be in reset before attempting configuration. */
if (g_fpgaState.reset_asserted)
{
dprintf("FPGA[cfg]: precondition not met: FPGA reset asserted.\n");
return ALT_E_ERROR;
}
/*
* Step 1:
* Verify MSEL is 000 or 001.
*/
dprintf("FPGA[cfg]: === Step 1 === (skipped due to precondition)\n");
/*
* Step 2:
* Determine the CFGWIDTH and CDRATIO from the programming mode and write to HW.
*/
dprintf("FPGA[cfg]: === Step 2 === (skipped due to precondition)\n");
/*
* Step 3:
* Verify no other devices are interfering with programming.
*/
dprintf("FPGA[cfg]: === Step 3 === (skipped due to precondition)\n");
/*
* Step 4:
* Deassert signal drives before taking over those overrides.
*/
dprintf("FPGA[cfg]: === Step 4 === (skipped due to precondition)\n");
/*
* Step 5:
* Enable overrides for DATA / DCLK / NCONFIG.
* Disable overrides for NSTATUS / CONF_DONE.
*/
dprintf("FPGA[cfg]: === Step 5 === (skipped due to precondition)\n");
/*
* Step 6:
* Drive chip select.
*/
dprintf("FPGA[cfg]: === Step 6 === (skipped due to precondition)\n");
/*
* Step 7:
* Repeat step 3, just in case.
*/
dprintf("FPGA[cfg]: === Step 7 === (skipped due to precondition)\n");
/*
* Step 8:
* Reset the configuration.
// Write: ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCFG = 0
// Wait: ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTAT_PIN = 0
*
// Write: ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCFG = 1
// Wait: ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTAT_PIN = 1
*
// Verify: ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN = 0
// Verify: ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_OE = 1
*/
dprintf("FPGA[cfg]: === Step 8 ===\n");
alt_clrbits_word(ALT_FPGAMGR_IMGCFG_CTL_00_ADDR, ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCFG_SET_MSK);
status = wait_for_fpga_status(ALT_FPGA_STATUS_F2S_NSTATUS_PIN, false, 100000);
/* Handle any error conditions after reset request has been withdrawn.
This is to avoid bailing out with reset assert requested. */
alt_setbits_word(ALT_FPGAMGR_IMGCFG_CTL_00_ADDR, ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCFG_SET_MSK);
/* This is the error handler from above, step 8a. */
if (status != ALT_E_SUCCESS)
{
dprintf("FPGA[8]: Error: Timeout waiting for F2S_NSTAT_PIN = 0.\n");
return ALT_E_FPGA_CFG;
}
status = wait_for_fpga_status(ALT_FPGA_STATUS_F2S_NSTATUS_PIN, true, 100000);
if (status != ALT_E_SUCCESS)
{
dprintf("FPGA[8]: Error: Timeout waiting for F2S_NSTAT_PIN = 1.\n");
return ALT_E_FPGA_CFG;
}
if ((alt_read_word(ALT_FPGAMGR_IMGCFG_STAT_ADDR) & (ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK | ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_OE_SET_MSK))
!= ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_OE_SET_MSK)
{
dprintf("FPGA[8]: Error: F2S_CONDONE_PIN != 0 or F2S_CONDONE_OE != 1.\n");
return ALT_E_ERROR;
}
/*
* Step 9:
* Enable DCLK and DATA path.
// Write: ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA = 1
// Write: ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTL = 1
*/
dprintf("FPGA[cfg]: === Step 9 ===\n");
alt_setbits_word(ALT_FPGAMGR_IMGCFG_CTL_02_ADDR,
ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK | ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTL_SET_MSK);
/*
* Helper function for Steps 10 - 13.
*/
status = alt_fpga_internal_configure_idata(fpga_data);
/*
* Step 14:
* Stop DATA and DCLK path.
// Write: ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA = 0
// Write: ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTL = 0
*/
dprintf("FPGA[cfg]: === Step 14 ===\n");
alt_clrbits_word(ALT_FPGAMGR_IMGCFG_CTL_02_ADDR,
ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK | ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTL_SET_MSK);
/*
* Step 15:
* Disable chip select.
// Write: ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE = 1
*/
dprintf("FPGA[cfg]: === Step 15 === (skipped due to post condition)\n");
/*
* Step 16:
* Disable overrides for DATA / DCLK / NCONFIG
// Write: ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NEN_CFG = 1
// Write: ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NEN_NCFG = 1
*/
dprintf("FPGA[cfg]: === Step 16 === (skipped due to post condition)\n");
/*
* Step 17:
* Final check.
// Verify: ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMOD = 1
// Verify: ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTAT_PIN = 1
// Verify: ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN = 1
*/
if (status == ALT_E_SUCCESS)
{
dprintf("FPGA[cfg]: === Step 17 ===\n");
if ((alt_read_word(ALT_FPGAMGR_IMGCFG_STAT_ADDR) & (ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMOD_SET_MSK | ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTAT_PIN_SET_MSK | ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK))
!= (ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMOD_SET_MSK | ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTAT_PIN_SET_MSK | ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK))
{
dprintf("FPGA[17]: Error: F2S_USERMOD != 1 or F2S_NSTAT_PIN != 1 or F2S_CONDONE_PIN != 1.\n");
return ALT_E_ERROR;
}
}
return status;
}
ALT_STATUS_CODE alt_fpga_configure(const void * buf,
size_t len)
{
FPGA_DATA_t fpga_data;
fpga_data.type = FPGA_DATA_FULL;
fpga_data.mode.full.buffer = buf;
fpga_data.mode.full.length = len;
#if ALT_FPGA_ENABLE_DMA_SUPPORT
fpga_data.use_dma = false;
#endif
return alt_fpga_internal_configure(&fpga_data);
}
ALT_STATUS_CODE alt_fpga_configure_list(const void ** buf_list,
const size_t * len_list,
size_t list_count)
{
FPGA_DATA_t fpga_data;
fpga_data.type = FPGA_DATA_LIST;
fpga_data.mode.list.buffer = buf_list;
fpga_data.mode.list.length = len_list;
fpga_data.mode.list.count = list_count;
#if ALT_FPGA_ENABLE_DMA_SUPPORT
fpga_data.use_dma = false;
#endif
return alt_fpga_internal_configure(&fpga_data);
}
#if ALT_FPGA_ENABLE_DMA_SUPPORT
ALT_STATUS_CODE alt_fpga_configure_dma(const void * buf,
size_t len,
ALT_DMA_CHANNEL_t dma_channel)
{
FPGA_DATA_t fpga_data;
fpga_data.type = FPGA_DATA_FULL;
fpga_data.mode.full.buffer = buf;
fpga_data.mode.full.length = len;
fpga_data.use_dma = true;
fpga_data.dma_channel = dma_channel;