summaryrefslogtreecommitdiff
path: root/Board/EM/Thunderbolt/TbtOemBoard/TbtOemLib.c
blob: 9e27a42191d8b7bb0fab9da5f1edd6b9dbcabe94 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2012, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************
//*************************************************************************
// $Header: /Alaska/SOURCE/Modules/Thunderbolt/TbtOemBoard/TbtOemLib.c 13    5/19/14 7:38a Barretlin $
//
// $Revision: 13 $
//
// $Date: 5/19/14 7:38a $
//*************************************************************************
// ReviGpion History
// ----------------
// $Log: /Alaska/SOURCE/Modules/Thunderbolt/TbtOemBoard/TbtOemLib.c $
// 
// 13    5/19/14 7:38a Barretlin
// [TAG]  		EIP165410
// [Category]  	New Feature
// [Description]  	Support Thunderbolt AIC at NB PCIE slot
// [Files]  		TbtPei.c TbtDxe.c TbtGpe.asl TbtSmm.c TbtOemBoard.c
// TbtOemLib.c TbtOemLib.h TbtSetup.sdl TbtSetup.sd TbtSetup.uni
// TbtSetupReset.c
// 
// 12    2/18/14 12:16p Barretlin
// [TAG]  		EIP152401
// [Category]  	Spec Update
// [Severity]  	Minor
// [Description]  	add new Thunderbolt chip series
// [Files]  		TbtOemLib.c
// 
// 11    1/05/14 2:06p Barretlin
// [TAG]  		EIP N/A
// [Category]  	New Feature
// [Description]  	Support Thunderbolt feature Enable/Disable in run time
// Support dynamic Thunderbolt AIC location in run time
// [Files]  		TbtOemBoard.h TbtOemBoard.c TbtOemLib.c TbtOemLib.h
// 
// 10    5/27/13 8:11a Barretlin
// [TAG]  		EIP124914
// [Category]  	New Feature
// [Description]  	Support Falcon Ridge chip
// [Files]  		TbtOemLib.c
// 
// 9     4/12/13 1:48p Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Use token decide default thunderbolt Chip
// [Files]  		TbtOemBoard.sdl TbtOemLib.c
// 
// 8     4/12/13 1:30p Barretlin
// 
// 7     4/10/13 2:57p Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Add new sample code for CRB
// [Files]  		TbtOemLib.c
// 
// 6     4/03/13 2:47a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Double check Security Level between Thunderbolt host FW
// and BIOS are same or not for Redwood Ridge chip when entering setup
// page.
// [Files]  		TbtOemLib.c TbtOemLib.h TbtSetupReset.c
// 
// 5     3/21/13 6:48a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Update sample code to fix FW's security level doesn't
// match BIOS configuration.
// [Files]  		TbtOemLib.c
// 
// 4     2/08/13 2:05a Barretlin
// 
// 3     2/06/13 6:45a Barretlin
// [TAG]  		EIP114556
// [Category]  	Spec Update
// [Severity]  	Minor
// [Description]  	Update RR handshake flow and sample code for
// Thunderbolt RR Spec 0.9
// [Files]  		TbtOemLib.c
// 
// 2     1/27/13 4:36a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Change sample code avoiding side effect
// [Files]  		TbtOemLib.c
// 
// 1     1/10/13 4:57a Barretlin
// Change SS path and update module for Thunderbolt Spec 1.6 for Cactus
// Ridge chip and Thunderbolt Spec 0.5 for Redwood Ridge chip
// 
// 12    12/13/12 4:06a Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Fix build error
// [Files]  		TbtOemLib.c
// 
// 11    12/12/12 4:14a Barretlin
// [TAG]  		EIP108272
// [Category]  	Spec Update
// [Severity]  	Important
// [Description]  	Update to Spec 1.4 to support Redwood Ridge chip
// [Files]  		TbtPei.c TbtSmm.c TbtDxe.c TbtDxe.sdl TbtGpe.asl
// TbtOemBoard.c TbtOemBoard.h TbtOemLib.c TbtOemLib.h
// 
// 10    10/28/12 11:50p Barretlin
// [TAG]  		EIP104870
// [Category]  	Spec Update
// [Severity]  	Important
// [Description]  	Change wake up flow for Spec 1.2 and Spec 1.3
// [Files]  		TbtPei.c TbtSmm.c TbtOemBoard.c TbtOemBoard.h
// TbtOemBoard.sdl TbtOemLib.c TbtOemLib.h
// 
// 9     10/04/12 10:42p Barretlin
// [TAG]  		EIP102947
// [Category]  	Spec Update
// [Severity]  	Normal
// [Description]  	Update Thunderbolt Spec to Rev 1.2
// [Files]  		TbtPei.c TbtOemLib.c TbtOemLib.h
// 
// 8     10/03/12 9:27p Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Change Programming security level timing for saving
// boot time
// [Files]  		TbtPei.c TbtDxe.c TbtOemBoard.h TbtOemLib.c TbtOemLib.h
// 
// 7     5/21/12 2:56a Barretlin
// [TAG]  		EIP90334
// [Category]  	Improvement
// [Description]  	Implement security level function on CRB
// [Files]  		TbtOemLib.c
// 
// 6     4/14/12 4:58a Barretlin
// 
// 5     3/05/12 1:16a Barretlin
// [TAG]  		EIP83266
// [Category]  	Spec Update
// [Description]  	Specificatoin Update 0.90
// [Files]  		TbtSetup.sdl
// TbtSetup.sd
// TbtSetup.uni
// TbtSetup.cif
// TbtOemBoard.h
// TbtOemLib.c
// TbtOemLib.h
// TbtSmm.c
// TbtPei..
// 
// 3     2/20/12 4:50a Wesleychen
// [TAG]         None
// [Category]    Improvement
// [Description] Add TbtOemLib.
// [Files]       TbtOemLib.c; TbtOemLib.h; TbtOemBoard,mak;
//               TbtOemBoard.cif.
// 
//*************************************************************************
#include <Efi.h>
#include <token.h>
#include <AmiLib.h>
#include <AmiDxeLib.h>
#include <AmiCspLib.h>

