summaryrefslogtreecommitdiff
path: root/Chipset/SB/SBPEI.c
blob: 918f226816577e34ee32c29e483610102f7cdef3 (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
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2013, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************

//*************************************************************************
// $Header: /Alaska/BIN/Chipset/Intel/SouthBridge/LynxPoint/Intel Pch SB Chipset/SBPEI.c 57    5/27/15 2:26a Dennisliu $
//
// $Revision: 57 $
//
// $Date: 5/27/15 2:26a $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/BIN/Chipset/Intel/SouthBridge/LynxPoint/Intel Pch SB Chipset/SBPEI.c $
// 
// 57    5/27/15 2:26a Dennisliu
// [TAG]  		EIP219399
// [Category]  	Improvement
// [Description]  	Static code analysis issue found in Aptio4 Intel
// LynxPoint PCH module
// [Files]  		Chipset\SB\SBPEI.c
// 
// 56    11/17/14 7:24a Mirayang
// [TAG]  		EIP190402 
// [Category]  	New Feature
// [Description]  	Support BootScriptHide eModule on Sharkbay CRB project
// 
// 55    9/23/14 6:12a Mirayang
// [TAG]  		EIP183246
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	BIOS capsule update usb issue
// [RootCause]  	Boot mode conditions miss "BOOT_ON_FLASH_UPDATE" in
// InitUsbMisc().
// [Solution]  	Add "BOOT_ON_FLASH_UPDATE" this boot mode conditions .
// 
// 54    7/21/14 10:42p Mirayang
// [TAG]  		EIP176923 
// [Category]  	Improvement
// [Description]  	Program BUC.SDO to 1 on normal boot in PCH component.
// 
// 53    4/01/14 10:18p Barretlin
// [TAG]  		EIP156783
// [Category]  	Improvement
// [Description]  	fix build error when removing usb recovery module
// [Files]  		SbPei.c
// 
// 52    1/14/14 1:37a Barretlin
// [TAG]  		EIP150529
// [Category]  	Improvement
// [Description]  	System hang due to dysfunctional MRC delay routine
// [Files]  		SBPei.c
// 
// 51    1/08/14 11:18p Barretlin
// [TAG]  		EIP149596
// [Category]  	Improvement
// [Description]  	system cannot enter to recovery mode when CMOS is bad
// [Files]  		SBPEI.c
// 
// 50    10/29/13 12:38a Barretlin
// [TAG]  		EIP136997
// [Category]  	Improvement
// [Description]  	fix build error when SUPPORT_RAID_DRIVER token is 0
// [Files]  		SBPei.c
// 
// 49    10/06/13 2:31a Barretlin
// [TAG]  		EIP138340
// [Category]  	Improvement
// [Description]  	SATA drive detection issue in PCH Platform BIOS
// reference code revision 1.6.2
// [Files]  		SB.sdl SBPEI.c
// 
// 48    9/17/13 2:48p Barretlin
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	use token to decide SATA RxEq policy vaule
// [Files]  		SB.sdl SBPei.c
// 
// 47    9/17/13 8:43a Barretlin
// [TAG]  		EIP136354
// [Category]  	Improvement
// [Description]  	remove setting RCBA Coprocessor Error Enable bit
// [Files]  		SB.sdl SbPei.c
// 
// 46    8/23/13 4:34a Barretlin
// [TAG]  		EIP133819
// [Category]  	Improvement
// [Description]  	change platform policy revision to 4
// [Files]  		SBPEI.c
// 
// 45    8/23/13 3:42a Barretlin
// [TAG]  		EIP133819
// [Category]  	Improvement
// [Description]  	update for Intel PCH RC 1.6.2.0
// [Files]  		SB.sdl SBPEI.c
// 
// 44    7/17/13 8:01a Scottyang
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Let's GPIO GPINDIS bit setting by custom define except
// GPO.
// [Files]  		SBPEI.c
// 
// 43    5/23/13 1:56a Scottyang
// [TAG]  		EIP120623
// [Category]  	Improvement
// [Description]  	LCD turn on automatically when resume from S3.
// [Files]  		SBPEI.c, SBDxe.c, AcpiModeEnable.c
// 
// 42    5/13/13 9:14a Scottyang
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Set time to default  when clear cmos by jumper or
// remove battery.
// [Files]  		SBPEI.c
// 
// 40    5/08/13 4:37a Scottyang
// [TAG]  		EIP123117
// [Category]  	Improvement
// [Description]  	Fixed enable USB precondition, and do power off, power
// on, it will hang 9C.
// [Files]  		SBPEI.c
// 
// 39    5/08/13 3:09a Scottyang
// [TAG]  		None
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	Enable bit set at wrong place.
// [RootCause]  	The offset error.
// [Solution]  	Make offset correct.
// [Files]  		SBPEI.c
// 
// 37    5/03/13 1:42a Scottyang
// [TAG]  		EIP115528
// [Category]  	Improvement
// [Description]  	Support XHCI port recovery when reset after boot to OS
// with XHCI driver.
// [Files]  		SBPEI.c
// 
// 36    4/24/13 6:42a Scottyang
// [TAG]  		EIP114861
// [Category]  	Improvement
// [Description]  	For PTT(Fast boot) 15 can reduce post for CD-ROM.
// [Files]  		SBPEI.c
// 
// 35    4/24/13 4:20a Scottyang
// [TAG]  		EIP118667
// [Category]  	Improvement
// [Description]  	For ULT when GPIO is GPO will also set GPI_DIS_DISABLE.
// [Files]  		SBPEI.c
// 
// 34    4/24/13 2:15a Scottyang
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Porting GPI interrupt by LPT-LP EDS 1.5.
// [Files]  		SB.sdl, SB.H, SBPPI.h, SBPEI.c
// 
// 33    4/23/13 4:51a Wesleychen
// [TAG]         None
// [Category]    Improvement
// [Description] Add token "ONLY_CLEAR_RTC_EN_IN_PEI" for improve
//               "EIP120623".
// [Files]       AcpiModeEnable.c; SB.SDL; SBPEI.c
// 
// 31    4/18/13 12:18a Wesleychen
// [TAG]        EIP120623
// [Category]   Bug Fix
// [Severity]   Important
// [Symptom]    LCD doesn't turn on automatically when resume from S3.
// [RootCause]  PM1_STS (PMBASE+00h) are cleared in EnableAcpiMode().
// [Solution]   Avoid PM1_STS clearing behavior is occurring in S3
//              resuming.
//              *AcpiModeEnable.c Rev#8~11(EIP101628 & EIP118531) are are
//              no need be existence.
// [Files]      SBPEI.c; AcpiModeEnable.c
// 
// 29    3/15/13 3:33a Scottyang
// [TAG]  		EIP118121
// [Category]  	Improvement
// [Description]  	Update PCH RC 1.3.0.
// [Files]  		..\ReferenceCode\Chipset\LynxPoint\*.*, SBDxe.c, SBPEI.c,
// SB.sd, SB.uni, GetSetupData.c, SbSetupData.h
// 
// 28    3/12/13 7:40a Scottyang
// [TAG]  		EIP115528
// [Category]  	Improvement
// [Description]  	USB ports are connected to EHCI not XHCI When recovery.
// [Files]  		SBPEI.c
// 
// 27    2/26/13 1:02a Scottyang
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Follow intel BIOS V112 to change IRQ rout.
// [Files]  		SB.sdl, SBPEI.c
// 
// 26    2/09/13 12:12a Scottyang
// [TAG]  		EIP114922
// [Category]  	Improvement
// [Description]  	Update PCH RC 1.1.0.
// [Files]  		..\ReferenceCode\Chipset\LynxPoint\*.*, SBDxe.c, SBPEI.c,
// SB.sd, SB.uni, GetSetupData.c, SbSetupDara.h
// 
// 25    1/31/13 10:51a Scottyang
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Add delay for HDD recovery.
// [Files]  		SBPEI.c
// 
// 24    1/30/13 1:15a Scottyang
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Fix build error at 4653.
// [Files]  		SBPEI.c
// 
// 23    1/27/13 11:01p Scottyang
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Capsule 2.0 crash dump link function.
// [Files]  		SBPEI.c
// SBDxe.c
// SBRun.c
// 
// 22    1/11/13 1:51a Scottyang
// [TAG]  		EIP88358
// [Category]  	Improvement
// [Description]  	Add FORCE_USER_TO_SETUP_IF_CMOS_BAD token
// [Files]  		SBDex.c, SBPei.c, RTC.h, SB.sdl
// 
// 21    12/24/12 5:51a Scottyang
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Add option for XHCI Idel L1 workaroung.
// [Files]  		GetSetupData.c, SbSetupData.h, SB.sd, SB.uni, SBDxe.c,
// SBPEI.c
// 
// 20    12/18/12 6:10a Scottyang
// [TAG] EIP109697
// [Category] Improvement
// [Description] Update PCH RC 0.8.1
// [Files] ReferenceCode\Chipset\LynxPoint\*.*, SBDxe.c, SBPEI.c, SB.sd,
// SbSetupData.c, GetSetupDate.c
// 
// 19    12/13/12 10:31a Scottyang
// [TAG]  		EIP106687
// [Category]  	Improvement
// [Description]  	Add option for delay to detect PCIE card.
// [Files]  		SBPEI.c, SB.sd, SB.uni, GetSetupData.c, SbSetupData.h,
// PciBus.c
// 
// 18    12/13/12 10:17a Scottyang
// [TAG]  		EIP107424
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	 System stops at CKP 0x9C when system performs a cool boot
// and AMI debugger is enabled.
// [RootCause]  	Unexpected USB EHCI Legacy Support Extended status is
// rised, it is out of USB module control.
// [Solution]  	 Clear unexpected USB EHCI Legacy Support Extended
// status.
// [Files]  		SBPEI.c 
// 
// 17    11/20/12 9:47a Scottyang
// [TAG]  		EIP107014
// [Category]  	Improvement
// [Description]  	Update RC 0.8.0
// [Files]  		ReferenceCode\Chipset\LynxPoint\*.*, SBDxe.c, SBPEI.c,
// SB.sd, SbSetupData.c, GetSetupDate.c
// 
// 16    11/06/12 8:12a Scottyang
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Reduce function "GetPchSeries()".
// [Files]  		SBPEI.c, SBDxe.c, SmiHandlerPorting.c, SmiHandlerPorting2.c
// 
// 15    10/25/12 8:16a Scottyang
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Add new device and remove device which no use
// [Files]  		SBPEI.c, SB.sdl
// 
// 14    10/16/12 4:16a Scottyang
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Assign IRQ for device 21
// 
// 13    10/14/12 8:33a Scottyang
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	One rom for two chip and one chip. 
// [Files]  		SPPEIBoard.c, SB.sd, SBDxe.c, SBPEI.c, PCH.asl
// 
// 12    10/12/12 4:55a Scottyang
// [TAG]  		EIP87695
// [Category]  	Improvement
// [Description]  	System should reboot successfully next time if S3
// resume fail
// [Files]  		SB.sdl, SBPei.c
// 
// 11    10/11/12 11:15p Scottyang
// [TAG]  		EIP86096
// [Category]  	Improvement
// [Description]  	Fix SATA AHCI port4 & 5 can't loading recovery image.
// [Files]  		SBPei.c
// 
// 10    9/26/12 3:55a Victortu
// [TAG]           None
// [Category]      Improvement
// [Description]   Update for Intel PCH LPT RC070.
// [Files]         SB.sdl, SBDXE.c, SBPEI.c, Pch.sdl, SB.sd, SB.uni
// 
// [TAG]           None
// [Category]      Improvement
// [Description]   Update for PCH LP GPIO compatible.
// [Files]         SB.sdl, SB.H, AcpiModeEnable.c, AcpiModeEnable.sdl,
// SBDxe.c, SBGeneric.c, SBPEI.c, SBSMI.c, SleepSmi.c,
// SmiHandlerPorting.c, SmiHandlerPorting2.c
// 
// 9     9/12/12 5:20a Victortu
// [TAG]           None
// [Category]      Improvement
// [Description]   Force HPET enabled for MRC initialization.
// [Files]         SB.sd, SBPEI.c
// 
// [TAG]           None
// [Category]      Improvement
// [Description]   Modify for ULT GPIO changed by PCH LPT-LP EDS 1.0.
// [Files]         SB.H, SB.sdl, AcpiModeEnable.c, AcpiModeEnable.sdl,
// SBPEI.c
// 
// 8     8/24/12 6:51a Victortu
// [TAG]           None
// [Category]      Improvement
// [Description]   Sync USB policy for UsbPrecondition function.
// [Files]         SBPEI.c
// 
// 7     8/14/12 11:26p Victortu
// [TAG]           None
// [Category]      Improvement
// [Description]   Update "SB_TEMP_MMIO_BASE" and
// "EHCI_MMIO_BASE_ADDRESS".
// [Files]         SB.sdl, SBDxe.c, SBPEI.c
// 
// 6     8/13/12 10:29a Victortu
// [TAG]           None
// [Category]      Improvement
// [Description]   Update correct MMIO BASE for TempMemBaseAddr policy.
// [Files]         SBPEI.c
// 
// [TAG]           None
// [Category]      Improvement
// [Description]   Update PCH Policy.
// [Files]         SB.sdl, SBDxe.c, SBPEI.c
// 
// [TAG]           None
// [Category]      Improvement
// [Description]   Implement USB Precondition option for policy
// "UsbPrecondition".
// [Files]         GetSetupData.c, SB.sd, SB.uni, SbSetupData.h, SBDxe.c,
// SBPEI.c
// 
// 5     7/27/12 6:14a Victortu
// [TAG]           None
// [Category]      Improvement
// [Description]   Update setup items and policies.
// [Files]         GetSetupData.c, SB.sdl, SB.sd, SB.uni, SbSetupData.h,
// SBPEI.c, SBDXE.c
// 
// [TAG]           None
// [Category]      Improvement
// [Description]   Update to support ULT Platform.
// [Files]         SB.H, SB.mak, SB.sdl, SB.sd, SBSetup.c,
// AcpiModeEnable.c, SBDxe.c, SBPEI.c, SBSMI.c, SleepSmi.c,
// SmiHandlerPorting.c, SmiHandlerPorting2.c, SBPPI.h, Pch.sdl
// 
// 4     7/02/12 10:17a Victortu
// [TAG]		None
// [Category]	Improvement
// [Description]	Updated and modified for PCH RC 0.6.0.
// [Files]		SBGeneric.c, SB.sdl, SBCspLib.h, SBDxe.c, SBPEI.c
// 
// 3     6/13/12 11:34p Victortu
// [TAG]           None
// [Category]      Improvement
// [Description]   Implement Warm Boot function for Secure Flash feature.
// [Files]         SB.H, SB.mak, SB.sdl, SBDxe.c, SBGeneric.c, SBPEI.c,
// SBSMI.c
// 
// 2     2/24/12 2:35a Victortu
// Support RapidStart_SUPPORT.
// 
// 1     2/08/12 8:24a Yurenlai
// Intel Lynx Point/SB eChipset initially releases.
// 
//*************************************************************************
//<AMI_FHDR_START>
//
// Name:        SBPEI.C
//
// Description: This file contains code for South Bridge initialization
//              in the PEI stage
//
//<AMI_FHDR_END>
//*************************************************************************

//----------------------------------------------------------------------------
// Include(s)
//----------------------------------------------------------------------------

#include <Efi.h>
#include <Pei.h>
#include <Token.h>
#include <AmiPeiLib.h>
#include <Hob.h>
#include <Setup.h>
#include <AmiCspLib.h>
// Produced/used PPI interfaces
#include <ppi\PciCfg2.h>
#include <ppi\SBPPI.h>
#include <ppi\CpuIo.h>
#include <ppi\CspLibPpi.h>
#include <RTC.h>
#include <PchAccess.h>
#include <Ppi\PchUsbPolicy\PchUsbPolicy.h>
#include <Ppi\PchInit\PchInit.h>

#include <Ppi\SmbusPolicy\SmbusPolicy.h>
#include <Ppi\PchPlatformPolicy\PchPlatformPolicy.h>

#if defined   iME_SUPPORT && iME_SUPPORT
#include <Guid\MeBiosExtensionSetup\MeBiosExtensionSetup.h>
#endif

#if SB_RESET_PPI_SUPPORT
#include <Ppi\Reset.h>
#endif

#if ATAPI_RECOVERY_SUPPORT
#include <Ppi\AtaController.h>
#endif

#if SB_STALL_PPI_SUPPORT
#include <Ppi\Stall.h>
#endif

#if WdtPei_SUPPORT
#include "ppi\Wdt\Wdt.h"
#endif
#include <ppi\NBPPI.h>

#if Capsule2_0_SUPPORT
#include <ppi\capsule.h>    //CAPSULE20
#endif

#if defined(SUPPORT_RAID_DRIVER) && SUPPORT_RAID_DRIVER && (PTT_VER > 15)
#include <FastBoot.h>

#define SATA1_BDF (0x1f << 3 | 0x02)
#define SATA2_BDF (0x1f << 3 | 0x05)
#endif
//----------------------------------------------------------------------------
// Constant, Macro and Type Definition(s)
//----------------------------------------------------------------------------
// Constant Definition(s)
#define EHCI_MEMORY_SPACE 0x400
#define TIMER_RESOLUTION  1
#define S3_SLP_TYP        0x05

#ifndef SMM_SUPPORT
  #define SMM_SUPPORT   0
#endif

#define RETRAIN_DELAY      50		// [EIP106687]

// Macro Definition(s)

// Type Definition(s)
#if defined   iME_SUPPORT && iME_SUPPORT
// TS on DIMM defines
#define TS_ON_CHANNEL0_SLOT_0_DIMM                 0x1
#define TS_ON_CHANNEL1_SLOT_0_DIMM                 0x2
#define TS_ON_C0_S0_AND_C1_S0_DIMM                 0x3
#endif

#ifndef CAPSULE_SUPPORT

#if defined Capsule2_0_SUPPORT && Capsule2_0_SUPPORT
#define CAPSULE_SUPPORT 1
#else
#define CAPSULE_SUPPORT 0
#endif

#endif

typedef struct _SATA_LENGTH_CONFIG {
    UINT8   SataGen1RxEqEnable;
    UINT8   SataGen1RxEqValue;
    UINT8   SataGen2RxEqEnable;
    UINT8   SataGen2RxEqValue;
    UINT8   SataGen3RxEqEnable;
    UINT8   SataGen3RxEqValue;
} SATA_LENGTH_CONFIG;
// Function Prototype(s)

BOOLEAN IsRecovery (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg,
    IN EFI_PEI_CPU_IO_PPI       *CpuIo
);

#if SB_RESET_PPI_SUPPORT
EFI_STATUS SBPEI_ResetSystem (
    IN EFI_PEI_SERVICES         **PeiServices
);
#endif

#if SB_STALL_PPI_SUPPORT
EFI_STATUS SBPEI_Stall (
    IN EFI_PEI_SERVICES         **PeiServices, 
    IN EFI_PEI_STALL_PPI        *This,
    IN UINTN                    Microseconds
);
#endif

#if ATAPI_RECOVERY_SUPPORT
EFI_STATUS EFIAPI EnableAtaChannel (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN PEI_ATA_CONTROLLER_PPI   *This,
    IN UINT8                    ChannelIndex
);
#endif

BOOLEAN IsS3 (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_CPU_IO_PPI       *CpuIo
);

VOID ProgramGPIO (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_CPU_IO_PPI       *CpuIo,
    IN SB_SETUP_DATA            *SbSetupData,
    IN AMI_GPIO_INIT_TABLE_STRUCT  *pTable
);

VOID ProgramSBSubId(
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg,
    IN AMI_PEI_SB_CUSTOM_PPI    *SBPeiOemPpi
);

VOID InitPCIe (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_CPU_IO_PPI       *CpuIo,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg
);

VOID InitSMBus (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_CPU_IO_PPI       *CpuIo,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg
);

VOID InitUsbMisc (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_CPU_IO_PPI       *CpuIo,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg,
    IN EFI_BOOT_MODE            BootMode
);

EFI_STATUS UpdateBootMode (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_CPU_IO_PPI       *CpuIo,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg
);

EFI_STATUS ProgramSBRegAfterMemInstalled (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
    IN VOID                     *InvokePpi
);

EFI_STATUS ProgramSBRegBeforeEndofPei (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
    IN VOID                     *InvokePpi
);

EFI_STATUS ProgramSBRegAfterMrc (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
    IN VOID                     *InvokePpi
);

EFI_STATUS ProgramSBRegEndOfMrc (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
    IN VOID                     *InvokePpi
);

VOID InitPMRegs(
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_CPU_IO_PPI       *CpuIo,
    IN SB_SETUP_DATA            *SbSetupData
);

VOID InitRTC(
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_CPU_IO_PPI       *CpuIo
);

VOID WriteSBDefaultSubId (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg
);

VOID ProgramRCRBMmio (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_CPU_IO_PPI       *CpuIo 
);

EFI_STATUS
ProgramSBIoDecodeRegs (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg
);

EFI_STATUS
ProgramPchDeviceBase (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_PCI_CFG2_PPI      *PciCfg
);

EFI_STATUS
GeneralPowerFailureHandler (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_CPU_IO_PPI       *CpuIo,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg
);

EFI_STATUS
SetTheStateToGoAfterG3 (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_CPU_IO_PPI       *CpuIo,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg,
    IN SB_SETUP_DATA            *SbSetupData
);

EFI_STATUS InstallPchPlatformPolicyPpi (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN SB_SETUP_DATA            *SbSetupData
);

EFI_STATUS
InstallAmtPlatformPolicyPpi (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN SB_SETUP_DATA            *SbSetupData
);

EFI_STATUS
CreateAmtForcePushPetPolicyHob(
    IN EFI_PEI_SERVICES         **PeiServices
);

//----------------------------------------------------------------------------
// Variable and External Declaration(s)
//----------------------------------------------------------------------------
// Variable Declaration(s)

// GUID Definition(s)
EFI_GUID gAmiPEISBInitPolicyGuid     = AMI_PEI_SBINIT_POLICY_PPI_GUID;
EFI_GUID gAmiPEIPCITableInitPpiGuid  = AMI_PEI_PCI_TABLE_INIT_PPI_GUID;
EFI_GUID gMasterBootModeGuid         = EFI_PEI_MASTER_BOOT_MODE_PEIM_PPI;
EFI_GUID gRecoveryBootModeGuid       = EFI_PEI_BOOT_IN_RECOVERY_MODE_PEIM_PPI;
EFI_GUID gEfiPeiPermMemInstalledGuid = EFI_PEI_PERMANENT_MEMORY_INSTALLED_PPI;
EFI_GUID gEfiPeiEndOfPeiPhasePpiGuid = EFI_PEI_END_OF_PEI_PHASE_PPI_GUID;
EFI_GUID gPeiSmbusPolicyPpiGuid      = PEI_SMBUS_POLICY_PPI_GUID;
EFI_GUID gPchPlatformPolicyPpiGuid   = PCH_PLATFORM_POLICY_PPI_GUID;
EFI_GUID gSetupGuid                  = SETUP_GUID;
EFI_GUID gPchUsbPolicyPpiGuid        = PCH_USB_POLICY_PPI_GUID;
EFI_GUID gPchInitPpiGuid             = PCH_INIT_PPI_GUID;

EFI_GUID gPeiCompleteMRCGuid         = AMI_PEI_AFTER_MRC_GUID;

#if SB_STALL_PPI_SUPPORT
EFI_GUID gStallPpiGuid = EFI_PEI_STALL_PPI_GUID;
#endif

#if SB_RESET_PPI_SUPPORT
EFI_GUID gPeiResetPpiGuid = EFI_PEI_RESET_PPI_GUID;
#endif

#if ATAPI_RECOVERY_SUPPORT
EFI_GUID gPeiAtaControllerPpiGuid = PEI_ATA_CONTROLLER_PPI_GUID;
#endif

#if WdtPei_SUPPORT
EFI_GUID  gWdtPpiGuid = WDT_PPI_GUID;
#endif

EFI_GUID gOemPchPlatformPolicyOverridePpiGuid   = AMI_PEI_SB_OEM_PLATFORM_POLICY_OVERRIDE_PPI_GUID;

// PPI Definition(s)

static AMI_PEI_SBINIT_POLICY_PPI mAMIPEISBInitPolicyPpi = {
    TRUE
};

#if ATAPI_RECOVERY_SUPPORT
static PEI_ATA_CONTROLLER_PPI mAtaControllerPpi = {
    EnableAtaChannel 
};
#endif

#if SB_STALL_PPI_SUPPORT
static EFI_PEI_STALL_PPI mStallPpi = {
    TIMER_RESOLUTION,
    SBPEI_Stall
};
#endif

#if SB_RESET_PPI_SUPPORT
static EFI_PEI_RESET_PPI mResetPpi = { 
    SBPEI_ResetSystem
};
#endif

// PPI that are installed

#if SB_STALL_PPI_SUPPORT
static EFI_PEI_PPI_DESCRIPTOR mBeforeBootModePpiList[] = {
    { EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, \
      &gStallPpiGuid, &mStallPpi },
};
#endif

static EFI_PEI_PPI_DESCRIPTOR mBootModePpi[] = {
    { EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, \
      &gMasterBootModeGuid, NULL },
};

static EFI_PEI_PPI_DESCRIPTOR mRecoveryModePpi[] = {
    { EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, \
      &gRecoveryBootModeGuid, NULL },
};

static EFI_PEI_PPI_DESCRIPTOR mPpiList[] = {
#if ATAPI_RECOVERY_SUPPORT
    { EFI_PEI_PPI_DESCRIPTOR_PPI, \
      &gPeiAtaControllerPpiGuid, &mAtaControllerPpi },
#endif
#if SB_RESET_PPI_SUPPORT
    { EFI_PEI_PPI_DESCRIPTOR_PPI, &gPeiResetPpiGuid, &mResetPpi },
#endif
    { EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, \
      &gAmiPEISBInitPolicyGuid, &mAMIPEISBInitPolicyPpi }
};

// PPI that are notified

static EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList[] = {
    { EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | \
      EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, \
      &gEfiPeiEndOfPeiPhasePpiGuid, ProgramSBRegBeforeEndofPei },
};

static EFI_PEI_NOTIFY_DESCRIPTOR mMemoryReadyNotify[] = {
    { EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | \
      EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
      &gEfiPeiPermMemInstalledGuid, ProgramSBRegAfterMemInstalled }
};

static EFI_PEI_NOTIFY_DESCRIPTOR AfterMrcNotifyList[] = {
    { EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | \
      EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, \
      &gAmiPeiAfterMrcGuid, ProgramSBRegAfterMrc },
};
static EFI_PEI_NOTIFY_DESCRIPTOR EndOfMrcNotifyList[] = {
    { EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | \
      EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, \
      &gAmiPeiEndOfMemDetectGuid, ProgramSBRegEndOfMrc },
};

static EFI_PEI_PPI_DESCRIPTOR StallPpiDescriptor_InMemory[] =
{ 
  {
    EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
    &gStallPpiGuid, 
    &mStallPpi 
  }
};

// External Declaration(s)

extern AMI_GPIO_INIT_TABLE_STRUCT stSB_GPIODefaultInitTable[];
extern AMI_GPIO_INIT_TABLE_STRUCT stSB_GPIODefaultULTInitTable[];

extern EFI_STATUS CountTime (
    IN UINTN            DelayTime,
    IN UINT16           BaseAddr
);

#if SB_RESET_PPI_SUPPORT
extern VOID SBLib_ResetSystem (
    IN EFI_RESET_TYPE   ResetType
);
#endif

// Function Definition(s)

#ifdef RAPID_START_FLAG

#define RAPID_START_FLAG_ENTRY_DONE   BIT0

EFI_STATUS
RapidStartGetFlag (
  OUT     UINT8                   *Value
  )
{
  *Value = RtcRead (FFS_NV_FLAG_REG);
  return EFI_SUCCESS;
}

BOOLEAN
RapidStartResumeCheck (
  VOID
)
{
  EFI_STATUS                        Status;
  BOOLEAN                           RapidStartFlag;

  Status = RapidStartGetFlag (&RapidStartFlag);
  if ( !EFI_ERROR (Status) && ((RapidStartFlag & RAPID_START_FLAG_ENTRY_DONE) != 0)) {
    return TRUE;
  }

  return FALSE;
}
#endif

//----------------------------------------------------------------------------

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   IsS3
//
// Description: This function determines if the system is resuming from an S3
//              sleep state.
//
// Input:       PeiServices - Pointer to the Pei Services function and data
//                            structure.
//
// Output:      TRUE - It is an S3 Resume
//              FALSE - It is not an S3 Resume
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

BOOLEAN IsS3 (
  IN EFI_PEI_SERVICES     **PeiServices,
  IN EFI_PEI_CPU_IO_PPI   *CpuIo )
{
  // Check PWR_FLR Bit
  if ((READ_PCI8_SB(R_PCH_LPC_GEN_PMCON_3) & B_PCH_LPC_GEN_PMCON_PWR_FLR) == 0) // 0xA4
    // Check PWRBTN override
    if ((READ_IO16_PM(R_PCH_ACPI_PM1_STS) & B_PCH_ACPI_PM1_STS_PRBTNOR) == 0) // 0x00
      // Check WAK_STS bit
      if ((READ_IO16_PM(R_PCH_ACPI_PM1_STS) & B_PCH_ACPI_PM1_STS_WAK)) // 0x00
        // Check the sleep type
        if ((READ_IO16_PM(R_PCH_ACPI_PM1_CNT) & B_PCH_ACPI_PM1_CNT_SLP_TYP) == V_PCH_ACPI_PM1_CNT_S3) //0x04
          return TRUE;

    return FALSE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   IsS4
//
// Description: This function determines if the system is resuming from an S4
//              sleep state.
//
// Input:       PeiServices - Pointer to the Pei Services function and data
//                            structure.
//
// Output:      TRUE - It is an S4 Resume
//              FALSE - It is not an S4 Resume
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

BOOLEAN IsS4 (
    IN EFI_PEI_SERVICES     **PeiServices,
    IN EFI_PEI_CPU_IO_PPI   *CpuIo )
{
    if ((READ_IO16_PM(ACPI_IOREG_PM1_CNTL) & 0x1c00) == 0x1800) //0x04
        return TRUE;
    return FALSE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   IsCmosBad
//
// Description: This function determines CMOS data is available.
//
// Input:       PeiServices - Pointer to the Pei Services function and data
//                            structure.
//
// Output:      TRUE - CMOS data is bad
//              FALSE - CMOS DATA is available
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

BOOLEAN IsCmosBad (
    IN EFI_PEI_SERVICES     **PeiServices,
    IN EFI_PEI_CPU_IO_PPI   *CpuIo )
{
    return (READ_IO8_RTC(CMOS_BAD_REG | RTC_NMI_MASK) & 0xc0) ? TRUE : FALSE;
}

#if KBC_SUPPORT && Recovery_SUPPORT
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ResetKbc
//
// Description: This function resets Keyboard controller for Ctrl-Home
//              recovery function.     
//
// Input:       PeiServices - Pointer to the Pei Services function and
//                            data structure
//              CpuIo       - Pointer to the CPU I/O PPI
//              PciCfg      - Pointer to the PCI Configuration PPI
//
// Output:      None
//
// Notes:       No porting required.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID ResetKbc (
    IN EFI_PEI_SERVICES     **PeiServices,
    IN EFI_PEI_CPU_IO_PPI   *CpuIo,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg
)
{
    volatile UINT8      KbcSts = 0;
    volatile UINT8      Buffer8;
    UINT32              TimeOut = 0x100;

    // Reset KBC
    if (CpuIo->IoRead8( PeiServices, CpuIo, KBC_IO_STS ) != 0xff) {
        // Clear KBC buffer
        do {
            Buffer8 = CpuIo->IoRead8( PeiServices, CpuIo, KBC_IO_DATA );
            KbcSts = CpuIo->IoRead8( PeiServices, CpuIo, KBC_IO_STS ); // 0x64
            TimeOut--;
        } while ((KbcSts & 3) && (TimeOut != 0));


        // Send BAT command 
        CpuIo->IoWrite8( PeiServices, CpuIo, KBC_IO_STS, 0xaa ); // 0x64

        // IBFree
        for (TimeOut = 0; TimeOut < 0x1000; TimeOut++) {
            CpuIo->IoWrite8( PeiServices, CpuIo, IO_DELAY_PORT, KbcSts );
            KbcSts = CpuIo->IoRead8( PeiServices, CpuIo, KBC_IO_STS ); // 0x64
            if ((KbcSts & 2) == 0) break;
        }

        // OBFree
        for (TimeOut = 0; TimeOut < 0x500; TimeOut++) {
            CpuIo->IoWrite8( PeiServices, CpuIo, IO_DELAY_PORT, KbcSts );
            KbcSts = CpuIo->IoRead8( PeiServices, CpuIo, KBC_IO_STS ); // 0x64
            if (KbcSts & 1) break;
        }

        // Get result if needed
        if (KbcSts & 1)
            Buffer8 = CpuIo->IoRead8( PeiServices, CpuIo, KBC_IO_DATA );
    }

    // Clear KBC status buffer.
    KbcSts = CpuIo->IoRead8 ( PeiServices, CpuIo, KBC_IO_STS ); // 0x64
}
#endif

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   UpdateBootMode
//
// Description: This function determines the boot mode of the system.
//              After the correct boot mode has been determined, the PEI 
//              Service function SetBootMode is called and then
//              the MasterBootModePpi is installed
//
// Input:       PeiServices - Pointer to the Pei Services function and
//                            data structure
//              CpuIo       - Pointer to the CPU I/O PPI
//              PciCfg      - Pointer to the PCI Configuration PPI
//
// Output:      Always returns EFI_SUCCESS
//              Also defines the boot mode for the system
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS UpdateBootMode (
    IN EFI_PEI_SERVICES     **PeiServices,
    IN EFI_PEI_CPU_IO_PPI   *CpuIo,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg
)
{
    EFI_STATUS              Status;
    EFI_BOOT_MODE           BootMode;

    // Check for changes in the possible boot modes.  This should be made in
    // prioritized order.  At the end of this function the boot mode is
    // determined.  The EFI_BOOT_MODE is defined in the PEI Spec

    Status = (*PeiServices)->GetBootMode (PeiServices, &BootMode);
    if (EFI_ERROR(Status) || (BootMode != BOOT_IN_RECOVERY_MODE))
      BootMode = BOOT_WITH_FULL_CONFIGURATION;

    // Returns 0 if no S3 resume detected returns -1 if this is an S3 boot
    if (IsS3(PeiServices, CpuIo)) {
        BootMode = BOOT_ON_S3_RESUME;
        PEI_TRACE((-1,PeiServices, "Boot mode = BOOT_ON_S3_RESUME\n"));
    } else {
        // Check for S4 resume
        if (IsS4(PeiServices, CpuIo)) {
            BootMode = BOOT_ON_S4_RESUME;
            PEI_TRACE((-1, PeiServices, "Boot mode = BOOT_ON_S4_RESUME\n"));
        }
        // Check for recovery mode
        #if KBC_SUPPORT && Recovery_SUPPORT && PERFORM_KBC_RESET
            ResetKbc(PeiServices,  CpuIo, PciCfg);
        #endif

        if (IsRecovery(PeiServices, PciCfg, CpuIo))
          BootMode = BOOT_IN_RECOVERY_MODE;
    }

    if (IsCmosBad(PeiServices, CpuIo)) {
        if (BootMode != BOOT_IN_RECOVERY_MODE){
            BootMode = BOOT_WITH_DEFAULT_SETTINGS;
            PEI_TRACE((-1,PeiServices,"Boot mode = BOOT_WITH_DEFAULT_SETTING\n"));
        }

#if FORCE_USER_TO_SETUP_IF_CMOS_BAD                                        // [EIP88358] >>
{
        EFI_STATUS          Status;
        UINT16              HobSize = sizeof(CMOS_BAD_HOB);
        EFI_GUID            CmosBadHobGuid = CMOS_BAD_HOB_GUID;
        CMOS_BAD_HOB        *CmosBadHob;

        Status = (*PeiServices)->CreateHob( PeiServices,
                                            EFI_HOB_TYPE_GUID_EXTENSION,
                                            HobSize,
                                            &CmosBadHob);
        if(!EFI_ERROR(Status)) {
            CmosBadHob->Header.Name = CmosBadHobGuid;    
        }
}
#endif                                                                     // [EIP88358] <<
    }
#if Capsule2_0_SUPPORT

#else
  #if CAPSULE_SUPPORT
    if (!EFI_ERROR(CheckIfCapsuleAvailable()))
        BootMode = BOOT_ON_FLASH_UPDATE;
  #endif
#endif
    // Set the system BootMode
    (*PeiServices)->SetBootMode(PeiServices, BootMode);

    // Let everyone know that boot mode has been determined by installing the
    // MasterBootMode PPI
    (*PeiServices)->InstallPpi(PeiServices, mBootModePpi );

    (*PeiServices)->GetBootMode (PeiServices, &BootMode); 

    if (BootMode == BOOT_IN_RECOVERY_MODE) // Recovery Boot Mode PPI
        (*PeiServices)->InstallPpi( PeiServices, mRecoveryModePpi );

                                        // [EIP87695]>
#if SYSTEM_REBOOT_NORMALLY_IF_S3_IS_FAILED
    if (BootMode == BOOT_ON_S3_RESUME) //S3 Boot Mode PPI
        WRITE_IO16_PM(ACPI_IOREG_PM1_CNTL, READ_IO16_PM(ACPI_IOREG_PM1_CNTL) & 0xe3ff ); // Clear S3 for avoiding S3 resume twice
#endif
                                        // <[EIP87695]

    return EFI_SUCCESS;
}

#if defined(SUPPORT_RAID_DRIVER) && SUPPORT_RAID_DRIVER && (PTT_VER > 15)
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   DetectSataPortInfo
//
// Description: This function provides SATA Port Information
//
// Input:       PeiServices - Pointer to the PEI services table
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID DetectSataPortInfo (
    IN EFI_PEI_SERVICES     **PeiServices)
{
    EFI_STATUS          Status;
    UINT16              HobSize = sizeof(SATA_PRESENT_HOB);
    EFI_GUID            SataPresentHobGuid = AMI_SATA_PRESENT_HOB_GUID;
    SATA_PRESENT_HOB    *SataPresentHob;
    UINT16              SataClassCode;
    UINT8               SataPortStatus;
    UINT8               SataPortEnable = 0;
    SB_SETUP_DATA       SbSetupData;
    UINT8               i;

    Status = (*PeiServices)->CreateHob (PeiServices,
                                        EFI_HOB_TYPE_GUID_EXTENSION,
                                        HobSize,
                                        &SataPresentHob);
    if(EFI_ERROR(Status)) return;

    SataPresentHob->EfiHobGuidType.Name = SataPresentHobGuid;

    for (i = 0; i < 4; i++) {
      SataPresentHob->SataInfo[i].ClassCode = 0;
      SataPresentHob->SataInfo[i].PresentPortBitMap = 0;
    }

    // The SATA Mode Select should be configured in PchInitPeim.
    SataClassCode = READ_PCI16_SATA(R_PCH_SATA_SUB_CLASS_CODE);
    SataPresentHob->SataInfo[0].ClassCode = SataClassCode;

    if ((SataClassCode & 0xFF) == V_PCH_SATA_SUB_CLASS_CODE_IDE) {
      // Lynx Point-LP didn't support IDE mode, so code should not enter here.
      SataPortStatus = READ_PCI16_SATA(R_PCH_SATA_PCS) >> 8;
      SataPresentHob->SataInfo[0].PresentPortBitMap = (SATA1_BDF << 16) | (SataPortStatus & 0xF); // Port 0~3
      SataPortStatus = READ_PCI16_SATA2(R_PCH_SATA_PCS) >> 8;
      SataPresentHob->SataInfo[1].PresentPortBitMap = (SATA2_BDF << 16) | (SataPortStatus & 0x3); // Port 4~5
      SataPresentHob->SataInfo[1].ClassCode = SataClassCode;
      SataPresentHob->ControllerCount = 2;
    } else { // AHCI or Raid
      GetSbSetupData (PeiServices, &SbSetupData, TRUE);
      SataPortStatus = READ_PCI16_SATA(R_PCH_SATA_PCS) >> 8;
      for (i = 0; i < GetPchMaxSataPortNum(); i++) {
        // SataPort controll is done in DXE, so check Setup value here.
        SataPortEnable |= (SbSetupData.SataPort[i] << i);
      }
      SataPortStatus &= SataPortEnable;
      SataPresentHob->SataInfo[0].PresentPortBitMap = (SATA1_BDF << 16) | (SataPortStatus & 0x3F); // Port 0~5
      SataPresentHob->ControllerCount = 1;
    }
}
#endif

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SBPEI_Init
//
// Description: This function is the entry point for this PEI. This function
//              initializes the chipset SB
//
// Input:       FfsHeader   - Pointer to the FFS file header
//              PeiServices - Pointer to the PEI services table
//
// Output:      Return Status based on errors that occurred while waiting for
//              time to expire.
//
// Notes:       This function should initialize South Bridge for memory 
//              detection.
//              Install AMI_PEI_SBINIT_POLICY_PPI to indicate that SB Init
//              PEIM is installed
//              Following things can be done at this point:
//                  - Enabling top of 4GB decode for full flash ROM
//                  - Programming South Bridge ports to enable access to
//                    South Bridge and other I/O bridge access
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI SBPEI_Init (
    IN EFI_FFS_FILE_HEADER      *FfsHeader,
    IN EFI_PEI_SERVICES         **PeiServices )
{
    EFI_STATUS                  Status;
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg;
    EFI_PEI_CPU_IO_PPI          *CpuIo;
    AMI_PEI_PCI_TABLE_INIT_PPI  *AMIPCITableInit;
    SB_SETUP_DATA               SbSetupData;
    AMI_GPIO_INIT_TABLE_STRUCT  *pTable;
    AMI_PEI_SB_CUSTOM_PPI       *SBPeiOemPpi = NULL;
	  PCH_SERIES PchSeries = GetPchSeries();

    // Get pointer to the PCI config PPI
    PciCfg = (*PeiServices)->PciCfg;
    CpuIo = (*PeiServices)->CpuIo;

    PEI_PROGRESS_CODE (PeiServices, PEI_CAR_SB_INIT);
    WRITE_IO8(PORTB_IO_CNTL, 0x0c); // Disable IOCHK NMI #, PCI SERR#. (0x61)

    // Locate AMI PCI Table Init PPI
    Status = (*PeiServices)->LocatePpi( PeiServices, \
                                        &gAmiPEIPCITableInitPpiGuid, \
                                        0, \
                                        NULL, \
                                        &AMIPCITableInit );

    // Assert if not found - the AMI PCI Table Init PPI should exist
    ASSERT_PEI_ERROR( PeiServices, Status );

    GetSbSetupData( PeiServices, &SbSetupData, TRUE );

#if WdtPei_SUPPORT
{
    WDT_PPI                     *WdtPpi;

    // Locate WDT PPI for access to Wdt->Disable()
    //
    Status = (*PeiServices)->LocatePpi (
                      PeiServices,
                      &gWdtPpiGuid,
                      0,
                      NULL,
                      &WdtPpi
                      );
    if (!EFI_ERROR (Status)) {
        WdtPpi->Disable();
    }
}
#endif
    // Program Pch devices bar base
    ProgramPchDeviceBase(PeiServices, PciCfg);

#if SB_STALL_PPI_SUPPORT
    // Install the SB Stall PPI
    Status = (*PeiServices)->InstallPpi( PeiServices, \
                                         &mBeforeBootModePpiList[0] );
    ASSERT_PEI_ERROR( PeiServices, Status );
#endif

    // Program Pch RCBA base
    ProgramRCRBMmio(PeiServices, CpuIo);

    UpdateBootMode( PeiServices, CpuIo, PciCfg );

    // Install PchPlatformPolicyPpi, it will notify to PchInitialize.
    InstallPchPlatformPolicyPpi( PeiServices, &SbSetupData);

    //DeCode LPC IO
    ProgramSBIoDecodeRegs( PeiServices, PciCfg);

    //Program GPIOs.
    //Program the default GPIO Setting for chipset.
#if defined PROGRAM_DEFAULT_GPIO && PROGRAM_DEFAULT_GPIO == 1
	if (PchSeries == PchLp) {
		pTable = stSB_GPIODefaultULTInitTable;
	}else{
    	pTable = stSB_GPIODefaultInitTable;
	}
		
    ProgramGPIO( PeiServices, \
                 CpuIo, \
                 &SbSetupData, \
                 pTable);
#endif

    //Program the OEM GPIO Setting for board.
    Status = (*PeiServices)->LocatePpi( PeiServices, \
                                        &gAmiPeiSBCustomPpiGuid, \
                                        0, \
                                        NULL, \
                                        &SBPeiOemPpi );

    if (Status == EFI_SUCCESS) {
        if (SBPeiOemPpi->GpioInit != NULL) {
            pTable = SBPeiOemPpi->GpioInit->GpioTable;
            ProgramGPIO( PeiServices, \
                         CpuIo, \
                         &SbSetupData, \
                         pTable);
        }
    } else {
      SBPeiOemPpi = NULL;
    }

    // Program SB Devices' Subsystem Vendor ID & Subsystem ID
    ProgramSBSubId( PeiServices, PciCfg, SBPeiOemPpi );

    //Program PM Regs.
    InitPMRegs(PeiServices, CpuIo, &SbSetupData);
   
    // General power failure handling
    GeneralPowerFailureHandler(PeiServices, CpuIo, PciCfg);

    InitRTC( PeiServices, CpuIo );
  
    // Set what state (S0/S5) to go to when power is re-applied after a power failure (G3 state)
    SetTheStateToGoAfterG3(PeiServices, CpuIo, PciCfg, &SbSetupData);
 
    InitPCIe( PeiServices, CpuIo, PciCfg );

    InitSMBus( PeiServices, CpuIo, PciCfg );


    // Install the SB Init Policy PPI
    Status = (*PeiServices)->InstallPpi( PeiServices, &mPpiList[0] );
    ASSERT_PEI_ERROR( PeiServices, Status );

    // Setup a SBPEI entry after PEI permantent memory be installed

    Status = (*PeiServices)->NotifyPpi( PeiServices, AfterMrcNotifyList );

    Status = (*PeiServices)->NotifyPpi ( PeiServices, mMemoryReadyNotify );
    ASSERT_PEI_ERROR( PeiServices, Status );

#if defined iME_SUPPORT && iME_SUPPORT == 0
    if (!IsS3(PeiServices, CpuIo)) {
        WRITE_IO16_PM(ACPI_IOREG_PM1_CNTL, 0x20); // Clear Sleep Type
    }
#endif

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ProgramGPIO
//
// Description: This function initializes SB GPIOs
//
// Input:       PeiServices - Pointer to the PEI services table
//              CpuIo       - Pointer to the CPU I/O PPI
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID ProgramGPIO (
    IN EFI_PEI_SERVICES     **PeiServices,
    IN EFI_PEI_CPU_IO_PPI   *CpuIo,
    IN SB_SETUP_DATA        *SbSetupData,
    IN AMI_GPIO_INIT_TABLE_STRUCT *pTable
)
{
    UINT16 i;
    UINT32 OWN1;
    UINT32 OWN2;
    UINT32 OWN3;
    UINT32 GPN_CFG1[96];
    UINT32 GPN_CFG2[96];
    UINT32 USE1_SEL;
    UINT32 USE2_SEL;
    UINT32 USE3_SEL;
    UINT32 IO1_SEL;
    UINT32 IO2_SEL;
    UINT32 IO3_SEL;
    UINT32 LVL1_SEL;
    UINT32 LVL2_SEL;
    UINT32 LVL3_SEL;
    UINT32 INV1_SEL;
    UINT32 RST1_SEL;
    UINT32 RST2_SEL;
    UINT32 RST3_SEL;
    UINT32 BLNK_SEL;
    UINT32 INT1_SEL;
    UINT32 INT2_SEL;
    UINT32 INT3_SEL;
    UINT16 Offset;
    UINT16 LpcDeviceId;
    PCH_SERIES PchSeries = GetPchSeries();

    LpcDeviceId = READ_PCI16_SB(R_PCH_LPC_DEVICE_ID);

    if (pTable[0].GpioNo != 0xffff) {
        if (PchSeries == PchLp) {
          OWN1 = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_OWN1);   // 0x00
          OWN2 = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_OWN2);   // 0x04
          OWN3 = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_OWN3);   // 0x08
          INT1_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_INT_SEL1);   // 0x90
          INT2_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_INT_SEL2);   // 0x94
          INT3_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_INT_SEL3);   // 0x98
        } else {
          USE1_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_USE_SEL);     // 0x00
          IO1_SEL  = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_IO_SEL);      // 0x04
          LVL1_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_LVL);      // 0x0C
          INV1_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GPI_INV);     // 0x2C

          USE2_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_USE_SEL2);    // 0x30
          IO2_SEL  = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_IO_SEL2);     // 0x34
          LVL2_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_LVL2);     // 0x38

          USE3_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_USE_SEL3);    // 0x40
          IO3_SEL  = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_IO_SEL3);     // 0x44
          LVL3_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_LVL3);     // 0x48
        }
        BLNK_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GPO_BLINK);   // 0x18
        RST1_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_RST_SEL1); // 0x60
        RST2_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_RST_SEL2); // 0x64
        RST3_SEL = READ_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_RST_SEL3); // 0x68

        for (i = 0; pTable[i].GpioNo != 0xffff; i++) {

          Offset = pTable[i].GpioNo;
            if (PchSeries == PchLp) {
              GPN_CFG1[Offset] = READ_IO32(GPIO_BASE_ADDRESS + (GP_IOREG_GP_GPN_CFG1 + GP_GPIO_CONFIG_SIZE*Offset));  // 0x100 + n*8h
              GPN_CFG2[Offset] = READ_IO32(GPIO_BASE_ADDRESS + (GP_IOREG_GP_GPN_CFG2 + GP_GPIO_CONFIG_SIZE*Offset));  // 0x104 + n*8h

              GPN_CFG1[Offset] =  (GPN_CFG1[Offset] & ~(BIT31))	| (pTable[i].GpioCfg.Fileds.LVL << 31);
              GPN_CFG1[Offset] =  (GPN_CFG1[Offset] & ~(BIT4))	| (pTable[i].GpioCfg.Fileds.LEB << 4);
              GPN_CFG1[Offset] =  (GPN_CFG1[Offset] & ~(BIT3))	| (pTable[i].GpioCfg.Fileds.INV << 3);
              GPN_CFG1[Offset] =  (GPN_CFG1[Offset] & ~(BIT2))	| (pTable[i].GpioCfg.Fileds.IO << 2);
              GPN_CFG1[Offset] =  (GPN_CFG1[Offset] & ~(1))	| (pTable[i].GpioCfg.Fileds.USE);
              GPN_CFG2[Offset] =  (GPN_CFG2[Offset] & ~(BIT2))	| (pTable[i].GpioCfg.Fileds.DIS << 2);
			                                                                              //(EIP118667)>>
              if(pTable[i].GpioCfg.Fileds.IO == 0 && pTable[i].GpioCfg.Fileds.USE == 1){
                GPN_CFG2[Offset] =  (GPN_CFG2[Offset] & ~(BIT2))	| (BIT2);
              }
			                                                                              //(EIP118667)<<
              GPN_CFG2[Offset] =  (GPN_CFG2[Offset] & ~(3))	| (pTable[i].GpioCfg.Fileds.WP);	
            }

          if (Offset < 32) {
            if (PchSeries == PchLp) {
              OWN1 = (OWN1 & ~(1 << Offset)) | (pTable[i].GpioCfg.Fileds.OWN << Offset);
              INT1_SEL = (INT1_SEL & ~(1 << Offset)) | (pTable[i].GpioCfg.Fileds.INT << Offset);
            } else {
              USE1_SEL = (USE1_SEL & ~(1 << Offset)) | (pTable[i].GpioCfg.Fileds.USE << Offset);
              IO1_SEL  = (IO1_SEL  & ~(1 << Offset)) | (pTable[i].GpioCfg.Fileds.IO  << Offset);
              LVL1_SEL = (LVL1_SEL & ~(1 << Offset)) | (pTable[i].GpioCfg.Fileds.LVL << Offset);
              INV1_SEL = (INV1_SEL & ~(1 << Offset)) | (pTable[i].GpioCfg.Fileds.INV << Offset);
            }
            RST1_SEL = (RST1_SEL & ~(1 << Offset)) | (pTable[i].GpioCfg.Fileds.RST << Offset);
            BLNK_SEL = (BLNK_SEL & ~(1 << Offset)) | (pTable[i].GpioCfg.Fileds.BLK << Offset);
          } else if ((Offset >= 32) && (Offset < 64)) {
            if (PchSeries == PchLp) {
              OWN2 = (OWN2 & ~(1 << (Offset - 32))) | (pTable[i].GpioCfg.Fileds.OWN << (Offset - 32));
              INT2_SEL = (INT2_SEL & ~(1 << (Offset - 32))) | (pTable[i].GpioCfg.Fileds.INT << (Offset - 32));
            } else {
              USE2_SEL = (USE2_SEL & ~(1 << (Offset - 32))) | (pTable[i].GpioCfg.Fileds.USE << (Offset - 32));
              IO2_SEL  = (IO2_SEL  & ~(1 << (Offset - 32))) | (pTable[i].GpioCfg.Fileds.IO  << (Offset - 32));
              LVL2_SEL = (LVL2_SEL & ~(1 << (Offset - 32))) | (pTable[i].GpioCfg.Fileds.LVL << (Offset - 32));
            }
            RST2_SEL = (RST2_SEL & ~(1 << (Offset - 32))) | (pTable[i].GpioCfg.Fileds.RST << (Offset - 32));
          } else {
            if (PchSeries == PchLp) {
              OWN3 = (OWN3 & ~(1 << (Offset - 64))) | (pTable[i].GpioCfg.Fileds.OWN << (Offset - 64));
              INT3_SEL = (INT3_SEL & ~(1 << (Offset - 64))) | (pTable[i].GpioCfg.Fileds.INT << (Offset - 64));
            } else {
              USE3_SEL = (USE3_SEL & ~(1 << (Offset - 64))) | (pTable[i].GpioCfg.Fileds.USE << (Offset - 64));
              IO3_SEL  = (IO3_SEL  & ~(1 << (Offset - 64))) | (pTable[i].GpioCfg.Fileds.IO  << (Offset - 64));
              LVL3_SEL = (LVL3_SEL & ~(1 << (Offset - 64))) | (pTable[i].GpioCfg.Fileds.LVL << (Offset - 64));
            }
            RST3_SEL = (RST3_SEL & ~(1 << (Offset - 64))) | (pTable[i].GpioCfg.Fileds.RST << (Offset - 64));
          }
            if (PchSeries == PchLp) {
              WRITE_IO32(GPIO_BASE_ADDRESS + (GP_IOREG_GP_GPN_CFG1 + GP_GPIO_CONFIG_SIZE*Offset), GPN_CFG1[Offset]);   // 0x100 + n*8h
              WRITE_IO32(GPIO_BASE_ADDRESS + (GP_IOREG_GP_GPN_CFG2 + GP_GPIO_CONFIG_SIZE*Offset), GPN_CFG2[Offset]);   // 0x104 + n*8h
            }
        }

        if (PchSeries == PchLp) {
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_OWN1, OWN1);   // 0x00
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_OWN2, OWN2);   // 0x04
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_OWN3, OWN3);   // 0x08
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_INT_SEL1, INT1_SEL);   // 0x90
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_INT_SEL2, INT2_SEL);   // 0x94
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_INT_SEL3, INT3_SEL);   // 0x98
          WRITE_IO32(GPIO_BASE_ADDRESS + GPI_IRQ_2_IOAPIC, GPI_IRQ_2_IOXAPIC);   // 0x10
        } else {
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_USE_SEL,     USE1_SEL);   // 0x00
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_IO_SEL,      IO1_SEL);    // 0x04
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_LVL,      LVL1_SEL);   // 0x0C

          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GPI_INV,     INV1_SEL);   // 0x2C

          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_USE_SEL2,    USE2_SEL);   // 0x30
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_IO_SEL2,     IO2_SEL);    // 0x34
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_LVL2,     LVL2_SEL);   // 0x38

          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_USE_SEL3,    USE3_SEL);   // 0x40
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_IO_SEL3,     IO3_SEL);    // 0x44
          WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_LVL3,     LVL3_SEL);   // 0x48
        }
        WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GPO_BLINK,   BLNK_SEL);   // 0x18
        WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_RST_SEL1, RST1_SEL);   // 0x60
        WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_RST_SEL2, RST2_SEL);   // 0x64
        WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_GP_RST_SEL3, RST3_SEL);   // 0x68
    }

    // BIOS implementation for SUS_PWR_DN_ACK
    // As soon as platform BARs are initialized, BIOS must ensure that the following things
    // are set high on all boot flows:
    // (1) GPIO_BASE_ADDRESS + 0x60[30]
    // (2) GPIO[30] pin (GPIO_BASE_ADDRESS + GP_LVL) (Done in GPIO.SDL)
    if (IS_PCH_LPT_LPC_DEVICE_ID_MOBILE (LpcDeviceId)) {
       SET_IO32 (GPIO_BASE_ADDRESS + GP_IOREG_GP_RST_SEL1, BIT30); // 0x60
    }
    //
    // Now enable LANPHY_PC GPIO if LAN is enabled in the setup.
    // Enabling this GPIO for all the boards. Both Red Fort and Buffalo Trail uses GPIO12 for this.
    // Electric Peak does not use GPIO12, so changing the value for all boards should not effect
    if (SbSetupData->PchLan) {
      if (PchSeries == PchLp) {
        SET_IO32 (GPIO_BASE_ADDRESS + (GP_IOREG_GP_GPN_CFG1 + GP_GPIO_CONFIG_SIZE*22), BIT31); // 0x190
      } else {
        SET_IO32 (GPIO_BASE_ADDRESS + GP_IOREG_GP_LVL, BIT12); // 0x0C
      }
    }

    // Clear GPI Status 
    if (PchSeries == PchLp) {
      WRITE_IO16_PM(ACPI_PCHLP_IOREG_GPE0_STS, 0xffff); // 0x80
      WRITE_IO16_PM(ACPI_PCHLP_IOREG_GPE0_STS + 4, 0xffff); // 0x84
      WRITE_IO16_PM(ACPI_PCHLP_IOREG_GPE0_STS + 8, 0xffff); // 0x88
    } else {
      WRITE_IO16_PM(ACPI_IOREG_GPE0_STS + 2, 0xffff); // 0x22
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   IsSBDevice
//
// Description: This function detimines SB PCI devices
//
// Input:       UINT64 PciAddress
//              UINT8  *PciSidReg
//
// Output:      EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS IsSBDevice(
    IN UINT64    PciAddress,
    IN OUT UINT8 *PciSidReg
)
{
    UINT8  i;
    AMI_SB_PCI_DEVICES_TABLE_STRUCT PchDeviceTable[] = { HECI_BUS_DEV_FUN,     ME_REG_SVID,
                                                         HECI2_BUS_DEV_FUN,    ME_REG_SVID,
                                                         IDER_BUS_DEV_FUN,     ME_REG_SVID,
                                                         KT_BUS_DEV_FUN,       ME_REG_SVID,
                                                         XHCI_BUS_DEV_FUN,     XHCI_REG_SVID,
                                                         LAN_BUS_DEV_FUN,      LAN_REG_SVID,
                                                         EHCI2_BUS_DEV_FUN,    EHCI_REG_SVID,
                                                         HDA_BUS_DEV_FUN,      HDA_REG_SVID,
                                                         PCIEBRS_BUS_DEV_FUN,  PCIEBRS_REG_SVID,
                                                         PCIEBRS2_BUS_DEV_FUN, PCIEBRS_REG_SVID,
                                                         PCIEBRS3_BUS_DEV_FUN, PCIEBRS_REG_SVID,
                                                         PCIEBRS4_BUS_DEV_FUN, PCIEBRS_REG_SVID,
                                                         PCIEBRS5_BUS_DEV_FUN, PCIEBRS_REG_SVID,
                                                         PCIEBRS6_BUS_DEV_FUN, PCIEBRS_REG_SVID,
                                                         PCIEBRS7_BUS_DEV_FUN, PCIEBRS_REG_SVID,
                                                         PCIEBRS8_BUS_DEV_FUN, PCIEBRS_REG_SVID,
                                                         PCIBR_BUS_DEV_FUN,    PCIBR_REG_SVID,
                                                         EHCI_BUS_DEV_FUN,     EHCI_REG_SVID,
                                                         SB_BUS_DEV_FUN,       R_PCH_LPC_SS,
                                                         SATA_BUS_DEV_FUN,     SATA_REG_SVID,
                                                         SMBUS_BUS_DEV_FUN,    SMBUS_REG_SVID,
                                                         SATA2_BUS_DEV_FUN,    SATA_REG_SVID,
                                                         THERMAL_BUS_DEV_FUN,  R_PCH_LPC_SS
                                                       };
    UINT32 TableSize = sizeof(PchDeviceTable) / sizeof(AMI_SB_PCI_DEVICES_TABLE_STRUCT);

    for (i = 0; i < TableSize; i++) {

      if (PciAddress != PchDeviceTable[i].PciAddr) {
        continue;
      } else {
        if (READ_PCI32((UINT8)(Shr64(PchDeviceTable[i].PciAddr, 24) & 0xff), \
                       (UINT8)(Shr64(PchDeviceTable[i].PciAddr, 16) & 0xff), \
                       (UINT8)(Shr64(PchDeviceTable[i].PciAddr, 8) & 0xff), \
                        0) == 0xffffffff) 
           return EFI_UNSUPPORTED;

        *PciSidReg = PchDeviceTable[i].PciSidReg;
        return EFI_SUCCESS;
      }
    }
    return EFI_UNSUPPORTED;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ProgramSBSubId
//
// Description: This function programs SB PCI devices sub-vendor ID and
//              sub-system ID.
//
// Input:       PeiServices - Pointer to the PEI services table
//              PciCfg      - Pointer to the PCI Configuration PPI
//
// Output:      VOID
//
// Notes:       1. This routine only programs the PCI device in SB, hence, we
//                 have to check the bus/device/function numbers whether they
//                 are a SB PCI device or not.
//              2. This routine is invoked by PEI phase.(After PEI permantent
//                 memory be installed)
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID ProgramSBSubId (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg,
    IN AMI_PEI_SB_CUSTOM_PPI    *SBPeiOemPpi
)
{
    EFI_STATUS                  Status = EFI_SUCCESS;
    UINTN                       i = 0;
    UINT32                      PciSid = 0xffffffff;
    UINT8                       PciSidReg = 0xff;
    AMI_SB_PCI_SSID_TABLE_STRUCT DefaultSIdTbl[] = {SB_PCI_DEVICES_SSID_TABLE};
    AMI_SB_PCI_SSID_TABLE_STRUCT *SsidTblPtr = DefaultSIdTbl;

    if ((SBPeiOemPpi != NULL) && (SBPeiOemPpi->SsidTable != NULL))
      SsidTblPtr = SBPeiOemPpi->SsidTable;

    while (SsidTblPtr[i].PciAddr != 0xffffffffffffffff) {
        if (IsSBDevice(SsidTblPtr[i].PciAddr, &PciSidReg) == EFI_SUCCESS) {
            if (SsidTblPtr[i].Sid == 0xffffffff) {
                Status = PciCfg->Read( PeiServices,
                                       PciCfg,
                                       EfiPeiPciCfgWidthUint32,
                                       SsidTblPtr[i].PciAddr,
                                       &PciSid);
            } else {
                PciSid = SsidTblPtr[i].Sid;
            }

            if (SsidTblPtr[i].PciAddr == EHCI_BUS_DEV_FUN)
              SET_PCI8_EHCI(R_PCH_EHCI_ACCESS_CNTL, 1);
            else if (SsidTblPtr[i].PciAddr == EHCI2_BUS_DEV_FUN)
              SET_PCI8_EHCI2(R_PCH_EHCI_ACCESS_CNTL, 1);

            Status = PciCfg->Write( PeiServices,
                                    PciCfg,
                                    EfiPeiPciCfgWidthUint32,
                                    SsidTblPtr[i].PciAddr | PciSidReg,
                                    &PciSid);

            if (SsidTblPtr[i].PciAddr == EHCI_BUS_DEV_FUN)
              RESET_PCI8_EHCI(R_PCH_EHCI_ACCESS_CNTL, 1);
            else if (SsidTblPtr[i].PciAddr == EHCI2_BUS_DEV_FUN)
              RESET_PCI8_EHCI2(R_PCH_EHCI_ACCESS_CNTL, 1);
        }
        i++;
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   InitPCIe
//
// Description: This function initializes SB PCI Express controller(s)
//
// Input:       PeiServices - Pointer to the PEI services table
//              CpuIo       - Pointer to the CPU I/O PPI
//              PciCfg      - Pointer to the PCI Configuration PPI
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID InitPCIe (
    IN EFI_PEI_SERVICES     **PeiServices,
    IN EFI_PEI_CPU_IO_PPI   *CpuIo,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg
)
{
    // TODO
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   InitSMBus
//
// Description: This function initializes SB SMBUS controller(s)
//
// Input:       PeiServices - Pointer to the PEI services table
//              CpuIo       - Pointer to the CPU I/O PPI
//              PciCfg      - Pointer to the PCI Configuration PPI
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID InitSMBus (
    IN EFI_PEI_SERVICES     **PeiServices,
    IN EFI_PEI_CPU_IO_PPI   *CpuIo,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg
)
{
    // TODO
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   InitUsbMisc
//
// Description: Miscellaneous USB initialization.
//
// Input:       PeiServices - Pointer to the PEI services table
//              CpuIo       - Pointer to the CPU I/O PPI
//              PciCfg      - Pointer to the PCI Configuration PPI
//              BootMode    - Boot Mode
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID InitUsbMisc (
    IN EFI_PEI_SERVICES     **PeiServices,
    IN EFI_PEI_CPU_IO_PPI   *CpuIo,
    IN EFI_PEI_PCI_CFG2_PPI *PciCfg,
    IN EFI_BOOT_MODE        BootMode
)
{
  PCH_SERIES PchSeries = GetPchSeries();
#ifndef PEI_XHCI_MMIOBASE
  UINT32      XhciMmioBase = 0xFE400000;                           //[EIP156783]
#else
  UINT32      XhciMmioBase = PEI_XHCI_MMIOBASE;                    //[EIP115528] >>
#endif
  UINT8       XhciCapLength;
  UINT8       XhciMaxPorts;
  UINT32      XhciPort;
  EFI_PEI_STALL_PPI   *StallPpi;
  StallPpi = &mStallPpi;

if ((BootMode == BOOT_IN_RECOVERY_MODE) || (BootMode == BOOT_ON_FLASH_UPDATE)) {       
    WRITE_PCI32( DEFAULT_PCI_BUS_NUMBER_PCH, \
                 PCI_DEVICE_NUMBER_PCH_XHCI, \
                 PCI_FUNCTION_NUMBER_PCH_XHCI, \
                 0x10, \
                 XhciMmioBase);
    WRITE_PCI8( DEFAULT_PCI_BUS_NUMBER_PCH, \
                 PCI_DEVICE_NUMBER_PCH_XHCI, \
                 PCI_FUNCTION_NUMBER_PCH_XHCI, \
                 0x04, \
                 0x06);    
    WRITE_PCI8( DEFAULT_PCI_BUS_NUMBER_PCH, \
                 PCI_DEVICE_NUMBER_PCH_XHCI, \
                 PCI_FUNCTION_NUMBER_PCH_XHCI, \
                 R_PCH_XHCI_USB3PR, \
                 0x3F);
    
    //Add some delay to wait that device links are stable
    StallPpi->Stall(PeiServices, StallPpi, 500 * 1000); 
    
    XhciCapLength = READ_MEM8(XhciMmioBase);
    if (PchSeries == PchH) {
        switch ((READ_MEM8(XhciMmioBase + R_PCH_XHCI_FUS)) & B_PCH_XHCI_FUS_SSPRTCNT) {
            case V_PCH_XHCI_FUS_SSPRTCNT_11B:
                XhciMaxPorts = 0x0F;
                break;

            case V_PCH_XHCI_FUS_SSPRTCNT_10B:
                XhciMaxPorts = 0x11;
                break;

            case V_PCH_XHCI_FUS_SSPRTCNT_01B:
                XhciMaxPorts = 0x13;
                break;

            case V_PCH_XHCI_FUS_SSPRTCNT_00B:
            default:
                XhciMaxPorts = 0x15;
                break;
        }
    } else {
        XhciMaxPorts = READ_MEM8(XhciMmioBase + 0x7);
    }
    //Clear each xHCI port power
    for (XhciPort = 0; XhciPort < XhciMaxPorts; XhciPort++) {
      RESET_MEM32(XhciMmioBase + XhciCapLength + 0x400 + (0x10 * XhciPort), BIT9);
    }
    // Set xHCI D0h & D8h as power-on default value.
    WRITE_PCI16( DEFAULT_PCI_BUS_NUMBER_PCH, \
                 PCI_DEVICE_NUMBER_PCH_XHCI, \
                 PCI_FUNCTION_NUMBER_PCH_XHCI, \
                 R_PCH_XHCI_USB2PR, \
                 0 );
    WRITE_PCI8( DEFAULT_PCI_BUS_NUMBER_PCH, \
                 PCI_DEVICE_NUMBER_PCH_XHCI, \
                 PCI_FUNCTION_NUMBER_PCH_XHCI, \
                 R_PCH_XHCI_USB3PR, \
                 0 );
    WRITE_PCI8( DEFAULT_PCI_BUS_NUMBER_PCH, \
                 PCI_DEVICE_NUMBER_PCH_XHCI, \
                 PCI_FUNCTION_NUMBER_PCH_XHCI, \
                 0x04, \
                 0);
    WRITE_PCI32( DEFAULT_PCI_BUS_NUMBER_PCH, \
                 PCI_DEVICE_NUMBER_PCH_XHCI, \
                 PCI_FUNCTION_NUMBER_PCH_XHCI, \
                 0x10, \
                 0);
  }


                                        // [EIP107424] [EIP123117]>
  if (BootMode != BOOT_ON_S3_RESUME) { 
    //
    // Clear unexpected USB EHCI Legacy Support Extended status.
    // Set B0:D1A/1D:F0 Reg#6Ch[31:29] = '111b'.
    //
    WRITE_PCI32_EHCI  ( R_PCH_EHCI_LEGEXT_CS, \
                      B_PCH_EHCI_LEGEXT_CS_SMIBAR | \
                      B_PCH_EHCI_LEGEXT_CS_SMIPCI | \
                      B_PCH_EHCI_LEGEXT_CS_SMIOS);
    if (PchSeries == PchH) {
      WRITE_PCI32_EHCI ( R_PCH_EHCI_LEGEXT_CS, \
                        B_PCH_EHCI_LEGEXT_CS_SMIBAR | \
                        B_PCH_EHCI_LEGEXT_CS_SMIPCI | \
                        B_PCH_EHCI_LEGEXT_CS_SMIOS);
    }                                    
  }
                                        // <[EIP107424] [EIP123117]    
										//[EIP115528]<<
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ProgramSBRegAfterMemInstalled
//
// Description: This function can be used to program any SB regisater after
//              PEI permantent memory be installed.
//
// Input:       FfsHeader        - Pointer to the desired FFS header.
//              PeiServices      - Pointer to the PEI services table.
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS ProgramSBRegAfterMemInstalled (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
    IN VOID                     *InvokePpi )
{
    EFI_STATUS                  Status = EFI_SUCCESS;
    EFI_PEI_CPU_IO_PPI          *CpuIo;
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg;
    EFI_BOOT_MODE               BootMode;
    EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadOnlyVariable;
    UINTN                       VariableSize;
    UINT32                      SbAslBufVarPtr;
    EFI_GUID                    SbAslBufPtrGuid = SB_ASL_BUFFER_PTR_GUID;
    CHAR16                      SbAslBufPtrVar[] = SB_ASL_BUFFER_PTR_VARIABLE;

    CpuIo = (*PeiServices)->CpuIo;
    PciCfg = (*PeiServices)->PciCfg;

    PEI_PROGRESS_CODE (PeiServices, PEI_MEM_SB_INIT);

    Status = (*PeiServices)->GetBootMode (PeiServices, &BootMode);

    InitUsbMisc( PeiServices, CpuIo, PciCfg, BootMode );

#if (ACPI_SUPPORT)
    if (BootMode == BOOT_ON_S3_RESUME) {
        Status = (*PeiServices)->LocatePpi( PeiServices, \
                                            &gEfiPeiReadOnlyVariable2PpiGuid, \
                                            0, \
                                            NULL, \
                                            &ReadOnlyVariable );
        ASSERT_PEI_ERROR( PeiServices, Status );
        VariableSize = sizeof(SbAslBufVarPtr);
        Status = ReadOnlyVariable->GetVariable( ReadOnlyVariable, \
                                                SbAslBufPtrVar, \
                                                &SbAslBufPtrGuid, \
                                                NULL, \
                                                &VariableSize, \
                                                &SbAslBufVarPtr );
        if (!EFI_ERROR(Status)) {
            // Update ACPI RTC status
            RESET_MEM8(SbAslBufVarPtr, 1); // Clear ACPI RTC status
            if (READ_IO16_PM(ACPI_IOREG_PM1_STS) & 0x400)
                SET_MEM8(SbAslBufVarPtr, 1); // Set ACPI RTC status
        }
#if defined BootScriptHide_SUPPORT && BootScriptHide_SUPPORT
        //SCI_EN = 1
        SET_IO8_PM(ACPI_IOREG_PM1_CNTL, B_PCH_ACPI_PM1_CNT_SCI_EN); //PM1_CNT
#endif
    }
#endif
    // Clear Global Reset Bit
    RESET_PCI32_SB(SB_REG_LPC_PMIR, B_ICH_LPC_PMIR_CF9GR);

    // Set up necessary PPI notifications after PEI permantent memory
    // be installed
    Status = (*PeiServices)->NotifyPpi( PeiServices, &mNotifyList[0] );
    ASSERT_PEI_ERROR ( PeiServices, Status );

#if defined RapidStart_SUPPORT && RapidStart_SUPPORT
    if (BootMode == BOOT_ON_S4_RESUME)
        if (RtcRead(FFS_NV_FLAG_REG) & BIT0)
          return EFI_SUCCESS;
#endif

    return  EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ProgramSBRegBeforeEndofPei
//
// Description: This function can be used to program any SB regisater before
//              end of PEI phase.
//
// Input:       PeiServices      - Pointer to the PEI services table
//              NotifyDescriptor - Pointer to the descriptor for the
//                                 notification event.
//              InvokePpi        - Pointer to the PPI that was installed
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS ProgramSBRegBeforeEndofPei (
    IN EFI_PEI_SERVICES             **PeiServices,
    IN EFI_PEI_NOTIFY_DESCRIPTOR    *NotifyDescriptor,
    IN VOID                         *InvokePpi )
{
    EFI_STATUS                  Status = EFI_SUCCESS;
    EFI_PEI_CPU_IO_PPI          *CpuIo;
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg;
    EFI_BOOT_MODE               BootMode;

    CpuIo = (*PeiServices)->CpuIo;
    PciCfg = (*PeiServices)->PciCfg;

    Status = (*PeiServices)->GetBootMode (PeiServices, &BootMode);

    if (BootMode == BOOT_ON_S3_RESUME) {
                                        // [EIP87695]>
#if SYSTEM_REBOOT_NORMALLY_IF_S3_IS_FAILED
    WRITE_IO16_PM(ACPI_IOREG_PM1_CNTL, READ_IO16_PM(ACPI_IOREG_PM1_CNTL) & 0xe3ff | (S3_SLP_TYP << 10)); // Clear S3 for avoiding S3 resume twice
#endif
                                        // <[EIP87695]
        // Porting if needed.
    } else {
        // Porting if needed.
    }

    // Setting 8254
    // program timer 1 as refresh timer
    IoWrite8(LEGACY_TIMER_CTRL,0x54);
    IoWrite8(LEGACY_TIMER_1_COUNT,0x12);

#if defined(SUPPORT_RAID_DRIVER) && SUPPORT_RAID_DRIVER && (PTT_VER > 15)
    DetectSataPortInfo(PeiServices);
#endif

    return  EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   IsRtcUipAlwaysSet
//
// Description: Check RTC Time Update In Progress.
//
// Input:       PeiServices - Pointer to the PEI services table
//
// Output:      Boolean
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN IsRtcUipAlwaysSet(
  IN EFI_PEI_SERVICES       **PeiServices
  )
{

  EFI_PEI_STALL_PPI   *StallPpi;
  UINTN               Count;

  StallPpi = &mStallPpi;

  for (Count = 0; Count < 500; Count++) {   // Maximum waiting approximates to 1.5 seconds (= 3 msec * 500) 
    if ((READ_IO8_RTC(RTC_NMI_MASK | RTC_REG_A_INDEX) & BIT07) == 0) {
      return FALSE;
    }
    StallPpi->Stall (PeiServices, StallPpi, 3000); 
  }

  return TRUE;  
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   InitRTC
//
// Description: This function initializes SB RTC related registers
//
// Input:       PeiServices - Pointer to the PEI services table
//              CpuIo       - Pointer to the CPU I/O PPI
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID InitRTC (
    IN EFI_PEI_SERVICES     **PeiServices,
    IN EFI_PEI_CPU_IO_PPI   *CpuIo )
{
    UINT8        Buffer8;
    UINT16       Buffer16;
    BOOLEAN      RtcUipIsAlwaysSet;

    //
    // PCH BIOS Specification 0.6.0 - Section 19.2, "Power Failure Consideration"
    // 
    // When the RTC_PWR_STS bit is set, it indicates that the RTCRST# signal went low.
    // Software should clear this bit. For example, changing the RTC battery sets this bit.
    // System BIOS should reset CMOS to default values if the RTC_PWR_STS is set.
    //
    // The System BIOS should execute the sequence below if the RTC_PWR_STS bit is set before memory initialization.
    // This will ensure that the RTC state machine has been initialized.
    //  1.    If the RTC_PWR_STS bit is set, which indicates a new coin-cell batttery
    //        insertion or a battery failure, steps 2 through 5 should be executed.
    //  2.    Set RTC Register 0Ah[6:4] to '110b' or '111b'.
    //  3.    Set RTC Register 0Bh[7].
    //  4.    Set RTC Register 0Ah[6:4] to '010b'.
    //  5.    Clear RTC Register 0Bh[7].
    Buffer16 = READ_PCI16_SB(R_PCH_LPC_GEN_PMCON_3); // 0xA4 
    RtcUipIsAlwaysSet = IsRtcUipAlwaysSet(PeiServices);
    if ((Buffer16 & B_PCH_LPC_GEN_PMCON_RTC_PWR_STS) || RtcUipIsAlwaysSet) { 

        //
        // Step 1.
        // BIOS clears this bit by writing a '0' to it.
        //
        if (Buffer16 & B_PCH_LPC_GEN_PMCON_RTC_PWR_STS) {
          Buffer16 &= ~B_PCH_LPC_GEN_PMCON_RTC_PWR_STS;
          WRITE_PCI16_SB(R_PCH_LPC_GEN_PMCON_3, Buffer16);
          WRITE_IO8_RTC((RTC_NMI_MASK | RTC_DAY_OF_MONTH_REG), 0xFF);
          WRITE_IO8_RTC((RTC_NMI_MASK | RTC_HOURS_REG), 0xFF);
        }

        //      
        // Step 2.
        // Set RTC Register 0Ah[6:4] to '110' or '111'.
        //
        WRITE_IO8_RTC((RTC_NMI_MASK | RTC_REG_A_INDEX), 0x66);

        //      
        // Step 3.
        // Set RTC Register 0Bh[7].
        //
        Buffer8 = (READ_IO8_RTC(RTC_NMI_MASK | RTC_REG_B_INDEX) | 0x80);
        WRITE_IO8_RTC((RTC_NMI_MASK | RTC_REG_B_INDEX), Buffer8);
  
        //      
        // Step 4.
        // Set RTC Register 0Ah[6:4] to '010'.
        //
        WRITE_IO8_RTC((RTC_NMI_MASK | RTC_REG_A_INDEX), 0x26);

        //      
        // Step 5.
        // Clear RTC Register 0Bh[7].
        //
        Buffer8 = (READ_IO8_RTC(RTC_NMI_MASK | RTC_REG_B_INDEX) & ~0x80);
        WRITE_IO8_RTC((RTC_NMI_MASK | RTC_REG_B_INDEX), Buffer8);
    }

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   InitPMRegs
//
// Description: This function initializes SB Power Management registers.
//
// Input:       PeiServices - Pointer to the PEI services table
//              CpuIo       - Pointer to the CPU I/O PPI
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID InitPMRegs (
    IN EFI_PEI_SERVICES     **PeiServices,
    IN EFI_PEI_CPU_IO_PPI   *CpuIo,
    IN SB_SETUP_DATA        *SbSetupData)
{
  EFI_STATUS     Status = EFI_SUCCESS;
  EFI_BOOT_MODE  BootMode = 0;
  PCH_SERIES     PchSeries = GetPchSeries();

  Status = (*PeiServices)->GetBootMode (PeiServices, &BootMode);
  if (BootMode != BOOT_ON_S3_RESUME) {
    WRITE_IO16_PM(ACPI_IOREG_PM1_EN, 0);
  }
  if (PchSeries == PchLp) {
    WRITE_IO32_PM(ACPI_PCHLP_IOREG_GPE0_EN+0x0c, 0);
  } else {
    WRITE_IO32_PM(ACPI_IOREG_GPE0_EN, 0);
    WRITE_IO32_PM(ACPI_IOREG_GPE0_EN + 4, 0);
  }

  // Clear Alternate GPI SMI Status Reg.
  if (PchSeries == PchLp) {
    WRITE_IO32(GPIO_BASE_ADDRESS + GP_IOREG_ALTGP_SMI_STS, 0xFFFFFFFF);
  } else {
    WRITE_IO16_PM(ACPI_IOREG_ALTGP_SMI_STS, 0xFFFF);
  }

#if EHCI_CON_DISCON_WAKE_UP_SUPPORT
    if (SbSetupData->EhciConDisConWakeUp)
      RESET_MEM8_RCRB(RCRB_MMIO_RMHWKCTL , 0xFF);
    else
      SET_MEM8_RCRB(RCRB_MMIO_RMHWKCTL , (BIT00 | BIT01 | BIT04 | BIT05));
#endif

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ProgramRCRBMmio
//
// Description: This function initializes SB Root Complex registers
//
// Input:       PeiServices - Pointer to the PEI services table
//              CpuIo       - Pointer to the CPU I/O PPI
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID ProgramRCRBMmio (
    IN EFI_PEI_SERVICES     **PeiServices,
    IN EFI_PEI_CPU_IO_PPI   *CpuIo )
{
  PCH_SERIES              PchSeries = GetPchSeries();

  //Enable IOAPIC Decoding and FERR#
  if(PchSeries == PchLp){
    SET_MEM16_RCRB(R_PCH_RCRB_OIC, B_PCH_RCRB_OIC_AEN);  
  } else {
    SET_MEM16_RCRB(R_PCH_RCRB_OIC, (B_PCH_RCRB_OIC_AEN | (PCH_RCRB_OIC_CEN << 9)));
  }

  // Enable No Reboot, Boot BIOS Destination
  SET_MEM32_RCRB(R_PCH_RCRB_GCS, (BIT06 | B_PCH_RCRB_GCS_NR));

  SET_MEM32_RCRB (R_PCH_RCRB_FD2, BIT00); //0x3428

  // PIRQ routing Info
  // Device 31 Interrupt Pin, reg#3100h
  WRITE_MEM32_RCRB(R_PCH_RCRB_D31IP, (RCRB_IRQB << 8)  +
                                     (RCRB_IRQC << 12) +
                                     (RCRB_IRQD << 16) +
                                     (RCRB_IRQB << 20) +
                                     (RCRB_IRQC << 24));

  // Device 30 Interrupt Pin, reg#3104h - Read Only

  // Device 29 Interrupt Pin, reg#3108h
  WRITE_MEM32_RCRB(R_PCH_RCRB_D29IP, (RCRB_IRQA << 0) +
                                     (RCRB_IRQB << 4) +
                                     (RCRB_IRQC << 8) +
                                     (RCRB_IRQD << 12) +
                                     (RCRB_IRQA << 16));

  // Device 28 Interrupt Pin, reg#310Ch
  WRITE_MEM32_RCRB(R_PCH_RCRB_D28IP, (RCRB_IRQA << 0) +
                                     (RCRB_IRQB << 4) +
                                     (RCRB_IRQC << 8) +
                                     (RCRB_IRQD << 12) +
                                     (RCRB_IRQA << 16) +
                                     (RCRB_IRQB << 20) +
                                     (RCRB_IRQC << 24) +
                                     (RCRB_IRQD << 28));

  // Device 27 Interrupt Pin, reg#3110h
  WRITE_MEM32_RCRB(R_PCH_RCRB_D27IP, (RCRB_IRQA << 0));

  // Device 26 Interrupt Pin, reg#3114h
  WRITE_MEM32_RCRB(R_PCH_RCRB_D26IP, (RCRB_IRQA << 0) +
                                     (RCRB_IRQB << 4) +
                                     (RCRB_IRQC << 8) +
                                     (RCRB_IRQD << 12));

  // Device 25 Interrupt Pin, reg#3118h
  WRITE_MEM32_RCRB(R_PCH_RCRB_D25IP, (RCRB_IRQA << 0));

  // Device 22 Interrupt Pin, reg#3124h
  WRITE_MEM16_RCRB(R_PCH_RCRB_D22IP, (RCRB_IRQA << 0) +
                                     (RCRB_IRQB << 4) +
                                     (RCRB_IRQC << 8) +
                                     (RCRB_IRQB << 12));

  // Device 20 Interrupt Pin, reg#3128h
  WRITE_MEM32_RCRB(R_PCH_RCRB_D20IP, (RCRB_IRQA << 0));

  // Device 31 Interrupt Route, reg#3140h
  WRITE_MEM16_RCRB(R_PCH_RCRB_D31IR, (RCRB_PIRQD << 4) +
                                     (RCRB_PIRQC << 8) +
                                     (RCRB_PIRQA << 12));

  // Device 29 Interrupt Route, reg#3144h
  WRITE_MEM16_RCRB(R_PCH_RCRB_D29IR, (RCRB_PIRQH << 0) +
                                     (RCRB_PIRQD << 4) +
                                     (RCRB_PIRQA << 8) +
                                     (RCRB_PIRQC << 12));

  // Device 28 Interrupt Route, reg#3146h
  WRITE_MEM16_RCRB(R_PCH_RCRB_D28IR, (RCRB_PIRQA << 0) +
                                     (RCRB_PIRQB << 4) +
                                     (RCRB_PIRQC << 8) +
                                     (RCRB_PIRQD << 12));

  // Device 27 Interrupt Route, reg#3148h
  WRITE_MEM16_RCRB(R_PCH_RCRB_D27IR, (RCRB_PIRQG << 0) +
                                     (RCRB_PIRQB << 4) +
                                     (RCRB_PIRQC << 8) +
                                     (RCRB_PIRQD << 12));

  // Device 26 Interrupt Route, reg#314Ch
  WRITE_MEM16_RCRB(R_PCH_RCRB_D26IR, (RCRB_PIRQA << 0) +
                                     (RCRB_PIRQF << 4) +
                                     (RCRB_PIRQC << 8) +
                                     (RCRB_PIRQD << 12));

  // Device 25 Interrupt Route, reg#3150h
  WRITE_MEM16_RCRB(R_PCH_RCRB_D25IR, (RCRB_PIRQE << 0) +
                                     (RCRB_PIRQF << 4) +
                                     (RCRB_PIRQG << 8) +
                                     (RCRB_PIRQH << 12));

  if (PchSeries == PchLp) {
  // Device 23 Interrupt Route, reg#3158h
  	WRITE_MEM16_RCRB(R_PCH_RCRB_D23IR, (RCRB_PIRQG << 0));
  }

  // Device 22 Interrupt Route, reg#315Ch
  WRITE_MEM16_RCRB(R_PCH_RCRB_D22IR, (RCRB_PIRQA << 0) +
                                     (RCRB_PIRQD << 4) +
                                     (RCRB_PIRQC << 8) +
                                     (RCRB_PIRQB << 12));

  if (PchSeries == PchLp) {
  // Device 21 Interrupt Route, reg#3164h
  	WRITE_MEM16_RCRB(R_PCH_RCRB_D21IR, (RCRB_PIRQE << 0) +
                                       (RCRB_PIRQE << 4) +
                                       (RCRB_PIRQF << 8) +
                                       (RCRB_PIRQF << 12));
  }

  // Device 20 Interrupt Route, reg#3160h
  WRITE_MEM16_RCRB(R_PCH_RCRB_D20IR, (RCRB_PIRQA << 0) +
                                     (RCRB_PIRQB << 4) +
                                     (RCRB_PIRQC << 8) +
                                     (RCRB_PIRQD << 12));

  if (PchSeries == PchLp) {
  // Device 19 Interrupt Route, reg#3168h
  	WRITE_MEM16_RCRB(R_PCH_RCRB_D19IR, (RCRB_PIRQH << 0));
  }

  // EIP176923
  #if defined DISABLE_DAYLIGHT_SAVINGS && DISABLE_DAYLIGHT_SAVINGS == 1
    SET_MEM16_RCRB(R_PCH_RCRB_BUC, B_PCH_RCRB_BUC_SDO);
  #endif
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ProgramSBIoDecodeRegs
//
// Description: This function initializes SB IO Decide Registers.
//
// Input:       PeiServices - Pointer to the PEI services table
//              PciCfg      - Pointer to the PCI Configuration PPI
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
ProgramSBIoDecodeRegs (
  IN EFI_PEI_SERVICES       **PeiServices,
  IN EFI_PEI_PCI_CFG2_PPI   *PciCfg
)
{

#if defined EMUL6064_SUPPORT && EMUL6064_SUPPORT == 1
    // Force KBC_LPC_EN bit (B0:D31:F0 Reg 82h[10]) = 1 if EMUL6064_SUPPORT = 1.
    SbLib_SetLpcDeviceDecoding(NULL, 0x60, 0, dsPS2K);
#endif
   return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ProgramPchDeviceBase
//
// Description: This function initializes SB Devices Base
//
// Input:       PeiServices - Pointer to the PEI services table
//              PciCfg      - Pointer to the PCI Configuration PPI
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
ProgramPchDeviceBase (
  IN EFI_PEI_SERVICES       **PeiServices,
  IN EFI_PEI_PCI_CFG2_PPI   *PciCfg
)
{

  // Program RCBA Base
  WRITE_PCI32_SB(SB_REG_RCBA, SB_RCRB_BASE_ADDRESS | 1);//0xF0

  // Write Heci Base Address and enable Heci device
  WRITE_PCI32_HECI(ME_REG_HECI_MBAR, HECI_BASE_ADDRESS);
  WRITE_PCI16_HECI(ME_REG_PCICMD, 0x06);

  // Write Heci Base Address and enable Heci device
  WRITE_PCI32_HECI2(ME_REG_HECI_MBAR, HECI2_BASE_ADDRESS);
  WRITE_PCI16_HECI2(ME_REG_PCICMD, 0x06);

  // Program PM Base
  WRITE_PCI16_SB(SB_REG_PMBASE, PM_BASE_ADDRESS);
  WRITE_PCI8_SB(SB_REG_ACPI_CNTL, 0x80);

  // Program GPIO Base
  WRITE_PCI16_SB(SB_REG_GPIOBASE, GPIO_BASE_ADDRESS);
  WRITE_PCI8_SB(SB_REG_GC, 0x10);
  
  return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   GeneralPowerFailureHandler
//
// Description: When the PWR_FLR bit is set, it indicates the trickle power
//              from the main battery or tricle supply failed while in suspend
//              or since last boot. This bit us ub the RTC well and is cleared
//              only by RTCRST#. Software writes a "1" to clear this bit.
//              System BIUOS should follow cold boot path if PWR_FLR, GEN_RST_STS
//              or PWRBTNOR_STS is set to 1 regardless of the value in the SLP_TYP
//              field.
//
// Input:       PeiServices - Pointer to the PEI services table
//              CpuIo       - Pointer to the CPU I/O PPI
//              PciCfg      - Pointer to the PCI Configuration PPI
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
GeneralPowerFailureHandler (
  IN EFI_PEI_SERVICES       **PeiServices,
  IN EFI_PEI_CPU_IO_PPI     *CpuIo,
  IN EFI_PEI_PCI_CFG2_PPI   *PciCfg
)
{

  UINT16       DataUint16;  
  UINT8        DataUint8;

  //
  // PCH BIOS Specification 0.6.0 - Section 19.2, "Power Failure Considerations"
  // 
  // When the PWR_FLR bit is set, it indicates the trickle power from the main
  // battery or tricle supply failed while in suspend or since last boot.
  // System BIOS should follow cold boot path if PWR_FLR, GEN_RST_STS or
  // PWRBTNOR_STS is set to 1 regardless of the value in the SLP_TYP field.
  //
  DataUint16 = READ_PCI16_SB(R_PCH_LPC_GEN_PMCON_3);
  if ((DataUint16 & (B_PCH_LPC_GEN_PMCON_GEN_RST_STS | B_PCH_LPC_GEN_PMCON_PWR_FLR)) || \
      (READ_IO16_PM(R_PCH_ACPI_PM1_STS) & B_PCH_ACPI_PM1_STS_PRBTNOR)) {
    //
    // BIOS clears these bits by writing a '1' to them.
    //
    WRITE_PCI16_SB(R_PCH_LPC_GEN_PMCON_3, DataUint16);
    WRITE_IO16_PM(R_PCH_ACPI_PM1_STS, B_PCH_ACPI_PM1_STS_PRBTNOR);

    //
    // Clear Wake Status (WAK_STS) and Sleep Type (SLP_TYP)
    //
    WRITE_IO16_PM(ACPI_IOREG_PM1_STS, B_PCH_ACPI_PM1_STS_WAK);
    DataUint16 = (READ_IO16_PM(ACPI_IOREG_PM1_CNTL) & ~B_PCH_ACPI_PM1_CNT_SLP_TYP);
    WRITE_IO16_PM(ACPI_IOREG_PM1_CNTL, DataUint16);
  }

  // 
  // When the CPUPWR_FLR bit is set, it indicates VRMPWRGD signal from
  // the CPU VRM went low. This bit is now set if VRMPWRGD goes low
  // during Intel (R) SpeedStep Technology transition.
  // Software must clear this bit if set.
  //
  DataUint8 = READ_PCI8_SB(R_PCH_LPC_GEN_PMCON_2);
  if (DataUint8 & B_PCH_LPC_GEN_PMCON_SYSPWR_FLR) {
    //
    // BIOS clears this bit by writing a '0' to it.
    //
    DataUint8 &= ~B_PCH_LPC_GEN_PMCON_SYSPWR_FLR;
    WRITE_PCI8_SB(R_PCH_LPC_GEN_PMCON_2, DataUint8);
  }

  return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SetTheStateToGoAfterG3
//
// Description: Set what state (S0/S5) to go to when power is re-applied
//              after a power failure (G3 state)
//
// Input:       PeiServices - Pointer to the PEI services table
//              CpuIo       - Pointer to the CPU I/O PPI
//              PciCfg      - Pointer to the PCI Configuration PPI
//              SbSetupData - Pointer to the SbSetupData
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
SetTheStateToGoAfterG3 (
  IN EFI_PEI_SERVICES       **PeiServices,
  IN EFI_PEI_CPU_IO_PPI     *CpuIo,
  IN EFI_PEI_PCI_CFG2_PPI   *PciCfg,
  IN SB_SETUP_DATA          *SbSetupData
)
{
  UINT16                              DataUint16;
  //
  // Make sure we have a setup data, if not, just return.
  //
  if (SbSetupData == NULL) {
    return EFI_UNSUPPORTED;
  }
 
  DataUint16 = READ_PCI16_SB(R_PCH_LPC_GEN_PMCON_3);
  if(SbSetupData->LastState == 0) {
    DataUint16 |= B_PCH_LPC_GEN_PMCON_AFTERG3_EN;
  } else {
    DataUint16 &= ~B_PCH_LPC_GEN_PMCON_AFTERG3_EN;
  }

// Done in PchMisc.c
//####  DataUint16 |= (SbSetupData->SlpS4AssW << 4);

  WRITE_PCI16_SB(R_PCH_LPC_GEN_PMCON_3, DataUint16);

  return EFI_SUCCESS;
}

static UINT8 mSmbusRsvdAddresses[DIMM_SLOT_NUM] = {
  DIMM1_SMBUS_ADDRESS, 
  DIMM2_SMBUS_ADDRESS, 
  DIMM3_SMBUS_ADDRESS, 
  DIMM4_SMBUS_ADDRESS
};

static PEI_SMBUS_POLICY_PPI mSmbusPolicyPpi = {
  SMBUS_BASE_ADDRESS,
  SMBUS_BUS_DEV_FUN,
  DIMM_SLOT_NUM,
  mSmbusRsvdAddresses
};

EFI_PEI_PPI_DESCRIPTOR SmbusPolicy_PpiDescriptor = {
  EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, 
  &gPeiSmbusPolicyPpiGuid, 
  &mSmbusPolicyPpi
};
																// [EIP106687]>
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SbPcieDetectNonComplaintPcieDevice
//
// Description: 
//
// Input:       IN UINT8           Index,
//              IN PCH_PCIE_CONFIG *PcieConfig
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
SbPcieDetectNonComplaintPcieDevice (
  IN EFI_PEI_SERVICES       **PeiServices,
  IN UINT8                  Index,
  IN PCH_PCIE_CONFIG        *PcieConfig,
  IN SB_SETUP_DATA          *SbSetupData
)
{
  if ((PcieConfig->PcieSpeed[Index] == PchPcieAuto) && \
      (SbSetupData->PcieRootPortEn[Index]!= 0))
  {
     PEI_TRACE((-1,PeiServices, "Enhance Detect Non-Compliance PCIE Device @B:0|D:1C|F:%x Start .\n", Index));
     // Assign temp bus
     PEI_TRACE((-1,PeiServices, "Assign temp bus ...\n"));
     WRITE_PCI16(PCIEBRS_BUS, PCIEBRS_DEV, Index, R_PCH_PCIE_BNUM+1, 0x0101);
     // Do a dummy Write
     WRITE_PCI32(1, 0, 0, PCI_VID, 0x12345678);
        
     if (READ_PCI16(1, 0, 0, PCI_VID) == 0xFFFF) {
        PEI_TRACE((-1,PeiServices, "Can't find Device... Retrain device first.\n"));
        WRITE_PCI8(PCIEBRS_BUS, PCIEBRS_DEV, Index, R_PCH_PCIE_LCTL, B_PCH_PCIE_LCTL_LD);
        CountTime((RETRAIN_DELAY * 10), PM_BASE_ADDRESS); //delay 500us
        WRITE_PCI8(PCIEBRS_BUS, PCIEBRS_DEV, Index, R_PCH_PCIE_LCTL, B_PCH_PCIE_LCTL_RL);
        CountTime((RETRAIN_DELAY * 8000), PM_BASE_ADDRESS); //delay 400ms
        if (READ_PCI16(1, 0, 0, PCI_VID) == 0xFFFF) {
           PEI_TRACE((-1,PeiServices, "Still can't find Device in Gen2 Speed... Speed is setted in Gen1 and delay 200 ms.\n"));
           // Set Speed to Gen1
           RW_PCI8(PCIEBRS_BUS, PCIEBRS_DEV, Index, R_PCH_PCIE_LCTL2, 0x01, 0x03);
           CountTime((RETRAIN_DELAY * 4000), PM_BASE_ADDRESS); //delay 200ms
                 
           if (READ_PCI16(1, 0, 0, PCI_VID) == 0xFFFF) {
              PEI_TRACE((-1,PeiServices, "Still can't find Device in Gen1 Speed... Retrain device again !!!\n"));
              WRITE_PCI8(PCIEBRS_BUS, PCIEBRS_DEV, Index, R_PCH_PCIE_LCTL, B_PCH_PCIE_LCTL_LD);
              CountTime((RETRAIN_DELAY * 10), PM_BASE_ADDRESS); //delay 500us
              WRITE_PCI8(PCIEBRS_BUS, PCIEBRS_DEV, Index, R_PCH_PCIE_LCTL, B_PCH_PCIE_LCTL_RL);
              CountTime((RETRAIN_DELAY * 8000), PM_BASE_ADDRESS); //delay 400ms
                   
              if (READ_PCI16(1, 0, 0, PCI_VID) != 0xFFFF) PcieConfig->PcieSpeed[Index] = PchPcieGen1;
           }
           else PcieConfig->PcieSpeed[Index] = PchPcieGen1;
        }
     }

     // Remove temp bus
     PEI_TRACE((-1,PeiServices, "Remove temp bus.\n"));
     WRITE_PCI32(PCIEBRS_BUS, PCIEBRS_DEV, Index, PCI_PBUS, 0xFF000000);
     
     PEI_TRACE((-1,PeiServices, "Enhance Detect Non-Compliance PCIE Device end.\n"));
  }
}
																		// <[EIP106687]
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   InstallPchPlatformPolicyPpi
//
// Description: Install Pch Platform Policy Ppi 
//
// Input:       IN EFI_PEI_SERVICES                   **PeiServices,
//              IN SB_SETUP_DATA                      *SbSetupData
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
InstallPchPlatformPolicyPpi (
  IN EFI_PEI_SERVICES                   **PeiServices,
  IN SB_SETUP_DATA                      *SbSetupData
)
{

  UINT8                                 *SbRcba = (UINT8*)(UINTN)SB_RCRB_BASE_ADDRESS;
  EFI_STATUS                            Status;
  EFI_PEI_PPI_DESCRIPTOR                *PchPlatformPolicyPpiDesc;
  PCH_PLATFORM_POLICY_PPI               *PchPlatformPolicyPpi;
  PCH_THERMAL_MANAGEMENT                *ThermalMgmt;
  PCH_MEMORY_THROTTLING                 *MemoryThrottling;
  PCH_HPET_CONFIG                       *HpetConfig;
  PCH_IOAPIC_CONFIG                     *IoApicConfig;
  PCH_SATA_CONTROL                      *SataConfig;
  PCH_SATA_TRACE_CONFIG                 *SataTraceConfig;
  PCH_GBE_CONFIG                        *GbeConfig;
  PCH_PCIE_CONFIG                       *PcieConfig;
  PCH_USB_CONFIG                        *UsbConfig;
  EFI_BOOT_MODE                         BootMode;
  UINT8                                 Index;
  PCH_PLATFORM_DATA                     *PlatformData;
  UINT16                                LpcDeviceId = READ_PCI16_SB(R_PCH_LPC_DEVICE_ID);
  UINT16                                Data16;
  UINT8                                 PortIndex;
  UINT16                                UsbPortLength[LPTH_USB_MAX_PHYSICAL_PORTS] = {USB_PORTS_LENGTH};
  UINT8                                 UsbPortLocation[LPTH_USB_MAX_PHYSICAL_PORTS] = {USB_PORT_LOCATION_CONFIG};
  UINT8                                 UsbOverCurrentMapping[LPTH_USB_MAX_PHYSICAL_PORTS] = {USB_OVER_CURRENT_MAPPING_SETTINGS};
  UINT8                                 Usb30OverCurrentMapping[LPTH_XHCI_MAX_USB3_PORTS] = {USB30_OVER_CURRENT_MAPPING_SETTINGS};
  PCH_SERIES                            PchSeries = GetPchSeries();
  SATA_LENGTH_CONFIG                    SataLengthConfigTable[] = {SATA_PORT1_LENGTH_CONFIG, 
                                                                   SATA_PORT2_LENGTH_CONFIG, 
                                                                   SATA_PORT3_LENGTH_CONFIG, 
                                                                   SATA_PORT4_LENGTH_CONFIG, 
                                                                   SATA_PORT5_LENGTH_CONFIG, 
                                                                   SATA_PORT6_LENGTH_CONFIG};
#if defined   iME_SUPPORT && iME_SUPPORT
  EFI_PEI_READ_ONLY_VARIABLE2_PPI       *ReadOnlyVariable;
  UINTN                                 VariableSize;
  ME_BIOS_EXTENSION_SETUP               MeBiosExtensionSetupData;
  EFI_GUID                              EfiMeBiosExtensionSetupGuid    = EFI_ME_BIOS_EXTENSION_SETUP_GUID;
  CHAR16                                EfiMeBiosExtensionSetupName[]  = EFI_ME_BIOS_EXTENSION_SETUP_VARIABLE_NAME;
#endif

  BootMode = BOOT_WITH_FULL_CONFIGURATION; 

  //  Install SmbusPolicy PPI
  Status = (**PeiServices).InstallPpi (PeiServices, &SmbusPolicy_PpiDescriptor);
  ASSERT_PEI_ERROR (PeiServices, Status);

  // Allocate descriptor and PPI structures.  Since these are dynamically updated
  // we cannot do a global variable PPI.
  Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (EFI_PEI_PPI_DESCRIPTOR), &PchPlatformPolicyPpiDesc);
  ASSERT_PEI_ERROR (PeiServices, Status);
  (*PeiServices)->SetMem ((VOID*) PchPlatformPolicyPpiDesc, sizeof (EFI_PEI_PPI_DESCRIPTOR), 0);

  Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (PCH_PLATFORM_POLICY_PPI), &PchPlatformPolicyPpi);
  ASSERT_PEI_ERROR (PeiServices, Status);
  (*PeiServices)->SetMem ((VOID*) PchPlatformPolicyPpi, sizeof (PCH_PLATFORM_POLICY_PPI), 0);

  Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (PCH_HPET_CONFIG), &HpetConfig);
  ASSERT_PEI_ERROR (PeiServices, Status);
  (*PeiServices)->SetMem ((VOID*) HpetConfig, sizeof (PCH_HPET_CONFIG), 0);

  Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (PCH_THERMAL_MANAGEMENT), &ThermalMgmt);
  ASSERT_PEI_ERROR (PeiServices, Status);
  (*PeiServices)->SetMem ((VOID*) ThermalMgmt, sizeof (PCH_THERMAL_MANAGEMENT), 0);

  Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (PCH_IOAPIC_CONFIG), &IoApicConfig);
  ASSERT_PEI_ERROR (PeiServices, Status);
  (*PeiServices)->SetMem ((VOID*) IoApicConfig, sizeof (PCH_IOAPIC_CONFIG), 0);

  Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (PCH_MEMORY_THROTTLING), &MemoryThrottling);
  ASSERT_PEI_ERROR (PeiServices, Status);
  (*PeiServices)->SetMem ((VOID*) MemoryThrottling, sizeof (PCH_MEMORY_THROTTLING), 0);

  Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (PCH_SATA_CONTROL), &SataConfig);
  ASSERT_PEI_ERROR (PeiServices, Status);
  (*PeiServices)->SetMem ((VOID*) SataConfig, sizeof (PCH_SATA_CONTROL), 0);

  Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (PCH_SATA_TRACE_CONFIG), &SataTraceConfig);
  ASSERT_PEI_ERROR (PeiServices, Status);
  (*PeiServices)->SetMem ((VOID*) SataTraceConfig, sizeof (PCH_SATA_TRACE_CONFIG), 0);

  Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (PCH_GBE_CONFIG), &GbeConfig);
  ASSERT_PEI_ERROR (PeiServices, Status);
  (*PeiServices)->SetMem ((VOID*) GbeConfig, sizeof (PCH_GBE_CONFIG), 0);

  Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (PCH_PCIE_CONFIG), &PcieConfig);
  ASSERT_PEI_ERROR (PeiServices, Status);
  (*PeiServices)->SetMem ((VOID*) PcieConfig, sizeof (PCH_PCIE_CONFIG), 0);

  Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (PCH_PLATFORM_DATA), &PlatformData);
  ASSERT_PEI_ERROR (PeiServices, Status);
  (*PeiServices)->SetMem ((VOID*) PlatformData, sizeof (PCH_PLATFORM_DATA), 0);

  Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (PCH_USB_CONFIG), &UsbConfig);
  ASSERT_PEI_ERROR (PeiServices, Status);
  (*PeiServices)->SetMem ((VOID*) UsbConfig, sizeof (PCH_USB_CONFIG), 0);

  PchPlatformPolicyPpi->Revision = PCH_PLATFORM_POLICY_PPI_REVISION_4;
  PchPlatformPolicyPpi->BusNumber = 0;
  PchPlatformPolicyPpi->Rcba = SB_RCRB_BASE_ADDRESS;
  PchPlatformPolicyPpi->PmBase = PM_BASE_ADDRESS;
  PchPlatformPolicyPpi->GpioBase = GPIO_BASE_ADDRESS;
  PchPlatformPolicyPpi->Port80Route = RESERVED_PAGE_ROUTE;
  
  PchPlatformPolicyPpi->GbeConfig = GbeConfig;
  PchPlatformPolicyPpi->ThermalMgmt = ThermalMgmt;
  PchPlatformPolicyPpi->HpetConfig = HpetConfig;
  PchPlatformPolicyPpi->SataConfig = SataConfig;
  PchPlatformPolicyPpi->PcieConfig = PcieConfig;
  PchPlatformPolicyPpi->IoApicConfig = IoApicConfig;
  PchPlatformPolicyPpi->PlatformData = PlatformData;
  PchPlatformPolicyPpi->UsbConfig = UsbConfig;

//-  ThermalMgmt->ThermalBaseB = SB_THERMAL_BASE_ADDRESS;
  ThermalMgmt->MemoryThrottling = MemoryThrottling;

  GbeConfig->EnableGbe = 1;
//-  GbeConfig->GbeMemBaseAddr = 0xFFFF8000; //PCH_LAN_MBARB_BASE_ADDRESS;

  for (Index = 0; Index < GetPchMaxPciePortNum (); Index++)
    PcieConfig->PcieSpeed[Index] = PchPcieAuto;

  HpetConfig->Enable = PCH_DEVICE_ENABLE;
  HpetConfig->Base = HPET_BASE_ADDRESS;


  IoApicConfig->IoApicId = PCH_IO_APIC_ID;
  IoApicConfig->ApicRangeSelect = PCH_APIC_RANGE_SELECT;
  IoApicConfig->IoApicEntry24_39 = PCH_DEVICE_ENABLE;

  if(SbSetupData != NULL) {
      GbeConfig->EnableGbe = SbSetupData->PchLan;

      if ((READ_MEM16_RCRB(R_PCH_SPI_HSFS) & B_PCH_SPI_HSFS_FDV) == B_PCH_SPI_HSFS_FDV) {
        RESET_MEM32_RCRB(R_PCH_SPI_FDOC, (UINT32) (B_PCH_SPI_FDOC_FDSS_MASK | B_PCH_SPI_FDOC_FDSI_MASK));
        SET_MEM32_RCRB(R_PCH_SPI_FDOC, (UINT32) (V_PCH_SPI_FDOC_FDSS_PCHS | R_PCH_SPI_STRP9));
        if ((READ_MEM32_RCRB (R_PCH_SPI_FDOD) & B_PCH_SPI_STRP9_GBE_PCIE_EN) == 0) {
           GbeConfig->EnableGbe = PCH_DEVICE_DISABLE;
        }
      }

      PchPlatformPolicyPpi->Port80Route = SbSetupData->Port80Route; // 0 - Forward to LPC. 1 - Forward to PCI.
      SataConfig->SataMode = SbSetupData->SataInterfaceMode;
      SataConfig->SataTraceConfig = SataTraceConfig;
//      HpetConfig->Enable = SbSetupData->Hpet; // Force HPET enabled for MRC initialization.
																			// [EIP106687]>
      for (Index = 0; Index < GetPchMaxPciePortNum (); Index++){
         PcieConfig->PcieSpeed[Index] = SbSetupData->PcieRootPortSpeed[Index];

         //Enhance Detect Non-Compliance PCIE Device
         if ((SbSetupData->PcieRPDetectNonComplaint[Index] == 1) && (SbSetupData->PcieRootPortEn[0]!= 0))
            SbPcieDetectNonComplaintPcieDevice(PeiServices, Index, PcieConfig, SbSetupData);
      }
																			// <[EIP106687]
      //In case of recovery change the SATA mode to IDE
      (*PeiServices)->GetBootMode (PeiServices, &BootMode);
      if(BootMode == BOOT_IN_RECOVERY_MODE) {
        SataConfig->SataMode = PchSataModeIde;
      }

      SataTraceConfig->TestMode        = PCH_DEVICE_DISABLE;
      PEI_TRACE((-1,PeiServices, "SBPei SATA RxEq Policy setting:\n"));
      for( PortIndex = 0; PortIndex < GetPchMaxSataPortNum(); PortIndex++ ) {
          SataTraceConfig->PortRxEq[PortIndex].GenSpeed[0].Enable = SataLengthConfigTable[PortIndex].SataGen1RxEqEnable;
          SataTraceConfig->PortRxEq[PortIndex].GenSpeed[1].Enable = SataLengthConfigTable[PortIndex].SataGen2RxEqEnable;
          SataTraceConfig->PortRxEq[PortIndex].GenSpeed[2].Enable = SataLengthConfigTable[PortIndex].SataGen3RxEqEnable;
          SataTraceConfig->PortRxEq[PortIndex].GenSpeed[0].RxEq = SataLengthConfigTable[PortIndex].SataGen1RxEqValue;
          SataTraceConfig->PortRxEq[PortIndex].GenSpeed[1].RxEq = SataLengthConfigTable[PortIndex].SataGen2RxEqValue;
          SataTraceConfig->PortRxEq[PortIndex].GenSpeed[2].RxEq = SataLengthConfigTable[PortIndex].SataGen3RxEqValue;
          PEI_TRACE((-1,PeiServices, "SATA Port%x: %x %x %x %x %x %x\n", PortIndex,\
                        SataLengthConfigTable[PortIndex].SataGen1RxEqEnable, SataLengthConfigTable[PortIndex].SataGen1RxEqValue,\
                        SataLengthConfigTable[PortIndex].SataGen2RxEqEnable, SataLengthConfigTable[PortIndex].SataGen2RxEqValue,\
                        SataLengthConfigTable[PortIndex].SataGen3RxEqEnable, SataLengthConfigTable[PortIndex].SataGen3RxEqValue));
      }
      // 
      // Thermal configuration - Initialize policy to SETUP values.
      // 
#if defined   iME_SUPPORT && iME_SUPPORT
      MemoryThrottling->Enable = SbSetupData->TrEnabled;
#else
      MemoryThrottling->Enable = PCH_DEVICE_DISABLE;
#endif
      MemoryThrottling->TsGpioPinSetting[TsGpioC].PmsyncEnable     = TSGPIO_C_PMSYN;
      MemoryThrottling->TsGpioPinSetting[TsGpioD].PmsyncEnable     = TSGPIO_D_PMSYN;
      MemoryThrottling->TsGpioPinSetting[TsGpioC].C0TransmitEnable = TSGPIO_C_C0_TRANSMIT;
      MemoryThrottling->TsGpioPinSetting[TsGpioD].C0TransmitEnable = TSGPIO_D_C0_TRANSMIT;
      MemoryThrottling->TsGpioPinSetting[TsGpioC].PinSelection     = TSGPIO_C_PIN_SEL;
      MemoryThrottling->TsGpioPinSetting[TsGpioD].PinSelection     = TSGPIO_D_PIN_SEL;
      ///
      /// UsbConfig should be initialized based on platform configuration if UsbPrecondition feature is
      /// enabled. Otherwise, the remaining data of UsbConfig can stay in zero.
      ///
      UsbConfig->UsbPrecondition = SbSetupData->UsbPrecondition;
#ifdef RAPID_START_FLAG
      if (RapidStartResumeCheck () == TRUE) {
        ///
        /// This is RapidStart resume, skip the UsbPrecondition feature in PEI phase
        ///
    	  UsbConfig->UsbPrecondition = 0;
  	  }
#endif

#ifdef USB_PRECONDITION_ENABLE_FLAG
  ///
  /// Update Precondition option for S4 resume. 
  /// Skip Precondition for S4 resume in case this boot may not connect BIOS USB driver.
  /// If BIOS USB driver will be connected always for S4, then disable below update.
  /// To keep consistency during boot, must enabled or disabled below function in both PEI and DXE
  /// PlatformPolicyInit driver.
  ///
  if (UsbConfig->UsbPrecondition == TRUE) {
    (*PeiServices)->GetBootMode (PeiServices, &BootMode);
    if ((BootMode == BOOT_ON_S4_RESUME) || (BootMode == BOOT_IN_RECOVERY_MODE)){
      UsbConfig->UsbPrecondition = FALSE;
      PEI_TRACE((-1,PeiServices, "BootMode is BOOT_ON_S4_RESUME or BOOT_IN_RECOVERY_MODE, disable Precondition"));
    }
  }
#endif  // USB_PRECONDITION_ENABLE_FLAG

      if (UsbConfig->UsbPrecondition) {
#if defined   iAMT_SUPPORT && iAMT_SUPPORT
        UsbConfig->Ehci1Usbr         = PCH_DEVICE_DISABLE;
        UsbConfig->Ehci2Usbr         = PCH_DEVICE_DISABLE;
#else
        UsbConfig->Ehci1Usbr         = PCH_DEVICE_DISABLE;
        UsbConfig->Ehci2Usbr         = PCH_DEVICE_DISABLE;
#endif

#if defined   iME_SUPPORT && iME_SUPPORT
        Status = (*PeiServices)->LocatePpi( PeiServices,
                                            &gEfiPeiReadOnlyVariable2PpiGuid,
                                            0,
                                            NULL,
                                            &ReadOnlyVariable );
        if (ReadOnlyVariable != NULL) {
          VariableSize = sizeof (MeBiosExtensionSetupData);
          Status = ReadOnlyVariable->GetVariable( ReadOnlyVariable,
                                                  EfiMeBiosExtensionSetupName,
                                                  &EfiMeBiosExtensionSetupGuid,
                                                  NULL,
                                                  &VariableSize,
                                                  &MeBiosExtensionSetupData );
          if (!EFI_ERROR (Status)) {
            UsbConfig->Ehci1Usbr |= (MeBiosExtensionSetupData.KvmEnable & KVM_ENABLE);
            UsbConfig->Ehci2Usbr |= (MeBiosExtensionSetupData.KvmEnable & KVM_ENABLE);
          }
        }
#endif

        UsbConfig->Usb20Settings[0].Enable = SbSetupData->PchUsb20[0];
        if (PchSeries == PchLp) {
          // [ EIP219399 ]
    	  //UsbOverCurrentMapping[LPTH_USB_MAX_PHYSICAL_PORTS]=ULT_USB_OVER_CURRENT_MAPPING_SETTINGS;
          UINT8     UltUsbOverCurrentMapping[LPTH_USB_MAX_PHYSICAL_PORTS] = {ULT_USB_OVER_CURRENT_MAPPING_SETTINGS};          
          (*PeiServices)->CopyMem(UsbOverCurrentMapping, UltUsbOverCurrentMapping, LPTH_USB_MAX_PHYSICAL_PORTS);

          UsbConfig->Usb20Settings[1].Enable = PCH_DEVICE_DISABLE;
        } else {
          UsbConfig->Usb20Settings[1].Enable = SbSetupData->PchUsb20[1];
        }

        if ((UsbConfig->Usb20Settings[0].Enable == PCH_DEVICE_DISABLE) &&
            (UsbConfig->Usb20Settings[1].Enable == PCH_DEVICE_DISABLE)) {
          UsbConfig->Usb20Settings[0].Enable = PCH_DEVICE_ENABLE;
          if (PchSeries == PchLp) {
            UsbConfig->Usb20Settings[1].Enable = PCH_DEVICE_ENABLE;
          }
        }

        UsbConfig->UsbPerPortCtl = SbSetupData->PchUsbPerPortCtl;

        for (PortIndex = 0; PortIndex < GetPchUsbMaxPhysicalPortNum (); PortIndex++) {
          if (SbSetupData->PchUsbPerPortCtl != PCH_DEVICE_DISABLE) {
            UsbConfig->PortSettings[PortIndex].Enable = SbSetupData->PchUsbPort[PortIndex];
          } else {
            UsbConfig->PortSettings[PortIndex].Enable = PCH_DEVICE_ENABLE;
          }

          UsbConfig->Usb20OverCurrentPins[PortIndex]  = UsbOverCurrentMapping[PortIndex];
          UsbConfig->PortSettings[PortIndex].Usb20PortLength = UsbPortLength[PortIndex];
          UsbConfig->PortSettings[PortIndex].Location = UsbPortLocation[PortIndex];

          if (PchSeries == PchH) {
            if (IS_PCH_LPT_LPC_DEVICE_ID_DESKTOP (LpcDeviceId)) {
              if (UsbConfig->PortSettings[PortIndex].Location == PchUsbPortLocationBackPanel) {
                UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam1 = 4; //Back Panel
              } else {
                UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam1 = 3; //Front Panel        
              }

              if (UsbConfig->PortSettings[PortIndex].Location == PchUsbPortLocationBackPanel) {
                if (UsbConfig->PortSettings[PortIndex].Usb20PortLength < 0x80) {
                  UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 2; //Back Panel, less than 7.9"
                } else if (UsbConfig->PortSettings[PortIndex].Usb20PortLength < 0x130) {
                  UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 3; //Back Panel, 8"-12.9"
                } else {
                  UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 4; //Back Panel, 13" onward
                }
              } else {
                UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 2; //Front Panel
              }
            } else if (IS_PCH_LPT_LPC_DEVICE_ID_MOBILE (LpcDeviceId)) {
              	if (UsbConfig->PortSettings[PortIndex].Location == PchUsbPortLocationInternalTopology) {
          			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam1 = 5; // Internal Topology
        		} else if (UsbConfig->PortSettings[PortIndex].Location == PchUsbPortLocationDock) {
          			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam1 = 4; // Dock
        		} else {
          			if (UsbConfig->PortSettings[PortIndex].Usb20PortLength < 0x70) {
            			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam1 = 5; //Back Panel, less than 7"
          			} else {
            			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam1 = 6; //Back Panel, 7" onward
          			}
        		}

        		if (UsbConfig->PortSettings[PortIndex].Location == PchUsbPortLocationInternalTopology) {
          			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 2; // Internal Topology
        		} else if (UsbConfig->PortSettings[PortIndex].Location == PchUsbPortLocationDock) {
          			if (UsbConfig->PortSettings[PortIndex].Usb20PortLength < 0x50) {
            			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 1; //Dock, less than 5"
          			} else {
            			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 2; //Dock, 5" onward
          			}
        		} else {
          			if (UsbConfig->PortSettings[PortIndex].Usb20PortLength < 0x100) {
            			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 2; //Back Panel, less than 10"
          			} else {
            			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 3; //Back Panel, 10" onward
          			}
        		}
            }
          } else if (PchSeries == PchLp) {
            if ((UsbConfig->PortSettings[PortIndex].Location == PchUsbPortLocationBackPanel) || 
          	(UsbConfig->PortSettings[PortIndex].Location == PchUsbPortLocationMiniPciE)) {
        		if (UsbConfig->PortSettings[PortIndex].Usb20PortLength < 0x70) {
          			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam1 = 5; //Back Panel, less than 7"
        		} else {
          			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam1 = 6; //Back Panel, 7" onward
        		}
      		} else if (UsbConfig->PortSettings[PortIndex].Location == PchUsbPortLocationDock) {
        		UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam1 = 4; // Dock
      		} else {
        		UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam1 = 5; // Internal Topology
      		} 

      		if ((UsbConfig->PortSettings[PortIndex].Location == PchUsbPortLocationBackPanel) || 
          	(UsbConfig->PortSettings[PortIndex].Location == PchUsbPortLocationMiniPciE)) {
        		if (UsbConfig->PortSettings[PortIndex].Usb20PortLength < 0x100) {
          			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 2; //Back Panel, less than 10"
        		} else {
          			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 3; //Back Panel, 10" onward
        		}
      		} else if (UsbConfig->PortSettings[PortIndex].Location == PchUsbPortLocationDock) {
        		if (UsbConfig->PortSettings[PortIndex].Usb20PortLength < 0x50) {
          			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 1; //Dock, less than 5"
        		} else {
          			UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 2; //Dock, 5" onward
        		}
      		} else {
        		UsbConfig->PortSettings[PortIndex].Usb20EyeDiagramTuningParam2 = 2; // Internal Topology
      		}
          }
        }

        UsbConfig->Usb30Settings.Mode             = SbSetupData->PchUsb30Mode;

  		//
  		// Automatically disable EHCI when XHCI Mode is Enabled to save power.
  		//
  		if (UsbConfig->Usb30Settings.Mode == 1) {
    		UsbConfig->Usb20Settings[0].Enable = PCH_DEVICE_DISABLE;
    		if (PchSeries == PchH) {
      			UsbConfig->Usb20Settings[1].Enable = PCH_DEVICE_DISABLE;
    		}
  		}

        if (SbSetupData->PchUsb30Mode == 3) {
          UsbConfig->Usb30Settings.PreBootSupport = 1;
        } else {
          UsbConfig->Usb30Settings.PreBootSupport = SbSetupData->PchUsb30PreBootSupport;
        }

        if (SbSetupData->PchUsb30Mode == 4) {
          UsbConfig->Usb30Settings.Mode           = 2;
          UsbConfig->Usb30Settings.ManualMode     = PCH_DEVICE_ENABLE;
        } else {
          UsbConfig->Usb30Settings.ManualMode     = PCH_DEVICE_DISABLE;
        }

  		//
  		// XhciIdleL1 can be set to disable for LPT-LP Ax stepping to workaround USB3 hot plug will fail after 1 hot plug removal. 
  		//
  		UsbConfig->Usb30Settings.XhciIdleL1 = SbSetupData->PchUsb30IdleL1;

      //
      // Btcg is for enabling/disabling trunk clock gating.
      //
      UsbConfig->Usb30Settings.Btcg = SbSetupData->PchUsb30Btcg;

        for (PortIndex = 0; PortIndex < GetPchUsbMaxPhysicalPortNum (); PortIndex++) {
          if (SbSetupData->PchUsb20PinRoute == 1){
            UsbConfig->Usb30Settings.ManualModeUsb20PerPinRoute[PortIndex] = 0;
          } else if (SbSetupData->PchUsb20PinRoute == 2){
            UsbConfig->Usb30Settings.ManualModeUsb20PerPinRoute[PortIndex] = 1;
          } else {
            UsbConfig->Usb30Settings.ManualModeUsb20PerPinRoute[PortIndex] = SbSetupData->ManualModeUsb20PerPinRoute[PortIndex];
          }
        }

        for (PortIndex = 0; PortIndex < GetPchXhciMaxUsb3PortNum (); PortIndex++) {
          if (SbSetupData->PchUsb30PinEnable == 1){
            UsbConfig->Usb30Settings.ManualModeUsb30PerPinEnable[PortIndex] = 0;
          } else if (SbSetupData->PchUsb30PinEnable == 2){
            UsbConfig->Usb30Settings.ManualModeUsb30PerPinEnable[PortIndex] = 1;
          } else {
            UsbConfig->Usb30Settings.ManualModeUsb30PerPinEnable[PortIndex] = SbSetupData->ManualModeUsb30PerPinEnable[PortIndex];
          }
          UsbConfig->Usb30OverCurrentPins[PortIndex]   = Usb30OverCurrentMapping[PortIndex];
        }
      } // if (UsbConfig->UsbPrecondition)

    if (IS_PCH_LPT_LPC_DEVICE_ID_MOBILE (LpcDeviceId)) {
      // Save R_PCH_LPC_ENABLES in Data16.
      Data16 = READ_PCI16_SB(R_PCH_LPC_ENABLES);

      if (!(Data16 & B_PCH_LPC_ENABLES_MC_EN))
        SET_PCI16_SB(R_PCH_LPC_ENABLES, B_PCH_LPC_ENABLES_MC_EN);

      if (READ_IO8(0x66) != 0xFF) PlatformData->EcPresent = 1;

      // Restore R_PCH_LPC_ENABLES.
      WRITE_PCI16_SB(R_PCH_LPC_ENABLES, Data16);
    }

  } // (SbSetupData != NULL)

  ///
  /// PlatformData->SmmBwp value directly depends on the value of CpuConfig->Pfat
  /// (found in CpuPolicyInitPei.c file)
  /// If CpuConfig->Pfat is set to 1 (enabled) then
  ///   PlatformData->SmmBwp has to be set to 1 (enabled)
  /// This is a PFAT Security requirement that needs to be addressed
  /// If CpuConfig->Pfat is set to 0 (disabled) then
  ///   PlatformData->SmmBwp value don't care, it can be set to either
  ///   1 (enabled) or 0 (disabled) based on customer implementation
  ///
  PlatformData->SmmBwp    = 0;

  ///
  /// Temporary Memory Base Address for PCI devices to be used to initialize MMIO registers.
  /// Minimum size is 64KB bytes.
  ///
  PlatformData->TempMemBaseAddr = SB_TEMP_MMIO_BASE;

  PchPlatformPolicyPpiDesc->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
  PchPlatformPolicyPpiDesc->Ppi = PchPlatformPolicyPpi;

  //
  // Install OEM PCH Platform Policy Override PPI
  //
  PchPlatformPolicyPpiDesc->Guid = &gOemPchPlatformPolicyOverridePpiGuid;
  Status = (**PeiServices).InstallPpi (PeiServices, PchPlatformPolicyPpiDesc);
  ASSERT_PEI_ERROR (PeiServices, Status);

  //
  // Install PCH Platform Policy PPI
  //
  PchPlatformPolicyPpiDesc->Guid = &gPchPlatformPolicyPpiGuid;
  Status = (**PeiServices).InstallPpi (PeiServices, PchPlatformPolicyPpiDesc);
  ASSERT_PEI_ERROR (PeiServices, Status);

                                        // [EIP120438]>
  if ((HpetConfig->Enable) && (HpetConfig->Base != 0)) {
    WRITE_MEM32(HpetConfig->Base + 0xF0, 0);
  }
                                        // <[EIP120438]

  return EFI_SUCCESS;

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ProgramSBRegAfterMrc 
//
// Description: This function can be used to program any SB regisater after
//              memory is detected.
//
// Input:       PeiServices      - Pointer to the PEI services table
//              NotifyDescriptor - Pointer to the descriptor for the
//                                 notification event.
//              InvokePpi        - Pointer to the PPI that was installed
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS ProgramSBRegAfterMrc (
    IN EFI_PEI_SERVICES             **PeiServices,
    IN EFI_PEI_NOTIFY_DESCRIPTOR    *NotifyDescriptor,
    IN VOID                         *InvokePpi )
{
    EFI_STATUS                      Status = EFI_SUCCESS;
    IN EFI_BOOT_MODE                BootMode;
    EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadOnlyVariable;
    UINTN                           VariableSize;
    EFI_GUID                        WarmResetGuid = SB_WARM_RESET_GUID;
    CHAR16                          WarmResetVar[] = SB_WARM_RESET_VARIABLE;
    UINT32                          WarmResetFlag = 0;
#if Capsule2_0_SUPPORT
    PEI_CAPSULE_PPI                 *Capsule;
#endif

    PEI_TRACE((-1,PeiServices, "ProgramSBRegAfterMrc(): Start.\n"));

    Status = (*PeiServices)->GetBootMode( PeiServices, &BootMode );

    if (BootMode == BOOT_ON_S3_RESUME) {

        PEI_TRACE((-1,PeiServices, "ProgramSBRegAfterMrc(): S3 Resume.\n"));

        Status = (*PeiServices)->LocatePpi( PeiServices, \
                                        &gEfiPeiReadOnlyVariable2PpiGuid, \
                                        0, \
                                        NULL, \
                                        &ReadOnlyVariable );
        if (!EFI_ERROR(Status)) {
            VariableSize = sizeof(WarmResetFlag);
            Status = ReadOnlyVariable->GetVariable( ReadOnlyVariable, \
                                                    WarmResetVar, \
                                                    &WarmResetGuid, \
                                                    NULL, \
                                                    &VariableSize, \
                                                    &WarmResetFlag );
            if (WarmResetFlag == SB_WARM_RESET_TAG) {
                PEI_TRACE((-1,PeiServices, "ProgramSBRegAfterMrc(): Update BootMode.\n"));
                BootMode = BOOT_WITH_FULL_CONFIGURATION;

#if Capsule2_0_SUPPORT
                //
                // Update BootMode, if Capsule 2.0 PPI is available.
                //
                Status = (*PeiServices)->LocatePpi ( PeiServices, \
                                                     &gPeiCapsulePpiGuid, \
                                                     0, \
                                                     NULL, \
                                                     &Capsule);

                if (!EFI_ERROR(Status)) {
                  BootMode = BOOT_ON_FLASH_UPDATE;
                  Status = (*PeiServices)->NotifyPpi( PeiServices, EndOfMrcNotifyList );

                } else {
                  // Clear ACPI Sleep Type 
                  RESET_IO32_PM(ACPI_IOREG_PM1_CNTL, 0x1c00 ); // 0x04
                }

                (*PeiServices)->SetBootMode(PeiServices, BootMode);
#else
                (*PeiServices)->SetBootMode(PeiServices, BootMode);
                // Clear ACPI Sleep Type 
                RESET_IO32_PM(ACPI_IOREG_PM1_CNTL, 0x1c00 ); // 0x04
#endif
            }
        }
    }

    PEI_TRACE((-1,PeiServices, "ProgramSBRegAfterMrc(): End.\n"));

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ProgramSBRegEndOfMrc
//
// Description: This function can be used to program any SB regisater at
//              end of MRC.
//
// Input:       PeiServices      - Pointer to the PEI services table
//              NotifyDescriptor - Pointer to the descriptor for the
//                                 notification event.
//              InvokePpi        - Pointer to the PPI that was installed
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS ProgramSBRegEndOfMrc (
    IN EFI_PEI_SERVICES             **PeiServices,
    IN EFI_PEI_NOTIFY_DESCRIPTOR    *NotifyDescriptor,
    IN VOID                         *InvokePpi )
{
    IN EFI_BOOT_MODE                BootMode;
    EFI_STATUS                      Status;
    VOID                            *RecoveryModePpi;

    PEI_TRACE((-1,PeiServices, "ProgramSBRegEndOfMrc(): Start.\n"));

    Status = (*PeiServices)->LocatePpi (PeiServices, \
                                        &gRecoveryBootModeGuid, \
                                        0, \
                                        NULL, \
                                        &RecoveryModePpi);

    if (EFI_ERROR(Status)) {
      // Update Bootmode
      (*PeiServices)->GetBootMode( PeiServices, &BootMode );
      PEI_TRACE((-1,PeiServices, "Before change BootMode = %X\n", BootMode));
      BootMode = BOOT_WITH_FULL_CONFIGURATION;
      (*PeiServices)->SetBootMode(PeiServices, BootMode);
    }
    PEI_TRACE((-1,PeiServices, "ProgramSBRegEndOfMrc(): End.\n"));

    return EFI_SUCCESS;
}

#if SB_RESET_PPI_SUPPORT

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SBPEI_ResetSystem
//
// Description: This function is the reset call interface function published
//              by the reset PPI
//
// Input:       PeiServices     Pointer to the PEI services table
//
// Output:      SYSTEM RESET
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SBPEI_ResetSystem (
    IN EFI_PEI_SERVICES         **PeiServices )
{
#if WdtPei_SUPPORT
    WDT_PPI      *Wdt;
    EFI_STATUS   Status;

    Status = (*PeiServices)->LocatePpi ( PeiServices, \
                                         &gWdtPpiGuid, \
                                         0, \
                                         NULL, \
                                         &Wdt );
  
    ASSERT_PEI_ERROR (PeiServices, Status);
  
    Wdt->AllowKnownReset();
#endif

    SBLib_ResetSystem(EfiResetCold);

    // We should never get this far
    return EFI_SUCCESS;
}

#endif

#if SB_STALL_PPI_SUPPORT

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SBPEI_Stall
//
// Description: This function provides a blocking stall at least number
//              of microseconds by SB ACPI timer
//
// Input:       PeiServices  - Pointer to the PEI services table
//              This         - Pointer to the Stall PPI
//              MicroSeconds - Number of microseconds for which to stall
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SBPEI_Stall (
    IN EFI_PEI_SERVICES     **PeiServices, 
    IN EFI_PEI_STALL_PPI    *This, 
    IN UINTN                MicroSeconds )
{
    EFI_STATUS              Status;
    EFI_PEI_PCI_CFG2_PPI     *PciCfg;

    // Locate PciCfg PPI
    PciCfg = (*PeiServices)->PciCfg;

    // At this time no manipulation needed.  The value passed in is in
    // MicroSeconds(us) and that is what the library function uses

    // Call Library function that is shared with Metronome
    // Architecture Protocol


    Status = CountTime(MicroSeconds, PM_BASE_ADDRESS);

    return Status;
}

#endif

#if ATAPI_RECOVERY_SUPPORT

EFI_GUID gIdeRecoveryNativeModePpiGuid = \
                                        PEI_IDE_RECOVERY_NATIVE_MODE_PPI_GUID;

PEI_IDE_RECOVERY_NATIVE_MODE_PPI IdeRecoveryNativeModePpi = {
                                                    SB_TEMP_IO_BASE+0x200,
                                                    SB_TEMP_IO_BASE+0x282,
                                                    SB_TEMP_IO_BASE+0x300,
                                                    SB_TEMP_IO_BASE+0x382
};

EFI_PEI_PPI_DESCRIPTOR IdeRecoveryNativeModePpiDescriptor = {
    (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), \
    &gIdeRecoveryNativeModePpiGuid, &IdeRecoveryNativeModePpi
};

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   InitSATARegs
//
// Description: This function initializes SATA controller registers
//              for ATA/ATAPI recovery support.
//
// Input:       PeiServices - Pointer to the PEI services table
//              PciCfg      - Pointer to the PCI Configuration PPI
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID InitSATARegs (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_PCI_CFG2_PPI     *PciCfg
)
{
    // TODO
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   EnableAtaChannel
//
// Description: This function enables the specific channel of ATA/SATA
//              controller(s) depend on input ChannelMask
//
// Input:       PeiServices - Pointer to the PEI services table
//              This        - Pointer to the ATA Controller PPI
//              ChannelMask - Mask for the specific channel.
//
// Output:      EFI_STATUS
//
// Notes:       Normally we have to enables all chennels on ATA/SATA
//              controller(s) regardless of ChannelMask.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EnableAtaChannel (
    IN EFI_PEI_SERVICES               **PeiServices,
    IN PEI_ATA_CONTROLLER_PPI         *This,
    IN UINT8                          ChannelMask )
{
  EFI_STATUS              Status;

  // SATA 0 (B0:D31:F2)
  // Initialize the controller to IDE mode
  RW_PCI8_SATA(R_PCH_SATA_MAP, 0, B_PCH_SATA_MAP_SMS_MASK | B_PCH_SATA_PORT_TO_CONTROLLER_CFG);  // [EIP86096]>>

  //Enable the SATA2 for setting IDE mode to recovery
  RESET_MEM32_RCRB (R_PCH_RCRB_FUNC_DIS ,BIT25);						 // <<[EIP86096]

  // Initialize Primary/Secondary IDE controller to operate in Legacy mode
  RW_PCI8_SATA(R_PCH_SATA_PI_REGISTER, 0, B_PCH_SATA_PI_REGISTER_PNE | B_PCH_SATA_PI_REGISTER_SNE);

  // Enable IO space decode
  RW_PCI16_SATA(R_PCH_SATA_COMMAND, B_PCH_SATA_COMMAND_IOSE, 0);

  if ((ChannelMask & PEI_ICH_IDE_PRIMARY) == PEI_ICH_IDE_PRIMARY)
    // Enable Primary IDE Controller decode
    RW_PCI16_SATA(R_PCH_SATA_TIMP, B_PCH_SATA_TIM_IDE, 0);

  if ((ChannelMask & PEI_ICH_IDE_SECONDARY) == PEI_ICH_IDE_SECONDARY)
    // Enable Secondary IDE Controller decode
    RW_PCI16_SATA(R_PCH_SATA_TIMS, B_PCH_SATA_TIM_IDE, 0);

  // Enable SATA ports 0, 1, 2 and 3.
  RW_PCI16_SATA(R_PCH_SATA_PCS, (B_PCH_SATA_PCS_PORT0_EN | B_PCH_SATA_PCS_PORT1_EN | B_PCH_SATA_PCS_PORT2_EN | B_PCH_SATA_PCS_PORT3_EN), 0);

  // SATA 1 (B0:D31:F5)
  RW_PCI16_SATA2(R_PCH_SATA_PCMD_BAR, SB_TEMP_IO_BASE + 0x200, 0);
  RW_PCI16_SATA2(R_PCH_SATA_PCNL_BAR, SB_TEMP_IO_BASE + 0x282, 0);
  RW_PCI16_SATA2(R_PCH_SATA_SCMD_BAR, SB_TEMP_IO_BASE + 0x300, 0);
  RW_PCI16_SATA2(R_PCH_SATA_SCNL_BAR, SB_TEMP_IO_BASE + 0x382, 0);

  // Enable IO space decode
  RW_PCI16_SATA2(R_PCH_SATA_COMMAND, B_PCH_SATA_COMMAND_IOSE, 0);

  if ((ChannelMask & PEI_ICH_IDE_PRIMARY) == PEI_ICH_IDE_PRIMARY)
    // Enable Primary IDE Controller decode
    RW_PCI16_SATA2(R_PCH_SATA_TIMP, B_PCH_SATA_TIM_IDE, 0);

  if ((ChannelMask & PEI_ICH_IDE_SECONDARY) == PEI_ICH_IDE_SECONDARY)
    // Enable Secondary IDE Controller decode
    RW_PCI16_SATA2(R_PCH_SATA_TIMS, B_PCH_SATA_TIM_IDE, 0);

  // Enable SATA1 ports 0(4) and 1(5).
  RW_PCI16_SATA2(R_PCH_SATA_PCS, (B_PCH_SATA_PCS_PORT0_EN | B_PCH_SATA_PCS_PORT1_EN), 0);

  // Delay 1ms for HDD devices ready.
  CountTime(1000, PM_BASE_ADDRESS);

  Status = (**PeiServices).InstallPpi( PeiServices, \
                                         &IdeRecoveryNativeModePpiDescriptor);
  if (EFI_ERROR (Status)) return Status;

  return EFI_SUCCESS;
}
#endif // ATAPI_RECOVERY_SUPPORT



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