forked from 90geek/RAIDOOBMODULE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSamedebug_old.c
2537 lines (2186 loc) · 73.9 KB
/
Samedebug_old.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
//***************************************************************************
//** **
//** (C)Copyright 1993-2011 Supermicro Computer, Inc. **
//** **
//** **
//***************************************************************************
//***************************************************************************
//
// File History
//
// Rev. 1.00
// Bug Fix: Initial Version.
// Reason:
// Auditor: Alan Chen
// Date: 08/13/2012
//
//***************************************************************************
//#include "token.h"
//#include <AmiLib.h>
#if DYNAMIC_PCIEXBASE_SUPPORT
#include <DynamicPciBase.h>
#endif
extern UINT64 ReadMsr (UINT32 Msr);
extern VOID WriteMsr(UINT32 Msr, UINT64 Value);
//;SameDiskDebugSampleCode Mon 06-14-2010 For support SPI Hyper-Terminal debug tool.>> [a]+
//#if A7_Command_SUPPORT
#ifndef SB_RCBA
#define SB_RCBA 0xfed1c000 // Default RCBA
#endif
#ifndef SPI_BAR_OFFSET
#define SPI_BAR_OFFSET 0x3800 // SPI BAR offset (ICH10, PCH)
#endif
#ifndef SB_SPI_BASE_ADDRESS
#define SB_SPI_BASE_ADDRESS 0xFE010000//(SB_RCBA + SPI_BAR_OFFSET) // SPI BASE Address (ICH10, PCH)
#endif
#ifndef DEFAULT_PCI_BUS_NUMBER_PCH
#define DEFAULT_PCI_BUS_NUMBER_PCH 0
#endif
#ifndef PCI_DEVICE_NUMBER_PCH_SPI
#define PCI_DEVICE_NUMBER_PCH_SPI 31
#endif
#ifndef PCI_FUNCTION_NUMBER_PCH_SPI
#define PCI_FUNCTION_NUMBER_PCH_SPI 5
#endif
#ifndef R_PCH_SPI_BAR0
#define R_PCH_SPI_BAR0 0x10
#endif
#ifndef PCI_COMMAND_OFFSET
#define PCI_COMMAND_OFFSET 0x04
#endif
#ifndef Command_97_SUPPORT
// #define Command_97_SUPPORT TRUE // Default set support 97 command.
#define Command_97_SUPPORT FALSE // Default set do not support 97 command.
#endif
#ifndef U8
typedef unsigned char U8;
typedef unsigned short U16;
typedef unsigned long U32;
#endif
//
// Completion code related definitions
//
#define COMPLETION_CODE_SUCCESS 0x00
#define COMPLETION_CODE_DEVICE_SPECIFIC_START 0x01
#define COMPLETION_CODE_DEVICE_SPECIFIC_END 0x7E
#define COMPLETION_CODE_COMMAND_SPECIFIC_START 0x80
#define COMPLETION_CODE_COMMAND_SPECIFIC_END 0xBE
#define ERROR_COMPLETION_CODE(a) !( (a == COMPLETION_CODE_SUCCESS) || \
( (a >= COMPLETION_CODE_DEVICE_SPECIFIC_START) &&\
(a <= COMPLETION_CODE_DEVICE_SPECIFIC_END) )||\
( (a >= COMPLETION_CODE_COMMAND_SPECIFIC_START) &&\
(a <= COMPLETION_CODE_COMMAND_SPECIFIC_END) ) )
typedef enum
{
KcsIdleState,
KcsReadState,
KcsWriteState,
KcsErrorState
}KCS_STATE;
UINT8 SoftErrorCount;
UINT8 TempData[100];
extern VOID MemCpy(VOID* pDestination, VOID* pSource, UINTN Count);
typedef char *A7Va_list;
#define A7PUTC(_c,_CharCount) StrWhole[_CharCount]=_c; _CharCount++;
#define A7INTSIZEOF(n) ( (sizeof(n) + sizeof(U32) - 1) & ~(sizeof(U32) - 1) )
#define A7Va_start(ap,v) ( ap = (A7Va_list)&v + A7INTSIZEOF(v) )
#define A7Isdigit(_c) ( ((_c) >= '0') && ((_c) <= '9'))
#define A7Va_arg(ap,t) ( *(t *)((ap += A7INTSIZEOF(t)) - A7INTSIZEOF(t)) )
#define A7GETMEM(addr,dataType) (*((dataType *)(addr)))
#define A7SETMEM(addr,dtyp,dat) (*((dtyp *)(addr))=dat)
#define A7SPIBAR(offset) (SB_SPI_BASE_ADDRESS + offset)
#define A7GETMEM8(addr) A7GETMEM(addr, U8)
#define A7GETMEM16(addr) A7GETMEM(addr, U16)
#define A7GETMEM32(addr) A7GETMEM(addr, U32)
#define A7SETMEM8(addr,dat) A7SETMEM(addr, U8, dat)
#define A7SETMEM32(addr,dat) A7SETMEM(addr, U32, dat)
#define A7SETSPI8(offs, dat) A7SETMEM8(A7SPIBAR(offs),dat);
#define A7SETSPI32(offs, dat) A7SETMEM32(A7SPIBAR(offs),dat);
#define SMC_PCIEX_BASE_ADDRESS 0xE0000000
U8 A7SpiGet8(U8 offs) {
return(A7GETMEM8(A7SPIBAR(offs)));
}
VOID JA7SETSPI8(U8 offs, U8 dat) {
A7SETMEM8(A7SPIBAR(offs),dat);
}
//FUNCTION: Wait until SPI Cycle NOT In Progress (SCIP).
VOID A7WaitUntiSpiReady() {
while( (A7SpiGet8(0x04) & 0x20) == 0x20 ); // Wait until "Cycle in Progress" bit is 0
}
//FUNCTION: Wait until SPI Cycle Done.
VOID A7WaitUntilSpiCycleDone() {
while( (A7SpiGet8(0x04) & 0x01) != 0x01 ); // Wait until "Flash Cycle Done" bit is 1
A7SETSPI8(0x04, 0x01); // Clear 'Cycle Done Status'.
}
//FUNCTION: Send a SPI-UART command
//Command : A7-Command-SO-SO // Send a UART command and get two byte data (SO-SO)
//INPUT : Command (SI-SI-SI)
//OUTPUT : SO-SO
U16 A7Command(U32 cmd32) {
U16 SoSo;
A7WaitUntiSpiReady(); // Wait device not busy
A7SETSPI8(0xA6, 0x02); // OP Type : Read
A7SETSPI32(0x08, cmd32); // FADDR (decode SPI FADDR as three byte command)
A7SETSPI8(0x07, 0x01); // Flash Data Byte count = 2
A7SETMEM8 (A7SPIBAR(0x04), 0x01); // Clear 'Flash Cycle Done'.
A7SETSPI8(0x06, 0x0B); // 5A command( Read SFDP ) , Go!
A7WaitUntilSpiCycleDone(); // Wait command completely
SoSo = A7GETMEM16(A7SPIBAR(0x10));
return( SoSo );
}
//FUNCTION: Check 'TETO Ready'
//Command : A7-00-00-00-00-x1 : TETO Busy
// A7-00-00-00-00-x0 : TETO Ready
//RETURN : 1 = Busy
// 0 = Ready
int A7ChkTetoReady() {
//alan return( A7Command(0) & 0x0100 ); // 0x0100 : Busy, 0x0000 : Ready
return 0;//alan
}
//FUNCTION: PUT TETO Data
//Command : A7-00-81-31-00-00 : Put TETO Data
int A7PutTetoData(U8 dat8) {
A7WaitUntiSpiReady(); // Wait device not busy
A7SETSPI32(0x08, 0x008100 | dat8); // FADDR (decode SPI FADDR as data)
A7SETSPI8(0x06, 0x0B); // 5A command( Read SFDP ) , Go!
A7WaitUntilSpiCycleDone(); // Wait command completely
return 0;
}
//FUNCTION: Sent TETO
//Command : A7-00-80-01-00-00 : Sent TETO
int A7SentTeto() {
A7WaitUntiSpiReady(); // Wait device not busy
A7SETSPI32(0x08, 0x008001); // FADDR (decode SPI FADDR as data)
A7SETSPI8(0x06, 0x0B); // 5A command( Read SFDP ) , Go!
A7WaitUntilSpiCycleDone(); // Wait command completely
return 0;
}
//FUNCTION: Get device ID
//Command : A7-00-80-00-ID-ID ; Command 'Get UART ID16'
//Comment : 25AA = ZC25U64/128 + STM32
// 2525 = ZC25U64/128 only
// 10AA = SPI2UART cable
// 0000/FFFF = Real Flash or Not supported version
// AA=ARM
// 25=ROMTER
// 10=Cable
//RETURN : Device ID of A7
U16 A7GetDeviceId() {
//alan return( A7Command(0x008000) ); // send command 00-80-00 to get Device ID of A7.
return( A7Command(0x008001) ); // send command 00-80-00 to get Device ID of A7. //alan
}
//FUNCTION: Check Samedisk device existed or not
//RETURN : TRUE = Existed.
// FALSE = Not existed.
BOOLEAN A7DeviceExisted() {
U8 deviceId8;
//alan deviceId8 = (U8) (A7GetDeviceId() >> 8);
deviceId8 = (U8) A7GetDeviceId();//alan
return( (deviceId8 == 0xAA) || (deviceId8 == 0x25) ); // AA=ARM, 25=ROMTER that supports A7 command
}
//FUNCTION: Check Samedisk Emulator device existed or not
//RETURN : TRUE = Existed.
// FALSE = Not existed.
BOOLEAN EmuDeviceExisted() {
U8 deviceId8;
//alan deviceId8 = (U8) (A7GetDeviceId() >> 8);
deviceId8 = (U8) A7GetDeviceId();//alan
return( deviceId8 == 0x25); // 25=ROMTER that supports A7 command
}
//FUNCTION: Send a character to show on Hyper-Terminal.
VOID A7PutChar(UINT8 CharData) {
while( (A7ChkTetoReady()) != 0); // Wait until TETO Ready
A7PutTetoData(CharData); // PUT TETO Data
A7SentTeto(); // Sent TETO
}
//FUNCTION: Print a string with specified length.
int A7SendStrLen(UINT8 *String, UINT8 len) {
UINT8 i;
for(i=0; i<len-1; i++) { // Skip End-Of-String (ASCIIZ) character.
if (*String=='\n') A7PutChar('\r'); // replace "\n" with "\r\n"
A7PutChar(*String);
String++;
}
return 0;
}
//FUNCTION: Send a string.
int A7SendString(char *String) {
UINT8 *Buffer;
UINT8 len=0;
Buffer = String;
while (*Buffer) {
len++;
if (*Buffer=='\n') {
A7SendStrLen(Buffer-len+1, len+1);
len = 0;
}
Buffer++;
}
A7SendStrLen(Buffer-len, len+1);
return 0;
}
//FUNCTION: transfer string.
void A7UintnToStr(U32 Value, char *Str, U32 Width, U32 Flags, U32 Base) {
U32 Negative;
U32 Int;
char *Ptr;
char Prefix;
char c;
U32 i;
const char Hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
Ptr = Str;
if ( (Value > 0x7fffffffU) && (Flags & 0x20) ) {
Int = ~Value + 1; /* -Value */
Negative = 1;
} else {
Int = Value;
Negative = 0;
}
i = 0;
do { /* generate digits in reverse order */
i++;
*Ptr++ = Hex[Int % Base];
if ( Flags & 0x40 ) {
if ( Base == 16 ) {
if ( i % 4 == 0 ) {
*Ptr++ = ',';
}
} else if ( Base == 10 ) {
if ( i % 3 == 0 ) {
*Ptr++ = ',';
}
}
}
} while ((Int /= Base) > 0);
if ( *(Ptr-1) == ',') {
Ptr--;
}
if (Negative) {
*Ptr++ = '-';
} else if ( Flags & 0x02 ) {
*Ptr++ = '+';
}
if ( Flags & 0x08 ) {
Prefix = '0';
} else if ( !(Flags & 0x01)) {
Prefix = ' ';
} else {
Prefix = 0x00;
}
if (Prefix != 0x00) {
for (i = (int)(Ptr - Str); i < Width; i++) {
//*Ptr++ = Prefix;
if(Prefix == '0')
*Ptr++ = '0';
if(Prefix == ' ')
*Ptr++ = ' ';
}
}
*Ptr = '\0';
/* reverse string */
while (Str < --Ptr) {
c = *Str;
*Str++ = *Ptr;
*Ptr = c;
}
}
//FUNCTION: transfer string to number.
U32 A7StrToNumber(char **String) {
U32 Sum;
char *Str;
Str = *String;
if (*Str == '0') {
Str++;
}
Sum = 0;
while (A7Isdigit(*Str)) {
Sum = Sum*10 + (*Str++ - '0');
}
*String = Str;
return (Sum);
}
//FUNCTION: printf command.
int A7Vprintf(const char *Format, A7Va_list Marker) {
char *p;
U32 Width;
U32 Flags;
char Str[160], StrWhole[255];
char *String;
U32 CharCount;
for (p = (char *)Format, CharCount = 0; *p; p++) {
if ( *p != '%' ) {
A7PUTC(*p, CharCount);
} else {
p++;
// Check for flags
Flags = 0;
if ( *p == '-') {
Flags |= 0x01;
} else if ( *p == '+' ) {
Flags |= 0x02;
} else if ( *p == ' ' ) {
Flags |= 0x04;
}
if (Flags != 0) {
p++;
}
// Check for width
if ( A7Isdigit(*p) ) {
if ( *p == '0' ) {
Flags |= 0x08;
}
Width = A7StrToNumber(&p);
} else if ( *p == '*' ) {
Width = A7Va_arg( Marker, int );
p++;
} else {
Width = 0;
}
if ( *p == ',' ) {
Flags |= 0x40;
p++;
}
// get type
String = Str;
switch (*p) {
case 'd':
case 'i':
// always print as UINTN will need extra code to print different widths
A7UintnToStr ((U32)A7Va_arg( Marker, U32 *),Str , Width, Flags | 0x20, 10);
break;
case 'u':
// always print as UINTN will need extra code to print different widths
A7UintnToStr ((U32)A7Va_arg( Marker, U32 *),Str , Width, Flags, 10);
break;
case 'x':
case 'X':
// always print as UINTN will need extra code to print different widths
A7UintnToStr ((U32)A7Va_arg( Marker, U32 *),Str , Width, Flags, 16);
break;
case 'c':
A7PUTC (A7Va_arg (Marker, char), CharCount);
Str[0] = '\0';
break;
case 's':
String = (char *)A7Va_arg (Marker, char *);
break;
case 'p':
A7UintnToStr ((U32)A7Va_arg( Marker, U32 *),Str , Width, Flags, 16);
break;
}
while (*String != '\0') {
A7PUTC (*String++, CharCount);
}
}
}
StrWhole[CharCount] = 0; // End of ASCIIZ
A7SendString(StrWhole);
return CharCount;
}
UINTN
A7_MmPciBase (
IN UINT32 Bus,
IN UINT32 Device,
IN UINT32 Function
)
{
return ((UINTN) (SMC_PCIEX_BASE_ADDRESS) + (UINTN) (Bus << 20) + (UINTN) (Device << 15) + (UINTN) (Function << 12));
}
VOID A7_InitSpiBar(void)
{
U8 data;
U32 data32;
UINTN PchSpiBase;
PchSpiBase = A7_MmPciBase (
DEFAULT_PCI_BUS_NUMBER_PCH,
PCI_DEVICE_NUMBER_PCH_SPI,
PCI_FUNCTION_NUMBER_PCH_SPI
);
data32 = A7GETMEM32(PchSpiBase + R_PCH_SPI_BAR0);
if( data32 != SB_SPI_BASE_ADDRESS )
{
A7SETMEM32(PchSpiBase + R_PCH_SPI_BAR0, SB_SPI_BASE_ADDRESS);
data = A7GETMEM8(PchSpiBase + PCI_COMMAND_OFFSET);
data = data | 0x02; //EFI_PCI_COMMAND_MEMORY_SPACE
A7SETMEM8(PchSpiBase + PCI_COMMAND_OFFSET, data);
}
}
//FUNCTION: Samedisk A7 printf() - send a string to Hyper-Terminal by A7 device that supports A7 command.
//USAGE : a7printf( <Format String>, <Argument 1>, [Argument 2, 3, ...] )
//EXAMPLE : Show i value with a string --> a7printf("i = %x\n", i);
VOID a7printf(const char *Format, ...) {
A7_InitSpiBar();
A7Command(0x000003);
A7Command(0x000025);
A7Command(0x000002);
if(A7DeviceExisted()) { // if A7 device existed
A7Va_list Marker;
A7Va_start(Marker, Format);
A7Vprintf (Format, Marker);
}
}
//FUNCTION: Print debug message
//INPUT : IN EFI_PEI_SERVICES *PeiServices
// : IN CHAR8 *String
//OUTPUT : None
VOID a7printf_message(IN VOID *PeiServices, IN CHAR8 *String)
{
a7printf("%s", String);
}
// Pause and Get key {
//============================================================================================ a7getch() / a7pause()
//FUNCTION: Get TERMINAL STAT
//Command : A7-00-00-00-SO-SO : TERMINAL STAT
//RETURN : Terminal state (SO-SO)
U16 A7GetState() {
return(A7Command(0)); // SI-SI-SI : 00-00-00
}
//FUNCTION: TEKI Request
//Command : A7-00-00-00-00-1x : TERMINAL STAT (TEKI Request)
//RETURN : 0 = No data (key) in
// 1000h = Has data (key) in
int A7GetTeki() {
return(A7GetState() & 0x1000);
}
//FUNCTION: Send TEKI Acknowledge
//Command : A7-00-80-10-00-00 : Send TEKI Acknowledge
VOID A7TekiAck() {
A7Command(0x008010); // send command 00-80-10 (TEKI Acknowledge)
}
//FUNCTION: Get TERM Data
//Command : A7-00-01-00-00-Data : Get TERM Data
//RETURN : Received data byte (-SO)
U8 A7GetTermData8() {
U16 dat16;
dat16 = A7Command(0x000100); // Get Terminal Data
A7TekiAck(); // send command 00-80-10 (TEKI Acknowledge)
return( (U8) (dat16 >> 8) );
}
//FUNCTION: C-like getch() - Wait and get a key from SUART.
//INPUT : None.
//RETURN : Pressed key code.
U8 a7getch() {
while(!A7GetTeki()); // wait until any key pressed.
return( A7GetTermData8() ); // return pressed key code.
}
//FUNCTION: Pause system and wait until user pressed [Space] key on PuTTY Hyper-Terminal.
//INPUT : None.
//OUTPUT : Pressed key code.
U8 a7pause() {
U8 ch, ch2;
while(1) {
a7printf("\n* a7pause: Press [Space] key to continue... ");
ch=a7getch(); // wait and get a key
ch2=ch;
// special characters
if(ch >= 8 && ch <= 13) // if [TAB] and [Enter]
ch2 = '.'; // show '.'
else if(ch == 27) // if [ESC] key
ch2 = '['; // show '['
// check and show key information.
if(ch != 0x20) { // if not [Space] key
a7printf("[%c] ", ch2);
a7printf("Keycode %02Xh, not [Space] *", ch);
} else {
a7printf("Continue! *\n");
break;
} // if()
} // while()
return(ch);
}
// Pause and Get key }
//FUNCTION: Send 86 command for port 80h
//Command : SPI86Command
//INPUT : Port 80 data
//
int SPI86Command(U8 cmd8) {
if(A7DeviceExisted()) { // if A7 device existed
A7WaitUntiSpiReady(); // Wait device not busy
A7SETSPI8(0x9E, 0x86); // OPMENU index 6 Code (86 command)
A7SETSPI8(0x97, 0xA4); // OP Type : Set Index 6 & 7 for Read
A7SETSPI8(0x08, 0x00);
A7SETSPI8(0x09, cmd8);
A7SETSPI8(0x0A, 0x00);
A7SETMEM8 (A7SPIBAR(0x90), 0x04); // Clear 'Cycle Done Status'.
A7SETSPI8(0x91, 0x62); // Go! Selece OPMenu index 6.
A7WaitUntilSpiCycleDone(); // Wait command completely
A7SETSPI8(0x9E, 0x86); // OPMENU index 6 Code (86 command)
A7SETSPI8(0x97, 0xA4); // OP Type : Set Index 6 & 7 for Read
A7SETSPI8(0x08, 0x00);
A7SETSPI8(0x09, cmd8);
A7SETSPI8(0x0A, 0x01);
A7SETMEM8 (A7SPIBAR(0x90), 0x04); // Clear 'Cycle Done Status'.
A7SETSPI8(0x91, 0x62); // Go! Selece OPMenu index 6.
A7WaitUntilSpiCycleDone(); // Wait command completely
}
return(0 );
}
//FUNCTION: Send 86 command for port 80h
//Command : SPI86Command2
//INPUT : Port 80 data
int SPI86Command2(U8 cmd8) {
if(A7DeviceExisted()) { // if A7 device existed
A7WaitUntiSpiReady(); // Wait device not busy
A7SETSPI8(0x9E, 0x86); // OPMENU index 6 Code (86 command)
A7SETSPI8(0x97, 0xA4); // OP Type : Set Index 6 & 7 for Read
A7SETSPI8(0x08, 0x00);
A7SETSPI8(0x09, cmd8);
A7SETSPI8(0x0A, 0x02);
A7SETMEM8 (A7SPIBAR(0x90), 0x04); // Clear 'Cycle Done Status'.
A7SETSPI8(0x91, 0x62); // Go! Selece OPMenu index 6.
A7WaitUntilSpiCycleDone(); // Wait command completely
A7SETMEM8 (A7SPIBAR(0x90), 0x04); // Clear 'Cycle Done Status'.
A7SETSPI8(0x91, 0x62); // Go! Selece OPMenu index 6.
A7WaitUntilSpiCycleDone(); // Wait command completely
A7SETMEM8 (A7SPIBAR(0x90), 0x04); // Clear 'Cycle Done Status'.
A7SETSPI8(0x91, 0x62); // Go! Selece OPMenu index 6.
A7WaitUntilSpiCycleDone(); // Wait command completely
A7SETMEM8 (A7SPIBAR(0x90), 0x04); // Clear 'Cycle Done Status'.
A7SETSPI8(0x91, 0x62); // Go! Selece OPMenu index 6.
A7WaitUntilSpiCycleDone(); // Wait command completely
}
return(0 );
}
//FUNCTION: Send 86 and A7printf command for port 80h
//Command : OUTSPI80
//INPUT : Port 80 data
VOID OUTSPI80(UINT8 g) {
if(A7DeviceExisted()) { // if A7 device existed
SPI86Command(g); // Send 86 command with port 80 data.
a7printf("Postcode %02Xh**\n", g); // Send A7printf command with port 80 data.
// a7printf("*\n");
}
}
//FUNCTION: Send "5EC0" on 86 command or "Power on" string to A7printf command for clear samdebug logscreen.
//Command : SPIPowerONInit
//INPUT : None.
VOID SPIPowerONInit() {
if(A7DeviceExisted()) { // if A7 device existed
A7WaitUntiSpiReady(); // Wait device not busy
A7SETSPI8(0x9E, 0x86); // Set OPMENU index 6 Code (86 command)
A7SETSPI8(0x97, 0xA4); // OP Type : Set Index 6 & 7 for Read
A7SETSPI8(0x08, 0x00);
A7SETSPI8(0x09, 0x5E);
A7SETSPI8(0x0A, 0x00);
A7SETMEM8 (A7SPIBAR(0x90), 0x04); // Clear 'Cycle Done Status'.
A7SETSPI8(0x91, 0x62); // Go! Selece OPMenu index 6.
A7WaitUntilSpiCycleDone(); // Wait command completely
A7SETSPI8(0x9E, 0x86); // Set OPMENU index 6 Code (86 command)
A7SETSPI8(0x97, 0xA4); // OP Type : Set Index 6 & 7 for Read
A7SETSPI8(0x08, 0x00);
A7SETSPI8(0x09, 0xC0);
A7SETSPI8(0x0A, 0x01);
A7SETMEM8 (A7SPIBAR(0x90), 0x04); // Clear 'Cycle Done Status'.
A7SETSPI8(0x91, 0x62); // Go! Selece OPMenu index 6.
A7WaitUntilSpiCycleDone(); // Wait command completely
a7printf("*Power on*\n");
}
}
//============================================================================================ 97 Command
//FUNCTION: Read data from Emulator.
//Command : CM-SI-SI-SI-SO-SO-...
// 97-Offset -Data
//INPUT : Offset 0x00-0x7F --> Point to offset 0x300-0x3FF of Emulator DEBUG.BIN.
//OUTPUT : HSPIBAR(0x11) --- DEBUG.BIN address (0x300 + Offset)
void hugetbios(unsigned char Offset, int len) {
A7WaitUntiSpiReady(); // Wait device not busy
A7SETSPI8(0x90,0x8C); // Clear status.
A7SETSPI8(0x9F,0x97); // Set OPMENU index 7 Code
A7SETSPI8(0x97, 0xA4); // OP Type : Set Index 6 & 7 for Read
A7SETSPI32(0x08, Offset); // FADDR (decode SPI FADDR as three byte command)
A7SETSPI8(0x92, (0x40+len)) // Byte count (one dummy at HSPIBAR(0x10))
A7SETSPI8(0x91, 0x72); // Go! Selece OPMenu index 7.
A7WaitUntilSpiCycleDone(); // Wait command completely
}
//============================================================================================ A7 Command for get and set 97 buffer.
//FUNCTION: Read data from Emulator.
//Command : A7-03-xx-Addr(8)-xx-DD-...
// A7-Offset -len
//INPUT : Offset 0x00-0x7F --> Point to offset 0x300-0x3FF of Emulator DEBUG.BIN.
//OUTPUT : HSPIBAR(0x11) --- DEBUG.BIN address (0x300 + Offset)
void A7hugetbios(unsigned char Offset, int len) {
A7WaitUntiSpiReady(); // Wait device not busy
A7SETSPI8(0x04, 0x01); // Clear 'Flash Cycle Done'.
A7SETSPI8(0xA6, 0x02); // OP Type : Read
A7SETSPI8(0x08, Offset); // The buffer offset of 0x97 buffer want to get.
A7SETSPI8(0x09, 0x00); // dummy data
A7SETSPI8(0x0A, 0x03); // 0x03 is command for get data to 0x97 buffer.
A7SETSPI8(0x07, len); // Byte count (one dummy at HSPIBAR(0x10))
A7SETSPI8(0x04, 0x01); // Clear 'Flash Cycle Done'.
A7SETSPI8(0x06, 0x0B); // 5A command( Read SFDP ) , Go!
A7WaitUntilSpiCycleDone(); // Wait command completely
}
//FUNCTION: Set data from Emulator.
//Command : A7-02-Addr(8)-Datain(8)-...
// A7-Offset -Data
//INPUT : Offset 0x00-0x7F --> Point to offset 0x300-0x3FF of Emulator DEBUG.BIN.
//OUTPUT : HSPIBAR(0x11) --- DEBUG.BIN address (0x300 + Offset)
void A7husetbios(unsigned char Offset, int Data) {
A7WaitUntiSpiReady(); // Wait device not busy
A7SETSPI8(0x04, 0x01); // Clear 'Flash Cycle Done'.
A7SETSPI8(0xA6, 0x02); // OP Type : Read
A7SETSPI8(0x08, Data); // The data for set to 0x97 buffer.
A7SETSPI8(0x09, Offset); // The buffer offset of 0x97 buffer want to set.
A7SETSPI8(0x0A, 0x02); // 0x02 is command for set data to 0x97 buffer.
A7SETSPI8(0x07, 0x01); // Flash Data Byte count = 2
A7SETSPI8(0x04, 0x01); // Clear 'Flash Cycle Done'.
A7SETSPI8(0x06, 0x0B); // 5A command( Read SFDP ) , Go!
A7WaitUntilSpiCycleDone(); // Wait command completely
}
//FUNCTION: Read data from Emulator.
//Command : CM-SI-SI-SI-SO
// 97-Offset -Data
//INPUT : Offset 0x00-0x7F --> Point to offset 0x300-0x3FF of Emulator DEBUG.BIN.
//OUTPUT : Data byte of DEBUG.BIN address (0x300 + Offset)
//Read 8bit data from Emulator.
unsigned char hugetbios8(unsigned char Offset) { // Get 8bit data form offset 0x300 ~ 0x3FF Emulator DEBUG.BIN.
unsigned char data8;
A7hugetbios(Offset, 1); // read 1 byte
//alan data8=A7GETMEM8(A7SPIBAR(0x11)); // read the first byte after dummy character.
data8=A7GETMEM8(A7SPIBAR(0x10)); // read the first byte after dummy character. //alan
return( data8 ); // Return one data byte
}
//FUNCTION: Read 16bit data from Emulator.
U16 hugetbios16(unsigned char Offset) { // Get 16bit data form offset 0x300 ~ 0x3FF Emulator DEBUG.BIN.
U16 data16;
A7hugetbios(Offset, 2); // read 2 byte
//alan data16=A7GETMEM16(A7SPIBAR(0x11)); // read the first byte after dummy character.
data16=A7GETMEM16(A7SPIBAR(0x10)); // read the first byte after dummy character. //alan
return( data16 ); // Return one data byte
}
//FUNCTION: Read 32bit data from Emulator.
U32 hugetbios32(unsigned char Offset) { // Get 32bit data form offset 0x300 ~ 0x3FF Emulator DEBUG.BIN.
U32 data32;
U16 data16;
A7hugetbios(Offset+2, 2); // read 2 byte
//alan data16=A7GETMEM16(A7SPIBAR(0x11)); // read the first byte after dummy character.
data16=A7GETMEM16(A7SPIBAR(0x10)); // read the first byte after dummy character. //alan
data32= (U32) (data16<<16); //
A7hugetbios(Offset, 2); // read 2 byte
//alan data16=A7GETMEM16(A7SPIBAR(0x11)); // read the first byte after dummy character.
data16=A7GETMEM16(A7SPIBAR(0x10)); // read the first byte after dummy character. //alan
data32=data32|data16; //
return( data32 ); // Return one data byte
}
//============================================================================================
//----------------------------------------------------------------------------- PCI & PCIe Address>>
U32 SDPcieAddr(U8 bus, U8 device, U8 function, U16 reg) {
#if DYNAMIC_PCIEXBASE_SUPPORT
return((U32)(GetPciBaseAddr() | (bus << 20) | (device << 15) | (function << 12) | reg));
#else
return((U32)(SMC_PCIEX_BASE_ADDRESS | (bus << 20) | (device << 15) | (function << 12) | reg));
#endif
}
#if DYNAMIC_PCIEXBASE_SUPPORT
#define SDSetPcieAddr(bus, device, function, reg, Data) (*(volatile UINT8*)(UINTN)(GetPciBaseAddr() | (bus << 20) | (device << 15) | (function << 12) | reg)= Data)
#else
#define SDSetPcieAddr(bus, device, function, reg, Data) (*(volatile UINT8*)(UINTN)(SMC_PCIEX_BASE_ADDRESS | (bus << 20) | (device << 15) | (function << 12) | reg)= Data)
#endif
/*
U32 SDReadPciDword(U8 bus, U8 device, U8 function, U16 reg)
{
// U32 data;
//
// regAddr = (U8 *) SDPcieAddr(bus, device, function, reg );
// data = *(volatile U32 *)regAddr;
// data = *(volatile U32 *)SDPcieAddr(bus, device, function, reg );
// (Under construction...)
return(*(volatile U32 *)SDPcieAddr(bus, device, function, reg ));
}
U16 SDReadPciWord(U8 bus, U8 device, U8 function, U16 reg)
{
return(*(volatile U16 *)SDPcieAddr(bus, device, function, reg ));
}
U8 SDReadPciByte(U8 bus, U8 device, U8 function, U16 reg)
{
return(*(volatile U8 *)SDPcieAddr(bus, device, function, reg ));
}
//FUNCTION: Read 256 bytes PCI/PCIe device registers.
VOID SDPcie(U8 Bus, U8 Device, U8 Function) {
U32 Address;
U16 i, j;
a7printf("==================================================\n");
a7printf("BUS : %04Xh / Device : %04Xh / Function : %04Xh\n", Bus, Device, Function);
a7printf("VID : %04Xh / DID : %04Xh\n", SDReadPciWord(Bus, Device, Function,0), SDReadPciWord(Bus, Device, Function,2));
// Read 256 bytes PCI registers
a7printf(" 00 01 02 03 04 05 06 07 08-09 0A 0B 0C 0D 0E 0F\n");
for(i=0; i<0x10; i++) {
a7printf("%02X ", i*0x10);
for(j=0; j<0x10; j++) {
if (j<0x0F) {
a7printf("%02X-", SDReadPciByte( (U8) Bus, (U8) Device, (U8) Function, (U16) (i*0x10+j) ));
} else {
a7printf("%02X", SDReadPciByte( (U8) Bus, (U8) Device, (U8) Function, (U16) (i*0x10+j) ));
}
} // for()
a7printf("\n");
} //for()
a7printf("==================================================\n");
#if DYNAMIC_PCIEXBASE_SUPPORT
Address=(GetPciBaseAddr() | (Bus << 20) | (Device << 15) | (Function << 12));
#else
Address=(SMC_PCIEX_BASE_ADDRESS | (Bus << 20) | (Device << 15) | (Function << 12));
#endif
a7printf("%08Xh\n",Address);
} // SDPcie()
//FUNCTION: Set PCI Address data.
VOID SDSetPCI() {
U8 Bus;
U8 Dev;
U8 Fun;
U8 Reg;
U8 Data;
Bus=hugetbios8(0xC1); // PCI BUS number.
Dev=hugetbios8(0xC2); // PCI Device number.
Fun=hugetbios8(0xC3); // PCI Function number.
Reg=hugetbios8(0xC4); // PCI Register number.
a7printf("PCI BUS : %02Xh / Device : %02Xh / Function : %02Xh/ Register: %2Xh\n", Bus,Dev,Fun,Reg);
Data=hugetbios8(0xC5); // Data
a7printf("Regsiter data set to %02Xh**\n", Data);
SDSetPcieAddr(Bus, Dev, Fun, Reg, Data);
SDPcie(Bus,Dev,Fun);
A7husetbios(0xC0,0x01);
} //SDSetPCI()
//FUNCTION: Get PCI List.
VOID SDGetPCIList() {
U8 Bus;
U8 Dev;
U8 Fun;
for(Bus=0; Bus<=0x3F; Bus++) {
for(Dev=0; Dev<=0x1F; Dev++) {
for(Fun=0; Fun<=0x7; Fun++) {
if (((SDReadPciByte(Bus, Dev, Fun, 0x00))!=0xFF)&&((SDReadPciByte(Bus, Dev, Fun, 0x00))!=0x00)) {
SDPcie(Bus,Dev,Fun);
}
} // for()
} // for()
} //for()
} //SDGetPCIList()
//----------------------------------------------------------------------------- PCI & PCIe address.<<
//----------------------------------------------------------------------------- Memory Base Address.>>
//FUNCTION: Read 256 bytes Memroy area registers.
VOID SDMem(UINT32 Address) {
U16 i, j;
a7printf("==================================================\n");
a7printf("Get Memory data : %08Xh\n", Address);
// Read 256 bytes Memory area registers
a7printf(" 00 01 02 03 04 05 06 07 08-09 0A 0B 0C 0D 0E 0F\n");
for(i=0; i<0x10; i++) {
a7printf("%02X ", i*0x10);
for(j=0; j<0x10; j++) {
if (j<0x0F) {
a7printf("%02X-", A7GETMEM8((U32)(Address +(i*0x10+j))));
} else {
a7printf("%02X", A7GETMEM8((U32)(Address +(i*0x10+j))));
}
} // for()
a7printf("\n");
} //for()
a7printf("==================================================\n");
} // SDMem()
//FUNCTION: Set Memory Base address data.
VOID SDSetMem() {
U32 Address;
U8 Data;
Address=hugetbios32(0xD1); // Memory_BaseAddress
a7printf("Memory Baseaddress %08Xh\n", Address);
Data=hugetbios8(0xD5); // Data value for set
a7printf("Set Data value to %02Xh**\n", Data);
A7SETMEM8(Address,Data);
SDMem(Address & 0xFFFFFF00);
A7husetbios(0xD0,0x01);
} //SDSetMem()
//----------------------------------------------------------------------------- Memory Base Address.<<
//----------------------------------------------------------------------------- IO Base Address.>>
//FUNCTION: Show 256 bytes data of IO Base Address.
VOID SDIO(UINT16 Address) {
int i, j;
a7printf("==================================================\n");
a7printf("Get IO Base address data : %04Xh\n", Address);
a7printf(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
for (i=0; i<0x10; i++) {
a7printf("%02X ", i * 0x10);
for (j=0; j<0x10; j++) {
if (j<0x0F) {
a7printf("%02X-", IoRead8((U16)(Address +(i*0x10+j))) );
} else {
a7printf("%02X ", IoRead8((U16)(Address +(i*0x10+j))) );
}
} // for()
a7printf("\n");
} // for()
a7printf("==================================================\n");
} //SDIO()
//FUNCTION: Set IO Base address data.
VOID SDSetIO() {
U16 Address;
U8 Data;
Address=hugetbios16(0x91); // IO_BaseAddress
a7printf("IOBaseaddress %04Xh**\n", Address);
Data=hugetbios8(0x93); // Data
a7printf("Set Data value to %02Xh**\n", Data);
IoWrite8(Address,Data);
SDIO(Address & 0xFF00);
A7husetbios(0x90,0x01);
} //SDSetIO()
//----------------------------------------------------------------------------- IO Base address.<<
//----------------------------------------------------------------------------- Index IO. >>
//FUNCTION: Get [IndexIO] = Index IO address.
unsigned char SDGetIndex(unsigned char IndexAddr, unsigned char IndexData) {
IoWrite8(IndexAddr, IndexData); // Index
return IoRead8(IndexAddr+1); // Data
}
//FUNCTION: Set [IndexIO] = Data by Index IO address.
int SDSetIndex(unsigned char IndexAddr, unsigned char IndexData, unsigned char DataSet) {
IoWrite8(IndexAddr, IndexData); // Index
IoWrite8(0xEB, 0x55); // IO delay
IoWrite8(IndexAddr+1, DataSet); // Write Data
return 0;
}
//FUNCTION: Show 256 bytes data of Index IO
VOID SDGetIndexIO(U8 IndexIO) {
int i, j;
a7printf("==================================================\n");
if (IndexIO==0x70 || IndexIO==0x72){
a7printf("Get CMOS %02Xh/%02Xh Data\n", IndexIO, IndexIO +1);
} else {
a7printf("Get Inedx IO %02Xh/%02Xh Data\n", IndexIO, IndexIO +1);
}
a7printf(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
for (i=0; i<0x10; i++) {
a7printf("%02X ", i * 0x10);
for (j=0; j<0x10; j++) {
if (j<0x0F) {
a7printf("%02X-", SDGetIndex(IndexIO,(0x10 * i + j)) );
} else {
a7printf("%02X ", SDGetIndex(IndexIO,(0x10 * i + j)) );
}
} // for()
a7printf("\n");
} // for()
a7printf("==================================================\n");
} //SDGetIndexIO()
//FUNCTION: Set INDEXIO offset address data.
VOID SDSetINDEXIO() {
U8 Addr;
U8 Offset;
U8 Data;
Addr=hugetbios8(0xA1); // INDEXIO_Address
Offset=hugetbios8(0xA2); // INDEXIO_Offset
a7printf("INDEXIO %02Xh Offset %2Xh\n", Addr, Offset);
Data=hugetbios8(0xA3); // Data need to set
a7printf("Set Data value to %02Xh**\n", Data);
SDSetIndex(Addr,Offset,Data);
SDGetIndexIO(Addr);
A7husetbios(0xA0,0x01);
} //SDSetINDEXIO()
//FUNCTION: Set CMOS offset address data.
VOID SDSetCMOS() {
U8 Addr;
U8 Data;
Addr=hugetbios8(0xB1); // CMOS_Address
a7printf("CMOS address %02Xh", Addr);
Data=hugetbios8(0xB2); // Data need to set
a7printf("Set Data value to %02Xh**\n", Data);
SDSetIndex(0x70,Addr,Data);
SDGetIndexIO(0x70);
A7husetbios(0xB0,0x01);
} //SDSetCMOS()
//----------------------------------------------------------------------------- Index IO. <<
//----------------------------------------------------------------------------- Super IO >>
unsigned char SDGet2E(unsigned char sioIndex) {
IoWrite8(0x2E, sioIndex); // Index
return IoRead8(0x2F); // Data
}