// Sample code for ITE8728F
/*
VOID SetSioLdn( IN UINT8 Ldn)
{
    IoWrite8(0x2e, Ldn);
    IoWrite8(0x2f, Ldn);
}

UINT8 ReadSio( IN UINT8 Index )
{
    IoWrite8(0x2e, Index);
    return IoRead8(0x2f);
}

VOID WriteSio( IN UINT8 Index, IN UINT8 Value )
{
    IoWrite8(0x2e, Index);
    IoWrite8(0x2f, Value);
}

VOID SetSio( IN UINT8 Index, IN UINT8 Set )
{
    UINT8 Data8;

    IoWrite8(0x2e, Index);
    Data8 = IoRead8(0x2f);
    Data8 |= Set;
    IoWrite8(0x2f, Data8);
}

VOID ResetSio( IN UINT8 Index, IN UINT8 Rst )
{
    UINT8 Data8;

    IoWrite8(0x2e, Index);
    Data8 = IoRead8(0x2f);
    Data8 &= ~Rst;
    IoWrite8(0x2f, Data8);
}

VOID OpenSioConfig ( VOID ) 
{
    IoWrite8(0x2e, 0x87);
    IoWrite8(0x2e, 0x01);
    IoWrite8(0x2e, 0x55);
    IoWrite8(0x2e, 0x55);
}
//*/

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   TbtSetPCIe2TBTCommand
//
// Description: This snipped code contains PCIE2TBT <-> TBT2PCIE handshake 
//              procedure and all related methods called directly or underectly 
//              by TbtSetPCIe2TBTCommand.
//              This function is Intel Sample code(Rev. 1.5).
//
// Input:       UINT8 - UpPortBus
//              UINT8 - Data
//              UINT8 - Command
//              UINTN - Timeout
//
// Output:      BOOLEAN
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN TbtSetPCIe2TBTCommand(
    IN UINT8             UpPortBus,
    IN UINT8             Data,
    IN UINT8             Command,
    IN UINTN             Timeout
)
{
    UINT32 REG_VAL = 0;
    UINTN  Counter = Timeout;
    
    REG_VAL = (Data << 8) | (Command << 1) | PCIE2TBT_VLD_B;
    
    WRITE_PCI32(UpPortBus, 0, 0, RR_PCIE2TBT, REG_VAL);
    
    while(Counter-- > 0){
        // BIOS support of Thunderbolt devices Specification Update Revision 0.9
        // Check Classcode, RevID
        REG_VAL = MMIO_READ32(TBT_CFG_ADDRESS(UpPortBus, 0, 0, PCI_RID));
        if (0xFFFFFFFF == REG_VAL){
            // Device is not here return now
            return FALSE;
        }
    
        // Check TBT2PCIE.Done
        REG_VAL = MMIO_READ32(TBT_CFG_ADDRESS(UpPortBus, 0, 0, RR_TBT2PCIE));
        if (REG_VAL & TBT2PCIE_DON_R){
            break;
        }
    }
    WRITE_PCI32(UpPortBus, 0, 0, RR_PCIE2TBT, 0);
    
    return TRUE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   TbtBeforeSxExitFlow
//
// Description: 
//              
//
// Input:       EFI_PEI_SERVICES - **PeiServices
//              UINT8            - TbtHostSeries
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID TbtBeforeSxExitFlow(
    IN VOID              *Services,
    IN UINT8             TbtHostSeries )
{
// Sample code for CRB
/*
    EFI_PEI_SERVICES    **PeiServices;
    UINT16      GPIOInv;
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT8       Data8;
#else
    UINT32      Data32;
#endif

    PeiServices = (EFI_PEI_SERVICES **)Services;

    if (TbtHostSeries == Cactus_Ridge){
        // only for SharkBay CRB
        GPIOInv = (IoRead16(GPIO_BASE_ADDRESS + GP_IOREG_GPI_INV) & 0xF7FF);
        IoWrite16(GPIO_BASE_ADDRESS + GP_IOREG_GPI_INV, GPIOInv);
    }

#if !defined BWT2_BOARD || BWT2_BOARD == 0
    OpenSioConfig();
    SetSioLdn (0x07);
    
    //program Super IO Base Address
    WriteSio(0x62, (UINT8)(IT8728_GPIO_BASE_ADDRESS >> 8));
    WriteSio(0x63, (UINT8)(IT8728_GPIO_BASE_ADDRESS & 0xFF));

    //program GP20, GP21, GP22 as GPIO pin
    Data8 = ReadSio (0x26);
    Data8 |= 0x07;
    WriteSio (0x26, Data8);
    
    if (TbtHostSeries == Cactus_Ridge){
#if defined (TBT_HR_PWR) && (TBT_HR_PWR != 0xFF)
        //program GP40 as GPIO pin
        Data8 = ReadSio (0x28);
        Data8 |= 0x01;
        WriteSio (0x28, Data8);
#endif
        
        //program GP66, GP67 as GPIO pin
        Data8 = ReadSio (0x29);
        Data8 |= BIT07;
        WriteSio (0x29, Data8);
    } // Cactus Ridge
    
    //program GP20, GP21, GP22 GPIO polarity
    Data8 = ReadSio (0xB1);
    Data8 &= 0xF8;
    WriteSio (0xB1, Data8);
    
#if defined (TBT_HR_PWR) && (TBT_HR_PWR != 0xFF)
    if (TbtHostSeries == Cactus_Ridge){
        //program GP40 GPIO polarity
        Data8 = ReadSio (0xB3);
        Data8 &= 0xFE;
        WriteSio (0xB3, Data8);
    }
#endif
    
    //GP20, GP21, GP22 internal pull-up enable
    Data8 = ReadSio (0xB9);
    Data8 |= 0x03;
    WriteSio (0xB9, Data8);
    
    if (TbtHostSeries == Cactus_Ridge){
#if defined (TBT_HR_PWR) && (TBT_HR_PWR != 0xFF)
        //GP40 internal internal pull-up enable
        Data8 = ReadSio (0xBB);
        Data8 |= 0x01;
        WriteSio (0xBB, Data8);
#endif
     
        //GP66, GP67 internal pull-up enable
        Data8 = ReadSio (0xBD);
        Data8 |= 0xC0;
        WriteSio (0xBD, Data8);
    }
    
    //GP20, GP21, GP22 Simple I/O enable
    Data8 = ReadSio (0xC1);
    Data8 |= 0x07;
    WriteSio (0xC1, Data8);
     
#if defined (TBT_HR_PWR) && (TBT_HR_PWR != 0xFF)
    if (TbtHostSeries == Cactus_Ridge){
        //GP40 Simple I/O enable
        Data8 = ReadSio (0xC3);
        Data8 |= 0x01;
        WriteSio (0xC3, Data8);
    }
#endif
     
    //GP20, GP21, GP22 Input/Output Configure
    //1:Output Mode 0:Input Mode
    Data8 = ReadSio (0xC9);
    Data8 |= 0x03;
    WriteSio (0xC9, Data8);
    
    if (TbtHostSeries == Cactus_Ridge){
#if defined (TBT_HR_PWR) && (TBT_HR_PWR != 0xFF)
        //GP40 Input/Output Configure
        //1:Output Mode 0:Input Mode
        Data8 = ReadSio (0xCB);
        Data8 |= 0x01;
        WriteSio (0xCB, Data8);
#endif
        
        //GP66, GP67 Input/Output Configure
        //1:Output Mode 0:Input Mode
        Data8 = ReadSio (0xCD);
        Data8 |= 0xC0;
        WriteSio (0xCD, Data8);
    }
    
    if (TbtHostSeries != Cactus_Ridge){
        //Pull high GPIO_9
        IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, 0x04);
    } // RR, FR and WR
#else
    // program ownship
    //Data32 = IoRead32(GPIO_BASE_ADDRESS + (TBT_HotPlugEvt/32)*4);
    //Data32 |= (BIT00 << (TBT_ForcePWR%32));
    //IoWrite32((GPIO_BASE_ADDRESS + (TBT_HotPlugEvt/32)*4), Data32);
    
    //Data32 = IoRead32(GPIO_BASE_ADDRESS + (TBT_HotPlugEvt/32)*4);
    //Data32 &= ~(BIT00 << (TBT_HotPlugEvt%32));
    //IoWrite32((GPIO_BASE_ADDRESS + (TBT_HotPlugEvt/32)*4), Data32);
    
    // program GPIO pin setting
    // TBT_ForcePWR is GPIO, is Output, is Level mode
    //Data32 = (BIT00 | BIT04) & (~BIT02);
    //IoWrite32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)), Data32);
    
    // TBT_HotPlugEvt is GPIO, is input, need invert, is Edge mode
    //Data32 = (BIT00 | BIT02 | BIT03) & (~BIT04);
    //IoWrite32((GPIO_BASE_ADDRESS + 0x100 + (TBT_HotPlugEvt * 8)), Data32);
#endif
//*/
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   GetHRInfo
//
// Description: Pull High GPIO_3 and assign temp bus to get Thunderbolt Host
//              Chip Series for distinguishing Thunderbolt host is Cactus Ridge 
//              or Redwood Ridge
//
//              If your case is only support Redwood Ridge or only support Cactus
//              Ridge, you can just return Thunderbolt Host number which is defined
//              in TbtOemBoard.h
//
//              According test result, the dynamic detect Thunderbolt HR series
//              still has fail rate, so we don't suggest you using the same way
//              to decide HR series. sample code is just for testing !!!
//
// Input:       None
//
// Output:      UINT8 - Thunderbolt Host chip Series
//                      1 - Cactus Ridge
//                      2 - Redwood Ridge
//                      3 - Falcon Ridge
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT8 GetHRInfo( VOID )
{
    UINT8        TBTHostSeries = DEFAULT_TBT_CHIP;
// Sample code for ITE8728F and WTM2
/*
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT8        Data8;
#else
    UINT32       Data32;
#endif
    UINT16       HRID;
    UINT32       REG_VAL = 0;
    BOOLEAN      RRCmd = FALSE;

#if !defined BWT2_BOARD || BWT2_BOARD == 0  
    OpenSioConfig();
    
    SetSioLdn(0x07);
    
    if(ReadSio (0x62) == 0x00){
        //program Super IO Base Address
        WriteSio(0x62, (UINT8)(IT8728_GPIO_BASE_ADDRESS >> 8));
        WriteSio(0x63, (UINT8)(IT8728_GPIO_BASE_ADDRESS & 0xFF));
    }
    
    //program GP21 as GPIO pin
    Data8 = ReadSio (0x26);
    Data8 |= 0x02;
    WriteSio (0x26, Data8);
    
    //program GP21 GPIO polarity
    Data8 = ReadSio (0xB1);
    Data8 &= 0xFD;
    WriteSio (0xB1, Data8);
    
    //program GP21 internal pull-up enable
    Data8 = ReadSio (0xB9);
    Data8 |= 0x02;
    WriteSio (0xB9, Data8);
    
    //program GP21 Simple I/O enable
    Data8 = ReadSio (0xC1);
    Data8 |= 0x02;
    WriteSio (0xC1, Data8);
    
    //program GP21 Input/Output Configure
    //1:Output Mode 0:Input Mode
    Data8 = ReadSio (0xC9);
    Data8 |= 0x02;
    WriteSio (0xC9, Data8);
#else
#endif
    
    // Assign temp bus
    WRITE_PCI16(TBT_UP_PORT_BUS, TBT_UP_PORT_DEV, TBT_UP_PORT_FUNC, PCI_PBUS+1, 0x0505);
    // Do a dummy Write
    WRITE_PCI32(5, 0, 0, PCI_VID, 0x12345678);
    
    // Pull High GPIO_3(__FORCE_PWR)
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
    Data8 |= 0x02;
    IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);
#else
    Data32 = IoRead32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)));
    Data32 |= BIT31;
    IoWrite32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)), Data32);
#endif

    // Delay 100 ms
    CountTime(100000, PM_BASE_ADDRESS);
    
    //Write OS_UP commond for RR chip
    TbtSetPCIe2TBTCommand(5, 0, TBT_OS_UP, 0x8FFFF);
    
    // Delay 600 ms
    CountTime(600000, PM_BASE_ADDRESS);
    
    // Get HR Info
    HRID = READ_PCI16(5, 0, 0, PCI_DID);
    switch (HRID){
        case 0x1547: // Cactus Ridge 4C
        case 0x1548: // Cactus Ridge 2C
            TBTHostSeries = Cactus_Ridge;
            break;
        
        case 0x1567: // Redwood Ridge 2C
        case 0x1569: // Redwood Ridge 4C
        case 0x156B: // Falcon Ridge 2C
        case 0x156D: // Falcon Ridge 4C
        case 0x157E: // BDW-TBT-LP(WR)
        default:
            if ((HRID == 0x1567) || (HRID == 0x1569)){
                TBTHostSeries = Redwood_Ridge;
            } else if ((HRID == 0x156B) || (HRID == 0x156D)){
                TBTHostSeries = Falcon_Ridge;
            } else {
                TBTHostSeries = BDW_TBT_LP;
            }
            
            // Reset FW's security level for RR chip, only for FW rev.26 or above.
            TbtSetPCIe2TBTCommand(5, 0, TBT_SET_SECURITY_LEVEL, 0x8FFFF);
            // Delay 100 ms
            CountTime(100000, PM_BASE_ADDRESS);
    } // end switch

    // Pull Low GPIO_3(__FORCE_PWR)
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
    Data8 &= 0xFD;
    IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);
#else
    Data32 = IoRead32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)));
    Data32 &= ~BIT31;
    IoWrite32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)), Data32);
#endif
    
    // Delay 100 ms
    CountTime(100000, PM_BASE_ADDRESS);
    
    // Remove temp bus
    WRITE_PCI32(TBT_UP_PORT_BUS, TBT_UP_PORT_DEV, TBT_UP_PORT_FUNC, PCI_PBUS, 0xFF000000);
//*/
    return TBTHostSeries;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SynchSecurityLevel
//
// Description: When entering Setup page, double check Security Level setting  
//              is same or not between Thunderbolt host Fw and BIOS.
//
//              This function only work for Thunderbolt Redwood Ridge chip
//
// Input:       UINT8           BiosSecurityLevel
//              UINT8           TbtHostLocation
//
// Output:      UINT8           0 - Security Level synchnized without change
//                              1 - Security Level synchnized with programming
//                                  again
//                              2 - Error
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT8 SynchSecurityLevel(
    IN UINT8             BiosSecurityLevel,
    IN UINT8             TbtHostLocation
)
{
    UINT8            SynchState = 0;
// Sample code for ITE8728F and WTM2
/*
    UINT8            TbtHRbus;
    UINT8            PWRFlag = 0;
    UINT8            TBus = TBT_UP_PORT_BUS;
    UINT8            TDev = TBT_UP_PORT_DEV;
    UINT8            TFun = TBT_UP_PORT_FUNC;
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT8            Data8;
#else
    UINT32           Data32;
#endif
    UINT32           RegVal;
    BOOLEAN          CmdDone;

    if (TbtHostLocation < 0x20){
        TFun = TbtHostLocation;
    } else {
        TDev = 0x01;
        TFun = TbtHostLocation - 0x20;
    }

    // Get Thunderbolt Host Router Location
    TbtHRbus = READ_PCI8(TBus, TDev, TFun, PCI_SBUS);

    // Check Thunderbolt Host state
    RegVal = MMIO_READ32(TBT_CFG_ADDRESS(TbtHRbus, 0, 0, PCI_RID));
    if (RegVal == 0xFFFFFFFF){
        // Pull high GPIO_3(__FORCE_PWR) pin
#if !defined BWT2_BOARD || BWT2_BOARD == 0
        Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
        Data8 |= 0x02;
        IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);
#else
        Data32 = IoRead32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)));
        Data32 |= BIT31;
        IoWrite32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)), Data32);
#endif
    
        // Delay 50ms
        CountTime(50000, PM_BASE_ADDRESS);
        PWRFlag = 1;
    }
    
    // Do Redwood Ridge handshake to get Thunderbolt FW security level 
    CmdDone = TbtSetPCIe2TBTCommand(TbtHRbus, 0, TBT_GET_SECURITY_LEVEL, 0x008FFFFF);
    
    if (CmdDone){
        RegVal = READ_PCI32(TbtHRbus, 0, 0, RR_TBT2PCIE);

        if ((RegVal & MASK_ERROR) == 0){
            RegVal = (RegVal & MASK_DATA) >> 8;
        }
        else SynchState = 2;
    } else SynchState = 2;
    // So far, RegVal variable might be:
    // 1: 0xFFFFFFFF
    // 2: Thunderbolt host Revision ID and Class Code
    // 3: Thunderbolt host Fw security level setting

    // check Security Level setting between Thunderbolt Fw and BIOS
    if ((UINT8)RegVal != (BiosSecurityLevel - 1)){ 
        if (PWRFlag == 0){
            // Pull high GPIO_3(__FORCE_PWR) pin
#if !defined BWT2_BOARD || BWT2_BOARD == 0
            Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
            Data8 |= 0x02;
            IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);
#else
            Data32 = IoRead32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)));
            Data32 |= BIT31;
            IoWrite32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)), Data32);
#endif

            PWRFlag = 1;
        }
        IoWrite8(0x80, (BiosSecurityLevel - 1 + 0xC0));
        // After testing, TBT Fw needs Delay 600ms
        CountTime(600000, PM_BASE_ADDRESS);
        
        // Re-config Security Level with BIOS setting
        CmdDone = TbtSetPCIe2TBTCommand(TbtHRbus, (BiosSecurityLevel - 1), TBT_SET_SECURITY_LEVEL, 0x008FFFFF);

        if (CmdDone)  SynchState = 1;
        else SynchState = 2;
    }
    
    if (PWRFlag == 1){
        // Pull low GPIO_3(__FORCE_PWR) pin
#if !defined BWT2_BOARD || BWT2_BOARD == 0
        Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
        Data8 &= 0xFD;
        IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);
#else
        Data32 = IoRead32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)));
        Data32 &= (~BIT31);
        IoWrite32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)), Data32);
#endif

        // Delay 100ms
        CountTime(100000, PM_BASE_ADDRESS);
    }
//*/
    return SynchState;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ProgramTbtSecurityLevel
//
// Description: This function is configure the Thunderbolt security level.
//              OEM Porting required !!!.
//
// Input:       UINT8           TbtSecurityLevel
//              UINT8           TBTHostSeries
//              UINT8           TbtHostLocation
//              BOOLEAN         IsPei
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID ProgramTbtSecurityLevel(
    IN UINT8             *TbtSecurityLevel,
    IN UINT8             TbtHostSeries,
    IN UINT8             TbtHostLocation,
    IN BOOLEAN           IsPei
)
{
// Sample code for ITE8728F and WTM2
/*
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT8               Data8    = 0;
#else
    UINT32              Data32   = 0;
#endif
    UINT8               SecLevel = *TbtSecurityLevel;
    UINT8               TbtHRbus;
    BOOLEAN             RRCmd    = FALSE;
    UINT8               TBus     = TBT_UP_PORT_BUS;
    UINT8               TDev     = TBT_UP_PORT_DEV;
    UINT8               TFun     = TBT_UP_PORT_FUNC;

#if !defined BWT2_BOARD || BWT2_BOARD == 0
    if (TbtHostSeries == Cactus_Ridge){
        // For Cactus Ridge chip
#if defined (TBT_HR_PWR) && (TBT_HR_PWR != 0xFF)
        Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 3);
        if (SecLevel == TBT_OFF_MODE){
            Data8 &= 0xFE;
            IoWrite8(IT8728_GPIO_BASE_ADDRESS + 3, Data8);
            return;
        }
        if (!(Data8 & BIT0)){
            Data8 |= 0x01;
            IoWrite8(IT8728_GPIO_BASE_ADDRESS + 3, Data8);
        }
#else
        if (SecLevel == TBT_OFF_MODE) return;
#endif
        
        Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 5);
        Data8 &= ~(BIT6 | BIT7);
        
        switch (SecLevel)
        {
            case TBT_DIRECT_CONNECTED_WO_NHI:
                Data8 |= BIT6;
                break;
            
            case TBT_REDRIVER_ONLY:
                Data8 |= BIT7;
                break;
            
            case TBT_NORMAL_MODE_WO_NHI:
                break;
            
            case TBT_NORMAL_MODE:
            case TBT_DEBUG_MODE:
            default:
                // Normal mode with NHI.
                Data8 |= (BIT6 | BIT7);
                break;
        } // end of switch

        // Set GP66 and GP67
        IoWrite8(IT8728_GPIO_BASE_ADDRESS + 5, Data8);
        
        // Set GPIO6 and GPIO7 to the desired levels and
        // assert GPIO3 for at least 400ms period.
        // GP21 Pull high
        Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
        Data8 |= 0x02;
        IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);
    } else {
        // For Redwood Ridge / Falcon Ridge / Win Ridge chip
        if (IsPei != TRUE){
            if(TbtHostLocation < 0x20){
                TFun = TbtHostLocation;
            } else {
                TDev = 0x01;
                TFun = TbtHostLocation - 0x20;
            }
            
            // Get Thunderbolt Host Router Location
            TbtHRbus = READ_PCI8(TBus, TDev, TFun, PCI_SBUS);
            
            // First pull high GPIO_3(__FORCE_PWR) pin
            Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
            Data8 |= 0x02;
            IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);

            IoWrite8(0x80, (0x80 | (SecLevel - 1)));
            CountTime(500000, PM_BASE_ADDRESS);
            
            // Do PCIE2TBT handshake
            RRCmd = TbtSetPCIe2TBTCommand(TbtHRbus, (SecLevel - 1), TBT_SET_SECURITY_LEVEL, 0x008FFFFF);
            if(RRCmd){
                IoWrite8(0x80, 0x5D);
                CountTime(100000, PM_BASE_ADDRESS);
            }

            // Finial pull low GPIO_3(__FORCE_PWR) pin
            Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
            Data8 &= 0xFD;
            IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);
        } // is not at PEI phase
    }
#else
    if (IsPei != TRUE){
        if (TbtHostLocation < 20){
            TFun = TbtHostLocation;
        } else {
            TDev = 0x01;
            TFun = TbtHostLocation - 0x20;
        }
     
        //Get Thunderbolt Host Router Location
        TbtHRbus = READ_PCI8(TBus, TDev, TFun, PCI_SBUS);
        
        // First pull high GPIO_3(__FORCE_PWR) pin
        Data32 = IoRead32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)));
        Data32 |= BIT31;
        IoWrite32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)), Data32);
        
        IoWrite8(0x80, (0x80 | (SecLevel - 1)));
        CountTime(500000, PM_BASE_ADDRESS);
        
        // Do PCIE2TBT handshake
        RRCmd = TbtSetPCIe2TBTCommand(TbtHRbus, (SecLevel - 1), TBT_SET_SECURITY_LEVEL, 0x008FFFFF);
        
        //for debug
        if(RRCmd){
            IoWrite8(0x80, 0x5D);
            CountTime(100000, PM_BASE_ADDRESS);
        }
     
        // finial pull low GPIO_3(__FORCE_PWR) pin
        Data32 = IoRead32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)));
        Data32 &= ~BIT31;
        IoWrite32((GPIO_BASE_ADDRESS + 0x100 + (TBT_ForcePWR * 8)), Data32);
    }
#endif
//*/
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   PeiFinialProgramTbtSecurityLevel
//
// Description: if system does not support "Wake from Thunderbolt device" 
//              function, BIOS should depend on Security Level and BootMod to 
//              pull low FORCE_PWR pin or not in PEI phase
//
//              This function only work for Thunderbolt Cactus Ridge chip and 
//              system doesn't support wake up from Thunderbolt Device
//
// Input:       UINT8           TbtSecurityLevel
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID PeiFinialProgramTbtSecurityLevel(
    IN UINT8             TbtSecurityLevel
)
{
// Sample code for ITE8728F
/*
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT8 Data8;
    UINT8 SecLevel = TbtSecurityLevel;

    OpenSioConfig();
    SetSioLdn (0x07);
    if(ReadSio (0x62) == 0x00){
        //program Super IO Base Address
        WriteSio(0x62, (UINT8)(IT8728_GPIO_BASE_ADDRESS >> 8));
        WriteSio(0x63, (UINT8)(IT8728_GPIO_BASE_ADDRESS & 0xFF));
    }
    
    //The 400 ms delay has been done in TbtPei.c
    //So just pull low GPIO_3(__FORCE_PWR) pin without any delay
    if (SecLevel <= 4) {
        Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
        Data8 &= 0xFD;
        IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);
    }
#endif
//*/
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   FinialProgramTbtSecurityLevel
//
// Description: BIOS should depend on Security Level to pull low FORCE_PWR pin
//              or not
//
//              This function only work for Thunderbolt Cactus Ridge chip
//
// Input:       AMI_TBT_PLATFORM_POLICY_PROTOCOL *PlatformPolocy
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID FinialProgramTbtSecurityLevel(
    IN AMI_TBT_PLATFORM_POLICY_PROTOCOL *PlatformPolocy
)
{
// Sample code for ITE8728F
/*
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT8 Data8;
    UINT8 SecLevel = PlatformPolocy->TbtSecurityLevel;
    
    OpenSioConfig();
    SetSioLdn (0x07);
    if(ReadSio (0x62) == 0x00){
        //program Super IO Base Address
        WriteSio(0x62, (UINT8)(IT8728_GPIO_BASE_ADDRESS >> 8));
        WriteSio(0x63, (UINT8)(IT8728_GPIO_BASE_ADDRESS & 0xFF));
    }
    
    if (SecLevel <= 4) {
        Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
        Data8 &= 0xFD;
        IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);
    }
#endif
//*/
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ActiveTbtGpio2
//
// Description: BIOS should assert GO2Sx pin
//              That will trigger Host Router to prepare underlying devices
//              For Thunderbolt host is CR chip
//
//              This function only work for Thunderbolt Cactus Ridge chip 
//
// Input:       None
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS ActiveTbtGpio2 ( VOID )
{
// Sample code for ITE8728F
/*
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT8 Data8;

    OpenSioConfig();

    SetSioLdn(0x07);
    if(ReadSio (0x62) == 0x00){
        //program Super IO Base Address
        WriteSio(0x62, (UINT8)(IT8728_GPIO_BASE_ADDRESS >> 8));
        WriteSio(0x63, (UINT8)(IT8728_GPIO_BASE_ADDRESS & 0xFF));
    }
    
    Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
    Data8 |= BIT0;
    IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);
#endif
//*/
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   InactiveTbtGpio2
//
// Description: BIOS should deassert GO2Sx pin
//              That will trigger Host Router to prepare underlying devices
//
//              This function only work for Thunderbolt Cactus Ridge chip
//
// Input:       None
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS InactiveTbtGpio2 ( VOID )
{
// Sample code for ITE8728F
/*
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT8 Data8;
    
    OpenSioConfig();
    
    SetSioLdn(0x07);
    if(ReadSio (0x62) == 0x00){
        //program Super IO Base Address
        WriteSio(0x62, (UINT8)(IT8728_GPIO_BASE_ADDRESS >> 8));
        WriteSio(0x63, (UINT8)(IT8728_GPIO_BASE_ADDRESS & 0xFF));
    }
  
    Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
    Data8 &= 0xFE;
    IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);
#endif
//*/
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   PollTbtGpio9
//
// Description: BIOS should poll OK2GO2SX_N_OD pin
//              Upon completion of all preparations, Host Router will assert
//              this pin to indicate readiness for Sx entry.
//
//              This function only work for Thunderbolt Cactus Ridge chip
//
// Input:       None
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS PollTbtGpio9 ( VOID )
{
// Sample code for ITE8728F
/*
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT32 counter = 0;
    
    while(IoRead8(IT8728_GPIO_BASE_ADDRESS + 1) & BIT2){
        if (counter == 0x008FFFFF) break;
            counter++;
    }
#endif
//*/
    return EFI_SUCCESS;
};

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   PullDownTbtGpio9
//
// Description: BIOS should pull down OK2GO2SX_N_OD pin in Wake flow
//              if remebered Host Router state was active.
//
//              This function only work for Thunderbolt Cactus Ridge chip and 
//              system doesn't support wake up from Thunderbolt Device
//
// Input:       None
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS PullDownTbtGpio9 ( VOID )
{
// Sample code for ITE8728F
/*
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT8 Data8;
    
    OpenSioConfig();
    
    SetSioLdn(0x07);
    
    if(ReadSio (0x62) == 0x00){
        //program Super IO Base Address
        WriteSio(0x62, (UINT8)(IT8728_GPIO_BASE_ADDRESS >> 8));
        WriteSio(0x63, (UINT8)(IT8728_GPIO_BASE_ADDRESS & 0xFF));
    }
    
    //program GP22 as GPIO pin
    Data8 = ReadSio (0x26);
    Data8 |= 0x04;
    WriteSio (0x26, Data8);
    
    //program GP22 GPIO polarity
    Data8 = ReadSio (0xB1);
    Data8 &= 0xF8;
    WriteSio (0xB1, Data8);
    
    //program GP22 internal pull-up enable
    Data8 = ReadSio (0xB9);
    Data8 |= 0x04;
    WriteSio (0xB9, Data8);
    
    //program GP22 Simple I/O enable
    Data8 = ReadSio (0xC1);
    Data8 |= 0x04;
    WriteSio (0xC1, Data8);
    
    //program GP22 Input/Output Configure
    //1:Output Mode 0:Input Mode
    Data8 = ReadSio (0xC9);
    Data8 |= 0x04;
    WriteSio (0xC9, Data8);
    
    Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 1);
    Data8 &= 0xFB;
    IoWrite8(IT8728_GPIO_BASE_ADDRESS + 1, Data8);
#endif
//*/
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ReleaseTbtGpio9
//
// Description: BIOS should release pull down OK2GO2SX_N_OD pin in Wake flow
//              if remebered Host Router state was active
//
//              This function only work for Thunderbolt Cactus Ridge chip and 
//              system doesn't support wake up from Thunderbolt Device
//
// Input:       None
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS ReleaseTbtGpio9 ( VOID )
{
// Sample code for ITE8728F
/*
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT8 Data8;
    
    OpenSioConfig();
    
    SetSioLdn(0x07);
    
    //program GP22 Input/Output Configure
    //1:Output Mode 0:Input Mode
    Data8 = ReadSio (0xC9);
    Data8 &= 0xFB;
    WriteSio (0xC9, Data8);
#endif
//*/
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   PowerOnPOC
//
// Description: Power on POC to wake up thunderbolt
//
//              This function is optional and only work for Thunderbolt Cactus 
//              Ridge chip and system doesn't support wake up from Thunderbolt 
//              Device.
//
// Input:       None
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS PowerOnPOC ( VOID )
{
// Sample code for ITE8728F
/*
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT8 Data8;
    
    OpenSioConfig();
    SetSioLdn(0x07);
    
    if(ReadSio (0x62) == 0x00){
        //program Super IO Base Address
        WriteSio(0x62, (UINT8)(IT8728_GPIO_BASE_ADDRESS >> 8));
        WriteSio(0x63, (UINT8)(IT8728_GPIO_BASE_ADDRESS & 0xFF));
    }

    //program GP40 as GPIO pin
    Data8 = ReadSio (0x28);
    Data8 |= 0x01;
    WriteSio (0x28, Data8);
    
    //program GP40 GPIO polarity
    Data8 = ReadSio (0xB3);
    Data8 &= 0xFE;
    WriteSio (0xB3, Data8);
    
    //GP40 internal internal pull-up enable
    Data8 = ReadSio (0xBB);
    Data8 |= 0x01;
    WriteSio (0xBB, Data8);
    
    //GP40 Simple I/O enable
    Data8 = ReadSio (0xC3);
    Data8 |= 0x01;
    WriteSio (0xC3, Data8);
    
    //GP40 Input/Output Configure
    //1:Output Mode 0:Input Mode
    Data8 = ReadSio (0xCB);
    Data8 |= 0x01;
    WriteSio (0xCB, Data8);
    
    Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 3);
    if (!(Data8 & BIT0)){
        Data8 |= 0x01;
        IoWrite8(IT8728_GPIO_BASE_ADDRESS + 3, Data8);
    }
#endif
//*/
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   PowerOffPOC
//
// Description: Power off POC to cut off thunderbolt power 
//
//              This function is optional and only work for Thunderbolt Cactus 
//              Ridge chip and system doesn't support wake up from Thunderbolt 
//              Device.
//
// Input:       None
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS PowerOffPOC ( VOID )
{
// Sample code for ITE8728F
/*
#if !defined BWT2_BOARD || BWT2_BOARD == 0
    UINT8 Data8;
    
    OpenSioConfig();
    SetSioLdn(0x07);
    
    if(ReadSio (0x62) == 0x00){
        //program Super IO Base Address
        WriteSio(0x62, (UINT8)(IT8728_GPIO_BASE_ADDRESS >> 8));
        WriteSio(0x63, (UINT8)(IT8728_GPIO_BASE_ADDRESS & 0xFF));
    }
  
    Data8 = IoRead8(IT8728_GPIO_BASE_ADDRESS + 3);
    Data8 &= 0xFE;
    IoWrite8(IT8728_GPIO_BASE_ADDRESS + 3, Data8);
#endif
//*/
    return EFI_SUCCESS;
}

//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2012, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************